timer-sun5i.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Allwinner SoCs hstimer driver.
  4. *
  5. * Copyright (C) 2013 Maxime Ripard
  6. *
  7. * Maxime Ripard <[email protected]>
  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/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #define TIMER_IRQ_EN_REG 0x00
  22. #define TIMER_IRQ_EN(val) BIT(val)
  23. #define TIMER_IRQ_ST_REG 0x04
  24. #define TIMER_CTL_REG(val) (0x20 * (val) + 0x10)
  25. #define TIMER_CTL_ENABLE BIT(0)
  26. #define TIMER_CTL_RELOAD BIT(1)
  27. #define TIMER_CTL_CLK_PRES(val) (((val) & 0x7) << 4)
  28. #define TIMER_CTL_ONESHOT BIT(7)
  29. #define TIMER_INTVAL_LO_REG(val) (0x20 * (val) + 0x14)
  30. #define TIMER_INTVAL_HI_REG(val) (0x20 * (val) + 0x18)
  31. #define TIMER_CNTVAL_LO_REG(val) (0x20 * (val) + 0x1c)
  32. #define TIMER_CNTVAL_HI_REG(val) (0x20 * (val) + 0x20)
  33. #define TIMER_SYNC_TICKS 3
  34. struct sun5i_timer {
  35. void __iomem *base;
  36. struct clk *clk;
  37. struct notifier_block clk_rate_cb;
  38. u32 ticks_per_jiffy;
  39. };
  40. #define to_sun5i_timer(x) \
  41. container_of(x, struct sun5i_timer, clk_rate_cb)
  42. struct sun5i_timer_clksrc {
  43. struct sun5i_timer timer;
  44. struct clocksource clksrc;
  45. };
  46. #define to_sun5i_timer_clksrc(x) \
  47. container_of(x, struct sun5i_timer_clksrc, clksrc)
  48. struct sun5i_timer_clkevt {
  49. struct sun5i_timer timer;
  50. struct clock_event_device clkevt;
  51. };
  52. #define to_sun5i_timer_clkevt(x) \
  53. container_of(x, struct sun5i_timer_clkevt, clkevt)
  54. /*
  55. * When we disable a timer, we need to wait at least for 2 cycles of
  56. * the timer source clock. We will use for that the clocksource timer
  57. * that is already setup and runs at the same frequency than the other
  58. * timers, and we never will be disabled.
  59. */
  60. static void sun5i_clkevt_sync(struct sun5i_timer_clkevt *ce)
  61. {
  62. u32 old = readl(ce->timer.base + TIMER_CNTVAL_LO_REG(1));
  63. while ((old - readl(ce->timer.base + TIMER_CNTVAL_LO_REG(1))) < TIMER_SYNC_TICKS)
  64. cpu_relax();
  65. }
  66. static void sun5i_clkevt_time_stop(struct sun5i_timer_clkevt *ce, u8 timer)
  67. {
  68. u32 val = readl(ce->timer.base + TIMER_CTL_REG(timer));
  69. writel(val & ~TIMER_CTL_ENABLE, ce->timer.base + TIMER_CTL_REG(timer));
  70. sun5i_clkevt_sync(ce);
  71. }
  72. static void sun5i_clkevt_time_setup(struct sun5i_timer_clkevt *ce, u8 timer, u32 delay)
  73. {
  74. writel(delay, ce->timer.base + TIMER_INTVAL_LO_REG(timer));
  75. }
  76. static void sun5i_clkevt_time_start(struct sun5i_timer_clkevt *ce, u8 timer, bool periodic)
  77. {
  78. u32 val = readl(ce->timer.base + TIMER_CTL_REG(timer));
  79. if (periodic)
  80. val &= ~TIMER_CTL_ONESHOT;
  81. else
  82. val |= TIMER_CTL_ONESHOT;
  83. writel(val | TIMER_CTL_ENABLE | TIMER_CTL_RELOAD,
  84. ce->timer.base + TIMER_CTL_REG(timer));
  85. }
  86. static int sun5i_clkevt_shutdown(struct clock_event_device *clkevt)
  87. {
  88. struct sun5i_timer_clkevt *ce = to_sun5i_timer_clkevt(clkevt);
  89. sun5i_clkevt_time_stop(ce, 0);
  90. return 0;
  91. }
  92. static int sun5i_clkevt_set_oneshot(struct clock_event_device *clkevt)
  93. {
  94. struct sun5i_timer_clkevt *ce = to_sun5i_timer_clkevt(clkevt);
  95. sun5i_clkevt_time_stop(ce, 0);
  96. sun5i_clkevt_time_start(ce, 0, false);
  97. return 0;
  98. }
  99. static int sun5i_clkevt_set_periodic(struct clock_event_device *clkevt)
  100. {
  101. struct sun5i_timer_clkevt *ce = to_sun5i_timer_clkevt(clkevt);
  102. sun5i_clkevt_time_stop(ce, 0);
  103. sun5i_clkevt_time_setup(ce, 0, ce->timer.ticks_per_jiffy);
  104. sun5i_clkevt_time_start(ce, 0, true);
  105. return 0;
  106. }
  107. static int sun5i_clkevt_next_event(unsigned long evt,
  108. struct clock_event_device *clkevt)
  109. {
  110. struct sun5i_timer_clkevt *ce = to_sun5i_timer_clkevt(clkevt);
  111. sun5i_clkevt_time_stop(ce, 0);
  112. sun5i_clkevt_time_setup(ce, 0, evt - TIMER_SYNC_TICKS);
  113. sun5i_clkevt_time_start(ce, 0, false);
  114. return 0;
  115. }
  116. static irqreturn_t sun5i_timer_interrupt(int irq, void *dev_id)
  117. {
  118. struct sun5i_timer_clkevt *ce = dev_id;
  119. writel(0x1, ce->timer.base + TIMER_IRQ_ST_REG);
  120. ce->clkevt.event_handler(&ce->clkevt);
  121. return IRQ_HANDLED;
  122. }
  123. static u64 sun5i_clksrc_read(struct clocksource *clksrc)
  124. {
  125. struct sun5i_timer_clksrc *cs = to_sun5i_timer_clksrc(clksrc);
  126. return ~readl(cs->timer.base + TIMER_CNTVAL_LO_REG(1));
  127. }
  128. static int sun5i_rate_cb_clksrc(struct notifier_block *nb,
  129. unsigned long event, void *data)
  130. {
  131. struct clk_notifier_data *ndata = data;
  132. struct sun5i_timer *timer = to_sun5i_timer(nb);
  133. struct sun5i_timer_clksrc *cs = container_of(timer, struct sun5i_timer_clksrc, timer);
  134. switch (event) {
  135. case PRE_RATE_CHANGE:
  136. clocksource_unregister(&cs->clksrc);
  137. break;
  138. case POST_RATE_CHANGE:
  139. clocksource_register_hz(&cs->clksrc, ndata->new_rate);
  140. break;
  141. default:
  142. break;
  143. }
  144. return NOTIFY_DONE;
  145. }
  146. static int __init sun5i_setup_clocksource(struct device_node *node,
  147. void __iomem *base,
  148. struct clk *clk, int irq)
  149. {
  150. struct sun5i_timer_clksrc *cs;
  151. unsigned long rate;
  152. int ret;
  153. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  154. if (!cs)
  155. return -ENOMEM;
  156. ret = clk_prepare_enable(clk);
  157. if (ret) {
  158. pr_err("Couldn't enable parent clock\n");
  159. goto err_free;
  160. }
  161. rate = clk_get_rate(clk);
  162. if (!rate) {
  163. pr_err("Couldn't get parent clock rate\n");
  164. ret = -EINVAL;
  165. goto err_disable_clk;
  166. }
  167. cs->timer.base = base;
  168. cs->timer.clk = clk;
  169. cs->timer.clk_rate_cb.notifier_call = sun5i_rate_cb_clksrc;
  170. cs->timer.clk_rate_cb.next = NULL;
  171. ret = clk_notifier_register(clk, &cs->timer.clk_rate_cb);
  172. if (ret) {
  173. pr_err("Unable to register clock notifier.\n");
  174. goto err_disable_clk;
  175. }
  176. writel(~0, base + TIMER_INTVAL_LO_REG(1));
  177. writel(TIMER_CTL_ENABLE | TIMER_CTL_RELOAD,
  178. base + TIMER_CTL_REG(1));
  179. cs->clksrc.name = node->name;
  180. cs->clksrc.rating = 340;
  181. cs->clksrc.read = sun5i_clksrc_read;
  182. cs->clksrc.mask = CLOCKSOURCE_MASK(32);
  183. cs->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  184. ret = clocksource_register_hz(&cs->clksrc, rate);
  185. if (ret) {
  186. pr_err("Couldn't register clock source.\n");
  187. goto err_remove_notifier;
  188. }
  189. return 0;
  190. err_remove_notifier:
  191. clk_notifier_unregister(clk, &cs->timer.clk_rate_cb);
  192. err_disable_clk:
  193. clk_disable_unprepare(clk);
  194. err_free:
  195. kfree(cs);
  196. return ret;
  197. }
  198. static int sun5i_rate_cb_clkevt(struct notifier_block *nb,
  199. unsigned long event, void *data)
  200. {
  201. struct clk_notifier_data *ndata = data;
  202. struct sun5i_timer *timer = to_sun5i_timer(nb);
  203. struct sun5i_timer_clkevt *ce = container_of(timer, struct sun5i_timer_clkevt, timer);
  204. if (event == POST_RATE_CHANGE) {
  205. clockevents_update_freq(&ce->clkevt, ndata->new_rate);
  206. ce->timer.ticks_per_jiffy = DIV_ROUND_UP(ndata->new_rate, HZ);
  207. }
  208. return NOTIFY_DONE;
  209. }
  210. static int __init sun5i_setup_clockevent(struct device_node *node, void __iomem *base,
  211. struct clk *clk, int irq)
  212. {
  213. struct sun5i_timer_clkevt *ce;
  214. unsigned long rate;
  215. int ret;
  216. u32 val;
  217. ce = kzalloc(sizeof(*ce), GFP_KERNEL);
  218. if (!ce)
  219. return -ENOMEM;
  220. ret = clk_prepare_enable(clk);
  221. if (ret) {
  222. pr_err("Couldn't enable parent clock\n");
  223. goto err_free;
  224. }
  225. rate = clk_get_rate(clk);
  226. if (!rate) {
  227. pr_err("Couldn't get parent clock rate\n");
  228. ret = -EINVAL;
  229. goto err_disable_clk;
  230. }
  231. ce->timer.base = base;
  232. ce->timer.ticks_per_jiffy = DIV_ROUND_UP(rate, HZ);
  233. ce->timer.clk = clk;
  234. ce->timer.clk_rate_cb.notifier_call = sun5i_rate_cb_clkevt;
  235. ce->timer.clk_rate_cb.next = NULL;
  236. ret = clk_notifier_register(clk, &ce->timer.clk_rate_cb);
  237. if (ret) {
  238. pr_err("Unable to register clock notifier.\n");
  239. goto err_disable_clk;
  240. }
  241. ce->clkevt.name = node->name;
  242. ce->clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  243. ce->clkevt.set_next_event = sun5i_clkevt_next_event;
  244. ce->clkevt.set_state_shutdown = sun5i_clkevt_shutdown;
  245. ce->clkevt.set_state_periodic = sun5i_clkevt_set_periodic;
  246. ce->clkevt.set_state_oneshot = sun5i_clkevt_set_oneshot;
  247. ce->clkevt.tick_resume = sun5i_clkevt_shutdown;
  248. ce->clkevt.rating = 340;
  249. ce->clkevt.irq = irq;
  250. ce->clkevt.cpumask = cpu_possible_mask;
  251. /* Enable timer0 interrupt */
  252. val = readl(base + TIMER_IRQ_EN_REG);
  253. writel(val | TIMER_IRQ_EN(0), base + TIMER_IRQ_EN_REG);
  254. clockevents_config_and_register(&ce->clkevt, rate,
  255. TIMER_SYNC_TICKS, 0xffffffff);
  256. ret = request_irq(irq, sun5i_timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL,
  257. "sun5i_timer0", ce);
  258. if (ret) {
  259. pr_err("Unable to register interrupt\n");
  260. goto err_remove_notifier;
  261. }
  262. return 0;
  263. err_remove_notifier:
  264. clk_notifier_unregister(clk, &ce->timer.clk_rate_cb);
  265. err_disable_clk:
  266. clk_disable_unprepare(clk);
  267. err_free:
  268. kfree(ce);
  269. return ret;
  270. }
  271. static int __init sun5i_timer_init(struct device_node *node)
  272. {
  273. struct reset_control *rstc;
  274. void __iomem *timer_base;
  275. struct clk *clk;
  276. int irq, ret;
  277. timer_base = of_io_request_and_map(node, 0, of_node_full_name(node));
  278. if (IS_ERR(timer_base)) {
  279. pr_err("Can't map registers\n");
  280. return PTR_ERR(timer_base);
  281. }
  282. irq = irq_of_parse_and_map(node, 0);
  283. if (irq <= 0) {
  284. pr_err("Can't parse IRQ\n");
  285. return -EINVAL;
  286. }
  287. clk = of_clk_get(node, 0);
  288. if (IS_ERR(clk)) {
  289. pr_err("Can't get timer clock\n");
  290. return PTR_ERR(clk);
  291. }
  292. rstc = of_reset_control_get(node, NULL);
  293. if (!IS_ERR(rstc))
  294. reset_control_deassert(rstc);
  295. ret = sun5i_setup_clocksource(node, timer_base, clk, irq);
  296. if (ret)
  297. return ret;
  298. return sun5i_setup_clockevent(node, timer_base, clk, irq);
  299. }
  300. TIMER_OF_DECLARE(sun5i_a13, "allwinner,sun5i-a13-hstimer",
  301. sun5i_timer_init);
  302. TIMER_OF_DECLARE(sun7i_a20, "allwinner,sun7i-a20-hstimer",
  303. sun5i_timer_init);