timer-mediatek.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Mediatek SoCs General-Purpose Timer handling.
  3. *
  4. * Copyright (C) 2014 Matthias Brugger
  5. *
  6. * Matthias Brugger <matthias.bgg@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/clockchips.h>
  20. #include <linux/clocksource.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/irqreturn.h>
  23. #include <linux/sched_clock.h>
  24. #include <linux/slab.h>
  25. #include "timer-of.h"
  26. #define TIMER_CLK_EVT (1)
  27. #define TIMER_CLK_SRC (2)
  28. #define TIMER_SYNC_TICKS (3)
  29. /* gpt */
  30. #define GPT_IRQ_EN_REG 0x00
  31. #define GPT_IRQ_ENABLE(val) BIT((val) - 1)
  32. #define GPT_IRQ_ACK_REG 0x08
  33. #define GPT_IRQ_ACK(val) BIT((val) - 1)
  34. #define GPT_CTRL_REG(val) (0x10 * (val))
  35. #define GPT_CTRL_OP(val) (((val) & 0x3) << 4)
  36. #define GPT_CTRL_OP_ONESHOT (0)
  37. #define GPT_CTRL_OP_REPEAT (1)
  38. #define GPT_CTRL_OP_FREERUN (3)
  39. #define GPT_CTRL_CLEAR (2)
  40. #define GPT_CTRL_ENABLE (1)
  41. #define GPT_CTRL_DISABLE (0)
  42. #define GPT_CLK_REG(val) (0x04 + (0x10 * (val)))
  43. #define GPT_CLK_SRC(val) (((val) & 0x1) << 4)
  44. #define GPT_CLK_SRC_SYS13M (0)
  45. #define GPT_CLK_SRC_RTC32K (1)
  46. #define GPT_CLK_DIV1 (0x0)
  47. #define GPT_CLK_DIV2 (0x1)
  48. #define GPT_CNT_REG(val) (0x08 + (0x10 * (val)))
  49. #define GPT_CMP_REG(val) (0x0C + (0x10 * (val)))
  50. /* system timer */
  51. #define SYST_BASE (0x40)
  52. #define SYST_CON (SYST_BASE + 0x0)
  53. #define SYST_VAL (SYST_BASE + 0x4)
  54. #define SYST_CON_REG(to) (timer_of_base(to) + SYST_CON)
  55. #define SYST_VAL_REG(to) (timer_of_base(to) + SYST_VAL)
  56. /*
  57. * SYST_CON_EN: Clock enable. Shall be set to
  58. * - Start timer countdown.
  59. * - Allow timeout ticks being updated.
  60. * - Allow changing interrupt functions.
  61. *
  62. * SYST_CON_IRQ_EN: Set to allow interrupt.
  63. *
  64. * SYST_CON_IRQ_CLR: Set to clear interrupt.
  65. */
  66. #define SYST_CON_EN BIT(0)
  67. #define SYST_CON_IRQ_EN BIT(1)
  68. #define SYST_CON_IRQ_CLR BIT(4)
  69. static void __iomem *gpt_sched_reg __read_mostly;
  70. static void mtk_syst_ack_irq(struct timer_of *to)
  71. {
  72. /* Clear and disable interrupt */
  73. writel(SYST_CON_IRQ_CLR | SYST_CON_EN, SYST_CON_REG(to));
  74. }
  75. static irqreturn_t mtk_syst_handler(int irq, void *dev_id)
  76. {
  77. struct clock_event_device *clkevt = dev_id;
  78. struct timer_of *to = to_timer_of(clkevt);
  79. mtk_syst_ack_irq(to);
  80. clkevt->event_handler(clkevt);
  81. return IRQ_HANDLED;
  82. }
  83. static int mtk_syst_clkevt_next_event(unsigned long ticks,
  84. struct clock_event_device *clkevt)
  85. {
  86. struct timer_of *to = to_timer_of(clkevt);
  87. /* Enable clock to allow timeout tick update later */
  88. writel(SYST_CON_EN, SYST_CON_REG(to));
  89. /*
  90. * Write new timeout ticks. Timer shall start countdown
  91. * after timeout ticks are updated.
  92. */
  93. writel(ticks, SYST_VAL_REG(to));
  94. /* Enable interrupt */
  95. writel(SYST_CON_EN | SYST_CON_IRQ_EN, SYST_CON_REG(to));
  96. return 0;
  97. }
  98. static int mtk_syst_clkevt_shutdown(struct clock_event_device *clkevt)
  99. {
  100. /* Disable timer */
  101. writel(0, SYST_CON_REG(to_timer_of(clkevt)));
  102. return 0;
  103. }
  104. static int mtk_syst_clkevt_resume(struct clock_event_device *clkevt)
  105. {
  106. return mtk_syst_clkevt_shutdown(clkevt);
  107. }
  108. static int mtk_syst_clkevt_oneshot(struct clock_event_device *clkevt)
  109. {
  110. return 0;
  111. }
  112. static u64 notrace mtk_gpt_read_sched_clock(void)
  113. {
  114. return readl_relaxed(gpt_sched_reg);
  115. }
  116. static void mtk_gpt_clkevt_time_stop(struct timer_of *to, u8 timer)
  117. {
  118. u32 val;
  119. val = readl(timer_of_base(to) + GPT_CTRL_REG(timer));
  120. writel(val & ~GPT_CTRL_ENABLE, timer_of_base(to) +
  121. GPT_CTRL_REG(timer));
  122. }
  123. static void mtk_gpt_clkevt_time_setup(struct timer_of *to,
  124. unsigned long delay, u8 timer)
  125. {
  126. writel(delay, timer_of_base(to) + GPT_CMP_REG(timer));
  127. }
  128. static void mtk_gpt_clkevt_time_start(struct timer_of *to,
  129. bool periodic, u8 timer)
  130. {
  131. u32 val;
  132. /* Acknowledge interrupt */
  133. writel(GPT_IRQ_ACK(timer), timer_of_base(to) + GPT_IRQ_ACK_REG);
  134. val = readl(timer_of_base(to) + GPT_CTRL_REG(timer));
  135. /* Clear 2 bit timer operation mode field */
  136. val &= ~GPT_CTRL_OP(0x3);
  137. if (periodic)
  138. val |= GPT_CTRL_OP(GPT_CTRL_OP_REPEAT);
  139. else
  140. val |= GPT_CTRL_OP(GPT_CTRL_OP_ONESHOT);
  141. writel(val | GPT_CTRL_ENABLE | GPT_CTRL_CLEAR,
  142. timer_of_base(to) + GPT_CTRL_REG(timer));
  143. }
  144. static int mtk_gpt_clkevt_shutdown(struct clock_event_device *clk)
  145. {
  146. mtk_gpt_clkevt_time_stop(to_timer_of(clk), TIMER_CLK_EVT);
  147. return 0;
  148. }
  149. static int mtk_gpt_clkevt_set_periodic(struct clock_event_device *clk)
  150. {
  151. struct timer_of *to = to_timer_of(clk);
  152. mtk_gpt_clkevt_time_stop(to, TIMER_CLK_EVT);
  153. mtk_gpt_clkevt_time_setup(to, to->of_clk.period, TIMER_CLK_EVT);
  154. mtk_gpt_clkevt_time_start(to, true, TIMER_CLK_EVT);
  155. return 0;
  156. }
  157. static int mtk_gpt_clkevt_next_event(unsigned long event,
  158. struct clock_event_device *clk)
  159. {
  160. struct timer_of *to = to_timer_of(clk);
  161. mtk_gpt_clkevt_time_stop(to, TIMER_CLK_EVT);
  162. mtk_gpt_clkevt_time_setup(to, event, TIMER_CLK_EVT);
  163. mtk_gpt_clkevt_time_start(to, false, TIMER_CLK_EVT);
  164. return 0;
  165. }
  166. static irqreturn_t mtk_gpt_interrupt(int irq, void *dev_id)
  167. {
  168. struct clock_event_device *clkevt = (struct clock_event_device *)dev_id;
  169. struct timer_of *to = to_timer_of(clkevt);
  170. /* Acknowledge timer0 irq */
  171. writel(GPT_IRQ_ACK(TIMER_CLK_EVT), timer_of_base(to) + GPT_IRQ_ACK_REG);
  172. clkevt->event_handler(clkevt);
  173. return IRQ_HANDLED;
  174. }
  175. static void
  176. __init mtk_gpt_setup(struct timer_of *to, u8 timer, u8 option)
  177. {
  178. writel(GPT_CTRL_CLEAR | GPT_CTRL_DISABLE,
  179. timer_of_base(to) + GPT_CTRL_REG(timer));
  180. writel(GPT_CLK_SRC(GPT_CLK_SRC_SYS13M) | GPT_CLK_DIV1,
  181. timer_of_base(to) + GPT_CLK_REG(timer));
  182. writel(0x0, timer_of_base(to) + GPT_CMP_REG(timer));
  183. writel(GPT_CTRL_OP(option) | GPT_CTRL_ENABLE,
  184. timer_of_base(to) + GPT_CTRL_REG(timer));
  185. }
  186. static void mtk_gpt_enable_irq(struct timer_of *to, u8 timer)
  187. {
  188. u32 val;
  189. /* Disable all interrupts */
  190. writel(0x0, timer_of_base(to) + GPT_IRQ_EN_REG);
  191. /* Acknowledge all spurious pending interrupts */
  192. writel(0x3f, timer_of_base(to) + GPT_IRQ_ACK_REG);
  193. val = readl(timer_of_base(to) + GPT_IRQ_EN_REG);
  194. writel(val | GPT_IRQ_ENABLE(timer),
  195. timer_of_base(to) + GPT_IRQ_EN_REG);
  196. }
  197. static struct timer_of to = {
  198. .flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK,
  199. .clkevt = {
  200. .name = "mtk-clkevt",
  201. .rating = 300,
  202. .cpumask = cpu_possible_mask,
  203. },
  204. .of_irq = {
  205. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  206. },
  207. };
  208. static int __init mtk_syst_init(struct device_node *node)
  209. {
  210. int ret;
  211. to.clkevt.features = CLOCK_EVT_FEAT_DYNIRQ | CLOCK_EVT_FEAT_ONESHOT;
  212. to.clkevt.set_state_shutdown = mtk_syst_clkevt_shutdown;
  213. to.clkevt.set_state_oneshot = mtk_syst_clkevt_oneshot;
  214. to.clkevt.tick_resume = mtk_syst_clkevt_resume;
  215. to.clkevt.set_next_event = mtk_syst_clkevt_next_event;
  216. to.of_irq.handler = mtk_syst_handler;
  217. ret = timer_of_init(node, &to);
  218. if (ret)
  219. return ret;
  220. clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
  221. TIMER_SYNC_TICKS, 0xffffffff);
  222. return 0;
  223. }
  224. static int __init mtk_gpt_init(struct device_node *node)
  225. {
  226. int ret;
  227. to.clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  228. to.clkevt.set_state_shutdown = mtk_gpt_clkevt_shutdown;
  229. to.clkevt.set_state_periodic = mtk_gpt_clkevt_set_periodic;
  230. to.clkevt.set_state_oneshot = mtk_gpt_clkevt_shutdown;
  231. to.clkevt.tick_resume = mtk_gpt_clkevt_shutdown;
  232. to.clkevt.set_next_event = mtk_gpt_clkevt_next_event;
  233. to.of_irq.handler = mtk_gpt_interrupt;
  234. ret = timer_of_init(node, &to);
  235. if (ret)
  236. return ret;
  237. /* Configure clock source */
  238. mtk_gpt_setup(&to, TIMER_CLK_SRC, GPT_CTRL_OP_FREERUN);
  239. clocksource_mmio_init(timer_of_base(&to) + GPT_CNT_REG(TIMER_CLK_SRC),
  240. node->name, timer_of_rate(&to), 300, 32,
  241. clocksource_mmio_readl_up);
  242. gpt_sched_reg = timer_of_base(&to) + GPT_CNT_REG(TIMER_CLK_SRC);
  243. sched_clock_register(mtk_gpt_read_sched_clock, 32, timer_of_rate(&to));
  244. /* Configure clock event */
  245. mtk_gpt_setup(&to, TIMER_CLK_EVT, GPT_CTRL_OP_REPEAT);
  246. clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
  247. TIMER_SYNC_TICKS, 0xffffffff);
  248. mtk_gpt_enable_irq(&to, TIMER_CLK_EVT);
  249. return 0;
  250. }
  251. TIMER_OF_DECLARE(mtk_mt6577, "mediatek,mt6577-timer", mtk_gpt_init);
  252. TIMER_OF_DECLARE(mtk_mt6765, "mediatek,mt6765-timer", mtk_syst_init);