timer-sun5i.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 cycle_t sun5i_clksrc_read(struct clocksource *clksrc)
  126. {
  127. struct sun5i_timer_clksrc *cs = to_sun5i_timer_clksrc(clksrc);
  128. return ~readl(cs->timer.base + TIMER_CNTVAL_LO_REG(1));
  129. }
  130. static int sun5i_rate_cb_clksrc(struct notifier_block *nb,
  131. unsigned long event, void *data)
  132. {
  133. struct clk_notifier_data *ndata = data;
  134. struct sun5i_timer *timer = to_sun5i_timer(nb);
  135. struct sun5i_timer_clksrc *cs = container_of(timer, struct sun5i_timer_clksrc, timer);
  136. switch (event) {
  137. case PRE_RATE_CHANGE:
  138. clocksource_unregister(&cs->clksrc);
  139. break;
  140. case POST_RATE_CHANGE:
  141. clocksource_register_hz(&cs->clksrc, ndata->new_rate);
  142. break;
  143. default:
  144. break;
  145. }
  146. return NOTIFY_DONE;
  147. }
  148. static int __init sun5i_setup_clocksource(struct device_node *node,
  149. void __iomem *base,
  150. struct clk *clk, int irq)
  151. {
  152. struct sun5i_timer_clksrc *cs;
  153. unsigned long rate;
  154. int ret;
  155. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  156. if (!cs)
  157. return -ENOMEM;
  158. ret = clk_prepare_enable(clk);
  159. if (ret) {
  160. pr_err("Couldn't enable parent clock\n");
  161. goto err_free;
  162. }
  163. rate = clk_get_rate(clk);
  164. cs->timer.base = base;
  165. cs->timer.clk = clk;
  166. cs->timer.clk_rate_cb.notifier_call = sun5i_rate_cb_clksrc;
  167. cs->timer.clk_rate_cb.next = NULL;
  168. ret = clk_notifier_register(clk, &cs->timer.clk_rate_cb);
  169. if (ret) {
  170. pr_err("Unable to register clock notifier.\n");
  171. goto err_disable_clk;
  172. }
  173. writel(~0, base + TIMER_INTVAL_LO_REG(1));
  174. writel(TIMER_CTL_ENABLE | TIMER_CTL_RELOAD,
  175. base + TIMER_CTL_REG(1));
  176. cs->clksrc.name = node->name;
  177. cs->clksrc.rating = 340;
  178. cs->clksrc.read = sun5i_clksrc_read;
  179. cs->clksrc.mask = CLOCKSOURCE_MASK(32);
  180. cs->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  181. ret = clocksource_register_hz(&cs->clksrc, rate);
  182. if (ret) {
  183. pr_err("Couldn't register clock source.\n");
  184. goto err_remove_notifier;
  185. }
  186. return 0;
  187. err_remove_notifier:
  188. clk_notifier_unregister(clk, &cs->timer.clk_rate_cb);
  189. err_disable_clk:
  190. clk_disable_unprepare(clk);
  191. err_free:
  192. kfree(cs);
  193. return ret;
  194. }
  195. static int sun5i_rate_cb_clkevt(struct notifier_block *nb,
  196. unsigned long event, void *data)
  197. {
  198. struct clk_notifier_data *ndata = data;
  199. struct sun5i_timer *timer = to_sun5i_timer(nb);
  200. struct sun5i_timer_clkevt *ce = container_of(timer, struct sun5i_timer_clkevt, timer);
  201. if (event == POST_RATE_CHANGE) {
  202. clockevents_update_freq(&ce->clkevt, ndata->new_rate);
  203. ce->timer.ticks_per_jiffy = DIV_ROUND_UP(ndata->new_rate, HZ);
  204. }
  205. return NOTIFY_DONE;
  206. }
  207. static int __init sun5i_setup_clockevent(struct device_node *node, void __iomem *base,
  208. struct clk *clk, int irq)
  209. {
  210. struct sun5i_timer_clkevt *ce;
  211. unsigned long rate;
  212. int ret;
  213. u32 val;
  214. ce = kzalloc(sizeof(*ce), GFP_KERNEL);
  215. if (!ce)
  216. return -ENOMEM;
  217. ret = clk_prepare_enable(clk);
  218. if (ret) {
  219. pr_err("Couldn't enable parent clock\n");
  220. goto err_free;
  221. }
  222. rate = clk_get_rate(clk);
  223. ce->timer.base = base;
  224. ce->timer.ticks_per_jiffy = DIV_ROUND_UP(rate, HZ);
  225. ce->timer.clk = clk;
  226. ce->timer.clk_rate_cb.notifier_call = sun5i_rate_cb_clkevt;
  227. ce->timer.clk_rate_cb.next = NULL;
  228. ret = clk_notifier_register(clk, &ce->timer.clk_rate_cb);
  229. if (ret) {
  230. pr_err("Unable to register clock notifier.\n");
  231. goto err_disable_clk;
  232. }
  233. ce->clkevt.name = node->name;
  234. ce->clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  235. ce->clkevt.set_next_event = sun5i_clkevt_next_event;
  236. ce->clkevt.set_state_shutdown = sun5i_clkevt_shutdown;
  237. ce->clkevt.set_state_periodic = sun5i_clkevt_set_periodic;
  238. ce->clkevt.set_state_oneshot = sun5i_clkevt_set_oneshot;
  239. ce->clkevt.tick_resume = sun5i_clkevt_shutdown;
  240. ce->clkevt.rating = 340;
  241. ce->clkevt.irq = irq;
  242. ce->clkevt.cpumask = cpu_possible_mask;
  243. /* Enable timer0 interrupt */
  244. val = readl(base + TIMER_IRQ_EN_REG);
  245. writel(val | TIMER_IRQ_EN(0), base + TIMER_IRQ_EN_REG);
  246. clockevents_config_and_register(&ce->clkevt, rate,
  247. TIMER_SYNC_TICKS, 0xffffffff);
  248. ret = request_irq(irq, sun5i_timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL,
  249. "sun5i_timer0", ce);
  250. if (ret) {
  251. pr_err("Unable to register interrupt\n");
  252. goto err_remove_notifier;
  253. }
  254. return 0;
  255. err_remove_notifier:
  256. clk_notifier_unregister(clk, &ce->timer.clk_rate_cb);
  257. err_disable_clk:
  258. clk_disable_unprepare(clk);
  259. err_free:
  260. kfree(ce);
  261. return ret;
  262. }
  263. static int __init sun5i_timer_init(struct device_node *node)
  264. {
  265. struct reset_control *rstc;
  266. void __iomem *timer_base;
  267. struct clk *clk;
  268. int irq, ret;
  269. timer_base = of_io_request_and_map(node, 0, of_node_full_name(node));
  270. if (IS_ERR(timer_base)) {
  271. pr_err("Can't map registers");
  272. return PTR_ERR(timer_base);;
  273. }
  274. irq = irq_of_parse_and_map(node, 0);
  275. if (irq <= 0) {
  276. pr_err("Can't parse IRQ");
  277. return -EINVAL;
  278. }
  279. clk = of_clk_get(node, 0);
  280. if (IS_ERR(clk)) {
  281. pr_err("Can't get timer clock");
  282. return PTR_ERR(clk);
  283. }
  284. rstc = of_reset_control_get(node, NULL);
  285. if (!IS_ERR(rstc))
  286. reset_control_deassert(rstc);
  287. ret = sun5i_setup_clocksource(node, timer_base, clk, irq);
  288. if (ret)
  289. return ret;
  290. return sun5i_setup_clockevent(node, timer_base, clk, irq);
  291. }
  292. CLOCKSOURCE_OF_DECLARE(sun5i_a13, "allwinner,sun5i-a13-hstimer",
  293. sun5i_timer_init);
  294. CLOCKSOURCE_OF_DECLARE(sun7i_a20, "allwinner,sun7i-a20-hstimer",
  295. sun5i_timer_init);