bonito.c 25 KB

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