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"
18 #include "sysemu/reset.h"
21 #define TYPE_SII3112_PCI "sii3112"
22 #define SII3112_PCI(obj) OBJECT_CHECK(SiI3112PCIState, (obj), \
25 typedef struct SiI3112Regs
{
32 typedef struct SiI3112PCIState
{
38 /* The sii3112_reg_read and sii3112_reg_write functions implement the
39 * Internal Register Space - BAR5 (section 6.7 of the data sheet).
42 static uint64_t sii3112_reg_read(void *opaque
, hwaddr addr
,
45 SiI3112PCIState
*d
= opaque
;
50 val
= d
->i
.bmdma
[0].cmd
;
53 val
= d
->regs
[0].swdata
;
56 val
= d
->i
.bmdma
[0].status
;
62 val
= bmdma_addr_ioport_ops
.read(&d
->i
.bmdma
[0], addr
- 4, size
);
65 val
= d
->i
.bmdma
[1].cmd
;
68 val
= d
->regs
[1].swdata
;
71 val
= d
->i
.bmdma
[1].status
;
77 val
= bmdma_addr_ioport_ops
.read(&d
->i
.bmdma
[1], addr
- 12, size
);
80 val
= d
->i
.bmdma
[0].cmd
;
81 val
|= (d
->regs
[0].confstat
& (1UL << 11) ? (1 << 4) : 0); /*SATAINT0*/
82 val
|= (d
->regs
[1].confstat
& (1UL << 11) ? (1 << 6) : 0); /*SATAINT1*/
83 val
|= (d
->i
.bmdma
[1].status
& BM_STATUS_INT
? (1 << 14) : 0);
84 val
|= (uint32_t)d
->i
.bmdma
[0].status
<< 16;
85 val
|= (uint32_t)d
->i
.bmdma
[1].status
<< 24;
88 val
= d
->i
.bmdma
[1].cmd
;
89 val
|= (d
->regs
[1].confstat
& (1UL << 11) ? (1 << 4) : 0);
90 val
|= (uint32_t)d
->i
.bmdma
[1].status
<< 16;
93 val
= pci_ide_data_le_ops
.read(&d
->i
.bus
[0], addr
- 0x80, size
);
96 val
= pci_ide_cmd_le_ops
.read(&d
->i
.bus
[0], 2, size
);
99 val
= d
->regs
[0].confstat
;
102 val
= pci_ide_data_le_ops
.read(&d
->i
.bus
[1], addr
- 0xc0, size
);
105 val
= pci_ide_cmd_le_ops
.read(&d
->i
.bus
[1], 2, size
);
108 val
= d
->regs
[1].confstat
;
111 val
= d
->regs
[0].scontrol
;
114 val
= (d
->i
.bus
[0].ifs
[0].blk
) ? 0x113 : 0;
117 val
= (uint32_t)d
->regs
[0].sien
<< 16;
120 val
= d
->regs
[1].scontrol
;
123 val
= (d
->i
.bus
[1].ifs
[0].blk
) ? 0x113 : 0;
126 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 d
->i
.bmdma
[0].status
= (val
& 0x60) | (d
->i
.bmdma
[0].status
& 1) |
153 (d
->i
.bmdma
[0].status
& ~val
& 6);
156 bmdma_addr_ioport_ops
.write(&d
->i
.bmdma
[0], addr
- 4, val
, size
);
160 bmdma_cmd_writeb(&d
->i
.bmdma
[1], val
);
164 d
->regs
[1].swdata
= val
& 0x3f;
168 d
->i
.bmdma
[1].status
= (val
& 0x60) | (d
->i
.bmdma
[1].status
& 1) |
169 (d
->i
.bmdma
[1].status
& ~val
& 6);
172 bmdma_addr_ioport_ops
.write(&d
->i
.bmdma
[1], addr
- 12, val
, size
);
175 pci_ide_data_le_ops
.write(&d
->i
.bus
[0], addr
- 0x80, val
, size
);
178 pci_ide_cmd_le_ops
.write(&d
->i
.bus
[0], 2, val
, size
);
181 pci_ide_data_le_ops
.write(&d
->i
.bus
[1], addr
- 0xc0, val
, size
);
184 pci_ide_cmd_le_ops
.write(&d
->i
.bus
[1], 2, val
, size
);
187 d
->regs
[0].scontrol
= val
& 0xfff;
189 ide_bus_reset(&d
->i
.bus
[0]);
193 d
->regs
[0].sien
= (val
>> 16) & 0x3eed;
196 d
->regs
[1].scontrol
= val
& 0xfff;
198 ide_bus_reset(&d
->i
.bus
[1]);
202 d
->regs
[1].sien
= (val
>> 16) & 0x3eed;
209 static const MemoryRegionOps sii3112_reg_ops
= {
210 .read
= sii3112_reg_read
,
211 .write
= sii3112_reg_write
,
212 .endianness
= DEVICE_LITTLE_ENDIAN
,
215 /* the PCI irq level is the logical OR of the two channels */
216 static void sii3112_update_irq(SiI3112PCIState
*s
)
220 for (i
= 0; i
< 2; i
++) {
221 set
|= s
->regs
[i
].confstat
& (1UL << 11);
223 pci_set_irq(PCI_DEVICE(s
), (set
? 1 : 0));
226 static void sii3112_set_irq(void *opaque
, int channel
, int level
)
228 SiI3112PCIState
*s
= opaque
;
230 trace_sii3112_set_irq(channel
, level
);
232 s
->regs
[channel
].confstat
|= (1UL << 11);
234 s
->regs
[channel
].confstat
&= ~(1UL << 11);
237 sii3112_update_irq(s
);
240 static void sii3112_reset(void *opaque
)
242 SiI3112PCIState
*s
= opaque
;
245 for (i
= 0; i
< 2; i
++) {
246 s
->regs
[i
].confstat
= 0x6515 << 16;
247 ide_bus_reset(&s
->i
.bus
[i
]);
251 static void sii3112_pci_realize(PCIDevice
*dev
, Error
**errp
)
253 SiI3112PCIState
*d
= SII3112_PCI(dev
);
254 PCIIDEState
*s
= PCI_IDE(dev
);
259 pci_config_set_interrupt_pin(dev
->config
, 1);
260 pci_set_byte(dev
->config
+ PCI_CACHE_LINE_SIZE
, 8);
262 /* BAR5 is in PCI memory space */
263 memory_region_init_io(&d
->mmio
, OBJECT(d
), &sii3112_reg_ops
, d
,
264 "sii3112.bar5", 0x200);
265 pci_register_bar(dev
, 5, PCI_BASE_ADDRESS_SPACE_MEMORY
, &d
->mmio
);
267 /* BAR0-BAR4 are PCI I/O space aliases into BAR5 */
268 mr
= g_new(MemoryRegion
, 1);
269 memory_region_init_alias(mr
, OBJECT(d
), "sii3112.bar0", &d
->mmio
, 0x80, 8);
270 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, mr
);
271 mr
= g_new(MemoryRegion
, 1);
272 memory_region_init_alias(mr
, OBJECT(d
), "sii3112.bar1", &d
->mmio
, 0x88, 4);
273 pci_register_bar(dev
, 1, PCI_BASE_ADDRESS_SPACE_IO
, mr
);
274 mr
= g_new(MemoryRegion
, 1);
275 memory_region_init_alias(mr
, OBJECT(d
), "sii3112.bar2", &d
->mmio
, 0xc0, 8);
276 pci_register_bar(dev
, 2, PCI_BASE_ADDRESS_SPACE_IO
, mr
);
277 mr
= g_new(MemoryRegion
, 1);
278 memory_region_init_alias(mr
, OBJECT(d
), "sii3112.bar3", &d
->mmio
, 0xc8, 4);
279 pci_register_bar(dev
, 3, PCI_BASE_ADDRESS_SPACE_IO
, mr
);
280 mr
= g_new(MemoryRegion
, 1);
281 memory_region_init_alias(mr
, OBJECT(d
), "sii3112.bar4", &d
->mmio
, 0, 16);
282 pci_register_bar(dev
, 4, PCI_BASE_ADDRESS_SPACE_IO
, mr
);
284 irq
= qemu_allocate_irqs(sii3112_set_irq
, d
, 2);
285 for (i
= 0; i
< 2; i
++) {
286 ide_bus_new(&s
->bus
[i
], sizeof(s
->bus
[i
]), DEVICE(dev
), i
, 1);
287 ide_init2(&s
->bus
[i
], irq
[i
]);
289 bmdma_init(&s
->bus
[i
], &s
->bmdma
[i
], s
);
290 s
->bmdma
[i
].bus
= &s
->bus
[i
];
291 ide_register_restart_cb(&s
->bus
[i
]);
293 qemu_register_reset(sii3112_reset
, s
);
296 static void sii3112_pci_class_init(ObjectClass
*klass
, void *data
)
298 DeviceClass
*dc
= DEVICE_CLASS(klass
);
299 PCIDeviceClass
*pd
= PCI_DEVICE_CLASS(klass
);
301 pd
->vendor_id
= 0x1095;
302 pd
->device_id
= 0x3112;
303 pd
->class_id
= PCI_CLASS_STORAGE_RAID
;
305 pd
->realize
= sii3112_pci_realize
;
306 dc
->desc
= "SiI3112A SATA controller";
307 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
310 static const TypeInfo sii3112_pci_info
= {
311 .name
= TYPE_SII3112_PCI
,
312 .parent
= TYPE_PCI_IDE
,
313 .instance_size
= sizeof(SiI3112PCIState
),
314 .class_init
= sii3112_pci_class_init
,
317 static void sii3112_register_types(void)
319 type_register_static(&sii3112_pci_info
);
322 type_init(sii3112_register_types
)