4 * Copyright (c) 2007 Alexander Graf
6 * Authors: Alexander Graf <agraf@suse.de>
7 * Susanne Graf <suse@csgraf.de>
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.
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.
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/>.
22 * *****************************************************************
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.
28 * This driver was mostly created by looking at the Linux AppleSMC driver
29 * implementation and does not support IRQ.
33 #include "qemu/osdep.h"
34 #include "hw/isa/isa.h"
35 #include "hw/qdev-properties.h"
36 #include "ui/console.h"
37 #include "qemu/module.h"
38 #include "qemu/timer.h"
39 #include "qom/object.h"
41 /* #define DEBUG_SMC */
43 #define APPLESMC_DEFAULT_IOBASE 0x300
46 APPLESMC_DATA_PORT
= 0x00,
47 APPLESMC_CMD_PORT
= 0x04,
48 APPLESMC_ERR_PORT
= 0x1e,
49 APPLESMC_NUM_PORTS
= 0x20,
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,
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,
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,
78 #define smc_debug(...) fprintf(stderr, "AppleSMC: " __VA_ARGS__)
80 #define smc_debug(...) do { } while (0)
83 static char default_osk
[64] = "This is a dummy key. Enter the real key "
84 "using the -osk parameter";
90 QLIST_ENTRY(AppleSMCData
) node
;
93 typedef struct AppleSMCState AppleSMCState
;
94 DECLARE_INSTANCE_CHECKER(AppleSMCState
, APPLE_SMC
,
97 struct AppleSMCState
{
100 MemoryRegion io_data
;
114 QLIST_HEAD(, AppleSMCData
) data_def
;
117 static void applesmc_io_cmd_write(void *opaque
, hwaddr addr
, uint64_t val
,
120 AppleSMCState
*s
= opaque
;
121 uint8_t status
= s
->status
& 0x0f;
123 smc_debug("CMD received: 0x%02x\n", (uint8_t)val
);
125 case APPLESMC_READ_CMD
:
126 /* did last command run through OK? */
127 if (status
== APPLESMC_ST_CMD_DONE
|| status
== APPLESMC_ST_NEW_CMD
) {
129 s
->status
= APPLESMC_ST_NEW_CMD
| APPLESMC_ST_ACK
;
131 smc_debug("ERROR: previous command interrupted!\n");
132 s
->status
= APPLESMC_ST_NEW_CMD
;
133 s
->status_1e
= APPLESMC_ST_1E_CMD_INTRUPTED
;
137 smc_debug("UNEXPECTED CMD 0x%02x\n", (uint8_t)val
);
138 s
->status
= APPLESMC_ST_NEW_CMD
;
139 s
->status_1e
= APPLESMC_ST_1E_BAD_CMD
;
145 static struct AppleSMCData
*applesmc_find_key(AppleSMCState
*s
)
147 struct AppleSMCData
*d
;
149 QLIST_FOREACH(d
, &s
->data_def
, node
) {
150 if (!memcmp(d
->key
, s
->key
, 4)) {
157 static void applesmc_io_data_write(void *opaque
, hwaddr addr
, uint64_t val
,
160 AppleSMCState
*s
= opaque
;
161 struct AppleSMCData
*d
;
163 smc_debug("DATA received: 0x%02x\n", (uint8_t)val
);
165 case APPLESMC_READ_CMD
:
166 if ((s
->status
& 0x0f) == APPLESMC_ST_CMD_DONE
) {
169 if (s
->read_pos
< 4) {
170 s
->key
[s
->read_pos
] = val
;
171 s
->status
= APPLESMC_ST_ACK
;
172 } else if (s
->read_pos
== 4) {
173 d
= applesmc_find_key(s
);
175 memcpy(s
->data
, d
->data
, d
->len
);
176 s
->data_len
= d
->len
;
178 s
->status
= APPLESMC_ST_ACK
| APPLESMC_ST_DATA_READY
;
179 s
->status_1e
= APPLESMC_ST_CMD_DONE
; /* clear on valid key */
181 smc_debug("READ_CMD: key '%c%c%c%c' not found!\n",
182 s
->key
[0], s
->key
[1], s
->key
[2], s
->key
[3]);
183 s
->status
= APPLESMC_ST_CMD_DONE
;
184 s
->status_1e
= APPLESMC_ST_1E_NOEXIST
;
190 s
->status
= APPLESMC_ST_CMD_DONE
;
191 s
->status_1e
= APPLESMC_ST_1E_STILL_BAD_CMD
;
195 static void applesmc_io_err_write(void *opaque
, hwaddr addr
, uint64_t val
,
198 smc_debug("ERR_CODE received: 0x%02x, ignoring!\n", (uint8_t)val
);
199 /* NOTE: writing to the error port not supported! */
202 static uint64_t applesmc_io_data_read(void *opaque
, hwaddr addr
, unsigned size
)
204 AppleSMCState
*s
= opaque
;
207 case APPLESMC_READ_CMD
:
208 if (!(s
->status
& APPLESMC_ST_DATA_READY
)) {
211 if (s
->data_pos
< s
->data_len
) {
212 s
->last_ret
= s
->data
[s
->data_pos
];
213 smc_debug("READ '%c%c%c%c'[%d] = %02x\n",
214 s
->key
[0], s
->key
[1], s
->key
[2], s
->key
[3],
215 s
->data_pos
, s
->last_ret
);
217 if (s
->data_pos
== s
->data_len
) {
218 s
->status
= APPLESMC_ST_CMD_DONE
;
219 smc_debug("READ '%c%c%c%c' Len=%d complete!\n",
220 s
->key
[0], s
->key
[1], s
->key
[2], s
->key
[3],
223 s
->status
= APPLESMC_ST_ACK
| APPLESMC_ST_DATA_READY
;
228 s
->status
= APPLESMC_ST_CMD_DONE
;
229 s
->status_1e
= APPLESMC_ST_1E_STILL_BAD_CMD
;
231 smc_debug("DATA sent: 0x%02x\n", s
->last_ret
);
236 static uint64_t applesmc_io_cmd_read(void *opaque
, hwaddr addr
, unsigned size
)
238 AppleSMCState
*s
= opaque
;
240 smc_debug("CMD sent: 0x%02x\n", s
->status
);
244 static uint64_t applesmc_io_err_read(void *opaque
, hwaddr addr
, unsigned size
)
246 AppleSMCState
*s
= opaque
;
248 /* NOTE: read does not clear the 1e status */
249 smc_debug("ERR_CODE sent: 0x%02x\n", s
->status_1e
);
253 static void applesmc_add_key(AppleSMCState
*s
, const char *key
,
254 int len
, const char *data
)
256 struct AppleSMCData
*def
;
258 def
= g_malloc0(sizeof(struct AppleSMCData
));
263 QLIST_INSERT_HEAD(&s
->data_def
, def
, node
);
266 static void qdev_applesmc_isa_reset(DeviceState
*dev
)
268 AppleSMCState
*s
= APPLE_SMC(dev
);
269 struct AppleSMCData
*d
, *next
;
271 /* Remove existing entries */
272 QLIST_FOREACH_SAFE(d
, &s
->data_def
, node
, next
) {
273 QLIST_REMOVE(d
, node
);
279 applesmc_add_key(s
, "REV ", 6, "\x01\x13\x0f\x00\x00\x03");
280 applesmc_add_key(s
, "OSK0", 32, s
->osk
);
281 applesmc_add_key(s
, "OSK1", 32, s
->osk
+ 32);
282 applesmc_add_key(s
, "NATJ", 1, "\0");
283 applesmc_add_key(s
, "MSSP", 1, "\0");
284 applesmc_add_key(s
, "MSSD", 1, "\0x3");
287 static const MemoryRegionOps applesmc_data_io_ops
= {
288 .write
= applesmc_io_data_write
,
289 .read
= applesmc_io_data_read
,
290 .endianness
= DEVICE_NATIVE_ENDIAN
,
292 .min_access_size
= 1,
293 .max_access_size
= 1,
297 static const MemoryRegionOps applesmc_cmd_io_ops
= {
298 .write
= applesmc_io_cmd_write
,
299 .read
= applesmc_io_cmd_read
,
300 .endianness
= DEVICE_NATIVE_ENDIAN
,
302 .min_access_size
= 1,
303 .max_access_size
= 1,
307 static const MemoryRegionOps applesmc_err_io_ops
= {
308 .write
= applesmc_io_err_write
,
309 .read
= applesmc_io_err_read
,
310 .endianness
= DEVICE_NATIVE_ENDIAN
,
312 .min_access_size
= 1,
313 .max_access_size
= 1,
317 static void applesmc_isa_realize(DeviceState
*dev
, Error
**errp
)
319 AppleSMCState
*s
= APPLE_SMC(dev
);
321 memory_region_init_io(&s
->io_data
, OBJECT(s
), &applesmc_data_io_ops
, s
,
323 isa_register_ioport(&s
->parent_obj
, &s
->io_data
,
324 s
->iobase
+ APPLESMC_DATA_PORT
);
326 memory_region_init_io(&s
->io_cmd
, OBJECT(s
), &applesmc_cmd_io_ops
, s
,
328 isa_register_ioport(&s
->parent_obj
, &s
->io_cmd
,
329 s
->iobase
+ APPLESMC_CMD_PORT
);
331 memory_region_init_io(&s
->io_err
, OBJECT(s
), &applesmc_err_io_ops
, s
,
333 isa_register_ioport(&s
->parent_obj
, &s
->io_err
,
334 s
->iobase
+ APPLESMC_ERR_PORT
);
336 if (!s
->osk
|| (strlen(s
->osk
) != 64)) {
337 warn_report("Using AppleSMC with invalid key");
338 s
->osk
= default_osk
;
341 QLIST_INIT(&s
->data_def
);
342 qdev_applesmc_isa_reset(dev
);
345 static Property applesmc_isa_properties
[] = {
346 DEFINE_PROP_UINT32(APPLESMC_PROP_IO_BASE
, AppleSMCState
, iobase
,
347 APPLESMC_DEFAULT_IOBASE
),
348 DEFINE_PROP_STRING("osk", AppleSMCState
, osk
),
349 DEFINE_PROP_END_OF_LIST(),
352 static void qdev_applesmc_class_init(ObjectClass
*klass
, void *data
)
354 DeviceClass
*dc
= DEVICE_CLASS(klass
);
356 dc
->realize
= applesmc_isa_realize
;
357 dc
->reset
= qdev_applesmc_isa_reset
;
358 device_class_set_props(dc
, applesmc_isa_properties
);
359 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
362 static const TypeInfo applesmc_isa_info
= {
363 .name
= TYPE_APPLE_SMC
,
364 .parent
= TYPE_ISA_DEVICE
,
365 .instance_size
= sizeof(AppleSMCState
),
366 .class_init
= qdev_applesmc_class_init
,
369 static void applesmc_register_types(void)
371 type_register_static(&applesmc_isa_info
);
374 type_init(applesmc_register_types
)