blob: 85f74f211be8b792c567b7329e31462e61283532 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2/*
3 * Copyright (C) 2004-2016 Synopsys, Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The names of the above-listed copyright holders may not be used
15 * to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * ALTERNATIVELY, this software may be distributed under the terms of the
19 * GNU General Public License ("GPL") as published by the Free Software
20 * Foundation; either version 2 of the License, or (at your option) any
21 * later version.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/of_device.h>
39
40#include <soc/asr/addr-map.h>
41
42#include "core.h"
43
44static void dwc2_set_bcm_params(struct dwc2_hsotg *hsotg)
45{
46 struct dwc2_core_params *p = &hsotg->params;
47
48 p->host_rx_fifo_size = 774;
49 p->max_transfer_size = 65535;
50 p->max_packet_count = 511;
51 p->ahbcfg = 0x10;
52}
53
54static void dwc2_set_his_params(struct dwc2_hsotg *hsotg)
55{
56 struct dwc2_core_params *p = &hsotg->params;
57
58 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
59 p->speed = DWC2_SPEED_PARAM_HIGH;
60 p->host_rx_fifo_size = 512;
61 p->host_nperio_tx_fifo_size = 512;
62 p->host_perio_tx_fifo_size = 512;
63 p->max_transfer_size = 65535;
64 p->max_packet_count = 511;
65 p->host_channels = 16;
66 p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
67 p->phy_utmi_width = 8;
68 p->i2c_enable = false;
69 p->reload_ctl = false;
70 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
71 GAHBCFG_HBSTLEN_SHIFT;
72 p->change_speed_quirk = true;
73 p->power_down = false;
74}
75
76static void dwc2_set_s3c6400_params(struct dwc2_hsotg *hsotg)
77{
78 struct dwc2_core_params *p = &hsotg->params;
79
80 p->power_down = 0;
81 p->phy_utmi_width = 8;
82}
83
84static void dwc2_set_rk_params(struct dwc2_hsotg *hsotg)
85{
86 struct dwc2_core_params *p = &hsotg->params;
87
88 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
89 p->host_rx_fifo_size = 525;
90 p->host_nperio_tx_fifo_size = 128;
91 p->host_perio_tx_fifo_size = 256;
92 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
93 GAHBCFG_HBSTLEN_SHIFT;
94 p->power_down = 0;
95}
96
97static void dwc2_set_ltq_params(struct dwc2_hsotg *hsotg)
98{
99 struct dwc2_core_params *p = &hsotg->params;
100
101 p->otg_cap = 2;
102 p->host_rx_fifo_size = 288;
103 p->host_nperio_tx_fifo_size = 128;
104 p->host_perio_tx_fifo_size = 96;
105 p->max_transfer_size = 65535;
106 p->max_packet_count = 511;
107 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
108 GAHBCFG_HBSTLEN_SHIFT;
109}
110
111static void dwc2_set_amlogic_params(struct dwc2_hsotg *hsotg)
112{
113 struct dwc2_core_params *p = &hsotg->params;
114
115 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
116 p->speed = DWC2_SPEED_PARAM_HIGH;
117 p->host_rx_fifo_size = 512;
118 p->host_nperio_tx_fifo_size = 500;
119 p->host_perio_tx_fifo_size = 500;
120 p->host_channels = 16;
121 p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
122 p->ahbcfg = GAHBCFG_HBSTLEN_INCR8 <<
123 GAHBCFG_HBSTLEN_SHIFT;
124 p->power_down = DWC2_POWER_DOWN_PARAM_NONE;
125}
126
127static void dwc2_set_amlogic_g12a_params(struct dwc2_hsotg *hsotg)
128{
129 struct dwc2_core_params *p = &hsotg->params;
130
131 p->lpm = false;
132 p->lpm_clock_gating = false;
133 p->besl = false;
134 p->hird_threshold_en = false;
135}
136
137static void dwc2_set_amcc_params(struct dwc2_hsotg *hsotg)
138{
139 struct dwc2_core_params *p = &hsotg->params;
140
141 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT;
142}
143
144static void dwc2_set_stm32f4x9_fsotg_params(struct dwc2_hsotg *hsotg)
145{
146 struct dwc2_core_params *p = &hsotg->params;
147
148 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
149 p->speed = DWC2_SPEED_PARAM_FULL;
150 p->host_rx_fifo_size = 128;
151 p->host_nperio_tx_fifo_size = 96;
152 p->host_perio_tx_fifo_size = 96;
153 p->max_packet_count = 256;
154 p->phy_type = DWC2_PHY_TYPE_PARAM_FS;
155 p->i2c_enable = false;
156 p->activate_stm_fs_transceiver = true;
157}
158
159static void dwc2_set_stm32f7_hsotg_params(struct dwc2_hsotg *hsotg)
160{
161 struct dwc2_core_params *p = &hsotg->params;
162
163 p->host_rx_fifo_size = 622;
164 p->host_nperio_tx_fifo_size = 128;
165 p->host_perio_tx_fifo_size = 256;
166}
167
168#ifdef CONFIG_CPU_ASR18XX
169static void dwc2_set_asr_hsotg_params(struct dwc2_hsotg *hsotg)
170{
171 struct dwc2_core_params *p = &hsotg->params;
172
173 p->otg_cap = 2;
174 p->ahbcfg = GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT;
175 p->max_transfer_size = 65535;
176 p->max_packet_count = 511;
177 p->lpm = false;
178 p->speed = DWC2_SPEED_PARAM_HIGH;
179#if 0
180 p->dma_desc_enable = false;
181 p->g_dma_desc = false;
182#endif
183
184 if (readl(CIU_VIRT_BASE) & (0x1 << 24))
185 p->phy_utmi_width = 16;
186 else
187 p->phy_utmi_width = 8;
188}
189#endif
190
191const struct of_device_id dwc2_of_match_table[] = {
192 { .compatible = "brcm,bcm2835-usb", .data = dwc2_set_bcm_params },
193 { .compatible = "hisilicon,hi6220-usb", .data = dwc2_set_his_params },
194 { .compatible = "rockchip,rk3066-usb", .data = dwc2_set_rk_params },
195 { .compatible = "lantiq,arx100-usb", .data = dwc2_set_ltq_params },
196 { .compatible = "lantiq,xrx200-usb", .data = dwc2_set_ltq_params },
197 { .compatible = "snps,dwc2" },
198 { .compatible = "samsung,s3c6400-hsotg",
199 .data = dwc2_set_s3c6400_params },
200 { .compatible = "amlogic,meson8-usb",
201 .data = dwc2_set_amlogic_params },
202 { .compatible = "amlogic,meson8b-usb",
203 .data = dwc2_set_amlogic_params },
204 { .compatible = "amlogic,meson-gxbb-usb",
205 .data = dwc2_set_amlogic_params },
206 { .compatible = "amlogic,meson-g12a-usb",
207 .data = dwc2_set_amlogic_g12a_params },
208 { .compatible = "amcc,dwc-otg", .data = dwc2_set_amcc_params },
209 { .compatible = "st,stm32f4x9-fsotg",
210 .data = dwc2_set_stm32f4x9_fsotg_params },
211 { .compatible = "st,stm32f4x9-hsotg" },
212 { .compatible = "st,stm32f7-hsotg",
213 .data = dwc2_set_stm32f7_hsotg_params },
214#ifdef CONFIG_CPU_ASR18XX
215 { .compatible = "asr,snps-dwc2",
216 .data = dwc2_set_asr_hsotg_params },
217#endif
218 {},
219};
220MODULE_DEVICE_TABLE(of, dwc2_of_match_table);
221
222static void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg)
223{
224 u8 val;
225
226 switch (hsotg->hw_params.op_mode) {
227 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
228 val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
229 break;
230 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
231 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
232 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
233 val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
234 break;
235 default:
236 val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
237 break;
238 }
239
240 hsotg->params.otg_cap = val;
241}
242
243static void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg)
244{
245 int val;
246 u32 hs_phy_type = hsotg->hw_params.hs_phy_type;
247
248 val = DWC2_PHY_TYPE_PARAM_FS;
249 if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
250 if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
251 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
252 val = DWC2_PHY_TYPE_PARAM_UTMI;
253 else
254 val = DWC2_PHY_TYPE_PARAM_ULPI;
255 }
256
257 if (dwc2_is_fs_iot(hsotg))
258 hsotg->params.phy_type = DWC2_PHY_TYPE_PARAM_FS;
259
260 hsotg->params.phy_type = val;
261}
262
263static void dwc2_set_param_speed(struct dwc2_hsotg *hsotg)
264{
265 int val;
266
267 val = hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS ?
268 DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
269
270 if (dwc2_is_fs_iot(hsotg))
271 val = DWC2_SPEED_PARAM_FULL;
272
273 if (dwc2_is_hs_iot(hsotg))
274 val = DWC2_SPEED_PARAM_HIGH;
275
276 hsotg->params.speed = val;
277}
278
279static void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
280{
281 int val;
282
283 val = (hsotg->hw_params.utmi_phy_data_width ==
284 GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
285
286 if (hsotg->phy) {
287 /*
288 * If using the generic PHY framework, check if the PHY bus
289 * width is 8-bit and set the phyif appropriately.
290 */
291 if (phy_get_bus_width(hsotg->phy) == 8)
292 val = 8;
293 }
294
295 hsotg->params.phy_utmi_width = val;
296}
297
298static void dwc2_set_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
299{
300 struct dwc2_core_params *p = &hsotg->params;
301 int depth_average;
302 int fifo_count;
303 int i;
304
305 fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
306
307 memset(p->g_tx_fifo_size, 0, sizeof(p->g_tx_fifo_size));
308 depth_average = dwc2_hsotg_tx_fifo_average_depth(hsotg);
309 for (i = 1; i <= fifo_count; i++)
310 p->g_tx_fifo_size[i] = depth_average;
311}
312
313static void dwc2_set_param_power_down(struct dwc2_hsotg *hsotg)
314{
315 int val;
316
317 if (hsotg->hw_params.hibernation)
318 val = 2;
319 else if (hsotg->hw_params.power_optimized)
320 val = 1;
321 else
322 val = 0;
323
324 hsotg->params.power_down = val;
325}
326
327static void dwc2_set_param_lpm(struct dwc2_hsotg *hsotg)
328{
329 struct dwc2_core_params *p = &hsotg->params;
330
331 p->lpm = hsotg->hw_params.lpm_mode;
332 if (p->lpm) {
333 p->lpm_clock_gating = true;
334 p->besl = true;
335 p->hird_threshold_en = true;
336 p->hird_threshold = 4;
337 } else {
338 p->lpm_clock_gating = false;
339 p->besl = false;
340 p->hird_threshold_en = false;
341 }
342}
343
344/**
345 * dwc2_set_default_params() - Set all core parameters to their
346 * auto-detected default values.
347 *
348 * @hsotg: Programming view of the DWC_otg controller
349 *
350 */
351static void dwc2_set_default_params(struct dwc2_hsotg *hsotg)
352{
353 struct dwc2_hw_params *hw = &hsotg->hw_params;
354 struct dwc2_core_params *p = &hsotg->params;
355 bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
356
357 dwc2_set_param_otg_cap(hsotg);
358 dwc2_set_param_phy_type(hsotg);
359 dwc2_set_param_speed(hsotg);
360 dwc2_set_param_phy_utmi_width(hsotg);
361 dwc2_set_param_power_down(hsotg);
362 dwc2_set_param_lpm(hsotg);
363 p->phy_ulpi_ddr = false;
364 p->phy_ulpi_ext_vbus = false;
365
366 p->enable_dynamic_fifo = hw->enable_dynamic_fifo;
367 p->en_multiple_tx_fifo = hw->en_multiple_tx_fifo;
368 p->i2c_enable = hw->i2c_enable;
369 p->acg_enable = hw->acg_enable;
370 p->ulpi_fs_ls = false;
371 p->ts_dline = false;
372 p->reload_ctl = (hw->snpsid >= DWC2_CORE_REV_2_92a);
373 p->uframe_sched = true;
374 p->external_id_pin_ctl = false;
375 p->ipg_isoc_en = false;
376 p->service_interval = false;
377 p->max_packet_count = hw->max_packet_count;
378 p->max_transfer_size = hw->max_transfer_size;
379 p->ahbcfg = GAHBCFG_HBSTLEN_INCR << GAHBCFG_HBSTLEN_SHIFT;
380 p->ref_clk_per = 33333;
381 p->sof_cnt_wkup_alert = 100;
382
383 if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
384 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
385 p->host_dma = dma_capable;
386 p->dma_desc_enable = false;
387 p->dma_desc_fs_enable = false;
388 p->host_support_fs_ls_low_power = false;
389 p->host_ls_low_power_phy_clk = false;
390 p->host_channels = hw->host_channels;
391 p->host_rx_fifo_size = hw->rx_fifo_size;
392 p->host_nperio_tx_fifo_size = hw->host_nperio_tx_fifo_size;
393 p->host_perio_tx_fifo_size = hw->host_perio_tx_fifo_size;
394 }
395
396 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
397 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
398 p->g_dma = dma_capable;
399 p->g_dma_desc = hw->dma_desc_enable;
400
401 /*
402 * The values for g_rx_fifo_size (2048) and
403 * g_np_tx_fifo_size (1024) come from the legacy s3c
404 * gadget driver. These defaults have been hard-coded
405 * for some time so many platforms depend on these
406 * values. Leave them as defaults for now and only
407 * auto-detect if the hardware does not support the
408 * default.
409 */
410 p->g_rx_fifo_size = 2048;
411 p->g_np_tx_fifo_size = 1024;
412 dwc2_set_param_tx_fifo_sizes(hsotg);
413 }
414}
415
416/**
417 * dwc2_get_device_properties() - Read in device properties.
418 *
419 * @hsotg: Programming view of the DWC_otg controller
420 *
421 * Read in the device properties and adjust core parameters if needed.
422 */
423static void dwc2_get_device_properties(struct dwc2_hsotg *hsotg)
424{
425 struct dwc2_core_params *p = &hsotg->params;
426 int num;
427 const __be32 *prop;
428 unsigned int proplen;
429 struct device_node *np = hsotg->dev->of_node;
430
431 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
432 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
433 device_property_read_u32(hsotg->dev, "g-rx-fifo-size",
434 &p->g_rx_fifo_size);
435
436 device_property_read_u32(hsotg->dev, "g-np-tx-fifo-size",
437 &p->g_np_tx_fifo_size);
438
439 num = device_property_count_u32(hsotg->dev, "g-tx-fifo-size");
440 if (num > 0) {
441 num = min(num, 15);
442 memset(p->g_tx_fifo_size, 0,
443 sizeof(p->g_tx_fifo_size));
444 device_property_read_u32_array(hsotg->dev,
445 "g-tx-fifo-size",
446 &p->g_tx_fifo_size[1],
447 num);
448 }
449 }
450
451 if (of_find_property(hsotg->dev->of_node, "disable-over-current", NULL))
452 p->oc_disable = true;
453
454 prop = of_get_property(np, "lpm-qos", &proplen);
455 if (!prop) {
456 dev_err(hsotg->dev, "lpm-qos not defined\n");
457 } else {
458 hsotg->lpm_qos = be32_to_cpup(prop);
459 hsotg->qos_idle.name = "dwc2_usb";
460 pm_qos_add_request(&hsotg->qos_idle, PM_QOS_CPUIDLE_BLOCK,
461 PM_QOS_CPUIDLE_BLOCK_DEFAULT_VALUE);
462 }
463
464 hsotg->allow_suspend = device_property_read_bool(hsotg->dev,
465 "allow-suspend");
466}
467
468static void dwc2_check_param_otg_cap(struct dwc2_hsotg *hsotg)
469{
470 int valid = 1;
471
472 switch (hsotg->params.otg_cap) {
473 case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
474 if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
475 valid = 0;
476 break;
477 case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
478 switch (hsotg->hw_params.op_mode) {
479 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
480 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
481 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
482 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
483 break;
484 default:
485 valid = 0;
486 break;
487 }
488 break;
489 case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
490 /* always valid */
491 break;
492 default:
493 valid = 0;
494 break;
495 }
496
497 if (!valid)
498 dwc2_set_param_otg_cap(hsotg);
499}
500
501static void dwc2_check_param_phy_type(struct dwc2_hsotg *hsotg)
502{
503 int valid = 0;
504 u32 hs_phy_type;
505 u32 fs_phy_type;
506
507 hs_phy_type = hsotg->hw_params.hs_phy_type;
508 fs_phy_type = hsotg->hw_params.fs_phy_type;
509
510 switch (hsotg->params.phy_type) {
511 case DWC2_PHY_TYPE_PARAM_FS:
512 if (fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
513 valid = 1;
514 break;
515 case DWC2_PHY_TYPE_PARAM_UTMI:
516 if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
517 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
518 valid = 1;
519 break;
520 case DWC2_PHY_TYPE_PARAM_ULPI:
521 if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
522 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
523 valid = 1;
524 break;
525 default:
526 break;
527 }
528
529 if (!valid)
530 dwc2_set_param_phy_type(hsotg);
531}
532
533static void dwc2_check_param_speed(struct dwc2_hsotg *hsotg)
534{
535 int valid = 1;
536 int phy_type = hsotg->params.phy_type;
537 int speed = hsotg->params.speed;
538
539 switch (speed) {
540 case DWC2_SPEED_PARAM_HIGH:
541 if ((hsotg->params.speed == DWC2_SPEED_PARAM_HIGH) &&
542 (phy_type == DWC2_PHY_TYPE_PARAM_FS))
543 valid = 0;
544 break;
545 case DWC2_SPEED_PARAM_FULL:
546 case DWC2_SPEED_PARAM_LOW:
547 break;
548 default:
549 valid = 0;
550 break;
551 }
552
553 if (!valid)
554 dwc2_set_param_speed(hsotg);
555}
556
557static void dwc2_check_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
558{
559 int valid = 0;
560 int param = hsotg->params.phy_utmi_width;
561 int width = hsotg->hw_params.utmi_phy_data_width;
562
563 switch (width) {
564 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
565 valid = (param == 8);
566 break;
567 case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
568 valid = (param == 16);
569 break;
570 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
571 valid = (param == 8 || param == 16);
572 break;
573 }
574
575 if (!valid)
576 dwc2_set_param_phy_utmi_width(hsotg);
577}
578
579static void dwc2_check_param_power_down(struct dwc2_hsotg *hsotg)
580{
581 int param = hsotg->params.power_down;
582
583 switch (param) {
584 case DWC2_POWER_DOWN_PARAM_NONE:
585 break;
586 case DWC2_POWER_DOWN_PARAM_PARTIAL:
587 if (hsotg->hw_params.power_optimized)
588 break;
589 dev_dbg(hsotg->dev,
590 "Partial power down isn't supported by HW\n");
591 param = DWC2_POWER_DOWN_PARAM_NONE;
592 break;
593 case DWC2_POWER_DOWN_PARAM_HIBERNATION:
594 if (hsotg->hw_params.hibernation)
595 break;
596 dev_dbg(hsotg->dev,
597 "Hibernation isn't supported by HW\n");
598 param = DWC2_POWER_DOWN_PARAM_NONE;
599 break;
600 default:
601 dev_err(hsotg->dev,
602 "%s: Invalid parameter power_down=%d\n",
603 __func__, param);
604 param = DWC2_POWER_DOWN_PARAM_NONE;
605 break;
606 }
607
608 hsotg->params.power_down = param;
609}
610
611static void dwc2_check_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
612{
613 int fifo_count;
614 int fifo;
615 int min;
616 u32 total = 0;
617 u32 dptxfszn;
618
619 fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
620 min = hsotg->hw_params.en_multiple_tx_fifo ? 16 : 4;
621
622 for (fifo = 1; fifo <= fifo_count; fifo++)
623 total += hsotg->params.g_tx_fifo_size[fifo];
624
625 if (total > dwc2_hsotg_tx_fifo_total_depth(hsotg) || !total) {
626 dev_warn(hsotg->dev, "%s: Invalid parameter g-tx-fifo-size, setting to default average\n",
627 __func__);
628 dwc2_set_param_tx_fifo_sizes(hsotg);
629 }
630
631 for (fifo = 1; fifo <= fifo_count; fifo++) {
632 dptxfszn = hsotg->hw_params.g_tx_fifo_size[fifo];
633
634 if (hsotg->params.g_tx_fifo_size[fifo] < min ||
635 hsotg->params.g_tx_fifo_size[fifo] > dptxfszn) {
636 dev_warn(hsotg->dev, "%s: Invalid parameter g_tx_fifo_size[%d]=%d\n",
637 __func__, fifo,
638 hsotg->params.g_tx_fifo_size[fifo]);
639 hsotg->params.g_tx_fifo_size[fifo] = dptxfszn;
640 }
641 }
642}
643
644#define CHECK_RANGE(_param, _min, _max, _def) do { \
645 if ((int)(hsotg->params._param) < (_min) || \
646 (hsotg->params._param) > (_max)) { \
647 dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
648 __func__, #_param, hsotg->params._param); \
649 hsotg->params._param = (_def); \
650 } \
651 } while (0)
652
653#define CHECK_BOOL(_param, _check) do { \
654 if (hsotg->params._param && !(_check)) { \
655 dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
656 __func__, #_param, hsotg->params._param); \
657 hsotg->params._param = false; \
658 } \
659 } while (0)
660
661static void dwc2_check_params(struct dwc2_hsotg *hsotg)
662{
663 struct dwc2_hw_params *hw = &hsotg->hw_params;
664 struct dwc2_core_params *p = &hsotg->params;
665 bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
666
667 dwc2_check_param_otg_cap(hsotg);
668 dwc2_check_param_phy_type(hsotg);
669 dwc2_check_param_speed(hsotg);
670 dwc2_check_param_phy_utmi_width(hsotg);
671 dwc2_check_param_power_down(hsotg);
672 CHECK_BOOL(enable_dynamic_fifo, hw->enable_dynamic_fifo);
673 CHECK_BOOL(en_multiple_tx_fifo, hw->en_multiple_tx_fifo);
674 CHECK_BOOL(i2c_enable, hw->i2c_enable);
675 CHECK_BOOL(ipg_isoc_en, hw->ipg_isoc_en);
676 CHECK_BOOL(acg_enable, hw->acg_enable);
677 CHECK_BOOL(reload_ctl, (hsotg->hw_params.snpsid > DWC2_CORE_REV_2_92a));
678 CHECK_BOOL(lpm, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_80a));
679 CHECK_BOOL(lpm, hw->lpm_mode);
680 CHECK_BOOL(lpm_clock_gating, hsotg->params.lpm);
681 CHECK_BOOL(besl, hsotg->params.lpm);
682 CHECK_BOOL(besl, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a));
683 CHECK_BOOL(hird_threshold_en, hsotg->params.lpm);
684 CHECK_RANGE(hird_threshold, 0, hsotg->params.besl ? 12 : 7, 0);
685 CHECK_BOOL(service_interval, hw->service_interval_mode);
686 CHECK_RANGE(max_packet_count,
687 15, hw->max_packet_count,
688 hw->max_packet_count);
689 CHECK_RANGE(max_transfer_size,
690 2047, hw->max_transfer_size,
691 hw->max_transfer_size);
692
693 if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
694 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
695 CHECK_BOOL(host_dma, dma_capable);
696 CHECK_BOOL(dma_desc_enable, p->host_dma);
697 CHECK_BOOL(dma_desc_fs_enable, p->dma_desc_enable);
698 CHECK_BOOL(host_ls_low_power_phy_clk,
699 p->phy_type == DWC2_PHY_TYPE_PARAM_FS);
700 CHECK_RANGE(host_channels,
701 1, hw->host_channels,
702 hw->host_channels);
703 CHECK_RANGE(host_rx_fifo_size,
704 16, hw->rx_fifo_size,
705 hw->rx_fifo_size);
706 CHECK_RANGE(host_nperio_tx_fifo_size,
707 16, hw->host_nperio_tx_fifo_size,
708 hw->host_nperio_tx_fifo_size);
709 CHECK_RANGE(host_perio_tx_fifo_size,
710 16, hw->host_perio_tx_fifo_size,
711 hw->host_perio_tx_fifo_size);
712 }
713
714 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
715 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
716 CHECK_BOOL(g_dma, dma_capable);
717 CHECK_BOOL(g_dma_desc, (p->g_dma && hw->dma_desc_enable));
718 CHECK_RANGE(g_rx_fifo_size,
719 16, hw->rx_fifo_size,
720 hw->rx_fifo_size);
721 CHECK_RANGE(g_np_tx_fifo_size,
722 16, hw->dev_nperio_tx_fifo_size,
723 hw->dev_nperio_tx_fifo_size);
724 dwc2_check_param_tx_fifo_sizes(hsotg);
725 }
726}
727
728/*
729 * Gets host hardware parameters. Forces host mode if not currently in
730 * host mode. Should be called immediately after a core soft reset in
731 * order to get the reset values.
732 */
733static void dwc2_get_host_hwparams(struct dwc2_hsotg *hsotg)
734{
735 struct dwc2_hw_params *hw = &hsotg->hw_params;
736 u32 gnptxfsiz;
737 u32 hptxfsiz;
738
739 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
740 return;
741
742 dwc2_force_mode(hsotg, true);
743
744 gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
745 hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
746
747 hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
748 FIFOSIZE_DEPTH_SHIFT;
749 hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
750 FIFOSIZE_DEPTH_SHIFT;
751}
752
753/*
754 * Gets device hardware parameters. Forces device mode if not
755 * currently in device mode. Should be called immediately after a core
756 * soft reset in order to get the reset values.
757 */
758static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
759{
760 struct dwc2_hw_params *hw = &hsotg->hw_params;
761 u32 gnptxfsiz;
762 int fifo, fifo_count;
763
764 if (hsotg->dr_mode == USB_DR_MODE_HOST)
765 return;
766
767 dwc2_force_mode(hsotg, false);
768
769 gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
770
771 fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
772
773 for (fifo = 1; fifo <= fifo_count; fifo++) {
774 hw->g_tx_fifo_size[fifo] =
775 (dwc2_readl(hsotg, DPTXFSIZN(fifo)) &
776 FIFOSIZE_DEPTH_MASK) >> FIFOSIZE_DEPTH_SHIFT;
777 }
778
779 hw->dev_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
780 FIFOSIZE_DEPTH_SHIFT;
781}
782
783/**
784 * During device initialization, read various hardware configuration
785 * registers and interpret the contents.
786 *
787 * @hsotg: Programming view of the DWC_otg controller
788 *
789 */
790int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
791{
792 struct dwc2_hw_params *hw = &hsotg->hw_params;
793 unsigned int width;
794 u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
795 u32 grxfsiz;
796
797 /*
798 * Attempt to ensure this device is really a DWC_otg Controller.
799 * Read and verify the GSNPSID register contents. The value should be
800 * 0x45f4xxxx, 0x5531xxxx or 0x5532xxxx
801 */
802
803 hw->snpsid = dwc2_readl(hsotg, GSNPSID);
804 if ((hw->snpsid & GSNPSID_ID_MASK) != DWC2_OTG_ID &&
805 (hw->snpsid & GSNPSID_ID_MASK) != DWC2_FS_IOT_ID &&
806 (hw->snpsid & GSNPSID_ID_MASK) != DWC2_HS_IOT_ID) {
807 dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
808 hw->snpsid);
809 return -ENODEV;
810 }
811
812 dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
813 hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
814 hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
815
816 hwcfg1 = dwc2_readl(hsotg, GHWCFG1);
817 hwcfg2 = dwc2_readl(hsotg, GHWCFG2);
818 hwcfg3 = dwc2_readl(hsotg, GHWCFG3);
819 hwcfg4 = dwc2_readl(hsotg, GHWCFG4);
820 grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
821
822 /* hwcfg1 */
823 hw->dev_ep_dirs = hwcfg1;
824
825 /* hwcfg2 */
826 hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
827 GHWCFG2_OP_MODE_SHIFT;
828 hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
829 GHWCFG2_ARCHITECTURE_SHIFT;
830 hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
831 hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
832 GHWCFG2_NUM_HOST_CHAN_SHIFT);
833 hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
834 GHWCFG2_HS_PHY_TYPE_SHIFT;
835 hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
836 GHWCFG2_FS_PHY_TYPE_SHIFT;
837 hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
838 GHWCFG2_NUM_DEV_EP_SHIFT;
839 hw->nperio_tx_q_depth =
840 (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
841 GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
842 hw->host_perio_tx_q_depth =
843 (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
844 GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
845 hw->dev_token_q_depth =
846 (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
847 GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
848
849 /* hwcfg3 */
850 width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
851 GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
852 hw->max_transfer_size = (1 << (width + 11)) - 1;
853 width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
854 GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
855 hw->max_packet_count = (1 << (width + 4)) - 1;
856 hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
857 hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
858 GHWCFG3_DFIFO_DEPTH_SHIFT;
859 hw->lpm_mode = !!(hwcfg3 & GHWCFG3_OTG_LPM_EN);
860
861 /* hwcfg4 */
862 hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
863 hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
864 GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
865 hw->num_dev_in_eps = (hwcfg4 & GHWCFG4_NUM_IN_EPS_MASK) >>
866 GHWCFG4_NUM_IN_EPS_SHIFT;
867 hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
868 hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
869 hw->hibernation = !!(hwcfg4 & GHWCFG4_HIBER);
870 hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
871 GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
872 hw->acg_enable = !!(hwcfg4 & GHWCFG4_ACG_SUPPORTED);
873 hw->ipg_isoc_en = !!(hwcfg4 & GHWCFG4_IPG_ISOC_SUPPORTED);
874 hw->service_interval_mode = !!(hwcfg4 &
875 GHWCFG4_SERVICE_INTERVAL_SUPPORTED);
876
877 /* fifo sizes */
878 hw->rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
879 GRXFSIZ_DEPTH_SHIFT;
880 /*
881 * Host specific hardware parameters. Reading these parameters
882 * requires the controller to be in host mode. The mode will
883 * be forced, if necessary, to read these values.
884 */
885 dwc2_get_host_hwparams(hsotg);
886 dwc2_get_dev_hwparams(hsotg);
887
888 return 0;
889}
890
891int dwc2_init_params(struct dwc2_hsotg *hsotg)
892{
893 const struct of_device_id *match;
894 void (*set_params)(struct dwc2_hsotg *data);
895
896 dwc2_set_default_params(hsotg);
897 dwc2_get_device_properties(hsotg);
898
899 match = of_match_device(dwc2_of_match_table, hsotg->dev);
900 if (match && match->data) {
901 set_params = match->data;
902 set_params(hsotg);
903 }
904
905 dwc2_check_params(hsotg);
906
907 return 0;
908}