fsl_gtm.c 12 KB

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