blob: 7896812304d9f047731c4cfddab5907070c9afb7 [file] [log] [blame]
Alexander Graf1ddda5c2010-06-30 10:41:12 +02001/*
2 * Apple SMC controller
3 *
4 * Copyright (c) 2007 Alexander Graf
5 *
6 * Authors: Alexander Graf <agraf@suse.de>
7 * Susanne Graf <suse@csgraf.de>
8 *
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.
13 *
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.
18 *
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/>.
21 *
22 * *****************************************************************
23 *
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.
27 *
28 * This driver was mostly created by looking at the Linux AppleSMC driver
29 * implementation and does not support IRQ.
30 *
31 */
32
Peter Maydell0d1c9782016-01-26 18:17:17 +000033#include "qemu/osdep.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010034#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010035#include "hw/isa/isa.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010036#include "ui/console.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010037#include "qemu/timer.h"
Alexander Graf1ddda5c2010-06-30 10:41:12 +020038
39/* #define DEBUG_SMC */
40
41#define APPLESMC_DEFAULT_IOBASE 0x300
Alexander Graf1ddda5c2010-06-30 10:41:12 +020042
Gabriel L. Somlo36bcd032017-06-16 14:55:14 -040043enum {
44 APPLESMC_DATA_PORT = 0x00,
45 APPLESMC_CMD_PORT = 0x04,
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -040046 APPLESMC_ERR_PORT = 0x1e,
Gabriel L. Somlo36bcd032017-06-16 14:55:14 -040047 APPLESMC_NUM_PORTS = 0x20,
48};
49
50enum {
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,
55};
Alexander Graf1ddda5c2010-06-30 10:41:12 +020056
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -040057enum {
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,
63};
64
65enum {
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,
73};
74
Alexander Graf1ddda5c2010-06-30 10:41:12 +020075#ifdef DEBUG_SMC
76#define smc_debug(...) fprintf(stderr, "AppleSMC: " __VA_ARGS__)
77#else
Gabriel L. Somlo36bcd032017-06-16 14:55:14 -040078#define smc_debug(...) do { } while (0)
Alexander Graf1ddda5c2010-06-30 10:41:12 +020079#endif
80
81static char default_osk[64] = "This is a dummy key. Enter the real key "
82 "using the -osk parameter";
83
84struct AppleSMCData {
85 uint8_t len;
86 const char *key;
87 const char *data;
88 QLIST_ENTRY(AppleSMCData) node;
89};
90
Andreas Färber82407b62013-04-27 22:18:36 +020091#define APPLE_SMC(obj) OBJECT_CHECK(AppleSMCState, (obj), TYPE_APPLE_SMC)
92
93typedef struct AppleSMCState AppleSMCState;
94struct AppleSMCState {
95 ISADevice parent_obj;
96
Jan Kiszkae3914e32013-06-22 08:06:55 +020097 MemoryRegion io_data;
98 MemoryRegion io_cmd;
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -040099 MemoryRegion io_err;
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200100 uint32_t iobase;
101 uint8_t cmd;
102 uint8_t status;
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400103 uint8_t status_1e;
104 uint8_t last_ret;
Gabriel L. Somlo36bcd032017-06-16 14:55:14 -0400105 char key[4];
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200106 uint8_t read_pos;
107 uint8_t data_len;
108 uint8_t data_pos;
109 uint8_t data[255];
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200110 char *osk;
111 QLIST_HEAD(, AppleSMCData) data_def;
112};
113
Jan Kiszkae3914e32013-06-22 08:06:55 +0200114static void applesmc_io_cmd_write(void *opaque, hwaddr addr, uint64_t val,
115 unsigned size)
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200116{
Andreas Färber82407b62013-04-27 22:18:36 +0200117 AppleSMCState *s = opaque;
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400118 uint8_t status = s->status & 0x0f;
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200119
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400120 smc_debug("CMD received: 0x%02x\n", (uint8_t)val);
Gabriel L. Somlo36bcd032017-06-16 14:55:14 -0400121 switch (val) {
122 case APPLESMC_READ_CMD:
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400123 /* did last command run through OK? */
124 if (status == APPLESMC_ST_CMD_DONE || status == APPLESMC_ST_NEW_CMD) {
125 s->cmd = val;
126 s->status = APPLESMC_ST_NEW_CMD | APPLESMC_ST_ACK;
127 } else {
128 smc_debug("ERROR: previous command interrupted!\n");
129 s->status = APPLESMC_ST_NEW_CMD;
130 s->status_1e = APPLESMC_ST_1E_CMD_INTRUPTED;
131 }
Gabriel L. Somlo36bcd032017-06-16 14:55:14 -0400132 break;
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400133 default:
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;
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200137 }
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200138 s->read_pos = 0;
139 s->data_pos = 0;
140}
141
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400142static struct AppleSMCData *applesmc_find_key(AppleSMCState *s)
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200143{
144 struct AppleSMCData *d;
145
146 QLIST_FOREACH(d, &s->data_def, node) {
147 if (!memcmp(d->key, s->key, 4)) {
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400148 return d;
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200149 }
150 }
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400151 return NULL;
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200152}
153
Jan Kiszkae3914e32013-06-22 08:06:55 +0200154static void applesmc_io_data_write(void *opaque, hwaddr addr, uint64_t val,
155 unsigned size)
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200156{
Andreas Färber82407b62013-04-27 22:18:36 +0200157 AppleSMCState *s = opaque;
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400158 struct AppleSMCData *d;
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200159
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400160 smc_debug("DATA received: 0x%02x\n", (uint8_t)val);
Gabriel L. Somlo36bcd032017-06-16 14:55:14 -0400161 switch (s->cmd) {
162 case APPLESMC_READ_CMD:
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400163 if ((s->status & 0x0f) == APPLESMC_ST_CMD_DONE) {
164 break;
165 }
Gabriel L. Somlo36bcd032017-06-16 14:55:14 -0400166 if (s->read_pos < 4) {
167 s->key[s->read_pos] = val;
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400168 s->status = APPLESMC_ST_ACK;
Gabriel L. Somlo36bcd032017-06-16 14:55:14 -0400169 } else if (s->read_pos == 4) {
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400170 d = applesmc_find_key(s);
171 if (d != NULL) {
172 memcpy(s->data, d->data, d->len);
173 s->data_len = d->len;
174 s->data_pos = 0;
175 s->status = APPLESMC_ST_ACK | APPLESMC_ST_DATA_READY;
176 s->status_1e = APPLESMC_ST_CMD_DONE; /* clear on valid key */
177 } else {
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;
182 }
Gabriel L. Somlo36bcd032017-06-16 14:55:14 -0400183 }
184 s->read_pos++;
185 break;
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400186 default:
187 s->status = APPLESMC_ST_CMD_DONE;
188 s->status_1e = APPLESMC_ST_1E_STILL_BAD_CMD;
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200189 }
190}
191
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400192static void applesmc_io_err_write(void *opaque, hwaddr addr, uint64_t val,
193 unsigned size)
194{
195 smc_debug("ERR_CODE received: 0x%02x, ignoring!\n", (uint8_t)val);
196 /* NOTE: writing to the error port not supported! */
197}
198
Gabriel L. Somlo36bcd032017-06-16 14:55:14 -0400199static uint64_t applesmc_io_data_read(void *opaque, hwaddr addr, unsigned size)
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200200{
Andreas Färber82407b62013-04-27 22:18:36 +0200201 AppleSMCState *s = opaque;
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200202
Gabriel L. Somlo36bcd032017-06-16 14:55:14 -0400203 switch (s->cmd) {
204 case APPLESMC_READ_CMD:
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400205 if (!(s->status & APPLESMC_ST_DATA_READY)) {
206 break;
207 }
Gabriel L. Somlo36bcd032017-06-16 14:55:14 -0400208 if (s->data_pos < s->data_len) {
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400209 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);
Gabriel L. Somlo36bcd032017-06-16 14:55:14 -0400213 s->data_pos++;
214 if (s->data_pos == s->data_len) {
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400215 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],
218 s->data_len);
Gabriel L. Somlo36bcd032017-06-16 14:55:14 -0400219 } else {
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400220 s->status = APPLESMC_ST_ACK | APPLESMC_ST_DATA_READY;
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200221 }
Gabriel L. Somlo36bcd032017-06-16 14:55:14 -0400222 }
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400223 break;
224 default:
225 s->status = APPLESMC_ST_CMD_DONE;
226 s->status_1e = APPLESMC_ST_1E_STILL_BAD_CMD;
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200227 }
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400228 smc_debug("DATA sent: 0x%02x\n", s->last_ret);
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200229
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400230 return s->last_ret;
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200231}
232
Gabriel L. Somlo36bcd032017-06-16 14:55:14 -0400233static uint64_t applesmc_io_cmd_read(void *opaque, hwaddr addr, unsigned size)
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200234{
Andreas Färber82407b62013-04-27 22:18:36 +0200235 AppleSMCState *s = opaque;
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200236
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400237 smc_debug("CMD sent: 0x%02x\n", s->status);
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200238 return s->status;
239}
240
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400241static uint64_t applesmc_io_err_read(void *opaque, hwaddr addr, unsigned size)
242{
243 AppleSMCState *s = opaque;
244
245 /* NOTE: read does not clear the 1e status */
246 smc_debug("ERR_CODE sent: 0x%02x\n", s->status_1e);
247 return s->status_1e;
248}
249
Andreas Färber82407b62013-04-27 22:18:36 +0200250static void applesmc_add_key(AppleSMCState *s, const char *key,
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200251 int len, const char *data)
252{
253 struct AppleSMCData *def;
254
Anthony Liguori7267c092011-08-20 22:09:37 -0500255 def = g_malloc0(sizeof(struct AppleSMCData));
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200256 def->key = key;
257 def->len = len;
258 def->data = data;
259
260 QLIST_INSERT_HEAD(&s->data_def, def, node);
261}
262
263static void qdev_applesmc_isa_reset(DeviceState *dev)
264{
Andreas Färber82407b62013-04-27 22:18:36 +0200265 AppleSMCState *s = APPLE_SMC(dev);
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200266 struct AppleSMCData *d, *next;
267
268 /* Remove existing entries */
269 QLIST_FOREACH_SAFE(d, &s->data_def, node, next) {
270 QLIST_REMOVE(d, node);
271 }
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400272 s->status = 0x00;
273 s->status_1e = 0x00;
274 s->last_ret = 0x00;
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200275
René Rebe7f90fa72011-03-21 11:33:21 +0100276 applesmc_add_key(s, "REV ", 6, "\x01\x13\x0f\x00\x00\x03");
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200277 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");
282}
283
Jan Kiszkae3914e32013-06-22 08:06:55 +0200284static const MemoryRegionOps applesmc_data_io_ops = {
285 .write = applesmc_io_data_write,
286 .read = applesmc_io_data_read,
287 .endianness = DEVICE_NATIVE_ENDIAN,
288 .impl = {
289 .min_access_size = 1,
290 .max_access_size = 1,
291 },
292};
293
294static const MemoryRegionOps applesmc_cmd_io_ops = {
295 .write = applesmc_io_cmd_write,
296 .read = applesmc_io_cmd_read,
297 .endianness = DEVICE_NATIVE_ENDIAN,
298 .impl = {
299 .min_access_size = 1,
300 .max_access_size = 1,
301 },
302};
303
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400304static const MemoryRegionOps applesmc_err_io_ops = {
305 .write = applesmc_io_err_write,
306 .read = applesmc_io_err_read,
307 .endianness = DEVICE_NATIVE_ENDIAN,
308 .impl = {
309 .min_access_size = 1,
310 .max_access_size = 1,
311 },
312};
313
Andreas Färberdb895a12012-11-25 02:37:14 +0100314static void applesmc_isa_realize(DeviceState *dev, Error **errp)
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200315{
Andreas Färber82407b62013-04-27 22:18:36 +0200316 AppleSMCState *s = APPLE_SMC(dev);
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200317
Paolo Bonzini3c161542013-06-06 21:25:08 -0400318 memory_region_init_io(&s->io_data, OBJECT(s), &applesmc_data_io_ops, s,
Gabriel L. Somlo1b8274d2017-06-16 14:55:16 -0400319 "applesmc-data", 1);
Jan Kiszkae3914e32013-06-22 08:06:55 +0200320 isa_register_ioport(&s->parent_obj, &s->io_data,
321 s->iobase + APPLESMC_DATA_PORT);
322
Paolo Bonzini3c161542013-06-06 21:25:08 -0400323 memory_region_init_io(&s->io_cmd, OBJECT(s), &applesmc_cmd_io_ops, s,
Gabriel L. Somlo1b8274d2017-06-16 14:55:16 -0400324 "applesmc-cmd", 1);
Jan Kiszkae3914e32013-06-22 08:06:55 +0200325 isa_register_ioport(&s->parent_obj, &s->io_cmd,
326 s->iobase + APPLESMC_CMD_PORT);
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200327
Gabriel L. Somlo9e507d72017-06-16 14:55:15 -0400328 memory_region_init_io(&s->io_err, OBJECT(s), &applesmc_err_io_ops, s,
329 "applesmc-err", 1);
330 isa_register_ioport(&s->parent_obj, &s->io_err,
331 s->iobase + APPLESMC_ERR_PORT);
332
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200333 if (!s->osk || (strlen(s->osk) != 64)) {
334 fprintf(stderr, "WARNING: Using AppleSMC with invalid key\n");
335 s->osk = default_osk;
336 }
337
338 QLIST_INIT(&s->data_def);
Andreas Färberdb895a12012-11-25 02:37:14 +0100339 qdev_applesmc_isa_reset(dev);
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200340}
341
Anthony Liguori39bffca2011-12-07 21:34:16 -0600342static Property applesmc_isa_properties[] = {
Igor Mammedov1142e452015-02-20 18:22:11 +0000343 DEFINE_PROP_UINT32(APPLESMC_PROP_IO_BASE, AppleSMCState, iobase,
344 APPLESMC_DEFAULT_IOBASE),
Andreas Färber82407b62013-04-27 22:18:36 +0200345 DEFINE_PROP_STRING("osk", AppleSMCState, osk),
Anthony Liguori39bffca2011-12-07 21:34:16 -0600346 DEFINE_PROP_END_OF_LIST(),
347};
348
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600349static void qdev_applesmc_class_init(ObjectClass *klass, void *data)
350{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600351 DeviceClass *dc = DEVICE_CLASS(klass);
Andreas Färberdb895a12012-11-25 02:37:14 +0100352
353 dc->realize = applesmc_isa_realize;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600354 dc->reset = qdev_applesmc_isa_reset;
355 dc->props = applesmc_isa_properties;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300356 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600357}
358
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100359static const TypeInfo applesmc_isa_info = {
Andreas Färber82407b62013-04-27 22:18:36 +0200360 .name = TYPE_APPLE_SMC,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600361 .parent = TYPE_ISA_DEVICE,
Andreas Färber82407b62013-04-27 22:18:36 +0200362 .instance_size = sizeof(AppleSMCState),
Anthony Liguori39bffca2011-12-07 21:34:16 -0600363 .class_init = qdev_applesmc_class_init,
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200364};
365
Andreas Färber83f7d432012-02-09 15:20:55 +0100366static void applesmc_register_types(void)
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200367{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600368 type_register_static(&applesmc_isa_info);
Alexander Graf1ddda5c2010-06-30 10:41:12 +0200369}
370
Andreas Färber83f7d432012-02-09 15:20:55 +0100371type_init(applesmc_register_types)