blob: 97de85521eb68c4a11410e59dcac44b40f3bb925 [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 Kaukab44e4a602015-10-12 11:23:27 +0200866 intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300867 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700868}
869
870/**
871 * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
872 *
873 * @hsotg: Programming view of DWC_otg controller
874 */
875void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
876{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300877 u32 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700878
879 /* Disable host mode interrupts without disturbing common interrupts */
880 intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
Mian Yousaf Kaukab77dbf712015-09-22 15:16:47 +0200881 GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300882 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700883}
884
Dinh Nguyen112fe8e2014-05-07 08:31:29 -0500885/*
886 * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
887 * For system that have a total fifo depth that is smaller than the default
888 * RX + TX fifo size.
889 *
890 * @hsotg: Programming view of DWC_otg controller
891 */
892static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
893{
894 struct dwc2_core_params *params = hsotg->core_params;
895 struct dwc2_hw_params *hw = &hsotg->hw_params;
896 u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
897
898 total_fifo_size = hw->total_fifo_size;
899 rxfsiz = params->host_rx_fifo_size;
900 nptxfsiz = params->host_nperio_tx_fifo_size;
901 ptxfsiz = params->host_perio_tx_fifo_size;
902
903 /*
904 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
905 * allocation with support for high bandwidth endpoints. Synopsys
906 * defines MPS(Max Packet size) for a periodic EP=1024, and for
907 * non-periodic as 512.
908 */
909 if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
910 /*
911 * For Buffer DMA mode/Scatter Gather DMA mode
912 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
913 * with n = number of host channel.
914 * 2 * ((1024/4) + 2) = 516
915 */
916 rxfsiz = 516 + hw->host_channels;
917
918 /*
919 * min non-periodic tx fifo depth
920 * 2 * (largest non-periodic USB packet used / 4)
921 * 2 * (512/4) = 256
922 */
923 nptxfsiz = 256;
924
925 /*
926 * min periodic tx fifo depth
927 * (largest packet size*MC)/4
928 * (1024 * 3)/4 = 768
929 */
930 ptxfsiz = 768;
931
932 params->host_rx_fifo_size = rxfsiz;
933 params->host_nperio_tx_fifo_size = nptxfsiz;
934 params->host_perio_tx_fifo_size = ptxfsiz;
935 }
936
937 /*
938 * If the summation of RX, NPTX and PTX fifo sizes is still
939 * bigger than the total_fifo_size, then we have a problem.
940 *
941 * We won't be able to allocate as many endpoints. Right now,
942 * we're just printing an error message, but ideally this FIFO
943 * allocation algorithm would be improved in the future.
944 *
945 * FIXME improve this FIFO allocation algorithm.
946 */
947 if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
948 dev_err(hsotg->dev, "invalid fifo sizes\n");
949}
950
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700951static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
952{
953 struct dwc2_core_params *params = hsotg->core_params;
Matthijs Kooijmana1fc5242013-08-30 18:45:20 +0200954 u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700955
Matthijs Kooijman12086052013-04-29 19:46:35 +0000956 if (!params->enable_dynamic_fifo)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700957 return;
958
Dinh Nguyen112fe8e2014-05-07 08:31:29 -0500959 dwc2_calculate_dynamic_fifo(hsotg);
960
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700961 /* Rx FIFO */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300962 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
Matthijs Kooijmana1fc5242013-08-30 18:45:20 +0200963 dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
964 grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
965 grxfsiz |= params->host_rx_fifo_size <<
966 GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300967 dwc2_writel(grxfsiz, hsotg->regs + GRXFSIZ);
968 dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
969 dwc2_readl(hsotg->regs + GRXFSIZ));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700970
971 /* Non-periodic Tx FIFO */
972 dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300973 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700974 nptxfsiz = params->host_nperio_tx_fifo_size <<
975 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
976 nptxfsiz |= params->host_rx_fifo_size <<
977 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300978 dwc2_writel(nptxfsiz, hsotg->regs + GNPTXFSIZ);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700979 dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300980 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700981
982 /* Periodic Tx FIFO */
983 dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300984 dwc2_readl(hsotg->regs + HPTXFSIZ));
Matthijs Kooijmanc35205a2013-08-30 18:45:18 +0200985 hptxfsiz = params->host_perio_tx_fifo_size <<
986 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
987 hptxfsiz |= (params->host_rx_fifo_size +
988 params->host_nperio_tx_fifo_size) <<
989 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300990 dwc2_writel(hptxfsiz, hsotg->regs + HPTXFSIZ);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700991 dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300992 dwc2_readl(hsotg->regs + HPTXFSIZ));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700993
994 if (hsotg->core_params->en_multiple_tx_fifo > 0 &&
Matthijs Kooijman9badec22013-08-30 18:45:21 +0200995 hsotg->hw_params.snpsid <= DWC2_CORE_REV_2_94a) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -0700996 /*
997 * Global DFIFOCFG calculation for Host mode -
998 * include RxFIFO, NPTXFIFO and HPTXFIFO
999 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001000 dfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001001 dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
Matthijs Kooijman08b9f9d2013-08-30 18:45:19 +02001002 dfifocfg |= (params->host_rx_fifo_size +
1003 params->host_nperio_tx_fifo_size +
1004 params->host_perio_tx_fifo_size) <<
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001005 GDFIFOCFG_EPINFOBASE_SHIFT &
1006 GDFIFOCFG_EPINFOBASE_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001007 dwc2_writel(dfifocfg, hsotg->regs + GDFIFOCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001008 }
1009}
1010
1011/**
1012 * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
1013 * Host mode
1014 *
1015 * @hsotg: Programming view of DWC_otg controller
1016 *
1017 * This function flushes the Tx and Rx FIFOs and flushes any entries in the
1018 * request queues. Host channels are reset to ensure that they are ready for
1019 * performing transfers.
1020 */
1021void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
1022{
1023 u32 hcfg, hfir, otgctl;
1024
1025 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
1026
1027 /* Restart the Phy Clock */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001028 dwc2_writel(0, hsotg->regs + PCGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001029
1030 /* Initialize Host Configuration Register */
1031 dwc2_init_fs_ls_pclk_sel(hsotg);
1032 if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001033 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001034 hcfg |= HCFG_FSLSSUPP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001035 dwc2_writel(hcfg, hsotg->regs + HCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001036 }
1037
1038 /*
1039 * This bit allows dynamic reloading of the HFIR register during
Masanari Iida0dcde5082013-09-13 23:34:36 +09001040 * runtime. This bit needs to be programmed during initial configuration
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001041 * and its value must not be changed during runtime.
1042 */
1043 if (hsotg->core_params->reload_ctl > 0) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001044 hfir = dwc2_readl(hsotg->regs + HFIR);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001045 hfir |= HFIR_RLDCTRL;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001046 dwc2_writel(hfir, hsotg->regs + HFIR);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001047 }
1048
1049 if (hsotg->core_params->dma_desc_enable > 0) {
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001050 u32 op_mode = hsotg->hw_params.op_mode;
1051 if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
1052 !hsotg->hw_params.dma_desc_enable ||
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001053 op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
1054 op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
1055 op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
1056 dev_err(hsotg->dev,
1057 "Hardware does not support descriptor DMA mode -\n");
1058 dev_err(hsotg->dev,
1059 "falling back to buffer DMA mode.\n");
1060 hsotg->core_params->dma_desc_enable = 0;
1061 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001062 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001063 hcfg |= HCFG_DESCDMA;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001064 dwc2_writel(hcfg, hsotg->regs + HCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001065 }
1066 }
1067
1068 /* Configure data FIFO sizes */
1069 dwc2_config_fifos(hsotg);
1070
1071 /* TODO - check this */
1072 /* Clear Host Set HNP Enable in the OTG Control Register */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001073 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001074 otgctl &= ~GOTGCTL_HSTSETHNPEN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001075 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001076
1077 /* Make sure the FIFOs are flushed */
1078 dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
1079 dwc2_flush_rx_fifo(hsotg);
1080
1081 /* Clear Host Set HNP Enable in the OTG Control Register */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001082 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001083 otgctl &= ~GOTGCTL_HSTSETHNPEN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001084 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001085
1086 if (hsotg->core_params->dma_desc_enable <= 0) {
1087 int num_channels, i;
1088 u32 hcchar;
1089
1090 /* Flush out any leftover queued requests */
1091 num_channels = hsotg->core_params->host_channels;
1092 for (i = 0; i < num_channels; i++) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001093 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001094 hcchar &= ~HCCHAR_CHENA;
1095 hcchar |= HCCHAR_CHDIS;
1096 hcchar &= ~HCCHAR_EPDIR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001097 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001098 }
1099
1100 /* Halt all channels to put them into a known state */
1101 for (i = 0; i < num_channels; i++) {
1102 int count = 0;
1103
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001104 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001105 hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
1106 hcchar &= ~HCCHAR_EPDIR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001107 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001108 dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
1109 __func__, i);
1110 do {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001111 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001112 if (++count > 1000) {
1113 dev_err(hsotg->dev,
1114 "Unable to clear enable on channel %d\n",
1115 i);
1116 break;
1117 }
1118 udelay(1);
1119 } while (hcchar & HCCHAR_CHENA);
1120 }
1121 }
1122
1123 /* Turn on the vbus power */
1124 dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
1125 if (hsotg->op_state == OTG_STATE_A_HOST) {
1126 u32 hprt0 = dwc2_read_hprt0(hsotg);
1127
1128 dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
1129 !!(hprt0 & HPRT0_PWR));
1130 if (!(hprt0 & HPRT0_PWR)) {
1131 hprt0 |= HPRT0_PWR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001132 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001133 }
1134 }
1135
1136 dwc2_enable_host_interrupts(hsotg);
1137}
1138
1139static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
1140 struct dwc2_host_chan *chan)
1141{
1142 u32 hcintmsk = HCINTMSK_CHHLTD;
1143
1144 switch (chan->ep_type) {
1145 case USB_ENDPOINT_XFER_CONTROL:
1146 case USB_ENDPOINT_XFER_BULK:
1147 dev_vdbg(hsotg->dev, "control/bulk\n");
1148 hcintmsk |= HCINTMSK_XFERCOMPL;
1149 hcintmsk |= HCINTMSK_STALL;
1150 hcintmsk |= HCINTMSK_XACTERR;
1151 hcintmsk |= HCINTMSK_DATATGLERR;
1152 if (chan->ep_is_in) {
1153 hcintmsk |= HCINTMSK_BBLERR;
1154 } else {
1155 hcintmsk |= HCINTMSK_NAK;
1156 hcintmsk |= HCINTMSK_NYET;
1157 if (chan->do_ping)
1158 hcintmsk |= HCINTMSK_ACK;
1159 }
1160
1161 if (chan->do_split) {
1162 hcintmsk |= HCINTMSK_NAK;
1163 if (chan->complete_split)
1164 hcintmsk |= HCINTMSK_NYET;
1165 else
1166 hcintmsk |= HCINTMSK_ACK;
1167 }
1168
1169 if (chan->error_state)
1170 hcintmsk |= HCINTMSK_ACK;
1171 break;
1172
1173 case USB_ENDPOINT_XFER_INT:
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001174 if (dbg_perio())
1175 dev_vdbg(hsotg->dev, "intr\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001176 hcintmsk |= HCINTMSK_XFERCOMPL;
1177 hcintmsk |= HCINTMSK_NAK;
1178 hcintmsk |= HCINTMSK_STALL;
1179 hcintmsk |= HCINTMSK_XACTERR;
1180 hcintmsk |= HCINTMSK_DATATGLERR;
1181 hcintmsk |= HCINTMSK_FRMOVRUN;
1182
1183 if (chan->ep_is_in)
1184 hcintmsk |= HCINTMSK_BBLERR;
1185 if (chan->error_state)
1186 hcintmsk |= HCINTMSK_ACK;
1187 if (chan->do_split) {
1188 if (chan->complete_split)
1189 hcintmsk |= HCINTMSK_NYET;
1190 else
1191 hcintmsk |= HCINTMSK_ACK;
1192 }
1193 break;
1194
1195 case USB_ENDPOINT_XFER_ISOC:
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001196 if (dbg_perio())
1197 dev_vdbg(hsotg->dev, "isoc\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001198 hcintmsk |= HCINTMSK_XFERCOMPL;
1199 hcintmsk |= HCINTMSK_FRMOVRUN;
1200 hcintmsk |= HCINTMSK_ACK;
1201
1202 if (chan->ep_is_in) {
1203 hcintmsk |= HCINTMSK_XACTERR;
1204 hcintmsk |= HCINTMSK_BBLERR;
1205 }
1206 break;
1207 default:
1208 dev_err(hsotg->dev, "## Unknown EP type ##\n");
1209 break;
1210 }
1211
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001212 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001213 if (dbg_hc(chan))
1214 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001215}
1216
1217static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
1218 struct dwc2_host_chan *chan)
1219{
1220 u32 hcintmsk = HCINTMSK_CHHLTD;
1221
1222 /*
1223 * For Descriptor DMA mode core halts the channel on AHB error.
1224 * Interrupt is not required.
1225 */
1226 if (hsotg->core_params->dma_desc_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001227 if (dbg_hc(chan))
1228 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001229 hcintmsk |= HCINTMSK_AHBERR;
1230 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001231 if (dbg_hc(chan))
1232 dev_vdbg(hsotg->dev, "desc DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001233 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1234 hcintmsk |= HCINTMSK_XFERCOMPL;
1235 }
1236
1237 if (chan->error_state && !chan->do_split &&
1238 chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001239 if (dbg_hc(chan))
1240 dev_vdbg(hsotg->dev, "setting ACK\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001241 hcintmsk |= HCINTMSK_ACK;
1242 if (chan->ep_is_in) {
1243 hcintmsk |= HCINTMSK_DATATGLERR;
1244 if (chan->ep_type != USB_ENDPOINT_XFER_INT)
1245 hcintmsk |= HCINTMSK_NAK;
1246 }
1247 }
1248
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001249 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001250 if (dbg_hc(chan))
1251 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001252}
1253
1254static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
1255 struct dwc2_host_chan *chan)
1256{
1257 u32 intmsk;
1258
1259 if (hsotg->core_params->dma_enable > 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001260 if (dbg_hc(chan))
1261 dev_vdbg(hsotg->dev, "DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001262 dwc2_hc_enable_dma_ints(hsotg, chan);
1263 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001264 if (dbg_hc(chan))
1265 dev_vdbg(hsotg->dev, "DMA disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001266 dwc2_hc_enable_slave_ints(hsotg, chan);
1267 }
1268
1269 /* Enable the top level host channel interrupt */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001270 intmsk = dwc2_readl(hsotg->regs + HAINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001271 intmsk |= 1 << chan->hc_num;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001272 dwc2_writel(intmsk, hsotg->regs + HAINTMSK);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001273 if (dbg_hc(chan))
1274 dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001275
1276 /* Make sure host channel interrupts are enabled */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001277 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001278 intmsk |= GINTSTS_HCHINT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001279 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001280 if (dbg_hc(chan))
1281 dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001282}
1283
1284/**
1285 * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
1286 * a specific endpoint
1287 *
1288 * @hsotg: Programming view of DWC_otg controller
1289 * @chan: Information needed to initialize the host channel
1290 *
1291 * The HCCHARn register is set up with the characteristics specified in chan.
1292 * Host channel interrupts that may need to be serviced while this transfer is
1293 * in progress are enabled.
1294 */
1295void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1296{
1297 u8 hc_num = chan->hc_num;
1298 u32 hcintmsk;
1299 u32 hcchar;
1300 u32 hcsplt = 0;
1301
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001302 if (dbg_hc(chan))
1303 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001304
1305 /* Clear old interrupt conditions for this host channel */
1306 hcintmsk = 0xffffffff;
1307 hcintmsk &= ~HCINTMSK_RESERVED14_31;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001308 dwc2_writel(hcintmsk, hsotg->regs + HCINT(hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001309
1310 /* Enable channel interrupts required for this transfer */
1311 dwc2_hc_enable_ints(hsotg, chan);
1312
1313 /*
1314 * Program the HCCHARn register with the endpoint characteristics for
1315 * the current transfer
1316 */
1317 hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
1318 hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
1319 if (chan->ep_is_in)
1320 hcchar |= HCCHAR_EPDIR;
1321 if (chan->speed == USB_SPEED_LOW)
1322 hcchar |= HCCHAR_LSPDDEV;
1323 hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
1324 hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001325 dwc2_writel(hcchar, hsotg->regs + HCCHAR(hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001326 if (dbg_hc(chan)) {
1327 dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
1328 hc_num, hcchar);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001329
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001330 dev_vdbg(hsotg->dev, "%s: Channel %d\n",
1331 __func__, hc_num);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001332 dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001333 chan->dev_addr);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001334 dev_vdbg(hsotg->dev, " Ep Num: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001335 chan->ep_num);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001336 dev_vdbg(hsotg->dev, " Is In: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001337 chan->ep_is_in);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001338 dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001339 chan->speed == USB_SPEED_LOW);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001340 dev_vdbg(hsotg->dev, " Ep Type: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001341 chan->ep_type);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001342 dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001343 chan->max_packet);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001344 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001345
1346 /* Program the HCSPLT register for SPLITs */
1347 if (chan->do_split) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001348 if (dbg_hc(chan))
1349 dev_vdbg(hsotg->dev,
1350 "Programming HC %d with split --> %s\n",
1351 hc_num,
1352 chan->complete_split ? "CSPLIT" : "SSPLIT");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001353 if (chan->complete_split)
1354 hcsplt |= HCSPLT_COMPSPLT;
1355 hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
1356 HCSPLT_XACTPOS_MASK;
1357 hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
1358 HCSPLT_HUBADDR_MASK;
1359 hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
1360 HCSPLT_PRTADDR_MASK;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001361 if (dbg_hc(chan)) {
1362 dev_vdbg(hsotg->dev, " comp split %d\n",
1363 chan->complete_split);
1364 dev_vdbg(hsotg->dev, " xact pos %d\n",
1365 chan->xact_pos);
1366 dev_vdbg(hsotg->dev, " hub addr %d\n",
1367 chan->hub_addr);
1368 dev_vdbg(hsotg->dev, " hub port %d\n",
1369 chan->hub_port);
1370 dev_vdbg(hsotg->dev, " is_in %d\n",
1371 chan->ep_is_in);
1372 dev_vdbg(hsotg->dev, " Max Pkt %d\n",
Matthijs Kooijman57bb8ae2013-08-30 18:45:17 +02001373 chan->max_packet);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001374 dev_vdbg(hsotg->dev, " xferlen %d\n",
1375 chan->xfer_len);
1376 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001377 }
1378
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001379 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001380}
1381
1382/**
1383 * dwc2_hc_halt() - Attempts to halt a host channel
1384 *
1385 * @hsotg: Controller register interface
1386 * @chan: Host channel to halt
1387 * @halt_status: Reason for halting the channel
1388 *
1389 * This function should only be called in Slave mode or to abort a transfer in
1390 * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
1391 * controller halts the channel when the transfer is complete or a condition
1392 * occurs that requires application intervention.
1393 *
1394 * In slave mode, checks for a free request queue entry, then sets the Channel
1395 * Enable and Channel Disable bits of the Host Channel Characteristics
1396 * register of the specified channel to intiate the halt. If there is no free
1397 * request queue entry, sets only the Channel Disable bit of the HCCHARn
1398 * register to flush requests for this channel. In the latter case, sets a
1399 * flag to indicate that the host channel needs to be halted when a request
1400 * queue slot is open.
1401 *
1402 * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
1403 * HCCHARn register. The controller ensures there is space in the request
1404 * queue before submitting the halt request.
1405 *
1406 * Some time may elapse before the core flushes any posted requests for this
1407 * host channel and halts. The Channel Halted interrupt handler completes the
1408 * deactivation of the host channel.
1409 */
1410void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
1411 enum dwc2_halt_status halt_status)
1412{
1413 u32 nptxsts, hptxsts, hcchar;
1414
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001415 if (dbg_hc(chan))
1416 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001417 if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
1418 dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
1419
1420 if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
1421 halt_status == DWC2_HC_XFER_AHB_ERR) {
1422 /*
1423 * Disable all channel interrupts except Ch Halted. The QTD
1424 * and QH state associated with this transfer has been cleared
1425 * (in the case of URB_DEQUEUE), so the channel needs to be
1426 * shut down carefully to prevent crashes.
1427 */
1428 u32 hcintmsk = HCINTMSK_CHHLTD;
1429
1430 dev_vdbg(hsotg->dev, "dequeue/error\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001431 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001432
1433 /*
1434 * Make sure no other interrupts besides halt are currently
1435 * pending. Handling another interrupt could cause a crash due
1436 * to the QTD and QH state.
1437 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001438 dwc2_writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001439
1440 /*
1441 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
1442 * even if the channel was already halted for some other
1443 * reason
1444 */
1445 chan->halt_status = halt_status;
1446
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001447 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001448 if (!(hcchar & HCCHAR_CHENA)) {
1449 /*
1450 * The channel is either already halted or it hasn't
1451 * started yet. In DMA mode, the transfer may halt if
1452 * it finishes normally or a condition occurs that
1453 * requires driver intervention. Don't want to halt
1454 * the channel again. In either Slave or DMA mode,
1455 * it's possible that the transfer has been assigned
1456 * to a channel, but not started yet when an URB is
1457 * dequeued. Don't want to halt a channel that hasn't
1458 * started yet.
1459 */
1460 return;
1461 }
1462 }
1463 if (chan->halt_pending) {
1464 /*
1465 * A halt has already been issued for this channel. This might
1466 * happen when a transfer is aborted by a higher level in
1467 * the stack.
1468 */
1469 dev_vdbg(hsotg->dev,
1470 "*** %s: Channel %d, chan->halt_pending already set ***\n",
1471 __func__, chan->hc_num);
1472 return;
1473 }
1474
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001475 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001476
1477 /* No need to set the bit in DDMA for disabling the channel */
1478 /* TODO check it everywhere channel is disabled */
1479 if (hsotg->core_params->dma_desc_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001480 if (dbg_hc(chan))
1481 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001482 hcchar |= HCCHAR_CHENA;
1483 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001484 if (dbg_hc(chan))
1485 dev_dbg(hsotg->dev, "desc DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001486 }
1487 hcchar |= HCCHAR_CHDIS;
1488
1489 if (hsotg->core_params->dma_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001490 if (dbg_hc(chan))
1491 dev_vdbg(hsotg->dev, "DMA not enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001492 hcchar |= HCCHAR_CHENA;
1493
1494 /* Check for space in the request queue to issue the halt */
1495 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1496 chan->ep_type == USB_ENDPOINT_XFER_BULK) {
1497 dev_vdbg(hsotg->dev, "control/bulk\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001498 nptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001499 if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
1500 dev_vdbg(hsotg->dev, "Disabling channel\n");
1501 hcchar &= ~HCCHAR_CHENA;
1502 }
1503 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001504 if (dbg_perio())
1505 dev_vdbg(hsotg->dev, "isoc/intr\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001506 hptxsts = dwc2_readl(hsotg->regs + HPTXSTS);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001507 if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
1508 hsotg->queuing_high_bandwidth) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001509 if (dbg_perio())
1510 dev_vdbg(hsotg->dev, "Disabling channel\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001511 hcchar &= ~HCCHAR_CHENA;
1512 }
1513 }
1514 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001515 if (dbg_hc(chan))
1516 dev_vdbg(hsotg->dev, "DMA enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001517 }
1518
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001519 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001520 chan->halt_status = halt_status;
1521
1522 if (hcchar & HCCHAR_CHENA) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001523 if (dbg_hc(chan))
1524 dev_vdbg(hsotg->dev, "Channel enabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001525 chan->halt_pending = 1;
1526 chan->halt_on_queue = 0;
1527 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001528 if (dbg_hc(chan))
1529 dev_vdbg(hsotg->dev, "Channel disabled\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001530 chan->halt_on_queue = 1;
1531 }
1532
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001533 if (dbg_hc(chan)) {
1534 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1535 chan->hc_num);
1536 dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
1537 hcchar);
1538 dev_vdbg(hsotg->dev, " halt_pending: %d\n",
1539 chan->halt_pending);
1540 dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
1541 chan->halt_on_queue);
1542 dev_vdbg(hsotg->dev, " halt_status: %d\n",
1543 chan->halt_status);
1544 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001545}
1546
1547/**
1548 * dwc2_hc_cleanup() - Clears the transfer state for a host channel
1549 *
1550 * @hsotg: Programming view of DWC_otg controller
1551 * @chan: Identifies the host channel to clean up
1552 *
1553 * This function is normally called after a transfer is done and the host
1554 * channel is being released
1555 */
1556void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1557{
1558 u32 hcintmsk;
1559
1560 chan->xfer_started = 0;
1561
1562 /*
1563 * Clear channel interrupt enables and any unhandled channel interrupt
1564 * conditions
1565 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001566 dwc2_writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001567 hcintmsk = 0xffffffff;
1568 hcintmsk &= ~HCINTMSK_RESERVED14_31;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001569 dwc2_writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001570}
1571
1572/**
1573 * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
1574 * which frame a periodic transfer should occur
1575 *
1576 * @hsotg: Programming view of DWC_otg controller
1577 * @chan: Identifies the host channel to set up and its properties
1578 * @hcchar: Current value of the HCCHAR register for the specified host channel
1579 *
1580 * This function has no effect on non-periodic transfers
1581 */
1582static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
1583 struct dwc2_host_chan *chan, u32 *hcchar)
1584{
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001585 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1586 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001587 /* 1 if _next_ frame is odd, 0 if it's even */
Paul Zimmerman81a58952013-06-24 11:34:23 -07001588 if (!(dwc2_hcd_get_frame_number(hsotg) & 0x1))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001589 *hcchar |= HCCHAR_ODDFRM;
1590 }
1591}
1592
1593static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1594{
1595 /* Set up the initial PID for the transfer */
1596 if (chan->speed == USB_SPEED_HIGH) {
1597 if (chan->ep_is_in) {
1598 if (chan->multi_count == 1)
1599 chan->data_pid_start = DWC2_HC_PID_DATA0;
1600 else if (chan->multi_count == 2)
1601 chan->data_pid_start = DWC2_HC_PID_DATA1;
1602 else
1603 chan->data_pid_start = DWC2_HC_PID_DATA2;
1604 } else {
1605 if (chan->multi_count == 1)
1606 chan->data_pid_start = DWC2_HC_PID_DATA0;
1607 else
1608 chan->data_pid_start = DWC2_HC_PID_MDATA;
1609 }
1610 } else {
1611 chan->data_pid_start = DWC2_HC_PID_DATA0;
1612 }
1613}
1614
1615/**
1616 * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1617 * the Host Channel
1618 *
1619 * @hsotg: Programming view of DWC_otg controller
1620 * @chan: Information needed to initialize the host channel
1621 *
1622 * This function should only be called in Slave mode. For a channel associated
1623 * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1624 * associated with a periodic EP, the periodic Tx FIFO is written.
1625 *
1626 * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1627 * the number of bytes written to the Tx FIFO.
1628 */
1629static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1630 struct dwc2_host_chan *chan)
1631{
1632 u32 i;
1633 u32 remaining_count;
1634 u32 byte_count;
1635 u32 dword_count;
1636 u32 __iomem *data_fifo;
1637 u32 *data_buf = (u32 *)chan->xfer_buf;
1638
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001639 if (dbg_hc(chan))
1640 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001641
1642 data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
1643
1644 remaining_count = chan->xfer_len - chan->xfer_count;
1645 if (remaining_count > chan->max_packet)
1646 byte_count = chan->max_packet;
1647 else
1648 byte_count = remaining_count;
1649
1650 dword_count = (byte_count + 3) / 4;
1651
1652 if (((unsigned long)data_buf & 0x3) == 0) {
1653 /* xfer_buf is DWORD aligned */
1654 for (i = 0; i < dword_count; i++, data_buf++)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001655 dwc2_writel(*data_buf, data_fifo);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001656 } else {
1657 /* xfer_buf is not DWORD aligned */
1658 for (i = 0; i < dword_count; i++, data_buf++) {
1659 u32 data = data_buf[0] | data_buf[1] << 8 |
1660 data_buf[2] << 16 | data_buf[3] << 24;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001661 dwc2_writel(data, data_fifo);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001662 }
1663 }
1664
1665 chan->xfer_count += byte_count;
1666 chan->xfer_buf += byte_count;
1667}
1668
1669/**
1670 * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1671 * channel and starts the transfer
1672 *
1673 * @hsotg: Programming view of DWC_otg controller
1674 * @chan: Information needed to initialize the host channel. The xfer_len value
1675 * may be reduced to accommodate the max widths of the XferSize and
1676 * PktCnt fields in the HCTSIZn register. The multi_count value may be
1677 * changed to reflect the final xfer_len value.
1678 *
1679 * This function may be called in either Slave mode or DMA mode. In Slave mode,
1680 * the caller must ensure that there is sufficient space in the request queue
1681 * and Tx Data FIFO.
1682 *
1683 * For an OUT transfer in Slave mode, it loads a data packet into the
1684 * appropriate FIFO. If necessary, additional data packets are loaded in the
1685 * Host ISR.
1686 *
1687 * For an IN transfer in Slave mode, a data packet is requested. The data
1688 * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1689 * additional data packets are requested in the Host ISR.
1690 *
1691 * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1692 * register along with a packet count of 1 and the channel is enabled. This
1693 * causes a single PING transaction to occur. Other fields in HCTSIZ are
1694 * simply set to 0 since no data transfer occurs in this case.
1695 *
1696 * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1697 * all the information required to perform the subsequent data transfer. In
1698 * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1699 * controller performs the entire PING protocol, then starts the data
1700 * transfer.
1701 */
1702void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1703 struct dwc2_host_chan *chan)
1704{
1705 u32 max_hc_xfer_size = hsotg->core_params->max_transfer_size;
1706 u16 max_hc_pkt_count = hsotg->core_params->max_packet_count;
1707 u32 hcchar;
1708 u32 hctsiz = 0;
1709 u16 num_packets;
1710
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001711 if (dbg_hc(chan))
1712 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001713
1714 if (chan->do_ping) {
1715 if (hsotg->core_params->dma_enable <= 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001716 if (dbg_hc(chan))
1717 dev_vdbg(hsotg->dev, "ping, no DMA\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001718 dwc2_hc_do_ping(hsotg, chan);
1719 chan->xfer_started = 1;
1720 return;
1721 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001722 if (dbg_hc(chan))
1723 dev_vdbg(hsotg->dev, "ping, DMA\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001724 hctsiz |= TSIZ_DOPNG;
1725 }
1726 }
1727
1728 if (chan->do_split) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001729 if (dbg_hc(chan))
1730 dev_vdbg(hsotg->dev, "split\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001731 num_packets = 1;
1732
1733 if (chan->complete_split && !chan->ep_is_in)
1734 /*
1735 * For CSPLIT OUT Transfer, set the size to 0 so the
1736 * core doesn't expect any data written to the FIFO
1737 */
1738 chan->xfer_len = 0;
1739 else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1740 chan->xfer_len = chan->max_packet;
1741 else if (!chan->ep_is_in && chan->xfer_len > 188)
1742 chan->xfer_len = 188;
1743
1744 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1745 TSIZ_XFERSIZE_MASK;
1746 } else {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001747 if (dbg_hc(chan))
1748 dev_vdbg(hsotg->dev, "no split\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001749 /*
1750 * Ensure that the transfer length and packet count will fit
1751 * in the widths allocated for them in the HCTSIZn register
1752 */
1753 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1754 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1755 /*
1756 * Make sure the transfer size is no larger than one
1757 * (micro)frame's worth of data. (A check was done
1758 * when the periodic transfer was accepted to ensure
1759 * that a (micro)frame's worth of data can be
1760 * programmed into a channel.)
1761 */
1762 u32 max_periodic_len =
1763 chan->multi_count * chan->max_packet;
1764
1765 if (chan->xfer_len > max_periodic_len)
1766 chan->xfer_len = max_periodic_len;
1767 } else if (chan->xfer_len > max_hc_xfer_size) {
1768 /*
1769 * Make sure that xfer_len is a multiple of max packet
1770 * size
1771 */
1772 chan->xfer_len =
1773 max_hc_xfer_size - chan->max_packet + 1;
1774 }
1775
1776 if (chan->xfer_len > 0) {
1777 num_packets = (chan->xfer_len + chan->max_packet - 1) /
1778 chan->max_packet;
1779 if (num_packets > max_hc_pkt_count) {
1780 num_packets = max_hc_pkt_count;
1781 chan->xfer_len = num_packets * chan->max_packet;
1782 }
1783 } else {
1784 /* Need 1 packet for transfer length of 0 */
1785 num_packets = 1;
1786 }
1787
1788 if (chan->ep_is_in)
1789 /*
1790 * Always program an integral # of max packets for IN
1791 * transfers
1792 */
1793 chan->xfer_len = num_packets * chan->max_packet;
1794
1795 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1796 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1797 /*
1798 * Make sure that the multi_count field matches the
1799 * actual transfer length
1800 */
1801 chan->multi_count = num_packets;
1802
1803 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1804 dwc2_set_pid_isoc(chan);
1805
1806 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1807 TSIZ_XFERSIZE_MASK;
1808 }
1809
1810 chan->start_pkt_count = num_packets;
1811 hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1812 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1813 TSIZ_SC_MC_PID_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001814 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001815 if (dbg_hc(chan)) {
1816 dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1817 hctsiz, chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001818
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001819 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1820 chan->hc_num);
1821 dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001822 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1823 TSIZ_XFERSIZE_SHIFT);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001824 dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001825 (hctsiz & TSIZ_PKTCNT_MASK) >>
1826 TSIZ_PKTCNT_SHIFT);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001827 dev_vdbg(hsotg->dev, " Start PID: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001828 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1829 TSIZ_SC_MC_PID_SHIFT);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001830 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001831
1832 if (hsotg->core_params->dma_enable > 0) {
1833 dma_addr_t dma_addr;
1834
1835 if (chan->align_buf) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001836 if (dbg_hc(chan))
1837 dev_vdbg(hsotg->dev, "align_buf\n");
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001838 dma_addr = chan->align_buf;
1839 } else {
1840 dma_addr = chan->xfer_dma;
1841 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001842 dwc2_writel((u32)dma_addr, hsotg->regs + HCDMA(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001843 if (dbg_hc(chan))
1844 dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1845 (unsigned long)dma_addr, chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001846 }
1847
1848 /* Start the split */
1849 if (chan->do_split) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001850 u32 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001851
1852 hcsplt |= HCSPLT_SPLTENA;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001853 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001854 }
1855
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001856 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001857 hcchar &= ~HCCHAR_MULTICNT_MASK;
1858 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1859 HCCHAR_MULTICNT_MASK;
1860 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1861
1862 if (hcchar & HCCHAR_CHDIS)
1863 dev_warn(hsotg->dev,
1864 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1865 __func__, chan->hc_num, hcchar);
1866
1867 /* Set host channel enable after all other setup is complete */
1868 hcchar |= HCCHAR_CHENA;
1869 hcchar &= ~HCCHAR_CHDIS;
1870
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001871 if (dbg_hc(chan))
1872 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001873 (hcchar & HCCHAR_MULTICNT_MASK) >>
1874 HCCHAR_MULTICNT_SHIFT);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001875
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001876 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001877 if (dbg_hc(chan))
1878 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1879 chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001880
1881 chan->xfer_started = 1;
1882 chan->requests++;
1883
1884 if (hsotg->core_params->dma_enable <= 0 &&
1885 !chan->ep_is_in && chan->xfer_len > 0)
1886 /* Load OUT packet into the appropriate Tx FIFO */
1887 dwc2_hc_write_packet(hsotg, chan);
1888}
1889
1890/**
1891 * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1892 * host channel and starts the transfer in Descriptor DMA mode
1893 *
1894 * @hsotg: Programming view of DWC_otg controller
1895 * @chan: Information needed to initialize the host channel
1896 *
1897 * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1898 * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1899 * with micro-frame bitmap.
1900 *
1901 * Initializes HCDMA register with descriptor list address and CTD value then
1902 * starts the transfer via enabling the channel.
1903 */
1904void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1905 struct dwc2_host_chan *chan)
1906{
1907 u32 hcchar;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001908 u32 hctsiz = 0;
1909
1910 if (chan->do_ping)
1911 hctsiz |= TSIZ_DOPNG;
1912
1913 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1914 dwc2_set_pid_isoc(chan);
1915
1916 /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1917 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1918 TSIZ_SC_MC_PID_MASK;
1919
1920 /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1921 hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1922
1923 /* Non-zero only for high-speed interrupt endpoints */
1924 hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1925
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001926 if (dbg_hc(chan)) {
1927 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1928 chan->hc_num);
1929 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1930 chan->data_pid_start);
1931 dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
1932 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001933
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001934 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001935
Gregory Herrero95105a92015-11-20 11:49:29 +01001936 dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
1937 chan->desc_list_sz, DMA_TO_DEVICE);
1938
Mian Yousaf Kaukabe23b8a52015-11-20 11:49:30 +01001939 dwc2_writel(chan->desc_list_addr, hsotg->regs + HCDMA(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001940
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001941 if (dbg_hc(chan))
Mian Yousaf Kaukabe23b8a52015-11-20 11:49:30 +01001942 dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
1943 &chan->desc_list_addr, chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001944
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001945 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001946 hcchar &= ~HCCHAR_MULTICNT_MASK;
1947 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1948 HCCHAR_MULTICNT_MASK;
1949
1950 if (hcchar & HCCHAR_CHDIS)
1951 dev_warn(hsotg->dev,
1952 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1953 __func__, chan->hc_num, hcchar);
1954
1955 /* Set host channel enable after all other setup is complete */
1956 hcchar |= HCCHAR_CHENA;
1957 hcchar &= ~HCCHAR_CHDIS;
1958
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001959 if (dbg_hc(chan))
1960 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001961 (hcchar & HCCHAR_MULTICNT_MASK) >>
1962 HCCHAR_MULTICNT_SHIFT);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001963
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001964 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001965 if (dbg_hc(chan))
1966 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1967 chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001968
1969 chan->xfer_started = 1;
1970 chan->requests++;
1971}
1972
1973/**
1974 * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
1975 * a previous call to dwc2_hc_start_transfer()
1976 *
1977 * @hsotg: Programming view of DWC_otg controller
1978 * @chan: Information needed to initialize the host channel
1979 *
1980 * The caller must ensure there is sufficient space in the request queue and Tx
1981 * Data FIFO. This function should only be called in Slave mode. In DMA mode,
1982 * the controller acts autonomously to complete transfers programmed to a host
1983 * channel.
1984 *
1985 * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
1986 * if there is any data remaining to be queued. For an IN transfer, another
1987 * data packet is always requested. For the SETUP phase of a control transfer,
1988 * this function does nothing.
1989 *
1990 * Return: 1 if a new request is queued, 0 if no more requests are required
1991 * for this transfer
1992 */
1993int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
1994 struct dwc2_host_chan *chan)
1995{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001996 if (dbg_hc(chan))
1997 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1998 chan->hc_num);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07001999
2000 if (chan->do_split)
2001 /* SPLITs always queue just once per channel */
2002 return 0;
2003
2004 if (chan->data_pid_start == DWC2_HC_PID_SETUP)
2005 /* SETUPs are queued only once since they can't be NAK'd */
2006 return 0;
2007
2008 if (chan->ep_is_in) {
2009 /*
2010 * Always queue another request for other IN transfers. If
2011 * back-to-back INs are issued and NAKs are received for both,
2012 * the driver may still be processing the first NAK when the
2013 * second NAK is received. When the interrupt handler clears
2014 * the NAK interrupt for the first NAK, the second NAK will
2015 * not be seen. So we can't depend on the NAK interrupt
2016 * handler to requeue a NAK'd request. Instead, IN requests
2017 * are issued each time this function is called. When the
2018 * transfer completes, the extra requests for the channel will
2019 * be flushed.
2020 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002021 u32 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002022
2023 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
2024 hcchar |= HCCHAR_CHENA;
2025 hcchar &= ~HCCHAR_CHDIS;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002026 if (dbg_hc(chan))
2027 dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
2028 hcchar);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002029 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002030 chan->requests++;
2031 return 1;
2032 }
2033
2034 /* OUT transfers */
2035
2036 if (chan->xfer_count < chan->xfer_len) {
2037 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2038 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002039 u32 hcchar = dwc2_readl(hsotg->regs +
2040 HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002041
2042 dwc2_hc_set_even_odd_frame(hsotg, chan,
2043 &hcchar);
2044 }
2045
2046 /* Load OUT packet into the appropriate Tx FIFO */
2047 dwc2_hc_write_packet(hsotg, chan);
2048 chan->requests++;
2049 return 1;
2050 }
2051
2052 return 0;
2053}
2054
2055/**
2056 * dwc2_hc_do_ping() - Starts a PING transfer
2057 *
2058 * @hsotg: Programming view of DWC_otg controller
2059 * @chan: Information needed to initialize the host channel
2060 *
2061 * This function should only be called in Slave mode. The Do Ping bit is set in
2062 * the HCTSIZ register, then the channel is enabled.
2063 */
2064void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
2065{
2066 u32 hcchar;
2067 u32 hctsiz;
2068
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002069 if (dbg_hc(chan))
2070 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
2071 chan->hc_num);
2072
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002073
2074 hctsiz = TSIZ_DOPNG;
2075 hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002076 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002077
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002078 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002079 hcchar |= HCCHAR_CHENA;
2080 hcchar &= ~HCCHAR_CHDIS;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002081 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002082}
2083
2084/**
2085 * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
2086 * the HFIR register according to PHY type and speed
2087 *
2088 * @hsotg: Programming view of DWC_otg controller
2089 *
2090 * NOTE: The caller can modify the value of the HFIR register only after the
2091 * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
2092 * has been set
2093 */
2094u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
2095{
2096 u32 usbcfg;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002097 u32 hprt0;
2098 int clock = 60; /* default value */
2099
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002100 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2101 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002102
2103 if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
2104 !(usbcfg & GUSBCFG_PHYIF16))
2105 clock = 60;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002106 if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002107 GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
2108 clock = 48;
2109 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2110 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
2111 clock = 30;
2112 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2113 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
2114 clock = 60;
2115 if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2116 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
2117 clock = 48;
2118 if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002119 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002120 clock = 48;
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02002121 if ((usbcfg & GUSBCFG_PHYSEL) &&
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002122 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002123 clock = 48;
2124
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02002125 if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002126 /* High speed case */
2127 return 125 * clock;
2128 else
2129 /* FS/LS case */
2130 return 1000 * clock;
2131}
2132
2133/**
2134 * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
2135 * buffer
2136 *
2137 * @core_if: Programming view of DWC_otg controller
2138 * @dest: Destination buffer for the packet
2139 * @bytes: Number of bytes to copy to the destination
2140 */
2141void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
2142{
2143 u32 __iomem *fifo = hsotg->regs + HCFIFO(0);
2144 u32 *data_buf = (u32 *)dest;
2145 int word_count = (bytes + 3) / 4;
2146 int i;
2147
2148 /*
2149 * Todo: Account for the case where dest is not dword aligned. This
2150 * requires reading data from the FIFO into a u32 temp buffer, then
2151 * moving it into the data buffer.
2152 */
2153
2154 dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
2155
2156 for (i = 0; i < word_count; i++, data_buf++)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002157 *data_buf = dwc2_readl(fifo);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002158}
2159
2160/**
2161 * dwc2_dump_host_registers() - Prints the host registers
2162 *
2163 * @hsotg: Programming view of DWC_otg controller
2164 *
2165 * NOTE: This function will be removed once the peripheral controller code
2166 * is integrated and the driver is stable
2167 */
2168void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
2169{
2170#ifdef DEBUG
2171 u32 __iomem *addr;
2172 int i;
2173
2174 dev_dbg(hsotg->dev, "Host Global Registers\n");
2175 addr = hsotg->regs + HCFG;
2176 dev_dbg(hsotg->dev, "HCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002177 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002178 addr = hsotg->regs + HFIR;
2179 dev_dbg(hsotg->dev, "HFIR @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002180 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002181 addr = hsotg->regs + HFNUM;
2182 dev_dbg(hsotg->dev, "HFNUM @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002183 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002184 addr = hsotg->regs + HPTXSTS;
2185 dev_dbg(hsotg->dev, "HPTXSTS @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002186 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002187 addr = hsotg->regs + HAINT;
2188 dev_dbg(hsotg->dev, "HAINT @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002189 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002190 addr = hsotg->regs + HAINTMSK;
2191 dev_dbg(hsotg->dev, "HAINTMSK @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002192 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002193 if (hsotg->core_params->dma_desc_enable > 0) {
2194 addr = hsotg->regs + HFLBADDR;
2195 dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002196 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002197 }
2198
2199 addr = hsotg->regs + HPRT0;
2200 dev_dbg(hsotg->dev, "HPRT0 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002201 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002202
2203 for (i = 0; i < hsotg->core_params->host_channels; i++) {
2204 dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
2205 addr = hsotg->regs + HCCHAR(i);
2206 dev_dbg(hsotg->dev, "HCCHAR @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002207 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002208 addr = hsotg->regs + HCSPLT(i);
2209 dev_dbg(hsotg->dev, "HCSPLT @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002210 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002211 addr = hsotg->regs + HCINT(i);
2212 dev_dbg(hsotg->dev, "HCINT @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002213 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002214 addr = hsotg->regs + HCINTMSK(i);
2215 dev_dbg(hsotg->dev, "HCINTMSK @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002216 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002217 addr = hsotg->regs + HCTSIZ(i);
2218 dev_dbg(hsotg->dev, "HCTSIZ @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002219 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002220 addr = hsotg->regs + HCDMA(i);
2221 dev_dbg(hsotg->dev, "HCDMA @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002222 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002223 if (hsotg->core_params->dma_desc_enable > 0) {
2224 addr = hsotg->regs + HCDMAB(i);
2225 dev_dbg(hsotg->dev, "HCDMAB @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002226 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002227 }
2228 }
2229#endif
2230}
2231
2232/**
2233 * dwc2_dump_global_registers() - Prints the core global registers
2234 *
2235 * @hsotg: Programming view of DWC_otg controller
2236 *
2237 * NOTE: This function will be removed once the peripheral controller code
2238 * is integrated and the driver is stable
2239 */
2240void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
2241{
2242#ifdef DEBUG
2243 u32 __iomem *addr;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002244
2245 dev_dbg(hsotg->dev, "Core Global Registers\n");
2246 addr = hsotg->regs + GOTGCTL;
2247 dev_dbg(hsotg->dev, "GOTGCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002248 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002249 addr = hsotg->regs + GOTGINT;
2250 dev_dbg(hsotg->dev, "GOTGINT @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002251 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002252 addr = hsotg->regs + GAHBCFG;
2253 dev_dbg(hsotg->dev, "GAHBCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002254 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002255 addr = hsotg->regs + GUSBCFG;
2256 dev_dbg(hsotg->dev, "GUSBCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002257 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002258 addr = hsotg->regs + GRSTCTL;
2259 dev_dbg(hsotg->dev, "GRSTCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002260 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002261 addr = hsotg->regs + GINTSTS;
2262 dev_dbg(hsotg->dev, "GINTSTS @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002263 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002264 addr = hsotg->regs + GINTMSK;
2265 dev_dbg(hsotg->dev, "GINTMSK @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002266 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002267 addr = hsotg->regs + GRXSTSR;
2268 dev_dbg(hsotg->dev, "GRXSTSR @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002269 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002270 addr = hsotg->regs + GRXFSIZ;
2271 dev_dbg(hsotg->dev, "GRXFSIZ @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002272 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002273 addr = hsotg->regs + GNPTXFSIZ;
2274 dev_dbg(hsotg->dev, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002275 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002276 addr = hsotg->regs + GNPTXSTS;
2277 dev_dbg(hsotg->dev, "GNPTXSTS @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002278 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002279 addr = hsotg->regs + GI2CCTL;
2280 dev_dbg(hsotg->dev, "GI2CCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002281 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002282 addr = hsotg->regs + GPVNDCTL;
2283 dev_dbg(hsotg->dev, "GPVNDCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002284 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002285 addr = hsotg->regs + GGPIO;
2286 dev_dbg(hsotg->dev, "GGPIO @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002287 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002288 addr = hsotg->regs + GUID;
2289 dev_dbg(hsotg->dev, "GUID @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002290 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002291 addr = hsotg->regs + GSNPSID;
2292 dev_dbg(hsotg->dev, "GSNPSID @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002293 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002294 addr = hsotg->regs + GHWCFG1;
2295 dev_dbg(hsotg->dev, "GHWCFG1 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002296 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002297 addr = hsotg->regs + GHWCFG2;
2298 dev_dbg(hsotg->dev, "GHWCFG2 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002299 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002300 addr = hsotg->regs + GHWCFG3;
2301 dev_dbg(hsotg->dev, "GHWCFG3 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002302 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002303 addr = hsotg->regs + GHWCFG4;
2304 dev_dbg(hsotg->dev, "GHWCFG4 @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002305 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002306 addr = hsotg->regs + GLPMCFG;
2307 dev_dbg(hsotg->dev, "GLPMCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002308 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002309 addr = hsotg->regs + GPWRDN;
2310 dev_dbg(hsotg->dev, "GPWRDN @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002311 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002312 addr = hsotg->regs + GDFIFOCFG;
2313 dev_dbg(hsotg->dev, "GDFIFOCFG @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002314 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002315 addr = hsotg->regs + HPTXFSIZ;
2316 dev_dbg(hsotg->dev, "HPTXFSIZ @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002317 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002318
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002319 addr = hsotg->regs + PCGCTL;
2320 dev_dbg(hsotg->dev, "PCGCTL @0x%08lX : 0x%08X\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002321 (unsigned long)addr, dwc2_readl(addr));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002322#endif
2323}
2324
2325/**
2326 * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
2327 *
2328 * @hsotg: Programming view of DWC_otg controller
2329 * @num: Tx FIFO to flush
2330 */
2331void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
2332{
2333 u32 greset;
2334 int count = 0;
2335
2336 dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
2337
2338 greset = GRSTCTL_TXFFLSH;
2339 greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002340 dwc2_writel(greset, hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002341
2342 do {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002343 greset = dwc2_readl(hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002344 if (++count > 10000) {
2345 dev_warn(hsotg->dev,
2346 "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
2347 __func__, greset,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002348 dwc2_readl(hsotg->regs + GNPTXSTS));
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002349 break;
2350 }
2351 udelay(1);
2352 } while (greset & GRSTCTL_TXFFLSH);
2353
2354 /* Wait for at least 3 PHY Clocks */
2355 udelay(1);
2356}
2357
2358/**
2359 * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
2360 *
2361 * @hsotg: Programming view of DWC_otg controller
2362 */
2363void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
2364{
2365 u32 greset;
2366 int count = 0;
2367
2368 dev_vdbg(hsotg->dev, "%s()\n", __func__);
2369
2370 greset = GRSTCTL_RXFFLSH;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002371 dwc2_writel(greset, hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002372
2373 do {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002374 greset = dwc2_readl(hsotg->regs + GRSTCTL);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002375 if (++count > 10000) {
2376 dev_warn(hsotg->dev, "%s() HANG! GRSTCTL=%0x\n",
2377 __func__, greset);
2378 break;
2379 }
2380 udelay(1);
2381 } while (greset & GRSTCTL_RXFFLSH);
2382
2383 /* Wait for at least 3 PHY Clocks */
2384 udelay(1);
2385}
2386
Paul Zimmerman498f0662013-11-22 16:43:47 -08002387#define DWC2_OUT_OF_BOUNDS(a, b, c) ((a) < (b) || (a) > (c))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002388
2389/* Parameter access functions */
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002390void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002391{
2392 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002393
2394 switch (val) {
2395 case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002396 if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002397 valid = 0;
2398 break;
2399 case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002400 switch (hsotg->hw_params.op_mode) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002401 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
2402 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
2403 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
2404 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
2405 break;
2406 default:
2407 valid = 0;
2408 break;
2409 }
2410 break;
2411 case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
2412 /* always valid */
2413 break;
2414 default:
2415 valid = 0;
2416 break;
2417 }
2418
2419 if (!valid) {
2420 if (val >= 0)
2421 dev_err(hsotg->dev,
2422 "%d invalid for otg_cap parameter. Check HW configuration.\n",
2423 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002424 switch (hsotg->hw_params.op_mode) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002425 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
2426 val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
2427 break;
2428 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
2429 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
2430 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
2431 val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
2432 break;
2433 default:
2434 val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
2435 break;
2436 }
2437 dev_dbg(hsotg->dev, "Setting otg_cap to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002438 }
2439
2440 hsotg->core_params->otg_cap = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002441}
2442
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002443void dwc2_set_param_dma_enable(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002444{
2445 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002446
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002447 if (val > 0 && hsotg->hw_params.arch == GHWCFG2_SLAVE_ONLY_ARCH)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002448 valid = 0;
2449 if (val < 0)
2450 valid = 0;
2451
2452 if (!valid) {
2453 if (val >= 0)
2454 dev_err(hsotg->dev,
2455 "%d invalid for dma_enable parameter. Check HW configuration.\n",
2456 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002457 val = hsotg->hw_params.arch != GHWCFG2_SLAVE_ONLY_ARCH;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002458 dev_dbg(hsotg->dev, "Setting dma_enable to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002459 }
2460
2461 hsotg->core_params->dma_enable = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002462}
2463
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002464void dwc2_set_param_dma_desc_enable(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002465{
2466 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002467
2468 if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002469 !hsotg->hw_params.dma_desc_enable))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002470 valid = 0;
2471 if (val < 0)
2472 valid = 0;
2473
2474 if (!valid) {
2475 if (val >= 0)
2476 dev_err(hsotg->dev,
2477 "%d invalid for dma_desc_enable parameter. Check HW configuration.\n",
2478 val);
2479 val = (hsotg->core_params->dma_enable > 0 &&
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002480 hsotg->hw_params.dma_desc_enable);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002481 dev_dbg(hsotg->dev, "Setting dma_desc_enable to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002482 }
2483
2484 hsotg->core_params->dma_desc_enable = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002485}
2486
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +01002487void dwc2_set_param_dma_desc_fs_enable(struct dwc2_hsotg *hsotg, int val)
2488{
2489 int valid = 1;
2490
2491 if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
2492 !hsotg->hw_params.dma_desc_enable))
2493 valid = 0;
2494 if (val < 0)
2495 valid = 0;
2496
2497 if (!valid) {
2498 if (val >= 0)
2499 dev_err(hsotg->dev,
2500 "%d invalid for dma_desc_fs_enable parameter. Check HW configuration.\n",
2501 val);
2502 val = (hsotg->core_params->dma_enable > 0 &&
2503 hsotg->hw_params.dma_desc_enable);
2504 }
2505
2506 hsotg->core_params->dma_desc_fs_enable = val;
2507 dev_dbg(hsotg->dev, "Setting dma_desc_fs_enable to %d\n", val);
2508}
2509
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002510void dwc2_set_param_host_support_fs_ls_low_power(struct dwc2_hsotg *hsotg,
2511 int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002512{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002513 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002514 if (val >= 0) {
2515 dev_err(hsotg->dev,
2516 "Wrong value for host_support_fs_low_power\n");
2517 dev_err(hsotg->dev,
2518 "host_support_fs_low_power must be 0 or 1\n");
2519 }
2520 val = 0;
2521 dev_dbg(hsotg->dev,
2522 "Setting host_support_fs_low_power to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002523 }
2524
2525 hsotg->core_params->host_support_fs_ls_low_power = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002526}
2527
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002528void dwc2_set_param_enable_dynamic_fifo(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 > 0 && !hsotg->hw_params.enable_dynamic_fifo)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002533 valid = 0;
2534 if (val < 0)
2535 valid = 0;
2536
2537 if (!valid) {
2538 if (val >= 0)
2539 dev_err(hsotg->dev,
2540 "%d invalid for enable_dynamic_fifo parameter. Check HW configuration.\n",
2541 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002542 val = hsotg->hw_params.enable_dynamic_fifo;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002543 dev_dbg(hsotg->dev, "Setting enable_dynamic_fifo to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002544 }
2545
2546 hsotg->core_params->enable_dynamic_fifo = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002547}
2548
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002549void dwc2_set_param_host_rx_fifo_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002550{
2551 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002552
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002553 if (val < 16 || val > hsotg->hw_params.host_rx_fifo_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002554 valid = 0;
2555
2556 if (!valid) {
2557 if (val >= 0)
2558 dev_err(hsotg->dev,
2559 "%d invalid for host_rx_fifo_size. Check HW configuration.\n",
2560 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002561 val = hsotg->hw_params.host_rx_fifo_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002562 dev_dbg(hsotg->dev, "Setting host_rx_fifo_size to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002563 }
2564
2565 hsotg->core_params->host_rx_fifo_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002566}
2567
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002568void dwc2_set_param_host_nperio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002569{
2570 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002571
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002572 if (val < 16 || val > hsotg->hw_params.host_nperio_tx_fifo_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002573 valid = 0;
2574
2575 if (!valid) {
2576 if (val >= 0)
2577 dev_err(hsotg->dev,
2578 "%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
2579 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002580 val = hsotg->hw_params.host_nperio_tx_fifo_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002581 dev_dbg(hsotg->dev, "Setting host_nperio_tx_fifo_size to %d\n",
2582 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002583 }
2584
2585 hsotg->core_params->host_nperio_tx_fifo_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002586}
2587
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002588void dwc2_set_param_host_perio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002589{
2590 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002591
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002592 if (val < 16 || val > hsotg->hw_params.host_perio_tx_fifo_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002593 valid = 0;
2594
2595 if (!valid) {
2596 if (val >= 0)
2597 dev_err(hsotg->dev,
2598 "%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
2599 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002600 val = hsotg->hw_params.host_perio_tx_fifo_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002601 dev_dbg(hsotg->dev, "Setting host_perio_tx_fifo_size to %d\n",
2602 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002603 }
2604
2605 hsotg->core_params->host_perio_tx_fifo_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002606}
2607
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002608void dwc2_set_param_max_transfer_size(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002609{
2610 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002611
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002612 if (val < 2047 || val > hsotg->hw_params.max_transfer_size)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002613 valid = 0;
2614
2615 if (!valid) {
2616 if (val >= 0)
2617 dev_err(hsotg->dev,
2618 "%d invalid for max_transfer_size. Check HW configuration.\n",
2619 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002620 val = hsotg->hw_params.max_transfer_size;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002621 dev_dbg(hsotg->dev, "Setting max_transfer_size to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002622 }
2623
2624 hsotg->core_params->max_transfer_size = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002625}
2626
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002627void dwc2_set_param_max_packet_count(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002628{
2629 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002630
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002631 if (val < 15 || val > hsotg->hw_params.max_packet_count)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002632 valid = 0;
2633
2634 if (!valid) {
2635 if (val >= 0)
2636 dev_err(hsotg->dev,
2637 "%d invalid for max_packet_count. Check HW configuration.\n",
2638 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002639 val = hsotg->hw_params.max_packet_count;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002640 dev_dbg(hsotg->dev, "Setting max_packet_count to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002641 }
2642
2643 hsotg->core_params->max_packet_count = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002644}
2645
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002646void dwc2_set_param_host_channels(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002647{
2648 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002649
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002650 if (val < 1 || val > hsotg->hw_params.host_channels)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002651 valid = 0;
2652
2653 if (!valid) {
2654 if (val >= 0)
2655 dev_err(hsotg->dev,
2656 "%d invalid for host_channels. Check HW configuration.\n",
2657 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002658 val = hsotg->hw_params.host_channels;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002659 dev_dbg(hsotg->dev, "Setting host_channels to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002660 }
2661
2662 hsotg->core_params->host_channels = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002663}
2664
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002665void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002666{
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002667 int valid = 0;
Luis Ortega Perez de Villar0464a3d2013-09-25 13:10:50 +02002668 u32 hs_phy_type, fs_phy_type;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002669
Paul Zimmerman498f0662013-11-22 16:43:47 -08002670 if (DWC2_OUT_OF_BOUNDS(val, DWC2_PHY_TYPE_PARAM_FS,
2671 DWC2_PHY_TYPE_PARAM_ULPI)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002672 if (val >= 0) {
2673 dev_err(hsotg->dev, "Wrong value for phy_type\n");
2674 dev_err(hsotg->dev, "phy_type must be 0, 1 or 2\n");
2675 }
2676
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002677 valid = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002678 }
2679
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002680 hs_phy_type = hsotg->hw_params.hs_phy_type;
2681 fs_phy_type = hsotg->hw_params.fs_phy_type;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002682 if (val == DWC2_PHY_TYPE_PARAM_UTMI &&
2683 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
2684 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
2685 valid = 1;
2686 else if (val == DWC2_PHY_TYPE_PARAM_ULPI &&
2687 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI ||
2688 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
2689 valid = 1;
2690 else if (val == DWC2_PHY_TYPE_PARAM_FS &&
2691 fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
2692 valid = 1;
2693
2694 if (!valid) {
2695 if (val >= 0)
2696 dev_err(hsotg->dev,
2697 "%d invalid for phy_type. Check HW configuration.\n",
2698 val);
Matthijs Kooijman929aea02013-04-29 19:36:48 +00002699 val = DWC2_PHY_TYPE_PARAM_FS;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002700 if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
2701 if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
2702 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
2703 val = DWC2_PHY_TYPE_PARAM_UTMI;
2704 else
2705 val = DWC2_PHY_TYPE_PARAM_ULPI;
2706 }
2707 dev_dbg(hsotg->dev, "Setting phy_type to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002708 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002709
2710 hsotg->core_params->phy_type = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002711}
2712
2713static int dwc2_get_param_phy_type(struct dwc2_hsotg *hsotg)
2714{
2715 return hsotg->core_params->phy_type;
2716}
2717
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002718void dwc2_set_param_speed(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002719{
2720 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002721
Paul Zimmerman498f0662013-11-22 16:43:47 -08002722 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002723 if (val >= 0) {
2724 dev_err(hsotg->dev, "Wrong value for speed parameter\n");
2725 dev_err(hsotg->dev, "max_speed parameter must be 0 or 1\n");
2726 }
2727 valid = 0;
2728 }
2729
Matthijs Kooijman929aea02013-04-29 19:36:48 +00002730 if (val == DWC2_SPEED_PARAM_HIGH &&
2731 dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002732 valid = 0;
2733
2734 if (!valid) {
2735 if (val >= 0)
2736 dev_err(hsotg->dev,
2737 "%d invalid for speed parameter. Check HW configuration.\n",
2738 val);
2739 val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS ?
Matthijs Kooijman929aea02013-04-29 19:36:48 +00002740 DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002741 dev_dbg(hsotg->dev, "Setting speed to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002742 }
2743
2744 hsotg->core_params->speed = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002745}
2746
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002747void dwc2_set_param_host_ls_low_power_phy_clk(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002748{
2749 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002750
Paul Zimmerman498f0662013-11-22 16:43:47 -08002751 if (DWC2_OUT_OF_BOUNDS(val, DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ,
2752 DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002753 if (val >= 0) {
2754 dev_err(hsotg->dev,
2755 "Wrong value for host_ls_low_power_phy_clk parameter\n");
2756 dev_err(hsotg->dev,
2757 "host_ls_low_power_phy_clk must be 0 or 1\n");
2758 }
2759 valid = 0;
2760 }
2761
2762 if (val == DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ &&
2763 dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
2764 valid = 0;
2765
2766 if (!valid) {
2767 if (val >= 0)
2768 dev_err(hsotg->dev,
2769 "%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
2770 val);
2771 val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS
2772 ? DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ
2773 : DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
2774 dev_dbg(hsotg->dev, "Setting host_ls_low_power_phy_clk to %d\n",
2775 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002776 }
2777
2778 hsotg->core_params->host_ls_low_power_phy_clk = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002779}
2780
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002781void dwc2_set_param_phy_ulpi_ddr(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002782{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002783 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002784 if (val >= 0) {
2785 dev_err(hsotg->dev, "Wrong value for phy_ulpi_ddr\n");
2786 dev_err(hsotg->dev, "phy_upli_ddr must be 0 or 1\n");
2787 }
2788 val = 0;
2789 dev_dbg(hsotg->dev, "Setting phy_upli_ddr to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002790 }
2791
2792 hsotg->core_params->phy_ulpi_ddr = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002793}
2794
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002795void dwc2_set_param_phy_ulpi_ext_vbus(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002796{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002797 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002798 if (val >= 0) {
2799 dev_err(hsotg->dev,
2800 "Wrong value for phy_ulpi_ext_vbus\n");
2801 dev_err(hsotg->dev,
2802 "phy_ulpi_ext_vbus must be 0 or 1\n");
2803 }
2804 val = 0;
2805 dev_dbg(hsotg->dev, "Setting phy_ulpi_ext_vbus to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002806 }
2807
2808 hsotg->core_params->phy_ulpi_ext_vbus = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002809}
2810
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002811void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002812{
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02002813 int valid = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002814
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02002815 switch (hsotg->hw_params.utmi_phy_data_width) {
2816 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
2817 valid = (val == 8);
2818 break;
2819 case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
2820 valid = (val == 16);
2821 break;
2822 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
2823 valid = (val == 8 || val == 16);
2824 break;
2825 }
2826
2827 if (!valid) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002828 if (val >= 0) {
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02002829 dev_err(hsotg->dev,
2830 "%d invalid for phy_utmi_width. Check HW configuration.\n",
2831 val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002832 }
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02002833 val = (hsotg->hw_params.utmi_phy_data_width ==
2834 GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002835 dev_dbg(hsotg->dev, "Setting phy_utmi_width to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002836 }
2837
2838 hsotg->core_params->phy_utmi_width = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002839}
2840
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002841void dwc2_set_param_ulpi_fs_ls(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002842{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002843 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002844 if (val >= 0) {
2845 dev_err(hsotg->dev, "Wrong value for ulpi_fs_ls\n");
2846 dev_err(hsotg->dev, "ulpi_fs_ls must be 0 or 1\n");
2847 }
2848 val = 0;
2849 dev_dbg(hsotg->dev, "Setting ulpi_fs_ls to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002850 }
2851
2852 hsotg->core_params->ulpi_fs_ls = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002853}
2854
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002855void dwc2_set_param_ts_dline(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002856{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002857 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002858 if (val >= 0) {
2859 dev_err(hsotg->dev, "Wrong value for ts_dline\n");
2860 dev_err(hsotg->dev, "ts_dline must be 0 or 1\n");
2861 }
2862 val = 0;
2863 dev_dbg(hsotg->dev, "Setting ts_dline to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002864 }
2865
2866 hsotg->core_params->ts_dline = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002867}
2868
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002869void dwc2_set_param_i2c_enable(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002870{
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002871 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002872
Paul Zimmerman498f0662013-11-22 16:43:47 -08002873 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002874 if (val >= 0) {
2875 dev_err(hsotg->dev, "Wrong value for i2c_enable\n");
2876 dev_err(hsotg->dev, "i2c_enable must be 0 or 1\n");
2877 }
2878
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002879 valid = 0;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002880 }
2881
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002882 if (val == 1 && !(hsotg->hw_params.i2c_enable))
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002883 valid = 0;
2884
2885 if (!valid) {
2886 if (val >= 0)
2887 dev_err(hsotg->dev,
2888 "%d invalid for i2c_enable. Check HW configuration.\n",
2889 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002890 val = hsotg->hw_params.i2c_enable;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002891 dev_dbg(hsotg->dev, "Setting i2c_enable to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002892 }
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002893
2894 hsotg->core_params->i2c_enable = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002895}
2896
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002897void dwc2_set_param_en_multiple_tx_fifo(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002898{
2899 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002900
Paul Zimmerman498f0662013-11-22 16:43:47 -08002901 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002902 if (val >= 0) {
2903 dev_err(hsotg->dev,
2904 "Wrong value for en_multiple_tx_fifo,\n");
2905 dev_err(hsotg->dev,
2906 "en_multiple_tx_fifo must be 0 or 1\n");
2907 }
2908 valid = 0;
2909 }
2910
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002911 if (val == 1 && !hsotg->hw_params.en_multiple_tx_fifo)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002912 valid = 0;
2913
2914 if (!valid) {
2915 if (val >= 0)
2916 dev_err(hsotg->dev,
2917 "%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
2918 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002919 val = hsotg->hw_params.en_multiple_tx_fifo;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002920 dev_dbg(hsotg->dev, "Setting en_multiple_tx_fifo to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002921 }
2922
2923 hsotg->core_params->en_multiple_tx_fifo = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002924}
2925
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002926void dwc2_set_param_reload_ctl(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002927{
2928 int valid = 1;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002929
Paul Zimmerman498f0662013-11-22 16:43:47 -08002930 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002931 if (val >= 0) {
2932 dev_err(hsotg->dev,
2933 "'%d' invalid for parameter reload_ctl\n", val);
2934 dev_err(hsotg->dev, "reload_ctl must be 0 or 1\n");
2935 }
2936 valid = 0;
2937 }
2938
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002939 if (val == 1 && hsotg->hw_params.snpsid < DWC2_CORE_REV_2_92a)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002940 valid = 0;
2941
2942 if (!valid) {
2943 if (val >= 0)
2944 dev_err(hsotg->dev,
2945 "%d invalid for parameter reload_ctl. Check HW configuration.\n",
2946 val);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02002947 val = hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_92a;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002948 dev_dbg(hsotg->dev, "Setting reload_ctl to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002949 }
2950
2951 hsotg->core_params->reload_ctl = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002952}
2953
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002954void dwc2_set_param_ahbcfg(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002955{
Paul Zimmerman4d3190e2013-07-16 12:22:12 -07002956 if (val != -1)
2957 hsotg->core_params->ahbcfg = val;
2958 else
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02002959 hsotg->core_params->ahbcfg = GAHBCFG_HBSTLEN_INCR4 <<
Luis Ortega Perez de Villar0464a3d2013-09-25 13:10:50 +02002960 GAHBCFG_HBSTLEN_SHIFT;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002961}
2962
Paul Zimmerman7218dae2013-11-22 16:43:48 -08002963void dwc2_set_param_otg_ver(struct dwc2_hsotg *hsotg, int val)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002964{
Paul Zimmerman498f0662013-11-22 16:43:47 -08002965 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002966 if (val >= 0) {
2967 dev_err(hsotg->dev,
2968 "'%d' invalid for parameter otg_ver\n", val);
2969 dev_err(hsotg->dev,
2970 "otg_ver must be 0 (for OTG 1.3 support) or 1 (for OTG 2.0 support)\n");
2971 }
2972 val = 0;
2973 dev_dbg(hsotg->dev, "Setting otg_ver to %d\n", val);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002974 }
2975
2976 hsotg->core_params->otg_ver = val;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07002977}
2978
Wei Yongjun49cf10c2013-11-28 10:27:59 +08002979static void dwc2_set_param_uframe_sched(struct dwc2_hsotg *hsotg, int val)
Paul Zimmermane8576e62013-11-25 13:42:47 -08002980{
2981 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2982 if (val >= 0) {
2983 dev_err(hsotg->dev,
2984 "'%d' invalid for parameter uframe_sched\n",
2985 val);
2986 dev_err(hsotg->dev, "uframe_sched must be 0 or 1\n");
2987 }
2988 val = 1;
2989 dev_dbg(hsotg->dev, "Setting uframe_sched to %d\n", val);
2990 }
2991
2992 hsotg->core_params->uframe_sched = val;
2993}
2994
Gregory Herreroa6d249d2015-04-29 22:09:04 +02002995static void dwc2_set_param_external_id_pin_ctl(struct dwc2_hsotg *hsotg,
2996 int val)
2997{
2998 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2999 if (val >= 0) {
3000 dev_err(hsotg->dev,
3001 "'%d' invalid for parameter external_id_pin_ctl\n",
3002 val);
3003 dev_err(hsotg->dev, "external_id_pin_ctl must be 0 or 1\n");
3004 }
3005 val = 0;
3006 dev_dbg(hsotg->dev, "Setting external_id_pin_ctl to %d\n", val);
3007 }
3008
3009 hsotg->core_params->external_id_pin_ctl = val;
3010}
3011
Gregory Herrero285046a2015-04-29 22:09:19 +02003012static void dwc2_set_param_hibernation(struct dwc2_hsotg *hsotg,
3013 int val)
3014{
3015 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
3016 if (val >= 0) {
3017 dev_err(hsotg->dev,
3018 "'%d' invalid for parameter hibernation\n",
3019 val);
3020 dev_err(hsotg->dev, "hibernation must be 0 or 1\n");
3021 }
3022 val = 0;
3023 dev_dbg(hsotg->dev, "Setting hibernation to %d\n", val);
3024 }
3025
3026 hsotg->core_params->hibernation = val;
3027}
3028
Paul Zimmermane8576e62013-11-25 13:42:47 -08003029/*
3030 * This function is called during module intialization to pass module parameters
3031 * for the DWC_otg core.
3032 */
3033void dwc2_set_parameters(struct dwc2_hsotg *hsotg,
3034 const struct dwc2_core_params *params)
3035{
3036 dev_dbg(hsotg->dev, "%s()\n", __func__);
3037
3038 dwc2_set_param_otg_cap(hsotg, params->otg_cap);
3039 dwc2_set_param_dma_enable(hsotg, params->dma_enable);
3040 dwc2_set_param_dma_desc_enable(hsotg, params->dma_desc_enable);
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +01003041 dwc2_set_param_dma_desc_fs_enable(hsotg, params->dma_desc_fs_enable);
Paul Zimmermane8576e62013-11-25 13:42:47 -08003042 dwc2_set_param_host_support_fs_ls_low_power(hsotg,
3043 params->host_support_fs_ls_low_power);
3044 dwc2_set_param_enable_dynamic_fifo(hsotg,
3045 params->enable_dynamic_fifo);
3046 dwc2_set_param_host_rx_fifo_size(hsotg,
3047 params->host_rx_fifo_size);
3048 dwc2_set_param_host_nperio_tx_fifo_size(hsotg,
3049 params->host_nperio_tx_fifo_size);
3050 dwc2_set_param_host_perio_tx_fifo_size(hsotg,
3051 params->host_perio_tx_fifo_size);
3052 dwc2_set_param_max_transfer_size(hsotg,
3053 params->max_transfer_size);
3054 dwc2_set_param_max_packet_count(hsotg,
3055 params->max_packet_count);
3056 dwc2_set_param_host_channels(hsotg, params->host_channels);
3057 dwc2_set_param_phy_type(hsotg, params->phy_type);
3058 dwc2_set_param_speed(hsotg, params->speed);
3059 dwc2_set_param_host_ls_low_power_phy_clk(hsotg,
3060 params->host_ls_low_power_phy_clk);
3061 dwc2_set_param_phy_ulpi_ddr(hsotg, params->phy_ulpi_ddr);
3062 dwc2_set_param_phy_ulpi_ext_vbus(hsotg,
3063 params->phy_ulpi_ext_vbus);
3064 dwc2_set_param_phy_utmi_width(hsotg, params->phy_utmi_width);
3065 dwc2_set_param_ulpi_fs_ls(hsotg, params->ulpi_fs_ls);
3066 dwc2_set_param_ts_dline(hsotg, params->ts_dline);
3067 dwc2_set_param_i2c_enable(hsotg, params->i2c_enable);
3068 dwc2_set_param_en_multiple_tx_fifo(hsotg,
3069 params->en_multiple_tx_fifo);
3070 dwc2_set_param_reload_ctl(hsotg, params->reload_ctl);
3071 dwc2_set_param_ahbcfg(hsotg, params->ahbcfg);
3072 dwc2_set_param_otg_ver(hsotg, params->otg_ver);
3073 dwc2_set_param_uframe_sched(hsotg, params->uframe_sched);
Gregory Herreroa6d249d2015-04-29 22:09:04 +02003074 dwc2_set_param_external_id_pin_ctl(hsotg, params->external_id_pin_ctl);
Gregory Herrero285046a2015-04-29 22:09:19 +02003075 dwc2_set_param_hibernation(hsotg, params->hibernation);
Paul Zimmermane8576e62013-11-25 13:42:47 -08003076}
3077
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003078/**
3079 * During device initialization, read various hardware configuration
3080 * registers and interpret the contents.
3081 */
3082int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
3083{
3084 struct dwc2_hw_params *hw = &hsotg->hw_params;
3085 unsigned width;
3086 u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
3087 u32 hptxfsiz, grxfsiz, gnptxfsiz;
3088 u32 gusbcfg;
3089
3090 /*
3091 * Attempt to ensure this device is really a DWC_otg Controller.
3092 * Read and verify the GSNPSID register contents. The value should be
3093 * 0x45f42xxx or 0x45f43xxx, which corresponds to either "OT2" or "OT3",
3094 * as in "OTG version 2.xx" or "OTG version 3.xx".
3095 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003096 hw->snpsid = dwc2_readl(hsotg->regs + GSNPSID);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003097 if ((hw->snpsid & 0xfffff000) != 0x4f542000 &&
3098 (hw->snpsid & 0xfffff000) != 0x4f543000) {
3099 dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
3100 hw->snpsid);
3101 return -ENODEV;
3102 }
3103
3104 dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
3105 hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
3106 hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
3107
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003108 hwcfg1 = dwc2_readl(hsotg->regs + GHWCFG1);
3109 hwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
3110 hwcfg3 = dwc2_readl(hsotg->regs + GHWCFG3);
3111 hwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
3112 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003113
3114 dev_dbg(hsotg->dev, "hwcfg1=%08x\n", hwcfg1);
3115 dev_dbg(hsotg->dev, "hwcfg2=%08x\n", hwcfg2);
3116 dev_dbg(hsotg->dev, "hwcfg3=%08x\n", hwcfg3);
3117 dev_dbg(hsotg->dev, "hwcfg4=%08x\n", hwcfg4);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003118 dev_dbg(hsotg->dev, "grxfsiz=%08x\n", grxfsiz);
3119
Doug Anderson2867c052014-08-07 12:48:11 -07003120 /* Force host mode to get HPTXFSIZ / GNPTXFSIZ exact power on value */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003121 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003122 gusbcfg |= GUSBCFG_FORCEHOSTMODE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003123 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003124 usleep_range(100000, 150000);
3125
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003126 gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
3127 hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
Doug Anderson2867c052014-08-07 12:48:11 -07003128 dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003129 dev_dbg(hsotg->dev, "hptxfsiz=%08x\n", hptxfsiz);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003130 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003131 gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003132 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003133 usleep_range(100000, 150000);
3134
3135 /* hwcfg2 */
3136 hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
3137 GHWCFG2_OP_MODE_SHIFT;
3138 hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
3139 GHWCFG2_ARCHITECTURE_SHIFT;
3140 hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
3141 hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
3142 GHWCFG2_NUM_HOST_CHAN_SHIFT);
3143 hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
3144 GHWCFG2_HS_PHY_TYPE_SHIFT;
3145 hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
3146 GHWCFG2_FS_PHY_TYPE_SHIFT;
3147 hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
3148 GHWCFG2_NUM_DEV_EP_SHIFT;
3149 hw->nperio_tx_q_depth =
3150 (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
3151 GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
3152 hw->host_perio_tx_q_depth =
3153 (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
3154 GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
3155 hw->dev_token_q_depth =
3156 (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
3157 GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
3158
3159 /* hwcfg3 */
3160 width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
3161 GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
3162 hw->max_transfer_size = (1 << (width + 11)) - 1;
Paul Zimmermane8f8c142014-09-16 13:47:26 -07003163 /*
3164 * Clip max_transfer_size to 65535. dwc2_hc_setup_align_buf() allocates
3165 * coherent buffers with this size, and if it's too large we can
3166 * exhaust the coherent DMA pool.
3167 */
3168 if (hw->max_transfer_size > 65535)
3169 hw->max_transfer_size = 65535;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003170 width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
3171 GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
3172 hw->max_packet_count = (1 << (width + 4)) - 1;
3173 hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
3174 hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
3175 GHWCFG3_DFIFO_DEPTH_SHIFT;
3176
3177 /* hwcfg4 */
3178 hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
3179 hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
3180 GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
3181 hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
3182 hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02003183 hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
3184 GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003185
3186 /* fifo sizes */
3187 hw->host_rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
3188 GRXFSIZ_DEPTH_SHIFT;
3189 hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
3190 FIFOSIZE_DEPTH_SHIFT;
3191 hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
3192 FIFOSIZE_DEPTH_SHIFT;
3193
3194 dev_dbg(hsotg->dev, "Detected values from hardware:\n");
3195 dev_dbg(hsotg->dev, " op_mode=%d\n",
3196 hw->op_mode);
3197 dev_dbg(hsotg->dev, " arch=%d\n",
3198 hw->arch);
3199 dev_dbg(hsotg->dev, " dma_desc_enable=%d\n",
3200 hw->dma_desc_enable);
3201 dev_dbg(hsotg->dev, " power_optimized=%d\n",
3202 hw->power_optimized);
3203 dev_dbg(hsotg->dev, " i2c_enable=%d\n",
3204 hw->i2c_enable);
3205 dev_dbg(hsotg->dev, " hs_phy_type=%d\n",
3206 hw->hs_phy_type);
3207 dev_dbg(hsotg->dev, " fs_phy_type=%d\n",
3208 hw->fs_phy_type);
Masanari Iida971bd8f2015-05-20 23:54:02 +09003209 dev_dbg(hsotg->dev, " utmi_phy_data_width=%d\n",
Matthijs Kooijmande4a1932013-08-30 18:45:22 +02003210 hw->utmi_phy_data_width);
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003211 dev_dbg(hsotg->dev, " num_dev_ep=%d\n",
3212 hw->num_dev_ep);
3213 dev_dbg(hsotg->dev, " num_dev_perio_in_ep=%d\n",
3214 hw->num_dev_perio_in_ep);
3215 dev_dbg(hsotg->dev, " host_channels=%d\n",
3216 hw->host_channels);
3217 dev_dbg(hsotg->dev, " max_transfer_size=%d\n",
3218 hw->max_transfer_size);
3219 dev_dbg(hsotg->dev, " max_packet_count=%d\n",
3220 hw->max_packet_count);
3221 dev_dbg(hsotg->dev, " nperio_tx_q_depth=0x%0x\n",
3222 hw->nperio_tx_q_depth);
3223 dev_dbg(hsotg->dev, " host_perio_tx_q_depth=0x%0x\n",
3224 hw->host_perio_tx_q_depth);
3225 dev_dbg(hsotg->dev, " dev_token_q_depth=0x%0x\n",
3226 hw->dev_token_q_depth);
3227 dev_dbg(hsotg->dev, " enable_dynamic_fifo=%d\n",
3228 hw->enable_dynamic_fifo);
3229 dev_dbg(hsotg->dev, " en_multiple_tx_fifo=%d\n",
3230 hw->en_multiple_tx_fifo);
3231 dev_dbg(hsotg->dev, " total_fifo_size=%d\n",
3232 hw->total_fifo_size);
3233 dev_dbg(hsotg->dev, " host_rx_fifo_size=%d\n",
3234 hw->host_rx_fifo_size);
3235 dev_dbg(hsotg->dev, " host_nperio_tx_fifo_size=%d\n",
3236 hw->host_nperio_tx_fifo_size);
3237 dev_dbg(hsotg->dev, " host_perio_tx_fifo_size=%d\n",
3238 hw->host_perio_tx_fifo_size);
3239 dev_dbg(hsotg->dev, "\n");
3240
3241 return 0;
3242}
Mian Yousaf Kaukabecb176c2015-04-29 22:09:05 +02003243
3244/*
3245 * Sets all parameters to the given value.
3246 *
3247 * Assumes that the dwc2_core_params struct contains only integers.
3248 */
3249void dwc2_set_all_params(struct dwc2_core_params *params, int value)
3250{
3251 int *p = (int *)params;
3252 size_t size = sizeof(*params) / sizeof(*p);
3253 int i;
3254
3255 for (i = 0; i < size; i++)
3256 p[i] = value;
3257}
Mian Yousaf Kaukabecb176c2015-04-29 22:09:05 +02003258
Matthijs Kooijman9badec22013-08-30 18:45:21 +02003259
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003260u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg)
3261{
Paul Zimmermanb66a3f02013-11-22 16:43:50 -08003262 return hsotg->core_params->otg_ver == 1 ? 0x0200 : 0x0103;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003263}
3264
Paul Zimmerman057715f2013-11-22 16:43:51 -08003265bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003266{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003267 if (dwc2_readl(hsotg->regs + GSNPSID) == 0xffffffff)
Paul Zimmerman057715f2013-11-22 16:43:51 -08003268 return false;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003269 else
Paul Zimmerman057715f2013-11-22 16:43:51 -08003270 return true;
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003271}
3272
3273/**
3274 * dwc2_enable_global_interrupts() - Enables the controller's Global
3275 * Interrupt in the AHB Config register
3276 *
3277 * @hsotg: Programming view of DWC_otg controller
3278 */
3279void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
3280{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003281 u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003282
3283 ahbcfg |= GAHBCFG_GLBL_INTR_EN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003284 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003285}
3286
3287/**
3288 * dwc2_disable_global_interrupts() - Disables the controller's Global
3289 * Interrupt in the AHB Config register
3290 *
3291 * @hsotg: Programming view of DWC_otg controller
3292 */
3293void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
3294{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003295 u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003296
3297 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003298 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
Paul Zimmerman56f5b1c2013-03-11 17:47:58 -07003299}
3300
3301MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
3302MODULE_AUTHOR("Synopsys, Inc.");
3303MODULE_LICENSE("Dual BSD/GPL");