On some board, the ID pin is not connected so the B session must be
overridden with "u-boot,force_b_session_valid" but the VBus sensing
must continue to be handle.

To managed it, this patch adds a new DT field
"u-boot,force-vbus-detection" to use with "u-boot,force_b_session_valid"

Signed-off-by: Patrick Delaunay <patrick.delau...@st.com>
---

 drivers/usb/gadget/dwc2_udc_otg.c      | 59 +++++++++++++++++---------
 drivers/usb/gadget/dwc2_udc_otg_regs.h |  2 +
 include/usb/dwc2_udc.h                 |  1 +
 3 files changed, 41 insertions(+), 21 deletions(-)

diff --git a/drivers/usb/gadget/dwc2_udc_otg.c 
b/drivers/usb/gadget/dwc2_udc_otg.c
index eaa5dcb9b1..d20ce6147e 100644
--- a/drivers/usb/gadget/dwc2_udc_otg.c
+++ b/drivers/usb/gadget/dwc2_udc_otg.c
@@ -1014,6 +1014,9 @@ static int dwc2_udc_otg_ofdata_to_platdata(struct udevice 
*dev)
        platdata->force_b_session_valid =
                dev_read_bool(dev, "u-boot,force-b-session-valid");
 
+       platdata->force_vbus_detection =
+               dev_read_bool(dev, "u-boot,force-vbus-detection");
+
        /* force platdata according compatible */
        drvdata = dev_get_driver_data(dev);
        if (drvdata) {
@@ -1106,31 +1109,45 @@ static int dwc2_udc_otg_probe(struct udevice *dev)
        if (ret)
                return ret;
 
-       if (CONFIG_IS_ENABLED(DM_REGULATOR) &&
-           platdata->activate_stm_id_vb_detection &&
-           !platdata->force_b_session_valid) {
-               ret = device_get_supply_regulator(dev, "usb33d-supply",
-                                                 &priv->usb33d_supply);
-               if (ret) {
-                       dev_err(dev, "can't get voltage level detector 
supply\n");
-                       return ret;
+       if (platdata->activate_stm_id_vb_detection) {
+               if (CONFIG_IS_ENABLED(DM_REGULATOR) &&
+                   (!platdata->force_b_session_valid ||
+                    platdata->force_vbus_detection)) {
+                       ret = device_get_supply_regulator(dev, "usb33d-supply",
+                                                         &priv->usb33d_supply);
+                       if (ret) {
+                               dev_err(dev, "can't get voltage level detector 
supply\n");
+                               return ret;
+                       }
+                       ret = regulator_set_enable(priv->usb33d_supply, true);
+                       if (ret) {
+                               dev_err(dev, "can't enable voltage level 
detector supply\n");
+                               return ret;
+                       }
                }
-               ret = regulator_set_enable(priv->usb33d_supply, true);
-               if (ret) {
-                       dev_err(dev, "can't enable voltage level detector 
supply\n");
-                       return ret;
+
+               if (platdata->force_b_session_valid &&
+                   !platdata->force_vbus_detection) {
+                       /* Override VBUS detection: enable then value*/
+                       setbits_le32(&usbotg_reg->gotgctl, VB_VALOEN);
+                       setbits_le32(&usbotg_reg->gotgctl, VB_VALOVAL);
+               } else {
+                       /* Enable VBUS sensing */
+                       setbits_le32(&usbotg_reg->ggpio,
+                                    GGPIO_STM32_OTG_GCCFG_VBDEN);
+               }
+               if (platdata->force_b_session_valid) {
+                       /* Override B session bits: enable then value */
+                       setbits_le32(&usbotg_reg->gotgctl, A_VALOEN | B_VALOEN);
+                       setbits_le32(&usbotg_reg->gotgctl,
+                                    A_VALOVAL | B_VALOVAL);
+               } else {
+                       /* Enable ID detection */
+                       setbits_le32(&usbotg_reg->ggpio,
+                                    GGPIO_STM32_OTG_GCCFG_IDEN);
                }
-               /* Enable vbus sensing */
-               setbits_le32(&usbotg_reg->ggpio,
-                            GGPIO_STM32_OTG_GCCFG_VBDEN |
-                            GGPIO_STM32_OTG_GCCFG_IDEN);
        }
 
-       if (platdata->force_b_session_valid)
-               /* Override B session bits : value and enable */
-               setbits_le32(&usbotg_reg->gotgctl,
-                            A_VALOEN | A_VALOVAL | B_VALOEN | B_VALOVAL);
-
        ret = dwc2_udc_probe(platdata);
        if (ret)
                return ret;
diff --git a/drivers/usb/gadget/dwc2_udc_otg_regs.h 
b/drivers/usb/gadget/dwc2_udc_otg_regs.h
index 2eda5c3720..9ca6f42375 100644
--- a/drivers/usb/gadget/dwc2_udc_otg_regs.h
+++ b/drivers/usb/gadget/dwc2_udc_otg_regs.h
@@ -94,6 +94,8 @@ struct dwc2_usbotg_reg {
 #define B_VALOEN                       BIT(6)
 #define A_VALOVAL                      BIT(5)
 #define A_VALOEN                       BIT(4)
+#define VB_VALOVAL                     BIT(3)
+#define VB_VALOEN                      BIT(2)
 
 /* DWC2_UDC_OTG_GOTINT */
 #define GOTGINT_SES_END_DET            (1<<2)
diff --git a/include/usb/dwc2_udc.h b/include/usb/dwc2_udc.h
index a2af381a66..aa37e957b4 100644
--- a/include/usb/dwc2_udc.h
+++ b/include/usb/dwc2_udc.h
@@ -28,6 +28,7 @@ struct dwc2_plat_otg_data {
        unsigned int    tx_fifo_sz_array[DWC2_MAX_HW_ENDPOINTS];
        unsigned char   tx_fifo_sz_nb;
        bool            force_b_session_valid;
+       bool            force_vbus_detection;
        bool            activate_stm_id_vb_detection;
 };
 
-- 
2.17.1

Reply via email to