blob: 549cd3dd0bc2873f91f4cc99bedac018cccf2293 [file] [log] [blame]
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001/*
2 * core.c - DesignWare HS OTG Controller common routines
3 *
4 * Copyright (C) 2004-2013 Synopsys, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the above-listed copyright holders may not be used
16 * to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * ALTERNATIVELY, this software may be distributed under the terms of the
20 * GNU General Public License ("GPL") as published by the Free Software
21 * Foundation; either version 2 of the License, or (at your option) any
22 * later version.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37/*
38 * The Core code provides basic services for accessing and managing the
39 * DWC_otg hardware. These services are used by both the Host Controller
40 * Driver and the Peripheral Controller Driver.
41 */
42#include <linux/kernel.h>
43#include <linux/module.h>
44#include <linux/moduleparam.h>
45#include <linux/spinlock.h>
46#include <linux/interrupt.h>
47#include <linux/dma-mapping.h>
48#include <linux/delay.h>
49#include <linux/io.h>
50#include <linux/slab.h>
51#include <linux/usb.h>
52
53#include <linux/usb/hcd.h>
54#include <linux/usb/ch11.h>
55
56#include "core.h"
57#include "hcd.h"
58
59/**
60 * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
61 * used in both device and host modes
62 *
63 * @hsotg: Programming view of the DWC_otg controller
64 */
65static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
66{
67 u32 intmsk;
68
69 /* Clear any pending OTG Interrupts */
70 writel(0xffffffff, hsotg->regs + GOTGINT);
71
72 /* Clear any pending interrupts */
73 writel(0xffffffff, hsotg->regs + GINTSTS);
74
75 /* Enable the interrupts in the GINTMSK */
76 intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
77
78 if (hsotg->core_params->dma_enable <= 0)
79 intmsk |= GINTSTS_RXFLVL;
80
81 intmsk |= GINTSTS_CONIDSTSCHNG | GINTSTS_WKUPINT | GINTSTS_USBSUSP |
82 GINTSTS_SESSREQINT;
83
84 writel(intmsk, hsotg->regs + GINTMSK);
85}
86
87/*
88 * Initializes the FSLSPClkSel field of the HCFG register depending on the
89 * PHY type
90 */
91static void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
92{
93 u32 hs_phy_type = hsotg->hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK;
94 u32 fs_phy_type = hsotg->hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK;
95 u32 hcfg, val;
96
97 if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
98 fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
99 hsotg->core_params->ulpi_fs_ls > 0) ||
100 hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
101 /* Full speed PHY */
102 val = HCFG_FSLSPCLKSEL_48_MHZ;
103 } else {
104 /* High speed PHY running at full speed or high speed */
105 val = HCFG_FSLSPCLKSEL_30_60_MHZ;
106 }
107
108 dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
109 hcfg = readl(hsotg->regs + HCFG);
110 hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
111 hcfg |= val;
112 writel(hcfg, hsotg->regs + HCFG);
113}
114
115/*
116 * Do core a soft reset of the core. Be careful with this because it
117 * resets all the internal state machines of the core.
118 */
119static void dwc2_core_reset(struct dwc2_hsotg *hsotg)
120{
121 u32 greset;
122 int count = 0;
123
124 dev_vdbg(hsotg->dev, "%s()\n", __func__);
125
126 /* Wait for AHB master IDLE state */
127 do {
128 usleep_range(20000, 40000);
129 greset = readl(hsotg->regs + GRSTCTL);
130 if (++count > 50) {
131 dev_warn(hsotg->dev,
132 "%s() HANG! AHB Idle GRSTCTL=%0x\n",
133 __func__, greset);
134 return;
135 }
136 } while (!(greset & GRSTCTL_AHBIDLE));
137
138 /* Core Soft Reset */
139 count = 0;
140 greset |= GRSTCTL_CSFTRST;
141 writel(greset, hsotg->regs + GRSTCTL);
142 do {
143 usleep_range(20000, 40000);
144 greset = readl(hsotg->regs + GRSTCTL);
145 if (++count > 50) {
146 dev_warn(hsotg->dev,
147 "%s() HANG! Soft Reset GRSTCTL=%0x\n",
148 __func__, greset);
149 break;
150 }
151 } while (greset & GRSTCTL_CSFTRST);
152
153 /*
154 * NOTE: This long sleep is _very_ important, otherwise the core will
155 * not stay in host mode after a connector ID change!
156 */
157 usleep_range(150000, 200000);
158}
159
160static void dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
161{
162 u32 usbcfg, i2cctl;
163
164 /*
165 * core_init() is now called on every switch so only call the
166 * following for the first time through
167 */
168 if (select_phy) {
169 dev_dbg(hsotg->dev, "FS PHY selected\n");
170 usbcfg = readl(hsotg->regs + GUSBCFG);
171 usbcfg |= GUSBCFG_PHYSEL;
172 writel(usbcfg, hsotg->regs + GUSBCFG);
173
174 /* Reset after a PHY select */
175 dwc2_core_reset(hsotg);
176 }
177
178 /*
179 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
180 * do this on HNP Dev/Host mode switches (done in dev_init and
181 * host_init).
182 */
183 if (dwc2_is_host_mode(hsotg))
184 dwc2_init_fs_ls_pclk_sel(hsotg);
185
186 if (hsotg->core_params->i2c_enable > 0) {
187 dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
188
189 /* Program GUSBCFG.OtgUtmiFsSel to I2C */
190 usbcfg = readl(hsotg->regs + GUSBCFG);
191 usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
192 writel(usbcfg, hsotg->regs + GUSBCFG);
193
194 /* Program GI2CCTL.I2CEn */
195 i2cctl = readl(hsotg->regs + GI2CCTL);
196 i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
197 i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
198 i2cctl &= ~GI2CCTL_I2CEN;
199 writel(i2cctl, hsotg->regs + GI2CCTL);
200 i2cctl |= GI2CCTL_I2CEN;
201 writel(i2cctl, hsotg->regs + GI2CCTL);
202 }
203}
204
205static void dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
206{
207 u32 usbcfg;
208
209 if (!select_phy)
210 return;
211
212 usbcfg = readl(hsotg->regs + GUSBCFG);
213
214 /*
215 * HS PHY parameters. These parameters are preserved during soft reset
216 * so only program the first time. Do a soft reset immediately after
217 * setting phyif.
218 */
219 switch (hsotg->core_params->phy_type) {
220 case DWC2_PHY_TYPE_PARAM_ULPI:
221 /* ULPI interface */
222 dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
223 usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
224 usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
225 if (hsotg->core_params->phy_ulpi_ddr > 0)
226 usbcfg |= GUSBCFG_DDRSEL;
227 break;
228 case DWC2_PHY_TYPE_PARAM_UTMI:
229 /* UTMI+ interface */
230 dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
231 usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
232 if (hsotg->core_params->phy_utmi_width == 16)
233 usbcfg |= GUSBCFG_PHYIF16;
234 break;
235 default:
236 dev_err(hsotg->dev, "FS PHY selected at HS!\n");
237 break;
238 }
239
240 writel(usbcfg, hsotg->regs + GUSBCFG);
241
242 /* Reset after setting the PHY parameters */
243 dwc2_core_reset(hsotg);
244}
245
246static void dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
247{
248 u32 usbcfg, hs_phy_type, fs_phy_type;
249
250 if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL &&
251 hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
252 /* If FS mode with FS PHY */
253 dwc2_fs_phy_init(hsotg, select_phy);
254 } else {
255 /* High speed PHY */
256 dwc2_hs_phy_init(hsotg, select_phy);
257 }
258
259 hs_phy_type = hsotg->hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK;
260 fs_phy_type = hsotg->hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK;
261
262 if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
263 fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
264 hsotg->core_params->ulpi_fs_ls > 0) {
265 dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
266 usbcfg = readl(hsotg->regs + GUSBCFG);
267 usbcfg |= GUSBCFG_ULPI_FS_LS;
268 usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
269 writel(usbcfg, hsotg->regs + GUSBCFG);
270 } else {
271 usbcfg = readl(hsotg->regs + GUSBCFG);
272 usbcfg &= ~GUSBCFG_ULPI_FS_LS;
273 usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
274 writel(usbcfg, hsotg->regs + GUSBCFG);
275 }
276}
277
278static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
279{
280 u32 ahbcfg = 0;
281
282 switch (hsotg->hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) {
283 case GHWCFG2_EXT_DMA_ARCH:
284 dev_err(hsotg->dev, "External DMA Mode not supported\n");
285 return -EINVAL;
286
287 case GHWCFG2_INT_DMA_ARCH:
288 dev_dbg(hsotg->dev, "Internal DMA Mode\n");
289 /*
290 * Old value was GAHBCFG_HBSTLEN_INCR - done for
291 * Host mode ISOC in issue fix - vahrama
292 */
293 ahbcfg |= GAHBCFG_HBSTLEN_INCR4;
294 break;
295
296 case GHWCFG2_SLAVE_ONLY_ARCH:
297 default:
298 dev_dbg(hsotg->dev, "Slave Only Mode\n");
299 break;
300 }
301
302 dev_dbg(hsotg->dev, "dma_enable:%d dma_desc_enable:%d\n",
303 hsotg->core_params->dma_enable,
304 hsotg->core_params->dma_desc_enable);
305
306 if (hsotg->core_params->dma_enable > 0) {
307 if (hsotg->core_params->dma_desc_enable > 0)
308 dev_dbg(hsotg->dev, "Using Descriptor DMA mode\n");
309 else
310 dev_dbg(hsotg->dev, "Using Buffer DMA mode\n");
311 } else {
312 dev_dbg(hsotg->dev, "Using Slave mode\n");
313 hsotg->core_params->dma_desc_enable = 0;
314 }
315
316 if (hsotg->core_params->ahb_single > 0)
317 ahbcfg |= GAHBCFG_AHB_SINGLE;
318
319 if (hsotg->core_params->dma_enable > 0)
320 ahbcfg |= GAHBCFG_DMA_EN;
321
322 writel(ahbcfg, hsotg->regs + GAHBCFG);
323
324 return 0;
325}
326
327static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
328{
329 u32 usbcfg;
330
331 usbcfg = readl(hsotg->regs + GUSBCFG);
332 usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
333
334 switch (hsotg->hwcfg2 & GHWCFG2_OP_MODE_MASK) {
335 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
336 if (hsotg->core_params->otg_cap ==
337 DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
338 usbcfg |= GUSBCFG_HNPCAP;
339 if (hsotg->core_params->otg_cap !=
340 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
341 usbcfg |= GUSBCFG_SRPCAP;
342 break;
343
344 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
345 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
346 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
347 if (hsotg->core_params->otg_cap !=
348 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
349 usbcfg |= GUSBCFG_SRPCAP;
350 break;
351
352 case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
353 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
354 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
355 default:
356 break;
357 }
358
359 writel(usbcfg, hsotg->regs + GUSBCFG);
360}
361
362/**
363 * dwc2_core_init() - Initializes the DWC_otg controller registers and
364 * prepares the core for device mode or host mode operation
365 *
366 * @hsotg: Programming view of the DWC_otg controller
367 * @select_phy: If true then also set the Phy type
368 */
369int dwc2_core_init(struct dwc2_hsotg *hsotg, bool select_phy)
370{
371 u32 usbcfg, otgctl;
372 int retval;
373
374 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
375
376 usbcfg = readl(hsotg->regs + GUSBCFG);
377
378 /* Set ULPI External VBUS bit if needed */
379 usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
380 if (hsotg->core_params->phy_ulpi_ext_vbus ==
381 DWC2_PHY_ULPI_EXTERNAL_VBUS)
382 usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
383
384 /* Set external TS Dline pulsing bit if needed */
385 usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
386 if (hsotg->core_params->ts_dline > 0)
387 usbcfg |= GUSBCFG_TERMSELDLPULSE;
388
389 writel(usbcfg, hsotg->regs + GUSBCFG);
390
391 /* Reset the Controller */
392 dwc2_core_reset(hsotg);
393
394 dev_dbg(hsotg->dev, "num_dev_perio_in_ep=%d\n",
395 hsotg->hwcfg4 >> GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT &
396 GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK >>
397 GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT);
398
399 hsotg->total_fifo_size = hsotg->hwcfg3 >> GHWCFG3_DFIFO_DEPTH_SHIFT &
400 GHWCFG3_DFIFO_DEPTH_MASK >> GHWCFG3_DFIFO_DEPTH_SHIFT;
401 hsotg->rx_fifo_size = readl(hsotg->regs + GRXFSIZ);
402 hsotg->nperio_tx_fifo_size =
403 readl(hsotg->regs + GNPTXFSIZ) >> 16 & 0xffff;
404
405 dev_dbg(hsotg->dev, "Total FIFO SZ=%d\n", hsotg->total_fifo_size);
406 dev_dbg(hsotg->dev, "RxFIFO SZ=%d\n", hsotg->rx_fifo_size);
407 dev_dbg(hsotg->dev, "NP TxFIFO SZ=%d\n", hsotg->nperio_tx_fifo_size);
408
409 /*
410 * This needs to happen in FS mode before any other programming occurs
411 */
412 dwc2_phy_init(hsotg, select_phy);
413
414 /* Program the GAHBCFG Register */
415 retval = dwc2_gahbcfg_init(hsotg);
416 if (retval)
417 return retval;
418
419 /* Program the GUSBCFG register */
420 dwc2_gusbcfg_init(hsotg);
421
422 /* Program the GOTGCTL register */
423 otgctl = readl(hsotg->regs + GOTGCTL);
424 otgctl &= ~GOTGCTL_OTGVER;
425 if (hsotg->core_params->otg_ver > 0)
426 otgctl |= GOTGCTL_OTGVER;
427 writel(otgctl, hsotg->regs + GOTGCTL);
428 dev_dbg(hsotg->dev, "OTG VER PARAM: %d\n", hsotg->core_params->otg_ver);
429
430 /* Clear the SRP success bit for FS-I2c */
431 hsotg->srp_success = 0;
432
433 /* Enable common interrupts */
434 dwc2_enable_common_interrupts(hsotg);
435
436 /*
437 * Do device or host intialization based on mode during PCD and
438 * HCD initialization
439 */
440 if (dwc2_is_host_mode(hsotg)) {
441 dev_dbg(hsotg->dev, "Host Mode\n");
442 hsotg->op_state = OTG_STATE_A_HOST;
443 } else {
444 dev_dbg(hsotg->dev, "Device Mode\n");
445 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
446 }
447
448 return 0;
449}
450
451/**
452 * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
453 *
454 * @hsotg: Programming view of DWC_otg controller
455 */
456void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
457{
458 u32 intmsk;
459
460 dev_dbg(hsotg->dev, "%s()\n", __func__);
461
462 /* Disable all interrupts */
463 writel(0, hsotg->regs + GINTMSK);
464 writel(0, hsotg->regs + HAINTMSK);
465
466 /* Clear any pending interrupts */
467 writel(0xffffffff, hsotg->regs + GINTSTS);
468
469 /* Enable the common interrupts */
470 dwc2_enable_common_interrupts(hsotg);
471
472 /* Enable host mode interrupts without disturbing common interrupts */
473 intmsk = readl(hsotg->regs + GINTMSK);
474 intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
475 writel(intmsk, hsotg->regs + GINTMSK);
476}
477
478/**
479 * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
480 *
481 * @hsotg: Programming view of DWC_otg controller
482 */
483void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
484{
485 u32 intmsk = readl(hsotg->regs + GINTMSK);
486
487 /* Disable host mode interrupts without disturbing common interrupts */
488 intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
489 GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP);
490 writel(intmsk, hsotg->regs + GINTMSK);
491}
492
493static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
494{
495 struct dwc2_core_params *params = hsotg->core_params;
496 u32 rxfsiz, nptxfsiz, ptxfsiz, hptxfsiz, dfifocfg;
497
498 if (!(hsotg->hwcfg2 & GHWCFG2_DYNAMIC_FIFO) ||
499 !params->enable_dynamic_fifo)
500 return;
501
502 dev_dbg(hsotg->dev, "Total FIFO Size=%d\n", hsotg->total_fifo_size);
503 dev_dbg(hsotg->dev, "Rx FIFO Size=%d\n", params->host_rx_fifo_size);
504 dev_dbg(hsotg->dev, "NP Tx FIFO Size=%d\n",
505 params->host_nperio_tx_fifo_size);
506 dev_dbg(hsotg->dev, "P Tx FIFO Size=%d\n",
507 params->host_perio_tx_fifo_size);
508
509 /* Rx FIFO */
510 dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n",
511 readl(hsotg->regs + GRXFSIZ));
512 writel(params->host_rx_fifo_size, hsotg->regs + GRXFSIZ);
513 dev_dbg(hsotg->dev, "new grxfsiz=%08x\n", readl(hsotg->regs + GRXFSIZ));
514
515 /* Non-periodic Tx FIFO */
516 dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
517 readl(hsotg->regs + GNPTXFSIZ));
518 nptxfsiz = params->host_nperio_tx_fifo_size <<
519 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
520 nptxfsiz |= params->host_rx_fifo_size <<
521 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
522 writel(nptxfsiz, hsotg->regs + GNPTXFSIZ);
523 dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
524 readl(hsotg->regs + GNPTXFSIZ));
525
526 /* Periodic Tx FIFO */
527 dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
528 readl(hsotg->regs + HPTXFSIZ));
529 ptxfsiz = params->host_perio_tx_fifo_size <<
530 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
531 ptxfsiz |= (params->host_rx_fifo_size +
532 params->host_nperio_tx_fifo_size) <<
533 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
534 writel(ptxfsiz, hsotg->regs + HPTXFSIZ);
535 dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
536 readl(hsotg->regs + HPTXFSIZ));
537
538 if (hsotg->core_params->en_multiple_tx_fifo > 0 &&
539 hsotg->snpsid <= DWC2_CORE_REV_2_94a) {
540 /*
541 * Global DFIFOCFG calculation for Host mode -
542 * include RxFIFO, NPTXFIFO and HPTXFIFO
543 */
544 dfifocfg = readl(hsotg->regs + GDFIFOCFG);
545 rxfsiz = readl(hsotg->regs + GRXFSIZ) & 0x0000ffff;
546 nptxfsiz = readl(hsotg->regs + GNPTXFSIZ) >> 16 & 0xffff;
547 hptxfsiz = readl(hsotg->regs + HPTXFSIZ) >> 16 & 0xffff;
548 dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
549 dfifocfg |= (rxfsiz + nptxfsiz + hptxfsiz) <<
550 GDFIFOCFG_EPINFOBASE_SHIFT &
551 GDFIFOCFG_EPINFOBASE_MASK;
552 writel(dfifocfg, hsotg->regs + GDFIFOCFG);
553 }
554}
555
556/**
557 * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
558 * Host mode
559 *
560 * @hsotg: Programming view of DWC_otg controller
561 *
562 * This function flushes the Tx and Rx FIFOs and flushes any entries in the
563 * request queues. Host channels are reset to ensure that they are ready for
564 * performing transfers.
565 */
566void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
567{
568 u32 hcfg, hfir, otgctl;
569
570 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
571
572 /* Restart the Phy Clock */
573 writel(0, hsotg->regs + PCGCTL);
574
575 /* Initialize Host Configuration Register */
576 dwc2_init_fs_ls_pclk_sel(hsotg);
577 if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL) {
578 hcfg = readl(hsotg->regs + HCFG);
579 hcfg |= HCFG_FSLSSUPP;
580 writel(hcfg, hsotg->regs + HCFG);
581 }
582
583 /*
584 * This bit allows dynamic reloading of the HFIR register during
585 * runtime. This bit needs to be programmed during inital configuration
586 * and its value must not be changed during runtime.
587 */
588 if (hsotg->core_params->reload_ctl > 0) {
589 hfir = readl(hsotg->regs + HFIR);
590 hfir |= HFIR_RLDCTRL;
591 writel(hfir, hsotg->regs + HFIR);
592 }
593
594 if (hsotg->core_params->dma_desc_enable > 0) {
595 u32 op_mode = hsotg->hwcfg2 & GHWCFG2_OP_MODE_MASK;
596
597 if (hsotg->snpsid < DWC2_CORE_REV_2_90a ||
598 !(hsotg->hwcfg4 & GHWCFG4_DESC_DMA) ||
599 op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
600 op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
601 op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
602 dev_err(hsotg->dev,
603 "Hardware does not support descriptor DMA mode -\n");
604 dev_err(hsotg->dev,
605 "falling back to buffer DMA mode.\n");
606 hsotg->core_params->dma_desc_enable = 0;
607 } else {
608 hcfg = readl(hsotg->regs + HCFG);
609 hcfg |= HCFG_DESCDMA;
610 writel(hcfg, hsotg->regs + HCFG);
611 }
612 }
613
614 /* Configure data FIFO sizes */
615 dwc2_config_fifos(hsotg);
616
617 /* TODO - check this */
618 /* Clear Host Set HNP Enable in the OTG Control Register */
619 otgctl = readl(hsotg->regs + GOTGCTL);
620 otgctl &= ~GOTGCTL_HSTSETHNPEN;
621 writel(otgctl, hsotg->regs + GOTGCTL);
622
623 /* Make sure the FIFOs are flushed */
624 dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
625 dwc2_flush_rx_fifo(hsotg);
626
627 /* Clear Host Set HNP Enable in the OTG Control Register */
628 otgctl = readl(hsotg->regs + GOTGCTL);
629 otgctl &= ~GOTGCTL_HSTSETHNPEN;
630 writel(otgctl, hsotg->regs + GOTGCTL);
631
632 if (hsotg->core_params->dma_desc_enable <= 0) {
633 int num_channels, i;
634 u32 hcchar;
635
636 /* Flush out any leftover queued requests */
637 num_channels = hsotg->core_params->host_channels;
638 for (i = 0; i < num_channels; i++) {
639 hcchar = readl(hsotg->regs + HCCHAR(i));
640 hcchar &= ~HCCHAR_CHENA;
641 hcchar |= HCCHAR_CHDIS;
642 hcchar &= ~HCCHAR_EPDIR;
643 writel(hcchar, hsotg->regs + HCCHAR(i));
644 }
645
646 /* Halt all channels to put them into a known state */
647 for (i = 0; i < num_channels; i++) {
648 int count = 0;
649
650 hcchar = readl(hsotg->regs + HCCHAR(i));
651 hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
652 hcchar &= ~HCCHAR_EPDIR;
653 writel(hcchar, hsotg->regs + HCCHAR(i));
654 dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
655 __func__, i);
656 do {
657 hcchar = readl(hsotg->regs + HCCHAR(i));
658 if (++count > 1000) {
659 dev_err(hsotg->dev,
660 "Unable to clear enable on channel %d\n",
661 i);
662 break;
663 }
664 udelay(1);
665 } while (hcchar & HCCHAR_CHENA);
666 }
667 }
668
669 /* Turn on the vbus power */
670 dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
671 if (hsotg->op_state == OTG_STATE_A_HOST) {
672 u32 hprt0 = dwc2_read_hprt0(hsotg);
673
674 dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
675 !!(hprt0 & HPRT0_PWR));
676 if (!(hprt0 & HPRT0_PWR)) {
677 hprt0 |= HPRT0_PWR;
678 writel(hprt0, hsotg->regs + HPRT0);
679 }
680 }
681
682 dwc2_enable_host_interrupts(hsotg);
683}
684
685static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
686 struct dwc2_host_chan *chan)
687{
688 u32 hcintmsk = HCINTMSK_CHHLTD;
689
690 switch (chan->ep_type) {
691 case USB_ENDPOINT_XFER_CONTROL:
692 case USB_ENDPOINT_XFER_BULK:
693 dev_vdbg(hsotg->dev, "control/bulk\n");
694 hcintmsk |= HCINTMSK_XFERCOMPL;
695 hcintmsk |= HCINTMSK_STALL;
696 hcintmsk |= HCINTMSK_XACTERR;
697 hcintmsk |= HCINTMSK_DATATGLERR;
698 if (chan->ep_is_in) {
699 hcintmsk |= HCINTMSK_BBLERR;
700 } else {
701 hcintmsk |= HCINTMSK_NAK;
702 hcintmsk |= HCINTMSK_NYET;
703 if (chan->do_ping)
704 hcintmsk |= HCINTMSK_ACK;
705 }
706
707 if (chan->do_split) {
708 hcintmsk |= HCINTMSK_NAK;
709 if (chan->complete_split)
710 hcintmsk |= HCINTMSK_NYET;
711 else
712 hcintmsk |= HCINTMSK_ACK;
713 }
714
715 if (chan->error_state)
716 hcintmsk |= HCINTMSK_ACK;
717 break;
718
719 case USB_ENDPOINT_XFER_INT:
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200720 if (dbg_perio())
721 dev_vdbg(hsotg->dev, "intr\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700722 hcintmsk |= HCINTMSK_XFERCOMPL;
723 hcintmsk |= HCINTMSK_NAK;
724 hcintmsk |= HCINTMSK_STALL;
725 hcintmsk |= HCINTMSK_XACTERR;
726 hcintmsk |= HCINTMSK_DATATGLERR;
727 hcintmsk |= HCINTMSK_FRMOVRUN;
728
729 if (chan->ep_is_in)
730 hcintmsk |= HCINTMSK_BBLERR;
731 if (chan->error_state)
732 hcintmsk |= HCINTMSK_ACK;
733 if (chan->do_split) {
734 if (chan->complete_split)
735 hcintmsk |= HCINTMSK_NYET;
736 else
737 hcintmsk |= HCINTMSK_ACK;
738 }
739 break;
740
741 case USB_ENDPOINT_XFER_ISOC:
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200742 if (dbg_perio())
743 dev_vdbg(hsotg->dev, "isoc\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700744 hcintmsk |= HCINTMSK_XFERCOMPL;
745 hcintmsk |= HCINTMSK_FRMOVRUN;
746 hcintmsk |= HCINTMSK_ACK;
747
748 if (chan->ep_is_in) {
749 hcintmsk |= HCINTMSK_XACTERR;
750 hcintmsk |= HCINTMSK_BBLERR;
751 }
752 break;
753 default:
754 dev_err(hsotg->dev, "## Unknown EP type ##\n");
755 break;
756 }
757
758 writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200759 if (dbg_hc(chan))
760 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700761}
762
763static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
764 struct dwc2_host_chan *chan)
765{
766 u32 hcintmsk = HCINTMSK_CHHLTD;
767
768 /*
769 * For Descriptor DMA mode core halts the channel on AHB error.
770 * Interrupt is not required.
771 */
772 if (hsotg->core_params->dma_desc_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200773 if (dbg_hc(chan))
774 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700775 hcintmsk |= HCINTMSK_AHBERR;
776 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200777 if (dbg_hc(chan))
778 dev_vdbg(hsotg->dev, "desc DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700779 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
780 hcintmsk |= HCINTMSK_XFERCOMPL;
781 }
782
783 if (chan->error_state && !chan->do_split &&
784 chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200785 if (dbg_hc(chan))
786 dev_vdbg(hsotg->dev, "setting ACK\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700787 hcintmsk |= HCINTMSK_ACK;
788 if (chan->ep_is_in) {
789 hcintmsk |= HCINTMSK_DATATGLERR;
790 if (chan->ep_type != USB_ENDPOINT_XFER_INT)
791 hcintmsk |= HCINTMSK_NAK;
792 }
793 }
794
795 writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200796 if (dbg_hc(chan))
797 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700798}
799
800static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
801 struct dwc2_host_chan *chan)
802{
803 u32 intmsk;
804
805 if (hsotg->core_params->dma_enable > 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200806 if (dbg_hc(chan))
807 dev_vdbg(hsotg->dev, "DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700808 dwc2_hc_enable_dma_ints(hsotg, chan);
809 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200810 if (dbg_hc(chan))
811 dev_vdbg(hsotg->dev, "DMA disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700812 dwc2_hc_enable_slave_ints(hsotg, chan);
813 }
814
815 /* Enable the top level host channel interrupt */
816 intmsk = readl(hsotg->regs + HAINTMSK);
817 intmsk |= 1 << chan->hc_num;
818 writel(intmsk, hsotg->regs + HAINTMSK);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200819 if (dbg_hc(chan))
820 dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700821
822 /* Make sure host channel interrupts are enabled */
823 intmsk = readl(hsotg->regs + GINTMSK);
824 intmsk |= GINTSTS_HCHINT;
825 writel(intmsk, hsotg->regs + GINTMSK);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200826 if (dbg_hc(chan))
827 dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700828}
829
830/**
831 * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
832 * a specific endpoint
833 *
834 * @hsotg: Programming view of DWC_otg controller
835 * @chan: Information needed to initialize the host channel
836 *
837 * The HCCHARn register is set up with the characteristics specified in chan.
838 * Host channel interrupts that may need to be serviced while this transfer is
839 * in progress are enabled.
840 */
841void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
842{
843 u8 hc_num = chan->hc_num;
844 u32 hcintmsk;
845 u32 hcchar;
846 u32 hcsplt = 0;
847
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200848 if (dbg_hc(chan))
849 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700850
851 /* Clear old interrupt conditions for this host channel */
852 hcintmsk = 0xffffffff;
853 hcintmsk &= ~HCINTMSK_RESERVED14_31;
854 writel(hcintmsk, hsotg->regs + HCINT(hc_num));
855
856 /* Enable channel interrupts required for this transfer */
857 dwc2_hc_enable_ints(hsotg, chan);
858
859 /*
860 * Program the HCCHARn register with the endpoint characteristics for
861 * the current transfer
862 */
863 hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
864 hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
865 if (chan->ep_is_in)
866 hcchar |= HCCHAR_EPDIR;
867 if (chan->speed == USB_SPEED_LOW)
868 hcchar |= HCCHAR_LSPDDEV;
869 hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
870 hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
871 writel(hcchar, hsotg->regs + HCCHAR(hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200872 if (dbg_hc(chan)) {
873 dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
874 hc_num, hcchar);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700875
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200876 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__, hc_num);
877 dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
878 hcchar >> HCCHAR_DEVADDR_SHIFT &
879 HCCHAR_DEVADDR_MASK >> HCCHAR_DEVADDR_SHIFT);
880 dev_vdbg(hsotg->dev, " Ep Num: %d\n",
881 hcchar >> HCCHAR_EPNUM_SHIFT &
882 HCCHAR_EPNUM_MASK >> HCCHAR_EPNUM_SHIFT);
883 dev_vdbg(hsotg->dev, " Is In: %d\n",
884 !!(hcchar & HCCHAR_EPDIR));
885 dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
886 !!(hcchar & HCCHAR_LSPDDEV));
887 dev_vdbg(hsotg->dev, " Ep Type: %d\n",
888 hcchar >> HCCHAR_EPTYPE_SHIFT &
889 HCCHAR_EPTYPE_MASK >> HCCHAR_EPTYPE_SHIFT);
890 dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
891 hcchar >> HCCHAR_MPS_SHIFT &
892 HCCHAR_MPS_MASK >> HCCHAR_MPS_SHIFT);
893 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
894 hcchar >> HCCHAR_MULTICNT_SHIFT &
895 HCCHAR_MULTICNT_MASK >> HCCHAR_MULTICNT_SHIFT);
896 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700897
898 /* Program the HCSPLT register for SPLITs */
899 if (chan->do_split) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200900 if (dbg_hc(chan))
901 dev_vdbg(hsotg->dev,
902 "Programming HC %d with split --> %s\n",
903 hc_num,
904 chan->complete_split ? "CSPLIT" : "SSPLIT");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700905 if (chan->complete_split)
906 hcsplt |= HCSPLT_COMPSPLT;
907 hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
908 HCSPLT_XACTPOS_MASK;
909 hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
910 HCSPLT_HUBADDR_MASK;
911 hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
912 HCSPLT_PRTADDR_MASK;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200913 if (dbg_hc(chan)) {
914 dev_vdbg(hsotg->dev, " comp split %d\n",
915 chan->complete_split);
916 dev_vdbg(hsotg->dev, " xact pos %d\n",
917 chan->xact_pos);
918 dev_vdbg(hsotg->dev, " hub addr %d\n",
919 chan->hub_addr);
920 dev_vdbg(hsotg->dev, " hub port %d\n",
921 chan->hub_port);
922 dev_vdbg(hsotg->dev, " is_in %d\n",
923 chan->ep_is_in);
924 dev_vdbg(hsotg->dev, " Max Pkt %d\n",
925 hcchar >> HCCHAR_MPS_SHIFT &
926 HCCHAR_MPS_MASK >> HCCHAR_MPS_SHIFT);
927 dev_vdbg(hsotg->dev, " xferlen %d\n",
928 chan->xfer_len);
929 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700930 }
931
932 writel(hcsplt, hsotg->regs + HCSPLT(hc_num));
933}
934
935/**
936 * dwc2_hc_halt() - Attempts to halt a host channel
937 *
938 * @hsotg: Controller register interface
939 * @chan: Host channel to halt
940 * @halt_status: Reason for halting the channel
941 *
942 * This function should only be called in Slave mode or to abort a transfer in
943 * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
944 * controller halts the channel when the transfer is complete or a condition
945 * occurs that requires application intervention.
946 *
947 * In slave mode, checks for a free request queue entry, then sets the Channel
948 * Enable and Channel Disable bits of the Host Channel Characteristics
949 * register of the specified channel to intiate the halt. If there is no free
950 * request queue entry, sets only the Channel Disable bit of the HCCHARn
951 * register to flush requests for this channel. In the latter case, sets a
952 * flag to indicate that the host channel needs to be halted when a request
953 * queue slot is open.
954 *
955 * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
956 * HCCHARn register. The controller ensures there is space in the request
957 * queue before submitting the halt request.
958 *
959 * Some time may elapse before the core flushes any posted requests for this
960 * host channel and halts. The Channel Halted interrupt handler completes the
961 * deactivation of the host channel.
962 */
963void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
964 enum dwc2_halt_status halt_status)
965{
966 u32 nptxsts, hptxsts, hcchar;
967
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200968 if (dbg_hc(chan))
969 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700970 if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
971 dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
972
973 if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
974 halt_status == DWC2_HC_XFER_AHB_ERR) {
975 /*
976 * Disable all channel interrupts except Ch Halted. The QTD
977 * and QH state associated with this transfer has been cleared
978 * (in the case of URB_DEQUEUE), so the channel needs to be
979 * shut down carefully to prevent crashes.
980 */
981 u32 hcintmsk = HCINTMSK_CHHLTD;
982
983 dev_vdbg(hsotg->dev, "dequeue/error\n");
984 writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
985
986 /*
987 * Make sure no other interrupts besides halt are currently
988 * pending. Handling another interrupt could cause a crash due
989 * to the QTD and QH state.
990 */
991 writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
992
993 /*
994 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
995 * even if the channel was already halted for some other
996 * reason
997 */
998 chan->halt_status = halt_status;
999
1000 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
1001 if (!(hcchar & HCCHAR_CHENA)) {
1002 /*
1003 * The channel is either already halted or it hasn't
1004 * started yet. In DMA mode, the transfer may halt if
1005 * it finishes normally or a condition occurs that
1006 * requires driver intervention. Don't want to halt
1007 * the channel again. In either Slave or DMA mode,
1008 * it's possible that the transfer has been assigned
1009 * to a channel, but not started yet when an URB is
1010 * dequeued. Don't want to halt a channel that hasn't
1011 * started yet.
1012 */
1013 return;
1014 }
1015 }
1016 if (chan->halt_pending) {
1017 /*
1018 * A halt has already been issued for this channel. This might
1019 * happen when a transfer is aborted by a higher level in
1020 * the stack.
1021 */
1022 dev_vdbg(hsotg->dev,
1023 "*** %s: Channel %d, chan->halt_pending already set ***\n",
1024 __func__, chan->hc_num);
1025 return;
1026 }
1027
1028 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
1029
1030 /* No need to set the bit in DDMA for disabling the channel */
1031 /* TODO check it everywhere channel is disabled */
1032 if (hsotg->core_params->dma_desc_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001033 if (dbg_hc(chan))
1034 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001035 hcchar |= HCCHAR_CHENA;
1036 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001037 if (dbg_hc(chan))
1038 dev_dbg(hsotg->dev, "desc DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001039 }
1040 hcchar |= HCCHAR_CHDIS;
1041
1042 if (hsotg->core_params->dma_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001043 if (dbg_hc(chan))
1044 dev_vdbg(hsotg->dev, "DMA not enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001045 hcchar |= HCCHAR_CHENA;
1046
1047 /* Check for space in the request queue to issue the halt */
1048 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1049 chan->ep_type == USB_ENDPOINT_XFER_BULK) {
1050 dev_vdbg(hsotg->dev, "control/bulk\n");
1051 nptxsts = readl(hsotg->regs + GNPTXSTS);
1052 if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
1053 dev_vdbg(hsotg->dev, "Disabling channel\n");
1054 hcchar &= ~HCCHAR_CHENA;
1055 }
1056 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001057 if (dbg_perio())
1058 dev_vdbg(hsotg->dev, "isoc/intr\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001059 hptxsts = readl(hsotg->regs + HPTXSTS);
1060 if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
1061 hsotg->queuing_high_bandwidth) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001062 if (dbg_perio())
1063 dev_vdbg(hsotg->dev, "Disabling channel\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001064 hcchar &= ~HCCHAR_CHENA;
1065 }
1066 }
1067 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001068 if (dbg_hc(chan))
1069 dev_vdbg(hsotg->dev, "DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001070 }
1071
1072 writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1073 chan->halt_status = halt_status;
1074
1075 if (hcchar & HCCHAR_CHENA) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001076 if (dbg_hc(chan))
1077 dev_vdbg(hsotg->dev, "Channel enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001078 chan->halt_pending = 1;
1079 chan->halt_on_queue = 0;
1080 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001081 if (dbg_hc(chan))
1082 dev_vdbg(hsotg->dev, "Channel disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001083 chan->halt_on_queue = 1;
1084 }
1085
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001086 if (dbg_hc(chan)) {
1087 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1088 chan->hc_num);
1089 dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
1090 hcchar);
1091 dev_vdbg(hsotg->dev, " halt_pending: %d\n",
1092 chan->halt_pending);
1093 dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
1094 chan->halt_on_queue);
1095 dev_vdbg(hsotg->dev, " halt_status: %d\n",
1096 chan->halt_status);
1097 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001098}
1099
1100/**
1101 * dwc2_hc_cleanup() - Clears the transfer state for a host channel
1102 *
1103 * @hsotg: Programming view of DWC_otg controller
1104 * @chan: Identifies the host channel to clean up
1105 *
1106 * This function is normally called after a transfer is done and the host
1107 * channel is being released
1108 */
1109void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1110{
1111 u32 hcintmsk;
1112
1113 chan->xfer_started = 0;
1114
1115 /*
1116 * Clear channel interrupt enables and any unhandled channel interrupt
1117 * conditions
1118 */
1119 writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
1120 hcintmsk = 0xffffffff;
1121 hcintmsk &= ~HCINTMSK_RESERVED14_31;
1122 writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1123}
1124
1125/**
1126 * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
1127 * which frame a periodic transfer should occur
1128 *
1129 * @hsotg: Programming view of DWC_otg controller
1130 * @chan: Identifies the host channel to set up and its properties
1131 * @hcchar: Current value of the HCCHAR register for the specified host channel
1132 *
1133 * This function has no effect on non-periodic transfers
1134 */
1135static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
1136 struct dwc2_host_chan *chan, u32 *hcchar)
1137{
1138 u32 hfnum, frnum;
1139
1140 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1141 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1142 hfnum = readl(hsotg->regs + HFNUM);
1143 frnum = hfnum >> HFNUM_FRNUM_SHIFT &
1144 HFNUM_FRNUM_MASK >> HFNUM_FRNUM_SHIFT;
1145
1146 /* 1 if _next_ frame is odd, 0 if it's even */
1147 if (frnum & 0x1)
1148 *hcchar |= HCCHAR_ODDFRM;
1149 }
1150}
1151
1152static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1153{
1154 /* Set up the initial PID for the transfer */
1155 if (chan->speed == USB_SPEED_HIGH) {
1156 if (chan->ep_is_in) {
1157 if (chan->multi_count == 1)
1158 chan->data_pid_start = DWC2_HC_PID_DATA0;
1159 else if (chan->multi_count == 2)
1160 chan->data_pid_start = DWC2_HC_PID_DATA1;
1161 else
1162 chan->data_pid_start = DWC2_HC_PID_DATA2;
1163 } else {
1164 if (chan->multi_count == 1)
1165 chan->data_pid_start = DWC2_HC_PID_DATA0;
1166 else
1167 chan->data_pid_start = DWC2_HC_PID_MDATA;
1168 }
1169 } else {
1170 chan->data_pid_start = DWC2_HC_PID_DATA0;
1171 }
1172}
1173
1174/**
1175 * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1176 * the Host Channel
1177 *
1178 * @hsotg: Programming view of DWC_otg controller
1179 * @chan: Information needed to initialize the host channel
1180 *
1181 * This function should only be called in Slave mode. For a channel associated
1182 * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1183 * associated with a periodic EP, the periodic Tx FIFO is written.
1184 *
1185 * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1186 * the number of bytes written to the Tx FIFO.
1187 */
1188static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1189 struct dwc2_host_chan *chan)
1190{
1191 u32 i;
1192 u32 remaining_count;
1193 u32 byte_count;
1194 u32 dword_count;
1195 u32 __iomem *data_fifo;
1196 u32 *data_buf = (u32 *)chan->xfer_buf;
1197
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001198 if (dbg_hc(chan))
1199 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001200
1201 data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
1202
1203 remaining_count = chan->xfer_len - chan->xfer_count;
1204 if (remaining_count > chan->max_packet)
1205 byte_count = chan->max_packet;
1206 else
1207 byte_count = remaining_count;
1208
1209 dword_count = (byte_count + 3) / 4;
1210
1211 if (((unsigned long)data_buf & 0x3) == 0) {
1212 /* xfer_buf is DWORD aligned */
1213 for (i = 0; i < dword_count; i++, data_buf++)
1214 writel(*data_buf, data_fifo);
1215 } else {
1216 /* xfer_buf is not DWORD aligned */
1217 for (i = 0; i < dword_count; i++, data_buf++) {
1218 u32 data = data_buf[0] | data_buf[1] << 8 |
1219 data_buf[2] << 16 | data_buf[3] << 24;
1220 writel(data, data_fifo);
1221 }
1222 }
1223
1224 chan->xfer_count += byte_count;
1225 chan->xfer_buf += byte_count;
1226}
1227
1228/**
1229 * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1230 * channel and starts the transfer
1231 *
1232 * @hsotg: Programming view of DWC_otg controller
1233 * @chan: Information needed to initialize the host channel. The xfer_len value
1234 * may be reduced to accommodate the max widths of the XferSize and
1235 * PktCnt fields in the HCTSIZn register. The multi_count value may be
1236 * changed to reflect the final xfer_len value.
1237 *
1238 * This function may be called in either Slave mode or DMA mode. In Slave mode,
1239 * the caller must ensure that there is sufficient space in the request queue
1240 * and Tx Data FIFO.
1241 *
1242 * For an OUT transfer in Slave mode, it loads a data packet into the
1243 * appropriate FIFO. If necessary, additional data packets are loaded in the
1244 * Host ISR.
1245 *
1246 * For an IN transfer in Slave mode, a data packet is requested. The data
1247 * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1248 * additional data packets are requested in the Host ISR.
1249 *
1250 * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1251 * register along with a packet count of 1 and the channel is enabled. This
1252 * causes a single PING transaction to occur. Other fields in HCTSIZ are
1253 * simply set to 0 since no data transfer occurs in this case.
1254 *
1255 * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1256 * all the information required to perform the subsequent data transfer. In
1257 * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1258 * controller performs the entire PING protocol, then starts the data
1259 * transfer.
1260 */
1261void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1262 struct dwc2_host_chan *chan)
1263{
1264 u32 max_hc_xfer_size = hsotg->core_params->max_transfer_size;
1265 u16 max_hc_pkt_count = hsotg->core_params->max_packet_count;
1266 u32 hcchar;
1267 u32 hctsiz = 0;
1268 u16 num_packets;
1269
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001270 if (dbg_hc(chan))
1271 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001272
1273 if (chan->do_ping) {
1274 if (hsotg->core_params->dma_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001275 if (dbg_hc(chan))
1276 dev_vdbg(hsotg->dev, "ping, no DMA\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001277 dwc2_hc_do_ping(hsotg, chan);
1278 chan->xfer_started = 1;
1279 return;
1280 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001281 if (dbg_hc(chan))
1282 dev_vdbg(hsotg->dev, "ping, DMA\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001283 hctsiz |= TSIZ_DOPNG;
1284 }
1285 }
1286
1287 if (chan->do_split) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001288 if (dbg_hc(chan))
1289 dev_vdbg(hsotg->dev, "split\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001290 num_packets = 1;
1291
1292 if (chan->complete_split && !chan->ep_is_in)
1293 /*
1294 * For CSPLIT OUT Transfer, set the size to 0 so the
1295 * core doesn't expect any data written to the FIFO
1296 */
1297 chan->xfer_len = 0;
1298 else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1299 chan->xfer_len = chan->max_packet;
1300 else if (!chan->ep_is_in && chan->xfer_len > 188)
1301 chan->xfer_len = 188;
1302
1303 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1304 TSIZ_XFERSIZE_MASK;
1305 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001306 if (dbg_hc(chan))
1307 dev_vdbg(hsotg->dev, "no split\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001308 /*
1309 * Ensure that the transfer length and packet count will fit
1310 * in the widths allocated for them in the HCTSIZn register
1311 */
1312 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1313 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1314 /*
1315 * Make sure the transfer size is no larger than one
1316 * (micro)frame's worth of data. (A check was done
1317 * when the periodic transfer was accepted to ensure
1318 * that a (micro)frame's worth of data can be
1319 * programmed into a channel.)
1320 */
1321 u32 max_periodic_len =
1322 chan->multi_count * chan->max_packet;
1323
1324 if (chan->xfer_len > max_periodic_len)
1325 chan->xfer_len = max_periodic_len;
1326 } else if (chan->xfer_len > max_hc_xfer_size) {
1327 /*
1328 * Make sure that xfer_len is a multiple of max packet
1329 * size
1330 */
1331 chan->xfer_len =
1332 max_hc_xfer_size - chan->max_packet + 1;
1333 }
1334
1335 if (chan->xfer_len > 0) {
1336 num_packets = (chan->xfer_len + chan->max_packet - 1) /
1337 chan->max_packet;
1338 if (num_packets > max_hc_pkt_count) {
1339 num_packets = max_hc_pkt_count;
1340 chan->xfer_len = num_packets * chan->max_packet;
1341 }
1342 } else {
1343 /* Need 1 packet for transfer length of 0 */
1344 num_packets = 1;
1345 }
1346
1347 if (chan->ep_is_in)
1348 /*
1349 * Always program an integral # of max packets for IN
1350 * transfers
1351 */
1352 chan->xfer_len = num_packets * chan->max_packet;
1353
1354 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1355 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1356 /*
1357 * Make sure that the multi_count field matches the
1358 * actual transfer length
1359 */
1360 chan->multi_count = num_packets;
1361
1362 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1363 dwc2_set_pid_isoc(chan);
1364
1365 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1366 TSIZ_XFERSIZE_MASK;
1367 }
1368
1369 chan->start_pkt_count = num_packets;
1370 hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1371 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1372 TSIZ_SC_MC_PID_MASK;
1373 writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001374 if (dbg_hc(chan)) {
1375 dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1376 hctsiz, chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001377
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001378 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1379 chan->hc_num);
1380 dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
1381 hctsiz >> TSIZ_XFERSIZE_SHIFT &
1382 TSIZ_XFERSIZE_MASK >> TSIZ_XFERSIZE_SHIFT);
1383 dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
1384 hctsiz >> TSIZ_PKTCNT_SHIFT &
1385 TSIZ_PKTCNT_MASK >> TSIZ_PKTCNT_SHIFT);
1386 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1387 hctsiz >> TSIZ_SC_MC_PID_SHIFT &
1388 TSIZ_SC_MC_PID_MASK >> TSIZ_SC_MC_PID_SHIFT);
1389 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001390
1391 if (hsotg->core_params->dma_enable > 0) {
1392 dma_addr_t dma_addr;
1393
1394 if (chan->align_buf) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001395 if (dbg_hc(chan))
1396 dev_vdbg(hsotg->dev, "align_buf\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001397 dma_addr = chan->align_buf;
1398 } else {
1399 dma_addr = chan->xfer_dma;
1400 }
1401 writel((u32)dma_addr, hsotg->regs + HCDMA(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001402 if (dbg_hc(chan))
1403 dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1404 (unsigned long)dma_addr, chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001405 }
1406
1407 /* Start the split */
1408 if (chan->do_split) {
1409 u32 hcsplt = readl(hsotg->regs + HCSPLT(chan->hc_num));
1410
1411 hcsplt |= HCSPLT_SPLTENA;
1412 writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
1413 }
1414
1415 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
1416 hcchar &= ~HCCHAR_MULTICNT_MASK;
1417 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1418 HCCHAR_MULTICNT_MASK;
1419 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1420
1421 if (hcchar & HCCHAR_CHDIS)
1422 dev_warn(hsotg->dev,
1423 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1424 __func__, chan->hc_num, hcchar);
1425
1426 /* Set host channel enable after all other setup is complete */
1427 hcchar |= HCCHAR_CHENA;
1428 hcchar &= ~HCCHAR_CHDIS;
1429
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001430 if (dbg_hc(chan))
1431 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
1432 hcchar >> HCCHAR_MULTICNT_SHIFT &
1433 HCCHAR_MULTICNT_MASK >> HCCHAR_MULTICNT_SHIFT);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001434
1435 writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001436 if (dbg_hc(chan))
1437 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1438 chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001439
1440 chan->xfer_started = 1;
1441 chan->requests++;
1442
1443 if (hsotg->core_params->dma_enable <= 0 &&
1444 !chan->ep_is_in && chan->xfer_len > 0)
1445 /* Load OUT packet into the appropriate Tx FIFO */
1446 dwc2_hc_write_packet(hsotg, chan);
1447}
1448
1449/**
1450 * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1451 * host channel and starts the transfer in Descriptor DMA mode
1452 *
1453 * @hsotg: Programming view of DWC_otg controller
1454 * @chan: Information needed to initialize the host channel
1455 *
1456 * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1457 * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1458 * with micro-frame bitmap.
1459 *
1460 * Initializes HCDMA register with descriptor list address and CTD value then
1461 * starts the transfer via enabling the channel.
1462 */
1463void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1464 struct dwc2_host_chan *chan)
1465{
1466 u32 hcchar;
1467 u32 hc_dma;
1468 u32 hctsiz = 0;
1469
1470 if (chan->do_ping)
1471 hctsiz |= TSIZ_DOPNG;
1472
1473 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1474 dwc2_set_pid_isoc(chan);
1475
1476 /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1477 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1478 TSIZ_SC_MC_PID_MASK;
1479
1480 /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1481 hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1482
1483 /* Non-zero only for high-speed interrupt endpoints */
1484 hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1485
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001486 if (dbg_hc(chan)) {
1487 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1488 chan->hc_num);
1489 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1490 chan->data_pid_start);
1491 dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
1492 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001493
1494 writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1495
1496 hc_dma = (u32)chan->desc_list_addr & HCDMA_DMA_ADDR_MASK;
1497
1498 /* Always start from first descriptor */
1499 hc_dma &= ~HCDMA_CTD_MASK;
1500 writel(hc_dma, hsotg->regs + HCDMA(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001501 if (dbg_hc(chan))
1502 dev_vdbg(hsotg->dev, "Wrote %08x to HCDMA(%d)\n",
1503 hc_dma, chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001504
1505 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
1506 hcchar &= ~HCCHAR_MULTICNT_MASK;
1507 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1508 HCCHAR_MULTICNT_MASK;
1509
1510 if (hcchar & HCCHAR_CHDIS)
1511 dev_warn(hsotg->dev,
1512 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1513 __func__, chan->hc_num, hcchar);
1514
1515 /* Set host channel enable after all other setup is complete */
1516 hcchar |= HCCHAR_CHENA;
1517 hcchar &= ~HCCHAR_CHDIS;
1518
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001519 if (dbg_hc(chan))
1520 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
1521 hcchar >> HCCHAR_MULTICNT_SHIFT &
1522 HCCHAR_MULTICNT_MASK >> HCCHAR_MULTICNT_SHIFT);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001523
1524 writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001525 if (dbg_hc(chan))
1526 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1527 chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001528
1529 chan->xfer_started = 1;
1530 chan->requests++;
1531}
1532
1533/**
1534 * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
1535 * a previous call to dwc2_hc_start_transfer()
1536 *
1537 * @hsotg: Programming view of DWC_otg controller
1538 * @chan: Information needed to initialize the host channel
1539 *
1540 * The caller must ensure there is sufficient space in the request queue and Tx
1541 * Data FIFO. This function should only be called in Slave mode. In DMA mode,
1542 * the controller acts autonomously to complete transfers programmed to a host
1543 * channel.
1544 *
1545 * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
1546 * if there is any data remaining to be queued. For an IN transfer, another
1547 * data packet is always requested. For the SETUP phase of a control transfer,
1548 * this function does nothing.
1549 *
1550 * Return: 1 if a new request is queued, 0 if no more requests are required
1551 * for this transfer
1552 */
1553int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
1554 struct dwc2_host_chan *chan)
1555{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001556 if (dbg_hc(chan))
1557 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1558 chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001559
1560 if (chan->do_split)
1561 /* SPLITs always queue just once per channel */
1562 return 0;
1563
1564 if (chan->data_pid_start == DWC2_HC_PID_SETUP)
1565 /* SETUPs are queued only once since they can't be NAK'd */
1566 return 0;
1567
1568 if (chan->ep_is_in) {
1569 /*
1570 * Always queue another request for other IN transfers. If
1571 * back-to-back INs are issued and NAKs are received for both,
1572 * the driver may still be processing the first NAK when the
1573 * second NAK is received. When the interrupt handler clears
1574 * the NAK interrupt for the first NAK, the second NAK will
1575 * not be seen. So we can't depend on the NAK interrupt
1576 * handler to requeue a NAK'd request. Instead, IN requests
1577 * are issued each time this function is called. When the
1578 * transfer completes, the extra requests for the channel will
1579 * be flushed.
1580 */
1581 u32 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
1582
1583 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1584 hcchar |= HCCHAR_CHENA;
1585 hcchar &= ~HCCHAR_CHDIS;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001586 if (dbg_hc(chan))
1587 dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
1588 hcchar);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001589 writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1590 chan->requests++;
1591 return 1;
1592 }
1593
1594 /* OUT transfers */
1595
1596 if (chan->xfer_count < chan->xfer_len) {
1597 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1598 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1599 u32 hcchar = readl(hsotg->regs +
1600 HCCHAR(chan->hc_num));
1601
1602 dwc2_hc_set_even_odd_frame(hsotg, chan,
1603 &hcchar);
1604 }
1605
1606 /* Load OUT packet into the appropriate Tx FIFO */
1607 dwc2_hc_write_packet(hsotg, chan);
1608 chan->requests++;
1609 return 1;
1610 }
1611
1612 return 0;
1613}
1614
1615/**
1616 * dwc2_hc_do_ping() - Starts a PING transfer
1617 *
1618 * @hsotg: Programming view of DWC_otg controller
1619 * @chan: Information needed to initialize the host channel
1620 *
1621 * This function should only be called in Slave mode. The Do Ping bit is set in
1622 * the HCTSIZ register, then the channel is enabled.
1623 */
1624void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1625{
1626 u32 hcchar;
1627 u32 hctsiz;
1628
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001629 if (dbg_hc(chan))
1630 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1631 chan->hc_num);
1632
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001633
1634 hctsiz = TSIZ_DOPNG;
1635 hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
1636 writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1637
1638 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
1639 hcchar |= HCCHAR_CHENA;
1640 hcchar &= ~HCCHAR_CHDIS;
1641 writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1642}
1643
1644/**
1645 * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
1646 * the HFIR register according to PHY type and speed
1647 *
1648 * @hsotg: Programming view of DWC_otg controller
1649 *
1650 * NOTE: The caller can modify the value of the HFIR register only after the
1651 * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
1652 * has been set
1653 */
1654u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
1655{
1656 u32 usbcfg;
1657 u32 hwcfg2;
1658 u32 hprt0;
1659 int clock = 60; /* default value */
1660
1661 usbcfg = readl(hsotg->regs + GUSBCFG);
1662 hwcfg2 = readl(hsotg->regs + GHWCFG2);
1663 hprt0 = readl(hsotg->regs + HPRT0);
1664
1665 if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
1666 !(usbcfg & GUSBCFG_PHYIF16))
1667 clock = 60;
1668 if ((usbcfg & GUSBCFG_PHYSEL) && (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) ==
1669 GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
1670 clock = 48;
1671 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
1672 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
1673 clock = 30;
1674 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
1675 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
1676 clock = 60;
1677 if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
1678 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
1679 clock = 48;
1680 if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
1681 (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) ==
1682 GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
1683 clock = 48;
1684 if ((usbcfg & GUSBCFG_PHYSEL) && (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) ==
1685 GHWCFG2_FS_PHY_TYPE_DEDICATED)
1686 clock = 48;
1687
1688 if ((hprt0 & HPRT0_SPD_MASK) == 0)
1689 /* High speed case */
1690 return 125 * clock;
1691 else
1692 /* FS/LS case */
1693 return 1000 * clock;
1694}
1695
1696/**
1697 * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
1698 * buffer
1699 *
1700 * @core_if: Programming view of DWC_otg controller
1701 * @dest: Destination buffer for the packet
1702 * @bytes: Number of bytes to copy to the destination
1703 */
1704void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
1705{
1706 u32 __iomem *fifo = hsotg->regs + HCFIFO(0);
1707 u32 *data_buf = (u32 *)dest;
1708 int word_count = (bytes + 3) / 4;
1709 int i;
1710
1711 /*
1712 * Todo: Account for the case where dest is not dword aligned. This
1713 * requires reading data from the FIFO into a u32 temp buffer, then
1714 * moving it into the data buffer.
1715 */
1716
1717 dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
1718
1719 for (i = 0; i < word_count; i++, data_buf++)
1720 *data_buf = readl(fifo);
1721}
1722
1723/**
1724 * dwc2_dump_host_registers() - Prints the host registers
1725 *
1726 * @hsotg: Programming view of DWC_otg controller
1727 *
1728 * NOTE: This function will be removed once the peripheral controller code
1729 * is integrated and the driver is stable
1730 */
1731void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
1732{
1733#ifdef DEBUG
1734 u32 __iomem *addr;
1735 int i;
1736
1737 dev_dbg(hsotg->dev, "Host Global Registers\n");
1738 addr = hsotg->regs + HCFG;
1739 dev_dbg(hsotg->dev, "HCFG @0x%08lX : 0x%08X\n",
1740 (unsigned long)addr, readl(addr));
1741 addr = hsotg->regs + HFIR;
1742 dev_dbg(hsotg->dev, "HFIR @0x%08lX : 0x%08X\n",
1743 (unsigned long)addr, readl(addr));
1744 addr = hsotg->regs + HFNUM;
1745 dev_dbg(hsotg->dev, "HFNUM @0x%08lX : 0x%08X\n",
1746 (unsigned long)addr, readl(addr));
1747 addr = hsotg->regs + HPTXSTS;
1748 dev_dbg(hsotg->dev, "HPTXSTS @0x%08lX : 0x%08X\n",
1749 (unsigned long)addr, readl(addr));
1750 addr = hsotg->regs + HAINT;
1751 dev_dbg(hsotg->dev, "HAINT @0x%08lX : 0x%08X\n",
1752 (unsigned long)addr, readl(addr));
1753 addr = hsotg->regs + HAINTMSK;
1754 dev_dbg(hsotg->dev, "HAINTMSK @0x%08lX : 0x%08X\n",
1755 (unsigned long)addr, readl(addr));
1756 if (hsotg->core_params->dma_desc_enable > 0) {
1757 addr = hsotg->regs + HFLBADDR;
1758 dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
1759 (unsigned long)addr, readl(addr));
1760 }
1761
1762 addr = hsotg->regs + HPRT0;
1763 dev_dbg(hsotg->dev, "HPRT0 @0x%08lX : 0x%08X\n",
1764 (unsigned long)addr, readl(addr));
1765
1766 for (i = 0; i < hsotg->core_params->host_channels; i++) {
1767 dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
1768 addr = hsotg->regs + HCCHAR(i);
1769 dev_dbg(hsotg->dev, "HCCHAR @0x%08lX : 0x%08X\n",
1770 (unsigned long)addr, readl(addr));
1771 addr = hsotg->regs + HCSPLT(i);
1772 dev_dbg(hsotg->dev, "HCSPLT @0x%08lX : 0x%08X\n",
1773 (unsigned long)addr, readl(addr));
1774 addr = hsotg->regs + HCINT(i);
1775 dev_dbg(hsotg->dev, "HCINT @0x%08lX : 0x%08X\n",
1776 (unsigned long)addr, readl(addr));
1777 addr = hsotg->regs + HCINTMSK(i);
1778 dev_dbg(hsotg->dev, "HCINTMSK @0x%08lX : 0x%08X\n",
1779 (unsigned long)addr, readl(addr));
1780 addr = hsotg->regs + HCTSIZ(i);
1781 dev_dbg(hsotg->dev, "HCTSIZ @0x%08lX : 0x%08X\n",
1782 (unsigned long)addr, readl(addr));
1783 addr = hsotg->regs + HCDMA(i);
1784 dev_dbg(hsotg->dev, "HCDMA @0x%08lX : 0x%08X\n",
1785 (unsigned long)addr, readl(addr));
1786 if (hsotg->core_params->dma_desc_enable > 0) {
1787 addr = hsotg->regs + HCDMAB(i);
1788 dev_dbg(hsotg->dev, "HCDMAB @0x%08lX : 0x%08X\n",
1789 (unsigned long)addr, readl(addr));
1790 }
1791 }
1792#endif
1793}
1794
1795/**
1796 * dwc2_dump_global_registers() - Prints the core global registers
1797 *
1798 * @hsotg: Programming view of DWC_otg controller
1799 *
1800 * NOTE: This function will be removed once the peripheral controller code
1801 * is integrated and the driver is stable
1802 */
1803void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
1804{
1805#ifdef DEBUG
1806 u32 __iomem *addr;
1807 int i, ep_num;
1808 char *txfsiz;
1809
1810 dev_dbg(hsotg->dev, "Core Global Registers\n");
1811 addr = hsotg->regs + GOTGCTL;
1812 dev_dbg(hsotg->dev, "GOTGCTL @0x%08lX : 0x%08X\n",
1813 (unsigned long)addr, readl(addr));
1814 addr = hsotg->regs + GOTGINT;
1815 dev_dbg(hsotg->dev, "GOTGINT @0x%08lX : 0x%08X\n",
1816 (unsigned long)addr, readl(addr));
1817 addr = hsotg->regs + GAHBCFG;
1818 dev_dbg(hsotg->dev, "GAHBCFG @0x%08lX : 0x%08X\n",
1819 (unsigned long)addr, readl(addr));
1820 addr = hsotg->regs + GUSBCFG;
1821 dev_dbg(hsotg->dev, "GUSBCFG @0x%08lX : 0x%08X\n",
1822 (unsigned long)addr, readl(addr));
1823 addr = hsotg->regs + GRSTCTL;
1824 dev_dbg(hsotg->dev, "GRSTCTL @0x%08lX : 0x%08X\n",
1825 (unsigned long)addr, readl(addr));
1826 addr = hsotg->regs + GINTSTS;
1827 dev_dbg(hsotg->dev, "GINTSTS @0x%08lX : 0x%08X\n",
1828 (unsigned long)addr, readl(addr));
1829 addr = hsotg->regs + GINTMSK;
1830 dev_dbg(hsotg->dev, "GINTMSK @0x%08lX : 0x%08X\n",
1831 (unsigned long)addr, readl(addr));
1832 addr = hsotg->regs + GRXSTSR;
1833 dev_dbg(hsotg->dev, "GRXSTSR @0x%08lX : 0x%08X\n",
1834 (unsigned long)addr, readl(addr));
1835 addr = hsotg->regs + GRXFSIZ;
1836 dev_dbg(hsotg->dev, "GRXFSIZ @0x%08lX : 0x%08X\n",
1837 (unsigned long)addr, readl(addr));
1838 addr = hsotg->regs + GNPTXFSIZ;
1839 dev_dbg(hsotg->dev, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
1840 (unsigned long)addr, readl(addr));
1841 addr = hsotg->regs + GNPTXSTS;
1842 dev_dbg(hsotg->dev, "GNPTXSTS @0x%08lX : 0x%08X\n",
1843 (unsigned long)addr, readl(addr));
1844 addr = hsotg->regs + GI2CCTL;
1845 dev_dbg(hsotg->dev, "GI2CCTL @0x%08lX : 0x%08X\n",
1846 (unsigned long)addr, readl(addr));
1847 addr = hsotg->regs + GPVNDCTL;
1848 dev_dbg(hsotg->dev, "GPVNDCTL @0x%08lX : 0x%08X\n",
1849 (unsigned long)addr, readl(addr));
1850 addr = hsotg->regs + GGPIO;
1851 dev_dbg(hsotg->dev, "GGPIO @0x%08lX : 0x%08X\n",
1852 (unsigned long)addr, readl(addr));
1853 addr = hsotg->regs + GUID;
1854 dev_dbg(hsotg->dev, "GUID @0x%08lX : 0x%08X\n",
1855 (unsigned long)addr, readl(addr));
1856 addr = hsotg->regs + GSNPSID;
1857 dev_dbg(hsotg->dev, "GSNPSID @0x%08lX : 0x%08X\n",
1858 (unsigned long)addr, readl(addr));
1859 addr = hsotg->regs + GHWCFG1;
1860 dev_dbg(hsotg->dev, "GHWCFG1 @0x%08lX : 0x%08X\n",
1861 (unsigned long)addr, readl(addr));
1862 addr = hsotg->regs + GHWCFG2;
1863 dev_dbg(hsotg->dev, "GHWCFG2 @0x%08lX : 0x%08X\n",
1864 (unsigned long)addr, readl(addr));
1865 addr = hsotg->regs + GHWCFG3;
1866 dev_dbg(hsotg->dev, "GHWCFG3 @0x%08lX : 0x%08X\n",
1867 (unsigned long)addr, readl(addr));
1868 addr = hsotg->regs + GHWCFG4;
1869 dev_dbg(hsotg->dev, "GHWCFG4 @0x%08lX : 0x%08X\n",
1870 (unsigned long)addr, readl(addr));
1871 addr = hsotg->regs + GLPMCFG;
1872 dev_dbg(hsotg->dev, "GLPMCFG @0x%08lX : 0x%08X\n",
1873 (unsigned long)addr, readl(addr));
1874 addr = hsotg->regs + GPWRDN;
1875 dev_dbg(hsotg->dev, "GPWRDN @0x%08lX : 0x%08X\n",
1876 (unsigned long)addr, readl(addr));
1877 addr = hsotg->regs + GDFIFOCFG;
1878 dev_dbg(hsotg->dev, "GDFIFOCFG @0x%08lX : 0x%08X\n",
1879 (unsigned long)addr, readl(addr));
1880 addr = hsotg->regs + HPTXFSIZ;
1881 dev_dbg(hsotg->dev, "HPTXFSIZ @0x%08lX : 0x%08X\n",
1882 (unsigned long)addr, readl(addr));
1883
1884 if (hsotg->core_params->en_multiple_tx_fifo <= 0) {
1885 ep_num = hsotg->hwcfg4 >> GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT &
1886 GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK >>
1887 GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
1888 txfsiz = "DPTXFSIZ";
1889 } else {
1890 ep_num = hsotg->hwcfg4 >> GHWCFG4_NUM_IN_EPS_SHIFT &
1891 GHWCFG4_NUM_IN_EPS_MASK >> GHWCFG4_NUM_IN_EPS_SHIFT;
1892 txfsiz = "DIENPTXF";
1893 }
1894
1895 for (i = 0; i < ep_num; i++) {
1896 addr = hsotg->regs + DPTXFSIZN(i + 1);
1897 dev_dbg(hsotg->dev, "%s[%d] @0x%08lX : 0x%08X\n", txfsiz, i + 1,
1898 (unsigned long)addr, readl(addr));
1899 }
1900
1901 addr = hsotg->regs + PCGCTL;
1902 dev_dbg(hsotg->dev, "PCGCTL @0x%08lX : 0x%08X\n",
1903 (unsigned long)addr, readl(addr));
1904#endif
1905}
1906
1907/**
1908 * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
1909 *
1910 * @hsotg: Programming view of DWC_otg controller
1911 * @num: Tx FIFO to flush
1912 */
1913void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
1914{
1915 u32 greset;
1916 int count = 0;
1917
1918 dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
1919
1920 greset = GRSTCTL_TXFFLSH;
1921 greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
1922 writel(greset, hsotg->regs + GRSTCTL);
1923
1924 do {
1925 greset = readl(hsotg->regs + GRSTCTL);
1926 if (++count > 10000) {
1927 dev_warn(hsotg->dev,
1928 "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
1929 __func__, greset,
1930 readl(hsotg->regs + GNPTXSTS));
1931 break;
1932 }
1933 udelay(1);
1934 } while (greset & GRSTCTL_TXFFLSH);
1935
1936 /* Wait for at least 3 PHY Clocks */
1937 udelay(1);
1938}
1939
1940/**
1941 * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
1942 *
1943 * @hsotg: Programming view of DWC_otg controller
1944 */
1945void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
1946{
1947 u32 greset;
1948 int count = 0;
1949
1950 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1951
1952 greset = GRSTCTL_RXFFLSH;
1953 writel(greset, hsotg->regs + GRSTCTL);
1954
1955 do {
1956 greset = readl(hsotg->regs + GRSTCTL);
1957 if (++count > 10000) {
1958 dev_warn(hsotg->dev, "%s() HANG! GRSTCTL=%0x\n",
1959 __func__, greset);
1960 break;
1961 }
1962 udelay(1);
1963 } while (greset & GRSTCTL_RXFFLSH);
1964
1965 /* Wait for at least 3 PHY Clocks */
1966 udelay(1);
1967}
1968
1969#define DWC2_PARAM_TEST(a, b, c) ((a) < (b) || (a) > (c))
1970
1971/* Parameter access functions */
1972int dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg, int val)
1973{
1974 int valid = 1;
1975 int retval = 0;
1976 u32 op_mode;
1977
1978 op_mode = hsotg->hwcfg2 & GHWCFG2_OP_MODE_MASK;
1979
1980 switch (val) {
1981 case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
1982 if (op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
1983 valid = 0;
1984 break;
1985 case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
1986 switch (op_mode) {
1987 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
1988 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
1989 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
1990 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
1991 break;
1992 default:
1993 valid = 0;
1994 break;
1995 }
1996 break;
1997 case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
1998 /* always valid */
1999 break;
2000 default:
2001 valid = 0;
2002 break;
2003 }
2004
2005 if (!valid) {
2006 if (val >= 0)
2007 dev_err(hsotg->dev,
2008 "%d invalid for otg_cap parameter. Check HW configuration.\n",
2009 val);
2010 switch (op_mode) {
2011 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
2012 val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
2013 break;
2014 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
2015 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
2016 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
2017 val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
2018 break;
2019 default:
2020 val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
2021 break;
2022 }
2023 dev_dbg(hsotg->dev, "Setting otg_cap to %d\n", val);
2024 retval = -EINVAL;
2025 }
2026
2027 hsotg->core_params->otg_cap = val;
2028 return retval;
2029}
2030
2031int dwc2_set_param_dma_enable(struct dwc2_hsotg *hsotg, int val)
2032{
2033 int valid = 1;
2034 int retval = 0;
2035
2036 if (val > 0 && (hsotg->hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) ==
2037 GHWCFG2_SLAVE_ONLY_ARCH)
2038 valid = 0;
2039 if (val < 0)
2040 valid = 0;
2041
2042 if (!valid) {
2043 if (val >= 0)
2044 dev_err(hsotg->dev,
2045 "%d invalid for dma_enable parameter. Check HW configuration.\n",
2046 val);
2047 val = (hsotg->hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) !=
2048 GHWCFG2_SLAVE_ONLY_ARCH;
2049 dev_dbg(hsotg->dev, "Setting dma_enable to %d\n", val);
2050 retval = -EINVAL;
2051 }
2052
2053 hsotg->core_params->dma_enable = val;
2054 return retval;
2055}
2056
2057int dwc2_set_param_dma_desc_enable(struct dwc2_hsotg *hsotg, int val)
2058{
2059 int valid = 1;
2060 int retval = 0;
2061
2062 if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
2063 !(hsotg->hwcfg4 & GHWCFG4_DESC_DMA)))
2064 valid = 0;
2065 if (val < 0)
2066 valid = 0;
2067
2068 if (!valid) {
2069 if (val >= 0)
2070 dev_err(hsotg->dev,
2071 "%d invalid for dma_desc_enable parameter. Check HW configuration.\n",
2072 val);
2073 val = (hsotg->core_params->dma_enable > 0 &&
2074 (hsotg->hwcfg4 & GHWCFG4_DESC_DMA));
2075 dev_dbg(hsotg->dev, "Setting dma_desc_enable to %d\n", val);
2076 retval = -EINVAL;
2077 }
2078
2079 hsotg->core_params->dma_desc_enable = val;
2080 return retval;
2081}
2082
2083int dwc2_set_param_host_support_fs_ls_low_power(struct dwc2_hsotg *hsotg,
2084 int val)
2085{
2086 int retval = 0;
2087
2088 if (DWC2_PARAM_TEST(val, 0, 1)) {
2089 if (val >= 0) {
2090 dev_err(hsotg->dev,
2091 "Wrong value for host_support_fs_low_power\n");
2092 dev_err(hsotg->dev,
2093 "host_support_fs_low_power must be 0 or 1\n");
2094 }
2095 val = 0;
2096 dev_dbg(hsotg->dev,
2097 "Setting host_support_fs_low_power to %d\n", val);
2098 retval = -EINVAL;
2099 }
2100
2101 hsotg->core_params->host_support_fs_ls_low_power = val;
2102 return retval;
2103}
2104
2105int dwc2_set_param_enable_dynamic_fifo(struct dwc2_hsotg *hsotg, int val)
2106{
2107 int valid = 1;
2108 int retval = 0;
2109
2110 if (val > 0 && !(hsotg->hwcfg2 & GHWCFG2_DYNAMIC_FIFO))
2111 valid = 0;
2112 if (val < 0)
2113 valid = 0;
2114
2115 if (!valid) {
2116 if (val >= 0)
2117 dev_err(hsotg->dev,
2118 "%d invalid for enable_dynamic_fifo parameter. Check HW configuration.\n",
2119 val);
2120 val = !!(hsotg->hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
2121 dev_dbg(hsotg->dev, "Setting enable_dynamic_fifo to %d\n", val);
2122 retval = -EINVAL;
2123 }
2124
2125 hsotg->core_params->enable_dynamic_fifo = val;
2126 return retval;
2127}
2128
2129int dwc2_set_param_host_rx_fifo_size(struct dwc2_hsotg *hsotg, int val)
2130{
2131 int valid = 1;
2132 int retval = 0;
2133
2134 if (val < 16 || val > readl(hsotg->regs + GRXFSIZ))
2135 valid = 0;
2136
2137 if (!valid) {
2138 if (val >= 0)
2139 dev_err(hsotg->dev,
2140 "%d invalid for host_rx_fifo_size. Check HW configuration.\n",
2141 val);
2142 val = readl(hsotg->regs + GRXFSIZ);
2143 dev_dbg(hsotg->dev, "Setting host_rx_fifo_size to %d\n", val);
2144 retval = -EINVAL;
2145 }
2146
2147 hsotg->core_params->host_rx_fifo_size = val;
2148 return retval;
2149}
2150
2151int dwc2_set_param_host_nperio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
2152{
2153 int valid = 1;
2154 int retval = 0;
2155
2156 if (val < 16 || val > (readl(hsotg->regs + GNPTXFSIZ) >> 16 & 0xffff))
2157 valid = 0;
2158
2159 if (!valid) {
2160 if (val >= 0)
2161 dev_err(hsotg->dev,
2162 "%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
2163 val);
2164 val = readl(hsotg->regs + GNPTXFSIZ) >> 16 & 0xffff;
2165 dev_dbg(hsotg->dev, "Setting host_nperio_tx_fifo_size to %d\n",
2166 val);
2167 retval = -EINVAL;
2168 }
2169
2170 hsotg->core_params->host_nperio_tx_fifo_size = val;
2171 return retval;
2172}
2173
2174int dwc2_set_param_host_perio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
2175{
2176 int valid = 1;
2177 int retval = 0;
2178
2179 if (val < 16 || val > (hsotg->hptxfsiz >> 16))
2180 valid = 0;
2181
2182 if (!valid) {
2183 if (val >= 0)
2184 dev_err(hsotg->dev,
2185 "%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
2186 val);
2187 val = hsotg->hptxfsiz >> 16;
2188 dev_dbg(hsotg->dev, "Setting host_perio_tx_fifo_size to %d\n",
2189 val);
2190 retval = -EINVAL;
2191 }
2192
2193 hsotg->core_params->host_perio_tx_fifo_size = val;
2194 return retval;
2195}
2196
2197int dwc2_set_param_max_transfer_size(struct dwc2_hsotg *hsotg, int val)
2198{
2199 int valid = 1;
2200 int retval = 0;
2201 int width = hsotg->hwcfg3 >> GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT &
2202 GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK >>
2203 GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
2204
2205 if (val < 2047 || val >= (1 << (width + 11)))
2206 valid = 0;
2207
2208 if (!valid) {
2209 if (val >= 0)
2210 dev_err(hsotg->dev,
2211 "%d invalid for max_transfer_size. Check HW configuration.\n",
2212 val);
2213 val = (1 << (width + 11)) - 1;
2214 dev_dbg(hsotg->dev, "Setting max_transfer_size to %d\n", val);
2215 retval = -EINVAL;
2216 }
2217
2218 hsotg->core_params->max_transfer_size = val;
2219 return retval;
2220}
2221
2222int dwc2_set_param_max_packet_count(struct dwc2_hsotg *hsotg, int val)
2223{
2224 int valid = 1;
2225 int retval = 0;
2226 int width = hsotg->hwcfg3 >> GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT &
2227 GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK >>
2228 GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
2229
2230 if (val < 15 || val > (1 << (width + 4)))
2231 valid = 0;
2232
2233 if (!valid) {
2234 if (val >= 0)
2235 dev_err(hsotg->dev,
2236 "%d invalid for max_packet_count. Check HW configuration.\n",
2237 val);
2238 val = (1 << (width + 4)) - 1;
2239 dev_dbg(hsotg->dev, "Setting max_packet_count to %d\n", val);
2240 retval = -EINVAL;
2241 }
2242
2243 hsotg->core_params->max_packet_count = val;
2244 return retval;
2245}
2246
2247int dwc2_set_param_host_channels(struct dwc2_hsotg *hsotg, int val)
2248{
2249 int valid = 1;
2250 int retval = 0;
2251 int num_chan = hsotg->hwcfg2 >> GHWCFG2_NUM_HOST_CHAN_SHIFT &
2252 GHWCFG2_NUM_HOST_CHAN_MASK >> GHWCFG2_NUM_HOST_CHAN_SHIFT;
2253
2254 if (val < 1 || val > num_chan + 1)
2255 valid = 0;
2256
2257 if (!valid) {
2258 if (val >= 0)
2259 dev_err(hsotg->dev,
2260 "%d invalid for host_channels. Check HW configuration.\n",
2261 val);
2262 val = num_chan + 1;
2263 dev_dbg(hsotg->dev, "Setting host_channels to %d\n", val);
2264 retval = -EINVAL;
2265 }
2266
2267 hsotg->core_params->host_channels = val;
2268 return retval;
2269}
2270
2271int dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg, int val)
2272{
2273#ifndef NO_FS_PHY_HW_CHECKS
2274 int valid = 0;
2275 u32 hs_phy_type;
2276 u32 fs_phy_type;
2277#endif
2278 int retval = 0;
2279
2280 if (DWC2_PARAM_TEST(val, DWC2_PHY_TYPE_PARAM_FS,
2281 DWC2_PHY_TYPE_PARAM_ULPI)) {
2282 if (val >= 0) {
2283 dev_err(hsotg->dev, "Wrong value for phy_type\n");
2284 dev_err(hsotg->dev, "phy_type must be 0, 1 or 2\n");
2285 }
2286
2287#ifndef NO_FS_PHY_HW_CHECKS
2288 valid = 0;
2289#else
2290 val = 0;
2291 dev_dbg(hsotg->dev, "Setting phy_type to %d\n", val);
2292 retval = -EINVAL;
2293#endif
2294 }
2295
2296#ifndef NO_FS_PHY_HW_CHECKS
2297 hs_phy_type = hsotg->hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK;
2298 fs_phy_type = hsotg->hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK;
2299
2300 if (val == DWC2_PHY_TYPE_PARAM_UTMI &&
2301 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
2302 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
2303 valid = 1;
2304 else if (val == DWC2_PHY_TYPE_PARAM_ULPI &&
2305 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI ||
2306 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
2307 valid = 1;
2308 else if (val == DWC2_PHY_TYPE_PARAM_FS &&
2309 fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
2310 valid = 1;
2311
2312 if (!valid) {
2313 if (val >= 0)
2314 dev_err(hsotg->dev,
2315 "%d invalid for phy_type. Check HW configuration.\n",
2316 val);
2317 val = 0;
2318 if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
2319 if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
2320 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
2321 val = DWC2_PHY_TYPE_PARAM_UTMI;
2322 else
2323 val = DWC2_PHY_TYPE_PARAM_ULPI;
2324 }
2325 dev_dbg(hsotg->dev, "Setting phy_type to %d\n", val);
2326 retval = -EINVAL;
2327 }
2328#endif
2329
2330 hsotg->core_params->phy_type = val;
2331 return retval;
2332}
2333
2334static int dwc2_get_param_phy_type(struct dwc2_hsotg *hsotg)
2335{
2336 return hsotg->core_params->phy_type;
2337}
2338
2339int dwc2_set_param_speed(struct dwc2_hsotg *hsotg, int val)
2340{
2341 int valid = 1;
2342 int retval = 0;
2343
2344 if (DWC2_PARAM_TEST(val, 0, 1)) {
2345 if (val >= 0) {
2346 dev_err(hsotg->dev, "Wrong value for speed parameter\n");
2347 dev_err(hsotg->dev, "max_speed parameter must be 0 or 1\n");
2348 }
2349 valid = 0;
2350 }
2351
2352 if (val == 0 && dwc2_get_param_phy_type(hsotg) ==
2353 DWC2_PHY_TYPE_PARAM_FS)
2354 valid = 0;
2355
2356 if (!valid) {
2357 if (val >= 0)
2358 dev_err(hsotg->dev,
2359 "%d invalid for speed parameter. Check HW configuration.\n",
2360 val);
2361 val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS ?
2362 1 : 0;
2363 dev_dbg(hsotg->dev, "Setting speed to %d\n", val);
2364 retval = -EINVAL;
2365 }
2366
2367 hsotg->core_params->speed = val;
2368 return retval;
2369}
2370
2371int dwc2_set_param_host_ls_low_power_phy_clk(struct dwc2_hsotg *hsotg, int val)
2372{
2373 int valid = 1;
2374 int retval = 0;
2375
2376 if (DWC2_PARAM_TEST(val, DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ,
2377 DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)) {
2378 if (val >= 0) {
2379 dev_err(hsotg->dev,
2380 "Wrong value for host_ls_low_power_phy_clk parameter\n");
2381 dev_err(hsotg->dev,
2382 "host_ls_low_power_phy_clk must be 0 or 1\n");
2383 }
2384 valid = 0;
2385 }
2386
2387 if (val == DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ &&
2388 dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
2389 valid = 0;
2390
2391 if (!valid) {
2392 if (val >= 0)
2393 dev_err(hsotg->dev,
2394 "%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
2395 val);
2396 val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS
2397 ? DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ
2398 : DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
2399 dev_dbg(hsotg->dev, "Setting host_ls_low_power_phy_clk to %d\n",
2400 val);
2401 retval = -EINVAL;
2402 }
2403
2404 hsotg->core_params->host_ls_low_power_phy_clk = val;
2405 return retval;
2406}
2407
2408int dwc2_set_param_phy_ulpi_ddr(struct dwc2_hsotg *hsotg, int val)
2409{
2410 int retval = 0;
2411
2412 if (DWC2_PARAM_TEST(val, 0, 1)) {
2413 if (val >= 0) {
2414 dev_err(hsotg->dev, "Wrong value for phy_ulpi_ddr\n");
2415 dev_err(hsotg->dev, "phy_upli_ddr must be 0 or 1\n");
2416 }
2417 val = 0;
2418 dev_dbg(hsotg->dev, "Setting phy_upli_ddr to %d\n", val);
2419 retval = -EINVAL;
2420 }
2421
2422 hsotg->core_params->phy_ulpi_ddr = val;
2423 return retval;
2424}
2425
2426int dwc2_set_param_phy_ulpi_ext_vbus(struct dwc2_hsotg *hsotg, int val)
2427{
2428 int retval = 0;
2429
2430 if (DWC2_PARAM_TEST(val, 0, 1)) {
2431 if (val >= 0) {
2432 dev_err(hsotg->dev,
2433 "Wrong value for phy_ulpi_ext_vbus\n");
2434 dev_err(hsotg->dev,
2435 "phy_ulpi_ext_vbus must be 0 or 1\n");
2436 }
2437 val = 0;
2438 dev_dbg(hsotg->dev, "Setting phy_ulpi_ext_vbus to %d\n", val);
2439 retval = -EINVAL;
2440 }
2441
2442 hsotg->core_params->phy_ulpi_ext_vbus = val;
2443 return retval;
2444}
2445
2446int dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg, int val)
2447{
2448 int retval = 0;
2449
2450 if (DWC2_PARAM_TEST(val, 8, 8) && DWC2_PARAM_TEST(val, 16, 16)) {
2451 if (val >= 0) {
2452 dev_err(hsotg->dev, "Wrong value for phy_utmi_width\n");
2453 dev_err(hsotg->dev, "phy_utmi_width must be 8 or 16\n");
2454 }
2455 val = 8;
2456 dev_dbg(hsotg->dev, "Setting phy_utmi_width to %d\n", val);
2457 retval = -EINVAL;
2458 }
2459
2460 hsotg->core_params->phy_utmi_width = val;
2461 return retval;
2462}
2463
2464int dwc2_set_param_ulpi_fs_ls(struct dwc2_hsotg *hsotg, int val)
2465{
2466 int retval = 0;
2467
2468 if (DWC2_PARAM_TEST(val, 0, 1)) {
2469 if (val >= 0) {
2470 dev_err(hsotg->dev, "Wrong value for ulpi_fs_ls\n");
2471 dev_err(hsotg->dev, "ulpi_fs_ls must be 0 or 1\n");
2472 }
2473 val = 0;
2474 dev_dbg(hsotg->dev, "Setting ulpi_fs_ls to %d\n", val);
2475 retval = -EINVAL;
2476 }
2477
2478 hsotg->core_params->ulpi_fs_ls = val;
2479 return retval;
2480}
2481
2482int dwc2_set_param_ts_dline(struct dwc2_hsotg *hsotg, int val)
2483{
2484 int retval = 0;
2485
2486 if (DWC2_PARAM_TEST(val, 0, 1)) {
2487 if (val >= 0) {
2488 dev_err(hsotg->dev, "Wrong value for ts_dline\n");
2489 dev_err(hsotg->dev, "ts_dline must be 0 or 1\n");
2490 }
2491 val = 0;
2492 dev_dbg(hsotg->dev, "Setting ts_dline to %d\n", val);
2493 retval = -EINVAL;
2494 }
2495
2496 hsotg->core_params->ts_dline = val;
2497 return retval;
2498}
2499
2500int dwc2_set_param_i2c_enable(struct dwc2_hsotg *hsotg, int val)
2501{
2502#ifndef NO_FS_PHY_HW_CHECKS
2503 int valid = 1;
2504#endif
2505 int retval = 0;
2506
2507 if (DWC2_PARAM_TEST(val, 0, 1)) {
2508 if (val >= 0) {
2509 dev_err(hsotg->dev, "Wrong value for i2c_enable\n");
2510 dev_err(hsotg->dev, "i2c_enable must be 0 or 1\n");
2511 }
2512
2513#ifndef NO_FS_PHY_HW_CHECKS
2514 valid = 0;
2515#else
2516 val = 0;
2517 dev_dbg(hsotg->dev, "Setting i2c_enable to %d\n", val);
2518 retval = -EINVAL;
2519#endif
2520 }
2521
2522#ifndef NO_FS_PHY_HW_CHECKS
2523 if (val == 1 && !(hsotg->hwcfg3 & GHWCFG3_I2C))
2524 valid = 0;
2525
2526 if (!valid) {
2527 if (val >= 0)
2528 dev_err(hsotg->dev,
2529 "%d invalid for i2c_enable. Check HW configuration.\n",
2530 val);
2531 val = !!(hsotg->hwcfg3 & GHWCFG3_I2C);
2532 dev_dbg(hsotg->dev, "Setting i2c_enable to %d\n", val);
2533 retval = -EINVAL;
2534 }
2535#endif
2536
2537 hsotg->core_params->i2c_enable = val;
2538 return retval;
2539}
2540
2541int dwc2_set_param_en_multiple_tx_fifo(struct dwc2_hsotg *hsotg, int val)
2542{
2543 int valid = 1;
2544 int retval = 0;
2545
2546 if (DWC2_PARAM_TEST(val, 0, 1)) {
2547 if (val >= 0) {
2548 dev_err(hsotg->dev,
2549 "Wrong value for en_multiple_tx_fifo,\n");
2550 dev_err(hsotg->dev,
2551 "en_multiple_tx_fifo must be 0 or 1\n");
2552 }
2553 valid = 0;
2554 }
2555
2556 if (val == 1 && !(hsotg->hwcfg4 & GHWCFG4_DED_FIFO_EN))
2557 valid = 0;
2558
2559 if (!valid) {
2560 if (val >= 0)
2561 dev_err(hsotg->dev,
2562 "%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
2563 val);
2564 val = !!(hsotg->hwcfg4 & GHWCFG4_DED_FIFO_EN);
2565 dev_dbg(hsotg->dev, "Setting en_multiple_tx_fifo to %d\n", val);
2566 retval = -EINVAL;
2567 }
2568
2569 hsotg->core_params->en_multiple_tx_fifo = val;
2570 return retval;
2571}
2572
2573int dwc2_set_param_reload_ctl(struct dwc2_hsotg *hsotg, int val)
2574{
2575 int valid = 1;
2576 int retval = 0;
2577
2578 if (DWC2_PARAM_TEST(val, 0, 1)) {
2579 if (val >= 0) {
2580 dev_err(hsotg->dev,
2581 "'%d' invalid for parameter reload_ctl\n", val);
2582 dev_err(hsotg->dev, "reload_ctl must be 0 or 1\n");
2583 }
2584 valid = 0;
2585 }
2586
2587 if (val == 1 && hsotg->snpsid < DWC2_CORE_REV_2_92a)
2588 valid = 0;
2589
2590 if (!valid) {
2591 if (val >= 0)
2592 dev_err(hsotg->dev,
2593 "%d invalid for parameter reload_ctl. Check HW configuration.\n",
2594 val);
2595 val = hsotg->snpsid >= DWC2_CORE_REV_2_92a;
2596 dev_dbg(hsotg->dev, "Setting reload_ctl to %d\n", val);
2597 retval = -EINVAL;
2598 }
2599
2600 hsotg->core_params->reload_ctl = val;
2601 return retval;
2602}
2603
2604int dwc2_set_param_ahb_single(struct dwc2_hsotg *hsotg, int val)
2605{
2606 int valid = 1;
2607 int retval = 0;
2608
2609 if (DWC2_PARAM_TEST(val, 0, 1)) {
2610 if (val >= 0) {
2611 dev_err(hsotg->dev,
2612 "'%d' invalid for parameter ahb_single\n", val);
2613 dev_err(hsotg->dev, "ahb_single must be 0 or 1\n");
2614 }
2615 valid = 0;
2616 }
2617
2618 if (val > 0 && hsotg->snpsid < DWC2_CORE_REV_2_94a)
2619 valid = 0;
2620
2621 if (!valid) {
2622 if (val >= 0)
2623 dev_err(hsotg->dev,
2624 "%d invalid for parameter ahb_single. Check HW configuration.\n",
2625 val);
2626 val = 0;
2627 dev_dbg(hsotg->dev, "Setting ahb_single to %d\n", val);
2628 retval = -EINVAL;
2629 }
2630
2631 hsotg->core_params->ahb_single = val;
2632 return retval;
2633}
2634
2635int dwc2_set_param_otg_ver(struct dwc2_hsotg *hsotg, int val)
2636{
2637 int retval = 0;
2638
2639 if (DWC2_PARAM_TEST(val, 0, 1)) {
2640 if (val >= 0) {
2641 dev_err(hsotg->dev,
2642 "'%d' invalid for parameter otg_ver\n", val);
2643 dev_err(hsotg->dev,
2644 "otg_ver must be 0 (for OTG 1.3 support) or 1 (for OTG 2.0 support)\n");
2645 }
2646 val = 0;
2647 dev_dbg(hsotg->dev, "Setting otg_ver to %d\n", val);
2648 retval = -EINVAL;
2649 }
2650
2651 hsotg->core_params->otg_ver = val;
2652 return retval;
2653}
2654
2655/*
2656 * This function is called during module intialization to pass module parameters
2657 * for the DWC_otg core. It returns non-0 if any parameters are invalid.
2658 */
2659int dwc2_set_parameters(struct dwc2_hsotg *hsotg,
2660 struct dwc2_core_params *params)
2661{
2662 int retval = 0;
2663
2664 dev_dbg(hsotg->dev, "%s()\n", __func__);
2665
2666 retval |= dwc2_set_param_otg_cap(hsotg, params->otg_cap);
2667 retval |= dwc2_set_param_dma_enable(hsotg, params->dma_enable);
2668 retval |= dwc2_set_param_dma_desc_enable(hsotg,
2669 params->dma_desc_enable);
2670 retval |= dwc2_set_param_host_support_fs_ls_low_power(hsotg,
2671 params->host_support_fs_ls_low_power);
2672 retval |= dwc2_set_param_enable_dynamic_fifo(hsotg,
2673 params->enable_dynamic_fifo);
2674 retval |= dwc2_set_param_host_rx_fifo_size(hsotg,
2675 params->host_rx_fifo_size);
2676 retval |= dwc2_set_param_host_nperio_tx_fifo_size(hsotg,
2677 params->host_nperio_tx_fifo_size);
2678 retval |= dwc2_set_param_host_perio_tx_fifo_size(hsotg,
2679 params->host_perio_tx_fifo_size);
2680 retval |= dwc2_set_param_max_transfer_size(hsotg,
2681 params->max_transfer_size);
2682 retval |= dwc2_set_param_max_packet_count(hsotg,
2683 params->max_packet_count);
2684 retval |= dwc2_set_param_host_channels(hsotg, params->host_channels);
2685 retval |= dwc2_set_param_phy_type(hsotg, params->phy_type);
2686 retval |= dwc2_set_param_speed(hsotg, params->speed);
2687 retval |= dwc2_set_param_host_ls_low_power_phy_clk(hsotg,
2688 params->host_ls_low_power_phy_clk);
2689 retval |= dwc2_set_param_phy_ulpi_ddr(hsotg, params->phy_ulpi_ddr);
2690 retval |= dwc2_set_param_phy_ulpi_ext_vbus(hsotg,
2691 params->phy_ulpi_ext_vbus);
2692 retval |= dwc2_set_param_phy_utmi_width(hsotg, params->phy_utmi_width);
2693 retval |= dwc2_set_param_ulpi_fs_ls(hsotg, params->ulpi_fs_ls);
2694 retval |= dwc2_set_param_ts_dline(hsotg, params->ts_dline);
2695 retval |= dwc2_set_param_i2c_enable(hsotg, params->i2c_enable);
2696 retval |= dwc2_set_param_en_multiple_tx_fifo(hsotg,
2697 params->en_multiple_tx_fifo);
2698 retval |= dwc2_set_param_reload_ctl(hsotg, params->reload_ctl);
2699 retval |= dwc2_set_param_ahb_single(hsotg, params->ahb_single);
2700 retval |= dwc2_set_param_otg_ver(hsotg, params->otg_ver);
2701
2702 return retval;
2703}
2704
2705u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg)
2706{
2707 return (u16)(hsotg->core_params->otg_ver == 1 ? 0x0200 : 0x0103);
2708}
2709
2710int dwc2_check_core_status(struct dwc2_hsotg *hsotg)
2711{
2712 if (readl(hsotg->regs + GSNPSID) == 0xffffffff)
2713 return -1;
2714 else
2715 return 0;
2716}
2717
2718/**
2719 * dwc2_enable_global_interrupts() - Enables the controller's Global
2720 * Interrupt in the AHB Config register
2721 *
2722 * @hsotg: Programming view of DWC_otg controller
2723 */
2724void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
2725{
2726 u32 ahbcfg = readl(hsotg->regs + GAHBCFG);
2727
2728 ahbcfg |= GAHBCFG_GLBL_INTR_EN;
2729 writel(ahbcfg, hsotg->regs + GAHBCFG);
2730}
2731
2732/**
2733 * dwc2_disable_global_interrupts() - Disables the controller's Global
2734 * Interrupt in the AHB Config register
2735 *
2736 * @hsotg: Programming view of DWC_otg controller
2737 */
2738void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
2739{
2740 u32 ahbcfg = readl(hsotg->regs + GAHBCFG);
2741
2742 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
2743 writel(ahbcfg, hsotg->regs + GAHBCFG);
2744}
2745
2746MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
2747MODULE_AUTHOR("Synopsys, Inc.");
2748MODULE_LICENSE("Dual BSD/GPL");