a3fa180c15c81926668b2676ca0db64f4bd2878e
[cascardo/linux.git] / arch / x86 / platform / olpc / olpc.c
1 /*
2  * Support for the OLPC DCON and OLPC EC access
3  *
4  * Copyright © 2006  Advanced Micro Devices, Inc.
5  * Copyright © 2007-2008  Andres Salomon <dilinger@debian.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/delay.h>
17 #include <linux/spinlock.h>
18 #include <linux/io.h>
19 #include <linux/string.h>
20 #include <linux/platform_device.h>
21 #include <linux/of.h>
22 #include <linux/syscore_ops.h>
23 #include <linux/debugfs.h>
24 #include <linux/mutex.h>
25 #include <linux/olpc-ec.h>
26
27 #include <asm/geode.h>
28 #include <asm/setup.h>
29 #include <asm/olpc.h>
30 #include <asm/olpc_ofw.h>
31
32 struct olpc_platform_t olpc_platform_info;
33 EXPORT_SYMBOL_GPL(olpc_platform_info);
34
35 static DEFINE_SPINLOCK(ec_lock);
36
37 /* debugfs interface to EC commands */
38 #define EC_MAX_CMD_ARGS (5 + 1) /* cmd byte + 5 args */
39 #define EC_MAX_CMD_REPLY (8)
40
41 static struct dentry *ec_debugfs_dir;
42 static DEFINE_MUTEX(ec_debugfs_cmd_lock);
43 static unsigned char ec_debugfs_resp[EC_MAX_CMD_REPLY];
44 static unsigned int ec_debugfs_resp_bytes;
45
46 /* EC event mask to be applied during suspend (defining wakeup sources). */
47 static u16 ec_wakeup_mask;
48
49 /* what the timeout *should* be (in ms) */
50 #define EC_BASE_TIMEOUT 20
51
52 /* the timeout that bugs in the EC might force us to actually use */
53 static int ec_timeout = EC_BASE_TIMEOUT;
54
55 static int __init olpc_ec_timeout_set(char *str)
56 {
57         if (get_option(&str, &ec_timeout) != 1) {
58                 ec_timeout = EC_BASE_TIMEOUT;
59                 printk(KERN_ERR "olpc-ec:  invalid argument to "
60                                 "'olpc_ec_timeout=', ignoring!\n");
61         }
62         printk(KERN_DEBUG "olpc-ec:  using %d ms delay for EC commands.\n",
63                         ec_timeout);
64         return 1;
65 }
66 __setup("olpc_ec_timeout=", olpc_ec_timeout_set);
67
68 /*
69  * These {i,o}bf_status functions return whether the buffers are full or not.
70  */
71
72 static inline unsigned int ibf_status(unsigned int port)
73 {
74         return !!(inb(port) & 0x02);
75 }
76
77 static inline unsigned int obf_status(unsigned int port)
78 {
79         return inb(port) & 0x01;
80 }
81
82 #define wait_on_ibf(p, d) __wait_on_ibf(__LINE__, (p), (d))
83 static int __wait_on_ibf(unsigned int line, unsigned int port, int desired)
84 {
85         unsigned int timeo;
86         int state = ibf_status(port);
87
88         for (timeo = ec_timeout; state != desired && timeo; timeo--) {
89                 mdelay(1);
90                 state = ibf_status(port);
91         }
92
93         if ((state == desired) && (ec_timeout > EC_BASE_TIMEOUT) &&
94                         timeo < (ec_timeout - EC_BASE_TIMEOUT)) {
95                 printk(KERN_WARNING "olpc-ec:  %d: waited %u ms for IBF!\n",
96                                 line, ec_timeout - timeo);
97         }
98
99         return !(state == desired);
100 }
101
102 #define wait_on_obf(p, d) __wait_on_obf(__LINE__, (p), (d))
103 static int __wait_on_obf(unsigned int line, unsigned int port, int desired)
104 {
105         unsigned int timeo;
106         int state = obf_status(port);
107
108         for (timeo = ec_timeout; state != desired && timeo; timeo--) {
109                 mdelay(1);
110                 state = obf_status(port);
111         }
112
113         if ((state == desired) && (ec_timeout > EC_BASE_TIMEOUT) &&
114                         timeo < (ec_timeout - EC_BASE_TIMEOUT)) {
115                 printk(KERN_WARNING "olpc-ec:  %d: waited %u ms for OBF!\n",
116                                 line, ec_timeout - timeo);
117         }
118
119         return !(state == desired);
120 }
121
122 /*
123  * This allows the kernel to run Embedded Controller commands.  The EC is
124  * documented at <http://wiki.laptop.org/go/Embedded_controller>, and the
125  * available EC commands are here:
126  * <http://wiki.laptop.org/go/Ec_specification>.  Unfortunately, while
127  * OpenFirmware's source is available, the EC's is not.
128  */
129 int olpc_ec_cmd_x86(unsigned char cmd, unsigned char *inbuf, size_t inlen,
130                 unsigned char *outbuf,  size_t outlen)
131 {
132         unsigned long flags;
133         int ret = -EIO;
134         int i;
135         int restarts = 0;
136
137         spin_lock_irqsave(&ec_lock, flags);
138
139         /* Clear OBF */
140         for (i = 0; i < 10 && (obf_status(0x6c) == 1); i++)
141                 inb(0x68);
142         if (i == 10) {
143                 printk(KERN_ERR "olpc-ec:  timeout while attempting to "
144                                 "clear OBF flag!\n");
145                 goto err;
146         }
147
148         if (wait_on_ibf(0x6c, 0)) {
149                 printk(KERN_ERR "olpc-ec:  timeout waiting for EC to "
150                                 "quiesce!\n");
151                 goto err;
152         }
153
154 restart:
155         /*
156          * Note that if we time out during any IBF checks, that's a failure;
157          * we have to return.  There's no way for the kernel to clear that.
158          *
159          * If we time out during an OBF check, we can restart the command;
160          * reissuing it will clear the OBF flag, and we should be alright.
161          * The OBF flag will sometimes misbehave due to what we believe
162          * is a hardware quirk..
163          */
164         pr_devel("olpc-ec:  running cmd 0x%x\n", cmd);
165         outb(cmd, 0x6c);
166
167         if (wait_on_ibf(0x6c, 0)) {
168                 printk(KERN_ERR "olpc-ec:  timeout waiting for EC to read "
169                                 "command!\n");
170                 goto err;
171         }
172
173         if (inbuf && inlen) {
174                 /* write data to EC */
175                 for (i = 0; i < inlen; i++) {
176                         pr_devel("olpc-ec:  sending cmd arg 0x%x\n", inbuf[i]);
177                         outb(inbuf[i], 0x68);
178                         if (wait_on_ibf(0x6c, 0)) {
179                                 printk(KERN_ERR "olpc-ec:  timeout waiting for"
180                                                 " EC accept data!\n");
181                                 goto err;
182                         }
183                 }
184         }
185         if (outbuf && outlen) {
186                 /* read data from EC */
187                 for (i = 0; i < outlen; i++) {
188                         if (wait_on_obf(0x6c, 1)) {
189                                 printk(KERN_ERR "olpc-ec:  timeout waiting for"
190                                                 " EC to provide data!\n");
191                                 if (restarts++ < 10)
192                                         goto restart;
193                                 goto err;
194                         }
195                         outbuf[i] = inb(0x68);
196                         pr_devel("olpc-ec:  received 0x%x\n", outbuf[i]);
197                 }
198         }
199
200         ret = 0;
201 err:
202         spin_unlock_irqrestore(&ec_lock, flags);
203         return ret;
204 }
205 EXPORT_SYMBOL_GPL(olpc_ec_cmd_x86);
206
207 void olpc_ec_wakeup_set(u16 value)
208 {
209         ec_wakeup_mask |= value;
210 }
211 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_set);
212
213 void olpc_ec_wakeup_clear(u16 value)
214 {
215         ec_wakeup_mask &= ~value;
216 }
217 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_clear);
218
219 /*
220  * Returns true if the compile and runtime configurations allow for EC events
221  * to wake the system.
222  */
223 bool olpc_ec_wakeup_available(void)
224 {
225         if (!machine_is_olpc())
226                 return false;
227
228         /*
229          * XO-1 EC wakeups are available when olpc-xo1-sci driver is
230          * compiled in
231          */
232 #ifdef CONFIG_OLPC_XO1_SCI
233         if (olpc_platform_info.boardrev < olpc_board_pre(0xd0)) /* XO-1 */
234                 return true;
235 #endif
236
237         /*
238          * XO-1.5 EC wakeups are available when olpc-xo15-sci driver is
239          * compiled in
240          */
241 #ifdef CONFIG_OLPC_XO15_SCI
242         if (olpc_platform_info.boardrev >= olpc_board_pre(0xd0)) /* XO-1.5 */
243                 return true;
244 #endif
245
246         return false;
247 }
248 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_available);
249
250 int olpc_ec_mask_write(u16 bits)
251 {
252         if (olpc_platform_info.flags & OLPC_F_EC_WIDE_SCI) {
253                 __be16 ec_word = cpu_to_be16(bits);
254                 return olpc_ec_cmd(EC_WRITE_EXT_SCI_MASK, (void *) &ec_word, 2,
255                                    NULL, 0);
256         } else {
257                 unsigned char ec_byte = bits & 0xff;
258                 return olpc_ec_cmd(EC_WRITE_SCI_MASK, &ec_byte, 1, NULL, 0);
259         }
260 }
261 EXPORT_SYMBOL_GPL(olpc_ec_mask_write);
262
263 int olpc_ec_sci_query(u16 *sci_value)
264 {
265         int ret;
266
267         if (olpc_platform_info.flags & OLPC_F_EC_WIDE_SCI) {
268                 __be16 ec_word;
269                 ret = olpc_ec_cmd(EC_EXT_SCI_QUERY,
270                         NULL, 0, (void *) &ec_word, 2);
271                 if (ret == 0)
272                         *sci_value = be16_to_cpu(ec_word);
273         } else {
274                 unsigned char ec_byte;
275                 ret = olpc_ec_cmd(EC_SCI_QUERY, NULL, 0, &ec_byte, 1);
276                 if (ret == 0)
277                         *sci_value = ec_byte;
278         }
279
280         return ret;
281 }
282 EXPORT_SYMBOL_GPL(olpc_ec_sci_query);
283
284 static ssize_t ec_debugfs_cmd_write(struct file *file, const char __user *buf,
285                                     size_t size, loff_t *ppos)
286 {
287         int i, m;
288         unsigned char ec_cmd[EC_MAX_CMD_ARGS];
289         unsigned int ec_cmd_int[EC_MAX_CMD_ARGS];
290         char cmdbuf[64];
291         int ec_cmd_bytes;
292
293         mutex_lock(&ec_debugfs_cmd_lock);
294
295         size = simple_write_to_buffer(cmdbuf, sizeof(cmdbuf), ppos, buf, size);
296
297         m = sscanf(cmdbuf, "%x:%u %x %x %x %x %x", &ec_cmd_int[0],
298                    &ec_debugfs_resp_bytes,
299                    &ec_cmd_int[1], &ec_cmd_int[2], &ec_cmd_int[3],
300                    &ec_cmd_int[4], &ec_cmd_int[5]);
301         if (m < 2 || ec_debugfs_resp_bytes > EC_MAX_CMD_REPLY) {
302                 /* reset to prevent overflow on read */
303                 ec_debugfs_resp_bytes = 0;
304
305                 printk(KERN_DEBUG "olpc-ec: bad ec cmd:  "
306                        "cmd:response-count [arg1 [arg2 ...]]\n");
307                 size = -EINVAL;
308                 goto out;
309         }
310
311         /* convert scanf'd ints to char */
312         ec_cmd_bytes = m - 2;
313         for (i = 0; i <= ec_cmd_bytes; i++)
314                 ec_cmd[i] = ec_cmd_int[i];
315
316         printk(KERN_DEBUG "olpc-ec: debugfs cmd 0x%02x with %d args "
317                "%02x %02x %02x %02x %02x, want %d returns\n",
318                ec_cmd[0], ec_cmd_bytes, ec_cmd[1], ec_cmd[2], ec_cmd[3],
319                ec_cmd[4], ec_cmd[5], ec_debugfs_resp_bytes);
320
321         olpc_ec_cmd(ec_cmd[0], (ec_cmd_bytes == 0) ? NULL : &ec_cmd[1],
322                     ec_cmd_bytes, ec_debugfs_resp, ec_debugfs_resp_bytes);
323
324         printk(KERN_DEBUG "olpc-ec: response "
325                "%02x %02x %02x %02x %02x %02x %02x %02x (%d bytes expected)\n",
326                ec_debugfs_resp[0], ec_debugfs_resp[1], ec_debugfs_resp[2],
327                ec_debugfs_resp[3], ec_debugfs_resp[4], ec_debugfs_resp[5],
328                ec_debugfs_resp[6], ec_debugfs_resp[7], ec_debugfs_resp_bytes);
329
330 out:
331         mutex_unlock(&ec_debugfs_cmd_lock);
332         return size;
333 }
334
335 static ssize_t ec_debugfs_cmd_read(struct file *file, char __user *buf,
336                                    size_t size, loff_t *ppos)
337 {
338         unsigned int i, r;
339         char *rp;
340         char respbuf[64];
341
342         mutex_lock(&ec_debugfs_cmd_lock);
343         rp = respbuf;
344         rp += sprintf(rp, "%02x", ec_debugfs_resp[0]);
345         for (i = 1; i < ec_debugfs_resp_bytes; i++)
346                 rp += sprintf(rp, ", %02x", ec_debugfs_resp[i]);
347         mutex_unlock(&ec_debugfs_cmd_lock);
348         rp += sprintf(rp, "\n");
349
350         r = rp - respbuf;
351         return simple_read_from_buffer(buf, size, ppos, respbuf, r);
352 }
353
354 static const struct file_operations ec_debugfs_genops = {
355         .write   = ec_debugfs_cmd_write,
356         .read    = ec_debugfs_cmd_read,
357 };
358
359 static void setup_debugfs(void)
360 {
361         ec_debugfs_dir = debugfs_create_dir("olpc-ec", 0);
362         if (ec_debugfs_dir == ERR_PTR(-ENODEV))
363                 return;
364
365         debugfs_create_file("cmd", 0600, ec_debugfs_dir, NULL,
366                             &ec_debugfs_genops);
367 }
368
369 static int olpc_ec_suspend(void)
370 {
371         return olpc_ec_mask_write(ec_wakeup_mask);
372 }
373
374 static bool __init check_ofw_architecture(struct device_node *root)
375 {
376         const char *olpc_arch;
377         int propsize;
378
379         olpc_arch = of_get_property(root, "architecture", &propsize);
380         return propsize == 5 && strncmp("OLPC", olpc_arch, 5) == 0;
381 }
382
383 static u32 __init get_board_revision(struct device_node *root)
384 {
385         int propsize;
386         const __be32 *rev;
387
388         rev = of_get_property(root, "board-revision-int", &propsize);
389         if (propsize != 4)
390                 return 0;
391
392         return be32_to_cpu(*rev);
393 }
394
395 static bool __init platform_detect(void)
396 {
397         struct device_node *root = of_find_node_by_path("/");
398         bool success;
399
400         if (!root)
401                 return false;
402
403         success = check_ofw_architecture(root);
404         if (success) {
405                 olpc_platform_info.boardrev = get_board_revision(root);
406                 olpc_platform_info.flags |= OLPC_F_PRESENT;
407         }
408
409         of_node_put(root);
410         return success;
411 }
412
413 static int __init add_xo1_platform_devices(void)
414 {
415         struct platform_device *pdev;
416
417         pdev = platform_device_register_simple("xo1-rfkill", -1, NULL, 0);
418         if (IS_ERR(pdev))
419                 return PTR_ERR(pdev);
420
421         pdev = platform_device_register_simple("olpc-xo1", -1, NULL, 0);
422         if (IS_ERR(pdev))
423                 return PTR_ERR(pdev);
424
425         return 0;
426 }
427
428 static struct syscore_ops olpc_syscore_ops = {
429         .suspend = olpc_ec_suspend,
430 };
431
432 static int __init olpc_init(void)
433 {
434         int r = 0;
435
436         if (!olpc_ofw_present() || !platform_detect())
437                 return 0;
438
439         spin_lock_init(&ec_lock);
440
441         /* assume B1 and above models always have a DCON */
442         if (olpc_board_at_least(olpc_board(0xb1)))
443                 olpc_platform_info.flags |= OLPC_F_DCON;
444
445         /* get the EC revision */
446         olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0,
447                         (unsigned char *) &olpc_platform_info.ecver, 1);
448
449 #ifdef CONFIG_PCI_OLPC
450         /* If the VSA exists let it emulate PCI, if not emulate in kernel.
451          * XO-1 only. */
452         if (olpc_platform_info.boardrev < olpc_board_pre(0xd0) &&
453                         !cs5535_has_vsa2())
454                 x86_init.pci.arch_init = pci_olpc_init;
455 #endif
456         /* EC version 0x5f adds support for wide SCI mask */
457         if (olpc_platform_info.ecver >= 0x5f)
458                 olpc_platform_info.flags |= OLPC_F_EC_WIDE_SCI;
459
460         printk(KERN_INFO "OLPC board revision %s%X (EC=%x)\n",
461                         ((olpc_platform_info.boardrev & 0xf) < 8) ? "pre" : "",
462                         olpc_platform_info.boardrev >> 4,
463                         olpc_platform_info.ecver);
464
465         if (olpc_platform_info.boardrev < olpc_board_pre(0xd0)) { /* XO-1 */
466                 r = add_xo1_platform_devices();
467                 if (r)
468                         return r;
469         }
470
471         register_syscore_ops(&olpc_syscore_ops);
472         setup_debugfs();
473
474         return 0;
475 }
476
477 postcore_initcall(olpc_init);