fsl_gtm.c 12 KB

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