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