1b293cb
From d8c80bb3b55b0821e1cf6a4814262c152ae5bc4b Mon Sep 17 00:00:00 2001
1b293cb
From: Vivek Gautam <gautam.vivek@samsung.com>
1b293cb
Date: Mon, 9 Oct 2017 14:00:51 +0200
1b293cb
Subject: phy: exynos5-usbdrd: Calibrate LOS levels for exynos5420/5800
1b293cb
1b293cb
Adding phy calibration sequence for USB 3.0 DRD PHY present on
1b293cb
Exynos5420/5800 systems.
1b293cb
This calibration facilitates setting certain PHY parameters viz.
1b293cb
the Loss-of-Signal (LOS) Detector Threshold Level, as well as
1b293cb
Tx-Vboost-Level for Super-Speed operations.
1b293cb
Additionally we also set proper time to wait for RxDetect measurement,
1b293cb
for desired PHY reference clock, so as to solve issue with enumeration
1b293cb
of few USB 3.0 devices, like Samsung SUM-TSB16S 3.0 USB drive
1b293cb
on the controller.
1b293cb
1b293cb
We are using CR_port for this purpose to send required data
1b293cb
to override the LOS values.
1b293cb
1b293cb
On testing with USB 3.0 devices on USB 3.0 port present on
1b293cb
SMDK5420, and peach-pit boards should see following message:
1b293cb
usb 2-1: new SuperSpeed USB device number 2 using xhci-hcd
1b293cb
1b293cb
and without this patch, should see below shown message:
1b293cb
usb 1-1: new high-speed USB device number 2 using xhci-hcd
1b293cb
1b293cb
[Also removed unnecessary extra lines in the register macro definitions]
1b293cb
1b293cb
Signed-off-by: Vivek Gautam <gautam.vivek@samsung.com>
1b293cb
[adapted to use phy_calibrate as entry point]
1b293cb
Signed-off-by: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
1b293cb
Acked-by: Felipe Balbi <felipe.balbi@linux.intel.com>
1b293cb
Signed-off-by: Kishon Vijay Abraham I <kishon@ti.com>
1b293cb
1b293cb
Signed-off-by: Kishon Vijay Abraham I <kishon@ti.com>
1b293cb
---
1b293cb
 drivers/phy/samsung/phy-exynos5-usbdrd.c | 183 +++++++++++++++++++++++++++++++
1b293cb
 drivers/usb/dwc3/core.c                  |   2 +
1b293cb
 2 files changed, 185 insertions(+)
1b293cb
1b293cb
diff --git a/drivers/phy/samsung/phy-exynos5-usbdrd.c b/drivers/phy/samsung/phy-exynos5-usbdrd.c
1b293cb
index 22c68f5..b8b226a 100644
1b293cb
--- a/drivers/phy/samsung/phy-exynos5-usbdrd.c
1b293cb
+++ b/drivers/phy/samsung/phy-exynos5-usbdrd.c
1b293cb
@@ -90,7 +90,17 @@
1b293cb
 #define PHYCLKRST_COMMONONN			BIT(0)
1b293cb
 
1b293cb
 #define EXYNOS5_DRD_PHYREG0			0x14
1b293cb
+#define PHYREG0_SSC_REF_CLK_SEL			BIT(21)
1b293cb
+#define PHYREG0_SSC_RANGE			BIT(20)
1b293cb
+#define PHYREG0_CR_WRITE			BIT(19)
1b293cb
+#define PHYREG0_CR_READ				BIT(18)
1b293cb
+#define PHYREG0_CR_DATA_IN(_x)			((_x) << 2)
1b293cb
+#define PHYREG0_CR_CAP_DATA			BIT(1)
1b293cb
+#define PHYREG0_CR_CAP_ADDR			BIT(0)
1b293cb
+
1b293cb
 #define EXYNOS5_DRD_PHYREG1			0x18
1b293cb
+#define PHYREG1_CR_DATA_OUT(_x)			((_x) << 1)
1b293cb
+#define PHYREG1_CR_ACK				BIT(0)
1b293cb
 
1b293cb
 #define EXYNOS5_DRD_PHYPARAM0			0x1c
1b293cb
 
1b293cb
@@ -119,6 +129,25 @@
1b293cb
 #define EXYNOS5_DRD_PHYRESUME			0x34
1b293cb
 #define EXYNOS5_DRD_LINKPORT			0x44
1b293cb
 
1b293cb
+/* USB 3.0 DRD PHY SS Function Control Reg; accessed by CR_PORT */
1b293cb
+#define EXYNOS5_DRD_PHYSS_LOSLEVEL_OVRD_IN		(0x15)
1b293cb
+#define LOSLEVEL_OVRD_IN_LOS_BIAS_5420			(0x5 << 13)
1b293cb
+#define LOSLEVEL_OVRD_IN_LOS_BIAS_DEFAULT		(0x0 << 13)
1b293cb
+#define LOSLEVEL_OVRD_IN_EN				(0x1 << 10)
1b293cb
+#define LOSLEVEL_OVRD_IN_LOS_LEVEL_DEFAULT		(0x9 << 0)
1b293cb
+
1b293cb
+#define EXYNOS5_DRD_PHYSS_TX_VBOOSTLEVEL_OVRD_IN	(0x12)
1b293cb
+#define TX_VBOOSTLEVEL_OVRD_IN_VBOOST_5420		(0x5 << 13)
1b293cb
+#define TX_VBOOSTLEVEL_OVRD_IN_VBOOST_DEFAULT		(0x4 << 13)
1b293cb
+
1b293cb
+#define EXYNOS5_DRD_PHYSS_LANE0_TX_DEBUG		(0x1010)
1b293cb
+#define LANE0_TX_DEBUG_RXDET_MEAS_TIME_19M2_20M		(0x4 << 4)
1b293cb
+#define LANE0_TX_DEBUG_RXDET_MEAS_TIME_24M		(0x8 << 4)
1b293cb
+#define LANE0_TX_DEBUG_RXDET_MEAS_TIME_25M_26M		(0x8 << 4)
1b293cb
+#define LANE0_TX_DEBUG_RXDET_MEAS_TIME_48M_50M_52M	(0x20 << 4)
1b293cb
+#define LANE0_TX_DEBUG_RXDET_MEAS_TIME_62M5		(0x20 << 4)
1b293cb
+#define LANE0_TX_DEBUG_RXDET_MEAS_TIME_96M_100M		(0x40 << 4)
1b293cb
+
1b293cb
 #define KHZ	1000
1b293cb
 #define MHZ	(KHZ * KHZ)
1b293cb
 
1b293cb
@@ -527,6 +556,151 @@ static int exynos5_usbdrd_phy_power_off(struct phy *phy)
1b293cb
 	return 0;
1b293cb
 }
1b293cb
 
1b293cb
+static int crport_handshake(struct exynos5_usbdrd_phy *phy_drd,
1b293cb
+			    u32 val, u32 cmd)
1b293cb
+{
1b293cb
+	u32 usec = 100;
1b293cb
+	unsigned int result;
1b293cb
+
1b293cb
+	writel(val | cmd, phy_drd->reg_phy + EXYNOS5_DRD_PHYREG0);
1b293cb
+
1b293cb
+	do {
1b293cb
+		result = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYREG1);
1b293cb
+		if (result & PHYREG1_CR_ACK)
1b293cb
+			break;
1b293cb
+
1b293cb
+		udelay(1);
1b293cb
+	} while (usec-- > 0);
1b293cb
+
1b293cb
+	if (!usec) {
1b293cb
+		dev_err(phy_drd->dev,
1b293cb
+			"CRPORT handshake timeout1 (0x%08x)\n", val);
1b293cb
+		return -ETIME;
1b293cb
+	}
1b293cb
+
1b293cb
+	usec = 100;
1b293cb
+
1b293cb
+	writel(val, phy_drd->reg_phy + EXYNOS5_DRD_PHYREG0);
1b293cb
+
1b293cb
+	do {
1b293cb
+		result = readl(phy_drd->reg_phy + EXYNOS5_DRD_PHYREG1);
1b293cb
+		if (!(result & PHYREG1_CR_ACK))
1b293cb
+			break;
1b293cb
+
1b293cb
+		udelay(1);
1b293cb
+	} while (usec-- > 0);
1b293cb
+
1b293cb
+	if (!usec) {
1b293cb
+		dev_err(phy_drd->dev,
1b293cb
+			"CRPORT handshake timeout2 (0x%08x)\n", val);
1b293cb
+		return -ETIME;
1b293cb
+	}
1b293cb
+
1b293cb
+	return 0;
1b293cb
+}
1b293cb
+
1b293cb
+static int crport_ctrl_write(struct exynos5_usbdrd_phy *phy_drd,
1b293cb
+			     u32 addr, u32 data)
1b293cb
+{
1b293cb
+	int ret;
1b293cb
+
1b293cb
+	/* Write Address */
1b293cb
+	writel(PHYREG0_CR_DATA_IN(addr),
1b293cb
+	       phy_drd->reg_phy + EXYNOS5_DRD_PHYREG0);
1b293cb
+	ret = crport_handshake(phy_drd, PHYREG0_CR_DATA_IN(addr),
1b293cb
+			       PHYREG0_CR_CAP_ADDR);
1b293cb
+	if (ret)
1b293cb
+		return ret;
1b293cb
+
1b293cb
+	/* Write Data */
1b293cb
+	writel(PHYREG0_CR_DATA_IN(data),
1b293cb
+	       phy_drd->reg_phy + EXYNOS5_DRD_PHYREG0);
1b293cb
+	ret = crport_handshake(phy_drd, PHYREG0_CR_DATA_IN(data),
1b293cb
+			       PHYREG0_CR_CAP_DATA);
1b293cb
+	if (ret)
1b293cb
+		return ret;
1b293cb
+
1b293cb
+	ret = crport_handshake(phy_drd, PHYREG0_CR_DATA_IN(data),
1b293cb
+			       PHYREG0_CR_WRITE);
1b293cb
+
1b293cb
+	return ret;
1b293cb
+}
1b293cb
+
1b293cb
+/*
1b293cb
+ * Calibrate few PHY parameters using CR_PORT register to meet
1b293cb
+ * SuperSpeed requirements on Exynos5420 and Exynos5800 systems,
1b293cb
+ * which have 28nm USB 3.0 DRD PHY.
1b293cb
+ */
1b293cb
+static int exynos5420_usbdrd_phy_calibrate(struct exynos5_usbdrd_phy *phy_drd)
1b293cb
+{
1b293cb
+	unsigned int temp;
1b293cb
+	int ret = 0;
1b293cb
+
1b293cb
+	/*
1b293cb
+	 * Change los_bias to (0x5) for 28nm PHY from a
1b293cb
+	 * default value (0x0); los_level is set as default
1b293cb
+	 * (0x9) as also reflected in los_level[30:26] bits
1b293cb
+	 * of PHYPARAM0 register.
1b293cb
+	 */
1b293cb
+	temp = LOSLEVEL_OVRD_IN_LOS_BIAS_5420 |
1b293cb
+		LOSLEVEL_OVRD_IN_EN |
1b293cb
+		LOSLEVEL_OVRD_IN_LOS_LEVEL_DEFAULT;
1b293cb
+	ret = crport_ctrl_write(phy_drd,
1b293cb
+				EXYNOS5_DRD_PHYSS_LOSLEVEL_OVRD_IN,
1b293cb
+				temp);
1b293cb
+	if (ret) {
1b293cb
+		dev_err(phy_drd->dev,
1b293cb
+			"Failed setting Loss-of-Signal level for SuperSpeed\n");
1b293cb
+		return ret;
1b293cb
+	}
1b293cb
+
1b293cb
+	/*
1b293cb
+	 * Set tx_vboost_lvl to (0x5) for 28nm PHY Tuning,
1b293cb
+	 * to raise Tx signal level from its default value of (0x4)
1b293cb
+	 */
1b293cb
+	temp = TX_VBOOSTLEVEL_OVRD_IN_VBOOST_5420;
1b293cb
+	ret = crport_ctrl_write(phy_drd,
1b293cb
+				EXYNOS5_DRD_PHYSS_TX_VBOOSTLEVEL_OVRD_IN,
1b293cb
+				temp);
1b293cb
+	if (ret) {
1b293cb
+		dev_err(phy_drd->dev,
1b293cb
+			"Failed setting Tx-Vboost-Level for SuperSpeed\n");
1b293cb
+		return ret;
1b293cb
+	}
1b293cb
+
1b293cb
+	/*
1b293cb
+	 * Set proper time to wait for RxDetect measurement, for
1b293cb
+	 * desired reference clock of PHY, by tuning the CR_PORT
1b293cb
+	 * register LANE0.TX_DEBUG which is internal to PHY.
1b293cb
+	 * This fixes issue with few USB 3.0 devices, which are
1b293cb
+	 * not detected (not even generate interrupts on the bus
1b293cb
+	 * on insertion) without this change.
1b293cb
+	 * e.g. Samsung SUM-TSB16S 3.0 USB drive.
1b293cb
+	 */
1b293cb
+	switch (phy_drd->extrefclk) {
1b293cb
+	case EXYNOS5_FSEL_50MHZ:
1b293cb
+		temp = LANE0_TX_DEBUG_RXDET_MEAS_TIME_48M_50M_52M;
1b293cb
+		break;
1b293cb
+	case EXYNOS5_FSEL_20MHZ:
1b293cb
+	case EXYNOS5_FSEL_19MHZ2:
1b293cb
+		temp = LANE0_TX_DEBUG_RXDET_MEAS_TIME_19M2_20M;
1b293cb
+		break;
1b293cb
+	case EXYNOS5_FSEL_24MHZ:
1b293cb
+	default:
1b293cb
+		temp = LANE0_TX_DEBUG_RXDET_MEAS_TIME_24M;
1b293cb
+		break;
1b293cb
+	}
1b293cb
+
1b293cb
+	ret = crport_ctrl_write(phy_drd,
1b293cb
+				EXYNOS5_DRD_PHYSS_LANE0_TX_DEBUG,
1b293cb
+				temp);
1b293cb
+	if (ret)
1b293cb
+		dev_err(phy_drd->dev,
1b293cb
+			"Fail to set RxDet measurement time for SuperSpeed\n");
1b293cb
+
1b293cb
+	return ret;
1b293cb
+}
1b293cb
+
1b293cb
 static struct phy *exynos5_usbdrd_phy_xlate(struct device *dev,
1b293cb
 					struct of_phandle_args *args)
1b293cb
 {
1b293cb
@@ -538,11 +712,20 @@ static struct phy *exynos5_usbdrd_phy_xlate(struct device *dev,
1b293cb
 	return phy_drd->phys[args->args[0]].phy;
1b293cb
 }
1b293cb
 
1b293cb
+static int exynos5_usbdrd_phy_calibrate(struct phy *phy)
1b293cb
+{
1b293cb
+	struct phy_usb_instance *inst = phy_get_drvdata(phy);
1b293cb
+	struct exynos5_usbdrd_phy *phy_drd = to_usbdrd_phy(inst);
1b293cb
+
1b293cb
+	return exynos5420_usbdrd_phy_calibrate(phy_drd);
1b293cb
+}
1b293cb
+
1b293cb
 static const struct phy_ops exynos5_usbdrd_phy_ops = {
1b293cb
 	.init		= exynos5_usbdrd_phy_init,
1b293cb
 	.exit		= exynos5_usbdrd_phy_exit,
1b293cb
 	.power_on	= exynos5_usbdrd_phy_power_on,
1b293cb
 	.power_off	= exynos5_usbdrd_phy_power_off,
1b293cb
+	.calibrate	= exynos5_usbdrd_phy_calibrate,
1b293cb
 	.owner		= THIS_MODULE,
1b293cb
 };
1b293cb
 
1b293cb
diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
1b293cb
index 0783250..71707a3 100644
1b293cb
--- a/drivers/usb/dwc3/core.c
1b293cb
+++ b/drivers/usb/dwc3/core.c
1b293cb
@@ -147,6 +147,7 @@ static void __dwc3_set_mode(struct work_struct *work)
1b293cb
 				otg_set_vbus(dwc->usb2_phy->otg, true);
1b293cb
 			phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
1b293cb
 			phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
1b293cb
+			phy_calibrate(dwc->usb2_generic_phy);
1b293cb
 		}
1b293cb
 		break;
1b293cb
 	case DWC3_GCTL_PRTCAP_DEVICE:
1b293cb
@@ -945,6 +946,7 @@ static int dwc3_core_init_mode(struct dwc3 *dwc)
1b293cb
 				dev_err(dev, "failed to initialize host\n");
1b293cb
 			return ret;
1b293cb
 		}
1b293cb
+		phy_calibrate(dwc->usb2_generic_phy);
1b293cb
 		break;
1b293cb
 	case USB_DR_MODE_OTG:
1b293cb
 		INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
1b293cb
-- 
1b293cb
cgit v1.1
1b293cb