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 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 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
);
272 struct AppleSMCData
*d
, *next
;
274 /* Remove existing entries */
275 QLIST_FOREACH_SAFE(d
, &s
->data_def
, node
, next
) {
276 QLIST_REMOVE(d
, node
);
282 applesmc_add_key(s
, "REV ", 6, "\x01\x13\x0f\x00\x00\x03");
283 applesmc_add_key(s
, "OSK0", 32, s
->osk
);
284 applesmc_add_key(s
, "OSK1", 32, s
->osk
+ 32);
285 applesmc_add_key(s
, "NATJ", 1, "\0");
286 applesmc_add_key(s
, "MSSP", 1, "\0");
287 applesmc_add_key(s
, "MSSD", 1, "\0x3");
290 static const MemoryRegionOps applesmc_data_io_ops
= {
291 .write
= applesmc_io_data_write
,
292 .read
= applesmc_io_data_read
,
293 .endianness
= DEVICE_NATIVE_ENDIAN
,
295 .min_access_size
= 1,
296 .max_access_size
= 1,
300 static const MemoryRegionOps applesmc_cmd_io_ops
= {
301 .write
= applesmc_io_cmd_write
,
302 .read
= applesmc_io_cmd_read
,
303 .endianness
= DEVICE_NATIVE_ENDIAN
,
305 .min_access_size
= 1,
306 .max_access_size
= 1,
310 static const MemoryRegionOps applesmc_err_io_ops
= {
311 .write
= applesmc_io_err_write
,
312 .read
= applesmc_io_err_read
,
313 .endianness
= DEVICE_NATIVE_ENDIAN
,
315 .min_access_size
= 1,
316 .max_access_size
= 1,
320 static void applesmc_isa_realize(DeviceState
*dev
, Error
**errp
)
322 AppleSMCState
*s
= APPLE_SMC(dev
);
324 memory_region_init_io(&s
->io_data
, OBJECT(s
), &applesmc_data_io_ops
, s
,
326 isa_register_ioport(&s
->parent_obj
, &s
->io_data
,
327 s
->iobase
+ APPLESMC_DATA_PORT
);
329 memory_region_init_io(&s
->io_cmd
, OBJECT(s
), &applesmc_cmd_io_ops
, s
,
331 isa_register_ioport(&s
->parent_obj
, &s
->io_cmd
,
332 s
->iobase
+ APPLESMC_CMD_PORT
);
334 memory_region_init_io(&s
->io_err
, OBJECT(s
), &applesmc_err_io_ops
, s
,
336 isa_register_ioport(&s
->parent_obj
, &s
->io_err
,
337 s
->iobase
+ APPLESMC_ERR_PORT
);
339 if (!s
->osk
|| (strlen(s
->osk
) != 64)) {
340 warn_report("Using AppleSMC with invalid key");
341 s
->osk
= default_osk
;
344 QLIST_INIT(&s
->data_def
);
345 qdev_applesmc_isa_reset(dev
);
348 static Property applesmc_isa_properties
[] = {
349 DEFINE_PROP_UINT32(APPLESMC_PROP_IO_BASE
, AppleSMCState
, iobase
,
350 APPLESMC_DEFAULT_IOBASE
),
351 DEFINE_PROP_STRING("osk", AppleSMCState
, osk
),
352 DEFINE_PROP_END_OF_LIST(),
355 static void build_applesmc_aml(AcpiDevAmlIf
*adev
, Aml
*scope
)
358 AppleSMCState
*s
= APPLE_SMC(adev
);
359 uint32_t iobase
= s
->iobase
;
360 Aml
*dev
= aml_device("SMC");
362 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("APP0001")));
363 /* device present, functioning, decoding, not shown in UI */
364 aml_append(dev
, aml_name_decl("_STA", aml_int(0xB)));
365 crs
= aml_resource_template();
367 aml_io(AML_DECODE16
, iobase
, iobase
, 0x01, APPLESMC_MAX_DATA_LENGTH
)
369 aml_append(crs
, aml_irq_no_flags(6));
370 aml_append(dev
, aml_name_decl("_CRS", crs
));
371 aml_append(scope
, dev
);
374 static void qdev_applesmc_class_init(ObjectClass
*klass
, void *data
)
376 DeviceClass
*dc
= DEVICE_CLASS(klass
);
377 AcpiDevAmlIfClass
*adevc
= ACPI_DEV_AML_IF_CLASS(klass
);
379 dc
->realize
= applesmc_isa_realize
;
380 dc
->reset
= qdev_applesmc_isa_reset
;
381 device_class_set_props(dc
, applesmc_isa_properties
);
382 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
383 adevc
->build_dev_aml
= build_applesmc_aml
;
386 static const TypeInfo applesmc_isa_info
= {
387 .name
= TYPE_APPLE_SMC
,
388 .parent
= TYPE_ISA_DEVICE
,
389 .instance_size
= sizeof(AppleSMCState
),
390 .class_init
= qdev_applesmc_class_init
,
391 .interfaces
= (InterfaceInfo
[]) {
392 { TYPE_ACPI_DEV_AML_IF
},
397 static void applesmc_register_types(void)
399 type_register_static(&applesmc_isa_info
);
402 type_init(applesmc_register_types
)