mpic_timer.c 13 KB

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