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 <asm/geode.h>
23 #ifdef CONFIG_OPEN_FIRMWARE
27 struct olpc_platform_t olpc_platform_info
;
28 EXPORT_SYMBOL_GPL(olpc_platform_info
);
30 static DEFINE_SPINLOCK(ec_lock
);
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 int olpc_ec_cmd(unsigned char cmd
, unsigned char *inbuf
, size_t inlen
,
113 unsigned char *outbuf
, size_t outlen
)
120 spin_lock_irqsave(&ec_lock
, flags
);
123 for (i
= 0; i
< 10 && (obf_status(0x6c) == 1); i
++)
126 printk(KERN_ERR
"olpc-ec: timeout while attempting to "
127 "clear OBF flag!\n");
131 if (wait_on_ibf(0x6c, 0)) {
132 printk(KERN_ERR
"olpc-ec: timeout waiting for EC to "
139 * Note that if we time out during any IBF checks, that's a failure;
140 * we have to return. There's no way for the kernel to clear that.
142 * If we time out during an OBF check, we can restart the command;
143 * reissuing it will clear the OBF flag, and we should be alright.
144 * The OBF flag will sometimes misbehave due to what we believe
145 * is a hardware quirk..
147 printk(KERN_DEBUG
"olpc-ec: running cmd 0x%x\n", cmd
);
150 if (wait_on_ibf(0x6c, 0)) {
151 printk(KERN_ERR
"olpc-ec: timeout waiting for EC to read "
156 if (inbuf
&& inlen
) {
157 /* write data to EC */
158 for (i
= 0; i
< inlen
; i
++) {
159 if (wait_on_ibf(0x6c, 0)) {
160 printk(KERN_ERR
"olpc-ec: timeout waiting for"
161 " EC accept data!\n");
164 printk(KERN_DEBUG
"olpc-ec: sending cmd arg 0x%x\n",
166 outb(inbuf
[i
], 0x68);
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");
179 outbuf
[i
] = inb(0x68);
180 printk(KERN_DEBUG
"olpc-ec: received 0x%x\n",
187 spin_unlock_irqrestore(&ec_lock
, flags
);
190 EXPORT_SYMBOL_GPL(olpc_ec_cmd
);
192 #ifdef CONFIG_OPEN_FIRMWARE
193 static void __init
platform_detect(void)
198 if (ofw("getprop", 4, 1, NULL
, "board-revision-int", &rev
, 4,
199 &propsize
) || propsize
!= 4) {
200 printk(KERN_ERR
"ofw: getprop call failed!\n");
201 rev
= cpu_to_be32(0);
203 olpc_platform_info
.boardrev
= be32_to_cpu(rev
);
206 static void __init
platform_detect(void)
208 /* stopgap until OFW support is added to the kernel */
209 olpc_platform_info
.boardrev
= olpc_board(0xc2);
213 static int __init
olpc_init(void)
215 unsigned char *romsig
;
217 /* The ioremap check is dangerous; limit what we run it on */
218 if (!is_geode() || cs5535_has_vsa2())
221 spin_lock_init(&ec_lock
);
223 romsig
= ioremap(0xffffffc0, 16);
227 if (strncmp(romsig
, "CL1 Q", 7))
229 if (strncmp(romsig
+6, romsig
+13, 3)) {
230 printk(KERN_INFO
"OLPC BIOS signature looks invalid. "
231 "Assuming not OLPC\n");
235 printk(KERN_INFO
"OLPC board with OpenFirmware %.16s\n", romsig
);
236 olpc_platform_info
.flags
|= OLPC_F_PRESENT
;
238 /* get the platform revision */
241 /* assume B1 and above models always have a DCON */
242 if (olpc_board_at_least(olpc_board(0xb1)))
243 olpc_platform_info
.flags
|= OLPC_F_DCON
;
245 /* get the EC revision */
246 olpc_ec_cmd(EC_FIRMWARE_REV
, NULL
, 0,
247 (unsigned char *) &olpc_platform_info
.ecver
, 1);
249 /* check to see if the VSA exists */
250 if (cs5535_has_vsa2())
251 olpc_platform_info
.flags
|= OLPC_F_VSA
;
253 printk(KERN_INFO
"OLPC board revision %s%X (EC=%x)\n",
254 ((olpc_platform_info
.boardrev
& 0xf) < 8) ? "pre" : "",
255 olpc_platform_info
.boardrev
>> 4,
256 olpc_platform_info
.ecver
);
263 postcore_initcall(olpc_init
);