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
7 * Author: Tor Krill tor@excito.com
14 #include <usb/ehci-ci.h>
17 #include <fdt_support.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
25 #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
28 #if CONFIG_IS_ENABLED(DM_USB)
29 struct ehci_fsl_priv {
30 struct ehci_ctrl ehci;
36 static void set_txfifothresh(struct usb_ehci *, u32);
37 #if CONFIG_IS_ENABLED(DM_USB)
38 static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
39 struct ehci_hccr *hccr, struct ehci_hcor *hcor);
41 static int ehci_fsl_init(int index, struct usb_ehci *ehci,
42 struct ehci_hccr *hccr, struct ehci_hcor *hcor);
45 /* Check USB PHY clock valid */
46 static int usb_phy_clk_valid(struct usb_ehci *ehci)
48 if (!((in_be32(&ehci->control) & PHY_CLK_VALID) ||
49 in_be32(&ehci->prictrl))) {
50 printf("USB PHY clock invalid!\n");
57 #if CONFIG_IS_ENABLED(DM_USB)
58 static int ehci_fsl_ofdata_to_platdata(struct udevice *dev)
60 struct ehci_fsl_priv *priv = dev_get_priv(dev);
63 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy_type",
66 priv->phy_type = (char *)prop;
67 debug("phy_type %s\n", priv->phy_type);
73 static int ehci_fsl_init_after_reset(struct ehci_ctrl *ctrl)
75 struct usb_ehci *ehci = NULL;
76 struct ehci_fsl_priv *priv = container_of(ctrl, struct ehci_fsl_priv,
79 ehci = (struct usb_ehci *)priv->hcd_base;
80 if (ehci_fsl_init(priv, ehci, priv->ehci.hccr, priv->ehci.hcor) < 0)
86 static const struct ehci_ops fsl_ehci_ops = {
87 .init_after_reset = ehci_fsl_init_after_reset,
90 static int ehci_fsl_probe(struct udevice *dev)
92 struct ehci_fsl_priv *priv = dev_get_priv(dev);
93 struct usb_ehci *ehci = NULL;
94 struct ehci_hccr *hccr;
95 struct ehci_hcor *hcor;
96 struct ehci_ctrl *ehci_ctrl = &priv->ehci;
99 * Get the base address for EHCI controller from the device node
101 priv->hcd_base = devfdt_get_addr(dev);
102 if (priv->hcd_base == FDT_ADDR_T_NONE) {
103 debug("Can't get the EHCI register base address\n");
106 ehci = (struct usb_ehci *)priv->hcd_base;
107 hccr = (struct ehci_hccr *)(&ehci->caplength);
108 hcor = (struct ehci_hcor *)
109 ((void *)hccr + HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
111 ehci_ctrl->has_fsl_erratum_a005275 = has_erratum_a005275();
113 if (ehci_fsl_init(priv, ehci, hccr, hcor) < 0)
116 debug("ehci-fsl: init hccr %p and hcor %p hc_length %d\n",
117 (void *)hccr, (void *)hcor,
118 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
120 return ehci_register(dev, hccr, hcor, &fsl_ehci_ops, 0, USB_INIT_HOST);
123 static const struct udevice_id ehci_usb_ids[] = {
124 { .compatible = "fsl-usb2-mph", },
125 { .compatible = "fsl-usb2-dr", },
129 U_BOOT_DRIVER(ehci_fsl) = {
132 .of_match = ehci_usb_ids,
133 .ofdata_to_platdata = ehci_fsl_ofdata_to_platdata,
134 .probe = ehci_fsl_probe,
135 .remove = ehci_deregister,
136 .ops = &ehci_usb_ops,
137 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
138 .priv_auto_alloc_size = sizeof(struct ehci_fsl_priv),
139 .flags = DM_FLAG_ALLOC_PRIV_DMA,
143 * Create the appropriate control structures to manage
144 * a new EHCI host controller.
146 * Excerpts from linux ehci fsl driver.
148 int ehci_hcd_init(int index, enum usb_init_type init,
149 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
151 struct ehci_ctrl *ehci_ctrl = container_of(hccr,
152 struct ehci_ctrl, hccr);
153 struct usb_ehci *ehci = NULL;
157 ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB1_ADDR;
160 ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB2_ADDR;
163 printf("ERROR: wrong controller index!!\n");
167 *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
168 *hcor = (struct ehci_hcor *)((uint32_t) *hccr +
169 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
171 ehci_ctrl->has_fsl_erratum_a005275 = has_erratum_a005275();
173 return ehci_fsl_init(index, ehci, *hccr, *hcor);
177 * Destroy the appropriate control structures corresponding
178 * the the EHCI host controller.
180 int ehci_hcd_stop(int index)
186 #if CONFIG_IS_ENABLED(DM_USB)
187 static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
188 struct ehci_hccr *hccr, struct ehci_hcor *hcor)
190 static int ehci_fsl_init(int index, struct usb_ehci *ehci,
191 struct ehci_hccr *hccr, struct ehci_hcor *hcor)
194 const char *phy_type = NULL;
195 #if !CONFIG_IS_ENABLED(DM_USB)
197 char current_usb_controller[5];
199 #ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
204 if (has_erratum_a007075()) {
206 * A 5ms delay is needed after applying soft-reset to the
207 * controller to let external ULPI phy come out of reset.
208 * This delay needs to be added before re-initializing
209 * the controller after soft-resetting completes
214 /* Set to Host mode */
215 setbits_le32(&ehci->usbmode, CM_HOST);
217 out_be32(&ehci->snoop1, SNOOP_SIZE_2GB);
218 out_be32(&ehci->snoop2, 0x80000000 | SNOOP_SIZE_2GB);
221 #if CONFIG_IS_ENABLED(DM_USB)
223 phy_type = priv->phy_type;
225 memset(current_usb_controller, '\0', 5);
226 snprintf(current_usb_controller, sizeof(current_usb_controller),
229 if (hwconfig_sub(current_usb_controller, "phy_type"))
230 phy_type = hwconfig_subarg(current_usb_controller,
234 phy_type = env_get("usb_phy_type");
237 #ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
238 /* if none specified assume internal UTMI */
239 strcpy(usb_phy, "utmi");
242 printf("WARNING: USB phy type not defined !!\n");
247 if (!strncmp(phy_type, "utmi", 4)) {
248 #if defined(CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY)
249 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
251 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
253 udelay(1000); /* delay required for PHY Clk to appear */
255 out_le32(&(hcor)->or_portsc[0], PORT_PTS_UTMI);
256 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
259 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
261 clrsetbits_be32(&ehci->control, UTMI_PHY_EN |
262 CONTROL_REGISTER_W1C_MASK, USB_EN);
263 udelay(1000); /* delay required for PHY Clk to appear */
264 if (!usb_phy_clk_valid(ehci))
266 out_le32(&(hcor)->or_portsc[0], PORT_PTS_ULPI);
269 out_be32(&ehci->prictrl, 0x0000000c);
270 out_be32(&ehci->age_cnt_limit, 0x00000040);
271 out_be32(&ehci->sictrl, 0x00000001);
273 in_le32(&ehci->usbmode);
275 if (has_erratum_a007798())
276 set_txfifothresh(ehci, TXFIFOTHRESH);
278 if (has_erratum_a004477()) {
280 * When reset is issued while any ULPI transaction is ongoing
281 * then it may result to corruption of ULPI Function Control
282 * Register which eventually causes phy clock to enter low
283 * power mode which stops the clock. Thus delay is required
284 * before reset to let ongoing ULPI transaction complete.
292 * Setting the value of TXFIFO_THRESH field in TXFILLTUNING register
293 * to counter DDR latencies in writing data into Tx buffer.
294 * This prevents Tx buffer from getting underrun
296 static void set_txfifothresh(struct usb_ehci *ehci, u32 txfifo_thresh)
299 cmd = ehci_readl(&ehci->txfilltuning);
300 cmd &= ~TXFIFO_THRESH_MASK;
301 cmd |= TXFIFO_THRESH(txfifo_thresh);
302 ehci_writel(&ehci->txfilltuning, cmd);