From e47fdd390454f3612107966197c34bef72355f08 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Mon, 16 Nov 2026 05:39:14 -0500 Subject: [PATCH 1/2] phy: apple: atc-t6050: J714s: keep the dock port's USB host through a router's sleep macOS keeps the USB host on the USB4 dock's port through the router's sleep in place, and xHCI restores its state at the wake, so no device behind the dock is reset. Linux parked the host's PIPE on its dummy producer and stopped the USB2 PHY, and the controller lost its power with nothing kept: the xHCI restore failed (USBSTS save/restore error) and every dock device was reset at each wake. Do as macOS does on J714s: - At the PIPE's park for the router's sleep (xHCI has saved its state by then; macOS does this right after the save), take the controller into hibernation through its power control ("pwrctl", the controller's window beside the PIPE handler, as on T8140): D3 asked for, VDD_OFF awaited and cleared, the wake events armed, SLEEP in the status. Without the region, or if the hibernation fails, the PIPE is parked on its dummy producer as before. - Keep the USB3 PIPE on the tunnel producer with its clock gated instead of moving it to the dummy producer, and take the core's reset after the CIO shutdown. - Suspend the USB2 PHY instead of stopping it, and give it back before the PHY's restart at the wake. - At the host's resume, give the PIPE its clock back on the tunnel, disarm the wake events, ask for D0, release the core's reset and wait for D0_ON (about 1 ms). A new RETAINED step reports whether the controller's bank kept the sleep's state, for the glue to resume the host in place. A refused sleep, the reset's release and a failed hibernation ask for D0 too. A power control that reads all zeros after the sleep lost its state: the host starts over, as before. A controller that gives no D0_ON and still reports SLEEP may be unpowered: the PHY is marked faulted and the glue keeps away from its core. The region is described for the J714s display layout of the right port's PHY and mapped only there. Signed-off-by: Diljit Singh --- .../bindings/phy/apple,combo-phy.yaml | 10 +- .../boot/dts/apple/t6050-j714s-display.dtsi | 5 +- drivers/phy/apple/atc-t6050-combo.h | 53 +++- drivers/phy/apple/atc-t6050-dp.c | 230 +++++++++++++++++- include/linux/phy/apple-atc.h | 11 +- 5 files changed, 297 insertions(+), 12 deletions(-) diff --git a/Documentation/devicetree/bindings/phy/apple,combo-phy.yaml b/Documentation/devicetree/bindings/phy/apple,combo-phy.yaml index 90e5dbf812a0e..04ea9e814932c 100644 --- a/Documentation/devicetree/bindings/phy/apple,combo-phy.yaml +++ b/Documentation/devicetree/bindings/phy/apple,combo-phy.yaml @@ -24,11 +24,11 @@ properties: reg: minItems: 6 - maxItems: 12 + maxItems: 13 reg-names: minItems: 6 - maxItems: 12 + maxItems: 13 '#phy-cells': enum: [0, 1] @@ -180,7 +180,7 @@ allOf: const: 1 reg: minItems: 11 - maxItems: 12 + maxItems: 13 reg-names: minItems: 11 items: @@ -197,6 +197,10 @@ allOf: - const: pipehandler - description: Source select words for the four USB4 DP pixel clocks const: dp-clock-source + - description: + Power control of the port's USB controller, for its hibernation through a + USB4 router's sleep + const: pwrctl required: - '#reset-cells' - apple,port-index diff --git a/arch/arm64/boot/dts/apple/t6050-j714s-display.dtsi b/arch/arm64/boot/dts/apple/t6050-j714s-display.dtsi index f4bd8b98ea502..f726665a95b3f 100644 --- a/arch/arm64/boot/dts/apple/t6050-j714s-display.dtsi +++ b/arch/arm64/boot/dts/apple/t6050-j714s-display.dtsi @@ -266,10 +266,11 @@ <0x3 0x83140000 0x0 0x4000>, <0x3 0x830c0000 0x0 0x10000>, <0x3 0x82a84000 0x0 0x4000>, - <0x2 0x80000068 0x0 0x10>; + <0x2 0x80000068 0x0 0x10>, + <0x3 0x82a80000 0x0 0x4000>; reg-names = "core", "usb2phy", "usb2-digital", "reset", "fabric", "axi2af", "lioa", "common-cfg", "usb-control", "firmware-data", "pipehandler", - "dp-clock-source"; + "dp-clock-source", "pwrctl"; power-domains = <&ps_atc2_usb_aon>, <&ps_atc2_common>, <&ps_atc2_phymxwrap>, <&ps_atc2_usb>, <&ps_atc2_common_dp_0>, <&ps_atc2_common_dp_1>, <&ps_atc2_common_dp_2>, <&ps_atc2_common_dp_3>; diff --git a/drivers/phy/apple/atc-t6050-combo.h b/drivers/phy/apple/atc-t6050-combo.h index ea79f497b979b..a3a60c9d486b1 100644 --- a/drivers/phy/apple/atc-t6050-combo.h +++ b/drivers/phy/apple/atc-t6050-combo.h @@ -151,6 +151,8 @@ static int combo_stop(struct t6050_dp *dp, bool keep_aon) if (dp->controller_active || dp->pipe_active) return -EBUSY; ret = combo_quiesce(dp); + dp->usb2_suspended = false; + dp->pipe_gated = false; if (!ret && dp->usb2_active) { ret = apple_t6050_usb2_stop(&dp->usb_platform); if (!ret) @@ -196,8 +198,28 @@ static int combo_start(struct t6050_dp *dp, enum combo_mode mode) if (!ret && dp->ndomains > 3) ret = atc_domain_set(&dp->domain[3], true); dp->aon_kept = false; - if (!ret) + /* + * J714s: a USB2 PHY suspended through the router's sleep is given back as macOS does + * (+0x4 bit 2, then +0x1c bit 30 clear, before the PHY's restart) when it kept its + * registers; one that lost them (its suspend bit gone) is started again as before. + */ + if (!ret && dp->usb2_suspended && dp->cio_sleep_hold) { + u32 r1c = readl(dp->banks[0] + 0x1c), r4 = readl(dp->banks[0] + 0x4); + + dp->usb2_suspended = false; + if (r1c & BIT(30)) { + apple_t6050_usb_mask(&dp->usb_io, 0, 0x4, BIT(2), BIT(2)); + apple_t6050_usb_mask(&dp->usb_io, 0, 0x1c, BIT(30), 0); + dev_dbg(dp->dev, "USB2 PHY given back after the router's sleep: +0x4 %#x -> %#x, +0x1c %#x -> %#x\n", + r4, readl(dp->banks[0] + 0x4), r1c, readl(dp->banks[0] + 0x1c)); + } else { + dev_info(dp->dev, "USB2 PHY lost its registers in the sleep (+0x1c %#x, +0x4 %#x): started again\n", + r1c, r4); + ret = APPLE_T6050_USB_RUN(&dp->usb_platform, apple_t6050_usb2_host); + } + } else if (!ret) { ret = APPLE_T6050_USB_RUN(&dp->usb_platform, apple_t6050_usb2_host); + } if (!ret) dp->usb2_active = true; if (!ret && mode == COMBO_USB) { @@ -560,6 +582,8 @@ static int combo_usb3_poweroff(struct phy *phy) if (!ret) dp->pipe_active = false; } + dp->usb2_suspended = false; + dp->pipe_gated = false; if (!ret && dp->usb2_active) { ret = apple_t6050_usb2_stop(&dp->usb_platform); if (!ret) @@ -625,8 +649,21 @@ static int combo_reset_deassert(struct reset_controller_dev *reset, unsigned lon * The bit resets to clear with the controller's domain, so set it for every session. */ apple_t6050_usb_mask(&dp->usb_io, 44, 0x20, BIT(15), BIT(15)); - apple_t6050_usb_mask(&dp->usb_io, 44, 0x1c, 0x10, 0); - apple_t6050_usb_mask(&dp->usb_io, 44, 0x1c, 1, 1); + /* + * J714s: a PIPE left gated on the tunnel producer by the router's sleep (mux 0x01, kept by + * the controller's always-on domain) goes on the dummy producer first, as a park leaves it. + */ + if (atc_sleep_macos_usb() && dp->pipe_gated && (readl(dp->banks[44] + 0xc) & 0x73) == 0x01) { + apple_t6050_usb_mask(&dp->usb_io, 44, 0xc, 0x3, 0x2); + apple_t6050_usb_mask(&dp->usb_io, 44, 0xc, 0x70, 0x40); + apple_t6050_usb_mask(&dp->usb_io, 44, 0x0, 0x4, 0); + dev_info(dp->dev, "gated USB3 PIPE kept through the sleep put on its dummy producer at the controller's reset release: mux %#x\n", + readl(dp->banks[44] + 0xc)); + } + dp->pipe_gated = false; + /* Out of a sleep's hibernation if it is still asked for, then the reset released */ + if (t6050_usb_d0(dp, true) == -ENODEV) + return -EIO; dp->controller_active = true; return 0; } @@ -670,6 +707,16 @@ static int combo_setup(struct platform_device *pdev, struct t6050_dp *dp) return PTR_ERR(dp->banks[extra[i].bank]); dp->resources[extra[i].bank] = *res; } + /* J714s: the USB controller's power control, for its hibernation through a router's sleep */ + if (of_machine_is_compatible("apple,j714s")) { + struct resource *res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pwrctl"); + + if (res) { + dp->pwrctl = devm_ioremap_resource(dev, res); + if (IS_ERR(dp->pwrctl)) + return PTR_ERR(dp->pwrctl); + } + } dp->usb2_default = devm_apple_tunable_parse(dev, dev->of_node, "apple,tunable-usb2-default", &dp->resources[0]); if (IS_ERR(dp->usb2_default)) diff --git a/drivers/phy/apple/atc-t6050-dp.c b/drivers/phy/apple/atc-t6050-dp.c index dc6c0776d1faf..dbacea0d57f96 100644 --- a/drivers/phy/apple/atc-t6050-dp.c +++ b/drivers/phy/apple/atc-t6050-dp.c @@ -145,8 +145,27 @@ struct t6050_dp { bool cio_usb_host; /* a router host that sleeps in place is bound to this PHY */ bool cio_usb_kept; /* kept for this sleep, until its USB controller's RESULT */ bool cio_usb_parked; /* its PIPE parked behind the router's sleep or refusal */ + /* J714s: the host on the router's tunnel kept in place through the router's sleep */ + bool usb2_suspended; /* USB2 PHY suspended (+0x1c bit 30, +0x4 bit 2), not stopped */ + bool pipe_gated; /* PIPE kept on the tunnel producer with its clock off (mux 0x01) */ + void __iomem *pwrctl; /* the USB controller's power control, if described */ + bool usb_hibernated; /* D3 asked for at the PIPE's park: D0 is asked on every way back */ }; +/* + * J714s: how macOS keeps the dock port's USB host through its USB4 router's sleep. The USB2 + * PHY is not stopped: after the PHY's CIO shutdown usb2phy +0x1c bit 30 is set and +0x4 bit 2 + * cleared, and both are given back right after the wake, before the PHY's restart, with no port + * reset and no tunables. The USB3 PIPE stays on the tunnel producer with its clock gated + * (pipehandler +0x4 bits 2:1 set, +0x0 bit 2 set, +0x4 bit 4 cleared, +0xc 0x21 -> 0x01), the + * core's reset (+0x1c bit 0) is taken after the CIO shutdown, and at the host's resume +0xc goes + * back to 0x21, +0x1c bit 0 is set and +0x0 bit 2 cleared. + */ +static bool atc_sleep_macos_usb(void) +{ + return of_machine_is_compatible("apple,j714s"); +} + static int atc_domain_set(struct atc_domain *pd, bool active) { int ret; @@ -627,6 +646,118 @@ static void atc_dp_tunnels_stop(struct t6050_dp *dp) atc_domain_set(&dp->dp_gate[k - 1], false); } +/* + * J714s: the power control of the dock port's USB controller ("pwrctl", the controller's window + * beside its PIPE handler), as on T8140. macOS's sleep of that host: right after xHCI's save + * (CSS) the controller goes into hibernation (D3 asked for, VDD_OFF awaited and cleared, the wake + * events armed, SLEEP in the status), then the PIPE's clock is gated on the tunnel producer. At + * the wake the PIPE gets its clock back, the wake events are disarmed, D0 is asked for, the + * core's reset is released and D0_ON awaited (about 1 ms); only then are the globals written + * back and xHCI restores its state (CRS). Without the hibernation the core loses its power with + * nothing kept, and xHCI's restore fails with a save/restore error (USBSTS SRE), so every device + * behind the dock is reset at each wake. + */ +#define T6050_PWR_CFG 0x00 +#define T6050_PWR_CFG_STATE_REQ GENMASK(5, 4) +#define T6050_PWR_STATUS 0x04 +#define T6050_PWR_STATUS_SLEEP BIT(20) +#define T6050_PWR_EVENTS 0x08 +#define T6050_PWR_ENABLE 0x10 +#define T6050_PWR_CLEAR 0x14 +#define T6050_PWR_EVENT_MASK GENMASK(3, 0) +#define T6050_PWR_EVENT_D0_ON BIT(0) +#define T6050_PWR_EVENT_VDD_OFF BIT(1) +#define T6050_PWR_EVENT_WAKE GENMASK(3, 2) +#define T6050_PWR_TIMEOUT_US 200000 + +static void t6050_pwr_mask(struct t6050_dp *dp, u32 off, u32 mask, u32 val) +{ + writel((readl(dp->pwrctl + off) & ~mask) | val, dp->pwrctl + off); +} + +/* + * D0 asked for, the core's reset released (with @reset, the PIPE handler's +0x1c: clamp off, + * reset bit on, as the reset's release does) and D0_ON awaited. A power control that reads all + * zeros lost its state in the sleep: no D0_ON comes, and the core starts over (-ENODATA). A + * controller that gives no D0_ON and still reports SLEEP may be unpowered, where an access to + * its core would be an SError: the PHY is marked faulted, and its callers keep away from it. + * Without a hibernation pending, only the reset is released. + */ +static int t6050_usb_d0(struct t6050_dp *dp, bool reset) +{ + u32 status, events = 0; + int ret; + + lockdep_assert_held(&dp->lock); + if (!dp->pwrctl || !dp->usb_hibernated) { + if (reset) { + apple_t6050_usb_mask(&dp->usb_io, 44, 0x1c, 0x10, 0); + apple_t6050_usb_mask(&dp->usb_io, 44, 0x1c, 1, 1); + } + return 0; + } + dp->usb_hibernated = false; + status = readl(dp->pwrctl + T6050_PWR_STATUS); + t6050_pwr_mask(dp, T6050_PWR_ENABLE, T6050_PWR_EVENT_MASK, 0); + t6050_pwr_mask(dp, T6050_PWR_CFG, T6050_PWR_CFG_STATE_REQ, 0); + if (reset) { + apple_t6050_usb_mask(&dp->usb_io, 44, 0x1c, 0x10, 0); + apple_t6050_usb_mask(&dp->usb_io, 44, 0x1c, 1, 1); + } + if (!status) { + dev_info(dp->dev, "USB controller's power control lost its state in the sleep: the core starts over\n"); + return -ENODATA; + } + ret = readl_poll_timeout(dp->pwrctl + T6050_PWR_EVENTS, events, + events & T6050_PWR_EVENT_D0_ON, 100, T6050_PWR_TIMEOUT_US); + t6050_pwr_mask(dp, T6050_PWR_CLEAR, T6050_PWR_EVENT_MASK, + T6050_PWR_EVENT_D0_ON | T6050_PWR_EVENT_WAKE); + if (!ret) { + dev_dbg(dp->dev, "USB controller back from hibernation: status %#x, events %#x\n", + status, events); + return 0; + } + status = readl(dp->pwrctl + T6050_PWR_STATUS); + dev_warn(dp->dev, "USB controller gave no D0_ON after its hibernation: status %#x, events %#x\n", + status, events); + /* No longer in SLEEP: out of hibernation, only the event is missing. */ + if (!(status & T6050_PWR_STATUS_SLEEP)) + return 0; + dp->faulted = true; + return -ENODEV; +} + +/* D3 asked for, VDD_OFF awaited and cleared, the wake events armed, SLEEP awaited. */ +static int t6050_usb_hibernate(struct t6050_dp *dp) +{ + u32 cfg, events = 0, status = 0; + int ret; + + lockdep_assert_held(&dp->lock); + if (!dp->pwrctl) + return -ENODEV; + cfg = readl(dp->pwrctl + T6050_PWR_CFG); + t6050_pwr_mask(dp, T6050_PWR_CFG, T6050_PWR_CFG_STATE_REQ, T6050_PWR_CFG_STATE_REQ); + dp->usb_hibernated = true; + ret = readl_poll_timeout(dp->pwrctl + T6050_PWR_EVENTS, events, + events & T6050_PWR_EVENT_VDD_OFF, 100, T6050_PWR_TIMEOUT_US); + if (!ret) { + t6050_pwr_mask(dp, T6050_PWR_CLEAR, T6050_PWR_EVENT_MASK, T6050_PWR_EVENT_VDD_OFF); + t6050_pwr_mask(dp, T6050_PWR_ENABLE, T6050_PWR_EVENT_MASK, T6050_PWR_EVENT_WAKE); + ret = readl_poll_timeout(dp->pwrctl + T6050_PWR_STATUS, status, + status & T6050_PWR_STATUS_SLEEP, 100, T6050_PWR_TIMEOUT_US); + } + if (ret) { + dev_warn(dp->dev, "USB controller did not hibernate for the router's sleep: config %#x, events %#x, status %#x\n", + cfg, events, status); + t6050_usb_d0(dp, false); + } else { + dev_dbg(dp->dev, "USB controller in hibernation for the router's sleep: status %#x\n", + status); + } + return ret; +} + #include "atc-t6050-combo.h" static int t6050_dp_set_mode(struct phy *phy, enum phy_mode mode, int submode) @@ -1213,12 +1344,25 @@ int apple_t6050_phy_cio_off(struct device *dev) if (!dp->cio_sleep || dp->cio_sleep_off || dp->faulted || dp->combo_mode != COMBO_CIO) return -EBUSY; atc_dp_tunnels_stop(dp); - /* A host kept for the sleep is out of reset here, its PIPE parked on the dummy producer. */ + /* A host kept for the sleep has parked its PIPE (on the dummy producer, or gated on J714s). */ if (dp->pipe_active || (dp->controller_active && !dp->cio_usb_parked)) return -EBUSY; dp->cio_sleep_hold = true; ret = combo_quiesce(dp); - if (!ret && dp->usb2_active) { + /* J714s: a gated PIPE's core reset after the CIO shutdown, as macOS */ + if (!ret && dp->pipe_gated) + apple_t6050_usb_mask(&dp->usb_io, 44, 0x1c, 1, 0); + if (!ret && dp->usb2_active && atc_sleep_macos_usb()) { + u32 r1c = readl(dp->banks[0] + 0x1c), r4 = readl(dp->banks[0] + 0x4); + + /* J714s: the USB2 PHY suspended, not stopped, as macOS */ + apple_t6050_usb_mask(&dp->usb_io, 0, 0x1c, BIT(30), BIT(30)); + apple_t6050_usb_mask(&dp->usb_io, 0, 0x4, BIT(2), 0); + dp->usb2_suspended = true; + dev_dbg(dp->dev, "USB2 PHY suspended for the router's sleep: +0x1c %#x -> %#x, +0x4 %#x -> %#x, PIPE %s\n", + r1c, readl(dp->banks[0] + 0x1c), r4, readl(dp->banks[0] + 0x4), + dp->pipe_gated ? "gated" : "not gated"); + } else if (!ret && dp->usb2_active) { ret = apple_t6050_usb2_stop(&dp->usb_platform); if (!ret) dp->usb2_active = false; @@ -1344,6 +1488,19 @@ int apple_t6050_phy_cio_usb(struct device *dev, enum apple_t6050_cio_usb step) if (dp->faulted) return -EIO; /* xHCI is suspended: no DMA, its SuperSpeed port in U3, the PIPE in P3. */ + if (dp->pipe_active && dp->pipe_tunnel && atc_sleep_macos_usb() && dp->pwrctl && + (mux & 0x73) == 0x21 && !t6050_usb_hibernate(dp)) { + /* J714s: the controller in hibernation, now the PIPE's clock gated */ + apple_t6050_usb_mask(&dp->usb_io, 44, 0x4, 0x6, 0x6); + apple_t6050_usb_mask(&dp->usb_io, 44, 0x0, 0x4, 0x4); + apple_t6050_usb_mask(&dp->usb_io, 44, 0x4, 0x10, 0); + apple_t6050_usb_mask(&dp->usb_io, 44, 0xc, 0x70, 0); + dp->pipe_active = dp->pipe_tunnel = false; + dp->pipe_gated = true; + dp->cio_usb_parked = true; + dev_info(dp->dev, "T6050 sleep: USB3 PIPE kept on the USB4 tunnel with its clock gated, the USB host in hibernation\n"); + return 0; + } if (dp->pipe_active && dp->pipe_tunnel) { ret = apple_t6050_usb3_pipe_stop(&dp->usb_io, combo_pipe_keep, dp); if (ret) { @@ -1358,7 +1515,76 @@ int apple_t6050_phy_cio_usb(struct device *dev, enum apple_t6050_cio_usb step) mux, mux & 0x73, lock, readl(dp->banks[44] + 0xc), readl(dp->banks[44] + 0xc) & 0x73, readl(dp->banks[44] + 0x14)); return 0; + case APPLE_T6050_CIO_USB_RETAINED: { + /* + * J714s: macOS keeps its USB host's state through a sleep (the PIPE handler still + * reads 0x01 at the host's resume) and gives the PIPE its clock back as the host + * resumes. The controller's bank is held by its always-on domain: it kept the gate as + * the sleep left it (the dock's router woke in place, the tunnel kept), or it did not. + */ + u32 pmux, gate; + + /* Nothing was saved without the hibernation: the core starts over. */ + if (!dp->pipe_gated || !dp->usb_hibernated || dp->controller_active || + dp->cio_tunnel_lost || dp->faulted || !dp->cio_prepared || !dp->usb2_active) + return -ENOENT; + pmux = readl(dp->banks[44] + 0xc); + gate = readl(dp->banks[44] + 0x1c); + if ((pmux & 0x73) != 0x01 || (gate & 0x10)) { + dev_info(dp->dev, "the USB controller's bank did not keep the sleep's state: mux %#x, +0x1c %#x\n", + pmux, gate); + /* A hibernated controller gets D0 with the reset's release that follows. */ + return -ENOENT; + } + apple_t6050_usb_mask(&dp->usb_io, 44, 0xc, 0x70, 0x20); + /* Out of hibernation, as macOS: D0, the core's reset released, D0_ON */ + ret = t6050_usb_d0(dp, true); + if (ret == -ENODEV) + return -EIO; + apple_t6050_usb_mask(&dp->usb_io, 44, 0x0, 0x4, 0); + dp->pipe_gated = false; + dp->controller_active = dp->pipe_active = dp->pipe_tunnel = true; + dp->cio_usb_kept = dp->cio_usb_parked = false; + /* Its power control lost its state too: the PIPE is back, the core starts over. */ + if (ret == -ENODATA) + return -ENOENT; + dev_info(dp->dev, "T6050 sleep: the USB controller kept its state: PIPE given its clock back on the USB4 tunnel\n"); + return APPLE_T6050_CIO_USB_KEPT; + } case APPLE_T6050_CIO_USB_RESULT: + /* + * J714s: a gated PIPE, before anything touches the USB host: given its clock back on + * the tunnel producer, as macOS at the USB host's resume, when the dock is back with + * its registers; otherwise put on the dummy producer, as the park did. + */ + if (dp->pipe_gated && !dp->controller_active) { + /* Not known yet whether the bank kept its state: see RETAINED. */ + dev_dbg(dp->dev, "gated USB3 PIPE, its controller forgotten by the firmware sleep\n"); + } else if (dp->pipe_gated) { + u32 before = readl(dp->banks[44] + 0xc); + + dp->pipe_gated = false; + if ((before & 0x73) == 0x01 && !dp->cio_tunnel_lost) { + apple_t6050_usb_mask(&dp->usb_io, 44, 0xc, 0x70, 0x20); + /* D0 with the reset's release, as at a wake */ + t6050_usb_d0(dp, true); + apple_t6050_usb_mask(&dp->usb_io, 44, 0x0, 0x4, 0); + dp->pipe_active = dp->pipe_tunnel = !dp->faulted; + } else { + if ((before & 0x73) == 0x01) { + apple_t6050_usb_mask(&dp->usb_io, 44, 0xc, 0x3, 0x2); + apple_t6050_usb_mask(&dp->usb_io, 44, 0xc, 0x70, 0x40); + apple_t6050_usb_mask(&dp->usb_io, 44, 0x0, 0x4, 0); + } + /* The core out of reset (and of hibernation), as the old park left it */ + t6050_usb_d0(dp, true); + } + dev_info(dp->dev, "T6050 sleep: gated USB3 PIPE at the USB host's resume: mux %#x -> %#x (%s), dock %s\n", + before, readl(dp->banks[44] + 0xc), + (readl(dp->banks[44] + 0xc) & 0x73) == 0x21 ? "the USB4 tunnel" : + (readl(dp->banks[44] + 0xc) & 0x73) == 0x42 ? "dummy producer" : "lost with the controller", + dp->cio_tunnel_lost ? "gone" : "back"); + } if (!dp->cio_usb_kept) return -ENOENT; parked = dp->cio_usb_parked; diff --git a/include/linux/phy/apple-atc.h b/include/linux/phy/apple-atc.h index f6beaeb901cb6..24781e720fc78 100644 --- a/include/linux/phy/apple-atc.h +++ b/include/linux/phy/apple-atc.h @@ -125,7 +125,8 @@ int apple_t6050_phy_cio_lost(struct device *dev); * sleep in place. The router's host BINDs and UNBINDs, the USB controller asks to KEEP its * host at the sleep's start, the router's host PARKs it once the router firmware has taken * its sleep (or once a refused sleep has woken the router), and the USB controller takes the - * RESULT after the wake: one of APPLE_T6050_CIO_USB_* or an error. + * RESULT after the wake: one of APPLE_T6050_CIO_USB_* or an error, then, after a LOST one, + * asks whether the controller's bank was RETAINED. */ enum apple_t6050_cio_usb { APPLE_T6050_CIO_USB_BIND, @@ -133,8 +134,14 @@ enum apple_t6050_cio_usb { APPLE_T6050_CIO_USB_KEEP, APPLE_T6050_CIO_USB_PARK, APPLE_T6050_CIO_USB_RESULT, + /* + * After a LOST RESULT: the controller's bank checked for the state the router's sleep left + * (the PIPE gated on the tunnel producer): then the PIPE gets its clock back on the tunnel, + * the controller comes out of hibernation and KEPT is returned (J714s). + */ + APPLE_T6050_CIO_USB_RETAINED, }; -/* Kept on the tunnel's PIPE, untouched: xHCI resumes with its state. */ +/* Kept on the tunnel's PIPE (gated and given back on J714s): xHCI resumes with its state. */ #define APPLE_T6050_CIO_USB_KEPT 0 /* PIPE parked on its dummy producer, the controller kept its registers: USB3 restarts later. */ #define APPLE_T6050_CIO_USB_PARKED 1 From de8e8980158ed10acc78b1772daa12383b2f94cb Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Mon, 16 Nov 2026 05:39:14 -0500 Subject: [PATCH 2/2] usb: dwc3: apple: J714s: resume the dock port's host in place after a deep sleep With the dock port's controller kept through the USB4 router's sleep in hibernation, its core still comes back without its registers (GUID at its reset value), but the controller's state is kept: macOS writes back only a few globals (the ADT's host tunables, the CIO timers and the DRD configuration), with no core soft reset, and xHCI then restores its saved state. Do the same on J714s: mark GUID before the sleep; after the wake, when the PHY reports the controller's bank kept the sleep's state, resume in place if the core kept the mark, otherwise write back the globals and the host's GCTL hibernation setup and let xHCI restore its state. After a sleep refused behind the hibernation the core is set up the same way. The J714s's T6050 controllers now get the same GCTL hibernation enable and power-down scale as T8140 at every start, as macOS's controller start sets them on the dock's port. Signed-off-by: Diljit Singh --- drivers/usb/dwc3/dwc3-apple.c | 117 +++++++++++++++++++++++++++------- 1 file changed, 95 insertions(+), 22 deletions(-) diff --git a/drivers/usb/dwc3/dwc3-apple.c b/drivers/usb/dwc3/dwc3-apple.c index 7b1004854107a..1d0c76da39682 100644 --- a/drivers/usb/dwc3/dwc3-apple.c +++ b/drivers/usb/dwc3/dwc3-apple.c @@ -302,23 +302,29 @@ static inline void dwc3_apple_mask(struct dwc3_apple *appledwc, u32 offset, u32 } /* - * macOS's T8140 controller reset sets GCTL's hibernation enable and a power-down scale - * of 0xd. Without the enable the core's power management accepts a - * D0/D3 request but saves and restores nothing, so the PHY's hibernation for a sleep - * needs it. + * macOS's T8140 and T6050 controller starts set GCTL's hibernation enable and a power-down + * scale of 0xd. Without the enable the core's power management accepts a D0/D3 request but + * saves and restores nothing, so the PHY's hibernation for a sleep needs it. */ -#define DWC3_APPLE_T8140_PWRDNSCALE 0xd +#define DWC3_APPLE_HIB_PWRDNSCALE 0xd static void dwc3_apple_setup_hibernation(struct dwc3_apple *appledwc) { struct dwc3 *dwc = &appledwc->dwc; u32 reg; - if (!of_device_is_compatible(appledwc->dev->of_node, "apple,t8140-dwc3")) + /* + * J714s: its T6050 controllers too (set at every start, in either role). The one on the + * USB4 dock's port goes through hibernation for its router's sleep (see + * apple_t6050_phy_cio_usb()), and xHCI restores its state after the wake. + */ + if (!of_device_is_compatible(appledwc->dev->of_node, "apple,t8140-dwc3") && + !(of_machine_is_compatible("apple,j714s") && + of_device_is_compatible(appledwc->dev->of_node, "apple,t6050-dwc3"))) return; reg = dwc3_readl(dwc, DWC3_GCTL); reg &= ~DWC3_GCTL_PWRDNSCALE_MASK; - reg |= DWC3_GCTL_PWRDNSCALE(DWC3_APPLE_T8140_PWRDNSCALE) | DWC3_GCTL_GBLHIBERNATIONEN; + reg |= DWC3_GCTL_PWRDNSCALE(DWC3_APPLE_HIB_PWRDNSCALE) | DWC3_GCTL_GBLHIBERNATIONEN; dwc3_writel(dwc, DWC3_GCTL, reg); } @@ -481,6 +487,31 @@ static void dwc3_apple_set_ptrcap(struct dwc3_apple *appledwc, u32 mode) #define DWC3_APPLE_CFG_STATUS_PIPE_PHY_READY BIT(30) #define DWC3_APPLE_PIPE_PHY_READY_US 100000 +/* + * A host core's globals written back after it lost its registers, with no core soft reset. + * After a sleep in place this is all macOS's wake of the dock port's host writes before xHCI's + * restore (CRS), once the controller is out of hibernation: the ADT's DRD_USB31_GBL_HOST + * tunables, Apple's CIO timers and its DRD configuration (after dwc3_core_init()'s soft reset + * the restore fails). The core keeps the PHYs it holds. + */ +static void dwc3_apple_core_restore(struct dwc3_apple *appledwc) +{ + struct dwc3 *dwc = &appledwc->dwc; + int ret; + + dwc3_apple_setup_cio(appledwc); + dwc3_apple_apply_tunables(appledwc); + dwc3_apple_setup_hibernation(appledwc); + dwc3_apple_set_ptrcap(appledwc, DWC3_GCTL_PRTCAP_HOST); + dwc->dis_u3_susphy_quirk = appledwc->dt_dis_u3_susphy; + dwc3_enable_susphy(dwc, true); + /* A PHY that came back in a USB3 mode routes the PIPE to it now, before xHCI starts. */ + ret = phy_set_mode(appledwc->usb3_phy, PHY_MODE_USB_HOST); + if (ret) + dev_warn(appledwc->dev, "USB3 PHY setup failed after the sleep, USB2 only, err=%d\n", + ret); +} + /* * The core is powered but its registers are at their reset values. Set it up again the way * dwc3_apple_core_start() and dwc3_apple_set_role() do, in place: the PHYs and the xHCI @@ -505,18 +536,7 @@ static int dwc3_apple_core_reinit(struct dwc3_apple *appledwc) phy_exit(dwc->usb2_generic_phy[i]); } - dwc3_apple_setup_cio(appledwc); - dwc3_apple_apply_tunables(appledwc); - dwc3_apple_setup_hibernation(appledwc); - dwc3_apple_set_ptrcap(appledwc, DWC3_GCTL_PRTCAP_HOST); - dwc->dis_u3_susphy_quirk = appledwc->dt_dis_u3_susphy; - dwc3_enable_susphy(dwc, true); - /* A PHY that came back in a USB3 mode routes the PIPE to it now, before xHCI starts. */ - ret = phy_set_mode(appledwc->usb3_phy, PHY_MODE_USB_HOST); - if (ret) - dev_warn(appledwc->dev, "USB3 PHY setup failed after the sleep, USB2 only, err=%d\n", - ret); - + dwc3_apple_core_restore(appledwc); return 0; } @@ -561,6 +581,55 @@ static int dwc3_apple_tunnel_resume(struct dwc3_apple *appledwc) appledwc->sleep_tunnel_result, pm_resume_via_firmware() ? "yes" : "no"); } ret = appledwc->sleep_tunnel_result; + /* + * J714s: the PHY forgot the controller in the firmware sleep, but its bank may have kept + * the state the sleep left, as macOS's does: then, the PIPE given its clock back and the + * controller out of hibernation, a core that kept the GUID mark kept its registers and + * xHCI resumes in place. + */ + if (ret == APPLE_T6050_CIO_USB_LOST && + dwc3_apple_tunnel_phy(appledwc, APPLE_T6050_CIO_USB_RETAINED) == APPLE_T6050_CIO_USB_KEPT) { + u32 guid = dwc3_readl(&appledwc->dwc, DWC3_GUID); + + dev_dbg(appledwc->dev, "USB host's core after the firmware sleep: GUID %#x (%s)\n", + guid, guid == DWC3_APPLE_SLEEP_GUID ? "kept its registers" : "lost them"); + if (guid == DWC3_APPLE_SLEEP_GUID) { + dwc3_writel(&appledwc->dwc, DWC3_GUID, appledwc->sleep_guid); + appledwc->sleep_tunnel_result = APPLE_T6050_CIO_USB_KEPT; + return 0; + } + /* + * The core lost its registers (GUID at its reset value) while the controller's + * bank kept the PIPE as the sleep left it: its globals written back as macOS does, + * xHCI restores its saved state rather than starting over and resetting every + * device on the dock. + */ + ret = reset_control_deassert(appledwc->reset); + if (!ret) + dwc3_apple_core_restore(appledwc); + dev_info(appledwc->dev, "USB host kept through the router's sleep: globals written back, xHCI restores its state (%d)\n", + ret); + if (!ret) { + appledwc->sleep_tunnel_result = DWC3_APPLE_TUNNEL_REINIT; + return 0; + } + appledwc->sleep_tunnel_result = APPLE_T6050_CIO_USB_LOST; + return ret; + } else if (ret == APPLE_T6050_CIO_USB_KEPT || ret == APPLE_T6050_CIO_USB_PARKED) { + u32 guid = dwc3_readl(&appledwc->dwc, DWC3_GUID); + + if (guid == DWC3_APPLE_SLEEP_GUID) { + dwc3_writel(&appledwc->dwc, DWC3_GUID, appledwc->sleep_guid); + } else { + /* + * A sleep refused after the controller's hibernation: the PHY brought + * it back to D0 and its core lost its registers as at a wake. Its + * globals are written back the same way, and xHCI restores its state. + */ + dwc3_apple_core_restore(appledwc); + dev_info(appledwc->dev, "USB host after the hibernation of a refused sleep: globals written back, xHCI restores its state\n"); + } + } if (ret == APPLE_T6050_CIO_USB_KEPT || ret == APPLE_T6050_CIO_USB_PARKED) return 0; /* Lost, or not known: nothing touches the core before it is out of reset. */ @@ -1463,9 +1532,10 @@ static int dwc3_apple_recover(struct dwc3_apple *appledwc) * mxt5, mxt6, mxt6b, seven sleeps), so the router's USB3 tunnel keeps its host. Linux stopped the * host here, 2.66 s before the dock's sleep bit (run sv6t9), and parked its PIPE. With its PIPE * on the router's USB3 tunnel the host now stays: xHCI and the devices behind it suspend as for - * any sleep, their links in U3; the glue stays a syscore device, so the core keeps its power and - * registers; the router's host parks the PIPE behind the router firmware's sleep, or with the - * router awake again after a refusal, see apple_t6050_phy_cio_usb(). + * any sleep, their links in U3; the glue stays a syscore device; the router's host parks the PIPE + * behind the router firmware's sleep, or with the router awake again after a refusal, see + * apple_t6050_phy_cio_usb(). On J714s the PHY takes the controller through hibernation there: + * its core comes back without its registers, its state kept for xHCI's restore. */ static bool dwc3_apple_tunnel_keep(struct dwc3_apple *appledwc) { @@ -1477,6 +1547,9 @@ static bool dwc3_apple_tunnel_keep(struct dwc3_apple *appledwc) appledwc->sleep_tunnel = true; appledwc->sleep_tunnel_taken = false; appledwc->sleep_tunnel_result = APPLE_T6050_CIO_USB_KEPT; + /* A mark in GUID, to tell after the sleep whether the core kept its registers */ + appledwc->sleep_guid = dwc3_readl(&appledwc->dwc, DWC3_GUID); + dwc3_writel(&appledwc->dwc, DWC3_GUID, DWC3_APPLE_SLEEP_GUID); appledwc->sleep_tunnel_role = USB_ROLE_HOST; dev_info(appledwc->dev, "USB host kept for the USB4 router's sleep: xHCI suspends in place\n"); return true;