mpic_timer.c 12 KB

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