1 /* Support for the OLPC DCON and OLPC EC access
2 * Copyright (C) 2006, Advanced Micro Devices, Inc.
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.
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>
20 /* This is our new multi-purpose structure used to contain the
21 * information about the platform that we detect
24 struct olpc_platform_t olpc_platform_info;
25 EXPORT_SYMBOL_GPL(olpc_platform_info);
27 /*********************************************************************
28 * EC locking and access
29 *********************************************************************/
31 static DEFINE_SPINLOCK(ec_lock);
33 /* what the timeout *should* be (in ms) */
34 #define EC_BASE_TIMEOUT 20
36 /* the timeout that bugs in the EC might force us to actually use */
37 static int ec_timeout = EC_BASE_TIMEOUT;
39 static int __init olpc_ec_timeout_set(char *str)
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");
46 printk(KERN_DEBUG "olpc-ec: using %d ms delay for EC commands.\n",
50 __setup("olpc_ec_timeout=", olpc_ec_timeout_set);
53 * These *bf_status functions return whether the buffers are full or not.
56 static inline unsigned int ibf_status(unsigned int port)
58 return inb(port) & 0x02;
61 static inline unsigned int obf_status(unsigned int port)
63 return inb(port) & 0x01;
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)
70 int state = ibf_status(port);
72 for (timeo = ec_timeout; state != desired && timeo; timeo--) {
74 state = ibf_status(port);
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);
83 return !(state == desired);
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)
90 int state = obf_status(port);
92 for (timeo = ec_timeout; state != desired && timeo; timeo--) {
94 state = obf_status(port);
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);
103 return !(state == desired);
106 int olpc_ec_cmd(unsigned char cmd, unsigned char *inbuf, size_t inlen,
107 unsigned char *outbuf, size_t outlen)
113 spin_lock_irqsave(&ec_lock, flags);
115 if (wait_on_ibf(0x6c, 0)) {
116 printk(KERN_ERR "olpc-ec: timeout waiting for EC to "
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.
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..
131 printk(KERN_DEBUG "olpc-ec: running cmd 0x%x\n", cmd);
134 if (wait_on_ibf(0x6c, 0)) {
135 printk(KERN_ERR "olpc-ec: timeout waiting for EC to read "
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");
148 printk(KERN_DEBUG "olpc-ec: sending cmd arg 0x%x\n",
150 outb(inbuf[i], 0x68);
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");
161 outbuf[i] = inb(0x68);
162 printk(KERN_DEBUG "olpc-ec: received 0x%x\n",
169 spin_unlock_irqrestore(&ec_lock, flags);
172 EXPORT_SYMBOL_GPL(olpc_ec_cmd);
174 /*********************************************************************
176 *********************************************************************/
178 static void olpc_power_off(void)
180 printk(KERN_INFO "OLPC power off sequence...\n");
192 olpc_ec_cmd(0x08, NULL, 0, (unsigned char *) &olpc_platform_info.ecver, 1);
195 /* Check to see if this version of the OLPC board has VSA built
199 static void __init vsa_detect(void)
203 outw(0xFC53, 0xAC1C);
204 outw(0x0003, 0xAC1C);
209 olpc_platform_info.flags |= OLPC_F_VSA;
212 /* Map OFW revisions to what OLPC_REV_* */
213 static const char __initdata *olpc_boardrev_str[] = {
226 static void __init platform_detect(char *revision, size_t len)
231 BUG_ON(ARRAY_SIZE(olpc_boardrev_str) != OLPC_REV_UNKNOWN);
233 if (ofw("getprop", 4, 1, NULL, "model", revision, len, &propsize)) {
234 printk(KERN_ERR "ofw: getprop call failed!\n");
237 if (len < propsize) {
238 printk(KERN_ERR "ofw: revision string is too long!\n");
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;
250 strncpy(revision, "Unknown", len);
251 olpc_platform_info.boardrev = OLPC_REV_UNKNOWN;
254 static int olpc_dcon_present = -1;
255 module_param(olpc_dcon_present, int, 0444);
258 * bit 440; DCON present bit
261 #define OLPC_CMOS_DCON_OFFSET (440 / 8)
262 #define OLPC_CMOS_DCON_MASK 0x01
264 static int __init olpc_init(void)
266 unsigned char *romsig;
269 spin_lock_init(&ec_lock);
271 romsig = ioremap(0xffffffc0, 16);
276 if (strncmp(romsig, "CL1 Q", 7))
278 if (strncmp(romsig+6, romsig+13, 3)) {
279 printk(KERN_INFO "OLPC BIOS signature looks invalid. Assuming not OLPC\n");
282 printk(KERN_INFO "OLPC board with OpenFirmware: %.16s\n", romsig);
284 olpc_platform_info.flags |= OLPC_F_PRESENT;
286 pm_power_off = olpc_power_off;
288 /* Get the platform revision */
289 platform_detect(revision, sizeof(revision));
291 /* If olpc_dcon_present isn't set by the command line, then
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;
302 if (olpc_dcon_present)
303 olpc_platform_info.flags |= OLPC_F_DCON;
305 /* Get the EC revision */
308 /* Check to see if the VSA exists */
311 printk(KERN_INFO "OLPC board revision: %s (EC=%x)\n", revision,
312 olpc_platform_info.ecver);
320 postcore_initcall(olpc_init);