Commit e09b88cd authored by Chunfeng Yun's avatar Chunfeng Yun Committed by Marek Vasut

usb: add MediaTek USB3 DRD driver

This patch adds support for the MediaTek USB3 DRD controller,
its host side is based on xHCI, this driver supports device mode
and host mode.
Signed-off-by: default avatarChunfeng Yun <chunfeng.yun@mediatek.com>
Acked-by: Bin Meng's avatarBin Meng <bmeng.cn@gmail.com>
parent d92e866f
......@@ -795,6 +795,7 @@ libs-y += drivers/usb/eth/
libs-$(CONFIG_USB_GADGET) += drivers/usb/gadget/
libs-$(CONFIG_USB_GADGET) += drivers/usb/gadget/udc/
libs-y += drivers/usb/host/
libs-y += drivers/usb/mtu3/
libs-y += drivers/usb/musb/
libs-y += drivers/usb/musb-new/
libs-y += drivers/usb/phy/
......
......@@ -72,6 +72,8 @@ source "drivers/usb/cdns3/Kconfig"
source "drivers/usb/dwc3/Kconfig"
source "drivers/usb/mtu3/Kconfig"
source "drivers/usb/musb/Kconfig"
source "drivers/usb/musb-new/Kconfig"
......
# SPDX-License-Identifier: GPL-2.0
#
# For MTK USB3.0 IP
config USB_MTU3
bool "MediaTek USB3 Dual Role controller"
depends on USB_HOST || USB_GADGET
depends on ARCH_MEDIATEK
help
Say Y here if your system runs on MediaTek SoCs with
Dual Role SuperSpeed USB controller. You can select usb
mode as peripheral role or host role.
If you don't know what this is, please say N.
if USB_MTU3
choice
bool "MTU3 Mode Selection"
default USB_MTU3_GADGET if USB_GADGET
default USB_MTU3_HOST if (USB_HOST && !USB_GADGET)
config USB_MTU3_HOST
bool "Host only mode"
depends on USB_XHCI_HCD
help
Select this when you want to use MTU3 in host mode only,
thereby the gadget feature will be regressed.
config USB_MTU3_GADGET
bool "Gadget only mode"
depends on USB_GADGET
select USB_GADGET_DUALSPEED
help
Select this when you want to use MTU3 in gadget mode only,
thereby the host feature will be regressed.
endchoice
config USB_MTU3_DEBUG
bool "Enable Debugging Messages"
help
Say Y here to enable debugging messages in the MTU3 Driver.
endif
# SPDX-License-Identifier: GPL-2.0
ccflags-$(CONFIG_USB_MTU3_DEBUG) += -DDEBUG
obj-$(CONFIG_USB_MTU3) += mtu3.o
mtu3-y := mtu3_plat.o
obj-$(CONFIG_USB_MTU3_GADGET) += mtu3_core.o mtu3_gadget_ep0.o mtu3_gadget.o
obj-$(CONFIG_USB_MTU3_GADGET) += mtu3_qmu.o
obj-$(CONFIG_USB_MTU3_HOST) += mtu3_host.o
This diff is collapsed.
This diff is collapsed.
/* SPDX-License-Identifier: GPL-2.0 */
/*
* mtu3_dr.h - dual role switch and host glue layer header
*
* Copyright (C) 2016 MediaTek Inc.
*
* Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
*/
#ifndef _MTU3_DR_H_
#define _MTU3_DR_H_
#if IS_ENABLED(CONFIG_USB_MTU3_HOST)
int ssusb_host_init(struct ssusb_mtk *ssusb);
void ssusb_host_exit(struct ssusb_mtk *ssusb);
#else
static inline int ssusb_host_init(struct ssusb_mtk *ssusb)
{
return 0;
}
static inline void ssusb_host_exit(struct ssusb_mtk *ssusb)
{}
#endif
#if IS_ENABLED(CONFIG_USB_MTU3_GADGET)
int ssusb_gadget_init(struct ssusb_mtk *ssusb);
void ssusb_gadget_exit(struct ssusb_mtk *ssusb);
irqreturn_t mtu3_irq(int irq, void *data);
#else
static inline int ssusb_gadget_init(struct ssusb_mtk *ssusb)
{
return 0;
}
static inline void ssusb_gadget_exit(struct ssusb_mtk *ssusb)
{}
static inline irqreturn_t mtu3_irq(int irq, void *data)
{
return IRQ_NONE;
}
#endif
void ssusb_set_force_mode(struct ssusb_mtk *ssusb,
enum mtu3_dr_force_mode mode);
#endif /* _MTU3_DR_H_ */
This diff is collapsed.
This diff is collapsed.
// SPDX-License-Identifier: GPL-2.0
/*
* mtu3_dr.c - dual role switch and host glue layer
*
* Copyright (C) 2016 MediaTek Inc.
*
* Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
*/
#include <dm/lists.h>
#include <linux/iopoll.h>
#include "mtu3.h"
#include "mtu3_dr.h"
static void host_ports_num_get(struct mtu3_host *u3h)
{
u32 xhci_cap;
xhci_cap = mtu3_readl(u3h->ippc_base, U3D_SSUSB_IP_XHCI_CAP);
u3h->u2_ports = SSUSB_IP_XHCI_U2_PORT_NUM(xhci_cap);
u3h->u3_ports = SSUSB_IP_XHCI_U3_PORT_NUM(xhci_cap);
dev_dbg(u3h->dev, "host - u2_ports:%d, u3_ports:%d\n",
u3h->u2_ports, u3h->u3_ports);
}
/* only configure ports will be used later */
static int ssusb_host_enable(struct mtu3_host *u3h)
{
void __iomem *ibase = u3h->ippc_base;
int num_u3p = u3h->u3_ports;
int num_u2p = u3h->u2_ports;
int u3_ports_disabed;
u32 check_clk;
u32 value;
int i;
/* power on host ip */
mtu3_clrbits(ibase, U3D_SSUSB_IP_PW_CTRL1, SSUSB_IP_HOST_PDN);
/* power on and enable u3 ports except skipped ones */
u3_ports_disabed = 0;
for (i = 0; i < num_u3p; i++) {
if ((0x1 << i) & u3h->u3p_dis_msk) {
u3_ports_disabed++;
continue;
}
value = mtu3_readl(ibase, SSUSB_U3_CTRL(i));
value &= ~(SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS);
value |= SSUSB_U3_PORT_HOST_SEL;
mtu3_writel(ibase, SSUSB_U3_CTRL(i), value);
}
/* power on and enable all u2 ports */
for (i = 0; i < num_u2p; i++) {
value = mtu3_readl(ibase, SSUSB_U2_CTRL(i));
value &= ~(SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS);
value |= SSUSB_U2_PORT_HOST_SEL;
mtu3_writel(ibase, SSUSB_U2_CTRL(i), value);
}
check_clk = SSUSB_XHCI_RST_B_STS;
if (num_u3p > u3_ports_disabed)
check_clk = SSUSB_U3_MAC_RST_B_STS;
return ssusb_check_clocks(u3h->ssusb, check_clk);
}
static void ssusb_host_disable(struct mtu3_host *u3h)
{
void __iomem *ibase = u3h->ippc_base;
int num_u3p = u3h->u3_ports;
int num_u2p = u3h->u2_ports;
u32 value;
int i;
/* power down and disable u3 ports except skipped ones */
for (i = 0; i < num_u3p; i++) {
if ((0x1 << i) & u3h->u3p_dis_msk)
continue;
value = mtu3_readl(ibase, SSUSB_U3_CTRL(i));
value |= SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS;
mtu3_writel(ibase, SSUSB_U3_CTRL(i), value);
}
/* power down and disable all u2 ports */
for (i = 0; i < num_u2p; i++) {
value = mtu3_readl(ibase, SSUSB_U2_CTRL(i));
value |= SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS;
mtu3_writel(ibase, SSUSB_U2_CTRL(i), value);
}
/* power down host ip */
mtu3_setbits(ibase, U3D_SSUSB_IP_PW_CTRL1, SSUSB_IP_HOST_PDN);
}
/*
* If host supports multiple ports, the VBUSes(5V) of ports except port0
* which supports OTG are better to be enabled by default in DTS.
* Because the host driver will keep link with devices attached when system
* enters suspend mode, so no need to control VBUSes after initialization.
*/
int ssusb_host_init(struct ssusb_mtk *ssusb)
{
struct mtu3_host *u3h = ssusb->u3h;
struct udevice *dev = u3h->dev;
int ret;
u3h->ssusb = ssusb;
u3h->hcd = ssusb->mac_base;
u3h->ippc_base = ssusb->ippc_base;
/* optional property, ignore the error */
dev_read_u32(dev, "mediatek,u3p-dis-msk", &u3h->u3p_dis_msk);
host_ports_num_get(u3h);
ret = ssusb_host_enable(u3h);
if (ret)
return ret;
ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_HOST);
ret = regulator_set_enable(ssusb->vbus_supply, true);
if (ret < 0 && ret != -ENOSYS) {
dev_err(dev, "failed to enable vbus %d!\n", ret);
return ret;
}
dev_info(dev, "%s done...\n", __func__);
return 0;
}
void ssusb_host_exit(struct ssusb_mtk *ssusb)
{
regulator_set_enable(ssusb->vbus_supply, false);
ssusb_host_disable(ssusb->u3h);
}
This diff is collapsed.
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2016 MediaTek Inc.
*
* Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
*/
#include <common.h>
#include <dm/lists.h>
#include <linux/iopoll.h>
#include "mtu3.h"
#include "mtu3_dr.h"
void ssusb_set_force_mode(struct ssusb_mtk *ssusb,
enum mtu3_dr_force_mode mode)
{
u32 value;
value = mtu3_readl(ssusb->ippc_base, SSUSB_U2_CTRL(0));
switch (mode) {
case MTU3_DR_FORCE_DEVICE:
value |= SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG;
break;
case MTU3_DR_FORCE_HOST:
value |= SSUSB_U2_PORT_FORCE_IDDIG;
value &= ~SSUSB_U2_PORT_RG_IDDIG;
break;
case MTU3_DR_FORCE_NONE:
value &= ~(SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG);
break;
default:
return;
}
mtu3_writel(ssusb->ippc_base, SSUSB_U2_CTRL(0), value);
}
/* u2-port0 should be powered on and enabled; */
int ssusb_check_clocks(struct ssusb_mtk *ssusb, u32 ex_clks)
{
void __iomem *ibase = ssusb->ippc_base;
u32 value, check_val;
int ret;
check_val = ex_clks | SSUSB_SYS125_RST_B_STS | SSUSB_SYSPLL_STABLE |
SSUSB_REF_RST_B_STS;
ret = readl_poll_timeout(ibase + U3D_SSUSB_IP_PW_STS1, value,
((value & check_val) == check_val), 10000);
if (ret) {
dev_err(ssusb->dev, "clks of sts1 are not stable!\n");
return ret;
}
ret = readl_poll_timeout(ibase + U3D_SSUSB_IP_PW_STS2, value,
(value & SSUSB_U2_MAC_SYS_RST_B_STS), 10000);
if (ret) {
dev_err(ssusb->dev, "mac2 clock is not stable\n");
return ret;
}
return 0;
}
int ssusb_phy_setup(struct ssusb_mtk *ssusb)
{
struct udevice *dev = ssusb->dev;
struct phy_bulk *phys = &ssusb->phys;
int ret;
ret = generic_phy_get_bulk(dev, phys);
if (ret)
return ret;
ret = generic_phy_init_bulk(phys);
if (ret)
return ret;
ret = generic_phy_power_on_bulk(phys);
if (ret)
generic_phy_exit_bulk(phys);
return ret;
}
void ssusb_phy_shutdown(struct ssusb_mtk *ssusb)
{
generic_phy_power_off_bulk(&ssusb->phys);
generic_phy_exit_bulk(&ssusb->phys);
}
static int ssusb_rscs_init(struct ssusb_mtk *ssusb)
{
int ret = 0;
ret = regulator_set_enable(ssusb->vusb33_supply, true);
if (ret < 0 && ret != -ENOSYS) {
dev_err(ssusb->dev, "failed to enable vusb33\n");
goto vusb33_err;
}
ret = clk_enable_bulk(&ssusb->clks);
if (ret)
goto clks_err;
ret = ssusb_phy_setup(ssusb);
if (ret) {
dev_err(ssusb->dev, "failed to setup phy\n");
goto phy_err;
}
return 0;
phy_err:
clk_disable_bulk(&ssusb->clks);
clks_err:
regulator_set_enable(ssusb->vusb33_supply, false);
vusb33_err:
return ret;
}
static void ssusb_rscs_exit(struct ssusb_mtk *ssusb)
{
clk_disable_bulk(&ssusb->clks);
regulator_set_enable(ssusb->vusb33_supply, false);
ssusb_phy_shutdown(ssusb);
}
static void ssusb_ip_sw_reset(struct ssusb_mtk *ssusb)
{
/* reset whole ip (xhci & u3d) */
mtu3_setbits(ssusb->ippc_base, U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
udelay(1);
mtu3_clrbits(ssusb->ippc_base, U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
}
static int get_ssusb_rscs(struct udevice *dev, struct ssusb_mtk *ssusb)
{
struct udevice *child;
int ret;
ret = device_get_supply_regulator(dev, "vusb33-supply",
&ssusb->vusb33_supply);
if (ret) /* optional, ignore error */
dev_warn(dev, "can't get optional vusb33 %d\n", ret);
ret = device_get_supply_regulator(dev, "vbus-supply",
&ssusb->vbus_supply);
if (ret) /* optional, ignore error */
dev_warn(dev, "can't get optional vbus regulator %d!\n", ret);
ret = clk_get_bulk(dev, &ssusb->clks);
if (ret) {
dev_err(dev, "failed to get clocks %d!\n", ret);
return ret;
}
ssusb->ippc_base = devfdt_remap_addr_name(dev, "ippc");
if (!ssusb->ippc_base) {
dev_err(dev, "error mapping memory for ippc\n");
return -ENODEV;
}
ret = device_find_first_child(dev, &child);
if (ret || !child) {
dev_err(dev, "failed to get child %d!\n", ret);
return ret;
}
ssusb->mac_base = devfdt_remap_addr_name(child, "mac");
if (!ssusb->mac_base) {
dev_err(dev, "error mapping memory for mac\n");
return -ENODEV;
}
ssusb->dr_mode = usb_get_dr_mode(child->node);
if (ssusb->dr_mode == USB_DR_MODE_UNKNOWN ||
ssusb->dr_mode == USB_DR_MODE_OTG)
ssusb->dr_mode = USB_DR_MODE_PERIPHERAL;
if (IS_ENABLED(CONFIG_USB_MTU3_GADGET))
ssusb->dr_mode = USB_DR_MODE_PERIPHERAL;
else if (IS_ENABLED(CONFIG_USB_MTU3_HOST))
ssusb->dr_mode = USB_DR_MODE_HOST;
dev_info(dev, "dr_mode: %d, ippc: 0x%p, mac: 0x%p\n",
ssusb->dr_mode, ssusb->ippc_base, ssusb->mac_base);
return 0;
}
static int mtu3_probe(struct udevice *dev)
{
struct ssusb_mtk *ssusb = dev_get_priv(dev);
int ret = -ENOMEM;
ssusb->dev = dev;
ret = get_ssusb_rscs(dev, ssusb);
if (ret)
return ret;
ret = ssusb_rscs_init(ssusb);
if (ret)
return ret;
ssusb_ip_sw_reset(ssusb);
return 0;
}
static int mtu3_remove(struct udevice *dev)
{
struct ssusb_mtk *ssusb = dev_to_ssusb(dev);
ssusb_rscs_exit(ssusb);
return 0;
}
static const struct udevice_id ssusb_of_match[] = {
{.compatible = "mediatek,ssusb",},
{},
};
#if CONFIG_IS_ENABLED(DM_USB_GADGET)
int dm_usb_gadget_handle_interrupts(struct udevice *dev)
{
struct mtu3 *mtu = dev_get_priv(dev);
mtu3_irq(0, mtu);
return 0;
}
static int mtu3_gadget_probe(struct udevice *dev)
{
struct ssusb_mtk *ssusb = dev_to_ssusb(dev->parent);
struct mtu3 *mtu = dev_get_priv(dev);
mtu->dev = dev;
ssusb->u3d = mtu;
return ssusb_gadget_init(ssusb);
}
static int mtu3_gadget_remove(struct udevice *dev)
{
struct mtu3 *mtu = dev_get_priv(dev);
ssusb_gadget_exit(mtu->ssusb);
return 0;
}
U_BOOT_DRIVER(mtu3_peripheral) = {
.name = "mtu3-peripheral",
.id = UCLASS_USB_GADGET_GENERIC,
.of_match = ssusb_of_match,
.probe = mtu3_gadget_probe,
.remove = mtu3_gadget_remove,
.priv_auto_alloc_size = sizeof(struct mtu3),
};
#endif
#if defined(CONFIG_SPL_USB_HOST_SUPPORT) || \
(!defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_HOST))
static int mtu3_host_probe(struct udevice *dev)
{
struct ssusb_mtk *ssusb = dev_to_ssusb(dev->parent);
struct mtu3_host *u3h = dev_get_priv(dev);
struct xhci_hcor *hcor;
int rc;
u3h->dev = dev;
ssusb->u3h = u3h;
rc = ssusb_host_init(ssusb);
if (rc)
return rc;
u3h->ctrl.quirks = XHCI_MTK_HOST;
hcor = (struct xhci_hcor *)((uintptr_t)u3h->hcd +
HC_LENGTH(xhci_readl(&u3h->hcd->cr_capbase)));
return xhci_register(dev, u3h->hcd, hcor);
}
static int mtu3_host_remove(struct udevice *dev)
{
struct mtu3_host *u3h = dev_get_priv(dev);
xhci_deregister(dev);
ssusb_host_exit(u3h->ssusb);
return 0;
}
U_BOOT_DRIVER(mtu3_host) = {
.name = "mtu3-host",
.id = UCLASS_USB,
.of_match = ssusb_of_match,
.probe = mtu3_host_probe,
.remove = mtu3_host_remove,
.priv_auto_alloc_size = sizeof(struct mtu3_host),
.ops = &xhci_usb_ops,
.flags = DM_FLAG_ALLOC_PRIV_DMA,
};
#endif
static int mtu3_glue_bind(struct udevice *parent)
{
struct udevice *dev;
enum usb_dr_mode dr_mode;
const char *driver;
const char *name;
ofnode node;
int ret;
node = ofnode_by_compatible(parent->node, "mediatek,ssusb");
if (!ofnode_valid(node))
return -ENODEV;
name = ofnode_get_name(node);
dr_mode = usb_get_dr_mode(node);
switch (dr_mode) {
#if CONFIG_IS_ENABLED(DM_USB_GADGET)
case USB_DR_MODE_PERIPHERAL:
case USB_DR_MODE_OTG:
dev_dbg(parent, "%s: dr_mode: peripheral\n", __func__);
driver = "mtu3-peripheral";
break;
#endif
#if defined(CONFIG_SPL_USB_HOST_SUPPORT) || \
(!defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_HOST))
case USB_DR_MODE_HOST:
dev_dbg(parent, "%s: dr_mode: host\n", __func__);
driver = "mtu3-host";
break;
#endif
default:
dev_err(parent, "%s: unsupported dr_mode %d\n",
__func__, dr_mode);
return -ENODEV;
};
dev_dbg(parent, "%s: node name: %s, driver %s, dr_mode %d\n",
__func__, name, driver, dr_mode);
ret = device_bind_driver_to_node(parent, driver, name, node, &dev);
if (ret)
dev_err(parent, "%s: not able to bind usb device mode\n",
__func__);
return ret;
}
static const struct udevice_id mtu3_of_match[] = {
{.compatible = "mediatek,mtu3",},
{},
};
U_BOOT_DRIVER(mtu3) = {
.name = "mtu3",
.id = UCLASS_NOP,
.of_match = mtu3_of_match,
.bind = mtu3_glue_bind,
.probe = mtu3_probe,
.remove = mtu3_remove,
.priv_auto_alloc_size = sizeof(struct ssusb_mtk),
};
This diff is collapsed.