nomadik-mtu.c 7.5 KB

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