vt82c686.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * VT82C686B south bridge support
  3. *
  4. * Copyright (c) 2008 yajin (yajin@vm-kernel.org)
  5. * Copyright (c) 2009 chenming (chenming@rdc.faw.com.cn)
  6. * Copyright (c) 2010 Huacai Chen (zltjiangshi@gmail.com)
  7. * This code is licensed under the GNU GPL v2.
  8. */
  9. #include "hw.h"
  10. #include "pc.h"
  11. #include "vt82c686.h"
  12. #include "i2c.h"
  13. #include "smbus.h"
  14. #include "pci.h"
  15. #include "isa.h"
  16. #include "sysbus.h"
  17. #include "mips.h"
  18. #include "apm.h"
  19. #include "acpi.h"
  20. #include "pm_smbus.h"
  21. #include "sysemu.h"
  22. #include "qemu-timer.h"
  23. typedef uint32_t pci_addr_t;
  24. #include "pci_host.h"
  25. //#define DEBUG_VT82C686B
  26. #ifdef DEBUG_VT82C686B
  27. #define DPRINTF(fmt, ...) fprintf(stderr, "%s: " fmt, __FUNCTION__, ##__VA_ARGS__)
  28. #else
  29. #define DPRINTF(fmt, ...)
  30. #endif
  31. typedef struct SuperIOConfig
  32. {
  33. uint8_t config[0xff];
  34. uint8_t index;
  35. uint8_t data;
  36. } SuperIOConfig;
  37. typedef struct VT82C686BState {
  38. PCIDevice dev;
  39. SuperIOConfig superio_conf;
  40. } VT82C686BState;
  41. static void superio_ioport_writeb(void *opaque, uint32_t addr, uint32_t data)
  42. {
  43. int can_write;
  44. SuperIOConfig *superio_conf = opaque;
  45. DPRINTF("superio_ioport_writeb address 0x%x val 0x%x\n", addr, data);
  46. if (addr == 0x3f0) {
  47. superio_conf->index = data & 0xff;
  48. } else {
  49. /* 0x3f1 */
  50. switch (superio_conf->index) {
  51. case 0x00 ... 0xdf:
  52. case 0xe4:
  53. case 0xe5:
  54. case 0xe9 ... 0xed:
  55. case 0xf3:
  56. case 0xf5:
  57. case 0xf7:
  58. case 0xf9 ... 0xfb:
  59. case 0xfd ... 0xff:
  60. can_write = 0;
  61. break;
  62. default:
  63. can_write = 1;
  64. if (can_write) {
  65. switch (superio_conf->index) {
  66. case 0xe7:
  67. if ((data & 0xff) != 0xfe) {
  68. DPRINTF("chage uart 1 base. unsupported yet\n");
  69. }
  70. break;
  71. case 0xe8:
  72. if ((data & 0xff) != 0xbe) {
  73. DPRINTF("chage uart 2 base. unsupported yet\n");
  74. }
  75. break;
  76. default:
  77. superio_conf->config[superio_conf->index] = data & 0xff;
  78. }
  79. }
  80. }
  81. superio_conf->config[superio_conf->index] = data & 0xff;
  82. }
  83. }
  84. static uint32_t superio_ioport_readb(void *opaque, uint32_t addr)
  85. {
  86. SuperIOConfig *superio_conf = opaque;
  87. DPRINTF("superio_ioport_readb address 0x%x\n", addr);
  88. return (superio_conf->config[superio_conf->index]);
  89. }
  90. static void vt82c686b_reset(void * opaque)
  91. {
  92. PCIDevice *d = opaque;
  93. uint8_t *pci_conf = d->config;
  94. VT82C686BState *vt82c = DO_UPCAST(VT82C686BState, dev, d);
  95. pci_set_long(pci_conf + PCI_CAPABILITY_LIST, 0x000000c0);
  96. pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  97. PCI_COMMAND_MASTER | PCI_COMMAND_SPECIAL);
  98. pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM);
  99. pci_conf[0x48] = 0x01; /* Miscellaneous Control 3 */
  100. pci_conf[0x4a] = 0x04; /* IDE interrupt Routing */
  101. pci_conf[0x4f] = 0x03; /* DMA/Master Mem Access Control 3 */
  102. pci_conf[0x50] = 0x2d; /* PnP DMA Request Control */
  103. pci_conf[0x59] = 0x04;
  104. pci_conf[0x5a] = 0x04; /* KBC/RTC Control*/
  105. pci_conf[0x5f] = 0x04;
  106. pci_conf[0x77] = 0x10; /* GPIO Control 1/2/3/4 */
  107. vt82c->superio_conf.config[0xe0] = 0x3c;
  108. vt82c->superio_conf.config[0xe2] = 0x03;
  109. vt82c->superio_conf.config[0xe3] = 0xfc;
  110. vt82c->superio_conf.config[0xe6] = 0xde;
  111. vt82c->superio_conf.config[0xe7] = 0xfe;
  112. vt82c->superio_conf.config[0xe8] = 0xbe;
  113. }
  114. /* write config pci function0 registers. PCI-ISA bridge */
  115. static void vt82c686b_write_config(PCIDevice * d, uint32_t address,
  116. uint32_t val, int len)
  117. {
  118. VT82C686BState *vt686 = DO_UPCAST(VT82C686BState, dev, d);
  119. DPRINTF("vt82c686b_write_config address 0x%x val 0x%x len 0x%x\n",
  120. address, val, len);
  121. pci_default_write_config(d, address, val, len);
  122. if (address == 0x85) { /* enable or disable super IO configure */
  123. if (val & 0x2) {
  124. /* floppy also uses 0x3f0 and 0x3f1.
  125. * But we do not emulate flopy,so just set it here. */
  126. isa_unassign_ioport(0x3f0, 2);
  127. register_ioport_read(0x3f0, 2, 1, superio_ioport_readb,
  128. &vt686->superio_conf);
  129. register_ioport_write(0x3f0, 2, 1, superio_ioport_writeb,
  130. &vt686->superio_conf);
  131. } else {
  132. isa_unassign_ioport(0x3f0, 2);
  133. }
  134. }
  135. }
  136. #define ACPI_DBG_IO_ADDR 0xb044
  137. typedef struct VT686PMState {
  138. PCIDevice dev;
  139. ACPIPM1EVT pm1a;
  140. ACPIPM1CNT pm1_cnt;
  141. APMState apm;
  142. ACPIPMTimer tmr;
  143. PMSMBus smb;
  144. uint32_t smb_io_base;
  145. } VT686PMState;
  146. typedef struct VT686AC97State {
  147. PCIDevice dev;
  148. } VT686AC97State;
  149. typedef struct VT686MC97State {
  150. PCIDevice dev;
  151. } VT686MC97State;
  152. static void pm_update_sci(VT686PMState *s)
  153. {
  154. int sci_level, pmsts;
  155. pmsts = acpi_pm1_evt_get_sts(&s->pm1a, s->tmr.overflow_time);
  156. sci_level = (((pmsts & s->pm1a.en) &
  157. (ACPI_BITMASK_RT_CLOCK_ENABLE |
  158. ACPI_BITMASK_POWER_BUTTON_ENABLE |
  159. ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
  160. ACPI_BITMASK_TIMER_ENABLE)) != 0);
  161. qemu_set_irq(s->dev.irq[0], sci_level);
  162. /* schedule a timer interruption if needed */
  163. acpi_pm_tmr_update(&s->tmr, (s->pm1a.en & ACPI_BITMASK_TIMER_ENABLE) &&
  164. !(pmsts & ACPI_BITMASK_TIMER_STATUS));
  165. }
  166. static void pm_tmr_timer(ACPIPMTimer *tmr)
  167. {
  168. VT686PMState *s = container_of(tmr, VT686PMState, tmr);
  169. pm_update_sci(s);
  170. }
  171. static void pm_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
  172. {
  173. VT686PMState *s = opaque;
  174. addr &= 0x0f;
  175. switch (addr) {
  176. case 0x00:
  177. acpi_pm1_evt_write_sts(&s->pm1a, &s->tmr, val);
  178. pm_update_sci(s);
  179. break;
  180. case 0x02:
  181. s->pm1a.en = val;
  182. pm_update_sci(s);
  183. break;
  184. case 0x04:
  185. acpi_pm1_cnt_write(&s->pm1a, &s->pm1_cnt, val);
  186. break;
  187. default:
  188. break;
  189. }
  190. DPRINTF("PM writew port=0x%04x val=0x%02x\n", addr, val);
  191. }
  192. static uint32_t pm_ioport_readw(void *opaque, uint32_t addr)
  193. {
  194. VT686PMState *s = opaque;
  195. uint32_t val;
  196. addr &= 0x0f;
  197. switch (addr) {
  198. case 0x00:
  199. val = acpi_pm1_evt_get_sts(&s->pm1a, s->tmr.overflow_time);
  200. break;
  201. case 0x02:
  202. val = s->pm1a.en;
  203. break;
  204. case 0x04:
  205. val = s->pm1_cnt.cnt;
  206. break;
  207. default:
  208. val = 0;
  209. break;
  210. }
  211. DPRINTF("PM readw port=0x%04x val=0x%02x\n", addr, val);
  212. return val;
  213. }
  214. static void pm_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
  215. {
  216. addr &= 0x0f;
  217. DPRINTF("PM writel port=0x%04x val=0x%08x\n", addr, val);
  218. }
  219. static uint32_t pm_ioport_readl(void *opaque, uint32_t addr)
  220. {
  221. VT686PMState *s = opaque;
  222. uint32_t val;
  223. addr &= 0x0f;
  224. switch (addr) {
  225. case 0x08:
  226. val = acpi_pm_tmr_get(&s->tmr);
  227. break;
  228. default:
  229. val = 0;
  230. break;
  231. }
  232. DPRINTF("PM readl port=0x%04x val=0x%08x\n", addr, val);
  233. return val;
  234. }
  235. static void pm_io_space_update(VT686PMState *s)
  236. {
  237. uint32_t pm_io_base;
  238. if (s->dev.config[0x80] & 1) {
  239. pm_io_base = pci_get_long(s->dev.config + 0x40);
  240. pm_io_base &= 0xffc0;
  241. /* XXX: need to improve memory and ioport allocation */
  242. DPRINTF("PM: mapping to 0x%x\n", pm_io_base);
  243. register_ioport_write(pm_io_base, 64, 2, pm_ioport_writew, s);
  244. register_ioport_read(pm_io_base, 64, 2, pm_ioport_readw, s);
  245. register_ioport_write(pm_io_base, 64, 4, pm_ioport_writel, s);
  246. register_ioport_read(pm_io_base, 64, 4, pm_ioport_readl, s);
  247. }
  248. }
  249. static void pm_write_config(PCIDevice *d,
  250. uint32_t address, uint32_t val, int len)
  251. {
  252. DPRINTF("pm_write_config address 0x%x val 0x%x len 0x%x\n",
  253. address, val, len);
  254. pci_default_write_config(d, address, val, len);
  255. }
  256. static int vmstate_acpi_post_load(void *opaque, int version_id)
  257. {
  258. VT686PMState *s = opaque;
  259. pm_io_space_update(s);
  260. return 0;
  261. }
  262. static const VMStateDescription vmstate_acpi = {
  263. .name = "vt82c686b_pm",
  264. .version_id = 1,
  265. .minimum_version_id = 1,
  266. .minimum_version_id_old = 1,
  267. .post_load = vmstate_acpi_post_load,
  268. .fields = (VMStateField []) {
  269. VMSTATE_PCI_DEVICE(dev, VT686PMState),
  270. VMSTATE_UINT16(pm1a.sts, VT686PMState),
  271. VMSTATE_UINT16(pm1a.en, VT686PMState),
  272. VMSTATE_UINT16(pm1_cnt.cnt, VT686PMState),
  273. VMSTATE_STRUCT(apm, VT686PMState, 0, vmstate_apm, APMState),
  274. VMSTATE_TIMER(tmr.timer, VT686PMState),
  275. VMSTATE_INT64(tmr.overflow_time, VT686PMState),
  276. VMSTATE_END_OF_LIST()
  277. }
  278. };
  279. /*
  280. * TODO: vt82c686b_ac97_init() and vt82c686b_mc97_init()
  281. * just register a PCI device now, functionalities will be implemented later.
  282. */
  283. static int vt82c686b_ac97_initfn(PCIDevice *dev)
  284. {
  285. VT686AC97State *s = DO_UPCAST(VT686AC97State, dev, dev);
  286. uint8_t *pci_conf = s->dev.config;
  287. pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_INVALIDATE |
  288. PCI_COMMAND_PARITY);
  289. pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_CAP_LIST |
  290. PCI_STATUS_DEVSEL_MEDIUM);
  291. pci_set_long(pci_conf + PCI_INTERRUPT_PIN, 0x03);
  292. return 0;
  293. }
  294. void vt82c686b_ac97_init(PCIBus *bus, int devfn)
  295. {
  296. PCIDevice *dev;
  297. dev = pci_create(bus, devfn, "VT82C686B_AC97");
  298. qdev_init_nofail(&dev->qdev);
  299. }
  300. static PCIDeviceInfo via_ac97_info = {
  301. .qdev.name = "VT82C686B_AC97",
  302. .qdev.desc = "AC97",
  303. .qdev.size = sizeof(VT686AC97State),
  304. .init = vt82c686b_ac97_initfn,
  305. .vendor_id = PCI_VENDOR_ID_VIA,
  306. .device_id = PCI_DEVICE_ID_VIA_AC97,
  307. .revision = 0x50,
  308. .class_id = PCI_CLASS_MULTIMEDIA_AUDIO,
  309. };
  310. static void vt82c686b_ac97_register(void)
  311. {
  312. pci_qdev_register(&via_ac97_info);
  313. }
  314. device_init(vt82c686b_ac97_register);
  315. static int vt82c686b_mc97_initfn(PCIDevice *dev)
  316. {
  317. VT686MC97State *s = DO_UPCAST(VT686MC97State, dev, dev);
  318. uint8_t *pci_conf = s->dev.config;
  319. pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_INVALIDATE |
  320. PCI_COMMAND_VGA_PALETTE);
  321. pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM);
  322. pci_set_long(pci_conf + PCI_INTERRUPT_PIN, 0x03);
  323. return 0;
  324. }
  325. void vt82c686b_mc97_init(PCIBus *bus, int devfn)
  326. {
  327. PCIDevice *dev;
  328. dev = pci_create(bus, devfn, "VT82C686B_MC97");
  329. qdev_init_nofail(&dev->qdev);
  330. }
  331. static PCIDeviceInfo via_mc97_info = {
  332. .qdev.name = "VT82C686B_MC97",
  333. .qdev.desc = "MC97",
  334. .qdev.size = sizeof(VT686MC97State),
  335. .init = vt82c686b_mc97_initfn,
  336. .vendor_id = PCI_VENDOR_ID_VIA,
  337. .device_id = PCI_DEVICE_ID_VIA_MC97,
  338. .class_id = PCI_CLASS_COMMUNICATION_OTHER,
  339. .revision = 0x30,
  340. };
  341. static void vt82c686b_mc97_register(void)
  342. {
  343. pci_qdev_register(&via_mc97_info);
  344. }
  345. device_init(vt82c686b_mc97_register);
  346. /* vt82c686 pm init */
  347. static int vt82c686b_pm_initfn(PCIDevice *dev)
  348. {
  349. VT686PMState *s = DO_UPCAST(VT686PMState, dev, dev);
  350. uint8_t *pci_conf;
  351. pci_conf = s->dev.config;
  352. pci_set_word(pci_conf + PCI_COMMAND, 0);
  353. pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_FAST_BACK |
  354. PCI_STATUS_DEVSEL_MEDIUM);
  355. /* 0x48-0x4B is Power Management I/O Base */
  356. pci_set_long(pci_conf + 0x48, 0x00000001);
  357. /* SMB ports:0xeee0~0xeeef */
  358. s->smb_io_base =((s->smb_io_base & 0xfff0) + 0x0);
  359. pci_conf[0x90] = s->smb_io_base | 1;
  360. pci_conf[0x91] = s->smb_io_base >> 8;
  361. pci_conf[0xd2] = 0x90;
  362. register_ioport_write(s->smb_io_base, 0xf, 1, smb_ioport_writeb, &s->smb);
  363. register_ioport_read(s->smb_io_base, 0xf, 1, smb_ioport_readb, &s->smb);
  364. apm_init(&s->apm, NULL, s);
  365. acpi_pm_tmr_init(&s->tmr, pm_tmr_timer);
  366. acpi_pm1_cnt_init(&s->pm1_cnt, NULL);
  367. pm_smbus_init(&s->dev.qdev, &s->smb);
  368. return 0;
  369. }
  370. i2c_bus *vt82c686b_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
  371. qemu_irq sci_irq)
  372. {
  373. PCIDevice *dev;
  374. VT686PMState *s;
  375. dev = pci_create(bus, devfn, "VT82C686B_PM");
  376. qdev_prop_set_uint32(&dev->qdev, "smb_io_base", smb_io_base);
  377. s = DO_UPCAST(VT686PMState, dev, dev);
  378. qdev_init_nofail(&dev->qdev);
  379. return s->smb.smbus;
  380. }
  381. static PCIDeviceInfo via_pm_info = {
  382. .qdev.name = "VT82C686B_PM",
  383. .qdev.desc = "PM",
  384. .qdev.size = sizeof(VT686PMState),
  385. .qdev.vmsd = &vmstate_acpi,
  386. .init = vt82c686b_pm_initfn,
  387. .config_write = pm_write_config,
  388. .vendor_id = PCI_VENDOR_ID_VIA,
  389. .device_id = PCI_DEVICE_ID_VIA_ACPI,
  390. .class_id = PCI_CLASS_BRIDGE_OTHER,
  391. .revision = 0x40,
  392. .qdev.props = (Property[]) {
  393. DEFINE_PROP_UINT32("smb_io_base", VT686PMState, smb_io_base, 0),
  394. DEFINE_PROP_END_OF_LIST(),
  395. }
  396. };
  397. static void vt82c686b_pm_register(void)
  398. {
  399. pci_qdev_register(&via_pm_info);
  400. }
  401. device_init(vt82c686b_pm_register);
  402. static const VMStateDescription vmstate_via = {
  403. .name = "vt82c686b",
  404. .version_id = 1,
  405. .minimum_version_id = 1,
  406. .minimum_version_id_old = 1,
  407. .fields = (VMStateField []) {
  408. VMSTATE_PCI_DEVICE(dev, VT82C686BState),
  409. VMSTATE_END_OF_LIST()
  410. }
  411. };
  412. /* init the PCI-to-ISA bridge */
  413. static int vt82c686b_initfn(PCIDevice *d)
  414. {
  415. uint8_t *pci_conf;
  416. uint8_t *wmask;
  417. int i;
  418. isa_bus_new(&d->qdev, pci_address_space_io(d));
  419. pci_conf = d->config;
  420. pci_config_set_prog_interface(pci_conf, 0x0);
  421. wmask = d->wmask;
  422. for (i = 0x00; i < 0xff; i++) {
  423. if (i<=0x03 || (i>=0x08 && i<=0x3f)) {
  424. wmask[i] = 0x00;
  425. }
  426. }
  427. qemu_register_reset(vt82c686b_reset, d);
  428. return 0;
  429. }
  430. int vt82c686b_init(PCIBus *bus, int devfn)
  431. {
  432. PCIDevice *d;
  433. d = pci_create_simple_multifunction(bus, devfn, true, "VT82C686B");
  434. return d->devfn;
  435. }
  436. static PCIDeviceInfo via_info = {
  437. .qdev.name = "VT82C686B",
  438. .qdev.desc = "ISA bridge",
  439. .qdev.size = sizeof(VT82C686BState),
  440. .qdev.vmsd = &vmstate_via,
  441. .qdev.no_user = 1,
  442. .init = vt82c686b_initfn,
  443. .config_write = vt82c686b_write_config,
  444. .vendor_id = PCI_VENDOR_ID_VIA,
  445. .device_id = PCI_DEVICE_ID_VIA_ISA_BRIDGE,
  446. .class_id = PCI_CLASS_BRIDGE_ISA,
  447. .revision = 0x40,
  448. };
  449. static void vt82c686b_register(void)
  450. {
  451. pci_qdev_register(&via_info);
  452. }
  453. device_init(vt82c686b_register);