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_clk.h>
  31. #include <linux/of_irq.h>
  32. #include <linux/sched_clock.h>
  33. #include <clocksource/timer-sp804.h>
  34. #include "timer-sp.h"
  35. static long __init sp804_get_clock_rate(struct clk *clk)
  36. {
  37. long rate;
  38. int err;
  39. err = clk_prepare(clk);
  40. if (err) {
  41. pr_err("sp804: clock failed to prepare: %d\n", err);
  42. clk_put(clk);
  43. return err;
  44. }
  45. err = clk_enable(clk);
  46. if (err) {
  47. pr_err("sp804: clock failed to enable: %d\n", err);
  48. clk_unprepare(clk);
  49. clk_put(clk);
  50. return err;
  51. }
  52. rate = clk_get_rate(clk);
  53. if (rate < 0) {
  54. pr_err("sp804: clock failed to get rate: %ld\n", rate);
  55. clk_disable(clk);
  56. clk_unprepare(clk);
  57. clk_put(clk);
  58. }
  59. return rate;
  60. }
  61. static void __iomem *sched_clock_base;
  62. static u64 notrace sp804_read(void)
  63. {
  64. return ~readl_relaxed(sched_clock_base + TIMER_VALUE);
  65. }
  66. void __init sp804_timer_disable(void __iomem *base)
  67. {
  68. writel(0, base + TIMER_CTRL);
  69. }
  70. int __init __sp804_clocksource_and_sched_clock_init(void __iomem *base,
  71. const char *name,
  72. struct clk *clk,
  73. int use_sched_clock)
  74. {
  75. long rate;
  76. if (!clk) {
  77. clk = clk_get_sys("sp804", name);
  78. if (IS_ERR(clk)) {
  79. pr_err("sp804: clock not found: %d\n",
  80. (int)PTR_ERR(clk));
  81. return PTR_ERR(clk);
  82. }
  83. }
  84. rate = sp804_get_clock_rate(clk);
  85. if (rate < 0)
  86. return -EINVAL;
  87. /* setup timer 0 as free-running clocksource */
  88. writel(0, base + TIMER_CTRL);
  89. writel(0xffffffff, base + TIMER_LOAD);
  90. writel(0xffffffff, base + TIMER_VALUE);
  91. writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
  92. base + TIMER_CTRL);
  93. clocksource_mmio_init(base + TIMER_VALUE, name,
  94. rate, 200, 32, clocksource_mmio_readl_down);
  95. if (use_sched_clock) {
  96. sched_clock_base = base;
  97. sched_clock_register(sp804_read, 32, rate);
  98. }
  99. return 0;
  100. }
  101. static void __iomem *clkevt_base;
  102. static unsigned long clkevt_reload;
  103. /*
  104. * IRQ handler for the timer
  105. */
  106. static irqreturn_t sp804_timer_interrupt(int irq, void *dev_id)
  107. {
  108. struct clock_event_device *evt = dev_id;
  109. /* clear the interrupt */
  110. writel(1, clkevt_base + TIMER_INTCLR);
  111. evt->event_handler(evt);
  112. return IRQ_HANDLED;
  113. }
  114. static inline void timer_shutdown(struct clock_event_device *evt)
  115. {
  116. writel(0, clkevt_base + TIMER_CTRL);
  117. }
  118. static int sp804_shutdown(struct clock_event_device *evt)
  119. {
  120. timer_shutdown(evt);
  121. return 0;
  122. }
  123. static int sp804_set_periodic(struct clock_event_device *evt)
  124. {
  125. unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE |
  126. TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
  127. timer_shutdown(evt);
  128. writel(clkevt_reload, clkevt_base + TIMER_LOAD);
  129. writel(ctrl, clkevt_base + TIMER_CTRL);
  130. return 0;
  131. }
  132. static int sp804_set_next_event(unsigned long next,
  133. struct clock_event_device *evt)
  134. {
  135. unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE |
  136. TIMER_CTRL_ONESHOT | TIMER_CTRL_ENABLE;
  137. writel(next, clkevt_base + TIMER_LOAD);
  138. writel(ctrl, clkevt_base + TIMER_CTRL);
  139. return 0;
  140. }
  141. static struct clock_event_device sp804_clockevent = {
  142. .features = CLOCK_EVT_FEAT_PERIODIC |
  143. CLOCK_EVT_FEAT_ONESHOT |
  144. CLOCK_EVT_FEAT_DYNIRQ,
  145. .set_state_shutdown = sp804_shutdown,
  146. .set_state_periodic = sp804_set_periodic,
  147. .set_state_oneshot = sp804_shutdown,
  148. .tick_resume = sp804_shutdown,
  149. .set_next_event = sp804_set_next_event,
  150. .rating = 300,
  151. };
  152. static struct irqaction sp804_timer_irq = {
  153. .name = "timer",
  154. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  155. .handler = sp804_timer_interrupt,
  156. .dev_id = &sp804_clockevent,
  157. };
  158. int __init __sp804_clockevents_init(void __iomem *base, unsigned int irq, struct clk *clk, const char *name)
  159. {
  160. struct clock_event_device *evt = &sp804_clockevent;
  161. long rate;
  162. if (!clk)
  163. clk = clk_get_sys("sp804", name);
  164. if (IS_ERR(clk)) {
  165. pr_err("sp804: %s clock not found: %d\n", name,
  166. (int)PTR_ERR(clk));
  167. return PTR_ERR(clk);
  168. }
  169. rate = sp804_get_clock_rate(clk);
  170. if (rate < 0)
  171. return -EINVAL;
  172. clkevt_base = base;
  173. clkevt_reload = DIV_ROUND_CLOSEST(rate, HZ);
  174. evt->name = name;
  175. evt->irq = irq;
  176. evt->cpumask = cpu_possible_mask;
  177. writel(0, base + TIMER_CTRL);
  178. setup_irq(irq, &sp804_timer_irq);
  179. clockevents_config_and_register(evt, rate, 0xf, 0xffffffff);
  180. return 0;
  181. }
  182. static int __init sp804_of_init(struct device_node *np)
  183. {
  184. static bool initialized = false;
  185. void __iomem *base;
  186. int irq, ret = -EINVAL;
  187. u32 irq_num = 0;
  188. struct clk *clk1, *clk2;
  189. const char *name = of_get_property(np, "compatible", NULL);
  190. base = of_iomap(np, 0);
  191. if (!base)
  192. return -ENXIO;
  193. /* Ensure timers are disabled */
  194. writel(0, base + TIMER_CTRL);
  195. writel(0, base + TIMER_2_BASE + TIMER_CTRL);
  196. if (initialized || !of_device_is_available(np)) {
  197. ret = -EINVAL;
  198. goto err;
  199. }
  200. clk1 = of_clk_get(np, 0);
  201. if (IS_ERR(clk1))
  202. clk1 = NULL;
  203. /* Get the 2nd clock if the timer has 3 timer clocks */
  204. if (of_clk_get_parent_count(np) == 3) {
  205. clk2 = of_clk_get(np, 1);
  206. if (IS_ERR(clk2)) {
  207. pr_err("sp804: %s clock not found: %d\n", np->name,
  208. (int)PTR_ERR(clk2));
  209. clk2 = NULL;
  210. }
  211. } else
  212. clk2 = clk1;
  213. irq = irq_of_parse_and_map(np, 0);
  214. if (irq <= 0)
  215. goto err;
  216. of_property_read_u32(np, "arm,sp804-has-irq", &irq_num);
  217. if (irq_num == 2) {
  218. ret = __sp804_clockevents_init(base + TIMER_2_BASE, irq, clk2, name);
  219. if (ret)
  220. goto err;
  221. ret = __sp804_clocksource_and_sched_clock_init(base, name, clk1, 1);
  222. if (ret)
  223. goto err;
  224. } else {
  225. ret = __sp804_clockevents_init(base, irq, clk1 , name);
  226. if (ret)
  227. goto err;
  228. ret =__sp804_clocksource_and_sched_clock_init(base + TIMER_2_BASE,
  229. name, clk2, 1);
  230. if (ret)
  231. goto err;
  232. }
  233. initialized = true;
  234. return 0;
  235. err:
  236. iounmap(base);
  237. return ret;
  238. }
  239. TIMER_OF_DECLARE(sp804, "arm,sp804", sp804_of_init);
  240. static int __init integrator_cp_of_init(struct device_node *np)
  241. {
  242. static int init_count = 0;
  243. void __iomem *base;
  244. int irq, ret = -EINVAL;
  245. const char *name = of_get_property(np, "compatible", NULL);
  246. struct clk *clk;
  247. base = of_iomap(np, 0);
  248. if (!base) {
  249. pr_err("Failed to iomap\n");
  250. return -ENXIO;
  251. }
  252. clk = of_clk_get(np, 0);
  253. if (IS_ERR(clk)) {
  254. pr_err("Failed to get clock\n");
  255. return PTR_ERR(clk);
  256. }
  257. /* Ensure timer is disabled */
  258. writel(0, base + TIMER_CTRL);
  259. if (init_count == 2 || !of_device_is_available(np))
  260. goto err;
  261. if (!init_count) {
  262. ret = __sp804_clocksource_and_sched_clock_init(base, name, clk, 0);
  263. if (ret)
  264. goto err;
  265. } else {
  266. irq = irq_of_parse_and_map(np, 0);
  267. if (irq <= 0)
  268. goto err;
  269. ret = __sp804_clockevents_init(base, irq, clk, name);
  270. if (ret)
  271. goto err;
  272. }
  273. init_count++;
  274. return 0;
  275. err:
  276. iounmap(base);
  277. return ret;
  278. }
  279. TIMER_OF_DECLARE(intcp, "arm,integrator-cp-timer", integrator_cp_of_init);