1 /* 2 * QEMU National Semiconductor PC87312 (Super I/O) 3 * 4 * Copyright (c) 2010-2012 Herve Poussineau 5 * Copyright (c) 2011-2012 Andreas Färber 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated documentation files (the "Software"), to deal 9 * in the Software without restriction, including without limitation the rights 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 * THE SOFTWARE. 24 */ 25 26 #include "hw/isa/pc87312.h" 27 #include "qemu/error-report.h" 28 #include "sysemu/blockdev.h" 29 #include "sysemu/sysemu.h" 30 #include "sysemu/char.h" 31 #include "trace.h" 32 33 34 #define REG_FER 0 35 #define REG_FAR 1 36 #define REG_PTR 2 37 38 #define FER_PARALLEL_EN 0x01 39 #define FER_UART1_EN 0x02 40 #define FER_UART2_EN 0x04 41 #define FER_FDC_EN 0x08 42 #define FER_FDC_4 0x10 43 #define FER_FDC_ADDR 0x20 44 #define FER_IDE_EN 0x40 45 #define FER_IDE_ADDR 0x80 46 47 #define FAR_PARALLEL_ADDR 0x03 48 #define FAR_UART1_ADDR 0x0C 49 #define FAR_UART2_ADDR 0x30 50 #define FAR_UART_3_4 0xC0 51 52 #define PTR_POWER_DOWN 0x01 53 #define PTR_CLOCK_DOWN 0x02 54 #define PTR_PWDN 0x04 55 #define PTR_IRQ_5_7 0x08 56 #define PTR_UART1_TEST 0x10 57 #define PTR_UART2_TEST 0x20 58 #define PTR_LOCK_CONF 0x40 59 #define PTR_EPP_MODE 0x80 60 61 62 /* Parallel port */ 63 64 static inline bool is_parallel_enabled(PC87312State *s) 65 { 66 return s->regs[REG_FER] & FER_PARALLEL_EN; 67 } 68 69 static const uint32_t parallel_base[] = { 0x378, 0x3bc, 0x278, 0x00 }; 70 71 static inline uint32_t get_parallel_iobase(PC87312State *s) 72 { 73 return parallel_base[s->regs[REG_FAR] & FAR_PARALLEL_ADDR]; 74 } 75 76 static const uint32_t parallel_irq[] = { 5, 7, 5, 0 }; 77 78 static inline uint32_t get_parallel_irq(PC87312State *s) 79 { 80 int idx; 81 idx = (s->regs[REG_FAR] & FAR_PARALLEL_ADDR); 82 if (idx == 0) { 83 return (s->regs[REG_PTR] & PTR_IRQ_5_7) ? 7 : 5; 84 } else { 85 return parallel_irq[idx]; 86 } 87 } 88 89 static inline bool is_parallel_epp(PC87312State *s) 90 { 91 return s->regs[REG_PTR] & PTR_EPP_MODE; 92 } 93 94 95 /* UARTs */ 96 97 static const uint32_t uart_base[2][4] = { 98 { 0x3e8, 0x338, 0x2e8, 0x220 }, 99 { 0x2e8, 0x238, 0x2e0, 0x228 } 100 }; 101 102 static inline uint32_t get_uart_iobase(PC87312State *s, int i) 103 { 104 int idx; 105 idx = (s->regs[REG_FAR] >> (2 * i + 2)) & 0x3; 106 if (idx == 0) { 107 return 0x3f8; 108 } else if (idx == 1) { 109 return 0x2f8; 110 } else { 111 return uart_base[idx & 1][(s->regs[REG_FAR] & FAR_UART_3_4) >> 6]; 112 } 113 } 114 115 static inline uint32_t get_uart_irq(PC87312State *s, int i) 116 { 117 int idx; 118 idx = (s->regs[REG_FAR] >> (2 * i + 2)) & 0x3; 119 return (idx & 1) ? 3 : 4; 120 } 121 122 static inline bool is_uart_enabled(PC87312State *s, int i) 123 { 124 return s->regs[REG_FER] & (FER_UART1_EN << i); 125 } 126 127 128 /* Floppy controller */ 129 130 static inline bool is_fdc_enabled(PC87312State *s) 131 { 132 return s->regs[REG_FER] & FER_FDC_EN; 133 } 134 135 static inline uint32_t get_fdc_iobase(PC87312State *s) 136 { 137 return (s->regs[REG_FER] & FER_FDC_ADDR) ? 0x370 : 0x3f0; 138 } 139 140 141 /* IDE controller */ 142 143 static inline bool is_ide_enabled(PC87312State *s) 144 { 145 return s->regs[REG_FER] & FER_IDE_EN; 146 } 147 148 static inline uint32_t get_ide_iobase(PC87312State *s) 149 { 150 return (s->regs[REG_FER] & FER_IDE_ADDR) ? 0x170 : 0x1f0; 151 } 152 153 154 static void reconfigure_devices(PC87312State *s) 155 { 156 error_report("pc87312: unsupported device reconfiguration (%02x %02x %02x)", 157 s->regs[REG_FER], s->regs[REG_FAR], s->regs[REG_PTR]); 158 } 159 160 static void pc87312_soft_reset(PC87312State *s) 161 { 162 static const uint8_t fer_init[] = { 163 0x4f, 0x4f, 0x4f, 0x4f, 0x4f, 0x4f, 0x4b, 0x4b, 164 0x4b, 0x4b, 0x4b, 0x4b, 0x0f, 0x0f, 0x0f, 0x0f, 165 0x49, 0x49, 0x49, 0x49, 0x07, 0x07, 0x07, 0x07, 166 0x47, 0x47, 0x47, 0x47, 0x47, 0x47, 0x08, 0x00, 167 }; 168 static const uint8_t far_init[] = { 169 0x10, 0x11, 0x11, 0x39, 0x24, 0x38, 0x00, 0x01, 170 0x01, 0x09, 0x08, 0x08, 0x10, 0x11, 0x39, 0x24, 171 0x00, 0x01, 0x01, 0x00, 0x10, 0x11, 0x39, 0x24, 172 0x10, 0x11, 0x11, 0x39, 0x24, 0x38, 0x10, 0x10, 173 }; 174 static const uint8_t ptr_init[] = { 175 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 176 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 177 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 178 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 179 }; 180 181 s->read_id_step = 0; 182 s->selected_index = REG_FER; 183 184 s->regs[REG_FER] = fer_init[s->config & 0x1f]; 185 s->regs[REG_FAR] = far_init[s->config & 0x1f]; 186 s->regs[REG_PTR] = ptr_init[s->config & 0x1f]; 187 } 188 189 static void pc87312_hard_reset(PC87312State *s) 190 { 191 pc87312_soft_reset(s); 192 } 193 194 static void pc87312_io_write(void *opaque, hwaddr addr, uint64_t val, 195 unsigned int size) 196 { 197 PC87312State *s = opaque; 198 199 trace_pc87312_io_write(addr, val); 200 201 if ((addr & 1) == 0) { 202 /* Index register */ 203 s->read_id_step = 2; 204 s->selected_index = val; 205 } else { 206 /* Data register */ 207 if (s->selected_index < 3) { 208 s->regs[s->selected_index] = val; 209 reconfigure_devices(s); 210 } 211 } 212 } 213 214 static uint64_t pc87312_io_read(void *opaque, hwaddr addr, unsigned int size) 215 { 216 PC87312State *s = opaque; 217 uint32_t val; 218 219 if ((addr & 1) == 0) { 220 /* Index register */ 221 if (s->read_id_step++ == 0) { 222 val = 0x88; 223 } else if (s->read_id_step++ == 1) { 224 val = 0; 225 } else { 226 val = s->selected_index; 227 } 228 } else { 229 /* Data register */ 230 if (s->selected_index < 3) { 231 val = s->regs[s->selected_index]; 232 } else { 233 /* Invalid selected index */ 234 val = 0; 235 } 236 } 237 238 trace_pc87312_io_read(addr, val); 239 return val; 240 } 241 242 static const MemoryRegionOps pc87312_io_ops = { 243 .read = pc87312_io_read, 244 .write = pc87312_io_write, 245 .endianness = DEVICE_LITTLE_ENDIAN, 246 .valid = { 247 .min_access_size = 1, 248 .max_access_size = 1, 249 }, 250 }; 251 252 static int pc87312_post_load(void *opaque, int version_id) 253 { 254 PC87312State *s = opaque; 255 256 reconfigure_devices(s); 257 return 0; 258 } 259 260 static void pc87312_reset(DeviceState *d) 261 { 262 PC87312State *s = PC87312(d); 263 264 pc87312_soft_reset(s); 265 } 266 267 static int pc87312_init(ISADevice *dev) 268 { 269 PC87312State *s; 270 DeviceState *d; 271 ISADevice *isa; 272 ISABus *bus; 273 CharDriverState *chr; 274 DriveInfo *drive; 275 char name[5]; 276 int i; 277 278 s = PC87312(dev); 279 bus = isa_bus_from_device(dev); 280 pc87312_hard_reset(s); 281 isa_register_ioport(dev, &s->io, s->iobase); 282 283 if (is_parallel_enabled(s)) { 284 chr = parallel_hds[0]; 285 if (chr == NULL) { 286 chr = qemu_chr_new("par0", "null", NULL); 287 } 288 isa = isa_create(bus, "isa-parallel"); 289 d = DEVICE(isa); 290 qdev_prop_set_uint32(d, "index", 0); 291 qdev_prop_set_uint32(d, "iobase", get_parallel_iobase(s)); 292 qdev_prop_set_uint32(d, "irq", get_parallel_irq(s)); 293 qdev_prop_set_chr(d, "chardev", chr); 294 qdev_init_nofail(d); 295 s->parallel.dev = isa; 296 trace_pc87312_info_parallel(get_parallel_iobase(s), 297 get_parallel_irq(s)); 298 } 299 300 for (i = 0; i < 2; i++) { 301 if (is_uart_enabled(s, i)) { 302 chr = serial_hds[i]; 303 if (chr == NULL) { 304 snprintf(name, sizeof(name), "ser%d", i); 305 chr = qemu_chr_new(name, "null", NULL); 306 } 307 isa = isa_create(bus, "isa-serial"); 308 d = DEVICE(isa); 309 qdev_prop_set_uint32(d, "index", i); 310 qdev_prop_set_uint32(d, "iobase", get_uart_iobase(s, i)); 311 qdev_prop_set_uint32(d, "irq", get_uart_irq(s, i)); 312 qdev_prop_set_chr(d, "chardev", chr); 313 qdev_init_nofail(d); 314 s->uart[i].dev = isa; 315 trace_pc87312_info_serial(i, get_uart_iobase(s, i), 316 get_uart_irq(s, i)); 317 } 318 } 319 320 if (is_fdc_enabled(s)) { 321 isa = isa_create(bus, "isa-fdc"); 322 d = DEVICE(isa); 323 qdev_prop_set_uint32(d, "iobase", get_fdc_iobase(s)); 324 qdev_prop_set_uint32(d, "irq", 6); 325 drive = drive_get(IF_FLOPPY, 0, 0); 326 if (drive != NULL) { 327 qdev_prop_set_drive_nofail(d, "driveA", drive->bdrv); 328 } 329 drive = drive_get(IF_FLOPPY, 0, 1); 330 if (drive != NULL) { 331 qdev_prop_set_drive_nofail(d, "driveB", drive->bdrv); 332 } 333 qdev_init_nofail(d); 334 s->fdc.dev = isa; 335 trace_pc87312_info_floppy(get_fdc_iobase(s)); 336 } 337 338 if (is_ide_enabled(s)) { 339 isa = isa_create(bus, "isa-ide"); 340 d = DEVICE(isa); 341 qdev_prop_set_uint32(d, "iobase", get_ide_iobase(s)); 342 qdev_prop_set_uint32(d, "iobase2", get_ide_iobase(s) + 0x206); 343 qdev_prop_set_uint32(d, "irq", 14); 344 qdev_init_nofail(d); 345 s->ide.dev = isa; 346 trace_pc87312_info_ide(get_ide_iobase(s)); 347 } 348 349 return 0; 350 } 351 352 static void pc87312_initfn(Object *obj) 353 { 354 PC87312State *s = PC87312(obj); 355 356 memory_region_init_io(&s->io, &pc87312_io_ops, s, "pc87312", 2); 357 } 358 359 static const VMStateDescription vmstate_pc87312 = { 360 .name = "pc87312", 361 .version_id = 1, 362 .minimum_version_id = 1, 363 .post_load = pc87312_post_load, 364 .fields = (VMStateField[]) { 365 VMSTATE_UINT8(read_id_step, PC87312State), 366 VMSTATE_UINT8(selected_index, PC87312State), 367 VMSTATE_UINT8_ARRAY(regs, PC87312State, 3), 368 VMSTATE_END_OF_LIST() 369 } 370 }; 371 372 static Property pc87312_properties[] = { 373 DEFINE_PROP_HEX32("iobase", PC87312State, iobase, 0x398), 374 DEFINE_PROP_UINT8("config", PC87312State, config, 1), 375 DEFINE_PROP_END_OF_LIST() 376 }; 377 378 static void pc87312_class_init(ObjectClass *klass, void *data) 379 { 380 DeviceClass *dc = DEVICE_CLASS(klass); 381 ISADeviceClass *ic = ISA_DEVICE_CLASS(klass); 382 383 ic->init = pc87312_init; 384 dc->reset = pc87312_reset; 385 dc->vmsd = &vmstate_pc87312; 386 dc->props = pc87312_properties; 387 } 388 389 static const TypeInfo pc87312_type_info = { 390 .name = TYPE_PC87312, 391 .parent = TYPE_ISA_DEVICE, 392 .instance_size = sizeof(PC87312State), 393 .instance_init = pc87312_initfn, 394 .class_init = pc87312_class_init, 395 }; 396 397 static void pc87312_register_types(void) 398 { 399 type_register_static(&pc87312_type_info); 400 } 401 402 type_init(pc87312_register_types) 403