[PATCH] deprecate AUDIT_POSSBILE
[firewire-audio.git] / drivers / macintosh / smu.c
blobf4516ca7aa3a20ee13f100e7d06fa9e18eded116
1 /*
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.
8 */
11 * TODO:
12 * - maybe add timeout to commands ?
13 * - blocking version of time functions
14 * - polling version of i2c commands (including timer that works with
15 * interrutps off)
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>
41 #include <asm/io.h>
42 #include <asm/prom.h>
43 #include <asm/machdep.h>
44 #include <asm/pmac_feature.h>
45 #include <asm/smu.h>
46 #include <asm/sections.h>
47 #include <asm/abs_addr.h>
48 #include <asm/uaccess.h>
49 #include <asm/of_device.h>
51 #define VERSION "0.7"
52 #define AUTHOR "(c) 2005 Benjamin Herrenschmidt, IBM Corp."
54 #undef DEBUG_SMU
56 #ifdef DEBUG_SMU
57 #define DPRINTK(fmt, args...) do { printk(KERN_DEBUG fmt , ##args); } while (0)
58 #else
59 #define DPRINTK(fmt, args...) do { } while (0)
60 #endif
63 * This is the command buffer passed to the SMU hardware
65 #define SMU_MAX_DATA 254
67 struct smu_cmd_buf {
68 u8 cmd;
69 u8 length;
70 u8 data[SMU_MAX_DATA];
73 struct smu_device {
74 spinlock_t lock;
75 struct device_node *of_node;
76 struct of_device *of_dev;
77 int doorbell; /* doorbell gpio */
78 u32 __iomem *db_buf; /* doorbell buffer */
79 int db_irq;
80 int msg;
81 int msg_irq;
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;
107 struct smu_cmd *cmd;
109 if (list_empty(&smu->cmd_list))
110 return;
112 /* Fetch first command in queue */
113 cmd = list_entry(smu->cmd_list.next, struct smu_cmd, link);
114 smu->cmd_cur = cmd;
115 list_del(&cmd->link);
117 DPRINTK("SMU: starting cmd %x, %d bytes data\n", cmd->cmd,
118 cmd->data_len);
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)
148 unsigned long flags;
149 struct smu_cmd *cmd;
150 void (*done)(struct smu_cmd *cmd, void *misc) = NULL;
151 void *misc = NULL;
152 u8 gpio;
153 int rc = 0;
155 /* SMU completed the command, well, we hope, let's make sure
156 * of it
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);
163 return IRQ_HANDLED;
166 cmd = smu->cmd_cur;
167 smu->cmd_cur = NULL;
168 if (cmd == NULL)
169 goto bail;
171 if (rc == 0) {
172 unsigned long faddr;
173 int reply_len;
174 u8 ack;
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);
184 /* Now check ack */
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);
189 rc = -EIO;
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
207 done = cmd->done;
208 misc = cmd->misc;
209 mb();
210 cmd->status = rc;
211 bail:
212 /* Start next command if any */
213 smu_start_cmd();
214 spin_unlock_irqrestore(&smu->lock, flags);
216 /* Call command completion handler if any */
217 if (done)
218 done(cmd, misc);
220 /* It's an edge interrupt, nothing to do */
221 return IRQ_HANDLED;
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 */
235 return IRQ_HANDLED;
240 * Queued command management.
244 int smu_queue_cmd(struct smu_cmd *cmd)
246 unsigned long flags;
248 if (smu == NULL)
249 return -ENODEV;
250 if (cmd->data_len > SMU_MAX_DATA ||
251 cmd->reply_len > SMU_MAX_DATA)
252 return -EINVAL;
254 cmd->status = 1;
255 spin_lock_irqsave(&smu->lock, flags);
256 list_add_tail(&cmd->link, &smu->cmd_list);
257 if (smu->cmd_cur == NULL)
258 smu_start_cmd();
259 spin_unlock_irqrestore(&smu->lock, flags);
261 return 0;
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),
269 void *misc, ...)
271 struct smu_cmd *cmd = &scmd->cmd;
272 va_list list;
273 int i;
275 if (data_len > sizeof(scmd->buffer))
276 return -EINVAL;
278 memset(scmd, 0, sizeof(*scmd));
279 cmd->cmd = command;
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;
284 cmd->done = done;
285 cmd->misc = misc;
287 va_start(list, misc);
288 for (i = 0; i < data_len; ++i)
289 scmd->buffer[i] = (u8)va_arg(list, int);
290 va_end(list);
292 return smu_queue_cmd(cmd);
294 EXPORT_SYMBOL(smu_queue_simple);
297 void smu_poll(void)
299 u8 gpio;
301 if (smu == NULL)
302 return;
304 gpio = pmac_do_feature_call(PMAC_FTR_READ_GPIO, NULL, smu->doorbell);
305 if ((gpio & 7) == 7)
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;
315 complete(comp);
317 EXPORT_SYMBOL(smu_done_complete);
320 void smu_spinwait_cmd(struct smu_cmd *cmd)
322 while(cmd->status == 1)
323 smu_poll();
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)
344 cmd_buf->cmd = 0x8e;
345 cmd_buf->length = 8;
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;
360 int rc;
362 if (smu == NULL)
363 return -ENODEV;
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);
368 if (rc)
369 return rc;
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;
380 return 0;
384 int smu_set_rtc_time(struct rtc_time *time, int spinwait)
386 struct smu_simple_cmd cmd;
387 int rc;
389 if (smu == NULL)
390 return -ENODEV;
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),
397 time->tm_wday,
398 hex2bcd(time->tm_mday),
399 hex2bcd(time->tm_mon) + 1,
400 hex2bcd(time->tm_year - 100));
401 if (rc)
402 return rc;
403 smu_spinwait_simple(&cmd);
405 return 0;
409 void smu_shutdown(void)
411 struct smu_simple_cmd cmd;
413 if (smu == NULL)
414 return;
416 if (smu_queue_simple(&cmd, SMU_CMD_POWER_COMMAND, 9, NULL, NULL,
417 'S', 'H', 'U', 'T', 'D', 'O', 'W', 'N', 0))
418 return;
419 smu_spinwait_simple(&cmd);
420 for (;;)
425 void smu_restart(void)
427 struct smu_simple_cmd cmd;
429 if (smu == NULL)
430 return;
432 if (smu_queue_simple(&cmd, SMU_CMD_POWER_COMMAND, 8, NULL, NULL,
433 'R', 'E', 'S', 'T', 'A', 'R', 'T', 0))
434 return;
435 smu_spinwait_simple(&cmd);
436 for (;;)
441 int smu_present(void)
443 return smu != NULL;
445 EXPORT_SYMBOL(smu_present);
448 int __init smu_init (void)
450 struct device_node *np;
451 u32 *data;
453 np = of_find_node_by_type(NULL, "smu");
454 if (np == NULL)
455 return -ENODEV;
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");
461 return -EINVAL;
464 smu = alloc_bootmem(sizeof(struct smu_device));
465 if (smu == NULL)
466 return -ENOMEM;
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);
472 smu->of_node = np;
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");
483 if (np == NULL) {
484 printk(KERN_ERR "SMU: Can't find doorbell GPIO !\n");
485 goto fail;
487 data = (u32 *)get_property(np, "reg", NULL);
488 if (data == NULL) {
489 of_node_put(np);
490 printk(KERN_ERR "SMU: Can't find doorbell GPIO address !\n");
491 goto fail;
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;
501 if (np->n_intrs > 0)
502 smu->db_irq = np->intrs[0].line;
504 of_node_put(np);
506 /* Now look for the smu-interrupt GPIO */
507 do {
508 np = of_find_node_by_name(NULL, "smu-interrupt");
509 if (np == NULL)
510 break;
511 data = (u32 *)get_property(np, "reg", NULL);
512 if (data == NULL) {
513 of_node_put(np);
514 break;
516 smu->msg = *data;
517 if (smu->msg < 0x50)
518 smu->msg += 0x50;
519 if (np->n_intrs > 0)
520 smu->msg_irq = np->intrs[0].line;
521 of_node_put(np);
522 } while(0);
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");
531 goto fail;
534 sys_ctrler = SYS_CTRLER_SMU;
535 return 0;
537 fail:
538 smu = NULL;
539 return -ENXIO;
544 static int smu_late_init(void)
546 if (!smu)
547 return 0;
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",
562 smu->db_irq);
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",
572 smu->msg_irq);
573 smu->msg_irq = NO_IRQ;
577 return 0;
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);
585 * sysfs visibility
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",
595 &smu->of_dev->dev);
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)
603 if (!smu)
604 return -ENODEV;
605 smu->of_dev = dev;
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);
613 return 0;
616 static struct of_device_id smu_platform_match[] =
619 .type = "smu",
624 static struct of_platform_driver smu_of_platform_driver =
626 .name = "smu",
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);
642 return 0;
645 device_initcall(smu_init_sysfs);
647 struct of_device *smu_get_ofdev(void)
649 if (!smu)
650 return NULL;
651 return smu->of_dev;
654 EXPORT_SYMBOL_GPL(smu_get_ofdev);
657 * i2c interface
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;
664 unsigned long flags;
666 /* Check for read case */
667 if (!fail && cmd->read) {
668 if (cmd->pdata[0] < 1)
669 fail = 1;
670 else
671 memcpy(cmd->info.data, &cmd->pdata[1],
672 cmd->info.datalen);
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;
683 wmb();
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)
699 smu_start_cmd();
701 spin_unlock_irqrestore(&smu->lock, flags);
703 /* Call command completion handler if any */
704 if (done)
705 done(cmd, misc);
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;
726 int fail = 0;
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)
733 fail = 1;
734 else if (cmd->read) {
735 if (cmd->stage == 0)
736 fail = cmd->pdata[0] != 0;
737 else
738 fail = cmd->pdata[0] >= 0x80;
739 } else {
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));
749 return;
752 /* If failure or stage 1, command is complete */
753 if (fail || cmd->stage != 0) {
754 smu_i2c_complete_command(cmd, fail);
755 return;
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;
764 scmd->data_len = 1;
765 cmd->pdata[0] = 0;
766 cmd->stage = 1;
767 cmd->retries = 20;
768 smu_queue_cmd(scmd);
772 int smu_queue_i2c(struct smu_i2c_cmd *cmd)
774 unsigned long flags;
776 if (smu == NULL)
777 return -ENODEV;
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;
787 cmd->stage = 0;
788 cmd->pdata[0] = 0xff;
789 cmd->retries = 20;
790 cmd->status = 1;
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);
800 break;
801 case SMU_I2C_TRANSFER_COMBINED:
802 cmd->info.devaddr &= 0xfe;
803 case SMU_I2C_TRANSFER_STDSUB:
804 if (cmd->info.sublen > 3)
805 return -EINVAL;
806 break;
807 default:
808 return -EINVAL;
811 /* Finish setting up command based on transfer direction
813 if (cmd->read) {
814 if (cmd->info.datalen > SMU_I2C_READ_MAX)
815 return -EINVAL;
816 memset(cmd->info.data, 0xff, cmd->info.datalen);
817 cmd->scmd.data_len = 9;
818 } else {
819 if (cmd->info.datalen > SMU_I2C_WRITE_MAX)
820 return -EINVAL;
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
832 * main command list
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)
839 smu_start_cmd();
840 } else
841 list_add_tail(&cmd->link, &smu->cmd_i2c_list);
842 spin_unlock_irqrestore(&smu->lock, flags);
844 return 0;
848 * Handling of "partitions"
851 static int smu_read_datablock(u8 *dest, unsigned int addr, unsigned int len)
853 DECLARE_COMPLETION(comp);
854 unsigned int chunk;
855 struct smu_cmd cmd;
856 int rc;
857 u8 params[8];
859 /* We currently use a chunk size of 0xe. We could check the
860 * SMU firmware version and use bigger sizes though
862 chunk = 0xe;
864 while (len) {
865 unsigned int clen = min(len, chunk);
867 cmd.cmd = SMU_CMD_MISC_ee_COMMAND;
868 cmd.data_len = 7;
869 cmd.data_buf = params;
870 cmd.reply_len = chunk;
871 cmd.reply_buf = dest;
872 cmd.done = smu_done_complete;
873 cmd.misc = &comp;
874 params[0] = SMU_CMD_MISC_ee_GET_DATABLOCK_REC;
875 params[1] = 0x4;
876 *((u32 *)&params[2]) = addr;
877 params[6] = clen;
879 rc = smu_queue_cmd(&cmd);
880 if (rc)
881 return rc;
882 wait_for_completion(&comp);
883 if (cmd.status != 0)
884 return rc;
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);
889 return -EIO;
891 len -= clen;
892 addr += clen;
893 dest += clen;
895 return 0;
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)
917 return NULL;
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);
928 if (prop == NULL)
929 return NULL;
930 hdr = (struct smu_sdbp_header *)(prop + 1);
931 prop->name = ((char *)prop) + tlen - 18;
932 sprintf(prop->name, "sdb-partition-%02x", id);
933 prop->length = len;
934 prop->value = (unsigned char *)hdr;
935 prop->next = NULL;
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);
941 goto failure;
944 /* Got it, check a few things and create the property */
945 if (hdr->id != id) {
946 printk(KERN_DEBUG "SMU: Reading partition %02x and got "
947 "%02x !\n", id, hdr->id);
948 goto failure;
950 if (prom_add_property(smu->of_node, prop)) {
951 printk(KERN_DEBUG "SMU: Failed creating sdb-partition-%02x "
952 "property !\n", id);
953 goto failure;
956 return hdr;
957 failure:
958 kfree(prop);
959 return NULL;
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,
966 int interruptible)
968 char pname[32];
969 struct smu_sdbp_header *part;
971 if (!smu)
972 return NULL;
974 sprintf(pname, "sdb-partition-%02x", id);
976 DPRINTK("smu_get_sdb_partition(%02x)\n", id);
978 if (interruptible) {
979 int rc;
980 rc = mutex_lock_interruptible(&smu_part_access);
981 if (rc)
982 return ERR_PTR(rc);
983 } else
984 mutex_lock(&smu_part_access);
986 part = (struct smu_sdbp_header *)get_property(smu->of_node,
987 pname, size);
988 if (part == NULL) {
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);
995 return part;
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 {
1014 smu_file_commands,
1015 smu_file_events,
1016 smu_file_closing
1019 struct smu_private
1021 struct list_head list;
1022 enum smu_file_mode mode;
1023 int busy;
1024 struct smu_cmd cmd;
1025 spinlock_t lock;
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);
1037 if (pp == 0)
1038 return -ENOMEM;
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;
1049 return 0;
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;
1067 int rc = 0;
1069 if (pp->busy)
1070 return -EBUSY;
1071 else if (copy_from_user(&hdr, buf, sizeof(hdr)))
1072 return -EFAULT;
1073 else if (hdr.cmdtype == SMU_CMDTYPE_WANTS_EVENTS) {
1074 pp->mode = smu_file_events;
1075 return 0;
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);
1079 if (part == NULL)
1080 return -EINVAL;
1081 else if (IS_ERR(part))
1082 return PTR_ERR(part);
1083 return 0;
1084 } else if (hdr.cmdtype != SMU_CMDTYPE_SMU)
1085 return -EINVAL;
1086 else if (pp->mode != smu_file_commands)
1087 return -EBADFD;
1088 else if (hdr.data_len > SMU_MAX_DATA)
1089 return -EINVAL;
1091 spin_lock_irqsave(&pp->lock, flags);
1092 if (pp->busy) {
1093 spin_unlock_irqrestore(&pp->lock, flags);
1094 return -EBUSY;
1096 pp->busy = 1;
1097 pp->cmd.status = 1;
1098 spin_unlock_irqrestore(&pp->lock, flags);
1100 if (copy_from_user(pp->buffer, buf + sizeof(hdr), hdr.data_len)) {
1101 pp->busy = 0;
1102 return -EFAULT;
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;
1111 pp->cmd.misc = pp;
1112 rc = smu_queue_cmd(&pp->cmd);
1113 if (rc < 0)
1114 return rc;
1115 return count;
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;
1125 int size, rc = 0;
1127 if (!pp->busy)
1128 return 0;
1129 if (count < sizeof(struct smu_user_reply_hdr))
1130 return -EOVERFLOW;
1131 spin_lock_irqsave(&pp->lock, flags);
1132 if (pp->cmd.status == 1) {
1133 if (file->f_flags & O_NONBLOCK)
1134 return -EAGAIN;
1135 add_wait_queue(&pp->wait, &wait);
1136 for (;;) {
1137 set_current_state(TASK_INTERRUPTIBLE);
1138 rc = 0;
1139 if (pp->cmd.status != 1)
1140 break;
1141 rc = -ERESTARTSYS;
1142 if (signal_pending(current))
1143 break;
1144 spin_unlock_irqrestore(&pp->lock, flags);
1145 schedule();
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);
1152 if (rc)
1153 return rc;
1154 if (pp->cmd.status != 0)
1155 pp->cmd.reply_len = 0;
1156 size = sizeof(hdr) + pp->cmd.reply_len;
1157 if (count < size)
1158 size = count;
1159 rc = size;
1160 hdr.status = pp->cmd.status;
1161 hdr.reply_len = pp->cmd.reply_len;
1162 if (copy_to_user(buf, &hdr, sizeof(hdr)))
1163 return -EFAULT;
1164 size -= sizeof(hdr);
1165 if (size && copy_to_user(buf + sizeof(hdr), pp->buffer, size))
1166 return -EFAULT;
1167 pp->busy = 0;
1169 return rc;
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);
1178 return 0;
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);
1192 return -EBADFD;
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;
1201 if (pp == 0)
1202 return 0;
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)
1209 mask |= POLLIN;
1210 spin_unlock_irqrestore(&pp->lock, flags);
1211 } if (pp->mode == smu_file_events) {
1212 /* Not yet implemented */
1214 return mask;
1217 static int smu_release(struct inode *inode, struct file *file)
1219 struct smu_private *pp = file->private_data;
1220 unsigned long flags;
1221 unsigned int busy;
1223 if (pp == 0)
1224 return 0;
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;
1231 busy = pp->busy;
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);
1238 for (;;) {
1239 set_current_state(TASK_UNINTERRUPTIBLE);
1240 if (pp->cmd.status != 1)
1241 break;
1242 spin_lock_irqsave(&pp->lock, flags);
1243 schedule();
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);
1254 kfree(pp);
1256 return 0;
1260 static struct file_operations smu_device_fops = {
1261 .llseek = no_llseek,
1262 .read = smu_read,
1263 .write = smu_write,
1264 .poll = smu_fpoll,
1265 .open = smu_open,
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)
1275 if (!smu)
1276 return -ENODEV;
1277 if (misc_register(&pmu_device) < 0)
1278 printk(KERN_ERR "via-pmu: cannot register misc device.\n");
1279 return 0;
1281 device_initcall(smu_device_init);