2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Based on linux/arch/mips/kernel/cevt-r4k.c,
7 * linux/arch/mips/jmr3927/rbhma3100/setup.c
9 * Copyright 2001 MontaVista Software Inc.
10 * Copyright (C) 2000-2001 Toshiba Corporation
11 * Copyright (C) 2007 MIPS Technologies, Inc.
12 * Copyright (C) 2007 Ralf Baechle <ralf@linux-mips.org>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
17 #include <asm/txx9tmr.h>
19 #define TCR_BASE (TXx9_TMTCR_CCDE | TXx9_TMTCR_CRE | TXx9_TMTCR_TMODE_ITVL)
20 #define TIMER_CCD 0 /* 1/2 */
21 #define TIMER_CLK(imclk) ((imclk) / (2 << TIMER_CCD))
23 struct txx9_clocksource {
24 struct clocksource cs;
25 struct txx9_tmr_reg __iomem *tmrptr;
28 static cycle_t txx9_cs_read(struct clocksource *cs)
30 struct txx9_clocksource *txx9_cs =
31 container_of(cs, struct txx9_clocksource, cs);
32 return __raw_readl(&txx9_cs->tmrptr->trr);
35 /* Use 1 bit smaller width to use full bits in that width */
36 #define TXX9_CLOCKSOURCE_BITS (TXX9_TIMER_BITS - 1)
38 static struct txx9_clocksource txx9_clocksource = {
43 .mask = CLOCKSOURCE_MASK(TXX9_CLOCKSOURCE_BITS),
44 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
48 void __init txx9_clocksource_init(unsigned long baseaddr,
49 unsigned int imbusclk)
51 struct txx9_tmr_reg __iomem *tmrptr;
53 clocksource_set_clock(&txx9_clocksource.cs, TIMER_CLK(imbusclk));
54 clocksource_register(&txx9_clocksource.cs);
56 tmrptr = ioremap(baseaddr, sizeof(struct txx9_tmr_reg));
57 __raw_writel(TCR_BASE, &tmrptr->tcr);
58 __raw_writel(0, &tmrptr->tisr);
59 __raw_writel(TIMER_CCD, &tmrptr->ccdr);
60 __raw_writel(TXx9_TMITMR_TZCE, &tmrptr->itmr);
61 __raw_writel(1 << TXX9_CLOCKSOURCE_BITS, &tmrptr->cpra);
62 __raw_writel(TCR_BASE | TXx9_TMTCR_TCE, &tmrptr->tcr);
63 txx9_clocksource.tmrptr = tmrptr;
66 struct txx9_clock_event_device {
67 struct clock_event_device cd;
68 struct txx9_tmr_reg __iomem *tmrptr;
71 static void txx9tmr_stop_and_clear(struct txx9_tmr_reg __iomem *tmrptr)
73 /* stop and reset counter */
74 __raw_writel(TCR_BASE, &tmrptr->tcr);
75 /* clear pending interrupt */
76 __raw_writel(0, &tmrptr->tisr);
79 static void txx9tmr_set_mode(enum clock_event_mode mode,
80 struct clock_event_device *evt)
82 struct txx9_clock_event_device *txx9_cd =
83 container_of(evt, struct txx9_clock_event_device, cd);
84 struct txx9_tmr_reg __iomem *tmrptr = txx9_cd->tmrptr;
86 txx9tmr_stop_and_clear(tmrptr);
88 case CLOCK_EVT_MODE_PERIODIC:
89 __raw_writel(TXx9_TMITMR_TIIE | TXx9_TMITMR_TZCE,
92 __raw_writel(((u64)(NSEC_PER_SEC / HZ) * evt->mult) >>
95 __raw_writel(TCR_BASE | TXx9_TMTCR_TCE, &tmrptr->tcr);
97 case CLOCK_EVT_MODE_SHUTDOWN:
98 case CLOCK_EVT_MODE_UNUSED:
99 __raw_writel(0, &tmrptr->itmr);
101 case CLOCK_EVT_MODE_ONESHOT:
102 __raw_writel(TXx9_TMITMR_TIIE, &tmrptr->itmr);
104 case CLOCK_EVT_MODE_RESUME:
105 __raw_writel(TIMER_CCD, &tmrptr->ccdr);
106 __raw_writel(0, &tmrptr->itmr);
111 static int txx9tmr_set_next_event(unsigned long delta,
112 struct clock_event_device *evt)
114 struct txx9_clock_event_device *txx9_cd =
115 container_of(evt, struct txx9_clock_event_device, cd);
116 struct txx9_tmr_reg __iomem *tmrptr = txx9_cd->tmrptr;
118 txx9tmr_stop_and_clear(tmrptr);
120 __raw_writel(delta, &tmrptr->cpra);
121 __raw_writel(TCR_BASE | TXx9_TMTCR_TCE, &tmrptr->tcr);
125 static struct txx9_clock_event_device txx9_clock_event_device = {
128 .features = CLOCK_EVT_FEAT_PERIODIC |
129 CLOCK_EVT_FEAT_ONESHOT,
131 .set_mode = txx9tmr_set_mode,
132 .set_next_event = txx9tmr_set_next_event,
136 static irqreturn_t txx9tmr_interrupt(int irq, void *dev_id)
138 struct txx9_clock_event_device *txx9_cd = dev_id;
139 struct clock_event_device *cd = &txx9_cd->cd;
140 struct txx9_tmr_reg __iomem *tmrptr = txx9_cd->tmrptr;
142 __raw_writel(0, &tmrptr->tisr); /* ack interrupt */
143 cd->event_handler(cd);
147 static struct irqaction txx9tmr_irq = {
148 .handler = txx9tmr_interrupt,
149 .flags = IRQF_DISABLED | IRQF_PERCPU,
151 .dev_id = &txx9_clock_event_device,
154 void __init txx9_clockevent_init(unsigned long baseaddr, int irq,
155 unsigned int imbusclk)
157 struct clock_event_device *cd = &txx9_clock_event_device.cd;
158 struct txx9_tmr_reg __iomem *tmrptr;
160 tmrptr = ioremap(baseaddr, sizeof(struct txx9_tmr_reg));
161 txx9tmr_stop_and_clear(tmrptr);
162 __raw_writel(TIMER_CCD, &tmrptr->ccdr);
163 __raw_writel(0, &tmrptr->itmr);
164 txx9_clock_event_device.tmrptr = tmrptr;
166 clockevent_set_clock(cd, TIMER_CLK(imbusclk));
168 clockevent_delta2ns(0xffffffff >> (32 - TXX9_TIMER_BITS), cd);
169 cd->min_delta_ns = clockevent_delta2ns(0xf, cd);
171 cd->cpumask = cpumask_of(0),
172 clockevents_register_device(cd);
173 setup_irq(irq, &txx9tmr_irq);
174 printk(KERN_INFO "TXx9: clockevent device at 0x%lx, irq %d\n",
178 void __init txx9_tmr_init(unsigned long baseaddr)
180 struct txx9_tmr_reg __iomem *tmrptr;
182 tmrptr = ioremap(baseaddr, sizeof(struct txx9_tmr_reg));
183 /* Start once to make CounterResetEnable effective */
184 __raw_writel(TXx9_TMTCR_CRE | TXx9_TMTCR_TCE, &tmrptr->tcr);
185 /* Stop and reset the counter */
186 __raw_writel(TXx9_TMTCR_CRE, &tmrptr->tcr);
187 __raw_writel(0, &tmrptr->tisr);
188 __raw_writel(0xffffffff, &tmrptr->cpra);
189 __raw_writel(0, &tmrptr->itmr);
190 __raw_writel(0, &tmrptr->ccdr);
191 __raw_writel(0, &tmrptr->pgmr);