timer-fttmr010.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. if (fttmr010->count_down) {
  120. /*
  121. * ASPEED Timer Controller will load TIMER1_LOAD register
  122. * into TIMER1_COUNT register when the timer is re-enabled.
  123. */
  124. writel(cycles, fttmr010->base + TIMER1_LOAD);
  125. } else {
  126. /* Setup the match register forward in time */
  127. cr = readl(fttmr010->base + TIMER1_COUNT);
  128. writel(cr + cycles, fttmr010->base + TIMER1_MATCH1);
  129. }
  130. /* Start */
  131. cr = readl(fttmr010->base + TIMER_CR);
  132. cr |= fttmr010->t1_enable_val;
  133. writel(cr, fttmr010->base + TIMER_CR);
  134. return 0;
  135. }
  136. static int fttmr010_timer_shutdown(struct clock_event_device *evt)
  137. {
  138. struct fttmr010 *fttmr010 = to_fttmr010(evt);
  139. u32 cr;
  140. /* Stop */
  141. cr = readl(fttmr010->base + TIMER_CR);
  142. cr &= ~fttmr010->t1_enable_val;
  143. writel(cr, fttmr010->base + TIMER_CR);
  144. return 0;
  145. }
  146. static int fttmr010_timer_set_oneshot(struct clock_event_device *evt)
  147. {
  148. struct fttmr010 *fttmr010 = to_fttmr010(evt);
  149. u32 cr;
  150. /* Stop */
  151. cr = readl(fttmr010->base + TIMER_CR);
  152. cr &= ~fttmr010->t1_enable_val;
  153. writel(cr, fttmr010->base + TIMER_CR);
  154. /* Setup counter start from 0 or ~0 */
  155. writel(0, fttmr010->base + TIMER1_COUNT);
  156. if (fttmr010->count_down)
  157. writel(~0, fttmr010->base + TIMER1_LOAD);
  158. else
  159. writel(0, fttmr010->base + TIMER1_LOAD);
  160. /* Enable interrupt */
  161. cr = readl(fttmr010->base + TIMER_INTR_MASK);
  162. cr &= ~(TIMER_1_INT_OVERFLOW | TIMER_1_INT_MATCH2);
  163. cr |= TIMER_1_INT_MATCH1;
  164. writel(cr, fttmr010->base + TIMER_INTR_MASK);
  165. return 0;
  166. }
  167. static int fttmr010_timer_set_periodic(struct clock_event_device *evt)
  168. {
  169. struct fttmr010 *fttmr010 = to_fttmr010(evt);
  170. u32 period = DIV_ROUND_CLOSEST(fttmr010->tick_rate, HZ);
  171. u32 cr;
  172. /* Stop */
  173. cr = readl(fttmr010->base + TIMER_CR);
  174. cr &= ~fttmr010->t1_enable_val;
  175. writel(cr, fttmr010->base + TIMER_CR);
  176. /* Setup timer to fire at 1/HZ intervals. */
  177. if (fttmr010->count_down) {
  178. writel(period, fttmr010->base + TIMER1_LOAD);
  179. writel(0, fttmr010->base + TIMER1_MATCH1);
  180. } else {
  181. cr = 0xffffffff - (period - 1);
  182. writel(cr, fttmr010->base + TIMER1_COUNT);
  183. writel(cr, fttmr010->base + TIMER1_LOAD);
  184. /* Enable interrupt on overflow */
  185. cr = readl(fttmr010->base + TIMER_INTR_MASK);
  186. cr &= ~(TIMER_1_INT_MATCH1 | TIMER_1_INT_MATCH2);
  187. cr |= TIMER_1_INT_OVERFLOW;
  188. writel(cr, fttmr010->base + TIMER_INTR_MASK);
  189. }
  190. /* Start the timer */
  191. cr = readl(fttmr010->base + TIMER_CR);
  192. cr |= fttmr010->t1_enable_val;
  193. writel(cr, fttmr010->base + TIMER_CR);
  194. return 0;
  195. }
  196. /*
  197. * IRQ handler for the timer
  198. */
  199. static irqreturn_t fttmr010_timer_interrupt(int irq, void *dev_id)
  200. {
  201. struct clock_event_device *evt = dev_id;
  202. evt->event_handler(evt);
  203. return IRQ_HANDLED;
  204. }
  205. static int __init fttmr010_common_init(struct device_node *np, bool is_aspeed)
  206. {
  207. struct fttmr010 *fttmr010;
  208. int irq;
  209. struct clk *clk;
  210. int ret;
  211. u32 val;
  212. /*
  213. * These implementations require a clock reference.
  214. * FIXME: we currently only support clocking using PCLK
  215. * and using EXTCLK is not supported in the driver.
  216. */
  217. clk = of_clk_get_by_name(np, "PCLK");
  218. if (IS_ERR(clk)) {
  219. pr_err("could not get PCLK\n");
  220. return PTR_ERR(clk);
  221. }
  222. ret = clk_prepare_enable(clk);
  223. if (ret) {
  224. pr_err("failed to enable PCLK\n");
  225. return ret;
  226. }
  227. fttmr010 = kzalloc(sizeof(*fttmr010), GFP_KERNEL);
  228. if (!fttmr010) {
  229. ret = -ENOMEM;
  230. goto out_disable_clock;
  231. }
  232. fttmr010->tick_rate = clk_get_rate(clk);
  233. fttmr010->base = of_iomap(np, 0);
  234. if (!fttmr010->base) {
  235. pr_err("Can't remap registers\n");
  236. ret = -ENXIO;
  237. goto out_free;
  238. }
  239. /* IRQ for timer 1 */
  240. irq = irq_of_parse_and_map(np, 0);
  241. if (irq <= 0) {
  242. pr_err("Can't parse IRQ\n");
  243. ret = -EINVAL;
  244. goto out_unmap;
  245. }
  246. /*
  247. * The Aspeed AST2400 moves bits around in the control register,
  248. * otherwise it works the same.
  249. */
  250. if (is_aspeed) {
  251. fttmr010->t1_enable_val = TIMER_1_CR_ASPEED_ENABLE |
  252. TIMER_1_CR_ASPEED_INT;
  253. /* Downward not available */
  254. fttmr010->count_down = true;
  255. } else {
  256. fttmr010->t1_enable_val = TIMER_1_CR_ENABLE | TIMER_1_CR_INT;
  257. }
  258. /*
  259. * Reset the interrupt mask and status
  260. */
  261. writel(TIMER_INT_ALL_MASK, fttmr010->base + TIMER_INTR_MASK);
  262. writel(0, fttmr010->base + TIMER_INTR_STATE);
  263. /*
  264. * Enable timer 1 count up, timer 2 count up, except on Aspeed,
  265. * where everything just counts down.
  266. */
  267. if (is_aspeed)
  268. val = TIMER_2_CR_ASPEED_ENABLE;
  269. else {
  270. val = TIMER_2_CR_ENABLE;
  271. if (!fttmr010->count_down)
  272. val |= TIMER_1_CR_UPDOWN | TIMER_2_CR_UPDOWN;
  273. }
  274. writel(val, fttmr010->base + TIMER_CR);
  275. /*
  276. * Setup free-running clocksource timer (interrupts
  277. * disabled.)
  278. */
  279. local_fttmr = fttmr010;
  280. writel(0, fttmr010->base + TIMER2_COUNT);
  281. writel(0, fttmr010->base + TIMER2_MATCH1);
  282. writel(0, fttmr010->base + TIMER2_MATCH2);
  283. if (fttmr010->count_down) {
  284. writel(~0, fttmr010->base + TIMER2_LOAD);
  285. clocksource_mmio_init(fttmr010->base + TIMER2_COUNT,
  286. "FTTMR010-TIMER2",
  287. fttmr010->tick_rate,
  288. 300, 32, clocksource_mmio_readl_down);
  289. sched_clock_register(fttmr010_read_sched_clock_down, 32,
  290. fttmr010->tick_rate);
  291. } else {
  292. writel(0, fttmr010->base + TIMER2_LOAD);
  293. clocksource_mmio_init(fttmr010->base + TIMER2_COUNT,
  294. "FTTMR010-TIMER2",
  295. fttmr010->tick_rate,
  296. 300, 32, clocksource_mmio_readl_up);
  297. sched_clock_register(fttmr010_read_sched_clock_up, 32,
  298. fttmr010->tick_rate);
  299. }
  300. /*
  301. * Setup clockevent timer (interrupt-driven) on timer 1.
  302. */
  303. writel(0, fttmr010->base + TIMER1_COUNT);
  304. writel(0, fttmr010->base + TIMER1_LOAD);
  305. writel(0, fttmr010->base + TIMER1_MATCH1);
  306. writel(0, fttmr010->base + TIMER1_MATCH2);
  307. ret = request_irq(irq, fttmr010_timer_interrupt, IRQF_TIMER,
  308. "FTTMR010-TIMER1", &fttmr010->clkevt);
  309. if (ret) {
  310. pr_err("FTTMR010-TIMER1 no IRQ\n");
  311. goto out_unmap;
  312. }
  313. fttmr010->clkevt.name = "FTTMR010-TIMER1";
  314. /* Reasonably fast and accurate clock event */
  315. fttmr010->clkevt.rating = 300;
  316. fttmr010->clkevt.features = CLOCK_EVT_FEAT_PERIODIC |
  317. CLOCK_EVT_FEAT_ONESHOT;
  318. fttmr010->clkevt.set_next_event = fttmr010_timer_set_next_event;
  319. fttmr010->clkevt.set_state_shutdown = fttmr010_timer_shutdown;
  320. fttmr010->clkevt.set_state_periodic = fttmr010_timer_set_periodic;
  321. fttmr010->clkevt.set_state_oneshot = fttmr010_timer_set_oneshot;
  322. fttmr010->clkevt.tick_resume = fttmr010_timer_shutdown;
  323. fttmr010->clkevt.cpumask = cpumask_of(0);
  324. fttmr010->clkevt.irq = irq;
  325. clockevents_config_and_register(&fttmr010->clkevt,
  326. fttmr010->tick_rate,
  327. 1, 0xffffffff);
  328. #ifdef CONFIG_ARM
  329. /* Also use this timer for delays */
  330. if (fttmr010->count_down)
  331. fttmr010->delay_timer.read_current_timer =
  332. fttmr010_read_current_timer_down;
  333. else
  334. fttmr010->delay_timer.read_current_timer =
  335. fttmr010_read_current_timer_up;
  336. fttmr010->delay_timer.freq = fttmr010->tick_rate;
  337. register_current_timer_delay(&fttmr010->delay_timer);
  338. #endif
  339. return 0;
  340. out_unmap:
  341. iounmap(fttmr010->base);
  342. out_free:
  343. kfree(fttmr010);
  344. out_disable_clock:
  345. clk_disable_unprepare(clk);
  346. return ret;
  347. }
  348. static __init int aspeed_timer_init(struct device_node *np)
  349. {
  350. return fttmr010_common_init(np, true);
  351. }
  352. static __init int fttmr010_timer_init(struct device_node *np)
  353. {
  354. return fttmr010_common_init(np, false);
  355. }
  356. TIMER_OF_DECLARE(fttmr010, "faraday,fttmr010", fttmr010_timer_init);
  357. TIMER_OF_DECLARE(gemini, "cortina,gemini-timer", fttmr010_timer_init);
  358. TIMER_OF_DECLARE(moxart, "moxa,moxart-timer", fttmr010_timer_init);
  359. TIMER_OF_DECLARE(ast2400, "aspeed,ast2400-timer", aspeed_timer_init);
  360. TIMER_OF_DECLARE(ast2500, "aspeed,ast2500-timer", aspeed_timer_init);