x86, um: ... and asm-x86 move
[safe/jmp/linux-2.6] / arch / x86 / kernel / mfgpt_32.c
index 81aa9db..3b59951 100644 (file)
 
 #include <linux/kernel.h>
 #include <linux/interrupt.h>
+#include <linux/module.h>
 #include <asm/geode.h>
 
+#define MFGPT_DEFAULT_IRQ      7
+
 static struct mfgpt_timer_t {
        unsigned int avail:1;
 } mfgpt_timers[MFGPT_MAX_TIMERS];
@@ -62,7 +65,7 @@ static int __init mfgpt_fix(char *s)
 
        /* The following udocumented bit resets the MFGPT timers */
        val = 0xFF; dummy = 0;
-       wrmsr(0x5140002B, val, dummy);
+       wrmsr(MSR_MFGPT_SETUP, val, dummy);
        return 1;
 }
 __setup("mfgptfix", mfgpt_fix);
@@ -126,17 +129,17 @@ int geode_mfgpt_toggle_event(int timer, int cmp, int event, int enable)
                 * 6; that is, resets for 7 and 8 will be ignored.  Is this
                 * a problem?   -dilinger
                 */
-               msr = MFGPT_NR_MSR;
+               msr = MSR_MFGPT_NR;
                mask = 1 << (timer + 24);
                break;
 
        case MFGPT_EVENT_NMI:
-               msr = MFGPT_NR_MSR;
+               msr = MSR_MFGPT_NR;
                mask = 1 << (timer + shift);
                break;
 
        case MFGPT_EVENT_IRQ:
-               msr = MFGPT_IRQ_MSR;
+               msr = MSR_MFGPT_IRQ;
                mask = 1 << (timer + shift);
                break;
 
@@ -154,30 +157,50 @@ int geode_mfgpt_toggle_event(int timer, int cmp, int event, int enable)
        wrmsr(msr, value, dummy);
        return 0;
 }
+EXPORT_SYMBOL_GPL(geode_mfgpt_toggle_event);
 
-int geode_mfgpt_set_irq(int timer, int cmp, int irq, int enable)
+int geode_mfgpt_set_irq(int timer, int cmp, int *irq, int enable)
 {
-       u32 val, dummy;
-       int offset;
+       u32 zsel, lpc, dummy;
+       int shift;
 
        if (timer < 0 || timer >= MFGPT_MAX_TIMERS)
                return -EIO;
 
-       if (geode_mfgpt_toggle_event(timer, cmp, MFGPT_EVENT_IRQ, enable))
+       /*
+        * Unfortunately, MFGPTs come in pairs sharing their IRQ lines. If VSA
+        * is using the same CMP of the timer's Siamese twin, the IRQ is set to
+        * 2, and we mustn't use nor change it.
+        * XXX: Likewise, 2 Linux drivers might clash if the 2nd overwrites the
+        * IRQ of the 1st. This can only happen if forcing an IRQ, calling this
+        * with *irq==0 is safe. Currently there _are_ no 2 drivers.
+        */
+       rdmsr(MSR_PIC_ZSEL_LOW, zsel, dummy);
+       shift = ((cmp == MFGPT_CMP1 ? 0 : 4) + timer % 4) * 4;
+       if (((zsel >> shift) & 0xF) == 2)
                return -EIO;
 
-       rdmsr(MSR_PIC_ZSEL_LOW, val, dummy);
-
-       offset = (timer % 4) * 4;
+       /* Choose IRQ: if none supplied, keep IRQ already set or use default */
+       if (!*irq)
+               *irq = (zsel >> shift) & 0xF;
+       if (!*irq)
+               *irq = MFGPT_DEFAULT_IRQ;
 
-       val &= ~((0xF << offset) | (0xF << (offset + 16)));
+       /* Can't use IRQ if it's 0 (=disabled), 2, or routed to LPC */
+       if (*irq < 1 || *irq == 2 || *irq > 15)
+               return -EIO;
+       rdmsr(MSR_PIC_IRQM_LPC, lpc, dummy);
+       if (lpc & (1 << *irq))
+               return -EIO;
 
+       /* All chosen and checked - go for it */
+       if (geode_mfgpt_toggle_event(timer, cmp, MFGPT_EVENT_IRQ, enable))
+               return -EIO;
        if (enable) {
-               val |= (irq & 0x0F) << (offset);
-               val |= (irq & 0x0F) << (offset + 16);
+               zsel = (zsel & ~(0xF << shift)) | (*irq << shift);
+               wrmsr(MSR_PIC_ZSEL_LOW, zsel, dummy);
        }
 
-       wrmsr(MSR_PIC_ZSEL_LOW, val, dummy);
        return 0;
 }
 
@@ -221,6 +244,7 @@ int geode_mfgpt_alloc_timer(int timer, int domain)
        /* No timers available - too bad */
        return -1;
 }
+EXPORT_SYMBOL_GPL(geode_mfgpt_alloc_timer);
 
 
 #ifdef CONFIG_GEODE_MFGPT_TIMER
@@ -239,7 +263,7 @@ int geode_mfgpt_alloc_timer(int timer, int domain)
 static unsigned int mfgpt_tick_mode = CLOCK_EVT_MODE_SHUTDOWN;
 static u16 mfgpt_event_clock;
 
-static int irq = 7;
+static int irq;
 static int __init mfgpt_setup(char *str)
 {
        get_option(&str, &irq);
@@ -293,10 +317,14 @@ static int mfgpt_next_event(unsigned long delta, struct clock_event_device *evt)
        return 0;
 }
 
-/* Assume (foolishly?), that this interrupt was due to our tick */
-
 static irqreturn_t mfgpt_tick(int irq, void *dev_id)
 {
+       u16 val = geode_mfgpt_read(mfgpt_event_clock, MFGPT_REG_SETUP);
+
+       /* See if the interrupt was for us */
+       if (!(val & (MFGPT_SETUP_SETUP  | MFGPT_SETUP_CMP2 | MFGPT_SETUP_CMP1)))
+               return IRQ_NONE;
+
        /* Turn off the clock (and clear the event) */
        mfgpt_disable_timer(mfgpt_event_clock);
 
@@ -339,7 +367,7 @@ int __init mfgpt_timer_setup(void)
        mfgpt_event_clock = timer;
 
        /* Set up the IRQ on the MFGPT side */
-       if (geode_mfgpt_setup_irq(mfgpt_event_clock, MFGPT_CMP2, irq)) {
+       if (geode_mfgpt_setup_irq(mfgpt_event_clock, MFGPT_CMP2, &irq)) {
                printk(KERN_ERR "mfgpt-timer:  Could not set up IRQ %d\n", irq);
                return -EIO;
        }
@@ -359,20 +387,22 @@ int __init mfgpt_timer_setup(void)
        geode_mfgpt_write(mfgpt_event_clock, MFGPT_REG_SETUP, val);
 
        /* Set up the clock event */
-       mfgpt_clockevent.mult = div_sc(MFGPT_HZ, NSEC_PER_SEC, 32);
+       mfgpt_clockevent.mult = div_sc(MFGPT_HZ, NSEC_PER_SEC,
+                                      mfgpt_clockevent.shift);
        mfgpt_clockevent.min_delta_ns = clockevent_delta2ns(0xF,
                        &mfgpt_clockevent);
        mfgpt_clockevent.max_delta_ns = clockevent_delta2ns(0xFFFE,
                        &mfgpt_clockevent);
 
        printk(KERN_INFO
-              "mfgpt-timer:  registering the MFGT timer as a clock event.\n");
+              "mfgpt-timer:  Registering MFGPT timer %d as a clock event, using IRQ %d\n",
+              timer, irq);
        clockevents_register_device(&mfgpt_clockevent);
 
        return 0;
 
 err:
-       geode_mfgpt_release_irq(mfgpt_event_clock, MFGPT_CMP2, irq);
+       geode_mfgpt_release_irq(mfgpt_event_clock, MFGPT_CMP2, &irq);
        printk(KERN_ERR
               "mfgpt-timer:  Unable to set up the MFGPT clock source\n");
        return -EIO;