]> Git Repo - J-u-boot.git/blame - drivers/usb/host/dwc2.c
usb: host: dwc2: force reset assert
[J-u-boot.git] / drivers / usb / host / dwc2.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
6e9e0626
OT
2/*
3 * Copyright (C) 2012 Oleksandr Tymoshenko <[email protected]>
4 * Copyright (C) 2014 Marek Vasut <[email protected]>
6e9e0626
OT
5 */
6
7#include <common.h>
0bc632c9 8#include <clk.h>
1eb69ae4 9#include <cpu_func.h>
f58a41e0 10#include <dm.h>
6e9e0626 11#include <errno.h>
e17a4bf1 12#include <generic-phy.h>
6e9e0626 13#include <malloc.h>
cf92e05c 14#include <memalign.h>
5c0beb5c 15#include <phys2bus.h>
0bc632c9 16#include <usb.h>
6e9e0626 17#include <usbroothubdes.h>
fd2cd662 18#include <wait_bit.h>
6e9e0626 19#include <asm/io.h>
336d4615 20#include <dm/device_compat.h>
5c735367 21#include <power/regulator.h>
88c34b8d 22#include <reset.h>
6e9e0626
OT
23
24#include "dwc2.h"
25
26/* Use only HC channel 0. */
27#define DWC2_HC_CHANNEL 0
28
29#define DWC2_STATUS_BUF_SIZE 64
42637fda 30#define DWC2_DATA_BUF_SIZE (CONFIG_USB_DWC2_BUFFER_SIZE * 1024)
6e9e0626 31
6e9e0626
OT
32#define MAX_DEVICE 16
33#define MAX_ENDPOINT 16
6e9e0626 34
cc3e3a9e 35struct dwc2_priv {
fd09c205 36#if CONFIG_IS_ENABLED(DM_USB)
db402e00
AS
37 uint8_t aligned_buffer[DWC2_DATA_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
38 uint8_t status_buffer[DWC2_STATUS_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
82e7975b
CK
39#ifdef CONFIG_DM_REGULATOR
40 struct udevice *vbus_supply;
41#endif
e17a4bf1 42 struct phy phy;
0bc632c9 43 struct clk_bulk clks;
f58a41e0 44#else
cc3e3a9e
SG
45 uint8_t *aligned_buffer;
46 uint8_t *status_buffer;
f58a41e0 47#endif
25612f23
SB
48 u8 in_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
49 u8 out_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
cc3e3a9e
SG
50 struct dwc2_core_regs *regs;
51 int root_hub_devnum;
618da563 52 bool ext_vbus;
dd22bace
MD
53 /*
54 * The hnp/srp capability must be disabled if the platform
55 * does't support hnp/srp. Otherwise the force mode can't work.
56 */
c65a3494 57 bool hnp_srp_disable;
b4fbd089 58 bool oc_disable;
88c34b8d
LFT
59
60 struct reset_ctl_bulk resets;
cc3e3a9e 61};
6e9e0626 62
fd09c205 63#if !CONFIG_IS_ENABLED(DM_USB)
db402e00
AS
64/* We need cacheline-aligned buffers for DMA transfers and dcache support */
65DEFINE_ALIGN_BUFFER(uint8_t, aligned_buffer_addr, DWC2_DATA_BUF_SIZE,
66 ARCH_DMA_MINALIGN);
67DEFINE_ALIGN_BUFFER(uint8_t, status_buffer_addr, DWC2_STATUS_BUF_SIZE,
68 ARCH_DMA_MINALIGN);
cc3e3a9e
SG
69
70static struct dwc2_priv local;
f58a41e0 71#endif
6e9e0626
OT
72
73/*
74 * DWC2 IP interface
75 */
6e9e0626
OT
76
77/*
78 * Initializes the FSLSPClkSel field of the HCFG register
79 * depending on the PHY type.
80 */
81static void init_fslspclksel(struct dwc2_core_regs *regs)
82{
83 uint32_t phyclk;
84
85#if (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
86 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
87#else
88 /* High speed PHY running at full speed or high speed */
89 phyclk = DWC2_HCFG_FSLSPCLKSEL_30_60_MHZ;
90#endif
91
92#ifdef CONFIG_DWC2_ULPI_FS_LS
93 uint32_t hwcfg2 = readl(&regs->ghwcfg2);
94 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
95 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
96 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
97 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
98
99 if (hval == 2 && fval == 1)
100 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
101#endif
102
103 clrsetbits_le32(&regs->host_regs.hcfg,
104 DWC2_HCFG_FSLSPCLKSEL_MASK,
105 phyclk << DWC2_HCFG_FSLSPCLKSEL_OFFSET);
106}
107
108/*
109 * Flush a Tx FIFO.
110 *
111 * @param regs Programming view of DWC_otg controller.
112 * @param num Tx FIFO to flush.
113 */
114static void dwc_otg_flush_tx_fifo(struct dwc2_core_regs *regs, const int num)
115{
116 int ret;
117
118 writel(DWC2_GRSTCTL_TXFFLSH | (num << DWC2_GRSTCTL_TXFNUM_OFFSET),
119 &regs->grstctl);
48263504
ÁFR
120 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_TXFFLSH,
121 false, 1000, false);
6e9e0626 122 if (ret)
ac6c796c 123 dev_info(dev, "%s: Timeout!\n", __func__);
6e9e0626
OT
124
125 /* Wait for 3 PHY Clocks */
126 udelay(1);
127}
128
129/*
130 * Flush Rx FIFO.
131 *
132 * @param regs Programming view of DWC_otg controller.
133 */
134static void dwc_otg_flush_rx_fifo(struct dwc2_core_regs *regs)
135{
136 int ret;
137
138 writel(DWC2_GRSTCTL_RXFFLSH, &regs->grstctl);
48263504
ÁFR
139 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_RXFFLSH,
140 false, 1000, false);
6e9e0626 141 if (ret)
ac6c796c 142 dev_info(dev, "%s: Timeout!\n", __func__);
6e9e0626
OT
143
144 /* Wait for 3 PHY Clocks */
145 udelay(1);
146}
147
148/*
149 * Do core a soft reset of the core. Be careful with this because it
150 * resets all the internal state machines of the core.
151 */
152static void dwc_otg_core_reset(struct dwc2_core_regs *regs)
153{
154 int ret;
155
156 /* Wait for AHB master IDLE state. */
48263504
ÁFR
157 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_AHBIDLE,
158 true, 1000, false);
6e9e0626 159 if (ret)
ac6c796c 160 dev_info(dev, "%s: Timeout!\n", __func__);
6e9e0626
OT
161
162 /* Core Soft Reset */
163 writel(DWC2_GRSTCTL_CSFTRST, &regs->grstctl);
48263504
ÁFR
164 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_CSFTRST,
165 false, 1000, false);
6e9e0626 166 if (ret)
ac6c796c 167 dev_info(dev, "%s: Timeout!\n", __func__);
6e9e0626
OT
168
169 /*
170 * Wait for core to come out of reset.
171 * NOTE: This long sleep is _very_ important, otherwise the core will
172 * not stay in host mode after a connector ID change!
173 */
174 mdelay(100);
175}
176
fd09c205 177#if CONFIG_IS_ENABLED(DM_USB) && defined(CONFIG_DM_REGULATOR)
5c735367
KY
178static int dwc_vbus_supply_init(struct udevice *dev)
179{
82e7975b 180 struct dwc2_priv *priv = dev_get_priv(dev);
5c735367
KY
181 int ret;
182
82e7975b
CK
183 ret = device_get_supply_regulator(dev, "vbus-supply",
184 &priv->vbus_supply);
5c735367
KY
185 if (ret) {
186 debug("%s: No vbus supply\n", dev->name);
187 return 0;
188 }
189
82e7975b 190 ret = regulator_set_enable(priv->vbus_supply, true);
5c735367 191 if (ret) {
ac6c796c 192 dev_err(dev, "Error enabling vbus supply\n");
5c735367
KY
193 return ret;
194 }
195
196 return 0;
197}
82e7975b
CK
198
199static int dwc_vbus_supply_exit(struct udevice *dev)
200{
201 struct dwc2_priv *priv = dev_get_priv(dev);
202 int ret;
203
204 if (priv->vbus_supply) {
205 ret = regulator_set_enable(priv->vbus_supply, false);
206 if (ret) {
207 dev_err(dev, "Error disabling vbus supply\n");
208 return ret;
209 }
210 }
211
212 return 0;
213}
5c735367
KY
214#else
215static int dwc_vbus_supply_init(struct udevice *dev)
216{
217 return 0;
218}
82e7975b 219
fd09c205 220#if CONFIG_IS_ENABLED(DM_USB)
82e7975b
CK
221static int dwc_vbus_supply_exit(struct udevice *dev)
222{
223 return 0;
224}
225#endif
5c735367
KY
226#endif
227
6e9e0626
OT
228/*
229 * This function initializes the DWC_otg controller registers for
230 * host mode.
231 *
232 * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
233 * request queues. Host channels are reset to ensure that they are ready for
234 * performing transfers.
235 *
5c735367 236 * @param dev USB Device (NULL if driver model is not being used)
6e9e0626
OT
237 * @param regs Programming view of DWC_otg controller
238 *
239 */
5c735367
KY
240static void dwc_otg_core_host_init(struct udevice *dev,
241 struct dwc2_core_regs *regs)
6e9e0626
OT
242{
243 uint32_t nptxfifosize = 0;
244 uint32_t ptxfifosize = 0;
245 uint32_t hprt0 = 0;
246 int i, ret, num_channels;
247
248 /* Restart the Phy Clock */
249 writel(0, &regs->pcgcctl);
250
251 /* Initialize Host Configuration Register */
252 init_fslspclksel(regs);
253#ifdef CONFIG_DWC2_DFLT_SPEED_FULL
254 setbits_le32(&regs->host_regs.hcfg, DWC2_HCFG_FSLSSUPP);
255#endif
256
257 /* Configure data FIFO sizes */
258#ifdef CONFIG_DWC2_ENABLE_DYNAMIC_FIFO
259 if (readl(&regs->ghwcfg2) & DWC2_HWCFG2_DYNAMIC_FIFO) {
260 /* Rx FIFO */
261 writel(CONFIG_DWC2_HOST_RX_FIFO_SIZE, &regs->grxfsiz);
262
263 /* Non-periodic Tx FIFO */
264 nptxfifosize |= CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE <<
265 DWC2_FIFOSIZE_DEPTH_OFFSET;
266 nptxfifosize |= CONFIG_DWC2_HOST_RX_FIFO_SIZE <<
267 DWC2_FIFOSIZE_STARTADDR_OFFSET;
268 writel(nptxfifosize, &regs->gnptxfsiz);
269
270 /* Periodic Tx FIFO */
271 ptxfifosize |= CONFIG_DWC2_HOST_PERIO_TX_FIFO_SIZE <<
272 DWC2_FIFOSIZE_DEPTH_OFFSET;
273 ptxfifosize |= (CONFIG_DWC2_HOST_RX_FIFO_SIZE +
274 CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE) <<
275 DWC2_FIFOSIZE_STARTADDR_OFFSET;
276 writel(ptxfifosize, &regs->hptxfsiz);
277 }
278#endif
279
280 /* Clear Host Set HNP Enable in the OTG Control Register */
281 clrbits_le32(&regs->gotgctl, DWC2_GOTGCTL_HSTSETHNPEN);
282
283 /* Make sure the FIFOs are flushed. */
284 dwc_otg_flush_tx_fifo(regs, 0x10); /* All Tx FIFOs */
285 dwc_otg_flush_rx_fifo(regs);
286
287 /* Flush out any leftover queued requests. */
288 num_channels = readl(&regs->ghwcfg2);
289 num_channels &= DWC2_HWCFG2_NUM_HOST_CHAN_MASK;
290 num_channels >>= DWC2_HWCFG2_NUM_HOST_CHAN_OFFSET;
291 num_channels += 1;
292
293 for (i = 0; i < num_channels; i++)
294 clrsetbits_le32(&regs->hc_regs[i].hcchar,
295 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_EPDIR,
296 DWC2_HCCHAR_CHDIS);
297
298 /* Halt all channels to put them into a known state. */
299 for (i = 0; i < num_channels; i++) {
300 clrsetbits_le32(&regs->hc_regs[i].hcchar,
301 DWC2_HCCHAR_EPDIR,
302 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS);
48263504
ÁFR
303 ret = wait_for_bit_le32(&regs->hc_regs[i].hcchar,
304 DWC2_HCCHAR_CHEN, false, 1000, false);
6e9e0626 305 if (ret)
ac6c796c 306 dev_info("%s: Timeout!\n", __func__);
6e9e0626
OT
307 }
308
309 /* Turn on the vbus power. */
310 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST) {
311 hprt0 = readl(&regs->hprt0);
312 hprt0 &= ~(DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET);
313 hprt0 &= ~(DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG);
314 if (!(hprt0 & DWC2_HPRT0_PRTPWR)) {
315 hprt0 |= DWC2_HPRT0_PRTPWR;
316 writel(hprt0, &regs->hprt0);
317 }
318 }
5c735367
KY
319
320 if (dev)
321 dwc_vbus_supply_init(dev);
6e9e0626
OT
322}
323
324/*
325 * This function initializes the DWC_otg controller registers and
326 * prepares the core for device mode or host mode operation.
327 *
328 * @param regs Programming view of the DWC_otg controller
329 */
55901989 330static void dwc_otg_core_init(struct dwc2_priv *priv)
6e9e0626 331{
55901989 332 struct dwc2_core_regs *regs = priv->regs;
6e9e0626
OT
333 uint32_t ahbcfg = 0;
334 uint32_t usbcfg = 0;
335 uint8_t brst_sz = CONFIG_DWC2_DMA_BURST_SIZE;
336
337 /* Common Initialization */
338 usbcfg = readl(&regs->gusbcfg);
339
340 /* Program the ULPI External VBUS bit if needed */
618da563 341 if (priv->ext_vbus) {
b4fbd089
MV
342 usbcfg |= DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
343 if (!priv->oc_disable) {
344 usbcfg |= DWC2_GUSBCFG_ULPI_INT_VBUS_INDICATOR |
345 DWC2_GUSBCFG_INDICATOR_PASSTHROUGH;
346 }
618da563
MV
347 } else {
348 usbcfg &= ~DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
349 }
6e9e0626
OT
350
351 /* Set external TS Dline pulsing */
352#ifdef CONFIG_DWC2_TS_DLINE
353 usbcfg |= DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
354#else
355 usbcfg &= ~DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
356#endif
357 writel(usbcfg, &regs->gusbcfg);
358
359 /* Reset the Controller */
360 dwc_otg_core_reset(regs);
361
362 /*
363 * This programming sequence needs to happen in FS mode before
364 * any other programming occurs
365 */
366#if defined(CONFIG_DWC2_DFLT_SPEED_FULL) && \
367 (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
368 /* If FS mode with FS PHY */
369 setbits_le32(&regs->gusbcfg, DWC2_GUSBCFG_PHYSEL);
370
371 /* Reset after a PHY select */
372 dwc_otg_core_reset(regs);
373
374 /*
375 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS.
376 * Also do this on HNP Dev/Host mode switches (done in dev_init
377 * and host_init).
378 */
379 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
380 init_fslspclksel(regs);
381
382#ifdef CONFIG_DWC2_I2C_ENABLE
383 /* Program GUSBCFG.OtgUtmifsSel to I2C */
384 setbits_le32(&regs->gusbcfg, DWC2_GUSBCFG_OTGUTMIFSSEL);
385
386 /* Program GI2CCTL.I2CEn */
387 clrsetbits_le32(&regs->gi2cctl, DWC2_GI2CCTL_I2CEN |
388 DWC2_GI2CCTL_I2CDEVADDR_MASK,
389 1 << DWC2_GI2CCTL_I2CDEVADDR_OFFSET);
390 setbits_le32(&regs->gi2cctl, DWC2_GI2CCTL_I2CEN);
391#endif
392
393#else
394 /* High speed PHY. */
395
396 /*
397 * HS PHY parameters. These parameters are preserved during
398 * soft reset so only program the first time. Do a soft reset
399 * immediately after setting phyif.
400 */
401 usbcfg &= ~(DWC2_GUSBCFG_ULPI_UTMI_SEL | DWC2_GUSBCFG_PHYIF);
402 usbcfg |= CONFIG_DWC2_PHY_TYPE << DWC2_GUSBCFG_ULPI_UTMI_SEL_OFFSET;
403
404 if (usbcfg & DWC2_GUSBCFG_ULPI_UTMI_SEL) { /* ULPI interface */
405#ifdef CONFIG_DWC2_PHY_ULPI_DDR
406 usbcfg |= DWC2_GUSBCFG_DDRSEL;
407#else
408 usbcfg &= ~DWC2_GUSBCFG_DDRSEL;
409#endif
410 } else { /* UTMI+ interface */
163f8858 411#if (CONFIG_DWC2_UTMI_WIDTH == 16)
6e9e0626
OT
412 usbcfg |= DWC2_GUSBCFG_PHYIF;
413#endif
414 }
415
416 writel(usbcfg, &regs->gusbcfg);
417
418 /* Reset after setting the PHY parameters */
419 dwc_otg_core_reset(regs);
420#endif
421
422 usbcfg = readl(&regs->gusbcfg);
423 usbcfg &= ~(DWC2_GUSBCFG_ULPI_FSLS | DWC2_GUSBCFG_ULPI_CLK_SUS_M);
424#ifdef CONFIG_DWC2_ULPI_FS_LS
425 uint32_t hwcfg2 = readl(&regs->ghwcfg2);
426 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
427 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
428 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
429 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
430 if (hval == 2 && fval == 1) {
431 usbcfg |= DWC2_GUSBCFG_ULPI_FSLS;
432 usbcfg |= DWC2_GUSBCFG_ULPI_CLK_SUS_M;
433 }
434#endif
c65a3494
MD
435 if (priv->hnp_srp_disable)
436 usbcfg |= DWC2_GUSBCFG_FORCEHOSTMODE;
437
6e9e0626
OT
438 writel(usbcfg, &regs->gusbcfg);
439
440 /* Program the GAHBCFG Register. */
441 switch (readl(&regs->ghwcfg2) & DWC2_HWCFG2_ARCHITECTURE_MASK) {
442 case DWC2_HWCFG2_ARCHITECTURE_SLAVE_ONLY:
443 break;
444 case DWC2_HWCFG2_ARCHITECTURE_EXT_DMA:
445 while (brst_sz > 1) {
446 ahbcfg |= ahbcfg + (1 << DWC2_GAHBCFG_HBURSTLEN_OFFSET);
447 ahbcfg &= DWC2_GAHBCFG_HBURSTLEN_MASK;
448 brst_sz >>= 1;
449 }
450
451#ifdef CONFIG_DWC2_DMA_ENABLE
452 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
453#endif
454 break;
455
456 case DWC2_HWCFG2_ARCHITECTURE_INT_DMA:
457 ahbcfg |= DWC2_GAHBCFG_HBURSTLEN_INCR4;
458#ifdef CONFIG_DWC2_DMA_ENABLE
459 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
460#endif
461 break;
462 }
463
464 writel(ahbcfg, &regs->gahbcfg);
465
c65a3494
MD
466 /* Program the capabilities in GUSBCFG Register */
467 usbcfg = 0;
6e9e0626 468
c65a3494
MD
469 if (!priv->hnp_srp_disable)
470 usbcfg |= DWC2_GUSBCFG_HNPCAP | DWC2_GUSBCFG_SRPCAP;
6e9e0626 471#ifdef CONFIG_DWC2_IC_USB_CAP
c65a3494 472 usbcfg |= DWC2_GUSBCFG_IC_USB_CAP;
6e9e0626 473#endif
c65a3494
MD
474
475 setbits_le32(&regs->gusbcfg, usbcfg);
6e9e0626
OT
476}
477
478/*
479 * Prepares a host channel for transferring packets to/from a specific
480 * endpoint. The HCCHARn register is set up with the characteristics specified
481 * in _hc. Host channel interrupts that may need to be serviced while this
482 * transfer is in progress are enabled.
483 *
484 * @param regs Programming view of DWC_otg controller
485 * @param hc Information needed to initialize the host channel
486 */
487static void dwc_otg_hc_init(struct dwc2_core_regs *regs, uint8_t hc_num,
ed9bcbc7
SW
488 struct usb_device *dev, uint8_t dev_addr, uint8_t ep_num,
489 uint8_t ep_is_in, uint8_t ep_type, uint16_t max_packet)
6e9e0626
OT
490{
491 struct dwc2_hc_regs *hc_regs = &regs->hc_regs[hc_num];
ed9bcbc7
SW
492 uint32_t hcchar = (dev_addr << DWC2_HCCHAR_DEVADDR_OFFSET) |
493 (ep_num << DWC2_HCCHAR_EPNUM_OFFSET) |
494 (ep_is_in << DWC2_HCCHAR_EPDIR_OFFSET) |
495 (ep_type << DWC2_HCCHAR_EPTYPE_OFFSET) |
496 (max_packet << DWC2_HCCHAR_MPS_OFFSET);
497
498 if (dev->speed == USB_SPEED_LOW)
499 hcchar |= DWC2_HCCHAR_LSPDDEV;
6e9e0626 500
6e9e0626
OT
501 /*
502 * Program the HCCHARn register with the endpoint characteristics
503 * for the current transfer.
504 */
505 writel(hcchar, &hc_regs->hcchar);
506
890f0ee4 507 /* Program the HCSPLIT register, default to no SPLIT */
6e9e0626
OT
508 writel(0, &hc_regs->hcsplt);
509}
510
890f0ee4
SB
511static void dwc_otg_hc_init_split(struct dwc2_hc_regs *hc_regs,
512 uint8_t hub_devnum, uint8_t hub_port)
513{
514 uint32_t hcsplt = 0;
515
516 hcsplt = DWC2_HCSPLT_SPLTENA;
517 hcsplt |= hub_devnum << DWC2_HCSPLT_HUBADDR_OFFSET;
518 hcsplt |= hub_port << DWC2_HCSPLT_PRTADDR_OFFSET;
519
520 /* Program the HCSPLIT register for SPLITs */
521 writel(hcsplt, &hc_regs->hcsplt);
522}
523
6e9e0626
OT
524/*
525 * DWC2 to USB API interface
526 */
527/* Direction: In ; Request: Status */
cc3e3a9e
SG
528static int dwc_otg_submit_rh_msg_in_status(struct dwc2_core_regs *regs,
529 struct usb_device *dev, void *buffer,
6e9e0626
OT
530 int txlen, struct devrequest *cmd)
531{
532 uint32_t hprt0 = 0;
533 uint32_t port_status = 0;
534 uint32_t port_change = 0;
535 int len = 0;
536 int stat = 0;
537
538 switch (cmd->requesttype & ~USB_DIR_IN) {
539 case 0:
540 *(uint16_t *)buffer = cpu_to_le16(1);
541 len = 2;
542 break;
543 case USB_RECIP_INTERFACE:
544 case USB_RECIP_ENDPOINT:
545 *(uint16_t *)buffer = cpu_to_le16(0);
546 len = 2;
547 break;
548 case USB_TYPE_CLASS:
549 *(uint32_t *)buffer = cpu_to_le32(0);
550 len = 4;
551 break;
552 case USB_RECIP_OTHER | USB_TYPE_CLASS:
553 hprt0 = readl(&regs->hprt0);
554 if (hprt0 & DWC2_HPRT0_PRTCONNSTS)
555 port_status |= USB_PORT_STAT_CONNECTION;
556 if (hprt0 & DWC2_HPRT0_PRTENA)
557 port_status |= USB_PORT_STAT_ENABLE;
558 if (hprt0 & DWC2_HPRT0_PRTSUSP)
559 port_status |= USB_PORT_STAT_SUSPEND;
560 if (hprt0 & DWC2_HPRT0_PRTOVRCURRACT)
561 port_status |= USB_PORT_STAT_OVERCURRENT;
562 if (hprt0 & DWC2_HPRT0_PRTRST)
563 port_status |= USB_PORT_STAT_RESET;
564 if (hprt0 & DWC2_HPRT0_PRTPWR)
565 port_status |= USB_PORT_STAT_POWER;
566
4748cce5
SW
567 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) == DWC2_HPRT0_PRTSPD_LOW)
568 port_status |= USB_PORT_STAT_LOW_SPEED;
569 else if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
570 DWC2_HPRT0_PRTSPD_HIGH)
571 port_status |= USB_PORT_STAT_HIGH_SPEED;
6e9e0626
OT
572
573 if (hprt0 & DWC2_HPRT0_PRTENCHNG)
574 port_change |= USB_PORT_STAT_C_ENABLE;
575 if (hprt0 & DWC2_HPRT0_PRTCONNDET)
576 port_change |= USB_PORT_STAT_C_CONNECTION;
577 if (hprt0 & DWC2_HPRT0_PRTOVRCURRCHNG)
578 port_change |= USB_PORT_STAT_C_OVERCURRENT;
579
580 *(uint32_t *)buffer = cpu_to_le32(port_status |
581 (port_change << 16));
582 len = 4;
583 break;
584 default:
585 puts("unsupported root hub command\n");
586 stat = USB_ST_STALLED;
587 }
588
589 dev->act_len = min(len, txlen);
590 dev->status = stat;
591
592 return stat;
593}
594
595/* Direction: In ; Request: Descriptor */
596static int dwc_otg_submit_rh_msg_in_descriptor(struct usb_device *dev,
597 void *buffer, int txlen,
598 struct devrequest *cmd)
599{
600 unsigned char data[32];
601 uint32_t dsc;
602 int len = 0;
603 int stat = 0;
604 uint16_t wValue = cpu_to_le16(cmd->value);
605 uint16_t wLength = cpu_to_le16(cmd->length);
606
607 switch (cmd->requesttype & ~USB_DIR_IN) {
608 case 0:
609 switch (wValue & 0xff00) {
610 case 0x0100: /* device descriptor */
b4141195 611 len = min3(txlen, (int)sizeof(root_hub_dev_des), (int)wLength);
6e9e0626
OT
612 memcpy(buffer, root_hub_dev_des, len);
613 break;
614 case 0x0200: /* configuration descriptor */
b4141195 615 len = min3(txlen, (int)sizeof(root_hub_config_des), (int)wLength);
6e9e0626
OT
616 memcpy(buffer, root_hub_config_des, len);
617 break;
618 case 0x0300: /* string descriptors */
619 switch (wValue & 0xff) {
620 case 0x00:
b4141195
MY
621 len = min3(txlen, (int)sizeof(root_hub_str_index0),
622 (int)wLength);
6e9e0626
OT
623 memcpy(buffer, root_hub_str_index0, len);
624 break;
625 case 0x01:
b4141195
MY
626 len = min3(txlen, (int)sizeof(root_hub_str_index1),
627 (int)wLength);
6e9e0626
OT
628 memcpy(buffer, root_hub_str_index1, len);
629 break;
630 }
631 break;
632 default:
633 stat = USB_ST_STALLED;
634 }
635 break;
636
637 case USB_TYPE_CLASS:
638 /* Root port config, set 1 port and nothing else. */
639 dsc = 0x00000001;
640
641 data[0] = 9; /* min length; */
642 data[1] = 0x29;
643 data[2] = dsc & RH_A_NDP;
644 data[3] = 0;
645 if (dsc & RH_A_PSM)
646 data[3] |= 0x1;
647 if (dsc & RH_A_NOCP)
648 data[3] |= 0x10;
649 else if (dsc & RH_A_OCPM)
650 data[3] |= 0x8;
651
652 /* corresponds to data[4-7] */
653 data[5] = (dsc & RH_A_POTPGT) >> 24;
654 data[7] = dsc & RH_B_DR;
655 if (data[2] < 7) {
656 data[8] = 0xff;
657 } else {
658 data[0] += 2;
659 data[8] = (dsc & RH_B_DR) >> 8;
660 data[9] = 0xff;
661 data[10] = data[9];
662 }
663
b4141195 664 len = min3(txlen, (int)data[0], (int)wLength);
6e9e0626
OT
665 memcpy(buffer, data, len);
666 break;
667 default:
668 puts("unsupported root hub command\n");
669 stat = USB_ST_STALLED;
670 }
671
672 dev->act_len = min(len, txlen);
673 dev->status = stat;
674
675 return stat;
676}
677
678/* Direction: In ; Request: Configuration */
679static int dwc_otg_submit_rh_msg_in_configuration(struct usb_device *dev,
680 void *buffer, int txlen,
681 struct devrequest *cmd)
682{
683 int len = 0;
684 int stat = 0;
685
686 switch (cmd->requesttype & ~USB_DIR_IN) {
687 case 0:
688 *(uint8_t *)buffer = 0x01;
689 len = 1;
690 break;
691 default:
692 puts("unsupported root hub command\n");
693 stat = USB_ST_STALLED;
694 }
695
696 dev->act_len = min(len, txlen);
697 dev->status = stat;
698
699 return stat;
700}
701
702/* Direction: In */
cc3e3a9e
SG
703static int dwc_otg_submit_rh_msg_in(struct dwc2_priv *priv,
704 struct usb_device *dev, void *buffer,
705 int txlen, struct devrequest *cmd)
6e9e0626
OT
706{
707 switch (cmd->request) {
708 case USB_REQ_GET_STATUS:
cc3e3a9e 709 return dwc_otg_submit_rh_msg_in_status(priv->regs, dev, buffer,
6e9e0626
OT
710 txlen, cmd);
711 case USB_REQ_GET_DESCRIPTOR:
712 return dwc_otg_submit_rh_msg_in_descriptor(dev, buffer,
713 txlen, cmd);
714 case USB_REQ_GET_CONFIGURATION:
715 return dwc_otg_submit_rh_msg_in_configuration(dev, buffer,
716 txlen, cmd);
717 default:
718 puts("unsupported root hub command\n");
719 return USB_ST_STALLED;
720 }
721}
722
723/* Direction: Out */
cc3e3a9e
SG
724static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
725 struct usb_device *dev,
726 void *buffer, int txlen,
727 struct devrequest *cmd)
6e9e0626 728{
cc3e3a9e 729 struct dwc2_core_regs *regs = priv->regs;
6e9e0626
OT
730 int len = 0;
731 int stat = 0;
732 uint16_t bmrtype_breq = cmd->requesttype | (cmd->request << 8);
733 uint16_t wValue = cpu_to_le16(cmd->value);
734
735 switch (bmrtype_breq & ~USB_DIR_IN) {
736 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_ENDPOINT:
737 case (USB_REQ_CLEAR_FEATURE << 8) | USB_TYPE_CLASS:
738 break;
739
740 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
741 switch (wValue) {
742 case USB_PORT_FEAT_C_CONNECTION:
743 setbits_le32(&regs->hprt0, DWC2_HPRT0_PRTCONNDET);
744 break;
745 }
746 break;
747
748 case (USB_REQ_SET_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
749 switch (wValue) {
750 case USB_PORT_FEAT_SUSPEND:
751 break;
752
753 case USB_PORT_FEAT_RESET:
754 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
755 DWC2_HPRT0_PRTCONNDET |
756 DWC2_HPRT0_PRTENCHNG |
757 DWC2_HPRT0_PRTOVRCURRCHNG,
758 DWC2_HPRT0_PRTRST);
759 mdelay(50);
760 clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTRST);
761 break;
762
763 case USB_PORT_FEAT_POWER:
764 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
765 DWC2_HPRT0_PRTCONNDET |
766 DWC2_HPRT0_PRTENCHNG |
767 DWC2_HPRT0_PRTOVRCURRCHNG,
768 DWC2_HPRT0_PRTRST);
769 break;
770
771 case USB_PORT_FEAT_ENABLE:
772 break;
773 }
774 break;
775 case (USB_REQ_SET_ADDRESS << 8):
cc3e3a9e 776 priv->root_hub_devnum = wValue;
6e9e0626
OT
777 break;
778 case (USB_REQ_SET_CONFIGURATION << 8):
779 break;
780 default:
781 puts("unsupported root hub command\n");
782 stat = USB_ST_STALLED;
783 }
784
785 len = min(len, txlen);
786
787 dev->act_len = len;
788 dev->status = stat;
789
790 return stat;
791}
792
cc3e3a9e
SG
793static int dwc_otg_submit_rh_msg(struct dwc2_priv *priv, struct usb_device *dev,
794 unsigned long pipe, void *buffer, int txlen,
6e9e0626
OT
795 struct devrequest *cmd)
796{
797 int stat = 0;
798
799 if (usb_pipeint(pipe)) {
800 puts("Root-Hub submit IRQ: NOT implemented\n");
801 return 0;
802 }
803
804 if (cmd->requesttype & USB_DIR_IN)
cc3e3a9e 805 stat = dwc_otg_submit_rh_msg_in(priv, dev, buffer, txlen, cmd);
6e9e0626 806 else
cc3e3a9e 807 stat = dwc_otg_submit_rh_msg_out(priv, dev, buffer, txlen, cmd);
6e9e0626
OT
808
809 mdelay(1);
810
811 return stat;
812}
813
25612f23 814int wait_for_chhltd(struct dwc2_hc_regs *hc_regs, uint32_t *sub, u8 *toggle)
4a1d21fc 815{
4a1d21fc
SW
816 int ret;
817 uint32_t hcint, hctsiz;
818
48263504 819 ret = wait_for_bit_le32(&hc_regs->hcint, DWC2_HCINT_CHHLTD, true,
c2e4c865 820 2000, false);
4a1d21fc
SW
821 if (ret)
822 return ret;
823
824 hcint = readl(&hc_regs->hcint);
4a1d21fc
SW
825 hctsiz = readl(&hc_regs->hctsiz);
826 *sub = (hctsiz & DWC2_HCTSIZ_XFERSIZE_MASK) >>
827 DWC2_HCTSIZ_XFERSIZE_OFFSET;
66ffc875 828 *toggle = (hctsiz & DWC2_HCTSIZ_PID_MASK) >> DWC2_HCTSIZ_PID_OFFSET;
4a1d21fc 829
03460cdc
SB
830 debug("%s: HCINT=%08x sub=%u toggle=%d\n", __func__, hcint, *sub,
831 *toggle);
4a1d21fc 832
03460cdc
SB
833 if (hcint & DWC2_HCINT_XFERCOMP)
834 return 0;
835
836 if (hcint & (DWC2_HCINT_NAK | DWC2_HCINT_FRMOVRUN))
837 return -EAGAIN;
838
839 debug("%s: Error (HCINT=%08x)\n", __func__, hcint);
840 return -EINVAL;
4a1d21fc
SW
841}
842
7b5e504d
SW
843static int dwc2_eptype[] = {
844 DWC2_HCCHAR_EPTYPE_ISOC,
845 DWC2_HCCHAR_EPTYPE_INTR,
846 DWC2_HCCHAR_EPTYPE_CONTROL,
847 DWC2_HCCHAR_EPTYPE_BULK,
848};
849
daed3059 850static int transfer_chunk(struct dwc2_hc_regs *hc_regs, void *aligned_buffer,
25612f23 851 u8 *pid, int in, void *buffer, int num_packets,
d2ff51b3 852 int xfer_len, int *actual_len, int odd_frame)
daed3059
SB
853{
854 int ret = 0;
855 uint32_t sub;
856
857 debug("%s: chunk: pid %d xfer_len %u pkts %u\n", __func__,
858 *pid, xfer_len, num_packets);
859
860 writel((xfer_len << DWC2_HCTSIZ_XFERSIZE_OFFSET) |
861 (num_packets << DWC2_HCTSIZ_PKTCNT_OFFSET) |
862 (*pid << DWC2_HCTSIZ_PID_OFFSET),
863 &hc_regs->hctsiz);
864
57ca63b8
EC
865 if (xfer_len) {
866 if (in) {
867 invalidate_dcache_range(
868 (uintptr_t)aligned_buffer,
869 (uintptr_t)aligned_buffer +
870 roundup(xfer_len, ARCH_DMA_MINALIGN));
871 } else {
872 memcpy(aligned_buffer, buffer, xfer_len);
873 flush_dcache_range(
874 (uintptr_t)aligned_buffer,
875 (uintptr_t)aligned_buffer +
876 roundup(xfer_len, ARCH_DMA_MINALIGN));
877 }
daed3059
SB
878 }
879
880 writel(phys_to_bus((unsigned long)aligned_buffer), &hc_regs->hcdma);
881
882 /* Clear old interrupt conditions for this host channel. */
883 writel(0x3fff, &hc_regs->hcint);
884
885 /* Set host channel enable after all other setup is complete. */
886 clrsetbits_le32(&hc_regs->hcchar, DWC2_HCCHAR_MULTICNT_MASK |
d2ff51b3
SB
887 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS |
888 DWC2_HCCHAR_ODDFRM,
daed3059 889 (1 << DWC2_HCCHAR_MULTICNT_OFFSET) |
d2ff51b3 890 (odd_frame << DWC2_HCCHAR_ODDFRM_OFFSET) |
daed3059
SB
891 DWC2_HCCHAR_CHEN);
892
893 ret = wait_for_chhltd(hc_regs, &sub, pid);
894 if (ret < 0)
895 return ret;
896
897 if (in) {
898 xfer_len -= sub;
899
900 invalidate_dcache_range((unsigned long)aligned_buffer,
901 (unsigned long)aligned_buffer +
902 roundup(xfer_len, ARCH_DMA_MINALIGN));
903
904 memcpy(buffer, aligned_buffer, xfer_len);
905 }
906 *actual_len = xfer_len;
907
908 return ret;
909}
910
cc3e3a9e 911int chunk_msg(struct dwc2_priv *priv, struct usb_device *dev,
25612f23 912 unsigned long pipe, u8 *pid, int in, void *buffer, int len)
6e9e0626 913{
cc3e3a9e 914 struct dwc2_core_regs *regs = priv->regs;
7b5e504d 915 struct dwc2_hc_regs *hc_regs = &regs->hc_regs[DWC2_HC_CHANNEL];
d2ff51b3 916 struct dwc2_host_regs *host_regs = &regs->host_regs;
6e9e0626
OT
917 int devnum = usb_pipedevice(pipe);
918 int ep = usb_pipeendpoint(pipe);
919 int max = usb_maxpacket(dev, pipe);
7b5e504d 920 int eptype = dwc2_eptype[usb_pipetype(pipe)];
6e9e0626 921 int done = 0;
5877de91 922 int ret = 0;
b54e4470
SB
923 int do_split = 0;
924 int complete_split = 0;
6e9e0626
OT
925 uint32_t xfer_len;
926 uint32_t num_packets;
927 int stop_transfer = 0;
56a7bbd7 928 uint32_t max_xfer_len;
d2ff51b3 929 int ssplit_frame_num = 0;
6e9e0626 930
7b5e504d
SW
931 debug("%s: msg: pipe %lx pid %d in %d len %d\n", __func__, pipe, *pid,
932 in, len);
6e9e0626 933
56a7bbd7
SB
934 max_xfer_len = CONFIG_DWC2_MAX_PACKET_COUNT * max;
935 if (max_xfer_len > CONFIG_DWC2_MAX_TRANSFER_SIZE)
936 max_xfer_len = CONFIG_DWC2_MAX_TRANSFER_SIZE;
937 if (max_xfer_len > DWC2_DATA_BUF_SIZE)
938 max_xfer_len = DWC2_DATA_BUF_SIZE;
939
940 /* Make sure that max_xfer_len is a multiple of max packet size. */
941 num_packets = max_xfer_len / max;
942 max_xfer_len = num_packets * max;
943
daed3059
SB
944 /* Initialize channel */
945 dwc_otg_hc_init(regs, DWC2_HC_CHANNEL, dev, devnum, ep, in,
946 eptype, max);
6e9e0626 947
b54e4470
SB
948 /* Check if the target is a FS/LS device behind a HS hub */
949 if (dev->speed != USB_SPEED_HIGH) {
950 uint8_t hub_addr;
951 uint8_t hub_port;
952 uint32_t hprt0 = readl(&regs->hprt0);
953 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
954 DWC2_HPRT0_PRTSPD_HIGH) {
955 usb_find_usb2_hub_address_port(dev, &hub_addr,
956 &hub_port);
957 dwc_otg_hc_init_split(hc_regs, hub_addr, hub_port);
958
959 do_split = 1;
960 num_packets = 1;
961 max_xfer_len = max;
962 }
963 }
6e9e0626 964
daed3059
SB
965 do {
966 int actual_len = 0;
b54e4470 967 uint32_t hcint;
d2ff51b3 968 int odd_frame = 0;
6e9e0626 969 xfer_len = len - done;
6e9e0626 970
56a7bbd7
SB
971 if (xfer_len > max_xfer_len)
972 xfer_len = max_xfer_len;
973 else if (xfer_len > max)
6e9e0626 974 num_packets = (xfer_len + max - 1) / max;
56a7bbd7 975 else
6e9e0626 976 num_packets = 1;
6e9e0626 977
b54e4470
SB
978 if (complete_split)
979 setbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
980 else if (do_split)
981 clrbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
db402e00 982
d2ff51b3
SB
983 if (eptype == DWC2_HCCHAR_EPTYPE_INTR) {
984 int uframe_num = readl(&host_regs->hfnum);
985 if (!(uframe_num & 0x1))
986 odd_frame = 1;
cc3e3a9e 987 }
d1c880c6 988
daed3059
SB
989 ret = transfer_chunk(hc_regs, priv->aligned_buffer, pid,
990 in, (char *)buffer + done, num_packets,
d2ff51b3 991 xfer_len, &actual_len, odd_frame);
7b5e504d 992
b54e4470
SB
993 hcint = readl(&hc_regs->hcint);
994 if (complete_split) {
995 stop_transfer = 0;
d2ff51b3 996 if (hcint & DWC2_HCINT_NYET) {
b54e4470 997 ret = 0;
d2ff51b3
SB
998 int frame_num = DWC2_HFNUM_MAX_FRNUM &
999 readl(&host_regs->hfnum);
1000 if (((frame_num - ssplit_frame_num) &
1001 DWC2_HFNUM_MAX_FRNUM) > 4)
1002 ret = -EAGAIN;
1003 } else
b54e4470
SB
1004 complete_split = 0;
1005 } else if (do_split) {
1006 if (hcint & DWC2_HCINT_ACK) {
d2ff51b3
SB
1007 ssplit_frame_num = DWC2_HFNUM_MAX_FRNUM &
1008 readl(&host_regs->hfnum);
b54e4470
SB
1009 ret = 0;
1010 complete_split = 1;
1011 }
1012 }
6e9e0626 1013
5877de91 1014 if (ret)
4a1d21fc 1015 break;
6e9e0626 1016
daed3059
SB
1017 if (actual_len < xfer_len)
1018 stop_transfer = 1;
6e9e0626 1019
daed3059 1020 done += actual_len;
d1c880c6 1021
b54e4470
SB
1022 /* Transactions are done when when either all data is transferred or
1023 * there is a short transfer. In case of a SPLIT make sure the CSPLIT
1024 * is executed.
1025 */
1026 } while (((done < len) && !stop_transfer) || complete_split);
6e9e0626
OT
1027
1028 writel(0, &hc_regs->hcintmsk);
1029 writel(0xFFFFFFFF, &hc_regs->hcint);
1030
1031 dev->status = 0;
1032 dev->act_len = done;
1033
5877de91 1034 return ret;
6e9e0626
OT
1035}
1036
7b5e504d 1037/* U-Boot USB transmission interface */
cc3e3a9e
SG
1038int _submit_bulk_msg(struct dwc2_priv *priv, struct usb_device *dev,
1039 unsigned long pipe, void *buffer, int len)
7b5e504d
SW
1040{
1041 int devnum = usb_pipedevice(pipe);
1042 int ep = usb_pipeendpoint(pipe);
25612f23 1043 u8* pid;
7b5e504d 1044
25612f23 1045 if ((devnum >= MAX_DEVICE) || (devnum == priv->root_hub_devnum)) {
7b5e504d
SW
1046 dev->status = 0;
1047 return -EINVAL;
1048 }
1049
25612f23
SB
1050 if (usb_pipein(pipe))
1051 pid = &priv->in_data_toggle[devnum][ep];
1052 else
1053 pid = &priv->out_data_toggle[devnum][ep];
1054
1055 return chunk_msg(priv, dev, pipe, pid, usb_pipein(pipe), buffer, len);
7b5e504d
SW
1056}
1057
cc3e3a9e
SG
1058static int _submit_control_msg(struct dwc2_priv *priv, struct usb_device *dev,
1059 unsigned long pipe, void *buffer, int len,
1060 struct devrequest *setup)
6e9e0626 1061{
6e9e0626 1062 int devnum = usb_pipedevice(pipe);
25612f23
SB
1063 int ret, act_len;
1064 u8 pid;
6e9e0626
OT
1065 /* For CONTROL endpoint pid should start with DATA1 */
1066 int status_direction;
1067
cc3e3a9e 1068 if (devnum == priv->root_hub_devnum) {
6e9e0626
OT
1069 dev->status = 0;
1070 dev->speed = USB_SPEED_HIGH;
cc3e3a9e
SG
1071 return dwc_otg_submit_rh_msg(priv, dev, pipe, buffer, len,
1072 setup);
6e9e0626
OT
1073 }
1074
b54e4470 1075 /* SETUP stage */
ee837554 1076 pid = DWC2_HC_PID_SETUP;
b54e4470
SB
1077 do {
1078 ret = chunk_msg(priv, dev, pipe, &pid, 0, setup, 8);
1079 } while (ret == -EAGAIN);
ee837554
SW
1080 if (ret)
1081 return ret;
6e9e0626 1082
b54e4470
SB
1083 /* DATA stage */
1084 act_len = 0;
6e9e0626 1085 if (buffer) {
282685e0 1086 pid = DWC2_HC_PID_DATA1;
b54e4470
SB
1087 do {
1088 ret = chunk_msg(priv, dev, pipe, &pid, usb_pipein(pipe),
1089 buffer, len);
1090 act_len += dev->act_len;
1091 buffer += dev->act_len;
1092 len -= dev->act_len;
1093 } while (ret == -EAGAIN);
ee837554
SW
1094 if (ret)
1095 return ret;
b54e4470
SB
1096 status_direction = usb_pipeout(pipe);
1097 } else {
1098 /* No-data CONTROL always ends with an IN transaction */
6e9e0626 1099 status_direction = 1;
b54e4470 1100 }
6e9e0626 1101
b54e4470 1102 /* STATUS stage */
ee837554 1103 pid = DWC2_HC_PID_DATA1;
b54e4470
SB
1104 do {
1105 ret = chunk_msg(priv, dev, pipe, &pid, status_direction,
1106 priv->status_buffer, 0);
1107 } while (ret == -EAGAIN);
ee837554
SW
1108 if (ret)
1109 return ret;
6e9e0626 1110
ee837554 1111 dev->act_len = act_len;
6e9e0626 1112
4a1d21fc 1113 return 0;
6e9e0626
OT
1114}
1115
cc3e3a9e 1116int _submit_int_msg(struct dwc2_priv *priv, struct usb_device *dev,
3437121c
MS
1117 unsigned long pipe, void *buffer, int len, int interval,
1118 bool nonblock)
6e9e0626 1119{
5877de91
SW
1120 unsigned long timeout;
1121 int ret;
1122
e236519b 1123 /* FIXME: what is interval? */
5877de91
SW
1124
1125 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1126 for (;;) {
1127 if (get_timer(0) > timeout) {
ac6c796c 1128 dev_err(dev, "Timeout poll on interrupt endpoint\n");
5877de91
SW
1129 return -ETIMEDOUT;
1130 }
cc3e3a9e 1131 ret = _submit_bulk_msg(priv, dev, pipe, buffer, len);
9dcab2c4 1132 if ((ret != -EAGAIN) || nonblock)
5877de91
SW
1133 return ret;
1134 }
6e9e0626
OT
1135}
1136
88c34b8d
LFT
1137static int dwc2_reset(struct udevice *dev)
1138{
1139 int ret;
1140 struct dwc2_priv *priv = dev_get_priv(dev);
1141
1142 ret = reset_get_bulk(dev, &priv->resets);
1143 if (ret) {
1144 dev_warn(dev, "Can't get reset: %d\n", ret);
1145 /* Return 0 if error due to !CONFIG_DM_RESET and reset
1146 * DT property is not present.
1147 */
1148 if (ret == -ENOENT || ret == -ENOTSUPP)
1149 return 0;
1150 else
1151 return ret;
1152 }
1153
66004381
PD
1154 /* force reset to clear all IP register */
1155 reset_assert_bulk(&priv->resets);
88c34b8d
LFT
1156 ret = reset_deassert_bulk(&priv->resets);
1157 if (ret) {
1158 reset_release_bulk(&priv->resets);
1159 dev_err(dev, "Failed to reset: %d\n", ret);
1160 return ret;
1161 }
1162
1163 return 0;
1164}
1165
5c735367 1166static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
6e9e0626 1167{
cc3e3a9e 1168 struct dwc2_core_regs *regs = priv->regs;
6e9e0626
OT
1169 uint32_t snpsid;
1170 int i, j;
88c34b8d
LFT
1171 int ret;
1172
1173 ret = dwc2_reset(dev);
1174 if (ret)
1175 return ret;
6e9e0626 1176
6e9e0626 1177 snpsid = readl(&regs->gsnpsid);
ac6c796c
PC
1178 dev_info(dev, "Core Release: %x.%03x\n",
1179 snpsid >> 12 & 0xf, snpsid & 0xfff);
6e9e0626 1180
5cfd6c00
PG
1181 if ((snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_2xx &&
1182 (snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_3xx) {
ac6c796c
PC
1183 dev_info(dev, "SNPSID invalid (not DWC2 OTG device): %08x\n",
1184 snpsid);
6e9e0626
OT
1185 return -ENODEV;
1186 }
1187
618da563
MV
1188#ifdef CONFIG_DWC2_PHY_ULPI_EXT_VBUS
1189 priv->ext_vbus = 1;
1190#else
1191 priv->ext_vbus = 0;
1192#endif
1193
55901989 1194 dwc_otg_core_init(priv);
5c735367 1195 dwc_otg_core_host_init(dev, regs);
6e9e0626
OT
1196
1197 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
1198 DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
1199 DWC2_HPRT0_PRTOVRCURRCHNG,
1200 DWC2_HPRT0_PRTRST);
1201 mdelay(50);
1202 clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET |
1203 DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG |
1204 DWC2_HPRT0_PRTRST);
1205
1206 for (i = 0; i < MAX_DEVICE; i++) {
25612f23
SB
1207 for (j = 0; j < MAX_ENDPOINT; j++) {
1208 priv->in_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1209 priv->out_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1210 }
6e9e0626
OT
1211 }
1212
2bf352f0
SR
1213 /*
1214 * Add a 1 second delay here. This gives the host controller
1215 * a bit time before the comminucation with the USB devices
1216 * is started (the bus is scanned) and fixes the USB detection
1217 * problems with some problematic USB keys.
1218 */
1219 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
1220 mdelay(1000);
1221
6e9e0626
OT
1222 return 0;
1223}
1224
cc3e3a9e 1225static void dwc2_uninit_common(struct dwc2_core_regs *regs)
6e9e0626
OT
1226{
1227 /* Put everything in reset. */
1228 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
1229 DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
1230 DWC2_HPRT0_PRTOVRCURRCHNG,
1231 DWC2_HPRT0_PRTRST);
cc3e3a9e
SG
1232}
1233
fd09c205 1234#if !CONFIG_IS_ENABLED(DM_USB)
cc3e3a9e
SG
1235int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1236 int len, struct devrequest *setup)
1237{
1238 return _submit_control_msg(&local, dev, pipe, buffer, len, setup);
1239}
1240
1241int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1242 int len)
1243{
1244 return _submit_bulk_msg(&local, dev, pipe, buffer, len);
1245}
1246
1247int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
3437121c 1248 int len, int interval, bool nonblock)
cc3e3a9e 1249{
3437121c
MS
1250 return _submit_int_msg(&local, dev, pipe, buffer, len, interval,
1251 nonblock);
cc3e3a9e
SG
1252}
1253
1254/* U-Boot USB control interface */
1255int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1256{
1257 struct dwc2_priv *priv = &local;
1258
1259 memset(priv, '\0', sizeof(*priv));
1260 priv->root_hub_devnum = 0;
1261 priv->regs = (struct dwc2_core_regs *)CONFIG_USB_DWC2_REG_ADDR;
1262 priv->aligned_buffer = aligned_buffer_addr;
1263 priv->status_buffer = status_buffer_addr;
1264
1265 /* board-dependant init */
1266 if (board_usb_init(index, USB_INIT_HOST))
1267 return -1;
1268
5c735367 1269 return dwc2_init_common(NULL, priv);
cc3e3a9e
SG
1270}
1271
1272int usb_lowlevel_stop(int index)
1273{
1274 dwc2_uninit_common(local.regs);
1275
6e9e0626
OT
1276 return 0;
1277}
f58a41e0
SG
1278#endif
1279
fd09c205 1280#if CONFIG_IS_ENABLED(DM_USB)
f58a41e0
SG
1281static int dwc2_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1282 unsigned long pipe, void *buffer, int length,
1283 struct devrequest *setup)
1284{
1285 struct dwc2_priv *priv = dev_get_priv(dev);
1286
1287 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1288 dev->name, udev, udev->dev->name, udev->portnr);
1289
1290 return _submit_control_msg(priv, udev, pipe, buffer, length, setup);
1291}
1292
1293static int dwc2_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1294 unsigned long pipe, void *buffer, int length)
1295{
1296 struct dwc2_priv *priv = dev_get_priv(dev);
1297
1298 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1299
1300 return _submit_bulk_msg(priv, udev, pipe, buffer, length);
1301}
1302
1303static int dwc2_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1304 unsigned long pipe, void *buffer, int length,
3437121c 1305 int interval, bool nonblock)
f58a41e0
SG
1306{
1307 struct dwc2_priv *priv = dev_get_priv(dev);
1308
1309 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1310
3437121c
MS
1311 return _submit_int_msg(priv, udev, pipe, buffer, length, interval,
1312 nonblock);
f58a41e0
SG
1313}
1314
1315static int dwc2_usb_ofdata_to_platdata(struct udevice *dev)
1316{
1317 struct dwc2_priv *priv = dev_get_priv(dev);
1318 fdt_addr_t addr;
1319
a9d3037a 1320 addr = dev_read_addr(dev);
f58a41e0
SG
1321 if (addr == FDT_ADDR_T_NONE)
1322 return -EINVAL;
1323 priv->regs = (struct dwc2_core_regs *)addr;
1324
dd22bace
MD
1325 priv->oc_disable = dev_read_bool(dev, "disable-over-current");
1326 priv->hnp_srp_disable = dev_read_bool(dev, "hnp-srp-disable");
c65a3494 1327
f58a41e0
SG
1328 return 0;
1329}
1330
e17a4bf1
PD
1331static int dwc2_setup_phy(struct udevice *dev)
1332{
1333 struct dwc2_priv *priv = dev_get_priv(dev);
1334 int ret;
1335
1336 ret = generic_phy_get_by_index(dev, 0, &priv->phy);
1337 if (ret) {
1338 if (ret == -ENOENT)
1339 return 0; /* no PHY, nothing to do */
1340 dev_err(dev, "Failed to get USB PHY: %d.\n", ret);
1341 return ret;
1342 }
1343
1344 ret = generic_phy_init(&priv->phy);
1345 if (ret) {
1346 dev_dbg(dev, "Failed to init USB PHY: %d.\n", ret);
1347 return ret;
1348 }
1349
1350 ret = generic_phy_power_on(&priv->phy);
1351 if (ret) {
1352 dev_dbg(dev, "Failed to power on USB PHY: %d.\n", ret);
1353 generic_phy_exit(&priv->phy);
1354 return ret;
1355 }
1356
1357 return 0;
1358}
1359
1360static int dwc2_shutdown_phy(struct udevice *dev)
1361{
1362 struct dwc2_priv *priv = dev_get_priv(dev);
1363 int ret;
1364
1365 /* PHY is not valid when generic_phy_get_by_index() = -ENOENT */
1366 if (!generic_phy_valid(&priv->phy))
1367 return 0; /* no PHY, nothing to do */
1368
1369 ret = generic_phy_power_off(&priv->phy);
1370 if (ret) {
1371 dev_dbg(dev, "Failed to power off USB PHY: %d.\n", ret);
1372 return ret;
1373 }
1374
1375 ret = generic_phy_exit(&priv->phy);
1376 if (ret) {
1377 dev_dbg(dev, "Failed to power off USB PHY: %d.\n", ret);
1378 return ret;
1379 }
1380
1381 return 0;
1382}
1383
0bc632c9
PD
1384static int dwc2_clk_init(struct udevice *dev)
1385{
1386 struct dwc2_priv *priv = dev_get_priv(dev);
1387 int ret;
1388
1389 ret = clk_get_bulk(dev, &priv->clks);
1390 if (ret == -ENOSYS || ret == -ENOENT)
1391 return 0;
1392 if (ret)
1393 return ret;
1394
1395 ret = clk_enable_bulk(&priv->clks);
1396 if (ret) {
1397 clk_release_bulk(&priv->clks);
1398 return ret;
1399 }
1400
1401 return 0;
1402}
1403
f58a41e0
SG
1404static int dwc2_usb_probe(struct udevice *dev)
1405{
1406 struct dwc2_priv *priv = dev_get_priv(dev);
e96e064f 1407 struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
e17a4bf1 1408 int ret;
e96e064f
MV
1409
1410 bus_priv->desc_before_addr = true;
f58a41e0 1411
0bc632c9
PD
1412 ret = dwc2_clk_init(dev);
1413 if (ret)
1414 return ret;
1415
e17a4bf1
PD
1416 ret = dwc2_setup_phy(dev);
1417 if (ret)
1418 return ret;
1419
5c735367 1420 return dwc2_init_common(dev, priv);
f58a41e0
SG
1421}
1422
1423static int dwc2_usb_remove(struct udevice *dev)
1424{
1425 struct dwc2_priv *priv = dev_get_priv(dev);
82e7975b
CK
1426 int ret;
1427
1428 ret = dwc_vbus_supply_exit(dev);
1429 if (ret)
1430 return ret;
f58a41e0 1431
e17a4bf1
PD
1432 ret = dwc2_shutdown_phy(dev);
1433 if (ret) {
1434 dev_dbg(dev, "Failed to shutdown USB PHY: %d.\n", ret);
1435 return ret;
1436 }
1437
f58a41e0
SG
1438 dwc2_uninit_common(priv->regs);
1439
88c34b8d 1440 reset_release_bulk(&priv->resets);
0bc632c9
PD
1441 clk_disable_bulk(&priv->clks);
1442 clk_release_bulk(&priv->clks);
88c34b8d 1443
f58a41e0
SG
1444 return 0;
1445}
1446
1447struct dm_usb_ops dwc2_usb_ops = {
1448 .control = dwc2_submit_control_msg,
1449 .bulk = dwc2_submit_bulk_msg,
1450 .interrupt = dwc2_submit_int_msg,
1451};
1452
1453static const struct udevice_id dwc2_usb_ids[] = {
1454 { .compatible = "brcm,bcm2835-usb" },
ff5d5cc2 1455 { .compatible = "brcm,bcm2708-usb" },
f522f947 1456 { .compatible = "snps,dwc2" },
f58a41e0
SG
1457 { }
1458};
1459
1460U_BOOT_DRIVER(usb_dwc2) = {
7a1386f9 1461 .name = "dwc2_usb",
f58a41e0
SG
1462 .id = UCLASS_USB,
1463 .of_match = dwc2_usb_ids,
1464 .ofdata_to_platdata = dwc2_usb_ofdata_to_platdata,
1465 .probe = dwc2_usb_probe,
1466 .remove = dwc2_usb_remove,
1467 .ops = &dwc2_usb_ops,
1468 .priv_auto_alloc_size = sizeof(struct dwc2_priv),
1469 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1470};
1471#endif
This page took 0.55773 seconds and 4 git commands to generate.