1016512f3SHuacai Chen /* 2016512f3SHuacai Chen * QEMU IDE Emulation: PCI VIA82C686B support. 3016512f3SHuacai Chen * 4016512f3SHuacai Chen * Copyright (c) 2003 Fabrice Bellard 5016512f3SHuacai Chen * Copyright (c) 2006 Openedhand Ltd. 6016512f3SHuacai Chen * Copyright (c) 2010 Huacai Chen <zltjiangshi@gmail.com> 7016512f3SHuacai Chen * 8016512f3SHuacai Chen * Permission is hereby granted, free of charge, to any person obtaining a copy 9016512f3SHuacai Chen * of this software and associated documentation files (the "Software"), to deal 10016512f3SHuacai Chen * in the Software without restriction, including without limitation the rights 11016512f3SHuacai Chen * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12016512f3SHuacai Chen * copies of the Software, and to permit persons to whom the Software is 13016512f3SHuacai Chen * furnished to do so, subject to the following conditions: 14016512f3SHuacai Chen * 15016512f3SHuacai Chen * The above copyright notice and this permission notice shall be included in 16016512f3SHuacai Chen * all copies or substantial portions of the Software. 17016512f3SHuacai Chen * 18016512f3SHuacai Chen * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19016512f3SHuacai Chen * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20016512f3SHuacai Chen * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21016512f3SHuacai Chen * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22016512f3SHuacai Chen * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23016512f3SHuacai Chen * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24016512f3SHuacai Chen * THE SOFTWARE. 25016512f3SHuacai Chen */ 26016512f3SHuacai Chen #include <hw/hw.h> 27016512f3SHuacai Chen #include <hw/pc.h> 28016512f3SHuacai Chen #include <hw/pci.h> 29016512f3SHuacai Chen #include <hw/isa.h> 30016512f3SHuacai Chen #include "block.h" 31016512f3SHuacai Chen #include "block_int.h" 32016512f3SHuacai Chen #include "sysemu.h" 33016512f3SHuacai Chen #include "dma.h" 34016512f3SHuacai Chen 35016512f3SHuacai Chen #include <hw/ide/pci.h> 36016512f3SHuacai Chen 37016512f3SHuacai Chen static uint32_t bmdma_readb(void *opaque, uint32_t addr) 38016512f3SHuacai Chen { 39016512f3SHuacai Chen BMDMAState *bm = opaque; 40016512f3SHuacai Chen uint32_t val; 41016512f3SHuacai Chen 42016512f3SHuacai Chen switch (addr & 3) { 43016512f3SHuacai Chen case 0: 44016512f3SHuacai Chen val = bm->cmd; 45016512f3SHuacai Chen break; 46016512f3SHuacai Chen case 2: 47016512f3SHuacai Chen val = bm->status; 48016512f3SHuacai Chen break; 49016512f3SHuacai Chen default: 50016512f3SHuacai Chen val = 0xff; 51016512f3SHuacai Chen break; 52016512f3SHuacai Chen } 53016512f3SHuacai Chen #ifdef DEBUG_IDE 54016512f3SHuacai Chen printf("bmdma: readb 0x%02x : 0x%02x\n", addr, val); 55016512f3SHuacai Chen #endif 56016512f3SHuacai Chen return val; 57016512f3SHuacai Chen } 58016512f3SHuacai Chen 59016512f3SHuacai Chen static void bmdma_writeb(void *opaque, uint32_t addr, uint32_t val) 60016512f3SHuacai Chen { 61016512f3SHuacai Chen BMDMAState *bm = opaque; 62016512f3SHuacai Chen #ifdef DEBUG_IDE 63016512f3SHuacai Chen printf("bmdma: writeb 0x%02x : 0x%02x\n", addr, val); 64016512f3SHuacai Chen #endif 65016512f3SHuacai Chen switch (addr & 3) { 66016512f3SHuacai Chen case 2: 67016512f3SHuacai Chen bm->status = (val & 0x60) | (bm->status & 1) | (bm->status & ~val & 0x06); 68016512f3SHuacai Chen break; 69016512f3SHuacai Chen default:; 70016512f3SHuacai Chen } 71016512f3SHuacai Chen } 72016512f3SHuacai Chen 73016512f3SHuacai Chen static void bmdma_map(PCIDevice *pci_dev, int region_num, 74016512f3SHuacai Chen pcibus_t addr, pcibus_t size, int type) 75016512f3SHuacai Chen { 76016512f3SHuacai Chen PCIIDEState *d = DO_UPCAST(PCIIDEState, dev, pci_dev); 77016512f3SHuacai Chen int i; 78016512f3SHuacai Chen 79016512f3SHuacai Chen for(i = 0;i < 2; i++) { 80016512f3SHuacai Chen BMDMAState *bm = &d->bmdma[i]; 81016512f3SHuacai Chen d->bus[i].bmdma = bm; 82016512f3SHuacai Chen bm->bus = d->bus+i; 83016512f3SHuacai Chen qemu_add_vm_change_state_handler(ide_dma_restart_cb, bm); 84016512f3SHuacai Chen 85016512f3SHuacai Chen register_ioport_write(addr, 1, 1, bmdma_cmd_writeb, bm); 86016512f3SHuacai Chen 87016512f3SHuacai Chen register_ioport_write(addr + 1, 3, 1, bmdma_writeb, bm); 88016512f3SHuacai Chen register_ioport_read(addr, 4, 1, bmdma_readb, bm); 89016512f3SHuacai Chen 90*9fbef1acSAvi Kivity iorange_init(&bm->addr_ioport, &bmdma_addr_ioport_ops, addr + 4, 4); 91*9fbef1acSAvi Kivity ioport_register(&bm->addr_ioport); 92016512f3SHuacai Chen addr += 8; 93016512f3SHuacai Chen } 94016512f3SHuacai Chen } 95016512f3SHuacai Chen 96016512f3SHuacai Chen static void via_reset(void *opaque) 97016512f3SHuacai Chen { 98016512f3SHuacai Chen PCIIDEState *d = opaque; 99016512f3SHuacai Chen uint8_t *pci_conf = d->dev.config; 100016512f3SHuacai Chen int i; 101016512f3SHuacai Chen 102016512f3SHuacai Chen for (i = 0; i < 2; i++) { 103016512f3SHuacai Chen ide_bus_reset(&d->bus[i]); 104016512f3SHuacai Chen ide_dma_reset(&d->bmdma[i]); 105016512f3SHuacai Chen } 106016512f3SHuacai Chen 107016512f3SHuacai Chen pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_WAIT); 108016512f3SHuacai Chen pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_FAST_BACK | 109016512f3SHuacai Chen PCI_STATUS_DEVSEL_MEDIUM); 110016512f3SHuacai Chen 111016512f3SHuacai Chen pci_set_long(pci_conf + PCI_BASE_ADDRESS_0, 0x000001f0); 112016512f3SHuacai Chen pci_set_long(pci_conf + PCI_BASE_ADDRESS_1, 0x000003f4); 113016512f3SHuacai Chen pci_set_long(pci_conf + PCI_BASE_ADDRESS_2, 0x00000170); 114016512f3SHuacai Chen pci_set_long(pci_conf + PCI_BASE_ADDRESS_3, 0x00000374); 115016512f3SHuacai Chen pci_set_long(pci_conf + PCI_BASE_ADDRESS_4, 0x0000cc01); /* BMIBA: 20-23h */ 116016512f3SHuacai Chen pci_set_long(pci_conf + PCI_INTERRUPT_LINE, 0x0000010e); 117016512f3SHuacai Chen 118016512f3SHuacai Chen /* IDE chip enable, IDE configuration 1/2, IDE FIFO Configuration*/ 119016512f3SHuacai Chen pci_set_long(pci_conf + 0x40, 0x0a090600); 120016512f3SHuacai Chen /* IDE misc configuration 1/2/3 */ 121016512f3SHuacai Chen pci_set_long(pci_conf + 0x44, 0x00c00068); 122016512f3SHuacai Chen /* IDE Timing control */ 123016512f3SHuacai Chen pci_set_long(pci_conf + 0x48, 0xa8a8a8a8); 124016512f3SHuacai Chen /* IDE Address Setup Time */ 125016512f3SHuacai Chen pci_set_long(pci_conf + 0x4c, 0x000000ff); 126016512f3SHuacai Chen /* UltraDMA Extended Timing Control*/ 127016512f3SHuacai Chen pci_set_long(pci_conf + 0x50, 0x07070707); 128016512f3SHuacai Chen /* UltraDMA FIFO Control */ 129016512f3SHuacai Chen pci_set_long(pci_conf + 0x54, 0x00000004); 130016512f3SHuacai Chen /* IDE primary sector size */ 131016512f3SHuacai Chen pci_set_long(pci_conf + 0x60, 0x00000200); 132016512f3SHuacai Chen /* IDE secondary sector size */ 133016512f3SHuacai Chen pci_set_long(pci_conf + 0x68, 0x00000200); 134016512f3SHuacai Chen /* PCI PM Block */ 135016512f3SHuacai Chen pci_set_long(pci_conf + 0xc0, 0x00020001); 136016512f3SHuacai Chen } 137016512f3SHuacai Chen 138016512f3SHuacai Chen /* via ide func */ 139016512f3SHuacai Chen static int vt82c686b_ide_initfn(PCIDevice *dev) 140016512f3SHuacai Chen { 141016512f3SHuacai Chen PCIIDEState *d = DO_UPCAST(PCIIDEState, dev, dev);; 142016512f3SHuacai Chen uint8_t *pci_conf = d->dev.config; 143016512f3SHuacai Chen 144016512f3SHuacai Chen pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_VIA); 145016512f3SHuacai Chen pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_VIA_IDE); 146016512f3SHuacai Chen pci_config_set_class(pci_conf, PCI_CLASS_STORAGE_IDE); 147016512f3SHuacai Chen pci_config_set_prog_interface(pci_conf, 0x8a); /* legacy ATA mode */ 148016512f3SHuacai Chen pci_config_set_revision(pci_conf,0x06); /* Revision 0.6 */ 149016512f3SHuacai Chen pci_set_long(pci_conf + PCI_CAPABILITY_LIST, 0x000000c0); 150016512f3SHuacai Chen 151016512f3SHuacai Chen qemu_register_reset(via_reset, d); 152016512f3SHuacai Chen pci_register_bar((PCIDevice *)d, 4, 0x10, 153016512f3SHuacai Chen PCI_BASE_ADDRESS_SPACE_IO, bmdma_map); 154016512f3SHuacai Chen 1551724f049SAlex Williamson vmstate_register(&dev->qdev, 0, &vmstate_ide_pci, d); 156016512f3SHuacai Chen 157016512f3SHuacai Chen ide_bus_new(&d->bus[0], &d->dev.qdev); 158016512f3SHuacai Chen ide_bus_new(&d->bus[1], &d->dev.qdev); 159016512f3SHuacai Chen ide_init2(&d->bus[0], isa_reserve_irq(14)); 160016512f3SHuacai Chen ide_init2(&d->bus[1], isa_reserve_irq(15)); 161016512f3SHuacai Chen ide_init_ioport(&d->bus[0], 0x1f0, 0x3f6); 162016512f3SHuacai Chen ide_init_ioport(&d->bus[1], 0x170, 0x376); 163016512f3SHuacai Chen 164016512f3SHuacai Chen return 0; 165016512f3SHuacai Chen } 166016512f3SHuacai Chen 167016512f3SHuacai Chen void vt82c686b_ide_init(PCIBus *bus, DriveInfo **hd_table, int devfn) 168016512f3SHuacai Chen { 169016512f3SHuacai Chen PCIDevice *dev; 170016512f3SHuacai Chen 171016512f3SHuacai Chen dev = pci_create_simple(bus, devfn, "via-ide"); 172016512f3SHuacai Chen pci_ide_create_devs(dev, hd_table); 173016512f3SHuacai Chen } 174016512f3SHuacai Chen 175016512f3SHuacai Chen static PCIDeviceInfo via_ide_info = { 176016512f3SHuacai Chen .qdev.name = "via-ide", 177016512f3SHuacai Chen .qdev.size = sizeof(PCIIDEState), 178016512f3SHuacai Chen .qdev.no_user = 1, 179016512f3SHuacai Chen .init = vt82c686b_ide_initfn, 180016512f3SHuacai Chen }; 181016512f3SHuacai Chen 182016512f3SHuacai Chen static void via_ide_register(void) 183016512f3SHuacai Chen { 184016512f3SHuacai Chen pci_qdev_register(&via_ide_info); 185016512f3SHuacai Chen } 186016512f3SHuacai Chen device_init(via_ide_register); 187