4 * Copyright 2013 Freescale Semiconductor, Inc.
5 * Author: Dongsheng Wang <Dongsheng.Wang@freescale.com>
6 * Li Yang <leoli@freescale.com>
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.
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/errno.h>
19 #include <linux/interrupt.h>
20 #include <linux/slab.h>
22 #include <linux/of_address.h>
23 #include <linux/of_device.h>
24 #include <linux/of_irq.h>
25 #include <linux/syscore_ops.h>
26 #include <sysdev/fsl_soc.h>
29 #include <asm/mpic_timer.h>
31 #define FSL_GLOBAL_TIMER 0x1
34 * Divide by 64 0x00000300
35 * Divide by 32 0x00000200
36 * Divide by 16 0x00000100
37 * Divide by 8 0x00000000 (Hardware default div)
39 #define MPIC_TIMER_TCR_CLKDIV 0x00000300
41 #define MPIC_TIMER_TCR_ROVR_OFFSET 24
43 #define TIMER_STOP 0x80000000
44 #define GTCCR_TOG 0x80000000
45 #define TIMERS_PER_GROUP 4
46 #define MAX_TICKS (~0U >> 1)
47 #define MAX_TICKS_CASCADE (~0U)
48 #define TIMER_OFFSET(num) (1 << (TIMERS_PER_GROUP - 1 - num))
50 /* tv_usec should be less than ONE_SECOND, otherwise use tv_sec */
51 #define ONE_SECOND 1000000
65 u32 tcr_value
; /* TCR register: CASC & ROVR value */
66 unsigned int cascade_map
; /* cascade map */
67 unsigned int timer_num
; /* cascade control timer */
70 struct timer_group_priv
{
71 struct timer_regs __iomem
*regs
;
72 struct mpic_timer timer
[TIMERS_PER_GROUP
];
73 struct list_head node
;
74 unsigned int timerfreq
;
78 void __iomem
*group_tcr
;
81 static struct cascade_priv cascade_timer
[] = {
82 /* cascade timer 0 and 1 */
84 /* cascade timer 1 and 2 */
86 /* cascade timer 2 and 3 */
90 static LIST_HEAD(timer_group_list
);
92 static void convert_ticks_to_time(struct timer_group_priv
*priv
,
93 const u64 ticks
, struct timeval
*time
)
97 time
->tv_sec
= (__kernel_time_t
)div_u64(ticks
, priv
->timerfreq
);
98 tmp_sec
= (u64
)time
->tv_sec
* (u64
)priv
->timerfreq
;
102 if (tmp_sec
<= ticks
)
103 time
->tv_usec
= (__kernel_suseconds_t
)
104 div_u64((ticks
- tmp_sec
) * 1000000, priv
->timerfreq
);
109 /* the time set by the user is converted to "ticks" */
110 static int convert_time_to_ticks(struct timer_group_priv
*priv
,
111 const struct timeval
*time
, u64
*ticks
)
113 u64 max_value
; /* prevent u64 overflow */
120 max_value
= div_u64(ULLONG_MAX
, priv
->timerfreq
);
122 if (time
->tv_sec
> max_value
||
123 (time
->tv_sec
== max_value
&& time
->tv_usec
> 0))
126 tmp_sec
= (u64
)time
->tv_sec
* (u64
)priv
->timerfreq
;
129 tmp_ms
= time
->tv_usec
/ 1000;
130 tmp_ms
= div_u64((u64
)tmp_ms
* (u64
)priv
->timerfreq
, 1000);
133 tmp_us
= time
->tv_usec
% 1000;
134 tmp_us
= div_u64((u64
)tmp_us
* (u64
)priv
->timerfreq
, 1000000);
142 /* detect whether there is a cascade timer available */
143 static struct mpic_timer
*detect_idle_cascade_timer(
144 struct timer_group_priv
*priv
)
146 struct cascade_priv
*casc_priv
;
148 unsigned int array_size
= ARRAY_SIZE(cascade_timer
);
153 casc_priv
= cascade_timer
;
154 for (i
= 0; i
< array_size
; i
++) {
155 spin_lock_irqsave(&priv
->lock
, flags
);
156 map
= casc_priv
->cascade_map
& priv
->idle
;
157 if (map
== casc_priv
->cascade_map
) {
158 num
= casc_priv
->timer_num
;
159 priv
->timer
[num
].cascade_handle
= casc_priv
;
162 priv
->idle
&= ~casc_priv
->cascade_map
;
163 spin_unlock_irqrestore(&priv
->lock
, flags
);
164 return &priv
->timer
[num
];
166 spin_unlock_irqrestore(&priv
->lock
, flags
);
173 static int set_cascade_timer(struct timer_group_priv
*priv
, u64 ticks
,
176 struct cascade_priv
*casc_priv
;
181 /* set group tcr reg for cascade */
182 casc_priv
= priv
->timer
[num
].cascade_handle
;
186 tcr
= casc_priv
->tcr_value
|
187 (casc_priv
->tcr_value
<< MPIC_TIMER_TCR_ROVR_OFFSET
);
188 setbits32(priv
->group_tcr
, tcr
);
190 tmp_ticks
= div_u64_rem(ticks
, MAX_TICKS_CASCADE
, &rem_ticks
);
192 out_be32(&priv
->regs
[num
].gtccr
, 0);
193 out_be32(&priv
->regs
[num
].gtbcr
, tmp_ticks
| TIMER_STOP
);
195 out_be32(&priv
->regs
[num
- 1].gtccr
, 0);
196 out_be32(&priv
->regs
[num
- 1].gtbcr
, rem_ticks
);
201 static struct mpic_timer
*get_cascade_timer(struct timer_group_priv
*priv
,
204 struct mpic_timer
*allocated_timer
;
206 /* Two cascade timers: Support the maximum time */
207 const u64 max_ticks
= (u64
)MAX_TICKS
* (u64
)MAX_TICKS_CASCADE
;
210 if (ticks
> max_ticks
)
213 /* detect idle timer */
214 allocated_timer
= detect_idle_cascade_timer(priv
);
215 if (!allocated_timer
)
218 /* set ticks to timer */
219 ret
= set_cascade_timer(priv
, ticks
, allocated_timer
->num
);
223 return allocated_timer
;
226 static struct mpic_timer
*get_timer(const struct timeval
*time
)
228 struct timer_group_priv
*priv
;
229 struct mpic_timer
*timer
;
237 list_for_each_entry(priv
, &timer_group_list
, node
) {
238 ret
= convert_time_to_ticks(priv
, time
, &ticks
);
242 if (ticks
> MAX_TICKS
) {
243 if (!(priv
->flags
& FSL_GLOBAL_TIMER
))
246 timer
= get_cascade_timer(priv
, ticks
);
253 for (i
= 0; i
< TIMERS_PER_GROUP
; i
++) {
254 /* one timer: Reverse allocation */
255 num
= TIMERS_PER_GROUP
- 1 - i
;
256 spin_lock_irqsave(&priv
->lock
, flags
);
257 if (priv
->idle
& (1 << i
)) {
259 priv
->idle
&= ~(1 << i
);
260 /* set ticks & stop timer */
261 out_be32(&priv
->regs
[num
].gtbcr
,
263 out_be32(&priv
->regs
[num
].gtccr
, 0);
264 priv
->timer
[num
].cascade_handle
= NULL
;
265 spin_unlock_irqrestore(&priv
->lock
, flags
);
266 return &priv
->timer
[num
];
268 spin_unlock_irqrestore(&priv
->lock
, flags
);
276 * mpic_start_timer - start hardware timer
277 * @handle: the timer to be started.
279 * It will do ->fn(->dev) callback from the hardware interrupt at
280 * the ->timeval point in the future.
282 void mpic_start_timer(struct mpic_timer
*handle
)
284 struct timer_group_priv
*priv
= container_of(handle
,
285 struct timer_group_priv
, timer
[handle
->num
]);
287 clrbits32(&priv
->regs
[handle
->num
].gtbcr
, TIMER_STOP
);
289 EXPORT_SYMBOL(mpic_start_timer
);
292 * mpic_stop_timer - stop hardware timer
293 * @handle: the timer to be stoped
295 * The timer periodically generates an interrupt. Unless user stops the timer.
297 void mpic_stop_timer(struct mpic_timer
*handle
)
299 struct timer_group_priv
*priv
= container_of(handle
,
300 struct timer_group_priv
, timer
[handle
->num
]);
301 struct cascade_priv
*casc_priv
;
303 setbits32(&priv
->regs
[handle
->num
].gtbcr
, TIMER_STOP
);
305 casc_priv
= priv
->timer
[handle
->num
].cascade_handle
;
307 out_be32(&priv
->regs
[handle
->num
].gtccr
, 0);
308 out_be32(&priv
->regs
[handle
->num
- 1].gtccr
, 0);
310 out_be32(&priv
->regs
[handle
->num
].gtccr
, 0);
313 EXPORT_SYMBOL(mpic_stop_timer
);
316 * mpic_get_remain_time - get timer time
317 * @handle: the timer to be selected.
318 * @time: time for timer
320 * Query timer remaining time.
322 void mpic_get_remain_time(struct mpic_timer
*handle
, struct timeval
*time
)
324 struct timer_group_priv
*priv
= container_of(handle
,
325 struct timer_group_priv
, timer
[handle
->num
]);
326 struct cascade_priv
*casc_priv
;
331 casc_priv
= priv
->timer
[handle
->num
].cascade_handle
;
333 tmp_ticks
= in_be32(&priv
->regs
[handle
->num
].gtccr
);
334 tmp_ticks
&= ~GTCCR_TOG
;
335 ticks
= ((u64
)tmp_ticks
& UINT_MAX
) * (u64
)MAX_TICKS_CASCADE
;
336 tmp_ticks
= in_be32(&priv
->regs
[handle
->num
- 1].gtccr
);
339 ticks
= in_be32(&priv
->regs
[handle
->num
].gtccr
);
343 convert_ticks_to_time(priv
, ticks
, time
);
345 EXPORT_SYMBOL(mpic_get_remain_time
);
348 * mpic_free_timer - free hardware timer
349 * @handle: the timer to be removed.
353 * Note: can not be used in interrupt context.
355 void mpic_free_timer(struct mpic_timer
*handle
)
357 struct timer_group_priv
*priv
= container_of(handle
,
358 struct timer_group_priv
, timer
[handle
->num
]);
360 struct cascade_priv
*casc_priv
;
363 mpic_stop_timer(handle
);
365 casc_priv
= priv
->timer
[handle
->num
].cascade_handle
;
367 free_irq(priv
->timer
[handle
->num
].irq
, priv
->timer
[handle
->num
].dev
);
369 spin_lock_irqsave(&priv
->lock
, flags
);
372 tcr
= casc_priv
->tcr_value
| (casc_priv
->tcr_value
<<
373 MPIC_TIMER_TCR_ROVR_OFFSET
);
374 clrbits32(priv
->group_tcr
, tcr
);
375 priv
->idle
|= casc_priv
->cascade_map
;
376 priv
->timer
[handle
->num
].cascade_handle
= NULL
;
378 priv
->idle
|= TIMER_OFFSET(handle
->num
);
380 spin_unlock_irqrestore(&priv
->lock
, flags
);
382 EXPORT_SYMBOL(mpic_free_timer
);
385 * mpic_request_timer - get a hardware timer
386 * @fn: interrupt handler function
387 * @dev: callback function of the data
388 * @time: time for timer
390 * This executes the "request_irq", returning NULL
391 * else "handle" on success.
393 struct mpic_timer
*mpic_request_timer(irq_handler_t fn
, void *dev
,
394 const struct timeval
*time
)
396 struct mpic_timer
*allocated_timer
;
399 if (list_empty(&timer_group_list
))
402 if (!(time
->tv_sec
+ time
->tv_usec
) ||
403 time
->tv_sec
< 0 || time
->tv_usec
< 0)
406 if (time
->tv_usec
> ONE_SECOND
)
409 allocated_timer
= get_timer(time
);
410 if (!allocated_timer
)
413 ret
= request_irq(allocated_timer
->irq
, fn
,
414 IRQF_TRIGGER_LOW
, "global-timer", dev
);
416 mpic_free_timer(allocated_timer
);
420 allocated_timer
->dev
= dev
;
422 return allocated_timer
;
424 EXPORT_SYMBOL(mpic_request_timer
);
426 static int timer_group_get_freq(struct device_node
*np
,
427 struct timer_group_priv
*priv
)
431 if (priv
->flags
& FSL_GLOBAL_TIMER
) {
432 struct device_node
*dn
;
434 dn
= of_find_compatible_node(NULL
, NULL
, "fsl,mpic");
436 of_property_read_u32(dn
, "clock-frequency",
442 if (priv
->timerfreq
<= 0)
445 if (priv
->flags
& FSL_GLOBAL_TIMER
) {
446 div
= (1 << (MPIC_TIMER_TCR_CLKDIV
>> 8)) * 8;
447 priv
->timerfreq
/= div
;
453 static int timer_group_get_irq(struct device_node
*np
,
454 struct timer_group_priv
*priv
)
456 const u32 all_timer
[] = { 0, TIMERS_PER_GROUP
};
463 unsigned int irq_index
= 0;
467 p
= of_get_property(np
, "fsl,available-ranges", &len
);
468 if (p
&& len
% (2 * sizeof(u32
)) != 0) {
469 pr_err("%s: malformed available-ranges property.\n",
476 len
= sizeof(all_timer
);
479 len
/= 2 * sizeof(u32
);
481 for (i
= 0; i
< len
; i
++) {
483 count
= p
[i
* 2 + 1];
484 for (j
= 0; j
< count
; j
++) {
485 irq
= irq_of_parse_and_map(np
, irq_index
);
487 pr_err("%s: irq parse and map failed.\n",
493 priv
->idle
|= TIMER_OFFSET((offset
+ j
));
494 priv
->timer
[offset
+ j
].irq
= irq
;
495 priv
->timer
[offset
+ j
].num
= offset
+ j
;
503 static void timer_group_init(struct device_node
*np
)
505 struct timer_group_priv
*priv
;
509 priv
= kzalloc(sizeof(struct timer_group_priv
), GFP_KERNEL
);
511 pr_err("%s: cannot allocate memory for group.\n",
516 if (of_device_is_compatible(np
, "fsl,mpic-global-timer"))
517 priv
->flags
|= FSL_GLOBAL_TIMER
;
519 priv
->regs
= of_iomap(np
, i
++);
521 pr_err("%s: cannot ioremap timer register address.\n",
526 if (priv
->flags
& FSL_GLOBAL_TIMER
) {
527 priv
->group_tcr
= of_iomap(np
, i
++);
528 if (!priv
->group_tcr
) {
529 pr_err("%s: cannot ioremap tcr address.\n",
535 ret
= timer_group_get_freq(np
, priv
);
537 pr_err("%s: cannot get timer frequency.\n", np
->full_name
);
541 ret
= timer_group_get_irq(np
, priv
);
543 pr_err("%s: cannot get timer irqs.\n", np
->full_name
);
547 spin_lock_init(&priv
->lock
);
549 /* Init FSL timer hardware */
550 if (priv
->flags
& FSL_GLOBAL_TIMER
)
551 setbits32(priv
->group_tcr
, MPIC_TIMER_TCR_CLKDIV
);
553 list_add_tail(&priv
->node
, &timer_group_list
);
562 iounmap(priv
->group_tcr
);
567 static void mpic_timer_resume(void)
569 struct timer_group_priv
*priv
;
571 list_for_each_entry(priv
, &timer_group_list
, node
) {
572 /* Init FSL timer hardware */
573 if (priv
->flags
& FSL_GLOBAL_TIMER
)
574 setbits32(priv
->group_tcr
, MPIC_TIMER_TCR_CLKDIV
);
578 static const struct of_device_id mpic_timer_ids
[] = {
579 { .compatible
= "fsl,mpic-global-timer", },
583 static struct syscore_ops mpic_timer_syscore_ops
= {
584 .resume
= mpic_timer_resume
,
587 static int __init
mpic_timer_init(void)
589 struct device_node
*np
= NULL
;
591 for_each_matching_node(np
, mpic_timer_ids
)
592 timer_group_init(np
);
594 register_syscore_ops(&mpic_timer_syscore_ops
);
596 if (list_empty(&timer_group_list
))
601 subsys_initcall(mpic_timer_init
);