Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E7DEEC433F5 for ; Fri, 24 Dec 2021 23:22:34 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 635D5837C1; Sat, 25 Dec 2021 00:22:31 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=openbsd.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; secure) header.d=kpnmail.nl header.i=@kpnmail.nl header.b="B8rWopod"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B37D58379C; Sat, 25 Dec 2021 00:22:29 +0100 (CET) Received: from ewsoutbound.kpnmail.nl (ewsoutbound.kpnmail.nl [195.121.94.167]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 7062D837C1 for ; Sat, 25 Dec 2021 00:22:25 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=openbsd.org Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=kettenis@openbsd.org X-KPN-MessageId: 4798ad4b-6510-11ec-9a2e-005056abbe64 Received: from smtp.kpnmail.nl (unknown [10.31.155.39]) by ewsoutbound.so.kpn.org (Halon) with ESMTPS id 4798ad4b-6510-11ec-9a2e-005056abbe64; Sat, 25 Dec 2021 00:21:53 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kpnmail.nl; s=kpnmail01; h=mime-version:message-id:date:subject:to:from; bh=4sx62lYmDmBDYQtO2S++6BeYqxLiE00VRa0AzDf/W9E=; b=B8rWopodOUvhXCLVMEf/vBOxKNSv853Nd/uXmejJNm3BwniPctwfAObA7VQH+hxfOr/SWOus0ElKL VFJeQV3RB6XHYM3EyI8q22bpglUgAEwgDR7bMi4O0fCG9HkWu/Mf97MizsrmGnorbQrq93LfklTyX+ RC8IliGW4pMBD0ss= X-KPN-VerifiedSender: No X-CMASSUN: 33|AHi+LLvuRkbfHlwS7URNpn3jH0lurdNXxpG3678t+ivCZwd8PeM0+imOmbGARkT T1hpCGAXUnqfnVNWZeb/8iA== X-Originating-IP: 83.163.83.176 Received: from copland.sibelius.xs4all.nl (sibelius.xs4all.nl [83.163.83.176]) by smtp.xs4all.nl (Halon) with ESMTPSA id 5985c39c-6510-11ec-81f5-005056ab7447; Sat, 25 Dec 2021 00:22:24 +0100 (CET) From: Mark Kettenis To: u-boot@lists.denx.de Cc: Mark Kettenis , Marek Vasut , Bin Meng , Matthias Schiffer , Simon Glass , Markus Niebel , Michael Walle Subject: [RFC] usb: xhci-dwc3: Support role switch default role Date: Sat, 25 Dec 2021 00:22:14 +0100 Message-Id: <20211224232216.56847-1-kettenis@openbsd.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.38 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean When the device tree indicates support for role switching through the "usb-role-switch" property, take the "role-switch-default-mode" property into account when deciding which role to put the controller into. This makes USB devices work on Apple M1 systems where the device tree may include a "dr_mode" property that is set to "otg", but where we need to put the controller into "host" mode to see devices connected to the type-C ports. Signed-off-by: Mark Kettenis --- I'm far from sure this is the right approach. But it does appear that U-Boot doesn't really support role switching. So making sure U-Boot uses the default role defined in the device tree makes sense to me. drivers/usb/common/common.c | 16 ++++++++++++++++ drivers/usb/host/xhci-dwc3.c | 6 ++++++ include/linux/usb/otg.h | 10 ++++++++++ 3 files changed, 32 insertions(+) diff --git a/drivers/usb/common/common.c b/drivers/usb/common/common.c index ee0c064f1f..cff86a51ae 100644 --- a/drivers/usb/common/common.c +++ b/drivers/usb/common/common.c @@ -40,6 +40,22 @@ enum usb_dr_mode usb_get_dr_mode(ofnode node) return USB_DR_MODE_UNKNOWN; } +enum usb_dr_mode usb_get_role_switch_default_mode(ofnode node) +{ + const char *dr_mode; + int i; + + dr_mode = ofnode_read_string(node, "role-switch-default-mode"); + if (!dr_mode) + return USB_DR_MODE_UNKNOWN; + + for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++) + if (!strcmp(dr_mode, usb_dr_modes[i])) + return i; + + return USB_DR_MODE_UNKNOWN; +} + static const char *const speed_names[] = { [USB_SPEED_UNKNOWN] = "UNKNOWN", [USB_SPEED_LOW] = "low-speed", diff --git a/drivers/usb/host/xhci-dwc3.c b/drivers/usb/host/xhci-dwc3.c index bec0d98081..1dbd65dfaa 100644 --- a/drivers/usb/host/xhci-dwc3.c +++ b/drivers/usb/host/xhci-dwc3.c @@ -209,6 +209,12 @@ static int xhci_dwc3_probe(struct udevice *dev) writel(reg, &dwc3_reg->g_usb2phycfg[0]); dr_mode = usb_get_dr_mode(dev_ofnode(dev)); + if (dr_mode == USB_DR_MODE_OTG && + dev_read_bool(dev, "usb-role-switch")) { + dr_mode = usb_get_role_switch_default_mode(dev_ofnode(dev)); + if (dr_mode == USB_DR_MODE_UNKNOWN) + dr_mode = USB_DR_MODE_OTG; + } if (dr_mode == USB_DR_MODE_UNKNOWN) /* by default set dual role mode to HOST */ dr_mode = USB_DR_MODE_HOST; diff --git a/include/linux/usb/otg.h b/include/linux/usb/otg.h index c19b916be9..5d0dac950e 100644 --- a/include/linux/usb/otg.h +++ b/include/linux/usb/otg.h @@ -27,6 +27,16 @@ enum usb_dr_mode { */ enum usb_dr_mode usb_get_dr_mode(ofnode node); +/** + * usb_get_dr_mode() - Get dual role mode for given device + * @node: ofnode of the given device + * + * The function gets phy interface string from property + * 'role-switch-defaulr-mode', and returns the correspondig enum + * usb_dr_mode + */ +enum usb_dr_mode usb_get_role_switch_default_mode(ofnode node); + /** * usb_get_maximum_speed() - Get maximum speed for given device * @node: ofnode of the given device -- 2.34.1