blob: f4516ca7aa3a20ee13f100e7d06fa9e18eded116 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PowerMac G5 SMU driver
3 *
4 * Copyright 2004 J. Mayer <l_indien@magic.fr>
5 * Copyright 2005 Benjamin Herrenschmidt, IBM Corp.
6 *
7 * Released under the term of the GNU GPL v2.
8 */
9
10/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * TODO:
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -070012 * - 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
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>
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -070033#include <linux/completion.h>
34#include <linux/miscdevice.h>
35#include <linux/delay.h>
36#include <linux/sysdev.h>
37#include <linux/poll.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080038#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
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>
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -070048#include <asm/uaccess.h>
49#include <asm/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +110051#define VERSION "0.7"
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -070052#define AUTHOR "(c) 2005 Benjamin Herrenschmidt, IBM Corp."
53
54#undef DEBUG_SMU
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#ifdef DEBUG_SMU
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +110057#define DPRINTK(fmt, args...) do { printk(KERN_DEBUG fmt , ##args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#else
59#define DPRINTK(fmt, args...) do { } while (0)
60#endif
61
62/*
63 * This is the command buffer passed to the SMU hardware
64 */
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -070065#define SMU_MAX_DATA 254
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067struct smu_cmd_buf {
68 u8 cmd;
69 u8 length;
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -070070 u8 data[SMU_MAX_DATA];
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
73struct smu_device {
74 spinlock_t lock;
75 struct device_node *of_node;
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -070076 struct of_device *of_dev;
77 int doorbell; /* doorbell gpio */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 u32 __iomem *db_buf; /* doorbell buffer */
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -070079 int db_irq;
80 int msg;
81 int msg_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 struct smu_cmd_buf *cmd_buf; /* command buffer virtual */
83 u32 cmd_buf_abs; /* command buffer absolute */
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -070084 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
91/*
92 * I don't think there will ever be more than one SMU, so
93 * for now, just hard code that
94 */
95static struct smu_device *smu;
Ingo Molnar14cc3e22006-03-26 01:37:14 -080096static DEFINE_MUTEX(smu_part_access);
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -070097
Benjamin Herrenschmidt730745a2006-01-07 11:30:44 +110098static void smu_i2c_retry(unsigned long data);
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700101 * SMU driver low level stuff
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700104static void smu_start_cmd(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700106 unsigned long faddr, fend;
107 struct smu_cmd *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700109 if (list_empty(&smu->cmd_list))
110 return;
111
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);
116
117 DPRINTK("SMU: starting cmd %x, %d bytes data\n", cmd->cmd,
118 cmd->data_len);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100119 DPRINTK("SMU: data buffer: %02x %02x %02x %02x %02x %02x %02x %02x\n",
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700120 ((u8 *)cmd->data_buf)[0], ((u8 *)cmd->data_buf)[1],
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100121 ((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]);
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700124
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);
129
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);
134
135 /* This isn't exactly a DMA mapping here, I suspect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * the SMU is actually communicating with us via i2c to the
137 * northbridge or the CPU to access RAM.
138 */
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700139 writel(smu->cmd_buf_abs, smu->db_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 /* Ring the SMU doorbell */
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700142 pmac_do_feature_call(PMAC_FTR_WRITE_GPIO, NULL, smu->doorbell, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700145
146static irqreturn_t smu_db_intr(int irq, void *arg, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700148 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700155 /* SMU completed the command, well, we hope, let's make sure
156 * of it
157 */
158 spin_lock_irqsave(&smu->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700160 gpio = pmac_do_feature_call(PMAC_FTR_READ_GPIO, NULL, smu->doorbell);
Benjamin Herrenschmidta44fe132005-09-30 08:25:17 +1000161 if ((gpio & 7) != 7) {
162 spin_unlock_irqrestore(&smu->lock, flags);
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700163 return IRQ_HANDLED;
Benjamin Herrenschmidta44fe132005-09-30 08:25:17 +1000164 }
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700165
166 cmd = smu->cmd_cur;
167 smu->cmd_cur = NULL;
168 if (cmd == NULL)
169 goto bail;
170
171 if (rc == 0) {
172 unsigned long faddr;
173 int reply_len;
174 u8 ack;
175
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)
180 */
181 faddr = (unsigned long)smu->cmd_buf;
182 flush_inval_dcache_range(faddr, faddr + 256);
183
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;
190 }
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;
198 }
199 cmd->reply_len = reply_len;
200 if (cmd->reply_buf && reply_len)
201 memcpy(cmd->reply_buf, smu->cmd_buf->data, reply_len);
202 }
203
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
206 */
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);
215
216 /* Call command completion handler if any */
217 if (done)
218 done(cmd, misc);
219
220 /* It's an edge interrupt, nothing to do */
221 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700224
225static irqreturn_t smu_msg_intr(int irq, void *arg, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700227 /* 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.
230 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700232 printk(KERN_INFO "SMU: message interrupt !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700234 /* It's an edge interrupt, nothing to do */
235 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700238
239/*
240 * Queued command management.
241 *
242 */
243
244int smu_queue_cmd(struct smu_cmd *cmd)
245{
246 unsigned long flags;
247
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;
253
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);
260
261 return 0;
262}
263EXPORT_SYMBOL(smu_queue_cmd);
264
265
266int 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, ...)
270{
271 struct smu_cmd *cmd = &scmd->cmd;
272 va_list list;
273 int i;
274
275 if (data_len > sizeof(scmd->buffer))
276 return -EINVAL;
277
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;
286
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);
291
292 return smu_queue_cmd(cmd);
293}
294EXPORT_SYMBOL(smu_queue_simple);
295
296
297void smu_poll(void)
298{
299 u8 gpio;
300
301 if (smu == NULL)
302 return;
303
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);
307}
308EXPORT_SYMBOL(smu_poll);
309
310
311void smu_done_complete(struct smu_cmd *cmd, void *misc)
312{
313 struct completion *comp = misc;
314
315 complete(comp);
316}
317EXPORT_SYMBOL(smu_done_complete);
318
319
320void smu_spinwait_cmd(struct smu_cmd *cmd)
321{
322 while(cmd->status == 1)
323 smu_poll();
324}
325EXPORT_SYMBOL(smu_spinwait_cmd);
326
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328/* RTC low level commands */
329static inline int bcd2hex (int n)
330{
331 return (((n & 0xf0) >> 4) * 10) + (n & 0xf);
332}
333
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335static inline int hex2bcd (int n)
336{
337 return ((n / 10) << 4) + (n % 10);
338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341static inline void smu_fill_set_rtc_cmd(struct smu_cmd_buf *cmd_buf,
342 struct rtc_time *time)
343{
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);
354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700357int smu_get_rtc_time(struct rtc_time *time, int spinwait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700359 struct smu_simple_cmd cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int rc;
361
362 if (smu == NULL)
363 return -ENODEV;
364
365 memset(time, 0, sizeof(struct rtc_time));
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700366 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700372 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;
379
380 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700383
384int smu_set_rtc_time(struct rtc_time *time, int spinwait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700386 struct smu_simple_cmd cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 int rc;
388
389 if (smu == NULL)
390 return -ENODEV;
391
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700392 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409void smu_shutdown(void)
410{
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700411 struct smu_simple_cmd cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 if (smu == NULL)
414 return;
415
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700416 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 for (;;)
421 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425void smu_restart(void)
426{
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700427 struct smu_simple_cmd cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 if (smu == NULL)
430 return;
431
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700432 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 for (;;)
437 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441int smu_present(void)
442{
443 return smu != NULL;
444}
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700445EXPORT_SYMBOL(smu_present);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100448int __init smu_init (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct device_node *np;
451 u32 *data;
452
453 np = of_find_node_by_type(NULL, "smu");
454 if (np == NULL)
455 return -ENODEV;
456
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700457 printk(KERN_INFO "SMU driver %s %s\n", VERSION, AUTHOR);
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (smu_cmdbuf_abs == 0) {
460 printk(KERN_ERR "SMU: Command buffer not allocated !\n");
461 return -EINVAL;
462 }
463
464 smu = alloc_bootmem(sizeof(struct smu_device));
465 if (smu == NULL)
466 return -ENOMEM;
467 memset(smu, 0, sizeof(*smu));
468
469 spin_lock_init(&smu->lock);
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700470 INIT_LIST_HEAD(&smu->cmd_list);
471 INIT_LIST_HEAD(&smu->cmd_i2c_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 smu->of_node = np;
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700473 smu->db_irq = NO_IRQ;
474 smu->msg_irq = NO_IRQ;
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /* smu_cmdbuf_abs is in the low 2G of RAM, can be converted to a
477 * 32 bits value safely
478 */
479 smu->cmd_buf_abs = (u32)smu_cmdbuf_abs;
480 smu->cmd_buf = (struct smu_cmd_buf *)abs_to_virt(smu_cmdbuf_abs);
481
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;
486 }
487 data = (u32 *)get_property(np, "reg", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (data == NULL) {
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700489 of_node_put(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 printk(KERN_ERR "SMU: Can't find doorbell GPIO address !\n");
491 goto fail;
492 }
493
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.
497 */
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700498 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;
503
504 of_node_put(np);
505
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;
515 }
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
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.
527 */
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;
532 }
533
534 sys_ctrler = SYS_CTRLER_SMU;
535 return 0;
536
537 fail:
538 smu = NULL;
539 return -ENXIO;
540
541}
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700542
543
544static int smu_late_init(void)
545{
546 if (!smu)
547 return 0;
548
Benjamin Herrenschmidt730745a2006-01-07 11:30:44 +1100549 init_timer(&smu->i2c_timer);
550 smu->i2c_timer.function = smu_i2c_retry;
551 smu->i2c_timer.data = (unsigned long)smu;
552
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700553 /*
554 * Try to request the interrupts
555 */
556
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;
564 }
565 }
566
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;
574 }
575 }
576
577 return 0;
578}
Benjamin Herrenschmidt730745a2006-01-07 11:30:44 +1100579/* 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
581 */
582core_initcall(smu_late_init);
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700583
584/*
585 * sysfs visibility
586 */
587
588static void smu_expose_childs(void *unused)
589{
Benjamin Herrenschmidta28d3af2006-01-07 11:35:26 +1100590 struct device_node *np;
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700591
Benjamin Herrenschmidta28d3af2006-01-07 11:35:26 +1100592 for (np = NULL; (np = of_get_next_child(smu->of_node, np)) != NULL;)
Benjamin Herrenschmidt75722d32005-11-07 16:08:17 +1100593 if (device_is_compatible(np, "smu-sensors"))
Benjamin Herrenschmidt730745a2006-01-07 11:30:44 +1100594 of_platform_device_create(np, "smu-sensors",
595 &smu->of_dev->dev);
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700596}
597
598static DECLARE_WORK(smu_expose_childs_work, smu_expose_childs, NULL);
599
600static int smu_platform_probe(struct of_device* dev,
601 const struct of_device_id *match)
602{
603 if (!smu)
604 return -ENODEV;
605 smu->of_dev = dev;
606
607 /*
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
610 */
611 schedule_work(&smu_expose_childs_work);
612
613 return 0;
614}
615
616static struct of_device_id smu_platform_match[] =
617{
618 {
619 .type = "smu",
620 },
621 {},
622};
623
624static struct of_platform_driver smu_of_platform_driver =
625{
626 .name = "smu",
627 .match_table = smu_platform_match,
628 .probe = smu_platform_probe,
629};
630
631static int __init smu_init_sysfs(void)
632{
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700633 /*
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
640 */
Bjorn Helgaas6ea671a2006-03-21 23:20:27 -0800641 of_register_driver(&smu_of_platform_driver);
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700642 return 0;
643}
644
645device_initcall(smu_init_sysfs);
646
647struct of_device *smu_get_ofdev(void)
648{
649 if (!smu)
650 return NULL;
651 return smu->of_dev;
652}
653
654EXPORT_SYMBOL_GPL(smu_get_ofdev);
655
656/*
657 * i2c interface
658 */
659
660static void smu_i2c_complete_command(struct smu_i2c_cmd *cmd, int fail)
661{
662 void (*done)(struct smu_i2c_cmd *cmd, void *misc) = cmd->done;
663 void *misc = cmd->misc;
664 unsigned long flags;
665
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);
673 }
674
675 DPRINTK("SMU: completing, success: %d\n", !fail);
676
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
680 */
681 spin_lock_irqsave(&smu->lock, flags);
682 smu->cmd_i2c_cur = NULL;
683 wmb();
684 cmd->status = fail ? -EIO : 0;
685
686 /* Is there another i2c command waiting ? */
687 if (!list_empty(&smu->cmd_i2c_list)) {
688 struct smu_i2c_cmd *newcmd;
689
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);
695
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();
700 }
701 spin_unlock_irqrestore(&smu->lock, flags);
702
703 /* Call command completion handler if any */
704 if (done)
705 done(cmd, misc);
706
707}
708
709
710static void smu_i2c_retry(unsigned long data)
711{
Benjamin Herrenschmidt730745a2006-01-07 11:30:44 +1100712 struct smu_i2c_cmd *cmd = smu->cmd_i2c_cur;
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700713
714 DPRINTK("SMU: i2c failure, requeuing...\n");
715
716 /* requeue command simply by resetting reply_len */
717 cmd->pdata[0] = 0xff;
Benjamin Herrenschmidt730745a2006-01-07 11:30:44 +1100718 cmd->scmd.reply_len = sizeof(cmd->pdata);
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700719 smu_queue_cmd(&cmd->scmd);
720}
721
722
723static void smu_i2c_low_completion(struct smu_cmd *scmd, void *misc)
724{
725 struct smu_i2c_cmd *cmd = misc;
726 int fail = 0;
727
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);
730
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;
741 }
742
743 /* Handle failures by requeuing command, after 5ms interval
744 */
745 if (fail && --cmd->retries > 0) {
746 DPRINTK("SMU: i2c failure, starting timer...\n");
Benjamin Herrenschmidt730745a2006-01-07 11:30:44 +1100747 BUG_ON(cmd != smu->cmd_i2c_cur);
748 mod_timer(&smu->i2c_timer, jiffies + msecs_to_jiffies(5));
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700749 return;
750 }
751
752 /* If failure or stage 1, command is complete */
753 if (fail || cmd->stage != 0) {
754 smu_i2c_complete_command(cmd, fail);
755 return;
756 }
757
758 DPRINTK("SMU: going to stage 1\n");
759
760 /* Ok, initial command complete, now poll status */
761 scmd->reply_buf = cmd->pdata;
Benjamin Herrenschmidt730745a2006-01-07 11:30:44 +1100762 scmd->reply_len = sizeof(cmd->pdata);
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700763 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);
769}
770
771
772int smu_queue_i2c(struct smu_i2c_cmd *cmd)
773{
774 unsigned long flags;
775
776 if (smu == NULL)
777 return -ENODEV;
778
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;
Benjamin Herrenschmidt730745a2006-01-07 11:30:44 +1100784 cmd->scmd.reply_len = sizeof(cmd->pdata);
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -0700785 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;
791
792 /* Check transfer type, sanitize some "info" fields
793 * based on transfer type and do more checking
794 */
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;
809 }
810
811 /* Finish setting up command based on transfer direction
812 */
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;
822 }
823
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);
829
830
831 /* Enqueue command in i2c list, and if empty, enqueue also in
832 * main command list
833 */
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);
843
844 return 0;
845}
846
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100847/*
848 * Handling of "partitions"
849 */
850
851static int smu_read_datablock(u8 *dest, unsigned int addr, unsigned int len)
852{
853 DECLARE_COMPLETION(comp);
854 unsigned int chunk;
855 struct smu_cmd cmd;
856 int rc;
857 u8 params[8];
858
859 /* We currently use a chunk size of 0xe. We could check the
860 * SMU firmware version and use bigger sizes though
861 */
862 chunk = 0xe;
863
864 while (len) {
865 unsigned int clen = min(len, chunk);
866
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;
878
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;
890 }
891 len -= clen;
892 addr += clen;
893 dest += clen;
894 }
895 return 0;
896}
897
898static struct smu_sdbp_header *smu_create_sdb_partition(int id)
899{
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;
905
906 /* First query the partition info */
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100907 DPRINTK("SMU: Query partition infos ... (irq=%d)\n", smu->db_irq);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100908 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);
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100912 DPRINTK("SMU: done, status: %d, reply_len: %d\n",
913 cmd.cmd.status, cmd.cmd.reply_len);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100914
915 /* Partition doesn't exist (or other error) */
916 if (cmd.cmd.status != 0 || cmd.cmd.reply_len != 6)
917 return NULL;
918
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
924 */
925 tlen = sizeof(struct property) + len + 18;
926
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;
936
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;
942 }
943
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;
949 }
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;
954 }
955
956 return hdr;
957 failure:
958 kfree(prop);
959 return NULL;
960}
961
962/* Note: Only allowed to return error code in pointers (using ERR_PTR)
963 * when interruptible is 1
964 */
965struct smu_sdbp_header *__smu_get_sdb_partition(int id, unsigned int *size,
966 int interruptible)
Benjamin Herrenschmidt43501472005-11-07 14:27:33 +1100967{
968 char pname[32];
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100969 struct smu_sdbp_header *part;
Benjamin Herrenschmidt43501472005-11-07 14:27:33 +1100970
971 if (!smu)
972 return NULL;
973
974 sprintf(pname, "sdb-partition-%02x", id);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100975
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100976 DPRINTK("smu_get_sdb_partition(%02x)\n", id);
977
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100978 if (interruptible) {
979 int rc;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800980 rc = mutex_lock_interruptible(&smu_part_access);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100981 if (rc)
982 return ERR_PTR(rc);
983 } else
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800984 mutex_lock(&smu_part_access);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100985
986 part = (struct smu_sdbp_header *)get_property(smu->of_node,
Benjamin Herrenschmidt43501472005-11-07 14:27:33 +1100987 pname, size);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100988 if (part == NULL) {
Benjamin Herrenschmidt1beb6a72005-12-14 13:10:10 +1100989 DPRINTK("trying to extract from SMU ...\n");
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100990 part = smu_create_sdb_partition(id);
991 if (part != NULL && size)
992 *size = part->len << 2;
993 }
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800994 mutex_unlock(&smu_part_access);
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +1100995 return part;
996}
997
998struct smu_sdbp_header *smu_get_sdb_partition(int id, unsigned int *size)
999{
1000 return __smu_get_sdb_partition(id, size, 0);
Benjamin Herrenschmidt43501472005-11-07 14:27:33 +11001001}
1002EXPORT_SYMBOL(smu_get_sdb_partition);
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -07001003
1004
1005/*
1006 * Userland driver interface
1007 */
1008
1009
1010static LIST_HEAD(smu_clist);
1011static DEFINE_SPINLOCK(smu_clist_lock);
1012
1013enum smu_file_mode {
1014 smu_file_commands,
1015 smu_file_events,
1016 smu_file_closing
1017};
1018
1019struct smu_private
1020{
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];
1028};
1029
1030
1031static int smu_open(struct inode *inode, struct file *file)
1032{
1033 struct smu_private *pp;
1034 unsigned long flags;
1035
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);
1043
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;
1048
1049 return 0;
1050}
1051
1052
1053static void smu_user_cmd_done(struct smu_cmd *cmd, void *misc)
1054{
1055 struct smu_private *pp = misc;
1056
1057 wake_up_all(&pp->wait);
1058}
1059
1060
1061static ssize_t smu_write(struct file *file, const char __user *buf,
1062 size_t count, loff_t *ppos)
1063{
1064 struct smu_private *pp = file->private_data;
1065 unsigned long flags;
1066 struct smu_user_cmd_hdr hdr;
1067 int rc = 0;
1068
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;
Benjamin Herrenschmidt183d0202005-11-07 14:29:02 +11001076 } 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;
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -07001084 } 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;
1090
1091 spin_lock_irqsave(&pp->lock, flags);
1092 if (pp->busy) {
1093 spin_unlock_irqrestore(&pp->lock, flags);
1094 return -EBUSY;
1095 }
1096 pp->busy = 1;
1097 pp->cmd.status = 1;
1098 spin_unlock_irqrestore(&pp->lock, flags);
1099
1100 if (copy_from_user(pp->buffer, buf + sizeof(hdr), hdr.data_len)) {
1101 pp->busy = 0;
1102 return -EFAULT;
1103 }
1104
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;
1116}
1117
1118
1119static ssize_t smu_read_command(struct file *file, struct smu_private *pp,
1120 char __user *buf, size_t count)
1121{
1122 DECLARE_WAITQUEUE(wait, current);
1123 struct smu_user_reply_hdr hdr;
1124 unsigned long flags;
1125 int size, rc = 0;
1126
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);
1147 }
1148 set_current_state(TASK_RUNNING);
1149 remove_wait_queue(&pp->wait, &wait);
1150 }
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;
1168
1169 return rc;
1170}
1171
1172
1173static ssize_t smu_read_events(struct file *file, struct smu_private *pp,
1174 char __user *buf, size_t count)
1175{
1176 /* Not implemented */
1177 msleep_interruptible(1000);
1178 return 0;
1179}
1180
1181
1182static ssize_t smu_read(struct file *file, char __user *buf,
1183 size_t count, loff_t *ppos)
1184{
1185 struct smu_private *pp = file->private_data;
1186
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);
1191
1192 return -EBADFD;
1193}
1194
1195static unsigned int smu_fpoll(struct file *file, poll_table *wait)
1196{
1197 struct smu_private *pp = file->private_data;
1198 unsigned int mask = 0;
1199 unsigned long flags;
1200
1201 if (pp == 0)
1202 return 0;
1203
1204 if (pp->mode == smu_file_commands) {
1205 poll_wait(file, &pp->wait, wait);
1206
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 */
1213 }
1214 return mask;
1215}
1216
1217static int smu_release(struct inode *inode, struct file *file)
1218{
1219 struct smu_private *pp = file->private_data;
1220 unsigned long flags;
1221 unsigned int busy;
1222
1223 if (pp == 0)
1224 return 0;
1225
1226 file->private_data = NULL;
1227
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;
1232
1233 /* Wait for any pending request to complete */
1234 if (busy && pp->cmd.status == 1) {
1235 DECLARE_WAITQUEUE(wait, current);
1236
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);
1245 }
1246 set_current_state(TASK_RUNNING);
1247 remove_wait_queue(&pp->wait, &wait);
1248 }
1249 spin_unlock_irqrestore(&pp->lock, flags);
1250
1251 spin_lock_irqsave(&smu_clist_lock, flags);
1252 list_del(&pp->list);
1253 spin_unlock_irqrestore(&smu_clist_lock, flags);
1254 kfree(pp);
1255
1256 return 0;
1257}
1258
1259
Stephen Rothwell6b67f62c2005-09-27 14:09:39 +10001260static struct file_operations smu_device_fops = {
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -07001261 .llseek = no_llseek,
1262 .read = smu_read,
1263 .write = smu_write,
1264 .poll = smu_fpoll,
1265 .open = smu_open,
1266 .release = smu_release,
1267};
1268
Stephen Rothwell6b67f62c2005-09-27 14:09:39 +10001269static struct miscdevice pmu_device = {
Benjamin Herrenschmidt0365ba72005-09-22 21:44:06 -07001270 MISC_DYNAMIC_MINOR, "smu", &smu_device_fops
1271};
1272
1273static int smu_device_init(void)
1274{
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;
1280}
1281device_initcall(smu_device_init);