nomadik-mtu.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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/platform_data/clocksource-nomadik-mtu.h>
  24. #include <linux/sched_clock.h>
  25. #include <asm/mach/time.h>
  26. /*
  27. * The MTU device hosts four different counters, with 4 set of
  28. * registers. These are register names.
  29. */
  30. #define MTU_IMSC 0x00 /* Interrupt mask set/clear */
  31. #define MTU_RIS 0x04 /* Raw interrupt status */
  32. #define MTU_MIS 0x08 /* Masked interrupt status */
  33. #define MTU_ICR 0x0C /* Interrupt clear register */
  34. /* per-timer registers take 0..3 as argument */
  35. #define MTU_LR(x) (0x10 + 0x10 * (x) + 0x00) /* Load value */
  36. #define MTU_VAL(x) (0x10 + 0x10 * (x) + 0x04) /* Current value */
  37. #define MTU_CR(x) (0x10 + 0x10 * (x) + 0x08) /* Control reg */
  38. #define MTU_BGLR(x) (0x10 + 0x10 * (x) + 0x0c) /* At next overflow */
  39. /* bits for the control register */
  40. #define MTU_CRn_ENA 0x80
  41. #define MTU_CRn_PERIODIC 0x40 /* if 0 = free-running */
  42. #define MTU_CRn_PRESCALE_MASK 0x0c
  43. #define MTU_CRn_PRESCALE_1 0x00
  44. #define MTU_CRn_PRESCALE_16 0x04
  45. #define MTU_CRn_PRESCALE_256 0x08
  46. #define MTU_CRn_32BITS 0x02
  47. #define MTU_CRn_ONESHOT 0x01 /* if 0 = wraps reloading from BGLR*/
  48. /* Other registers are usual amba/primecell registers, currently not used */
  49. #define MTU_ITCR 0xff0
  50. #define MTU_ITOP 0xff4
  51. #define MTU_PERIPH_ID0 0xfe0
  52. #define MTU_PERIPH_ID1 0xfe4
  53. #define MTU_PERIPH_ID2 0xfe8
  54. #define MTU_PERIPH_ID3 0xfeC
  55. #define MTU_PCELL0 0xff0
  56. #define MTU_PCELL1 0xff4
  57. #define MTU_PCELL2 0xff8
  58. #define MTU_PCELL3 0xffC
  59. static void __iomem *mtu_base;
  60. static bool clkevt_periodic;
  61. static u32 clk_prescale;
  62. static u32 nmdk_cycle; /* write-once */
  63. static struct delay_timer mtu_delay_timer;
  64. #ifdef CONFIG_CLKSRC_NOMADIK_MTU_SCHED_CLOCK
  65. /*
  66. * Override the global weak sched_clock symbol with this
  67. * local implementation which uses the clocksource to get some
  68. * better resolution when scheduling the kernel.
  69. */
  70. static u32 notrace nomadik_read_sched_clock(void)
  71. {
  72. if (unlikely(!mtu_base))
  73. return 0;
  74. return -readl(mtu_base + MTU_VAL(0));
  75. }
  76. #endif
  77. static unsigned long nmdk_timer_read_current_timer(void)
  78. {
  79. return ~readl_relaxed(mtu_base + MTU_VAL(0));
  80. }
  81. /* Clockevent device: use one-shot mode */
  82. static int nmdk_clkevt_next(unsigned long evt, struct clock_event_device *ev)
  83. {
  84. writel(1 << 1, mtu_base + MTU_IMSC);
  85. writel(evt, mtu_base + MTU_LR(1));
  86. /* Load highest value, enable device, enable interrupts */
  87. writel(MTU_CRn_ONESHOT | clk_prescale |
  88. MTU_CRn_32BITS | MTU_CRn_ENA,
  89. mtu_base + MTU_CR(1));
  90. return 0;
  91. }
  92. void nmdk_clkevt_reset(void)
  93. {
  94. if (clkevt_periodic) {
  95. /* Timer: configure load and background-load, and fire it up */
  96. writel(nmdk_cycle, mtu_base + MTU_LR(1));
  97. writel(nmdk_cycle, mtu_base + MTU_BGLR(1));
  98. writel(MTU_CRn_PERIODIC | clk_prescale |
  99. MTU_CRn_32BITS | MTU_CRn_ENA,
  100. mtu_base + MTU_CR(1));
  101. writel(1 << 1, mtu_base + MTU_IMSC);
  102. } else {
  103. /* Generate an interrupt to start the clockevent again */
  104. (void) nmdk_clkevt_next(nmdk_cycle, NULL);
  105. }
  106. }
  107. static void nmdk_clkevt_mode(enum clock_event_mode mode,
  108. struct clock_event_device *dev)
  109. {
  110. switch (mode) {
  111. case CLOCK_EVT_MODE_PERIODIC:
  112. clkevt_periodic = true;
  113. nmdk_clkevt_reset();
  114. break;
  115. case CLOCK_EVT_MODE_ONESHOT:
  116. clkevt_periodic = false;
  117. break;
  118. case CLOCK_EVT_MODE_SHUTDOWN:
  119. case CLOCK_EVT_MODE_UNUSED:
  120. writel(0, mtu_base + MTU_IMSC);
  121. /* disable timer */
  122. writel(0, mtu_base + MTU_CR(1));
  123. /* load some high default value */
  124. writel(0xffffffff, mtu_base + MTU_LR(1));
  125. break;
  126. case CLOCK_EVT_MODE_RESUME:
  127. break;
  128. }
  129. }
  130. void nmdk_clksrc_reset(void)
  131. {
  132. /* Disable */
  133. writel(0, mtu_base + MTU_CR(0));
  134. /* ClockSource: configure load and background-load, and fire it up */
  135. writel(nmdk_cycle, mtu_base + MTU_LR(0));
  136. writel(nmdk_cycle, mtu_base + MTU_BGLR(0));
  137. writel(clk_prescale | MTU_CRn_32BITS | MTU_CRn_ENA,
  138. mtu_base + MTU_CR(0));
  139. }
  140. static void nmdk_clkevt_resume(struct clock_event_device *cedev)
  141. {
  142. nmdk_clkevt_reset();
  143. nmdk_clksrc_reset();
  144. }
  145. static struct clock_event_device nmdk_clkevt = {
  146. .name = "mtu_1",
  147. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC |
  148. CLOCK_EVT_FEAT_DYNIRQ,
  149. .rating = 200,
  150. .set_mode = nmdk_clkevt_mode,
  151. .set_next_event = nmdk_clkevt_next,
  152. .resume = nmdk_clkevt_resume,
  153. };
  154. /*
  155. * IRQ Handler for timer 1 of the MTU block.
  156. */
  157. static irqreturn_t nmdk_timer_interrupt(int irq, void *dev_id)
  158. {
  159. struct clock_event_device *evdev = dev_id;
  160. writel(1 << 1, mtu_base + MTU_ICR); /* Interrupt clear reg */
  161. evdev->event_handler(evdev);
  162. return IRQ_HANDLED;
  163. }
  164. static struct irqaction nmdk_timer_irq = {
  165. .name = "Nomadik Timer Tick",
  166. .flags = IRQF_DISABLED | IRQF_TIMER,
  167. .handler = nmdk_timer_interrupt,
  168. .dev_id = &nmdk_clkevt,
  169. };
  170. static void __init __nmdk_timer_init(void __iomem *base, int irq,
  171. struct clk *pclk, struct clk *clk)
  172. {
  173. unsigned long rate;
  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. if (clocksource_mmio_init(mtu_base + MTU_VAL(0), "mtu_0",
  197. rate, 200, 32, clocksource_mmio_readl_down))
  198. pr_err("timer: failed to initialize clock source %s\n",
  199. "mtu_0");
  200. #ifdef CONFIG_CLKSRC_NOMADIK_MTU_SCHED_CLOCK
  201. setup_sched_clock(nomadik_read_sched_clock, 32, rate);
  202. #endif
  203. /* Timer 1 is used for events, register irq and clockevents */
  204. setup_irq(irq, &nmdk_timer_irq);
  205. nmdk_clkevt.cpumask = cpumask_of(0);
  206. nmdk_clkevt.irq = irq;
  207. clockevents_config_and_register(&nmdk_clkevt, rate, 2, 0xffffffffU);
  208. mtu_delay_timer.read_current_timer = &nmdk_timer_read_current_timer;
  209. mtu_delay_timer.freq = rate;
  210. register_current_timer_delay(&mtu_delay_timer);
  211. }
  212. void __init nmdk_timer_init(void __iomem *base, int irq)
  213. {
  214. struct clk *clk0, *pclk0;
  215. pclk0 = clk_get_sys("mtu0", "apb_pclk");
  216. BUG_ON(IS_ERR(pclk0));
  217. clk0 = clk_get_sys("mtu0", NULL);
  218. BUG_ON(IS_ERR(clk0));
  219. __nmdk_timer_init(base, irq, pclk0, clk0);
  220. }
  221. static void __init nmdk_timer_of_init(struct device_node *node)
  222. {
  223. struct clk *pclk;
  224. struct clk *clk;
  225. void __iomem *base;
  226. int irq;
  227. base = of_iomap(node, 0);
  228. if (!base)
  229. panic("Can't remap registers");
  230. pclk = of_clk_get_by_name(node, "apb_pclk");
  231. if (IS_ERR(pclk))
  232. panic("could not get apb_pclk");
  233. clk = of_clk_get_by_name(node, "timclk");
  234. if (IS_ERR(clk))
  235. panic("could not get timclk");
  236. irq = irq_of_parse_and_map(node, 0);
  237. if (irq <= 0)
  238. panic("Can't parse IRQ");
  239. __nmdk_timer_init(base, irq, pclk, clk);
  240. }
  241. CLOCKSOURCE_OF_DECLARE(nomadik_mtu, "st,nomadik-mtu",
  242. nmdk_timer_of_init);