blob: 889dc5fad47c9ce41d21fdb09a268a11f9935285 [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
Gregory Herrerod17ee772015-04-29 22:09:01 +020059#if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
60/**
61 * dwc2_backup_host_registers() - Backup controller host registers.
62 * When suspending usb bus, registers needs to be backuped
63 * if controller power is disabled once suspended.
64 *
65 * @hsotg: Programming view of the DWC_otg controller
66 */
67static int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
68{
69 struct dwc2_hregs_backup *hr;
70 int i;
71
72 dev_dbg(hsotg->dev, "%s\n", __func__);
73
74 /* Backup Host regs */
75 hr = hsotg->hr_backup;
76 if (!hr) {
77 hr = devm_kzalloc(hsotg->dev, sizeof(*hr), GFP_KERNEL);
78 if (!hr) {
79 dev_err(hsotg->dev, "%s: can't allocate host regs\n",
80 __func__);
81 return -ENOMEM;
82 }
83
84 hsotg->hr_backup = hr;
85 }
86 hr->hcfg = readl(hsotg->regs + HCFG);
87 hr->haintmsk = readl(hsotg->regs + HAINTMSK);
88 for (i = 0; i < hsotg->core_params->host_channels; ++i)
89 hr->hcintmsk[i] = readl(hsotg->regs + HCINTMSK(i));
90
91 hr->hprt0 = readl(hsotg->regs + HPRT0);
92 hr->hfir = readl(hsotg->regs + HFIR);
93
94 return 0;
95}
96
97/**
98 * dwc2_restore_host_registers() - Restore controller host registers.
99 * When resuming usb bus, device registers needs to be restored
100 * if controller power were disabled.
101 *
102 * @hsotg: Programming view of the DWC_otg controller
103 */
104static int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
105{
106 struct dwc2_hregs_backup *hr;
107 int i;
108
109 dev_dbg(hsotg->dev, "%s\n", __func__);
110
111 /* Restore host regs */
112 hr = hsotg->hr_backup;
113 if (!hr) {
114 dev_err(hsotg->dev, "%s: no host registers to restore\n",
115 __func__);
116 return -EINVAL;
117 }
118
119 writel(hr->hcfg, hsotg->regs + HCFG);
120 writel(hr->haintmsk, hsotg->regs + HAINTMSK);
121
122 for (i = 0; i < hsotg->core_params->host_channels; ++i)
123 writel(hr->hcintmsk[i], hsotg->regs + HCINTMSK(i));
124
125 writel(hr->hprt0, hsotg->regs + HPRT0);
126 writel(hr->hfir, hsotg->regs + HFIR);
127
128 return 0;
129}
130#else
131static inline int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
132{ return 0; }
133
134static inline int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
135{ return 0; }
136#endif
137
138#if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
139 IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
140/**
141 * dwc2_backup_device_registers() - Backup controller device registers.
142 * When suspending usb bus, registers needs to be backuped
143 * if controller power is disabled once suspended.
144 *
145 * @hsotg: Programming view of the DWC_otg controller
146 */
147static int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
148{
149 struct dwc2_dregs_backup *dr;
150 int i;
151
152 dev_dbg(hsotg->dev, "%s\n", __func__);
153
154 /* Backup dev regs */
155 dr = hsotg->dr_backup;
156 if (!dr) {
157 dr = devm_kzalloc(hsotg->dev, sizeof(*dr), GFP_KERNEL);
158 if (!dr) {
159 dev_err(hsotg->dev, "%s: can't allocate device regs\n",
160 __func__);
161 return -ENOMEM;
162 }
163
164 hsotg->dr_backup = dr;
165 }
166
167 dr->dcfg = readl(hsotg->regs + DCFG);
168 dr->dctl = readl(hsotg->regs + DCTL);
169 dr->daintmsk = readl(hsotg->regs + DAINTMSK);
170 dr->diepmsk = readl(hsotg->regs + DIEPMSK);
171 dr->doepmsk = readl(hsotg->regs + DOEPMSK);
172
173 for (i = 0; i < hsotg->num_of_eps; i++) {
174 /* Backup IN EPs */
175 dr->diepctl[i] = readl(hsotg->regs + DIEPCTL(i));
176
177 /* Ensure DATA PID is correctly configured */
178 if (dr->diepctl[i] & DXEPCTL_DPID)
179 dr->diepctl[i] |= DXEPCTL_SETD1PID;
180 else
181 dr->diepctl[i] |= DXEPCTL_SETD0PID;
182
183 dr->dieptsiz[i] = readl(hsotg->regs + DIEPTSIZ(i));
184 dr->diepdma[i] = readl(hsotg->regs + DIEPDMA(i));
185
186 /* Backup OUT EPs */
187 dr->doepctl[i] = readl(hsotg->regs + DOEPCTL(i));
188
189 /* Ensure DATA PID is correctly configured */
190 if (dr->doepctl[i] & DXEPCTL_DPID)
191 dr->doepctl[i] |= DXEPCTL_SETD1PID;
192 else
193 dr->doepctl[i] |= DXEPCTL_SETD0PID;
194
195 dr->doeptsiz[i] = readl(hsotg->regs + DOEPTSIZ(i));
196 dr->doepdma[i] = readl(hsotg->regs + DOEPDMA(i));
197 }
198
199 return 0;
200}
201
202/**
203 * dwc2_restore_device_registers() - Restore controller device registers.
204 * When resuming usb bus, device registers needs to be restored
205 * if controller power were disabled.
206 *
207 * @hsotg: Programming view of the DWC_otg controller
208 */
209static int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
210{
211 struct dwc2_dregs_backup *dr;
212 u32 dctl;
213 int i;
214
215 dev_dbg(hsotg->dev, "%s\n", __func__);
216
217 /* Restore dev regs */
218 dr = hsotg->dr_backup;
219 if (!dr) {
220 dev_err(hsotg->dev, "%s: no device registers to restore\n",
221 __func__);
222 return -EINVAL;
223 }
224
225 writel(dr->dcfg, hsotg->regs + DCFG);
226 writel(dr->dctl, hsotg->regs + DCTL);
227 writel(dr->daintmsk, hsotg->regs + DAINTMSK);
228 writel(dr->diepmsk, hsotg->regs + DIEPMSK);
229 writel(dr->doepmsk, hsotg->regs + DOEPMSK);
230
231 for (i = 0; i < hsotg->num_of_eps; i++) {
232 /* Restore IN EPs */
233 writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
234 writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
235 writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
236
237 /* Restore OUT EPs */
238 writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
239 writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
240 writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
241 }
242
243 /* Set the Power-On Programming done bit */
244 dctl = readl(hsotg->regs + DCTL);
245 dctl |= DCTL_PWRONPRGDONE;
246 writel(dctl, hsotg->regs + DCTL);
247
248 return 0;
249}
250#else
251static inline int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
252{ return 0; }
253
254static inline int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
255{ return 0; }
256#endif
257
258/**
259 * dwc2_backup_global_registers() - Backup global controller registers.
260 * When suspending usb bus, registers needs to be backuped
261 * if controller power is disabled once suspended.
262 *
263 * @hsotg: Programming view of the DWC_otg controller
264 */
265static int dwc2_backup_global_registers(struct dwc2_hsotg *hsotg)
266{
267 struct dwc2_gregs_backup *gr;
268 int i;
269
270 /* Backup global regs */
271 gr = hsotg->gr_backup;
272 if (!gr) {
273 gr = devm_kzalloc(hsotg->dev, sizeof(*gr), GFP_KERNEL);
274 if (!gr) {
275 dev_err(hsotg->dev, "%s: can't allocate global regs\n",
276 __func__);
277 return -ENOMEM;
278 }
279
280 hsotg->gr_backup = gr;
281 }
282
283 gr->gotgctl = readl(hsotg->regs + GOTGCTL);
284 gr->gintmsk = readl(hsotg->regs + GINTMSK);
285 gr->gahbcfg = readl(hsotg->regs + GAHBCFG);
286 gr->gusbcfg = readl(hsotg->regs + GUSBCFG);
287 gr->grxfsiz = readl(hsotg->regs + GRXFSIZ);
288 gr->gnptxfsiz = readl(hsotg->regs + GNPTXFSIZ);
289 gr->hptxfsiz = readl(hsotg->regs + HPTXFSIZ);
290 gr->gdfifocfg = readl(hsotg->regs + GDFIFOCFG);
291 for (i = 0; i < MAX_EPS_CHANNELS; i++)
292 gr->dtxfsiz[i] = readl(hsotg->regs + DPTXFSIZN(i));
293
294 return 0;
295}
296
297/**
298 * dwc2_restore_global_registers() - Restore controller global registers.
299 * When resuming usb bus, device registers needs to be restored
300 * if controller power were disabled.
301 *
302 * @hsotg: Programming view of the DWC_otg controller
303 */
304static int dwc2_restore_global_registers(struct dwc2_hsotg *hsotg)
305{
306 struct dwc2_gregs_backup *gr;
307 int i;
308
309 dev_dbg(hsotg->dev, "%s\n", __func__);
310
311 /* Restore global regs */
312 gr = hsotg->gr_backup;
313 if (!gr) {
314 dev_err(hsotg->dev, "%s: no global registers to restore\n",
315 __func__);
316 return -EINVAL;
317 }
318
319 writel(0xffffffff, hsotg->regs + GINTSTS);
320 writel(gr->gotgctl, hsotg->regs + GOTGCTL);
321 writel(gr->gintmsk, hsotg->regs + GINTMSK);
322 writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
323 writel(gr->gahbcfg, hsotg->regs + GAHBCFG);
324 writel(gr->grxfsiz, hsotg->regs + GRXFSIZ);
325 writel(gr->gnptxfsiz, hsotg->regs + GNPTXFSIZ);
326 writel(gr->hptxfsiz, hsotg->regs + HPTXFSIZ);
327 writel(gr->gdfifocfg, hsotg->regs + GDFIFOCFG);
328 for (i = 0; i < MAX_EPS_CHANNELS; i++)
329 writel(gr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
330
331 return 0;
332}
333
334/**
335 * dwc2_exit_hibernation() - Exit controller from Partial Power Down.
336 *
337 * @hsotg: Programming view of the DWC_otg controller
338 * @restore: Controller registers need to be restored
339 */
340int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, bool restore)
341{
342 u32 pcgcctl;
343 int ret = 0;
344
345 pcgcctl = readl(hsotg->regs + PCGCTL);
346 pcgcctl &= ~PCGCTL_STOPPCLK;
347 writel(pcgcctl, hsotg->regs + PCGCTL);
348
349 pcgcctl = readl(hsotg->regs + PCGCTL);
350 pcgcctl &= ~PCGCTL_PWRCLMP;
351 writel(pcgcctl, hsotg->regs + PCGCTL);
352
353 pcgcctl = readl(hsotg->regs + PCGCTL);
354 pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
355 writel(pcgcctl, hsotg->regs + PCGCTL);
356
357 udelay(100);
358 if (restore) {
359 ret = dwc2_restore_global_registers(hsotg);
360 if (ret) {
361 dev_err(hsotg->dev, "%s: failed to restore registers\n",
362 __func__);
363 return ret;
364 }
365 if (dwc2_is_host_mode(hsotg)) {
366 ret = dwc2_restore_host_registers(hsotg);
367 if (ret) {
368 dev_err(hsotg->dev, "%s: failed to restore host registers\n",
369 __func__);
370 return ret;
371 }
372 } else {
373 ret = dwc2_restore_device_registers(hsotg);
374 if (ret) {
375 dev_err(hsotg->dev, "%s: failed to restore device registers\n",
376 __func__);
377 return ret;
378 }
379 }
380 }
381
382 return ret;
383}
384
385/**
386 * dwc2_enter_hibernation() - Put controller in Partial Power Down.
387 *
388 * @hsotg: Programming view of the DWC_otg controller
389 */
390int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg)
391{
392 u32 pcgcctl;
393 int ret = 0;
394
395 /* Backup all registers */
396 ret = dwc2_backup_global_registers(hsotg);
397 if (ret) {
398 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
399 __func__);
400 return ret;
401 }
402
403 if (dwc2_is_host_mode(hsotg)) {
404 ret = dwc2_backup_host_registers(hsotg);
405 if (ret) {
406 dev_err(hsotg->dev, "%s: failed to backup host registers\n",
407 __func__);
408 return ret;
409 }
410 } else {
411 ret = dwc2_backup_device_registers(hsotg);
412 if (ret) {
413 dev_err(hsotg->dev, "%s: failed to backup device registers\n",
414 __func__);
415 return ret;
416 }
417 }
418
419 /* Put the controller in low power state */
420 pcgcctl = readl(hsotg->regs + PCGCTL);
421
422 pcgcctl |= PCGCTL_PWRCLMP;
423 writel(pcgcctl, hsotg->regs + PCGCTL);
424 ndelay(20);
425
426 pcgcctl |= PCGCTL_RSTPDWNMODULE;
427 writel(pcgcctl, hsotg->regs + PCGCTL);
428 ndelay(20);
429
430 pcgcctl |= PCGCTL_STOPPCLK;
431 writel(pcgcctl, hsotg->regs + PCGCTL);
432
433 return ret;
434}
435
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700436/**
437 * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
438 * used in both device and host modes
439 *
440 * @hsotg: Programming view of the DWC_otg controller
441 */
442static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
443{
444 u32 intmsk;
445
446 /* Clear any pending OTG Interrupts */
447 writel(0xffffffff, hsotg->regs + GOTGINT);
448
449 /* Clear any pending interrupts */
450 writel(0xffffffff, hsotg->regs + GINTSTS);
451
452 /* Enable the interrupts in the GINTMSK */
453 intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
454
455 if (hsotg->core_params->dma_enable <= 0)
456 intmsk |= GINTSTS_RXFLVL;
457
458 intmsk |= GINTSTS_CONIDSTSCHNG | GINTSTS_WKUPINT | GINTSTS_USBSUSP |
459 GINTSTS_SESSREQINT;
460
461 writel(intmsk, hsotg->regs + GINTMSK);
462}
463
464/*
465 * Initializes the FSLSPClkSel field of the HCFG register depending on the
466 * PHY type
467 */
468static void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
469{
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700470 u32 hcfg, val;
471
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200472 if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
473 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700474 hsotg->core_params->ulpi_fs_ls > 0) ||
475 hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
476 /* Full speed PHY */
477 val = HCFG_FSLSPCLKSEL_48_MHZ;
478 } else {
479 /* High speed PHY running at full speed or high speed */
480 val = HCFG_FSLSPCLKSEL_30_60_MHZ;
481 }
482
483 dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
484 hcfg = readl(hsotg->regs + HCFG);
485 hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200486 hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700487 writel(hcfg, hsotg->regs + HCFG);
488}
489
490/*
491 * Do core a soft reset of the core. Be careful with this because it
492 * resets all the internal state machines of the core.
493 */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100494static int dwc2_core_reset(struct dwc2_hsotg *hsotg)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700495{
496 u32 greset;
497 int count = 0;
Kever Yangc0155b92014-08-06 09:01:50 +0800498 u32 gusbcfg;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700499
500 dev_vdbg(hsotg->dev, "%s()\n", __func__);
501
502 /* Wait for AHB master IDLE state */
503 do {
504 usleep_range(20000, 40000);
505 greset = readl(hsotg->regs + GRSTCTL);
506 if (++count > 50) {
507 dev_warn(hsotg->dev,
508 "%s() HANG! AHB Idle GRSTCTL=%0x\n",
509 __func__, greset);
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100510 return -EBUSY;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700511 }
512 } while (!(greset & GRSTCTL_AHBIDLE));
513
514 /* Core Soft Reset */
515 count = 0;
516 greset |= GRSTCTL_CSFTRST;
517 writel(greset, hsotg->regs + GRSTCTL);
518 do {
519 usleep_range(20000, 40000);
520 greset = readl(hsotg->regs + GRSTCTL);
521 if (++count > 50) {
522 dev_warn(hsotg->dev,
523 "%s() HANG! Soft Reset GRSTCTL=%0x\n",
524 __func__, greset);
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100525 return -EBUSY;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700526 }
527 } while (greset & GRSTCTL_CSFTRST);
528
Kever Yangc0155b92014-08-06 09:01:50 +0800529 if (hsotg->dr_mode == USB_DR_MODE_HOST) {
530 gusbcfg = readl(hsotg->regs + GUSBCFG);
531 gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
532 gusbcfg |= GUSBCFG_FORCEHOSTMODE;
533 writel(gusbcfg, hsotg->regs + GUSBCFG);
534 } else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
535 gusbcfg = readl(hsotg->regs + GUSBCFG);
536 gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
537 gusbcfg |= GUSBCFG_FORCEDEVMODE;
538 writel(gusbcfg, hsotg->regs + GUSBCFG);
539 } else if (hsotg->dr_mode == USB_DR_MODE_OTG) {
540 gusbcfg = readl(hsotg->regs + GUSBCFG);
541 gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
542 gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
543 writel(gusbcfg, hsotg->regs + GUSBCFG);
544 }
545
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700546 /*
547 * NOTE: This long sleep is _very_ important, otherwise the core will
548 * not stay in host mode after a connector ID change!
549 */
550 usleep_range(150000, 200000);
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100551
552 return 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700553}
554
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100555static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700556{
557 u32 usbcfg, i2cctl;
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100558 int retval = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700559
560 /*
561 * core_init() is now called on every switch so only call the
562 * following for the first time through
563 */
564 if (select_phy) {
565 dev_dbg(hsotg->dev, "FS PHY selected\n");
566 usbcfg = readl(hsotg->regs + GUSBCFG);
567 usbcfg |= GUSBCFG_PHYSEL;
568 writel(usbcfg, hsotg->regs + GUSBCFG);
569
570 /* Reset after a PHY select */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100571 retval = dwc2_core_reset(hsotg);
572 if (retval) {
573 dev_err(hsotg->dev, "%s() Reset failed, aborting",
574 __func__);
575 return retval;
576 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700577 }
578
579 /*
580 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
581 * do this on HNP Dev/Host mode switches (done in dev_init and
582 * host_init).
583 */
584 if (dwc2_is_host_mode(hsotg))
585 dwc2_init_fs_ls_pclk_sel(hsotg);
586
587 if (hsotg->core_params->i2c_enable > 0) {
588 dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
589
590 /* Program GUSBCFG.OtgUtmiFsSel to I2C */
591 usbcfg = readl(hsotg->regs + GUSBCFG);
592 usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
593 writel(usbcfg, hsotg->regs + GUSBCFG);
594
595 /* Program GI2CCTL.I2CEn */
596 i2cctl = readl(hsotg->regs + GI2CCTL);
597 i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
598 i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
599 i2cctl &= ~GI2CCTL_I2CEN;
600 writel(i2cctl, hsotg->regs + GI2CCTL);
601 i2cctl |= GI2CCTL_I2CEN;
602 writel(i2cctl, hsotg->regs + GI2CCTL);
603 }
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100604
605 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700606}
607
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100608static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700609{
610 u32 usbcfg;
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100611 int retval = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700612
613 if (!select_phy)
Paul Zimmermana23666c2014-02-04 11:42:15 -0800614 return 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700615
616 usbcfg = readl(hsotg->regs + GUSBCFG);
617
618 /*
619 * HS PHY parameters. These parameters are preserved during soft reset
620 * so only program the first time. Do a soft reset immediately after
621 * setting phyif.
622 */
623 switch (hsotg->core_params->phy_type) {
624 case DWC2_PHY_TYPE_PARAM_ULPI:
625 /* ULPI interface */
626 dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
627 usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
628 usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
629 if (hsotg->core_params->phy_ulpi_ddr > 0)
630 usbcfg |= GUSBCFG_DDRSEL;
631 break;
632 case DWC2_PHY_TYPE_PARAM_UTMI:
633 /* UTMI+ interface */
634 dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
635 usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
636 if (hsotg->core_params->phy_utmi_width == 16)
637 usbcfg |= GUSBCFG_PHYIF16;
638 break;
639 default:
640 dev_err(hsotg->dev, "FS PHY selected at HS!\n");
641 break;
642 }
643
644 writel(usbcfg, hsotg->regs + GUSBCFG);
645
646 /* Reset after setting the PHY parameters */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100647 retval = dwc2_core_reset(hsotg);
648 if (retval) {
649 dev_err(hsotg->dev, "%s() Reset failed, aborting",
650 __func__);
651 return retval;
652 }
653
654 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700655}
656
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100657static int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700658{
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200659 u32 usbcfg;
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100660 int retval = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700661
662 if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL &&
663 hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
664 /* If FS mode with FS PHY */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100665 retval = dwc2_fs_phy_init(hsotg, select_phy);
666 if (retval)
667 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700668 } else {
669 /* High speed PHY */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100670 retval = dwc2_hs_phy_init(hsotg, select_phy);
671 if (retval)
672 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700673 }
674
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200675 if (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
676 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700677 hsotg->core_params->ulpi_fs_ls > 0) {
678 dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
679 usbcfg = readl(hsotg->regs + GUSBCFG);
680 usbcfg |= GUSBCFG_ULPI_FS_LS;
681 usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
682 writel(usbcfg, hsotg->regs + GUSBCFG);
683 } else {
684 usbcfg = readl(hsotg->regs + GUSBCFG);
685 usbcfg &= ~GUSBCFG_ULPI_FS_LS;
686 usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
687 writel(usbcfg, hsotg->regs + GUSBCFG);
688 }
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100689
690 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700691}
692
693static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
694{
Paul Zimmerman4d3190e2013-07-16 12:22:12 -0700695 u32 ahbcfg = readl(hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700696
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200697 switch (hsotg->hw_params.arch) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700698 case GHWCFG2_EXT_DMA_ARCH:
699 dev_err(hsotg->dev, "External DMA Mode not supported\n");
700 return -EINVAL;
701
702 case GHWCFG2_INT_DMA_ARCH:
703 dev_dbg(hsotg->dev, "Internal DMA Mode\n");
Paul Zimmerman4d3190e2013-07-16 12:22:12 -0700704 if (hsotg->core_params->ahbcfg != -1) {
705 ahbcfg &= GAHBCFG_CTRL_MASK;
706 ahbcfg |= hsotg->core_params->ahbcfg &
707 ~GAHBCFG_CTRL_MASK;
708 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700709 break;
710
711 case GHWCFG2_SLAVE_ONLY_ARCH:
712 default:
713 dev_dbg(hsotg->dev, "Slave Only Mode\n");
714 break;
715 }
716
717 dev_dbg(hsotg->dev, "dma_enable:%d dma_desc_enable:%d\n",
718 hsotg->core_params->dma_enable,
719 hsotg->core_params->dma_desc_enable);
720
721 if (hsotg->core_params->dma_enable > 0) {
722 if (hsotg->core_params->dma_desc_enable > 0)
723 dev_dbg(hsotg->dev, "Using Descriptor DMA mode\n");
724 else
725 dev_dbg(hsotg->dev, "Using Buffer DMA mode\n");
726 } else {
727 dev_dbg(hsotg->dev, "Using Slave mode\n");
728 hsotg->core_params->dma_desc_enable = 0;
729 }
730
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700731 if (hsotg->core_params->dma_enable > 0)
732 ahbcfg |= GAHBCFG_DMA_EN;
733
734 writel(ahbcfg, hsotg->regs + GAHBCFG);
735
736 return 0;
737}
738
739static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
740{
741 u32 usbcfg;
742
743 usbcfg = readl(hsotg->regs + GUSBCFG);
744 usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
745
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200746 switch (hsotg->hw_params.op_mode) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700747 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
748 if (hsotg->core_params->otg_cap ==
749 DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
750 usbcfg |= GUSBCFG_HNPCAP;
751 if (hsotg->core_params->otg_cap !=
752 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
753 usbcfg |= GUSBCFG_SRPCAP;
754 break;
755
756 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
757 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
758 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
759 if (hsotg->core_params->otg_cap !=
760 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
761 usbcfg |= GUSBCFG_SRPCAP;
762 break;
763
764 case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
765 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
766 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
767 default:
768 break;
769 }
770
771 writel(usbcfg, hsotg->regs + GUSBCFG);
772}
773
774/**
775 * dwc2_core_init() - Initializes the DWC_otg controller registers and
776 * prepares the core for device mode or host mode operation
777 *
778 * @hsotg: Programming view of the DWC_otg controller
779 * @select_phy: If true then also set the Phy type
Matthijs Kooijman6706c722013-04-11 17:52:41 +0200780 * @irq: If >= 0, the irq to register
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700781 */
Matthijs Kooijman6706c722013-04-11 17:52:41 +0200782int dwc2_core_init(struct dwc2_hsotg *hsotg, bool select_phy, int irq)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700783{
784 u32 usbcfg, otgctl;
785 int retval;
786
787 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
788
789 usbcfg = readl(hsotg->regs + GUSBCFG);
790
791 /* Set ULPI External VBUS bit if needed */
792 usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
793 if (hsotg->core_params->phy_ulpi_ext_vbus ==
794 DWC2_PHY_ULPI_EXTERNAL_VBUS)
795 usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
796
797 /* Set external TS Dline pulsing bit if needed */
798 usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
799 if (hsotg->core_params->ts_dline > 0)
800 usbcfg |= GUSBCFG_TERMSELDLPULSE;
801
802 writel(usbcfg, hsotg->regs + GUSBCFG);
803
804 /* Reset the Controller */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100805 retval = dwc2_core_reset(hsotg);
806 if (retval) {
807 dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
808 __func__);
809 return retval;
810 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700811
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700812 /*
813 * This needs to happen in FS mode before any other programming occurs
814 */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100815 retval = dwc2_phy_init(hsotg, select_phy);
816 if (retval)
817 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700818
819 /* Program the GAHBCFG Register */
820 retval = dwc2_gahbcfg_init(hsotg);
821 if (retval)
822 return retval;
823
824 /* Program the GUSBCFG register */
825 dwc2_gusbcfg_init(hsotg);
826
827 /* Program the GOTGCTL register */
828 otgctl = readl(hsotg->regs + GOTGCTL);
829 otgctl &= ~GOTGCTL_OTGVER;
830 if (hsotg->core_params->otg_ver > 0)
831 otgctl |= GOTGCTL_OTGVER;
832 writel(otgctl, hsotg->regs + GOTGCTL);
833 dev_dbg(hsotg->dev, "OTG VER PARAM: %d\n", hsotg->core_params->otg_ver);
834
835 /* Clear the SRP success bit for FS-I2c */
836 hsotg->srp_success = 0;
837
838 /* Enable common interrupts */
839 dwc2_enable_common_interrupts(hsotg);
840
841 /*
Mickael Maison997f4f82014-12-23 17:39:45 +0100842 * Do device or host initialization based on mode during PCD and
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700843 * HCD initialization
844 */
845 if (dwc2_is_host_mode(hsotg)) {
846 dev_dbg(hsotg->dev, "Host Mode\n");
847 hsotg->op_state = OTG_STATE_A_HOST;
848 } else {
849 dev_dbg(hsotg->dev, "Device Mode\n");
850 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
851 }
852
853 return 0;
854}
855
856/**
857 * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
858 *
859 * @hsotg: Programming view of DWC_otg controller
860 */
861void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
862{
863 u32 intmsk;
864
865 dev_dbg(hsotg->dev, "%s()\n", __func__);
866
867 /* Disable all interrupts */
868 writel(0, hsotg->regs + GINTMSK);
869 writel(0, hsotg->regs + HAINTMSK);
870
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700871 /* Enable the common interrupts */
872 dwc2_enable_common_interrupts(hsotg);
873
874 /* Enable host mode interrupts without disturbing common interrupts */
875 intmsk = readl(hsotg->regs + GINTMSK);
876 intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
877 writel(intmsk, hsotg->regs + GINTMSK);
878}
879
880/**
881 * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
882 *
883 * @hsotg: Programming view of DWC_otg controller
884 */
885void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
886{
887 u32 intmsk = readl(hsotg->regs + GINTMSK);
888
889 /* Disable host mode interrupts without disturbing common interrupts */
890 intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
891 GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP);
892 writel(intmsk, hsotg->regs + GINTMSK);
893}
894
Dinh Nguyen112fe8e2014-05-07 08:31:29 -0500895/*
896 * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
897 * For system that have a total fifo depth that is smaller than the default
898 * RX + TX fifo size.
899 *
900 * @hsotg: Programming view of DWC_otg controller
901 */
902static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
903{
904 struct dwc2_core_params *params = hsotg->core_params;
905 struct dwc2_hw_params *hw = &hsotg->hw_params;
906 u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
907
908 total_fifo_size = hw->total_fifo_size;
909 rxfsiz = params->host_rx_fifo_size;
910 nptxfsiz = params->host_nperio_tx_fifo_size;
911 ptxfsiz = params->host_perio_tx_fifo_size;
912
913 /*
914 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
915 * allocation with support for high bandwidth endpoints. Synopsys
916 * defines MPS(Max Packet size) for a periodic EP=1024, and for
917 * non-periodic as 512.
918 */
919 if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
920 /*
921 * For Buffer DMA mode/Scatter Gather DMA mode
922 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
923 * with n = number of host channel.
924 * 2 * ((1024/4) + 2) = 516
925 */
926 rxfsiz = 516 + hw->host_channels;
927
928 /*
929 * min non-periodic tx fifo depth
930 * 2 * (largest non-periodic USB packet used / 4)
931 * 2 * (512/4) = 256
932 */
933 nptxfsiz = 256;
934
935 /*
936 * min periodic tx fifo depth
937 * (largest packet size*MC)/4
938 * (1024 * 3)/4 = 768
939 */
940 ptxfsiz = 768;
941
942 params->host_rx_fifo_size = rxfsiz;
943 params->host_nperio_tx_fifo_size = nptxfsiz;
944 params->host_perio_tx_fifo_size = ptxfsiz;
945 }
946
947 /*
948 * If the summation of RX, NPTX and PTX fifo sizes is still
949 * bigger than the total_fifo_size, then we have a problem.
950 *
951 * We won't be able to allocate as many endpoints. Right now,
952 * we're just printing an error message, but ideally this FIFO
953 * allocation algorithm would be improved in the future.
954 *
955 * FIXME improve this FIFO allocation algorithm.
956 */
957 if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
958 dev_err(hsotg->dev, "invalid fifo sizes\n");
959}
960
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700961static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
962{
963 struct dwc2_core_params *params = hsotg->core_params;
Matthijs Kooijmana1fc5242013-08-30 18:45:20 +0200964 u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700965
Matthijs Kooijman12086052013-04-29 19:46:35 +0000966 if (!params->enable_dynamic_fifo)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700967 return;
968
Dinh Nguyen112fe8e2014-05-07 08:31:29 -0500969 dwc2_calculate_dynamic_fifo(hsotg);
970
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700971 /* Rx FIFO */
Matthijs Kooijmana1fc5242013-08-30 18:45:20 +0200972 grxfsiz = readl(hsotg->regs + GRXFSIZ);
973 dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
974 grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
975 grxfsiz |= params->host_rx_fifo_size <<
976 GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
977 writel(grxfsiz, hsotg->regs + GRXFSIZ);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700978 dev_dbg(hsotg->dev, "new grxfsiz=%08x\n", readl(hsotg->regs + GRXFSIZ));
979
980 /* Non-periodic Tx FIFO */
981 dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
982 readl(hsotg->regs + GNPTXFSIZ));
983 nptxfsiz = params->host_nperio_tx_fifo_size <<
984 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
985 nptxfsiz |= params->host_rx_fifo_size <<
986 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
987 writel(nptxfsiz, hsotg->regs + GNPTXFSIZ);
988 dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
989 readl(hsotg->regs + GNPTXFSIZ));
990
991 /* Periodic Tx FIFO */
992 dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
993 readl(hsotg->regs + HPTXFSIZ));
Matthijs Kooijmanc35205a2013-08-30 18:45:18 +0200994 hptxfsiz = params->host_perio_tx_fifo_size <<
995 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
996 hptxfsiz |= (params->host_rx_fifo_size +
997 params->host_nperio_tx_fifo_size) <<
998 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
999 writel(hptxfsiz, hsotg->regs + HPTXFSIZ);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001000 dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
1001 readl(hsotg->regs + HPTXFSIZ));
1002
1003 if (hsotg->core_params->en_multiple_tx_fifo > 0 &&
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001004 hsotg->hw_params.snpsid <= DWC2_CORE_REV_2_94a) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001005 /*
1006 * Global DFIFOCFG calculation for Host mode -
1007 * include RxFIFO, NPTXFIFO and HPTXFIFO
1008 */
1009 dfifocfg = readl(hsotg->regs + GDFIFOCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001010 dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
Matthijs Kooijman08b9f9d2013-08-30 18:45:19 +02001011 dfifocfg |= (params->host_rx_fifo_size +
1012 params->host_nperio_tx_fifo_size +
1013 params->host_perio_tx_fifo_size) <<
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001014 GDFIFOCFG_EPINFOBASE_SHIFT &
1015 GDFIFOCFG_EPINFOBASE_MASK;
1016 writel(dfifocfg, hsotg->regs + GDFIFOCFG);
1017 }
1018}
1019
1020/**
1021 * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
1022 * Host mode
1023 *
1024 * @hsotg: Programming view of DWC_otg controller
1025 *
1026 * This function flushes the Tx and Rx FIFOs and flushes any entries in the
1027 * request queues. Host channels are reset to ensure that they are ready for
1028 * performing transfers.
1029 */
1030void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
1031{
1032 u32 hcfg, hfir, otgctl;
1033
1034 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
1035
1036 /* Restart the Phy Clock */
1037 writel(0, hsotg->regs + PCGCTL);
1038
1039 /* Initialize Host Configuration Register */
1040 dwc2_init_fs_ls_pclk_sel(hsotg);
1041 if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL) {
1042 hcfg = readl(hsotg->regs + HCFG);
1043 hcfg |= HCFG_FSLSSUPP;
1044 writel(hcfg, hsotg->regs + HCFG);
1045 }
1046
1047 /*
1048 * This bit allows dynamic reloading of the HFIR register during
Masanari Iida0dcde5082013-09-13 23:34:36 +09001049 * runtime. This bit needs to be programmed during initial configuration
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001050 * and its value must not be changed during runtime.
1051 */
1052 if (hsotg->core_params->reload_ctl > 0) {
1053 hfir = readl(hsotg->regs + HFIR);
1054 hfir |= HFIR_RLDCTRL;
1055 writel(hfir, hsotg->regs + HFIR);
1056 }
1057
1058 if (hsotg->core_params->dma_desc_enable > 0) {
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001059 u32 op_mode = hsotg->hw_params.op_mode;
1060 if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
1061 !hsotg->hw_params.dma_desc_enable ||
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001062 op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
1063 op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
1064 op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
1065 dev_err(hsotg->dev,
1066 "Hardware does not support descriptor DMA mode -\n");
1067 dev_err(hsotg->dev,
1068 "falling back to buffer DMA mode.\n");
1069 hsotg->core_params->dma_desc_enable = 0;
1070 } else {
1071 hcfg = readl(hsotg->regs + HCFG);
1072 hcfg |= HCFG_DESCDMA;
1073 writel(hcfg, hsotg->regs + HCFG);
1074 }
1075 }
1076
1077 /* Configure data FIFO sizes */
1078 dwc2_config_fifos(hsotg);
1079
1080 /* TODO - check this */
1081 /* Clear Host Set HNP Enable in the OTG Control Register */
1082 otgctl = readl(hsotg->regs + GOTGCTL);
1083 otgctl &= ~GOTGCTL_HSTSETHNPEN;
1084 writel(otgctl, hsotg->regs + GOTGCTL);
1085
1086 /* Make sure the FIFOs are flushed */
1087 dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
1088 dwc2_flush_rx_fifo(hsotg);
1089
1090 /* Clear Host Set HNP Enable in the OTG Control Register */
1091 otgctl = readl(hsotg->regs + GOTGCTL);
1092 otgctl &= ~GOTGCTL_HSTSETHNPEN;
1093 writel(otgctl, hsotg->regs + GOTGCTL);
1094
1095 if (hsotg->core_params->dma_desc_enable <= 0) {
1096 int num_channels, i;
1097 u32 hcchar;
1098
1099 /* Flush out any leftover queued requests */
1100 num_channels = hsotg->core_params->host_channels;
1101 for (i = 0; i < num_channels; i++) {
1102 hcchar = readl(hsotg->regs + HCCHAR(i));
1103 hcchar &= ~HCCHAR_CHENA;
1104 hcchar |= HCCHAR_CHDIS;
1105 hcchar &= ~HCCHAR_EPDIR;
1106 writel(hcchar, hsotg->regs + HCCHAR(i));
1107 }
1108
1109 /* Halt all channels to put them into a known state */
1110 for (i = 0; i < num_channels; i++) {
1111 int count = 0;
1112
1113 hcchar = readl(hsotg->regs + HCCHAR(i));
1114 hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
1115 hcchar &= ~HCCHAR_EPDIR;
1116 writel(hcchar, hsotg->regs + HCCHAR(i));
1117 dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
1118 __func__, i);
1119 do {
1120 hcchar = readl(hsotg->regs + HCCHAR(i));
1121 if (++count > 1000) {
1122 dev_err(hsotg->dev,
1123 "Unable to clear enable on channel %d\n",
1124 i);
1125 break;
1126 }
1127 udelay(1);
1128 } while (hcchar & HCCHAR_CHENA);
1129 }
1130 }
1131
1132 /* Turn on the vbus power */
1133 dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
1134 if (hsotg->op_state == OTG_STATE_A_HOST) {
1135 u32 hprt0 = dwc2_read_hprt0(hsotg);
1136
1137 dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
1138 !!(hprt0 & HPRT0_PWR));
1139 if (!(hprt0 & HPRT0_PWR)) {
1140 hprt0 |= HPRT0_PWR;
1141 writel(hprt0, hsotg->regs + HPRT0);
1142 }
1143 }
1144
1145 dwc2_enable_host_interrupts(hsotg);
1146}
1147
1148static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
1149 struct dwc2_host_chan *chan)
1150{
1151 u32 hcintmsk = HCINTMSK_CHHLTD;
1152
1153 switch (chan->ep_type) {
1154 case USB_ENDPOINT_XFER_CONTROL:
1155 case USB_ENDPOINT_XFER_BULK:
1156 dev_vdbg(hsotg->dev, "control/bulk\n");
1157 hcintmsk |= HCINTMSK_XFERCOMPL;
1158 hcintmsk |= HCINTMSK_STALL;
1159 hcintmsk |= HCINTMSK_XACTERR;
1160 hcintmsk |= HCINTMSK_DATATGLERR;
1161 if (chan->ep_is_in) {
1162 hcintmsk |= HCINTMSK_BBLERR;
1163 } else {
1164 hcintmsk |= HCINTMSK_NAK;
1165 hcintmsk |= HCINTMSK_NYET;
1166 if (chan->do_ping)
1167 hcintmsk |= HCINTMSK_ACK;
1168 }
1169
1170 if (chan->do_split) {
1171 hcintmsk |= HCINTMSK_NAK;
1172 if (chan->complete_split)
1173 hcintmsk |= HCINTMSK_NYET;
1174 else
1175 hcintmsk |= HCINTMSK_ACK;
1176 }
1177
1178 if (chan->error_state)
1179 hcintmsk |= HCINTMSK_ACK;
1180 break;
1181
1182 case USB_ENDPOINT_XFER_INT:
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001183 if (dbg_perio())
1184 dev_vdbg(hsotg->dev, "intr\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001185 hcintmsk |= HCINTMSK_XFERCOMPL;
1186 hcintmsk |= HCINTMSK_NAK;
1187 hcintmsk |= HCINTMSK_STALL;
1188 hcintmsk |= HCINTMSK_XACTERR;
1189 hcintmsk |= HCINTMSK_DATATGLERR;
1190 hcintmsk |= HCINTMSK_FRMOVRUN;
1191
1192 if (chan->ep_is_in)
1193 hcintmsk |= HCINTMSK_BBLERR;
1194 if (chan->error_state)
1195 hcintmsk |= HCINTMSK_ACK;
1196 if (chan->do_split) {
1197 if (chan->complete_split)
1198 hcintmsk |= HCINTMSK_NYET;
1199 else
1200 hcintmsk |= HCINTMSK_ACK;
1201 }
1202 break;
1203
1204 case USB_ENDPOINT_XFER_ISOC:
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001205 if (dbg_perio())
1206 dev_vdbg(hsotg->dev, "isoc\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001207 hcintmsk |= HCINTMSK_XFERCOMPL;
1208 hcintmsk |= HCINTMSK_FRMOVRUN;
1209 hcintmsk |= HCINTMSK_ACK;
1210
1211 if (chan->ep_is_in) {
1212 hcintmsk |= HCINTMSK_XACTERR;
1213 hcintmsk |= HCINTMSK_BBLERR;
1214 }
1215 break;
1216 default:
1217 dev_err(hsotg->dev, "## Unknown EP type ##\n");
1218 break;
1219 }
1220
1221 writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001222 if (dbg_hc(chan))
1223 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001224}
1225
1226static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
1227 struct dwc2_host_chan *chan)
1228{
1229 u32 hcintmsk = HCINTMSK_CHHLTD;
1230
1231 /*
1232 * For Descriptor DMA mode core halts the channel on AHB error.
1233 * Interrupt is not required.
1234 */
1235 if (hsotg->core_params->dma_desc_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001236 if (dbg_hc(chan))
1237 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001238 hcintmsk |= HCINTMSK_AHBERR;
1239 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001240 if (dbg_hc(chan))
1241 dev_vdbg(hsotg->dev, "desc DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001242 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1243 hcintmsk |= HCINTMSK_XFERCOMPL;
1244 }
1245
1246 if (chan->error_state && !chan->do_split &&
1247 chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001248 if (dbg_hc(chan))
1249 dev_vdbg(hsotg->dev, "setting ACK\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001250 hcintmsk |= HCINTMSK_ACK;
1251 if (chan->ep_is_in) {
1252 hcintmsk |= HCINTMSK_DATATGLERR;
1253 if (chan->ep_type != USB_ENDPOINT_XFER_INT)
1254 hcintmsk |= HCINTMSK_NAK;
1255 }
1256 }
1257
1258 writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001259 if (dbg_hc(chan))
1260 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001261}
1262
1263static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
1264 struct dwc2_host_chan *chan)
1265{
1266 u32 intmsk;
1267
1268 if (hsotg->core_params->dma_enable > 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001269 if (dbg_hc(chan))
1270 dev_vdbg(hsotg->dev, "DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001271 dwc2_hc_enable_dma_ints(hsotg, chan);
1272 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001273 if (dbg_hc(chan))
1274 dev_vdbg(hsotg->dev, "DMA disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001275 dwc2_hc_enable_slave_ints(hsotg, chan);
1276 }
1277
1278 /* Enable the top level host channel interrupt */
1279 intmsk = readl(hsotg->regs + HAINTMSK);
1280 intmsk |= 1 << chan->hc_num;
1281 writel(intmsk, hsotg->regs + HAINTMSK);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001282 if (dbg_hc(chan))
1283 dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001284
1285 /* Make sure host channel interrupts are enabled */
1286 intmsk = readl(hsotg->regs + GINTMSK);
1287 intmsk |= GINTSTS_HCHINT;
1288 writel(intmsk, hsotg->regs + GINTMSK);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001289 if (dbg_hc(chan))
1290 dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001291}
1292
1293/**
1294 * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
1295 * a specific endpoint
1296 *
1297 * @hsotg: Programming view of DWC_otg controller
1298 * @chan: Information needed to initialize the host channel
1299 *
1300 * The HCCHARn register is set up with the characteristics specified in chan.
1301 * Host channel interrupts that may need to be serviced while this transfer is
1302 * in progress are enabled.
1303 */
1304void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1305{
1306 u8 hc_num = chan->hc_num;
1307 u32 hcintmsk;
1308 u32 hcchar;
1309 u32 hcsplt = 0;
1310
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001311 if (dbg_hc(chan))
1312 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001313
1314 /* Clear old interrupt conditions for this host channel */
1315 hcintmsk = 0xffffffff;
1316 hcintmsk &= ~HCINTMSK_RESERVED14_31;
1317 writel(hcintmsk, hsotg->regs + HCINT(hc_num));
1318
1319 /* Enable channel interrupts required for this transfer */
1320 dwc2_hc_enable_ints(hsotg, chan);
1321
1322 /*
1323 * Program the HCCHARn register with the endpoint characteristics for
1324 * the current transfer
1325 */
1326 hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
1327 hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
1328 if (chan->ep_is_in)
1329 hcchar |= HCCHAR_EPDIR;
1330 if (chan->speed == USB_SPEED_LOW)
1331 hcchar |= HCCHAR_LSPDDEV;
1332 hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
1333 hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
1334 writel(hcchar, hsotg->regs + HCCHAR(hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001335 if (dbg_hc(chan)) {
1336 dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
1337 hc_num, hcchar);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001338
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001339 dev_vdbg(hsotg->dev, "%s: Channel %d\n",
1340 __func__, hc_num);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001341 dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001342 chan->dev_addr);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001343 dev_vdbg(hsotg->dev, " Ep Num: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001344 chan->ep_num);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001345 dev_vdbg(hsotg->dev, " Is In: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001346 chan->ep_is_in);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001347 dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001348 chan->speed == USB_SPEED_LOW);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001349 dev_vdbg(hsotg->dev, " Ep Type: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001350 chan->ep_type);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001351 dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001352 chan->max_packet);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001353 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001354
1355 /* Program the HCSPLT register for SPLITs */
1356 if (chan->do_split) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001357 if (dbg_hc(chan))
1358 dev_vdbg(hsotg->dev,
1359 "Programming HC %d with split --> %s\n",
1360 hc_num,
1361 chan->complete_split ? "CSPLIT" : "SSPLIT");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001362 if (chan->complete_split)
1363 hcsplt |= HCSPLT_COMPSPLT;
1364 hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
1365 HCSPLT_XACTPOS_MASK;
1366 hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
1367 HCSPLT_HUBADDR_MASK;
1368 hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
1369 HCSPLT_PRTADDR_MASK;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001370 if (dbg_hc(chan)) {
1371 dev_vdbg(hsotg->dev, " comp split %d\n",
1372 chan->complete_split);
1373 dev_vdbg(hsotg->dev, " xact pos %d\n",
1374 chan->xact_pos);
1375 dev_vdbg(hsotg->dev, " hub addr %d\n",
1376 chan->hub_addr);
1377 dev_vdbg(hsotg->dev, " hub port %d\n",
1378 chan->hub_port);
1379 dev_vdbg(hsotg->dev, " is_in %d\n",
1380 chan->ep_is_in);
1381 dev_vdbg(hsotg->dev, " Max Pkt %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001382 chan->max_packet);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001383 dev_vdbg(hsotg->dev, " xferlen %d\n",
1384 chan->xfer_len);
1385 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001386 }
1387
1388 writel(hcsplt, hsotg->regs + HCSPLT(hc_num));
1389}
1390
1391/**
1392 * dwc2_hc_halt() - Attempts to halt a host channel
1393 *
1394 * @hsotg: Controller register interface
1395 * @chan: Host channel to halt
1396 * @halt_status: Reason for halting the channel
1397 *
1398 * This function should only be called in Slave mode or to abort a transfer in
1399 * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
1400 * controller halts the channel when the transfer is complete or a condition
1401 * occurs that requires application intervention.
1402 *
1403 * In slave mode, checks for a free request queue entry, then sets the Channel
1404 * Enable and Channel Disable bits of the Host Channel Characteristics
1405 * register of the specified channel to intiate the halt. If there is no free
1406 * request queue entry, sets only the Channel Disable bit of the HCCHARn
1407 * register to flush requests for this channel. In the latter case, sets a
1408 * flag to indicate that the host channel needs to be halted when a request
1409 * queue slot is open.
1410 *
1411 * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
1412 * HCCHARn register. The controller ensures there is space in the request
1413 * queue before submitting the halt request.
1414 *
1415 * Some time may elapse before the core flushes any posted requests for this
1416 * host channel and halts. The Channel Halted interrupt handler completes the
1417 * deactivation of the host channel.
1418 */
1419void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
1420 enum dwc2_halt_status halt_status)
1421{
1422 u32 nptxsts, hptxsts, hcchar;
1423
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001424 if (dbg_hc(chan))
1425 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001426 if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
1427 dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
1428
1429 if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
1430 halt_status == DWC2_HC_XFER_AHB_ERR) {
1431 /*
1432 * Disable all channel interrupts except Ch Halted. The QTD
1433 * and QH state associated with this transfer has been cleared
1434 * (in the case of URB_DEQUEUE), so the channel needs to be
1435 * shut down carefully to prevent crashes.
1436 */
1437 u32 hcintmsk = HCINTMSK_CHHLTD;
1438
1439 dev_vdbg(hsotg->dev, "dequeue/error\n");
1440 writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
1441
1442 /*
1443 * Make sure no other interrupts besides halt are currently
1444 * pending. Handling another interrupt could cause a crash due
1445 * to the QTD and QH state.
1446 */
1447 writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1448
1449 /*
1450 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
1451 * even if the channel was already halted for some other
1452 * reason
1453 */
1454 chan->halt_status = halt_status;
1455
1456 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
1457 if (!(hcchar & HCCHAR_CHENA)) {
1458 /*
1459 * The channel is either already halted or it hasn't
1460 * started yet. In DMA mode, the transfer may halt if
1461 * it finishes normally or a condition occurs that
1462 * requires driver intervention. Don't want to halt
1463 * the channel again. In either Slave or DMA mode,
1464 * it's possible that the transfer has been assigned
1465 * to a channel, but not started yet when an URB is
1466 * dequeued. Don't want to halt a channel that hasn't
1467 * started yet.
1468 */
1469 return;
1470 }
1471 }
1472 if (chan->halt_pending) {
1473 /*
1474 * A halt has already been issued for this channel. This might
1475 * happen when a transfer is aborted by a higher level in
1476 * the stack.
1477 */
1478 dev_vdbg(hsotg->dev,
1479 "*** %s: Channel %d, chan->halt_pending already set ***\n",
1480 __func__, chan->hc_num);
1481 return;
1482 }
1483
1484 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
1485
1486 /* No need to set the bit in DDMA for disabling the channel */
1487 /* TODO check it everywhere channel is disabled */
1488 if (hsotg->core_params->dma_desc_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001489 if (dbg_hc(chan))
1490 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001491 hcchar |= HCCHAR_CHENA;
1492 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001493 if (dbg_hc(chan))
1494 dev_dbg(hsotg->dev, "desc DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001495 }
1496 hcchar |= HCCHAR_CHDIS;
1497
1498 if (hsotg->core_params->dma_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001499 if (dbg_hc(chan))
1500 dev_vdbg(hsotg->dev, "DMA not enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001501 hcchar |= HCCHAR_CHENA;
1502
1503 /* Check for space in the request queue to issue the halt */
1504 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1505 chan->ep_type == USB_ENDPOINT_XFER_BULK) {
1506 dev_vdbg(hsotg->dev, "control/bulk\n");
1507 nptxsts = readl(hsotg->regs + GNPTXSTS);
1508 if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
1509 dev_vdbg(hsotg->dev, "Disabling channel\n");
1510 hcchar &= ~HCCHAR_CHENA;
1511 }
1512 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001513 if (dbg_perio())
1514 dev_vdbg(hsotg->dev, "isoc/intr\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001515 hptxsts = readl(hsotg->regs + HPTXSTS);
1516 if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
1517 hsotg->queuing_high_bandwidth) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001518 if (dbg_perio())
1519 dev_vdbg(hsotg->dev, "Disabling channel\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001520 hcchar &= ~HCCHAR_CHENA;
1521 }
1522 }
1523 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001524 if (dbg_hc(chan))
1525 dev_vdbg(hsotg->dev, "DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001526 }
1527
1528 writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1529 chan->halt_status = halt_status;
1530
1531 if (hcchar & HCCHAR_CHENA) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001532 if (dbg_hc(chan))
1533 dev_vdbg(hsotg->dev, "Channel enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001534 chan->halt_pending = 1;
1535 chan->halt_on_queue = 0;
1536 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001537 if (dbg_hc(chan))
1538 dev_vdbg(hsotg->dev, "Channel disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001539 chan->halt_on_queue = 1;
1540 }
1541
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001542 if (dbg_hc(chan)) {
1543 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1544 chan->hc_num);
1545 dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
1546 hcchar);
1547 dev_vdbg(hsotg->dev, " halt_pending: %d\n",
1548 chan->halt_pending);
1549 dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
1550 chan->halt_on_queue);
1551 dev_vdbg(hsotg->dev, " halt_status: %d\n",
1552 chan->halt_status);
1553 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001554}
1555
1556/**
1557 * dwc2_hc_cleanup() - Clears the transfer state for a host channel
1558 *
1559 * @hsotg: Programming view of DWC_otg controller
1560 * @chan: Identifies the host channel to clean up
1561 *
1562 * This function is normally called after a transfer is done and the host
1563 * channel is being released
1564 */
1565void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1566{
1567 u32 hcintmsk;
1568
1569 chan->xfer_started = 0;
1570
1571 /*
1572 * Clear channel interrupt enables and any unhandled channel interrupt
1573 * conditions
1574 */
1575 writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
1576 hcintmsk = 0xffffffff;
1577 hcintmsk &= ~HCINTMSK_RESERVED14_31;
1578 writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1579}
1580
1581/**
1582 * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
1583 * which frame a periodic transfer should occur
1584 *
1585 * @hsotg: Programming view of DWC_otg controller
1586 * @chan: Identifies the host channel to set up and its properties
1587 * @hcchar: Current value of the HCCHAR register for the specified host channel
1588 *
1589 * This function has no effect on non-periodic transfers
1590 */
1591static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
1592 struct dwc2_host_chan *chan, u32 *hcchar)
1593{
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001594 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1595 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001596 /* 1 if _next_ frame is odd, 0 if it's even */
Paul Zimmerman81a58952013-06-24 11:34:23 -07001597 if (!(dwc2_hcd_get_frame_number(hsotg) & 0x1))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001598 *hcchar |= HCCHAR_ODDFRM;
1599 }
1600}
1601
1602static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1603{
1604 /* Set up the initial PID for the transfer */
1605 if (chan->speed == USB_SPEED_HIGH) {
1606 if (chan->ep_is_in) {
1607 if (chan->multi_count == 1)
1608 chan->data_pid_start = DWC2_HC_PID_DATA0;
1609 else if (chan->multi_count == 2)
1610 chan->data_pid_start = DWC2_HC_PID_DATA1;
1611 else
1612 chan->data_pid_start = DWC2_HC_PID_DATA2;
1613 } else {
1614 if (chan->multi_count == 1)
1615 chan->data_pid_start = DWC2_HC_PID_DATA0;
1616 else
1617 chan->data_pid_start = DWC2_HC_PID_MDATA;
1618 }
1619 } else {
1620 chan->data_pid_start = DWC2_HC_PID_DATA0;
1621 }
1622}
1623
1624/**
1625 * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1626 * the Host Channel
1627 *
1628 * @hsotg: Programming view of DWC_otg controller
1629 * @chan: Information needed to initialize the host channel
1630 *
1631 * This function should only be called in Slave mode. For a channel associated
1632 * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1633 * associated with a periodic EP, the periodic Tx FIFO is written.
1634 *
1635 * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1636 * the number of bytes written to the Tx FIFO.
1637 */
1638static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1639 struct dwc2_host_chan *chan)
1640{
1641 u32 i;
1642 u32 remaining_count;
1643 u32 byte_count;
1644 u32 dword_count;
1645 u32 __iomem *data_fifo;
1646 u32 *data_buf = (u32 *)chan->xfer_buf;
1647
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001648 if (dbg_hc(chan))
1649 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001650
1651 data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
1652
1653 remaining_count = chan->xfer_len - chan->xfer_count;
1654 if (remaining_count > chan->max_packet)
1655 byte_count = chan->max_packet;
1656 else
1657 byte_count = remaining_count;
1658
1659 dword_count = (byte_count + 3) / 4;
1660
1661 if (((unsigned long)data_buf & 0x3) == 0) {
1662 /* xfer_buf is DWORD aligned */
1663 for (i = 0; i < dword_count; i++, data_buf++)
1664 writel(*data_buf, data_fifo);
1665 } else {
1666 /* xfer_buf is not DWORD aligned */
1667 for (i = 0; i < dword_count; i++, data_buf++) {
1668 u32 data = data_buf[0] | data_buf[1] << 8 |
1669 data_buf[2] << 16 | data_buf[3] << 24;
1670 writel(data, data_fifo);
1671 }
1672 }
1673
1674 chan->xfer_count += byte_count;
1675 chan->xfer_buf += byte_count;
1676}
1677
1678/**
1679 * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1680 * channel and starts the transfer
1681 *
1682 * @hsotg: Programming view of DWC_otg controller
1683 * @chan: Information needed to initialize the host channel. The xfer_len value
1684 * may be reduced to accommodate the max widths of the XferSize and
1685 * PktCnt fields in the HCTSIZn register. The multi_count value may be
1686 * changed to reflect the final xfer_len value.
1687 *
1688 * This function may be called in either Slave mode or DMA mode. In Slave mode,
1689 * the caller must ensure that there is sufficient space in the request queue
1690 * and Tx Data FIFO.
1691 *
1692 * For an OUT transfer in Slave mode, it loads a data packet into the
1693 * appropriate FIFO. If necessary, additional data packets are loaded in the
1694 * Host ISR.
1695 *
1696 * For an IN transfer in Slave mode, a data packet is requested. The data
1697 * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1698 * additional data packets are requested in the Host ISR.
1699 *
1700 * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1701 * register along with a packet count of 1 and the channel is enabled. This
1702 * causes a single PING transaction to occur. Other fields in HCTSIZ are
1703 * simply set to 0 since no data transfer occurs in this case.
1704 *
1705 * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1706 * all the information required to perform the subsequent data transfer. In
1707 * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1708 * controller performs the entire PING protocol, then starts the data
1709 * transfer.
1710 */
1711void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1712 struct dwc2_host_chan *chan)
1713{
1714 u32 max_hc_xfer_size = hsotg->core_params->max_transfer_size;
1715 u16 max_hc_pkt_count = hsotg->core_params->max_packet_count;
1716 u32 hcchar;
1717 u32 hctsiz = 0;
1718 u16 num_packets;
1719
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001720 if (dbg_hc(chan))
1721 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001722
1723 if (chan->do_ping) {
1724 if (hsotg->core_params->dma_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001725 if (dbg_hc(chan))
1726 dev_vdbg(hsotg->dev, "ping, no DMA\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001727 dwc2_hc_do_ping(hsotg, chan);
1728 chan->xfer_started = 1;
1729 return;
1730 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001731 if (dbg_hc(chan))
1732 dev_vdbg(hsotg->dev, "ping, DMA\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001733 hctsiz |= TSIZ_DOPNG;
1734 }
1735 }
1736
1737 if (chan->do_split) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001738 if (dbg_hc(chan))
1739 dev_vdbg(hsotg->dev, "split\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001740 num_packets = 1;
1741
1742 if (chan->complete_split && !chan->ep_is_in)
1743 /*
1744 * For CSPLIT OUT Transfer, set the size to 0 so the
1745 * core doesn't expect any data written to the FIFO
1746 */
1747 chan->xfer_len = 0;
1748 else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1749 chan->xfer_len = chan->max_packet;
1750 else if (!chan->ep_is_in && chan->xfer_len > 188)
1751 chan->xfer_len = 188;
1752
1753 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1754 TSIZ_XFERSIZE_MASK;
1755 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001756 if (dbg_hc(chan))
1757 dev_vdbg(hsotg->dev, "no split\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001758 /*
1759 * Ensure that the transfer length and packet count will fit
1760 * in the widths allocated for them in the HCTSIZn register
1761 */
1762 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1763 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1764 /*
1765 * Make sure the transfer size is no larger than one
1766 * (micro)frame's worth of data. (A check was done
1767 * when the periodic transfer was accepted to ensure
1768 * that a (micro)frame's worth of data can be
1769 * programmed into a channel.)
1770 */
1771 u32 max_periodic_len =
1772 chan->multi_count * chan->max_packet;
1773
1774 if (chan->xfer_len > max_periodic_len)
1775 chan->xfer_len = max_periodic_len;
1776 } else if (chan->xfer_len > max_hc_xfer_size) {
1777 /*
1778 * Make sure that xfer_len is a multiple of max packet
1779 * size
1780 */
1781 chan->xfer_len =
1782 max_hc_xfer_size - chan->max_packet + 1;
1783 }
1784
1785 if (chan->xfer_len > 0) {
1786 num_packets = (chan->xfer_len + chan->max_packet - 1) /
1787 chan->max_packet;
1788 if (num_packets > max_hc_pkt_count) {
1789 num_packets = max_hc_pkt_count;
1790 chan->xfer_len = num_packets * chan->max_packet;
1791 }
1792 } else {
1793 /* Need 1 packet for transfer length of 0 */
1794 num_packets = 1;
1795 }
1796
1797 if (chan->ep_is_in)
1798 /*
1799 * Always program an integral # of max packets for IN
1800 * transfers
1801 */
1802 chan->xfer_len = num_packets * chan->max_packet;
1803
1804 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1805 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1806 /*
1807 * Make sure that the multi_count field matches the
1808 * actual transfer length
1809 */
1810 chan->multi_count = num_packets;
1811
1812 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1813 dwc2_set_pid_isoc(chan);
1814
1815 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1816 TSIZ_XFERSIZE_MASK;
1817 }
1818
1819 chan->start_pkt_count = num_packets;
1820 hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1821 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1822 TSIZ_SC_MC_PID_MASK;
1823 writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001824 if (dbg_hc(chan)) {
1825 dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1826 hctsiz, chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001827
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001828 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1829 chan->hc_num);
1830 dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001831 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1832 TSIZ_XFERSIZE_SHIFT);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001833 dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001834 (hctsiz & TSIZ_PKTCNT_MASK) >>
1835 TSIZ_PKTCNT_SHIFT);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001836 dev_vdbg(hsotg->dev, " Start PID: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001837 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1838 TSIZ_SC_MC_PID_SHIFT);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001839 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001840
1841 if (hsotg->core_params->dma_enable > 0) {
1842 dma_addr_t dma_addr;
1843
1844 if (chan->align_buf) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001845 if (dbg_hc(chan))
1846 dev_vdbg(hsotg->dev, "align_buf\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001847 dma_addr = chan->align_buf;
1848 } else {
1849 dma_addr = chan->xfer_dma;
1850 }
1851 writel((u32)dma_addr, hsotg->regs + HCDMA(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001852 if (dbg_hc(chan))
1853 dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1854 (unsigned long)dma_addr, chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001855 }
1856
1857 /* Start the split */
1858 if (chan->do_split) {
1859 u32 hcsplt = readl(hsotg->regs + HCSPLT(chan->hc_num));
1860
1861 hcsplt |= HCSPLT_SPLTENA;
1862 writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
1863 }
1864
1865 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
1866 hcchar &= ~HCCHAR_MULTICNT_MASK;
1867 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1868 HCCHAR_MULTICNT_MASK;
1869 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1870
1871 if (hcchar & HCCHAR_CHDIS)
1872 dev_warn(hsotg->dev,
1873 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1874 __func__, chan->hc_num, hcchar);
1875
1876 /* Set host channel enable after all other setup is complete */
1877 hcchar |= HCCHAR_CHENA;
1878 hcchar &= ~HCCHAR_CHDIS;
1879
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001880 if (dbg_hc(chan))
1881 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001882 (hcchar & HCCHAR_MULTICNT_MASK) >>
1883 HCCHAR_MULTICNT_SHIFT);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001884
1885 writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001886 if (dbg_hc(chan))
1887 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1888 chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001889
1890 chan->xfer_started = 1;
1891 chan->requests++;
1892
1893 if (hsotg->core_params->dma_enable <= 0 &&
1894 !chan->ep_is_in && chan->xfer_len > 0)
1895 /* Load OUT packet into the appropriate Tx FIFO */
1896 dwc2_hc_write_packet(hsotg, chan);
1897}
1898
1899/**
1900 * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1901 * host channel and starts the transfer in Descriptor DMA mode
1902 *
1903 * @hsotg: Programming view of DWC_otg controller
1904 * @chan: Information needed to initialize the host channel
1905 *
1906 * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1907 * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1908 * with micro-frame bitmap.
1909 *
1910 * Initializes HCDMA register with descriptor list address and CTD value then
1911 * starts the transfer via enabling the channel.
1912 */
1913void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1914 struct dwc2_host_chan *chan)
1915{
1916 u32 hcchar;
1917 u32 hc_dma;
1918 u32 hctsiz = 0;
1919
1920 if (chan->do_ping)
1921 hctsiz |= TSIZ_DOPNG;
1922
1923 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1924 dwc2_set_pid_isoc(chan);
1925
1926 /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1927 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1928 TSIZ_SC_MC_PID_MASK;
1929
1930 /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1931 hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1932
1933 /* Non-zero only for high-speed interrupt endpoints */
1934 hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1935
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001936 if (dbg_hc(chan)) {
1937 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1938 chan->hc_num);
1939 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1940 chan->data_pid_start);
1941 dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
1942 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001943
1944 writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1945
1946 hc_dma = (u32)chan->desc_list_addr & HCDMA_DMA_ADDR_MASK;
1947
1948 /* Always start from first descriptor */
1949 hc_dma &= ~HCDMA_CTD_MASK;
1950 writel(hc_dma, hsotg->regs + HCDMA(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001951 if (dbg_hc(chan))
1952 dev_vdbg(hsotg->dev, "Wrote %08x to HCDMA(%d)\n",
1953 hc_dma, chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001954
1955 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
1956 hcchar &= ~HCCHAR_MULTICNT_MASK;
1957 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1958 HCCHAR_MULTICNT_MASK;
1959
1960 if (hcchar & HCCHAR_CHDIS)
1961 dev_warn(hsotg->dev,
1962 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1963 __func__, chan->hc_num, hcchar);
1964
1965 /* Set host channel enable after all other setup is complete */
1966 hcchar |= HCCHAR_CHENA;
1967 hcchar &= ~HCCHAR_CHDIS;
1968
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001969 if (dbg_hc(chan))
1970 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001971 (hcchar & HCCHAR_MULTICNT_MASK) >>
1972 HCCHAR_MULTICNT_SHIFT);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001973
1974 writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001975 if (dbg_hc(chan))
1976 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1977 chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001978
1979 chan->xfer_started = 1;
1980 chan->requests++;
1981}
1982
1983/**
1984 * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
1985 * a previous call to dwc2_hc_start_transfer()
1986 *
1987 * @hsotg: Programming view of DWC_otg controller
1988 * @chan: Information needed to initialize the host channel
1989 *
1990 * The caller must ensure there is sufficient space in the request queue and Tx
1991 * Data FIFO. This function should only be called in Slave mode. In DMA mode,
1992 * the controller acts autonomously to complete transfers programmed to a host
1993 * channel.
1994 *
1995 * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
1996 * if there is any data remaining to be queued. For an IN transfer, another
1997 * data packet is always requested. For the SETUP phase of a control transfer,
1998 * this function does nothing.
1999 *
2000 * Return: 1 if a new request is queued, 0 if no more requests are required
2001 * for this transfer
2002 */
2003int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
2004 struct dwc2_host_chan *chan)
2005{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002006 if (dbg_hc(chan))
2007 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
2008 chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002009
2010 if (chan->do_split)
2011 /* SPLITs always queue just once per channel */
2012 return 0;
2013
2014 if (chan->data_pid_start == DWC2_HC_PID_SETUP)
2015 /* SETUPs are queued only once since they can't be NAK'd */
2016 return 0;
2017
2018 if (chan->ep_is_in) {
2019 /*
2020 * Always queue another request for other IN transfers. If
2021 * back-to-back INs are issued and NAKs are received for both,
2022 * the driver may still be processing the first NAK when the
2023 * second NAK is received. When the interrupt handler clears
2024 * the NAK interrupt for the first NAK, the second NAK will
2025 * not be seen. So we can't depend on the NAK interrupt
2026 * handler to requeue a NAK'd request. Instead, IN requests
2027 * are issued each time this function is called. When the
2028 * transfer completes, the extra requests for the channel will
2029 * be flushed.
2030 */
2031 u32 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
2032
2033 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
2034 hcchar |= HCCHAR_CHENA;
2035 hcchar &= ~HCCHAR_CHDIS;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002036 if (dbg_hc(chan))
2037 dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
2038 hcchar);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002039 writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
2040 chan->requests++;
2041 return 1;
2042 }
2043
2044 /* OUT transfers */
2045
2046 if (chan->xfer_count < chan->xfer_len) {
2047 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2048 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
2049 u32 hcchar = readl(hsotg->regs +
2050 HCCHAR(chan->hc_num));
2051
2052 dwc2_hc_set_even_odd_frame(hsotg, chan,
2053 &hcchar);
2054 }
2055
2056 /* Load OUT packet into the appropriate Tx FIFO */
2057 dwc2_hc_write_packet(hsotg, chan);
2058 chan->requests++;
2059 return 1;
2060 }
2061
2062 return 0;
2063}
2064
2065/**
2066 * dwc2_hc_do_ping() - Starts a PING transfer
2067 *
2068 * @hsotg: Programming view of DWC_otg controller
2069 * @chan: Information needed to initialize the host channel
2070 *
2071 * This function should only be called in Slave mode. The Do Ping bit is set in
2072 * the HCTSIZ register, then the channel is enabled.
2073 */
2074void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
2075{
2076 u32 hcchar;
2077 u32 hctsiz;
2078
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002079 if (dbg_hc(chan))
2080 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
2081 chan->hc_num);
2082
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002083
2084 hctsiz = TSIZ_DOPNG;
2085 hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
2086 writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
2087
2088 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
2089 hcchar |= HCCHAR_CHENA;
2090 hcchar &= ~HCCHAR_CHDIS;
2091 writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
2092}
2093
2094/**
2095 * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
2096 * the HFIR register according to PHY type and speed
2097 *
2098 * @hsotg: Programming view of DWC_otg controller
2099 *
2100 * NOTE: The caller can modify the value of the HFIR register only after the
2101 * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
2102 * has been set
2103 */
2104u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
2105{
2106 u32 usbcfg;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002107 u32 hprt0;
2108 int clock = 60; /* default value */
2109
2110 usbcfg = readl(hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002111 hprt0 = readl(hsotg->regs + HPRT0);
2112
2113 if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
2114 !(usbcfg & GUSBCFG_PHYIF16))
2115 clock = 60;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002116 if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002117 GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
2118 clock = 48;
2119 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2120 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
2121 clock = 30;
2122 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2123 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
2124 clock = 60;
2125 if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2126 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
2127 clock = 48;
2128 if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002129 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002130 clock = 48;
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02002131 if ((usbcfg & GUSBCFG_PHYSEL) &&
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002132 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002133 clock = 48;
2134
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02002135 if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002136 /* High speed case */
2137 return 125 * clock;
2138 else
2139 /* FS/LS case */
2140 return 1000 * clock;
2141}
2142
2143/**
2144 * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
2145 * buffer
2146 *
2147 * @core_if: Programming view of DWC_otg controller
2148 * @dest: Destination buffer for the packet
2149 * @bytes: Number of bytes to copy to the destination
2150 */
2151void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
2152{
2153 u32 __iomem *fifo = hsotg->regs + HCFIFO(0);
2154 u32 *data_buf = (u32 *)dest;
2155 int word_count = (bytes + 3) / 4;
2156 int i;
2157
2158 /*
2159 * Todo: Account for the case where dest is not dword aligned. This
2160 * requires reading data from the FIFO into a u32 temp buffer, then
2161 * moving it into the data buffer.
2162 */
2163
2164 dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
2165
2166 for (i = 0; i < word_count; i++, data_buf++)
2167 *data_buf = readl(fifo);
2168}
2169
2170/**
2171 * dwc2_dump_host_registers() - Prints the host registers
2172 *
2173 * @hsotg: Programming view of DWC_otg controller
2174 *
2175 * NOTE: This function will be removed once the peripheral controller code
2176 * is integrated and the driver is stable
2177 */
2178void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
2179{
2180#ifdef DEBUG
2181 u32 __iomem *addr;
2182 int i;
2183
2184 dev_dbg(hsotg->dev, "Host Global Registers\n");
2185 addr = hsotg->regs + HCFG;
2186 dev_dbg(hsotg->dev, "HCFG @0x%08lX : 0x%08X\n",
2187 (unsigned long)addr, readl(addr));
2188 addr = hsotg->regs + HFIR;
2189 dev_dbg(hsotg->dev, "HFIR @0x%08lX : 0x%08X\n",
2190 (unsigned long)addr, readl(addr));
2191 addr = hsotg->regs + HFNUM;
2192 dev_dbg(hsotg->dev, "HFNUM @0x%08lX : 0x%08X\n",
2193 (unsigned long)addr, readl(addr));
2194 addr = hsotg->regs + HPTXSTS;
2195 dev_dbg(hsotg->dev, "HPTXSTS @0x%08lX : 0x%08X\n",
2196 (unsigned long)addr, readl(addr));
2197 addr = hsotg->regs + HAINT;
2198 dev_dbg(hsotg->dev, "HAINT @0x%08lX : 0x%08X\n",
2199 (unsigned long)addr, readl(addr));
2200 addr = hsotg->regs + HAINTMSK;
2201 dev_dbg(hsotg->dev, "HAINTMSK @0x%08lX : 0x%08X\n",
2202 (unsigned long)addr, readl(addr));
2203 if (hsotg->core_params->dma_desc_enable > 0) {
2204 addr = hsotg->regs + HFLBADDR;
2205 dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
2206 (unsigned long)addr, readl(addr));
2207 }
2208
2209 addr = hsotg->regs + HPRT0;
2210 dev_dbg(hsotg->dev, "HPRT0 @0x%08lX : 0x%08X\n",
2211 (unsigned long)addr, readl(addr));
2212
2213 for (i = 0; i < hsotg->core_params->host_channels; i++) {
2214 dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
2215 addr = hsotg->regs + HCCHAR(i);
2216 dev_dbg(hsotg->dev, "HCCHAR @0x%08lX : 0x%08X\n",
2217 (unsigned long)addr, readl(addr));
2218 addr = hsotg->regs + HCSPLT(i);
2219 dev_dbg(hsotg->dev, "HCSPLT @0x%08lX : 0x%08X\n",
2220 (unsigned long)addr, readl(addr));
2221 addr = hsotg->regs + HCINT(i);
2222 dev_dbg(hsotg->dev, "HCINT @0x%08lX : 0x%08X\n",
2223 (unsigned long)addr, readl(addr));
2224 addr = hsotg->regs + HCINTMSK(i);
2225 dev_dbg(hsotg->dev, "HCINTMSK @0x%08lX : 0x%08X\n",
2226 (unsigned long)addr, readl(addr));
2227 addr = hsotg->regs + HCTSIZ(i);
2228 dev_dbg(hsotg->dev, "HCTSIZ @0x%08lX : 0x%08X\n",
2229 (unsigned long)addr, readl(addr));
2230 addr = hsotg->regs + HCDMA(i);
2231 dev_dbg(hsotg->dev, "HCDMA @0x%08lX : 0x%08X\n",
2232 (unsigned long)addr, readl(addr));
2233 if (hsotg->core_params->dma_desc_enable > 0) {
2234 addr = hsotg->regs + HCDMAB(i);
2235 dev_dbg(hsotg->dev, "HCDMAB @0x%08lX : 0x%08X\n",
2236 (unsigned long)addr, readl(addr));
2237 }
2238 }
2239#endif
2240}
2241
2242/**
2243 * dwc2_dump_global_registers() - Prints the core global registers
2244 *
2245 * @hsotg: Programming view of DWC_otg controller
2246 *
2247 * NOTE: This function will be removed once the peripheral controller code
2248 * is integrated and the driver is stable
2249 */
2250void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
2251{
2252#ifdef DEBUG
2253 u32 __iomem *addr;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002254
2255 dev_dbg(hsotg->dev, "Core Global Registers\n");
2256 addr = hsotg->regs + GOTGCTL;
2257 dev_dbg(hsotg->dev, "GOTGCTL @0x%08lX : 0x%08X\n",
2258 (unsigned long)addr, readl(addr));
2259 addr = hsotg->regs + GOTGINT;
2260 dev_dbg(hsotg->dev, "GOTGINT @0x%08lX : 0x%08X\n",
2261 (unsigned long)addr, readl(addr));
2262 addr = hsotg->regs + GAHBCFG;
2263 dev_dbg(hsotg->dev, "GAHBCFG @0x%08lX : 0x%08X\n",
2264 (unsigned long)addr, readl(addr));
2265 addr = hsotg->regs + GUSBCFG;
2266 dev_dbg(hsotg->dev, "GUSBCFG @0x%08lX : 0x%08X\n",
2267 (unsigned long)addr, readl(addr));
2268 addr = hsotg->regs + GRSTCTL;
2269 dev_dbg(hsotg->dev, "GRSTCTL @0x%08lX : 0x%08X\n",
2270 (unsigned long)addr, readl(addr));
2271 addr = hsotg->regs + GINTSTS;
2272 dev_dbg(hsotg->dev, "GINTSTS @0x%08lX : 0x%08X\n",
2273 (unsigned long)addr, readl(addr));
2274 addr = hsotg->regs + GINTMSK;
2275 dev_dbg(hsotg->dev, "GINTMSK @0x%08lX : 0x%08X\n",
2276 (unsigned long)addr, readl(addr));
2277 addr = hsotg->regs + GRXSTSR;
2278 dev_dbg(hsotg->dev, "GRXSTSR @0x%08lX : 0x%08X\n",
2279 (unsigned long)addr, readl(addr));
2280 addr = hsotg->regs + GRXFSIZ;
2281 dev_dbg(hsotg->dev, "GRXFSIZ @0x%08lX : 0x%08X\n",
2282 (unsigned long)addr, readl(addr));
2283 addr = hsotg->regs + GNPTXFSIZ;
2284 dev_dbg(hsotg->dev, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
2285 (unsigned long)addr, readl(addr));
2286 addr = hsotg->regs + GNPTXSTS;
2287 dev_dbg(hsotg->dev, "GNPTXSTS @0x%08lX : 0x%08X\n",
2288 (unsigned long)addr, readl(addr));
2289 addr = hsotg->regs + GI2CCTL;
2290 dev_dbg(hsotg->dev, "GI2CCTL @0x%08lX : 0x%08X\n",
2291 (unsigned long)addr, readl(addr));
2292 addr = hsotg->regs + GPVNDCTL;
2293 dev_dbg(hsotg->dev, "GPVNDCTL @0x%08lX : 0x%08X\n",
2294 (unsigned long)addr, readl(addr));
2295 addr = hsotg->regs + GGPIO;
2296 dev_dbg(hsotg->dev, "GGPIO @0x%08lX : 0x%08X\n",
2297 (unsigned long)addr, readl(addr));
2298 addr = hsotg->regs + GUID;
2299 dev_dbg(hsotg->dev, "GUID @0x%08lX : 0x%08X\n",
2300 (unsigned long)addr, readl(addr));
2301 addr = hsotg->regs + GSNPSID;
2302 dev_dbg(hsotg->dev, "GSNPSID @0x%08lX : 0x%08X\n",
2303 (unsigned long)addr, readl(addr));
2304 addr = hsotg->regs + GHWCFG1;
2305 dev_dbg(hsotg->dev, "GHWCFG1 @0x%08lX : 0x%08X\n",
2306 (unsigned long)addr, readl(addr));
2307 addr = hsotg->regs + GHWCFG2;
2308 dev_dbg(hsotg->dev, "GHWCFG2 @0x%08lX : 0x%08X\n",
2309 (unsigned long)addr, readl(addr));
2310 addr = hsotg->regs + GHWCFG3;
2311 dev_dbg(hsotg->dev, "GHWCFG3 @0x%08lX : 0x%08X\n",
2312 (unsigned long)addr, readl(addr));
2313 addr = hsotg->regs + GHWCFG4;
2314 dev_dbg(hsotg->dev, "GHWCFG4 @0x%08lX : 0x%08X\n",
2315 (unsigned long)addr, readl(addr));
2316 addr = hsotg->regs + GLPMCFG;
2317 dev_dbg(hsotg->dev, "GLPMCFG @0x%08lX : 0x%08X\n",
2318 (unsigned long)addr, readl(addr));
2319 addr = hsotg->regs + GPWRDN;
2320 dev_dbg(hsotg->dev, "GPWRDN @0x%08lX : 0x%08X\n",
2321 (unsigned long)addr, readl(addr));
2322 addr = hsotg->regs + GDFIFOCFG;
2323 dev_dbg(hsotg->dev, "GDFIFOCFG @0x%08lX : 0x%08X\n",
2324 (unsigned long)addr, readl(addr));
2325 addr = hsotg->regs + HPTXFSIZ;
2326 dev_dbg(hsotg->dev, "HPTXFSIZ @0x%08lX : 0x%08X\n",
2327 (unsigned long)addr, readl(addr));
2328
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002329 addr = hsotg->regs + PCGCTL;
2330 dev_dbg(hsotg->dev, "PCGCTL @0x%08lX : 0x%08X\n",
2331 (unsigned long)addr, readl(addr));
2332#endif
2333}
2334
2335/**
2336 * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
2337 *
2338 * @hsotg: Programming view of DWC_otg controller
2339 * @num: Tx FIFO to flush
2340 */
2341void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
2342{
2343 u32 greset;
2344 int count = 0;
2345
2346 dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
2347
2348 greset = GRSTCTL_TXFFLSH;
2349 greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
2350 writel(greset, hsotg->regs + GRSTCTL);
2351
2352 do {
2353 greset = readl(hsotg->regs + GRSTCTL);
2354 if (++count > 10000) {
2355 dev_warn(hsotg->dev,
2356 "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
2357 __func__, greset,
2358 readl(hsotg->regs + GNPTXSTS));
2359 break;
2360 }
2361 udelay(1);
2362 } while (greset & GRSTCTL_TXFFLSH);
2363
2364 /* Wait for at least 3 PHY Clocks */
2365 udelay(1);
2366}
2367
2368/**
2369 * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
2370 *
2371 * @hsotg: Programming view of DWC_otg controller
2372 */
2373void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
2374{
2375 u32 greset;
2376 int count = 0;
2377
2378 dev_vdbg(hsotg->dev, "%s()\n", __func__);
2379
2380 greset = GRSTCTL_RXFFLSH;
2381 writel(greset, hsotg->regs + GRSTCTL);
2382
2383 do {
2384 greset = readl(hsotg->regs + GRSTCTL);
2385 if (++count > 10000) {
2386 dev_warn(hsotg->dev, "%s() HANG! GRSTCTL=%0x\n",
2387 __func__, greset);
2388 break;
2389 }
2390 udelay(1);
2391 } while (greset & GRSTCTL_RXFFLSH);
2392
2393 /* Wait for at least 3 PHY Clocks */
2394 udelay(1);
2395}
2396
Paul Zimmerman498f0662013-11-22 16:43:47 -08002397#define DWC2_OUT_OF_BOUNDS(a, b, c) ((a) < (b) || (a) > (c))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002398
2399/* Parameter access functions */
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002400void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002401{
2402 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002403
2404 switch (val) {
2405 case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002406 if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002407 valid = 0;
2408 break;
2409 case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002410 switch (hsotg->hw_params.op_mode) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002411 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
2412 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
2413 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
2414 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
2415 break;
2416 default:
2417 valid = 0;
2418 break;
2419 }
2420 break;
2421 case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
2422 /* always valid */
2423 break;
2424 default:
2425 valid = 0;
2426 break;
2427 }
2428
2429 if (!valid) {
2430 if (val >= 0)
2431 dev_err(hsotg->dev,
2432 "%d invalid for otg_cap parameter. Check HW configuration.\n",
2433 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002434 switch (hsotg->hw_params.op_mode) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002435 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
2436 val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
2437 break;
2438 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
2439 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
2440 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
2441 val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
2442 break;
2443 default:
2444 val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
2445 break;
2446 }
2447 dev_dbg(hsotg->dev, "Setting otg_cap to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002448 }
2449
2450 hsotg->core_params->otg_cap = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002451}
2452
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002453void dwc2_set_param_dma_enable(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002454{
2455 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002456
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002457 if (val > 0 && hsotg->hw_params.arch == GHWCFG2_SLAVE_ONLY_ARCH)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002458 valid = 0;
2459 if (val < 0)
2460 valid = 0;
2461
2462 if (!valid) {
2463 if (val >= 0)
2464 dev_err(hsotg->dev,
2465 "%d invalid for dma_enable parameter. Check HW configuration.\n",
2466 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002467 val = hsotg->hw_params.arch != GHWCFG2_SLAVE_ONLY_ARCH;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002468 dev_dbg(hsotg->dev, "Setting dma_enable to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002469 }
2470
2471 hsotg->core_params->dma_enable = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002472}
2473
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002474void dwc2_set_param_dma_desc_enable(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002475{
2476 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002477
2478 if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002479 !hsotg->hw_params.dma_desc_enable))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002480 valid = 0;
2481 if (val < 0)
2482 valid = 0;
2483
2484 if (!valid) {
2485 if (val >= 0)
2486 dev_err(hsotg->dev,
2487 "%d invalid for dma_desc_enable parameter. Check HW configuration.\n",
2488 val);
2489 val = (hsotg->core_params->dma_enable > 0 &&
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002490 hsotg->hw_params.dma_desc_enable);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002491 dev_dbg(hsotg->dev, "Setting dma_desc_enable to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002492 }
2493
2494 hsotg->core_params->dma_desc_enable = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002495}
2496
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002497void dwc2_set_param_host_support_fs_ls_low_power(struct dwc2_hsotg *hsotg,
2498 int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002499{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002500 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002501 if (val >= 0) {
2502 dev_err(hsotg->dev,
2503 "Wrong value for host_support_fs_low_power\n");
2504 dev_err(hsotg->dev,
2505 "host_support_fs_low_power must be 0 or 1\n");
2506 }
2507 val = 0;
2508 dev_dbg(hsotg->dev,
2509 "Setting host_support_fs_low_power to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002510 }
2511
2512 hsotg->core_params->host_support_fs_ls_low_power = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002513}
2514
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002515void dwc2_set_param_enable_dynamic_fifo(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002516{
2517 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002518
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002519 if (val > 0 && !hsotg->hw_params.enable_dynamic_fifo)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002520 valid = 0;
2521 if (val < 0)
2522 valid = 0;
2523
2524 if (!valid) {
2525 if (val >= 0)
2526 dev_err(hsotg->dev,
2527 "%d invalid for enable_dynamic_fifo parameter. Check HW configuration.\n",
2528 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002529 val = hsotg->hw_params.enable_dynamic_fifo;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002530 dev_dbg(hsotg->dev, "Setting enable_dynamic_fifo to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002531 }
2532
2533 hsotg->core_params->enable_dynamic_fifo = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002534}
2535
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002536void dwc2_set_param_host_rx_fifo_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002537{
2538 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002539
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002540 if (val < 16 || val > hsotg->hw_params.host_rx_fifo_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002541 valid = 0;
2542
2543 if (!valid) {
2544 if (val >= 0)
2545 dev_err(hsotg->dev,
2546 "%d invalid for host_rx_fifo_size. Check HW configuration.\n",
2547 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002548 val = hsotg->hw_params.host_rx_fifo_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002549 dev_dbg(hsotg->dev, "Setting host_rx_fifo_size to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002550 }
2551
2552 hsotg->core_params->host_rx_fifo_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002553}
2554
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002555void dwc2_set_param_host_nperio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002556{
2557 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002558
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002559 if (val < 16 || val > hsotg->hw_params.host_nperio_tx_fifo_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002560 valid = 0;
2561
2562 if (!valid) {
2563 if (val >= 0)
2564 dev_err(hsotg->dev,
2565 "%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
2566 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002567 val = hsotg->hw_params.host_nperio_tx_fifo_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002568 dev_dbg(hsotg->dev, "Setting host_nperio_tx_fifo_size to %d\n",
2569 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002570 }
2571
2572 hsotg->core_params->host_nperio_tx_fifo_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002573}
2574
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002575void dwc2_set_param_host_perio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002576{
2577 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002578
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002579 if (val < 16 || val > hsotg->hw_params.host_perio_tx_fifo_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002580 valid = 0;
2581
2582 if (!valid) {
2583 if (val >= 0)
2584 dev_err(hsotg->dev,
2585 "%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
2586 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002587 val = hsotg->hw_params.host_perio_tx_fifo_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002588 dev_dbg(hsotg->dev, "Setting host_perio_tx_fifo_size to %d\n",
2589 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002590 }
2591
2592 hsotg->core_params->host_perio_tx_fifo_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002593}
2594
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002595void dwc2_set_param_max_transfer_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002596{
2597 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002598
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002599 if (val < 2047 || val > hsotg->hw_params.max_transfer_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002600 valid = 0;
2601
2602 if (!valid) {
2603 if (val >= 0)
2604 dev_err(hsotg->dev,
2605 "%d invalid for max_transfer_size. Check HW configuration.\n",
2606 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002607 val = hsotg->hw_params.max_transfer_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002608 dev_dbg(hsotg->dev, "Setting max_transfer_size to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002609 }
2610
2611 hsotg->core_params->max_transfer_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002612}
2613
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002614void dwc2_set_param_max_packet_count(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002615{
2616 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002617
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002618 if (val < 15 || val > hsotg->hw_params.max_packet_count)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002619 valid = 0;
2620
2621 if (!valid) {
2622 if (val >= 0)
2623 dev_err(hsotg->dev,
2624 "%d invalid for max_packet_count. Check HW configuration.\n",
2625 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002626 val = hsotg->hw_params.max_packet_count;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002627 dev_dbg(hsotg->dev, "Setting max_packet_count to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002628 }
2629
2630 hsotg->core_params->max_packet_count = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002631}
2632
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002633void dwc2_set_param_host_channels(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002634{
2635 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002636
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002637 if (val < 1 || val > hsotg->hw_params.host_channels)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002638 valid = 0;
2639
2640 if (!valid) {
2641 if (val >= 0)
2642 dev_err(hsotg->dev,
2643 "%d invalid for host_channels. Check HW configuration.\n",
2644 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002645 val = hsotg->hw_params.host_channels;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002646 dev_dbg(hsotg->dev, "Setting host_channels to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002647 }
2648
2649 hsotg->core_params->host_channels = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002650}
2651
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002652void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002653{
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002654 int valid = 0;
Luis Ortega Perez de Villar0464a3d2013-09-25 13:10:50 +02002655 u32 hs_phy_type, fs_phy_type;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002656
Paul Zimmerman498f0662013-11-22 16:43:47 -08002657 if (DWC2_OUT_OF_BOUNDS(val, DWC2_PHY_TYPE_PARAM_FS,
2658 DWC2_PHY_TYPE_PARAM_ULPI)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002659 if (val >= 0) {
2660 dev_err(hsotg->dev, "Wrong value for phy_type\n");
2661 dev_err(hsotg->dev, "phy_type must be 0, 1 or 2\n");
2662 }
2663
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002664 valid = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002665 }
2666
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002667 hs_phy_type = hsotg->hw_params.hs_phy_type;
2668 fs_phy_type = hsotg->hw_params.fs_phy_type;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002669 if (val == DWC2_PHY_TYPE_PARAM_UTMI &&
2670 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
2671 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
2672 valid = 1;
2673 else if (val == DWC2_PHY_TYPE_PARAM_ULPI &&
2674 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI ||
2675 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
2676 valid = 1;
2677 else if (val == DWC2_PHY_TYPE_PARAM_FS &&
2678 fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
2679 valid = 1;
2680
2681 if (!valid) {
2682 if (val >= 0)
2683 dev_err(hsotg->dev,
2684 "%d invalid for phy_type. Check HW configuration.\n",
2685 val);
Matthijs Kooijman929aea02013-04-29 19:36:48 +00002686 val = DWC2_PHY_TYPE_PARAM_FS;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002687 if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
2688 if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
2689 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
2690 val = DWC2_PHY_TYPE_PARAM_UTMI;
2691 else
2692 val = DWC2_PHY_TYPE_PARAM_ULPI;
2693 }
2694 dev_dbg(hsotg->dev, "Setting phy_type to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002695 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002696
2697 hsotg->core_params->phy_type = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002698}
2699
2700static int dwc2_get_param_phy_type(struct dwc2_hsotg *hsotg)
2701{
2702 return hsotg->core_params->phy_type;
2703}
2704
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002705void dwc2_set_param_speed(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002706{
2707 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002708
Paul Zimmerman498f0662013-11-22 16:43:47 -08002709 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002710 if (val >= 0) {
2711 dev_err(hsotg->dev, "Wrong value for speed parameter\n");
2712 dev_err(hsotg->dev, "max_speed parameter must be 0 or 1\n");
2713 }
2714 valid = 0;
2715 }
2716
Matthijs Kooijman929aea02013-04-29 19:36:48 +00002717 if (val == DWC2_SPEED_PARAM_HIGH &&
2718 dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002719 valid = 0;
2720
2721 if (!valid) {
2722 if (val >= 0)
2723 dev_err(hsotg->dev,
2724 "%d invalid for speed parameter. Check HW configuration.\n",
2725 val);
2726 val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS ?
Matthijs Kooijman929aea02013-04-29 19:36:48 +00002727 DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002728 dev_dbg(hsotg->dev, "Setting speed to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002729 }
2730
2731 hsotg->core_params->speed = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002732}
2733
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002734void dwc2_set_param_host_ls_low_power_phy_clk(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002735{
2736 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002737
Paul Zimmerman498f0662013-11-22 16:43:47 -08002738 if (DWC2_OUT_OF_BOUNDS(val, DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ,
2739 DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002740 if (val >= 0) {
2741 dev_err(hsotg->dev,
2742 "Wrong value for host_ls_low_power_phy_clk parameter\n");
2743 dev_err(hsotg->dev,
2744 "host_ls_low_power_phy_clk must be 0 or 1\n");
2745 }
2746 valid = 0;
2747 }
2748
2749 if (val == DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ &&
2750 dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
2751 valid = 0;
2752
2753 if (!valid) {
2754 if (val >= 0)
2755 dev_err(hsotg->dev,
2756 "%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
2757 val);
2758 val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS
2759 ? DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ
2760 : DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
2761 dev_dbg(hsotg->dev, "Setting host_ls_low_power_phy_clk to %d\n",
2762 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002763 }
2764
2765 hsotg->core_params->host_ls_low_power_phy_clk = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002766}
2767
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002768void dwc2_set_param_phy_ulpi_ddr(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002769{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002770 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002771 if (val >= 0) {
2772 dev_err(hsotg->dev, "Wrong value for phy_ulpi_ddr\n");
2773 dev_err(hsotg->dev, "phy_upli_ddr must be 0 or 1\n");
2774 }
2775 val = 0;
2776 dev_dbg(hsotg->dev, "Setting phy_upli_ddr to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002777 }
2778
2779 hsotg->core_params->phy_ulpi_ddr = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002780}
2781
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002782void dwc2_set_param_phy_ulpi_ext_vbus(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002783{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002784 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002785 if (val >= 0) {
2786 dev_err(hsotg->dev,
2787 "Wrong value for phy_ulpi_ext_vbus\n");
2788 dev_err(hsotg->dev,
2789 "phy_ulpi_ext_vbus must be 0 or 1\n");
2790 }
2791 val = 0;
2792 dev_dbg(hsotg->dev, "Setting phy_ulpi_ext_vbus to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002793 }
2794
2795 hsotg->core_params->phy_ulpi_ext_vbus = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002796}
2797
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002798void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002799{
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02002800 int valid = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002801
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02002802 switch (hsotg->hw_params.utmi_phy_data_width) {
2803 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
2804 valid = (val == 8);
2805 break;
2806 case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
2807 valid = (val == 16);
2808 break;
2809 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
2810 valid = (val == 8 || val == 16);
2811 break;
2812 }
2813
2814 if (!valid) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002815 if (val >= 0) {
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02002816 dev_err(hsotg->dev,
2817 "%d invalid for phy_utmi_width. Check HW configuration.\n",
2818 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002819 }
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02002820 val = (hsotg->hw_params.utmi_phy_data_width ==
2821 GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002822 dev_dbg(hsotg->dev, "Setting phy_utmi_width to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002823 }
2824
2825 hsotg->core_params->phy_utmi_width = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002826}
2827
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002828void dwc2_set_param_ulpi_fs_ls(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002829{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002830 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002831 if (val >= 0) {
2832 dev_err(hsotg->dev, "Wrong value for ulpi_fs_ls\n");
2833 dev_err(hsotg->dev, "ulpi_fs_ls must be 0 or 1\n");
2834 }
2835 val = 0;
2836 dev_dbg(hsotg->dev, "Setting ulpi_fs_ls to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002837 }
2838
2839 hsotg->core_params->ulpi_fs_ls = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002840}
2841
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002842void dwc2_set_param_ts_dline(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002843{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002844 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002845 if (val >= 0) {
2846 dev_err(hsotg->dev, "Wrong value for ts_dline\n");
2847 dev_err(hsotg->dev, "ts_dline must be 0 or 1\n");
2848 }
2849 val = 0;
2850 dev_dbg(hsotg->dev, "Setting ts_dline to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002851 }
2852
2853 hsotg->core_params->ts_dline = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002854}
2855
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002856void dwc2_set_param_i2c_enable(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002857{
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002858 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002859
Paul Zimmerman498f0662013-11-22 16:43:47 -08002860 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002861 if (val >= 0) {
2862 dev_err(hsotg->dev, "Wrong value for i2c_enable\n");
2863 dev_err(hsotg->dev, "i2c_enable must be 0 or 1\n");
2864 }
2865
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002866 valid = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002867 }
2868
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002869 if (val == 1 && !(hsotg->hw_params.i2c_enable))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002870 valid = 0;
2871
2872 if (!valid) {
2873 if (val >= 0)
2874 dev_err(hsotg->dev,
2875 "%d invalid for i2c_enable. Check HW configuration.\n",
2876 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002877 val = hsotg->hw_params.i2c_enable;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002878 dev_dbg(hsotg->dev, "Setting i2c_enable to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002879 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002880
2881 hsotg->core_params->i2c_enable = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002882}
2883
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002884void dwc2_set_param_en_multiple_tx_fifo(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002885{
2886 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002887
Paul Zimmerman498f0662013-11-22 16:43:47 -08002888 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002889 if (val >= 0) {
2890 dev_err(hsotg->dev,
2891 "Wrong value for en_multiple_tx_fifo,\n");
2892 dev_err(hsotg->dev,
2893 "en_multiple_tx_fifo must be 0 or 1\n");
2894 }
2895 valid = 0;
2896 }
2897
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002898 if (val == 1 && !hsotg->hw_params.en_multiple_tx_fifo)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002899 valid = 0;
2900
2901 if (!valid) {
2902 if (val >= 0)
2903 dev_err(hsotg->dev,
2904 "%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
2905 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002906 val = hsotg->hw_params.en_multiple_tx_fifo;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002907 dev_dbg(hsotg->dev, "Setting en_multiple_tx_fifo to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002908 }
2909
2910 hsotg->core_params->en_multiple_tx_fifo = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002911}
2912
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002913void dwc2_set_param_reload_ctl(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002914{
2915 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002916
Paul Zimmerman498f0662013-11-22 16:43:47 -08002917 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002918 if (val >= 0) {
2919 dev_err(hsotg->dev,
2920 "'%d' invalid for parameter reload_ctl\n", val);
2921 dev_err(hsotg->dev, "reload_ctl must be 0 or 1\n");
2922 }
2923 valid = 0;
2924 }
2925
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002926 if (val == 1 && hsotg->hw_params.snpsid < DWC2_CORE_REV_2_92a)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002927 valid = 0;
2928
2929 if (!valid) {
2930 if (val >= 0)
2931 dev_err(hsotg->dev,
2932 "%d invalid for parameter reload_ctl. Check HW configuration.\n",
2933 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002934 val = hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_92a;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002935 dev_dbg(hsotg->dev, "Setting reload_ctl to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002936 }
2937
2938 hsotg->core_params->reload_ctl = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002939}
2940
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002941void dwc2_set_param_ahbcfg(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002942{
Paul Zimmerman4d3190e2013-07-16 12:22:12 -07002943 if (val != -1)
2944 hsotg->core_params->ahbcfg = val;
2945 else
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02002946 hsotg->core_params->ahbcfg = GAHBCFG_HBSTLEN_INCR4 <<
Luis Ortega Perez de Villar0464a3d2013-09-25 13:10:50 +02002947 GAHBCFG_HBSTLEN_SHIFT;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002948}
2949
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002950void dwc2_set_param_otg_ver(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002951{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002952 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002953 if (val >= 0) {
2954 dev_err(hsotg->dev,
2955 "'%d' invalid for parameter otg_ver\n", val);
2956 dev_err(hsotg->dev,
2957 "otg_ver must be 0 (for OTG 1.3 support) or 1 (for OTG 2.0 support)\n");
2958 }
2959 val = 0;
2960 dev_dbg(hsotg->dev, "Setting otg_ver to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002961 }
2962
2963 hsotg->core_params->otg_ver = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002964}
2965
Wei Yongjun49cf10c2013-11-28 10:27:59 +08002966static void dwc2_set_param_uframe_sched(struct dwc2_hsotg *hsotg, int val)
Paul Zimmermane8576e62013-11-25 13:42:47 -08002967{
2968 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2969 if (val >= 0) {
2970 dev_err(hsotg->dev,
2971 "'%d' invalid for parameter uframe_sched\n",
2972 val);
2973 dev_err(hsotg->dev, "uframe_sched must be 0 or 1\n");
2974 }
2975 val = 1;
2976 dev_dbg(hsotg->dev, "Setting uframe_sched to %d\n", val);
2977 }
2978
2979 hsotg->core_params->uframe_sched = val;
2980}
2981
2982/*
2983 * This function is called during module intialization to pass module parameters
2984 * for the DWC_otg core.
2985 */
2986void dwc2_set_parameters(struct dwc2_hsotg *hsotg,
2987 const struct dwc2_core_params *params)
2988{
2989 dev_dbg(hsotg->dev, "%s()\n", __func__);
2990
2991 dwc2_set_param_otg_cap(hsotg, params->otg_cap);
2992 dwc2_set_param_dma_enable(hsotg, params->dma_enable);
2993 dwc2_set_param_dma_desc_enable(hsotg, params->dma_desc_enable);
2994 dwc2_set_param_host_support_fs_ls_low_power(hsotg,
2995 params->host_support_fs_ls_low_power);
2996 dwc2_set_param_enable_dynamic_fifo(hsotg,
2997 params->enable_dynamic_fifo);
2998 dwc2_set_param_host_rx_fifo_size(hsotg,
2999 params->host_rx_fifo_size);
3000 dwc2_set_param_host_nperio_tx_fifo_size(hsotg,
3001 params->host_nperio_tx_fifo_size);
3002 dwc2_set_param_host_perio_tx_fifo_size(hsotg,
3003 params->host_perio_tx_fifo_size);
3004 dwc2_set_param_max_transfer_size(hsotg,
3005 params->max_transfer_size);
3006 dwc2_set_param_max_packet_count(hsotg,
3007 params->max_packet_count);
3008 dwc2_set_param_host_channels(hsotg, params->host_channels);
3009 dwc2_set_param_phy_type(hsotg, params->phy_type);
3010 dwc2_set_param_speed(hsotg, params->speed);
3011 dwc2_set_param_host_ls_low_power_phy_clk(hsotg,
3012 params->host_ls_low_power_phy_clk);
3013 dwc2_set_param_phy_ulpi_ddr(hsotg, params->phy_ulpi_ddr);
3014 dwc2_set_param_phy_ulpi_ext_vbus(hsotg,
3015 params->phy_ulpi_ext_vbus);
3016 dwc2_set_param_phy_utmi_width(hsotg, params->phy_utmi_width);
3017 dwc2_set_param_ulpi_fs_ls(hsotg, params->ulpi_fs_ls);
3018 dwc2_set_param_ts_dline(hsotg, params->ts_dline);
3019 dwc2_set_param_i2c_enable(hsotg, params->i2c_enable);
3020 dwc2_set_param_en_multiple_tx_fifo(hsotg,
3021 params->en_multiple_tx_fifo);
3022 dwc2_set_param_reload_ctl(hsotg, params->reload_ctl);
3023 dwc2_set_param_ahbcfg(hsotg, params->ahbcfg);
3024 dwc2_set_param_otg_ver(hsotg, params->otg_ver);
3025 dwc2_set_param_uframe_sched(hsotg, params->uframe_sched);
3026}
3027
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003028/**
3029 * During device initialization, read various hardware configuration
3030 * registers and interpret the contents.
3031 */
3032int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
3033{
3034 struct dwc2_hw_params *hw = &hsotg->hw_params;
3035 unsigned width;
3036 u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
3037 u32 hptxfsiz, grxfsiz, gnptxfsiz;
3038 u32 gusbcfg;
3039
3040 /*
3041 * Attempt to ensure this device is really a DWC_otg Controller.
3042 * Read and verify the GSNPSID register contents. The value should be
3043 * 0x45f42xxx or 0x45f43xxx, which corresponds to either "OT2" or "OT3",
3044 * as in "OTG version 2.xx" or "OTG version 3.xx".
3045 */
3046 hw->snpsid = readl(hsotg->regs + GSNPSID);
3047 if ((hw->snpsid & 0xfffff000) != 0x4f542000 &&
3048 (hw->snpsid & 0xfffff000) != 0x4f543000) {
3049 dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
3050 hw->snpsid);
3051 return -ENODEV;
3052 }
3053
3054 dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
3055 hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
3056 hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
3057
3058 hwcfg1 = readl(hsotg->regs + GHWCFG1);
3059 hwcfg2 = readl(hsotg->regs + GHWCFG2);
3060 hwcfg3 = readl(hsotg->regs + GHWCFG3);
3061 hwcfg4 = readl(hsotg->regs + GHWCFG4);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003062 grxfsiz = readl(hsotg->regs + GRXFSIZ);
3063
3064 dev_dbg(hsotg->dev, "hwcfg1=%08x\n", hwcfg1);
3065 dev_dbg(hsotg->dev, "hwcfg2=%08x\n", hwcfg2);
3066 dev_dbg(hsotg->dev, "hwcfg3=%08x\n", hwcfg3);
3067 dev_dbg(hsotg->dev, "hwcfg4=%08x\n", hwcfg4);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003068 dev_dbg(hsotg->dev, "grxfsiz=%08x\n", grxfsiz);
3069
Doug Anderson2867c052014-08-07 12:48:11 -07003070 /* Force host mode to get HPTXFSIZ / GNPTXFSIZ exact power on value */
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003071 gusbcfg = readl(hsotg->regs + GUSBCFG);
3072 gusbcfg |= GUSBCFG_FORCEHOSTMODE;
3073 writel(gusbcfg, hsotg->regs + GUSBCFG);
3074 usleep_range(100000, 150000);
3075
Doug Anderson2867c052014-08-07 12:48:11 -07003076 gnptxfsiz = readl(hsotg->regs + GNPTXFSIZ);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003077 hptxfsiz = readl(hsotg->regs + HPTXFSIZ);
Doug Anderson2867c052014-08-07 12:48:11 -07003078 dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003079 dev_dbg(hsotg->dev, "hptxfsiz=%08x\n", hptxfsiz);
3080 gusbcfg = readl(hsotg->regs + GUSBCFG);
3081 gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
3082 writel(gusbcfg, hsotg->regs + GUSBCFG);
3083 usleep_range(100000, 150000);
3084
3085 /* hwcfg2 */
3086 hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
3087 GHWCFG2_OP_MODE_SHIFT;
3088 hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
3089 GHWCFG2_ARCHITECTURE_SHIFT;
3090 hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
3091 hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
3092 GHWCFG2_NUM_HOST_CHAN_SHIFT);
3093 hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
3094 GHWCFG2_HS_PHY_TYPE_SHIFT;
3095 hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
3096 GHWCFG2_FS_PHY_TYPE_SHIFT;
3097 hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
3098 GHWCFG2_NUM_DEV_EP_SHIFT;
3099 hw->nperio_tx_q_depth =
3100 (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
3101 GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
3102 hw->host_perio_tx_q_depth =
3103 (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
3104 GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
3105 hw->dev_token_q_depth =
3106 (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
3107 GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
3108
3109 /* hwcfg3 */
3110 width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
3111 GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
3112 hw->max_transfer_size = (1 << (width + 11)) - 1;
Paul Zimmermane8f8c142014-09-16 13:47:26 -07003113 /*
3114 * Clip max_transfer_size to 65535. dwc2_hc_setup_align_buf() allocates
3115 * coherent buffers with this size, and if it's too large we can
3116 * exhaust the coherent DMA pool.
3117 */
3118 if (hw->max_transfer_size > 65535)
3119 hw->max_transfer_size = 65535;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003120 width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
3121 GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
3122 hw->max_packet_count = (1 << (width + 4)) - 1;
3123 hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
3124 hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
3125 GHWCFG3_DFIFO_DEPTH_SHIFT;
3126
3127 /* hwcfg4 */
3128 hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
3129 hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
3130 GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
3131 hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
3132 hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02003133 hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
3134 GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003135
3136 /* fifo sizes */
3137 hw->host_rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
3138 GRXFSIZ_DEPTH_SHIFT;
3139 hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
3140 FIFOSIZE_DEPTH_SHIFT;
3141 hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
3142 FIFOSIZE_DEPTH_SHIFT;
3143
3144 dev_dbg(hsotg->dev, "Detected values from hardware:\n");
3145 dev_dbg(hsotg->dev, " op_mode=%d\n",
3146 hw->op_mode);
3147 dev_dbg(hsotg->dev, " arch=%d\n",
3148 hw->arch);
3149 dev_dbg(hsotg->dev, " dma_desc_enable=%d\n",
3150 hw->dma_desc_enable);
3151 dev_dbg(hsotg->dev, " power_optimized=%d\n",
3152 hw->power_optimized);
3153 dev_dbg(hsotg->dev, " i2c_enable=%d\n",
3154 hw->i2c_enable);
3155 dev_dbg(hsotg->dev, " hs_phy_type=%d\n",
3156 hw->hs_phy_type);
3157 dev_dbg(hsotg->dev, " fs_phy_type=%d\n",
3158 hw->fs_phy_type);
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02003159 dev_dbg(hsotg->dev, " utmi_phy_data_wdith=%d\n",
3160 hw->utmi_phy_data_width);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003161 dev_dbg(hsotg->dev, " num_dev_ep=%d\n",
3162 hw->num_dev_ep);
3163 dev_dbg(hsotg->dev, " num_dev_perio_in_ep=%d\n",
3164 hw->num_dev_perio_in_ep);
3165 dev_dbg(hsotg->dev, " host_channels=%d\n",
3166 hw->host_channels);
3167 dev_dbg(hsotg->dev, " max_transfer_size=%d\n",
3168 hw->max_transfer_size);
3169 dev_dbg(hsotg->dev, " max_packet_count=%d\n",
3170 hw->max_packet_count);
3171 dev_dbg(hsotg->dev, " nperio_tx_q_depth=0x%0x\n",
3172 hw->nperio_tx_q_depth);
3173 dev_dbg(hsotg->dev, " host_perio_tx_q_depth=0x%0x\n",
3174 hw->host_perio_tx_q_depth);
3175 dev_dbg(hsotg->dev, " dev_token_q_depth=0x%0x\n",
3176 hw->dev_token_q_depth);
3177 dev_dbg(hsotg->dev, " enable_dynamic_fifo=%d\n",
3178 hw->enable_dynamic_fifo);
3179 dev_dbg(hsotg->dev, " en_multiple_tx_fifo=%d\n",
3180 hw->en_multiple_tx_fifo);
3181 dev_dbg(hsotg->dev, " total_fifo_size=%d\n",
3182 hw->total_fifo_size);
3183 dev_dbg(hsotg->dev, " host_rx_fifo_size=%d\n",
3184 hw->host_rx_fifo_size);
3185 dev_dbg(hsotg->dev, " host_nperio_tx_fifo_size=%d\n",
3186 hw->host_nperio_tx_fifo_size);
3187 dev_dbg(hsotg->dev, " host_perio_tx_fifo_size=%d\n",
3188 hw->host_perio_tx_fifo_size);
3189 dev_dbg(hsotg->dev, "\n");
3190
3191 return 0;
3192}
3193
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003194u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg)
3195{
Paul Zimmermanb66a3f02013-11-22 16:43:50 -08003196 return hsotg->core_params->otg_ver == 1 ? 0x0200 : 0x0103;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003197}
3198
Paul Zimmerman057715f2013-11-22 16:43:51 -08003199bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003200{
3201 if (readl(hsotg->regs + GSNPSID) == 0xffffffff)
Paul Zimmerman057715f2013-11-22 16:43:51 -08003202 return false;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003203 else
Paul Zimmerman057715f2013-11-22 16:43:51 -08003204 return true;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003205}
3206
3207/**
3208 * dwc2_enable_global_interrupts() - Enables the controller's Global
3209 * Interrupt in the AHB Config register
3210 *
3211 * @hsotg: Programming view of DWC_otg controller
3212 */
3213void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
3214{
3215 u32 ahbcfg = readl(hsotg->regs + GAHBCFG);
3216
3217 ahbcfg |= GAHBCFG_GLBL_INTR_EN;
3218 writel(ahbcfg, hsotg->regs + GAHBCFG);
3219}
3220
3221/**
3222 * dwc2_disable_global_interrupts() - Disables the controller's Global
3223 * Interrupt in the AHB Config register
3224 *
3225 * @hsotg: Programming view of DWC_otg controller
3226 */
3227void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
3228{
3229 u32 ahbcfg = readl(hsotg->regs + GAHBCFG);
3230
3231 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
3232 writel(ahbcfg, hsotg->regs + GAHBCFG);
3233}
3234
3235MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
3236MODULE_AUTHOR("Synopsys, Inc.");
3237MODULE_LICENSE("Dual BSD/GPL");