2 * QEMU TEWS TPCI200 IndustryPack carrier emulation
4 * Copyright (C) 2012 Igalia, S.L.
7 * This code is licensed under the GNU GPL v2 or (at your option) any
11 #include "hw/ipack/ipack.h"
12 #include "hw/pci/pci.h"
13 #include "qemu/bitops.h"
16 /* #define DEBUG_TPCI */
19 #define DPRINTF(fmt, ...) \
20 do { fprintf(stderr, "TPCI200: " fmt, ## __VA_ARGS__); } while (0)
22 #define DPRINTF(fmt, ...) do { } while (0)
28 #define IP_INT_SPACE 3
29 #define IP_IO_SPACE_ADDR_MASK 0x7F
30 #define IP_ID_SPACE_ADDR_MASK 0x3F
31 #define IP_INT_SPACE_ADDR_MASK 0x3F
33 #define STATUS_INT(IP, INTNO) BIT((IP) * 2 + (INTNO))
34 #define STATUS_TIME(IP) BIT((IP) + 12)
35 #define STATUS_ERR_ANY 0xF00
37 #define CTRL_CLKRATE BIT(0)
38 #define CTRL_RECOVER BIT(1)
39 #define CTRL_TIME_INT BIT(2)
40 #define CTRL_ERR_INT BIT(3)
41 #define CTRL_INT_EDGE(INTNO) BIT(4 + (INTNO))
42 #define CTRL_INT(INTNO) BIT(6 + (INTNO))
44 #define REG_REV_ID 0x00
45 #define REG_IP_A_CTRL 0x02
46 #define REG_IP_B_CTRL 0x04
47 #define REG_IP_C_CTRL 0x06
48 #define REG_IP_D_CTRL 0x08
49 #define REG_RESET 0x0A
50 #define REG_STATUS 0x0C
51 #define IP_N_FROM_REG(REG) ((REG) / 2 - 1)
63 uint8_t ctrl[N_MODULES];
68 #define TYPE_TPCI200 "tpci200"
70 #define TPCI200(obj) \
71 OBJECT_CHECK(TPCI200State, (obj), TYPE_TPCI200)
73 static const uint8_t local_config_regs[] = {
74 0x00, 0xFF, 0xFF, 0x0F, 0x00, 0xFC, 0xFF, 0x0F, 0x00, 0x00, 0x00,
75 0x0E, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
76 0x00, 0x08, 0x01, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00, 0x01,
77 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x60, 0x41, 0xD4,
78 0xA2, 0x20, 0x41, 0x14, 0xA2, 0x20, 0x41, 0x14, 0xA2, 0x20, 0x01,
79 0x14, 0x00, 0x00, 0x00, 0x00, 0x81, 0x00, 0x00, 0x08, 0x01, 0x02,
80 0x00, 0x04, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x80, 0x02, 0x41,
81 0x00, 0x00, 0x00, 0x00, 0x40, 0x7A, 0x00, 0x52, 0x92, 0x24, 0x02
84 static void adjust_addr(bool big_endian, hwaddr *addr, unsigned size)
86 /* During 8 bit access in big endian mode,
87 odd and even addresses are swapped */
88 if (big_endian && size == 1) {
93 static uint64_t adjust_value(bool big_endian, uint64_t *val, unsigned size)
95 /* Local spaces only support 8/16 bit access,
96 * so there's no need to care for sizes > 2 */
97 if (big_endian && size == 2) {
103 static void tpci200_set_irq(void *opaque, int intno, int level)
105 IPackDevice *ip = opaque;
106 IPackBus *bus = IPACK_BUS(qdev_get_parent_bus(DEVICE(ip)));
107 PCIDevice *pcidev = PCI_DEVICE(BUS(bus)->parent);
108 TPCI200State *dev = TPCI200(pcidev);
109 unsigned ip_n = ip->slot;
110 uint16_t prev_status = dev->status;
112 assert(ip->slot >= 0 && ip->slot < N_MODULES);
114 /* The requested interrupt must be enabled in the IP CONTROL
116 if (!(dev->ctrl[ip_n] & CTRL_INT(intno))) {
120 /* Update the interrupt status in the IP STATUS register */
122 dev->status |= STATUS_INT(ip_n, intno);
124 dev->status &= ~STATUS_INT(ip_n, intno);
127 /* Return if there are no changes */
128 if (dev->status == prev_status) {
132 DPRINTF("IP %u INT%u#: %u\n", ip_n, intno, level);
134 /* Check if the interrupt is edge sensitive */
135 if (dev->ctrl[ip_n] & CTRL_INT_EDGE(intno)) {
137 pci_set_irq(&dev->dev, !dev->int_set);
138 pci_set_irq(&dev->dev, dev->int_set);
142 uint16_t level_status = dev->status;
144 /* Check if there are any level sensitive interrupts set by
145 removing the ones that are edge sensitive from the status
147 for (i = 0; i < N_MODULES; i++) {
148 for (j = 0; j < 2; j++) {
149 if (dev->ctrl[i] & CTRL_INT_EDGE(j)) {
150 level_status &= ~STATUS_INT(i, j);
155 if (level_status && !dev->int_set) {
156 pci_irq_assert(&dev->dev);
158 } else if (!level_status && dev->int_set) {
159 pci_irq_deassert(&dev->dev);
165 static uint64_t tpci200_read_cfg(void *opaque, hwaddr addr, unsigned size)
167 TPCI200State *s = opaque;
169 if (addr < ARRAY_SIZE(local_config_regs)) {
170 ret = local_config_regs[addr];
172 /* Endianness is stored in the first bit of these registers */
173 if ((addr == 0x2b && s->big_endian[0]) ||
174 (addr == 0x2f && s->big_endian[1]) ||
175 (addr == 0x33 && s->big_endian[2])) {
178 DPRINTF("Read from LCR 0x%x: 0x%x\n", (unsigned) addr, (unsigned) ret);
182 static void tpci200_write_cfg(void *opaque, hwaddr addr, uint64_t val,
185 TPCI200State *s = opaque;
186 /* Endianness is stored in the first bit of these registers */
187 if (addr == 0x2b || addr == 0x2f || addr == 0x33) {
188 unsigned las = (addr - 0x2b) / 4;
189 s->big_endian[las] = val & 1;
190 DPRINTF("LAS%u big endian mode: %u\n", las, (unsigned) val & 1);
192 DPRINTF("Write to LCR 0x%x: 0x%x\n", (unsigned) addr, (unsigned) val);
196 static uint64_t tpci200_read_las0(void *opaque, hwaddr addr, unsigned size)
198 TPCI200State *s = opaque;
204 DPRINTF("Read REVISION ID\n"); /* Current value is 0x00 */
212 unsigned ip_n = IP_N_FROM_REG(addr);
214 DPRINTF("Read IP %c CONTROL: 0x%x\n", 'A' + ip_n, (unsigned) ret);
219 DPRINTF("Read RESET\n"); /* Not implemented */
224 DPRINTF("Read STATUS: 0x%x\n", (unsigned) ret);
229 DPRINTF("Unsupported read from LAS0 0x%x\n", (unsigned) addr);
233 return adjust_value(s->big_endian[0], &ret, size);
236 static void tpci200_write_las0(void *opaque, hwaddr addr, uint64_t val,
239 TPCI200State *s = opaque;
241 adjust_value(s->big_endian[0], &val, size);
246 DPRINTF("Write Revision ID: 0x%x\n", (unsigned) val); /* No effect */
254 unsigned ip_n = IP_N_FROM_REG(addr);
256 DPRINTF("Write IP %c CONTROL: 0x%x\n", 'A' + ip_n, (unsigned) val);
261 DPRINTF("Write RESET: 0x%x\n", (unsigned) val); /* Not implemented */
268 for (i = 0; i < N_MODULES; i++) {
269 IPackDevice *ip = ipack_device_find(&s->bus, i);
272 if (val & STATUS_INT(i, 0)) {
273 DPRINTF("Clear IP %c INT0# status\n", 'A' + i);
274 qemu_irq_lower(ip->irq[0]);
276 if (val & STATUS_INT(i, 1)) {
277 DPRINTF("Clear IP %c INT1# status\n", 'A' + i);
278 qemu_irq_lower(ip->irq[1]);
282 if (val & STATUS_TIME(i)) {
283 DPRINTF("Clear IP %c timeout\n", 'A' + i);
284 s->status &= ~STATUS_TIME(i);
288 if (val & STATUS_ERR_ANY) {
289 DPRINTF("Unexpected write to STATUS register: 0x%x\n",
297 DPRINTF("Unsupported write to LAS0 0x%x: 0x%x\n",
298 (unsigned) addr, (unsigned) val);
303 static uint64_t tpci200_read_las1(void *opaque, hwaddr addr, unsigned size)
305 TPCI200State *s = opaque;
308 unsigned ip_n, space;
311 adjust_addr(s->big_endian[1], &addr, size);
314 * The address is divided into the IP module number (0-4), the IP
315 * address space (I/O, ID, INT) and the offset within that space.
318 space = (addr >> 6) & 3;
319 ip = ipack_device_find(&s->bus, ip_n);
322 DPRINTF("Read LAS1: IP module %u not installed\n", ip_n);
324 IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(ip);
328 offset = addr & IP_ID_SPACE_ADDR_MASK;
330 ret = k->id_read(ip, offset);
335 offset = addr & IP_INT_SPACE_ADDR_MASK;
337 /* Read address 0 to ACK IP INT0# and address 2 to ACK IP INT1# */
338 if (offset == 0 || offset == 2) {
339 unsigned intno = offset / 2;
340 bool int_set = s->status & STATUS_INT(ip_n, intno);
341 bool int_edge_sensitive = s->ctrl[ip_n] & CTRL_INT_EDGE(intno);
342 if (int_set && !int_edge_sensitive) {
343 qemu_irq_lower(ip->irq[intno]);
348 ret = k->int_read(ip, offset);
353 offset = addr & IP_IO_SPACE_ADDR_MASK;
355 ret = k->io_read(ip, offset);
361 return adjust_value(s->big_endian[1], &ret, size);
364 static void tpci200_write_las1(void *opaque, hwaddr addr, uint64_t val,
367 TPCI200State *s = opaque;
369 unsigned ip_n, space;
372 adjust_addr(s->big_endian[1], &addr, size);
373 adjust_value(s->big_endian[1], &val, size);
376 * The address is divided into the IP module number, the IP
377 * address space (I/O, ID, INT) and the offset within that space.
380 space = (addr >> 6) & 3;
381 ip = ipack_device_find(&s->bus, ip_n);
384 DPRINTF("Write LAS1: IP module %u not installed\n", ip_n);
386 IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(ip);
390 offset = addr & IP_ID_SPACE_ADDR_MASK;
392 k->id_write(ip, offset, val);
397 offset = addr & IP_INT_SPACE_ADDR_MASK;
399 k->int_write(ip, offset, val);
404 offset = addr & IP_IO_SPACE_ADDR_MASK;
406 k->io_write(ip, offset, val);
413 static uint64_t tpci200_read_las2(void *opaque, hwaddr addr, unsigned size)
415 TPCI200State *s = opaque;
421 adjust_addr(s->big_endian[2], &addr, size);
424 * The address is divided into the IP module number and the offset
425 * within the IP module MEM space.
428 offset = addr & 0x7fffff;
429 ip = ipack_device_find(&s->bus, ip_n);
432 DPRINTF("Read LAS2: IP module %u not installed\n", ip_n);
434 IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(ip);
436 ret = k->mem_read16(ip, offset);
440 return adjust_value(s->big_endian[2], &ret, size);
443 static void tpci200_write_las2(void *opaque, hwaddr addr, uint64_t val,
446 TPCI200State *s = opaque;
451 adjust_addr(s->big_endian[2], &addr, size);
452 adjust_value(s->big_endian[2], &val, size);
455 * The address is divided into the IP module number and the offset
456 * within the IP module MEM space.
459 offset = addr & 0x7fffff;
460 ip = ipack_device_find(&s->bus, ip_n);
463 DPRINTF("Write LAS2: IP module %u not installed\n", ip_n);
465 IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(ip);
466 if (k->mem_write16) {
467 k->mem_write16(ip, offset, val);
472 static uint64_t tpci200_read_las3(void *opaque, hwaddr addr, unsigned size)
474 TPCI200State *s = opaque;
478 * The address is divided into the IP module number and the offset
479 * within the IP module MEM space.
481 unsigned ip_n = addr >> 22;
482 uint32_t offset = addr & 0x3fffff;
484 ip = ipack_device_find(&s->bus, ip_n);
487 DPRINTF("Read LAS3: IP module %u not installed\n", ip_n);
489 IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(ip);
491 ret = k->mem_read8(ip, offset);
498 static void tpci200_write_las3(void *opaque, hwaddr addr, uint64_t val,
501 TPCI200State *s = opaque;
504 * The address is divided into the IP module number and the offset
505 * within the IP module MEM space.
507 unsigned ip_n = addr >> 22;
508 uint32_t offset = addr & 0x3fffff;
510 ip = ipack_device_find(&s->bus, ip_n);
513 DPRINTF("Write LAS3: IP module %u not installed\n", ip_n);
515 IPackDeviceClass *k = IPACK_DEVICE_GET_CLASS(ip);
517 k->mem_write8(ip, offset, val);
522 static const MemoryRegionOps tpci200_cfg_ops = {
523 .read = tpci200_read_cfg,
524 .write = tpci200_write_cfg,
525 .endianness = DEVICE_NATIVE_ENDIAN,
527 .min_access_size = 1,
531 .min_access_size = 1,
536 static const MemoryRegionOps tpci200_las0_ops = {
537 .read = tpci200_read_las0,
538 .write = tpci200_write_las0,
539 .endianness = DEVICE_NATIVE_ENDIAN,
541 .min_access_size = 2,
546 static const MemoryRegionOps tpci200_las1_ops = {
547 .read = tpci200_read_las1,
548 .write = tpci200_write_las1,
549 .endianness = DEVICE_NATIVE_ENDIAN,
551 .min_access_size = 1,
556 static const MemoryRegionOps tpci200_las2_ops = {
557 .read = tpci200_read_las2,
558 .write = tpci200_write_las2,
559 .endianness = DEVICE_NATIVE_ENDIAN,
561 .min_access_size = 1,
566 static const MemoryRegionOps tpci200_las3_ops = {
567 .read = tpci200_read_las3,
568 .write = tpci200_write_las3,
569 .endianness = DEVICE_NATIVE_ENDIAN,
571 .min_access_size = 1,
576 static int tpci200_initfn(PCIDevice *pci_dev)
578 TPCI200State *s = TPCI200(pci_dev);
579 uint8_t *c = s->dev.config;
581 pci_set_word(c + PCI_COMMAND, 0x0003);
582 pci_set_word(c + PCI_STATUS, 0x0280);
584 pci_set_byte(c + PCI_INTERRUPT_PIN, 0x01); /* Interrupt pin A */
586 pci_set_byte(c + PCI_CAPABILITY_LIST, 0x40);
587 pci_set_long(c + 0x40, 0x48014801);
588 pci_set_long(c + 0x48, 0x00024C06);
589 pci_set_long(c + 0x4C, 0x00000003);
591 memory_region_init_io(&s->mmio, OBJECT(s), &tpci200_cfg_ops,
592 s, "tpci200_mmio", 128);
593 memory_region_init_io(&s->io, OBJECT(s), &tpci200_cfg_ops,
594 s, "tpci200_io", 128);
595 memory_region_init_io(&s->las0, OBJECT(s), &tpci200_las0_ops,
596 s, "tpci200_las0", 256);
597 memory_region_init_io(&s->las1, OBJECT(s), &tpci200_las1_ops,
598 s, "tpci200_las1", 1024);
599 memory_region_init_io(&s->las2, OBJECT(s), &tpci200_las2_ops,
600 s, "tpci200_las2", 1024*1024*32);
601 memory_region_init_io(&s->las3, OBJECT(s), &tpci200_las3_ops,
602 s, "tpci200_las3", 1024*1024*16);
603 pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mmio);
604 pci_register_bar(&s->dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
605 pci_register_bar(&s->dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->las0);
606 pci_register_bar(&s->dev, 3, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->las1);
607 pci_register_bar(&s->dev, 4, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->las2);
608 pci_register_bar(&s->dev, 5, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->las3);
610 ipack_bus_new_inplace(&s->bus, sizeof(s->bus), DEVICE(pci_dev), NULL,
611 N_MODULES, tpci200_set_irq);
616 static void tpci200_exitfn(PCIDevice *pci_dev)
618 TPCI200State *s = TPCI200(pci_dev);
620 memory_region_destroy(&s->mmio);
621 memory_region_destroy(&s->io);
622 memory_region_destroy(&s->las0);
623 memory_region_destroy(&s->las1);
624 memory_region_destroy(&s->las2);
625 memory_region_destroy(&s->las3);
628 static const VMStateDescription vmstate_tpci200 = {
631 .minimum_version_id = 1,
632 .minimum_version_id_old = 1,
633 .fields = (VMStateField[]) {
634 VMSTATE_PCI_DEVICE(dev, TPCI200State),
635 VMSTATE_BOOL_ARRAY(big_endian, TPCI200State, 3),
636 VMSTATE_UINT8_ARRAY(ctrl, TPCI200State, N_MODULES),
637 VMSTATE_UINT16(status, TPCI200State),
638 VMSTATE_UINT8(int_set, TPCI200State),
639 VMSTATE_END_OF_LIST()
643 static void tpci200_class_init(ObjectClass *klass, void *data)
645 DeviceClass *dc = DEVICE_CLASS(klass);
646 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
648 k->init = tpci200_initfn;
649 k->exit = tpci200_exitfn;
650 k->vendor_id = PCI_VENDOR_ID_TEWS;
651 k->device_id = PCI_DEVICE_ID_TEWS_TPCI200;
652 k->class_id = PCI_CLASS_BRIDGE_OTHER;
653 k->subsystem_vendor_id = PCI_VENDOR_ID_TEWS;
654 k->subsystem_id = 0x300A;
655 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
656 dc->desc = "TEWS TPCI200 IndustryPack carrier";
657 dc->vmsd = &vmstate_tpci200;
660 static const TypeInfo tpci200_info = {
661 .name = TYPE_TPCI200,
662 .parent = TYPE_PCI_DEVICE,
663 .instance_size = sizeof(TPCI200State),
664 .class_init = tpci200_class_init,
667 static void tpci200_register_types(void)
669 type_register_static(&tpci200_info);
672 type_init(tpci200_register_types)