blob: 45900968fb89c47b572e3aef971237fcd2bc2b9d [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>
Andres Salomon3ef0e1f2008-04-29 00:59:53 -070017#include <linux/io.h>
18#include <linux/string.h>
Daniel Drake447b1d42010-10-13 19:10:42 +010019#include <linux/platform_device.h>
Daniel Drake45bb1672011-03-13 15:10:17 +000020#include <linux/of.h>
Daniel Drakebc4ecd52011-06-25 17:34:13 +010021#include <linux/syscore_ops.h>
Daniel Drakea3c81212012-03-27 16:07:40 +010022#include <linux/debugfs.h>
23#include <linux/mutex.h>
Andres Salomon3bf94282012-07-11 01:16:29 -070024#include <linux/olpc-ec.h>
Thomas Gleixnerd5d0e882010-02-22 05:42:04 -080025
Andres Salomon3ef0e1f2008-04-29 00:59:53 -070026#include <asm/geode.h>
Thomas Gleixnerd5d0e882010-02-22 05:42:04 -080027#include <asm/setup.h>
Andres Salomon3ef0e1f2008-04-29 00:59:53 -070028#include <asm/olpc.h>
Andres Salomonfd699c72010-06-18 17:46:53 -040029#include <asm/olpc_ofw.h>
Andres Salomon3ef0e1f2008-04-29 00:59:53 -070030
31struct olpc_platform_t olpc_platform_info;
32EXPORT_SYMBOL_GPL(olpc_platform_info);
33
Daniel Drakea3c81212012-03-27 16:07:40 +010034/* debugfs interface to EC commands */
35#define EC_MAX_CMD_ARGS (5 + 1) /* cmd byte + 5 args */
36#define EC_MAX_CMD_REPLY (8)
37
38static struct dentry *ec_debugfs_dir;
39static DEFINE_MUTEX(ec_debugfs_cmd_lock);
40static unsigned char ec_debugfs_resp[EC_MAX_CMD_REPLY];
41static unsigned int ec_debugfs_resp_bytes;
42
Daniel Drakebc4ecd52011-06-25 17:34:13 +010043/* EC event mask to be applied during suspend (defining wakeup sources). */
44static u16 ec_wakeup_mask;
45
Andres Salomon3ef0e1f2008-04-29 00:59:53 -070046/* what the timeout *should* be (in ms) */
47#define EC_BASE_TIMEOUT 20
48
49/* the timeout that bugs in the EC might force us to actually use */
50static int ec_timeout = EC_BASE_TIMEOUT;
51
52static int __init olpc_ec_timeout_set(char *str)
53{
54 if (get_option(&str, &ec_timeout) != 1) {
55 ec_timeout = EC_BASE_TIMEOUT;
56 printk(KERN_ERR "olpc-ec: invalid argument to "
57 "'olpc_ec_timeout=', ignoring!\n");
58 }
59 printk(KERN_DEBUG "olpc-ec: using %d ms delay for EC commands.\n",
60 ec_timeout);
61 return 1;
62}
63__setup("olpc_ec_timeout=", olpc_ec_timeout_set);
64
65/*
66 * These {i,o}bf_status functions return whether the buffers are full or not.
67 */
68
69static inline unsigned int ibf_status(unsigned int port)
70{
71 return !!(inb(port) & 0x02);
72}
73
74static inline unsigned int obf_status(unsigned int port)
75{
76 return inb(port) & 0x01;
77}
78
79#define wait_on_ibf(p, d) __wait_on_ibf(__LINE__, (p), (d))
80static int __wait_on_ibf(unsigned int line, unsigned int port, int desired)
81{
82 unsigned int timeo;
83 int state = ibf_status(port);
84
85 for (timeo = ec_timeout; state != desired && timeo; timeo--) {
86 mdelay(1);
87 state = ibf_status(port);
88 }
89
90 if ((state == desired) && (ec_timeout > EC_BASE_TIMEOUT) &&
91 timeo < (ec_timeout - EC_BASE_TIMEOUT)) {
92 printk(KERN_WARNING "olpc-ec: %d: waited %u ms for IBF!\n",
93 line, ec_timeout - timeo);
94 }
95
96 return !(state == desired);
97}
98
99#define wait_on_obf(p, d) __wait_on_obf(__LINE__, (p), (d))
100static int __wait_on_obf(unsigned int line, unsigned int port, int desired)
101{
102 unsigned int timeo;
103 int state = obf_status(port);
104
105 for (timeo = ec_timeout; state != desired && timeo; timeo--) {
106 mdelay(1);
107 state = obf_status(port);
108 }
109
110 if ((state == desired) && (ec_timeout > EC_BASE_TIMEOUT) &&
111 timeo < (ec_timeout - EC_BASE_TIMEOUT)) {
112 printk(KERN_WARNING "olpc-ec: %d: waited %u ms for OBF!\n",
113 line, ec_timeout - timeo);
114 }
115
116 return !(state == desired);
117}
118
119/*
120 * This allows the kernel to run Embedded Controller commands. The EC is
121 * documented at <http://wiki.laptop.org/go/Embedded_controller>, and the
122 * available EC commands are here:
123 * <http://wiki.laptop.org/go/Ec_specification>. Unfortunately, while
124 * OpenFirmware's source is available, the EC's is not.
125 */
Andres Salomon85f90cf2012-07-12 17:57:28 -0700126static int olpc_xo1_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf,
127 size_t outlen, void *arg)
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700128{
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700129 int ret = -EIO;
130 int i;
Paul Fox286e5b92010-10-01 18:17:19 +0100131 int restarts = 0;
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700132
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700133 /* Clear OBF */
134 for (i = 0; i < 10 && (obf_status(0x6c) == 1); i++)
135 inb(0x68);
136 if (i == 10) {
137 printk(KERN_ERR "olpc-ec: timeout while attempting to "
138 "clear OBF flag!\n");
139 goto err;
140 }
141
142 if (wait_on_ibf(0x6c, 0)) {
143 printk(KERN_ERR "olpc-ec: timeout waiting for EC to "
144 "quiesce!\n");
145 goto err;
146 }
147
148restart:
149 /*
150 * Note that if we time out during any IBF checks, that's a failure;
151 * we have to return. There's no way for the kernel to clear that.
152 *
153 * If we time out during an OBF check, we can restart the command;
154 * reissuing it will clear the OBF flag, and we should be alright.
155 * The OBF flag will sometimes misbehave due to what we believe
156 * is a hardware quirk..
157 */
Andres Salomon25971862010-06-16 23:19:28 -0400158 pr_devel("olpc-ec: running cmd 0x%x\n", cmd);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700159 outb(cmd, 0x6c);
160
161 if (wait_on_ibf(0x6c, 0)) {
162 printk(KERN_ERR "olpc-ec: timeout waiting for EC to read "
163 "command!\n");
164 goto err;
165 }
166
167 if (inbuf && inlen) {
168 /* write data to EC */
169 for (i = 0; i < inlen; i++) {
Paul Foxa3ea14d2011-07-26 16:42:26 +0100170 pr_devel("olpc-ec: sending cmd arg 0x%x\n", inbuf[i]);
171 outb(inbuf[i], 0x68);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700172 if (wait_on_ibf(0x6c, 0)) {
173 printk(KERN_ERR "olpc-ec: timeout waiting for"
174 " EC accept data!\n");
175 goto err;
176 }
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700177 }
178 }
179 if (outbuf && outlen) {
180 /* read data from EC */
181 for (i = 0; i < outlen; i++) {
182 if (wait_on_obf(0x6c, 1)) {
183 printk(KERN_ERR "olpc-ec: timeout waiting for"
184 " EC to provide data!\n");
Paul Fox286e5b92010-10-01 18:17:19 +0100185 if (restarts++ < 10)
186 goto restart;
187 goto err;
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700188 }
189 outbuf[i] = inb(0x68);
Andres Salomon25971862010-06-16 23:19:28 -0400190 pr_devel("olpc-ec: received 0x%x\n", outbuf[i]);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700191 }
192 }
193
194 ret = 0;
195err:
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700196 return ret;
197}
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700198
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100199void olpc_ec_wakeup_set(u16 value)
200{
201 ec_wakeup_mask |= value;
202}
203EXPORT_SYMBOL_GPL(olpc_ec_wakeup_set);
204
205void olpc_ec_wakeup_clear(u16 value)
206{
207 ec_wakeup_mask &= ~value;
208}
209EXPORT_SYMBOL_GPL(olpc_ec_wakeup_clear);
210
211/*
212 * Returns true if the compile and runtime configurations allow for EC events
213 * to wake the system.
214 */
215bool olpc_ec_wakeup_available(void)
216{
217 if (!machine_is_olpc())
218 return false;
219
220 /*
221 * XO-1 EC wakeups are available when olpc-xo1-sci driver is
222 * compiled in
223 */
224#ifdef CONFIG_OLPC_XO1_SCI
225 if (olpc_platform_info.boardrev < olpc_board_pre(0xd0)) /* XO-1 */
226 return true;
227#endif
228
Daniel Drakea0f30f52011-06-25 17:34:18 +0100229 /*
230 * XO-1.5 EC wakeups are available when olpc-xo15-sci driver is
231 * compiled in
232 */
233#ifdef CONFIG_OLPC_XO15_SCI
234 if (olpc_platform_info.boardrev >= olpc_board_pre(0xd0)) /* XO-1.5 */
235 return true;
236#endif
237
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100238 return false;
239}
240EXPORT_SYMBOL_GPL(olpc_ec_wakeup_available);
241
242int olpc_ec_mask_write(u16 bits)
243{
244 if (olpc_platform_info.flags & OLPC_F_EC_WIDE_SCI) {
245 __be16 ec_word = cpu_to_be16(bits);
246 return olpc_ec_cmd(EC_WRITE_EXT_SCI_MASK, (void *) &ec_word, 2,
247 NULL, 0);
248 } else {
249 unsigned char ec_byte = bits & 0xff;
250 return olpc_ec_cmd(EC_WRITE_SCI_MASK, &ec_byte, 1, NULL, 0);
251 }
252}
253EXPORT_SYMBOL_GPL(olpc_ec_mask_write);
254
255int olpc_ec_sci_query(u16 *sci_value)
256{
257 int ret;
258
259 if (olpc_platform_info.flags & OLPC_F_EC_WIDE_SCI) {
260 __be16 ec_word;
261 ret = olpc_ec_cmd(EC_EXT_SCI_QUERY,
262 NULL, 0, (void *) &ec_word, 2);
263 if (ret == 0)
264 *sci_value = be16_to_cpu(ec_word);
265 } else {
266 unsigned char ec_byte;
267 ret = olpc_ec_cmd(EC_SCI_QUERY, NULL, 0, &ec_byte, 1);
268 if (ret == 0)
269 *sci_value = ec_byte;
270 }
271
272 return ret;
273}
274EXPORT_SYMBOL_GPL(olpc_ec_sci_query);
275
Daniel Drakea3c81212012-03-27 16:07:40 +0100276static ssize_t ec_debugfs_cmd_write(struct file *file, const char __user *buf,
277 size_t size, loff_t *ppos)
278{
279 int i, m;
280 unsigned char ec_cmd[EC_MAX_CMD_ARGS];
281 unsigned int ec_cmd_int[EC_MAX_CMD_ARGS];
282 char cmdbuf[64];
283 int ec_cmd_bytes;
284
285 mutex_lock(&ec_debugfs_cmd_lock);
286
287 size = simple_write_to_buffer(cmdbuf, sizeof(cmdbuf), ppos, buf, size);
288
289 m = sscanf(cmdbuf, "%x:%u %x %x %x %x %x", &ec_cmd_int[0],
290 &ec_debugfs_resp_bytes,
291 &ec_cmd_int[1], &ec_cmd_int[2], &ec_cmd_int[3],
292 &ec_cmd_int[4], &ec_cmd_int[5]);
293 if (m < 2 || ec_debugfs_resp_bytes > EC_MAX_CMD_REPLY) {
294 /* reset to prevent overflow on read */
295 ec_debugfs_resp_bytes = 0;
296
297 printk(KERN_DEBUG "olpc-ec: bad ec cmd: "
298 "cmd:response-count [arg1 [arg2 ...]]\n");
299 size = -EINVAL;
300 goto out;
301 }
302
303 /* convert scanf'd ints to char */
304 ec_cmd_bytes = m - 2;
305 for (i = 0; i <= ec_cmd_bytes; i++)
306 ec_cmd[i] = ec_cmd_int[i];
307
308 printk(KERN_DEBUG "olpc-ec: debugfs cmd 0x%02x with %d args "
309 "%02x %02x %02x %02x %02x, want %d returns\n",
310 ec_cmd[0], ec_cmd_bytes, ec_cmd[1], ec_cmd[2], ec_cmd[3],
311 ec_cmd[4], ec_cmd[5], ec_debugfs_resp_bytes);
312
313 olpc_ec_cmd(ec_cmd[0], (ec_cmd_bytes == 0) ? NULL : &ec_cmd[1],
314 ec_cmd_bytes, ec_debugfs_resp, ec_debugfs_resp_bytes);
315
316 printk(KERN_DEBUG "olpc-ec: response "
317 "%02x %02x %02x %02x %02x %02x %02x %02x (%d bytes expected)\n",
318 ec_debugfs_resp[0], ec_debugfs_resp[1], ec_debugfs_resp[2],
319 ec_debugfs_resp[3], ec_debugfs_resp[4], ec_debugfs_resp[5],
320 ec_debugfs_resp[6], ec_debugfs_resp[7], ec_debugfs_resp_bytes);
321
322out:
323 mutex_unlock(&ec_debugfs_cmd_lock);
324 return size;
325}
326
327static ssize_t ec_debugfs_cmd_read(struct file *file, char __user *buf,
328 size_t size, loff_t *ppos)
329{
330 unsigned int i, r;
331 char *rp;
332 char respbuf[64];
333
334 mutex_lock(&ec_debugfs_cmd_lock);
335 rp = respbuf;
336 rp += sprintf(rp, "%02x", ec_debugfs_resp[0]);
337 for (i = 1; i < ec_debugfs_resp_bytes; i++)
338 rp += sprintf(rp, ", %02x", ec_debugfs_resp[i]);
339 mutex_unlock(&ec_debugfs_cmd_lock);
340 rp += sprintf(rp, "\n");
341
342 r = rp - respbuf;
343 return simple_read_from_buffer(buf, size, ppos, respbuf, r);
344}
345
346static const struct file_operations ec_debugfs_genops = {
347 .write = ec_debugfs_cmd_write,
348 .read = ec_debugfs_cmd_read,
349};
350
351static void setup_debugfs(void)
352{
353 ec_debugfs_dir = debugfs_create_dir("olpc-ec", 0);
354 if (ec_debugfs_dir == ERR_PTR(-ENODEV))
355 return;
356
357 debugfs_create_file("cmd", 0600, ec_debugfs_dir, NULL,
358 &ec_debugfs_genops);
359}
360
Andres Salomon85f90cf2012-07-12 17:57:28 -0700361static int olpc_ec_suspend(struct platform_device *pdev)
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100362{
363 return olpc_ec_mask_write(ec_wakeup_mask);
364}
365
Daniel Drake45bb1672011-03-13 15:10:17 +0000366static bool __init check_ofw_architecture(struct device_node *root)
Daniel Drake3e3c4862010-09-23 17:28:46 +0100367{
Daniel Drake45bb1672011-03-13 15:10:17 +0000368 const char *olpc_arch;
369 int propsize;
Daniel Drake3e3c4862010-09-23 17:28:46 +0100370
Daniel Drake45bb1672011-03-13 15:10:17 +0000371 olpc_arch = of_get_property(root, "architecture", &propsize);
Daniel Drake3e3c4862010-09-23 17:28:46 +0100372 return propsize == 5 && strncmp("OLPC", olpc_arch, 5) == 0;
373}
374
Daniel Drake45bb1672011-03-13 15:10:17 +0000375static u32 __init get_board_revision(struct device_node *root)
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700376{
Daniel Drake45bb1672011-03-13 15:10:17 +0000377 int propsize;
378 const __be32 *rev;
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700379
Daniel Drake45bb1672011-03-13 15:10:17 +0000380 rev = of_get_property(root, "board-revision-int", &propsize);
381 if (propsize != 4)
382 return 0;
383
384 return be32_to_cpu(*rev);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700385}
Daniel Drake3e3c4862010-09-23 17:28:46 +0100386
387static bool __init platform_detect(void)
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700388{
Daniel Drake45bb1672011-03-13 15:10:17 +0000389 struct device_node *root = of_find_node_by_path("/");
390 bool success;
391
392 if (!root)
Daniel Drake3e3c4862010-09-23 17:28:46 +0100393 return false;
Daniel Drake45bb1672011-03-13 15:10:17 +0000394
395 success = check_ofw_architecture(root);
396 if (success) {
397 olpc_platform_info.boardrev = get_board_revision(root);
398 olpc_platform_info.flags |= OLPC_F_PRESENT;
399 }
400
401 of_node_put(root);
402 return success;
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700403}
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700404
Daniel Drake447b1d42010-10-13 19:10:42 +0100405static int __init add_xo1_platform_devices(void)
406{
407 struct platform_device *pdev;
408
409 pdev = platform_device_register_simple("xo1-rfkill", -1, NULL, 0);
410 if (IS_ERR(pdev))
411 return PTR_ERR(pdev);
412
413 pdev = platform_device_register_simple("olpc-xo1", -1, NULL, 0);
414 if (IS_ERR(pdev))
415 return PTR_ERR(pdev);
416
417 return 0;
418}
419
Andres Salomon85f90cf2012-07-12 17:57:28 -0700420static int olpc_xo1_ec_probe(struct platform_device *pdev)
421{
422 /* get the EC revision */
423 olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0,
424 (unsigned char *) &olpc_platform_info.ecver, 1);
425
426 /* EC version 0x5f adds support for wide SCI mask */
427 if (olpc_platform_info.ecver >= 0x5f)
428 olpc_platform_info.flags |= OLPC_F_EC_WIDE_SCI;
429
430 pr_info("OLPC board revision %s%X (EC=%x)\n",
431 ((olpc_platform_info.boardrev & 0xf) < 8) ? "pre" : "",
432 olpc_platform_info.boardrev >> 4,
433 olpc_platform_info.ecver);
434
435 return 0;
436}
437
438static struct olpc_ec_driver ec_xo1_driver = {
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100439 .suspend = olpc_ec_suspend,
Andres Salomon85f90cf2012-07-12 17:57:28 -0700440 .probe = olpc_xo1_ec_probe,
441 .ec_cmd = olpc_xo1_ec_cmd,
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100442};
443
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700444static int __init olpc_init(void)
445{
Daniel Drake447b1d42010-10-13 19:10:42 +0100446 int r = 0;
447
Daniel Drake3e3c4862010-09-23 17:28:46 +0100448 if (!olpc_ofw_present() || !platform_detect())
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700449 return 0;
450
Andres Salomon85f90cf2012-07-12 17:57:28 -0700451 /* register the XO-1 and 1.5-specific EC handler */
452 olpc_ec_driver_register(&ec_xo1_driver, NULL);
453 platform_device_register_simple("olpc-ec", -1, NULL, 0);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700454
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700455 /* assume B1 and above models always have a DCON */
456 if (olpc_board_at_least(olpc_board(0xb1)))
457 olpc_platform_info.flags |= OLPC_F_DCON;
458
Thomas Gleixnerd5d0e882010-02-22 05:42:04 -0800459#ifdef CONFIG_PCI_OLPC
Daniel Drake76fb6572010-09-23 17:28:04 +0100460 /* If the VSA exists let it emulate PCI, if not emulate in kernel.
461 * XO-1 only. */
462 if (olpc_platform_info.boardrev < olpc_board_pre(0xd0) &&
463 !cs5535_has_vsa2())
Thomas Gleixnerd5d0e882010-02-22 05:42:04 -0800464 x86_init.pci.arch_init = pci_olpc_init;
465#endif
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700466
Daniel Drake447b1d42010-10-13 19:10:42 +0100467 if (olpc_platform_info.boardrev < olpc_board_pre(0xd0)) { /* XO-1 */
468 r = add_xo1_platform_devices();
469 if (r)
470 return r;
471 }
472
Daniel Drakea3c81212012-03-27 16:07:40 +0100473 setup_debugfs();
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100474
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700475 return 0;
476}
477
478postcore_initcall(olpc_init);