timer-sun5i.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. ret = clocksource_register_hz(&cs->clksrc, rate);
  154. if (ret) {
  155. dev_err(&pdev->dev, "Couldn't register clock source.\n");
  156. return ret;
  157. }
  158. return 0;
  159. }
  160. static int sun5i_setup_clockevent(struct platform_device *pdev,
  161. unsigned long rate, int irq)
  162. {
  163. struct device *dev = &pdev->dev;
  164. struct sun5i_timer *ce = platform_get_drvdata(pdev);
  165. void __iomem *base = ce->base;
  166. int ret;
  167. u32 val;
  168. ce->clkevt.name = dev->of_node->name;
  169. ce->clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  170. ce->clkevt.set_next_event = sun5i_clkevt_next_event;
  171. ce->clkevt.set_state_shutdown = sun5i_clkevt_shutdown;
  172. ce->clkevt.set_state_periodic = sun5i_clkevt_set_periodic;
  173. ce->clkevt.set_state_oneshot = sun5i_clkevt_set_oneshot;
  174. ce->clkevt.tick_resume = sun5i_clkevt_shutdown;
  175. ce->clkevt.rating = 340;
  176. ce->clkevt.irq = irq;
  177. ce->clkevt.cpumask = cpu_possible_mask;
  178. /* Enable timer0 interrupt */
  179. val = readl(base + TIMER_IRQ_EN_REG);
  180. writel(val | TIMER_IRQ_EN(0), base + TIMER_IRQ_EN_REG);
  181. clockevents_config_and_register(&ce->clkevt, rate,
  182. TIMER_SYNC_TICKS, 0xffffffff);
  183. ret = devm_request_irq(dev, irq, sun5i_timer_interrupt,
  184. IRQF_TIMER | IRQF_IRQPOLL,
  185. "sun5i_timer0", ce);
  186. if (ret) {
  187. dev_err(dev, "Unable to register interrupt\n");
  188. return ret;
  189. }
  190. return 0;
  191. }
  192. static int sun5i_timer_probe(struct platform_device *pdev)
  193. {
  194. struct device *dev = &pdev->dev;
  195. struct sun5i_timer *st;
  196. struct reset_control *rstc;
  197. void __iomem *timer_base;
  198. struct clk *clk;
  199. unsigned long rate;
  200. int irq, ret;
  201. st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
  202. if (!st)
  203. return -ENOMEM;
  204. platform_set_drvdata(pdev, st);
  205. timer_base = devm_platform_ioremap_resource(pdev, 0);
  206. if (IS_ERR(timer_base)) {
  207. dev_err(dev, "Can't map registers\n");
  208. return PTR_ERR(timer_base);
  209. }
  210. irq = platform_get_irq(pdev, 0);
  211. if (irq < 0)
  212. return irq;
  213. clk = devm_clk_get_enabled(dev, NULL);
  214. if (IS_ERR(clk)) {
  215. dev_err(dev, "Can't get timer clock\n");
  216. return PTR_ERR(clk);
  217. }
  218. rate = clk_get_rate(clk);
  219. if (!rate) {
  220. dev_err(dev, "Couldn't get parent clock rate\n");
  221. return -EINVAL;
  222. }
  223. st->base = timer_base;
  224. st->ticks_per_jiffy = DIV_ROUND_UP(rate, HZ);
  225. st->clk = clk;
  226. st->clk_rate_cb.notifier_call = sun5i_rate_cb;
  227. st->clk_rate_cb.next = NULL;
  228. ret = devm_clk_notifier_register(dev, clk, &st->clk_rate_cb);
  229. if (ret) {
  230. dev_err(dev, "Unable to register clock notifier.\n");
  231. return ret;
  232. }
  233. rstc = devm_reset_control_get_optional_exclusive(dev, NULL);
  234. if (rstc)
  235. reset_control_deassert(rstc);
  236. ret = sun5i_setup_clocksource(pdev, rate);
  237. if (ret)
  238. return ret;
  239. ret = sun5i_setup_clockevent(pdev, rate, irq);
  240. if (ret)
  241. goto err_unreg_clocksource;
  242. return 0;
  243. err_unreg_clocksource:
  244. clocksource_unregister(&st->clksrc);
  245. return ret;
  246. }
  247. static void sun5i_timer_remove(struct platform_device *pdev)
  248. {
  249. struct sun5i_timer *st = platform_get_drvdata(pdev);
  250. clocksource_unregister(&st->clksrc);
  251. }
  252. static const struct of_device_id sun5i_timer_of_match[] = {
  253. { .compatible = "allwinner,sun5i-a13-hstimer" },
  254. { .compatible = "allwinner,sun7i-a20-hstimer" },
  255. {},
  256. };
  257. MODULE_DEVICE_TABLE(of, sun5i_timer_of_match);
  258. static struct platform_driver sun5i_timer_driver = {
  259. .probe = sun5i_timer_probe,
  260. .remove_new = sun5i_timer_remove,
  261. .driver = {
  262. .name = "sun5i-timer",
  263. .of_match_table = sun5i_timer_of_match,
  264. .suppress_bind_attrs = true,
  265. },
  266. };
  267. module_platform_driver(sun5i_timer_driver);