timer-sp804.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. void __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;
  81. }
  82. }
  83. rate = sp804_get_clock_rate(clk);
  84. if (rate < 0)
  85. return;
  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. }
  99. static void __iomem *clkevt_base;
  100. static unsigned long clkevt_reload;
  101. /*
  102. * IRQ handler for the timer
  103. */
  104. static irqreturn_t sp804_timer_interrupt(int irq, void *dev_id)
  105. {
  106. struct clock_event_device *evt = dev_id;
  107. /* clear the interrupt */
  108. writel(1, clkevt_base + TIMER_INTCLR);
  109. evt->event_handler(evt);
  110. return IRQ_HANDLED;
  111. }
  112. static inline void timer_shutdown(struct clock_event_device *evt)
  113. {
  114. writel(0, clkevt_base + TIMER_CTRL);
  115. }
  116. static int sp804_shutdown(struct clock_event_device *evt)
  117. {
  118. timer_shutdown(evt);
  119. return 0;
  120. }
  121. static int sp804_set_periodic(struct clock_event_device *evt)
  122. {
  123. unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE |
  124. TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
  125. timer_shutdown(evt);
  126. writel(clkevt_reload, clkevt_base + TIMER_LOAD);
  127. writel(ctrl, clkevt_base + TIMER_CTRL);
  128. return 0;
  129. }
  130. static int sp804_set_next_event(unsigned long next,
  131. struct clock_event_device *evt)
  132. {
  133. unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE |
  134. TIMER_CTRL_ONESHOT | TIMER_CTRL_ENABLE;
  135. writel(next, clkevt_base + TIMER_LOAD);
  136. writel(ctrl, clkevt_base + TIMER_CTRL);
  137. return 0;
  138. }
  139. static struct clock_event_device sp804_clockevent = {
  140. .features = CLOCK_EVT_FEAT_PERIODIC |
  141. CLOCK_EVT_FEAT_ONESHOT |
  142. CLOCK_EVT_FEAT_DYNIRQ,
  143. .set_state_shutdown = sp804_shutdown,
  144. .set_state_periodic = sp804_set_periodic,
  145. .set_state_oneshot = sp804_shutdown,
  146. .tick_resume = sp804_shutdown,
  147. .set_next_event = sp804_set_next_event,
  148. .rating = 300,
  149. };
  150. static struct irqaction sp804_timer_irq = {
  151. .name = "timer",
  152. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  153. .handler = sp804_timer_interrupt,
  154. .dev_id = &sp804_clockevent,
  155. };
  156. void __init __sp804_clockevents_init(void __iomem *base, unsigned int irq, struct clk *clk, const char *name)
  157. {
  158. struct clock_event_device *evt = &sp804_clockevent;
  159. long rate;
  160. if (!clk)
  161. clk = clk_get_sys("sp804", name);
  162. if (IS_ERR(clk)) {
  163. pr_err("sp804: %s clock not found: %d\n", name,
  164. (int)PTR_ERR(clk));
  165. return;
  166. }
  167. rate = sp804_get_clock_rate(clk);
  168. if (rate < 0)
  169. return;
  170. clkevt_base = base;
  171. clkevt_reload = DIV_ROUND_CLOSEST(rate, HZ);
  172. evt->name = name;
  173. evt->irq = irq;
  174. evt->cpumask = cpu_possible_mask;
  175. writel(0, base + TIMER_CTRL);
  176. setup_irq(irq, &sp804_timer_irq);
  177. clockevents_config_and_register(evt, rate, 0xf, 0xffffffff);
  178. }
  179. static void __init sp804_of_init(struct device_node *np)
  180. {
  181. static bool initialized = false;
  182. void __iomem *base;
  183. int irq;
  184. u32 irq_num = 0;
  185. struct clk *clk1, *clk2;
  186. const char *name = of_get_property(np, "compatible", NULL);
  187. base = of_iomap(np, 0);
  188. if (WARN_ON(!base))
  189. return;
  190. /* Ensure timers are disabled */
  191. writel(0, base + TIMER_CTRL);
  192. writel(0, base + TIMER_2_BASE + TIMER_CTRL);
  193. if (initialized || !of_device_is_available(np))
  194. goto err;
  195. clk1 = of_clk_get(np, 0);
  196. if (IS_ERR(clk1))
  197. clk1 = NULL;
  198. /* Get the 2nd clock if the timer has 3 timer clocks */
  199. if (of_count_phandle_with_args(np, "clocks", "#clock-cells") == 3) {
  200. clk2 = of_clk_get(np, 1);
  201. if (IS_ERR(clk2)) {
  202. pr_err("sp804: %s clock not found: %d\n", np->name,
  203. (int)PTR_ERR(clk2));
  204. clk2 = NULL;
  205. }
  206. } else
  207. clk2 = clk1;
  208. irq = irq_of_parse_and_map(np, 0);
  209. if (irq <= 0)
  210. goto err;
  211. of_property_read_u32(np, "arm,sp804-has-irq", &irq_num);
  212. if (irq_num == 2) {
  213. __sp804_clockevents_init(base + TIMER_2_BASE, irq, clk2, name);
  214. __sp804_clocksource_and_sched_clock_init(base, name, clk1, 1);
  215. } else {
  216. __sp804_clockevents_init(base, irq, clk1 , name);
  217. __sp804_clocksource_and_sched_clock_init(base + TIMER_2_BASE,
  218. name, clk2, 1);
  219. }
  220. initialized = true;
  221. return;
  222. err:
  223. iounmap(base);
  224. }
  225. CLOCKSOURCE_OF_DECLARE(sp804, "arm,sp804", sp804_of_init);
  226. static void __init integrator_cp_of_init(struct device_node *np)
  227. {
  228. static int init_count = 0;
  229. void __iomem *base;
  230. int irq;
  231. const char *name = of_get_property(np, "compatible", NULL);
  232. struct clk *clk;
  233. base = of_iomap(np, 0);
  234. if (WARN_ON(!base))
  235. return;
  236. clk = of_clk_get(np, 0);
  237. if (WARN_ON(IS_ERR(clk)))
  238. return;
  239. /* Ensure timer is disabled */
  240. writel(0, base + TIMER_CTRL);
  241. if (init_count == 2 || !of_device_is_available(np))
  242. goto err;
  243. if (!init_count)
  244. __sp804_clocksource_and_sched_clock_init(base, name, clk, 0);
  245. else {
  246. irq = irq_of_parse_and_map(np, 0);
  247. if (irq <= 0)
  248. goto err;
  249. __sp804_clockevents_init(base, irq, clk, name);
  250. }
  251. init_count++;
  252. return;
  253. err:
  254. iounmap(base);
  255. }
  256. CLOCKSOURCE_OF_DECLARE(intcp, "arm,integrator-cp-timer", integrator_cp_of_init);