2
0

bonito.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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/hw.h"
  39. #include "hw/pci/pci.h"
  40. #include "hw/i386/pc.h"
  41. #include "hw/mips/mips.h"
  42. #include "hw/pci/pci_host.h"
  43. #include "sysemu/sysemu.h"
  44. #include "exec/address-spaces.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. MemoryRegion bonito_pciio;
  177. MemoryRegion bonito_localio;
  178. } PCIBonitoState;
  179. #define BONITO_PCI_HOST_BRIDGE(obj) \
  180. OBJECT_CHECK(BonitoState, (obj), TYPE_BONITO_PCI_HOST_BRIDGE)
  181. struct BonitoState {
  182. PCIHostState parent_obj;
  183. qemu_irq *pic;
  184. PCIBonitoState *pci_dev;
  185. };
  186. static void bonito_writel(void *opaque, hwaddr addr,
  187. uint64_t val, unsigned size)
  188. {
  189. PCIBonitoState *s = opaque;
  190. uint32_t saddr;
  191. int reset = 0;
  192. saddr = (addr - BONITO_REGBASE) >> 2;
  193. DPRINTF("bonito_writel "TARGET_FMT_plx" val %x saddr %x\n", addr, val, saddr);
  194. switch (saddr) {
  195. case BONITO_BONPONCFG:
  196. case BONITO_IODEVCFG:
  197. case BONITO_SDCFG:
  198. case BONITO_PCIMAP:
  199. case BONITO_PCIMEMBASECFG:
  200. case BONITO_PCIMAP_CFG:
  201. case BONITO_GPIODATA:
  202. case BONITO_GPIOIE:
  203. case BONITO_INTEDGE:
  204. case BONITO_INTSTEER:
  205. case BONITO_INTPOL:
  206. case BONITO_PCIMAIL0:
  207. case BONITO_PCIMAIL1:
  208. case BONITO_PCIMAIL2:
  209. case BONITO_PCIMAIL3:
  210. case BONITO_PCICACHECTRL:
  211. case BONITO_PCICACHETAG:
  212. case BONITO_PCIBADADDR:
  213. case BONITO_PCIMSTAT:
  214. case BONITO_TIMECFG:
  215. case BONITO_CPUCFG:
  216. case BONITO_DQCFG:
  217. case BONITO_MEMSIZE:
  218. s->regs[saddr] = val;
  219. break;
  220. case BONITO_BONGENCFG:
  221. if (!(s->regs[saddr] & 0x04) && (val & 0x04)) {
  222. reset = 1; /* bit 2 jump from 0 to 1 cause reset */
  223. }
  224. s->regs[saddr] = val;
  225. if (reset) {
  226. qemu_system_reset_request();
  227. }
  228. break;
  229. case BONITO_INTENSET:
  230. s->regs[BONITO_INTENSET] = val;
  231. s->regs[BONITO_INTEN] |= val;
  232. break;
  233. case BONITO_INTENCLR:
  234. s->regs[BONITO_INTENCLR] = val;
  235. s->regs[BONITO_INTEN] &= ~val;
  236. break;
  237. case BONITO_INTEN:
  238. case BONITO_INTISR:
  239. DPRINTF("write to readonly bonito register %x\n", saddr);
  240. break;
  241. default:
  242. DPRINTF("write to unknown bonito register %x\n", saddr);
  243. break;
  244. }
  245. }
  246. static uint64_t bonito_readl(void *opaque, hwaddr addr,
  247. unsigned size)
  248. {
  249. PCIBonitoState *s = opaque;
  250. uint32_t saddr;
  251. saddr = (addr - BONITO_REGBASE) >> 2;
  252. DPRINTF("bonito_readl "TARGET_FMT_plx"\n", addr);
  253. switch (saddr) {
  254. case BONITO_INTISR:
  255. return s->regs[saddr];
  256. default:
  257. return s->regs[saddr];
  258. }
  259. }
  260. static const MemoryRegionOps bonito_ops = {
  261. .read = bonito_readl,
  262. .write = bonito_writel,
  263. .endianness = DEVICE_NATIVE_ENDIAN,
  264. .valid = {
  265. .min_access_size = 4,
  266. .max_access_size = 4,
  267. },
  268. };
  269. static void bonito_pciconf_writel(void *opaque, hwaddr addr,
  270. uint64_t val, unsigned size)
  271. {
  272. PCIBonitoState *s = opaque;
  273. PCIDevice *d = PCI_DEVICE(s);
  274. DPRINTF("bonito_pciconf_writel "TARGET_FMT_plx" val %x\n", addr, val);
  275. d->config_write(d, addr, val, 4);
  276. }
  277. static uint64_t bonito_pciconf_readl(void *opaque, hwaddr addr,
  278. unsigned size)
  279. {
  280. PCIBonitoState *s = opaque;
  281. PCIDevice *d = PCI_DEVICE(s);
  282. DPRINTF("bonito_pciconf_readl "TARGET_FMT_plx"\n", addr);
  283. return d->config_read(d, addr, 4);
  284. }
  285. /* north bridge PCI configure space. 0x1fe0 0000 - 0x1fe0 00ff */
  286. static const MemoryRegionOps bonito_pciconf_ops = {
  287. .read = bonito_pciconf_readl,
  288. .write = bonito_pciconf_writel,
  289. .endianness = DEVICE_NATIVE_ENDIAN,
  290. .valid = {
  291. .min_access_size = 4,
  292. .max_access_size = 4,
  293. },
  294. };
  295. static uint64_t bonito_ldma_readl(void *opaque, hwaddr addr,
  296. unsigned size)
  297. {
  298. uint32_t val;
  299. PCIBonitoState *s = opaque;
  300. val = ((uint32_t *)(&s->bonldma))[addr/sizeof(uint32_t)];
  301. return val;
  302. }
  303. static void bonito_ldma_writel(void *opaque, hwaddr addr,
  304. uint64_t val, unsigned size)
  305. {
  306. PCIBonitoState *s = opaque;
  307. ((uint32_t *)(&s->bonldma))[addr/sizeof(uint32_t)] = val & 0xffffffff;
  308. }
  309. static const MemoryRegionOps bonito_ldma_ops = {
  310. .read = bonito_ldma_readl,
  311. .write = bonito_ldma_writel,
  312. .endianness = DEVICE_NATIVE_ENDIAN,
  313. .valid = {
  314. .min_access_size = 4,
  315. .max_access_size = 4,
  316. },
  317. };
  318. static uint64_t bonito_cop_readl(void *opaque, hwaddr addr,
  319. unsigned size)
  320. {
  321. uint32_t val;
  322. PCIBonitoState *s = opaque;
  323. val = ((uint32_t *)(&s->boncop))[addr/sizeof(uint32_t)];
  324. return val;
  325. }
  326. static void bonito_cop_writel(void *opaque, hwaddr addr,
  327. uint64_t val, unsigned size)
  328. {
  329. PCIBonitoState *s = opaque;
  330. ((uint32_t *)(&s->boncop))[addr/sizeof(uint32_t)] = val & 0xffffffff;
  331. }
  332. static const MemoryRegionOps bonito_cop_ops = {
  333. .read = bonito_cop_readl,
  334. .write = bonito_cop_writel,
  335. .endianness = DEVICE_NATIVE_ENDIAN,
  336. .valid = {
  337. .min_access_size = 4,
  338. .max_access_size = 4,
  339. },
  340. };
  341. static uint32_t bonito_sbridge_pciaddr(void *opaque, hwaddr addr)
  342. {
  343. PCIBonitoState *s = opaque;
  344. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  345. uint32_t cfgaddr;
  346. uint32_t idsel;
  347. uint32_t devno;
  348. uint32_t funno;
  349. uint32_t regno;
  350. uint32_t pciaddr;
  351. /* support type0 pci config */
  352. if ((s->regs[BONITO_PCIMAP_CFG] & 0x10000) != 0x0) {
  353. return 0xffffffff;
  354. }
  355. cfgaddr = addr & 0xffff;
  356. cfgaddr |= (s->regs[BONITO_PCIMAP_CFG] & 0xffff) << 16;
  357. idsel = (cfgaddr & BONITO_PCICONF_IDSEL_MASK) >> BONITO_PCICONF_IDSEL_OFFSET;
  358. devno = ffs(idsel) - 1;
  359. funno = (cfgaddr & BONITO_PCICONF_FUN_MASK) >> BONITO_PCICONF_FUN_OFFSET;
  360. regno = (cfgaddr & BONITO_PCICONF_REG_MASK) >> BONITO_PCICONF_REG_OFFSET;
  361. if (idsel == 0) {
  362. fprintf(stderr, "error in bonito pci config address " TARGET_FMT_plx
  363. ",pcimap_cfg=%x\n", addr, s->regs[BONITO_PCIMAP_CFG]);
  364. exit(1);
  365. }
  366. pciaddr = PCI_ADDR(pci_bus_num(phb->bus), devno, funno, regno);
  367. DPRINTF("cfgaddr %x pciaddr %x busno %x devno %d funno %d regno %d\n",
  368. cfgaddr, pciaddr, pci_bus_num(phb->bus), devno, funno, regno);
  369. return pciaddr;
  370. }
  371. static void bonito_spciconf_writeb(void *opaque, hwaddr addr,
  372. uint32_t val)
  373. {
  374. PCIBonitoState *s = opaque;
  375. PCIDevice *d = PCI_DEVICE(s);
  376. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  377. uint32_t pciaddr;
  378. uint16_t status;
  379. DPRINTF("bonito_spciconf_writeb "TARGET_FMT_plx" val %x\n", addr, val);
  380. pciaddr = bonito_sbridge_pciaddr(s, addr);
  381. if (pciaddr == 0xffffffff) {
  382. return;
  383. }
  384. /* set the pci address in s->config_reg */
  385. phb->config_reg = (pciaddr) | (1u << 31);
  386. pci_data_write(phb->bus, phb->config_reg, val & 0xff, 1);
  387. /* clear PCI_STATUS_REC_MASTER_ABORT and PCI_STATUS_REC_TARGET_ABORT */
  388. status = pci_get_word(d->config + PCI_STATUS);
  389. status &= ~(PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT);
  390. pci_set_word(d->config + PCI_STATUS, status);
  391. }
  392. static void bonito_spciconf_writew(void *opaque, hwaddr addr,
  393. uint32_t val)
  394. {
  395. PCIBonitoState *s = opaque;
  396. PCIDevice *d = PCI_DEVICE(s);
  397. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  398. uint32_t pciaddr;
  399. uint16_t status;
  400. DPRINTF("bonito_spciconf_writew "TARGET_FMT_plx" val %x\n", addr, val);
  401. assert((addr & 0x1) == 0);
  402. pciaddr = bonito_sbridge_pciaddr(s, addr);
  403. if (pciaddr == 0xffffffff) {
  404. return;
  405. }
  406. /* set the pci address in s->config_reg */
  407. phb->config_reg = (pciaddr) | (1u << 31);
  408. pci_data_write(phb->bus, phb->config_reg, val, 2);
  409. /* clear PCI_STATUS_REC_MASTER_ABORT and PCI_STATUS_REC_TARGET_ABORT */
  410. status = pci_get_word(d->config + PCI_STATUS);
  411. status &= ~(PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT);
  412. pci_set_word(d->config + PCI_STATUS, status);
  413. }
  414. static void bonito_spciconf_writel(void *opaque, hwaddr addr,
  415. uint32_t val)
  416. {
  417. PCIBonitoState *s = opaque;
  418. PCIDevice *d = PCI_DEVICE(s);
  419. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  420. uint32_t pciaddr;
  421. uint16_t status;
  422. DPRINTF("bonito_spciconf_writel "TARGET_FMT_plx" val %x\n", addr, val);
  423. assert((addr & 0x3) == 0);
  424. pciaddr = bonito_sbridge_pciaddr(s, addr);
  425. if (pciaddr == 0xffffffff) {
  426. return;
  427. }
  428. /* set the pci address in s->config_reg */
  429. phb->config_reg = (pciaddr) | (1u << 31);
  430. pci_data_write(phb->bus, phb->config_reg, val, 4);
  431. /* clear PCI_STATUS_REC_MASTER_ABORT and PCI_STATUS_REC_TARGET_ABORT */
  432. status = pci_get_word(d->config + PCI_STATUS);
  433. status &= ~(PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT);
  434. pci_set_word(d->config + PCI_STATUS, status);
  435. }
  436. static uint32_t bonito_spciconf_readb(void *opaque, hwaddr addr)
  437. {
  438. PCIBonitoState *s = opaque;
  439. PCIDevice *d = PCI_DEVICE(s);
  440. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  441. uint32_t pciaddr;
  442. uint16_t status;
  443. DPRINTF("bonito_spciconf_readb "TARGET_FMT_plx"\n", addr);
  444. pciaddr = bonito_sbridge_pciaddr(s, addr);
  445. if (pciaddr == 0xffffffff) {
  446. return 0xff;
  447. }
  448. /* set the pci address in s->config_reg */
  449. phb->config_reg = (pciaddr) | (1u << 31);
  450. /* clear PCI_STATUS_REC_MASTER_ABORT and PCI_STATUS_REC_TARGET_ABORT */
  451. status = pci_get_word(d->config + PCI_STATUS);
  452. status &= ~(PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT);
  453. pci_set_word(d->config + PCI_STATUS, status);
  454. return pci_data_read(phb->bus, phb->config_reg, 1);
  455. }
  456. static uint32_t bonito_spciconf_readw(void *opaque, hwaddr addr)
  457. {
  458. PCIBonitoState *s = opaque;
  459. PCIDevice *d = PCI_DEVICE(s);
  460. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  461. uint32_t pciaddr;
  462. uint16_t status;
  463. DPRINTF("bonito_spciconf_readw "TARGET_FMT_plx"\n", addr);
  464. assert((addr & 0x1) == 0);
  465. pciaddr = bonito_sbridge_pciaddr(s, addr);
  466. if (pciaddr == 0xffffffff) {
  467. return 0xffff;
  468. }
  469. /* set the pci address in s->config_reg */
  470. phb->config_reg = (pciaddr) | (1u << 31);
  471. /* clear PCI_STATUS_REC_MASTER_ABORT and PCI_STATUS_REC_TARGET_ABORT */
  472. status = pci_get_word(d->config + PCI_STATUS);
  473. status &= ~(PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT);
  474. pci_set_word(d->config + PCI_STATUS, status);
  475. return pci_data_read(phb->bus, phb->config_reg, 2);
  476. }
  477. static uint32_t bonito_spciconf_readl(void *opaque, hwaddr addr)
  478. {
  479. PCIBonitoState *s = opaque;
  480. PCIDevice *d = PCI_DEVICE(s);
  481. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  482. uint32_t pciaddr;
  483. uint16_t status;
  484. DPRINTF("bonito_spciconf_readl "TARGET_FMT_plx"\n", addr);
  485. assert((addr & 0x3) == 0);
  486. pciaddr = bonito_sbridge_pciaddr(s, addr);
  487. if (pciaddr == 0xffffffff) {
  488. return 0xffffffff;
  489. }
  490. /* set the pci address in s->config_reg */
  491. phb->config_reg = (pciaddr) | (1u << 31);
  492. /* clear PCI_STATUS_REC_MASTER_ABORT and PCI_STATUS_REC_TARGET_ABORT */
  493. status = pci_get_word(d->config + PCI_STATUS);
  494. status &= ~(PCI_STATUS_REC_MASTER_ABORT | PCI_STATUS_REC_TARGET_ABORT);
  495. pci_set_word(d->config + PCI_STATUS, status);
  496. return pci_data_read(phb->bus, phb->config_reg, 4);
  497. }
  498. /* south bridge PCI configure space. 0x1fe8 0000 - 0x1fef ffff */
  499. static const MemoryRegionOps bonito_spciconf_ops = {
  500. .old_mmio = {
  501. .read = {
  502. bonito_spciconf_readb,
  503. bonito_spciconf_readw,
  504. bonito_spciconf_readl,
  505. },
  506. .write = {
  507. bonito_spciconf_writeb,
  508. bonito_spciconf_writew,
  509. bonito_spciconf_writel,
  510. },
  511. },
  512. .endianness = DEVICE_NATIVE_ENDIAN,
  513. };
  514. #define BONITO_IRQ_BASE 32
  515. static void pci_bonito_set_irq(void *opaque, int irq_num, int level)
  516. {
  517. BonitoState *s = opaque;
  518. qemu_irq *pic = s->pic;
  519. PCIBonitoState *bonito_state = s->pci_dev;
  520. int internal_irq = irq_num - BONITO_IRQ_BASE;
  521. if (bonito_state->regs[BONITO_INTEDGE] & (1 << internal_irq)) {
  522. qemu_irq_pulse(*pic);
  523. } else { /* level triggered */
  524. if (bonito_state->regs[BONITO_INTPOL] & (1 << internal_irq)) {
  525. qemu_irq_raise(*pic);
  526. } else {
  527. qemu_irq_lower(*pic);
  528. }
  529. }
  530. }
  531. /* map the original irq (0~3) to bonito irq (16~47, but 16~31 are unused) */
  532. static int pci_bonito_map_irq(PCIDevice * pci_dev, int irq_num)
  533. {
  534. int slot;
  535. slot = (pci_dev->devfn >> 3);
  536. switch (slot) {
  537. case 5: /* FULONG2E_VIA_SLOT, SouthBridge, IDE, USB, ACPI, AC97, MC97 */
  538. return irq_num % 4 + BONITO_IRQ_BASE;
  539. case 6: /* FULONG2E_ATI_SLOT, VGA */
  540. return 4 + BONITO_IRQ_BASE;
  541. case 7: /* FULONG2E_RTL_SLOT, RTL8139 */
  542. return 5 + BONITO_IRQ_BASE;
  543. case 8 ... 12: /* PCI slot 1 to 4 */
  544. return (slot - 8 + irq_num) + 6 + BONITO_IRQ_BASE;
  545. default: /* Unknown device, don't do any translation */
  546. return irq_num;
  547. }
  548. }
  549. static void bonito_reset(void *opaque)
  550. {
  551. PCIBonitoState *s = opaque;
  552. /* set the default value of north bridge registers */
  553. s->regs[BONITO_BONPONCFG] = 0xc40;
  554. s->regs[BONITO_BONGENCFG] = 0x1384;
  555. s->regs[BONITO_IODEVCFG] = 0x2bff8010;
  556. s->regs[BONITO_SDCFG] = 0x255e0091;
  557. s->regs[BONITO_GPIODATA] = 0x1ff;
  558. s->regs[BONITO_GPIOIE] = 0x1ff;
  559. s->regs[BONITO_DQCFG] = 0x8;
  560. s->regs[BONITO_MEMSIZE] = 0x10000000;
  561. s->regs[BONITO_PCIMAP] = 0x6140;
  562. }
  563. static const VMStateDescription vmstate_bonito = {
  564. .name = "Bonito",
  565. .version_id = 1,
  566. .minimum_version_id = 1,
  567. .fields = (VMStateField[]) {
  568. VMSTATE_PCI_DEVICE(dev, PCIBonitoState),
  569. VMSTATE_END_OF_LIST()
  570. }
  571. };
  572. static int bonito_pcihost_initfn(SysBusDevice *dev)
  573. {
  574. PCIHostState *phb = PCI_HOST_BRIDGE(dev);
  575. phb->bus = pci_register_bus(DEVICE(dev), "pci",
  576. pci_bonito_set_irq, pci_bonito_map_irq, dev,
  577. get_system_memory(), get_system_io(),
  578. 0x28, 32, TYPE_PCI_BUS);
  579. return 0;
  580. }
  581. static int bonito_initfn(PCIDevice *dev)
  582. {
  583. PCIBonitoState *s = DO_UPCAST(PCIBonitoState, dev, dev);
  584. SysBusDevice *sysbus = SYS_BUS_DEVICE(s->pcihost);
  585. PCIHostState *phb = PCI_HOST_BRIDGE(s->pcihost);
  586. /* Bonito North Bridge, built on FPGA, VENDOR_ID/DEVICE_ID are "undefined" */
  587. pci_config_set_prog_interface(dev->config, 0x00);
  588. /* set the north bridge register mapping */
  589. memory_region_init_io(&s->iomem, OBJECT(s), &bonito_ops, s,
  590. "north-bridge-register", BONITO_INTERNAL_REG_SIZE);
  591. sysbus_init_mmio(sysbus, &s->iomem);
  592. sysbus_mmio_map(sysbus, 0, BONITO_INTERNAL_REG_BASE);
  593. /* set the north bridge pci configure mapping */
  594. memory_region_init_io(&phb->conf_mem, OBJECT(s), &bonito_pciconf_ops, s,
  595. "north-bridge-pci-config", BONITO_PCICONFIG_SIZE);
  596. sysbus_init_mmio(sysbus, &phb->conf_mem);
  597. sysbus_mmio_map(sysbus, 1, BONITO_PCICONFIG_BASE);
  598. /* set the south bridge pci configure mapping */
  599. memory_region_init_io(&phb->data_mem, OBJECT(s), &bonito_spciconf_ops, s,
  600. "south-bridge-pci-config", BONITO_SPCICONFIG_SIZE);
  601. sysbus_init_mmio(sysbus, &phb->data_mem);
  602. sysbus_mmio_map(sysbus, 2, BONITO_SPCICONFIG_BASE);
  603. memory_region_init_io(&s->iomem_ldma, OBJECT(s), &bonito_ldma_ops, s,
  604. "ldma", 0x100);
  605. sysbus_init_mmio(sysbus, &s->iomem_ldma);
  606. sysbus_mmio_map(sysbus, 3, 0xbfe00200);
  607. memory_region_init_io(&s->iomem_cop, OBJECT(s), &bonito_cop_ops, s,
  608. "cop", 0x100);
  609. sysbus_init_mmio(sysbus, &s->iomem_cop);
  610. sysbus_mmio_map(sysbus, 4, 0xbfe00300);
  611. /* Map PCI IO Space 0x1fd0 0000 - 0x1fd1 0000 */
  612. memory_region_init_alias(&s->bonito_pciio, OBJECT(s), "isa_mmio",
  613. get_system_io(), 0, BONITO_PCIIO_SIZE);
  614. sysbus_init_mmio(sysbus, &s->bonito_pciio);
  615. sysbus_mmio_map(sysbus, 5, BONITO_PCIIO_BASE);
  616. /* add pci local io mapping */
  617. memory_region_init_alias(&s->bonito_localio, OBJECT(s), "isa_mmio",
  618. get_system_io(), 0, BONITO_DEV_SIZE);
  619. sysbus_init_mmio(sysbus, &s->bonito_localio);
  620. sysbus_mmio_map(sysbus, 6, BONITO_DEV_BASE);
  621. /* set the default value of north bridge pci config */
  622. pci_set_word(dev->config + PCI_COMMAND, 0x0000);
  623. pci_set_word(dev->config + PCI_STATUS, 0x0000);
  624. pci_set_word(dev->config + PCI_SUBSYSTEM_VENDOR_ID, 0x0000);
  625. pci_set_word(dev->config + PCI_SUBSYSTEM_ID, 0x0000);
  626. pci_set_byte(dev->config + PCI_INTERRUPT_LINE, 0x00);
  627. pci_set_byte(dev->config + PCI_INTERRUPT_PIN, 0x01);
  628. pci_set_byte(dev->config + PCI_MIN_GNT, 0x3c);
  629. pci_set_byte(dev->config + PCI_MAX_LAT, 0x00);
  630. qemu_register_reset(bonito_reset, s);
  631. return 0;
  632. }
  633. PCIBus *bonito_init(qemu_irq *pic)
  634. {
  635. DeviceState *dev;
  636. BonitoState *pcihost;
  637. PCIHostState *phb;
  638. PCIBonitoState *s;
  639. PCIDevice *d;
  640. dev = qdev_create(NULL, TYPE_BONITO_PCI_HOST_BRIDGE);
  641. phb = PCI_HOST_BRIDGE(dev);
  642. pcihost = BONITO_PCI_HOST_BRIDGE(dev);
  643. pcihost->pic = pic;
  644. qdev_init_nofail(dev);
  645. /* set the pcihost pointer before bonito_initfn is called */
  646. d = pci_create(phb->bus, PCI_DEVFN(0, 0), "Bonito");
  647. s = DO_UPCAST(PCIBonitoState, dev, d);
  648. s->pcihost = pcihost;
  649. pcihost->pci_dev = s;
  650. qdev_init_nofail(DEVICE(d));
  651. return phb->bus;
  652. }
  653. static void bonito_class_init(ObjectClass *klass, void *data)
  654. {
  655. DeviceClass *dc = DEVICE_CLASS(klass);
  656. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  657. k->init = bonito_initfn;
  658. k->vendor_id = 0xdf53;
  659. k->device_id = 0x00d5;
  660. k->revision = 0x01;
  661. k->class_id = PCI_CLASS_BRIDGE_HOST;
  662. dc->desc = "Host bridge";
  663. dc->vmsd = &vmstate_bonito;
  664. /*
  665. * PCI-facing part of the host bridge, not usable without the
  666. * host-facing part, which can't be device_add'ed, yet.
  667. */
  668. dc->cannot_instantiate_with_device_add_yet = true;
  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. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  679. k->init = bonito_pcihost_initfn;
  680. }
  681. static const TypeInfo bonito_pcihost_info = {
  682. .name = TYPE_BONITO_PCI_HOST_BRIDGE,
  683. .parent = TYPE_PCI_HOST_BRIDGE,
  684. .instance_size = sizeof(BonitoState),
  685. .class_init = bonito_pcihost_class_init,
  686. };
  687. static void bonito_register_types(void)
  688. {
  689. type_register_static(&bonito_pcihost_info);
  690. type_register_static(&bonito_info);
  691. }
  692. type_init(bonito_register_types)