d540cb0eae524958fdab7c196865b2c84d08ba9b
[openwrt.git] / target / linux / olpc / files / arch / i386 / kernel / olpc.c
1 /* Support for the OLPC DCON and OLPC EC access
2 * Copyright (C) 2006, Advanced Micro Devices, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
10 #include <linux/autoconf.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/mc146818rtc.h>
14 #include <linux/delay.h>
15 #include <linux/spinlock.h>
16
17 #include <asm/olpc.h>
18 #include <asm/ofw.h>
19
20 /* This is our new multi-purpose structure used to contain the
21 * information about the platform that we detect
22 */
23
24 struct olpc_platform_t olpc_platform_info;
25 EXPORT_SYMBOL_GPL(olpc_platform_info);
26
27 /*********************************************************************
28 * EC locking and access
29 *********************************************************************/
30
31 static DEFINE_SPINLOCK(ec_lock);
32
33 /* what the timeout *should* be (in ms) */
34 #define EC_BASE_TIMEOUT 20
35
36 /* the timeout that bugs in the EC might force us to actually use */
37 static int ec_timeout = EC_BASE_TIMEOUT;
38
39 static int __init olpc_ec_timeout_set(char *str)
40 {
41 if (get_option(&str, &ec_timeout) != 1) {
42 ec_timeout = EC_BASE_TIMEOUT;
43 printk(KERN_ERR "olpc-ec: invalid argument to "
44 "'olpc_ec_timeout=', ignoring!\n");
45 }
46 printk(KERN_DEBUG "olpc-ec: using %d ms delay for EC commands.\n",
47 ec_timeout);
48 return 1;
49 }
50 __setup("olpc_ec_timeout=", olpc_ec_timeout_set);
51
52 /*
53 * These *bf_status functions return whether the buffers are full or not.
54 */
55
56 static inline unsigned int ibf_status(unsigned int port)
57 {
58 return inb(port) & 0x02;
59 }
60
61 static inline unsigned int obf_status(unsigned int port)
62 {
63 return inb(port) & 0x01;
64 }
65
66 #define wait_on_ibf(p, d) __wait_on_ibf(__LINE__, (p), (d))
67 static int __wait_on_ibf(unsigned int line, unsigned int port, int desired)
68 {
69 unsigned int timeo;
70 int state = ibf_status(port);
71
72 for (timeo = ec_timeout; state != desired && timeo; timeo--) {
73 mdelay(1);
74 state = ibf_status(port);
75 }
76
77 if ((state == desired) && (ec_timeout > EC_BASE_TIMEOUT) &&
78 timeo < (ec_timeout - EC_BASE_TIMEOUT)) {
79 printk(KERN_WARNING "olpc-ec: waited %u ms for IBF (%d)!\n",
80 EC_BASE_TIMEOUT-timeo, line);
81 }
82
83 return !(state == desired);
84 }
85
86 #define wait_on_obf(p, d) __wait_on_obf(__LINE__, (p), (d))
87 static int __wait_on_obf(unsigned int line, unsigned int port, int desired)
88 {
89 unsigned int timeo;
90 int state = obf_status(port);
91
92 for (timeo = ec_timeout; state != desired && timeo; timeo--) {
93 mdelay(1);
94 state = obf_status(port);
95 }
96
97 if ((state == desired) && (ec_timeout > EC_BASE_TIMEOUT) &&
98 timeo < (ec_timeout - EC_BASE_TIMEOUT)) {
99 printk(KERN_WARNING "olpc-ec: waited %u ms for OBF (%d)!\n",
100 EC_BASE_TIMEOUT-timeo, line);
101 }
102
103 return !(state == desired);
104 }
105
106 int olpc_ec_cmd(unsigned char cmd, unsigned char *inbuf, size_t inlen,
107 unsigned char *outbuf, size_t outlen)
108 {
109 unsigned long flags;
110 int ret = -EIO;
111 int i;
112
113 spin_lock_irqsave(&ec_lock, flags);
114
115 if (wait_on_ibf(0x6c, 0)) {
116 printk(KERN_ERR "olpc-ec: timeout waiting for EC to "
117 "quiesce!\n");
118 goto err;
119 }
120
121 restart:
122 /*
123 * Note that if we time out during any IBF checks, that's a failure;
124 * we have to return. There's no way for the kernel to clear that.
125 *
126 * If we time out during an OBF check, we can restart the command;
127 * reissuing it will clear the OBF flag, and we should be alright.
128 * The OBF flag will sometimes misbehave due to what we believe
129 * is a hardware quirk..
130 */
131 printk(KERN_DEBUG "olpc-ec: running cmd 0x%x\n", cmd);
132 outb(cmd, 0x6c);
133
134 if (wait_on_ibf(0x6c, 0)) {
135 printk(KERN_ERR "olpc-ec: timeout waiting for EC to read "
136 "command!\n");
137 goto err;
138 }
139
140 if (inbuf && inlen) {
141 /* write data to EC */
142 for (i = 0; i < inlen; i++) {
143 if (wait_on_ibf(0x6c, 0)) {
144 printk(KERN_ERR "olpc-ec: timeout waiting for"
145 " EC accept data!\n");
146 goto err;
147 }
148 printk(KERN_DEBUG "olpc-ec: sending cmd arg 0x%x\n",
149 inbuf[i]);
150 outb(inbuf[i], 0x68);
151 }
152 }
153 if (outbuf && outlen) {
154 /* read data from EC */
155 for (i = 0; i < outlen; i++) {
156 if (wait_on_obf(0x6c, 1)) {
157 printk(KERN_ERR "olpc-ec: timeout waiting for"
158 " EC to provide data!\n");
159 goto restart;
160 }
161 outbuf[i] = inb(0x68);
162 printk(KERN_DEBUG "olpc-ec: received 0x%x\n",
163 outbuf[i]);
164 }
165 }
166
167 ret = 0;
168 err:
169 spin_unlock_irqrestore(&ec_lock, flags);
170 return ret;
171 }
172 EXPORT_SYMBOL_GPL(olpc_ec_cmd);
173
174 /*********************************************************************
175 * DCON stuff
176 *********************************************************************/
177
178 static void olpc_power_off(void)
179 {
180 printk(KERN_INFO "OLPC power off sequence...\n");
181 outb(0xff, 0x381);
182 outb(0x14, 0x382);
183 outb(0x01, 0x383);
184 outb(0xff, 0x381);
185 outb(0x14, 0x382);
186 outb(0x00, 0x383);
187 }
188
189 static void __init
190 ec_detect(void)
191 {
192 olpc_ec_cmd(0x08, NULL, 0, (unsigned char *) &olpc_platform_info.ecver, 1);
193 }
194
195 /* Check to see if this version of the OLPC board has VSA built
196 * in, and set a flag
197 */
198
199 static void __init vsa_detect(void)
200 {
201 u16 rev;
202
203 outw(0xFC53, 0xAC1C);
204 outw(0x0003, 0xAC1C);
205
206 rev = inw(0xAC1E);
207
208 if (rev == 0x4132)
209 olpc_platform_info.flags |= OLPC_F_VSA;
210 }
211
212 /* Map OFW revisions to what OLPC_REV_* */
213 static const char __initdata *olpc_boardrev_str[] = {
214 "A1",
215 "preB1",
216 "B1",
217 "preB2",
218 "B2",
219 "preB3",
220 "B3",
221 "B4",
222 "C1",
223 "R1",
224 };
225
226 static void __init platform_detect(char *revision, size_t len)
227 {
228 size_t propsize;
229 int i;
230
231 BUG_ON(ARRAY_SIZE(olpc_boardrev_str) != OLPC_REV_UNKNOWN);
232
233 if (ofw("getprop", 4, 1, NULL, "model", revision, len, &propsize)) {
234 printk(KERN_ERR "ofw: getprop call failed!\n");
235 goto failed;
236 }
237 if (len < propsize) {
238 printk(KERN_ERR "ofw: revision string is too long!\n");
239 goto failed;
240 }
241
242 for (i=0; i < ARRAY_SIZE(olpc_boardrev_str); i++) {
243 if (strcmp(revision, olpc_boardrev_str[i]) == 0) {
244 olpc_platform_info.boardrev = i;
245 return;
246 }
247 }
248
249 failed:
250 strncpy(revision, "Unknown", len);
251 olpc_platform_info.boardrev = OLPC_REV_UNKNOWN;
252 }
253
254 static int olpc_dcon_present = -1;
255 module_param(olpc_dcon_present, int, 0444);
256
257 /* REV_A CMOS map:
258 * bit 440; DCON present bit
259 */
260
261 #define OLPC_CMOS_DCON_OFFSET (440 / 8)
262 #define OLPC_CMOS_DCON_MASK 0x01
263
264 static int __init olpc_init(void)
265 {
266 unsigned char *romsig;
267 char revision[10];
268
269 spin_lock_init(&ec_lock);
270
271 romsig = ioremap(0xffffffc0, 16);
272
273 if (!romsig)
274 return 0;
275
276 if (strncmp(romsig, "CL1 Q", 7))
277 goto unmap;
278 if (strncmp(romsig+6, romsig+13, 3)) {
279 printk(KERN_INFO "OLPC BIOS signature looks invalid. Assuming not OLPC\n");
280 goto unmap;
281 }
282 printk(KERN_INFO "OLPC board with OpenFirmware: %.16s\n", romsig);
283
284 olpc_platform_info.flags |= OLPC_F_PRESENT;
285
286 pm_power_off = olpc_power_off;
287
288 /* Get the platform revision */
289 platform_detect(revision, sizeof(revision));
290
291 /* If olpc_dcon_present isn't set by the command line, then
292 * "detect" it
293 */
294
295 if (olpc_dcon_present == -1) {
296 /* B1 and greater always has a DCON */
297 if (olpc_platform_info.boardrev >= OLPC_REV_B1 &&
298 olpc_platform_info.boardrev < OLPC_REV_UNKNOWN)
299 olpc_dcon_present = 1;
300 }
301
302 if (olpc_dcon_present)
303 olpc_platform_info.flags |= OLPC_F_DCON;
304
305 /* Get the EC revision */
306 ec_detect();
307
308 /* Check to see if the VSA exists */
309 vsa_detect();
310
311 printk(KERN_INFO "OLPC board revision: %s (EC=%x)\n", revision,
312 olpc_platform_info.ecver);
313
314 unmap:
315 iounmap(romsig);
316
317 return 0;
318 }
319
320 postcore_initcall(olpc_init);
This page took 0.055248 seconds and 3 git commands to generate.