blob: abdb7c71199abb73c63a7f3085feeaab53a663a9 [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 */
Jordan Crousef0875152008-02-09 23:24:08 +010077
78
79static int timers = -1;
80
81static void geode_mfgpt_detect(void)
Andres Salomon83d73842007-10-12 23:04:06 +020082{
Jordan Crousef0875152008-02-09 23:24:08 +010083 int i;
Andres Salomon83d73842007-10-12 23:04:06 +020084 u16 val;
85
Jordan Crousef0875152008-02-09 23:24:08 +010086 timers = 0;
87
Andres Salomon83d73842007-10-12 23:04:06 +020088 if (disable) {
Jordan Crousef0875152008-02-09 23:24:08 +010089 printk(KERN_INFO "geode-mfgpt: MFGPT support is disabled\n");
90 goto done;
91 }
92
93 if (!geode_get_dev_base(GEODE_DEV_MFGPT)) {
94 printk(KERN_INFO "geode-mfgpt: MFGPT LBAR is not set up\n");
95 goto done;
Andres Salomon83d73842007-10-12 23:04:06 +020096 }
97
98 for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
99 val = geode_mfgpt_read(i, MFGPT_REG_SETUP);
100 if (!(val & MFGPT_SETUP_SETUP)) {
Andres Salomon9501b2e2008-02-09 23:24:08 +0100101 mfgpt_timers[i].avail = 1;
Jordan Crousef0875152008-02-09 23:24:08 +0100102 timers++;
Andres Salomon83d73842007-10-12 23:04:06 +0200103 }
104 }
105
Jordan Crousef0875152008-02-09 23:24:08 +0100106done:
107 printk(KERN_INFO "geode-mfgpt: %d MFGPT timers available.\n", timers);
Andres Salomon83d73842007-10-12 23:04:06 +0200108}
109
110int geode_mfgpt_toggle_event(int timer, int cmp, int event, int enable)
111{
112 u32 msr, mask, value, dummy;
113 int shift = (cmp == MFGPT_CMP1) ? 0 : 8;
114
115 if (timer < 0 || timer >= MFGPT_MAX_TIMERS)
116 return -EIO;
117
118 /*
119 * The register maps for these are described in sections 6.17.1.x of
120 * the AMD Geode CS5536 Companion Device Data Book.
121 */
122 switch (event) {
123 case MFGPT_EVENT_RESET:
124 /*
125 * XXX: According to the docs, we cannot reset timers above
126 * 6; that is, resets for 7 and 8 will be ignored. Is this
127 * a problem? -dilinger
128 */
129 msr = MFGPT_NR_MSR;
130 mask = 1 << (timer + 24);
131 break;
132
133 case MFGPT_EVENT_NMI:
134 msr = MFGPT_NR_MSR;
135 mask = 1 << (timer + shift);
136 break;
137
138 case MFGPT_EVENT_IRQ:
139 msr = MFGPT_IRQ_MSR;
140 mask = 1 << (timer + shift);
141 break;
142
143 default:
144 return -EIO;
145 }
146
147 rdmsr(msr, value, dummy);
148
149 if (enable)
150 value |= mask;
151 else
152 value &= ~mask;
153
154 wrmsr(msr, value, dummy);
155 return 0;
156}
157
158int geode_mfgpt_set_irq(int timer, int cmp, int irq, int enable)
159{
160 u32 val, dummy;
161 int offset;
162
163 if (timer < 0 || timer >= MFGPT_MAX_TIMERS)
164 return -EIO;
165
166 if (geode_mfgpt_toggle_event(timer, cmp, MFGPT_EVENT_IRQ, enable))
167 return -EIO;
168
169 rdmsr(MSR_PIC_ZSEL_LOW, val, dummy);
170
171 offset = (timer % 4) * 4;
172
173 val &= ~((0xF << offset) | (0xF << (offset + 16)));
174
175 if (enable) {
176 val |= (irq & 0x0F) << (offset);
177 val |= (irq & 0x0F) << (offset + 16);
178 }
179
180 wrmsr(MSR_PIC_ZSEL_LOW, val, dummy);
181 return 0;
182}
183
Andres Salomonfa28e062008-02-09 23:24:08 +0100184static int mfgpt_get(int timer)
Andres Salomon83d73842007-10-12 23:04:06 +0200185{
Andres Salomon9501b2e2008-02-09 23:24:08 +0100186 mfgpt_timers[timer].avail = 0;
Andres Salomon83d73842007-10-12 23:04:06 +0200187 printk(KERN_INFO "geode-mfgpt: Registered timer %d\n", timer);
188 return timer;
189}
190
Andres Salomonfa28e062008-02-09 23:24:08 +0100191int geode_mfgpt_alloc_timer(int timer, int domain)
Andres Salomon83d73842007-10-12 23:04:06 +0200192{
193 int i;
194
Jordan Crousef0875152008-02-09 23:24:08 +0100195 if (timers == -1) {
196 /* timers haven't been detected yet */
197 geode_mfgpt_detect();
198 }
199
200 if (!timers)
201 return -1;
202
Andres Salomon83d73842007-10-12 23:04:06 +0200203 if (timer >= MFGPT_MAX_TIMERS)
Jordan Crousef0875152008-02-09 23:24:08 +0100204 return -1;
Andres Salomon83d73842007-10-12 23:04:06 +0200205
206 if (timer < 0) {
207 /* Try to find an available timer */
208 for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
Andres Salomon9501b2e2008-02-09 23:24:08 +0100209 if (mfgpt_timers[i].avail)
Andres Salomonfa28e062008-02-09 23:24:08 +0100210 return mfgpt_get(i);
Andres Salomon83d73842007-10-12 23:04:06 +0200211
212 if (i == 5 && domain == MFGPT_DOMAIN_WORKING)
213 break;
214 }
215 } else {
216 /* If they requested a specific timer, try to honor that */
Andres Salomon9501b2e2008-02-09 23:24:08 +0100217 if (mfgpt_timers[timer].avail)
Andres Salomonfa28e062008-02-09 23:24:08 +0100218 return mfgpt_get(timer);
Andres Salomon83d73842007-10-12 23:04:06 +0200219 }
220
221 /* No timers available - too bad */
222 return -1;
223}
224
Andres Salomon8f368812007-10-12 23:04:06 +0200225
226#ifdef CONFIG_GEODE_MFGPT_TIMER
227
228/*
229 * The MFPGT timers on the CS5536 provide us with suitable timers to use
230 * as clock event sources - not as good as a HPET or APIC, but certainly
231 * better then the PIT. This isn't a general purpose MFGPT driver, but
232 * a simplified one designed specifically to act as a clock event source.
233 * For full details about the MFGPT, please consult the CS5536 data sheet.
234 */
235
236#include <linux/clocksource.h>
237#include <linux/clockchips.h>
238
239static unsigned int mfgpt_tick_mode = CLOCK_EVT_MODE_SHUTDOWN;
240static u16 mfgpt_event_clock;
241
242static int irq = 7;
243static int __init mfgpt_setup(char *str)
244{
245 get_option(&str, &irq);
246 return 1;
247}
248__setup("mfgpt_irq=", mfgpt_setup);
249
Andres Salomone78a77c2008-02-09 23:24:08 +0100250static void mfgpt_disable_timer(u16 clock)
Andres Salomon8f368812007-10-12 23:04:06 +0200251{
252 u16 val = geode_mfgpt_read(clock, MFGPT_REG_SETUP);
253 geode_mfgpt_write(clock, MFGPT_REG_SETUP, val & ~MFGPT_SETUP_CNTEN);
254}
255
256static int mfgpt_next_event(unsigned long, struct clock_event_device *);
257static void mfgpt_set_mode(enum clock_event_mode, struct clock_event_device *);
258
259static struct clock_event_device mfgpt_clockevent = {
260 .name = "mfgpt-timer",
261 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
262 .set_mode = mfgpt_set_mode,
263 .set_next_event = mfgpt_next_event,
264 .rating = 250,
265 .cpumask = CPU_MASK_ALL,
266 .shift = 32
267};
268
Andres Salomone78a77c2008-02-09 23:24:08 +0100269static void mfgpt_start_timer(u16 delta)
Andres Salomon8f368812007-10-12 23:04:06 +0200270{
271 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_CMP2, (u16) delta);
272 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_COUNTER, 0);
273
274 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP,
275 MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
276}
277
278static void mfgpt_set_mode(enum clock_event_mode mode,
279 struct clock_event_device *evt)
280{
281 mfgpt_disable_timer(mfgpt_event_clock);
282
283 if (mode == CLOCK_EVT_MODE_PERIODIC)
Andres Salomone78a77c2008-02-09 23:24:08 +0100284 mfgpt_start_timer(MFGPT_PERIODIC);
Andres Salomon8f368812007-10-12 23:04:06 +0200285
286 mfgpt_tick_mode = mode;
287}
288
289static int mfgpt_next_event(unsigned long delta, struct clock_event_device *evt)
290{
Andres Salomone78a77c2008-02-09 23:24:08 +0100291 mfgpt_start_timer(delta);
Andres Salomon8f368812007-10-12 23:04:06 +0200292 return 0;
293}
294
295/* Assume (foolishly?), that this interrupt was due to our tick */
296
297static irqreturn_t mfgpt_tick(int irq, void *dev_id)
298{
Jordan Crouse667984d2008-01-22 23:30:16 +0100299 /* Turn off the clock (and clear the event) */
300 mfgpt_disable_timer(mfgpt_event_clock);
301
Andres Salomon8f368812007-10-12 23:04:06 +0200302 if (mfgpt_tick_mode == CLOCK_EVT_MODE_SHUTDOWN)
303 return IRQ_HANDLED;
304
Andres Salomon8f368812007-10-12 23:04:06 +0200305 /* Clear the counter */
306 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_COUNTER, 0);
307
308 /* Restart the clock in periodic mode */
309
310 if (mfgpt_tick_mode == CLOCK_EVT_MODE_PERIODIC) {
311 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP,
312 MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
313 }
314
315 mfgpt_clockevent.event_handler(&mfgpt_clockevent);
316 return IRQ_HANDLED;
317}
318
319static struct irqaction mfgptirq = {
320 .handler = mfgpt_tick,
321 .flags = IRQF_DISABLED | IRQF_NOBALANCING,
322 .mask = CPU_MASK_NONE,
323 .name = "mfgpt-timer"
324};
325
Andres Salomonb0e6bf22008-02-09 23:24:08 +0100326int __init mfgpt_timer_setup(void)
Andres Salomon8f368812007-10-12 23:04:06 +0200327{
328 int timer, ret;
329 u16 val;
330
Andres Salomonfa28e062008-02-09 23:24:08 +0100331 timer = geode_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
Andres Salomon8f368812007-10-12 23:04:06 +0200332 if (timer < 0) {
333 printk(KERN_ERR
334 "mfgpt-timer: Could not allocate a MFPGT timer\n");
335 return -ENODEV;
336 }
337
338 mfgpt_event_clock = timer;
Andres Salomon8f368812007-10-12 23:04:06 +0200339
340 /* Set up the IRQ on the MFGPT side */
341 if (geode_mfgpt_setup_irq(mfgpt_event_clock, MFGPT_CMP2, irq)) {
342 printk(KERN_ERR "mfgpt-timer: Could not set up IRQ %d\n", irq);
343 return -EIO;
344 }
345
346 /* And register it with the kernel */
347 ret = setup_irq(irq, &mfgptirq);
348
349 if (ret) {
350 printk(KERN_ERR
351 "mfgpt-timer: Unable to set up the interrupt.\n");
352 goto err;
353 }
354
Jordan Crouse667984d2008-01-22 23:30:16 +0100355 /* Set the clock scale and enable the event mode for CMP2 */
356 val = MFGPT_SCALE | (3 << 8);
357
358 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP, val);
359
Andres Salomon8f368812007-10-12 23:04:06 +0200360 /* Set up the clock event */
361 mfgpt_clockevent.mult = div_sc(MFGPT_HZ, NSEC_PER_SEC, 32);
362 mfgpt_clockevent.min_delta_ns = clockevent_delta2ns(0xF,
363 &mfgpt_clockevent);
364 mfgpt_clockevent.max_delta_ns = clockevent_delta2ns(0xFFFE,
365 &mfgpt_clockevent);
366
367 printk(KERN_INFO
368 "mfgpt-timer: registering the MFGT timer as a clock event.\n");
369 clockevents_register_device(&mfgpt_clockevent);
370
371 return 0;
372
373err:
374 geode_mfgpt_release_irq(mfgpt_event_clock, MFGPT_CMP2, irq);
375 printk(KERN_ERR
376 "mfgpt-timer: Unable to set up the MFGPT clock source\n");
377 return -EIO;
378}
379
380#endif