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