1 // SPDX-License-Identifier: GPL-2.0
3 * xHCI host controller driver for R-Car SoCs
5 * Copyright (C) 2014 Renesas Electronics Corporation
8 #include <linux/firmware.h>
9 #include <linux/iopoll.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
13 #include <linux/usb/phy.h>
16 #include "xhci-plat.h"
17 #include "xhci-rzv2m.h"
19 #define XHCI_RCAR_FIRMWARE_NAME_V1 "/*(DEBLOBBED)*/"
20 #define XHCI_RCAR_FIRMWARE_NAME_V3 "/*(DEBLOBBED)*/"
23 * - The V3 firmware is for all R-Car Gen3
24 * - The V2 firmware is possible to use on R-Car Gen2. However, the V2 causes
25 * performance degradation. So, this driver continues to use the V1 if R-Car
27 * - The V1 firmware is impossible to use on R-Car Gen3.
31 /*** Register Offset ***/
32 #define RCAR_USB3_AXH_STA 0x104 /* AXI Host Control Status */
33 #define RCAR_USB3_INT_ENA 0x224 /* Interrupt Enable */
34 #define RCAR_USB3_DL_CTRL 0x250 /* FW Download Control & Status */
35 #define RCAR_USB3_FW_DATA0 0x258 /* FW Data0 */
37 #define RCAR_USB3_LCLK 0xa44 /* LCLK Select */
38 #define RCAR_USB3_CONF1 0xa48 /* USB3.0 Configuration1 */
39 #define RCAR_USB3_CONF2 0xa5c /* USB3.0 Configuration2 */
40 #define RCAR_USB3_CONF3 0xaa8 /* USB3.0 Configuration3 */
41 #define RCAR_USB3_RX_POL 0xab0 /* USB3.0 RX Polarity */
42 #define RCAR_USB3_TX_POL 0xab8 /* USB3.0 TX Polarity */
44 /*** Register Settings ***/
45 /* AXI Host Control Status */
46 #define RCAR_USB3_AXH_STA_B3_PLL_ACTIVE 0x00010000
47 #define RCAR_USB3_AXH_STA_B2_PLL_ACTIVE 0x00000001
48 #define RCAR_USB3_AXH_STA_PLL_ACTIVE_MASK (RCAR_USB3_AXH_STA_B3_PLL_ACTIVE | \
49 RCAR_USB3_AXH_STA_B2_PLL_ACTIVE)
51 /* Interrupt Enable */
52 #define RCAR_USB3_INT_XHC_ENA 0x00000001
53 #define RCAR_USB3_INT_PME_ENA 0x00000002
54 #define RCAR_USB3_INT_HSE_ENA 0x00000004
55 #define RCAR_USB3_INT_ENA_VAL (RCAR_USB3_INT_XHC_ENA | \
56 RCAR_USB3_INT_PME_ENA | RCAR_USB3_INT_HSE_ENA)
58 /* FW Download Control & Status */
59 #define RCAR_USB3_DL_CTRL_ENABLE 0x00000001
60 #define RCAR_USB3_DL_CTRL_FW_SUCCESS 0x00000010
61 #define RCAR_USB3_DL_CTRL_FW_SET_DATA0 0x00000100
64 #define RCAR_USB3_LCLK_ENA_VAL 0x01030001
66 /* USB3.0 Configuration */
67 #define RCAR_USB3_CONF1_VAL 0x00030204
68 #define RCAR_USB3_CONF2_VAL 0x00030300
69 #define RCAR_USB3_CONF3_VAL 0x13802007
72 #define RCAR_USB3_RX_POL_VAL BIT(21)
73 #define RCAR_USB3_TX_POL_VAL BIT(4)
75 static void xhci_rcar_start_gen2(struct usb_hcd *hcd)
78 writel(RCAR_USB3_LCLK_ENA_VAL, hcd->regs + RCAR_USB3_LCLK);
79 /* USB3.0 Configuration */
80 writel(RCAR_USB3_CONF1_VAL, hcd->regs + RCAR_USB3_CONF1);
81 writel(RCAR_USB3_CONF2_VAL, hcd->regs + RCAR_USB3_CONF2);
82 writel(RCAR_USB3_CONF3_VAL, hcd->regs + RCAR_USB3_CONF3);
84 writel(RCAR_USB3_RX_POL_VAL, hcd->regs + RCAR_USB3_RX_POL);
85 writel(RCAR_USB3_TX_POL_VAL, hcd->regs + RCAR_USB3_TX_POL);
88 static int xhci_rcar_is_gen2(struct device *dev)
90 struct device_node *node = dev->of_node;
92 return of_device_is_compatible(node, "renesas,xhci-r8a7790") ||
93 of_device_is_compatible(node, "renesas,xhci-r8a7791") ||
94 of_device_is_compatible(node, "renesas,xhci-r8a7793") ||
95 of_device_is_compatible(node, "renesas,rcar-gen2-xhci");
98 static void xhci_rcar_start(struct usb_hcd *hcd)
102 if (hcd->regs != NULL) {
103 /* Interrupt Enable */
104 temp = readl(hcd->regs + RCAR_USB3_INT_ENA);
105 temp |= RCAR_USB3_INT_ENA_VAL;
106 writel(temp, hcd->regs + RCAR_USB3_INT_ENA);
107 if (xhci_rcar_is_gen2(hcd->self.controller))
108 xhci_rcar_start_gen2(hcd);
112 static int xhci_rcar_download_firmware(struct usb_hcd *hcd)
114 struct device *dev = hcd->self.controller;
115 void __iomem *regs = hcd->regs;
116 struct xhci_plat_priv *priv = hcd_to_xhci_priv(hcd);
117 const struct firmware *fw;
118 int retval, index, j;
122 * According to the datasheet, "Upon the completion of FW Download,
123 * there is no need to write or reload FW".
125 if (readl(regs + RCAR_USB3_DL_CTRL) & RCAR_USB3_DL_CTRL_FW_SUCCESS)
128 /* request R-Car USB3.0 firmware */
129 retval = reject_firmware(&fw, priv->firmware_name, dev);
133 /* download R-Car USB3.0 firmware */
134 temp = readl(regs + RCAR_USB3_DL_CTRL);
135 temp |= RCAR_USB3_DL_CTRL_ENABLE;
136 writel(temp, regs + RCAR_USB3_DL_CTRL);
138 for (index = 0; index < fw->size; index += 4) {
139 /* to avoid reading beyond the end of the buffer */
140 for (data = 0, j = 3; j >= 0; j--) {
141 if ((j + index) < fw->size)
142 data |= fw->data[index + j] << (8 * j);
144 writel(data, regs + RCAR_USB3_FW_DATA0);
145 temp = readl(regs + RCAR_USB3_DL_CTRL);
146 temp |= RCAR_USB3_DL_CTRL_FW_SET_DATA0;
147 writel(temp, regs + RCAR_USB3_DL_CTRL);
149 retval = readl_poll_timeout_atomic(regs + RCAR_USB3_DL_CTRL,
150 val, !(val & RCAR_USB3_DL_CTRL_FW_SET_DATA0),
156 temp = readl(regs + RCAR_USB3_DL_CTRL);
157 temp &= ~RCAR_USB3_DL_CTRL_ENABLE;
158 writel(temp, regs + RCAR_USB3_DL_CTRL);
160 retval = readl_poll_timeout_atomic((regs + RCAR_USB3_DL_CTRL),
161 val, val & RCAR_USB3_DL_CTRL_FW_SUCCESS, 1, 10000);
163 release_firmware(fw);
168 static bool xhci_rcar_wait_for_pll_active(struct usb_hcd *hcd)
171 u32 val, mask = RCAR_USB3_AXH_STA_PLL_ACTIVE_MASK;
173 retval = readl_poll_timeout_atomic(hcd->regs + RCAR_USB3_AXH_STA,
174 val, (val & mask) == mask, 1, 1000);
178 /* This function needs to initialize a "phy" of usb before */
179 static int xhci_rcar_init_quirk(struct usb_hcd *hcd)
181 /* If hcd->regs is NULL, we don't just call the following function */
185 if (!xhci_rcar_wait_for_pll_active(hcd))
188 return xhci_rcar_download_firmware(hcd);
191 static int xhci_rcar_resume_quirk(struct usb_hcd *hcd)
195 ret = xhci_rcar_download_firmware(hcd);
197 xhci_rcar_start(hcd);
203 * On R-Car Gen2 and Gen3, the AC64 bit (bit 0) of HCCPARAMS1 is set
204 * to 1. However, these SoCs don't support 64-bit address memory
205 * pointers. So, this driver clears the AC64 bit of xhci->hcc_params
206 * to call dma_set_coherent_mask(dev, DMA_BIT_MASK(32)) in
207 * xhci_gen_setup() by using the XHCI_NO_64BIT_SUPPORT quirk.
209 * And, since the firmware/internal CPU control the USBSTS.STS_HALT
210 * and the process speed is down when the roothub port enters U3,
211 * long delay for the handshake of STS_HALT is neeed in xhci_suspend()
212 * by using the XHCI_SLOW_SUSPEND quirk.
214 #define SET_XHCI_PLAT_PRIV_FOR_RCAR(firmware) \
215 .firmware_name = firmware, \
216 .quirks = XHCI_NO_64BIT_SUPPORT | XHCI_TRUST_TX_LENGTH | \
218 .init_quirk = xhci_rcar_init_quirk, \
219 .plat_start = xhci_rcar_start, \
220 .resume_quirk = xhci_rcar_resume_quirk,
222 static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen2 = {
223 SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V1)
226 static const struct xhci_plat_priv xhci_plat_renesas_rcar_gen3 = {
227 SET_XHCI_PLAT_PRIV_FOR_RCAR(XHCI_RCAR_FIRMWARE_NAME_V3)
230 static const struct xhci_plat_priv xhci_plat_renesas_rzv2m = {
231 .quirks = XHCI_NO_64BIT_SUPPORT | XHCI_TRUST_TX_LENGTH |
233 .init_quirk = xhci_rzv2m_init_quirk,
234 .plat_start = xhci_rzv2m_start,
237 static const struct of_device_id usb_xhci_of_match[] = {
239 .compatible = "renesas,xhci-r8a7790",
240 .data = &xhci_plat_renesas_rcar_gen2,
242 .compatible = "renesas,xhci-r8a7791",
243 .data = &xhci_plat_renesas_rcar_gen2,
245 .compatible = "renesas,xhci-r8a7793",
246 .data = &xhci_plat_renesas_rcar_gen2,
248 .compatible = "renesas,xhci-r8a7795",
249 .data = &xhci_plat_renesas_rcar_gen3,
251 .compatible = "renesas,xhci-r8a7796",
252 .data = &xhci_plat_renesas_rcar_gen3,
254 .compatible = "renesas,rcar-gen2-xhci",
255 .data = &xhci_plat_renesas_rcar_gen2,
257 .compatible = "renesas,rcar-gen3-xhci",
258 .data = &xhci_plat_renesas_rcar_gen3,
260 .compatible = "renesas,rzv2m-xhci",
261 .data = &xhci_plat_renesas_rzv2m,
265 MODULE_DEVICE_TABLE(of, usb_xhci_of_match);
267 static int xhci_renesas_probe(struct platform_device *pdev)
269 const struct xhci_plat_priv *priv_match;
271 priv_match = of_device_get_match_data(&pdev->dev);
273 return xhci_plat_probe(pdev, NULL, priv_match);
276 static struct platform_driver usb_xhci_renesas_driver = {
277 .probe = xhci_renesas_probe,
278 .remove_new = xhci_plat_remove,
279 .shutdown = usb_hcd_platform_shutdown,
281 .name = "xhci-renesas-hcd",
282 .pm = &xhci_plat_pm_ops,
283 .of_match_table = usb_xhci_of_match,
286 module_platform_driver(usb_xhci_renesas_driver);
288 MODULE_DESCRIPTION("xHCI Platform Host Controller Driver for Renesas R-Car and RZ");
289 MODULE_LICENSE("GPL");