blob: ced295ef0fa40166228b1360f6f4b8364fb3352e [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>
Michael Trimarchi6b924872008-11-28 13:22:09 +010012#include <pci.h>
13#include <usb.h>
Michael Trimarchi6b924872008-11-28 13:22:09 +010014#include <asm/io.h>
Mateusz Kulikowskie162c6b2016-03-31 23:12:23 +020015#include <usb/ehci-ci.h>
Ramneek Mehresh1b719e62011-03-23 15:20:43 +053016#include <hwconfig.h>
Nikhil Badolac26c80a2014-09-30 11:22:43 +053017#include <fsl_usb.h>
Nikhil Badolaa1c04e22014-10-20 16:50:49 +053018#include <fdt_support.h>
Rajesh Bhagatba699a52016-07-01 18:51:46 +053019#include <dm.h>
Michael Trimarchi6b924872008-11-28 13:22:09 +010020
Jean-Christophe PLAGNIOL-VILLARD2731b9a2009-04-03 12:46:58 +020021#include "ehci.h"
Michael Trimarchi6b924872008-11-28 13:22:09 +010022
Rajesh Bhagatba699a52016-07-01 18:51:46 +053023DECLARE_GLOBAL_DATA_PTR;
24
Nikhil Badolaa1c04e22014-10-20 16:50:49 +053025#ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
26#define CONFIG_USB_MAX_CONTROLLER_COUNT 1
27#endif
28
Sven Schwermerfd09c202018-11-21 08:43:56 +010029#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagatba699a52016-07-01 18:51:46 +053030struct ehci_fsl_priv {
31 struct ehci_ctrl ehci;
32 fdt_addr_t hcd_base;
33 char *phy_type;
34};
35#endif
36
Nikhil Badola896720c2014-04-07 08:46:14 +053037static void set_txfifothresh(struct usb_ehci *, u32);
Sven Schwermerfd09c202018-11-21 08:43:56 +010038#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagatba699a52016-07-01 18:51:46 +053039static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
40 struct ehci_hccr *hccr, struct ehci_hcor *hcor);
41#else
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +053042static int ehci_fsl_init(int index, struct usb_ehci *ehci,
43 struct ehci_hccr *hccr, struct ehci_hcor *hcor);
Rajesh Bhagatba699a52016-07-01 18:51:46 +053044#endif
Nikhil Badola896720c2014-04-07 08:46:14 +053045
Shengzhou Liu047cea32012-10-22 13:18:24 +080046/* Check USB PHY clock valid */
47static int usb_phy_clk_valid(struct usb_ehci *ehci)
48{
49 if (!((in_be32(&ehci->control) & PHY_CLK_VALID) ||
50 in_be32(&ehci->prictrl))) {
51 printf("USB PHY clock invalid!\n");
52 return 0;
53 } else {
54 return 1;
55 }
56}
57
Sven Schwermerfd09c202018-11-21 08:43:56 +010058#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagatba699a52016-07-01 18:51:46 +053059static int ehci_fsl_ofdata_to_platdata(struct udevice *dev)
60{
61 struct ehci_fsl_priv *priv = dev_get_priv(dev);
62 const void *prop;
63
Simon Glasse160f7d2017-01-17 16:52:55 -070064 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy_type",
Rajesh Bhagatba699a52016-07-01 18:51:46 +053065 NULL);
66 if (prop) {
67 priv->phy_type = (char *)prop;
68 debug("phy_type %s\n", priv->phy_type);
69 }
70
71 return 0;
72}
73
74static int ehci_fsl_init_after_reset(struct ehci_ctrl *ctrl)
75{
76 struct usb_ehci *ehci = NULL;
77 struct ehci_fsl_priv *priv = container_of(ctrl, struct ehci_fsl_priv,
78 ehci);
Yinbo Zhuad9f2be2019-04-11 11:02:05 +000079#ifdef CONFIG_PPC
80 ehci = (struct usb_ehci *)lower_32_bits(priv->hcd_base);
81#else
Rajesh Bhagatba699a52016-07-01 18:51:46 +053082 ehci = (struct usb_ehci *)priv->hcd_base;
Yinbo Zhuad9f2be2019-04-11 11:02:05 +000083#endif
84
Rajesh Bhagatba699a52016-07-01 18:51:46 +053085 if (ehci_fsl_init(priv, ehci, priv->ehci.hccr, priv->ehci.hcor) < 0)
86 return -ENXIO;
87
88 return 0;
89}
90
91static const struct ehci_ops fsl_ehci_ops = {
92 .init_after_reset = ehci_fsl_init_after_reset,
93};
94
95static int ehci_fsl_probe(struct udevice *dev)
96{
97 struct ehci_fsl_priv *priv = dev_get_priv(dev);
98 struct usb_ehci *ehci = NULL;
99 struct ehci_hccr *hccr;
100 struct ehci_hcor *hcor;
Chris Packham4eaf7f52018-10-04 20:03:53 +1300101 struct ehci_ctrl *ehci_ctrl = &priv->ehci;
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530102
103 /*
104 * Get the base address for EHCI controller from the device node
105 */
Simon Glassa821c4a2017-05-17 17:18:05 -0600106 priv->hcd_base = devfdt_get_addr(dev);
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530107 if (priv->hcd_base == FDT_ADDR_T_NONE) {
108 debug("Can't get the EHCI register base address\n");
109 return -ENXIO;
110 }
Yinbo Zhuad9f2be2019-04-11 11:02:05 +0000111#ifdef CONFIG_PPC
112 ehci = (struct usb_ehci *)lower_32_bits(priv->hcd_base);
113#else
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530114 ehci = (struct usb_ehci *)priv->hcd_base;
Yinbo Zhuad9f2be2019-04-11 11:02:05 +0000115#endif
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530116 hccr = (struct ehci_hccr *)(&ehci->caplength);
117 hcor = (struct ehci_hcor *)
Ran Wangbe3872e2017-12-20 10:34:19 +0800118 ((void *)hccr + HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530119
Chris Packham4eaf7f52018-10-04 20:03:53 +1300120 ehci_ctrl->has_fsl_erratum_a005275 = has_erratum_a005275();
121
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530122 if (ehci_fsl_init(priv, ehci, hccr, hcor) < 0)
123 return -ENXIO;
124
Ran Wangbe3872e2017-12-20 10:34:19 +0800125 debug("ehci-fsl: init hccr %p and hcor %p hc_length %d\n",
126 (void *)hccr, (void *)hcor,
127 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530128
129 return ehci_register(dev, hccr, hcor, &fsl_ehci_ops, 0, USB_INIT_HOST);
130}
131
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530132static const struct udevice_id ehci_usb_ids[] = {
133 { .compatible = "fsl-usb2-mph", },
134 { .compatible = "fsl-usb2-dr", },
135 { }
136};
137
138U_BOOT_DRIVER(ehci_fsl) = {
139 .name = "ehci_fsl",
140 .id = UCLASS_USB,
141 .of_match = ehci_usb_ids,
142 .ofdata_to_platdata = ehci_fsl_ofdata_to_platdata,
143 .probe = ehci_fsl_probe,
Masahiro Yamada40527342016-09-06 22:17:34 +0900144 .remove = ehci_deregister,
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530145 .ops = &ehci_usb_ops,
146 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
147 .priv_auto_alloc_size = sizeof(struct ehci_fsl_priv),
148 .flags = DM_FLAG_ALLOC_PRIV_DMA,
149};
150#else
Michael Trimarchi6b924872008-11-28 13:22:09 +0100151/*
152 * Create the appropriate control structures to manage
153 * a new EHCI host controller.
154 *
155 * Excerpts from linux ehci fsl driver.
156 */
Troy Kisky127efc42013-10-10 15:27:57 -0700157int ehci_hcd_init(int index, enum usb_init_type init,
158 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
Michael Trimarchi6b924872008-11-28 13:22:09 +0100159{
Chris Packham4eaf7f52018-10-04 20:03:53 +1300160 struct ehci_ctrl *ehci_ctrl = container_of(hccr,
161 struct ehci_ctrl, hccr);
ramneek mehresh77354e92013-09-12 16:35:49 +0530162 struct usb_ehci *ehci = NULL;
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530163
164 switch (index) {
165 case 0:
166 ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB1_ADDR;
167 break;
168 case 1:
169 ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB2_ADDR;
170 break;
171 default:
172 printf("ERROR: wrong controller index!!\n");
173 return -EINVAL;
174 };
175
176 *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
177 *hcor = (struct ehci_hcor *)((uint32_t) *hccr +
178 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
179
Chris Packham4eaf7f52018-10-04 20:03:53 +1300180 ehci_ctrl->has_fsl_erratum_a005275 = has_erratum_a005275();
181
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530182 return ehci_fsl_init(index, ehci, *hccr, *hcor);
183}
184
185/*
186 * Destroy the appropriate control structures corresponding
187 * the the EHCI host controller.
188 */
189int ehci_hcd_stop(int index)
190{
191 return 0;
192}
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530193#endif
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530194
Sven Schwermerfd09c202018-11-21 08:43:56 +0100195#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530196static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
197 struct ehci_hccr *hccr, struct ehci_hcor *hcor)
198#else
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530199static int ehci_fsl_init(int index, struct usb_ehci *ehci,
200 struct ehci_hccr *hccr, struct ehci_hcor *hcor)
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530201#endif
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530202{
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530203 const char *phy_type = NULL;
Sven Schwermerfd09c202018-11-21 08:43:56 +0100204#if !CONFIG_IS_ENABLED(DM_USB)
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530205 size_t len;
Nikhil Badola0ecb15c2013-12-19 11:08:46 +0530206 char current_usb_controller[5];
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530207#endif
Kumar Galadd22f7c2011-11-09 10:04:15 -0600208#ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
209 char usb_phy[5];
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530210
211 usb_phy[0] = '\0';
Kumar Galadd22f7c2011-11-09 10:04:15 -0600212#endif
Nikhil Badola11856912014-02-26 17:43:15 +0530213 if (has_erratum_a007075()) {
214 /*
215 * A 5ms delay is needed after applying soft-reset to the
216 * controller to let external ULPI phy come out of reset.
217 * This delay needs to be added before re-initializing
218 * the controller after soft-resetting completes
219 */
220 mdelay(5);
221 }
Michael Trimarchi6b924872008-11-28 13:22:09 +0100222
Michael Trimarchi6b924872008-11-28 13:22:09 +0100223 /* Set to Host mode */
Vivek Mahajan08066152009-06-19 17:56:00 +0530224 setbits_le32(&ehci->usbmode, CM_HOST);
Michael Trimarchi6b924872008-11-28 13:22:09 +0100225
Vivek Mahajan08066152009-06-19 17:56:00 +0530226 out_be32(&ehci->snoop1, SNOOP_SIZE_2GB);
227 out_be32(&ehci->snoop2, 0x80000000 | SNOOP_SIZE_2GB);
Michael Trimarchi6b924872008-11-28 13:22:09 +0100228
229 /* Init phy */
Sven Schwermerfd09c202018-11-21 08:43:56 +0100230#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530231 if (priv->phy_type)
232 phy_type = priv->phy_type;
233#else
234 memset(current_usb_controller, '\0', 5);
235 snprintf(current_usb_controller, sizeof(current_usb_controller),
236 "usb%d", index+1);
237
Nikhil Badola0ecb15c2013-12-19 11:08:46 +0530238 if (hwconfig_sub(current_usb_controller, "phy_type"))
239 phy_type = hwconfig_subarg(current_usb_controller,
240 "phy_type", &len);
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530241#endif
Vivek Mahajan4ef01012009-05-25 17:23:16 +0530242 else
Simon Glass00caae62017-08-03 12:22:12 -0600243 phy_type = env_get("usb_phy_type");
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530244
245 if (!phy_type) {
246#ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
247 /* if none specified assume internal UTMI */
248 strcpy(usb_phy, "utmi");
249 phy_type = usb_phy;
250#else
251 printf("WARNING: USB phy type not defined !!\n");
252 return -1;
253#endif
254 }
255
Nikhil Badola91d77462014-02-17 16:58:36 +0530256 if (!strncmp(phy_type, "utmi", 4)) {
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530257#if defined(CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY)
Nikhil Badola15231f62014-05-08 17:05:26 +0530258 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
259 PHY_CLK_SEL_UTMI);
260 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
261 UTMI_PHY_EN);
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530262 udelay(1000); /* delay required for PHY Clk to appear */
263#endif
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530264 out_le32(&(hcor)->or_portsc[0], PORT_PTS_UTMI);
Nikhil Badola15231f62014-05-08 17:05:26 +0530265 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
266 USB_EN);
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530267 } else {
Nikhil Badola15231f62014-05-08 17:05:26 +0530268 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
269 PHY_CLK_SEL_ULPI);
270 clrsetbits_be32(&ehci->control, UTMI_PHY_EN |
271 CONTROL_REGISTER_W1C_MASK, USB_EN);
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530272 udelay(1000); /* delay required for PHY Clk to appear */
Shengzhou Liu047cea32012-10-22 13:18:24 +0800273 if (!usb_phy_clk_valid(ehci))
274 return -EINVAL;
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530275 out_le32(&(hcor)->or_portsc[0], PORT_PTS_ULPI);
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530276 }
Michael Trimarchi6b924872008-11-28 13:22:09 +0100277
Vivek Mahajan08066152009-06-19 17:56:00 +0530278 out_be32(&ehci->prictrl, 0x0000000c);
279 out_be32(&ehci->age_cnt_limit, 0x00000040);
280 out_be32(&ehci->sictrl, 0x00000001);
Michael Trimarchi6b924872008-11-28 13:22:09 +0100281
Vivek Mahajan08066152009-06-19 17:56:00 +0530282 in_le32(&ehci->usbmode);
Michael Trimarchi6b924872008-11-28 13:22:09 +0100283
Nikhil Badolaf3dff692014-10-17 09:12:07 +0530284 if (has_erratum_a007798())
Nikhil Badola896720c2014-04-07 08:46:14 +0530285 set_txfifothresh(ehci, TXFIFOTHRESH);
286
Nikhil Badola0dc78ff2014-11-21 17:25:21 +0530287 if (has_erratum_a004477()) {
288 /*
289 * When reset is issued while any ULPI transaction is ongoing
290 * then it may result to corruption of ULPI Function Control
291 * Register which eventually causes phy clock to enter low
292 * power mode which stops the clock. Thus delay is required
293 * before reset to let ongoing ULPI transaction complete.
294 */
295 udelay(1);
296 }
Michael Trimarchi6b924872008-11-28 13:22:09 +0100297 return 0;
298}
299
300/*
Nikhil Badola896720c2014-04-07 08:46:14 +0530301 * Setting the value of TXFIFO_THRESH field in TXFILLTUNING register
302 * to counter DDR latencies in writing data into Tx buffer.
303 * This prevents Tx buffer from getting underrun
304 */
305static void set_txfifothresh(struct usb_ehci *ehci, u32 txfifo_thresh)
306{
307 u32 cmd;
308 cmd = ehci_readl(&ehci->txfilltuning);
309 cmd &= ~TXFIFO_THRESH_MASK;
310 cmd |= TXFIFO_THRESH(txfifo_thresh);
311 ehci_writel(&ehci->txfilltuning, cmd);
312}