blob: ec643e9f45b9ef9c451efd712baa3012ed73d545 [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>
Simon Glassdbd79542020-05-10 11:40:11 -060023#include <linux/delay.h>
Kever Yang327c24d2017-03-10 12:05:14 +080024#include <power/regulator.h>
Ley Foon Tan23865562018-08-29 00:08:48 +080025#include <reset.h>
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070026
27#include "dwc2.h"
28
29/* Use only HC channel 0. */
30#define DWC2_HC_CHANNEL 0
31
32#define DWC2_STATUS_BUF_SIZE 64
Alexey Brodkinf19414b2018-02-28 16:16:58 +030033#define DWC2_DATA_BUF_SIZE (CONFIG_USB_DWC2_BUFFER_SIZE * 1024)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070034
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070035#define MAX_DEVICE 16
36#define MAX_ENDPOINT 16
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070037
Simon Glasse3c23a02015-07-07 20:53:36 -060038struct dwc2_priv {
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +010039#if CONFIG_IS_ENABLED(DM_USB)
Alexander Stein76fac502015-07-24 09:22:14 +020040 uint8_t aligned_buffer[DWC2_DATA_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
41 uint8_t status_buffer[DWC2_STATUS_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +010042#ifdef CONFIG_DM_REGULATOR
43 struct udevice *vbus_supply;
44#endif
Patrick Delaunaybb3569d2020-04-27 15:29:58 +020045 struct phy phy;
Patrick Delaunay9225f3e2020-04-27 15:29:59 +020046 struct clk_bulk clks;
Simon Glassa7ea72c2015-07-07 20:53:37 -060047#else
Simon Glasse3c23a02015-07-07 20:53:36 -060048 uint8_t *aligned_buffer;
49 uint8_t *status_buffer;
Simon Glassa7ea72c2015-07-07 20:53:37 -060050#endif
Stefan Brüns081dcc72016-01-23 01:42:25 +010051 u8 in_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
52 u8 out_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
Simon Glasse3c23a02015-07-07 20:53:36 -060053 struct dwc2_core_regs *regs;
54 int root_hub_devnum;
Marek Vasut39209492016-04-27 14:55:57 +020055 bool ext_vbus;
Meng Dongyang697a8bc2017-06-28 19:22:43 +080056 /*
57 * The hnp/srp capability must be disabled if the platform
58 * does't support hnp/srp. Otherwise the force mode can't work.
59 */
Meng Dongyangcc3fe062017-06-08 15:34:20 +080060 bool hnp_srp_disable;
Marek Vasut43db5a62016-04-27 14:58:49 +020061 bool oc_disable;
Ley Foon Tan23865562018-08-29 00:08:48 +080062
63 struct reset_ctl_bulk resets;
Simon Glasse3c23a02015-07-07 20:53:36 -060064};
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070065
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +010066#if !CONFIG_IS_ENABLED(DM_USB)
Alexander Stein76fac502015-07-24 09:22:14 +020067/* We need cacheline-aligned buffers for DMA transfers and dcache support */
68DEFINE_ALIGN_BUFFER(uint8_t, aligned_buffer_addr, DWC2_DATA_BUF_SIZE,
69 ARCH_DMA_MINALIGN);
70DEFINE_ALIGN_BUFFER(uint8_t, status_buffer_addr, DWC2_STATUS_BUF_SIZE,
71 ARCH_DMA_MINALIGN);
Simon Glasse3c23a02015-07-07 20:53:36 -060072
73static struct dwc2_priv local;
Simon Glassa7ea72c2015-07-07 20:53:37 -060074#endif
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070075
76/*
77 * DWC2 IP interface
78 */
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070079
80/*
81 * Initializes the FSLSPClkSel field of the HCFG register
82 * depending on the PHY type.
83 */
84static void init_fslspclksel(struct dwc2_core_regs *regs)
85{
86 uint32_t phyclk;
87
88#if (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
89 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
90#else
91 /* High speed PHY running at full speed or high speed */
92 phyclk = DWC2_HCFG_FSLSPCLKSEL_30_60_MHZ;
93#endif
94
95#ifdef CONFIG_DWC2_ULPI_FS_LS
96 uint32_t hwcfg2 = readl(&regs->ghwcfg2);
97 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
98 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
99 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
100 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
101
102 if (hval == 2 && fval == 1)
103 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
104#endif
105
106 clrsetbits_le32(&regs->host_regs.hcfg,
107 DWC2_HCFG_FSLSPCLKSEL_MASK,
108 phyclk << DWC2_HCFG_FSLSPCLKSEL_OFFSET);
109}
110
111/*
112 * Flush a Tx FIFO.
113 *
114 * @param regs Programming view of DWC_otg controller.
115 * @param num Tx FIFO to flush.
116 */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400117static void dwc_otg_flush_tx_fifo(struct udevice *dev,
118 struct dwc2_core_regs *regs, const int num)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700119{
120 int ret;
121
122 writel(DWC2_GRSTCTL_TXFFLSH | (num << DWC2_GRSTCTL_TXFNUM_OFFSET),
123 &regs->grstctl);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100124 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_TXFFLSH,
125 false, 1000, false);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700126 if (ret)
Patrice Chotarda259c1d2018-03-15 18:00:32 +0100127 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700128
129 /* Wait for 3 PHY Clocks */
130 udelay(1);
131}
132
133/*
134 * Flush Rx FIFO.
135 *
136 * @param regs Programming view of DWC_otg controller.
137 */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400138static void dwc_otg_flush_rx_fifo(struct udevice *dev,
139 struct dwc2_core_regs *regs)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700140{
141 int ret;
142
143 writel(DWC2_GRSTCTL_RXFFLSH, &regs->grstctl);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100144 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_RXFFLSH,
145 false, 1000, false);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700146 if (ret)
Patrice Chotarda259c1d2018-03-15 18:00:32 +0100147 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700148
149 /* Wait for 3 PHY Clocks */
150 udelay(1);
151}
152
153/*
154 * Do core a soft reset of the core. Be careful with this because it
155 * resets all the internal state machines of the core.
156 */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400157static void dwc_otg_core_reset(struct udevice *dev,
158 struct dwc2_core_regs *regs)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700159{
160 int ret;
161
162 /* Wait for AHB master IDLE state. */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100163 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_AHBIDLE,
164 true, 1000, false);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700165 if (ret)
Patrice Chotarda259c1d2018-03-15 18:00:32 +0100166 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700167
168 /* Core Soft Reset */
169 writel(DWC2_GRSTCTL_CSFTRST, &regs->grstctl);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100170 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_CSFTRST,
171 false, 1000, false);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700172 if (ret)
Patrice Chotarda259c1d2018-03-15 18:00:32 +0100173 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700174
175 /*
176 * Wait for core to come out of reset.
177 * NOTE: This long sleep is _very_ important, otherwise the core will
178 * not stay in host mode after a connector ID change!
179 */
180 mdelay(100);
181}
182
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100183#if CONFIG_IS_ENABLED(DM_USB) && defined(CONFIG_DM_REGULATOR)
Kever Yang327c24d2017-03-10 12:05:14 +0800184static int dwc_vbus_supply_init(struct udevice *dev)
185{
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100186 struct dwc2_priv *priv = dev_get_priv(dev);
Kever Yang327c24d2017-03-10 12:05:14 +0800187 int ret;
188
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100189 ret = device_get_supply_regulator(dev, "vbus-supply",
190 &priv->vbus_supply);
Kever Yang327c24d2017-03-10 12:05:14 +0800191 if (ret) {
192 debug("%s: No vbus supply\n", dev->name);
193 return 0;
194 }
195
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100196 ret = regulator_set_enable(priv->vbus_supply, true);
Kever Yang327c24d2017-03-10 12:05:14 +0800197 if (ret) {
Patrice Chotarda259c1d2018-03-15 18:00:32 +0100198 dev_err(dev, "Error enabling vbus supply\n");
Kever Yang327c24d2017-03-10 12:05:14 +0800199 return ret;
200 }
201
202 return 0;
203}
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100204
205static int dwc_vbus_supply_exit(struct udevice *dev)
206{
207 struct dwc2_priv *priv = dev_get_priv(dev);
208 int ret;
209
210 if (priv->vbus_supply) {
211 ret = regulator_set_enable(priv->vbus_supply, false);
212 if (ret) {
213 dev_err(dev, "Error disabling vbus supply\n");
214 return ret;
215 }
216 }
217
218 return 0;
219}
Kever Yang327c24d2017-03-10 12:05:14 +0800220#else
221static int dwc_vbus_supply_init(struct udevice *dev)
222{
223 return 0;
224}
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100225
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100226#if CONFIG_IS_ENABLED(DM_USB)
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100227static int dwc_vbus_supply_exit(struct udevice *dev)
228{
229 return 0;
230}
Kever Yang327c24d2017-03-10 12:05:14 +0800231#endif
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100232#endif
Kever Yang327c24d2017-03-10 12:05:14 +0800233
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700234/*
235 * This function initializes the DWC_otg controller registers for
236 * host mode.
237 *
238 * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
239 * request queues. Host channels are reset to ensure that they are ready for
240 * performing transfers.
241 *
Kever Yang327c24d2017-03-10 12:05:14 +0800242 * @param dev USB Device (NULL if driver model is not being used)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700243 * @param regs Programming view of DWC_otg controller
244 *
245 */
Kever Yang327c24d2017-03-10 12:05:14 +0800246static void dwc_otg_core_host_init(struct udevice *dev,
247 struct dwc2_core_regs *regs)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700248{
249 uint32_t nptxfifosize = 0;
250 uint32_t ptxfifosize = 0;
251 uint32_t hprt0 = 0;
252 int i, ret, num_channels;
253
254 /* Restart the Phy Clock */
255 writel(0, &regs->pcgcctl);
256
257 /* Initialize Host Configuration Register */
258 init_fslspclksel(regs);
259#ifdef CONFIG_DWC2_DFLT_SPEED_FULL
260 setbits_le32(&regs->host_regs.hcfg, DWC2_HCFG_FSLSSUPP);
261#endif
262
263 /* Configure data FIFO sizes */
264#ifdef CONFIG_DWC2_ENABLE_DYNAMIC_FIFO
265 if (readl(&regs->ghwcfg2) & DWC2_HWCFG2_DYNAMIC_FIFO) {
266 /* Rx FIFO */
267 writel(CONFIG_DWC2_HOST_RX_FIFO_SIZE, &regs->grxfsiz);
268
269 /* Non-periodic Tx FIFO */
270 nptxfifosize |= CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE <<
271 DWC2_FIFOSIZE_DEPTH_OFFSET;
272 nptxfifosize |= CONFIG_DWC2_HOST_RX_FIFO_SIZE <<
273 DWC2_FIFOSIZE_STARTADDR_OFFSET;
274 writel(nptxfifosize, &regs->gnptxfsiz);
275
276 /* Periodic Tx FIFO */
277 ptxfifosize |= CONFIG_DWC2_HOST_PERIO_TX_FIFO_SIZE <<
278 DWC2_FIFOSIZE_DEPTH_OFFSET;
279 ptxfifosize |= (CONFIG_DWC2_HOST_RX_FIFO_SIZE +
280 CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE) <<
281 DWC2_FIFOSIZE_STARTADDR_OFFSET;
282 writel(ptxfifosize, &regs->hptxfsiz);
283 }
284#endif
285
286 /* Clear Host Set HNP Enable in the OTG Control Register */
287 clrbits_le32(&regs->gotgctl, DWC2_GOTGCTL_HSTSETHNPEN);
288
289 /* Make sure the FIFOs are flushed. */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400290 dwc_otg_flush_tx_fifo(dev, regs, 0x10); /* All Tx FIFOs */
291 dwc_otg_flush_rx_fifo(dev, regs);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700292
293 /* Flush out any leftover queued requests. */
294 num_channels = readl(&regs->ghwcfg2);
295 num_channels &= DWC2_HWCFG2_NUM_HOST_CHAN_MASK;
296 num_channels >>= DWC2_HWCFG2_NUM_HOST_CHAN_OFFSET;
297 num_channels += 1;
298
299 for (i = 0; i < num_channels; i++)
300 clrsetbits_le32(&regs->hc_regs[i].hcchar,
301 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_EPDIR,
302 DWC2_HCCHAR_CHDIS);
303
304 /* Halt all channels to put them into a known state. */
305 for (i = 0; i < num_channels; i++) {
306 clrsetbits_le32(&regs->hc_regs[i].hcchar,
307 DWC2_HCCHAR_EPDIR,
308 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100309 ret = wait_for_bit_le32(&regs->hc_regs[i].hcchar,
310 DWC2_HCCHAR_CHEN, false, 1000, false);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700311 if (ret)
Sean Anderson62eff9b2020-09-15 10:45:15 -0400312 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700313 }
314
315 /* Turn on the vbus power. */
316 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST) {
317 hprt0 = readl(&regs->hprt0);
318 hprt0 &= ~(DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET);
319 hprt0 &= ~(DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG);
320 if (!(hprt0 & DWC2_HPRT0_PRTPWR)) {
321 hprt0 |= DWC2_HPRT0_PRTPWR;
322 writel(hprt0, &regs->hprt0);
323 }
324 }
Kever Yang327c24d2017-03-10 12:05:14 +0800325
326 if (dev)
327 dwc_vbus_supply_init(dev);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700328}
329
330/*
331 * This function initializes the DWC_otg controller registers and
332 * prepares the core for device mode or host mode operation.
333 *
334 * @param regs Programming view of the DWC_otg controller
335 */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400336static void dwc_otg_core_init(struct udevice *dev)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700337{
Sean Anderson62eff9b2020-09-15 10:45:15 -0400338 struct dwc2_priv *priv = dev_get_priv(dev);
Marek Vasut36fc5692016-04-27 14:53:33 +0200339 struct dwc2_core_regs *regs = priv->regs;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700340 uint32_t ahbcfg = 0;
341 uint32_t usbcfg = 0;
342 uint8_t brst_sz = CONFIG_DWC2_DMA_BURST_SIZE;
343
344 /* Common Initialization */
345 usbcfg = readl(&regs->gusbcfg);
346
347 /* Program the ULPI External VBUS bit if needed */
Marek Vasut39209492016-04-27 14:55:57 +0200348 if (priv->ext_vbus) {
Marek Vasut43db5a62016-04-27 14:58:49 +0200349 usbcfg |= DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
350 if (!priv->oc_disable) {
351 usbcfg |= DWC2_GUSBCFG_ULPI_INT_VBUS_INDICATOR |
352 DWC2_GUSBCFG_INDICATOR_PASSTHROUGH;
353 }
Marek Vasut39209492016-04-27 14:55:57 +0200354 } else {
355 usbcfg &= ~DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
356 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700357
358 /* Set external TS Dline pulsing */
359#ifdef CONFIG_DWC2_TS_DLINE
360 usbcfg |= DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
361#else
362 usbcfg &= ~DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
363#endif
364 writel(usbcfg, &regs->gusbcfg);
365
366 /* Reset the Controller */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400367 dwc_otg_core_reset(dev, regs);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700368
369 /*
370 * This programming sequence needs to happen in FS mode before
371 * any other programming occurs
372 */
373#if defined(CONFIG_DWC2_DFLT_SPEED_FULL) && \
374 (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
375 /* If FS mode with FS PHY */
376 setbits_le32(&regs->gusbcfg, DWC2_GUSBCFG_PHYSEL);
377
378 /* Reset after a PHY select */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400379 dwc_otg_core_reset(dev, regs);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700380
381 /*
382 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS.
383 * Also do this on HNP Dev/Host mode switches (done in dev_init
384 * and host_init).
385 */
386 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
387 init_fslspclksel(regs);
388
389#ifdef CONFIG_DWC2_I2C_ENABLE
390 /* Program GUSBCFG.OtgUtmifsSel to I2C */
391 setbits_le32(&regs->gusbcfg, DWC2_GUSBCFG_OTGUTMIFSSEL);
392
393 /* Program GI2CCTL.I2CEn */
394 clrsetbits_le32(&regs->gi2cctl, DWC2_GI2CCTL_I2CEN |
395 DWC2_GI2CCTL_I2CDEVADDR_MASK,
396 1 << DWC2_GI2CCTL_I2CDEVADDR_OFFSET);
397 setbits_le32(&regs->gi2cctl, DWC2_GI2CCTL_I2CEN);
398#endif
399
400#else
401 /* High speed PHY. */
402
403 /*
404 * HS PHY parameters. These parameters are preserved during
405 * soft reset so only program the first time. Do a soft reset
406 * immediately after setting phyif.
407 */
408 usbcfg &= ~(DWC2_GUSBCFG_ULPI_UTMI_SEL | DWC2_GUSBCFG_PHYIF);
409 usbcfg |= CONFIG_DWC2_PHY_TYPE << DWC2_GUSBCFG_ULPI_UTMI_SEL_OFFSET;
410
411 if (usbcfg & DWC2_GUSBCFG_ULPI_UTMI_SEL) { /* ULPI interface */
412#ifdef CONFIG_DWC2_PHY_ULPI_DDR
413 usbcfg |= DWC2_GUSBCFG_DDRSEL;
414#else
415 usbcfg &= ~DWC2_GUSBCFG_DDRSEL;
416#endif
417 } else { /* UTMI+ interface */
Alexey Brodkin7628ad22018-01-31 17:56:59 +0300418#if (CONFIG_DWC2_UTMI_WIDTH == 16)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700419 usbcfg |= DWC2_GUSBCFG_PHYIF;
420#endif
421 }
422
423 writel(usbcfg, &regs->gusbcfg);
424
425 /* Reset after setting the PHY parameters */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400426 dwc_otg_core_reset(dev, regs);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700427#endif
428
429 usbcfg = readl(&regs->gusbcfg);
430 usbcfg &= ~(DWC2_GUSBCFG_ULPI_FSLS | DWC2_GUSBCFG_ULPI_CLK_SUS_M);
431#ifdef CONFIG_DWC2_ULPI_FS_LS
432 uint32_t hwcfg2 = readl(&regs->ghwcfg2);
433 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
434 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
435 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
436 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
437 if (hval == 2 && fval == 1) {
438 usbcfg |= DWC2_GUSBCFG_ULPI_FSLS;
439 usbcfg |= DWC2_GUSBCFG_ULPI_CLK_SUS_M;
440 }
441#endif
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800442 if (priv->hnp_srp_disable)
443 usbcfg |= DWC2_GUSBCFG_FORCEHOSTMODE;
444
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700445 writel(usbcfg, &regs->gusbcfg);
446
447 /* Program the GAHBCFG Register. */
448 switch (readl(&regs->ghwcfg2) & DWC2_HWCFG2_ARCHITECTURE_MASK) {
449 case DWC2_HWCFG2_ARCHITECTURE_SLAVE_ONLY:
450 break;
451 case DWC2_HWCFG2_ARCHITECTURE_EXT_DMA:
452 while (brst_sz > 1) {
453 ahbcfg |= ahbcfg + (1 << DWC2_GAHBCFG_HBURSTLEN_OFFSET);
454 ahbcfg &= DWC2_GAHBCFG_HBURSTLEN_MASK;
455 brst_sz >>= 1;
456 }
457
458#ifdef CONFIG_DWC2_DMA_ENABLE
459 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
460#endif
461 break;
462
463 case DWC2_HWCFG2_ARCHITECTURE_INT_DMA:
464 ahbcfg |= DWC2_GAHBCFG_HBURSTLEN_INCR4;
465#ifdef CONFIG_DWC2_DMA_ENABLE
466 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
467#endif
468 break;
469 }
470
471 writel(ahbcfg, &regs->gahbcfg);
472
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800473 /* Program the capabilities in GUSBCFG Register */
474 usbcfg = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700475
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800476 if (!priv->hnp_srp_disable)
477 usbcfg |= DWC2_GUSBCFG_HNPCAP | DWC2_GUSBCFG_SRPCAP;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700478#ifdef CONFIG_DWC2_IC_USB_CAP
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800479 usbcfg |= DWC2_GUSBCFG_IC_USB_CAP;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700480#endif
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800481
482 setbits_le32(&regs->gusbcfg, usbcfg);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700483}
484
485/*
486 * Prepares a host channel for transferring packets to/from a specific
487 * endpoint. The HCCHARn register is set up with the characteristics specified
488 * in _hc. Host channel interrupts that may need to be serviced while this
489 * transfer is in progress are enabled.
490 *
491 * @param regs Programming view of DWC_otg controller
492 * @param hc Information needed to initialize the host channel
493 */
494static void dwc_otg_hc_init(struct dwc2_core_regs *regs, uint8_t hc_num,
Stephen Warrendead8db2015-04-10 21:05:21 -0600495 struct usb_device *dev, uint8_t dev_addr, uint8_t ep_num,
496 uint8_t ep_is_in, uint8_t ep_type, uint16_t max_packet)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700497{
498 struct dwc2_hc_regs *hc_regs = &regs->hc_regs[hc_num];
Stephen Warrendead8db2015-04-10 21:05:21 -0600499 uint32_t hcchar = (dev_addr << DWC2_HCCHAR_DEVADDR_OFFSET) |
500 (ep_num << DWC2_HCCHAR_EPNUM_OFFSET) |
501 (ep_is_in << DWC2_HCCHAR_EPDIR_OFFSET) |
502 (ep_type << DWC2_HCCHAR_EPTYPE_OFFSET) |
503 (max_packet << DWC2_HCCHAR_MPS_OFFSET);
504
505 if (dev->speed == USB_SPEED_LOW)
506 hcchar |= DWC2_HCCHAR_LSPDDEV;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700507
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700508 /*
509 * Program the HCCHARn register with the endpoint characteristics
510 * for the current transfer.
511 */
512 writel(hcchar, &hc_regs->hcchar);
513
Stefan Brüns2e194e22016-01-17 04:09:54 +0100514 /* Program the HCSPLIT register, default to no SPLIT */
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700515 writel(0, &hc_regs->hcsplt);
516}
517
Stefan Brüns2e194e22016-01-17 04:09:54 +0100518static void dwc_otg_hc_init_split(struct dwc2_hc_regs *hc_regs,
519 uint8_t hub_devnum, uint8_t hub_port)
520{
521 uint32_t hcsplt = 0;
522
523 hcsplt = DWC2_HCSPLT_SPLTENA;
524 hcsplt |= hub_devnum << DWC2_HCSPLT_HUBADDR_OFFSET;
525 hcsplt |= hub_port << DWC2_HCSPLT_PRTADDR_OFFSET;
526
527 /* Program the HCSPLIT register for SPLITs */
528 writel(hcsplt, &hc_regs->hcsplt);
529}
530
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700531/*
532 * DWC2 to USB API interface
533 */
534/* Direction: In ; Request: Status */
Simon Glasse3c23a02015-07-07 20:53:36 -0600535static int dwc_otg_submit_rh_msg_in_status(struct dwc2_core_regs *regs,
536 struct usb_device *dev, void *buffer,
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700537 int txlen, struct devrequest *cmd)
538{
539 uint32_t hprt0 = 0;
540 uint32_t port_status = 0;
541 uint32_t port_change = 0;
542 int len = 0;
543 int stat = 0;
544
545 switch (cmd->requesttype & ~USB_DIR_IN) {
546 case 0:
547 *(uint16_t *)buffer = cpu_to_le16(1);
548 len = 2;
549 break;
550 case USB_RECIP_INTERFACE:
551 case USB_RECIP_ENDPOINT:
552 *(uint16_t *)buffer = cpu_to_le16(0);
553 len = 2;
554 break;
555 case USB_TYPE_CLASS:
556 *(uint32_t *)buffer = cpu_to_le32(0);
557 len = 4;
558 break;
559 case USB_RECIP_OTHER | USB_TYPE_CLASS:
560 hprt0 = readl(&regs->hprt0);
561 if (hprt0 & DWC2_HPRT0_PRTCONNSTS)
562 port_status |= USB_PORT_STAT_CONNECTION;
563 if (hprt0 & DWC2_HPRT0_PRTENA)
564 port_status |= USB_PORT_STAT_ENABLE;
565 if (hprt0 & DWC2_HPRT0_PRTSUSP)
566 port_status |= USB_PORT_STAT_SUSPEND;
567 if (hprt0 & DWC2_HPRT0_PRTOVRCURRACT)
568 port_status |= USB_PORT_STAT_OVERCURRENT;
569 if (hprt0 & DWC2_HPRT0_PRTRST)
570 port_status |= USB_PORT_STAT_RESET;
571 if (hprt0 & DWC2_HPRT0_PRTPWR)
572 port_status |= USB_PORT_STAT_POWER;
573
Stephen Warrend3388f82015-03-27 21:55:38 -0600574 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) == DWC2_HPRT0_PRTSPD_LOW)
575 port_status |= USB_PORT_STAT_LOW_SPEED;
576 else if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
577 DWC2_HPRT0_PRTSPD_HIGH)
578 port_status |= USB_PORT_STAT_HIGH_SPEED;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700579
580 if (hprt0 & DWC2_HPRT0_PRTENCHNG)
581 port_change |= USB_PORT_STAT_C_ENABLE;
582 if (hprt0 & DWC2_HPRT0_PRTCONNDET)
583 port_change |= USB_PORT_STAT_C_CONNECTION;
584 if (hprt0 & DWC2_HPRT0_PRTOVRCURRCHNG)
585 port_change |= USB_PORT_STAT_C_OVERCURRENT;
586
587 *(uint32_t *)buffer = cpu_to_le32(port_status |
588 (port_change << 16));
589 len = 4;
590 break;
591 default:
592 puts("unsupported root hub command\n");
593 stat = USB_ST_STALLED;
594 }
595
596 dev->act_len = min(len, txlen);
597 dev->status = stat;
598
599 return stat;
600}
601
602/* Direction: In ; Request: Descriptor */
603static int dwc_otg_submit_rh_msg_in_descriptor(struct usb_device *dev,
604 void *buffer, int txlen,
605 struct devrequest *cmd)
606{
607 unsigned char data[32];
608 uint32_t dsc;
609 int len = 0;
610 int stat = 0;
611 uint16_t wValue = cpu_to_le16(cmd->value);
612 uint16_t wLength = cpu_to_le16(cmd->length);
613
614 switch (cmd->requesttype & ~USB_DIR_IN) {
615 case 0:
616 switch (wValue & 0xff00) {
617 case 0x0100: /* device descriptor */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900618 len = min3(txlen, (int)sizeof(root_hub_dev_des), (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700619 memcpy(buffer, root_hub_dev_des, len);
620 break;
621 case 0x0200: /* configuration descriptor */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900622 len = min3(txlen, (int)sizeof(root_hub_config_des), (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700623 memcpy(buffer, root_hub_config_des, len);
624 break;
625 case 0x0300: /* string descriptors */
626 switch (wValue & 0xff) {
627 case 0x00:
Masahiro Yamadadb204642014-11-07 03:03:31 +0900628 len = min3(txlen, (int)sizeof(root_hub_str_index0),
629 (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700630 memcpy(buffer, root_hub_str_index0, len);
631 break;
632 case 0x01:
Masahiro Yamadadb204642014-11-07 03:03:31 +0900633 len = min3(txlen, (int)sizeof(root_hub_str_index1),
634 (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700635 memcpy(buffer, root_hub_str_index1, len);
636 break;
637 }
638 break;
639 default:
640 stat = USB_ST_STALLED;
641 }
642 break;
643
644 case USB_TYPE_CLASS:
645 /* Root port config, set 1 port and nothing else. */
646 dsc = 0x00000001;
647
648 data[0] = 9; /* min length; */
649 data[1] = 0x29;
650 data[2] = dsc & RH_A_NDP;
651 data[3] = 0;
652 if (dsc & RH_A_PSM)
653 data[3] |= 0x1;
654 if (dsc & RH_A_NOCP)
655 data[3] |= 0x10;
656 else if (dsc & RH_A_OCPM)
657 data[3] |= 0x8;
658
659 /* corresponds to data[4-7] */
660 data[5] = (dsc & RH_A_POTPGT) >> 24;
661 data[7] = dsc & RH_B_DR;
662 if (data[2] < 7) {
663 data[8] = 0xff;
664 } else {
665 data[0] += 2;
666 data[8] = (dsc & RH_B_DR) >> 8;
667 data[9] = 0xff;
668 data[10] = data[9];
669 }
670
Masahiro Yamadadb204642014-11-07 03:03:31 +0900671 len = min3(txlen, (int)data[0], (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700672 memcpy(buffer, data, len);
673 break;
674 default:
675 puts("unsupported root hub command\n");
676 stat = USB_ST_STALLED;
677 }
678
679 dev->act_len = min(len, txlen);
680 dev->status = stat;
681
682 return stat;
683}
684
685/* Direction: In ; Request: Configuration */
686static int dwc_otg_submit_rh_msg_in_configuration(struct usb_device *dev,
687 void *buffer, int txlen,
688 struct devrequest *cmd)
689{
690 int len = 0;
691 int stat = 0;
692
693 switch (cmd->requesttype & ~USB_DIR_IN) {
694 case 0:
695 *(uint8_t *)buffer = 0x01;
696 len = 1;
697 break;
698 default:
699 puts("unsupported root hub command\n");
700 stat = USB_ST_STALLED;
701 }
702
703 dev->act_len = min(len, txlen);
704 dev->status = stat;
705
706 return stat;
707}
708
709/* Direction: In */
Simon Glasse3c23a02015-07-07 20:53:36 -0600710static int dwc_otg_submit_rh_msg_in(struct dwc2_priv *priv,
711 struct usb_device *dev, void *buffer,
712 int txlen, struct devrequest *cmd)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700713{
714 switch (cmd->request) {
715 case USB_REQ_GET_STATUS:
Simon Glasse3c23a02015-07-07 20:53:36 -0600716 return dwc_otg_submit_rh_msg_in_status(priv->regs, dev, buffer,
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700717 txlen, cmd);
718 case USB_REQ_GET_DESCRIPTOR:
719 return dwc_otg_submit_rh_msg_in_descriptor(dev, buffer,
720 txlen, cmd);
721 case USB_REQ_GET_CONFIGURATION:
722 return dwc_otg_submit_rh_msg_in_configuration(dev, buffer,
723 txlen, cmd);
724 default:
725 puts("unsupported root hub command\n");
726 return USB_ST_STALLED;
727 }
728}
729
730/* Direction: Out */
Simon Glasse3c23a02015-07-07 20:53:36 -0600731static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
732 struct usb_device *dev,
733 void *buffer, int txlen,
734 struct devrequest *cmd)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700735{
Simon Glasse3c23a02015-07-07 20:53:36 -0600736 struct dwc2_core_regs *regs = priv->regs;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700737 int len = 0;
738 int stat = 0;
739 uint16_t bmrtype_breq = cmd->requesttype | (cmd->request << 8);
740 uint16_t wValue = cpu_to_le16(cmd->value);
741
742 switch (bmrtype_breq & ~USB_DIR_IN) {
743 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_ENDPOINT:
744 case (USB_REQ_CLEAR_FEATURE << 8) | USB_TYPE_CLASS:
745 break;
746
747 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
748 switch (wValue) {
749 case USB_PORT_FEAT_C_CONNECTION:
750 setbits_le32(&regs->hprt0, DWC2_HPRT0_PRTCONNDET);
751 break;
752 }
753 break;
754
755 case (USB_REQ_SET_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
756 switch (wValue) {
757 case USB_PORT_FEAT_SUSPEND:
758 break;
759
760 case USB_PORT_FEAT_RESET:
761 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
762 DWC2_HPRT0_PRTCONNDET |
763 DWC2_HPRT0_PRTENCHNG |
764 DWC2_HPRT0_PRTOVRCURRCHNG,
765 DWC2_HPRT0_PRTRST);
766 mdelay(50);
767 clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTRST);
768 break;
769
770 case USB_PORT_FEAT_POWER:
771 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
772 DWC2_HPRT0_PRTCONNDET |
773 DWC2_HPRT0_PRTENCHNG |
774 DWC2_HPRT0_PRTOVRCURRCHNG,
775 DWC2_HPRT0_PRTRST);
776 break;
777
778 case USB_PORT_FEAT_ENABLE:
779 break;
780 }
781 break;
782 case (USB_REQ_SET_ADDRESS << 8):
Simon Glasse3c23a02015-07-07 20:53:36 -0600783 priv->root_hub_devnum = wValue;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700784 break;
785 case (USB_REQ_SET_CONFIGURATION << 8):
786 break;
787 default:
788 puts("unsupported root hub command\n");
789 stat = USB_ST_STALLED;
790 }
791
792 len = min(len, txlen);
793
794 dev->act_len = len;
795 dev->status = stat;
796
797 return stat;
798}
799
Simon Glasse3c23a02015-07-07 20:53:36 -0600800static int dwc_otg_submit_rh_msg(struct dwc2_priv *priv, struct usb_device *dev,
801 unsigned long pipe, void *buffer, int txlen,
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700802 struct devrequest *cmd)
803{
804 int stat = 0;
805
806 if (usb_pipeint(pipe)) {
807 puts("Root-Hub submit IRQ: NOT implemented\n");
808 return 0;
809 }
810
811 if (cmd->requesttype & USB_DIR_IN)
Simon Glasse3c23a02015-07-07 20:53:36 -0600812 stat = dwc_otg_submit_rh_msg_in(priv, dev, buffer, txlen, cmd);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700813 else
Simon Glasse3c23a02015-07-07 20:53:36 -0600814 stat = dwc_otg_submit_rh_msg_out(priv, dev, buffer, txlen, cmd);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700815
816 mdelay(1);
817
818 return stat;
819}
820
Stefan Brüns081dcc72016-01-23 01:42:25 +0100821int wait_for_chhltd(struct dwc2_hc_regs *hc_regs, uint32_t *sub, u8 *toggle)
Stephen Warren8a346662015-03-07 22:48:51 -0700822{
Stephen Warren8a346662015-03-07 22:48:51 -0700823 int ret;
824 uint32_t hcint, hctsiz;
825
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100826 ret = wait_for_bit_le32(&hc_regs->hcint, DWC2_HCINT_CHHLTD, true,
Christophe Kerello4edc9802018-03-15 18:00:31 +0100827 2000, false);
Stephen Warren8a346662015-03-07 22:48:51 -0700828 if (ret)
829 return ret;
830
831 hcint = readl(&hc_regs->hcint);
Stephen Warren8a346662015-03-07 22:48:51 -0700832 hctsiz = readl(&hc_regs->hctsiz);
833 *sub = (hctsiz & DWC2_HCTSIZ_XFERSIZE_MASK) >>
834 DWC2_HCTSIZ_XFERSIZE_OFFSET;
Stephen Warren9f80e742015-03-07 22:48:55 -0700835 *toggle = (hctsiz & DWC2_HCTSIZ_PID_MASK) >> DWC2_HCTSIZ_PID_OFFSET;
Stephen Warren8a346662015-03-07 22:48:51 -0700836
Stefan Brünsaa9506e2016-01-17 04:09:52 +0100837 debug("%s: HCINT=%08x sub=%u toggle=%d\n", __func__, hcint, *sub,
838 *toggle);
Stephen Warren8a346662015-03-07 22:48:51 -0700839
Stefan Brünsaa9506e2016-01-17 04:09:52 +0100840 if (hcint & DWC2_HCINT_XFERCOMP)
841 return 0;
842
843 if (hcint & (DWC2_HCINT_NAK | DWC2_HCINT_FRMOVRUN))
844 return -EAGAIN;
Stephen Warren8a346662015-03-07 22:48:51 -0700845
Stefan Brünsaa9506e2016-01-17 04:09:52 +0100846 debug("%s: Error (HCINT=%08x)\n", __func__, hcint);
847 return -EINVAL;
Stephen Warren8a346662015-03-07 22:48:51 -0700848}
849
Stephen Warren972ad642015-03-07 22:48:52 -0700850static int dwc2_eptype[] = {
851 DWC2_HCCHAR_EPTYPE_ISOC,
852 DWC2_HCCHAR_EPTYPE_INTR,
853 DWC2_HCCHAR_EPTYPE_CONTROL,
854 DWC2_HCCHAR_EPTYPE_BULK,
855};
856
Stefan Brüns2385db32016-01-17 04:09:53 +0100857static int transfer_chunk(struct dwc2_hc_regs *hc_regs, void *aligned_buffer,
Stefan Brüns081dcc72016-01-23 01:42:25 +0100858 u8 *pid, int in, void *buffer, int num_packets,
Stefan Brüns247241e2016-01-17 04:09:56 +0100859 int xfer_len, int *actual_len, int odd_frame)
Stefan Brüns2385db32016-01-17 04:09:53 +0100860{
861 int ret = 0;
862 uint32_t sub;
863
864 debug("%s: chunk: pid %d xfer_len %u pkts %u\n", __func__,
865 *pid, xfer_len, num_packets);
866
867 writel((xfer_len << DWC2_HCTSIZ_XFERSIZE_OFFSET) |
868 (num_packets << DWC2_HCTSIZ_PKTCNT_OFFSET) |
869 (*pid << DWC2_HCTSIZ_PID_OFFSET),
870 &hc_regs->hctsiz);
871
Eddie Cai408bdee2017-04-06 11:37:04 +0800872 if (xfer_len) {
873 if (in) {
874 invalidate_dcache_range(
875 (uintptr_t)aligned_buffer,
876 (uintptr_t)aligned_buffer +
877 roundup(xfer_len, ARCH_DMA_MINALIGN));
878 } else {
879 memcpy(aligned_buffer, buffer, xfer_len);
880 flush_dcache_range(
881 (uintptr_t)aligned_buffer,
882 (uintptr_t)aligned_buffer +
883 roundup(xfer_len, ARCH_DMA_MINALIGN));
884 }
Stefan Brüns2385db32016-01-17 04:09:53 +0100885 }
886
887 writel(phys_to_bus((unsigned long)aligned_buffer), &hc_regs->hcdma);
888
889 /* Clear old interrupt conditions for this host channel. */
890 writel(0x3fff, &hc_regs->hcint);
891
892 /* Set host channel enable after all other setup is complete. */
893 clrsetbits_le32(&hc_regs->hcchar, DWC2_HCCHAR_MULTICNT_MASK |
Stefan Brüns247241e2016-01-17 04:09:56 +0100894 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS |
895 DWC2_HCCHAR_ODDFRM,
Stefan Brüns2385db32016-01-17 04:09:53 +0100896 (1 << DWC2_HCCHAR_MULTICNT_OFFSET) |
Stefan Brüns247241e2016-01-17 04:09:56 +0100897 (odd_frame << DWC2_HCCHAR_ODDFRM_OFFSET) |
Stefan Brüns2385db32016-01-17 04:09:53 +0100898 DWC2_HCCHAR_CHEN);
899
900 ret = wait_for_chhltd(hc_regs, &sub, pid);
901 if (ret < 0)
902 return ret;
903
904 if (in) {
905 xfer_len -= sub;
906
907 invalidate_dcache_range((unsigned long)aligned_buffer,
908 (unsigned long)aligned_buffer +
909 roundup(xfer_len, ARCH_DMA_MINALIGN));
910
911 memcpy(buffer, aligned_buffer, xfer_len);
912 }
913 *actual_len = xfer_len;
914
915 return ret;
916}
917
Simon Glasse3c23a02015-07-07 20:53:36 -0600918int chunk_msg(struct dwc2_priv *priv, struct usb_device *dev,
Stefan Brüns081dcc72016-01-23 01:42:25 +0100919 unsigned long pipe, u8 *pid, int in, void *buffer, int len)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700920{
Simon Glasse3c23a02015-07-07 20:53:36 -0600921 struct dwc2_core_regs *regs = priv->regs;
Stephen Warren972ad642015-03-07 22:48:52 -0700922 struct dwc2_hc_regs *hc_regs = &regs->hc_regs[DWC2_HC_CHANNEL];
Stefan Brüns247241e2016-01-17 04:09:56 +0100923 struct dwc2_host_regs *host_regs = &regs->host_regs;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700924 int devnum = usb_pipedevice(pipe);
925 int ep = usb_pipeendpoint(pipe);
926 int max = usb_maxpacket(dev, pipe);
Stephen Warren972ad642015-03-07 22:48:52 -0700927 int eptype = dwc2_eptype[usb_pipetype(pipe)];
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700928 int done = 0;
Stephen Warren766fe412015-04-11 21:52:02 -0600929 int ret = 0;
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100930 int do_split = 0;
931 int complete_split = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700932 uint32_t xfer_len;
933 uint32_t num_packets;
934 int stop_transfer = 0;
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100935 uint32_t max_xfer_len;
Stefan Brüns247241e2016-01-17 04:09:56 +0100936 int ssplit_frame_num = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700937
Stephen Warren972ad642015-03-07 22:48:52 -0700938 debug("%s: msg: pipe %lx pid %d in %d len %d\n", __func__, pipe, *pid,
939 in, len);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700940
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100941 max_xfer_len = CONFIG_DWC2_MAX_PACKET_COUNT * max;
942 if (max_xfer_len > CONFIG_DWC2_MAX_TRANSFER_SIZE)
943 max_xfer_len = CONFIG_DWC2_MAX_TRANSFER_SIZE;
944 if (max_xfer_len > DWC2_DATA_BUF_SIZE)
945 max_xfer_len = DWC2_DATA_BUF_SIZE;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700946
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100947 /* Make sure that max_xfer_len is a multiple of max packet size. */
948 num_packets = max_xfer_len / max;
949 max_xfer_len = num_packets * max;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700950
Stefan Brüns2385db32016-01-17 04:09:53 +0100951 /* Initialize channel */
952 dwc_otg_hc_init(regs, DWC2_HC_CHANNEL, dev, devnum, ep, in,
953 eptype, max);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700954
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100955 /* Check if the target is a FS/LS device behind a HS hub */
956 if (dev->speed != USB_SPEED_HIGH) {
957 uint8_t hub_addr;
958 uint8_t hub_port;
959 uint32_t hprt0 = readl(&regs->hprt0);
960 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
961 DWC2_HPRT0_PRTSPD_HIGH) {
962 usb_find_usb2_hub_address_port(dev, &hub_addr,
963 &hub_port);
964 dwc_otg_hc_init_split(hc_regs, hub_addr, hub_port);
965
966 do_split = 1;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700967 num_packets = 1;
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100968 max_xfer_len = max;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700969 }
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100970 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700971
Stefan Brüns2385db32016-01-17 04:09:53 +0100972 do {
973 int actual_len = 0;
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100974 uint32_t hcint;
Stefan Brüns247241e2016-01-17 04:09:56 +0100975 int odd_frame = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700976 xfer_len = len - done;
Stephen Warren972ad642015-03-07 22:48:52 -0700977
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100978 if (xfer_len > max_xfer_len)
979 xfer_len = max_xfer_len;
980 else if (xfer_len > max)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700981 num_packets = (xfer_len + max - 1) / max;
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100982 else
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700983 num_packets = 1;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700984
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100985 if (complete_split)
986 setbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
987 else if (do_split)
988 clrbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
Alexander Stein76fac502015-07-24 09:22:14 +0200989
Stefan Brüns247241e2016-01-17 04:09:56 +0100990 if (eptype == DWC2_HCCHAR_EPTYPE_INTR) {
991 int uframe_num = readl(&host_regs->hfnum);
992 if (!(uframe_num & 0x1))
993 odd_frame = 1;
Simon Glasse3c23a02015-07-07 20:53:36 -0600994 }
Stephen Warren7100da32015-03-08 11:08:13 -0600995
Stefan Brüns2385db32016-01-17 04:09:53 +0100996 ret = transfer_chunk(hc_regs, priv->aligned_buffer, pid,
997 in, (char *)buffer + done, num_packets,
Stefan Brüns247241e2016-01-17 04:09:56 +0100998 xfer_len, &actual_len, odd_frame);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700999
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001000 hcint = readl(&hc_regs->hcint);
1001 if (complete_split) {
1002 stop_transfer = 0;
Stefan Brüns247241e2016-01-17 04:09:56 +01001003 if (hcint & DWC2_HCINT_NYET) {
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001004 ret = 0;
Stefan Brüns247241e2016-01-17 04:09:56 +01001005 int frame_num = DWC2_HFNUM_MAX_FRNUM &
1006 readl(&host_regs->hfnum);
1007 if (((frame_num - ssplit_frame_num) &
1008 DWC2_HFNUM_MAX_FRNUM) > 4)
1009 ret = -EAGAIN;
1010 } else
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001011 complete_split = 0;
1012 } else if (do_split) {
1013 if (hcint & DWC2_HCINT_ACK) {
Stefan Brüns247241e2016-01-17 04:09:56 +01001014 ssplit_frame_num = DWC2_HFNUM_MAX_FRNUM &
1015 readl(&host_regs->hfnum);
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001016 ret = 0;
1017 complete_split = 1;
1018 }
1019 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001020
Stephen Warren766fe412015-04-11 21:52:02 -06001021 if (ret)
Stephen Warren8a346662015-03-07 22:48:51 -07001022 break;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001023
Stefan Brüns2385db32016-01-17 04:09:53 +01001024 if (actual_len < xfer_len)
1025 stop_transfer = 1;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001026
Stefan Brüns2385db32016-01-17 04:09:53 +01001027 done += actual_len;
Stephen Warren7100da32015-03-08 11:08:13 -06001028
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001029 /* Transactions are done when when either all data is transferred or
1030 * there is a short transfer. In case of a SPLIT make sure the CSPLIT
1031 * is executed.
1032 */
1033 } while (((done < len) && !stop_transfer) || complete_split);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001034
1035 writel(0, &hc_regs->hcintmsk);
1036 writel(0xFFFFFFFF, &hc_regs->hcint);
1037
1038 dev->status = 0;
1039 dev->act_len = done;
1040
Stephen Warren766fe412015-04-11 21:52:02 -06001041 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001042}
1043
Stephen Warren972ad642015-03-07 22:48:52 -07001044/* U-Boot USB transmission interface */
Simon Glasse3c23a02015-07-07 20:53:36 -06001045int _submit_bulk_msg(struct dwc2_priv *priv, struct usb_device *dev,
1046 unsigned long pipe, void *buffer, int len)
Stephen Warren972ad642015-03-07 22:48:52 -07001047{
1048 int devnum = usb_pipedevice(pipe);
1049 int ep = usb_pipeendpoint(pipe);
Stefan Brüns081dcc72016-01-23 01:42:25 +01001050 u8* pid;
Stephen Warren972ad642015-03-07 22:48:52 -07001051
Stefan Brüns081dcc72016-01-23 01:42:25 +01001052 if ((devnum >= MAX_DEVICE) || (devnum == priv->root_hub_devnum)) {
Stephen Warren972ad642015-03-07 22:48:52 -07001053 dev->status = 0;
1054 return -EINVAL;
1055 }
1056
Stefan Brüns081dcc72016-01-23 01:42:25 +01001057 if (usb_pipein(pipe))
1058 pid = &priv->in_data_toggle[devnum][ep];
1059 else
1060 pid = &priv->out_data_toggle[devnum][ep];
1061
1062 return chunk_msg(priv, dev, pipe, pid, usb_pipein(pipe), buffer, len);
Stephen Warren972ad642015-03-07 22:48:52 -07001063}
1064
Simon Glasse3c23a02015-07-07 20:53:36 -06001065static int _submit_control_msg(struct dwc2_priv *priv, struct usb_device *dev,
1066 unsigned long pipe, void *buffer, int len,
1067 struct devrequest *setup)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001068{
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001069 int devnum = usb_pipedevice(pipe);
Stefan Brüns081dcc72016-01-23 01:42:25 +01001070 int ret, act_len;
1071 u8 pid;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001072 /* For CONTROL endpoint pid should start with DATA1 */
1073 int status_direction;
1074
Simon Glasse3c23a02015-07-07 20:53:36 -06001075 if (devnum == priv->root_hub_devnum) {
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001076 dev->status = 0;
1077 dev->speed = USB_SPEED_HIGH;
Simon Glasse3c23a02015-07-07 20:53:36 -06001078 return dwc_otg_submit_rh_msg(priv, dev, pipe, buffer, len,
1079 setup);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001080 }
1081
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001082 /* SETUP stage */
Stephen Warren4db200e2015-03-07 22:48:53 -07001083 pid = DWC2_HC_PID_SETUP;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001084 do {
1085 ret = chunk_msg(priv, dev, pipe, &pid, 0, setup, 8);
1086 } while (ret == -EAGAIN);
Stephen Warren4db200e2015-03-07 22:48:53 -07001087 if (ret)
1088 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001089
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001090 /* DATA stage */
1091 act_len = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001092 if (buffer) {
Stephen Warrenb0ad4a32015-03-07 22:48:54 -07001093 pid = DWC2_HC_PID_DATA1;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001094 do {
1095 ret = chunk_msg(priv, dev, pipe, &pid, usb_pipein(pipe),
1096 buffer, len);
1097 act_len += dev->act_len;
1098 buffer += dev->act_len;
1099 len -= dev->act_len;
1100 } while (ret == -EAGAIN);
Stephen Warren4db200e2015-03-07 22:48:53 -07001101 if (ret)
1102 return ret;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001103 status_direction = usb_pipeout(pipe);
1104 } else {
1105 /* No-data CONTROL always ends with an IN transaction */
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001106 status_direction = 1;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001107 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001108
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001109 /* STATUS stage */
Stephen Warren4db200e2015-03-07 22:48:53 -07001110 pid = DWC2_HC_PID_DATA1;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001111 do {
1112 ret = chunk_msg(priv, dev, pipe, &pid, status_direction,
1113 priv->status_buffer, 0);
1114 } while (ret == -EAGAIN);
Stephen Warren4db200e2015-03-07 22:48:53 -07001115 if (ret)
1116 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001117
Stephen Warren4db200e2015-03-07 22:48:53 -07001118 dev->act_len = act_len;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001119
Stephen Warren8a346662015-03-07 22:48:51 -07001120 return 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001121}
1122
Simon Glasse3c23a02015-07-07 20:53:36 -06001123int _submit_int_msg(struct dwc2_priv *priv, struct usb_device *dev,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001124 unsigned long pipe, void *buffer, int len, int interval,
1125 bool nonblock)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001126{
Stephen Warren766fe412015-04-11 21:52:02 -06001127 unsigned long timeout;
1128 int ret;
1129
Stephen Warrendf7b37d2015-04-10 21:05:22 -06001130 /* FIXME: what is interval? */
Stephen Warren766fe412015-04-11 21:52:02 -06001131
1132 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1133 for (;;) {
1134 if (get_timer(0) > timeout) {
Sean Anderson62eff9b2020-09-15 10:45:15 -04001135#if CONFIG_IS_ENABLED(DM_USB)
1136 dev_err(dev->dev,
1137 "Timeout poll on interrupt endpoint\n");
1138#else
1139 log_err("Timeout poll on interrupt endpoint\n");
1140#endif
Stephen Warren766fe412015-04-11 21:52:02 -06001141 return -ETIMEDOUT;
1142 }
Simon Glasse3c23a02015-07-07 20:53:36 -06001143 ret = _submit_bulk_msg(priv, dev, pipe, buffer, len);
Michal Suchanekc7a7ae52019-08-18 10:55:28 +02001144 if ((ret != -EAGAIN) || nonblock)
Stephen Warren766fe412015-04-11 21:52:02 -06001145 return ret;
1146 }
Ley Foon Tan23865562018-08-29 00:08:48 +08001147}
1148
1149static int dwc2_reset(struct udevice *dev)
1150{
1151 int ret;
1152 struct dwc2_priv *priv = dev_get_priv(dev);
1153
1154 ret = reset_get_bulk(dev, &priv->resets);
1155 if (ret) {
1156 dev_warn(dev, "Can't get reset: %d\n", ret);
1157 /* Return 0 if error due to !CONFIG_DM_RESET and reset
1158 * DT property is not present.
1159 */
1160 if (ret == -ENOENT || ret == -ENOTSUPP)
1161 return 0;
1162 else
1163 return ret;
1164 }
1165
Patrick Delaunay8bef1692020-04-27 15:30:00 +02001166 /* force reset to clear all IP register */
1167 reset_assert_bulk(&priv->resets);
Ley Foon Tan23865562018-08-29 00:08:48 +08001168 ret = reset_deassert_bulk(&priv->resets);
1169 if (ret) {
1170 reset_release_bulk(&priv->resets);
1171 dev_err(dev, "Failed to reset: %d\n", ret);
1172 return ret;
1173 }
1174
1175 return 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001176}
1177
Kever Yang327c24d2017-03-10 12:05:14 +08001178static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001179{
Simon Glasse3c23a02015-07-07 20:53:36 -06001180 struct dwc2_core_regs *regs = priv->regs;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001181 uint32_t snpsid;
1182 int i, j;
Ley Foon Tan23865562018-08-29 00:08:48 +08001183 int ret;
1184
1185 ret = dwc2_reset(dev);
1186 if (ret)
1187 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001188
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001189 snpsid = readl(&regs->gsnpsid);
Patrice Chotarda259c1d2018-03-15 18:00:32 +01001190 dev_info(dev, "Core Release: %x.%03x\n",
1191 snpsid >> 12 & 0xf, snpsid & 0xfff);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001192
Peter Griffin79d657d2015-05-12 14:38:27 +01001193 if ((snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_2xx &&
1194 (snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_3xx) {
Patrice Chotarda259c1d2018-03-15 18:00:32 +01001195 dev_info(dev, "SNPSID invalid (not DWC2 OTG device): %08x\n",
1196 snpsid);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001197 return -ENODEV;
1198 }
1199
Marek Vasut39209492016-04-27 14:55:57 +02001200#ifdef CONFIG_DWC2_PHY_ULPI_EXT_VBUS
1201 priv->ext_vbus = 1;
1202#else
1203 priv->ext_vbus = 0;
1204#endif
1205
Sean Anderson62eff9b2020-09-15 10:45:15 -04001206 dwc_otg_core_init(dev);
Kever Yang327c24d2017-03-10 12:05:14 +08001207 dwc_otg_core_host_init(dev, regs);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001208
1209 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
1210 DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
1211 DWC2_HPRT0_PRTOVRCURRCHNG,
1212 DWC2_HPRT0_PRTRST);
1213 mdelay(50);
1214 clrbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET |
1215 DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG |
1216 DWC2_HPRT0_PRTRST);
1217
1218 for (i = 0; i < MAX_DEVICE; i++) {
Stefan Brüns081dcc72016-01-23 01:42:25 +01001219 for (j = 0; j < MAX_ENDPOINT; j++) {
1220 priv->in_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1221 priv->out_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1222 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001223 }
1224
Stefan Roesec526e832016-05-06 13:53:37 +02001225 /*
1226 * Add a 1 second delay here. This gives the host controller
1227 * a bit time before the comminucation with the USB devices
1228 * is started (the bus is scanned) and fixes the USB detection
1229 * problems with some problematic USB keys.
1230 */
1231 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
1232 mdelay(1000);
1233
Patrick Delaunayce17fe12020-04-27 15:30:01 +02001234 printf("USB DWC2\n");
1235
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001236 return 0;
1237}
1238
Simon Glasse3c23a02015-07-07 20:53:36 -06001239static void dwc2_uninit_common(struct dwc2_core_regs *regs)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001240{
1241 /* Put everything in reset. */
1242 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_PRTENA |
1243 DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
1244 DWC2_HPRT0_PRTOVRCURRCHNG,
1245 DWC2_HPRT0_PRTRST);
Simon Glasse3c23a02015-07-07 20:53:36 -06001246}
1247
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01001248#if !CONFIG_IS_ENABLED(DM_USB)
Simon Glasse3c23a02015-07-07 20:53:36 -06001249int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1250 int len, struct devrequest *setup)
1251{
1252 return _submit_control_msg(&local, dev, pipe, buffer, len, setup);
1253}
1254
1255int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1256 int len)
1257{
1258 return _submit_bulk_msg(&local, dev, pipe, buffer, len);
1259}
1260
1261int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001262 int len, int interval, bool nonblock)
Simon Glasse3c23a02015-07-07 20:53:36 -06001263{
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001264 return _submit_int_msg(&local, dev, pipe, buffer, len, interval,
1265 nonblock);
Simon Glasse3c23a02015-07-07 20:53:36 -06001266}
1267
1268/* U-Boot USB control interface */
1269int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1270{
1271 struct dwc2_priv *priv = &local;
1272
1273 memset(priv, '\0', sizeof(*priv));
1274 priv->root_hub_devnum = 0;
1275 priv->regs = (struct dwc2_core_regs *)CONFIG_USB_DWC2_REG_ADDR;
1276 priv->aligned_buffer = aligned_buffer_addr;
1277 priv->status_buffer = status_buffer_addr;
1278
1279 /* board-dependant init */
1280 if (board_usb_init(index, USB_INIT_HOST))
1281 return -1;
1282
Kever Yang327c24d2017-03-10 12:05:14 +08001283 return dwc2_init_common(NULL, priv);
Simon Glasse3c23a02015-07-07 20:53:36 -06001284}
1285
1286int usb_lowlevel_stop(int index)
1287{
1288 dwc2_uninit_common(local.regs);
1289
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001290 return 0;
1291}
Simon Glassa7ea72c2015-07-07 20:53:37 -06001292#endif
1293
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01001294#if CONFIG_IS_ENABLED(DM_USB)
Simon Glassa7ea72c2015-07-07 20:53:37 -06001295static int dwc2_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1296 unsigned long pipe, void *buffer, int length,
1297 struct devrequest *setup)
1298{
1299 struct dwc2_priv *priv = dev_get_priv(dev);
1300
1301 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1302 dev->name, udev, udev->dev->name, udev->portnr);
1303
1304 return _submit_control_msg(priv, udev, pipe, buffer, length, setup);
1305}
1306
1307static int dwc2_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1308 unsigned long pipe, void *buffer, int length)
1309{
1310 struct dwc2_priv *priv = dev_get_priv(dev);
1311
1312 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1313
1314 return _submit_bulk_msg(priv, udev, pipe, buffer, length);
1315}
1316
1317static int dwc2_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1318 unsigned long pipe, void *buffer, int length,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001319 int interval, bool nonblock)
Simon Glassa7ea72c2015-07-07 20:53:37 -06001320{
1321 struct dwc2_priv *priv = dev_get_priv(dev);
1322
1323 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1324
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001325 return _submit_int_msg(priv, udev, pipe, buffer, length, interval,
1326 nonblock);
Simon Glassa7ea72c2015-07-07 20:53:37 -06001327}
1328
Simon Glassaad29ae2020-12-03 16:55:21 -07001329static int dwc2_usb_of_to_plat(struct udevice *dev)
Simon Glassa7ea72c2015-07-07 20:53:37 -06001330{
1331 struct dwc2_priv *priv = dev_get_priv(dev);
Simon Glassa7ea72c2015-07-07 20:53:37 -06001332
Sean Anderson62eff9b2020-09-15 10:45:15 -04001333 priv->regs = dev_read_addr_ptr(dev);
1334 if (!priv->regs)
Simon Glassa7ea72c2015-07-07 20:53:37 -06001335 return -EINVAL;
Simon Glassa7ea72c2015-07-07 20:53:37 -06001336
Meng Dongyang697a8bc2017-06-28 19:22:43 +08001337 priv->oc_disable = dev_read_bool(dev, "disable-over-current");
1338 priv->hnp_srp_disable = dev_read_bool(dev, "hnp-srp-disable");
Meng Dongyangcc3fe062017-06-08 15:34:20 +08001339
Simon Glassa7ea72c2015-07-07 20:53:37 -06001340 return 0;
1341}
1342
Patrick Delaunaybb3569d2020-04-27 15:29:58 +02001343static int dwc2_setup_phy(struct udevice *dev)
1344{
1345 struct dwc2_priv *priv = dev_get_priv(dev);
1346 int ret;
1347
1348 ret = generic_phy_get_by_index(dev, 0, &priv->phy);
1349 if (ret) {
1350 if (ret == -ENOENT)
1351 return 0; /* no PHY, nothing to do */
1352 dev_err(dev, "Failed to get USB PHY: %d.\n", ret);
1353 return ret;
1354 }
1355
1356 ret = generic_phy_init(&priv->phy);
1357 if (ret) {
1358 dev_dbg(dev, "Failed to init USB PHY: %d.\n", ret);
1359 return ret;
1360 }
1361
1362 ret = generic_phy_power_on(&priv->phy);
1363 if (ret) {
1364 dev_dbg(dev, "Failed to power on USB PHY: %d.\n", ret);
1365 generic_phy_exit(&priv->phy);
1366 return ret;
1367 }
1368
1369 return 0;
1370}
1371
1372static int dwc2_shutdown_phy(struct udevice *dev)
1373{
1374 struct dwc2_priv *priv = dev_get_priv(dev);
1375 int ret;
1376
1377 /* PHY is not valid when generic_phy_get_by_index() = -ENOENT */
1378 if (!generic_phy_valid(&priv->phy))
1379 return 0; /* no PHY, nothing to do */
1380
1381 ret = generic_phy_power_off(&priv->phy);
1382 if (ret) {
1383 dev_dbg(dev, "Failed to power off USB PHY: %d.\n", ret);
1384 return ret;
1385 }
1386
1387 ret = generic_phy_exit(&priv->phy);
1388 if (ret) {
1389 dev_dbg(dev, "Failed to power off USB PHY: %d.\n", ret);
1390 return ret;
1391 }
1392
1393 return 0;
1394}
1395
Patrick Delaunay9225f3e2020-04-27 15:29:59 +02001396static int dwc2_clk_init(struct udevice *dev)
1397{
1398 struct dwc2_priv *priv = dev_get_priv(dev);
1399 int ret;
1400
1401 ret = clk_get_bulk(dev, &priv->clks);
1402 if (ret == -ENOSYS || ret == -ENOENT)
1403 return 0;
1404 if (ret)
1405 return ret;
1406
1407 ret = clk_enable_bulk(&priv->clks);
1408 if (ret) {
1409 clk_release_bulk(&priv->clks);
1410 return ret;
1411 }
1412
1413 return 0;
1414}
1415
Simon Glassa7ea72c2015-07-07 20:53:37 -06001416static int dwc2_usb_probe(struct udevice *dev)
1417{
1418 struct dwc2_priv *priv = dev_get_priv(dev);
Marek Vasut1ea9ac62016-04-26 03:02:35 +02001419 struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
Patrick Delaunaybb3569d2020-04-27 15:29:58 +02001420 int ret;
Marek Vasut1ea9ac62016-04-26 03:02:35 +02001421
1422 bus_priv->desc_before_addr = true;
Simon Glassa7ea72c2015-07-07 20:53:37 -06001423
Patrick Delaunay9225f3e2020-04-27 15:29:59 +02001424 ret = dwc2_clk_init(dev);
1425 if (ret)
1426 return ret;
1427
Patrick Delaunaybb3569d2020-04-27 15:29:58 +02001428 ret = dwc2_setup_phy(dev);
1429 if (ret)
1430 return ret;
1431
Kever Yang327c24d2017-03-10 12:05:14 +08001432 return dwc2_init_common(dev, priv);
Simon Glassa7ea72c2015-07-07 20:53:37 -06001433}
1434
1435static int dwc2_usb_remove(struct udevice *dev)
1436{
1437 struct dwc2_priv *priv = dev_get_priv(dev);
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +01001438 int ret;
1439
1440 ret = dwc_vbus_supply_exit(dev);
1441 if (ret)
1442 return ret;
Simon Glassa7ea72c2015-07-07 20:53:37 -06001443
Patrick Delaunaybb3569d2020-04-27 15:29:58 +02001444 ret = dwc2_shutdown_phy(dev);
1445 if (ret) {
1446 dev_dbg(dev, "Failed to shutdown USB PHY: %d.\n", ret);
1447 return ret;
1448 }
1449
Simon Glassa7ea72c2015-07-07 20:53:37 -06001450 dwc2_uninit_common(priv->regs);
1451
Ley Foon Tan23865562018-08-29 00:08:48 +08001452 reset_release_bulk(&priv->resets);
Patrick Delaunay9225f3e2020-04-27 15:29:59 +02001453 clk_disable_bulk(&priv->clks);
1454 clk_release_bulk(&priv->clks);
Ley Foon Tan23865562018-08-29 00:08:48 +08001455
Simon Glassa7ea72c2015-07-07 20:53:37 -06001456 return 0;
1457}
1458
1459struct dm_usb_ops dwc2_usb_ops = {
1460 .control = dwc2_submit_control_msg,
1461 .bulk = dwc2_submit_bulk_msg,
1462 .interrupt = dwc2_submit_int_msg,
1463};
1464
1465static const struct udevice_id dwc2_usb_ids[] = {
1466 { .compatible = "brcm,bcm2835-usb" },
Emmanuel Vadot80447002018-07-02 14:34:23 +02001467 { .compatible = "brcm,bcm2708-usb" },
Marek Vasutac4a35f2015-08-12 22:19:14 +02001468 { .compatible = "snps,dwc2" },
Simon Glassa7ea72c2015-07-07 20:53:37 -06001469 { }
1470};
1471
1472U_BOOT_DRIVER(usb_dwc2) = {
Marek Vasutaf83c782015-08-12 22:19:15 +02001473 .name = "dwc2_usb",
Simon Glassa7ea72c2015-07-07 20:53:37 -06001474 .id = UCLASS_USB,
1475 .of_match = dwc2_usb_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001476 .of_to_plat = dwc2_usb_of_to_plat,
Simon Glassa7ea72c2015-07-07 20:53:37 -06001477 .probe = dwc2_usb_probe,
1478 .remove = dwc2_usb_remove,
1479 .ops = &dwc2_usb_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001480 .priv_auto = sizeof(struct dwc2_priv),
Simon Glassa7ea72c2015-07-07 20:53:37 -06001481 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1482};
1483#endif