x86: SGI UV: Map low MMR ranges
[safe/jmp/linux-2.6] / arch / x86 / kernel / mfgpt_32.c
index 6f79061..2a62d84 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];
@@ -43,12 +46,6 @@ static struct mfgpt_timer_t {
 #define MFGPT_HZ  (32768 / MFGPT_DIVISOR)
 #define MFGPT_PERIODIC (MFGPT_HZ / HZ)
 
-#ifdef CONFIG_GEODE_MFGPT_TIMER
-static int __init mfgpt_timer_setup(void);
-#else
-#define mfgpt_timer_setup() (0)
-#endif
-
 /* Allow for disabling of MFGPTs */
 static int disable;
 static int __init mfgpt_disable(char *s)
@@ -68,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);
@@ -80,28 +77,37 @@ __setup("mfgptfix", mfgpt_fix);
  * In other cases (such as with VSAless OpenFirmware), the system firmware
  * leaves timers available for us to use.
  */
-int __init geode_mfgpt_detect(void)
+
+
+static int timers = -1;
+
+static void geode_mfgpt_detect(void)
 {
-       int count = 0, i;
+       int i;
        u16 val;
 
+       timers = 0;
+
        if (disable) {
-               printk(KERN_INFO "geode-mfgpt:  Skipping MFGPT setup\n");
-               return 0;
+               printk(KERN_INFO "geode-mfgpt:  MFGPT support is disabled\n");
+               goto done;
+       }
+
+       if (!geode_get_dev_base(GEODE_DEV_MFGPT)) {
+               printk(KERN_INFO "geode-mfgpt:  MFGPT LBAR is not set up\n");
+               goto done;
        }
 
        for (i = 0; i < MFGPT_MAX_TIMERS; i++) {
                val = geode_mfgpt_read(i, MFGPT_REG_SETUP);
                if (!(val & MFGPT_SETUP_SETUP)) {
                        mfgpt_timers[i].avail = 1;
-                       count++;
+                       timers++;
                }
        }
 
-       /* set up clock event device, if desired */
-       i = mfgpt_timer_setup();
-
-       return count;
+done:
+       printk(KERN_INFO "geode-mfgpt:  %d MFGPT timers available.\n", timers);
 }
 
 int geode_mfgpt_toggle_event(int timer, int cmp, int event, int enable)
@@ -123,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;
 
@@ -151,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;
 }
 
@@ -189,10 +215,16 @@ int geode_mfgpt_alloc_timer(int timer, int domain)
 {
        int i;
 
-       if (!geode_get_dev_base(GEODE_DEV_MFGPT))
-               return -ENODEV;
+       if (timers == -1) {
+               /* timers haven't been detected yet */
+               geode_mfgpt_detect();
+       }
+
+       if (!timers)
+               return -1;
+
        if (timer >= MFGPT_MAX_TIMERS)
-               return -EIO;
+               return -1;
 
        if (timer < 0) {
                /* Try to find an available timer */
@@ -212,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
@@ -219,7 +252,7 @@ int geode_mfgpt_alloc_timer(int timer, int domain)
 /*
  * The MFPGT timers on the CS5536 provide us with suitable timers to use
  * as clock event sources - not as good as a HPET or APIC, but certainly
- * better then the PIT.  This isn't a general purpose MFGPT driver, but
+ * better than the PIT.  This isn't a general purpose MFGPT driver, but
  * a simplified one designed specifically to act as a clock event source.
  * For full details about the MFGPT, please consult the CS5536 data sheet.
  */
@@ -230,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);
@@ -240,8 +273,9 @@ __setup("mfgpt_irq=", mfgpt_setup);
 
 static void mfgpt_disable_timer(u16 clock)
 {
-       u16 val = geode_mfgpt_read(clock, MFGPT_REG_SETUP);
-       geode_mfgpt_write(clock, MFGPT_REG_SETUP, val & ~MFGPT_SETUP_CNTEN);
+       /* avoid races by clearing CMP1 and CMP2 unconditionally */
+       geode_mfgpt_write(clock, MFGPT_REG_SETUP, (u16) ~MFGPT_SETUP_CNTEN |
+                       MFGPT_SETUP_CMP1 | MFGPT_SETUP_CMP2);
 }
 
 static int mfgpt_next_event(unsigned long, struct clock_event_device *);
@@ -253,7 +287,7 @@ static struct clock_event_device mfgpt_clockevent = {
        .set_mode = mfgpt_set_mode,
        .set_next_event = mfgpt_next_event,
        .rating = 250,
-       .cpumask = CPU_MASK_ALL,
+       .cpumask = cpu_all_mask,
        .shift = 32
 };
 
@@ -283,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);
 
@@ -309,12 +347,11 @@ static irqreturn_t mfgpt_tick(int irq, void *dev_id)
 
 static struct irqaction mfgptirq  = {
        .handler = mfgpt_tick,
-       .flags = IRQF_DISABLED | IRQF_NOBALANCING,
-       .mask = CPU_MASK_NONE,
+       .flags = IRQF_DISABLED | IRQF_NOBALANCING | IRQF_TIMER,
        .name = "mfgpt-timer"
 };
 
-static int __init mfgpt_timer_setup(void)
+int __init mfgpt_timer_setup(void)
 {
        int timer, ret;
        u16 val;
@@ -329,7 +366,7 @@ static 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;
        }
@@ -349,20 +386,22 @@ static 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;