blob: 0d694f3d39a5e1724936b2beb41ef13841df953b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Oleksandr Tymoshenko7a881752014-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 Tymoshenko7a881752014-02-01 21:51:25 -07005 */
6
7#include <common.h>
Patrick Delaunay9225f3e2020-04-27 15:29:59 +02008#include <clk.h>
Simon Glass63334482019-11-14 12:57:39 -07009#include <cpu_func.h>
Simon Glassa7ea72c2015-07-07 20:53:37 -060010#include <dm.h>
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070011#include <errno.h>
Patrick Delaunaybb3569d2020-04-27 15:29:58 +020012#include <generic-phy.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070014#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060015#include <memalign.h>
Stephen Warren79beb282015-03-24 20:07:35 -060016#include <phys2bus.h>
Patrick Delaunay9225f3e2020-04-27 15:29:59 +020017#include <usb.h>
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070018#include <usbroothubdes.h>
Mateusz Kulikowski2765f1e2016-01-23 11:54:30 +010019#include <wait_bit.h>
Simon Glass274e0b02020-05-10 11:39:56 -060020#include <asm/cache.h>
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070021#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070022#include <dm/device_compat.h>
Kever Yang327c24d2017-03-10 12:05:14 +080023#include <power/regulator.h>
Ley Foon Tan23865562018-08-29 00:08:48 +080024#include <reset.h>
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070025
26#include "dwc2.h"
27
28/* Use only HC channel 0. */
29#define DWC2_HC_CHANNEL 0
30
31#define DWC2_STATUS_BUF_SIZE 64
Alexey Brodkinf19414b2018-02-28 16:16:58 +030032#define DWC2_DATA_BUF_SIZE (CONFIG_USB_DWC2_BUFFER_SIZE * 1024)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070033
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070034#define MAX_DEVICE 16
35#define MAX_ENDPOINT 16
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070036
Simon Glasse3c23a02015-07-07 20:53:36 -060037struct dwc2_priv {
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +010038#if CONFIG_IS_ENABLED(DM_USB)
Alexander Stein76fac502015-07-24 09:22:14 +020039 uint8_t aligned_buffer[DWC2_DATA_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
40 uint8_t status_buffer[DWC2_STATUS_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +010041#ifdef CONFIG_DM_REGULATOR
42 struct udevice *vbus_supply;
43#endif
Patrick Delaunaybb3569d2020-04-27 15:29:58 +020044 struct phy phy;
Patrick Delaunay9225f3e2020-04-27 15:29:59 +020045 struct clk_bulk clks;
Simon Glassa7ea72c2015-07-07 20:53:37 -060046#else
Simon Glasse3c23a02015-07-07 20:53:36 -060047 uint8_t *aligned_buffer;
48 uint8_t *status_buffer;
Simon Glassa7ea72c2015-07-07 20:53:37 -060049#endif
Stefan Brüns081dcc72016-01-23 01:42:25 +010050 u8 in_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
51 u8 out_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
Simon Glasse3c23a02015-07-07 20:53:36 -060052 struct dwc2_core_regs *regs;
53 int root_hub_devnum;
Marek Vasut39209492016-04-27 14:55:57 +020054 bool ext_vbus;
Meng Dongyang697a8bc2017-06-28 19:22:43 +080055 /*
56 * The hnp/srp capability must be disabled if the platform
57 * does't support hnp/srp. Otherwise the force mode can't work.
58 */
Meng Dongyangcc3fe062017-06-08 15:34:20 +080059 bool hnp_srp_disable;
Marek Vasut43db5a62016-04-27 14:58:49 +020060 bool oc_disable;
Ley Foon Tan23865562018-08-29 00:08:48 +080061
62 struct reset_ctl_bulk resets;
Simon Glasse3c23a02015-07-07 20:53:36 -060063};
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070064
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +010065#if !CONFIG_IS_ENABLED(DM_USB)
Alexander Stein76fac502015-07-24 09:22:14 +020066/* We need cacheline-aligned buffers for DMA transfers and dcache support */
67DEFINE_ALIGN_BUFFER(uint8_t, aligned_buffer_addr, DWC2_DATA_BUF_SIZE,
68 ARCH_DMA_MINALIGN);
69DEFINE_ALIGN_BUFFER(uint8_t, status_buffer_addr, DWC2_STATUS_BUF_SIZE,
70 ARCH_DMA_MINALIGN);
Simon Glasse3c23a02015-07-07 20:53:36 -060071
72static struct dwc2_priv local;
Simon Glassa7ea72c2015-07-07 20:53:37 -060073#endif
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070074
75/*
76 * DWC2 IP interface
77 */
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070078
79/*
80 * Initializes the FSLSPClkSel field of the HCFG register
81 * depending on the PHY type.
82 */
83static void init_fslspclksel(struct dwc2_core_regs *regs)
84{
85 uint32_t phyclk;
86
87#if (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
88 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
89#else
90 /* High speed PHY running at full speed or high speed */
91 phyclk = DWC2_HCFG_FSLSPCLKSEL_30_60_MHZ;
92#endif
93
94#ifdef CONFIG_DWC2_ULPI_FS_LS
95 uint32_t hwcfg2 = readl(&regs->ghwcfg2);
96 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
97 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
98 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
99 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
100
101 if (hval == 2 && fval == 1)
102 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
103#endif
104
105 clrsetbits_le32(&regs->host_regs.hcfg,
106 DWC2_HCFG_FSLSPCLKSEL_MASK,
107 phyclk << DWC2_HCFG_FSLSPCLKSEL_OFFSET);
108}
109
110/*
111 * Flush a Tx FIFO.
112 *
113 * @param regs Programming view of DWC_otg controller.
114 * @param num Tx FIFO to flush.
115 */
116static void dwc_otg_flush_tx_fifo(struct dwc2_core_regs *regs, const int num)
117{
118 int ret;
119
120 writel(DWC2_GRSTCTL_TXFFLSH | (num << DWC2_GRSTCTL_TXFNUM_OFFSET),
121 &regs->grstctl);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100122 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_TXFFLSH,
123 false, 1000, false);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700124 if (ret)
Patrice Chotarda259c1d2018-03-15 18:00:32 +0100125 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700126
127 /* Wait for 3 PHY Clocks */
128 udelay(1);
129}
130
131/*
132 * Flush Rx FIFO.
133 *
134 * @param regs Programming view of DWC_otg controller.
135 */
136static void dwc_otg_flush_rx_fifo(struct dwc2_core_regs *regs)
137{
138 int ret;
139
140 writel(DWC2_GRSTCTL_RXFFLSH, &regs->grstctl);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100141 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_RXFFLSH,
142 false, 1000, false);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700143 if (ret)
Patrice Chotarda259c1d2018-03-15 18:00:32 +0100144 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700145
146 /* Wait for 3 PHY Clocks */
147 udelay(1);
148}
149
150/*
151 * Do core a soft reset of the core. Be careful with this because it
152 * resets all the internal state machines of the core.
153 */
154static void dwc_otg_core_reset(struct dwc2_core_regs *regs)
155{
156 int ret;
157
158 /* Wait for AHB master IDLE state. */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100159 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_AHBIDLE,
160 true, 1000, false);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700161 if (ret)
Patrice Chotarda259c1d2018-03-15 18:00:32 +0100162 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700163
164 /* Core Soft Reset */
165 writel(DWC2_GRSTCTL_CSFTRST, &regs->grstctl);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100166 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_CSFTRST,
167 false, 1000, false);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700168 if (ret)
Patrice Chotarda259c1d2018-03-15 18:00:32 +0100169 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700170
171 /*
172 * Wait for core to come out of reset.
173 * NOTE: This long sleep is _very_ important, otherwise the core will
174 * not stay in host mode after a connector ID change!
175 */
176 mdelay(100);
177}
178
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100179#if CONFIG_IS_ENABLED(DM_USB) && defined(CONFIG_DM_REGULATOR)
Kever Yang327c24d2017-03-10 12:05:14 +0800180static int dwc_vbus_supply_init(struct udevice *dev)
181{
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100182 struct dwc2_priv *priv = dev_get_priv(dev);
Kever Yang327c24d2017-03-10 12:05:14 +0800183 int ret;
184
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100185 ret = device_get_supply_regulator(dev, "vbus-supply",
186 &priv->vbus_supply);
Kever Yang327c24d2017-03-10 12:05:14 +0800187 if (ret) {
188 debug("%s: No vbus supply\n", dev->name);
189 return 0;
190 }
191
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100192 ret = regulator_set_enable(priv->vbus_supply, true);
Kever Yang327c24d2017-03-10 12:05:14 +0800193 if (ret) {
Patrice Chotarda259c1d2018-03-15 18:00:32 +0100194 dev_err(dev, "Error enabling vbus supply\n");
Kever Yang327c24d2017-03-10 12:05:14 +0800195 return ret;
196 }
197
198 return 0;
199}
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100200
201static int dwc_vbus_supply_exit(struct udevice *dev)
202{
203 struct dwc2_priv *priv = dev_get_priv(dev);
204 int ret;
205
206 if (priv->vbus_supply) {
207 ret = regulator_set_enable(priv->vbus_supply, false);
208 if (ret) {
209 dev_err(dev, "Error disabling vbus supply\n");
210 return ret;
211 }
212 }
213
214 return 0;
215}
Kever Yang327c24d2017-03-10 12:05:14 +0800216#else
217static int dwc_vbus_supply_init(struct udevice *dev)
218{
219 return 0;
220}
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100221
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100222#if CONFIG_IS_ENABLED(DM_USB)
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100223static int dwc_vbus_supply_exit(struct udevice *dev)
224{
225 return 0;
226}
Kever Yang327c24d2017-03-10 12:05:14 +0800227#endif
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100228#endif
Kever Yang327c24d2017-03-10 12:05:14 +0800229
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700230/*
231 * This function initializes the DWC_otg controller registers for
232 * host mode.
233 *
234 * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
235 * request queues. Host channels are reset to ensure that they are ready for
236 * performing transfers.
237 *
Kever Yang327c24d2017-03-10 12:05:14 +0800238 * @param dev USB Device (NULL if driver model is not being used)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700239 * @param regs Programming view of DWC_otg controller
240 *
241 */
Kever Yang327c24d2017-03-10 12:05:14 +0800242static void dwc_otg_core_host_init(struct udevice *dev,
243 struct dwc2_core_regs *regs)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700244{
245 uint32_t nptxfifosize = 0;
246 uint32_t ptxfifosize = 0;
247 uint32_t hprt0 = 0;
248 int i, ret, num_channels;
249
250 /* Restart the Phy Clock */
251 writel(0, &regs->pcgcctl);
252
253 /* Initialize Host Configuration Register */
254 init_fslspclksel(regs);
255#ifdef CONFIG_DWC2_DFLT_SPEED_FULL
256 setbits_le32(&regs->host_regs.hcfg, DWC2_HCFG_FSLSSUPP);
257#endif
258
259 /* Configure data FIFO sizes */
260#ifdef CONFIG_DWC2_ENABLE_DYNAMIC_FIFO
261 if (readl(&regs->ghwcfg2) & DWC2_HWCFG2_DYNAMIC_FIFO) {
262 /* Rx FIFO */
263 writel(CONFIG_DWC2_HOST_RX_FIFO_SIZE, &regs->grxfsiz);
264
265 /* Non-periodic Tx FIFO */
266 nptxfifosize |= CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE <<
267 DWC2_FIFOSIZE_DEPTH_OFFSET;
268 nptxfifosize |= CONFIG_DWC2_HOST_RX_FIFO_SIZE <<
269 DWC2_FIFOSIZE_STARTADDR_OFFSET;
270 writel(nptxfifosize, &regs->gnptxfsiz);
271
272 /* Periodic Tx FIFO */
273 ptxfifosize |= CONFIG_DWC2_HOST_PERIO_TX_FIFO_SIZE <<
274 DWC2_FIFOSIZE_DEPTH_OFFSET;
275 ptxfifosize |= (CONFIG_DWC2_HOST_RX_FIFO_SIZE +
276 CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE) <<
277 DWC2_FIFOSIZE_STARTADDR_OFFSET;
278 writel(ptxfifosize, &regs->hptxfsiz);
279 }
280#endif
281
282 /* Clear Host Set HNP Enable in the OTG Control Register */
283 clrbits_le32(&regs->gotgctl, DWC2_GOTGCTL_HSTSETHNPEN);
284
285 /* Make sure the FIFOs are flushed. */
286 dwc_otg_flush_tx_fifo(regs, 0x10); /* All Tx FIFOs */
287 dwc_otg_flush_rx_fifo(regs);
288
289 /* Flush out any leftover queued requests. */
290 num_channels = readl(&regs->ghwcfg2);
291 num_channels &= DWC2_HWCFG2_NUM_HOST_CHAN_MASK;
292 num_channels >>= DWC2_HWCFG2_NUM_HOST_CHAN_OFFSET;
293 num_channels += 1;
294
295 for (i = 0; i < num_channels; i++)
296 clrsetbits_le32(&regs->hc_regs[i].hcchar,
297 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_EPDIR,
298 DWC2_HCCHAR_CHDIS);
299
300 /* Halt all channels to put them into a known state. */
301 for (i = 0; i < num_channels; i++) {
302 clrsetbits_le32(&regs->hc_regs[i].hcchar,
303 DWC2_HCCHAR_EPDIR,
304 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100305 ret = wait_for_bit_le32(&regs->hc_regs[i].hcchar,
306 DWC2_HCCHAR_CHEN, false, 1000, false);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700307 if (ret)
Patrice Chotarda259c1d2018-03-15 18:00:32 +0100308 dev_info("%s: Timeout!\n", __func__);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700309 }
310
311 /* Turn on the vbus power. */
312 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST) {
313 hprt0 = readl(&regs->hprt0);
314 hprt0 &= ~(DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET);
315 hprt0 &= ~(DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG);
316 if (!(hprt0 & DWC2_HPRT0_PRTPWR)) {
317 hprt0 |= DWC2_HPRT0_PRTPWR;
318 writel(hprt0, &regs->hprt0);
319 }
320 }
Kever Yang327c24d2017-03-10 12:05:14 +0800321
322 if (dev)
323 dwc_vbus_supply_init(dev);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700324}
325
326/*
327 * This function initializes the DWC_otg controller registers and
328 * prepares the core for device mode or host mode operation.
329 *
330 * @param regs Programming view of the DWC_otg controller
331 */
Marek Vasut36fc5692016-04-27 14:53:33 +0200332static void dwc_otg_core_init(struct dwc2_priv *priv)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700333{
Marek Vasut36fc5692016-04-27 14:53:33 +0200334 struct dwc2_core_regs *regs = priv->regs;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700335 uint32_t ahbcfg = 0;
336 uint32_t usbcfg = 0;
337 uint8_t brst_sz = CONFIG_DWC2_DMA_BURST_SIZE;
338
339 /* Common Initialization */
340 usbcfg = readl(&regs->gusbcfg);
341
342 /* Program the ULPI External VBUS bit if needed */
Marek Vasut39209492016-04-27 14:55:57 +0200343 if (priv->ext_vbus) {
Marek Vasut43db5a62016-04-27 14:58:49 +0200344 usbcfg |= DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
345 if (!priv->oc_disable) {
346 usbcfg |= DWC2_GUSBCFG_ULPI_INT_VBUS_INDICATOR |
347 DWC2_GUSBCFG_INDICATOR_PASSTHROUGH;
348 }
Marek Vasut39209492016-04-27 14:55:57 +0200349 } else {
350 usbcfg &= ~DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
351 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700352
353 /* Set external TS Dline pulsing */
354#ifdef CONFIG_DWC2_TS_DLINE
355 usbcfg |= DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
356#else
357 usbcfg &= ~DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
358#endif
359 writel(usbcfg, &regs->gusbcfg);
360
361 /* Reset the Controller */
362 dwc_otg_core_reset(regs);
363
364 /*
365 * This programming sequence needs to happen in FS mode before
366 * any other programming occurs
367 */
368#if defined(CONFIG_DWC2_DFLT_SPEED_FULL) && \
369 (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
370 /* If FS mode with FS PHY */
371 setbits_le32(&regs->gusbcfg, DWC2_GUSBCFG_PHYSEL);
372
373 /* Reset after a PHY select */
374 dwc_otg_core_reset(regs);
375
376 /*
377 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS.
378 * Also do this on HNP Dev/Host mode switches (done in dev_init
379 * and host_init).
380 */
381 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
382 init_fslspclksel(regs);
383
384#ifdef CONFIG_DWC2_I2C_ENABLE
385 /* Program GUSBCFG.OtgUtmifsSel to I2C */
386 setbits_le32(&regs->gusbcfg, DWC2_GUSBCFG_OTGUTMIFSSEL);
387
388 /* Program GI2CCTL.I2CEn */
389 clrsetbits_le32(&regs->gi2cctl, DWC2_GI2CCTL_I2CEN |
390 DWC2_GI2CCTL_I2CDEVADDR_MASK,
391 1 << DWC2_GI2CCTL_I2CDEVADDR_OFFSET);
392 setbits_le32(&regs->gi2cctl, DWC2_GI2CCTL_I2CEN);
393#endif
394
395#else
396 /* High speed PHY. */
397
398 /*
399 * HS PHY parameters. These parameters are preserved during
400 * soft reset so only program the first time. Do a soft reset
401 * immediately after setting phyif.
402 */
403 usbcfg &= ~(DWC2_GUSBCFG_ULPI_UTMI_SEL | DWC2_GUSBCFG_PHYIF);
404 usbcfg |= CONFIG_DWC2_PHY_TYPE << DWC2_GUSBCFG_ULPI_UTMI_SEL_OFFSET;
405
406 if (usbcfg & DWC2_GUSBCFG_ULPI_UTMI_SEL) { /* ULPI interface */
407#ifdef CONFIG_DWC2_PHY_ULPI_DDR
408 usbcfg |= DWC2_GUSBCFG_DDRSEL;
409#else
410 usbcfg &= ~DWC2_GUSBCFG_DDRSEL;
411#endif
412 } else { /* UTMI+ interface */
Alexey Brodkin7628ad22018-01-31 17:56:59 +0300413#if (CONFIG_DWC2_UTMI_WIDTH == 16)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700414 usbcfg |= DWC2_GUSBCFG_PHYIF;
415#endif
416 }
417
418 writel(usbcfg, &regs->gusbcfg);
419
420 /* Reset after setting the PHY parameters */
421 dwc_otg_core_reset(regs);
422#endif
423
424 usbcfg = readl(&regs->gusbcfg);
425 usbcfg &= ~(DWC2_GUSBCFG_ULPI_FSLS | DWC2_GUSBCFG_ULPI_CLK_SUS_M);
426#ifdef CONFIG_DWC2_ULPI_FS_LS
427 uint32_t hwcfg2 = readl(&regs->ghwcfg2);
428 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
429 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
430 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
431 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
432 if (hval == 2 && fval == 1) {
433 usbcfg |= DWC2_GUSBCFG_ULPI_FSLS;
434 usbcfg |= DWC2_GUSBCFG_ULPI_CLK_SUS_M;
435 }
436#endif
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800437 if (priv->hnp_srp_disable)
438 usbcfg |= DWC2_GUSBCFG_FORCEHOSTMODE;
439
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700440 writel(usbcfg, &regs->gusbcfg);
441
442 /* Program the GAHBCFG Register. */
443 switch (readl(&regs->ghwcfg2) & DWC2_HWCFG2_ARCHITECTURE_MASK) {
444 case DWC2_HWCFG2_ARCHITECTURE_SLAVE_ONLY:
445 break;
446 case DWC2_HWCFG2_ARCHITECTURE_EXT_DMA:
447 while (brst_sz > 1) {
448 ahbcfg |= ahbcfg + (1 << DWC2_GAHBCFG_HBURSTLEN_OFFSET);
449 ahbcfg &= DWC2_GAHBCFG_HBURSTLEN_MASK;
450 brst_sz >>= 1;
451 }
452
453#ifdef CONFIG_DWC2_DMA_ENABLE
454 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
455#endif
456 break;
457
458 case DWC2_HWCFG2_ARCHITECTURE_INT_DMA:
459 ahbcfg |= DWC2_GAHBCFG_HBURSTLEN_INCR4;
460#ifdef CONFIG_DWC2_DMA_ENABLE
461 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
462#endif
463 break;
464 }
465
466 writel(ahbcfg, &regs->gahbcfg);
467
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800468 /* Program the capabilities in GUSBCFG Register */
469 usbcfg = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700470
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800471 if (!priv->hnp_srp_disable)
472 usbcfg |= DWC2_GUSBCFG_HNPCAP | DWC2_GUSBCFG_SRPCAP;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700473#ifdef CONFIG_DWC2_IC_USB_CAP
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800474 usbcfg |= DWC2_GUSBCFG_IC_USB_CAP;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700475#endif
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800476
477 setbits_le32(&regs->gusbcfg, usbcfg);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700478}
479
480/*
481 * Prepares a host channel for transferring packets to/from a specific
482 * endpoint. The HCCHARn register is set up with the characteristics specified
483 * in _hc. Host channel interrupts that may need to be serviced while this
484 * transfer is in progress are enabled.
485 *
486 * @param regs Programming view of DWC_otg controller
487 * @param hc Information needed to initialize the host channel
488 */
489static void dwc_otg_hc_init(struct dwc2_core_regs *regs, uint8_t hc_num,
Stephen Warrendead8db2015-04-10 21:05:21 -0600490 struct usb_device *dev, uint8_t dev_addr, uint8_t ep_num,
491 uint8_t ep_is_in, uint8_t ep_type, uint16_t max_packet)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700492{
493 struct dwc2_hc_regs *hc_regs = &regs->hc_regs[hc_num];
Stephen Warrendead8db2015-04-10 21:05:21 -0600494 uint32_t hcchar = (dev_addr << DWC2_HCCHAR_DEVADDR_OFFSET) |
495 (ep_num << DWC2_HCCHAR_EPNUM_OFFSET) |
496 (ep_is_in << DWC2_HCCHAR_EPDIR_OFFSET) |
497 (ep_type << DWC2_HCCHAR_EPTYPE_OFFSET) |
498 (max_packet << DWC2_HCCHAR_MPS_OFFSET);
499
500 if (dev->speed == USB_SPEED_LOW)
501 hcchar |= DWC2_HCCHAR_LSPDDEV;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700502
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700503 /*
504 * Program the HCCHARn register with the endpoint characteristics
505 * for the current transfer.
506 */
507 writel(hcchar, &hc_regs->hcchar);
508
Stefan Brüns2e194e22016-01-17 04:09:54 +0100509 /* Program the HCSPLIT register, default to no SPLIT */
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700510 writel(0, &hc_regs->hcsplt);
511}
512
Stefan Brüns2e194e22016-01-17 04:09:54 +0100513static void dwc_otg_hc_init_split(struct dwc2_hc_regs *hc_regs,
514 uint8_t hub_devnum, uint8_t hub_port)
515{
516 uint32_t hcsplt = 0;
517
518 hcsplt = DWC2_HCSPLT_SPLTENA;
519 hcsplt |= hub_devnum << DWC2_HCSPLT_HUBADDR_OFFSET;
520 hcsplt |= hub_port << DWC2_HCSPLT_PRTADDR_OFFSET;
521
522 /* Program the HCSPLIT register for SPLITs */
523 writel(hcsplt, &hc_regs->hcsplt);
524}
525
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700526/*
527 * DWC2 to USB API interface
528 */
529/* Direction: In ; Request: Status */
Simon Glasse3c23a02015-07-07 20:53:36 -0600530static int dwc_otg_submit_rh_msg_in_status(struct dwc2_core_regs *regs,
531 struct usb_device *dev, void *buffer,
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700532 int txlen, struct devrequest *cmd)
533{
534 uint32_t hprt0 = 0;
535 uint32_t port_status = 0;
536 uint32_t port_change = 0;
537 int len = 0;
538 int stat = 0;
539
540 switch (cmd->requesttype & ~USB_DIR_IN) {
541 case 0:
542 *(uint16_t *)buffer = cpu_to_le16(1);
543 len = 2;
544 break;
545 case USB_RECIP_INTERFACE:
546 case USB_RECIP_ENDPOINT:
547 *(uint16_t *)buffer = cpu_to_le16(0);
548 len = 2;
549 break;
550 case USB_TYPE_CLASS:
551 *(uint32_t *)buffer = cpu_to_le32(0);
552 len = 4;
553 break;
554 case USB_RECIP_OTHER | USB_TYPE_CLASS:
555 hprt0 = readl(&regs->hprt0);
556 if (hprt0 & DWC2_HPRT0_PRTCONNSTS)
557 port_status |= USB_PORT_STAT_CONNECTION;
558 if (hprt0 & DWC2_HPRT0_PRTENA)
559 port_status |= USB_PORT_STAT_ENABLE;
560 if (hprt0 & DWC2_HPRT0_PRTSUSP)
561 port_status |= USB_PORT_STAT_SUSPEND;
562 if (hprt0 & DWC2_HPRT0_PRTOVRCURRACT)
563 port_status |= USB_PORT_STAT_OVERCURRENT;
564 if (hprt0 & DWC2_HPRT0_PRTRST)
565 port_status |= USB_PORT_STAT_RESET;
566 if (hprt0 & DWC2_HPRT0_PRTPWR)
567 port_status |= USB_PORT_STAT_POWER;
568
Stephen Warrend3388f82015-03-27 21:55:38 -0600569 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) == DWC2_HPRT0_PRTSPD_LOW)
570 port_status |= USB_PORT_STAT_LOW_SPEED;
571 else if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
572 DWC2_HPRT0_PRTSPD_HIGH)
573 port_status |= USB_PORT_STAT_HIGH_SPEED;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700574
575 if (hprt0 & DWC2_HPRT0_PRTENCHNG)
576 port_change |= USB_PORT_STAT_C_ENABLE;
577 if (hprt0 & DWC2_HPRT0_PRTCONNDET)
578 port_change |= USB_PORT_STAT_C_CONNECTION;
579 if (hprt0 & DWC2_HPRT0_PRTOVRCURRCHNG)
580 port_change |= USB_PORT_STAT_C_OVERCURRENT;
581
582 *(uint32_t *)buffer = cpu_to_le32(port_status |
583 (port_change << 16));
584 len = 4;
585 break;
586 default:
587 puts("unsupported root hub command\n");
588 stat = USB_ST_STALLED;
589 }
590
591 dev->act_len = min(len, txlen);
592 dev->status = stat;
593
594 return stat;
595}
596
597/* Direction: In ; Request: Descriptor */
598static int dwc_otg_submit_rh_msg_in_descriptor(struct usb_device *dev,
599 void *buffer, int txlen,
600 struct devrequest *cmd)
601{
602 unsigned char data[32];
603 uint32_t dsc;
604 int len = 0;
605 int stat = 0;
606 uint16_t wValue = cpu_to_le16(cmd->value);
607 uint16_t wLength = cpu_to_le16(cmd->length);
608
609 switch (cmd->requesttype & ~USB_DIR_IN) {
610 case 0:
611 switch (wValue & 0xff00) {
612 case 0x0100: /* device descriptor */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900613 len = min3(txlen, (int)sizeof(root_hub_dev_des), (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700614 memcpy(buffer, root_hub_dev_des, len);
615 break;
616 case 0x0200: /* configuration descriptor */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900617 len = min3(txlen, (int)sizeof(root_hub_config_des), (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700618 memcpy(buffer, root_hub_config_des, len);
619 break;
620 case 0x0300: /* string descriptors */
621 switch (wValue & 0xff) {
622 case 0x00:
Masahiro Yamadadb204642014-11-07 03:03:31 +0900623 len = min3(txlen, (int)sizeof(root_hub_str_index0),
624 (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700625 memcpy(buffer, root_hub_str_index0, len);
626 break;
627 case 0x01:
Masahiro Yamadadb204642014-11-07 03:03:31 +0900628 len = min3(txlen, (int)sizeof(root_hub_str_index1),
629 (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700630 memcpy(buffer, root_hub_str_index1, len);
631 break;
632 }
633 break;
634 default:
635 stat = USB_ST_STALLED;
636 }
637 break;
638
639 case USB_TYPE_CLASS:
640 /* Root port config, set 1 port and nothing else. */
641 dsc = 0x00000001;
642
643 data[0] = 9; /* min length; */
644 data[1] = 0x29;
645 data[2] = dsc & RH_A_NDP;
646 data[3] = 0;
647 if (dsc & RH_A_PSM)
648 data[3] |= 0x1;
649 if (dsc & RH_A_NOCP)
650 data[3] |= 0x10;
651 else if (dsc & RH_A_OCPM)
652 data[3] |= 0x8;
653
654 /* corresponds to data[4-7] */
655 data[5] = (dsc & RH_A_POTPGT) >> 24;
656 data[7] = dsc & RH_B_DR;
657 if (data[2] < 7) {
658 data[8] = 0xff;
659 } else {
660 data[0] += 2;
661 data[8] = (dsc & RH_B_DR) >> 8;
662 data[9] = 0xff;
663 data[10] = data[9];
664 }
665
Masahiro Yamadadb204642014-11-07 03:03:31 +0900666 len = min3(txlen, (int)data[0], (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700667 memcpy(buffer, data, len);
668 break;
669 default:
670 puts("unsupported root hub command\n");
671 stat = USB_ST_STALLED;
672 }
673
674 dev->act_len = min(len, txlen);
675 dev->status = stat;
676
677 return stat;
678}
679
680/* Direction: In ; Request: Configuration */
681static int dwc_otg_submit_rh_msg_in_configuration(struct usb_device *dev,
682 void *buffer, int txlen,
683 struct devrequest *cmd)
684{
685 int len = 0;
686 int stat = 0;
687
688 switch (cmd->requesttype & ~USB_DIR_IN) {
689 case 0:
690 *(uint8_t *)buffer = 0x01;
691 len = 1;
692 break;
693 default:
694 puts("unsupported root hub command\n");
695 stat = USB_ST_STALLED;
696 }
697
698 dev->act_len = min(len, txlen);
699 dev->status = stat;
700
701 return stat;
702}
703
704/* Direction: In */
Simon Glasse3c23a02015-07-07 20:53:36 -0600705static int dwc_otg_submit_rh_msg_in(struct dwc2_priv *priv,
706 struct usb_device *dev, void *buffer,
707 int txlen, struct devrequest *cmd)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700708{
709 switch (cmd->request) {
710 case USB_REQ_GET_STATUS:
Simon Glasse3c23a02015-07-07 20:53:36 -0600711 return dwc_otg_submit_rh_msg_in_status(priv->regs, dev, buffer,
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700712 txlen, cmd);
713 case USB_REQ_GET_DESCRIPTOR:
714 return dwc_otg_submit_rh_msg_in_descriptor(dev, buffer,
715 txlen, cmd);
716 case USB_REQ_GET_CONFIGURATION:
717 return dwc_otg_submit_rh_msg_in_configuration(dev, buffer,
718 txlen, cmd);
719 default:
720 puts("unsupported root hub command\n");
721 return USB_ST_STALLED;
722 }
723}
724
725/* Direction: Out */
Simon Glasse3c23a02015-07-07 20:53:36 -0600726static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
727 struct usb_device *dev,
728 void *buffer, int txlen,
729 struct devrequest *cmd)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700730{
Simon Glasse3c23a02015-07-07 20:53:36 -0600731 struct dwc2_core_regs *regs = priv->regs;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700732 int len = 0;
733 int stat = 0;
734 uint16_t bmrtype_breq = cmd->requesttype | (cmd->request << 8);
735 uint16_t wValue = cpu_to_le16(cmd->value);
736
737 switch (bmrtype_breq & ~USB_DIR_IN) {
738 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_ENDPOINT:
739 case (USB_REQ_CLEAR_FEATURE << 8) | USB_TYPE_CLASS:
740 break;
741
742 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
743 switch (wValue) {
744 case USB_PORT_FEAT_C_CONNECTION:
745 setbits_le32(&regs->hprt0, DWC2_HPRT0_PRTCONNDET);
746 break;
747 }
748 break;
749
750 case (USB_REQ_SET_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
751 switch (wValue) {
752 case USB_PORT_FEAT_SUSPEND:
753 break;
754
755 case USB_PORT_FEAT_RESET:
756 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
757 DWC2_HPRT0_PRTCONNDET |
758 DWC2_HPRT0_PRTENCHNG |
759 DWC2_HPRT0_PRTOVRCURRCHNG,
760 DWC2_HPRT0_PRTRST);
761 mdelay(50);
762 clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTRST);
763 break;
764
765 case USB_PORT_FEAT_POWER:
766 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
767 DWC2_HPRT0_PRTCONNDET |
768 DWC2_HPRT0_PRTENCHNG |
769 DWC2_HPRT0_PRTOVRCURRCHNG,
770 DWC2_HPRT0_PRTRST);
771 break;
772
773 case USB_PORT_FEAT_ENABLE:
774 break;
775 }
776 break;
777 case (USB_REQ_SET_ADDRESS << 8):
Simon Glasse3c23a02015-07-07 20:53:36 -0600778 priv->root_hub_devnum = wValue;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700779 break;
780 case (USB_REQ_SET_CONFIGURATION << 8):
781 break;
782 default:
783 puts("unsupported root hub command\n");
784 stat = USB_ST_STALLED;
785 }
786
787 len = min(len, txlen);
788
789 dev->act_len = len;
790 dev->status = stat;
791
792 return stat;
793}
794
Simon Glasse3c23a02015-07-07 20:53:36 -0600795static int dwc_otg_submit_rh_msg(struct dwc2_priv *priv, struct usb_device *dev,
796 unsigned long pipe, void *buffer, int txlen,
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700797 struct devrequest *cmd)
798{
799 int stat = 0;
800
801 if (usb_pipeint(pipe)) {
802 puts("Root-Hub submit IRQ: NOT implemented\n");
803 return 0;
804 }
805
806 if (cmd->requesttype & USB_DIR_IN)
Simon Glasse3c23a02015-07-07 20:53:36 -0600807 stat = dwc_otg_submit_rh_msg_in(priv, dev, buffer, txlen, cmd);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700808 else
Simon Glasse3c23a02015-07-07 20:53:36 -0600809 stat = dwc_otg_submit_rh_msg_out(priv, dev, buffer, txlen, cmd);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700810
811 mdelay(1);
812
813 return stat;
814}
815
Stefan Brüns081dcc72016-01-23 01:42:25 +0100816int wait_for_chhltd(struct dwc2_hc_regs *hc_regs, uint32_t *sub, u8 *toggle)
Stephen Warren8a346662015-03-07 22:48:51 -0700817{
Stephen Warren8a346662015-03-07 22:48:51 -0700818 int ret;
819 uint32_t hcint, hctsiz;
820
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100821 ret = wait_for_bit_le32(&hc_regs->hcint, DWC2_HCINT_CHHLTD, true,
Christophe Kerello4edc9802018-03-15 18:00:31 +0100822 2000, false);
Stephen Warren8a346662015-03-07 22:48:51 -0700823 if (ret)
824 return ret;
825
826 hcint = readl(&hc_regs->hcint);
Stephen Warren8a346662015-03-07 22:48:51 -0700827 hctsiz = readl(&hc_regs->hctsiz);
828 *sub = (hctsiz & DWC2_HCTSIZ_XFERSIZE_MASK) >>
829 DWC2_HCTSIZ_XFERSIZE_OFFSET;
Stephen Warren9f80e742015-03-07 22:48:55 -0700830 *toggle = (hctsiz & DWC2_HCTSIZ_PID_MASK) >> DWC2_HCTSIZ_PID_OFFSET;
Stephen Warren8a346662015-03-07 22:48:51 -0700831
Stefan Brünsaa9506e2016-01-17 04:09:52 +0100832 debug("%s: HCINT=%08x sub=%u toggle=%d\n", __func__, hcint, *sub,
833 *toggle);
Stephen Warren8a346662015-03-07 22:48:51 -0700834
Stefan Brünsaa9506e2016-01-17 04:09:52 +0100835 if (hcint & DWC2_HCINT_XFERCOMP)
836 return 0;
837
838 if (hcint & (DWC2_HCINT_NAK | DWC2_HCINT_FRMOVRUN))
839 return -EAGAIN;
Stephen Warren8a346662015-03-07 22:48:51 -0700840
Stefan Brünsaa9506e2016-01-17 04:09:52 +0100841 debug("%s: Error (HCINT=%08x)\n", __func__, hcint);
842 return -EINVAL;
Stephen Warren8a346662015-03-07 22:48:51 -0700843}
844
Stephen Warren972ad642015-03-07 22:48:52 -0700845static int dwc2_eptype[] = {
846 DWC2_HCCHAR_EPTYPE_ISOC,
847 DWC2_HCCHAR_EPTYPE_INTR,
848 DWC2_HCCHAR_EPTYPE_CONTROL,
849 DWC2_HCCHAR_EPTYPE_BULK,
850};
851
Stefan Brüns2385db32016-01-17 04:09:53 +0100852static int transfer_chunk(struct dwc2_hc_regs *hc_regs, void *aligned_buffer,
Stefan Brüns081dcc72016-01-23 01:42:25 +0100853 u8 *pid, int in, void *buffer, int num_packets,
Stefan Brüns247241e2016-01-17 04:09:56 +0100854 int xfer_len, int *actual_len, int odd_frame)
Stefan Brüns2385db32016-01-17 04:09:53 +0100855{
856 int ret = 0;
857 uint32_t sub;
858
859 debug("%s: chunk: pid %d xfer_len %u pkts %u\n", __func__,
860 *pid, xfer_len, num_packets);
861
862 writel((xfer_len << DWC2_HCTSIZ_XFERSIZE_OFFSET) |
863 (num_packets << DWC2_HCTSIZ_PKTCNT_OFFSET) |
864 (*pid << DWC2_HCTSIZ_PID_OFFSET),
865 &hc_regs->hctsiz);
866
Eddie Cai408bdee2017-04-06 11:37:04 +0800867 if (xfer_len) {
868 if (in) {
869 invalidate_dcache_range(
870 (uintptr_t)aligned_buffer,
871 (uintptr_t)aligned_buffer +
872 roundup(xfer_len, ARCH_DMA_MINALIGN));
873 } else {
874 memcpy(aligned_buffer, buffer, xfer_len);
875 flush_dcache_range(
876 (uintptr_t)aligned_buffer,
877 (uintptr_t)aligned_buffer +
878 roundup(xfer_len, ARCH_DMA_MINALIGN));
879 }
Stefan Brüns2385db32016-01-17 04:09:53 +0100880 }
881
882 writel(phys_to_bus((unsigned long)aligned_buffer), &hc_regs->hcdma);
883
884 /* Clear old interrupt conditions for this host channel. */
885 writel(0x3fff, &hc_regs->hcint);
886
887 /* Set host channel enable after all other setup is complete. */
888 clrsetbits_le32(&hc_regs->hcchar, DWC2_HCCHAR_MULTICNT_MASK |
Stefan Brüns247241e2016-01-17 04:09:56 +0100889 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS |
890 DWC2_HCCHAR_ODDFRM,
Stefan Brüns2385db32016-01-17 04:09:53 +0100891 (1 << DWC2_HCCHAR_MULTICNT_OFFSET) |
Stefan Brüns247241e2016-01-17 04:09:56 +0100892 (odd_frame << DWC2_HCCHAR_ODDFRM_OFFSET) |
Stefan Brüns2385db32016-01-17 04:09:53 +0100893 DWC2_HCCHAR_CHEN);
894
895 ret = wait_for_chhltd(hc_regs, &sub, pid);
896 if (ret < 0)
897 return ret;
898
899 if (in) {
900 xfer_len -= sub;
901
902 invalidate_dcache_range((unsigned long)aligned_buffer,
903 (unsigned long)aligned_buffer +
904 roundup(xfer_len, ARCH_DMA_MINALIGN));
905
906 memcpy(buffer, aligned_buffer, xfer_len);
907 }
908 *actual_len = xfer_len;
909
910 return ret;
911}
912
Simon Glasse3c23a02015-07-07 20:53:36 -0600913int chunk_msg(struct dwc2_priv *priv, struct usb_device *dev,
Stefan Brüns081dcc72016-01-23 01:42:25 +0100914 unsigned long pipe, u8 *pid, int in, void *buffer, int len)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700915{
Simon Glasse3c23a02015-07-07 20:53:36 -0600916 struct dwc2_core_regs *regs = priv->regs;
Stephen Warren972ad642015-03-07 22:48:52 -0700917 struct dwc2_hc_regs *hc_regs = &regs->hc_regs[DWC2_HC_CHANNEL];
Stefan Brüns247241e2016-01-17 04:09:56 +0100918 struct dwc2_host_regs *host_regs = &regs->host_regs;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700919 int devnum = usb_pipedevice(pipe);
920 int ep = usb_pipeendpoint(pipe);
921 int max = usb_maxpacket(dev, pipe);
Stephen Warren972ad642015-03-07 22:48:52 -0700922 int eptype = dwc2_eptype[usb_pipetype(pipe)];
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700923 int done = 0;
Stephen Warren766fe412015-04-11 21:52:02 -0600924 int ret = 0;
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100925 int do_split = 0;
926 int complete_split = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700927 uint32_t xfer_len;
928 uint32_t num_packets;
929 int stop_transfer = 0;
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100930 uint32_t max_xfer_len;
Stefan Brüns247241e2016-01-17 04:09:56 +0100931 int ssplit_frame_num = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700932
Stephen Warren972ad642015-03-07 22:48:52 -0700933 debug("%s: msg: pipe %lx pid %d in %d len %d\n", __func__, pipe, *pid,
934 in, len);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700935
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100936 max_xfer_len = CONFIG_DWC2_MAX_PACKET_COUNT * max;
937 if (max_xfer_len > CONFIG_DWC2_MAX_TRANSFER_SIZE)
938 max_xfer_len = CONFIG_DWC2_MAX_TRANSFER_SIZE;
939 if (max_xfer_len > DWC2_DATA_BUF_SIZE)
940 max_xfer_len = DWC2_DATA_BUF_SIZE;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700941
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100942 /* Make sure that max_xfer_len is a multiple of max packet size. */
943 num_packets = max_xfer_len / max;
944 max_xfer_len = num_packets * max;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700945
Stefan Brüns2385db32016-01-17 04:09:53 +0100946 /* Initialize channel */
947 dwc_otg_hc_init(regs, DWC2_HC_CHANNEL, dev, devnum, ep, in,
948 eptype, max);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700949
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100950 /* Check if the target is a FS/LS device behind a HS hub */
951 if (dev->speed != USB_SPEED_HIGH) {
952 uint8_t hub_addr;
953 uint8_t hub_port;
954 uint32_t hprt0 = readl(&regs->hprt0);
955 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
956 DWC2_HPRT0_PRTSPD_HIGH) {
957 usb_find_usb2_hub_address_port(dev, &hub_addr,
958 &hub_port);
959 dwc_otg_hc_init_split(hc_regs, hub_addr, hub_port);
960
961 do_split = 1;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700962 num_packets = 1;
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100963 max_xfer_len = max;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700964 }
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100965 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700966
Stefan Brüns2385db32016-01-17 04:09:53 +0100967 do {
968 int actual_len = 0;
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100969 uint32_t hcint;
Stefan Brüns247241e2016-01-17 04:09:56 +0100970 int odd_frame = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700971 xfer_len = len - done;
Stephen Warren972ad642015-03-07 22:48:52 -0700972
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100973 if (xfer_len > max_xfer_len)
974 xfer_len = max_xfer_len;
975 else if (xfer_len > max)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700976 num_packets = (xfer_len + max - 1) / max;
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100977 else
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700978 num_packets = 1;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700979
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100980 if (complete_split)
981 setbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
982 else if (do_split)
983 clrbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
Alexander Stein76fac502015-07-24 09:22:14 +0200984
Stefan Brüns247241e2016-01-17 04:09:56 +0100985 if (eptype == DWC2_HCCHAR_EPTYPE_INTR) {
986 int uframe_num = readl(&host_regs->hfnum);
987 if (!(uframe_num & 0x1))
988 odd_frame = 1;
Simon Glasse3c23a02015-07-07 20:53:36 -0600989 }
Stephen Warren7100da32015-03-08 11:08:13 -0600990
Stefan Brüns2385db32016-01-17 04:09:53 +0100991 ret = transfer_chunk(hc_regs, priv->aligned_buffer, pid,
992 in, (char *)buffer + done, num_packets,
Stefan Brüns247241e2016-01-17 04:09:56 +0100993 xfer_len, &actual_len, odd_frame);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700994
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100995 hcint = readl(&hc_regs->hcint);
996 if (complete_split) {
997 stop_transfer = 0;
Stefan Brüns247241e2016-01-17 04:09:56 +0100998 if (hcint & DWC2_HCINT_NYET) {
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100999 ret = 0;
Stefan Brüns247241e2016-01-17 04:09:56 +01001000 int frame_num = DWC2_HFNUM_MAX_FRNUM &
1001 readl(&host_regs->hfnum);
1002 if (((frame_num - ssplit_frame_num) &
1003 DWC2_HFNUM_MAX_FRNUM) > 4)
1004 ret = -EAGAIN;
1005 } else
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001006 complete_split = 0;
1007 } else if (do_split) {
1008 if (hcint & DWC2_HCINT_ACK) {
Stefan Brüns247241e2016-01-17 04:09:56 +01001009 ssplit_frame_num = DWC2_HFNUM_MAX_FRNUM &
1010 readl(&host_regs->hfnum);
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001011 ret = 0;
1012 complete_split = 1;
1013 }
1014 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001015
Stephen Warren766fe412015-04-11 21:52:02 -06001016 if (ret)
Stephen Warren8a346662015-03-07 22:48:51 -07001017 break;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001018
Stefan Brüns2385db32016-01-17 04:09:53 +01001019 if (actual_len < xfer_len)
1020 stop_transfer = 1;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001021
Stefan Brüns2385db32016-01-17 04:09:53 +01001022 done += actual_len;
Stephen Warren7100da32015-03-08 11:08:13 -06001023
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001024 /* Transactions are done when when either all data is transferred or
1025 * there is a short transfer. In case of a SPLIT make sure the CSPLIT
1026 * is executed.
1027 */
1028 } while (((done < len) && !stop_transfer) || complete_split);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001029
1030 writel(0, &hc_regs->hcintmsk);
1031 writel(0xFFFFFFFF, &hc_regs->hcint);
1032
1033 dev->status = 0;
1034 dev->act_len = done;
1035
Stephen Warren766fe412015-04-11 21:52:02 -06001036 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001037}
1038
Stephen Warren972ad642015-03-07 22:48:52 -07001039/* U-Boot USB transmission interface */
Simon Glasse3c23a02015-07-07 20:53:36 -06001040int _submit_bulk_msg(struct dwc2_priv *priv, struct usb_device *dev,
1041 unsigned long pipe, void *buffer, int len)
Stephen Warren972ad642015-03-07 22:48:52 -07001042{
1043 int devnum = usb_pipedevice(pipe);
1044 int ep = usb_pipeendpoint(pipe);
Stefan Brüns081dcc72016-01-23 01:42:25 +01001045 u8* pid;
Stephen Warren972ad642015-03-07 22:48:52 -07001046
Stefan Brüns081dcc72016-01-23 01:42:25 +01001047 if ((devnum >= MAX_DEVICE) || (devnum == priv->root_hub_devnum)) {
Stephen Warren972ad642015-03-07 22:48:52 -07001048 dev->status = 0;
1049 return -EINVAL;
1050 }
1051
Stefan Brüns081dcc72016-01-23 01:42:25 +01001052 if (usb_pipein(pipe))
1053 pid = &priv->in_data_toggle[devnum][ep];
1054 else
1055 pid = &priv->out_data_toggle[devnum][ep];
1056
1057 return chunk_msg(priv, dev, pipe, pid, usb_pipein(pipe), buffer, len);
Stephen Warren972ad642015-03-07 22:48:52 -07001058}
1059
Simon Glasse3c23a02015-07-07 20:53:36 -06001060static int _submit_control_msg(struct dwc2_priv *priv, struct usb_device *dev,
1061 unsigned long pipe, void *buffer, int len,
1062 struct devrequest *setup)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001063{
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001064 int devnum = usb_pipedevice(pipe);
Stefan Brüns081dcc72016-01-23 01:42:25 +01001065 int ret, act_len;
1066 u8 pid;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001067 /* For CONTROL endpoint pid should start with DATA1 */
1068 int status_direction;
1069
Simon Glasse3c23a02015-07-07 20:53:36 -06001070 if (devnum == priv->root_hub_devnum) {
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001071 dev->status = 0;
1072 dev->speed = USB_SPEED_HIGH;
Simon Glasse3c23a02015-07-07 20:53:36 -06001073 return dwc_otg_submit_rh_msg(priv, dev, pipe, buffer, len,
1074 setup);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001075 }
1076
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001077 /* SETUP stage */
Stephen Warren4db200e2015-03-07 22:48:53 -07001078 pid = DWC2_HC_PID_SETUP;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001079 do {
1080 ret = chunk_msg(priv, dev, pipe, &pid, 0, setup, 8);
1081 } while (ret == -EAGAIN);
Stephen Warren4db200e2015-03-07 22:48:53 -07001082 if (ret)
1083 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001084
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001085 /* DATA stage */
1086 act_len = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001087 if (buffer) {
Stephen Warrenb0ad4a32015-03-07 22:48:54 -07001088 pid = DWC2_HC_PID_DATA1;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001089 do {
1090 ret = chunk_msg(priv, dev, pipe, &pid, usb_pipein(pipe),
1091 buffer, len);
1092 act_len += dev->act_len;
1093 buffer += dev->act_len;
1094 len -= dev->act_len;
1095 } while (ret == -EAGAIN);
Stephen Warren4db200e2015-03-07 22:48:53 -07001096 if (ret)
1097 return ret;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001098 status_direction = usb_pipeout(pipe);
1099 } else {
1100 /* No-data CONTROL always ends with an IN transaction */
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001101 status_direction = 1;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001102 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001103
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001104 /* STATUS stage */
Stephen Warren4db200e2015-03-07 22:48:53 -07001105 pid = DWC2_HC_PID_DATA1;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001106 do {
1107 ret = chunk_msg(priv, dev, pipe, &pid, status_direction,
1108 priv->status_buffer, 0);
1109 } while (ret == -EAGAIN);
Stephen Warren4db200e2015-03-07 22:48:53 -07001110 if (ret)
1111 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001112
Stephen Warren4db200e2015-03-07 22:48:53 -07001113 dev->act_len = act_len;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001114
Stephen Warren8a346662015-03-07 22:48:51 -07001115 return 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001116}
1117
Simon Glasse3c23a02015-07-07 20:53:36 -06001118int _submit_int_msg(struct dwc2_priv *priv, struct usb_device *dev,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001119 unsigned long pipe, void *buffer, int len, int interval,
1120 bool nonblock)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001121{
Stephen Warren766fe412015-04-11 21:52:02 -06001122 unsigned long timeout;
1123 int ret;
1124
Stephen Warrendf7b37d2015-04-10 21:05:22 -06001125 /* FIXME: what is interval? */
Stephen Warren766fe412015-04-11 21:52:02 -06001126
1127 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1128 for (;;) {
1129 if (get_timer(0) > timeout) {
Patrice Chotarda259c1d2018-03-15 18:00:32 +01001130 dev_err(dev, "Timeout poll on interrupt endpoint\n");
Stephen Warren766fe412015-04-11 21:52:02 -06001131 return -ETIMEDOUT;
1132 }
Simon Glasse3c23a02015-07-07 20:53:36 -06001133 ret = _submit_bulk_msg(priv, dev, pipe, buffer, len);
Michal Suchanekc7a7ae52019-08-18 10:55:28 +02001134 if ((ret != -EAGAIN) || nonblock)
Stephen Warren766fe412015-04-11 21:52:02 -06001135 return ret;
1136 }
Ley Foon Tan23865562018-08-29 00:08:48 +08001137}
1138
1139static int dwc2_reset(struct udevice *dev)
1140{
1141 int ret;
1142 struct dwc2_priv *priv = dev_get_priv(dev);
1143
1144 ret = reset_get_bulk(dev, &priv->resets);
1145 if (ret) {
1146 dev_warn(dev, "Can't get reset: %d\n", ret);
1147 /* Return 0 if error due to !CONFIG_DM_RESET and reset
1148 * DT property is not present.
1149 */
1150 if (ret == -ENOENT || ret == -ENOTSUPP)
1151 return 0;
1152 else
1153 return ret;
1154 }
1155
Patrick Delaunay8bef1692020-04-27 15:30:00 +02001156 /* force reset to clear all IP register */
1157 reset_assert_bulk(&priv->resets);
Ley Foon Tan23865562018-08-29 00:08:48 +08001158 ret = reset_deassert_bulk(&priv->resets);
1159 if (ret) {
1160 reset_release_bulk(&priv->resets);
1161 dev_err(dev, "Failed to reset: %d\n", ret);
1162 return ret;
1163 }
1164
1165 return 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001166}
1167
Kever Yang327c24d2017-03-10 12:05:14 +08001168static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001169{
Simon Glasse3c23a02015-07-07 20:53:36 -06001170 struct dwc2_core_regs *regs = priv->regs;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001171 uint32_t snpsid;
1172 int i, j;
Ley Foon Tan23865562018-08-29 00:08:48 +08001173 int ret;
1174
1175 ret = dwc2_reset(dev);
1176 if (ret)
1177 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001178
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001179 snpsid = readl(&regs->gsnpsid);
Patrice Chotarda259c1d2018-03-15 18:00:32 +01001180 dev_info(dev, "Core Release: %x.%03x\n",
1181 snpsid >> 12 & 0xf, snpsid & 0xfff);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001182
Peter Griffin79d657d2015-05-12 14:38:27 +01001183 if ((snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_2xx &&
1184 (snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_3xx) {
Patrice Chotarda259c1d2018-03-15 18:00:32 +01001185 dev_info(dev, "SNPSID invalid (not DWC2 OTG device): %08x\n",
1186 snpsid);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001187 return -ENODEV;
1188 }
1189
Marek Vasut39209492016-04-27 14:55:57 +02001190#ifdef CONFIG_DWC2_PHY_ULPI_EXT_VBUS
1191 priv->ext_vbus = 1;
1192#else
1193 priv->ext_vbus = 0;
1194#endif
1195
Marek Vasut36fc5692016-04-27 14:53:33 +02001196 dwc_otg_core_init(priv);
Kever Yang327c24d2017-03-10 12:05:14 +08001197 dwc_otg_core_host_init(dev, regs);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001198
1199 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
1200 DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
1201 DWC2_HPRT0_PRTOVRCURRCHNG,
1202 DWC2_HPRT0_PRTRST);
1203 mdelay(50);
1204 clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET |
1205 DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG |
1206 DWC2_HPRT0_PRTRST);
1207
1208 for (i = 0; i < MAX_DEVICE; i++) {
Stefan Brüns081dcc72016-01-23 01:42:25 +01001209 for (j = 0; j < MAX_ENDPOINT; j++) {
1210 priv->in_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1211 priv->out_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1212 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001213 }
1214
Stefan Roesec526e832016-05-06 13:53:37 +02001215 /*
1216 * Add a 1 second delay here. This gives the host controller
1217 * a bit time before the comminucation with the USB devices
1218 * is started (the bus is scanned) and fixes the USB detection
1219 * problems with some problematic USB keys.
1220 */
1221 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
1222 mdelay(1000);
1223
Patrick Delaunayce17fe12020-04-27 15:30:01 +02001224 printf("USB DWC2\n");
1225
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001226 return 0;
1227}
1228
Simon Glasse3c23a02015-07-07 20:53:36 -06001229static void dwc2_uninit_common(struct dwc2_core_regs *regs)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001230{
1231 /* Put everything in reset. */
1232 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
1233 DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
1234 DWC2_HPRT0_PRTOVRCURRCHNG,
1235 DWC2_HPRT0_PRTRST);
Simon Glasse3c23a02015-07-07 20:53:36 -06001236}
1237
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01001238#if !CONFIG_IS_ENABLED(DM_USB)
Simon Glasse3c23a02015-07-07 20:53:36 -06001239int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1240 int len, struct devrequest *setup)
1241{
1242 return _submit_control_msg(&local, dev, pipe, buffer, len, setup);
1243}
1244
1245int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1246 int len)
1247{
1248 return _submit_bulk_msg(&local, dev, pipe, buffer, len);
1249}
1250
1251int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001252 int len, int interval, bool nonblock)
Simon Glasse3c23a02015-07-07 20:53:36 -06001253{
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001254 return _submit_int_msg(&local, dev, pipe, buffer, len, interval,
1255 nonblock);
Simon Glasse3c23a02015-07-07 20:53:36 -06001256}
1257
1258/* U-Boot USB control interface */
1259int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1260{
1261 struct dwc2_priv *priv = &local;
1262
1263 memset(priv, '\0', sizeof(*priv));
1264 priv->root_hub_devnum = 0;
1265 priv->regs = (struct dwc2_core_regs *)CONFIG_USB_DWC2_REG_ADDR;
1266 priv->aligned_buffer = aligned_buffer_addr;
1267 priv->status_buffer = status_buffer_addr;
1268
1269 /* board-dependant init */
1270 if (board_usb_init(index, USB_INIT_HOST))
1271 return -1;
1272
Kever Yang327c24d2017-03-10 12:05:14 +08001273 return dwc2_init_common(NULL, priv);
Simon Glasse3c23a02015-07-07 20:53:36 -06001274}
1275
1276int usb_lowlevel_stop(int index)
1277{
1278 dwc2_uninit_common(local.regs);
1279
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001280 return 0;
1281}
Simon Glassa7ea72c2015-07-07 20:53:37 -06001282#endif
1283
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01001284#if CONFIG_IS_ENABLED(DM_USB)
Simon Glassa7ea72c2015-07-07 20:53:37 -06001285static int dwc2_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1286 unsigned long pipe, void *buffer, int length,
1287 struct devrequest *setup)
1288{
1289 struct dwc2_priv *priv = dev_get_priv(dev);
1290
1291 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1292 dev->name, udev, udev->dev->name, udev->portnr);
1293
1294 return _submit_control_msg(priv, udev, pipe, buffer, length, setup);
1295}
1296
1297static int dwc2_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1298 unsigned long pipe, void *buffer, int length)
1299{
1300 struct dwc2_priv *priv = dev_get_priv(dev);
1301
1302 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1303
1304 return _submit_bulk_msg(priv, udev, pipe, buffer, length);
1305}
1306
1307static int dwc2_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1308 unsigned long pipe, void *buffer, int length,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001309 int interval, bool nonblock)
Simon Glassa7ea72c2015-07-07 20:53:37 -06001310{
1311 struct dwc2_priv *priv = dev_get_priv(dev);
1312
1313 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1314
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001315 return _submit_int_msg(priv, udev, pipe, buffer, length, interval,
1316 nonblock);
Simon Glassa7ea72c2015-07-07 20:53:37 -06001317}
1318
1319static int dwc2_usb_ofdata_to_platdata(struct udevice *dev)
1320{
1321 struct dwc2_priv *priv = dev_get_priv(dev);
1322 fdt_addr_t addr;
1323
Philipp Tomsich77b9e2e2017-09-12 17:32:27 +02001324 addr = dev_read_addr(dev);
Simon Glassa7ea72c2015-07-07 20:53:37 -06001325 if (addr == FDT_ADDR_T_NONE)
1326 return -EINVAL;
1327 priv->regs = (struct dwc2_core_regs *)addr;
1328
Meng Dongyang697a8bc2017-06-28 19:22:43 +08001329 priv->oc_disable = dev_read_bool(dev, "disable-over-current");
1330 priv->hnp_srp_disable = dev_read_bool(dev, "hnp-srp-disable");
Meng Dongyangcc3fe062017-06-08 15:34:20 +08001331
Simon Glassa7ea72c2015-07-07 20:53:37 -06001332 return 0;
1333}
1334
Patrick Delaunaybb3569d2020-04-27 15:29:58 +02001335static int dwc2_setup_phy(struct udevice *dev)
1336{
1337 struct dwc2_priv *priv = dev_get_priv(dev);
1338 int ret;
1339
1340 ret = generic_phy_get_by_index(dev, 0, &priv->phy);
1341 if (ret) {
1342 if (ret == -ENOENT)
1343 return 0; /* no PHY, nothing to do */
1344 dev_err(dev, "Failed to get USB PHY: %d.\n", ret);
1345 return ret;
1346 }
1347
1348 ret = generic_phy_init(&priv->phy);
1349 if (ret) {
1350 dev_dbg(dev, "Failed to init USB PHY: %d.\n", ret);
1351 return ret;
1352 }
1353
1354 ret = generic_phy_power_on(&priv->phy);
1355 if (ret) {
1356 dev_dbg(dev, "Failed to power on USB PHY: %d.\n", ret);
1357 generic_phy_exit(&priv->phy);
1358 return ret;
1359 }
1360
1361 return 0;
1362}
1363
1364static int dwc2_shutdown_phy(struct udevice *dev)
1365{
1366 struct dwc2_priv *priv = dev_get_priv(dev);
1367 int ret;
1368
1369 /* PHY is not valid when generic_phy_get_by_index() = -ENOENT */
1370 if (!generic_phy_valid(&priv->phy))
1371 return 0; /* no PHY, nothing to do */
1372
1373 ret = generic_phy_power_off(&priv->phy);
1374 if (ret) {
1375 dev_dbg(dev, "Failed to power off USB PHY: %d.\n", ret);
1376 return ret;
1377 }
1378
1379 ret = generic_phy_exit(&priv->phy);
1380 if (ret) {
1381 dev_dbg(dev, "Failed to power off USB PHY: %d.\n", ret);
1382 return ret;
1383 }
1384
1385 return 0;
1386}
1387
Patrick Delaunay9225f3e2020-04-27 15:29:59 +02001388static int dwc2_clk_init(struct udevice *dev)
1389{
1390 struct dwc2_priv *priv = dev_get_priv(dev);
1391 int ret;
1392
1393 ret = clk_get_bulk(dev, &priv->clks);
1394 if (ret == -ENOSYS || ret == -ENOENT)
1395 return 0;
1396 if (ret)
1397 return ret;
1398
1399 ret = clk_enable_bulk(&priv->clks);
1400 if (ret) {
1401 clk_release_bulk(&priv->clks);
1402 return ret;
1403 }
1404
1405 return 0;
1406}
1407
Simon Glassa7ea72c2015-07-07 20:53:37 -06001408static int dwc2_usb_probe(struct udevice *dev)
1409{
1410 struct dwc2_priv *priv = dev_get_priv(dev);
Marek Vasut1ea9ac62016-04-26 03:02:35 +02001411 struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
Patrick Delaunaybb3569d2020-04-27 15:29:58 +02001412 int ret;
Marek Vasut1ea9ac62016-04-26 03:02:35 +02001413
1414 bus_priv->desc_before_addr = true;
Simon Glassa7ea72c2015-07-07 20:53:37 -06001415
Patrick Delaunay9225f3e2020-04-27 15:29:59 +02001416 ret = dwc2_clk_init(dev);
1417 if (ret)
1418 return ret;
1419
Patrick Delaunaybb3569d2020-04-27 15:29:58 +02001420 ret = dwc2_setup_phy(dev);
1421 if (ret)
1422 return ret;
1423
Kever Yang327c24d2017-03-10 12:05:14 +08001424 return dwc2_init_common(dev, priv);
Simon Glassa7ea72c2015-07-07 20:53:37 -06001425}
1426
1427static int dwc2_usb_remove(struct udevice *dev)
1428{
1429 struct dwc2_priv *priv = dev_get_priv(dev);
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +01001430 int ret;
1431
1432 ret = dwc_vbus_supply_exit(dev);
1433 if (ret)
1434 return ret;
Simon Glassa7ea72c2015-07-07 20:53:37 -06001435
Patrick Delaunaybb3569d2020-04-27 15:29:58 +02001436 ret = dwc2_shutdown_phy(dev);
1437 if (ret) {
1438 dev_dbg(dev, "Failed to shutdown USB PHY: %d.\n", ret);
1439 return ret;
1440 }
1441
Simon Glassa7ea72c2015-07-07 20:53:37 -06001442 dwc2_uninit_common(priv->regs);
1443
Ley Foon Tan23865562018-08-29 00:08:48 +08001444 reset_release_bulk(&priv->resets);
Patrick Delaunay9225f3e2020-04-27 15:29:59 +02001445 clk_disable_bulk(&priv->clks);
1446 clk_release_bulk(&priv->clks);
Ley Foon Tan23865562018-08-29 00:08:48 +08001447
Simon Glassa7ea72c2015-07-07 20:53:37 -06001448 return 0;
1449}
1450
1451struct dm_usb_ops dwc2_usb_ops = {
1452 .control = dwc2_submit_control_msg,
1453 .bulk = dwc2_submit_bulk_msg,
1454 .interrupt = dwc2_submit_int_msg,
1455};
1456
1457static const struct udevice_id dwc2_usb_ids[] = {
1458 { .compatible = "brcm,bcm2835-usb" },
Emmanuel Vadot80447002018-07-02 14:34:23 +02001459 { .compatible = "brcm,bcm2708-usb" },
Marek Vasutac4a35f2015-08-12 22:19:14 +02001460 { .compatible = "snps,dwc2" },
Simon Glassa7ea72c2015-07-07 20:53:37 -06001461 { }
1462};
1463
1464U_BOOT_DRIVER(usb_dwc2) = {
Marek Vasutaf83c782015-08-12 22:19:15 +02001465 .name = "dwc2_usb",
Simon Glassa7ea72c2015-07-07 20:53:37 -06001466 .id = UCLASS_USB,
1467 .of_match = dwc2_usb_ids,
1468 .ofdata_to_platdata = dwc2_usb_ofdata_to_platdata,
1469 .probe = dwc2_usb_probe,
1470 .remove = dwc2_usb_remove,
1471 .ops = &dwc2_usb_ops,
1472 .priv_auto_alloc_size = sizeof(struct dwc2_priv),
1473 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1474};
1475#endif