time-lpc32xx.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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/delay.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/kernel.h>
  23. #include <linux/of.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/sched_clock.h>
  27. #define LPC32XX_TIMER_IR 0x000
  28. #define LPC32XX_TIMER_IR_MR0INT BIT(0)
  29. #define LPC32XX_TIMER_TCR 0x004
  30. #define LPC32XX_TIMER_TCR_CEN BIT(0)
  31. #define LPC32XX_TIMER_TCR_CRST BIT(1)
  32. #define LPC32XX_TIMER_TC 0x008
  33. #define LPC32XX_TIMER_PR 0x00c
  34. #define LPC32XX_TIMER_MCR 0x014
  35. #define LPC32XX_TIMER_MCR_MR0I BIT(0)
  36. #define LPC32XX_TIMER_MCR_MR0R BIT(1)
  37. #define LPC32XX_TIMER_MCR_MR0S BIT(2)
  38. #define LPC32XX_TIMER_MR0 0x018
  39. #define LPC32XX_TIMER_CTCR 0x070
  40. struct lpc32xx_clock_event_ddata {
  41. struct clock_event_device evtdev;
  42. void __iomem *base;
  43. u32 ticks_per_jiffy;
  44. };
  45. /* Needed for the sched clock */
  46. static void __iomem *clocksource_timer_counter;
  47. static u64 notrace lpc32xx_read_sched_clock(void)
  48. {
  49. return readl(clocksource_timer_counter);
  50. }
  51. static unsigned long lpc32xx_delay_timer_read(void)
  52. {
  53. return readl(clocksource_timer_counter);
  54. }
  55. static struct delay_timer lpc32xx_delay_timer = {
  56. .read_current_timer = lpc32xx_delay_timer_read,
  57. };
  58. static int lpc32xx_clkevt_next_event(unsigned long delta,
  59. struct clock_event_device *evtdev)
  60. {
  61. struct lpc32xx_clock_event_ddata *ddata =
  62. container_of(evtdev, struct lpc32xx_clock_event_ddata, evtdev);
  63. /*
  64. * Place timer in reset and program the delta in the match
  65. * channel 0 (MR0). When the timer counter matches the value
  66. * in MR0 register the match will trigger an interrupt.
  67. * After setup the timer is released from reset and enabled.
  68. */
  69. writel_relaxed(LPC32XX_TIMER_TCR_CRST, ddata->base + LPC32XX_TIMER_TCR);
  70. writel_relaxed(delta, ddata->base + LPC32XX_TIMER_MR0);
  71. writel_relaxed(LPC32XX_TIMER_TCR_CEN, ddata->base + LPC32XX_TIMER_TCR);
  72. return 0;
  73. }
  74. static int lpc32xx_clkevt_shutdown(struct clock_event_device *evtdev)
  75. {
  76. struct lpc32xx_clock_event_ddata *ddata =
  77. container_of(evtdev, struct lpc32xx_clock_event_ddata, evtdev);
  78. /* Disable the timer */
  79. writel_relaxed(0, ddata->base + LPC32XX_TIMER_TCR);
  80. return 0;
  81. }
  82. static int lpc32xx_clkevt_oneshot(struct clock_event_device *evtdev)
  83. {
  84. struct lpc32xx_clock_event_ddata *ddata =
  85. container_of(evtdev, struct lpc32xx_clock_event_ddata, evtdev);
  86. /*
  87. * When using oneshot, we must also disable the timer
  88. * to wait for the first call to set_next_event().
  89. */
  90. writel_relaxed(0, ddata->base + LPC32XX_TIMER_TCR);
  91. /* Enable interrupt, reset on match and stop on match (MCR). */
  92. writel_relaxed(LPC32XX_TIMER_MCR_MR0I | LPC32XX_TIMER_MCR_MR0R |
  93. LPC32XX_TIMER_MCR_MR0S, ddata->base + LPC32XX_TIMER_MCR);
  94. return 0;
  95. }
  96. static int lpc32xx_clkevt_periodic(struct clock_event_device *evtdev)
  97. {
  98. struct lpc32xx_clock_event_ddata *ddata =
  99. container_of(evtdev, struct lpc32xx_clock_event_ddata, evtdev);
  100. /* Enable interrupt and reset on match. */
  101. writel_relaxed(LPC32XX_TIMER_MCR_MR0I | LPC32XX_TIMER_MCR_MR0R,
  102. ddata->base + LPC32XX_TIMER_MCR);
  103. /*
  104. * Place timer in reset and program the delta in the match
  105. * channel 0 (MR0).
  106. */
  107. writel_relaxed(LPC32XX_TIMER_TCR_CRST, ddata->base + LPC32XX_TIMER_TCR);
  108. writel_relaxed(ddata->ticks_per_jiffy, ddata->base + LPC32XX_TIMER_MR0);
  109. writel_relaxed(LPC32XX_TIMER_TCR_CEN, ddata->base + LPC32XX_TIMER_TCR);
  110. return 0;
  111. }
  112. static irqreturn_t lpc32xx_clock_event_handler(int irq, void *dev_id)
  113. {
  114. struct lpc32xx_clock_event_ddata *ddata = dev_id;
  115. /* Clear match on channel 0 */
  116. writel_relaxed(LPC32XX_TIMER_IR_MR0INT, ddata->base + LPC32XX_TIMER_IR);
  117. ddata->evtdev.event_handler(&ddata->evtdev);
  118. return IRQ_HANDLED;
  119. }
  120. static struct lpc32xx_clock_event_ddata lpc32xx_clk_event_ddata = {
  121. .evtdev = {
  122. .name = "lpc3220 clockevent",
  123. .features = CLOCK_EVT_FEAT_ONESHOT |
  124. CLOCK_EVT_FEAT_PERIODIC,
  125. .rating = 300,
  126. .set_next_event = lpc32xx_clkevt_next_event,
  127. .set_state_shutdown = lpc32xx_clkevt_shutdown,
  128. .set_state_oneshot = lpc32xx_clkevt_oneshot,
  129. .set_state_periodic = lpc32xx_clkevt_periodic,
  130. },
  131. };
  132. static int __init lpc32xx_clocksource_init(struct device_node *np)
  133. {
  134. void __iomem *base;
  135. unsigned long rate;
  136. struct clk *clk;
  137. int ret;
  138. clk = of_clk_get_by_name(np, "timerclk");
  139. if (IS_ERR(clk)) {
  140. pr_err("clock get failed (%ld)\n", PTR_ERR(clk));
  141. return PTR_ERR(clk);
  142. }
  143. ret = clk_prepare_enable(clk);
  144. if (ret) {
  145. pr_err("clock enable failed (%d)\n", ret);
  146. goto err_clk_enable;
  147. }
  148. base = of_iomap(np, 0);
  149. if (!base) {
  150. pr_err("unable to map registers\n");
  151. ret = -EADDRNOTAVAIL;
  152. goto err_iomap;
  153. }
  154. /*
  155. * Disable and reset timer then set it to free running timer
  156. * mode (CTCR) with no prescaler (PR) or match operations (MCR).
  157. * After setup the timer is released from reset and enabled.
  158. */
  159. writel_relaxed(LPC32XX_TIMER_TCR_CRST, base + LPC32XX_TIMER_TCR);
  160. writel_relaxed(0, base + LPC32XX_TIMER_PR);
  161. writel_relaxed(0, base + LPC32XX_TIMER_MCR);
  162. writel_relaxed(0, base + LPC32XX_TIMER_CTCR);
  163. writel_relaxed(LPC32XX_TIMER_TCR_CEN, base + LPC32XX_TIMER_TCR);
  164. rate = clk_get_rate(clk);
  165. ret = clocksource_mmio_init(base + LPC32XX_TIMER_TC, "lpc3220 timer",
  166. rate, 300, 32, clocksource_mmio_readl_up);
  167. if (ret) {
  168. pr_err("failed to init clocksource (%d)\n", ret);
  169. goto err_clocksource_init;
  170. }
  171. clocksource_timer_counter = base + LPC32XX_TIMER_TC;
  172. lpc32xx_delay_timer.freq = rate;
  173. register_current_timer_delay(&lpc32xx_delay_timer);
  174. sched_clock_register(lpc32xx_read_sched_clock, 32, rate);
  175. return 0;
  176. err_clocksource_init:
  177. iounmap(base);
  178. err_iomap:
  179. clk_disable_unprepare(clk);
  180. err_clk_enable:
  181. clk_put(clk);
  182. return ret;
  183. }
  184. static int __init lpc32xx_clockevent_init(struct device_node *np)
  185. {
  186. void __iomem *base;
  187. unsigned long rate;
  188. struct clk *clk;
  189. int ret, irq;
  190. clk = of_clk_get_by_name(np, "timerclk");
  191. if (IS_ERR(clk)) {
  192. pr_err("clock get failed (%ld)\n", PTR_ERR(clk));
  193. return PTR_ERR(clk);
  194. }
  195. ret = clk_prepare_enable(clk);
  196. if (ret) {
  197. pr_err("clock enable failed (%d)\n", ret);
  198. goto err_clk_enable;
  199. }
  200. base = of_iomap(np, 0);
  201. if (!base) {
  202. pr_err("unable to map registers\n");
  203. ret = -EADDRNOTAVAIL;
  204. goto err_iomap;
  205. }
  206. irq = irq_of_parse_and_map(np, 0);
  207. if (!irq) {
  208. pr_err("get irq failed\n");
  209. ret = -ENOENT;
  210. goto err_irq;
  211. }
  212. /*
  213. * Disable timer and clear any pending interrupt (IR) on match
  214. * channel 0 (MR0). Clear the prescaler as it's not used.
  215. */
  216. writel_relaxed(0, base + LPC32XX_TIMER_TCR);
  217. writel_relaxed(0, base + LPC32XX_TIMER_PR);
  218. writel_relaxed(0, base + LPC32XX_TIMER_CTCR);
  219. writel_relaxed(LPC32XX_TIMER_IR_MR0INT, base + LPC32XX_TIMER_IR);
  220. rate = clk_get_rate(clk);
  221. lpc32xx_clk_event_ddata.base = base;
  222. lpc32xx_clk_event_ddata.ticks_per_jiffy = DIV_ROUND_CLOSEST(rate, HZ);
  223. clockevents_config_and_register(&lpc32xx_clk_event_ddata.evtdev,
  224. rate, 1, -1);
  225. ret = request_irq(irq, lpc32xx_clock_event_handler,
  226. IRQF_TIMER | IRQF_IRQPOLL, "lpc3220 clockevent",
  227. &lpc32xx_clk_event_ddata);
  228. if (ret) {
  229. pr_err("request irq failed\n");
  230. goto err_irq;
  231. }
  232. return 0;
  233. err_irq:
  234. iounmap(base);
  235. err_iomap:
  236. clk_disable_unprepare(clk);
  237. err_clk_enable:
  238. clk_put(clk);
  239. return ret;
  240. }
  241. /*
  242. * This function asserts that we have exactly one clocksource and one
  243. * clock_event_device in the end.
  244. */
  245. static int __init lpc32xx_timer_init(struct device_node *np)
  246. {
  247. static int has_clocksource, has_clockevent;
  248. int ret = 0;
  249. if (!has_clocksource) {
  250. ret = lpc32xx_clocksource_init(np);
  251. if (!ret) {
  252. has_clocksource = 1;
  253. return 0;
  254. }
  255. }
  256. if (!has_clockevent) {
  257. ret = lpc32xx_clockevent_init(np);
  258. if (!ret) {
  259. has_clockevent = 1;
  260. return 0;
  261. }
  262. }
  263. return ret;
  264. }
  265. CLOCKSOURCE_OF_DECLARE(lpc32xx_timer, "nxp,lpc3220-timer", lpc32xx_timer_init);