mtk_timer.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #include <linux/clk.h>
  19. #include <linux/clockchips.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/irqreturn.h>
  23. #include <linux/of.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/sched_clock.h>
  27. #include <linux/slab.h>
  28. #define GPT_IRQ_EN_REG 0x00
  29. #define GPT_IRQ_ENABLE(val) BIT((val) - 1)
  30. #define GPT_IRQ_ACK_REG 0x08
  31. #define GPT_IRQ_ACK(val) BIT((val) - 1)
  32. #define TIMER_CTRL_REG(val) (0x10 * (val))
  33. #define TIMER_CTRL_OP(val) (((val) & 0x3) << 4)
  34. #define TIMER_CTRL_OP_ONESHOT (0)
  35. #define TIMER_CTRL_OP_REPEAT (1)
  36. #define TIMER_CTRL_OP_FREERUN (3)
  37. #define TIMER_CTRL_CLEAR (2)
  38. #define TIMER_CTRL_ENABLE (1)
  39. #define TIMER_CTRL_DISABLE (0)
  40. #define TIMER_CLK_REG(val) (0x04 + (0x10 * (val)))
  41. #define TIMER_CLK_SRC(val) (((val) & 0x1) << 4)
  42. #define TIMER_CLK_SRC_SYS13M (0)
  43. #define TIMER_CLK_SRC_RTC32K (1)
  44. #define TIMER_CLK_DIV1 (0x0)
  45. #define TIMER_CLK_DIV2 (0x1)
  46. #define TIMER_CNT_REG(val) (0x08 + (0x10 * (val)))
  47. #define TIMER_CMP_REG(val) (0x0C + (0x10 * (val)))
  48. #define GPT_CLK_EVT 1
  49. #define GPT_CLK_SRC 2
  50. struct mtk_clock_event_device {
  51. void __iomem *gpt_base;
  52. u32 ticks_per_jiffy;
  53. struct clock_event_device dev;
  54. };
  55. static void __iomem *gpt_sched_reg __read_mostly;
  56. static u64 notrace mtk_read_sched_clock(void)
  57. {
  58. return readl_relaxed(gpt_sched_reg);
  59. }
  60. static inline struct mtk_clock_event_device *to_mtk_clk(
  61. struct clock_event_device *c)
  62. {
  63. return container_of(c, struct mtk_clock_event_device, dev);
  64. }
  65. static void mtk_clkevt_time_stop(struct mtk_clock_event_device *evt, u8 timer)
  66. {
  67. u32 val;
  68. val = readl(evt->gpt_base + TIMER_CTRL_REG(timer));
  69. writel(val & ~TIMER_CTRL_ENABLE, evt->gpt_base +
  70. TIMER_CTRL_REG(timer));
  71. }
  72. static void mtk_clkevt_time_setup(struct mtk_clock_event_device *evt,
  73. unsigned long delay, u8 timer)
  74. {
  75. writel(delay, evt->gpt_base + TIMER_CMP_REG(timer));
  76. }
  77. static void mtk_clkevt_time_start(struct mtk_clock_event_device *evt,
  78. bool periodic, u8 timer)
  79. {
  80. u32 val;
  81. /* Acknowledge interrupt */
  82. writel(GPT_IRQ_ACK(timer), evt->gpt_base + GPT_IRQ_ACK_REG);
  83. val = readl(evt->gpt_base + TIMER_CTRL_REG(timer));
  84. /* Clear 2 bit timer operation mode field */
  85. val &= ~TIMER_CTRL_OP(0x3);
  86. if (periodic)
  87. val |= TIMER_CTRL_OP(TIMER_CTRL_OP_REPEAT);
  88. else
  89. val |= TIMER_CTRL_OP(TIMER_CTRL_OP_ONESHOT);
  90. writel(val | TIMER_CTRL_ENABLE | TIMER_CTRL_CLEAR,
  91. evt->gpt_base + TIMER_CTRL_REG(timer));
  92. }
  93. static int mtk_clkevt_shutdown(struct clock_event_device *clk)
  94. {
  95. mtk_clkevt_time_stop(to_mtk_clk(clk), GPT_CLK_EVT);
  96. return 0;
  97. }
  98. static int mtk_clkevt_set_periodic(struct clock_event_device *clk)
  99. {
  100. struct mtk_clock_event_device *evt = to_mtk_clk(clk);
  101. mtk_clkevt_time_stop(evt, GPT_CLK_EVT);
  102. mtk_clkevt_time_setup(evt, evt->ticks_per_jiffy, GPT_CLK_EVT);
  103. mtk_clkevt_time_start(evt, true, GPT_CLK_EVT);
  104. return 0;
  105. }
  106. static int mtk_clkevt_next_event(unsigned long event,
  107. struct clock_event_device *clk)
  108. {
  109. struct mtk_clock_event_device *evt = to_mtk_clk(clk);
  110. mtk_clkevt_time_stop(evt, GPT_CLK_EVT);
  111. mtk_clkevt_time_setup(evt, event, GPT_CLK_EVT);
  112. mtk_clkevt_time_start(evt, false, GPT_CLK_EVT);
  113. return 0;
  114. }
  115. static irqreturn_t mtk_timer_interrupt(int irq, void *dev_id)
  116. {
  117. struct mtk_clock_event_device *evt = dev_id;
  118. /* Acknowledge timer0 irq */
  119. writel(GPT_IRQ_ACK(GPT_CLK_EVT), evt->gpt_base + GPT_IRQ_ACK_REG);
  120. evt->dev.event_handler(&evt->dev);
  121. return IRQ_HANDLED;
  122. }
  123. static void
  124. mtk_timer_setup(struct mtk_clock_event_device *evt, u8 timer, u8 option)
  125. {
  126. writel(TIMER_CTRL_CLEAR | TIMER_CTRL_DISABLE,
  127. evt->gpt_base + TIMER_CTRL_REG(timer));
  128. writel(TIMER_CLK_SRC(TIMER_CLK_SRC_SYS13M) | TIMER_CLK_DIV1,
  129. evt->gpt_base + TIMER_CLK_REG(timer));
  130. writel(0x0, evt->gpt_base + TIMER_CMP_REG(timer));
  131. writel(TIMER_CTRL_OP(option) | TIMER_CTRL_ENABLE,
  132. evt->gpt_base + TIMER_CTRL_REG(timer));
  133. }
  134. static void mtk_timer_enable_irq(struct mtk_clock_event_device *evt, u8 timer)
  135. {
  136. u32 val;
  137. /* Disable all interrupts */
  138. writel(0x0, evt->gpt_base + GPT_IRQ_EN_REG);
  139. /* Acknowledge all spurious pending interrupts */
  140. writel(0x3f, evt->gpt_base + GPT_IRQ_ACK_REG);
  141. val = readl(evt->gpt_base + GPT_IRQ_EN_REG);
  142. writel(val | GPT_IRQ_ENABLE(timer),
  143. evt->gpt_base + GPT_IRQ_EN_REG);
  144. }
  145. static void __init mtk_timer_init(struct device_node *node)
  146. {
  147. struct mtk_clock_event_device *evt;
  148. struct resource res;
  149. unsigned long rate = 0;
  150. struct clk *clk;
  151. evt = kzalloc(sizeof(*evt), GFP_KERNEL);
  152. if (!evt) {
  153. pr_warn("Can't allocate mtk clock event driver struct");
  154. return;
  155. }
  156. evt->dev.name = "mtk_tick";
  157. evt->dev.rating = 300;
  158. evt->dev.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  159. evt->dev.set_state_shutdown = mtk_clkevt_shutdown;
  160. evt->dev.set_state_periodic = mtk_clkevt_set_periodic;
  161. evt->dev.set_state_oneshot = mtk_clkevt_shutdown;
  162. evt->dev.tick_resume = mtk_clkevt_shutdown;
  163. evt->dev.set_next_event = mtk_clkevt_next_event;
  164. evt->dev.cpumask = cpu_possible_mask;
  165. evt->gpt_base = of_io_request_and_map(node, 0, "mtk-timer");
  166. if (IS_ERR(evt->gpt_base)) {
  167. pr_warn("Can't get resource\n");
  168. return;
  169. }
  170. evt->dev.irq = irq_of_parse_and_map(node, 0);
  171. if (evt->dev.irq <= 0) {
  172. pr_warn("Can't parse IRQ");
  173. goto err_mem;
  174. }
  175. clk = of_clk_get(node, 0);
  176. if (IS_ERR(clk)) {
  177. pr_warn("Can't get timer clock");
  178. goto err_irq;
  179. }
  180. if (clk_prepare_enable(clk)) {
  181. pr_warn("Can't prepare clock");
  182. goto err_clk_put;
  183. }
  184. rate = clk_get_rate(clk);
  185. if (request_irq(evt->dev.irq, mtk_timer_interrupt,
  186. IRQF_TIMER | IRQF_IRQPOLL, "mtk_timer", evt)) {
  187. pr_warn("failed to setup irq %d\n", evt->dev.irq);
  188. goto err_clk_disable;
  189. }
  190. evt->ticks_per_jiffy = DIV_ROUND_UP(rate, HZ);
  191. /* Configure clock source */
  192. mtk_timer_setup(evt, GPT_CLK_SRC, TIMER_CTRL_OP_FREERUN);
  193. clocksource_mmio_init(evt->gpt_base + TIMER_CNT_REG(GPT_CLK_SRC),
  194. node->name, rate, 300, 32, clocksource_mmio_readl_up);
  195. gpt_sched_reg = evt->gpt_base + TIMER_CNT_REG(GPT_CLK_SRC);
  196. sched_clock_register(mtk_read_sched_clock, 32, rate);
  197. /* Configure clock event */
  198. mtk_timer_setup(evt, GPT_CLK_EVT, TIMER_CTRL_OP_REPEAT);
  199. clockevents_config_and_register(&evt->dev, rate, 0x3,
  200. 0xffffffff);
  201. mtk_timer_enable_irq(evt, GPT_CLK_EVT);
  202. return;
  203. err_clk_disable:
  204. clk_disable_unprepare(clk);
  205. err_clk_put:
  206. clk_put(clk);
  207. err_irq:
  208. irq_dispose_mapping(evt->dev.irq);
  209. err_mem:
  210. iounmap(evt->gpt_base);
  211. of_address_to_resource(node, 0, &res);
  212. release_mem_region(res.start, resource_size(&res));
  213. }
  214. CLOCKSOURCE_OF_DECLARE(mtk_mt6577, "mediatek,mt6577-timer", mtk_timer_init);