Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[pandora-kernel.git] / drivers / i2c / busses / scx200_acb.c
index 517a050..a140e45 100644 (file)
@@ -2,6 +2,7 @@
     Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
 
     National Semiconductor SCx200 ACCESS.bus support
+    Also supports the AMD CS5535 and AMD CS5536
 
     Based on i2c-keywest.c which is:
         Copyright (c) 2001 Benjamin Herrenschmidt <benh@kernel.crashing.org>
@@ -30,7 +31,9 @@
 #include <linux/smp_lock.h>
 #include <linux/pci.h>
 #include <linux/delay.h>
+#include <linux/mutex.h>
 #include <asm/io.h>
+#include <asm/msr.h>
 
 #include <linux/scx200.h>
 
@@ -45,10 +48,7 @@ static int base[MAX_DEVICES] = { 0x820, 0x840 };
 module_param_array(base, int, NULL, 0);
 MODULE_PARM_DESC(base, "Base addresses for the ACCESS.bus controllers");
 
-/* The hardware supports interrupt driven mode too, but I haven't
-   implemented that. */
-#define POLLED_MODE 1
-#define POLL_TIMEOUT (HZ)
+#define POLL_TIMEOUT   (HZ/5)
 
 enum scx200_acb_state {
        state_idle,
@@ -75,7 +75,7 @@ struct scx200_acb_iface {
        struct scx200_acb_iface *next;
        struct i2c_adapter adapter;
        unsigned base;
-       struct semaphore sem;
+       struct mutex mutex;
 
        /* State machine data */
        enum scx200_acb_state state;
@@ -133,6 +133,9 @@ static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
 
                outb(inb(ACBCTL1) | ACBCTL1_STOP, ACBCTL1);
                outb(ACBST_STASTR | ACBST_NEGACK, ACBST);
+
+               /* Reset the status register */
+               outb(0, ACBST);
                return;
        }
 
@@ -220,7 +223,6 @@ static void scx200_acb_machine(struct scx200_acb_iface *iface, u8 status)
        iface->needs_reset = 1;
 }
 
-#ifdef POLLED_MODE
 static void scx200_acb_poll(struct scx200_acb_iface *iface)
 {
        u8 status;
@@ -229,11 +231,15 @@ static void scx200_acb_poll(struct scx200_acb_iface *iface)
        timeout = jiffies + POLL_TIMEOUT;
        while (time_before(jiffies, timeout)) {
                status = inb(ACBST);
+
+               /* Reset the status register to avoid the hang */
+               outb(0, ACBST);
+
                if ((status & (ACBST_SDAST|ACBST_BER|ACBST_NEGACK)) != 0) {
                        scx200_acb_machine(iface, status);
                        return;
                }
-               msleep(10);
+               yield();
        }
 
        dev_err(&iface->adapter.dev, "timeout in state %s\n",
@@ -243,7 +249,6 @@ static void scx200_acb_poll(struct scx200_acb_iface *iface)
        iface->result = -EIO;
        iface->needs_reset = 1;
 }
-#endif /* POLLED_MODE */
 
 static void scx200_acb_reset(struct scx200_acb_iface *iface)
 {
@@ -317,7 +322,7 @@ static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
                return -EINVAL;
        }
 
-       down(&iface->sem);
+       mutex_lock(&iface->mutex);
 
        iface->address_byte = (address << 1) | rw;
        iface->command = command;
@@ -333,19 +338,15 @@ static s32 scx200_acb_smbus_xfer(struct i2c_adapter *adapter,
        else
                iface->state = state_address;
 
-#ifdef POLLED_MODE
        while (iface->state != state_idle)
                scx200_acb_poll(iface);
-#else /* POLLED_MODE */
-#error Interrupt driven mode not implemented
-#endif /* POLLED_MODE */       
 
        if (iface->needs_reset)
                scx200_acb_reset(iface);
 
        rc = iface->result;
 
-       up(&iface->sem);
+       mutex_unlock(&iface->mutex);
 
        if (rc == 0 && size == I2C_SMBUS_WORD_DATA && rw == I2C_SMBUS_READ)
                data->word = le16_to_cpu(cur_word);
@@ -378,6 +379,7 @@ static struct i2c_algorithm scx200_acb_algorithm = {
 };
 
 static struct scx200_acb_iface *scx200_acb_list;
+static DECLARE_MUTEX(scx200_acb_list_mutex);
 
 static int scx200_acb_probe(struct scx200_acb_iface *iface)
 {
@@ -415,12 +417,11 @@ static int scx200_acb_probe(struct scx200_acb_iface *iface)
        return 0;
 }
 
-static int  __init scx200_acb_create(int base, int index)
+static int  __init scx200_acb_create(const char *text, int base, int index)
 {
        struct scx200_acb_iface *iface;
        struct i2c_adapter *adapter;
        int rc;
-       char description[64];
 
        iface = kzalloc(sizeof(*iface), GFP_KERNEL);
        if (!iface) {
@@ -431,17 +432,15 @@ static int  __init scx200_acb_create(int base, int index)
 
        adapter = &iface->adapter;
        i2c_set_adapdata(adapter, iface);
-       snprintf(adapter->name, I2C_NAME_SIZE, "SCx200 ACB%d", index);
+       snprintf(adapter->name, I2C_NAME_SIZE, "%s ACB%d", text, index);
        adapter->owner = THIS_MODULE;
        adapter->id = I2C_HW_SMBUS_SCX200;
        adapter->algo = &scx200_acb_algorithm;
        adapter->class = I2C_CLASS_HWMON;
 
-       init_MUTEX(&iface->sem);
+       mutex_init(&iface->mutex);
 
-       snprintf(description, sizeof(description),
-                "NatSemi SCx200 ACCESS.bus [%s]", adapter->name);
-       if (request_region(base, 8, description) == 0) {
+       if (!request_region(base, 8, adapter->name)) {
                printk(KERN_ERR NAME ": can't allocate io 0x%x-0x%x\n",
                        base, base + 8-1);
                rc = -EBUSY;
@@ -463,10 +462,10 @@ static int  __init scx200_acb_create(int base, int index)
                goto errout_release;
        }
 
-       lock_kernel();
+       down(&scx200_acb_list_mutex);
        iface->next = scx200_acb_list;
        scx200_acb_list = iface;
-       unlock_kernel();
+       up(&scx200_acb_list_mutex);
 
        return 0;
 
@@ -484,22 +483,51 @@ static struct pci_device_id scx200[] = {
        { },
 };
 
+static struct pci_device_id divil_pci[] = {
+       { PCI_DEVICE(PCI_VENDOR_ID_NS,  PCI_DEVICE_ID_NS_CS5535_ISA) },
+       { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA) },
+       { } /* NULL entry */
+};
+
+#define MSR_LBAR_SMB           0x5140000B
+
+static int scx200_add_cs553x(void)
+{
+       u32     low, hi;
+       u32     smb_base;
+
+       /* Grab & reserve the SMB I/O range */
+       rdmsr(MSR_LBAR_SMB, low, hi);
+
+       /* Check the IO mask and whether SMB is enabled */
+       if (hi != 0x0000F001) {
+               printk(KERN_WARNING NAME ": SMBus not enabled\n");
+               return -ENODEV;
+       }
+
+       /* SMBus IO size is 8 bytes */
+       smb_base = low & 0x0000FFF8;
+
+       return scx200_acb_create("CS5535", smb_base, 0);
+}
+
 static int __init scx200_acb_init(void)
 {
        int i;
-       int rc;
+       int     rc = -ENODEV;
 
        pr_debug(NAME ": NatSemi SCx200 ACCESS.bus Driver\n");
 
        /* Verify that this really is a SCx200 processor */
-       if (pci_dev_present(scx200) == 0)
-               return -ENODEV;
+       if (pci_dev_present(scx200)) {
+               for (i = 0; i < MAX_DEVICES; ++i) {
+                       if (base[i] > 0)
+                               rc = scx200_acb_create("SCx200", base[i], i);
+               }
+       } else if (pci_dev_present(divil_pci))
+               rc = scx200_add_cs553x();
 
-       rc = -ENXIO;
-       for (i = 0; i < MAX_DEVICES; ++i) {
-               if (base[i] > 0)
-                       rc = scx200_acb_create(base[i], i);
-       }
+       /* If at least one bus was created, init must succeed */
        if (scx200_acb_list)
                return 0;
        return rc;
@@ -509,17 +537,17 @@ static void __exit scx200_acb_cleanup(void)
 {
        struct scx200_acb_iface *iface;
 
-       lock_kernel();
+       down(&scx200_acb_list_mutex);
        while ((iface = scx200_acb_list) != NULL) {
                scx200_acb_list = iface->next;
-               unlock_kernel();
+               up(&scx200_acb_list_mutex);
 
                i2c_del_adapter(&iface->adapter);
                release_region(iface->base, 8);
                kfree(iface);
-               lock_kernel();
+               down(&scx200_acb_list_mutex);
        }
-       unlock_kernel();
+       up(&scx200_acb_list_mutex);
 }
 
 module_init(scx200_acb_init);