blob: 9818f9be94e010398e5a3b5986b127e3ebf3972c [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>
João Loureiroec76d072021-03-17 16:52:21 +010024#include <linux/usb/otg.h>
Kever Yang327c24d2017-03-10 12:05:14 +080025#include <power/regulator.h>
Ley Foon Tan23865562018-08-29 00:08:48 +080026#include <reset.h>
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070027
28#include "dwc2.h"
29
30/* Use only HC channel 0. */
31#define DWC2_HC_CHANNEL 0
32
33#define DWC2_STATUS_BUF_SIZE 64
Alexey Brodkinf19414b2018-02-28 16:16:58 +030034#define DWC2_DATA_BUF_SIZE (CONFIG_USB_DWC2_BUFFER_SIZE * 1024)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070035
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070036#define MAX_DEVICE 16
37#define MAX_ENDPOINT 16
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070038
Simon Glasse3c23a02015-07-07 20:53:36 -060039struct dwc2_priv {
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +010040#if CONFIG_IS_ENABLED(DM_USB)
Alexander Stein76fac502015-07-24 09:22:14 +020041 uint8_t aligned_buffer[DWC2_DATA_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
42 uint8_t status_buffer[DWC2_STATUS_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +010043#ifdef CONFIG_DM_REGULATOR
44 struct udevice *vbus_supply;
45#endif
Patrick Delaunaybb3569d2020-04-27 15:29:58 +020046 struct phy phy;
Patrick Delaunay9225f3e2020-04-27 15:29:59 +020047 struct clk_bulk clks;
Simon Glassa7ea72c2015-07-07 20:53:37 -060048#else
Simon Glasse3c23a02015-07-07 20:53:36 -060049 uint8_t *aligned_buffer;
50 uint8_t *status_buffer;
Simon Glassa7ea72c2015-07-07 20:53:37 -060051#endif
Stefan Brüns081dcc72016-01-23 01:42:25 +010052 u8 in_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
53 u8 out_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
Simon Glasse3c23a02015-07-07 20:53:36 -060054 struct dwc2_core_regs *regs;
55 int root_hub_devnum;
Marek Vasut39209492016-04-27 14:55:57 +020056 bool ext_vbus;
Meng Dongyang697a8bc2017-06-28 19:22:43 +080057 /*
58 * The hnp/srp capability must be disabled if the platform
59 * does't support hnp/srp. Otherwise the force mode can't work.
60 */
Meng Dongyangcc3fe062017-06-08 15:34:20 +080061 bool hnp_srp_disable;
Marek Vasut43db5a62016-04-27 14:58:49 +020062 bool oc_disable;
Ley Foon Tan23865562018-08-29 00:08:48 +080063
64 struct reset_ctl_bulk resets;
Simon Glasse3c23a02015-07-07 20:53:36 -060065};
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070066
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +010067#if !CONFIG_IS_ENABLED(DM_USB)
Alexander Stein76fac502015-07-24 09:22:14 +020068/* We need cacheline-aligned buffers for DMA transfers and dcache support */
69DEFINE_ALIGN_BUFFER(uint8_t, aligned_buffer_addr, DWC2_DATA_BUF_SIZE,
70 ARCH_DMA_MINALIGN);
71DEFINE_ALIGN_BUFFER(uint8_t, status_buffer_addr, DWC2_STATUS_BUF_SIZE,
72 ARCH_DMA_MINALIGN);
Simon Glasse3c23a02015-07-07 20:53:36 -060073
74static struct dwc2_priv local;
Simon Glassa7ea72c2015-07-07 20:53:37 -060075#endif
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070076
77/*
78 * DWC2 IP interface
79 */
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070080
81/*
82 * Initializes the FSLSPClkSel field of the HCFG register
83 * depending on the PHY type.
84 */
85static void init_fslspclksel(struct dwc2_core_regs *regs)
86{
87 uint32_t phyclk;
88
Tom Rinia99817c2021-08-10 16:17:55 -040089#if (DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070090 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
91#else
92 /* High speed PHY running at full speed or high speed */
93 phyclk = DWC2_HCFG_FSLSPCLKSEL_30_60_MHZ;
94#endif
95
Tom Rinia99817c2021-08-10 16:17:55 -040096#ifdef DWC2_ULPI_FS_LS
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070097 uint32_t hwcfg2 = readl(&regs->ghwcfg2);
98 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
99 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
100 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
101 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
102
103 if (hval == 2 && fval == 1)
104 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
105#endif
106
107 clrsetbits_le32(&regs->host_regs.hcfg,
108 DWC2_HCFG_FSLSPCLKSEL_MASK,
109 phyclk << DWC2_HCFG_FSLSPCLKSEL_OFFSET);
110}
111
112/*
113 * Flush a Tx FIFO.
114 *
115 * @param regs Programming view of DWC_otg controller.
116 * @param num Tx FIFO to flush.
117 */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400118static void dwc_otg_flush_tx_fifo(struct udevice *dev,
119 struct dwc2_core_regs *regs, const int num)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700120{
121 int ret;
122
123 writel(DWC2_GRSTCTL_TXFFLSH | (num << DWC2_GRSTCTL_TXFNUM_OFFSET),
124 &regs->grstctl);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100125 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_TXFFLSH,
126 false, 1000, false);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700127 if (ret)
Patrice Chotarda259c1d2018-03-15 18:00:32 +0100128 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700129
130 /* Wait for 3 PHY Clocks */
131 udelay(1);
132}
133
134/*
135 * Flush Rx FIFO.
136 *
137 * @param regs Programming view of DWC_otg controller.
138 */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400139static void dwc_otg_flush_rx_fifo(struct udevice *dev,
140 struct dwc2_core_regs *regs)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700141{
142 int ret;
143
144 writel(DWC2_GRSTCTL_RXFFLSH, &regs->grstctl);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100145 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_RXFFLSH,
146 false, 1000, false);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700147 if (ret)
Patrice Chotarda259c1d2018-03-15 18:00:32 +0100148 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700149
150 /* Wait for 3 PHY Clocks */
151 udelay(1);
152}
153
154/*
155 * Do core a soft reset of the core. Be careful with this because it
156 * resets all the internal state machines of the core.
157 */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400158static void dwc_otg_core_reset(struct udevice *dev,
159 struct dwc2_core_regs *regs)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700160{
161 int ret;
162
163 /* Wait for AHB master IDLE state. */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100164 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_AHBIDLE,
165 true, 1000, false);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700166 if (ret)
Patrice Chotarda259c1d2018-03-15 18:00:32 +0100167 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700168
169 /* Core Soft Reset */
170 writel(DWC2_GRSTCTL_CSFTRST, &regs->grstctl);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100171 ret = wait_for_bit_le32(&regs->grstctl, DWC2_GRSTCTL_CSFTRST,
172 false, 1000, false);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700173 if (ret)
Patrice Chotarda259c1d2018-03-15 18:00:32 +0100174 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700175
176 /*
177 * Wait for core to come out of reset.
178 * NOTE: This long sleep is _very_ important, otherwise the core will
179 * not stay in host mode after a connector ID change!
180 */
181 mdelay(100);
182}
183
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100184#if CONFIG_IS_ENABLED(DM_USB) && defined(CONFIG_DM_REGULATOR)
Kever Yang327c24d2017-03-10 12:05:14 +0800185static int dwc_vbus_supply_init(struct udevice *dev)
186{
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100187 struct dwc2_priv *priv = dev_get_priv(dev);
Kever Yang327c24d2017-03-10 12:05:14 +0800188 int ret;
189
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100190 ret = device_get_supply_regulator(dev, "vbus-supply",
191 &priv->vbus_supply);
Kever Yang327c24d2017-03-10 12:05:14 +0800192 if (ret) {
193 debug("%s: No vbus supply\n", dev->name);
194 return 0;
195 }
196
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100197 ret = regulator_set_enable(priv->vbus_supply, true);
Kever Yang327c24d2017-03-10 12:05:14 +0800198 if (ret) {
Patrice Chotarda259c1d2018-03-15 18:00:32 +0100199 dev_err(dev, "Error enabling vbus supply\n");
Kever Yang327c24d2017-03-10 12:05:14 +0800200 return ret;
201 }
202
203 return 0;
204}
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100205
206static int dwc_vbus_supply_exit(struct udevice *dev)
207{
208 struct dwc2_priv *priv = dev_get_priv(dev);
209 int ret;
210
211 if (priv->vbus_supply) {
212 ret = regulator_set_enable(priv->vbus_supply, false);
213 if (ret) {
214 dev_err(dev, "Error disabling vbus supply\n");
215 return ret;
216 }
217 }
218
219 return 0;
220}
Kever Yang327c24d2017-03-10 12:05:14 +0800221#else
222static int dwc_vbus_supply_init(struct udevice *dev)
223{
224 return 0;
225}
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100226
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100227#if CONFIG_IS_ENABLED(DM_USB)
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100228static int dwc_vbus_supply_exit(struct udevice *dev)
229{
230 return 0;
231}
Kever Yang327c24d2017-03-10 12:05:14 +0800232#endif
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100233#endif
Kever Yang327c24d2017-03-10 12:05:14 +0800234
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700235/*
236 * This function initializes the DWC_otg controller registers for
237 * host mode.
238 *
239 * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
240 * request queues. Host channels are reset to ensure that they are ready for
241 * performing transfers.
242 *
Kever Yang327c24d2017-03-10 12:05:14 +0800243 * @param dev USB Device (NULL if driver model is not being used)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700244 * @param regs Programming view of DWC_otg controller
245 *
246 */
Kever Yang327c24d2017-03-10 12:05:14 +0800247static void dwc_otg_core_host_init(struct udevice *dev,
248 struct dwc2_core_regs *regs)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700249{
250 uint32_t nptxfifosize = 0;
251 uint32_t ptxfifosize = 0;
252 uint32_t hprt0 = 0;
253 int i, ret, num_channels;
254
255 /* Restart the Phy Clock */
256 writel(0, &regs->pcgcctl);
257
258 /* Initialize Host Configuration Register */
259 init_fslspclksel(regs);
Tom Rinia99817c2021-08-10 16:17:55 -0400260#ifdef DWC2_DFLT_SPEED_FULL
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700261 setbits_le32(&regs->host_regs.hcfg, DWC2_HCFG_FSLSSUPP);
262#endif
263
264 /* Configure data FIFO sizes */
Tom Rinia99817c2021-08-10 16:17:55 -0400265#ifdef DWC2_ENABLE_DYNAMIC_FIFO
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700266 if (readl(&regs->ghwcfg2) & DWC2_HWCFG2_DYNAMIC_FIFO) {
267 /* Rx FIFO */
Tom Rinia99817c2021-08-10 16:17:55 -0400268 writel(DWC2_HOST_RX_FIFO_SIZE, &regs->grxfsiz);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700269
270 /* Non-periodic Tx FIFO */
Tom Rinia99817c2021-08-10 16:17:55 -0400271 nptxfifosize |= DWC2_HOST_NPERIO_TX_FIFO_SIZE <<
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700272 DWC2_FIFOSIZE_DEPTH_OFFSET;
Tom Rinia99817c2021-08-10 16:17:55 -0400273 nptxfifosize |= DWC2_HOST_RX_FIFO_SIZE <<
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700274 DWC2_FIFOSIZE_STARTADDR_OFFSET;
275 writel(nptxfifosize, &regs->gnptxfsiz);
276
277 /* Periodic Tx FIFO */
Tom Rinia99817c2021-08-10 16:17:55 -0400278 ptxfifosize |= DWC2_HOST_PERIO_TX_FIFO_SIZE <<
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700279 DWC2_FIFOSIZE_DEPTH_OFFSET;
Tom Rinia99817c2021-08-10 16:17:55 -0400280 ptxfifosize |= (DWC2_HOST_RX_FIFO_SIZE +
281 DWC2_HOST_NPERIO_TX_FIFO_SIZE) <<
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700282 DWC2_FIFOSIZE_STARTADDR_OFFSET;
283 writel(ptxfifosize, &regs->hptxfsiz);
284 }
285#endif
286
287 /* Clear Host Set HNP Enable in the OTG Control Register */
288 clrbits_le32(&regs->gotgctl, DWC2_GOTGCTL_HSTSETHNPEN);
289
290 /* Make sure the FIFOs are flushed. */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400291 dwc_otg_flush_tx_fifo(dev, regs, 0x10); /* All Tx FIFOs */
292 dwc_otg_flush_rx_fifo(dev, regs);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700293
294 /* Flush out any leftover queued requests. */
295 num_channels = readl(&regs->ghwcfg2);
296 num_channels &= DWC2_HWCFG2_NUM_HOST_CHAN_MASK;
297 num_channels >>= DWC2_HWCFG2_NUM_HOST_CHAN_OFFSET;
298 num_channels += 1;
299
300 for (i = 0; i < num_channels; i++)
301 clrsetbits_le32(&regs->hc_regs[i].hcchar,
302 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_EPDIR,
303 DWC2_HCCHAR_CHDIS);
304
305 /* Halt all channels to put them into a known state. */
306 for (i = 0; i < num_channels; i++) {
307 clrsetbits_le32(&regs->hc_regs[i].hcchar,
308 DWC2_HCCHAR_EPDIR,
309 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100310 ret = wait_for_bit_le32(&regs->hc_regs[i].hcchar,
311 DWC2_HCCHAR_CHEN, false, 1000, false);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700312 if (ret)
Sean Anderson62eff9b2020-09-15 10:45:15 -0400313 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700314 }
315
316 /* Turn on the vbus power. */
317 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST) {
Teik Heng Chong9715f3c2023-06-21 11:13:58 +0800318 hprt0 = readl(&regs->hprt0) & ~DWC2_HPRT0_W1C_MASK;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700319 if (!(hprt0 & DWC2_HPRT0_PRTPWR)) {
320 hprt0 |= DWC2_HPRT0_PRTPWR;
321 writel(hprt0, &regs->hprt0);
322 }
323 }
Kever Yang327c24d2017-03-10 12:05:14 +0800324
325 if (dev)
326 dwc_vbus_supply_init(dev);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700327}
328
329/*
330 * This function initializes the DWC_otg controller registers and
331 * prepares the core for device mode or host mode operation.
332 *
333 * @param regs Programming view of the DWC_otg controller
334 */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400335static void dwc_otg_core_init(struct udevice *dev)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700336{
Sean Anderson62eff9b2020-09-15 10:45:15 -0400337 struct dwc2_priv *priv = dev_get_priv(dev);
Marek Vasut36fc5692016-04-27 14:53:33 +0200338 struct dwc2_core_regs *regs = priv->regs;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700339 uint32_t ahbcfg = 0;
340 uint32_t usbcfg = 0;
Tom Rinia99817c2021-08-10 16:17:55 -0400341 uint8_t brst_sz = DWC2_DMA_BURST_SIZE;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700342
343 /* Common Initialization */
344 usbcfg = readl(&regs->gusbcfg);
345
346 /* Program the ULPI External VBUS bit if needed */
Marek Vasut39209492016-04-27 14:55:57 +0200347 if (priv->ext_vbus) {
Marek Vasut43db5a62016-04-27 14:58:49 +0200348 usbcfg |= DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
349 if (!priv->oc_disable) {
350 usbcfg |= DWC2_GUSBCFG_ULPI_INT_VBUS_INDICATOR |
351 DWC2_GUSBCFG_INDICATOR_PASSTHROUGH;
352 }
Marek Vasut39209492016-04-27 14:55:57 +0200353 } else {
354 usbcfg &= ~DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
355 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700356
357 /* Set external TS Dline pulsing */
Tom Rinia99817c2021-08-10 16:17:55 -0400358#ifdef DWC2_TS_DLINE
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700359 usbcfg |= DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
360#else
361 usbcfg &= ~DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
362#endif
363 writel(usbcfg, &regs->gusbcfg);
364
365 /* Reset the Controller */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400366 dwc_otg_core_reset(dev, regs);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700367
368 /*
369 * This programming sequence needs to happen in FS mode before
370 * any other programming occurs
371 */
Tom Rinia99817c2021-08-10 16:17:55 -0400372#if defined(DWC2_DFLT_SPEED_FULL) && \
373 (DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700374 /* If FS mode with FS PHY */
375 setbits_le32(&regs->gusbcfg, DWC2_GUSBCFG_PHYSEL);
376
377 /* Reset after a PHY select */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400378 dwc_otg_core_reset(dev, regs);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700379
380 /*
381 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS.
382 * Also do this on HNP Dev/Host mode switches (done in dev_init
383 * and host_init).
384 */
385 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
386 init_fslspclksel(regs);
387
Tom Rinia99817c2021-08-10 16:17:55 -0400388#ifdef DWC2_I2C_ENABLE
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700389 /* Program GUSBCFG.OtgUtmifsSel to I2C */
390 setbits_le32(&regs->gusbcfg, DWC2_GUSBCFG_OTGUTMIFSSEL);
391
392 /* Program GI2CCTL.I2CEn */
393 clrsetbits_le32(&regs->gi2cctl, DWC2_GI2CCTL_I2CEN |
394 DWC2_GI2CCTL_I2CDEVADDR_MASK,
395 1 << DWC2_GI2CCTL_I2CDEVADDR_OFFSET);
396 setbits_le32(&regs->gi2cctl, DWC2_GI2CCTL_I2CEN);
397#endif
398
399#else
400 /* High speed PHY. */
401
402 /*
403 * HS PHY parameters. These parameters are preserved during
404 * soft reset so only program the first time. Do a soft reset
405 * immediately after setting phyif.
406 */
407 usbcfg &= ~(DWC2_GUSBCFG_ULPI_UTMI_SEL | DWC2_GUSBCFG_PHYIF);
Tom Rinia99817c2021-08-10 16:17:55 -0400408 usbcfg |= DWC2_PHY_TYPE << DWC2_GUSBCFG_ULPI_UTMI_SEL_OFFSET;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700409
410 if (usbcfg & DWC2_GUSBCFG_ULPI_UTMI_SEL) { /* ULPI interface */
Tom Rinia99817c2021-08-10 16:17:55 -0400411#ifdef DWC2_PHY_ULPI_DDR
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700412 usbcfg |= DWC2_GUSBCFG_DDRSEL;
413#else
414 usbcfg &= ~DWC2_GUSBCFG_DDRSEL;
415#endif
416 } else { /* UTMI+ interface */
Tom Rinia99817c2021-08-10 16:17:55 -0400417#if (DWC2_UTMI_WIDTH == 16)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700418 usbcfg |= DWC2_GUSBCFG_PHYIF;
419#endif
420 }
421
422 writel(usbcfg, &regs->gusbcfg);
423
424 /* Reset after setting the PHY parameters */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400425 dwc_otg_core_reset(dev, regs);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700426#endif
427
428 usbcfg = readl(&regs->gusbcfg);
429 usbcfg &= ~(DWC2_GUSBCFG_ULPI_FSLS | DWC2_GUSBCFG_ULPI_CLK_SUS_M);
Tom Rinia99817c2021-08-10 16:17:55 -0400430#ifdef DWC2_ULPI_FS_LS
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700431 uint32_t hwcfg2 = readl(&regs->ghwcfg2);
432 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
433 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
434 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
435 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
436 if (hval == 2 && fval == 1) {
437 usbcfg |= DWC2_GUSBCFG_ULPI_FSLS;
438 usbcfg |= DWC2_GUSBCFG_ULPI_CLK_SUS_M;
439 }
440#endif
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800441 if (priv->hnp_srp_disable)
442 usbcfg |= DWC2_GUSBCFG_FORCEHOSTMODE;
443
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700444 writel(usbcfg, &regs->gusbcfg);
445
446 /* Program the GAHBCFG Register. */
447 switch (readl(&regs->ghwcfg2) & DWC2_HWCFG2_ARCHITECTURE_MASK) {
448 case DWC2_HWCFG2_ARCHITECTURE_SLAVE_ONLY:
449 break;
450 case DWC2_HWCFG2_ARCHITECTURE_EXT_DMA:
451 while (brst_sz > 1) {
452 ahbcfg |= ahbcfg + (1 << DWC2_GAHBCFG_HBURSTLEN_OFFSET);
453 ahbcfg &= DWC2_GAHBCFG_HBURSTLEN_MASK;
454 brst_sz >>= 1;
455 }
456
Tom Rinia99817c2021-08-10 16:17:55 -0400457#ifdef DWC2_DMA_ENABLE
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700458 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
459#endif
460 break;
461
462 case DWC2_HWCFG2_ARCHITECTURE_INT_DMA:
463 ahbcfg |= DWC2_GAHBCFG_HBURSTLEN_INCR4;
Tom Rinia99817c2021-08-10 16:17:55 -0400464#ifdef DWC2_DMA_ENABLE
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700465 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
466#endif
467 break;
468 }
469
470 writel(ahbcfg, &regs->gahbcfg);
471
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800472 /* Program the capabilities in GUSBCFG Register */
473 usbcfg = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700474
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800475 if (!priv->hnp_srp_disable)
476 usbcfg |= DWC2_GUSBCFG_HNPCAP | DWC2_GUSBCFG_SRPCAP;
Tom Rinia99817c2021-08-10 16:17:55 -0400477#ifdef DWC2_IC_USB_CAP
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800478 usbcfg |= DWC2_GUSBCFG_IC_USB_CAP;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700479#endif
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800480
481 setbits_le32(&regs->gusbcfg, usbcfg);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700482}
483
484/*
485 * Prepares a host channel for transferring packets to/from a specific
486 * endpoint. The HCCHARn register is set up with the characteristics specified
487 * in _hc. Host channel interrupts that may need to be serviced while this
488 * transfer is in progress are enabled.
489 *
490 * @param regs Programming view of DWC_otg controller
491 * @param hc Information needed to initialize the host channel
492 */
493static void dwc_otg_hc_init(struct dwc2_core_regs *regs, uint8_t hc_num,
Stephen Warrendead8db2015-04-10 21:05:21 -0600494 struct usb_device *dev, uint8_t dev_addr, uint8_t ep_num,
495 uint8_t ep_is_in, uint8_t ep_type, uint16_t max_packet)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700496{
497 struct dwc2_hc_regs *hc_regs = &regs->hc_regs[hc_num];
Stephen Warrendead8db2015-04-10 21:05:21 -0600498 uint32_t hcchar = (dev_addr << DWC2_HCCHAR_DEVADDR_OFFSET) |
499 (ep_num << DWC2_HCCHAR_EPNUM_OFFSET) |
500 (ep_is_in << DWC2_HCCHAR_EPDIR_OFFSET) |
501 (ep_type << DWC2_HCCHAR_EPTYPE_OFFSET) |
502 (max_packet << DWC2_HCCHAR_MPS_OFFSET);
503
504 if (dev->speed == USB_SPEED_LOW)
505 hcchar |= DWC2_HCCHAR_LSPDDEV;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700506
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700507 /*
508 * Program the HCCHARn register with the endpoint characteristics
509 * for the current transfer.
510 */
511 writel(hcchar, &hc_regs->hcchar);
512
Stefan Brüns2e194e22016-01-17 04:09:54 +0100513 /* Program the HCSPLIT register, default to no SPLIT */
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700514 writel(0, &hc_regs->hcsplt);
515}
516
Stefan Brüns2e194e22016-01-17 04:09:54 +0100517static void dwc_otg_hc_init_split(struct dwc2_hc_regs *hc_regs,
518 uint8_t hub_devnum, uint8_t hub_port)
519{
520 uint32_t hcsplt = 0;
521
522 hcsplt = DWC2_HCSPLT_SPLTENA;
523 hcsplt |= hub_devnum << DWC2_HCSPLT_HUBADDR_OFFSET;
524 hcsplt |= hub_port << DWC2_HCSPLT_PRTADDR_OFFSET;
525
526 /* Program the HCSPLIT register for SPLITs */
527 writel(hcsplt, &hc_regs->hcsplt);
528}
529
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700530/*
531 * DWC2 to USB API interface
532 */
533/* Direction: In ; Request: Status */
Simon Glasse3c23a02015-07-07 20:53:36 -0600534static int dwc_otg_submit_rh_msg_in_status(struct dwc2_core_regs *regs,
535 struct usb_device *dev, void *buffer,
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700536 int txlen, struct devrequest *cmd)
537{
538 uint32_t hprt0 = 0;
539 uint32_t port_status = 0;
540 uint32_t port_change = 0;
541 int len = 0;
542 int stat = 0;
543
544 switch (cmd->requesttype & ~USB_DIR_IN) {
545 case 0:
546 *(uint16_t *)buffer = cpu_to_le16(1);
547 len = 2;
548 break;
549 case USB_RECIP_INTERFACE:
550 case USB_RECIP_ENDPOINT:
551 *(uint16_t *)buffer = cpu_to_le16(0);
552 len = 2;
553 break;
554 case USB_TYPE_CLASS:
555 *(uint32_t *)buffer = cpu_to_le32(0);
556 len = 4;
557 break;
558 case USB_RECIP_OTHER | USB_TYPE_CLASS:
559 hprt0 = readl(&regs->hprt0);
560 if (hprt0 & DWC2_HPRT0_PRTCONNSTS)
561 port_status |= USB_PORT_STAT_CONNECTION;
562 if (hprt0 & DWC2_HPRT0_PRTENA)
563 port_status |= USB_PORT_STAT_ENABLE;
564 if (hprt0 & DWC2_HPRT0_PRTSUSP)
565 port_status |= USB_PORT_STAT_SUSPEND;
566 if (hprt0 & DWC2_HPRT0_PRTOVRCURRACT)
567 port_status |= USB_PORT_STAT_OVERCURRENT;
568 if (hprt0 & DWC2_HPRT0_PRTRST)
569 port_status |= USB_PORT_STAT_RESET;
570 if (hprt0 & DWC2_HPRT0_PRTPWR)
571 port_status |= USB_PORT_STAT_POWER;
572
Stephen Warrend3388f82015-03-27 21:55:38 -0600573 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) == DWC2_HPRT0_PRTSPD_LOW)
574 port_status |= USB_PORT_STAT_LOW_SPEED;
575 else if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
576 DWC2_HPRT0_PRTSPD_HIGH)
577 port_status |= USB_PORT_STAT_HIGH_SPEED;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700578
579 if (hprt0 & DWC2_HPRT0_PRTENCHNG)
580 port_change |= USB_PORT_STAT_C_ENABLE;
581 if (hprt0 & DWC2_HPRT0_PRTCONNDET)
582 port_change |= USB_PORT_STAT_C_CONNECTION;
583 if (hprt0 & DWC2_HPRT0_PRTOVRCURRCHNG)
584 port_change |= USB_PORT_STAT_C_OVERCURRENT;
585
586 *(uint32_t *)buffer = cpu_to_le32(port_status |
587 (port_change << 16));
588 len = 4;
589 break;
590 default:
591 puts("unsupported root hub command\n");
592 stat = USB_ST_STALLED;
593 }
594
595 dev->act_len = min(len, txlen);
596 dev->status = stat;
597
598 return stat;
599}
600
601/* Direction: In ; Request: Descriptor */
602static int dwc_otg_submit_rh_msg_in_descriptor(struct usb_device *dev,
603 void *buffer, int txlen,
604 struct devrequest *cmd)
605{
606 unsigned char data[32];
607 uint32_t dsc;
608 int len = 0;
609 int stat = 0;
610 uint16_t wValue = cpu_to_le16(cmd->value);
611 uint16_t wLength = cpu_to_le16(cmd->length);
612
613 switch (cmd->requesttype & ~USB_DIR_IN) {
614 case 0:
615 switch (wValue & 0xff00) {
616 case 0x0100: /* device descriptor */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900617 len = min3(txlen, (int)sizeof(root_hub_dev_des), (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700618 memcpy(buffer, root_hub_dev_des, len);
619 break;
620 case 0x0200: /* configuration descriptor */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900621 len = min3(txlen, (int)sizeof(root_hub_config_des), (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700622 memcpy(buffer, root_hub_config_des, len);
623 break;
624 case 0x0300: /* string descriptors */
625 switch (wValue & 0xff) {
626 case 0x00:
Masahiro Yamadadb204642014-11-07 03:03:31 +0900627 len = min3(txlen, (int)sizeof(root_hub_str_index0),
628 (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700629 memcpy(buffer, root_hub_str_index0, len);
630 break;
631 case 0x01:
Masahiro Yamadadb204642014-11-07 03:03:31 +0900632 len = min3(txlen, (int)sizeof(root_hub_str_index1),
633 (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700634 memcpy(buffer, root_hub_str_index1, len);
635 break;
636 }
637 break;
638 default:
639 stat = USB_ST_STALLED;
640 }
641 break;
642
643 case USB_TYPE_CLASS:
644 /* Root port config, set 1 port and nothing else. */
645 dsc = 0x00000001;
646
647 data[0] = 9; /* min length; */
648 data[1] = 0x29;
649 data[2] = dsc & RH_A_NDP;
650 data[3] = 0;
651 if (dsc & RH_A_PSM)
652 data[3] |= 0x1;
653 if (dsc & RH_A_NOCP)
654 data[3] |= 0x10;
655 else if (dsc & RH_A_OCPM)
656 data[3] |= 0x8;
657
658 /* corresponds to data[4-7] */
659 data[5] = (dsc & RH_A_POTPGT) >> 24;
660 data[7] = dsc & RH_B_DR;
661 if (data[2] < 7) {
662 data[8] = 0xff;
663 } else {
664 data[0] += 2;
665 data[8] = (dsc & RH_B_DR) >> 8;
666 data[9] = 0xff;
667 data[10] = data[9];
668 }
669
Masahiro Yamadadb204642014-11-07 03:03:31 +0900670 len = min3(txlen, (int)data[0], (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700671 memcpy(buffer, data, len);
672 break;
673 default:
674 puts("unsupported root hub command\n");
675 stat = USB_ST_STALLED;
676 }
677
678 dev->act_len = min(len, txlen);
679 dev->status = stat;
680
681 return stat;
682}
683
684/* Direction: In ; Request: Configuration */
685static int dwc_otg_submit_rh_msg_in_configuration(struct usb_device *dev,
686 void *buffer, int txlen,
687 struct devrequest *cmd)
688{
689 int len = 0;
690 int stat = 0;
691
692 switch (cmd->requesttype & ~USB_DIR_IN) {
693 case 0:
694 *(uint8_t *)buffer = 0x01;
695 len = 1;
696 break;
697 default:
698 puts("unsupported root hub command\n");
699 stat = USB_ST_STALLED;
700 }
701
702 dev->act_len = min(len, txlen);
703 dev->status = stat;
704
705 return stat;
706}
707
708/* Direction: In */
Simon Glasse3c23a02015-07-07 20:53:36 -0600709static int dwc_otg_submit_rh_msg_in(struct dwc2_priv *priv,
710 struct usb_device *dev, void *buffer,
711 int txlen, struct devrequest *cmd)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700712{
713 switch (cmd->request) {
714 case USB_REQ_GET_STATUS:
Simon Glasse3c23a02015-07-07 20:53:36 -0600715 return dwc_otg_submit_rh_msg_in_status(priv->regs, dev, buffer,
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700716 txlen, cmd);
717 case USB_REQ_GET_DESCRIPTOR:
718 return dwc_otg_submit_rh_msg_in_descriptor(dev, buffer,
719 txlen, cmd);
720 case USB_REQ_GET_CONFIGURATION:
721 return dwc_otg_submit_rh_msg_in_configuration(dev, buffer,
722 txlen, cmd);
723 default:
724 puts("unsupported root hub command\n");
725 return USB_ST_STALLED;
726 }
727}
728
729/* Direction: Out */
Simon Glasse3c23a02015-07-07 20:53:36 -0600730static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
731 struct usb_device *dev,
732 void *buffer, int txlen,
733 struct devrequest *cmd)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700734{
Simon Glasse3c23a02015-07-07 20:53:36 -0600735 struct dwc2_core_regs *regs = priv->regs;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700736 int len = 0;
737 int stat = 0;
738 uint16_t bmrtype_breq = cmd->requesttype | (cmd->request << 8);
739 uint16_t wValue = cpu_to_le16(cmd->value);
740
741 switch (bmrtype_breq & ~USB_DIR_IN) {
742 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_ENDPOINT:
743 case (USB_REQ_CLEAR_FEATURE << 8) | USB_TYPE_CLASS:
744 break;
745
746 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
747 switch (wValue) {
748 case USB_PORT_FEAT_C_CONNECTION:
Teik Heng Chong9715f3c2023-06-21 11:13:58 +0800749 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTCONNDET);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700750 break;
751 }
752 break;
753
754 case (USB_REQ_SET_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
755 switch (wValue) {
756 case USB_PORT_FEAT_SUSPEND:
757 break;
758
759 case USB_PORT_FEAT_RESET:
Teik Heng Chong9715f3c2023-06-21 11:13:58 +0800760 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTRST);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700761 mdelay(50);
Teik Heng Chong9715f3c2023-06-21 11:13:58 +0800762 clrbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK | DWC2_HPRT0_PRTRST);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700763 break;
764
765 case USB_PORT_FEAT_POWER:
Teik Heng Chong9715f3c2023-06-21 11:13:58 +0800766 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTRST);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700767 break;
768
769 case USB_PORT_FEAT_ENABLE:
770 break;
771 }
772 break;
773 case (USB_REQ_SET_ADDRESS << 8):
Simon Glasse3c23a02015-07-07 20:53:36 -0600774 priv->root_hub_devnum = wValue;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700775 break;
776 case (USB_REQ_SET_CONFIGURATION << 8):
777 break;
778 default:
779 puts("unsupported root hub command\n");
780 stat = USB_ST_STALLED;
781 }
782
783 len = min(len, txlen);
784
785 dev->act_len = len;
786 dev->status = stat;
787
788 return stat;
789}
790
Simon Glasse3c23a02015-07-07 20:53:36 -0600791static int dwc_otg_submit_rh_msg(struct dwc2_priv *priv, struct usb_device *dev,
792 unsigned long pipe, void *buffer, int txlen,
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700793 struct devrequest *cmd)
794{
795 int stat = 0;
796
797 if (usb_pipeint(pipe)) {
798 puts("Root-Hub submit IRQ: NOT implemented\n");
799 return 0;
800 }
801
802 if (cmd->requesttype & USB_DIR_IN)
Simon Glasse3c23a02015-07-07 20:53:36 -0600803 stat = dwc_otg_submit_rh_msg_in(priv, dev, buffer, txlen, cmd);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700804 else
Simon Glasse3c23a02015-07-07 20:53:36 -0600805 stat = dwc_otg_submit_rh_msg_out(priv, dev, buffer, txlen, cmd);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700806
807 mdelay(1);
808
809 return stat;
810}
811
Stefan Brüns081dcc72016-01-23 01:42:25 +0100812int wait_for_chhltd(struct dwc2_hc_regs *hc_regs, uint32_t *sub, u8 *toggle)
Stephen Warren8a346662015-03-07 22:48:51 -0700813{
Stephen Warren8a346662015-03-07 22:48:51 -0700814 int ret;
815 uint32_t hcint, hctsiz;
816
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100817 ret = wait_for_bit_le32(&hc_regs->hcint, DWC2_HCINT_CHHLTD, true,
Christophe Kerello4edc9802018-03-15 18:00:31 +0100818 2000, false);
Stephen Warren8a346662015-03-07 22:48:51 -0700819 if (ret)
820 return ret;
821
822 hcint = readl(&hc_regs->hcint);
Stephen Warren8a346662015-03-07 22:48:51 -0700823 hctsiz = readl(&hc_regs->hctsiz);
824 *sub = (hctsiz & DWC2_HCTSIZ_XFERSIZE_MASK) >>
825 DWC2_HCTSIZ_XFERSIZE_OFFSET;
Stephen Warren9f80e742015-03-07 22:48:55 -0700826 *toggle = (hctsiz & DWC2_HCTSIZ_PID_MASK) >> DWC2_HCTSIZ_PID_OFFSET;
Stephen Warren8a346662015-03-07 22:48:51 -0700827
Stefan Brünsaa9506e2016-01-17 04:09:52 +0100828 debug("%s: HCINT=%08x sub=%u toggle=%d\n", __func__, hcint, *sub,
829 *toggle);
Stephen Warren8a346662015-03-07 22:48:51 -0700830
Stefan Brünsaa9506e2016-01-17 04:09:52 +0100831 if (hcint & DWC2_HCINT_XFERCOMP)
832 return 0;
833
834 if (hcint & (DWC2_HCINT_NAK | DWC2_HCINT_FRMOVRUN))
835 return -EAGAIN;
Stephen Warren8a346662015-03-07 22:48:51 -0700836
Stefan Brünsaa9506e2016-01-17 04:09:52 +0100837 debug("%s: Error (HCINT=%08x)\n", __func__, hcint);
838 return -EINVAL;
Stephen Warren8a346662015-03-07 22:48:51 -0700839}
840
Stephen Warren972ad642015-03-07 22:48:52 -0700841static int dwc2_eptype[] = {
842 DWC2_HCCHAR_EPTYPE_ISOC,
843 DWC2_HCCHAR_EPTYPE_INTR,
844 DWC2_HCCHAR_EPTYPE_CONTROL,
845 DWC2_HCCHAR_EPTYPE_BULK,
846};
847
Stefan Brüns2385db32016-01-17 04:09:53 +0100848static int transfer_chunk(struct dwc2_hc_regs *hc_regs, void *aligned_buffer,
Stefan Brüns081dcc72016-01-23 01:42:25 +0100849 u8 *pid, int in, void *buffer, int num_packets,
Stefan Brüns247241e2016-01-17 04:09:56 +0100850 int xfer_len, int *actual_len, int odd_frame)
Stefan Brüns2385db32016-01-17 04:09:53 +0100851{
852 int ret = 0;
853 uint32_t sub;
854
855 debug("%s: chunk: pid %d xfer_len %u pkts %u\n", __func__,
856 *pid, xfer_len, num_packets);
857
858 writel((xfer_len << DWC2_HCTSIZ_XFERSIZE_OFFSET) |
859 (num_packets << DWC2_HCTSIZ_PKTCNT_OFFSET) |
860 (*pid << DWC2_HCTSIZ_PID_OFFSET),
861 &hc_regs->hctsiz);
862
Eddie Cai408bdee2017-04-06 11:37:04 +0800863 if (xfer_len) {
864 if (in) {
865 invalidate_dcache_range(
866 (uintptr_t)aligned_buffer,
867 (uintptr_t)aligned_buffer +
868 roundup(xfer_len, ARCH_DMA_MINALIGN));
869 } else {
870 memcpy(aligned_buffer, buffer, xfer_len);
871 flush_dcache_range(
872 (uintptr_t)aligned_buffer,
873 (uintptr_t)aligned_buffer +
874 roundup(xfer_len, ARCH_DMA_MINALIGN));
875 }
Stefan Brüns2385db32016-01-17 04:09:53 +0100876 }
877
878 writel(phys_to_bus((unsigned long)aligned_buffer), &hc_regs->hcdma);
879
880 /* Clear old interrupt conditions for this host channel. */
881 writel(0x3fff, &hc_regs->hcint);
882
883 /* Set host channel enable after all other setup is complete. */
884 clrsetbits_le32(&hc_regs->hcchar, DWC2_HCCHAR_MULTICNT_MASK |
Stefan Brüns247241e2016-01-17 04:09:56 +0100885 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS |
886 DWC2_HCCHAR_ODDFRM,
Stefan Brüns2385db32016-01-17 04:09:53 +0100887 (1 << DWC2_HCCHAR_MULTICNT_OFFSET) |
Stefan Brüns247241e2016-01-17 04:09:56 +0100888 (odd_frame << DWC2_HCCHAR_ODDFRM_OFFSET) |
Stefan Brüns2385db32016-01-17 04:09:53 +0100889 DWC2_HCCHAR_CHEN);
890
891 ret = wait_for_chhltd(hc_regs, &sub, pid);
892 if (ret < 0)
893 return ret;
894
895 if (in) {
896 xfer_len -= sub;
897
898 invalidate_dcache_range((unsigned long)aligned_buffer,
899 (unsigned long)aligned_buffer +
900 roundup(xfer_len, ARCH_DMA_MINALIGN));
901
902 memcpy(buffer, aligned_buffer, xfer_len);
903 }
904 *actual_len = xfer_len;
905
906 return ret;
907}
908
Simon Glasse3c23a02015-07-07 20:53:36 -0600909int chunk_msg(struct dwc2_priv *priv, struct usb_device *dev,
Stefan Brüns081dcc72016-01-23 01:42:25 +0100910 unsigned long pipe, u8 *pid, int in, void *buffer, int len)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700911{
Simon Glasse3c23a02015-07-07 20:53:36 -0600912 struct dwc2_core_regs *regs = priv->regs;
Stephen Warren972ad642015-03-07 22:48:52 -0700913 struct dwc2_hc_regs *hc_regs = &regs->hc_regs[DWC2_HC_CHANNEL];
Stefan Brüns247241e2016-01-17 04:09:56 +0100914 struct dwc2_host_regs *host_regs = &regs->host_regs;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700915 int devnum = usb_pipedevice(pipe);
916 int ep = usb_pipeendpoint(pipe);
917 int max = usb_maxpacket(dev, pipe);
Stephen Warren972ad642015-03-07 22:48:52 -0700918 int eptype = dwc2_eptype[usb_pipetype(pipe)];
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700919 int done = 0;
Stephen Warren766fe412015-04-11 21:52:02 -0600920 int ret = 0;
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100921 int do_split = 0;
922 int complete_split = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700923 uint32_t xfer_len;
924 uint32_t num_packets;
925 int stop_transfer = 0;
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100926 uint32_t max_xfer_len;
Stefan Brüns247241e2016-01-17 04:09:56 +0100927 int ssplit_frame_num = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700928
Stephen Warren972ad642015-03-07 22:48:52 -0700929 debug("%s: msg: pipe %lx pid %d in %d len %d\n", __func__, pipe, *pid,
930 in, len);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700931
Tom Rinia99817c2021-08-10 16:17:55 -0400932 max_xfer_len = DWC2_MAX_PACKET_COUNT * max;
933 if (max_xfer_len > DWC2_MAX_TRANSFER_SIZE)
934 max_xfer_len = DWC2_MAX_TRANSFER_SIZE;
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100935 if (max_xfer_len > DWC2_DATA_BUF_SIZE)
936 max_xfer_len = DWC2_DATA_BUF_SIZE;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700937
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100938 /* Make sure that max_xfer_len is a multiple of max packet size. */
939 num_packets = max_xfer_len / max;
940 max_xfer_len = num_packets * max;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700941
Stefan Brüns2385db32016-01-17 04:09:53 +0100942 /* Initialize channel */
943 dwc_otg_hc_init(regs, DWC2_HC_CHANNEL, dev, devnum, ep, in,
944 eptype, max);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700945
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100946 /* Check if the target is a FS/LS device behind a HS hub */
947 if (dev->speed != USB_SPEED_HIGH) {
948 uint8_t hub_addr;
949 uint8_t hub_port;
950 uint32_t hprt0 = readl(&regs->hprt0);
951 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
952 DWC2_HPRT0_PRTSPD_HIGH) {
953 usb_find_usb2_hub_address_port(dev, &hub_addr,
954 &hub_port);
955 dwc_otg_hc_init_split(hc_regs, hub_addr, hub_port);
956
957 do_split = 1;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700958 num_packets = 1;
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100959 max_xfer_len = max;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700960 }
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100961 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700962
Stefan Brüns2385db32016-01-17 04:09:53 +0100963 do {
964 int actual_len = 0;
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100965 uint32_t hcint;
Stefan Brüns247241e2016-01-17 04:09:56 +0100966 int odd_frame = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700967 xfer_len = len - done;
Stephen Warren972ad642015-03-07 22:48:52 -0700968
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100969 if (xfer_len > max_xfer_len)
970 xfer_len = max_xfer_len;
971 else if (xfer_len > max)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700972 num_packets = (xfer_len + max - 1) / max;
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100973 else
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700974 num_packets = 1;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700975
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100976 if (complete_split)
977 setbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
978 else if (do_split)
979 clrbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
Alexander Stein76fac502015-07-24 09:22:14 +0200980
Stefan Brüns247241e2016-01-17 04:09:56 +0100981 if (eptype == DWC2_HCCHAR_EPTYPE_INTR) {
982 int uframe_num = readl(&host_regs->hfnum);
983 if (!(uframe_num & 0x1))
984 odd_frame = 1;
Simon Glasse3c23a02015-07-07 20:53:36 -0600985 }
Stephen Warren7100da32015-03-08 11:08:13 -0600986
Stefan Brüns2385db32016-01-17 04:09:53 +0100987 ret = transfer_chunk(hc_regs, priv->aligned_buffer, pid,
988 in, (char *)buffer + done, num_packets,
Stefan Brüns247241e2016-01-17 04:09:56 +0100989 xfer_len, &actual_len, odd_frame);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700990
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100991 hcint = readl(&hc_regs->hcint);
992 if (complete_split) {
993 stop_transfer = 0;
Stefan Brüns247241e2016-01-17 04:09:56 +0100994 if (hcint & DWC2_HCINT_NYET) {
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100995 ret = 0;
Stefan Brüns247241e2016-01-17 04:09:56 +0100996 int frame_num = DWC2_HFNUM_MAX_FRNUM &
997 readl(&host_regs->hfnum);
998 if (((frame_num - ssplit_frame_num) &
999 DWC2_HFNUM_MAX_FRNUM) > 4)
1000 ret = -EAGAIN;
1001 } else
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001002 complete_split = 0;
1003 } else if (do_split) {
1004 if (hcint & DWC2_HCINT_ACK) {
Stefan Brüns247241e2016-01-17 04:09:56 +01001005 ssplit_frame_num = DWC2_HFNUM_MAX_FRNUM &
1006 readl(&host_regs->hfnum);
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001007 ret = 0;
1008 complete_split = 1;
1009 }
1010 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001011
Stephen Warren766fe412015-04-11 21:52:02 -06001012 if (ret)
Stephen Warren8a346662015-03-07 22:48:51 -07001013 break;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001014
Stefan Brüns2385db32016-01-17 04:09:53 +01001015 if (actual_len < xfer_len)
1016 stop_transfer = 1;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001017
Stefan Brüns2385db32016-01-17 04:09:53 +01001018 done += actual_len;
Stephen Warren7100da32015-03-08 11:08:13 -06001019
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001020 /* Transactions are done when when either all data is transferred or
1021 * there is a short transfer. In case of a SPLIT make sure the CSPLIT
1022 * is executed.
1023 */
1024 } while (((done < len) && !stop_transfer) || complete_split);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001025
1026 writel(0, &hc_regs->hcintmsk);
1027 writel(0xFFFFFFFF, &hc_regs->hcint);
1028
1029 dev->status = 0;
1030 dev->act_len = done;
1031
Stephen Warren766fe412015-04-11 21:52:02 -06001032 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001033}
1034
Stephen Warren972ad642015-03-07 22:48:52 -07001035/* U-Boot USB transmission interface */
Simon Glasse3c23a02015-07-07 20:53:36 -06001036int _submit_bulk_msg(struct dwc2_priv *priv, struct usb_device *dev,
1037 unsigned long pipe, void *buffer, int len)
Stephen Warren972ad642015-03-07 22:48:52 -07001038{
1039 int devnum = usb_pipedevice(pipe);
1040 int ep = usb_pipeendpoint(pipe);
Stefan Brüns081dcc72016-01-23 01:42:25 +01001041 u8* pid;
Stephen Warren972ad642015-03-07 22:48:52 -07001042
Stefan Brüns081dcc72016-01-23 01:42:25 +01001043 if ((devnum >= MAX_DEVICE) || (devnum == priv->root_hub_devnum)) {
Stephen Warren972ad642015-03-07 22:48:52 -07001044 dev->status = 0;
1045 return -EINVAL;
1046 }
1047
Stefan Brüns081dcc72016-01-23 01:42:25 +01001048 if (usb_pipein(pipe))
1049 pid = &priv->in_data_toggle[devnum][ep];
1050 else
1051 pid = &priv->out_data_toggle[devnum][ep];
1052
1053 return chunk_msg(priv, dev, pipe, pid, usb_pipein(pipe), buffer, len);
Stephen Warren972ad642015-03-07 22:48:52 -07001054}
1055
Simon Glasse3c23a02015-07-07 20:53:36 -06001056static int _submit_control_msg(struct dwc2_priv *priv, struct usb_device *dev,
1057 unsigned long pipe, void *buffer, int len,
1058 struct devrequest *setup)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001059{
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001060 int devnum = usb_pipedevice(pipe);
Stefan Brüns081dcc72016-01-23 01:42:25 +01001061 int ret, act_len;
1062 u8 pid;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001063 /* For CONTROL endpoint pid should start with DATA1 */
1064 int status_direction;
1065
Simon Glasse3c23a02015-07-07 20:53:36 -06001066 if (devnum == priv->root_hub_devnum) {
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001067 dev->status = 0;
1068 dev->speed = USB_SPEED_HIGH;
Simon Glasse3c23a02015-07-07 20:53:36 -06001069 return dwc_otg_submit_rh_msg(priv, dev, pipe, buffer, len,
1070 setup);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001071 }
1072
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001073 /* SETUP stage */
Stephen Warren4db200e2015-03-07 22:48:53 -07001074 pid = DWC2_HC_PID_SETUP;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001075 do {
1076 ret = chunk_msg(priv, dev, pipe, &pid, 0, setup, 8);
1077 } while (ret == -EAGAIN);
Stephen Warren4db200e2015-03-07 22:48:53 -07001078 if (ret)
1079 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001080
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001081 /* DATA stage */
1082 act_len = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001083 if (buffer) {
Stephen Warrenb0ad4a32015-03-07 22:48:54 -07001084 pid = DWC2_HC_PID_DATA1;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001085 do {
1086 ret = chunk_msg(priv, dev, pipe, &pid, usb_pipein(pipe),
1087 buffer, len);
1088 act_len += dev->act_len;
1089 buffer += dev->act_len;
1090 len -= dev->act_len;
1091 } while (ret == -EAGAIN);
Stephen Warren4db200e2015-03-07 22:48:53 -07001092 if (ret)
1093 return ret;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001094 status_direction = usb_pipeout(pipe);
1095 } else {
1096 /* No-data CONTROL always ends with an IN transaction */
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001097 status_direction = 1;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001098 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001099
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001100 /* STATUS stage */
Stephen Warren4db200e2015-03-07 22:48:53 -07001101 pid = DWC2_HC_PID_DATA1;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001102 do {
1103 ret = chunk_msg(priv, dev, pipe, &pid, status_direction,
1104 priv->status_buffer, 0);
1105 } while (ret == -EAGAIN);
Stephen Warren4db200e2015-03-07 22:48:53 -07001106 if (ret)
1107 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001108
Stephen Warren4db200e2015-03-07 22:48:53 -07001109 dev->act_len = act_len;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001110
Stephen Warren8a346662015-03-07 22:48:51 -07001111 return 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001112}
1113
Simon Glasse3c23a02015-07-07 20:53:36 -06001114int _submit_int_msg(struct dwc2_priv *priv, struct usb_device *dev,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001115 unsigned long pipe, void *buffer, int len, int interval,
1116 bool nonblock)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001117{
Stephen Warren766fe412015-04-11 21:52:02 -06001118 unsigned long timeout;
1119 int ret;
1120
Stephen Warrendf7b37d2015-04-10 21:05:22 -06001121 /* FIXME: what is interval? */
Stephen Warren766fe412015-04-11 21:52:02 -06001122
1123 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1124 for (;;) {
1125 if (get_timer(0) > timeout) {
Sean Anderson62eff9b2020-09-15 10:45:15 -04001126#if CONFIG_IS_ENABLED(DM_USB)
1127 dev_err(dev->dev,
1128 "Timeout poll on interrupt endpoint\n");
1129#else
1130 log_err("Timeout poll on interrupt endpoint\n");
1131#endif
Stephen Warren766fe412015-04-11 21:52:02 -06001132 return -ETIMEDOUT;
1133 }
Simon Glasse3c23a02015-07-07 20:53:36 -06001134 ret = _submit_bulk_msg(priv, dev, pipe, buffer, len);
Michal Suchanekc7a7ae52019-08-18 10:55:28 +02001135 if ((ret != -EAGAIN) || nonblock)
Stephen Warren766fe412015-04-11 21:52:02 -06001136 return ret;
1137 }
Ley Foon Tan23865562018-08-29 00:08:48 +08001138}
1139
1140static int dwc2_reset(struct udevice *dev)
1141{
1142 int ret;
1143 struct dwc2_priv *priv = dev_get_priv(dev);
1144
1145 ret = reset_get_bulk(dev, &priv->resets);
1146 if (ret) {
1147 dev_warn(dev, "Can't get reset: %d\n", ret);
1148 /* Return 0 if error due to !CONFIG_DM_RESET and reset
1149 * DT property is not present.
1150 */
1151 if (ret == -ENOENT || ret == -ENOTSUPP)
1152 return 0;
1153 else
1154 return ret;
1155 }
1156
Patrick Delaunay8bef1692020-04-27 15:30:00 +02001157 /* force reset to clear all IP register */
1158 reset_assert_bulk(&priv->resets);
Ley Foon Tan23865562018-08-29 00:08:48 +08001159 ret = reset_deassert_bulk(&priv->resets);
1160 if (ret) {
1161 reset_release_bulk(&priv->resets);
1162 dev_err(dev, "Failed to reset: %d\n", ret);
1163 return ret;
1164 }
1165
1166 return 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001167}
1168
Kever Yang327c24d2017-03-10 12:05:14 +08001169static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001170{
Simon Glasse3c23a02015-07-07 20:53:36 -06001171 struct dwc2_core_regs *regs = priv->regs;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001172 uint32_t snpsid;
1173 int i, j;
Ley Foon Tan23865562018-08-29 00:08:48 +08001174 int ret;
1175
1176 ret = dwc2_reset(dev);
1177 if (ret)
1178 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001179
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001180 snpsid = readl(&regs->gsnpsid);
Patrice Chotarda259c1d2018-03-15 18:00:32 +01001181 dev_info(dev, "Core Release: %x.%03x\n",
1182 snpsid >> 12 & 0xf, snpsid & 0xfff);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001183
Peter Griffin79d657d2015-05-12 14:38:27 +01001184 if ((snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_2xx &&
1185 (snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_3xx) {
Patrice Chotarda259c1d2018-03-15 18:00:32 +01001186 dev_info(dev, "SNPSID invalid (not DWC2 OTG device): %08x\n",
1187 snpsid);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001188 return -ENODEV;
1189 }
1190
Tom Rinia99817c2021-08-10 16:17:55 -04001191#ifdef DWC2_PHY_ULPI_EXT_VBUS
Marek Vasut39209492016-04-27 14:55:57 +02001192 priv->ext_vbus = 1;
1193#else
1194 priv->ext_vbus = 0;
1195#endif
1196
Sean Anderson62eff9b2020-09-15 10:45:15 -04001197 dwc_otg_core_init(dev);
João Loureiroec76d072021-03-17 16:52:21 +01001198
1199 if (usb_get_dr_mode(dev_ofnode(dev)) == USB_DR_MODE_PERIPHERAL) {
1200 dev_dbg(dev, "USB device %s dr_mode set to %d. Skipping host_init.\n",
1201 dev->name, usb_get_dr_mode(dev_ofnode(dev)));
1202 } else {
1203 dwc_otg_core_host_init(dev, regs);
1204 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001205
Teik Heng Chong9715f3c2023-06-21 11:13:58 +08001206 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTRST);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001207 mdelay(50);
Teik Heng Chong9715f3c2023-06-21 11:13:58 +08001208 clrbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK | DWC2_HPRT0_PRTRST);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001209
1210 for (i = 0; i < MAX_DEVICE; i++) {
Stefan Brüns081dcc72016-01-23 01:42:25 +01001211 for (j = 0; j < MAX_ENDPOINT; j++) {
1212 priv->in_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1213 priv->out_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1214 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001215 }
1216
Stefan Roesec526e832016-05-06 13:53:37 +02001217 /*
1218 * Add a 1 second delay here. This gives the host controller
1219 * a bit time before the comminucation with the USB devices
1220 * is started (the bus is scanned) and fixes the USB detection
1221 * problems with some problematic USB keys.
1222 */
1223 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
1224 mdelay(1000);
1225
Patrick Delaunayce17fe12020-04-27 15:30:01 +02001226 printf("USB DWC2\n");
1227
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001228 return 0;
1229}
1230
Simon Glasse3c23a02015-07-07 20:53:36 -06001231static void dwc2_uninit_common(struct dwc2_core_regs *regs)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001232{
1233 /* Put everything in reset. */
Teik Heng Chong9715f3c2023-06-21 11:13:58 +08001234 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTRST);
Simon Glasse3c23a02015-07-07 20:53:36 -06001235}
1236
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01001237#if !CONFIG_IS_ENABLED(DM_USB)
Simon Glasse3c23a02015-07-07 20:53:36 -06001238int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1239 int len, struct devrequest *setup)
1240{
1241 return _submit_control_msg(&local, dev, pipe, buffer, len, setup);
1242}
1243
1244int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1245 int len)
1246{
1247 return _submit_bulk_msg(&local, dev, pipe, buffer, len);
1248}
1249
1250int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001251 int len, int interval, bool nonblock)
Simon Glasse3c23a02015-07-07 20:53:36 -06001252{
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001253 return _submit_int_msg(&local, dev, pipe, buffer, len, interval,
1254 nonblock);
Simon Glasse3c23a02015-07-07 20:53:36 -06001255}
1256
1257/* U-Boot USB control interface */
1258int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1259{
1260 struct dwc2_priv *priv = &local;
1261
1262 memset(priv, '\0', sizeof(*priv));
1263 priv->root_hub_devnum = 0;
1264 priv->regs = (struct dwc2_core_regs *)CONFIG_USB_DWC2_REG_ADDR;
1265 priv->aligned_buffer = aligned_buffer_addr;
1266 priv->status_buffer = status_buffer_addr;
1267
1268 /* board-dependant init */
1269 if (board_usb_init(index, USB_INIT_HOST))
1270 return -1;
1271
Kever Yang327c24d2017-03-10 12:05:14 +08001272 return dwc2_init_common(NULL, priv);
Simon Glasse3c23a02015-07-07 20:53:36 -06001273}
1274
1275int usb_lowlevel_stop(int index)
1276{
1277 dwc2_uninit_common(local.regs);
1278
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001279 return 0;
1280}
Simon Glassa7ea72c2015-07-07 20:53:37 -06001281#endif
1282
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01001283#if CONFIG_IS_ENABLED(DM_USB)
Simon Glassa7ea72c2015-07-07 20:53:37 -06001284static int dwc2_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1285 unsigned long pipe, void *buffer, int length,
1286 struct devrequest *setup)
1287{
1288 struct dwc2_priv *priv = dev_get_priv(dev);
1289
1290 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1291 dev->name, udev, udev->dev->name, udev->portnr);
1292
1293 return _submit_control_msg(priv, udev, pipe, buffer, length, setup);
1294}
1295
1296static int dwc2_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1297 unsigned long pipe, void *buffer, int length)
1298{
1299 struct dwc2_priv *priv = dev_get_priv(dev);
1300
1301 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1302
1303 return _submit_bulk_msg(priv, udev, pipe, buffer, length);
1304}
1305
1306static int dwc2_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1307 unsigned long pipe, void *buffer, int length,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001308 int interval, bool nonblock)
Simon Glassa7ea72c2015-07-07 20:53:37 -06001309{
1310 struct dwc2_priv *priv = dev_get_priv(dev);
1311
1312 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1313
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001314 return _submit_int_msg(priv, udev, pipe, buffer, length, interval,
1315 nonblock);
Simon Glassa7ea72c2015-07-07 20:53:37 -06001316}
1317
Simon Glassaad29ae2020-12-03 16:55:21 -07001318static int dwc2_usb_of_to_plat(struct udevice *dev)
Simon Glassa7ea72c2015-07-07 20:53:37 -06001319{
1320 struct dwc2_priv *priv = dev_get_priv(dev);
Simon Glassa7ea72c2015-07-07 20:53:37 -06001321
Sean Anderson62eff9b2020-09-15 10:45:15 -04001322 priv->regs = dev_read_addr_ptr(dev);
1323 if (!priv->regs)
Simon Glassa7ea72c2015-07-07 20:53:37 -06001324 return -EINVAL;
Simon Glassa7ea72c2015-07-07 20:53:37 -06001325
Meng Dongyang697a8bc2017-06-28 19:22:43 +08001326 priv->oc_disable = dev_read_bool(dev, "disable-over-current");
1327 priv->hnp_srp_disable = dev_read_bool(dev, "hnp-srp-disable");
Meng Dongyangcc3fe062017-06-08 15:34:20 +08001328
Simon Glassa7ea72c2015-07-07 20:53:37 -06001329 return 0;
1330}
1331
Patrick Delaunaybb3569d2020-04-27 15:29:58 +02001332static int dwc2_setup_phy(struct udevice *dev)
1333{
1334 struct dwc2_priv *priv = dev_get_priv(dev);
1335 int ret;
1336
1337 ret = generic_phy_get_by_index(dev, 0, &priv->phy);
1338 if (ret) {
1339 if (ret == -ENOENT)
1340 return 0; /* no PHY, nothing to do */
1341 dev_err(dev, "Failed to get USB PHY: %d.\n", ret);
1342 return ret;
1343 }
1344
1345 ret = generic_phy_init(&priv->phy);
1346 if (ret) {
1347 dev_dbg(dev, "Failed to init USB PHY: %d.\n", ret);
1348 return ret;
1349 }
1350
1351 ret = generic_phy_power_on(&priv->phy);
1352 if (ret) {
1353 dev_dbg(dev, "Failed to power on USB PHY: %d.\n", ret);
1354 generic_phy_exit(&priv->phy);
1355 return ret;
1356 }
1357
1358 return 0;
1359}
1360
1361static int dwc2_shutdown_phy(struct udevice *dev)
1362{
1363 struct dwc2_priv *priv = dev_get_priv(dev);
1364 int ret;
1365
1366 /* PHY is not valid when generic_phy_get_by_index() = -ENOENT */
1367 if (!generic_phy_valid(&priv->phy))
1368 return 0; /* no PHY, nothing to do */
1369
1370 ret = generic_phy_power_off(&priv->phy);
1371 if (ret) {
1372 dev_dbg(dev, "Failed to power off USB PHY: %d.\n", ret);
1373 return ret;
1374 }
1375
1376 ret = generic_phy_exit(&priv->phy);
1377 if (ret) {
1378 dev_dbg(dev, "Failed to power off USB PHY: %d.\n", ret);
1379 return ret;
1380 }
1381
1382 return 0;
1383}
1384
Patrick Delaunay9225f3e2020-04-27 15:29:59 +02001385static int dwc2_clk_init(struct udevice *dev)
1386{
1387 struct dwc2_priv *priv = dev_get_priv(dev);
1388 int ret;
1389
1390 ret = clk_get_bulk(dev, &priv->clks);
1391 if (ret == -ENOSYS || ret == -ENOENT)
1392 return 0;
1393 if (ret)
1394 return ret;
1395
1396 ret = clk_enable_bulk(&priv->clks);
1397 if (ret) {
1398 clk_release_bulk(&priv->clks);
1399 return ret;
1400 }
1401
1402 return 0;
1403}
1404
Simon Glassa7ea72c2015-07-07 20:53:37 -06001405static int dwc2_usb_probe(struct udevice *dev)
1406{
1407 struct dwc2_priv *priv = dev_get_priv(dev);
Marek Vasut1ea9ac62016-04-26 03:02:35 +02001408 struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
Patrick Delaunaybb3569d2020-04-27 15:29:58 +02001409 int ret;
Marek Vasut1ea9ac62016-04-26 03:02:35 +02001410
1411 bus_priv->desc_before_addr = true;
Simon Glassa7ea72c2015-07-07 20:53:37 -06001412
Patrick Delaunay9225f3e2020-04-27 15:29:59 +02001413 ret = dwc2_clk_init(dev);
1414 if (ret)
1415 return ret;
1416
Patrick Delaunaybb3569d2020-04-27 15:29:58 +02001417 ret = dwc2_setup_phy(dev);
1418 if (ret)
1419 return ret;
1420
Kever Yang327c24d2017-03-10 12:05:14 +08001421 return dwc2_init_common(dev, priv);
Simon Glassa7ea72c2015-07-07 20:53:37 -06001422}
1423
1424static int dwc2_usb_remove(struct udevice *dev)
1425{
1426 struct dwc2_priv *priv = dev_get_priv(dev);
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +01001427 int ret;
1428
1429 ret = dwc_vbus_supply_exit(dev);
1430 if (ret)
1431 return ret;
Simon Glassa7ea72c2015-07-07 20:53:37 -06001432
Patrick Delaunaybb3569d2020-04-27 15:29:58 +02001433 ret = dwc2_shutdown_phy(dev);
1434 if (ret) {
1435 dev_dbg(dev, "Failed to shutdown USB PHY: %d.\n", ret);
1436 return ret;
1437 }
1438
Simon Glassa7ea72c2015-07-07 20:53:37 -06001439 dwc2_uninit_common(priv->regs);
1440
Ley Foon Tan23865562018-08-29 00:08:48 +08001441 reset_release_bulk(&priv->resets);
Patrick Delaunay9225f3e2020-04-27 15:29:59 +02001442 clk_disable_bulk(&priv->clks);
1443 clk_release_bulk(&priv->clks);
Ley Foon Tan23865562018-08-29 00:08:48 +08001444
Simon Glassa7ea72c2015-07-07 20:53:37 -06001445 return 0;
1446}
1447
1448struct dm_usb_ops dwc2_usb_ops = {
1449 .control = dwc2_submit_control_msg,
1450 .bulk = dwc2_submit_bulk_msg,
1451 .interrupt = dwc2_submit_int_msg,
1452};
1453
1454static const struct udevice_id dwc2_usb_ids[] = {
1455 { .compatible = "brcm,bcm2835-usb" },
Emmanuel Vadot80447002018-07-02 14:34:23 +02001456 { .compatible = "brcm,bcm2708-usb" },
Marek Vasutac4a35f2015-08-12 22:19:14 +02001457 { .compatible = "snps,dwc2" },
Simon Glassa7ea72c2015-07-07 20:53:37 -06001458 { }
1459};
1460
1461U_BOOT_DRIVER(usb_dwc2) = {
Marek Vasutaf83c782015-08-12 22:19:15 +02001462 .name = "dwc2_usb",
Simon Glassa7ea72c2015-07-07 20:53:37 -06001463 .id = UCLASS_USB,
1464 .of_match = dwc2_usb_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001465 .of_to_plat = dwc2_usb_of_to_plat,
Simon Glassa7ea72c2015-07-07 20:53:37 -06001466 .probe = dwc2_usb_probe,
1467 .remove = dwc2_usb_remove,
1468 .ops = &dwc2_usb_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001469 .priv_auto = sizeof(struct dwc2_priv),
Simon Glassa7ea72c2015-07-07 20:53:37 -06001470 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1471};
1472#endif