]>
Commit | Line | Data |
---|---|---|
1ddda5cd AG |
1 | /* |
2 | * Apple SMC controller | |
3 | * | |
4 | * Copyright (c) 2007 Alexander Graf | |
5 | * | |
6 | * Authors: Alexander Graf <agraf@suse.de> | |
7 | * Susanne Graf <suse@csgraf.de> | |
8 | * | |
9 | * This library is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2 of the License, or (at your option) any later version. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. | |
21 | * | |
22 | * ***************************************************************** | |
23 | * | |
24 | * In all Intel-based Apple hardware there is an SMC chip to control the | |
25 | * backlight, fans and several other generic device parameters. It also | |
26 | * contains the magic keys used to dongle Mac OS X to the device. | |
27 | * | |
28 | * This driver was mostly created by looking at the Linux AppleSMC driver | |
29 | * implementation and does not support IRQ. | |
30 | * | |
31 | */ | |
32 | ||
0d1c9782 | 33 | #include "qemu/osdep.h" |
0d09e41a | 34 | #include "hw/isa/isa.h" |
a27bd6c7 | 35 | #include "hw/qdev-properties.h" |
28ecbaee | 36 | #include "ui/console.h" |
0b8fa32f | 37 | #include "qemu/module.h" |
1de7afc9 | 38 | #include "qemu/timer.h" |
db1015e9 | 39 | #include "qom/object.h" |
1ddda5cd AG |
40 | |
41 | /* #define DEBUG_SMC */ | |
42 | ||
43 | #define APPLESMC_DEFAULT_IOBASE 0x300 | |
1ddda5cd | 44 | |
36bcd035 GS |
45 | enum { |
46 | APPLESMC_DATA_PORT = 0x00, | |
47 | APPLESMC_CMD_PORT = 0x04, | |
9e507d7e | 48 | APPLESMC_ERR_PORT = 0x1e, |
36bcd035 GS |
49 | APPLESMC_NUM_PORTS = 0x20, |
50 | }; | |
51 | ||
52 | enum { | |
53 | APPLESMC_READ_CMD = 0x10, | |
54 | APPLESMC_WRITE_CMD = 0x11, | |
55 | APPLESMC_GET_KEY_BY_INDEX_CMD = 0x12, | |
56 | APPLESMC_GET_KEY_TYPE_CMD = 0x13, | |
57 | }; | |
1ddda5cd | 58 | |
9e507d7e GS |
59 | enum { |
60 | APPLESMC_ST_CMD_DONE = 0x00, | |
61 | APPLESMC_ST_DATA_READY = 0x01, | |
62 | APPLESMC_ST_BUSY = 0x02, | |
63 | APPLESMC_ST_ACK = 0x04, | |
64 | APPLESMC_ST_NEW_CMD = 0x08, | |
65 | }; | |
66 | ||
67 | enum { | |
68 | APPLESMC_ST_1E_CMD_INTRUPTED = 0x80, | |
69 | APPLESMC_ST_1E_STILL_BAD_CMD = 0x81, | |
70 | APPLESMC_ST_1E_BAD_CMD = 0x82, | |
71 | APPLESMC_ST_1E_NOEXIST = 0x84, | |
72 | APPLESMC_ST_1E_WRITEONLY = 0x85, | |
73 | APPLESMC_ST_1E_READONLY = 0x86, | |
74 | APPLESMC_ST_1E_BAD_INDEX = 0xb8, | |
75 | }; | |
76 | ||
1ddda5cd AG |
77 | #ifdef DEBUG_SMC |
78 | #define smc_debug(...) fprintf(stderr, "AppleSMC: " __VA_ARGS__) | |
79 | #else | |
36bcd035 | 80 | #define smc_debug(...) do { } while (0) |
1ddda5cd AG |
81 | #endif |
82 | ||
83 | static char default_osk[64] = "This is a dummy key. Enter the real key " | |
84 | "using the -osk parameter"; | |
85 | ||
86 | struct AppleSMCData { | |
87 | uint8_t len; | |
88 | const char *key; | |
89 | const char *data; | |
90 | QLIST_ENTRY(AppleSMCData) node; | |
91 | }; | |
92 | ||
db1015e9 | 93 | typedef struct AppleSMCState AppleSMCState; |
82407b6c AF |
94 | #define APPLE_SMC(obj) OBJECT_CHECK(AppleSMCState, (obj), TYPE_APPLE_SMC) |
95 | ||
82407b6c AF |
96 | struct AppleSMCState { |
97 | ISADevice parent_obj; | |
98 | ||
e3914e3a JK |
99 | MemoryRegion io_data; |
100 | MemoryRegion io_cmd; | |
9e507d7e | 101 | MemoryRegion io_err; |
1ddda5cd AG |
102 | uint32_t iobase; |
103 | uint8_t cmd; | |
104 | uint8_t status; | |
9e507d7e GS |
105 | uint8_t status_1e; |
106 | uint8_t last_ret; | |
36bcd035 | 107 | char key[4]; |
1ddda5cd AG |
108 | uint8_t read_pos; |
109 | uint8_t data_len; | |
110 | uint8_t data_pos; | |
111 | uint8_t data[255]; | |
1ddda5cd AG |
112 | char *osk; |
113 | QLIST_HEAD(, AppleSMCData) data_def; | |
114 | }; | |
115 | ||
e3914e3a JK |
116 | static void applesmc_io_cmd_write(void *opaque, hwaddr addr, uint64_t val, |
117 | unsigned size) | |
1ddda5cd | 118 | { |
82407b6c | 119 | AppleSMCState *s = opaque; |
9e507d7e | 120 | uint8_t status = s->status & 0x0f; |
1ddda5cd | 121 | |
9e507d7e | 122 | smc_debug("CMD received: 0x%02x\n", (uint8_t)val); |
36bcd035 GS |
123 | switch (val) { |
124 | case APPLESMC_READ_CMD: | |
9e507d7e GS |
125 | /* did last command run through OK? */ |
126 | if (status == APPLESMC_ST_CMD_DONE || status == APPLESMC_ST_NEW_CMD) { | |
127 | s->cmd = val; | |
128 | s->status = APPLESMC_ST_NEW_CMD | APPLESMC_ST_ACK; | |
129 | } else { | |
130 | smc_debug("ERROR: previous command interrupted!\n"); | |
131 | s->status = APPLESMC_ST_NEW_CMD; | |
132 | s->status_1e = APPLESMC_ST_1E_CMD_INTRUPTED; | |
133 | } | |
36bcd035 | 134 | break; |
9e507d7e GS |
135 | default: |
136 | smc_debug("UNEXPECTED CMD 0x%02x\n", (uint8_t)val); | |
137 | s->status = APPLESMC_ST_NEW_CMD; | |
138 | s->status_1e = APPLESMC_ST_1E_BAD_CMD; | |
1ddda5cd | 139 | } |
1ddda5cd AG |
140 | s->read_pos = 0; |
141 | s->data_pos = 0; | |
142 | } | |
143 | ||
9e507d7e | 144 | static struct AppleSMCData *applesmc_find_key(AppleSMCState *s) |
1ddda5cd AG |
145 | { |
146 | struct AppleSMCData *d; | |
147 | ||
148 | QLIST_FOREACH(d, &s->data_def, node) { | |
149 | if (!memcmp(d->key, s->key, 4)) { | |
9e507d7e | 150 | return d; |
1ddda5cd AG |
151 | } |
152 | } | |
9e507d7e | 153 | return NULL; |
1ddda5cd AG |
154 | } |
155 | ||
e3914e3a JK |
156 | static void applesmc_io_data_write(void *opaque, hwaddr addr, uint64_t val, |
157 | unsigned size) | |
1ddda5cd | 158 | { |
82407b6c | 159 | AppleSMCState *s = opaque; |
9e507d7e | 160 | struct AppleSMCData *d; |
1ddda5cd | 161 | |
9e507d7e | 162 | smc_debug("DATA received: 0x%02x\n", (uint8_t)val); |
36bcd035 GS |
163 | switch (s->cmd) { |
164 | case APPLESMC_READ_CMD: | |
9e507d7e GS |
165 | if ((s->status & 0x0f) == APPLESMC_ST_CMD_DONE) { |
166 | break; | |
167 | } | |
36bcd035 GS |
168 | if (s->read_pos < 4) { |
169 | s->key[s->read_pos] = val; | |
9e507d7e | 170 | s->status = APPLESMC_ST_ACK; |
36bcd035 | 171 | } else if (s->read_pos == 4) { |
9e507d7e GS |
172 | d = applesmc_find_key(s); |
173 | if (d != NULL) { | |
174 | memcpy(s->data, d->data, d->len); | |
175 | s->data_len = d->len; | |
176 | s->data_pos = 0; | |
177 | s->status = APPLESMC_ST_ACK | APPLESMC_ST_DATA_READY; | |
178 | s->status_1e = APPLESMC_ST_CMD_DONE; /* clear on valid key */ | |
179 | } else { | |
180 | smc_debug("READ_CMD: key '%c%c%c%c' not found!\n", | |
181 | s->key[0], s->key[1], s->key[2], s->key[3]); | |
182 | s->status = APPLESMC_ST_CMD_DONE; | |
183 | s->status_1e = APPLESMC_ST_1E_NOEXIST; | |
184 | } | |
36bcd035 GS |
185 | } |
186 | s->read_pos++; | |
187 | break; | |
9e507d7e GS |
188 | default: |
189 | s->status = APPLESMC_ST_CMD_DONE; | |
190 | s->status_1e = APPLESMC_ST_1E_STILL_BAD_CMD; | |
1ddda5cd AG |
191 | } |
192 | } | |
193 | ||
9e507d7e GS |
194 | static void applesmc_io_err_write(void *opaque, hwaddr addr, uint64_t val, |
195 | unsigned size) | |
196 | { | |
197 | smc_debug("ERR_CODE received: 0x%02x, ignoring!\n", (uint8_t)val); | |
198 | /* NOTE: writing to the error port not supported! */ | |
199 | } | |
200 | ||
36bcd035 | 201 | static uint64_t applesmc_io_data_read(void *opaque, hwaddr addr, unsigned size) |
1ddda5cd | 202 | { |
82407b6c | 203 | AppleSMCState *s = opaque; |
1ddda5cd | 204 | |
36bcd035 GS |
205 | switch (s->cmd) { |
206 | case APPLESMC_READ_CMD: | |
9e507d7e GS |
207 | if (!(s->status & APPLESMC_ST_DATA_READY)) { |
208 | break; | |
209 | } | |
36bcd035 | 210 | if (s->data_pos < s->data_len) { |
9e507d7e GS |
211 | s->last_ret = s->data[s->data_pos]; |
212 | smc_debug("READ '%c%c%c%c'[%d] = %02x\n", | |
213 | s->key[0], s->key[1], s->key[2], s->key[3], | |
214 | s->data_pos, s->last_ret); | |
36bcd035 GS |
215 | s->data_pos++; |
216 | if (s->data_pos == s->data_len) { | |
9e507d7e GS |
217 | s->status = APPLESMC_ST_CMD_DONE; |
218 | smc_debug("READ '%c%c%c%c' Len=%d complete!\n", | |
219 | s->key[0], s->key[1], s->key[2], s->key[3], | |
220 | s->data_len); | |
36bcd035 | 221 | } else { |
9e507d7e | 222 | s->status = APPLESMC_ST_ACK | APPLESMC_ST_DATA_READY; |
1ddda5cd | 223 | } |
36bcd035 | 224 | } |
9e507d7e GS |
225 | break; |
226 | default: | |
227 | s->status = APPLESMC_ST_CMD_DONE; | |
228 | s->status_1e = APPLESMC_ST_1E_STILL_BAD_CMD; | |
1ddda5cd | 229 | } |
9e507d7e | 230 | smc_debug("DATA sent: 0x%02x\n", s->last_ret); |
1ddda5cd | 231 | |
9e507d7e | 232 | return s->last_ret; |
1ddda5cd AG |
233 | } |
234 | ||
36bcd035 | 235 | static uint64_t applesmc_io_cmd_read(void *opaque, hwaddr addr, unsigned size) |
1ddda5cd | 236 | { |
82407b6c | 237 | AppleSMCState *s = opaque; |
1ddda5cd | 238 | |
9e507d7e | 239 | smc_debug("CMD sent: 0x%02x\n", s->status); |
1ddda5cd AG |
240 | return s->status; |
241 | } | |
242 | ||
9e507d7e GS |
243 | static uint64_t applesmc_io_err_read(void *opaque, hwaddr addr, unsigned size) |
244 | { | |
245 | AppleSMCState *s = opaque; | |
246 | ||
247 | /* NOTE: read does not clear the 1e status */ | |
248 | smc_debug("ERR_CODE sent: 0x%02x\n", s->status_1e); | |
249 | return s->status_1e; | |
250 | } | |
251 | ||
82407b6c | 252 | static void applesmc_add_key(AppleSMCState *s, const char *key, |
1ddda5cd AG |
253 | int len, const char *data) |
254 | { | |
255 | struct AppleSMCData *def; | |
256 | ||
7267c094 | 257 | def = g_malloc0(sizeof(struct AppleSMCData)); |
1ddda5cd AG |
258 | def->key = key; |
259 | def->len = len; | |
260 | def->data = data; | |
261 | ||
262 | QLIST_INSERT_HEAD(&s->data_def, def, node); | |
263 | } | |
264 | ||
265 | static void qdev_applesmc_isa_reset(DeviceState *dev) | |
266 | { | |
82407b6c | 267 | AppleSMCState *s = APPLE_SMC(dev); |
1ddda5cd AG |
268 | struct AppleSMCData *d, *next; |
269 | ||
270 | /* Remove existing entries */ | |
271 | QLIST_FOREACH_SAFE(d, &s->data_def, node, next) { | |
272 | QLIST_REMOVE(d, node); | |
273 | } | |
9e507d7e GS |
274 | s->status = 0x00; |
275 | s->status_1e = 0x00; | |
276 | s->last_ret = 0x00; | |
1ddda5cd | 277 | |
7f90fa77 | 278 | applesmc_add_key(s, "REV ", 6, "\x01\x13\x0f\x00\x00\x03"); |
1ddda5cd AG |
279 | applesmc_add_key(s, "OSK0", 32, s->osk); |
280 | applesmc_add_key(s, "OSK1", 32, s->osk + 32); | |
281 | applesmc_add_key(s, "NATJ", 1, "\0"); | |
282 | applesmc_add_key(s, "MSSP", 1, "\0"); | |
283 | applesmc_add_key(s, "MSSD", 1, "\0x3"); | |
284 | } | |
285 | ||
e3914e3a JK |
286 | static const MemoryRegionOps applesmc_data_io_ops = { |
287 | .write = applesmc_io_data_write, | |
288 | .read = applesmc_io_data_read, | |
289 | .endianness = DEVICE_NATIVE_ENDIAN, | |
290 | .impl = { | |
291 | .min_access_size = 1, | |
292 | .max_access_size = 1, | |
293 | }, | |
294 | }; | |
295 | ||
296 | static const MemoryRegionOps applesmc_cmd_io_ops = { | |
297 | .write = applesmc_io_cmd_write, | |
298 | .read = applesmc_io_cmd_read, | |
299 | .endianness = DEVICE_NATIVE_ENDIAN, | |
300 | .impl = { | |
301 | .min_access_size = 1, | |
302 | .max_access_size = 1, | |
303 | }, | |
304 | }; | |
305 | ||
9e507d7e GS |
306 | static const MemoryRegionOps applesmc_err_io_ops = { |
307 | .write = applesmc_io_err_write, | |
308 | .read = applesmc_io_err_read, | |
309 | .endianness = DEVICE_NATIVE_ENDIAN, | |
310 | .impl = { | |
311 | .min_access_size = 1, | |
312 | .max_access_size = 1, | |
313 | }, | |
314 | }; | |
315 | ||
db895a1e | 316 | static void applesmc_isa_realize(DeviceState *dev, Error **errp) |
1ddda5cd | 317 | { |
82407b6c | 318 | AppleSMCState *s = APPLE_SMC(dev); |
1ddda5cd | 319 | |
3c161542 | 320 | memory_region_init_io(&s->io_data, OBJECT(s), &applesmc_data_io_ops, s, |
1b8274d4 | 321 | "applesmc-data", 1); |
e3914e3a JK |
322 | isa_register_ioport(&s->parent_obj, &s->io_data, |
323 | s->iobase + APPLESMC_DATA_PORT); | |
324 | ||
3c161542 | 325 | memory_region_init_io(&s->io_cmd, OBJECT(s), &applesmc_cmd_io_ops, s, |
1b8274d4 | 326 | "applesmc-cmd", 1); |
e3914e3a JK |
327 | isa_register_ioport(&s->parent_obj, &s->io_cmd, |
328 | s->iobase + APPLESMC_CMD_PORT); | |
1ddda5cd | 329 | |
9e507d7e GS |
330 | memory_region_init_io(&s->io_err, OBJECT(s), &applesmc_err_io_ops, s, |
331 | "applesmc-err", 1); | |
332 | isa_register_ioport(&s->parent_obj, &s->io_err, | |
333 | s->iobase + APPLESMC_ERR_PORT); | |
334 | ||
1ddda5cd | 335 | if (!s->osk || (strlen(s->osk) != 64)) { |
2ab4b135 | 336 | warn_report("Using AppleSMC with invalid key"); |
1ddda5cd AG |
337 | s->osk = default_osk; |
338 | } | |
339 | ||
340 | QLIST_INIT(&s->data_def); | |
db895a1e | 341 | qdev_applesmc_isa_reset(dev); |
1ddda5cd AG |
342 | } |
343 | ||
39bffca2 | 344 | static Property applesmc_isa_properties[] = { |
1142e45f IM |
345 | DEFINE_PROP_UINT32(APPLESMC_PROP_IO_BASE, AppleSMCState, iobase, |
346 | APPLESMC_DEFAULT_IOBASE), | |
82407b6c | 347 | DEFINE_PROP_STRING("osk", AppleSMCState, osk), |
39bffca2 AL |
348 | DEFINE_PROP_END_OF_LIST(), |
349 | }; | |
350 | ||
8f04ee08 AL |
351 | static void qdev_applesmc_class_init(ObjectClass *klass, void *data) |
352 | { | |
39bffca2 | 353 | DeviceClass *dc = DEVICE_CLASS(klass); |
db895a1e AF |
354 | |
355 | dc->realize = applesmc_isa_realize; | |
39bffca2 | 356 | dc->reset = qdev_applesmc_isa_reset; |
4f67d30b | 357 | device_class_set_props(dc, applesmc_isa_properties); |
125ee0ed | 358 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
8f04ee08 AL |
359 | } |
360 | ||
8c43a6f0 | 361 | static const TypeInfo applesmc_isa_info = { |
82407b6c | 362 | .name = TYPE_APPLE_SMC, |
39bffca2 | 363 | .parent = TYPE_ISA_DEVICE, |
82407b6c | 364 | .instance_size = sizeof(AppleSMCState), |
39bffca2 | 365 | .class_init = qdev_applesmc_class_init, |
1ddda5cd AG |
366 | }; |
367 | ||
83f7d43a | 368 | static void applesmc_register_types(void) |
1ddda5cd | 369 | { |
39bffca2 | 370 | type_register_static(&applesmc_isa_info); |
1ddda5cd AG |
371 | } |
372 | ||
83f7d43a | 373 | type_init(applesmc_register_types) |