bonito.c 23 KB

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