fsl_gtm.c 12 KB

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