dm: usb: exynos: Drop legacy USB code
[platform/kernel/u-boot.git] / drivers / usb / host / xhci-exynos5.c
1 /*
2  * SAMSUNG EXYNOS5 USB HOST XHCI Controller
3  *
4  * Copyright (C) 2012 Samsung Electronics Co.Ltd
5  *      Vivek Gautam <gautam.vivek@samsung.com>
6  *      Vikas Sajjan <vikas.sajjan@samsung.com>
7  *
8  * SPDX-License-Identifier:     GPL-2.0+
9  */
10
11 /*
12  * This file is a conglomeration for DWC3-init sequence and further
13  * exynos5 specific PHY-init sequence.
14  */
15
16 #include <common.h>
17 #include <dm.h>
18 #include <fdtdec.h>
19 #include <libfdt.h>
20 #include <malloc.h>
21 #include <usb.h>
22 #include <watchdog.h>
23 #include <asm/arch/cpu.h>
24 #include <asm/arch/power.h>
25 #include <asm/arch/xhci-exynos.h>
26 #include <asm/gpio.h>
27 #include <asm-generic/errno.h>
28 #include <linux/compat.h>
29 #include <linux/usb/dwc3.h>
30
31 #include "xhci.h"
32
33 /* Declare global data pointer */
34 DECLARE_GLOBAL_DATA_PTR;
35
36 struct exynos_xhci_platdata {
37         fdt_addr_t hcd_base;
38         fdt_addr_t phy_base;
39         struct gpio_desc vbus_gpio;
40 };
41
42 /**
43  * Contains pointers to register base addresses
44  * for the usb controller.
45  */
46 struct exynos_xhci {
47         struct usb_platdata usb_plat;
48         struct xhci_ctrl ctrl;
49         struct exynos_usb3_phy *usb3_phy;
50         struct xhci_hccr *hcd;
51         struct dwc3 *dwc3_reg;
52 };
53
54 static int xhci_usb_ofdata_to_platdata(struct udevice *dev)
55 {
56         struct exynos_xhci_platdata *plat = dev_get_platdata(dev);
57         const void *blob = gd->fdt_blob;
58         unsigned int node;
59         int depth;
60
61         /*
62          * Get the base address for XHCI controller from the device node
63          */
64         plat->hcd_base = fdtdec_get_addr(blob, dev->of_offset, "reg");
65         if (plat->hcd_base == FDT_ADDR_T_NONE) {
66                 debug("Can't get the XHCI register base address\n");
67                 return -ENXIO;
68         }
69
70         depth = 0;
71         node = fdtdec_next_compatible_subnode(blob, dev->of_offset,
72                                 COMPAT_SAMSUNG_EXYNOS5_USB3_PHY, &depth);
73         if (node <= 0) {
74                 debug("XHCI: Can't get device node for usb3-phy controller\n");
75                 return -ENODEV;
76         }
77
78         /*
79          * Get the base address for usbphy from the device node
80          */
81         plat->phy_base = fdtdec_get_addr(blob, node, "reg");
82         if (plat->phy_base == FDT_ADDR_T_NONE) {
83                 debug("Can't get the usbphy register address\n");
84                 return -ENXIO;
85         }
86
87         /* Vbus gpio */
88         gpio_request_by_name(dev, "samsung,vbus-gpio", 0,
89                              &plat->vbus_gpio, GPIOD_IS_OUT);
90
91         return 0;
92 }
93
94 static void exynos5_usb3_phy_init(struct exynos_usb3_phy *phy)
95 {
96         u32 reg;
97
98         /* enabling usb_drd phy */
99         set_usbdrd_phy_ctrl(POWER_USB_DRD_PHY_CTRL_EN);
100
101         /* Reset USB 3.0 PHY */
102         writel(0x0, &phy->phy_reg0);
103
104         clrbits_le32(&phy->phy_param0,
105                         /* Select PHY CLK source */
106                         PHYPARAM0_REF_USE_PAD |
107                         /* Set Loss-of-Signal Detector sensitivity */
108                         PHYPARAM0_REF_LOSLEVEL_MASK);
109         setbits_le32(&phy->phy_param0, PHYPARAM0_REF_LOSLEVEL);
110
111         writel(0x0, &phy->phy_resume);
112
113         /*
114          * Setting the Frame length Adj value[6:1] to default 0x20
115          * See xHCI 1.0 spec, 5.2.4
116          */
117         setbits_le32(&phy->link_system,
118                         LINKSYSTEM_XHCI_VERSION_CONTROL |
119                         LINKSYSTEM_FLADJ(0x20));
120
121         /* Set Tx De-Emphasis level */
122         clrbits_le32(&phy->phy_param1, PHYPARAM1_PCS_TXDEEMPH_MASK);
123         setbits_le32(&phy->phy_param1, PHYPARAM1_PCS_TXDEEMPH);
124
125         setbits_le32(&phy->phy_batchg, PHYBATCHG_UTMI_CLKSEL);
126
127         /* PHYTEST POWERDOWN Control */
128         clrbits_le32(&phy->phy_test,
129                         PHYTEST_POWERDOWN_SSP |
130                         PHYTEST_POWERDOWN_HSP);
131
132         /* UTMI Power Control */
133         writel(PHYUTMI_OTGDISABLE, &phy->phy_utmi);
134
135                 /* Use core clock from main PLL */
136         reg = PHYCLKRST_REFCLKSEL_EXT_REFCLK |
137                 /* Default 24Mhz crystal clock */
138                 PHYCLKRST_FSEL(FSEL_CLKSEL_24M) |
139                 PHYCLKRST_MPLL_MULTIPLIER_24MHZ_REF |
140                 PHYCLKRST_SSC_REFCLKSEL(0x88) |
141                 /* Force PortReset of PHY */
142                 PHYCLKRST_PORTRESET |
143                 /* Digital power supply in normal operating mode */
144                 PHYCLKRST_RETENABLEN |
145                 /* Enable ref clock for SS function */
146                 PHYCLKRST_REF_SSP_EN |
147                 /* Enable spread spectrum */
148                 PHYCLKRST_SSC_EN |
149                 /* Power down HS Bias and PLL blocks in suspend mode */
150                 PHYCLKRST_COMMONONN;
151
152         writel(reg, &phy->phy_clk_rst);
153
154         /* giving time to Phy clock to settle before resetting */
155         udelay(10);
156
157         reg &= ~PHYCLKRST_PORTRESET;
158         writel(reg, &phy->phy_clk_rst);
159 }
160
161 static void exynos5_usb3_phy_exit(struct exynos_usb3_phy *phy)
162 {
163         setbits_le32(&phy->phy_utmi,
164                         PHYUTMI_OTGDISABLE |
165                         PHYUTMI_FORCESUSPEND |
166                         PHYUTMI_FORCESLEEP);
167
168         clrbits_le32(&phy->phy_clk_rst,
169                         PHYCLKRST_REF_SSP_EN |
170                         PHYCLKRST_SSC_EN |
171                         PHYCLKRST_COMMONONN);
172
173         /* PHYTEST POWERDOWN Control to remove leakage current */
174         setbits_le32(&phy->phy_test,
175                         PHYTEST_POWERDOWN_SSP |
176                         PHYTEST_POWERDOWN_HSP);
177
178         /* disabling usb_drd phy */
179         set_usbdrd_phy_ctrl(POWER_USB_DRD_PHY_CTRL_DISABLE);
180 }
181
182 static void dwc3_set_mode(struct dwc3 *dwc3_reg, u32 mode)
183 {
184         clrsetbits_le32(&dwc3_reg->g_ctl,
185                         DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG),
186                         DWC3_GCTL_PRTCAPDIR(mode));
187 }
188
189 static void dwc3_core_soft_reset(struct dwc3 *dwc3_reg)
190 {
191         /* Before Resetting PHY, put Core in Reset */
192         setbits_le32(&dwc3_reg->g_ctl,
193                         DWC3_GCTL_CORESOFTRESET);
194
195         /* Assert USB3 PHY reset */
196         setbits_le32(&dwc3_reg->g_usb3pipectl[0],
197                         DWC3_GUSB3PIPECTL_PHYSOFTRST);
198
199         /* Assert USB2 PHY reset */
200         setbits_le32(&dwc3_reg->g_usb2phycfg,
201                         DWC3_GUSB2PHYCFG_PHYSOFTRST);
202
203         mdelay(100);
204
205         /* Clear USB3 PHY reset */
206         clrbits_le32(&dwc3_reg->g_usb3pipectl[0],
207                         DWC3_GUSB3PIPECTL_PHYSOFTRST);
208
209         /* Clear USB2 PHY reset */
210         clrbits_le32(&dwc3_reg->g_usb2phycfg,
211                         DWC3_GUSB2PHYCFG_PHYSOFTRST);
212
213         /* After PHYs are stable we can take Core out of reset state */
214         clrbits_le32(&dwc3_reg->g_ctl,
215                         DWC3_GCTL_CORESOFTRESET);
216 }
217
218 static int dwc3_core_init(struct dwc3 *dwc3_reg)
219 {
220         u32 reg;
221         u32 revision;
222         unsigned int dwc3_hwparams1;
223
224         revision = readl(&dwc3_reg->g_snpsid);
225         /* This should read as U3 followed by revision number */
226         if ((revision & DWC3_GSNPSID_MASK) != 0x55330000) {
227                 puts("this is not a DesignWare USB3 DRD Core\n");
228                 return -EINVAL;
229         }
230
231         dwc3_core_soft_reset(dwc3_reg);
232
233         dwc3_hwparams1 = readl(&dwc3_reg->g_hwparams1);
234
235         reg = readl(&dwc3_reg->g_ctl);
236         reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
237         reg &= ~DWC3_GCTL_DISSCRAMBLE;
238         switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc3_hwparams1)) {
239         case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
240                 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
241                 break;
242         default:
243                 debug("No power optimization available\n");
244         }
245
246         /*
247          * WORKAROUND: DWC3 revisions <1.90a have a bug
248          * where the device can fail to connect at SuperSpeed
249          * and falls back to high-speed mode which causes
250          * the device to enter a Connect/Disconnect loop
251          */
252         if ((revision & DWC3_REVISION_MASK) < 0x190a)
253                 reg |= DWC3_GCTL_U2RSTECN;
254
255         writel(reg, &dwc3_reg->g_ctl);
256
257         return 0;
258 }
259
260 static int exynos_xhci_core_init(struct exynos_xhci *exynos)
261 {
262         int ret;
263
264         exynos5_usb3_phy_init(exynos->usb3_phy);
265
266         ret = dwc3_core_init(exynos->dwc3_reg);
267         if (ret) {
268                 debug("failed to initialize core\n");
269                 return -EINVAL;
270         }
271
272         /* We are hard-coding DWC3 core to Host Mode */
273         dwc3_set_mode(exynos->dwc3_reg, DWC3_GCTL_PRTCAP_HOST);
274
275         return 0;
276 }
277
278 static void exynos_xhci_core_exit(struct exynos_xhci *exynos)
279 {
280         exynos5_usb3_phy_exit(exynos->usb3_phy);
281 }
282
283 static int xhci_usb_probe(struct udevice *dev)
284 {
285         struct exynos_xhci_platdata *plat = dev_get_platdata(dev);
286         struct exynos_xhci *ctx = dev_get_priv(dev);
287         struct xhci_hcor *hcor;
288         int ret;
289
290         ctx->hcd = (struct xhci_hccr *)plat->hcd_base;
291         ctx->usb3_phy = (struct exynos_usb3_phy *)plat->phy_base;
292         ctx->dwc3_reg = (struct dwc3 *)((char *)(ctx->hcd) + DWC3_REG_OFFSET);
293         hcor = (struct xhci_hcor *)((uint32_t)ctx->hcd +
294                         HC_LENGTH(xhci_readl(&ctx->hcd->cr_capbase)));
295
296         /* setup the Vbus gpio here */
297         if (dm_gpio_is_valid(&plat->vbus_gpio))
298                 dm_gpio_set_value(&plat->vbus_gpio, 1);
299
300         ret = exynos_xhci_core_init(ctx);
301         if (ret) {
302                 puts("XHCI: failed to initialize controller\n");
303                 return -EINVAL;
304         }
305
306         return xhci_register(dev, ctx->hcd, hcor);
307 }
308
309 static int xhci_usb_remove(struct udevice *dev)
310 {
311         struct exynos_xhci *ctx = dev_get_priv(dev);
312         int ret;
313
314         ret = xhci_deregister(dev);
315         if (ret)
316                 return ret;
317         exynos_xhci_core_exit(ctx);
318
319         return 0;
320 }
321
322 static const struct udevice_id xhci_usb_ids[] = {
323         { .compatible = "samsung,exynos5250-xhci" },
324         { }
325 };
326
327 U_BOOT_DRIVER(usb_xhci) = {
328         .name   = "xhci_exynos",
329         .id     = UCLASS_USB,
330         .of_match = xhci_usb_ids,
331         .ofdata_to_platdata = xhci_usb_ofdata_to_platdata,
332         .probe = xhci_usb_probe,
333         .remove = xhci_usb_remove,
334         .ops    = &xhci_usb_ops,
335         .platdata_auto_alloc_size = sizeof(struct exynos_xhci_platdata),
336         .priv_auto_alloc_size = sizeof(struct exynos_xhci),
337         .flags  = DM_FLAG_ALLOC_PRIV_DMA,
338 };