bonito.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /*
  2. * bonito north bridge support
  3. *
  4. * Copyright (c) 2008 yajin (yajin@vm-kernel.org)
  5. * Copyright (c) 2010 Huacai Chen (zltjiangshi@gmail.com)
  6. *
  7. * This code is licensed under the GNU GPL v2.
  8. *
  9. * Contributions after 2012-01-13 are licensed under the terms of the
  10. * GNU GPL, version 2 or (at your option) any later version.
  11. */
  12. /*
  13. * fulong 2e mini pc has a bonito north bridge.
  14. */
  15. /* what is the meaning of devfn in qemu and IDSEL in bonito northbridge?
  16. *
  17. * devfn pci_slot<<3 + funno
  18. * one pci bus can have 32 devices and each device can have 8 functions.
  19. *
  20. * In bonito north bridge, pci slot = IDSEL bit - 12.
  21. * For example, PCI_IDSEL_VIA686B = 17,
  22. * pci slot = 17-12=5
  23. *
  24. * so
  25. * VT686B_FUN0's devfn = (5<<3)+0
  26. * VT686B_FUN1's devfn = (5<<3)+1
  27. *
  28. * qemu also uses pci address for north bridge to access pci config register.
  29. * bus_no [23:16]
  30. * dev_no [15:11]
  31. * fun_no [10:8]
  32. * reg_no [7:2]
  33. *
  34. * so function bonito_sbridge_pciaddr for the translation from
  35. * north bridge address to pci address.
  36. */
  37. #include <assert.h>
  38. #include "hw.h"
  39. #include "pci.h"
  40. #include "pc.h"
  41. #include "mips.h"
  42. #include "pci_host.h"
  43. #include "sysemu.h"
  44. #include "exec-memory.h"
  45. //#define DEBUG_BONITO
  46. #ifdef DEBUG_BONITO
  47. #define DPRINTF(fmt, ...) fprintf(stderr, "%s: " fmt, __FUNCTION__, ##__VA_ARGS__)
  48. #else
  49. #define DPRINTF(fmt, ...)
  50. #endif
  51. /* from linux soure code. include/asm-mips/mips-boards/bonito64.h*/
  52. #define BONITO_BOOT_BASE 0x1fc00000
  53. #define BONITO_BOOT_SIZE 0x00100000
  54. #define BONITO_BOOT_TOP (BONITO_BOOT_BASE+BONITO_BOOT_SIZE-1)
  55. #define BONITO_FLASH_BASE 0x1c000000
  56. #define BONITO_FLASH_SIZE 0x03000000
  57. #define BONITO_FLASH_TOP (BONITO_FLASH_BASE+BONITO_FLASH_SIZE-1)
  58. #define BONITO_SOCKET_BASE 0x1f800000
  59. #define BONITO_SOCKET_SIZE 0x00400000
  60. #define BONITO_SOCKET_TOP (BONITO_SOCKET_BASE+BONITO_SOCKET_SIZE-1)
  61. #define BONITO_REG_BASE 0x1fe00000
  62. #define BONITO_REG_SIZE 0x00040000
  63. #define BONITO_REG_TOP (BONITO_REG_BASE+BONITO_REG_SIZE-1)
  64. #define BONITO_DEV_BASE 0x1ff00000
  65. #define BONITO_DEV_SIZE 0x00100000
  66. #define BONITO_DEV_TOP (BONITO_DEV_BASE+BONITO_DEV_SIZE-1)
  67. #define BONITO_PCILO_BASE 0x10000000
  68. #define BONITO_PCILO_BASE_VA 0xb0000000
  69. #define BONITO_PCILO_SIZE 0x0c000000
  70. #define BONITO_PCILO_TOP (BONITO_PCILO_BASE+BONITO_PCILO_SIZE-1)
  71. #define BONITO_PCILO0_BASE 0x10000000
  72. #define BONITO_PCILO1_BASE 0x14000000
  73. #define BONITO_PCILO2_BASE 0x18000000
  74. #define BONITO_PCIHI_BASE 0x20000000
  75. #define BONITO_PCIHI_SIZE 0x20000000
  76. #define BONITO_PCIHI_TOP (BONITO_PCIHI_BASE+BONITO_PCIHI_SIZE-1)
  77. #define BONITO_PCIIO_BASE 0x1fd00000
  78. #define BONITO_PCIIO_BASE_VA 0xbfd00000
  79. #define BONITO_PCIIO_SIZE 0x00010000
  80. #define BONITO_PCIIO_TOP (BONITO_PCIIO_BASE+BONITO_PCIIO_SIZE-1)
  81. #define BONITO_PCICFG_BASE 0x1fe80000
  82. #define BONITO_PCICFG_SIZE 0x00080000
  83. #define BONITO_PCICFG_TOP (BONITO_PCICFG_BASE+BONITO_PCICFG_SIZE-1)
  84. #define BONITO_PCICONFIGBASE 0x00
  85. #define BONITO_REGBASE 0x100
  86. #define BONITO_PCICONFIG_BASE (BONITO_PCICONFIGBASE+BONITO_REG_BASE)
  87. #define BONITO_PCICONFIG_SIZE (0x100)
  88. #define BONITO_INTERNAL_REG_BASE (BONITO_REGBASE+BONITO_REG_BASE)
  89. #define BONITO_INTERNAL_REG_SIZE (0x70)
  90. #define BONITO_SPCICONFIG_BASE (BONITO_PCICFG_BASE)
  91. #define BONITO_SPCICONFIG_SIZE (BONITO_PCICFG_SIZE)
  92. /* 1. Bonito h/w Configuration */
  93. /* Power on register */
  94. #define BONITO_BONPONCFG (0x00 >> 2) /* 0x100 */
  95. #define BONITO_BONGENCFG_OFFSET 0x4
  96. #define BONITO_BONGENCFG (BONITO_BONGENCFG_OFFSET>>2) /*0x104 */
  97. /* 2. IO & IDE configuration */
  98. #define BONITO_IODEVCFG (0x08 >> 2) /* 0x108 */
  99. /* 3. IO & IDE configuration */
  100. #define BONITO_SDCFG (0x0c >> 2) /* 0x10c */
  101. /* 4. PCI address map control */
  102. #define BONITO_PCIMAP (0x10 >> 2) /* 0x110 */
  103. #define BONITO_PCIMEMBASECFG (0x14 >> 2) /* 0x114 */
  104. #define BONITO_PCIMAP_CFG (0x18 >> 2) /* 0x118 */
  105. /* 5. ICU & GPIO regs */
  106. /* GPIO Regs - r/w */
  107. #define BONITO_GPIODATA_OFFSET 0x1c
  108. #define BONITO_GPIODATA (BONITO_GPIODATA_OFFSET >> 2) /* 0x11c */
  109. #define BONITO_GPIOIE (0x20 >> 2) /* 0x120 */
  110. /* ICU Configuration Regs - r/w */
  111. #define BONITO_INTEDGE (0x24 >> 2) /* 0x124 */
  112. #define BONITO_INTSTEER (0x28 >> 2) /* 0x128 */
  113. #define BONITO_INTPOL (0x2c >> 2) /* 0x12c */
  114. /* ICU Enable Regs - IntEn & IntISR are r/o. */
  115. #define BONITO_INTENSET (0x30 >> 2) /* 0x130 */
  116. #define BONITO_INTENCLR (0x34 >> 2) /* 0x134 */
  117. #define BONITO_INTEN (0x38 >> 2) /* 0x138 */
  118. #define BONITO_INTISR (0x3c >> 2) /* 0x13c */
  119. /* PCI mail boxes */
  120. #define BONITO_PCIMAIL0_OFFSET 0x40
  121. #define BONITO_PCIMAIL1_OFFSET 0x44
  122. #define BONITO_PCIMAIL2_OFFSET 0x48
  123. #define BONITO_PCIMAIL3_OFFSET 0x4c
  124. #define BONITO_PCIMAIL0 (0x40 >> 2) /* 0x140 */
  125. #define BONITO_PCIMAIL1 (0x44 >> 2) /* 0x144 */
  126. #define BONITO_PCIMAIL2 (0x48 >> 2) /* 0x148 */
  127. #define BONITO_PCIMAIL3 (0x4c >> 2) /* 0x14c */
  128. /* 6. PCI cache */
  129. #define BONITO_PCICACHECTRL (0x50 >> 2) /* 0x150 */
  130. #define BONITO_PCICACHETAG (0x54 >> 2) /* 0x154 */
  131. #define BONITO_PCIBADADDR (0x58 >> 2) /* 0x158 */
  132. #define BONITO_PCIMSTAT (0x5c >> 2) /* 0x15c */
  133. /* 7. other*/
  134. #define BONITO_TIMECFG (0x60 >> 2) /* 0x160 */
  135. #define BONITO_CPUCFG (0x64 >> 2) /* 0x164 */
  136. #define BONITO_DQCFG (0x68 >> 2) /* 0x168 */
  137. #define BONITO_MEMSIZE (0x6C >> 2) /* 0x16c */
  138. #define BONITO_REGS (0x70 >> 2)
  139. /* PCI config for south bridge. type 0 */
  140. #define BONITO_PCICONF_IDSEL_MASK 0xfffff800 /* [31:11] */
  141. #define BONITO_PCICONF_IDSEL_OFFSET 11
  142. #define BONITO_PCICONF_FUN_MASK 0x700 /* [10:8] */
  143. #define BONITO_PCICONF_FUN_OFFSET 8
  144. #define BONITO_PCICONF_REG_MASK 0xFC
  145. #define BONITO_PCICONF_REG_OFFSET 0
  146. /* idsel BIT = pci slot number +12 */
  147. #define PCI_SLOT_BASE 12
  148. #define PCI_IDSEL_VIA686B_BIT (17)
  149. #define PCI_IDSEL_VIA686B (1<<PCI_IDSEL_VIA686B_BIT)
  150. #define PCI_ADDR(busno,devno,funno,regno) \
  151. ((((busno)<<16)&0xff0000) + (((devno)<<11)&0xf800) + (((funno)<<8)&0x700) + (regno))
  152. #define TYPE_BONITO_PCI_HOST_BRIDGE "Bonito-pcihost"
  153. typedef struct BonitoState BonitoState;
  154. typedef struct PCIBonitoState
  155. {
  156. PCIDevice dev;
  157. BonitoState *pcihost;
  158. uint32_t regs[BONITO_REGS];
  159. struct bonldma {
  160. uint32_t ldmactrl;
  161. uint32_t ldmastat;
  162. uint32_t ldmaaddr;
  163. uint32_t ldmago;
  164. } bonldma;
  165. /* Based at 1fe00300, bonito Copier */
  166. struct boncop {
  167. uint32_t copctrl;
  168. uint32_t copstat;
  169. uint32_t coppaddr;
  170. uint32_t copgo;
  171. } boncop;
  172. /* Bonito registers */
  173. MemoryRegion iomem;
  174. MemoryRegion iomem_ldma;
  175. MemoryRegion iomem_cop;
  176. target_phys_addr_t bonito_pciio_start;
  177. target_phys_addr_t bonito_pciio_length;
  178. int bonito_pciio_handle;
  179. target_phys_addr_t bonito_localio_start;
  180. target_phys_addr_t bonito_localio_length;
  181. int bonito_localio_handle;
  182. } PCIBonitoState;
  183. #define BONITO_PCI_HOST_BRIDGE(obj) \
  184. OBJECT_CHECK(BonitoState, (obj), TYPE_BONITO_PCI_HOST_BRIDGE)
  185. struct BonitoState {
  186. PCIHostState parent_obj;
  187. qemu_irq *pic;
  188. PCIBonitoState *pci_dev;
  189. };
  190. static void bonito_writel(void *opaque, target_phys_addr_t addr,
  191. uint64_t val, unsigned size)
  192. {
  193. PCIBonitoState *s = opaque;
  194. uint32_t saddr;
  195. int reset = 0;
  196. saddr = (addr - BONITO_REGBASE) >> 2;
  197. DPRINTF("bonito_writel "TARGET_FMT_plx" val %x saddr %x\n", addr, val, saddr);
  198. switch (saddr) {
  199. case BONITO_BONPONCFG:
  200. case BONITO_IODEVCFG:
  201. case BONITO_SDCFG:
  202. case BONITO_PCIMAP:
  203. case BONITO_PCIMEMBASECFG:
  204. case BONITO_PCIMAP_CFG:
  205. case BONITO_GPIODATA:
  206. case BONITO_GPIOIE:
  207. case BONITO_INTEDGE:
  208. case BONITO_INTSTEER:
  209. case BONITO_INTPOL:
  210. case BONITO_PCIMAIL0:
  211. case BONITO_PCIMAIL1:
  212. case BONITO_PCIMAIL2:
  213. case BONITO_PCIMAIL3:
  214. case BONITO_PCICACHECTRL:
  215. case BONITO_PCICACHETAG:
  216. case BONITO_PCIBADADDR:
  217. case BONITO_PCIMSTAT:
  218. case BONITO_TIMECFG:
  219. case BONITO_CPUCFG:
  220. case BONITO_DQCFG:
  221. case BONITO_MEMSIZE:
  222. s->regs[saddr] = val;
  223. break;
  224. case BONITO_BONGENCFG:
  225. if (!(s->regs[saddr] & 0x04) && (val & 0x04)) {
  226. reset = 1; /* bit 2 jump from 0 to 1 cause reset */
  227. }
  228. s->regs[saddr] = val;
  229. if (reset) {
  230. qemu_system_reset_request();
  231. }
  232. break;
  233. case BONITO_INTENSET:
  234. s->regs[BONITO_INTENSET] = val;
  235. s->regs[BONITO_INTEN] |= val;
  236. break;
  237. case BONITO_INTENCLR:
  238. s->regs[BONITO_INTENCLR] = val;
  239. s->regs[BONITO_INTEN] &= ~val;
  240. break;
  241. case BONITO_INTEN:
  242. case BONITO_INTISR:
  243. DPRINTF("write to readonly bonito register %x\n", saddr);
  244. break;
  245. default:
  246. DPRINTF("write to unknown bonito register %x\n", saddr);
  247. break;
  248. }
  249. }
  250. static uint64_t bonito_readl(void *opaque, target_phys_addr_t addr,
  251. unsigned size)
  252. {
  253. PCIBonitoState *s = opaque;
  254. uint32_t saddr;
  255. saddr = (addr - BONITO_REGBASE) >> 2;
  256. DPRINTF("bonito_readl "TARGET_FMT_plx"\n", addr);
  257. switch (saddr) {
  258. case BONITO_INTISR:
  259. return s->regs[saddr];
  260. default:
  261. return s->regs[saddr];
  262. }
  263. }
  264. static const MemoryRegionOps bonito_ops = {
  265. .read = bonito_readl,
  266. .write = bonito_writel,
  267. .endianness = DEVICE_NATIVE_ENDIAN,
  268. .valid = {
  269. .min_access_size = 4,
  270. .max_access_size = 4,
  271. },
  272. };
  273. static void bonito_pciconf_writel(void *opaque, target_phys_addr_t addr,
  274. uint64_t val, unsigned size)
  275. {
  276. PCIBonitoState *s = opaque;
  277. PCIDevice *d = PCI_DEVICE(s);
  278. DPRINTF("bonito_pciconf_writel "TARGET_FMT_plx" val %x\n", addr, val);
  279. d->config_write(d, addr, val, 4);
  280. }
  281. static uint64_t bonito_pciconf_readl(void *opaque, target_phys_addr_t addr,
  282. unsigned size)
  283. {
  284. PCIBonitoState *s = opaque;
  285. PCIDevice *d = PCI_DEVICE(s);
  286. DPRINTF("bonito_pciconf_readl "TARGET_FMT_plx"\n", addr);
  287. return d->config_read(d, addr, 4);
  288. }
  289. /* north bridge PCI configure space. 0x1fe0 0000 - 0x1fe0 00ff */
  290. static const MemoryRegionOps bonito_pciconf_ops = {
  291. .read = bonito_pciconf_readl,
  292. .write = bonito_pciconf_writel,
  293. .endianness = DEVICE_NATIVE_ENDIAN,
  294. .valid = {
  295. .min_access_size = 4,
  296. .max_access_size = 4,
  297. },
  298. };
  299. static uint64_t bonito_ldma_readl(void *opaque, target_phys_addr_t addr,
  300. unsigned size)
  301. {
  302. uint32_t val;
  303. PCIBonitoState *s = opaque;
  304. val = ((uint32_t *)(&s->bonldma))[addr/sizeof(uint32_t)];
  305. return val;
  306. }
  307. static void bonito_ldma_writel(void *opaque, target_phys_addr_t addr,
  308. uint64_t val, unsigned size)
  309. {
  310. PCIBonitoState *s = opaque;
  311. ((uint32_t *)(&s->bonldma))[addr/sizeof(uint32_t)] = val & 0xffffffff;
  312. }
  313. static const MemoryRegionOps bonito_ldma_ops = {
  314. .read = bonito_ldma_readl,
  315. .write = bonito_ldma_writel,
  316. .endianness = DEVICE_NATIVE_ENDIAN,
  317. .valid = {
  318. .min_access_size = 4,
  319. .max_access_size = 4,
  320. },
  321. };
  322. static uint64_t bonito_cop_readl(void *opaque, target_phys_addr_t addr,
  323. unsigned size)
  324. {
  325. uint32_t val;
  326. PCIBonitoState *s = opaque;
  327. val = ((uint32_t *)(&s->boncop))[addr/sizeof(uint32_t)];
  328. return val;
  329. }
  330. static void bonito_cop_writel(void *opaque, target_phys_addr_t addr,
  331. uint64_t val, unsigned size)
  332. {
  333. PCIBonitoState *s = opaque;
  334. ((uint32_t *)(&s->boncop))[addr/sizeof(uint32_t)] = val & 0xffffffff;
  335. }
  336. static const MemoryRegionOps bonito_cop_ops = {
  337. .read = bonito_cop_readl,
  338. .write = bonito_cop_writel,
  339. .endianness = DEVICE_NATIVE_ENDIAN,
  340. .valid = {
  341. .min_access_size = 4,
  342. .max_access_size = 4,
  343. },
  344. };
  345. static uint32_t bonito_sbridge_pciaddr(void *opaque, target_phys_addr_t addr)
  346. {
  347. PCIBonitoState *s = opaque;
  348. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  349. uint32_t cfgaddr;
  350. uint32_t idsel;
  351. uint32_t devno;
  352. uint32_t funno;
  353. uint32_t regno;
  354. uint32_t pciaddr;
  355. /* support type0 pci config */
  356. if ((s->regs[BONITO_PCIMAP_CFG] & 0x10000) != 0x0) {
  357. return 0xffffffff;
  358. }
  359. cfgaddr = addr & 0xffff;
  360. cfgaddr |= (s->regs[BONITO_PCIMAP_CFG] & 0xffff) << 16;
  361. idsel = (cfgaddr & BONITO_PCICONF_IDSEL_MASK) >> BONITO_PCICONF_IDSEL_OFFSET;
  362. devno = ffs(idsel) - 1;
  363. funno = (cfgaddr & BONITO_PCICONF_FUN_MASK) >> BONITO_PCICONF_FUN_OFFSET;
  364. regno = (cfgaddr & BONITO_PCICONF_REG_MASK) >> BONITO_PCICONF_REG_OFFSET;
  365. if (idsel == 0) {
  366. fprintf(stderr, "error in bonito pci config address " TARGET_FMT_plx
  367. ",pcimap_cfg=%x\n", addr, s->regs[BONITO_PCIMAP_CFG]);
  368. exit(1);
  369. }
  370. pciaddr = PCI_ADDR(pci_bus_num(phb->bus), devno, funno, regno);
  371. DPRINTF("cfgaddr %x pciaddr %x busno %x devno %d funno %d regno %d\n",
  372. cfgaddr, pciaddr, pci_bus_num(phb->bus), devno, funno, regno);
  373. return pciaddr;
  374. }
  375. static void bonito_spciconf_writeb(void *opaque, target_phys_addr_t addr,
  376. uint32_t val)
  377. {
  378. PCIBonitoState *s = opaque;
  379. PCIDevice *d = PCI_DEVICE(s);
  380. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  381. uint32_t pciaddr;
  382. uint16_t status;
  383. DPRINTF("bonito_spciconf_writeb "TARGET_FMT_plx" val %x\n", addr, val);
  384. pciaddr = bonito_sbridge_pciaddr(s, addr);
  385. if (pciaddr == 0xffffffff) {
  386. return;
  387. }
  388. /* set the pci address in s->config_reg */
  389. phb->config_reg = (pciaddr) | (1u << 31);
  390. pci_data_write(phb->bus, phb->config_reg, val & 0xff, 1);
  391. /* clear PCI_STATUS_REC_MASTER_ABORT and PCI_STATUS_REC_TARGET_ABORT */
  392. status = pci_get_word(d->config + PCI_STATUS);
  393. status &= ~(PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT);
  394. pci_set_word(d->config + PCI_STATUS, status);
  395. }
  396. static void bonito_spciconf_writew(void *opaque, target_phys_addr_t addr,
  397. uint32_t val)
  398. {
  399. PCIBonitoState *s = opaque;
  400. PCIDevice *d = PCI_DEVICE(s);
  401. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  402. uint32_t pciaddr;
  403. uint16_t status;
  404. DPRINTF("bonito_spciconf_writew "TARGET_FMT_plx" val %x\n", addr, val);
  405. assert((addr & 0x1) == 0);
  406. pciaddr = bonito_sbridge_pciaddr(s, addr);
  407. if (pciaddr == 0xffffffff) {
  408. return;
  409. }
  410. /* set the pci address in s->config_reg */
  411. phb->config_reg = (pciaddr) | (1u << 31);
  412. pci_data_write(phb->bus, phb->config_reg, val, 2);
  413. /* clear PCI_STATUS_REC_MASTER_ABORT and PCI_STATUS_REC_TARGET_ABORT */
  414. status = pci_get_word(d->config + PCI_STATUS);
  415. status &= ~(PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT);
  416. pci_set_word(d->config + PCI_STATUS, status);
  417. }
  418. static void bonito_spciconf_writel(void *opaque, target_phys_addr_t addr,
  419. uint32_t val)
  420. {
  421. PCIBonitoState *s = opaque;
  422. PCIDevice *d = PCI_DEVICE(s);
  423. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  424. uint32_t pciaddr;
  425. uint16_t status;
  426. DPRINTF("bonito_spciconf_writel "TARGET_FMT_plx" val %x\n", addr, val);
  427. assert((addr & 0x3) == 0);
  428. pciaddr = bonito_sbridge_pciaddr(s, addr);
  429. if (pciaddr == 0xffffffff) {
  430. return;
  431. }
  432. /* set the pci address in s->config_reg */
  433. phb->config_reg = (pciaddr) | (1u << 31);
  434. pci_data_write(phb->bus, phb->config_reg, val, 4);
  435. /* clear PCI_STATUS_REC_MASTER_ABORT and PCI_STATUS_REC_TARGET_ABORT */
  436. status = pci_get_word(d->config + PCI_STATUS);
  437. status &= ~(PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT);
  438. pci_set_word(d->config + PCI_STATUS, status);
  439. }
  440. static uint32_t bonito_spciconf_readb(void *opaque, target_phys_addr_t addr)
  441. {
  442. PCIBonitoState *s = opaque;
  443. PCIDevice *d = PCI_DEVICE(s);
  444. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  445. uint32_t pciaddr;
  446. uint16_t status;
  447. DPRINTF("bonito_spciconf_readb "TARGET_FMT_plx"\n", addr);
  448. pciaddr = bonito_sbridge_pciaddr(s, addr);
  449. if (pciaddr == 0xffffffff) {
  450. return 0xff;
  451. }
  452. /* set the pci address in s->config_reg */
  453. phb->config_reg = (pciaddr) | (1u << 31);
  454. /* clear PCI_STATUS_REC_MASTER_ABORT and PCI_STATUS_REC_TARGET_ABORT */
  455. status = pci_get_word(d->config + PCI_STATUS);
  456. status &= ~(PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT);
  457. pci_set_word(d->config + PCI_STATUS, status);
  458. return pci_data_read(phb->bus, phb->config_reg, 1);
  459. }
  460. static uint32_t bonito_spciconf_readw(void *opaque, target_phys_addr_t addr)
  461. {
  462. PCIBonitoState *s = opaque;
  463. PCIDevice *d = PCI_DEVICE(s);
  464. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  465. uint32_t pciaddr;
  466. uint16_t status;
  467. DPRINTF("bonito_spciconf_readw "TARGET_FMT_plx"\n", addr);
  468. assert((addr & 0x1) == 0);
  469. pciaddr = bonito_sbridge_pciaddr(s, addr);
  470. if (pciaddr == 0xffffffff) {
  471. return 0xffff;
  472. }
  473. /* set the pci address in s->config_reg */
  474. phb->config_reg = (pciaddr) | (1u << 31);
  475. /* clear PCI_STATUS_REC_MASTER_ABORT and PCI_STATUS_REC_TARGET_ABORT */
  476. status = pci_get_word(d->config + PCI_STATUS);
  477. status &= ~(PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT);
  478. pci_set_word(d->config + PCI_STATUS, status);
  479. return pci_data_read(phb->bus, phb->config_reg, 2);
  480. }
  481. static uint32_t bonito_spciconf_readl(void *opaque, target_phys_addr_t addr)
  482. {
  483. PCIBonitoState *s = opaque;
  484. PCIDevice *d = PCI_DEVICE(s);
  485. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  486. uint32_t pciaddr;
  487. uint16_t status;
  488. DPRINTF("bonito_spciconf_readl "TARGET_FMT_plx"\n", addr);
  489. assert((addr & 0x3) == 0);
  490. pciaddr = bonito_sbridge_pciaddr(s, addr);
  491. if (pciaddr == 0xffffffff) {
  492. return 0xffffffff;
  493. }
  494. /* set the pci address in s->config_reg */
  495. phb->config_reg = (pciaddr) | (1u << 31);
  496. /* clear PCI_STATUS_REC_MASTER_ABORT and PCI_STATUS_REC_TARGET_ABORT */
  497. status = pci_get_word(d->config + PCI_STATUS);
  498. status &= ~(PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT);
  499. pci_set_word(d->config + PCI_STATUS, status);
  500. return pci_data_read(phb->bus, phb->config_reg, 4);
  501. }
  502. /* south bridge PCI configure space. 0x1fe8 0000 - 0x1fef ffff */
  503. static const MemoryRegionOps bonito_spciconf_ops = {
  504. .old_mmio = {
  505. .read = {
  506. bonito_spciconf_readb,
  507. bonito_spciconf_readw,
  508. bonito_spciconf_readl,
  509. },
  510. .write = {
  511. bonito_spciconf_writeb,
  512. bonito_spciconf_writew,
  513. bonito_spciconf_writel,
  514. },
  515. },
  516. .endianness = DEVICE_NATIVE_ENDIAN,
  517. };
  518. #define BONITO_IRQ_BASE 32
  519. static void pci_bonito_set_irq(void *opaque, int irq_num, int level)
  520. {
  521. BonitoState *s = opaque;
  522. qemu_irq *pic = s->pic;
  523. PCIBonitoState *bonito_state = s->pci_dev;
  524. int internal_irq = irq_num - BONITO_IRQ_BASE;
  525. if (bonito_state->regs[BONITO_INTEDGE] & (1 << internal_irq)) {
  526. qemu_irq_pulse(*pic);
  527. } else { /* level triggered */
  528. if (bonito_state->regs[BONITO_INTPOL] & (1 << internal_irq)) {
  529. qemu_irq_raise(*pic);
  530. } else {
  531. qemu_irq_lower(*pic);
  532. }
  533. }
  534. }
  535. /* map the original irq (0~3) to bonito irq (16~47, but 16~31 are unused) */
  536. static int pci_bonito_map_irq(PCIDevice * pci_dev, int irq_num)
  537. {
  538. int slot;
  539. slot = (pci_dev->devfn >> 3);
  540. switch (slot) {
  541. case 5: /* FULONG2E_VIA_SLOT, SouthBridge, IDE, USB, ACPI, AC97, MC97 */
  542. return irq_num % 4 + BONITO_IRQ_BASE;
  543. case 6: /* FULONG2E_ATI_SLOT, VGA */
  544. return 4 + BONITO_IRQ_BASE;
  545. case 7: /* FULONG2E_RTL_SLOT, RTL8139 */
  546. return 5 + BONITO_IRQ_BASE;
  547. case 8 ... 12: /* PCI slot 1 to 4 */
  548. return (slot - 8 + irq_num) + 6 + BONITO_IRQ_BASE;
  549. default: /* Unknown device, don't do any translation */
  550. return irq_num;
  551. }
  552. }
  553. static void bonito_reset(void *opaque)
  554. {
  555. PCIBonitoState *s = opaque;
  556. /* set the default value of north bridge registers */
  557. s->regs[BONITO_BONPONCFG] = 0xc40;
  558. s->regs[BONITO_BONGENCFG] = 0x1384;
  559. s->regs[BONITO_IODEVCFG] = 0x2bff8010;
  560. s->regs[BONITO_SDCFG] = 0x255e0091;
  561. s->regs[BONITO_GPIODATA] = 0x1ff;
  562. s->regs[BONITO_GPIOIE] = 0x1ff;
  563. s->regs[BONITO_DQCFG] = 0x8;
  564. s->regs[BONITO_MEMSIZE] = 0x10000000;
  565. s->regs[BONITO_PCIMAP] = 0x6140;
  566. }
  567. static const VMStateDescription vmstate_bonito = {
  568. .name = "Bonito",
  569. .version_id = 1,
  570. .minimum_version_id = 1,
  571. .minimum_version_id_old = 1,
  572. .fields = (VMStateField []) {
  573. VMSTATE_PCI_DEVICE(dev, PCIBonitoState),
  574. VMSTATE_END_OF_LIST()
  575. }
  576. };
  577. static int bonito_pcihost_initfn(SysBusDevice *dev)
  578. {
  579. PCIHostState *phb = PCI_HOST_BRIDGE(dev);
  580. phb->bus = pci_register_bus(DEVICE(dev), "pci",
  581. pci_bonito_set_irq, pci_bonito_map_irq, dev,
  582. get_system_memory(), get_system_io(),
  583. 0x28, 32);
  584. return 0;
  585. }
  586. static int bonito_initfn(PCIDevice *dev)
  587. {
  588. PCIBonitoState *s = DO_UPCAST(PCIBonitoState, dev, dev);
  589. SysBusDevice *sysbus = SYS_BUS_DEVICE(s->pcihost);
  590. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  591. /* Bonito North Bridge, built on FPGA, VENDOR_ID/DEVICE_ID are "undefined" */
  592. pci_config_set_prog_interface(dev->config, 0x00);
  593. /* set the north bridge register mapping */
  594. memory_region_init_io(&s->iomem, &bonito_ops, s,
  595. "north-bridge-register", BONITO_INTERNAL_REG_SIZE);
  596. sysbus_init_mmio(sysbus, &s->iomem);
  597. sysbus_mmio_map(sysbus, 0, BONITO_INTERNAL_REG_BASE);
  598. /* set the north bridge pci configure mapping */
  599. memory_region_init_io(&phb->conf_mem, &bonito_pciconf_ops, s,
  600. "north-bridge-pci-config", BONITO_PCICONFIG_SIZE);
  601. sysbus_init_mmio(sysbus, &phb->conf_mem);
  602. sysbus_mmio_map(sysbus, 1, BONITO_PCICONFIG_BASE);
  603. /* set the south bridge pci configure mapping */
  604. memory_region_init_io(&phb->data_mem, &bonito_spciconf_ops, s,
  605. "south-bridge-pci-config", BONITO_SPCICONFIG_SIZE);
  606. sysbus_init_mmio(sysbus, &phb->data_mem);
  607. sysbus_mmio_map(sysbus, 2, BONITO_SPCICONFIG_BASE);
  608. memory_region_init_io(&s->iomem_ldma, &bonito_ldma_ops, s,
  609. "ldma", 0x100);
  610. sysbus_init_mmio(sysbus, &s->iomem_ldma);
  611. sysbus_mmio_map(sysbus, 3, 0xbfe00200);
  612. memory_region_init_io(&s->iomem_cop, &bonito_cop_ops, s,
  613. "cop", 0x100);
  614. sysbus_init_mmio(sysbus, &s->iomem_cop);
  615. sysbus_mmio_map(sysbus, 4, 0xbfe00300);
  616. /* Map PCI IO Space 0x1fd0 0000 - 0x1fd1 0000 */
  617. s->bonito_pciio_start = BONITO_PCIIO_BASE;
  618. s->bonito_pciio_length = BONITO_PCIIO_SIZE;
  619. isa_mem_base = s->bonito_pciio_start;
  620. isa_mmio_init(s->bonito_pciio_start, s->bonito_pciio_length);
  621. /* add pci local io mapping */
  622. s->bonito_localio_start = BONITO_DEV_BASE;
  623. s->bonito_localio_length = BONITO_DEV_SIZE;
  624. isa_mmio_init(s->bonito_localio_start, s->bonito_localio_length);
  625. /* set the default value of north bridge pci config */
  626. pci_set_word(dev->config + PCI_COMMAND, 0x0000);
  627. pci_set_word(dev->config + PCI_STATUS, 0x0000);
  628. pci_set_word(dev->config + PCI_SUBSYSTEM_VENDOR_ID, 0x0000);
  629. pci_set_word(dev->config + PCI_SUBSYSTEM_ID, 0x0000);
  630. pci_set_byte(dev->config + PCI_INTERRUPT_LINE, 0x00);
  631. pci_set_byte(dev->config + PCI_INTERRUPT_PIN, 0x01);
  632. pci_set_byte(dev->config + PCI_MIN_GNT, 0x3c);
  633. pci_set_byte(dev->config + PCI_MAX_LAT, 0x00);
  634. qemu_register_reset(bonito_reset, s);
  635. return 0;
  636. }
  637. PCIBus *bonito_init(qemu_irq *pic)
  638. {
  639. DeviceState *dev;
  640. BonitoState *pcihost;
  641. PCIHostState *phb;
  642. PCIBonitoState *s;
  643. PCIDevice *d;
  644. dev = qdev_create(NULL, TYPE_BONITO_PCI_HOST_BRIDGE);
  645. phb = PCI_HOST_BRIDGE(dev);
  646. pcihost = BONITO_PCI_HOST_BRIDGE(dev);
  647. pcihost->pic = pic;
  648. qdev_init_nofail(dev);
  649. /* set the pcihost pointer before bonito_initfn is called */
  650. d = pci_create(phb->bus, PCI_DEVFN(0, 0), "Bonito");
  651. s = DO_UPCAST(PCIBonitoState, dev, d);
  652. s->pcihost = pcihost;
  653. pcihost->pci_dev = s;
  654. qdev_init_nofail(DEVICE(d));
  655. return phb->bus;
  656. }
  657. static void bonito_class_init(ObjectClass *klass, void *data)
  658. {
  659. DeviceClass *dc = DEVICE_CLASS(klass);
  660. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  661. k->init = bonito_initfn;
  662. k->vendor_id = 0xdf53;
  663. k->device_id = 0x00d5;
  664. k->revision = 0x01;
  665. k->class_id = PCI_CLASS_BRIDGE_HOST;
  666. dc->desc = "Host bridge";
  667. dc->no_user = 1;
  668. dc->vmsd = &vmstate_bonito;
  669. }
  670. static const TypeInfo bonito_info = {
  671. .name = "Bonito",
  672. .parent = TYPE_PCI_DEVICE,
  673. .instance_size = sizeof(PCIBonitoState),
  674. .class_init = bonito_class_init,
  675. };
  676. static void bonito_pcihost_class_init(ObjectClass *klass, void *data)
  677. {
  678. DeviceClass *dc = DEVICE_CLASS(klass);
  679. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  680. k->init = bonito_pcihost_initfn;
  681. dc->no_user = 1;
  682. }
  683. static const TypeInfo bonito_pcihost_info = {
  684. .name = TYPE_BONITO_PCI_HOST_BRIDGE,
  685. .parent = TYPE_PCI_HOST_BRIDGE,
  686. .instance_size = sizeof(BonitoState),
  687. .class_init = bonito_pcihost_class_init,
  688. };
  689. static void bonito_register_types(void)
  690. {
  691. type_register_static(&bonito_pcihost_info);
  692. type_register_static(&bonito_info);
  693. }
  694. type_init(bonito_register_types)