xref: /openbmc/linux/drivers/usb/dwc2/params.c (revision 1112cf4c4109473fd8a30a4678d25f9321ef5d67)
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>
39323230efSJohn Youn 
40323230efSJohn Youn #include "core.h"
41323230efSJohn Youn 
427de1debcSJohn Youn static void dwc2_set_bcm_params(struct dwc2_hsotg *hsotg)
437de1debcSJohn Youn {
447de1debcSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
45323230efSJohn Youn 
467de1debcSJohn Youn 	p->host_rx_fifo_size = 774;
477de1debcSJohn Youn 	p->max_transfer_size = 65535;
487de1debcSJohn Youn 	p->max_packet_count = 511;
497de1debcSJohn Youn 	p->ahbcfg = 0x10;
507de1debcSJohn Youn }
51323230efSJohn Youn 
527de1debcSJohn Youn static void dwc2_set_his_params(struct dwc2_hsotg *hsotg)
537de1debcSJohn Youn {
547de1debcSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
55323230efSJohn Youn 
567de1debcSJohn Youn 	p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
577de1debcSJohn Youn 	p->speed = DWC2_SPEED_PARAM_HIGH;
587de1debcSJohn Youn 	p->host_rx_fifo_size = 512;
597de1debcSJohn Youn 	p->host_nperio_tx_fifo_size = 512;
607de1debcSJohn Youn 	p->host_perio_tx_fifo_size = 512;
617de1debcSJohn Youn 	p->max_transfer_size = 65535;
627de1debcSJohn Youn 	p->max_packet_count = 511;
637de1debcSJohn Youn 	p->host_channels = 16;
647de1debcSJohn Youn 	p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
657de1debcSJohn Youn 	p->phy_utmi_width = 8;
667de1debcSJohn Youn 	p->i2c_enable = false;
677de1debcSJohn Youn 	p->reload_ctl = false;
687de1debcSJohn Youn 	p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
697de1debcSJohn Youn 		GAHBCFG_HBSTLEN_SHIFT;
70ca8b0332SChen Yu 	p->change_speed_quirk = true;
71d98c624aSJohn Stultz 	p->power_down = false;
727de1debcSJohn Youn }
73323230efSJohn Youn 
7435a60541SMarek Szyprowski static void dwc2_set_s3c6400_params(struct dwc2_hsotg *hsotg)
7535a60541SMarek Szyprowski {
7635a60541SMarek Szyprowski 	struct dwc2_core_params *p = &hsotg->params;
7735a60541SMarek Szyprowski 
7835a60541SMarek Szyprowski 	p->power_down = 0;
79*1112cf4cSMarek Szyprowski 	p->phy_utmi_width = 8;
8035a60541SMarek Szyprowski }
8135a60541SMarek Szyprowski 
827de1debcSJohn Youn static void dwc2_set_rk_params(struct dwc2_hsotg *hsotg)
837de1debcSJohn Youn {
847de1debcSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
857de1debcSJohn Youn 
867de1debcSJohn Youn 	p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
877de1debcSJohn Youn 	p->host_rx_fifo_size = 525;
887de1debcSJohn Youn 	p->host_nperio_tx_fifo_size = 128;
897de1debcSJohn Youn 	p->host_perio_tx_fifo_size = 256;
907de1debcSJohn Youn 	p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
917de1debcSJohn Youn 		GAHBCFG_HBSTLEN_SHIFT;
92c216765dSSolidHal 	p->power_down = 0;
937de1debcSJohn Youn }
947de1debcSJohn Youn 
957de1debcSJohn Youn static void dwc2_set_ltq_params(struct dwc2_hsotg *hsotg)
967de1debcSJohn Youn {
977de1debcSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
987de1debcSJohn Youn 
997de1debcSJohn Youn 	p->otg_cap = 2;
1007de1debcSJohn Youn 	p->host_rx_fifo_size = 288;
1017de1debcSJohn Youn 	p->host_nperio_tx_fifo_size = 128;
1027de1debcSJohn Youn 	p->host_perio_tx_fifo_size = 96;
1037de1debcSJohn Youn 	p->max_transfer_size = 65535;
1047de1debcSJohn Youn 	p->max_packet_count = 511;
1057de1debcSJohn Youn 	p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
1067de1debcSJohn Youn 		GAHBCFG_HBSTLEN_SHIFT;
1077de1debcSJohn Youn }
1087de1debcSJohn Youn 
1097de1debcSJohn Youn static void dwc2_set_amlogic_params(struct dwc2_hsotg *hsotg)
1107de1debcSJohn Youn {
1117de1debcSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
1127de1debcSJohn Youn 
1137de1debcSJohn Youn 	p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
1147de1debcSJohn Youn 	p->speed = DWC2_SPEED_PARAM_HIGH;
1157de1debcSJohn Youn 	p->host_rx_fifo_size = 512;
1167de1debcSJohn Youn 	p->host_nperio_tx_fifo_size = 500;
1177de1debcSJohn Youn 	p->host_perio_tx_fifo_size = 500;
1187de1debcSJohn Youn 	p->host_channels = 16;
1197de1debcSJohn Youn 	p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
1207de1debcSJohn Youn 	p->ahbcfg = GAHBCFG_HBSTLEN_INCR8 <<
1217de1debcSJohn Youn 		GAHBCFG_HBSTLEN_SHIFT;
122cc10ce0cSMartin Blumenstingl 	p->power_down = DWC2_POWER_DOWN_PARAM_NONE;
1237de1debcSJohn Youn }
1247de1debcSJohn Youn 
125fc4e326eSNeil Armstrong static void dwc2_set_amlogic_g12a_params(struct dwc2_hsotg *hsotg)
126fc4e326eSNeil Armstrong {
127fc4e326eSNeil Armstrong 	struct dwc2_core_params *p = &hsotg->params;
128fc4e326eSNeil Armstrong 
129fc4e326eSNeil Armstrong 	p->lpm = false;
130fc4e326eSNeil Armstrong 	p->lpm_clock_gating = false;
131fc4e326eSNeil Armstrong 	p->besl = false;
132fc4e326eSNeil Armstrong 	p->hird_threshold_en = false;
133fc4e326eSNeil Armstrong }
134fc4e326eSNeil Armstrong 
1357de1debcSJohn Youn static void dwc2_set_amcc_params(struct dwc2_hsotg *hsotg)
1367de1debcSJohn Youn {
1377de1debcSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
1387de1debcSJohn Youn 
1397de1debcSJohn Youn 	p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT;
1407de1debcSJohn Youn }
141323230efSJohn Youn 
142e35b1350SBruno Herrera static void dwc2_set_stm32f4x9_fsotg_params(struct dwc2_hsotg *hsotg)
143e35b1350SBruno Herrera {
144e35b1350SBruno Herrera 	struct dwc2_core_params *p = &hsotg->params;
145e35b1350SBruno Herrera 
146e35b1350SBruno Herrera 	p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
147e35b1350SBruno Herrera 	p->speed = DWC2_SPEED_PARAM_FULL;
148e35b1350SBruno Herrera 	p->host_rx_fifo_size = 128;
149e35b1350SBruno Herrera 	p->host_nperio_tx_fifo_size = 96;
150e35b1350SBruno Herrera 	p->host_perio_tx_fifo_size = 96;
151e35b1350SBruno Herrera 	p->max_packet_count = 256;
152e35b1350SBruno Herrera 	p->phy_type = DWC2_PHY_TYPE_PARAM_FS;
153e35b1350SBruno Herrera 	p->i2c_enable = false;
154e35b1350SBruno Herrera 	p->activate_stm_fs_transceiver = true;
155e35b1350SBruno Herrera }
156e35b1350SBruno Herrera 
1571a149e35SAmelie Delaunay static void dwc2_set_stm32f7_hsotg_params(struct dwc2_hsotg *hsotg)
158d8fae8b9SAmelie Delaunay {
159d8fae8b9SAmelie Delaunay 	struct dwc2_core_params *p = &hsotg->params;
160d8fae8b9SAmelie Delaunay 
161d8fae8b9SAmelie Delaunay 	p->host_rx_fifo_size = 622;
162d8fae8b9SAmelie Delaunay 	p->host_nperio_tx_fifo_size = 128;
163d8fae8b9SAmelie Delaunay 	p->host_perio_tx_fifo_size = 256;
164d8fae8b9SAmelie Delaunay }
165d8fae8b9SAmelie Delaunay 
166323230efSJohn Youn const struct of_device_id dwc2_of_match_table[] = {
1677de1debcSJohn Youn 	{ .compatible = "brcm,bcm2835-usb", .data = dwc2_set_bcm_params },
1687de1debcSJohn Youn 	{ .compatible = "hisilicon,hi6220-usb", .data = dwc2_set_his_params  },
1697de1debcSJohn Youn 	{ .compatible = "rockchip,rk3066-usb", .data = dwc2_set_rk_params },
1707de1debcSJohn Youn 	{ .compatible = "lantiq,arx100-usb", .data = dwc2_set_ltq_params },
1717de1debcSJohn Youn 	{ .compatible = "lantiq,xrx200-usb", .data = dwc2_set_ltq_params },
1727de1debcSJohn Youn 	{ .compatible = "snps,dwc2" },
17335a60541SMarek Szyprowski 	{ .compatible = "samsung,s3c6400-hsotg",
17435a60541SMarek Szyprowski 	  .data = dwc2_set_s3c6400_params },
17555b644fdSMartin Blumenstingl 	{ .compatible = "amlogic,meson8-usb",
17655b644fdSMartin Blumenstingl 	  .data = dwc2_set_amlogic_params },
1777de1debcSJohn Youn 	{ .compatible = "amlogic,meson8b-usb",
1787de1debcSJohn Youn 	  .data = dwc2_set_amlogic_params },
1797de1debcSJohn Youn 	{ .compatible = "amlogic,meson-gxbb-usb",
1807de1debcSJohn Youn 	  .data = dwc2_set_amlogic_params },
181fc4e326eSNeil Armstrong 	{ .compatible = "amlogic,meson-g12a-usb",
182fc4e326eSNeil Armstrong 	  .data = dwc2_set_amlogic_g12a_params },
1837de1debcSJohn Youn 	{ .compatible = "amcc,dwc-otg", .data = dwc2_set_amcc_params },
184e35b1350SBruno Herrera 	{ .compatible = "st,stm32f4x9-fsotg",
185e35b1350SBruno Herrera 	  .data = dwc2_set_stm32f4x9_fsotg_params },
186e35b1350SBruno Herrera 	{ .compatible = "st,stm32f4x9-hsotg" },
1871a149e35SAmelie Delaunay 	{ .compatible = "st,stm32f7-hsotg",
1881a149e35SAmelie Delaunay 	  .data = dwc2_set_stm32f7_hsotg_params },
189323230efSJohn Youn 	{},
190323230efSJohn Youn };
191323230efSJohn Youn MODULE_DEVICE_TABLE(of, dwc2_of_match_table);
192323230efSJohn Youn 
193245977c9SJohn Youn static void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg)
19405ee799fSJohn Youn {
195245977c9SJohn Youn 	u8 val;
19605ee799fSJohn Youn 
197323230efSJohn Youn 	switch (hsotg->hw_params.op_mode) {
198323230efSJohn Youn 	case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
199323230efSJohn Youn 		val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
200323230efSJohn Youn 		break;
201323230efSJohn Youn 	case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
202323230efSJohn Youn 	case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
203323230efSJohn Youn 	case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
204323230efSJohn Youn 		val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
205323230efSJohn Youn 		break;
206323230efSJohn Youn 	default:
207323230efSJohn Youn 		val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
208323230efSJohn Youn 		break;
209323230efSJohn Youn 	}
210323230efSJohn Youn 
211bea8e86cSJohn Youn 	hsotg->params.otg_cap = val;
212323230efSJohn Youn }
213323230efSJohn Youn 
214245977c9SJohn Youn static void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg)
215323230efSJohn Youn {
216245977c9SJohn Youn 	int val;
217245977c9SJohn Youn 	u32 hs_phy_type = hsotg->hw_params.hs_phy_type;
218323230efSJohn Youn 
219323230efSJohn Youn 	val = DWC2_PHY_TYPE_PARAM_FS;
220323230efSJohn Youn 	if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
221323230efSJohn Youn 		if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
222323230efSJohn Youn 		    hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
223323230efSJohn Youn 			val = DWC2_PHY_TYPE_PARAM_UTMI;
224323230efSJohn Youn 		else
225323230efSJohn Youn 			val = DWC2_PHY_TYPE_PARAM_ULPI;
226323230efSJohn Youn 	}
227245977c9SJohn Youn 
228245977c9SJohn Youn 	if (dwc2_is_fs_iot(hsotg))
229245977c9SJohn Youn 		hsotg->params.phy_type = DWC2_PHY_TYPE_PARAM_FS;
230323230efSJohn Youn 
231bea8e86cSJohn Youn 	hsotg->params.phy_type = val;
232323230efSJohn Youn }
233323230efSJohn Youn 
234245977c9SJohn Youn static void dwc2_set_param_speed(struct dwc2_hsotg *hsotg)
235323230efSJohn Youn {
236245977c9SJohn Youn 	int val;
237323230efSJohn Youn 
238245977c9SJohn Youn 	val = hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS ?
239323230efSJohn Youn 		DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
240245977c9SJohn Youn 
241245977c9SJohn Youn 	if (dwc2_is_fs_iot(hsotg))
242245977c9SJohn Youn 		val = DWC2_SPEED_PARAM_FULL;
243245977c9SJohn Youn 
244245977c9SJohn Youn 	if (dwc2_is_hs_iot(hsotg))
245245977c9SJohn Youn 		val = DWC2_SPEED_PARAM_HIGH;
246323230efSJohn Youn 
247bea8e86cSJohn Youn 	hsotg->params.speed = val;
248323230efSJohn Youn }
249323230efSJohn Youn 
250245977c9SJohn Youn static void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
251323230efSJohn Youn {
252245977c9SJohn Youn 	int val;
253323230efSJohn Youn 
254323230efSJohn Youn 	val = (hsotg->hw_params.utmi_phy_data_width ==
255323230efSJohn Youn 	       GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
256323230efSJohn Youn 
257bea8e86cSJohn Youn 	hsotg->params.phy_utmi_width = val;
258323230efSJohn Youn }
259323230efSJohn Youn 
26005ee799fSJohn Youn static void dwc2_set_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
26105ee799fSJohn Youn {
26205ee799fSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
263c138ecfaSSevak Arakelyan 	int depth_average;
264c138ecfaSSevak Arakelyan 	int fifo_count;
265c138ecfaSSevak Arakelyan 	int i;
266c138ecfaSSevak Arakelyan 
267c138ecfaSSevak Arakelyan 	fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
26805ee799fSJohn Youn 
269245977c9SJohn Youn 	memset(p->g_tx_fifo_size, 0, sizeof(p->g_tx_fifo_size));
270c138ecfaSSevak Arakelyan 	depth_average = dwc2_hsotg_tx_fifo_average_depth(hsotg);
271c138ecfaSSevak Arakelyan 	for (i = 1; i <= fifo_count; i++)
272c138ecfaSSevak Arakelyan 		p->g_tx_fifo_size[i] = depth_average;
2739962b62fSJohn Youn }
2749962b62fSJohn Youn 
27503ea6d6eSJohn Youn static void dwc2_set_param_power_down(struct dwc2_hsotg *hsotg)
27603ea6d6eSJohn Youn {
27703ea6d6eSJohn Youn 	int val;
27803ea6d6eSJohn Youn 
27903ea6d6eSJohn Youn 	if (hsotg->hw_params.hibernation)
28003ea6d6eSJohn Youn 		val = 2;
28103ea6d6eSJohn Youn 	else if (hsotg->hw_params.power_optimized)
28203ea6d6eSJohn Youn 		val = 1;
28303ea6d6eSJohn Youn 	else
28403ea6d6eSJohn Youn 		val = 0;
28503ea6d6eSJohn Youn 
28603ea6d6eSJohn Youn 	hsotg->params.power_down = val;
28703ea6d6eSJohn Youn }
28803ea6d6eSJohn Youn 
28928b5c129SMinas Harutyunyan static void dwc2_set_param_lpm(struct dwc2_hsotg *hsotg)
29028b5c129SMinas Harutyunyan {
29128b5c129SMinas Harutyunyan 	struct dwc2_core_params *p = &hsotg->params;
29228b5c129SMinas Harutyunyan 
29328b5c129SMinas Harutyunyan 	p->lpm = hsotg->hw_params.lpm_mode;
29428b5c129SMinas Harutyunyan 	if (p->lpm) {
29528b5c129SMinas Harutyunyan 		p->lpm_clock_gating = true;
29628b5c129SMinas Harutyunyan 		p->besl = true;
29728b5c129SMinas Harutyunyan 		p->hird_threshold_en = true;
29828b5c129SMinas Harutyunyan 		p->hird_threshold = 4;
29928b5c129SMinas Harutyunyan 	} else {
30028b5c129SMinas Harutyunyan 		p->lpm_clock_gating = false;
30128b5c129SMinas Harutyunyan 		p->besl = false;
30228b5c129SMinas Harutyunyan 		p->hird_threshold_en = false;
30328b5c129SMinas Harutyunyan 	}
30428b5c129SMinas Harutyunyan }
30528b5c129SMinas Harutyunyan 
30605ee799fSJohn Youn /**
307245977c9SJohn Youn  * dwc2_set_default_params() - Set all core parameters to their
308245977c9SJohn Youn  * auto-detected default values.
3096fb914d7SGrigor Tovmasyan  *
3106fb914d7SGrigor Tovmasyan  * @hsotg: Programming view of the DWC_otg controller
3116fb914d7SGrigor Tovmasyan  *
312323230efSJohn Youn  */
313245977c9SJohn Youn static void dwc2_set_default_params(struct dwc2_hsotg *hsotg)
314323230efSJohn Youn {
31505ee799fSJohn Youn 	struct dwc2_hw_params *hw = &hsotg->hw_params;
31605ee799fSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
3176b66ce51SJohn Youn 	bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
318323230efSJohn Youn 
319245977c9SJohn Youn 	dwc2_set_param_otg_cap(hsotg);
320245977c9SJohn Youn 	dwc2_set_param_phy_type(hsotg);
321245977c9SJohn Youn 	dwc2_set_param_speed(hsotg);
322245977c9SJohn Youn 	dwc2_set_param_phy_utmi_width(hsotg);
32303ea6d6eSJohn Youn 	dwc2_set_param_power_down(hsotg);
32428b5c129SMinas Harutyunyan 	dwc2_set_param_lpm(hsotg);
325245977c9SJohn Youn 	p->phy_ulpi_ddr = false;
326245977c9SJohn Youn 	p->phy_ulpi_ext_vbus = false;
327245977c9SJohn Youn 
328245977c9SJohn Youn 	p->enable_dynamic_fifo = hw->enable_dynamic_fifo;
329245977c9SJohn Youn 	p->en_multiple_tx_fifo = hw->en_multiple_tx_fifo;
330245977c9SJohn Youn 	p->i2c_enable = hw->i2c_enable;
33166e77a24SRazmik Karapetyan 	p->acg_enable = hw->acg_enable;
332245977c9SJohn Youn 	p->ulpi_fs_ls = false;
333245977c9SJohn Youn 	p->ts_dline = false;
334245977c9SJohn Youn 	p->reload_ctl = (hw->snpsid >= DWC2_CORE_REV_2_92a);
335245977c9SJohn Youn 	p->uframe_sched = true;
336245977c9SJohn Youn 	p->external_id_pin_ctl = false;
337b43ebc96SGrigor Tovmasyan 	p->ipg_isoc_en = false;
338ca531bc2SGrigor Tovmasyan 	p->service_interval = false;
339245977c9SJohn Youn 	p->max_packet_count = hw->max_packet_count;
340245977c9SJohn Youn 	p->max_transfer_size = hw->max_transfer_size;
3411b52d2faSRazmik Karapetyan 	p->ahbcfg = GAHBCFG_HBSTLEN_INCR << GAHBCFG_HBSTLEN_SHIFT;
342f3a61e4eSGrigor Tovmasyan 	p->ref_clk_per = 33333;
343f3a61e4eSGrigor Tovmasyan 	p->sof_cnt_wkup_alert = 100;
344245977c9SJohn Youn 
3456b66ce51SJohn Youn 	if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
3466b66ce51SJohn Youn 	    (hsotg->dr_mode == USB_DR_MODE_OTG)) {
347245977c9SJohn Youn 		p->host_dma = dma_capable;
348245977c9SJohn Youn 		p->dma_desc_enable = false;
349245977c9SJohn Youn 		p->dma_desc_fs_enable = false;
350245977c9SJohn Youn 		p->host_support_fs_ls_low_power = false;
351245977c9SJohn Youn 		p->host_ls_low_power_phy_clk = false;
352245977c9SJohn Youn 		p->host_channels = hw->host_channels;
353245977c9SJohn Youn 		p->host_rx_fifo_size = hw->rx_fifo_size;
354245977c9SJohn Youn 		p->host_nperio_tx_fifo_size = hw->host_nperio_tx_fifo_size;
355245977c9SJohn Youn 		p->host_perio_tx_fifo_size = hw->host_perio_tx_fifo_size;
3566b66ce51SJohn Youn 	}
3576b66ce51SJohn Youn 
35805ee799fSJohn Youn 	if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
35905ee799fSJohn Youn 	    (hsotg->dr_mode == USB_DR_MODE_OTG)) {
360245977c9SJohn Youn 		p->g_dma = dma_capable;
361245977c9SJohn Youn 		p->g_dma_desc = hw->dma_desc_enable;
36205ee799fSJohn Youn 
36305ee799fSJohn Youn 		/*
36405ee799fSJohn Youn 		 * The values for g_rx_fifo_size (2048) and
36505ee799fSJohn Youn 		 * g_np_tx_fifo_size (1024) come from the legacy s3c
36605ee799fSJohn Youn 		 * gadget driver. These defaults have been hard-coded
36705ee799fSJohn Youn 		 * for some time so many platforms depend on these
36805ee799fSJohn Youn 		 * values. Leave them as defaults for now and only
36905ee799fSJohn Youn 		 * auto-detect if the hardware does not support the
37005ee799fSJohn Youn 		 * default.
37105ee799fSJohn Youn 		 */
372245977c9SJohn Youn 		p->g_rx_fifo_size = 2048;
373245977c9SJohn Youn 		p->g_np_tx_fifo_size = 1024;
37405ee799fSJohn Youn 		dwc2_set_param_tx_fifo_sizes(hsotg);
37505ee799fSJohn Youn 	}
376323230efSJohn Youn }
377323230efSJohn Youn 
378f9f93cbbSJohn Youn /**
379f9f93cbbSJohn Youn  * dwc2_get_device_properties() - Read in device properties.
380f9f93cbbSJohn Youn  *
3816fb914d7SGrigor Tovmasyan  * @hsotg: Programming view of the DWC_otg controller
3826fb914d7SGrigor Tovmasyan  *
383f9f93cbbSJohn Youn  * Read in the device properties and adjust core parameters if needed.
384f9f93cbbSJohn Youn  */
385f9f93cbbSJohn Youn static void dwc2_get_device_properties(struct dwc2_hsotg *hsotg)
386f9f93cbbSJohn Youn {
387f9f93cbbSJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
388f9f93cbbSJohn Youn 	int num;
389f9f93cbbSJohn Youn 
390f9f93cbbSJohn Youn 	if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
391f9f93cbbSJohn Youn 	    (hsotg->dr_mode == USB_DR_MODE_OTG)) {
392f9f93cbbSJohn Youn 		device_property_read_u32(hsotg->dev, "g-rx-fifo-size",
393f9f93cbbSJohn Youn 					 &p->g_rx_fifo_size);
394f9f93cbbSJohn Youn 
395f9f93cbbSJohn Youn 		device_property_read_u32(hsotg->dev, "g-np-tx-fifo-size",
396f9f93cbbSJohn Youn 					 &p->g_np_tx_fifo_size);
397f9f93cbbSJohn Youn 
398f9f93cbbSJohn Youn 		num = device_property_read_u32_array(hsotg->dev,
399f9f93cbbSJohn Youn 						     "g-tx-fifo-size",
400f9f93cbbSJohn Youn 						     NULL, 0);
401f9f93cbbSJohn Youn 
402f9f93cbbSJohn Youn 		if (num > 0) {
403f9f93cbbSJohn Youn 			num = min(num, 15);
404f9f93cbbSJohn Youn 			memset(p->g_tx_fifo_size, 0,
405f9f93cbbSJohn Youn 			       sizeof(p->g_tx_fifo_size));
406f9f93cbbSJohn Youn 			device_property_read_u32_array(hsotg->dev,
407f9f93cbbSJohn Youn 						       "g-tx-fifo-size",
408f9f93cbbSJohn Youn 						       &p->g_tx_fifo_size[1],
409f9f93cbbSJohn Youn 						       num);
410f9f93cbbSJohn Youn 		}
411f9f93cbbSJohn Youn 	}
412b11633c4SDinh Nguyen 
413b11633c4SDinh Nguyen 	if (of_find_property(hsotg->dev->of_node, "disable-over-current", NULL))
414b11633c4SDinh Nguyen 		p->oc_disable = true;
415f9f93cbbSJohn Youn }
416f9f93cbbSJohn Youn 
417d936e666SJohn Youn static void dwc2_check_param_otg_cap(struct dwc2_hsotg *hsotg)
418d936e666SJohn Youn {
419d936e666SJohn Youn 	int valid = 1;
420d936e666SJohn Youn 
421d936e666SJohn Youn 	switch (hsotg->params.otg_cap) {
422d936e666SJohn Youn 	case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
423d936e666SJohn Youn 		if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
424d936e666SJohn Youn 			valid = 0;
425d936e666SJohn Youn 		break;
426d936e666SJohn Youn 	case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
427d936e666SJohn Youn 		switch (hsotg->hw_params.op_mode) {
428d936e666SJohn Youn 		case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
429d936e666SJohn Youn 		case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
430d936e666SJohn Youn 		case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
431d936e666SJohn Youn 		case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
432d936e666SJohn Youn 			break;
433d936e666SJohn Youn 		default:
434d936e666SJohn Youn 			valid = 0;
435d936e666SJohn Youn 			break;
436d936e666SJohn Youn 		}
437d936e666SJohn Youn 		break;
438d936e666SJohn Youn 	case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
439d936e666SJohn Youn 		/* always valid */
440d936e666SJohn Youn 		break;
441d936e666SJohn Youn 	default:
442d936e666SJohn Youn 		valid = 0;
443d936e666SJohn Youn 		break;
444d936e666SJohn Youn 	}
445d936e666SJohn Youn 
446d936e666SJohn Youn 	if (!valid)
447d936e666SJohn Youn 		dwc2_set_param_otg_cap(hsotg);
448d936e666SJohn Youn }
449d936e666SJohn Youn 
450d936e666SJohn Youn static void dwc2_check_param_phy_type(struct dwc2_hsotg *hsotg)
451d936e666SJohn Youn {
452d936e666SJohn Youn 	int valid = 0;
453d936e666SJohn Youn 	u32 hs_phy_type;
454d936e666SJohn Youn 	u32 fs_phy_type;
455d936e666SJohn Youn 
456d936e666SJohn Youn 	hs_phy_type = hsotg->hw_params.hs_phy_type;
457d936e666SJohn Youn 	fs_phy_type = hsotg->hw_params.fs_phy_type;
458d936e666SJohn Youn 
459d936e666SJohn Youn 	switch (hsotg->params.phy_type) {
460d936e666SJohn Youn 	case DWC2_PHY_TYPE_PARAM_FS:
461d936e666SJohn Youn 		if (fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
462d936e666SJohn Youn 			valid = 1;
463d936e666SJohn Youn 		break;
464d936e666SJohn Youn 	case DWC2_PHY_TYPE_PARAM_UTMI:
465d936e666SJohn Youn 		if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
466d936e666SJohn Youn 		    (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
467d936e666SJohn Youn 			valid = 1;
468d936e666SJohn Youn 		break;
469d936e666SJohn Youn 	case DWC2_PHY_TYPE_PARAM_ULPI:
470d936e666SJohn Youn 		if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
471d936e666SJohn Youn 		    (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
472d936e666SJohn Youn 			valid = 1;
473d936e666SJohn Youn 		break;
474d936e666SJohn Youn 	default:
475d936e666SJohn Youn 		break;
476d936e666SJohn Youn 	}
477d936e666SJohn Youn 
478d936e666SJohn Youn 	if (!valid)
479d936e666SJohn Youn 		dwc2_set_param_phy_type(hsotg);
480d936e666SJohn Youn }
481d936e666SJohn Youn 
482d936e666SJohn Youn static void dwc2_check_param_speed(struct dwc2_hsotg *hsotg)
483d936e666SJohn Youn {
484d936e666SJohn Youn 	int valid = 1;
485d936e666SJohn Youn 	int phy_type = hsotg->params.phy_type;
486d936e666SJohn Youn 	int speed = hsotg->params.speed;
487d936e666SJohn Youn 
488d936e666SJohn Youn 	switch (speed) {
489d936e666SJohn Youn 	case DWC2_SPEED_PARAM_HIGH:
490d936e666SJohn Youn 		if ((hsotg->params.speed == DWC2_SPEED_PARAM_HIGH) &&
491d936e666SJohn Youn 		    (phy_type == DWC2_PHY_TYPE_PARAM_FS))
492d936e666SJohn Youn 			valid = 0;
493d936e666SJohn Youn 		break;
494d936e666SJohn Youn 	case DWC2_SPEED_PARAM_FULL:
495d936e666SJohn Youn 	case DWC2_SPEED_PARAM_LOW:
496d936e666SJohn Youn 		break;
497d936e666SJohn Youn 	default:
498d936e666SJohn Youn 		valid = 0;
499d936e666SJohn Youn 		break;
500d936e666SJohn Youn 	}
501d936e666SJohn Youn 
502d936e666SJohn Youn 	if (!valid)
503d936e666SJohn Youn 		dwc2_set_param_speed(hsotg);
504d936e666SJohn Youn }
505d936e666SJohn Youn 
506d936e666SJohn Youn static void dwc2_check_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
507d936e666SJohn Youn {
508d936e666SJohn Youn 	int valid = 0;
509d936e666SJohn Youn 	int param = hsotg->params.phy_utmi_width;
510d936e666SJohn Youn 	int width = hsotg->hw_params.utmi_phy_data_width;
511d936e666SJohn Youn 
512d936e666SJohn Youn 	switch (width) {
513d936e666SJohn Youn 	case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
514d936e666SJohn Youn 		valid = (param == 8);
515d936e666SJohn Youn 		break;
516d936e666SJohn Youn 	case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
517d936e666SJohn Youn 		valid = (param == 16);
518d936e666SJohn Youn 		break;
519d936e666SJohn Youn 	case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
520d936e666SJohn Youn 		valid = (param == 8 || param == 16);
521d936e666SJohn Youn 		break;
522d936e666SJohn Youn 	}
523d936e666SJohn Youn 
524d936e666SJohn Youn 	if (!valid)
525d936e666SJohn Youn 		dwc2_set_param_phy_utmi_width(hsotg);
526d936e666SJohn Youn }
527d936e666SJohn Youn 
528631a2310SVardan Mikayelyan static void dwc2_check_param_power_down(struct dwc2_hsotg *hsotg)
529631a2310SVardan Mikayelyan {
530631a2310SVardan Mikayelyan 	int param = hsotg->params.power_down;
531631a2310SVardan Mikayelyan 
532631a2310SVardan Mikayelyan 	switch (param) {
533631a2310SVardan Mikayelyan 	case DWC2_POWER_DOWN_PARAM_NONE:
534631a2310SVardan Mikayelyan 		break;
535631a2310SVardan Mikayelyan 	case DWC2_POWER_DOWN_PARAM_PARTIAL:
536631a2310SVardan Mikayelyan 		if (hsotg->hw_params.power_optimized)
537631a2310SVardan Mikayelyan 			break;
538631a2310SVardan Mikayelyan 		dev_dbg(hsotg->dev,
539631a2310SVardan Mikayelyan 			"Partial power down isn't supported by HW\n");
540631a2310SVardan Mikayelyan 		param = DWC2_POWER_DOWN_PARAM_NONE;
541631a2310SVardan Mikayelyan 		break;
542631a2310SVardan Mikayelyan 	case DWC2_POWER_DOWN_PARAM_HIBERNATION:
543631a2310SVardan Mikayelyan 		if (hsotg->hw_params.hibernation)
544631a2310SVardan Mikayelyan 			break;
545631a2310SVardan Mikayelyan 		dev_dbg(hsotg->dev,
546631a2310SVardan Mikayelyan 			"Hibernation isn't supported by HW\n");
547631a2310SVardan Mikayelyan 		param = DWC2_POWER_DOWN_PARAM_NONE;
548631a2310SVardan Mikayelyan 		break;
549631a2310SVardan Mikayelyan 	default:
550631a2310SVardan Mikayelyan 		dev_err(hsotg->dev,
551631a2310SVardan Mikayelyan 			"%s: Invalid parameter power_down=%d\n",
552631a2310SVardan Mikayelyan 			__func__, param);
553631a2310SVardan Mikayelyan 		param = DWC2_POWER_DOWN_PARAM_NONE;
554631a2310SVardan Mikayelyan 		break;
555631a2310SVardan Mikayelyan 	}
556631a2310SVardan Mikayelyan 
557631a2310SVardan Mikayelyan 	hsotg->params.power_down = param;
558631a2310SVardan Mikayelyan }
559631a2310SVardan Mikayelyan 
5603c6aea73SSevak Arakelyan static void dwc2_check_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
5613c6aea73SSevak Arakelyan {
5623c6aea73SSevak Arakelyan 	int fifo_count;
5633c6aea73SSevak Arakelyan 	int fifo;
5643c6aea73SSevak Arakelyan 	int min;
5653c6aea73SSevak Arakelyan 	u32 total = 0;
5663c6aea73SSevak Arakelyan 	u32 dptxfszn;
5673c6aea73SSevak Arakelyan 
5683c6aea73SSevak Arakelyan 	fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
5693c6aea73SSevak Arakelyan 	min = hsotg->hw_params.en_multiple_tx_fifo ? 16 : 4;
5703c6aea73SSevak Arakelyan 
5713c6aea73SSevak Arakelyan 	for (fifo = 1; fifo <= fifo_count; fifo++)
5723c6aea73SSevak Arakelyan 		total += hsotg->params.g_tx_fifo_size[fifo];
5733c6aea73SSevak Arakelyan 
5743c6aea73SSevak Arakelyan 	if (total > dwc2_hsotg_tx_fifo_total_depth(hsotg) || !total) {
5753c6aea73SSevak Arakelyan 		dev_warn(hsotg->dev, "%s: Invalid parameter g-tx-fifo-size, setting to default average\n",
5763c6aea73SSevak Arakelyan 			 __func__);
5773c6aea73SSevak Arakelyan 		dwc2_set_param_tx_fifo_sizes(hsotg);
5783c6aea73SSevak Arakelyan 	}
5793c6aea73SSevak Arakelyan 
5803c6aea73SSevak Arakelyan 	for (fifo = 1; fifo <= fifo_count; fifo++) {
5819273083aSMinas Harutyunyan 		dptxfszn = hsotg->hw_params.g_tx_fifo_size[fifo];
5823c6aea73SSevak Arakelyan 
5833c6aea73SSevak Arakelyan 		if (hsotg->params.g_tx_fifo_size[fifo] < min ||
5843c6aea73SSevak Arakelyan 		    hsotg->params.g_tx_fifo_size[fifo] >  dptxfszn) {
5853c6aea73SSevak Arakelyan 			dev_warn(hsotg->dev, "%s: Invalid parameter g_tx_fifo_size[%d]=%d\n",
5863c6aea73SSevak Arakelyan 				 __func__, fifo,
5873c6aea73SSevak Arakelyan 				 hsotg->params.g_tx_fifo_size[fifo]);
5883c6aea73SSevak Arakelyan 			hsotg->params.g_tx_fifo_size[fifo] = dptxfszn;
5893c6aea73SSevak Arakelyan 		}
5903c6aea73SSevak Arakelyan 	}
5913c6aea73SSevak Arakelyan }
5923c6aea73SSevak Arakelyan 
593d936e666SJohn Youn #define CHECK_RANGE(_param, _min, _max, _def) do {			\
59447265c06SGrigor Tovmasyan 		if ((int)(hsotg->params._param) < (_min) ||		\
595d936e666SJohn Youn 		    (hsotg->params._param) > (_max)) {			\
596d936e666SJohn Youn 			dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
597d936e666SJohn Youn 				 __func__, #_param, hsotg->params._param); \
598d936e666SJohn Youn 			hsotg->params._param = (_def);			\
599d936e666SJohn Youn 		}							\
600d936e666SJohn Youn 	} while (0)
601d936e666SJohn Youn 
602d936e666SJohn Youn #define CHECK_BOOL(_param, _check) do {					\
603d936e666SJohn Youn 		if (hsotg->params._param && !(_check)) {		\
604d936e666SJohn Youn 			dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
605d936e666SJohn Youn 				 __func__, #_param, hsotg->params._param); \
606d936e666SJohn Youn 			hsotg->params._param = false;			\
607d936e666SJohn Youn 		}							\
608d936e666SJohn Youn 	} while (0)
609d936e666SJohn Youn 
610d936e666SJohn Youn static void dwc2_check_params(struct dwc2_hsotg *hsotg)
611d936e666SJohn Youn {
612d936e666SJohn Youn 	struct dwc2_hw_params *hw = &hsotg->hw_params;
613d936e666SJohn Youn 	struct dwc2_core_params *p = &hsotg->params;
614d936e666SJohn Youn 	bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
615d936e666SJohn Youn 
616d936e666SJohn Youn 	dwc2_check_param_otg_cap(hsotg);
617d936e666SJohn Youn 	dwc2_check_param_phy_type(hsotg);
618d936e666SJohn Youn 	dwc2_check_param_speed(hsotg);
619d936e666SJohn Youn 	dwc2_check_param_phy_utmi_width(hsotg);
620631a2310SVardan Mikayelyan 	dwc2_check_param_power_down(hsotg);
621d936e666SJohn Youn 	CHECK_BOOL(enable_dynamic_fifo, hw->enable_dynamic_fifo);
622d936e666SJohn Youn 	CHECK_BOOL(en_multiple_tx_fifo, hw->en_multiple_tx_fifo);
623d936e666SJohn Youn 	CHECK_BOOL(i2c_enable, hw->i2c_enable);
624b43ebc96SGrigor Tovmasyan 	CHECK_BOOL(ipg_isoc_en, hw->ipg_isoc_en);
62566e77a24SRazmik Karapetyan 	CHECK_BOOL(acg_enable, hw->acg_enable);
626d936e666SJohn Youn 	CHECK_BOOL(reload_ctl, (hsotg->hw_params.snpsid > DWC2_CORE_REV_2_92a));
6276f80b6deSSevak Arakelyan 	CHECK_BOOL(lpm, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_80a));
6286f80b6deSSevak Arakelyan 	CHECK_BOOL(lpm, hw->lpm_mode);
6296f80b6deSSevak Arakelyan 	CHECK_BOOL(lpm_clock_gating, hsotg->params.lpm);
6306f80b6deSSevak Arakelyan 	CHECK_BOOL(besl, hsotg->params.lpm);
6316f80b6deSSevak Arakelyan 	CHECK_BOOL(besl, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a));
6326f80b6deSSevak Arakelyan 	CHECK_BOOL(hird_threshold_en, hsotg->params.lpm);
6336f80b6deSSevak Arakelyan 	CHECK_RANGE(hird_threshold, 0, hsotg->params.besl ? 12 : 7, 0);
634ca531bc2SGrigor Tovmasyan 	CHECK_BOOL(service_interval, hw->service_interval_mode);
635d936e666SJohn Youn 	CHECK_RANGE(max_packet_count,
636d936e666SJohn Youn 		    15, hw->max_packet_count,
637d936e666SJohn Youn 		    hw->max_packet_count);
638d936e666SJohn Youn 	CHECK_RANGE(max_transfer_size,
639d936e666SJohn Youn 		    2047, hw->max_transfer_size,
640d936e666SJohn Youn 		    hw->max_transfer_size);
641d936e666SJohn Youn 
642d936e666SJohn Youn 	if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
643d936e666SJohn Youn 	    (hsotg->dr_mode == USB_DR_MODE_OTG)) {
644d936e666SJohn Youn 		CHECK_BOOL(host_dma, dma_capable);
645d936e666SJohn Youn 		CHECK_BOOL(dma_desc_enable, p->host_dma);
646d936e666SJohn Youn 		CHECK_BOOL(dma_desc_fs_enable, p->dma_desc_enable);
647d936e666SJohn Youn 		CHECK_BOOL(host_ls_low_power_phy_clk,
648d936e666SJohn Youn 			   p->phy_type == DWC2_PHY_TYPE_PARAM_FS);
649d936e666SJohn Youn 		CHECK_RANGE(host_channels,
650d936e666SJohn Youn 			    1, hw->host_channels,
651d936e666SJohn Youn 			    hw->host_channels);
652d936e666SJohn Youn 		CHECK_RANGE(host_rx_fifo_size,
653d936e666SJohn Youn 			    16, hw->rx_fifo_size,
654d936e666SJohn Youn 			    hw->rx_fifo_size);
655d936e666SJohn Youn 		CHECK_RANGE(host_nperio_tx_fifo_size,
656d936e666SJohn Youn 			    16, hw->host_nperio_tx_fifo_size,
657d936e666SJohn Youn 			    hw->host_nperio_tx_fifo_size);
658d936e666SJohn Youn 		CHECK_RANGE(host_perio_tx_fifo_size,
659d936e666SJohn Youn 			    16, hw->host_perio_tx_fifo_size,
660d936e666SJohn Youn 			    hw->host_perio_tx_fifo_size);
661d936e666SJohn Youn 	}
662d936e666SJohn Youn 
663d936e666SJohn Youn 	if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
664d936e666SJohn Youn 	    (hsotg->dr_mode == USB_DR_MODE_OTG)) {
665d936e666SJohn Youn 		CHECK_BOOL(g_dma, dma_capable);
666d936e666SJohn Youn 		CHECK_BOOL(g_dma_desc, (p->g_dma && hw->dma_desc_enable));
667d936e666SJohn Youn 		CHECK_RANGE(g_rx_fifo_size,
668d936e666SJohn Youn 			    16, hw->rx_fifo_size,
669d936e666SJohn Youn 			    hw->rx_fifo_size);
670d936e666SJohn Youn 		CHECK_RANGE(g_np_tx_fifo_size,
671d936e666SJohn Youn 			    16, hw->dev_nperio_tx_fifo_size,
672d936e666SJohn Youn 			    hw->dev_nperio_tx_fifo_size);
6733c6aea73SSevak Arakelyan 		dwc2_check_param_tx_fifo_sizes(hsotg);
674d936e666SJohn Youn 	}
675d936e666SJohn Youn }
676d936e666SJohn Youn 
677323230efSJohn Youn /*
678323230efSJohn Youn  * Gets host hardware parameters. Forces host mode if not currently in
679323230efSJohn Youn  * host mode. Should be called immediately after a core soft reset in
680323230efSJohn Youn  * order to get the reset values.
681323230efSJohn Youn  */
682323230efSJohn Youn static void dwc2_get_host_hwparams(struct dwc2_hsotg *hsotg)
683323230efSJohn Youn {
684323230efSJohn Youn 	struct dwc2_hw_params *hw = &hsotg->hw_params;
685323230efSJohn Youn 	u32 gnptxfsiz;
686323230efSJohn Youn 	u32 hptxfsiz;
687323230efSJohn Youn 
688323230efSJohn Youn 	if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
689323230efSJohn Youn 		return;
690323230efSJohn Youn 
69113b1f8e2SVardan Mikayelyan 	dwc2_force_mode(hsotg, true);
692323230efSJohn Youn 
693f25c42b8SGevorg Sahakyan 	gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
694f25c42b8SGevorg Sahakyan 	hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
695323230efSJohn Youn 
696323230efSJohn Youn 	hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
697323230efSJohn Youn 				       FIFOSIZE_DEPTH_SHIFT;
698323230efSJohn Youn 	hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
699323230efSJohn Youn 				      FIFOSIZE_DEPTH_SHIFT;
700323230efSJohn Youn }
701323230efSJohn Youn 
702323230efSJohn Youn /*
703323230efSJohn Youn  * Gets device hardware parameters. Forces device mode if not
704323230efSJohn Youn  * currently in device mode. Should be called immediately after a core
705323230efSJohn Youn  * soft reset in order to get the reset values.
706323230efSJohn Youn  */
707323230efSJohn Youn static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
708323230efSJohn Youn {
709323230efSJohn Youn 	struct dwc2_hw_params *hw = &hsotg->hw_params;
710323230efSJohn Youn 	u32 gnptxfsiz;
7119273083aSMinas Harutyunyan 	int fifo, fifo_count;
712323230efSJohn Youn 
713323230efSJohn Youn 	if (hsotg->dr_mode == USB_DR_MODE_HOST)
714323230efSJohn Youn 		return;
715323230efSJohn Youn 
71613b1f8e2SVardan Mikayelyan 	dwc2_force_mode(hsotg, false);
717323230efSJohn Youn 
718f25c42b8SGevorg Sahakyan 	gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
719323230efSJohn Youn 
7209273083aSMinas Harutyunyan 	fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
7219273083aSMinas Harutyunyan 
7229273083aSMinas Harutyunyan 	for (fifo = 1; fifo <= fifo_count; fifo++) {
7239273083aSMinas Harutyunyan 		hw->g_tx_fifo_size[fifo] =
724f25c42b8SGevorg Sahakyan 			(dwc2_readl(hsotg, DPTXFSIZN(fifo)) &
7259273083aSMinas Harutyunyan 			 FIFOSIZE_DEPTH_MASK) >> FIFOSIZE_DEPTH_SHIFT;
7269273083aSMinas Harutyunyan 	}
7279273083aSMinas Harutyunyan 
728323230efSJohn Youn 	hw->dev_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
729323230efSJohn Youn 				       FIFOSIZE_DEPTH_SHIFT;
730323230efSJohn Youn }
731323230efSJohn Youn 
732323230efSJohn Youn /**
733323230efSJohn Youn  * During device initialization, read various hardware configuration
734323230efSJohn Youn  * registers and interpret the contents.
7356fb914d7SGrigor Tovmasyan  *
7366fb914d7SGrigor Tovmasyan  * @hsotg: Programming view of the DWC_otg controller
7376fb914d7SGrigor Tovmasyan  *
738323230efSJohn Youn  */
739323230efSJohn Youn int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
740323230efSJohn Youn {
741323230efSJohn Youn 	struct dwc2_hw_params *hw = &hsotg->hw_params;
742323230efSJohn Youn 	unsigned int width;
743323230efSJohn Youn 	u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
744323230efSJohn Youn 	u32 grxfsiz;
745323230efSJohn Youn 
746323230efSJohn Youn 	/*
747323230efSJohn Youn 	 * Attempt to ensure this device is really a DWC_otg Controller.
748323230efSJohn Youn 	 * Read and verify the GSNPSID register contents. The value should be
749d14ccabaSGevorg Sahakyan 	 * 0x45f4xxxx, 0x5531xxxx or 0x5532xxxx
750323230efSJohn Youn 	 */
751d14ccabaSGevorg Sahakyan 
752f25c42b8SGevorg Sahakyan 	hw->snpsid = dwc2_readl(hsotg, GSNPSID);
753d14ccabaSGevorg Sahakyan 	if ((hw->snpsid & GSNPSID_ID_MASK) != DWC2_OTG_ID &&
754d14ccabaSGevorg Sahakyan 	    (hw->snpsid & GSNPSID_ID_MASK) != DWC2_FS_IOT_ID &&
755d14ccabaSGevorg Sahakyan 	    (hw->snpsid & GSNPSID_ID_MASK) != DWC2_HS_IOT_ID) {
756323230efSJohn Youn 		dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
757323230efSJohn Youn 			hw->snpsid);
758323230efSJohn Youn 		return -ENODEV;
759323230efSJohn Youn 	}
760323230efSJohn Youn 
761323230efSJohn Youn 	dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
762323230efSJohn Youn 		hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
763323230efSJohn Youn 		hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
764323230efSJohn Youn 
765f25c42b8SGevorg Sahakyan 	hwcfg1 = dwc2_readl(hsotg, GHWCFG1);
766f25c42b8SGevorg Sahakyan 	hwcfg2 = dwc2_readl(hsotg, GHWCFG2);
767f25c42b8SGevorg Sahakyan 	hwcfg3 = dwc2_readl(hsotg, GHWCFG3);
768f25c42b8SGevorg Sahakyan 	hwcfg4 = dwc2_readl(hsotg, GHWCFG4);
769f25c42b8SGevorg Sahakyan 	grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
770323230efSJohn Youn 
771323230efSJohn Youn 	/* hwcfg1 */
772323230efSJohn Youn 	hw->dev_ep_dirs = hwcfg1;
773323230efSJohn Youn 
774323230efSJohn Youn 	/* hwcfg2 */
775323230efSJohn Youn 	hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
776323230efSJohn Youn 		      GHWCFG2_OP_MODE_SHIFT;
777323230efSJohn Youn 	hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
778323230efSJohn Youn 		   GHWCFG2_ARCHITECTURE_SHIFT;
779323230efSJohn Youn 	hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
780323230efSJohn Youn 	hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
781323230efSJohn Youn 				GHWCFG2_NUM_HOST_CHAN_SHIFT);
782323230efSJohn Youn 	hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
783323230efSJohn Youn 			  GHWCFG2_HS_PHY_TYPE_SHIFT;
784323230efSJohn Youn 	hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
785323230efSJohn Youn 			  GHWCFG2_FS_PHY_TYPE_SHIFT;
786323230efSJohn Youn 	hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
787323230efSJohn Youn 			 GHWCFG2_NUM_DEV_EP_SHIFT;
788323230efSJohn Youn 	hw->nperio_tx_q_depth =
789323230efSJohn Youn 		(hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
790323230efSJohn Youn 		GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
791323230efSJohn Youn 	hw->host_perio_tx_q_depth =
792323230efSJohn Youn 		(hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
793323230efSJohn Youn 		GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
794323230efSJohn Youn 	hw->dev_token_q_depth =
795323230efSJohn Youn 		(hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
796323230efSJohn Youn 		GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
797323230efSJohn Youn 
798323230efSJohn Youn 	/* hwcfg3 */
799323230efSJohn Youn 	width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
800323230efSJohn Youn 		GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
801323230efSJohn Youn 	hw->max_transfer_size = (1 << (width + 11)) - 1;
802323230efSJohn Youn 	width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
803323230efSJohn Youn 		GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
804323230efSJohn Youn 	hw->max_packet_count = (1 << (width + 4)) - 1;
805323230efSJohn Youn 	hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
806323230efSJohn Youn 	hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
807323230efSJohn Youn 			      GHWCFG3_DFIFO_DEPTH_SHIFT;
8086f80b6deSSevak Arakelyan 	hw->lpm_mode = !!(hwcfg3 & GHWCFG3_OTG_LPM_EN);
809323230efSJohn Youn 
810323230efSJohn Youn 	/* hwcfg4 */
811323230efSJohn Youn 	hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
812323230efSJohn Youn 	hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
813323230efSJohn Youn 				  GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
8149273083aSMinas Harutyunyan 	hw->num_dev_in_eps = (hwcfg4 & GHWCFG4_NUM_IN_EPS_MASK) >>
8159273083aSMinas Harutyunyan 			     GHWCFG4_NUM_IN_EPS_SHIFT;
816323230efSJohn Youn 	hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
817323230efSJohn Youn 	hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
818631a2310SVardan Mikayelyan 	hw->hibernation = !!(hwcfg4 & GHWCFG4_HIBER);
819323230efSJohn Youn 	hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
820323230efSJohn Youn 				  GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
82166e77a24SRazmik Karapetyan 	hw->acg_enable = !!(hwcfg4 & GHWCFG4_ACG_SUPPORTED);
822b43ebc96SGrigor Tovmasyan 	hw->ipg_isoc_en = !!(hwcfg4 & GHWCFG4_IPG_ISOC_SUPPORTED);
823ca531bc2SGrigor Tovmasyan 	hw->service_interval_mode = !!(hwcfg4 &
824ca531bc2SGrigor Tovmasyan 				       GHWCFG4_SERVICE_INTERVAL_SUPPORTED);
825323230efSJohn Youn 
826323230efSJohn Youn 	/* fifo sizes */
827d1531319SJohn Youn 	hw->rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
828323230efSJohn Youn 				GRXFSIZ_DEPTH_SHIFT;
8299273083aSMinas Harutyunyan 	/*
8309273083aSMinas Harutyunyan 	 * Host specific hardware parameters. Reading these parameters
8319273083aSMinas Harutyunyan 	 * requires the controller to be in host mode. The mode will
8329273083aSMinas Harutyunyan 	 * be forced, if necessary, to read these values.
8339273083aSMinas Harutyunyan 	 */
8349273083aSMinas Harutyunyan 	dwc2_get_host_hwparams(hsotg);
8359273083aSMinas Harutyunyan 	dwc2_get_dev_hwparams(hsotg);
836323230efSJohn Youn 
837323230efSJohn Youn 	return 0;
838323230efSJohn Youn }
839323230efSJohn Youn 
840334bbd4eSJohn Youn int dwc2_init_params(struct dwc2_hsotg *hsotg)
841334bbd4eSJohn Youn {
8427de1debcSJohn Youn 	const struct of_device_id *match;
8437de1debcSJohn Youn 	void (*set_params)(void *data);
8447de1debcSJohn Youn 
845245977c9SJohn Youn 	dwc2_set_default_params(hsotg);
846f9f93cbbSJohn Youn 	dwc2_get_device_properties(hsotg);
847334bbd4eSJohn Youn 
8487de1debcSJohn Youn 	match = of_match_device(dwc2_of_match_table, hsotg->dev);
8497de1debcSJohn Youn 	if (match && match->data) {
8507de1debcSJohn Youn 		set_params = match->data;
8517de1debcSJohn Youn 		set_params(hsotg);
8527de1debcSJohn Youn 	}
8537de1debcSJohn Youn 
854d936e666SJohn Youn 	dwc2_check_params(hsotg);
855d936e666SJohn Youn 
856334bbd4eSJohn Youn 	return 0;
857334bbd4eSJohn Youn }
858