mpic_timer.c 13 KB

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