blob: daf0f9ac714917a327d2ebb8e8bd599a1bc7ee20 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
John Youn323230e2016-11-03 17:55:50 -07002/*
3 * Copyright (C) 2004-2016 Synopsys, Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The names of the above-listed copyright holders may not be used
15 * to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * ALTERNATIVELY, this software may be distributed under the terms of the
19 * GNU General Public License ("GPL") as published by the Free Software
20 * Foundation; either version 2 of the License, or (at your option) any
21 * later version.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/of_device.h>
39
40#include "core.h"
41
John Youn7de1deb2017-01-23 14:57:04 -080042static void dwc2_set_bcm_params(struct dwc2_hsotg *hsotg)
43{
44 struct dwc2_core_params *p = &hsotg->params;
John Youn323230e2016-11-03 17:55:50 -070045
John Youn7de1deb2017-01-23 14:57:04 -080046 p->host_rx_fifo_size = 774;
John Youn7de1deb2017-01-23 14:57:04 -080047 p->max_transfer_size = 65535;
48 p->max_packet_count = 511;
John Youn7de1deb2017-01-23 14:57:04 -080049 p->ahbcfg = 0x10;
50 p->uframe_sched = false;
51}
John Youn323230e2016-11-03 17:55:50 -070052
John Youn7de1deb2017-01-23 14:57:04 -080053static void dwc2_set_his_params(struct dwc2_hsotg *hsotg)
54{
55 struct dwc2_core_params *p = &hsotg->params;
John Youn323230e2016-11-03 17:55:50 -070056
John Youn7de1deb2017-01-23 14:57:04 -080057 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
58 p->speed = DWC2_SPEED_PARAM_HIGH;
59 p->host_rx_fifo_size = 512;
60 p->host_nperio_tx_fifo_size = 512;
61 p->host_perio_tx_fifo_size = 512;
62 p->max_transfer_size = 65535;
63 p->max_packet_count = 511;
64 p->host_channels = 16;
65 p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
66 p->phy_utmi_width = 8;
67 p->i2c_enable = false;
John Youn7de1deb2017-01-23 14:57:04 -080068 p->reload_ctl = false;
69 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
70 GAHBCFG_HBSTLEN_SHIFT;
71 p->uframe_sched = false;
Chen Yuca8b0332017-01-23 15:00:18 -080072 p->change_speed_quirk = true;
John Youn7de1deb2017-01-23 14:57:04 -080073}
John Youn323230e2016-11-03 17:55:50 -070074
John Youn7de1deb2017-01-23 14:57:04 -080075static void dwc2_set_rk_params(struct dwc2_hsotg *hsotg)
76{
77 struct dwc2_core_params *p = &hsotg->params;
78
79 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
80 p->host_rx_fifo_size = 525;
81 p->host_nperio_tx_fifo_size = 128;
82 p->host_perio_tx_fifo_size = 256;
83 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
84 GAHBCFG_HBSTLEN_SHIFT;
85}
86
87static void dwc2_set_ltq_params(struct dwc2_hsotg *hsotg)
88{
89 struct dwc2_core_params *p = &hsotg->params;
90
91 p->otg_cap = 2;
92 p->host_rx_fifo_size = 288;
93 p->host_nperio_tx_fifo_size = 128;
94 p->host_perio_tx_fifo_size = 96;
95 p->max_transfer_size = 65535;
96 p->max_packet_count = 511;
97 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
98 GAHBCFG_HBSTLEN_SHIFT;
99}
100
101static void dwc2_set_amlogic_params(struct dwc2_hsotg *hsotg)
102{
103 struct dwc2_core_params *p = &hsotg->params;
104
105 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
106 p->speed = DWC2_SPEED_PARAM_HIGH;
107 p->host_rx_fifo_size = 512;
108 p->host_nperio_tx_fifo_size = 500;
109 p->host_perio_tx_fifo_size = 500;
110 p->host_channels = 16;
111 p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
112 p->ahbcfg = GAHBCFG_HBSTLEN_INCR8 <<
113 GAHBCFG_HBSTLEN_SHIFT;
114 p->uframe_sched = false;
115}
116
117static void dwc2_set_amcc_params(struct dwc2_hsotg *hsotg)
118{
119 struct dwc2_core_params *p = &hsotg->params;
120
121 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT;
122}
John Youn323230e2016-11-03 17:55:50 -0700123
Bruno Herrerae35b1352017-01-31 23:25:43 -0200124static void dwc2_set_stm32f4x9_fsotg_params(struct dwc2_hsotg *hsotg)
125{
126 struct dwc2_core_params *p = &hsotg->params;
127
128 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
129 p->speed = DWC2_SPEED_PARAM_FULL;
130 p->host_rx_fifo_size = 128;
131 p->host_nperio_tx_fifo_size = 96;
132 p->host_perio_tx_fifo_size = 96;
133 p->max_packet_count = 256;
134 p->phy_type = DWC2_PHY_TYPE_PARAM_FS;
135 p->i2c_enable = false;
136 p->uframe_sched = false;
137 p->activate_stm_fs_transceiver = true;
138}
139
Amelie Delaunayd8fae8b2017-08-17 11:33:01 +0200140static void dwc2_set_stm32f7xx_hsotg_params(struct dwc2_hsotg *hsotg)
141{
142 struct dwc2_core_params *p = &hsotg->params;
143
144 p->host_rx_fifo_size = 622;
145 p->host_nperio_tx_fifo_size = 128;
146 p->host_perio_tx_fifo_size = 256;
147}
148
John Youn323230e2016-11-03 17:55:50 -0700149const struct of_device_id dwc2_of_match_table[] = {
John Youn7de1deb2017-01-23 14:57:04 -0800150 { .compatible = "brcm,bcm2835-usb", .data = dwc2_set_bcm_params },
151 { .compatible = "hisilicon,hi6220-usb", .data = dwc2_set_his_params },
152 { .compatible = "rockchip,rk3066-usb", .data = dwc2_set_rk_params },
153 { .compatible = "lantiq,arx100-usb", .data = dwc2_set_ltq_params },
154 { .compatible = "lantiq,xrx200-usb", .data = dwc2_set_ltq_params },
155 { .compatible = "snps,dwc2" },
156 { .compatible = "samsung,s3c6400-hsotg" },
Martin Blumenstingl55b644f2017-05-06 19:37:45 +0200157 { .compatible = "amlogic,meson8-usb",
158 .data = dwc2_set_amlogic_params },
John Youn7de1deb2017-01-23 14:57:04 -0800159 { .compatible = "amlogic,meson8b-usb",
160 .data = dwc2_set_amlogic_params },
161 { .compatible = "amlogic,meson-gxbb-usb",
162 .data = dwc2_set_amlogic_params },
163 { .compatible = "amcc,dwc-otg", .data = dwc2_set_amcc_params },
Bruno Herrerae35b1352017-01-31 23:25:43 -0200164 { .compatible = "st,stm32f4x9-fsotg",
165 .data = dwc2_set_stm32f4x9_fsotg_params },
166 { .compatible = "st,stm32f4x9-hsotg" },
Amelie Delaunayd8fae8b2017-08-17 11:33:01 +0200167 { .compatible = "st,stm32f7xx-hsotg",
168 .data = dwc2_set_stm32f7xx_hsotg_params },
John Youn323230e2016-11-03 17:55:50 -0700169 {},
170};
171MODULE_DEVICE_TABLE(of, dwc2_of_match_table);
172
John Youn245977c2017-01-23 14:55:14 -0800173static void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg)
John Youn05ee7992016-11-03 17:56:05 -0700174{
John Youn245977c2017-01-23 14:55:14 -0800175 u8 val;
John Youn05ee7992016-11-03 17:56:05 -0700176
John Youn245977c2017-01-23 14:55:14 -0800177 switch (hsotg->hw_params.op_mode) {
178 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
179 val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
John Youn05ee7992016-11-03 17:56:05 -0700180 break;
John Youn245977c2017-01-23 14:55:14 -0800181 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
182 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
183 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
184 val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
John Youn05ee7992016-11-03 17:56:05 -0700185 break;
186 default:
John Youn245977c2017-01-23 14:55:14 -0800187 val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
John Youn05ee7992016-11-03 17:56:05 -0700188 break;
John Youn323230e2016-11-03 17:55:50 -0700189 }
190
John Younbea8e862016-11-03 17:55:53 -0700191 hsotg->params.otg_cap = val;
John Youn323230e2016-11-03 17:55:50 -0700192}
193
John Youn245977c2017-01-23 14:55:14 -0800194static void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg)
John Youn323230e2016-11-03 17:55:50 -0700195{
John Youn245977c2017-01-23 14:55:14 -0800196 int val;
197 u32 hs_phy_type = hsotg->hw_params.hs_phy_type;
John Youn323230e2016-11-03 17:55:50 -0700198
John Youn245977c2017-01-23 14:55:14 -0800199 val = DWC2_PHY_TYPE_PARAM_FS;
200 if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
201 if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
202 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
203 val = DWC2_PHY_TYPE_PARAM_UTMI;
204 else
205 val = DWC2_PHY_TYPE_PARAM_ULPI;
John Youn323230e2016-11-03 17:55:50 -0700206 }
207
John Youn245977c2017-01-23 14:55:14 -0800208 if (dwc2_is_fs_iot(hsotg))
209 hsotg->params.phy_type = DWC2_PHY_TYPE_PARAM_FS;
John Youn323230e2016-11-03 17:55:50 -0700210
John Younbea8e862016-11-03 17:55:53 -0700211 hsotg->params.phy_type = val;
John Youn323230e2016-11-03 17:55:50 -0700212}
213
John Youn245977c2017-01-23 14:55:14 -0800214static void dwc2_set_param_speed(struct dwc2_hsotg *hsotg)
John Youn323230e2016-11-03 17:55:50 -0700215{
John Youn245977c2017-01-23 14:55:14 -0800216 int val;
John Youn323230e2016-11-03 17:55:50 -0700217
John Youn245977c2017-01-23 14:55:14 -0800218 val = hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS ?
219 DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
John Youn323230e2016-11-03 17:55:50 -0700220
John Youn245977c2017-01-23 14:55:14 -0800221 if (dwc2_is_fs_iot(hsotg))
222 val = DWC2_SPEED_PARAM_FULL;
John Youn323230e2016-11-03 17:55:50 -0700223
John Youn245977c2017-01-23 14:55:14 -0800224 if (dwc2_is_hs_iot(hsotg))
225 val = DWC2_SPEED_PARAM_HIGH;
John Youn323230e2016-11-03 17:55:50 -0700226
John Younbea8e862016-11-03 17:55:53 -0700227 hsotg->params.speed = val;
John Youn323230e2016-11-03 17:55:50 -0700228}
229
John Youn245977c2017-01-23 14:55:14 -0800230static void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
John Youn323230e2016-11-03 17:55:50 -0700231{
John Youn245977c2017-01-23 14:55:14 -0800232 int val;
John Youn323230e2016-11-03 17:55:50 -0700233
John Youn245977c2017-01-23 14:55:14 -0800234 val = (hsotg->hw_params.utmi_phy_data_width ==
235 GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
John Youn323230e2016-11-03 17:55:50 -0700236
John Younbea8e862016-11-03 17:55:53 -0700237 hsotg->params.phy_utmi_width = val;
John Youn323230e2016-11-03 17:55:50 -0700238}
239
John Youn05ee7992016-11-03 17:56:05 -0700240static void dwc2_set_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
241{
John Youn05ee7992016-11-03 17:56:05 -0700242 struct dwc2_core_params *p = &hsotg->params;
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800243 int depth_average;
244 int fifo_count;
245 int i;
246
247 fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
John Youn05ee7992016-11-03 17:56:05 -0700248
249 memset(p->g_tx_fifo_size, 0, sizeof(p->g_tx_fifo_size));
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800250 depth_average = dwc2_hsotg_tx_fifo_average_depth(hsotg);
251 for (i = 1; i <= fifo_count; i++)
252 p->g_tx_fifo_size[i] = depth_average;
John Youn9962b622016-11-09 19:27:40 -0800253}
254
John Youn05ee7992016-11-03 17:56:05 -0700255/**
John Youn245977c2017-01-23 14:55:14 -0800256 * dwc2_set_default_params() - Set all core parameters to their
257 * auto-detected default values.
John Youn323230e2016-11-03 17:55:50 -0700258 */
John Youn245977c2017-01-23 14:55:14 -0800259static void dwc2_set_default_params(struct dwc2_hsotg *hsotg)
John Youn323230e2016-11-03 17:55:50 -0700260{
John Youn05ee7992016-11-03 17:56:05 -0700261 struct dwc2_hw_params *hw = &hsotg->hw_params;
262 struct dwc2_core_params *p = &hsotg->params;
John Youn6b66ce52016-11-03 17:56:12 -0700263 bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
John Youn323230e2016-11-03 17:55:50 -0700264
John Youn245977c2017-01-23 14:55:14 -0800265 dwc2_set_param_otg_cap(hsotg);
266 dwc2_set_param_phy_type(hsotg);
267 dwc2_set_param_speed(hsotg);
268 dwc2_set_param_phy_utmi_width(hsotg);
269 p->phy_ulpi_ddr = false;
270 p->phy_ulpi_ext_vbus = false;
271
272 p->enable_dynamic_fifo = hw->enable_dynamic_fifo;
273 p->en_multiple_tx_fifo = hw->en_multiple_tx_fifo;
274 p->i2c_enable = hw->i2c_enable;
Razmik Karapetyan66e77a22018-01-24 17:40:29 +0400275 p->acg_enable = hw->acg_enable;
John Youn245977c2017-01-23 14:55:14 -0800276 p->ulpi_fs_ls = false;
277 p->ts_dline = false;
278 p->reload_ctl = (hw->snpsid >= DWC2_CORE_REV_2_92a);
279 p->uframe_sched = true;
280 p->external_id_pin_ctl = false;
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +0400281 p->power_down = false;
Sevak Arakelyan6f80b6d2018-01-24 17:41:48 +0400282 p->lpm = true;
283 p->lpm_clock_gating = true;
284 p->besl = true;
285 p->hird_threshold_en = true;
286 p->hird_threshold = 4;
John Youn245977c2017-01-23 14:55:14 -0800287 p->max_packet_count = hw->max_packet_count;
288 p->max_transfer_size = hw->max_transfer_size;
Razmik Karapetyan1b52d2f2018-01-19 14:40:23 +0400289 p->ahbcfg = GAHBCFG_HBSTLEN_INCR << GAHBCFG_HBSTLEN_SHIFT;
John Youn245977c2017-01-23 14:55:14 -0800290
John Youn6b66ce52016-11-03 17:56:12 -0700291 if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
292 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
John Youn245977c2017-01-23 14:55:14 -0800293 p->host_dma = dma_capable;
294 p->dma_desc_enable = false;
295 p->dma_desc_fs_enable = false;
296 p->host_support_fs_ls_low_power = false;
297 p->host_ls_low_power_phy_clk = false;
298 p->host_channels = hw->host_channels;
299 p->host_rx_fifo_size = hw->rx_fifo_size;
300 p->host_nperio_tx_fifo_size = hw->host_nperio_tx_fifo_size;
301 p->host_perio_tx_fifo_size = hw->host_perio_tx_fifo_size;
John Youn6b66ce52016-11-03 17:56:12 -0700302 }
303
John Youn05ee7992016-11-03 17:56:05 -0700304 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
305 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
John Youn245977c2017-01-23 14:55:14 -0800306 p->g_dma = dma_capable;
307 p->g_dma_desc = hw->dma_desc_enable;
John Youn05ee7992016-11-03 17:56:05 -0700308
309 /*
310 * The values for g_rx_fifo_size (2048) and
311 * g_np_tx_fifo_size (1024) come from the legacy s3c
312 * gadget driver. These defaults have been hard-coded
313 * for some time so many platforms depend on these
314 * values. Leave them as defaults for now and only
315 * auto-detect if the hardware does not support the
316 * default.
317 */
John Youn245977c2017-01-23 14:55:14 -0800318 p->g_rx_fifo_size = 2048;
319 p->g_np_tx_fifo_size = 1024;
John Youn05ee7992016-11-03 17:56:05 -0700320 dwc2_set_param_tx_fifo_sizes(hsotg);
321 }
John Youn323230e2016-11-03 17:55:50 -0700322}
323
John Younf9f93cb2017-01-23 14:55:35 -0800324/**
325 * dwc2_get_device_properties() - Read in device properties.
326 *
327 * Read in the device properties and adjust core parameters if needed.
328 */
329static void dwc2_get_device_properties(struct dwc2_hsotg *hsotg)
330{
331 struct dwc2_core_params *p = &hsotg->params;
332 int num;
333
334 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
335 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
336 device_property_read_u32(hsotg->dev, "g-rx-fifo-size",
337 &p->g_rx_fifo_size);
338
339 device_property_read_u32(hsotg->dev, "g-np-tx-fifo-size",
340 &p->g_np_tx_fifo_size);
341
342 num = device_property_read_u32_array(hsotg->dev,
343 "g-tx-fifo-size",
344 NULL, 0);
345
346 if (num > 0) {
347 num = min(num, 15);
348 memset(p->g_tx_fifo_size, 0,
349 sizeof(p->g_tx_fifo_size));
350 device_property_read_u32_array(hsotg->dev,
351 "g-tx-fifo-size",
352 &p->g_tx_fifo_size[1],
353 num);
354 }
355 }
Dinh Nguyenb11633c2017-10-16 08:57:18 -0500356
357 if (of_find_property(hsotg->dev->of_node, "disable-over-current", NULL))
358 p->oc_disable = true;
John Younf9f93cb2017-01-23 14:55:35 -0800359}
360
John Yound936e662017-01-23 14:56:43 -0800361static void dwc2_check_param_otg_cap(struct dwc2_hsotg *hsotg)
362{
363 int valid = 1;
364
365 switch (hsotg->params.otg_cap) {
366 case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
367 if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
368 valid = 0;
369 break;
370 case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
371 switch (hsotg->hw_params.op_mode) {
372 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
373 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
374 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
375 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
376 break;
377 default:
378 valid = 0;
379 break;
380 }
381 break;
382 case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
383 /* always valid */
384 break;
385 default:
386 valid = 0;
387 break;
388 }
389
390 if (!valid)
391 dwc2_set_param_otg_cap(hsotg);
392}
393
394static void dwc2_check_param_phy_type(struct dwc2_hsotg *hsotg)
395{
396 int valid = 0;
397 u32 hs_phy_type;
398 u32 fs_phy_type;
399
400 hs_phy_type = hsotg->hw_params.hs_phy_type;
401 fs_phy_type = hsotg->hw_params.fs_phy_type;
402
403 switch (hsotg->params.phy_type) {
404 case DWC2_PHY_TYPE_PARAM_FS:
405 if (fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
406 valid = 1;
407 break;
408 case DWC2_PHY_TYPE_PARAM_UTMI:
409 if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
410 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
411 valid = 1;
412 break;
413 case DWC2_PHY_TYPE_PARAM_ULPI:
414 if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
415 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
416 valid = 1;
417 break;
418 default:
419 break;
420 }
421
422 if (!valid)
423 dwc2_set_param_phy_type(hsotg);
424}
425
426static void dwc2_check_param_speed(struct dwc2_hsotg *hsotg)
427{
428 int valid = 1;
429 int phy_type = hsotg->params.phy_type;
430 int speed = hsotg->params.speed;
431
432 switch (speed) {
433 case DWC2_SPEED_PARAM_HIGH:
434 if ((hsotg->params.speed == DWC2_SPEED_PARAM_HIGH) &&
435 (phy_type == DWC2_PHY_TYPE_PARAM_FS))
436 valid = 0;
437 break;
438 case DWC2_SPEED_PARAM_FULL:
439 case DWC2_SPEED_PARAM_LOW:
440 break;
441 default:
442 valid = 0;
443 break;
444 }
445
446 if (!valid)
447 dwc2_set_param_speed(hsotg);
448}
449
450static void dwc2_check_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
451{
452 int valid = 0;
453 int param = hsotg->params.phy_utmi_width;
454 int width = hsotg->hw_params.utmi_phy_data_width;
455
456 switch (width) {
457 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
458 valid = (param == 8);
459 break;
460 case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
461 valid = (param == 16);
462 break;
463 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
464 valid = (param == 8 || param == 16);
465 break;
466 }
467
468 if (!valid)
469 dwc2_set_param_phy_utmi_width(hsotg);
470}
471
Vardan Mikayelyan631a2312018-02-16 14:07:05 +0400472static void dwc2_check_param_power_down(struct dwc2_hsotg *hsotg)
473{
474 int param = hsotg->params.power_down;
475
476 switch (param) {
477 case DWC2_POWER_DOWN_PARAM_NONE:
478 break;
479 case DWC2_POWER_DOWN_PARAM_PARTIAL:
480 if (hsotg->hw_params.power_optimized)
481 break;
482 dev_dbg(hsotg->dev,
483 "Partial power down isn't supported by HW\n");
484 param = DWC2_POWER_DOWN_PARAM_NONE;
485 break;
486 case DWC2_POWER_DOWN_PARAM_HIBERNATION:
487 if (hsotg->hw_params.hibernation)
488 break;
489 dev_dbg(hsotg->dev,
490 "Hibernation isn't supported by HW\n");
491 param = DWC2_POWER_DOWN_PARAM_NONE;
492 break;
493 default:
494 dev_err(hsotg->dev,
495 "%s: Invalid parameter power_down=%d\n",
496 __func__, param);
497 param = DWC2_POWER_DOWN_PARAM_NONE;
498 break;
499 }
500
501 hsotg->params.power_down = param;
502}
503
Sevak Arakelyan3c6aea72017-01-23 15:01:45 -0800504static void dwc2_check_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
505{
506 int fifo_count;
507 int fifo;
508 int min;
509 u32 total = 0;
510 u32 dptxfszn;
511
512 fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
513 min = hsotg->hw_params.en_multiple_tx_fifo ? 16 : 4;
514
515 for (fifo = 1; fifo <= fifo_count; fifo++)
516 total += hsotg->params.g_tx_fifo_size[fifo];
517
518 if (total > dwc2_hsotg_tx_fifo_total_depth(hsotg) || !total) {
519 dev_warn(hsotg->dev, "%s: Invalid parameter g-tx-fifo-size, setting to default average\n",
520 __func__);
521 dwc2_set_param_tx_fifo_sizes(hsotg);
522 }
523
524 for (fifo = 1; fifo <= fifo_count; fifo++) {
Minas Harutyunyan92730832017-11-30 12:16:37 +0400525 dptxfszn = hsotg->hw_params.g_tx_fifo_size[fifo];
Sevak Arakelyan3c6aea72017-01-23 15:01:45 -0800526
527 if (hsotg->params.g_tx_fifo_size[fifo] < min ||
528 hsotg->params.g_tx_fifo_size[fifo] > dptxfszn) {
529 dev_warn(hsotg->dev, "%s: Invalid parameter g_tx_fifo_size[%d]=%d\n",
530 __func__, fifo,
531 hsotg->params.g_tx_fifo_size[fifo]);
532 hsotg->params.g_tx_fifo_size[fifo] = dptxfszn;
533 }
534 }
535}
536
John Yound936e662017-01-23 14:56:43 -0800537#define CHECK_RANGE(_param, _min, _max, _def) do { \
538 if ((hsotg->params._param) < (_min) || \
539 (hsotg->params._param) > (_max)) { \
540 dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
541 __func__, #_param, hsotg->params._param); \
542 hsotg->params._param = (_def); \
543 } \
544 } while (0)
545
546#define CHECK_BOOL(_param, _check) do { \
547 if (hsotg->params._param && !(_check)) { \
548 dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
549 __func__, #_param, hsotg->params._param); \
550 hsotg->params._param = false; \
551 } \
552 } while (0)
553
554static void dwc2_check_params(struct dwc2_hsotg *hsotg)
555{
556 struct dwc2_hw_params *hw = &hsotg->hw_params;
557 struct dwc2_core_params *p = &hsotg->params;
558 bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
559
560 dwc2_check_param_otg_cap(hsotg);
561 dwc2_check_param_phy_type(hsotg);
562 dwc2_check_param_speed(hsotg);
563 dwc2_check_param_phy_utmi_width(hsotg);
Vardan Mikayelyan631a2312018-02-16 14:07:05 +0400564 dwc2_check_param_power_down(hsotg);
John Yound936e662017-01-23 14:56:43 -0800565 CHECK_BOOL(enable_dynamic_fifo, hw->enable_dynamic_fifo);
566 CHECK_BOOL(en_multiple_tx_fifo, hw->en_multiple_tx_fifo);
567 CHECK_BOOL(i2c_enable, hw->i2c_enable);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +0400568 CHECK_BOOL(acg_enable, hw->acg_enable);
John Yound936e662017-01-23 14:56:43 -0800569 CHECK_BOOL(reload_ctl, (hsotg->hw_params.snpsid > DWC2_CORE_REV_2_92a));
Sevak Arakelyan6f80b6d2018-01-24 17:41:48 +0400570 CHECK_BOOL(lpm, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_80a));
571 CHECK_BOOL(lpm, hw->lpm_mode);
572 CHECK_BOOL(lpm_clock_gating, hsotg->params.lpm);
573 CHECK_BOOL(besl, hsotg->params.lpm);
574 CHECK_BOOL(besl, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a));
575 CHECK_BOOL(hird_threshold_en, hsotg->params.lpm);
576 CHECK_RANGE(hird_threshold, 0, hsotg->params.besl ? 12 : 7, 0);
John Yound936e662017-01-23 14:56:43 -0800577 CHECK_RANGE(max_packet_count,
578 15, hw->max_packet_count,
579 hw->max_packet_count);
580 CHECK_RANGE(max_transfer_size,
581 2047, hw->max_transfer_size,
582 hw->max_transfer_size);
583
584 if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
585 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
586 CHECK_BOOL(host_dma, dma_capable);
587 CHECK_BOOL(dma_desc_enable, p->host_dma);
588 CHECK_BOOL(dma_desc_fs_enable, p->dma_desc_enable);
589 CHECK_BOOL(host_ls_low_power_phy_clk,
590 p->phy_type == DWC2_PHY_TYPE_PARAM_FS);
591 CHECK_RANGE(host_channels,
592 1, hw->host_channels,
593 hw->host_channels);
594 CHECK_RANGE(host_rx_fifo_size,
595 16, hw->rx_fifo_size,
596 hw->rx_fifo_size);
597 CHECK_RANGE(host_nperio_tx_fifo_size,
598 16, hw->host_nperio_tx_fifo_size,
599 hw->host_nperio_tx_fifo_size);
600 CHECK_RANGE(host_perio_tx_fifo_size,
601 16, hw->host_perio_tx_fifo_size,
602 hw->host_perio_tx_fifo_size);
603 }
604
605 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
606 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
607 CHECK_BOOL(g_dma, dma_capable);
608 CHECK_BOOL(g_dma_desc, (p->g_dma && hw->dma_desc_enable));
609 CHECK_RANGE(g_rx_fifo_size,
610 16, hw->rx_fifo_size,
611 hw->rx_fifo_size);
612 CHECK_RANGE(g_np_tx_fifo_size,
613 16, hw->dev_nperio_tx_fifo_size,
614 hw->dev_nperio_tx_fifo_size);
Sevak Arakelyan3c6aea72017-01-23 15:01:45 -0800615 dwc2_check_param_tx_fifo_sizes(hsotg);
John Yound936e662017-01-23 14:56:43 -0800616 }
617}
618
John Youn323230e2016-11-03 17:55:50 -0700619/*
620 * Gets host hardware parameters. Forces host mode if not currently in
621 * host mode. Should be called immediately after a core soft reset in
622 * order to get the reset values.
623 */
624static void dwc2_get_host_hwparams(struct dwc2_hsotg *hsotg)
625{
626 struct dwc2_hw_params *hw = &hsotg->hw_params;
627 u32 gnptxfsiz;
628 u32 hptxfsiz;
629 bool forced;
630
631 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
632 return;
633
634 forced = dwc2_force_mode_if_needed(hsotg, true);
635
636 gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
637 hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
John Youn323230e2016-11-03 17:55:50 -0700638
639 if (forced)
640 dwc2_clear_force_mode(hsotg);
641
642 hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
643 FIFOSIZE_DEPTH_SHIFT;
644 hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
645 FIFOSIZE_DEPTH_SHIFT;
646}
647
648/*
649 * Gets device hardware parameters. Forces device mode if not
650 * currently in device mode. Should be called immediately after a core
651 * soft reset in order to get the reset values.
652 */
653static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
654{
655 struct dwc2_hw_params *hw = &hsotg->hw_params;
656 bool forced;
657 u32 gnptxfsiz;
Minas Harutyunyan92730832017-11-30 12:16:37 +0400658 int fifo, fifo_count;
John Youn323230e2016-11-03 17:55:50 -0700659
660 if (hsotg->dr_mode == USB_DR_MODE_HOST)
661 return;
662
663 forced = dwc2_force_mode_if_needed(hsotg, false);
664
665 gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
John Youn323230e2016-11-03 17:55:50 -0700666
Minas Harutyunyan92730832017-11-30 12:16:37 +0400667 fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
668
669 for (fifo = 1; fifo <= fifo_count; fifo++) {
670 hw->g_tx_fifo_size[fifo] =
671 (dwc2_readl(hsotg->regs + DPTXFSIZN(fifo)) &
672 FIFOSIZE_DEPTH_MASK) >> FIFOSIZE_DEPTH_SHIFT;
673 }
674
John Youn323230e2016-11-03 17:55:50 -0700675 if (forced)
676 dwc2_clear_force_mode(hsotg);
677
678 hw->dev_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
679 FIFOSIZE_DEPTH_SHIFT;
680}
681
682/**
683 * During device initialization, read various hardware configuration
684 * registers and interpret the contents.
685 */
686int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
687{
688 struct dwc2_hw_params *hw = &hsotg->hw_params;
689 unsigned int width;
690 u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
691 u32 grxfsiz;
692
693 /*
694 * Attempt to ensure this device is really a DWC_otg Controller.
695 * Read and verify the GSNPSID register contents. The value should be
Gevorg Sahakyand14ccab2018-01-16 16:21:46 +0400696 * 0x45f4xxxx, 0x5531xxxx or 0x5532xxxx
John Youn323230e2016-11-03 17:55:50 -0700697 */
Gevorg Sahakyand14ccab2018-01-16 16:21:46 +0400698
John Youn323230e2016-11-03 17:55:50 -0700699 hw->snpsid = dwc2_readl(hsotg->regs + GSNPSID);
Gevorg Sahakyand14ccab2018-01-16 16:21:46 +0400700 if ((hw->snpsid & GSNPSID_ID_MASK) != DWC2_OTG_ID &&
701 (hw->snpsid & GSNPSID_ID_MASK) != DWC2_FS_IOT_ID &&
702 (hw->snpsid & GSNPSID_ID_MASK) != DWC2_HS_IOT_ID) {
John Youn323230e2016-11-03 17:55:50 -0700703 dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
704 hw->snpsid);
705 return -ENODEV;
706 }
707
708 dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
709 hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
710 hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
711
712 hwcfg1 = dwc2_readl(hsotg->regs + GHWCFG1);
713 hwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
714 hwcfg3 = dwc2_readl(hsotg->regs + GHWCFG3);
715 hwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
716 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
717
John Youn323230e2016-11-03 17:55:50 -0700718 /* hwcfg1 */
719 hw->dev_ep_dirs = hwcfg1;
720
721 /* hwcfg2 */
722 hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
723 GHWCFG2_OP_MODE_SHIFT;
724 hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
725 GHWCFG2_ARCHITECTURE_SHIFT;
726 hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
727 hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
728 GHWCFG2_NUM_HOST_CHAN_SHIFT);
729 hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
730 GHWCFG2_HS_PHY_TYPE_SHIFT;
731 hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
732 GHWCFG2_FS_PHY_TYPE_SHIFT;
733 hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
734 GHWCFG2_NUM_DEV_EP_SHIFT;
735 hw->nperio_tx_q_depth =
736 (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
737 GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
738 hw->host_perio_tx_q_depth =
739 (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
740 GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
741 hw->dev_token_q_depth =
742 (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
743 GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
744
745 /* hwcfg3 */
746 width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
747 GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
748 hw->max_transfer_size = (1 << (width + 11)) - 1;
749 width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
750 GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
751 hw->max_packet_count = (1 << (width + 4)) - 1;
752 hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
753 hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
754 GHWCFG3_DFIFO_DEPTH_SHIFT;
Sevak Arakelyan6f80b6d2018-01-24 17:41:48 +0400755 hw->lpm_mode = !!(hwcfg3 & GHWCFG3_OTG_LPM_EN);
John Youn323230e2016-11-03 17:55:50 -0700756
757 /* hwcfg4 */
758 hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
759 hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
760 GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
Minas Harutyunyan92730832017-11-30 12:16:37 +0400761 hw->num_dev_in_eps = (hwcfg4 & GHWCFG4_NUM_IN_EPS_MASK) >>
762 GHWCFG4_NUM_IN_EPS_SHIFT;
John Youn323230e2016-11-03 17:55:50 -0700763 hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
764 hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
Vardan Mikayelyan631a2312018-02-16 14:07:05 +0400765 hw->hibernation = !!(hwcfg4 & GHWCFG4_HIBER);
John Youn323230e2016-11-03 17:55:50 -0700766 hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
767 GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
Razmik Karapetyan66e77a22018-01-24 17:40:29 +0400768 hw->acg_enable = !!(hwcfg4 & GHWCFG4_ACG_SUPPORTED);
John Youn323230e2016-11-03 17:55:50 -0700769
770 /* fifo sizes */
John Yound1531312016-11-03 17:56:02 -0700771 hw->rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
John Youn323230e2016-11-03 17:55:50 -0700772 GRXFSIZ_DEPTH_SHIFT;
Minas Harutyunyan92730832017-11-30 12:16:37 +0400773 /*
774 * Host specific hardware parameters. Reading these parameters
775 * requires the controller to be in host mode. The mode will
776 * be forced, if necessary, to read these values.
777 */
778 dwc2_get_host_hwparams(hsotg);
779 dwc2_get_dev_hwparams(hsotg);
John Youn323230e2016-11-03 17:55:50 -0700780
John Youn323230e2016-11-03 17:55:50 -0700781 return 0;
782}
783
John Youn334bbd42016-11-03 17:55:55 -0700784int dwc2_init_params(struct dwc2_hsotg *hsotg)
785{
John Youn7de1deb2017-01-23 14:57:04 -0800786 const struct of_device_id *match;
787 void (*set_params)(void *data);
788
John Youn245977c2017-01-23 14:55:14 -0800789 dwc2_set_default_params(hsotg);
John Younf9f93cb2017-01-23 14:55:35 -0800790 dwc2_get_device_properties(hsotg);
John Youn334bbd42016-11-03 17:55:55 -0700791
John Youn7de1deb2017-01-23 14:57:04 -0800792 match = of_match_device(dwc2_of_match_table, hsotg->dev);
793 if (match && match->data) {
794 set_params = match->data;
795 set_params(hsotg);
796 }
797
John Yound936e662017-01-23 14:56:43 -0800798 dwc2_check_params(hsotg);
799
John Youn334bbd42016-11-03 17:55:55 -0700800 return 0;
801}