Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 1 | /* |
| 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 Maydell | 0d1c978 | 2016-01-26 18:17:17 +0000 | [diff] [blame] | 33 | #include "qemu/osdep.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 34 | #include "hw/hw.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 35 | #include "hw/isa/isa.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 36 | #include "ui/console.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 37 | #include "qemu/timer.h" |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 38 | |
| 39 | /* #define DEBUG_SMC */ |
| 40 | |
| 41 | #define APPLESMC_DEFAULT_IOBASE 0x300 |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 42 | |
Gabriel L. Somlo | 36bcd03 | 2017-06-16 14:55:14 -0400 | [diff] [blame] | 43 | enum { |
| 44 | APPLESMC_DATA_PORT = 0x00, |
| 45 | APPLESMC_CMD_PORT = 0x04, |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 46 | APPLESMC_ERR_PORT = 0x1e, |
Gabriel L. Somlo | 36bcd03 | 2017-06-16 14:55:14 -0400 | [diff] [blame] | 47 | APPLESMC_NUM_PORTS = 0x20, |
| 48 | }; |
| 49 | |
| 50 | enum { |
| 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 Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 56 | |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 57 | enum { |
| 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 | |
| 65 | enum { |
| 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 Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 75 | #ifdef DEBUG_SMC |
| 76 | #define smc_debug(...) fprintf(stderr, "AppleSMC: " __VA_ARGS__) |
| 77 | #else |
Gabriel L. Somlo | 36bcd03 | 2017-06-16 14:55:14 -0400 | [diff] [blame] | 78 | #define smc_debug(...) do { } while (0) |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 79 | #endif |
| 80 | |
| 81 | static char default_osk[64] = "This is a dummy key. Enter the real key " |
| 82 | "using the -osk parameter"; |
| 83 | |
| 84 | struct AppleSMCData { |
| 85 | uint8_t len; |
| 86 | const char *key; |
| 87 | const char *data; |
| 88 | QLIST_ENTRY(AppleSMCData) node; |
| 89 | }; |
| 90 | |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 91 | #define APPLE_SMC(obj) OBJECT_CHECK(AppleSMCState, (obj), TYPE_APPLE_SMC) |
| 92 | |
| 93 | typedef struct AppleSMCState AppleSMCState; |
| 94 | struct AppleSMCState { |
| 95 | ISADevice parent_obj; |
| 96 | |
Jan Kiszka | e3914e3 | 2013-06-22 08:06:55 +0200 | [diff] [blame] | 97 | MemoryRegion io_data; |
| 98 | MemoryRegion io_cmd; |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 99 | MemoryRegion io_err; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 100 | uint32_t iobase; |
| 101 | uint8_t cmd; |
| 102 | uint8_t status; |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 103 | uint8_t status_1e; |
| 104 | uint8_t last_ret; |
Gabriel L. Somlo | 36bcd03 | 2017-06-16 14:55:14 -0400 | [diff] [blame] | 105 | char key[4]; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 106 | uint8_t read_pos; |
| 107 | uint8_t data_len; |
| 108 | uint8_t data_pos; |
| 109 | uint8_t data[255]; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 110 | char *osk; |
| 111 | QLIST_HEAD(, AppleSMCData) data_def; |
| 112 | }; |
| 113 | |
Jan Kiszka | e3914e3 | 2013-06-22 08:06:55 +0200 | [diff] [blame] | 114 | static void applesmc_io_cmd_write(void *opaque, hwaddr addr, uint64_t val, |
| 115 | unsigned size) |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 116 | { |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 117 | AppleSMCState *s = opaque; |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 118 | uint8_t status = s->status & 0x0f; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 119 | |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 120 | smc_debug("CMD received: 0x%02x\n", (uint8_t)val); |
Gabriel L. Somlo | 36bcd03 | 2017-06-16 14:55:14 -0400 | [diff] [blame] | 121 | switch (val) { |
| 122 | case APPLESMC_READ_CMD: |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 123 | /* 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. Somlo | 36bcd03 | 2017-06-16 14:55:14 -0400 | [diff] [blame] | 132 | break; |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 133 | 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 Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 137 | } |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 138 | s->read_pos = 0; |
| 139 | s->data_pos = 0; |
| 140 | } |
| 141 | |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 142 | static struct AppleSMCData *applesmc_find_key(AppleSMCState *s) |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 143 | { |
| 144 | struct AppleSMCData *d; |
| 145 | |
| 146 | QLIST_FOREACH(d, &s->data_def, node) { |
| 147 | if (!memcmp(d->key, s->key, 4)) { |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 148 | return d; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 149 | } |
| 150 | } |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 151 | return NULL; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Jan Kiszka | e3914e3 | 2013-06-22 08:06:55 +0200 | [diff] [blame] | 154 | static void applesmc_io_data_write(void *opaque, hwaddr addr, uint64_t val, |
| 155 | unsigned size) |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 156 | { |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 157 | AppleSMCState *s = opaque; |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 158 | struct AppleSMCData *d; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 159 | |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 160 | smc_debug("DATA received: 0x%02x\n", (uint8_t)val); |
Gabriel L. Somlo | 36bcd03 | 2017-06-16 14:55:14 -0400 | [diff] [blame] | 161 | switch (s->cmd) { |
| 162 | case APPLESMC_READ_CMD: |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 163 | if ((s->status & 0x0f) == APPLESMC_ST_CMD_DONE) { |
| 164 | break; |
| 165 | } |
Gabriel L. Somlo | 36bcd03 | 2017-06-16 14:55:14 -0400 | [diff] [blame] | 166 | if (s->read_pos < 4) { |
| 167 | s->key[s->read_pos] = val; |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 168 | s->status = APPLESMC_ST_ACK; |
Gabriel L. Somlo | 36bcd03 | 2017-06-16 14:55:14 -0400 | [diff] [blame] | 169 | } else if (s->read_pos == 4) { |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 170 | 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. Somlo | 36bcd03 | 2017-06-16 14:55:14 -0400 | [diff] [blame] | 183 | } |
| 184 | s->read_pos++; |
| 185 | break; |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 186 | default: |
| 187 | s->status = APPLESMC_ST_CMD_DONE; |
| 188 | s->status_1e = APPLESMC_ST_1E_STILL_BAD_CMD; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 192 | static 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. Somlo | 36bcd03 | 2017-06-16 14:55:14 -0400 | [diff] [blame] | 199 | static uint64_t applesmc_io_data_read(void *opaque, hwaddr addr, unsigned size) |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 200 | { |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 201 | AppleSMCState *s = opaque; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 202 | |
Gabriel L. Somlo | 36bcd03 | 2017-06-16 14:55:14 -0400 | [diff] [blame] | 203 | switch (s->cmd) { |
| 204 | case APPLESMC_READ_CMD: |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 205 | if (!(s->status & APPLESMC_ST_DATA_READY)) { |
| 206 | break; |
| 207 | } |
Gabriel L. Somlo | 36bcd03 | 2017-06-16 14:55:14 -0400 | [diff] [blame] | 208 | if (s->data_pos < s->data_len) { |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 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); |
Gabriel L. Somlo | 36bcd03 | 2017-06-16 14:55:14 -0400 | [diff] [blame] | 213 | s->data_pos++; |
| 214 | if (s->data_pos == s->data_len) { |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 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], |
| 218 | s->data_len); |
Gabriel L. Somlo | 36bcd03 | 2017-06-16 14:55:14 -0400 | [diff] [blame] | 219 | } else { |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 220 | s->status = APPLESMC_ST_ACK | APPLESMC_ST_DATA_READY; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 221 | } |
Gabriel L. Somlo | 36bcd03 | 2017-06-16 14:55:14 -0400 | [diff] [blame] | 222 | } |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 223 | break; |
| 224 | default: |
| 225 | s->status = APPLESMC_ST_CMD_DONE; |
| 226 | s->status_1e = APPLESMC_ST_1E_STILL_BAD_CMD; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 227 | } |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 228 | smc_debug("DATA sent: 0x%02x\n", s->last_ret); |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 229 | |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 230 | return s->last_ret; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Gabriel L. Somlo | 36bcd03 | 2017-06-16 14:55:14 -0400 | [diff] [blame] | 233 | static uint64_t applesmc_io_cmd_read(void *opaque, hwaddr addr, unsigned size) |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 234 | { |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 235 | AppleSMCState *s = opaque; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 236 | |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 237 | smc_debug("CMD sent: 0x%02x\n", s->status); |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 238 | return s->status; |
| 239 | } |
| 240 | |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 241 | static 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ärber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 250 | static void applesmc_add_key(AppleSMCState *s, const char *key, |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 251 | int len, const char *data) |
| 252 | { |
| 253 | struct AppleSMCData *def; |
| 254 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 255 | def = g_malloc0(sizeof(struct AppleSMCData)); |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 256 | def->key = key; |
| 257 | def->len = len; |
| 258 | def->data = data; |
| 259 | |
| 260 | QLIST_INSERT_HEAD(&s->data_def, def, node); |
| 261 | } |
| 262 | |
| 263 | static void qdev_applesmc_isa_reset(DeviceState *dev) |
| 264 | { |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 265 | AppleSMCState *s = APPLE_SMC(dev); |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 266 | 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. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 272 | s->status = 0x00; |
| 273 | s->status_1e = 0x00; |
| 274 | s->last_ret = 0x00; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 275 | |
René Rebe | 7f90fa7 | 2011-03-21 11:33:21 +0100 | [diff] [blame] | 276 | applesmc_add_key(s, "REV ", 6, "\x01\x13\x0f\x00\x00\x03"); |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 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"); |
| 282 | } |
| 283 | |
Jan Kiszka | e3914e3 | 2013-06-22 08:06:55 +0200 | [diff] [blame] | 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, |
| 288 | .impl = { |
| 289 | .min_access_size = 1, |
| 290 | .max_access_size = 1, |
| 291 | }, |
| 292 | }; |
| 293 | |
| 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, |
| 298 | .impl = { |
| 299 | .min_access_size = 1, |
| 300 | .max_access_size = 1, |
| 301 | }, |
| 302 | }; |
| 303 | |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 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, |
| 308 | .impl = { |
| 309 | .min_access_size = 1, |
| 310 | .max_access_size = 1, |
| 311 | }, |
| 312 | }; |
| 313 | |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 314 | static void applesmc_isa_realize(DeviceState *dev, Error **errp) |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 315 | { |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 316 | AppleSMCState *s = APPLE_SMC(dev); |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 317 | |
Paolo Bonzini | 3c16154 | 2013-06-06 21:25:08 -0400 | [diff] [blame] | 318 | memory_region_init_io(&s->io_data, OBJECT(s), &applesmc_data_io_ops, s, |
Gabriel L. Somlo | 1b8274d | 2017-06-16 14:55:16 -0400 | [diff] [blame] | 319 | "applesmc-data", 1); |
Jan Kiszka | e3914e3 | 2013-06-22 08:06:55 +0200 | [diff] [blame] | 320 | isa_register_ioport(&s->parent_obj, &s->io_data, |
| 321 | s->iobase + APPLESMC_DATA_PORT); |
| 322 | |
Paolo Bonzini | 3c16154 | 2013-06-06 21:25:08 -0400 | [diff] [blame] | 323 | memory_region_init_io(&s->io_cmd, OBJECT(s), &applesmc_cmd_io_ops, s, |
Gabriel L. Somlo | 1b8274d | 2017-06-16 14:55:16 -0400 | [diff] [blame] | 324 | "applesmc-cmd", 1); |
Jan Kiszka | e3914e3 | 2013-06-22 08:06:55 +0200 | [diff] [blame] | 325 | isa_register_ioport(&s->parent_obj, &s->io_cmd, |
| 326 | s->iobase + APPLESMC_CMD_PORT); |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 327 | |
Gabriel L. Somlo | 9e507d7 | 2017-06-16 14:55:15 -0400 | [diff] [blame] | 328 | 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 Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 333 | 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ärber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 339 | qdev_applesmc_isa_reset(dev); |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 340 | } |
| 341 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 342 | static Property applesmc_isa_properties[] = { |
Igor Mammedov | 1142e45 | 2015-02-20 18:22:11 +0000 | [diff] [blame] | 343 | DEFINE_PROP_UINT32(APPLESMC_PROP_IO_BASE, AppleSMCState, iobase, |
| 344 | APPLESMC_DEFAULT_IOBASE), |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 345 | DEFINE_PROP_STRING("osk", AppleSMCState, osk), |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 346 | DEFINE_PROP_END_OF_LIST(), |
| 347 | }; |
| 348 | |
Anthony Liguori | 8f04ee0 | 2011-12-04 11:52:49 -0600 | [diff] [blame] | 349 | static void qdev_applesmc_class_init(ObjectClass *klass, void *data) |
| 350 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 351 | DeviceClass *dc = DEVICE_CLASS(klass); |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 352 | |
| 353 | dc->realize = applesmc_isa_realize; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 354 | dc->reset = qdev_applesmc_isa_reset; |
| 355 | dc->props = applesmc_isa_properties; |
Marcel Apfelbaum | 125ee0e | 2013-07-29 17:17:45 +0300 | [diff] [blame] | 356 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
Anthony Liguori | 8f04ee0 | 2011-12-04 11:52:49 -0600 | [diff] [blame] | 357 | } |
| 358 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 359 | static const TypeInfo applesmc_isa_info = { |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 360 | .name = TYPE_APPLE_SMC, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 361 | .parent = TYPE_ISA_DEVICE, |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 362 | .instance_size = sizeof(AppleSMCState), |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 363 | .class_init = qdev_applesmc_class_init, |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 364 | }; |
| 365 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 366 | static void applesmc_register_types(void) |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 367 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 368 | type_register_static(&applesmc_isa_info); |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 371 | type_init(applesmc_register_types) |