blob: 2a12e7fa97484edcd46370ca65754cced3455bea [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/***************************************************************************/
2
3/*
Greg Ungererb671b652006-06-26 10:33:10 +10004 * pit.c -- Freescale ColdFire PIT timer. Currently this type of
5 * hardware timer only exists in the Freescale ColdFire
Greg Ungerer8d80c5e2008-02-01 17:40:21 +10006 * 5270/5271, 5282 and 5208 CPUs. No doubt newer ColdFire
7 * family members will probably use it too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Greg Ungerer8d80c5e2008-02-01 17:40:21 +10009 * Copyright (C) 1999-2008, Greg Ungerer (gerg@snapgear.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Copyright (C) 2001-2004, SnapGear Inc. (www.snapgear.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13/***************************************************************************/
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
16#include <linux/sched.h>
17#include <linux/param.h>
18#include <linux/init.h>
19#include <linux/interrupt.h>
Greg Ungerer5c4525d2007-07-27 01:09:00 +100020#include <linux/irq.h>
Sebastian Siewior2b9a6982008-04-28 11:43:04 +020021#include <linux/clockchips.h>
Greg Ungerer2f2c2672007-10-23 14:37:54 +100022#include <asm/machdep.h>
Greg Ungererb671b652006-06-26 10:33:10 +100023#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/coldfire.h>
25#include <asm/mcfpit.h>
26#include <asm/mcfsim.h>
27
28/***************************************************************************/
29
Greg Ungererb671b652006-06-26 10:33:10 +100030/*
31 * By default use timer1 as the system clock timer.
32 */
Greg Ungerer8d80c5e2008-02-01 17:40:21 +100033#define FREQ ((MCF_CLK / 2) / 64)
Greg Ungererb671b652006-06-26 10:33:10 +100034#define TA(a) (MCF_IPSBAR + MCFPIT_BASE1 + (a))
Greg Ungerer8d80c5e2008-02-01 17:40:21 +100035#define INTC0 (MCF_IPSBAR + MCFICM_INTC0)
Sebastian Siewior2b9a6982008-04-28 11:43:04 +020036#define PIT_CYCLES_PER_JIFFY (FREQ / HZ)
Greg Ungerer8d80c5e2008-02-01 17:40:21 +100037
Greg Ungerer8d80c5e2008-02-01 17:40:21 +100038static u32 pit_cnt;
Greg Ungererb671b652006-06-26 10:33:10 +100039
Sebastian Siewior2b9a6982008-04-28 11:43:04 +020040/*
41 * Initialize the PIT timer.
42 *
43 * This is also called after resume to bring the PIT into operation again.
44 */
45
46static void init_cf_pit_timer(enum clock_event_mode mode,
47 struct clock_event_device *evt)
48{
49 switch (mode) {
50 case CLOCK_EVT_MODE_PERIODIC:
51
52 __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
53 __raw_writew(PIT_CYCLES_PER_JIFFY, TA(MCFPIT_PMR));
54 __raw_writew(MCFPIT_PCSR_EN | MCFPIT_PCSR_PIE | \
55 MCFPIT_PCSR_OVW | MCFPIT_PCSR_RLD | \
56 MCFPIT_PCSR_CLK64, TA(MCFPIT_PCSR));
57 break;
58
59 case CLOCK_EVT_MODE_SHUTDOWN:
60 case CLOCK_EVT_MODE_UNUSED:
61
62 __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
63 break;
64
65 case CLOCK_EVT_MODE_ONESHOT:
66
67 __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
68 __raw_writew(MCFPIT_PCSR_EN | MCFPIT_PCSR_PIE | \
69 MCFPIT_PCSR_OVW | MCFPIT_PCSR_CLK64, \
70 TA(MCFPIT_PCSR));
71 break;
72
73 case CLOCK_EVT_MODE_RESUME:
74 /* Nothing to do here */
75 break;
76 }
77}
78
79/*
80 * Program the next event in oneshot mode
81 *
82 * Delta is given in PIT ticks
83 */
84static int cf_pit_next_event(unsigned long delta,
85 struct clock_event_device *evt)
86{
87 __raw_writew(delta, TA(MCFPIT_PMR));
88 return 0;
89}
90
91struct clock_event_device cf_pit_clockevent = {
92 .name = "pit",
93 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
94 .set_mode = init_cf_pit_timer,
95 .set_next_event = cf_pit_next_event,
96 .shift = 32,
97 .irq = MCFINT_VECBASE + MCFINT_PIT1,
98};
99
100
101
Greg Ungererb671b652006-06-26 10:33:10 +1000102/***************************************************************************/
103
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000104static irqreturn_t pit_tick(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Sebastian Siewior2b9a6982008-04-28 11:43:04 +0200106 struct clock_event_device *evt = &cf_pit_clockevent;
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000107 u16 pcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 /* Reset the ColdFire timer */
Greg Ungererb671b652006-06-26 10:33:10 +1000110 pcsr = __raw_readw(TA(MCFPIT_PCSR));
111 __raw_writew(pcsr | MCFPIT_PCSR_PIF, TA(MCFPIT_PCSR));
Greg Ungerer2f2c2672007-10-23 14:37:54 +1000112
Sebastian Siewior2b9a6982008-04-28 11:43:04 +0200113 pit_cnt += PIT_CYCLES_PER_JIFFY;
114 evt->event_handler(evt);
115 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118/***************************************************************************/
119
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000120static struct irqaction pit_irq = {
Greg Ungerer2f2c2672007-10-23 14:37:54 +1000121 .name = "timer",
122 .flags = IRQF_DISABLED | IRQF_TIMER,
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000123 .handler = pit_tick,
Greg Ungerer5c4525d2007-07-27 01:09:00 +1000124};
125
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000126/***************************************************************************/
127
128static cycle_t pit_read_clk(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000130 unsigned long flags;
131 u32 cycles;
132 u16 pcntr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000134 local_irq_save(flags);
135 pcntr = __raw_readw(TA(MCFPIT_PCNTR));
136 cycles = pit_cnt;
137 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Sebastian Siewior2b9a6982008-04-28 11:43:04 +0200139 return cycles + PIT_CYCLES_PER_JIFFY - pcntr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142/***************************************************************************/
143
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000144static struct clocksource pit_clk = {
145 .name = "pit",
Sebastian Siewior2b9a6982008-04-28 11:43:04 +0200146 .rating = 100,
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000147 .read = pit_read_clk,
148 .shift = 20,
149 .mask = CLOCKSOURCE_MASK(32),
150 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
151};
152
153/***************************************************************************/
154
155void hw_timer_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000157 u32 imr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Rusty Russell320ab2b2008-12-13 21:20:26 +1030159 cf_pit_clockevent.cpumask = cpumask_of(smp_processor_id());
Sebastian Siewior2b9a6982008-04-28 11:43:04 +0200160 cf_pit_clockevent.mult = div_sc(FREQ, NSEC_PER_SEC, 32);
161 cf_pit_clockevent.max_delta_ns =
162 clockevent_delta2ns(0xFFFF, &cf_pit_clockevent);
163 cf_pit_clockevent.min_delta_ns =
164 clockevent_delta2ns(0x3f, &cf_pit_clockevent);
165 clockevents_register_device(&cf_pit_clockevent);
166
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000167 setup_irq(MCFINT_VECBASE + MCFINT_PIT1, &pit_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000169 __raw_writeb(ICR_INTRCONF, INTC0 + MCFINTC_ICR0 + MCFINT_PIT1);
170 imr = __raw_readl(INTC0 + MCFPIT_IMR);
171 imr &= ~MCFPIT_IMR_IBIT;
172 __raw_writel(imr, INTC0 + MCFPIT_IMR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Greg Ungerer8d80c5e2008-02-01 17:40:21 +1000174 pit_clk.mult = clocksource_hz2mult(FREQ, pit_clk.shift);
175 clocksource_register(&pit_clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178/***************************************************************************/