mpic_timer.c 12 KB

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