blob: ee3eb065b142143717c38ef50d8688d137d56810 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michael Trimarchie30a3362008-11-28 13:22:09 +01002/*
Rajesh Bhagat48c5c512016-07-01 18:51:46 +05303 * (C) Copyright 2009, 2011, 2016 Freescale Semiconductor, Inc.
Vivek Mahajan288f7fb2009-05-25 17:23:16 +05304 *
Michael Trimarchie30a3362008-11-28 13:22:09 +01005 * (C) Copyright 2008, Excito Elektronik i Sk=E5ne AB
6 *
7 * Author: Tor Krill tor@excito.com
Michael Trimarchie30a3362008-11-28 13:22:09 +01008 */
9
Simon Glass0af6e2d2019-08-01 09:46:52 -060010#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Michael Trimarchie30a3362008-11-28 13:22:09 +010012#include <pci.h>
13#include <usb.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Michael Trimarchie30a3362008-11-28 13:22:09 +010015#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
Mateusz Kulikowski3add69e2016-03-31 23:12:23 +020017#include <usb/ehci-ci.h>
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +053018#include <hwconfig.h>
Nikhil Badola76c2f2e2014-09-30 11:22:43 +053019#include <fsl_usb.h>
Nikhil Badolab6fd44c2014-10-20 16:50:49 +053020#include <fdt_support.h>
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053021#include <dm.h>
Michael Trimarchie30a3362008-11-28 13:22:09 +010022
Jean-Christophe PLAGNIOL-VILLARD8f6bcf42009-04-03 12:46:58 +020023#include "ehci.h"
Michael Trimarchie30a3362008-11-28 13:22:09 +010024
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053025DECLARE_GLOBAL_DATA_PTR;
26
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053027struct ehci_fsl_priv {
28 struct ehci_ctrl ehci;
29 fdt_addr_t hcd_base;
30 char *phy_type;
31};
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053032
Nikhil Badolab0b48da2014-04-07 08:46:14 +053033static void set_txfifothresh(struct usb_ehci *, u32);
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053034static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
35 struct ehci_hccr *hccr, struct ehci_hcor *hcor);
Nikhil Badolab0b48da2014-04-07 08:46:14 +053036
Shengzhou Liud407e1f2012-10-22 13:18:24 +080037/* Check USB PHY clock valid */
38static int usb_phy_clk_valid(struct usb_ehci *ehci)
39{
40 if (!((in_be32(&ehci->control) & PHY_CLK_VALID) ||
41 in_be32(&ehci->prictrl))) {
42 printf("USB PHY clock invalid!\n");
43 return 0;
44 } else {
45 return 1;
46 }
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053047}
48
Simon Glassaad29ae2020-12-03 16:55:21 -070049static int ehci_fsl_of_to_plat(struct udevice *dev)
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053050{
51 struct ehci_fsl_priv *priv = dev_get_priv(dev);
52 const void *prop;
53
Simon Glassdd79d6e2017-01-17 16:52:55 -070054 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy_type",
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053055 NULL);
56 if (prop) {
57 priv->phy_type = (char *)prop;
58 debug("phy_type %s\n", priv->phy_type);
59 }
60
61 return 0;
62}
63
64static int ehci_fsl_init_after_reset(struct ehci_ctrl *ctrl)
65{
66 struct usb_ehci *ehci = NULL;
67 struct ehci_fsl_priv *priv = container_of(ctrl, struct ehci_fsl_priv,
68 ehci);
Yinbo Zhu8c8fd942019-04-11 11:02:05 +000069#ifdef CONFIG_PPC
70 ehci = (struct usb_ehci *)lower_32_bits(priv->hcd_base);
71#else
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053072 ehci = (struct usb_ehci *)priv->hcd_base;
Yinbo Zhu8c8fd942019-04-11 11:02:05 +000073#endif
74
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053075 if (ehci_fsl_init(priv, ehci, priv->ehci.hccr, priv->ehci.hcor) < 0)
76 return -ENXIO;
77
78 return 0;
79}
80
81static const struct ehci_ops fsl_ehci_ops = {
82 .init_after_reset = ehci_fsl_init_after_reset,
83};
84
85static int ehci_fsl_probe(struct udevice *dev)
86{
87 struct ehci_fsl_priv *priv = dev_get_priv(dev);
88 struct usb_ehci *ehci = NULL;
89 struct ehci_hccr *hccr;
90 struct ehci_hcor *hcor;
Chris Packham434f0582018-10-04 20:03:53 +130091 struct ehci_ctrl *ehci_ctrl = &priv->ehci;
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053092
93 /*
94 * Get the base address for EHCI controller from the device node
95 */
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +090096 priv->hcd_base = dev_read_addr(dev);
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053097 if (priv->hcd_base == FDT_ADDR_T_NONE) {
98 debug("Can't get the EHCI register base address\n");
99 return -ENXIO;
100 }
Yinbo Zhu8c8fd942019-04-11 11:02:05 +0000101#ifdef CONFIG_PPC
102 ehci = (struct usb_ehci *)lower_32_bits(priv->hcd_base);
103#else
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530104 ehci = (struct usb_ehci *)priv->hcd_base;
Yinbo Zhu8c8fd942019-04-11 11:02:05 +0000105#endif
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530106 hccr = (struct ehci_hccr *)(&ehci->caplength);
107 hcor = (struct ehci_hcor *)
Ran Wang54443252017-12-20 10:34:19 +0800108 ((void *)hccr + HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530109
Chris Packham434f0582018-10-04 20:03:53 +1300110 ehci_ctrl->has_fsl_erratum_a005275 = has_erratum_a005275();
111
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530112 if (ehci_fsl_init(priv, ehci, hccr, hcor) < 0)
113 return -ENXIO;
114
Ran Wang54443252017-12-20 10:34:19 +0800115 debug("ehci-fsl: init hccr %p and hcor %p hc_length %d\n",
116 (void *)hccr, (void *)hcor,
117 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530118
119 return ehci_register(dev, hccr, hcor, &fsl_ehci_ops, 0, USB_INIT_HOST);
120}
121
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530122static const struct udevice_id ehci_usb_ids[] = {
123 { .compatible = "fsl-usb2-mph", },
124 { .compatible = "fsl-usb2-dr", },
125 { }
126};
127
128U_BOOT_DRIVER(ehci_fsl) = {
129 .name = "ehci_fsl",
130 .id = UCLASS_USB,
131 .of_match = ehci_usb_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700132 .of_to_plat = ehci_fsl_of_to_plat,
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530133 .probe = ehci_fsl_probe,
Masahiro Yamadad41919b2016-09-06 22:17:34 +0900134 .remove = ehci_deregister,
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530135 .ops = &ehci_usb_ops,
Simon Glassb75b15b2020-12-03 16:55:23 -0700136 .plat_auto = sizeof(struct usb_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700137 .priv_auto = sizeof(struct ehci_fsl_priv),
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530138 .flags = DM_FLAG_ALLOC_PRIV_DMA,
139};
Rajesh Bhagat2542d962016-07-01 18:51:45 +0530140
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530141static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
142 struct ehci_hccr *hccr, struct ehci_hcor *hcor)
Rajesh Bhagat2542d962016-07-01 18:51:45 +0530143{
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +0530144 const char *phy_type = NULL;
Kumar Gala7b83c352011-11-09 10:04:15 -0600145#ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
146 char usb_phy[5];
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +0530147
148 usb_phy[0] = '\0';
Kumar Gala7b83c352011-11-09 10:04:15 -0600149#endif
Nikhil Badola2613cfc2014-02-26 17:43:15 +0530150 if (has_erratum_a007075()) {
151 /*
152 * A 5ms delay is needed after applying soft-reset to the
153 * controller to let external ULPI phy come out of reset.
154 * This delay needs to be added before re-initializing
155 * the controller after soft-resetting completes
156 */
157 mdelay(5);
158 }
Michael Trimarchie30a3362008-11-28 13:22:09 +0100159
Michael Trimarchie30a3362008-11-28 13:22:09 +0100160 /* Set to Host mode */
Vivek Mahajan32c52202009-06-19 17:56:00 +0530161 setbits_le32(&ehci->usbmode, CM_HOST);
Michael Trimarchie30a3362008-11-28 13:22:09 +0100162
Vivek Mahajan32c52202009-06-19 17:56:00 +0530163 out_be32(&ehci->snoop1, SNOOP_SIZE_2GB);
164 out_be32(&ehci->snoop2, 0x80000000 | SNOOP_SIZE_2GB);
Michael Trimarchie30a3362008-11-28 13:22:09 +0100165
166 /* Init phy */
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530167 if (priv->phy_type)
168 phy_type = priv->phy_type;
Vivek Mahajan288f7fb2009-05-25 17:23:16 +0530169 else
Simon Glass64b723f2017-08-03 12:22:12 -0600170 phy_type = env_get("usb_phy_type");
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +0530171
172 if (!phy_type) {
173#ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
174 /* if none specified assume internal UTMI */
175 strcpy(usb_phy, "utmi");
176 phy_type = usb_phy;
177#else
178 printf("WARNING: USB phy type not defined !!\n");
179 return -1;
180#endif
181 }
182
Nikhil Badola09a3b562014-02-17 16:58:36 +0530183 if (!strncmp(phy_type, "utmi", 4)) {
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +0530184#if defined(CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY)
Nikhil Badola369f6632014-05-08 17:05:26 +0530185 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
186 PHY_CLK_SEL_UTMI);
187 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
188 UTMI_PHY_EN);
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +0530189 udelay(1000); /* delay required for PHY Clk to appear */
190#endif
Rajesh Bhagat2542d962016-07-01 18:51:45 +0530191 out_le32(&(hcor)->or_portsc[0], PORT_PTS_UTMI);
Nikhil Badola369f6632014-05-08 17:05:26 +0530192 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
193 USB_EN);
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +0530194 } else {
Nikhil Badola369f6632014-05-08 17:05:26 +0530195 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
196 PHY_CLK_SEL_ULPI);
197 clrsetbits_be32(&ehci->control, UTMI_PHY_EN |
198 CONTROL_REGISTER_W1C_MASK, USB_EN);
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +0530199 udelay(1000); /* delay required for PHY Clk to appear */
Shengzhou Liud407e1f2012-10-22 13:18:24 +0800200 if (!usb_phy_clk_valid(ehci))
201 return -EINVAL;
Rajesh Bhagat2542d962016-07-01 18:51:45 +0530202 out_le32(&(hcor)->or_portsc[0], PORT_PTS_ULPI);
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +0530203 }
Michael Trimarchie30a3362008-11-28 13:22:09 +0100204
Vivek Mahajan32c52202009-06-19 17:56:00 +0530205 out_be32(&ehci->prictrl, 0x0000000c);
206 out_be32(&ehci->age_cnt_limit, 0x00000040);
207 out_be32(&ehci->sictrl, 0x00000001);
Michael Trimarchie30a3362008-11-28 13:22:09 +0100208
Vivek Mahajan32c52202009-06-19 17:56:00 +0530209 in_le32(&ehci->usbmode);
Michael Trimarchie30a3362008-11-28 13:22:09 +0100210
Nikhil Badola67f4b262014-10-17 09:12:07 +0530211 if (has_erratum_a007798())
Nikhil Badolab0b48da2014-04-07 08:46:14 +0530212 set_txfifothresh(ehci, TXFIFOTHRESH);
213
Nikhil Badola288542c2014-11-21 17:25:21 +0530214 if (has_erratum_a004477()) {
215 /*
216 * When reset is issued while any ULPI transaction is ongoing
217 * then it may result to corruption of ULPI Function Control
218 * Register which eventually causes phy clock to enter low
219 * power mode which stops the clock. Thus delay is required
220 * before reset to let ongoing ULPI transaction complete.
221 */
222 udelay(1);
223 }
Michael Trimarchie30a3362008-11-28 13:22:09 +0100224 return 0;
225}
226
227/*
Nikhil Badolab0b48da2014-04-07 08:46:14 +0530228 * Setting the value of TXFIFO_THRESH field in TXFILLTUNING register
229 * to counter DDR latencies in writing data into Tx buffer.
230 * This prevents Tx buffer from getting underrun
231 */
232static void set_txfifothresh(struct usb_ehci *ehci, u32 txfifo_thresh)
233{
234 u32 cmd;
235 cmd = ehci_readl(&ehci->txfilltuning);
236 cmd &= ~TXFIFO_THRESH_MASK;
237 cmd |= TXFIFO_THRESH(txfifo_thresh);
238 ehci_writel(&ehci->txfilltuning, cmd);
239}