1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright 2013 Freescale Semiconductor, Inc.
6 * Author: Dongsheng Wang <Dongsheng.Wang@freescale.com>
7 * Li Yang <leoli@freescale.com>
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_device.h>
20 #include <linux/of_irq.h>
21 #include <linux/syscore_ops.h>
22 #include <sysdev/fsl_soc.h>
25 #include <asm/mpic_timer.h>
27 #define FSL_GLOBAL_TIMER 0x1
30 * Divide by 64 0x00000300
31 * Divide by 32 0x00000200
32 * Divide by 16 0x00000100
33 * Divide by 8 0x00000000 (Hardware default div)
35 #define MPIC_TIMER_TCR_CLKDIV 0x00000300
37 #define MPIC_TIMER_TCR_ROVR_OFFSET 24
39 #define TIMER_STOP 0x80000000
40 #define GTCCR_TOG 0x80000000
41 #define TIMERS_PER_GROUP 4
42 #define MAX_TICKS (~0U >> 1)
43 #define MAX_TICKS_CASCADE (~0U)
44 #define TIMER_OFFSET(num) (1 << (TIMERS_PER_GROUP - 1 - num))
58 u32 tcr_value
; /* TCR register: CASC & ROVR value */
59 unsigned int cascade_map
; /* cascade map */
60 unsigned int timer_num
; /* cascade control timer */
63 struct timer_group_priv
{
64 struct timer_regs __iomem
*regs
;
65 struct mpic_timer timer
[TIMERS_PER_GROUP
];
66 struct list_head node
;
67 unsigned int timerfreq
;
71 void __iomem
*group_tcr
;
74 static struct cascade_priv cascade_timer
[] = {
75 /* cascade timer 0 and 1 */
77 /* cascade timer 1 and 2 */
79 /* cascade timer 2 and 3 */
83 static LIST_HEAD(timer_group_list
);
85 static void convert_ticks_to_time(struct timer_group_priv
*priv
,
86 const u64 ticks
, time64_t
*time
)
88 *time
= (u64
)div_u64(ticks
, priv
->timerfreq
);
91 /* the time set by the user is converted to "ticks" */
92 static int convert_time_to_ticks(struct timer_group_priv
*priv
,
93 time64_t time
, u64
*ticks
)
95 u64 max_value
; /* prevent u64 overflow */
97 max_value
= div_u64(ULLONG_MAX
, priv
->timerfreq
);
102 *ticks
= (u64
)time
* (u64
)priv
->timerfreq
;
107 /* detect whether there is a cascade timer available */
108 static struct mpic_timer
*detect_idle_cascade_timer(
109 struct timer_group_priv
*priv
)
111 struct cascade_priv
*casc_priv
;
113 unsigned int array_size
= ARRAY_SIZE(cascade_timer
);
118 casc_priv
= cascade_timer
;
119 for (i
= 0; i
< array_size
; i
++) {
120 spin_lock_irqsave(&priv
->lock
, flags
);
121 map
= casc_priv
->cascade_map
& priv
->idle
;
122 if (map
== casc_priv
->cascade_map
) {
123 num
= casc_priv
->timer_num
;
124 priv
->timer
[num
].cascade_handle
= casc_priv
;
127 priv
->idle
&= ~casc_priv
->cascade_map
;
128 spin_unlock_irqrestore(&priv
->lock
, flags
);
129 return &priv
->timer
[num
];
131 spin_unlock_irqrestore(&priv
->lock
, flags
);
138 static int set_cascade_timer(struct timer_group_priv
*priv
, u64 ticks
,
141 struct cascade_priv
*casc_priv
;
146 /* set group tcr reg for cascade */
147 casc_priv
= priv
->timer
[num
].cascade_handle
;
151 tcr
= casc_priv
->tcr_value
|
152 (casc_priv
->tcr_value
<< MPIC_TIMER_TCR_ROVR_OFFSET
);
153 setbits32(priv
->group_tcr
, tcr
);
155 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
);
160 out_be32(&priv
->regs
[num
- 1].gtccr
, 0);
161 out_be32(&priv
->regs
[num
- 1].gtbcr
, rem_ticks
);
166 static struct mpic_timer
*get_cascade_timer(struct timer_group_priv
*priv
,
169 struct mpic_timer
*allocated_timer
;
171 /* Two cascade timers: Support the maximum time */
172 const u64 max_ticks
= (u64
)MAX_TICKS
* (u64
)MAX_TICKS_CASCADE
;
175 if (ticks
> max_ticks
)
178 /* detect idle timer */
179 allocated_timer
= detect_idle_cascade_timer(priv
);
180 if (!allocated_timer
)
183 /* set ticks to timer */
184 ret
= set_cascade_timer(priv
, ticks
, allocated_timer
->num
);
188 return allocated_timer
;
191 static struct mpic_timer
*get_timer(time64_t time
)
193 struct timer_group_priv
*priv
;
194 struct mpic_timer
*timer
;
202 list_for_each_entry(priv
, &timer_group_list
, node
) {
203 ret
= convert_time_to_ticks(priv
, time
, &ticks
);
207 if (ticks
> MAX_TICKS
) {
208 if (!(priv
->flags
& FSL_GLOBAL_TIMER
))
211 timer
= get_cascade_timer(priv
, ticks
);
218 for (i
= 0; i
< TIMERS_PER_GROUP
; i
++) {
219 /* one timer: Reverse allocation */
220 num
= TIMERS_PER_GROUP
- 1 - i
;
221 spin_lock_irqsave(&priv
->lock
, flags
);
222 if (priv
->idle
& (1 << i
)) {
224 priv
->idle
&= ~(1 << i
);
225 /* set ticks & stop timer */
226 out_be32(&priv
->regs
[num
].gtbcr
,
228 out_be32(&priv
->regs
[num
].gtccr
, 0);
229 priv
->timer
[num
].cascade_handle
= NULL
;
230 spin_unlock_irqrestore(&priv
->lock
, flags
);
231 return &priv
->timer
[num
];
233 spin_unlock_irqrestore(&priv
->lock
, flags
);
241 * mpic_start_timer - start hardware timer
242 * @handle: the timer to be started.
244 * It will do ->fn(->dev) callback from the hardware interrupt at
245 * the 'time64_t' point in the future.
247 void mpic_start_timer(struct mpic_timer
*handle
)
249 struct timer_group_priv
*priv
= container_of(handle
,
250 struct timer_group_priv
, timer
[handle
->num
]);
252 clrbits32(&priv
->regs
[handle
->num
].gtbcr
, TIMER_STOP
);
254 EXPORT_SYMBOL(mpic_start_timer
);
257 * mpic_stop_timer - stop hardware timer
258 * @handle: the timer to be stoped
260 * The timer periodically generates an interrupt. Unless user stops the timer.
262 void mpic_stop_timer(struct mpic_timer
*handle
)
264 struct timer_group_priv
*priv
= container_of(handle
,
265 struct timer_group_priv
, timer
[handle
->num
]);
266 struct cascade_priv
*casc_priv
;
268 setbits32(&priv
->regs
[handle
->num
].gtbcr
, TIMER_STOP
);
270 casc_priv
= priv
->timer
[handle
->num
].cascade_handle
;
272 out_be32(&priv
->regs
[handle
->num
].gtccr
, 0);
273 out_be32(&priv
->regs
[handle
->num
- 1].gtccr
, 0);
275 out_be32(&priv
->regs
[handle
->num
].gtccr
, 0);
278 EXPORT_SYMBOL(mpic_stop_timer
);
281 * mpic_get_remain_time - get timer time
282 * @handle: the timer to be selected.
283 * @time: time for timer
285 * Query timer remaining time.
287 void mpic_get_remain_time(struct mpic_timer
*handle
, time64_t
*time
)
289 struct timer_group_priv
*priv
= container_of(handle
,
290 struct timer_group_priv
, timer
[handle
->num
]);
291 struct cascade_priv
*casc_priv
;
296 casc_priv
= priv
->timer
[handle
->num
].cascade_handle
;
298 tmp_ticks
= in_be32(&priv
->regs
[handle
->num
].gtccr
);
299 tmp_ticks
&= ~GTCCR_TOG
;
300 ticks
= ((u64
)tmp_ticks
& UINT_MAX
) * (u64
)MAX_TICKS_CASCADE
;
301 tmp_ticks
= in_be32(&priv
->regs
[handle
->num
- 1].gtccr
);
304 ticks
= in_be32(&priv
->regs
[handle
->num
].gtccr
);
308 convert_ticks_to_time(priv
, ticks
, time
);
310 EXPORT_SYMBOL(mpic_get_remain_time
);
313 * mpic_free_timer - free hardware timer
314 * @handle: the timer to be removed.
318 * Note: can not be used in interrupt context.
320 void mpic_free_timer(struct mpic_timer
*handle
)
322 struct timer_group_priv
*priv
= container_of(handle
,
323 struct timer_group_priv
, timer
[handle
->num
]);
325 struct cascade_priv
*casc_priv
;
328 mpic_stop_timer(handle
);
330 casc_priv
= priv
->timer
[handle
->num
].cascade_handle
;
332 free_irq(priv
->timer
[handle
->num
].irq
, priv
->timer
[handle
->num
].dev
);
334 spin_lock_irqsave(&priv
->lock
, flags
);
337 tcr
= casc_priv
->tcr_value
| (casc_priv
->tcr_value
<<
338 MPIC_TIMER_TCR_ROVR_OFFSET
);
339 clrbits32(priv
->group_tcr
, tcr
);
340 priv
->idle
|= casc_priv
->cascade_map
;
341 priv
->timer
[handle
->num
].cascade_handle
= NULL
;
343 priv
->idle
|= TIMER_OFFSET(handle
->num
);
345 spin_unlock_irqrestore(&priv
->lock
, flags
);
347 EXPORT_SYMBOL(mpic_free_timer
);
350 * mpic_request_timer - get a hardware timer
351 * @fn: interrupt handler function
352 * @dev: callback function of the data
353 * @time: time for timer
355 * This executes the "request_irq", returning NULL
356 * else "handle" on success.
358 struct mpic_timer
*mpic_request_timer(irq_handler_t fn
, void *dev
,
361 struct mpic_timer
*allocated_timer
;
364 if (list_empty(&timer_group_list
))
370 allocated_timer
= get_timer(time
);
371 if (!allocated_timer
)
374 ret
= request_irq(allocated_timer
->irq
, fn
,
375 IRQF_TRIGGER_LOW
, "global-timer", dev
);
377 mpic_free_timer(allocated_timer
);
381 allocated_timer
->dev
= dev
;
383 return allocated_timer
;
385 EXPORT_SYMBOL(mpic_request_timer
);
387 static int timer_group_get_freq(struct device_node
*np
,
388 struct timer_group_priv
*priv
)
392 if (priv
->flags
& FSL_GLOBAL_TIMER
) {
393 struct device_node
*dn
;
395 dn
= of_find_compatible_node(NULL
, NULL
, "fsl,mpic");
397 of_property_read_u32(dn
, "clock-frequency",
403 if (priv
->timerfreq
<= 0)
406 if (priv
->flags
& FSL_GLOBAL_TIMER
) {
407 div
= (1 << (MPIC_TIMER_TCR_CLKDIV
>> 8)) * 8;
408 priv
->timerfreq
/= div
;
414 static int timer_group_get_irq(struct device_node
*np
,
415 struct timer_group_priv
*priv
)
417 const u32 all_timer
[] = { 0, TIMERS_PER_GROUP
};
424 unsigned int irq_index
= 0;
428 p
= of_get_property(np
, "fsl,available-ranges", &len
);
429 if (p
&& len
% (2 * sizeof(u32
)) != 0) {
430 pr_err("%pOF: malformed available-ranges property.\n", np
);
436 len
= sizeof(all_timer
);
439 len
/= 2 * sizeof(u32
);
441 for (i
= 0; i
< len
; i
++) {
443 count
= p
[i
* 2 + 1];
444 for (j
= 0; j
< count
; j
++) {
445 irq
= irq_of_parse_and_map(np
, irq_index
);
447 pr_err("%pOF: irq parse and map failed.\n", np
);
452 priv
->idle
|= TIMER_OFFSET((offset
+ j
));
453 priv
->timer
[offset
+ j
].irq
= irq
;
454 priv
->timer
[offset
+ j
].num
= offset
+ j
;
462 static void timer_group_init(struct device_node
*np
)
464 struct timer_group_priv
*priv
;
468 priv
= kzalloc(sizeof(struct timer_group_priv
), GFP_KERNEL
);
470 pr_err("%pOF: cannot allocate memory for group.\n", np
);
474 if (of_device_is_compatible(np
, "fsl,mpic-global-timer"))
475 priv
->flags
|= FSL_GLOBAL_TIMER
;
477 priv
->regs
= of_iomap(np
, i
++);
479 pr_err("%pOF: cannot ioremap timer register address.\n", np
);
483 if (priv
->flags
& FSL_GLOBAL_TIMER
) {
484 priv
->group_tcr
= of_iomap(np
, i
++);
485 if (!priv
->group_tcr
) {
486 pr_err("%pOF: cannot ioremap tcr address.\n", np
);
491 ret
= timer_group_get_freq(np
, priv
);
493 pr_err("%pOF: cannot get timer frequency.\n", np
);
497 ret
= timer_group_get_irq(np
, priv
);
499 pr_err("%pOF: cannot get timer irqs.\n", np
);
503 spin_lock_init(&priv
->lock
);
505 /* Init FSL timer hardware */
506 if (priv
->flags
& FSL_GLOBAL_TIMER
)
507 setbits32(priv
->group_tcr
, MPIC_TIMER_TCR_CLKDIV
);
509 list_add_tail(&priv
->node
, &timer_group_list
);
518 iounmap(priv
->group_tcr
);
523 static void mpic_timer_resume(void)
525 struct timer_group_priv
*priv
;
527 list_for_each_entry(priv
, &timer_group_list
, node
) {
528 /* Init FSL timer hardware */
529 if (priv
->flags
& FSL_GLOBAL_TIMER
)
530 setbits32(priv
->group_tcr
, MPIC_TIMER_TCR_CLKDIV
);
534 static const struct of_device_id mpic_timer_ids
[] = {
535 { .compatible
= "fsl,mpic-global-timer", },
539 static struct syscore_ops mpic_timer_syscore_ops
= {
540 .resume
= mpic_timer_resume
,
543 static int __init
mpic_timer_init(void)
545 struct device_node
*np
= NULL
;
547 for_each_matching_node(np
, mpic_timer_ids
)
548 timer_group_init(np
);
550 register_syscore_ops(&mpic_timer_syscore_ops
);
552 if (list_empty(&timer_group_list
))
557 subsys_initcall(mpic_timer_init
);