fsl_gtm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Freescale General-purpose Timers Module
  4. *
  5. * Copyright (c) Freescale Semiconductor, Inc. 2006.
  6. * Shlomi Gridish <[email protected]>
  7. * Jerry Huang <[email protected]>
  8. * Copyright (c) MontaVista Software, Inc. 2008.
  9. * Anton Vorontsov <[email protected]>
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/err.h>
  13. #include <linux/errno.h>
  14. #include <linux/list.h>
  15. #include <linux/io.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/bitops.h>
  21. #include <linux/slab.h>
  22. #include <linux/export.h>
  23. #include <asm/fsl_gtm.h>
  24. #define GTCFR_STP(x) ((x) & 1 ? 1 << 5 : 1 << 1)
  25. #define GTCFR_RST(x) ((x) & 1 ? 1 << 4 : 1 << 0)
  26. #define GTMDR_ICLK_MASK (3 << 1)
  27. #define GTMDR_ICLK_ICAS (0 << 1)
  28. #define GTMDR_ICLK_ICLK (1 << 1)
  29. #define GTMDR_ICLK_SLGO (2 << 1)
  30. #define GTMDR_FRR (1 << 3)
  31. #define GTMDR_ORI (1 << 4)
  32. #define GTMDR_SPS(x) ((x) << 8)
  33. struct gtm_timers_regs {
  34. u8 gtcfr1; /* Timer 1, Timer 2 global config register */
  35. u8 res0[0x3];
  36. u8 gtcfr2; /* Timer 3, timer 4 global config register */
  37. u8 res1[0xB];
  38. __be16 gtmdr1; /* Timer 1 mode register */
  39. __be16 gtmdr2; /* Timer 2 mode register */
  40. __be16 gtrfr1; /* Timer 1 reference register */
  41. __be16 gtrfr2; /* Timer 2 reference register */
  42. __be16 gtcpr1; /* Timer 1 capture register */
  43. __be16 gtcpr2; /* Timer 2 capture register */
  44. __be16 gtcnr1; /* Timer 1 counter */
  45. __be16 gtcnr2; /* Timer 2 counter */
  46. __be16 gtmdr3; /* Timer 3 mode register */
  47. __be16 gtmdr4; /* Timer 4 mode register */
  48. __be16 gtrfr3; /* Timer 3 reference register */
  49. __be16 gtrfr4; /* Timer 4 reference register */
  50. __be16 gtcpr3; /* Timer 3 capture register */
  51. __be16 gtcpr4; /* Timer 4 capture register */
  52. __be16 gtcnr3; /* Timer 3 counter */
  53. __be16 gtcnr4; /* Timer 4 counter */
  54. __be16 gtevr1; /* Timer 1 event register */
  55. __be16 gtevr2; /* Timer 2 event register */
  56. __be16 gtevr3; /* Timer 3 event register */
  57. __be16 gtevr4; /* Timer 4 event register */
  58. __be16 gtpsr1; /* Timer 1 prescale register */
  59. __be16 gtpsr2; /* Timer 2 prescale register */
  60. __be16 gtpsr3; /* Timer 3 prescale register */
  61. __be16 gtpsr4; /* Timer 4 prescale register */
  62. u8 res2[0x40];
  63. } __attribute__ ((packed));
  64. struct gtm {
  65. unsigned int clock;
  66. struct gtm_timers_regs __iomem *regs;
  67. struct gtm_timer timers[4];
  68. spinlock_t lock;
  69. struct list_head list_node;
  70. };
  71. static LIST_HEAD(gtms);
  72. /**
  73. * gtm_get_timer - request GTM timer to use it with the rest of GTM API
  74. * Context: non-IRQ
  75. *
  76. * This function reserves GTM timer for later use. It returns gtm_timer
  77. * structure to use with the rest of GTM API, you should use timer->irq
  78. * to manage timer interrupt.
  79. */
  80. struct gtm_timer *gtm_get_timer16(void)
  81. {
  82. struct gtm *gtm;
  83. int i;
  84. list_for_each_entry(gtm, &gtms, list_node) {
  85. spin_lock_irq(&gtm->lock);
  86. for (i = 0; i < ARRAY_SIZE(gtm->timers); i++) {
  87. if (!gtm->timers[i].requested) {
  88. gtm->timers[i].requested = true;
  89. spin_unlock_irq(&gtm->lock);
  90. return &gtm->timers[i];
  91. }
  92. }
  93. spin_unlock_irq(&gtm->lock);
  94. }
  95. if (!list_empty(&gtms))
  96. return ERR_PTR(-EBUSY);
  97. return ERR_PTR(-ENODEV);
  98. }
  99. EXPORT_SYMBOL(gtm_get_timer16);
  100. /**
  101. * gtm_get_specific_timer - request specific GTM timer
  102. * @gtm: specific GTM, pass here GTM's device_node->data
  103. * @timer: specific timer number, Timer1 is 0.
  104. * Context: non-IRQ
  105. *
  106. * This function reserves GTM timer for later use. It returns gtm_timer
  107. * structure to use with the rest of GTM API, you should use timer->irq
  108. * to manage timer interrupt.
  109. */
  110. struct gtm_timer *gtm_get_specific_timer16(struct gtm *gtm,
  111. unsigned int timer)
  112. {
  113. struct gtm_timer *ret = ERR_PTR(-EBUSY);
  114. if (timer > 3)
  115. return ERR_PTR(-EINVAL);
  116. spin_lock_irq(&gtm->lock);
  117. if (gtm->timers[timer].requested)
  118. goto out;
  119. ret = &gtm->timers[timer];
  120. ret->requested = true;
  121. out:
  122. spin_unlock_irq(&gtm->lock);
  123. return ret;
  124. }
  125. EXPORT_SYMBOL(gtm_get_specific_timer16);
  126. /**
  127. * gtm_put_timer16 - release 16 bits GTM timer
  128. * @tmr: pointer to the gtm_timer structure obtained from gtm_get_timer
  129. * Context: any
  130. *
  131. * This function releases GTM timer so others may request it.
  132. */
  133. void gtm_put_timer16(struct gtm_timer *tmr)
  134. {
  135. gtm_stop_timer16(tmr);
  136. spin_lock_irq(&tmr->gtm->lock);
  137. tmr->requested = false;
  138. spin_unlock_irq(&tmr->gtm->lock);
  139. }
  140. EXPORT_SYMBOL(gtm_put_timer16);
  141. /*
  142. * This is back-end for the exported functions, it's used to reset single
  143. * timer in reference mode.
  144. */
  145. static int gtm_set_ref_timer16(struct gtm_timer *tmr, int frequency,
  146. int reference_value, bool free_run)
  147. {
  148. struct gtm *gtm = tmr->gtm;
  149. int num = tmr - &gtm->timers[0];
  150. unsigned int prescaler;
  151. u8 iclk = GTMDR_ICLK_ICLK;
  152. u8 psr;
  153. u8 sps;
  154. unsigned long flags;
  155. int max_prescaler = 256 * 256 * 16;
  156. /* CPM2 doesn't have primary prescaler */
  157. if (!tmr->gtpsr)
  158. max_prescaler /= 256;
  159. prescaler = gtm->clock / frequency;
  160. /*
  161. * We have two 8 bit prescalers -- primary and secondary (psr, sps),
  162. * plus "slow go" mode (clk / 16). So, total prescale value is
  163. * 16 * (psr + 1) * (sps + 1). Though, for CPM2 GTMs we losing psr.
  164. */
  165. if (prescaler > max_prescaler)
  166. return -EINVAL;
  167. if (prescaler > max_prescaler / 16) {
  168. iclk = GTMDR_ICLK_SLGO;
  169. prescaler /= 16;
  170. }
  171. if (prescaler <= 256) {
  172. psr = 0;
  173. sps = prescaler - 1;
  174. } else {
  175. psr = 256 - 1;
  176. sps = prescaler / 256 - 1;
  177. }
  178. spin_lock_irqsave(&gtm->lock, flags);
  179. /*
  180. * Properly reset timers: stop, reset, set up prescalers, reference
  181. * value and clear event register.
  182. */
  183. clrsetbits_8(tmr->gtcfr, ~(GTCFR_STP(num) | GTCFR_RST(num)),
  184. GTCFR_STP(num) | GTCFR_RST(num));
  185. setbits8(tmr->gtcfr, GTCFR_STP(num));
  186. if (tmr->gtpsr)
  187. out_be16(tmr->gtpsr, psr);
  188. clrsetbits_be16(tmr->gtmdr, 0xFFFF, iclk | GTMDR_SPS(sps) |
  189. GTMDR_ORI | (free_run ? GTMDR_FRR : 0));
  190. out_be16(tmr->gtcnr, 0);
  191. out_be16(tmr->gtrfr, reference_value);
  192. out_be16(tmr->gtevr, 0xFFFF);
  193. /* Let it be. */
  194. clrbits8(tmr->gtcfr, GTCFR_STP(num));
  195. spin_unlock_irqrestore(&gtm->lock, flags);
  196. return 0;
  197. }
  198. /**
  199. * gtm_set_timer16 - (re)set 16 bit timer with arbitrary precision
  200. * @tmr: pointer to the gtm_timer structure obtained from gtm_get_timer
  201. * @usec: timer interval in microseconds
  202. * @reload: if set, the timer will reset upon expiry rather than
  203. * continue running free.
  204. * Context: any
  205. *
  206. * This function (re)sets the GTM timer so that it counts up to the requested
  207. * interval value, and fires the interrupt when the value is reached. This
  208. * function will reduce the precision of the timer as needed in order for the
  209. * requested timeout to fit in a 16-bit register.
  210. */
  211. int gtm_set_timer16(struct gtm_timer *tmr, unsigned long usec, bool reload)
  212. {
  213. /* quite obvious, frequency which is enough for µSec precision */
  214. int freq = 1000000;
  215. unsigned int bit;
  216. bit = fls_long(usec);
  217. if (bit > 15) {
  218. freq >>= bit - 15;
  219. usec >>= bit - 15;
  220. }
  221. if (!freq)
  222. return -EINVAL;
  223. return gtm_set_ref_timer16(tmr, freq, usec, reload);
  224. }
  225. EXPORT_SYMBOL(gtm_set_timer16);
  226. /**
  227. * gtm_set_exact_utimer16 - (re)set 16 bits timer
  228. * @tmr: pointer to the gtm_timer structure obtained from gtm_get_timer
  229. * @usec: timer interval in microseconds
  230. * @reload: if set, the timer will reset upon expiry rather than
  231. * continue running free.
  232. * Context: any
  233. *
  234. * This function (re)sets GTM timer so that it counts up to the requested
  235. * interval value, and fires the interrupt when the value is reached. If reload
  236. * flag was set, timer will also reset itself upon reference value, otherwise
  237. * it continues to increment.
  238. *
  239. * The _exact_ bit in the function name states that this function will not
  240. * crop precision of the "usec" argument, thus usec is limited to 16 bits
  241. * (single timer width).
  242. */
  243. int gtm_set_exact_timer16(struct gtm_timer *tmr, u16 usec, bool reload)
  244. {
  245. /* quite obvious, frequency which is enough for µSec precision */
  246. const int freq = 1000000;
  247. /*
  248. * We can lower the frequency (and probably power consumption) by
  249. * dividing both frequency and usec by 2 until there is no remainder.
  250. * But we won't bother with this unless savings are measured, so just
  251. * run the timer as is.
  252. */
  253. return gtm_set_ref_timer16(tmr, freq, usec, reload);
  254. }
  255. EXPORT_SYMBOL(gtm_set_exact_timer16);
  256. /**
  257. * gtm_stop_timer16 - stop single timer
  258. * @tmr: pointer to the gtm_timer structure obtained from gtm_get_timer
  259. * Context: any
  260. *
  261. * This function simply stops the GTM timer.
  262. */
  263. void gtm_stop_timer16(struct gtm_timer *tmr)
  264. {
  265. struct gtm *gtm = tmr->gtm;
  266. int num = tmr - &gtm->timers[0];
  267. unsigned long flags;
  268. spin_lock_irqsave(&gtm->lock, flags);
  269. setbits8(tmr->gtcfr, GTCFR_STP(num));
  270. out_be16(tmr->gtevr, 0xFFFF);
  271. spin_unlock_irqrestore(&gtm->lock, flags);
  272. }
  273. EXPORT_SYMBOL(gtm_stop_timer16);
  274. /**
  275. * gtm_ack_timer16 - acknowledge timer event (free-run timers only)
  276. * @tmr: pointer to the gtm_timer structure obtained from gtm_get_timer
  277. * @events: events mask to ack
  278. * Context: any
  279. *
  280. * Thus function used to acknowledge timer interrupt event, use it inside the
  281. * interrupt handler.
  282. */
  283. void gtm_ack_timer16(struct gtm_timer *tmr, u16 events)
  284. {
  285. out_be16(tmr->gtevr, events);
  286. }
  287. EXPORT_SYMBOL(gtm_ack_timer16);
  288. static void __init gtm_set_shortcuts(struct device_node *np,
  289. struct gtm_timer *timers,
  290. struct gtm_timers_regs __iomem *regs)
  291. {
  292. /*
  293. * Yeah, I don't like this either, but timers' registers a bit messed,
  294. * so we have to provide shortcuts to write timer independent code.
  295. * Alternative option is to create gt*() accessors, but that will be
  296. * even uglier and cryptic.
  297. */
  298. timers[0].gtcfr = &regs->gtcfr1;
  299. timers[0].gtmdr = &regs->gtmdr1;
  300. timers[0].gtcnr = &regs->gtcnr1;
  301. timers[0].gtrfr = &regs->gtrfr1;
  302. timers[0].gtevr = &regs->gtevr1;
  303. timers[1].gtcfr = &regs->gtcfr1;
  304. timers[1].gtmdr = &regs->gtmdr2;
  305. timers[1].gtcnr = &regs->gtcnr2;
  306. timers[1].gtrfr = &regs->gtrfr2;
  307. timers[1].gtevr = &regs->gtevr2;
  308. timers[2].gtcfr = &regs->gtcfr2;
  309. timers[2].gtmdr = &regs->gtmdr3;
  310. timers[2].gtcnr = &regs->gtcnr3;
  311. timers[2].gtrfr = &regs->gtrfr3;
  312. timers[2].gtevr = &regs->gtevr3;
  313. timers[3].gtcfr = &regs->gtcfr2;
  314. timers[3].gtmdr = &regs->gtmdr4;
  315. timers[3].gtcnr = &regs->gtcnr4;
  316. timers[3].gtrfr = &regs->gtrfr4;
  317. timers[3].gtevr = &regs->gtevr4;
  318. /* CPM2 doesn't have primary prescaler */
  319. if (!of_device_is_compatible(np, "fsl,cpm2-gtm")) {
  320. timers[0].gtpsr = &regs->gtpsr1;
  321. timers[1].gtpsr = &regs->gtpsr2;
  322. timers[2].gtpsr = &regs->gtpsr3;
  323. timers[3].gtpsr = &regs->gtpsr4;
  324. }
  325. }
  326. static int __init fsl_gtm_init(void)
  327. {
  328. struct device_node *np;
  329. for_each_compatible_node(np, NULL, "fsl,gtm") {
  330. int i;
  331. struct gtm *gtm;
  332. const u32 *clock;
  333. int size;
  334. gtm = kzalloc(sizeof(*gtm), GFP_KERNEL);
  335. if (!gtm) {
  336. pr_err("%pOF: unable to allocate memory\n",
  337. np);
  338. continue;
  339. }
  340. spin_lock_init(&gtm->lock);
  341. clock = of_get_property(np, "clock-frequency", &size);
  342. if (!clock || size != sizeof(*clock)) {
  343. pr_err("%pOF: no clock-frequency\n", np);
  344. goto err;
  345. }
  346. gtm->clock = *clock;
  347. for (i = 0; i < ARRAY_SIZE(gtm->timers); i++) {
  348. unsigned int irq;
  349. irq = irq_of_parse_and_map(np, i);
  350. if (!irq) {
  351. pr_err("%pOF: not enough interrupts specified\n",
  352. np);
  353. goto err;
  354. }
  355. gtm->timers[i].irq = irq;
  356. gtm->timers[i].gtm = gtm;
  357. }
  358. gtm->regs = of_iomap(np, 0);
  359. if (!gtm->regs) {
  360. pr_err("%pOF: unable to iomap registers\n",
  361. np);
  362. goto err;
  363. }
  364. gtm_set_shortcuts(np, gtm->timers, gtm->regs);
  365. list_add(&gtm->list_node, &gtms);
  366. /* We don't want to lose the node and its ->data */
  367. np->data = gtm;
  368. of_node_get(np);
  369. continue;
  370. err:
  371. kfree(gtm);
  372. }
  373. return 0;
  374. }
  375. arch_initcall(fsl_gtm_init);