vt82c686.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 "qemu/osdep.h"
  13. #include "hw/isa/vt82c686.h"
  14. #include "hw/i2c/i2c.h"
  15. #include "hw/pci/pci.h"
  16. #include "hw/qdev-properties.h"
  17. #include "hw/isa/isa.h"
  18. #include "hw/isa/superio.h"
  19. #include "hw/sysbus.h"
  20. #include "migration/vmstate.h"
  21. #include "hw/mips/mips.h"
  22. #include "hw/isa/apm.h"
  23. #include "hw/acpi/acpi.h"
  24. #include "hw/i2c/pm_smbus.h"
  25. #include "qemu/module.h"
  26. #include "qemu/timer.h"
  27. #include "exec/address-spaces.h"
  28. //#define DEBUG_VT82C686B
  29. #ifdef DEBUG_VT82C686B
  30. #define DPRINTF(fmt, ...) fprintf(stderr, "%s: " fmt, __func__, ##__VA_ARGS__)
  31. #else
  32. #define DPRINTF(fmt, ...)
  33. #endif
  34. typedef struct SuperIOConfig
  35. {
  36. uint8_t config[0x100];
  37. uint8_t index;
  38. uint8_t data;
  39. } SuperIOConfig;
  40. typedef struct VT82C686BState {
  41. PCIDevice dev;
  42. MemoryRegion superio;
  43. SuperIOConfig superio_conf;
  44. } VT82C686BState;
  45. #define TYPE_VT82C686B_DEVICE "VT82C686B"
  46. #define VT82C686B_DEVICE(obj) \
  47. OBJECT_CHECK(VT82C686BState, (obj), TYPE_VT82C686B_DEVICE)
  48. static void superio_ioport_writeb(void *opaque, hwaddr addr, uint64_t data,
  49. unsigned size)
  50. {
  51. SuperIOConfig *superio_conf = opaque;
  52. DPRINTF("superio_ioport_writeb address 0x%x val 0x%x\n", addr, data);
  53. if (addr == 0x3f0) {
  54. superio_conf->index = data & 0xff;
  55. } else {
  56. bool can_write = true;
  57. /* 0x3f1 */
  58. switch (superio_conf->index) {
  59. case 0x00 ... 0xdf:
  60. case 0xe4:
  61. case 0xe5:
  62. case 0xe9 ... 0xed:
  63. case 0xf3:
  64. case 0xf5:
  65. case 0xf7:
  66. case 0xf9 ... 0xfb:
  67. case 0xfd ... 0xff:
  68. can_write = false;
  69. break;
  70. case 0xe7:
  71. if ((data & 0xff) != 0xfe) {
  72. DPRINTF("change uart 1 base. unsupported yet\n");
  73. can_write = false;
  74. }
  75. break;
  76. case 0xe8:
  77. if ((data & 0xff) != 0xbe) {
  78. DPRINTF("change uart 2 base. unsupported yet\n");
  79. can_write = false;
  80. }
  81. break;
  82. default:
  83. break;
  84. }
  85. if (can_write) {
  86. superio_conf->config[superio_conf->index] = data & 0xff;
  87. }
  88. }
  89. }
  90. static uint64_t superio_ioport_readb(void *opaque, hwaddr addr, unsigned size)
  91. {
  92. SuperIOConfig *superio_conf = opaque;
  93. DPRINTF("superio_ioport_readb address 0x%x\n", addr);
  94. return (superio_conf->config[superio_conf->index]);
  95. }
  96. static const MemoryRegionOps superio_ops = {
  97. .read = superio_ioport_readb,
  98. .write = superio_ioport_writeb,
  99. .endianness = DEVICE_NATIVE_ENDIAN,
  100. .impl = {
  101. .min_access_size = 1,
  102. .max_access_size = 1,
  103. },
  104. };
  105. static void vt82c686b_isa_reset(DeviceState *dev)
  106. {
  107. VT82C686BState *vt82c = VT82C686B_DEVICE(dev);
  108. uint8_t *pci_conf = vt82c->dev.config;
  109. pci_set_long(pci_conf + PCI_CAPABILITY_LIST, 0x000000c0);
  110. pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  111. PCI_COMMAND_MASTER | PCI_COMMAND_SPECIAL);
  112. pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM);
  113. pci_conf[0x48] = 0x01; /* Miscellaneous Control 3 */
  114. pci_conf[0x4a] = 0x04; /* IDE interrupt Routing */
  115. pci_conf[0x4f] = 0x03; /* DMA/Master Mem Access Control 3 */
  116. pci_conf[0x50] = 0x2d; /* PnP DMA Request Control */
  117. pci_conf[0x59] = 0x04;
  118. pci_conf[0x5a] = 0x04; /* KBC/RTC Control*/
  119. pci_conf[0x5f] = 0x04;
  120. pci_conf[0x77] = 0x10; /* GPIO Control 1/2/3/4 */
  121. vt82c->superio_conf.config[0xe0] = 0x3c;
  122. vt82c->superio_conf.config[0xe2] = 0x03;
  123. vt82c->superio_conf.config[0xe3] = 0xfc;
  124. vt82c->superio_conf.config[0xe6] = 0xde;
  125. vt82c->superio_conf.config[0xe7] = 0xfe;
  126. vt82c->superio_conf.config[0xe8] = 0xbe;
  127. }
  128. /* write config pci function0 registers. PCI-ISA bridge */
  129. static void vt82c686b_write_config(PCIDevice * d, uint32_t address,
  130. uint32_t val, int len)
  131. {
  132. VT82C686BState *vt686 = VT82C686B_DEVICE(d);
  133. DPRINTF("vt82c686b_write_config address 0x%x val 0x%x len 0x%x\n",
  134. address, val, len);
  135. pci_default_write_config(d, address, val, len);
  136. if (address == 0x85) { /* enable or disable super IO configure */
  137. memory_region_set_enabled(&vt686->superio, val & 0x2);
  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. #define TYPE_VT82C686B_PM_DEVICE "VT82C686B_PM"
  156. #define VT82C686B_PM_DEVICE(obj) \
  157. OBJECT_CHECK(VT686PMState, (obj), TYPE_VT82C686B_PM_DEVICE)
  158. #define TYPE_VT82C686B_MC97_DEVICE "VT82C686B_MC97"
  159. #define VT82C686B_MC97_DEVICE(obj) \
  160. OBJECT_CHECK(VT686MC97State, (obj), TYPE_VT82C686B_MC97_DEVICE)
  161. #define TYPE_VT82C686B_AC97_DEVICE "VT82C686B_AC97"
  162. #define VT82C686B_AC97_DEVICE(obj) \
  163. OBJECT_CHECK(VT686AC97State, (obj), TYPE_VT82C686B_AC97_DEVICE)
  164. static void pm_update_sci(VT686PMState *s)
  165. {
  166. int sci_level, pmsts;
  167. pmsts = acpi_pm1_evt_get_sts(&s->ar);
  168. sci_level = (((pmsts & s->ar.pm1.evt.en) &
  169. (ACPI_BITMASK_RT_CLOCK_ENABLE |
  170. ACPI_BITMASK_POWER_BUTTON_ENABLE |
  171. ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
  172. ACPI_BITMASK_TIMER_ENABLE)) != 0);
  173. pci_set_irq(&s->dev, sci_level);
  174. /* schedule a timer interruption if needed */
  175. acpi_pm_tmr_update(&s->ar, (s->ar.pm1.evt.en & ACPI_BITMASK_TIMER_ENABLE) &&
  176. !(pmsts & ACPI_BITMASK_TIMER_STATUS));
  177. }
  178. static void pm_tmr_timer(ACPIREGS *ar)
  179. {
  180. VT686PMState *s = container_of(ar, VT686PMState, ar);
  181. pm_update_sci(s);
  182. }
  183. static void pm_io_space_update(VT686PMState *s)
  184. {
  185. uint32_t pm_io_base;
  186. pm_io_base = pci_get_long(s->dev.config + 0x40);
  187. pm_io_base &= 0xffc0;
  188. memory_region_transaction_begin();
  189. memory_region_set_enabled(&s->io, s->dev.config[0x80] & 1);
  190. memory_region_set_address(&s->io, pm_io_base);
  191. memory_region_transaction_commit();
  192. }
  193. static void pm_write_config(PCIDevice *d,
  194. uint32_t address, uint32_t val, int len)
  195. {
  196. DPRINTF("pm_write_config address 0x%x val 0x%x len 0x%x\n",
  197. address, val, len);
  198. pci_default_write_config(d, address, val, len);
  199. }
  200. static int vmstate_acpi_post_load(void *opaque, int version_id)
  201. {
  202. VT686PMState *s = opaque;
  203. pm_io_space_update(s);
  204. return 0;
  205. }
  206. static const VMStateDescription vmstate_acpi = {
  207. .name = "vt82c686b_pm",
  208. .version_id = 1,
  209. .minimum_version_id = 1,
  210. .post_load = vmstate_acpi_post_load,
  211. .fields = (VMStateField[]) {
  212. VMSTATE_PCI_DEVICE(dev, VT686PMState),
  213. VMSTATE_UINT16(ar.pm1.evt.sts, VT686PMState),
  214. VMSTATE_UINT16(ar.pm1.evt.en, VT686PMState),
  215. VMSTATE_UINT16(ar.pm1.cnt.cnt, VT686PMState),
  216. VMSTATE_STRUCT(apm, VT686PMState, 0, vmstate_apm, APMState),
  217. VMSTATE_TIMER_PTR(ar.tmr.timer, VT686PMState),
  218. VMSTATE_INT64(ar.tmr.overflow_time, VT686PMState),
  219. VMSTATE_END_OF_LIST()
  220. }
  221. };
  222. /*
  223. * TODO: vt82c686b_ac97_init() and vt82c686b_mc97_init()
  224. * just register a PCI device now, functionalities will be implemented later.
  225. */
  226. static void vt82c686b_ac97_realize(PCIDevice *dev, Error **errp)
  227. {
  228. VT686AC97State *s = VT82C686B_AC97_DEVICE(dev);
  229. uint8_t *pci_conf = s->dev.config;
  230. pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_INVALIDATE |
  231. PCI_COMMAND_PARITY);
  232. pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_CAP_LIST |
  233. PCI_STATUS_DEVSEL_MEDIUM);
  234. pci_set_long(pci_conf + PCI_INTERRUPT_PIN, 0x03);
  235. }
  236. void vt82c686b_ac97_init(PCIBus *bus, int devfn)
  237. {
  238. PCIDevice *dev;
  239. dev = pci_create(bus, devfn, TYPE_VT82C686B_AC97_DEVICE);
  240. qdev_init_nofail(&dev->qdev);
  241. }
  242. static void via_ac97_class_init(ObjectClass *klass, void *data)
  243. {
  244. DeviceClass *dc = DEVICE_CLASS(klass);
  245. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  246. k->realize = vt82c686b_ac97_realize;
  247. k->vendor_id = PCI_VENDOR_ID_VIA;
  248. k->device_id = PCI_DEVICE_ID_VIA_AC97;
  249. k->revision = 0x50;
  250. k->class_id = PCI_CLASS_MULTIMEDIA_AUDIO;
  251. set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
  252. dc->desc = "AC97";
  253. }
  254. static const TypeInfo via_ac97_info = {
  255. .name = TYPE_VT82C686B_AC97_DEVICE,
  256. .parent = TYPE_PCI_DEVICE,
  257. .instance_size = sizeof(VT686AC97State),
  258. .class_init = via_ac97_class_init,
  259. .interfaces = (InterfaceInfo[]) {
  260. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  261. { },
  262. },
  263. };
  264. static void vt82c686b_mc97_realize(PCIDevice *dev, Error **errp)
  265. {
  266. VT686MC97State *s = VT82C686B_MC97_DEVICE(dev);
  267. uint8_t *pci_conf = s->dev.config;
  268. pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_INVALIDATE |
  269. PCI_COMMAND_VGA_PALETTE);
  270. pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM);
  271. pci_set_long(pci_conf + PCI_INTERRUPT_PIN, 0x03);
  272. }
  273. void vt82c686b_mc97_init(PCIBus *bus, int devfn)
  274. {
  275. PCIDevice *dev;
  276. dev = pci_create(bus, devfn, TYPE_VT82C686B_MC97_DEVICE);
  277. qdev_init_nofail(&dev->qdev);
  278. }
  279. static void via_mc97_class_init(ObjectClass *klass, void *data)
  280. {
  281. DeviceClass *dc = DEVICE_CLASS(klass);
  282. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  283. k->realize = vt82c686b_mc97_realize;
  284. k->vendor_id = PCI_VENDOR_ID_VIA;
  285. k->device_id = PCI_DEVICE_ID_VIA_MC97;
  286. k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
  287. k->revision = 0x30;
  288. set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
  289. dc->desc = "MC97";
  290. }
  291. static const TypeInfo via_mc97_info = {
  292. .name = TYPE_VT82C686B_MC97_DEVICE,
  293. .parent = TYPE_PCI_DEVICE,
  294. .instance_size = sizeof(VT686MC97State),
  295. .class_init = via_mc97_class_init,
  296. .interfaces = (InterfaceInfo[]) {
  297. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  298. { },
  299. },
  300. };
  301. /* vt82c686 pm init */
  302. static void vt82c686b_pm_realize(PCIDevice *dev, Error **errp)
  303. {
  304. VT686PMState *s = VT82C686B_PM_DEVICE(dev);
  305. uint8_t *pci_conf;
  306. pci_conf = s->dev.config;
  307. pci_set_word(pci_conf + PCI_COMMAND, 0);
  308. pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_FAST_BACK |
  309. PCI_STATUS_DEVSEL_MEDIUM);
  310. /* 0x48-0x4B is Power Management I/O Base */
  311. pci_set_long(pci_conf + 0x48, 0x00000001);
  312. /* SMB ports:0xeee0~0xeeef */
  313. s->smb_io_base =((s->smb_io_base & 0xfff0) + 0x0);
  314. pci_conf[0x90] = s->smb_io_base | 1;
  315. pci_conf[0x91] = s->smb_io_base >> 8;
  316. pci_conf[0xd2] = 0x90;
  317. pm_smbus_init(DEVICE(s), &s->smb, false);
  318. memory_region_add_subregion(get_system_io(), s->smb_io_base, &s->smb.io);
  319. apm_init(dev, &s->apm, NULL, s);
  320. memory_region_init(&s->io, OBJECT(dev), "vt82c686-pm", 64);
  321. memory_region_set_enabled(&s->io, false);
  322. memory_region_add_subregion(get_system_io(), 0, &s->io);
  323. acpi_pm_tmr_init(&s->ar, pm_tmr_timer, &s->io);
  324. acpi_pm1_evt_init(&s->ar, pm_tmr_timer, &s->io);
  325. acpi_pm1_cnt_init(&s->ar, &s->io, false, false, 2);
  326. }
  327. I2CBus *vt82c686b_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
  328. qemu_irq sci_irq)
  329. {
  330. PCIDevice *dev;
  331. VT686PMState *s;
  332. dev = pci_create(bus, devfn, TYPE_VT82C686B_PM_DEVICE);
  333. qdev_prop_set_uint32(&dev->qdev, "smb_io_base", smb_io_base);
  334. s = VT82C686B_PM_DEVICE(dev);
  335. qdev_init_nofail(&dev->qdev);
  336. return s->smb.smbus;
  337. }
  338. static Property via_pm_properties[] = {
  339. DEFINE_PROP_UINT32("smb_io_base", VT686PMState, smb_io_base, 0),
  340. DEFINE_PROP_END_OF_LIST(),
  341. };
  342. static void via_pm_class_init(ObjectClass *klass, void *data)
  343. {
  344. DeviceClass *dc = DEVICE_CLASS(klass);
  345. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  346. k->realize = vt82c686b_pm_realize;
  347. k->config_write = pm_write_config;
  348. k->vendor_id = PCI_VENDOR_ID_VIA;
  349. k->device_id = PCI_DEVICE_ID_VIA_ACPI;
  350. k->class_id = PCI_CLASS_BRIDGE_OTHER;
  351. k->revision = 0x40;
  352. dc->desc = "PM";
  353. dc->vmsd = &vmstate_acpi;
  354. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  355. dc->props = via_pm_properties;
  356. }
  357. static const TypeInfo via_pm_info = {
  358. .name = TYPE_VT82C686B_PM_DEVICE,
  359. .parent = TYPE_PCI_DEVICE,
  360. .instance_size = sizeof(VT686PMState),
  361. .class_init = via_pm_class_init,
  362. .interfaces = (InterfaceInfo[]) {
  363. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  364. { },
  365. },
  366. };
  367. static const VMStateDescription vmstate_via = {
  368. .name = "vt82c686b",
  369. .version_id = 1,
  370. .minimum_version_id = 1,
  371. .fields = (VMStateField[]) {
  372. VMSTATE_PCI_DEVICE(dev, VT82C686BState),
  373. VMSTATE_END_OF_LIST()
  374. }
  375. };
  376. /* init the PCI-to-ISA bridge */
  377. static void vt82c686b_realize(PCIDevice *d, Error **errp)
  378. {
  379. VT82C686BState *vt82c = VT82C686B_DEVICE(d);
  380. uint8_t *pci_conf;
  381. ISABus *isa_bus;
  382. uint8_t *wmask;
  383. int i;
  384. isa_bus = isa_bus_new(DEVICE(d), get_system_memory(),
  385. pci_address_space_io(d), errp);
  386. if (!isa_bus) {
  387. return;
  388. }
  389. pci_conf = d->config;
  390. pci_config_set_prog_interface(pci_conf, 0x0);
  391. wmask = d->wmask;
  392. for (i = 0x00; i < 0xff; i++) {
  393. if (i<=0x03 || (i>=0x08 && i<=0x3f)) {
  394. wmask[i] = 0x00;
  395. }
  396. }
  397. memory_region_init_io(&vt82c->superio, OBJECT(d), &superio_ops,
  398. &vt82c->superio_conf, "superio", 2);
  399. memory_region_set_enabled(&vt82c->superio, false);
  400. /* The floppy also uses 0x3f0 and 0x3f1.
  401. * But we do not emulate a floppy, so just set it here. */
  402. memory_region_add_subregion(isa_bus->address_space_io, 0x3f0,
  403. &vt82c->superio);
  404. }
  405. ISABus *vt82c686b_isa_init(PCIBus *bus, int devfn)
  406. {
  407. PCIDevice *d;
  408. d = pci_create_simple_multifunction(bus, devfn, true,
  409. TYPE_VT82C686B_DEVICE);
  410. return ISA_BUS(qdev_get_child_bus(DEVICE(d), "isa.0"));
  411. }
  412. static void via_class_init(ObjectClass *klass, void *data)
  413. {
  414. DeviceClass *dc = DEVICE_CLASS(klass);
  415. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  416. k->realize = vt82c686b_realize;
  417. k->config_write = vt82c686b_write_config;
  418. k->vendor_id = PCI_VENDOR_ID_VIA;
  419. k->device_id = PCI_DEVICE_ID_VIA_ISA_BRIDGE;
  420. k->class_id = PCI_CLASS_BRIDGE_ISA;
  421. k->revision = 0x40;
  422. dc->reset = vt82c686b_isa_reset;
  423. dc->desc = "ISA bridge";
  424. dc->vmsd = &vmstate_via;
  425. /*
  426. * Reason: part of VIA VT82C686 southbridge, needs to be wired up,
  427. * e.g. by mips_fulong2e_init()
  428. */
  429. dc->user_creatable = false;
  430. }
  431. static const TypeInfo via_info = {
  432. .name = TYPE_VT82C686B_DEVICE,
  433. .parent = TYPE_PCI_DEVICE,
  434. .instance_size = sizeof(VT82C686BState),
  435. .class_init = via_class_init,
  436. .interfaces = (InterfaceInfo[]) {
  437. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  438. { },
  439. },
  440. };
  441. static void vt82c686b_superio_class_init(ObjectClass *klass, void *data)
  442. {
  443. ISASuperIOClass *sc = ISA_SUPERIO_CLASS(klass);
  444. sc->serial.count = 2;
  445. sc->parallel.count = 1;
  446. sc->ide.count = 0;
  447. sc->floppy.count = 1;
  448. }
  449. static const TypeInfo via_superio_info = {
  450. .name = TYPE_VT82C686B_SUPERIO,
  451. .parent = TYPE_ISA_SUPERIO,
  452. .instance_size = sizeof(ISASuperIODevice),
  453. .class_size = sizeof(ISASuperIOClass),
  454. .class_init = vt82c686b_superio_class_init,
  455. };
  456. static void vt82c686b_register_types(void)
  457. {
  458. type_register_static(&via_ac97_info);
  459. type_register_static(&via_mc97_info);
  460. type_register_static(&via_pm_info);
  461. type_register_static(&via_superio_info);
  462. type_register_static(&via_info);
  463. }
  464. type_init(vt82c686b_register_types)