timer-fttmr010.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Faraday Technology FTTMR010 timer driver
  4. * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
  5. *
  6. * Based on a rewrite of arch/arm/mach-gemini/timer.c:
  7. * Copyright (C) 2001-2006 Storlink, Corp.
  8. * Copyright (C) 2008-2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
  9. */
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/clockchips.h>
  16. #include <linux/clocksource.h>
  17. #include <linux/sched_clock.h>
  18. #include <linux/clk.h>
  19. #include <linux/slab.h>
  20. #include <linux/bitops.h>
  21. #include <linux/delay.h>
  22. /*
  23. * Register definitions for the timers
  24. */
  25. #define TIMER1_COUNT (0x00)
  26. #define TIMER1_LOAD (0x04)
  27. #define TIMER1_MATCH1 (0x08)
  28. #define TIMER1_MATCH2 (0x0c)
  29. #define TIMER2_COUNT (0x10)
  30. #define TIMER2_LOAD (0x14)
  31. #define TIMER2_MATCH1 (0x18)
  32. #define TIMER2_MATCH2 (0x1c)
  33. #define TIMER3_COUNT (0x20)
  34. #define TIMER3_LOAD (0x24)
  35. #define TIMER3_MATCH1 (0x28)
  36. #define TIMER3_MATCH2 (0x2c)
  37. #define TIMER_CR (0x30)
  38. #define TIMER_INTR_STATE (0x34)
  39. #define TIMER_INTR_MASK (0x38)
  40. #define TIMER_1_CR_ENABLE BIT(0)
  41. #define TIMER_1_CR_CLOCK BIT(1)
  42. #define TIMER_1_CR_INT BIT(2)
  43. #define TIMER_2_CR_ENABLE BIT(3)
  44. #define TIMER_2_CR_CLOCK BIT(4)
  45. #define TIMER_2_CR_INT BIT(5)
  46. #define TIMER_3_CR_ENABLE BIT(6)
  47. #define TIMER_3_CR_CLOCK BIT(7)
  48. #define TIMER_3_CR_INT BIT(8)
  49. #define TIMER_1_CR_UPDOWN BIT(9)
  50. #define TIMER_2_CR_UPDOWN BIT(10)
  51. #define TIMER_3_CR_UPDOWN BIT(11)
  52. /*
  53. * The Aspeed AST2400 moves bits around in the control register
  54. * and lacks bits for setting the timer to count upwards.
  55. */
  56. #define TIMER_1_CR_ASPEED_ENABLE BIT(0)
  57. #define TIMER_1_CR_ASPEED_CLOCK BIT(1)
  58. #define TIMER_1_CR_ASPEED_INT BIT(2)
  59. #define TIMER_2_CR_ASPEED_ENABLE BIT(4)
  60. #define TIMER_2_CR_ASPEED_CLOCK BIT(5)
  61. #define TIMER_2_CR_ASPEED_INT BIT(6)
  62. #define TIMER_3_CR_ASPEED_ENABLE BIT(8)
  63. #define TIMER_3_CR_ASPEED_CLOCK BIT(9)
  64. #define TIMER_3_CR_ASPEED_INT BIT(10)
  65. #define TIMER_1_INT_MATCH1 BIT(0)
  66. #define TIMER_1_INT_MATCH2 BIT(1)
  67. #define TIMER_1_INT_OVERFLOW BIT(2)
  68. #define TIMER_2_INT_MATCH1 BIT(3)
  69. #define TIMER_2_INT_MATCH2 BIT(4)
  70. #define TIMER_2_INT_OVERFLOW BIT(5)
  71. #define TIMER_3_INT_MATCH1 BIT(6)
  72. #define TIMER_3_INT_MATCH2 BIT(7)
  73. #define TIMER_3_INT_OVERFLOW BIT(8)
  74. #define TIMER_INT_ALL_MASK 0x1ff
  75. struct fttmr010 {
  76. void __iomem *base;
  77. unsigned int tick_rate;
  78. bool count_down;
  79. u32 t1_enable_val;
  80. struct clock_event_device clkevt;
  81. #ifdef CONFIG_ARM
  82. struct delay_timer delay_timer;
  83. #endif
  84. };
  85. /*
  86. * A local singleton used by sched_clock and delay timer reads, which are
  87. * fast and stateless
  88. */
  89. static struct fttmr010 *local_fttmr;
  90. static inline struct fttmr010 *to_fttmr010(struct clock_event_device *evt)
  91. {
  92. return container_of(evt, struct fttmr010, clkevt);
  93. }
  94. static unsigned long fttmr010_read_current_timer_up(void)
  95. {
  96. return readl(local_fttmr->base + TIMER2_COUNT);
  97. }
  98. static unsigned long fttmr010_read_current_timer_down(void)
  99. {
  100. return ~readl(local_fttmr->base + TIMER2_COUNT);
  101. }
  102. static u64 notrace fttmr010_read_sched_clock_up(void)
  103. {
  104. return fttmr010_read_current_timer_up();
  105. }
  106. static u64 notrace fttmr010_read_sched_clock_down(void)
  107. {
  108. return fttmr010_read_current_timer_down();
  109. }
  110. static int fttmr010_timer_set_next_event(unsigned long cycles,
  111. struct clock_event_device *evt)
  112. {
  113. struct fttmr010 *fttmr010 = to_fttmr010(evt);
  114. u32 cr;
  115. /* Stop */
  116. cr = readl(fttmr010->base + TIMER_CR);
  117. cr &= ~fttmr010->t1_enable_val;
  118. writel(cr, fttmr010->base + TIMER_CR);
  119. /* Setup the match register forward/backward in time */
  120. cr = readl(fttmr010->base + TIMER1_COUNT);
  121. if (fttmr010->count_down)
  122. cr -= cycles;
  123. else
  124. cr += cycles;
  125. writel(cr, fttmr010->base + TIMER1_MATCH1);
  126. /* Start */
  127. cr = readl(fttmr010->base + TIMER_CR);
  128. cr |= fttmr010->t1_enable_val;
  129. writel(cr, fttmr010->base + TIMER_CR);
  130. return 0;
  131. }
  132. static int fttmr010_timer_shutdown(struct clock_event_device *evt)
  133. {
  134. struct fttmr010 *fttmr010 = to_fttmr010(evt);
  135. u32 cr;
  136. /* Stop */
  137. cr = readl(fttmr010->base + TIMER_CR);
  138. cr &= ~fttmr010->t1_enable_val;
  139. writel(cr, fttmr010->base + TIMER_CR);
  140. return 0;
  141. }
  142. static int fttmr010_timer_set_oneshot(struct clock_event_device *evt)
  143. {
  144. struct fttmr010 *fttmr010 = to_fttmr010(evt);
  145. u32 cr;
  146. /* Stop */
  147. cr = readl(fttmr010->base + TIMER_CR);
  148. cr &= ~fttmr010->t1_enable_val;
  149. writel(cr, fttmr010->base + TIMER_CR);
  150. /* Setup counter start from 0 or ~0 */
  151. writel(0, fttmr010->base + TIMER1_COUNT);
  152. if (fttmr010->count_down)
  153. writel(~0, fttmr010->base + TIMER1_LOAD);
  154. else
  155. writel(0, fttmr010->base + TIMER1_LOAD);
  156. /* Enable interrupt */
  157. cr = readl(fttmr010->base + TIMER_INTR_MASK);
  158. cr &= ~(TIMER_1_INT_OVERFLOW | TIMER_1_INT_MATCH2);
  159. cr |= TIMER_1_INT_MATCH1;
  160. writel(cr, fttmr010->base + TIMER_INTR_MASK);
  161. return 0;
  162. }
  163. static int fttmr010_timer_set_periodic(struct clock_event_device *evt)
  164. {
  165. struct fttmr010 *fttmr010 = to_fttmr010(evt);
  166. u32 period = DIV_ROUND_CLOSEST(fttmr010->tick_rate, HZ);
  167. u32 cr;
  168. /* Stop */
  169. cr = readl(fttmr010->base + TIMER_CR);
  170. cr &= ~fttmr010->t1_enable_val;
  171. writel(cr, fttmr010->base + TIMER_CR);
  172. /* Setup timer to fire at 1/HZ intervals. */
  173. if (fttmr010->count_down) {
  174. writel(period, fttmr010->base + TIMER1_LOAD);
  175. writel(0, fttmr010->base + TIMER1_MATCH1);
  176. } else {
  177. cr = 0xffffffff - (period - 1);
  178. writel(cr, fttmr010->base + TIMER1_COUNT);
  179. writel(cr, fttmr010->base + TIMER1_LOAD);
  180. /* Enable interrupt on overflow */
  181. cr = readl(fttmr010->base + TIMER_INTR_MASK);
  182. cr &= ~(TIMER_1_INT_MATCH1 | TIMER_1_INT_MATCH2);
  183. cr |= TIMER_1_INT_OVERFLOW;
  184. writel(cr, fttmr010->base + TIMER_INTR_MASK);
  185. }
  186. /* Start the timer */
  187. cr = readl(fttmr010->base + TIMER_CR);
  188. cr |= fttmr010->t1_enable_val;
  189. writel(cr, fttmr010->base + TIMER_CR);
  190. return 0;
  191. }
  192. /*
  193. * IRQ handler for the timer
  194. */
  195. static irqreturn_t fttmr010_timer_interrupt(int irq, void *dev_id)
  196. {
  197. struct clock_event_device *evt = dev_id;
  198. evt->event_handler(evt);
  199. return IRQ_HANDLED;
  200. }
  201. static int __init fttmr010_common_init(struct device_node *np, bool is_aspeed)
  202. {
  203. struct fttmr010 *fttmr010;
  204. int irq;
  205. struct clk *clk;
  206. int ret;
  207. u32 val;
  208. /*
  209. * These implementations require a clock reference.
  210. * FIXME: we currently only support clocking using PCLK
  211. * and using EXTCLK is not supported in the driver.
  212. */
  213. clk = of_clk_get_by_name(np, "PCLK");
  214. if (IS_ERR(clk)) {
  215. pr_err("could not get PCLK\n");
  216. return PTR_ERR(clk);
  217. }
  218. ret = clk_prepare_enable(clk);
  219. if (ret) {
  220. pr_err("failed to enable PCLK\n");
  221. return ret;
  222. }
  223. fttmr010 = kzalloc(sizeof(*fttmr010), GFP_KERNEL);
  224. if (!fttmr010) {
  225. ret = -ENOMEM;
  226. goto out_disable_clock;
  227. }
  228. fttmr010->tick_rate = clk_get_rate(clk);
  229. fttmr010->base = of_iomap(np, 0);
  230. if (!fttmr010->base) {
  231. pr_err("Can't remap registers\n");
  232. ret = -ENXIO;
  233. goto out_free;
  234. }
  235. /* IRQ for timer 1 */
  236. irq = irq_of_parse_and_map(np, 0);
  237. if (irq <= 0) {
  238. pr_err("Can't parse IRQ\n");
  239. ret = -EINVAL;
  240. goto out_unmap;
  241. }
  242. /*
  243. * The Aspeed AST2400 moves bits around in the control register,
  244. * otherwise it works the same.
  245. */
  246. if (is_aspeed) {
  247. fttmr010->t1_enable_val = TIMER_1_CR_ASPEED_ENABLE |
  248. TIMER_1_CR_ASPEED_INT;
  249. /* Downward not available */
  250. fttmr010->count_down = true;
  251. } else {
  252. fttmr010->t1_enable_val = TIMER_1_CR_ENABLE | TIMER_1_CR_INT;
  253. }
  254. /*
  255. * Reset the interrupt mask and status
  256. */
  257. writel(TIMER_INT_ALL_MASK, fttmr010->base + TIMER_INTR_MASK);
  258. writel(0, fttmr010->base + TIMER_INTR_STATE);
  259. /*
  260. * Enable timer 1 count up, timer 2 count up, except on Aspeed,
  261. * where everything just counts down.
  262. */
  263. if (is_aspeed)
  264. val = TIMER_2_CR_ASPEED_ENABLE;
  265. else {
  266. val = TIMER_2_CR_ENABLE;
  267. if (!fttmr010->count_down)
  268. val |= TIMER_1_CR_UPDOWN | TIMER_2_CR_UPDOWN;
  269. }
  270. writel(val, fttmr010->base + TIMER_CR);
  271. /*
  272. * Setup free-running clocksource timer (interrupts
  273. * disabled.)
  274. */
  275. local_fttmr = fttmr010;
  276. writel(0, fttmr010->base + TIMER2_COUNT);
  277. writel(0, fttmr010->base + TIMER2_MATCH1);
  278. writel(0, fttmr010->base + TIMER2_MATCH2);
  279. if (fttmr010->count_down) {
  280. writel(~0, fttmr010->base + TIMER2_LOAD);
  281. clocksource_mmio_init(fttmr010->base + TIMER2_COUNT,
  282. "FTTMR010-TIMER2",
  283. fttmr010->tick_rate,
  284. 300, 32, clocksource_mmio_readl_down);
  285. sched_clock_register(fttmr010_read_sched_clock_down, 32,
  286. fttmr010->tick_rate);
  287. } else {
  288. writel(0, fttmr010->base + TIMER2_LOAD);
  289. clocksource_mmio_init(fttmr010->base + TIMER2_COUNT,
  290. "FTTMR010-TIMER2",
  291. fttmr010->tick_rate,
  292. 300, 32, clocksource_mmio_readl_up);
  293. sched_clock_register(fttmr010_read_sched_clock_up, 32,
  294. fttmr010->tick_rate);
  295. }
  296. /*
  297. * Setup clockevent timer (interrupt-driven) on timer 1.
  298. */
  299. writel(0, fttmr010->base + TIMER1_COUNT);
  300. writel(0, fttmr010->base + TIMER1_LOAD);
  301. writel(0, fttmr010->base + TIMER1_MATCH1);
  302. writel(0, fttmr010->base + TIMER1_MATCH2);
  303. ret = request_irq(irq, fttmr010_timer_interrupt, IRQF_TIMER,
  304. "FTTMR010-TIMER1", &fttmr010->clkevt);
  305. if (ret) {
  306. pr_err("FTTMR010-TIMER1 no IRQ\n");
  307. goto out_unmap;
  308. }
  309. fttmr010->clkevt.name = "FTTMR010-TIMER1";
  310. /* Reasonably fast and accurate clock event */
  311. fttmr010->clkevt.rating = 300;
  312. fttmr010->clkevt.features = CLOCK_EVT_FEAT_PERIODIC |
  313. CLOCK_EVT_FEAT_ONESHOT;
  314. fttmr010->clkevt.set_next_event = fttmr010_timer_set_next_event;
  315. fttmr010->clkevt.set_state_shutdown = fttmr010_timer_shutdown;
  316. fttmr010->clkevt.set_state_periodic = fttmr010_timer_set_periodic;
  317. fttmr010->clkevt.set_state_oneshot = fttmr010_timer_set_oneshot;
  318. fttmr010->clkevt.tick_resume = fttmr010_timer_shutdown;
  319. fttmr010->clkevt.cpumask = cpumask_of(0);
  320. fttmr010->clkevt.irq = irq;
  321. clockevents_config_and_register(&fttmr010->clkevt,
  322. fttmr010->tick_rate,
  323. 1, 0xffffffff);
  324. #ifdef CONFIG_ARM
  325. /* Also use this timer for delays */
  326. if (fttmr010->count_down)
  327. fttmr010->delay_timer.read_current_timer =
  328. fttmr010_read_current_timer_down;
  329. else
  330. fttmr010->delay_timer.read_current_timer =
  331. fttmr010_read_current_timer_up;
  332. fttmr010->delay_timer.freq = fttmr010->tick_rate;
  333. register_current_timer_delay(&fttmr010->delay_timer);
  334. #endif
  335. return 0;
  336. out_unmap:
  337. iounmap(fttmr010->base);
  338. out_free:
  339. kfree(fttmr010);
  340. out_disable_clock:
  341. clk_disable_unprepare(clk);
  342. return ret;
  343. }
  344. static __init int aspeed_timer_init(struct device_node *np)
  345. {
  346. return fttmr010_common_init(np, true);
  347. }
  348. static __init int fttmr010_timer_init(struct device_node *np)
  349. {
  350. return fttmr010_common_init(np, false);
  351. }
  352. TIMER_OF_DECLARE(fttmr010, "faraday,fttmr010", fttmr010_timer_init);
  353. TIMER_OF_DECLARE(gemini, "cortina,gemini-timer", fttmr010_timer_init);
  354. TIMER_OF_DECLARE(moxart, "moxa,moxart-timer", fttmr010_timer_init);
  355. TIMER_OF_DECLARE(ast2400, "aspeed,ast2400-timer", aspeed_timer_init);
  356. TIMER_OF_DECLARE(ast2500, "aspeed,ast2500-timer", aspeed_timer_init);