blob: 335144a791bece1f5d76d2f80a8d3dd04a6ac500 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2/*
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
42static void dwc2_set_bcm_params(struct dwc2_hsotg *hsotg)
43{
44 struct dwc2_core_params *p = &hsotg->params;
45
46 p->host_rx_fifo_size = 774;
47 p->max_transfer_size = 65535;
48 p->max_packet_count = 511;
49 p->ahbcfg = 0x10;
50}
51
52static void dwc2_set_his_params(struct dwc2_hsotg *hsotg)
53{
54 struct dwc2_core_params *p = &hsotg->params;
55
56 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;
67 p->reload_ctl = false;
68 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
69 GAHBCFG_HBSTLEN_SHIFT;
70 p->change_speed_quirk = true;
71 p->power_down = false;
72}
73
74static void dwc2_set_s3c6400_params(struct dwc2_hsotg *hsotg)
75{
76 struct dwc2_core_params *p = &hsotg->params;
77
78 p->power_down = 0;
79}
80
81static void dwc2_set_rk_params(struct dwc2_hsotg *hsotg)
82{
83 struct dwc2_core_params *p = &hsotg->params;
84
85 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
86 p->host_rx_fifo_size = 525;
87 p->host_nperio_tx_fifo_size = 128;
88 p->host_perio_tx_fifo_size = 256;
89 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
90 GAHBCFG_HBSTLEN_SHIFT;
91 p->power_down = 0;
92}
93
94static void dwc2_set_ltq_params(struct dwc2_hsotg *hsotg)
95{
96 struct dwc2_core_params *p = &hsotg->params;
97
98 p->otg_cap = 2;
99 p->host_rx_fifo_size = 288;
100 p->host_nperio_tx_fifo_size = 128;
101 p->host_perio_tx_fifo_size = 96;
102 p->max_transfer_size = 65535;
103 p->max_packet_count = 511;
104 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 <<
105 GAHBCFG_HBSTLEN_SHIFT;
106}
107
108static void dwc2_set_amlogic_params(struct dwc2_hsotg *hsotg)
109{
110 struct dwc2_core_params *p = &hsotg->params;
111
112 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
113 p->speed = DWC2_SPEED_PARAM_HIGH;
114 p->host_rx_fifo_size = 512;
115 p->host_nperio_tx_fifo_size = 500;
116 p->host_perio_tx_fifo_size = 500;
117 p->host_channels = 16;
118 p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI;
119 p->ahbcfg = GAHBCFG_HBSTLEN_INCR8 <<
120 GAHBCFG_HBSTLEN_SHIFT;
121 p->power_down = DWC2_POWER_DOWN_PARAM_NONE;
122}
123
124static void dwc2_set_amcc_params(struct dwc2_hsotg *hsotg)
125{
126 struct dwc2_core_params *p = &hsotg->params;
127
128 p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT;
129}
130
131static void dwc2_set_stm32f4x9_fsotg_params(struct dwc2_hsotg *hsotg)
132{
133 struct dwc2_core_params *p = &hsotg->params;
134
135 p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
136 p->speed = DWC2_SPEED_PARAM_FULL;
137 p->host_rx_fifo_size = 128;
138 p->host_nperio_tx_fifo_size = 96;
139 p->host_perio_tx_fifo_size = 96;
140 p->max_packet_count = 256;
141 p->phy_type = DWC2_PHY_TYPE_PARAM_FS;
142 p->i2c_enable = false;
143 p->activate_stm_fs_transceiver = true;
144}
145
146static void dwc2_set_stm32f7_hsotg_params(struct dwc2_hsotg *hsotg)
147{
148 struct dwc2_core_params *p = &hsotg->params;
149
150 p->host_rx_fifo_size = 622;
151 p->host_nperio_tx_fifo_size = 128;
152 p->host_perio_tx_fifo_size = 256;
153}
154
155const struct of_device_id dwc2_of_match_table[] = {
156 { .compatible = "brcm,bcm2835-usb", .data = dwc2_set_bcm_params },
157 { .compatible = "hisilicon,hi6220-usb", .data = dwc2_set_his_params },
158 { .compatible = "rockchip,rk3066-usb", .data = dwc2_set_rk_params },
159 { .compatible = "lantiq,arx100-usb", .data = dwc2_set_ltq_params },
160 { .compatible = "lantiq,xrx200-usb", .data = dwc2_set_ltq_params },
161 { .compatible = "snps,dwc2" },
162 { .compatible = "samsung,s3c6400-hsotg",
163 .data = dwc2_set_s3c6400_params },
164 { .compatible = "amlogic,meson8-usb",
165 .data = dwc2_set_amlogic_params },
166 { .compatible = "amlogic,meson8b-usb",
167 .data = dwc2_set_amlogic_params },
168 { .compatible = "amlogic,meson-gxbb-usb",
169 .data = dwc2_set_amlogic_params },
170 { .compatible = "amcc,dwc-otg", .data = dwc2_set_amcc_params },
171 { .compatible = "st,stm32f4x9-fsotg",
172 .data = dwc2_set_stm32f4x9_fsotg_params },
173 { .compatible = "st,stm32f4x9-hsotg" },
174 { .compatible = "st,stm32f7-hsotg",
175 .data = dwc2_set_stm32f7_hsotg_params },
176 {},
177};
178MODULE_DEVICE_TABLE(of, dwc2_of_match_table);
179
180static void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg)
181{
182 u8 val;
183
184 switch (hsotg->hw_params.op_mode) {
185 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
186 val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
187 break;
188 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
189 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
190 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
191 val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
192 break;
193 default:
194 val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
195 break;
196 }
197
198 hsotg->params.otg_cap = val;
199}
200
201static void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg)
202{
203 int val;
204 u32 hs_phy_type = hsotg->hw_params.hs_phy_type;
205
206 val = DWC2_PHY_TYPE_PARAM_FS;
207 if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
208 if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
209 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
210 val = DWC2_PHY_TYPE_PARAM_UTMI;
211 else
212 val = DWC2_PHY_TYPE_PARAM_ULPI;
213 }
214
215 if (dwc2_is_fs_iot(hsotg))
216 hsotg->params.phy_type = DWC2_PHY_TYPE_PARAM_FS;
217
218 hsotg->params.phy_type = val;
219}
220
221static void dwc2_set_param_speed(struct dwc2_hsotg *hsotg)
222{
223 int val;
224
225 val = hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS ?
226 DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
227
228 if (dwc2_is_fs_iot(hsotg))
229 val = DWC2_SPEED_PARAM_FULL;
230
231 if (dwc2_is_hs_iot(hsotg))
232 val = DWC2_SPEED_PARAM_HIGH;
233
234 hsotg->params.speed = val;
235}
236
237static void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
238{
239 int val;
240
241 val = (hsotg->hw_params.utmi_phy_data_width ==
242 GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
243
244 hsotg->params.phy_utmi_width = val;
245}
246
247static void dwc2_set_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
248{
249 struct dwc2_core_params *p = &hsotg->params;
250 int depth_average;
251 int fifo_count;
252 int i;
253
254 fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
255
256 memset(p->g_tx_fifo_size, 0, sizeof(p->g_tx_fifo_size));
257 depth_average = dwc2_hsotg_tx_fifo_average_depth(hsotg);
258 for (i = 1; i <= fifo_count; i++)
259 p->g_tx_fifo_size[i] = depth_average;
260}
261
262static void dwc2_set_param_power_down(struct dwc2_hsotg *hsotg)
263{
264 int val;
265
266 if (hsotg->hw_params.hibernation)
267 val = 2;
268 else if (hsotg->hw_params.power_optimized)
269 val = 1;
270 else
271 val = 0;
272
273 hsotg->params.power_down = val;
274}
275
276static void dwc2_set_param_lpm(struct dwc2_hsotg *hsotg)
277{
278 struct dwc2_core_params *p = &hsotg->params;
279
280 p->lpm = hsotg->hw_params.lpm_mode;
281 if (p->lpm) {
282 p->lpm_clock_gating = true;
283 p->besl = true;
284 p->hird_threshold_en = true;
285 p->hird_threshold = 4;
286 } else {
287 p->lpm_clock_gating = false;
288 p->besl = false;
289 p->hird_threshold_en = false;
290 }
291}
292
293/**
294 * dwc2_set_default_params() - Set all core parameters to their
295 * auto-detected default values.
296 *
297 * @hsotg: Programming view of the DWC_otg controller
298 *
299 */
300static void dwc2_set_default_params(struct dwc2_hsotg *hsotg)
301{
302 struct dwc2_hw_params *hw = &hsotg->hw_params;
303 struct dwc2_core_params *p = &hsotg->params;
304 bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
305
306 dwc2_set_param_otg_cap(hsotg);
307 dwc2_set_param_phy_type(hsotg);
308 dwc2_set_param_speed(hsotg);
309 dwc2_set_param_phy_utmi_width(hsotg);
310 dwc2_set_param_power_down(hsotg);
311 dwc2_set_param_lpm(hsotg);
312 p->phy_ulpi_ddr = false;
313 p->phy_ulpi_ext_vbus = false;
314
315 p->enable_dynamic_fifo = hw->enable_dynamic_fifo;
316 p->en_multiple_tx_fifo = hw->en_multiple_tx_fifo;
317 p->i2c_enable = hw->i2c_enable;
318 p->acg_enable = hw->acg_enable;
319 p->ulpi_fs_ls = false;
320 p->ts_dline = false;
321 p->reload_ctl = (hw->snpsid >= DWC2_CORE_REV_2_92a);
322 p->uframe_sched = true;
323 p->external_id_pin_ctl = false;
324 p->ipg_isoc_en = false;
325 p->max_packet_count = hw->max_packet_count;
326 p->max_transfer_size = hw->max_transfer_size;
327 p->ahbcfg = GAHBCFG_HBSTLEN_INCR << GAHBCFG_HBSTLEN_SHIFT;
328
329 if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
330 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
331 p->host_dma = dma_capable;
332 p->dma_desc_enable = false;
333 p->dma_desc_fs_enable = false;
334 p->host_support_fs_ls_low_power = false;
335 p->host_ls_low_power_phy_clk = false;
336 p->host_channels = hw->host_channels;
337 p->host_rx_fifo_size = hw->rx_fifo_size;
338 p->host_nperio_tx_fifo_size = hw->host_nperio_tx_fifo_size;
339 p->host_perio_tx_fifo_size = hw->host_perio_tx_fifo_size;
340 }
341
342 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
343 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
344 p->g_dma = dma_capable;
345 p->g_dma_desc = hw->dma_desc_enable;
346
347 /*
348 * The values for g_rx_fifo_size (2048) and
349 * g_np_tx_fifo_size (1024) come from the legacy s3c
350 * gadget driver. These defaults have been hard-coded
351 * for some time so many platforms depend on these
352 * values. Leave them as defaults for now and only
353 * auto-detect if the hardware does not support the
354 * default.
355 */
356 p->g_rx_fifo_size = 2048;
357 p->g_np_tx_fifo_size = 1024;
358 dwc2_set_param_tx_fifo_sizes(hsotg);
359 }
360}
361
362/**
363 * dwc2_get_device_properties() - Read in device properties.
364 *
365 * @hsotg: Programming view of the DWC_otg controller
366 *
367 * Read in the device properties and adjust core parameters if needed.
368 */
369static void dwc2_get_device_properties(struct dwc2_hsotg *hsotg)
370{
371 struct dwc2_core_params *p = &hsotg->params;
372 int num;
373
374 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
375 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
376 device_property_read_u32(hsotg->dev, "g-rx-fifo-size",
377 &p->g_rx_fifo_size);
378
379 device_property_read_u32(hsotg->dev, "g-np-tx-fifo-size",
380 &p->g_np_tx_fifo_size);
381
382 num = device_property_read_u32_array(hsotg->dev,
383 "g-tx-fifo-size",
384 NULL, 0);
385
386 if (num > 0) {
387 num = min(num, 15);
388 memset(p->g_tx_fifo_size, 0,
389 sizeof(p->g_tx_fifo_size));
390 device_property_read_u32_array(hsotg->dev,
391 "g-tx-fifo-size",
392 &p->g_tx_fifo_size[1],
393 num);
394 }
395 }
396
397 if (of_find_property(hsotg->dev->of_node, "disable-over-current", NULL))
398 p->oc_disable = true;
399}
400
401static void dwc2_check_param_otg_cap(struct dwc2_hsotg *hsotg)
402{
403 int valid = 1;
404
405 switch (hsotg->params.otg_cap) {
406 case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
407 if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
408 valid = 0;
409 break;
410 case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
411 switch (hsotg->hw_params.op_mode) {
412 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
413 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
414 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
415 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
416 break;
417 default:
418 valid = 0;
419 break;
420 }
421 break;
422 case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
423 /* always valid */
424 break;
425 default:
426 valid = 0;
427 break;
428 }
429
430 if (!valid)
431 dwc2_set_param_otg_cap(hsotg);
432}
433
434static void dwc2_check_param_phy_type(struct dwc2_hsotg *hsotg)
435{
436 int valid = 0;
437 u32 hs_phy_type;
438 u32 fs_phy_type;
439
440 hs_phy_type = hsotg->hw_params.hs_phy_type;
441 fs_phy_type = hsotg->hw_params.fs_phy_type;
442
443 switch (hsotg->params.phy_type) {
444 case DWC2_PHY_TYPE_PARAM_FS:
445 if (fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
446 valid = 1;
447 break;
448 case DWC2_PHY_TYPE_PARAM_UTMI:
449 if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
450 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
451 valid = 1;
452 break;
453 case DWC2_PHY_TYPE_PARAM_ULPI:
454 if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) ||
455 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
456 valid = 1;
457 break;
458 default:
459 break;
460 }
461
462 if (!valid)
463 dwc2_set_param_phy_type(hsotg);
464}
465
466static void dwc2_check_param_speed(struct dwc2_hsotg *hsotg)
467{
468 int valid = 1;
469 int phy_type = hsotg->params.phy_type;
470 int speed = hsotg->params.speed;
471
472 switch (speed) {
473 case DWC2_SPEED_PARAM_HIGH:
474 if ((hsotg->params.speed == DWC2_SPEED_PARAM_HIGH) &&
475 (phy_type == DWC2_PHY_TYPE_PARAM_FS))
476 valid = 0;
477 break;
478 case DWC2_SPEED_PARAM_FULL:
479 case DWC2_SPEED_PARAM_LOW:
480 break;
481 default:
482 valid = 0;
483 break;
484 }
485
486 if (!valid)
487 dwc2_set_param_speed(hsotg);
488}
489
490static void dwc2_check_param_phy_utmi_width(struct dwc2_hsotg *hsotg)
491{
492 int valid = 0;
493 int param = hsotg->params.phy_utmi_width;
494 int width = hsotg->hw_params.utmi_phy_data_width;
495
496 switch (width) {
497 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
498 valid = (param == 8);
499 break;
500 case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
501 valid = (param == 16);
502 break;
503 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
504 valid = (param == 8 || param == 16);
505 break;
506 }
507
508 if (!valid)
509 dwc2_set_param_phy_utmi_width(hsotg);
510}
511
512static void dwc2_check_param_power_down(struct dwc2_hsotg *hsotg)
513{
514 int param = hsotg->params.power_down;
515
516 switch (param) {
517 case DWC2_POWER_DOWN_PARAM_NONE:
518 break;
519 case DWC2_POWER_DOWN_PARAM_PARTIAL:
520 if (hsotg->hw_params.power_optimized)
521 break;
522 dev_dbg(hsotg->dev,
523 "Partial power down isn't supported by HW\n");
524 param = DWC2_POWER_DOWN_PARAM_NONE;
525 break;
526 case DWC2_POWER_DOWN_PARAM_HIBERNATION:
527 if (hsotg->hw_params.hibernation)
528 break;
529 dev_dbg(hsotg->dev,
530 "Hibernation isn't supported by HW\n");
531 param = DWC2_POWER_DOWN_PARAM_NONE;
532 break;
533 default:
534 dev_err(hsotg->dev,
535 "%s: Invalid parameter power_down=%d\n",
536 __func__, param);
537 param = DWC2_POWER_DOWN_PARAM_NONE;
538 break;
539 }
540
541 hsotg->params.power_down = param;
542}
543
544static void dwc2_check_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg)
545{
546 int fifo_count;
547 int fifo;
548 int min;
549 u32 total = 0;
550 u32 dptxfszn;
551
552 fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
553 min = hsotg->hw_params.en_multiple_tx_fifo ? 16 : 4;
554
555 for (fifo = 1; fifo <= fifo_count; fifo++)
556 total += hsotg->params.g_tx_fifo_size[fifo];
557
558 if (total > dwc2_hsotg_tx_fifo_total_depth(hsotg) || !total) {
559 dev_warn(hsotg->dev, "%s: Invalid parameter g-tx-fifo-size, setting to default average\n",
560 __func__);
561 dwc2_set_param_tx_fifo_sizes(hsotg);
562 }
563
564 for (fifo = 1; fifo <= fifo_count; fifo++) {
565 dptxfszn = hsotg->hw_params.g_tx_fifo_size[fifo];
566
567 if (hsotg->params.g_tx_fifo_size[fifo] < min ||
568 hsotg->params.g_tx_fifo_size[fifo] > dptxfszn) {
569 dev_warn(hsotg->dev, "%s: Invalid parameter g_tx_fifo_size[%d]=%d\n",
570 __func__, fifo,
571 hsotg->params.g_tx_fifo_size[fifo]);
572 hsotg->params.g_tx_fifo_size[fifo] = dptxfszn;
573 }
574 }
575}
576
577#define CHECK_RANGE(_param, _min, _max, _def) do { \
578 if ((int)(hsotg->params._param) < (_min) || \
579 (hsotg->params._param) > (_max)) { \
580 dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
581 __func__, #_param, hsotg->params._param); \
582 hsotg->params._param = (_def); \
583 } \
584 } while (0)
585
586#define CHECK_BOOL(_param, _check) do { \
587 if (hsotg->params._param && !(_check)) { \
588 dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \
589 __func__, #_param, hsotg->params._param); \
590 hsotg->params._param = false; \
591 } \
592 } while (0)
593
594static void dwc2_check_params(struct dwc2_hsotg *hsotg)
595{
596 struct dwc2_hw_params *hw = &hsotg->hw_params;
597 struct dwc2_core_params *p = &hsotg->params;
598 bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH);
599
600 dwc2_check_param_otg_cap(hsotg);
601 dwc2_check_param_phy_type(hsotg);
602 dwc2_check_param_speed(hsotg);
603 dwc2_check_param_phy_utmi_width(hsotg);
604 dwc2_check_param_power_down(hsotg);
605 CHECK_BOOL(enable_dynamic_fifo, hw->enable_dynamic_fifo);
606 CHECK_BOOL(en_multiple_tx_fifo, hw->en_multiple_tx_fifo);
607 CHECK_BOOL(i2c_enable, hw->i2c_enable);
608 CHECK_BOOL(ipg_isoc_en, hw->ipg_isoc_en);
609 CHECK_BOOL(acg_enable, hw->acg_enable);
610 CHECK_BOOL(reload_ctl, (hsotg->hw_params.snpsid > DWC2_CORE_REV_2_92a));
611 CHECK_BOOL(lpm, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_80a));
612 CHECK_BOOL(lpm, hw->lpm_mode);
613 CHECK_BOOL(lpm_clock_gating, hsotg->params.lpm);
614 CHECK_BOOL(besl, hsotg->params.lpm);
615 CHECK_BOOL(besl, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a));
616 CHECK_BOOL(hird_threshold_en, hsotg->params.lpm);
617 CHECK_RANGE(hird_threshold, 0, hsotg->params.besl ? 12 : 7, 0);
618 CHECK_RANGE(max_packet_count,
619 15, hw->max_packet_count,
620 hw->max_packet_count);
621 CHECK_RANGE(max_transfer_size,
622 2047, hw->max_transfer_size,
623 hw->max_transfer_size);
624
625 if ((hsotg->dr_mode == USB_DR_MODE_HOST) ||
626 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
627 CHECK_BOOL(host_dma, dma_capable);
628 CHECK_BOOL(dma_desc_enable, p->host_dma);
629 CHECK_BOOL(dma_desc_fs_enable, p->dma_desc_enable);
630 CHECK_BOOL(host_ls_low_power_phy_clk,
631 p->phy_type == DWC2_PHY_TYPE_PARAM_FS);
632 CHECK_RANGE(host_channels,
633 1, hw->host_channels,
634 hw->host_channels);
635 CHECK_RANGE(host_rx_fifo_size,
636 16, hw->rx_fifo_size,
637 hw->rx_fifo_size);
638 CHECK_RANGE(host_nperio_tx_fifo_size,
639 16, hw->host_nperio_tx_fifo_size,
640 hw->host_nperio_tx_fifo_size);
641 CHECK_RANGE(host_perio_tx_fifo_size,
642 16, hw->host_perio_tx_fifo_size,
643 hw->host_perio_tx_fifo_size);
644 }
645
646 if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) ||
647 (hsotg->dr_mode == USB_DR_MODE_OTG)) {
648 CHECK_BOOL(g_dma, dma_capable);
649 CHECK_BOOL(g_dma_desc, (p->g_dma && hw->dma_desc_enable));
650 CHECK_RANGE(g_rx_fifo_size,
651 16, hw->rx_fifo_size,
652 hw->rx_fifo_size);
653 CHECK_RANGE(g_np_tx_fifo_size,
654 16, hw->dev_nperio_tx_fifo_size,
655 hw->dev_nperio_tx_fifo_size);
656 dwc2_check_param_tx_fifo_sizes(hsotg);
657 }
658}
659
660/*
661 * Gets host hardware parameters. Forces host mode if not currently in
662 * host mode. Should be called immediately after a core soft reset in
663 * order to get the reset values.
664 */
665static void dwc2_get_host_hwparams(struct dwc2_hsotg *hsotg)
666{
667 struct dwc2_hw_params *hw = &hsotg->hw_params;
668 u32 gnptxfsiz;
669 u32 hptxfsiz;
670
671 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
672 return;
673
674 dwc2_force_mode(hsotg, true);
675
676 gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
677 hptxfsiz = dwc2_readl(hsotg, HPTXFSIZ);
678
679 hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
680 FIFOSIZE_DEPTH_SHIFT;
681 hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
682 FIFOSIZE_DEPTH_SHIFT;
683}
684
685/*
686 * Gets device hardware parameters. Forces device mode if not
687 * currently in device mode. Should be called immediately after a core
688 * soft reset in order to get the reset values.
689 */
690static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg)
691{
692 struct dwc2_hw_params *hw = &hsotg->hw_params;
693 u32 gnptxfsiz;
694 int fifo, fifo_count;
695
696 if (hsotg->dr_mode == USB_DR_MODE_HOST)
697 return;
698
699 dwc2_force_mode(hsotg, false);
700
701 gnptxfsiz = dwc2_readl(hsotg, GNPTXFSIZ);
702
703 fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
704
705 for (fifo = 1; fifo <= fifo_count; fifo++) {
706 hw->g_tx_fifo_size[fifo] =
707 (dwc2_readl(hsotg, DPTXFSIZN(fifo)) &
708 FIFOSIZE_DEPTH_MASK) >> FIFOSIZE_DEPTH_SHIFT;
709 }
710
711 hw->dev_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
712 FIFOSIZE_DEPTH_SHIFT;
713}
714
715/**
716 * During device initialization, read various hardware configuration
717 * registers and interpret the contents.
718 *
719 * @hsotg: Programming view of the DWC_otg controller
720 *
721 */
722int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
723{
724 struct dwc2_hw_params *hw = &hsotg->hw_params;
725 unsigned int width;
726 u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
727 u32 grxfsiz;
728
729 /*
730 * Attempt to ensure this device is really a DWC_otg Controller.
731 * Read and verify the GSNPSID register contents. The value should be
732 * 0x45f4xxxx, 0x5531xxxx or 0x5532xxxx
733 */
734
735 hw->snpsid = dwc2_readl(hsotg, GSNPSID);
736 if ((hw->snpsid & GSNPSID_ID_MASK) != DWC2_OTG_ID &&
737 (hw->snpsid & GSNPSID_ID_MASK) != DWC2_FS_IOT_ID &&
738 (hw->snpsid & GSNPSID_ID_MASK) != DWC2_HS_IOT_ID) {
739 dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
740 hw->snpsid);
741 return -ENODEV;
742 }
743
744 dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
745 hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
746 hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
747
748 hwcfg1 = dwc2_readl(hsotg, GHWCFG1);
749 hwcfg2 = dwc2_readl(hsotg, GHWCFG2);
750 hwcfg3 = dwc2_readl(hsotg, GHWCFG3);
751 hwcfg4 = dwc2_readl(hsotg, GHWCFG4);
752 grxfsiz = dwc2_readl(hsotg, GRXFSIZ);
753
754 /* hwcfg1 */
755 hw->dev_ep_dirs = hwcfg1;
756
757 /* hwcfg2 */
758 hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
759 GHWCFG2_OP_MODE_SHIFT;
760 hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
761 GHWCFG2_ARCHITECTURE_SHIFT;
762 hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
763 hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
764 GHWCFG2_NUM_HOST_CHAN_SHIFT);
765 hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
766 GHWCFG2_HS_PHY_TYPE_SHIFT;
767 hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
768 GHWCFG2_FS_PHY_TYPE_SHIFT;
769 hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
770 GHWCFG2_NUM_DEV_EP_SHIFT;
771 hw->nperio_tx_q_depth =
772 (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
773 GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
774 hw->host_perio_tx_q_depth =
775 (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
776 GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
777 hw->dev_token_q_depth =
778 (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
779 GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
780
781 /* hwcfg3 */
782 width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
783 GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
784 hw->max_transfer_size = (1 << (width + 11)) - 1;
785 width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
786 GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
787 hw->max_packet_count = (1 << (width + 4)) - 1;
788 hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
789 hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
790 GHWCFG3_DFIFO_DEPTH_SHIFT;
791 hw->lpm_mode = !!(hwcfg3 & GHWCFG3_OTG_LPM_EN);
792
793 /* hwcfg4 */
794 hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
795 hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
796 GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
797 hw->num_dev_in_eps = (hwcfg4 & GHWCFG4_NUM_IN_EPS_MASK) >>
798 GHWCFG4_NUM_IN_EPS_SHIFT;
799 hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
800 hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
801 hw->hibernation = !!(hwcfg4 & GHWCFG4_HIBER);
802 hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
803 GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
804 hw->acg_enable = !!(hwcfg4 & GHWCFG4_ACG_SUPPORTED);
805 hw->ipg_isoc_en = !!(hwcfg4 & GHWCFG4_IPG_ISOC_SUPPORTED);
806
807 /* fifo sizes */
808 hw->rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
809 GRXFSIZ_DEPTH_SHIFT;
810 /*
811 * Host specific hardware parameters. Reading these parameters
812 * requires the controller to be in host mode. The mode will
813 * be forced, if necessary, to read these values.
814 */
815 dwc2_get_host_hwparams(hsotg);
816 dwc2_get_dev_hwparams(hsotg);
817
818 return 0;
819}
820
821int dwc2_init_params(struct dwc2_hsotg *hsotg)
822{
823 const struct of_device_id *match;
824 void (*set_params)(void *data);
825
826 dwc2_set_default_params(hsotg);
827 dwc2_get_device_properties(hsotg);
828
829 match = of_match_device(dwc2_of_match_table, hsotg->dev);
830 if (match && match->data) {
831 set_params = match->data;
832 set_params(hsotg);
833 }
834
835 dwc2_check_params(hsotg);
836
837 return 0;
838}