2 * Driver/API for AMD Geode Multi-Function General Purpose Timers (MFGPT)
4 * Copyright (C) 2006, Advanced Micro Devices, Inc.
5 * Copyright (C) 2007, Andres Salomon <dilinger@debian.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU General Public License
9 * as published by the Free Software Foundation.
11 * The MFGPTs are documented in AMD Geode CS5536 Companion Device Data Book.
15 * We are using the 32Khz input clock - its the only one that has the
16 * ranges we find desirable. The following table lists the suitable
17 * divisors and the associated hz, minimum interval
18 * and the maximum interval:
20 * Divisor Hz Min Delta (S) Max Delta (S)
28 * 128 250 .064 262.144
29 * 256 125 .128 524.288
32 #include <linux/kernel.h>
33 #include <linux/interrupt.h>
34 #include <linux/module.h>
35 #include <asm/geode.h>
39 static struct mfgpt_timer_t {
42 } mfgpt_timers[MFGPT_MAX_TIMERS];
44 /* Selected from the table above */
46 #define MFGPT_DIVISOR 16
47 #define MFGPT_SCALE 4 /* divisor = 2^(scale) */
48 #define MFGPT_HZ (32000 / MFGPT_DIVISOR)
49 #define MFGPT_PERIODIC (MFGPT_HZ / HZ)
51 #ifdef CONFIG_GEODE_MFGPT_TIMER
52 static int __init mfgpt_timer_setup(void);
54 #define mfgpt_timer_setup() (0)
57 /* Allow for disabling of MFGPTs */
59 static int __init mfgpt_disable(char *s)
64 __setup("nomfgpt", mfgpt_disable);
66 /* Reset the MFGPT timers. This is required by some broken BIOSes which already
67 * do the same and leave the system in an unstable state. TinyBIOS 0.98 is
68 * affected at least (0.99 is OK with MFGPT workaround left to off).
70 static int __init mfgpt_fix(char *s)
74 /* The following udocumented bit resets the MFGPT timers */
75 val = 0xFF; dummy = 0;
76 wrmsr(0x5140002B, val, dummy);
79 __setup("mfgptfix", mfgpt_fix);
82 * Check whether any MFGPTs are available for the kernel to use. In most
83 * cases, firmware that uses AMD's VSA code will claim all timers during
84 * bootup; we certainly don't want to take them if they're already in use.
85 * In other cases (such as with VSAless OpenFirmware), the system firmware
86 * leaves timers available for us to use.
88 int __init geode_mfgpt_detect(void)
94 printk(KERN_INFO "geode-mfgpt: Skipping MFGPT setup\n");
98 for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
99 val = geode_mfgpt_read(i, MFGPT_REG_SETUP);
100 if (!(val & MFGPT_SETUP_SETUP)) {
101 mfgpt_timers[i].flags = F_AVAIL;
106 /* set up clock event device, if desired */
107 i = mfgpt_timer_setup();
112 int geode_mfgpt_toggle_event(int timer, int cmp, int event, int enable)
114 u32 msr, mask, value, dummy;
115 int shift = (cmp == MFGPT_CMP1) ? 0 : 8;
117 if (timer < 0 || timer >= MFGPT_MAX_TIMERS)
121 * The register maps for these are described in sections 6.17.1.x of
122 * the AMD Geode CS5536 Companion Device Data Book.
125 case MFGPT_EVENT_RESET:
127 * XXX: According to the docs, we cannot reset timers above
128 * 6; that is, resets for 7 and 8 will be ignored. Is this
129 * a problem? -dilinger
132 mask = 1 << (timer + 24);
135 case MFGPT_EVENT_NMI:
137 mask = 1 << (timer + shift);
140 case MFGPT_EVENT_IRQ:
142 mask = 1 << (timer + shift);
149 rdmsr(msr, value, dummy);
156 wrmsr(msr, value, dummy);
160 int geode_mfgpt_set_irq(int timer, int cmp, int irq, int enable)
165 if (timer < 0 || timer >= MFGPT_MAX_TIMERS)
168 if (geode_mfgpt_toggle_event(timer, cmp, MFGPT_EVENT_IRQ, enable))
171 rdmsr(MSR_PIC_ZSEL_LOW, val, dummy);
173 offset = (timer % 4) * 4;
175 val &= ~((0xF << offset) | (0xF << (offset + 16)));
178 val |= (irq & 0x0F) << (offset);
179 val |= (irq & 0x0F) << (offset + 16);
182 wrmsr(MSR_PIC_ZSEL_LOW, val, dummy);
186 static int mfgpt_get(int timer, struct module *owner)
188 mfgpt_timers[timer].flags &= ~F_AVAIL;
189 mfgpt_timers[timer].owner = owner;
190 printk(KERN_INFO "geode-mfgpt: Registered timer %d\n", timer);
194 int geode_mfgpt_alloc_timer(int timer, int domain, struct module *owner)
198 if (!geode_get_dev_base(GEODE_DEV_MFGPT))
200 if (timer >= MFGPT_MAX_TIMERS)
204 /* Try to find an available timer */
205 for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
206 if (mfgpt_timers[i].flags & F_AVAIL)
207 return mfgpt_get(i, owner);
209 if (i == 5 && domain == MFGPT_DOMAIN_WORKING)
213 /* If they requested a specific timer, try to honor that */
214 if (mfgpt_timers[timer].flags & F_AVAIL)
215 return mfgpt_get(timer, owner);
218 /* No timers available - too bad */
223 #ifdef CONFIG_GEODE_MFGPT_TIMER
226 * The MFPGT timers on the CS5536 provide us with suitable timers to use
227 * as clock event sources - not as good as a HPET or APIC, but certainly
228 * better then the PIT. This isn't a general purpose MFGPT driver, but
229 * a simplified one designed specifically to act as a clock event source.
230 * For full details about the MFGPT, please consult the CS5536 data sheet.
233 #include <linux/clocksource.h>
234 #include <linux/clockchips.h>
236 static unsigned int mfgpt_tick_mode = CLOCK_EVT_MODE_SHUTDOWN;
237 static u16 mfgpt_event_clock;
240 static int __init mfgpt_setup(char *str)
242 get_option(&str, &irq);
245 __setup("mfgpt_irq=", mfgpt_setup);
247 static inline void mfgpt_disable_timer(u16 clock)
249 u16 val = geode_mfgpt_read(clock, MFGPT_REG_SETUP);
250 geode_mfgpt_write(clock, MFGPT_REG_SETUP, val & ~MFGPT_SETUP_CNTEN);
253 static int mfgpt_next_event(unsigned long, struct clock_event_device *);
254 static void mfgpt_set_mode(enum clock_event_mode, struct clock_event_device *);
256 static struct clock_event_device mfgpt_clockevent = {
257 .name = "mfgpt-timer",
258 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
259 .set_mode = mfgpt_set_mode,
260 .set_next_event = mfgpt_next_event,
262 .cpumask = CPU_MASK_ALL,
266 static inline void mfgpt_start_timer(u16 clock, u16 delta)
268 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_CMP2, (u16) delta);
269 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_COUNTER, 0);
271 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP,
272 MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
275 static void mfgpt_set_mode(enum clock_event_mode mode,
276 struct clock_event_device *evt)
278 mfgpt_disable_timer(mfgpt_event_clock);
280 if (mode == CLOCK_EVT_MODE_PERIODIC)
281 mfgpt_start_timer(mfgpt_event_clock, MFGPT_PERIODIC);
283 mfgpt_tick_mode = mode;
286 static int mfgpt_next_event(unsigned long delta, struct clock_event_device *evt)
288 mfgpt_start_timer(mfgpt_event_clock, delta);
292 /* Assume (foolishly?), that this interrupt was due to our tick */
294 static irqreturn_t mfgpt_tick(int irq, void *dev_id)
296 /* Turn off the clock (and clear the event) */
297 mfgpt_disable_timer(mfgpt_event_clock);
299 if (mfgpt_tick_mode == CLOCK_EVT_MODE_SHUTDOWN)
302 /* Clear the counter */
303 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_COUNTER, 0);
305 /* Restart the clock in periodic mode */
307 if (mfgpt_tick_mode == CLOCK_EVT_MODE_PERIODIC) {
308 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP,
309 MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
312 mfgpt_clockevent.event_handler(&mfgpt_clockevent);
316 static struct irqaction mfgptirq = {
317 .handler = mfgpt_tick,
318 .flags = IRQF_DISABLED | IRQF_NOBALANCING,
319 .mask = CPU_MASK_NONE,
320 .name = "mfgpt-timer"
323 static int __init mfgpt_timer_setup(void)
328 timer = geode_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING,
332 "mfgpt-timer: Could not allocate a MFPGT timer\n");
336 mfgpt_event_clock = timer;
338 /* Set up the IRQ on the MFGPT side */
339 if (geode_mfgpt_setup_irq(mfgpt_event_clock, MFGPT_CMP2, irq)) {
340 printk(KERN_ERR "mfgpt-timer: Could not set up IRQ %d\n", irq);
344 /* And register it with the kernel */
345 ret = setup_irq(irq, &mfgptirq);
349 "mfgpt-timer: Unable to set up the interrupt.\n");
353 /* Set the clock scale and enable the event mode for CMP2 */
354 val = MFGPT_SCALE | (3 << 8);
356 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP, val);
358 /* Set up the clock event */
359 mfgpt_clockevent.mult = div_sc(MFGPT_HZ, NSEC_PER_SEC, 32);
360 mfgpt_clockevent.min_delta_ns = clockevent_delta2ns(0xF,
362 mfgpt_clockevent.max_delta_ns = clockevent_delta2ns(0xFFFE,
366 "mfgpt-timer: registering the MFGT timer as a clock event.\n");
367 clockevents_register_device(&mfgpt_clockevent);
372 geode_mfgpt_release_irq(mfgpt_event_clock, MFGPT_CMP2, irq);
374 "mfgpt-timer: Unable to set up the MFGPT clock source\n");