sh_pci.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. */
  24. #include "qemu/osdep.h"
  25. #include "hw/sysbus.h"
  26. #include "hw/sh4/sh.h"
  27. #include "hw/irq.h"
  28. #include "hw/pci/pci_device.h"
  29. #include "hw/pci/pci_host.h"
  30. #include "qemu/bswap.h"
  31. #include "qemu/module.h"
  32. #include "qom/object.h"
  33. #define TYPE_SH_PCI_HOST_BRIDGE "sh_pci"
  34. OBJECT_DECLARE_SIMPLE_TYPE(SHPCIState, SH_PCI_HOST_BRIDGE)
  35. struct SHPCIState {
  36. PCIHostState parent_obj;
  37. PCIDevice *dev;
  38. qemu_irq irq[4];
  39. MemoryRegion memconfig_p4;
  40. MemoryRegion memconfig_a7;
  41. MemoryRegion isa;
  42. uint32_t par;
  43. uint32_t mbr;
  44. uint32_t iobr;
  45. };
  46. static void sh_pci_reg_write(void *p, hwaddr addr, uint64_t val, unsigned size)
  47. {
  48. SHPCIState *pcic = p;
  49. PCIHostState *phb = PCI_HOST_BRIDGE(pcic);
  50. switch (addr) {
  51. case 0 ... 0xfc:
  52. stl_le_p(pcic->dev->config + addr, val);
  53. break;
  54. case 0x1c0:
  55. pcic->par = val;
  56. break;
  57. case 0x1c4:
  58. pcic->mbr = val & 0xff000001;
  59. break;
  60. case 0x1c8:
  61. pcic->iobr = val & 0xfffc0001;
  62. memory_region_set_alias_offset(&pcic->isa, val & 0xfffc0000);
  63. break;
  64. case 0x220:
  65. pci_data_write(phb->bus, pcic->par, val, 4);
  66. break;
  67. }
  68. }
  69. static uint64_t sh_pci_reg_read(void *p, hwaddr addr, unsigned size)
  70. {
  71. SHPCIState *pcic = p;
  72. PCIHostState *phb = PCI_HOST_BRIDGE(pcic);
  73. switch (addr) {
  74. case 0 ... 0xfc:
  75. return ldl_le_p(pcic->dev->config + addr);
  76. case 0x1c0:
  77. return pcic->par;
  78. case 0x1c4:
  79. return pcic->mbr;
  80. case 0x1c8:
  81. return pcic->iobr;
  82. case 0x220:
  83. return pci_data_read(phb->bus, pcic->par, 4);
  84. }
  85. return 0;
  86. }
  87. static const MemoryRegionOps sh_pci_reg_ops = {
  88. .read = sh_pci_reg_read,
  89. .write = sh_pci_reg_write,
  90. .endianness = DEVICE_NATIVE_ENDIAN,
  91. .valid = {
  92. .min_access_size = 4,
  93. .max_access_size = 4,
  94. },
  95. };
  96. static int sh_pci_map_irq(PCIDevice *d, int irq_num)
  97. {
  98. return PCI_SLOT(d->devfn);
  99. }
  100. static void sh_pci_set_irq(void *opaque, int irq_num, int level)
  101. {
  102. qemu_irq *pic = opaque;
  103. qemu_set_irq(pic[irq_num], level);
  104. }
  105. static void sh_pci_device_realize(DeviceState *dev, Error **errp)
  106. {
  107. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  108. SHPCIState *s = SH_PCI_HOST_BRIDGE(dev);
  109. PCIHostState *phb = PCI_HOST_BRIDGE(s);
  110. int i;
  111. for (i = 0; i < 4; i++) {
  112. sysbus_init_irq(sbd, &s->irq[i]);
  113. }
  114. phb->bus = pci_register_root_bus(dev, "pci",
  115. sh_pci_set_irq, sh_pci_map_irq,
  116. s->irq,
  117. get_system_memory(),
  118. get_system_io(),
  119. PCI_DEVFN(0, 0), 4, TYPE_PCI_BUS);
  120. memory_region_init_io(&s->memconfig_p4, OBJECT(s), &sh_pci_reg_ops, s,
  121. "sh_pci", 0x224);
  122. memory_region_init_alias(&s->memconfig_a7, OBJECT(s), "sh_pci.2",
  123. &s->memconfig_p4, 0, 0x224);
  124. memory_region_init_alias(&s->isa, OBJECT(s), "sh_pci.isa",
  125. get_system_io(), 0, 0x40000);
  126. sysbus_init_mmio(sbd, &s->memconfig_p4);
  127. sysbus_init_mmio(sbd, &s->memconfig_a7);
  128. memory_region_add_subregion(get_system_memory(), 0xfe240000, &s->isa);
  129. s->dev = pci_create_simple(phb->bus, PCI_DEVFN(0, 0), "sh_pci_host");
  130. }
  131. static void sh_pci_host_realize(PCIDevice *d, Error **errp)
  132. {
  133. pci_set_word(d->config + PCI_COMMAND, PCI_COMMAND_WAIT);
  134. pci_set_word(d->config + PCI_STATUS, PCI_STATUS_CAP_LIST |
  135. PCI_STATUS_FAST_BACK | PCI_STATUS_DEVSEL_MEDIUM);
  136. }
  137. static void sh_pci_host_class_init(ObjectClass *klass, void *data)
  138. {
  139. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  140. DeviceClass *dc = DEVICE_CLASS(klass);
  141. k->realize = sh_pci_host_realize;
  142. k->vendor_id = PCI_VENDOR_ID_HITACHI;
  143. k->device_id = PCI_DEVICE_ID_HITACHI_SH7751R;
  144. /*
  145. * PCI-facing part of the host bridge, not usable without the
  146. * host-facing part, which can't be device_add'ed, yet.
  147. */
  148. dc->user_creatable = false;
  149. }
  150. static const TypeInfo sh_pci_host_info = {
  151. .name = "sh_pci_host",
  152. .parent = TYPE_PCI_DEVICE,
  153. .instance_size = sizeof(PCIDevice),
  154. .class_init = sh_pci_host_class_init,
  155. .interfaces = (InterfaceInfo[]) {
  156. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  157. { },
  158. },
  159. };
  160. static void sh_pci_device_class_init(ObjectClass *klass, void *data)
  161. {
  162. DeviceClass *dc = DEVICE_CLASS(klass);
  163. dc->realize = sh_pci_device_realize;
  164. }
  165. static const TypeInfo sh_pci_device_info = {
  166. .name = TYPE_SH_PCI_HOST_BRIDGE,
  167. .parent = TYPE_PCI_HOST_BRIDGE,
  168. .instance_size = sizeof(SHPCIState),
  169. .class_init = sh_pci_device_class_init,
  170. };
  171. static void sh_pci_register_types(void)
  172. {
  173. type_register_static(&sh_pci_device_info);
  174. type_register_static(&sh_pci_host_info);
  175. }
  176. type_init(sh_pci_register_types)