blob: a3fa180c15c81926668b2676ca0db64f4bd2878e [file] [log] [blame]
Andres Salomon3ef0e1f2008-04-29 00:59:53 -07001/*
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>
Daniel Drake447b1d42010-10-13 19:10:42 +010020#include <linux/platform_device.h>
Daniel Drake45bb1672011-03-13 15:10:17 +000021#include <linux/of.h>
Daniel Drakebc4ecd52011-06-25 17:34:13 +010022#include <linux/syscore_ops.h>
Daniel Drakea3c81212012-03-27 16:07:40 +010023#include <linux/debugfs.h>
24#include <linux/mutex.h>
Andres Salomon3bf94282012-07-11 01:16:29 -070025#include <linux/olpc-ec.h>
Thomas Gleixnerd5d0e882010-02-22 05:42:04 -080026
Andres Salomon3ef0e1f2008-04-29 00:59:53 -070027#include <asm/geode.h>
Thomas Gleixnerd5d0e882010-02-22 05:42:04 -080028#include <asm/setup.h>
Andres Salomon3ef0e1f2008-04-29 00:59:53 -070029#include <asm/olpc.h>
Andres Salomonfd699c72010-06-18 17:46:53 -040030#include <asm/olpc_ofw.h>
Andres Salomon3ef0e1f2008-04-29 00:59:53 -070031
32struct olpc_platform_t olpc_platform_info;
33EXPORT_SYMBOL_GPL(olpc_platform_info);
34
35static DEFINE_SPINLOCK(ec_lock);
36
Daniel Drakea3c81212012-03-27 16:07:40 +010037/* 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
41static struct dentry *ec_debugfs_dir;
42static DEFINE_MUTEX(ec_debugfs_cmd_lock);
43static unsigned char ec_debugfs_resp[EC_MAX_CMD_REPLY];
44static unsigned int ec_debugfs_resp_bytes;
45
Daniel Drakebc4ecd52011-06-25 17:34:13 +010046/* EC event mask to be applied during suspend (defining wakeup sources). */
47static u16 ec_wakeup_mask;
48
Andres Salomon3ef0e1f2008-04-29 00:59:53 -070049/* 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 */
53static int ec_timeout = EC_BASE_TIMEOUT;
54
55static 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
72static inline unsigned int ibf_status(unsigned int port)
73{
74 return !!(inb(port) & 0x02);
75}
76
77static 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))
83static 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))
103static 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 */
Andres Salomon392a3252012-07-10 19:31:51 -0700129int olpc_ec_cmd_x86(unsigned char cmd, unsigned char *inbuf, size_t inlen,
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700130 unsigned char *outbuf, size_t outlen)
131{
132 unsigned long flags;
133 int ret = -EIO;
134 int i;
Paul Fox286e5b92010-10-01 18:17:19 +0100135 int restarts = 0;
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700136
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
154restart:
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 */
Andres Salomon25971862010-06-16 23:19:28 -0400164 pr_devel("olpc-ec: running cmd 0x%x\n", cmd);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700165 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++) {
Paul Foxa3ea14d2011-07-26 16:42:26 +0100176 pr_devel("olpc-ec: sending cmd arg 0x%x\n", inbuf[i]);
177 outb(inbuf[i], 0x68);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700178 if (wait_on_ibf(0x6c, 0)) {
179 printk(KERN_ERR "olpc-ec: timeout waiting for"
180 " EC accept data!\n");
181 goto err;
182 }
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700183 }
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");
Paul Fox286e5b92010-10-01 18:17:19 +0100191 if (restarts++ < 10)
192 goto restart;
193 goto err;
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700194 }
195 outbuf[i] = inb(0x68);
Andres Salomon25971862010-06-16 23:19:28 -0400196 pr_devel("olpc-ec: received 0x%x\n", outbuf[i]);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700197 }
198 }
199
200 ret = 0;
201err:
202 spin_unlock_irqrestore(&ec_lock, flags);
203 return ret;
204}
Andres Salomon392a3252012-07-10 19:31:51 -0700205EXPORT_SYMBOL_GPL(olpc_ec_cmd_x86);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700206
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100207void olpc_ec_wakeup_set(u16 value)
208{
209 ec_wakeup_mask |= value;
210}
211EXPORT_SYMBOL_GPL(olpc_ec_wakeup_set);
212
213void olpc_ec_wakeup_clear(u16 value)
214{
215 ec_wakeup_mask &= ~value;
216}
217EXPORT_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 */
223bool 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
Daniel Drakea0f30f52011-06-25 17:34:18 +0100237 /*
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
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100246 return false;
247}
248EXPORT_SYMBOL_GPL(olpc_ec_wakeup_available);
249
250int 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}
261EXPORT_SYMBOL_GPL(olpc_ec_mask_write);
262
263int 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}
282EXPORT_SYMBOL_GPL(olpc_ec_sci_query);
283
Daniel Drakea3c81212012-03-27 16:07:40 +0100284static 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
330out:
331 mutex_unlock(&ec_debugfs_cmd_lock);
332 return size;
333}
334
335static 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
354static const struct file_operations ec_debugfs_genops = {
355 .write = ec_debugfs_cmd_write,
356 .read = ec_debugfs_cmd_read,
357};
358
359static 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
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100369static int olpc_ec_suspend(void)
370{
371 return olpc_ec_mask_write(ec_wakeup_mask);
372}
373
Daniel Drake45bb1672011-03-13 15:10:17 +0000374static bool __init check_ofw_architecture(struct device_node *root)
Daniel Drake3e3c4862010-09-23 17:28:46 +0100375{
Daniel Drake45bb1672011-03-13 15:10:17 +0000376 const char *olpc_arch;
377 int propsize;
Daniel Drake3e3c4862010-09-23 17:28:46 +0100378
Daniel Drake45bb1672011-03-13 15:10:17 +0000379 olpc_arch = of_get_property(root, "architecture", &propsize);
Daniel Drake3e3c4862010-09-23 17:28:46 +0100380 return propsize == 5 && strncmp("OLPC", olpc_arch, 5) == 0;
381}
382
Daniel Drake45bb1672011-03-13 15:10:17 +0000383static u32 __init get_board_revision(struct device_node *root)
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700384{
Daniel Drake45bb1672011-03-13 15:10:17 +0000385 int propsize;
386 const __be32 *rev;
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700387
Daniel Drake45bb1672011-03-13 15:10:17 +0000388 rev = of_get_property(root, "board-revision-int", &propsize);
389 if (propsize != 4)
390 return 0;
391
392 return be32_to_cpu(*rev);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700393}
Daniel Drake3e3c4862010-09-23 17:28:46 +0100394
395static bool __init platform_detect(void)
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700396{
Daniel Drake45bb1672011-03-13 15:10:17 +0000397 struct device_node *root = of_find_node_by_path("/");
398 bool success;
399
400 if (!root)
Daniel Drake3e3c4862010-09-23 17:28:46 +0100401 return false;
Daniel Drake45bb1672011-03-13 15:10:17 +0000402
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;
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700411}
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700412
Daniel Drake447b1d42010-10-13 19:10:42 +0100413static 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
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100428static struct syscore_ops olpc_syscore_ops = {
429 .suspend = olpc_ec_suspend,
430};
431
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700432static int __init olpc_init(void)
433{
Daniel Drake447b1d42010-10-13 19:10:42 +0100434 int r = 0;
435
Daniel Drake3e3c4862010-09-23 17:28:46 +0100436 if (!olpc_ofw_present() || !platform_detect())
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700437 return 0;
438
439 spin_lock_init(&ec_lock);
440
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700441 /* 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
Thomas Gleixnerd5d0e882010-02-22 05:42:04 -0800449#ifdef CONFIG_PCI_OLPC
Daniel Drake76fb6572010-09-23 17:28:04 +0100450 /* 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())
Thomas Gleixnerd5d0e882010-02-22 05:42:04 -0800454 x86_init.pci.arch_init = pci_olpc_init;
455#endif
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100456 /* 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;
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700459
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
Daniel Drake447b1d42010-10-13 19:10:42 +0100465 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
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100471 register_syscore_ops(&olpc_syscore_ops);
Daniel Drakea3c81212012-03-27 16:07:40 +0100472 setup_debugfs();
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100473
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700474 return 0;
475}
476
477postcore_initcall(olpc_init);