timer-fttmr010.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Faraday Technology FTTMR010 timer driver
  4. * Copyright (C) 2017 Linus Walleij <[email protected]>
  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 <[email protected]>
  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 common for all the timer variants.
  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. /*
  39. * Control register set to clear for ast2600 only.
  40. */
  41. #define AST2600_TIMER_CR_CLR (0x3c)
  42. /*
  43. * Control register (TMC30) bit fields for fttmr010/gemini/moxart timers.
  44. */
  45. #define TIMER_1_CR_ENABLE BIT(0)
  46. #define TIMER_1_CR_CLOCK BIT(1)
  47. #define TIMER_1_CR_INT BIT(2)
  48. #define TIMER_2_CR_ENABLE BIT(3)
  49. #define TIMER_2_CR_CLOCK BIT(4)
  50. #define TIMER_2_CR_INT BIT(5)
  51. #define TIMER_3_CR_ENABLE BIT(6)
  52. #define TIMER_3_CR_CLOCK BIT(7)
  53. #define TIMER_3_CR_INT BIT(8)
  54. #define TIMER_1_CR_UPDOWN BIT(9)
  55. #define TIMER_2_CR_UPDOWN BIT(10)
  56. #define TIMER_3_CR_UPDOWN BIT(11)
  57. /*
  58. * Control register (TMC30) bit fields for aspeed ast2400/ast2500 timers.
  59. * The aspeed timers move bits around in the control register and lacks
  60. * bits for setting the timer to count upwards.
  61. */
  62. #define TIMER_1_CR_ASPEED_ENABLE BIT(0)
  63. #define TIMER_1_CR_ASPEED_CLOCK BIT(1)
  64. #define TIMER_1_CR_ASPEED_INT BIT(2)
  65. #define TIMER_2_CR_ASPEED_ENABLE BIT(4)
  66. #define TIMER_2_CR_ASPEED_CLOCK BIT(5)
  67. #define TIMER_2_CR_ASPEED_INT BIT(6)
  68. #define TIMER_3_CR_ASPEED_ENABLE BIT(8)
  69. #define TIMER_3_CR_ASPEED_CLOCK BIT(9)
  70. #define TIMER_3_CR_ASPEED_INT BIT(10)
  71. /*
  72. * Interrupt status/mask register definitions for fttmr010/gemini/moxart
  73. * timers.
  74. * The registers don't exist and they are not needed on aspeed timers
  75. * because:
  76. * - aspeed timer overflow interrupt is controlled by bits in Control
  77. * Register (TMC30).
  78. * - aspeed timers always generate interrupt when either one of the
  79. * Match registers equals to Status register.
  80. */
  81. #define TIMER_INTR_STATE (0x34)
  82. #define TIMER_INTR_MASK (0x38)
  83. #define TIMER_1_INT_MATCH1 BIT(0)
  84. #define TIMER_1_INT_MATCH2 BIT(1)
  85. #define TIMER_1_INT_OVERFLOW BIT(2)
  86. #define TIMER_2_INT_MATCH1 BIT(3)
  87. #define TIMER_2_INT_MATCH2 BIT(4)
  88. #define TIMER_2_INT_OVERFLOW BIT(5)
  89. #define TIMER_3_INT_MATCH1 BIT(6)
  90. #define TIMER_3_INT_MATCH2 BIT(7)
  91. #define TIMER_3_INT_OVERFLOW BIT(8)
  92. #define TIMER_INT_ALL_MASK 0x1ff
  93. struct fttmr010 {
  94. void __iomem *base;
  95. unsigned int tick_rate;
  96. bool is_aspeed;
  97. u32 t1_enable_val;
  98. struct clock_event_device clkevt;
  99. int (*timer_shutdown)(struct clock_event_device *evt);
  100. #ifdef CONFIG_ARM
  101. struct delay_timer delay_timer;
  102. #endif
  103. };
  104. /*
  105. * A local singleton used by sched_clock and delay timer reads, which are
  106. * fast and stateless
  107. */
  108. static struct fttmr010 *local_fttmr;
  109. static inline struct fttmr010 *to_fttmr010(struct clock_event_device *evt)
  110. {
  111. return container_of(evt, struct fttmr010, clkevt);
  112. }
  113. static unsigned long fttmr010_read_current_timer_up(void)
  114. {
  115. return readl(local_fttmr->base + TIMER2_COUNT);
  116. }
  117. static unsigned long fttmr010_read_current_timer_down(void)
  118. {
  119. return ~readl(local_fttmr->base + TIMER2_COUNT);
  120. }
  121. static u64 notrace fttmr010_read_sched_clock_up(void)
  122. {
  123. return fttmr010_read_current_timer_up();
  124. }
  125. static u64 notrace fttmr010_read_sched_clock_down(void)
  126. {
  127. return fttmr010_read_current_timer_down();
  128. }
  129. static int fttmr010_timer_set_next_event(unsigned long cycles,
  130. struct clock_event_device *evt)
  131. {
  132. struct fttmr010 *fttmr010 = to_fttmr010(evt);
  133. u32 cr;
  134. /* Stop */
  135. fttmr010->timer_shutdown(evt);
  136. if (fttmr010->is_aspeed) {
  137. /*
  138. * ASPEED Timer Controller will load TIMER1_LOAD register
  139. * into TIMER1_COUNT register when the timer is re-enabled.
  140. */
  141. writel(cycles, fttmr010->base + TIMER1_LOAD);
  142. } else {
  143. /* Setup the match register forward in time */
  144. cr = readl(fttmr010->base + TIMER1_COUNT);
  145. writel(cr + cycles, fttmr010->base + TIMER1_MATCH1);
  146. }
  147. /* Start */
  148. cr = readl(fttmr010->base + TIMER_CR);
  149. cr |= fttmr010->t1_enable_val;
  150. writel(cr, fttmr010->base + TIMER_CR);
  151. return 0;
  152. }
  153. static int ast2600_timer_shutdown(struct clock_event_device *evt)
  154. {
  155. struct fttmr010 *fttmr010 = to_fttmr010(evt);
  156. /* Stop */
  157. writel(fttmr010->t1_enable_val, fttmr010->base + AST2600_TIMER_CR_CLR);
  158. return 0;
  159. }
  160. static int fttmr010_timer_shutdown(struct clock_event_device *evt)
  161. {
  162. struct fttmr010 *fttmr010 = to_fttmr010(evt);
  163. u32 cr;
  164. /* Stop */
  165. cr = readl(fttmr010->base + TIMER_CR);
  166. cr &= ~fttmr010->t1_enable_val;
  167. writel(cr, fttmr010->base + TIMER_CR);
  168. return 0;
  169. }
  170. static int fttmr010_timer_set_oneshot(struct clock_event_device *evt)
  171. {
  172. struct fttmr010 *fttmr010 = to_fttmr010(evt);
  173. u32 cr;
  174. /* Stop */
  175. fttmr010->timer_shutdown(evt);
  176. /* Setup counter start from 0 or ~0 */
  177. writel(0, fttmr010->base + TIMER1_COUNT);
  178. if (fttmr010->is_aspeed) {
  179. writel(~0, fttmr010->base + TIMER1_LOAD);
  180. } else {
  181. writel(0, fttmr010->base + TIMER1_LOAD);
  182. /* Enable interrupt */
  183. cr = readl(fttmr010->base + TIMER_INTR_MASK);
  184. cr &= ~(TIMER_1_INT_OVERFLOW | TIMER_1_INT_MATCH2);
  185. cr |= TIMER_1_INT_MATCH1;
  186. writel(cr, fttmr010->base + TIMER_INTR_MASK);
  187. }
  188. return 0;
  189. }
  190. static int fttmr010_timer_set_periodic(struct clock_event_device *evt)
  191. {
  192. struct fttmr010 *fttmr010 = to_fttmr010(evt);
  193. u32 period = DIV_ROUND_CLOSEST(fttmr010->tick_rate, HZ);
  194. u32 cr;
  195. /* Stop */
  196. fttmr010->timer_shutdown(evt);
  197. /* Setup timer to fire at 1/HZ intervals. */
  198. if (fttmr010->is_aspeed) {
  199. writel(period, fttmr010->base + TIMER1_LOAD);
  200. } else {
  201. cr = 0xffffffff - (period - 1);
  202. writel(cr, fttmr010->base + TIMER1_COUNT);
  203. writel(cr, fttmr010->base + TIMER1_LOAD);
  204. /* Enable interrupt on overflow */
  205. cr = readl(fttmr010->base + TIMER_INTR_MASK);
  206. cr &= ~(TIMER_1_INT_MATCH1 | TIMER_1_INT_MATCH2);
  207. cr |= TIMER_1_INT_OVERFLOW;
  208. writel(cr, fttmr010->base + TIMER_INTR_MASK);
  209. }
  210. /* Start the timer */
  211. cr = readl(fttmr010->base + TIMER_CR);
  212. cr |= fttmr010->t1_enable_val;
  213. writel(cr, fttmr010->base + TIMER_CR);
  214. return 0;
  215. }
  216. /*
  217. * IRQ handler for the timer
  218. */
  219. static irqreturn_t fttmr010_timer_interrupt(int irq, void *dev_id)
  220. {
  221. struct clock_event_device *evt = dev_id;
  222. evt->event_handler(evt);
  223. return IRQ_HANDLED;
  224. }
  225. static irqreturn_t ast2600_timer_interrupt(int irq, void *dev_id)
  226. {
  227. struct clock_event_device *evt = dev_id;
  228. struct fttmr010 *fttmr010 = to_fttmr010(evt);
  229. writel(0x1, fttmr010->base + TIMER_INTR_STATE);
  230. evt->event_handler(evt);
  231. return IRQ_HANDLED;
  232. }
  233. static int __init fttmr010_common_init(struct device_node *np,
  234. bool is_aspeed, bool is_ast2600)
  235. {
  236. struct fttmr010 *fttmr010;
  237. int irq;
  238. struct clk *clk;
  239. int ret;
  240. u32 val;
  241. /*
  242. * These implementations require a clock reference.
  243. * FIXME: we currently only support clocking using PCLK
  244. * and using EXTCLK is not supported in the driver.
  245. */
  246. clk = of_clk_get_by_name(np, "PCLK");
  247. if (IS_ERR(clk)) {
  248. pr_err("could not get PCLK\n");
  249. return PTR_ERR(clk);
  250. }
  251. ret = clk_prepare_enable(clk);
  252. if (ret) {
  253. pr_err("failed to enable PCLK\n");
  254. return ret;
  255. }
  256. fttmr010 = kzalloc(sizeof(*fttmr010), GFP_KERNEL);
  257. if (!fttmr010) {
  258. ret = -ENOMEM;
  259. goto out_disable_clock;
  260. }
  261. fttmr010->tick_rate = clk_get_rate(clk);
  262. fttmr010->base = of_iomap(np, 0);
  263. if (!fttmr010->base) {
  264. pr_err("Can't remap registers\n");
  265. ret = -ENXIO;
  266. goto out_free;
  267. }
  268. /* IRQ for timer 1 */
  269. irq = irq_of_parse_and_map(np, 0);
  270. if (irq <= 0) {
  271. pr_err("Can't parse IRQ\n");
  272. ret = -EINVAL;
  273. goto out_unmap;
  274. }
  275. /*
  276. * The Aspeed timers move bits around in the control register.
  277. */
  278. if (is_aspeed) {
  279. fttmr010->t1_enable_val = TIMER_1_CR_ASPEED_ENABLE |
  280. TIMER_1_CR_ASPEED_INT;
  281. fttmr010->is_aspeed = true;
  282. } else {
  283. fttmr010->t1_enable_val = TIMER_1_CR_ENABLE | TIMER_1_CR_INT;
  284. /*
  285. * Reset the interrupt mask and status
  286. */
  287. writel(TIMER_INT_ALL_MASK, fttmr010->base + TIMER_INTR_MASK);
  288. writel(0, fttmr010->base + TIMER_INTR_STATE);
  289. }
  290. /*
  291. * Enable timer 1 count up, timer 2 count up, except on Aspeed,
  292. * where everything just counts down.
  293. */
  294. if (is_aspeed)
  295. val = TIMER_2_CR_ASPEED_ENABLE;
  296. else {
  297. val = TIMER_2_CR_ENABLE | TIMER_1_CR_UPDOWN |
  298. TIMER_2_CR_UPDOWN;
  299. }
  300. writel(val, fttmr010->base + TIMER_CR);
  301. /*
  302. * Setup free-running clocksource timer (interrupts
  303. * disabled.)
  304. */
  305. local_fttmr = fttmr010;
  306. writel(0, fttmr010->base + TIMER2_COUNT);
  307. writel(0, fttmr010->base + TIMER2_MATCH1);
  308. writel(0, fttmr010->base + TIMER2_MATCH2);
  309. if (fttmr010->is_aspeed) {
  310. writel(~0, fttmr010->base + TIMER2_LOAD);
  311. clocksource_mmio_init(fttmr010->base + TIMER2_COUNT,
  312. "FTTMR010-TIMER2",
  313. fttmr010->tick_rate,
  314. 300, 32, clocksource_mmio_readl_down);
  315. sched_clock_register(fttmr010_read_sched_clock_down, 32,
  316. fttmr010->tick_rate);
  317. } else {
  318. writel(0, fttmr010->base + TIMER2_LOAD);
  319. clocksource_mmio_init(fttmr010->base + TIMER2_COUNT,
  320. "FTTMR010-TIMER2",
  321. fttmr010->tick_rate,
  322. 300, 32, clocksource_mmio_readl_up);
  323. sched_clock_register(fttmr010_read_sched_clock_up, 32,
  324. fttmr010->tick_rate);
  325. }
  326. /*
  327. * Setup clockevent timer (interrupt-driven) on timer 1.
  328. */
  329. writel(0, fttmr010->base + TIMER1_COUNT);
  330. writel(0, fttmr010->base + TIMER1_LOAD);
  331. writel(0, fttmr010->base + TIMER1_MATCH1);
  332. writel(0, fttmr010->base + TIMER1_MATCH2);
  333. if (is_ast2600) {
  334. fttmr010->timer_shutdown = ast2600_timer_shutdown;
  335. ret = request_irq(irq, ast2600_timer_interrupt,
  336. IRQF_TIMER, "FTTMR010-TIMER1",
  337. &fttmr010->clkevt);
  338. } else {
  339. fttmr010->timer_shutdown = fttmr010_timer_shutdown;
  340. ret = request_irq(irq, fttmr010_timer_interrupt,
  341. IRQF_TIMER, "FTTMR010-TIMER1",
  342. &fttmr010->clkevt);
  343. }
  344. if (ret) {
  345. pr_err("FTTMR010-TIMER1 no IRQ\n");
  346. goto out_unmap;
  347. }
  348. fttmr010->clkevt.name = "FTTMR010-TIMER1";
  349. /* Reasonably fast and accurate clock event */
  350. fttmr010->clkevt.rating = 300;
  351. fttmr010->clkevt.features = CLOCK_EVT_FEAT_PERIODIC |
  352. CLOCK_EVT_FEAT_ONESHOT;
  353. fttmr010->clkevt.set_next_event = fttmr010_timer_set_next_event;
  354. fttmr010->clkevt.set_state_shutdown = fttmr010->timer_shutdown;
  355. fttmr010->clkevt.set_state_periodic = fttmr010_timer_set_periodic;
  356. fttmr010->clkevt.set_state_oneshot = fttmr010_timer_set_oneshot;
  357. fttmr010->clkevt.tick_resume = fttmr010->timer_shutdown;
  358. fttmr010->clkevt.cpumask = cpumask_of(0);
  359. fttmr010->clkevt.irq = irq;
  360. clockevents_config_and_register(&fttmr010->clkevt,
  361. fttmr010->tick_rate,
  362. 1, 0xffffffff);
  363. #ifdef CONFIG_ARM
  364. /* Also use this timer for delays */
  365. if (fttmr010->is_aspeed)
  366. fttmr010->delay_timer.read_current_timer =
  367. fttmr010_read_current_timer_down;
  368. else
  369. fttmr010->delay_timer.read_current_timer =
  370. fttmr010_read_current_timer_up;
  371. fttmr010->delay_timer.freq = fttmr010->tick_rate;
  372. register_current_timer_delay(&fttmr010->delay_timer);
  373. #endif
  374. return 0;
  375. out_unmap:
  376. iounmap(fttmr010->base);
  377. out_free:
  378. kfree(fttmr010);
  379. out_disable_clock:
  380. clk_disable_unprepare(clk);
  381. return ret;
  382. }
  383. static __init int ast2600_timer_init(struct device_node *np)
  384. {
  385. return fttmr010_common_init(np, true, true);
  386. }
  387. static __init int aspeed_timer_init(struct device_node *np)
  388. {
  389. return fttmr010_common_init(np, true, false);
  390. }
  391. static __init int fttmr010_timer_init(struct device_node *np)
  392. {
  393. return fttmr010_common_init(np, false, false);
  394. }
  395. TIMER_OF_DECLARE(fttmr010, "faraday,fttmr010", fttmr010_timer_init);
  396. TIMER_OF_DECLARE(gemini, "cortina,gemini-timer", fttmr010_timer_init);
  397. TIMER_OF_DECLARE(moxart, "moxa,moxart-timer", fttmr010_timer_init);
  398. TIMER_OF_DECLARE(ast2400, "aspeed,ast2400-timer", aspeed_timer_init);
  399. TIMER_OF_DECLARE(ast2500, "aspeed,ast2500-timer", aspeed_timer_init);
  400. TIMER_OF_DECLARE(ast2600, "aspeed,ast2600-timer", ast2600_timer_init);