Merge branch 'linux-2.6'
[safe/jmp/linux-2.6] / drivers / macintosh / smu.c
index a85ac18..d409f67 100644 (file)
@@ -19,7 +19,6 @@
  *    the userland interface
  */
 
-#include <linux/config.h>
 #include <linux/types.h>
 #include <linux/kernel.h>
 #include <linux/device.h>
@@ -35,6 +34,7 @@
 #include <linux/delay.h>
 #include <linux/sysdev.h>
 #include <linux/poll.h>
+#include <linux/mutex.h>
 
 #include <asm/byteorder.h>
 #include <asm/io.h>
@@ -46,8 +46,9 @@
 #include <asm/abs_addr.h>
 #include <asm/uaccess.h>
 #include <asm/of_device.h>
+#include <asm/of_platform.h>
 
-#define VERSION "0.6"
+#define VERSION "0.7"
 #define AUTHOR  "(c) 2005 Benjamin Herrenschmidt, IBM Corp."
 
 #undef DEBUG_SMU
@@ -75,9 +76,11 @@ struct smu_device {
        struct of_device        *of_dev;
        int                     doorbell;       /* doorbell gpio */
        u32 __iomem             *db_buf;        /* doorbell buffer */
-       int                     db_irq;
+       struct device_node      *db_node;
+       unsigned int            db_irq;
        int                     msg;
-       int                     msg_irq;
+       struct device_node      *msg_node;
+       unsigned int            msg_irq;
        struct smu_cmd_buf      *cmd_buf;       /* command buffer virtual */
        u32                     cmd_buf_abs;    /* command buffer absolute */
        struct list_head        cmd_list;
@@ -92,7 +95,10 @@ struct smu_device {
  * for now, just hard code that
  */
 static struct smu_device       *smu;
+static DEFINE_MUTEX(smu_part_access);
+static int smu_irq_inited;
 
+static void smu_i2c_retry(unsigned long data);
 
 /*
  * SMU driver low level stuff
@@ -113,9 +119,11 @@ static void smu_start_cmd(void)
 
        DPRINTK("SMU: starting cmd %x, %d bytes data\n", cmd->cmd,
                cmd->data_len);
-       DPRINTK("SMU: data buffer: %02x %02x %02x %02x ...\n",
+       DPRINTK("SMU: data buffer: %02x %02x %02x %02x %02x %02x %02x %02x\n",
                ((u8 *)cmd->data_buf)[0], ((u8 *)cmd->data_buf)[1],
-               ((u8 *)cmd->data_buf)[2], ((u8 *)cmd->data_buf)[3]);
+               ((u8 *)cmd->data_buf)[2], ((u8 *)cmd->data_buf)[3],
+               ((u8 *)cmd->data_buf)[4], ((u8 *)cmd->data_buf)[5],
+               ((u8 *)cmd->data_buf)[6], ((u8 *)cmd->data_buf)[7]);
 
        /* Fill the SMU command buffer */
        smu->cmd_buf->cmd = cmd->cmd;
@@ -138,7 +146,7 @@ static void smu_start_cmd(void)
 }
 
 
-static irqreturn_t smu_db_intr(int irq, void *arg, struct pt_regs *regs)
+static irqreturn_t smu_db_intr(int irq, void *arg)
 {
        unsigned long flags;
        struct smu_cmd *cmd;
@@ -153,8 +161,10 @@ static irqreturn_t smu_db_intr(int irq, void *arg, struct pt_regs *regs)
        spin_lock_irqsave(&smu->lock, flags);
 
        gpio = pmac_do_feature_call(PMAC_FTR_READ_GPIO, NULL, smu->doorbell);
-       if ((gpio & 7) != 7)
+       if ((gpio & 7) != 7) {
+               spin_unlock_irqrestore(&smu->lock, flags);
                return IRQ_HANDLED;
+       }
 
        cmd = smu->cmd_cur;
        smu->cmd_cur = NULL;
@@ -215,7 +225,7 @@ static irqreturn_t smu_db_intr(int irq, void *arg, struct pt_regs *regs)
 }
 
 
-static irqreturn_t smu_msg_intr(int irq, void *arg, struct pt_regs *regs)
+static irqreturn_t smu_msg_intr(int irq, void *arg)
 {
        /* I don't quite know what to do with this one, we seem to never
         * receive it, so I suspect we have to arm it someway in the SMU
@@ -251,6 +261,10 @@ int smu_queue_cmd(struct smu_cmd *cmd)
                smu_start_cmd();
        spin_unlock_irqrestore(&smu->lock, flags);
 
+       /* Workaround for early calls when irq isn't available */
+       if (!smu_irq_inited || smu->db_irq == NO_IRQ)
+               smu_spinwait_cmd(cmd);
+
        return 0;
 }
 EXPORT_SYMBOL(smu_queue_cmd);
@@ -296,7 +310,7 @@ void smu_poll(void)
 
        gpio = pmac_do_feature_call(PMAC_FTR_READ_GPIO, NULL, smu->doorbell);
        if ((gpio & 7) == 7)
-               smu_db_intr(smu->db_irq, smu, NULL);
+               smu_db_intr(smu->db_irq, smu);
 }
 EXPORT_SYMBOL(smu_poll);
 
@@ -438,10 +452,10 @@ int smu_present(void)
 EXPORT_SYMBOL(smu_present);
 
 
-int smu_init (void)
+int __init smu_init (void)
 {
        struct device_node *np;
-       u32 *data;
+       const u32 *data;
 
         np = of_find_node_by_type(NULL, "smu");
         if (np == NULL)
@@ -465,7 +479,6 @@ int smu_init (void)
        smu->of_node = np;
        smu->db_irq = NO_IRQ;
        smu->msg_irq = NO_IRQ;
-       init_timer(&smu->i2c_timer);
 
        /* smu_cmdbuf_abs is in the low 2G of RAM, can be converted to a
         * 32 bits value safely
@@ -473,14 +486,15 @@ int smu_init (void)
        smu->cmd_buf_abs = (u32)smu_cmdbuf_abs;
        smu->cmd_buf = (struct smu_cmd_buf *)abs_to_virt(smu_cmdbuf_abs);
 
-       np = of_find_node_by_name(NULL, "smu-doorbell");
-       if (np == NULL) {
+       smu->db_node = of_find_node_by_name(NULL, "smu-doorbell");
+       if (smu->db_node == NULL) {
                printk(KERN_ERR "SMU: Can't find doorbell GPIO !\n");
                goto fail;
        }
-       data = (u32 *)get_property(np, "reg", NULL);
+       data = of_get_property(smu->db_node, "reg", NULL);
        if (data == NULL) {
-               of_node_put(np);
+               of_node_put(smu->db_node);
+               smu->db_node = NULL;
                printk(KERN_ERR "SMU: Can't find doorbell GPIO address !\n");
                goto fail;
        }
@@ -492,27 +506,21 @@ int smu_init (void)
        smu->doorbell = *data;
        if (smu->doorbell < 0x50)
                smu->doorbell += 0x50;
-       if (np->n_intrs > 0)
-               smu->db_irq = np->intrs[0].line;
-
-       of_node_put(np);
 
        /* Now look for the smu-interrupt GPIO */
        do {
-               np = of_find_node_by_name(NULL, "smu-interrupt");
-               if (np == NULL)
+               smu->msg_node = of_find_node_by_name(NULL, "smu-interrupt");
+               if (smu->msg_node == NULL)
                        break;
-               data = (u32 *)get_property(np, "reg", NULL);
+               data = of_get_property(smu->msg_node, "reg", NULL);
                if (data == NULL) {
-                       of_node_put(np);
+                       of_node_put(smu->msg_node);
+                       smu->msg_node = NULL;
                        break;
                }
                smu->msg = *data;
                if (smu->msg < 0x50)
                        smu->msg += 0x50;
-               if (np->n_intrs > 0)
-                       smu->msg_irq = np->intrs[0].line;
-               of_node_put(np);
        } while(0);
 
        /* Doorbell buffer is currently hard-coded, I didn't find a proper
@@ -540,13 +548,30 @@ static int smu_late_init(void)
        if (!smu)
                return 0;
 
+       init_timer(&smu->i2c_timer);
+       smu->i2c_timer.function = smu_i2c_retry;
+       smu->i2c_timer.data = (unsigned long)smu;
+
+       if (smu->db_node) {
+               smu->db_irq = irq_of_parse_and_map(smu->db_node, 0);
+               if (smu->db_irq == NO_IRQ)
+                       printk(KERN_ERR "smu: failed to map irq for node %s\n",
+                              smu->db_node->full_name);
+       }
+       if (smu->msg_node) {
+               smu->msg_irq = irq_of_parse_and_map(smu->msg_node, 0);
+               if (smu->msg_irq == NO_IRQ)
+                       printk(KERN_ERR "smu: failed to map irq for node %s\n",
+                              smu->msg_node->full_name);
+       }
+
        /*
         * Try to request the interrupts
         */
 
        if (smu->db_irq != NO_IRQ) {
                if (request_irq(smu->db_irq, smu_db_intr,
-                               SA_SHIRQ, "SMU doorbell", smu) < 0) {
+                               IRQF_SHARED, "SMU doorbell", smu) < 0) {
                        printk(KERN_WARNING "SMU: can't "
                               "request interrupt %d\n",
                               smu->db_irq);
@@ -556,7 +581,7 @@ static int smu_late_init(void)
 
        if (smu->msg_irq != NO_IRQ) {
                if (request_irq(smu->msg_irq, smu_msg_intr,
-                               SA_SHIRQ, "SMU message", smu) < 0) {
+                               IRQF_SHARED, "SMU message", smu) < 0) {
                        printk(KERN_WARNING "SMU: can't "
                               "request interrupt %d\n",
                               smu->msg_irq);
@@ -564,33 +589,29 @@ static int smu_late_init(void)
                }
        }
 
+       smu_irq_inited = 1;
        return 0;
 }
-arch_initcall(smu_late_init);
+/* This has to be before arch_initcall as the low i2c stuff relies on the
+ * above having been done before we reach arch_initcalls
+ */
+core_initcall(smu_late_init);
 
 /*
  * sysfs visibility
  */
 
-static void smu_expose_childs(void *unused)
+static void smu_expose_childs(struct work_struct *unused)
 {
        struct device_node *np;
 
-       for (np = NULL; (np = of_get_next_child(smu->of_node, np)) != NULL;) {
-               if (device_is_compatible(np, "smu-i2c")) {
-                       char name[32];
-                       u32 *reg = (u32 *)get_property(np, "reg", NULL);
-
-                       if (reg == NULL)
-                               continue;
-                       sprintf(name, "smu-i2c-%02x", *reg);
-                       of_platform_device_create(np, name, &smu->of_dev->dev);
-               }
-       }
-
+       for (np = NULL; (np = of_get_next_child(smu->of_node, np)) != NULL;)
+               if (of_device_is_compatible(np, "smu-sensors"))
+                       of_platform_device_create(np, "smu-sensors",
+                                                 &smu->of_dev->dev);
 }
 
-static DECLARE_WORK(smu_expose_childs_work, smu_expose_childs, NULL);
+static DECLARE_WORK(smu_expose_childs_work, smu_expose_childs);
 
 static int smu_platform_probe(struct of_device* dev,
                              const struct of_device_id *match)
@@ -625,8 +646,6 @@ static struct of_platform_driver smu_of_platform_driver =
 
 static int __init smu_init_sysfs(void)
 {
-       int rc;
-
        /*
         * Due to sysfs bogosity, a sysdev is not a real device, so
         * we should in fact create both if we want sysdev semantics
@@ -635,7 +654,7 @@ static int __init smu_init_sysfs(void)
         * I'm a bit too far from figuring out how that works with those
         * new chipsets, but that will come back and bite us
         */
-       rc = of_register_driver(&smu_of_platform_driver);
+       of_register_platform_driver(&smu_of_platform_driver);
        return 0;
 }
 
@@ -706,13 +725,13 @@ static void smu_i2c_complete_command(struct smu_i2c_cmd *cmd, int fail)
 
 static void smu_i2c_retry(unsigned long data)
 {
-       struct smu_i2c_cmd      *cmd = (struct smu_i2c_cmd *)data;
+       struct smu_i2c_cmd      *cmd = smu->cmd_i2c_cur;
 
        DPRINTK("SMU: i2c failure, requeuing...\n");
 
        /* requeue command simply by resetting reply_len */
        cmd->pdata[0] = 0xff;
-       cmd->scmd.reply_len = 0x10;
+       cmd->scmd.reply_len = sizeof(cmd->pdata);
        smu_queue_cmd(&cmd->scmd);
 }
 
@@ -741,10 +760,13 @@ static void smu_i2c_low_completion(struct smu_cmd *scmd, void *misc)
         */
        if (fail && --cmd->retries > 0) {
                DPRINTK("SMU: i2c failure, starting timer...\n");
-               smu->i2c_timer.function = smu_i2c_retry;
-               smu->i2c_timer.data = (unsigned long)cmd;
-               smu->i2c_timer.expires = jiffies + msecs_to_jiffies(5);
-               add_timer(&smu->i2c_timer);
+               BUG_ON(cmd != smu->cmd_i2c_cur);
+               if (!smu_irq_inited) {
+                       mdelay(5);
+                       smu_i2c_retry(0);
+                       return;
+               }
+               mod_timer(&smu->i2c_timer, jiffies + msecs_to_jiffies(5));
                return;
        }
 
@@ -758,7 +780,7 @@ static void smu_i2c_low_completion(struct smu_cmd *scmd, void *misc)
 
        /* Ok, initial command complete, now poll status */
        scmd->reply_buf = cmd->pdata;
-       scmd->reply_len = 0x10;
+       scmd->reply_len = sizeof(cmd->pdata);
        scmd->data_buf = cmd->pdata;
        scmd->data_len = 1;
        cmd->pdata[0] = 0;
@@ -780,7 +802,7 @@ int smu_queue_i2c(struct smu_i2c_cmd *cmd)
        cmd->scmd.done = smu_i2c_low_completion;
        cmd->scmd.misc = cmd;
        cmd->scmd.reply_buf = cmd->pdata;
-       cmd->scmd.reply_len = 0x10;
+       cmd->scmd.reply_len = sizeof(cmd->pdata);
        cmd->scmd.data_buf = (u8 *)(char *)&cmd->info;
        cmd->scmd.status = 1;
        cmd->stage = 0;
@@ -843,6 +865,161 @@ int smu_queue_i2c(struct smu_i2c_cmd *cmd)
        return 0;
 }
 
+/*
+ * Handling of "partitions"
+ */
+
+static int smu_read_datablock(u8 *dest, unsigned int addr, unsigned int len)
+{
+       DECLARE_COMPLETION_ONSTACK(comp);
+       unsigned int chunk;
+       struct smu_cmd cmd;
+       int rc;
+       u8 params[8];
+
+       /* We currently use a chunk size of 0xe. We could check the
+        * SMU firmware version and use bigger sizes though
+        */
+       chunk = 0xe;
+
+       while (len) {
+               unsigned int clen = min(len, chunk);
+
+               cmd.cmd = SMU_CMD_MISC_ee_COMMAND;
+               cmd.data_len = 7;
+               cmd.data_buf = params;
+               cmd.reply_len = chunk;
+               cmd.reply_buf = dest;
+               cmd.done = smu_done_complete;
+               cmd.misc = &comp;
+               params[0] = SMU_CMD_MISC_ee_GET_DATABLOCK_REC;
+               params[1] = 0x4;
+               *((u32 *)&params[2]) = addr;
+               params[6] = clen;
+
+               rc = smu_queue_cmd(&cmd);
+               if (rc)
+                       return rc;
+               wait_for_completion(&comp);
+               if (cmd.status != 0)
+                       return rc;
+               if (cmd.reply_len != clen) {
+                       printk(KERN_DEBUG "SMU: short read in "
+                              "smu_read_datablock, got: %d, want: %d\n",
+                              cmd.reply_len, clen);
+                       return -EIO;
+               }
+               len -= clen;
+               addr += clen;
+               dest += clen;
+       }
+       return 0;
+}
+
+static struct smu_sdbp_header *smu_create_sdb_partition(int id)
+{
+       DECLARE_COMPLETION_ONSTACK(comp);
+       struct smu_simple_cmd cmd;
+       unsigned int addr, len, tlen;
+       struct smu_sdbp_header *hdr;
+       struct property *prop;
+
+       /* First query the partition info */
+       DPRINTK("SMU: Query partition infos ... (irq=%d)\n", smu->db_irq);
+       smu_queue_simple(&cmd, SMU_CMD_PARTITION_COMMAND, 2,
+                        smu_done_complete, &comp,
+                        SMU_CMD_PARTITION_LATEST, id);
+       wait_for_completion(&comp);
+       DPRINTK("SMU: done, status: %d, reply_len: %d\n",
+               cmd.cmd.status, cmd.cmd.reply_len);
+
+       /* Partition doesn't exist (or other error) */
+       if (cmd.cmd.status != 0 || cmd.cmd.reply_len != 6)
+               return NULL;
+
+       /* Fetch address and length from reply */
+       addr = *((u16 *)cmd.buffer);
+       len = cmd.buffer[3] << 2;
+       /* Calucluate total length to allocate, including the 17 bytes
+        * for "sdb-partition-XX" that we append at the end of the buffer
+        */
+       tlen = sizeof(struct property) + len + 18;
+
+       prop = kzalloc(tlen, GFP_KERNEL);
+       if (prop == NULL)
+               return NULL;
+       hdr = (struct smu_sdbp_header *)(prop + 1);
+       prop->name = ((char *)prop) + tlen - 18;
+       sprintf(prop->name, "sdb-partition-%02x", id);
+       prop->length = len;
+       prop->value = hdr;
+       prop->next = NULL;
+
+       /* Read the datablock */
+       if (smu_read_datablock((u8 *)hdr, addr, len)) {
+               printk(KERN_DEBUG "SMU: datablock read failed while reading "
+                      "partition %02x !\n", id);
+               goto failure;
+       }
+
+       /* Got it, check a few things and create the property */
+       if (hdr->id != id) {
+               printk(KERN_DEBUG "SMU: Reading partition %02x and got "
+                      "%02x !\n", id, hdr->id);
+               goto failure;
+       }
+       if (prom_add_property(smu->of_node, prop)) {
+               printk(KERN_DEBUG "SMU: Failed creating sdb-partition-%02x "
+                      "property !\n", id);
+               goto failure;
+       }
+
+       return hdr;
+ failure:
+       kfree(prop);
+       return NULL;
+}
+
+/* Note: Only allowed to return error code in pointers (using ERR_PTR)
+ * when interruptible is 1
+ */
+const struct smu_sdbp_header *__smu_get_sdb_partition(int id,
+               unsigned int *size, int interruptible)
+{
+       char pname[32];
+       const struct smu_sdbp_header *part;
+
+       if (!smu)
+               return NULL;
+
+       sprintf(pname, "sdb-partition-%02x", id);
+
+       DPRINTK("smu_get_sdb_partition(%02x)\n", id);
+
+       if (interruptible) {
+               int rc;
+               rc = mutex_lock_interruptible(&smu_part_access);
+               if (rc)
+                       return ERR_PTR(rc);
+       } else
+               mutex_lock(&smu_part_access);
+
+       part = of_get_property(smu->of_node, pname, size);
+       if (part == NULL) {
+               DPRINTK("trying to extract from SMU ...\n");
+               part = smu_create_sdb_partition(id);
+               if (part != NULL && size)
+                       *size = part->len << 2;
+       }
+       mutex_unlock(&smu_part_access);
+       return part;
+}
+
+const struct smu_sdbp_header *smu_get_sdb_partition(int id, unsigned int *size)
+{
+       return __smu_get_sdb_partition(id, size, 0);
+}
+EXPORT_SYMBOL(smu_get_sdb_partition);
 
 
 /*
@@ -876,10 +1053,9 @@ static int smu_open(struct inode *inode, struct file *file)
        struct smu_private *pp;
        unsigned long flags;
 
-       pp = kmalloc(sizeof(struct smu_private), GFP_KERNEL);
+       pp = kzalloc(sizeof(struct smu_private), GFP_KERNEL);
        if (pp == 0)
                return -ENOMEM;
-       memset(pp, 0, sizeof(struct smu_private));
        spin_lock_init(&pp->lock);
        pp->mode = smu_file_commands;
        init_waitqueue_head(&pp->wait);
@@ -916,6 +1092,14 @@ static ssize_t smu_write(struct file *file, const char __user *buf,
        else if (hdr.cmdtype == SMU_CMDTYPE_WANTS_EVENTS) {
                pp->mode = smu_file_events;
                return 0;
+       } else if (hdr.cmdtype == SMU_CMDTYPE_GET_PARTITION) {
+               const struct smu_sdbp_header *part;
+               part = __smu_get_sdb_partition(hdr.cmd, NULL, 1);
+               if (part == NULL)
+                       return -EINVAL;
+               else if (IS_ERR(part))
+                       return PTR_ERR(part);
+               return 0;
        } else if (hdr.cmdtype != SMU_CMDTYPE_SMU)
                return -EINVAL;
        else if (pp->mode != smu_file_commands)
@@ -1074,9 +1258,9 @@ static int smu_release(struct inode *inode, struct file *file)
                        set_current_state(TASK_UNINTERRUPTIBLE);
                        if (pp->cmd.status != 1)
                                break;
-                       spin_lock_irqsave(&pp->lock, flags);
-                       schedule();
                        spin_unlock_irqrestore(&pp->lock, flags);
+                       schedule();
+                       spin_lock_irqsave(&pp->lock, flags);
                }
                set_current_state(TASK_RUNNING);
                remove_wait_queue(&pp->wait, &wait);
@@ -1092,7 +1276,7 @@ static int smu_release(struct inode *inode, struct file *file)
 }
 
 
-static struct file_operations smu_device_fops __pmacdata = {
+static const struct file_operations smu_device_fops = {
        .llseek         = no_llseek,
        .read           = smu_read,
        .write          = smu_write,
@@ -1101,7 +1285,7 @@ static struct file_operations smu_device_fops __pmacdata = {
        .release        = smu_release,
 };
 
-static struct miscdevice pmu_device __pmacdata = {
+static struct miscdevice pmu_device = {
        MISC_DYNAMIC_MINOR, "smu", &smu_device_fops
 };