time-lpc32xx.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Clocksource driver for NXP LPC32xx/18xx/43xx timer
  3. *
  4. * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
  5. *
  6. * Based on:
  7. * time-efm32 Copyright (C) 2013 Pengutronix
  8. * mach-lpc32xx/timer.c Copyright (C) 2009 - 2010 NXP Semiconductors
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. *
  14. */
  15. #define pr_fmt(fmt) "%s: " fmt, __func__
  16. #include <linux/clk.h>
  17. #include <linux/clockchips.h>
  18. #include <linux/clocksource.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irq.h>
  21. #include <linux/kernel.h>
  22. #include <linux/of.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/sched_clock.h>
  26. #define LPC32XX_TIMER_IR 0x000
  27. #define LPC32XX_TIMER_IR_MR0INT BIT(0)
  28. #define LPC32XX_TIMER_TCR 0x004
  29. #define LPC32XX_TIMER_TCR_CEN BIT(0)
  30. #define LPC32XX_TIMER_TCR_CRST BIT(1)
  31. #define LPC32XX_TIMER_TC 0x008
  32. #define LPC32XX_TIMER_PR 0x00c
  33. #define LPC32XX_TIMER_MCR 0x014
  34. #define LPC32XX_TIMER_MCR_MR0I BIT(0)
  35. #define LPC32XX_TIMER_MCR_MR0R BIT(1)
  36. #define LPC32XX_TIMER_MCR_MR0S BIT(2)
  37. #define LPC32XX_TIMER_MR0 0x018
  38. #define LPC32XX_TIMER_CTCR 0x070
  39. struct lpc32xx_clock_event_ddata {
  40. struct clock_event_device evtdev;
  41. void __iomem *base;
  42. };
  43. /* Needed for the sched clock */
  44. static void __iomem *clocksource_timer_counter;
  45. static u64 notrace lpc32xx_read_sched_clock(void)
  46. {
  47. return readl(clocksource_timer_counter);
  48. }
  49. static int lpc32xx_clkevt_next_event(unsigned long delta,
  50. struct clock_event_device *evtdev)
  51. {
  52. struct lpc32xx_clock_event_ddata *ddata =
  53. container_of(evtdev, struct lpc32xx_clock_event_ddata, evtdev);
  54. /*
  55. * Place timer in reset and program the delta in the prescale
  56. * register (PR). When the prescale counter matches the value
  57. * in PR the counter register is incremented and the compare
  58. * match will trigger. After setup the timer is released from
  59. * reset and enabled.
  60. */
  61. writel_relaxed(LPC32XX_TIMER_TCR_CRST, ddata->base + LPC32XX_TIMER_TCR);
  62. writel_relaxed(delta, ddata->base + LPC32XX_TIMER_PR);
  63. writel_relaxed(LPC32XX_TIMER_TCR_CEN, ddata->base + LPC32XX_TIMER_TCR);
  64. return 0;
  65. }
  66. static int lpc32xx_clkevt_shutdown(struct clock_event_device *evtdev)
  67. {
  68. struct lpc32xx_clock_event_ddata *ddata =
  69. container_of(evtdev, struct lpc32xx_clock_event_ddata, evtdev);
  70. /* Disable the timer */
  71. writel_relaxed(0, ddata->base + LPC32XX_TIMER_TCR);
  72. return 0;
  73. }
  74. static int lpc32xx_clkevt_oneshot(struct clock_event_device *evtdev)
  75. {
  76. /*
  77. * When using oneshot, we must also disable the timer
  78. * to wait for the first call to set_next_event().
  79. */
  80. return lpc32xx_clkevt_shutdown(evtdev);
  81. }
  82. static irqreturn_t lpc32xx_clock_event_handler(int irq, void *dev_id)
  83. {
  84. struct lpc32xx_clock_event_ddata *ddata = dev_id;
  85. /* Clear match on channel 0 */
  86. writel_relaxed(LPC32XX_TIMER_IR_MR0INT, ddata->base + LPC32XX_TIMER_IR);
  87. ddata->evtdev.event_handler(&ddata->evtdev);
  88. return IRQ_HANDLED;
  89. }
  90. static struct lpc32xx_clock_event_ddata lpc32xx_clk_event_ddata = {
  91. .evtdev = {
  92. .name = "lpc3220 clockevent",
  93. .features = CLOCK_EVT_FEAT_ONESHOT,
  94. .rating = 300,
  95. .set_next_event = lpc32xx_clkevt_next_event,
  96. .set_state_shutdown = lpc32xx_clkevt_shutdown,
  97. .set_state_oneshot = lpc32xx_clkevt_oneshot,
  98. },
  99. };
  100. static int __init lpc32xx_clocksource_init(struct device_node *np)
  101. {
  102. void __iomem *base;
  103. unsigned long rate;
  104. struct clk *clk;
  105. int ret;
  106. clk = of_clk_get_by_name(np, "timerclk");
  107. if (IS_ERR(clk)) {
  108. pr_err("clock get failed (%lu)\n", PTR_ERR(clk));
  109. return PTR_ERR(clk);
  110. }
  111. ret = clk_prepare_enable(clk);
  112. if (ret) {
  113. pr_err("clock enable failed (%d)\n", ret);
  114. goto err_clk_enable;
  115. }
  116. base = of_iomap(np, 0);
  117. if (!base) {
  118. pr_err("unable to map registers\n");
  119. ret = -EADDRNOTAVAIL;
  120. goto err_iomap;
  121. }
  122. /*
  123. * Disable and reset timer then set it to free running timer
  124. * mode (CTCR) with no prescaler (PR) or match operations (MCR).
  125. * After setup the timer is released from reset and enabled.
  126. */
  127. writel_relaxed(LPC32XX_TIMER_TCR_CRST, base + LPC32XX_TIMER_TCR);
  128. writel_relaxed(0, base + LPC32XX_TIMER_PR);
  129. writel_relaxed(0, base + LPC32XX_TIMER_MCR);
  130. writel_relaxed(0, base + LPC32XX_TIMER_CTCR);
  131. writel_relaxed(LPC32XX_TIMER_TCR_CEN, base + LPC32XX_TIMER_TCR);
  132. rate = clk_get_rate(clk);
  133. ret = clocksource_mmio_init(base + LPC32XX_TIMER_TC, "lpc3220 timer",
  134. rate, 300, 32, clocksource_mmio_readl_up);
  135. if (ret) {
  136. pr_err("failed to init clocksource (%d)\n", ret);
  137. goto err_clocksource_init;
  138. }
  139. clocksource_timer_counter = base + LPC32XX_TIMER_TC;
  140. sched_clock_register(lpc32xx_read_sched_clock, 32, rate);
  141. return 0;
  142. err_clocksource_init:
  143. iounmap(base);
  144. err_iomap:
  145. clk_disable_unprepare(clk);
  146. err_clk_enable:
  147. clk_put(clk);
  148. return ret;
  149. }
  150. static int __init lpc32xx_clockevent_init(struct device_node *np)
  151. {
  152. void __iomem *base;
  153. unsigned long rate;
  154. struct clk *clk;
  155. int ret, irq;
  156. clk = of_clk_get_by_name(np, "timerclk");
  157. if (IS_ERR(clk)) {
  158. pr_err("clock get failed (%lu)\n", PTR_ERR(clk));
  159. return PTR_ERR(clk);
  160. }
  161. ret = clk_prepare_enable(clk);
  162. if (ret) {
  163. pr_err("clock enable failed (%d)\n", ret);
  164. goto err_clk_enable;
  165. }
  166. base = of_iomap(np, 0);
  167. if (!base) {
  168. pr_err("unable to map registers\n");
  169. ret = -EADDRNOTAVAIL;
  170. goto err_iomap;
  171. }
  172. irq = irq_of_parse_and_map(np, 0);
  173. if (!irq) {
  174. pr_err("get irq failed\n");
  175. ret = -ENOENT;
  176. goto err_irq;
  177. }
  178. /*
  179. * Disable timer and clear any pending interrupt (IR) on match
  180. * channel 0 (MR0). Configure a compare match value of 1 on MR0
  181. * and enable interrupt, reset on match and stop on match (MCR).
  182. */
  183. writel_relaxed(0, base + LPC32XX_TIMER_TCR);
  184. writel_relaxed(0, base + LPC32XX_TIMER_CTCR);
  185. writel_relaxed(LPC32XX_TIMER_IR_MR0INT, base + LPC32XX_TIMER_IR);
  186. writel_relaxed(1, base + LPC32XX_TIMER_MR0);
  187. writel_relaxed(LPC32XX_TIMER_MCR_MR0I | LPC32XX_TIMER_MCR_MR0R |
  188. LPC32XX_TIMER_MCR_MR0S, base + LPC32XX_TIMER_MCR);
  189. rate = clk_get_rate(clk);
  190. lpc32xx_clk_event_ddata.base = base;
  191. clockevents_config_and_register(&lpc32xx_clk_event_ddata.evtdev,
  192. rate, 1, -1);
  193. ret = request_irq(irq, lpc32xx_clock_event_handler,
  194. IRQF_TIMER | IRQF_IRQPOLL, "lpc3220 clockevent",
  195. &lpc32xx_clk_event_ddata);
  196. if (ret) {
  197. pr_err("request irq failed\n");
  198. goto err_irq;
  199. }
  200. return 0;
  201. err_irq:
  202. iounmap(base);
  203. err_iomap:
  204. clk_disable_unprepare(clk);
  205. err_clk_enable:
  206. clk_put(clk);
  207. return ret;
  208. }
  209. /*
  210. * This function asserts that we have exactly one clocksource and one
  211. * clock_event_device in the end.
  212. */
  213. static void __init lpc32xx_timer_init(struct device_node *np)
  214. {
  215. static int has_clocksource, has_clockevent;
  216. int ret;
  217. if (!has_clocksource) {
  218. ret = lpc32xx_clocksource_init(np);
  219. if (!ret) {
  220. has_clocksource = 1;
  221. return;
  222. }
  223. }
  224. if (!has_clockevent) {
  225. ret = lpc32xx_clockevent_init(np);
  226. if (!ret) {
  227. has_clockevent = 1;
  228. return;
  229. }
  230. }
  231. }
  232. CLOCKSOURCE_OF_DECLARE(lpc32xx_timer, "nxp,lpc3220-timer", lpc32xx_timer_init);