2 * QEMU Floppy disk emulator (Intel 82078)
4 * Copyright (c) 2003, 2007 Jocelyn Mayer
5 * Copyright (c) 2008 Hervé Poussineau
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
26 #include "qemu/osdep.h"
27 #include "qapi/error.h"
28 #include "qom/object.h"
29 #include "hw/sysbus.h"
30 #include "hw/block/fdc.h"
31 #include "migration/vmstate.h"
32 #include "fdc-internal.h"
35 #define TYPE_SYSBUS_FDC "base-sysbus-fdc"
36 typedef struct FDCtrlSysBusClass FDCtrlSysBusClass
;
37 typedef struct FDCtrlSysBus FDCtrlSysBus
;
38 DECLARE_OBJ_CHECKERS(FDCtrlSysBus
, FDCtrlSysBusClass
,
39 SYSBUS_FDC
, TYPE_SYSBUS_FDC
)
41 struct FDCtrlSysBusClass
{
43 SysBusDeviceClass parent_class
;
51 SysBusDevice parent_obj
;
57 static uint64_t fdctrl_read_mem(void *opaque
, hwaddr reg
, unsigned ize
)
59 return fdctrl_read(opaque
, (uint32_t)reg
);
62 static void fdctrl_write_mem(void *opaque
, hwaddr reg
,
63 uint64_t value
, unsigned size
)
65 fdctrl_write(opaque
, (uint32_t)reg
, value
);
68 static const MemoryRegionOps fdctrl_mem_ops
= {
69 .read
= fdctrl_read_mem
,
70 .write
= fdctrl_write_mem
,
71 .endianness
= DEVICE_NATIVE_ENDIAN
,
74 static const MemoryRegionOps fdctrl_mem_strict_ops
= {
75 .read
= fdctrl_read_mem
,
76 .write
= fdctrl_write_mem
,
77 .endianness
= DEVICE_NATIVE_ENDIAN
,
84 static void fdctrl_external_reset_sysbus(DeviceState
*d
)
86 FDCtrlSysBus
*sys
= SYSBUS_FDC(d
);
87 FDCtrl
*s
= &sys
->state
;
92 static void fdctrl_handle_tc(void *opaque
, int irq
, int level
)
94 trace_fdctrl_tc_pulse(level
);
97 void fdctrl_init_sysbus(qemu_irq irq
, int dma_chann
,
98 hwaddr mmio_base
, DriveInfo
**fds
)
105 dev
= qdev_new("sysbus-fdc");
106 sys
= SYSBUS_FDC(dev
);
107 fdctrl
= &sys
->state
;
108 fdctrl
->dma_chann
= dma_chann
; /* FIXME */
109 sbd
= SYS_BUS_DEVICE(dev
);
110 sysbus_realize_and_unref(sbd
, &error_fatal
);
111 sysbus_connect_irq(sbd
, 0, irq
);
112 sysbus_mmio_map(sbd
, 0, mmio_base
);
114 fdctrl_init_drives(&sys
->state
.bus
, fds
);
117 void sun4m_fdctrl_init(qemu_irq irq
, hwaddr io_base
,
118 DriveInfo
**fds
, qemu_irq
*fdc_tc
)
123 dev
= qdev_new("sun-fdtwo");
124 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev
), &error_fatal
);
125 sys
= SYSBUS_FDC(dev
);
126 sysbus_connect_irq(SYS_BUS_DEVICE(sys
), 0, irq
);
127 sysbus_mmio_map(SYS_BUS_DEVICE(sys
), 0, io_base
);
128 *fdc_tc
= qdev_get_gpio_in(dev
, 0);
130 fdctrl_init_drives(&sys
->state
.bus
, fds
);
133 static void sysbus_fdc_common_instance_init(Object
*obj
)
135 DeviceState
*dev
= DEVICE(obj
);
136 FDCtrlSysBusClass
*sbdc
= SYSBUS_FDC_GET_CLASS(obj
);
137 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
138 FDCtrlSysBus
*sys
= SYSBUS_FDC(obj
);
139 FDCtrl
*fdctrl
= &sys
->state
;
141 qdev_set_legacy_instance_id(dev
, 0 /* io */, 2); /* FIXME */
143 memory_region_init_io(&fdctrl
->iomem
, obj
,
144 sbdc
->use_strict_io
? &fdctrl_mem_strict_ops
146 fdctrl
, "fdc", 0x08);
147 sysbus_init_mmio(sbd
, &fdctrl
->iomem
);
149 sysbus_init_irq(sbd
, &fdctrl
->irq
);
150 qdev_init_gpio_in(dev
, fdctrl_handle_tc
, 1);
153 static void sysbus_fdc_realize(DeviceState
*dev
, Error
**errp
)
155 FDCtrlSysBus
*sys
= SYSBUS_FDC(dev
);
156 FDCtrl
*fdctrl
= &sys
->state
;
158 fdctrl_realize_common(dev
, fdctrl
, errp
);
161 static const VMStateDescription vmstate_sysbus_fdc
= {
164 .minimum_version_id
= 2,
165 .fields
= (VMStateField
[]) {
166 VMSTATE_STRUCT(state
, FDCtrlSysBus
, 0, vmstate_fdc
, FDCtrl
),
167 VMSTATE_END_OF_LIST()
171 static void sysbus_fdc_common_class_init(ObjectClass
*klass
, void *data
)
173 DeviceClass
*dc
= DEVICE_CLASS(klass
);
175 dc
->realize
= sysbus_fdc_realize
;
176 dc
->reset
= fdctrl_external_reset_sysbus
;
177 dc
->vmsd
= &vmstate_sysbus_fdc
;
178 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
181 static const TypeInfo sysbus_fdc_common_typeinfo
= {
182 .name
= TYPE_SYSBUS_FDC
,
183 .parent
= TYPE_SYS_BUS_DEVICE
,
184 .instance_size
= sizeof(FDCtrlSysBus
),
185 .instance_init
= sysbus_fdc_common_instance_init
,
187 .class_init
= sysbus_fdc_common_class_init
,
188 .class_size
= sizeof(FDCtrlSysBusClass
),
191 static Property sysbus_fdc_properties
[] = {
192 DEFINE_PROP_SIGNED("fdtypeA", FDCtrlSysBus
, state
.qdev_for_drives
[0].type
,
193 FLOPPY_DRIVE_TYPE_AUTO
, qdev_prop_fdc_drive_type
,
195 DEFINE_PROP_SIGNED("fdtypeB", FDCtrlSysBus
, state
.qdev_for_drives
[1].type
,
196 FLOPPY_DRIVE_TYPE_AUTO
, qdev_prop_fdc_drive_type
,
198 DEFINE_PROP_SIGNED("fallback", FDCtrlSysBus
, state
.fallback
,
199 FLOPPY_DRIVE_TYPE_144
, qdev_prop_fdc_drive_type
,
201 DEFINE_PROP_END_OF_LIST(),
204 static void sysbus_fdc_class_init(ObjectClass
*klass
, void *data
)
206 DeviceClass
*dc
= DEVICE_CLASS(klass
);
208 dc
->desc
= "virtual floppy controller";
209 device_class_set_props(dc
, sysbus_fdc_properties
);
212 static const TypeInfo sysbus_fdc_typeinfo
= {
213 .name
= "sysbus-fdc",
214 .parent
= TYPE_SYSBUS_FDC
,
215 .class_init
= sysbus_fdc_class_init
,
218 static Property sun4m_fdc_properties
[] = {
219 DEFINE_PROP_SIGNED("fdtype", FDCtrlSysBus
, state
.qdev_for_drives
[0].type
,
220 FLOPPY_DRIVE_TYPE_AUTO
, qdev_prop_fdc_drive_type
,
222 DEFINE_PROP_SIGNED("fallback", FDCtrlSysBus
, state
.fallback
,
223 FLOPPY_DRIVE_TYPE_144
, qdev_prop_fdc_drive_type
,
225 DEFINE_PROP_END_OF_LIST(),
228 static void sun4m_fdc_class_init(ObjectClass
*klass
, void *data
)
230 FDCtrlSysBusClass
*sbdc
= SYSBUS_FDC_CLASS(klass
);
231 DeviceClass
*dc
= DEVICE_CLASS(klass
);
233 sbdc
->use_strict_io
= true;
234 dc
->desc
= "virtual floppy controller";
235 device_class_set_props(dc
, sun4m_fdc_properties
);
238 static const TypeInfo sun4m_fdc_typeinfo
= {
240 .parent
= TYPE_SYSBUS_FDC
,
241 .class_init
= sun4m_fdc_class_init
,
244 static void sysbus_fdc_register_types(void)
246 type_register_static(&sysbus_fdc_common_typeinfo
);
247 type_register_static(&sysbus_fdc_typeinfo
);
248 type_register_static(&sun4m_fdc_typeinfo
);
251 type_init(sysbus_fdc_register_types
)