1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Sanchayan Maity <sanchayan.maity@toradex.com>
4 * Copyright (C) 2015 Toradex AG
6 * Based on ehci-mx6 driver
14 #include <asm/global_data.h>
15 #include <linux/compiler.h>
17 #include <asm-generic/gpio.h>
18 #include <asm/arch/clock.h>
19 #include <asm/arch/imx-regs.h>
20 #include <asm/arch/crm_regs.h>
21 #include <asm/mach-imx/iomux-v3.h>
22 #include <asm/mach-imx/regs-usbphy.h>
23 #include <linux/delay.h>
24 #include <usb/ehci-ci.h>
25 #include <linux/libfdt.h>
30 #define USB_NC_REG_OFFSET 0x00000800
32 #define ANADIG_PLL_CTRL_EN_USB_CLKS (1 << 6)
34 #define UCTRL_OVER_CUR_POL (1 << 8) /* OTG Polarity of Overcurrent */
35 #define UCTRL_OVER_CUR_DIS (1 << 7) /* Disable OTG Overcurrent Detection */
38 #define UCMD_RUN_STOP (1 << 0) /* controller run/stop */
39 #define UCMD_RESET (1 << 1) /* controller reset */
41 DECLARE_GLOBAL_DATA_PTR;
43 static const unsigned phy_bases[] = {
48 static const unsigned nc_reg_bases[] = {
53 static void usb_internal_phy_clock_gate(int index)
55 void __iomem *phy_reg;
57 phy_reg = (void __iomem *)phy_bases[index];
58 clrbits_le32(phy_reg + USBPHY_CTRL, USBPHY_CTRL_CLKGATE);
61 static void usb_power_config(int index)
63 struct anadig_reg __iomem *anadig =
64 (struct anadig_reg __iomem *)ANADIG_BASE_ADDR;
65 void __iomem *pll_ctrl;
69 pll_ctrl = &anadig->pll3_ctrl;
70 clrbits_le32(pll_ctrl, ANADIG_PLL3_CTRL_BYPASS);
71 setbits_le32(pll_ctrl, ANADIG_PLL3_CTRL_ENABLE
72 | ANADIG_PLL3_CTRL_POWERDOWN
73 | ANADIG_PLL_CTRL_EN_USB_CLKS);
76 pll_ctrl = &anadig->pll7_ctrl;
77 clrbits_le32(pll_ctrl, ANADIG_PLL7_CTRL_BYPASS);
78 setbits_le32(pll_ctrl, ANADIG_PLL7_CTRL_ENABLE
79 | ANADIG_PLL7_CTRL_POWERDOWN
80 | ANADIG_PLL_CTRL_EN_USB_CLKS);
87 static void usb_phy_enable(int index, struct usb_ehci *ehci)
89 void __iomem *phy_reg;
90 void __iomem *phy_ctrl;
91 void __iomem *usb_cmd;
93 phy_reg = (void __iomem *)phy_bases[index];
94 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
95 usb_cmd = (void __iomem *)&ehci->usbcmd;
98 clrbits_le32(usb_cmd, UCMD_RUN_STOP);
99 while (readl(usb_cmd) & UCMD_RUN_STOP)
102 setbits_le32(usb_cmd, UCMD_RESET);
103 while (readl(usb_cmd) & UCMD_RESET)
106 /* Reset USBPHY module */
107 setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST);
110 /* Remove CLKGATE and SFTRST */
111 clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST);
114 /* Power up the PHY */
115 writel(0, phy_reg + USBPHY_PWD);
117 /* Enable FS/LS device */
118 setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 |
119 USBPHY_CTRL_ENUTMILEVEL3);
122 static void usb_oc_config(int index)
126 ctrl = (void __iomem *)(nc_reg_bases[index] + USB_NC_REG_OFFSET);
128 setbits_le32(ctrl, UCTRL_OVER_CUR_POL);
129 setbits_le32(ctrl, UCTRL_OVER_CUR_DIS);
132 int __weak board_usb_phy_mode(int port)
137 int __weak board_ehci_hcd_init(int port)
142 int ehci_vf_common_init(struct usb_ehci *ehci, int index)
146 /* Do board specific initialisation */
147 ret = board_ehci_hcd_init(index);
151 usb_power_config(index);
152 usb_oc_config(index);
153 usb_internal_phy_clock_gate(index);
154 usb_phy_enable(index, ehci);
159 #if !CONFIG_IS_ENABLED(DM_USB)
160 int ehci_hcd_init(int index, enum usb_init_type init,
161 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
163 struct usb_ehci *ehci;
164 enum usb_init_type type;
167 if (index >= ARRAY_SIZE(nc_reg_bases))
170 ehci = (struct usb_ehci *)nc_reg_bases[index];
172 ret = ehci_vf_common_init(index);
176 *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
177 *hcor = (struct ehci_hcor *)((uint32_t)*hccr +
178 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
180 type = board_usb_phy_mode(index);
184 if (init == USB_INIT_DEVICE) {
185 setbits_le32(&ehci->usbmode, CM_DEVICE);
186 writel((PORT_PTS_UTMI | PORT_PTS_PTW), &ehci->portsc);
187 setbits_le32(&ehci->portsc, USB_EN);
188 } else if (init == USB_INIT_HOST) {
189 setbits_le32(&ehci->usbmode, CM_HOST);
190 writel((PORT_PTS_UTMI | PORT_PTS_PTW), &ehci->portsc);
191 setbits_le32(&ehci->portsc, USB_EN);
197 int ehci_hcd_stop(int index)
202 /* Possible port types (dual role mode) */
205 DR_MODE_HOST, /* supports host operation */
206 DR_MODE_DEVICE, /* supports device operation */
207 DR_MODE_OTG, /* supports both */
210 struct ehci_vf_priv_data {
211 struct ehci_ctrl ctrl;
212 struct usb_ehci *ehci;
213 struct gpio_desc cdet_gpio;
214 enum usb_init_type init_type;
215 enum dr_mode dr_mode;
219 static int vf_usb_of_to_plat(struct udevice *dev)
221 struct ehci_vf_priv_data *priv = dev_get_priv(dev);
222 const void *dt_blob = gd->fdt_blob;
223 int node = dev_of_offset(dev);
226 priv->portnr = dev_seq(dev);
228 priv->ehci = dev_read_addr_ptr(dev);
229 mode = fdt_getprop(dt_blob, node, "dr_mode", NULL);
231 if (0 == strcmp(mode, "host")) {
232 priv->dr_mode = DR_MODE_HOST;
233 priv->init_type = USB_INIT_HOST;
234 } else if (0 == strcmp(mode, "peripheral")) {
235 priv->dr_mode = DR_MODE_DEVICE;
236 priv->init_type = USB_INIT_DEVICE;
237 } else if (0 == strcmp(mode, "otg")) {
238 priv->dr_mode = DR_MODE_OTG;
240 * We set init_type to device by default when OTG
241 * mode is requested. If a valid gpio is provided
242 * we will switch the init_type based on the state
245 priv->init_type = USB_INIT_DEVICE;
247 debug("%s: Cannot decode dr_mode '%s'\n",
252 priv->dr_mode = DR_MODE_HOST;
253 priv->init_type = USB_INIT_HOST;
256 if (priv->dr_mode == DR_MODE_OTG) {
257 gpio_request_by_name_nodev(offset_to_ofnode(node),
258 "fsl,cdet-gpio", 0, &priv->cdet_gpio,
260 if (dm_gpio_is_valid(&priv->cdet_gpio)) {
261 if (dm_gpio_get_value(&priv->cdet_gpio))
262 priv->init_type = USB_INIT_DEVICE;
264 priv->init_type = USB_INIT_HOST;
271 static int vf_init_after_reset(struct ehci_ctrl *dev)
273 struct ehci_vf_priv_data *priv = dev->priv;
274 enum usb_init_type type = priv->init_type;
275 struct usb_ehci *ehci = priv->ehci;
278 ret = ehci_vf_common_init(priv->ehci, priv->portnr);
282 if (type == USB_INIT_DEVICE)
285 setbits_le32(&ehci->usbmode, CM_HOST);
286 writel((PORT_PTS_UTMI | PORT_PTS_PTW), &ehci->portsc);
287 setbits_le32(&ehci->portsc, USB_EN);
294 static const struct ehci_ops vf_ehci_ops = {
295 .init_after_reset = vf_init_after_reset
298 static int vf_usb_bind(struct udevice *dev)
301 * Without this hack, if we return ENODEV for USB Controller 0, on
302 * probe for the next controller, USB Controller 1 will be given a
303 * sequence number of 0. This conflicts with our requirement of
304 * sequence numbers while initialising the peripherals.
306 * FIXME: Check that this still works OK with the new sequence numbers
312 static int ehci_usb_probe(struct udevice *dev)
314 struct usb_plat *plat = dev_get_plat(dev);
315 struct ehci_vf_priv_data *priv = dev_get_priv(dev);
316 struct usb_ehci *ehci = priv->ehci;
317 struct ehci_hccr *hccr;
318 struct ehci_hcor *hcor;
321 ret = ehci_vf_common_init(ehci, priv->portnr);
325 if (priv->init_type != plat->init_type)
328 if (priv->init_type == USB_INIT_HOST) {
329 setbits_le32(&ehci->usbmode, CM_HOST);
330 writel((PORT_PTS_UTMI | PORT_PTS_PTW), &ehci->portsc);
331 setbits_le32(&ehci->portsc, USB_EN);
336 hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
337 hcor = (struct ehci_hcor *)((uint32_t)hccr +
338 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
340 return ehci_register(dev, hccr, hcor, &vf_ehci_ops, 0, priv->init_type);
343 static const struct udevice_id vf_usb_ids[] = {
344 { .compatible = "fsl,vf610-usb" },
348 U_BOOT_DRIVER(usb_ehci) = {
351 .of_match = vf_usb_ids,
353 .probe = ehci_usb_probe,
354 .remove = ehci_deregister,
355 .ops = &ehci_usb_ops,
356 .of_to_plat = vf_usb_of_to_plat,
357 .plat_auto = sizeof(struct usb_plat),
358 .priv_auto = sizeof(struct ehci_vf_priv_data),
359 .flags = DM_FLAG_ALLOC_PRIV_DMA,