ppce500_pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * QEMU PowerPC E500 embedded processors pci controller emulation
  3. *
  4. * Copyright (C) 2009 Freescale Semiconductor, Inc. All rights reserved.
  5. *
  6. * Author: Yu Liu, <yu.liu@freescale.com>
  7. *
  8. * This file is derived from hw/ppc4xx_pci.c,
  9. * the copyright for that material belongs to the original owners.
  10. *
  11. * This is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. #include "hw.h"
  17. #include "ppc.h"
  18. #include "ppce500.h"
  19. typedef target_phys_addr_t pci_addr_t;
  20. #include "pci.h"
  21. #include "pci_host.h"
  22. #include "bswap.h"
  23. #include "qemu-log.h"
  24. #ifdef DEBUG_PCI
  25. #define pci_debug(fmt, arg...) fprintf(stderr, fmt, ##arg)
  26. #else
  27. #define pci_debug(fmt, arg...)
  28. #endif
  29. #define PCIE500_CFGADDR 0x0
  30. #define PCIE500_CFGDATA 0x4
  31. #define PCIE500_REG_BASE 0xC00
  32. #define PCIE500_REG_SIZE (0x1000 - PCIE500_REG_BASE)
  33. #define PPCE500_PCI_CONFIG_ADDR 0x0
  34. #define PPCE500_PCI_CONFIG_DATA 0x4
  35. #define PPCE500_PCI_INTACK 0x8
  36. #define PPCE500_PCI_OW1 (0xC20 - PCIE500_REG_BASE)
  37. #define PPCE500_PCI_OW2 (0xC40 - PCIE500_REG_BASE)
  38. #define PPCE500_PCI_OW3 (0xC60 - PCIE500_REG_BASE)
  39. #define PPCE500_PCI_OW4 (0xC80 - PCIE500_REG_BASE)
  40. #define PPCE500_PCI_IW3 (0xDA0 - PCIE500_REG_BASE)
  41. #define PPCE500_PCI_IW2 (0xDC0 - PCIE500_REG_BASE)
  42. #define PPCE500_PCI_IW1 (0xDE0 - PCIE500_REG_BASE)
  43. #define PPCE500_PCI_GASKET_TIMR (0xE20 - PCIE500_REG_BASE)
  44. #define PCI_POTAR 0x0
  45. #define PCI_POTEAR 0x4
  46. #define PCI_POWBAR 0x8
  47. #define PCI_POWAR 0x10
  48. #define PCI_PITAR 0x0
  49. #define PCI_PIWBAR 0x8
  50. #define PCI_PIWBEAR 0xC
  51. #define PCI_PIWAR 0x10
  52. #define PPCE500_PCI_NR_POBS 5
  53. #define PPCE500_PCI_NR_PIBS 3
  54. struct pci_outbound {
  55. uint32_t potar;
  56. uint32_t potear;
  57. uint32_t powbar;
  58. uint32_t powar;
  59. };
  60. struct pci_inbound {
  61. uint32_t pitar;
  62. uint32_t piwbar;
  63. uint32_t piwbear;
  64. uint32_t piwar;
  65. };
  66. struct PPCE500PCIState {
  67. struct pci_outbound pob[PPCE500_PCI_NR_POBS];
  68. struct pci_inbound pib[PPCE500_PCI_NR_PIBS];
  69. uint32_t gasket_time;
  70. PCIHostState pci_state;
  71. PCIDevice *pci_dev;
  72. };
  73. typedef struct PPCE500PCIState PPCE500PCIState;
  74. static uint32_t pcie500_cfgaddr_readl(void *opaque, target_phys_addr_t addr)
  75. {
  76. PPCE500PCIState *pci = opaque;
  77. pci_debug("%s: (addr:%Lx) -> value:%x\n", __func__, addr,
  78. pci->pci_state.config_reg);
  79. return pci->pci_state.config_reg;
  80. }
  81. static CPUReadMemoryFunc *pcie500_cfgaddr_read[] = {
  82. &pcie500_cfgaddr_readl,
  83. &pcie500_cfgaddr_readl,
  84. &pcie500_cfgaddr_readl,
  85. };
  86. static void pcie500_cfgaddr_writel(void *opaque, target_phys_addr_t addr,
  87. uint32_t value)
  88. {
  89. PPCE500PCIState *controller = opaque;
  90. pci_debug("%s: value:%x -> (addr%Lx)\n", __func__, value, addr);
  91. controller->pci_state.config_reg = value & ~0x3;
  92. }
  93. static CPUWriteMemoryFunc *pcie500_cfgaddr_write[] = {
  94. &pcie500_cfgaddr_writel,
  95. &pcie500_cfgaddr_writel,
  96. &pcie500_cfgaddr_writel,
  97. };
  98. static CPUReadMemoryFunc *pcie500_cfgdata_read[] = {
  99. &pci_host_data_readb,
  100. &pci_host_data_readw,
  101. &pci_host_data_readl,
  102. };
  103. static CPUWriteMemoryFunc *pcie500_cfgdata_write[] = {
  104. &pci_host_data_writeb,
  105. &pci_host_data_writew,
  106. &pci_host_data_writel,
  107. };
  108. static uint32_t pci_reg_read4(void *opaque, target_phys_addr_t addr)
  109. {
  110. PPCE500PCIState *pci = opaque;
  111. unsigned long win;
  112. uint32_t value = 0;
  113. win = addr & 0xfe0;
  114. switch (win) {
  115. case PPCE500_PCI_OW1:
  116. case PPCE500_PCI_OW2:
  117. case PPCE500_PCI_OW3:
  118. case PPCE500_PCI_OW4:
  119. switch (addr & 0xC) {
  120. case PCI_POTAR: value = pci->pob[(addr >> 5) & 0x7].potar; break;
  121. case PCI_POTEAR: value = pci->pob[(addr >> 5) & 0x7].potear; break;
  122. case PCI_POWBAR: value = pci->pob[(addr >> 5) & 0x7].powbar; break;
  123. case PCI_POWAR: value = pci->pob[(addr >> 5) & 0x7].powar; break;
  124. default: break;
  125. }
  126. break;
  127. case PPCE500_PCI_IW3:
  128. case PPCE500_PCI_IW2:
  129. case PPCE500_PCI_IW1:
  130. switch (addr & 0xC) {
  131. case PCI_PITAR: value = pci->pib[(addr >> 5) & 0x3].pitar; break;
  132. case PCI_PIWBAR: value = pci->pib[(addr >> 5) & 0x3].piwbar; break;
  133. case PCI_PIWBEAR: value = pci->pib[(addr >> 5) & 0x3].piwbear; break;
  134. case PCI_PIWAR: value = pci->pib[(addr >> 5) & 0x3].piwar; break;
  135. default: break;
  136. };
  137. break;
  138. case PPCE500_PCI_GASKET_TIMR:
  139. value = pci->gasket_time;
  140. break;
  141. default:
  142. break;
  143. }
  144. pci_debug("%s: win:%lx(addr:%Lx) -> value:%x\n",__func__,win,addr,value);
  145. return value;
  146. }
  147. static CPUReadMemoryFunc *e500_pci_reg_read[] = {
  148. &pci_reg_read4,
  149. &pci_reg_read4,
  150. &pci_reg_read4,
  151. };
  152. static void pci_reg_write4(void *opaque, target_phys_addr_t addr,
  153. uint32_t value)
  154. {
  155. PPCE500PCIState *pci = opaque;
  156. unsigned long win;
  157. win = addr & 0xfe0;
  158. pci_debug("%s: value:%x -> win:%lx(addr:%Lx)\n",__func__,value,win,addr);
  159. switch (win) {
  160. case PPCE500_PCI_OW1:
  161. case PPCE500_PCI_OW2:
  162. case PPCE500_PCI_OW3:
  163. case PPCE500_PCI_OW4:
  164. switch (addr & 0xC) {
  165. case PCI_POTAR: pci->pob[(addr >> 5) & 0x7].potar = value; break;
  166. case PCI_POTEAR: pci->pob[(addr >> 5) & 0x7].potear = value; break;
  167. case PCI_POWBAR: pci->pob[(addr >> 5) & 0x7].powbar = value; break;
  168. case PCI_POWAR: pci->pob[(addr >> 5) & 0x7].powar = value; break;
  169. default: break;
  170. };
  171. break;
  172. case PPCE500_PCI_IW3:
  173. case PPCE500_PCI_IW2:
  174. case PPCE500_PCI_IW1:
  175. switch (addr & 0xC) {
  176. case PCI_PITAR: pci->pib[(addr >> 5) & 0x3].pitar = value; break;
  177. case PCI_PIWBAR: pci->pib[(addr >> 5) & 0x3].piwbar = value; break;
  178. case PCI_PIWBEAR: pci->pib[(addr >> 5) & 0x3].piwbear = value; break;
  179. case PCI_PIWAR: pci->pib[(addr >> 5) & 0x3].piwar = value; break;
  180. default: break;
  181. };
  182. break;
  183. case PPCE500_PCI_GASKET_TIMR:
  184. pci->gasket_time = value;
  185. break;
  186. default:
  187. break;
  188. };
  189. }
  190. static CPUWriteMemoryFunc *e500_pci_reg_write[] = {
  191. &pci_reg_write4,
  192. &pci_reg_write4,
  193. &pci_reg_write4,
  194. };
  195. static int mpc85xx_pci_map_irq(PCIDevice *pci_dev, int irq_num)
  196. {
  197. int devno = pci_dev->devfn >> 3, ret = 0;
  198. switch (devno) {
  199. /* Two PCI slot */
  200. case 0x11:
  201. case 0x12:
  202. ret = (irq_num + devno - 0x10) % 4;
  203. break;
  204. default:
  205. printf("Error:%s:unknow dev number\n", __func__);
  206. }
  207. pci_debug("%s: devfn %x irq %d -> %d devno:%x\n", __func__,
  208. pci_dev->devfn, irq_num, ret, devno);
  209. return ret;
  210. }
  211. static void mpc85xx_pci_set_irq(qemu_irq *pic, int irq_num, int level)
  212. {
  213. pci_debug("%s: PCI irq %d, level:%d\n", __func__, irq_num, level);
  214. qemu_set_irq(pic[irq_num], level);
  215. }
  216. static void ppce500_pci_save(QEMUFile *f, void *opaque)
  217. {
  218. PPCE500PCIState *controller = opaque;
  219. int i;
  220. pci_device_save(controller->pci_dev, f);
  221. for (i = 0; i < PPCE500_PCI_NR_POBS; i++) {
  222. qemu_put_be32s(f, &controller->pob[i].potar);
  223. qemu_put_be32s(f, &controller->pob[i].potear);
  224. qemu_put_be32s(f, &controller->pob[i].powbar);
  225. qemu_put_be32s(f, &controller->pob[i].powar);
  226. }
  227. for (i = 0; i < PPCE500_PCI_NR_PIBS; i++) {
  228. qemu_put_be32s(f, &controller->pib[i].pitar);
  229. qemu_put_be32s(f, &controller->pib[i].piwbar);
  230. qemu_put_be32s(f, &controller->pib[i].piwbear);
  231. qemu_put_be32s(f, &controller->pib[i].piwar);
  232. }
  233. qemu_put_be32s(f, &controller->gasket_time);
  234. }
  235. static int ppce500_pci_load(QEMUFile *f, void *opaque, int version_id)
  236. {
  237. PPCE500PCIState *controller = opaque;
  238. int i;
  239. if (version_id != 1)
  240. return -EINVAL;
  241. pci_device_load(controller->pci_dev, f);
  242. for (i = 0; i < PPCE500_PCI_NR_POBS; i++) {
  243. qemu_get_be32s(f, &controller->pob[i].potar);
  244. qemu_get_be32s(f, &controller->pob[i].potear);
  245. qemu_get_be32s(f, &controller->pob[i].powbar);
  246. qemu_get_be32s(f, &controller->pob[i].powar);
  247. }
  248. for (i = 0; i < PPCE500_PCI_NR_PIBS; i++) {
  249. qemu_get_be32s(f, &controller->pib[i].pitar);
  250. qemu_get_be32s(f, &controller->pib[i].piwbar);
  251. qemu_get_be32s(f, &controller->pib[i].piwbear);
  252. qemu_get_be32s(f, &controller->pib[i].piwar);
  253. }
  254. qemu_get_be32s(f, &controller->gasket_time);
  255. return 0;
  256. }
  257. PCIBus *ppce500_pci_init(qemu_irq pci_irqs[4], target_phys_addr_t registers)
  258. {
  259. PPCE500PCIState *controller;
  260. PCIDevice *d;
  261. int index;
  262. static int ppce500_pci_id;
  263. controller = qemu_mallocz(sizeof(PPCE500PCIState));
  264. controller->pci_state.bus = pci_register_bus(mpc85xx_pci_set_irq,
  265. mpc85xx_pci_map_irq,
  266. pci_irqs, 0x88, 4);
  267. d = pci_register_device(controller->pci_state.bus,
  268. "host bridge", sizeof(PCIDevice),
  269. 0, NULL, NULL);
  270. pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_FSL);
  271. pci_config_set_device_id(d->config, PCI_DEVICE_ID_FSL_E500);
  272. pci_config_set_class(d->config, PCI_CLASS_PROCESSOR_POWERPC);
  273. controller->pci_dev = d;
  274. /* CFGADDR */
  275. index = cpu_register_io_memory(0, pcie500_cfgaddr_read,
  276. pcie500_cfgaddr_write, controller);
  277. if (index < 0)
  278. goto free;
  279. cpu_register_physical_memory(registers + PCIE500_CFGADDR, 4, index);
  280. /* CFGDATA */
  281. index = cpu_register_io_memory(0, pcie500_cfgdata_read,
  282. pcie500_cfgdata_write,
  283. &controller->pci_state);
  284. if (index < 0)
  285. goto free;
  286. cpu_register_physical_memory(registers + PCIE500_CFGDATA, 4, index);
  287. index = cpu_register_io_memory(0, e500_pci_reg_read,
  288. e500_pci_reg_write, controller);
  289. if (index < 0)
  290. goto free;
  291. cpu_register_physical_memory(registers + PCIE500_REG_BASE,
  292. PCIE500_REG_SIZE, index);
  293. /* XXX load/save code not tested. */
  294. register_savevm("ppce500_pci", ppce500_pci_id++, 1,
  295. ppce500_pci_save, ppce500_pci_load, controller);
  296. return controller->pci_state.bus;
  297. free:
  298. printf("%s error\n", __func__);
  299. qemu_free(controller);
  300. return NULL;
  301. }