timer-fttmr010.c 7.6 KB

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