timer-fttmr010.c 11 KB

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