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
36 #define FER regs[REG_FER]
37 #define FAR regs[REG_FAR]
38 #define PTR regs[REG_PTR]
40 #define FER_PARALLEL_EN 0x01
41 #define FER_UART1_EN 0x02
42 #define FER_UART2_EN 0x04
43 #define FER_FDC_EN 0x08
44 #define FER_FDC_4 0x10
45 #define FER_FDC_ADDR 0x20
46 #define FER_IDE_EN 0x40
47 #define FER_IDE_ADDR 0x80
49 #define FAR_PARALLEL_ADDR 0x03
50 #define FAR_UART1_ADDR 0x0C
51 #define FAR_UART2_ADDR 0x30
52 #define FAR_UART_3_4 0xC0
54 #define PTR_POWER_DOWN 0x01
55 #define PTR_CLOCK_DOWN 0x02
57 #define PTR_IRQ_5_7 0x08
58 #define PTR_UART1_TEST 0x10
59 #define PTR_UART2_TEST 0x20
60 #define PTR_LOCK_CONF 0x40
61 #define PTR_EPP_MODE 0x80
66 static inline bool is_parallel_enabled(PC87312State
*s
)
68 return s
->FER
& FER_PARALLEL_EN
;
71 static const uint32_t parallel_base
[] = { 0x378, 0x3bc, 0x278, 0x00 };
73 static inline uint32_t get_parallel_iobase(PC87312State
*s
)
75 return parallel_base
[s
->FAR
& FAR_PARALLEL_ADDR
];
78 static const uint32_t parallel_irq
[] = { 5, 7, 5, 0 };
80 static inline uint32_t get_parallel_irq(PC87312State
*s
)
83 idx
= (s
->FAR
& FAR_PARALLEL_ADDR
);
85 return (s
->PTR
& PTR_IRQ_5_7
) ? 7 : 5;
87 return parallel_irq
[idx
];
91 static inline bool is_parallel_epp(PC87312State
*s
)
93 return s
->PTR
& PTR_EPP_MODE
;
99 static const uint32_t uart_base
[2][4] = {
100 { 0x3e8, 0x338, 0x2e8, 0x220 },
101 { 0x2e8, 0x238, 0x2e0, 0x228 }
104 static inline uint32_t get_uart_iobase(PC87312State
*s
, int i
)
107 idx
= (s
->FAR
>> (2 * i
+ 2)) & 0x3;
110 } else if (idx
== 1) {
113 return uart_base
[idx
& 1][(s
->FAR
& FAR_UART_3_4
) >> 6];
117 static inline uint32_t get_uart_irq(PC87312State
*s
, int i
)
120 idx
= (s
->FAR
>> (2 * i
+ 2)) & 0x3;
121 return (idx
& 1) ? 3 : 4;
124 static inline bool is_uart_enabled(PC87312State
*s
, int i
)
126 return s
->FER
& (FER_UART1_EN
<< i
);
130 /* Floppy controller */
132 static inline bool is_fdc_enabled(PC87312State
*s
)
134 return s
->FER
& FER_FDC_EN
;
137 static inline uint32_t get_fdc_iobase(PC87312State
*s
)
139 return (s
->FER
& FER_FDC_ADDR
) ? 0x370 : 0x3f0;
145 static inline bool is_ide_enabled(PC87312State
*s
)
147 return s
->FER
& FER_IDE_EN
;
150 static inline uint32_t get_ide_iobase(PC87312State
*s
)
152 return (s
->FER
& FER_IDE_ADDR
) ? 0x170 : 0x1f0;
156 static void reconfigure_devices(PC87312State
*s
)
158 error_report("pc87312: unsupported device reconfiguration (%02x %02x %02x)",
159 s
->FER
, s
->FAR
, s
->PTR
);
162 static void pc87312_soft_reset(PC87312State
*s
)
164 static const uint8_t fer_init
[] = {
165 0x4f, 0x4f, 0x4f, 0x4f, 0x4f, 0x4f, 0x4b, 0x4b,
166 0x4b, 0x4b, 0x4b, 0x4b, 0x0f, 0x0f, 0x0f, 0x0f,
167 0x49, 0x49, 0x49, 0x49, 0x07, 0x07, 0x07, 0x07,
168 0x47, 0x47, 0x47, 0x47, 0x47, 0x47, 0x08, 0x00,
170 static const uint8_t far_init
[] = {
171 0x10, 0x11, 0x11, 0x39, 0x24, 0x38, 0x00, 0x01,
172 0x01, 0x09, 0x08, 0x08, 0x10, 0x11, 0x39, 0x24,
173 0x00, 0x01, 0x01, 0x00, 0x10, 0x11, 0x39, 0x24,
174 0x10, 0x11, 0x11, 0x39, 0x24, 0x38, 0x10, 0x10,
176 static const uint8_t ptr_init
[] = {
177 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
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, 0x02,
184 s
->selected_index
= REG_FER
;
186 s
->FER
= fer_init
[s
->config
& 0x1f];
187 s
->FAR
= far_init
[s
->config
& 0x1f];
188 s
->PTR
= ptr_init
[s
->config
& 0x1f];
191 static void pc87312_hard_reset(PC87312State
*s
)
193 pc87312_soft_reset(s
);
196 static void pc87312_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
198 PC87312State
*s
= opaque
;
200 trace_pc87312_io_write(addr
, val
);
202 if ((addr
& 1) == 0) {
205 s
->selected_index
= val
;
208 if (s
->selected_index
< 3) {
209 s
->regs
[s
->selected_index
] = val
;
210 reconfigure_devices(s
);
215 static uint32_t pc87312_ioport_read(void *opaque
, uint32_t addr
)
217 PC87312State
*s
= opaque
;
220 if ((addr
& 1) == 0) {
222 if (s
->read_id_step
++ == 0) {
224 } else if (s
->read_id_step
++ == 1) {
227 val
= s
->selected_index
;
231 if (s
->selected_index
< 3) {
232 val
= s
->regs
[s
->selected_index
];
234 /* Invalid selected index */
239 trace_pc87312_io_read(addr
, val
);
243 static int pc87312_post_load(void *opaque
, int version_id
)
245 PC87312State
*s
= opaque
;
247 reconfigure_devices(s
);
251 static void pc87312_reset(DeviceState
*d
)
253 PC87312State
*s
= PC87312(d
);
255 pc87312_soft_reset(s
);
258 static int pc87312_init(ISADevice
*dev
)
264 CharDriverState
*chr
;
270 bus
= isa_bus_from_device(dev
);
271 pc87312_hard_reset(s
);
273 if (is_parallel_enabled(s
)) {
274 chr
= parallel_hds
[0];
276 chr
= qemu_chr_new("par0", "null", NULL
);
278 isa
= isa_create(bus
, "isa-parallel");
280 qdev_prop_set_uint32(d
, "index", 0);
281 qdev_prop_set_uint32(d
, "iobase", get_parallel_iobase(s
));
282 qdev_prop_set_uint32(d
, "irq", get_parallel_irq(s
));
283 qdev_prop_set_chr(d
, "chardev", chr
);
285 s
->parallel
.dev
= isa
;
286 trace_pc87312_info_parallel(get_parallel_iobase(s
),
287 get_parallel_irq(s
));
290 for (i
= 0; i
< 2; i
++) {
291 if (is_uart_enabled(s
, i
)) {
294 snprintf(name
, sizeof(name
), "ser%d", i
);
295 chr
= qemu_chr_new(name
, "null", NULL
);
297 isa
= isa_create(bus
, "isa-serial");
299 qdev_prop_set_uint32(d
, "index", i
);
300 qdev_prop_set_uint32(d
, "iobase", get_uart_iobase(s
, i
));
301 qdev_prop_set_uint32(d
, "irq", get_uart_irq(s
, i
));
302 qdev_prop_set_chr(d
, "chardev", chr
);
304 s
->uart
[i
].dev
= isa
;
305 trace_pc87312_info_serial(i
, get_uart_iobase(s
, i
),
310 if (is_fdc_enabled(s
)) {
311 isa
= isa_create(bus
, "isa-fdc");
313 qdev_prop_set_uint32(d
, "iobase", get_fdc_iobase(s
));
314 qdev_prop_set_uint32(d
, "irq", 6);
315 drive
= drive_get(IF_FLOPPY
, 0, 0);
317 qdev_prop_set_drive_nofail(d
, "driveA", drive
->bdrv
);
319 drive
= drive_get(IF_FLOPPY
, 0, 1);
321 qdev_prop_set_drive_nofail(d
, "driveB", drive
->bdrv
);
325 trace_pc87312_info_floppy(get_fdc_iobase(s
));
328 if (is_ide_enabled(s
)) {
329 isa
= isa_create(bus
, "isa-ide");
331 qdev_prop_set_uint32(d
, "iobase", get_ide_iobase(s
));
332 qdev_prop_set_uint32(d
, "iobase2", get_ide_iobase(s
) + 0x206);
333 qdev_prop_set_uint32(d
, "irq", 14);
336 trace_pc87312_info_ide(get_ide_iobase(s
));
339 register_ioport_write(s
->iobase
, 2, 1, pc87312_ioport_write
, s
);
340 register_ioport_read(s
->iobase
, 2, 1, pc87312_ioport_read
, s
);
344 static const VMStateDescription vmstate_pc87312
= {
347 .minimum_version_id
= 1,
348 .post_load
= pc87312_post_load
,
349 .fields
= (VMStateField
[]) {
350 VMSTATE_UINT8(read_id_step
, PC87312State
),
351 VMSTATE_UINT8(selected_index
, PC87312State
),
352 VMSTATE_UINT8_ARRAY(regs
, PC87312State
, 3),
353 VMSTATE_END_OF_LIST()
357 static Property pc87312_properties
[] = {
358 DEFINE_PROP_HEX32("iobase", PC87312State
, iobase
, 0x398),
359 DEFINE_PROP_UINT8("config", PC87312State
, config
, 1),
360 DEFINE_PROP_END_OF_LIST()
363 static void pc87312_class_init(ObjectClass
*klass
, void *data
)
365 DeviceClass
*dc
= DEVICE_CLASS(klass
);
366 ISADeviceClass
*ic
= ISA_DEVICE_CLASS(klass
);
368 ic
->init
= pc87312_init
;
369 dc
->reset
= pc87312_reset
;
370 dc
->vmsd
= &vmstate_pc87312
;
371 dc
->props
= pc87312_properties
;
374 static const TypeInfo pc87312_type_info
= {
375 .name
= TYPE_PC87312
,
376 .parent
= TYPE_ISA_DEVICE
,
377 .instance_size
= sizeof(PC87312State
),
378 .class_init
= pc87312_class_init
,
381 static void pc87312_register_types(void)
383 type_register_static(&pc87312_type_info
);
386 type_init(pc87312_register_types
)