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>
24 #include <asm/geode.h>
25 #include <asm/setup.h>
27 #include <asm/olpc_ofw.h>
29 struct olpc_platform_t olpc_platform_info
;
30 EXPORT_SYMBOL_GPL(olpc_platform_info
);
32 static DEFINE_SPINLOCK(ec_lock
);
34 /* EC event mask to be applied during suspend (defining wakeup sources). */
35 static u16 ec_wakeup_mask
;
37 /* what the timeout *should* be (in ms) */
38 #define EC_BASE_TIMEOUT 20
40 /* the timeout that bugs in the EC might force us to actually use */
41 static int ec_timeout
= EC_BASE_TIMEOUT
;
43 static int __init
olpc_ec_timeout_set(char *str
)
45 if (get_option(&str
, &ec_timeout
) != 1) {
46 ec_timeout
= EC_BASE_TIMEOUT
;
47 printk(KERN_ERR
"olpc-ec: invalid argument to "
48 "'olpc_ec_timeout=', ignoring!\n");
50 printk(KERN_DEBUG
"olpc-ec: using %d ms delay for EC commands.\n",
54 __setup("olpc_ec_timeout=", olpc_ec_timeout_set
);
57 * These {i,o}bf_status functions return whether the buffers are full or not.
60 static inline unsigned int ibf_status(unsigned int port
)
62 return !!(inb(port
) & 0x02);
65 static inline unsigned int obf_status(unsigned int port
)
67 return inb(port
) & 0x01;
70 #define wait_on_ibf(p, d) __wait_on_ibf(__LINE__, (p), (d))
71 static int __wait_on_ibf(unsigned int line
, unsigned int port
, int desired
)
74 int state
= ibf_status(port
);
76 for (timeo
= ec_timeout
; state
!= desired
&& timeo
; timeo
--) {
78 state
= ibf_status(port
);
81 if ((state
== desired
) && (ec_timeout
> EC_BASE_TIMEOUT
) &&
82 timeo
< (ec_timeout
- EC_BASE_TIMEOUT
)) {
83 printk(KERN_WARNING
"olpc-ec: %d: waited %u ms for IBF!\n",
84 line
, ec_timeout
- timeo
);
87 return !(state
== desired
);
90 #define wait_on_obf(p, d) __wait_on_obf(__LINE__, (p), (d))
91 static int __wait_on_obf(unsigned int line
, unsigned int port
, int desired
)
94 int state
= obf_status(port
);
96 for (timeo
= ec_timeout
; state
!= desired
&& timeo
; timeo
--) {
98 state
= obf_status(port
);
101 if ((state
== desired
) && (ec_timeout
> EC_BASE_TIMEOUT
) &&
102 timeo
< (ec_timeout
- EC_BASE_TIMEOUT
)) {
103 printk(KERN_WARNING
"olpc-ec: %d: waited %u ms for OBF!\n",
104 line
, ec_timeout
- timeo
);
107 return !(state
== desired
);
111 * This allows the kernel to run Embedded Controller commands. The EC is
112 * documented at <http://wiki.laptop.org/go/Embedded_controller>, and the
113 * available EC commands are here:
114 * <http://wiki.laptop.org/go/Ec_specification>. Unfortunately, while
115 * OpenFirmware's source is available, the EC's is not.
117 int olpc_ec_cmd(unsigned char cmd
, unsigned char *inbuf
, size_t inlen
,
118 unsigned char *outbuf
, size_t outlen
)
125 spin_lock_irqsave(&ec_lock
, flags
);
128 for (i
= 0; i
< 10 && (obf_status(0x6c) == 1); i
++)
131 printk(KERN_ERR
"olpc-ec: timeout while attempting to "
132 "clear OBF flag!\n");
136 if (wait_on_ibf(0x6c, 0)) {
137 printk(KERN_ERR
"olpc-ec: timeout waiting for EC to "
144 * Note that if we time out during any IBF checks, that's a failure;
145 * we have to return. There's no way for the kernel to clear that.
147 * If we time out during an OBF check, we can restart the command;
148 * reissuing it will clear the OBF flag, and we should be alright.
149 * The OBF flag will sometimes misbehave due to what we believe
150 * is a hardware quirk..
152 pr_devel("olpc-ec: running cmd 0x%x\n", cmd
);
155 if (wait_on_ibf(0x6c, 0)) {
156 printk(KERN_ERR
"olpc-ec: timeout waiting for EC to read "
161 if (inbuf
&& inlen
) {
162 /* write data to EC */
163 for (i
= 0; i
< inlen
; i
++) {
164 pr_devel("olpc-ec: sending cmd arg 0x%x\n", inbuf
[i
]);
165 outb(inbuf
[i
], 0x68);
166 if (wait_on_ibf(0x6c, 0)) {
167 printk(KERN_ERR
"olpc-ec: timeout waiting for"
168 " EC accept data!\n");
173 if (outbuf
&& outlen
) {
174 /* read data from EC */
175 for (i
= 0; i
< outlen
; i
++) {
176 if (wait_on_obf(0x6c, 1)) {
177 printk(KERN_ERR
"olpc-ec: timeout waiting for"
178 " EC to provide data!\n");
183 outbuf
[i
] = inb(0x68);
184 pr_devel("olpc-ec: received 0x%x\n", outbuf
[i
]);
190 spin_unlock_irqrestore(&ec_lock
, flags
);
193 EXPORT_SYMBOL_GPL(olpc_ec_cmd
);
195 void olpc_ec_wakeup_set(u16 value
)
197 ec_wakeup_mask
|= value
;
199 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_set
);
201 void olpc_ec_wakeup_clear(u16 value
)
203 ec_wakeup_mask
&= ~value
;
205 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_clear
);
208 * Returns true if the compile and runtime configurations allow for EC events
209 * to wake the system.
211 bool olpc_ec_wakeup_available(void)
213 if (!machine_is_olpc())
217 * XO-1 EC wakeups are available when olpc-xo1-sci driver is
220 #ifdef CONFIG_OLPC_XO1_SCI
221 if (olpc_platform_info
.boardrev
< olpc_board_pre(0xd0)) /* XO-1 */
226 * XO-1.5 EC wakeups are available when olpc-xo15-sci driver is
229 #ifdef CONFIG_OLPC_XO15_SCI
230 if (olpc_platform_info
.boardrev
>= olpc_board_pre(0xd0)) /* XO-1.5 */
236 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_available
);
238 int olpc_ec_mask_write(u16 bits
)
240 if (olpc_platform_info
.flags
& OLPC_F_EC_WIDE_SCI
) {
241 __be16 ec_word
= cpu_to_be16(bits
);
242 return olpc_ec_cmd(EC_WRITE_EXT_SCI_MASK
, (void *) &ec_word
, 2,
245 unsigned char ec_byte
= bits
& 0xff;
246 return olpc_ec_cmd(EC_WRITE_SCI_MASK
, &ec_byte
, 1, NULL
, 0);
249 EXPORT_SYMBOL_GPL(olpc_ec_mask_write
);
251 int olpc_ec_sci_query(u16
*sci_value
)
255 if (olpc_platform_info
.flags
& OLPC_F_EC_WIDE_SCI
) {
257 ret
= olpc_ec_cmd(EC_EXT_SCI_QUERY
,
258 NULL
, 0, (void *) &ec_word
, 2);
260 *sci_value
= be16_to_cpu(ec_word
);
262 unsigned char ec_byte
;
263 ret
= olpc_ec_cmd(EC_SCI_QUERY
, NULL
, 0, &ec_byte
, 1);
265 *sci_value
= ec_byte
;
270 EXPORT_SYMBOL_GPL(olpc_ec_sci_query
);
272 static int olpc_ec_suspend(void)
274 return olpc_ec_mask_write(ec_wakeup_mask
);
277 static bool __init
check_ofw_architecture(struct device_node
*root
)
279 const char *olpc_arch
;
282 olpc_arch
= of_get_property(root
, "architecture", &propsize
);
283 return propsize
== 5 && strncmp("OLPC", olpc_arch
, 5) == 0;
286 static u32 __init
get_board_revision(struct device_node
*root
)
291 rev
= of_get_property(root
, "board-revision-int", &propsize
);
295 return be32_to_cpu(*rev
);
298 static bool __init
platform_detect(void)
300 struct device_node
*root
= of_find_node_by_path("/");
306 success
= check_ofw_architecture(root
);
308 olpc_platform_info
.boardrev
= get_board_revision(root
);
309 olpc_platform_info
.flags
|= OLPC_F_PRESENT
;
316 static int __init
add_xo1_platform_devices(void)
318 struct platform_device
*pdev
;
320 pdev
= platform_device_register_simple("xo1-rfkill", -1, NULL
, 0);
322 return PTR_ERR(pdev
);
324 pdev
= platform_device_register_simple("olpc-xo1", -1, NULL
, 0);
326 return PTR_ERR(pdev
);
331 static struct syscore_ops olpc_syscore_ops
= {
332 .suspend
= olpc_ec_suspend
,
335 static int __init
olpc_init(void)
339 if (!olpc_ofw_present() || !platform_detect())
342 spin_lock_init(&ec_lock
);
344 /* assume B1 and above models always have a DCON */
345 if (olpc_board_at_least(olpc_board(0xb1)))
346 olpc_platform_info
.flags
|= OLPC_F_DCON
;
348 /* get the EC revision */
349 olpc_ec_cmd(EC_FIRMWARE_REV
, NULL
, 0,
350 (unsigned char *) &olpc_platform_info
.ecver
, 1);
352 #ifdef CONFIG_PCI_OLPC
353 /* If the VSA exists let it emulate PCI, if not emulate in kernel.
355 if (olpc_platform_info
.boardrev
< olpc_board_pre(0xd0) &&
357 x86_init
.pci
.arch_init
= pci_olpc_init
;
359 /* EC version 0x5f adds support for wide SCI mask */
360 if (olpc_platform_info
.ecver
>= 0x5f)
361 olpc_platform_info
.flags
|= OLPC_F_EC_WIDE_SCI
;
363 printk(KERN_INFO
"OLPC board revision %s%X (EC=%x)\n",
364 ((olpc_platform_info
.boardrev
& 0xf) < 8) ? "pre" : "",
365 olpc_platform_info
.boardrev
>> 4,
366 olpc_platform_info
.ecver
);
368 if (olpc_platform_info
.boardrev
< olpc_board_pre(0xd0)) { /* XO-1 */
369 r
= add_xo1_platform_devices();
374 register_syscore_ops(&olpc_syscore_ops
);
379 postcore_initcall(olpc_init
);