nomadik-mtu.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2008 STMicroelectronics
  4. * Copyright (C) 2010 Alessandro Rubini
  5. * Copyright (C) 2010 Linus Walleij for ST-Ericsson
  6. */
  7. #include <linux/init.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/irq.h>
  10. #include <linux/io.h>
  11. #include <linux/clockchips.h>
  12. #include <linux/clocksource.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/clk.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/delay.h>
  19. #include <linux/err.h>
  20. #include <linux/sched_clock.h>
  21. #include <asm/mach/time.h>
  22. /*
  23. * The MTU device hosts four different counters, with 4 set of
  24. * registers. These are register names.
  25. */
  26. #define MTU_IMSC 0x00 /* Interrupt mask set/clear */
  27. #define MTU_RIS 0x04 /* Raw interrupt status */
  28. #define MTU_MIS 0x08 /* Masked interrupt status */
  29. #define MTU_ICR 0x0C /* Interrupt clear register */
  30. /* per-timer registers take 0..3 as argument */
  31. #define MTU_LR(x) (0x10 + 0x10 * (x) + 0x00) /* Load value */
  32. #define MTU_VAL(x) (0x10 + 0x10 * (x) + 0x04) /* Current value */
  33. #define MTU_CR(x) (0x10 + 0x10 * (x) + 0x08) /* Control reg */
  34. #define MTU_BGLR(x) (0x10 + 0x10 * (x) + 0x0c) /* At next overflow */
  35. /* bits for the control register */
  36. #define MTU_CRn_ENA 0x80
  37. #define MTU_CRn_PERIODIC 0x40 /* if 0 = free-running */
  38. #define MTU_CRn_PRESCALE_MASK 0x0c
  39. #define MTU_CRn_PRESCALE_1 0x00
  40. #define MTU_CRn_PRESCALE_16 0x04
  41. #define MTU_CRn_PRESCALE_256 0x08
  42. #define MTU_CRn_32BITS 0x02
  43. #define MTU_CRn_ONESHOT 0x01 /* if 0 = wraps reloading from BGLR*/
  44. /* Other registers are usual amba/primecell registers, currently not used */
  45. #define MTU_ITCR 0xff0
  46. #define MTU_ITOP 0xff4
  47. #define MTU_PERIPH_ID0 0xfe0
  48. #define MTU_PERIPH_ID1 0xfe4
  49. #define MTU_PERIPH_ID2 0xfe8
  50. #define MTU_PERIPH_ID3 0xfeC
  51. #define MTU_PCELL0 0xff0
  52. #define MTU_PCELL1 0xff4
  53. #define MTU_PCELL2 0xff8
  54. #define MTU_PCELL3 0xffC
  55. static void __iomem *mtu_base;
  56. static bool clkevt_periodic;
  57. static u32 clk_prescale;
  58. static u32 nmdk_cycle; /* write-once */
  59. static struct delay_timer mtu_delay_timer;
  60. /*
  61. * Override the global weak sched_clock symbol with this
  62. * local implementation which uses the clocksource to get some
  63. * better resolution when scheduling the kernel.
  64. */
  65. static u64 notrace nomadik_read_sched_clock(void)
  66. {
  67. if (unlikely(!mtu_base))
  68. return 0;
  69. return -readl(mtu_base + MTU_VAL(0));
  70. }
  71. static unsigned long nmdk_timer_read_current_timer(void)
  72. {
  73. return ~readl_relaxed(mtu_base + MTU_VAL(0));
  74. }
  75. /* Clockevent device: use one-shot mode */
  76. static int nmdk_clkevt_next(unsigned long evt, struct clock_event_device *ev)
  77. {
  78. writel(1 << 1, mtu_base + MTU_IMSC);
  79. writel(evt, mtu_base + MTU_LR(1));
  80. /* Load highest value, enable device, enable interrupts */
  81. writel(MTU_CRn_ONESHOT | clk_prescale |
  82. MTU_CRn_32BITS | MTU_CRn_ENA,
  83. mtu_base + MTU_CR(1));
  84. return 0;
  85. }
  86. static void nmdk_clkevt_reset(void)
  87. {
  88. if (clkevt_periodic) {
  89. /* Timer: configure load and background-load, and fire it up */
  90. writel(nmdk_cycle, mtu_base + MTU_LR(1));
  91. writel(nmdk_cycle, mtu_base + MTU_BGLR(1));
  92. writel(MTU_CRn_PERIODIC | clk_prescale |
  93. MTU_CRn_32BITS | MTU_CRn_ENA,
  94. mtu_base + MTU_CR(1));
  95. writel(1 << 1, mtu_base + MTU_IMSC);
  96. } else {
  97. /* Generate an interrupt to start the clockevent again */
  98. (void) nmdk_clkevt_next(nmdk_cycle, NULL);
  99. }
  100. }
  101. static int nmdk_clkevt_shutdown(struct clock_event_device *evt)
  102. {
  103. writel(0, mtu_base + MTU_IMSC);
  104. /* disable timer */
  105. writel(0, mtu_base + MTU_CR(1));
  106. /* load some high default value */
  107. writel(0xffffffff, mtu_base + MTU_LR(1));
  108. return 0;
  109. }
  110. static int nmdk_clkevt_set_oneshot(struct clock_event_device *evt)
  111. {
  112. clkevt_periodic = false;
  113. return 0;
  114. }
  115. static int nmdk_clkevt_set_periodic(struct clock_event_device *evt)
  116. {
  117. clkevt_periodic = true;
  118. nmdk_clkevt_reset();
  119. return 0;
  120. }
  121. static void nmdk_clksrc_reset(void)
  122. {
  123. /* Disable */
  124. writel(0, mtu_base + MTU_CR(0));
  125. /* ClockSource: configure load and background-load, and fire it up */
  126. writel(nmdk_cycle, mtu_base + MTU_LR(0));
  127. writel(nmdk_cycle, mtu_base + MTU_BGLR(0));
  128. writel(clk_prescale | MTU_CRn_32BITS | MTU_CRn_ENA,
  129. mtu_base + MTU_CR(0));
  130. }
  131. static void nmdk_clkevt_resume(struct clock_event_device *cedev)
  132. {
  133. nmdk_clkevt_reset();
  134. nmdk_clksrc_reset();
  135. }
  136. static struct clock_event_device nmdk_clkevt = {
  137. .name = "mtu_1",
  138. .features = CLOCK_EVT_FEAT_ONESHOT |
  139. CLOCK_EVT_FEAT_PERIODIC |
  140. CLOCK_EVT_FEAT_DYNIRQ,
  141. .rating = 200,
  142. .set_state_shutdown = nmdk_clkevt_shutdown,
  143. .set_state_periodic = nmdk_clkevt_set_periodic,
  144. .set_state_oneshot = nmdk_clkevt_set_oneshot,
  145. .set_next_event = nmdk_clkevt_next,
  146. .resume = nmdk_clkevt_resume,
  147. };
  148. /*
  149. * IRQ Handler for timer 1 of the MTU block.
  150. */
  151. static irqreturn_t nmdk_timer_interrupt(int irq, void *dev_id)
  152. {
  153. struct clock_event_device *evdev = dev_id;
  154. writel(1 << 1, mtu_base + MTU_ICR); /* Interrupt clear reg */
  155. evdev->event_handler(evdev);
  156. return IRQ_HANDLED;
  157. }
  158. static int __init nmdk_timer_init(void __iomem *base, int irq,
  159. struct clk *pclk, struct clk *clk)
  160. {
  161. unsigned long rate;
  162. int ret;
  163. int min_ticks;
  164. mtu_base = base;
  165. BUG_ON(clk_prepare_enable(pclk));
  166. BUG_ON(clk_prepare_enable(clk));
  167. /*
  168. * Tick rate is 2.4MHz for Nomadik and 2.4Mhz, 100MHz or 133 MHz
  169. * for ux500, and in one specific Ux500 case 32768 Hz.
  170. *
  171. * Use a divide-by-16 counter if the tick rate is more than 32MHz.
  172. * At 32 MHz, the timer (with 32 bit counter) can be programmed
  173. * to wake-up at a max 127s a head in time. Dividing a 2.4 MHz timer
  174. * with 16 gives too low timer resolution.
  175. */
  176. rate = clk_get_rate(clk);
  177. if (rate > 32000000) {
  178. rate /= 16;
  179. clk_prescale = MTU_CRn_PRESCALE_16;
  180. } else {
  181. clk_prescale = MTU_CRn_PRESCALE_1;
  182. }
  183. /* Cycles for periodic mode */
  184. nmdk_cycle = DIV_ROUND_CLOSEST(rate, HZ);
  185. /* Timer 0 is the free running clocksource */
  186. nmdk_clksrc_reset();
  187. ret = clocksource_mmio_init(mtu_base + MTU_VAL(0), "mtu_0",
  188. rate, 200, 32, clocksource_mmio_readl_down);
  189. if (ret) {
  190. pr_err("timer: failed to initialize clock source %s\n", "mtu_0");
  191. return ret;
  192. }
  193. sched_clock_register(nomadik_read_sched_clock, 32, rate);
  194. /* Timer 1 is used for events, register irq and clockevents */
  195. if (request_irq(irq, nmdk_timer_interrupt, IRQF_TIMER,
  196. "Nomadik Timer Tick", &nmdk_clkevt))
  197. pr_err("%s: request_irq() failed\n", "Nomadik Timer Tick");
  198. nmdk_clkevt.cpumask = cpumask_of(0);
  199. nmdk_clkevt.irq = irq;
  200. if (rate < 100000)
  201. min_ticks = 5;
  202. else
  203. min_ticks = 2;
  204. clockevents_config_and_register(&nmdk_clkevt, rate, min_ticks,
  205. 0xffffffffU);
  206. mtu_delay_timer.read_current_timer = &nmdk_timer_read_current_timer;
  207. mtu_delay_timer.freq = rate;
  208. register_current_timer_delay(&mtu_delay_timer);
  209. return 0;
  210. }
  211. static int __init nmdk_timer_of_init(struct device_node *node)
  212. {
  213. struct clk *pclk;
  214. struct clk *clk;
  215. void __iomem *base;
  216. int irq;
  217. base = of_iomap(node, 0);
  218. if (!base) {
  219. pr_err("Can't remap registers\n");
  220. return -ENXIO;
  221. }
  222. pclk = of_clk_get_by_name(node, "apb_pclk");
  223. if (IS_ERR(pclk)) {
  224. pr_err("could not get apb_pclk\n");
  225. return PTR_ERR(pclk);
  226. }
  227. clk = of_clk_get_by_name(node, "timclk");
  228. if (IS_ERR(clk)) {
  229. pr_err("could not get timclk\n");
  230. return PTR_ERR(clk);
  231. }
  232. irq = irq_of_parse_and_map(node, 0);
  233. if (irq <= 0) {
  234. pr_err("Can't parse IRQ\n");
  235. return -EINVAL;
  236. }
  237. return nmdk_timer_init(base, irq, pclk, clk);
  238. }
  239. TIMER_OF_DECLARE(nomadik_mtu, "st,nomadik-mtu",
  240. nmdk_timer_of_init);