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
16 #include <linux/delay.h>
17 #include <usb/ehci-ci.h>
20 #include <fdt_support.h>
25 DECLARE_GLOBAL_DATA_PTR;
27 #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
28 #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
31 #if CONFIG_IS_ENABLED(DM_USB)
32 struct ehci_fsl_priv {
33 struct ehci_ctrl ehci;
39 static void set_txfifothresh(struct usb_ehci *, u32);
40 #if CONFIG_IS_ENABLED(DM_USB)
41 static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
42 struct ehci_hccr *hccr, struct ehci_hcor *hcor);
44 static int ehci_fsl_init(int index, struct usb_ehci *ehci,
45 struct ehci_hccr *hccr, struct ehci_hcor *hcor);
48 /* Check USB PHY clock valid */
49 static int usb_phy_clk_valid(struct usb_ehci *ehci)
51 if (!((in_be32(&ehci->control) & PHY_CLK_VALID) ||
52 in_be32(&ehci->prictrl))) {
53 printf("USB PHY clock invalid!\n");
60 #if CONFIG_IS_ENABLED(DM_USB)
61 static int ehci_fsl_ofdata_to_platdata(struct udevice *dev)
63 struct ehci_fsl_priv *priv = dev_get_priv(dev);
66 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy_type",
69 priv->phy_type = (char *)prop;
70 debug("phy_type %s\n", priv->phy_type);
76 static int ehci_fsl_init_after_reset(struct ehci_ctrl *ctrl)
78 struct usb_ehci *ehci = NULL;
79 struct ehci_fsl_priv *priv = container_of(ctrl, struct ehci_fsl_priv,
82 ehci = (struct usb_ehci *)lower_32_bits(priv->hcd_base);
84 ehci = (struct usb_ehci *)priv->hcd_base;
87 if (ehci_fsl_init(priv, ehci, priv->ehci.hccr, priv->ehci.hcor) < 0)
93 static const struct ehci_ops fsl_ehci_ops = {
94 .init_after_reset = ehci_fsl_init_after_reset,
97 static int ehci_fsl_probe(struct udevice *dev)
99 struct ehci_fsl_priv *priv = dev_get_priv(dev);
100 struct usb_ehci *ehci = NULL;
101 struct ehci_hccr *hccr;
102 struct ehci_hcor *hcor;
103 struct ehci_ctrl *ehci_ctrl = &priv->ehci;
106 * Get the base address for EHCI controller from the device node
108 priv->hcd_base = devfdt_get_addr(dev);
109 if (priv->hcd_base == FDT_ADDR_T_NONE) {
110 debug("Can't get the EHCI register base address\n");
114 ehci = (struct usb_ehci *)lower_32_bits(priv->hcd_base);
116 ehci = (struct usb_ehci *)priv->hcd_base;
118 hccr = (struct ehci_hccr *)(&ehci->caplength);
119 hcor = (struct ehci_hcor *)
120 ((void *)hccr + HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
122 ehci_ctrl->has_fsl_erratum_a005275 = has_erratum_a005275();
124 if (ehci_fsl_init(priv, ehci, hccr, hcor) < 0)
127 debug("ehci-fsl: init hccr %p and hcor %p hc_length %d\n",
128 (void *)hccr, (void *)hcor,
129 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
131 return ehci_register(dev, hccr, hcor, &fsl_ehci_ops, 0, USB_INIT_HOST);
134 static const struct udevice_id ehci_usb_ids[] = {
135 { .compatible = "fsl-usb2-mph", },
136 { .compatible = "fsl-usb2-dr", },
140 U_BOOT_DRIVER(ehci_fsl) = {
143 .of_match = ehci_usb_ids,
144 .ofdata_to_platdata = ehci_fsl_ofdata_to_platdata,
145 .probe = ehci_fsl_probe,
146 .remove = ehci_deregister,
147 .ops = &ehci_usb_ops,
148 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
149 .priv_auto_alloc_size = sizeof(struct ehci_fsl_priv),
150 .flags = DM_FLAG_ALLOC_PRIV_DMA,
154 * Create the appropriate control structures to manage
155 * a new EHCI host controller.
157 * Excerpts from linux ehci fsl driver.
159 int ehci_hcd_init(int index, enum usb_init_type init,
160 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
162 struct ehci_ctrl *ehci_ctrl = container_of(hccr,
163 struct ehci_ctrl, hccr);
164 struct usb_ehci *ehci = NULL;
168 ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB1_ADDR;
171 ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB2_ADDR;
174 printf("ERROR: wrong controller index!!\n");
178 *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
179 *hcor = (struct ehci_hcor *)((uint32_t) *hccr +
180 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
182 ehci_ctrl->has_fsl_erratum_a005275 = has_erratum_a005275();
184 return ehci_fsl_init(index, ehci, *hccr, *hcor);
188 * Destroy the appropriate control structures corresponding
189 * the the EHCI host controller.
191 int ehci_hcd_stop(int index)
197 #if CONFIG_IS_ENABLED(DM_USB)
198 static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
199 struct ehci_hccr *hccr, struct ehci_hcor *hcor)
201 static int ehci_fsl_init(int index, struct usb_ehci *ehci,
202 struct ehci_hccr *hccr, struct ehci_hcor *hcor)
205 const char *phy_type = NULL;
206 #if !CONFIG_IS_ENABLED(DM_USB)
208 char current_usb_controller[5];
210 #ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
215 if (has_erratum_a007075()) {
217 * A 5ms delay is needed after applying soft-reset to the
218 * controller to let external ULPI phy come out of reset.
219 * This delay needs to be added before re-initializing
220 * the controller after soft-resetting completes
225 /* Set to Host mode */
226 setbits_le32(&ehci->usbmode, CM_HOST);
228 out_be32(&ehci->snoop1, SNOOP_SIZE_2GB);
229 out_be32(&ehci->snoop2, 0x80000000 | SNOOP_SIZE_2GB);
232 #if CONFIG_IS_ENABLED(DM_USB)
234 phy_type = priv->phy_type;
236 memset(current_usb_controller, '\0', 5);
237 snprintf(current_usb_controller, sizeof(current_usb_controller),
240 if (hwconfig_sub(current_usb_controller, "phy_type"))
241 phy_type = hwconfig_subarg(current_usb_controller,
245 phy_type = env_get("usb_phy_type");
248 #ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
249 /* if none specified assume internal UTMI */
250 strcpy(usb_phy, "utmi");
253 printf("WARNING: USB phy type not defined !!\n");
258 if (!strncmp(phy_type, "utmi", 4)) {
259 #if defined(CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY)
260 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
262 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
264 udelay(1000); /* delay required for PHY Clk to appear */
266 out_le32(&(hcor)->or_portsc[0], PORT_PTS_UTMI);
267 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
270 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
272 clrsetbits_be32(&ehci->control, UTMI_PHY_EN |
273 CONTROL_REGISTER_W1C_MASK, USB_EN);
274 udelay(1000); /* delay required for PHY Clk to appear */
275 if (!usb_phy_clk_valid(ehci))
277 out_le32(&(hcor)->or_portsc[0], PORT_PTS_ULPI);
280 out_be32(&ehci->prictrl, 0x0000000c);
281 out_be32(&ehci->age_cnt_limit, 0x00000040);
282 out_be32(&ehci->sictrl, 0x00000001);
284 in_le32(&ehci->usbmode);
286 if (has_erratum_a007798())
287 set_txfifothresh(ehci, TXFIFOTHRESH);
289 if (has_erratum_a004477()) {
291 * When reset is issued while any ULPI transaction is ongoing
292 * then it may result to corruption of ULPI Function Control
293 * Register which eventually causes phy clock to enter low
294 * power mode which stops the clock. Thus delay is required
295 * before reset to let ongoing ULPI transaction complete.
303 * Setting the value of TXFIFO_THRESH field in TXFILLTUNING register
304 * to counter DDR latencies in writing data into Tx buffer.
305 * This prevents Tx buffer from getting underrun
307 static void set_txfifothresh(struct usb_ehci *ehci, u32 txfifo_thresh)
310 cmd = ehci_readl(&ehci->txfilltuning);
311 cmd &= ~TXFIFO_THRESH_MASK;
312 cmd |= TXFIFO_THRESH(txfifo_thresh);
313 ehci_writel(&ehci->txfilltuning, cmd);