articia.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Mai Logic Articia S emulation
  3. *
  4. * Copyright (c) 2023 BALATON Zoltan
  5. *
  6. * This work is licensed under the GNU GPL license version 2 or later.
  7. *
  8. */
  9. #include "qemu/osdep.h"
  10. #include "qemu/log.h"
  11. #include "qapi/error.h"
  12. #include "hw/pci/pci_device.h"
  13. #include "hw/pci/pci_host.h"
  14. #include "hw/irq.h"
  15. #include "hw/i2c/bitbang_i2c.h"
  16. #include "hw/intc/i8259.h"
  17. #include "hw/pci-host/articia.h"
  18. /*
  19. * This is a minimal emulation of this chip as used in AmigaOne board.
  20. * Most features are missing but those are not needed by firmware and guests.
  21. */
  22. OBJECT_DECLARE_SIMPLE_TYPE(ArticiaState, ARTICIA)
  23. OBJECT_DECLARE_SIMPLE_TYPE(ArticiaHostState, ARTICIA_PCI_HOST)
  24. struct ArticiaHostState {
  25. PCIDevice parent_obj;
  26. ArticiaState *as;
  27. };
  28. /* TYPE_ARTICIA */
  29. struct ArticiaState {
  30. PCIHostState parent_obj;
  31. qemu_irq irq[PCI_NUM_PINS];
  32. MemoryRegion io;
  33. MemoryRegion mem;
  34. MemoryRegion reg;
  35. bitbang_i2c_interface smbus;
  36. uint32_t gpio; /* bits 0-7 in, 8-15 out, 16-23 direction (0 in, 1 out) */
  37. hwaddr gpio_base;
  38. MemoryRegion gpio_reg;
  39. };
  40. static uint64_t articia_gpio_read(void *opaque, hwaddr addr, unsigned int size)
  41. {
  42. ArticiaState *s = opaque;
  43. return (s->gpio >> (addr * 8)) & 0xff;
  44. }
  45. static void articia_gpio_write(void *opaque, hwaddr addr, uint64_t val,
  46. unsigned int size)
  47. {
  48. ArticiaState *s = opaque;
  49. uint32_t sh = addr * 8;
  50. if (addr == 0) {
  51. /* in bits read only? */
  52. return;
  53. }
  54. if ((s->gpio & (0xff << sh)) != (val & 0xff) << sh) {
  55. s->gpio &= ~(0xff << sh | 0xff);
  56. s->gpio |= (val & 0xff) << sh;
  57. s->gpio |= bitbang_i2c_set(&s->smbus, BITBANG_I2C_SDA,
  58. s->gpio & BIT(16) ?
  59. !!(s->gpio & BIT(8)) : 1);
  60. if ((s->gpio & BIT(17))) {
  61. s->gpio &= ~BIT(0);
  62. s->gpio |= bitbang_i2c_set(&s->smbus, BITBANG_I2C_SCL,
  63. !!(s->gpio & BIT(9)));
  64. }
  65. }
  66. }
  67. static const MemoryRegionOps articia_gpio_ops = {
  68. .read = articia_gpio_read,
  69. .write = articia_gpio_write,
  70. .valid.min_access_size = 1,
  71. .valid.max_access_size = 1,
  72. .endianness = DEVICE_LITTLE_ENDIAN,
  73. };
  74. static uint64_t articia_reg_read(void *opaque, hwaddr addr, unsigned int size)
  75. {
  76. ArticiaState *s = opaque;
  77. uint64_t ret = UINT_MAX;
  78. switch (addr) {
  79. case 0xc00cf8:
  80. ret = pci_host_conf_le_ops.read(PCI_HOST_BRIDGE(s), 0, size);
  81. break;
  82. case 0xe00cfc ... 0xe00cff:
  83. ret = pci_host_data_le_ops.read(PCI_HOST_BRIDGE(s), addr - 0xe00cfc, size);
  84. break;
  85. case 0xf00000:
  86. ret = pic_read_irq(isa_pic);
  87. break;
  88. default:
  89. qemu_log_mask(LOG_UNIMP, "%s: Unimplemented register read 0x%"
  90. HWADDR_PRIx " %d\n", __func__, addr, size);
  91. break;
  92. }
  93. return ret;
  94. }
  95. static void articia_reg_write(void *opaque, hwaddr addr, uint64_t val,
  96. unsigned int size)
  97. {
  98. ArticiaState *s = opaque;
  99. switch (addr) {
  100. case 0xc00cf8:
  101. pci_host_conf_le_ops.write(PCI_HOST_BRIDGE(s), 0, val, size);
  102. break;
  103. case 0xe00cfc ... 0xe00cff:
  104. pci_host_data_le_ops.write(PCI_HOST_BRIDGE(s), addr, val, size);
  105. break;
  106. default:
  107. qemu_log_mask(LOG_UNIMP, "%s: Unimplemented register write 0x%"
  108. HWADDR_PRIx " %d <- %"PRIx64"\n", __func__, addr, size, val);
  109. break;
  110. }
  111. }
  112. static const MemoryRegionOps articia_reg_ops = {
  113. .read = articia_reg_read,
  114. .write = articia_reg_write,
  115. .valid.min_access_size = 1,
  116. .valid.max_access_size = 4,
  117. .endianness = DEVICE_LITTLE_ENDIAN,
  118. };
  119. static void articia_pcihost_set_irq(void *opaque, int n, int level)
  120. {
  121. ArticiaState *s = opaque;
  122. qemu_set_irq(s->irq[n], level);
  123. }
  124. /*
  125. * AmigaOne SE PCI slot to IRQ routing
  126. *
  127. * repository: https://source.denx.de/u-boot/custodians/u-boot-avr32.git
  128. * refspec: v2010.06
  129. * file: board/MAI/AmigaOneG3SE/articiaS_pci.c
  130. */
  131. static int amigaone_pcihost_bus0_map_irq(PCIDevice *pdev, int pin)
  132. {
  133. int devfn_slot = PCI_SLOT(pdev->devfn);
  134. switch (devfn_slot) {
  135. case 6: /* On board ethernet */
  136. return 3;
  137. case 7: /* South bridge */
  138. return pin;
  139. default: /* PCI Slot 1 Devfn slot 8, Slot 2 Devfn 9, Slot 3 Devfn 10 */
  140. return pci_swizzle(devfn_slot, pin);
  141. }
  142. }
  143. static void articia_realize(DeviceState *dev, Error **errp)
  144. {
  145. ArticiaState *s = ARTICIA(dev);
  146. PCIHostState *h = PCI_HOST_BRIDGE(dev);
  147. PCIDevice *pdev;
  148. bitbang_i2c_init(&s->smbus, i2c_init_bus(dev, "smbus"));
  149. memory_region_init_io(&s->gpio_reg, OBJECT(s), &articia_gpio_ops, s,
  150. TYPE_ARTICIA, 4);
  151. memory_region_init(&s->mem, OBJECT(dev), "pci-mem", UINT64_MAX);
  152. memory_region_init(&s->io, OBJECT(dev), "pci-io", 0xc00000);
  153. memory_region_init_io(&s->reg, OBJECT(s), &articia_reg_ops, s,
  154. TYPE_ARTICIA, 0x1000000);
  155. memory_region_add_subregion_overlap(&s->reg, 0, &s->io, 1);
  156. /* devfn_min is 8 that matches first PCI slot in AmigaOne */
  157. h->bus = pci_register_root_bus(dev, NULL, articia_pcihost_set_irq,
  158. amigaone_pcihost_bus0_map_irq, dev, &s->mem,
  159. &s->io, PCI_DEVFN(8, 0), 4, TYPE_PCI_BUS);
  160. pdev = pci_create_simple_multifunction(h->bus, PCI_DEVFN(0, 0),
  161. TYPE_ARTICIA_PCI_HOST);
  162. ARTICIA_PCI_HOST(pdev)->as = s;
  163. pci_create_simple(h->bus, PCI_DEVFN(0, 1), TYPE_ARTICIA_PCI_BRIDGE);
  164. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->reg);
  165. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mem);
  166. qdev_init_gpio_out(dev, s->irq, ARRAY_SIZE(s->irq));
  167. }
  168. static void articia_class_init(ObjectClass *klass, void *data)
  169. {
  170. DeviceClass *dc = DEVICE_CLASS(klass);
  171. dc->realize = articia_realize;
  172. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  173. }
  174. /* TYPE_ARTICIA_PCI_HOST */
  175. static void articia_pci_host_cfg_write(PCIDevice *d, uint32_t addr,
  176. uint32_t val, int len)
  177. {
  178. ArticiaState *s = ARTICIA_PCI_HOST(d)->as;
  179. pci_default_write_config(d, addr, val, len);
  180. switch (addr) {
  181. case 0x40:
  182. s->gpio_base = val;
  183. break;
  184. case 0x44:
  185. if (val != 0x11) {
  186. /* FIXME what do the bits actually mean? */
  187. break;
  188. }
  189. if (memory_region_is_mapped(&s->gpio_reg)) {
  190. memory_region_del_subregion(&s->io, &s->gpio_reg);
  191. }
  192. memory_region_add_subregion(&s->io, s->gpio_base + 0x38, &s->gpio_reg);
  193. break;
  194. }
  195. }
  196. static void articia_pci_host_class_init(ObjectClass *klass, void *data)
  197. {
  198. DeviceClass *dc = DEVICE_CLASS(klass);
  199. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  200. k->config_write = articia_pci_host_cfg_write;
  201. k->vendor_id = 0x10cc;
  202. k->device_id = 0x0660;
  203. k->class_id = PCI_CLASS_BRIDGE_HOST;
  204. /*
  205. * PCI-facing part of the host bridge,
  206. * not usable without the host-facing part
  207. */
  208. dc->user_creatable = false;
  209. }
  210. /* TYPE_ARTICIA_PCI_BRIDGE */
  211. static void articia_pci_bridge_class_init(ObjectClass *klass, void *data)
  212. {
  213. DeviceClass *dc = DEVICE_CLASS(klass);
  214. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  215. k->vendor_id = 0x10cc;
  216. k->device_id = 0x0661;
  217. k->class_id = PCI_CLASS_BRIDGE_HOST;
  218. /*
  219. * PCI-facing part of the host bridge,
  220. * not usable without the host-facing part
  221. */
  222. dc->user_creatable = false;
  223. }
  224. static const TypeInfo articia_types[] = {
  225. {
  226. .name = TYPE_ARTICIA,
  227. .parent = TYPE_PCI_HOST_BRIDGE,
  228. .instance_size = sizeof(ArticiaState),
  229. .class_init = articia_class_init,
  230. },
  231. {
  232. .name = TYPE_ARTICIA_PCI_HOST,
  233. .parent = TYPE_PCI_DEVICE,
  234. .instance_size = sizeof(ArticiaHostState),
  235. .class_init = articia_pci_host_class_init,
  236. .interfaces = (InterfaceInfo[]) {
  237. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  238. { },
  239. },
  240. },
  241. {
  242. .name = TYPE_ARTICIA_PCI_BRIDGE,
  243. .parent = TYPE_PCI_DEVICE,
  244. .instance_size = sizeof(PCIDevice),
  245. .class_init = articia_pci_bridge_class_init,
  246. .interfaces = (InterfaceInfo[]) {
  247. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  248. { },
  249. },
  250. },
  251. };
  252. DEFINE_TYPES(articia_types)