xref: /openbmc/linux/drivers/usb/dwc2/params.c (revision 9e894ee30afe0910edf2d85dc4680974b5720ddf)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2323230efSJohn Youn /*
3323230efSJohn Youn  * Copyright (C) 2004-2016 Synopsys, Inc.
4323230efSJohn Youn  *
5323230efSJohn Youn  * Redistribution and use in source and binary forms, with or without
6323230efSJohn Youn  * modification, are permitted provided that the following conditions
7323230efSJohn Youn  * are met:
8323230efSJohn Youn  * 1. Redistributions of source code must retain the above copyright
9323230efSJohn Youn  *    notice, this list of conditions, and the following disclaimer,
10323230efSJohn Youn  *    without modification.
11323230efSJohn Youn  * 2. Redistributions in binary form must reproduce the above copyright
12323230efSJohn Youn  *    notice, this list of conditions and the following disclaimer in the
13323230efSJohn Youn  *    documentation and/or other materials provided with the distribution.
14323230efSJohn Youn  * 3. The names of the above-listed copyright holders may not be used
15323230efSJohn Youn  *    to endorse or promote products derived from this software without
16323230efSJohn Youn  *    specific prior written permission.
17323230efSJohn Youn  *
18323230efSJohn Youn  * ALTERNATIVELY, this software may be distributed under the terms of the
19323230efSJohn Youn  * GNU General Public License ("GPL") as published by the Free Software
20323230efSJohn Youn  * Foundation; either version 2 of the License, or (at your option) any
21323230efSJohn Youn  * later version.
22323230efSJohn Youn  *
23323230efSJohn Youn  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24323230efSJohn Youn  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25323230efSJohn Youn  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26323230efSJohn Youn  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27323230efSJohn Youn  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28323230efSJohn Youn  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29323230efSJohn Youn  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30323230efSJohn Youn  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31323230efSJohn Youn  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32323230efSJohn Youn  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33323230efSJohn Youn  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34323230efSJohn Youn  */
35323230efSJohn Youn 
36323230efSJohn Youn #include <linux/kernel.h>
37323230efSJohn Youn #include <linux/module.h>
38323230efSJohn Youn #include <linux/of_device.h>
39f5c8a6cbSFabrice Gasnier #include <linux/usb/of.h>
40323230efSJohn Youn 
41323230efSJohn Youn #include "core.h"
42323230efSJohn Youn 
437de1debcSJohn Youn static void dwc2_set_bcm_params(struct dwc2_hsotg *hsotg)
447de1debcSJohn Youn {
457de1debcSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
46323230efSJohn Youn 
477de1debcSJohn Youn 	p->host_rx_fifo_size = 774;
487de1debcSJohn Youn 	p->max_transfer_size = 65535;
497de1debcSJohn Youn 	p->max_packet_count = 511;
507de1debcSJohn Youn 	p->ahbcfg = 0x10;
517de1debcSJohn Youn }
52323230efSJohn Youn 
537de1debcSJohn Youn static void dwc2_set_his_params(struct dwc2_hsotg *hsotg)
547de1debcSJohn Youn {
557de1debcSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
56323230efSJohn Youn 
57f5c8a6cbSFabrice Gasnier 	p->otg_caps.hnp_support = false;
58f5c8a6cbSFabrice Gasnier 	p->otg_caps.srp_support = false;
597de1debcSJohn Youn 	p->speed = DWC2_SPEED_PARAM_HIGH;
607de1debcSJohn Youn 	p->host_rx_fifo_size = 512;
617de1debcSJohn Youn 	p->host_nperio_tx_fifo_size = 512;
627de1debcSJohn Youn 	p->host_perio_tx_fifo_size = 512;
637de1debcSJohn Youn 	p->max_transfer_size = 65535;
647de1debcSJohn Youn 	p->max_packet_count = 511;
657de1debcSJohn Youn 	p->host_channels = 16;
667de1debcSJohn Youn 	p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
677de1debcSJohn Youn 	p->phy_utmi_width = 8;
687de1debcSJohn Youn 	p->i2c_enable = false;
697de1debcSJohn Youn 	p->reload_ctl = false;
707de1debcSJohn Youn 	p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
717de1debcSJohn Youn 		GAHBCFG_HBSTLEN_SHIFT;
72ca8b0332SChen Yu 	p->change_speed_quirk = true;
7307d9878fSJisheng Zhang 	p->power_down = DWC2_POWER_DOWN_PARAM_NONE;
747de1debcSJohn Youn }
75323230efSJohn Youn 
7635a60541SMarek Szyprowski static void dwc2_set_s3c6400_params(struct dwc2_hsotg *hsotg)
7735a60541SMarek Szyprowski {
7835a60541SMarek Szyprowski 	struct dwc2_core_params *p = &hsotg->params;
7935a60541SMarek Szyprowski 
8007d9878fSJisheng Zhang 	p->power_down = DWC2_POWER_DOWN_PARAM_NONE;
81c4a0f7a6SMarek Szyprowski 	p->no_clock_gating = true;
821112cf4cSMarek Szyprowski 	p->phy_utmi_width = 8;
8335a60541SMarek Szyprowski }
8435a60541SMarek Szyprowski 
857de1debcSJohn Youn static void dwc2_set_rk_params(struct dwc2_hsotg *hsotg)
867de1debcSJohn Youn {
877de1debcSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
887de1debcSJohn Youn 
89f5c8a6cbSFabrice Gasnier 	p->otg_caps.hnp_support = false;
90f5c8a6cbSFabrice Gasnier 	p->otg_caps.srp_support = false;
917de1debcSJohn Youn 	p->host_rx_fifo_size = 525;
927de1debcSJohn Youn 	p->host_nperio_tx_fifo_size = 128;
937de1debcSJohn Youn 	p->host_perio_tx_fifo_size = 256;
947de1debcSJohn Youn 	p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
957de1debcSJohn Youn 		GAHBCFG_HBSTLEN_SHIFT;
9607d9878fSJisheng Zhang 	p->power_down = DWC2_POWER_DOWN_PARAM_NONE;
977de1debcSJohn Youn }
987de1debcSJohn Youn 
997de1debcSJohn Youn static void dwc2_set_ltq_params(struct dwc2_hsotg *hsotg)
1007de1debcSJohn Youn {
1017de1debcSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
1027de1debcSJohn Youn 
103f5c8a6cbSFabrice Gasnier 	p->otg_caps.hnp_support = false;
104f5c8a6cbSFabrice Gasnier 	p->otg_caps.srp_support = false;
1057de1debcSJohn Youn 	p->host_rx_fifo_size = 288;
1067de1debcSJohn Youn 	p->host_nperio_tx_fifo_size = 128;
1077de1debcSJohn Youn 	p->host_perio_tx_fifo_size = 96;
1087de1debcSJohn Youn 	p->max_transfer_size = 65535;
1097de1debcSJohn Youn 	p->max_packet_count = 511;
1107de1debcSJohn Youn 	p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
1117de1debcSJohn Youn 		GAHBCFG_HBSTLEN_SHIFT;
1127de1debcSJohn Youn }
1137de1debcSJohn Youn 
1147de1debcSJohn Youn static void dwc2_set_amlogic_params(struct dwc2_hsotg *hsotg)
1157de1debcSJohn Youn {
1167de1debcSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
1177de1debcSJohn Youn 
118f5c8a6cbSFabrice Gasnier 	p->otg_caps.hnp_support = false;
119f5c8a6cbSFabrice Gasnier 	p->otg_caps.srp_support = false;
1207de1debcSJohn Youn 	p->speed = DWC2_SPEED_PARAM_HIGH;
1217de1debcSJohn Youn 	p->host_rx_fifo_size = 512;
1227de1debcSJohn Youn 	p->host_nperio_tx_fifo_size = 500;
1237de1debcSJohn Youn 	p->host_perio_tx_fifo_size = 500;
1247de1debcSJohn Youn 	p->host_channels = 16;
1257de1debcSJohn Youn 	p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
1267de1debcSJohn Youn 	p->ahbcfg = GAHBCFG_HBSTLEN_INCR8 <<
1277de1debcSJohn Youn 		GAHBCFG_HBSTLEN_SHIFT;
128cc10ce0cSMartin Blumenstingl 	p->power_down = DWC2_POWER_DOWN_PARAM_NONE;
1297de1debcSJohn Youn }
1307de1debcSJohn Youn 
131fc4e326eSNeil Armstrong static void dwc2_set_amlogic_g12a_params(struct dwc2_hsotg *hsotg)
132fc4e326eSNeil Armstrong {
133fc4e326eSNeil Armstrong 	struct dwc2_core_params *p = &hsotg->params;
134fc4e326eSNeil Armstrong 
135fc4e326eSNeil Armstrong 	p->lpm = false;
136fc4e326eSNeil Armstrong 	p->lpm_clock_gating = false;
137fc4e326eSNeil Armstrong 	p->besl = false;
138fc4e326eSNeil Armstrong 	p->hird_threshold_en = false;
139fc4e326eSNeil Armstrong }
140fc4e326eSNeil Armstrong 
1417de1debcSJohn Youn static void dwc2_set_amcc_params(struct dwc2_hsotg *hsotg)
1427de1debcSJohn Youn {
1437de1debcSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
1447de1debcSJohn Youn 
1457de1debcSJohn Youn 	p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT;
1467de1debcSJohn Youn }
147323230efSJohn Youn 
148e35b1350SBruno Herrera static void dwc2_set_stm32f4x9_fsotg_params(struct dwc2_hsotg *hsotg)
149e35b1350SBruno Herrera {
150e35b1350SBruno Herrera 	struct dwc2_core_params *p = &hsotg->params;
151e35b1350SBruno Herrera 
152f5c8a6cbSFabrice Gasnier 	p->otg_caps.hnp_support = false;
153f5c8a6cbSFabrice Gasnier 	p->otg_caps.srp_support = false;
154e35b1350SBruno Herrera 	p->speed = DWC2_SPEED_PARAM_FULL;
155e35b1350SBruno Herrera 	p->host_rx_fifo_size = 128;
156e35b1350SBruno Herrera 	p->host_nperio_tx_fifo_size = 96;
157e35b1350SBruno Herrera 	p->host_perio_tx_fifo_size = 96;
158e35b1350SBruno Herrera 	p->max_packet_count = 256;
159e35b1350SBruno Herrera 	p->phy_type = DWC2_PHY_TYPE_PARAM_FS;
160e35b1350SBruno Herrera 	p->i2c_enable = false;
161e35b1350SBruno Herrera 	p->activate_stm_fs_transceiver = true;
162e35b1350SBruno Herrera }
163e35b1350SBruno Herrera 
1641a149e35SAmelie Delaunay static void dwc2_set_stm32f7_hsotg_params(struct dwc2_hsotg *hsotg)
165d8fae8b9SAmelie Delaunay {
166d8fae8b9SAmelie Delaunay 	struct dwc2_core_params *p = &hsotg->params;
167d8fae8b9SAmelie Delaunay 
168d8fae8b9SAmelie Delaunay 	p->host_rx_fifo_size = 622;
169d8fae8b9SAmelie Delaunay 	p->host_nperio_tx_fifo_size = 128;
170d8fae8b9SAmelie Delaunay 	p->host_perio_tx_fifo_size = 256;
171d8fae8b9SAmelie Delaunay }
172d8fae8b9SAmelie Delaunay 
173a415083aSAmelie Delaunay static void dwc2_set_stm32mp15_fsotg_params(struct dwc2_hsotg *hsotg)
174a415083aSAmelie Delaunay {
175a415083aSAmelie Delaunay 	struct dwc2_core_params *p = &hsotg->params;
176a415083aSAmelie Delaunay 
177f5c8a6cbSFabrice Gasnier 	p->otg_caps.hnp_support = false;
178f5c8a6cbSFabrice Gasnier 	p->otg_caps.srp_support = false;
179*9e894ee3SFabrice Gasnier 	p->otg_caps.otg_rev = 0x200;
180a415083aSAmelie Delaunay 	p->speed = DWC2_SPEED_PARAM_FULL;
181a415083aSAmelie Delaunay 	p->host_rx_fifo_size = 128;
182a415083aSAmelie Delaunay 	p->host_nperio_tx_fifo_size = 96;
183a415083aSAmelie Delaunay 	p->host_perio_tx_fifo_size = 96;
184a415083aSAmelie Delaunay 	p->max_packet_count = 256;
185a415083aSAmelie Delaunay 	p->phy_type = DWC2_PHY_TYPE_PARAM_FS;
186a415083aSAmelie Delaunay 	p->i2c_enable = false;
187a415083aSAmelie Delaunay 	p->activate_stm_fs_transceiver = true;
188a415083aSAmelie Delaunay 	p->activate_stm_id_vb_detection = true;
1892979ee7aSAmelie Delaunay 	p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT;
190a415083aSAmelie Delaunay 	p->power_down = DWC2_POWER_DOWN_PARAM_NONE;
191f228cb27SAmelie Delaunay 	p->host_support_fs_ls_low_power = true;
192f228cb27SAmelie Delaunay 	p->host_ls_low_power_phy_clk = true;
193a415083aSAmelie Delaunay }
194a415083aSAmelie Delaunay 
195a415083aSAmelie Delaunay static void dwc2_set_stm32mp15_hsotg_params(struct dwc2_hsotg *hsotg)
196a415083aSAmelie Delaunay {
197a415083aSAmelie Delaunay 	struct dwc2_core_params *p = &hsotg->params;
198a415083aSAmelie Delaunay 
199f5c8a6cbSFabrice Gasnier 	p->otg_caps.hnp_support = false;
200f5c8a6cbSFabrice Gasnier 	p->otg_caps.srp_support = false;
201*9e894ee3SFabrice Gasnier 	p->otg_caps.otg_rev = 0x200;
202d58ba480SAmelie Delaunay 	p->activate_stm_id_vb_detection = !device_property_read_bool(hsotg->dev, "usb-role-switch");
203a415083aSAmelie Delaunay 	p->host_rx_fifo_size = 440;
204a415083aSAmelie Delaunay 	p->host_nperio_tx_fifo_size = 256;
205a415083aSAmelie Delaunay 	p->host_perio_tx_fifo_size = 256;
2062979ee7aSAmelie Delaunay 	p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT;
207a415083aSAmelie Delaunay 	p->power_down = DWC2_POWER_DOWN_PARAM_NONE;
20853febc95SAmelie Delaunay 	p->lpm = false;
20953febc95SAmelie Delaunay 	p->lpm_clock_gating = false;
21053febc95SAmelie Delaunay 	p->besl = false;
21153febc95SAmelie Delaunay 	p->hird_threshold_en = false;
212a415083aSAmelie Delaunay }
213a415083aSAmelie Delaunay 
214323230efSJohn Youn const struct of_device_id dwc2_of_match_table[] = {
2157de1debcSJohn Youn 	{ .compatible = "brcm,bcm2835-usb", .data = dwc2_set_bcm_params },
2167de1debcSJohn Youn 	{ .compatible = "hisilicon,hi6220-usb", .data = dwc2_set_his_params  },
2177de1debcSJohn Youn 	{ .compatible = "rockchip,rk3066-usb", .data = dwc2_set_rk_params },
2187de1debcSJohn Youn 	{ .compatible = "lantiq,arx100-usb", .data = dwc2_set_ltq_params },
2197de1debcSJohn Youn 	{ .compatible = "lantiq,xrx200-usb", .data = dwc2_set_ltq_params },
2207de1debcSJohn Youn 	{ .compatible = "snps,dwc2" },
22135a60541SMarek Szyprowski 	{ .compatible = "samsung,s3c6400-hsotg",
22235a60541SMarek Szyprowski 	  .data = dwc2_set_s3c6400_params },
22355b644fdSMartin Blumenstingl 	{ .compatible = "amlogic,meson8-usb",
22455b644fdSMartin Blumenstingl 	  .data = dwc2_set_amlogic_params },
2257de1debcSJohn Youn 	{ .compatible = "amlogic,meson8b-usb",
2267de1debcSJohn Youn 	  .data = dwc2_set_amlogic_params },
2277de1debcSJohn Youn 	{ .compatible = "amlogic,meson-gxbb-usb",
2287de1debcSJohn Youn 	  .data = dwc2_set_amlogic_params },
229fc4e326eSNeil Armstrong 	{ .compatible = "amlogic,meson-g12a-usb",
230fc4e326eSNeil Armstrong 	  .data = dwc2_set_amlogic_g12a_params },
2317de1debcSJohn Youn 	{ .compatible = "amcc,dwc-otg", .data = dwc2_set_amcc_params },
2320abe3863SChristian Lamparter 	{ .compatible = "apm,apm82181-dwc-otg", .data = dwc2_set_amcc_params },
233e35b1350SBruno Herrera 	{ .compatible = "st,stm32f4x9-fsotg",
234e35b1350SBruno Herrera 	  .data = dwc2_set_stm32f4x9_fsotg_params },
235e35b1350SBruno Herrera 	{ .compatible = "st,stm32f4x9-hsotg" },
2361a149e35SAmelie Delaunay 	{ .compatible = "st,stm32f7-hsotg",
2371a149e35SAmelie Delaunay 	  .data = dwc2_set_stm32f7_hsotg_params },
238a415083aSAmelie Delaunay 	{ .compatible = "st,stm32mp15-fsotg",
239a415083aSAmelie Delaunay 	  .data = dwc2_set_stm32mp15_fsotg_params },
240a415083aSAmelie Delaunay 	{ .compatible = "st,stm32mp15-hsotg",
241a415083aSAmelie Delaunay 	  .data = dwc2_set_stm32mp15_hsotg_params },
242323230efSJohn Youn 	{},
243323230efSJohn Youn };
244323230efSJohn Youn MODULE_DEVICE_TABLE(of, dwc2_of_match_table);
245323230efSJohn Youn 
2462e5db2c0SJeremy Linton const struct acpi_device_id dwc2_acpi_match[] = {
2472e5db2c0SJeremy Linton 	{ "BCM2848", (kernel_ulong_t)dwc2_set_bcm_params },
2482e5db2c0SJeremy Linton 	{ },
2492e5db2c0SJeremy Linton };
2502e5db2c0SJeremy Linton MODULE_DEVICE_TABLE(acpi, dwc2_acpi_match);
2512e5db2c0SJeremy Linton 
252245977c9SJohn Youn static void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg)
25305ee799fSJohn Youn {
254323230efSJohn Youn 	switch (hsotg->hw_params.op_mode) {
255323230efSJohn Youn 	case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
256f5c8a6cbSFabrice Gasnier 		hsotg->params.otg_caps.hnp_support = true;
257f5c8a6cbSFabrice Gasnier 		hsotg->params.otg_caps.srp_support = true;
258323230efSJohn Youn 		break;
259323230efSJohn Youn 	case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
260323230efSJohn Youn 	case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
261323230efSJohn Youn 	case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
262f5c8a6cbSFabrice Gasnier 		hsotg->params.otg_caps.hnp_support = false;
263f5c8a6cbSFabrice Gasnier 		hsotg->params.otg_caps.srp_support = true;
264323230efSJohn Youn 		break;
265323230efSJohn Youn 	default:
266f5c8a6cbSFabrice Gasnier 		hsotg->params.otg_caps.hnp_support = false;
267f5c8a6cbSFabrice Gasnier 		hsotg->params.otg_caps.srp_support = false;
268323230efSJohn Youn 		break;
269323230efSJohn Youn 	}
270323230efSJohn Youn }
271323230efSJohn Youn 
272245977c9SJohn Youn static void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg)
273323230efSJohn Youn {
274245977c9SJohn Youn 	int val;
275245977c9SJohn Youn 	u32 hs_phy_type = hsotg->hw_params.hs_phy_type;
276323230efSJohn Youn 
277323230efSJohn Youn 	val = DWC2_PHY_TYPE_PARAM_FS;
278323230efSJohn Youn 	if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
279323230efSJohn Youn 		if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
280323230efSJohn Youn 		    hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
281323230efSJohn Youn 			val = DWC2_PHY_TYPE_PARAM_UTMI;
282323230efSJohn Youn 		else
283323230efSJohn Youn 			val = DWC2_PHY_TYPE_PARAM_ULPI;
284323230efSJohn Youn 	}
285245977c9SJohn Youn 
286245977c9SJohn Youn 	if (dwc2_is_fs_iot(hsotg))
287245977c9SJohn Youn 		hsotg->params.phy_type = DWC2_PHY_TYPE_PARAM_FS;
288323230efSJohn Youn 
289bea8e86cSJohn Youn 	hsotg->params.phy_type = val;
290323230efSJohn Youn }
291323230efSJohn Youn 
292245977c9SJohn Youn static void dwc2_set_param_speed(struct dwc2_hsotg *hsotg)
293323230efSJohn Youn {
294245977c9SJohn Youn 	int val;
295323230efSJohn Youn 
296245977c9SJohn Youn 	val = hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS ?
297323230efSJohn Youn 		DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
298245977c9SJohn Youn 
299245977c9SJohn Youn 	if (dwc2_is_fs_iot(hsotg))
300245977c9SJohn Youn 		val = DWC2_SPEED_PARAM_FULL;
301245977c9SJohn Youn 
302245977c9SJohn Youn 	if (dwc2_is_hs_iot(hsotg))
303245977c9SJohn Youn 		val = DWC2_SPEED_PARAM_HIGH;
304323230efSJohn Youn 
305bea8e86cSJohn Youn 	hsotg->params.speed = val;
306323230efSJohn Youn }
307323230efSJohn Youn 
308245977c9SJohn Youn static void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
309323230efSJohn Youn {
310245977c9SJohn Youn 	int val;
311323230efSJohn Youn 
312323230efSJohn Youn 	val = (hsotg->hw_params.utmi_phy_data_width ==
313323230efSJohn Youn 	       GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
314323230efSJohn Youn 
31542de8afcSJules Maselbas 	if (hsotg->phy) {
31642de8afcSJules Maselbas 		/*
31742de8afcSJules Maselbas 		 * If using the generic PHY framework, check if the PHY bus
31842de8afcSJules Maselbas 		 * width is 8-bit and set the phyif appropriately.
31942de8afcSJules Maselbas 		 */
32042de8afcSJules Maselbas 		if (phy_get_bus_width(hsotg->phy) == 8)
32142de8afcSJules Maselbas 			val = 8;
32242de8afcSJules Maselbas 	}
32342de8afcSJules Maselbas 
324bea8e86cSJohn Youn 	hsotg->params.phy_utmi_width = val;
325323230efSJohn Youn }
326323230efSJohn Youn 
32705ee799fSJohn Youn static void dwc2_set_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
32805ee799fSJohn Youn {
32905ee799fSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
330c138ecfaSSevak Arakelyan 	int depth_average;
331c138ecfaSSevak Arakelyan 	int fifo_count;
332c138ecfaSSevak Arakelyan 	int i;
333c138ecfaSSevak Arakelyan 
334c138ecfaSSevak Arakelyan 	fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
33505ee799fSJohn Youn 
336245977c9SJohn Youn 	memset(p->g_tx_fifo_size, 0, sizeof(p->g_tx_fifo_size));
337c138ecfaSSevak Arakelyan 	depth_average = dwc2_hsotg_tx_fifo_average_depth(hsotg);
338c138ecfaSSevak Arakelyan 	for (i = 1; i <= fifo_count; i++)
339c138ecfaSSevak Arakelyan 		p->g_tx_fifo_size[i] = depth_average;
3409962b62fSJohn Youn }
3419962b62fSJohn Youn 
34203ea6d6eSJohn Youn static void dwc2_set_param_power_down(struct dwc2_hsotg *hsotg)
34303ea6d6eSJohn Youn {
34403ea6d6eSJohn Youn 	int val;
34503ea6d6eSJohn Youn 
34603ea6d6eSJohn Youn 	if (hsotg->hw_params.hibernation)
34707d9878fSJisheng Zhang 		val = DWC2_POWER_DOWN_PARAM_HIBERNATION;
34803ea6d6eSJohn Youn 	else if (hsotg->hw_params.power_optimized)
34907d9878fSJisheng Zhang 		val = DWC2_POWER_DOWN_PARAM_PARTIAL;
35003ea6d6eSJohn Youn 	else
35107d9878fSJisheng Zhang 		val = DWC2_POWER_DOWN_PARAM_NONE;
35203ea6d6eSJohn Youn 
35303ea6d6eSJohn Youn 	hsotg->params.power_down = val;
35403ea6d6eSJohn Youn }
35503ea6d6eSJohn Youn 
35628b5c129SMinas Harutyunyan static void dwc2_set_param_lpm(struct dwc2_hsotg *hsotg)
35728b5c129SMinas Harutyunyan {
35828b5c129SMinas Harutyunyan 	struct dwc2_core_params *p = &hsotg->params;
35928b5c129SMinas Harutyunyan 
36028b5c129SMinas Harutyunyan 	p->lpm = hsotg->hw_params.lpm_mode;
36128b5c129SMinas Harutyunyan 	if (p->lpm) {
36228b5c129SMinas Harutyunyan 		p->lpm_clock_gating = true;
36328b5c129SMinas Harutyunyan 		p->besl = true;
36428b5c129SMinas Harutyunyan 		p->hird_threshold_en = true;
36528b5c129SMinas Harutyunyan 		p->hird_threshold = 4;
36628b5c129SMinas Harutyunyan 	} else {
36728b5c129SMinas Harutyunyan 		p->lpm_clock_gating = false;
36828b5c129SMinas Harutyunyan 		p->besl = false;
36928b5c129SMinas Harutyunyan 		p->hird_threshold_en = false;
37028b5c129SMinas Harutyunyan 	}
37128b5c129SMinas Harutyunyan }
37228b5c129SMinas Harutyunyan 
37305ee799fSJohn Youn /**
374245977c9SJohn Youn  * dwc2_set_default_params() - Set all core parameters to their
375245977c9SJohn Youn  * auto-detected default values.
3766fb914d7SGrigor Tovmasyan  *
3776fb914d7SGrigor Tovmasyan  * @hsotg: Programming view of the DWC_otg controller
3786fb914d7SGrigor Tovmasyan  *
379323230efSJohn Youn  */
380245977c9SJohn Youn static void dwc2_set_default_params(struct dwc2_hsotg *hsotg)
381323230efSJohn Youn {
38205ee799fSJohn Youn 	struct dwc2_hw_params *hw = &hsotg->hw_params;
38305ee799fSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
3846b66ce51SJohn Youn 	bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
385323230efSJohn Youn 
386245977c9SJohn Youn 	dwc2_set_param_otg_cap(hsotg);
387245977c9SJohn Youn 	dwc2_set_param_phy_type(hsotg);
388245977c9SJohn Youn 	dwc2_set_param_speed(hsotg);
389245977c9SJohn Youn 	dwc2_set_param_phy_utmi_width(hsotg);
39003ea6d6eSJohn Youn 	dwc2_set_param_power_down(hsotg);
39128b5c129SMinas Harutyunyan 	dwc2_set_param_lpm(hsotg);
392245977c9SJohn Youn 	p->phy_ulpi_ddr = false;
393245977c9SJohn Youn 	p->phy_ulpi_ext_vbus = false;
394245977c9SJohn Youn 
395245977c9SJohn Youn 	p->enable_dynamic_fifo = hw->enable_dynamic_fifo;
396245977c9SJohn Youn 	p->en_multiple_tx_fifo = hw->en_multiple_tx_fifo;
397245977c9SJohn Youn 	p->i2c_enable = hw->i2c_enable;
39866e77a24SRazmik Karapetyan 	p->acg_enable = hw->acg_enable;
399245977c9SJohn Youn 	p->ulpi_fs_ls = false;
400245977c9SJohn Youn 	p->ts_dline = false;
401245977c9SJohn Youn 	p->reload_ctl = (hw->snpsid >= DWC2_CORE_REV_2_92a);
402245977c9SJohn Youn 	p->uframe_sched = true;
403245977c9SJohn Youn 	p->external_id_pin_ctl = false;
404b43ebc96SGrigor Tovmasyan 	p->ipg_isoc_en = false;
405ca531bc2SGrigor Tovmasyan 	p->service_interval = false;
406245977c9SJohn Youn 	p->max_packet_count = hw->max_packet_count;
407245977c9SJohn Youn 	p->max_transfer_size = hw->max_transfer_size;
4081b52d2faSRazmik Karapetyan 	p->ahbcfg = GAHBCFG_HBSTLEN_INCR << GAHBCFG_HBSTLEN_SHIFT;
409f3a61e4eSGrigor Tovmasyan 	p->ref_clk_per = 33333;
410f3a61e4eSGrigor Tovmasyan 	p->sof_cnt_wkup_alert = 100;
411245977c9SJohn Youn 
4126b66ce51SJohn Youn 	if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
4136b66ce51SJohn Youn 	    (hsotg->dr_mode == USB_DR_MODE_OTG)) {
414245977c9SJohn Youn 		p->host_dma = dma_capable;
415245977c9SJohn Youn 		p->dma_desc_enable = false;
416245977c9SJohn Youn 		p->dma_desc_fs_enable = false;
417245977c9SJohn Youn 		p->host_support_fs_ls_low_power = false;
418245977c9SJohn Youn 		p->host_ls_low_power_phy_clk = false;
419245977c9SJohn Youn 		p->host_channels = hw->host_channels;
420245977c9SJohn Youn 		p->host_rx_fifo_size = hw->rx_fifo_size;
421245977c9SJohn Youn 		p->host_nperio_tx_fifo_size = hw->host_nperio_tx_fifo_size;
422245977c9SJohn Youn 		p->host_perio_tx_fifo_size = hw->host_perio_tx_fifo_size;
4236b66ce51SJohn Youn 	}
4246b66ce51SJohn Youn 
42505ee799fSJohn Youn 	if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
42605ee799fSJohn Youn 	    (hsotg->dr_mode == USB_DR_MODE_OTG)) {
427245977c9SJohn Youn 		p->g_dma = dma_capable;
428245977c9SJohn Youn 		p->g_dma_desc = hw->dma_desc_enable;
42905ee799fSJohn Youn 
43005ee799fSJohn Youn 		/*
43105ee799fSJohn Youn 		 * The values for g_rx_fifo_size (2048) and
43205ee799fSJohn Youn 		 * g_np_tx_fifo_size (1024) come from the legacy s3c
43305ee799fSJohn Youn 		 * gadget driver. These defaults have been hard-coded
43405ee799fSJohn Youn 		 * for some time so many platforms depend on these
43505ee799fSJohn Youn 		 * values. Leave them as defaults for now and only
43605ee799fSJohn Youn 		 * auto-detect if the hardware does not support the
43705ee799fSJohn Youn 		 * default.
43805ee799fSJohn Youn 		 */
439245977c9SJohn Youn 		p->g_rx_fifo_size = 2048;
440245977c9SJohn Youn 		p->g_np_tx_fifo_size = 1024;
44105ee799fSJohn Youn 		dwc2_set_param_tx_fifo_sizes(hsotg);
44205ee799fSJohn Youn 	}
443323230efSJohn Youn }
444323230efSJohn Youn 
445f9f93cbbSJohn Youn /**
446f9f93cbbSJohn Youn  * dwc2_get_device_properties() - Read in device properties.
447f9f93cbbSJohn Youn  *
4486fb914d7SGrigor Tovmasyan  * @hsotg: Programming view of the DWC_otg controller
4496fb914d7SGrigor Tovmasyan  *
450f9f93cbbSJohn Youn  * Read in the device properties and adjust core parameters if needed.
451f9f93cbbSJohn Youn  */
452f9f93cbbSJohn Youn static void dwc2_get_device_properties(struct dwc2_hsotg *hsotg)
453f9f93cbbSJohn Youn {
454f9f93cbbSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
455f9f93cbbSJohn Youn 	int num;
456f9f93cbbSJohn Youn 
457f9f93cbbSJohn Youn 	if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
458f9f93cbbSJohn Youn 	    (hsotg->dr_mode == USB_DR_MODE_OTG)) {
459f9f93cbbSJohn Youn 		device_property_read_u32(hsotg->dev, "g-rx-fifo-size",
460f9f93cbbSJohn Youn 					 &p->g_rx_fifo_size);
461f9f93cbbSJohn Youn 
462f9f93cbbSJohn Youn 		device_property_read_u32(hsotg->dev, "g-np-tx-fifo-size",
463f9f93cbbSJohn Youn 					 &p->g_np_tx_fifo_size);
464f9f93cbbSJohn Youn 
46507e803ecSAndy Shevchenko 		num = device_property_count_u32(hsotg->dev, "g-tx-fifo-size");
466f9f93cbbSJohn Youn 		if (num > 0) {
467f9f93cbbSJohn Youn 			num = min(num, 15);
468f9f93cbbSJohn Youn 			memset(p->g_tx_fifo_size, 0,
469f9f93cbbSJohn Youn 			       sizeof(p->g_tx_fifo_size));
470f9f93cbbSJohn Youn 			device_property_read_u32_array(hsotg->dev,
471f9f93cbbSJohn Youn 						       "g-tx-fifo-size",
472f9f93cbbSJohn Youn 						       &p->g_tx_fifo_size[1],
473f9f93cbbSJohn Youn 						       num);
474f9f93cbbSJohn Youn 		}
475f5c8a6cbSFabrice Gasnier 
476f5c8a6cbSFabrice Gasnier 		of_usb_update_otg_caps(hsotg->dev->of_node, &p->otg_caps);
477f9f93cbbSJohn Youn 	}
478b11633c4SDinh Nguyen 
479b11633c4SDinh Nguyen 	if (of_find_property(hsotg->dev->of_node, "disable-over-current", NULL))
480b11633c4SDinh Nguyen 		p->oc_disable = true;
481f9f93cbbSJohn Youn }
482f9f93cbbSJohn Youn 
483d936e666SJohn Youn static void dwc2_check_param_otg_cap(struct dwc2_hsotg *hsotg)
484d936e666SJohn Youn {
485d936e666SJohn Youn 	int valid = 1;
486d936e666SJohn Youn 
487f5c8a6cbSFabrice Gasnier 	if (hsotg->params.otg_caps.hnp_support && hsotg->params.otg_caps.srp_support) {
488f5c8a6cbSFabrice Gasnier 		/* check HNP && SRP capable */
489d936e666SJohn Youn 		if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
490d936e666SJohn Youn 			valid = 0;
491f5c8a6cbSFabrice Gasnier 	} else if (!hsotg->params.otg_caps.hnp_support) {
492f5c8a6cbSFabrice Gasnier 		/* check SRP only capable */
493f5c8a6cbSFabrice Gasnier 		if (hsotg->params.otg_caps.srp_support) {
494d936e666SJohn Youn 			switch (hsotg->hw_params.op_mode) {
495d936e666SJohn Youn 			case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
496d936e666SJohn Youn 			case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
497d936e666SJohn Youn 			case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
498d936e666SJohn Youn 			case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
499d936e666SJohn Youn 				break;
500d936e666SJohn Youn 			default:
501d936e666SJohn Youn 				valid = 0;
502d936e666SJohn Youn 				break;
503d936e666SJohn Youn 			}
504f5c8a6cbSFabrice Gasnier 		}
505f5c8a6cbSFabrice Gasnier 		/* else: NO HNP && NO SRP capable: always valid */
506f5c8a6cbSFabrice Gasnier 	} else {
507d936e666SJohn Youn 		valid = 0;
508d936e666SJohn Youn 	}
509d936e666SJohn Youn 
510d936e666SJohn Youn 	if (!valid)
511d936e666SJohn Youn 		dwc2_set_param_otg_cap(hsotg);
512d936e666SJohn Youn }
513d936e666SJohn Youn 
514d936e666SJohn Youn static void dwc2_check_param_phy_type(struct dwc2_hsotg *hsotg)
515d936e666SJohn Youn {
516d936e666SJohn Youn 	int valid = 0;
517d936e666SJohn Youn 	u32 hs_phy_type;
518d936e666SJohn Youn 	u32 fs_phy_type;
519d936e666SJohn Youn 
520d936e666SJohn Youn 	hs_phy_type = hsotg->hw_params.hs_phy_type;
521d936e666SJohn Youn 	fs_phy_type = hsotg->hw_params.fs_phy_type;
522d936e666SJohn Youn 
523d936e666SJohn Youn 	switch (hsotg->params.phy_type) {
524d936e666SJohn Youn 	case DWC2_PHY_TYPE_PARAM_FS:
525d936e666SJohn Youn 		if (fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
526d936e666SJohn Youn 			valid = 1;
527d936e666SJohn Youn 		break;
528d936e666SJohn Youn 	case DWC2_PHY_TYPE_PARAM_UTMI:
529d936e666SJohn Youn 		if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
530d936e666SJohn Youn 		    (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
531d936e666SJohn Youn 			valid = 1;
532d936e666SJohn Youn 		break;
533d936e666SJohn Youn 	case DWC2_PHY_TYPE_PARAM_ULPI:
534d936e666SJohn Youn 		if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
535d936e666SJohn Youn 		    (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
536d936e666SJohn Youn 			valid = 1;
537d936e666SJohn Youn 		break;
538d936e666SJohn Youn 	default:
539d936e666SJohn Youn 		break;
540d936e666SJohn Youn 	}
541d936e666SJohn Youn 
542d936e666SJohn Youn 	if (!valid)
543d936e666SJohn Youn 		dwc2_set_param_phy_type(hsotg);
544d936e666SJohn Youn }
545d936e666SJohn Youn 
546d936e666SJohn Youn static void dwc2_check_param_speed(struct dwc2_hsotg *hsotg)
547d936e666SJohn Youn {
548d936e666SJohn Youn 	int valid = 1;
549d936e666SJohn Youn 	int phy_type = hsotg->params.phy_type;
550d936e666SJohn Youn 	int speed = hsotg->params.speed;
551d936e666SJohn Youn 
552d936e666SJohn Youn 	switch (speed) {
553d936e666SJohn Youn 	case DWC2_SPEED_PARAM_HIGH:
554d936e666SJohn Youn 		if ((hsotg->params.speed == DWC2_SPEED_PARAM_HIGH) &&
555d936e666SJohn Youn 		    (phy_type == DWC2_PHY_TYPE_PARAM_FS))
556d936e666SJohn Youn 			valid = 0;
557d936e666SJohn Youn 		break;
558d936e666SJohn Youn 	case DWC2_SPEED_PARAM_FULL:
559d936e666SJohn Youn 	case DWC2_SPEED_PARAM_LOW:
560d936e666SJohn Youn 		break;
561d936e666SJohn Youn 	default:
562d936e666SJohn Youn 		valid = 0;
563d936e666SJohn Youn 		break;
564d936e666SJohn Youn 	}
565d936e666SJohn Youn 
566d936e666SJohn Youn 	if (!valid)
567d936e666SJohn Youn 		dwc2_set_param_speed(hsotg);
568d936e666SJohn Youn }
569d936e666SJohn Youn 
570d936e666SJohn Youn static void dwc2_check_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
571d936e666SJohn Youn {
572d936e666SJohn Youn 	int valid = 0;
573d936e666SJohn Youn 	int param = hsotg->params.phy_utmi_width;
574d936e666SJohn Youn 	int width = hsotg->hw_params.utmi_phy_data_width;
575d936e666SJohn Youn 
576d936e666SJohn Youn 	switch (width) {
577d936e666SJohn Youn 	case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
578d936e666SJohn Youn 		valid = (param == 8);
579d936e666SJohn Youn 		break;
580d936e666SJohn Youn 	case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
581d936e666SJohn Youn 		valid = (param == 16);
582d936e666SJohn Youn 		break;
583d936e666SJohn Youn 	case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
584d936e666SJohn Youn 		valid = (param == 8 || param == 16);
585d936e666SJohn Youn 		break;
586d936e666SJohn Youn 	}
587d936e666SJohn Youn 
588d936e666SJohn Youn 	if (!valid)
589d936e666SJohn Youn 		dwc2_set_param_phy_utmi_width(hsotg);
590d936e666SJohn Youn }
591d936e666SJohn Youn 
592631a2310SVardan Mikayelyan static void dwc2_check_param_power_down(struct dwc2_hsotg *hsotg)
593631a2310SVardan Mikayelyan {
594631a2310SVardan Mikayelyan 	int param = hsotg->params.power_down;
595631a2310SVardan Mikayelyan 
596631a2310SVardan Mikayelyan 	switch (param) {
597631a2310SVardan Mikayelyan 	case DWC2_POWER_DOWN_PARAM_NONE:
598631a2310SVardan Mikayelyan 		break;
599631a2310SVardan Mikayelyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
600631a2310SVardan Mikayelyan 		if (hsotg->hw_params.power_optimized)
601631a2310SVardan Mikayelyan 			break;
602631a2310SVardan Mikayelyan 		dev_dbg(hsotg->dev,
603631a2310SVardan Mikayelyan 			"Partial power down isn't supported by HW\n");
604631a2310SVardan Mikayelyan 		param = DWC2_POWER_DOWN_PARAM_NONE;
605631a2310SVardan Mikayelyan 		break;
606631a2310SVardan Mikayelyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
607631a2310SVardan Mikayelyan 		if (hsotg->hw_params.hibernation)
608631a2310SVardan Mikayelyan 			break;
609631a2310SVardan Mikayelyan 		dev_dbg(hsotg->dev,
610631a2310SVardan Mikayelyan 			"Hibernation isn't supported by HW\n");
611631a2310SVardan Mikayelyan 		param = DWC2_POWER_DOWN_PARAM_NONE;
612631a2310SVardan Mikayelyan 		break;
613631a2310SVardan Mikayelyan 	default:
614631a2310SVardan Mikayelyan 		dev_err(hsotg->dev,
615631a2310SVardan Mikayelyan 			"%s: Invalid parameter power_down=%d\n",
616631a2310SVardan Mikayelyan 			__func__, param);
617631a2310SVardan Mikayelyan 		param = DWC2_POWER_DOWN_PARAM_NONE;
618631a2310SVardan Mikayelyan 		break;
619631a2310SVardan Mikayelyan 	}
620631a2310SVardan Mikayelyan 
621631a2310SVardan Mikayelyan 	hsotg->params.power_down = param;
622631a2310SVardan Mikayelyan }
623631a2310SVardan Mikayelyan 
6243c6aea73SSevak Arakelyan static void dwc2_check_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
6253c6aea73SSevak Arakelyan {
6263c6aea73SSevak Arakelyan 	int fifo_count;
6273c6aea73SSevak Arakelyan 	int fifo;
6283c6aea73SSevak Arakelyan 	int min;
6293c6aea73SSevak Arakelyan 	u32 total = 0;
6303c6aea73SSevak Arakelyan 	u32 dptxfszn;
6313c6aea73SSevak Arakelyan 
6323c6aea73SSevak Arakelyan 	fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
6333c6aea73SSevak Arakelyan 	min = hsotg->hw_params.en_multiple_tx_fifo ? 16 : 4;
6343c6aea73SSevak Arakelyan 
6353c6aea73SSevak Arakelyan 	for (fifo = 1; fifo <= fifo_count; fifo++)
6363c6aea73SSevak Arakelyan 		total += hsotg->params.g_tx_fifo_size[fifo];
6373c6aea73SSevak Arakelyan 
6383c6aea73SSevak Arakelyan 	if (total > dwc2_hsotg_tx_fifo_total_depth(hsotg) || !total) {
6393c6aea73SSevak Arakelyan 		dev_warn(hsotg->dev, "%s: Invalid parameter g-tx-fifo-size, setting to default average\n",
6403c6aea73SSevak Arakelyan 			 __func__);
6413c6aea73SSevak Arakelyan 		dwc2_set_param_tx_fifo_sizes(hsotg);
6423c6aea73SSevak Arakelyan 	}
6433c6aea73SSevak Arakelyan 
6443c6aea73SSevak Arakelyan 	for (fifo = 1; fifo <= fifo_count; fifo++) {
6459273083aSMinas Harutyunyan 		dptxfszn = hsotg->hw_params.g_tx_fifo_size[fifo];
6463c6aea73SSevak Arakelyan 
6473c6aea73SSevak Arakelyan 		if (hsotg->params.g_tx_fifo_size[fifo] < min ||
6483c6aea73SSevak Arakelyan 		    hsotg->params.g_tx_fifo_size[fifo] >  dptxfszn) {
6493c6aea73SSevak Arakelyan 			dev_warn(hsotg->dev, "%s: Invalid parameter g_tx_fifo_size[%d]=%d\n",
6503c6aea73SSevak Arakelyan 				 __func__, fifo,
6513c6aea73SSevak Arakelyan 				 hsotg->params.g_tx_fifo_size[fifo]);
6523c6aea73SSevak Arakelyan 			hsotg->params.g_tx_fifo_size[fifo] = dptxfszn;
6533c6aea73SSevak Arakelyan 		}
6543c6aea73SSevak Arakelyan 	}
6553c6aea73SSevak Arakelyan }
6563c6aea73SSevak Arakelyan 
657d936e666SJohn Youn #define CHECK_RANGE(_param, _min, _max, _def) do {			\
65847265c06SGrigor Tovmasyan 		if ((int)(hsotg->params._param) < (_min) ||		\
659d936e666SJohn Youn 		    (hsotg->params._param) > (_max)) {			\
660d936e666SJohn Youn 			dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
661d936e666SJohn Youn 				 __func__, #_param, hsotg->params._param); \
662d936e666SJohn Youn 			hsotg->params._param = (_def);			\
663d936e666SJohn Youn 		}							\
664d936e666SJohn Youn 	} while (0)
665d936e666SJohn Youn 
666d936e666SJohn Youn #define CHECK_BOOL(_param, _check) do {					\
667d936e666SJohn Youn 		if (hsotg->params._param && !(_check)) {		\
668d936e666SJohn Youn 			dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
669d936e666SJohn Youn 				 __func__, #_param, hsotg->params._param); \
670d936e666SJohn Youn 			hsotg->params._param = false;			\
671d936e666SJohn Youn 		}							\
672d936e666SJohn Youn 	} while (0)
673d936e666SJohn Youn 
674d936e666SJohn Youn static void dwc2_check_params(struct dwc2_hsotg *hsotg)
675d936e666SJohn Youn {
676d936e666SJohn Youn 	struct dwc2_hw_params *hw = &hsotg->hw_params;
677d936e666SJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
678d936e666SJohn Youn 	bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
679d936e666SJohn Youn 
680d936e666SJohn Youn 	dwc2_check_param_otg_cap(hsotg);
681d936e666SJohn Youn 	dwc2_check_param_phy_type(hsotg);
682d936e666SJohn Youn 	dwc2_check_param_speed(hsotg);
683d936e666SJohn Youn 	dwc2_check_param_phy_utmi_width(hsotg);
684631a2310SVardan Mikayelyan 	dwc2_check_param_power_down(hsotg);
685d936e666SJohn Youn 	CHECK_BOOL(enable_dynamic_fifo, hw->enable_dynamic_fifo);
686d936e666SJohn Youn 	CHECK_BOOL(en_multiple_tx_fifo, hw->en_multiple_tx_fifo);
687d936e666SJohn Youn 	CHECK_BOOL(i2c_enable, hw->i2c_enable);
688b43ebc96SGrigor Tovmasyan 	CHECK_BOOL(ipg_isoc_en, hw->ipg_isoc_en);
68966e77a24SRazmik Karapetyan 	CHECK_BOOL(acg_enable, hw->acg_enable);
690d936e666SJohn Youn 	CHECK_BOOL(reload_ctl, (hsotg->hw_params.snpsid > DWC2_CORE_REV_2_92a));
6916f80b6deSSevak Arakelyan 	CHECK_BOOL(lpm, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_80a));
6926f80b6deSSevak Arakelyan 	CHECK_BOOL(lpm, hw->lpm_mode);
6936f80b6deSSevak Arakelyan 	CHECK_BOOL(lpm_clock_gating, hsotg->params.lpm);
6946f80b6deSSevak Arakelyan 	CHECK_BOOL(besl, hsotg->params.lpm);
6956f80b6deSSevak Arakelyan 	CHECK_BOOL(besl, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a));
6966f80b6deSSevak Arakelyan 	CHECK_BOOL(hird_threshold_en, hsotg->params.lpm);
6976f80b6deSSevak Arakelyan 	CHECK_RANGE(hird_threshold, 0, hsotg->params.besl ? 12 : 7, 0);
698ca531bc2SGrigor Tovmasyan 	CHECK_BOOL(service_interval, hw->service_interval_mode);
699d936e666SJohn Youn 	CHECK_RANGE(max_packet_count,
700d936e666SJohn Youn 		    15, hw->max_packet_count,
701d936e666SJohn Youn 		    hw->max_packet_count);
702d936e666SJohn Youn 	CHECK_RANGE(max_transfer_size,
703d936e666SJohn Youn 		    2047, hw->max_transfer_size,
704d936e666SJohn Youn 		    hw->max_transfer_size);
705d936e666SJohn Youn 
706d936e666SJohn Youn 	if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
707d936e666SJohn Youn 	    (hsotg->dr_mode == USB_DR_MODE_OTG)) {
708d936e666SJohn Youn 		CHECK_BOOL(host_dma, dma_capable);
709d936e666SJohn Youn 		CHECK_BOOL(dma_desc_enable, p->host_dma);
710d936e666SJohn Youn 		CHECK_BOOL(dma_desc_fs_enable, p->dma_desc_enable);
711d936e666SJohn Youn 		CHECK_BOOL(host_ls_low_power_phy_clk,
712d936e666SJohn Youn 			   p->phy_type == DWC2_PHY_TYPE_PARAM_FS);
713d936e666SJohn Youn 		CHECK_RANGE(host_channels,
714d936e666SJohn Youn 			    1, hw->host_channels,
715d936e666SJohn Youn 			    hw->host_channels);
716d936e666SJohn Youn 		CHECK_RANGE(host_rx_fifo_size,
717d936e666SJohn Youn 			    16, hw->rx_fifo_size,
718d936e666SJohn Youn 			    hw->rx_fifo_size);
719d936e666SJohn Youn 		CHECK_RANGE(host_nperio_tx_fifo_size,
720d936e666SJohn Youn 			    16, hw->host_nperio_tx_fifo_size,
721d936e666SJohn Youn 			    hw->host_nperio_tx_fifo_size);
722d936e666SJohn Youn 		CHECK_RANGE(host_perio_tx_fifo_size,
723d936e666SJohn Youn 			    16, hw->host_perio_tx_fifo_size,
724d936e666SJohn Youn 			    hw->host_perio_tx_fifo_size);
725d936e666SJohn Youn 	}
726d936e666SJohn Youn 
727d936e666SJohn Youn 	if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
728d936e666SJohn Youn 	    (hsotg->dr_mode == USB_DR_MODE_OTG)) {
729d936e666SJohn Youn 		CHECK_BOOL(g_dma, dma_capable);
730d936e666SJohn Youn 		CHECK_BOOL(g_dma_desc, (p->g_dma && hw->dma_desc_enable));
731d936e666SJohn Youn 		CHECK_RANGE(g_rx_fifo_size,
732d936e666SJohn Youn 			    16, hw->rx_fifo_size,
733d936e666SJohn Youn 			    hw->rx_fifo_size);
734d936e666SJohn Youn 		CHECK_RANGE(g_np_tx_fifo_size,
735d936e666SJohn Youn 			    16, hw->dev_nperio_tx_fifo_size,
736d936e666SJohn Youn 			    hw->dev_nperio_tx_fifo_size);
7373c6aea73SSevak Arakelyan 		dwc2_check_param_tx_fifo_sizes(hsotg);
738d936e666SJohn Youn 	}
739d936e666SJohn Youn }
740d936e666SJohn Youn 
741323230efSJohn Youn /*
742323230efSJohn Youn  * Gets host hardware parameters. Forces host mode if not currently in
743323230efSJohn Youn  * host mode. Should be called immediately after a core soft reset in
744323230efSJohn Youn  * order to get the reset values.
745323230efSJohn Youn  */
746323230efSJohn Youn static void dwc2_get_host_hwparams(struct dwc2_hsotg *hsotg)
747323230efSJohn Youn {
748323230efSJohn Youn 	struct dwc2_hw_params *hw = &hsotg->hw_params;
749323230efSJohn Youn 	u32 gnptxfsiz;
750323230efSJohn Youn 	u32 hptxfsiz;
751323230efSJohn Youn 
752323230efSJohn Youn 	if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
753323230efSJohn Youn 		return;
754323230efSJohn Youn 
75513b1f8e2SVardan Mikayelyan 	dwc2_force_mode(hsotg, true);
756323230efSJohn Youn 
757f25c42b8SGevorg Sahakyan 	gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
758f25c42b8SGevorg Sahakyan 	hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
759323230efSJohn Youn 
760323230efSJohn Youn 	hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
761323230efSJohn Youn 				       FIFOSIZE_DEPTH_SHIFT;
762323230efSJohn Youn 	hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
763323230efSJohn Youn 				      FIFOSIZE_DEPTH_SHIFT;
764323230efSJohn Youn }
765323230efSJohn Youn 
766323230efSJohn Youn /*
767323230efSJohn Youn  * Gets device hardware parameters. Forces device mode if not
768323230efSJohn Youn  * currently in device mode. Should be called immediately after a core
769323230efSJohn Youn  * soft reset in order to get the reset values.
770323230efSJohn Youn  */
771323230efSJohn Youn static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
772323230efSJohn Youn {
773323230efSJohn Youn 	struct dwc2_hw_params *hw = &hsotg->hw_params;
774323230efSJohn Youn 	u32 gnptxfsiz;
7759273083aSMinas Harutyunyan 	int fifo, fifo_count;
776323230efSJohn Youn 
777323230efSJohn Youn 	if (hsotg->dr_mode == USB_DR_MODE_HOST)
778323230efSJohn Youn 		return;
779323230efSJohn Youn 
78013b1f8e2SVardan Mikayelyan 	dwc2_force_mode(hsotg, false);
781323230efSJohn Youn 
782f25c42b8SGevorg Sahakyan 	gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
783323230efSJohn Youn 
7849273083aSMinas Harutyunyan 	fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
7859273083aSMinas Harutyunyan 
7869273083aSMinas Harutyunyan 	for (fifo = 1; fifo <= fifo_count; fifo++) {
7879273083aSMinas Harutyunyan 		hw->g_tx_fifo_size[fifo] =
788f25c42b8SGevorg Sahakyan 			(dwc2_readl(hsotg, DPTXFSIZN(fifo)) &
7899273083aSMinas Harutyunyan 			 FIFOSIZE_DEPTH_MASK) >> FIFOSIZE_DEPTH_SHIFT;
7909273083aSMinas Harutyunyan 	}
7919273083aSMinas Harutyunyan 
792323230efSJohn Youn 	hw->dev_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
793323230efSJohn Youn 				       FIFOSIZE_DEPTH_SHIFT;
794323230efSJohn Youn }
795323230efSJohn Youn 
796323230efSJohn Youn /**
797bd37fbd5SLee Jones  * dwc2_get_hwparams() - During device initialization, read various hardware
798bd37fbd5SLee Jones  *                       configuration registers and interpret the contents.
7996fb914d7SGrigor Tovmasyan  *
8006fb914d7SGrigor Tovmasyan  * @hsotg: Programming view of the DWC_otg controller
8016fb914d7SGrigor Tovmasyan  *
802323230efSJohn Youn  */
803323230efSJohn Youn int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
804323230efSJohn Youn {
805323230efSJohn Youn 	struct dwc2_hw_params *hw = &hsotg->hw_params;
806323230efSJohn Youn 	unsigned int width;
807323230efSJohn Youn 	u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
808323230efSJohn Youn 	u32 grxfsiz;
809323230efSJohn Youn 
810f25c42b8SGevorg Sahakyan 	hwcfg1 = dwc2_readl(hsotg, GHWCFG1);
811f25c42b8SGevorg Sahakyan 	hwcfg2 = dwc2_readl(hsotg, GHWCFG2);
812f25c42b8SGevorg Sahakyan 	hwcfg3 = dwc2_readl(hsotg, GHWCFG3);
813f25c42b8SGevorg Sahakyan 	hwcfg4 = dwc2_readl(hsotg, GHWCFG4);
814f25c42b8SGevorg Sahakyan 	grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
815323230efSJohn Youn 
816323230efSJohn Youn 	/* hwcfg1 */
817323230efSJohn Youn 	hw->dev_ep_dirs = hwcfg1;
818323230efSJohn Youn 
819323230efSJohn Youn 	/* hwcfg2 */
820323230efSJohn Youn 	hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
821323230efSJohn Youn 		      GHWCFG2_OP_MODE_SHIFT;
822323230efSJohn Youn 	hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
823323230efSJohn Youn 		   GHWCFG2_ARCHITECTURE_SHIFT;
824323230efSJohn Youn 	hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
825323230efSJohn Youn 	hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
826323230efSJohn Youn 				GHWCFG2_NUM_HOST_CHAN_SHIFT);
827323230efSJohn Youn 	hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
828323230efSJohn Youn 			  GHWCFG2_HS_PHY_TYPE_SHIFT;
829323230efSJohn Youn 	hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
830323230efSJohn Youn 			  GHWCFG2_FS_PHY_TYPE_SHIFT;
831323230efSJohn Youn 	hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
832323230efSJohn Youn 			 GHWCFG2_NUM_DEV_EP_SHIFT;
833323230efSJohn Youn 	hw->nperio_tx_q_depth =
834323230efSJohn Youn 		(hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
835323230efSJohn Youn 		GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
836323230efSJohn Youn 	hw->host_perio_tx_q_depth =
837323230efSJohn Youn 		(hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
838323230efSJohn Youn 		GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
839323230efSJohn Youn 	hw->dev_token_q_depth =
840323230efSJohn Youn 		(hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
841323230efSJohn Youn 		GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
842323230efSJohn Youn 
843323230efSJohn Youn 	/* hwcfg3 */
844323230efSJohn Youn 	width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
845323230efSJohn Youn 		GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
846323230efSJohn Youn 	hw->max_transfer_size = (1 << (width + 11)) - 1;
847323230efSJohn Youn 	width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
848323230efSJohn Youn 		GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
849323230efSJohn Youn 	hw->max_packet_count = (1 << (width + 4)) - 1;
850323230efSJohn Youn 	hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
851323230efSJohn Youn 	hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
852323230efSJohn Youn 			      GHWCFG3_DFIFO_DEPTH_SHIFT;
8536f80b6deSSevak Arakelyan 	hw->lpm_mode = !!(hwcfg3 & GHWCFG3_OTG_LPM_EN);
854323230efSJohn Youn 
855323230efSJohn Youn 	/* hwcfg4 */
856323230efSJohn Youn 	hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
857323230efSJohn Youn 	hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
858323230efSJohn Youn 				  GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
8599273083aSMinas Harutyunyan 	hw->num_dev_in_eps = (hwcfg4 & GHWCFG4_NUM_IN_EPS_MASK) >>
8609273083aSMinas Harutyunyan 			     GHWCFG4_NUM_IN_EPS_SHIFT;
861323230efSJohn Youn 	hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
862323230efSJohn Youn 	hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
863631a2310SVardan Mikayelyan 	hw->hibernation = !!(hwcfg4 & GHWCFG4_HIBER);
864323230efSJohn Youn 	hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
865323230efSJohn Youn 				  GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
86666e77a24SRazmik Karapetyan 	hw->acg_enable = !!(hwcfg4 & GHWCFG4_ACG_SUPPORTED);
867b43ebc96SGrigor Tovmasyan 	hw->ipg_isoc_en = !!(hwcfg4 & GHWCFG4_IPG_ISOC_SUPPORTED);
868ca531bc2SGrigor Tovmasyan 	hw->service_interval_mode = !!(hwcfg4 &
869ca531bc2SGrigor Tovmasyan 				       GHWCFG4_SERVICE_INTERVAL_SUPPORTED);
870323230efSJohn Youn 
871323230efSJohn Youn 	/* fifo sizes */
872d1531319SJohn Youn 	hw->rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
873323230efSJohn Youn 				GRXFSIZ_DEPTH_SHIFT;
8749273083aSMinas Harutyunyan 	/*
8759273083aSMinas Harutyunyan 	 * Host specific hardware parameters. Reading these parameters
8769273083aSMinas Harutyunyan 	 * requires the controller to be in host mode. The mode will
8779273083aSMinas Harutyunyan 	 * be forced, if necessary, to read these values.
8789273083aSMinas Harutyunyan 	 */
8799273083aSMinas Harutyunyan 	dwc2_get_host_hwparams(hsotg);
8809273083aSMinas Harutyunyan 	dwc2_get_dev_hwparams(hsotg);
881323230efSJohn Youn 
882323230efSJohn Youn 	return 0;
883323230efSJohn Youn }
884323230efSJohn Youn 
8852e5db2c0SJeremy Linton typedef void (*set_params_cb)(struct dwc2_hsotg *data);
8862e5db2c0SJeremy Linton 
887334bbd4eSJohn Youn int dwc2_init_params(struct dwc2_hsotg *hsotg)
888334bbd4eSJohn Youn {
8897de1debcSJohn Youn 	const struct of_device_id *match;
8902e5db2c0SJeremy Linton 	set_params_cb set_params;
8917de1debcSJohn Youn 
892245977c9SJohn Youn 	dwc2_set_default_params(hsotg);
893f9f93cbbSJohn Youn 	dwc2_get_device_properties(hsotg);
894334bbd4eSJohn Youn 
8957de1debcSJohn Youn 	match = of_match_device(dwc2_of_match_table, hsotg->dev);
8967de1debcSJohn Youn 	if (match && match->data) {
8977de1debcSJohn Youn 		set_params = match->data;
8987de1debcSJohn Youn 		set_params(hsotg);
8992e5db2c0SJeremy Linton 	} else {
9002e5db2c0SJeremy Linton 		const struct acpi_device_id *amatch;
9012e5db2c0SJeremy Linton 
9022e5db2c0SJeremy Linton 		amatch = acpi_match_device(dwc2_acpi_match, hsotg->dev);
9032e5db2c0SJeremy Linton 		if (amatch && amatch->driver_data) {
9042e5db2c0SJeremy Linton 			set_params = (set_params_cb)amatch->driver_data;
9052e5db2c0SJeremy Linton 			set_params(hsotg);
9062e5db2c0SJeremy Linton 		}
9077de1debcSJohn Youn 	}
9087de1debcSJohn Youn 
909d936e666SJohn Youn 	dwc2_check_params(hsotg);
910d936e666SJohn Youn 
911334bbd4eSJohn Youn 	return 0;
912334bbd4eSJohn Youn }
913