1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2009, 2011, 2016 Freescale Semiconductor, Inc.
5 * (C) Copyright 2008, Excito Elektronik i Sk=E5ne AB
15 #include <asm/global_data.h>
17 #include <linux/delay.h>
18 #include <usb/ehci-ci.h>
21 #include <fdt_support.h>
26 DECLARE_GLOBAL_DATA_PTR;
28 #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
29 #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
32 #if CONFIG_IS_ENABLED(DM_USB)
33 struct ehci_fsl_priv {
34 struct ehci_ctrl ehci;
40 static void set_txfifothresh(struct usb_ehci *, u32);
41 #if CONFIG_IS_ENABLED(DM_USB)
42 static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
43 struct ehci_hccr *hccr, struct ehci_hcor *hcor);
45 static int ehci_fsl_init(int index, struct usb_ehci *ehci,
46 struct ehci_hccr *hccr, struct ehci_hcor *hcor);
49 /* Check USB PHY clock valid */
50 static int usb_phy_clk_valid(struct usb_ehci *ehci)
52 if (!((in_be32(&ehci->control) & PHY_CLK_VALID) ||
53 in_be32(&ehci->prictrl))) {
54 printf("USB PHY clock invalid!\n");
61 #if CONFIG_IS_ENABLED(DM_USB)
62 static int ehci_fsl_of_to_plat(struct udevice *dev)
64 struct ehci_fsl_priv *priv = dev_get_priv(dev);
67 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy_type",
70 priv->phy_type = (char *)prop;
71 debug("phy_type %s\n", priv->phy_type);
77 static int ehci_fsl_init_after_reset(struct ehci_ctrl *ctrl)
79 struct usb_ehci *ehci = NULL;
80 struct ehci_fsl_priv *priv = container_of(ctrl, struct ehci_fsl_priv,
83 ehci = (struct usb_ehci *)lower_32_bits(priv->hcd_base);
85 ehci = (struct usb_ehci *)priv->hcd_base;
88 if (ehci_fsl_init(priv, ehci, priv->ehci.hccr, priv->ehci.hcor) < 0)
94 static const struct ehci_ops fsl_ehci_ops = {
95 .init_after_reset = ehci_fsl_init_after_reset,
98 static int ehci_fsl_probe(struct udevice *dev)
100 struct ehci_fsl_priv *priv = dev_get_priv(dev);
101 struct usb_ehci *ehci = NULL;
102 struct ehci_hccr *hccr;
103 struct ehci_hcor *hcor;
104 struct ehci_ctrl *ehci_ctrl = &priv->ehci;
107 * Get the base address for EHCI controller from the device node
109 priv->hcd_base = dev_read_addr(dev);
110 if (priv->hcd_base == FDT_ADDR_T_NONE) {
111 debug("Can't get the EHCI register base address\n");
115 ehci = (struct usb_ehci *)lower_32_bits(priv->hcd_base);
117 ehci = (struct usb_ehci *)priv->hcd_base;
119 hccr = (struct ehci_hccr *)(&ehci->caplength);
120 hcor = (struct ehci_hcor *)
121 ((void *)hccr + HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
123 ehci_ctrl->has_fsl_erratum_a005275 = has_erratum_a005275();
125 if (ehci_fsl_init(priv, ehci, hccr, hcor) < 0)
128 debug("ehci-fsl: init hccr %p and hcor %p hc_length %d\n",
129 (void *)hccr, (void *)hcor,
130 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
132 return ehci_register(dev, hccr, hcor, &fsl_ehci_ops, 0, USB_INIT_HOST);
135 static const struct udevice_id ehci_usb_ids[] = {
136 { .compatible = "fsl-usb2-mph", },
137 { .compatible = "fsl-usb2-dr", },
141 U_BOOT_DRIVER(ehci_fsl) = {
144 .of_match = ehci_usb_ids,
145 .of_to_plat = ehci_fsl_of_to_plat,
146 .probe = ehci_fsl_probe,
147 .remove = ehci_deregister,
148 .ops = &ehci_usb_ops,
149 .plat_auto = sizeof(struct usb_plat),
150 .priv_auto = sizeof(struct ehci_fsl_priv),
151 .flags = DM_FLAG_ALLOC_PRIV_DMA,
155 * Create the appropriate control structures to manage
156 * a new EHCI host controller.
158 * Excerpts from linux ehci fsl driver.
160 int ehci_hcd_init(int index, enum usb_init_type init,
161 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
163 struct ehci_ctrl *ehci_ctrl = container_of(hccr,
164 struct ehci_ctrl, hccr);
165 struct usb_ehci *ehci = NULL;
169 ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB1_ADDR;
172 ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB2_ADDR;
175 printf("ERROR: wrong controller index!!\n");
179 *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
180 *hcor = (struct ehci_hcor *)((uint32_t) *hccr +
181 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
183 ehci_ctrl->has_fsl_erratum_a005275 = has_erratum_a005275();
185 return ehci_fsl_init(index, ehci, *hccr, *hcor);
189 * Destroy the appropriate control structures corresponding
190 * the the EHCI host controller.
192 int ehci_hcd_stop(int index)
198 #if CONFIG_IS_ENABLED(DM_USB)
199 static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
200 struct ehci_hccr *hccr, struct ehci_hcor *hcor)
202 static int ehci_fsl_init(int index, struct usb_ehci *ehci,
203 struct ehci_hccr *hccr, struct ehci_hcor *hcor)
206 const char *phy_type = NULL;
207 #if !CONFIG_IS_ENABLED(DM_USB)
209 char current_usb_controller[5];
211 #ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
216 if (has_erratum_a007075()) {
218 * A 5ms delay is needed after applying soft-reset to the
219 * controller to let external ULPI phy come out of reset.
220 * This delay needs to be added before re-initializing
221 * the controller after soft-resetting completes
226 /* Set to Host mode */
227 setbits_le32(&ehci->usbmode, CM_HOST);
229 out_be32(&ehci->snoop1, SNOOP_SIZE_2GB);
230 out_be32(&ehci->snoop2, 0x80000000 | SNOOP_SIZE_2GB);
233 #if CONFIG_IS_ENABLED(DM_USB)
235 phy_type = priv->phy_type;
237 memset(current_usb_controller, '\0', 5);
238 snprintf(current_usb_controller, sizeof(current_usb_controller),
241 if (hwconfig_sub(current_usb_controller, "phy_type"))
242 phy_type = hwconfig_subarg(current_usb_controller,
246 phy_type = env_get("usb_phy_type");
249 #ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
250 /* if none specified assume internal UTMI */
251 strcpy(usb_phy, "utmi");
254 printf("WARNING: USB phy type not defined !!\n");
259 if (!strncmp(phy_type, "utmi", 4)) {
260 #if defined(CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY)
261 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
263 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
265 udelay(1000); /* delay required for PHY Clk to appear */
267 out_le32(&(hcor)->or_portsc[0], PORT_PTS_UTMI);
268 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
271 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
273 clrsetbits_be32(&ehci->control, UTMI_PHY_EN |
274 CONTROL_REGISTER_W1C_MASK, USB_EN);
275 udelay(1000); /* delay required for PHY Clk to appear */
276 if (!usb_phy_clk_valid(ehci))
278 out_le32(&(hcor)->or_portsc[0], PORT_PTS_ULPI);
281 out_be32(&ehci->prictrl, 0x0000000c);
282 out_be32(&ehci->age_cnt_limit, 0x00000040);
283 out_be32(&ehci->sictrl, 0x00000001);
285 in_le32(&ehci->usbmode);
287 if (has_erratum_a007798())
288 set_txfifothresh(ehci, TXFIFOTHRESH);
290 if (has_erratum_a004477()) {
292 * When reset is issued while any ULPI transaction is ongoing
293 * then it may result to corruption of ULPI Function Control
294 * Register which eventually causes phy clock to enter low
295 * power mode which stops the clock. Thus delay is required
296 * before reset to let ongoing ULPI transaction complete.
304 * Setting the value of TXFIFO_THRESH field in TXFILLTUNING register
305 * to counter DDR latencies in writing data into Tx buffer.
306 * This prevents Tx buffer from getting underrun
308 static void set_txfifothresh(struct usb_ehci *ehci, u32 txfifo_thresh)
311 cmd = ehci_readl(&ehci->txfilltuning);
312 cmd &= ~TXFIFO_THRESH_MASK;
313 cmd |= TXFIFO_THRESH(txfifo_thresh);
314 ehci_writel(&ehci->txfilltuning, cmd);