IB: fix race in device_create
[safe/jmp/linux-2.6] / drivers / ide / pci / cmd640.c
index 662099a..aaf3810 100644 (file)
@@ -4,7 +4,7 @@
 
 /*
  *  Original authors:  abramov@cecmow.enet.dec.com (Igor Abramov)
- *                     mlord@pobox.com (Mark Lord)
+ *                     mlord@pobox.com (Mark Lord)
  *
  *  See linux/MAINTAINERS for address of current maintainer.
  *
@@ -98,7 +98,7 @@
 
 #define CMD640_PREFETCH_MASKS 1
 
-//#define CMD640_DUMP_REGS
+/*#define CMD640_DUMP_REGS */
 
 #include <linux/types.h>
 #include <linux/kernel.h>
 
 #include <asm/io.h>
 
-/*
- * This flag is set in ide.c by the parameter:  ide0=cmd640_vlb
- */
-int cmd640_vlb = 0;
+#define DRV_NAME "cmd640"
+
+static int cmd640_vlb;
 
 /*
  * CMD640 specific registers definition.
@@ -206,13 +205,13 @@ static unsigned int cmd640_chip_version;
 
 /* PCI method 1 access */
 
-static void put_cmd640_reg_pci1 (u16 reg, u8 val)
+static void put_cmd640_reg_pci1(u16 reg, u8 val)
 {
        outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
        outb_p(val, (reg & 3) | 0xcfc);
 }
 
-static u8 get_cmd640_reg_pci1 (u16 reg)
+static u8 get_cmd640_reg_pci1(u16 reg)
 {
        outl_p((reg & 0xfc) | cmd640_key, 0xcf8);
        return inb_p((reg & 3) | 0xcfc);
@@ -220,14 +219,14 @@ static u8 get_cmd640_reg_pci1 (u16 reg)
 
 /* PCI method 2 access (from CMD datasheet) */
 
-static void put_cmd640_reg_pci2 (u16 reg, u8 val)
+static void put_cmd640_reg_pci2(u16 reg, u8 val)
 {
        outb_p(0x10, 0xcf8);
        outb_p(val, cmd640_key + reg);
        outb_p(0, 0xcf8);
 }
 
-static u8 get_cmd640_reg_pci2 (u16 reg)
+static u8 get_cmd640_reg_pci2(u16 reg)
 {
        u8 b;
 
@@ -239,13 +238,13 @@ static u8 get_cmd640_reg_pci2 (u16 reg)
 
 /* VLB access */
 
-static void put_cmd640_reg_vlb (u16 reg, u8 val)
+static void put_cmd640_reg_vlb(u16 reg, u8 val)
 {
        outb_p(reg, cmd640_key);
        outb_p(val, cmd640_key + 4);
 }
 
-static u8 get_cmd640_reg_vlb (u16 reg)
+static u8 get_cmd640_reg_vlb(u16 reg)
 {
        outb_p(reg, cmd640_key);
        return inb_p(cmd640_key + 4);
@@ -267,11 +266,11 @@ static void put_cmd640_reg(u16 reg, u8 val)
        unsigned long flags;
 
        spin_lock_irqsave(&cmd640_lock, flags);
-       __put_cmd640_reg(reg,val);
+       __put_cmd640_reg(reg, val);
        spin_unlock_irqrestore(&cmd640_lock, flags);
 }
 
-static int __init match_pci_cmd640_device (void)
+static int __init match_pci_cmd640_device(void)
 {
        const u8 ven_dev[4] = {0x95, 0x10, 0x40, 0x06};
        unsigned int i;
@@ -291,7 +290,7 @@ static int __init match_pci_cmd640_device (void)
 /*
  * Probe for CMD640x -- pci method 1
  */
-static int __init probe_for_cmd640_pci1 (void)
+static int __init probe_for_cmd640_pci1(void)
 {
        __get_cmd640_reg = get_cmd640_reg_pci1;
        __put_cmd640_reg = put_cmd640_reg_pci1;
@@ -307,7 +306,7 @@ static int __init probe_for_cmd640_pci1 (void)
 /*
  * Probe for CMD640x -- pci method 2
  */
-static int __init probe_for_cmd640_pci2 (void)
+static int __init probe_for_cmd640_pci2(void)
 {
        __get_cmd640_reg = get_cmd640_reg_pci2;
        __put_cmd640_reg = put_cmd640_reg_pci2;
@@ -321,7 +320,7 @@ static int __init probe_for_cmd640_pci2 (void)
 /*
  * Probe for CMD640x -- vlb
  */
-static int __init probe_for_cmd640_vlb (void)
+static int __init probe_for_cmd640_vlb(void)
 {
        u8 b;
 
@@ -342,18 +341,18 @@ static int __init probe_for_cmd640_vlb (void)
  *  Returns 1 if an IDE interface/drive exists at 0x170,
  *  Returns 0 otherwise.
  */
-static int __init secondary_port_responding (void)
+static int __init secondary_port_responding(void)
 {
        unsigned long flags;
 
        spin_lock_irqsave(&cmd640_lock, flags);
 
-       outb_p(0x0a, 0x170 + IDE_SELECT_OFFSET);        /* select drive0 */
+       outb_p(0x0a, 0x176);    /* select drive0 */
        udelay(100);
-       if ((inb_p(0x170 + IDE_SELECT_OFFSET) & 0x1f) != 0x0a) {
-               outb_p(0x1a, 0x170 + IDE_SELECT_OFFSET); /* select drive1 */
+       if ((inb_p(0x176) & 0x1f) != 0x0a) {
+               outb_p(0x1a, 0x176); /* select drive1 */
                udelay(100);
-               if ((inb_p(0x170 + IDE_SELECT_OFFSET) & 0x1f) != 0x1a) {
+               if ((inb_p(0x176) & 0x1f) != 0x1a) {
                        spin_unlock_irqrestore(&cmd640_lock, flags);
                        return 0; /* nothing responded */
                }
@@ -366,7 +365,7 @@ static int __init secondary_port_responding (void)
 /*
  * Dump out all cmd640 registers.  May be called from ide.c
  */
-static void cmd640_dump_regs (void)
+static void cmd640_dump_regs(void)
 {
        unsigned int reg = cmd640_vlb ? 0x50 : 0x00;
 
@@ -381,6 +380,7 @@ static void cmd640_dump_regs (void)
 }
 #endif
 
+#ifndef CONFIG_BLK_DEV_CMD640_ENHANCED
 /*
  * Check whether prefetch is on for a drive,
  * and initialize the unmask flags for safe operation.
@@ -401,9 +401,7 @@ static void __init check_prefetch(ide_drive_t *drive, unsigned int index)
                drive->no_io_32bit = 0;
        }
 }
-
-#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
-
+#else
 /*
  * Sets prefetch mode for a drive.
  */
@@ -435,7 +433,7 @@ static void set_prefetch_mode(ide_drive_t *drive, unsigned int index, int mode)
 /*
  * Dump out current drive clocks settings
  */
-static void display_clocks (unsigned int index)
+static void display_clocks(unsigned int index)
 {
        u8 active_count, recovery_count;
 
@@ -454,40 +452,12 @@ static void display_clocks (unsigned int index)
  * Pack active and recovery counts into single byte representation
  * used by controller
  */
-static inline u8 pack_nibbles (u8 upper, u8 lower)
+static inline u8 pack_nibbles(u8 upper, u8 lower)
 {
        return ((upper & 0x0f) << 4) | (lower & 0x0f);
 }
 
 /*
- * This routine retrieves the initial drive timings from the chipset.
- */
-static void __init retrieve_drive_counts (unsigned int index)
-{
-       u8 b;
-
-       /*
-        * Get the internal setup timing, and convert to clock count
-        */
-       b = get_cmd640_reg(arttim_regs[index]) & ~0x3f;
-       switch (b) {
-               case 0x00: b = 4; break;
-               case 0x80: b = 3; break;
-               case 0x40: b = 2; break;
-               default:   b = 5; break;
-       }
-       setup_counts[index] = b;
-
-       /*
-        * Get the active/recovery counts
-        */
-       b = get_cmd640_reg(drwtim_regs[index]);
-       active_counts[index]   = (b >> 4)   ? (b >> 4)   : 0x10;
-       recovery_counts[index] = (b & 0x0f) ? (b & 0x0f) : 0x10;
-}
-
-
-/*
  * This routine writes the prepared setup/active/recovery counts
  * for a drive into the cmd640 chipset registers to active them.
  */
@@ -523,11 +493,11 @@ static void program_drive_counts(ide_drive_t *drive, unsigned int index)
         * Convert setup_count to internal chipset representation
         */
        switch (setup_count) {
-               case 4:  setup_count = 0x00; break;
-               case 3:  setup_count = 0x80; break;
-               case 1:
-               case 2:  setup_count = 0x40; break;
-               default: setup_count = 0xc0; /* case 5 */
+       case 4:  setup_count = 0x00; break;
+       case 3:  setup_count = 0x80; break;
+       case 1:
+       case 2:  setup_count = 0x40; break;
+       default: setup_count = 0xc0; /* case 5 */
        }
 
        /*
@@ -553,7 +523,14 @@ static void cmd640_set_mode(ide_drive_t *drive, unsigned int index,
 {
        int setup_time, active_time, recovery_time, clock_time;
        u8 setup_count, active_count, recovery_count, recovery_count2, cycle_count;
-       int bus_speed = system_bus_clock();
+       int bus_speed;
+
+       if (cmd640_vlb && ide_vlb_clk)
+               bus_speed = ide_vlb_clk;
+       else if (!cmd640_vlb && ide_pci_clk)
+               bus_speed = ide_pci_clk;
+       else
+               bus_speed = system_bus_clock();
 
        if (pio_mode > 5)
                pio_mode = 5;
@@ -607,20 +584,21 @@ static void cmd640_set_pio_mode(ide_drive_t *drive, const u8 pio)
        u8 b;
 
        switch (pio) {
-               case 6: /* set fast-devsel off */
-               case 7: /* set fast-devsel on */
-                       b = get_cmd640_reg(CNTRL) & ~0x27;
-                       if (pio & 1)
-                               b |= 0x27;
-                       put_cmd640_reg(CNTRL, b);
-                       printk("%s: %sabled cmd640 fast host timing (devsel)\n", drive->name, (pio & 1) ? "en" : "dis");
-                       return;
-
-               case 8: /* set prefetch off */
-               case 9: /* set prefetch on */
-                       set_prefetch_mode(drive, index, pio & 1);
-                       printk("%s: %sabled cmd640 prefetch\n", drive->name, (pio & 1) ? "en" : "dis");
-                       return;
+       case 6: /* set fast-devsel off */
+       case 7: /* set fast-devsel on */
+               b = get_cmd640_reg(CNTRL) & ~0x27;
+               if (pio & 1)
+                       b |= 0x27;
+               put_cmd640_reg(CNTRL, b);
+               printk("%s: %sabled cmd640 fast host timing (devsel)\n",
+                       drive->name, (pio & 1) ? "en" : "dis");
+               return;
+       case 8: /* set prefetch off */
+       case 9: /* set prefetch on */
+               set_prefetch_mode(drive, index, pio & 1);
+               printk("%s: %sabled cmd640 prefetch\n",
+                       drive->name, (pio & 1) ? "en" : "dis");
+               return;
        }
 
        cycle_time = ide_pio_cycle_time(drive, pio);
@@ -632,6 +610,9 @@ static void cmd640_set_pio_mode(ide_drive_t *drive, const u8 pio)
        display_clocks(index);
 }
 
+static const struct ide_port_ops cmd640_port_ops = {
+       .set_pio_mode           = cmd640_set_pio_mode,
+};
 #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
 
 static int pci_conf1(void)
@@ -673,14 +654,32 @@ static const struct ide_port_info cmd640_port_info __initdata = {
        .chipset                = ide_cmd640,
        .host_flags             = IDE_HFLAG_SERIALIZE |
                                  IDE_HFLAG_NO_DMA |
-                                 IDE_HFLAG_NO_AUTOTUNE |
                                  IDE_HFLAG_ABUSE_PREFETCH |
                                  IDE_HFLAG_ABUSE_FAST_DEVSEL,
 #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
+       .port_ops               = &cmd640_port_ops,
        .pio_mask               = ATA_PIO5,
 #endif
 };
 
+static int cmd640x_init_one(unsigned long base, unsigned long ctl)
+{
+       if (!request_region(base, 8, DRV_NAME)) {
+               printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
+                               DRV_NAME, base, base + 7);
+               return -EBUSY;
+       }
+
+       if (!request_region(ctl, 1, DRV_NAME)) {
+               printk(KERN_ERR "%s: I/O resource 0x%lX not free.\n",
+                               DRV_NAME, ctl);
+               release_region(base, 8);
+               return -EBUSY;
+       }
+
+       return 0;
+}
+
 /*
  * Probe for a cmd640 chipset, and initialize it if found.
  */
@@ -689,7 +688,7 @@ static int __init cmd640x_init(void)
 #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
        int second_port_toggled = 0;
 #endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
-       int second_port_cmd640 = 0;
+       int second_port_cmd640 = 0, rc;
        const char *bus_type, *port2;
        unsigned int index;
        u8 b, cfr;
@@ -729,10 +728,21 @@ static int __init cmd640x_init(void)
        cfr = get_cmd640_reg(CFR);
        cmd640_chip_version = cfr & CFR_DEVREV;
        if (cmd640_chip_version == 0) {
-               printk ("ide: bad cmd640 revision: %d\n", cmd640_chip_version);
+               printk("ide: bad cmd640 revision: %d\n", cmd640_chip_version);
                return 0;
        }
 
+       rc = cmd640x_init_one(0x1f0, 0x3f6);
+       if (rc)
+               return rc;
+
+       rc = cmd640x_init_one(0x170, 0x376);
+       if (rc) {
+               release_region(0x3f6, 1);
+               release_region(0x1f0, 8);
+               return rc;
+       }
+
        memset(&hw, 0, sizeof(hw));
 
        ide_std_init_ports(&hw[0], 0x1f0, 0x3f6);
@@ -751,10 +761,6 @@ static int __init cmd640x_init(void)
         */
        if (cmd_hwif0) {
                ide_init_port_hw(cmd_hwif0, &hw[0]);
-#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
-               cmd_hwif0->set_pio_mode = &cmd640_set_pio_mode;
-#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
-
                idx[0] = cmd_hwif0->index;
        }
 
@@ -807,10 +813,6 @@ static int __init cmd640x_init(void)
         */
        if (second_port_cmd640 && cmd_hwif1) {
                ide_init_port_hw(cmd_hwif1, &hw[1]);
-#ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
-               cmd_hwif1->set_pio_mode = &cmd640_set_pio_mode;
-#endif /* CONFIG_BLK_DEV_CMD640_ENHANCED */
-
                idx[1] = cmd_hwif1->index;
        }
        printk(KERN_INFO "cmd640: %sserialized, secondary interface %s\n",
@@ -834,28 +836,16 @@ static int __init cmd640x_init(void)
                }
 
 #ifdef CONFIG_BLK_DEV_CMD640_ENHANCED
-               if (drive->autotune || ((index > 1) && second_port_toggled)) {
-                       /*
-                        * Reset timing to the slowest speed and turn off prefetch.
-                        * This way, the drive identify code has a better chance.
-                        */
-                       setup_counts    [index] = 4;    /* max possible */
-                       active_counts   [index] = 16;   /* max possible */
-                       recovery_counts [index] = 16;   /* max possible */
-                       program_drive_counts(drive, index);
-                       set_prefetch_mode(drive, index, 0);
-                       printk("cmd640: drive%d timings/prefetch cleared\n", index);
-               } else {
-                       /*
-                        * Record timings/prefetch without changing them.
-                        * This preserves any prior BIOS setup.
-                        */
-                       retrieve_drive_counts (index);
-                       check_prefetch(drive, index);
-                       printk("cmd640: drive%d timings/prefetch(%s) preserved",
-                               index, drive->no_io_32bit ? "off" : "on");
-                       display_clocks(index);
-               }
+               /*
+                * Reset timing to the slowest speed and turn off prefetch.
+                * This way, the drive identify code has a better chance.
+                */
+               setup_counts    [index] = 4;    /* max possible */
+               active_counts   [index] = 16;   /* max possible */
+               recovery_counts [index] = 16;   /* max possible */
+               program_drive_counts(drive, index);
+               set_prefetch_mode(drive, index, 0);
+               printk("cmd640: drive%d timings/prefetch cleared\n", index);
 #else
                /*
                 * Set the drive unmask flags to match the prefetch setting