1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Support for the OLPC DCON and OLPC EC access
5 * Copyright © 2006 Advanced Micro Devices, Inc.
6 * Copyright © 2007-2008 Andres Salomon <dilinger@debian.org>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/export.h>
12 #include <linux/delay.h>
14 #include <linux/string.h>
15 #include <linux/platform_device.h>
17 #include <linux/syscore_ops.h>
18 #include <linux/mutex.h>
19 #include <linux/olpc-ec.h>
21 #include <asm/geode.h>
22 #include <asm/setup.h>
24 #include <asm/olpc_ofw.h>
26 struct olpc_platform_t olpc_platform_info;
27 EXPORT_SYMBOL_GPL(olpc_platform_info);
29 /* EC event mask to be applied during suspend (defining wakeup sources). */
30 static u16 ec_wakeup_mask;
32 /* what the timeout *should* be (in ms) */
33 #define EC_BASE_TIMEOUT 20
35 /* the timeout that bugs in the EC might force us to actually use */
36 static int ec_timeout = EC_BASE_TIMEOUT;
38 static int __init olpc_ec_timeout_set(char *str)
40 if (get_option(&str, &ec_timeout) != 1) {
41 ec_timeout = EC_BASE_TIMEOUT;
42 printk(KERN_ERR "olpc-ec: invalid argument to "
43 "'olpc_ec_timeout=', ignoring!\n");
45 printk(KERN_DEBUG "olpc-ec: using %d ms delay for EC commands.\n",
49 __setup("olpc_ec_timeout=", olpc_ec_timeout_set);
52 * These {i,o}bf_status functions return whether the buffers are full or not.
55 static inline unsigned int ibf_status(unsigned int port)
57 return !!(inb(port) & 0x02);
60 static inline unsigned int obf_status(unsigned int port)
62 return inb(port) & 0x01;
65 #define wait_on_ibf(p, d) __wait_on_ibf(__LINE__, (p), (d))
66 static int __wait_on_ibf(unsigned int line, unsigned int port, int desired)
69 int state = ibf_status(port);
71 for (timeo = ec_timeout; state != desired && timeo; timeo--) {
73 state = ibf_status(port);
76 if ((state == desired) && (ec_timeout > EC_BASE_TIMEOUT) &&
77 timeo < (ec_timeout - EC_BASE_TIMEOUT)) {
78 printk(KERN_WARNING "olpc-ec: %d: waited %u ms for IBF!\n",
79 line, ec_timeout - timeo);
82 return !(state == desired);
85 #define wait_on_obf(p, d) __wait_on_obf(__LINE__, (p), (d))
86 static int __wait_on_obf(unsigned int line, unsigned int port, int desired)
89 int state = obf_status(port);
91 for (timeo = ec_timeout; state != desired && timeo; timeo--) {
93 state = obf_status(port);
96 if ((state == desired) && (ec_timeout > EC_BASE_TIMEOUT) &&
97 timeo < (ec_timeout - EC_BASE_TIMEOUT)) {
98 printk(KERN_WARNING "olpc-ec: %d: waited %u ms for OBF!\n",
99 line, ec_timeout - timeo);
102 return !(state == desired);
106 * This allows the kernel to run Embedded Controller commands. The EC is
107 * documented at <http://wiki.laptop.org/go/Embedded_controller>, and the
108 * available EC commands are here:
109 * <http://wiki.laptop.org/go/Ec_specification>. Unfortunately, while
110 * OpenFirmware's source is available, the EC's is not.
112 static int olpc_xo1_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf,
113 size_t outlen, void *arg)
120 for (i = 0; i < 10 && (obf_status(0x6c) == 1); i++)
123 printk(KERN_ERR "olpc-ec: timeout while attempting to "
124 "clear OBF flag!\n");
128 if (wait_on_ibf(0x6c, 0)) {
129 printk(KERN_ERR "olpc-ec: timeout waiting for EC to "
136 * Note that if we time out during any IBF checks, that's a failure;
137 * we have to return. There's no way for the kernel to clear that.
139 * If we time out during an OBF check, we can restart the command;
140 * reissuing it will clear the OBF flag, and we should be alright.
141 * The OBF flag will sometimes misbehave due to what we believe
142 * is a hardware quirk..
144 pr_devel("olpc-ec: running cmd 0x%x\n", cmd);
147 if (wait_on_ibf(0x6c, 0)) {
148 printk(KERN_ERR "olpc-ec: timeout waiting for EC to read "
153 if (inbuf && inlen) {
154 /* write data to EC */
155 for (i = 0; i < inlen; i++) {
156 pr_devel("olpc-ec: sending cmd arg 0x%x\n", inbuf[i]);
157 outb(inbuf[i], 0x68);
158 if (wait_on_ibf(0x6c, 0)) {
159 printk(KERN_ERR "olpc-ec: timeout waiting for"
160 " EC accept data!\n");
165 if (outbuf && outlen) {
166 /* read data from EC */
167 for (i = 0; i < outlen; i++) {
168 if (wait_on_obf(0x6c, 1)) {
169 printk(KERN_ERR "olpc-ec: timeout waiting for"
170 " EC to provide data!\n");
175 outbuf[i] = inb(0x68);
176 pr_devel("olpc-ec: received 0x%x\n", outbuf[i]);
185 void olpc_ec_wakeup_set(u16 value)
187 ec_wakeup_mask |= value;
189 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_set);
191 void olpc_ec_wakeup_clear(u16 value)
193 ec_wakeup_mask &= ~value;
195 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_clear);
198 * Returns true if the compile and runtime configurations allow for EC events
199 * to wake the system.
201 bool olpc_ec_wakeup_available(void)
203 if (!machine_is_olpc())
207 * XO-1 EC wakeups are available when olpc-xo1-sci driver is
210 #ifdef CONFIG_OLPC_XO1_SCI
211 if (olpc_platform_info.boardrev < olpc_board_pre(0xd0)) /* XO-1 */
216 * XO-1.5 EC wakeups are available when olpc-xo15-sci driver is
219 #ifdef CONFIG_OLPC_XO15_SCI
220 if (olpc_platform_info.boardrev >= olpc_board_pre(0xd0)) /* XO-1.5 */
226 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_available);
228 int olpc_ec_mask_write(u16 bits)
230 if (olpc_platform_info.flags & OLPC_F_EC_WIDE_SCI) {
231 __be16 ec_word = cpu_to_be16(bits);
232 return olpc_ec_cmd(EC_WRITE_EXT_SCI_MASK, (void *) &ec_word, 2,
235 unsigned char ec_byte = bits & 0xff;
236 return olpc_ec_cmd(EC_WRITE_SCI_MASK, &ec_byte, 1, NULL, 0);
239 EXPORT_SYMBOL_GPL(olpc_ec_mask_write);
241 int olpc_ec_sci_query(u16 *sci_value)
245 if (olpc_platform_info.flags & OLPC_F_EC_WIDE_SCI) {
247 ret = olpc_ec_cmd(EC_EXT_SCI_QUERY,
248 NULL, 0, (void *) &ec_word, 2);
250 *sci_value = be16_to_cpu(ec_word);
252 unsigned char ec_byte;
253 ret = olpc_ec_cmd(EC_SCI_QUERY, NULL, 0, &ec_byte, 1);
255 *sci_value = ec_byte;
260 EXPORT_SYMBOL_GPL(olpc_ec_sci_query);
262 static bool __init check_ofw_architecture(struct device_node *root)
264 const char *olpc_arch;
267 olpc_arch = of_get_property(root, "architecture", &propsize);
268 return propsize == 5 && strncmp("OLPC", olpc_arch, 5) == 0;
271 static u32 __init get_board_revision(struct device_node *root)
276 rev = of_get_property(root, "board-revision-int", &propsize);
280 return be32_to_cpu(*rev);
283 static bool __init platform_detect(void)
285 struct device_node *root = of_find_node_by_path("/");
291 success = check_ofw_architecture(root);
293 olpc_platform_info.boardrev = get_board_revision(root);
294 olpc_platform_info.flags |= OLPC_F_PRESENT;
301 static int __init add_xo1_platform_devices(void)
303 struct platform_device *pdev;
305 pdev = platform_device_register_simple("xo1-rfkill", -1, NULL, 0);
307 return PTR_ERR(pdev);
309 pdev = platform_device_register_simple("olpc-xo1", -1, NULL, 0);
311 return PTR_ERR_OR_ZERO(pdev);
314 static int olpc_xo1_ec_probe(struct platform_device *pdev)
316 /* get the EC revision */
317 olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0,
318 (unsigned char *) &olpc_platform_info.ecver, 1);
320 /* EC version 0x5f adds support for wide SCI mask */
321 if (olpc_platform_info.ecver >= 0x5f)
322 olpc_platform_info.flags |= OLPC_F_EC_WIDE_SCI;
324 pr_info("OLPC board revision %s%X (EC=%x)\n",
325 ((olpc_platform_info.boardrev & 0xf) < 8) ? "pre" : "",
326 olpc_platform_info.boardrev >> 4,
327 olpc_platform_info.ecver);
331 static int olpc_xo1_ec_suspend(struct platform_device *pdev)
333 olpc_ec_mask_write(ec_wakeup_mask);
336 * Squelch SCIs while suspended. This is a fix for
337 * <http://dev.laptop.org/ticket/1835>.
339 return olpc_ec_cmd(EC_SET_SCI_INHIBIT, NULL, 0, NULL, 0);
342 static int olpc_xo1_ec_resume(struct platform_device *pdev)
344 /* Tell the EC to stop inhibiting SCIs */
345 olpc_ec_cmd(EC_SET_SCI_INHIBIT_RELEASE, NULL, 0, NULL, 0);
348 * Tell the wireless module to restart USB communication.
349 * Must be done twice.
351 olpc_ec_cmd(EC_WAKE_UP_WLAN, NULL, 0, NULL, 0);
352 olpc_ec_cmd(EC_WAKE_UP_WLAN, NULL, 0, NULL, 0);
357 static struct olpc_ec_driver ec_xo1_driver = {
358 .probe = olpc_xo1_ec_probe,
359 .suspend = olpc_xo1_ec_suspend,
360 .resume = olpc_xo1_ec_resume,
361 .ec_cmd = olpc_xo1_ec_cmd,
364 static struct olpc_ec_driver ec_xo1_5_driver = {
365 .probe = olpc_xo1_ec_probe,
366 .ec_cmd = olpc_xo1_ec_cmd,
369 static int __init olpc_init(void)
373 if (!olpc_ofw_present() || !platform_detect())
376 /* register the XO-1 and 1.5-specific EC handler */
377 if (olpc_platform_info.boardrev < olpc_board_pre(0xd0)) /* XO-1 */
378 olpc_ec_driver_register(&ec_xo1_driver, NULL);
380 olpc_ec_driver_register(&ec_xo1_5_driver, NULL);
381 platform_device_register_simple("olpc-ec", -1, NULL, 0);
383 /* assume B1 and above models always have a DCON */
384 if (olpc_board_at_least(olpc_board(0xb1)))
385 olpc_platform_info.flags |= OLPC_F_DCON;
387 #ifdef CONFIG_PCI_OLPC
388 /* If the VSA exists let it emulate PCI, if not emulate in kernel.
390 if (olpc_platform_info.boardrev < olpc_board_pre(0xd0) &&
392 x86_init.pci.arch_init = pci_olpc_init;
395 if (olpc_platform_info.boardrev < olpc_board_pre(0xd0)) { /* XO-1 */
396 r = add_xo1_platform_devices();
404 postcore_initcall(olpc_init);