2 * at91sam926x_time.c - Periodic Interval Timer (PIT) for at91sam926x
4 * Copyright (C) 2005-2006 M. Amine SAYA, ATMEL Rousset, France
5 * Revision 2005 M. Nicolas Diremdjian, ATMEL Rousset, France
6 * Converted to ClockSource/ClockEvents by David Brownell.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/kernel.h>
15 #include <linux/clk.h>
16 #include <linux/clockchips.h>
18 #include <asm/mach/time.h>
20 #include <asm/arch/at91_pit.h>
23 #define PIT_CPIV(x) ((x) & AT91_PIT_CPIV)
24 #define PIT_PICNT(x) (((x) & AT91_PIT_PICNT) >> 20)
26 static u32 pit_cycle
; /* write-once */
27 static u32 pit_cnt
; /* access only w/system irq blocked */
31 * Clocksource: just a monotonic counter of MCK/16 cycles.
32 * We don't care whether or not PIT irqs are enabled.
34 static cycle_t
read_pit_clk(void)
40 raw_local_irq_save(flags
);
42 t
= at91_sys_read(AT91_PIT_PIIR
);
43 raw_local_irq_restore(flags
);
45 elapsed
+= PIT_PICNT(t
) * pit_cycle
;
46 elapsed
+= PIT_CPIV(t
);
50 static struct clocksource pit_clk
= {
55 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
60 * Clockevent device: interrupts every 1/HZ (== pit_cycles * MCK/16)
63 pit_clkevt_mode(enum clock_event_mode mode
, struct clock_event_device
*dev
)
68 case CLOCK_EVT_MODE_PERIODIC
:
69 /* update clocksource counter, then enable the IRQ */
70 raw_local_irq_save(flags
);
71 pit_cnt
+= pit_cycle
* PIT_PICNT(at91_sys_read(AT91_PIT_PIVR
));
72 at91_sys_write(AT91_PIT_MR
, (pit_cycle
- 1) | AT91_PIT_PITEN
74 raw_local_irq_restore(flags
);
76 case CLOCK_EVT_MODE_ONESHOT
:
79 case CLOCK_EVT_MODE_SHUTDOWN
:
80 case CLOCK_EVT_MODE_UNUSED
:
81 /* disable irq, leaving the clocksource active */
82 at91_sys_write(AT91_PIT_MR
, (pit_cycle
- 1) | AT91_PIT_PITEN
);
84 case CLOCK_EVT_MODE_RESUME
:
89 static struct clock_event_device pit_clkevt
= {
91 .features
= CLOCK_EVT_FEAT_PERIODIC
,
94 .cpumask
= CPU_MASK_CPU0
,
95 .set_mode
= pit_clkevt_mode
,
100 * IRQ handler for the timer.
102 static irqreturn_t
at91sam926x_pit_interrupt(int irq
, void *dev_id
)
105 /* The PIT interrupt may be disabled, and is shared */
106 if ((pit_clkevt
.mode
== CLOCK_EVT_MODE_PERIODIC
)
107 && (at91_sys_read(AT91_PIT_SR
) & AT91_PIT_PITS
)) {
110 /* Get number of ticks performed before irq, and ack it */
111 nr_ticks
= PIT_PICNT(at91_sys_read(AT91_PIT_PIVR
));
113 pit_cnt
+= pit_cycle
;
114 pit_clkevt
.event_handler(&pit_clkevt
);
124 static struct irqaction at91sam926x_pit_irq
= {
126 .flags
= IRQF_SHARED
| IRQF_DISABLED
| IRQF_TIMER
| IRQF_IRQPOLL
,
127 .handler
= at91sam926x_pit_interrupt
130 static void at91sam926x_pit_reset(void)
132 /* Disable timer and irqs */
133 at91_sys_write(AT91_PIT_MR
, 0);
135 /* Clear any pending interrupts, wait for PIT to stop counting */
136 while (PIT_CPIV(at91_sys_read(AT91_PIT_PIVR
)) != 0)
139 /* Start PIT but don't enable IRQ */
140 at91_sys_write(AT91_PIT_MR
, (pit_cycle
- 1) | AT91_PIT_PITEN
);
144 * Set up both clocksource and clockevent support.
146 static void __init
at91sam926x_pit_init(void)
148 unsigned long pit_rate
;
152 * Use our actual MCK to figure out how many MCK/16 ticks per
153 * 1/HZ period (instead of a compile-time constant LATCH).
155 pit_rate
= clk_get_rate(clk_get(NULL
, "mck")) / 16;
156 pit_cycle
= (pit_rate
+ HZ
/2) / HZ
;
157 WARN_ON(((pit_cycle
- 1) & ~AT91_PIT_PIV
) != 0);
159 /* Initialize and enable the timer */
160 at91sam926x_pit_reset();
163 * Register clocksource. The high order bits of PIV are unused,
164 * so this isn't a 32-bit counter unless we get clockevent irqs.
166 pit_clk
.mult
= clocksource_hz2mult(pit_rate
, pit_clk
.shift
);
167 bits
= 12 /* PICNT */ + ilog2(pit_cycle
) /* PIV */;
168 pit_clk
.mask
= CLOCKSOURCE_MASK(bits
);
169 clocksource_register(&pit_clk
);
171 /* Set up irq handler */
172 setup_irq(AT91_ID_SYS
, &at91sam926x_pit_irq
);
174 /* Set up and register clockevents */
175 pit_clkevt
.mult
= div_sc(pit_rate
, NSEC_PER_SEC
, pit_clkevt
.shift
);
176 clockevents_register_device(&pit_clkevt
);
179 static void at91sam926x_pit_suspend(void)
182 at91_sys_write(AT91_PIT_MR
, 0);
185 struct sys_timer at91sam926x_timer
= {
186 .init
= at91sam926x_pit_init
,
187 .suspend
= at91sam926x_pit_suspend
,
188 .resume
= at91sam926x_pit_reset
,