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