blob: cf1f882441bfb7f2550d7ee96e2b58c8d7f61c6f [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
Nikhil Badolaa1c04e22014-10-20 16:50:49 +053028#ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
29#define CONFIG_USB_MAX_CONTROLLER_COUNT 1
30#endif
31
Sven Schwermerfd09c202018-11-21 08:43:56 +010032#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagatba699a52016-07-01 18:51:46 +053033struct ehci_fsl_priv {
34 struct ehci_ctrl ehci;
35 fdt_addr_t hcd_base;
36 char *phy_type;
37};
38#endif
39
Nikhil Badola896720c2014-04-07 08:46:14 +053040static void set_txfifothresh(struct usb_ehci *, u32);
Sven Schwermerfd09c202018-11-21 08:43:56 +010041#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagatba699a52016-07-01 18:51:46 +053042static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
43 struct ehci_hccr *hccr, struct ehci_hcor *hcor);
44#else
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +053045static int ehci_fsl_init(int index, struct usb_ehci *ehci,
46 struct ehci_hccr *hccr, struct ehci_hcor *hcor);
Rajesh Bhagatba699a52016-07-01 18:51:46 +053047#endif
Nikhil Badola896720c2014-04-07 08:46:14 +053048
Shengzhou Liu047cea32012-10-22 13:18:24 +080049/* Check USB PHY clock valid */
50static int usb_phy_clk_valid(struct usb_ehci *ehci)
51{
52 if (!((in_be32(&ehci->control) & PHY_CLK_VALID) ||
53 in_be32(&ehci->prictrl))) {
54 printf("USB PHY clock invalid!\n");
55 return 0;
56 } else {
57 return 1;
58 }
59}
60
Sven Schwermerfd09c202018-11-21 08:43:56 +010061#if CONFIG_IS_ENABLED(DM_USB)
Simon Glassd1998a92020-12-03 16:55:21 -070062static int ehci_fsl_of_to_plat(struct udevice *dev)
Rajesh Bhagatba699a52016-07-01 18:51:46 +053063{
64 struct ehci_fsl_priv *priv = dev_get_priv(dev);
65 const void *prop;
66
Simon Glasse160f7d2017-01-17 16:52:55 -070067 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy_type",
Rajesh Bhagatba699a52016-07-01 18:51:46 +053068 NULL);
69 if (prop) {
70 priv->phy_type = (char *)prop;
71 debug("phy_type %s\n", priv->phy_type);
72 }
73
74 return 0;
75}
76
77static int ehci_fsl_init_after_reset(struct ehci_ctrl *ctrl)
78{
79 struct usb_ehci *ehci = NULL;
80 struct ehci_fsl_priv *priv = container_of(ctrl, struct ehci_fsl_priv,
81 ehci);
Yinbo Zhuad9f2be2019-04-11 11:02:05 +000082#ifdef CONFIG_PPC
83 ehci = (struct usb_ehci *)lower_32_bits(priv->hcd_base);
84#else
Rajesh Bhagatba699a52016-07-01 18:51:46 +053085 ehci = (struct usb_ehci *)priv->hcd_base;
Yinbo Zhuad9f2be2019-04-11 11:02:05 +000086#endif
87
Rajesh Bhagatba699a52016-07-01 18:51:46 +053088 if (ehci_fsl_init(priv, ehci, priv->ehci.hccr, priv->ehci.hcor) < 0)
89 return -ENXIO;
90
91 return 0;
92}
93
94static const struct ehci_ops fsl_ehci_ops = {
95 .init_after_reset = ehci_fsl_init_after_reset,
96};
97
98static int ehci_fsl_probe(struct udevice *dev)
99{
100 struct ehci_fsl_priv *priv = dev_get_priv(dev);
101 struct usb_ehci *ehci = NULL;
102 struct ehci_hccr *hccr;
103 struct ehci_hcor *hcor;
Chris Packham4eaf7f52018-10-04 20:03:53 +1300104 struct ehci_ctrl *ehci_ctrl = &priv->ehci;
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530105
106 /*
107 * Get the base address for EHCI controller from the device node
108 */
Masahiro Yamada25484932020-07-17 14:36:48 +0900109 priv->hcd_base = dev_read_addr(dev);
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530110 if (priv->hcd_base == FDT_ADDR_T_NONE) {
111 debug("Can't get the EHCI register base address\n");
112 return -ENXIO;
113 }
Yinbo Zhuad9f2be2019-04-11 11:02:05 +0000114#ifdef CONFIG_PPC
115 ehci = (struct usb_ehci *)lower_32_bits(priv->hcd_base);
116#else
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530117 ehci = (struct usb_ehci *)priv->hcd_base;
Yinbo Zhuad9f2be2019-04-11 11:02:05 +0000118#endif
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530119 hccr = (struct ehci_hccr *)(&ehci->caplength);
120 hcor = (struct ehci_hcor *)
Ran Wangbe3872e2017-12-20 10:34:19 +0800121 ((void *)hccr + HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530122
Chris Packham4eaf7f52018-10-04 20:03:53 +1300123 ehci_ctrl->has_fsl_erratum_a005275 = has_erratum_a005275();
124
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530125 if (ehci_fsl_init(priv, ehci, hccr, hcor) < 0)
126 return -ENXIO;
127
Ran Wangbe3872e2017-12-20 10:34:19 +0800128 debug("ehci-fsl: init hccr %p and hcor %p hc_length %d\n",
129 (void *)hccr, (void *)hcor,
130 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530131
132 return ehci_register(dev, hccr, hcor, &fsl_ehci_ops, 0, USB_INIT_HOST);
133}
134
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530135static const struct udevice_id ehci_usb_ids[] = {
136 { .compatible = "fsl-usb2-mph", },
137 { .compatible = "fsl-usb2-dr", },
138 { }
139};
140
141U_BOOT_DRIVER(ehci_fsl) = {
142 .name = "ehci_fsl",
143 .id = UCLASS_USB,
144 .of_match = ehci_usb_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700145 .of_to_plat = ehci_fsl_of_to_plat,
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530146 .probe = ehci_fsl_probe,
Masahiro Yamada40527342016-09-06 22:17:34 +0900147 .remove = ehci_deregister,
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530148 .ops = &ehci_usb_ops,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700149 .plat_auto = sizeof(struct usb_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700150 .priv_auto = sizeof(struct ehci_fsl_priv),
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530151 .flags = DM_FLAG_ALLOC_PRIV_DMA,
152};
153#else
Michael Trimarchi6b924872008-11-28 13:22:09 +0100154/*
155 * Create the appropriate control structures to manage
156 * a new EHCI host controller.
157 *
158 * Excerpts from linux ehci fsl driver.
159 */
Troy Kisky127efc42013-10-10 15:27:57 -0700160int ehci_hcd_init(int index, enum usb_init_type init,
161 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
Michael Trimarchi6b924872008-11-28 13:22:09 +0100162{
Chris Packham4eaf7f52018-10-04 20:03:53 +1300163 struct ehci_ctrl *ehci_ctrl = container_of(hccr,
164 struct ehci_ctrl, hccr);
ramneek mehresh77354e92013-09-12 16:35:49 +0530165 struct usb_ehci *ehci = NULL;
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530166
167 switch (index) {
168 case 0:
169 ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB1_ADDR;
170 break;
171 case 1:
172 ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB2_ADDR;
173 break;
174 default:
175 printf("ERROR: wrong controller index!!\n");
176 return -EINVAL;
177 };
178
179 *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
180 *hcor = (struct ehci_hcor *)((uint32_t) *hccr +
181 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
182
Chris Packham4eaf7f52018-10-04 20:03:53 +1300183 ehci_ctrl->has_fsl_erratum_a005275 = has_erratum_a005275();
184
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530185 return ehci_fsl_init(index, ehci, *hccr, *hcor);
186}
187
188/*
189 * Destroy the appropriate control structures corresponding
190 * the the EHCI host controller.
191 */
192int ehci_hcd_stop(int index)
193{
194 return 0;
195}
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530196#endif
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530197
Sven Schwermerfd09c202018-11-21 08:43:56 +0100198#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530199static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
200 struct ehci_hccr *hccr, struct ehci_hcor *hcor)
201#else
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530202static int ehci_fsl_init(int index, struct usb_ehci *ehci,
203 struct ehci_hccr *hccr, struct ehci_hcor *hcor)
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530204#endif
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530205{
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530206 const char *phy_type = NULL;
Sven Schwermerfd09c202018-11-21 08:43:56 +0100207#if !CONFIG_IS_ENABLED(DM_USB)
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530208 size_t len;
Nikhil Badola0ecb15c2013-12-19 11:08:46 +0530209 char current_usb_controller[5];
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530210#endif
Kumar Galadd22f7c2011-11-09 10:04:15 -0600211#ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
212 char usb_phy[5];
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530213
214 usb_phy[0] = '\0';
Kumar Galadd22f7c2011-11-09 10:04:15 -0600215#endif
Nikhil Badola11856912014-02-26 17:43:15 +0530216 if (has_erratum_a007075()) {
217 /*
218 * A 5ms delay is needed after applying soft-reset to the
219 * controller to let external ULPI phy come out of reset.
220 * This delay needs to be added before re-initializing
221 * the controller after soft-resetting completes
222 */
223 mdelay(5);
224 }
Michael Trimarchi6b924872008-11-28 13:22:09 +0100225
Michael Trimarchi6b924872008-11-28 13:22:09 +0100226 /* Set to Host mode */
Vivek Mahajan08066152009-06-19 17:56:00 +0530227 setbits_le32(&ehci->usbmode, CM_HOST);
Michael Trimarchi6b924872008-11-28 13:22:09 +0100228
Vivek Mahajan08066152009-06-19 17:56:00 +0530229 out_be32(&ehci->snoop1, SNOOP_SIZE_2GB);
230 out_be32(&ehci->snoop2, 0x80000000 | SNOOP_SIZE_2GB);
Michael Trimarchi6b924872008-11-28 13:22:09 +0100231
232 /* Init phy */
Sven Schwermerfd09c202018-11-21 08:43:56 +0100233#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530234 if (priv->phy_type)
235 phy_type = priv->phy_type;
236#else
237 memset(current_usb_controller, '\0', 5);
238 snprintf(current_usb_controller, sizeof(current_usb_controller),
239 "usb%d", index+1);
240
Nikhil Badola0ecb15c2013-12-19 11:08:46 +0530241 if (hwconfig_sub(current_usb_controller, "phy_type"))
242 phy_type = hwconfig_subarg(current_usb_controller,
243 "phy_type", &len);
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530244#endif
Vivek Mahajan4ef01012009-05-25 17:23:16 +0530245 else
Simon Glass00caae62017-08-03 12:22:12 -0600246 phy_type = env_get("usb_phy_type");
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530247
248 if (!phy_type) {
249#ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
250 /* if none specified assume internal UTMI */
251 strcpy(usb_phy, "utmi");
252 phy_type = usb_phy;
253#else
254 printf("WARNING: USB phy type not defined !!\n");
255 return -1;
256#endif
257 }
258
Nikhil Badola91d77462014-02-17 16:58:36 +0530259 if (!strncmp(phy_type, "utmi", 4)) {
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530260#if defined(CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY)
Nikhil Badola15231f62014-05-08 17:05:26 +0530261 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
262 PHY_CLK_SEL_UTMI);
263 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
264 UTMI_PHY_EN);
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530265 udelay(1000); /* delay required for PHY Clk to appear */
266#endif
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530267 out_le32(&(hcor)->or_portsc[0], PORT_PTS_UTMI);
Nikhil Badola15231f62014-05-08 17:05:26 +0530268 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
269 USB_EN);
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530270 } else {
Nikhil Badola15231f62014-05-08 17:05:26 +0530271 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
272 PHY_CLK_SEL_ULPI);
273 clrsetbits_be32(&ehci->control, UTMI_PHY_EN |
274 CONTROL_REGISTER_W1C_MASK, USB_EN);
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530275 udelay(1000); /* delay required for PHY Clk to appear */
Shengzhou Liu047cea32012-10-22 13:18:24 +0800276 if (!usb_phy_clk_valid(ehci))
277 return -EINVAL;
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530278 out_le32(&(hcor)->or_portsc[0], PORT_PTS_ULPI);
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530279 }
Michael Trimarchi6b924872008-11-28 13:22:09 +0100280
Vivek Mahajan08066152009-06-19 17:56:00 +0530281 out_be32(&ehci->prictrl, 0x0000000c);
282 out_be32(&ehci->age_cnt_limit, 0x00000040);
283 out_be32(&ehci->sictrl, 0x00000001);
Michael Trimarchi6b924872008-11-28 13:22:09 +0100284
Vivek Mahajan08066152009-06-19 17:56:00 +0530285 in_le32(&ehci->usbmode);
Michael Trimarchi6b924872008-11-28 13:22:09 +0100286
Nikhil Badolaf3dff692014-10-17 09:12:07 +0530287 if (has_erratum_a007798())
Nikhil Badola896720c2014-04-07 08:46:14 +0530288 set_txfifothresh(ehci, TXFIFOTHRESH);
289
Nikhil Badola0dc78ff2014-11-21 17:25:21 +0530290 if (has_erratum_a004477()) {
291 /*
292 * When reset is issued while any ULPI transaction is ongoing
293 * then it may result to corruption of ULPI Function Control
294 * Register which eventually causes phy clock to enter low
295 * power mode which stops the clock. Thus delay is required
296 * before reset to let ongoing ULPI transaction complete.
297 */
298 udelay(1);
299 }
Michael Trimarchi6b924872008-11-28 13:22:09 +0100300 return 0;
301}
302
303/*
Nikhil Badola896720c2014-04-07 08:46:14 +0530304 * Setting the value of TXFIFO_THRESH field in TXFILLTUNING register
305 * to counter DDR latencies in writing data into Tx buffer.
306 * This prevents Tx buffer from getting underrun
307 */
308static void set_txfifothresh(struct usb_ehci *ehci, u32 txfifo_thresh)
309{
310 u32 cmd;
311 cmd = ehci_readl(&ehci->txfilltuning);
312 cmd &= ~TXFIFO_THRESH_MASK;
313 cmd |= TXFIFO_THRESH(txfifo_thresh);
314 ehci_writel(&ehci->txfilltuning, cmd);
315}