Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,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;
Expand Down
10 changes: 10 additions & 0 deletions src/uartproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "uartproxy.h"
#include "assert.h"
#include "dockchannel_uart.h"
#include "exception.h"
#include "iodev.h"
#include "proxy.h"
Expand Down Expand Up @@ -96,6 +97,15 @@ enum stage1_proxy_result uartproxy_wait_stage1(unsigned int timeout_ms)
bool schedule_failed = false;
if (chip_id == T8140) {
schedule_failed = usb_cdc_schedule(1000, 0, BIT(1) | BIT(2));
#ifdef DEBUGUSB_PROXY
} else if (usb_cdc_t6040_usb2() && dockchannel_uart_host_seen()) {
/*
* T6040 with the inherited DockChannel (KIS) carrier: that carrier is the window's
* transport. A generic gadget here would own USB0, and the explicit USB2 CDC handoff
* (usb_cdc_link_start()) refuses while it does. Window boots without that carrier keep
* the generic gadget. The carrier check exists in DEBUGUSB_PROXY builds only.
*/
#endif
} else {
usb_init();
usb_iodev_init();
Expand Down
197 changes: 192 additions & 5 deletions src/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,27 @@ static int usb_drd_adt_offset(u32 idx, char drd_path[sizeof(FMT_DRD_PATH)], int
return adt_path_offset_trace(adt, drd_path, adt_drd_path);
}

/* 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");
}

bool usb_cdc_t6040_usb2(void)
{
return usb_atc0_phy_is_t6040();
}

/* The T6050 ATC PHY has a different PIPE clock field. Never run the older automatic gadget
* initialization on it, including when the explicit setup is refused. The T6040 keeps the generic
* gadget and proxy on boots without the inherited debug carrier; only the explicit USB2 CDC handoff
* (usb_cdc_link_start()) selects usb_t6040_init(), by usb_cdc_t6040_usb2(). */
bool usb_cdc_deferred_usb2(void)
{
static int deferred = -1;
Expand Down Expand Up @@ -311,6 +330,166 @@ 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, &regs->phy, &regs->phy_size) < 0 ||
adt_get_reg(adt, drd_path, "reg", 0, &regs->dwc, &dwc_size) < 0 ||
adt_get_reg(adt, drd_path, "reg", 3, &regs->pipe, &regs->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(&regs)) {
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. The installed Stage 1 also
* leaves the released PIPE with the DWC3 still held in reset (aon 0, mux 0x22): a core in
* reset produces nothing, and its registers can't be read to prove it, so that state is
* taken as retired as it is. */
if (!(aon & PIPEHANDLER_AON_GEN_DWC3_RESET_N) && mux == 0x22) {
printf("CDC USB2: released PIPE with the DWC3 held in reset; already retired\n");
} else 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.
Expand Down Expand Up @@ -871,14 +1050,22 @@ int usb_cdc_link_start(void (*set_step)(unsigned step), bool force_swapped)
* its live controller or DART during the carrier transition. */
return !force_swapped && board_id == 0x24 && iodev_get_opaque(IODEV_USB0) ? 0 : -1;
}
if (chip_id == T8152 || usb_cdc_deferred_usb2()) {
bool t6040 = usb_cdc_t6040_usb2();
if (chip_id == T8152 || usb_cdc_deferred_usb2() || t6040) {
/* On the T6040 a live generic gadget owns USB0 when there was no debug carrier to keep:
* refuse the explicit handoff and leave that gadget running. */
if (force_swapped || iodev_get_opaque(IODEV_USB0))
return -1;
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 (t6040)
dwc = usb_t6040_init(set_step);
else if (!usb_cdc_deferred_usb2())
dwc = usb_t8152_init(set_step);
else
dwc = usb_t6050_init(set_step);
if (!dwc) {
free(device);
return -1;
Expand Down
1 change: 1 addition & 0 deletions src/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ bool usb_iodev_shutdown_except(iodev_id_t keep);
void usb_iodev_vuart_setup(iodev_id_t iodev);
int usb_cdc_link_start(void (*set_step)(unsigned step), bool force_swapped);
bool usb_cdc_deferred_usb2(void);
bool usb_cdc_t6040_usb2(void);

dwc3_dev_t *usb_t8152_init(void (*set_step)(unsigned step));

Expand Down
20 changes: 16 additions & 4 deletions src/usb_cdc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -18,7 +19,8 @@ static bool carrier_owned;

static bool usb2_target(void)
{
return ((chip_id == T8142 || chip_id == T8152) && board_id == 0x24) || usb_cdc_deferred_usb2();
return ((chip_id == T8142 || chip_id == T8152) && board_id == 0x24) ||
usb_cdc_deferred_usb2() || usb_cdc_t6040_usb2();
}

static bool cdc_usb2_hpm(char *path, void *unused)
Expand Down Expand Up @@ -66,9 +68,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;
Expand Down Expand Up @@ -98,7 +103,11 @@ void usb_cdc_poll(void)
/* Consume once before touching hardware. Never retry failed setup. */
cdc.state = USB_CDC_FAILED;
cdc.step = 1;
if (usb_cdc_link_start(cdc_set_step, false))
int started = usb_cdc_link_start(cdc_set_step, false);
/* usb_dwc3_init() has consumed the speed request, or the start was refused (USB0
* already owned) and it never ran: either way it must not reach a later init. */
usb_dwc3_usb2_full_speed = false;
if (started)
goto usb2_failed;
cdc.step = 5;
if (tps6598x_foreach_hpm(cdc_usb2_hpm, cdc_usb2_route, NULL) != HPM_ACTION_STOP)
Expand Down Expand Up @@ -169,6 +178,8 @@ int usb_cdc_cancel(void)
if (carrier_owned || cdc.state != USB_CDC_SCHEDULED)
return -1;
cdc.state = USB_CDC_IDLE;
/* The full-speed request belongs to this session only. */
usb_dwc3_usb2_full_speed = false;
if (watchdog_armed) {
wdt_disable();
watchdog_armed = false;
Expand All @@ -182,6 +193,7 @@ void usb_cdc_cleanup(void)
/* USB is down: a transition still scheduled must not run from a later usb_cdc_poll(). */
if (cdc.state == USB_CDC_SCHEDULED)
cdc.state = USB_CDC_IDLE;
usb_dwc3_usb2_full_speed = false;
if (watchdog_armed) {
wdt_disable();
watchdog_armed = false;
Expand Down
3 changes: 3 additions & 0 deletions src/usb_cdc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/usb_dwc3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/usb_dwc3.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading
Loading