Home | History | Annotate | Download | only in hw

Lines Matching full:ohci

2  * QEMU USB OHCI Emulation
77 /* OHCI state */
106 /* PXA27x Non-OHCI events */
131 static void ohci_bus_stop(OHCIState *ohci);
216 /* OHCI Local stuff */
311 static inline void ohci_intr_update(OHCIState *ohci)
315 if ((ohci->intr & OHCI_INTR_MIE) &&
316 (ohci->intr_status & ohci->intr))
319 qemu_set_irq(ohci->irq, level);
323 static inline void ohci_set_interrupt(OHCIState *ohci, uint32_t intr)
325 ohci->intr_status |= intr;
326 ohci_intr_update(ohci);
356 dprintf("usb-ohci: Attached port %d\n", port1->index);
374 dprintf("usb-ohci: Detached port %d\n", port1->index);
384 OHCIState *ohci = opaque;
388 ohci_bus_stop(ohci);
389 ohci->ctl = 0;
390 ohci->old_ctl = 0;
391 ohci->status = 0;
392 ohci->intr_status = 0;
393 ohci->intr = OHCI_INTR_MIE;
395 ohci->hcca = 0;
396 ohci->ctrl_head = ohci->ctrl_cur = 0;
397 ohci->bulk_head = ohci->bulk_cur = 0;
398 ohci->per_cur = 0;
399 ohci->done = 0;
400 ohci->done_count = 7;
405 ohci->fsmps = 0x2778;
406 ohci->fi = 0x2edf;
407 ohci->fit = 0;
408 ohci->frt = 0;
409 ohci->frame_number = 0;
410 ohci->pstart = 0;
411 ohci->lst = OHCI_LS_THRESH;
413 ohci->rhdesc_a = OHCI_RHA_NPS | ohci->num_ports;
414 ohci->rhdesc_b = 0x0; /* Impl. specific */
415 ohci->rhstatus = 0;
417 for (i = 0; i < ohci->num_ports; i++)
419 port = &ohci->rhport[i];
424 if (ohci->async_td) {
425 usb_cancel_packet(&ohci->usb_packet);
426 ohci->async_td = 0;
428 dprintf("usb-ohci: Reset %s\n", ohci->name);
432 static inline int get_dwords(OHCIState *ohci,
437 addr += ohci->localmem_base;
448 static inline int put_dwords(OHCIState *ohci,
453 addr += ohci->localmem_base;
464 static inline int get_words(OHCIState *ohci,
469 addr += ohci->localmem_base;
480 static inline int put_words(OHCIState *ohci,
485 addr += ohci->localmem_base;
495 static inline int ohci_read_ed(OHCIState *ohci,
498 return get_dwords(ohci, addr, (uint32_t *)ed, sizeof(*ed) >> 2);
501 static inline int ohci_read_td(OHCIState *ohci,
504 return get_dwords(ohci, addr, (uint32_t *)td, sizeof(*td) >> 2);
507 static inline int ohci_read_iso_td(OHCIState *ohci,
510 return (get_dwords(ohci, addr, (uint32_t *)td, 4) &&
511 get_words(ohci, addr + 16, td->offset, 8));
514 static inline int ohci_read_hcca(OHCIState *ohci,
517 cpu_physical_memory_rw(addr + ohci->localmem_base,
522 static inline int ohci_put_ed(OHCIState *ohci,
525 return put_dwords(ohci, addr, (uint32_t *)ed, sizeof(*ed) >> 2);
528 static inline int ohci_put_td(OHCIState *ohci,
531 return put_dwords(ohci, addr, (uint32_t *)td, sizeof(*td) >> 2);
534 static inline int ohci_put_iso_td(OHCIState *ohci,
537 return (put_dwords(ohci, addr, (uint32_t *)td, 4) &&
538 put_words(ohci, addr + 16, td->offset, 8));
541 static inline int ohci_put_hcca(OHCIState *ohci,
544 cpu_physical_memory_rw(addr + ohci->localmem_base,
550 static void ohci_copy_td(OHCIState *ohci, struct ohci_td *td,
560 cpu_physical_memory_rw(ptr + ohci->localmem_base, buf, n, write);
565 cpu_physical_memory_rw(ptr + ohci->localmem_base, buf, len - n, write);
569 static void ohci_copy_iso_td(OHCIState *ohci,
580 cpu_physical_memory_rw(ptr + ohci->localmem_base, buf, n, write);
585 cpu_physical_memory_rw(ptr + ohci->localmem_base, buf, len - n, write);
588 static void ohci_process_lists(OHCIState *ohci, int completion);
592 OHCIState *ohci = opaque;
596 ohci->async_complete = 1;
597 ohci_process_lists(ohci, 1);
602 static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed,
622 if (!ohci_read_iso_td(ohci, addr, &iso_td)) {
623 printf("usb-ohci: ISO_TD read error at %x\n", addr);
629 relative_frame_number = USUB(ohci->frame_number, starting_frame);
643 ohci->frame_number, starting_frame,
649 dprintf("usb-ohci: ISO_TD R=%d < 0\n", relative_frame_number);
654 dprintf("usb-ohci: ISO_TD R=%d > FC=%d\n", relative_frame_number,
659 iso_td.next = ohci->done;
660 ohci->done = addr;
662 if (i < ohci->done_count)
663 ohci->done_count = i;
664 ohci_put_iso_td(ohci, addr, &iso_td);
683 printf("usb-ohci: Bad direction %d\n", dir);
688 printf("usb-ohci: ISO_TD bp 0x%.8x be 0x%.8x\n", iso_td.bp, iso_td.be);
698 printf("usb-ohci: ISO_TD cc != not accessed 0x%.8x 0x%.8x\n",
704 printf("usb-ohci: ISO_TD start_offset=0x%.8x > next_offset=0x%.8x\n",
739 ohci_copy_iso_td(ohci, start_addr, end_addr, ohci->usb_buf, len, 0);
743 ret = ohci->usb_packet.len;
746 for (i = 0; i < ohci->num_ports; i++) {
747 dev = ohci->rhport[i].port.dev;
748 if ((ohci->rhport[i].ctrl & OHCI_PORT_PES) == 0)
750 ohci->usb_packet.pid = pid;
751 ohci->usb_packet.devaddr = OHCI_BM(ed->flags, ED_FA);
752 ohci->usb_packet.devep = OHCI_BM(ed->flags, ED_EN);
753 ohci->usb_packet.data = ohci->usb_buf;
754 ohci->usb_packet.len = len;
755 ohci->usb_packet.complete_cb = ohci_async_complete_packet;
756 ohci->usb_packet.complete_opaque = ohci;
757 ret = dev->handle_packet(dev, &ohci->usb_packet);
775 ohci_copy_iso_td(ohci, start_addr, end_addr, ohci->usb_buf, ret, 1);
786 printf("usb-ohci: DataOverrun %d > %zu\n", ret, len);
792 printf("usb-ohci: DataUnderrun %d\n", ret);
805 printf("usb-ohci: got NAK/STALL %d\n", ret);
812 printf("usb-ohci: Bad device response %d\n", ret);
825 iso_td.next = ohci->done;
826 ohci->done = addr;
828 if (i < ohci->done_count)
829 ohci->done_count = i;
831 ohci_put_iso_td(ohci, addr, &iso_td);
838 static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed)
854 completion = (addr == ohci->async_td);
855 if (completion && !ohci->async_complete) {
861 if (!ohci_read_td(ohci, addr, &td)) {
862 fprintf(stderr, "usb-ohci: TD read error at %x\n", addr);
891 fprintf(stderr, "usb-ohci: Bad direction\n");
902 ohci_copy_td(ohci, &td, ohci->usb_buf, len, 0);
914 printf(" %.2x", ohci->usb_buf[i]);
919 ret = ohci->usb_packet.len;
920 ohci->async_td = 0;
921 ohci->async_complete = 0;
924 for (i = 0; i < ohci->num_ports; i++) {
925 dev = ohci->rhport[i].port.dev;
926 if ((ohci->rhport[i].ctrl & OHCI_PORT_PES) == 0)
929 if (ohci->async_td) {
940 ohci->usb_packet.pid = pid;
941 ohci->usb_packet.devaddr = OHCI_BM(ed->flags, ED_FA);
942 ohci->usb_packet.devep = OHCI_BM(ed->flags, ED_EN);
943 ohci->usb_packet.data = ohci->usb_buf;
944 ohci->usb_packet.len = len;
945 ohci->usb_packet.complete_cb = ohci_async_complete_packet;
946 ohci->usb_packet.complete_opaque = ohci;
947 ret = dev->handle_packet(dev, &ohci->usb_packet);
955 ohci->async_td = addr;
961 ohci_copy_td(ohci, &td, ohci->usb_buf, ret, 1);
965 printf(" %.2x", ohci->usb_buf[i]);
995 dprintf("usb-ohci: Underrun\n");
1002 dprintf("usb-ohci: got NAK\n");
1005 dprintf("usb-ohci: got STALL\n");
1009 dprintf("usb-ohci: got BABBLE\n");
1013 fprintf(stderr, "usb-ohci: Bad device response %d\n", ret);
1025 td.next = ohci->done;
1026 ohci->done = addr;
1028 if (i < ohci->done_count)
1029 ohci->done_count = i;
1030 ohci_put_td(ohci, addr, &td);
1035 static int ohci_service_ed_list(OHCIState *ohci, uint32_t head, int completion)
1048 if (!ohci_read_ed(ohci, cur, &ed)) {
1049 fprintf(stderr, "usb-ohci: ED read error at %x\n", cur);
1059 if (ohci->async_td && addr == ohci->async_td) {
1060 usb_cancel_packet(&ohci->usb_packet);
1061 ohci->async_td = 0;
1080 if (ohci_service_td(ohci, &ed))
1084 if (ohci_service_iso_td(ohci, &ed, completion))
1089 ohci_put_ed(ohci, cur, &ed);
1096 static void ohci_sof(OHCIState *ohci)
1098 ohci->sof_time = qemu_get_clock_ns(vm_clock);
1099 qemu_mod_timer(ohci->eof_timer, ohci->sof_time + usb_frame_time);
1100 ohci_set_interrupt(ohci, OHCI_INTR_SF);
1104 static void ohci_process_lists(OHCIState *ohci, int completion)
1106 if ((ohci->ctl & OHCI_CTL_CLE) && (ohci->status & OHCI_STATUS_CLF)) {
1107 if (ohci->ctrl_cur && ohci->ctrl_cur != ohci->ctrl_head)
1108 dprintf("usb-ohci: head %x, cur %x\n",
1109 ohci->ctrl_head, ohci->ctrl_cur);
1110 if (!ohci_service_ed_list(ohci, ohci->ctrl_head, completion)) {
1111 ohci->ctrl_cur = 0;
1112 ohci->status &= ~OHCI_STATUS_CLF;
1116 if ((ohci->ctl & OHCI_CTL_BLE) && (ohci->status & OHCI_STATUS_BLF)) {
1117 if (!ohci_service_ed_list(ohci, ohci->bulk_head, completion)) {
1118 ohci->bulk_cur = 0;
1119 ohci->status &= ~OHCI_STATUS_BLF;
1127 OHCIState *ohci = opaque;
1130 ohci_read_hcca(ohci, ohci->hcca, &hcca);
1133 if (ohci->ctl & OHCI_CTL_PLE) {
1136 n = ohci->frame_number & 0x1f;
1137 ohci_service_ed_list(ohci, le32_to_cpu(hcca.intr[n]), 0);
1141 if (ohci->async_td &&
1142 ohci->old_ctl & (~ohci->ctl) & (OHCI_CTL_BLE | OHCI_CTL_CLE)) {
1143 usb_cancel_packet(&ohci->usb_packet);
1144 ohci->async_td = 0;
1146 ohci->old_ctl = ohci->ctl;
1147 ohci_process_lists(ohci, 0);
1150 ohci->frt = ohci->fit;
1153 ohci->frame_number = (ohci->frame_number + 1) & 0xffff;
1154 hcca.frame = cpu_to_le32(ohci->frame_number);
1156 if (ohci->done_count == 0 && !(ohci->intr_status & OHCI_INTR_WD)) {
1157 if (!ohci->done)
1159 if (ohci->intr & ohci->intr_status)
1160 ohci->done |= 1;
1161 hcca.done = cpu_to_le32(ohci->done);
1162 ohci->done = 0;
1163 ohci->done_count = 7;
1164 ohci_set_interrupt(ohci, OHCI_INTR_WD);
1167 if (ohci->done_count != 7 && ohci->done_count != 0)
1168 ohci->done_count--;
1171 ohci_sof(ohci);
1174 ohci_put_hcca(ohci, ohci->hcca, &hcca);
1180 static int ohci_bus_start(OHCIState *ohci)
1182 ohci->eof_timer = qemu_new_timer_ns(vm_clock,
1184 ohci);
1186 if (ohci->eof_timer == NULL) {
1187 fprintf(stderr, "usb-ohci: %s: qemu_new_timer_ns failed\n", ohci->name);
1192 dprintf("usb-ohci: %s: USB Operational\n", ohci->name);
1194 ohci_sof(ohci);
1200 static void ohci_bus_stop(OHCIState *ohci)
1202 if (ohci->eof_timer)
1203 qemu_del_timer(ohci->eof_timer);
1204 ohci->eof_timer = NULL;
1211 static int ohci_port_set_if_connected(OHCIState *ohci, int i, uint32_t val)
1222 if (!(ohci->rhport[i].ctrl & OHCI_PORT_CCS)) {
1223 ohci->rhport[i].ctrl |= OHCI_PORT_CSC;
1224 if (ohci->rhstatus & OHCI_RHS_DRWE) {
1230 if (ohci->rhport[i].ctrl & val)
1234 ohci->rhport[i].ctrl |= val;
1240 static void ohci_set_frame_interval(OHCIState *ohci, uint16_t val)
1244 if (val != ohci->fi) {
1245 dprintf("usb-ohci: %s: FrameInterval = 0x%x (%u)\n",
1246 ohci->name, ohci->fi, ohci->fi);
1249 ohci->fi = val;
1252 static void ohci_port_power(OHCIState *ohci, int i, int p)
1255 ohci->rhport[i].ctrl |= OHCI_PORT_PPS;
1257 ohci->rhport[i].ctrl &= ~(OHCI_PORT_PPS|
1265 static void ohci_set_ctl(OHCIState *ohci, uint32_t val)
1270 old_state = ohci->ctl & OHCI_CTL_HCFS;
1271 ohci->ctl = val;
1272 new_state = ohci->ctl & OHCI_CTL_HCFS;
1280 ohci_bus_start(ohci);
1283 ohci_bus_stop(ohci);
1284 dprintf("usb-ohci: %s: USB Suspended\n", ohci->name);
1287 dprintf("usb-ohci: %s: USB Resume\n", ohci->name);
1290 ohci_reset(ohci);
1291 dprintf("usb-ohci: %s: USB Reset\n", ohci->name);
1296 static uint32_t ohci_get_frame_remaining(OHCIState *ohci)
1301 if ((ohci->ctl & OHCI_CTL_HCFS) != OHCI_USB_OPERATIONAL)
1302 return (ohci->frt << 31);
1307 tks = qemu_get_clock_ns(vm_clock) - ohci->sof_time;
1311 return (ohci->frt << 31);
1314 fr = (uint16_t)(ohci->fi - tks);
1316 return (ohci->frt << 31) | fr;
1321 static void ohci_set_hub_status(OHCIState *ohci, uint32_t val)
1325 old_state = ohci->rhstatus;
1329 ohci->rhstatus &= ~OHCI_RHS_OCIC;
1334 for (i = 0; i < ohci->num_ports; i++)
1335 ohci_port_power(ohci, i, 0);
1336 dprintf("usb-ohci: powered down all ports\n");
1342 for (i = 0; i < ohci->num_ports; i++)
1343 ohci_port_power(ohci, i, 1);
1344 dprintf("usb-ohci: powered up all ports\n");
1348 ohci->rhstatus |= OHCI_RHS_DRWE;
1351 ohci->rhstatus &= ~OHCI_RHS_DRWE;
1353 if (old_state != ohci->rhstatus)
1354 ohci_set_interrupt(ohci, OHCI_INTR_RHSC);
1358 static void ohci_port_set_status(OHCIState *ohci, int portnum, uint32_t val)
1363 port = &ohci->rhport[portnum];
1373 ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PES);
1375 if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PSS))
1376 dprintf("usb-ohci: port %d: SUSPEND\n", portnum);
1378 if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PRS)) {
1379 dprintf("usb-ohci: port %d: RESET\n", portnum);
1390 ohci_port_power(ohci, portnum, 0);
1392 ohci_port_power(ohci, portnum, 1);
1395 ohci_set_interrupt(ohci, OHCI_INTR_RHSC);
1402 OHCIState *ohci = ptr;
1405 /* Only aligned reads are allowed on OHCI */
1407 fprintf(stderr, "usb-ohci: Mis-aligned read\n");
1409 } else if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
1411 retval = ohci->rhport[(addr - 0x54) >> 2].ctrl | OHCI_PORT_PPS;
1419 retval = ohci->ctl;
1423 retval = ohci->status;
1427 retval = ohci->intr_status;
1432 retval = ohci->intr;
1436 retval = ohci->hcca;
1440 retval = ohci->per_cur;
1444 retval = ohci->ctrl_head;
1448 retval = ohci->ctrl_cur;
1452 retval = ohci->bulk_head;
1456 retval = ohci->bulk_cur;
1460 retval = ohci->done;
1464 retval = (ohci->fit << 31) | (ohci->fsmps << 16) | (ohci->fi);
1468 retval = ohci_get_frame_remaining(ohci);
1472 retval = ohci->frame_number;
1476 retval = ohci->pstart;
1480 retval = ohci->lst;
1484 retval = ohci->rhdesc_a;
1488 retval = ohci->rhdesc_b;
1492 retval = ohci->rhstatus;
1497 retval = ohci->hstatus & ohci->hmask;
1501 retval = ohci->hreset;
1505 retval = ohci->hmask;
1509 retval = ohci->htest;
1526 OHCIState *ohci = ptr;
1532 /* Only aligned reads are allowed on OHCI */
1534 fprintf(stderr, "usb-ohci: Mis-aligned write\n");
1538 if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
1540 ohci_port_set_status(ohci, (addr - 0x54) >> 2, val);
1546 ohci_set_ctl(ohci, val);
1554 ohci->status |= val;
1556 if (ohci->status & OHCI_STATUS_HCR)
1557 ohci_reset(ohci);
1561 ohci->intr_status &= ~val;
1562 ohci_intr_update(ohci);
1566 ohci->intr |= val;
1567 ohci_intr_update(ohci);
1571 ohci->intr &= ~val;
1572 ohci_intr_update(ohci);
1576 ohci->hcca = val & OHCI_HCCA_MASK;
1580 ohci->ctrl_head = val & OHCI_EDPTR_MASK;
1584 ohci->ctrl_cur = val & OHCI_EDPTR_MASK;
1588 ohci->bulk_head = val & OHCI_EDPTR_MASK;
1592 ohci->bulk_cur = val & OHCI_EDPTR_MASK;
1596 ohci->fsmps = (val & OHCI_FMI_FSMPS) >> 16;
1597 ohci->fit = (val & OHCI_FMI_FIT) >> 31;
1598 ohci_set_frame_interval(ohci, val);
1605 ohci->pstart = val & 0xffff;
1609 ohci->lst = val & 0xffff;
1613 ohci->rhdesc_a &= ~OHCI_RHA_RW_MASK;
1614 ohci->rhdesc_a |= val & OHCI_RHA_RW_MASK;
1621 ohci_set_hub_status(ohci, val);
1626 ohci->hstatus &= ~(val & ohci->hmask);
1629 ohci->hreset = val & ~OHCI_HRESET_FSBIR;
1631 ohci_reset(ohci);
1635 ohci->hmask = val;
1639 ohci->htest = val;
1648 /* Only dword reads are defined on OHCI register space */
1655 /* Only dword writes are defined on OHCI register space */
1662 static void usb_ohci_init(OHCIState *ohci, int num_ports, int devfn,
1680 dprintf("usb-ohci: usb_bit_time=%lli usb_frame_time=%lli\n",
1684 ohci->mem = cpu_register_io_memory(ohci_readfn, ohci_writefn, ohci);
1685 ohci->localmem_base = localmem_base;
1686 ohci->name = name;
1688 ohci->irq = irq;
1689 ohci->type = type;
1691 ohci->num_ports = num_ports;
1693 qemu_register_usb_port(&ohci->rhport[i].port, ohci, i, ohci_attach);
1696 ohci->async_td = 0;
1697 qemu_register_reset(ohci_reset, 0, ohci);
1698 ohci_reset(ohci);
1709 OHCIPCIState *ohci = (OHCIPCIState *)pci_dev;
1710 cpu_register_physical_memory(addr, size, ohci->state.mem);
1715 OHCIPCIState *ohci;
1717 ohci = (OHCIPCIState *)pci_register_device(bus, "OHCI USB", sizeof(*ohci),
1719 if (ohci == NULL) {
1720 fprintf(stderr, "usb-ohci: Failed to register PCI device\n");
1724 pci_config_set_vendor_id(ohci->pci_dev.config, PCI_VENDOR_ID_APPLE);
1725 pci_config_set_device_id(ohci->pci_dev.config,
1727 ohci->pci_dev.config[0x09] = 0x10; /* OHCI */
1728 pci_config_set_class(ohci->pci_dev.config, PCI_CLASS_SERIAL_USB);
1729 ohci->pci_dev.config[0x3d] = 0x01; /* interrupt pin 1 */
1731 usb_ohci_init(&ohci->state, num_ports, devfn, ohci->pci_dev.irq[0],
1732 OHCI_TYPE_PCI, ohci->pci_dev.name, 0);
1734 pci_register_bar((struct PCIDevice *)ohci, 0, 256,
1741 OHCIState *ohci = (OHCIState *)qemu_mallocz(sizeof(OHCIState));
1743 usb_ohci_init(ohci, num_ports, devfn, irq,
1744 OHCI_TYPE_PXA, "OHCI USB", 0);
1746 cpu_register_physical_memory(base, 0x1000, ohci->mem);
1752 OHCIState *ohci = (OHCIState *)qemu_mallocz(sizeof(OHCIState));
1754 usb_ohci_init(ohci, num_ports, devfn, irq,
1755 OHCI_TYPE_SM501, "OHCI USB", localmem_base);
1757 cpu_register_physical_memory(mmio_base, 0x1000, ohci->mem);