blob: 586228140b9e942832b8cf1e8cf6eb233d1b84cf [file] [log] [blame]
Andres Salomon83d73842007-10-12 23:04:06 +02001/*
2 * Driver/API for AMD Geode Multi-Function General Purpose Timers (MFGPT)
3 *
4 * Copyright (C) 2006, Advanced Micro Devices, Inc.
5 * Copyright (C) 2007, Andres Salomon <dilinger@debian.org>
6 *
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.
10 *
11 * The MFGPTs are documented in AMD Geode CS5536 Companion Device Data Book.
12 */
13
14/*
Willy Tarreau36445cf32008-02-09 23:24:08 +010015 * We are using the 32.768kHz input clock - it's the only one that has the
Andres Salomon83d73842007-10-12 23:04:06 +020016 * ranges we find desirable. The following table lists the suitable
Willy Tarreau36445cf32008-02-09 23:24:08 +010017 * divisors and the associated Hz, minimum interval and the maximum interval:
Andres Salomon83d73842007-10-12 23:04:06 +020018 *
Willy Tarreau36445cf32008-02-09 23:24:08 +010019 * Divisor Hz Min Delta (s) Max Delta (s)
20 * 1 32768 .00048828125 2.000
21 * 2 16384 .0009765625 4.000
22 * 4 8192 .001953125 8.000
23 * 8 4096 .00390625 16.000
24 * 16 2048 .0078125 32.000
25 * 32 1024 .015625 64.000
26 * 64 512 .03125 128.000
27 * 128 256 .0625 256.000
28 * 256 128 .125 512.000
Andres Salomon83d73842007-10-12 23:04:06 +020029 */
30
31#include <linux/kernel.h>
32#include <linux/interrupt.h>
33#include <linux/module.h>
34#include <asm/geode.h>
35
36#define F_AVAIL 0x01
37
38static struct mfgpt_timer_t {
39 int flags;
40 struct module *owner;
41} mfgpt_timers[MFGPT_MAX_TIMERS];
42
43/* Selected from the table above */
44
45#define MFGPT_DIVISOR 16
46#define MFGPT_SCALE 4 /* divisor = 2^(scale) */
Willy Tarreau36445cf32008-02-09 23:24:08 +010047#define MFGPT_HZ (32768 / MFGPT_DIVISOR)
Andres Salomon83d73842007-10-12 23:04:06 +020048#define MFGPT_PERIODIC (MFGPT_HZ / HZ)
49
Andres Salomon8f368812007-10-12 23:04:06 +020050#ifdef CONFIG_GEODE_MFGPT_TIMER
51static int __init mfgpt_timer_setup(void);
52#else
53#define mfgpt_timer_setup() (0)
54#endif
55
Andres Salomon83d73842007-10-12 23:04:06 +020056/* Allow for disabling of MFGPTs */
57static int disable;
58static int __init mfgpt_disable(char *s)
59{
60 disable = 1;
61 return 1;
62}
63__setup("nomfgpt", mfgpt_disable);
64
Willy Tarreaue6c4dc62008-01-30 13:33:33 +010065/* Reset the MFGPT timers. This is required by some broken BIOSes which already
66 * do the same and leave the system in an unstable state. TinyBIOS 0.98 is
67 * affected at least (0.99 is OK with MFGPT workaround left to off).
68 */
69static int __init mfgpt_fix(char *s)
70{
71 u32 val, dummy;
72
73 /* The following udocumented bit resets the MFGPT timers */
74 val = 0xFF; dummy = 0;
75 wrmsr(0x5140002B, val, dummy);
76 return 1;
77}
78__setup("mfgptfix", mfgpt_fix);
79
Andres Salomon83d73842007-10-12 23:04:06 +020080/*
81 * Check whether any MFGPTs are available for the kernel to use. In most
82 * cases, firmware that uses AMD's VSA code will claim all timers during
83 * bootup; we certainly don't want to take them if they're already in use.
84 * In other cases (such as with VSAless OpenFirmware), the system firmware
85 * leaves timers available for us to use.
86 */
87int __init geode_mfgpt_detect(void)
88{
89 int count = 0, i;
90 u16 val;
91
92 if (disable) {
93 printk(KERN_INFO "geode-mfgpt: Skipping MFGPT setup\n");
94 return 0;
95 }
96
97 for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
98 val = geode_mfgpt_read(i, MFGPT_REG_SETUP);
99 if (!(val & MFGPT_SETUP_SETUP)) {
100 mfgpt_timers[i].flags = F_AVAIL;
101 count++;
102 }
103 }
104
Andres Salomon8f368812007-10-12 23:04:06 +0200105 /* set up clock event device, if desired */
106 i = mfgpt_timer_setup();
107
Andres Salomon83d73842007-10-12 23:04:06 +0200108 return count;
109}
110
111int geode_mfgpt_toggle_event(int timer, int cmp, int event, int enable)
112{
113 u32 msr, mask, value, dummy;
114 int shift = (cmp == MFGPT_CMP1) ? 0 : 8;
115
116 if (timer < 0 || timer >= MFGPT_MAX_TIMERS)
117 return -EIO;
118
119 /*
120 * The register maps for these are described in sections 6.17.1.x of
121 * the AMD Geode CS5536 Companion Device Data Book.
122 */
123 switch (event) {
124 case MFGPT_EVENT_RESET:
125 /*
126 * XXX: According to the docs, we cannot reset timers above
127 * 6; that is, resets for 7 and 8 will be ignored. Is this
128 * a problem? -dilinger
129 */
130 msr = MFGPT_NR_MSR;
131 mask = 1 << (timer + 24);
132 break;
133
134 case MFGPT_EVENT_NMI:
135 msr = MFGPT_NR_MSR;
136 mask = 1 << (timer + shift);
137 break;
138
139 case MFGPT_EVENT_IRQ:
140 msr = MFGPT_IRQ_MSR;
141 mask = 1 << (timer + shift);
142 break;
143
144 default:
145 return -EIO;
146 }
147
148 rdmsr(msr, value, dummy);
149
150 if (enable)
151 value |= mask;
152 else
153 value &= ~mask;
154
155 wrmsr(msr, value, dummy);
156 return 0;
157}
158
159int geode_mfgpt_set_irq(int timer, int cmp, int irq, int enable)
160{
161 u32 val, dummy;
162 int offset;
163
164 if (timer < 0 || timer >= MFGPT_MAX_TIMERS)
165 return -EIO;
166
167 if (geode_mfgpt_toggle_event(timer, cmp, MFGPT_EVENT_IRQ, enable))
168 return -EIO;
169
170 rdmsr(MSR_PIC_ZSEL_LOW, val, dummy);
171
172 offset = (timer % 4) * 4;
173
174 val &= ~((0xF << offset) | (0xF << (offset + 16)));
175
176 if (enable) {
177 val |= (irq & 0x0F) << (offset);
178 val |= (irq & 0x0F) << (offset + 16);
179 }
180
181 wrmsr(MSR_PIC_ZSEL_LOW, val, dummy);
182 return 0;
183}
184
185static int mfgpt_get(int timer, struct module *owner)
186{
187 mfgpt_timers[timer].flags &= ~F_AVAIL;
188 mfgpt_timers[timer].owner = owner;
189 printk(KERN_INFO "geode-mfgpt: Registered timer %d\n", timer);
190 return timer;
191}
192
193int geode_mfgpt_alloc_timer(int timer, int domain, struct module *owner)
194{
195 int i;
196
197 if (!geode_get_dev_base(GEODE_DEV_MFGPT))
198 return -ENODEV;
199 if (timer >= MFGPT_MAX_TIMERS)
200 return -EIO;
201
202 if (timer < 0) {
203 /* Try to find an available timer */
204 for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
205 if (mfgpt_timers[i].flags & F_AVAIL)
206 return mfgpt_get(i, owner);
207
208 if (i == 5 && domain == MFGPT_DOMAIN_WORKING)
209 break;
210 }
211 } else {
212 /* If they requested a specific timer, try to honor that */
213 if (mfgpt_timers[timer].flags & F_AVAIL)
214 return mfgpt_get(timer, owner);
215 }
216
217 /* No timers available - too bad */
218 return -1;
219}
220
Andres Salomon8f368812007-10-12 23:04:06 +0200221
222#ifdef CONFIG_GEODE_MFGPT_TIMER
223
224/*
225 * The MFPGT timers on the CS5536 provide us with suitable timers to use
226 * as clock event sources - not as good as a HPET or APIC, but certainly
227 * better then the PIT. This isn't a general purpose MFGPT driver, but
228 * a simplified one designed specifically to act as a clock event source.
229 * For full details about the MFGPT, please consult the CS5536 data sheet.
230 */
231
232#include <linux/clocksource.h>
233#include <linux/clockchips.h>
234
235static unsigned int mfgpt_tick_mode = CLOCK_EVT_MODE_SHUTDOWN;
236static u16 mfgpt_event_clock;
237
238static int irq = 7;
239static int __init mfgpt_setup(char *str)
240{
241 get_option(&str, &irq);
242 return 1;
243}
244__setup("mfgpt_irq=", mfgpt_setup);
245
Andres Salomone78a77c2008-02-09 23:24:08 +0100246static void mfgpt_disable_timer(u16 clock)
Andres Salomon8f368812007-10-12 23:04:06 +0200247{
248 u16 val = geode_mfgpt_read(clock, MFGPT_REG_SETUP);
249 geode_mfgpt_write(clock, MFGPT_REG_SETUP, val & ~MFGPT_SETUP_CNTEN);
250}
251
252static int mfgpt_next_event(unsigned long, struct clock_event_device *);
253static void mfgpt_set_mode(enum clock_event_mode, struct clock_event_device *);
254
255static struct clock_event_device mfgpt_clockevent = {
256 .name = "mfgpt-timer",
257 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
258 .set_mode = mfgpt_set_mode,
259 .set_next_event = mfgpt_next_event,
260 .rating = 250,
261 .cpumask = CPU_MASK_ALL,
262 .shift = 32
263};
264
Andres Salomone78a77c2008-02-09 23:24:08 +0100265static void mfgpt_start_timer(u16 delta)
Andres Salomon8f368812007-10-12 23:04:06 +0200266{
267 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_CMP2, (u16) delta);
268 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_COUNTER, 0);
269
270 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP,
271 MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
272}
273
274static void mfgpt_set_mode(enum clock_event_mode mode,
275 struct clock_event_device *evt)
276{
277 mfgpt_disable_timer(mfgpt_event_clock);
278
279 if (mode == CLOCK_EVT_MODE_PERIODIC)
Andres Salomone78a77c2008-02-09 23:24:08 +0100280 mfgpt_start_timer(MFGPT_PERIODIC);
Andres Salomon8f368812007-10-12 23:04:06 +0200281
282 mfgpt_tick_mode = mode;
283}
284
285static int mfgpt_next_event(unsigned long delta, struct clock_event_device *evt)
286{
Andres Salomone78a77c2008-02-09 23:24:08 +0100287 mfgpt_start_timer(delta);
Andres Salomon8f368812007-10-12 23:04:06 +0200288 return 0;
289}
290
291/* Assume (foolishly?), that this interrupt was due to our tick */
292
293static irqreturn_t mfgpt_tick(int irq, void *dev_id)
294{
Jordan Crouse667984d2008-01-22 23:30:16 +0100295 /* Turn off the clock (and clear the event) */
296 mfgpt_disable_timer(mfgpt_event_clock);
297
Andres Salomon8f368812007-10-12 23:04:06 +0200298 if (mfgpt_tick_mode == CLOCK_EVT_MODE_SHUTDOWN)
299 return IRQ_HANDLED;
300
Andres Salomon8f368812007-10-12 23:04:06 +0200301 /* Clear the counter */
302 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_COUNTER, 0);
303
304 /* Restart the clock in periodic mode */
305
306 if (mfgpt_tick_mode == CLOCK_EVT_MODE_PERIODIC) {
307 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP,
308 MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
309 }
310
311 mfgpt_clockevent.event_handler(&mfgpt_clockevent);
312 return IRQ_HANDLED;
313}
314
315static struct irqaction mfgptirq = {
316 .handler = mfgpt_tick,
317 .flags = IRQF_DISABLED | IRQF_NOBALANCING,
318 .mask = CPU_MASK_NONE,
319 .name = "mfgpt-timer"
320};
321
322static int __init mfgpt_timer_setup(void)
323{
324 int timer, ret;
325 u16 val;
326
327 timer = geode_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING,
328 THIS_MODULE);
329 if (timer < 0) {
330 printk(KERN_ERR
331 "mfgpt-timer: Could not allocate a MFPGT timer\n");
332 return -ENODEV;
333 }
334
335 mfgpt_event_clock = timer;
Andres Salomon8f368812007-10-12 23:04:06 +0200336
337 /* Set up the IRQ on the MFGPT side */
338 if (geode_mfgpt_setup_irq(mfgpt_event_clock, MFGPT_CMP2, irq)) {
339 printk(KERN_ERR "mfgpt-timer: Could not set up IRQ %d\n", irq);
340 return -EIO;
341 }
342
343 /* And register it with the kernel */
344 ret = setup_irq(irq, &mfgptirq);
345
346 if (ret) {
347 printk(KERN_ERR
348 "mfgpt-timer: Unable to set up the interrupt.\n");
349 goto err;
350 }
351
Jordan Crouse667984d2008-01-22 23:30:16 +0100352 /* Set the clock scale and enable the event mode for CMP2 */
353 val = MFGPT_SCALE | (3 << 8);
354
355 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP, val);
356
Andres Salomon8f368812007-10-12 23:04:06 +0200357 /* Set up the clock event */
358 mfgpt_clockevent.mult = div_sc(MFGPT_HZ, NSEC_PER_SEC, 32);
359 mfgpt_clockevent.min_delta_ns = clockevent_delta2ns(0xF,
360 &mfgpt_clockevent);
361 mfgpt_clockevent.max_delta_ns = clockevent_delta2ns(0xFFFE,
362 &mfgpt_clockevent);
363
364 printk(KERN_INFO
365 "mfgpt-timer: registering the MFGT timer as a clock event.\n");
366 clockevents_register_device(&mfgpt_clockevent);
367
368 return 0;
369
370err:
371 geode_mfgpt_release_irq(mfgpt_event_clock, MFGPT_CMP2, irq);
372 printk(KERN_ERR
373 "mfgpt-timer: Unable to set up the MFGPT clock source\n");
374 return -EIO;
375}
376
377#endif