blob: 5cf3a839530ceffff392c98ce9418ddc81cd538e [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>
Andres Salomon83d73842007-10-12 23:04:06 +020033#include <asm/geode.h>
34
Andres Salomon83d73842007-10-12 23:04:06 +020035static struct mfgpt_timer_t {
Andres Salomon9501b2e2008-02-09 23:24:08 +010036 unsigned int avail:1;
Andres Salomon83d73842007-10-12 23:04:06 +020037} mfgpt_timers[MFGPT_MAX_TIMERS];
38
39/* Selected from the table above */
40
41#define MFGPT_DIVISOR 16
42#define MFGPT_SCALE 4 /* divisor = 2^(scale) */
Willy Tarreau36445cf32008-02-09 23:24:08 +010043#define MFGPT_HZ (32768 / MFGPT_DIVISOR)
Andres Salomon83d73842007-10-12 23:04:06 +020044#define MFGPT_PERIODIC (MFGPT_HZ / HZ)
45
46/* Allow for disabling of MFGPTs */
47static int disable;
48static int __init mfgpt_disable(char *s)
49{
50 disable = 1;
51 return 1;
52}
53__setup("nomfgpt", mfgpt_disable);
54
Willy Tarreaue6c4dc62008-01-30 13:33:33 +010055/* Reset the MFGPT timers. This is required by some broken BIOSes which already
56 * do the same and leave the system in an unstable state. TinyBIOS 0.98 is
57 * affected at least (0.99 is OK with MFGPT workaround left to off).
58 */
59static int __init mfgpt_fix(char *s)
60{
61 u32 val, dummy;
62
63 /* The following udocumented bit resets the MFGPT timers */
64 val = 0xFF; dummy = 0;
65 wrmsr(0x5140002B, val, dummy);
66 return 1;
67}
68__setup("mfgptfix", mfgpt_fix);
69
Andres Salomon83d73842007-10-12 23:04:06 +020070/*
71 * Check whether any MFGPTs are available for the kernel to use. In most
72 * cases, firmware that uses AMD's VSA code will claim all timers during
73 * bootup; we certainly don't want to take them if they're already in use.
74 * In other cases (such as with VSAless OpenFirmware), the system firmware
75 * leaves timers available for us to use.
76 */
77int __init geode_mfgpt_detect(void)
78{
79 int count = 0, i;
80 u16 val;
81
82 if (disable) {
83 printk(KERN_INFO "geode-mfgpt: Skipping MFGPT setup\n");
84 return 0;
85 }
86
87 for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
88 val = geode_mfgpt_read(i, MFGPT_REG_SETUP);
89 if (!(val & MFGPT_SETUP_SETUP)) {
Andres Salomon9501b2e2008-02-09 23:24:08 +010090 mfgpt_timers[i].avail = 1;
Andres Salomon83d73842007-10-12 23:04:06 +020091 count++;
92 }
93 }
94
Andres Salomon8f368812007-10-12 23:04:06 +020095 /* set up clock event device, if desired */
96 i = mfgpt_timer_setup();
97
Andres Salomon83d73842007-10-12 23:04:06 +020098 return count;
99}
100
101int geode_mfgpt_toggle_event(int timer, int cmp, int event, int enable)
102{
103 u32 msr, mask, value, dummy;
104 int shift = (cmp == MFGPT_CMP1) ? 0 : 8;
105
106 if (timer < 0 || timer >= MFGPT_MAX_TIMERS)
107 return -EIO;
108
109 /*
110 * The register maps for these are described in sections 6.17.1.x of
111 * the AMD Geode CS5536 Companion Device Data Book.
112 */
113 switch (event) {
114 case MFGPT_EVENT_RESET:
115 /*
116 * XXX: According to the docs, we cannot reset timers above
117 * 6; that is, resets for 7 and 8 will be ignored. Is this
118 * a problem? -dilinger
119 */
120 msr = MFGPT_NR_MSR;
121 mask = 1 << (timer + 24);
122 break;
123
124 case MFGPT_EVENT_NMI:
125 msr = MFGPT_NR_MSR;
126 mask = 1 << (timer + shift);
127 break;
128
129 case MFGPT_EVENT_IRQ:
130 msr = MFGPT_IRQ_MSR;
131 mask = 1 << (timer + shift);
132 break;
133
134 default:
135 return -EIO;
136 }
137
138 rdmsr(msr, value, dummy);
139
140 if (enable)
141 value |= mask;
142 else
143 value &= ~mask;
144
145 wrmsr(msr, value, dummy);
146 return 0;
147}
148
149int geode_mfgpt_set_irq(int timer, int cmp, int irq, int enable)
150{
151 u32 val, dummy;
152 int offset;
153
154 if (timer < 0 || timer >= MFGPT_MAX_TIMERS)
155 return -EIO;
156
157 if (geode_mfgpt_toggle_event(timer, cmp, MFGPT_EVENT_IRQ, enable))
158 return -EIO;
159
160 rdmsr(MSR_PIC_ZSEL_LOW, val, dummy);
161
162 offset = (timer % 4) * 4;
163
164 val &= ~((0xF << offset) | (0xF << (offset + 16)));
165
166 if (enable) {
167 val |= (irq & 0x0F) << (offset);
168 val |= (irq & 0x0F) << (offset + 16);
169 }
170
171 wrmsr(MSR_PIC_ZSEL_LOW, val, dummy);
172 return 0;
173}
174
Andres Salomonfa28e062008-02-09 23:24:08 +0100175static int mfgpt_get(int timer)
Andres Salomon83d73842007-10-12 23:04:06 +0200176{
Andres Salomon9501b2e2008-02-09 23:24:08 +0100177 mfgpt_timers[timer].avail = 0;
Andres Salomon83d73842007-10-12 23:04:06 +0200178 printk(KERN_INFO "geode-mfgpt: Registered timer %d\n", timer);
179 return timer;
180}
181
Andres Salomonfa28e062008-02-09 23:24:08 +0100182int geode_mfgpt_alloc_timer(int timer, int domain)
Andres Salomon83d73842007-10-12 23:04:06 +0200183{
184 int i;
185
186 if (!geode_get_dev_base(GEODE_DEV_MFGPT))
187 return -ENODEV;
188 if (timer >= MFGPT_MAX_TIMERS)
189 return -EIO;
190
191 if (timer < 0) {
192 /* Try to find an available timer */
193 for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
Andres Salomon9501b2e2008-02-09 23:24:08 +0100194 if (mfgpt_timers[i].avail)
Andres Salomonfa28e062008-02-09 23:24:08 +0100195 return mfgpt_get(i);
Andres Salomon83d73842007-10-12 23:04:06 +0200196
197 if (i == 5 && domain == MFGPT_DOMAIN_WORKING)
198 break;
199 }
200 } else {
201 /* If they requested a specific timer, try to honor that */
Andres Salomon9501b2e2008-02-09 23:24:08 +0100202 if (mfgpt_timers[timer].avail)
Andres Salomonfa28e062008-02-09 23:24:08 +0100203 return mfgpt_get(timer);
Andres Salomon83d73842007-10-12 23:04:06 +0200204 }
205
206 /* No timers available - too bad */
207 return -1;
208}
209
Andres Salomon8f368812007-10-12 23:04:06 +0200210
211#ifdef CONFIG_GEODE_MFGPT_TIMER
212
213/*
214 * The MFPGT timers on the CS5536 provide us with suitable timers to use
215 * as clock event sources - not as good as a HPET or APIC, but certainly
216 * better then the PIT. This isn't a general purpose MFGPT driver, but
217 * a simplified one designed specifically to act as a clock event source.
218 * For full details about the MFGPT, please consult the CS5536 data sheet.
219 */
220
221#include <linux/clocksource.h>
222#include <linux/clockchips.h>
223
224static unsigned int mfgpt_tick_mode = CLOCK_EVT_MODE_SHUTDOWN;
225static u16 mfgpt_event_clock;
226
227static int irq = 7;
228static int __init mfgpt_setup(char *str)
229{
230 get_option(&str, &irq);
231 return 1;
232}
233__setup("mfgpt_irq=", mfgpt_setup);
234
Andres Salomone78a77c2008-02-09 23:24:08 +0100235static void mfgpt_disable_timer(u16 clock)
Andres Salomon8f368812007-10-12 23:04:06 +0200236{
237 u16 val = geode_mfgpt_read(clock, MFGPT_REG_SETUP);
238 geode_mfgpt_write(clock, MFGPT_REG_SETUP, val & ~MFGPT_SETUP_CNTEN);
239}
240
241static int mfgpt_next_event(unsigned long, struct clock_event_device *);
242static void mfgpt_set_mode(enum clock_event_mode, struct clock_event_device *);
243
244static struct clock_event_device mfgpt_clockevent = {
245 .name = "mfgpt-timer",
246 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
247 .set_mode = mfgpt_set_mode,
248 .set_next_event = mfgpt_next_event,
249 .rating = 250,
250 .cpumask = CPU_MASK_ALL,
251 .shift = 32
252};
253
Andres Salomone78a77c2008-02-09 23:24:08 +0100254static void mfgpt_start_timer(u16 delta)
Andres Salomon8f368812007-10-12 23:04:06 +0200255{
256 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_CMP2, (u16) delta);
257 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_COUNTER, 0);
258
259 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP,
260 MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
261}
262
263static void mfgpt_set_mode(enum clock_event_mode mode,
264 struct clock_event_device *evt)
265{
266 mfgpt_disable_timer(mfgpt_event_clock);
267
268 if (mode == CLOCK_EVT_MODE_PERIODIC)
Andres Salomone78a77c2008-02-09 23:24:08 +0100269 mfgpt_start_timer(MFGPT_PERIODIC);
Andres Salomon8f368812007-10-12 23:04:06 +0200270
271 mfgpt_tick_mode = mode;
272}
273
274static int mfgpt_next_event(unsigned long delta, struct clock_event_device *evt)
275{
Andres Salomone78a77c2008-02-09 23:24:08 +0100276 mfgpt_start_timer(delta);
Andres Salomon8f368812007-10-12 23:04:06 +0200277 return 0;
278}
279
280/* Assume (foolishly?), that this interrupt was due to our tick */
281
282static irqreturn_t mfgpt_tick(int irq, void *dev_id)
283{
Jordan Crouse667984d2008-01-22 23:30:16 +0100284 /* Turn off the clock (and clear the event) */
285 mfgpt_disable_timer(mfgpt_event_clock);
286
Andres Salomon8f368812007-10-12 23:04:06 +0200287 if (mfgpt_tick_mode == CLOCK_EVT_MODE_SHUTDOWN)
288 return IRQ_HANDLED;
289
Andres Salomon8f368812007-10-12 23:04:06 +0200290 /* Clear the counter */
291 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_COUNTER, 0);
292
293 /* Restart the clock in periodic mode */
294
295 if (mfgpt_tick_mode == CLOCK_EVT_MODE_PERIODIC) {
296 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP,
297 MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
298 }
299
300 mfgpt_clockevent.event_handler(&mfgpt_clockevent);
301 return IRQ_HANDLED;
302}
303
304static struct irqaction mfgptirq = {
305 .handler = mfgpt_tick,
306 .flags = IRQF_DISABLED | IRQF_NOBALANCING,
307 .mask = CPU_MASK_NONE,
308 .name = "mfgpt-timer"
309};
310
Andres Salomonb0e6bf22008-02-09 23:24:08 +0100311int __init mfgpt_timer_setup(void)
Andres Salomon8f368812007-10-12 23:04:06 +0200312{
313 int timer, ret;
314 u16 val;
315
Andres Salomonfa28e062008-02-09 23:24:08 +0100316 timer = geode_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
Andres Salomon8f368812007-10-12 23:04:06 +0200317 if (timer < 0) {
318 printk(KERN_ERR
319 "mfgpt-timer: Could not allocate a MFPGT timer\n");
320 return -ENODEV;
321 }
322
323 mfgpt_event_clock = timer;
Andres Salomon8f368812007-10-12 23:04:06 +0200324
325 /* Set up the IRQ on the MFGPT side */
326 if (geode_mfgpt_setup_irq(mfgpt_event_clock, MFGPT_CMP2, irq)) {
327 printk(KERN_ERR "mfgpt-timer: Could not set up IRQ %d\n", irq);
328 return -EIO;
329 }
330
331 /* And register it with the kernel */
332 ret = setup_irq(irq, &mfgptirq);
333
334 if (ret) {
335 printk(KERN_ERR
336 "mfgpt-timer: Unable to set up the interrupt.\n");
337 goto err;
338 }
339
Jordan Crouse667984d2008-01-22 23:30:16 +0100340 /* Set the clock scale and enable the event mode for CMP2 */
341 val = MFGPT_SCALE | (3 << 8);
342
343 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP, val);
344
Andres Salomon8f368812007-10-12 23:04:06 +0200345 /* Set up the clock event */
346 mfgpt_clockevent.mult = div_sc(MFGPT_HZ, NSEC_PER_SEC, 32);
347 mfgpt_clockevent.min_delta_ns = clockevent_delta2ns(0xF,
348 &mfgpt_clockevent);
349 mfgpt_clockevent.max_delta_ns = clockevent_delta2ns(0xFFFE,
350 &mfgpt_clockevent);
351
352 printk(KERN_INFO
353 "mfgpt-timer: registering the MFGT timer as a clock event.\n");
354 clockevents_register_device(&mfgpt_clockevent);
355
356 return 0;
357
358err:
359 geode_mfgpt_release_irq(mfgpt_event_clock, MFGPT_CMP2, irq);
360 printk(KERN_ERR
361 "mfgpt-timer: Unable to set up the MFGPT clock source\n");
362 return -EIO;
363}
364
365#endif