sh_pci.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "sysbus.h"
  25. #include "sh.h"
  26. #include "pci.h"
  27. #include "pci_host.h"
  28. #include "bswap.h"
  29. #include "exec-memory.h"
  30. typedef struct SHPCIState {
  31. SysBusDevice busdev;
  32. PCIBus *bus;
  33. PCIDevice *dev;
  34. qemu_irq irq[4];
  35. MemoryRegion memconfig_p4;
  36. MemoryRegion memconfig_a7;
  37. MemoryRegion isa;
  38. uint32_t par;
  39. uint32_t mbr;
  40. uint32_t iobr;
  41. } SHPCIState;
  42. static void sh_pci_reg_write (void *p, target_phys_addr_t addr, uint64_t val,
  43. unsigned size)
  44. {
  45. SHPCIState *pcic = p;
  46. switch(addr) {
  47. case 0 ... 0xfc:
  48. cpu_to_le32w((uint32_t*)(pcic->dev->config + addr), val);
  49. break;
  50. case 0x1c0:
  51. pcic->par = val;
  52. break;
  53. case 0x1c4:
  54. pcic->mbr = val & 0xff000001;
  55. break;
  56. case 0x1c8:
  57. if ((val & 0xfffc0000) != (pcic->iobr & 0xfffc0000)) {
  58. memory_region_del_subregion(get_system_memory(), &pcic->isa);
  59. pcic->iobr = val & 0xfffc0001;
  60. memory_region_add_subregion(get_system_memory(),
  61. pcic->iobr & 0xfffc0000, &pcic->isa);
  62. }
  63. break;
  64. case 0x220:
  65. pci_data_write(pcic->bus, pcic->par, val, 4);
  66. break;
  67. }
  68. }
  69. static uint64_t sh_pci_reg_read (void *p, target_phys_addr_t addr,
  70. unsigned size)
  71. {
  72. SHPCIState *pcic = p;
  73. switch(addr) {
  74. case 0 ... 0xfc:
  75. return le32_to_cpup((uint32_t*)(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(pcic->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 (d->devfn >> 3);
  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_map(SysBusDevice *dev, target_phys_addr_t base)
  106. {
  107. SHPCIState *s = FROM_SYSBUS(SHPCIState, dev);
  108. memory_region_add_subregion(get_system_memory(),
  109. P4ADDR(base),
  110. &s->memconfig_p4);
  111. memory_region_add_subregion(get_system_memory(),
  112. A7ADDR(base),
  113. &s->memconfig_a7);
  114. s->iobr = 0xfe240000;
  115. memory_region_add_subregion(get_system_memory(), s->iobr, &s->isa);
  116. }
  117. static void sh_pci_unmap(SysBusDevice *dev, target_phys_addr_t base)
  118. {
  119. SHPCIState *s = FROM_SYSBUS(SHPCIState, dev);
  120. memory_region_del_subregion(get_system_memory(), &s->memconfig_p4);
  121. memory_region_del_subregion(get_system_memory(), &s->memconfig_a7);
  122. memory_region_del_subregion(get_system_memory(), &s->isa);
  123. }
  124. static int sh_pci_init_device(SysBusDevice *dev)
  125. {
  126. SHPCIState *s;
  127. int i;
  128. s = FROM_SYSBUS(SHPCIState, dev);
  129. for (i = 0; i < 4; i++) {
  130. sysbus_init_irq(dev, &s->irq[i]);
  131. }
  132. s->bus = pci_register_bus(&s->busdev.qdev, "pci",
  133. sh_pci_set_irq, sh_pci_map_irq,
  134. s->irq,
  135. get_system_memory(),
  136. get_system_io(),
  137. PCI_DEVFN(0, 0), 4);
  138. memory_region_init_io(&s->memconfig_p4, &sh_pci_reg_ops, s,
  139. "sh_pci", 0x224);
  140. memory_region_init_alias(&s->memconfig_a7, "sh_pci.2", &s->memconfig_p4,
  141. 0, 0x224);
  142. isa_mmio_setup(&s->isa, 0x40000);
  143. sysbus_init_mmio_cb2(dev, sh_pci_map, sh_pci_unmap);
  144. sysbus_init_mmio_region(dev, &s->memconfig_a7);
  145. sysbus_init_mmio_region(dev, &s->isa);
  146. s->dev = pci_create_simple(s->bus, PCI_DEVFN(0, 0), "sh_pci_host");
  147. return 0;
  148. }
  149. static int sh_pci_host_init(PCIDevice *d)
  150. {
  151. pci_set_word(d->config + PCI_COMMAND, PCI_COMMAND_WAIT);
  152. pci_set_word(d->config + PCI_STATUS, PCI_STATUS_CAP_LIST |
  153. PCI_STATUS_FAST_BACK | PCI_STATUS_DEVSEL_MEDIUM);
  154. return 0;
  155. }
  156. static PCIDeviceInfo sh_pci_host_info = {
  157. .qdev.name = "sh_pci_host",
  158. .qdev.size = sizeof(PCIDevice),
  159. .init = sh_pci_host_init,
  160. .vendor_id = PCI_VENDOR_ID_HITACHI,
  161. .device_id = PCI_DEVICE_ID_HITACHI_SH7751R,
  162. };
  163. static void sh_pci_register_devices(void)
  164. {
  165. sysbus_register_dev("sh_pci", sizeof(SHPCIState),
  166. sh_pci_init_device);
  167. pci_qdev_register(&sh_pci_host_info);
  168. }
  169. device_init(sh_pci_register_devices)