fsl_ftm_timer.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 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 void ftm_set_mode(enum clock_event_mode mode,
  130. struct clock_event_device *evt)
  131. {
  132. switch (mode) {
  133. case CLOCK_EVT_MODE_PERIODIC:
  134. ftm_set_next_event(priv->periodic_cyc, evt);
  135. break;
  136. case CLOCK_EVT_MODE_ONESHOT:
  137. ftm_counter_disable(priv->clkevt_base);
  138. break;
  139. default:
  140. return;
  141. }
  142. }
  143. static irqreturn_t ftm_evt_interrupt(int irq, void *dev_id)
  144. {
  145. struct clock_event_device *evt = dev_id;
  146. ftm_irq_acknowledge(priv->clkevt_base);
  147. if (likely(evt->mode == CLOCK_EVT_MODE_ONESHOT)) {
  148. ftm_irq_disable(priv->clkevt_base);
  149. ftm_counter_disable(priv->clkevt_base);
  150. }
  151. evt->event_handler(evt);
  152. return IRQ_HANDLED;
  153. }
  154. static struct clock_event_device ftm_clockevent = {
  155. .name = "Freescale ftm timer",
  156. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  157. .set_mode = ftm_set_mode,
  158. .set_next_event = ftm_set_next_event,
  159. .rating = 300,
  160. };
  161. static struct irqaction ftm_timer_irq = {
  162. .name = "Freescale ftm timer",
  163. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  164. .handler = ftm_evt_interrupt,
  165. .dev_id = &ftm_clockevent,
  166. };
  167. static int __init ftm_clockevent_init(unsigned long freq, int irq)
  168. {
  169. int err;
  170. ftm_writel(0x00, priv->clkevt_base + FTM_CNTIN);
  171. ftm_writel(~0UL, priv->clkevt_base + FTM_MOD);
  172. ftm_reset_counter(priv->clkevt_base);
  173. err = setup_irq(irq, &ftm_timer_irq);
  174. if (err) {
  175. pr_err("ftm: setup irq failed: %d\n", err);
  176. return err;
  177. }
  178. ftm_clockevent.cpumask = cpumask_of(0);
  179. ftm_clockevent.irq = irq;
  180. clockevents_config_and_register(&ftm_clockevent,
  181. freq / (1 << priv->ps),
  182. 1, 0xffff);
  183. ftm_counter_enable(priv->clkevt_base);
  184. return 0;
  185. }
  186. static int __init ftm_clocksource_init(unsigned long freq)
  187. {
  188. int err;
  189. ftm_writel(0x00, priv->clksrc_base + FTM_CNTIN);
  190. ftm_writel(~0UL, priv->clksrc_base + FTM_MOD);
  191. ftm_reset_counter(priv->clksrc_base);
  192. sched_clock_register(ftm_read_sched_clock, 16, freq / (1 << priv->ps));
  193. err = clocksource_mmio_init(priv->clksrc_base + FTM_CNT, "fsl-ftm",
  194. freq / (1 << priv->ps), 300, 16,
  195. clocksource_mmio_readl_up);
  196. if (err) {
  197. pr_err("ftm: init clock source mmio failed: %d\n", err);
  198. return err;
  199. }
  200. ftm_counter_enable(priv->clksrc_base);
  201. return 0;
  202. }
  203. static int __init __ftm_clk_init(struct device_node *np, char *cnt_name,
  204. char *ftm_name)
  205. {
  206. struct clk *clk;
  207. int err;
  208. clk = of_clk_get_by_name(np, cnt_name);
  209. if (IS_ERR(clk)) {
  210. pr_err("ftm: Cannot get \"%s\": %ld\n", cnt_name, PTR_ERR(clk));
  211. return PTR_ERR(clk);
  212. }
  213. err = clk_prepare_enable(clk);
  214. if (err) {
  215. pr_err("ftm: clock failed to prepare+enable \"%s\": %d\n",
  216. cnt_name, err);
  217. return err;
  218. }
  219. clk = of_clk_get_by_name(np, ftm_name);
  220. if (IS_ERR(clk)) {
  221. pr_err("ftm: Cannot get \"%s\": %ld\n", ftm_name, PTR_ERR(clk));
  222. return PTR_ERR(clk);
  223. }
  224. err = clk_prepare_enable(clk);
  225. if (err)
  226. pr_err("ftm: clock failed to prepare+enable \"%s\": %d\n",
  227. ftm_name, err);
  228. return clk_get_rate(clk);
  229. }
  230. static unsigned long __init ftm_clk_init(struct device_node *np)
  231. {
  232. unsigned long freq;
  233. freq = __ftm_clk_init(np, "ftm-evt-counter-en", "ftm-evt");
  234. if (freq <= 0)
  235. return 0;
  236. freq = __ftm_clk_init(np, "ftm-src-counter-en", "ftm-src");
  237. if (freq <= 0)
  238. return 0;
  239. return freq;
  240. }
  241. static int __init ftm_calc_closest_round_cyc(unsigned long freq)
  242. {
  243. priv->ps = 0;
  244. /* The counter register is only using the lower 16 bits, and
  245. * if the 'freq' value is to big here, then the periodic_cyc
  246. * may exceed 0xFFFF.
  247. */
  248. do {
  249. priv->periodic_cyc = DIV_ROUND_CLOSEST(freq,
  250. HZ * (1 << priv->ps++));
  251. } while (priv->periodic_cyc > 0xFFFF);
  252. if (priv->ps > FTM_PS_MAX) {
  253. pr_err("ftm: the prescaler is %lu > %d\n",
  254. priv->ps, FTM_PS_MAX);
  255. return -EINVAL;
  256. }
  257. return 0;
  258. }
  259. static void __init ftm_timer_init(struct device_node *np)
  260. {
  261. unsigned long freq;
  262. int irq;
  263. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  264. if (!priv)
  265. return;
  266. priv->clkevt_base = of_iomap(np, 0);
  267. if (!priv->clkevt_base) {
  268. pr_err("ftm: unable to map event timer registers\n");
  269. goto err;
  270. }
  271. priv->clksrc_base = of_iomap(np, 1);
  272. if (!priv->clksrc_base) {
  273. pr_err("ftm: unable to map source timer registers\n");
  274. goto err;
  275. }
  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. if (ftm_calc_closest_round_cyc(freq))
  286. goto err;
  287. if (ftm_clocksource_init(freq))
  288. goto err;
  289. if (ftm_clockevent_init(freq, irq))
  290. goto err;
  291. return;
  292. err:
  293. kfree(priv);
  294. }
  295. CLOCKSOURCE_OF_DECLARE(flextimer, "fsl,ftm-timer", ftm_timer_init);