• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * xHCI host controller driver for R-Car SoCs
4  *
5  * Copyright (C) 2014 Renesas Electronics Corporation
6  */
7 
8 #include <linux/firmware.h>
9 #include <linux/iopoll.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/of.h>
13 #include <linux/usb/phy.h>
14 #include <linux/sys_soc.h>
15 
16 #include "xhci.h"
17 #include "xhci-plat.h"
18 #include "xhci-rcar.h"
19 
20 /*
21 * - The V3 firmware is for almost all R-Car Gen3 (except r8a7795 ES1.x)
22 * - The V2 firmware is for r8a7795 ES1.x.
23 * - The V2 firmware is possible to use on R-Car Gen2. However, the V2 causes
24 *   performance degradation. So, this driver continues to use the V1 if R-Car
25 *   Gen2.
26 * - The V1 firmware is impossible to use on R-Car Gen3.
27 */
28 MODULE_FIRMWARE(XHCI_RCAR_FIRMWARE_NAME_V1);
29 MODULE_FIRMWARE(XHCI_RCAR_FIRMWARE_NAME_V2);
30 MODULE_FIRMWARE(XHCI_RCAR_FIRMWARE_NAME_V3);
31 
32 /*** Register Offset ***/
33 #define RCAR_USB3_AXH_STA	0x104	/* AXI Host Control Status */
34 #define RCAR_USB3_INT_ENA	0x224	/* Interrupt Enable */
35 #define RCAR_USB3_DL_CTRL	0x250	/* FW Download Control & Status */
36 #define RCAR_USB3_FW_DATA0	0x258	/* FW Data0 */
37 
38 #define RCAR_USB3_LCLK		0xa44	/* LCLK Select */
39 #define RCAR_USB3_CONF1		0xa48	/* USB3.0 Configuration1 */
40 #define RCAR_USB3_CONF2		0xa5c	/* USB3.0 Configuration2 */
41 #define RCAR_USB3_CONF3		0xaa8	/* USB3.0 Configuration3 */
42 #define RCAR_USB3_RX_POL	0xab0	/* USB3.0 RX Polarity */
43 #define RCAR_USB3_TX_POL	0xab8	/* USB3.0 TX Polarity */
44 
45 /*** Register Settings ***/
46 /* AXI Host Control Status */
47 #define RCAR_USB3_AXH_STA_B3_PLL_ACTIVE		0x00010000
48 #define RCAR_USB3_AXH_STA_B2_PLL_ACTIVE		0x00000001
49 #define RCAR_USB3_AXH_STA_PLL_ACTIVE_MASK (RCAR_USB3_AXH_STA_B3_PLL_ACTIVE | \
50 					   RCAR_USB3_AXH_STA_B2_PLL_ACTIVE)
51 
52 /* Interrupt Enable */
53 #define RCAR_USB3_INT_XHC_ENA	0x00000001
54 #define RCAR_USB3_INT_PME_ENA	0x00000002
55 #define RCAR_USB3_INT_HSE_ENA	0x00000004
56 #define RCAR_USB3_INT_ENA_VAL	(RCAR_USB3_INT_XHC_ENA | \
57 				RCAR_USB3_INT_PME_ENA | RCAR_USB3_INT_HSE_ENA)
58 
59 /* FW Download Control & Status */
60 #define RCAR_USB3_DL_CTRL_ENABLE	0x00000001
61 #define RCAR_USB3_DL_CTRL_FW_SUCCESS	0x00000010
62 #define RCAR_USB3_DL_CTRL_FW_SET_DATA0	0x00000100
63 
64 /* LCLK Select */
65 #define RCAR_USB3_LCLK_ENA_VAL	0x01030001
66 
67 /* USB3.0 Configuration */
68 #define RCAR_USB3_CONF1_VAL	0x00030204
69 #define RCAR_USB3_CONF2_VAL	0x00030300
70 #define RCAR_USB3_CONF3_VAL	0x13802007
71 
72 /* USB3.0 Polarity */
73 #define RCAR_USB3_RX_POL_VAL	BIT(21)
74 #define RCAR_USB3_TX_POL_VAL	BIT(4)
75 
76 /* For soc_device_attribute */
77 #define RCAR_XHCI_FIRMWARE_V2   BIT(0) /* FIRMWARE V2 */
78 
79 static const struct soc_device_attribute rcar_quirks_match[]  = {
80 	{
81 		.soc_id = "r8a7795", .revision = "ES1.*",
82 		.data = (void *)RCAR_XHCI_FIRMWARE_V2,
83 	},
84 	{ /* sentinel */ },
85 };
86 
xhci_rcar_start_gen2(struct usb_hcd * hcd)87 static void xhci_rcar_start_gen2(struct usb_hcd *hcd)
88 {
89 	/* LCLK Select */
90 	writel(RCAR_USB3_LCLK_ENA_VAL, hcd->regs + RCAR_USB3_LCLK);
91 	/* USB3.0 Configuration */
92 	writel(RCAR_USB3_CONF1_VAL, hcd->regs + RCAR_USB3_CONF1);
93 	writel(RCAR_USB3_CONF2_VAL, hcd->regs + RCAR_USB3_CONF2);
94 	writel(RCAR_USB3_CONF3_VAL, hcd->regs + RCAR_USB3_CONF3);
95 	/* USB3.0 Polarity */
96 	writel(RCAR_USB3_RX_POL_VAL, hcd->regs + RCAR_USB3_RX_POL);
97 	writel(RCAR_USB3_TX_POL_VAL, hcd->regs + RCAR_USB3_TX_POL);
98 }
99 
xhci_rcar_is_gen2(struct device * dev)100 static int xhci_rcar_is_gen2(struct device *dev)
101 {
102 	struct device_node *node = dev->of_node;
103 
104 	return of_device_is_compatible(node, "renesas,xhci-r8a7790") ||
105 		of_device_is_compatible(node, "renesas,xhci-r8a7791") ||
106 		of_device_is_compatible(node, "renesas,xhci-r8a7793") ||
107 		of_device_is_compatible(node, "renesas,rcar-gen2-xhci");
108 }
109 
xhci_rcar_start(struct usb_hcd * hcd)110 void xhci_rcar_start(struct usb_hcd *hcd)
111 {
112 	u32 temp;
113 
114 	if (hcd->regs != NULL) {
115 		/* Interrupt Enable */
116 		temp = readl(hcd->regs + RCAR_USB3_INT_ENA);
117 		temp |= RCAR_USB3_INT_ENA_VAL;
118 		writel(temp, hcd->regs + RCAR_USB3_INT_ENA);
119 		if (xhci_rcar_is_gen2(hcd->self.controller))
120 			xhci_rcar_start_gen2(hcd);
121 	}
122 }
123 
xhci_rcar_download_firmware(struct usb_hcd * hcd)124 static int xhci_rcar_download_firmware(struct usb_hcd *hcd)
125 {
126 	struct device *dev = hcd->self.controller;
127 	void __iomem *regs = hcd->regs;
128 	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
129 	const struct firmware *fw;
130 	int retval, index, j;
131 	u32 data, val, temp;
132 	u32 quirks = 0;
133 	const struct soc_device_attribute *attr;
134 	const char *firmware_name;
135 
136 	/*
137 	 * According to the datasheet, "Upon the completion of FW Download,
138 	 * there is no need to write or reload FW".
139 	 */
140 	if (readl(regs + RCAR_USB3_DL_CTRL) & RCAR_USB3_DL_CTRL_FW_SUCCESS)
141 		return 0;
142 
143 	attr = soc_device_match(rcar_quirks_match);
144 	if (attr)
145 		quirks = (uintptr_t)attr->data;
146 
147 	if (quirks & RCAR_XHCI_FIRMWARE_V2)
148 		firmware_name = XHCI_RCAR_FIRMWARE_NAME_V2;
149 	else
150 		firmware_name = priv->firmware_name;
151 
152 	/* request R-Car USB3.0 firmware */
153 	retval = request_firmware(&fw, firmware_name, dev);
154 	if (retval)
155 		return retval;
156 
157 	/* download R-Car USB3.0 firmware */
158 	temp = readl(regs + RCAR_USB3_DL_CTRL);
159 	temp |= RCAR_USB3_DL_CTRL_ENABLE;
160 	writel(temp, regs + RCAR_USB3_DL_CTRL);
161 
162 	for (index = 0; index < fw->size; index += 4) {
163 		/* to avoid reading beyond the end of the buffer */
164 		for (data = 0, j = 3; j >= 0; j--) {
165 			if ((j + index) < fw->size)
166 				data |= fw->data[index + j] << (8 * j);
167 		}
168 		writel(data, regs + RCAR_USB3_FW_DATA0);
169 		temp = readl(regs + RCAR_USB3_DL_CTRL);
170 		temp |= RCAR_USB3_DL_CTRL_FW_SET_DATA0;
171 		writel(temp, regs + RCAR_USB3_DL_CTRL);
172 
173 		retval = readl_poll_timeout_atomic(regs + RCAR_USB3_DL_CTRL,
174 				val, !(val & RCAR_USB3_DL_CTRL_FW_SET_DATA0),
175 				1, 10000);
176 		if (retval < 0)
177 			break;
178 	}
179 
180 	temp = readl(regs + RCAR_USB3_DL_CTRL);
181 	temp &= ~RCAR_USB3_DL_CTRL_ENABLE;
182 	writel(temp, regs + RCAR_USB3_DL_CTRL);
183 
184 	retval = readl_poll_timeout_atomic((regs + RCAR_USB3_DL_CTRL),
185 			val, val & RCAR_USB3_DL_CTRL_FW_SUCCESS, 1, 10000);
186 
187 	release_firmware(fw);
188 
189 	return retval;
190 }
191 
xhci_rcar_wait_for_pll_active(struct usb_hcd * hcd)192 static bool xhci_rcar_wait_for_pll_active(struct usb_hcd *hcd)
193 {
194 	int retval;
195 	u32 val, mask = RCAR_USB3_AXH_STA_PLL_ACTIVE_MASK;
196 
197 	retval = readl_poll_timeout_atomic(hcd->regs + RCAR_USB3_AXH_STA,
198 			val, (val & mask) == mask, 1, 1000);
199 	return !retval;
200 }
201 
202 /* This function needs to initialize a "phy" of usb before */
xhci_rcar_init_quirk(struct usb_hcd * hcd)203 int xhci_rcar_init_quirk(struct usb_hcd *hcd)
204 {
205 	/* If hcd->regs is NULL, we don't just call the following function */
206 	if (!hcd->regs)
207 		return 0;
208 
209 	if (!xhci_rcar_wait_for_pll_active(hcd))
210 		return -ETIMEDOUT;
211 
212 	return xhci_rcar_download_firmware(hcd);
213 }
214 
xhci_rcar_resume_quirk(struct usb_hcd * hcd)215 int xhci_rcar_resume_quirk(struct usb_hcd *hcd)
216 {
217 	int ret;
218 
219 	ret = xhci_rcar_download_firmware(hcd);
220 	if (!ret)
221 		xhci_rcar_start(hcd);
222 
223 	return ret;
224 }
225