blob: 0569dd54fff937af7ac0f8fc32ed3609e7e73118 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michael Trimarchi6b924872008-11-28 13:22:09 +01002/*
Rajesh Bhagatba699a52016-07-01 18:51:46 +05303 * (C) Copyright 2009, 2011, 2016 Freescale Semiconductor, Inc.
Vivek Mahajan4ef01012009-05-25 17:23:16 +05304 *
Michael Trimarchi6b924872008-11-28 13:22:09 +01005 * (C) Copyright 2008, Excito Elektronik i Sk=E5ne AB
6 *
7 * Author: Tor Krill tor@excito.com
Michael Trimarchi6b924872008-11-28 13:22:09 +01008 */
9
10#include <common.h>
Simon Glass7b51b572019-08-01 09:46:52 -060011#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Michael Trimarchi6b924872008-11-28 13:22:09 +010013#include <pci.h>
14#include <usb.h>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <asm/global_data.h>
Michael Trimarchi6b924872008-11-28 13:22:09 +010016#include <asm/io.h>
Simon Glassc05ed002020-05-10 11:40:11 -060017#include <linux/delay.h>
Mateusz Kulikowskie162c6b2016-03-31 23:12:23 +020018#include <usb/ehci-ci.h>
Ramneek Mehresh1b719e62011-03-23 15:20:43 +053019#include <hwconfig.h>
Nikhil Badolac26c80a2014-09-30 11:22:43 +053020#include <fsl_usb.h>
Nikhil Badolaa1c04e22014-10-20 16:50:49 +053021#include <fdt_support.h>
Rajesh Bhagatba699a52016-07-01 18:51:46 +053022#include <dm.h>
Michael Trimarchi6b924872008-11-28 13:22:09 +010023
Jean-Christophe PLAGNIOL-VILLARD2731b9a2009-04-03 12:46:58 +020024#include "ehci.h"
Michael Trimarchi6b924872008-11-28 13:22:09 +010025
Rajesh Bhagatba699a52016-07-01 18:51:46 +053026DECLARE_GLOBAL_DATA_PTR;
27
Rajesh Bhagatba699a52016-07-01 18:51:46 +053028struct ehci_fsl_priv {
29 struct ehci_ctrl ehci;
30 fdt_addr_t hcd_base;
31 char *phy_type;
32};
Rajesh Bhagatba699a52016-07-01 18:51:46 +053033
Nikhil Badola896720c2014-04-07 08:46:14 +053034static void set_txfifothresh(struct usb_ehci *, u32);
Rajesh Bhagatba699a52016-07-01 18:51:46 +053035static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
36 struct ehci_hccr *hccr, struct ehci_hcor *hcor);
Nikhil Badola896720c2014-04-07 08:46:14 +053037
Shengzhou Liu047cea32012-10-22 13:18:24 +080038/* Check USB PHY clock valid */
39static int usb_phy_clk_valid(struct usb_ehci *ehci)
40{
41 if (!((in_be32(&ehci->control) & PHY_CLK_VALID) ||
42 in_be32(&ehci->prictrl))) {
43 printf("USB PHY clock invalid!\n");
44 return 0;
45 } else {
46 return 1;
47 }
48}
49
Simon Glassd1998a92020-12-03 16:55:21 -070050static int ehci_fsl_of_to_plat(struct udevice *dev)
Rajesh Bhagatba699a52016-07-01 18:51:46 +053051{
52 struct ehci_fsl_priv *priv = dev_get_priv(dev);
53 const void *prop;
54
Simon Glasse160f7d2017-01-17 16:52:55 -070055 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy_type",
Rajesh Bhagatba699a52016-07-01 18:51:46 +053056 NULL);
57 if (prop) {
58 priv->phy_type = (char *)prop;
59 debug("phy_type %s\n", priv->phy_type);
60 }
61
62 return 0;
63}
64
65static int ehci_fsl_init_after_reset(struct ehci_ctrl *ctrl)
66{
67 struct usb_ehci *ehci = NULL;
68 struct ehci_fsl_priv *priv = container_of(ctrl, struct ehci_fsl_priv,
69 ehci);
Yinbo Zhuad9f2be2019-04-11 11:02:05 +000070#ifdef CONFIG_PPC
71 ehci = (struct usb_ehci *)lower_32_bits(priv->hcd_base);
72#else
Rajesh Bhagatba699a52016-07-01 18:51:46 +053073 ehci = (struct usb_ehci *)priv->hcd_base;
Yinbo Zhuad9f2be2019-04-11 11:02:05 +000074#endif
75
Rajesh Bhagatba699a52016-07-01 18:51:46 +053076 if (ehci_fsl_init(priv, ehci, priv->ehci.hccr, priv->ehci.hcor) < 0)
77 return -ENXIO;
78
79 return 0;
80}
81
82static const struct ehci_ops fsl_ehci_ops = {
83 .init_after_reset = ehci_fsl_init_after_reset,
84};
85
86static int ehci_fsl_probe(struct udevice *dev)
87{
88 struct ehci_fsl_priv *priv = dev_get_priv(dev);
89 struct usb_ehci *ehci = NULL;
90 struct ehci_hccr *hccr;
91 struct ehci_hcor *hcor;
Chris Packham4eaf7f52018-10-04 20:03:53 +130092 struct ehci_ctrl *ehci_ctrl = &priv->ehci;
Rajesh Bhagatba699a52016-07-01 18:51:46 +053093
94 /*
95 * Get the base address for EHCI controller from the device node
96 */
Masahiro Yamada25484932020-07-17 14:36:48 +090097 priv->hcd_base = dev_read_addr(dev);
Rajesh Bhagatba699a52016-07-01 18:51:46 +053098 if (priv->hcd_base == FDT_ADDR_T_NONE) {
99 debug("Can't get the EHCI register base address\n");
100 return -ENXIO;
101 }
Yinbo Zhuad9f2be2019-04-11 11:02:05 +0000102#ifdef CONFIG_PPC
103 ehci = (struct usb_ehci *)lower_32_bits(priv->hcd_base);
104#else
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530105 ehci = (struct usb_ehci *)priv->hcd_base;
Yinbo Zhuad9f2be2019-04-11 11:02:05 +0000106#endif
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530107 hccr = (struct ehci_hccr *)(&ehci->caplength);
108 hcor = (struct ehci_hcor *)
Ran Wangbe3872e2017-12-20 10:34:19 +0800109 ((void *)hccr + HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530110
Chris Packham4eaf7f52018-10-04 20:03:53 +1300111 ehci_ctrl->has_fsl_erratum_a005275 = has_erratum_a005275();
112
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530113 if (ehci_fsl_init(priv, ehci, hccr, hcor) < 0)
114 return -ENXIO;
115
Ran Wangbe3872e2017-12-20 10:34:19 +0800116 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)));
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530119
120 return ehci_register(dev, hccr, hcor, &fsl_ehci_ops, 0, USB_INIT_HOST);
121}
122
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530123static const struct udevice_id ehci_usb_ids[] = {
124 { .compatible = "fsl-usb2-mph", },
125 { .compatible = "fsl-usb2-dr", },
126 { }
127};
128
129U_BOOT_DRIVER(ehci_fsl) = {
130 .name = "ehci_fsl",
131 .id = UCLASS_USB,
132 .of_match = ehci_usb_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700133 .of_to_plat = ehci_fsl_of_to_plat,
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530134 .probe = ehci_fsl_probe,
Masahiro Yamada40527342016-09-06 22:17:34 +0900135 .remove = ehci_deregister,
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530136 .ops = &ehci_usb_ops,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700137 .plat_auto = sizeof(struct usb_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700138 .priv_auto = sizeof(struct ehci_fsl_priv),
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530139 .flags = DM_FLAG_ALLOC_PRIV_DMA,
140};
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530141
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530142static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
143 struct ehci_hccr *hccr, struct ehci_hcor *hcor)
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530144{
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530145 const char *phy_type = NULL;
Kumar Galadd22f7c2011-11-09 10:04:15 -0600146#ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
147 char usb_phy[5];
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530148
149 usb_phy[0] = '\0';
Kumar Galadd22f7c2011-11-09 10:04:15 -0600150#endif
Nikhil Badola11856912014-02-26 17:43:15 +0530151 if (has_erratum_a007075()) {
152 /*
153 * A 5ms delay is needed after applying soft-reset to the
154 * controller to let external ULPI phy come out of reset.
155 * This delay needs to be added before re-initializing
156 * the controller after soft-resetting completes
157 */
158 mdelay(5);
159 }
Michael Trimarchi6b924872008-11-28 13:22:09 +0100160
Michael Trimarchi6b924872008-11-28 13:22:09 +0100161 /* Set to Host mode */
Vivek Mahajan08066152009-06-19 17:56:00 +0530162 setbits_le32(&ehci->usbmode, CM_HOST);
Michael Trimarchi6b924872008-11-28 13:22:09 +0100163
Vivek Mahajan08066152009-06-19 17:56:00 +0530164 out_be32(&ehci->snoop1, SNOOP_SIZE_2GB);
165 out_be32(&ehci->snoop2, 0x80000000 | SNOOP_SIZE_2GB);
Michael Trimarchi6b924872008-11-28 13:22:09 +0100166
167 /* Init phy */
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530168 if (priv->phy_type)
169 phy_type = priv->phy_type;
Vivek Mahajan4ef01012009-05-25 17:23:16 +0530170 else
Simon Glass00caae62017-08-03 12:22:12 -0600171 phy_type = env_get("usb_phy_type");
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530172
173 if (!phy_type) {
174#ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
175 /* if none specified assume internal UTMI */
176 strcpy(usb_phy, "utmi");
177 phy_type = usb_phy;
178#else
179 printf("WARNING: USB phy type not defined !!\n");
180 return -1;
181#endif
182 }
183
Nikhil Badola91d77462014-02-17 16:58:36 +0530184 if (!strncmp(phy_type, "utmi", 4)) {
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530185#if defined(CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY)
Nikhil Badola15231f62014-05-08 17:05:26 +0530186 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
187 PHY_CLK_SEL_UTMI);
188 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
189 UTMI_PHY_EN);
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530190 udelay(1000); /* delay required for PHY Clk to appear */
191#endif
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530192 out_le32(&(hcor)->or_portsc[0], PORT_PTS_UTMI);
Nikhil Badola15231f62014-05-08 17:05:26 +0530193 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
194 USB_EN);
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530195 } else {
Nikhil Badola15231f62014-05-08 17:05:26 +0530196 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
197 PHY_CLK_SEL_ULPI);
198 clrsetbits_be32(&ehci->control, UTMI_PHY_EN |
199 CONTROL_REGISTER_W1C_MASK, USB_EN);
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530200 udelay(1000); /* delay required for PHY Clk to appear */
Shengzhou Liu047cea32012-10-22 13:18:24 +0800201 if (!usb_phy_clk_valid(ehci))
202 return -EINVAL;
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530203 out_le32(&(hcor)->or_portsc[0], PORT_PTS_ULPI);
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530204 }
Michael Trimarchi6b924872008-11-28 13:22:09 +0100205
Vivek Mahajan08066152009-06-19 17:56:00 +0530206 out_be32(&ehci->prictrl, 0x0000000c);
207 out_be32(&ehci->age_cnt_limit, 0x00000040);
208 out_be32(&ehci->sictrl, 0x00000001);
Michael Trimarchi6b924872008-11-28 13:22:09 +0100209
Vivek Mahajan08066152009-06-19 17:56:00 +0530210 in_le32(&ehci->usbmode);
Michael Trimarchi6b924872008-11-28 13:22:09 +0100211
Nikhil Badolaf3dff692014-10-17 09:12:07 +0530212 if (has_erratum_a007798())
Nikhil Badola896720c2014-04-07 08:46:14 +0530213 set_txfifothresh(ehci, TXFIFOTHRESH);
214
Nikhil Badola0dc78ff2014-11-21 17:25:21 +0530215 if (has_erratum_a004477()) {
216 /*
217 * When reset is issued while any ULPI transaction is ongoing
218 * then it may result to corruption of ULPI Function Control
219 * Register which eventually causes phy clock to enter low
220 * power mode which stops the clock. Thus delay is required
221 * before reset to let ongoing ULPI transaction complete.
222 */
223 udelay(1);
224 }
Michael Trimarchi6b924872008-11-28 13:22:09 +0100225 return 0;
226}
227
228/*
Nikhil Badola896720c2014-04-07 08:46:14 +0530229 * Setting the value of TXFIFO_THRESH field in TXFILLTUNING register
230 * to counter DDR latencies in writing data into Tx buffer.
231 * This prevents Tx buffer from getting underrun
232 */
233static void set_txfifothresh(struct usb_ehci *ehci, u32 txfifo_thresh)
234{
235 u32 cmd;
236 cmd = ehci_readl(&ehci->txfilltuning);
237 cmd &= ~TXFIFO_THRESH_MASK;
238 cmd |= TXFIFO_THRESH(txfifo_thresh);
239 ehci_writel(&ehci->txfilltuning, cmd);
240}