vt82c686.c 14 KB

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