mtk_timer.c 7.1 KB

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