Siddharth Vadapalli bfb530e06c usb: cdns3: use VBUS Valid to determine role for dr_mode OTG
The cdns3_bind() function is responsible for identifying the appropriate
driver to bind to the USB Controller's device-tree node. If the device-tree
node has the 'dr_mode' property set to 'otg', the existing approach fails
to bind a driver, leading to loss of functionality.

To address this, use the VBUS Valid field of the OTG Status register to
determine the role as follows:
- If VBUS Valid field is set, it indicates that a USB Host is supplying
  power and the Controller should assume the Peripheral role.
- If VBUS Valid field is clear, it indicates the absence of a USB Host and
  the Controller should assume the Host role.

Additionally, when 'dr_mode' happens to be 'otg' and the STRAP settings
are not specified, use VBUS Valid to determine the role in cdns3_drd_init()
and assign it to cdns->dr_mode.

Signed-off-by: Siddharth Vadapalli <s-vadapalli@ti.com>
Reviewed-by: Marek Vasut <marek.vasut@mailbox.org>
2026-02-16 15:08:43 +01:00

587 lines
13 KiB
C

// SPDX-License-Identifier: GPL-2.0
/*
* Cadence USBSS DRD Driver.
*
* Copyright (C) 2018-2019 Cadence.
* Copyright (C) 2017-2018 NXP
* Copyright (C) 2019 Texas Instruments
*
* Author: Peter Chen <peter.chen@nxp.com>
* Pawel Laszczak <pawell@cadence.com>
* Roger Quadros <rogerq@ti.com>
*/
#include <dm.h>
#include <log.h>
#include <dm/device-internal.h>
#include <dm/device_compat.h>
#include <dm/devres.h>
#include <dm/lists.h>
#include <linux/bug.h>
#include <linux/kernel.h>
#include <linux/io.h>
#include <linux/usb/gadget.h>
#include <usb.h>
#include <usb/xhci.h>
#include "core.h"
#include "host-export.h"
#include "gadget-export.h"
#include "drd.h"
static int cdns3_idle_init(struct cdns3 *cdns);
struct cdns3_host_priv {
struct xhci_ctrl xhci_ctrl;
struct cdns3 cdns;
};
struct cdns3_gadget_priv {
struct cdns3 cdns;
};
static inline
struct cdns3_role_driver *cdns3_get_current_role_driver(struct cdns3 *cdns)
{
WARN_ON(!cdns->roles[cdns->role]);
return cdns->roles[cdns->role];
}
static int cdns3_role_start(struct cdns3 *cdns, enum usb_role role)
{
int ret;
if (WARN_ON(role > USB_ROLE_DEVICE))
return 0;
mutex_lock(&cdns->mutex);
cdns->role = role;
mutex_unlock(&cdns->mutex);
if (!cdns->roles[role])
return -ENXIO;
if (cdns->roles[role]->state == CDNS3_ROLE_STATE_ACTIVE)
return 0;
mutex_lock(&cdns->mutex);
ret = cdns->roles[role]->start(cdns);
if (!ret)
cdns->roles[role]->state = CDNS3_ROLE_STATE_ACTIVE;
mutex_unlock(&cdns->mutex);
return ret;
}
static void cdns3_role_stop(struct cdns3 *cdns)
{
enum usb_role role = cdns->role;
if (WARN_ON(role > USB_ROLE_DEVICE))
return;
if (cdns->roles[role]->state == CDNS3_ROLE_STATE_INACTIVE)
return;
mutex_lock(&cdns->mutex);
cdns->roles[role]->stop(cdns);
cdns->roles[role]->state = CDNS3_ROLE_STATE_INACTIVE;
mutex_unlock(&cdns->mutex);
}
static void cdns3_exit_roles(struct cdns3 *cdns)
{
cdns3_role_stop(cdns);
cdns3_drd_exit(cdns);
}
static enum usb_role cdsn3_hw_role_state_machine(struct cdns3 *cdns);
/**
* cdns3_core_init_role - initialize role of operation
* @cdns: Pointer to cdns3 structure
*
* Returns 0 on success otherwise negative errno
*/
static int cdns3_core_init_role(struct cdns3 *cdns)
{
struct udevice *dev = cdns->dev;
enum usb_dr_mode best_dr_mode;
enum usb_dr_mode dr_mode;
int ret = 0;
dr_mode = usb_get_dr_mode(dev_ofnode(dev));
cdns->role = USB_ROLE_NONE;
/*
* If driver can't read mode by means of usb_get_dr_mode function then
* chooses mode according with Kernel configuration. This setting
* can be restricted later depending on strap pin configuration.
*/
if (dr_mode == USB_DR_MODE_UNKNOWN) {
if (IS_ENABLED(CONFIG_USB_CDNS3_HOST) &&
IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
dr_mode = USB_DR_MODE_OTG;
else if (IS_ENABLED(CONFIG_USB_CDNS3_HOST))
dr_mode = USB_DR_MODE_HOST;
else if (IS_ENABLED(CONFIG_USB_CDNS3_GADGET))
dr_mode = USB_DR_MODE_PERIPHERAL;
}
/*
* At this point cdns->dr_mode contains strap configuration.
* Driver try update this setting considering kernel configuration
*/
best_dr_mode = cdns->dr_mode;
ret = cdns3_idle_init(cdns);
if (ret)
return ret;
if (dr_mode == USB_DR_MODE_OTG) {
best_dr_mode = cdns->dr_mode;
} else if (cdns->dr_mode == USB_DR_MODE_OTG) {
best_dr_mode = dr_mode;
} else if (cdns->dr_mode != dr_mode) {
dev_err(dev, "Incorrect DRD configuration\n");
return -EINVAL;
}
dr_mode = best_dr_mode;
#if defined(CONFIG_SPL_USB_HOST) || !defined(CONFIG_XPL_BUILD)
if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
ret = cdns3_host_init(cdns);
if (ret) {
dev_err(dev, "Host initialization failed with %d\n",
ret);
goto err;
}
}
#endif
#if CONFIG_IS_ENABLED(DM_USB_GADGET)
if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
ret = cdns3_gadget_init(cdns);
if (ret) {
dev_err(dev, "Device initialization failed with %d\n",
ret);
goto err;
}
}
#endif
cdns->dr_mode = dr_mode;
ret = cdns3_drd_update_mode(cdns);
if (ret)
goto err;
if (cdns->dr_mode != USB_DR_MODE_OTG) {
ret = cdns3_hw_role_switch(cdns);
if (ret)
goto err;
}
return ret;
err:
cdns3_exit_roles(cdns);
return ret;
}
/**
* cdsn3_hw_role_state_machine - role switch state machine based on hw events
* @cdns: Pointer to controller structure.
*
* Returns next role to be entered based on hw events.
*/
static enum usb_role cdsn3_hw_role_state_machine(struct cdns3 *cdns)
{
enum usb_role role;
int id, vbus;
if (cdns->dr_mode != USB_DR_MODE_OTG)
goto not_otg;
id = cdns3_get_id(cdns);
vbus = cdns3_get_vbus(cdns);
/*
* Role change state machine
* Inputs: ID, VBUS
* Previous state: cdns->role
* Next state: role
*/
role = cdns->role;
switch (role) {
case USB_ROLE_NONE:
/*
* Driver treats USB_ROLE_NONE synonymous to IDLE state from
* controller specification.
*/
if (!id)
role = USB_ROLE_HOST;
else if (vbus)
role = USB_ROLE_DEVICE;
break;
case USB_ROLE_HOST: /* from HOST, we can only change to NONE */
if (id)
role = USB_ROLE_NONE;
break;
case USB_ROLE_DEVICE: /* from GADGET, we can only change to NONE*/
if (!vbus)
role = USB_ROLE_NONE;
break;
}
dev_dbg(cdns->dev, "role %d -> %d\n", cdns->role, role);
return role;
not_otg:
if (cdns3_is_host(cdns))
role = USB_ROLE_HOST;
if (cdns3_is_device(cdns))
role = USB_ROLE_DEVICE;
return role;
}
static int cdns3_idle_role_start(struct cdns3 *cdns)
{
return 0;
}
static void cdns3_idle_role_stop(struct cdns3 *cdns)
{
/* Program Lane swap and bring PHY out of RESET */
generic_phy_reset(&cdns->usb3_phy);
}
static int cdns3_idle_init(struct cdns3 *cdns)
{
struct cdns3_role_driver *rdrv;
rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
if (!rdrv)
return -ENOMEM;
rdrv->start = cdns3_idle_role_start;
rdrv->stop = cdns3_idle_role_stop;
rdrv->state = CDNS3_ROLE_STATE_INACTIVE;
rdrv->suspend = NULL;
rdrv->resume = NULL;
rdrv->name = "idle";
cdns->roles[USB_ROLE_NONE] = rdrv;
return 0;
}
/**
* cdns3_hw_role_switch - switch roles based on HW state
* @cdns3: controller
*/
int cdns3_hw_role_switch(struct cdns3 *cdns)
{
enum usb_role real_role, current_role;
int ret = 0;
/* Do nothing if role based on syfs. */
if (cdns->role_override)
return 0;
current_role = cdns->role;
real_role = cdsn3_hw_role_state_machine(cdns);
/* Do nothing if nothing changed */
if (current_role == real_role)
goto exit;
cdns3_role_stop(cdns);
dev_dbg(cdns->dev, "Switching role %d -> %d", current_role, real_role);
ret = cdns3_role_start(cdns, real_role);
if (ret) {
/* Back to current role */
dev_err(cdns->dev, "set %d has failed, back to %d\n",
real_role, current_role);
ret = cdns3_role_start(cdns, current_role);
if (ret)
dev_err(cdns->dev, "back to %d failed too\n",
current_role);
}
exit:
return ret;
}
static int cdns3_probe(struct cdns3 *cdns)
{
struct udevice *dev = cdns->dev;
int ret;
cdns->xhci_regs = dev_remap_addr_name(dev, "xhci");
if (!cdns->xhci_regs)
return -EINVAL;
cdns->dev_regs = dev_remap_addr_name(dev, "dev");
if (!cdns->dev_regs)
return -EINVAL;
mutex_init(&cdns->mutex);
ret = generic_phy_get_by_name(dev, "cdns3,usb2-phy", &cdns->usb2_phy);
if (!ret) {
ret = generic_phy_init(&cdns->usb2_phy);
if (ret) {
dev_err(dev, "USB2 PHY init failed: %d\n", ret);
return ret;
}
} else if (ret != -ENOENT && ret != -ENODATA) {
dev_err(dev, "Couldn't get USB2 PHY: %d\n", ret);
return ret;
}
ret = generic_phy_get_by_name(dev, "cdns3,usb3-phy", &cdns->usb3_phy);
if (!ret) {
ret = generic_phy_init(&cdns->usb3_phy);
if (ret) {
dev_err(dev, "USB3 PHY init failed: %d\n", ret);
return ret;
}
} else if (ret != -ENOENT && ret != -ENODATA) {
dev_err(dev, "Couldn't get USB3 PHY: %d\n", ret);
return ret;
}
ret = generic_phy_power_on(&cdns->usb2_phy);
if (ret)
return ret;
ret = generic_phy_power_on(&cdns->usb3_phy);
if (ret)
return ret;
ret = cdns3_drd_init(cdns);
if (ret)
return ret;
ret = cdns3_core_init_role(cdns);
if (ret)
return ret;
dev_dbg(dev, "Cadence USB3 core: probe succeed\n");
return 0;
}
static int cdns3_remove(struct cdns3 *cdns)
{
cdns3_exit_roles(cdns);
generic_phy_power_off(&cdns->usb2_phy);
generic_phy_power_off(&cdns->usb3_phy);
generic_phy_exit(&cdns->usb2_phy);
generic_phy_exit(&cdns->usb3_phy);
return 0;
}
static const struct udevice_id cdns3_ids[] = {
{ .compatible = "cdns,usb3" },
{ },
};
/*
* The VBUS Valid Bit in the OTG Status register can be used to determine
* the role. When VBUS Valid is set, it indicates that a USB Host is supplying
* power, so the Controller should assume the PERIPHERAL role. If it isn't set,
* it indicates the absence of a USB Host, so the Controller should assume the
* HOST role. If the OTG Status register is inaccessible, return an error.
*/
static int cdns3_get_otg_mode(struct udevice *parent, enum usb_dr_mode *mode)
{
/* Create a temporary child device for using devfdt_remap_addr_name() */
struct udevice child = {
.parent = parent,
};
struct cdns3 cdns, *cdnsp;
void __iomem *otg_regs;
dev_set_ofnode(&child, ofnode_first_subnode(dev_ofnode(parent)));
otg_regs = devfdt_remap_addr_name(&child, "otg");
if (!otg_regs) {
dev_err(parent, "failed to get otg registers for child node\n");
return -ENXIO;
}
/*
* As mentioned in drivers/usb/cdns3/drd.c, there are two versions
* of the Controller. The following logic detects the version of the
* Controller and interprets the register layout accordingly.
*/
cdnsp = &cdns;
cdnsp->otg_v0_regs = otg_regs;
if (!readl(&cdnsp->otg_v0_regs->cmd)) {
cdnsp->otg_regs = otg_regs;
} else {
cdnsp->otg_v1_regs = otg_regs;
cdnsp->otg_regs = (void *)&cdnsp->otg_v1_regs->cmd;
}
/* Use VBUS Valid to determine role */
if (readl(&cdnsp->otg_regs->sts) & OTGSTS_VBUS_VALID)
*mode = USB_DR_MODE_PERIPHERAL;
else
*mode = USB_DR_MODE_HOST;
return 0;
}
int cdns3_bind(struct udevice *parent)
{
enum usb_dr_mode dr_mode;
struct udevice *dev;
const char *driver;
const char *name;
ofnode node;
int ret;
node = ofnode_by_compatible(dev_ofnode(parent), "cdns,usb3");
if (!ofnode_valid(node)) {
ret = -ENODEV;
goto fail;
}
name = ofnode_get_name(node);
dr_mode = usb_get_dr_mode(node);
if (dr_mode == USB_DR_MODE_UNKNOWN)
dr_mode = usb_get_dr_mode(dev_ofnode(parent));
/* Use VBUS Valid to determine role */
if (dr_mode == USB_DR_MODE_OTG) {
ret = cdns3_get_otg_mode(parent, &dr_mode);
if (ret < 0)
return ret;
}
switch (dr_mode) {
#if defined(CONFIG_SPL_USB_HOST) || \
(!defined(CONFIG_XPL_BUILD) && defined(CONFIG_USB_HOST))
case USB_DR_MODE_HOST:
debug("%s: dr_mode: HOST\n", __func__);
driver = "cdns-usb3-host";
break;
#endif
#if CONFIG_IS_ENABLED(DM_USB_GADGET)
case USB_DR_MODE_PERIPHERAL:
debug("%s: dr_mode: PERIPHERAL\n", __func__);
driver = "cdns-usb3-peripheral";
break;
#endif
default:
printf("%s: unsupported dr_mode\n", __func__);
ret = -ENODEV;
goto fail;
};
ret = device_bind_driver_to_node(parent, driver, name, node, &dev);
if (ret) {
printf("%s: not able to bind usb device mode\n",
__func__);
goto fail;
}
return 0;
fail:
/* do not return an error: failing to bind would hang the board */
return 0;
}
#if CONFIG_IS_ENABLED(DM_USB_GADGET)
static int cdns3_gadget_probe(struct udevice *dev)
{
struct cdns3_gadget_priv *priv = dev_get_priv(dev);
struct cdns3 *cdns = &priv->cdns;
cdns->dev = dev;
return cdns3_probe(cdns);
}
static int cdns3_gadget_remove(struct udevice *dev)
{
struct cdns3_gadget_priv *priv = dev_get_priv(dev);
struct cdns3 *cdns = &priv->cdns;
return cdns3_remove(cdns);
}
static int cdns3_gadget_handle_interrupts(struct udevice *dev)
{
struct cdns3 *cdns = dev_get_priv(dev);
cdns3_gadget_uboot_handle_interrupt(cdns);
return 0;
}
static const struct usb_gadget_generic_ops cdns3_gadget_ops = {
.handle_interrupts = cdns3_gadget_handle_interrupts,
};
U_BOOT_DRIVER(cdns_usb3_peripheral) = {
.name = "cdns-usb3-peripheral",
.id = UCLASS_USB_GADGET_GENERIC,
.of_match = cdns3_ids,
.ops = &cdns3_gadget_ops,
.probe = cdns3_gadget_probe,
.remove = cdns3_gadget_remove,
.priv_auto = sizeof(struct cdns3_gadget_priv),
.flags = DM_FLAG_ALLOC_PRIV_DMA,
};
#else
int dm_usb_gadget_handle_interrupts(struct udevice *dev)
{
struct cdns3 *cdns = dev_get_priv(dev);
cdns3_gadget_uboot_handle_interrupt(cdns);
return 0;
}
#endif
#if defined(CONFIG_SPL_USB_HOST) || \
(!defined(CONFIG_XPL_BUILD) && defined(CONFIG_USB_HOST))
static int cdns3_host_probe(struct udevice *dev)
{
struct cdns3_host_priv *priv = dev_get_priv(dev);
struct cdns3 *cdns = &priv->cdns;
cdns->dev = dev;
return cdns3_probe(cdns);
}
static int cdns3_host_remove(struct udevice *dev)
{
struct cdns3_host_priv *priv = dev_get_priv(dev);
struct cdns3 *cdns = &priv->cdns;
return cdns3_remove(cdns);
}
U_BOOT_DRIVER(cdns_usb3_host) = {
.name = "cdns-usb3-host",
.id = UCLASS_USB,
.of_match = cdns3_ids,
.probe = cdns3_host_probe,
.remove = cdns3_host_remove,
.priv_auto = sizeof(struct cdns3_host_priv),
.ops = &xhci_usb_ops,
.flags = DM_FLAG_ALLOC_PRIV_DMA,
};
#endif