timer-fttmr010.c 11 KB

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