mpic_timer.c 14 KB

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