timer-sun5i.c 8.8 KB

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