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> |
Daniel Drake | 447b1d4 | 2010-10-13 19:10:42 +0100 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
Thomas Gleixner | d5d0e88 | 2010-02-22 05:42:04 -0800 | [diff] [blame] | 21 | |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 22 | #include <asm/geode.h> |
Thomas Gleixner | d5d0e88 | 2010-02-22 05:42:04 -0800 | [diff] [blame] | 23 | #include <asm/setup.h> |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 24 | #include <asm/olpc.h> |
Andres Salomon | fd699c7 | 2010-06-18 17:46:53 -0400 | [diff] [blame] | 25 | #include <asm/olpc_ofw.h> |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 26 | |
| 27 | struct olpc_platform_t olpc_platform_info; |
| 28 | EXPORT_SYMBOL_GPL(olpc_platform_info); |
| 29 | |
| 30 | static DEFINE_SPINLOCK(ec_lock); |
| 31 | |
| 32 | /* what the timeout *should* be (in ms) */ |
| 33 | #define EC_BASE_TIMEOUT 20 |
| 34 | |
| 35 | /* the timeout that bugs in the EC might force us to actually use */ |
| 36 | static int ec_timeout = EC_BASE_TIMEOUT; |
| 37 | |
| 38 | static int __init olpc_ec_timeout_set(char *str) |
| 39 | { |
| 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"); |
| 44 | } |
| 45 | printk(KERN_DEBUG "olpc-ec: using %d ms delay for EC commands.\n", |
| 46 | ec_timeout); |
| 47 | return 1; |
| 48 | } |
| 49 | __setup("olpc_ec_timeout=", olpc_ec_timeout_set); |
| 50 | |
| 51 | /* |
| 52 | * These {i,o}bf_status functions return whether the buffers are full or not. |
| 53 | */ |
| 54 | |
| 55 | static inline unsigned int ibf_status(unsigned int port) |
| 56 | { |
| 57 | return !!(inb(port) & 0x02); |
| 58 | } |
| 59 | |
| 60 | static inline unsigned int obf_status(unsigned int port) |
| 61 | { |
| 62 | return inb(port) & 0x01; |
| 63 | } |
| 64 | |
| 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) |
| 67 | { |
| 68 | unsigned int timeo; |
| 69 | int state = ibf_status(port); |
| 70 | |
| 71 | for (timeo = ec_timeout; state != desired && timeo; timeo--) { |
| 72 | mdelay(1); |
| 73 | state = ibf_status(port); |
| 74 | } |
| 75 | |
| 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); |
| 80 | } |
| 81 | |
| 82 | return !(state == desired); |
| 83 | } |
| 84 | |
| 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) |
| 87 | { |
| 88 | unsigned int timeo; |
| 89 | int state = obf_status(port); |
| 90 | |
| 91 | for (timeo = ec_timeout; state != desired && timeo; timeo--) { |
| 92 | mdelay(1); |
| 93 | state = obf_status(port); |
| 94 | } |
| 95 | |
| 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); |
| 100 | } |
| 101 | |
| 102 | return !(state == desired); |
| 103 | } |
| 104 | |
| 105 | /* |
| 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. |
| 111 | */ |
| 112 | int olpc_ec_cmd(unsigned char cmd, unsigned char *inbuf, size_t inlen, |
| 113 | unsigned char *outbuf, size_t outlen) |
| 114 | { |
| 115 | unsigned long flags; |
| 116 | int ret = -EIO; |
| 117 | int i; |
Paul Fox | 286e5b9 | 2010-10-01 18:17:19 +0100 | [diff] [blame] | 118 | int restarts = 0; |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 119 | |
| 120 | spin_lock_irqsave(&ec_lock, flags); |
| 121 | |
| 122 | /* Clear OBF */ |
| 123 | for (i = 0; i < 10 && (obf_status(0x6c) == 1); i++) |
| 124 | inb(0x68); |
| 125 | if (i == 10) { |
| 126 | printk(KERN_ERR "olpc-ec: timeout while attempting to " |
| 127 | "clear OBF flag!\n"); |
| 128 | goto err; |
| 129 | } |
| 130 | |
| 131 | if (wait_on_ibf(0x6c, 0)) { |
| 132 | printk(KERN_ERR "olpc-ec: timeout waiting for EC to " |
| 133 | "quiesce!\n"); |
| 134 | goto err; |
| 135 | } |
| 136 | |
| 137 | restart: |
| 138 | /* |
| 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. |
| 141 | * |
| 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.. |
| 146 | */ |
Andres Salomon | 2597186 | 2010-06-16 23:19:28 -0400 | [diff] [blame] | 147 | pr_devel("olpc-ec: running cmd 0x%x\n", cmd); |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 148 | outb(cmd, 0x6c); |
| 149 | |
| 150 | if (wait_on_ibf(0x6c, 0)) { |
| 151 | printk(KERN_ERR "olpc-ec: timeout waiting for EC to read " |
| 152 | "command!\n"); |
| 153 | goto err; |
| 154 | } |
| 155 | |
| 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"); |
| 162 | goto err; |
| 163 | } |
Andres Salomon | 2597186 | 2010-06-16 23:19:28 -0400 | [diff] [blame] | 164 | pr_devel("olpc-ec: sending cmd arg 0x%x\n", inbuf[i]); |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 165 | outb(inbuf[i], 0x68); |
| 166 | } |
| 167 | } |
| 168 | if (outbuf && outlen) { |
| 169 | /* read data from EC */ |
| 170 | for (i = 0; i < outlen; i++) { |
| 171 | if (wait_on_obf(0x6c, 1)) { |
| 172 | printk(KERN_ERR "olpc-ec: timeout waiting for" |
| 173 | " EC to provide data!\n"); |
Paul Fox | 286e5b9 | 2010-10-01 18:17:19 +0100 | [diff] [blame] | 174 | if (restarts++ < 10) |
| 175 | goto restart; |
| 176 | goto err; |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 177 | } |
| 178 | outbuf[i] = inb(0x68); |
Andres Salomon | 2597186 | 2010-06-16 23:19:28 -0400 | [diff] [blame] | 179 | pr_devel("olpc-ec: received 0x%x\n", outbuf[i]); |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | ret = 0; |
| 184 | err: |
| 185 | spin_unlock_irqrestore(&ec_lock, flags); |
| 186 | return ret; |
| 187 | } |
| 188 | EXPORT_SYMBOL_GPL(olpc_ec_cmd); |
| 189 | |
Daniel Drake | 3e3c486 | 2010-09-23 17:28:46 +0100 | [diff] [blame] | 190 | static bool __init check_ofw_architecture(void) |
| 191 | { |
| 192 | size_t propsize; |
| 193 | char olpc_arch[5]; |
| 194 | const void *args[] = { NULL, "architecture", olpc_arch, (void *)5 }; |
| 195 | void *res[] = { &propsize }; |
| 196 | |
| 197 | if (olpc_ofw("getprop", args, res)) { |
| 198 | printk(KERN_ERR "ofw: getprop call failed!\n"); |
| 199 | return false; |
| 200 | } |
| 201 | return propsize == 5 && strncmp("OLPC", olpc_arch, 5) == 0; |
| 202 | } |
| 203 | |
| 204 | static u32 __init get_board_revision(void) |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 205 | { |
| 206 | size_t propsize; |
Harvey Harrison | e51a1ac | 2008-09-23 15:20:09 -0700 | [diff] [blame] | 207 | __be32 rev; |
Andres Salomon | 54e5bc02 | 2010-06-28 22:00:29 -0400 | [diff] [blame] | 208 | const void *args[] = { NULL, "board-revision-int", &rev, (void *)4 }; |
Andres Salomon | fd699c7 | 2010-06-18 17:46:53 -0400 | [diff] [blame] | 209 | void *res[] = { &propsize }; |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 210 | |
Andres Salomon | fd699c7 | 2010-06-18 17:46:53 -0400 | [diff] [blame] | 211 | if (olpc_ofw("getprop", args, res) || propsize != 4) { |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 212 | printk(KERN_ERR "ofw: getprop call failed!\n"); |
Daniel Drake | 3e3c486 | 2010-09-23 17:28:46 +0100 | [diff] [blame] | 213 | return cpu_to_be32(0); |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 214 | } |
Daniel Drake | 3e3c486 | 2010-09-23 17:28:46 +0100 | [diff] [blame] | 215 | return be32_to_cpu(rev); |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 216 | } |
Daniel Drake | 3e3c486 | 2010-09-23 17:28:46 +0100 | [diff] [blame] | 217 | |
| 218 | static bool __init platform_detect(void) |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 219 | { |
Daniel Drake | 3e3c486 | 2010-09-23 17:28:46 +0100 | [diff] [blame] | 220 | if (!check_ofw_architecture()) |
| 221 | return false; |
| 222 | olpc_platform_info.flags |= OLPC_F_PRESENT; |
| 223 | olpc_platform_info.boardrev = get_board_revision(); |
| 224 | return true; |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 225 | } |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 226 | |
Daniel Drake | 447b1d4 | 2010-10-13 19:10:42 +0100 | [diff] [blame] | 227 | static int __init add_xo1_platform_devices(void) |
| 228 | { |
| 229 | struct platform_device *pdev; |
| 230 | |
| 231 | pdev = platform_device_register_simple("xo1-rfkill", -1, NULL, 0); |
| 232 | if (IS_ERR(pdev)) |
| 233 | return PTR_ERR(pdev); |
| 234 | |
| 235 | pdev = platform_device_register_simple("olpc-xo1", -1, NULL, 0); |
| 236 | if (IS_ERR(pdev)) |
| 237 | return PTR_ERR(pdev); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 242 | static int __init olpc_init(void) |
| 243 | { |
Daniel Drake | 447b1d4 | 2010-10-13 19:10:42 +0100 | [diff] [blame] | 244 | int r = 0; |
| 245 | |
Daniel Drake | 3e3c486 | 2010-09-23 17:28:46 +0100 | [diff] [blame] | 246 | if (!olpc_ofw_present() || !platform_detect()) |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 247 | return 0; |
| 248 | |
| 249 | spin_lock_init(&ec_lock); |
| 250 | |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 251 | /* assume B1 and above models always have a DCON */ |
| 252 | if (olpc_board_at_least(olpc_board(0xb1))) |
| 253 | olpc_platform_info.flags |= OLPC_F_DCON; |
| 254 | |
| 255 | /* get the EC revision */ |
| 256 | olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0, |
| 257 | (unsigned char *) &olpc_platform_info.ecver, 1); |
| 258 | |
Thomas Gleixner | d5d0e88 | 2010-02-22 05:42:04 -0800 | [diff] [blame] | 259 | #ifdef CONFIG_PCI_OLPC |
Daniel Drake | 76fb657 | 2010-09-23 17:28:04 +0100 | [diff] [blame] | 260 | /* If the VSA exists let it emulate PCI, if not emulate in kernel. |
| 261 | * XO-1 only. */ |
| 262 | if (olpc_platform_info.boardrev < olpc_board_pre(0xd0) && |
| 263 | !cs5535_has_vsa2()) |
Thomas Gleixner | d5d0e88 | 2010-02-22 05:42:04 -0800 | [diff] [blame] | 264 | x86_init.pci.arch_init = pci_olpc_init; |
| 265 | #endif |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 266 | |
| 267 | printk(KERN_INFO "OLPC board revision %s%X (EC=%x)\n", |
| 268 | ((olpc_platform_info.boardrev & 0xf) < 8) ? "pre" : "", |
| 269 | olpc_platform_info.boardrev >> 4, |
| 270 | olpc_platform_info.ecver); |
| 271 | |
Daniel Drake | 447b1d4 | 2010-10-13 19:10:42 +0100 | [diff] [blame] | 272 | if (olpc_platform_info.boardrev < olpc_board_pre(0xd0)) { /* XO-1 */ |
| 273 | r = add_xo1_platform_devices(); |
| 274 | if (r) |
| 275 | return r; |
| 276 | } |
| 277 | |
Andres Salomon | 3ef0e1f | 2008-04-29 00:59:53 -0700 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | postcore_initcall(olpc_init); |