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 * The controller is used in Sun4m systems in a slightly different
27 * way. There are changes in DOR register and DMA is not available.
30 #include "qemu/osdep.h"
31 #include "hw/block/fdc.h"
32 #include "qapi/error.h"
33 #include "qemu/error-report.h"
34 #include "qemu/timer.h"
35 #include "hw/acpi/acpi_aml_interface.h"
37 #include "hw/isa/isa.h"
38 #include "hw/qdev-properties.h"
39 #include "hw/qdev-properties-system.h"
40 #include "migration/vmstate.h"
41 #include "hw/block/block.h"
42 #include "sysemu/block-backend.h"
43 #include "sysemu/blockdev.h"
44 #include "sysemu/sysemu.h"
46 #include "qemu/main-loop.h"
47 #include "qemu/module.h"
49 #include "qom/object.h"
50 #include "fdc-internal.h"
52 OBJECT_DECLARE_SIMPLE_TYPE(FDCtrlISABus
, ISA_FDC
)
67 static void fdctrl_external_reset_isa(DeviceState
*d
)
69 FDCtrlISABus
*isa
= ISA_FDC(d
);
70 FDCtrl
*s
= &isa
->state
;
75 void isa_fdc_init_drives(ISADevice
*fdc
, DriveInfo
**fds
)
77 fdctrl_init_drives(&ISA_FDC(fdc
)->state
.bus
, fds
);
80 static const MemoryRegionPortio fdc_portio_list
[] = {
81 { 1, 5, 1, .read
= fdctrl_read
, .write
= fdctrl_write
},
82 { 7, 1, 1, .read
= fdctrl_read
, .write
= fdctrl_write
},
86 static void isabus_fdc_realize(DeviceState
*dev
, Error
**errp
)
88 ISADevice
*isadev
= ISA_DEVICE(dev
);
89 FDCtrlISABus
*isa
= ISA_FDC(dev
);
90 FDCtrl
*fdctrl
= &isa
->state
;
93 isa_register_portio_list(isadev
, &fdctrl
->portio_list
,
94 isa
->iobase
, fdc_portio_list
, fdctrl
,
97 fdctrl
->irq
= isa_get_irq(isadev
, isa
->irq
);
98 fdctrl
->dma_chann
= isa
->dma
;
99 if (fdctrl
->dma_chann
!= -1) {
101 fdctrl
->dma
= isa_get_dma(isa_bus_from_device(isadev
), isa
->dma
);
103 error_setg(errp
, "ISA controller does not support DMA");
106 k
= ISADMA_GET_CLASS(fdctrl
->dma
);
107 k
->register_channel(fdctrl
->dma
, fdctrl
->dma_chann
,
108 &fdctrl_transfer_handler
, fdctrl
);
111 qdev_set_legacy_instance_id(dev
, isa
->iobase
, 2);
113 fdctrl_realize_common(dev
, fdctrl
, &err
);
115 error_propagate(errp
, err
);
120 FloppyDriveType
isa_fdc_get_drive_type(ISADevice
*fdc
, int i
)
122 FDCtrlISABus
*isa
= ISA_FDC(fdc
);
124 return isa
->state
.drives
[i
].drive
;
127 static void isa_fdc_get_drive_max_chs(FloppyDriveType type
, uint8_t *maxc
,
128 uint8_t *maxh
, uint8_t *maxs
)
132 *maxc
= *maxh
= *maxs
= 0;
133 for (fdf
= fd_formats
; fdf
->drive
!= FLOPPY_DRIVE_TYPE_NONE
; fdf
++) {
134 if (fdf
->drive
!= type
) {
137 if (*maxc
< fdf
->max_track
) {
138 *maxc
= fdf
->max_track
;
140 if (*maxh
< fdf
->max_head
) {
141 *maxh
= fdf
->max_head
;
143 if (*maxs
< fdf
->last_sect
) {
144 *maxs
= fdf
->last_sect
;
150 static Aml
*build_fdinfo_aml(int idx
, FloppyDriveType type
)
153 uint8_t maxc
, maxh
, maxs
;
155 isa_fdc_get_drive_max_chs(type
, &maxc
, &maxh
, &maxs
);
157 dev
= aml_device("FLP%c", 'A' + idx
);
159 aml_append(dev
, aml_name_decl("_ADR", aml_int(idx
)));
161 fdi
= aml_package(16);
162 aml_append(fdi
, aml_int(idx
)); /* Drive Number */
164 aml_int(cmos_get_fd_drive_type(type
))); /* Device Type */
166 * the values below are the limits of the drive, and are thus independent
167 * of the inserted media
169 aml_append(fdi
, aml_int(maxc
)); /* Maximum Cylinder Number */
170 aml_append(fdi
, aml_int(maxs
)); /* Maximum Sector Number */
171 aml_append(fdi
, aml_int(maxh
)); /* Maximum Head Number */
173 * SeaBIOS returns the below values for int 0x13 func 0x08 regardless of
174 * the drive type, so shall we
176 aml_append(fdi
, aml_int(0xAF)); /* disk_specify_1 */
177 aml_append(fdi
, aml_int(0x02)); /* disk_specify_2 */
178 aml_append(fdi
, aml_int(0x25)); /* disk_motor_wait */
179 aml_append(fdi
, aml_int(0x02)); /* disk_sector_siz */
180 aml_append(fdi
, aml_int(0x12)); /* disk_eot */
181 aml_append(fdi
, aml_int(0x1B)); /* disk_rw_gap */
182 aml_append(fdi
, aml_int(0xFF)); /* disk_dtl */
183 aml_append(fdi
, aml_int(0x6C)); /* disk_formt_gap */
184 aml_append(fdi
, aml_int(0xF6)); /* disk_fill */
185 aml_append(fdi
, aml_int(0x0F)); /* disk_head_sttl */
186 aml_append(fdi
, aml_int(0x08)); /* disk_motor_strt */
188 aml_append(dev
, aml_name_decl("_FDI", fdi
));
192 int cmos_get_fd_drive_type(FloppyDriveType fd0
)
197 case FLOPPY_DRIVE_TYPE_144
:
198 /* 1.44 Mb 3"5 drive */
201 case FLOPPY_DRIVE_TYPE_288
:
202 /* 2.88 Mb 3"5 drive */
205 case FLOPPY_DRIVE_TYPE_120
:
206 /* 1.2 Mb 5"5 drive */
209 case FLOPPY_DRIVE_TYPE_NONE
:
217 static void build_fdc_aml(AcpiDevAmlIf
*adev
, Aml
*scope
)
219 FDCtrlISABus
*isa
= ISA_FDC(adev
);
224 #define ACPI_FDE_MAX_FD 4
225 uint32_t fde_buf
[5] = {
226 0, 0, 0, 0, /* presence of floppy drives #0 - #3 */
227 cpu_to_le32(2) /* tape presence (2 == never present) */
230 crs
= aml_resource_template();
232 aml_io(AML_DECODE16
, isa
->iobase
+ 2, isa
->iobase
+ 2, 0x00, 0x04));
234 aml_io(AML_DECODE16
, isa
->iobase
+ 7, isa
->iobase
+ 7, 0x00, 0x01));
235 aml_append(crs
, aml_irq_no_flags(isa
->irq
));
237 aml_dma(AML_COMPATIBILITY
, AML_NOTBUSMASTER
, AML_TRANSFER8
, isa
->dma
));
239 dev
= aml_device("FDC0");
240 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0700")));
241 aml_append(dev
, aml_name_decl("_CRS", crs
));
243 for (i
= 0; i
< MIN(MAX_FD
, ACPI_FDE_MAX_FD
); i
++) {
244 FloppyDriveType type
= isa_fdc_get_drive_type(ISA_DEVICE(adev
), i
);
246 if (type
< FLOPPY_DRIVE_TYPE_NONE
) {
247 fde_buf
[i
] = cpu_to_le32(1); /* drive present */
248 aml_append(dev
, build_fdinfo_aml(i
, type
));
251 aml_append(dev
, aml_name_decl("_FDE",
252 aml_buffer(sizeof(fde_buf
), (uint8_t *)fde_buf
)));
254 aml_append(scope
, dev
);
257 static const VMStateDescription vmstate_isa_fdc
= {
260 .minimum_version_id
= 2,
261 .fields
= (VMStateField
[]) {
262 VMSTATE_STRUCT(state
, FDCtrlISABus
, 0, vmstate_fdc
, FDCtrl
),
263 VMSTATE_END_OF_LIST()
267 static Property isa_fdc_properties
[] = {
268 DEFINE_PROP_UINT32("iobase", FDCtrlISABus
, iobase
, 0x3f0),
269 DEFINE_PROP_UINT32("irq", FDCtrlISABus
, irq
, 6),
270 DEFINE_PROP_UINT32("dma", FDCtrlISABus
, dma
, 2),
271 DEFINE_PROP_SIGNED("fdtypeA", FDCtrlISABus
, state
.qdev_for_drives
[0].type
,
272 FLOPPY_DRIVE_TYPE_AUTO
, qdev_prop_fdc_drive_type
,
274 DEFINE_PROP_SIGNED("fdtypeB", FDCtrlISABus
, state
.qdev_for_drives
[1].type
,
275 FLOPPY_DRIVE_TYPE_AUTO
, qdev_prop_fdc_drive_type
,
277 DEFINE_PROP_SIGNED("fallback", FDCtrlISABus
, state
.fallback
,
278 FLOPPY_DRIVE_TYPE_288
, qdev_prop_fdc_drive_type
,
280 DEFINE_PROP_END_OF_LIST(),
283 static void isabus_fdc_class_init(ObjectClass
*klass
, void *data
)
285 DeviceClass
*dc
= DEVICE_CLASS(klass
);
286 AcpiDevAmlIfClass
*adevc
= ACPI_DEV_AML_IF_CLASS(klass
);
288 dc
->desc
= "virtual floppy controller";
289 dc
->realize
= isabus_fdc_realize
;
291 dc
->reset
= fdctrl_external_reset_isa
;
292 dc
->vmsd
= &vmstate_isa_fdc
;
293 adevc
->build_dev_aml
= build_fdc_aml
;
294 device_class_set_props(dc
, isa_fdc_properties
);
295 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
298 static void isabus_fdc_instance_init(Object
*obj
)
300 FDCtrlISABus
*isa
= ISA_FDC(obj
);
302 device_add_bootindex_property(obj
, &isa
->bootindexA
,
303 "bootindexA", "/floppy@0",
305 device_add_bootindex_property(obj
, &isa
->bootindexB
,
306 "bootindexB", "/floppy@1",
310 static const TypeInfo isa_fdc_info
= {
311 .name
= TYPE_ISA_FDC
,
312 .parent
= TYPE_ISA_DEVICE
,
313 .instance_size
= sizeof(FDCtrlISABus
),
314 .class_init
= isabus_fdc_class_init
,
315 .instance_init
= isabus_fdc_instance_init
,
316 .interfaces
= (InterfaceInfo
[]) {
317 { TYPE_ACPI_DEV_AML_IF
},
322 static void isa_fdc_register_types(void)
324 type_register_static(&isa_fdc_info
);
327 type_init(isa_fdc_register_types
)