diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md new file mode 100644 index 000000000..95c6d4574 --- /dev/null +++ b/.github/copilot-instructions.md @@ -0,0 +1,28 @@ +# Aurora Silicon assistant policy + +This is Aurora Silicon's own policy for AI coding assistants working in +this repository. It is the only assistant instruction file this tree +carries. Upstream projects' instruction files are never carried here. + +AGENTS.md, CLAUDE.md, GEMINI.md and .github/copilot-instructions.md are +copies of .github/aurora/AGENTS.md. The aurora-agent-policy workflow +puts them back if anything else replaces them, including an upstream +merge. To change the policy, edit .github/aurora/AGENTS.md. + +## Rules + +- Only public code goes in. Never copy code from proprietary sources. +- Changes are tested on the hardware they affect before they are merged. + +## Testing and collaboration + +- Use the Context Mode plugin whenever it can reduce low-signal output. +- Fan independent work out to isolated agents and preserve unrelated worktrees. +- Never write unit tests after you write code. +- Highly prefer E2E tests as the sole testing mechanism. Use them to verify + complex features work. At the end of E2E tests, produce a verifiable and + repeatable artifact. +- If you must test a system in isolation, first write down all the ways it + could fail, then write the code. +- Delete low-signal unit tests that would not catch a real bug missed by E2E + tests. Preserve tests that provide distinct failure coverage. diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 000000000..95c6d4574 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,28 @@ +# Aurora Silicon assistant policy + +This is Aurora Silicon's own policy for AI coding assistants working in +this repository. It is the only assistant instruction file this tree +carries. Upstream projects' instruction files are never carried here. + +AGENTS.md, CLAUDE.md, GEMINI.md and .github/copilot-instructions.md are +copies of .github/aurora/AGENTS.md. The aurora-agent-policy workflow +puts them back if anything else replaces them, including an upstream +merge. To change the policy, edit .github/aurora/AGENTS.md. + +## Rules + +- Only public code goes in. Never copy code from proprietary sources. +- Changes are tested on the hardware they affect before they are merged. + +## Testing and collaboration + +- Use the Context Mode plugin whenever it can reduce low-signal output. +- Fan independent work out to isolated agents and preserve unrelated worktrees. +- Never write unit tests after you write code. +- Highly prefer E2E tests as the sole testing mechanism. Use them to verify + complex features work. At the end of E2E tests, produce a verifiable and + repeatable artifact. +- If you must test a system in isolation, first write down all the ways it + could fail, then write the code. +- Delete low-signal unit tests that would not catch a real bug missed by E2E + tests. Preserve tests that provide distinct failure coverage. diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 000000000..95c6d4574 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,28 @@ +# Aurora Silicon assistant policy + +This is Aurora Silicon's own policy for AI coding assistants working in +this repository. It is the only assistant instruction file this tree +carries. Upstream projects' instruction files are never carried here. + +AGENTS.md, CLAUDE.md, GEMINI.md and .github/copilot-instructions.md are +copies of .github/aurora/AGENTS.md. The aurora-agent-policy workflow +puts them back if anything else replaces them, including an upstream +merge. To change the policy, edit .github/aurora/AGENTS.md. + +## Rules + +- Only public code goes in. Never copy code from proprietary sources. +- Changes are tested on the hardware they affect before they are merged. + +## Testing and collaboration + +- Use the Context Mode plugin whenever it can reduce low-signal output. +- Fan independent work out to isolated agents and preserve unrelated worktrees. +- Never write unit tests after you write code. +- Highly prefer E2E tests as the sole testing mechanism. Use them to verify + complex features work. At the end of E2E tests, produce a verifiable and + repeatable artifact. +- If you must test a system in isolation, first write down all the ways it + could fail, then write the code. +- Delete low-signal unit tests that would not catch a real bug missed by E2E + tests. Preserve tests that provide distinct failure coverage. diff --git a/GEMINI.md b/GEMINI.md new file mode 100644 index 000000000..95c6d4574 --- /dev/null +++ b/GEMINI.md @@ -0,0 +1,28 @@ +# Aurora Silicon assistant policy + +This is Aurora Silicon's own policy for AI coding assistants working in +this repository. It is the only assistant instruction file this tree +carries. Upstream projects' instruction files are never carried here. + +AGENTS.md, CLAUDE.md, GEMINI.md and .github/copilot-instructions.md are +copies of .github/aurora/AGENTS.md. The aurora-agent-policy workflow +puts them back if anything else replaces them, including an upstream +merge. To change the policy, edit .github/aurora/AGENTS.md. + +## Rules + +- Only public code goes in. Never copy code from proprietary sources. +- Changes are tested on the hardware they affect before they are merged. + +## Testing and collaboration + +- Use the Context Mode plugin whenever it can reduce low-signal output. +- Fan independent work out to isolated agents and preserve unrelated worktrees. +- Never write unit tests after you write code. +- Highly prefer E2E tests as the sole testing mechanism. Use them to verify + complex features work. At the end of E2E tests, produce a verifiable and + repeatable artifact. +- If you must test a system in isolation, first write down all the ways it + could fail, then write the code. +- Delete low-signal unit tests that would not catch a real bug missed by E2E + tests. Preserve tests that provide distinct failure coverage. diff --git a/src/proxy.c b/src/proxy.c index f924dffa9..50fd9e2f0 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -504,7 +504,7 @@ int proxy_process(ProxyRequest *request, ProxyReply *reply) break; case P_CDC_SCHEDULE: - if (request->args[0] > 15000 || request->args[1] || request->args[2] > 7 || + if (request->args[0] > 15000 || request->args[1] || request->args[2] > 0xf || usb_cdc_schedule(request->args[0], request->args[1], request->args[2])) reply->status = S_ERROR; break; diff --git a/src/usb.c b/src/usb.c index dc4132668..4caa8d5b7 100644 --- a/src/usb.c +++ b/src/usb.c @@ -131,8 +131,20 @@ static int usb_drd_adt_offset(u32 idx, char drd_path[sizeof(FMT_DRD_PATH)], int return offset; } -/* T6050 has a different PIPE clock field. Never run the older automatic - * gadget initialization on it, including when the explicit setup is refused. */ +/* ATC0 is the tether/debug port. Select the T6040 explicit USB2 handoff by the port's PHY IP + * revision in the ADT, not by board: any machine whose atc-phy0 is atc-phy,t6040 gets it (proven + * on the J616s, the 16-inch M4 Pro MacBook Pro), and usb_t6040_get_regs() still refuses a port + * whose DRD, DART or register layout does not match. */ +static bool usb_atc0_phy_is_t6040(void) +{ + if (!adt) + return false; + int node = adt_path_offset(adt, "/arm-io/atc-phy0"); + return node >= 0 && adt_is_compatible(adt, node, "atc-phy,t6040"); +} + +/* The T6050 and T6040 ATC PHYs have a different PIPE clock field. Never run the older automatic + * gadget initialization on them, including when the explicit setup is refused. */ bool usb_cdc_deferred_usb2(void) { static int deferred = -1; @@ -145,8 +157,9 @@ bool usb_cdc_deferred_usb2(void) * The J714c (M5 Max) enumerates through the generic gadget path on every port, as the * omacom m1n1 Stage 1 does there; the explicit port-0 setup never comes up on it. */ - deferred = node >= 0 && adt_is_compatible(adt, node, "atc-phy,t6050") && - !adt_is_compatible(adt, 0, "J714cAP"); + deferred = node >= 0 && ((adt_is_compatible(adt, node, "atc-phy,t6050") && + !adt_is_compatible(adt, 0, "J714cAP")) || + adt_is_compatible(adt, node, "atc-phy,t6040")); } return deferred; } @@ -301,6 +314,161 @@ static dwc3_dev_t *usb_t6050_init(void (*set_step)(unsigned step)) return dwc; } +/* The T6040 ATC0 tether/debug port. ATC0 USB-drd0/dart-usb0/atc-phy0 are the same roles as the + * M5's, so this mirrors usb_t6050_init: resolve the DWC3 core (usb-drd0 reg 0), the PIPEHANDLER + * (usb-drd0 reg 3), the USB2 PHY (atc-phy0 reg 0) and the T8110 DART (dart-usb0 reg 1, mapper SID) + * from the ADT, validate by compatible and size, then park the dummy PIPE and bring up the USB2 + * device PHY from the ADT's own tunable_USB2PHY_DFLT/DEV. The qualified high-speed path + * (docs/j616s-bringup.md: 480 Mb/s + an exact 64 KiB transfer) leaves the ATC PHY in its initial + * state and needs neither the event-counter/"digital" bank the M5 writes (the generic bring-up that + * proved 480 Mb/s never touched atc-phy0 reg 1) nor the ACIO SuperSpeed PHY processor. SuperSpeed + * stays unqualified: it needs the CIO80 ACIOPHY processor (acio-phy-cpu0) brought to firmware + * tunables-done, which is not done yet; a USB2-only or forced-FS link runs at full speed. */ +static int usb_t6040_get_regs(struct usb_t6050_regs *regs) +{ + int phy_path[8], drd_path[8], dart_path[8]; + int phy = adt_path_offset_trace(adt, "/arm-io/atc-phy0", phy_path); + int drd = adt_path_offset_trace(adt, "/arm-io/usb-drd0", drd_path); + int hpm = adt_path_offset(adt, "/arm-io/nub-spmi-a0/hpm0"); + int dart = adt_path_offset_trace(adt, "/arm-io/dart-usb0", dart_path); + int mapper = adt_path_offset(adt, "/arm-io/dart-usb0/mapper-usb0"); + u32 phy_port, drd_port, hpm_port, port_type, sid; + u64 dwc_size, dart_base, dart_size; + + regs->digital = 0; /* T6040 high speed needs no event-counter bank; see usb_t6040_init. */ + if (phy < 0 || drd < 0 || hpm < 0 || dart < 0 || mapper < 0 || + !adt_is_compatible(adt, phy, "atc-phy,t6040") || + !adt_is_compatible(adt, drd, "usb-drd,t6040") || + !adt_is_compatible(adt, dart, "dart,t8110") || + adt_get_property(adt, drd, "usb-device-disable") || + ADT_GETPROP(adt, phy, "port-number", &phy_port) < 0 || + ADT_GETPROP(adt, drd, "port-number", &drd_port) < 0 || + ADT_GETPROP(adt, hpm, "port-number", &hpm_port) < 0 || !phy_port || phy_port != drd_port || + phy_port != hpm_port || ADT_GETPROP(adt, phy, "port-type", &port_type) < 0 || + port_type != 2 || ADT_GETPROP(adt, mapper, "reg", &sid) < 0 || sid >= 256 || + adt_get_reg(adt, phy_path, "reg", 0, ®s->phy, ®s->phy_size) < 0 || + adt_get_reg(adt, drd_path, "reg", 0, ®s->dwc, &dwc_size) < 0 || + adt_get_reg(adt, drd_path, "reg", 3, ®s->pipe, ®s->pipe_size) < 0 || + adt_get_reg(adt, dart_path, "reg", 1, &dart_base, &dart_size) < 0 || + regs->phy_size < 0x20 || dwc_size < DWC3_DSTS + 4 || regs->pipe_size < 0x48 || + dart_size < 0x1800 || + tunables_validate_compact("/arm-io/atc-phy0", "tunable_USB2PHY_DFLT", regs->phy_size) || + tunables_validate_compact("/arm-io/atc-phy0", "tunable_USB2PHY_DEV", regs->phy_size)) + return -1; + + return 0; +} + +static dwc3_dev_t *usb_t6040_init(void (*set_step)(unsigned step)) +{ + struct usb_t6050_regs regs; + if (usb_t6040_get_regs(®s)) { + printf("CDC USB2: unsupported T6040 ADT layout\n"); + return NULL; + } + + set_step(2); + if (pmgr_adt_power_enable("/arm-io/atc-phy0") < 0 || + pmgr_adt_power_enable("/arm-io/dart-usb0") < 0 || + pmgr_adt_power_enable("/arm-io/usb-drd0") < 0) + return NULL; + + u64 pipe = regs.pipe; + u32 aon = read32(pipe + PIPEHANDLER_AON_GEN); + u32 mux = read32(pipe + PIPEHANDLER_MUX_CTRL) & 0x73; + if (dart_adt_locked("/arm-io/dart-usb0", 1)) { + printf("CDC USB2: T6040 USB DART is locked; leaving it alone\n"); + return NULL; + } + /* A chainload over the DockChannel/KIS proxy leaves usb-drd0 cold (mux 0, no DWC3 reset). An + * in-place raw chainload past a USB Stage 1 leaves its dummy PIPE released (0x22); accept only + * a fully stopped device, never a live or native PIPE producer. */ + if ((aon & PIPEHANDLER_AON_GEN_DWC3_RESET_N) || mux == 0x22) { + u32 gctl = read32(regs.dwc + DWC3_GCTL); + bool retired = (mux == 0x22 || mux == 0x42) && + (gctl & DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG)) == + DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_DEVICE) && + !(read32(regs.dwc + DWC3_DCTL) & DWC3_DCTL_RUN_STOP) && + (read32(regs.dwc + DWC3_DSTS) & DWC3_DSTS_DEVCTRLHLT) && + !read32(regs.dwc + DWC3_DEVTEN) && !read32(regs.dwc + DWC3_DALEPENA); + if (!retired) { + printf("CDC USB2: T6040 PIPE consumer is not retired (aon=%x mux=%x)\n", aon, mux); + return NULL; + } + set32(pipe + PIPEHANDLER_AON_GEN, PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN); + clear32(pipe + PIPEHANDLER_AON_GEN, PIPEHANDLER_AON_GEN_DWC3_RESET_N); + if (poll32(pipe + PIPEHANDLER_AON_GEN, PIPEHANDLER_AON_GEN_DWC3_RESET_N, 0, 1000)) + return NULL; + udelay(10); + printf("CDC USB2: retired Stage 1 gadget held in reset\n"); + } else if (mux != 0 && mux != 2 && mux != 0x42) { + printf("CDC USB2: unknown T6040 PIPE producer (aon=%x mux=%x)\n", aon, mux); + return NULL; + } + set32(pipe + PIPEHANDLER_AON_GEN, PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN); + mask32(pipe + PIPEHANDLER_MUX_CTRL, 0x70, 0); + udelay(10); + mask32(pipe + PIPEHANDLER_MUX_CTRL, 3, 2); + udelay(10); + /* T6040's MUX_CTRL has no bit 6: the M5's 0x42 reads back 0x02 here (J616s hardware row, + * 2026-11-19). The dummy USB2 route is 0x22, the value the generic port bring-up writes. */ + mask32(pipe + PIPEHANDLER_MUX_CTRL, 0x70, PIPEHANDLER_MUX_CTRL_DUMMY & 0x70); + udelay(10); + if ((read32(pipe + PIPEHANDLER_MUX_CTRL) & 0x73) != PIPEHANDLER_MUX_CTRL_DUMMY) { + printf("CDC USB2: T6040 PIPE mux did not latch (mux=%x)\n", + read32(pipe + PIPEHANDLER_MUX_CTRL)); + return NULL; + } + + set_step(3); + if (tunables_apply_compact_addr("/arm-io/atc-phy0", "tunable_USB2PHY_DFLT", regs.phy, + regs.phy_size) || + tunables_apply_compact_addr("/arm-io/atc-phy0", "tunable_USB2PHY_DEV", regs.phy, + regs.phy_size)) + return NULL; + /* The J616s tunables leave the PHY in host mode (SIG read 0x01c0700f after them on hardware, + * 2026-11-19): no device pull-up, so the host never sees a connect. Device mode clears it. */ + clear32(regs.phy + USB2PHY_SIG, USB2PHY_SIG_HOST); + set32(regs.phy + USB2PHY_SIG, USB2PHY_SIG_VBUSDET_FORCE_VAL | USB2PHY_SIG_VBUSDET_FORCE_EN | + USB2PHY_SIG_VBUSVLDEXT_FORCE_VAL | + USB2PHY_SIG_VBUSVLDEXT_FORCE_EN); + udelay(10000); + clear32(regs.phy + USB2PHY_CTL, USB2PHY_CTL_SIDDQ); + udelay(10); + clear32(regs.phy + USB2PHY_CTL, USB2PHY_CTL_RESET | USB2PHY_CTL_PORT_RESET); + /* No USB2 event-counter/"digital" write: the generic bring-up that qualified 480 Mb/s on the + * J616s never touched atc-phy0 reg 1 (the M5 does this on its separate digital bank). */ + set32(regs.phy + USB2PHY_CTL, USB2PHY_CTL_APB_RESET_N); + clear32(regs.phy + USB2PHY_MISCTUNE, + USB2PHY_MISCTUNE_APBCLK_GATE_OFF | USB2PHY_MISCTUNE_REFCLK_GATE_OFF); + udelay(30); + udelay(5000); + mask32(regs.phy + USB2PHY_USBCTL, USB2PHY_USBCTL_USB_MODE, USB2PHY_USBCTL_RUN); + + set32(pipe + PIPEHANDLER_NONSELECTED_OVERRIDE, PIPEHANDLER_DUMMY_PHY_EN); + clear32(pipe + PIPEHANDLER_AON_GEN, PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN); + set32(pipe + PIPEHANDLER_AON_GEN, PIPEHANDLER_AON_GEN_DWC3_RESET_N); + if (poll32(pipe + PIPEHANDLER_AON_GEN, + PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN | PIPEHANDLER_AON_GEN_DWC3_RESET_N, + PIPEHANDLER_AON_GEN_DWC3_RESET_N, 1000) || + (read32(regs.phy + USB2PHY_USBCTL) & USB2PHY_USBCTL_USB_MODE) != USB2PHY_USBCTL_RUN || + (read32(regs.phy + USB2PHY_CTL) & 0xf) != USB2PHY_CTL_APB_RESET_N) { + printf("CDC USB2: T6040 reset/clock setup did not latch\n"); + return NULL; + } + + set_step(4); + dart_dev_t *dart = usb_dart_init(0); /* ADT mapper SID, t8110 on T6040 */ + if (!dart) + return NULL; + dwc3_dev_t *dwc = usb_dwc3_init(regs.dwc, dart); + if (!dwc) + return NULL; + printf("CDC USB2: T6040 controller initialized, PIPE=%x; link unverified\n", + read32(pipe + PIPEHANDLER_MUX_CTRL)); + return dwc; +} + /* * The ADT marks ports that must never run as a USB device with usb-device-disable, for * example the J773 front ports, whose hub prohibits device mode. Leave those alone. @@ -707,8 +875,13 @@ int usb_cdc_link_start(void (*set_step)(unsigned step), bool force_swapped) struct iodev *device = memalign(SPINLOCK_ALIGN, sizeof(*device)); if (!device) return -1; - dwc3_dev_t *dwc = - usb_cdc_deferred_usb2() ? usb_t6050_init(set_step) : usb_t8152_init(set_step); + dwc3_dev_t *dwc; + if (!usb_cdc_deferred_usb2()) + dwc = usb_t8152_init(set_step); + else if (usb_atc0_phy_is_t6040()) + dwc = usb_t6040_init(set_step); + else + dwc = usb_t6050_init(set_step); if (!dwc) { free(device); return -1; diff --git a/src/usb_cdc.c b/src/usb_cdc.c index 849475ab5..545aef471 100644 --- a/src/usb_cdc.c +++ b/src/usb_cdc.c @@ -8,6 +8,7 @@ #include "usb.h" #include "usb_cdc.h" #include "usb_cdc_state.h" +#include "usb_dwc3.h" #include "utils.h" #include "wdt.h" @@ -66,9 +67,12 @@ static void cdc_set_step(unsigned step) int usb_cdc_schedule(u32 delay_ms, u32 reserved, u32 flags) { if (usb2_target()) { - /* Explicit USB2 transition only: no HPM reset, lane swap or watchdog. */ - if (flags || reserved || cdc.state != USB_CDC_IDLE || delay_ms < 1000 || delay_ms > 15000) + /* Explicit USB2 transition only: no HPM reset, lane swap or watchdog. The one + * accepted flag advertises full speed (12 Mb/s) instead of high speed. */ + if ((flags & ~USB_CDC_USB2_FULL_SPEED) || reserved || cdc.state != USB_CDC_IDLE || + delay_ms < 1000 || delay_ms > 15000) return -1; + usb_dwc3_usb2_full_speed = !!(flags & USB_CDC_USB2_FULL_SPEED); cdc.deadline_ms = ticks_to_msecs(get_ticks()) + delay_ms; cdc.state = USB_CDC_SCHEDULED; return 0; diff --git a/src/usb_cdc.h b/src/usb_cdc.h index be42a9cc1..08edd2f15 100644 --- a/src/usb_cdc.h +++ b/src/usb_cdc.h @@ -4,6 +4,9 @@ #include "types.h" +/* USB2 targets (usb2_target()): the only flag they accept. T8140 uses bits 0-2. */ +#define USB_CDC_USB2_FULL_SPEED BIT(3) + int usb_cdc_schedule(u32 delay_ms, u32 reserved, u32 flags); u32 usb_cdc_status(void); void usb_cdc_poll(void); diff --git a/src/usb_dwc3.c b/src/usb_dwc3.c index 0821d596a..78142d0e9 100644 --- a/src/usb_dwc3.c +++ b/src/usb_dwc3.c @@ -3,6 +3,8 @@ #include "usb_dwc3.h" #include "malloc.h" +bool usb_dwc3_usb2_full_speed; + typedef struct dwc3_hs_dev dwc3_hs_dev_t; dwc3_hs_dev_t *usb_dwc3_hs_init(uintptr_t regs, dart_dev_t *dart); bool usb_dwc3_hs_shutdown(dwc3_hs_dev_t *dev); diff --git a/src/usb_dwc3.h b/src/usb_dwc3.h index 9eb196503..a69780bcb 100644 --- a/src/usb_dwc3.h +++ b/src/usb_dwc3.h @@ -15,6 +15,12 @@ typedef enum _cdc_acm_pipe_id_t { } cdc_acm_pipe_id_t; dwc3_dev_t *usb_dwc3_init(uintptr_t regs, dart_dev_t *dart); +/* + * USB2 (high-speed engine) only: advertise full speed instead of high speed on the next + * usb_dwc3_init(). Hosts or hubs limited to 12 Mb/s get the same CDC device either way; the + * bulk endpoints follow the speed the host actually connected at. + */ +extern bool usb_dwc3_usb2_full_speed; dwc3_dev_t *usb_dwc3_init_mode(uintptr_t regs, dart_dev_t *dart, bool superspeed); /* False when the controller may still reach its DMA buffers; they are then retained. */ bool usb_dwc3_shutdown(dwc3_dev_t *dev); diff --git a/src/usb_dwc3_impl.inc b/src/usb_dwc3_impl.inc index 8f0f0dd53..1265fcd68 100644 --- a/src/usb_dwc3_impl.inc +++ b/src/usb_dwc3_impl.inc @@ -49,6 +49,7 @@ #define DWC3_EP0_MAX_DESC_LEN 62 #define CDC_BULK_PACKET_SIZE 512 #define CDC_CONTROL_PACKET_SIZE 64 +#define CDC_FS_BULK_PACKET_SIZE 64 #endif #define CDC_DEVICE_CLASS 0x02 @@ -134,6 +135,10 @@ typedef struct dwc3_dev { bool dma_unsafe; /* usb_dwc3_shutdown() is retiring transfers: only their completions are handled */ bool shutting_down; +#if !USB_DWC3_SUPERSPEED + /* Bulk max packet size for the connected speed: 512 at high speed, 64 at full speed. */ + u16 bulk_mps; +#endif #if USB_DWC3_SUPERSPEED bool reset_in_progress; bool ep0_progressed_during_reset; @@ -515,6 +520,20 @@ static int usb_dwc3_retire_transfer(dwc3_dev_t *dev, u8 ep) return 0; } +#if !USB_DWC3_SUPERSPEED +/* DEPCFG "modify" action: change an initialized, not yet enabled bulk endpoint's packet size. */ +#define DWC3_DEPCFG_ACTION_MODIFY (2 << 30) +static int usb_dwc3_ep_reconfigure_mps(dwc3_dev_t *dev, u8 ep, u32 max_packet_len) +{ + u32 param0 = DWC3_DEPCFG_EP_TYPE(DWC3_DEPCMD_TYPE_BULK) | + DWC3_DEPCFG_MAX_PACKET_SIZE(max_packet_len) | DWC3_DEPCFG_FIFO_NUMBER(ep) | + DWC3_DEPCFG_ACTION_MODIFY; + u32 param1 = + DWC3_DEPCFG_XFER_COMPLETE_EN | DWC3_DEPCFG_XFER_NOT_READY_EN | DWC3_DEPCFG_EP_NUMBER(ep); + return usb_dwc3_ep_command(dev, ep, DWC3_DEPCMD_SETEPCONFIG, param0, param1, 0); +} +#endif + static int usb_dwc3_ep_configure(dwc3_dev_t *dev, u8 ep, u8 type, u32 max_packet_len) { u32 param0, param1; @@ -813,6 +832,29 @@ static void usb_cdc_get_string_descriptor(u32 index, const void **descriptor, u1 *descriptor_len = min(*descriptor_len, DWC3_EP0_MAX_DESC_LEN); } +#if !USB_DWC3_SUPERSPEED +/* The configuration descriptor with bulk packet sizes for the connected speed. */ +static const void *usb_dwc3_cdc_config_for(dwc3_dev_t *dev) +{ + static u8 fs[sizeof(struct cdc_dev_desc)] ALIGNED(4); + static const size_t bulk[] = { + offsetof(struct cdc_dev_desc, endpoint_data_in), + offsetof(struct cdc_dev_desc, endpoint_data_out), + offsetof(struct cdc_dev_desc, sec_endpoint_data_in), + offsetof(struct cdc_dev_desc, sec_endpoint_data_out), + }; + u16 mps = dev->bulk_mps; + + if (mps == CDC_BULK_PACKET_SIZE) + return &cdc_configuration_descriptor; + memcpy(fs, &cdc_configuration_descriptor, sizeof(fs)); + for (size_t i = 0; i < ARRAY_SIZE(bulk); i++) + memcpy(fs + bulk[i] + offsetof(struct usb_endpoint_descriptor, wMaxPacketSize), &mps, + sizeof(mps)); + return fs; +} +#endif + static int usb_dwc3_handle_ep0_get_descriptor(dwc3_dev_t *dev, const struct usb_setup_packet_get_descriptor *get_descriptor) @@ -835,7 +877,7 @@ usb_dwc3_handle_ep0_get_descriptor(dwc3_dev_t *dev, descriptor = usb_cdc_ss_configuration; descriptor_len = usb_cdc_ss_configuration_len; #else - descriptor = &cdc_configuration_descriptor; + descriptor = usb_dwc3_cdc_config_for(dev); descriptor_len = cdc_configuration_descriptor.configuration.wTotalLength; #endif break; @@ -1301,7 +1343,11 @@ static void usb_dwc3_cdc_start_bulk_in_xfer(dwc3_dev_t *dev, u8 endpoint_number) device2host->read = previous_read; return; } +#if USB_DWC3_SUPERSPEED dev->endpoints[endpoint_number].zlp_pending = len && (len % CDC_BULK_PACKET_SIZE) == 0; +#else + dev->endpoints[endpoint_number].zlp_pending = len && (len % dev->bulk_mps) == 0; +#endif } static bool usb_dwc3_cdc_copy_bulk_out_progress(dwc3_dev_t *dev, u8 ep) @@ -1609,11 +1655,27 @@ static void usb_dwc3_handle_event_connect_done(dwc3_dev_t *dev) return; } #else - if (speed != DWC3_DSTS_HIGHSPEED) { - usb_debug_printf( - "WARNING: we only support high speed right now but %02x was requested in DSTS\n", - speed); + bool full_speed = speed == DWC3_DSTS_FULLSPEED1 || speed == DWC3_DSTS_FULLSPEED2; + if (!full_speed && speed != DWC3_DSTS_HIGHSPEED) { + usb_debug_printf("WARNING: unsupported USB2 speed %02x in DSTS\n", speed); + } + u16 bulk_mps = full_speed ? CDC_FS_BULK_PACKET_SIZE : CDC_BULK_PACKET_SIZE; + if (bulk_mps != dev->bulk_mps) { + /* USB 2.0 9.6.6: full-speed bulk endpoints carry at most 64 bytes per packet. */ + for (int i = 0; i < CDC_ACM_PIPE_MAX; i++) { + if (usb_dwc3_ep_reconfigure_mps(dev, dev->pipe[i].ep_in, bulk_mps) || + usb_dwc3_ep_reconfigure_mps(dev, dev->pipe[i].ep_out, bulk_mps)) { + /* The descriptors would advertise a packet size the endpoints do not have: + * drop the link rather than run with mismatched bulk endpoints. */ + usb_debug_printf("cannot resize bulk endpoints for speed %02x\n", speed); + clear32(dev->regs + DWC3_DCTL, DWC3_DCTL_RUN_STOP); + usb_cdc_link_failed(); + return; + } + } + dev->bulk_mps = bulk_mps; } + usb_debug_printf("CDC USB2: connected at %s speed\n", full_speed ? "full" : "high"); #endif if (!dev->endpoints[USB_LEP_CTRL_OUT].xfer_in_progress && @@ -1936,7 +1998,9 @@ dwc3_dev_t *usb_dwc3_init(uintptr_t regs, dart_dev_t *dart) #if USB_DWC3_SUPERSPEED mask32(dev->regs + DWC3_DCFG, DWC3_DCFG_SPEED_MASK, DWC3_DCFG_SUPERSPEED); #else - mask32(dev->regs + DWC3_DCFG, DWC3_DCFG_SPEED_MASK, DWC3_DCFG_HIGHSPEED); + dev->bulk_mps = CDC_BULK_PACKET_SIZE; + mask32(dev->regs + DWC3_DCFG, DWC3_DCFG_SPEED_MASK, + usb_dwc3_usb2_full_speed ? DWC3_DCFG_FULLSPEED2 : DWC3_DCFG_HIGHSPEED); #endif /* setup scratchpad at SCRATCHPAD_IOVA */ diff --git a/tests/python/test_usb_hs_session.py b/tests/python/test_usb_hs_session.py index ff44dd92e..b379d10e0 100644 --- a/tests/python/test_usb_hs_session.py +++ b/tests/python/test_usb_hs_session.py @@ -37,7 +37,7 @@ def macro(source, name): #include #include #include -typedef uint8_t u8; typedef uint32_t u32; typedef uint64_t u64; +typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; #define USB_DWC3_SUPERSPEED 0 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #define MAX_ENDPOINTS 6 @@ -49,8 +49,14 @@ def macro(source, name): #define DWC3_DCFG 0 #define DWC3_DALEPENA 4 #define DWC3_DSTS 8 +#define DWC3_DCTL 12 +#define DWC3_DCTL_RUN_STOP (1U << 31) #define DWC3_DSTS_CONNECTSPD 7 #define DWC3_DSTS_HIGHSPEED 0 +#define DWC3_DSTS_FULLSPEED2 1 +#define DWC3_DSTS_FULLSPEED1 3 +#define CDC_BULK_PACKET_SIZE 512 +#define CDC_FS_BULK_PACKET_SIZE 64 #define DWC3_DCFG_DEVADDR_MASK 127 #define DWC3_DCFG_DEVADDR(x) (x) #define DWC3_DALEPENA_EP(x) (1U << (x)) @@ -70,7 +76,7 @@ def macro(source, name): typedef struct {size_t read, write;} ringbuffer_t; struct endpoint {bool xfer_in_progress, zlp_pending, stalled; int resource_index, trb_count, completed_trbs, transfer_length; unsigned char xfer_buffer[16]; struct dwc3_trb trb[2];}; -typedef struct {unsigned regs; bool failed; int ep0_state; struct endpoint endpoints[MAX_ENDPOINTS]; +typedef struct {unsigned regs; bool failed; int ep0_state; u16 bulk_mps; struct endpoint endpoints[MAX_ENDPOINTS]; struct {bool ready; ringbuffer_t *host2device, *device2host; u8 ep_in, ep_out;} pipe[2];} dwc3_dev_t; /* Controller model. */ @@ -82,7 +88,14 @@ def macro(source, name): static u64 now_ms; static void mask32(unsigned reg, unsigned mask, unsigned value) {(void)mask; if (reg == DWC3_DCFG) address = value;} static void write32(unsigned reg, unsigned value) {if (reg == DWC3_DALEPENA) enabled = value;} -static u32 read32(unsigned reg) {(void)reg; return DWC3_DSTS_HIGHSPEED;} +static u32 dsts_speed = DWC3_DSTS_HIGHSPEED; +static u32 read32(unsigned reg) {(void)reg; return dsts_speed;} +/* Bulk endpoint resizes (DEPCFG modify): count and last packet size. */ +static int resizes; static u32 resized_mps; +static int resize_fail, link_failures; static bool run_stop = true; +static int usb_dwc3_ep_reconfigure_mps(dwc3_dev_t *d, u8 ep, u32 mps) {(void)d; assert(ep == 4 || ep == 5 || ep == 0); if (resize_fail) return -1; resizes++; resized_mps = mps; return 0;} +static void clear32(unsigned reg, unsigned mask) {if (reg == DWC3_DCTL && (mask & DWC3_DCTL_RUN_STOP)) run_stop = false;} +static void usb_cdc_link_failed(void) {link_failures++;} static int usb_dwc3_end_transfer(dwc3_dev_t *d, int ep) {(void)d; assert(address == 0 && enabled == 3); hw_owned[ep] = false; ends++; return 0;} static void usb_dwc3_ep_set_stall(dwc3_dev_t *d, int ep, int value) {assert(!value); d->endpoints[ep].stalled = false;} static int usb_dwc3_start_setup_phase(dwc3_dev_t *d) { @@ -113,6 +126,7 @@ def macro(source, name): memset(&d, 0, sizeof(d)); memset(hw_owned, 0, sizeof(hw_owned)); ends = setup_starts = setup_failures = out_arms = in_kicks = events_polled = 0; address = 29; enabled = 63; now_ms = 0; close_after = -1; stuck_ms = 0; chip_id = T8122; + d.bulk_mps = CDC_BULK_PACKET_SIZE; dsts_speed = DWC3_DSTS_HIGHSPEED; resizes = 0; resized_mps = 0; for (int i = 0; i < 2; i++) { d.pipe[i].ready = true; d.pipe[i].host2device = &rb[2 * i]; d.pipe[i].device2host = &rb[2 * i + 1]; rb[2 * i] = (ringbuffer_t){0, 0}; rb[2 * i + 1] = (ringbuffer_t){0, 0}; @@ -174,7 +188,30 @@ def macro(source, name): setup(); assert(usb_dwc3_can_read(&d, 0) == 0 && out_arms == 1); d.pipe[0].ready = false; assert(usb_dwc3_can_read(&d, 0) == 0 && out_arms == 1); - puts("USB HS session: reset, disconnect and closed-pipe loops PASS"); + /* High speed keeps the 512-byte bulk endpoints untouched. */ + setup(); idle_with_setup_armed(); + dev_event(DWC3_DEVT_USBRST); dev_event(DWC3_DEVT_CONNECTDONE); + assert(d.bulk_mps == CDC_BULK_PACKET_SIZE && !resizes); + /* A full-speed host gets 64-byte bulk endpoints on every pipe; SETUP still re-arms. */ + for (u32 fs = DWC3_DSTS_FULLSPEED2; fs <= DWC3_DSTS_FULLSPEED1; fs += 2) { + setup(); idle_with_setup_armed(); dsts_speed = fs; + dev_event(DWC3_DEVT_USBRST); dev_event(DWC3_DEVT_CONNECTDONE); + assert(d.bulk_mps == CDC_FS_BULK_PACKET_SIZE && resizes == 2 * CDC_ACM_PIPE_MAX); + assert(resized_mps == CDC_FS_BULK_PACKET_SIZE && !setup_failures); + assert(setup_completion_seen(&d)); + /* A later high-speed connect restores 512. */ + dsts_speed = DWC3_DSTS_HIGHSPEED; + dev_event(DWC3_DEVT_USBRST); dev_event(DWC3_DEVT_CONNECTDONE); + assert(d.bulk_mps == CDC_BULK_PACKET_SIZE && resized_mps == CDC_BULK_PACKET_SIZE); + } + + /* A resize that fails drops the link instead of advertising sizes the endpoints lack. */ + setup(); idle_with_setup_armed(); dsts_speed = DWC3_DSTS_FULLSPEED1; resize_fail = 1; + dev_event(DWC3_DEVT_USBRST); dev_event(DWC3_DEVT_CONNECTDONE); + assert(link_failures == 1 && !run_stop && d.bulk_mps == CDC_BULK_PACKET_SIZE); + resize_fail = 0; run_stop = true; dsts_speed = DWC3_DSTS_HIGHSPEED; + + puts("USB HS session: reset, disconnect, closed-pipe, full-speed and resize-failure loops PASS"); return 0; } '''