Skip to content
Open
10 changes: 10 additions & 0 deletions Documentation/devicetree/bindings/phy/apple,t6040-atcphy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ properties:
$ref: "#/$defs/apple,tunable"
description: USB2 host tunables, applied to the bank0 region.

apple,tunable-usb2-default:
$ref: "#/$defs/apple,tunable"
description:
USB2 default tunables, applied to the bank0 region before the device
tunables when the port runs as a USB device.

apple,tunable-usb2-device:
$ref: "#/$defs/apple,tunable"
description: USB2 device tunables, applied to the bank0 region.

required:
- compatible
- reg
Expand Down
8 changes: 8 additions & 0 deletions drivers/phy/apple/atc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4612,9 +4612,17 @@ static int atcphy_rpm_usb2_set_mode(struct phy *phy, enum phy_mode mode, int sub
return ret;
}

/* Both roles: atcphy_usb2_set_mode() sets or clears the host signature. */
static int atcphy_usb2_validate(struct phy *phy, enum phy_mode mode, int submode,
union phy_configure_opts *opts)
{
return mode == PHY_MODE_USB_HOST || mode == PHY_MODE_USB_DEVICE ? 0 : -EOPNOTSUPP;
}

static const struct phy_ops apple_atc_usb2_phy_ops = {
.owner = THIS_MODULE,
.init = atcphy_rpm_usb2_init,
.validate = atcphy_usb2_validate,
.set_mode = atcphy_rpm_usb2_set_mode,
};

Expand Down
152 changes: 120 additions & 32 deletions drivers/phy/apple/t6040-usb2.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
/*
* Apple T6040 Type-C PHY -- USB2 and USB3 host
* Apple T6040 Type-C PHY -- USB2 host and device, USB3 host
*
* This deliberately does not share the T8103/T8122 ATC PHY compatible. T6040
* has CIO4/TB5-era resources and a different event-bank layout. The
* implemented operations are the USB2 host sequence and the USB3 host attach
* (mode 4) decoded from the paired AppleT6040TypeCPhy driver.
* implemented operations are the USB2 host and device sequences and the USB3
* host attach (mode 4) decoded from the paired AppleT6040TypeCPhy driver.
*
* USB2 device: the same eUSB2 power-on as the host, with the device tunables
* from the boot loader instead of the host one, the role signature cleared and
* VBUS forced valid (the Type-C port controller, not this PHY, sees VBUS: the
* controller only enters device mode once the port controller has reported a
* USB data connection from a host). The USB3 PHY has no device sequence, so a
* device port runs at high speed with its PIPE on the dummy PHY.
*
* USB3: the Type-C orientation switch records the plug orientation; power_on
* of the USB3 PHY wakes the PHY, applies the tunables and the crossbar for
Expand Down Expand Up @@ -56,7 +63,7 @@

#define T6040_USB2_USBCTL 0x00
#define T6040_USB2_USBCTL_MODE GENMASK(2, 0)
#define T6040_USB2_USBCTL_MODE_HOST 2
#define T6040_USB2_USBCTL_MODE_HOST 2 /* "run": host and device alike */

#define T6040_USB2_CTL 0x04
#define T6040_USB2_CTL_RESET BIT(0)
Expand All @@ -67,6 +74,8 @@
#define T6040_USB2_SIG 0x08
#define T6040_USB2_SIG_VBUSDET_FORCE_VAL BIT(0)
#define T6040_USB2_SIG_VBUSDET_FORCE_EN BIT(1)
#define T6040_USB2_SIG_VBUSVLDEXT_FORCE_VAL BIT(2)
#define T6040_USB2_SIG_VBUSVLDEXT_FORCE_EN BIT(3)
#define T6040_USB2_SIG_HOST_0 BIT(12)
#define T6040_USB2_SIG_HOST_1 BIT(13)
#define T6040_USB2_SIG_HOST_2 BIT(14)
Expand Down Expand Up @@ -280,6 +289,9 @@ struct t6040_usb2_phy {
struct apple_tunable *tunable_pipehandler;
/* ADT tunable_USB2PHY_HOST, applied after the signal setup. */
struct apple_tunable *tunable_usb2_host;
/* ADT tunable_USB2PHY_DFLT and _DEV, applied in their place for device mode. */
struct apple_tunable *tunable_usb2_default;
struct apple_tunable *tunable_usb2_device;
struct apple_rtkit *rtk;
struct device *mbox_dev;
struct completion app_reply;
Expand Down Expand Up @@ -326,11 +338,17 @@ static inline void t6040_usb2_clear32(void __iomem *reg, u32 clear)
t6040_usb2_mask32(reg, clear, 0);
}

static int t6040_usb2_validate(struct phy *phy, enum phy_mode mode, int submode,
union phy_configure_opts *opts)
{
return mode == PHY_MODE_USB_HOST || mode == PHY_MODE_USB_DEVICE ? 0 : -EOPNOTSUPP;
}

static int t6040_usb2_set_mode(struct phy *phy, enum phy_mode mode, int submode)
{
struct t6040_usb2_phy *tphy = phy_get_drvdata(phy);

if (mode != PHY_MODE_USB_HOST)
if (t6040_usb2_validate(phy, mode, submode, NULL))
return -EOPNOTSUPP;

guard(mutex)(&tphy->lock);
Expand All @@ -347,16 +365,37 @@ static int t6040_usb2_set_mode(struct phy *phy, enum phy_mode mode, int submode)
#define T6040_USB2_SIG_INIT (T6040_USB2_SIG_HOST_0 | T6040_USB2_SIG_HOST_1 | \
T6040_USB2_SIG_HOST_2 | T6040_USB2_SIG_VBUSDET_FORCE_VAL | \
T6040_USB2_SIG_VBUSDET_FORCE_EN)
#define T6040_USB2_SIG_ROLE (T6040_USB2_SIG_HOST_0 | T6040_USB2_SIG_HOST_1 | \
T6040_USB2_SIG_HOST_2)
#define T6040_USB2_SIG_VBUSVLDEXT (T6040_USB2_SIG_VBUSVLDEXT_FORCE_VAL | \
T6040_USB2_SIG_VBUSVLDEXT_FORCE_EN)
/* The signal bits power_on programs and checks: the init ones, plus VBUS valid in device mode. */
#define T6040_USB2_SIG_CHECKED (T6040_USB2_SIG_INIT | T6040_USB2_SIG_VBUSVLDEXT)

static u32 t6040_usb2_sig_want(struct t6040_usb2_phy *tphy)
static u32 t6040_usb2_sig_tunable(u32 sig, const struct apple_tunable *t)
{
struct apple_tunable *t = tphy->tunable_usb2_host;
u32 want = T6040_USB2_SIG_INIT;
size_t i;

for (i = 0; t && i < t->sz; i++)
if (t->values[i].offset == T6040_USB2_SIG)
want = (want & ~t->values[i].mask) | t->values[i].value;
sig = (sig & ~t->values[i].mask) | t->values[i].value;
return sig;
}

/* The checked signal bits as power_on leaves them in the current mode. */
static u32 t6040_usb2_sig_want(struct t6040_usb2_phy *tphy)
{
u32 want = T6040_USB2_SIG_INIT;

if (tphy->mode == PHY_MODE_USB_DEVICE) {
want = t6040_usb2_sig_tunable(want, tphy->tunable_usb2_default);
want = t6040_usb2_sig_tunable(want, tphy->tunable_usb2_device);
want = (want & ~T6040_USB2_SIG_ROLE) | T6040_USB2_SIG_VBUSVLDEXT;
return want & T6040_USB2_SIG_CHECKED;
}

want = t6040_usb2_sig_tunable(want, tphy->tunable_usb2_default);
want = t6040_usb2_sig_tunable(want, tphy->tunable_usb2_host);
return want & T6040_USB2_SIG_INIT;
}

Expand All @@ -369,7 +408,7 @@ static int t6040_usb2_power_on(struct phy *phy)
if (tphy->shutdown_failed)
return -EIO;

if (tphy->mode != PHY_MODE_USB_HOST)
if (tphy->mode != PHY_MODE_USB_HOST && tphy->mode != PHY_MODE_USB_DEVICE)
return -EINVAL;
if (tphy->powered)
return 0;
Expand All @@ -390,13 +429,33 @@ static int t6040_usb2_power_on(struct phy *phy)
t6040_usb2_set32(tphy->usb2 + T6040_USB2_SIG,
T6040_USB2_SIG_VBUSDET_FORCE_EN);

/*
* The host tunable then sets the signal fields for host mode: on the
* J616s it clears SIG[14:12] again (mask 0x7003, value 0x3). Left at
* their init values, which are the device-mode ones, the USB2 port
* never sees a connect.
*/
apple_tunable_apply(tphy->usb2, tphy->tunable_usb2_host);
if (tphy->mode == PHY_MODE_USB_DEVICE) {
/*
* Device mode takes the default and device tunables instead. On
* the J616s they leave SIG[14:12] set (SIG reads 0x01c0700f), and
* with those set a host never sees the device connect: clear them,
* as the host tunable does for host mode. VBUS is the port
* controller's: force it valid, or the controller sees no session
* (J616s USB2 device row, aura#245, 2026-11-19).
*/
if (!tphy->tunable_usb2_device)
dev_dbg(tphy->dev, "USB2 device mode without device tunables\n");
apple_tunable_apply(tphy->usb2, tphy->tunable_usb2_default);
apple_tunable_apply(tphy->usb2, tphy->tunable_usb2_device);
t6040_usb2_clear32(tphy->usb2 + T6040_USB2_SIG, T6040_USB2_SIG_ROLE);
t6040_usb2_set32(tphy->usb2 + T6040_USB2_SIG, T6040_USB2_SIG_VBUSVLDEXT);
} else {
/*
* The host tunable then sets the signal fields for host mode: on
* the J616s it clears SIG[14:12] again (mask 0x7003, value 0x3).
* Left at their init values the USB2 port never sees a connect.
* The default tunable, when the boot loader provides it, goes
* first, as for a device: fields only the device tunable writes
* do not carry over from a device session.
*/
apple_tunable_apply(tphy->usb2, tphy->tunable_usb2_default);
apple_tunable_apply(tphy->usb2, tphy->tunable_usb2_host);
}

msleep(10);

Expand Down Expand Up @@ -430,7 +489,7 @@ static int t6040_usb2_power_on(struct phy *phy)
T6040_USB2_USBCTL_MODE,
T6040_USB2_USBCTL_MODE_HOST);

/* Confirm the programmed fields before the core can start the host. */
/* Confirm the programmed fields before the core can start. */
sig_want = t6040_usb2_sig_want(tphy);
ctl = readl(tphy->usb2 + T6040_USB2_CTL);
sig = readl(tphy->usb2 + T6040_USB2_SIG);
Expand All @@ -442,15 +501,17 @@ static int t6040_usb2_power_on(struct phy *phy)
(ctl & (T6040_USB2_CTL_RESET | T6040_USB2_CTL_PORT_RESET |
T6040_USB2_CTL_SIDDQ | T6040_USB2_CTL_APB_RESET_N)) !=
T6040_USB2_CTL_APB_RESET_N ||
(sig & T6040_USB2_SIG_INIT) != sig_want ||
(sig & (tphy->mode == PHY_MODE_USB_DEVICE ? T6040_USB2_SIG_CHECKED :
T6040_USB2_SIG_INIT)) != sig_want ||
(tune & (T6040_USB2_MISCTUNE_APBCLK_GATE_OFF |
T6040_USB2_MISCTUNE_REFCLK_GATE_OFF)) ||
(mode & T6040_USB2_USBCTL_MODE) != T6040_USB2_USBCTL_MODE_HOST ||
!(status & T6040_USB2_EVT_CTL_EN))
goto failed;

tphy->powered = true;
dev_info(tphy->dev, "USB2 host sequence complete\n");
dev_info(tphy->dev, "USB2 %s sequence complete\n",
tphy->mode == PHY_MODE_USB_DEVICE ? "device" : "host");
return 0;
failed:
tphy->shutdown_failed = true;
Expand Down Expand Up @@ -512,11 +573,19 @@ static int t6040_usb2_power_off(struct phy *phy)
return -EIO;
tphy->shutdown_failed = false;
tphy->powered = false;
/*
* dwc3-apple selects device mode before each device session; xHCI asks
* for host mode only once the PHY is powered. Start the next session in
* host mode, as at probe, or a host after a device session would power
* on as a device and then fail xHCI's request.
*/
tphy->mode = PHY_MODE_USB_HOST;
return 0;
}

static const struct phy_ops t6040_usb2_phy_ops = {
.owner = THIS_MODULE,
.validate = t6040_usb2_validate,
.set_mode = t6040_usb2_set_mode,
.power_on = t6040_usb2_power_on,
.power_off = t6040_usb2_power_off,
Expand Down Expand Up @@ -2327,6 +2396,9 @@ static int t6040_usb2_reset_assert(struct reset_controller_dev *rcdev,
T6040_PIPEHANDLER_AON_GEN_DWC3_RESET_N);
t6040_usb2_set32(tphy->pipehandler + T6040_PIPEHANDLER_AON_GEN,
T6040_PIPEHANDLER_AON_GEN_DWC3_FORCE_CLAMP_EN);
/* A device start that failed before power_on also ends here: back to host mode. */
if (!tphy->powered)
tphy->mode = PHY_MODE_USB_HOST;
if (tphy->dwc3_running) {
tphy->dwc3_running = false;
pm_runtime_put(tphy->dev);
Expand Down Expand Up @@ -2399,6 +2471,22 @@ static struct phy *t6040_usb2_xlate(struct device *dev,
}
}

/* An optional USB2 tunable from the boot loader, for the bank0 region. */
static int t6040_usb2_tunable(struct platform_device *pdev, const char *name,
struct apple_tunable **tunable)
{
struct device *dev = &pdev->dev;

if (!of_property_present(dev->of_node, name))
return 0;
*tunable = devm_apple_tunable_parse(dev, dev->of_node, name,
platform_get_resource_byname(pdev, IORESOURCE_MEM,
"bank0"));
if (IS_ERR(*tunable))
return dev_err_probe(dev, PTR_ERR(*tunable), "invalid %s\n", name);
return 0;
}

static int t6040_usb2_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
Expand All @@ -2417,9 +2505,9 @@ static int t6040_usb2_probe(struct platform_device *pdev)
* to this phy. dwc3-apple's early phy_set_mode() call happens while
* dwc.usb2_generic_phy[] is still unpopulated, so it is a silent no-op
* on a NULL phy, and a PHY_MODE_INVALID default then fails power_on
* with -EINVAL ("failed to initialize core", ticket 108). This
* provider implements exactly one mode, so start in it; set_mode
* still rejects every request that is not PHY_MODE_USB_HOST.
* with -EINVAL ("failed to initialize core", ticket 108). Start in
* host mode; dwc3-apple selects device mode before a device session,
* and power_off returns to host mode.
*/
tphy->mode = PHY_MODE_USB_HOST;
mutex_init(&tphy->lock);
Expand All @@ -2431,15 +2519,15 @@ static int t6040_usb2_probe(struct platform_device *pdev)
return dev_err_probe(dev, PTR_ERR(tphy->usb2),
"failed to map USB2 bank 0\n");

if (of_property_present(dev->of_node, "apple,tunable-usb2-host")) {
tphy->tunable_usb2_host =
devm_apple_tunable_parse(dev, dev->of_node, "apple,tunable-usb2-host",
platform_get_resource_byname(pdev, IORESOURCE_MEM,
"bank0"));
if (IS_ERR(tphy->tunable_usb2_host))
return dev_err_probe(dev, PTR_ERR(tphy->tunable_usb2_host),
"invalid apple,tunable-usb2-host\n");
}
ret = t6040_usb2_tunable(pdev, "apple,tunable-usb2-host", &tphy->tunable_usb2_host);
if (!ret)
ret = t6040_usb2_tunable(pdev, "apple,tunable-usb2-default",
&tphy->tunable_usb2_default);
if (!ret)
ret = t6040_usb2_tunable(pdev, "apple,tunable-usb2-device",
&tphy->tunable_usb2_device);
if (ret)
return ret;

tphy->evt = devm_platform_ioremap_resource_byname(pdev, "bank1");
if (IS_ERR(tphy->evt))
Expand Down
Loading
Loading