fsl_ftm_timer.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Freescale FlexTimer Module (FTM) timer driver.
  3. *
  4. * Copyright 2014 Freescale Semiconductor, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/clockchips.h>
  13. #include <linux/clocksource.h>
  14. #include <linux/err.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/sched_clock.h>
  20. #include <linux/slab.h>
  21. #define FTM_SC 0x00
  22. #define FTM_SC_CLK_SHIFT 3
  23. #define FTM_SC_CLK_MASK (0x3 << FTM_SC_CLK_SHIFT)
  24. #define FTM_SC_CLK(c) ((c) << FTM_SC_CLK_SHIFT)
  25. #define FTM_SC_PS_MASK 0x7
  26. #define FTM_SC_TOIE BIT(6)
  27. #define FTM_SC_TOF BIT(7)
  28. #define FTM_CNT 0x04
  29. #define FTM_MOD 0x08
  30. #define FTM_CNTIN 0x4C
  31. #define FTM_PS_MAX 7
  32. struct ftm_clock_device {
  33. void __iomem *clksrc_base;
  34. void __iomem *clkevt_base;
  35. unsigned long periodic_cyc;
  36. unsigned long ps;
  37. bool big_endian;
  38. };
  39. static struct ftm_clock_device *priv;
  40. static inline u32 ftm_readl(void __iomem *addr)
  41. {
  42. if (priv->big_endian)
  43. return ioread32be(addr);
  44. else
  45. return ioread32(addr);
  46. }
  47. static inline void ftm_writel(u32 val, void __iomem *addr)
  48. {
  49. if (priv->big_endian)
  50. iowrite32be(val, addr);
  51. else
  52. iowrite32(val, addr);
  53. }
  54. static inline void ftm_counter_enable(void __iomem *base)
  55. {
  56. u32 val;
  57. /* select and enable counter clock source */
  58. val = ftm_readl(base + FTM_SC);
  59. val &= ~(FTM_SC_PS_MASK | FTM_SC_CLK_MASK);
  60. val |= priv->ps | FTM_SC_CLK(1);
  61. ftm_writel(val, base + FTM_SC);
  62. }
  63. static inline void ftm_counter_disable(void __iomem *base)
  64. {
  65. u32 val;
  66. /* disable counter clock source */
  67. val = ftm_readl(base + FTM_SC);
  68. val &= ~(FTM_SC_PS_MASK | FTM_SC_CLK_MASK);
  69. ftm_writel(val, base + FTM_SC);
  70. }
  71. static inline void ftm_irq_acknowledge(void __iomem *base)
  72. {
  73. u32 val;
  74. val = ftm_readl(base + FTM_SC);
  75. val &= ~FTM_SC_TOF;
  76. ftm_writel(val, base + FTM_SC);
  77. }
  78. static inline void ftm_irq_enable(void __iomem *base)
  79. {
  80. u32 val;
  81. val = ftm_readl(base + FTM_SC);
  82. val |= FTM_SC_TOIE;
  83. ftm_writel(val, base + FTM_SC);
  84. }
  85. static inline void ftm_irq_disable(void __iomem *base)
  86. {
  87. u32 val;
  88. val = ftm_readl(base + FTM_SC);
  89. val &= ~FTM_SC_TOIE;
  90. ftm_writel(val, base + FTM_SC);
  91. }
  92. static inline void ftm_reset_counter(void __iomem *base)
  93. {
  94. /*
  95. * The CNT register contains the FTM counter value.
  96. * Reset clears the CNT register. Writing any value to COUNT
  97. * updates the counter with its initial value, CNTIN.
  98. */
  99. ftm_writel(0x00, base + FTM_CNT);
  100. }
  101. static u64 notrace ftm_read_sched_clock(void)
  102. {
  103. return ftm_readl(priv->clksrc_base + FTM_CNT);
  104. }
  105. static int ftm_set_next_event(unsigned long delta,
  106. struct clock_event_device *unused)
  107. {
  108. /*
  109. * The CNNIN and MOD are all double buffer registers, writing
  110. * to the MOD register latches the value into a buffer. The MOD
  111. * register is updated with the value of its write buffer with
  112. * the following scenario:
  113. * a, the counter source clock is diabled.
  114. */
  115. ftm_counter_disable(priv->clkevt_base);
  116. /* Force the value of CNTIN to be loaded into the FTM counter */
  117. ftm_reset_counter(priv->clkevt_base);
  118. /*
  119. * The counter increments until the value of MOD is reached,
  120. * at which point the counter is reloaded with the value of CNTIN.
  121. * The TOF (the overflow flag) bit is set when the FTM counter
  122. * changes from MOD to CNTIN. So we should using the delta - 1.
  123. */
  124. ftm_writel(delta - 1, priv->clkevt_base + FTM_MOD);
  125. ftm_counter_enable(priv->clkevt_base);
  126. ftm_irq_enable(priv->clkevt_base);
  127. return 0;
  128. }
  129. static int ftm_set_oneshot(struct clock_event_device *evt)
  130. {
  131. ftm_counter_disable(priv->clkevt_base);
  132. return 0;
  133. }
  134. static int ftm_set_periodic(struct clock_event_device *evt)
  135. {
  136. ftm_set_next_event(priv->periodic_cyc, evt);
  137. return 0;
  138. }
  139. static irqreturn_t ftm_evt_interrupt(int irq, void *dev_id)
  140. {
  141. struct clock_event_device *evt = dev_id;
  142. ftm_irq_acknowledge(priv->clkevt_base);
  143. if (likely(clockevent_state_oneshot(evt))) {
  144. ftm_irq_disable(priv->clkevt_base);
  145. ftm_counter_disable(priv->clkevt_base);
  146. }
  147. evt->event_handler(evt);
  148. return IRQ_HANDLED;
  149. }
  150. static struct clock_event_device ftm_clockevent = {
  151. .name = "Freescale ftm timer",
  152. .features = CLOCK_EVT_FEAT_PERIODIC |
  153. CLOCK_EVT_FEAT_ONESHOT,
  154. .set_state_periodic = ftm_set_periodic,
  155. .set_state_oneshot = ftm_set_oneshot,
  156. .set_next_event = ftm_set_next_event,
  157. .rating = 300,
  158. };
  159. static struct irqaction ftm_timer_irq = {
  160. .name = "Freescale ftm timer",
  161. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  162. .handler = ftm_evt_interrupt,
  163. .dev_id = &ftm_clockevent,
  164. };
  165. static int __init ftm_clockevent_init(unsigned long freq, int irq)
  166. {
  167. int err;
  168. ftm_writel(0x00, priv->clkevt_base + FTM_CNTIN);
  169. ftm_writel(~0u, priv->clkevt_base + FTM_MOD);
  170. ftm_reset_counter(priv->clkevt_base);
  171. err = setup_irq(irq, &ftm_timer_irq);
  172. if (err) {
  173. pr_err("ftm: setup irq failed: %d\n", err);
  174. return err;
  175. }
  176. ftm_clockevent.cpumask = cpumask_of(0);
  177. ftm_clockevent.irq = irq;
  178. clockevents_config_and_register(&ftm_clockevent,
  179. freq / (1 << priv->ps),
  180. 1, 0xffff);
  181. ftm_counter_enable(priv->clkevt_base);
  182. return 0;
  183. }
  184. static int __init ftm_clocksource_init(unsigned long freq)
  185. {
  186. int err;
  187. ftm_writel(0x00, priv->clksrc_base + FTM_CNTIN);
  188. ftm_writel(~0u, priv->clksrc_base + FTM_MOD);
  189. ftm_reset_counter(priv->clksrc_base);
  190. sched_clock_register(ftm_read_sched_clock, 16, freq / (1 << priv->ps));
  191. err = clocksource_mmio_init(priv->clksrc_base + FTM_CNT, "fsl-ftm",
  192. freq / (1 << priv->ps), 300, 16,
  193. clocksource_mmio_readl_up);
  194. if (err) {
  195. pr_err("ftm: init clock source mmio failed: %d\n", err);
  196. return err;
  197. }
  198. ftm_counter_enable(priv->clksrc_base);
  199. return 0;
  200. }
  201. static int __init __ftm_clk_init(struct device_node *np, char *cnt_name,
  202. char *ftm_name)
  203. {
  204. struct clk *clk;
  205. int err;
  206. clk = of_clk_get_by_name(np, cnt_name);
  207. if (IS_ERR(clk)) {
  208. pr_err("ftm: Cannot get \"%s\": %ld\n", cnt_name, PTR_ERR(clk));
  209. return PTR_ERR(clk);
  210. }
  211. err = clk_prepare_enable(clk);
  212. if (err) {
  213. pr_err("ftm: clock failed to prepare+enable \"%s\": %d\n",
  214. cnt_name, err);
  215. return err;
  216. }
  217. clk = of_clk_get_by_name(np, ftm_name);
  218. if (IS_ERR(clk)) {
  219. pr_err("ftm: Cannot get \"%s\": %ld\n", ftm_name, PTR_ERR(clk));
  220. return PTR_ERR(clk);
  221. }
  222. err = clk_prepare_enable(clk);
  223. if (err)
  224. pr_err("ftm: clock failed to prepare+enable \"%s\": %d\n",
  225. ftm_name, err);
  226. return clk_get_rate(clk);
  227. }
  228. static unsigned long __init ftm_clk_init(struct device_node *np)
  229. {
  230. long freq;
  231. freq = __ftm_clk_init(np, "ftm-evt-counter-en", "ftm-evt");
  232. if (freq <= 0)
  233. return 0;
  234. freq = __ftm_clk_init(np, "ftm-src-counter-en", "ftm-src");
  235. if (freq <= 0)
  236. return 0;
  237. return freq;
  238. }
  239. static int __init ftm_calc_closest_round_cyc(unsigned long freq)
  240. {
  241. priv->ps = 0;
  242. /* The counter register is only using the lower 16 bits, and
  243. * if the 'freq' value is to big here, then the periodic_cyc
  244. * may exceed 0xFFFF.
  245. */
  246. do {
  247. priv->periodic_cyc = DIV_ROUND_CLOSEST(freq,
  248. HZ * (1 << priv->ps++));
  249. } while (priv->periodic_cyc > 0xFFFF);
  250. if (priv->ps > FTM_PS_MAX) {
  251. pr_err("ftm: the prescaler is %lu > %d\n",
  252. priv->ps, FTM_PS_MAX);
  253. return -EINVAL;
  254. }
  255. return 0;
  256. }
  257. static int __init ftm_timer_init(struct device_node *np)
  258. {
  259. unsigned long freq;
  260. int ret, irq;
  261. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  262. if (!priv)
  263. return -ENOMEM;
  264. ret = -ENXIO;
  265. priv->clkevt_base = of_iomap(np, 0);
  266. if (!priv->clkevt_base) {
  267. pr_err("ftm: unable to map event timer registers\n");
  268. goto err;
  269. }
  270. priv->clksrc_base = of_iomap(np, 1);
  271. if (!priv->clksrc_base) {
  272. pr_err("ftm: unable to map source timer registers\n");
  273. goto err;
  274. }
  275. ret = -EINVAL;
  276. irq = irq_of_parse_and_map(np, 0);
  277. if (irq <= 0) {
  278. pr_err("ftm: unable to get IRQ from DT, %d\n", irq);
  279. goto err;
  280. }
  281. priv->big_endian = of_property_read_bool(np, "big-endian");
  282. freq = ftm_clk_init(np);
  283. if (!freq)
  284. goto err;
  285. ret = ftm_calc_closest_round_cyc(freq);
  286. if (ret)
  287. goto err;
  288. ret = ftm_clocksource_init(freq);
  289. if (ret)
  290. goto err;
  291. ret = ftm_clockevent_init(freq, irq);
  292. if (ret)
  293. goto err;
  294. return 0;
  295. err:
  296. kfree(priv);
  297. return ret;
  298. }
  299. CLOCKSOURCE_OF_DECLARE(flextimer, "fsl,ftm-timer", ftm_timer_init);