blob: 7472613025983f362efd81c2556ff7d8476cad8d [file] [log] [blame]
Andres Salomon3ef0e1f2008-04-29 00:59:53 -07001/*
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 Gleixnerd5d0e882010-02-22 05:42:04 -080020
Andres Salomon3ef0e1f2008-04-29 00:59:53 -070021#include <asm/geode.h>
Thomas Gleixnerd5d0e882010-02-22 05:42:04 -080022#include <asm/setup.h>
Andres Salomon3ef0e1f2008-04-29 00:59:53 -070023#include <asm/olpc.h>
Andres Salomonfd699c72010-06-18 17:46:53 -040024#include <asm/olpc_ofw.h>
Andres Salomon3ef0e1f2008-04-29 00:59:53 -070025
26struct olpc_platform_t olpc_platform_info;
27EXPORT_SYMBOL_GPL(olpc_platform_info);
28
29static 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 */
35static int ec_timeout = EC_BASE_TIMEOUT;
36
37static 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
54static inline unsigned int ibf_status(unsigned int port)
55{
56 return !!(inb(port) & 0x02);
57}
58
59static 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))
65static 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))
85static 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 */
111int 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;
Paul Fox286e5b92010-10-01 18:17:19 +0100117 int restarts = 0;
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700118
119 spin_lock_irqsave(&ec_lock, flags);
120
121 /* Clear OBF */
122 for (i = 0; i < 10 && (obf_status(0x6c) == 1); i++)
123 inb(0x68);
124 if (i == 10) {
125 printk(KERN_ERR "olpc-ec: timeout while attempting to "
126 "clear OBF flag!\n");
127 goto err;
128 }
129
130 if (wait_on_ibf(0x6c, 0)) {
131 printk(KERN_ERR "olpc-ec: timeout waiting for EC to "
132 "quiesce!\n");
133 goto err;
134 }
135
136restart:
137 /*
138 * Note that if we time out during any IBF checks, that's a failure;
139 * we have to return. There's no way for the kernel to clear that.
140 *
141 * If we time out during an OBF check, we can restart the command;
142 * reissuing it will clear the OBF flag, and we should be alright.
143 * The OBF flag will sometimes misbehave due to what we believe
144 * is a hardware quirk..
145 */
Andres Salomon25971862010-06-16 23:19:28 -0400146 pr_devel("olpc-ec: running cmd 0x%x\n", cmd);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700147 outb(cmd, 0x6c);
148
149 if (wait_on_ibf(0x6c, 0)) {
150 printk(KERN_ERR "olpc-ec: timeout waiting for EC to read "
151 "command!\n");
152 goto err;
153 }
154
155 if (inbuf && inlen) {
156 /* write data to EC */
157 for (i = 0; i < inlen; i++) {
158 if (wait_on_ibf(0x6c, 0)) {
159 printk(KERN_ERR "olpc-ec: timeout waiting for"
160 " EC accept data!\n");
161 goto err;
162 }
Andres Salomon25971862010-06-16 23:19:28 -0400163 pr_devel("olpc-ec: sending cmd arg 0x%x\n", inbuf[i]);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700164 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");
Paul Fox286e5b92010-10-01 18:17:19 +0100173 if (restarts++ < 10)
174 goto restart;
175 goto err;
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700176 }
177 outbuf[i] = inb(0x68);
Andres Salomon25971862010-06-16 23:19:28 -0400178 pr_devel("olpc-ec: received 0x%x\n", outbuf[i]);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700179 }
180 }
181
182 ret = 0;
183err:
184 spin_unlock_irqrestore(&ec_lock, flags);
185 return ret;
186}
187EXPORT_SYMBOL_GPL(olpc_ec_cmd);
188
Daniel Drake3e3c4862010-09-23 17:28:46 +0100189static bool __init check_ofw_architecture(void)
190{
191 size_t propsize;
192 char olpc_arch[5];
193 const void *args[] = { NULL, "architecture", olpc_arch, (void *)5 };
194 void *res[] = { &propsize };
195
196 if (olpc_ofw("getprop", args, res)) {
197 printk(KERN_ERR "ofw: getprop call failed!\n");
198 return false;
199 }
200 return propsize == 5 && strncmp("OLPC", olpc_arch, 5) == 0;
201}
202
203static u32 __init get_board_revision(void)
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700204{
205 size_t propsize;
Harvey Harrisone51a1ac2008-09-23 15:20:09 -0700206 __be32 rev;
Andres Salomon54e5bc02010-06-28 22:00:29 -0400207 const void *args[] = { NULL, "board-revision-int", &rev, (void *)4 };
Andres Salomonfd699c72010-06-18 17:46:53 -0400208 void *res[] = { &propsize };
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700209
Andres Salomonfd699c72010-06-18 17:46:53 -0400210 if (olpc_ofw("getprop", args, res) || propsize != 4) {
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700211 printk(KERN_ERR "ofw: getprop call failed!\n");
Daniel Drake3e3c4862010-09-23 17:28:46 +0100212 return cpu_to_be32(0);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700213 }
Daniel Drake3e3c4862010-09-23 17:28:46 +0100214 return be32_to_cpu(rev);
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700215}
Daniel Drake3e3c4862010-09-23 17:28:46 +0100216
217static bool __init platform_detect(void)
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700218{
Daniel Drake3e3c4862010-09-23 17:28:46 +0100219 if (!check_ofw_architecture())
220 return false;
221 olpc_platform_info.flags |= OLPC_F_PRESENT;
222 olpc_platform_info.boardrev = get_board_revision();
223 return true;
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700224}
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700225
226static int __init olpc_init(void)
227{
Daniel Drake3e3c4862010-09-23 17:28:46 +0100228 if (!olpc_ofw_present() || !platform_detect())
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700229 return 0;
230
231 spin_lock_init(&ec_lock);
232
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700233 /* assume B1 and above models always have a DCON */
234 if (olpc_board_at_least(olpc_board(0xb1)))
235 olpc_platform_info.flags |= OLPC_F_DCON;
236
237 /* get the EC revision */
238 olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0,
239 (unsigned char *) &olpc_platform_info.ecver, 1);
240
Thomas Gleixnerd5d0e882010-02-22 05:42:04 -0800241#ifdef CONFIG_PCI_OLPC
Daniel Drake76fb6572010-09-23 17:28:04 +0100242 /* If the VSA exists let it emulate PCI, if not emulate in kernel.
243 * XO-1 only. */
244 if (olpc_platform_info.boardrev < olpc_board_pre(0xd0) &&
245 !cs5535_has_vsa2())
Thomas Gleixnerd5d0e882010-02-22 05:42:04 -0800246 x86_init.pci.arch_init = pci_olpc_init;
247#endif
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700248
249 printk(KERN_INFO "OLPC board revision %s%X (EC=%x)\n",
250 ((olpc_platform_info.boardrev & 0xf) < 8) ? "pre" : "",
251 olpc_platform_info.boardrev >> 4,
252 olpc_platform_info.ecver);
253
Andres Salomon3ef0e1f2008-04-29 00:59:53 -0700254 return 0;
255}
256
257postcore_initcall(olpc_init);