blob: bf5e951fbb7f8b400aa96e6bceb1792856119b3d [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 */
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +020075 hr = &hsotg->hr_backup;
Antti Seppälä95c8bc32015-08-20 21:41:07 +030076 hr->hcfg = dwc2_readl(hsotg->regs + HCFG);
77 hr->haintmsk = dwc2_readl(hsotg->regs + HAINTMSK);
Gregory Herrerod17ee772015-04-29 22:09:01 +020078 for (i = 0; i < hsotg->core_params->host_channels; ++i)
Antti Seppälä95c8bc32015-08-20 21:41:07 +030079 hr->hcintmsk[i] = dwc2_readl(hsotg->regs + HCINTMSK(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +020080
Gregory Herrerocc047ce2015-09-22 15:16:37 +020081 hr->hprt0 = dwc2_read_hprt0(hsotg);
Antti Seppälä95c8bc32015-08-20 21:41:07 +030082 hr->hfir = dwc2_readl(hsotg->regs + HFIR);
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +020083 hr->valid = true;
Gregory Herrerod17ee772015-04-29 22:09:01 +020084
85 return 0;
86}
87
88/**
89 * dwc2_restore_host_registers() - Restore controller host registers.
90 * When resuming usb bus, device registers needs to be restored
91 * if controller power were disabled.
92 *
93 * @hsotg: Programming view of the DWC_otg controller
94 */
95static int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
96{
97 struct dwc2_hregs_backup *hr;
98 int i;
99
100 dev_dbg(hsotg->dev, "%s\n", __func__);
101
102 /* Restore host regs */
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200103 hr = &hsotg->hr_backup;
104 if (!hr->valid) {
Gregory Herrerod17ee772015-04-29 22:09:01 +0200105 dev_err(hsotg->dev, "%s: no host registers to restore\n",
106 __func__);
107 return -EINVAL;
108 }
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200109 hr->valid = false;
Gregory Herrerod17ee772015-04-29 22:09:01 +0200110
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300111 dwc2_writel(hr->hcfg, hsotg->regs + HCFG);
112 dwc2_writel(hr->haintmsk, hsotg->regs + HAINTMSK);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200113
114 for (i = 0; i < hsotg->core_params->host_channels; ++i)
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300115 dwc2_writel(hr->hcintmsk[i], hsotg->regs + HCINTMSK(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200116
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300117 dwc2_writel(hr->hprt0, hsotg->regs + HPRT0);
118 dwc2_writel(hr->hfir, hsotg->regs + HFIR);
Gregory Herrero08c4ffc2015-09-22 15:16:45 +0200119 hsotg->frame_number = 0;
Gregory Herrerod17ee772015-04-29 22:09:01 +0200120
121 return 0;
122}
123#else
124static inline int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
125{ return 0; }
126
127static inline int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
128{ return 0; }
129#endif
130
131#if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
132 IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
133/**
134 * dwc2_backup_device_registers() - Backup controller device registers.
135 * When suspending usb bus, registers needs to be backuped
136 * if controller power is disabled once suspended.
137 *
138 * @hsotg: Programming view of the DWC_otg controller
139 */
140static int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
141{
142 struct dwc2_dregs_backup *dr;
143 int i;
144
145 dev_dbg(hsotg->dev, "%s\n", __func__);
146
147 /* Backup dev regs */
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200148 dr = &hsotg->dr_backup;
Gregory Herrerod17ee772015-04-29 22:09:01 +0200149
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300150 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
151 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
152 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
153 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
154 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200155
156 for (i = 0; i < hsotg->num_of_eps; i++) {
157 /* Backup IN EPs */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300158 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200159
160 /* Ensure DATA PID is correctly configured */
161 if (dr->diepctl[i] & DXEPCTL_DPID)
162 dr->diepctl[i] |= DXEPCTL_SETD1PID;
163 else
164 dr->diepctl[i] |= DXEPCTL_SETD0PID;
165
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300166 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
167 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200168
169 /* Backup OUT EPs */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300170 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200171
172 /* Ensure DATA PID is correctly configured */
173 if (dr->doepctl[i] & DXEPCTL_DPID)
174 dr->doepctl[i] |= DXEPCTL_SETD1PID;
175 else
176 dr->doepctl[i] |= DXEPCTL_SETD0PID;
177
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300178 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
179 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200180 }
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200181 dr->valid = true;
Gregory Herrerod17ee772015-04-29 22:09:01 +0200182 return 0;
183}
184
185/**
186 * dwc2_restore_device_registers() - Restore controller device registers.
187 * When resuming usb bus, device registers needs to be restored
188 * if controller power were disabled.
189 *
190 * @hsotg: Programming view of the DWC_otg controller
191 */
192static int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
193{
194 struct dwc2_dregs_backup *dr;
195 u32 dctl;
196 int i;
197
198 dev_dbg(hsotg->dev, "%s\n", __func__);
199
200 /* Restore dev regs */
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200201 dr = &hsotg->dr_backup;
202 if (!dr->valid) {
Gregory Herrerod17ee772015-04-29 22:09:01 +0200203 dev_err(hsotg->dev, "%s: no device registers to restore\n",
204 __func__);
205 return -EINVAL;
206 }
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200207 dr->valid = false;
Gregory Herrerod17ee772015-04-29 22:09:01 +0200208
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300209 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
210 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
211 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
212 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
213 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200214
215 for (i = 0; i < hsotg->num_of_eps; i++) {
216 /* Restore IN EPs */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300217 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
218 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
219 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200220
221 /* Restore OUT EPs */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300222 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
223 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
224 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200225 }
226
227 /* Set the Power-On Programming done bit */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300228 dctl = dwc2_readl(hsotg->regs + DCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200229 dctl |= DCTL_PWRONPRGDONE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300230 dwc2_writel(dctl, hsotg->regs + DCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200231
232 return 0;
233}
234#else
235static inline int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
236{ return 0; }
237
238static inline int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
239{ return 0; }
240#endif
241
242/**
243 * dwc2_backup_global_registers() - Backup global controller registers.
244 * When suspending usb bus, registers needs to be backuped
245 * if controller power is disabled once suspended.
246 *
247 * @hsotg: Programming view of the DWC_otg controller
248 */
249static int dwc2_backup_global_registers(struct dwc2_hsotg *hsotg)
250{
251 struct dwc2_gregs_backup *gr;
252 int i;
253
254 /* Backup global regs */
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200255 gr = &hsotg->gr_backup;
Gregory Herrerod17ee772015-04-29 22:09:01 +0200256
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300257 gr->gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
258 gr->gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
259 gr->gahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
260 gr->gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
261 gr->grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
262 gr->gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
263 gr->hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
264 gr->gdfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200265 for (i = 0; i < MAX_EPS_CHANNELS; i++)
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300266 gr->dtxfsiz[i] = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200267
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200268 gr->valid = true;
Gregory Herrerod17ee772015-04-29 22:09:01 +0200269 return 0;
270}
271
272/**
273 * dwc2_restore_global_registers() - Restore controller global registers.
274 * When resuming usb bus, device registers needs to be restored
275 * if controller power were disabled.
276 *
277 * @hsotg: Programming view of the DWC_otg controller
278 */
279static int dwc2_restore_global_registers(struct dwc2_hsotg *hsotg)
280{
281 struct dwc2_gregs_backup *gr;
282 int i;
283
284 dev_dbg(hsotg->dev, "%s\n", __func__);
285
286 /* Restore global regs */
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200287 gr = &hsotg->gr_backup;
288 if (!gr->valid) {
Gregory Herrerod17ee772015-04-29 22:09:01 +0200289 dev_err(hsotg->dev, "%s: no global registers to restore\n",
290 __func__);
291 return -EINVAL;
292 }
Mian Yousaf Kaukabcc1e2042015-06-29 11:05:30 +0200293 gr->valid = false;
Gregory Herrerod17ee772015-04-29 22:09:01 +0200294
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300295 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
296 dwc2_writel(gr->gotgctl, hsotg->regs + GOTGCTL);
297 dwc2_writel(gr->gintmsk, hsotg->regs + GINTMSK);
298 dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
299 dwc2_writel(gr->gahbcfg, hsotg->regs + GAHBCFG);
300 dwc2_writel(gr->grxfsiz, hsotg->regs + GRXFSIZ);
301 dwc2_writel(gr->gnptxfsiz, hsotg->regs + GNPTXFSIZ);
302 dwc2_writel(gr->hptxfsiz, hsotg->regs + HPTXFSIZ);
303 dwc2_writel(gr->gdfifocfg, hsotg->regs + GDFIFOCFG);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200304 for (i = 0; i < MAX_EPS_CHANNELS; i++)
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300305 dwc2_writel(gr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
Gregory Herrerod17ee772015-04-29 22:09:01 +0200306
307 return 0;
308}
309
310/**
311 * dwc2_exit_hibernation() - Exit controller from Partial Power Down.
312 *
313 * @hsotg: Programming view of the DWC_otg controller
314 * @restore: Controller registers need to be restored
315 */
316int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, bool restore)
317{
318 u32 pcgcctl;
319 int ret = 0;
320
Gregory Herrero285046a2015-04-29 22:09:19 +0200321 if (!hsotg->core_params->hibernation)
322 return -ENOTSUPP;
323
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300324 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200325 pcgcctl &= ~PCGCTL_STOPPCLK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300326 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200327
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300328 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200329 pcgcctl &= ~PCGCTL_PWRCLMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300330 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200331
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300332 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200333 pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300334 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200335
336 udelay(100);
337 if (restore) {
338 ret = dwc2_restore_global_registers(hsotg);
339 if (ret) {
340 dev_err(hsotg->dev, "%s: failed to restore registers\n",
341 __func__);
342 return ret;
343 }
344 if (dwc2_is_host_mode(hsotg)) {
345 ret = dwc2_restore_host_registers(hsotg);
346 if (ret) {
347 dev_err(hsotg->dev, "%s: failed to restore host registers\n",
348 __func__);
349 return ret;
350 }
351 } else {
352 ret = dwc2_restore_device_registers(hsotg);
353 if (ret) {
354 dev_err(hsotg->dev, "%s: failed to restore device registers\n",
355 __func__);
356 return ret;
357 }
358 }
359 }
360
361 return ret;
362}
363
364/**
365 * dwc2_enter_hibernation() - Put controller in Partial Power Down.
366 *
367 * @hsotg: Programming view of the DWC_otg controller
368 */
369int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg)
370{
371 u32 pcgcctl;
372 int ret = 0;
373
Gregory Herrero285046a2015-04-29 22:09:19 +0200374 if (!hsotg->core_params->hibernation)
375 return -ENOTSUPP;
376
Gregory Herrerod17ee772015-04-29 22:09:01 +0200377 /* Backup all registers */
378 ret = dwc2_backup_global_registers(hsotg);
379 if (ret) {
380 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
381 __func__);
382 return ret;
383 }
384
385 if (dwc2_is_host_mode(hsotg)) {
386 ret = dwc2_backup_host_registers(hsotg);
387 if (ret) {
388 dev_err(hsotg->dev, "%s: failed to backup host registers\n",
389 __func__);
390 return ret;
391 }
392 } else {
393 ret = dwc2_backup_device_registers(hsotg);
394 if (ret) {
395 dev_err(hsotg->dev, "%s: failed to backup device registers\n",
396 __func__);
397 return ret;
398 }
399 }
400
Gregory Herrerocad73da2015-09-22 15:16:49 +0200401 /*
402 * Clear any pending interrupts since dwc2 will not be able to
403 * clear them after entering hibernation.
404 */
405 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
406
Gregory Herrerod17ee772015-04-29 22:09:01 +0200407 /* Put the controller in low power state */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300408 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200409
410 pcgcctl |= PCGCTL_PWRCLMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300411 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200412 ndelay(20);
413
414 pcgcctl |= PCGCTL_RSTPDWNMODULE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300415 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200416 ndelay(20);
417
418 pcgcctl |= PCGCTL_STOPPCLK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300419 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
Gregory Herrerod17ee772015-04-29 22:09:01 +0200420
421 return ret;
422}
423
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700424/**
425 * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
426 * used in both device and host modes
427 *
428 * @hsotg: Programming view of the DWC_otg controller
429 */
430static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
431{
432 u32 intmsk;
433
434 /* Clear any pending OTG Interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300435 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700436
437 /* Clear any pending interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300438 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700439
440 /* Enable the interrupts in the GINTMSK */
441 intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
442
443 if (hsotg->core_params->dma_enable <= 0)
444 intmsk |= GINTSTS_RXFLVL;
Gregory Herreroa6d249d2015-04-29 22:09:04 +0200445 if (hsotg->core_params->external_id_pin_ctl <= 0)
446 intmsk |= GINTSTS_CONIDSTSCHNG;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700447
Gregory Herreroa6d249d2015-04-29 22:09:04 +0200448 intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP |
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700449 GINTSTS_SESSREQINT;
450
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300451 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700452}
453
454/*
455 * Initializes the FSLSPClkSel field of the HCFG register depending on the
456 * PHY type
457 */
458static void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
459{
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700460 u32 hcfg, val;
461
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200462 if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
463 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700464 hsotg->core_params->ulpi_fs_ls > 0) ||
465 hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
466 /* Full speed PHY */
467 val = HCFG_FSLSPCLKSEL_48_MHZ;
468 } else {
469 /* High speed PHY running at full speed or high speed */
470 val = HCFG_FSLSPCLKSEL_30_60_MHZ;
471 }
472
473 dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300474 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700475 hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200476 hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300477 dwc2_writel(hcfg, hsotg->regs + HCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700478}
479
480/*
481 * Do core a soft reset of the core. Be careful with this because it
482 * resets all the internal state machines of the core.
483 */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100484static int dwc2_core_reset(struct dwc2_hsotg *hsotg)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700485{
486 u32 greset;
487 int count = 0;
Kever Yangc0155b92014-08-06 09:01:50 +0800488 u32 gusbcfg;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700489
490 dev_vdbg(hsotg->dev, "%s()\n", __func__);
491
492 /* Wait for AHB master IDLE state */
493 do {
494 usleep_range(20000, 40000);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300495 greset = dwc2_readl(hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700496 if (++count > 50) {
497 dev_warn(hsotg->dev,
498 "%s() HANG! AHB Idle GRSTCTL=%0x\n",
499 __func__, greset);
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100500 return -EBUSY;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700501 }
502 } while (!(greset & GRSTCTL_AHBIDLE));
503
504 /* Core Soft Reset */
505 count = 0;
506 greset |= GRSTCTL_CSFTRST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300507 dwc2_writel(greset, hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700508 do {
509 usleep_range(20000, 40000);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300510 greset = dwc2_readl(hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700511 if (++count > 50) {
512 dev_warn(hsotg->dev,
513 "%s() HANG! Soft Reset GRSTCTL=%0x\n",
514 __func__, greset);
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100515 return -EBUSY;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700516 }
517 } while (greset & GRSTCTL_CSFTRST);
518
Kever Yangc0155b92014-08-06 09:01:50 +0800519 if (hsotg->dr_mode == USB_DR_MODE_HOST) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300520 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Kever Yangc0155b92014-08-06 09:01:50 +0800521 gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
522 gusbcfg |= GUSBCFG_FORCEHOSTMODE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300523 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
Kever Yangc0155b92014-08-06 09:01:50 +0800524 } else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300525 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Kever Yangc0155b92014-08-06 09:01:50 +0800526 gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
527 gusbcfg |= GUSBCFG_FORCEDEVMODE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300528 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
Kever Yangc0155b92014-08-06 09:01:50 +0800529 } else if (hsotg->dr_mode == USB_DR_MODE_OTG) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300530 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Kever Yangc0155b92014-08-06 09:01:50 +0800531 gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
532 gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300533 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
Kever Yangc0155b92014-08-06 09:01:50 +0800534 }
535
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700536 /*
537 * NOTE: This long sleep is _very_ important, otherwise the core will
538 * not stay in host mode after a connector ID change!
539 */
540 usleep_range(150000, 200000);
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100541
542 return 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700543}
544
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100545static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700546{
547 u32 usbcfg, i2cctl;
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100548 int retval = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700549
550 /*
551 * core_init() is now called on every switch so only call the
552 * following for the first time through
553 */
554 if (select_phy) {
555 dev_dbg(hsotg->dev, "FS PHY selected\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300556 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700557 usbcfg |= GUSBCFG_PHYSEL;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300558 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700559
560 /* Reset after a PHY select */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100561 retval = dwc2_core_reset(hsotg);
562 if (retval) {
563 dev_err(hsotg->dev, "%s() Reset failed, aborting",
564 __func__);
565 return retval;
566 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700567 }
568
569 /*
570 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
571 * do this on HNP Dev/Host mode switches (done in dev_init and
572 * host_init).
573 */
574 if (dwc2_is_host_mode(hsotg))
575 dwc2_init_fs_ls_pclk_sel(hsotg);
576
577 if (hsotg->core_params->i2c_enable > 0) {
578 dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
579
580 /* Program GUSBCFG.OtgUtmiFsSel to I2C */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300581 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700582 usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300583 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700584
585 /* Program GI2CCTL.I2CEn */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300586 i2cctl = dwc2_readl(hsotg->regs + GI2CCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700587 i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
588 i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
589 i2cctl &= ~GI2CCTL_I2CEN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300590 dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700591 i2cctl |= GI2CCTL_I2CEN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300592 dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700593 }
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100594
595 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700596}
597
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100598static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700599{
600 u32 usbcfg;
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100601 int retval = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700602
603 if (!select_phy)
Paul Zimmermana23666c2014-02-04 11:42:15 -0800604 return 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700605
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300606 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700607
608 /*
609 * HS PHY parameters. These parameters are preserved during soft reset
610 * so only program the first time. Do a soft reset immediately after
611 * setting phyif.
612 */
613 switch (hsotg->core_params->phy_type) {
614 case DWC2_PHY_TYPE_PARAM_ULPI:
615 /* ULPI interface */
616 dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
617 usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
618 usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
619 if (hsotg->core_params->phy_ulpi_ddr > 0)
620 usbcfg |= GUSBCFG_DDRSEL;
621 break;
622 case DWC2_PHY_TYPE_PARAM_UTMI:
623 /* UTMI+ interface */
624 dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
625 usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
626 if (hsotg->core_params->phy_utmi_width == 16)
627 usbcfg |= GUSBCFG_PHYIF16;
628 break;
629 default:
630 dev_err(hsotg->dev, "FS PHY selected at HS!\n");
631 break;
632 }
633
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300634 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700635
636 /* Reset after setting the PHY parameters */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100637 retval = dwc2_core_reset(hsotg);
638 if (retval) {
639 dev_err(hsotg->dev, "%s() Reset failed, aborting",
640 __func__);
641 return retval;
642 }
643
644 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700645}
646
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100647static int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700648{
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200649 u32 usbcfg;
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100650 int retval = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700651
652 if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL &&
653 hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
654 /* If FS mode with FS PHY */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100655 retval = dwc2_fs_phy_init(hsotg, select_phy);
656 if (retval)
657 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700658 } else {
659 /* High speed PHY */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100660 retval = dwc2_hs_phy_init(hsotg, select_phy);
661 if (retval)
662 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700663 }
664
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200665 if (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
666 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700667 hsotg->core_params->ulpi_fs_ls > 0) {
668 dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300669 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700670 usbcfg |= GUSBCFG_ULPI_FS_LS;
671 usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300672 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700673 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300674 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700675 usbcfg &= ~GUSBCFG_ULPI_FS_LS;
676 usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300677 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700678 }
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100679
680 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700681}
682
683static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
684{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300685 u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700686
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200687 switch (hsotg->hw_params.arch) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700688 case GHWCFG2_EXT_DMA_ARCH:
689 dev_err(hsotg->dev, "External DMA Mode not supported\n");
690 return -EINVAL;
691
692 case GHWCFG2_INT_DMA_ARCH:
693 dev_dbg(hsotg->dev, "Internal DMA Mode\n");
Paul Zimmerman4d3190e2013-07-16 12:22:12 -0700694 if (hsotg->core_params->ahbcfg != -1) {
695 ahbcfg &= GAHBCFG_CTRL_MASK;
696 ahbcfg |= hsotg->core_params->ahbcfg &
697 ~GAHBCFG_CTRL_MASK;
698 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700699 break;
700
701 case GHWCFG2_SLAVE_ONLY_ARCH:
702 default:
703 dev_dbg(hsotg->dev, "Slave Only Mode\n");
704 break;
705 }
706
707 dev_dbg(hsotg->dev, "dma_enable:%d dma_desc_enable:%d\n",
708 hsotg->core_params->dma_enable,
709 hsotg->core_params->dma_desc_enable);
710
711 if (hsotg->core_params->dma_enable > 0) {
712 if (hsotg->core_params->dma_desc_enable > 0)
713 dev_dbg(hsotg->dev, "Using Descriptor DMA mode\n");
714 else
715 dev_dbg(hsotg->dev, "Using Buffer DMA mode\n");
716 } else {
717 dev_dbg(hsotg->dev, "Using Slave mode\n");
718 hsotg->core_params->dma_desc_enable = 0;
719 }
720
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700721 if (hsotg->core_params->dma_enable > 0)
722 ahbcfg |= GAHBCFG_DMA_EN;
723
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300724 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700725
726 return 0;
727}
728
729static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
730{
731 u32 usbcfg;
732
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300733 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700734 usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
735
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200736 switch (hsotg->hw_params.op_mode) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700737 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
738 if (hsotg->core_params->otg_cap ==
739 DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
740 usbcfg |= GUSBCFG_HNPCAP;
741 if (hsotg->core_params->otg_cap !=
742 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
743 usbcfg |= GUSBCFG_SRPCAP;
744 break;
745
746 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
747 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
748 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
749 if (hsotg->core_params->otg_cap !=
750 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
751 usbcfg |= GUSBCFG_SRPCAP;
752 break;
753
754 case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
755 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
756 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
757 default:
758 break;
759 }
760
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300761 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700762}
763
764/**
765 * dwc2_core_init() - Initializes the DWC_otg controller registers and
766 * prepares the core for device mode or host mode operation
767 *
768 * @hsotg: Programming view of the DWC_otg controller
769 * @select_phy: If true then also set the Phy type
Matthijs Kooijman6706c722013-04-11 17:52:41 +0200770 * @irq: If >= 0, the irq to register
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700771 */
Matthijs Kooijman6706c722013-04-11 17:52:41 +0200772int dwc2_core_init(struct dwc2_hsotg *hsotg, bool select_phy, int irq)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700773{
774 u32 usbcfg, otgctl;
775 int retval;
776
777 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
778
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300779 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700780
781 /* Set ULPI External VBUS bit if needed */
782 usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
783 if (hsotg->core_params->phy_ulpi_ext_vbus ==
784 DWC2_PHY_ULPI_EXTERNAL_VBUS)
785 usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
786
787 /* Set external TS Dline pulsing bit if needed */
788 usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
789 if (hsotg->core_params->ts_dline > 0)
790 usbcfg |= GUSBCFG_TERMSELDLPULSE;
791
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300792 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700793
794 /* Reset the Controller */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100795 retval = dwc2_core_reset(hsotg);
796 if (retval) {
797 dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
798 __func__);
799 return retval;
800 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700801
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700802 /*
803 * This needs to happen in FS mode before any other programming occurs
804 */
Julien DELACOUbeb7e592013-11-20 17:29:49 +0100805 retval = dwc2_phy_init(hsotg, select_phy);
806 if (retval)
807 return retval;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700808
809 /* Program the GAHBCFG Register */
810 retval = dwc2_gahbcfg_init(hsotg);
811 if (retval)
812 return retval;
813
814 /* Program the GUSBCFG register */
815 dwc2_gusbcfg_init(hsotg);
816
817 /* Program the GOTGCTL register */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300818 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700819 otgctl &= ~GOTGCTL_OTGVER;
820 if (hsotg->core_params->otg_ver > 0)
821 otgctl |= GOTGCTL_OTGVER;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300822 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700823 dev_dbg(hsotg->dev, "OTG VER PARAM: %d\n", hsotg->core_params->otg_ver);
824
825 /* Clear the SRP success bit for FS-I2c */
826 hsotg->srp_success = 0;
827
828 /* Enable common interrupts */
829 dwc2_enable_common_interrupts(hsotg);
830
831 /*
Mickael Maison997f4f82014-12-23 17:39:45 +0100832 * Do device or host initialization based on mode during PCD and
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700833 * HCD initialization
834 */
835 if (dwc2_is_host_mode(hsotg)) {
836 dev_dbg(hsotg->dev, "Host Mode\n");
837 hsotg->op_state = OTG_STATE_A_HOST;
838 } else {
839 dev_dbg(hsotg->dev, "Device Mode\n");
840 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
841 }
842
843 return 0;
844}
845
846/**
847 * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
848 *
849 * @hsotg: Programming view of DWC_otg controller
850 */
851void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
852{
853 u32 intmsk;
854
855 dev_dbg(hsotg->dev, "%s()\n", __func__);
856
857 /* Disable all interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300858 dwc2_writel(0, hsotg->regs + GINTMSK);
859 dwc2_writel(0, hsotg->regs + HAINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700860
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700861 /* Enable the common interrupts */
862 dwc2_enable_common_interrupts(hsotg);
863
864 /* Enable host mode interrupts without disturbing common interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300865 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
Mian Yousaf Kaukab77dbf712015-09-22 15:16:47 +0200866 intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT |
867 GINTSTS_DISCONNINT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300868 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700869}
870
871/**
872 * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
873 *
874 * @hsotg: Programming view of DWC_otg controller
875 */
876void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
877{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300878 u32 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700879
880 /* Disable host mode interrupts without disturbing common interrupts */
881 intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
Mian Yousaf Kaukab77dbf712015-09-22 15:16:47 +0200882 GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300883 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700884}
885
Dinh Nguyen112fe8e2014-05-07 08:31:29 -0500886/*
887 * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
888 * For system that have a total fifo depth that is smaller than the default
889 * RX + TX fifo size.
890 *
891 * @hsotg: Programming view of DWC_otg controller
892 */
893static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
894{
895 struct dwc2_core_params *params = hsotg->core_params;
896 struct dwc2_hw_params *hw = &hsotg->hw_params;
897 u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
898
899 total_fifo_size = hw->total_fifo_size;
900 rxfsiz = params->host_rx_fifo_size;
901 nptxfsiz = params->host_nperio_tx_fifo_size;
902 ptxfsiz = params->host_perio_tx_fifo_size;
903
904 /*
905 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
906 * allocation with support for high bandwidth endpoints. Synopsys
907 * defines MPS(Max Packet size) for a periodic EP=1024, and for
908 * non-periodic as 512.
909 */
910 if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
911 /*
912 * For Buffer DMA mode/Scatter Gather DMA mode
913 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
914 * with n = number of host channel.
915 * 2 * ((1024/4) + 2) = 516
916 */
917 rxfsiz = 516 + hw->host_channels;
918
919 /*
920 * min non-periodic tx fifo depth
921 * 2 * (largest non-periodic USB packet used / 4)
922 * 2 * (512/4) = 256
923 */
924 nptxfsiz = 256;
925
926 /*
927 * min periodic tx fifo depth
928 * (largest packet size*MC)/4
929 * (1024 * 3)/4 = 768
930 */
931 ptxfsiz = 768;
932
933 params->host_rx_fifo_size = rxfsiz;
934 params->host_nperio_tx_fifo_size = nptxfsiz;
935 params->host_perio_tx_fifo_size = ptxfsiz;
936 }
937
938 /*
939 * If the summation of RX, NPTX and PTX fifo sizes is still
940 * bigger than the total_fifo_size, then we have a problem.
941 *
942 * We won't be able to allocate as many endpoints. Right now,
943 * we're just printing an error message, but ideally this FIFO
944 * allocation algorithm would be improved in the future.
945 *
946 * FIXME improve this FIFO allocation algorithm.
947 */
948 if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
949 dev_err(hsotg->dev, "invalid fifo sizes\n");
950}
951
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700952static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
953{
954 struct dwc2_core_params *params = hsotg->core_params;
Matthijs Kooijmana1fc5242013-08-30 18:45:20 +0200955 u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700956
Matthijs Kooijman12086052013-04-29 19:46:35 +0000957 if (!params->enable_dynamic_fifo)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700958 return;
959
Dinh Nguyen112fe8e2014-05-07 08:31:29 -0500960 dwc2_calculate_dynamic_fifo(hsotg);
961
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700962 /* Rx FIFO */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300963 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
Matthijs Kooijmana1fc5242013-08-30 18:45:20 +0200964 dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
965 grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
966 grxfsiz |= params->host_rx_fifo_size <<
967 GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300968 dwc2_writel(grxfsiz, hsotg->regs + GRXFSIZ);
969 dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
970 dwc2_readl(hsotg->regs + GRXFSIZ));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700971
972 /* Non-periodic Tx FIFO */
973 dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300974 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700975 nptxfsiz = params->host_nperio_tx_fifo_size <<
976 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
977 nptxfsiz |= params->host_rx_fifo_size <<
978 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300979 dwc2_writel(nptxfsiz, hsotg->regs + GNPTXFSIZ);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700980 dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300981 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700982
983 /* Periodic Tx FIFO */
984 dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300985 dwc2_readl(hsotg->regs + HPTXFSIZ));
Matthijs Kooijmanc35205a2013-08-30 18:45:18 +0200986 hptxfsiz = params->host_perio_tx_fifo_size <<
987 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
988 hptxfsiz |= (params->host_rx_fifo_size +
989 params->host_nperio_tx_fifo_size) <<
990 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300991 dwc2_writel(hptxfsiz, hsotg->regs + HPTXFSIZ);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700992 dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300993 dwc2_readl(hsotg->regs + HPTXFSIZ));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700994
995 if (hsotg->core_params->en_multiple_tx_fifo > 0 &&
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200996 hsotg->hw_params.snpsid <= DWC2_CORE_REV_2_94a) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700997 /*
998 * Global DFIFOCFG calculation for Host mode -
999 * include RxFIFO, NPTXFIFO and HPTXFIFO
1000 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001001 dfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001002 dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
Matthijs Kooijman08b9f9d2013-08-30 18:45:19 +02001003 dfifocfg |= (params->host_rx_fifo_size +
1004 params->host_nperio_tx_fifo_size +
1005 params->host_perio_tx_fifo_size) <<
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001006 GDFIFOCFG_EPINFOBASE_SHIFT &
1007 GDFIFOCFG_EPINFOBASE_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001008 dwc2_writel(dfifocfg, hsotg->regs + GDFIFOCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001009 }
1010}
1011
1012/**
1013 * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
1014 * Host mode
1015 *
1016 * @hsotg: Programming view of DWC_otg controller
1017 *
1018 * This function flushes the Tx and Rx FIFOs and flushes any entries in the
1019 * request queues. Host channels are reset to ensure that they are ready for
1020 * performing transfers.
1021 */
1022void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
1023{
1024 u32 hcfg, hfir, otgctl;
1025
1026 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
1027
1028 /* Restart the Phy Clock */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001029 dwc2_writel(0, hsotg->regs + PCGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001030
1031 /* Initialize Host Configuration Register */
1032 dwc2_init_fs_ls_pclk_sel(hsotg);
1033 if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001034 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001035 hcfg |= HCFG_FSLSSUPP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001036 dwc2_writel(hcfg, hsotg->regs + HCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001037 }
1038
1039 /*
1040 * This bit allows dynamic reloading of the HFIR register during
Masanari Iida0dcde5082013-09-13 23:34:36 +09001041 * runtime. This bit needs to be programmed during initial configuration
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001042 * and its value must not be changed during runtime.
1043 */
1044 if (hsotg->core_params->reload_ctl > 0) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001045 hfir = dwc2_readl(hsotg->regs + HFIR);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001046 hfir |= HFIR_RLDCTRL;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001047 dwc2_writel(hfir, hsotg->regs + HFIR);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001048 }
1049
1050 if (hsotg->core_params->dma_desc_enable > 0) {
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001051 u32 op_mode = hsotg->hw_params.op_mode;
1052 if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
1053 !hsotg->hw_params.dma_desc_enable ||
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001054 op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
1055 op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
1056 op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
1057 dev_err(hsotg->dev,
1058 "Hardware does not support descriptor DMA mode -\n");
1059 dev_err(hsotg->dev,
1060 "falling back to buffer DMA mode.\n");
1061 hsotg->core_params->dma_desc_enable = 0;
1062 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001063 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001064 hcfg |= HCFG_DESCDMA;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001065 dwc2_writel(hcfg, hsotg->regs + HCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001066 }
1067 }
1068
1069 /* Configure data FIFO sizes */
1070 dwc2_config_fifos(hsotg);
1071
1072 /* TODO - check this */
1073 /* Clear Host Set HNP Enable in the OTG Control Register */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001074 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001075 otgctl &= ~GOTGCTL_HSTSETHNPEN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001076 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001077
1078 /* Make sure the FIFOs are flushed */
1079 dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
1080 dwc2_flush_rx_fifo(hsotg);
1081
1082 /* Clear Host Set HNP Enable in the OTG Control Register */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001083 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001084 otgctl &= ~GOTGCTL_HSTSETHNPEN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001085 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001086
1087 if (hsotg->core_params->dma_desc_enable <= 0) {
1088 int num_channels, i;
1089 u32 hcchar;
1090
1091 /* Flush out any leftover queued requests */
1092 num_channels = hsotg->core_params->host_channels;
1093 for (i = 0; i < num_channels; i++) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001094 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001095 hcchar &= ~HCCHAR_CHENA;
1096 hcchar |= HCCHAR_CHDIS;
1097 hcchar &= ~HCCHAR_EPDIR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001098 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001099 }
1100
1101 /* Halt all channels to put them into a known state */
1102 for (i = 0; i < num_channels; i++) {
1103 int count = 0;
1104
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001105 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001106 hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
1107 hcchar &= ~HCCHAR_EPDIR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001108 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001109 dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
1110 __func__, i);
1111 do {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001112 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001113 if (++count > 1000) {
1114 dev_err(hsotg->dev,
1115 "Unable to clear enable on channel %d\n",
1116 i);
1117 break;
1118 }
1119 udelay(1);
1120 } while (hcchar & HCCHAR_CHENA);
1121 }
1122 }
1123
1124 /* Turn on the vbus power */
1125 dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
1126 if (hsotg->op_state == OTG_STATE_A_HOST) {
1127 u32 hprt0 = dwc2_read_hprt0(hsotg);
1128
1129 dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
1130 !!(hprt0 & HPRT0_PWR));
1131 if (!(hprt0 & HPRT0_PWR)) {
1132 hprt0 |= HPRT0_PWR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001133 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001134 }
1135 }
1136
1137 dwc2_enable_host_interrupts(hsotg);
1138}
1139
1140static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
1141 struct dwc2_host_chan *chan)
1142{
1143 u32 hcintmsk = HCINTMSK_CHHLTD;
1144
1145 switch (chan->ep_type) {
1146 case USB_ENDPOINT_XFER_CONTROL:
1147 case USB_ENDPOINT_XFER_BULK:
1148 dev_vdbg(hsotg->dev, "control/bulk\n");
1149 hcintmsk |= HCINTMSK_XFERCOMPL;
1150 hcintmsk |= HCINTMSK_STALL;
1151 hcintmsk |= HCINTMSK_XACTERR;
1152 hcintmsk |= HCINTMSK_DATATGLERR;
1153 if (chan->ep_is_in) {
1154 hcintmsk |= HCINTMSK_BBLERR;
1155 } else {
1156 hcintmsk |= HCINTMSK_NAK;
1157 hcintmsk |= HCINTMSK_NYET;
1158 if (chan->do_ping)
1159 hcintmsk |= HCINTMSK_ACK;
1160 }
1161
1162 if (chan->do_split) {
1163 hcintmsk |= HCINTMSK_NAK;
1164 if (chan->complete_split)
1165 hcintmsk |= HCINTMSK_NYET;
1166 else
1167 hcintmsk |= HCINTMSK_ACK;
1168 }
1169
1170 if (chan->error_state)
1171 hcintmsk |= HCINTMSK_ACK;
1172 break;
1173
1174 case USB_ENDPOINT_XFER_INT:
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001175 if (dbg_perio())
1176 dev_vdbg(hsotg->dev, "intr\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001177 hcintmsk |= HCINTMSK_XFERCOMPL;
1178 hcintmsk |= HCINTMSK_NAK;
1179 hcintmsk |= HCINTMSK_STALL;
1180 hcintmsk |= HCINTMSK_XACTERR;
1181 hcintmsk |= HCINTMSK_DATATGLERR;
1182 hcintmsk |= HCINTMSK_FRMOVRUN;
1183
1184 if (chan->ep_is_in)
1185 hcintmsk |= HCINTMSK_BBLERR;
1186 if (chan->error_state)
1187 hcintmsk |= HCINTMSK_ACK;
1188 if (chan->do_split) {
1189 if (chan->complete_split)
1190 hcintmsk |= HCINTMSK_NYET;
1191 else
1192 hcintmsk |= HCINTMSK_ACK;
1193 }
1194 break;
1195
1196 case USB_ENDPOINT_XFER_ISOC:
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001197 if (dbg_perio())
1198 dev_vdbg(hsotg->dev, "isoc\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001199 hcintmsk |= HCINTMSK_XFERCOMPL;
1200 hcintmsk |= HCINTMSK_FRMOVRUN;
1201 hcintmsk |= HCINTMSK_ACK;
1202
1203 if (chan->ep_is_in) {
1204 hcintmsk |= HCINTMSK_XACTERR;
1205 hcintmsk |= HCINTMSK_BBLERR;
1206 }
1207 break;
1208 default:
1209 dev_err(hsotg->dev, "## Unknown EP type ##\n");
1210 break;
1211 }
1212
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001213 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001214 if (dbg_hc(chan))
1215 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001216}
1217
1218static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
1219 struct dwc2_host_chan *chan)
1220{
1221 u32 hcintmsk = HCINTMSK_CHHLTD;
1222
1223 /*
1224 * For Descriptor DMA mode core halts the channel on AHB error.
1225 * Interrupt is not required.
1226 */
1227 if (hsotg->core_params->dma_desc_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001228 if (dbg_hc(chan))
1229 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001230 hcintmsk |= HCINTMSK_AHBERR;
1231 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001232 if (dbg_hc(chan))
1233 dev_vdbg(hsotg->dev, "desc DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001234 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1235 hcintmsk |= HCINTMSK_XFERCOMPL;
1236 }
1237
1238 if (chan->error_state && !chan->do_split &&
1239 chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001240 if (dbg_hc(chan))
1241 dev_vdbg(hsotg->dev, "setting ACK\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001242 hcintmsk |= HCINTMSK_ACK;
1243 if (chan->ep_is_in) {
1244 hcintmsk |= HCINTMSK_DATATGLERR;
1245 if (chan->ep_type != USB_ENDPOINT_XFER_INT)
1246 hcintmsk |= HCINTMSK_NAK;
1247 }
1248 }
1249
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001250 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001251 if (dbg_hc(chan))
1252 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001253}
1254
1255static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
1256 struct dwc2_host_chan *chan)
1257{
1258 u32 intmsk;
1259
1260 if (hsotg->core_params->dma_enable > 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001261 if (dbg_hc(chan))
1262 dev_vdbg(hsotg->dev, "DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001263 dwc2_hc_enable_dma_ints(hsotg, chan);
1264 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001265 if (dbg_hc(chan))
1266 dev_vdbg(hsotg->dev, "DMA disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001267 dwc2_hc_enable_slave_ints(hsotg, chan);
1268 }
1269
1270 /* Enable the top level host channel interrupt */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001271 intmsk = dwc2_readl(hsotg->regs + HAINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001272 intmsk |= 1 << chan->hc_num;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001273 dwc2_writel(intmsk, hsotg->regs + HAINTMSK);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001274 if (dbg_hc(chan))
1275 dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001276
1277 /* Make sure host channel interrupts are enabled */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001278 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001279 intmsk |= GINTSTS_HCHINT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001280 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001281 if (dbg_hc(chan))
1282 dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001283}
1284
1285/**
1286 * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
1287 * a specific endpoint
1288 *
1289 * @hsotg: Programming view of DWC_otg controller
1290 * @chan: Information needed to initialize the host channel
1291 *
1292 * The HCCHARn register is set up with the characteristics specified in chan.
1293 * Host channel interrupts that may need to be serviced while this transfer is
1294 * in progress are enabled.
1295 */
1296void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1297{
1298 u8 hc_num = chan->hc_num;
1299 u32 hcintmsk;
1300 u32 hcchar;
1301 u32 hcsplt = 0;
1302
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001303 if (dbg_hc(chan))
1304 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001305
1306 /* Clear old interrupt conditions for this host channel */
1307 hcintmsk = 0xffffffff;
1308 hcintmsk &= ~HCINTMSK_RESERVED14_31;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001309 dwc2_writel(hcintmsk, hsotg->regs + HCINT(hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001310
1311 /* Enable channel interrupts required for this transfer */
1312 dwc2_hc_enable_ints(hsotg, chan);
1313
1314 /*
1315 * Program the HCCHARn register with the endpoint characteristics for
1316 * the current transfer
1317 */
1318 hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
1319 hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
1320 if (chan->ep_is_in)
1321 hcchar |= HCCHAR_EPDIR;
1322 if (chan->speed == USB_SPEED_LOW)
1323 hcchar |= HCCHAR_LSPDDEV;
1324 hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
1325 hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001326 dwc2_writel(hcchar, hsotg->regs + HCCHAR(hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001327 if (dbg_hc(chan)) {
1328 dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
1329 hc_num, hcchar);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001330
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001331 dev_vdbg(hsotg->dev, "%s: Channel %d\n",
1332 __func__, hc_num);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001333 dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001334 chan->dev_addr);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001335 dev_vdbg(hsotg->dev, " Ep Num: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001336 chan->ep_num);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001337 dev_vdbg(hsotg->dev, " Is In: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001338 chan->ep_is_in);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001339 dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001340 chan->speed == USB_SPEED_LOW);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001341 dev_vdbg(hsotg->dev, " Ep Type: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001342 chan->ep_type);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001343 dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001344 chan->max_packet);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001345 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001346
1347 /* Program the HCSPLT register for SPLITs */
1348 if (chan->do_split) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001349 if (dbg_hc(chan))
1350 dev_vdbg(hsotg->dev,
1351 "Programming HC %d with split --> %s\n",
1352 hc_num,
1353 chan->complete_split ? "CSPLIT" : "SSPLIT");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001354 if (chan->complete_split)
1355 hcsplt |= HCSPLT_COMPSPLT;
1356 hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
1357 HCSPLT_XACTPOS_MASK;
1358 hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
1359 HCSPLT_HUBADDR_MASK;
1360 hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
1361 HCSPLT_PRTADDR_MASK;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001362 if (dbg_hc(chan)) {
1363 dev_vdbg(hsotg->dev, " comp split %d\n",
1364 chan->complete_split);
1365 dev_vdbg(hsotg->dev, " xact pos %d\n",
1366 chan->xact_pos);
1367 dev_vdbg(hsotg->dev, " hub addr %d\n",
1368 chan->hub_addr);
1369 dev_vdbg(hsotg->dev, " hub port %d\n",
1370 chan->hub_port);
1371 dev_vdbg(hsotg->dev, " is_in %d\n",
1372 chan->ep_is_in);
1373 dev_vdbg(hsotg->dev, " Max Pkt %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001374 chan->max_packet);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001375 dev_vdbg(hsotg->dev, " xferlen %d\n",
1376 chan->xfer_len);
1377 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001378 }
1379
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001380 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001381}
1382
1383/**
1384 * dwc2_hc_halt() - Attempts to halt a host channel
1385 *
1386 * @hsotg: Controller register interface
1387 * @chan: Host channel to halt
1388 * @halt_status: Reason for halting the channel
1389 *
1390 * This function should only be called in Slave mode or to abort a transfer in
1391 * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
1392 * controller halts the channel when the transfer is complete or a condition
1393 * occurs that requires application intervention.
1394 *
1395 * In slave mode, checks for a free request queue entry, then sets the Channel
1396 * Enable and Channel Disable bits of the Host Channel Characteristics
1397 * register of the specified channel to intiate the halt. If there is no free
1398 * request queue entry, sets only the Channel Disable bit of the HCCHARn
1399 * register to flush requests for this channel. In the latter case, sets a
1400 * flag to indicate that the host channel needs to be halted when a request
1401 * queue slot is open.
1402 *
1403 * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
1404 * HCCHARn register. The controller ensures there is space in the request
1405 * queue before submitting the halt request.
1406 *
1407 * Some time may elapse before the core flushes any posted requests for this
1408 * host channel and halts. The Channel Halted interrupt handler completes the
1409 * deactivation of the host channel.
1410 */
1411void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
1412 enum dwc2_halt_status halt_status)
1413{
1414 u32 nptxsts, hptxsts, hcchar;
1415
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001416 if (dbg_hc(chan))
1417 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001418 if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
1419 dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
1420
1421 if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
1422 halt_status == DWC2_HC_XFER_AHB_ERR) {
1423 /*
1424 * Disable all channel interrupts except Ch Halted. The QTD
1425 * and QH state associated with this transfer has been cleared
1426 * (in the case of URB_DEQUEUE), so the channel needs to be
1427 * shut down carefully to prevent crashes.
1428 */
1429 u32 hcintmsk = HCINTMSK_CHHLTD;
1430
1431 dev_vdbg(hsotg->dev, "dequeue/error\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001432 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001433
1434 /*
1435 * Make sure no other interrupts besides halt are currently
1436 * pending. Handling another interrupt could cause a crash due
1437 * to the QTD and QH state.
1438 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001439 dwc2_writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001440
1441 /*
1442 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
1443 * even if the channel was already halted for some other
1444 * reason
1445 */
1446 chan->halt_status = halt_status;
1447
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001448 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001449 if (!(hcchar & HCCHAR_CHENA)) {
1450 /*
1451 * The channel is either already halted or it hasn't
1452 * started yet. In DMA mode, the transfer may halt if
1453 * it finishes normally or a condition occurs that
1454 * requires driver intervention. Don't want to halt
1455 * the channel again. In either Slave or DMA mode,
1456 * it's possible that the transfer has been assigned
1457 * to a channel, but not started yet when an URB is
1458 * dequeued. Don't want to halt a channel that hasn't
1459 * started yet.
1460 */
1461 return;
1462 }
1463 }
1464 if (chan->halt_pending) {
1465 /*
1466 * A halt has already been issued for this channel. This might
1467 * happen when a transfer is aborted by a higher level in
1468 * the stack.
1469 */
1470 dev_vdbg(hsotg->dev,
1471 "*** %s: Channel %d, chan->halt_pending already set ***\n",
1472 __func__, chan->hc_num);
1473 return;
1474 }
1475
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001476 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001477
1478 /* No need to set the bit in DDMA for disabling the channel */
1479 /* TODO check it everywhere channel is disabled */
1480 if (hsotg->core_params->dma_desc_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001481 if (dbg_hc(chan))
1482 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001483 hcchar |= HCCHAR_CHENA;
1484 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001485 if (dbg_hc(chan))
1486 dev_dbg(hsotg->dev, "desc DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001487 }
1488 hcchar |= HCCHAR_CHDIS;
1489
1490 if (hsotg->core_params->dma_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001491 if (dbg_hc(chan))
1492 dev_vdbg(hsotg->dev, "DMA not enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001493 hcchar |= HCCHAR_CHENA;
1494
1495 /* Check for space in the request queue to issue the halt */
1496 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1497 chan->ep_type == USB_ENDPOINT_XFER_BULK) {
1498 dev_vdbg(hsotg->dev, "control/bulk\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001499 nptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001500 if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
1501 dev_vdbg(hsotg->dev, "Disabling channel\n");
1502 hcchar &= ~HCCHAR_CHENA;
1503 }
1504 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001505 if (dbg_perio())
1506 dev_vdbg(hsotg->dev, "isoc/intr\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001507 hptxsts = dwc2_readl(hsotg->regs + HPTXSTS);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001508 if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
1509 hsotg->queuing_high_bandwidth) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001510 if (dbg_perio())
1511 dev_vdbg(hsotg->dev, "Disabling channel\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001512 hcchar &= ~HCCHAR_CHENA;
1513 }
1514 }
1515 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001516 if (dbg_hc(chan))
1517 dev_vdbg(hsotg->dev, "DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001518 }
1519
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001520 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001521 chan->halt_status = halt_status;
1522
1523 if (hcchar & HCCHAR_CHENA) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001524 if (dbg_hc(chan))
1525 dev_vdbg(hsotg->dev, "Channel enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001526 chan->halt_pending = 1;
1527 chan->halt_on_queue = 0;
1528 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001529 if (dbg_hc(chan))
1530 dev_vdbg(hsotg->dev, "Channel disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001531 chan->halt_on_queue = 1;
1532 }
1533
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001534 if (dbg_hc(chan)) {
1535 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1536 chan->hc_num);
1537 dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
1538 hcchar);
1539 dev_vdbg(hsotg->dev, " halt_pending: %d\n",
1540 chan->halt_pending);
1541 dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
1542 chan->halt_on_queue);
1543 dev_vdbg(hsotg->dev, " halt_status: %d\n",
1544 chan->halt_status);
1545 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001546}
1547
1548/**
1549 * dwc2_hc_cleanup() - Clears the transfer state for a host channel
1550 *
1551 * @hsotg: Programming view of DWC_otg controller
1552 * @chan: Identifies the host channel to clean up
1553 *
1554 * This function is normally called after a transfer is done and the host
1555 * channel is being released
1556 */
1557void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1558{
1559 u32 hcintmsk;
1560
1561 chan->xfer_started = 0;
1562
1563 /*
1564 * Clear channel interrupt enables and any unhandled channel interrupt
1565 * conditions
1566 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001567 dwc2_writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001568 hcintmsk = 0xffffffff;
1569 hcintmsk &= ~HCINTMSK_RESERVED14_31;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001570 dwc2_writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001571}
1572
1573/**
1574 * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
1575 * which frame a periodic transfer should occur
1576 *
1577 * @hsotg: Programming view of DWC_otg controller
1578 * @chan: Identifies the host channel to set up and its properties
1579 * @hcchar: Current value of the HCCHAR register for the specified host channel
1580 *
1581 * This function has no effect on non-periodic transfers
1582 */
1583static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
1584 struct dwc2_host_chan *chan, u32 *hcchar)
1585{
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001586 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1587 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001588 /* 1 if _next_ frame is odd, 0 if it's even */
Paul Zimmerman81a58952013-06-24 11:34:23 -07001589 if (!(dwc2_hcd_get_frame_number(hsotg) & 0x1))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001590 *hcchar |= HCCHAR_ODDFRM;
1591 }
1592}
1593
1594static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1595{
1596 /* Set up the initial PID for the transfer */
1597 if (chan->speed == USB_SPEED_HIGH) {
1598 if (chan->ep_is_in) {
1599 if (chan->multi_count == 1)
1600 chan->data_pid_start = DWC2_HC_PID_DATA0;
1601 else if (chan->multi_count == 2)
1602 chan->data_pid_start = DWC2_HC_PID_DATA1;
1603 else
1604 chan->data_pid_start = DWC2_HC_PID_DATA2;
1605 } else {
1606 if (chan->multi_count == 1)
1607 chan->data_pid_start = DWC2_HC_PID_DATA0;
1608 else
1609 chan->data_pid_start = DWC2_HC_PID_MDATA;
1610 }
1611 } else {
1612 chan->data_pid_start = DWC2_HC_PID_DATA0;
1613 }
1614}
1615
1616/**
1617 * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1618 * the Host Channel
1619 *
1620 * @hsotg: Programming view of DWC_otg controller
1621 * @chan: Information needed to initialize the host channel
1622 *
1623 * This function should only be called in Slave mode. For a channel associated
1624 * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1625 * associated with a periodic EP, the periodic Tx FIFO is written.
1626 *
1627 * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1628 * the number of bytes written to the Tx FIFO.
1629 */
1630static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1631 struct dwc2_host_chan *chan)
1632{
1633 u32 i;
1634 u32 remaining_count;
1635 u32 byte_count;
1636 u32 dword_count;
1637 u32 __iomem *data_fifo;
1638 u32 *data_buf = (u32 *)chan->xfer_buf;
1639
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001640 if (dbg_hc(chan))
1641 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001642
1643 data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
1644
1645 remaining_count = chan->xfer_len - chan->xfer_count;
1646 if (remaining_count > chan->max_packet)
1647 byte_count = chan->max_packet;
1648 else
1649 byte_count = remaining_count;
1650
1651 dword_count = (byte_count + 3) / 4;
1652
1653 if (((unsigned long)data_buf & 0x3) == 0) {
1654 /* xfer_buf is DWORD aligned */
1655 for (i = 0; i < dword_count; i++, data_buf++)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001656 dwc2_writel(*data_buf, data_fifo);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001657 } else {
1658 /* xfer_buf is not DWORD aligned */
1659 for (i = 0; i < dword_count; i++, data_buf++) {
1660 u32 data = data_buf[0] | data_buf[1] << 8 |
1661 data_buf[2] << 16 | data_buf[3] << 24;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001662 dwc2_writel(data, data_fifo);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001663 }
1664 }
1665
1666 chan->xfer_count += byte_count;
1667 chan->xfer_buf += byte_count;
1668}
1669
1670/**
1671 * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1672 * channel and starts the transfer
1673 *
1674 * @hsotg: Programming view of DWC_otg controller
1675 * @chan: Information needed to initialize the host channel. The xfer_len value
1676 * may be reduced to accommodate the max widths of the XferSize and
1677 * PktCnt fields in the HCTSIZn register. The multi_count value may be
1678 * changed to reflect the final xfer_len value.
1679 *
1680 * This function may be called in either Slave mode or DMA mode. In Slave mode,
1681 * the caller must ensure that there is sufficient space in the request queue
1682 * and Tx Data FIFO.
1683 *
1684 * For an OUT transfer in Slave mode, it loads a data packet into the
1685 * appropriate FIFO. If necessary, additional data packets are loaded in the
1686 * Host ISR.
1687 *
1688 * For an IN transfer in Slave mode, a data packet is requested. The data
1689 * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1690 * additional data packets are requested in the Host ISR.
1691 *
1692 * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1693 * register along with a packet count of 1 and the channel is enabled. This
1694 * causes a single PING transaction to occur. Other fields in HCTSIZ are
1695 * simply set to 0 since no data transfer occurs in this case.
1696 *
1697 * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1698 * all the information required to perform the subsequent data transfer. In
1699 * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1700 * controller performs the entire PING protocol, then starts the data
1701 * transfer.
1702 */
1703void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1704 struct dwc2_host_chan *chan)
1705{
1706 u32 max_hc_xfer_size = hsotg->core_params->max_transfer_size;
1707 u16 max_hc_pkt_count = hsotg->core_params->max_packet_count;
1708 u32 hcchar;
1709 u32 hctsiz = 0;
1710 u16 num_packets;
1711
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001712 if (dbg_hc(chan))
1713 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001714
1715 if (chan->do_ping) {
1716 if (hsotg->core_params->dma_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001717 if (dbg_hc(chan))
1718 dev_vdbg(hsotg->dev, "ping, no DMA\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001719 dwc2_hc_do_ping(hsotg, chan);
1720 chan->xfer_started = 1;
1721 return;
1722 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001723 if (dbg_hc(chan))
1724 dev_vdbg(hsotg->dev, "ping, DMA\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001725 hctsiz |= TSIZ_DOPNG;
1726 }
1727 }
1728
1729 if (chan->do_split) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001730 if (dbg_hc(chan))
1731 dev_vdbg(hsotg->dev, "split\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001732 num_packets = 1;
1733
1734 if (chan->complete_split && !chan->ep_is_in)
1735 /*
1736 * For CSPLIT OUT Transfer, set the size to 0 so the
1737 * core doesn't expect any data written to the FIFO
1738 */
1739 chan->xfer_len = 0;
1740 else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1741 chan->xfer_len = chan->max_packet;
1742 else if (!chan->ep_is_in && chan->xfer_len > 188)
1743 chan->xfer_len = 188;
1744
1745 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1746 TSIZ_XFERSIZE_MASK;
1747 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001748 if (dbg_hc(chan))
1749 dev_vdbg(hsotg->dev, "no split\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001750 /*
1751 * Ensure that the transfer length and packet count will fit
1752 * in the widths allocated for them in the HCTSIZn register
1753 */
1754 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1755 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1756 /*
1757 * Make sure the transfer size is no larger than one
1758 * (micro)frame's worth of data. (A check was done
1759 * when the periodic transfer was accepted to ensure
1760 * that a (micro)frame's worth of data can be
1761 * programmed into a channel.)
1762 */
1763 u32 max_periodic_len =
1764 chan->multi_count * chan->max_packet;
1765
1766 if (chan->xfer_len > max_periodic_len)
1767 chan->xfer_len = max_periodic_len;
1768 } else if (chan->xfer_len > max_hc_xfer_size) {
1769 /*
1770 * Make sure that xfer_len is a multiple of max packet
1771 * size
1772 */
1773 chan->xfer_len =
1774 max_hc_xfer_size - chan->max_packet + 1;
1775 }
1776
1777 if (chan->xfer_len > 0) {
1778 num_packets = (chan->xfer_len + chan->max_packet - 1) /
1779 chan->max_packet;
1780 if (num_packets > max_hc_pkt_count) {
1781 num_packets = max_hc_pkt_count;
1782 chan->xfer_len = num_packets * chan->max_packet;
1783 }
1784 } else {
1785 /* Need 1 packet for transfer length of 0 */
1786 num_packets = 1;
1787 }
1788
1789 if (chan->ep_is_in)
1790 /*
1791 * Always program an integral # of max packets for IN
1792 * transfers
1793 */
1794 chan->xfer_len = num_packets * chan->max_packet;
1795
1796 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1797 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1798 /*
1799 * Make sure that the multi_count field matches the
1800 * actual transfer length
1801 */
1802 chan->multi_count = num_packets;
1803
1804 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1805 dwc2_set_pid_isoc(chan);
1806
1807 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1808 TSIZ_XFERSIZE_MASK;
1809 }
1810
1811 chan->start_pkt_count = num_packets;
1812 hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1813 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1814 TSIZ_SC_MC_PID_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001815 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001816 if (dbg_hc(chan)) {
1817 dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1818 hctsiz, chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001819
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001820 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1821 chan->hc_num);
1822 dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001823 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1824 TSIZ_XFERSIZE_SHIFT);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001825 dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001826 (hctsiz & TSIZ_PKTCNT_MASK) >>
1827 TSIZ_PKTCNT_SHIFT);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001828 dev_vdbg(hsotg->dev, " Start PID: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001829 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1830 TSIZ_SC_MC_PID_SHIFT);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001831 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001832
1833 if (hsotg->core_params->dma_enable > 0) {
1834 dma_addr_t dma_addr;
1835
1836 if (chan->align_buf) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001837 if (dbg_hc(chan))
1838 dev_vdbg(hsotg->dev, "align_buf\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001839 dma_addr = chan->align_buf;
1840 } else {
1841 dma_addr = chan->xfer_dma;
1842 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001843 dwc2_writel((u32)dma_addr, hsotg->regs + HCDMA(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001844 if (dbg_hc(chan))
1845 dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1846 (unsigned long)dma_addr, chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001847 }
1848
1849 /* Start the split */
1850 if (chan->do_split) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001851 u32 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001852
1853 hcsplt |= HCSPLT_SPLTENA;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001854 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001855 }
1856
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001857 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001858 hcchar &= ~HCCHAR_MULTICNT_MASK;
1859 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1860 HCCHAR_MULTICNT_MASK;
1861 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1862
1863 if (hcchar & HCCHAR_CHDIS)
1864 dev_warn(hsotg->dev,
1865 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1866 __func__, chan->hc_num, hcchar);
1867
1868 /* Set host channel enable after all other setup is complete */
1869 hcchar |= HCCHAR_CHENA;
1870 hcchar &= ~HCCHAR_CHDIS;
1871
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001872 if (dbg_hc(chan))
1873 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001874 (hcchar & HCCHAR_MULTICNT_MASK) >>
1875 HCCHAR_MULTICNT_SHIFT);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001876
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001877 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001878 if (dbg_hc(chan))
1879 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1880 chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001881
1882 chan->xfer_started = 1;
1883 chan->requests++;
1884
1885 if (hsotg->core_params->dma_enable <= 0 &&
1886 !chan->ep_is_in && chan->xfer_len > 0)
1887 /* Load OUT packet into the appropriate Tx FIFO */
1888 dwc2_hc_write_packet(hsotg, chan);
1889}
1890
1891/**
1892 * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1893 * host channel and starts the transfer in Descriptor DMA mode
1894 *
1895 * @hsotg: Programming view of DWC_otg controller
1896 * @chan: Information needed to initialize the host channel
1897 *
1898 * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1899 * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1900 * with micro-frame bitmap.
1901 *
1902 * Initializes HCDMA register with descriptor list address and CTD value then
1903 * starts the transfer via enabling the channel.
1904 */
1905void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1906 struct dwc2_host_chan *chan)
1907{
1908 u32 hcchar;
1909 u32 hc_dma;
1910 u32 hctsiz = 0;
1911
1912 if (chan->do_ping)
1913 hctsiz |= TSIZ_DOPNG;
1914
1915 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1916 dwc2_set_pid_isoc(chan);
1917
1918 /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1919 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1920 TSIZ_SC_MC_PID_MASK;
1921
1922 /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1923 hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1924
1925 /* Non-zero only for high-speed interrupt endpoints */
1926 hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1927
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001928 if (dbg_hc(chan)) {
1929 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1930 chan->hc_num);
1931 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1932 chan->data_pid_start);
1933 dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
1934 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001935
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001936 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001937
1938 hc_dma = (u32)chan->desc_list_addr & HCDMA_DMA_ADDR_MASK;
1939
1940 /* Always start from first descriptor */
1941 hc_dma &= ~HCDMA_CTD_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001942 dwc2_writel(hc_dma, hsotg->regs + HCDMA(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001943 if (dbg_hc(chan))
1944 dev_vdbg(hsotg->dev, "Wrote %08x to HCDMA(%d)\n",
1945 hc_dma, chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001946
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001947 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001948 hcchar &= ~HCCHAR_MULTICNT_MASK;
1949 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1950 HCCHAR_MULTICNT_MASK;
1951
1952 if (hcchar & HCCHAR_CHDIS)
1953 dev_warn(hsotg->dev,
1954 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1955 __func__, chan->hc_num, hcchar);
1956
1957 /* Set host channel enable after all other setup is complete */
1958 hcchar |= HCCHAR_CHENA;
1959 hcchar &= ~HCCHAR_CHDIS;
1960
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001961 if (dbg_hc(chan))
1962 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001963 (hcchar & HCCHAR_MULTICNT_MASK) >>
1964 HCCHAR_MULTICNT_SHIFT);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001965
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001966 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001967 if (dbg_hc(chan))
1968 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1969 chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001970
1971 chan->xfer_started = 1;
1972 chan->requests++;
1973}
1974
1975/**
1976 * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
1977 * a previous call to dwc2_hc_start_transfer()
1978 *
1979 * @hsotg: Programming view of DWC_otg controller
1980 * @chan: Information needed to initialize the host channel
1981 *
1982 * The caller must ensure there is sufficient space in the request queue and Tx
1983 * Data FIFO. This function should only be called in Slave mode. In DMA mode,
1984 * the controller acts autonomously to complete transfers programmed to a host
1985 * channel.
1986 *
1987 * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
1988 * if there is any data remaining to be queued. For an IN transfer, another
1989 * data packet is always requested. For the SETUP phase of a control transfer,
1990 * this function does nothing.
1991 *
1992 * Return: 1 if a new request is queued, 0 if no more requests are required
1993 * for this transfer
1994 */
1995int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
1996 struct dwc2_host_chan *chan)
1997{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001998 if (dbg_hc(chan))
1999 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
2000 chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002001
2002 if (chan->do_split)
2003 /* SPLITs always queue just once per channel */
2004 return 0;
2005
2006 if (chan->data_pid_start == DWC2_HC_PID_SETUP)
2007 /* SETUPs are queued only once since they can't be NAK'd */
2008 return 0;
2009
2010 if (chan->ep_is_in) {
2011 /*
2012 * Always queue another request for other IN transfers. If
2013 * back-to-back INs are issued and NAKs are received for both,
2014 * the driver may still be processing the first NAK when the
2015 * second NAK is received. When the interrupt handler clears
2016 * the NAK interrupt for the first NAK, the second NAK will
2017 * not be seen. So we can't depend on the NAK interrupt
2018 * handler to requeue a NAK'd request. Instead, IN requests
2019 * are issued each time this function is called. When the
2020 * transfer completes, the extra requests for the channel will
2021 * be flushed.
2022 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002023 u32 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002024
2025 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
2026 hcchar |= HCCHAR_CHENA;
2027 hcchar &= ~HCCHAR_CHDIS;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002028 if (dbg_hc(chan))
2029 dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
2030 hcchar);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002031 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002032 chan->requests++;
2033 return 1;
2034 }
2035
2036 /* OUT transfers */
2037
2038 if (chan->xfer_count < chan->xfer_len) {
2039 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2040 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002041 u32 hcchar = dwc2_readl(hsotg->regs +
2042 HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002043
2044 dwc2_hc_set_even_odd_frame(hsotg, chan,
2045 &hcchar);
2046 }
2047
2048 /* Load OUT packet into the appropriate Tx FIFO */
2049 dwc2_hc_write_packet(hsotg, chan);
2050 chan->requests++;
2051 return 1;
2052 }
2053
2054 return 0;
2055}
2056
2057/**
2058 * dwc2_hc_do_ping() - Starts a PING transfer
2059 *
2060 * @hsotg: Programming view of DWC_otg controller
2061 * @chan: Information needed to initialize the host channel
2062 *
2063 * This function should only be called in Slave mode. The Do Ping bit is set in
2064 * the HCTSIZ register, then the channel is enabled.
2065 */
2066void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
2067{
2068 u32 hcchar;
2069 u32 hctsiz;
2070
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002071 if (dbg_hc(chan))
2072 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
2073 chan->hc_num);
2074
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002075
2076 hctsiz = TSIZ_DOPNG;
2077 hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002078 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002079
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002080 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002081 hcchar |= HCCHAR_CHENA;
2082 hcchar &= ~HCCHAR_CHDIS;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002083 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002084}
2085
2086/**
2087 * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
2088 * the HFIR register according to PHY type and speed
2089 *
2090 * @hsotg: Programming view of DWC_otg controller
2091 *
2092 * NOTE: The caller can modify the value of the HFIR register only after the
2093 * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
2094 * has been set
2095 */
2096u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
2097{
2098 u32 usbcfg;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002099 u32 hprt0;
2100 int clock = 60; /* default value */
2101
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002102 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2103 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002104
2105 if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
2106 !(usbcfg & GUSBCFG_PHYIF16))
2107 clock = 60;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002108 if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002109 GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
2110 clock = 48;
2111 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2112 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
2113 clock = 30;
2114 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2115 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
2116 clock = 60;
2117 if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2118 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
2119 clock = 48;
2120 if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002121 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002122 clock = 48;
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02002123 if ((usbcfg & GUSBCFG_PHYSEL) &&
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002124 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002125 clock = 48;
2126
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02002127 if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002128 /* High speed case */
2129 return 125 * clock;
2130 else
2131 /* FS/LS case */
2132 return 1000 * clock;
2133}
2134
2135/**
2136 * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
2137 * buffer
2138 *
2139 * @core_if: Programming view of DWC_otg controller
2140 * @dest: Destination buffer for the packet
2141 * @bytes: Number of bytes to copy to the destination
2142 */
2143void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
2144{
2145 u32 __iomem *fifo = hsotg->regs + HCFIFO(0);
2146 u32 *data_buf = (u32 *)dest;
2147 int word_count = (bytes + 3) / 4;
2148 int i;
2149
2150 /*
2151 * Todo: Account for the case where dest is not dword aligned. This
2152 * requires reading data from the FIFO into a u32 temp buffer, then
2153 * moving it into the data buffer.
2154 */
2155
2156 dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
2157
2158 for (i = 0; i < word_count; i++, data_buf++)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002159 *data_buf = dwc2_readl(fifo);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002160}
2161
2162/**
2163 * dwc2_dump_host_registers() - Prints the host registers
2164 *
2165 * @hsotg: Programming view of DWC_otg controller
2166 *
2167 * NOTE: This function will be removed once the peripheral controller code
2168 * is integrated and the driver is stable
2169 */
2170void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
2171{
2172#ifdef DEBUG
2173 u32 __iomem *addr;
2174 int i;
2175
2176 dev_dbg(hsotg->dev, "Host Global Registers\n");
2177 addr = hsotg->regs + HCFG;
2178 dev_dbg(hsotg->dev, "HCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002179 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002180 addr = hsotg->regs + HFIR;
2181 dev_dbg(hsotg->dev, "HFIR @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002182 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002183 addr = hsotg->regs + HFNUM;
2184 dev_dbg(hsotg->dev, "HFNUM @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002185 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002186 addr = hsotg->regs + HPTXSTS;
2187 dev_dbg(hsotg->dev, "HPTXSTS @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002188 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002189 addr = hsotg->regs + HAINT;
2190 dev_dbg(hsotg->dev, "HAINT @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002191 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002192 addr = hsotg->regs + HAINTMSK;
2193 dev_dbg(hsotg->dev, "HAINTMSK @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002194 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002195 if (hsotg->core_params->dma_desc_enable > 0) {
2196 addr = hsotg->regs + HFLBADDR;
2197 dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002198 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002199 }
2200
2201 addr = hsotg->regs + HPRT0;
2202 dev_dbg(hsotg->dev, "HPRT0 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002203 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002204
2205 for (i = 0; i < hsotg->core_params->host_channels; i++) {
2206 dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
2207 addr = hsotg->regs + HCCHAR(i);
2208 dev_dbg(hsotg->dev, "HCCHAR @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002209 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002210 addr = hsotg->regs + HCSPLT(i);
2211 dev_dbg(hsotg->dev, "HCSPLT @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002212 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002213 addr = hsotg->regs + HCINT(i);
2214 dev_dbg(hsotg->dev, "HCINT @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002215 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002216 addr = hsotg->regs + HCINTMSK(i);
2217 dev_dbg(hsotg->dev, "HCINTMSK @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002218 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002219 addr = hsotg->regs + HCTSIZ(i);
2220 dev_dbg(hsotg->dev, "HCTSIZ @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002221 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002222 addr = hsotg->regs + HCDMA(i);
2223 dev_dbg(hsotg->dev, "HCDMA @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002224 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002225 if (hsotg->core_params->dma_desc_enable > 0) {
2226 addr = hsotg->regs + HCDMAB(i);
2227 dev_dbg(hsotg->dev, "HCDMAB @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002228 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002229 }
2230 }
2231#endif
2232}
2233
2234/**
2235 * dwc2_dump_global_registers() - Prints the core global registers
2236 *
2237 * @hsotg: Programming view of DWC_otg controller
2238 *
2239 * NOTE: This function will be removed once the peripheral controller code
2240 * is integrated and the driver is stable
2241 */
2242void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
2243{
2244#ifdef DEBUG
2245 u32 __iomem *addr;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002246
2247 dev_dbg(hsotg->dev, "Core Global Registers\n");
2248 addr = hsotg->regs + GOTGCTL;
2249 dev_dbg(hsotg->dev, "GOTGCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002250 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002251 addr = hsotg->regs + GOTGINT;
2252 dev_dbg(hsotg->dev, "GOTGINT @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002253 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002254 addr = hsotg->regs + GAHBCFG;
2255 dev_dbg(hsotg->dev, "GAHBCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002256 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002257 addr = hsotg->regs + GUSBCFG;
2258 dev_dbg(hsotg->dev, "GUSBCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002259 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002260 addr = hsotg->regs + GRSTCTL;
2261 dev_dbg(hsotg->dev, "GRSTCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002262 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002263 addr = hsotg->regs + GINTSTS;
2264 dev_dbg(hsotg->dev, "GINTSTS @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002265 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002266 addr = hsotg->regs + GINTMSK;
2267 dev_dbg(hsotg->dev, "GINTMSK @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002268 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002269 addr = hsotg->regs + GRXSTSR;
2270 dev_dbg(hsotg->dev, "GRXSTSR @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002271 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002272 addr = hsotg->regs + GRXFSIZ;
2273 dev_dbg(hsotg->dev, "GRXFSIZ @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002274 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002275 addr = hsotg->regs + GNPTXFSIZ;
2276 dev_dbg(hsotg->dev, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002277 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002278 addr = hsotg->regs + GNPTXSTS;
2279 dev_dbg(hsotg->dev, "GNPTXSTS @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002280 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002281 addr = hsotg->regs + GI2CCTL;
2282 dev_dbg(hsotg->dev, "GI2CCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002283 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002284 addr = hsotg->regs + GPVNDCTL;
2285 dev_dbg(hsotg->dev, "GPVNDCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002286 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002287 addr = hsotg->regs + GGPIO;
2288 dev_dbg(hsotg->dev, "GGPIO @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002289 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002290 addr = hsotg->regs + GUID;
2291 dev_dbg(hsotg->dev, "GUID @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002292 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002293 addr = hsotg->regs + GSNPSID;
2294 dev_dbg(hsotg->dev, "GSNPSID @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002295 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002296 addr = hsotg->regs + GHWCFG1;
2297 dev_dbg(hsotg->dev, "GHWCFG1 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002298 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002299 addr = hsotg->regs + GHWCFG2;
2300 dev_dbg(hsotg->dev, "GHWCFG2 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002301 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002302 addr = hsotg->regs + GHWCFG3;
2303 dev_dbg(hsotg->dev, "GHWCFG3 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002304 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002305 addr = hsotg->regs + GHWCFG4;
2306 dev_dbg(hsotg->dev, "GHWCFG4 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002307 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002308 addr = hsotg->regs + GLPMCFG;
2309 dev_dbg(hsotg->dev, "GLPMCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002310 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002311 addr = hsotg->regs + GPWRDN;
2312 dev_dbg(hsotg->dev, "GPWRDN @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002313 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002314 addr = hsotg->regs + GDFIFOCFG;
2315 dev_dbg(hsotg->dev, "GDFIFOCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002316 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002317 addr = hsotg->regs + HPTXFSIZ;
2318 dev_dbg(hsotg->dev, "HPTXFSIZ @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002319 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002320
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002321 addr = hsotg->regs + PCGCTL;
2322 dev_dbg(hsotg->dev, "PCGCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002323 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002324#endif
2325}
2326
2327/**
2328 * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
2329 *
2330 * @hsotg: Programming view of DWC_otg controller
2331 * @num: Tx FIFO to flush
2332 */
2333void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
2334{
2335 u32 greset;
2336 int count = 0;
2337
2338 dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
2339
2340 greset = GRSTCTL_TXFFLSH;
2341 greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002342 dwc2_writel(greset, hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002343
2344 do {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002345 greset = dwc2_readl(hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002346 if (++count > 10000) {
2347 dev_warn(hsotg->dev,
2348 "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
2349 __func__, greset,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002350 dwc2_readl(hsotg->regs + GNPTXSTS));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002351 break;
2352 }
2353 udelay(1);
2354 } while (greset & GRSTCTL_TXFFLSH);
2355
2356 /* Wait for at least 3 PHY Clocks */
2357 udelay(1);
2358}
2359
2360/**
2361 * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
2362 *
2363 * @hsotg: Programming view of DWC_otg controller
2364 */
2365void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
2366{
2367 u32 greset;
2368 int count = 0;
2369
2370 dev_vdbg(hsotg->dev, "%s()\n", __func__);
2371
2372 greset = GRSTCTL_RXFFLSH;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002373 dwc2_writel(greset, hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002374
2375 do {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002376 greset = dwc2_readl(hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002377 if (++count > 10000) {
2378 dev_warn(hsotg->dev, "%s() HANG! GRSTCTL=%0x\n",
2379 __func__, greset);
2380 break;
2381 }
2382 udelay(1);
2383 } while (greset & GRSTCTL_RXFFLSH);
2384
2385 /* Wait for at least 3 PHY Clocks */
2386 udelay(1);
2387}
2388
Paul Zimmerman498f0662013-11-22 16:43:47 -08002389#define DWC2_OUT_OF_BOUNDS(a, b, c) ((a) < (b) || (a) > (c))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002390
2391/* Parameter access functions */
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002392void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002393{
2394 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002395
2396 switch (val) {
2397 case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002398 if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002399 valid = 0;
2400 break;
2401 case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002402 switch (hsotg->hw_params.op_mode) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002403 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
2404 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
2405 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
2406 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
2407 break;
2408 default:
2409 valid = 0;
2410 break;
2411 }
2412 break;
2413 case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
2414 /* always valid */
2415 break;
2416 default:
2417 valid = 0;
2418 break;
2419 }
2420
2421 if (!valid) {
2422 if (val >= 0)
2423 dev_err(hsotg->dev,
2424 "%d invalid for otg_cap parameter. Check HW configuration.\n",
2425 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002426 switch (hsotg->hw_params.op_mode) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002427 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
2428 val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
2429 break;
2430 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
2431 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
2432 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
2433 val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
2434 break;
2435 default:
2436 val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
2437 break;
2438 }
2439 dev_dbg(hsotg->dev, "Setting otg_cap to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002440 }
2441
2442 hsotg->core_params->otg_cap = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002443}
2444
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002445void dwc2_set_param_dma_enable(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002446{
2447 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002448
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002449 if (val > 0 && hsotg->hw_params.arch == GHWCFG2_SLAVE_ONLY_ARCH)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002450 valid = 0;
2451 if (val < 0)
2452 valid = 0;
2453
2454 if (!valid) {
2455 if (val >= 0)
2456 dev_err(hsotg->dev,
2457 "%d invalid for dma_enable parameter. Check HW configuration.\n",
2458 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002459 val = hsotg->hw_params.arch != GHWCFG2_SLAVE_ONLY_ARCH;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002460 dev_dbg(hsotg->dev, "Setting dma_enable to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002461 }
2462
2463 hsotg->core_params->dma_enable = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002464}
2465
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002466void dwc2_set_param_dma_desc_enable(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002467{
2468 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002469
2470 if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002471 !hsotg->hw_params.dma_desc_enable))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002472 valid = 0;
2473 if (val < 0)
2474 valid = 0;
2475
2476 if (!valid) {
2477 if (val >= 0)
2478 dev_err(hsotg->dev,
2479 "%d invalid for dma_desc_enable parameter. Check HW configuration.\n",
2480 val);
2481 val = (hsotg->core_params->dma_enable > 0 &&
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002482 hsotg->hw_params.dma_desc_enable);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002483 dev_dbg(hsotg->dev, "Setting dma_desc_enable to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002484 }
2485
2486 hsotg->core_params->dma_desc_enable = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002487}
2488
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002489void dwc2_set_param_host_support_fs_ls_low_power(struct dwc2_hsotg *hsotg,
2490 int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002491{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002492 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002493 if (val >= 0) {
2494 dev_err(hsotg->dev,
2495 "Wrong value for host_support_fs_low_power\n");
2496 dev_err(hsotg->dev,
2497 "host_support_fs_low_power must be 0 or 1\n");
2498 }
2499 val = 0;
2500 dev_dbg(hsotg->dev,
2501 "Setting host_support_fs_low_power to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002502 }
2503
2504 hsotg->core_params->host_support_fs_ls_low_power = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002505}
2506
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002507void dwc2_set_param_enable_dynamic_fifo(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002508{
2509 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002510
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002511 if (val > 0 && !hsotg->hw_params.enable_dynamic_fifo)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002512 valid = 0;
2513 if (val < 0)
2514 valid = 0;
2515
2516 if (!valid) {
2517 if (val >= 0)
2518 dev_err(hsotg->dev,
2519 "%d invalid for enable_dynamic_fifo parameter. Check HW configuration.\n",
2520 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002521 val = hsotg->hw_params.enable_dynamic_fifo;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002522 dev_dbg(hsotg->dev, "Setting enable_dynamic_fifo to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002523 }
2524
2525 hsotg->core_params->enable_dynamic_fifo = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002526}
2527
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002528void dwc2_set_param_host_rx_fifo_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002529{
2530 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002531
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002532 if (val < 16 || val > hsotg->hw_params.host_rx_fifo_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002533 valid = 0;
2534
2535 if (!valid) {
2536 if (val >= 0)
2537 dev_err(hsotg->dev,
2538 "%d invalid for host_rx_fifo_size. Check HW configuration.\n",
2539 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002540 val = hsotg->hw_params.host_rx_fifo_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002541 dev_dbg(hsotg->dev, "Setting host_rx_fifo_size to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002542 }
2543
2544 hsotg->core_params->host_rx_fifo_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002545}
2546
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002547void dwc2_set_param_host_nperio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002548{
2549 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002550
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002551 if (val < 16 || val > hsotg->hw_params.host_nperio_tx_fifo_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002552 valid = 0;
2553
2554 if (!valid) {
2555 if (val >= 0)
2556 dev_err(hsotg->dev,
2557 "%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
2558 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002559 val = hsotg->hw_params.host_nperio_tx_fifo_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002560 dev_dbg(hsotg->dev, "Setting host_nperio_tx_fifo_size to %d\n",
2561 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002562 }
2563
2564 hsotg->core_params->host_nperio_tx_fifo_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002565}
2566
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002567void dwc2_set_param_host_perio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002568{
2569 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002570
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002571 if (val < 16 || val > hsotg->hw_params.host_perio_tx_fifo_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002572 valid = 0;
2573
2574 if (!valid) {
2575 if (val >= 0)
2576 dev_err(hsotg->dev,
2577 "%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
2578 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002579 val = hsotg->hw_params.host_perio_tx_fifo_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002580 dev_dbg(hsotg->dev, "Setting host_perio_tx_fifo_size to %d\n",
2581 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002582 }
2583
2584 hsotg->core_params->host_perio_tx_fifo_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002585}
2586
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002587void dwc2_set_param_max_transfer_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002588{
2589 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002590
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002591 if (val < 2047 || val > hsotg->hw_params.max_transfer_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002592 valid = 0;
2593
2594 if (!valid) {
2595 if (val >= 0)
2596 dev_err(hsotg->dev,
2597 "%d invalid for max_transfer_size. Check HW configuration.\n",
2598 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002599 val = hsotg->hw_params.max_transfer_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002600 dev_dbg(hsotg->dev, "Setting max_transfer_size to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002601 }
2602
2603 hsotg->core_params->max_transfer_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002604}
2605
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002606void dwc2_set_param_max_packet_count(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002607{
2608 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002609
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002610 if (val < 15 || val > hsotg->hw_params.max_packet_count)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002611 valid = 0;
2612
2613 if (!valid) {
2614 if (val >= 0)
2615 dev_err(hsotg->dev,
2616 "%d invalid for max_packet_count. Check HW configuration.\n",
2617 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002618 val = hsotg->hw_params.max_packet_count;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002619 dev_dbg(hsotg->dev, "Setting max_packet_count to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002620 }
2621
2622 hsotg->core_params->max_packet_count = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002623}
2624
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002625void dwc2_set_param_host_channels(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002626{
2627 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002628
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002629 if (val < 1 || val > hsotg->hw_params.host_channels)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002630 valid = 0;
2631
2632 if (!valid) {
2633 if (val >= 0)
2634 dev_err(hsotg->dev,
2635 "%d invalid for host_channels. Check HW configuration.\n",
2636 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002637 val = hsotg->hw_params.host_channels;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002638 dev_dbg(hsotg->dev, "Setting host_channels to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002639 }
2640
2641 hsotg->core_params->host_channels = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002642}
2643
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002644void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002645{
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002646 int valid = 0;
Luis Ortega Perez de Villar0464a3d2013-09-25 13:10:50 +02002647 u32 hs_phy_type, fs_phy_type;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002648
Paul Zimmerman498f0662013-11-22 16:43:47 -08002649 if (DWC2_OUT_OF_BOUNDS(val, DWC2_PHY_TYPE_PARAM_FS,
2650 DWC2_PHY_TYPE_PARAM_ULPI)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002651 if (val >= 0) {
2652 dev_err(hsotg->dev, "Wrong value for phy_type\n");
2653 dev_err(hsotg->dev, "phy_type must be 0, 1 or 2\n");
2654 }
2655
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002656 valid = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002657 }
2658
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002659 hs_phy_type = hsotg->hw_params.hs_phy_type;
2660 fs_phy_type = hsotg->hw_params.fs_phy_type;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002661 if (val == DWC2_PHY_TYPE_PARAM_UTMI &&
2662 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
2663 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
2664 valid = 1;
2665 else if (val == DWC2_PHY_TYPE_PARAM_ULPI &&
2666 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI ||
2667 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
2668 valid = 1;
2669 else if (val == DWC2_PHY_TYPE_PARAM_FS &&
2670 fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
2671 valid = 1;
2672
2673 if (!valid) {
2674 if (val >= 0)
2675 dev_err(hsotg->dev,
2676 "%d invalid for phy_type. Check HW configuration.\n",
2677 val);
Matthijs Kooijman929aea02013-04-29 19:36:48 +00002678 val = DWC2_PHY_TYPE_PARAM_FS;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002679 if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
2680 if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
2681 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
2682 val = DWC2_PHY_TYPE_PARAM_UTMI;
2683 else
2684 val = DWC2_PHY_TYPE_PARAM_ULPI;
2685 }
2686 dev_dbg(hsotg->dev, "Setting phy_type to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002687 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002688
2689 hsotg->core_params->phy_type = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002690}
2691
2692static int dwc2_get_param_phy_type(struct dwc2_hsotg *hsotg)
2693{
2694 return hsotg->core_params->phy_type;
2695}
2696
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002697void dwc2_set_param_speed(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002698{
2699 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002700
Paul Zimmerman498f0662013-11-22 16:43:47 -08002701 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002702 if (val >= 0) {
2703 dev_err(hsotg->dev, "Wrong value for speed parameter\n");
2704 dev_err(hsotg->dev, "max_speed parameter must be 0 or 1\n");
2705 }
2706 valid = 0;
2707 }
2708
Matthijs Kooijman929aea02013-04-29 19:36:48 +00002709 if (val == DWC2_SPEED_PARAM_HIGH &&
2710 dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002711 valid = 0;
2712
2713 if (!valid) {
2714 if (val >= 0)
2715 dev_err(hsotg->dev,
2716 "%d invalid for speed parameter. Check HW configuration.\n",
2717 val);
2718 val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS ?
Matthijs Kooijman929aea02013-04-29 19:36:48 +00002719 DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002720 dev_dbg(hsotg->dev, "Setting speed to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002721 }
2722
2723 hsotg->core_params->speed = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002724}
2725
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002726void dwc2_set_param_host_ls_low_power_phy_clk(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002727{
2728 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002729
Paul Zimmerman498f0662013-11-22 16:43:47 -08002730 if (DWC2_OUT_OF_BOUNDS(val, DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ,
2731 DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002732 if (val >= 0) {
2733 dev_err(hsotg->dev,
2734 "Wrong value for host_ls_low_power_phy_clk parameter\n");
2735 dev_err(hsotg->dev,
2736 "host_ls_low_power_phy_clk must be 0 or 1\n");
2737 }
2738 valid = 0;
2739 }
2740
2741 if (val == DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ &&
2742 dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
2743 valid = 0;
2744
2745 if (!valid) {
2746 if (val >= 0)
2747 dev_err(hsotg->dev,
2748 "%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
2749 val);
2750 val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS
2751 ? DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ
2752 : DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
2753 dev_dbg(hsotg->dev, "Setting host_ls_low_power_phy_clk to %d\n",
2754 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002755 }
2756
2757 hsotg->core_params->host_ls_low_power_phy_clk = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002758}
2759
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002760void dwc2_set_param_phy_ulpi_ddr(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002761{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002762 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002763 if (val >= 0) {
2764 dev_err(hsotg->dev, "Wrong value for phy_ulpi_ddr\n");
2765 dev_err(hsotg->dev, "phy_upli_ddr must be 0 or 1\n");
2766 }
2767 val = 0;
2768 dev_dbg(hsotg->dev, "Setting phy_upli_ddr to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002769 }
2770
2771 hsotg->core_params->phy_ulpi_ddr = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002772}
2773
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002774void dwc2_set_param_phy_ulpi_ext_vbus(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002775{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002776 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002777 if (val >= 0) {
2778 dev_err(hsotg->dev,
2779 "Wrong value for phy_ulpi_ext_vbus\n");
2780 dev_err(hsotg->dev,
2781 "phy_ulpi_ext_vbus must be 0 or 1\n");
2782 }
2783 val = 0;
2784 dev_dbg(hsotg->dev, "Setting phy_ulpi_ext_vbus to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002785 }
2786
2787 hsotg->core_params->phy_ulpi_ext_vbus = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002788}
2789
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002790void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002791{
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02002792 int valid = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002793
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02002794 switch (hsotg->hw_params.utmi_phy_data_width) {
2795 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
2796 valid = (val == 8);
2797 break;
2798 case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
2799 valid = (val == 16);
2800 break;
2801 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
2802 valid = (val == 8 || val == 16);
2803 break;
2804 }
2805
2806 if (!valid) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002807 if (val >= 0) {
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02002808 dev_err(hsotg->dev,
2809 "%d invalid for phy_utmi_width. Check HW configuration.\n",
2810 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002811 }
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02002812 val = (hsotg->hw_params.utmi_phy_data_width ==
2813 GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002814 dev_dbg(hsotg->dev, "Setting phy_utmi_width to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002815 }
2816
2817 hsotg->core_params->phy_utmi_width = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002818}
2819
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002820void dwc2_set_param_ulpi_fs_ls(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002821{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002822 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002823 if (val >= 0) {
2824 dev_err(hsotg->dev, "Wrong value for ulpi_fs_ls\n");
2825 dev_err(hsotg->dev, "ulpi_fs_ls must be 0 or 1\n");
2826 }
2827 val = 0;
2828 dev_dbg(hsotg->dev, "Setting ulpi_fs_ls to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002829 }
2830
2831 hsotg->core_params->ulpi_fs_ls = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002832}
2833
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002834void dwc2_set_param_ts_dline(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002835{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002836 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002837 if (val >= 0) {
2838 dev_err(hsotg->dev, "Wrong value for ts_dline\n");
2839 dev_err(hsotg->dev, "ts_dline must be 0 or 1\n");
2840 }
2841 val = 0;
2842 dev_dbg(hsotg->dev, "Setting ts_dline to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002843 }
2844
2845 hsotg->core_params->ts_dline = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002846}
2847
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002848void dwc2_set_param_i2c_enable(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002849{
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002850 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002851
Paul Zimmerman498f0662013-11-22 16:43:47 -08002852 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002853 if (val >= 0) {
2854 dev_err(hsotg->dev, "Wrong value for i2c_enable\n");
2855 dev_err(hsotg->dev, "i2c_enable must be 0 or 1\n");
2856 }
2857
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002858 valid = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002859 }
2860
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002861 if (val == 1 && !(hsotg->hw_params.i2c_enable))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002862 valid = 0;
2863
2864 if (!valid) {
2865 if (val >= 0)
2866 dev_err(hsotg->dev,
2867 "%d invalid for i2c_enable. Check HW configuration.\n",
2868 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002869 val = hsotg->hw_params.i2c_enable;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002870 dev_dbg(hsotg->dev, "Setting i2c_enable to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002871 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002872
2873 hsotg->core_params->i2c_enable = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002874}
2875
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002876void dwc2_set_param_en_multiple_tx_fifo(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002877{
2878 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002879
Paul Zimmerman498f0662013-11-22 16:43:47 -08002880 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002881 if (val >= 0) {
2882 dev_err(hsotg->dev,
2883 "Wrong value for en_multiple_tx_fifo,\n");
2884 dev_err(hsotg->dev,
2885 "en_multiple_tx_fifo must be 0 or 1\n");
2886 }
2887 valid = 0;
2888 }
2889
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002890 if (val == 1 && !hsotg->hw_params.en_multiple_tx_fifo)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002891 valid = 0;
2892
2893 if (!valid) {
2894 if (val >= 0)
2895 dev_err(hsotg->dev,
2896 "%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
2897 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002898 val = hsotg->hw_params.en_multiple_tx_fifo;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002899 dev_dbg(hsotg->dev, "Setting en_multiple_tx_fifo to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002900 }
2901
2902 hsotg->core_params->en_multiple_tx_fifo = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002903}
2904
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002905void dwc2_set_param_reload_ctl(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002906{
2907 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002908
Paul Zimmerman498f0662013-11-22 16:43:47 -08002909 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002910 if (val >= 0) {
2911 dev_err(hsotg->dev,
2912 "'%d' invalid for parameter reload_ctl\n", val);
2913 dev_err(hsotg->dev, "reload_ctl must be 0 or 1\n");
2914 }
2915 valid = 0;
2916 }
2917
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002918 if (val == 1 && hsotg->hw_params.snpsid < DWC2_CORE_REV_2_92a)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002919 valid = 0;
2920
2921 if (!valid) {
2922 if (val >= 0)
2923 dev_err(hsotg->dev,
2924 "%d invalid for parameter reload_ctl. Check HW configuration.\n",
2925 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002926 val = hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_92a;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002927 dev_dbg(hsotg->dev, "Setting reload_ctl to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002928 }
2929
2930 hsotg->core_params->reload_ctl = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002931}
2932
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002933void dwc2_set_param_ahbcfg(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002934{
Paul Zimmerman4d3190e2013-07-16 12:22:12 -07002935 if (val != -1)
2936 hsotg->core_params->ahbcfg = val;
2937 else
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02002938 hsotg->core_params->ahbcfg = GAHBCFG_HBSTLEN_INCR4 <<
Luis Ortega Perez de Villar0464a3d2013-09-25 13:10:50 +02002939 GAHBCFG_HBSTLEN_SHIFT;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002940}
2941
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002942void dwc2_set_param_otg_ver(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002943{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002944 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002945 if (val >= 0) {
2946 dev_err(hsotg->dev,
2947 "'%d' invalid for parameter otg_ver\n", val);
2948 dev_err(hsotg->dev,
2949 "otg_ver must be 0 (for OTG 1.3 support) or 1 (for OTG 2.0 support)\n");
2950 }
2951 val = 0;
2952 dev_dbg(hsotg->dev, "Setting otg_ver to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002953 }
2954
2955 hsotg->core_params->otg_ver = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002956}
2957
Wei Yongjun49cf10c2013-11-28 10:27:59 +08002958static void dwc2_set_param_uframe_sched(struct dwc2_hsotg *hsotg, int val)
Paul Zimmermane8576e62013-11-25 13:42:47 -08002959{
2960 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2961 if (val >= 0) {
2962 dev_err(hsotg->dev,
2963 "'%d' invalid for parameter uframe_sched\n",
2964 val);
2965 dev_err(hsotg->dev, "uframe_sched must be 0 or 1\n");
2966 }
2967 val = 1;
2968 dev_dbg(hsotg->dev, "Setting uframe_sched to %d\n", val);
2969 }
2970
2971 hsotg->core_params->uframe_sched = val;
2972}
2973
Gregory Herreroa6d249d2015-04-29 22:09:04 +02002974static void dwc2_set_param_external_id_pin_ctl(struct dwc2_hsotg *hsotg,
2975 int val)
2976{
2977 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2978 if (val >= 0) {
2979 dev_err(hsotg->dev,
2980 "'%d' invalid for parameter external_id_pin_ctl\n",
2981 val);
2982 dev_err(hsotg->dev, "external_id_pin_ctl must be 0 or 1\n");
2983 }
2984 val = 0;
2985 dev_dbg(hsotg->dev, "Setting external_id_pin_ctl to %d\n", val);
2986 }
2987
2988 hsotg->core_params->external_id_pin_ctl = val;
2989}
2990
Gregory Herrero285046a2015-04-29 22:09:19 +02002991static void dwc2_set_param_hibernation(struct dwc2_hsotg *hsotg,
2992 int val)
2993{
2994 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2995 if (val >= 0) {
2996 dev_err(hsotg->dev,
2997 "'%d' invalid for parameter hibernation\n",
2998 val);
2999 dev_err(hsotg->dev, "hibernation must be 0 or 1\n");
3000 }
3001 val = 0;
3002 dev_dbg(hsotg->dev, "Setting hibernation to %d\n", val);
3003 }
3004
3005 hsotg->core_params->hibernation = val;
3006}
3007
Paul Zimmermane8576e62013-11-25 13:42:47 -08003008/*
3009 * This function is called during module intialization to pass module parameters
3010 * for the DWC_otg core.
3011 */
3012void dwc2_set_parameters(struct dwc2_hsotg *hsotg,
3013 const struct dwc2_core_params *params)
3014{
3015 dev_dbg(hsotg->dev, "%s()\n", __func__);
3016
3017 dwc2_set_param_otg_cap(hsotg, params->otg_cap);
3018 dwc2_set_param_dma_enable(hsotg, params->dma_enable);
3019 dwc2_set_param_dma_desc_enable(hsotg, params->dma_desc_enable);
3020 dwc2_set_param_host_support_fs_ls_low_power(hsotg,
3021 params->host_support_fs_ls_low_power);
3022 dwc2_set_param_enable_dynamic_fifo(hsotg,
3023 params->enable_dynamic_fifo);
3024 dwc2_set_param_host_rx_fifo_size(hsotg,
3025 params->host_rx_fifo_size);
3026 dwc2_set_param_host_nperio_tx_fifo_size(hsotg,
3027 params->host_nperio_tx_fifo_size);
3028 dwc2_set_param_host_perio_tx_fifo_size(hsotg,
3029 params->host_perio_tx_fifo_size);
3030 dwc2_set_param_max_transfer_size(hsotg,
3031 params->max_transfer_size);
3032 dwc2_set_param_max_packet_count(hsotg,
3033 params->max_packet_count);
3034 dwc2_set_param_host_channels(hsotg, params->host_channels);
3035 dwc2_set_param_phy_type(hsotg, params->phy_type);
3036 dwc2_set_param_speed(hsotg, params->speed);
3037 dwc2_set_param_host_ls_low_power_phy_clk(hsotg,
3038 params->host_ls_low_power_phy_clk);
3039 dwc2_set_param_phy_ulpi_ddr(hsotg, params->phy_ulpi_ddr);
3040 dwc2_set_param_phy_ulpi_ext_vbus(hsotg,
3041 params->phy_ulpi_ext_vbus);
3042 dwc2_set_param_phy_utmi_width(hsotg, params->phy_utmi_width);
3043 dwc2_set_param_ulpi_fs_ls(hsotg, params->ulpi_fs_ls);
3044 dwc2_set_param_ts_dline(hsotg, params->ts_dline);
3045 dwc2_set_param_i2c_enable(hsotg, params->i2c_enable);
3046 dwc2_set_param_en_multiple_tx_fifo(hsotg,
3047 params->en_multiple_tx_fifo);
3048 dwc2_set_param_reload_ctl(hsotg, params->reload_ctl);
3049 dwc2_set_param_ahbcfg(hsotg, params->ahbcfg);
3050 dwc2_set_param_otg_ver(hsotg, params->otg_ver);
3051 dwc2_set_param_uframe_sched(hsotg, params->uframe_sched);
Gregory Herreroa6d249d2015-04-29 22:09:04 +02003052 dwc2_set_param_external_id_pin_ctl(hsotg, params->external_id_pin_ctl);
Gregory Herrero285046a2015-04-29 22:09:19 +02003053 dwc2_set_param_hibernation(hsotg, params->hibernation);
Paul Zimmermane8576e62013-11-25 13:42:47 -08003054}
3055
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003056/**
3057 * During device initialization, read various hardware configuration
3058 * registers and interpret the contents.
3059 */
3060int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
3061{
3062 struct dwc2_hw_params *hw = &hsotg->hw_params;
3063 unsigned width;
3064 u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
3065 u32 hptxfsiz, grxfsiz, gnptxfsiz;
3066 u32 gusbcfg;
3067
3068 /*
3069 * Attempt to ensure this device is really a DWC_otg Controller.
3070 * Read and verify the GSNPSID register contents. The value should be
3071 * 0x45f42xxx or 0x45f43xxx, which corresponds to either "OT2" or "OT3",
3072 * as in "OTG version 2.xx" or "OTG version 3.xx".
3073 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003074 hw->snpsid = dwc2_readl(hsotg->regs + GSNPSID);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003075 if ((hw->snpsid & 0xfffff000) != 0x4f542000 &&
3076 (hw->snpsid & 0xfffff000) != 0x4f543000) {
3077 dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
3078 hw->snpsid);
3079 return -ENODEV;
3080 }
3081
3082 dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
3083 hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
3084 hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
3085
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003086 hwcfg1 = dwc2_readl(hsotg->regs + GHWCFG1);
3087 hwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
3088 hwcfg3 = dwc2_readl(hsotg->regs + GHWCFG3);
3089 hwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
3090 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003091
3092 dev_dbg(hsotg->dev, "hwcfg1=%08x\n", hwcfg1);
3093 dev_dbg(hsotg->dev, "hwcfg2=%08x\n", hwcfg2);
3094 dev_dbg(hsotg->dev, "hwcfg3=%08x\n", hwcfg3);
3095 dev_dbg(hsotg->dev, "hwcfg4=%08x\n", hwcfg4);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003096 dev_dbg(hsotg->dev, "grxfsiz=%08x\n", grxfsiz);
3097
Doug Anderson2867c052014-08-07 12:48:11 -07003098 /* Force host mode to get HPTXFSIZ / GNPTXFSIZ exact power on value */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003099 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003100 gusbcfg |= GUSBCFG_FORCEHOSTMODE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003101 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003102 usleep_range(100000, 150000);
3103
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003104 gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
3105 hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
Doug Anderson2867c052014-08-07 12:48:11 -07003106 dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003107 dev_dbg(hsotg->dev, "hptxfsiz=%08x\n", hptxfsiz);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003108 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003109 gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003110 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003111 usleep_range(100000, 150000);
3112
3113 /* hwcfg2 */
3114 hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
3115 GHWCFG2_OP_MODE_SHIFT;
3116 hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
3117 GHWCFG2_ARCHITECTURE_SHIFT;
3118 hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
3119 hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
3120 GHWCFG2_NUM_HOST_CHAN_SHIFT);
3121 hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
3122 GHWCFG2_HS_PHY_TYPE_SHIFT;
3123 hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
3124 GHWCFG2_FS_PHY_TYPE_SHIFT;
3125 hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
3126 GHWCFG2_NUM_DEV_EP_SHIFT;
3127 hw->nperio_tx_q_depth =
3128 (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
3129 GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
3130 hw->host_perio_tx_q_depth =
3131 (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
3132 GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
3133 hw->dev_token_q_depth =
3134 (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
3135 GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
3136
3137 /* hwcfg3 */
3138 width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
3139 GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
3140 hw->max_transfer_size = (1 << (width + 11)) - 1;
Paul Zimmermane8f8c142014-09-16 13:47:26 -07003141 /*
3142 * Clip max_transfer_size to 65535. dwc2_hc_setup_align_buf() allocates
3143 * coherent buffers with this size, and if it's too large we can
3144 * exhaust the coherent DMA pool.
3145 */
3146 if (hw->max_transfer_size > 65535)
3147 hw->max_transfer_size = 65535;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003148 width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
3149 GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
3150 hw->max_packet_count = (1 << (width + 4)) - 1;
3151 hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
3152 hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
3153 GHWCFG3_DFIFO_DEPTH_SHIFT;
3154
3155 /* hwcfg4 */
3156 hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
3157 hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
3158 GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
3159 hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
3160 hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02003161 hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
3162 GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003163
3164 /* fifo sizes */
3165 hw->host_rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
3166 GRXFSIZ_DEPTH_SHIFT;
3167 hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
3168 FIFOSIZE_DEPTH_SHIFT;
3169 hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
3170 FIFOSIZE_DEPTH_SHIFT;
3171
3172 dev_dbg(hsotg->dev, "Detected values from hardware:\n");
3173 dev_dbg(hsotg->dev, " op_mode=%d\n",
3174 hw->op_mode);
3175 dev_dbg(hsotg->dev, " arch=%d\n",
3176 hw->arch);
3177 dev_dbg(hsotg->dev, " dma_desc_enable=%d\n",
3178 hw->dma_desc_enable);
3179 dev_dbg(hsotg->dev, " power_optimized=%d\n",
3180 hw->power_optimized);
3181 dev_dbg(hsotg->dev, " i2c_enable=%d\n",
3182 hw->i2c_enable);
3183 dev_dbg(hsotg->dev, " hs_phy_type=%d\n",
3184 hw->hs_phy_type);
3185 dev_dbg(hsotg->dev, " fs_phy_type=%d\n",
3186 hw->fs_phy_type);
Masanari Iida971bd8f2015-05-20 23:54:02 +09003187 dev_dbg(hsotg->dev, " utmi_phy_data_width=%d\n",
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02003188 hw->utmi_phy_data_width);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003189 dev_dbg(hsotg->dev, " num_dev_ep=%d\n",
3190 hw->num_dev_ep);
3191 dev_dbg(hsotg->dev, " num_dev_perio_in_ep=%d\n",
3192 hw->num_dev_perio_in_ep);
3193 dev_dbg(hsotg->dev, " host_channels=%d\n",
3194 hw->host_channels);
3195 dev_dbg(hsotg->dev, " max_transfer_size=%d\n",
3196 hw->max_transfer_size);
3197 dev_dbg(hsotg->dev, " max_packet_count=%d\n",
3198 hw->max_packet_count);
3199 dev_dbg(hsotg->dev, " nperio_tx_q_depth=0x%0x\n",
3200 hw->nperio_tx_q_depth);
3201 dev_dbg(hsotg->dev, " host_perio_tx_q_depth=0x%0x\n",
3202 hw->host_perio_tx_q_depth);
3203 dev_dbg(hsotg->dev, " dev_token_q_depth=0x%0x\n",
3204 hw->dev_token_q_depth);
3205 dev_dbg(hsotg->dev, " enable_dynamic_fifo=%d\n",
3206 hw->enable_dynamic_fifo);
3207 dev_dbg(hsotg->dev, " en_multiple_tx_fifo=%d\n",
3208 hw->en_multiple_tx_fifo);
3209 dev_dbg(hsotg->dev, " total_fifo_size=%d\n",
3210 hw->total_fifo_size);
3211 dev_dbg(hsotg->dev, " host_rx_fifo_size=%d\n",
3212 hw->host_rx_fifo_size);
3213 dev_dbg(hsotg->dev, " host_nperio_tx_fifo_size=%d\n",
3214 hw->host_nperio_tx_fifo_size);
3215 dev_dbg(hsotg->dev, " host_perio_tx_fifo_size=%d\n",
3216 hw->host_perio_tx_fifo_size);
3217 dev_dbg(hsotg->dev, "\n");
3218
3219 return 0;
3220}
Mian Yousaf Kaukabecb176c2015-04-29 22:09:05 +02003221
3222/*
3223 * Sets all parameters to the given value.
3224 *
3225 * Assumes that the dwc2_core_params struct contains only integers.
3226 */
3227void dwc2_set_all_params(struct dwc2_core_params *params, int value)
3228{
3229 int *p = (int *)params;
3230 size_t size = sizeof(*params) / sizeof(*p);
3231 int i;
3232
3233 for (i = 0; i < size; i++)
3234 p[i] = value;
3235}
Mian Yousaf Kaukabecb176c2015-04-29 22:09:05 +02003236
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003237
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003238u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg)
3239{
Paul Zimmermanb66a3f02013-11-22 16:43:50 -08003240 return hsotg->core_params->otg_ver == 1 ? 0x0200 : 0x0103;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003241}
3242
Paul Zimmerman057715f2013-11-22 16:43:51 -08003243bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003244{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003245 if (dwc2_readl(hsotg->regs + GSNPSID) == 0xffffffff)
Paul Zimmerman057715f2013-11-22 16:43:51 -08003246 return false;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003247 else
Paul Zimmerman057715f2013-11-22 16:43:51 -08003248 return true;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003249}
3250
3251/**
3252 * dwc2_enable_global_interrupts() - Enables the controller's Global
3253 * Interrupt in the AHB Config register
3254 *
3255 * @hsotg: Programming view of DWC_otg controller
3256 */
3257void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
3258{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003259 u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003260
3261 ahbcfg |= GAHBCFG_GLBL_INTR_EN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003262 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003263}
3264
3265/**
3266 * dwc2_disable_global_interrupts() - Disables the controller's Global
3267 * Interrupt in the AHB Config register
3268 *
3269 * @hsotg: Programming view of DWC_otg controller
3270 */
3271void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
3272{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003273 u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003274
3275 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003276 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003277}
3278
3279MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
3280MODULE_AUTHOR("Synopsys, Inc.");
3281MODULE_LICENSE("Dual BSD/GPL");