mpic_timer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * MPIC timer driver
  3. *
  4. * Copyright 2013 Freescale Semiconductor, Inc.
  5. * Author: Dongsheng Wang <Dongsheng.Wang@freescale.com>
  6. * Li Yang <leoli@freescale.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/errno.h>
  17. #include <linux/mm.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/slab.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <linux/of_device.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/syscore_ops.h>
  25. #include <sysdev/fsl_soc.h>
  26. #include <asm/io.h>
  27. #include <asm/mpic_timer.h>
  28. #define FSL_GLOBAL_TIMER 0x1
  29. /* Clock Ratio
  30. * Divide by 64 0x00000300
  31. * Divide by 32 0x00000200
  32. * Divide by 16 0x00000100
  33. * Divide by 8 0x00000000 (Hardware default div)
  34. */
  35. #define MPIC_TIMER_TCR_CLKDIV 0x00000300
  36. #define MPIC_TIMER_TCR_ROVR_OFFSET 24
  37. #define TIMER_STOP 0x80000000
  38. #define GTCCR_TOG 0x80000000
  39. #define TIMERS_PER_GROUP 4
  40. #define MAX_TICKS (~0U >> 1)
  41. #define MAX_TICKS_CASCADE (~0U)
  42. #define TIMER_OFFSET(num) (1 << (TIMERS_PER_GROUP - 1 - num))
  43. struct timer_regs {
  44. u32 gtccr;
  45. u32 res0[3];
  46. u32 gtbcr;
  47. u32 res1[3];
  48. u32 gtvpr;
  49. u32 res2[3];
  50. u32 gtdr;
  51. u32 res3[3];
  52. };
  53. struct cascade_priv {
  54. u32 tcr_value; /* TCR register: CASC & ROVR value */
  55. unsigned int cascade_map; /* cascade map */
  56. unsigned int timer_num; /* cascade control timer */
  57. };
  58. struct timer_group_priv {
  59. struct timer_regs __iomem *regs;
  60. struct mpic_timer timer[TIMERS_PER_GROUP];
  61. struct list_head node;
  62. unsigned int timerfreq;
  63. unsigned int idle;
  64. unsigned int flags;
  65. spinlock_t lock;
  66. void __iomem *group_tcr;
  67. };
  68. static struct cascade_priv cascade_timer[] = {
  69. /* cascade timer 0 and 1 */
  70. {0x1, 0xc, 0x1},
  71. /* cascade timer 1 and 2 */
  72. {0x2, 0x6, 0x2},
  73. /* cascade timer 2 and 3 */
  74. {0x4, 0x3, 0x3}
  75. };
  76. static LIST_HEAD(timer_group_list);
  77. static void convert_ticks_to_time(struct timer_group_priv *priv,
  78. const u64 ticks, time64_t *time)
  79. {
  80. *time = (u64)div_u64(ticks, priv->timerfreq);
  81. }
  82. /* the time set by the user is converted to "ticks" */
  83. static int convert_time_to_ticks(struct timer_group_priv *priv,
  84. time64_t time, u64 *ticks)
  85. {
  86. u64 max_value; /* prevent u64 overflow */
  87. max_value = div_u64(ULLONG_MAX, priv->timerfreq);
  88. if (time > max_value)
  89. return -EINVAL;
  90. *ticks = (u64)time * (u64)priv->timerfreq;
  91. return 0;
  92. }
  93. /* detect whether there is a cascade timer available */
  94. static struct mpic_timer *detect_idle_cascade_timer(
  95. struct timer_group_priv *priv)
  96. {
  97. struct cascade_priv *casc_priv;
  98. unsigned int map;
  99. unsigned int array_size = ARRAY_SIZE(cascade_timer);
  100. unsigned int num;
  101. unsigned int i;
  102. unsigned long flags;
  103. casc_priv = cascade_timer;
  104. for (i = 0; i < array_size; i++) {
  105. spin_lock_irqsave(&priv->lock, flags);
  106. map = casc_priv->cascade_map & priv->idle;
  107. if (map == casc_priv->cascade_map) {
  108. num = casc_priv->timer_num;
  109. priv->timer[num].cascade_handle = casc_priv;
  110. /* set timer busy */
  111. priv->idle &= ~casc_priv->cascade_map;
  112. spin_unlock_irqrestore(&priv->lock, flags);
  113. return &priv->timer[num];
  114. }
  115. spin_unlock_irqrestore(&priv->lock, flags);
  116. casc_priv++;
  117. }
  118. return NULL;
  119. }
  120. static int set_cascade_timer(struct timer_group_priv *priv, u64 ticks,
  121. unsigned int num)
  122. {
  123. struct cascade_priv *casc_priv;
  124. u32 tcr;
  125. u32 tmp_ticks;
  126. u32 rem_ticks;
  127. /* set group tcr reg for cascade */
  128. casc_priv = priv->timer[num].cascade_handle;
  129. if (!casc_priv)
  130. return -EINVAL;
  131. tcr = casc_priv->tcr_value |
  132. (casc_priv->tcr_value << MPIC_TIMER_TCR_ROVR_OFFSET);
  133. setbits32(priv->group_tcr, tcr);
  134. tmp_ticks = div_u64_rem(ticks, MAX_TICKS_CASCADE, &rem_ticks);
  135. out_be32(&priv->regs[num].gtccr, 0);
  136. out_be32(&priv->regs[num].gtbcr, tmp_ticks | TIMER_STOP);
  137. out_be32(&priv->regs[num - 1].gtccr, 0);
  138. out_be32(&priv->regs[num - 1].gtbcr, rem_ticks);
  139. return 0;
  140. }
  141. static struct mpic_timer *get_cascade_timer(struct timer_group_priv *priv,
  142. u64 ticks)
  143. {
  144. struct mpic_timer *allocated_timer;
  145. /* Two cascade timers: Support the maximum time */
  146. const u64 max_ticks = (u64)MAX_TICKS * (u64)MAX_TICKS_CASCADE;
  147. int ret;
  148. if (ticks > max_ticks)
  149. return NULL;
  150. /* detect idle timer */
  151. allocated_timer = detect_idle_cascade_timer(priv);
  152. if (!allocated_timer)
  153. return NULL;
  154. /* set ticks to timer */
  155. ret = set_cascade_timer(priv, ticks, allocated_timer->num);
  156. if (ret < 0)
  157. return NULL;
  158. return allocated_timer;
  159. }
  160. static struct mpic_timer *get_timer(time64_t time)
  161. {
  162. struct timer_group_priv *priv;
  163. struct mpic_timer *timer;
  164. u64 ticks;
  165. unsigned int num;
  166. unsigned int i;
  167. unsigned long flags;
  168. int ret;
  169. list_for_each_entry(priv, &timer_group_list, node) {
  170. ret = convert_time_to_ticks(priv, time, &ticks);
  171. if (ret < 0)
  172. return NULL;
  173. if (ticks > MAX_TICKS) {
  174. if (!(priv->flags & FSL_GLOBAL_TIMER))
  175. return NULL;
  176. timer = get_cascade_timer(priv, ticks);
  177. if (!timer)
  178. continue;
  179. return timer;
  180. }
  181. for (i = 0; i < TIMERS_PER_GROUP; i++) {
  182. /* one timer: Reverse allocation */
  183. num = TIMERS_PER_GROUP - 1 - i;
  184. spin_lock_irqsave(&priv->lock, flags);
  185. if (priv->idle & (1 << i)) {
  186. /* set timer busy */
  187. priv->idle &= ~(1 << i);
  188. /* set ticks & stop timer */
  189. out_be32(&priv->regs[num].gtbcr,
  190. ticks | TIMER_STOP);
  191. out_be32(&priv->regs[num].gtccr, 0);
  192. priv->timer[num].cascade_handle = NULL;
  193. spin_unlock_irqrestore(&priv->lock, flags);
  194. return &priv->timer[num];
  195. }
  196. spin_unlock_irqrestore(&priv->lock, flags);
  197. }
  198. }
  199. return NULL;
  200. }
  201. /**
  202. * mpic_start_timer - start hardware timer
  203. * @handle: the timer to be started.
  204. *
  205. * It will do ->fn(->dev) callback from the hardware interrupt at
  206. * the 'time64_t' point in the future.
  207. */
  208. void mpic_start_timer(struct mpic_timer *handle)
  209. {
  210. struct timer_group_priv *priv = container_of(handle,
  211. struct timer_group_priv, timer[handle->num]);
  212. clrbits32(&priv->regs[handle->num].gtbcr, TIMER_STOP);
  213. }
  214. EXPORT_SYMBOL(mpic_start_timer);
  215. /**
  216. * mpic_stop_timer - stop hardware timer
  217. * @handle: the timer to be stoped
  218. *
  219. * The timer periodically generates an interrupt. Unless user stops the timer.
  220. */
  221. void mpic_stop_timer(struct mpic_timer *handle)
  222. {
  223. struct timer_group_priv *priv = container_of(handle,
  224. struct timer_group_priv, timer[handle->num]);
  225. struct cascade_priv *casc_priv;
  226. setbits32(&priv->regs[handle->num].gtbcr, TIMER_STOP);
  227. casc_priv = priv->timer[handle->num].cascade_handle;
  228. if (casc_priv) {
  229. out_be32(&priv->regs[handle->num].gtccr, 0);
  230. out_be32(&priv->regs[handle->num - 1].gtccr, 0);
  231. } else {
  232. out_be32(&priv->regs[handle->num].gtccr, 0);
  233. }
  234. }
  235. EXPORT_SYMBOL(mpic_stop_timer);
  236. /**
  237. * mpic_get_remain_time - get timer time
  238. * @handle: the timer to be selected.
  239. * @time: time for timer
  240. *
  241. * Query timer remaining time.
  242. */
  243. void mpic_get_remain_time(struct mpic_timer *handle, time64_t *time)
  244. {
  245. struct timer_group_priv *priv = container_of(handle,
  246. struct timer_group_priv, timer[handle->num]);
  247. struct cascade_priv *casc_priv;
  248. u64 ticks;
  249. u32 tmp_ticks;
  250. casc_priv = priv->timer[handle->num].cascade_handle;
  251. if (casc_priv) {
  252. tmp_ticks = in_be32(&priv->regs[handle->num].gtccr);
  253. tmp_ticks &= ~GTCCR_TOG;
  254. ticks = ((u64)tmp_ticks & UINT_MAX) * (u64)MAX_TICKS_CASCADE;
  255. tmp_ticks = in_be32(&priv->regs[handle->num - 1].gtccr);
  256. ticks += tmp_ticks;
  257. } else {
  258. ticks = in_be32(&priv->regs[handle->num].gtccr);
  259. ticks &= ~GTCCR_TOG;
  260. }
  261. convert_ticks_to_time(priv, ticks, time);
  262. }
  263. EXPORT_SYMBOL(mpic_get_remain_time);
  264. /**
  265. * mpic_free_timer - free hardware timer
  266. * @handle: the timer to be removed.
  267. *
  268. * Free the timer.
  269. *
  270. * Note: can not be used in interrupt context.
  271. */
  272. void mpic_free_timer(struct mpic_timer *handle)
  273. {
  274. struct timer_group_priv *priv = container_of(handle,
  275. struct timer_group_priv, timer[handle->num]);
  276. struct cascade_priv *casc_priv;
  277. unsigned long flags;
  278. mpic_stop_timer(handle);
  279. casc_priv = priv->timer[handle->num].cascade_handle;
  280. free_irq(priv->timer[handle->num].irq, priv->timer[handle->num].dev);
  281. spin_lock_irqsave(&priv->lock, flags);
  282. if (casc_priv) {
  283. u32 tcr;
  284. tcr = casc_priv->tcr_value | (casc_priv->tcr_value <<
  285. MPIC_TIMER_TCR_ROVR_OFFSET);
  286. clrbits32(priv->group_tcr, tcr);
  287. priv->idle |= casc_priv->cascade_map;
  288. priv->timer[handle->num].cascade_handle = NULL;
  289. } else {
  290. priv->idle |= TIMER_OFFSET(handle->num);
  291. }
  292. spin_unlock_irqrestore(&priv->lock, flags);
  293. }
  294. EXPORT_SYMBOL(mpic_free_timer);
  295. /**
  296. * mpic_request_timer - get a hardware timer
  297. * @fn: interrupt handler function
  298. * @dev: callback function of the data
  299. * @time: time for timer
  300. *
  301. * This executes the "request_irq", returning NULL
  302. * else "handle" on success.
  303. */
  304. struct mpic_timer *mpic_request_timer(irq_handler_t fn, void *dev,
  305. time64_t time)
  306. {
  307. struct mpic_timer *allocated_timer;
  308. int ret;
  309. if (list_empty(&timer_group_list))
  310. return NULL;
  311. if (time < 0)
  312. return NULL;
  313. allocated_timer = get_timer(time);
  314. if (!allocated_timer)
  315. return NULL;
  316. ret = request_irq(allocated_timer->irq, fn,
  317. IRQF_TRIGGER_LOW, "global-timer", dev);
  318. if (ret) {
  319. mpic_free_timer(allocated_timer);
  320. return NULL;
  321. }
  322. allocated_timer->dev = dev;
  323. return allocated_timer;
  324. }
  325. EXPORT_SYMBOL(mpic_request_timer);
  326. static int timer_group_get_freq(struct device_node *np,
  327. struct timer_group_priv *priv)
  328. {
  329. u32 div;
  330. if (priv->flags & FSL_GLOBAL_TIMER) {
  331. struct device_node *dn;
  332. dn = of_find_compatible_node(NULL, NULL, "fsl,mpic");
  333. if (dn) {
  334. of_property_read_u32(dn, "clock-frequency",
  335. &priv->timerfreq);
  336. of_node_put(dn);
  337. }
  338. }
  339. if (priv->timerfreq <= 0)
  340. return -EINVAL;
  341. if (priv->flags & FSL_GLOBAL_TIMER) {
  342. div = (1 << (MPIC_TIMER_TCR_CLKDIV >> 8)) * 8;
  343. priv->timerfreq /= div;
  344. }
  345. return 0;
  346. }
  347. static int timer_group_get_irq(struct device_node *np,
  348. struct timer_group_priv *priv)
  349. {
  350. const u32 all_timer[] = { 0, TIMERS_PER_GROUP };
  351. const u32 *p;
  352. u32 offset;
  353. u32 count;
  354. unsigned int i;
  355. unsigned int j;
  356. unsigned int irq_index = 0;
  357. unsigned int irq;
  358. int len;
  359. p = of_get_property(np, "fsl,available-ranges", &len);
  360. if (p && len % (2 * sizeof(u32)) != 0) {
  361. pr_err("%pOF: malformed available-ranges property.\n", np);
  362. return -EINVAL;
  363. }
  364. if (!p) {
  365. p = all_timer;
  366. len = sizeof(all_timer);
  367. }
  368. len /= 2 * sizeof(u32);
  369. for (i = 0; i < len; i++) {
  370. offset = p[i * 2];
  371. count = p[i * 2 + 1];
  372. for (j = 0; j < count; j++) {
  373. irq = irq_of_parse_and_map(np, irq_index);
  374. if (!irq) {
  375. pr_err("%pOF: irq parse and map failed.\n", np);
  376. return -EINVAL;
  377. }
  378. /* Set timer idle */
  379. priv->idle |= TIMER_OFFSET((offset + j));
  380. priv->timer[offset + j].irq = irq;
  381. priv->timer[offset + j].num = offset + j;
  382. irq_index++;
  383. }
  384. }
  385. return 0;
  386. }
  387. static void timer_group_init(struct device_node *np)
  388. {
  389. struct timer_group_priv *priv;
  390. unsigned int i = 0;
  391. int ret;
  392. priv = kzalloc(sizeof(struct timer_group_priv), GFP_KERNEL);
  393. if (!priv) {
  394. pr_err("%pOF: cannot allocate memory for group.\n", np);
  395. return;
  396. }
  397. if (of_device_is_compatible(np, "fsl,mpic-global-timer"))
  398. priv->flags |= FSL_GLOBAL_TIMER;
  399. priv->regs = of_iomap(np, i++);
  400. if (!priv->regs) {
  401. pr_err("%pOF: cannot ioremap timer register address.\n", np);
  402. goto out;
  403. }
  404. if (priv->flags & FSL_GLOBAL_TIMER) {
  405. priv->group_tcr = of_iomap(np, i++);
  406. if (!priv->group_tcr) {
  407. pr_err("%pOF: cannot ioremap tcr address.\n", np);
  408. goto out;
  409. }
  410. }
  411. ret = timer_group_get_freq(np, priv);
  412. if (ret < 0) {
  413. pr_err("%pOF: cannot get timer frequency.\n", np);
  414. goto out;
  415. }
  416. ret = timer_group_get_irq(np, priv);
  417. if (ret < 0) {
  418. pr_err("%pOF: cannot get timer irqs.\n", np);
  419. goto out;
  420. }
  421. spin_lock_init(&priv->lock);
  422. /* Init FSL timer hardware */
  423. if (priv->flags & FSL_GLOBAL_TIMER)
  424. setbits32(priv->group_tcr, MPIC_TIMER_TCR_CLKDIV);
  425. list_add_tail(&priv->node, &timer_group_list);
  426. return;
  427. out:
  428. if (priv->regs)
  429. iounmap(priv->regs);
  430. if (priv->group_tcr)
  431. iounmap(priv->group_tcr);
  432. kfree(priv);
  433. }
  434. static void mpic_timer_resume(void)
  435. {
  436. struct timer_group_priv *priv;
  437. list_for_each_entry(priv, &timer_group_list, node) {
  438. /* Init FSL timer hardware */
  439. if (priv->flags & FSL_GLOBAL_TIMER)
  440. setbits32(priv->group_tcr, MPIC_TIMER_TCR_CLKDIV);
  441. }
  442. }
  443. static const struct of_device_id mpic_timer_ids[] = {
  444. { .compatible = "fsl,mpic-global-timer", },
  445. {},
  446. };
  447. static struct syscore_ops mpic_timer_syscore_ops = {
  448. .resume = mpic_timer_resume,
  449. };
  450. static int __init mpic_timer_init(void)
  451. {
  452. struct device_node *np = NULL;
  453. for_each_matching_node(np, mpic_timer_ids)
  454. timer_group_init(np);
  455. register_syscore_ops(&mpic_timer_syscore_ops);
  456. if (list_empty(&timer_group_list))
  457. return -ENODEV;
  458. return 0;
  459. }
  460. subsys_initcall(mpic_timer_init);