2 * PowerMac G5 SMU driver
4 * Copyright 2004 J. Mayer <l_indien@magic.fr>
5 * Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
7 * Released under the term of the GNU GPL v2.
12 * - maybe add timeout to commands ?
13 * - blocking version of time functions
14 * - polling version of i2c commands (including timer that works with
16 * - maybe avoid some data copies with i2c by directly using the smu cmd
17 * buffer and a lower level internal interface
18 * - understand SMU -> CPU events and implement reception of them via
19 * the userland interface
22 #include <linux/config.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/device.h>
26 #include <linux/dmapool.h>
27 #include <linux/bootmem.h>
28 #include <linux/vmalloc.h>
29 #include <linux/highmem.h>
30 #include <linux/jiffies.h>
31 #include <linux/interrupt.h>
32 #include <linux/rtc.h>
33 #include <linux/completion.h>
34 #include <linux/miscdevice.h>
35 #include <linux/delay.h>
36 #include <linux/sysdev.h>
37 #include <linux/poll.h>
38 #include <linux/mutex.h>
40 #include <asm/byteorder.h>
43 #include <asm/machdep.h>
44 #include <asm/pmac_feature.h>
46 #include <asm/sections.h>
47 #include <asm/abs_addr.h>
48 #include <asm/uaccess.h>
49 #include <asm/of_device.h>
52 #define AUTHOR "(c) 2005 Benjamin Herrenschmidt, IBM Corp."
57 #define DPRINTK(fmt, args...) do { printk(KERN_DEBUG fmt , ##args); } while (0)
59 #define DPRINTK(fmt, args...) do { } while (0)
63 * This is the command buffer passed to the SMU hardware
65 #define SMU_MAX_DATA 254
70 u8 data[SMU_MAX_DATA];
75 struct device_node *of_node;
76 struct of_device *of_dev;
77 int doorbell; /* doorbell gpio */
78 u32 __iomem *db_buf; /* doorbell buffer */
82 struct smu_cmd_buf *cmd_buf; /* command buffer virtual */
83 u32 cmd_buf_abs; /* command buffer absolute */
84 struct list_head cmd_list;
85 struct smu_cmd *cmd_cur; /* pending command */
86 struct list_head cmd_i2c_list;
87 struct smu_i2c_cmd *cmd_i2c_cur; /* pending i2c command */
88 struct timer_list i2c_timer;
92 * I don't think there will ever be more than one SMU, so
93 * for now, just hard code that
95 static struct smu_device *smu;
96 static DEFINE_MUTEX(smu_part_access);
98 static void smu_i2c_retry(unsigned long data);
101 * SMU driver low level stuff
104 static void smu_start_cmd(void)
106 unsigned long faddr, fend;
109 if (list_empty(&smu->cmd_list))
112 /* Fetch first command in queue */
113 cmd = list_entry(smu->cmd_list.next, struct smu_cmd, link);
115 list_del(&cmd->link);
117 DPRINTK("SMU: starting cmd %x, %d bytes data\n", cmd->cmd,
119 DPRINTK("SMU: data buffer: %02x %02x %02x %02x %02x %02x %02x %02x\n",
120 ((u8 *)cmd->data_buf)[0], ((u8 *)cmd->data_buf)[1],
121 ((u8 *)cmd->data_buf)[2], ((u8 *)cmd->data_buf)[3],
122 ((u8 *)cmd->data_buf)[4], ((u8 *)cmd->data_buf)[5],
123 ((u8 *)cmd->data_buf)[6], ((u8 *)cmd->data_buf)[7]);
125 /* Fill the SMU command buffer */
126 smu->cmd_buf->cmd = cmd->cmd;
127 smu->cmd_buf->length = cmd->data_len;
128 memcpy(smu->cmd_buf->data, cmd->data_buf, cmd->data_len);
130 /* Flush command and data to RAM */
131 faddr = (unsigned long)smu->cmd_buf;
132 fend = faddr + smu->cmd_buf->length + 2;
133 flush_inval_dcache_range(faddr, fend);
135 /* This isn't exactly a DMA mapping here, I suspect
136 * the SMU is actually communicating with us via i2c to the
137 * northbridge or the CPU to access RAM.
139 writel(smu->cmd_buf_abs, smu->db_buf);
141 /* Ring the SMU doorbell */
142 pmac_do_feature_call(PMAC_FTR_WRITE_GPIO, NULL, smu->doorbell, 4);
146 static irqreturn_t smu_db_intr(int irq, void *arg, struct pt_regs *regs)
150 void (*done)(struct smu_cmd *cmd, void *misc) = NULL;
155 /* SMU completed the command, well, we hope, let's make sure
158 spin_lock_irqsave(&smu->lock, flags);
160 gpio = pmac_do_feature_call(PMAC_FTR_READ_GPIO, NULL, smu->doorbell);
161 if ((gpio & 7) != 7) {
162 spin_unlock_irqrestore(&smu->lock, flags);
176 /* CPU might have brought back the cache line, so we need
177 * to flush again before peeking at the SMU response. We
178 * flush the entire buffer for now as we haven't read the
179 * reply lenght (it's only 2 cache lines anyway)
181 faddr = (unsigned long)smu->cmd_buf;
182 flush_inval_dcache_range(faddr, faddr + 256);
185 ack = (~cmd->cmd) & 0xff;
186 if (ack != smu->cmd_buf->cmd) {
187 DPRINTK("SMU: incorrect ack, want %x got %x\n",
188 ack, smu->cmd_buf->cmd);
191 reply_len = rc == 0 ? smu->cmd_buf->length : 0;
192 DPRINTK("SMU: reply len: %d\n", reply_len);
193 if (reply_len > cmd->reply_len) {
194 printk(KERN_WARNING "SMU: reply buffer too small,"
195 "got %d bytes for a %d bytes buffer\n",
196 reply_len, cmd->reply_len);
197 reply_len = cmd->reply_len;
199 cmd->reply_len = reply_len;
200 if (cmd->reply_buf && reply_len)
201 memcpy(cmd->reply_buf, smu->cmd_buf->data, reply_len);
204 /* Now complete the command. Write status last in order as we lost
205 * ownership of the command structure as soon as it's no longer -1
212 /* Start next command if any */
214 spin_unlock_irqrestore(&smu->lock, flags);
216 /* Call command completion handler if any */
220 /* It's an edge interrupt, nothing to do */
225 static irqreturn_t smu_msg_intr(int irq, void *arg, struct pt_regs *regs)
227 /* I don't quite know what to do with this one, we seem to never
228 * receive it, so I suspect we have to arm it someway in the SMU
229 * to start getting events that way.
232 printk(KERN_INFO "SMU: message interrupt !\n");
234 /* It's an edge interrupt, nothing to do */
240 * Queued command management.
244 int smu_queue_cmd(struct smu_cmd *cmd)
250 if (cmd->data_len > SMU_MAX_DATA ||
251 cmd->reply_len > SMU_MAX_DATA)
255 spin_lock_irqsave(&smu->lock, flags);
256 list_add_tail(&cmd->link, &smu->cmd_list);
257 if (smu->cmd_cur == NULL)
259 spin_unlock_irqrestore(&smu->lock, flags);
263 EXPORT_SYMBOL(smu_queue_cmd);
266 int smu_queue_simple(struct smu_simple_cmd *scmd, u8 command,
267 unsigned int data_len,
268 void (*done)(struct smu_cmd *cmd, void *misc),
271 struct smu_cmd *cmd = &scmd->cmd;
275 if (data_len > sizeof(scmd->buffer))
278 memset(scmd, 0, sizeof(*scmd));
280 cmd->data_len = data_len;
281 cmd->data_buf = scmd->buffer;
282 cmd->reply_len = sizeof(scmd->buffer);
283 cmd->reply_buf = scmd->buffer;
287 va_start(list, misc);
288 for (i = 0; i < data_len; ++i)
289 scmd->buffer[i] = (u8)va_arg(list, int);
292 return smu_queue_cmd(cmd);
294 EXPORT_SYMBOL(smu_queue_simple);
304 gpio = pmac_do_feature_call(PMAC_FTR_READ_GPIO, NULL, smu->doorbell);
306 smu_db_intr(smu->db_irq, smu, NULL);
308 EXPORT_SYMBOL(smu_poll);
311 void smu_done_complete(struct smu_cmd *cmd, void *misc)
313 struct completion *comp = misc;
317 EXPORT_SYMBOL(smu_done_complete);
320 void smu_spinwait_cmd(struct smu_cmd *cmd)
322 while(cmd->status == 1)
325 EXPORT_SYMBOL(smu_spinwait_cmd);
328 /* RTC low level commands */
329 static inline int bcd2hex (int n)
331 return (((n & 0xf0) >> 4) * 10) + (n & 0xf);
335 static inline int hex2bcd (int n)
337 return ((n / 10) << 4) + (n % 10);
341 static inline void smu_fill_set_rtc_cmd(struct smu_cmd_buf *cmd_buf,
342 struct rtc_time *time)
346 cmd_buf->data[0] = 0x80;
347 cmd_buf->data[1] = hex2bcd(time->tm_sec);
348 cmd_buf->data[2] = hex2bcd(time->tm_min);
349 cmd_buf->data[3] = hex2bcd(time->tm_hour);
350 cmd_buf->data[4] = time->tm_wday;
351 cmd_buf->data[5] = hex2bcd(time->tm_mday);
352 cmd_buf->data[6] = hex2bcd(time->tm_mon) + 1;
353 cmd_buf->data[7] = hex2bcd(time->tm_year - 100);
357 int smu_get_rtc_time(struct rtc_time *time, int spinwait)
359 struct smu_simple_cmd cmd;
365 memset(time, 0, sizeof(struct rtc_time));
366 rc = smu_queue_simple(&cmd, SMU_CMD_RTC_COMMAND, 1, NULL, NULL,
367 SMU_CMD_RTC_GET_DATETIME);
370 smu_spinwait_simple(&cmd);
372 time->tm_sec = bcd2hex(cmd.buffer[0]);
373 time->tm_min = bcd2hex(cmd.buffer[1]);
374 time->tm_hour = bcd2hex(cmd.buffer[2]);
375 time->tm_wday = bcd2hex(cmd.buffer[3]);
376 time->tm_mday = bcd2hex(cmd.buffer[4]);
377 time->tm_mon = bcd2hex(cmd.buffer[5]) - 1;
378 time->tm_year = bcd2hex(cmd.buffer[6]) + 100;
384 int smu_set_rtc_time(struct rtc_time *time, int spinwait)
386 struct smu_simple_cmd cmd;
392 rc = smu_queue_simple(&cmd, SMU_CMD_RTC_COMMAND, 8, NULL, NULL,
393 SMU_CMD_RTC_SET_DATETIME,
394 hex2bcd(time->tm_sec),
395 hex2bcd(time->tm_min),
396 hex2bcd(time->tm_hour),
398 hex2bcd(time->tm_mday),
399 hex2bcd(time->tm_mon) + 1,
400 hex2bcd(time->tm_year - 100));
403 smu_spinwait_simple(&cmd);
409 void smu_shutdown(void)
411 struct smu_simple_cmd cmd;
416 if (smu_queue_simple(&cmd, SMU_CMD_POWER_COMMAND, 9, NULL, NULL,
417 'S', 'H', 'U', 'T', 'D', 'O', 'W', 'N', 0))
419 smu_spinwait_simple(&cmd);
425 void smu_restart(void)
427 struct smu_simple_cmd cmd;
432 if (smu_queue_simple(&cmd, SMU_CMD_POWER_COMMAND, 8, NULL, NULL,
433 'R', 'E', 'S', 'T', 'A', 'R', 'T', 0))
435 smu_spinwait_simple(&cmd);
441 int smu_present(void)
445 EXPORT_SYMBOL(smu_present);
448 int __init smu_init (void)
450 struct device_node *np;
453 np = of_find_node_by_type(NULL, "smu");
457 printk(KERN_INFO "SMU driver %s %s\n", VERSION, AUTHOR);
459 if (smu_cmdbuf_abs == 0) {
460 printk(KERN_ERR "SMU: Command buffer not allocated !\n");
464 smu = alloc_bootmem(sizeof(struct smu_device));
467 memset(smu, 0, sizeof(*smu));
469 spin_lock_init(&smu->lock);
470 INIT_LIST_HEAD(&smu->cmd_list);
471 INIT_LIST_HEAD(&smu->cmd_i2c_list);
473 smu->db_irq = NO_IRQ;
474 smu->msg_irq = NO_IRQ;
476 /* smu_cmdbuf_abs is in the low 2G of RAM, can be converted to a
477 * 32 bits value safely
479 smu->cmd_buf_abs = (u32)smu_cmdbuf_abs;
480 smu->cmd_buf = (struct smu_cmd_buf *)abs_to_virt(smu_cmdbuf_abs);
482 np = of_find_node_by_name(NULL, "smu-doorbell");
484 printk(KERN_ERR "SMU: Can't find doorbell GPIO !\n");
487 data = (u32 *)get_property(np, "reg", NULL);
490 printk(KERN_ERR "SMU: Can't find doorbell GPIO address !\n");
494 /* Current setup has one doorbell GPIO that does both doorbell
495 * and ack. GPIOs are at 0x50, best would be to find that out
496 * in the device-tree though.
498 smu->doorbell = *data;
499 if (smu->doorbell < 0x50)
500 smu->doorbell += 0x50;
502 smu->db_irq = np->intrs[0].line;
506 /* Now look for the smu-interrupt GPIO */
508 np = of_find_node_by_name(NULL, "smu-interrupt");
511 data = (u32 *)get_property(np, "reg", NULL);
520 smu->msg_irq = np->intrs[0].line;
524 /* Doorbell buffer is currently hard-coded, I didn't find a proper
525 * device-tree entry giving the address. Best would probably to use
526 * an offset for K2 base though, but let's do it that way for now.
528 smu->db_buf = ioremap(0x8000860c, 0x1000);
529 if (smu->db_buf == NULL) {
530 printk(KERN_ERR "SMU: Can't map doorbell buffer pointer !\n");
534 sys_ctrler = SYS_CTRLER_SMU;
544 static int smu_late_init(void)
549 init_timer(&smu->i2c_timer);
550 smu->i2c_timer.function = smu_i2c_retry;
551 smu->i2c_timer.data = (unsigned long)smu;
554 * Try to request the interrupts
557 if (smu->db_irq != NO_IRQ) {
558 if (request_irq(smu->db_irq, smu_db_intr,
559 SA_SHIRQ, "SMU doorbell", smu) < 0) {
560 printk(KERN_WARNING "SMU: can't "
561 "request interrupt %d\n",
563 smu->db_irq = NO_IRQ;
567 if (smu->msg_irq != NO_IRQ) {
568 if (request_irq(smu->msg_irq, smu_msg_intr,
569 SA_SHIRQ, "SMU message", smu) < 0) {
570 printk(KERN_WARNING "SMU: can't "
571 "request interrupt %d\n",
573 smu->msg_irq = NO_IRQ;
579 /* This has to be before arch_initcall as the low i2c stuff relies on the
580 * above having been done before we reach arch_initcalls
582 core_initcall(smu_late_init);
588 static void smu_expose_childs(void *unused)
590 struct device_node *np;
592 for (np = NULL; (np = of_get_next_child(smu->of_node, np)) != NULL;)
593 if (device_is_compatible(np, "smu-sensors"))
594 of_platform_device_create(np, "smu-sensors",
598 static DECLARE_WORK(smu_expose_childs_work, smu_expose_childs, NULL);
600 static int smu_platform_probe(struct of_device* dev,
601 const struct of_device_id *match)
608 * Ok, we are matched, now expose all i2c busses. We have to defer
609 * that unfortunately or it would deadlock inside the device model
611 schedule_work(&smu_expose_childs_work);
616 static struct of_device_id smu_platform_match[] =
624 static struct of_platform_driver smu_of_platform_driver =
627 .match_table = smu_platform_match,
628 .probe = smu_platform_probe,
631 static int __init smu_init_sysfs(void)
634 * Due to sysfs bogosity, a sysdev is not a real device, so
635 * we should in fact create both if we want sysdev semantics
636 * for power management.
637 * For now, we don't power manage machines with an SMU chip,
638 * I'm a bit too far from figuring out how that works with those
639 * new chipsets, but that will come back and bite us
641 of_register_driver(&smu_of_platform_driver);
645 device_initcall(smu_init_sysfs);
647 struct of_device *smu_get_ofdev(void)
654 EXPORT_SYMBOL_GPL(smu_get_ofdev);
660 static void smu_i2c_complete_command(struct smu_i2c_cmd *cmd, int fail)
662 void (*done)(struct smu_i2c_cmd *cmd, void *misc) = cmd->done;
663 void *misc = cmd->misc;
666 /* Check for read case */
667 if (!fail && cmd->read) {
668 if (cmd->pdata[0] < 1)
671 memcpy(cmd->info.data, &cmd->pdata[1],
675 DPRINTK("SMU: completing, success: %d\n", !fail);
677 /* Update status and mark no pending i2c command with lock
678 * held so nobody comes in while we dequeue an eventual
679 * pending next i2c command
681 spin_lock_irqsave(&smu->lock, flags);
682 smu->cmd_i2c_cur = NULL;
684 cmd->status = fail ? -EIO : 0;
686 /* Is there another i2c command waiting ? */
687 if (!list_empty(&smu->cmd_i2c_list)) {
688 struct smu_i2c_cmd *newcmd;
690 /* Fetch it, new current, remove from list */
691 newcmd = list_entry(smu->cmd_i2c_list.next,
692 struct smu_i2c_cmd, link);
693 smu->cmd_i2c_cur = newcmd;
694 list_del(&cmd->link);
696 /* Queue with low level smu */
697 list_add_tail(&cmd->scmd.link, &smu->cmd_list);
698 if (smu->cmd_cur == NULL)
701 spin_unlock_irqrestore(&smu->lock, flags);
703 /* Call command completion handler if any */
710 static void smu_i2c_retry(unsigned long data)
712 struct smu_i2c_cmd *cmd = smu->cmd_i2c_cur;
714 DPRINTK("SMU: i2c failure, requeuing...\n");
716 /* requeue command simply by resetting reply_len */
717 cmd->pdata[0] = 0xff;
718 cmd->scmd.reply_len = sizeof(cmd->pdata);
719 smu_queue_cmd(&cmd->scmd);
723 static void smu_i2c_low_completion(struct smu_cmd *scmd, void *misc)
725 struct smu_i2c_cmd *cmd = misc;
728 DPRINTK("SMU: i2c compl. stage=%d status=%x pdata[0]=%x rlen: %x\n",
729 cmd->stage, scmd->status, cmd->pdata[0], scmd->reply_len);
731 /* Check for possible status */
732 if (scmd->status < 0)
734 else if (cmd->read) {
736 fail = cmd->pdata[0] != 0;
738 fail = cmd->pdata[0] >= 0x80;
740 fail = cmd->pdata[0] != 0;
743 /* Handle failures by requeuing command, after 5ms interval
745 if (fail && --cmd->retries > 0) {
746 DPRINTK("SMU: i2c failure, starting timer...\n");
747 BUG_ON(cmd != smu->cmd_i2c_cur);
748 mod_timer(&smu->i2c_timer, jiffies + msecs_to_jiffies(5));
752 /* If failure or stage 1, command is complete */
753 if (fail || cmd->stage != 0) {
754 smu_i2c_complete_command(cmd, fail);
758 DPRINTK("SMU: going to stage 1\n");
760 /* Ok, initial command complete, now poll status */
761 scmd->reply_buf = cmd->pdata;
762 scmd->reply_len = sizeof(cmd->pdata);
763 scmd->data_buf = cmd->pdata;
772 int smu_queue_i2c(struct smu_i2c_cmd *cmd)
779 /* Fill most fields of scmd */
780 cmd->scmd.cmd = SMU_CMD_I2C_COMMAND;
781 cmd->scmd.done = smu_i2c_low_completion;
782 cmd->scmd.misc = cmd;
783 cmd->scmd.reply_buf = cmd->pdata;
784 cmd->scmd.reply_len = sizeof(cmd->pdata);
785 cmd->scmd.data_buf = (u8 *)(char *)&cmd->info;
786 cmd->scmd.status = 1;
788 cmd->pdata[0] = 0xff;
792 /* Check transfer type, sanitize some "info" fields
793 * based on transfer type and do more checking
795 cmd->info.caddr = cmd->info.devaddr;
796 cmd->read = cmd->info.devaddr & 0x01;
797 switch(cmd->info.type) {
798 case SMU_I2C_TRANSFER_SIMPLE:
799 memset(&cmd->info.sublen, 0, 4);
801 case SMU_I2C_TRANSFER_COMBINED:
802 cmd->info.devaddr &= 0xfe;
803 case SMU_I2C_TRANSFER_STDSUB:
804 if (cmd->info.sublen > 3)
811 /* Finish setting up command based on transfer direction
814 if (cmd->info.datalen > SMU_I2C_READ_MAX)
816 memset(cmd->info.data, 0xff, cmd->info.datalen);
817 cmd->scmd.data_len = 9;
819 if (cmd->info.datalen > SMU_I2C_WRITE_MAX)
821 cmd->scmd.data_len = 9 + cmd->info.datalen;
824 DPRINTK("SMU: i2c enqueuing command\n");
825 DPRINTK("SMU: %s, len=%d bus=%x addr=%x sub0=%x type=%x\n",
826 cmd->read ? "read" : "write", cmd->info.datalen,
827 cmd->info.bus, cmd->info.caddr,
828 cmd->info.subaddr[0], cmd->info.type);
831 /* Enqueue command in i2c list, and if empty, enqueue also in
834 spin_lock_irqsave(&smu->lock, flags);
835 if (smu->cmd_i2c_cur == NULL) {
836 smu->cmd_i2c_cur = cmd;
837 list_add_tail(&cmd->scmd.link, &smu->cmd_list);
838 if (smu->cmd_cur == NULL)
841 list_add_tail(&cmd->link, &smu->cmd_i2c_list);
842 spin_unlock_irqrestore(&smu->lock, flags);
848 * Handling of "partitions"
851 static int smu_read_datablock(u8 *dest, unsigned int addr, unsigned int len)
853 DECLARE_COMPLETION(comp);
859 /* We currently use a chunk size of 0xe. We could check the
860 * SMU firmware version and use bigger sizes though
865 unsigned int clen = min(len, chunk);
867 cmd.cmd = SMU_CMD_MISC_ee_COMMAND;
869 cmd.data_buf = params;
870 cmd.reply_len = chunk;
871 cmd.reply_buf = dest;
872 cmd.done = smu_done_complete;
874 params[0] = SMU_CMD_MISC_ee_GET_DATABLOCK_REC;
876 *((u32 *)¶ms[2]) = addr;
879 rc = smu_queue_cmd(&cmd);
882 wait_for_completion(&comp);
885 if (cmd.reply_len != clen) {
886 printk(KERN_DEBUG "SMU: short read in "
887 "smu_read_datablock, got: %d, want: %d\n",
888 cmd.reply_len, clen);
898 static struct smu_sdbp_header *smu_create_sdb_partition(int id)
900 DECLARE_COMPLETION(comp);
901 struct smu_simple_cmd cmd;
902 unsigned int addr, len, tlen;
903 struct smu_sdbp_header *hdr;
904 struct property *prop;
906 /* First query the partition info */
907 DPRINTK("SMU: Query partition infos ... (irq=%d)\n", smu->db_irq);
908 smu_queue_simple(&cmd, SMU_CMD_PARTITION_COMMAND, 2,
909 smu_done_complete, &comp,
910 SMU_CMD_PARTITION_LATEST, id);
911 wait_for_completion(&comp);
912 DPRINTK("SMU: done, status: %d, reply_len: %d\n",
913 cmd.cmd.status, cmd.cmd.reply_len);
915 /* Partition doesn't exist (or other error) */
916 if (cmd.cmd.status != 0 || cmd.cmd.reply_len != 6)
919 /* Fetch address and length from reply */
920 addr = *((u16 *)cmd.buffer);
921 len = cmd.buffer[3] << 2;
922 /* Calucluate total length to allocate, including the 17 bytes
923 * for "sdb-partition-XX" that we append at the end of the buffer
925 tlen = sizeof(struct property) + len + 18;
927 prop = kcalloc(tlen, 1, GFP_KERNEL);
930 hdr = (struct smu_sdbp_header *)(prop + 1);
931 prop->name = ((char *)prop) + tlen - 18;
932 sprintf(prop->name, "sdb-partition-%02x", id);
934 prop->value = (unsigned char *)hdr;
937 /* Read the datablock */
938 if (smu_read_datablock((u8 *)hdr, addr, len)) {
939 printk(KERN_DEBUG "SMU: datablock read failed while reading "
940 "partition %02x !\n", id);
944 /* Got it, check a few things and create the property */
946 printk(KERN_DEBUG "SMU: Reading partition %02x and got "
947 "%02x !\n", id, hdr->id);
950 if (prom_add_property(smu->of_node, prop)) {
951 printk(KERN_DEBUG "SMU: Failed creating sdb-partition-%02x "
962 /* Note: Only allowed to return error code in pointers (using ERR_PTR)
963 * when interruptible is 1
965 struct smu_sdbp_header *__smu_get_sdb_partition(int id, unsigned int *size,
969 struct smu_sdbp_header *part;
974 sprintf(pname, "sdb-partition-%02x", id);
976 DPRINTK("smu_get_sdb_partition(%02x)\n", id);
980 rc = mutex_lock_interruptible(&smu_part_access);
984 mutex_lock(&smu_part_access);
986 part = (struct smu_sdbp_header *)get_property(smu->of_node,
989 DPRINTK("trying to extract from SMU ...\n");
990 part = smu_create_sdb_partition(id);
991 if (part != NULL && size)
992 *size = part->len << 2;
994 mutex_unlock(&smu_part_access);
998 struct smu_sdbp_header *smu_get_sdb_partition(int id, unsigned int *size)
1000 return __smu_get_sdb_partition(id, size, 0);
1002 EXPORT_SYMBOL(smu_get_sdb_partition);
1006 * Userland driver interface
1010 static LIST_HEAD(smu_clist);
1011 static DEFINE_SPINLOCK(smu_clist_lock);
1013 enum smu_file_mode {
1021 struct list_head list;
1022 enum smu_file_mode mode;
1026 wait_queue_head_t wait;
1027 u8 buffer[SMU_MAX_DATA];
1031 static int smu_open(struct inode *inode, struct file *file)
1033 struct smu_private *pp;
1034 unsigned long flags;
1036 pp = kmalloc(sizeof(struct smu_private), GFP_KERNEL);
1039 memset(pp, 0, sizeof(struct smu_private));
1040 spin_lock_init(&pp->lock);
1041 pp->mode = smu_file_commands;
1042 init_waitqueue_head(&pp->wait);
1044 spin_lock_irqsave(&smu_clist_lock, flags);
1045 list_add(&pp->list, &smu_clist);
1046 spin_unlock_irqrestore(&smu_clist_lock, flags);
1047 file->private_data = pp;
1053 static void smu_user_cmd_done(struct smu_cmd *cmd, void *misc)
1055 struct smu_private *pp = misc;
1057 wake_up_all(&pp->wait);
1061 static ssize_t smu_write(struct file *file, const char __user *buf,
1062 size_t count, loff_t *ppos)
1064 struct smu_private *pp = file->private_data;
1065 unsigned long flags;
1066 struct smu_user_cmd_hdr hdr;
1071 else if (copy_from_user(&hdr, buf, sizeof(hdr)))
1073 else if (hdr.cmdtype == SMU_CMDTYPE_WANTS_EVENTS) {
1074 pp->mode = smu_file_events;
1076 } else if (hdr.cmdtype == SMU_CMDTYPE_GET_PARTITION) {
1077 struct smu_sdbp_header *part;
1078 part = __smu_get_sdb_partition(hdr.cmd, NULL, 1);
1081 else if (IS_ERR(part))
1082 return PTR_ERR(part);
1084 } else if (hdr.cmdtype != SMU_CMDTYPE_SMU)
1086 else if (pp->mode != smu_file_commands)
1088 else if (hdr.data_len > SMU_MAX_DATA)
1091 spin_lock_irqsave(&pp->lock, flags);
1093 spin_unlock_irqrestore(&pp->lock, flags);
1098 spin_unlock_irqrestore(&pp->lock, flags);
1100 if (copy_from_user(pp->buffer, buf + sizeof(hdr), hdr.data_len)) {
1105 pp->cmd.cmd = hdr.cmd;
1106 pp->cmd.data_len = hdr.data_len;
1107 pp->cmd.reply_len = SMU_MAX_DATA;
1108 pp->cmd.data_buf = pp->buffer;
1109 pp->cmd.reply_buf = pp->buffer;
1110 pp->cmd.done = smu_user_cmd_done;
1112 rc = smu_queue_cmd(&pp->cmd);
1119 static ssize_t smu_read_command(struct file *file, struct smu_private *pp,
1120 char __user *buf, size_t count)
1122 DECLARE_WAITQUEUE(wait, current);
1123 struct smu_user_reply_hdr hdr;
1124 unsigned long flags;
1129 if (count < sizeof(struct smu_user_reply_hdr))
1131 spin_lock_irqsave(&pp->lock, flags);
1132 if (pp->cmd.status == 1) {
1133 if (file->f_flags & O_NONBLOCK)
1135 add_wait_queue(&pp->wait, &wait);
1137 set_current_state(TASK_INTERRUPTIBLE);
1139 if (pp->cmd.status != 1)
1142 if (signal_pending(current))
1144 spin_unlock_irqrestore(&pp->lock, flags);
1146 spin_lock_irqsave(&pp->lock, flags);
1148 set_current_state(TASK_RUNNING);
1149 remove_wait_queue(&pp->wait, &wait);
1151 spin_unlock_irqrestore(&pp->lock, flags);
1154 if (pp->cmd.status != 0)
1155 pp->cmd.reply_len = 0;
1156 size = sizeof(hdr) + pp->cmd.reply_len;
1160 hdr.status = pp->cmd.status;
1161 hdr.reply_len = pp->cmd.reply_len;
1162 if (copy_to_user(buf, &hdr, sizeof(hdr)))
1164 size -= sizeof(hdr);
1165 if (size && copy_to_user(buf + sizeof(hdr), pp->buffer, size))
1173 static ssize_t smu_read_events(struct file *file, struct smu_private *pp,
1174 char __user *buf, size_t count)
1176 /* Not implemented */
1177 msleep_interruptible(1000);
1182 static ssize_t smu_read(struct file *file, char __user *buf,
1183 size_t count, loff_t *ppos)
1185 struct smu_private *pp = file->private_data;
1187 if (pp->mode == smu_file_commands)
1188 return smu_read_command(file, pp, buf, count);
1189 if (pp->mode == smu_file_events)
1190 return smu_read_events(file, pp, buf, count);
1195 static unsigned int smu_fpoll(struct file *file, poll_table *wait)
1197 struct smu_private *pp = file->private_data;
1198 unsigned int mask = 0;
1199 unsigned long flags;
1204 if (pp->mode == smu_file_commands) {
1205 poll_wait(file, &pp->wait, wait);
1207 spin_lock_irqsave(&pp->lock, flags);
1208 if (pp->busy && pp->cmd.status != 1)
1210 spin_unlock_irqrestore(&pp->lock, flags);
1211 } if (pp->mode == smu_file_events) {
1212 /* Not yet implemented */
1217 static int smu_release(struct inode *inode, struct file *file)
1219 struct smu_private *pp = file->private_data;
1220 unsigned long flags;
1226 file->private_data = NULL;
1228 /* Mark file as closing to avoid races with new request */
1229 spin_lock_irqsave(&pp->lock, flags);
1230 pp->mode = smu_file_closing;
1233 /* Wait for any pending request to complete */
1234 if (busy && pp->cmd.status == 1) {
1235 DECLARE_WAITQUEUE(wait, current);
1237 add_wait_queue(&pp->wait, &wait);
1239 set_current_state(TASK_UNINTERRUPTIBLE);
1240 if (pp->cmd.status != 1)
1242 spin_lock_irqsave(&pp->lock, flags);
1244 spin_unlock_irqrestore(&pp->lock, flags);
1246 set_current_state(TASK_RUNNING);
1247 remove_wait_queue(&pp->wait, &wait);
1249 spin_unlock_irqrestore(&pp->lock, flags);
1251 spin_lock_irqsave(&smu_clist_lock, flags);
1252 list_del(&pp->list);
1253 spin_unlock_irqrestore(&smu_clist_lock, flags);
1260 static struct file_operations smu_device_fops = {
1261 .llseek = no_llseek,
1266 .release = smu_release,
1269 static struct miscdevice pmu_device = {
1270 MISC_DYNAMIC_MINOR, "smu", &smu_device_fops
1273 static int smu_device_init(void)
1277 if (misc_register(&pmu_device) < 0)
1278 printk(KERN_ERR "via-pmu: cannot register misc device.\n");
1281 device_initcall(smu_device_init);