15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) 2323230efSJohn Youn /* 3323230efSJohn Youn * Copyright (C) 2004-2016 Synopsys, Inc. 4323230efSJohn Youn * 5323230efSJohn Youn * Redistribution and use in source and binary forms, with or without 6323230efSJohn Youn * modification, are permitted provided that the following conditions 7323230efSJohn Youn * are met: 8323230efSJohn Youn * 1. Redistributions of source code must retain the above copyright 9323230efSJohn Youn * notice, this list of conditions, and the following disclaimer, 10323230efSJohn Youn * without modification. 11323230efSJohn Youn * 2. Redistributions in binary form must reproduce the above copyright 12323230efSJohn Youn * notice, this list of conditions and the following disclaimer in the 13323230efSJohn Youn * documentation and/or other materials provided with the distribution. 14323230efSJohn Youn * 3. The names of the above-listed copyright holders may not be used 15323230efSJohn Youn * to endorse or promote products derived from this software without 16323230efSJohn Youn * specific prior written permission. 17323230efSJohn Youn * 18323230efSJohn Youn * ALTERNATIVELY, this software may be distributed under the terms of the 19323230efSJohn Youn * GNU General Public License ("GPL") as published by the Free Software 20323230efSJohn Youn * Foundation; either version 2 of the License, or (at your option) any 21323230efSJohn Youn * later version. 22323230efSJohn Youn * 23323230efSJohn Youn * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 24323230efSJohn Youn * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 25323230efSJohn Youn * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26323230efSJohn Youn * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 27323230efSJohn Youn * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28323230efSJohn Youn * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29323230efSJohn Youn * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30323230efSJohn Youn * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31323230efSJohn Youn * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32323230efSJohn Youn * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33323230efSJohn Youn * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34323230efSJohn Youn */ 35323230efSJohn Youn 36323230efSJohn Youn #include <linux/kernel.h> 37323230efSJohn Youn #include <linux/module.h> 38323230efSJohn Youn #include <linux/of_device.h> 39323230efSJohn Youn 40323230efSJohn Youn #include "core.h" 41323230efSJohn Youn 427de1debcSJohn Youn static void dwc2_set_bcm_params(struct dwc2_hsotg *hsotg) 437de1debcSJohn Youn { 447de1debcSJohn Youn struct dwc2_core_params *p = &hsotg->params; 45323230efSJohn Youn 467de1debcSJohn Youn p->host_rx_fifo_size = 774; 477de1debcSJohn Youn p->max_transfer_size = 65535; 487de1debcSJohn Youn p->max_packet_count = 511; 497de1debcSJohn Youn p->ahbcfg = 0x10; 507de1debcSJohn Youn p->uframe_sched = false; 517de1debcSJohn Youn } 52323230efSJohn Youn 537de1debcSJohn Youn static void dwc2_set_his_params(struct dwc2_hsotg *hsotg) 547de1debcSJohn Youn { 557de1debcSJohn Youn struct dwc2_core_params *p = &hsotg->params; 56323230efSJohn Youn 577de1debcSJohn Youn p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE; 587de1debcSJohn Youn p->speed = DWC2_SPEED_PARAM_HIGH; 597de1debcSJohn Youn p->host_rx_fifo_size = 512; 607de1debcSJohn Youn p->host_nperio_tx_fifo_size = 512; 617de1debcSJohn Youn p->host_perio_tx_fifo_size = 512; 627de1debcSJohn Youn p->max_transfer_size = 65535; 637de1debcSJohn Youn p->max_packet_count = 511; 647de1debcSJohn Youn p->host_channels = 16; 657de1debcSJohn Youn p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI; 667de1debcSJohn Youn p->phy_utmi_width = 8; 677de1debcSJohn Youn p->i2c_enable = false; 687de1debcSJohn Youn p->reload_ctl = false; 697de1debcSJohn Youn p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << 707de1debcSJohn Youn GAHBCFG_HBSTLEN_SHIFT; 717de1debcSJohn Youn p->uframe_sched = false; 72ca8b0332SChen Yu p->change_speed_quirk = true; 737de1debcSJohn Youn } 74323230efSJohn Youn 757de1debcSJohn Youn static void dwc2_set_rk_params(struct dwc2_hsotg *hsotg) 767de1debcSJohn Youn { 777de1debcSJohn Youn struct dwc2_core_params *p = &hsotg->params; 787de1debcSJohn Youn 797de1debcSJohn Youn p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE; 807de1debcSJohn Youn p->host_rx_fifo_size = 525; 817de1debcSJohn Youn p->host_nperio_tx_fifo_size = 128; 827de1debcSJohn Youn p->host_perio_tx_fifo_size = 256; 837de1debcSJohn Youn p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << 847de1debcSJohn Youn GAHBCFG_HBSTLEN_SHIFT; 857de1debcSJohn Youn } 867de1debcSJohn Youn 877de1debcSJohn Youn static void dwc2_set_ltq_params(struct dwc2_hsotg *hsotg) 887de1debcSJohn Youn { 897de1debcSJohn Youn struct dwc2_core_params *p = &hsotg->params; 907de1debcSJohn Youn 917de1debcSJohn Youn p->otg_cap = 2; 927de1debcSJohn Youn p->host_rx_fifo_size = 288; 937de1debcSJohn Youn p->host_nperio_tx_fifo_size = 128; 947de1debcSJohn Youn p->host_perio_tx_fifo_size = 96; 957de1debcSJohn Youn p->max_transfer_size = 65535; 967de1debcSJohn Youn p->max_packet_count = 511; 977de1debcSJohn Youn p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << 987de1debcSJohn Youn GAHBCFG_HBSTLEN_SHIFT; 997de1debcSJohn Youn } 1007de1debcSJohn Youn 1017de1debcSJohn Youn static void dwc2_set_amlogic_params(struct dwc2_hsotg *hsotg) 1027de1debcSJohn Youn { 1037de1debcSJohn Youn struct dwc2_core_params *p = &hsotg->params; 1047de1debcSJohn Youn 1057de1debcSJohn Youn p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE; 1067de1debcSJohn Youn p->speed = DWC2_SPEED_PARAM_HIGH; 1077de1debcSJohn Youn p->host_rx_fifo_size = 512; 1087de1debcSJohn Youn p->host_nperio_tx_fifo_size = 500; 1097de1debcSJohn Youn p->host_perio_tx_fifo_size = 500; 1107de1debcSJohn Youn p->host_channels = 16; 1117de1debcSJohn Youn p->phy_type = DWC2_PHY_TYPE_PARAM_UTMI; 1127de1debcSJohn Youn p->ahbcfg = GAHBCFG_HBSTLEN_INCR8 << 1137de1debcSJohn Youn GAHBCFG_HBSTLEN_SHIFT; 1147de1debcSJohn Youn p->uframe_sched = false; 1157de1debcSJohn Youn } 1167de1debcSJohn Youn 1177de1debcSJohn Youn static void dwc2_set_amcc_params(struct dwc2_hsotg *hsotg) 1187de1debcSJohn Youn { 1197de1debcSJohn Youn struct dwc2_core_params *p = &hsotg->params; 1207de1debcSJohn Youn 1217de1debcSJohn Youn p->ahbcfg = GAHBCFG_HBSTLEN_INCR16 << GAHBCFG_HBSTLEN_SHIFT; 1227de1debcSJohn Youn } 123323230efSJohn Youn 124e35b1350SBruno Herrera static void dwc2_set_stm32f4x9_fsotg_params(struct dwc2_hsotg *hsotg) 125e35b1350SBruno Herrera { 126e35b1350SBruno Herrera struct dwc2_core_params *p = &hsotg->params; 127e35b1350SBruno Herrera 128e35b1350SBruno Herrera p->otg_cap = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE; 129e35b1350SBruno Herrera p->speed = DWC2_SPEED_PARAM_FULL; 130e35b1350SBruno Herrera p->host_rx_fifo_size = 128; 131e35b1350SBruno Herrera p->host_nperio_tx_fifo_size = 96; 132e35b1350SBruno Herrera p->host_perio_tx_fifo_size = 96; 133e35b1350SBruno Herrera p->max_packet_count = 256; 134e35b1350SBruno Herrera p->phy_type = DWC2_PHY_TYPE_PARAM_FS; 135e35b1350SBruno Herrera p->i2c_enable = false; 136e35b1350SBruno Herrera p->uframe_sched = false; 137e35b1350SBruno Herrera p->activate_stm_fs_transceiver = true; 138e35b1350SBruno Herrera } 139e35b1350SBruno Herrera 1401a149e35SAmelie Delaunay static void dwc2_set_stm32f7_hsotg_params(struct dwc2_hsotg *hsotg) 141d8fae8b9SAmelie Delaunay { 142d8fae8b9SAmelie Delaunay struct dwc2_core_params *p = &hsotg->params; 143d8fae8b9SAmelie Delaunay 144d8fae8b9SAmelie Delaunay p->host_rx_fifo_size = 622; 145d8fae8b9SAmelie Delaunay p->host_nperio_tx_fifo_size = 128; 146d8fae8b9SAmelie Delaunay p->host_perio_tx_fifo_size = 256; 147d8fae8b9SAmelie Delaunay } 148d8fae8b9SAmelie Delaunay 149323230efSJohn Youn const struct of_device_id dwc2_of_match_table[] = { 1507de1debcSJohn Youn { .compatible = "brcm,bcm2835-usb", .data = dwc2_set_bcm_params }, 1517de1debcSJohn Youn { .compatible = "hisilicon,hi6220-usb", .data = dwc2_set_his_params }, 1527de1debcSJohn Youn { .compatible = "rockchip,rk3066-usb", .data = dwc2_set_rk_params }, 1537de1debcSJohn Youn { .compatible = "lantiq,arx100-usb", .data = dwc2_set_ltq_params }, 1547de1debcSJohn Youn { .compatible = "lantiq,xrx200-usb", .data = dwc2_set_ltq_params }, 1557de1debcSJohn Youn { .compatible = "snps,dwc2" }, 1567de1debcSJohn Youn { .compatible = "samsung,s3c6400-hsotg" }, 15755b644fdSMartin Blumenstingl { .compatible = "amlogic,meson8-usb", 15855b644fdSMartin Blumenstingl .data = dwc2_set_amlogic_params }, 1597de1debcSJohn Youn { .compatible = "amlogic,meson8b-usb", 1607de1debcSJohn Youn .data = dwc2_set_amlogic_params }, 1617de1debcSJohn Youn { .compatible = "amlogic,meson-gxbb-usb", 1627de1debcSJohn Youn .data = dwc2_set_amlogic_params }, 1637de1debcSJohn Youn { .compatible = "amcc,dwc-otg", .data = dwc2_set_amcc_params }, 164e35b1350SBruno Herrera { .compatible = "st,stm32f4x9-fsotg", 165e35b1350SBruno Herrera .data = dwc2_set_stm32f4x9_fsotg_params }, 166e35b1350SBruno Herrera { .compatible = "st,stm32f4x9-hsotg" }, 1671a149e35SAmelie Delaunay { .compatible = "st,stm32f7-hsotg", 1681a149e35SAmelie Delaunay .data = dwc2_set_stm32f7_hsotg_params }, 169323230efSJohn Youn {}, 170323230efSJohn Youn }; 171323230efSJohn Youn MODULE_DEVICE_TABLE(of, dwc2_of_match_table); 172323230efSJohn Youn 173245977c9SJohn Youn static void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg) 17405ee799fSJohn Youn { 175245977c9SJohn Youn u8 val; 17605ee799fSJohn Youn 177323230efSJohn Youn switch (hsotg->hw_params.op_mode) { 178323230efSJohn Youn case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE: 179323230efSJohn Youn val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE; 180323230efSJohn Youn break; 181323230efSJohn Youn case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE: 182323230efSJohn Youn case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE: 183323230efSJohn Youn case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST: 184323230efSJohn Youn val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE; 185323230efSJohn Youn break; 186323230efSJohn Youn default: 187323230efSJohn Youn val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE; 188323230efSJohn Youn break; 189323230efSJohn Youn } 190323230efSJohn Youn 191bea8e86cSJohn Youn hsotg->params.otg_cap = val; 192323230efSJohn Youn } 193323230efSJohn Youn 194245977c9SJohn Youn static void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg) 195323230efSJohn Youn { 196245977c9SJohn Youn int val; 197245977c9SJohn Youn u32 hs_phy_type = hsotg->hw_params.hs_phy_type; 198323230efSJohn Youn 199323230efSJohn Youn val = DWC2_PHY_TYPE_PARAM_FS; 200323230efSJohn Youn if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) { 201323230efSJohn Youn if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI || 202323230efSJohn Youn hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI) 203323230efSJohn Youn val = DWC2_PHY_TYPE_PARAM_UTMI; 204323230efSJohn Youn else 205323230efSJohn Youn val = DWC2_PHY_TYPE_PARAM_ULPI; 206323230efSJohn Youn } 207245977c9SJohn Youn 208245977c9SJohn Youn if (dwc2_is_fs_iot(hsotg)) 209245977c9SJohn Youn hsotg->params.phy_type = DWC2_PHY_TYPE_PARAM_FS; 210323230efSJohn Youn 211bea8e86cSJohn Youn hsotg->params.phy_type = val; 212323230efSJohn Youn } 213323230efSJohn Youn 214245977c9SJohn Youn static void dwc2_set_param_speed(struct dwc2_hsotg *hsotg) 215323230efSJohn Youn { 216245977c9SJohn Youn int val; 217323230efSJohn Youn 218245977c9SJohn Youn val = hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS ? 219323230efSJohn Youn DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH; 220245977c9SJohn Youn 221245977c9SJohn Youn if (dwc2_is_fs_iot(hsotg)) 222245977c9SJohn Youn val = DWC2_SPEED_PARAM_FULL; 223245977c9SJohn Youn 224245977c9SJohn Youn if (dwc2_is_hs_iot(hsotg)) 225245977c9SJohn Youn val = DWC2_SPEED_PARAM_HIGH; 226323230efSJohn Youn 227bea8e86cSJohn Youn hsotg->params.speed = val; 228323230efSJohn Youn } 229323230efSJohn Youn 230245977c9SJohn Youn static void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg) 231323230efSJohn Youn { 232245977c9SJohn Youn int val; 233323230efSJohn Youn 234323230efSJohn Youn val = (hsotg->hw_params.utmi_phy_data_width == 235323230efSJohn Youn GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16; 236323230efSJohn Youn 237bea8e86cSJohn Youn hsotg->params.phy_utmi_width = val; 238323230efSJohn Youn } 239323230efSJohn Youn 24005ee799fSJohn Youn static void dwc2_set_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg) 24105ee799fSJohn Youn { 24205ee799fSJohn Youn struct dwc2_core_params *p = &hsotg->params; 243c138ecfaSSevak Arakelyan int depth_average; 244c138ecfaSSevak Arakelyan int fifo_count; 245c138ecfaSSevak Arakelyan int i; 246c138ecfaSSevak Arakelyan 247c138ecfaSSevak Arakelyan fifo_count = dwc2_hsotg_tx_fifo_count(hsotg); 24805ee799fSJohn Youn 249245977c9SJohn Youn memset(p->g_tx_fifo_size, 0, sizeof(p->g_tx_fifo_size)); 250c138ecfaSSevak Arakelyan depth_average = dwc2_hsotg_tx_fifo_average_depth(hsotg); 251c138ecfaSSevak Arakelyan for (i = 1; i <= fifo_count; i++) 252c138ecfaSSevak Arakelyan p->g_tx_fifo_size[i] = depth_average; 2539962b62fSJohn Youn } 2549962b62fSJohn Youn 25503ea6d6eSJohn Youn static void dwc2_set_param_power_down(struct dwc2_hsotg *hsotg) 25603ea6d6eSJohn Youn { 25703ea6d6eSJohn Youn int val; 25803ea6d6eSJohn Youn 25903ea6d6eSJohn Youn if (hsotg->hw_params.hibernation) 26003ea6d6eSJohn Youn val = 2; 26103ea6d6eSJohn Youn else if (hsotg->hw_params.power_optimized) 26203ea6d6eSJohn Youn val = 1; 26303ea6d6eSJohn Youn else 26403ea6d6eSJohn Youn val = 0; 26503ea6d6eSJohn Youn 26603ea6d6eSJohn Youn hsotg->params.power_down = val; 26703ea6d6eSJohn Youn } 26803ea6d6eSJohn Youn 26905ee799fSJohn Youn /** 270245977c9SJohn Youn * dwc2_set_default_params() - Set all core parameters to their 271245977c9SJohn Youn * auto-detected default values. 272*6fb914d7SGrigor Tovmasyan * 273*6fb914d7SGrigor Tovmasyan * @hsotg: Programming view of the DWC_otg controller 274*6fb914d7SGrigor Tovmasyan * 275323230efSJohn Youn */ 276245977c9SJohn Youn static void dwc2_set_default_params(struct dwc2_hsotg *hsotg) 277323230efSJohn Youn { 27805ee799fSJohn Youn struct dwc2_hw_params *hw = &hsotg->hw_params; 27905ee799fSJohn Youn struct dwc2_core_params *p = &hsotg->params; 2806b66ce51SJohn Youn bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH); 281323230efSJohn Youn 282245977c9SJohn Youn dwc2_set_param_otg_cap(hsotg); 283245977c9SJohn Youn dwc2_set_param_phy_type(hsotg); 284245977c9SJohn Youn dwc2_set_param_speed(hsotg); 285245977c9SJohn Youn dwc2_set_param_phy_utmi_width(hsotg); 28603ea6d6eSJohn Youn dwc2_set_param_power_down(hsotg); 287245977c9SJohn Youn p->phy_ulpi_ddr = false; 288245977c9SJohn Youn p->phy_ulpi_ext_vbus = false; 289245977c9SJohn Youn 290245977c9SJohn Youn p->enable_dynamic_fifo = hw->enable_dynamic_fifo; 291245977c9SJohn Youn p->en_multiple_tx_fifo = hw->en_multiple_tx_fifo; 292245977c9SJohn Youn p->i2c_enable = hw->i2c_enable; 29366e77a24SRazmik Karapetyan p->acg_enable = hw->acg_enable; 294245977c9SJohn Youn p->ulpi_fs_ls = false; 295245977c9SJohn Youn p->ts_dline = false; 296245977c9SJohn Youn p->reload_ctl = (hw->snpsid >= DWC2_CORE_REV_2_92a); 297245977c9SJohn Youn p->uframe_sched = true; 298245977c9SJohn Youn p->external_id_pin_ctl = false; 2996f80b6deSSevak Arakelyan p->lpm = true; 3006f80b6deSSevak Arakelyan p->lpm_clock_gating = true; 3016f80b6deSSevak Arakelyan p->besl = true; 3026f80b6deSSevak Arakelyan p->hird_threshold_en = true; 3036f80b6deSSevak Arakelyan p->hird_threshold = 4; 304b43ebc96SGrigor Tovmasyan p->ipg_isoc_en = false; 305245977c9SJohn Youn p->max_packet_count = hw->max_packet_count; 306245977c9SJohn Youn p->max_transfer_size = hw->max_transfer_size; 3071b52d2faSRazmik Karapetyan p->ahbcfg = GAHBCFG_HBSTLEN_INCR << GAHBCFG_HBSTLEN_SHIFT; 308245977c9SJohn Youn 3096b66ce51SJohn Youn if ((hsotg->dr_mode == USB_DR_MODE_HOST) || 3106b66ce51SJohn Youn (hsotg->dr_mode == USB_DR_MODE_OTG)) { 311245977c9SJohn Youn p->host_dma = dma_capable; 312245977c9SJohn Youn p->dma_desc_enable = false; 313245977c9SJohn Youn p->dma_desc_fs_enable = false; 314245977c9SJohn Youn p->host_support_fs_ls_low_power = false; 315245977c9SJohn Youn p->host_ls_low_power_phy_clk = false; 316245977c9SJohn Youn p->host_channels = hw->host_channels; 317245977c9SJohn Youn p->host_rx_fifo_size = hw->rx_fifo_size; 318245977c9SJohn Youn p->host_nperio_tx_fifo_size = hw->host_nperio_tx_fifo_size; 319245977c9SJohn Youn p->host_perio_tx_fifo_size = hw->host_perio_tx_fifo_size; 3206b66ce51SJohn Youn } 3216b66ce51SJohn Youn 32205ee799fSJohn Youn if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) || 32305ee799fSJohn Youn (hsotg->dr_mode == USB_DR_MODE_OTG)) { 324245977c9SJohn Youn p->g_dma = dma_capable; 325245977c9SJohn Youn p->g_dma_desc = hw->dma_desc_enable; 32605ee799fSJohn Youn 32705ee799fSJohn Youn /* 32805ee799fSJohn Youn * The values for g_rx_fifo_size (2048) and 32905ee799fSJohn Youn * g_np_tx_fifo_size (1024) come from the legacy s3c 33005ee799fSJohn Youn * gadget driver. These defaults have been hard-coded 33105ee799fSJohn Youn * for some time so many platforms depend on these 33205ee799fSJohn Youn * values. Leave them as defaults for now and only 33305ee799fSJohn Youn * auto-detect if the hardware does not support the 33405ee799fSJohn Youn * default. 33505ee799fSJohn Youn */ 336245977c9SJohn Youn p->g_rx_fifo_size = 2048; 337245977c9SJohn Youn p->g_np_tx_fifo_size = 1024; 33805ee799fSJohn Youn dwc2_set_param_tx_fifo_sizes(hsotg); 33905ee799fSJohn Youn } 340323230efSJohn Youn } 341323230efSJohn Youn 342f9f93cbbSJohn Youn /** 343f9f93cbbSJohn Youn * dwc2_get_device_properties() - Read in device properties. 344f9f93cbbSJohn Youn * 345*6fb914d7SGrigor Tovmasyan * @hsotg: Programming view of the DWC_otg controller 346*6fb914d7SGrigor Tovmasyan * 347f9f93cbbSJohn Youn * Read in the device properties and adjust core parameters if needed. 348f9f93cbbSJohn Youn */ 349f9f93cbbSJohn Youn static void dwc2_get_device_properties(struct dwc2_hsotg *hsotg) 350f9f93cbbSJohn Youn { 351f9f93cbbSJohn Youn struct dwc2_core_params *p = &hsotg->params; 352f9f93cbbSJohn Youn int num; 353f9f93cbbSJohn Youn 354f9f93cbbSJohn Youn if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) || 355f9f93cbbSJohn Youn (hsotg->dr_mode == USB_DR_MODE_OTG)) { 356f9f93cbbSJohn Youn device_property_read_u32(hsotg->dev, "g-rx-fifo-size", 357f9f93cbbSJohn Youn &p->g_rx_fifo_size); 358f9f93cbbSJohn Youn 359f9f93cbbSJohn Youn device_property_read_u32(hsotg->dev, "g-np-tx-fifo-size", 360f9f93cbbSJohn Youn &p->g_np_tx_fifo_size); 361f9f93cbbSJohn Youn 362f9f93cbbSJohn Youn num = device_property_read_u32_array(hsotg->dev, 363f9f93cbbSJohn Youn "g-tx-fifo-size", 364f9f93cbbSJohn Youn NULL, 0); 365f9f93cbbSJohn Youn 366f9f93cbbSJohn Youn if (num > 0) { 367f9f93cbbSJohn Youn num = min(num, 15); 368f9f93cbbSJohn Youn memset(p->g_tx_fifo_size, 0, 369f9f93cbbSJohn Youn sizeof(p->g_tx_fifo_size)); 370f9f93cbbSJohn Youn device_property_read_u32_array(hsotg->dev, 371f9f93cbbSJohn Youn "g-tx-fifo-size", 372f9f93cbbSJohn Youn &p->g_tx_fifo_size[1], 373f9f93cbbSJohn Youn num); 374f9f93cbbSJohn Youn } 375f9f93cbbSJohn Youn } 376b11633c4SDinh Nguyen 377b11633c4SDinh Nguyen if (of_find_property(hsotg->dev->of_node, "disable-over-current", NULL)) 378b11633c4SDinh Nguyen p->oc_disable = true; 379f9f93cbbSJohn Youn } 380f9f93cbbSJohn Youn 381d936e666SJohn Youn static void dwc2_check_param_otg_cap(struct dwc2_hsotg *hsotg) 382d936e666SJohn Youn { 383d936e666SJohn Youn int valid = 1; 384d936e666SJohn Youn 385d936e666SJohn Youn switch (hsotg->params.otg_cap) { 386d936e666SJohn Youn case DWC2_CAP_PARAM_HNP_SRP_CAPABLE: 387d936e666SJohn Youn if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE) 388d936e666SJohn Youn valid = 0; 389d936e666SJohn Youn break; 390d936e666SJohn Youn case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE: 391d936e666SJohn Youn switch (hsotg->hw_params.op_mode) { 392d936e666SJohn Youn case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE: 393d936e666SJohn Youn case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE: 394d936e666SJohn Youn case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE: 395d936e666SJohn Youn case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST: 396d936e666SJohn Youn break; 397d936e666SJohn Youn default: 398d936e666SJohn Youn valid = 0; 399d936e666SJohn Youn break; 400d936e666SJohn Youn } 401d936e666SJohn Youn break; 402d936e666SJohn Youn case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE: 403d936e666SJohn Youn /* always valid */ 404d936e666SJohn Youn break; 405d936e666SJohn Youn default: 406d936e666SJohn Youn valid = 0; 407d936e666SJohn Youn break; 408d936e666SJohn Youn } 409d936e666SJohn Youn 410d936e666SJohn Youn if (!valid) 411d936e666SJohn Youn dwc2_set_param_otg_cap(hsotg); 412d936e666SJohn Youn } 413d936e666SJohn Youn 414d936e666SJohn Youn static void dwc2_check_param_phy_type(struct dwc2_hsotg *hsotg) 415d936e666SJohn Youn { 416d936e666SJohn Youn int valid = 0; 417d936e666SJohn Youn u32 hs_phy_type; 418d936e666SJohn Youn u32 fs_phy_type; 419d936e666SJohn Youn 420d936e666SJohn Youn hs_phy_type = hsotg->hw_params.hs_phy_type; 421d936e666SJohn Youn fs_phy_type = hsotg->hw_params.fs_phy_type; 422d936e666SJohn Youn 423d936e666SJohn Youn switch (hsotg->params.phy_type) { 424d936e666SJohn Youn case DWC2_PHY_TYPE_PARAM_FS: 425d936e666SJohn Youn if (fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) 426d936e666SJohn Youn valid = 1; 427d936e666SJohn Youn break; 428d936e666SJohn Youn case DWC2_PHY_TYPE_PARAM_UTMI: 429d936e666SJohn Youn if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) || 430d936e666SJohn Youn (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)) 431d936e666SJohn Youn valid = 1; 432d936e666SJohn Youn break; 433d936e666SJohn Youn case DWC2_PHY_TYPE_PARAM_ULPI: 434d936e666SJohn Youn if ((hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI) || 435d936e666SJohn Youn (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)) 436d936e666SJohn Youn valid = 1; 437d936e666SJohn Youn break; 438d936e666SJohn Youn default: 439d936e666SJohn Youn break; 440d936e666SJohn Youn } 441d936e666SJohn Youn 442d936e666SJohn Youn if (!valid) 443d936e666SJohn Youn dwc2_set_param_phy_type(hsotg); 444d936e666SJohn Youn } 445d936e666SJohn Youn 446d936e666SJohn Youn static void dwc2_check_param_speed(struct dwc2_hsotg *hsotg) 447d936e666SJohn Youn { 448d936e666SJohn Youn int valid = 1; 449d936e666SJohn Youn int phy_type = hsotg->params.phy_type; 450d936e666SJohn Youn int speed = hsotg->params.speed; 451d936e666SJohn Youn 452d936e666SJohn Youn switch (speed) { 453d936e666SJohn Youn case DWC2_SPEED_PARAM_HIGH: 454d936e666SJohn Youn if ((hsotg->params.speed == DWC2_SPEED_PARAM_HIGH) && 455d936e666SJohn Youn (phy_type == DWC2_PHY_TYPE_PARAM_FS)) 456d936e666SJohn Youn valid = 0; 457d936e666SJohn Youn break; 458d936e666SJohn Youn case DWC2_SPEED_PARAM_FULL: 459d936e666SJohn Youn case DWC2_SPEED_PARAM_LOW: 460d936e666SJohn Youn break; 461d936e666SJohn Youn default: 462d936e666SJohn Youn valid = 0; 463d936e666SJohn Youn break; 464d936e666SJohn Youn } 465d936e666SJohn Youn 466d936e666SJohn Youn if (!valid) 467d936e666SJohn Youn dwc2_set_param_speed(hsotg); 468d936e666SJohn Youn } 469d936e666SJohn Youn 470d936e666SJohn Youn static void dwc2_check_param_phy_utmi_width(struct dwc2_hsotg *hsotg) 471d936e666SJohn Youn { 472d936e666SJohn Youn int valid = 0; 473d936e666SJohn Youn int param = hsotg->params.phy_utmi_width; 474d936e666SJohn Youn int width = hsotg->hw_params.utmi_phy_data_width; 475d936e666SJohn Youn 476d936e666SJohn Youn switch (width) { 477d936e666SJohn Youn case GHWCFG4_UTMI_PHY_DATA_WIDTH_8: 478d936e666SJohn Youn valid = (param == 8); 479d936e666SJohn Youn break; 480d936e666SJohn Youn case GHWCFG4_UTMI_PHY_DATA_WIDTH_16: 481d936e666SJohn Youn valid = (param == 16); 482d936e666SJohn Youn break; 483d936e666SJohn Youn case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16: 484d936e666SJohn Youn valid = (param == 8 || param == 16); 485d936e666SJohn Youn break; 486d936e666SJohn Youn } 487d936e666SJohn Youn 488d936e666SJohn Youn if (!valid) 489d936e666SJohn Youn dwc2_set_param_phy_utmi_width(hsotg); 490d936e666SJohn Youn } 491d936e666SJohn Youn 492631a2310SVardan Mikayelyan static void dwc2_check_param_power_down(struct dwc2_hsotg *hsotg) 493631a2310SVardan Mikayelyan { 494631a2310SVardan Mikayelyan int param = hsotg->params.power_down; 495631a2310SVardan Mikayelyan 496631a2310SVardan Mikayelyan switch (param) { 497631a2310SVardan Mikayelyan case DWC2_POWER_DOWN_PARAM_NONE: 498631a2310SVardan Mikayelyan break; 499631a2310SVardan Mikayelyan case DWC2_POWER_DOWN_PARAM_PARTIAL: 500631a2310SVardan Mikayelyan if (hsotg->hw_params.power_optimized) 501631a2310SVardan Mikayelyan break; 502631a2310SVardan Mikayelyan dev_dbg(hsotg->dev, 503631a2310SVardan Mikayelyan "Partial power down isn't supported by HW\n"); 504631a2310SVardan Mikayelyan param = DWC2_POWER_DOWN_PARAM_NONE; 505631a2310SVardan Mikayelyan break; 506631a2310SVardan Mikayelyan case DWC2_POWER_DOWN_PARAM_HIBERNATION: 507631a2310SVardan Mikayelyan if (hsotg->hw_params.hibernation) 508631a2310SVardan Mikayelyan break; 509631a2310SVardan Mikayelyan dev_dbg(hsotg->dev, 510631a2310SVardan Mikayelyan "Hibernation isn't supported by HW\n"); 511631a2310SVardan Mikayelyan param = DWC2_POWER_DOWN_PARAM_NONE; 512631a2310SVardan Mikayelyan break; 513631a2310SVardan Mikayelyan default: 514631a2310SVardan Mikayelyan dev_err(hsotg->dev, 515631a2310SVardan Mikayelyan "%s: Invalid parameter power_down=%d\n", 516631a2310SVardan Mikayelyan __func__, param); 517631a2310SVardan Mikayelyan param = DWC2_POWER_DOWN_PARAM_NONE; 518631a2310SVardan Mikayelyan break; 519631a2310SVardan Mikayelyan } 520631a2310SVardan Mikayelyan 521631a2310SVardan Mikayelyan hsotg->params.power_down = param; 522631a2310SVardan Mikayelyan } 523631a2310SVardan Mikayelyan 5243c6aea73SSevak Arakelyan static void dwc2_check_param_tx_fifo_sizes(struct dwc2_hsotg *hsotg) 5253c6aea73SSevak Arakelyan { 5263c6aea73SSevak Arakelyan int fifo_count; 5273c6aea73SSevak Arakelyan int fifo; 5283c6aea73SSevak Arakelyan int min; 5293c6aea73SSevak Arakelyan u32 total = 0; 5303c6aea73SSevak Arakelyan u32 dptxfszn; 5313c6aea73SSevak Arakelyan 5323c6aea73SSevak Arakelyan fifo_count = dwc2_hsotg_tx_fifo_count(hsotg); 5333c6aea73SSevak Arakelyan min = hsotg->hw_params.en_multiple_tx_fifo ? 16 : 4; 5343c6aea73SSevak Arakelyan 5353c6aea73SSevak Arakelyan for (fifo = 1; fifo <= fifo_count; fifo++) 5363c6aea73SSevak Arakelyan total += hsotg->params.g_tx_fifo_size[fifo]; 5373c6aea73SSevak Arakelyan 5383c6aea73SSevak Arakelyan if (total > dwc2_hsotg_tx_fifo_total_depth(hsotg) || !total) { 5393c6aea73SSevak Arakelyan dev_warn(hsotg->dev, "%s: Invalid parameter g-tx-fifo-size, setting to default average\n", 5403c6aea73SSevak Arakelyan __func__); 5413c6aea73SSevak Arakelyan dwc2_set_param_tx_fifo_sizes(hsotg); 5423c6aea73SSevak Arakelyan } 5433c6aea73SSevak Arakelyan 5443c6aea73SSevak Arakelyan for (fifo = 1; fifo <= fifo_count; fifo++) { 5459273083aSMinas Harutyunyan dptxfszn = hsotg->hw_params.g_tx_fifo_size[fifo]; 5463c6aea73SSevak Arakelyan 5473c6aea73SSevak Arakelyan if (hsotg->params.g_tx_fifo_size[fifo] < min || 5483c6aea73SSevak Arakelyan hsotg->params.g_tx_fifo_size[fifo] > dptxfszn) { 5493c6aea73SSevak Arakelyan dev_warn(hsotg->dev, "%s: Invalid parameter g_tx_fifo_size[%d]=%d\n", 5503c6aea73SSevak Arakelyan __func__, fifo, 5513c6aea73SSevak Arakelyan hsotg->params.g_tx_fifo_size[fifo]); 5523c6aea73SSevak Arakelyan hsotg->params.g_tx_fifo_size[fifo] = dptxfszn; 5533c6aea73SSevak Arakelyan } 5543c6aea73SSevak Arakelyan } 5553c6aea73SSevak Arakelyan } 5563c6aea73SSevak Arakelyan 557d936e666SJohn Youn #define CHECK_RANGE(_param, _min, _max, _def) do { \ 558d936e666SJohn Youn if ((hsotg->params._param) < (_min) || \ 559d936e666SJohn Youn (hsotg->params._param) > (_max)) { \ 560d936e666SJohn Youn dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \ 561d936e666SJohn Youn __func__, #_param, hsotg->params._param); \ 562d936e666SJohn Youn hsotg->params._param = (_def); \ 563d936e666SJohn Youn } \ 564d936e666SJohn Youn } while (0) 565d936e666SJohn Youn 566d936e666SJohn Youn #define CHECK_BOOL(_param, _check) do { \ 567d936e666SJohn Youn if (hsotg->params._param && !(_check)) { \ 568d936e666SJohn Youn dev_warn(hsotg->dev, "%s: Invalid parameter %s=%d\n", \ 569d936e666SJohn Youn __func__, #_param, hsotg->params._param); \ 570d936e666SJohn Youn hsotg->params._param = false; \ 571d936e666SJohn Youn } \ 572d936e666SJohn Youn } while (0) 573d936e666SJohn Youn 574d936e666SJohn Youn static void dwc2_check_params(struct dwc2_hsotg *hsotg) 575d936e666SJohn Youn { 576d936e666SJohn Youn struct dwc2_hw_params *hw = &hsotg->hw_params; 577d936e666SJohn Youn struct dwc2_core_params *p = &hsotg->params; 578d936e666SJohn Youn bool dma_capable = !(hw->arch == GHWCFG2_SLAVE_ONLY_ARCH); 579d936e666SJohn Youn 580d936e666SJohn Youn dwc2_check_param_otg_cap(hsotg); 581d936e666SJohn Youn dwc2_check_param_phy_type(hsotg); 582d936e666SJohn Youn dwc2_check_param_speed(hsotg); 583d936e666SJohn Youn dwc2_check_param_phy_utmi_width(hsotg); 584631a2310SVardan Mikayelyan dwc2_check_param_power_down(hsotg); 585d936e666SJohn Youn CHECK_BOOL(enable_dynamic_fifo, hw->enable_dynamic_fifo); 586d936e666SJohn Youn CHECK_BOOL(en_multiple_tx_fifo, hw->en_multiple_tx_fifo); 587d936e666SJohn Youn CHECK_BOOL(i2c_enable, hw->i2c_enable); 588b43ebc96SGrigor Tovmasyan CHECK_BOOL(ipg_isoc_en, hw->ipg_isoc_en); 58966e77a24SRazmik Karapetyan CHECK_BOOL(acg_enable, hw->acg_enable); 590d936e666SJohn Youn CHECK_BOOL(reload_ctl, (hsotg->hw_params.snpsid > DWC2_CORE_REV_2_92a)); 5916f80b6deSSevak Arakelyan CHECK_BOOL(lpm, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_80a)); 5926f80b6deSSevak Arakelyan CHECK_BOOL(lpm, hw->lpm_mode); 5936f80b6deSSevak Arakelyan CHECK_BOOL(lpm_clock_gating, hsotg->params.lpm); 5946f80b6deSSevak Arakelyan CHECK_BOOL(besl, hsotg->params.lpm); 5956f80b6deSSevak Arakelyan CHECK_BOOL(besl, (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a)); 5966f80b6deSSevak Arakelyan CHECK_BOOL(hird_threshold_en, hsotg->params.lpm); 5976f80b6deSSevak Arakelyan CHECK_RANGE(hird_threshold, 0, hsotg->params.besl ? 12 : 7, 0); 598d936e666SJohn Youn CHECK_RANGE(max_packet_count, 599d936e666SJohn Youn 15, hw->max_packet_count, 600d936e666SJohn Youn hw->max_packet_count); 601d936e666SJohn Youn CHECK_RANGE(max_transfer_size, 602d936e666SJohn Youn 2047, hw->max_transfer_size, 603d936e666SJohn Youn hw->max_transfer_size); 604d936e666SJohn Youn 605d936e666SJohn Youn if ((hsotg->dr_mode == USB_DR_MODE_HOST) || 606d936e666SJohn Youn (hsotg->dr_mode == USB_DR_MODE_OTG)) { 607d936e666SJohn Youn CHECK_BOOL(host_dma, dma_capable); 608d936e666SJohn Youn CHECK_BOOL(dma_desc_enable, p->host_dma); 609d936e666SJohn Youn CHECK_BOOL(dma_desc_fs_enable, p->dma_desc_enable); 610d936e666SJohn Youn CHECK_BOOL(host_ls_low_power_phy_clk, 611d936e666SJohn Youn p->phy_type == DWC2_PHY_TYPE_PARAM_FS); 612d936e666SJohn Youn CHECK_RANGE(host_channels, 613d936e666SJohn Youn 1, hw->host_channels, 614d936e666SJohn Youn hw->host_channels); 615d936e666SJohn Youn CHECK_RANGE(host_rx_fifo_size, 616d936e666SJohn Youn 16, hw->rx_fifo_size, 617d936e666SJohn Youn hw->rx_fifo_size); 618d936e666SJohn Youn CHECK_RANGE(host_nperio_tx_fifo_size, 619d936e666SJohn Youn 16, hw->host_nperio_tx_fifo_size, 620d936e666SJohn Youn hw->host_nperio_tx_fifo_size); 621d936e666SJohn Youn CHECK_RANGE(host_perio_tx_fifo_size, 622d936e666SJohn Youn 16, hw->host_perio_tx_fifo_size, 623d936e666SJohn Youn hw->host_perio_tx_fifo_size); 624d936e666SJohn Youn } 625d936e666SJohn Youn 626d936e666SJohn Youn if ((hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) || 627d936e666SJohn Youn (hsotg->dr_mode == USB_DR_MODE_OTG)) { 628d936e666SJohn Youn CHECK_BOOL(g_dma, dma_capable); 629d936e666SJohn Youn CHECK_BOOL(g_dma_desc, (p->g_dma && hw->dma_desc_enable)); 630d936e666SJohn Youn CHECK_RANGE(g_rx_fifo_size, 631d936e666SJohn Youn 16, hw->rx_fifo_size, 632d936e666SJohn Youn hw->rx_fifo_size); 633d936e666SJohn Youn CHECK_RANGE(g_np_tx_fifo_size, 634d936e666SJohn Youn 16, hw->dev_nperio_tx_fifo_size, 635d936e666SJohn Youn hw->dev_nperio_tx_fifo_size); 6363c6aea73SSevak Arakelyan dwc2_check_param_tx_fifo_sizes(hsotg); 637d936e666SJohn Youn } 638d936e666SJohn Youn } 639d936e666SJohn Youn 640323230efSJohn Youn /* 641323230efSJohn Youn * Gets host hardware parameters. Forces host mode if not currently in 642323230efSJohn Youn * host mode. Should be called immediately after a core soft reset in 643323230efSJohn Youn * order to get the reset values. 644323230efSJohn Youn */ 645323230efSJohn Youn static void dwc2_get_host_hwparams(struct dwc2_hsotg *hsotg) 646323230efSJohn Youn { 647323230efSJohn Youn struct dwc2_hw_params *hw = &hsotg->hw_params; 648323230efSJohn Youn u32 gnptxfsiz; 649323230efSJohn Youn u32 hptxfsiz; 650323230efSJohn Youn 651323230efSJohn Youn if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) 652323230efSJohn Youn return; 653323230efSJohn Youn 65413b1f8e2SVardan Mikayelyan dwc2_force_mode(hsotg, true); 655323230efSJohn Youn 656323230efSJohn Youn gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ); 657323230efSJohn Youn hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ); 658323230efSJohn Youn 659323230efSJohn Youn hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >> 660323230efSJohn Youn FIFOSIZE_DEPTH_SHIFT; 661323230efSJohn Youn hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >> 662323230efSJohn Youn FIFOSIZE_DEPTH_SHIFT; 663323230efSJohn Youn } 664323230efSJohn Youn 665323230efSJohn Youn /* 666323230efSJohn Youn * Gets device hardware parameters. Forces device mode if not 667323230efSJohn Youn * currently in device mode. Should be called immediately after a core 668323230efSJohn Youn * soft reset in order to get the reset values. 669323230efSJohn Youn */ 670323230efSJohn Youn static void dwc2_get_dev_hwparams(struct dwc2_hsotg *hsotg) 671323230efSJohn Youn { 672323230efSJohn Youn struct dwc2_hw_params *hw = &hsotg->hw_params; 673323230efSJohn Youn u32 gnptxfsiz; 6749273083aSMinas Harutyunyan int fifo, fifo_count; 675323230efSJohn Youn 676323230efSJohn Youn if (hsotg->dr_mode == USB_DR_MODE_HOST) 677323230efSJohn Youn return; 678323230efSJohn Youn 67913b1f8e2SVardan Mikayelyan dwc2_force_mode(hsotg, false); 680323230efSJohn Youn 681323230efSJohn Youn gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ); 682323230efSJohn Youn 6839273083aSMinas Harutyunyan fifo_count = dwc2_hsotg_tx_fifo_count(hsotg); 6849273083aSMinas Harutyunyan 6859273083aSMinas Harutyunyan for (fifo = 1; fifo <= fifo_count; fifo++) { 6869273083aSMinas Harutyunyan hw->g_tx_fifo_size[fifo] = 6879273083aSMinas Harutyunyan (dwc2_readl(hsotg->regs + DPTXFSIZN(fifo)) & 6889273083aSMinas Harutyunyan FIFOSIZE_DEPTH_MASK) >> FIFOSIZE_DEPTH_SHIFT; 6899273083aSMinas Harutyunyan } 6909273083aSMinas Harutyunyan 691323230efSJohn Youn hw->dev_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >> 692323230efSJohn Youn FIFOSIZE_DEPTH_SHIFT; 693323230efSJohn Youn } 694323230efSJohn Youn 695323230efSJohn Youn /** 696323230efSJohn Youn * During device initialization, read various hardware configuration 697323230efSJohn Youn * registers and interpret the contents. 698*6fb914d7SGrigor Tovmasyan * 699*6fb914d7SGrigor Tovmasyan * @hsotg: Programming view of the DWC_otg controller 700*6fb914d7SGrigor Tovmasyan * 701323230efSJohn Youn */ 702323230efSJohn Youn int dwc2_get_hwparams(struct dwc2_hsotg *hsotg) 703323230efSJohn Youn { 704323230efSJohn Youn struct dwc2_hw_params *hw = &hsotg->hw_params; 705323230efSJohn Youn unsigned int width; 706323230efSJohn Youn u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4; 707323230efSJohn Youn u32 grxfsiz; 708323230efSJohn Youn 709323230efSJohn Youn /* 710323230efSJohn Youn * Attempt to ensure this device is really a DWC_otg Controller. 711323230efSJohn Youn * Read and verify the GSNPSID register contents. The value should be 712d14ccabaSGevorg Sahakyan * 0x45f4xxxx, 0x5531xxxx or 0x5532xxxx 713323230efSJohn Youn */ 714d14ccabaSGevorg Sahakyan 715323230efSJohn Youn hw->snpsid = dwc2_readl(hsotg->regs + GSNPSID); 716d14ccabaSGevorg Sahakyan if ((hw->snpsid & GSNPSID_ID_MASK) != DWC2_OTG_ID && 717d14ccabaSGevorg Sahakyan (hw->snpsid & GSNPSID_ID_MASK) != DWC2_FS_IOT_ID && 718d14ccabaSGevorg Sahakyan (hw->snpsid & GSNPSID_ID_MASK) != DWC2_HS_IOT_ID) { 719323230efSJohn Youn dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n", 720323230efSJohn Youn hw->snpsid); 721323230efSJohn Youn return -ENODEV; 722323230efSJohn Youn } 723323230efSJohn Youn 724323230efSJohn Youn dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n", 725323230efSJohn Youn hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf, 726323230efSJohn Youn hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid); 727323230efSJohn Youn 728323230efSJohn Youn hwcfg1 = dwc2_readl(hsotg->regs + GHWCFG1); 729323230efSJohn Youn hwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2); 730323230efSJohn Youn hwcfg3 = dwc2_readl(hsotg->regs + GHWCFG3); 731323230efSJohn Youn hwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4); 732323230efSJohn Youn grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ); 733323230efSJohn Youn 734323230efSJohn Youn /* hwcfg1 */ 735323230efSJohn Youn hw->dev_ep_dirs = hwcfg1; 736323230efSJohn Youn 737323230efSJohn Youn /* hwcfg2 */ 738323230efSJohn Youn hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >> 739323230efSJohn Youn GHWCFG2_OP_MODE_SHIFT; 740323230efSJohn Youn hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >> 741323230efSJohn Youn GHWCFG2_ARCHITECTURE_SHIFT; 742323230efSJohn Youn hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO); 743323230efSJohn Youn hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >> 744323230efSJohn Youn GHWCFG2_NUM_HOST_CHAN_SHIFT); 745323230efSJohn Youn hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >> 746323230efSJohn Youn GHWCFG2_HS_PHY_TYPE_SHIFT; 747323230efSJohn Youn hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >> 748323230efSJohn Youn GHWCFG2_FS_PHY_TYPE_SHIFT; 749323230efSJohn Youn hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >> 750323230efSJohn Youn GHWCFG2_NUM_DEV_EP_SHIFT; 751323230efSJohn Youn hw->nperio_tx_q_depth = 752323230efSJohn Youn (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >> 753323230efSJohn Youn GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1; 754323230efSJohn Youn hw->host_perio_tx_q_depth = 755323230efSJohn Youn (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >> 756323230efSJohn Youn GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1; 757323230efSJohn Youn hw->dev_token_q_depth = 758323230efSJohn Youn (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >> 759323230efSJohn Youn GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT; 760323230efSJohn Youn 761323230efSJohn Youn /* hwcfg3 */ 762323230efSJohn Youn width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >> 763323230efSJohn Youn GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT; 764323230efSJohn Youn hw->max_transfer_size = (1 << (width + 11)) - 1; 765323230efSJohn Youn width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >> 766323230efSJohn Youn GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT; 767323230efSJohn Youn hw->max_packet_count = (1 << (width + 4)) - 1; 768323230efSJohn Youn hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C); 769323230efSJohn Youn hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >> 770323230efSJohn Youn GHWCFG3_DFIFO_DEPTH_SHIFT; 7716f80b6deSSevak Arakelyan hw->lpm_mode = !!(hwcfg3 & GHWCFG3_OTG_LPM_EN); 772323230efSJohn Youn 773323230efSJohn Youn /* hwcfg4 */ 774323230efSJohn Youn hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN); 775323230efSJohn Youn hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >> 776323230efSJohn Youn GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT; 7779273083aSMinas Harutyunyan hw->num_dev_in_eps = (hwcfg4 & GHWCFG4_NUM_IN_EPS_MASK) >> 7789273083aSMinas Harutyunyan GHWCFG4_NUM_IN_EPS_SHIFT; 779323230efSJohn Youn hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA); 780323230efSJohn Youn hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ); 781631a2310SVardan Mikayelyan hw->hibernation = !!(hwcfg4 & GHWCFG4_HIBER); 782323230efSJohn Youn hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >> 783323230efSJohn Youn GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT; 78466e77a24SRazmik Karapetyan hw->acg_enable = !!(hwcfg4 & GHWCFG4_ACG_SUPPORTED); 785b43ebc96SGrigor Tovmasyan hw->ipg_isoc_en = !!(hwcfg4 & GHWCFG4_IPG_ISOC_SUPPORTED); 786323230efSJohn Youn 787323230efSJohn Youn /* fifo sizes */ 788d1531319SJohn Youn hw->rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >> 789323230efSJohn Youn GRXFSIZ_DEPTH_SHIFT; 7909273083aSMinas Harutyunyan /* 7919273083aSMinas Harutyunyan * Host specific hardware parameters. Reading these parameters 7929273083aSMinas Harutyunyan * requires the controller to be in host mode. The mode will 7939273083aSMinas Harutyunyan * be forced, if necessary, to read these values. 7949273083aSMinas Harutyunyan */ 7959273083aSMinas Harutyunyan dwc2_get_host_hwparams(hsotg); 7969273083aSMinas Harutyunyan dwc2_get_dev_hwparams(hsotg); 797323230efSJohn Youn 798323230efSJohn Youn return 0; 799323230efSJohn Youn } 800323230efSJohn Youn 801334bbd4eSJohn Youn int dwc2_init_params(struct dwc2_hsotg *hsotg) 802334bbd4eSJohn Youn { 8037de1debcSJohn Youn const struct of_device_id *match; 8047de1debcSJohn Youn void (*set_params)(void *data); 8057de1debcSJohn Youn 806245977c9SJohn Youn dwc2_set_default_params(hsotg); 807f9f93cbbSJohn Youn dwc2_get_device_properties(hsotg); 808334bbd4eSJohn Youn 8097de1debcSJohn Youn match = of_match_device(dwc2_of_match_table, hsotg->dev); 8107de1debcSJohn Youn if (match && match->data) { 8117de1debcSJohn Youn set_params = match->data; 8127de1debcSJohn Youn set_params(hsotg); 8137de1debcSJohn Youn } 8147de1debcSJohn Youn 815d936e666SJohn Youn dwc2_check_params(hsotg); 816d936e666SJohn Youn 817334bbd4eSJohn Youn return 0; 818334bbd4eSJohn Youn } 819