blob: 949d988f13cc7b4703474c4a9c9078f490fcc8d4 [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->otg_cap = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
46 p->speed = DWC2_SPEED_PARAM_HIGH;
47 p->host_rx_fifo_size = 774;
48 p->host_nperio_tx_fifo_size = 256;
49 p->host_perio_tx_fifo_size = 512;
50 p->max_transfer_size = 65535;
51 p->max_packet_count = 511;
52 p->host_channels = 8;
53 p->phy_type = 1;
54 p->phy_utmi_width = 8;
55 p->i2c_enable = false;
56 p->host_ls_low_power_phy_clk = 0;
57 p->reload_ctl = false;
58 p->ahbcfg = 0x10;
59 p->uframe_sched = false;
60}
John Youn323230e2016-11-03 17:55:50 -070061
John Youn7de1deb2017-01-23 14:57:04 -080062static void dwc2_set_his_params(struct dwc2_hsotg *hsotg)
63{
64 struct dwc2_core_params *p = &hsotg->params;
John Youn323230e2016-11-03 17:55:50 -070065
John Youn7de1deb2017-01-23 14:57:04 -080066 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
67 p->speed = DWC2_SPEED_PARAM_HIGH;
68 p->host_rx_fifo_size = 512;
69 p->host_nperio_tx_fifo_size = 512;
70 p->host_perio_tx_fifo_size = 512;
71 p->max_transfer_size = 65535;
72 p->max_packet_count = 511;
73 p->host_channels = 16;
74 p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
75 p->phy_utmi_width = 8;
76 p->i2c_enable = false;
77 p->host_ls_low_power_phy_clk = 0;
78 p->reload_ctl = false;
79 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
80 GAHBCFG_HBSTLEN_SHIFT;
81 p->uframe_sched = false;
82}
John Youn323230e2016-11-03 17:55:50 -070083
John Youn7de1deb2017-01-23 14:57:04 -080084static void dwc2_set_rk_params(struct dwc2_hsotg *hsotg)
85{
86 struct dwc2_core_params *p = &hsotg->params;
87
88 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
89 p->host_rx_fifo_size = 525;
90 p->host_nperio_tx_fifo_size = 128;
91 p->host_perio_tx_fifo_size = 256;
92 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
93 GAHBCFG_HBSTLEN_SHIFT;
94}
95
96static void dwc2_set_ltq_params(struct dwc2_hsotg *hsotg)
97{
98 struct dwc2_core_params *p = &hsotg->params;
99
100 p->otg_cap = 2;
101 p->host_rx_fifo_size = 288;
102 p->host_nperio_tx_fifo_size = 128;
103 p->host_perio_tx_fifo_size = 96;
104 p->max_transfer_size = 65535;
105 p->max_packet_count = 511;
106 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
107 GAHBCFG_HBSTLEN_SHIFT;
108}
109
110static void dwc2_set_amlogic_params(struct dwc2_hsotg *hsotg)
111{
112 struct dwc2_core_params *p = &hsotg->params;
113
114 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
115 p->speed = DWC2_SPEED_PARAM_HIGH;
116 p->host_rx_fifo_size = 512;
117 p->host_nperio_tx_fifo_size = 500;
118 p->host_perio_tx_fifo_size = 500;
119 p->host_channels = 16;
120 p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
121 p->ahbcfg = GAHBCFG_HBSTLEN_INCR8 <<
122 GAHBCFG_HBSTLEN_SHIFT;
123 p->uframe_sched = false;
124}
125
126static void dwc2_set_amcc_params(struct dwc2_hsotg *hsotg)
127{
128 struct dwc2_core_params *p = &hsotg->params;
129
130 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT;
131}
John Youn323230e2016-11-03 17:55:50 -0700132
133const struct of_device_id dwc2_of_match_table[] = {
John Youn7de1deb2017-01-23 14:57:04 -0800134 { .compatible = "brcm,bcm2835-usb", .data = dwc2_set_bcm_params },
135 { .compatible = "hisilicon,hi6220-usb", .data = dwc2_set_his_params },
136 { .compatible = "rockchip,rk3066-usb", .data = dwc2_set_rk_params },
137 { .compatible = "lantiq,arx100-usb", .data = dwc2_set_ltq_params },
138 { .compatible = "lantiq,xrx200-usb", .data = dwc2_set_ltq_params },
139 { .compatible = "snps,dwc2" },
140 { .compatible = "samsung,s3c6400-hsotg" },
141 { .compatible = "amlogic,meson8b-usb",
142 .data = dwc2_set_amlogic_params },
143 { .compatible = "amlogic,meson-gxbb-usb",
144 .data = dwc2_set_amlogic_params },
145 { .compatible = "amcc,dwc-otg", .data = dwc2_set_amcc_params },
John Youn323230e2016-11-03 17:55:50 -0700146 {},
147};
148MODULE_DEVICE_TABLE(of, dwc2_of_match_table);
149
John Youn245977c2017-01-23 14:55:14 -0800150static void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg)
John Youn05ee7992016-11-03 17:56:05 -0700151{
John Youn245977c2017-01-23 14:55:14 -0800152 u8 val;
John Youn05ee7992016-11-03 17:56:05 -0700153
John Youn245977c2017-01-23 14:55:14 -0800154 switch (hsotg->hw_params.op_mode) {
155 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
156 val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
John Youn05ee7992016-11-03 17:56:05 -0700157 break;
John Youn245977c2017-01-23 14:55:14 -0800158 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
159 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
160 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
161 val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
John Youn05ee7992016-11-03 17:56:05 -0700162 break;
163 default:
John Youn245977c2017-01-23 14:55:14 -0800164 val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
John Youn05ee7992016-11-03 17:56:05 -0700165 break;
John Youn323230e2016-11-03 17:55:50 -0700166 }
167
John Younbea8e862016-11-03 17:55:53 -0700168 hsotg->params.otg_cap = val;
John Youn323230e2016-11-03 17:55:50 -0700169}
170
John Youn245977c2017-01-23 14:55:14 -0800171static void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg)
John Youn323230e2016-11-03 17:55:50 -0700172{
John Youn245977c2017-01-23 14:55:14 -0800173 int val;
174 u32 hs_phy_type = hsotg->hw_params.hs_phy_type;
John Youn323230e2016-11-03 17:55:50 -0700175
John Youn245977c2017-01-23 14:55:14 -0800176 val = DWC2_PHY_TYPE_PARAM_FS;
177 if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
178 if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
179 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
180 val = DWC2_PHY_TYPE_PARAM_UTMI;
181 else
182 val = DWC2_PHY_TYPE_PARAM_ULPI;
John Youn323230e2016-11-03 17:55:50 -0700183 }
184
John Youn245977c2017-01-23 14:55:14 -0800185 if (dwc2_is_fs_iot(hsotg))
186 hsotg->params.phy_type = DWC2_PHY_TYPE_PARAM_FS;
John Youn323230e2016-11-03 17:55:50 -0700187
John Younbea8e862016-11-03 17:55:53 -0700188 hsotg->params.phy_type = val;
John Youn323230e2016-11-03 17:55:50 -0700189}
190
John Youn245977c2017-01-23 14:55:14 -0800191static void dwc2_set_param_speed(struct dwc2_hsotg *hsotg)
John Youn323230e2016-11-03 17:55:50 -0700192{
John Youn245977c2017-01-23 14:55:14 -0800193 int val;
John Youn323230e2016-11-03 17:55:50 -0700194
John Youn245977c2017-01-23 14:55:14 -0800195 val = hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS ?
196 DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
John Youn323230e2016-11-03 17:55:50 -0700197
John Youn245977c2017-01-23 14:55:14 -0800198 if (dwc2_is_fs_iot(hsotg))
199 val = DWC2_SPEED_PARAM_FULL;
John Youn323230e2016-11-03 17:55:50 -0700200
John Youn245977c2017-01-23 14:55:14 -0800201 if (dwc2_is_hs_iot(hsotg))
202 val = DWC2_SPEED_PARAM_HIGH;
John Youn323230e2016-11-03 17:55:50 -0700203
John Younbea8e862016-11-03 17:55:53 -0700204 hsotg->params.speed = val;
John Youn323230e2016-11-03 17:55:50 -0700205}
206
John Youn245977c2017-01-23 14:55:14 -0800207static void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
John Youn323230e2016-11-03 17:55:50 -0700208{
John Youn245977c2017-01-23 14:55:14 -0800209 int val;
John Youn323230e2016-11-03 17:55:50 -0700210
John Youn245977c2017-01-23 14:55:14 -0800211 val = (hsotg->hw_params.utmi_phy_data_width ==
212 GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
John Youn323230e2016-11-03 17:55:50 -0700213
John Younbea8e862016-11-03 17:55:53 -0700214 hsotg->params.phy_utmi_width = val;
John Youn323230e2016-11-03 17:55:50 -0700215}
216
John Youn05ee7992016-11-03 17:56:05 -0700217static void dwc2_set_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
218{
John Youn05ee7992016-11-03 17:56:05 -0700219 struct dwc2_core_params *p = &hsotg->params;
John Youn245977c2017-01-23 14:55:14 -0800220 u32 p_tx_fifo[] = DWC2_G_P_LEGACY_TX_FIFO_SIZE;
John Youn05ee7992016-11-03 17:56:05 -0700221
222 memset(p->g_tx_fifo_size, 0, sizeof(p->g_tx_fifo_size));
John Youn245977c2017-01-23 14:55:14 -0800223 memcpy(&p->g_tx_fifo_size[1],
224 p_tx_fifo,
225 sizeof(p_tx_fifo));
John Youn9962b622016-11-09 19:27:40 -0800226}
227
John Youn05ee7992016-11-03 17:56:05 -0700228/**
John Youn245977c2017-01-23 14:55:14 -0800229 * dwc2_set_default_params() - Set all core parameters to their
230 * auto-detected default values.
John Youn323230e2016-11-03 17:55:50 -0700231 */
John Youn245977c2017-01-23 14:55:14 -0800232static void dwc2_set_default_params(struct dwc2_hsotg *hsotg)
John Youn323230e2016-11-03 17:55:50 -0700233{
John Youn05ee7992016-11-03 17:56:05 -0700234 struct dwc2_hw_params *hw = &hsotg->hw_params;
235 struct dwc2_core_params *p = &hsotg->params;
John Youn6b66ce52016-11-03 17:56:12 -0700236 bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
John Youn323230e2016-11-03 17:55:50 -0700237
John Youn245977c2017-01-23 14:55:14 -0800238 dwc2_set_param_otg_cap(hsotg);
239 dwc2_set_param_phy_type(hsotg);
240 dwc2_set_param_speed(hsotg);
241 dwc2_set_param_phy_utmi_width(hsotg);
242 p->phy_ulpi_ddr = false;
243 p->phy_ulpi_ext_vbus = false;
244
245 p->enable_dynamic_fifo = hw->enable_dynamic_fifo;
246 p->en_multiple_tx_fifo = hw->en_multiple_tx_fifo;
247 p->i2c_enable = hw->i2c_enable;
248 p->ulpi_fs_ls = false;
249 p->ts_dline = false;
250 p->reload_ctl = (hw->snpsid >= DWC2_CORE_REV_2_92a);
251 p->uframe_sched = true;
252 p->external_id_pin_ctl = false;
253 p->hibernation = false;
254 p->max_packet_count = hw->max_packet_count;
255 p->max_transfer_size = hw->max_transfer_size;
256 p->ahbcfg = GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT;
257
John Youn6b66ce52016-11-03 17:56:12 -0700258 if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
259 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
John Youn245977c2017-01-23 14:55:14 -0800260 p->host_dma = dma_capable;
261 p->dma_desc_enable = false;
262 p->dma_desc_fs_enable = false;
263 p->host_support_fs_ls_low_power = false;
264 p->host_ls_low_power_phy_clk = false;
265 p->host_channels = hw->host_channels;
266 p->host_rx_fifo_size = hw->rx_fifo_size;
267 p->host_nperio_tx_fifo_size = hw->host_nperio_tx_fifo_size;
268 p->host_perio_tx_fifo_size = hw->host_perio_tx_fifo_size;
John Youn6b66ce52016-11-03 17:56:12 -0700269 }
270
John Youn05ee7992016-11-03 17:56:05 -0700271 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
272 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
John Youn245977c2017-01-23 14:55:14 -0800273 p->g_dma = dma_capable;
274 p->g_dma_desc = hw->dma_desc_enable;
John Youn05ee7992016-11-03 17:56:05 -0700275
276 /*
277 * The values for g_rx_fifo_size (2048) and
278 * g_np_tx_fifo_size (1024) come from the legacy s3c
279 * gadget driver. These defaults have been hard-coded
280 * for some time so many platforms depend on these
281 * values. Leave them as defaults for now and only
282 * auto-detect if the hardware does not support the
283 * default.
284 */
John Youn245977c2017-01-23 14:55:14 -0800285 p->g_rx_fifo_size = 2048;
286 p->g_np_tx_fifo_size = 1024;
John Youn05ee7992016-11-03 17:56:05 -0700287 dwc2_set_param_tx_fifo_sizes(hsotg);
288 }
John Youn323230e2016-11-03 17:55:50 -0700289}
290
John Younf9f93cb2017-01-23 14:55:35 -0800291/**
292 * dwc2_get_device_properties() - Read in device properties.
293 *
294 * Read in the device properties and adjust core parameters if needed.
295 */
296static void dwc2_get_device_properties(struct dwc2_hsotg *hsotg)
297{
298 struct dwc2_core_params *p = &hsotg->params;
299 int num;
300
301 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
302 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
303 device_property_read_u32(hsotg->dev, "g-rx-fifo-size",
304 &p->g_rx_fifo_size);
305
306 device_property_read_u32(hsotg->dev, "g-np-tx-fifo-size",
307 &p->g_np_tx_fifo_size);
308
309 num = device_property_read_u32_array(hsotg->dev,
310 "g-tx-fifo-size",
311 NULL, 0);
312
313 if (num > 0) {
314 num = min(num, 15);
315 memset(p->g_tx_fifo_size, 0,
316 sizeof(p->g_tx_fifo_size));
317 device_property_read_u32_array(hsotg->dev,
318 "g-tx-fifo-size",
319 &p->g_tx_fifo_size[1],
320 num);
321 }
322 }
323}
324
John Yound936e662017-01-23 14:56:43 -0800325static void dwc2_check_param_otg_cap(struct dwc2_hsotg *hsotg)
326{
327 int valid = 1;
328
329 switch (hsotg->params.otg_cap) {
330 case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
331 if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
332 valid = 0;
333 break;
334 case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
335 switch (hsotg->hw_params.op_mode) {
336 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
337 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
338 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
339 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
340 break;
341 default:
342 valid = 0;
343 break;
344 }
345 break;
346 case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
347 /* always valid */
348 break;
349 default:
350 valid = 0;
351 break;
352 }
353
354 if (!valid)
355 dwc2_set_param_otg_cap(hsotg);
356}
357
358static void dwc2_check_param_phy_type(struct dwc2_hsotg *hsotg)
359{
360 int valid = 0;
361 u32 hs_phy_type;
362 u32 fs_phy_type;
363
364 hs_phy_type = hsotg->hw_params.hs_phy_type;
365 fs_phy_type = hsotg->hw_params.fs_phy_type;
366
367 switch (hsotg->params.phy_type) {
368 case DWC2_PHY_TYPE_PARAM_FS:
369 if (fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
370 valid = 1;
371 break;
372 case DWC2_PHY_TYPE_PARAM_UTMI:
373 if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
374 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
375 valid = 1;
376 break;
377 case DWC2_PHY_TYPE_PARAM_ULPI:
378 if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
379 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
380 valid = 1;
381 break;
382 default:
383 break;
384 }
385
386 if (!valid)
387 dwc2_set_param_phy_type(hsotg);
388}
389
390static void dwc2_check_param_speed(struct dwc2_hsotg *hsotg)
391{
392 int valid = 1;
393 int phy_type = hsotg->params.phy_type;
394 int speed = hsotg->params.speed;
395
396 switch (speed) {
397 case DWC2_SPEED_PARAM_HIGH:
398 if ((hsotg->params.speed == DWC2_SPEED_PARAM_HIGH) &&
399 (phy_type == DWC2_PHY_TYPE_PARAM_FS))
400 valid = 0;
401 break;
402 case DWC2_SPEED_PARAM_FULL:
403 case DWC2_SPEED_PARAM_LOW:
404 break;
405 default:
406 valid = 0;
407 break;
408 }
409
410 if (!valid)
411 dwc2_set_param_speed(hsotg);
412}
413
414static void dwc2_check_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
415{
416 int valid = 0;
417 int param = hsotg->params.phy_utmi_width;
418 int width = hsotg->hw_params.utmi_phy_data_width;
419
420 switch (width) {
421 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
422 valid = (param == 8);
423 break;
424 case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
425 valid = (param == 16);
426 break;
427 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
428 valid = (param == 8 || param == 16);
429 break;
430 }
431
432 if (!valid)
433 dwc2_set_param_phy_utmi_width(hsotg);
434}
435
436#define CHECK_RANGE(_param, _min, _max, _def) do { \
437 if ((hsotg->params._param) < (_min) || \
438 (hsotg->params._param) > (_max)) { \
439 dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
440 __func__, #_param, hsotg->params._param); \
441 hsotg->params._param = (_def); \
442 } \
443 } while (0)
444
445#define CHECK_BOOL(_param, _check) do { \
446 if (hsotg->params._param && !(_check)) { \
447 dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
448 __func__, #_param, hsotg->params._param); \
449 hsotg->params._param = false; \
450 } \
451 } while (0)
452
453static void dwc2_check_params(struct dwc2_hsotg *hsotg)
454{
455 struct dwc2_hw_params *hw = &hsotg->hw_params;
456 struct dwc2_core_params *p = &hsotg->params;
457 bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
458
459 dwc2_check_param_otg_cap(hsotg);
460 dwc2_check_param_phy_type(hsotg);
461 dwc2_check_param_speed(hsotg);
462 dwc2_check_param_phy_utmi_width(hsotg);
463 CHECK_BOOL(enable_dynamic_fifo, hw->enable_dynamic_fifo);
464 CHECK_BOOL(en_multiple_tx_fifo, hw->en_multiple_tx_fifo);
465 CHECK_BOOL(i2c_enable, hw->i2c_enable);
466 CHECK_BOOL(reload_ctl, (hsotg->hw_params.snpsid > DWC2_CORE_REV_2_92a));
467 CHECK_RANGE(max_packet_count,
468 15, hw->max_packet_count,
469 hw->max_packet_count);
470 CHECK_RANGE(max_transfer_size,
471 2047, hw->max_transfer_size,
472 hw->max_transfer_size);
473
474 if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
475 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
476 CHECK_BOOL(host_dma, dma_capable);
477 CHECK_BOOL(dma_desc_enable, p->host_dma);
478 CHECK_BOOL(dma_desc_fs_enable, p->dma_desc_enable);
479 CHECK_BOOL(host_ls_low_power_phy_clk,
480 p->phy_type == DWC2_PHY_TYPE_PARAM_FS);
481 CHECK_RANGE(host_channels,
482 1, hw->host_channels,
483 hw->host_channels);
484 CHECK_RANGE(host_rx_fifo_size,
485 16, hw->rx_fifo_size,
486 hw->rx_fifo_size);
487 CHECK_RANGE(host_nperio_tx_fifo_size,
488 16, hw->host_nperio_tx_fifo_size,
489 hw->host_nperio_tx_fifo_size);
490 CHECK_RANGE(host_perio_tx_fifo_size,
491 16, hw->host_perio_tx_fifo_size,
492 hw->host_perio_tx_fifo_size);
493 }
494
495 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
496 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
497 CHECK_BOOL(g_dma, dma_capable);
498 CHECK_BOOL(g_dma_desc, (p->g_dma && hw->dma_desc_enable));
499 CHECK_RANGE(g_rx_fifo_size,
500 16, hw->rx_fifo_size,
501 hw->rx_fifo_size);
502 CHECK_RANGE(g_np_tx_fifo_size,
503 16, hw->dev_nperio_tx_fifo_size,
504 hw->dev_nperio_tx_fifo_size);
505 }
506}
507
John Youn323230e2016-11-03 17:55:50 -0700508/*
509 * Gets host hardware parameters. Forces host mode if not currently in
510 * host mode. Should be called immediately after a core soft reset in
511 * order to get the reset values.
512 */
513static void dwc2_get_host_hwparams(struct dwc2_hsotg *hsotg)
514{
515 struct dwc2_hw_params *hw = &hsotg->hw_params;
516 u32 gnptxfsiz;
517 u32 hptxfsiz;
518 bool forced;
519
520 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
521 return;
522
523 forced = dwc2_force_mode_if_needed(hsotg, true);
524
525 gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
526 hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
527 dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
528 dev_dbg(hsotg->dev, "hptxfsiz=%08x\n", hptxfsiz);
529
530 if (forced)
531 dwc2_clear_force_mode(hsotg);
532
533 hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
534 FIFOSIZE_DEPTH_SHIFT;
535 hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
536 FIFOSIZE_DEPTH_SHIFT;
537}
538
539/*
540 * Gets device hardware parameters. Forces device mode if not
541 * currently in device mode. Should be called immediately after a core
542 * soft reset in order to get the reset values.
543 */
544static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
545{
546 struct dwc2_hw_params *hw = &hsotg->hw_params;
547 bool forced;
548 u32 gnptxfsiz;
549
550 if (hsotg->dr_mode == USB_DR_MODE_HOST)
551 return;
552
553 forced = dwc2_force_mode_if_needed(hsotg, false);
554
555 gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
556 dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
557
558 if (forced)
559 dwc2_clear_force_mode(hsotg);
560
561 hw->dev_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
562 FIFOSIZE_DEPTH_SHIFT;
563}
564
565/**
566 * During device initialization, read various hardware configuration
567 * registers and interpret the contents.
568 */
569int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
570{
571 struct dwc2_hw_params *hw = &hsotg->hw_params;
572 unsigned int width;
573 u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
574 u32 grxfsiz;
575
576 /*
577 * Attempt to ensure this device is really a DWC_otg Controller.
578 * Read and verify the GSNPSID register contents. The value should be
579 * 0x45f42xxx or 0x45f43xxx, which corresponds to either "OT2" or "OT3",
580 * as in "OTG version 2.xx" or "OTG version 3.xx".
581 */
582 hw->snpsid = dwc2_readl(hsotg->regs + GSNPSID);
583 if ((hw->snpsid & 0xfffff000) != 0x4f542000 &&
Vardan Mikayelyan1e6b98e2016-11-14 19:16:58 -0800584 (hw->snpsid & 0xfffff000) != 0x4f543000 &&
585 (hw->snpsid & 0xffff0000) != 0x55310000 &&
586 (hw->snpsid & 0xffff0000) != 0x55320000) {
John Youn323230e2016-11-03 17:55:50 -0700587 dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
588 hw->snpsid);
589 return -ENODEV;
590 }
591
592 dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
593 hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
594 hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
595
596 hwcfg1 = dwc2_readl(hsotg->regs + GHWCFG1);
597 hwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
598 hwcfg3 = dwc2_readl(hsotg->regs + GHWCFG3);
599 hwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
600 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
601
602 dev_dbg(hsotg->dev, "hwcfg1=%08x\n", hwcfg1);
603 dev_dbg(hsotg->dev, "hwcfg2=%08x\n", hwcfg2);
604 dev_dbg(hsotg->dev, "hwcfg3=%08x\n", hwcfg3);
605 dev_dbg(hsotg->dev, "hwcfg4=%08x\n", hwcfg4);
606 dev_dbg(hsotg->dev, "grxfsiz=%08x\n", grxfsiz);
607
608 /*
609 * Host specific hardware parameters. Reading these parameters
610 * requires the controller to be in host mode. The mode will
611 * be forced, if necessary, to read these values.
612 */
613 dwc2_get_host_hwparams(hsotg);
614 dwc2_get_dev_hwparams(hsotg);
615
616 /* hwcfg1 */
617 hw->dev_ep_dirs = hwcfg1;
618
619 /* hwcfg2 */
620 hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
621 GHWCFG2_OP_MODE_SHIFT;
622 hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
623 GHWCFG2_ARCHITECTURE_SHIFT;
624 hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
625 hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
626 GHWCFG2_NUM_HOST_CHAN_SHIFT);
627 hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
628 GHWCFG2_HS_PHY_TYPE_SHIFT;
629 hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
630 GHWCFG2_FS_PHY_TYPE_SHIFT;
631 hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
632 GHWCFG2_NUM_DEV_EP_SHIFT;
633 hw->nperio_tx_q_depth =
634 (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
635 GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
636 hw->host_perio_tx_q_depth =
637 (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
638 GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
639 hw->dev_token_q_depth =
640 (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
641 GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
642
643 /* hwcfg3 */
644 width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
645 GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
646 hw->max_transfer_size = (1 << (width + 11)) - 1;
647 width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
648 GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
649 hw->max_packet_count = (1 << (width + 4)) - 1;
650 hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
651 hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
652 GHWCFG3_DFIFO_DEPTH_SHIFT;
653
654 /* hwcfg4 */
655 hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
656 hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
657 GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
658 hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
659 hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
660 hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
661 GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
662
663 /* fifo sizes */
John Yound1531312016-11-03 17:56:02 -0700664 hw->rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
John Youn323230e2016-11-03 17:55:50 -0700665 GRXFSIZ_DEPTH_SHIFT;
666
667 dev_dbg(hsotg->dev, "Detected values from hardware:\n");
668 dev_dbg(hsotg->dev, " op_mode=%d\n",
669 hw->op_mode);
670 dev_dbg(hsotg->dev, " arch=%d\n",
671 hw->arch);
672 dev_dbg(hsotg->dev, " dma_desc_enable=%d\n",
673 hw->dma_desc_enable);
674 dev_dbg(hsotg->dev, " power_optimized=%d\n",
675 hw->power_optimized);
676 dev_dbg(hsotg->dev, " i2c_enable=%d\n",
677 hw->i2c_enable);
678 dev_dbg(hsotg->dev, " hs_phy_type=%d\n",
679 hw->hs_phy_type);
680 dev_dbg(hsotg->dev, " fs_phy_type=%d\n",
681 hw->fs_phy_type);
682 dev_dbg(hsotg->dev, " utmi_phy_data_width=%d\n",
683 hw->utmi_phy_data_width);
684 dev_dbg(hsotg->dev, " num_dev_ep=%d\n",
685 hw->num_dev_ep);
686 dev_dbg(hsotg->dev, " num_dev_perio_in_ep=%d\n",
687 hw->num_dev_perio_in_ep);
688 dev_dbg(hsotg->dev, " host_channels=%d\n",
689 hw->host_channels);
690 dev_dbg(hsotg->dev, " max_transfer_size=%d\n",
691 hw->max_transfer_size);
692 dev_dbg(hsotg->dev, " max_packet_count=%d\n",
693 hw->max_packet_count);
694 dev_dbg(hsotg->dev, " nperio_tx_q_depth=0x%0x\n",
695 hw->nperio_tx_q_depth);
696 dev_dbg(hsotg->dev, " host_perio_tx_q_depth=0x%0x\n",
697 hw->host_perio_tx_q_depth);
698 dev_dbg(hsotg->dev, " dev_token_q_depth=0x%0x\n",
699 hw->dev_token_q_depth);
700 dev_dbg(hsotg->dev, " enable_dynamic_fifo=%d\n",
701 hw->enable_dynamic_fifo);
702 dev_dbg(hsotg->dev, " en_multiple_tx_fifo=%d\n",
703 hw->en_multiple_tx_fifo);
704 dev_dbg(hsotg->dev, " total_fifo_size=%d\n",
705 hw->total_fifo_size);
John Yound1531312016-11-03 17:56:02 -0700706 dev_dbg(hsotg->dev, " rx_fifo_size=%d\n",
707 hw->rx_fifo_size);
John Youn323230e2016-11-03 17:55:50 -0700708 dev_dbg(hsotg->dev, " host_nperio_tx_fifo_size=%d\n",
709 hw->host_nperio_tx_fifo_size);
710 dev_dbg(hsotg->dev, " host_perio_tx_fifo_size=%d\n",
711 hw->host_perio_tx_fifo_size);
712 dev_dbg(hsotg->dev, "\n");
713
714 return 0;
715}
716
John Youn334bbd42016-11-03 17:55:55 -0700717int dwc2_init_params(struct dwc2_hsotg *hsotg)
718{
John Youn7de1deb2017-01-23 14:57:04 -0800719 const struct of_device_id *match;
720 void (*set_params)(void *data);
721
John Youn245977c2017-01-23 14:55:14 -0800722 dwc2_set_default_params(hsotg);
John Younf9f93cb2017-01-23 14:55:35 -0800723 dwc2_get_device_properties(hsotg);
John Youn334bbd42016-11-03 17:55:55 -0700724
John Youn7de1deb2017-01-23 14:57:04 -0800725 match = of_match_device(dwc2_of_match_table, hsotg->dev);
726 if (match && match->data) {
727 set_params = match->data;
728 set_params(hsotg);
729 }
730
John Yound936e662017-01-23 14:56:43 -0800731 dwc2_check_params(hsotg);
732
John Youn334bbd42016-11-03 17:55:55 -0700733 return 0;
734}