1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019 Linus Walleij <linus.walleij@linaro.org>
6 * Based on arch/arm/mach-ixp4xx/common.c
7 * Copyright 2002 (C) Intel Corporation
8 * Copyright 2003-2004 (C) MontaVista, Software, Inc.
9 * Copyright (C) Deepak Saxena <dsaxena@plexity.net>
11 #include <linux/interrupt.h>
13 #include <linux/clockchips.h>
14 #include <linux/clocksource.h>
15 #include <linux/sched_clock.h>
16 #include <linux/slab.h>
17 #include <linux/bitops.h>
18 #include <linux/delay.h>
19 #include <linux/of_address.h>
20 #include <linux/of_irq.h>
21 #include <linux/platform_device.h>
22 /* Goes away with OF conversion */
23 #include <linux/platform_data/timer-ixp4xx.h>
26 * Constants to make it easy to access Timer Control/Status registers
28 #define IXP4XX_OSTS_OFFSET 0x00 /* Continuous Timestamp */
29 #define IXP4XX_OST1_OFFSET 0x04 /* Timer 1 Timestamp */
30 #define IXP4XX_OSRT1_OFFSET 0x08 /* Timer 1 Reload */
31 #define IXP4XX_OST2_OFFSET 0x0C /* Timer 2 Timestamp */
32 #define IXP4XX_OSRT2_OFFSET 0x10 /* Timer 2 Reload */
33 #define IXP4XX_OSST_OFFSET 0x20 /* Timer Status */
36 * Timer register values and bit definitions
38 #define IXP4XX_OST_ENABLE 0x00000001
39 #define IXP4XX_OST_ONE_SHOT 0x00000002
40 /* Low order bits of reload value ignored */
41 #define IXP4XX_OST_RELOAD_MASK 0x00000003
42 #define IXP4XX_OST_DISABLED 0x00000000
43 #define IXP4XX_OSST_TIMER_1_PEND 0x00000001
44 #define IXP4XX_OSST_TIMER_2_PEND 0x00000002
45 #define IXP4XX_OSST_TIMER_TS_PEND 0x00000004
46 /* Remaining registers are for the watchdog and defined in the watchdog driver */
51 struct clock_event_device clkevt;
53 struct delay_timer delay_timer;
58 * A local singleton used by sched_clock and delay timer reads, which are
61 static struct ixp4xx_timer *local_ixp4xx_timer;
63 static inline struct ixp4xx_timer *
64 to_ixp4xx_timer(struct clock_event_device *evt)
66 return container_of(evt, struct ixp4xx_timer, clkevt);
69 static unsigned long ixp4xx_read_timer(void)
71 return __raw_readl(local_ixp4xx_timer->base + IXP4XX_OSTS_OFFSET);
74 static u64 notrace ixp4xx_read_sched_clock(void)
76 return ixp4xx_read_timer();
79 static u64 ixp4xx_clocksource_read(struct clocksource *c)
81 return ixp4xx_read_timer();
84 static irqreturn_t ixp4xx_timer_interrupt(int irq, void *dev_id)
86 struct ixp4xx_timer *tmr = dev_id;
87 struct clock_event_device *evt = &tmr->clkevt;
89 /* Clear Pending Interrupt */
90 __raw_writel(IXP4XX_OSST_TIMER_1_PEND,
91 tmr->base + IXP4XX_OSST_OFFSET);
93 evt->event_handler(evt);
98 static int ixp4xx_set_next_event(unsigned long cycles,
99 struct clock_event_device *evt)
101 struct ixp4xx_timer *tmr = to_ixp4xx_timer(evt);
104 val = __raw_readl(tmr->base + IXP4XX_OSRT1_OFFSET);
105 /* Keep enable/oneshot bits */
106 val &= IXP4XX_OST_RELOAD_MASK;
107 __raw_writel((cycles & ~IXP4XX_OST_RELOAD_MASK) | val,
108 tmr->base + IXP4XX_OSRT1_OFFSET);
113 static int ixp4xx_shutdown(struct clock_event_device *evt)
115 struct ixp4xx_timer *tmr = to_ixp4xx_timer(evt);
118 val = __raw_readl(tmr->base + IXP4XX_OSRT1_OFFSET);
119 val &= ~IXP4XX_OST_ENABLE;
120 __raw_writel(val, tmr->base + IXP4XX_OSRT1_OFFSET);
125 static int ixp4xx_set_oneshot(struct clock_event_device *evt)
127 struct ixp4xx_timer *tmr = to_ixp4xx_timer(evt);
129 __raw_writel(IXP4XX_OST_ENABLE | IXP4XX_OST_ONE_SHOT,
130 tmr->base + IXP4XX_OSRT1_OFFSET);
135 static int ixp4xx_set_periodic(struct clock_event_device *evt)
137 struct ixp4xx_timer *tmr = to_ixp4xx_timer(evt);
140 val = tmr->latch & ~IXP4XX_OST_RELOAD_MASK;
141 val |= IXP4XX_OST_ENABLE;
142 __raw_writel(val, tmr->base + IXP4XX_OSRT1_OFFSET);
147 static int ixp4xx_resume(struct clock_event_device *evt)
149 struct ixp4xx_timer *tmr = to_ixp4xx_timer(evt);
152 val = __raw_readl(tmr->base + IXP4XX_OSRT1_OFFSET);
153 val |= IXP4XX_OST_ENABLE;
154 __raw_writel(val, tmr->base + IXP4XX_OSRT1_OFFSET);
161 * We use OS timer1 on the CPU for the timer tick and the timestamp
162 * counter as a source of real clock ticks to account for missed jiffies.
164 static __init int ixp4xx_timer_register(void __iomem *base,
166 unsigned int timer_freq)
168 struct ixp4xx_timer *tmr;
171 tmr = kzalloc(sizeof(*tmr), GFP_KERNEL);
177 * The timer register doesn't allow to specify the two least
178 * significant bits of the timeout value and assumes them being zero.
179 * So make sure the latch is the best value with the two least
180 * significant bits unset.
182 tmr->latch = DIV_ROUND_CLOSEST(timer_freq,
183 (IXP4XX_OST_RELOAD_MASK + 1) * HZ)
184 * (IXP4XX_OST_RELOAD_MASK + 1);
186 local_ixp4xx_timer = tmr;
188 /* Reset/disable counter */
189 __raw_writel(0, tmr->base + IXP4XX_OSRT1_OFFSET);
191 /* Clear any pending interrupt on timer 1 */
192 __raw_writel(IXP4XX_OSST_TIMER_1_PEND,
193 tmr->base + IXP4XX_OSST_OFFSET);
195 /* Reset time-stamp counter */
196 __raw_writel(0, tmr->base + IXP4XX_OSTS_OFFSET);
198 clocksource_mmio_init(NULL, "OSTS", timer_freq, 200, 32,
199 ixp4xx_clocksource_read);
201 tmr->clkevt.name = "ixp4xx timer1";
202 tmr->clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
203 tmr->clkevt.rating = 200;
204 tmr->clkevt.set_state_shutdown = ixp4xx_shutdown;
205 tmr->clkevt.set_state_periodic = ixp4xx_set_periodic;
206 tmr->clkevt.set_state_oneshot = ixp4xx_set_oneshot;
207 tmr->clkevt.tick_resume = ixp4xx_resume;
208 tmr->clkevt.set_next_event = ixp4xx_set_next_event;
209 tmr->clkevt.cpumask = cpumask_of(0);
210 tmr->clkevt.irq = timer_irq;
211 ret = request_irq(timer_irq, ixp4xx_timer_interrupt,
212 IRQF_TIMER, "IXP4XX-TIMER1", tmr);
214 pr_crit("no timer IRQ\n");
217 clockevents_config_and_register(&tmr->clkevt, timer_freq,
220 sched_clock_register(ixp4xx_read_sched_clock, 32, timer_freq);
223 /* Also use this timer for delays */
224 tmr->delay_timer.read_current_timer = ixp4xx_read_timer;
225 tmr->delay_timer.freq = timer_freq;
226 register_current_timer_delay(&tmr->delay_timer);
232 static struct platform_device ixp4xx_watchdog_device = {
233 .name = "ixp4xx-watchdog",
238 * This probe gets called after the timer is already up and running. The main
239 * function on this platform is to spawn the watchdog device as a child.
241 static int ixp4xx_timer_probe(struct platform_device *pdev)
243 struct device *dev = &pdev->dev;
245 /* Pass the base address as platform data and nothing else */
246 ixp4xx_watchdog_device.dev.platform_data = local_ixp4xx_timer->base;
247 ixp4xx_watchdog_device.dev.parent = dev;
248 return platform_device_register(&ixp4xx_watchdog_device);
251 static const struct of_device_id ixp4xx_timer_dt_id[] = {
252 { .compatible = "intel,ixp4xx-timer", },
256 static struct platform_driver ixp4xx_timer_driver = {
257 .probe = ixp4xx_timer_probe,
259 .name = "ixp4xx-timer",
260 .of_match_table = ixp4xx_timer_dt_id,
261 .suppress_bind_attrs = true,
264 builtin_platform_driver(ixp4xx_timer_driver);
267 * ixp4xx_timer_setup() - Timer setup function to be called from boardfiles
268 * @timerbase: physical base of timer block
269 * @timer_irq: Linux IRQ number for the timer
270 * @timer_freq: Fixed frequency of the timer
272 void __init ixp4xx_timer_setup(resource_size_t timerbase,
274 unsigned int timer_freq)
278 base = ioremap(timerbase, 0x100);
280 pr_crit("IXP4xx: can't remap timer\n");
283 ixp4xx_timer_register(base, timer_irq, timer_freq);
287 static __init int ixp4xx_of_timer_init(struct device_node *np)
293 base = of_iomap(np, 0);
295 pr_crit("IXP4xx: can't remap timer\n");
299 irq = irq_of_parse_and_map(np, 0);
301 pr_err("Can't parse IRQ\n");
306 /* TODO: get some fixed clocks into the device tree */
307 ret = ixp4xx_timer_register(base, irq, 66666000);
316 TIMER_OF_DECLARE(ixp4xx, "intel,ixp4xx-timer", ixp4xx_of_timer_init);