blob: ed41b437b37b83f5098742cee27afabf6d1234c5 [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/mutex.h>
Andres Salomon3bf94282012-07-11 01:16:29 -070023#include <linux/olpc-ec.h>
Thomas Gleixnerd5d0e882010-02-22 05:42:04 -080024
Andres Salomon3ef0e1f2008-04-29 00:59:53 -070025#include <asm/geode.h>
Thomas Gleixnerd5d0e882010-02-22 05:42:04 -080026#include <asm/setup.h>
Andres Salomon3ef0e1f2008-04-29 00:59:53 -070027#include <asm/olpc.h>
Andres Salomonfd699c72010-06-18 17:46:53 -040028#include <asm/olpc_ofw.h>
Andres Salomon3ef0e1f2008-04-29 00:59:53 -070029
30struct olpc_platform_t olpc_platform_info;
31EXPORT_SYMBOL_GPL(olpc_platform_info);
32
Daniel Drakebc4ecd52011-06-25 17:34:13 +010033/* EC event mask to be applied during suspend (defining wakeup sources). */
34static u16 ec_wakeup_mask;
35
Andres Salomon3ef0e1f2008-04-29 00:59:53 -070036/* what the timeout *should* be (in ms) */
37#define EC_BASE_TIMEOUT 20
38
39/* the timeout that bugs in the EC might force us to actually use */
40static int ec_timeout = EC_BASE_TIMEOUT;
41
42static int __init olpc_ec_timeout_set(char *str)
43{
44 if (get_option(&str, &ec_timeout) != 1) {
45 ec_timeout = EC_BASE_TIMEOUT;
46 printk(KERN_ERR "olpc-ec: invalid argument to "
47 "'olpc_ec_timeout=', ignoring!\n");
48 }
49 printk(KERN_DEBUG "olpc-ec: using %d ms delay for EC commands.\n",
50 ec_timeout);
51 return 1;
52}
53__setup("olpc_ec_timeout=", olpc_ec_timeout_set);
54
55/*
56 * These {i,o}bf_status functions return whether the buffers are full or not.
57 */
58
59static inline unsigned int ibf_status(unsigned int port)
60{
61 return !!(inb(port) & 0x02);
62}
63
64static inline unsigned int obf_status(unsigned int port)
65{
66 return inb(port) & 0x01;
67}
68
69#define wait_on_ibf(p, d) __wait_on_ibf(__LINE__, (p), (d))
70static int __wait_on_ibf(unsigned int line, unsigned int port, int desired)
71{
72 unsigned int timeo;
73 int state = ibf_status(port);
74
75 for (timeo = ec_timeout; state != desired && timeo; timeo--) {
76 mdelay(1);
77 state = ibf_status(port);
78 }
79
80 if ((state == desired) && (ec_timeout > EC_BASE_TIMEOUT) &&
81 timeo < (ec_timeout - EC_BASE_TIMEOUT)) {
82 printk(KERN_WARNING "olpc-ec: %d: waited %u ms for IBF!\n",
83 line, ec_timeout - timeo);
84 }
85
86 return !(state == desired);
87}
88
89#define wait_on_obf(p, d) __wait_on_obf(__LINE__, (p), (d))
90static int __wait_on_obf(unsigned int line, unsigned int port, int desired)
91{
92 unsigned int timeo;
93 int state = obf_status(port);
94
95 for (timeo = ec_timeout; state != desired && timeo; timeo--) {
96 mdelay(1);
97 state = obf_status(port);
98 }
99
100 if ((state == desired) && (ec_timeout > EC_BASE_TIMEOUT) &&
101 timeo < (ec_timeout - EC_BASE_TIMEOUT)) {
102 printk(KERN_WARNING "olpc-ec: %d: waited %u ms for OBF!\n",
103 line, ec_timeout - timeo);
104 }
105
106 return !(state == desired);
107}
108
109/*
110 * This allows the kernel to run Embedded Controller commands. The EC is
111 * documented at <http://wiki.laptop.org/go/Embedded_controller>, and the
112 * available EC commands are here:
113 * <http://wiki.laptop.org/go/Ec_specification>. Unfortunately, while
114 * OpenFirmware's source is available, the EC's is not.
115 */
Andres Salomon85f90cf2012-07-12 17:57:28 -0700116static int olpc_xo1_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf,
117 size_t outlen, void *arg)
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700118{
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700119 int ret = -EIO;
120 int i;
Paul Fox286e5b92010-10-01 18:17:19 +0100121 int restarts = 0;
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700122
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700123 /* Clear OBF */
124 for (i = 0; i < 10 && (obf_status(0x6c) == 1); i++)
125 inb(0x68);
126 if (i == 10) {
127 printk(KERN_ERR "olpc-ec: timeout while attempting to "
128 "clear OBF flag!\n");
129 goto err;
130 }
131
132 if (wait_on_ibf(0x6c, 0)) {
133 printk(KERN_ERR "olpc-ec: timeout waiting for EC to "
134 "quiesce!\n");
135 goto err;
136 }
137
138restart:
139 /*
140 * Note that if we time out during any IBF checks, that's a failure;
141 * we have to return. There's no way for the kernel to clear that.
142 *
143 * If we time out during an OBF check, we can restart the command;
144 * reissuing it will clear the OBF flag, and we should be alright.
145 * The OBF flag will sometimes misbehave due to what we believe
146 * is a hardware quirk..
147 */
Andres Salomon25971862010-06-16 23:19:28 -0400148 pr_devel("olpc-ec: running cmd 0x%x\n", cmd);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700149 outb(cmd, 0x6c);
150
151 if (wait_on_ibf(0x6c, 0)) {
152 printk(KERN_ERR "olpc-ec: timeout waiting for EC to read "
153 "command!\n");
154 goto err;
155 }
156
157 if (inbuf && inlen) {
158 /* write data to EC */
159 for (i = 0; i < inlen; i++) {
Paul Foxa3ea14d2011-07-26 16:42:26 +0100160 pr_devel("olpc-ec: sending cmd arg 0x%x\n", inbuf[i]);
161 outb(inbuf[i], 0x68);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700162 if (wait_on_ibf(0x6c, 0)) {
163 printk(KERN_ERR "olpc-ec: timeout waiting for"
164 " EC accept data!\n");
165 goto err;
166 }
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700167 }
168 }
169 if (outbuf && outlen) {
170 /* read data from EC */
171 for (i = 0; i < outlen; i++) {
172 if (wait_on_obf(0x6c, 1)) {
173 printk(KERN_ERR "olpc-ec: timeout waiting for"
174 " EC to provide data!\n");
Paul Fox286e5b92010-10-01 18:17:19 +0100175 if (restarts++ < 10)
176 goto restart;
177 goto err;
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700178 }
179 outbuf[i] = inb(0x68);
Andres Salomon25971862010-06-16 23:19:28 -0400180 pr_devel("olpc-ec: received 0x%x\n", outbuf[i]);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700181 }
182 }
183
184 ret = 0;
185err:
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700186 return ret;
187}
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700188
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100189void olpc_ec_wakeup_set(u16 value)
190{
191 ec_wakeup_mask |= value;
192}
193EXPORT_SYMBOL_GPL(olpc_ec_wakeup_set);
194
195void olpc_ec_wakeup_clear(u16 value)
196{
197 ec_wakeup_mask &= ~value;
198}
199EXPORT_SYMBOL_GPL(olpc_ec_wakeup_clear);
200
201/*
202 * Returns true if the compile and runtime configurations allow for EC events
203 * to wake the system.
204 */
205bool olpc_ec_wakeup_available(void)
206{
207 if (!machine_is_olpc())
208 return false;
209
210 /*
211 * XO-1 EC wakeups are available when olpc-xo1-sci driver is
212 * compiled in
213 */
214#ifdef CONFIG_OLPC_XO1_SCI
215 if (olpc_platform_info.boardrev < olpc_board_pre(0xd0)) /* XO-1 */
216 return true;
217#endif
218
Daniel Drakea0f30f52011-06-25 17:34:18 +0100219 /*
220 * XO-1.5 EC wakeups are available when olpc-xo15-sci driver is
221 * compiled in
222 */
223#ifdef CONFIG_OLPC_XO15_SCI
224 if (olpc_platform_info.boardrev >= olpc_board_pre(0xd0)) /* XO-1.5 */
225 return true;
226#endif
227
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100228 return false;
229}
230EXPORT_SYMBOL_GPL(olpc_ec_wakeup_available);
231
232int olpc_ec_mask_write(u16 bits)
233{
234 if (olpc_platform_info.flags & OLPC_F_EC_WIDE_SCI) {
235 __be16 ec_word = cpu_to_be16(bits);
236 return olpc_ec_cmd(EC_WRITE_EXT_SCI_MASK, (void *) &ec_word, 2,
237 NULL, 0);
238 } else {
239 unsigned char ec_byte = bits & 0xff;
240 return olpc_ec_cmd(EC_WRITE_SCI_MASK, &ec_byte, 1, NULL, 0);
241 }
242}
243EXPORT_SYMBOL_GPL(olpc_ec_mask_write);
244
245int olpc_ec_sci_query(u16 *sci_value)
246{
247 int ret;
248
249 if (olpc_platform_info.flags & OLPC_F_EC_WIDE_SCI) {
250 __be16 ec_word;
251 ret = olpc_ec_cmd(EC_EXT_SCI_QUERY,
252 NULL, 0, (void *) &ec_word, 2);
253 if (ret == 0)
254 *sci_value = be16_to_cpu(ec_word);
255 } else {
256 unsigned char ec_byte;
257 ret = olpc_ec_cmd(EC_SCI_QUERY, NULL, 0, &ec_byte, 1);
258 if (ret == 0)
259 *sci_value = ec_byte;
260 }
261
262 return ret;
263}
264EXPORT_SYMBOL_GPL(olpc_ec_sci_query);
265
Andres Salomon85f90cf2012-07-12 17:57:28 -0700266static int olpc_ec_suspend(struct platform_device *pdev)
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100267{
268 return olpc_ec_mask_write(ec_wakeup_mask);
269}
270
Daniel Drake45bb1672011-03-13 15:10:17 +0000271static bool __init check_ofw_architecture(struct device_node *root)
Daniel Drake3e3c4862010-09-23 17:28:46 +0100272{
Daniel Drake45bb1672011-03-13 15:10:17 +0000273 const char *olpc_arch;
274 int propsize;
Daniel Drake3e3c4862010-09-23 17:28:46 +0100275
Daniel Drake45bb1672011-03-13 15:10:17 +0000276 olpc_arch = of_get_property(root, "architecture", &propsize);
Daniel Drake3e3c4862010-09-23 17:28:46 +0100277 return propsize == 5 && strncmp("OLPC", olpc_arch, 5) == 0;
278}
279
Daniel Drake45bb1672011-03-13 15:10:17 +0000280static u32 __init get_board_revision(struct device_node *root)
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700281{
Daniel Drake45bb1672011-03-13 15:10:17 +0000282 int propsize;
283 const __be32 *rev;
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700284
Daniel Drake45bb1672011-03-13 15:10:17 +0000285 rev = of_get_property(root, "board-revision-int", &propsize);
286 if (propsize != 4)
287 return 0;
288
289 return be32_to_cpu(*rev);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700290}
Daniel Drake3e3c4862010-09-23 17:28:46 +0100291
292static bool __init platform_detect(void)
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700293{
Daniel Drake45bb1672011-03-13 15:10:17 +0000294 struct device_node *root = of_find_node_by_path("/");
295 bool success;
296
297 if (!root)
Daniel Drake3e3c4862010-09-23 17:28:46 +0100298 return false;
Daniel Drake45bb1672011-03-13 15:10:17 +0000299
300 success = check_ofw_architecture(root);
301 if (success) {
302 olpc_platform_info.boardrev = get_board_revision(root);
303 olpc_platform_info.flags |= OLPC_F_PRESENT;
304 }
305
306 of_node_put(root);
307 return success;
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700308}
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700309
Daniel Drake447b1d42010-10-13 19:10:42 +0100310static int __init add_xo1_platform_devices(void)
311{
312 struct platform_device *pdev;
313
314 pdev = platform_device_register_simple("xo1-rfkill", -1, NULL, 0);
315 if (IS_ERR(pdev))
316 return PTR_ERR(pdev);
317
318 pdev = platform_device_register_simple("olpc-xo1", -1, NULL, 0);
319 if (IS_ERR(pdev))
320 return PTR_ERR(pdev);
321
322 return 0;
323}
324
Andres Salomon85f90cf2012-07-12 17:57:28 -0700325static int olpc_xo1_ec_probe(struct platform_device *pdev)
326{
327 /* get the EC revision */
328 olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0,
329 (unsigned char *) &olpc_platform_info.ecver, 1);
330
331 /* EC version 0x5f adds support for wide SCI mask */
332 if (olpc_platform_info.ecver >= 0x5f)
333 olpc_platform_info.flags |= OLPC_F_EC_WIDE_SCI;
334
335 pr_info("OLPC board revision %s%X (EC=%x)\n",
336 ((olpc_platform_info.boardrev & 0xf) < 8) ? "pre" : "",
337 olpc_platform_info.boardrev >> 4,
338 olpc_platform_info.ecver);
339
340 return 0;
341}
342
343static struct olpc_ec_driver ec_xo1_driver = {
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100344 .suspend = olpc_ec_suspend,
Andres Salomon85f90cf2012-07-12 17:57:28 -0700345 .probe = olpc_xo1_ec_probe,
346 .ec_cmd = olpc_xo1_ec_cmd,
Daniel Drakebc4ecd52011-06-25 17:34:13 +0100347};
348
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700349static int __init olpc_init(void)
350{
Daniel Drake447b1d42010-10-13 19:10:42 +0100351 int r = 0;
352
Daniel Drake3e3c4862010-09-23 17:28:46 +0100353 if (!olpc_ofw_present() || !platform_detect())
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700354 return 0;
355
Andres Salomon85f90cf2012-07-12 17:57:28 -0700356 /* register the XO-1 and 1.5-specific EC handler */
357 olpc_ec_driver_register(&ec_xo1_driver, NULL);
358 platform_device_register_simple("olpc-ec", -1, NULL, 0);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700359
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700360 /* assume B1 and above models always have a DCON */
361 if (olpc_board_at_least(olpc_board(0xb1)))
362 olpc_platform_info.flags |= OLPC_F_DCON;
363
Thomas Gleixnerd5d0e882010-02-22 05:42:04 -0800364#ifdef CONFIG_PCI_OLPC
Daniel Drake76fb6572010-09-23 17:28:04 +0100365 /* If the VSA exists let it emulate PCI, if not emulate in kernel.
366 * XO-1 only. */
367 if (olpc_platform_info.boardrev < olpc_board_pre(0xd0) &&
368 !cs5535_has_vsa2())
Thomas Gleixnerd5d0e882010-02-22 05:42:04 -0800369 x86_init.pci.arch_init = pci_olpc_init;
370#endif
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700371
Daniel Drake447b1d42010-10-13 19:10:42 +0100372 if (olpc_platform_info.boardrev < olpc_board_pre(0xd0)) { /* XO-1 */
373 r = add_xo1_platform_devices();
374 if (r)
375 return r;
376 }
377
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700378 return 0;
379}
380
381postcore_initcall(olpc_init);