timer-gemini.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Gemini timer driver
  3. * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
  4. *
  5. * Based on a rewrite of arch/arm/mach-gemini/timer.c:
  6. * Copyright (C) 2001-2006 Storlink, Corp.
  7. * Copyright (C) 2008-2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
  8. */
  9. #include <linux/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/regmap.h>
  16. #include <linux/clockchips.h>
  17. #include <linux/clocksource.h>
  18. #include <linux/sched_clock.h>
  19. /*
  20. * Relevant registers in the global syscon
  21. */
  22. #define GLOBAL_STATUS 0x04
  23. #define CPU_AHB_RATIO_MASK (0x3 << 18)
  24. #define CPU_AHB_1_1 (0x0 << 18)
  25. #define CPU_AHB_3_2 (0x1 << 18)
  26. #define CPU_AHB_24_13 (0x2 << 18)
  27. #define CPU_AHB_2_1 (0x3 << 18)
  28. #define REG_TO_AHB_SPEED(reg) ((((reg) >> 15) & 0x7) * 10 + 130)
  29. /*
  30. * Register definitions for the timers
  31. */
  32. #define TIMER1_COUNT (0x00)
  33. #define TIMER1_LOAD (0x04)
  34. #define TIMER1_MATCH1 (0x08)
  35. #define TIMER1_MATCH2 (0x0c)
  36. #define TIMER2_COUNT (0x10)
  37. #define TIMER2_LOAD (0x14)
  38. #define TIMER2_MATCH1 (0x18)
  39. #define TIMER2_MATCH2 (0x1c)
  40. #define TIMER3_COUNT (0x20)
  41. #define TIMER3_LOAD (0x24)
  42. #define TIMER3_MATCH1 (0x28)
  43. #define TIMER3_MATCH2 (0x2c)
  44. #define TIMER_CR (0x30)
  45. #define TIMER_INTR_STATE (0x34)
  46. #define TIMER_INTR_MASK (0x38)
  47. #define TIMER_1_CR_ENABLE (1 << 0)
  48. #define TIMER_1_CR_CLOCK (1 << 1)
  49. #define TIMER_1_CR_INT (1 << 2)
  50. #define TIMER_2_CR_ENABLE (1 << 3)
  51. #define TIMER_2_CR_CLOCK (1 << 4)
  52. #define TIMER_2_CR_INT (1 << 5)
  53. #define TIMER_3_CR_ENABLE (1 << 6)
  54. #define TIMER_3_CR_CLOCK (1 << 7)
  55. #define TIMER_3_CR_INT (1 << 8)
  56. #define TIMER_1_CR_UPDOWN (1 << 9)
  57. #define TIMER_2_CR_UPDOWN (1 << 10)
  58. #define TIMER_3_CR_UPDOWN (1 << 11)
  59. #define TIMER_DEFAULT_FLAGS (TIMER_1_CR_UPDOWN | \
  60. TIMER_3_CR_ENABLE | \
  61. TIMER_3_CR_UPDOWN)
  62. #define TIMER_1_INT_MATCH1 (1 << 0)
  63. #define TIMER_1_INT_MATCH2 (1 << 1)
  64. #define TIMER_1_INT_OVERFLOW (1 << 2)
  65. #define TIMER_2_INT_MATCH1 (1 << 3)
  66. #define TIMER_2_INT_MATCH2 (1 << 4)
  67. #define TIMER_2_INT_OVERFLOW (1 << 5)
  68. #define TIMER_3_INT_MATCH1 (1 << 6)
  69. #define TIMER_3_INT_MATCH2 (1 << 7)
  70. #define TIMER_3_INT_OVERFLOW (1 << 8)
  71. #define TIMER_INT_ALL_MASK 0x1ff
  72. static unsigned int tick_rate;
  73. static void __iomem *base;
  74. static u64 notrace gemini_read_sched_clock(void)
  75. {
  76. return readl(base + TIMER3_COUNT);
  77. }
  78. static int gemini_timer_set_next_event(unsigned long cycles,
  79. struct clock_event_device *evt)
  80. {
  81. u32 cr;
  82. /* Setup the match register */
  83. cr = readl(base + TIMER1_COUNT);
  84. writel(cr + cycles, base + TIMER1_MATCH1);
  85. if (readl(base + TIMER1_COUNT) - cr > cycles)
  86. return -ETIME;
  87. return 0;
  88. }
  89. static int gemini_timer_shutdown(struct clock_event_device *evt)
  90. {
  91. u32 cr;
  92. /*
  93. * Disable also for oneshot: the set_next() call will arm the timer
  94. * instead.
  95. */
  96. /* Stop timer and interrupt. */
  97. cr = readl(base + TIMER_CR);
  98. cr &= ~(TIMER_1_CR_ENABLE | TIMER_1_CR_INT);
  99. writel(cr, base + TIMER_CR);
  100. /* Setup counter start from 0 */
  101. writel(0, base + TIMER1_COUNT);
  102. writel(0, base + TIMER1_LOAD);
  103. /* enable interrupt */
  104. cr = readl(base + TIMER_INTR_MASK);
  105. cr &= ~(TIMER_1_INT_OVERFLOW | TIMER_1_INT_MATCH2);
  106. cr |= TIMER_1_INT_MATCH1;
  107. writel(cr, base + TIMER_INTR_MASK);
  108. /* start the timer */
  109. cr = readl(base + TIMER_CR);
  110. cr |= TIMER_1_CR_ENABLE;
  111. writel(cr, base + TIMER_CR);
  112. return 0;
  113. }
  114. static int gemini_timer_set_periodic(struct clock_event_device *evt)
  115. {
  116. u32 period = DIV_ROUND_CLOSEST(tick_rate, HZ);
  117. u32 cr;
  118. /* Stop timer and interrupt */
  119. cr = readl(base + TIMER_CR);
  120. cr &= ~(TIMER_1_CR_ENABLE | TIMER_1_CR_INT);
  121. writel(cr, base + TIMER_CR);
  122. /* Setup timer to fire at 1/HT intervals. */
  123. cr = 0xffffffff - (period - 1);
  124. writel(cr, base + TIMER1_COUNT);
  125. writel(cr, base + TIMER1_LOAD);
  126. /* enable interrupt on overflow */
  127. cr = readl(base + TIMER_INTR_MASK);
  128. cr &= ~(TIMER_1_INT_MATCH1 | TIMER_1_INT_MATCH2);
  129. cr |= TIMER_1_INT_OVERFLOW;
  130. writel(cr, base + TIMER_INTR_MASK);
  131. /* Start the timer */
  132. cr = readl(base + TIMER_CR);
  133. cr |= TIMER_1_CR_ENABLE;
  134. cr |= TIMER_1_CR_INT;
  135. writel(cr, base + TIMER_CR);
  136. return 0;
  137. }
  138. /* Use TIMER1 as clock event */
  139. static struct clock_event_device gemini_clockevent = {
  140. .name = "TIMER1",
  141. /* Reasonably fast and accurate clock event */
  142. .rating = 300,
  143. .shift = 32,
  144. .features = CLOCK_EVT_FEAT_PERIODIC |
  145. CLOCK_EVT_FEAT_ONESHOT,
  146. .set_next_event = gemini_timer_set_next_event,
  147. .set_state_shutdown = gemini_timer_shutdown,
  148. .set_state_periodic = gemini_timer_set_periodic,
  149. .set_state_oneshot = gemini_timer_shutdown,
  150. .tick_resume = gemini_timer_shutdown,
  151. };
  152. /*
  153. * IRQ handler for the timer
  154. */
  155. static irqreturn_t gemini_timer_interrupt(int irq, void *dev_id)
  156. {
  157. struct clock_event_device *evt = &gemini_clockevent;
  158. evt->event_handler(evt);
  159. return IRQ_HANDLED;
  160. }
  161. static struct irqaction gemini_timer_irq = {
  162. .name = "Gemini Timer Tick",
  163. .flags = IRQF_TIMER,
  164. .handler = gemini_timer_interrupt,
  165. };
  166. static int __init gemini_timer_of_init(struct device_node *np)
  167. {
  168. static struct regmap *map;
  169. int irq;
  170. int ret;
  171. u32 val;
  172. map = syscon_regmap_lookup_by_phandle(np, "syscon");
  173. if (IS_ERR(map)) {
  174. pr_err("Can't get regmap for syscon handle");
  175. return -ENODEV;
  176. }
  177. ret = regmap_read(map, GLOBAL_STATUS, &val);
  178. if (ret) {
  179. pr_err("Can't read syscon status register");
  180. return -ENXIO;
  181. }
  182. base = of_iomap(np, 0);
  183. if (!base) {
  184. pr_err("Can't remap registers");
  185. return -ENXIO;
  186. }
  187. /* IRQ for timer 1 */
  188. irq = irq_of_parse_and_map(np, 0);
  189. if (irq <= 0) {
  190. pr_err("Can't parse IRQ");
  191. return -EINVAL;
  192. }
  193. tick_rate = REG_TO_AHB_SPEED(val) * 1000000;
  194. printk(KERN_INFO "Bus: %dMHz", tick_rate / 1000000);
  195. tick_rate /= 6; /* APB bus run AHB*(1/6) */
  196. switch (val & CPU_AHB_RATIO_MASK) {
  197. case CPU_AHB_1_1:
  198. printk(KERN_CONT "(1/1)\n");
  199. break;
  200. case CPU_AHB_3_2:
  201. printk(KERN_CONT "(3/2)\n");
  202. break;
  203. case CPU_AHB_24_13:
  204. printk(KERN_CONT "(24/13)\n");
  205. break;
  206. case CPU_AHB_2_1:
  207. printk(KERN_CONT "(2/1)\n");
  208. break;
  209. }
  210. /*
  211. * Reset the interrupt mask and status
  212. */
  213. writel(TIMER_INT_ALL_MASK, base + TIMER_INTR_MASK);
  214. writel(0, base + TIMER_INTR_STATE);
  215. writel(TIMER_DEFAULT_FLAGS, base + TIMER_CR);
  216. /*
  217. * Setup free-running clocksource timer (interrupts
  218. * disabled.)
  219. */
  220. writel(0, base + TIMER3_COUNT);
  221. writel(0, base + TIMER3_LOAD);
  222. writel(0, base + TIMER3_MATCH1);
  223. writel(0, base + TIMER3_MATCH2);
  224. clocksource_mmio_init(base + TIMER3_COUNT,
  225. "gemini_clocksource", tick_rate,
  226. 300, 32, clocksource_mmio_readl_up);
  227. sched_clock_register(gemini_read_sched_clock, 32, tick_rate);
  228. /*
  229. * Setup clockevent timer (interrupt-driven.)
  230. */
  231. writel(0, base + TIMER1_COUNT);
  232. writel(0, base + TIMER1_LOAD);
  233. writel(0, base + TIMER1_MATCH1);
  234. writel(0, base + TIMER1_MATCH2);
  235. setup_irq(irq, &gemini_timer_irq);
  236. gemini_clockevent.cpumask = cpumask_of(0);
  237. clockevents_config_and_register(&gemini_clockevent, tick_rate,
  238. 1, 0xffffffff);
  239. return 0;
  240. }
  241. CLOCKSOURCE_OF_DECLARE(nomadik_mtu, "cortina,gemini-timer",
  242. gemini_timer_of_init);