blob: 5f4ca7abf7bf76f282b992639f09779eb629c924 [file] [log] [blame]
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001/*
2 * Copyright (C) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
3 * Copyright (C) 2014 Marek Vasut <marex@denx.de>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <errno.h>
10#include <usb.h>
11#include <malloc.h>
12#include <usbroothubdes.h>
13#include <asm/io.h>
14
15#include "dwc2.h"
16
17/* Use only HC channel 0. */
18#define DWC2_HC_CHANNEL 0
19
20#define DWC2_STATUS_BUF_SIZE 64
21#define DWC2_DATA_BUF_SIZE (64 * 1024)
22
23/* We need doubleword-aligned buffers for DMA transfers */
24DEFINE_ALIGN_BUFFER(uint8_t, aligned_buffer, DWC2_DATA_BUF_SIZE, 8);
25DEFINE_ALIGN_BUFFER(uint8_t, status_buffer, DWC2_STATUS_BUF_SIZE, 8);
26
27#define MAX_DEVICE 16
28#define MAX_ENDPOINT 16
29static int bulk_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070030
31static int root_hub_devnum;
32
33static struct dwc2_core_regs *regs =
34 (struct dwc2_core_regs *)CONFIG_USB_DWC2_REG_ADDR;
35
36/*
37 * DWC2 IP interface
38 */
39static int wait_for_bit(void *reg, const uint32_t mask, bool set)
40{
41 unsigned int timeout = 1000000;
42 uint32_t val;
43
44 while (--timeout) {
45 val = readl(reg);
46 if (!set)
47 val = ~val;
48
49 if ((val & mask) == mask)
50 return 0;
51
52 udelay(1);
53 }
54
55 debug("%s: Timeout (reg=%p mask=%08x wait_set=%i)\n",
56 __func__, reg, mask, set);
57
58 return -ETIMEDOUT;
59}
60
61/*
62 * Initializes the FSLSPClkSel field of the HCFG register
63 * depending on the PHY type.
64 */
65static void init_fslspclksel(struct dwc2_core_regs *regs)
66{
67 uint32_t phyclk;
68
69#if (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
70 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
71#else
72 /* High speed PHY running at full speed or high speed */
73 phyclk = DWC2_HCFG_FSLSPCLKSEL_30_60_MHZ;
74#endif
75
76#ifdef CONFIG_DWC2_ULPI_FS_LS
77 uint32_t hwcfg2 = readl(&regs->ghwcfg2);
78 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
79 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
80 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
81 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
82
83 if (hval == 2 && fval == 1)
84 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
85#endif
86
87 clrsetbits_le32(&regs->host_regs.hcfg,
88 DWC2_HCFG_FSLSPCLKSEL_MASK,
89 phyclk << DWC2_HCFG_FSLSPCLKSEL_OFFSET);
90}
91
92/*
93 * Flush a Tx FIFO.
94 *
95 * @param regs Programming view of DWC_otg controller.
96 * @param num Tx FIFO to flush.
97 */
98static void dwc_otg_flush_tx_fifo(struct dwc2_core_regs *regs, const int num)
99{
100 int ret;
101
102 writel(DWC2_GRSTCTL_TXFFLSH | (num << DWC2_GRSTCTL_TXFNUM_OFFSET),
103 &regs->grstctl);
104 ret = wait_for_bit(&regs->grstctl, DWC2_GRSTCTL_TXFFLSH, 0);
105 if (ret)
106 printf("%s: Timeout!\n", __func__);
107
108 /* Wait for 3 PHY Clocks */
109 udelay(1);
110}
111
112/*
113 * Flush Rx FIFO.
114 *
115 * @param regs Programming view of DWC_otg controller.
116 */
117static void dwc_otg_flush_rx_fifo(struct dwc2_core_regs *regs)
118{
119 int ret;
120
121 writel(DWC2_GRSTCTL_RXFFLSH, &regs->grstctl);
122 ret = wait_for_bit(&regs->grstctl, DWC2_GRSTCTL_RXFFLSH, 0);
123 if (ret)
124 printf("%s: Timeout!\n", __func__);
125
126 /* Wait for 3 PHY Clocks */
127 udelay(1);
128}
129
130/*
131 * Do core a soft reset of the core. Be careful with this because it
132 * resets all the internal state machines of the core.
133 */
134static void dwc_otg_core_reset(struct dwc2_core_regs *regs)
135{
136 int ret;
137
138 /* Wait for AHB master IDLE state. */
139 ret = wait_for_bit(&regs->grstctl, DWC2_GRSTCTL_AHBIDLE, 1);
140 if (ret)
141 printf("%s: Timeout!\n", __func__);
142
143 /* Core Soft Reset */
144 writel(DWC2_GRSTCTL_CSFTRST, &regs->grstctl);
145 ret = wait_for_bit(&regs->grstctl, DWC2_GRSTCTL_CSFTRST, 0);
146 if (ret)
147 printf("%s: Timeout!\n", __func__);
148
149 /*
150 * Wait for core to come out of reset.
151 * NOTE: This long sleep is _very_ important, otherwise the core will
152 * not stay in host mode after a connector ID change!
153 */
154 mdelay(100);
155}
156
157/*
158 * This function initializes the DWC_otg controller registers for
159 * host mode.
160 *
161 * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
162 * request queues. Host channels are reset to ensure that they are ready for
163 * performing transfers.
164 *
165 * @param regs Programming view of DWC_otg controller
166 *
167 */
168static void dwc_otg_core_host_init(struct dwc2_core_regs *regs)
169{
170 uint32_t nptxfifosize = 0;
171 uint32_t ptxfifosize = 0;
172 uint32_t hprt0 = 0;
173 int i, ret, num_channels;
174
175 /* Restart the Phy Clock */
176 writel(0, &regs->pcgcctl);
177
178 /* Initialize Host Configuration Register */
179 init_fslspclksel(regs);
180#ifdef CONFIG_DWC2_DFLT_SPEED_FULL
181 setbits_le32(&regs->host_regs.hcfg, DWC2_HCFG_FSLSSUPP);
182#endif
183
184 /* Configure data FIFO sizes */
185#ifdef CONFIG_DWC2_ENABLE_DYNAMIC_FIFO
186 if (readl(&regs->ghwcfg2) & DWC2_HWCFG2_DYNAMIC_FIFO) {
187 /* Rx FIFO */
188 writel(CONFIG_DWC2_HOST_RX_FIFO_SIZE, &regs->grxfsiz);
189
190 /* Non-periodic Tx FIFO */
191 nptxfifosize |= CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE <<
192 DWC2_FIFOSIZE_DEPTH_OFFSET;
193 nptxfifosize |= CONFIG_DWC2_HOST_RX_FIFO_SIZE <<
194 DWC2_FIFOSIZE_STARTADDR_OFFSET;
195 writel(nptxfifosize, &regs->gnptxfsiz);
196
197 /* Periodic Tx FIFO */
198 ptxfifosize |= CONFIG_DWC2_HOST_PERIO_TX_FIFO_SIZE <<
199 DWC2_FIFOSIZE_DEPTH_OFFSET;
200 ptxfifosize |= (CONFIG_DWC2_HOST_RX_FIFO_SIZE +
201 CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE) <<
202 DWC2_FIFOSIZE_STARTADDR_OFFSET;
203 writel(ptxfifosize, &regs->hptxfsiz);
204 }
205#endif
206
207 /* Clear Host Set HNP Enable in the OTG Control Register */
208 clrbits_le32(&regs->gotgctl, DWC2_GOTGCTL_HSTSETHNPEN);
209
210 /* Make sure the FIFOs are flushed. */
211 dwc_otg_flush_tx_fifo(regs, 0x10); /* All Tx FIFOs */
212 dwc_otg_flush_rx_fifo(regs);
213
214 /* Flush out any leftover queued requests. */
215 num_channels = readl(&regs->ghwcfg2);
216 num_channels &= DWC2_HWCFG2_NUM_HOST_CHAN_MASK;
217 num_channels >>= DWC2_HWCFG2_NUM_HOST_CHAN_OFFSET;
218 num_channels += 1;
219
220 for (i = 0; i < num_channels; i++)
221 clrsetbits_le32(&regs->hc_regs[i].hcchar,
222 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_EPDIR,
223 DWC2_HCCHAR_CHDIS);
224
225 /* Halt all channels to put them into a known state. */
226 for (i = 0; i < num_channels; i++) {
227 clrsetbits_le32(&regs->hc_regs[i].hcchar,
228 DWC2_HCCHAR_EPDIR,
229 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS);
230 ret = wait_for_bit(&regs->hc_regs[i].hcchar,
231 DWC2_HCCHAR_CHEN, 0);
232 if (ret)
233 printf("%s: Timeout!\n", __func__);
234 }
235
236 /* Turn on the vbus power. */
237 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST) {
238 hprt0 = readl(&regs->hprt0);
239 hprt0 &= ~(DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET);
240 hprt0 &= ~(DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG);
241 if (!(hprt0 & DWC2_HPRT0_PRTPWR)) {
242 hprt0 |= DWC2_HPRT0_PRTPWR;
243 writel(hprt0, &regs->hprt0);
244 }
245 }
246}
247
248/*
249 * This function initializes the DWC_otg controller registers and
250 * prepares the core for device mode or host mode operation.
251 *
252 * @param regs Programming view of the DWC_otg controller
253 */
254static void dwc_otg_core_init(struct dwc2_core_regs *regs)
255{
256 uint32_t ahbcfg = 0;
257 uint32_t usbcfg = 0;
258 uint8_t brst_sz = CONFIG_DWC2_DMA_BURST_SIZE;
259
260 /* Common Initialization */
261 usbcfg = readl(&regs->gusbcfg);
262
263 /* Program the ULPI External VBUS bit if needed */
264#ifdef CONFIG_DWC2_PHY_ULPI_EXT_VBUS
265 usbcfg |= DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
266#else
267 usbcfg &= ~DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
268#endif
269
270 /* Set external TS Dline pulsing */
271#ifdef CONFIG_DWC2_TS_DLINE
272 usbcfg |= DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
273#else
274 usbcfg &= ~DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
275#endif
276 writel(usbcfg, &regs->gusbcfg);
277
278 /* Reset the Controller */
279 dwc_otg_core_reset(regs);
280
281 /*
282 * This programming sequence needs to happen in FS mode before
283 * any other programming occurs
284 */
285#if defined(CONFIG_DWC2_DFLT_SPEED_FULL) && \
286 (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
287 /* If FS mode with FS PHY */
288 setbits_le32(&regs->gusbcfg, DWC2_GUSBCFG_PHYSEL);
289
290 /* Reset after a PHY select */
291 dwc_otg_core_reset(regs);
292
293 /*
294 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS.
295 * Also do this on HNP Dev/Host mode switches (done in dev_init
296 * and host_init).
297 */
298 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
299 init_fslspclksel(regs);
300
301#ifdef CONFIG_DWC2_I2C_ENABLE
302 /* Program GUSBCFG.OtgUtmifsSel to I2C */
303 setbits_le32(&regs->gusbcfg, DWC2_GUSBCFG_OTGUTMIFSSEL);
304
305 /* Program GI2CCTL.I2CEn */
306 clrsetbits_le32(&regs->gi2cctl, DWC2_GI2CCTL_I2CEN |
307 DWC2_GI2CCTL_I2CDEVADDR_MASK,
308 1 << DWC2_GI2CCTL_I2CDEVADDR_OFFSET);
309 setbits_le32(&regs->gi2cctl, DWC2_GI2CCTL_I2CEN);
310#endif
311
312#else
313 /* High speed PHY. */
314
315 /*
316 * HS PHY parameters. These parameters are preserved during
317 * soft reset so only program the first time. Do a soft reset
318 * immediately after setting phyif.
319 */
320 usbcfg &= ~(DWC2_GUSBCFG_ULPI_UTMI_SEL | DWC2_GUSBCFG_PHYIF);
321 usbcfg |= CONFIG_DWC2_PHY_TYPE << DWC2_GUSBCFG_ULPI_UTMI_SEL_OFFSET;
322
323 if (usbcfg & DWC2_GUSBCFG_ULPI_UTMI_SEL) { /* ULPI interface */
324#ifdef CONFIG_DWC2_PHY_ULPI_DDR
325 usbcfg |= DWC2_GUSBCFG_DDRSEL;
326#else
327 usbcfg &= ~DWC2_GUSBCFG_DDRSEL;
328#endif
329 } else { /* UTMI+ interface */
330#if (CONFIG_DWC2_UTMI_PHY_WIDTH == 16)
331 usbcfg |= DWC2_GUSBCFG_PHYIF;
332#endif
333 }
334
335 writel(usbcfg, &regs->gusbcfg);
336
337 /* Reset after setting the PHY parameters */
338 dwc_otg_core_reset(regs);
339#endif
340
341 usbcfg = readl(&regs->gusbcfg);
342 usbcfg &= ~(DWC2_GUSBCFG_ULPI_FSLS | DWC2_GUSBCFG_ULPI_CLK_SUS_M);
343#ifdef CONFIG_DWC2_ULPI_FS_LS
344 uint32_t hwcfg2 = readl(&regs->ghwcfg2);
345 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
346 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
347 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
348 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
349 if (hval == 2 && fval == 1) {
350 usbcfg |= DWC2_GUSBCFG_ULPI_FSLS;
351 usbcfg |= DWC2_GUSBCFG_ULPI_CLK_SUS_M;
352 }
353#endif
354 writel(usbcfg, &regs->gusbcfg);
355
356 /* Program the GAHBCFG Register. */
357 switch (readl(&regs->ghwcfg2) & DWC2_HWCFG2_ARCHITECTURE_MASK) {
358 case DWC2_HWCFG2_ARCHITECTURE_SLAVE_ONLY:
359 break;
360 case DWC2_HWCFG2_ARCHITECTURE_EXT_DMA:
361 while (brst_sz > 1) {
362 ahbcfg |= ahbcfg + (1 << DWC2_GAHBCFG_HBURSTLEN_OFFSET);
363 ahbcfg &= DWC2_GAHBCFG_HBURSTLEN_MASK;
364 brst_sz >>= 1;
365 }
366
367#ifdef CONFIG_DWC2_DMA_ENABLE
368 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
369#endif
370 break;
371
372 case DWC2_HWCFG2_ARCHITECTURE_INT_DMA:
373 ahbcfg |= DWC2_GAHBCFG_HBURSTLEN_INCR4;
374#ifdef CONFIG_DWC2_DMA_ENABLE
375 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
376#endif
377 break;
378 }
379
380 writel(ahbcfg, &regs->gahbcfg);
381
382 /* Program the GUSBCFG register for HNP/SRP. */
383 setbits_le32(&regs->gusbcfg, DWC2_GUSBCFG_HNPCAP | DWC2_GUSBCFG_SRPCAP);
384
385#ifdef CONFIG_DWC2_IC_USB_CAP
386 setbits_le32(&regs->gusbcfg, DWC2_GUSBCFG_IC_USB_CAP);
387#endif
388}
389
390/*
391 * Prepares a host channel for transferring packets to/from a specific
392 * endpoint. The HCCHARn register is set up with the characteristics specified
393 * in _hc. Host channel interrupts that may need to be serviced while this
394 * transfer is in progress are enabled.
395 *
396 * @param regs Programming view of DWC_otg controller
397 * @param hc Information needed to initialize the host channel
398 */
399static void dwc_otg_hc_init(struct dwc2_core_regs *regs, uint8_t hc_num,
400 uint8_t dev_addr, uint8_t ep_num, uint8_t ep_is_in,
401 uint8_t ep_type, uint16_t max_packet)
402{
403 struct dwc2_hc_regs *hc_regs = &regs->hc_regs[hc_num];
404 const uint32_t hcchar = (dev_addr << DWC2_HCCHAR_DEVADDR_OFFSET) |
405 (ep_num << DWC2_HCCHAR_EPNUM_OFFSET) |
406 (ep_is_in << DWC2_HCCHAR_EPDIR_OFFSET) |
407 (ep_type << DWC2_HCCHAR_EPTYPE_OFFSET) |
408 (max_packet << DWC2_HCCHAR_MPS_OFFSET);
409
410 /* Clear old interrupt conditions for this host channel. */
411 writel(0x3fff, &hc_regs->hcint);
412
413 /*
414 * Program the HCCHARn register with the endpoint characteristics
415 * for the current transfer.
416 */
417 writel(hcchar, &hc_regs->hcchar);
418
419 /* Program the HCSPLIT register for SPLITs */
420 writel(0, &hc_regs->hcsplt);
421}
422
423/*
424 * DWC2 to USB API interface
425 */
426/* Direction: In ; Request: Status */
427static int dwc_otg_submit_rh_msg_in_status(struct usb_device *dev, void *buffer,
428 int txlen, struct devrequest *cmd)
429{
430 uint32_t hprt0 = 0;
431 uint32_t port_status = 0;
432 uint32_t port_change = 0;
433 int len = 0;
434 int stat = 0;
435
436 switch (cmd->requesttype & ~USB_DIR_IN) {
437 case 0:
438 *(uint16_t *)buffer = cpu_to_le16(1);
439 len = 2;
440 break;
441 case USB_RECIP_INTERFACE:
442 case USB_RECIP_ENDPOINT:
443 *(uint16_t *)buffer = cpu_to_le16(0);
444 len = 2;
445 break;
446 case USB_TYPE_CLASS:
447 *(uint32_t *)buffer = cpu_to_le32(0);
448 len = 4;
449 break;
450 case USB_RECIP_OTHER | USB_TYPE_CLASS:
451 hprt0 = readl(&regs->hprt0);
452 if (hprt0 & DWC2_HPRT0_PRTCONNSTS)
453 port_status |= USB_PORT_STAT_CONNECTION;
454 if (hprt0 & DWC2_HPRT0_PRTENA)
455 port_status |= USB_PORT_STAT_ENABLE;
456 if (hprt0 & DWC2_HPRT0_PRTSUSP)
457 port_status |= USB_PORT_STAT_SUSPEND;
458 if (hprt0 & DWC2_HPRT0_PRTOVRCURRACT)
459 port_status |= USB_PORT_STAT_OVERCURRENT;
460 if (hprt0 & DWC2_HPRT0_PRTRST)
461 port_status |= USB_PORT_STAT_RESET;
462 if (hprt0 & DWC2_HPRT0_PRTPWR)
463 port_status |= USB_PORT_STAT_POWER;
464
465 port_status |= USB_PORT_STAT_HIGH_SPEED;
466
467 if (hprt0 & DWC2_HPRT0_PRTENCHNG)
468 port_change |= USB_PORT_STAT_C_ENABLE;
469 if (hprt0 & DWC2_HPRT0_PRTCONNDET)
470 port_change |= USB_PORT_STAT_C_CONNECTION;
471 if (hprt0 & DWC2_HPRT0_PRTOVRCURRCHNG)
472 port_change |= USB_PORT_STAT_C_OVERCURRENT;
473
474 *(uint32_t *)buffer = cpu_to_le32(port_status |
475 (port_change << 16));
476 len = 4;
477 break;
478 default:
479 puts("unsupported root hub command\n");
480 stat = USB_ST_STALLED;
481 }
482
483 dev->act_len = min(len, txlen);
484 dev->status = stat;
485
486 return stat;
487}
488
489/* Direction: In ; Request: Descriptor */
490static int dwc_otg_submit_rh_msg_in_descriptor(struct usb_device *dev,
491 void *buffer, int txlen,
492 struct devrequest *cmd)
493{
494 unsigned char data[32];
495 uint32_t dsc;
496 int len = 0;
497 int stat = 0;
498 uint16_t wValue = cpu_to_le16(cmd->value);
499 uint16_t wLength = cpu_to_le16(cmd->length);
500
501 switch (cmd->requesttype & ~USB_DIR_IN) {
502 case 0:
503 switch (wValue & 0xff00) {
504 case 0x0100: /* device descriptor */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900505 len = min3(txlen, (int)sizeof(root_hub_dev_des), (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700506 memcpy(buffer, root_hub_dev_des, len);
507 break;
508 case 0x0200: /* configuration descriptor */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900509 len = min3(txlen, (int)sizeof(root_hub_config_des), (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700510 memcpy(buffer, root_hub_config_des, len);
511 break;
512 case 0x0300: /* string descriptors */
513 switch (wValue & 0xff) {
514 case 0x00:
Masahiro Yamadadb204642014-11-07 03:03:31 +0900515 len = min3(txlen, (int)sizeof(root_hub_str_index0),
516 (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700517 memcpy(buffer, root_hub_str_index0, len);
518 break;
519 case 0x01:
Masahiro Yamadadb204642014-11-07 03:03:31 +0900520 len = min3(txlen, (int)sizeof(root_hub_str_index1),
521 (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700522 memcpy(buffer, root_hub_str_index1, len);
523 break;
524 }
525 break;
526 default:
527 stat = USB_ST_STALLED;
528 }
529 break;
530
531 case USB_TYPE_CLASS:
532 /* Root port config, set 1 port and nothing else. */
533 dsc = 0x00000001;
534
535 data[0] = 9; /* min length; */
536 data[1] = 0x29;
537 data[2] = dsc & RH_A_NDP;
538 data[3] = 0;
539 if (dsc & RH_A_PSM)
540 data[3] |= 0x1;
541 if (dsc & RH_A_NOCP)
542 data[3] |= 0x10;
543 else if (dsc & RH_A_OCPM)
544 data[3] |= 0x8;
545
546 /* corresponds to data[4-7] */
547 data[5] = (dsc & RH_A_POTPGT) >> 24;
548 data[7] = dsc & RH_B_DR;
549 if (data[2] < 7) {
550 data[8] = 0xff;
551 } else {
552 data[0] += 2;
553 data[8] = (dsc & RH_B_DR) >> 8;
554 data[9] = 0xff;
555 data[10] = data[9];
556 }
557
Masahiro Yamadadb204642014-11-07 03:03:31 +0900558 len = min3(txlen, (int)data[0], (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700559 memcpy(buffer, data, len);
560 break;
561 default:
562 puts("unsupported root hub command\n");
563 stat = USB_ST_STALLED;
564 }
565
566 dev->act_len = min(len, txlen);
567 dev->status = stat;
568
569 return stat;
570}
571
572/* Direction: In ; Request: Configuration */
573static int dwc_otg_submit_rh_msg_in_configuration(struct usb_device *dev,
574 void *buffer, int txlen,
575 struct devrequest *cmd)
576{
577 int len = 0;
578 int stat = 0;
579
580 switch (cmd->requesttype & ~USB_DIR_IN) {
581 case 0:
582 *(uint8_t *)buffer = 0x01;
583 len = 1;
584 break;
585 default:
586 puts("unsupported root hub command\n");
587 stat = USB_ST_STALLED;
588 }
589
590 dev->act_len = min(len, txlen);
591 dev->status = stat;
592
593 return stat;
594}
595
596/* Direction: In */
597static int dwc_otg_submit_rh_msg_in(struct usb_device *dev,
598 void *buffer, int txlen,
599 struct devrequest *cmd)
600{
601 switch (cmd->request) {
602 case USB_REQ_GET_STATUS:
603 return dwc_otg_submit_rh_msg_in_status(dev, buffer,
604 txlen, cmd);
605 case USB_REQ_GET_DESCRIPTOR:
606 return dwc_otg_submit_rh_msg_in_descriptor(dev, buffer,
607 txlen, cmd);
608 case USB_REQ_GET_CONFIGURATION:
609 return dwc_otg_submit_rh_msg_in_configuration(dev, buffer,
610 txlen, cmd);
611 default:
612 puts("unsupported root hub command\n");
613 return USB_ST_STALLED;
614 }
615}
616
617/* Direction: Out */
618static int dwc_otg_submit_rh_msg_out(struct usb_device *dev,
619 void *buffer, int txlen,
620 struct devrequest *cmd)
621{
622 int len = 0;
623 int stat = 0;
624 uint16_t bmrtype_breq = cmd->requesttype | (cmd->request << 8);
625 uint16_t wValue = cpu_to_le16(cmd->value);
626
627 switch (bmrtype_breq & ~USB_DIR_IN) {
628 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_ENDPOINT:
629 case (USB_REQ_CLEAR_FEATURE << 8) | USB_TYPE_CLASS:
630 break;
631
632 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
633 switch (wValue) {
634 case USB_PORT_FEAT_C_CONNECTION:
635 setbits_le32(&regs->hprt0, DWC2_HPRT0_PRTCONNDET);
636 break;
637 }
638 break;
639
640 case (USB_REQ_SET_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
641 switch (wValue) {
642 case USB_PORT_FEAT_SUSPEND:
643 break;
644
645 case USB_PORT_FEAT_RESET:
646 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
647 DWC2_HPRT0_PRTCONNDET |
648 DWC2_HPRT0_PRTENCHNG |
649 DWC2_HPRT0_PRTOVRCURRCHNG,
650 DWC2_HPRT0_PRTRST);
651 mdelay(50);
652 clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTRST);
653 break;
654
655 case USB_PORT_FEAT_POWER:
656 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
657 DWC2_HPRT0_PRTCONNDET |
658 DWC2_HPRT0_PRTENCHNG |
659 DWC2_HPRT0_PRTOVRCURRCHNG,
660 DWC2_HPRT0_PRTRST);
661 break;
662
663 case USB_PORT_FEAT_ENABLE:
664 break;
665 }
666 break;
667 case (USB_REQ_SET_ADDRESS << 8):
668 root_hub_devnum = wValue;
669 break;
670 case (USB_REQ_SET_CONFIGURATION << 8):
671 break;
672 default:
673 puts("unsupported root hub command\n");
674 stat = USB_ST_STALLED;
675 }
676
677 len = min(len, txlen);
678
679 dev->act_len = len;
680 dev->status = stat;
681
682 return stat;
683}
684
685static int dwc_otg_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
686 void *buffer, int txlen,
687 struct devrequest *cmd)
688{
689 int stat = 0;
690
691 if (usb_pipeint(pipe)) {
692 puts("Root-Hub submit IRQ: NOT implemented\n");
693 return 0;
694 }
695
696 if (cmd->requesttype & USB_DIR_IN)
697 stat = dwc_otg_submit_rh_msg_in(dev, buffer, txlen, cmd);
698 else
699 stat = dwc_otg_submit_rh_msg_out(dev, buffer, txlen, cmd);
700
701 mdelay(1);
702
703 return stat;
704}
705
Stephen Warrenb7dcaf02015-03-23 23:01:01 -0600706int wait_for_chhltd(uint32_t *sub, int *toggle, bool ignore_ack)
Stephen Warren8a346662015-03-07 22:48:51 -0700707{
Stephen Warrenb7dcaf02015-03-23 23:01:01 -0600708 uint32_t hcint_comp_hlt_ack = DWC2_HCINT_XFERCOMP | DWC2_HCINT_CHHLTD;
Stephen Warren8a346662015-03-07 22:48:51 -0700709 struct dwc2_hc_regs *hc_regs = &regs->hc_regs[DWC2_HC_CHANNEL];
710 int ret;
711 uint32_t hcint, hctsiz;
712
713 ret = wait_for_bit(&hc_regs->hcint, DWC2_HCINT_CHHLTD, true);
714 if (ret)
715 return ret;
716
717 hcint = readl(&hc_regs->hcint);
Stephen Warrenb7dcaf02015-03-23 23:01:01 -0600718 if (ignore_ack)
719 hcint &= ~DWC2_HCINT_ACK;
720 else
721 hcint_comp_hlt_ack |= DWC2_HCINT_ACK;
Stephen Warren8a346662015-03-07 22:48:51 -0700722 if (hcint != hcint_comp_hlt_ack) {
723 debug("%s: Error (HCINT=%08x)\n", __func__, hcint);
724 return -EINVAL;
725 }
726
727 hctsiz = readl(&hc_regs->hctsiz);
728 *sub = (hctsiz & DWC2_HCTSIZ_XFERSIZE_MASK) >>
729 DWC2_HCTSIZ_XFERSIZE_OFFSET;
Stephen Warren9f80e742015-03-07 22:48:55 -0700730 *toggle = (hctsiz & DWC2_HCTSIZ_PID_MASK) >> DWC2_HCTSIZ_PID_OFFSET;
Stephen Warren8a346662015-03-07 22:48:51 -0700731
Stephen Warren9f80e742015-03-07 22:48:55 -0700732 debug("%s: sub=%u toggle=%d\n", __func__, *sub, *toggle);
Stephen Warren8a346662015-03-07 22:48:51 -0700733
734 return 0;
735}
736
Stephen Warren972ad642015-03-07 22:48:52 -0700737static int dwc2_eptype[] = {
738 DWC2_HCCHAR_EPTYPE_ISOC,
739 DWC2_HCCHAR_EPTYPE_INTR,
740 DWC2_HCCHAR_EPTYPE_CONTROL,
741 DWC2_HCCHAR_EPTYPE_BULK,
742};
743
744int chunk_msg(struct usb_device *dev, unsigned long pipe, int *pid, int in,
Stephen Warrenb7dcaf02015-03-23 23:01:01 -0600745 void *buffer, int len, bool ignore_ack)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700746{
Stephen Warren972ad642015-03-07 22:48:52 -0700747 struct dwc2_hc_regs *hc_regs = &regs->hc_regs[DWC2_HC_CHANNEL];
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700748 int devnum = usb_pipedevice(pipe);
749 int ep = usb_pipeendpoint(pipe);
750 int max = usb_maxpacket(dev, pipe);
Stephen Warren972ad642015-03-07 22:48:52 -0700751 int eptype = dwc2_eptype[usb_pipetype(pipe)];
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700752 int done = 0;
Stephen Warren8a346662015-03-07 22:48:51 -0700753 int ret;
754 uint32_t sub;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700755 uint32_t xfer_len;
756 uint32_t num_packets;
757 int stop_transfer = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700758
Stephen Warren972ad642015-03-07 22:48:52 -0700759 debug("%s: msg: pipe %lx pid %d in %d len %d\n", __func__, pipe, *pid,
760 in, len);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700761
Stephen Warren4db200e2015-03-07 22:48:53 -0700762 do {
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700763 /* Initialize channel */
Stephen Warren972ad642015-03-07 22:48:52 -0700764 dwc_otg_hc_init(regs, DWC2_HC_CHANNEL, devnum, ep, in, eptype,
765 max);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700766
767 xfer_len = len - done;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700768 if (xfer_len > CONFIG_DWC2_MAX_TRANSFER_SIZE)
769 xfer_len = CONFIG_DWC2_MAX_TRANSFER_SIZE - max + 1;
Stephen Warrenc12885c2015-03-08 11:08:14 -0600770 if (xfer_len > DWC2_DATA_BUF_SIZE)
771 xfer_len = DWC2_DATA_BUF_SIZE - max + 1;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700772
Stephen Warrenc12885c2015-03-08 11:08:14 -0600773 /* Make sure that xfer_len is a multiple of max packet size. */
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700774 if (xfer_len > 0) {
775 num_packets = (xfer_len + max - 1) / max;
776 if (num_packets > CONFIG_DWC2_MAX_PACKET_COUNT) {
777 num_packets = CONFIG_DWC2_MAX_PACKET_COUNT;
778 xfer_len = num_packets * max;
779 }
780 } else {
781 num_packets = 1;
782 }
783
Stephen Warren972ad642015-03-07 22:48:52 -0700784 if (in)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700785 xfer_len = num_packets * max;
786
Stephen Warren972ad642015-03-07 22:48:52 -0700787 debug("%s: chunk: pid %d xfer_len %u pkts %u\n", __func__,
788 *pid, xfer_len, num_packets);
789
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700790 writel((xfer_len << DWC2_HCTSIZ_XFERSIZE_OFFSET) |
791 (num_packets << DWC2_HCTSIZ_PKTCNT_OFFSET) |
Stephen Warren972ad642015-03-07 22:48:52 -0700792 (*pid << DWC2_HCTSIZ_PID_OFFSET),
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700793 &hc_regs->hctsiz);
794
Stephen Warren7100da32015-03-08 11:08:13 -0600795 if (!in)
796 memcpy(aligned_buffer, (char *)buffer + done, len);
797
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700798 writel((uint32_t)aligned_buffer, &hc_regs->hcdma);
799
800 /* Set host channel enable after all other setup is complete. */
801 clrsetbits_le32(&hc_regs->hcchar, DWC2_HCCHAR_MULTICNT_MASK |
802 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS,
803 (1 << DWC2_HCCHAR_MULTICNT_OFFSET) |
804 DWC2_HCCHAR_CHEN);
805
Stephen Warrenb7dcaf02015-03-23 23:01:01 -0600806 ret = wait_for_chhltd(&sub, pid, ignore_ack);
Stephen Warren8a346662015-03-07 22:48:51 -0700807 if (ret) {
808 stop_transfer = 1;
809 break;
810 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700811
Stephen Warren972ad642015-03-07 22:48:52 -0700812 if (in) {
Stephen Warren7100da32015-03-08 11:08:13 -0600813 xfer_len -= sub;
814 memcpy(buffer + done, aligned_buffer, xfer_len);
Stephen Warren8a346662015-03-07 22:48:51 -0700815 if (sub)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700816 stop_transfer = 1;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700817 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700818
Stephen Warren7100da32015-03-08 11:08:13 -0600819 done += xfer_len;
820
821 } while ((done < len) && !stop_transfer);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700822
823 writel(0, &hc_regs->hcintmsk);
824 writel(0xFFFFFFFF, &hc_regs->hcint);
825
826 dev->status = 0;
827 dev->act_len = done;
828
829 return 0;
830}
831
Stephen Warren972ad642015-03-07 22:48:52 -0700832/* U-Boot USB transmission interface */
833int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
834 int len)
835{
836 int devnum = usb_pipedevice(pipe);
837 int ep = usb_pipeendpoint(pipe);
838
839 if (devnum == root_hub_devnum) {
840 dev->status = 0;
841 return -EINVAL;
842 }
843
844 return chunk_msg(dev, pipe, &bulk_data_toggle[devnum][ep],
Stephen Warrenb7dcaf02015-03-23 23:01:01 -0600845 usb_pipein(pipe), buffer, len, true);
Stephen Warren972ad642015-03-07 22:48:52 -0700846}
847
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700848int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
849 int len, struct devrequest *setup)
850{
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700851 int devnum = usb_pipedevice(pipe);
Stephen Warren4db200e2015-03-07 22:48:53 -0700852 int pid, ret, act_len;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700853 /* For CONTROL endpoint pid should start with DATA1 */
854 int status_direction;
855
856 if (devnum == root_hub_devnum) {
857 dev->status = 0;
858 dev->speed = USB_SPEED_HIGH;
859 return dwc_otg_submit_rh_msg(dev, pipe, buffer, len, setup);
860 }
861
Stephen Warren4db200e2015-03-07 22:48:53 -0700862 pid = DWC2_HC_PID_SETUP;
Stephen Warrenb7dcaf02015-03-23 23:01:01 -0600863 ret = chunk_msg(dev, pipe, &pid, 0, setup, 8, true);
Stephen Warren4db200e2015-03-07 22:48:53 -0700864 if (ret)
865 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700866
867 if (buffer) {
Stephen Warrenb0ad4a32015-03-07 22:48:54 -0700868 pid = DWC2_HC_PID_DATA1;
869 ret = chunk_msg(dev, pipe, &pid, usb_pipein(pipe), buffer,
Stephen Warrenb7dcaf02015-03-23 23:01:01 -0600870 len, false);
Stephen Warren4db200e2015-03-07 22:48:53 -0700871 if (ret)
872 return ret;
873 act_len = dev->act_len;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700874 } /* End of DATA stage */
Stephen Warren4db200e2015-03-07 22:48:53 -0700875 else
876 act_len = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700877
878 /* STATUS stage */
879 if ((len == 0) || usb_pipeout(pipe))
880 status_direction = 1;
881 else
882 status_direction = 0;
883
Stephen Warren4db200e2015-03-07 22:48:53 -0700884 pid = DWC2_HC_PID_DATA1;
Stephen Warrenb7dcaf02015-03-23 23:01:01 -0600885 ret = chunk_msg(dev, pipe, &pid, status_direction, status_buffer, 0,
886 false);
Stephen Warren4db200e2015-03-07 22:48:53 -0700887 if (ret)
888 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700889
Stephen Warren4db200e2015-03-07 22:48:53 -0700890 dev->act_len = act_len;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700891
Stephen Warren8a346662015-03-07 22:48:51 -0700892 return 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700893}
894
895int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
896 int len, int interval)
897{
898 printf("dev = %p pipe = %#lx buf = %p size = %d int = %d\n",
899 dev, pipe, buffer, len, interval);
900 return -ENOSYS;
901}
902
903/* U-Boot USB control interface */
904int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
905{
906 uint32_t snpsid;
907 int i, j;
908
909 root_hub_devnum = 0;
910
911 snpsid = readl(&regs->gsnpsid);
912 printf("Core Release: %x.%03x\n", snpsid >> 12 & 0xf, snpsid & 0xfff);
913
914 if ((snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_2xx) {
915 printf("SNPSID invalid (not DWC2 OTG device): %08x\n", snpsid);
916 return -ENODEV;
917 }
918
919 dwc_otg_core_init(regs);
920 dwc_otg_core_host_init(regs);
921
922 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
923 DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
924 DWC2_HPRT0_PRTOVRCURRCHNG,
925 DWC2_HPRT0_PRTRST);
926 mdelay(50);
927 clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET |
928 DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG |
929 DWC2_HPRT0_PRTRST);
930
931 for (i = 0; i < MAX_DEVICE; i++) {
Stephen Warrenb0ad4a32015-03-07 22:48:54 -0700932 for (j = 0; j < MAX_ENDPOINT; j++)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700933 bulk_data_toggle[i][j] = DWC2_HC_PID_DATA0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700934 }
935
936 return 0;
937}
938
939int usb_lowlevel_stop(int index)
940{
941 /* Put everything in reset. */
942 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
943 DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
944 DWC2_HPRT0_PRTOVRCURRCHNG,
945 DWC2_HPRT0_PRTRST);
946 return 0;
947}