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 | |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 33 | #include "hw/hw.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 34 | #include "hw/isa/isa.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 35 | #include "ui/console.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 36 | #include "qemu/timer.h" |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 37 | |
| 38 | /* #define DEBUG_SMC */ |
| 39 | |
| 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 |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 46 | |
| 47 | #define APPLESMC_READ_CMD 0x10 |
| 48 | #define APPLESMC_WRITE_CMD 0x11 |
| 49 | #define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12 |
| 50 | #define APPLESMC_GET_KEY_TYPE_CMD 0x13 |
| 51 | |
| 52 | #ifdef DEBUG_SMC |
| 53 | #define smc_debug(...) fprintf(stderr, "AppleSMC: " __VA_ARGS__) |
| 54 | #else |
| 55 | #define smc_debug(...) do { } while(0) |
| 56 | #endif |
| 57 | |
| 58 | static char default_osk[64] = "This is a dummy key. Enter the real key " |
| 59 | "using the -osk parameter"; |
| 60 | |
| 61 | struct AppleSMCData { |
| 62 | uint8_t len; |
| 63 | const char *key; |
| 64 | const char *data; |
| 65 | QLIST_ENTRY(AppleSMCData) node; |
| 66 | }; |
| 67 | |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 68 | #define APPLE_SMC(obj) OBJECT_CHECK(AppleSMCState, (obj), TYPE_APPLE_SMC) |
| 69 | |
| 70 | typedef struct AppleSMCState AppleSMCState; |
| 71 | struct AppleSMCState { |
| 72 | ISADevice parent_obj; |
| 73 | |
Jan Kiszka | e3914e3 | 2013-06-22 08:06:55 +0200 | [diff] [blame] | 74 | MemoryRegion io_data; |
| 75 | MemoryRegion io_cmd; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 76 | uint32_t iobase; |
| 77 | uint8_t cmd; |
| 78 | uint8_t status; |
| 79 | uint8_t key[4]; |
| 80 | uint8_t read_pos; |
| 81 | uint8_t data_len; |
| 82 | uint8_t data_pos; |
| 83 | uint8_t data[255]; |
| 84 | uint8_t charactic[4]; |
| 85 | char *osk; |
| 86 | QLIST_HEAD(, AppleSMCData) data_def; |
| 87 | }; |
| 88 | |
Jan Kiszka | e3914e3 | 2013-06-22 08:06:55 +0200 | [diff] [blame] | 89 | static void applesmc_io_cmd_write(void *opaque, hwaddr addr, uint64_t val, |
| 90 | unsigned size) |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 91 | { |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 92 | AppleSMCState *s = opaque; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 93 | |
| 94 | smc_debug("CMD Write B: %#x = %#x\n", addr, val); |
| 95 | switch(val) { |
| 96 | case APPLESMC_READ_CMD: |
| 97 | s->status = 0x0c; |
| 98 | break; |
| 99 | } |
| 100 | s->cmd = val; |
| 101 | s->read_pos = 0; |
| 102 | s->data_pos = 0; |
| 103 | } |
| 104 | |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 105 | static void applesmc_fill_data(AppleSMCState *s) |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 106 | { |
| 107 | struct AppleSMCData *d; |
| 108 | |
| 109 | QLIST_FOREACH(d, &s->data_def, node) { |
| 110 | if (!memcmp(d->key, s->key, 4)) { |
| 111 | smc_debug("Key matched (%s Len=%d Data=%s)\n", d->key, |
| 112 | d->len, d->data); |
| 113 | memcpy(s->data, d->data, d->len); |
| 114 | return; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
Jan Kiszka | e3914e3 | 2013-06-22 08:06:55 +0200 | [diff] [blame] | 119 | static void applesmc_io_data_write(void *opaque, hwaddr addr, uint64_t val, |
| 120 | unsigned size) |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 121 | { |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 122 | AppleSMCState *s = opaque; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 123 | |
| 124 | smc_debug("DATA Write B: %#x = %#x\n", addr, val); |
| 125 | switch(s->cmd) { |
| 126 | case APPLESMC_READ_CMD: |
| 127 | if(s->read_pos < 4) { |
| 128 | s->key[s->read_pos] = val; |
| 129 | s->status = 0x04; |
| 130 | } else if(s->read_pos == 4) { |
| 131 | s->data_len = val; |
| 132 | s->status = 0x05; |
| 133 | s->data_pos = 0; |
| 134 | smc_debug("Key = %c%c%c%c Len = %d\n", s->key[0], |
| 135 | s->key[1], s->key[2], s->key[3], val); |
| 136 | applesmc_fill_data(s); |
| 137 | } |
| 138 | s->read_pos++; |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
Jan Kiszka | e3914e3 | 2013-06-22 08:06:55 +0200 | [diff] [blame] | 143 | static uint64_t applesmc_io_data_read(void *opaque, hwaddr addr1, |
| 144 | unsigned size) |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 145 | { |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 146 | AppleSMCState *s = opaque; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 147 | uint8_t retval = 0; |
| 148 | |
| 149 | switch(s->cmd) { |
| 150 | case APPLESMC_READ_CMD: |
| 151 | if(s->data_pos < s->data_len) { |
| 152 | retval = s->data[s->data_pos]; |
| 153 | smc_debug("READ_DATA[%d] = %#hhx\n", s->data_pos, |
| 154 | retval); |
| 155 | s->data_pos++; |
| 156 | if(s->data_pos == s->data_len) { |
| 157 | s->status = 0x00; |
| 158 | smc_debug("EOF\n"); |
| 159 | } else |
| 160 | s->status = 0x05; |
| 161 | } |
| 162 | } |
| 163 | smc_debug("DATA Read b: %#x = %#x\n", addr1, retval); |
| 164 | |
| 165 | return retval; |
| 166 | } |
| 167 | |
Jan Kiszka | e3914e3 | 2013-06-22 08:06:55 +0200 | [diff] [blame] | 168 | static uint64_t applesmc_io_cmd_read(void *opaque, hwaddr addr1, unsigned size) |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 169 | { |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 170 | AppleSMCState *s = opaque; |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 171 | |
| 172 | smc_debug("CMD Read B: %#x\n", addr1); |
| 173 | return s->status; |
| 174 | } |
| 175 | |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 176 | static void applesmc_add_key(AppleSMCState *s, const char *key, |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 177 | int len, const char *data) |
| 178 | { |
| 179 | struct AppleSMCData *def; |
| 180 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 181 | def = g_malloc0(sizeof(struct AppleSMCData)); |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 182 | def->key = key; |
| 183 | def->len = len; |
| 184 | def->data = data; |
| 185 | |
| 186 | QLIST_INSERT_HEAD(&s->data_def, def, node); |
| 187 | } |
| 188 | |
| 189 | static void qdev_applesmc_isa_reset(DeviceState *dev) |
| 190 | { |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 191 | AppleSMCState *s = APPLE_SMC(dev); |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 192 | struct AppleSMCData *d, *next; |
| 193 | |
| 194 | /* Remove existing entries */ |
| 195 | QLIST_FOREACH_SAFE(d, &s->data_def, node, next) { |
| 196 | QLIST_REMOVE(d, node); |
| 197 | } |
| 198 | |
René Rebe | 7f90fa7 | 2011-03-21 11:33:21 +0100 | [diff] [blame] | 199 | applesmc_add_key(s, "REV ", 6, "\x01\x13\x0f\x00\x00\x03"); |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 200 | applesmc_add_key(s, "OSK0", 32, s->osk); |
| 201 | applesmc_add_key(s, "OSK1", 32, s->osk + 32); |
| 202 | applesmc_add_key(s, "NATJ", 1, "\0"); |
| 203 | applesmc_add_key(s, "MSSP", 1, "\0"); |
| 204 | applesmc_add_key(s, "MSSD", 1, "\0x3"); |
| 205 | } |
| 206 | |
Jan Kiszka | e3914e3 | 2013-06-22 08:06:55 +0200 | [diff] [blame] | 207 | static const MemoryRegionOps applesmc_data_io_ops = { |
| 208 | .write = applesmc_io_data_write, |
| 209 | .read = applesmc_io_data_read, |
| 210 | .endianness = DEVICE_NATIVE_ENDIAN, |
| 211 | .impl = { |
| 212 | .min_access_size = 1, |
| 213 | .max_access_size = 1, |
| 214 | }, |
| 215 | }; |
| 216 | |
| 217 | static const MemoryRegionOps applesmc_cmd_io_ops = { |
| 218 | .write = applesmc_io_cmd_write, |
| 219 | .read = applesmc_io_cmd_read, |
| 220 | .endianness = DEVICE_NATIVE_ENDIAN, |
| 221 | .impl = { |
| 222 | .min_access_size = 1, |
| 223 | .max_access_size = 1, |
| 224 | }, |
| 225 | }; |
| 226 | |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 227 | static void applesmc_isa_realize(DeviceState *dev, Error **errp) |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 228 | { |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 229 | AppleSMCState *s = APPLE_SMC(dev); |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 230 | |
Paolo Bonzini | 3c16154 | 2013-06-06 21:25:08 -0400 | [diff] [blame] | 231 | memory_region_init_io(&s->io_data, OBJECT(s), &applesmc_data_io_ops, s, |
Jan Kiszka | e3914e3 | 2013-06-22 08:06:55 +0200 | [diff] [blame] | 232 | "applesmc-data", 4); |
| 233 | isa_register_ioport(&s->parent_obj, &s->io_data, |
| 234 | s->iobase + APPLESMC_DATA_PORT); |
| 235 | |
Paolo Bonzini | 3c16154 | 2013-06-06 21:25:08 -0400 | [diff] [blame] | 236 | memory_region_init_io(&s->io_cmd, OBJECT(s), &applesmc_cmd_io_ops, s, |
Jan Kiszka | e3914e3 | 2013-06-22 08:06:55 +0200 | [diff] [blame] | 237 | "applesmc-cmd", 4); |
| 238 | isa_register_ioport(&s->parent_obj, &s->io_cmd, |
| 239 | s->iobase + APPLESMC_CMD_PORT); |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 240 | |
| 241 | if (!s->osk || (strlen(s->osk) != 64)) { |
| 242 | fprintf(stderr, "WARNING: Using AppleSMC with invalid key\n"); |
| 243 | s->osk = default_osk; |
| 244 | } |
| 245 | |
| 246 | QLIST_INIT(&s->data_def); |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 247 | qdev_applesmc_isa_reset(dev); |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 250 | static Property applesmc_isa_properties[] = { |
Igor Mammedov | 1142e45 | 2015-02-20 18:22:11 +0000 | [diff] [blame] | 251 | DEFINE_PROP_UINT32(APPLESMC_PROP_IO_BASE, AppleSMCState, iobase, |
| 252 | APPLESMC_DEFAULT_IOBASE), |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 253 | DEFINE_PROP_STRING("osk", AppleSMCState, osk), |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 254 | DEFINE_PROP_END_OF_LIST(), |
| 255 | }; |
| 256 | |
Anthony Liguori | 8f04ee0 | 2011-12-04 11:52:49 -0600 | [diff] [blame] | 257 | static void qdev_applesmc_class_init(ObjectClass *klass, void *data) |
| 258 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 259 | DeviceClass *dc = DEVICE_CLASS(klass); |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 260 | |
| 261 | dc->realize = applesmc_isa_realize; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 262 | dc->reset = qdev_applesmc_isa_reset; |
| 263 | dc->props = applesmc_isa_properties; |
Marcel Apfelbaum | 125ee0e | 2013-07-29 17:17:45 +0300 | [diff] [blame] | 264 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
Anthony Liguori | 8f04ee0 | 2011-12-04 11:52:49 -0600 | [diff] [blame] | 265 | } |
| 266 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 267 | static const TypeInfo applesmc_isa_info = { |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 268 | .name = TYPE_APPLE_SMC, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 269 | .parent = TYPE_ISA_DEVICE, |
Andreas Färber | 82407b6 | 2013-04-27 22:18:36 +0200 | [diff] [blame] | 270 | .instance_size = sizeof(AppleSMCState), |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 271 | .class_init = qdev_applesmc_class_init, |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 272 | }; |
| 273 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 274 | static void applesmc_register_types(void) |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 275 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 276 | type_register_static(&applesmc_isa_info); |
Alexander Graf | 1ddda5c | 2010-06-30 10:41:12 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 279 | type_init(applesmc_register_types) |