blob: cbe065bbaf08f87004cf748191951fe858ba4ab5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07002/*
3 * Copyright (C) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
4 * Copyright (C) 2014 Marek Vasut <marex@denx.de>
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07005 */
6
7#include <common.h>
Simon Glassf58a41e2015-07-07 20:53:37 -06008#include <dm.h>
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07009#include <errno.h>
10#include <usb.h>
11#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060012#include <memalign.h>
Stephen Warren5c0beb52015-03-24 20:07:35 -060013#include <phys2bus.h>
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -070014#include <usbroothubdes.h>
Mateusz Kulikowskifd2cd662016-01-23 11:54:30 +010015#include <wait_bit.h>
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -070016#include <asm/io.h>
Kever Yang5c735362017-03-10 12:05:14 +080017#include <power/regulator.h>
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -070018
19#include "dwc2.h"
20
21/* Use only HC channel 0. */
22#define DWC2_HC_CHANNEL 0
23
24#define DWC2_STATUS_BUF_SIZE 64
Alexey Brodkin42637fd2018-02-28 16:16:58 +030025#define DWC2_DATA_BUF_SIZE (CONFIG_USB_DWC2_BUFFER_SIZE * 1024)
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -070026
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -070027#define MAX_DEVICE 16
28#define MAX_ENDPOINT 16
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -070029
Simon Glasscc3e3a92015-07-07 20:53:36 -060030struct dwc2_priv {
Simon Glassf58a41e2015-07-07 20:53:37 -060031#ifdef CONFIG_DM_USB
Alexander Steindb402e02015-07-24 09:22:14 +020032 uint8_t aligned_buffer[DWC2_DATA_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
33 uint8_t status_buffer[DWC2_STATUS_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
Christophe Kerello82e79752018-03-15 18:00:30 +010034#ifdef CONFIG_DM_REGULATOR
35 struct udevice *vbus_supply;
36#endif
Simon Glassf58a41e2015-07-07 20:53:37 -060037#else
Simon Glasscc3e3a92015-07-07 20:53:36 -060038 uint8_t *aligned_buffer;
39 uint8_t *status_buffer;
Simon Glassf58a41e2015-07-07 20:53:37 -060040#endif
Stefan Brüns25612f22016-01-23 01:42:25 +010041 u8 in_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
42 u8 out_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
Simon Glasscc3e3a92015-07-07 20:53:36 -060043 struct dwc2_core_regs *regs;
44 int root_hub_devnum;
Marek Vasut618da562016-04-27 14:55:57 +020045 bool ext_vbus;
Meng Dongyangdd22bac2017-06-28 19:22:43 +080046 /*
47 * The hnp/srp capability must be disabled if the platform
48 * does't support hnp/srp. Otherwise the force mode can't work.
49 */
Meng Dongyangc65a3492017-06-08 15:34:20 +080050 bool hnp_srp_disable;
Marek Vasutb4fbd082016-04-27 14:58:49 +020051 bool oc_disable;
Simon Glasscc3e3a92015-07-07 20:53:36 -060052};
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -070053
Simon Glassf58a41e2015-07-07 20:53:37 -060054#ifndef CONFIG_DM_USB
Alexander Steindb402e02015-07-24 09:22:14 +020055/* We need cacheline-aligned buffers for DMA transfers and dcache support */
56DEFINE_ALIGN_BUFFER(uint8_t, aligned_buffer_addr, DWC2_DATA_BUF_SIZE,
57 ARCH_DMA_MINALIGN);
58DEFINE_ALIGN_BUFFER(uint8_t, status_buffer_addr, DWC2_STATUS_BUF_SIZE,
59 ARCH_DMA_MINALIGN);
Simon Glasscc3e3a92015-07-07 20:53:36 -060060
61static struct dwc2_priv local;
Simon Glassf58a41e2015-07-07 20:53:37 -060062#endif
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -070063
64/*
65 * DWC2 IP interface
66 */
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -070067
68/*
69 * Initializes the FSLSPClkSel field of the HCFG register
70 * depending on the PHY type.
71 */
72static void init_fslspclksel(struct dwc2_core_regs *regs)
73{
74 uint32_t phyclk;
75
76#if (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
77 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
78#else
79 /* High speed PHY running at full speed or high speed */
80 phyclk = DWC2_HCFG_FSLSPCLKSEL_30_60_MHZ;
81#endif
82
83#ifdef CONFIG_DWC2_ULPI_FS_LS
84 uint32_t hwcfg2 = readl(&regs->ghwcfg2);
85 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
86 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
87 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
88 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
89
90 if (hval == 2 && fval == 1)
91 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
92#endif
93
94 clrsetbits_le32(&regs->host_regs.hcfg,
95 DWC2_HCFG_FSLSPCLKSEL_MASK,
96 phyclk << DWC2_HCFG_FSLSPCLKSEL_OFFSET);
97}
98
99/*
100 * Flush a Tx FIFO.
101 *
102 * @param regs Programming view of DWC_otg controller.
103 * @param num Tx FIFO to flush.
104 */
105static void dwc_otg_flush_tx_fifo(struct dwc2_core_regs *regs, const int num)
106{
107 int ret;
108
109 writel(DWC2_GRSTCTL_TXFFLSH | (num << DWC2_GRSTCTL_TXFNUM_OFFSET),
110 &regs->grstctl);
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100111 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_TXFFLSH,
112 false, 1000, false);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700113 if (ret)
Patrice Chotardac6c7962018-03-15 18:00:32 +0100114 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700115
116 /* Wait for 3 PHY Clocks */
117 udelay(1);
118}
119
120/*
121 * Flush Rx FIFO.
122 *
123 * @param regs Programming view of DWC_otg controller.
124 */
125static void dwc_otg_flush_rx_fifo(struct dwc2_core_regs *regs)
126{
127 int ret;
128
129 writel(DWC2_GRSTCTL_RXFFLSH, &regs->grstctl);
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100130 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_RXFFLSH,
131 false, 1000, false);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700132 if (ret)
Patrice Chotardac6c7962018-03-15 18:00:32 +0100133 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700134
135 /* Wait for 3 PHY Clocks */
136 udelay(1);
137}
138
139/*
140 * Do core a soft reset of the core. Be careful with this because it
141 * resets all the internal state machines of the core.
142 */
143static void dwc_otg_core_reset(struct dwc2_core_regs *regs)
144{
145 int ret;
146
147 /* Wait for AHB master IDLE state. */
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100148 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_AHBIDLE,
149 true, 1000, false);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700150 if (ret)
Patrice Chotardac6c7962018-03-15 18:00:32 +0100151 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700152
153 /* Core Soft Reset */
154 writel(DWC2_GRSTCTL_CSFTRST, &regs->grstctl);
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100155 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_CSFTRST,
156 false, 1000, false);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700157 if (ret)
Patrice Chotardac6c7962018-03-15 18:00:32 +0100158 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700159
160 /*
161 * Wait for core to come out of reset.
162 * NOTE: This long sleep is _very_ important, otherwise the core will
163 * not stay in host mode after a connector ID change!
164 */
165 mdelay(100);
166}
167
Kever Yang5c735362017-03-10 12:05:14 +0800168#if defined(CONFIG_DM_USB) && defined(CONFIG_DM_REGULATOR)
169static int dwc_vbus_supply_init(struct udevice *dev)
170{
Christophe Kerello82e79752018-03-15 18:00:30 +0100171 struct dwc2_priv *priv = dev_get_priv(dev);
Kever Yang5c735362017-03-10 12:05:14 +0800172 int ret;
173
Christophe Kerello82e79752018-03-15 18:00:30 +0100174 ret = device_get_supply_regulator(dev, "vbus-supply",
175 &priv->vbus_supply);
Kever Yang5c735362017-03-10 12:05:14 +0800176 if (ret) {
177 debug("%s: No vbus supply\n", dev->name);
178 return 0;
179 }
180
Christophe Kerello82e79752018-03-15 18:00:30 +0100181 ret = regulator_set_enable(priv->vbus_supply, true);
Kever Yang5c735362017-03-10 12:05:14 +0800182 if (ret) {
Patrice Chotardac6c7962018-03-15 18:00:32 +0100183 dev_err(dev, "Error enabling vbus supply\n");
Kever Yang5c735362017-03-10 12:05:14 +0800184 return ret;
185 }
186
187 return 0;
188}
Christophe Kerello82e79752018-03-15 18:00:30 +0100189
190static int dwc_vbus_supply_exit(struct udevice *dev)
191{
192 struct dwc2_priv *priv = dev_get_priv(dev);
193 int ret;
194
195 if (priv->vbus_supply) {
196 ret = regulator_set_enable(priv->vbus_supply, false);
197 if (ret) {
198 dev_err(dev, "Error disabling vbus supply\n");
199 return ret;
200 }
201 }
202
203 return 0;
204}
Kever Yang5c735362017-03-10 12:05:14 +0800205#else
206static int dwc_vbus_supply_init(struct udevice *dev)
207{
208 return 0;
209}
Christophe Kerello82e79752018-03-15 18:00:30 +0100210
211#if defined(CONFIG_DM_USB)
212static int dwc_vbus_supply_exit(struct udevice *dev)
213{
214 return 0;
215}
216#endif
Kever Yang5c735362017-03-10 12:05:14 +0800217#endif
218
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700219/*
220 * This function initializes the DWC_otg controller registers for
221 * host mode.
222 *
223 * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
224 * request queues. Host channels are reset to ensure that they are ready for
225 * performing transfers.
226 *
Kever Yang5c735362017-03-10 12:05:14 +0800227 * @param dev USB Device (NULL if driver model is not being used)
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700228 * @param regs Programming view of DWC_otg controller
229 *
230 */
Kever Yang5c735362017-03-10 12:05:14 +0800231static void dwc_otg_core_host_init(struct udevice *dev,
232 struct dwc2_core_regs *regs)
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700233{
234 uint32_t nptxfifosize = 0;
235 uint32_t ptxfifosize = 0;
236 uint32_t hprt0 = 0;
237 int i, ret, num_channels;
238
239 /* Restart the Phy Clock */
240 writel(0, &regs->pcgcctl);
241
242 /* Initialize Host Configuration Register */
243 init_fslspclksel(regs);
244#ifdef CONFIG_DWC2_DFLT_SPEED_FULL
245 setbits_le32(&regs->host_regs.hcfg, DWC2_HCFG_FSLSSUPP);
246#endif
247
248 /* Configure data FIFO sizes */
249#ifdef CONFIG_DWC2_ENABLE_DYNAMIC_FIFO
250 if (readl(&regs->ghwcfg2) & DWC2_HWCFG2_DYNAMIC_FIFO) {
251 /* Rx FIFO */
252 writel(CONFIG_DWC2_HOST_RX_FIFO_SIZE, &regs->grxfsiz);
253
254 /* Non-periodic Tx FIFO */
255 nptxfifosize |= CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE <<
256 DWC2_FIFOSIZE_DEPTH_OFFSET;
257 nptxfifosize |= CONFIG_DWC2_HOST_RX_FIFO_SIZE <<
258 DWC2_FIFOSIZE_STARTADDR_OFFSET;
259 writel(nptxfifosize, &regs->gnptxfsiz);
260
261 /* Periodic Tx FIFO */
262 ptxfifosize |= CONFIG_DWC2_HOST_PERIO_TX_FIFO_SIZE <<
263 DWC2_FIFOSIZE_DEPTH_OFFSET;
264 ptxfifosize |= (CONFIG_DWC2_HOST_RX_FIFO_SIZE +
265 CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE) <<
266 DWC2_FIFOSIZE_STARTADDR_OFFSET;
267 writel(ptxfifosize, &regs->hptxfsiz);
268 }
269#endif
270
271 /* Clear Host Set HNP Enable in the OTG Control Register */
272 clrbits_le32(&regs->gotgctl, DWC2_GOTGCTL_HSTSETHNPEN);
273
274 /* Make sure the FIFOs are flushed. */
275 dwc_otg_flush_tx_fifo(regs, 0x10); /* All Tx FIFOs */
276 dwc_otg_flush_rx_fifo(regs);
277
278 /* Flush out any leftover queued requests. */
279 num_channels = readl(&regs->ghwcfg2);
280 num_channels &= DWC2_HWCFG2_NUM_HOST_CHAN_MASK;
281 num_channels >>= DWC2_HWCFG2_NUM_HOST_CHAN_OFFSET;
282 num_channels += 1;
283
284 for (i = 0; i < num_channels; i++)
285 clrsetbits_le32(&regs->hc_regs[i].hcchar,
286 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_EPDIR,
287 DWC2_HCCHAR_CHDIS);
288
289 /* Halt all channels to put them into a known state. */
290 for (i = 0; i < num_channels; i++) {
291 clrsetbits_le32(&regs->hc_regs[i].hcchar,
292 DWC2_HCCHAR_EPDIR,
293 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS);
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100294 ret = wait_for_bit_le32(&regs->hc_regs[i].hcchar,
295 DWC2_HCCHAR_CHEN, false, 1000, false);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700296 if (ret)
Patrice Chotardac6c7962018-03-15 18:00:32 +0100297 dev_info("%s: Timeout!\n", __func__);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700298 }
299
300 /* Turn on the vbus power. */
301 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST) {
302 hprt0 = readl(&regs->hprt0);
303 hprt0 &= ~(DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET);
304 hprt0 &= ~(DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG);
305 if (!(hprt0 & DWC2_HPRT0_PRTPWR)) {
306 hprt0 |= DWC2_HPRT0_PRTPWR;
307 writel(hprt0, &regs->hprt0);
308 }
309 }
Kever Yang5c735362017-03-10 12:05:14 +0800310
311 if (dev)
312 dwc_vbus_supply_init(dev);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700313}
314
315/*
316 * This function initializes the DWC_otg controller registers and
317 * prepares the core for device mode or host mode operation.
318 *
319 * @param regs Programming view of the DWC_otg controller
320 */
Marek Vasut55901982016-04-27 14:53:33 +0200321static void dwc_otg_core_init(struct dwc2_priv *priv)
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700322{
Marek Vasut55901982016-04-27 14:53:33 +0200323 struct dwc2_core_regs *regs = priv->regs;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700324 uint32_t ahbcfg = 0;
325 uint32_t usbcfg = 0;
326 uint8_t brst_sz = CONFIG_DWC2_DMA_BURST_SIZE;
327
328 /* Common Initialization */
329 usbcfg = readl(&regs->gusbcfg);
330
331 /* Program the ULPI External VBUS bit if needed */
Marek Vasut618da562016-04-27 14:55:57 +0200332 if (priv->ext_vbus) {
Marek Vasutb4fbd082016-04-27 14:58:49 +0200333 usbcfg |= DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
334 if (!priv->oc_disable) {
335 usbcfg |= DWC2_GUSBCFG_ULPI_INT_VBUS_INDICATOR |
336 DWC2_GUSBCFG_INDICATOR_PASSTHROUGH;
337 }
Marek Vasut618da562016-04-27 14:55:57 +0200338 } else {
339 usbcfg &= ~DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
340 }
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700341
342 /* Set external TS Dline pulsing */
343#ifdef CONFIG_DWC2_TS_DLINE
344 usbcfg |= DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
345#else
346 usbcfg &= ~DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
347#endif
348 writel(usbcfg, &regs->gusbcfg);
349
350 /* Reset the Controller */
351 dwc_otg_core_reset(regs);
352
353 /*
354 * This programming sequence needs to happen in FS mode before
355 * any other programming occurs
356 */
357#if defined(CONFIG_DWC2_DFLT_SPEED_FULL) && \
358 (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
359 /* If FS mode with FS PHY */
360 setbits_le32(&regs->gusbcfg, DWC2_GUSBCFG_PHYSEL);
361
362 /* Reset after a PHY select */
363 dwc_otg_core_reset(regs);
364
365 /*
366 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS.
367 * Also do this on HNP Dev/Host mode switches (done in dev_init
368 * and host_init).
369 */
370 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
371 init_fslspclksel(regs);
372
373#ifdef CONFIG_DWC2_I2C_ENABLE
374 /* Program GUSBCFG.OtgUtmifsSel to I2C */
375 setbits_le32(&regs->gusbcfg, DWC2_GUSBCFG_OTGUTMIFSSEL);
376
377 /* Program GI2CCTL.I2CEn */
378 clrsetbits_le32(&regs->gi2cctl, DWC2_GI2CCTL_I2CEN |
379 DWC2_GI2CCTL_I2CDEVADDR_MASK,
380 1 << DWC2_GI2CCTL_I2CDEVADDR_OFFSET);
381 setbits_le32(&regs->gi2cctl, DWC2_GI2CCTL_I2CEN);
382#endif
383
384#else
385 /* High speed PHY. */
386
387 /*
388 * HS PHY parameters. These parameters are preserved during
389 * soft reset so only program the first time. Do a soft reset
390 * immediately after setting phyif.
391 */
392 usbcfg &= ~(DWC2_GUSBCFG_ULPI_UTMI_SEL | DWC2_GUSBCFG_PHYIF);
393 usbcfg |= CONFIG_DWC2_PHY_TYPE << DWC2_GUSBCFG_ULPI_UTMI_SEL_OFFSET;
394
395 if (usbcfg & DWC2_GUSBCFG_ULPI_UTMI_SEL) { /* ULPI interface */
396#ifdef CONFIG_DWC2_PHY_ULPI_DDR
397 usbcfg |= DWC2_GUSBCFG_DDRSEL;
398#else
399 usbcfg &= ~DWC2_GUSBCFG_DDRSEL;
400#endif
401 } else { /* UTMI+ interface */
Alexey Brodkin163f8852018-01-31 17:56:59 +0300402#if (CONFIG_DWC2_UTMI_WIDTH == 16)
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700403 usbcfg |= DWC2_GUSBCFG_PHYIF;
404#endif
405 }
406
407 writel(usbcfg, &regs->gusbcfg);
408
409 /* Reset after setting the PHY parameters */
410 dwc_otg_core_reset(regs);
411#endif
412
413 usbcfg = readl(&regs->gusbcfg);
414 usbcfg &= ~(DWC2_GUSBCFG_ULPI_FSLS | DWC2_GUSBCFG_ULPI_CLK_SUS_M);
415#ifdef CONFIG_DWC2_ULPI_FS_LS
416 uint32_t hwcfg2 = readl(&regs->ghwcfg2);
417 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
418 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
419 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
420 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
421 if (hval == 2 && fval == 1) {
422 usbcfg |= DWC2_GUSBCFG_ULPI_FSLS;
423 usbcfg |= DWC2_GUSBCFG_ULPI_CLK_SUS_M;
424 }
425#endif
Meng Dongyangc65a3492017-06-08 15:34:20 +0800426 if (priv->hnp_srp_disable)
427 usbcfg |= DWC2_GUSBCFG_FORCEHOSTMODE;
428
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700429 writel(usbcfg, &regs->gusbcfg);
430
431 /* Program the GAHBCFG Register. */
432 switch (readl(&regs->ghwcfg2) & DWC2_HWCFG2_ARCHITECTURE_MASK) {
433 case DWC2_HWCFG2_ARCHITECTURE_SLAVE_ONLY:
434 break;
435 case DWC2_HWCFG2_ARCHITECTURE_EXT_DMA:
436 while (brst_sz > 1) {
437 ahbcfg |= ahbcfg + (1 << DWC2_GAHBCFG_HBURSTLEN_OFFSET);
438 ahbcfg &= DWC2_GAHBCFG_HBURSTLEN_MASK;
439 brst_sz >>= 1;
440 }
441
442#ifdef CONFIG_DWC2_DMA_ENABLE
443 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
444#endif
445 break;
446
447 case DWC2_HWCFG2_ARCHITECTURE_INT_DMA:
448 ahbcfg |= DWC2_GAHBCFG_HBURSTLEN_INCR4;
449#ifdef CONFIG_DWC2_DMA_ENABLE
450 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
451#endif
452 break;
453 }
454
455 writel(ahbcfg, &regs->gahbcfg);
456
Meng Dongyangc65a3492017-06-08 15:34:20 +0800457 /* Program the capabilities in GUSBCFG Register */
458 usbcfg = 0;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700459
Meng Dongyangc65a3492017-06-08 15:34:20 +0800460 if (!priv->hnp_srp_disable)
461 usbcfg |= DWC2_GUSBCFG_HNPCAP | DWC2_GUSBCFG_SRPCAP;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700462#ifdef CONFIG_DWC2_IC_USB_CAP
Meng Dongyangc65a3492017-06-08 15:34:20 +0800463 usbcfg |= DWC2_GUSBCFG_IC_USB_CAP;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700464#endif
Meng Dongyangc65a3492017-06-08 15:34:20 +0800465
466 setbits_le32(&regs->gusbcfg, usbcfg);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700467}
468
469/*
470 * Prepares a host channel for transferring packets to/from a specific
471 * endpoint. The HCCHARn register is set up with the characteristics specified
472 * in _hc. Host channel interrupts that may need to be serviced while this
473 * transfer is in progress are enabled.
474 *
475 * @param regs Programming view of DWC_otg controller
476 * @param hc Information needed to initialize the host channel
477 */
478static void dwc_otg_hc_init(struct dwc2_core_regs *regs, uint8_t hc_num,
Stephen Warrened9bcbc2015-04-10 21:05:21 -0600479 struct usb_device *dev, uint8_t dev_addr, uint8_t ep_num,
480 uint8_t ep_is_in, uint8_t ep_type, uint16_t max_packet)
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700481{
482 struct dwc2_hc_regs *hc_regs = &regs->hc_regs[hc_num];
Stephen Warrened9bcbc2015-04-10 21:05:21 -0600483 uint32_t hcchar = (dev_addr << DWC2_HCCHAR_DEVADDR_OFFSET) |
484 (ep_num << DWC2_HCCHAR_EPNUM_OFFSET) |
485 (ep_is_in << DWC2_HCCHAR_EPDIR_OFFSET) |
486 (ep_type << DWC2_HCCHAR_EPTYPE_OFFSET) |
487 (max_packet << DWC2_HCCHAR_MPS_OFFSET);
488
489 if (dev->speed == USB_SPEED_LOW)
490 hcchar |= DWC2_HCCHAR_LSPDDEV;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700491
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700492 /*
493 * Program the HCCHARn register with the endpoint characteristics
494 * for the current transfer.
495 */
496 writel(hcchar, &hc_regs->hcchar);
497
Stefan Brüns890f0ee2016-01-17 04:09:54 +0100498 /* Program the HCSPLIT register, default to no SPLIT */
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700499 writel(0, &hc_regs->hcsplt);
500}
501
Stefan Brüns890f0ee2016-01-17 04:09:54 +0100502static void dwc_otg_hc_init_split(struct dwc2_hc_regs *hc_regs,
503 uint8_t hub_devnum, uint8_t hub_port)
504{
505 uint32_t hcsplt = 0;
506
507 hcsplt = DWC2_HCSPLT_SPLTENA;
508 hcsplt |= hub_devnum << DWC2_HCSPLT_HUBADDR_OFFSET;
509 hcsplt |= hub_port << DWC2_HCSPLT_PRTADDR_OFFSET;
510
511 /* Program the HCSPLIT register for SPLITs */
512 writel(hcsplt, &hc_regs->hcsplt);
513}
514
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700515/*
516 * DWC2 to USB API interface
517 */
518/* Direction: In ; Request: Status */
Simon Glasscc3e3a92015-07-07 20:53:36 -0600519static int dwc_otg_submit_rh_msg_in_status(struct dwc2_core_regs *regs,
520 struct usb_device *dev, void *buffer,
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700521 int txlen, struct devrequest *cmd)
522{
523 uint32_t hprt0 = 0;
524 uint32_t port_status = 0;
525 uint32_t port_change = 0;
526 int len = 0;
527 int stat = 0;
528
529 switch (cmd->requesttype & ~USB_DIR_IN) {
530 case 0:
531 *(uint16_t *)buffer = cpu_to_le16(1);
532 len = 2;
533 break;
534 case USB_RECIP_INTERFACE:
535 case USB_RECIP_ENDPOINT:
536 *(uint16_t *)buffer = cpu_to_le16(0);
537 len = 2;
538 break;
539 case USB_TYPE_CLASS:
540 *(uint32_t *)buffer = cpu_to_le32(0);
541 len = 4;
542 break;
543 case USB_RECIP_OTHER | USB_TYPE_CLASS:
544 hprt0 = readl(&regs->hprt0);
545 if (hprt0 & DWC2_HPRT0_PRTCONNSTS)
546 port_status |= USB_PORT_STAT_CONNECTION;
547 if (hprt0 & DWC2_HPRT0_PRTENA)
548 port_status |= USB_PORT_STAT_ENABLE;
549 if (hprt0 & DWC2_HPRT0_PRTSUSP)
550 port_status |= USB_PORT_STAT_SUSPEND;
551 if (hprt0 & DWC2_HPRT0_PRTOVRCURRACT)
552 port_status |= USB_PORT_STAT_OVERCURRENT;
553 if (hprt0 & DWC2_HPRT0_PRTRST)
554 port_status |= USB_PORT_STAT_RESET;
555 if (hprt0 & DWC2_HPRT0_PRTPWR)
556 port_status |= USB_PORT_STAT_POWER;
557
Stephen Warren4748cce2015-03-27 21:55:38 -0600558 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) == DWC2_HPRT0_PRTSPD_LOW)
559 port_status |= USB_PORT_STAT_LOW_SPEED;
560 else if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
561 DWC2_HPRT0_PRTSPD_HIGH)
562 port_status |= USB_PORT_STAT_HIGH_SPEED;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700563
564 if (hprt0 & DWC2_HPRT0_PRTENCHNG)
565 port_change |= USB_PORT_STAT_C_ENABLE;
566 if (hprt0 & DWC2_HPRT0_PRTCONNDET)
567 port_change |= USB_PORT_STAT_C_CONNECTION;
568 if (hprt0 & DWC2_HPRT0_PRTOVRCURRCHNG)
569 port_change |= USB_PORT_STAT_C_OVERCURRENT;
570
571 *(uint32_t *)buffer = cpu_to_le32(port_status |
572 (port_change << 16));
573 len = 4;
574 break;
575 default:
576 puts("unsupported root hub command\n");
577 stat = USB_ST_STALLED;
578 }
579
580 dev->act_len = min(len, txlen);
581 dev->status = stat;
582
583 return stat;
584}
585
586/* Direction: In ; Request: Descriptor */
587static int dwc_otg_submit_rh_msg_in_descriptor(struct usb_device *dev,
588 void *buffer, int txlen,
589 struct devrequest *cmd)
590{
591 unsigned char data[32];
592 uint32_t dsc;
593 int len = 0;
594 int stat = 0;
595 uint16_t wValue = cpu_to_le16(cmd->value);
596 uint16_t wLength = cpu_to_le16(cmd->length);
597
598 switch (cmd->requesttype & ~USB_DIR_IN) {
599 case 0:
600 switch (wValue & 0xff00) {
601 case 0x0100: /* device descriptor */
Masahiro Yamadab4141192014-11-07 03:03:31 +0900602 len = min3(txlen, (int)sizeof(root_hub_dev_des), (int)wLength);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700603 memcpy(buffer, root_hub_dev_des, len);
604 break;
605 case 0x0200: /* configuration descriptor */
Masahiro Yamadab4141192014-11-07 03:03:31 +0900606 len = min3(txlen, (int)sizeof(root_hub_config_des), (int)wLength);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700607 memcpy(buffer, root_hub_config_des, len);
608 break;
609 case 0x0300: /* string descriptors */
610 switch (wValue & 0xff) {
611 case 0x00:
Masahiro Yamadab4141192014-11-07 03:03:31 +0900612 len = min3(txlen, (int)sizeof(root_hub_str_index0),
613 (int)wLength);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700614 memcpy(buffer, root_hub_str_index0, len);
615 break;
616 case 0x01:
Masahiro Yamadab4141192014-11-07 03:03:31 +0900617 len = min3(txlen, (int)sizeof(root_hub_str_index1),
618 (int)wLength);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700619 memcpy(buffer, root_hub_str_index1, len);
620 break;
621 }
622 break;
623 default:
624 stat = USB_ST_STALLED;
625 }
626 break;
627
628 case USB_TYPE_CLASS:
629 /* Root port config, set 1 port and nothing else. */
630 dsc = 0x00000001;
631
632 data[0] = 9; /* min length; */
633 data[1] = 0x29;
634 data[2] = dsc & RH_A_NDP;
635 data[3] = 0;
636 if (dsc & RH_A_PSM)
637 data[3] |= 0x1;
638 if (dsc & RH_A_NOCP)
639 data[3] |= 0x10;
640 else if (dsc & RH_A_OCPM)
641 data[3] |= 0x8;
642
643 /* corresponds to data[4-7] */
644 data[5] = (dsc & RH_A_POTPGT) >> 24;
645 data[7] = dsc & RH_B_DR;
646 if (data[2] < 7) {
647 data[8] = 0xff;
648 } else {
649 data[0] += 2;
650 data[8] = (dsc & RH_B_DR) >> 8;
651 data[9] = 0xff;
652 data[10] = data[9];
653 }
654
Masahiro Yamadab4141192014-11-07 03:03:31 +0900655 len = min3(txlen, (int)data[0], (int)wLength);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700656 memcpy(buffer, data, len);
657 break;
658 default:
659 puts("unsupported root hub command\n");
660 stat = USB_ST_STALLED;
661 }
662
663 dev->act_len = min(len, txlen);
664 dev->status = stat;
665
666 return stat;
667}
668
669/* Direction: In ; Request: Configuration */
670static int dwc_otg_submit_rh_msg_in_configuration(struct usb_device *dev,
671 void *buffer, int txlen,
672 struct devrequest *cmd)
673{
674 int len = 0;
675 int stat = 0;
676
677 switch (cmd->requesttype & ~USB_DIR_IN) {
678 case 0:
679 *(uint8_t *)buffer = 0x01;
680 len = 1;
681 break;
682 default:
683 puts("unsupported root hub command\n");
684 stat = USB_ST_STALLED;
685 }
686
687 dev->act_len = min(len, txlen);
688 dev->status = stat;
689
690 return stat;
691}
692
693/* Direction: In */
Simon Glasscc3e3a92015-07-07 20:53:36 -0600694static int dwc_otg_submit_rh_msg_in(struct dwc2_priv *priv,
695 struct usb_device *dev, void *buffer,
696 int txlen, struct devrequest *cmd)
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700697{
698 switch (cmd->request) {
699 case USB_REQ_GET_STATUS:
Simon Glasscc3e3a92015-07-07 20:53:36 -0600700 return dwc_otg_submit_rh_msg_in_status(priv->regs, dev, buffer,
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700701 txlen, cmd);
702 case USB_REQ_GET_DESCRIPTOR:
703 return dwc_otg_submit_rh_msg_in_descriptor(dev, buffer,
704 txlen, cmd);
705 case USB_REQ_GET_CONFIGURATION:
706 return dwc_otg_submit_rh_msg_in_configuration(dev, buffer,
707 txlen, cmd);
708 default:
709 puts("unsupported root hub command\n");
710 return USB_ST_STALLED;
711 }
712}
713
714/* Direction: Out */
Simon Glasscc3e3a92015-07-07 20:53:36 -0600715static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
716 struct usb_device *dev,
717 void *buffer, int txlen,
718 struct devrequest *cmd)
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700719{
Simon Glasscc3e3a92015-07-07 20:53:36 -0600720 struct dwc2_core_regs *regs = priv->regs;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700721 int len = 0;
722 int stat = 0;
723 uint16_t bmrtype_breq = cmd->requesttype | (cmd->request << 8);
724 uint16_t wValue = cpu_to_le16(cmd->value);
725
726 switch (bmrtype_breq & ~USB_DIR_IN) {
727 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_ENDPOINT:
728 case (USB_REQ_CLEAR_FEATURE << 8) | USB_TYPE_CLASS:
729 break;
730
731 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
732 switch (wValue) {
733 case USB_PORT_FEAT_C_CONNECTION:
734 setbits_le32(&regs->hprt0, DWC2_HPRT0_PRTCONNDET);
735 break;
736 }
737 break;
738
739 case (USB_REQ_SET_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
740 switch (wValue) {
741 case USB_PORT_FEAT_SUSPEND:
742 break;
743
744 case USB_PORT_FEAT_RESET:
745 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
746 DWC2_HPRT0_PRTCONNDET |
747 DWC2_HPRT0_PRTENCHNG |
748 DWC2_HPRT0_PRTOVRCURRCHNG,
749 DWC2_HPRT0_PRTRST);
750 mdelay(50);
751 clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTRST);
752 break;
753
754 case USB_PORT_FEAT_POWER:
755 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
756 DWC2_HPRT0_PRTCONNDET |
757 DWC2_HPRT0_PRTENCHNG |
758 DWC2_HPRT0_PRTOVRCURRCHNG,
759 DWC2_HPRT0_PRTRST);
760 break;
761
762 case USB_PORT_FEAT_ENABLE:
763 break;
764 }
765 break;
766 case (USB_REQ_SET_ADDRESS << 8):
Simon Glasscc3e3a92015-07-07 20:53:36 -0600767 priv->root_hub_devnum = wValue;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700768 break;
769 case (USB_REQ_SET_CONFIGURATION << 8):
770 break;
771 default:
772 puts("unsupported root hub command\n");
773 stat = USB_ST_STALLED;
774 }
775
776 len = min(len, txlen);
777
778 dev->act_len = len;
779 dev->status = stat;
780
781 return stat;
782}
783
Simon Glasscc3e3a92015-07-07 20:53:36 -0600784static int dwc_otg_submit_rh_msg(struct dwc2_priv *priv, struct usb_device *dev,
785 unsigned long pipe, void *buffer, int txlen,
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700786 struct devrequest *cmd)
787{
788 int stat = 0;
789
790 if (usb_pipeint(pipe)) {
791 puts("Root-Hub submit IRQ: NOT implemented\n");
792 return 0;
793 }
794
795 if (cmd->requesttype & USB_DIR_IN)
Simon Glasscc3e3a92015-07-07 20:53:36 -0600796 stat = dwc_otg_submit_rh_msg_in(priv, dev, buffer, txlen, cmd);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700797 else
Simon Glasscc3e3a92015-07-07 20:53:36 -0600798 stat = dwc_otg_submit_rh_msg_out(priv, dev, buffer, txlen, cmd);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700799
800 mdelay(1);
801
802 return stat;
803}
804
Stefan Brüns25612f22016-01-23 01:42:25 +0100805int wait_for_chhltd(struct dwc2_hc_regs *hc_regs, uint32_t *sub, u8 *toggle)
Stephen Warren4a1d21f2015-03-07 22:48:51 -0700806{
Stephen Warren4a1d21f2015-03-07 22:48:51 -0700807 int ret;
808 uint32_t hcint, hctsiz;
809
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100810 ret = wait_for_bit_le32(&hc_regs->hcint, DWC2_HCINT_CHHLTD, true,
Christophe Kerelloc2e4c862018-03-15 18:00:31 +0100811 2000, false);
Stephen Warren4a1d21f2015-03-07 22:48:51 -0700812 if (ret)
813 return ret;
814
815 hcint = readl(&hc_regs->hcint);
Stephen Warren4a1d21f2015-03-07 22:48:51 -0700816 hctsiz = readl(&hc_regs->hctsiz);
817 *sub = (hctsiz & DWC2_HCTSIZ_XFERSIZE_MASK) >>
818 DWC2_HCTSIZ_XFERSIZE_OFFSET;
Stephen Warren66ffc872015-03-07 22:48:55 -0700819 *toggle = (hctsiz & DWC2_HCTSIZ_PID_MASK) >> DWC2_HCTSIZ_PID_OFFSET;
Stephen Warren4a1d21f2015-03-07 22:48:51 -0700820
Stefan Brüns03460cd2016-01-17 04:09:52 +0100821 debug("%s: HCINT=%08x sub=%u toggle=%d\n", __func__, hcint, *sub,
822 *toggle);
Stephen Warren4a1d21f2015-03-07 22:48:51 -0700823
Stefan Brüns03460cd2016-01-17 04:09:52 +0100824 if (hcint & DWC2_HCINT_XFERCOMP)
825 return 0;
826
827 if (hcint & (DWC2_HCINT_NAK | DWC2_HCINT_FRMOVRUN))
828 return -EAGAIN;
829
830 debug("%s: Error (HCINT=%08x)\n", __func__, hcint);
831 return -EINVAL;
Stephen Warren4a1d21f2015-03-07 22:48:51 -0700832}
833
Stephen Warren7b5e5042015-03-07 22:48:52 -0700834static int dwc2_eptype[] = {
835 DWC2_HCCHAR_EPTYPE_ISOC,
836 DWC2_HCCHAR_EPTYPE_INTR,
837 DWC2_HCCHAR_EPTYPE_CONTROL,
838 DWC2_HCCHAR_EPTYPE_BULK,
839};
840
Stefan Brünsdaed3052016-01-17 04:09:53 +0100841static int transfer_chunk(struct dwc2_hc_regs *hc_regs, void *aligned_buffer,
Stefan Brüns25612f22016-01-23 01:42:25 +0100842 u8 *pid, int in, void *buffer, int num_packets,
Stefan Brünsd2ff51b2016-01-17 04:09:56 +0100843 int xfer_len, int *actual_len, int odd_frame)
Stefan Brünsdaed3052016-01-17 04:09:53 +0100844{
845 int ret = 0;
846 uint32_t sub;
847
848 debug("%s: chunk: pid %d xfer_len %u pkts %u\n", __func__,
849 *pid, xfer_len, num_packets);
850
851 writel((xfer_len << DWC2_HCTSIZ_XFERSIZE_OFFSET) |
852 (num_packets << DWC2_HCTSIZ_PKTCNT_OFFSET) |
853 (*pid << DWC2_HCTSIZ_PID_OFFSET),
854 &hc_regs->hctsiz);
855
Eddie Cai57ca63b2017-04-06 11:37:04 +0800856 if (xfer_len) {
857 if (in) {
858 invalidate_dcache_range(
859 (uintptr_t)aligned_buffer,
860 (uintptr_t)aligned_buffer +
861 roundup(xfer_len, ARCH_DMA_MINALIGN));
862 } else {
863 memcpy(aligned_buffer, buffer, xfer_len);
864 flush_dcache_range(
865 (uintptr_t)aligned_buffer,
866 (uintptr_t)aligned_buffer +
867 roundup(xfer_len, ARCH_DMA_MINALIGN));
868 }
Stefan Brünsdaed3052016-01-17 04:09:53 +0100869 }
870
871 writel(phys_to_bus((unsigned long)aligned_buffer), &hc_regs->hcdma);
872
873 /* Clear old interrupt conditions for this host channel. */
874 writel(0x3fff, &hc_regs->hcint);
875
876 /* Set host channel enable after all other setup is complete. */
877 clrsetbits_le32(&hc_regs->hcchar, DWC2_HCCHAR_MULTICNT_MASK |
Stefan Brünsd2ff51b2016-01-17 04:09:56 +0100878 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS |
879 DWC2_HCCHAR_ODDFRM,
Stefan Brünsdaed3052016-01-17 04:09:53 +0100880 (1 << DWC2_HCCHAR_MULTICNT_OFFSET) |
Stefan Brünsd2ff51b2016-01-17 04:09:56 +0100881 (odd_frame << DWC2_HCCHAR_ODDFRM_OFFSET) |
Stefan Brünsdaed3052016-01-17 04:09:53 +0100882 DWC2_HCCHAR_CHEN);
883
884 ret = wait_for_chhltd(hc_regs, &sub, pid);
885 if (ret < 0)
886 return ret;
887
888 if (in) {
889 xfer_len -= sub;
890
891 invalidate_dcache_range((unsigned long)aligned_buffer,
892 (unsigned long)aligned_buffer +
893 roundup(xfer_len, ARCH_DMA_MINALIGN));
894
895 memcpy(buffer, aligned_buffer, xfer_len);
896 }
897 *actual_len = xfer_len;
898
899 return ret;
900}
901
Simon Glasscc3e3a92015-07-07 20:53:36 -0600902int chunk_msg(struct dwc2_priv *priv, struct usb_device *dev,
Stefan Brüns25612f22016-01-23 01:42:25 +0100903 unsigned long pipe, u8 *pid, int in, void *buffer, int len)
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700904{
Simon Glasscc3e3a92015-07-07 20:53:36 -0600905 struct dwc2_core_regs *regs = priv->regs;
Stephen Warren7b5e5042015-03-07 22:48:52 -0700906 struct dwc2_hc_regs *hc_regs = &regs->hc_regs[DWC2_HC_CHANNEL];
Stefan Brünsd2ff51b2016-01-17 04:09:56 +0100907 struct dwc2_host_regs *host_regs = &regs->host_regs;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700908 int devnum = usb_pipedevice(pipe);
909 int ep = usb_pipeendpoint(pipe);
910 int max = usb_maxpacket(dev, pipe);
Stephen Warren7b5e5042015-03-07 22:48:52 -0700911 int eptype = dwc2_eptype[usb_pipetype(pipe)];
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700912 int done = 0;
Stephen Warren5877de92015-04-11 21:52:02 -0600913 int ret = 0;
Stefan Brünsb54e4472016-01-17 04:09:55 +0100914 int do_split = 0;
915 int complete_split = 0;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700916 uint32_t xfer_len;
917 uint32_t num_packets;
918 int stop_transfer = 0;
Stefan Brüns56a7bbd2016-01-17 04:09:51 +0100919 uint32_t max_xfer_len;
Stefan Brünsd2ff51b2016-01-17 04:09:56 +0100920 int ssplit_frame_num = 0;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700921
Stephen Warren7b5e5042015-03-07 22:48:52 -0700922 debug("%s: msg: pipe %lx pid %d in %d len %d\n", __func__, pipe, *pid,
923 in, len);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700924
Stefan Brüns56a7bbd2016-01-17 04:09:51 +0100925 max_xfer_len = CONFIG_DWC2_MAX_PACKET_COUNT * max;
926 if (max_xfer_len > CONFIG_DWC2_MAX_TRANSFER_SIZE)
927 max_xfer_len = CONFIG_DWC2_MAX_TRANSFER_SIZE;
928 if (max_xfer_len > DWC2_DATA_BUF_SIZE)
929 max_xfer_len = DWC2_DATA_BUF_SIZE;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700930
Stefan Brüns56a7bbd2016-01-17 04:09:51 +0100931 /* Make sure that max_xfer_len is a multiple of max packet size. */
932 num_packets = max_xfer_len / max;
933 max_xfer_len = num_packets * max;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700934
Stefan Brünsdaed3052016-01-17 04:09:53 +0100935 /* Initialize channel */
936 dwc_otg_hc_init(regs, DWC2_HC_CHANNEL, dev, devnum, ep, in,
937 eptype, max);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700938
Stefan Brünsb54e4472016-01-17 04:09:55 +0100939 /* Check if the target is a FS/LS device behind a HS hub */
940 if (dev->speed != USB_SPEED_HIGH) {
941 uint8_t hub_addr;
942 uint8_t hub_port;
943 uint32_t hprt0 = readl(&regs->hprt0);
944 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
945 DWC2_HPRT0_PRTSPD_HIGH) {
946 usb_find_usb2_hub_address_port(dev, &hub_addr,
947 &hub_port);
948 dwc_otg_hc_init_split(hc_regs, hub_addr, hub_port);
949
950 do_split = 1;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700951 num_packets = 1;
Stefan Brünsb54e4472016-01-17 04:09:55 +0100952 max_xfer_len = max;
953 }
954 }
955
Stefan Brünsdaed3052016-01-17 04:09:53 +0100956 do {
957 int actual_len = 0;
Stefan Brünsb54e4472016-01-17 04:09:55 +0100958 uint32_t hcint;
Stefan Brünsd2ff51b2016-01-17 04:09:56 +0100959 int odd_frame = 0;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700960 xfer_len = len - done;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700961
Stefan Brüns56a7bbd2016-01-17 04:09:51 +0100962 if (xfer_len > max_xfer_len)
963 xfer_len = max_xfer_len;
964 else if (xfer_len > max)
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700965 num_packets = (xfer_len + max - 1) / max;
Stefan Brüns56a7bbd2016-01-17 04:09:51 +0100966 else
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700967 num_packets = 1;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700968
Stefan Brünsb54e4472016-01-17 04:09:55 +0100969 if (complete_split)
970 setbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
971 else if (do_split)
972 clrbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
973
Stefan Brünsd2ff51b2016-01-17 04:09:56 +0100974 if (eptype == DWC2_HCCHAR_EPTYPE_INTR) {
975 int uframe_num = readl(&host_regs->hfnum);
976 if (!(uframe_num & 0x1))
977 odd_frame = 1;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700978 }
979
Stefan Brünsdaed3052016-01-17 04:09:53 +0100980 ret = transfer_chunk(hc_regs, priv->aligned_buffer, pid,
981 in, (char *)buffer + done, num_packets,
Stefan Brünsd2ff51b2016-01-17 04:09:56 +0100982 xfer_len, &actual_len, odd_frame);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -0700983
Stefan Brünsb54e4472016-01-17 04:09:55 +0100984 hcint = readl(&hc_regs->hcint);
985 if (complete_split) {
986 stop_transfer = 0;
Stefan Brünsd2ff51b2016-01-17 04:09:56 +0100987 if (hcint & DWC2_HCINT_NYET) {
Stefan Brünsb54e4472016-01-17 04:09:55 +0100988 ret = 0;
Stefan Brünsd2ff51b2016-01-17 04:09:56 +0100989 int frame_num = DWC2_HFNUM_MAX_FRNUM &
990 readl(&host_regs->hfnum);
991 if (((frame_num - ssplit_frame_num) &
992 DWC2_HFNUM_MAX_FRNUM) > 4)
993 ret = -EAGAIN;
994 } else
Stefan Brünsb54e4472016-01-17 04:09:55 +0100995 complete_split = 0;
996 } else if (do_split) {
997 if (hcint & DWC2_HCINT_ACK) {
Stefan Brünsd2ff51b2016-01-17 04:09:56 +0100998 ssplit_frame_num = DWC2_HFNUM_MAX_FRNUM &
999 readl(&host_regs->hfnum);
Stefan Brünsb54e4472016-01-17 04:09:55 +01001000 ret = 0;
1001 complete_split = 1;
1002 }
Simon Glasscc3e3a92015-07-07 20:53:36 -06001003 }
Stephen Warrend1c880c2015-03-08 11:08:13 -06001004
Stephen Warren5877de92015-04-11 21:52:02 -06001005 if (ret)
Stephen Warren4a1d21f2015-03-07 22:48:51 -07001006 break;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001007
Stefan Brünsdaed3052016-01-17 04:09:53 +01001008 if (actual_len < xfer_len)
1009 stop_transfer = 1;
Alexander Steindb402e02015-07-24 09:22:14 +02001010
Stefan Brünsdaed3052016-01-17 04:09:53 +01001011 done += actual_len;
Alexander Steindb402e02015-07-24 09:22:14 +02001012
Stefan Brünsb54e4472016-01-17 04:09:55 +01001013 /* Transactions are done when when either all data is transferred or
1014 * there is a short transfer. In case of a SPLIT make sure the CSPLIT
1015 * is executed.
1016 */
1017 } while (((done < len) && !stop_transfer) || complete_split);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001018
1019 writel(0, &hc_regs->hcintmsk);
1020 writel(0xFFFFFFFF, &hc_regs->hcint);
1021
1022 dev->status = 0;
1023 dev->act_len = done;
1024
Stephen Warren5877de92015-04-11 21:52:02 -06001025 return ret;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001026}
1027
Stephen Warren7b5e5042015-03-07 22:48:52 -07001028/* U-Boot USB transmission interface */
Simon Glasscc3e3a92015-07-07 20:53:36 -06001029int _submit_bulk_msg(struct dwc2_priv *priv, struct usb_device *dev,
1030 unsigned long pipe, void *buffer, int len)
Stephen Warren7b5e5042015-03-07 22:48:52 -07001031{
1032 int devnum = usb_pipedevice(pipe);
1033 int ep = usb_pipeendpoint(pipe);
Stefan Brüns25612f22016-01-23 01:42:25 +01001034 u8* pid;
Stephen Warren7b5e5042015-03-07 22:48:52 -07001035
Stefan Brüns25612f22016-01-23 01:42:25 +01001036 if ((devnum >= MAX_DEVICE) || (devnum == priv->root_hub_devnum)) {
Stephen Warren7b5e5042015-03-07 22:48:52 -07001037 dev->status = 0;
1038 return -EINVAL;
1039 }
1040
Stefan Brüns25612f22016-01-23 01:42:25 +01001041 if (usb_pipein(pipe))
1042 pid = &priv->in_data_toggle[devnum][ep];
1043 else
1044 pid = &priv->out_data_toggle[devnum][ep];
1045
1046 return chunk_msg(priv, dev, pipe, pid, usb_pipein(pipe), buffer, len);
Stephen Warren7b5e5042015-03-07 22:48:52 -07001047}
1048
Simon Glasscc3e3a92015-07-07 20:53:36 -06001049static int _submit_control_msg(struct dwc2_priv *priv, struct usb_device *dev,
1050 unsigned long pipe, void *buffer, int len,
1051 struct devrequest *setup)
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001052{
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001053 int devnum = usb_pipedevice(pipe);
Stefan Brüns25612f22016-01-23 01:42:25 +01001054 int ret, act_len;
1055 u8 pid;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001056 /* For CONTROL endpoint pid should start with DATA1 */
1057 int status_direction;
1058
Simon Glasscc3e3a92015-07-07 20:53:36 -06001059 if (devnum == priv->root_hub_devnum) {
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001060 dev->status = 0;
1061 dev->speed = USB_SPEED_HIGH;
Simon Glasscc3e3a92015-07-07 20:53:36 -06001062 return dwc_otg_submit_rh_msg(priv, dev, pipe, buffer, len,
1063 setup);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001064 }
1065
Stefan Brünsb54e4472016-01-17 04:09:55 +01001066 /* SETUP stage */
Stephen Warrenee837552015-03-07 22:48:53 -07001067 pid = DWC2_HC_PID_SETUP;
Stefan Brünsb54e4472016-01-17 04:09:55 +01001068 do {
1069 ret = chunk_msg(priv, dev, pipe, &pid, 0, setup, 8);
1070 } while (ret == -EAGAIN);
Stephen Warrenee837552015-03-07 22:48:53 -07001071 if (ret)
1072 return ret;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001073
Stefan Brünsb54e4472016-01-17 04:09:55 +01001074 /* DATA stage */
1075 act_len = 0;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001076 if (buffer) {
Stephen Warren282685e2015-03-07 22:48:54 -07001077 pid = DWC2_HC_PID_DATA1;
Stefan Brünsb54e4472016-01-17 04:09:55 +01001078 do {
1079 ret = chunk_msg(priv, dev, pipe, &pid, usb_pipein(pipe),
1080 buffer, len);
1081 act_len += dev->act_len;
1082 buffer += dev->act_len;
1083 len -= dev->act_len;
1084 } while (ret == -EAGAIN);
Stephen Warrenee837552015-03-07 22:48:53 -07001085 if (ret)
1086 return ret;
Stefan Brünsb54e4472016-01-17 04:09:55 +01001087 status_direction = usb_pipeout(pipe);
1088 } else {
1089 /* No-data CONTROL always ends with an IN transaction */
1090 status_direction = 1;
1091 }
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001092
1093 /* STATUS stage */
Stephen Warrenee837552015-03-07 22:48:53 -07001094 pid = DWC2_HC_PID_DATA1;
Stefan Brünsb54e4472016-01-17 04:09:55 +01001095 do {
1096 ret = chunk_msg(priv, dev, pipe, &pid, status_direction,
1097 priv->status_buffer, 0);
1098 } while (ret == -EAGAIN);
Stephen Warrenee837552015-03-07 22:48:53 -07001099 if (ret)
1100 return ret;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001101
Stephen Warrenee837552015-03-07 22:48:53 -07001102 dev->act_len = act_len;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001103
Stephen Warren4a1d21f2015-03-07 22:48:51 -07001104 return 0;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001105}
1106
Simon Glasscc3e3a92015-07-07 20:53:36 -06001107int _submit_int_msg(struct dwc2_priv *priv, struct usb_device *dev,
1108 unsigned long pipe, void *buffer, int len, int interval)
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001109{
Stephen Warren5877de92015-04-11 21:52:02 -06001110 unsigned long timeout;
1111 int ret;
1112
Stephen Warrene2365192015-04-10 21:05:22 -06001113 /* FIXME: what is interval? */
Stephen Warren5877de92015-04-11 21:52:02 -06001114
1115 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1116 for (;;) {
1117 if (get_timer(0) > timeout) {
Patrice Chotardac6c7962018-03-15 18:00:32 +01001118 dev_err(dev, "Timeout poll on interrupt endpoint\n");
Stephen Warren5877de92015-04-11 21:52:02 -06001119 return -ETIMEDOUT;
1120 }
Simon Glasscc3e3a92015-07-07 20:53:36 -06001121 ret = _submit_bulk_msg(priv, dev, pipe, buffer, len);
Stephen Warren5877de92015-04-11 21:52:02 -06001122 if (ret != -EAGAIN)
1123 return ret;
1124 }
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001125}
1126
Kever Yang5c735362017-03-10 12:05:14 +08001127static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001128{
Simon Glasscc3e3a92015-07-07 20:53:36 -06001129 struct dwc2_core_regs *regs = priv->regs;
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001130 uint32_t snpsid;
1131 int i, j;
1132
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001133 snpsid = readl(&regs->gsnpsid);
Patrice Chotardac6c7962018-03-15 18:00:32 +01001134 dev_info(dev, "Core Release: %x.%03x\n",
1135 snpsid >> 12 & 0xf, snpsid & 0xfff);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001136
Peter Griffin5cfd6c02015-05-12 14:38:27 +01001137 if ((snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_2xx &&
1138 (snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_3xx) {
Patrice Chotardac6c7962018-03-15 18:00:32 +01001139 dev_info(dev, "SNPSID invalid (not DWC2 OTG device): %08x\n",
1140 snpsid);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001141 return -ENODEV;
1142 }
1143
Marek Vasut618da562016-04-27 14:55:57 +02001144#ifdef CONFIG_DWC2_PHY_ULPI_EXT_VBUS
1145 priv->ext_vbus = 1;
1146#else
1147 priv->ext_vbus = 0;
1148#endif
1149
Marek Vasut55901982016-04-27 14:53:33 +02001150 dwc_otg_core_init(priv);
Kever Yang5c735362017-03-10 12:05:14 +08001151 dwc_otg_core_host_init(dev, regs);
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001152
1153 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
1154 DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
1155 DWC2_HPRT0_PRTOVRCURRCHNG,
1156 DWC2_HPRT0_PRTRST);
1157 mdelay(50);
1158 clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET |
1159 DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG |
1160 DWC2_HPRT0_PRTRST);
1161
1162 for (i = 0; i < MAX_DEVICE; i++) {
Stefan Brüns25612f22016-01-23 01:42:25 +01001163 for (j = 0; j < MAX_ENDPOINT; j++) {
1164 priv->in_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1165 priv->out_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1166 }
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001167 }
1168
Stefan Roese2bf352f2016-05-06 13:53:37 +02001169 /*
1170 * Add a 1 second delay here. This gives the host controller
1171 * a bit time before the comminucation with the USB devices
1172 * is started (the bus is scanned) and fixes the USB detection
1173 * problems with some problematic USB keys.
1174 */
1175 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
1176 mdelay(1000);
1177
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001178 return 0;
1179}
1180
Simon Glasscc3e3a92015-07-07 20:53:36 -06001181static void dwc2_uninit_common(struct dwc2_core_regs *regs)
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001182{
1183 /* Put everything in reset. */
1184 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
1185 DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
1186 DWC2_HPRT0_PRTOVRCURRCHNG,
1187 DWC2_HPRT0_PRTRST);
Simon Glasscc3e3a92015-07-07 20:53:36 -06001188}
1189
Simon Glassf58a41e2015-07-07 20:53:37 -06001190#ifndef CONFIG_DM_USB
Simon Glasscc3e3a92015-07-07 20:53:36 -06001191int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1192 int len, struct devrequest *setup)
1193{
1194 return _submit_control_msg(&local, dev, pipe, buffer, len, setup);
1195}
1196
1197int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1198 int len)
1199{
1200 return _submit_bulk_msg(&local, dev, pipe, buffer, len);
1201}
1202
1203int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1204 int len, int interval)
1205{
1206 return _submit_int_msg(&local, dev, pipe, buffer, len, interval);
1207}
1208
1209/* U-Boot USB control interface */
1210int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1211{
1212 struct dwc2_priv *priv = &local;
1213
1214 memset(priv, '\0', sizeof(*priv));
1215 priv->root_hub_devnum = 0;
1216 priv->regs = (struct dwc2_core_regs *)CONFIG_USB_DWC2_REG_ADDR;
1217 priv->aligned_buffer = aligned_buffer_addr;
1218 priv->status_buffer = status_buffer_addr;
1219
1220 /* board-dependant init */
1221 if (board_usb_init(index, USB_INIT_HOST))
1222 return -1;
1223
Kever Yang5c735362017-03-10 12:05:14 +08001224 return dwc2_init_common(NULL, priv);
Simon Glasscc3e3a92015-07-07 20:53:36 -06001225}
1226
1227int usb_lowlevel_stop(int index)
1228{
1229 dwc2_uninit_common(local.regs);
1230
Oleksandr Tymoshenko6e9e0622014-02-01 21:51:25 -07001231 return 0;
1232}
Simon Glassf58a41e2015-07-07 20:53:37 -06001233#endif
1234
1235#ifdef CONFIG_DM_USB
1236static int dwc2_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1237 unsigned long pipe, void *buffer, int length,
1238 struct devrequest *setup)
1239{
1240 struct dwc2_priv *priv = dev_get_priv(dev);
1241
1242 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1243 dev->name, udev, udev->dev->name, udev->portnr);
1244
1245 return _submit_control_msg(priv, udev, pipe, buffer, length, setup);
1246}
1247
1248static int dwc2_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1249 unsigned long pipe, void *buffer, int length)
1250{
1251 struct dwc2_priv *priv = dev_get_priv(dev);
1252
1253 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1254
1255 return _submit_bulk_msg(priv, udev, pipe, buffer, length);
1256}
1257
1258static int dwc2_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1259 unsigned long pipe, void *buffer, int length,
1260 int interval)
1261{
1262 struct dwc2_priv *priv = dev_get_priv(dev);
1263
1264 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1265
1266 return _submit_int_msg(priv, udev, pipe, buffer, length, interval);
1267}
1268
1269static int dwc2_usb_ofdata_to_platdata(struct udevice *dev)
1270{
1271 struct dwc2_priv *priv = dev_get_priv(dev);
1272 fdt_addr_t addr;
1273
Philipp Tomsicha9d30372017-09-12 17:32:27 +02001274 addr = dev_read_addr(dev);
Simon Glassf58a41e2015-07-07 20:53:37 -06001275 if (addr == FDT_ADDR_T_NONE)
1276 return -EINVAL;
1277 priv->regs = (struct dwc2_core_regs *)addr;
1278
Meng Dongyangdd22bac2017-06-28 19:22:43 +08001279 priv->oc_disable = dev_read_bool(dev, "disable-over-current");
1280 priv->hnp_srp_disable = dev_read_bool(dev, "hnp-srp-disable");
Meng Dongyangc65a3492017-06-08 15:34:20 +08001281
Simon Glassf58a41e2015-07-07 20:53:37 -06001282 return 0;
1283}
1284
1285static int dwc2_usb_probe(struct udevice *dev)
1286{
1287 struct dwc2_priv *priv = dev_get_priv(dev);
Marek Vasute96e0642016-04-26 03:02:35 +02001288 struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
1289
1290 bus_priv->desc_before_addr = true;
Simon Glassf58a41e2015-07-07 20:53:37 -06001291
Kever Yang5c735362017-03-10 12:05:14 +08001292 return dwc2_init_common(dev, priv);
Simon Glassf58a41e2015-07-07 20:53:37 -06001293}
1294
1295static int dwc2_usb_remove(struct udevice *dev)
1296{
1297 struct dwc2_priv *priv = dev_get_priv(dev);
Christophe Kerello82e79752018-03-15 18:00:30 +01001298 int ret;
1299
1300 ret = dwc_vbus_supply_exit(dev);
1301 if (ret)
1302 return ret;
Simon Glassf58a41e2015-07-07 20:53:37 -06001303
1304 dwc2_uninit_common(priv->regs);
1305
1306 return 0;
1307}
1308
1309struct dm_usb_ops dwc2_usb_ops = {
1310 .control = dwc2_submit_control_msg,
1311 .bulk = dwc2_submit_bulk_msg,
1312 .interrupt = dwc2_submit_int_msg,
1313};
1314
1315static const struct udevice_id dwc2_usb_ids[] = {
1316 { .compatible = "brcm,bcm2835-usb" },
Emmanuel Vadotff5d5cc2018-07-02 14:34:23 +02001317 { .compatible = "brcm,bcm2708-usb" },
Marek Vasutf522f942015-08-12 22:19:14 +02001318 { .compatible = "snps,dwc2" },
Simon Glassf58a41e2015-07-07 20:53:37 -06001319 { }
1320};
1321
1322U_BOOT_DRIVER(usb_dwc2) = {
Marek Vasut7a1386f2015-08-12 22:19:15 +02001323 .name = "dwc2_usb",
Simon Glassf58a41e2015-07-07 20:53:37 -06001324 .id = UCLASS_USB,
1325 .of_match = dwc2_usb_ids,
1326 .ofdata_to_platdata = dwc2_usb_ofdata_to_platdata,
1327 .probe = dwc2_usb_probe,
1328 .remove = dwc2_usb_remove,
1329 .ops = &dwc2_usb_ops,
1330 .priv_auto_alloc_size = sizeof(struct dwc2_priv),
1331 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1332};
1333#endif