blob: b43d8c6a749d5ed7b448e0aece1136acc6712b12 [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 Delaunay1a149e32018-03-01 11:05:35 +0100140static void dwc2_set_stm32f7_hsotg_params(struct dwc2_hsotg *hsotg)
Amelie Delaunayd8fae8b2017-08-17 11:33:01 +0200141{
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 Delaunay1a149e32018-03-01 11:05:35 +0100167 { .compatible = "st,stm32f7-hsotg",
168 .data = dwc2_set_stm32f7_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 Youn03ea6d62018-02-16 14:12:28 +0400255static void dwc2_set_param_power_down(struct dwc2_hsotg *hsotg)
256{
257 int val;
258
259 if (hsotg->hw_params.hibernation)
260 val = 2;
261 else if (hsotg->hw_params.power_optimized)
262 val = 1;
263 else
264 val = 0;
265
266 hsotg->params.power_down = val;
267}
268
John Youn05ee7992016-11-03 17:56:05 -0700269/**
John Youn245977c2017-01-23 14:55:14 -0800270 * dwc2_set_default_params() - Set all core parameters to their
271 * auto-detected default values.
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400272 *
273 * @hsotg: Programming view of the DWC_otg controller
274 *
John Youn323230e2016-11-03 17:55:50 -0700275 */
John Youn245977c2017-01-23 14:55:14 -0800276static void dwc2_set_default_params(struct dwc2_hsotg *hsotg)
John Youn323230e2016-11-03 17:55:50 -0700277{
John Youn05ee7992016-11-03 17:56:05 -0700278 struct dwc2_hw_params *hw = &hsotg->hw_params;
279 struct dwc2_core_params *p = &hsotg->params;
John Youn6b66ce52016-11-03 17:56:12 -0700280 bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
John Youn323230e2016-11-03 17:55:50 -0700281
John Youn245977c2017-01-23 14:55:14 -0800282 dwc2_set_param_otg_cap(hsotg);
283 dwc2_set_param_phy_type(hsotg);
284 dwc2_set_param_speed(hsotg);
285 dwc2_set_param_phy_utmi_width(hsotg);
John Youn03ea6d62018-02-16 14:12:28 +0400286 dwc2_set_param_power_down(hsotg);
John Youn245977c2017-01-23 14:55:14 -0800287 p->phy_ulpi_ddr = false;
288 p->phy_ulpi_ext_vbus = false;
289
290 p->enable_dynamic_fifo = hw->enable_dynamic_fifo;
291 p->en_multiple_tx_fifo = hw->en_multiple_tx_fifo;
292 p->i2c_enable = hw->i2c_enable;
Razmik Karapetyan66e77a22018-01-24 17:40:29 +0400293 p->acg_enable = hw->acg_enable;
John Youn245977c2017-01-23 14:55:14 -0800294 p->ulpi_fs_ls = false;
295 p->ts_dline = false;
296 p->reload_ctl = (hw->snpsid >= DWC2_CORE_REV_2_92a);
297 p->uframe_sched = true;
298 p->external_id_pin_ctl = false;
Sevak Arakelyan6f80b6d2018-01-24 17:41:48 +0400299 p->lpm = true;
300 p->lpm_clock_gating = true;
301 p->besl = true;
302 p->hird_threshold_en = true;
303 p->hird_threshold = 4;
Grigor Tovmasyanb43ebc92018-05-05 12:17:58 +0400304 p->ipg_isoc_en = false;
John Youn245977c2017-01-23 14:55:14 -0800305 p->max_packet_count = hw->max_packet_count;
306 p->max_transfer_size = hw->max_transfer_size;
Razmik Karapetyan1b52d2f2018-01-19 14:40:23 +0400307 p->ahbcfg = GAHBCFG_HBSTLEN_INCR << GAHBCFG_HBSTLEN_SHIFT;
John Youn245977c2017-01-23 14:55:14 -0800308
John Youn6b66ce52016-11-03 17:56:12 -0700309 if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
310 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
John Youn245977c2017-01-23 14:55:14 -0800311 p->host_dma = dma_capable;
312 p->dma_desc_enable = false;
313 p->dma_desc_fs_enable = false;
314 p->host_support_fs_ls_low_power = false;
315 p->host_ls_low_power_phy_clk = false;
316 p->host_channels = hw->host_channels;
317 p->host_rx_fifo_size = hw->rx_fifo_size;
318 p->host_nperio_tx_fifo_size = hw->host_nperio_tx_fifo_size;
319 p->host_perio_tx_fifo_size = hw->host_perio_tx_fifo_size;
John Youn6b66ce52016-11-03 17:56:12 -0700320 }
321
John Youn05ee7992016-11-03 17:56:05 -0700322 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
323 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
John Youn245977c2017-01-23 14:55:14 -0800324 p->g_dma = dma_capable;
325 p->g_dma_desc = hw->dma_desc_enable;
John Youn05ee7992016-11-03 17:56:05 -0700326
327 /*
328 * The values for g_rx_fifo_size (2048) and
329 * g_np_tx_fifo_size (1024) come from the legacy s3c
330 * gadget driver. These defaults have been hard-coded
331 * for some time so many platforms depend on these
332 * values. Leave them as defaults for now and only
333 * auto-detect if the hardware does not support the
334 * default.
335 */
John Youn245977c2017-01-23 14:55:14 -0800336 p->g_rx_fifo_size = 2048;
337 p->g_np_tx_fifo_size = 1024;
John Youn05ee7992016-11-03 17:56:05 -0700338 dwc2_set_param_tx_fifo_sizes(hsotg);
339 }
John Youn323230e2016-11-03 17:55:50 -0700340}
341
John Younf9f93cb2017-01-23 14:55:35 -0800342/**
343 * dwc2_get_device_properties() - Read in device properties.
344 *
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400345 * @hsotg: Programming view of the DWC_otg controller
346 *
John Younf9f93cb2017-01-23 14:55:35 -0800347 * Read in the device properties and adjust core parameters if needed.
348 */
349static void dwc2_get_device_properties(struct dwc2_hsotg *hsotg)
350{
351 struct dwc2_core_params *p = &hsotg->params;
352 int num;
353
354 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
355 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
356 device_property_read_u32(hsotg->dev, "g-rx-fifo-size",
357 &p->g_rx_fifo_size);
358
359 device_property_read_u32(hsotg->dev, "g-np-tx-fifo-size",
360 &p->g_np_tx_fifo_size);
361
362 num = device_property_read_u32_array(hsotg->dev,
363 "g-tx-fifo-size",
364 NULL, 0);
365
366 if (num > 0) {
367 num = min(num, 15);
368 memset(p->g_tx_fifo_size, 0,
369 sizeof(p->g_tx_fifo_size));
370 device_property_read_u32_array(hsotg->dev,
371 "g-tx-fifo-size",
372 &p->g_tx_fifo_size[1],
373 num);
374 }
375 }
Dinh Nguyenb11633c2017-10-16 08:57:18 -0500376
377 if (of_find_property(hsotg->dev->of_node, "disable-over-current", NULL))
378 p->oc_disable = true;
John Younf9f93cb2017-01-23 14:55:35 -0800379}
380
John Yound936e662017-01-23 14:56:43 -0800381static void dwc2_check_param_otg_cap(struct dwc2_hsotg *hsotg)
382{
383 int valid = 1;
384
385 switch (hsotg->params.otg_cap) {
386 case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
387 if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
388 valid = 0;
389 break;
390 case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
391 switch (hsotg->hw_params.op_mode) {
392 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
393 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
394 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
395 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
396 break;
397 default:
398 valid = 0;
399 break;
400 }
401 break;
402 case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
403 /* always valid */
404 break;
405 default:
406 valid = 0;
407 break;
408 }
409
410 if (!valid)
411 dwc2_set_param_otg_cap(hsotg);
412}
413
414static void dwc2_check_param_phy_type(struct dwc2_hsotg *hsotg)
415{
416 int valid = 0;
417 u32 hs_phy_type;
418 u32 fs_phy_type;
419
420 hs_phy_type = hsotg->hw_params.hs_phy_type;
421 fs_phy_type = hsotg->hw_params.fs_phy_type;
422
423 switch (hsotg->params.phy_type) {
424 case DWC2_PHY_TYPE_PARAM_FS:
425 if (fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
426 valid = 1;
427 break;
428 case DWC2_PHY_TYPE_PARAM_UTMI:
429 if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
430 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
431 valid = 1;
432 break;
433 case DWC2_PHY_TYPE_PARAM_ULPI:
434 if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
435 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
436 valid = 1;
437 break;
438 default:
439 break;
440 }
441
442 if (!valid)
443 dwc2_set_param_phy_type(hsotg);
444}
445
446static void dwc2_check_param_speed(struct dwc2_hsotg *hsotg)
447{
448 int valid = 1;
449 int phy_type = hsotg->params.phy_type;
450 int speed = hsotg->params.speed;
451
452 switch (speed) {
453 case DWC2_SPEED_PARAM_HIGH:
454 if ((hsotg->params.speed == DWC2_SPEED_PARAM_HIGH) &&
455 (phy_type == DWC2_PHY_TYPE_PARAM_FS))
456 valid = 0;
457 break;
458 case DWC2_SPEED_PARAM_FULL:
459 case DWC2_SPEED_PARAM_LOW:
460 break;
461 default:
462 valid = 0;
463 break;
464 }
465
466 if (!valid)
467 dwc2_set_param_speed(hsotg);
468}
469
470static void dwc2_check_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
471{
472 int valid = 0;
473 int param = hsotg->params.phy_utmi_width;
474 int width = hsotg->hw_params.utmi_phy_data_width;
475
476 switch (width) {
477 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
478 valid = (param == 8);
479 break;
480 case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
481 valid = (param == 16);
482 break;
483 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
484 valid = (param == 8 || param == 16);
485 break;
486 }
487
488 if (!valid)
489 dwc2_set_param_phy_utmi_width(hsotg);
490}
491
Vardan Mikayelyan631a2312018-02-16 14:07:05 +0400492static void dwc2_check_param_power_down(struct dwc2_hsotg *hsotg)
493{
494 int param = hsotg->params.power_down;
495
496 switch (param) {
497 case DWC2_POWER_DOWN_PARAM_NONE:
498 break;
499 case DWC2_POWER_DOWN_PARAM_PARTIAL:
500 if (hsotg->hw_params.power_optimized)
501 break;
502 dev_dbg(hsotg->dev,
503 "Partial power down isn't supported by HW\n");
504 param = DWC2_POWER_DOWN_PARAM_NONE;
505 break;
506 case DWC2_POWER_DOWN_PARAM_HIBERNATION:
507 if (hsotg->hw_params.hibernation)
508 break;
509 dev_dbg(hsotg->dev,
510 "Hibernation isn't supported by HW\n");
511 param = DWC2_POWER_DOWN_PARAM_NONE;
512 break;
513 default:
514 dev_err(hsotg->dev,
515 "%s: Invalid parameter power_down=%d\n",
516 __func__, param);
517 param = DWC2_POWER_DOWN_PARAM_NONE;
518 break;
519 }
520
521 hsotg->params.power_down = param;
522}
523
Sevak Arakelyan3c6aea72017-01-23 15:01:45 -0800524static void dwc2_check_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
525{
526 int fifo_count;
527 int fifo;
528 int min;
529 u32 total = 0;
530 u32 dptxfszn;
531
532 fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
533 min = hsotg->hw_params.en_multiple_tx_fifo ? 16 : 4;
534
535 for (fifo = 1; fifo <= fifo_count; fifo++)
536 total += hsotg->params.g_tx_fifo_size[fifo];
537
538 if (total > dwc2_hsotg_tx_fifo_total_depth(hsotg) || !total) {
539 dev_warn(hsotg->dev, "%s: Invalid parameter g-tx-fifo-size, setting to default average\n",
540 __func__);
541 dwc2_set_param_tx_fifo_sizes(hsotg);
542 }
543
544 for (fifo = 1; fifo <= fifo_count; fifo++) {
Minas Harutyunyan92730832017-11-30 12:16:37 +0400545 dptxfszn = hsotg->hw_params.g_tx_fifo_size[fifo];
Sevak Arakelyan3c6aea72017-01-23 15:01:45 -0800546
547 if (hsotg->params.g_tx_fifo_size[fifo] < min ||
548 hsotg->params.g_tx_fifo_size[fifo] > dptxfszn) {
549 dev_warn(hsotg->dev, "%s: Invalid parameter g_tx_fifo_size[%d]=%d\n",
550 __func__, fifo,
551 hsotg->params.g_tx_fifo_size[fifo]);
552 hsotg->params.g_tx_fifo_size[fifo] = dptxfszn;
553 }
554 }
555}
556
John Yound936e662017-01-23 14:56:43 -0800557#define CHECK_RANGE(_param, _min, _max, _def) do { \
558 if ((hsotg->params._param) < (_min) || \
559 (hsotg->params._param) > (_max)) { \
560 dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
561 __func__, #_param, hsotg->params._param); \
562 hsotg->params._param = (_def); \
563 } \
564 } while (0)
565
566#define CHECK_BOOL(_param, _check) do { \
567 if (hsotg->params._param && !(_check)) { \
568 dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
569 __func__, #_param, hsotg->params._param); \
570 hsotg->params._param = false; \
571 } \
572 } while (0)
573
574static void dwc2_check_params(struct dwc2_hsotg *hsotg)
575{
576 struct dwc2_hw_params *hw = &hsotg->hw_params;
577 struct dwc2_core_params *p = &hsotg->params;
578 bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
579
580 dwc2_check_param_otg_cap(hsotg);
581 dwc2_check_param_phy_type(hsotg);
582 dwc2_check_param_speed(hsotg);
583 dwc2_check_param_phy_utmi_width(hsotg);
Vardan Mikayelyan631a2312018-02-16 14:07:05 +0400584 dwc2_check_param_power_down(hsotg);
John Yound936e662017-01-23 14:56:43 -0800585 CHECK_BOOL(enable_dynamic_fifo, hw->enable_dynamic_fifo);
586 CHECK_BOOL(en_multiple_tx_fifo, hw->en_multiple_tx_fifo);
587 CHECK_BOOL(i2c_enable, hw->i2c_enable);
Grigor Tovmasyanb43ebc92018-05-05 12:17:58 +0400588 CHECK_BOOL(ipg_isoc_en, hw->ipg_isoc_en);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +0400589 CHECK_BOOL(acg_enable, hw->acg_enable);
John Yound936e662017-01-23 14:56:43 -0800590 CHECK_BOOL(reload_ctl, (hsotg->hw_params.snpsid > DWC2_CORE_REV_2_92a));
Sevak Arakelyan6f80b6d2018-01-24 17:41:48 +0400591 CHECK_BOOL(lpm, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_80a));
592 CHECK_BOOL(lpm, hw->lpm_mode);
593 CHECK_BOOL(lpm_clock_gating, hsotg->params.lpm);
594 CHECK_BOOL(besl, hsotg->params.lpm);
595 CHECK_BOOL(besl, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a));
596 CHECK_BOOL(hird_threshold_en, hsotg->params.lpm);
597 CHECK_RANGE(hird_threshold, 0, hsotg->params.besl ? 12 : 7, 0);
John Yound936e662017-01-23 14:56:43 -0800598 CHECK_RANGE(max_packet_count,
599 15, hw->max_packet_count,
600 hw->max_packet_count);
601 CHECK_RANGE(max_transfer_size,
602 2047, hw->max_transfer_size,
603 hw->max_transfer_size);
604
605 if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
606 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
607 CHECK_BOOL(host_dma, dma_capable);
608 CHECK_BOOL(dma_desc_enable, p->host_dma);
609 CHECK_BOOL(dma_desc_fs_enable, p->dma_desc_enable);
610 CHECK_BOOL(host_ls_low_power_phy_clk,
611 p->phy_type == DWC2_PHY_TYPE_PARAM_FS);
612 CHECK_RANGE(host_channels,
613 1, hw->host_channels,
614 hw->host_channels);
615 CHECK_RANGE(host_rx_fifo_size,
616 16, hw->rx_fifo_size,
617 hw->rx_fifo_size);
618 CHECK_RANGE(host_nperio_tx_fifo_size,
619 16, hw->host_nperio_tx_fifo_size,
620 hw->host_nperio_tx_fifo_size);
621 CHECK_RANGE(host_perio_tx_fifo_size,
622 16, hw->host_perio_tx_fifo_size,
623 hw->host_perio_tx_fifo_size);
624 }
625
626 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
627 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
628 CHECK_BOOL(g_dma, dma_capable);
629 CHECK_BOOL(g_dma_desc, (p->g_dma && hw->dma_desc_enable));
630 CHECK_RANGE(g_rx_fifo_size,
631 16, hw->rx_fifo_size,
632 hw->rx_fifo_size);
633 CHECK_RANGE(g_np_tx_fifo_size,
634 16, hw->dev_nperio_tx_fifo_size,
635 hw->dev_nperio_tx_fifo_size);
Sevak Arakelyan3c6aea72017-01-23 15:01:45 -0800636 dwc2_check_param_tx_fifo_sizes(hsotg);
John Yound936e662017-01-23 14:56:43 -0800637 }
638}
639
John Youn323230e2016-11-03 17:55:50 -0700640/*
641 * Gets host hardware parameters. Forces host mode if not currently in
642 * host mode. Should be called immediately after a core soft reset in
643 * order to get the reset values.
644 */
645static void dwc2_get_host_hwparams(struct dwc2_hsotg *hsotg)
646{
647 struct dwc2_hw_params *hw = &hsotg->hw_params;
648 u32 gnptxfsiz;
649 u32 hptxfsiz;
John Youn323230e2016-11-03 17:55:50 -0700650
651 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
652 return;
653
Vardan Mikayelyan13b1f8e2018-02-16 12:56:03 +0400654 dwc2_force_mode(hsotg, true);
John Youn323230e2016-11-03 17:55:50 -0700655
656 gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
657 hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
John Youn323230e2016-11-03 17:55:50 -0700658
John Youn323230e2016-11-03 17:55:50 -0700659 hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
660 FIFOSIZE_DEPTH_SHIFT;
661 hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
662 FIFOSIZE_DEPTH_SHIFT;
663}
664
665/*
666 * Gets device hardware parameters. Forces device mode if not
667 * currently in device mode. Should be called immediately after a core
668 * soft reset in order to get the reset values.
669 */
670static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
671{
672 struct dwc2_hw_params *hw = &hsotg->hw_params;
John Youn323230e2016-11-03 17:55:50 -0700673 u32 gnptxfsiz;
Minas Harutyunyan92730832017-11-30 12:16:37 +0400674 int fifo, fifo_count;
John Youn323230e2016-11-03 17:55:50 -0700675
676 if (hsotg->dr_mode == USB_DR_MODE_HOST)
677 return;
678
Vardan Mikayelyan13b1f8e2018-02-16 12:56:03 +0400679 dwc2_force_mode(hsotg, false);
John Youn323230e2016-11-03 17:55:50 -0700680
681 gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
John Youn323230e2016-11-03 17:55:50 -0700682
Minas Harutyunyan92730832017-11-30 12:16:37 +0400683 fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
684
685 for (fifo = 1; fifo <= fifo_count; fifo++) {
686 hw->g_tx_fifo_size[fifo] =
687 (dwc2_readl(hsotg->regs + DPTXFSIZN(fifo)) &
688 FIFOSIZE_DEPTH_MASK) >> FIFOSIZE_DEPTH_SHIFT;
689 }
690
John Youn323230e2016-11-03 17:55:50 -0700691 hw->dev_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
692 FIFOSIZE_DEPTH_SHIFT;
693}
694
695/**
696 * During device initialization, read various hardware configuration
697 * registers and interpret the contents.
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400698 *
699 * @hsotg: Programming view of the DWC_otg controller
700 *
John Youn323230e2016-11-03 17:55:50 -0700701 */
702int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
703{
704 struct dwc2_hw_params *hw = &hsotg->hw_params;
705 unsigned int width;
706 u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
707 u32 grxfsiz;
708
709 /*
710 * Attempt to ensure this device is really a DWC_otg Controller.
711 * Read and verify the GSNPSID register contents. The value should be
Gevorg Sahakyand14ccab2018-01-16 16:21:46 +0400712 * 0x45f4xxxx, 0x5531xxxx or 0x5532xxxx
John Youn323230e2016-11-03 17:55:50 -0700713 */
Gevorg Sahakyand14ccab2018-01-16 16:21:46 +0400714
John Youn323230e2016-11-03 17:55:50 -0700715 hw->snpsid = dwc2_readl(hsotg->regs + GSNPSID);
Gevorg Sahakyand14ccab2018-01-16 16:21:46 +0400716 if ((hw->snpsid & GSNPSID_ID_MASK) != DWC2_OTG_ID &&
717 (hw->snpsid & GSNPSID_ID_MASK) != DWC2_FS_IOT_ID &&
718 (hw->snpsid & GSNPSID_ID_MASK) != DWC2_HS_IOT_ID) {
John Youn323230e2016-11-03 17:55:50 -0700719 dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
720 hw->snpsid);
721 return -ENODEV;
722 }
723
724 dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
725 hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
726 hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
727
728 hwcfg1 = dwc2_readl(hsotg->regs + GHWCFG1);
729 hwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
730 hwcfg3 = dwc2_readl(hsotg->regs + GHWCFG3);
731 hwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
732 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
733
John Youn323230e2016-11-03 17:55:50 -0700734 /* hwcfg1 */
735 hw->dev_ep_dirs = hwcfg1;
736
737 /* hwcfg2 */
738 hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
739 GHWCFG2_OP_MODE_SHIFT;
740 hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
741 GHWCFG2_ARCHITECTURE_SHIFT;
742 hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
743 hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
744 GHWCFG2_NUM_HOST_CHAN_SHIFT);
745 hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
746 GHWCFG2_HS_PHY_TYPE_SHIFT;
747 hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
748 GHWCFG2_FS_PHY_TYPE_SHIFT;
749 hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
750 GHWCFG2_NUM_DEV_EP_SHIFT;
751 hw->nperio_tx_q_depth =
752 (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
753 GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
754 hw->host_perio_tx_q_depth =
755 (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
756 GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
757 hw->dev_token_q_depth =
758 (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
759 GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
760
761 /* hwcfg3 */
762 width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
763 GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
764 hw->max_transfer_size = (1 << (width + 11)) - 1;
765 width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
766 GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
767 hw->max_packet_count = (1 << (width + 4)) - 1;
768 hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
769 hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
770 GHWCFG3_DFIFO_DEPTH_SHIFT;
Sevak Arakelyan6f80b6d2018-01-24 17:41:48 +0400771 hw->lpm_mode = !!(hwcfg3 & GHWCFG3_OTG_LPM_EN);
John Youn323230e2016-11-03 17:55:50 -0700772
773 /* hwcfg4 */
774 hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
775 hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
776 GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
Minas Harutyunyan92730832017-11-30 12:16:37 +0400777 hw->num_dev_in_eps = (hwcfg4 & GHWCFG4_NUM_IN_EPS_MASK) >>
778 GHWCFG4_NUM_IN_EPS_SHIFT;
John Youn323230e2016-11-03 17:55:50 -0700779 hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
780 hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
Vardan Mikayelyan631a2312018-02-16 14:07:05 +0400781 hw->hibernation = !!(hwcfg4 & GHWCFG4_HIBER);
John Youn323230e2016-11-03 17:55:50 -0700782 hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
783 GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
Razmik Karapetyan66e77a22018-01-24 17:40:29 +0400784 hw->acg_enable = !!(hwcfg4 & GHWCFG4_ACG_SUPPORTED);
Grigor Tovmasyanb43ebc92018-05-05 12:17:58 +0400785 hw->ipg_isoc_en = !!(hwcfg4 & GHWCFG4_IPG_ISOC_SUPPORTED);
John Youn323230e2016-11-03 17:55:50 -0700786
787 /* fifo sizes */
John Yound1531312016-11-03 17:56:02 -0700788 hw->rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
John Youn323230e2016-11-03 17:55:50 -0700789 GRXFSIZ_DEPTH_SHIFT;
Minas Harutyunyan92730832017-11-30 12:16:37 +0400790 /*
791 * Host specific hardware parameters. Reading these parameters
792 * requires the controller to be in host mode. The mode will
793 * be forced, if necessary, to read these values.
794 */
795 dwc2_get_host_hwparams(hsotg);
796 dwc2_get_dev_hwparams(hsotg);
John Youn323230e2016-11-03 17:55:50 -0700797
John Youn323230e2016-11-03 17:55:50 -0700798 return 0;
799}
800
John Youn334bbd42016-11-03 17:55:55 -0700801int dwc2_init_params(struct dwc2_hsotg *hsotg)
802{
John Youn7de1deb2017-01-23 14:57:04 -0800803 const struct of_device_id *match;
804 void (*set_params)(void *data);
805
John Youn245977c2017-01-23 14:55:14 -0800806 dwc2_set_default_params(hsotg);
John Younf9f93cb2017-01-23 14:55:35 -0800807 dwc2_get_device_properties(hsotg);
John Youn334bbd42016-11-03 17:55:55 -0700808
John Youn7de1deb2017-01-23 14:57:04 -0800809 match = of_match_device(dwc2_of_match_table, hsotg->dev);
810 if (match && match->data) {
811 set_params = match->data;
812 set_params(hsotg);
813 }
814
John Yound936e662017-01-23 14:56:43 -0800815 dwc2_check_params(hsotg);
816
John Youn334bbd42016-11-03 17:55:55 -0700817 return 0;
818}