From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796049701; b=kfIQ+PhlAMrjVcdlr6KGeocllWjV4EkS74xxj+70+v3lwg5vMGe0CVYgi8geTRgFGgmb ernkH4hMR3ByYjQelZdacufCkz+9VVb62hoGO+Dn6Xy9muDIDHPDX1fIoByQBDsKpLhJp cbnxYs0TdJpKTQDsgiq9+9Mxh4MUzldXtUQukZjR/gHZrs6tjzgPpPbVHcKEJYgTh+Gge EBdUjzAKkAGupe50kHFXHe2t6CR0Hm6hOWvHOYckgR/0tUioptg7PhGe3UWPHQEykMVL4 pr849BboUXGUWO5XW69zSN9EwSpHrRAf4snmIbyPeWnCXwD8K04FBlJy5dufT18px4Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796049701; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=Z5F0VOVx3A7YGEAzu8GaDJrNslTAAHgr84b1qa2Breg=; b=krGQzvjLCQevzkSLmG36nCJqbKQLWmK8BTvmKxku+vzvu4qSiArYTcllfr3CNcgVcGEi Rmw7Ff9L/y8Wd9ITp/GsXH0Y5dW+hxPYBHuKgPBcN5Avfny9TukaYKkKdZNSqNNFWIbyX iXUMdZLmxSoDwdxi3J9HUocXdb0WsfdjxZl59dz16B/3MCxcj+8RI2dPZKREMONZFdvGH yudSoyd8yBmK8c89hbNpY2OgWsqM8kQIgpUzrT+Wz2CKyBpxwXnVhbqPXrPVtzv0j+5bz WUTwz1KXdgG62oWU3X5t+J0qIoU1e/0H1VKVYuX0VDBp9ODmpovGT853NrQlIgqNInQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=dUM4YqsR; arc=none smtp.remote-ip=209.85.216.42 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 680ED606A31C for ; Mon, 30 Nov 2026 14:41:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 680ED606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=dUM4YqsR Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-3b40a522c00so1396350a91.3 for ; Mon, 30 Nov 2026 06:41:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796049701; x=1796654501; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=Z5F0VOVx3A7YGEAzu8GaDJrNslTAAHgr84b1qa2Breg=; b=dUM4YqsRcce4qpr2DdDnppOpZxRXuhhlxMkM611RACTtTxEqLzGplAmmu/LycF52ha c7YY7oXMy3i9eMsuMQxVAUuo/ywvGnQG4y5+/nCnNSJlU+nF9isN+z3GFHeo/+Q58AQa fLs5WIti7LnEsIiWzZUrvtsDgZ+XIO/ADRg0rDniQrxXNeFusUcU0fdGOvO/aoT1f9C4 NCrfCcC+qTaBA5f6dk55IKXcj51g3k8nFK+pBnxm0ceyYM23NdyrsCh6cRSBc88xCAVo MqkRgrotSCQY8CDzTcl+yZoyfRiXhlyUiS+vxwjUmTkvZPwJ9Fejm/V2ETxirXxc3ZF/ i7fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796049701; x=1796654501; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Z5F0VOVx3A7YGEAzu8GaDJrNslTAAHgr84b1qa2Breg=; b=nYFTurXIdWWOzcbQPvY0Q3uaMNcVBzKGsA34fGiGfcvgXV+P6EJDyvGxbN4y69/zK6 L/T0RLILYpRO0K2WGAzvfquGM8Nqz6ti8EW2GR8BIdeZghNmVa0Ke0SEjnn/ElOy/wgD Wq1A5nHZJSkRRFvXqb+Nu9zNBGndmB9oEak5aABBT1u7V76rQp2Wv9xjLMasnWPrU92p o7oL3J+EEQ7G+zhAV7RaY+9flWGKns2q5KT2kGq+P9NZIzbaZEHJAOm2mRXQwh4HI2di aQvsiFnPCuRQyumu8/XaS9r7J7ft9AY9REEzedN6jCz4VIWN4+YU1t7jmhs3fMI1PjW6 Ud3Q== X-Forwarded-Encrypted: i=1; AL+hErFC4/tEFpwDYP6mVXmOHjM2h3vkD6mZkbej826tU6svyo7EzMQ6EK30ExSVpO+JPK5Ii8+/rZe7nFp4F8M=@vger.kernel.org X-Gm-Message-State: APuU4Z9jmIzBEmGOcBAyv4qwixtNREdTnppm3pOv0kQQstWd6FGpyyum 0WpbLHUnArybfunhGCVZeFADxXf/mJ6ZNeuW4dBYoPnubL7HhW7tZS5O X-Gm-Gg: AZFdTZfr9ts2u4JDqcZhrvmCHd0jYbKyaI7QR7tCWb6Z814dxEKag3QQrBhUfvLElXr F8RUio0p9D+NuW/dw09Qia/MTY9IUIT7OE1kEjsoKfe6ugr25i0Uv2yG8Q1p3zRkyEoxDqOlIT2 KnFvLu7SjeFF1gAd+3QA8y7/Cb/vLZvqo8uSnMjXNhDP5HbHUV3IGWZ4YvVGZQZtAQc2qbkmzcN SUjuo33MhE5wGQAATBnwM25KUsecREYsekKHUxIneoJAOOqH7uCmN34ZCzjrb63YkUl5QTWhYGa SiDVwYddU1Z5G7Vbkin8+1kzqsUipDs0SZlwiI/IYuMjU4b6X7sdpw8NAoVo8lFhDV4bSl7wbCK dY0cGOY2XWvoUYn8CjBOS7eBnU2lhLCFrru6sbXloPfQQ7gBKDtKoGh8EFfxMnR/50udvvzqQu2 69CIYKUoprEYgjo4OUPst3fmCxvSDhDDfICkcQkMHBIMVJ8507IyHZNpH2mEXzbYdobvKw4jLSO ptTkYvitUr5aCTRUcA6m/Trgd0nDRiD4uWoVLgFnBPa+PxzjNRsrw3M78OaZ2D/2CQbso/JsHnm ToFIyv7HNviLf3CBWiGXJygjhVOffQ1MuEzjMX0wez4LeLM1iYFVQ8rrWRiLRxPdzkXgSh9l/sl Gr3CZYIYl/etNDS4G5hJa5QvdKdyhF8o74Q== X-Received: by 2002:a17:90b:17cd:b0:3b4:9d6:5be1 with SMTP id 98e67ed59e1d1-3b41c93b187mr54647407a91.4.1796049700618; Mon, 30 Nov 2026 06:41:40 -0800 (PST) Received: from chenyou.. ([140.116.39.229]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b40a878d50sm55726664a91.11.2026.11.30.06.41.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 06:41:39 -0800 (PST) From: Chen-Yu Yeh To: hminas@synopsys.com, gregkh@linuxfoundation.org Cc: inochiama@gmail.com, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, sophgo@lists.linux.dev, Chen-Yu Yeh Subject: [PATCH] usb: dwc2: Skip HCD PHY setup when a role switch is used Date: Mon, 30 Nov 2026 22:41:32 +0800 Message-ID: <20261130144132.619573-1-chenyou910331@gmail.com> X-Mailer: git-send-email 2.43.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit When a USB role switch is registered, dwc2_drd_init() sets the PHY mode according to the default role. Later in probe, dwc2_hcd_init() calls usb_add_hcd(), which initializes the PHY again and unconditionally sets it to host mode. This overrides the role chosen at boot. With role-switch-default-mode = "peripheral", the controller is forced into device mode while the PHY is left in host mode, so the gadget is never enumerated by the remote host: phy phy-3000000.syscon:phy@48.0: set mode 6 <- dwc2_drd_init() phy phy-3000000.syscon:phy@48.0: set mode 5 <- usb_add_hcd() phy phy-3000000.syscon:phy@48.0: set mode 1 <- usb_add_hcd() Set skip_phy_initialization when a role switch is present, so the DRD code is the only place that sets the PHY mode. This is safe because dwc2_lowlevel_hw_enable() already initializes and powers on the PHY earlier in probe. Without a role switch, the behaviour is unchanged. Tested on Milk-V Duo 256M (SG2002). Besides the "set mode" messages, the PHY state was read from the USB PHY control register 0x03000048: 0x3c0 means the ID is overridden to device, 0x142 means the ID is overridden to host with VBUS enabled. 1) role-switch-default-mode = "peripheral", Duo connected to a PC Before this patch: set mode 6, 5, 1 0x03000048 = 0x142 the PC does not see any gadget After this patch: set mode 6 0x03000048 = 0x3c0 the PC sees the g_ether gadget: ID 0525:a4a2 Netchip Technology, Inc. Linux-USB Ethernet/RNDIS Gadget 2) role-switch-default-mode = "host", Duo on the IO board, USB stick plugged in before boot After this patch: set mode 1 0x03000048 = 0x142 usb 1-1: new high-speed USB device number 2 using dwc2 hub 1-1:1.0: 4 ports detected usb 1-1.1: new high-speed USB device number 3 using dwc2 sda: sda1 sda2 sda3 sda4 Unplugging and replugging the stick enumerates it again at high speed. 3) Role changes through sysfs, same setup as 1), three rounds echo host: set mode 1, 0x03000048 = 0x142 echo device: set mode 6, 0x03000048 = 0x3c0, UDC "configured" echo device while already in device: set mode 6, 0x3c0 echo none: 0x03000048 = 0x3c0 (default role) 4) dr_mode = "host" without usb-role-switch, Duo on the IO board (regression test, role_sw is NULL so nothing is skipped) set mode 5, 1 (still set by usb_add_hcd()) 0x03000048 = 0x142 /sys/class/usb_role/ is empty hub and USB stick enumerated at high speed, sda1-sda4 present --- This is the fix for the boot role problem you mentioned in the cover letter of "[PATCH 0/4] usb: dwc2: Add USB DRD support on CV1800 Series SoC" [1] and asked me to send [2]. It applies on top of that series. It also needs the fix for the inverted is_host check in patch 2/4 ("usb: dwc2: drd: Sync the generic PHY mode with role requests"), which sashiko-bot reported and you agreed to fix in the next version [3]. In dwc2_drd_init(), v1 has: if (hsotg->role_sw_default_mode != USB_DR_MODE_UNKNOWN) is_host = hsotg->dr_mode == USB_DR_MODE_HOST; else is_host = hsotg->role_sw_default_mode == USB_DR_MODE_HOST; All the tests above were run with the two branches swapped: if (hsotg->role_sw_default_mode != USB_DR_MODE_UNKNOWN) is_host = hsotg->role_sw_default_mode == USB_DR_MODE_HOST; else is_host = hsotg->dr_mode == USB_DR_MODE_HOST; Until now the inverted check was hidden, because usb_add_hcd() set the PHY to host mode right after. With this patch but without that fix, role-switch-default-mode = "host" on the IO board gives: set mode 6 0x03000048 = 0x3c0 usb 1-1: new full-speed USB device number 2 using dwc2 usb 1-1: not running at top speed; connect to a high speed hub usb 1-1.1: new full-speed USB device number 3 using dwc2 usb 1-1.1: not running at top speed; connect to a high speed hub The controller is forced to host by dwc2_ovr_init(), but the PHY is in device mode, so devices only come up at full speed. With the fix, the same setup runs at high speed (test 2 above). With this patch, role-switch-default-mode in the DT takes effect at boot. About the Duo 256M DTS: you offered to add it if it defaults to host [2], and I replied that I would prefer peripheral [5]. Now that both defaults work at boot, I will leave the choice between "peripheral" and "host" to you, since you know this platform better. Either way, the DTS change will go as a separate patch, not mixed with the USB patches [4]. I am happy to send it, or you can add it with your DTS patch, whichever you prefer. [1] https://lore.kernel.org/all/20261110070627.217946-1-inochiama@gmail.com/ [2] https://lore.kernel.org/all/av2M-6w1u5mOyqDM@inochi.infowork/ [3] https://lore.kernel.org/all/avLHFUl6S3BUOqeI@inochi.infowork/ [4] https://lore.kernel.org/all/2e878ad2-1a20-4ee7-8460-64e0d9774ff5@kernel.org/ [5] https://lore.kernel.org/all/CAKspUh+LLonm90ywMGrRUcsLDE+Axw64qjs0OtDB_ySUsrPPQw@mail.gmail.com/ Signed-off-by: Chen-Yu Yeh --- drivers/usb/dwc2/hcd.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drivers/usb/dwc2/hcd.c b/drivers/usb/dwc2/hcd.c index d5275cd658d4..4425596e6eb6 100644 --- a/drivers/usb/dwc2/hcd.c +++ b/drivers/usb/dwc2/hcd.c @@ -5321,6 +5321,16 @@ int dwc2_hcd_init(struct dwc2_hsotg *hsotg) if (!IS_ERR_OR_NULL(hsotg->uphy)) otg_set_host(hsotg->uphy->otg, &hcd->self); + /* + * With a role switch, the PHY mode is set by the dwc2 DRD code + * according to the role. Without this flag, usb_add_hcd() would + * set the PHY to host mode and override the role chosen at boot. + * Skipping is safe because dwc2_lowlevel_hw_enable() already + * initializes and powers on the PHY. + */ + if (hsotg->role_sw) + hcd->skip_phy_initialization = 1; + /* * Finish generic HCD initialization and start the HCD. This function * allocates the DMA buffer pool, registers the USB bus, requests the -- 2.43.0