blob: c12314c9e86fd512367a588646770ea0c4149a5c [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 Salomon923a0cf2008-03-26 14:13:01 -040033#include <linux/module.h>
Andres Salomon83d73842007-10-12 23:04:06 +020034#include <asm/geode.h>
35
Jens Rottmann0d5cdc92008-08-04 14:40:16 +020036#define MFGPT_DEFAULT_IRQ 7
37
Andres Salomon83d73842007-10-12 23:04:06 +020038static struct mfgpt_timer_t {
Andres Salomon9501b2e2008-02-09 23:24:08 +010039 unsigned int avail:1;
Andres Salomon83d73842007-10-12 23:04:06 +020040} mfgpt_timers[MFGPT_MAX_TIMERS];
41
42/* Selected from the table above */
43
44#define MFGPT_DIVISOR 16
45#define MFGPT_SCALE 4 /* divisor = 2^(scale) */
Willy Tarreau36445cf32008-02-09 23:24:08 +010046#define MFGPT_HZ (32768 / MFGPT_DIVISOR)
Andres Salomon83d73842007-10-12 23:04:06 +020047#define MFGPT_PERIODIC (MFGPT_HZ / HZ)
48
49/* Allow for disabling of MFGPTs */
50static int disable;
51static int __init mfgpt_disable(char *s)
52{
53 disable = 1;
54 return 1;
55}
56__setup("nomfgpt", mfgpt_disable);
57
Willy Tarreaue6c4dc62008-01-30 13:33:33 +010058/* Reset the MFGPT timers. This is required by some broken BIOSes which already
59 * do the same and leave the system in an unstable state. TinyBIOS 0.98 is
60 * affected at least (0.99 is OK with MFGPT workaround left to off).
61 */
62static int __init mfgpt_fix(char *s)
63{
64 u32 val, dummy;
65
66 /* The following udocumented bit resets the MFGPT timers */
67 val = 0xFF; dummy = 0;
Andres Salomon32bf87e2008-04-28 02:14:53 -070068 wrmsr(MSR_MFGPT_SETUP, val, dummy);
Willy Tarreaue6c4dc62008-01-30 13:33:33 +010069 return 1;
70}
71__setup("mfgptfix", mfgpt_fix);
72
Andres Salomon83d73842007-10-12 23:04:06 +020073/*
74 * Check whether any MFGPTs are available for the kernel to use. In most
75 * cases, firmware that uses AMD's VSA code will claim all timers during
76 * bootup; we certainly don't want to take them if they're already in use.
77 * In other cases (such as with VSAless OpenFirmware), the system firmware
78 * leaves timers available for us to use.
79 */
Jordan Crousef0875152008-02-09 23:24:08 +010080
81
82static int timers = -1;
83
84static void geode_mfgpt_detect(void)
Andres Salomon83d73842007-10-12 23:04:06 +020085{
Jordan Crousef0875152008-02-09 23:24:08 +010086 int i;
Andres Salomon83d73842007-10-12 23:04:06 +020087 u16 val;
88
Jordan Crousef0875152008-02-09 23:24:08 +010089 timers = 0;
90
Andres Salomon83d73842007-10-12 23:04:06 +020091 if (disable) {
Jordan Crousef0875152008-02-09 23:24:08 +010092 printk(KERN_INFO "geode-mfgpt: MFGPT support is disabled\n");
93 goto done;
94 }
95
96 if (!geode_get_dev_base(GEODE_DEV_MFGPT)) {
97 printk(KERN_INFO "geode-mfgpt: MFGPT LBAR is not set up\n");
98 goto done;
Andres Salomon83d73842007-10-12 23:04:06 +020099 }
100
101 for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
102 val = geode_mfgpt_read(i, MFGPT_REG_SETUP);
103 if (!(val & MFGPT_SETUP_SETUP)) {
Andres Salomon9501b2e2008-02-09 23:24:08 +0100104 mfgpt_timers[i].avail = 1;
Jordan Crousef0875152008-02-09 23:24:08 +0100105 timers++;
Andres Salomon83d73842007-10-12 23:04:06 +0200106 }
107 }
108
Jordan Crousef0875152008-02-09 23:24:08 +0100109done:
110 printk(KERN_INFO "geode-mfgpt: %d MFGPT timers available.\n", timers);
Andres Salomon83d73842007-10-12 23:04:06 +0200111}
112
113int geode_mfgpt_toggle_event(int timer, int cmp, int event, int enable)
114{
115 u32 msr, mask, value, dummy;
116 int shift = (cmp == MFGPT_CMP1) ? 0 : 8;
117
118 if (timer < 0 || timer >= MFGPT_MAX_TIMERS)
119 return -EIO;
120
121 /*
122 * The register maps for these are described in sections 6.17.1.x of
123 * the AMD Geode CS5536 Companion Device Data Book.
124 */
125 switch (event) {
126 case MFGPT_EVENT_RESET:
127 /*
128 * XXX: According to the docs, we cannot reset timers above
129 * 6; that is, resets for 7 and 8 will be ignored. Is this
130 * a problem? -dilinger
131 */
Andres Salomon32bf87e2008-04-28 02:14:53 -0700132 msr = MSR_MFGPT_NR;
Andres Salomon83d73842007-10-12 23:04:06 +0200133 mask = 1 << (timer + 24);
134 break;
135
136 case MFGPT_EVENT_NMI:
Andres Salomon32bf87e2008-04-28 02:14:53 -0700137 msr = MSR_MFGPT_NR;
Andres Salomon83d73842007-10-12 23:04:06 +0200138 mask = 1 << (timer + shift);
139 break;
140
141 case MFGPT_EVENT_IRQ:
Andres Salomon32bf87e2008-04-28 02:14:53 -0700142 msr = MSR_MFGPT_IRQ;
Andres Salomon83d73842007-10-12 23:04:06 +0200143 mask = 1 << (timer + shift);
144 break;
145
146 default:
147 return -EIO;
148 }
149
150 rdmsr(msr, value, dummy);
151
152 if (enable)
153 value |= mask;
154 else
155 value &= ~mask;
156
157 wrmsr(msr, value, dummy);
158 return 0;
159}
Ingo Molnar3703f392008-06-04 18:13:37 +0200160EXPORT_SYMBOL_GPL(geode_mfgpt_toggle_event);
Andres Salomon83d73842007-10-12 23:04:06 +0200161
Jens Rottmann0d5cdc92008-08-04 14:40:16 +0200162int geode_mfgpt_set_irq(int timer, int cmp, int *irq, int enable)
Andres Salomon83d73842007-10-12 23:04:06 +0200163{
Jens Rottmann0d5cdc92008-08-04 14:40:16 +0200164 u32 zsel, lpc, dummy;
165 int shift;
Andres Salomon83d73842007-10-12 23:04:06 +0200166
167 if (timer < 0 || timer >= MFGPT_MAX_TIMERS)
168 return -EIO;
169
Jens Rottmann0d5cdc92008-08-04 14:40:16 +0200170 /*
171 * Unfortunately, MFGPTs come in pairs sharing their IRQ lines. If VSA
172 * is using the same CMP of the timer's Siamese twin, the IRQ is set to
173 * 2, and we mustn't use nor change it.
174 * XXX: Likewise, 2 Linux drivers might clash if the 2nd overwrites the
175 * IRQ of the 1st. This can only happen if forcing an IRQ, calling this
176 * with *irq==0 is safe. Currently there _are_ no 2 drivers.
177 */
178 rdmsr(MSR_PIC_ZSEL_LOW, zsel, dummy);
179 shift = ((cmp == MFGPT_CMP1 ? 0 : 4) + timer % 4) * 4;
180 if (((zsel >> shift) & 0xF) == 2)
Andres Salomon83d73842007-10-12 23:04:06 +0200181 return -EIO;
182
Jens Rottmann0d5cdc92008-08-04 14:40:16 +0200183 /* Choose IRQ: if none supplied, keep IRQ already set or use default */
184 if (!*irq)
185 *irq = (zsel >> shift) & 0xF;
186 if (!*irq)
187 *irq = MFGPT_DEFAULT_IRQ;
Andres Salomon83d73842007-10-12 23:04:06 +0200188
Jens Rottmann0d5cdc92008-08-04 14:40:16 +0200189 /* Can't use IRQ if it's 0 (=disabled), 2, or routed to LPC */
190 if (*irq < 1 || *irq == 2 || *irq > 15)
191 return -EIO;
192 rdmsr(MSR_PIC_IRQM_LPC, lpc, dummy);
193 if (lpc & (1 << *irq))
194 return -EIO;
Andres Salomon83d73842007-10-12 23:04:06 +0200195
Jens Rottmann0d5cdc92008-08-04 14:40:16 +0200196 /* All chosen and checked - go for it */
197 if (geode_mfgpt_toggle_event(timer, cmp, MFGPT_EVENT_IRQ, enable))
198 return -EIO;
Andres Salomon83d73842007-10-12 23:04:06 +0200199 if (enable) {
Jens Rottmann0d5cdc92008-08-04 14:40:16 +0200200 zsel = (zsel & ~(0xF << shift)) | (*irq << shift);
201 wrmsr(MSR_PIC_ZSEL_LOW, zsel, dummy);
Andres Salomon83d73842007-10-12 23:04:06 +0200202 }
203
Andres Salomon83d73842007-10-12 23:04:06 +0200204 return 0;
205}
206
Andres Salomonfa28e062008-02-09 23:24:08 +0100207static int mfgpt_get(int timer)
Andres Salomon83d73842007-10-12 23:04:06 +0200208{
Andres Salomon9501b2e2008-02-09 23:24:08 +0100209 mfgpt_timers[timer].avail = 0;
Andres Salomon83d73842007-10-12 23:04:06 +0200210 printk(KERN_INFO "geode-mfgpt: Registered timer %d\n", timer);
211 return timer;
212}
213
Andres Salomonfa28e062008-02-09 23:24:08 +0100214int geode_mfgpt_alloc_timer(int timer, int domain)
Andres Salomon83d73842007-10-12 23:04:06 +0200215{
216 int i;
217
Jordan Crousef0875152008-02-09 23:24:08 +0100218 if (timers == -1) {
219 /* timers haven't been detected yet */
220 geode_mfgpt_detect();
221 }
222
223 if (!timers)
224 return -1;
225
Andres Salomon83d73842007-10-12 23:04:06 +0200226 if (timer >= MFGPT_MAX_TIMERS)
Jordan Crousef0875152008-02-09 23:24:08 +0100227 return -1;
Andres Salomon83d73842007-10-12 23:04:06 +0200228
229 if (timer < 0) {
230 /* Try to find an available timer */
231 for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
Andres Salomon9501b2e2008-02-09 23:24:08 +0100232 if (mfgpt_timers[i].avail)
Andres Salomonfa28e062008-02-09 23:24:08 +0100233 return mfgpt_get(i);
Andres Salomon83d73842007-10-12 23:04:06 +0200234
235 if (i == 5 && domain == MFGPT_DOMAIN_WORKING)
236 break;
237 }
238 } else {
239 /* If they requested a specific timer, try to honor that */
Andres Salomon9501b2e2008-02-09 23:24:08 +0100240 if (mfgpt_timers[timer].avail)
Andres Salomonfa28e062008-02-09 23:24:08 +0100241 return mfgpt_get(timer);
Andres Salomon83d73842007-10-12 23:04:06 +0200242 }
243
244 /* No timers available - too bad */
245 return -1;
246}
Ingo Molnar3703f392008-06-04 18:13:37 +0200247EXPORT_SYMBOL_GPL(geode_mfgpt_alloc_timer);
Andres Salomon83d73842007-10-12 23:04:06 +0200248
Andres Salomon8f368812007-10-12 23:04:06 +0200249
250#ifdef CONFIG_GEODE_MFGPT_TIMER
251
252/*
253 * The MFPGT timers on the CS5536 provide us with suitable timers to use
254 * as clock event sources - not as good as a HPET or APIC, but certainly
255 * better then the PIT. This isn't a general purpose MFGPT driver, but
256 * a simplified one designed specifically to act as a clock event source.
257 * For full details about the MFGPT, please consult the CS5536 data sheet.
258 */
259
260#include <linux/clocksource.h>
261#include <linux/clockchips.h>
262
263static unsigned int mfgpt_tick_mode = CLOCK_EVT_MODE_SHUTDOWN;
264static u16 mfgpt_event_clock;
265
Jens Rottmann0d5cdc92008-08-04 14:40:16 +0200266static int irq;
Andres Salomon8f368812007-10-12 23:04:06 +0200267static int __init mfgpt_setup(char *str)
268{
269 get_option(&str, &irq);
270 return 1;
271}
272__setup("mfgpt_irq=", mfgpt_setup);
273
Andres Salomone78a77c2008-02-09 23:24:08 +0100274static void mfgpt_disable_timer(u16 clock)
Andres Salomon8f368812007-10-12 23:04:06 +0200275{
Andres Salomonf54ae692008-02-09 23:24:08 +0100276 /* avoid races by clearing CMP1 and CMP2 unconditionally */
277 geode_mfgpt_write(clock, MFGPT_REG_SETUP, (u16) ~MFGPT_SETUP_CNTEN |
278 MFGPT_SETUP_CMP1 | MFGPT_SETUP_CMP2);
Andres Salomon8f368812007-10-12 23:04:06 +0200279}
280
281static int mfgpt_next_event(unsigned long, struct clock_event_device *);
282static void mfgpt_set_mode(enum clock_event_mode, struct clock_event_device *);
283
284static struct clock_event_device mfgpt_clockevent = {
285 .name = "mfgpt-timer",
286 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
287 .set_mode = mfgpt_set_mode,
288 .set_next_event = mfgpt_next_event,
289 .rating = 250,
Rusty Russell320ab2b2008-12-13 21:20:26 +1030290 .cpumask = cpu_all_mask,
Andres Salomon8f368812007-10-12 23:04:06 +0200291 .shift = 32
292};
293
Andres Salomone78a77c2008-02-09 23:24:08 +0100294static void mfgpt_start_timer(u16 delta)
Andres Salomon8f368812007-10-12 23:04:06 +0200295{
296 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_CMP2, (u16) delta);
297 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_COUNTER, 0);
298
299 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP,
300 MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
301}
302
303static void mfgpt_set_mode(enum clock_event_mode mode,
304 struct clock_event_device *evt)
305{
306 mfgpt_disable_timer(mfgpt_event_clock);
307
308 if (mode == CLOCK_EVT_MODE_PERIODIC)
Andres Salomone78a77c2008-02-09 23:24:08 +0100309 mfgpt_start_timer(MFGPT_PERIODIC);
Andres Salomon8f368812007-10-12 23:04:06 +0200310
311 mfgpt_tick_mode = mode;
312}
313
314static int mfgpt_next_event(unsigned long delta, struct clock_event_device *evt)
315{
Andres Salomone78a77c2008-02-09 23:24:08 +0100316 mfgpt_start_timer(delta);
Andres Salomon8f368812007-10-12 23:04:06 +0200317 return 0;
318}
319
Andres Salomon8f368812007-10-12 23:04:06 +0200320static irqreturn_t mfgpt_tick(int irq, void *dev_id)
321{
Jordan Crousedcee77b2008-02-09 23:24:08 +0100322 u16 val = geode_mfgpt_read(mfgpt_event_clock, MFGPT_REG_SETUP);
323
324 /* See if the interrupt was for us */
325 if (!(val & (MFGPT_SETUP_SETUP | MFGPT_SETUP_CMP2 | MFGPT_SETUP_CMP1)))
326 return IRQ_NONE;
327
Jordan Crouse667984d2008-01-22 23:30:16 +0100328 /* Turn off the clock (and clear the event) */
329 mfgpt_disable_timer(mfgpt_event_clock);
330
Andres Salomon8f368812007-10-12 23:04:06 +0200331 if (mfgpt_tick_mode == CLOCK_EVT_MODE_SHUTDOWN)
332 return IRQ_HANDLED;
333
Andres Salomon8f368812007-10-12 23:04:06 +0200334 /* Clear the counter */
335 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_COUNTER, 0);
336
337 /* Restart the clock in periodic mode */
338
339 if (mfgpt_tick_mode == CLOCK_EVT_MODE_PERIODIC) {
340 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP,
341 MFGPT_SETUP_CNTEN | MFGPT_SETUP_CMP2);
342 }
343
344 mfgpt_clockevent.event_handler(&mfgpt_clockevent);
345 return IRQ_HANDLED;
346}
347
348static struct irqaction mfgptirq = {
349 .handler = mfgpt_tick,
350 .flags = IRQF_DISABLED | IRQF_NOBALANCING,
351 .mask = CPU_MASK_NONE,
352 .name = "mfgpt-timer"
353};
354
Andres Salomonb0e6bf22008-02-09 23:24:08 +0100355int __init mfgpt_timer_setup(void)
Andres Salomon8f368812007-10-12 23:04:06 +0200356{
357 int timer, ret;
358 u16 val;
359
Andres Salomonfa28e062008-02-09 23:24:08 +0100360 timer = geode_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
Andres Salomon8f368812007-10-12 23:04:06 +0200361 if (timer < 0) {
362 printk(KERN_ERR
363 "mfgpt-timer: Could not allocate a MFPGT timer\n");
364 return -ENODEV;
365 }
366
367 mfgpt_event_clock = timer;
Andres Salomon8f368812007-10-12 23:04:06 +0200368
369 /* Set up the IRQ on the MFGPT side */
Jens Rottmann0d5cdc92008-08-04 14:40:16 +0200370 if (geode_mfgpt_setup_irq(mfgpt_event_clock, MFGPT_CMP2, &irq)) {
Andres Salomon8f368812007-10-12 23:04:06 +0200371 printk(KERN_ERR "mfgpt-timer: Could not set up IRQ %d\n", irq);
372 return -EIO;
373 }
374
375 /* And register it with the kernel */
376 ret = setup_irq(irq, &mfgptirq);
377
378 if (ret) {
379 printk(KERN_ERR
380 "mfgpt-timer: Unable to set up the interrupt.\n");
381 goto err;
382 }
383
Jordan Crouse667984d2008-01-22 23:30:16 +0100384 /* Set the clock scale and enable the event mode for CMP2 */
385 val = MFGPT_SCALE | (3 << 8);
386
387 geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP, val);
388
Andres Salomon8f368812007-10-12 23:04:06 +0200389 /* Set up the clock event */
Akinobu Mita877084f2008-04-19 23:55:16 +0900390 mfgpt_clockevent.mult = div_sc(MFGPT_HZ, NSEC_PER_SEC,
391 mfgpt_clockevent.shift);
Andres Salomon8f368812007-10-12 23:04:06 +0200392 mfgpt_clockevent.min_delta_ns = clockevent_delta2ns(0xF,
393 &mfgpt_clockevent);
394 mfgpt_clockevent.max_delta_ns = clockevent_delta2ns(0xFFFE,
395 &mfgpt_clockevent);
396
397 printk(KERN_INFO
Jens Rottmann0d5cdc92008-08-04 14:40:16 +0200398 "mfgpt-timer: Registering MFGPT timer %d as a clock event, using IRQ %d\n",
399 timer, irq);
Andres Salomon8f368812007-10-12 23:04:06 +0200400 clockevents_register_device(&mfgpt_clockevent);
401
402 return 0;
403
404err:
Jens Rottmann0d5cdc92008-08-04 14:40:16 +0200405 geode_mfgpt_release_irq(mfgpt_event_clock, MFGPT_CMP2, &irq);
Andres Salomon8f368812007-10-12 23:04:06 +0200406 printk(KERN_ERR
407 "mfgpt-timer: Unable to set up the MFGPT clock source\n");
408 return -EIO;
409}
410
411#endif