1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
3 * Copyright (C) 2004-2016 Synopsys, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/of_device.h>
39 #include <linux/usb/of.h>
43 static void dwc2_set_bcm_params(struct dwc2_hsotg *hsotg)
45 struct dwc2_core_params *p = &hsotg->params;
47 p->host_rx_fifo_size = 774;
48 p->max_transfer_size = 65535;
49 p->max_packet_count = 511;
53 static void dwc2_set_his_params(struct dwc2_hsotg *hsotg)
55 struct dwc2_core_params *p = &hsotg->params;
57 p->otg_caps.hnp_support = false;
58 p->otg_caps.srp_support = false;
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 = DWC2_POWER_DOWN_PARAM_NONE;
76 static void dwc2_set_s3c6400_params(struct dwc2_hsotg *hsotg)
78 struct dwc2_core_params *p = &hsotg->params;
80 p->power_down = DWC2_POWER_DOWN_PARAM_NONE;
81 p->no_clock_gating = true;
82 p->phy_utmi_width = 8;
85 static void dwc2_set_rk_params(struct dwc2_hsotg *hsotg)
87 struct dwc2_core_params *p = &hsotg->params;
89 p->otg_caps.hnp_support = false;
90 p->otg_caps.srp_support = false;
91 p->host_rx_fifo_size = 525;
92 p->host_nperio_tx_fifo_size = 128;
93 p->host_perio_tx_fifo_size = 256;
94 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
95 GAHBCFG_HBSTLEN_SHIFT;
96 p->power_down = DWC2_POWER_DOWN_PARAM_NONE;
99 static void dwc2_set_ltq_params(struct dwc2_hsotg *hsotg)
101 struct dwc2_core_params *p = &hsotg->params;
103 p->otg_caps.hnp_support = false;
104 p->otg_caps.srp_support = false;
105 p->host_rx_fifo_size = 288;
106 p->host_nperio_tx_fifo_size = 128;
107 p->host_perio_tx_fifo_size = 96;
108 p->max_transfer_size = 65535;
109 p->max_packet_count = 511;
110 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
111 GAHBCFG_HBSTLEN_SHIFT;
114 static void dwc2_set_amlogic_params(struct dwc2_hsotg *hsotg)
116 struct dwc2_core_params *p = &hsotg->params;
118 p->otg_caps.hnp_support = false;
119 p->otg_caps.srp_support = false;
120 p->speed = DWC2_SPEED_PARAM_HIGH;
121 p->host_rx_fifo_size = 512;
122 p->host_nperio_tx_fifo_size = 500;
123 p->host_perio_tx_fifo_size = 500;
124 p->host_channels = 16;
125 p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
126 p->ahbcfg = GAHBCFG_HBSTLEN_INCR8 <<
127 GAHBCFG_HBSTLEN_SHIFT;
128 p->power_down = DWC2_POWER_DOWN_PARAM_NONE;
131 static void dwc2_set_amlogic_g12a_params(struct dwc2_hsotg *hsotg)
133 struct dwc2_core_params *p = &hsotg->params;
136 p->lpm_clock_gating = false;
138 p->hird_threshold_en = false;
141 static void dwc2_set_amcc_params(struct dwc2_hsotg *hsotg)
143 struct dwc2_core_params *p = &hsotg->params;
145 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT;
148 static void dwc2_set_stm32f4x9_fsotg_params(struct dwc2_hsotg *hsotg)
150 struct dwc2_core_params *p = &hsotg->params;
152 p->otg_caps.hnp_support = false;
153 p->otg_caps.srp_support = false;
154 p->speed = DWC2_SPEED_PARAM_FULL;
155 p->host_rx_fifo_size = 128;
156 p->host_nperio_tx_fifo_size = 96;
157 p->host_perio_tx_fifo_size = 96;
158 p->max_packet_count = 256;
159 p->phy_type = DWC2_PHY_TYPE_PARAM_FS;
160 p->i2c_enable = false;
161 p->activate_stm_fs_transceiver = true;
164 static void dwc2_set_stm32f7_hsotg_params(struct dwc2_hsotg *hsotg)
166 struct dwc2_core_params *p = &hsotg->params;
168 p->host_rx_fifo_size = 622;
169 p->host_nperio_tx_fifo_size = 128;
170 p->host_perio_tx_fifo_size = 256;
173 static void dwc2_set_stm32mp15_fsotg_params(struct dwc2_hsotg *hsotg)
175 struct dwc2_core_params *p = &hsotg->params;
177 p->otg_caps.hnp_support = false;
178 p->otg_caps.srp_support = false;
179 p->otg_caps.otg_rev = 0x200;
180 p->speed = DWC2_SPEED_PARAM_FULL;
181 p->host_rx_fifo_size = 128;
182 p->host_nperio_tx_fifo_size = 96;
183 p->host_perio_tx_fifo_size = 96;
184 p->max_packet_count = 256;
185 p->phy_type = DWC2_PHY_TYPE_PARAM_FS;
186 p->i2c_enable = false;
187 p->activate_stm_fs_transceiver = true;
188 p->activate_stm_id_vb_detection = true;
189 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT;
190 p->power_down = DWC2_POWER_DOWN_PARAM_NONE;
191 p->host_support_fs_ls_low_power = true;
192 p->host_ls_low_power_phy_clk = true;
195 static void dwc2_set_stm32mp15_hsotg_params(struct dwc2_hsotg *hsotg)
197 struct dwc2_core_params *p = &hsotg->params;
199 p->otg_caps.hnp_support = false;
200 p->otg_caps.srp_support = false;
201 p->otg_caps.otg_rev = 0x200;
202 p->activate_stm_id_vb_detection = !device_property_read_bool(hsotg->dev, "usb-role-switch");
203 p->host_rx_fifo_size = 440;
204 p->host_nperio_tx_fifo_size = 256;
205 p->host_perio_tx_fifo_size = 256;
206 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT;
207 p->power_down = DWC2_POWER_DOWN_PARAM_NONE;
209 p->lpm_clock_gating = false;
211 p->hird_threshold_en = false;
214 const struct of_device_id dwc2_of_match_table[] = {
215 { .compatible = "brcm,bcm2835-usb", .data = dwc2_set_bcm_params },
216 { .compatible = "hisilicon,hi6220-usb", .data = dwc2_set_his_params },
217 { .compatible = "rockchip,rk3066-usb", .data = dwc2_set_rk_params },
218 { .compatible = "lantiq,arx100-usb", .data = dwc2_set_ltq_params },
219 { .compatible = "lantiq,xrx200-usb", .data = dwc2_set_ltq_params },
220 { .compatible = "snps,dwc2" },
221 { .compatible = "samsung,s3c6400-hsotg",
222 .data = dwc2_set_s3c6400_params },
223 { .compatible = "amlogic,meson8-usb",
224 .data = dwc2_set_amlogic_params },
225 { .compatible = "amlogic,meson8b-usb",
226 .data = dwc2_set_amlogic_params },
227 { .compatible = "amlogic,meson-gxbb-usb",
228 .data = dwc2_set_amlogic_params },
229 { .compatible = "amlogic,meson-g12a-usb",
230 .data = dwc2_set_amlogic_g12a_params },
231 { .compatible = "amcc,dwc-otg", .data = dwc2_set_amcc_params },
232 { .compatible = "apm,apm82181-dwc-otg", .data = dwc2_set_amcc_params },
233 { .compatible = "st,stm32f4x9-fsotg",
234 .data = dwc2_set_stm32f4x9_fsotg_params },
235 { .compatible = "st,stm32f4x9-hsotg" },
236 { .compatible = "st,stm32f7-hsotg",
237 .data = dwc2_set_stm32f7_hsotg_params },
238 { .compatible = "st,stm32mp15-fsotg",
239 .data = dwc2_set_stm32mp15_fsotg_params },
240 { .compatible = "st,stm32mp15-hsotg",
241 .data = dwc2_set_stm32mp15_hsotg_params },
244 MODULE_DEVICE_TABLE(of, dwc2_of_match_table);
246 const struct acpi_device_id dwc2_acpi_match[] = {
247 { "BCM2848", (kernel_ulong_t)dwc2_set_bcm_params },
250 MODULE_DEVICE_TABLE(acpi, dwc2_acpi_match);
252 static void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg)
254 switch (hsotg->hw_params.op_mode) {
255 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
256 hsotg->params.otg_caps.hnp_support = true;
257 hsotg->params.otg_caps.srp_support = true;
259 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
260 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
261 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
262 hsotg->params.otg_caps.hnp_support = false;
263 hsotg->params.otg_caps.srp_support = true;
266 hsotg->params.otg_caps.hnp_support = false;
267 hsotg->params.otg_caps.srp_support = false;
272 static void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg)
275 u32 hs_phy_type = hsotg->hw_params.hs_phy_type;
277 val = DWC2_PHY_TYPE_PARAM_FS;
278 if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
279 if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
280 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
281 val = DWC2_PHY_TYPE_PARAM_UTMI;
283 val = DWC2_PHY_TYPE_PARAM_ULPI;
286 if (dwc2_is_fs_iot(hsotg))
287 hsotg->params.phy_type = DWC2_PHY_TYPE_PARAM_FS;
289 hsotg->params.phy_type = val;
292 static void dwc2_set_param_speed(struct dwc2_hsotg *hsotg)
296 val = hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS ?
297 DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
299 if (dwc2_is_fs_iot(hsotg))
300 val = DWC2_SPEED_PARAM_FULL;
302 if (dwc2_is_hs_iot(hsotg))
303 val = DWC2_SPEED_PARAM_HIGH;
305 hsotg->params.speed = val;
308 static void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
312 val = (hsotg->hw_params.utmi_phy_data_width ==
313 GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
317 * If using the generic PHY framework, check if the PHY bus
318 * width is 8-bit and set the phyif appropriately.
320 if (phy_get_bus_width(hsotg->phy) == 8)
324 hsotg->params.phy_utmi_width = val;
327 static void dwc2_set_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
329 struct dwc2_core_params *p = &hsotg->params;
334 fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
336 memset(p->g_tx_fifo_size, 0, sizeof(p->g_tx_fifo_size));
337 depth_average = dwc2_hsotg_tx_fifo_average_depth(hsotg);
338 for (i = 1; i <= fifo_count; i++)
339 p->g_tx_fifo_size[i] = depth_average;
342 static void dwc2_set_param_power_down(struct dwc2_hsotg *hsotg)
346 if (hsotg->hw_params.hibernation)
347 val = DWC2_POWER_DOWN_PARAM_HIBERNATION;
348 else if (hsotg->hw_params.power_optimized)
349 val = DWC2_POWER_DOWN_PARAM_PARTIAL;
351 val = DWC2_POWER_DOWN_PARAM_NONE;
353 hsotg->params.power_down = val;
356 static void dwc2_set_param_lpm(struct dwc2_hsotg *hsotg)
358 struct dwc2_core_params *p = &hsotg->params;
360 p->lpm = hsotg->hw_params.lpm_mode;
362 p->lpm_clock_gating = true;
364 p->hird_threshold_en = true;
365 p->hird_threshold = 4;
367 p->lpm_clock_gating = false;
369 p->hird_threshold_en = false;
374 * dwc2_set_default_params() - Set all core parameters to their
375 * auto-detected default values.
377 * @hsotg: Programming view of the DWC_otg controller
380 static void dwc2_set_default_params(struct dwc2_hsotg *hsotg)
382 struct dwc2_hw_params *hw = &hsotg->hw_params;
383 struct dwc2_core_params *p = &hsotg->params;
384 bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
386 dwc2_set_param_otg_cap(hsotg);
387 dwc2_set_param_phy_type(hsotg);
388 dwc2_set_param_speed(hsotg);
389 dwc2_set_param_phy_utmi_width(hsotg);
390 dwc2_set_param_power_down(hsotg);
391 dwc2_set_param_lpm(hsotg);
392 p->phy_ulpi_ddr = false;
393 p->phy_ulpi_ext_vbus = false;
395 p->enable_dynamic_fifo = hw->enable_dynamic_fifo;
396 p->en_multiple_tx_fifo = hw->en_multiple_tx_fifo;
397 p->i2c_enable = hw->i2c_enable;
398 p->acg_enable = hw->acg_enable;
399 p->ulpi_fs_ls = false;
401 p->reload_ctl = (hw->snpsid >= DWC2_CORE_REV_2_92a);
402 p->uframe_sched = true;
403 p->external_id_pin_ctl = false;
404 p->ipg_isoc_en = false;
405 p->service_interval = false;
406 p->max_packet_count = hw->max_packet_count;
407 p->max_transfer_size = hw->max_transfer_size;
408 p->ahbcfg = GAHBCFG_HBSTLEN_INCR << GAHBCFG_HBSTLEN_SHIFT;
409 p->ref_clk_per = 33333;
410 p->sof_cnt_wkup_alert = 100;
412 if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
413 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
414 p->host_dma = dma_capable;
415 p->dma_desc_enable = false;
416 p->dma_desc_fs_enable = false;
417 p->host_support_fs_ls_low_power = false;
418 p->host_ls_low_power_phy_clk = false;
419 p->host_channels = hw->host_channels;
420 p->host_rx_fifo_size = hw->rx_fifo_size;
421 p->host_nperio_tx_fifo_size = hw->host_nperio_tx_fifo_size;
422 p->host_perio_tx_fifo_size = hw->host_perio_tx_fifo_size;
425 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
426 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
427 p->g_dma = dma_capable;
428 p->g_dma_desc = hw->dma_desc_enable;
431 * The values for g_rx_fifo_size (2048) and
432 * g_np_tx_fifo_size (1024) come from the legacy s3c
433 * gadget driver. These defaults have been hard-coded
434 * for some time so many platforms depend on these
435 * values. Leave them as defaults for now and only
436 * auto-detect if the hardware does not support the
439 p->g_rx_fifo_size = 2048;
440 p->g_np_tx_fifo_size = 1024;
441 dwc2_set_param_tx_fifo_sizes(hsotg);
446 * dwc2_get_device_properties() - Read in device properties.
448 * @hsotg: Programming view of the DWC_otg controller
450 * Read in the device properties and adjust core parameters if needed.
452 static void dwc2_get_device_properties(struct dwc2_hsotg *hsotg)
454 struct dwc2_core_params *p = &hsotg->params;
457 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
458 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
459 device_property_read_u32(hsotg->dev, "g-rx-fifo-size",
462 device_property_read_u32(hsotg->dev, "g-np-tx-fifo-size",
463 &p->g_np_tx_fifo_size);
465 num = device_property_count_u32(hsotg->dev, "g-tx-fifo-size");
468 memset(p->g_tx_fifo_size, 0,
469 sizeof(p->g_tx_fifo_size));
470 device_property_read_u32_array(hsotg->dev,
472 &p->g_tx_fifo_size[1],
476 of_usb_update_otg_caps(hsotg->dev->of_node, &p->otg_caps);
479 if (of_find_property(hsotg->dev->of_node, "disable-over-current", NULL))
480 p->oc_disable = true;
483 static void dwc2_check_param_otg_cap(struct dwc2_hsotg *hsotg)
487 if (hsotg->params.otg_caps.hnp_support && hsotg->params.otg_caps.srp_support) {
488 /* check HNP && SRP capable */
489 if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
491 } else if (!hsotg->params.otg_caps.hnp_support) {
492 /* check SRP only capable */
493 if (hsotg->params.otg_caps.srp_support) {
494 switch (hsotg->hw_params.op_mode) {
495 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
496 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
497 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
498 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
505 /* else: NO HNP && NO SRP capable: always valid */
511 dwc2_set_param_otg_cap(hsotg);
514 static void dwc2_check_param_phy_type(struct dwc2_hsotg *hsotg)
520 hs_phy_type = hsotg->hw_params.hs_phy_type;
521 fs_phy_type = hsotg->hw_params.fs_phy_type;
523 switch (hsotg->params.phy_type) {
524 case DWC2_PHY_TYPE_PARAM_FS:
525 if (fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
528 case DWC2_PHY_TYPE_PARAM_UTMI:
529 if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
530 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
533 case DWC2_PHY_TYPE_PARAM_ULPI:
534 if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
535 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
543 dwc2_set_param_phy_type(hsotg);
546 static void dwc2_check_param_speed(struct dwc2_hsotg *hsotg)
549 int phy_type = hsotg->params.phy_type;
550 int speed = hsotg->params.speed;
553 case DWC2_SPEED_PARAM_HIGH:
554 if ((hsotg->params.speed == DWC2_SPEED_PARAM_HIGH) &&
555 (phy_type == DWC2_PHY_TYPE_PARAM_FS))
558 case DWC2_SPEED_PARAM_FULL:
559 case DWC2_SPEED_PARAM_LOW:
567 dwc2_set_param_speed(hsotg);
570 static void dwc2_check_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
573 int param = hsotg->params.phy_utmi_width;
574 int width = hsotg->hw_params.utmi_phy_data_width;
577 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
578 valid = (param == 8);
580 case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
581 valid = (param == 16);
583 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
584 valid = (param == 8 || param == 16);
589 dwc2_set_param_phy_utmi_width(hsotg);
592 static void dwc2_check_param_power_down(struct dwc2_hsotg *hsotg)
594 int param = hsotg->params.power_down;
597 case DWC2_POWER_DOWN_PARAM_NONE:
599 case DWC2_POWER_DOWN_PARAM_PARTIAL:
600 if (hsotg->hw_params.power_optimized)
603 "Partial power down isn't supported by HW\n");
604 param = DWC2_POWER_DOWN_PARAM_NONE;
606 case DWC2_POWER_DOWN_PARAM_HIBERNATION:
607 if (hsotg->hw_params.hibernation)
610 "Hibernation isn't supported by HW\n");
611 param = DWC2_POWER_DOWN_PARAM_NONE;
615 "%s: Invalid parameter power_down=%d\n",
617 param = DWC2_POWER_DOWN_PARAM_NONE;
621 hsotg->params.power_down = param;
624 static void dwc2_check_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
632 fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
633 min = hsotg->hw_params.en_multiple_tx_fifo ? 16 : 4;
635 for (fifo = 1; fifo <= fifo_count; fifo++)
636 total += hsotg->params.g_tx_fifo_size[fifo];
638 if (total > dwc2_hsotg_tx_fifo_total_depth(hsotg) || !total) {
639 dev_warn(hsotg->dev, "%s: Invalid parameter g-tx-fifo-size, setting to default average\n",
641 dwc2_set_param_tx_fifo_sizes(hsotg);
644 for (fifo = 1; fifo <= fifo_count; fifo++) {
645 dptxfszn = hsotg->hw_params.g_tx_fifo_size[fifo];
647 if (hsotg->params.g_tx_fifo_size[fifo] < min ||
648 hsotg->params.g_tx_fifo_size[fifo] > dptxfszn) {
649 dev_warn(hsotg->dev, "%s: Invalid parameter g_tx_fifo_size[%d]=%d\n",
651 hsotg->params.g_tx_fifo_size[fifo]);
652 hsotg->params.g_tx_fifo_size[fifo] = dptxfszn;
657 #define CHECK_RANGE(_param, _min, _max, _def) do { \
658 if ((int)(hsotg->params._param) < (_min) || \
659 (hsotg->params._param) > (_max)) { \
660 dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
661 __func__, #_param, hsotg->params._param); \
662 hsotg->params._param = (_def); \
666 #define CHECK_BOOL(_param, _check) do { \
667 if (hsotg->params._param && !(_check)) { \
668 dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
669 __func__, #_param, hsotg->params._param); \
670 hsotg->params._param = false; \
674 static void dwc2_check_params(struct dwc2_hsotg *hsotg)
676 struct dwc2_hw_params *hw = &hsotg->hw_params;
677 struct dwc2_core_params *p = &hsotg->params;
678 bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
680 dwc2_check_param_otg_cap(hsotg);
681 dwc2_check_param_phy_type(hsotg);
682 dwc2_check_param_speed(hsotg);
683 dwc2_check_param_phy_utmi_width(hsotg);
684 dwc2_check_param_power_down(hsotg);
685 CHECK_BOOL(enable_dynamic_fifo, hw->enable_dynamic_fifo);
686 CHECK_BOOL(en_multiple_tx_fifo, hw->en_multiple_tx_fifo);
687 CHECK_BOOL(i2c_enable, hw->i2c_enable);
688 CHECK_BOOL(ipg_isoc_en, hw->ipg_isoc_en);
689 CHECK_BOOL(acg_enable, hw->acg_enable);
690 CHECK_BOOL(reload_ctl, (hsotg->hw_params.snpsid > DWC2_CORE_REV_2_92a));
691 CHECK_BOOL(lpm, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_80a));
692 CHECK_BOOL(lpm, hw->lpm_mode);
693 CHECK_BOOL(lpm_clock_gating, hsotg->params.lpm);
694 CHECK_BOOL(besl, hsotg->params.lpm);
695 CHECK_BOOL(besl, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a));
696 CHECK_BOOL(hird_threshold_en, hsotg->params.lpm);
697 CHECK_RANGE(hird_threshold, 0, hsotg->params.besl ? 12 : 7, 0);
698 CHECK_BOOL(service_interval, hw->service_interval_mode);
699 CHECK_RANGE(max_packet_count,
700 15, hw->max_packet_count,
701 hw->max_packet_count);
702 CHECK_RANGE(max_transfer_size,
703 2047, hw->max_transfer_size,
704 hw->max_transfer_size);
706 if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
707 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
708 CHECK_BOOL(host_dma, dma_capable);
709 CHECK_BOOL(dma_desc_enable, p->host_dma);
710 CHECK_BOOL(dma_desc_fs_enable, p->dma_desc_enable);
711 CHECK_BOOL(host_ls_low_power_phy_clk,
712 p->phy_type == DWC2_PHY_TYPE_PARAM_FS);
713 CHECK_RANGE(host_channels,
714 1, hw->host_channels,
716 CHECK_RANGE(host_rx_fifo_size,
717 16, hw->rx_fifo_size,
719 CHECK_RANGE(host_nperio_tx_fifo_size,
720 16, hw->host_nperio_tx_fifo_size,
721 hw->host_nperio_tx_fifo_size);
722 CHECK_RANGE(host_perio_tx_fifo_size,
723 16, hw->host_perio_tx_fifo_size,
724 hw->host_perio_tx_fifo_size);
727 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
728 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
729 CHECK_BOOL(g_dma, dma_capable);
730 CHECK_BOOL(g_dma_desc, (p->g_dma && hw->dma_desc_enable));
731 CHECK_RANGE(g_rx_fifo_size,
732 16, hw->rx_fifo_size,
734 CHECK_RANGE(g_np_tx_fifo_size,
735 16, hw->dev_nperio_tx_fifo_size,
736 hw->dev_nperio_tx_fifo_size);
737 dwc2_check_param_tx_fifo_sizes(hsotg);
742 * Gets host hardware parameters. Forces host mode if not currently in
743 * host mode. Should be called immediately after a core soft reset in
744 * order to get the reset values.
746 static void dwc2_get_host_hwparams(struct dwc2_hsotg *hsotg)
748 struct dwc2_hw_params *hw = &hsotg->hw_params;
752 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
755 dwc2_force_mode(hsotg, true);
757 gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
758 hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
760 hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
761 FIFOSIZE_DEPTH_SHIFT;
762 hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
763 FIFOSIZE_DEPTH_SHIFT;
767 * Gets device hardware parameters. Forces device mode if not
768 * currently in device mode. Should be called immediately after a core
769 * soft reset in order to get the reset values.
771 static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
773 struct dwc2_hw_params *hw = &hsotg->hw_params;
775 int fifo, fifo_count;
777 if (hsotg->dr_mode == USB_DR_MODE_HOST)
780 dwc2_force_mode(hsotg, false);
782 gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
784 fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
786 for (fifo = 1; fifo <= fifo_count; fifo++) {
787 hw->g_tx_fifo_size[fifo] =
788 (dwc2_readl(hsotg, DPTXFSIZN(fifo)) &
789 FIFOSIZE_DEPTH_MASK) >> FIFOSIZE_DEPTH_SHIFT;
792 hw->dev_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
793 FIFOSIZE_DEPTH_SHIFT;
797 * dwc2_get_hwparams() - During device initialization, read various hardware
798 * configuration registers and interpret the contents.
800 * @hsotg: Programming view of the DWC_otg controller
803 int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
805 struct dwc2_hw_params *hw = &hsotg->hw_params;
807 u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
810 hwcfg1 = dwc2_readl(hsotg, GHWCFG1);
811 hwcfg2 = dwc2_readl(hsotg, GHWCFG2);
812 hwcfg3 = dwc2_readl(hsotg, GHWCFG3);
813 hwcfg4 = dwc2_readl(hsotg, GHWCFG4);
814 grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
817 hw->dev_ep_dirs = hwcfg1;
820 hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
821 GHWCFG2_OP_MODE_SHIFT;
822 hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
823 GHWCFG2_ARCHITECTURE_SHIFT;
824 hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
825 hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
826 GHWCFG2_NUM_HOST_CHAN_SHIFT);
827 hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
828 GHWCFG2_HS_PHY_TYPE_SHIFT;
829 hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
830 GHWCFG2_FS_PHY_TYPE_SHIFT;
831 hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
832 GHWCFG2_NUM_DEV_EP_SHIFT;
833 hw->nperio_tx_q_depth =
834 (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
835 GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
836 hw->host_perio_tx_q_depth =
837 (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
838 GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
839 hw->dev_token_q_depth =
840 (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
841 GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
844 width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
845 GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
846 hw->max_transfer_size = (1 << (width + 11)) - 1;
847 width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
848 GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
849 hw->max_packet_count = (1 << (width + 4)) - 1;
850 hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
851 hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
852 GHWCFG3_DFIFO_DEPTH_SHIFT;
853 hw->lpm_mode = !!(hwcfg3 & GHWCFG3_OTG_LPM_EN);
856 hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
857 hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
858 GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
859 hw->num_dev_in_eps = (hwcfg4 & GHWCFG4_NUM_IN_EPS_MASK) >>
860 GHWCFG4_NUM_IN_EPS_SHIFT;
861 hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
862 hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
863 hw->hibernation = !!(hwcfg4 & GHWCFG4_HIBER);
864 hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
865 GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
866 hw->acg_enable = !!(hwcfg4 & GHWCFG4_ACG_SUPPORTED);
867 hw->ipg_isoc_en = !!(hwcfg4 & GHWCFG4_IPG_ISOC_SUPPORTED);
868 hw->service_interval_mode = !!(hwcfg4 &
869 GHWCFG4_SERVICE_INTERVAL_SUPPORTED);
872 hw->rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
875 * Host specific hardware parameters. Reading these parameters
876 * requires the controller to be in host mode. The mode will
877 * be forced, if necessary, to read these values.
879 dwc2_get_host_hwparams(hsotg);
880 dwc2_get_dev_hwparams(hsotg);
885 typedef void (*set_params_cb)(struct dwc2_hsotg *data);
887 int dwc2_init_params(struct dwc2_hsotg *hsotg)
889 const struct of_device_id *match;
890 set_params_cb set_params;
892 dwc2_set_default_params(hsotg);
893 dwc2_get_device_properties(hsotg);
895 match = of_match_device(dwc2_of_match_table, hsotg->dev);
896 if (match && match->data) {
897 set_params = match->data;
900 const struct acpi_device_id *amatch;
902 amatch = acpi_match_device(dwc2_acpi_match, hsotg->dev);
903 if (amatch && amatch->driver_data) {
904 set_params = (set_params_cb)amatch->driver_data;
909 dwc2_check_params(hsotg);