blob: a3ffe97170ffd7ccdaf42cc892963ec22793d380 [file] [log] [blame]
John Youn323230e2016-11-03 17:55:50 -07001/*
2 * Copyright (C) 2004-2016 Synopsys, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions, and the following disclaimer,
9 * without modification.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The names of the above-listed copyright holders may not be used
14 * to endorse or promote products derived from this software without
15 * specific prior written permission.
16 *
17 * ALTERNATIVELY, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") as published by the Free Software
19 * Foundation; either version 2 of the License, or (at your option) any
20 * later version.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include <linux/kernel.h>
36#include <linux/module.h>
37#include <linux/of_device.h>
38
39#include "core.h"
40
John Youn7de1deb2017-01-23 14:57:04 -080041static void dwc2_set_bcm_params(struct dwc2_hsotg *hsotg)
42{
43 struct dwc2_core_params *p = &hsotg->params;
John Youn323230e2016-11-03 17:55:50 -070044
John Youn7de1deb2017-01-23 14:57:04 -080045 p->host_rx_fifo_size = 774;
John Youn7de1deb2017-01-23 14:57:04 -080046 p->max_transfer_size = 65535;
47 p->max_packet_count = 511;
John Youn7de1deb2017-01-23 14:57:04 -080048 p->ahbcfg = 0x10;
49 p->uframe_sched = false;
50}
John Youn323230e2016-11-03 17:55:50 -070051
John Youn7de1deb2017-01-23 14:57:04 -080052static void dwc2_set_his_params(struct dwc2_hsotg *hsotg)
53{
54 struct dwc2_core_params *p = &hsotg->params;
John Youn323230e2016-11-03 17:55:50 -070055
John Youn7de1deb2017-01-23 14:57:04 -080056 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
57 p->speed = DWC2_SPEED_PARAM_HIGH;
58 p->host_rx_fifo_size = 512;
59 p->host_nperio_tx_fifo_size = 512;
60 p->host_perio_tx_fifo_size = 512;
61 p->max_transfer_size = 65535;
62 p->max_packet_count = 511;
63 p->host_channels = 16;
64 p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
65 p->phy_utmi_width = 8;
66 p->i2c_enable = false;
John Youn7de1deb2017-01-23 14:57:04 -080067 p->reload_ctl = false;
68 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
69 GAHBCFG_HBSTLEN_SHIFT;
70 p->uframe_sched = false;
Chen Yuca8b0332017-01-23 15:00:18 -080071 p->change_speed_quirk = true;
John Youn7de1deb2017-01-23 14:57:04 -080072}
John Youn323230e2016-11-03 17:55:50 -070073
John Youn7de1deb2017-01-23 14:57:04 -080074static void dwc2_set_rk_params(struct dwc2_hsotg *hsotg)
75{
76 struct dwc2_core_params *p = &hsotg->params;
77
78 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
79 p->host_rx_fifo_size = 525;
80 p->host_nperio_tx_fifo_size = 128;
81 p->host_perio_tx_fifo_size = 256;
82 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
83 GAHBCFG_HBSTLEN_SHIFT;
84}
85
86static void dwc2_set_ltq_params(struct dwc2_hsotg *hsotg)
87{
88 struct dwc2_core_params *p = &hsotg->params;
89
90 p->otg_cap = 2;
91 p->host_rx_fifo_size = 288;
92 p->host_nperio_tx_fifo_size = 128;
93 p->host_perio_tx_fifo_size = 96;
94 p->max_transfer_size = 65535;
95 p->max_packet_count = 511;
96 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
97 GAHBCFG_HBSTLEN_SHIFT;
98}
99
100static void dwc2_set_amlogic_params(struct dwc2_hsotg *hsotg)
101{
102 struct dwc2_core_params *p = &hsotg->params;
103
104 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
105 p->speed = DWC2_SPEED_PARAM_HIGH;
106 p->host_rx_fifo_size = 512;
107 p->host_nperio_tx_fifo_size = 500;
108 p->host_perio_tx_fifo_size = 500;
109 p->host_channels = 16;
110 p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
111 p->ahbcfg = GAHBCFG_HBSTLEN_INCR8 <<
112 GAHBCFG_HBSTLEN_SHIFT;
113 p->uframe_sched = false;
114}
115
116static void dwc2_set_amcc_params(struct dwc2_hsotg *hsotg)
117{
118 struct dwc2_core_params *p = &hsotg->params;
119
120 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT;
121}
John Youn323230e2016-11-03 17:55:50 -0700122
Bruno Herrerae35b1352017-01-31 23:25:43 -0200123static void dwc2_set_stm32f4x9_fsotg_params(struct dwc2_hsotg *hsotg)
124{
125 struct dwc2_core_params *p = &hsotg->params;
126
127 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
128 p->speed = DWC2_SPEED_PARAM_FULL;
129 p->host_rx_fifo_size = 128;
130 p->host_nperio_tx_fifo_size = 96;
131 p->host_perio_tx_fifo_size = 96;
132 p->max_packet_count = 256;
133 p->phy_type = DWC2_PHY_TYPE_PARAM_FS;
134 p->i2c_enable = false;
135 p->uframe_sched = false;
136 p->activate_stm_fs_transceiver = true;
137}
138
John Youn323230e2016-11-03 17:55:50 -0700139const struct of_device_id dwc2_of_match_table[] = {
John Youn7de1deb2017-01-23 14:57:04 -0800140 { .compatible = "brcm,bcm2835-usb", .data = dwc2_set_bcm_params },
141 { .compatible = "hisilicon,hi6220-usb", .data = dwc2_set_his_params },
142 { .compatible = "rockchip,rk3066-usb", .data = dwc2_set_rk_params },
143 { .compatible = "lantiq,arx100-usb", .data = dwc2_set_ltq_params },
144 { .compatible = "lantiq,xrx200-usb", .data = dwc2_set_ltq_params },
145 { .compatible = "snps,dwc2" },
146 { .compatible = "samsung,s3c6400-hsotg" },
Martin Blumenstingl55b644f2017-05-06 19:37:45 +0200147 { .compatible = "amlogic,meson8-usb",
148 .data = dwc2_set_amlogic_params },
John Youn7de1deb2017-01-23 14:57:04 -0800149 { .compatible = "amlogic,meson8b-usb",
150 .data = dwc2_set_amlogic_params },
151 { .compatible = "amlogic,meson-gxbb-usb",
152 .data = dwc2_set_amlogic_params },
153 { .compatible = "amcc,dwc-otg", .data = dwc2_set_amcc_params },
Bruno Herrerae35b1352017-01-31 23:25:43 -0200154 { .compatible = "st,stm32f4x9-fsotg",
155 .data = dwc2_set_stm32f4x9_fsotg_params },
156 { .compatible = "st,stm32f4x9-hsotg" },
John Youn323230e2016-11-03 17:55:50 -0700157 {},
158};
159MODULE_DEVICE_TABLE(of, dwc2_of_match_table);
160
John Youn245977c2017-01-23 14:55:14 -0800161static void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg)
John Youn05ee7992016-11-03 17:56:05 -0700162{
John Youn245977c2017-01-23 14:55:14 -0800163 u8 val;
John Youn05ee7992016-11-03 17:56:05 -0700164
John Youn245977c2017-01-23 14:55:14 -0800165 switch (hsotg->hw_params.op_mode) {
166 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
167 val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
John Youn05ee7992016-11-03 17:56:05 -0700168 break;
John Youn245977c2017-01-23 14:55:14 -0800169 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
170 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
171 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
172 val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
John Youn05ee7992016-11-03 17:56:05 -0700173 break;
174 default:
John Youn245977c2017-01-23 14:55:14 -0800175 val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
John Youn05ee7992016-11-03 17:56:05 -0700176 break;
John Youn323230e2016-11-03 17:55:50 -0700177 }
178
John Younbea8e862016-11-03 17:55:53 -0700179 hsotg->params.otg_cap = val;
John Youn323230e2016-11-03 17:55:50 -0700180}
181
John Youn245977c2017-01-23 14:55:14 -0800182static void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg)
John Youn323230e2016-11-03 17:55:50 -0700183{
John Youn245977c2017-01-23 14:55:14 -0800184 int val;
185 u32 hs_phy_type = hsotg->hw_params.hs_phy_type;
John Youn323230e2016-11-03 17:55:50 -0700186
John Youn245977c2017-01-23 14:55:14 -0800187 val = DWC2_PHY_TYPE_PARAM_FS;
188 if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
189 if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
190 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
191 val = DWC2_PHY_TYPE_PARAM_UTMI;
192 else
193 val = DWC2_PHY_TYPE_PARAM_ULPI;
John Youn323230e2016-11-03 17:55:50 -0700194 }
195
John Youn245977c2017-01-23 14:55:14 -0800196 if (dwc2_is_fs_iot(hsotg))
197 hsotg->params.phy_type = DWC2_PHY_TYPE_PARAM_FS;
John Youn323230e2016-11-03 17:55:50 -0700198
John Younbea8e862016-11-03 17:55:53 -0700199 hsotg->params.phy_type = val;
John Youn323230e2016-11-03 17:55:50 -0700200}
201
John Youn245977c2017-01-23 14:55:14 -0800202static void dwc2_set_param_speed(struct dwc2_hsotg *hsotg)
John Youn323230e2016-11-03 17:55:50 -0700203{
John Youn245977c2017-01-23 14:55:14 -0800204 int val;
John Youn323230e2016-11-03 17:55:50 -0700205
John Youn245977c2017-01-23 14:55:14 -0800206 val = hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS ?
207 DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
John Youn323230e2016-11-03 17:55:50 -0700208
John Youn245977c2017-01-23 14:55:14 -0800209 if (dwc2_is_fs_iot(hsotg))
210 val = DWC2_SPEED_PARAM_FULL;
John Youn323230e2016-11-03 17:55:50 -0700211
John Youn245977c2017-01-23 14:55:14 -0800212 if (dwc2_is_hs_iot(hsotg))
213 val = DWC2_SPEED_PARAM_HIGH;
John Youn323230e2016-11-03 17:55:50 -0700214
John Younbea8e862016-11-03 17:55:53 -0700215 hsotg->params.speed = val;
John Youn323230e2016-11-03 17:55:50 -0700216}
217
John Youn245977c2017-01-23 14:55:14 -0800218static void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
John Youn323230e2016-11-03 17:55:50 -0700219{
John Youn245977c2017-01-23 14:55:14 -0800220 int val;
John Youn323230e2016-11-03 17:55:50 -0700221
John Youn245977c2017-01-23 14:55:14 -0800222 val = (hsotg->hw_params.utmi_phy_data_width ==
223 GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
John Youn323230e2016-11-03 17:55:50 -0700224
John Younbea8e862016-11-03 17:55:53 -0700225 hsotg->params.phy_utmi_width = val;
John Youn323230e2016-11-03 17:55:50 -0700226}
227
John Youn05ee7992016-11-03 17:56:05 -0700228static void dwc2_set_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
229{
John Youn05ee7992016-11-03 17:56:05 -0700230 struct dwc2_core_params *p = &hsotg->params;
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800231 int depth_average;
232 int fifo_count;
233 int i;
234
235 fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
John Youn05ee7992016-11-03 17:56:05 -0700236
237 memset(p->g_tx_fifo_size, 0, sizeof(p->g_tx_fifo_size));
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800238 depth_average = dwc2_hsotg_tx_fifo_average_depth(hsotg);
239 for (i = 1; i <= fifo_count; i++)
240 p->g_tx_fifo_size[i] = depth_average;
John Youn9962b622016-11-09 19:27:40 -0800241}
242
John Youn05ee7992016-11-03 17:56:05 -0700243/**
John Youn245977c2017-01-23 14:55:14 -0800244 * dwc2_set_default_params() - Set all core parameters to their
245 * auto-detected default values.
John Youn323230e2016-11-03 17:55:50 -0700246 */
John Youn245977c2017-01-23 14:55:14 -0800247static void dwc2_set_default_params(struct dwc2_hsotg *hsotg)
John Youn323230e2016-11-03 17:55:50 -0700248{
John Youn05ee7992016-11-03 17:56:05 -0700249 struct dwc2_hw_params *hw = &hsotg->hw_params;
250 struct dwc2_core_params *p = &hsotg->params;
John Youn6b66ce52016-11-03 17:56:12 -0700251 bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
John Youn323230e2016-11-03 17:55:50 -0700252
John Youn245977c2017-01-23 14:55:14 -0800253 dwc2_set_param_otg_cap(hsotg);
254 dwc2_set_param_phy_type(hsotg);
255 dwc2_set_param_speed(hsotg);
256 dwc2_set_param_phy_utmi_width(hsotg);
257 p->phy_ulpi_ddr = false;
258 p->phy_ulpi_ext_vbus = false;
259
260 p->enable_dynamic_fifo = hw->enable_dynamic_fifo;
261 p->en_multiple_tx_fifo = hw->en_multiple_tx_fifo;
262 p->i2c_enable = hw->i2c_enable;
263 p->ulpi_fs_ls = false;
264 p->ts_dline = false;
265 p->reload_ctl = (hw->snpsid >= DWC2_CORE_REV_2_92a);
266 p->uframe_sched = true;
267 p->external_id_pin_ctl = false;
268 p->hibernation = false;
269 p->max_packet_count = hw->max_packet_count;
270 p->max_transfer_size = hw->max_transfer_size;
271 p->ahbcfg = GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT;
272
John Youn6b66ce52016-11-03 17:56:12 -0700273 if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
274 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
John Youn245977c2017-01-23 14:55:14 -0800275 p->host_dma = dma_capable;
276 p->dma_desc_enable = false;
277 p->dma_desc_fs_enable = false;
278 p->host_support_fs_ls_low_power = false;
279 p->host_ls_low_power_phy_clk = false;
280 p->host_channels = hw->host_channels;
281 p->host_rx_fifo_size = hw->rx_fifo_size;
282 p->host_nperio_tx_fifo_size = hw->host_nperio_tx_fifo_size;
283 p->host_perio_tx_fifo_size = hw->host_perio_tx_fifo_size;
John Youn6b66ce52016-11-03 17:56:12 -0700284 }
285
John Youn05ee7992016-11-03 17:56:05 -0700286 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
287 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
John Youn245977c2017-01-23 14:55:14 -0800288 p->g_dma = dma_capable;
289 p->g_dma_desc = hw->dma_desc_enable;
John Youn05ee7992016-11-03 17:56:05 -0700290
291 /*
292 * The values for g_rx_fifo_size (2048) and
293 * g_np_tx_fifo_size (1024) come from the legacy s3c
294 * gadget driver. These defaults have been hard-coded
295 * for some time so many platforms depend on these
296 * values. Leave them as defaults for now and only
297 * auto-detect if the hardware does not support the
298 * default.
299 */
John Youn245977c2017-01-23 14:55:14 -0800300 p->g_rx_fifo_size = 2048;
301 p->g_np_tx_fifo_size = 1024;
John Youn05ee7992016-11-03 17:56:05 -0700302 dwc2_set_param_tx_fifo_sizes(hsotg);
303 }
John Youn323230e2016-11-03 17:55:50 -0700304}
305
John Younf9f93cb2017-01-23 14:55:35 -0800306/**
307 * dwc2_get_device_properties() - Read in device properties.
308 *
309 * Read in the device properties and adjust core parameters if needed.
310 */
311static void dwc2_get_device_properties(struct dwc2_hsotg *hsotg)
312{
313 struct dwc2_core_params *p = &hsotg->params;
314 int num;
315
316 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
317 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
318 device_property_read_u32(hsotg->dev, "g-rx-fifo-size",
319 &p->g_rx_fifo_size);
320
321 device_property_read_u32(hsotg->dev, "g-np-tx-fifo-size",
322 &p->g_np_tx_fifo_size);
323
324 num = device_property_read_u32_array(hsotg->dev,
325 "g-tx-fifo-size",
326 NULL, 0);
327
328 if (num > 0) {
329 num = min(num, 15);
330 memset(p->g_tx_fifo_size, 0,
331 sizeof(p->g_tx_fifo_size));
332 device_property_read_u32_array(hsotg->dev,
333 "g-tx-fifo-size",
334 &p->g_tx_fifo_size[1],
335 num);
336 }
337 }
338}
339
John Yound936e662017-01-23 14:56:43 -0800340static void dwc2_check_param_otg_cap(struct dwc2_hsotg *hsotg)
341{
342 int valid = 1;
343
344 switch (hsotg->params.otg_cap) {
345 case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
346 if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
347 valid = 0;
348 break;
349 case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
350 switch (hsotg->hw_params.op_mode) {
351 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
352 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
353 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
354 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
355 break;
356 default:
357 valid = 0;
358 break;
359 }
360 break;
361 case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
362 /* always valid */
363 break;
364 default:
365 valid = 0;
366 break;
367 }
368
369 if (!valid)
370 dwc2_set_param_otg_cap(hsotg);
371}
372
373static void dwc2_check_param_phy_type(struct dwc2_hsotg *hsotg)
374{
375 int valid = 0;
376 u32 hs_phy_type;
377 u32 fs_phy_type;
378
379 hs_phy_type = hsotg->hw_params.hs_phy_type;
380 fs_phy_type = hsotg->hw_params.fs_phy_type;
381
382 switch (hsotg->params.phy_type) {
383 case DWC2_PHY_TYPE_PARAM_FS:
384 if (fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
385 valid = 1;
386 break;
387 case DWC2_PHY_TYPE_PARAM_UTMI:
388 if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
389 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
390 valid = 1;
391 break;
392 case DWC2_PHY_TYPE_PARAM_ULPI:
393 if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
394 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
395 valid = 1;
396 break;
397 default:
398 break;
399 }
400
401 if (!valid)
402 dwc2_set_param_phy_type(hsotg);
403}
404
405static void dwc2_check_param_speed(struct dwc2_hsotg *hsotg)
406{
407 int valid = 1;
408 int phy_type = hsotg->params.phy_type;
409 int speed = hsotg->params.speed;
410
411 switch (speed) {
412 case DWC2_SPEED_PARAM_HIGH:
413 if ((hsotg->params.speed == DWC2_SPEED_PARAM_HIGH) &&
414 (phy_type == DWC2_PHY_TYPE_PARAM_FS))
415 valid = 0;
416 break;
417 case DWC2_SPEED_PARAM_FULL:
418 case DWC2_SPEED_PARAM_LOW:
419 break;
420 default:
421 valid = 0;
422 break;
423 }
424
425 if (!valid)
426 dwc2_set_param_speed(hsotg);
427}
428
429static void dwc2_check_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
430{
431 int valid = 0;
432 int param = hsotg->params.phy_utmi_width;
433 int width = hsotg->hw_params.utmi_phy_data_width;
434
435 switch (width) {
436 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
437 valid = (param == 8);
438 break;
439 case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
440 valid = (param == 16);
441 break;
442 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
443 valid = (param == 8 || param == 16);
444 break;
445 }
446
447 if (!valid)
448 dwc2_set_param_phy_utmi_width(hsotg);
449}
450
Sevak Arakelyan3c6aea72017-01-23 15:01:45 -0800451static void dwc2_check_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
452{
453 int fifo_count;
454 int fifo;
455 int min;
456 u32 total = 0;
457 u32 dptxfszn;
458
459 fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
460 min = hsotg->hw_params.en_multiple_tx_fifo ? 16 : 4;
461
462 for (fifo = 1; fifo <= fifo_count; fifo++)
463 total += hsotg->params.g_tx_fifo_size[fifo];
464
465 if (total > dwc2_hsotg_tx_fifo_total_depth(hsotg) || !total) {
466 dev_warn(hsotg->dev, "%s: Invalid parameter g-tx-fifo-size, setting to default average\n",
467 __func__);
468 dwc2_set_param_tx_fifo_sizes(hsotg);
469 }
470
471 for (fifo = 1; fifo <= fifo_count; fifo++) {
472 dptxfszn = (dwc2_readl(hsotg->regs + DPTXFSIZN(fifo)) &
473 FIFOSIZE_DEPTH_MASK) >> FIFOSIZE_DEPTH_SHIFT;
474
475 if (hsotg->params.g_tx_fifo_size[fifo] < min ||
476 hsotg->params.g_tx_fifo_size[fifo] > dptxfszn) {
477 dev_warn(hsotg->dev, "%s: Invalid parameter g_tx_fifo_size[%d]=%d\n",
478 __func__, fifo,
479 hsotg->params.g_tx_fifo_size[fifo]);
480 hsotg->params.g_tx_fifo_size[fifo] = dptxfszn;
481 }
482 }
483}
484
John Yound936e662017-01-23 14:56:43 -0800485#define CHECK_RANGE(_param, _min, _max, _def) do { \
486 if ((hsotg->params._param) < (_min) || \
487 (hsotg->params._param) > (_max)) { \
488 dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
489 __func__, #_param, hsotg->params._param); \
490 hsotg->params._param = (_def); \
491 } \
492 } while (0)
493
494#define CHECK_BOOL(_param, _check) do { \
495 if (hsotg->params._param && !(_check)) { \
496 dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
497 __func__, #_param, hsotg->params._param); \
498 hsotg->params._param = false; \
499 } \
500 } while (0)
501
502static void dwc2_check_params(struct dwc2_hsotg *hsotg)
503{
504 struct dwc2_hw_params *hw = &hsotg->hw_params;
505 struct dwc2_core_params *p = &hsotg->params;
506 bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
507
508 dwc2_check_param_otg_cap(hsotg);
509 dwc2_check_param_phy_type(hsotg);
510 dwc2_check_param_speed(hsotg);
511 dwc2_check_param_phy_utmi_width(hsotg);
512 CHECK_BOOL(enable_dynamic_fifo, hw->enable_dynamic_fifo);
513 CHECK_BOOL(en_multiple_tx_fifo, hw->en_multiple_tx_fifo);
514 CHECK_BOOL(i2c_enable, hw->i2c_enable);
515 CHECK_BOOL(reload_ctl, (hsotg->hw_params.snpsid > DWC2_CORE_REV_2_92a));
516 CHECK_RANGE(max_packet_count,
517 15, hw->max_packet_count,
518 hw->max_packet_count);
519 CHECK_RANGE(max_transfer_size,
520 2047, hw->max_transfer_size,
521 hw->max_transfer_size);
522
523 if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
524 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
525 CHECK_BOOL(host_dma, dma_capable);
526 CHECK_BOOL(dma_desc_enable, p->host_dma);
527 CHECK_BOOL(dma_desc_fs_enable, p->dma_desc_enable);
528 CHECK_BOOL(host_ls_low_power_phy_clk,
529 p->phy_type == DWC2_PHY_TYPE_PARAM_FS);
530 CHECK_RANGE(host_channels,
531 1, hw->host_channels,
532 hw->host_channels);
533 CHECK_RANGE(host_rx_fifo_size,
534 16, hw->rx_fifo_size,
535 hw->rx_fifo_size);
536 CHECK_RANGE(host_nperio_tx_fifo_size,
537 16, hw->host_nperio_tx_fifo_size,
538 hw->host_nperio_tx_fifo_size);
539 CHECK_RANGE(host_perio_tx_fifo_size,
540 16, hw->host_perio_tx_fifo_size,
541 hw->host_perio_tx_fifo_size);
542 }
543
544 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
545 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
546 CHECK_BOOL(g_dma, dma_capable);
547 CHECK_BOOL(g_dma_desc, (p->g_dma && hw->dma_desc_enable));
548 CHECK_RANGE(g_rx_fifo_size,
549 16, hw->rx_fifo_size,
550 hw->rx_fifo_size);
551 CHECK_RANGE(g_np_tx_fifo_size,
552 16, hw->dev_nperio_tx_fifo_size,
553 hw->dev_nperio_tx_fifo_size);
Sevak Arakelyan3c6aea72017-01-23 15:01:45 -0800554 dwc2_check_param_tx_fifo_sizes(hsotg);
John Yound936e662017-01-23 14:56:43 -0800555 }
556}
557
John Youn323230e2016-11-03 17:55:50 -0700558/*
559 * Gets host hardware parameters. Forces host mode if not currently in
560 * host mode. Should be called immediately after a core soft reset in
561 * order to get the reset values.
562 */
563static void dwc2_get_host_hwparams(struct dwc2_hsotg *hsotg)
564{
565 struct dwc2_hw_params *hw = &hsotg->hw_params;
566 u32 gnptxfsiz;
567 u32 hptxfsiz;
568 bool forced;
569
570 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
571 return;
572
573 forced = dwc2_force_mode_if_needed(hsotg, true);
574
575 gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
576 hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
John Youn323230e2016-11-03 17:55:50 -0700577
578 if (forced)
579 dwc2_clear_force_mode(hsotg);
580
581 hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
582 FIFOSIZE_DEPTH_SHIFT;
583 hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
584 FIFOSIZE_DEPTH_SHIFT;
585}
586
587/*
588 * Gets device hardware parameters. Forces device mode if not
589 * currently in device mode. Should be called immediately after a core
590 * soft reset in order to get the reset values.
591 */
592static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
593{
594 struct dwc2_hw_params *hw = &hsotg->hw_params;
595 bool forced;
596 u32 gnptxfsiz;
597
598 if (hsotg->dr_mode == USB_DR_MODE_HOST)
599 return;
600
601 forced = dwc2_force_mode_if_needed(hsotg, false);
602
603 gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
John Youn323230e2016-11-03 17:55:50 -0700604
605 if (forced)
606 dwc2_clear_force_mode(hsotg);
607
608 hw->dev_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
609 FIFOSIZE_DEPTH_SHIFT;
610}
611
612/**
613 * During device initialization, read various hardware configuration
614 * registers and interpret the contents.
615 */
616int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
617{
618 struct dwc2_hw_params *hw = &hsotg->hw_params;
619 unsigned int width;
620 u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
621 u32 grxfsiz;
622
623 /*
624 * Attempt to ensure this device is really a DWC_otg Controller.
625 * Read and verify the GSNPSID register contents. The value should be
626 * 0x45f42xxx or 0x45f43xxx, which corresponds to either "OT2" or "OT3",
627 * as in "OTG version 2.xx" or "OTG version 3.xx".
628 */
629 hw->snpsid = dwc2_readl(hsotg->regs + GSNPSID);
630 if ((hw->snpsid & 0xfffff000) != 0x4f542000 &&
Vardan Mikayelyan1e6b98e2016-11-14 19:16:58 -0800631 (hw->snpsid & 0xfffff000) != 0x4f543000 &&
632 (hw->snpsid & 0xffff0000) != 0x55310000 &&
633 (hw->snpsid & 0xffff0000) != 0x55320000) {
John Youn323230e2016-11-03 17:55:50 -0700634 dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
635 hw->snpsid);
636 return -ENODEV;
637 }
638
639 dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
640 hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
641 hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
642
643 hwcfg1 = dwc2_readl(hsotg->regs + GHWCFG1);
644 hwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
645 hwcfg3 = dwc2_readl(hsotg->regs + GHWCFG3);
646 hwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
647 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
648
John Youn323230e2016-11-03 17:55:50 -0700649 /*
650 * Host specific hardware parameters. Reading these parameters
651 * requires the controller to be in host mode. The mode will
652 * be forced, if necessary, to read these values.
653 */
654 dwc2_get_host_hwparams(hsotg);
655 dwc2_get_dev_hwparams(hsotg);
656
657 /* hwcfg1 */
658 hw->dev_ep_dirs = hwcfg1;
659
660 /* hwcfg2 */
661 hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
662 GHWCFG2_OP_MODE_SHIFT;
663 hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
664 GHWCFG2_ARCHITECTURE_SHIFT;
665 hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
666 hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
667 GHWCFG2_NUM_HOST_CHAN_SHIFT);
668 hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
669 GHWCFG2_HS_PHY_TYPE_SHIFT;
670 hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
671 GHWCFG2_FS_PHY_TYPE_SHIFT;
672 hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
673 GHWCFG2_NUM_DEV_EP_SHIFT;
674 hw->nperio_tx_q_depth =
675 (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
676 GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
677 hw->host_perio_tx_q_depth =
678 (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
679 GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
680 hw->dev_token_q_depth =
681 (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
682 GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
683
684 /* hwcfg3 */
685 width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
686 GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
687 hw->max_transfer_size = (1 << (width + 11)) - 1;
688 width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
689 GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
690 hw->max_packet_count = (1 << (width + 4)) - 1;
691 hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
692 hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
693 GHWCFG3_DFIFO_DEPTH_SHIFT;
694
695 /* hwcfg4 */
696 hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
697 hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
698 GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
699 hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
700 hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
701 hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
702 GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
703
704 /* fifo sizes */
John Yound1531312016-11-03 17:56:02 -0700705 hw->rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
John Youn323230e2016-11-03 17:55:50 -0700706 GRXFSIZ_DEPTH_SHIFT;
707
John Youn323230e2016-11-03 17:55:50 -0700708 return 0;
709}
710
John Youn334bbd42016-11-03 17:55:55 -0700711int dwc2_init_params(struct dwc2_hsotg *hsotg)
712{
John Youn7de1deb2017-01-23 14:57:04 -0800713 const struct of_device_id *match;
714 void (*set_params)(void *data);
715
John Youn245977c2017-01-23 14:55:14 -0800716 dwc2_set_default_params(hsotg);
John Younf9f93cb2017-01-23 14:55:35 -0800717 dwc2_get_device_properties(hsotg);
John Youn334bbd42016-11-03 17:55:55 -0700718
John Youn7de1deb2017-01-23 14:57:04 -0800719 match = of_match_device(dwc2_of_match_table, hsotg->dev);
720 if (match && match->data) {
721 set_params = match->data;
722 set_params(hsotg);
723 }
724
John Yound936e662017-01-23 14:56:43 -0800725 dwc2_check_params(hsotg);
726
John Youn334bbd42016-11-03 17:55:55 -0700727 return 0;
728}