Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Support for the OLPC DCON and OLPC EC access |
| 3 | * |
| 4 | * Copyright © 2006 Advanced Micro Devices, Inc. |
| 5 | * Copyright © 2007-2008 Andres Salomon <dilinger@debian.org> |
| 6 | * |
| 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. |
| 11 | */ |
| 12 | |
| 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> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/string.h> |
Thomas Gleixner | d5d0e88 | 2010-02-22 05:42:04 -0800 | [diff] [blame] | 20 | |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 21 | #include <asm/geode.h> |
Thomas Gleixner | d5d0e88 | 2010-02-22 05:42:04 -0800 | [diff] [blame] | 22 | #include <asm/setup.h> |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 23 | #include <asm/olpc.h> |
Andres Salomon | fd699c7 | 2010-06-18 17:46:53 -0400 | [diff] [blame^] | 24 | #include <asm/olpc_ofw.h> |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 25 | |
| 26 | struct olpc_platform_t olpc_platform_info; |
| 27 | EXPORT_SYMBOL_GPL(olpc_platform_info); |
| 28 | |
| 29 | static DEFINE_SPINLOCK(ec_lock); |
| 30 | |
| 31 | /* what the timeout *should* be (in ms) */ |
| 32 | #define EC_BASE_TIMEOUT 20 |
| 33 | |
| 34 | /* the timeout that bugs in the EC might force us to actually use */ |
| 35 | static int ec_timeout = EC_BASE_TIMEOUT; |
| 36 | |
| 37 | static int __init olpc_ec_timeout_set(char *str) |
| 38 | { |
| 39 | if (get_option(&str, &ec_timeout) != 1) { |
| 40 | ec_timeout = EC_BASE_TIMEOUT; |
| 41 | printk(KERN_ERR "olpc-ec: invalid argument to " |
| 42 | "'olpc_ec_timeout=', ignoring!\n"); |
| 43 | } |
| 44 | printk(KERN_DEBUG "olpc-ec: using %d ms delay for EC commands.\n", |
| 45 | ec_timeout); |
| 46 | return 1; |
| 47 | } |
| 48 | __setup("olpc_ec_timeout=", olpc_ec_timeout_set); |
| 49 | |
| 50 | /* |
| 51 | * These {i,o}bf_status functions return whether the buffers are full or not. |
| 52 | */ |
| 53 | |
| 54 | static inline unsigned int ibf_status(unsigned int port) |
| 55 | { |
| 56 | return !!(inb(port) & 0x02); |
| 57 | } |
| 58 | |
| 59 | static inline unsigned int obf_status(unsigned int port) |
| 60 | { |
| 61 | return inb(port) & 0x01; |
| 62 | } |
| 63 | |
| 64 | #define wait_on_ibf(p, d) __wait_on_ibf(__LINE__, (p), (d)) |
| 65 | static int __wait_on_ibf(unsigned int line, unsigned int port, int desired) |
| 66 | { |
| 67 | unsigned int timeo; |
| 68 | int state = ibf_status(port); |
| 69 | |
| 70 | for (timeo = ec_timeout; state != desired && timeo; timeo--) { |
| 71 | mdelay(1); |
| 72 | state = ibf_status(port); |
| 73 | } |
| 74 | |
| 75 | if ((state == desired) && (ec_timeout > EC_BASE_TIMEOUT) && |
| 76 | timeo < (ec_timeout - EC_BASE_TIMEOUT)) { |
| 77 | printk(KERN_WARNING "olpc-ec: %d: waited %u ms for IBF!\n", |
| 78 | line, ec_timeout - timeo); |
| 79 | } |
| 80 | |
| 81 | return !(state == desired); |
| 82 | } |
| 83 | |
| 84 | #define wait_on_obf(p, d) __wait_on_obf(__LINE__, (p), (d)) |
| 85 | static int __wait_on_obf(unsigned int line, unsigned int port, int desired) |
| 86 | { |
| 87 | unsigned int timeo; |
| 88 | int state = obf_status(port); |
| 89 | |
| 90 | for (timeo = ec_timeout; state != desired && timeo; timeo--) { |
| 91 | mdelay(1); |
| 92 | state = obf_status(port); |
| 93 | } |
| 94 | |
| 95 | if ((state == desired) && (ec_timeout > EC_BASE_TIMEOUT) && |
| 96 | timeo < (ec_timeout - EC_BASE_TIMEOUT)) { |
| 97 | printk(KERN_WARNING "olpc-ec: %d: waited %u ms for OBF!\n", |
| 98 | line, ec_timeout - timeo); |
| 99 | } |
| 100 | |
| 101 | return !(state == desired); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * This allows the kernel to run Embedded Controller commands. The EC is |
| 106 | * documented at <http://wiki.laptop.org/go/Embedded_controller>, and the |
| 107 | * available EC commands are here: |
| 108 | * <http://wiki.laptop.org/go/Ec_specification>. Unfortunately, while |
| 109 | * OpenFirmware's source is available, the EC's is not. |
| 110 | */ |
| 111 | int olpc_ec_cmd(unsigned char cmd, unsigned char *inbuf, size_t inlen, |
| 112 | unsigned char *outbuf, size_t outlen) |
| 113 | { |
| 114 | unsigned long flags; |
| 115 | int ret = -EIO; |
| 116 | int i; |
| 117 | |
| 118 | spin_lock_irqsave(&ec_lock, flags); |
| 119 | |
| 120 | /* Clear OBF */ |
| 121 | for (i = 0; i < 10 && (obf_status(0x6c) == 1); i++) |
| 122 | inb(0x68); |
| 123 | if (i == 10) { |
| 124 | printk(KERN_ERR "olpc-ec: timeout while attempting to " |
| 125 | "clear OBF flag!\n"); |
| 126 | goto err; |
| 127 | } |
| 128 | |
| 129 | if (wait_on_ibf(0x6c, 0)) { |
| 130 | printk(KERN_ERR "olpc-ec: timeout waiting for EC to " |
| 131 | "quiesce!\n"); |
| 132 | goto err; |
| 133 | } |
| 134 | |
| 135 | restart: |
| 136 | /* |
| 137 | * Note that if we time out during any IBF checks, that's a failure; |
| 138 | * we have to return. There's no way for the kernel to clear that. |
| 139 | * |
| 140 | * If we time out during an OBF check, we can restart the command; |
| 141 | * reissuing it will clear the OBF flag, and we should be alright. |
| 142 | * The OBF flag will sometimes misbehave due to what we believe |
| 143 | * is a hardware quirk.. |
| 144 | */ |
| 145 | printk(KERN_DEBUG "olpc-ec: running cmd 0x%x\n", cmd); |
| 146 | outb(cmd, 0x6c); |
| 147 | |
| 148 | if (wait_on_ibf(0x6c, 0)) { |
| 149 | printk(KERN_ERR "olpc-ec: timeout waiting for EC to read " |
| 150 | "command!\n"); |
| 151 | goto err; |
| 152 | } |
| 153 | |
| 154 | if (inbuf && inlen) { |
| 155 | /* write data to EC */ |
| 156 | for (i = 0; i < inlen; i++) { |
| 157 | if (wait_on_ibf(0x6c, 0)) { |
| 158 | printk(KERN_ERR "olpc-ec: timeout waiting for" |
| 159 | " EC accept data!\n"); |
| 160 | goto err; |
| 161 | } |
| 162 | printk(KERN_DEBUG "olpc-ec: sending cmd arg 0x%x\n", |
| 163 | inbuf[i]); |
| 164 | outb(inbuf[i], 0x68); |
| 165 | } |
| 166 | } |
| 167 | if (outbuf && outlen) { |
| 168 | /* read data from EC */ |
| 169 | for (i = 0; i < outlen; i++) { |
| 170 | if (wait_on_obf(0x6c, 1)) { |
| 171 | printk(KERN_ERR "olpc-ec: timeout waiting for" |
| 172 | " EC to provide data!\n"); |
| 173 | goto restart; |
| 174 | } |
| 175 | outbuf[i] = inb(0x68); |
| 176 | printk(KERN_DEBUG "olpc-ec: received 0x%x\n", |
| 177 | outbuf[i]); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | ret = 0; |
| 182 | err: |
| 183 | spin_unlock_irqrestore(&ec_lock, flags); |
| 184 | return ret; |
| 185 | } |
| 186 | EXPORT_SYMBOL_GPL(olpc_ec_cmd); |
| 187 | |
Andres Salomon | fd699c7 | 2010-06-18 17:46:53 -0400 | [diff] [blame^] | 188 | #ifdef CONFIG_OLPC_OPENFIRMWARE |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 189 | static void __init platform_detect(void) |
| 190 | { |
| 191 | size_t propsize; |
Harvey Harrison | e51a1ac | 2008-09-23 15:20:09 -0700 | [diff] [blame] | 192 | __be32 rev; |
Andres Salomon | fd699c7 | 2010-06-18 17:46:53 -0400 | [diff] [blame^] | 193 | void *args[] = { NULL, "board-revision-int", &rev, (void *)4 }; |
| 194 | void *res[] = { &propsize }; |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 195 | |
Andres Salomon | fd699c7 | 2010-06-18 17:46:53 -0400 | [diff] [blame^] | 196 | if (olpc_ofw("getprop", args, res) || propsize != 4) { |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 197 | printk(KERN_ERR "ofw: getprop call failed!\n"); |
Harvey Harrison | e51a1ac | 2008-09-23 15:20:09 -0700 | [diff] [blame] | 198 | rev = cpu_to_be32(0); |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 199 | } |
| 200 | olpc_platform_info.boardrev = be32_to_cpu(rev); |
| 201 | } |
| 202 | #else |
| 203 | static void __init platform_detect(void) |
| 204 | { |
| 205 | /* stopgap until OFW support is added to the kernel */ |
Chris Ball | e49590b | 2009-02-13 20:56:18 -0500 | [diff] [blame] | 206 | olpc_platform_info.boardrev = olpc_board(0xc2); |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 207 | } |
| 208 | #endif |
| 209 | |
| 210 | static int __init olpc_init(void) |
| 211 | { |
| 212 | unsigned char *romsig; |
| 213 | |
| 214 | /* The ioremap check is dangerous; limit what we run it on */ |
Andres Salomon | f060f27 | 2009-12-14 18:00:40 -0800 | [diff] [blame] | 215 | if (!is_geode() || cs5535_has_vsa2()) |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 216 | return 0; |
| 217 | |
| 218 | spin_lock_init(&ec_lock); |
| 219 | |
| 220 | romsig = ioremap(0xffffffc0, 16); |
| 221 | if (!romsig) |
| 222 | return 0; |
| 223 | |
| 224 | if (strncmp(romsig, "CL1 Q", 7)) |
| 225 | goto unmap; |
| 226 | if (strncmp(romsig+6, romsig+13, 3)) { |
| 227 | printk(KERN_INFO "OLPC BIOS signature looks invalid. " |
| 228 | "Assuming not OLPC\n"); |
| 229 | goto unmap; |
| 230 | } |
| 231 | |
| 232 | printk(KERN_INFO "OLPC board with OpenFirmware %.16s\n", romsig); |
| 233 | olpc_platform_info.flags |= OLPC_F_PRESENT; |
| 234 | |
| 235 | /* get the platform revision */ |
| 236 | platform_detect(); |
| 237 | |
| 238 | /* assume B1 and above models always have a DCON */ |
| 239 | if (olpc_board_at_least(olpc_board(0xb1))) |
| 240 | olpc_platform_info.flags |= OLPC_F_DCON; |
| 241 | |
| 242 | /* get the EC revision */ |
| 243 | olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0, |
| 244 | (unsigned char *) &olpc_platform_info.ecver, 1); |
| 245 | |
Thomas Gleixner | d5d0e88 | 2010-02-22 05:42:04 -0800 | [diff] [blame] | 246 | #ifdef CONFIG_PCI_OLPC |
| 247 | /* If the VSA exists let it emulate PCI, if not emulate in kernel */ |
| 248 | if (!cs5535_has_vsa2()) |
| 249 | x86_init.pci.arch_init = pci_olpc_init; |
| 250 | #endif |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 251 | |
| 252 | printk(KERN_INFO "OLPC board revision %s%X (EC=%x)\n", |
| 253 | ((olpc_platform_info.boardrev & 0xf) < 8) ? "pre" : "", |
| 254 | olpc_platform_info.boardrev >> 4, |
| 255 | olpc_platform_info.ecver); |
| 256 | |
| 257 | unmap: |
| 258 | iounmap(romsig); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | postcore_initcall(olpc_init); |