11e5459a3Sbalrog /* 21e5459a3Sbalrog * SuperH on-chip PCIC emulation. 31e5459a3Sbalrog * 41e5459a3Sbalrog * Copyright (c) 2008 Takashi YOSHII 51e5459a3Sbalrog * 61e5459a3Sbalrog * Permission is hereby granted, free of charge, to any person obtaining a copy 71e5459a3Sbalrog * of this software and associated documentation files (the "Software"), to deal 81e5459a3Sbalrog * in the Software without restriction, including without limitation the rights 91e5459a3Sbalrog * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 101e5459a3Sbalrog * copies of the Software, and to permit persons to whom the Software is 111e5459a3Sbalrog * furnished to do so, subject to the following conditions: 121e5459a3Sbalrog * 131e5459a3Sbalrog * The above copyright notice and this permission notice shall be included in 141e5459a3Sbalrog * all copies or substantial portions of the Software. 151e5459a3Sbalrog * 161e5459a3Sbalrog * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 171e5459a3Sbalrog * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 181e5459a3Sbalrog * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 191e5459a3Sbalrog * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 201e5459a3Sbalrog * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 211e5459a3Sbalrog * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 221e5459a3Sbalrog * THE SOFTWARE. 231e5459a3Sbalrog */ 2483c9f4caSPaolo Bonzini #include "hw/sysbus.h" 25*0d09e41aSPaolo Bonzini #include "hw/sh4/sh.h" 2683c9f4caSPaolo Bonzini #include "hw/pci/pci.h" 2783c9f4caSPaolo Bonzini #include "hw/pci/pci_host.h" 281de7afc9SPaolo Bonzini #include "qemu/bswap.h" 29022c62cbSPaolo Bonzini #include "exec/address-spaces.h" 301e5459a3Sbalrog 31cf154394SAurelien Jarno typedef struct SHPCIState { 32cf154394SAurelien Jarno SysBusDevice busdev; 331e5459a3Sbalrog PCIBus *bus; 341e5459a3Sbalrog PCIDevice *dev; 35cf154394SAurelien Jarno qemu_irq irq[4]; 36fb57117aSAvi Kivity MemoryRegion memconfig_p4; 37fb57117aSAvi Kivity MemoryRegion memconfig_a7; 38fb57117aSAvi Kivity MemoryRegion isa; 391e5459a3Sbalrog uint32_t par; 401e5459a3Sbalrog uint32_t mbr; 411e5459a3Sbalrog uint32_t iobr; 42cf154394SAurelien Jarno } SHPCIState; 431e5459a3Sbalrog 44a8170e5eSAvi Kivity static void sh_pci_reg_write (void *p, hwaddr addr, uint64_t val, 45fb57117aSAvi Kivity unsigned size) 461e5459a3Sbalrog { 47cf154394SAurelien Jarno SHPCIState *pcic = p; 481e5459a3Sbalrog switch(addr) { 491e5459a3Sbalrog case 0 ... 0xfc: 501e5459a3Sbalrog cpu_to_le32w((uint32_t*)(pcic->dev->config + addr), val); 511e5459a3Sbalrog break; 521e5459a3Sbalrog case 0x1c0: 531e5459a3Sbalrog pcic->par = val; 541e5459a3Sbalrog break; 551e5459a3Sbalrog case 0x1c4: 565ba9e952SAurelien Jarno pcic->mbr = val & 0xff000001; 571e5459a3Sbalrog break; 581e5459a3Sbalrog case 0x1c8: 595ba9e952SAurelien Jarno if ((val & 0xfffc0000) != (pcic->iobr & 0xfffc0000)) { 60fb57117aSAvi Kivity memory_region_del_subregion(get_system_memory(), &pcic->isa); 615ba9e952SAurelien Jarno pcic->iobr = val & 0xfffc0001; 62fb57117aSAvi Kivity memory_region_add_subregion(get_system_memory(), 63fb57117aSAvi Kivity pcic->iobr & 0xfffc0000, &pcic->isa); 645ba9e952SAurelien Jarno } 651e5459a3Sbalrog break; 661e5459a3Sbalrog case 0x220: 671e5459a3Sbalrog pci_data_write(pcic->bus, pcic->par, val, 4); 681e5459a3Sbalrog break; 691e5459a3Sbalrog } 701e5459a3Sbalrog } 711e5459a3Sbalrog 72a8170e5eSAvi Kivity static uint64_t sh_pci_reg_read (void *p, hwaddr addr, 73fb57117aSAvi Kivity unsigned size) 741e5459a3Sbalrog { 75cf154394SAurelien Jarno SHPCIState *pcic = p; 761e5459a3Sbalrog switch(addr) { 771e5459a3Sbalrog case 0 ... 0xfc: 781e5459a3Sbalrog return le32_to_cpup((uint32_t*)(pcic->dev->config + addr)); 791e5459a3Sbalrog case 0x1c0: 801e5459a3Sbalrog return pcic->par; 815ba9e952SAurelien Jarno case 0x1c4: 825ba9e952SAurelien Jarno return pcic->mbr; 835ba9e952SAurelien Jarno case 0x1c8: 845ba9e952SAurelien Jarno return pcic->iobr; 851e5459a3Sbalrog case 0x220: 861e5459a3Sbalrog return pci_data_read(pcic->bus, pcic->par, 4); 871e5459a3Sbalrog } 881e5459a3Sbalrog return 0; 891e5459a3Sbalrog } 901e5459a3Sbalrog 91fb57117aSAvi Kivity static const MemoryRegionOps sh_pci_reg_ops = { 92fb57117aSAvi Kivity .read = sh_pci_reg_read, 93fb57117aSAvi Kivity .write = sh_pci_reg_write, 94fb57117aSAvi Kivity .endianness = DEVICE_NATIVE_ENDIAN, 95fb57117aSAvi Kivity .valid = { 96fb57117aSAvi Kivity .min_access_size = 4, 97fb57117aSAvi Kivity .max_access_size = 4, 98fb57117aSAvi Kivity }, 991e5459a3Sbalrog }; 1001e5459a3Sbalrog 101cf154394SAurelien Jarno static int sh_pci_map_irq(PCIDevice *d, int irq_num) 1021e5459a3Sbalrog { 103cf154394SAurelien Jarno return (d->devfn >> 3); 1041e5459a3Sbalrog } 105cf154394SAurelien Jarno 106cf154394SAurelien Jarno static void sh_pci_set_irq(void *opaque, int irq_num, int level) 107cf154394SAurelien Jarno { 108cf154394SAurelien Jarno qemu_irq *pic = opaque; 109cf154394SAurelien Jarno 110cf154394SAurelien Jarno qemu_set_irq(pic[irq_num], level); 111cf154394SAurelien Jarno } 112cf154394SAurelien Jarno 113999e12bbSAnthony Liguori static int sh_pci_device_init(SysBusDevice *dev) 114cf154394SAurelien Jarno { 115cf154394SAurelien Jarno SHPCIState *s; 116cf154394SAurelien Jarno int i; 117cf154394SAurelien Jarno 118cf154394SAurelien Jarno s = FROM_SYSBUS(SHPCIState, dev); 119cf154394SAurelien Jarno for (i = 0; i < 4; i++) { 120cf154394SAurelien Jarno sysbus_init_irq(dev, &s->irq[i]); 121cf154394SAurelien Jarno } 122cf154394SAurelien Jarno s->bus = pci_register_bus(&s->busdev.qdev, "pci", 123cf154394SAurelien Jarno sh_pci_set_irq, sh_pci_map_irq, 124aee97b84SAvi Kivity s->irq, 125aee97b84SAvi Kivity get_system_memory(), 126aee97b84SAvi Kivity get_system_io(), 12760a0e443SAlex Williamson PCI_DEVFN(0, 0), 4, TYPE_PCI_BUS); 128fb57117aSAvi Kivity memory_region_init_io(&s->memconfig_p4, &sh_pci_reg_ops, s, 129fb57117aSAvi Kivity "sh_pci", 0x224); 13073c92f9aSAvi Kivity memory_region_init_alias(&s->memconfig_a7, "sh_pci.2", &s->memconfig_p4, 131fb57117aSAvi Kivity 0, 0x224); 132fb57117aSAvi Kivity isa_mmio_setup(&s->isa, 0x40000); 1338c106233SBenoît Canet sysbus_init_mmio(dev, &s->memconfig_p4); 134750ecd44SAvi Kivity sysbus_init_mmio(dev, &s->memconfig_a7); 1358c106233SBenoît Canet s->iobr = 0xfe240000; 1368c106233SBenoît Canet memory_region_add_subregion(get_system_memory(), s->iobr, &s->isa); 1378c106233SBenoît Canet 138cf154394SAurelien Jarno s->dev = pci_create_simple(s->bus, PCI_DEVFN(0, 0), "sh_pci_host"); 139cf154394SAurelien Jarno return 0; 140cf154394SAurelien Jarno } 141cf154394SAurelien Jarno 142cf154394SAurelien Jarno static int sh_pci_host_init(PCIDevice *d) 143cf154394SAurelien Jarno { 144cf154394SAurelien Jarno pci_set_word(d->config + PCI_COMMAND, PCI_COMMAND_WAIT); 145cf154394SAurelien Jarno pci_set_word(d->config + PCI_STATUS, PCI_STATUS_CAP_LIST | 146cf154394SAurelien Jarno PCI_STATUS_FAST_BACK | PCI_STATUS_DEVSEL_MEDIUM); 147cf154394SAurelien Jarno return 0; 148cf154394SAurelien Jarno } 149cf154394SAurelien Jarno 15040021f08SAnthony Liguori static void sh_pci_host_class_init(ObjectClass *klass, void *data) 15140021f08SAnthony Liguori { 15240021f08SAnthony Liguori PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 15340021f08SAnthony Liguori 15440021f08SAnthony Liguori k->init = sh_pci_host_init; 15540021f08SAnthony Liguori k->vendor_id = PCI_VENDOR_ID_HITACHI; 15640021f08SAnthony Liguori k->device_id = PCI_DEVICE_ID_HITACHI_SH7751R; 15740021f08SAnthony Liguori } 15840021f08SAnthony Liguori 1598c43a6f0SAndreas Färber static const TypeInfo sh_pci_host_info = { 16040021f08SAnthony Liguori .name = "sh_pci_host", 16139bffca2SAnthony Liguori .parent = TYPE_PCI_DEVICE, 16239bffca2SAnthony Liguori .instance_size = sizeof(PCIDevice), 16340021f08SAnthony Liguori .class_init = sh_pci_host_class_init, 164cf154394SAurelien Jarno }; 165cf154394SAurelien Jarno 166999e12bbSAnthony Liguori static void sh_pci_device_class_init(ObjectClass *klass, void *data) 167999e12bbSAnthony Liguori { 168999e12bbSAnthony Liguori SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass); 169999e12bbSAnthony Liguori 170999e12bbSAnthony Liguori sdc->init = sh_pci_device_init; 171999e12bbSAnthony Liguori } 172999e12bbSAnthony Liguori 1738c43a6f0SAndreas Färber static const TypeInfo sh_pci_device_info = { 174999e12bbSAnthony Liguori .name = "sh_pci", 17539bffca2SAnthony Liguori .parent = TYPE_SYS_BUS_DEVICE, 17639bffca2SAnthony Liguori .instance_size = sizeof(SHPCIState), 177999e12bbSAnthony Liguori .class_init = sh_pci_device_class_init, 178999e12bbSAnthony Liguori }; 179999e12bbSAnthony Liguori 18083f7d43aSAndreas Färber static void sh_pci_register_types(void) 181cf154394SAurelien Jarno { 18239bffca2SAnthony Liguori type_register_static(&sh_pci_device_info); 18339bffca2SAnthony Liguori type_register_static(&sh_pci_host_info); 184cf154394SAurelien Jarno } 185cf154394SAurelien Jarno 18683f7d43aSAndreas Färber type_init(sh_pci_register_types) 187