xref: /openbmc/linux/drivers/usb/host/xhci-rcar.c (revision c9dc580c)
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/of_device.h>
14 #include <linux/usb/phy.h>
15 #include <linux/sys_soc.h>
16 
17 #include "xhci.h"
18 #include "xhci-plat.h"
19 #include "xhci-rzv2m.h"
20 
21 #define XHCI_RCAR_FIRMWARE_NAME_V1	"r8a779x_usb3_v1.dlmem"
22 #define XHCI_RCAR_FIRMWARE_NAME_V2	"r8a779x_usb3_v2.dlmem"
23 #define XHCI_RCAR_FIRMWARE_NAME_V3	"r8a779x_usb3_v3.dlmem"
24 
25 /*
26 * - The V3 firmware is for almost all R-Car Gen3 (except r8a7795 ES1.x)
27 * - The V2 firmware is for r8a7795 ES1.x.
28 * - The V2 firmware is possible to use on R-Car Gen2. However, the V2 causes
29 *   performance degradation. So, this driver continues to use the V1 if R-Car
30 *   Gen2.
31 * - The V1 firmware is impossible to use on R-Car Gen3.
32 */
33 MODULE_FIRMWARE(XHCI_RCAR_FIRMWARE_NAME_V1);
34 MODULE_FIRMWARE(XHCI_RCAR_FIRMWARE_NAME_V2);
35 MODULE_FIRMWARE(XHCI_RCAR_FIRMWARE_NAME_V3);
36 
37 /*** Register Offset ***/
38 #define RCAR_USB3_AXH_STA	0x104	/* AXI Host Control Status */
39 #define RCAR_USB3_INT_ENA	0x224	/* Interrupt Enable */
40 #define RCAR_USB3_DL_CTRL	0x250	/* FW Download Control & Status */
41 #define RCAR_USB3_FW_DATA0	0x258	/* FW Data0 */
42 
43 #define RCAR_USB3_LCLK		0xa44	/* LCLK Select */
44 #define RCAR_USB3_CONF1		0xa48	/* USB3.0 Configuration1 */
45 #define RCAR_USB3_CONF2		0xa5c	/* USB3.0 Configuration2 */
46 #define RCAR_USB3_CONF3		0xaa8	/* USB3.0 Configuration3 */
47 #define RCAR_USB3_RX_POL	0xab0	/* USB3.0 RX Polarity */
48 #define RCAR_USB3_TX_POL	0xab8	/* USB3.0 TX Polarity */
49 
50 /*** Register Settings ***/
51 /* AXI Host Control Status */
52 #define RCAR_USB3_AXH_STA_B3_PLL_ACTIVE		0x00010000
53 #define RCAR_USB3_AXH_STA_B2_PLL_ACTIVE		0x00000001
54 #define RCAR_USB3_AXH_STA_PLL_ACTIVE_MASK (RCAR_USB3_AXH_STA_B3_PLL_ACTIVE | \
55 					   RCAR_USB3_AXH_STA_B2_PLL_ACTIVE)
56 
57 /* Interrupt Enable */
58 #define RCAR_USB3_INT_XHC_ENA	0x00000001
59 #define RCAR_USB3_INT_PME_ENA	0x00000002
60 #define RCAR_USB3_INT_HSE_ENA	0x00000004
61 #define RCAR_USB3_INT_ENA_VAL	(RCAR_USB3_INT_XHC_ENA | \
62 				RCAR_USB3_INT_PME_ENA | RCAR_USB3_INT_HSE_ENA)
63 
64 /* FW Download Control & Status */
65 #define RCAR_USB3_DL_CTRL_ENABLE	0x00000001
66 #define RCAR_USB3_DL_CTRL_FW_SUCCESS	0x00000010
67 #define RCAR_USB3_DL_CTRL_FW_SET_DATA0	0x00000100
68 
69 /* LCLK Select */
70 #define RCAR_USB3_LCLK_ENA_VAL	0x01030001
71 
72 /* USB3.0 Configuration */
73 #define RCAR_USB3_CONF1_VAL	0x00030204
74 #define RCAR_USB3_CONF2_VAL	0x00030300
75 #define RCAR_USB3_CONF3_VAL	0x13802007
76 
77 /* USB3.0 Polarity */
78 #define RCAR_USB3_RX_POL_VAL	BIT(21)
79 #define RCAR_USB3_TX_POL_VAL	BIT(4)
80 
81 /* For soc_device_attribute */
82 #define RCAR_XHCI_FIRMWARE_V2   BIT(0) /* FIRMWARE V2 */
83 #define RCAR_XHCI_FIRMWARE_V3   BIT(1) /* FIRMWARE V3 */
84 
85 static const struct soc_device_attribute rcar_quirks_match[]  = {
86 	{
87 		.soc_id = "r8a7795", .revision = "ES1.*",
88 		.data = (void *)RCAR_XHCI_FIRMWARE_V2,
89 	},
90 	{ /* sentinel */ }
91 };
92 
93 static void xhci_rcar_start_gen2(struct usb_hcd *hcd)
94 {
95 	/* LCLK Select */
96 	writel(RCAR_USB3_LCLK_ENA_VAL, hcd->regs + RCAR_USB3_LCLK);
97 	/* USB3.0 Configuration */
98 	writel(RCAR_USB3_CONF1_VAL, hcd->regs + RCAR_USB3_CONF1);
99 	writel(RCAR_USB3_CONF2_VAL, hcd->regs + RCAR_USB3_CONF2);
100 	writel(RCAR_USB3_CONF3_VAL, hcd->regs + RCAR_USB3_CONF3);
101 	/* USB3.0 Polarity */
102 	writel(RCAR_USB3_RX_POL_VAL, hcd->regs + RCAR_USB3_RX_POL);
103 	writel(RCAR_USB3_TX_POL_VAL, hcd->regs + RCAR_USB3_TX_POL);
104 }
105 
106 static int xhci_rcar_is_gen2(struct device *dev)
107 {
108 	struct device_node *node = dev->of_node;
109 
110 	return of_device_is_compatible(node, "renesas,xhci-r8a7790") ||
111 		of_device_is_compatible(node, "renesas,xhci-r8a7791") ||
112 		of_device_is_compatible(node, "renesas,xhci-r8a7793") ||
113 		of_device_is_compatible(node, "renesas,rcar-gen2-xhci");
114 }
115 
116 static void xhci_rcar_start(struct usb_hcd *hcd)
117 {
118 	u32 temp;
119 
120 	if (hcd->regs != NULL) {
121 		/* Interrupt Enable */
122 		temp = readl(hcd->regs + RCAR_USB3_INT_ENA);
123 		temp |= RCAR_USB3_INT_ENA_VAL;
124 		writel(temp, hcd->regs + RCAR_USB3_INT_ENA);
125 		if (xhci_rcar_is_gen2(hcd->self.controller))
126 			xhci_rcar_start_gen2(hcd);
127 	}
128 }
129 
130 static int xhci_rcar_download_firmware(struct usb_hcd *hcd)
131 {
132 	struct device *dev = hcd->self.controller;
133 	void __iomem *regs = hcd->regs;
134 	struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
135 	const struct firmware *fw;
136 	int retval, index, j;
137 	u32 data, val, temp;
138 	u32 quirks = 0;
139 	const struct soc_device_attribute *attr;
140 	const char *firmware_name;
141 
142 	/*
143 	 * According to the datasheet, "Upon the completion of FW Download,
144 	 * there is no need to write or reload FW".
145 	 */
146 	if (readl(regs + RCAR_USB3_DL_CTRL) & RCAR_USB3_DL_CTRL_FW_SUCCESS)
147 		return 0;
148 
149 	attr = soc_device_match(rcar_quirks_match);
150 	if (attr)
151 		quirks = (uintptr_t)attr->data;
152 
153 	if (quirks & RCAR_XHCI_FIRMWARE_V2)
154 		firmware_name = XHCI_RCAR_FIRMWARE_NAME_V2;
155 	else if (quirks & RCAR_XHCI_FIRMWARE_V3)
156 		firmware_name = XHCI_RCAR_FIRMWARE_NAME_V3;
157 	else
158 		firmware_name = priv->firmware_name;
159 
160 	/* request R-Car USB3.0 firmware */
161 	retval = request_firmware(&fw, firmware_name, dev);
162 	if (retval)
163 		return retval;
164 
165 	/* download R-Car USB3.0 firmware */
166 	temp = readl(regs + RCAR_USB3_DL_CTRL);
167 	temp |= RCAR_USB3_DL_CTRL_ENABLE;
168 	writel(temp, regs + RCAR_USB3_DL_CTRL);
169 
170 	for (index = 0; index < fw->size; index += 4) {
171 		/* to avoid reading beyond the end of the buffer */
172 		for (data = 0, j = 3; j >= 0; j--) {
173 			if ((j + index) < fw->size)
174 				data |= fw->data[index + j] << (8 * j);
175 		}
176 		writel(data, regs + RCAR_USB3_FW_DATA0);
177 		temp = readl(regs + RCAR_USB3_DL_CTRL);
178 		temp |= RCAR_USB3_DL_CTRL_FW_SET_DATA0;
179 		writel(temp, regs + RCAR_USB3_DL_CTRL);
180 
181 		retval = readl_poll_timeout_atomic(regs + RCAR_USB3_DL_CTRL,
182 				val, !(val & RCAR_USB3_DL_CTRL_FW_SET_DATA0),
183 				1, 10000);
184 		if (retval < 0)
185 			break;
186 	}
187 
188 	temp = readl(regs + RCAR_USB3_DL_CTRL);
189 	temp &= ~RCAR_USB3_DL_CTRL_ENABLE;
190 	writel(temp, regs + RCAR_USB3_DL_CTRL);
191 
192 	retval = readl_poll_timeout_atomic((regs + RCAR_USB3_DL_CTRL),
193 			val, val & RCAR_USB3_DL_CTRL_FW_SUCCESS, 1, 10000);
194 
195 	release_firmware(fw);
196 
197 	return retval;
198 }
199 
200 static bool xhci_rcar_wait_for_pll_active(struct usb_hcd *hcd)
201 {
202 	int retval;
203 	u32 val, mask = RCAR_USB3_AXH_STA_PLL_ACTIVE_MASK;
204 
205 	retval = readl_poll_timeout_atomic(hcd->regs + RCAR_USB3_AXH_STA,
206 			val, (val & mask) == mask, 1, 1000);
207 	return !retval;
208 }
209 
210 /* This function needs to initialize a "phy" of usb before */
211 static int xhci_rcar_init_quirk(struct usb_hcd *hcd)
212 {
213 	/* If hcd->regs is NULL, we don't just call the following function */
214 	if (!hcd->regs)
215 		return 0;
216 
217 	if (!xhci_rcar_wait_for_pll_active(hcd))
218 		return -ETIMEDOUT;
219 
220 	return xhci_rcar_download_firmware(hcd);
221 }
222 
223 static int xhci_rcar_resume_quirk(struct usb_hcd *hcd)
224 {
225 	int ret;
226 
227 	ret = xhci_rcar_download_firmware(hcd);
228 	if (!ret)
229 		xhci_rcar_start(hcd);
230 
231 	return ret;
232 }
233 
234 /*
235  * On R-Car Gen2 and Gen3, the AC64 bit (bit 0) of HCCPARAMS1 is set
236  * to 1. However, these SoCs don't support 64-bit address memory
237  * pointers. So, this driver clears the AC64 bit of xhci->hcc_params
238  * to call dma_set_coherent_mask(dev, DMA_BIT_MASK(32)) in
239  * xhci_gen_setup() by using the XHCI_NO_64BIT_SUPPORT quirk.
240  *
241  * And, since the firmware/internal CPU control the USBSTS.STS_HALT
242  * and the process speed is down when the roothub port enters U3,
243  * long delay for the handshake of STS_HALT is neeed in xhci_suspend()
244  * by using the XHCI_SLOW_SUSPEND quirk.
245  */
246 #define SET_XHCI_PLAT_PRIV_FOR_RCAR(firmware)				\
247 	.firmware_name = firmware,					\
248 	.quirks = XHCI_NO_64BIT_SUPPORT | XHCI_TRUST_TX_LENGTH |	\
249 		  XHCI_SLOW_SUSPEND,					\
250 	.init_quirk = xhci_rcar_init_quirk,				\
251 	.plat_start = xhci_rcar_start,					\
252 	.resume_quirk = xhci_rcar_resume_quirk,
253 
254 static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen2 = {
255 	SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V1)
256 };
257 
258 static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen3 = {
259 	SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V3)
260 };
261 
262 static const struct xhci_plat_priv xhci_plat_renesas_rzv2m = {
263 	.quirks = XHCI_NO_64BIT_SUPPORT | XHCI_TRUST_TX_LENGTH |
264 		  XHCI_SLOW_SUSPEND,
265 	.init_quirk = xhci_rzv2m_init_quirk,
266 	.plat_start = xhci_rzv2m_start,
267 };
268 
269 static const struct of_device_id usb_xhci_of_match[] = {
270 	{
271 		.compatible = "renesas,xhci-r8a7790",
272 		.data = &xhci_plat_renesas_rcar_gen2,
273 	}, {
274 		.compatible = "renesas,xhci-r8a7791",
275 		.data = &xhci_plat_renesas_rcar_gen2,
276 	}, {
277 		.compatible = "renesas,xhci-r8a7793",
278 		.data = &xhci_plat_renesas_rcar_gen2,
279 	}, {
280 		.compatible = "renesas,xhci-r8a7795",
281 		.data = &xhci_plat_renesas_rcar_gen3,
282 	}, {
283 		.compatible = "renesas,xhci-r8a7796",
284 		.data = &xhci_plat_renesas_rcar_gen3,
285 	}, {
286 		.compatible = "renesas,rcar-gen2-xhci",
287 		.data = &xhci_plat_renesas_rcar_gen2,
288 	}, {
289 		.compatible = "renesas,rcar-gen3-xhci",
290 		.data = &xhci_plat_renesas_rcar_gen3,
291 	}, {
292 		.compatible = "renesas,rzv2m-xhci",
293 		.data = &xhci_plat_renesas_rzv2m,
294 	},
295 	{ },
296 };
297 MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
298 
299 static int xhci_renesas_probe(struct platform_device *pdev)
300 {
301 	const struct xhci_plat_priv *priv_match;
302 
303 	priv_match = of_device_get_match_data(&pdev->dev);
304 
305 	return xhci_plat_probe(pdev, NULL, priv_match);
306 }
307 
308 static struct platform_driver usb_xhci_renesas_driver = {
309 	.probe	= xhci_renesas_probe,
310 	.remove	= xhci_plat_remove,
311 	.shutdown = usb_hcd_platform_shutdown,
312 	.driver	= {
313 		.name = "xhci-renesas-hcd",
314 		.pm = &xhci_plat_pm_ops,
315 		.of_match_table = of_match_ptr(usb_xhci_of_match),
316 	},
317 };
318 module_platform_driver(usb_xhci_renesas_driver);
319 
320 MODULE_DESCRIPTION("xHCI Platform Host Controller Driver for Renesas R-Car and RZ");
321 MODULE_LICENSE("GPL");
322