fsl_gtm.c 12 KB

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