balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SuperH on-chip PCIC emulation. |
| 3 | * |
| 4 | * Copyright (c) 2008 Takashi YOSHII |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 24 | #include "sysbus.h" |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 25 | #include "sh.h" |
| 26 | #include "pci.h" |
Isaku Yamahata | b6243d9 | 2009-11-12 14:58:30 +0900 | [diff] [blame] | 27 | #include "pci_host.h" |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 28 | #include "bswap.h" |
Avi Kivity | 1e39101 | 2011-07-26 14:26:19 +0300 | [diff] [blame] | 29 | #include "exec-memory.h" |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 30 | |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 31 | typedef struct SHPCIState { |
| 32 | SysBusDevice busdev; |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 33 | PCIBus *bus; |
| 34 | PCIDevice *dev; |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 35 | qemu_irq irq[4]; |
Avi Kivity | fb57117 | 2011-08-15 17:17:30 +0300 | [diff] [blame] | 36 | MemoryRegion memconfig_p4; |
| 37 | MemoryRegion memconfig_a7; |
| 38 | MemoryRegion isa; |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 39 | uint32_t par; |
| 40 | uint32_t mbr; |
| 41 | uint32_t iobr; |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 42 | } SHPCIState; |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 43 | |
Avi Kivity | fb57117 | 2011-08-15 17:17:30 +0300 | [diff] [blame] | 44 | static void sh_pci_reg_write (void *p, target_phys_addr_t addr, uint64_t val, |
| 45 | unsigned size) |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 46 | { |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 47 | SHPCIState *pcic = p; |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 48 | switch(addr) { |
| 49 | case 0 ... 0xfc: |
| 50 | cpu_to_le32w((uint32_t*)(pcic->dev->config + addr), val); |
| 51 | break; |
| 52 | case 0x1c0: |
| 53 | pcic->par = val; |
| 54 | break; |
| 55 | case 0x1c4: |
Aurelien Jarno | 5ba9e95 | 2010-04-11 23:59:39 +0200 | [diff] [blame] | 56 | pcic->mbr = val & 0xff000001; |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 57 | break; |
| 58 | case 0x1c8: |
Aurelien Jarno | 5ba9e95 | 2010-04-11 23:59:39 +0200 | [diff] [blame] | 59 | if ((val & 0xfffc0000) != (pcic->iobr & 0xfffc0000)) { |
Avi Kivity | fb57117 | 2011-08-15 17:17:30 +0300 | [diff] [blame] | 60 | memory_region_del_subregion(get_system_memory(), &pcic->isa); |
Aurelien Jarno | 5ba9e95 | 2010-04-11 23:59:39 +0200 | [diff] [blame] | 61 | pcic->iobr = val & 0xfffc0001; |
Avi Kivity | fb57117 | 2011-08-15 17:17:30 +0300 | [diff] [blame] | 62 | memory_region_add_subregion(get_system_memory(), |
| 63 | pcic->iobr & 0xfffc0000, &pcic->isa); |
Aurelien Jarno | 5ba9e95 | 2010-04-11 23:59:39 +0200 | [diff] [blame] | 64 | } |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 65 | break; |
| 66 | case 0x220: |
| 67 | pci_data_write(pcic->bus, pcic->par, val, 4); |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | |
Avi Kivity | fb57117 | 2011-08-15 17:17:30 +0300 | [diff] [blame] | 72 | static uint64_t sh_pci_reg_read (void *p, target_phys_addr_t addr, |
| 73 | unsigned size) |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 74 | { |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 75 | SHPCIState *pcic = p; |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 76 | switch(addr) { |
| 77 | case 0 ... 0xfc: |
| 78 | return le32_to_cpup((uint32_t*)(pcic->dev->config + addr)); |
| 79 | case 0x1c0: |
| 80 | return pcic->par; |
Aurelien Jarno | 5ba9e95 | 2010-04-11 23:59:39 +0200 | [diff] [blame] | 81 | case 0x1c4: |
| 82 | return pcic->mbr; |
| 83 | case 0x1c8: |
| 84 | return pcic->iobr; |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 85 | case 0x220: |
| 86 | return pci_data_read(pcic->bus, pcic->par, 4); |
| 87 | } |
| 88 | return 0; |
| 89 | } |
| 90 | |
Avi Kivity | fb57117 | 2011-08-15 17:17:30 +0300 | [diff] [blame] | 91 | static const MemoryRegionOps sh_pci_reg_ops = { |
| 92 | .read = sh_pci_reg_read, |
| 93 | .write = sh_pci_reg_write, |
| 94 | .endianness = DEVICE_NATIVE_ENDIAN, |
| 95 | .valid = { |
| 96 | .min_access_size = 4, |
| 97 | .max_access_size = 4, |
| 98 | }, |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 101 | static int sh_pci_map_irq(PCIDevice *d, int irq_num) |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 102 | { |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 103 | return (d->devfn >> 3); |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 104 | } |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 105 | |
| 106 | static void sh_pci_set_irq(void *opaque, int irq_num, int level) |
| 107 | { |
| 108 | qemu_irq *pic = opaque; |
| 109 | |
| 110 | qemu_set_irq(pic[irq_num], level); |
| 111 | } |
| 112 | |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 113 | static int sh_pci_device_init(SysBusDevice *dev) |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 114 | { |
| 115 | SHPCIState *s; |
| 116 | int i; |
| 117 | |
| 118 | s = FROM_SYSBUS(SHPCIState, dev); |
| 119 | for (i = 0; i < 4; i++) { |
| 120 | sysbus_init_irq(dev, &s->irq[i]); |
| 121 | } |
| 122 | s->bus = pci_register_bus(&s->busdev.qdev, "pci", |
| 123 | sh_pci_set_irq, sh_pci_map_irq, |
Avi Kivity | aee97b8 | 2011-08-08 16:09:04 +0300 | [diff] [blame] | 124 | s->irq, |
| 125 | get_system_memory(), |
| 126 | get_system_io(), |
Avi Kivity | 1e39101 | 2011-07-26 14:26:19 +0300 | [diff] [blame] | 127 | PCI_DEVFN(0, 0), 4); |
Avi Kivity | fb57117 | 2011-08-15 17:17:30 +0300 | [diff] [blame] | 128 | memory_region_init_io(&s->memconfig_p4, &sh_pci_reg_ops, s, |
| 129 | "sh_pci", 0x224); |
Avi Kivity | 73c92f9 | 2011-08-28 18:17:04 +0300 | [diff] [blame] | 130 | memory_region_init_alias(&s->memconfig_a7, "sh_pci.2", &s->memconfig_p4, |
Avi Kivity | fb57117 | 2011-08-15 17:17:30 +0300 | [diff] [blame] | 131 | 0, 0x224); |
| 132 | isa_mmio_setup(&s->isa, 0x40000); |
Benoît Canet | 8c10623 | 2011-12-16 23:37:46 +0100 | [diff] [blame] | 133 | sysbus_init_mmio(dev, &s->memconfig_p4); |
Avi Kivity | 750ecd4 | 2011-11-27 11:38:10 +0200 | [diff] [blame] | 134 | sysbus_init_mmio(dev, &s->memconfig_a7); |
Benoît Canet | 8c10623 | 2011-12-16 23:37:46 +0100 | [diff] [blame] | 135 | s->iobr = 0xfe240000; |
| 136 | memory_region_add_subregion(get_system_memory(), s->iobr, &s->isa); |
| 137 | |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 138 | s->dev = pci_create_simple(s->bus, PCI_DEVFN(0, 0), "sh_pci_host"); |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int sh_pci_host_init(PCIDevice *d) |
| 143 | { |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 144 | pci_set_word(d->config + PCI_COMMAND, PCI_COMMAND_WAIT); |
| 145 | pci_set_word(d->config + PCI_STATUS, PCI_STATUS_CAP_LIST | |
| 146 | PCI_STATUS_FAST_BACK | PCI_STATUS_DEVSEL_MEDIUM); |
| 147 | return 0; |
| 148 | } |
| 149 | |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 150 | static void sh_pci_host_class_init(ObjectClass *klass, void *data) |
| 151 | { |
| 152 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
| 153 | |
| 154 | k->init = sh_pci_host_init; |
| 155 | k->vendor_id = PCI_VENDOR_ID_HITACHI; |
| 156 | k->device_id = PCI_DEVICE_ID_HITACHI_SH7751R; |
| 157 | } |
| 158 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 159 | static TypeInfo sh_pci_host_info = { |
| 160 | .name = "sh_pci_host", |
| 161 | .parent = TYPE_PCI_DEVICE, |
| 162 | .instance_size = sizeof(PCIDevice), |
| 163 | .class_init = sh_pci_host_class_init, |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 164 | }; |
| 165 | |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 166 | static void sh_pci_device_class_init(ObjectClass *klass, void *data) |
| 167 | { |
| 168 | SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass); |
| 169 | |
| 170 | sdc->init = sh_pci_device_init; |
| 171 | } |
| 172 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 173 | static TypeInfo sh_pci_device_info = { |
| 174 | .name = "sh_pci", |
| 175 | .parent = TYPE_SYS_BUS_DEVICE, |
| 176 | .instance_size = sizeof(SHPCIState), |
| 177 | .class_init = sh_pci_device_class_init, |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 178 | }; |
| 179 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 180 | static void sh_pci_register_types(void) |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 181 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 182 | type_register_static(&sh_pci_device_info); |
| 183 | type_register_static(&sh_pci_host_info); |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 186 | type_init(sh_pci_register_types) |