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.
34 #include "hw/isa/isa.h"
35 #include "ui/console.h"
36 #include "qemu/timer.h"
38 /* #define DEBUG_SMC */
40 #define APPLESMC_DEFAULT_IOBASE 0x300
41 /* data port used by Apple SMC */
42 #define APPLESMC_DATA_PORT 0x0
43 /* command/status port used by Apple SMC */
44 #define APPLESMC_CMD_PORT 0x4
45 #define APPLESMC_NR_PORTS 32
46 #define APPLESMC_MAX_DATA_LENGTH 32
48 #define APPLESMC_READ_CMD 0x10
49 #define APPLESMC_WRITE_CMD 0x11
50 #define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
51 #define APPLESMC_GET_KEY_TYPE_CMD 0x13
54 #define smc_debug(...) fprintf(stderr, "AppleSMC: " __VA_ARGS__)
56 #define smc_debug(...) do { } while(0)
59 static char default_osk
[64] = "This is a dummy key. Enter the real key "
60 "using the -osk parameter";
66 QLIST_ENTRY(AppleSMCData
) node
;
69 #define TYPE_APPLE_SMC "isa-applesmc"
70 #define APPLE_SMC(obj) OBJECT_CHECK(AppleSMCState, (obj), TYPE_APPLE_SMC)
72 typedef struct AppleSMCState AppleSMCState
;
73 struct AppleSMCState
{
88 QLIST_HEAD(, AppleSMCData
) data_def
;
91 static void applesmc_io_cmd_write(void *opaque
, hwaddr addr
, uint64_t val
,
94 AppleSMCState
*s
= opaque
;
96 smc_debug("CMD Write B: %#x = %#x\n", addr
, val
);
98 case APPLESMC_READ_CMD
:
107 static void applesmc_fill_data(AppleSMCState
*s
)
109 struct AppleSMCData
*d
;
111 QLIST_FOREACH(d
, &s
->data_def
, node
) {
112 if (!memcmp(d
->key
, s
->key
, 4)) {
113 smc_debug("Key matched (%s Len=%d Data=%s)\n", d
->key
,
115 memcpy(s
->data
, d
->data
, d
->len
);
121 static void applesmc_io_data_write(void *opaque
, hwaddr addr
, uint64_t val
,
124 AppleSMCState
*s
= opaque
;
126 smc_debug("DATA Write B: %#x = %#x\n", addr
, val
);
128 case APPLESMC_READ_CMD
:
129 if(s
->read_pos
< 4) {
130 s
->key
[s
->read_pos
] = val
;
132 } else if(s
->read_pos
== 4) {
136 smc_debug("Key = %c%c%c%c Len = %d\n", s
->key
[0],
137 s
->key
[1], s
->key
[2], s
->key
[3], val
);
138 applesmc_fill_data(s
);
145 static uint64_t applesmc_io_data_read(void *opaque
, hwaddr addr1
,
148 AppleSMCState
*s
= opaque
;
152 case APPLESMC_READ_CMD
:
153 if(s
->data_pos
< s
->data_len
) {
154 retval
= s
->data
[s
->data_pos
];
155 smc_debug("READ_DATA[%d] = %#hhx\n", s
->data_pos
,
158 if(s
->data_pos
== s
->data_len
) {
165 smc_debug("DATA Read b: %#x = %#x\n", addr1
, retval
);
170 static uint64_t applesmc_io_cmd_read(void *opaque
, hwaddr addr1
, unsigned size
)
172 AppleSMCState
*s
= opaque
;
174 smc_debug("CMD Read B: %#x\n", addr1
);
178 static void applesmc_add_key(AppleSMCState
*s
, const char *key
,
179 int len
, const char *data
)
181 struct AppleSMCData
*def
;
183 def
= g_malloc0(sizeof(struct AppleSMCData
));
188 QLIST_INSERT_HEAD(&s
->data_def
, def
, node
);
191 static void qdev_applesmc_isa_reset(DeviceState
*dev
)
193 AppleSMCState
*s
= APPLE_SMC(dev
);
194 struct AppleSMCData
*d
, *next
;
196 /* Remove existing entries */
197 QLIST_FOREACH_SAFE(d
, &s
->data_def
, node
, next
) {
198 QLIST_REMOVE(d
, node
);
201 applesmc_add_key(s
, "REV ", 6, "\x01\x13\x0f\x00\x00\x03");
202 applesmc_add_key(s
, "OSK0", 32, s
->osk
);
203 applesmc_add_key(s
, "OSK1", 32, s
->osk
+ 32);
204 applesmc_add_key(s
, "NATJ", 1, "\0");
205 applesmc_add_key(s
, "MSSP", 1, "\0");
206 applesmc_add_key(s
, "MSSD", 1, "\0x3");
209 static const MemoryRegionOps applesmc_data_io_ops
= {
210 .write
= applesmc_io_data_write
,
211 .read
= applesmc_io_data_read
,
212 .endianness
= DEVICE_NATIVE_ENDIAN
,
214 .min_access_size
= 1,
215 .max_access_size
= 1,
219 static const MemoryRegionOps applesmc_cmd_io_ops
= {
220 .write
= applesmc_io_cmd_write
,
221 .read
= applesmc_io_cmd_read
,
222 .endianness
= DEVICE_NATIVE_ENDIAN
,
224 .min_access_size
= 1,
225 .max_access_size
= 1,
229 static void applesmc_isa_realize(DeviceState
*dev
, Error
**errp
)
231 AppleSMCState
*s
= APPLE_SMC(dev
);
233 memory_region_init_io(&s
->io_data
, NULL
, &applesmc_data_io_ops
, s
,
235 isa_register_ioport(&s
->parent_obj
, &s
->io_data
,
236 s
->iobase
+ APPLESMC_DATA_PORT
);
238 memory_region_init_io(&s
->io_cmd
, NULL
, &applesmc_cmd_io_ops
, s
,
240 isa_register_ioport(&s
->parent_obj
, &s
->io_cmd
,
241 s
->iobase
+ APPLESMC_CMD_PORT
);
243 if (!s
->osk
|| (strlen(s
->osk
) != 64)) {
244 fprintf(stderr
, "WARNING: Using AppleSMC with invalid key\n");
245 s
->osk
= default_osk
;
248 QLIST_INIT(&s
->data_def
);
249 qdev_applesmc_isa_reset(dev
);
252 static Property applesmc_isa_properties
[] = {
253 DEFINE_PROP_HEX32("iobase", AppleSMCState
, iobase
,
254 APPLESMC_DEFAULT_IOBASE
),
255 DEFINE_PROP_STRING("osk", AppleSMCState
, osk
),
256 DEFINE_PROP_END_OF_LIST(),
259 static void qdev_applesmc_class_init(ObjectClass
*klass
, void *data
)
261 DeviceClass
*dc
= DEVICE_CLASS(klass
);
263 dc
->realize
= applesmc_isa_realize
;
264 dc
->reset
= qdev_applesmc_isa_reset
;
265 dc
->props
= applesmc_isa_properties
;
268 static const TypeInfo applesmc_isa_info
= {
269 .name
= TYPE_APPLE_SMC
,
270 .parent
= TYPE_ISA_DEVICE
,
271 .instance_size
= sizeof(AppleSMCState
),
272 .class_init
= qdev_applesmc_class_init
,
275 static void applesmc_register_types(void)
277 type_register_static(&applesmc_isa_info
);
280 type_init(applesmc_register_types
)