timer-sun5i.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Allwinner SoCs hstimer driver.
  4. *
  5. * Copyright (C) 2013 Maxime Ripard
  6. *
  7. * Maxime Ripard <maxime.ripard@free-electrons.com>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/clockchips.h>
  11. #include <linux/clocksource.h>
  12. #include <linux/delay.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irq.h>
  15. #include <linux/irqreturn.h>
  16. #include <linux/reset.h>
  17. #include <linux/slab.h>
  18. #include <linux/platform_device.h>
  19. #define TIMER_IRQ_EN_REG 0x00
  20. #define TIMER_IRQ_EN(val) BIT(val)
  21. #define TIMER_IRQ_ST_REG 0x04
  22. #define TIMER_CTL_REG(val) (0x20 * (val) + 0x10)
  23. #define TIMER_CTL_ENABLE BIT(0)
  24. #define TIMER_CTL_RELOAD BIT(1)
  25. #define TIMER_CTL_CLK_PRES(val) (((val) & 0x7) << 4)
  26. #define TIMER_CTL_ONESHOT BIT(7)
  27. #define TIMER_INTVAL_LO_REG(val) (0x20 * (val) + 0x14)
  28. #define TIMER_INTVAL_HI_REG(val) (0x20 * (val) + 0x18)
  29. #define TIMER_CNTVAL_LO_REG(val) (0x20 * (val) + 0x1c)
  30. #define TIMER_CNTVAL_HI_REG(val) (0x20 * (val) + 0x20)
  31. #define TIMER_SYNC_TICKS 3
  32. struct sun5i_timer {
  33. void __iomem *base;
  34. struct clk *clk;
  35. struct notifier_block clk_rate_cb;
  36. u32 ticks_per_jiffy;
  37. struct clocksource clksrc;
  38. struct clock_event_device clkevt;
  39. };
  40. #define nb_to_sun5i_timer(x) \
  41. container_of(x, struct sun5i_timer, clk_rate_cb)
  42. #define clksrc_to_sun5i_timer(x) \
  43. container_of(x, struct sun5i_timer, clksrc)
  44. #define clkevt_to_sun5i_timer(x) \
  45. container_of(x, struct sun5i_timer, clkevt)
  46. /*
  47. * When we disable a timer, we need to wait at least for 2 cycles of
  48. * the timer source clock. We will use for that the clocksource timer
  49. * that is already setup and runs at the same frequency than the other
  50. * timers, and we never will be disabled.
  51. */
  52. static void sun5i_clkevt_sync(struct sun5i_timer *ce)
  53. {
  54. u32 old = readl(ce->base + TIMER_CNTVAL_LO_REG(1));
  55. while ((old - readl(ce->base + TIMER_CNTVAL_LO_REG(1))) < TIMER_SYNC_TICKS)
  56. cpu_relax();
  57. }
  58. static void sun5i_clkevt_time_stop(struct sun5i_timer *ce, u8 timer)
  59. {
  60. u32 val = readl(ce->base + TIMER_CTL_REG(timer));
  61. writel(val & ~TIMER_CTL_ENABLE, ce->base + TIMER_CTL_REG(timer));
  62. sun5i_clkevt_sync(ce);
  63. }
  64. static void sun5i_clkevt_time_setup(struct sun5i_timer *ce, u8 timer, u32 delay)
  65. {
  66. writel(delay, ce->base + TIMER_INTVAL_LO_REG(timer));
  67. }
  68. static void sun5i_clkevt_time_start(struct sun5i_timer *ce, u8 timer, bool periodic)
  69. {
  70. u32 val = readl(ce->base + TIMER_CTL_REG(timer));
  71. if (periodic)
  72. val &= ~TIMER_CTL_ONESHOT;
  73. else
  74. val |= TIMER_CTL_ONESHOT;
  75. writel(val | TIMER_CTL_ENABLE | TIMER_CTL_RELOAD,
  76. ce->base + TIMER_CTL_REG(timer));
  77. }
  78. static int sun5i_clkevt_shutdown(struct clock_event_device *clkevt)
  79. {
  80. struct sun5i_timer *ce = clkevt_to_sun5i_timer(clkevt);
  81. sun5i_clkevt_time_stop(ce, 0);
  82. return 0;
  83. }
  84. static int sun5i_clkevt_set_oneshot(struct clock_event_device *clkevt)
  85. {
  86. struct sun5i_timer *ce = clkevt_to_sun5i_timer(clkevt);
  87. sun5i_clkevt_time_stop(ce, 0);
  88. sun5i_clkevt_time_start(ce, 0, false);
  89. return 0;
  90. }
  91. static int sun5i_clkevt_set_periodic(struct clock_event_device *clkevt)
  92. {
  93. struct sun5i_timer *ce = clkevt_to_sun5i_timer(clkevt);
  94. sun5i_clkevt_time_stop(ce, 0);
  95. sun5i_clkevt_time_setup(ce, 0, ce->ticks_per_jiffy);
  96. sun5i_clkevt_time_start(ce, 0, true);
  97. return 0;
  98. }
  99. static int sun5i_clkevt_next_event(unsigned long evt,
  100. struct clock_event_device *clkevt)
  101. {
  102. struct sun5i_timer *ce = clkevt_to_sun5i_timer(clkevt);
  103. sun5i_clkevt_time_stop(ce, 0);
  104. sun5i_clkevt_time_setup(ce, 0, evt - TIMER_SYNC_TICKS);
  105. sun5i_clkevt_time_start(ce, 0, false);
  106. return 0;
  107. }
  108. static irqreturn_t sun5i_timer_interrupt(int irq, void *dev_id)
  109. {
  110. struct sun5i_timer *ce = dev_id;
  111. writel(0x1, ce->base + TIMER_IRQ_ST_REG);
  112. ce->clkevt.event_handler(&ce->clkevt);
  113. return IRQ_HANDLED;
  114. }
  115. static u64 sun5i_clksrc_read(struct clocksource *clksrc)
  116. {
  117. struct sun5i_timer *cs = clksrc_to_sun5i_timer(clksrc);
  118. return ~readl(cs->base + TIMER_CNTVAL_LO_REG(1));
  119. }
  120. static int sun5i_rate_cb(struct notifier_block *nb,
  121. unsigned long event, void *data)
  122. {
  123. struct clk_notifier_data *ndata = data;
  124. struct sun5i_timer *cs = nb_to_sun5i_timer(nb);
  125. switch (event) {
  126. case PRE_RATE_CHANGE:
  127. clocksource_unregister(&cs->clksrc);
  128. break;
  129. case POST_RATE_CHANGE:
  130. clocksource_register_hz(&cs->clksrc, ndata->new_rate);
  131. clockevents_update_freq(&cs->clkevt, ndata->new_rate);
  132. cs->ticks_per_jiffy = DIV_ROUND_UP(ndata->new_rate, HZ);
  133. break;
  134. default:
  135. break;
  136. }
  137. return NOTIFY_DONE;
  138. }
  139. static int sun5i_setup_clocksource(struct platform_device *pdev,
  140. unsigned long rate)
  141. {
  142. struct sun5i_timer *cs = platform_get_drvdata(pdev);
  143. void __iomem *base = cs->base;
  144. int ret;
  145. writel(~0, base + TIMER_INTVAL_LO_REG(1));
  146. writel(TIMER_CTL_ENABLE | TIMER_CTL_RELOAD,
  147. base + TIMER_CTL_REG(1));
  148. cs->clksrc.name = pdev->dev.of_node->name;
  149. cs->clksrc.rating = 340;
  150. cs->clksrc.read = sun5i_clksrc_read;
  151. cs->clksrc.mask = CLOCKSOURCE_MASK(32);
  152. cs->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  153. cs->clksrc.owner = THIS_MODULE;
  154. ret = clocksource_register_hz(&cs->clksrc, rate);
  155. if (ret) {
  156. dev_err(&pdev->dev, "Couldn't register clock source.\n");
  157. return ret;
  158. }
  159. return 0;
  160. }
  161. static int sun5i_setup_clockevent(struct platform_device *pdev,
  162. unsigned long rate, int irq)
  163. {
  164. struct device *dev = &pdev->dev;
  165. struct sun5i_timer *ce = platform_get_drvdata(pdev);
  166. void __iomem *base = ce->base;
  167. int ret;
  168. u32 val;
  169. ce->clkevt.name = dev->of_node->name;
  170. ce->clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  171. ce->clkevt.set_next_event = sun5i_clkevt_next_event;
  172. ce->clkevt.set_state_shutdown = sun5i_clkevt_shutdown;
  173. ce->clkevt.set_state_periodic = sun5i_clkevt_set_periodic;
  174. ce->clkevt.set_state_oneshot = sun5i_clkevt_set_oneshot;
  175. ce->clkevt.tick_resume = sun5i_clkevt_shutdown;
  176. ce->clkevt.rating = 340;
  177. ce->clkevt.irq = irq;
  178. ce->clkevt.cpumask = cpu_possible_mask;
  179. ce->clkevt.owner = THIS_MODULE;
  180. /* Enable timer0 interrupt */
  181. val = readl(base + TIMER_IRQ_EN_REG);
  182. writel(val | TIMER_IRQ_EN(0), base + TIMER_IRQ_EN_REG);
  183. clockevents_config_and_register(&ce->clkevt, rate,
  184. TIMER_SYNC_TICKS, 0xffffffff);
  185. ret = devm_request_irq(dev, irq, sun5i_timer_interrupt,
  186. IRQF_TIMER | IRQF_IRQPOLL,
  187. "sun5i_timer0", ce);
  188. if (ret) {
  189. dev_err(dev, "Unable to register interrupt\n");
  190. return ret;
  191. }
  192. return 0;
  193. }
  194. static int sun5i_timer_probe(struct platform_device *pdev)
  195. {
  196. struct device *dev = &pdev->dev;
  197. struct sun5i_timer *st;
  198. struct reset_control *rstc;
  199. void __iomem *timer_base;
  200. struct clk *clk;
  201. unsigned long rate;
  202. int irq, ret;
  203. st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
  204. if (!st)
  205. return -ENOMEM;
  206. platform_set_drvdata(pdev, st);
  207. timer_base = devm_platform_ioremap_resource(pdev, 0);
  208. if (IS_ERR(timer_base)) {
  209. dev_err(dev, "Can't map registers\n");
  210. return PTR_ERR(timer_base);
  211. }
  212. irq = platform_get_irq(pdev, 0);
  213. if (irq < 0)
  214. return irq;
  215. clk = devm_clk_get_enabled(dev, NULL);
  216. if (IS_ERR(clk)) {
  217. dev_err(dev, "Can't get timer clock\n");
  218. return PTR_ERR(clk);
  219. }
  220. rate = clk_get_rate(clk);
  221. if (!rate) {
  222. dev_err(dev, "Couldn't get parent clock rate\n");
  223. return -EINVAL;
  224. }
  225. st->base = timer_base;
  226. st->ticks_per_jiffy = DIV_ROUND_UP(rate, HZ);
  227. st->clk = clk;
  228. st->clk_rate_cb.notifier_call = sun5i_rate_cb;
  229. st->clk_rate_cb.next = NULL;
  230. ret = devm_clk_notifier_register(dev, clk, &st->clk_rate_cb);
  231. if (ret) {
  232. dev_err(dev, "Unable to register clock notifier.\n");
  233. return ret;
  234. }
  235. rstc = devm_reset_control_get_optional_exclusive(dev, NULL);
  236. if (rstc)
  237. reset_control_deassert(rstc);
  238. ret = sun5i_setup_clocksource(pdev, rate);
  239. if (ret)
  240. return ret;
  241. ret = sun5i_setup_clockevent(pdev, rate, irq);
  242. if (ret)
  243. goto err_unreg_clocksource;
  244. return 0;
  245. err_unreg_clocksource:
  246. clocksource_unregister(&st->clksrc);
  247. return ret;
  248. }
  249. static void sun5i_timer_remove(struct platform_device *pdev)
  250. {
  251. struct sun5i_timer *st = platform_get_drvdata(pdev);
  252. clocksource_unregister(&st->clksrc);
  253. }
  254. static const struct of_device_id sun5i_timer_of_match[] = {
  255. { .compatible = "allwinner,sun5i-a13-hstimer" },
  256. { .compatible = "allwinner,sun7i-a20-hstimer" },
  257. {},
  258. };
  259. MODULE_DEVICE_TABLE(of, sun5i_timer_of_match);
  260. static struct platform_driver sun5i_timer_driver = {
  261. .probe = sun5i_timer_probe,
  262. .remove = sun5i_timer_remove,
  263. .driver = {
  264. .name = "sun5i-timer",
  265. .of_match_table = sun5i_timer_of_match,
  266. .suppress_bind_attrs = true,
  267. },
  268. };
  269. module_platform_driver(sun5i_timer_driver);