1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright 2013 Freescale Semiconductor, Inc.
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/errno.h>
15 #include <linux/interrupt.h>
16 #include <linux/slab.h>
18 #include <linux/of_address.h>
19 #include <linux/of_irq.h>
20 #include <linux/syscore_ops.h>
21 #include <sysdev/fsl_soc.h>
24 #include <asm/mpic_timer.h>
26 #define FSL_GLOBAL_TIMER 0x1
29 * Divide by 64 0x00000300
30 * Divide by 32 0x00000200
31 * Divide by 16 0x00000100
32 * Divide by 8 0x00000000 (Hardware default div)
34 #define MPIC_TIMER_TCR_CLKDIV 0x00000300
36 #define MPIC_TIMER_TCR_ROVR_OFFSET 24
38 #define TIMER_STOP 0x80000000
39 #define GTCCR_TOG 0x80000000
40 #define TIMERS_PER_GROUP 4
41 #define MAX_TICKS (~0U >> 1)
42 #define MAX_TICKS_CASCADE (~0U)
43 #define TIMER_OFFSET(num) (1 << (TIMERS_PER_GROUP - 1 - num))
57 u32 tcr_value; /* TCR register: CASC & ROVR value */
58 unsigned int cascade_map; /* cascade map */
59 unsigned int timer_num; /* cascade control timer */
62 struct timer_group_priv {
63 struct timer_regs __iomem *regs;
64 struct mpic_timer timer[TIMERS_PER_GROUP];
65 struct list_head node;
66 unsigned int timerfreq;
70 void __iomem *group_tcr;
73 static struct cascade_priv cascade_timer[] = {
74 /* cascade timer 0 and 1 */
76 /* cascade timer 1 and 2 */
78 /* cascade timer 2 and 3 */
82 static LIST_HEAD(timer_group_list);
84 static void convert_ticks_to_time(struct timer_group_priv *priv,
85 const u64 ticks, time64_t *time)
87 *time = (u64)div_u64(ticks, priv->timerfreq);
90 /* the time set by the user is converted to "ticks" */
91 static int convert_time_to_ticks(struct timer_group_priv *priv,
92 time64_t time, u64 *ticks)
94 u64 max_value; /* prevent u64 overflow */
96 max_value = div_u64(ULLONG_MAX, priv->timerfreq);
101 *ticks = (u64)time * (u64)priv->timerfreq;
106 /* detect whether there is a cascade timer available */
107 static struct mpic_timer *detect_idle_cascade_timer(
108 struct timer_group_priv *priv)
110 struct cascade_priv *casc_priv;
112 unsigned int array_size = ARRAY_SIZE(cascade_timer);
117 casc_priv = cascade_timer;
118 for (i = 0; i < array_size; i++) {
119 spin_lock_irqsave(&priv->lock, flags);
120 map = casc_priv->cascade_map & priv->idle;
121 if (map == casc_priv->cascade_map) {
122 num = casc_priv->timer_num;
123 priv->timer[num].cascade_handle = casc_priv;
126 priv->idle &= ~casc_priv->cascade_map;
127 spin_unlock_irqrestore(&priv->lock, flags);
128 return &priv->timer[num];
130 spin_unlock_irqrestore(&priv->lock, flags);
137 static int set_cascade_timer(struct timer_group_priv *priv, u64 ticks,
140 struct cascade_priv *casc_priv;
145 /* set group tcr reg for cascade */
146 casc_priv = priv->timer[num].cascade_handle;
150 tcr = casc_priv->tcr_value |
151 (casc_priv->tcr_value << MPIC_TIMER_TCR_ROVR_OFFSET);
152 setbits32(priv->group_tcr, tcr);
154 tmp_ticks = div_u64_rem(ticks, MAX_TICKS_CASCADE, &rem_ticks);
156 out_be32(&priv->regs[num].gtccr, 0);
157 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);
165 static struct mpic_timer *get_cascade_timer(struct timer_group_priv *priv,
168 struct mpic_timer *allocated_timer;
170 /* Two cascade timers: Support the maximum time */
171 const u64 max_ticks = (u64)MAX_TICKS * (u64)MAX_TICKS_CASCADE;
174 if (ticks > max_ticks)
177 /* detect idle timer */
178 allocated_timer = detect_idle_cascade_timer(priv);
179 if (!allocated_timer)
182 /* set ticks to timer */
183 ret = set_cascade_timer(priv, ticks, allocated_timer->num);
187 return allocated_timer;
190 static struct mpic_timer *get_timer(time64_t time)
192 struct timer_group_priv *priv;
193 struct mpic_timer *timer;
201 list_for_each_entry(priv, &timer_group_list, node) {
202 ret = convert_time_to_ticks(priv, time, &ticks);
206 if (ticks > MAX_TICKS) {
207 if (!(priv->flags & FSL_GLOBAL_TIMER))
210 timer = get_cascade_timer(priv, ticks);
217 for (i = 0; i < TIMERS_PER_GROUP; i++) {
218 /* one timer: Reverse allocation */
219 num = TIMERS_PER_GROUP - 1 - i;
220 spin_lock_irqsave(&priv->lock, flags);
221 if (priv->idle & (1 << i)) {
223 priv->idle &= ~(1 << i);
224 /* set ticks & stop timer */
225 out_be32(&priv->regs[num].gtbcr,
227 out_be32(&priv->regs[num].gtccr, 0);
228 priv->timer[num].cascade_handle = NULL;
229 spin_unlock_irqrestore(&priv->lock, flags);
230 return &priv->timer[num];
232 spin_unlock_irqrestore(&priv->lock, flags);
240 * mpic_start_timer - start hardware timer
241 * @handle: the timer to be started.
243 * It will do ->fn(->dev) callback from the hardware interrupt at
244 * the 'time64_t' point in the future.
246 void mpic_start_timer(struct mpic_timer *handle)
248 struct timer_group_priv *priv = container_of(handle,
249 struct timer_group_priv, timer[handle->num]);
251 clrbits32(&priv->regs[handle->num].gtbcr, TIMER_STOP);
253 EXPORT_SYMBOL(mpic_start_timer);
256 * mpic_stop_timer - stop hardware timer
257 * @handle: the timer to be stopped
259 * The timer periodically generates an interrupt. Unless user stops the timer.
261 void mpic_stop_timer(struct mpic_timer *handle)
263 struct timer_group_priv *priv = container_of(handle,
264 struct timer_group_priv, timer[handle->num]);
265 struct cascade_priv *casc_priv;
267 setbits32(&priv->regs[handle->num].gtbcr, TIMER_STOP);
269 casc_priv = priv->timer[handle->num].cascade_handle;
271 out_be32(&priv->regs[handle->num].gtccr, 0);
272 out_be32(&priv->regs[handle->num - 1].gtccr, 0);
274 out_be32(&priv->regs[handle->num].gtccr, 0);
277 EXPORT_SYMBOL(mpic_stop_timer);
280 * mpic_get_remain_time - get timer time
281 * @handle: the timer to be selected.
282 * @time: time for timer
284 * Query timer remaining time.
286 void mpic_get_remain_time(struct mpic_timer *handle, time64_t *time)
288 struct timer_group_priv *priv = container_of(handle,
289 struct timer_group_priv, timer[handle->num]);
290 struct cascade_priv *casc_priv;
295 casc_priv = priv->timer[handle->num].cascade_handle;
297 tmp_ticks = in_be32(&priv->regs[handle->num].gtccr);
298 tmp_ticks &= ~GTCCR_TOG;
299 ticks = ((u64)tmp_ticks & UINT_MAX) * (u64)MAX_TICKS_CASCADE;
300 tmp_ticks = in_be32(&priv->regs[handle->num - 1].gtccr);
303 ticks = in_be32(&priv->regs[handle->num].gtccr);
307 convert_ticks_to_time(priv, ticks, time);
309 EXPORT_SYMBOL(mpic_get_remain_time);
312 * mpic_free_timer - free hardware timer
313 * @handle: the timer to be removed.
317 * Note: can not be used in interrupt context.
319 void mpic_free_timer(struct mpic_timer *handle)
321 struct timer_group_priv *priv = container_of(handle,
322 struct timer_group_priv, timer[handle->num]);
324 struct cascade_priv *casc_priv;
327 mpic_stop_timer(handle);
329 casc_priv = priv->timer[handle->num].cascade_handle;
331 free_irq(priv->timer[handle->num].irq, priv->timer[handle->num].dev);
333 spin_lock_irqsave(&priv->lock, flags);
336 tcr = casc_priv->tcr_value | (casc_priv->tcr_value <<
337 MPIC_TIMER_TCR_ROVR_OFFSET);
338 clrbits32(priv->group_tcr, tcr);
339 priv->idle |= casc_priv->cascade_map;
340 priv->timer[handle->num].cascade_handle = NULL;
342 priv->idle |= TIMER_OFFSET(handle->num);
344 spin_unlock_irqrestore(&priv->lock, flags);
346 EXPORT_SYMBOL(mpic_free_timer);
349 * mpic_request_timer - get a hardware timer
350 * @fn: interrupt handler function
351 * @dev: callback function of the data
352 * @time: time for timer
354 * This executes the "request_irq", returning NULL
355 * else "handle" on success.
357 struct mpic_timer *mpic_request_timer(irq_handler_t fn, void *dev,
360 struct mpic_timer *allocated_timer;
363 if (list_empty(&timer_group_list))
369 allocated_timer = get_timer(time);
370 if (!allocated_timer)
373 ret = request_irq(allocated_timer->irq, fn,
374 IRQF_TRIGGER_LOW, "global-timer", dev);
376 mpic_free_timer(allocated_timer);
380 allocated_timer->dev = dev;
382 return allocated_timer;
384 EXPORT_SYMBOL(mpic_request_timer);
386 static int __init timer_group_get_freq(struct device_node *np,
387 struct timer_group_priv *priv)
391 if (priv->flags & FSL_GLOBAL_TIMER) {
392 struct device_node *dn;
394 dn = of_find_compatible_node(NULL, NULL, "fsl,mpic");
396 of_property_read_u32(dn, "clock-frequency",
402 if (priv->timerfreq <= 0)
405 if (priv->flags & FSL_GLOBAL_TIMER) {
406 div = (1 << (MPIC_TIMER_TCR_CLKDIV >> 8)) * 8;
407 priv->timerfreq /= div;
413 static int __init timer_group_get_irq(struct device_node *np,
414 struct timer_group_priv *priv)
416 const u32 all_timer[] = { 0, TIMERS_PER_GROUP };
423 unsigned int irq_index = 0;
427 p = of_get_property(np, "fsl,available-ranges", &len);
428 if (p && len % (2 * sizeof(u32)) != 0) {
429 pr_err("%pOF: malformed available-ranges property.\n", np);
435 len = sizeof(all_timer);
438 len /= 2 * sizeof(u32);
440 for (i = 0; i < len; i++) {
442 count = p[i * 2 + 1];
443 for (j = 0; j < count; j++) {
444 irq = irq_of_parse_and_map(np, irq_index);
446 pr_err("%pOF: irq parse and map failed.\n", np);
451 priv->idle |= TIMER_OFFSET((offset + j));
452 priv->timer[offset + j].irq = irq;
453 priv->timer[offset + j].num = offset + j;
461 static void __init timer_group_init(struct device_node *np)
463 struct timer_group_priv *priv;
467 priv = kzalloc(sizeof(struct timer_group_priv), GFP_KERNEL);
469 pr_err("%pOF: cannot allocate memory for group.\n", np);
473 if (of_device_is_compatible(np, "fsl,mpic-global-timer"))
474 priv->flags |= FSL_GLOBAL_TIMER;
476 priv->regs = of_iomap(np, i++);
478 pr_err("%pOF: cannot ioremap timer register address.\n", np);
482 if (priv->flags & FSL_GLOBAL_TIMER) {
483 priv->group_tcr = of_iomap(np, i++);
484 if (!priv->group_tcr) {
485 pr_err("%pOF: cannot ioremap tcr address.\n", np);
490 ret = timer_group_get_freq(np, priv);
492 pr_err("%pOF: cannot get timer frequency.\n", np);
496 ret = timer_group_get_irq(np, priv);
498 pr_err("%pOF: cannot get timer irqs.\n", np);
502 spin_lock_init(&priv->lock);
504 /* Init FSL timer hardware */
505 if (priv->flags & FSL_GLOBAL_TIMER)
506 setbits32(priv->group_tcr, MPIC_TIMER_TCR_CLKDIV);
508 list_add_tail(&priv->node, &timer_group_list);
517 iounmap(priv->group_tcr);
522 static void mpic_timer_resume(void)
524 struct timer_group_priv *priv;
526 list_for_each_entry(priv, &timer_group_list, node) {
527 /* Init FSL timer hardware */
528 if (priv->flags & FSL_GLOBAL_TIMER)
529 setbits32(priv->group_tcr, MPIC_TIMER_TCR_CLKDIV);
533 static const struct of_device_id mpic_timer_ids[] = {
534 { .compatible = "fsl,mpic-global-timer", },
538 static struct syscore_ops mpic_timer_syscore_ops = {
539 .resume = mpic_timer_resume,
542 static int __init mpic_timer_init(void)
544 struct device_node *np = NULL;
546 for_each_matching_node(np, mpic_timer_ids)
547 timer_group_init(np);
549 register_syscore_ops(&mpic_timer_syscore_ops);
551 if (list_empty(&timer_group_list))
556 subsys_initcall(mpic_timer_init);