sh_pci.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "hw.h"
  25. #include "sh.h"
  26. #include "pci.h"
  27. #include "bswap.h"
  28. typedef struct {
  29. PCIBus *bus;
  30. PCIDevice *dev;
  31. uint32_t par;
  32. uint32_t mbr;
  33. uint32_t iobr;
  34. } SHPCIC;
  35. static void sh_pci_reg_write (void *p, target_phys_addr_t addr, uint32_t val)
  36. {
  37. SHPCIC *pcic = p;
  38. switch(addr) {
  39. case 0 ... 0xfc:
  40. cpu_to_le32w((uint32_t*)(pcic->dev->config + addr), val);
  41. break;
  42. case 0x1c0:
  43. pcic->par = val;
  44. break;
  45. case 0x1c4:
  46. pcic->mbr = val;
  47. break;
  48. case 0x1c8:
  49. pcic->iobr = val;
  50. break;
  51. case 0x220:
  52. pci_data_write(pcic->bus, pcic->par, val, 4);
  53. break;
  54. }
  55. }
  56. static uint32_t sh_pci_reg_read (void *p, target_phys_addr_t addr)
  57. {
  58. SHPCIC *pcic = p;
  59. switch(addr) {
  60. case 0 ... 0xfc:
  61. return le32_to_cpup((uint32_t*)(pcic->dev->config + addr));
  62. case 0x1c0:
  63. return pcic->par;
  64. case 0x220:
  65. return pci_data_read(pcic->bus, pcic->par, 4);
  66. }
  67. return 0;
  68. }
  69. static void sh_pci_data_write (SHPCIC *pcic, target_phys_addr_t addr,
  70. uint32_t val, int size)
  71. {
  72. pci_data_write(pcic->bus, addr + pcic->mbr, val, size);
  73. }
  74. static uint32_t sh_pci_mem_read (SHPCIC *pcic, target_phys_addr_t addr,
  75. int size)
  76. {
  77. return pci_data_read(pcic->bus, addr + pcic->mbr, size);
  78. }
  79. static void sh_pci_writeb (void *p, target_phys_addr_t addr, uint32_t val)
  80. {
  81. sh_pci_data_write(p, addr, val, 1);
  82. }
  83. static void sh_pci_writew (void *p, target_phys_addr_t addr, uint32_t val)
  84. {
  85. sh_pci_data_write(p, addr, val, 2);
  86. }
  87. static void sh_pci_writel (void *p, target_phys_addr_t addr, uint32_t val)
  88. {
  89. sh_pci_data_write(p, addr, val, 4);
  90. }
  91. static uint32_t sh_pci_readb (void *p, target_phys_addr_t addr)
  92. {
  93. return sh_pci_mem_read(p, addr, 1);
  94. }
  95. static uint32_t sh_pci_readw (void *p, target_phys_addr_t addr)
  96. {
  97. return sh_pci_mem_read(p, addr, 2);
  98. }
  99. static uint32_t sh_pci_readl (void *p, target_phys_addr_t addr)
  100. {
  101. return sh_pci_mem_read(p, addr, 4);
  102. }
  103. static int sh_pci_addr2port(SHPCIC *pcic, target_phys_addr_t addr)
  104. {
  105. return addr + pcic->iobr;
  106. }
  107. static void sh_pci_outb (void *p, target_phys_addr_t addr, uint32_t val)
  108. {
  109. cpu_outb(NULL, sh_pci_addr2port(p, addr), val);
  110. }
  111. static void sh_pci_outw (void *p, target_phys_addr_t addr, uint32_t val)
  112. {
  113. cpu_outw(NULL, sh_pci_addr2port(p, addr), val);
  114. }
  115. static void sh_pci_outl (void *p, target_phys_addr_t addr, uint32_t val)
  116. {
  117. cpu_outl(NULL, sh_pci_addr2port(p, addr), val);
  118. }
  119. static uint32_t sh_pci_inb (void *p, target_phys_addr_t addr)
  120. {
  121. return cpu_inb(NULL, sh_pci_addr2port(p, addr));
  122. }
  123. static uint32_t sh_pci_inw (void *p, target_phys_addr_t addr)
  124. {
  125. return cpu_inw(NULL, sh_pci_addr2port(p, addr));
  126. }
  127. static uint32_t sh_pci_inl (void *p, target_phys_addr_t addr)
  128. {
  129. return cpu_inl(NULL, sh_pci_addr2port(p, addr));
  130. }
  131. typedef struct {
  132. CPUReadMemoryFunc *r[3];
  133. CPUWriteMemoryFunc *w[3];
  134. } MemOp;
  135. static MemOp sh_pci_reg = {
  136. { NULL, NULL, sh_pci_reg_read },
  137. { NULL, NULL, sh_pci_reg_write },
  138. };
  139. static MemOp sh_pci_mem = {
  140. { sh_pci_readb, sh_pci_readw, sh_pci_readl },
  141. { sh_pci_writeb, sh_pci_writew, sh_pci_writel },
  142. };
  143. static MemOp sh_pci_iop = {
  144. { sh_pci_inb, sh_pci_inw, sh_pci_inl },
  145. { sh_pci_outb, sh_pci_outw, sh_pci_outl },
  146. };
  147. PCIBus *sh_pci_register_bus(pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
  148. qemu_irq *pic, int devfn_min, int nirq)
  149. {
  150. SHPCIC *p;
  151. int mem, reg, iop;
  152. p = qemu_mallocz(sizeof(SHPCIC));
  153. p->bus = pci_register_bus(set_irq, map_irq, pic, devfn_min, nirq);
  154. p->dev = pci_register_device(p->bus, "SH PCIC", sizeof(PCIDevice),
  155. -1, NULL, NULL);
  156. reg = cpu_register_io_memory(0, sh_pci_reg.r, sh_pci_reg.w, p);
  157. iop = cpu_register_io_memory(0, sh_pci_iop.r, sh_pci_iop.w, p);
  158. mem = cpu_register_io_memory(0, sh_pci_mem.r, sh_pci_mem.w, p);
  159. cpu_register_physical_memory(0x1e200000, 0x224, reg);
  160. cpu_register_physical_memory(0x1e240000, 0x40000, iop);
  161. cpu_register_physical_memory(0x1d000000, 0x1000000, mem);
  162. cpu_register_physical_memory(0xfe200000, 0x224, reg);
  163. cpu_register_physical_memory(0xfe240000, 0x40000, iop);
  164. cpu_register_physical_memory(0xfd000000, 0x1000000, mem);
  165. pci_config_set_vendor_id(p->dev->config, PCI_VENDOR_ID_HITACHI);
  166. pci_config_set_device_id(p->dev->config, 0x350e); // SH7751R
  167. p->dev->config[0x04] = 0x80;
  168. p->dev->config[0x05] = 0x00;
  169. p->dev->config[0x06] = 0x90;
  170. p->dev->config[0x07] = 0x02;
  171. return p->bus;
  172. }