sh_pci.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. typedef struct SHPCIState {
  30. SysBusDevice busdev;
  31. PCIBus *bus;
  32. PCIDevice *dev;
  33. qemu_irq irq[4];
  34. int memconfig;
  35. uint32_t par;
  36. uint32_t mbr;
  37. uint32_t iobr;
  38. } SHPCIState;
  39. static void sh_pci_reg_write (void *p, target_phys_addr_t addr, uint32_t val)
  40. {
  41. SHPCIState *pcic = p;
  42. switch(addr) {
  43. case 0 ... 0xfc:
  44. cpu_to_le32w((uint32_t*)(pcic->dev->config + addr), val);
  45. break;
  46. case 0x1c0:
  47. pcic->par = val;
  48. break;
  49. case 0x1c4:
  50. pcic->mbr = val & 0xff000001;
  51. break;
  52. case 0x1c8:
  53. if ((val & 0xfffc0000) != (pcic->iobr & 0xfffc0000)) {
  54. cpu_register_physical_memory(pcic->iobr & 0xfffc0000, 0x40000,
  55. IO_MEM_UNASSIGNED);
  56. pcic->iobr = val & 0xfffc0001;
  57. isa_mmio_init(pcic->iobr & 0xfffc0000, 0x40000);
  58. }
  59. break;
  60. case 0x220:
  61. pci_data_write(pcic->bus, pcic->par, val, 4);
  62. break;
  63. }
  64. }
  65. static uint32_t sh_pci_reg_read (void *p, target_phys_addr_t addr)
  66. {
  67. SHPCIState *pcic = p;
  68. switch(addr) {
  69. case 0 ... 0xfc:
  70. return le32_to_cpup((uint32_t*)(pcic->dev->config + addr));
  71. case 0x1c0:
  72. return pcic->par;
  73. case 0x1c4:
  74. return pcic->mbr;
  75. case 0x1c8:
  76. return pcic->iobr;
  77. case 0x220:
  78. return pci_data_read(pcic->bus, pcic->par, 4);
  79. }
  80. return 0;
  81. }
  82. typedef struct {
  83. CPUReadMemoryFunc * const r[3];
  84. CPUWriteMemoryFunc * const w[3];
  85. } MemOp;
  86. static MemOp sh_pci_reg = {
  87. { NULL, NULL, sh_pci_reg_read },
  88. { NULL, NULL, sh_pci_reg_write },
  89. };
  90. static int sh_pci_map_irq(PCIDevice *d, int irq_num)
  91. {
  92. return (d->devfn >> 3);
  93. }
  94. static void sh_pci_set_irq(void *opaque, int irq_num, int level)
  95. {
  96. qemu_irq *pic = opaque;
  97. qemu_set_irq(pic[irq_num], level);
  98. }
  99. static void sh_pci_map(SysBusDevice *dev, target_phys_addr_t base)
  100. {
  101. SHPCIState *s = FROM_SYSBUS(SHPCIState, dev);
  102. cpu_register_physical_memory(P4ADDR(base), 0x224, s->memconfig);
  103. cpu_register_physical_memory(A7ADDR(base), 0x224, s->memconfig);
  104. s->iobr = 0xfe240000;
  105. isa_mmio_init(s->iobr, 0x40000);
  106. }
  107. static int sh_pci_init_device(SysBusDevice *dev)
  108. {
  109. SHPCIState *s;
  110. int i;
  111. s = FROM_SYSBUS(SHPCIState, dev);
  112. for (i = 0; i < 4; i++) {
  113. sysbus_init_irq(dev, &s->irq[i]);
  114. }
  115. s->bus = pci_register_bus(&s->busdev.qdev, "pci",
  116. sh_pci_set_irq, sh_pci_map_irq,
  117. s->irq, PCI_DEVFN(0, 0), 4);
  118. s->memconfig = cpu_register_io_memory(sh_pci_reg.r, sh_pci_reg.w,
  119. s, DEVICE_NATIVE_ENDIAN);
  120. sysbus_init_mmio_cb(dev, 0x224, sh_pci_map);
  121. s->dev = pci_create_simple(s->bus, PCI_DEVFN(0, 0), "sh_pci_host");
  122. return 0;
  123. }
  124. static int sh_pci_host_init(PCIDevice *d)
  125. {
  126. pci_set_word(d->config + PCI_COMMAND, PCI_COMMAND_WAIT);
  127. pci_set_word(d->config + PCI_STATUS, PCI_STATUS_CAP_LIST |
  128. PCI_STATUS_FAST_BACK | PCI_STATUS_DEVSEL_MEDIUM);
  129. return 0;
  130. }
  131. static PCIDeviceInfo sh_pci_host_info = {
  132. .qdev.name = "sh_pci_host",
  133. .qdev.size = sizeof(PCIDevice),
  134. .init = sh_pci_host_init,
  135. .vendor_id = PCI_VENDOR_ID_HITACHI,
  136. .device_id = PCI_DEVICE_ID_HITACHI_SH7751R,
  137. };
  138. static void sh_pci_register_devices(void)
  139. {
  140. sysbus_register_dev("sh_pci", sizeof(SHPCIState),
  141. sh_pci_init_device);
  142. pci_qdev_register(&sh_pci_host_info);
  143. }
  144. device_init(sh_pci_register_devices)