bonito.c 25 KB

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