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 | */ |
Peter Maydell | 9d4c994 | 2016-01-26 18:17:20 +0000 | [diff] [blame] | 24 | #include "qemu/osdep.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 25 | #include "hw/sysbus.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 26 | #include "hw/sh4/sh.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 27 | #include "hw/pci/pci.h" |
| 28 | #include "hw/pci/pci_host.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 29 | #include "qemu/bswap.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 30 | #include "exec/address-spaces.h" |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 31 | |
Paolo Bonzini | b23ea25 | 2013-07-22 15:54:29 +0200 | [diff] [blame] | 32 | #define TYPE_SH_PCI_HOST_BRIDGE "sh_pci" |
| 33 | |
| 34 | #define SH_PCI_HOST_BRIDGE(obj) \ |
| 35 | OBJECT_CHECK(SHPCIState, (obj), TYPE_SH_PCI_HOST_BRIDGE) |
| 36 | |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 37 | typedef struct SHPCIState { |
Paolo Bonzini | b23ea25 | 2013-07-22 15:54:29 +0200 | [diff] [blame] | 38 | PCIHostState parent_obj; |
| 39 | |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 40 | PCIDevice *dev; |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 41 | qemu_irq irq[4]; |
Avi Kivity | fb57117 | 2011-08-15 17:17:30 +0300 | [diff] [blame] | 42 | MemoryRegion memconfig_p4; |
| 43 | MemoryRegion memconfig_a7; |
| 44 | MemoryRegion isa; |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 45 | uint32_t par; |
| 46 | uint32_t mbr; |
| 47 | uint32_t iobr; |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 48 | } SHPCIState; |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 49 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 50 | static void sh_pci_reg_write (void *p, hwaddr addr, uint64_t val, |
Avi Kivity | fb57117 | 2011-08-15 17:17:30 +0300 | [diff] [blame] | 51 | unsigned size) |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 52 | { |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 53 | SHPCIState *pcic = p; |
Paolo Bonzini | b23ea25 | 2013-07-22 15:54:29 +0200 | [diff] [blame] | 54 | PCIHostState *phb = PCI_HOST_BRIDGE(pcic); |
| 55 | |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 56 | switch(addr) { |
| 57 | case 0 ... 0xfc: |
Peter Maydell | b7a51124 | 2016-06-10 17:10:21 +0100 | [diff] [blame] | 58 | stl_le_p(pcic->dev->config + addr, val); |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 59 | break; |
| 60 | case 0x1c0: |
| 61 | pcic->par = val; |
| 62 | break; |
| 63 | case 0x1c4: |
Aurelien Jarno | 5ba9e95 | 2010-04-11 23:59:39 +0200 | [diff] [blame] | 64 | pcic->mbr = val & 0xff000001; |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 65 | break; |
| 66 | case 0x1c8: |
Aurelien Jarno | 5ba9e95 | 2010-04-11 23:59:39 +0200 | [diff] [blame] | 67 | if ((val & 0xfffc0000) != (pcic->iobr & 0xfffc0000)) { |
Avi Kivity | fb57117 | 2011-08-15 17:17:30 +0300 | [diff] [blame] | 68 | memory_region_del_subregion(get_system_memory(), &pcic->isa); |
Aurelien Jarno | 5ba9e95 | 2010-04-11 23:59:39 +0200 | [diff] [blame] | 69 | pcic->iobr = val & 0xfffc0001; |
Avi Kivity | fb57117 | 2011-08-15 17:17:30 +0300 | [diff] [blame] | 70 | memory_region_add_subregion(get_system_memory(), |
| 71 | pcic->iobr & 0xfffc0000, &pcic->isa); |
Aurelien Jarno | 5ba9e95 | 2010-04-11 23:59:39 +0200 | [diff] [blame] | 72 | } |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 73 | break; |
| 74 | case 0x220: |
Paolo Bonzini | b23ea25 | 2013-07-22 15:54:29 +0200 | [diff] [blame] | 75 | pci_data_write(phb->bus, pcic->par, val, 4); |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 80 | static uint64_t sh_pci_reg_read (void *p, hwaddr addr, |
Avi Kivity | fb57117 | 2011-08-15 17:17:30 +0300 | [diff] [blame] | 81 | unsigned size) |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 82 | { |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 83 | SHPCIState *pcic = p; |
Paolo Bonzini | b23ea25 | 2013-07-22 15:54:29 +0200 | [diff] [blame] | 84 | PCIHostState *phb = PCI_HOST_BRIDGE(pcic); |
| 85 | |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 86 | switch(addr) { |
| 87 | case 0 ... 0xfc: |
Peter Maydell | b7a51124 | 2016-06-10 17:10:21 +0100 | [diff] [blame] | 88 | return ldl_le_p(pcic->dev->config + addr); |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 89 | case 0x1c0: |
| 90 | return pcic->par; |
Aurelien Jarno | 5ba9e95 | 2010-04-11 23:59:39 +0200 | [diff] [blame] | 91 | case 0x1c4: |
| 92 | return pcic->mbr; |
| 93 | case 0x1c8: |
| 94 | return pcic->iobr; |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 95 | case 0x220: |
Paolo Bonzini | b23ea25 | 2013-07-22 15:54:29 +0200 | [diff] [blame] | 96 | return pci_data_read(phb->bus, pcic->par, 4); |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 97 | } |
| 98 | return 0; |
| 99 | } |
| 100 | |
Avi Kivity | fb57117 | 2011-08-15 17:17:30 +0300 | [diff] [blame] | 101 | static const MemoryRegionOps sh_pci_reg_ops = { |
| 102 | .read = sh_pci_reg_read, |
| 103 | .write = sh_pci_reg_write, |
| 104 | .endianness = DEVICE_NATIVE_ENDIAN, |
| 105 | .valid = { |
| 106 | .min_access_size = 4, |
| 107 | .max_access_size = 4, |
| 108 | }, |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 111 | static int sh_pci_map_irq(PCIDevice *d, int irq_num) |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 112 | { |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 113 | return (d->devfn >> 3); |
balrog | 1e5459a | 2008-12-07 19:08:45 +0000 | [diff] [blame] | 114 | } |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 115 | |
| 116 | static void sh_pci_set_irq(void *opaque, int irq_num, int level) |
| 117 | { |
| 118 | qemu_irq *pic = opaque; |
| 119 | |
| 120 | qemu_set_irq(pic[irq_num], level); |
| 121 | } |
| 122 | |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 123 | static int sh_pci_device_init(SysBusDevice *dev) |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 124 | { |
Paolo Bonzini | b23ea25 | 2013-07-22 15:54:29 +0200 | [diff] [blame] | 125 | PCIHostState *phb; |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 126 | SHPCIState *s; |
| 127 | int i; |
| 128 | |
Paolo Bonzini | b23ea25 | 2013-07-22 15:54:29 +0200 | [diff] [blame] | 129 | s = SH_PCI_HOST_BRIDGE(dev); |
| 130 | phb = PCI_HOST_BRIDGE(s); |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 131 | for (i = 0; i < 4; i++) { |
| 132 | sysbus_init_irq(dev, &s->irq[i]); |
| 133 | } |
Paolo Bonzini | b23ea25 | 2013-07-22 15:54:29 +0200 | [diff] [blame] | 134 | phb->bus = pci_register_bus(DEVICE(dev), "pci", |
| 135 | sh_pci_set_irq, sh_pci_map_irq, |
| 136 | s->irq, |
| 137 | get_system_memory(), |
| 138 | get_system_io(), |
| 139 | PCI_DEVFN(0, 0), 4, TYPE_PCI_BUS); |
Paolo Bonzini | 2977673 | 2013-06-06 21:25:08 -0400 | [diff] [blame] | 140 | memory_region_init_io(&s->memconfig_p4, OBJECT(s), &sh_pci_reg_ops, s, |
Avi Kivity | fb57117 | 2011-08-15 17:17:30 +0300 | [diff] [blame] | 141 | "sh_pci", 0x224); |
Paolo Bonzini | 2977673 | 2013-06-06 21:25:08 -0400 | [diff] [blame] | 142 | memory_region_init_alias(&s->memconfig_a7, OBJECT(s), "sh_pci.2", |
| 143 | &s->memconfig_p4, 0, 0x224); |
Paolo Bonzini | 4759ab6 | 2013-07-22 15:54:11 +0200 | [diff] [blame] | 144 | memory_region_init_alias(&s->isa, OBJECT(s), "sh_pci.isa", |
| 145 | get_system_io(), 0, 0x40000); |
Benoît Canet | 8c10623 | 2011-12-16 23:37:46 +0100 | [diff] [blame] | 146 | sysbus_init_mmio(dev, &s->memconfig_p4); |
Avi Kivity | 750ecd4 | 2011-11-27 11:38:10 +0200 | [diff] [blame] | 147 | sysbus_init_mmio(dev, &s->memconfig_a7); |
Benoît Canet | 8c10623 | 2011-12-16 23:37:46 +0100 | [diff] [blame] | 148 | s->iobr = 0xfe240000; |
| 149 | memory_region_add_subregion(get_system_memory(), s->iobr, &s->isa); |
| 150 | |
Paolo Bonzini | b23ea25 | 2013-07-22 15:54:29 +0200 | [diff] [blame] | 151 | s->dev = pci_create_simple(phb->bus, PCI_DEVFN(0, 0), "sh_pci_host"); |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
Cao jin | 9f23b27 | 2015-12-18 19:03:48 +0800 | [diff] [blame] | 155 | static void sh_pci_host_realize(PCIDevice *d, Error **errp) |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 156 | { |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 157 | pci_set_word(d->config + PCI_COMMAND, PCI_COMMAND_WAIT); |
| 158 | pci_set_word(d->config + PCI_STATUS, PCI_STATUS_CAP_LIST | |
| 159 | PCI_STATUS_FAST_BACK | PCI_STATUS_DEVSEL_MEDIUM); |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 162 | static void sh_pci_host_class_init(ObjectClass *klass, void *data) |
| 163 | { |
| 164 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
Markus Armbruster | 08c58f9 | 2013-11-28 17:26:58 +0100 | [diff] [blame] | 165 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 166 | |
Cao jin | 9f23b27 | 2015-12-18 19:03:48 +0800 | [diff] [blame] | 167 | k->realize = sh_pci_host_realize; |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 168 | k->vendor_id = PCI_VENDOR_ID_HITACHI; |
| 169 | k->device_id = PCI_DEVICE_ID_HITACHI_SH7751R; |
Markus Armbruster | 08c58f9 | 2013-11-28 17:26:58 +0100 | [diff] [blame] | 170 | /* |
| 171 | * PCI-facing part of the host bridge, not usable without the |
| 172 | * host-facing part, which can't be device_add'ed, yet. |
| 173 | */ |
| 174 | dc->cannot_instantiate_with_device_add_yet = true; |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 175 | } |
| 176 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 177 | static const TypeInfo sh_pci_host_info = { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 178 | .name = "sh_pci_host", |
| 179 | .parent = TYPE_PCI_DEVICE, |
| 180 | .instance_size = sizeof(PCIDevice), |
| 181 | .class_init = sh_pci_host_class_init, |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 182 | }; |
| 183 | |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 184 | static void sh_pci_device_class_init(ObjectClass *klass, void *data) |
| 185 | { |
| 186 | SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass); |
| 187 | |
| 188 | sdc->init = sh_pci_device_init; |
| 189 | } |
| 190 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 191 | static const TypeInfo sh_pci_device_info = { |
Paolo Bonzini | b23ea25 | 2013-07-22 15:54:29 +0200 | [diff] [blame] | 192 | .name = TYPE_SH_PCI_HOST_BRIDGE, |
| 193 | .parent = TYPE_PCI_HOST_BRIDGE, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 194 | .instance_size = sizeof(SHPCIState), |
| 195 | .class_init = sh_pci_device_class_init, |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 196 | }; |
| 197 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 198 | static void sh_pci_register_types(void) |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 199 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 200 | type_register_static(&sh_pci_device_info); |
| 201 | type_register_static(&sh_pci_host_info); |
Aurelien Jarno | cf15439 | 2011-01-19 18:23:59 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 204 | type_init(sh_pci_register_types) |