blob: 75d24ae0155cfdadd5b126866d6f334758aaf542 [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
10#include <common.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060011#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Michael Trimarchie30a3362008-11-28 13:22:09 +010013#include <pci.h>
14#include <usb.h>
Michael Trimarchie30a3362008-11-28 13:22:09 +010015#include <asm/io.h>
Mateusz Kulikowski3add69e2016-03-31 23:12:23 +020016#include <usb/ehci-ci.h>
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +053017#include <hwconfig.h>
Nikhil Badola76c2f2e2014-09-30 11:22:43 +053018#include <fsl_usb.h>
Nikhil Badolab6fd44c2014-10-20 16:50:49 +053019#include <fdt_support.h>
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053020#include <dm.h>
Michael Trimarchie30a3362008-11-28 13:22:09 +010021
Jean-Christophe PLAGNIOL-VILLARD8f6bcf42009-04-03 12:46:58 +020022#include "ehci.h"
Michael Trimarchie30a3362008-11-28 13:22:09 +010023
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053024DECLARE_GLOBAL_DATA_PTR;
25
Nikhil Badolab6fd44c2014-10-20 16:50:49 +053026#ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
27#define CONFIG_USB_MAX_CONTROLLER_COUNT 1
28#endif
29
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +010030#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053031struct ehci_fsl_priv {
32 struct ehci_ctrl ehci;
33 fdt_addr_t hcd_base;
34 char *phy_type;
35};
36#endif
37
Nikhil Badolab0b48da2014-04-07 08:46:14 +053038static void set_txfifothresh(struct usb_ehci *, u32);
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +010039#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053040static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
41 struct ehci_hccr *hccr, struct ehci_hcor *hcor);
42#else
Rajesh Bhagat2542d962016-07-01 18:51:45 +053043static int ehci_fsl_init(int index, struct usb_ehci *ehci,
44 struct ehci_hccr *hccr, struct ehci_hcor *hcor);
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053045#endif
Nikhil Badolab0b48da2014-04-07 08:46:14 +053046
Shengzhou Liud407e1f2012-10-22 13:18:24 +080047/* Check USB PHY clock valid */
48static int usb_phy_clk_valid(struct usb_ehci *ehci)
49{
50 if (!((in_be32(&ehci->control) & PHY_CLK_VALID) ||
51 in_be32(&ehci->prictrl))) {
52 printf("USB PHY clock invalid!\n");
53 return 0;
54 } else {
55 return 1;
56 }
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053057}
58
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +010059#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053060static int ehci_fsl_ofdata_to_platdata(struct udevice *dev)
61{
62 struct ehci_fsl_priv *priv = dev_get_priv(dev);
63 const void *prop;
64
Simon Glassdd79d6e2017-01-17 16:52:55 -070065 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy_type",
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053066 NULL);
67 if (prop) {
68 priv->phy_type = (char *)prop;
69 debug("phy_type %s\n", priv->phy_type);
70 }
71
72 return 0;
73}
74
75static int ehci_fsl_init_after_reset(struct ehci_ctrl *ctrl)
76{
77 struct usb_ehci *ehci = NULL;
78 struct ehci_fsl_priv *priv = container_of(ctrl, struct ehci_fsl_priv,
79 ehci);
Yinbo Zhu8c8fd942019-04-11 11:02:05 +000080#ifdef CONFIG_PPC
81 ehci = (struct usb_ehci *)lower_32_bits(priv->hcd_base);
82#else
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053083 ehci = (struct usb_ehci *)priv->hcd_base;
Yinbo Zhu8c8fd942019-04-11 11:02:05 +000084#endif
85
Rajesh Bhagat48c5c512016-07-01 18:51:46 +053086 if (ehci_fsl_init(priv, ehci, priv->ehci.hccr, priv->ehci.hcor) < 0)
87 return -ENXIO;
88
89 return 0;
90}
91
92static const struct ehci_ops fsl_ehci_ops = {
93 .init_after_reset = ehci_fsl_init_after_reset,
94};
95
96static int ehci_fsl_probe(struct udevice *dev)
97{
98 struct ehci_fsl_priv *priv = dev_get_priv(dev);
99 struct usb_ehci *ehci = NULL;
100 struct ehci_hccr *hccr;
101 struct ehci_hcor *hcor;
Chris Packham434f0582018-10-04 20:03:53 +1300102 struct ehci_ctrl *ehci_ctrl = &priv->ehci;
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530103
104 /*
105 * Get the base address for EHCI controller from the device node
106 */
Simon Glassba1dea42017-05-17 17:18:05 -0600107 priv->hcd_base = devfdt_get_addr(dev);
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530108 if (priv->hcd_base == FDT_ADDR_T_NONE) {
109 debug("Can't get the EHCI register base address\n");
110 return -ENXIO;
111 }
Yinbo Zhu8c8fd942019-04-11 11:02:05 +0000112#ifdef CONFIG_PPC
113 ehci = (struct usb_ehci *)lower_32_bits(priv->hcd_base);
114#else
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530115 ehci = (struct usb_ehci *)priv->hcd_base;
Yinbo Zhu8c8fd942019-04-11 11:02:05 +0000116#endif
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530117 hccr = (struct ehci_hccr *)(&ehci->caplength);
118 hcor = (struct ehci_hcor *)
Ran Wang54443252017-12-20 10:34:19 +0800119 ((void *)hccr + HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530120
Chris Packham434f0582018-10-04 20:03:53 +1300121 ehci_ctrl->has_fsl_erratum_a005275 = has_erratum_a005275();
122
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530123 if (ehci_fsl_init(priv, ehci, hccr, hcor) < 0)
124 return -ENXIO;
125
Ran Wang54443252017-12-20 10:34:19 +0800126 debug("ehci-fsl: init hccr %p and hcor %p hc_length %d\n",
127 (void *)hccr, (void *)hcor,
128 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530129
130 return ehci_register(dev, hccr, hcor, &fsl_ehci_ops, 0, USB_INIT_HOST);
131}
132
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530133static const struct udevice_id ehci_usb_ids[] = {
134 { .compatible = "fsl-usb2-mph", },
135 { .compatible = "fsl-usb2-dr", },
136 { }
137};
138
139U_BOOT_DRIVER(ehci_fsl) = {
140 .name = "ehci_fsl",
141 .id = UCLASS_USB,
142 .of_match = ehci_usb_ids,
143 .ofdata_to_platdata = ehci_fsl_ofdata_to_platdata,
144 .probe = ehci_fsl_probe,
Masahiro Yamadad41919b2016-09-06 22:17:34 +0900145 .remove = ehci_deregister,
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530146 .ops = &ehci_usb_ops,
147 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
148 .priv_auto_alloc_size = sizeof(struct ehci_fsl_priv),
149 .flags = DM_FLAG_ALLOC_PRIV_DMA,
150};
151#else
Michael Trimarchie30a3362008-11-28 13:22:09 +0100152/*
153 * Create the appropriate control structures to manage
154 * a new EHCI host controller.
155 *
156 * Excerpts from linux ehci fsl driver.
157 */
Troy Kisky7d6bbb92013-10-10 15:27:57 -0700158int ehci_hcd_init(int index, enum usb_init_type init,
159 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
Michael Trimarchie30a3362008-11-28 13:22:09 +0100160{
Chris Packham434f0582018-10-04 20:03:53 +1300161 struct ehci_ctrl *ehci_ctrl = container_of(hccr,
162 struct ehci_ctrl, hccr);
ramneek mehresh16b08062013-09-12 16:35:49 +0530163 struct usb_ehci *ehci = NULL;
Rajesh Bhagat2542d962016-07-01 18:51:45 +0530164
165 switch (index) {
166 case 0:
167 ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB1_ADDR;
168 break;
169 case 1:
170 ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB2_ADDR;
171 break;
172 default:
173 printf("ERROR: wrong controller index!!\n");
174 return -EINVAL;
175 };
176
177 *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
178 *hcor = (struct ehci_hcor *)((uint32_t) *hccr +
179 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
180
Chris Packham434f0582018-10-04 20:03:53 +1300181 ehci_ctrl->has_fsl_erratum_a005275 = has_erratum_a005275();
182
Rajesh Bhagat2542d962016-07-01 18:51:45 +0530183 return ehci_fsl_init(index, ehci, *hccr, *hcor);
184}
185
186/*
187 * Destroy the appropriate control structures corresponding
188 * the the EHCI host controller.
189 */
190int ehci_hcd_stop(int index)
191{
192 return 0;
193}
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530194#endif
Rajesh Bhagat2542d962016-07-01 18:51:45 +0530195
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100196#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530197static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
198 struct ehci_hccr *hccr, struct ehci_hcor *hcor)
199#else
Rajesh Bhagat2542d962016-07-01 18:51:45 +0530200static int ehci_fsl_init(int index, struct usb_ehci *ehci,
201 struct ehci_hccr *hccr, struct ehci_hcor *hcor)
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530202#endif
Rajesh Bhagat2542d962016-07-01 18:51:45 +0530203{
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +0530204 const char *phy_type = NULL;
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100205#if !CONFIG_IS_ENABLED(DM_USB)
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +0530206 size_t len;
Nikhil Badolaeb97e252013-12-19 11:08:46 +0530207 char current_usb_controller[5];
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530208#endif
Kumar Gala7b83c352011-11-09 10:04:15 -0600209#ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
210 char usb_phy[5];
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +0530211
212 usb_phy[0] = '\0';
Kumar Gala7b83c352011-11-09 10:04:15 -0600213#endif
Nikhil Badola2613cfc2014-02-26 17:43:15 +0530214 if (has_erratum_a007075()) {
215 /*
216 * A 5ms delay is needed after applying soft-reset to the
217 * controller to let external ULPI phy come out of reset.
218 * This delay needs to be added before re-initializing
219 * the controller after soft-resetting completes
220 */
221 mdelay(5);
222 }
Michael Trimarchie30a3362008-11-28 13:22:09 +0100223
Michael Trimarchie30a3362008-11-28 13:22:09 +0100224 /* Set to Host mode */
Vivek Mahajan32c52202009-06-19 17:56:00 +0530225 setbits_le32(&ehci->usbmode, CM_HOST);
Michael Trimarchie30a3362008-11-28 13:22:09 +0100226
Vivek Mahajan32c52202009-06-19 17:56:00 +0530227 out_be32(&ehci->snoop1, SNOOP_SIZE_2GB);
228 out_be32(&ehci->snoop2, 0x80000000 | SNOOP_SIZE_2GB);
Michael Trimarchie30a3362008-11-28 13:22:09 +0100229
230 /* Init phy */
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100231#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530232 if (priv->phy_type)
233 phy_type = priv->phy_type;
234#else
235 memset(current_usb_controller, '\0', 5);
236 snprintf(current_usb_controller, sizeof(current_usb_controller),
237 "usb%d", index+1);
238
Nikhil Badolaeb97e252013-12-19 11:08:46 +0530239 if (hwconfig_sub(current_usb_controller, "phy_type"))
240 phy_type = hwconfig_subarg(current_usb_controller,
241 "phy_type", &len);
Rajesh Bhagat48c5c512016-07-01 18:51:46 +0530242#endif
Vivek Mahajan288f7fb2009-05-25 17:23:16 +0530243 else
Simon Glass64b723f2017-08-03 12:22:12 -0600244 phy_type = env_get("usb_phy_type");
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +0530245
246 if (!phy_type) {
247#ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
248 /* if none specified assume internal UTMI */
249 strcpy(usb_phy, "utmi");
250 phy_type = usb_phy;
251#else
252 printf("WARNING: USB phy type not defined !!\n");
253 return -1;
254#endif
255 }
256
Nikhil Badola09a3b562014-02-17 16:58:36 +0530257 if (!strncmp(phy_type, "utmi", 4)) {
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +0530258#if defined(CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY)
Nikhil Badola369f6632014-05-08 17:05:26 +0530259 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
260 PHY_CLK_SEL_UTMI);
261 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
262 UTMI_PHY_EN);
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +0530263 udelay(1000); /* delay required for PHY Clk to appear */
264#endif
Rajesh Bhagat2542d962016-07-01 18:51:45 +0530265 out_le32(&(hcor)->or_portsc[0], PORT_PTS_UTMI);
Nikhil Badola369f6632014-05-08 17:05:26 +0530266 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
267 USB_EN);
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +0530268 } else {
Nikhil Badola369f6632014-05-08 17:05:26 +0530269 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
270 PHY_CLK_SEL_ULPI);
271 clrsetbits_be32(&ehci->control, UTMI_PHY_EN |
272 CONTROL_REGISTER_W1C_MASK, USB_EN);
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +0530273 udelay(1000); /* delay required for PHY Clk to appear */
Shengzhou Liud407e1f2012-10-22 13:18:24 +0800274 if (!usb_phy_clk_valid(ehci))
275 return -EINVAL;
Rajesh Bhagat2542d962016-07-01 18:51:45 +0530276 out_le32(&(hcor)->or_portsc[0], PORT_PTS_ULPI);
Ramneek Mehresh3fb68ee2011-03-23 15:20:43 +0530277 }
Michael Trimarchie30a3362008-11-28 13:22:09 +0100278
Vivek Mahajan32c52202009-06-19 17:56:00 +0530279 out_be32(&ehci->prictrl, 0x0000000c);
280 out_be32(&ehci->age_cnt_limit, 0x00000040);
281 out_be32(&ehci->sictrl, 0x00000001);
Michael Trimarchie30a3362008-11-28 13:22:09 +0100282
Vivek Mahajan32c52202009-06-19 17:56:00 +0530283 in_le32(&ehci->usbmode);
Michael Trimarchie30a3362008-11-28 13:22:09 +0100284
Nikhil Badola67f4b262014-10-17 09:12:07 +0530285 if (has_erratum_a007798())
Nikhil Badolab0b48da2014-04-07 08:46:14 +0530286 set_txfifothresh(ehci, TXFIFOTHRESH);
287
Nikhil Badola288542c2014-11-21 17:25:21 +0530288 if (has_erratum_a004477()) {
289 /*
290 * When reset is issued while any ULPI transaction is ongoing
291 * then it may result to corruption of ULPI Function Control
292 * Register which eventually causes phy clock to enter low
293 * power mode which stops the clock. Thus delay is required
294 * before reset to let ongoing ULPI transaction complete.
295 */
296 udelay(1);
297 }
Michael Trimarchie30a3362008-11-28 13:22:09 +0100298 return 0;
299}
300
301/*
Nikhil Badolab0b48da2014-04-07 08:46:14 +0530302 * Setting the value of TXFIFO_THRESH field in TXFILLTUNING register
303 * to counter DDR latencies in writing data into Tx buffer.
304 * This prevents Tx buffer from getting underrun
305 */
306static void set_txfifothresh(struct usb_ehci *ehci, u32 txfifo_thresh)
307{
308 u32 cmd;
309 cmd = ehci_readl(&ehci->txfilltuning);
310 cmd &= ~TXFIFO_THRESH_MASK;
311 cmd |= TXFIFO_THRESH(txfifo_thresh);
312 ehci_writel(&ehci->txfilltuning, cmd);
313}