2 * QEMU National Semiconductor PC87312 (Super I/O)
4 * Copyright (c) 2010-2012 Herve Poussineau
5 * Copyright (c) 2011-2012 Andreas Färber
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
27 #include "sysemu/blockdev.h"
28 #include "sysemu/sysemu.h"
29 #include "char/char.h"
37 #define FER regs[REG_FER]
38 #define FAR regs[REG_FAR]
39 #define PTR regs[REG_PTR]
41 #define FER_PARALLEL_EN 0x01
42 #define FER_UART1_EN 0x02
43 #define FER_UART2_EN 0x04
44 #define FER_FDC_EN 0x08
45 #define FER_FDC_4 0x10
46 #define FER_FDC_ADDR 0x20
47 #define FER_IDE_EN 0x40
48 #define FER_IDE_ADDR 0x80
50 #define FAR_PARALLEL_ADDR 0x03
51 #define FAR_UART1_ADDR 0x0C
52 #define FAR_UART2_ADDR 0x30
53 #define FAR_UART_3_4 0xC0
55 #define PTR_POWER_DOWN 0x01
56 #define PTR_CLOCK_DOWN 0x02
58 #define PTR_IRQ_5_7 0x08
59 #define PTR_UART1_TEST 0x10
60 #define PTR_UART2_TEST 0x20
61 #define PTR_LOCK_CONF 0x40
62 #define PTR_EPP_MODE 0x80
67 static inline bool is_parallel_enabled(PC87312State *s)
69 return s->FER & FER_PARALLEL_EN;
72 static const uint32_t parallel_base[] = { 0x378, 0x3bc, 0x278, 0x00 };
74 static inline uint32_t get_parallel_iobase(PC87312State *s)
76 return parallel_base[s->FAR & FAR_PARALLEL_ADDR];
79 static const uint32_t parallel_irq[] = { 5, 7, 5, 0 };
81 static inline uint32_t get_parallel_irq(PC87312State *s)
84 idx = (s->FAR & FAR_PARALLEL_ADDR);
86 return (s->PTR & PTR_IRQ_5_7) ? 7 : 5;
88 return parallel_irq[idx];
92 static inline bool is_parallel_epp(PC87312State *s)
94 return s->PTR & PTR_EPP_MODE;
100 static const uint32_t uart_base[2][4] = {
101 { 0x3e8, 0x338, 0x2e8, 0x220 },
102 { 0x2e8, 0x238, 0x2e0, 0x228 }
105 static inline uint32_t get_uart_iobase(PC87312State *s, int i)
108 idx = (s->FAR >> (2 * i + 2)) & 0x3;
111 } else if (idx == 1) {
114 return uart_base[idx & 1][(s->FAR & FAR_UART_3_4) >> 6];
118 static inline uint32_t get_uart_irq(PC87312State *s, int i)
121 idx = (s->FAR >> (2 * i + 2)) & 0x3;
122 return (idx & 1) ? 3 : 4;
125 static inline bool is_uart_enabled(PC87312State *s, int i)
127 return s->FER & (FER_UART1_EN << i);
131 /* Floppy controller */
133 static inline bool is_fdc_enabled(PC87312State *s)
135 return s->FER & FER_FDC_EN;
138 static inline uint32_t get_fdc_iobase(PC87312State *s)
140 return (s->FER & FER_FDC_ADDR) ? 0x370 : 0x3f0;
146 static inline bool is_ide_enabled(PC87312State *s)
148 return s->FER & FER_IDE_EN;
151 static inline uint32_t get_ide_iobase(PC87312State *s)
153 return (s->FER & FER_IDE_ADDR) ? 0x170 : 0x1f0;
157 static void reconfigure_devices(PC87312State *s)
159 error_report("pc87312: unsupported device reconfiguration (%02x %02x %02x)",
160 s->FER, s->FAR, s->PTR);
163 static void pc87312_soft_reset(PC87312State *s)
165 static const uint8_t fer_init[] = {
166 0x4f, 0x4f, 0x4f, 0x4f, 0x4f, 0x4f, 0x4b, 0x4b,
167 0x4b, 0x4b, 0x4b, 0x4b, 0x0f, 0x0f, 0x0f, 0x0f,
168 0x49, 0x49, 0x49, 0x49, 0x07, 0x07, 0x07, 0x07,
169 0x47, 0x47, 0x47, 0x47, 0x47, 0x47, 0x08, 0x00,
171 static const uint8_t far_init[] = {
172 0x10, 0x11, 0x11, 0x39, 0x24, 0x38, 0x00, 0x01,
173 0x01, 0x09, 0x08, 0x08, 0x10, 0x11, 0x39, 0x24,
174 0x00, 0x01, 0x01, 0x00, 0x10, 0x11, 0x39, 0x24,
175 0x10, 0x11, 0x11, 0x39, 0x24, 0x38, 0x10, 0x10,
177 static const uint8_t ptr_init[] = {
178 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
179 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
180 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
181 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
185 s->selected_index = REG_FER;
187 s->FER = fer_init[s->config & 0x1f];
188 s->FAR = far_init[s->config & 0x1f];
189 s->PTR = ptr_init[s->config & 0x1f];
192 static void pc87312_hard_reset(PC87312State *s)
194 pc87312_soft_reset(s);
197 static void pc87312_ioport_write(void *opaque, uint32_t addr, uint32_t val)
199 PC87312State *s = opaque;
201 trace_pc87312_io_write(addr, val);
203 if ((addr & 1) == 0) {
206 s->selected_index = val;
209 if (s->selected_index < 3) {
210 s->regs[s->selected_index] = val;
211 reconfigure_devices(s);
216 static uint32_t pc87312_ioport_read(void *opaque, uint32_t addr)
218 PC87312State *s = opaque;
221 if ((addr & 1) == 0) {
223 if (s->read_id_step++ == 0) {
225 } else if (s->read_id_step++ == 1) {
228 val = s->selected_index;
232 if (s->selected_index < 3) {
233 val = s->regs[s->selected_index];
235 /* Invalid selected index */
240 trace_pc87312_io_read(addr, val);
244 static int pc87312_post_load(void *opaque, int version_id)
246 PC87312State *s = opaque;
248 reconfigure_devices(s);
252 static void pc87312_reset(DeviceState *d)
254 PC87312State *s = PC87312(d);
256 pc87312_soft_reset(s);
259 static int pc87312_init(ISADevice *dev)
265 CharDriverState *chr;
271 bus = isa_bus_from_device(dev);
272 pc87312_hard_reset(s);
274 if (is_parallel_enabled(s)) {
275 chr = parallel_hds[0];
277 chr = qemu_chr_new("par0", "null", NULL);
279 isa = isa_create(bus, "isa-parallel");
281 qdev_prop_set_uint32(d, "index", 0);
282 qdev_prop_set_uint32(d, "iobase", get_parallel_iobase(s));
283 qdev_prop_set_uint32(d, "irq", get_parallel_irq(s));
284 qdev_prop_set_chr(d, "chardev", chr);
286 s->parallel.dev = isa;
287 trace_pc87312_info_parallel(get_parallel_iobase(s),
288 get_parallel_irq(s));
291 for (i = 0; i < 2; i++) {
292 if (is_uart_enabled(s, i)) {
295 snprintf(name, sizeof(name), "ser%d", i);
296 chr = qemu_chr_new(name, "null", NULL);
298 isa = isa_create(bus, "isa-serial");
300 qdev_prop_set_uint32(d, "index", i);
301 qdev_prop_set_uint32(d, "iobase", get_uart_iobase(s, i));
302 qdev_prop_set_uint32(d, "irq", get_uart_irq(s, i));
303 qdev_prop_set_chr(d, "chardev", chr);
305 s->uart[i].dev = isa;
306 trace_pc87312_info_serial(i, get_uart_iobase(s, i),
311 if (is_fdc_enabled(s)) {
312 isa = isa_create(bus, "isa-fdc");
314 qdev_prop_set_uint32(d, "iobase", get_fdc_iobase(s));
315 qdev_prop_set_uint32(d, "irq", 6);
316 drive = drive_get(IF_FLOPPY, 0, 0);
318 qdev_prop_set_drive_nofail(d, "driveA", drive->bdrv);
320 drive = drive_get(IF_FLOPPY, 0, 1);
322 qdev_prop_set_drive_nofail(d, "driveB", drive->bdrv);
326 trace_pc87312_info_floppy(get_fdc_iobase(s));
329 if (is_ide_enabled(s)) {
330 isa = isa_create(bus, "isa-ide");
332 qdev_prop_set_uint32(d, "iobase", get_ide_iobase(s));
333 qdev_prop_set_uint32(d, "iobase2", get_ide_iobase(s) + 0x206);
334 qdev_prop_set_uint32(d, "irq", 14);
337 trace_pc87312_info_ide(get_ide_iobase(s));
340 register_ioport_write(s->iobase, 2, 1, pc87312_ioport_write, s);
341 register_ioport_read(s->iobase, 2, 1, pc87312_ioport_read, s);
345 static const VMStateDescription vmstate_pc87312 = {
348 .minimum_version_id = 1,
349 .post_load = pc87312_post_load,
350 .fields = (VMStateField[]) {
351 VMSTATE_UINT8(read_id_step, PC87312State),
352 VMSTATE_UINT8(selected_index, PC87312State),
353 VMSTATE_UINT8_ARRAY(regs, PC87312State, 3),
354 VMSTATE_END_OF_LIST()
358 static Property pc87312_properties[] = {
359 DEFINE_PROP_HEX32("iobase", PC87312State, iobase, 0x398),
360 DEFINE_PROP_UINT8("config", PC87312State, config, 1),
361 DEFINE_PROP_END_OF_LIST()
364 static void pc87312_class_init(ObjectClass *klass, void *data)
366 DeviceClass *dc = DEVICE_CLASS(klass);
367 ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
369 ic->init = pc87312_init;
370 dc->reset = pc87312_reset;
371 dc->vmsd = &vmstate_pc87312;
372 dc->props = pc87312_properties;
375 static const TypeInfo pc87312_type_info = {
376 .name = TYPE_PC87312,
377 .parent = TYPE_ISA_DEVICE,
378 .instance_size = sizeof(PC87312State),
379 .class_init = pc87312_class_init,
382 static void pc87312_register_types(void)
384 type_register_static(&pc87312_type_info);
387 type_init(pc87312_register_types)