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"
35 #include "hw/isa/isa.h"
36 #include "ui/console.h"
37 #include "qemu/timer.h"
39 /* #define DEBUG_SMC */
41 #define APPLESMC_DEFAULT_IOBASE 0x300
44 APPLESMC_DATA_PORT
= 0x00,
45 APPLESMC_CMD_PORT
= 0x04,
46 APPLESMC_ERR_PORT
= 0x1e,
47 APPLESMC_NUM_PORTS
= 0x20,
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,
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,
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,
76 #define smc_debug(...) fprintf(stderr, "AppleSMC: " __VA_ARGS__)
78 #define smc_debug(...) do { } while (0)
81 static char default_osk
[64] = "This is a dummy key. Enter the real key "
82 "using the -osk parameter";
88 QLIST_ENTRY(AppleSMCData
) node
;
91 #define APPLE_SMC(obj) OBJECT_CHECK(AppleSMCState, (obj), TYPE_APPLE_SMC)
93 typedef struct AppleSMCState AppleSMCState
;
94 struct AppleSMCState
{
111 QLIST_HEAD(, AppleSMCData
) data_def
;
114 static void applesmc_io_cmd_write(void *opaque
, hwaddr addr
, uint64_t val
,
117 AppleSMCState
*s
= opaque
;
118 uint8_t status
= s
->status
& 0x0f;
120 smc_debug("CMD received: 0x%02x\n", (uint8_t)val
);
122 case APPLESMC_READ_CMD
:
123 /* did last command run through OK? */
124 if (status
== APPLESMC_ST_CMD_DONE
|| status
== APPLESMC_ST_NEW_CMD
) {
126 s
->status
= APPLESMC_ST_NEW_CMD
| APPLESMC_ST_ACK
;
128 smc_debug("ERROR: previous command interrupted!\n");
129 s
->status
= APPLESMC_ST_NEW_CMD
;
130 s
->status_1e
= APPLESMC_ST_1E_CMD_INTRUPTED
;
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
;
142 static struct AppleSMCData
*applesmc_find_key(AppleSMCState
*s
)
144 struct AppleSMCData
*d
;
146 QLIST_FOREACH(d
, &s
->data_def
, node
) {
147 if (!memcmp(d
->key
, s
->key
, 4)) {
154 static void applesmc_io_data_write(void *opaque
, hwaddr addr
, uint64_t val
,
157 AppleSMCState
*s
= opaque
;
158 struct AppleSMCData
*d
;
160 smc_debug("DATA received: 0x%02x\n", (uint8_t)val
);
162 case APPLESMC_READ_CMD
:
163 if ((s
->status
& 0x0f) == APPLESMC_ST_CMD_DONE
) {
166 if (s
->read_pos
< 4) {
167 s
->key
[s
->read_pos
] = val
;
168 s
->status
= APPLESMC_ST_ACK
;
169 } else if (s
->read_pos
== 4) {
170 d
= applesmc_find_key(s
);
172 memcpy(s
->data
, d
->data
, d
->len
);
173 s
->data_len
= d
->len
;
175 s
->status
= APPLESMC_ST_ACK
| APPLESMC_ST_DATA_READY
;
176 s
->status_1e
= APPLESMC_ST_CMD_DONE
; /* clear on valid key */
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
;
187 s
->status
= APPLESMC_ST_CMD_DONE
;
188 s
->status_1e
= APPLESMC_ST_1E_STILL_BAD_CMD
;
192 static void applesmc_io_err_write(void *opaque
, hwaddr addr
, uint64_t val
,
195 smc_debug("ERR_CODE received: 0x%02x, ignoring!\n", (uint8_t)val
);
196 /* NOTE: writing to the error port not supported! */
199 static uint64_t applesmc_io_data_read(void *opaque
, hwaddr addr
, unsigned size
)
201 AppleSMCState
*s
= opaque
;
204 case APPLESMC_READ_CMD
:
205 if (!(s
->status
& APPLESMC_ST_DATA_READY
)) {
208 if (s
->data_pos
< s
->data_len
) {
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
);
214 if (s
->data_pos
== s
->data_len
) {
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],
220 s
->status
= APPLESMC_ST_ACK
| APPLESMC_ST_DATA_READY
;
225 s
->status
= APPLESMC_ST_CMD_DONE
;
226 s
->status_1e
= APPLESMC_ST_1E_STILL_BAD_CMD
;
228 smc_debug("DATA sent: 0x%02x\n", s
->last_ret
);
233 static uint64_t applesmc_io_cmd_read(void *opaque
, hwaddr addr
, unsigned size
)
235 AppleSMCState
*s
= opaque
;
237 smc_debug("CMD sent: 0x%02x\n", s
->status
);
241 static uint64_t applesmc_io_err_read(void *opaque
, hwaddr addr
, unsigned size
)
243 AppleSMCState
*s
= opaque
;
245 /* NOTE: read does not clear the 1e status */
246 smc_debug("ERR_CODE sent: 0x%02x\n", s
->status_1e
);
250 static void applesmc_add_key(AppleSMCState
*s
, const char *key
,
251 int len
, const char *data
)
253 struct AppleSMCData
*def
;
255 def
= g_malloc0(sizeof(struct AppleSMCData
));
260 QLIST_INSERT_HEAD(&s
->data_def
, def
, node
);
263 static void qdev_applesmc_isa_reset(DeviceState
*dev
)
265 AppleSMCState
*s
= APPLE_SMC(dev
);
266 struct AppleSMCData
*d
, *next
;
268 /* Remove existing entries */
269 QLIST_FOREACH_SAFE(d
, &s
->data_def
, node
, next
) {
270 QLIST_REMOVE(d
, node
);
276 applesmc_add_key(s
, "REV ", 6, "\x01\x13\x0f\x00\x00\x03");
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");
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
,
289 .min_access_size
= 1,
290 .max_access_size
= 1,
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
,
299 .min_access_size
= 1,
300 .max_access_size
= 1,
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
,
309 .min_access_size
= 1,
310 .max_access_size
= 1,
314 static void applesmc_isa_realize(DeviceState
*dev
, Error
**errp
)
316 AppleSMCState
*s
= APPLE_SMC(dev
);
318 memory_region_init_io(&s
->io_data
, OBJECT(s
), &applesmc_data_io_ops
, s
,
320 isa_register_ioport(&s
->parent_obj
, &s
->io_data
,
321 s
->iobase
+ APPLESMC_DATA_PORT
);
323 memory_region_init_io(&s
->io_cmd
, OBJECT(s
), &applesmc_cmd_io_ops
, s
,
325 isa_register_ioport(&s
->parent_obj
, &s
->io_cmd
,
326 s
->iobase
+ APPLESMC_CMD_PORT
);
328 memory_region_init_io(&s
->io_err
, OBJECT(s
), &applesmc_err_io_ops
, s
,
330 isa_register_ioport(&s
->parent_obj
, &s
->io_err
,
331 s
->iobase
+ APPLESMC_ERR_PORT
);
333 if (!s
->osk
|| (strlen(s
->osk
) != 64)) {
334 warn_report("Using AppleSMC with invalid key");
335 s
->osk
= default_osk
;
338 QLIST_INIT(&s
->data_def
);
339 qdev_applesmc_isa_reset(dev
);
342 static Property applesmc_isa_properties
[] = {
343 DEFINE_PROP_UINT32(APPLESMC_PROP_IO_BASE
, AppleSMCState
, iobase
,
344 APPLESMC_DEFAULT_IOBASE
),
345 DEFINE_PROP_STRING("osk", AppleSMCState
, osk
),
346 DEFINE_PROP_END_OF_LIST(),
349 static void qdev_applesmc_class_init(ObjectClass
*klass
, void *data
)
351 DeviceClass
*dc
= DEVICE_CLASS(klass
);
353 dc
->realize
= applesmc_isa_realize
;
354 dc
->reset
= qdev_applesmc_isa_reset
;
355 dc
->props
= applesmc_isa_properties
;
356 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
359 static const TypeInfo applesmc_isa_info
= {
360 .name
= TYPE_APPLE_SMC
,
361 .parent
= TYPE_ISA_DEVICE
,
362 .instance_size
= sizeof(AppleSMCState
),
363 .class_init
= qdev_applesmc_class_init
,
366 static void applesmc_register_types(void)
368 type_register_static(&applesmc_isa_info
);
371 type_init(applesmc_register_types
)