blob: b8f8e7a794fcb4b6bf72112028772792a534f1d6 [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>
11#include <pci.h>
12#include <usb.h>
Michael Trimarchi6b924872008-11-28 13:22:09 +010013#include <asm/io.h>
Mateusz Kulikowskie162c6b2016-03-31 23:12:23 +020014#include <usb/ehci-ci.h>
Ramneek Mehresh1b719e62011-03-23 15:20:43 +053015#include <hwconfig.h>
Nikhil Badolac26c80a2014-09-30 11:22:43 +053016#include <fsl_usb.h>
Nikhil Badolaa1c04e22014-10-20 16:50:49 +053017#include <fdt_support.h>
Rajesh Bhagatba699a52016-07-01 18:51:46 +053018#include <dm.h>
Michael Trimarchi6b924872008-11-28 13:22:09 +010019
Jean-Christophe PLAGNIOL-VILLARD2731b9a2009-04-03 12:46:58 +020020#include "ehci.h"
Michael Trimarchi6b924872008-11-28 13:22:09 +010021
Rajesh Bhagatba699a52016-07-01 18:51:46 +053022DECLARE_GLOBAL_DATA_PTR;
23
Nikhil Badolaa1c04e22014-10-20 16:50:49 +053024#ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
25#define CONFIG_USB_MAX_CONTROLLER_COUNT 1
26#endif
27
Sven Schwermerfd09c202018-11-21 08:43:56 +010028#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagatba699a52016-07-01 18:51:46 +053029struct ehci_fsl_priv {
30 struct ehci_ctrl ehci;
31 fdt_addr_t hcd_base;
32 char *phy_type;
33};
34#endif
35
Nikhil Badola896720c2014-04-07 08:46:14 +053036static void set_txfifothresh(struct usb_ehci *, u32);
Sven Schwermerfd09c202018-11-21 08:43:56 +010037#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagatba699a52016-07-01 18:51:46 +053038static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
39 struct ehci_hccr *hccr, struct ehci_hcor *hcor);
40#else
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +053041static int ehci_fsl_init(int index, struct usb_ehci *ehci,
42 struct ehci_hccr *hccr, struct ehci_hcor *hcor);
Rajesh Bhagatba699a52016-07-01 18:51:46 +053043#endif
Nikhil Badola896720c2014-04-07 08:46:14 +053044
Shengzhou Liu047cea32012-10-22 13:18:24 +080045/* Check USB PHY clock valid */
46static int usb_phy_clk_valid(struct usb_ehci *ehci)
47{
48 if (!((in_be32(&ehci->control) & PHY_CLK_VALID) ||
49 in_be32(&ehci->prictrl))) {
50 printf("USB PHY clock invalid!\n");
51 return 0;
52 } else {
53 return 1;
54 }
55}
56
Sven Schwermerfd09c202018-11-21 08:43:56 +010057#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagatba699a52016-07-01 18:51:46 +053058static int ehci_fsl_ofdata_to_platdata(struct udevice *dev)
59{
60 struct ehci_fsl_priv *priv = dev_get_priv(dev);
61 const void *prop;
62
Simon Glasse160f7d2017-01-17 16:52:55 -070063 prop = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy_type",
Rajesh Bhagatba699a52016-07-01 18:51:46 +053064 NULL);
65 if (prop) {
66 priv->phy_type = (char *)prop;
67 debug("phy_type %s\n", priv->phy_type);
68 }
69
70 return 0;
71}
72
73static int ehci_fsl_init_after_reset(struct ehci_ctrl *ctrl)
74{
75 struct usb_ehci *ehci = NULL;
76 struct ehci_fsl_priv *priv = container_of(ctrl, struct ehci_fsl_priv,
77 ehci);
Yinbo Zhuad9f2be2019-04-11 11:02:05 +000078#ifdef CONFIG_PPC
79 ehci = (struct usb_ehci *)lower_32_bits(priv->hcd_base);
80#else
Rajesh Bhagatba699a52016-07-01 18:51:46 +053081 ehci = (struct usb_ehci *)priv->hcd_base;
Yinbo Zhuad9f2be2019-04-11 11:02:05 +000082#endif
83
Rajesh Bhagatba699a52016-07-01 18:51:46 +053084 if (ehci_fsl_init(priv, ehci, priv->ehci.hccr, priv->ehci.hcor) < 0)
85 return -ENXIO;
86
87 return 0;
88}
89
90static const struct ehci_ops fsl_ehci_ops = {
91 .init_after_reset = ehci_fsl_init_after_reset,
92};
93
94static int ehci_fsl_probe(struct udevice *dev)
95{
96 struct ehci_fsl_priv *priv = dev_get_priv(dev);
97 struct usb_ehci *ehci = NULL;
98 struct ehci_hccr *hccr;
99 struct ehci_hcor *hcor;
Chris Packham4eaf7f52018-10-04 20:03:53 +1300100 struct ehci_ctrl *ehci_ctrl = &priv->ehci;
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530101
102 /*
103 * Get the base address for EHCI controller from the device node
104 */
Simon Glassa821c4a2017-05-17 17:18:05 -0600105 priv->hcd_base = devfdt_get_addr(dev);
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530106 if (priv->hcd_base == FDT_ADDR_T_NONE) {
107 debug("Can't get the EHCI register base address\n");
108 return -ENXIO;
109 }
Yinbo Zhuad9f2be2019-04-11 11:02:05 +0000110#ifdef CONFIG_PPC
111 ehci = (struct usb_ehci *)lower_32_bits(priv->hcd_base);
112#else
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530113 ehci = (struct usb_ehci *)priv->hcd_base;
Yinbo Zhuad9f2be2019-04-11 11:02:05 +0000114#endif
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530115 hccr = (struct ehci_hccr *)(&ehci->caplength);
116 hcor = (struct ehci_hcor *)
Ran Wangbe3872e2017-12-20 10:34:19 +0800117 ((void *)hccr + HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530118
Chris Packham4eaf7f52018-10-04 20:03:53 +1300119 ehci_ctrl->has_fsl_erratum_a005275 = has_erratum_a005275();
120
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530121 if (ehci_fsl_init(priv, ehci, hccr, hcor) < 0)
122 return -ENXIO;
123
Ran Wangbe3872e2017-12-20 10:34:19 +0800124 debug("ehci-fsl: init hccr %p and hcor %p hc_length %d\n",
125 (void *)hccr, (void *)hcor,
126 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530127
128 return ehci_register(dev, hccr, hcor, &fsl_ehci_ops, 0, USB_INIT_HOST);
129}
130
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530131static const struct udevice_id ehci_usb_ids[] = {
132 { .compatible = "fsl-usb2-mph", },
133 { .compatible = "fsl-usb2-dr", },
134 { }
135};
136
137U_BOOT_DRIVER(ehci_fsl) = {
138 .name = "ehci_fsl",
139 .id = UCLASS_USB,
140 .of_match = ehci_usb_ids,
141 .ofdata_to_platdata = ehci_fsl_ofdata_to_platdata,
142 .probe = ehci_fsl_probe,
Masahiro Yamada40527342016-09-06 22:17:34 +0900143 .remove = ehci_deregister,
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530144 .ops = &ehci_usb_ops,
145 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
146 .priv_auto_alloc_size = sizeof(struct ehci_fsl_priv),
147 .flags = DM_FLAG_ALLOC_PRIV_DMA,
148};
149#else
Michael Trimarchi6b924872008-11-28 13:22:09 +0100150/*
151 * Create the appropriate control structures to manage
152 * a new EHCI host controller.
153 *
154 * Excerpts from linux ehci fsl driver.
155 */
Troy Kisky127efc42013-10-10 15:27:57 -0700156int ehci_hcd_init(int index, enum usb_init_type init,
157 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
Michael Trimarchi6b924872008-11-28 13:22:09 +0100158{
Chris Packham4eaf7f52018-10-04 20:03:53 +1300159 struct ehci_ctrl *ehci_ctrl = container_of(hccr,
160 struct ehci_ctrl, hccr);
ramneek mehresh77354e92013-09-12 16:35:49 +0530161 struct usb_ehci *ehci = NULL;
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530162
163 switch (index) {
164 case 0:
165 ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB1_ADDR;
166 break;
167 case 1:
168 ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB2_ADDR;
169 break;
170 default:
171 printf("ERROR: wrong controller index!!\n");
172 return -EINVAL;
173 };
174
175 *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
176 *hcor = (struct ehci_hcor *)((uint32_t) *hccr +
177 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
178
Chris Packham4eaf7f52018-10-04 20:03:53 +1300179 ehci_ctrl->has_fsl_erratum_a005275 = has_erratum_a005275();
180
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530181 return ehci_fsl_init(index, ehci, *hccr, *hcor);
182}
183
184/*
185 * Destroy the appropriate control structures corresponding
186 * the the EHCI host controller.
187 */
188int ehci_hcd_stop(int index)
189{
190 return 0;
191}
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530192#endif
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530193
Sven Schwermerfd09c202018-11-21 08:43:56 +0100194#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530195static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
196 struct ehci_hccr *hccr, struct ehci_hcor *hcor)
197#else
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530198static int ehci_fsl_init(int index, struct usb_ehci *ehci,
199 struct ehci_hccr *hccr, struct ehci_hcor *hcor)
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530200#endif
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530201{
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530202 const char *phy_type = NULL;
Sven Schwermerfd09c202018-11-21 08:43:56 +0100203#if !CONFIG_IS_ENABLED(DM_USB)
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530204 size_t len;
Nikhil Badola0ecb15c2013-12-19 11:08:46 +0530205 char current_usb_controller[5];
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530206#endif
Kumar Galadd22f7c2011-11-09 10:04:15 -0600207#ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
208 char usb_phy[5];
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530209
210 usb_phy[0] = '\0';
Kumar Galadd22f7c2011-11-09 10:04:15 -0600211#endif
Nikhil Badola11856912014-02-26 17:43:15 +0530212 if (has_erratum_a007075()) {
213 /*
214 * A 5ms delay is needed after applying soft-reset to the
215 * controller to let external ULPI phy come out of reset.
216 * This delay needs to be added before re-initializing
217 * the controller after soft-resetting completes
218 */
219 mdelay(5);
220 }
Michael Trimarchi6b924872008-11-28 13:22:09 +0100221
Michael Trimarchi6b924872008-11-28 13:22:09 +0100222 /* Set to Host mode */
Vivek Mahajan08066152009-06-19 17:56:00 +0530223 setbits_le32(&ehci->usbmode, CM_HOST);
Michael Trimarchi6b924872008-11-28 13:22:09 +0100224
Vivek Mahajan08066152009-06-19 17:56:00 +0530225 out_be32(&ehci->snoop1, SNOOP_SIZE_2GB);
226 out_be32(&ehci->snoop2, 0x80000000 | SNOOP_SIZE_2GB);
Michael Trimarchi6b924872008-11-28 13:22:09 +0100227
228 /* Init phy */
Sven Schwermerfd09c202018-11-21 08:43:56 +0100229#if CONFIG_IS_ENABLED(DM_USB)
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530230 if (priv->phy_type)
231 phy_type = priv->phy_type;
232#else
233 memset(current_usb_controller, '\0', 5);
234 snprintf(current_usb_controller, sizeof(current_usb_controller),
235 "usb%d", index+1);
236
Nikhil Badola0ecb15c2013-12-19 11:08:46 +0530237 if (hwconfig_sub(current_usb_controller, "phy_type"))
238 phy_type = hwconfig_subarg(current_usb_controller,
239 "phy_type", &len);
Rajesh Bhagatba699a52016-07-01 18:51:46 +0530240#endif
Vivek Mahajan4ef01012009-05-25 17:23:16 +0530241 else
Simon Glass00caae62017-08-03 12:22:12 -0600242 phy_type = env_get("usb_phy_type");
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530243
244 if (!phy_type) {
245#ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
246 /* if none specified assume internal UTMI */
247 strcpy(usb_phy, "utmi");
248 phy_type = usb_phy;
249#else
250 printf("WARNING: USB phy type not defined !!\n");
251 return -1;
252#endif
253 }
254
Nikhil Badola91d77462014-02-17 16:58:36 +0530255 if (!strncmp(phy_type, "utmi", 4)) {
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530256#if defined(CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY)
Nikhil Badola15231f62014-05-08 17:05:26 +0530257 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
258 PHY_CLK_SEL_UTMI);
259 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
260 UTMI_PHY_EN);
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530261 udelay(1000); /* delay required for PHY Clk to appear */
262#endif
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530263 out_le32(&(hcor)->or_portsc[0], PORT_PTS_UTMI);
Nikhil Badola15231f62014-05-08 17:05:26 +0530264 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
265 USB_EN);
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530266 } else {
Nikhil Badola15231f62014-05-08 17:05:26 +0530267 clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
268 PHY_CLK_SEL_ULPI);
269 clrsetbits_be32(&ehci->control, UTMI_PHY_EN |
270 CONTROL_REGISTER_W1C_MASK, USB_EN);
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530271 udelay(1000); /* delay required for PHY Clk to appear */
Shengzhou Liu047cea32012-10-22 13:18:24 +0800272 if (!usb_phy_clk_valid(ehci))
273 return -EINVAL;
Rajesh Bhagat1e61ce92016-07-01 18:51:45 +0530274 out_le32(&(hcor)->or_portsc[0], PORT_PTS_ULPI);
Ramneek Mehresh1b719e62011-03-23 15:20:43 +0530275 }
Michael Trimarchi6b924872008-11-28 13:22:09 +0100276
Vivek Mahajan08066152009-06-19 17:56:00 +0530277 out_be32(&ehci->prictrl, 0x0000000c);
278 out_be32(&ehci->age_cnt_limit, 0x00000040);
279 out_be32(&ehci->sictrl, 0x00000001);
Michael Trimarchi6b924872008-11-28 13:22:09 +0100280
Vivek Mahajan08066152009-06-19 17:56:00 +0530281 in_le32(&ehci->usbmode);
Michael Trimarchi6b924872008-11-28 13:22:09 +0100282
Nikhil Badolaf3dff692014-10-17 09:12:07 +0530283 if (has_erratum_a007798())
Nikhil Badola896720c2014-04-07 08:46:14 +0530284 set_txfifothresh(ehci, TXFIFOTHRESH);
285
Nikhil Badola0dc78ff2014-11-21 17:25:21 +0530286 if (has_erratum_a004477()) {
287 /*
288 * When reset is issued while any ULPI transaction is ongoing
289 * then it may result to corruption of ULPI Function Control
290 * Register which eventually causes phy clock to enter low
291 * power mode which stops the clock. Thus delay is required
292 * before reset to let ongoing ULPI transaction complete.
293 */
294 udelay(1);
295 }
Michael Trimarchi6b924872008-11-28 13:22:09 +0100296 return 0;
297}
298
299/*
Nikhil Badola896720c2014-04-07 08:46:14 +0530300 * Setting the value of TXFIFO_THRESH field in TXFILLTUNING register
301 * to counter DDR latencies in writing data into Tx buffer.
302 * This prevents Tx buffer from getting underrun
303 */
304static void set_txfifothresh(struct usb_ehci *ehci, u32 txfifo_thresh)
305{
306 u32 cmd;
307 cmd = ehci_readl(&ehci->txfilltuning);
308 cmd &= ~TXFIFO_THRESH_MASK;
309 cmd |= TXFIFO_THRESH(txfifo_thresh);
310 ehci_writel(&ehci->txfilltuning, cmd);
311}