timer-mediatek.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Mediatek SoCs General-Purpose Timer handling.
  4. *
  5. * Copyright (C) 2014 Matthias Brugger
  6. *
  7. * Matthias Brugger <[email protected]>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/clockchips.h>
  11. #include <linux/clocksource.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irqreturn.h>
  14. #include <linux/module.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/sched_clock.h>
  18. #include <linux/slab.h>
  19. #include "timer-of.h"
  20. #define TIMER_CLK_EVT (1)
  21. #define TIMER_CLK_SRC (2)
  22. #define TIMER_SYNC_TICKS (3)
  23. /* gpt */
  24. #define GPT_IRQ_EN_REG 0x00
  25. #define GPT_IRQ_ENABLE(val) BIT((val) - 1)
  26. #define GPT_IRQ_ACK_REG 0x08
  27. #define GPT_IRQ_ACK(val) BIT((val) - 1)
  28. #define GPT_CTRL_REG(val) (0x10 * (val))
  29. #define GPT_CTRL_OP(val) (((val) & 0x3) << 4)
  30. #define GPT_CTRL_OP_ONESHOT (0)
  31. #define GPT_CTRL_OP_REPEAT (1)
  32. #define GPT_CTRL_OP_FREERUN (3)
  33. #define GPT_CTRL_CLEAR (2)
  34. #define GPT_CTRL_ENABLE (1)
  35. #define GPT_CTRL_DISABLE (0)
  36. #define GPT_CLK_REG(val) (0x04 + (0x10 * (val)))
  37. #define GPT_CLK_SRC(val) (((val) & 0x1) << 4)
  38. #define GPT_CLK_SRC_SYS13M (0)
  39. #define GPT_CLK_SRC_RTC32K (1)
  40. #define GPT_CLK_DIV1 (0x0)
  41. #define GPT_CLK_DIV2 (0x1)
  42. #define GPT_CNT_REG(val) (0x08 + (0x10 * (val)))
  43. #define GPT_CMP_REG(val) (0x0C + (0x10 * (val)))
  44. /* system timer */
  45. #define SYST_BASE (0x40)
  46. #define SYST_CON (SYST_BASE + 0x0)
  47. #define SYST_VAL (SYST_BASE + 0x4)
  48. #define SYST_CON_REG(to) (timer_of_base(to) + SYST_CON)
  49. #define SYST_VAL_REG(to) (timer_of_base(to) + SYST_VAL)
  50. /*
  51. * SYST_CON_EN: Clock enable. Shall be set to
  52. * - Start timer countdown.
  53. * - Allow timeout ticks being updated.
  54. * - Allow changing interrupt status,like clear irq pending.
  55. *
  56. * SYST_CON_IRQ_EN: Set to enable interrupt.
  57. *
  58. * SYST_CON_IRQ_CLR: Set to clear interrupt.
  59. */
  60. #define SYST_CON_EN BIT(0)
  61. #define SYST_CON_IRQ_EN BIT(1)
  62. #define SYST_CON_IRQ_CLR BIT(4)
  63. static void __iomem *gpt_sched_reg __read_mostly;
  64. static void mtk_syst_ack_irq(struct timer_of *to)
  65. {
  66. /* Clear and disable interrupt */
  67. writel(SYST_CON_EN, SYST_CON_REG(to));
  68. writel(SYST_CON_IRQ_CLR | SYST_CON_EN, SYST_CON_REG(to));
  69. }
  70. static irqreturn_t mtk_syst_handler(int irq, void *dev_id)
  71. {
  72. struct clock_event_device *clkevt = dev_id;
  73. struct timer_of *to = to_timer_of(clkevt);
  74. mtk_syst_ack_irq(to);
  75. clkevt->event_handler(clkevt);
  76. return IRQ_HANDLED;
  77. }
  78. static int mtk_syst_clkevt_next_event(unsigned long ticks,
  79. struct clock_event_device *clkevt)
  80. {
  81. struct timer_of *to = to_timer_of(clkevt);
  82. /* Enable clock to allow timeout tick update later */
  83. writel(SYST_CON_EN, SYST_CON_REG(to));
  84. /*
  85. * Write new timeout ticks. Timer shall start countdown
  86. * after timeout ticks are updated.
  87. */
  88. writel(ticks, SYST_VAL_REG(to));
  89. /* Enable interrupt */
  90. writel(SYST_CON_EN | SYST_CON_IRQ_EN, SYST_CON_REG(to));
  91. return 0;
  92. }
  93. static int mtk_syst_clkevt_shutdown(struct clock_event_device *clkevt)
  94. {
  95. /* Clear any irq */
  96. mtk_syst_ack_irq(to_timer_of(clkevt));
  97. /* Disable timer */
  98. writel(0, SYST_CON_REG(to_timer_of(clkevt)));
  99. return 0;
  100. }
  101. static int mtk_syst_clkevt_resume(struct clock_event_device *clkevt)
  102. {
  103. return mtk_syst_clkevt_shutdown(clkevt);
  104. }
  105. static int mtk_syst_clkevt_oneshot(struct clock_event_device *clkevt)
  106. {
  107. return 0;
  108. }
  109. static u64 notrace mtk_gpt_read_sched_clock(void)
  110. {
  111. return readl_relaxed(gpt_sched_reg);
  112. }
  113. static void mtk_gpt_clkevt_time_stop(struct timer_of *to, u8 timer)
  114. {
  115. u32 val;
  116. val = readl(timer_of_base(to) + GPT_CTRL_REG(timer));
  117. writel(val & ~GPT_CTRL_ENABLE, timer_of_base(to) +
  118. GPT_CTRL_REG(timer));
  119. }
  120. static void mtk_gpt_clkevt_time_setup(struct timer_of *to,
  121. unsigned long delay, u8 timer)
  122. {
  123. writel(delay, timer_of_base(to) + GPT_CMP_REG(timer));
  124. }
  125. static void mtk_gpt_clkevt_time_start(struct timer_of *to,
  126. bool periodic, u8 timer)
  127. {
  128. u32 val;
  129. /* Acknowledge interrupt */
  130. writel(GPT_IRQ_ACK(timer), timer_of_base(to) + GPT_IRQ_ACK_REG);
  131. val = readl(timer_of_base(to) + GPT_CTRL_REG(timer));
  132. /* Clear 2 bit timer operation mode field */
  133. val &= ~GPT_CTRL_OP(0x3);
  134. if (periodic)
  135. val |= GPT_CTRL_OP(GPT_CTRL_OP_REPEAT);
  136. else
  137. val |= GPT_CTRL_OP(GPT_CTRL_OP_ONESHOT);
  138. writel(val | GPT_CTRL_ENABLE | GPT_CTRL_CLEAR,
  139. timer_of_base(to) + GPT_CTRL_REG(timer));
  140. }
  141. static int mtk_gpt_clkevt_shutdown(struct clock_event_device *clk)
  142. {
  143. mtk_gpt_clkevt_time_stop(to_timer_of(clk), TIMER_CLK_EVT);
  144. return 0;
  145. }
  146. static int mtk_gpt_clkevt_set_periodic(struct clock_event_device *clk)
  147. {
  148. struct timer_of *to = to_timer_of(clk);
  149. mtk_gpt_clkevt_time_stop(to, TIMER_CLK_EVT);
  150. mtk_gpt_clkevt_time_setup(to, to->of_clk.period, TIMER_CLK_EVT);
  151. mtk_gpt_clkevt_time_start(to, true, TIMER_CLK_EVT);
  152. return 0;
  153. }
  154. static int mtk_gpt_clkevt_next_event(unsigned long event,
  155. struct clock_event_device *clk)
  156. {
  157. struct timer_of *to = to_timer_of(clk);
  158. mtk_gpt_clkevt_time_stop(to, TIMER_CLK_EVT);
  159. mtk_gpt_clkevt_time_setup(to, event, TIMER_CLK_EVT);
  160. mtk_gpt_clkevt_time_start(to, false, TIMER_CLK_EVT);
  161. return 0;
  162. }
  163. static irqreturn_t mtk_gpt_interrupt(int irq, void *dev_id)
  164. {
  165. struct clock_event_device *clkevt = (struct clock_event_device *)dev_id;
  166. struct timer_of *to = to_timer_of(clkevt);
  167. /* Acknowledge timer0 irq */
  168. writel(GPT_IRQ_ACK(TIMER_CLK_EVT), timer_of_base(to) + GPT_IRQ_ACK_REG);
  169. clkevt->event_handler(clkevt);
  170. return IRQ_HANDLED;
  171. }
  172. static void
  173. __init mtk_gpt_setup(struct timer_of *to, u8 timer, u8 option)
  174. {
  175. writel(GPT_CTRL_CLEAR | GPT_CTRL_DISABLE,
  176. timer_of_base(to) + GPT_CTRL_REG(timer));
  177. writel(GPT_CLK_SRC(GPT_CLK_SRC_SYS13M) | GPT_CLK_DIV1,
  178. timer_of_base(to) + GPT_CLK_REG(timer));
  179. writel(0x0, timer_of_base(to) + GPT_CMP_REG(timer));
  180. writel(GPT_CTRL_OP(option) | GPT_CTRL_ENABLE,
  181. timer_of_base(to) + GPT_CTRL_REG(timer));
  182. }
  183. static void mtk_gpt_enable_irq(struct timer_of *to, u8 timer)
  184. {
  185. u32 val;
  186. /* Disable all interrupts */
  187. writel(0x0, timer_of_base(to) + GPT_IRQ_EN_REG);
  188. /* Acknowledge all spurious pending interrupts */
  189. writel(0x3f, timer_of_base(to) + GPT_IRQ_ACK_REG);
  190. val = readl(timer_of_base(to) + GPT_IRQ_EN_REG);
  191. writel(val | GPT_IRQ_ENABLE(timer),
  192. timer_of_base(to) + GPT_IRQ_EN_REG);
  193. }
  194. static void mtk_gpt_resume(struct clock_event_device *clk)
  195. {
  196. struct timer_of *to = to_timer_of(clk);
  197. mtk_gpt_enable_irq(to, TIMER_CLK_EVT);
  198. }
  199. static void mtk_gpt_suspend(struct clock_event_device *clk)
  200. {
  201. struct timer_of *to = to_timer_of(clk);
  202. /* Disable all interrupts */
  203. writel(0x0, timer_of_base(to) + GPT_IRQ_EN_REG);
  204. /*
  205. * This is called with interrupts disabled,
  206. * so we need to ack any interrupt that is pending
  207. * or for example ATF will prevent a suspend from completing.
  208. */
  209. writel(0x3f, timer_of_base(to) + GPT_IRQ_ACK_REG);
  210. }
  211. static struct timer_of to = {
  212. .flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK,
  213. .clkevt = {
  214. .name = "mtk-clkevt",
  215. .rating = 300,
  216. .cpumask = cpu_possible_mask,
  217. },
  218. .of_irq = {
  219. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  220. },
  221. };
  222. static int __init mtk_syst_init(struct device_node *node)
  223. {
  224. int ret;
  225. to.clkevt.features = CLOCK_EVT_FEAT_DYNIRQ | CLOCK_EVT_FEAT_ONESHOT;
  226. to.clkevt.set_state_shutdown = mtk_syst_clkevt_shutdown;
  227. to.clkevt.set_state_oneshot = mtk_syst_clkevt_oneshot;
  228. to.clkevt.tick_resume = mtk_syst_clkevt_resume;
  229. to.clkevt.set_next_event = mtk_syst_clkevt_next_event;
  230. to.of_irq.handler = mtk_syst_handler;
  231. ret = timer_of_init(node, &to);
  232. if (ret)
  233. return ret;
  234. clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
  235. TIMER_SYNC_TICKS, 0xffffffff);
  236. return 0;
  237. }
  238. static int __init mtk_gpt_init(struct device_node *node)
  239. {
  240. int ret;
  241. to.clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  242. to.clkevt.set_state_shutdown = mtk_gpt_clkevt_shutdown;
  243. to.clkevt.set_state_periodic = mtk_gpt_clkevt_set_periodic;
  244. to.clkevt.set_state_oneshot = mtk_gpt_clkevt_shutdown;
  245. to.clkevt.tick_resume = mtk_gpt_clkevt_shutdown;
  246. to.clkevt.set_next_event = mtk_gpt_clkevt_next_event;
  247. to.clkevt.suspend = mtk_gpt_suspend;
  248. to.clkevt.resume = mtk_gpt_resume;
  249. to.of_irq.handler = mtk_gpt_interrupt;
  250. ret = timer_of_init(node, &to);
  251. if (ret)
  252. return ret;
  253. /* Configure clock source */
  254. mtk_gpt_setup(&to, TIMER_CLK_SRC, GPT_CTRL_OP_FREERUN);
  255. clocksource_mmio_init(timer_of_base(&to) + GPT_CNT_REG(TIMER_CLK_SRC),
  256. node->name, timer_of_rate(&to), 300, 32,
  257. clocksource_mmio_readl_up);
  258. gpt_sched_reg = timer_of_base(&to) + GPT_CNT_REG(TIMER_CLK_SRC);
  259. sched_clock_register(mtk_gpt_read_sched_clock, 32, timer_of_rate(&to));
  260. /* Configure clock event */
  261. mtk_gpt_setup(&to, TIMER_CLK_EVT, GPT_CTRL_OP_REPEAT);
  262. clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
  263. TIMER_SYNC_TICKS, 0xffffffff);
  264. mtk_gpt_enable_irq(&to, TIMER_CLK_EVT);
  265. return 0;
  266. }
  267. #ifndef MODULE
  268. TIMER_OF_DECLARE(mtk_mt6577, "mediatek,mt6577-timer", mtk_gpt_init);
  269. TIMER_OF_DECLARE(mtk_mt6765, "mediatek,mt6765-timer", mtk_syst_init);
  270. #else
  271. static int mtk_timer_probe(struct platform_device *pdev)
  272. {
  273. int (*timer_init)(struct device_node *node);
  274. struct device_node *np = pdev->dev.of_node;
  275. timer_init = of_device_get_match_data(&pdev->dev);
  276. return timer_init(np);
  277. }
  278. static const struct of_device_id mtk_timer_match_table[] = {
  279. { .compatible = "mediatek,mt6577-timer", .data = mtk_gpt_init },
  280. { .compatible = "mediatek,mt6765-timer", .data = mtk_syst_init },
  281. { /* sentinel */ }
  282. };
  283. static struct platform_driver mtk_timer_driver = {
  284. .probe = mtk_timer_probe,
  285. .driver = {
  286. .name = "mediatek-timer",
  287. .of_match_table = mtk_timer_match_table,
  288. },
  289. };
  290. module_platform_driver(mtk_timer_driver);
  291. MODULE_DESCRIPTION("MediaTek Timer driver");
  292. MODULE_LICENSE("GPL v2");
  293. #endif