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.1 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/error-report.h"
38 #include "qemu/module.h"
39 #include "qemu/timer.h"
40 #include "qom/object.h"
41 #include "hw/acpi/acpi_aml_interface.h"
43 /* #define DEBUG_SMC */
45 #define APPLESMC_DEFAULT_IOBASE 0x300
46 #define TYPE_APPLE_SMC "isa-applesmc"
47 #define APPLESMC_MAX_DATA_LENGTH 32
48 #define APPLESMC_PROP_IO_BASE "iobase"
51 APPLESMC_DATA_PORT
= 0x00,
52 APPLESMC_CMD_PORT
= 0x04,
53 APPLESMC_ERR_PORT
= 0x1e,
54 APPLESMC_NUM_PORTS
= 0x20,
58 APPLESMC_READ_CMD
= 0x10,
59 APPLESMC_WRITE_CMD
= 0x11,
60 APPLESMC_GET_KEY_BY_INDEX_CMD
= 0x12,
61 APPLESMC_GET_KEY_TYPE_CMD
= 0x13,
65 APPLESMC_ST_CMD_DONE
= 0x00,
66 APPLESMC_ST_DATA_READY
= 0x01,
67 APPLESMC_ST_BUSY
= 0x02,
68 APPLESMC_ST_ACK
= 0x04,
69 APPLESMC_ST_NEW_CMD
= 0x08,
73 APPLESMC_ST_1E_CMD_INTRUPTED
= 0x80,
74 APPLESMC_ST_1E_STILL_BAD_CMD
= 0x81,
75 APPLESMC_ST_1E_BAD_CMD
= 0x82,
76 APPLESMC_ST_1E_NOEXIST
= 0x84,
77 APPLESMC_ST_1E_WRITEONLY
= 0x85,
78 APPLESMC_ST_1E_READONLY
= 0x86,
79 APPLESMC_ST_1E_BAD_INDEX
= 0xb8,
83 #define smc_debug(...) fprintf(stderr, "AppleSMC: " __VA_ARGS__)
85 #define smc_debug(...) do { } while (0)
88 static char default_osk
[64] = "This is a dummy key. Enter the real key "
89 "using the -osk parameter";
95 QLIST_ENTRY(AppleSMCData
) node
;
98 OBJECT_DECLARE_SIMPLE_TYPE(AppleSMCState
, APPLE_SMC
)
100 struct AppleSMCState
{
101 ISADevice parent_obj
;
103 MemoryRegion io_data
;
117 QLIST_HEAD(, AppleSMCData
) data_def
;
120 static void applesmc_io_cmd_write(void *opaque
, hwaddr addr
, uint64_t val
,
123 AppleSMCState
*s
= opaque
;
124 uint8_t status
= s
->status
& 0x0f;
126 smc_debug("CMD received: 0x%02x\n", (uint8_t)val
);
128 case APPLESMC_READ_CMD
:
129 /* did last command run through OK? */
130 if (status
== APPLESMC_ST_CMD_DONE
|| status
== APPLESMC_ST_NEW_CMD
) {
132 s
->status
= APPLESMC_ST_NEW_CMD
| APPLESMC_ST_ACK
;
134 smc_debug("ERROR: previous command interrupted!\n");
135 s
->status
= APPLESMC_ST_NEW_CMD
;
136 s
->status_1e
= APPLESMC_ST_1E_CMD_INTRUPTED
;
140 smc_debug("UNEXPECTED CMD 0x%02x\n", (uint8_t)val
);
141 s
->status
= APPLESMC_ST_NEW_CMD
;
142 s
->status_1e
= APPLESMC_ST_1E_BAD_CMD
;
148 static const struct AppleSMCData
*applesmc_find_key(AppleSMCState
*s
)
150 struct AppleSMCData
*d
;
152 QLIST_FOREACH(d
, &s
->data_def
, node
) {
153 if (!memcmp(d
->key
, s
->key
, 4)) {
160 static void applesmc_io_data_write(void *opaque
, hwaddr addr
, uint64_t val
,
163 AppleSMCState
*s
= opaque
;
164 const struct AppleSMCData
*d
;
166 smc_debug("DATA received: 0x%02x\n", (uint8_t)val
);
168 case APPLESMC_READ_CMD
:
169 if ((s
->status
& 0x0f) == APPLESMC_ST_CMD_DONE
) {
172 if (s
->read_pos
< 4) {
173 s
->key
[s
->read_pos
] = val
;
174 s
->status
= APPLESMC_ST_ACK
;
175 } else if (s
->read_pos
== 4) {
176 d
= applesmc_find_key(s
);
178 memcpy(s
->data
, d
->data
, d
->len
);
179 s
->data_len
= d
->len
;
181 s
->status
= APPLESMC_ST_ACK
| APPLESMC_ST_DATA_READY
;
182 s
->status_1e
= APPLESMC_ST_CMD_DONE
; /* clear on valid key */
184 smc_debug("READ_CMD: key '%c%c%c%c' not found!\n",
185 s
->key
[0], s
->key
[1], s
->key
[2], s
->key
[3]);
186 s
->status
= APPLESMC_ST_CMD_DONE
;
187 s
->status_1e
= APPLESMC_ST_1E_NOEXIST
;
193 s
->status
= APPLESMC_ST_CMD_DONE
;
194 s
->status_1e
= APPLESMC_ST_1E_STILL_BAD_CMD
;
198 static void applesmc_io_err_write(void *opaque
, hwaddr addr
, uint64_t val
,
201 smc_debug("ERR_CODE received: 0x%02x, ignoring!\n", (uint8_t)val
);
202 /* NOTE: writing to the error port not supported! */
205 static uint64_t applesmc_io_data_read(void *opaque
, hwaddr addr
, unsigned size
)
207 AppleSMCState
*s
= opaque
;
210 case APPLESMC_READ_CMD
:
211 if (!(s
->status
& APPLESMC_ST_DATA_READY
)) {
214 if (s
->data_pos
< s
->data_len
) {
215 s
->last_ret
= s
->data
[s
->data_pos
];
216 smc_debug("READ '%c%c%c%c'[%d] = %02x\n",
217 s
->key
[0], s
->key
[1], s
->key
[2], s
->key
[3],
218 s
->data_pos
, s
->last_ret
);
220 if (s
->data_pos
== s
->data_len
) {
221 s
->status
= APPLESMC_ST_CMD_DONE
;
222 smc_debug("READ '%c%c%c%c' Len=%d complete!\n",
223 s
->key
[0], s
->key
[1], s
->key
[2], s
->key
[3],
226 s
->status
= APPLESMC_ST_ACK
| APPLESMC_ST_DATA_READY
;
231 s
->status
= APPLESMC_ST_CMD_DONE
;
232 s
->status_1e
= APPLESMC_ST_1E_STILL_BAD_CMD
;
234 smc_debug("DATA sent: 0x%02x\n", s
->last_ret
);
239 static uint64_t applesmc_io_cmd_read(void *opaque
, hwaddr addr
, unsigned size
)
241 AppleSMCState
*s
= opaque
;
243 smc_debug("CMD sent: 0x%02x\n", s
->status
);
247 static uint64_t applesmc_io_err_read(void *opaque
, hwaddr addr
, unsigned size
)
249 AppleSMCState
*s
= opaque
;
251 /* NOTE: read does not clear the 1e status */
252 smc_debug("ERR_CODE sent: 0x%02x\n", s
->status_1e
);
256 static void applesmc_add_key(AppleSMCState
*s
, const char *key
,
257 int len
, const char *data
)
259 struct AppleSMCData
*def
;
261 def
= g_new0(struct AppleSMCData
, 1);
266 QLIST_INSERT_HEAD(&s
->data_def
, def
, node
);
269 static void qdev_applesmc_isa_reset(DeviceState
*dev
)
271 AppleSMCState
*s
= APPLE_SMC(dev
);
278 static const MemoryRegionOps applesmc_data_io_ops
= {
279 .write
= applesmc_io_data_write
,
280 .read
= applesmc_io_data_read
,
281 .endianness
= DEVICE_NATIVE_ENDIAN
,
283 .min_access_size
= 1,
284 .max_access_size
= 1,
288 static const MemoryRegionOps applesmc_cmd_io_ops
= {
289 .write
= applesmc_io_cmd_write
,
290 .read
= applesmc_io_cmd_read
,
291 .endianness
= DEVICE_NATIVE_ENDIAN
,
293 .min_access_size
= 1,
294 .max_access_size
= 1,
298 static const MemoryRegionOps applesmc_err_io_ops
= {
299 .write
= applesmc_io_err_write
,
300 .read
= applesmc_io_err_read
,
301 .endianness
= DEVICE_NATIVE_ENDIAN
,
303 .min_access_size
= 1,
304 .max_access_size
= 1,
308 static void applesmc_isa_realize(DeviceState
*dev
, Error
**errp
)
310 AppleSMCState
*s
= APPLE_SMC(dev
);
312 memory_region_init_io(&s
->io_data
, OBJECT(s
), &applesmc_data_io_ops
, s
,
314 isa_register_ioport(&s
->parent_obj
, &s
->io_data
,
315 s
->iobase
+ APPLESMC_DATA_PORT
);
317 memory_region_init_io(&s
->io_cmd
, OBJECT(s
), &applesmc_cmd_io_ops
, s
,
319 isa_register_ioport(&s
->parent_obj
, &s
->io_cmd
,
320 s
->iobase
+ APPLESMC_CMD_PORT
);
322 memory_region_init_io(&s
->io_err
, OBJECT(s
), &applesmc_err_io_ops
, s
,
324 isa_register_ioport(&s
->parent_obj
, &s
->io_err
,
325 s
->iobase
+ APPLESMC_ERR_PORT
);
327 if (!s
->osk
|| (strlen(s
->osk
) != 64)) {
328 warn_report("Using AppleSMC with invalid key");
329 s
->osk
= default_osk
;
332 QLIST_INIT(&s
->data_def
);
333 applesmc_add_key(s
, "REV ", 6, "\x01\x13\x0f\x00\x00\x03");
334 applesmc_add_key(s
, "OSK0", 32, s
->osk
);
335 applesmc_add_key(s
, "OSK1", 32, s
->osk
+ 32);
336 applesmc_add_key(s
, "NATJ", 1, "\0");
337 applesmc_add_key(s
, "MSSP", 1, "\0");
338 applesmc_add_key(s
, "MSSD", 1, "\0x3");
341 static void applesmc_unrealize(DeviceState
*dev
)
343 AppleSMCState
*s
= APPLE_SMC(dev
);
344 struct AppleSMCData
*d
, *next
;
346 /* Remove existing entries */
347 QLIST_FOREACH_SAFE(d
, &s
->data_def
, node
, next
) {
348 QLIST_REMOVE(d
, node
);
353 static Property applesmc_isa_properties
[] = {
354 DEFINE_PROP_UINT32(APPLESMC_PROP_IO_BASE
, AppleSMCState
, iobase
,
355 APPLESMC_DEFAULT_IOBASE
),
356 DEFINE_PROP_STRING("osk", AppleSMCState
, osk
),
357 DEFINE_PROP_END_OF_LIST(),
360 static void build_applesmc_aml(AcpiDevAmlIf
*adev
, Aml
*scope
)
363 AppleSMCState
*s
= APPLE_SMC(adev
);
364 uint32_t iobase
= s
->iobase
;
365 Aml
*dev
= aml_device("SMC");
367 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("APP0001")));
368 /* device present, functioning, decoding, not shown in UI */
369 aml_append(dev
, aml_name_decl("_STA", aml_int(0xB)));
370 crs
= aml_resource_template();
372 aml_io(AML_DECODE16
, iobase
, iobase
, 0x01, APPLESMC_MAX_DATA_LENGTH
)
374 aml_append(crs
, aml_irq_no_flags(6));
375 aml_append(dev
, aml_name_decl("_CRS", crs
));
376 aml_append(scope
, dev
);
379 static void qdev_applesmc_class_init(ObjectClass
*klass
, void *data
)
381 DeviceClass
*dc
= DEVICE_CLASS(klass
);
382 AcpiDevAmlIfClass
*adevc
= ACPI_DEV_AML_IF_CLASS(klass
);
384 dc
->realize
= applesmc_isa_realize
;
385 dc
->unrealize
= applesmc_unrealize
;
386 device_class_set_legacy_reset(dc
, qdev_applesmc_isa_reset
);
387 device_class_set_props(dc
, applesmc_isa_properties
);
388 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
389 adevc
->build_dev_aml
= build_applesmc_aml
;
392 static const TypeInfo applesmc_isa_info
= {
393 .name
= TYPE_APPLE_SMC
,
394 .parent
= TYPE_ISA_DEVICE
,
395 .instance_size
= sizeof(AppleSMCState
),
396 .class_init
= qdev_applesmc_class_init
,
397 .interfaces
= (InterfaceInfo
[]) {
398 { TYPE_ACPI_DEV_AML_IF
},
403 static void applesmc_register_types(void)
405 type_register_static(&applesmc_isa_info
);
408 type_init(applesmc_register_types
)