blob: a9dbb85f4e6c5a9122108eb64f77e99758b840e8 [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
Patrick Delaunay9225f3e2020-04-27 15:29:59 +02007#include <clk.h>
Simon Glass63334482019-11-14 12:57:39 -07008#include <cpu_func.h>
Simon Glassa7ea72c2015-07-07 20:53:37 -06009#include <dm.h>
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070010#include <errno.h>
Patrick Delaunaybb3569d2020-04-27 15:29:58 +020011#include <generic-phy.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070013#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060014#include <memalign.h>
Stephen Warren79beb282015-03-24 20:07:35 -060015#include <phys2bus.h>
Patrick Delaunay9225f3e2020-04-27 15:29:59 +020016#include <usb.h>
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070017#include <usbroothubdes.h>
Mateusz Kulikowski2765f1e2016-01-23 11:54:30 +010018#include <wait_bit.h>
Simon Glass274e0b02020-05-10 11:39:56 -060019#include <asm/cache.h>
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070020#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070021#include <dm/device_compat.h>
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
João Loureiroec76d072021-03-17 16:52:21 +010023#include <linux/usb/otg.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
Tom Rinia99817c2021-08-10 16:17:55 -040088#if (DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070089 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
Tom Rinia99817c2021-08-10 16:17:55 -040095#ifdef DWC2_ULPI_FS_LS
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -070096 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
Jonas Karlman4f634d62023-07-19 21:20:56 +0000196 ret = regulator_set_enable_if_allowed(priv->vbus_supply, true);
197 if (ret && ret != -ENOSYS) {
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
Jonas Karlman4f634d62023-07-19 21:20:56 +0000210 ret = regulator_set_enable_if_allowed(priv->vbus_supply, false);
211 if (ret && ret != -ENOSYS) {
212 dev_err(dev, "Error disabling vbus supply\n");
213 return ret;
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100214 }
215
216 return 0;
217}
Kever Yang327c24d2017-03-10 12:05:14 +0800218#else
219static int dwc_vbus_supply_init(struct udevice *dev)
220{
221 return 0;
222}
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100223
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100224#if CONFIG_IS_ENABLED(DM_USB)
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100225static int dwc_vbus_supply_exit(struct udevice *dev)
226{
227 return 0;
228}
Kever Yang327c24d2017-03-10 12:05:14 +0800229#endif
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +0100230#endif
Kever Yang327c24d2017-03-10 12:05:14 +0800231
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700232/*
233 * This function initializes the DWC_otg controller registers for
234 * host mode.
235 *
236 * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
237 * request queues. Host channels are reset to ensure that they are ready for
238 * performing transfers.
239 *
Kever Yang327c24d2017-03-10 12:05:14 +0800240 * @param dev USB Device (NULL if driver model is not being used)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700241 * @param regs Programming view of DWC_otg controller
242 *
243 */
Kever Yang327c24d2017-03-10 12:05:14 +0800244static void dwc_otg_core_host_init(struct udevice *dev,
245 struct dwc2_core_regs *regs)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700246{
247 uint32_t nptxfifosize = 0;
248 uint32_t ptxfifosize = 0;
249 uint32_t hprt0 = 0;
250 int i, ret, num_channels;
251
252 /* Restart the Phy Clock */
253 writel(0, &regs->pcgcctl);
254
255 /* Initialize Host Configuration Register */
256 init_fslspclksel(regs);
Tom Rinia99817c2021-08-10 16:17:55 -0400257#ifdef DWC2_DFLT_SPEED_FULL
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700258 setbits_le32(&regs->host_regs.hcfg, DWC2_HCFG_FSLSSUPP);
259#endif
260
261 /* Configure data FIFO sizes */
Tom Rinia99817c2021-08-10 16:17:55 -0400262#ifdef DWC2_ENABLE_DYNAMIC_FIFO
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700263 if (readl(&regs->ghwcfg2) & DWC2_HWCFG2_DYNAMIC_FIFO) {
264 /* Rx FIFO */
Tom Rinia99817c2021-08-10 16:17:55 -0400265 writel(DWC2_HOST_RX_FIFO_SIZE, &regs->grxfsiz);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700266
267 /* Non-periodic Tx FIFO */
Tom Rinia99817c2021-08-10 16:17:55 -0400268 nptxfifosize |= DWC2_HOST_NPERIO_TX_FIFO_SIZE <<
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700269 DWC2_FIFOSIZE_DEPTH_OFFSET;
Tom Rinia99817c2021-08-10 16:17:55 -0400270 nptxfifosize |= DWC2_HOST_RX_FIFO_SIZE <<
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700271 DWC2_FIFOSIZE_STARTADDR_OFFSET;
272 writel(nptxfifosize, &regs->gnptxfsiz);
273
274 /* Periodic Tx FIFO */
Tom Rinia99817c2021-08-10 16:17:55 -0400275 ptxfifosize |= DWC2_HOST_PERIO_TX_FIFO_SIZE <<
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700276 DWC2_FIFOSIZE_DEPTH_OFFSET;
Tom Rinia99817c2021-08-10 16:17:55 -0400277 ptxfifosize |= (DWC2_HOST_RX_FIFO_SIZE +
278 DWC2_HOST_NPERIO_TX_FIFO_SIZE) <<
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700279 DWC2_FIFOSIZE_STARTADDR_OFFSET;
280 writel(ptxfifosize, &regs->hptxfsiz);
281 }
282#endif
283
284 /* Clear Host Set HNP Enable in the OTG Control Register */
285 clrbits_le32(&regs->gotgctl, DWC2_GOTGCTL_HSTSETHNPEN);
286
287 /* Make sure the FIFOs are flushed. */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400288 dwc_otg_flush_tx_fifo(dev, regs, 0x10); /* All Tx FIFOs */
289 dwc_otg_flush_rx_fifo(dev, regs);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700290
291 /* Flush out any leftover queued requests. */
292 num_channels = readl(&regs->ghwcfg2);
293 num_channels &= DWC2_HWCFG2_NUM_HOST_CHAN_MASK;
294 num_channels >>= DWC2_HWCFG2_NUM_HOST_CHAN_OFFSET;
295 num_channels += 1;
296
297 for (i = 0; i < num_channels; i++)
298 clrsetbits_le32(&regs->hc_regs[i].hcchar,
299 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_EPDIR,
300 DWC2_HCCHAR_CHDIS);
301
302 /* Halt all channels to put them into a known state. */
303 for (i = 0; i < num_channels; i++) {
304 clrsetbits_le32(&regs->hc_regs[i].hcchar,
305 DWC2_HCCHAR_EPDIR,
306 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100307 ret = wait_for_bit_le32(&regs->hc_regs[i].hcchar,
308 DWC2_HCCHAR_CHEN, false, 1000, false);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700309 if (ret)
Sean Anderson62eff9b2020-09-15 10:45:15 -0400310 dev_info(dev, "%s: Timeout!\n", __func__);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700311 }
312
313 /* Turn on the vbus power. */
314 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST) {
Teik Heng Chong9715f3c2023-06-21 11:13:58 +0800315 hprt0 = readl(&regs->hprt0) & ~DWC2_HPRT0_W1C_MASK;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700316 if (!(hprt0 & DWC2_HPRT0_PRTPWR)) {
317 hprt0 |= DWC2_HPRT0_PRTPWR;
318 writel(hprt0, &regs->hprt0);
319 }
320 }
Kever Yang327c24d2017-03-10 12:05:14 +0800321
322 if (dev)
323 dwc_vbus_supply_init(dev);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700324}
325
326/*
327 * This function initializes the DWC_otg controller registers and
328 * prepares the core for device mode or host mode operation.
329 *
330 * @param regs Programming view of the DWC_otg controller
331 */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400332static void dwc_otg_core_init(struct udevice *dev)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700333{
Sean Anderson62eff9b2020-09-15 10:45:15 -0400334 struct dwc2_priv *priv = dev_get_priv(dev);
Marek Vasut36fc5692016-04-27 14:53:33 +0200335 struct dwc2_core_regs *regs = priv->regs;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700336 uint32_t ahbcfg = 0;
337 uint32_t usbcfg = 0;
Tom Rinia99817c2021-08-10 16:17:55 -0400338 uint8_t brst_sz = DWC2_DMA_BURST_SIZE;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700339
340 /* Common Initialization */
341 usbcfg = readl(&regs->gusbcfg);
342
343 /* Program the ULPI External VBUS bit if needed */
Marek Vasut39209492016-04-27 14:55:57 +0200344 if (priv->ext_vbus) {
Marek Vasut43db5a62016-04-27 14:58:49 +0200345 usbcfg |= DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
346 if (!priv->oc_disable) {
347 usbcfg |= DWC2_GUSBCFG_ULPI_INT_VBUS_INDICATOR |
348 DWC2_GUSBCFG_INDICATOR_PASSTHROUGH;
349 }
Marek Vasut39209492016-04-27 14:55:57 +0200350 } else {
351 usbcfg &= ~DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
352 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700353
354 /* Set external TS Dline pulsing */
Tom Rinia99817c2021-08-10 16:17:55 -0400355#ifdef DWC2_TS_DLINE
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700356 usbcfg |= DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
357#else
358 usbcfg &= ~DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
359#endif
360 writel(usbcfg, &regs->gusbcfg);
361
362 /* Reset the Controller */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400363 dwc_otg_core_reset(dev, regs);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700364
365 /*
366 * This programming sequence needs to happen in FS mode before
367 * any other programming occurs
368 */
Tom Rinia99817c2021-08-10 16:17:55 -0400369#if defined(DWC2_DFLT_SPEED_FULL) && \
370 (DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700371 /* If FS mode with FS PHY */
372 setbits_le32(&regs->gusbcfg, DWC2_GUSBCFG_PHYSEL);
373
374 /* Reset after a PHY select */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400375 dwc_otg_core_reset(dev, regs);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700376
377 /*
378 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS.
379 * Also do this on HNP Dev/Host mode switches (done in dev_init
380 * and host_init).
381 */
382 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
383 init_fslspclksel(regs);
384
Tom Rinia99817c2021-08-10 16:17:55 -0400385#ifdef DWC2_I2C_ENABLE
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700386 /* Program GUSBCFG.OtgUtmifsSel to I2C */
387 setbits_le32(&regs->gusbcfg, DWC2_GUSBCFG_OTGUTMIFSSEL);
388
389 /* Program GI2CCTL.I2CEn */
390 clrsetbits_le32(&regs->gi2cctl, DWC2_GI2CCTL_I2CEN |
391 DWC2_GI2CCTL_I2CDEVADDR_MASK,
392 1 << DWC2_GI2CCTL_I2CDEVADDR_OFFSET);
393 setbits_le32(&regs->gi2cctl, DWC2_GI2CCTL_I2CEN);
394#endif
395
396#else
397 /* High speed PHY. */
398
399 /*
400 * HS PHY parameters. These parameters are preserved during
401 * soft reset so only program the first time. Do a soft reset
402 * immediately after setting phyif.
403 */
404 usbcfg &= ~(DWC2_GUSBCFG_ULPI_UTMI_SEL | DWC2_GUSBCFG_PHYIF);
Tom Rinia99817c2021-08-10 16:17:55 -0400405 usbcfg |= DWC2_PHY_TYPE << DWC2_GUSBCFG_ULPI_UTMI_SEL_OFFSET;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700406
407 if (usbcfg & DWC2_GUSBCFG_ULPI_UTMI_SEL) { /* ULPI interface */
Tom Rinia99817c2021-08-10 16:17:55 -0400408#ifdef DWC2_PHY_ULPI_DDR
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700409 usbcfg |= DWC2_GUSBCFG_DDRSEL;
410#else
411 usbcfg &= ~DWC2_GUSBCFG_DDRSEL;
412#endif
413 } else { /* UTMI+ interface */
Tom Rinia99817c2021-08-10 16:17:55 -0400414#if (DWC2_UTMI_WIDTH == 16)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700415 usbcfg |= DWC2_GUSBCFG_PHYIF;
416#endif
417 }
418
419 writel(usbcfg, &regs->gusbcfg);
420
421 /* Reset after setting the PHY parameters */
Sean Anderson62eff9b2020-09-15 10:45:15 -0400422 dwc_otg_core_reset(dev, regs);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700423#endif
424
425 usbcfg = readl(&regs->gusbcfg);
426 usbcfg &= ~(DWC2_GUSBCFG_ULPI_FSLS | DWC2_GUSBCFG_ULPI_CLK_SUS_M);
Tom Rinia99817c2021-08-10 16:17:55 -0400427#ifdef DWC2_ULPI_FS_LS
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700428 uint32_t hwcfg2 = readl(&regs->ghwcfg2);
429 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
430 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
431 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
432 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
433 if (hval == 2 && fval == 1) {
434 usbcfg |= DWC2_GUSBCFG_ULPI_FSLS;
435 usbcfg |= DWC2_GUSBCFG_ULPI_CLK_SUS_M;
436 }
437#endif
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800438 if (priv->hnp_srp_disable)
439 usbcfg |= DWC2_GUSBCFG_FORCEHOSTMODE;
440
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700441 writel(usbcfg, &regs->gusbcfg);
442
443 /* Program the GAHBCFG Register. */
444 switch (readl(&regs->ghwcfg2) & DWC2_HWCFG2_ARCHITECTURE_MASK) {
445 case DWC2_HWCFG2_ARCHITECTURE_SLAVE_ONLY:
446 break;
447 case DWC2_HWCFG2_ARCHITECTURE_EXT_DMA:
448 while (brst_sz > 1) {
449 ahbcfg |= ahbcfg + (1 << DWC2_GAHBCFG_HBURSTLEN_OFFSET);
450 ahbcfg &= DWC2_GAHBCFG_HBURSTLEN_MASK;
451 brst_sz >>= 1;
452 }
453
Tom Rinia99817c2021-08-10 16:17:55 -0400454#ifdef DWC2_DMA_ENABLE
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700455 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
456#endif
457 break;
458
459 case DWC2_HWCFG2_ARCHITECTURE_INT_DMA:
460 ahbcfg |= DWC2_GAHBCFG_HBURSTLEN_INCR4;
Tom Rinia99817c2021-08-10 16:17:55 -0400461#ifdef DWC2_DMA_ENABLE
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700462 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
463#endif
464 break;
465 }
466
467 writel(ahbcfg, &regs->gahbcfg);
468
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800469 /* Program the capabilities in GUSBCFG Register */
470 usbcfg = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700471
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800472 if (!priv->hnp_srp_disable)
473 usbcfg |= DWC2_GUSBCFG_HNPCAP | DWC2_GUSBCFG_SRPCAP;
Tom Rinia99817c2021-08-10 16:17:55 -0400474#ifdef DWC2_IC_USB_CAP
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800475 usbcfg |= DWC2_GUSBCFG_IC_USB_CAP;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700476#endif
Meng Dongyangcc3fe062017-06-08 15:34:20 +0800477
478 setbits_le32(&regs->gusbcfg, usbcfg);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700479}
480
481/*
482 * Prepares a host channel for transferring packets to/from a specific
483 * endpoint. The HCCHARn register is set up with the characteristics specified
484 * in _hc. Host channel interrupts that may need to be serviced while this
485 * transfer is in progress are enabled.
486 *
487 * @param regs Programming view of DWC_otg controller
488 * @param hc Information needed to initialize the host channel
489 */
490static void dwc_otg_hc_init(struct dwc2_core_regs *regs, uint8_t hc_num,
Stephen Warrendead8db2015-04-10 21:05:21 -0600491 struct usb_device *dev, uint8_t dev_addr, uint8_t ep_num,
492 uint8_t ep_is_in, uint8_t ep_type, uint16_t max_packet)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700493{
494 struct dwc2_hc_regs *hc_regs = &regs->hc_regs[hc_num];
Stephen Warrendead8db2015-04-10 21:05:21 -0600495 uint32_t hcchar = (dev_addr << DWC2_HCCHAR_DEVADDR_OFFSET) |
496 (ep_num << DWC2_HCCHAR_EPNUM_OFFSET) |
497 (ep_is_in << DWC2_HCCHAR_EPDIR_OFFSET) |
498 (ep_type << DWC2_HCCHAR_EPTYPE_OFFSET) |
499 (max_packet << DWC2_HCCHAR_MPS_OFFSET);
500
501 if (dev->speed == USB_SPEED_LOW)
502 hcchar |= DWC2_HCCHAR_LSPDDEV;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700503
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700504 /*
505 * Program the HCCHARn register with the endpoint characteristics
506 * for the current transfer.
507 */
508 writel(hcchar, &hc_regs->hcchar);
509
Stefan Brüns2e194e22016-01-17 04:09:54 +0100510 /* Program the HCSPLIT register, default to no SPLIT */
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700511 writel(0, &hc_regs->hcsplt);
512}
513
Stefan Brüns2e194e22016-01-17 04:09:54 +0100514static void dwc_otg_hc_init_split(struct dwc2_hc_regs *hc_regs,
515 uint8_t hub_devnum, uint8_t hub_port)
516{
517 uint32_t hcsplt = 0;
518
519 hcsplt = DWC2_HCSPLT_SPLTENA;
520 hcsplt |= hub_devnum << DWC2_HCSPLT_HUBADDR_OFFSET;
521 hcsplt |= hub_port << DWC2_HCSPLT_PRTADDR_OFFSET;
522
523 /* Program the HCSPLIT register for SPLITs */
524 writel(hcsplt, &hc_regs->hcsplt);
525}
526
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700527/*
528 * DWC2 to USB API interface
529 */
530/* Direction: In ; Request: Status */
Simon Glasse3c23a02015-07-07 20:53:36 -0600531static int dwc_otg_submit_rh_msg_in_status(struct dwc2_core_regs *regs,
532 struct usb_device *dev, void *buffer,
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700533 int txlen, struct devrequest *cmd)
534{
535 uint32_t hprt0 = 0;
536 uint32_t port_status = 0;
537 uint32_t port_change = 0;
538 int len = 0;
539 int stat = 0;
540
541 switch (cmd->requesttype & ~USB_DIR_IN) {
542 case 0:
543 *(uint16_t *)buffer = cpu_to_le16(1);
544 len = 2;
545 break;
546 case USB_RECIP_INTERFACE:
547 case USB_RECIP_ENDPOINT:
548 *(uint16_t *)buffer = cpu_to_le16(0);
549 len = 2;
550 break;
551 case USB_TYPE_CLASS:
552 *(uint32_t *)buffer = cpu_to_le32(0);
553 len = 4;
554 break;
555 case USB_RECIP_OTHER | USB_TYPE_CLASS:
556 hprt0 = readl(&regs->hprt0);
557 if (hprt0 & DWC2_HPRT0_PRTCONNSTS)
558 port_status |= USB_PORT_STAT_CONNECTION;
559 if (hprt0 & DWC2_HPRT0_PRTENA)
560 port_status |= USB_PORT_STAT_ENABLE;
561 if (hprt0 & DWC2_HPRT0_PRTSUSP)
562 port_status |= USB_PORT_STAT_SUSPEND;
563 if (hprt0 & DWC2_HPRT0_PRTOVRCURRACT)
564 port_status |= USB_PORT_STAT_OVERCURRENT;
565 if (hprt0 & DWC2_HPRT0_PRTRST)
566 port_status |= USB_PORT_STAT_RESET;
567 if (hprt0 & DWC2_HPRT0_PRTPWR)
568 port_status |= USB_PORT_STAT_POWER;
569
Stephen Warrend3388f82015-03-27 21:55:38 -0600570 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) == DWC2_HPRT0_PRTSPD_LOW)
571 port_status |= USB_PORT_STAT_LOW_SPEED;
572 else if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
573 DWC2_HPRT0_PRTSPD_HIGH)
574 port_status |= USB_PORT_STAT_HIGH_SPEED;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700575
576 if (hprt0 & DWC2_HPRT0_PRTENCHNG)
577 port_change |= USB_PORT_STAT_C_ENABLE;
578 if (hprt0 & DWC2_HPRT0_PRTCONNDET)
579 port_change |= USB_PORT_STAT_C_CONNECTION;
580 if (hprt0 & DWC2_HPRT0_PRTOVRCURRCHNG)
581 port_change |= USB_PORT_STAT_C_OVERCURRENT;
582
583 *(uint32_t *)buffer = cpu_to_le32(port_status |
584 (port_change << 16));
585 len = 4;
586 break;
587 default:
588 puts("unsupported root hub command\n");
589 stat = USB_ST_STALLED;
590 }
591
592 dev->act_len = min(len, txlen);
593 dev->status = stat;
594
595 return stat;
596}
597
598/* Direction: In ; Request: Descriptor */
599static int dwc_otg_submit_rh_msg_in_descriptor(struct usb_device *dev,
600 void *buffer, int txlen,
601 struct devrequest *cmd)
602{
603 unsigned char data[32];
604 uint32_t dsc;
605 int len = 0;
606 int stat = 0;
607 uint16_t wValue = cpu_to_le16(cmd->value);
608 uint16_t wLength = cpu_to_le16(cmd->length);
609
610 switch (cmd->requesttype & ~USB_DIR_IN) {
611 case 0:
612 switch (wValue & 0xff00) {
613 case 0x0100: /* device descriptor */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900614 len = min3(txlen, (int)sizeof(root_hub_dev_des), (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700615 memcpy(buffer, root_hub_dev_des, len);
616 break;
617 case 0x0200: /* configuration descriptor */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900618 len = min3(txlen, (int)sizeof(root_hub_config_des), (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700619 memcpy(buffer, root_hub_config_des, len);
620 break;
621 case 0x0300: /* string descriptors */
622 switch (wValue & 0xff) {
623 case 0x00:
Masahiro Yamadadb204642014-11-07 03:03:31 +0900624 len = min3(txlen, (int)sizeof(root_hub_str_index0),
625 (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700626 memcpy(buffer, root_hub_str_index0, len);
627 break;
628 case 0x01:
Masahiro Yamadadb204642014-11-07 03:03:31 +0900629 len = min3(txlen, (int)sizeof(root_hub_str_index1),
630 (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700631 memcpy(buffer, root_hub_str_index1, len);
632 break;
633 }
634 break;
635 default:
636 stat = USB_ST_STALLED;
637 }
638 break;
639
640 case USB_TYPE_CLASS:
641 /* Root port config, set 1 port and nothing else. */
642 dsc = 0x00000001;
643
644 data[0] = 9; /* min length; */
645 data[1] = 0x29;
646 data[2] = dsc & RH_A_NDP;
647 data[3] = 0;
648 if (dsc & RH_A_PSM)
649 data[3] |= 0x1;
650 if (dsc & RH_A_NOCP)
651 data[3] |= 0x10;
652 else if (dsc & RH_A_OCPM)
653 data[3] |= 0x8;
654
655 /* corresponds to data[4-7] */
656 data[5] = (dsc & RH_A_POTPGT) >> 24;
657 data[7] = dsc & RH_B_DR;
658 if (data[2] < 7) {
659 data[8] = 0xff;
660 } else {
661 data[0] += 2;
662 data[8] = (dsc & RH_B_DR) >> 8;
663 data[9] = 0xff;
664 data[10] = data[9];
665 }
666
Masahiro Yamadadb204642014-11-07 03:03:31 +0900667 len = min3(txlen, (int)data[0], (int)wLength);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700668 memcpy(buffer, data, len);
669 break;
670 default:
671 puts("unsupported root hub command\n");
672 stat = USB_ST_STALLED;
673 }
674
675 dev->act_len = min(len, txlen);
676 dev->status = stat;
677
678 return stat;
679}
680
681/* Direction: In ; Request: Configuration */
682static int dwc_otg_submit_rh_msg_in_configuration(struct usb_device *dev,
683 void *buffer, int txlen,
684 struct devrequest *cmd)
685{
686 int len = 0;
687 int stat = 0;
688
689 switch (cmd->requesttype & ~USB_DIR_IN) {
690 case 0:
691 *(uint8_t *)buffer = 0x01;
692 len = 1;
693 break;
694 default:
695 puts("unsupported root hub command\n");
696 stat = USB_ST_STALLED;
697 }
698
699 dev->act_len = min(len, txlen);
700 dev->status = stat;
701
702 return stat;
703}
704
705/* Direction: In */
Simon Glasse3c23a02015-07-07 20:53:36 -0600706static int dwc_otg_submit_rh_msg_in(struct dwc2_priv *priv,
707 struct usb_device *dev, void *buffer,
708 int txlen, struct devrequest *cmd)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700709{
710 switch (cmd->request) {
711 case USB_REQ_GET_STATUS:
Simon Glasse3c23a02015-07-07 20:53:36 -0600712 return dwc_otg_submit_rh_msg_in_status(priv->regs, dev, buffer,
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700713 txlen, cmd);
714 case USB_REQ_GET_DESCRIPTOR:
715 return dwc_otg_submit_rh_msg_in_descriptor(dev, buffer,
716 txlen, cmd);
717 case USB_REQ_GET_CONFIGURATION:
718 return dwc_otg_submit_rh_msg_in_configuration(dev, buffer,
719 txlen, cmd);
720 default:
721 puts("unsupported root hub command\n");
722 return USB_ST_STALLED;
723 }
724}
725
726/* Direction: Out */
Simon Glasse3c23a02015-07-07 20:53:36 -0600727static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
728 struct usb_device *dev,
729 void *buffer, int txlen,
730 struct devrequest *cmd)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700731{
Simon Glasse3c23a02015-07-07 20:53:36 -0600732 struct dwc2_core_regs *regs = priv->regs;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700733 int len = 0;
734 int stat = 0;
735 uint16_t bmrtype_breq = cmd->requesttype | (cmd->request << 8);
736 uint16_t wValue = cpu_to_le16(cmd->value);
737
738 switch (bmrtype_breq & ~USB_DIR_IN) {
739 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_ENDPOINT:
740 case (USB_REQ_CLEAR_FEATURE << 8) | USB_TYPE_CLASS:
741 break;
742
743 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
744 switch (wValue) {
745 case USB_PORT_FEAT_C_CONNECTION:
Teik Heng Chong9715f3c2023-06-21 11:13:58 +0800746 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTCONNDET);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700747 break;
748 }
749 break;
750
751 case (USB_REQ_SET_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
752 switch (wValue) {
753 case USB_PORT_FEAT_SUSPEND:
754 break;
755
756 case USB_PORT_FEAT_RESET:
Teik Heng Chong9715f3c2023-06-21 11:13:58 +0800757 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTRST);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700758 mdelay(50);
Teik Heng Chong9715f3c2023-06-21 11:13:58 +0800759 clrbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK | DWC2_HPRT0_PRTRST);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700760 break;
761
762 case USB_PORT_FEAT_POWER:
Teik Heng Chong9715f3c2023-06-21 11:13:58 +0800763 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTRST);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700764 break;
765
766 case USB_PORT_FEAT_ENABLE:
767 break;
768 }
769 break;
770 case (USB_REQ_SET_ADDRESS << 8):
Simon Glasse3c23a02015-07-07 20:53:36 -0600771 priv->root_hub_devnum = wValue;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700772 break;
773 case (USB_REQ_SET_CONFIGURATION << 8):
774 break;
775 default:
776 puts("unsupported root hub command\n");
777 stat = USB_ST_STALLED;
778 }
779
780 len = min(len, txlen);
781
782 dev->act_len = len;
783 dev->status = stat;
784
785 return stat;
786}
787
Simon Glasse3c23a02015-07-07 20:53:36 -0600788static int dwc_otg_submit_rh_msg(struct dwc2_priv *priv, struct usb_device *dev,
789 unsigned long pipe, void *buffer, int txlen,
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700790 struct devrequest *cmd)
791{
792 int stat = 0;
793
794 if (usb_pipeint(pipe)) {
795 puts("Root-Hub submit IRQ: NOT implemented\n");
796 return 0;
797 }
798
799 if (cmd->requesttype & USB_DIR_IN)
Simon Glasse3c23a02015-07-07 20:53:36 -0600800 stat = dwc_otg_submit_rh_msg_in(priv, dev, buffer, txlen, cmd);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700801 else
Simon Glasse3c23a02015-07-07 20:53:36 -0600802 stat = dwc_otg_submit_rh_msg_out(priv, dev, buffer, txlen, cmd);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700803
804 mdelay(1);
805
806 return stat;
807}
808
Stefan Brüns081dcc72016-01-23 01:42:25 +0100809int wait_for_chhltd(struct dwc2_hc_regs *hc_regs, uint32_t *sub, u8 *toggle)
Stephen Warren8a346662015-03-07 22:48:51 -0700810{
Stephen Warren8a346662015-03-07 22:48:51 -0700811 int ret;
812 uint32_t hcint, hctsiz;
813
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100814 ret = wait_for_bit_le32(&hc_regs->hcint, DWC2_HCINT_CHHLTD, true,
Christophe Kerello4edc9802018-03-15 18:00:31 +0100815 2000, false);
Stephen Warren8a346662015-03-07 22:48:51 -0700816 if (ret)
817 return ret;
818
819 hcint = readl(&hc_regs->hcint);
Stephen Warren8a346662015-03-07 22:48:51 -0700820 hctsiz = readl(&hc_regs->hctsiz);
821 *sub = (hctsiz & DWC2_HCTSIZ_XFERSIZE_MASK) >>
822 DWC2_HCTSIZ_XFERSIZE_OFFSET;
Stephen Warren9f80e742015-03-07 22:48:55 -0700823 *toggle = (hctsiz & DWC2_HCTSIZ_PID_MASK) >> DWC2_HCTSIZ_PID_OFFSET;
Stephen Warren8a346662015-03-07 22:48:51 -0700824
Stefan Brünsaa9506e2016-01-17 04:09:52 +0100825 debug("%s: HCINT=%08x sub=%u toggle=%d\n", __func__, hcint, *sub,
826 *toggle);
Stephen Warren8a346662015-03-07 22:48:51 -0700827
Stefan Brünsaa9506e2016-01-17 04:09:52 +0100828 if (hcint & DWC2_HCINT_XFERCOMP)
829 return 0;
830
831 if (hcint & (DWC2_HCINT_NAK | DWC2_HCINT_FRMOVRUN))
832 return -EAGAIN;
Stephen Warren8a346662015-03-07 22:48:51 -0700833
Stefan Brünsaa9506e2016-01-17 04:09:52 +0100834 debug("%s: Error (HCINT=%08x)\n", __func__, hcint);
835 return -EINVAL;
Stephen Warren8a346662015-03-07 22:48:51 -0700836}
837
Stephen Warren972ad642015-03-07 22:48:52 -0700838static int dwc2_eptype[] = {
839 DWC2_HCCHAR_EPTYPE_ISOC,
840 DWC2_HCCHAR_EPTYPE_INTR,
841 DWC2_HCCHAR_EPTYPE_CONTROL,
842 DWC2_HCCHAR_EPTYPE_BULK,
843};
844
Stefan Brüns2385db32016-01-17 04:09:53 +0100845static int transfer_chunk(struct dwc2_hc_regs *hc_regs, void *aligned_buffer,
Stefan Brüns081dcc72016-01-23 01:42:25 +0100846 u8 *pid, int in, void *buffer, int num_packets,
Stefan Brüns247241e2016-01-17 04:09:56 +0100847 int xfer_len, int *actual_len, int odd_frame)
Stefan Brüns2385db32016-01-17 04:09:53 +0100848{
849 int ret = 0;
850 uint32_t sub;
851
852 debug("%s: chunk: pid %d xfer_len %u pkts %u\n", __func__,
853 *pid, xfer_len, num_packets);
854
855 writel((xfer_len << DWC2_HCTSIZ_XFERSIZE_OFFSET) |
856 (num_packets << DWC2_HCTSIZ_PKTCNT_OFFSET) |
857 (*pid << DWC2_HCTSIZ_PID_OFFSET),
858 &hc_regs->hctsiz);
859
Eddie Cai408bdee2017-04-06 11:37:04 +0800860 if (xfer_len) {
861 if (in) {
862 invalidate_dcache_range(
863 (uintptr_t)aligned_buffer,
864 (uintptr_t)aligned_buffer +
865 roundup(xfer_len, ARCH_DMA_MINALIGN));
866 } else {
867 memcpy(aligned_buffer, buffer, xfer_len);
868 flush_dcache_range(
869 (uintptr_t)aligned_buffer,
870 (uintptr_t)aligned_buffer +
871 roundup(xfer_len, ARCH_DMA_MINALIGN));
872 }
Stefan Brüns2385db32016-01-17 04:09:53 +0100873 }
874
875 writel(phys_to_bus((unsigned long)aligned_buffer), &hc_regs->hcdma);
876
877 /* Clear old interrupt conditions for this host channel. */
878 writel(0x3fff, &hc_regs->hcint);
879
880 /* Set host channel enable after all other setup is complete. */
881 clrsetbits_le32(&hc_regs->hcchar, DWC2_HCCHAR_MULTICNT_MASK |
Stefan Brüns247241e2016-01-17 04:09:56 +0100882 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS |
883 DWC2_HCCHAR_ODDFRM,
Stefan Brüns2385db32016-01-17 04:09:53 +0100884 (1 << DWC2_HCCHAR_MULTICNT_OFFSET) |
Stefan Brüns247241e2016-01-17 04:09:56 +0100885 (odd_frame << DWC2_HCCHAR_ODDFRM_OFFSET) |
Stefan Brüns2385db32016-01-17 04:09:53 +0100886 DWC2_HCCHAR_CHEN);
887
888 ret = wait_for_chhltd(hc_regs, &sub, pid);
889 if (ret < 0)
890 return ret;
891
892 if (in) {
893 xfer_len -= sub;
894
895 invalidate_dcache_range((unsigned long)aligned_buffer,
896 (unsigned long)aligned_buffer +
897 roundup(xfer_len, ARCH_DMA_MINALIGN));
898
899 memcpy(buffer, aligned_buffer, xfer_len);
900 }
901 *actual_len = xfer_len;
902
903 return ret;
904}
905
Simon Glasse3c23a02015-07-07 20:53:36 -0600906int chunk_msg(struct dwc2_priv *priv, struct usb_device *dev,
Stefan Brüns081dcc72016-01-23 01:42:25 +0100907 unsigned long pipe, u8 *pid, int in, void *buffer, int len)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700908{
Simon Glasse3c23a02015-07-07 20:53:36 -0600909 struct dwc2_core_regs *regs = priv->regs;
Stephen Warren972ad642015-03-07 22:48:52 -0700910 struct dwc2_hc_regs *hc_regs = &regs->hc_regs[DWC2_HC_CHANNEL];
Stefan Brüns247241e2016-01-17 04:09:56 +0100911 struct dwc2_host_regs *host_regs = &regs->host_regs;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700912 int devnum = usb_pipedevice(pipe);
913 int ep = usb_pipeendpoint(pipe);
914 int max = usb_maxpacket(dev, pipe);
Stephen Warren972ad642015-03-07 22:48:52 -0700915 int eptype = dwc2_eptype[usb_pipetype(pipe)];
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700916 int done = 0;
Stephen Warren766fe412015-04-11 21:52:02 -0600917 int ret = 0;
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100918 int do_split = 0;
919 int complete_split = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700920 uint32_t xfer_len;
921 uint32_t num_packets;
922 int stop_transfer = 0;
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100923 uint32_t max_xfer_len;
Stefan Brüns247241e2016-01-17 04:09:56 +0100924 int ssplit_frame_num = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700925
Stephen Warren972ad642015-03-07 22:48:52 -0700926 debug("%s: msg: pipe %lx pid %d in %d len %d\n", __func__, pipe, *pid,
927 in, len);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700928
Tom Rinia99817c2021-08-10 16:17:55 -0400929 max_xfer_len = DWC2_MAX_PACKET_COUNT * max;
930 if (max_xfer_len > DWC2_MAX_TRANSFER_SIZE)
931 max_xfer_len = DWC2_MAX_TRANSFER_SIZE;
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100932 if (max_xfer_len > DWC2_DATA_BUF_SIZE)
933 max_xfer_len = DWC2_DATA_BUF_SIZE;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700934
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100935 /* Make sure that max_xfer_len is a multiple of max packet size. */
936 num_packets = max_xfer_len / max;
937 max_xfer_len = num_packets * max;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700938
Stefan Brüns2385db32016-01-17 04:09:53 +0100939 /* Initialize channel */
940 dwc_otg_hc_init(regs, DWC2_HC_CHANNEL, dev, devnum, ep, in,
941 eptype, max);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700942
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100943 /* Check if the target is a FS/LS device behind a HS hub */
944 if (dev->speed != USB_SPEED_HIGH) {
945 uint8_t hub_addr;
946 uint8_t hub_port;
947 uint32_t hprt0 = readl(&regs->hprt0);
948 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
949 DWC2_HPRT0_PRTSPD_HIGH) {
950 usb_find_usb2_hub_address_port(dev, &hub_addr,
951 &hub_port);
952 dwc_otg_hc_init_split(hc_regs, hub_addr, hub_port);
953
954 do_split = 1;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700955 num_packets = 1;
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100956 max_xfer_len = max;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700957 }
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100958 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700959
Stefan Brüns2385db32016-01-17 04:09:53 +0100960 do {
961 int actual_len = 0;
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100962 uint32_t hcint;
Stefan Brüns247241e2016-01-17 04:09:56 +0100963 int odd_frame = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700964 xfer_len = len - done;
Stephen Warren972ad642015-03-07 22:48:52 -0700965
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100966 if (xfer_len > max_xfer_len)
967 xfer_len = max_xfer_len;
968 else if (xfer_len > max)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700969 num_packets = (xfer_len + max - 1) / max;
Stefan Brüns575b0eb2016-01-17 04:09:51 +0100970 else
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700971 num_packets = 1;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700972
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100973 if (complete_split)
974 setbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
975 else if (do_split)
976 clrbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
Alexander Stein76fac502015-07-24 09:22:14 +0200977
Stefan Brüns247241e2016-01-17 04:09:56 +0100978 if (eptype == DWC2_HCCHAR_EPTYPE_INTR) {
979 int uframe_num = readl(&host_regs->hfnum);
980 if (!(uframe_num & 0x1))
981 odd_frame = 1;
Simon Glasse3c23a02015-07-07 20:53:36 -0600982 }
Stephen Warren7100da32015-03-08 11:08:13 -0600983
Stefan Brüns2385db32016-01-17 04:09:53 +0100984 ret = transfer_chunk(hc_regs, priv->aligned_buffer, pid,
985 in, (char *)buffer + done, num_packets,
Stefan Brüns247241e2016-01-17 04:09:56 +0100986 xfer_len, &actual_len, odd_frame);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -0700987
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100988 hcint = readl(&hc_regs->hcint);
989 if (complete_split) {
990 stop_transfer = 0;
Stefan Brüns247241e2016-01-17 04:09:56 +0100991 if (hcint & DWC2_HCINT_NYET) {
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100992 ret = 0;
Stefan Brüns247241e2016-01-17 04:09:56 +0100993 int frame_num = DWC2_HFNUM_MAX_FRNUM &
994 readl(&host_regs->hfnum);
995 if (((frame_num - ssplit_frame_num) &
996 DWC2_HFNUM_MAX_FRNUM) > 4)
997 ret = -EAGAIN;
998 } else
Stefan Brüns0c4b0652016-01-17 04:09:55 +0100999 complete_split = 0;
1000 } else if (do_split) {
1001 if (hcint & DWC2_HCINT_ACK) {
Stefan Brüns247241e2016-01-17 04:09:56 +01001002 ssplit_frame_num = DWC2_HFNUM_MAX_FRNUM &
1003 readl(&host_regs->hfnum);
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001004 ret = 0;
1005 complete_split = 1;
1006 }
1007 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001008
Stephen Warren766fe412015-04-11 21:52:02 -06001009 if (ret)
Stephen Warren8a346662015-03-07 22:48:51 -07001010 break;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001011
Stefan Brüns2385db32016-01-17 04:09:53 +01001012 if (actual_len < xfer_len)
1013 stop_transfer = 1;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001014
Stefan Brüns2385db32016-01-17 04:09:53 +01001015 done += actual_len;
Stephen Warren7100da32015-03-08 11:08:13 -06001016
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001017 /* Transactions are done when when either all data is transferred or
1018 * there is a short transfer. In case of a SPLIT make sure the CSPLIT
1019 * is executed.
1020 */
1021 } while (((done < len) && !stop_transfer) || complete_split);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001022
1023 writel(0, &hc_regs->hcintmsk);
1024 writel(0xFFFFFFFF, &hc_regs->hcint);
1025
1026 dev->status = 0;
1027 dev->act_len = done;
1028
Stephen Warren766fe412015-04-11 21:52:02 -06001029 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001030}
1031
Stephen Warren972ad642015-03-07 22:48:52 -07001032/* U-Boot USB transmission interface */
Simon Glasse3c23a02015-07-07 20:53:36 -06001033int _submit_bulk_msg(struct dwc2_priv *priv, struct usb_device *dev,
1034 unsigned long pipe, void *buffer, int len)
Stephen Warren972ad642015-03-07 22:48:52 -07001035{
1036 int devnum = usb_pipedevice(pipe);
1037 int ep = usb_pipeendpoint(pipe);
Stefan Brüns081dcc72016-01-23 01:42:25 +01001038 u8* pid;
Stephen Warren972ad642015-03-07 22:48:52 -07001039
Stefan Brüns081dcc72016-01-23 01:42:25 +01001040 if ((devnum >= MAX_DEVICE) || (devnum == priv->root_hub_devnum)) {
Stephen Warren972ad642015-03-07 22:48:52 -07001041 dev->status = 0;
1042 return -EINVAL;
1043 }
1044
Stefan Brüns081dcc72016-01-23 01:42:25 +01001045 if (usb_pipein(pipe))
1046 pid = &priv->in_data_toggle[devnum][ep];
1047 else
1048 pid = &priv->out_data_toggle[devnum][ep];
1049
1050 return chunk_msg(priv, dev, pipe, pid, usb_pipein(pipe), buffer, len);
Stephen Warren972ad642015-03-07 22:48:52 -07001051}
1052
Simon Glasse3c23a02015-07-07 20:53:36 -06001053static int _submit_control_msg(struct dwc2_priv *priv, struct usb_device *dev,
1054 unsigned long pipe, void *buffer, int len,
1055 struct devrequest *setup)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001056{
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001057 int devnum = usb_pipedevice(pipe);
Stefan Brüns081dcc72016-01-23 01:42:25 +01001058 int ret, act_len;
1059 u8 pid;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001060 /* For CONTROL endpoint pid should start with DATA1 */
1061 int status_direction;
1062
Simon Glasse3c23a02015-07-07 20:53:36 -06001063 if (devnum == priv->root_hub_devnum) {
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001064 dev->status = 0;
1065 dev->speed = USB_SPEED_HIGH;
Simon Glasse3c23a02015-07-07 20:53:36 -06001066 return dwc_otg_submit_rh_msg(priv, dev, pipe, buffer, len,
1067 setup);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001068 }
1069
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001070 /* SETUP stage */
Stephen Warren4db200e2015-03-07 22:48:53 -07001071 pid = DWC2_HC_PID_SETUP;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001072 do {
1073 ret = chunk_msg(priv, dev, pipe, &pid, 0, setup, 8);
1074 } while (ret == -EAGAIN);
Stephen Warren4db200e2015-03-07 22:48:53 -07001075 if (ret)
1076 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001077
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001078 /* DATA stage */
1079 act_len = 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001080 if (buffer) {
Stephen Warrenb0ad4a32015-03-07 22:48:54 -07001081 pid = DWC2_HC_PID_DATA1;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001082 do {
1083 ret = chunk_msg(priv, dev, pipe, &pid, usb_pipein(pipe),
1084 buffer, len);
1085 act_len += dev->act_len;
1086 buffer += dev->act_len;
1087 len -= dev->act_len;
1088 } while (ret == -EAGAIN);
Stephen Warren4db200e2015-03-07 22:48:53 -07001089 if (ret)
1090 return ret;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001091 status_direction = usb_pipeout(pipe);
1092 } else {
1093 /* No-data CONTROL always ends with an IN transaction */
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001094 status_direction = 1;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001095 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001096
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001097 /* STATUS stage */
Stephen Warren4db200e2015-03-07 22:48:53 -07001098 pid = DWC2_HC_PID_DATA1;
Stefan Brüns0c4b0652016-01-17 04:09:55 +01001099 do {
1100 ret = chunk_msg(priv, dev, pipe, &pid, status_direction,
1101 priv->status_buffer, 0);
1102 } while (ret == -EAGAIN);
Stephen Warren4db200e2015-03-07 22:48:53 -07001103 if (ret)
1104 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001105
Stephen Warren4db200e2015-03-07 22:48:53 -07001106 dev->act_len = act_len;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001107
Stephen Warren8a346662015-03-07 22:48:51 -07001108 return 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001109}
1110
Simon Glasse3c23a02015-07-07 20:53:36 -06001111int _submit_int_msg(struct dwc2_priv *priv, struct usb_device *dev,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001112 unsigned long pipe, void *buffer, int len, int interval,
1113 bool nonblock)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001114{
Stephen Warren766fe412015-04-11 21:52:02 -06001115 unsigned long timeout;
1116 int ret;
1117
Stephen Warrendf7b37d2015-04-10 21:05:22 -06001118 /* FIXME: what is interval? */
Stephen Warren766fe412015-04-11 21:52:02 -06001119
1120 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1121 for (;;) {
1122 if (get_timer(0) > timeout) {
Sean Anderson62eff9b2020-09-15 10:45:15 -04001123#if CONFIG_IS_ENABLED(DM_USB)
1124 dev_err(dev->dev,
1125 "Timeout poll on interrupt endpoint\n");
1126#else
1127 log_err("Timeout poll on interrupt endpoint\n");
1128#endif
Stephen Warren766fe412015-04-11 21:52:02 -06001129 return -ETIMEDOUT;
1130 }
Simon Glasse3c23a02015-07-07 20:53:36 -06001131 ret = _submit_bulk_msg(priv, dev, pipe, buffer, len);
Michal Suchanekc7a7ae52019-08-18 10:55:28 +02001132 if ((ret != -EAGAIN) || nonblock)
Stephen Warren766fe412015-04-11 21:52:02 -06001133 return ret;
1134 }
Ley Foon Tan23865562018-08-29 00:08:48 +08001135}
1136
1137static int dwc2_reset(struct udevice *dev)
1138{
1139 int ret;
1140 struct dwc2_priv *priv = dev_get_priv(dev);
1141
1142 ret = reset_get_bulk(dev, &priv->resets);
1143 if (ret) {
1144 dev_warn(dev, "Can't get reset: %d\n", ret);
1145 /* Return 0 if error due to !CONFIG_DM_RESET and reset
1146 * DT property is not present.
1147 */
1148 if (ret == -ENOENT || ret == -ENOTSUPP)
1149 return 0;
1150 else
1151 return ret;
1152 }
1153
Patrick Delaunay8bef1692020-04-27 15:30:00 +02001154 /* force reset to clear all IP register */
1155 reset_assert_bulk(&priv->resets);
Ley Foon Tan23865562018-08-29 00:08:48 +08001156 ret = reset_deassert_bulk(&priv->resets);
1157 if (ret) {
1158 reset_release_bulk(&priv->resets);
1159 dev_err(dev, "Failed to reset: %d\n", ret);
1160 return ret;
1161 }
1162
1163 return 0;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001164}
1165
Kever Yang327c24d2017-03-10 12:05:14 +08001166static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001167{
Simon Glasse3c23a02015-07-07 20:53:36 -06001168 struct dwc2_core_regs *regs = priv->regs;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001169 uint32_t snpsid;
1170 int i, j;
Ley Foon Tan23865562018-08-29 00:08:48 +08001171 int ret;
1172
1173 ret = dwc2_reset(dev);
1174 if (ret)
1175 return ret;
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001176
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001177 snpsid = readl(&regs->gsnpsid);
Patrice Chotarda259c1d2018-03-15 18:00:32 +01001178 dev_info(dev, "Core Release: %x.%03x\n",
1179 snpsid >> 12 & 0xf, snpsid & 0xfff);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001180
Peter Griffin79d657d2015-05-12 14:38:27 +01001181 if ((snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_2xx &&
1182 (snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_3xx) {
Patrice Chotarda259c1d2018-03-15 18:00:32 +01001183 dev_info(dev, "SNPSID invalid (not DWC2 OTG device): %08x\n",
1184 snpsid);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001185 return -ENODEV;
1186 }
1187
Tom Rinia99817c2021-08-10 16:17:55 -04001188#ifdef DWC2_PHY_ULPI_EXT_VBUS
Marek Vasut39209492016-04-27 14:55:57 +02001189 priv->ext_vbus = 1;
1190#else
1191 priv->ext_vbus = 0;
1192#endif
1193
Sean Anderson62eff9b2020-09-15 10:45:15 -04001194 dwc_otg_core_init(dev);
João Loureiroec76d072021-03-17 16:52:21 +01001195
1196 if (usb_get_dr_mode(dev_ofnode(dev)) == USB_DR_MODE_PERIPHERAL) {
1197 dev_dbg(dev, "USB device %s dr_mode set to %d. Skipping host_init.\n",
1198 dev->name, usb_get_dr_mode(dev_ofnode(dev)));
1199 } else {
1200 dwc_otg_core_host_init(dev, regs);
1201 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001202
Teik Heng Chong9715f3c2023-06-21 11:13:58 +08001203 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTRST);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001204 mdelay(50);
Teik Heng Chong9715f3c2023-06-21 11:13:58 +08001205 clrbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK | DWC2_HPRT0_PRTRST);
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001206
1207 for (i = 0; i < MAX_DEVICE; i++) {
Stefan Brüns081dcc72016-01-23 01:42:25 +01001208 for (j = 0; j < MAX_ENDPOINT; j++) {
1209 priv->in_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1210 priv->out_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1211 }
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001212 }
1213
Stefan Roesec526e832016-05-06 13:53:37 +02001214 /*
1215 * Add a 1 second delay here. This gives the host controller
1216 * a bit time before the comminucation with the USB devices
1217 * is started (the bus is scanned) and fixes the USB detection
1218 * problems with some problematic USB keys.
1219 */
1220 if (readl(&regs->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
1221 mdelay(1000);
1222
Patrick Delaunayce17fe12020-04-27 15:30:01 +02001223 printf("USB DWC2\n");
1224
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001225 return 0;
1226}
1227
Simon Glasse3c23a02015-07-07 20:53:36 -06001228static void dwc2_uninit_common(struct dwc2_core_regs *regs)
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001229{
1230 /* Put everything in reset. */
Teik Heng Chong9715f3c2023-06-21 11:13:58 +08001231 clrsetbits_le32(&regs->hprt0, DWC2_HPRT0_W1C_MASK, DWC2_HPRT0_PRTRST);
Simon Glasse3c23a02015-07-07 20:53:36 -06001232}
1233
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01001234#if !CONFIG_IS_ENABLED(DM_USB)
Simon Glasse3c23a02015-07-07 20:53:36 -06001235int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1236 int len, struct devrequest *setup)
1237{
1238 return _submit_control_msg(&local, dev, pipe, buffer, len, setup);
1239}
1240
1241int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1242 int len)
1243{
1244 return _submit_bulk_msg(&local, dev, pipe, buffer, len);
1245}
1246
1247int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001248 int len, int interval, bool nonblock)
Simon Glasse3c23a02015-07-07 20:53:36 -06001249{
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001250 return _submit_int_msg(&local, dev, pipe, buffer, len, interval,
1251 nonblock);
Simon Glasse3c23a02015-07-07 20:53:36 -06001252}
1253
1254/* U-Boot USB control interface */
1255int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1256{
1257 struct dwc2_priv *priv = &local;
1258
1259 memset(priv, '\0', sizeof(*priv));
1260 priv->root_hub_devnum = 0;
1261 priv->regs = (struct dwc2_core_regs *)CONFIG_USB_DWC2_REG_ADDR;
1262 priv->aligned_buffer = aligned_buffer_addr;
1263 priv->status_buffer = status_buffer_addr;
1264
1265 /* board-dependant init */
1266 if (board_usb_init(index, USB_INIT_HOST))
1267 return -1;
1268
Kever Yang327c24d2017-03-10 12:05:14 +08001269 return dwc2_init_common(NULL, priv);
Simon Glasse3c23a02015-07-07 20:53:36 -06001270}
1271
1272int usb_lowlevel_stop(int index)
1273{
1274 dwc2_uninit_common(local.regs);
1275
Oleksandr Tymoshenko7a881752014-02-01 21:51:25 -07001276 return 0;
1277}
Simon Glassa7ea72c2015-07-07 20:53:37 -06001278#endif
1279
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +01001280#if CONFIG_IS_ENABLED(DM_USB)
Simon Glassa7ea72c2015-07-07 20:53:37 -06001281static int dwc2_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1282 unsigned long pipe, void *buffer, int length,
1283 struct devrequest *setup)
1284{
1285 struct dwc2_priv *priv = dev_get_priv(dev);
1286
1287 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1288 dev->name, udev, udev->dev->name, udev->portnr);
1289
1290 return _submit_control_msg(priv, udev, pipe, buffer, length, setup);
1291}
1292
1293static int dwc2_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1294 unsigned long pipe, void *buffer, int length)
1295{
1296 struct dwc2_priv *priv = dev_get_priv(dev);
1297
1298 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1299
1300 return _submit_bulk_msg(priv, udev, pipe, buffer, length);
1301}
1302
1303static int dwc2_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1304 unsigned long pipe, void *buffer, int length,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001305 int interval, bool nonblock)
Simon Glassa7ea72c2015-07-07 20:53:37 -06001306{
1307 struct dwc2_priv *priv = dev_get_priv(dev);
1308
1309 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1310
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001311 return _submit_int_msg(priv, udev, pipe, buffer, length, interval,
1312 nonblock);
Simon Glassa7ea72c2015-07-07 20:53:37 -06001313}
1314
Simon Glassaad29ae2020-12-03 16:55:21 -07001315static int dwc2_usb_of_to_plat(struct udevice *dev)
Simon Glassa7ea72c2015-07-07 20:53:37 -06001316{
1317 struct dwc2_priv *priv = dev_get_priv(dev);
Simon Glassa7ea72c2015-07-07 20:53:37 -06001318
Sean Anderson62eff9b2020-09-15 10:45:15 -04001319 priv->regs = dev_read_addr_ptr(dev);
1320 if (!priv->regs)
Simon Glassa7ea72c2015-07-07 20:53:37 -06001321 return -EINVAL;
Simon Glassa7ea72c2015-07-07 20:53:37 -06001322
Meng Dongyang697a8bc2017-06-28 19:22:43 +08001323 priv->oc_disable = dev_read_bool(dev, "disable-over-current");
1324 priv->hnp_srp_disable = dev_read_bool(dev, "hnp-srp-disable");
Meng Dongyangcc3fe062017-06-08 15:34:20 +08001325
Simon Glassa7ea72c2015-07-07 20:53:37 -06001326 return 0;
1327}
1328
Patrick Delaunaybb3569d2020-04-27 15:29:58 +02001329static int dwc2_setup_phy(struct udevice *dev)
1330{
1331 struct dwc2_priv *priv = dev_get_priv(dev);
1332 int ret;
1333
1334 ret = generic_phy_get_by_index(dev, 0, &priv->phy);
1335 if (ret) {
1336 if (ret == -ENOENT)
1337 return 0; /* no PHY, nothing to do */
1338 dev_err(dev, "Failed to get USB PHY: %d.\n", ret);
1339 return ret;
1340 }
1341
1342 ret = generic_phy_init(&priv->phy);
1343 if (ret) {
1344 dev_dbg(dev, "Failed to init USB PHY: %d.\n", ret);
1345 return ret;
1346 }
1347
1348 ret = generic_phy_power_on(&priv->phy);
1349 if (ret) {
1350 dev_dbg(dev, "Failed to power on USB PHY: %d.\n", ret);
1351 generic_phy_exit(&priv->phy);
1352 return ret;
1353 }
1354
1355 return 0;
1356}
1357
1358static int dwc2_shutdown_phy(struct udevice *dev)
1359{
1360 struct dwc2_priv *priv = dev_get_priv(dev);
1361 int ret;
1362
1363 /* PHY is not valid when generic_phy_get_by_index() = -ENOENT */
1364 if (!generic_phy_valid(&priv->phy))
1365 return 0; /* no PHY, nothing to do */
1366
1367 ret = generic_phy_power_off(&priv->phy);
1368 if (ret) {
1369 dev_dbg(dev, "Failed to power off USB PHY: %d.\n", ret);
1370 return ret;
1371 }
1372
1373 ret = generic_phy_exit(&priv->phy);
1374 if (ret) {
1375 dev_dbg(dev, "Failed to power off USB PHY: %d.\n", ret);
1376 return ret;
1377 }
1378
1379 return 0;
1380}
1381
Patrick Delaunay9225f3e2020-04-27 15:29:59 +02001382static int dwc2_clk_init(struct udevice *dev)
1383{
1384 struct dwc2_priv *priv = dev_get_priv(dev);
1385 int ret;
1386
1387 ret = clk_get_bulk(dev, &priv->clks);
1388 if (ret == -ENOSYS || ret == -ENOENT)
1389 return 0;
1390 if (ret)
1391 return ret;
1392
1393 ret = clk_enable_bulk(&priv->clks);
1394 if (ret) {
1395 clk_release_bulk(&priv->clks);
1396 return ret;
1397 }
1398
1399 return 0;
1400}
1401
Simon Glassa7ea72c2015-07-07 20:53:37 -06001402static int dwc2_usb_probe(struct udevice *dev)
1403{
1404 struct dwc2_priv *priv = dev_get_priv(dev);
Marek Vasut1ea9ac62016-04-26 03:02:35 +02001405 struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
Patrick Delaunaybb3569d2020-04-27 15:29:58 +02001406 int ret;
Marek Vasut1ea9ac62016-04-26 03:02:35 +02001407
1408 bus_priv->desc_before_addr = true;
Simon Glassa7ea72c2015-07-07 20:53:37 -06001409
Patrick Delaunay9225f3e2020-04-27 15:29:59 +02001410 ret = dwc2_clk_init(dev);
1411 if (ret)
1412 return ret;
1413
Patrick Delaunaybb3569d2020-04-27 15:29:58 +02001414 ret = dwc2_setup_phy(dev);
1415 if (ret)
1416 return ret;
1417
Kever Yang327c24d2017-03-10 12:05:14 +08001418 return dwc2_init_common(dev, priv);
Simon Glassa7ea72c2015-07-07 20:53:37 -06001419}
1420
1421static int dwc2_usb_remove(struct udevice *dev)
1422{
1423 struct dwc2_priv *priv = dev_get_priv(dev);
Christophe Kerellof2a5a0b2018-03-15 18:00:30 +01001424 int ret;
1425
1426 ret = dwc_vbus_supply_exit(dev);
1427 if (ret)
1428 return ret;
Simon Glassa7ea72c2015-07-07 20:53:37 -06001429
Patrick Delaunaybb3569d2020-04-27 15:29:58 +02001430 ret = dwc2_shutdown_phy(dev);
1431 if (ret) {
1432 dev_dbg(dev, "Failed to shutdown USB PHY: %d.\n", ret);
1433 return ret;
1434 }
1435
Simon Glassa7ea72c2015-07-07 20:53:37 -06001436 dwc2_uninit_common(priv->regs);
1437
Ley Foon Tan23865562018-08-29 00:08:48 +08001438 reset_release_bulk(&priv->resets);
Patrick Delaunay9225f3e2020-04-27 15:29:59 +02001439 clk_disable_bulk(&priv->clks);
1440 clk_release_bulk(&priv->clks);
Ley Foon Tan23865562018-08-29 00:08:48 +08001441
Simon Glassa7ea72c2015-07-07 20:53:37 -06001442 return 0;
1443}
1444
1445struct dm_usb_ops dwc2_usb_ops = {
1446 .control = dwc2_submit_control_msg,
1447 .bulk = dwc2_submit_bulk_msg,
1448 .interrupt = dwc2_submit_int_msg,
1449};
1450
1451static const struct udevice_id dwc2_usb_ids[] = {
1452 { .compatible = "brcm,bcm2835-usb" },
Emmanuel Vadot80447002018-07-02 14:34:23 +02001453 { .compatible = "brcm,bcm2708-usb" },
Marek Vasutac4a35f2015-08-12 22:19:14 +02001454 { .compatible = "snps,dwc2" },
Simon Glassa7ea72c2015-07-07 20:53:37 -06001455 { }
1456};
1457
1458U_BOOT_DRIVER(usb_dwc2) = {
Marek Vasutaf83c782015-08-12 22:19:15 +02001459 .name = "dwc2_usb",
Simon Glassa7ea72c2015-07-07 20:53:37 -06001460 .id = UCLASS_USB,
1461 .of_match = dwc2_usb_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001462 .of_to_plat = dwc2_usb_of_to_plat,
Simon Glassa7ea72c2015-07-07 20:53:37 -06001463 .probe = dwc2_usb_probe,
1464 .remove = dwc2_usb_remove,
1465 .ops = &dwc2_usb_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001466 .priv_auto = sizeof(struct dwc2_priv),
Simon Glassa7ea72c2015-07-07 20:53:37 -06001467 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1468};
1469#endif