22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/device.h>
31 #include <linux/rtc.h>
35 #include <linux/poll.h>
39 #include <linux/slab.h>
41 #include <asm/byteorder.h>
44 #include <asm/machdep.h>
47 #include <asm/sections.h>
48 #include <asm/uaccess.h>
51 #define AUTHOR "(c) 2005 Benjamin Herrenschmidt, IBM Corp."
56 #define DPRINTK(fmt, args...) do { printk(KERN_DEBUG fmt , ##args); } while (0)
58 #define DPRINTK(fmt, args...) do { } while (0)
64 #define SMU_MAX_DATA 254
100 static int smu_irq_inited;
102 static void smu_i2c_retry(
unsigned long data);
108 static void smu_start_cmd(
void)
110 unsigned long faddr, fend;
121 DPRINTK(
"SMU: starting cmd %x, %d bytes data\n", cmd->cmd,
123 DPRINTK(
"SMU: data buffer: %02x %02x %02x %02x %02x %02x %02x %02x\n",
124 ((
u8 *)cmd->data_buf)[0], ((
u8 *)cmd->data_buf)[1],
125 ((
u8 *)cmd->data_buf)[2], ((
u8 *)cmd->data_buf)[3],
126 ((
u8 *)cmd->data_buf)[4], ((
u8 *)cmd->data_buf)[5],
127 ((
u8 *)cmd->data_buf)[6], ((
u8 *)cmd->data_buf)[7]);
131 smu->
cmd_buf->length = cmd->data_len;
136 fend = faddr + smu->
cmd_buf->length + 2;
137 flush_inval_dcache_range(faddr, fend);
178 if ((gpio & 7) != 7) {
179 spin_unlock_irqrestore(&smu->
lock, flags);
199 flush_inval_dcache_range(faddr, faddr + 256);
202 ack = (~cmd->cmd) & 0xff;
203 if (ack != smu->
cmd_buf->cmd) {
204 DPRINTK(
"SMU: incorrect ack, want %x got %x\n",
208 reply_len = rc == 0 ? smu->
cmd_buf->length : 0;
209 DPRINTK(
"SMU: reply len: %d\n", reply_len);
210 if (reply_len > cmd->reply_len) {
212 "got %d bytes for a %d bytes buffer\n",
213 reply_len, cmd->reply_len);
214 reply_len = cmd->reply_len;
216 cmd->reply_len = reply_len;
217 if (cmd->reply_buf && reply_len)
235 spin_unlock_irqrestore(&smu->
lock, flags);
246 static irqreturn_t smu_msg_intr(
int irq,
void *arg)
280 spin_unlock_irqrestore(&smu->
lock, flags);
293 void (*
done)(
struct smu_cmd *cmd,
void *misc),
296 struct smu_cmd *cmd = &scmd->cmd;
300 if (data_len >
sizeof(scmd->buffer))
303 memset(scmd, 0,
sizeof(*scmd));
306 cmd->data_buf = scmd->buffer;
307 cmd->reply_len =
sizeof(scmd->buffer);
308 cmd->reply_buf = scmd->buffer;
314 scmd->buffer[i] = (
u8)
va_arg(list,
int);
331 smu_db_intr(smu->
db_irq, smu);
347 while(cmd->status == 1)
354 static inline int bcd2hex (
int n)
356 return (((n & 0xf0) >> 4) * 10) + (n & 0xf);
360 static inline int hex2bcd (
int n)
362 return ((n / 10) << 4) + (n % 10);
371 cmd_buf->
data[0] = 0x80;
384 struct smu_simple_cmd cmd;
395 smu_spinwait_simple(&cmd);
397 time->
tm_sec = bcd2hex(cmd.buffer[0]);
398 time->
tm_min = bcd2hex(cmd.buffer[1]);
399 time->
tm_hour = bcd2hex(cmd.buffer[2]);
400 time->
tm_wday = bcd2hex(cmd.buffer[3]);
401 time->
tm_mday = bcd2hex(cmd.buffer[4]);
402 time->
tm_mon = bcd2hex(cmd.buffer[5]) - 1;
403 time->
tm_year = bcd2hex(cmd.buffer[6]) + 100;
411 struct smu_simple_cmd cmd;
424 hex2bcd(time->
tm_mon) + 1,
428 smu_spinwait_simple(&cmd);
436 struct smu_simple_cmd cmd;
442 'S',
'H',
'U',
'T',
'D',
'O',
'W',
'N', 0))
444 smu_spinwait_simple(&cmd);
452 struct smu_simple_cmd cmd;
458 'R',
'E',
'S',
'T',
'A',
'R',
'T', 0))
460 smu_spinwait_simple(&cmd);
485 if (smu_cmdbuf_abs == 0) {
576 static int smu_late_init(
void)
606 "request interrupt %d\n",
616 "request interrupt %d\n",
640 of_platform_device_create(np,
"smu-sensors",
644 static DECLARE_WORK(smu_expose_childs_work, smu_expose_childs);
674 .of_match_table = smu_platform_match,
676 .probe = smu_platform_probe,
679 static int __init smu_init_sysfs(
void)
705 static void smu_i2c_complete_command(
struct smu_i2c_cmd *cmd,
int fail)
707 void (*
done)(
struct smu_i2c_cmd *
cmd,
void *misc) = cmd->done;
708 void *misc = cmd->misc;
712 if (!fail && cmd->read) {
713 if (cmd->pdata[0] < 1)
716 memcpy(cmd->info.data, &cmd->pdata[1],
720 DPRINTK(
"SMU: completing, success: %d\n", !fail);
729 cmd->status = fail ? -
EIO : 0;
733 struct smu_i2c_cmd *newcmd;
737 struct smu_i2c_cmd,
link);
746 spin_unlock_irqrestore(&smu->
lock, flags);
755 static void smu_i2c_retry(
unsigned long data)
759 DPRINTK(
"SMU: i2c failure, requeuing...\n");
762 cmd->pdata[0] = 0xff;
763 cmd->scmd.reply_len =
sizeof(cmd->pdata);
768 static void smu_i2c_low_completion(
struct smu_cmd *scmd,
void *misc)
770 struct smu_i2c_cmd *cmd = misc;
773 DPRINTK(
"SMU: i2c compl. stage=%d status=%x pdata[0]=%x rlen: %x\n",
774 cmd->stage, scmd->status, cmd->pdata[0], scmd->reply_len);
777 if (scmd->status < 0)
779 else if (cmd->read) {
781 fail = cmd->pdata[0] != 0;
783 fail = cmd->pdata[0] >= 0x80;
785 fail = cmd->pdata[0] != 0;
790 if (fail && --cmd->retries > 0) {
791 DPRINTK(
"SMU: i2c failure, starting timer...\n");
793 if (!smu_irq_inited) {
803 if (fail || cmd->stage != 0) {
804 smu_i2c_complete_command(cmd, fail);
808 DPRINTK(
"SMU: going to stage 1\n");
811 scmd->reply_buf = cmd->pdata;
812 scmd->reply_len =
sizeof(cmd->pdata);
813 scmd->data_buf = cmd->pdata;
831 cmd->scmd.done = smu_i2c_low_completion;
832 cmd->scmd.misc =
cmd;
833 cmd->scmd.reply_buf = cmd->pdata;
834 cmd->scmd.reply_len =
sizeof(cmd->pdata);
835 cmd->scmd.data_buf = (
u8 *)(
char *)&cmd->info;
836 cmd->scmd.status = 1;
838 cmd->pdata[0] = 0xff;
845 cmd->info.caddr = cmd->info.devaddr;
846 cmd->read = cmd->info.devaddr & 0x01;
847 switch(cmd->info.type) {
849 memset(&cmd->info.sublen, 0, 4);
852 cmd->info.devaddr &= 0xfe;
854 if (cmd->info.sublen > 3)
864 if (cmd->info.datalen > SMU_I2C_READ_MAX)
866 memset(cmd->info.data, 0xff, cmd->info.datalen);
867 cmd->scmd.data_len = 9;
869 if (cmd->info.datalen > SMU_I2C_WRITE_MAX)
871 cmd->scmd.data_len = 9 + cmd->info.datalen;
874 DPRINTK(
"SMU: i2c enqueuing command\n");
875 DPRINTK(
"SMU: %s, len=%d bus=%x addr=%x sub0=%x type=%x\n",
876 cmd->read ?
"read" :
"write", cmd->info.datalen,
877 cmd->info.bus, cmd->info.caddr,
878 cmd->info.subaddr[0], cmd->info.type);
892 spin_unlock_irqrestore(&smu->
lock, flags);
901 static int smu_read_datablock(
u8 *
dest,
unsigned int addr,
unsigned int len)
915 unsigned int clen =
min(len, chunk);
920 cmd.reply_len =
chunk;
921 cmd.reply_buf =
dest;
935 if (cmd.reply_len != clen) {
937 "smu_read_datablock, got: %d, want: %d\n",
938 cmd.reply_len, clen);
951 struct smu_simple_cmd cmd;
952 unsigned int addr, len, tlen;
957 DPRINTK(
"SMU: Query partition infos ... (irq=%d)\n", smu->
db_irq);
962 DPRINTK(
"SMU: done, status: %d, reply_len: %d\n",
963 cmd.cmd.status, cmd.cmd.reply_len);
966 if (cmd.cmd.status != 0 || cmd.cmd.reply_len != 6)
970 addr = *((
u16 *)cmd.buffer);
971 len = cmd.buffer[3] << 2;
975 tlen =
sizeof(
struct property) + len + 18;
981 prop->name = ((
char *)prop) + tlen - 18;
982 sprintf(prop->name,
"sdb-partition-%02x",
id);
988 if (smu_read_datablock((
u8 *)hdr, addr, len)) {
990 "partition %02x !\n",
id);
997 "%02x !\n",
id, hdr->
id);
1002 "property !\n",
id);
1016 unsigned int *
size,
int interruptible)
1024 sprintf(pname,
"sdb-partition-%02x",
id);
1026 DPRINTK(
"smu_get_sdb_partition(%02x)\n",
id);
1028 if (interruptible) {
1038 DPRINTK(
"trying to extract from SMU ...\n");
1039 part = smu_create_sdb_partition(
id);
1040 if (part !=
NULL && size)
1041 *size = part->
len << 2;
1083 unsigned long flags;
1094 list_add(&pp->
list, &smu_clist);
1095 spin_unlock_irqrestore(&smu_clist_lock, flags);
1103 static void smu_user_cmd_done(
struct smu_cmd *cmd,
void *misc)
1112 size_t count, loff_t *ppos)
1115 unsigned long flags;
1131 else if (IS_ERR(part))
1132 return PTR_ERR(part);
1143 spin_unlock_irqrestore(&pp->
lock, flags);
1148 spin_unlock_irqrestore(&pp->
lock, flags);
1155 pp->
cmd.cmd = hdr.cmd;
1156 pp->
cmd.data_len = hdr.data_len;
1160 pp->
cmd.done = smu_user_cmd_done;
1170 char __user *buf,
size_t count)
1174 unsigned long flags;
1182 if (pp->
cmd.status == 1) {
1184 spin_unlock_irqrestore(&pp->
lock, flags);
1191 if (pp->
cmd.status != 1)
1196 spin_unlock_irqrestore(&pp->
lock, flags);
1203 spin_unlock_irqrestore(&pp->
lock, flags);
1206 if (pp->
cmd.status != 0)
1207 pp->
cmd.reply_len = 0;
1208 size =
sizeof(
hdr) + pp->
cmd.reply_len;
1212 hdr.status = pp->
cmd.status;
1213 hdr.reply_len = pp->
cmd.reply_len;
1216 size -=
sizeof(
hdr);
1226 char __user *buf,
size_t count)
1234 static ssize_t smu_read(
struct file *file,
char __user *buf,
1235 size_t count, loff_t *ppos)
1240 return smu_read_command(file, pp, buf, count);
1242 return smu_read_events(file, pp, buf, count);
1247 static unsigned int smu_fpoll(
struct file *file,
poll_table *
wait)
1250 unsigned int mask = 0;
1251 unsigned long flags;
1257 poll_wait(file, &pp->
wait, wait);
1260 if (pp->
busy && pp->
cmd.status != 1)
1262 spin_unlock_irqrestore(&pp->
lock, flags);
1269 static int smu_release(
struct inode *
inode,
struct file *file)
1272 unsigned long flags;
1286 if (busy && pp->
cmd.status == 1) {
1292 if (pp->
cmd.status != 1)
1294 spin_unlock_irqrestore(&pp->
lock, flags);
1301 spin_unlock_irqrestore(&pp->
lock, flags);
1305 spin_unlock_irqrestore(&smu_clist_lock, flags);
1318 .release = smu_release,
1325 static int smu_device_init(
void)