timer-sp804.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * linux/drivers/clocksource/timer-sp.c
  3. *
  4. * Copyright (C) 1999 - 2003 ARM Limited
  5. * Copyright (C) 2000 Deep Blue Solutions Ltd
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/clk.h>
  22. #include <linux/clocksource.h>
  23. #include <linux/clockchips.h>
  24. #include <linux/err.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/irq.h>
  27. #include <linux/io.h>
  28. #include <linux/of.h>
  29. #include <linux/of_address.h>
  30. #include <linux/of_irq.h>
  31. #include <linux/sched_clock.h>
  32. #include <clocksource/timer-sp804.h>
  33. #include "timer-sp.h"
  34. static long __init sp804_get_clock_rate(struct clk *clk)
  35. {
  36. long rate;
  37. int err;
  38. err = clk_prepare(clk);
  39. if (err) {
  40. pr_err("sp804: clock failed to prepare: %d\n", err);
  41. clk_put(clk);
  42. return err;
  43. }
  44. err = clk_enable(clk);
  45. if (err) {
  46. pr_err("sp804: clock failed to enable: %d\n", err);
  47. clk_unprepare(clk);
  48. clk_put(clk);
  49. return err;
  50. }
  51. rate = clk_get_rate(clk);
  52. if (rate < 0) {
  53. pr_err("sp804: clock failed to get rate: %ld\n", rate);
  54. clk_disable(clk);
  55. clk_unprepare(clk);
  56. clk_put(clk);
  57. }
  58. return rate;
  59. }
  60. static void __iomem *sched_clock_base;
  61. static u64 notrace sp804_read(void)
  62. {
  63. return ~readl_relaxed(sched_clock_base + TIMER_VALUE);
  64. }
  65. void __init sp804_timer_disable(void __iomem *base)
  66. {
  67. writel(0, base + TIMER_CTRL);
  68. }
  69. int __init __sp804_clocksource_and_sched_clock_init(void __iomem *base,
  70. const char *name,
  71. struct clk *clk,
  72. int use_sched_clock)
  73. {
  74. long rate;
  75. if (!clk) {
  76. clk = clk_get_sys("sp804", name);
  77. if (IS_ERR(clk)) {
  78. pr_err("sp804: clock not found: %d\n",
  79. (int)PTR_ERR(clk));
  80. return PTR_ERR(clk);
  81. }
  82. }
  83. rate = sp804_get_clock_rate(clk);
  84. if (rate < 0)
  85. return -EINVAL;
  86. /* setup timer 0 as free-running clocksource */
  87. writel(0, base + TIMER_CTRL);
  88. writel(0xffffffff, base + TIMER_LOAD);
  89. writel(0xffffffff, base + TIMER_VALUE);
  90. writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
  91. base + TIMER_CTRL);
  92. clocksource_mmio_init(base + TIMER_VALUE, name,
  93. rate, 200, 32, clocksource_mmio_readl_down);
  94. if (use_sched_clock) {
  95. sched_clock_base = base;
  96. sched_clock_register(sp804_read, 32, rate);
  97. }
  98. return 0;
  99. }
  100. static void __iomem *clkevt_base;
  101. static unsigned long clkevt_reload;
  102. /*
  103. * IRQ handler for the timer
  104. */
  105. static irqreturn_t sp804_timer_interrupt(int irq, void *dev_id)
  106. {
  107. struct clock_event_device *evt = dev_id;
  108. /* clear the interrupt */
  109. writel(1, clkevt_base + TIMER_INTCLR);
  110. evt->event_handler(evt);
  111. return IRQ_HANDLED;
  112. }
  113. static inline void timer_shutdown(struct clock_event_device *evt)
  114. {
  115. writel(0, clkevt_base + TIMER_CTRL);
  116. }
  117. static int sp804_shutdown(struct clock_event_device *evt)
  118. {
  119. timer_shutdown(evt);
  120. return 0;
  121. }
  122. static int sp804_set_periodic(struct clock_event_device *evt)
  123. {
  124. unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE |
  125. TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
  126. timer_shutdown(evt);
  127. writel(clkevt_reload, clkevt_base + TIMER_LOAD);
  128. writel(ctrl, clkevt_base + TIMER_CTRL);
  129. return 0;
  130. }
  131. static int sp804_set_next_event(unsigned long next,
  132. struct clock_event_device *evt)
  133. {
  134. unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE |
  135. TIMER_CTRL_ONESHOT | TIMER_CTRL_ENABLE;
  136. writel(next, clkevt_base + TIMER_LOAD);
  137. writel(ctrl, clkevt_base + TIMER_CTRL);
  138. return 0;
  139. }
  140. static struct clock_event_device sp804_clockevent = {
  141. .features = CLOCK_EVT_FEAT_PERIODIC |
  142. CLOCK_EVT_FEAT_ONESHOT |
  143. CLOCK_EVT_FEAT_DYNIRQ,
  144. .set_state_shutdown = sp804_shutdown,
  145. .set_state_periodic = sp804_set_periodic,
  146. .set_state_oneshot = sp804_shutdown,
  147. .tick_resume = sp804_shutdown,
  148. .set_next_event = sp804_set_next_event,
  149. .rating = 300,
  150. };
  151. static struct irqaction sp804_timer_irq = {
  152. .name = "timer",
  153. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  154. .handler = sp804_timer_interrupt,
  155. .dev_id = &sp804_clockevent,
  156. };
  157. int __init __sp804_clockevents_init(void __iomem *base, unsigned int irq, struct clk *clk, const char *name)
  158. {
  159. struct clock_event_device *evt = &sp804_clockevent;
  160. long rate;
  161. if (!clk)
  162. clk = clk_get_sys("sp804", name);
  163. if (IS_ERR(clk)) {
  164. pr_err("sp804: %s clock not found: %d\n", name,
  165. (int)PTR_ERR(clk));
  166. return PTR_ERR(clk);
  167. }
  168. rate = sp804_get_clock_rate(clk);
  169. if (rate < 0)
  170. return -EINVAL;
  171. clkevt_base = base;
  172. clkevt_reload = DIV_ROUND_CLOSEST(rate, HZ);
  173. evt->name = name;
  174. evt->irq = irq;
  175. evt->cpumask = cpu_possible_mask;
  176. writel(0, base + TIMER_CTRL);
  177. setup_irq(irq, &sp804_timer_irq);
  178. clockevents_config_and_register(evt, rate, 0xf, 0xffffffff);
  179. return 0;
  180. }
  181. static int __init sp804_of_init(struct device_node *np)
  182. {
  183. static bool initialized = false;
  184. void __iomem *base;
  185. int irq, ret = -EINVAL;
  186. u32 irq_num = 0;
  187. struct clk *clk1, *clk2;
  188. const char *name = of_get_property(np, "compatible", NULL);
  189. base = of_iomap(np, 0);
  190. if (!base)
  191. return -ENXIO;
  192. /* Ensure timers are disabled */
  193. writel(0, base + TIMER_CTRL);
  194. writel(0, base + TIMER_2_BASE + TIMER_CTRL);
  195. if (initialized || !of_device_is_available(np)) {
  196. ret = -EINVAL;
  197. goto err;
  198. }
  199. clk1 = of_clk_get(np, 0);
  200. if (IS_ERR(clk1))
  201. clk1 = NULL;
  202. /* Get the 2nd clock if the timer has 3 timer clocks */
  203. if (of_count_phandle_with_args(np, "clocks", "#clock-cells") == 3) {
  204. clk2 = of_clk_get(np, 1);
  205. if (IS_ERR(clk2)) {
  206. pr_err("sp804: %s clock not found: %d\n", np->name,
  207. (int)PTR_ERR(clk2));
  208. clk2 = NULL;
  209. }
  210. } else
  211. clk2 = clk1;
  212. irq = irq_of_parse_and_map(np, 0);
  213. if (irq <= 0)
  214. goto err;
  215. of_property_read_u32(np, "arm,sp804-has-irq", &irq_num);
  216. if (irq_num == 2) {
  217. ret = __sp804_clockevents_init(base + TIMER_2_BASE, irq, clk2, name);
  218. if (ret)
  219. goto err;
  220. ret = __sp804_clocksource_and_sched_clock_init(base, name, clk1, 1);
  221. if (ret)
  222. goto err;
  223. } else {
  224. ret = __sp804_clockevents_init(base, irq, clk1 , name);
  225. if (ret)
  226. goto err;
  227. ret =__sp804_clocksource_and_sched_clock_init(base + TIMER_2_BASE,
  228. name, clk2, 1);
  229. if (ret)
  230. goto err;
  231. }
  232. initialized = true;
  233. return 0;
  234. err:
  235. iounmap(base);
  236. return ret;
  237. }
  238. CLOCKSOURCE_OF_DECLARE(sp804, "arm,sp804", sp804_of_init);
  239. static int __init integrator_cp_of_init(struct device_node *np)
  240. {
  241. static int init_count = 0;
  242. void __iomem *base;
  243. int irq, ret = -EINVAL;
  244. const char *name = of_get_property(np, "compatible", NULL);
  245. struct clk *clk;
  246. base = of_iomap(np, 0);
  247. if (!base) {
  248. pr_err("Failed to iomap");
  249. return -ENXIO;
  250. }
  251. clk = of_clk_get(np, 0);
  252. if (IS_ERR(clk)) {
  253. pr_err("Failed to get clock");
  254. return PTR_ERR(clk);
  255. }
  256. /* Ensure timer is disabled */
  257. writel(0, base + TIMER_CTRL);
  258. if (init_count == 2 || !of_device_is_available(np))
  259. goto err;
  260. if (!init_count) {
  261. ret = __sp804_clocksource_and_sched_clock_init(base, name, clk, 0);
  262. if (ret)
  263. goto err;
  264. } else {
  265. irq = irq_of_parse_and_map(np, 0);
  266. if (irq <= 0)
  267. goto err;
  268. ret = __sp804_clockevents_init(base, irq, clk, name);
  269. if (ret)
  270. goto err;
  271. }
  272. init_count++;
  273. return 0;
  274. err:
  275. iounmap(base);
  276. return ret;
  277. }
  278. CLOCKSOURCE_OF_DECLARE(intcp, "arm,integrator-cp-timer", integrator_cp_of_init);