2 * QEMU SiI3112A PCI to Serial ATA Controller Emulation
4 * Copyright (C) 2017 BALATON Zoltan <balaton@eik.bme.hu>
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
11 /* For documentation on this and similar cards see:
12 * http://wiki.osdev.org/User:Quok/Silicon_Image_Datasheets
15 #include "qemu/osdep.h"
16 #include "hw/ide/pci.h"
17 #include "qemu/module.h"
19 #include "qom/object.h"
21 #define TYPE_SII3112_PCI "sii3112"
22 OBJECT_DECLARE_SIMPLE_TYPE(SiI3112PCIState
, SII3112_PCI
)
24 typedef struct SiI3112Regs
{
31 struct SiI3112PCIState
{
37 /* The sii3112_reg_read and sii3112_reg_write functions implement the
38 * Internal Register Space - BAR5 (section 6.7 of the data sheet).
41 static uint64_t sii3112_reg_read(void *opaque
, hwaddr addr
,
44 SiI3112PCIState
*d
= opaque
;
49 val
= d
->i
.bmdma
[0].cmd
;
52 val
= d
->regs
[0].swdata
;
55 val
= d
->i
.bmdma
[0].status
;
61 val
= bmdma_addr_ioport_ops
.read(&d
->i
.bmdma
[0], addr
- 4, size
);
64 val
= d
->i
.bmdma
[1].cmd
;
67 val
= d
->regs
[1].swdata
;
70 val
= d
->i
.bmdma
[1].status
;
76 val
= bmdma_addr_ioport_ops
.read(&d
->i
.bmdma
[1], addr
- 12, size
);
79 val
= d
->i
.bmdma
[0].cmd
;
80 val
|= (d
->regs
[0].confstat
& (1UL << 11) ? (1 << 4) : 0); /*SATAINT0*/
81 val
|= (d
->regs
[1].confstat
& (1UL << 11) ? (1 << 6) : 0); /*SATAINT1*/
82 val
|= (d
->i
.bmdma
[1].status
& BM_STATUS_INT
? (1 << 14) : 0);
83 val
|= (uint32_t)d
->i
.bmdma
[0].status
<< 16;
84 val
|= (uint32_t)d
->i
.bmdma
[1].status
<< 24;
87 val
= d
->i
.bmdma
[1].cmd
;
88 val
|= (d
->regs
[1].confstat
& (1UL << 11) ? (1 << 4) : 0);
89 val
|= (uint32_t)d
->i
.bmdma
[1].status
<< 16;
92 val
= pci_ide_data_le_ops
.read(&d
->i
.bus
[0], addr
- 0x80, size
);
95 val
= pci_ide_cmd_le_ops
.read(&d
->i
.bus
[0], 2, size
);
98 val
= d
->regs
[0].confstat
;
101 val
= pci_ide_data_le_ops
.read(&d
->i
.bus
[1], addr
- 0xc0, size
);
104 val
= pci_ide_cmd_le_ops
.read(&d
->i
.bus
[1], 2, size
);
107 val
= d
->regs
[1].confstat
;
110 val
= d
->regs
[0].scontrol
;
113 val
= (d
->i
.bus
[0].ifs
[0].blk
) ? 0x113 : 0;
116 val
= (uint32_t)d
->regs
[0].sien
<< 16;
119 val
= d
->regs
[1].scontrol
;
122 val
= (d
->i
.bus
[1].ifs
[0].blk
) ? 0x113 : 0;
125 val
= (uint32_t)d
->regs
[1].sien
<< 16;
131 trace_sii3112_read(size
, addr
, val
);
135 static void sii3112_reg_write(void *opaque
, hwaddr addr
,
136 uint64_t val
, unsigned int size
)
138 SiI3112PCIState
*d
= opaque
;
140 trace_sii3112_write(size
, addr
, val
);
144 bmdma_cmd_writeb(&d
->i
.bmdma
[0], val
);
148 d
->regs
[0].swdata
= val
& 0x3f;
152 bmdma_status_writeb(&d
->i
.bmdma
[0], val
);
155 bmdma_addr_ioport_ops
.write(&d
->i
.bmdma
[0], addr
- 4, val
, size
);
159 bmdma_cmd_writeb(&d
->i
.bmdma
[1], val
);
163 d
->regs
[1].swdata
= val
& 0x3f;
167 bmdma_status_writeb(&d
->i
.bmdma
[1], val
);
170 bmdma_addr_ioport_ops
.write(&d
->i
.bmdma
[1], addr
- 12, val
, size
);
173 pci_ide_data_le_ops
.write(&d
->i
.bus
[0], addr
- 0x80, val
, size
);
176 pci_ide_cmd_le_ops
.write(&d
->i
.bus
[0], 2, val
, size
);
179 pci_ide_data_le_ops
.write(&d
->i
.bus
[1], addr
- 0xc0, val
, size
);
182 pci_ide_cmd_le_ops
.write(&d
->i
.bus
[1], 2, val
, size
);
185 d
->regs
[0].scontrol
= val
& 0xfff;
187 ide_bus_reset(&d
->i
.bus
[0]);
191 d
->regs
[0].sien
= (val
>> 16) & 0x3eed;
194 d
->regs
[1].scontrol
= val
& 0xfff;
196 ide_bus_reset(&d
->i
.bus
[1]);
200 d
->regs
[1].sien
= (val
>> 16) & 0x3eed;
207 static const MemoryRegionOps sii3112_reg_ops
= {
208 .read
= sii3112_reg_read
,
209 .write
= sii3112_reg_write
,
210 .endianness
= DEVICE_LITTLE_ENDIAN
,
213 /* the PCI irq level is the logical OR of the two channels */
214 static void sii3112_update_irq(SiI3112PCIState
*s
)
218 for (i
= 0; i
< 2; i
++) {
219 set
|= s
->regs
[i
].confstat
& (1UL << 11);
221 pci_set_irq(PCI_DEVICE(s
), (set
? 1 : 0));
224 static void sii3112_set_irq(void *opaque
, int channel
, int level
)
226 SiI3112PCIState
*s
= opaque
;
228 trace_sii3112_set_irq(channel
, level
);
230 s
->regs
[channel
].confstat
|= (1UL << 11);
232 s
->regs
[channel
].confstat
&= ~(1UL << 11);
235 sii3112_update_irq(s
);
238 static void sii3112_reset(DeviceState
*dev
)
240 SiI3112PCIState
*s
= SII3112_PCI(dev
);
243 for (i
= 0; i
< 2; i
++) {
244 s
->regs
[i
].confstat
= 0x6515 << 16;
245 ide_bus_reset(&s
->i
.bus
[i
]);
249 static void sii3112_pci_realize(PCIDevice
*dev
, Error
**errp
)
251 SiI3112PCIState
*d
= SII3112_PCI(dev
);
252 PCIIDEState
*s
= PCI_IDE(dev
);
253 DeviceState
*ds
= DEVICE(dev
);
257 pci_config_set_interrupt_pin(dev
->config
, 1);
258 pci_set_byte(dev
->config
+ PCI_CACHE_LINE_SIZE
, 8);
260 /* BAR5 is in PCI memory space */
261 memory_region_init_io(&d
->mmio
, OBJECT(d
), &sii3112_reg_ops
, d
,
262 "sii3112.bar5", 0x200);
263 pci_register_bar(dev
, 5, PCI_BASE_ADDRESS_SPACE_MEMORY
, &d
->mmio
);
265 /* BAR0-BAR4 are PCI I/O space aliases into BAR5 */
266 mr
= g_new(MemoryRegion
, 1);
267 memory_region_init_alias(mr
, OBJECT(d
), "sii3112.bar0", &d
->mmio
, 0x80, 8);
268 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, mr
);
269 mr
= g_new(MemoryRegion
, 1);
270 memory_region_init_alias(mr
, OBJECT(d
), "sii3112.bar1", &d
->mmio
, 0x88, 4);
271 pci_register_bar(dev
, 1, PCI_BASE_ADDRESS_SPACE_IO
, mr
);
272 mr
= g_new(MemoryRegion
, 1);
273 memory_region_init_alias(mr
, OBJECT(d
), "sii3112.bar2", &d
->mmio
, 0xc0, 8);
274 pci_register_bar(dev
, 2, PCI_BASE_ADDRESS_SPACE_IO
, mr
);
275 mr
= g_new(MemoryRegion
, 1);
276 memory_region_init_alias(mr
, OBJECT(d
), "sii3112.bar3", &d
->mmio
, 0xc8, 4);
277 pci_register_bar(dev
, 3, PCI_BASE_ADDRESS_SPACE_IO
, mr
);
278 mr
= g_new(MemoryRegion
, 1);
279 memory_region_init_alias(mr
, OBJECT(d
), "sii3112.bar4", &d
->mmio
, 0, 16);
280 pci_register_bar(dev
, 4, PCI_BASE_ADDRESS_SPACE_IO
, mr
);
282 qdev_init_gpio_in(ds
, sii3112_set_irq
, 2);
283 for (i
= 0; i
< 2; i
++) {
284 ide_bus_init(&s
->bus
[i
], sizeof(s
->bus
[i
]), ds
, i
, 1);
285 ide_bus_init_output_irq(&s
->bus
[i
], qdev_get_gpio_in(ds
, i
));
287 bmdma_init(&s
->bus
[i
], &s
->bmdma
[i
], s
);
288 ide_bus_register_restart_cb(&s
->bus
[i
]);
292 static void sii3112_pci_class_init(ObjectClass
*klass
, void *data
)
294 DeviceClass
*dc
= DEVICE_CLASS(klass
);
295 PCIDeviceClass
*pd
= PCI_DEVICE_CLASS(klass
);
297 pd
->vendor_id
= 0x1095;
298 pd
->device_id
= 0x3112;
299 pd
->class_id
= PCI_CLASS_STORAGE_RAID
;
301 pd
->realize
= sii3112_pci_realize
;
302 dc
->reset
= sii3112_reset
;
303 dc
->desc
= "SiI3112A SATA controller";
304 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
307 static const TypeInfo sii3112_pci_info
= {
308 .name
= TYPE_SII3112_PCI
,
309 .parent
= TYPE_PCI_IDE
,
310 .instance_size
= sizeof(SiI3112PCIState
),
311 .class_init
= sii3112_pci_class_init
,
314 static void sii3112_register_types(void)
316 type_register_static(&sii3112_pci_info
);
319 type_init(sii3112_register_types
)