2010-06-29 06:49:50 +04:00
|
|
|
/*
|
|
|
|
* QEMU IDE Emulation: PCI VIA82C686B support.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003 Fabrice Bellard
|
|
|
|
* Copyright (c) 2006 Openedhand Ltd.
|
|
|
|
* Copyright (c) 2010 Huacai Chen <zltjiangshi@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2019-05-23 17:35:07 +03:00
|
|
|
|
2016-01-26 21:17:09 +03:00
|
|
|
#include "qemu/osdep.h"
|
2016-06-22 20:11:19 +03:00
|
|
|
#include "hw/pci/pci.h"
|
2019-08-12 08:23:45 +03:00
|
|
|
#include "migration/vmstate.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2023-11-16 13:33:55 +03:00
|
|
|
#include "qemu/range.h"
|
2012-12-17 21:20:04 +04:00
|
|
|
#include "sysemu/dma.h"
|
2021-10-15 04:06:20 +03:00
|
|
|
#include "hw/isa/vt82c686.h"
|
2016-06-22 20:11:19 +03:00
|
|
|
#include "hw/ide/pci.h"
|
2023-06-01 00:10:38 +03:00
|
|
|
#include "hw/irq.h"
|
2017-09-18 22:01:25 +03:00
|
|
|
#include "trace.h"
|
2010-06-29 06:49:50 +04:00
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static uint64_t bmdma_read(void *opaque, hwaddr addr,
|
2011-08-08 17:09:11 +04:00
|
|
|
unsigned size)
|
2010-06-29 06:49:50 +04:00
|
|
|
{
|
|
|
|
BMDMAState *bm = opaque;
|
|
|
|
uint32_t val;
|
|
|
|
|
2011-08-08 17:09:11 +04:00
|
|
|
if (size != 1) {
|
|
|
|
return ((uint64_t)1 << (size * 8)) - 1;
|
|
|
|
}
|
|
|
|
|
2010-06-29 06:49:50 +04:00
|
|
|
switch (addr & 3) {
|
|
|
|
case 0:
|
|
|
|
val = bm->cmd;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
val = bm->status;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
val = 0xff;
|
|
|
|
break;
|
|
|
|
}
|
2017-09-18 22:01:25 +03:00
|
|
|
|
|
|
|
trace_bmdma_read_via(addr, val);
|
2010-06-29 06:49:50 +04:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void bmdma_write(void *opaque, hwaddr addr,
|
2011-08-08 17:09:11 +04:00
|
|
|
uint64_t val, unsigned size)
|
2010-06-29 06:49:50 +04:00
|
|
|
{
|
|
|
|
BMDMAState *bm = opaque;
|
2011-08-08 17:09:11 +04:00
|
|
|
|
|
|
|
if (size != 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-18 22:01:25 +03:00
|
|
|
trace_bmdma_write_via(addr, val);
|
2010-06-29 06:49:50 +04:00
|
|
|
switch (addr & 3) {
|
2011-08-08 17:09:11 +04:00
|
|
|
case 0:
|
2012-07-08 10:56:53 +04:00
|
|
|
bmdma_cmd_writeb(bm, val);
|
|
|
|
break;
|
2010-06-29 06:49:50 +04:00
|
|
|
case 2:
|
2023-06-01 00:10:41 +03:00
|
|
|
bmdma_status_writeb(bm, val);
|
2010-06-29 06:49:50 +04:00
|
|
|
break;
|
|
|
|
default:;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-05 14:19:07 +04:00
|
|
|
static const MemoryRegionOps via_bmdma_ops = {
|
2011-08-08 17:09:11 +04:00
|
|
|
.read = bmdma_read,
|
|
|
|
.write = bmdma_write,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void bmdma_setup_bar(PCIIDEState *d)
|
2010-06-29 06:49:50 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init(&d->bmdma_bar, OBJECT(d), "via-bmdma-container", 16);
|
2021-03-24 20:47:59 +03:00
|
|
|
for (i = 0; i < ARRAY_SIZE(d->bmdma); i++) {
|
2010-06-29 06:49:50 +04:00
|
|
|
BMDMAState *bm = &d->bmdma[i];
|
|
|
|
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init_io(&bm->extra_io, OBJECT(d), &via_bmdma_ops, bm,
|
2011-08-08 17:09:11 +04:00
|
|
|
"via-bmdma", 4);
|
|
|
|
memory_region_add_subregion(&d->bmdma_bar, i * 8, &bm->extra_io);
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init_io(&bm->addr_ioport, OBJECT(d),
|
|
|
|
&bmdma_addr_ioport_ops, bm, "bmdma", 4);
|
2011-08-08 17:09:11 +04:00
|
|
|
memory_region_add_subregion(&d->bmdma_bar, i * 8 + 4, &bm->addr_ioport);
|
2010-06-29 06:49:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 22:52:12 +03:00
|
|
|
static void via_ide_set_irq(void *opaque, int n, int level)
|
|
|
|
{
|
2023-06-01 00:10:38 +03:00
|
|
|
PCIIDEState *s = opaque;
|
|
|
|
PCIDevice *d = PCI_DEVICE(s);
|
2019-01-25 22:52:12 +03:00
|
|
|
|
|
|
|
if (level) {
|
|
|
|
d->config[0x70 + n * 8] |= 0x80;
|
|
|
|
} else {
|
|
|
|
d->config[0x70 + n * 8] &= ~0x80;
|
|
|
|
}
|
|
|
|
|
2023-06-01 00:10:38 +03:00
|
|
|
qemu_set_irq(s->isa_irq[n], level);
|
2019-01-25 22:52:12 +03:00
|
|
|
}
|
|
|
|
|
2019-10-10 16:15:24 +03:00
|
|
|
static void via_ide_reset(DeviceState *dev)
|
2010-06-29 06:49:50 +04:00
|
|
|
{
|
2019-10-10 16:15:24 +03:00
|
|
|
PCIIDEState *d = PCI_IDE(dev);
|
|
|
|
PCIDevice *pd = PCI_DEVICE(dev);
|
2013-07-17 20:44:48 +04:00
|
|
|
uint8_t *pci_conf = pd->config;
|
2010-06-29 06:49:50 +04:00
|
|
|
int i;
|
|
|
|
|
2021-03-24 20:47:59 +03:00
|
|
|
for (i = 0; i < ARRAY_SIZE(d->bus); i++) {
|
2010-06-29 06:49:50 +04:00
|
|
|
ide_bus_reset(&d->bus[i]);
|
|
|
|
}
|
|
|
|
|
2023-11-16 13:33:55 +03:00
|
|
|
pci_config_set_prog_interface(pci_conf, 0x8a); /* legacy mode */
|
|
|
|
pci_ide_update_mode(d);
|
|
|
|
|
2019-01-25 22:52:12 +03:00
|
|
|
pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_WAIT);
|
2010-06-29 06:49:50 +04:00
|
|
|
pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_FAST_BACK |
|
|
|
|
PCI_STATUS_DEVSEL_MEDIUM);
|
|
|
|
|
2023-11-16 13:33:55 +03:00
|
|
|
pci_set_byte(pci_conf + PCI_INTERRUPT_LINE, 0xe);
|
2010-06-29 06:49:50 +04:00
|
|
|
|
|
|
|
/* IDE chip enable, IDE configuration 1/2, IDE FIFO Configuration*/
|
|
|
|
pci_set_long(pci_conf + 0x40, 0x0a090600);
|
|
|
|
/* IDE misc configuration 1/2/3 */
|
|
|
|
pci_set_long(pci_conf + 0x44, 0x00c00068);
|
|
|
|
/* IDE Timing control */
|
|
|
|
pci_set_long(pci_conf + 0x48, 0xa8a8a8a8);
|
|
|
|
/* IDE Address Setup Time */
|
|
|
|
pci_set_long(pci_conf + 0x4c, 0x000000ff);
|
|
|
|
/* UltraDMA Extended Timing Control*/
|
|
|
|
pci_set_long(pci_conf + 0x50, 0x07070707);
|
|
|
|
/* UltraDMA FIFO Control */
|
|
|
|
pci_set_long(pci_conf + 0x54, 0x00000004);
|
|
|
|
/* IDE primary sector size */
|
|
|
|
pci_set_long(pci_conf + 0x60, 0x00000200);
|
|
|
|
/* IDE secondary sector size */
|
|
|
|
pci_set_long(pci_conf + 0x68, 0x00000200);
|
|
|
|
/* PCI PM Block */
|
|
|
|
pci_set_long(pci_conf + 0xc0, 0x00020001);
|
|
|
|
}
|
|
|
|
|
2023-11-16 13:33:55 +03:00
|
|
|
static uint32_t via_ide_cfg_read(PCIDevice *pd, uint32_t addr, int len)
|
|
|
|
{
|
|
|
|
uint32_t val = pci_default_read_config(pd, addr, len);
|
|
|
|
uint8_t mode = pd->config[PCI_CLASS_PROG];
|
|
|
|
|
2023-11-25 17:01:35 +03:00
|
|
|
if ((mode & 0xf) == 0xa) {
|
|
|
|
if (ranges_overlap(addr, len, PCI_BASE_ADDRESS_0, 16)) {
|
|
|
|
/* BARs 0-3 always read back zero in legacy mode */
|
|
|
|
for (int i = addr; i < addr + len; i++) {
|
|
|
|
if (i >= PCI_BASE_ADDRESS_0 && i < PCI_BASE_ADDRESS_0 + 16) {
|
|
|
|
val &= ~(0xffULL << ((i - addr) << 3));
|
|
|
|
}
|
2023-11-16 13:33:55 +03:00
|
|
|
}
|
|
|
|
}
|
2023-11-25 17:01:35 +03:00
|
|
|
if (addr == PCI_BASE_ADDRESS_4 && val == PCI_BASE_ADDRESS_SPACE_IO) {
|
|
|
|
/* BAR4 default value if unset */
|
|
|
|
val = 0xcc00 | PCI_BASE_ADDRESS_SPACE_IO;
|
|
|
|
}
|
2023-11-16 13:33:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void via_ide_cfg_write(PCIDevice *pd, uint32_t addr,
|
|
|
|
uint32_t val, int len)
|
|
|
|
{
|
|
|
|
PCIIDEState *d = PCI_IDE(pd);
|
|
|
|
|
|
|
|
pci_default_write_config(pd, addr, val, len);
|
|
|
|
|
|
|
|
if (range_covers_byte(addr, len, PCI_CLASS_PROG)) {
|
|
|
|
pci_ide_update_mode(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 22:52:12 +03:00
|
|
|
static void via_ide_realize(PCIDevice *dev, Error **errp)
|
2010-06-29 06:49:50 +04:00
|
|
|
{
|
2013-07-17 20:44:48 +04:00
|
|
|
PCIIDEState *d = PCI_IDE(dev);
|
2020-03-25 00:05:18 +03:00
|
|
|
DeviceState *ds = DEVICE(dev);
|
2013-07-17 20:44:48 +04:00
|
|
|
uint8_t *pci_conf = dev->config;
|
2019-01-25 22:52:11 +03:00
|
|
|
int i;
|
2010-06-29 06:49:50 +04:00
|
|
|
|
|
|
|
pci_set_long(pci_conf + PCI_CAPABILITY_LIST, 0x000000c0);
|
2020-03-13 11:24:41 +03:00
|
|
|
dev->wmask[PCI_INTERRUPT_LINE] = 0;
|
2020-03-13 11:24:43 +03:00
|
|
|
dev->wmask[PCI_CLASS_PROG] = 5;
|
2010-06-29 06:49:50 +04:00
|
|
|
|
2019-01-25 22:52:12 +03:00
|
|
|
memory_region_init_io(&d->data_bar[0], OBJECT(d), &pci_ide_data_le_ops,
|
|
|
|
&d->bus[0], "via-ide0-data", 8);
|
|
|
|
pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &d->data_bar[0]);
|
|
|
|
|
|
|
|
memory_region_init_io(&d->cmd_bar[0], OBJECT(d), &pci_ide_cmd_le_ops,
|
|
|
|
&d->bus[0], "via-ide0-cmd", 4);
|
|
|
|
pci_register_bar(dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &d->cmd_bar[0]);
|
|
|
|
|
|
|
|
memory_region_init_io(&d->data_bar[1], OBJECT(d), &pci_ide_data_le_ops,
|
|
|
|
&d->bus[1], "via-ide1-data", 8);
|
|
|
|
pci_register_bar(dev, 2, PCI_BASE_ADDRESS_SPACE_IO, &d->data_bar[1]);
|
|
|
|
|
|
|
|
memory_region_init_io(&d->cmd_bar[1], OBJECT(d), &pci_ide_cmd_le_ops,
|
|
|
|
&d->bus[1], "via-ide1-cmd", 4);
|
|
|
|
pci_register_bar(dev, 3, PCI_BASE_ADDRESS_SPACE_IO, &d->cmd_bar[1]);
|
|
|
|
|
2011-08-08 17:09:11 +04:00
|
|
|
bmdma_setup_bar(d);
|
2013-07-17 20:44:48 +04:00
|
|
|
pci_register_bar(dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &d->bmdma_bar);
|
2010-06-29 06:49:50 +04:00
|
|
|
|
2021-03-24 20:47:59 +03:00
|
|
|
qdev_init_gpio_in(ds, via_ide_set_irq, ARRAY_SIZE(d->bus));
|
|
|
|
for (i = 0; i < ARRAY_SIZE(d->bus); i++) {
|
|
|
|
ide_bus_init(&d->bus[i], sizeof(d->bus[i]), ds, i, MAX_IDE_DEVS);
|
2023-02-09 13:27:23 +03:00
|
|
|
ide_bus_init_output_irq(&d->bus[i], qdev_get_gpio_in(ds, i));
|
2019-01-25 22:52:11 +03:00
|
|
|
|
|
|
|
bmdma_init(&d->bus[i], &d->bmdma[i], d);
|
2023-02-14 18:33:38 +03:00
|
|
|
ide_bus_register_restart_cb(&d->bus[i]);
|
2019-01-25 22:52:11 +03:00
|
|
|
}
|
2010-06-29 06:49:50 +04:00
|
|
|
}
|
|
|
|
|
2019-01-25 22:52:12 +03:00
|
|
|
static void via_ide_exitfn(PCIDevice *dev)
|
2011-08-08 17:09:11 +04:00
|
|
|
{
|
2013-07-17 20:44:48 +04:00
|
|
|
PCIIDEState *d = PCI_IDE(dev);
|
2011-08-08 17:09:11 +04:00
|
|
|
unsigned i;
|
|
|
|
|
2021-03-24 20:47:59 +03:00
|
|
|
for (i = 0; i < ARRAY_SIZE(d->bmdma); ++i) {
|
2011-08-08 17:09:11 +04:00
|
|
|
memory_region_del_subregion(&d->bmdma_bar, &d->bmdma[i].extra_io);
|
|
|
|
memory_region_del_subregion(&d->bmdma_bar, &d->bmdma[i].addr_ioport);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-04 22:22:06 +04:00
|
|
|
static void via_ide_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2011-12-04 22:22:06 +04:00
|
|
|
PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
|
|
|
|
|
2019-10-10 16:15:24 +03:00
|
|
|
dc->reset = via_ide_reset;
|
2020-03-13 11:24:38 +03:00
|
|
|
dc->vmsd = &vmstate_ide_pci;
|
2021-10-15 12:16:54 +03:00
|
|
|
/* Reason: only works as function of VIA southbridge */
|
|
|
|
dc->user_creatable = false;
|
|
|
|
|
2023-11-16 13:33:55 +03:00
|
|
|
k->config_read = via_ide_cfg_read;
|
|
|
|
k->config_write = via_ide_cfg_write;
|
2019-01-25 22:52:12 +03:00
|
|
|
k->realize = via_ide_realize;
|
|
|
|
k->exit = via_ide_exitfn;
|
2011-12-04 22:22:06 +04:00
|
|
|
k->vendor_id = PCI_VENDOR_ID_VIA;
|
|
|
|
k->device_id = PCI_DEVICE_ID_VIA_IDE;
|
|
|
|
k->revision = 0x06;
|
|
|
|
k->class_id = PCI_CLASS_STORAGE_IDE;
|
2013-07-29 18:17:45 +04:00
|
|
|
set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
|
2011-12-04 22:22:06 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo via_ide_info = {
|
2022-09-01 14:41:19 +03:00
|
|
|
.name = TYPE_VIA_IDE,
|
2013-07-17 20:44:48 +04:00
|
|
|
.parent = TYPE_PCI_IDE,
|
2011-12-08 07:34:16 +04:00
|
|
|
.class_init = via_ide_class_init,
|
2010-06-29 06:49:50 +04:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void via_ide_register_types(void)
|
2010-06-29 06:49:50 +04:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&via_ide_info);
|
2010-06-29 06:49:50 +04:00
|
|
|
}
|
2012-02-09 18:20:55 +04:00
|
|
|
|
|
|
|
type_init(via_ide_register_types)
|