2 * Support for the OLPC DCON and OLPC EC access
4 * Copyright © 2006 Advanced Micro Devices, Inc.
5 * Copyright © 2007-2008 Andres Salomon <dilinger@debian.org>
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.
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>
19 #include <linux/string.h>
20 #include <linux/platform_device.h>
22 #include <linux/syscore_ops.h>
23 #include <linux/debugfs.h>
24 #include <linux/mutex.h>
25 #include <linux/olpc-ec.h>
27 #include <asm/geode.h>
28 #include <asm/setup.h>
30 #include <asm/olpc_ofw.h>
32 struct olpc_platform_t olpc_platform_info;
33 EXPORT_SYMBOL_GPL(olpc_platform_info);
35 static DEFINE_SPINLOCK(ec_lock);
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)
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;
46 /* EC event mask to be applied during suspend (defining wakeup sources). */
47 static u16 ec_wakeup_mask;
49 /* what the timeout *should* be (in ms) */
50 #define EC_BASE_TIMEOUT 20
52 /* the timeout that bugs in the EC might force us to actually use */
53 static int ec_timeout = EC_BASE_TIMEOUT;
55 static int __init olpc_ec_timeout_set(char *str)
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");
62 printk(KERN_DEBUG "olpc-ec: using %d ms delay for EC commands.\n",
66 __setup("olpc_ec_timeout=", olpc_ec_timeout_set);
69 * These {i,o}bf_status functions return whether the buffers are full or not.
72 static inline unsigned int ibf_status(unsigned int port)
74 return !!(inb(port) & 0x02);
77 static inline unsigned int obf_status(unsigned int port)
79 return inb(port) & 0x01;
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)
86 int state = ibf_status(port);
88 for (timeo = ec_timeout; state != desired && timeo; timeo--) {
90 state = ibf_status(port);
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);
99 return !(state == desired);
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)
106 int state = obf_status(port);
108 for (timeo = ec_timeout; state != desired && timeo; timeo--) {
110 state = obf_status(port);
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);
119 return !(state == desired);
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.
129 int olpc_ec_cmd_x86(unsigned char cmd, unsigned char *inbuf, size_t inlen,
130 unsigned char *outbuf, size_t outlen)
137 spin_lock_irqsave(&ec_lock, flags);
140 for (i = 0; i < 10 && (obf_status(0x6c) == 1); i++)
143 printk(KERN_ERR "olpc-ec: timeout while attempting to "
144 "clear OBF flag!\n");
148 if (wait_on_ibf(0x6c, 0)) {
149 printk(KERN_ERR "olpc-ec: timeout waiting for EC to "
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.
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..
164 pr_devel("olpc-ec: running cmd 0x%x\n", cmd);
167 if (wait_on_ibf(0x6c, 0)) {
168 printk(KERN_ERR "olpc-ec: timeout waiting for EC to read "
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");
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");
195 outbuf[i] = inb(0x68);
196 pr_devel("olpc-ec: received 0x%x\n", outbuf[i]);
202 spin_unlock_irqrestore(&ec_lock, flags);
205 EXPORT_SYMBOL_GPL(olpc_ec_cmd_x86);
207 void olpc_ec_wakeup_set(u16 value)
209 ec_wakeup_mask |= value;
211 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_set);
213 void olpc_ec_wakeup_clear(u16 value)
215 ec_wakeup_mask &= ~value;
217 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_clear);
220 * Returns true if the compile and runtime configurations allow for EC events
221 * to wake the system.
223 bool olpc_ec_wakeup_available(void)
225 if (!machine_is_olpc())
229 * XO-1 EC wakeups are available when olpc-xo1-sci driver is
232 #ifdef CONFIG_OLPC_XO1_SCI
233 if (olpc_platform_info.boardrev < olpc_board_pre(0xd0)) /* XO-1 */
238 * XO-1.5 EC wakeups are available when olpc-xo15-sci driver is
241 #ifdef CONFIG_OLPC_XO15_SCI
242 if (olpc_platform_info.boardrev >= olpc_board_pre(0xd0)) /* XO-1.5 */
248 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_available);
250 int olpc_ec_mask_write(u16 bits)
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,
257 unsigned char ec_byte = bits & 0xff;
258 return olpc_ec_cmd(EC_WRITE_SCI_MASK, &ec_byte, 1, NULL, 0);
261 EXPORT_SYMBOL_GPL(olpc_ec_mask_write);
263 int olpc_ec_sci_query(u16 *sci_value)
267 if (olpc_platform_info.flags & OLPC_F_EC_WIDE_SCI) {
269 ret = olpc_ec_cmd(EC_EXT_SCI_QUERY,
270 NULL, 0, (void *) &ec_word, 2);
272 *sci_value = be16_to_cpu(ec_word);
274 unsigned char ec_byte;
275 ret = olpc_ec_cmd(EC_SCI_QUERY, NULL, 0, &ec_byte, 1);
277 *sci_value = ec_byte;
282 EXPORT_SYMBOL_GPL(olpc_ec_sci_query);
284 static ssize_t ec_debugfs_cmd_write(struct file *file, const char __user *buf,
285 size_t size, loff_t *ppos)
288 unsigned char ec_cmd[EC_MAX_CMD_ARGS];
289 unsigned int ec_cmd_int[EC_MAX_CMD_ARGS];
293 mutex_lock(&ec_debugfs_cmd_lock);
295 size = simple_write_to_buffer(cmdbuf, sizeof(cmdbuf), ppos, buf, size);
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;
305 printk(KERN_DEBUG "olpc-ec: bad ec cmd: "
306 "cmd:response-count [arg1 [arg2 ...]]\n");
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];
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);
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);
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);
331 mutex_unlock(&ec_debugfs_cmd_lock);
335 static ssize_t ec_debugfs_cmd_read(struct file *file, char __user *buf,
336 size_t size, loff_t *ppos)
342 mutex_lock(&ec_debugfs_cmd_lock);
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");
351 return simple_read_from_buffer(buf, size, ppos, respbuf, r);
354 static const struct file_operations ec_debugfs_genops = {
355 .write = ec_debugfs_cmd_write,
356 .read = ec_debugfs_cmd_read,
359 static void setup_debugfs(void)
361 ec_debugfs_dir = debugfs_create_dir("olpc-ec", 0);
362 if (ec_debugfs_dir == ERR_PTR(-ENODEV))
365 debugfs_create_file("cmd", 0600, ec_debugfs_dir, NULL,
369 static int olpc_ec_suspend(void)
371 return olpc_ec_mask_write(ec_wakeup_mask);
374 static bool __init check_ofw_architecture(struct device_node *root)
376 const char *olpc_arch;
379 olpc_arch = of_get_property(root, "architecture", &propsize);
380 return propsize == 5 && strncmp("OLPC", olpc_arch, 5) == 0;
383 static u32 __init get_board_revision(struct device_node *root)
388 rev = of_get_property(root, "board-revision-int", &propsize);
392 return be32_to_cpu(*rev);
395 static bool __init platform_detect(void)
397 struct device_node *root = of_find_node_by_path("/");
403 success = check_ofw_architecture(root);
405 olpc_platform_info.boardrev = get_board_revision(root);
406 olpc_platform_info.flags |= OLPC_F_PRESENT;
413 static int __init add_xo1_platform_devices(void)
415 struct platform_device *pdev;
417 pdev = platform_device_register_simple("xo1-rfkill", -1, NULL, 0);
419 return PTR_ERR(pdev);
421 pdev = platform_device_register_simple("olpc-xo1", -1, NULL, 0);
423 return PTR_ERR(pdev);
428 static struct syscore_ops olpc_syscore_ops = {
429 .suspend = olpc_ec_suspend,
432 static int __init olpc_init(void)
436 if (!olpc_ofw_present() || !platform_detect())
439 spin_lock_init(&ec_lock);
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;
445 /* get the EC revision */
446 olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0,
447 (unsigned char *) &olpc_platform_info.ecver, 1);
449 #ifdef CONFIG_PCI_OLPC
450 /* If the VSA exists let it emulate PCI, if not emulate in kernel.
452 if (olpc_platform_info.boardrev < olpc_board_pre(0xd0) &&
454 x86_init.pci.arch_init = pci_olpc_init;
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;
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);
465 if (olpc_platform_info.boardrev < olpc_board_pre(0xd0)) { /* XO-1 */
466 r = add_xo1_platform_devices();
471 register_syscore_ops(&olpc_syscore_ops);
477 postcore_initcall(olpc_init);