xref: /kernel/linux/linux-5.10/drivers/usb/dwc2/drd.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * drd.c - DesignWare USB2 DRD Controller Dual-role support
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2020 STMicroelectronics
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author(s): Amelie Delaunay <amelie.delaunay@st.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/clk.h>
118c2ecf20Sopenharmony_ci#include <linux/iopoll.h>
128c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
138c2ecf20Sopenharmony_ci#include <linux/usb/role.h>
148c2ecf20Sopenharmony_ci#include "core.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistatic void dwc2_ovr_init(struct dwc2_hsotg *hsotg)
178c2ecf20Sopenharmony_ci{
188c2ecf20Sopenharmony_ci	unsigned long flags;
198c2ecf20Sopenharmony_ci	u32 gotgctl;
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hsotg->lock, flags);
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	gotgctl = dwc2_readl(hsotg, GOTGCTL);
248c2ecf20Sopenharmony_ci	gotgctl |= GOTGCTL_BVALOEN | GOTGCTL_AVALOEN | GOTGCTL_VBVALOEN;
258c2ecf20Sopenharmony_ci	gotgctl |= GOTGCTL_DBNCE_FLTR_BYPASS;
268c2ecf20Sopenharmony_ci	gotgctl &= ~(GOTGCTL_BVALOVAL | GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL);
278c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, gotgctl, GOTGCTL);
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	dwc2_force_mode(hsotg, (hsotg->dr_mode == USB_DR_MODE_HOST));
328c2ecf20Sopenharmony_ci}
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic int dwc2_ovr_avalid(struct dwc2_hsotg *hsotg, bool valid)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	u32 gotgctl = dwc2_readl(hsotg, GOTGCTL);
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	/* Check if A-Session is already in the right state */
398c2ecf20Sopenharmony_ci	if ((valid && (gotgctl & GOTGCTL_ASESVLD)) ||
408c2ecf20Sopenharmony_ci	    (!valid && !(gotgctl & GOTGCTL_ASESVLD)))
418c2ecf20Sopenharmony_ci		return -EALREADY;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	gotgctl &= ~GOTGCTL_BVALOVAL;
448c2ecf20Sopenharmony_ci	if (valid)
458c2ecf20Sopenharmony_ci		gotgctl |= GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL;
468c2ecf20Sopenharmony_ci	else
478c2ecf20Sopenharmony_ci		gotgctl &= ~(GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL);
488c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, gotgctl, GOTGCTL);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	return 0;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic int dwc2_ovr_bvalid(struct dwc2_hsotg *hsotg, bool valid)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	u32 gotgctl = dwc2_readl(hsotg, GOTGCTL);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	/* Check if B-Session is already in the right state */
588c2ecf20Sopenharmony_ci	if ((valid && (gotgctl & GOTGCTL_BSESVLD)) ||
598c2ecf20Sopenharmony_ci	    (!valid && !(gotgctl & GOTGCTL_BSESVLD)))
608c2ecf20Sopenharmony_ci		return -EALREADY;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	gotgctl &= ~GOTGCTL_AVALOVAL;
638c2ecf20Sopenharmony_ci	if (valid)
648c2ecf20Sopenharmony_ci		gotgctl |= GOTGCTL_BVALOVAL | GOTGCTL_VBVALOVAL;
658c2ecf20Sopenharmony_ci	else
668c2ecf20Sopenharmony_ci		gotgctl &= ~(GOTGCTL_BVALOVAL | GOTGCTL_VBVALOVAL);
678c2ecf20Sopenharmony_ci	dwc2_writel(hsotg, gotgctl, GOTGCTL);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	return 0;
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic int dwc2_drd_role_sw_set(struct usb_role_switch *sw, enum usb_role role)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	struct dwc2_hsotg *hsotg = usb_role_switch_get_drvdata(sw);
758c2ecf20Sopenharmony_ci	unsigned long flags;
768c2ecf20Sopenharmony_ci	int already = 0;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/* Skip session not in line with dr_mode */
798c2ecf20Sopenharmony_ci	if ((role == USB_ROLE_DEVICE && hsotg->dr_mode == USB_DR_MODE_HOST) ||
808c2ecf20Sopenharmony_ci	    (role == USB_ROLE_HOST && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL))
818c2ecf20Sopenharmony_ci		return -EINVAL;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
848c2ecf20Sopenharmony_ci	IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
858c2ecf20Sopenharmony_ci	/* Skip session if core is in test mode */
868c2ecf20Sopenharmony_ci	if (role == USB_ROLE_NONE && hsotg->test_mode) {
878c2ecf20Sopenharmony_ci		dev_dbg(hsotg->dev, "Core is in test mode\n");
888c2ecf20Sopenharmony_ci		return -EBUSY;
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci#endif
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	/*
938c2ecf20Sopenharmony_ci	 * In case of USB_DR_MODE_PERIPHERAL, clock is disabled at the end of
948c2ecf20Sopenharmony_ci	 * the probe and enabled on udc_start.
958c2ecf20Sopenharmony_ci	 * If role-switch set is called before the udc_start, we need to enable
968c2ecf20Sopenharmony_ci	 * the clock to read/write GOTGCTL and GUSBCFG registers to override
978c2ecf20Sopenharmony_ci	 * mode and sessions. It is the case if cable is plugged at boot.
988c2ecf20Sopenharmony_ci	 */
998c2ecf20Sopenharmony_ci	if (!hsotg->ll_hw_enabled && hsotg->clk) {
1008c2ecf20Sopenharmony_ci		int ret = clk_prepare_enable(hsotg->clk);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci		if (ret)
1038c2ecf20Sopenharmony_ci			return ret;
1048c2ecf20Sopenharmony_ci	}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hsotg->lock, flags);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	if (role == USB_ROLE_HOST) {
1098c2ecf20Sopenharmony_ci		already = dwc2_ovr_avalid(hsotg, true);
1108c2ecf20Sopenharmony_ci	} else if (role == USB_ROLE_DEVICE) {
1118c2ecf20Sopenharmony_ci		already = dwc2_ovr_bvalid(hsotg, true);
1128c2ecf20Sopenharmony_ci		if (dwc2_is_device_enabled(hsotg)) {
1138c2ecf20Sopenharmony_ci			/* This clear DCTL.SFTDISCON bit */
1148c2ecf20Sopenharmony_ci			dwc2_hsotg_core_connect(hsotg);
1158c2ecf20Sopenharmony_ci		}
1168c2ecf20Sopenharmony_ci	} else {
1178c2ecf20Sopenharmony_ci		if (dwc2_is_device_mode(hsotg)) {
1188c2ecf20Sopenharmony_ci			if (!dwc2_ovr_bvalid(hsotg, false))
1198c2ecf20Sopenharmony_ci				/* This set DCTL.SFTDISCON bit */
1208c2ecf20Sopenharmony_ci				dwc2_hsotg_core_disconnect(hsotg);
1218c2ecf20Sopenharmony_ci		} else {
1228c2ecf20Sopenharmony_ci			dwc2_ovr_avalid(hsotg, false);
1238c2ecf20Sopenharmony_ci		}
1248c2ecf20Sopenharmony_ci	}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hsotg->lock, flags);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	if (!already && hsotg->dr_mode == USB_DR_MODE_OTG)
1298c2ecf20Sopenharmony_ci		/* This will raise a Connector ID Status Change Interrupt */
1308c2ecf20Sopenharmony_ci		dwc2_force_mode(hsotg, role == USB_ROLE_HOST);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	if (!hsotg->ll_hw_enabled && hsotg->clk)
1338c2ecf20Sopenharmony_ci		clk_disable_unprepare(hsotg->clk);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	dev_dbg(hsotg->dev, "%s-session valid\n",
1368c2ecf20Sopenharmony_ci		role == USB_ROLE_NONE ? "No" :
1378c2ecf20Sopenharmony_ci		role == USB_ROLE_HOST ? "A" : "B");
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	return 0;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ciint dwc2_drd_init(struct dwc2_hsotg *hsotg)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	struct usb_role_switch_desc role_sw_desc = {0};
1458c2ecf20Sopenharmony_ci	struct usb_role_switch *role_sw;
1468c2ecf20Sopenharmony_ci	int ret;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	if (!device_property_read_bool(hsotg->dev, "usb-role-switch"))
1498c2ecf20Sopenharmony_ci		return 0;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	role_sw_desc.driver_data = hsotg;
1528c2ecf20Sopenharmony_ci	role_sw_desc.fwnode = dev_fwnode(hsotg->dev);
1538c2ecf20Sopenharmony_ci	role_sw_desc.set = dwc2_drd_role_sw_set;
1548c2ecf20Sopenharmony_ci	role_sw_desc.allow_userspace_control = true;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	role_sw = usb_role_switch_register(hsotg->dev, &role_sw_desc);
1578c2ecf20Sopenharmony_ci	if (IS_ERR(role_sw)) {
1588c2ecf20Sopenharmony_ci		ret = PTR_ERR(role_sw);
1598c2ecf20Sopenharmony_ci		dev_err(hsotg->dev,
1608c2ecf20Sopenharmony_ci			"failed to register role switch: %d\n", ret);
1618c2ecf20Sopenharmony_ci		return ret;
1628c2ecf20Sopenharmony_ci	}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	hsotg->role_sw = role_sw;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	/* Enable override and initialize values */
1678c2ecf20Sopenharmony_ci	dwc2_ovr_init(hsotg);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	return 0;
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_civoid dwc2_drd_suspend(struct dwc2_hsotg *hsotg)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	u32 gintsts, gintmsk;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	if (hsotg->role_sw && !hsotg->params.external_id_pin_ctl) {
1778c2ecf20Sopenharmony_ci		gintmsk = dwc2_readl(hsotg, GINTMSK);
1788c2ecf20Sopenharmony_ci		gintmsk &= ~GINTSTS_CONIDSTSCHNG;
1798c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, gintmsk, GINTMSK);
1808c2ecf20Sopenharmony_ci		gintsts = dwc2_readl(hsotg, GINTSTS);
1818c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, gintsts | GINTSTS_CONIDSTSCHNG, GINTSTS);
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_civoid dwc2_drd_resume(struct dwc2_hsotg *hsotg)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	u32 gintsts, gintmsk;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	if (hsotg->role_sw && !hsotg->params.external_id_pin_ctl) {
1908c2ecf20Sopenharmony_ci		gintsts = dwc2_readl(hsotg, GINTSTS);
1918c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, gintsts | GINTSTS_CONIDSTSCHNG, GINTSTS);
1928c2ecf20Sopenharmony_ci		gintmsk = dwc2_readl(hsotg, GINTMSK);
1938c2ecf20Sopenharmony_ci		gintmsk |= GINTSTS_CONIDSTSCHNG;
1948c2ecf20Sopenharmony_ci		dwc2_writel(hsotg, gintmsk, GINTMSK);
1958c2ecf20Sopenharmony_ci	}
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_civoid dwc2_drd_exit(struct dwc2_hsotg *hsotg)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	if (hsotg->role_sw)
2018c2ecf20Sopenharmony_ci		usb_role_switch_unregister(hsotg->role_sw);
2028c2ecf20Sopenharmony_ci}
203