linux/drivers/phy/phy-zynqmp.c
<<
>>
Prefs
   1/*
   2 * phy-zynqmp.c - PHY driver for Xilinx ZynqMP GT.
   3 *
   4 * Copyright (C) 2015 - 2016 Xilinx Inc.
   5 *
   6 * Author: Subbaraya Sundeep <sbhatta@xilinx.com>
   7 * Author: Anurag Kumar Vulisha <anuragku@xilinx.com>
   8 *
   9 * This program is free software: you can redistribute it and/or modify
  10 * it under the terms of the GNU General Public License version 2  of
  11 * the License as published by the Free Software Foundation.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 * GNU General Public License for more details.
  17 *
  18 * This driver is tested for USB and SATA currently.
  19 * Other controllers PCIe, Display Port and SGMII should also
  20 * work but that is experimental as of now.
  21 */
  22
  23#include <linux/clk.h>
  24#include <linux/delay.h>
  25#include <linux/io.h>
  26#include <linux/kernel.h>
  27#include <linux/module.h>
  28#include <linux/of.h>
  29#include <linux/of_platform.h>
  30#include <linux/of_address.h>
  31#include <linux/phy/phy.h>
  32#include <linux/phy/phy-zynqmp.h>
  33#include <linux/platform_device.h>
  34#include <linux/delay.h>
  35#include <dt-bindings/phy/phy.h>
  36#include <linux/soc/xilinx/zynqmp/fw.h>
  37#include <linux/soc/xilinx/zynqmp/firmware.h>
  38#include <linux/reset.h>
  39#include <linux/list.h>
  40#include <linux/slab.h>
  41
  42#define MAX_LANES                       4
  43
  44#define RST_ULPI                        0x0250
  45#define RST_ULPI_HI                     0x202
  46#define RST_ULPI_LOW                    0x02
  47
  48#define RST_ULPI_TIMEOUT                10
  49#define RST_TIMEOUT                     1000
  50
  51#define ICM_CFG0                        0x10010
  52#define ICM_CFG1                        0x10014
  53#define ICM_CFG0_L0_MASK                0x07
  54#define ICM_CFG0_L1_MASK                0x70
  55#define ICM_CFG1_L2_MASK                0x07
  56#define ICM_CFG2_L3_MASK                0x70
  57
  58#define TM_CMN_RST                      0x10018
  59#define TM_CMN_RST_MASK                 0x3
  60#define TM_CMN_RST_EN                   0x1
  61#define TM_CMN_RST_SET                  0x2
  62
  63#define ICM_PROTOCOL_PD                 0x0
  64#define ICM_PROTOCOL_PCIE               0x1
  65#define ICM_PROTOCOL_SATA               0x2
  66#define ICM_PROTOCOL_USB                0x3
  67#define ICM_PROTOCOL_DP                 0x4
  68#define ICM_PROTOCOL_SGMII              0x5
  69
  70#define PLL_REF_SEL0                    0x10000
  71#define PLL_REF_OFFSET                  0x4
  72#define PLL_FREQ_MASK                   0x1F
  73
  74#define L0_L0_REF_CLK_SEL               0x2860
  75
  76#define L0_PLL_STATUS_READ_1            0x23E4
  77#define PLL_STATUS_READ_OFFSET          0x4000
  78#define PLL_STATUS_LOCKED               0x10
  79
  80#define L0_PLL_SS_STEP_SIZE_0_LSB       0x2370
  81#define L0_PLL_SS_STEP_SIZE_1           0x2374
  82#define L0_PLL_SS_STEP_SIZE_2           0x2378
  83#define L0_PLL_SS_STEP_SIZE_3_MSB       0x237C
  84#define STEP_SIZE_OFFSET                0x4000
  85#define STEP_SIZE_0_MASK                0xFF
  86#define STEP_SIZE_1_MASK                0xFF
  87#define STEP_SIZE_2_MASK                0xFF
  88#define STEP_SIZE_3_MASK                0x3
  89#define FORCE_STEP_SIZE                 0x10
  90#define FORCE_STEPS                     0x20
  91
  92#define L0_PLL_SS_STEPS_0_LSB           0x2368
  93#define L0_PLL_SS_STEPS_1_MSB           0x236C
  94#define STEPS_OFFSET                    0x4000
  95#define STEPS_0_MASK                    0xFF
  96#define STEPS_1_MASK                    0x07
  97
  98#define BGCAL_REF_SEL                   0x10028
  99#define BGCAL_REF_VALUE                 0x0C
 100
 101#define L3_TM_CALIB_DIG19               0xEC4C
 102#define L3_TM_CALIB_DIG19_NSW           0x07
 103
 104#define TM_OVERRIDE_NSW_CODE            0x20
 105
 106#define L3_CALIB_DONE_STATUS            0xEF14
 107#define CALIB_DONE                      0x02
 108
 109#define L0_TXPMA_ST_3                   0x0B0C
 110#define DN_CALIB_CODE                   0x3F
 111#define DN_CALIB_SHIFT                  3
 112
 113#define L3_TM_CALIB_DIG18               0xEC48
 114#define L3_TM_CALIB_DIG18_NSW           0xE0
 115#define NSW_SHIFT                       5
 116#define NSW_PIPE_SHIFT                  4
 117
 118#define L0_TM_PLL_DIG_37                0x2094
 119#define TM_PLL_DIG_37_OFFSET            0x4000
 120#define TM_COARSE_CODE_LIMIT            0x10
 121
 122#define L0_TM_DIG_6                     0x106C
 123#define TM_DIG_6_OFFSET                 0x4000
 124#define TM_DISABLE_DESCRAMBLE_DECODER   0x0F
 125
 126#define L0_TX_DIG_61                    0x00F4
 127#define TX_DIG_61_OFFSET                0x4000
 128#define TM_DISABLE_SCRAMBLE_ENCODER     0x0F
 129
 130#define L0_TX_ANA_TM_18                 0x0048
 131#define TX_ANA_TM_18_OFFSET             0x4000
 132
 133#define L0_TX_ANA_TM_118                0x01D8
 134#define TX_ANA_TM_118_OFFSET            0x4000
 135#define L0_TX_ANA_TM_118_FORCE_17_0     BIT(0)
 136
 137#define L0_TXPMD_TM_45                  0x0CB4
 138#define TXPMD_TM_45_OFFSET              0x4000
 139#define L0_TXPMD_TM_45_OVER_DP_MAIN     BIT(0)
 140#define L0_TXPMD_TM_45_ENABLE_DP_MAIN   BIT(1)
 141#define L0_TXPMD_TM_45_OVER_DP_POST1    BIT(2)
 142#define L0_TXPMD_TM_45_ENABLE_DP_POST1  BIT(3)
 143#define L0_TXPMD_TM_45_OVER_DP_POST2    BIT(4)
 144#define L0_TXPMD_TM_45_ENABLE_DP_POST2  BIT(5)
 145
 146#define L0_TXPMD_TM_48                  0x0CC0
 147#define TXPMD_TM_48_OFFSET              0x4000
 148
 149#define TX_PROT_BUS_WIDTH               0x10040
 150#define RX_PROT_BUS_WIDTH               0x10044
 151
 152#define PROT_BUS_WIDTH_SHIFT            2
 153#define PROT_BUS_WIDTH_10               0x0
 154#define PROT_BUS_WIDTH_20               0x1
 155#define PROT_BUS_WIDTH_40               0x2
 156
 157#define LANE_CLK_SHARE_MASK             0x8F
 158
 159#define SATA_CONTROL_OFFSET             0x0100
 160
 161#define CONTROLLERS_PER_LANE            5
 162
 163#define IOU_SLCR                        0xFF180000
 164
 165#define IOU_GEM_CTRL_OFFSET             0x360
 166#define SGMII_SD_MASK                   0x3
 167#define SGMII_SD_OFFSET                 2
 168#define SGMII_PCS_SD_0                  0x0
 169#define SGMII_PCS_SD_1                  0x1
 170#define SGMII_PCS_SD_PHY                0x2
 171
 172#define IOU_GEM_CLK_CTRL_OFFSET         0x308
 173#define GEM_CLK_CTRL_MASK               0xF
 174#define GEM_CLK_CTRL_OFFSET             5
 175#define GEM_RX_SRC_SEL_GTR              0x1
 176#define GEM_REF_SRC_SEL_GTR             0x2
 177#define GEM_SGMII_MODE                  0x4
 178#define GEM_FIFO_CLK_PL                 0x8
 179
 180#define PIPE_CLK_OFFSET                 0x7c
 181#define PIPE_CLK_ON                     1
 182#define PIPE_CLK_OFF                    0
 183#define PIPE_POWER_OFFSET               0x80
 184#define PIPE_POWER_ON                   1
 185#define PIPE_POWER_OFF                  0
 186
 187#define XPSGTR_TYPE_USB0        0 /* USB controller 0 */
 188#define XPSGTR_TYPE_USB1        1 /* USB controller 1 */
 189#define XPSGTR_TYPE_SATA_0      2 /* SATA controller lane 0 */
 190#define XPSGTR_TYPE_SATA_1      3 /* SATA controller lane 1 */
 191#define XPSGTR_TYPE_PCIE_0      4 /* PCIe controller lane 0 */
 192#define XPSGTR_TYPE_PCIE_1      5 /* PCIe controller lane 1 */
 193#define XPSGTR_TYPE_PCIE_2      6 /* PCIe controller lane 2 */
 194#define XPSGTR_TYPE_PCIE_3      7 /* PCIe controller lane 3 */
 195#define XPSGTR_TYPE_DP_0        8 /* Display Port controller lane 0 */
 196#define XPSGTR_TYPE_DP_1        9 /* Display Port controller lane 1 */
 197#define XPSGTR_TYPE_SGMII0      10 /* Ethernet SGMII controller 0 */
 198#define XPSGTR_TYPE_SGMII1      11 /* Ethernet SGMII controller 1 */
 199#define XPSGTR_TYPE_SGMII2      12 /* Ethernet SGMII controller 2 */
 200#define XPSGTR_TYPE_SGMII3      13 /* Ethernet SGMII controller 3 */
 201
 202/*
 203 * This table holds the valid combinations of controllers and
 204 * lanes(Interconnect Matrix).
 205 */
 206static unsigned int icm_matrix[][CONTROLLERS_PER_LANE] = {
 207        { XPSGTR_TYPE_PCIE_0, XPSGTR_TYPE_SATA_0, XPSGTR_TYPE_USB0,
 208                XPSGTR_TYPE_DP_1, XPSGTR_TYPE_SGMII0 },
 209        { XPSGTR_TYPE_PCIE_1, XPSGTR_TYPE_SATA_1, XPSGTR_TYPE_USB0,
 210                XPSGTR_TYPE_DP_0, XPSGTR_TYPE_SGMII1 },
 211        { XPSGTR_TYPE_PCIE_2, XPSGTR_TYPE_SATA_0, XPSGTR_TYPE_USB0,
 212                XPSGTR_TYPE_DP_1, XPSGTR_TYPE_SGMII2 },
 213        { XPSGTR_TYPE_PCIE_3, XPSGTR_TYPE_SATA_1, XPSGTR_TYPE_USB1,
 214                XPSGTR_TYPE_DP_0, XPSGTR_TYPE_SGMII3 }
 215};
 216
 217/* Allowed PLL reference clock frequencies */
 218enum pll_frequencies {
 219        REF_19_2M = 0,
 220        REF_20M,
 221        REF_24M,
 222        REF_26M,
 223        REF_27M,
 224        REF_38_4M,
 225        REF_40M,
 226        REF_52M,
 227        REF_100M,
 228        REF_108M,
 229        REF_125M,
 230        REF_135M,
 231        REF_150M,
 232};
 233
 234/**
 235 * struct xpsgtr_phy - representation of a lane
 236 * @phy: pointer to the kernel PHY device
 237 * @type: controller which uses this lane
 238 * @lane: lane number
 239 * @protocol: protocol in which the lane operates
 240 * @ref_clk: enum of allowed ref clock rates for this lane PLL
 241 * @pll_lock: PLL status
 242 * @data: pointer to hold private data
 243 * @refclk_rate: PLL reference clock frequency
 244 * @share_laneclk: lane number of the clock to be shared
 245 */
 246struct xpsgtr_phy {
 247        struct phy *phy;
 248        u8 type;
 249        u8 lane;
 250        u8 protocol;
 251        enum pll_frequencies ref_clk;
 252        bool pll_lock;
 253        void *data;
 254        u32 refclk_rate;
 255        u32 share_laneclk;
 256};
 257
 258/**
 259 * struct xpsgtr_ssc - structure to hold SSC settings for a lane
 260 * @refclk_rate: PLL reference clock frequency
 261 * @pll_ref_clk: value to be written to register for corresponding ref clk rate
 262 * @steps: number of steps of SSC (Spread Spectrum Clock)
 263 * @step_size: step size of each step
 264 */
 265struct xpsgtr_ssc {
 266        u32 refclk_rate;
 267        u8  pll_ref_clk;
 268        u32 steps;
 269        u32 step_size;
 270};
 271
 272/* lookup table to hold all settings needed for a ref clock frequency */
 273static struct xpsgtr_ssc ssc_lookup[] = {
 274        {19200000, 0x05, 608, 264020},
 275        {20000000, 0x06, 634, 243454},
 276        {24000000, 0x07, 760, 168973},
 277        {26000000, 0x08, 824, 143860},
 278        {27000000, 0x09, 856, 86551},
 279        {38400000, 0x0A, 1218, 65896},
 280        {40000000, 0x0B, 634, 243454},
 281        {52000000, 0x0C, 824, 143860},
 282        {100000000, 0x0D, 1058, 87533},
 283        {108000000, 0x0E, 856, 86551},
 284        {125000000, 0x0F, 992, 119497},
 285        {135000000, 0x10, 1070, 55393},
 286        {150000000, 0x11, 792, 187091}
 287};
 288
 289/**
 290 * struct xpsgtr_dev - representation of a ZynMP GT device
 291 * @dev: pointer to device
 292 * @serdes: serdes base address
 293 * @siou: siou base address
 294 * @gtr_mutex: mutex for locking
 295 * @phys: pointer to all the lanes
 296 * @lpd: base address for low power domain devices reset control
 297 * @regs: address that phy needs to configure during configuring lane protocol
 298 * @tx_term_fix: fix for GT issue
 299 * @sata_rst: a reset control for SATA
 300 * @dp_rst: a reset control for DP
 301 * @usb0_crst: a reset control for usb0 core
 302 * @usb1_crst: a reset control for usb1 core
 303 * @usb0_hibrst: a reset control for usb0 hibernation module
 304 * @usb1_hibrst: a reset control for usb1 hibernation module
 305 * @usb0_apbrst: a reset control for usb0 apb bus
 306 * @usb1_apbrst: a reset control for usb1 apb bus
 307 * @gem0_rst: a reset control for gem0
 308 * @gem1_rst: a reset control for gem1
 309 * @gem2_rst: a reset control for gem2
 310 * @gem3_rst: a reset control for gem3
 311 */
 312struct xpsgtr_dev {
 313        struct device *dev;
 314        void __iomem *serdes;
 315        void __iomem *siou;
 316        struct mutex gtr_mutex;
 317        struct xpsgtr_phy **phys;
 318        void __iomem *lpd;
 319        void __iomem *regs;
 320        bool tx_term_fix;
 321        struct reset_control *sata_rst;
 322        struct reset_control *dp_rst;
 323        struct reset_control *usb0_crst;
 324        struct reset_control *usb1_crst;
 325        struct reset_control *usb0_hibrst;
 326        struct reset_control *usb1_hibrst;
 327        struct reset_control *usb0_apbrst;
 328        struct reset_control *usb1_apbrst;
 329        struct reset_control *gem0_rst;
 330        struct reset_control *gem1_rst;
 331        struct reset_control *gem2_rst;
 332        struct reset_control *gem3_rst;
 333};
 334
 335/**
 336 * xpsgtr_set_protregs - Called by the lane protocol to set phy related control
 337 *                       regs into gtr_dev, so that these address can be used
 338 *                       by phy while configuring lane.(Currently USB does this)
 339 *
 340 * @phy: pointer to lane
 341 * @regs:    pointer to protocol control register address
 342 *
 343 * Return: 0 on success
 344 */
 345int xpsgtr_set_protregs(struct phy *phy, void __iomem *regs)
 346{
 347        struct xpsgtr_phy *gtr_phy = phy_get_drvdata(phy);
 348        struct xpsgtr_dev *gtr_dev = gtr_phy->data;
 349
 350        gtr_dev->regs = regs;
 351        return 0;
 352}
 353EXPORT_SYMBOL_GPL(xpsgtr_set_protregs);
 354
 355int xpsgtr_override_deemph(struct phy *phy, u8 plvl, u8 vlvl)
 356{
 357        struct xpsgtr_phy *gtr_phy = phy_get_drvdata(phy);
 358        struct xpsgtr_dev *gtr_dev = gtr_phy->data;
 359        static u8 pe[4][4] = { { 0x2, 0x2, 0x2, 0x2 },
 360                               { 0x1, 0x1, 0x1, 0xff },
 361                               { 0x0, 0x0, 0xff, 0xff },
 362                               { 0xff, 0xff, 0xff, 0xff } };
 363
 364        writel(pe[plvl][vlvl],
 365               gtr_dev->serdes + gtr_phy->lane * TX_ANA_TM_18_OFFSET +
 366               L0_TX_ANA_TM_18);
 367
 368        return 0;
 369}
 370EXPORT_SYMBOL_GPL(xpsgtr_override_deemph);
 371
 372int xpsgtr_margining_factor(struct phy *phy, u8 plvl, u8 vlvl)
 373{
 374        struct xpsgtr_phy *gtr_phy = phy_get_drvdata(phy);
 375        struct xpsgtr_dev *gtr_dev = gtr_phy->data;
 376        static u8 vs[4][4] = { { 0x2a, 0x27, 0x24, 0x20 },
 377                               { 0x27, 0x23, 0x20, 0xff },
 378                               { 0x24, 0x20, 0xff, 0xff },
 379                               { 0xff, 0xff, 0xff, 0xff } };
 380
 381        writel(vs[plvl][vlvl],
 382               gtr_dev->serdes + gtr_phy->lane * TXPMD_TM_48_OFFSET +
 383               L0_TXPMD_TM_48);
 384
 385        return 0;
 386}
 387EXPORT_SYMBOL_GPL(xpsgtr_margining_factor);
 388
 389/**
 390 * xpsgtr_configure_pll - configures SSC settings for a lane
 391 * @gtr_phy: pointer to lane
 392 */
 393static void xpsgtr_configure_pll(struct xpsgtr_phy *gtr_phy)
 394{
 395        struct xpsgtr_dev *gtr_dev = gtr_phy->data;
 396        u32 reg;
 397        u32 offset;
 398        u32 steps;
 399        u32 size;
 400        u8 pll_ref_clk;
 401
 402        steps = ssc_lookup[gtr_phy->ref_clk].steps;
 403        size = ssc_lookup[gtr_phy->ref_clk].step_size;
 404        pll_ref_clk = ssc_lookup[gtr_phy->ref_clk].pll_ref_clk;
 405
 406        offset = gtr_phy->lane * PLL_REF_OFFSET + PLL_REF_SEL0;
 407        reg = readl(gtr_dev->serdes + offset);
 408        reg = (reg & ~PLL_FREQ_MASK) | pll_ref_clk;
 409        writel(reg, gtr_dev->serdes + offset);
 410
 411        /* Enable lane clock sharing, if required */
 412        if (gtr_phy->share_laneclk != gtr_phy->lane) {
 413                /* Lane3 Ref Clock Selection Register */
 414                offset = gtr_phy->lane * PLL_REF_OFFSET + L0_L0_REF_CLK_SEL;
 415                reg = readl(gtr_dev->serdes + offset);
 416                reg = (reg & ~LANE_CLK_SHARE_MASK) |
 417                                (1 << gtr_phy->share_laneclk);
 418                writel(reg, gtr_dev->serdes + offset);
 419        }
 420
 421        /* SSC step size [7:0] */
 422        offset = gtr_phy->lane * STEP_SIZE_OFFSET + L0_PLL_SS_STEP_SIZE_0_LSB;
 423        reg = readl(gtr_dev->serdes + offset);
 424        reg = (reg & ~STEP_SIZE_0_MASK) |
 425                (size & STEP_SIZE_0_MASK);
 426        writel(reg, gtr_dev->serdes + offset);
 427
 428        /* SSC step size [15:8] */
 429        size = size >> 8;
 430        offset = gtr_phy->lane * STEP_SIZE_OFFSET + L0_PLL_SS_STEP_SIZE_1;
 431        reg = readl(gtr_dev->serdes + offset);
 432        reg = (reg & ~STEP_SIZE_1_MASK) |
 433                (size & STEP_SIZE_1_MASK);
 434        writel(reg, gtr_dev->serdes + offset);
 435
 436        /* SSC step size [23:16] */
 437        size = size >> 8;
 438        offset = gtr_phy->lane * STEP_SIZE_OFFSET + L0_PLL_SS_STEP_SIZE_2;
 439        reg = readl(gtr_dev->serdes + offset);
 440        reg = (reg & ~STEP_SIZE_2_MASK) |
 441                (size & STEP_SIZE_2_MASK);
 442        writel(reg, gtr_dev->serdes + offset);
 443
 444        /* SSC steps [7:0] */
 445        offset = gtr_phy->lane * STEPS_OFFSET + L0_PLL_SS_STEPS_0_LSB;
 446        reg = readl(gtr_dev->serdes + offset);
 447        reg = (reg & ~STEPS_0_MASK) |
 448                (steps & STEPS_0_MASK);
 449        writel(reg, gtr_dev->serdes + offset);
 450
 451        /* SSC steps [10:8] */
 452        steps = steps >> 8;
 453        offset = gtr_phy->lane * STEPS_OFFSET + L0_PLL_SS_STEPS_1_MSB;
 454        reg = readl(gtr_dev->serdes + offset);
 455        reg = (reg & ~STEPS_1_MASK) |
 456                (steps & STEPS_1_MASK);
 457        writel(reg, gtr_dev->serdes + offset);
 458
 459        /* SSC step size [24:25] */
 460        size = size >> 8;
 461        offset = gtr_phy->lane * STEP_SIZE_OFFSET + L0_PLL_SS_STEP_SIZE_3_MSB;
 462        reg = readl(gtr_dev->serdes + offset);
 463        reg = (reg & ~STEP_SIZE_3_MASK) |
 464                (size & STEP_SIZE_3_MASK);
 465        reg |= FORCE_STEP_SIZE | FORCE_STEPS;
 466        writel(reg, gtr_dev->serdes + offset);
 467}
 468
 469/**
 470 * xpsgtr_lane_setprotocol - sets required protocol in ICM registers
 471 * @gtr_phy: pointer to lane
 472 */
 473static void xpsgtr_lane_setprotocol(struct xpsgtr_phy *gtr_phy)
 474{
 475        struct xpsgtr_dev *gtr_dev = gtr_phy->data;
 476        u32 reg;
 477        u8 protocol = gtr_phy->protocol;
 478
 479        switch (gtr_phy->lane) {
 480        case 0:
 481                reg = readl(gtr_dev->serdes + ICM_CFG0);
 482                reg = (reg & ~ICM_CFG0_L0_MASK) | protocol;
 483                writel(reg, gtr_dev->serdes + ICM_CFG0);
 484                break;
 485        case 1:
 486                reg = readl(gtr_dev->serdes + ICM_CFG0);
 487                reg = (reg & ~ICM_CFG0_L1_MASK) | (protocol << 4);
 488                writel(reg, gtr_dev->serdes + ICM_CFG0);
 489                break;
 490        case 2:
 491                reg = readl(gtr_dev->serdes + ICM_CFG1);
 492                reg = (reg & ~ICM_CFG0_L0_MASK) | protocol;
 493                writel(reg, gtr_dev->serdes + ICM_CFG1);
 494                break;
 495        case 3:
 496                reg = readl(gtr_dev->serdes + ICM_CFG1);
 497                reg = (reg & ~ICM_CFG0_L1_MASK) | (protocol << 4);
 498                writel(reg, gtr_dev->serdes + ICM_CFG1);
 499                break;
 500        default:
 501                /* We already checked 0 <= lane <= 3 */
 502                break;
 503        }
 504}
 505
 506/**
 507 * xpsgtr_get_ssc - gets the required ssc settings based on clk rate
 508 * @gtr_phy: pointer to lane
 509 *
 510 * Return: 0 on success or error on failure
 511 */
 512static int xpsgtr_get_ssc(struct xpsgtr_phy *gtr_phy)
 513{
 514        u32 i;
 515
 516        /*
 517         * Assign the required spread spectrum(SSC) settings
 518         * from lane refernce clk rate
 519         */
 520        for (i = 0 ; i < ARRAY_SIZE(ssc_lookup); i++) {
 521                if (gtr_phy->refclk_rate == ssc_lookup[i].refclk_rate) {
 522                        gtr_phy->ref_clk = i;
 523                        return 0;
 524                }
 525        }
 526
 527        /* Did not get valid ssc settings*/
 528        return -EINVAL;
 529}
 530
 531/**
 532 * xpsgtr_configure_lane - configures SSC settings for a lane
 533 * @gtr_phy: pointer to lane
 534 *
 535 * Return: 0 on success or error on failure
 536 */
 537static int xpsgtr_configure_lane(struct xpsgtr_phy *gtr_phy)
 538{
 539
 540        switch (gtr_phy->type) {
 541        case XPSGTR_TYPE_USB0:
 542        case XPSGTR_TYPE_USB1:
 543                gtr_phy->protocol = ICM_PROTOCOL_USB;
 544                break;
 545        case XPSGTR_TYPE_SATA_0:
 546        case XPSGTR_TYPE_SATA_1:
 547                gtr_phy->protocol = ICM_PROTOCOL_SATA;
 548                break;
 549        case XPSGTR_TYPE_DP_0:
 550        case XPSGTR_TYPE_DP_1:
 551                gtr_phy->protocol = ICM_PROTOCOL_DP;
 552                break;
 553        case XPSGTR_TYPE_PCIE_0:
 554        case XPSGTR_TYPE_PCIE_1:
 555        case XPSGTR_TYPE_PCIE_2:
 556        case XPSGTR_TYPE_PCIE_3:
 557                gtr_phy->protocol = ICM_PROTOCOL_PCIE;
 558                break;
 559        case XPSGTR_TYPE_SGMII0:
 560        case XPSGTR_TYPE_SGMII1:
 561        case XPSGTR_TYPE_SGMII2:
 562        case XPSGTR_TYPE_SGMII3:
 563                gtr_phy->protocol = ICM_PROTOCOL_SGMII;
 564                break;
 565        default:
 566                gtr_phy->protocol = ICM_PROTOCOL_PD;
 567                break;
 568        }
 569
 570        /* Get SSC settinsg for refernce clk rate */
 571        if (xpsgtr_get_ssc(gtr_phy) < 0)
 572                return -EINVAL;
 573
 574        return 0;
 575}
 576
 577/**
 578 * xpsgtr_config_usbpipe - configures the PIPE3 signals for USB
 579 * @gtr_dev: pointer to gtr device
 580 */
 581static void xpsgtr_config_usbpipe(struct xpsgtr_dev *gtr_dev)
 582{
 583        if (gtr_dev->regs != NULL) {
 584                /* Set PIPE power present signal */
 585                writel(PIPE_POWER_ON, gtr_dev->regs + PIPE_POWER_OFFSET);
 586                /* Clear PIPE CLK signal */
 587                writel(PIPE_CLK_OFF, gtr_dev->regs + PIPE_CLK_OFFSET);
 588        }
 589}
 590
 591/**
 592 * xpsgtr_reset_assert - asserts reset using reset framework
 593 * @rstc: pointer to reset_control
 594 *
 595 * Return: 0 on success or error on failure
 596 */
 597static int xpsgtr_reset_assert(struct reset_control *rstc)
 598{
 599        unsigned long loop_time = msecs_to_jiffies(RST_TIMEOUT);
 600        unsigned long timeout;
 601
 602        reset_control_assert(rstc);
 603
 604        /* wait until reset is asserted or timeout */
 605        timeout = jiffies + loop_time;
 606
 607        while (!time_after_eq(jiffies, timeout)) {
 608                if (reset_control_status(rstc) > 0)
 609                        return 0;
 610
 611                cpu_relax();
 612        }
 613
 614        return -ETIMEDOUT;
 615}
 616
 617/**
 618 * xpsgtr_reset_release - de-asserts reset using reset framework
 619 * @rstc: pointer to reset_control
 620 *
 621 * Return: 0 on success or error on failure
 622 */
 623static int xpsgtr_reset_release(struct reset_control *rstc)
 624{
 625        unsigned long loop_time = msecs_to_jiffies(RST_TIMEOUT);
 626        unsigned long timeout;
 627
 628        reset_control_deassert(rstc);
 629
 630        /* wait until reset is de-asserted or timeout */
 631        timeout = jiffies + loop_time;
 632        while (!time_after_eq(jiffies, timeout)) {
 633                if (!reset_control_status(rstc))
 634                        return 0;
 635
 636                cpu_relax();
 637        }
 638
 639        return -ETIMEDOUT;
 640}
 641
 642/**
 643 * xpsgtr_controller_reset - puts controller in reset
 644 * @gtr_phy: pointer to lane
 645 *
 646 * Return: 0 on success or error on failure
 647 */
 648static int xpsgtr_controller_reset(struct xpsgtr_phy *gtr_phy)
 649{
 650        struct xpsgtr_dev *gtr_dev = gtr_phy->data;
 651        int ret;
 652
 653        switch (gtr_phy->type) {
 654        case XPSGTR_TYPE_USB0:
 655                ret = xpsgtr_reset_assert(gtr_dev->usb0_crst);
 656                ret = xpsgtr_reset_assert(gtr_dev->usb0_hibrst);
 657                ret = xpsgtr_reset_assert(gtr_dev->usb0_apbrst);
 658                break;
 659        case XPSGTR_TYPE_USB1:
 660                ret = xpsgtr_reset_assert(gtr_dev->usb1_crst);
 661                ret = xpsgtr_reset_assert(gtr_dev->usb1_hibrst);
 662                ret = xpsgtr_reset_assert(gtr_dev->usb1_apbrst);
 663                break;
 664        case XPSGTR_TYPE_SATA_0:
 665        case XPSGTR_TYPE_SATA_1:
 666                ret = xpsgtr_reset_assert(gtr_dev->sata_rst);
 667                break;
 668        case XPSGTR_TYPE_DP_0:
 669        case XPSGTR_TYPE_DP_1:
 670                ret = xpsgtr_reset_assert(gtr_dev->dp_rst);
 671                break;
 672        case XPSGTR_TYPE_SGMII0:
 673                ret = xpsgtr_reset_assert(gtr_dev->gem0_rst);
 674                break;
 675        case XPSGTR_TYPE_SGMII1:
 676                ret = xpsgtr_reset_assert(gtr_dev->gem1_rst);
 677                break;
 678        case XPSGTR_TYPE_SGMII2:
 679                ret = xpsgtr_reset_assert(gtr_dev->gem2_rst);
 680                break;
 681        case XPSGTR_TYPE_SGMII3:
 682                ret = xpsgtr_reset_assert(gtr_dev->gem3_rst);
 683                break;
 684        default:
 685                ret = -EINVAL;
 686                break;
 687        }
 688
 689        return ret;
 690}
 691
 692/**
 693 * xpsgtr_controller_release_reset - releases controller from reset
 694 * @gtr_phy: pointer to lane
 695 *
 696 * Return: 0 on success or error on failure
 697 */
 698static int xpsgtr_controller_release_reset(struct xpsgtr_phy *gtr_phy)
 699{
 700        struct xpsgtr_dev *gtr_dev = gtr_phy->data;
 701        int ret;
 702
 703        switch (gtr_phy->type) {
 704        case XPSGTR_TYPE_USB0:
 705                xpsgtr_reset_release(gtr_dev->usb0_apbrst);
 706
 707                /* Config PIPE3 signals after releasing APB reset */
 708                xpsgtr_config_usbpipe(gtr_dev);
 709
 710                ret = xpsgtr_reset_release(gtr_dev->usb0_crst);
 711                ret = xpsgtr_reset_release(gtr_dev->usb0_hibrst);
 712                break;
 713        case XPSGTR_TYPE_USB1:
 714                xpsgtr_reset_release(gtr_dev->usb1_apbrst);
 715
 716                /* Config PIPE3 signals after releasing APB reset */
 717                xpsgtr_config_usbpipe(gtr_dev);
 718
 719                ret = xpsgtr_reset_release(gtr_dev->usb1_crst);
 720                ret = xpsgtr_reset_release(gtr_dev->usb1_hibrst);
 721                break;
 722        case XPSGTR_TYPE_SATA_0:
 723        case XPSGTR_TYPE_SATA_1:
 724                ret = xpsgtr_reset_release(gtr_dev->sata_rst);
 725                break;
 726        case XPSGTR_TYPE_DP_0:
 727        case XPSGTR_TYPE_DP_1:
 728                ret = xpsgtr_reset_release(gtr_dev->dp_rst);
 729                break;
 730        case XPSGTR_TYPE_SGMII0:
 731                ret = xpsgtr_reset_release(gtr_dev->gem0_rst);
 732                break;
 733        case XPSGTR_TYPE_SGMII1:
 734                ret = xpsgtr_reset_release(gtr_dev->gem1_rst);
 735                break;
 736        case XPSGTR_TYPE_SGMII2:
 737                ret = xpsgtr_reset_release(gtr_dev->gem2_rst);
 738                break;
 739        case XPSGTR_TYPE_SGMII3:
 740                ret = xpsgtr_reset_release(gtr_dev->gem3_rst);
 741                break;
 742        default:
 743                ret = -EINVAL;
 744                break;
 745        }
 746
 747        return ret;
 748}
 749
 750int xpsgtr_wait_pll_lock(struct phy *phy)
 751{
 752        struct xpsgtr_phy *gtr_phy = phy_get_drvdata(phy);
 753        struct xpsgtr_dev *gtr_dev = gtr_phy->data;
 754        u32 offset, reg;
 755        u32 timeout = 1000;
 756        int ret = 0;
 757
 758        /* Check pll is locked */
 759        offset = gtr_phy->lane * PLL_STATUS_READ_OFFSET + L0_PLL_STATUS_READ_1;
 760        dev_dbg(gtr_dev->dev, "Waiting for PLL lock...\n");
 761
 762        do {
 763                reg = readl(gtr_dev->serdes + offset);
 764                if ((reg & PLL_STATUS_LOCKED) == PLL_STATUS_LOCKED)
 765                        break;
 766
 767                if (!--timeout) {
 768                        dev_err(gtr_dev->dev, "PLL lock time out\n");
 769                        ret = -ETIMEDOUT;
 770                        break;
 771                }
 772                udelay(1);
 773        } while (1);
 774
 775        if (ret == 0)
 776                gtr_phy->pll_lock = true;
 777
 778        dev_info(gtr_dev->dev, "Lane:%d type:%d protocol:%d pll_locked:%s\n",
 779                        gtr_phy->lane, gtr_phy->type, gtr_phy->protocol,
 780                        gtr_phy->pll_lock ? "yes" : "no");
 781        return ret;
 782}
 783EXPORT_SYMBOL_GPL(xpsgtr_wait_pll_lock);
 784
 785/**
 786 * xpsgtr_set_txwidth - This function sets the tx bus width of the lane
 787 * @gtr_phy: pointer to lane
 788 * @width: tx bus width size
 789 */
 790static void xpsgtr_set_txwidth(struct xpsgtr_phy *gtr_phy, u32 width)
 791{
 792        struct xpsgtr_dev *gtr_dev = gtr_phy->data;
 793
 794        writel(gtr_phy->lane * PROT_BUS_WIDTH_SHIFT >> width,
 795                                        gtr_dev->serdes + TX_PROT_BUS_WIDTH);
 796}
 797
 798/**
 799 * xpsgtr_set_rxwidth - This function sets the rx bus width of the lane
 800 * @gtr_phy: pointer to lane
 801 * @width: rx bus width size
 802 */
 803static void xpsgtr_set_rxwidth(struct xpsgtr_phy *gtr_phy, u32 width)
 804{
 805        struct xpsgtr_dev *gtr_dev = gtr_phy->data;
 806
 807        writel(gtr_phy->lane * PROT_BUS_WIDTH_SHIFT >> width,
 808                                        gtr_dev->serdes + RX_PROT_BUS_WIDTH);
 809}
 810
 811/**
 812 * xpsgtr_bypass_scramenc - This bypasses scrambler and 8b/10b encoder feature
 813 * @gtr_phy: pointer to lane
 814 */
 815static void xpsgtr_bypass_scramenc(struct xpsgtr_phy *gtr_phy)
 816{
 817        u32 offset;
 818        struct xpsgtr_dev *gtr_dev = gtr_phy->data;
 819
 820        /* bypass Scrambler and 8b/10b Encoder */
 821        offset = gtr_phy->lane * TX_DIG_61_OFFSET + L0_TX_DIG_61;
 822        writel(TM_DISABLE_SCRAMBLE_ENCODER, gtr_dev->serdes + offset);
 823}
 824
 825/**
 826 * xpsgtr_bypass_descramdec - bypasses descrambler and 8b/10b encoder feature
 827 * @gtr_phy: pointer to lane
 828 */
 829static void xpsgtr_bypass_descramdec(struct xpsgtr_phy *gtr_phy)
 830{
 831        u32 offset;
 832        struct xpsgtr_dev *gtr_dev = gtr_phy->data;
 833
 834        /* bypass Descrambler and 8b/10b decoder */
 835        offset = gtr_phy->lane * TM_DIG_6_OFFSET + L0_TM_DIG_6;
 836        writel(TM_DISABLE_DESCRAMBLE_DECODER, gtr_dev->serdes + offset);
 837}
 838
 839/**
 840 * xpsgtr_misc_sgmii - miscellaneous settings for SGMII
 841 * @gtr_phy: pointer to lane
 842 */
 843static void xpsgtr_misc_sgmii(struct xpsgtr_phy *gtr_phy)
 844{
 845        /* Set SGMII protocol tx bus width 10 bits */
 846        xpsgtr_set_txwidth(gtr_phy, PROT_BUS_WIDTH_10);
 847
 848        /* Set SGMII protocol rx bus width 10 bits */
 849        xpsgtr_set_rxwidth(gtr_phy, PROT_BUS_WIDTH_10);
 850
 851        /* bypass Descrambler and 8b/10b decoder */
 852        xpsgtr_bypass_descramdec(gtr_phy);
 853
 854        /* bypass Scrambler and 8b/10b Encoder */
 855        xpsgtr_bypass_scramenc(gtr_phy);
 856}
 857
 858/**
 859 * xpsgtr_misc_sata - miscellaneous settings for SATA
 860 * @gtr_phy: pointer to lane
 861 */
 862static void xpsgtr_misc_sata(struct xpsgtr_phy *gtr_phy)
 863{
 864        struct xpsgtr_dev *gtr_dev = gtr_phy->data;
 865
 866        /* bypass Descrambler and 8b/10b decoder */
 867        xpsgtr_bypass_descramdec(gtr_phy);
 868
 869        /* bypass Scrambler and 8b/10b Encoder */
 870        xpsgtr_bypass_scramenc(gtr_phy);
 871
 872        writel(gtr_phy->lane, gtr_dev->siou + SATA_CONTROL_OFFSET);
 873}
 874
 875
 876/**
 877 * xpsgtr_ulpi_reset - This function does ULPI reset.
 878 * @gtr_phy: pointer to lane
 879 */
 880static void xpsgtr_ulpi_reset(struct xpsgtr_phy *gtr_phy)
 881{
 882        struct xpsgtr_dev *gtr_dev = gtr_phy->data;
 883        unsigned long loop_time = msecs_to_jiffies(RST_ULPI_TIMEOUT);
 884        unsigned long timeout;
 885
 886        writel(RST_ULPI_HI, gtr_dev->lpd + RST_ULPI);
 887
 888        /* wait for some time */
 889        timeout = jiffies + loop_time;
 890        do {
 891                cpu_relax();
 892        } while (!time_after_eq(jiffies, timeout));
 893
 894        writel(RST_ULPI_LOW, gtr_dev->lpd + RST_ULPI);
 895
 896        /* wait for some time */
 897        timeout = jiffies + loop_time;
 898        do {
 899                cpu_relax();
 900        } while (!time_after_eq(jiffies, timeout));
 901
 902        writel(RST_ULPI_HI, gtr_dev->lpd + RST_ULPI);
 903
 904}
 905
 906/**
 907 * xpsgtr_set_sgmii_pcs - This function sets the sgmii mode for GEM.
 908 * @gtr_phy: pointer to lane
 909 *
 910 * Return: 0 on success, -EINVAL on non existing SGMII type or error from
 911 * communication with firmware
 912 */
 913static int xpsgtr_set_sgmii_pcs(struct xpsgtr_phy *gtr_phy)
 914{
 915        u32 shift, mask, value;
 916        int ret = 0;
 917        struct xpsgtr_dev *gtr_dev = gtr_phy->data;
 918
 919        /* Set the PCS signal detect to 1 */
 920        switch (gtr_phy->type) {
 921        case XPSGTR_TYPE_SGMII0:
 922                shift = 0;
 923                break;
 924        case XPSGTR_TYPE_SGMII1:
 925                shift = 1;
 926                break;
 927        case XPSGTR_TYPE_SGMII2:
 928                shift = 2;
 929                break;
 930        case XPSGTR_TYPE_SGMII3:
 931                shift = 3;
 932                break;
 933        default:
 934                return -EINVAL;
 935        }
 936
 937        /* Tie the GEM PCS Signal Detect to 1 */
 938        mask = SGMII_SD_MASK << SGMII_SD_OFFSET * shift;
 939        value = SGMII_PCS_SD_1 << SGMII_SD_OFFSET * shift;
 940        ret = zynqmp_pm_mmio_write(IOU_SLCR + IOU_GEM_CTRL_OFFSET, mask, value);
 941        if (ret < 0) {
 942                dev_err(gtr_dev->dev, "failed to set GEM PCS SD\n");
 943                return ret;
 944        }
 945
 946        /* Set the GEM to SGMII mode */
 947        mask = GEM_CLK_CTRL_MASK << GEM_CLK_CTRL_OFFSET * shift;
 948        value = GEM_RX_SRC_SEL_GTR | GEM_SGMII_MODE;
 949        ret = zynqmp_pm_mmio_write(IOU_SLCR + IOU_GEM_CLK_CTRL_OFFSET,
 950                                                                mask, value);
 951        if (ret < 0) {
 952                dev_err(gtr_dev->dev, "failed to set GEM to SGMII mode\n");
 953                return ret;
 954        }
 955
 956        return ret;
 957}
 958
 959/**
 960 * xpsgtr_phy_init - initializes a lane
 961 * @phy: pointer to kernel PHY device
 962 *
 963 * Return: 0 on success or error on failure
 964 */
 965static int xpsgtr_phy_init(struct phy *phy)
 966{
 967        struct xpsgtr_phy *gtr_phy = phy_get_drvdata(phy);
 968        struct xpsgtr_dev *gtr_dev = gtr_phy->data;
 969        int ret = 0;
 970        u32 offset;
 971        u32 reg;
 972        u32 nsw;
 973        u32 timeout = 500;
 974
 975        mutex_lock(&gtr_dev->gtr_mutex);
 976
 977        /* Put controller in reset */
 978        ret = xpsgtr_controller_reset(gtr_phy);
 979        if (ret != 0) {
 980                dev_err(gtr_dev->dev, "Failed to assert reset\n");
 981                goto out;
 982        }
 983
 984        /*
 985         * There is a functional issue in the GT. The TX termination resistance
 986         * can be out of spec due to a bug in the calibration logic. Below is
 987         * the workaround to fix it. This below is required for XCZU9EG silicon.
 988         */
 989        if (gtr_dev->tx_term_fix) {
 990
 991                /* Enabling Test Mode control for CMN Rest */
 992                reg = readl(gtr_dev->serdes + TM_CMN_RST);
 993                reg = (reg & ~TM_CMN_RST_MASK) | TM_CMN_RST_SET;
 994                writel(reg, gtr_dev->serdes + TM_CMN_RST);
 995
 996                /* Set Test Mode reset */
 997                reg = readl(gtr_dev->serdes + TM_CMN_RST);
 998                reg = (reg & ~TM_CMN_RST_MASK) | TM_CMN_RST_EN;
 999                writel(reg, gtr_dev->serdes + TM_CMN_RST);
1000
1001                writel(0x00, gtr_dev->serdes + L3_TM_CALIB_DIG18);
1002                writel(TM_OVERRIDE_NSW_CODE, gtr_dev->serdes +
1003                                L3_TM_CALIB_DIG19);
1004
1005                /* As a part of work around sequence for PMOS calibration fix,
1006                 * we need to configure any lane ICM_CFG to valid protocol. This
1007                 * will deassert the CMN_Resetn signal.
1008                 */
1009                xpsgtr_lane_setprotocol(gtr_phy);
1010
1011                /* Clear Test Mode reset */
1012                reg = readl(gtr_dev->serdes + TM_CMN_RST);
1013                reg = (reg & ~TM_CMN_RST_MASK) | TM_CMN_RST_SET;
1014                writel(reg, gtr_dev->serdes + TM_CMN_RST);
1015
1016                dev_dbg(gtr_dev->dev, "calibrating...\n");
1017
1018                do {
1019                        reg = readl(gtr_dev->serdes + L3_CALIB_DONE_STATUS);
1020                        if ((reg & CALIB_DONE) == CALIB_DONE)
1021                                break;
1022
1023                        if (!--timeout) {
1024                                dev_err(gtr_dev->dev, "calibration time out\n");
1025                                ret = -ETIMEDOUT;
1026                                goto out;
1027                        }
1028                        udelay(1);
1029                } while (1);
1030
1031                dev_dbg(gtr_dev->dev, "calibration done\n");
1032
1033                /* Reading NMOS Register Code */
1034                nsw = readl(gtr_dev->serdes + L0_TXPMA_ST_3);
1035
1036                /* Set Test Mode reset */
1037                reg = readl(gtr_dev->serdes + TM_CMN_RST);
1038                reg = (reg & ~TM_CMN_RST_MASK) | TM_CMN_RST_EN;
1039                writel(reg, gtr_dev->serdes + TM_CMN_RST);
1040
1041                nsw = nsw & DN_CALIB_CODE;
1042
1043                /* Writing NMOS register values back [5:3] */
1044                reg = nsw >> DN_CALIB_SHIFT;
1045                writel(reg, gtr_dev->serdes + L3_TM_CALIB_DIG19);
1046
1047                /* Writing NMOS register value [2:0] */
1048                reg = ((nsw & 0x7) << NSW_SHIFT) | (1 << NSW_PIPE_SHIFT);
1049                writel(reg, gtr_dev->serdes + L3_TM_CALIB_DIG18);
1050
1051                /* Clear Test Mode reset */
1052                reg = readl(gtr_dev->serdes + TM_CMN_RST);
1053                reg = (reg & ~TM_CMN_RST_MASK) | TM_CMN_RST_SET;
1054                writel(reg, gtr_dev->serdes + TM_CMN_RST);
1055
1056                gtr_dev->tx_term_fix = false;
1057        }
1058
1059        /* Enable coarse code saturation limiting logic */
1060        offset = gtr_phy->lane * TM_PLL_DIG_37_OFFSET + L0_TM_PLL_DIG_37;
1061        writel(TM_COARSE_CODE_LIMIT, gtr_dev->serdes + offset);
1062
1063        xpsgtr_configure_pll(gtr_phy);
1064        xpsgtr_lane_setprotocol(gtr_phy);
1065
1066        if (gtr_phy->protocol == ICM_PROTOCOL_SATA)
1067                xpsgtr_misc_sata(gtr_phy);
1068
1069        if (gtr_phy->protocol == ICM_PROTOCOL_SGMII)
1070                xpsgtr_misc_sgmii(gtr_phy);
1071
1072        /* Bring controller out of reset */
1073        ret = xpsgtr_controller_release_reset(gtr_phy);
1074        if (ret != 0) {
1075                dev_err(gtr_dev->dev, "Failed to release reset\n");
1076                goto out;
1077        }
1078
1079        /* Wait till pll is locked for all protocols except DP. For DP
1080         * pll locking function will be called from driver.
1081         */
1082        if (gtr_phy->protocol != ICM_PROTOCOL_DP) {
1083                ret = xpsgtr_wait_pll_lock(phy);
1084                if (ret != 0)
1085                        goto out;
1086        } else {
1087                offset = gtr_phy->lane * TXPMD_TM_45_OFFSET + L0_TXPMD_TM_45;
1088                reg = L0_TXPMD_TM_45_OVER_DP_MAIN |
1089                      L0_TXPMD_TM_45_ENABLE_DP_MAIN |
1090                      L0_TXPMD_TM_45_OVER_DP_POST1 |
1091                      L0_TXPMD_TM_45_OVER_DP_POST2 |
1092                      L0_TXPMD_TM_45_ENABLE_DP_POST2;
1093                writel(reg, gtr_dev->serdes + offset);
1094                offset = gtr_phy->lane * TX_ANA_TM_118_OFFSET +
1095                         L0_TX_ANA_TM_118;
1096                writel(L0_TX_ANA_TM_118_FORCE_17_0,
1097                       gtr_dev->serdes + offset);
1098        }
1099
1100        /* Do ULPI reset for usb */
1101        if (gtr_phy->protocol == ICM_PROTOCOL_USB)
1102                xpsgtr_ulpi_reset(gtr_phy);
1103
1104        /* Select SGMII Mode for GEM and set the PCS Signal detect*/
1105        if (gtr_phy->protocol == ICM_PROTOCOL_SGMII)
1106                ret = xpsgtr_set_sgmii_pcs(gtr_phy);
1107out:
1108        mutex_unlock(&gtr_dev->gtr_mutex);
1109        return ret;
1110}
1111
1112/**
1113 * xpsgtr_set_lanetype - derives lane type from dts arguments
1114 * @gtr_phy: pointer to lane
1115 * @controller: type of controller
1116 * @instance_num: instance number of the controller in case multilane controller
1117 *
1118 * Return: 0 on success or error on failure
1119 */
1120static int xpsgtr_set_lanetype(struct xpsgtr_phy *gtr_phy, u8 controller,
1121                                u8 instance_num)
1122{
1123        switch (controller) {
1124        case PHY_TYPE_SATA:
1125                if (instance_num == 0)
1126                        gtr_phy->type = XPSGTR_TYPE_SATA_0;
1127                else if (instance_num == 1)
1128                        gtr_phy->type = XPSGTR_TYPE_SATA_1;
1129                else
1130                        return -EINVAL;
1131                break;
1132        case PHY_TYPE_USB3:
1133                if (instance_num == 0)
1134                        gtr_phy->type = XPSGTR_TYPE_USB0;
1135                else if (instance_num == 1)
1136                        gtr_phy->type = XPSGTR_TYPE_USB1;
1137                else
1138                        return -EINVAL;
1139                break;
1140        case PHY_TYPE_DP:
1141                if (instance_num == 0)
1142                        gtr_phy->type = XPSGTR_TYPE_DP_0;
1143                else if (instance_num == 1)
1144                        gtr_phy->type = XPSGTR_TYPE_DP_1;
1145                else
1146                        return -EINVAL;
1147                break;
1148        case PHY_TYPE_PCIE:
1149                if (instance_num == 0)
1150                        gtr_phy->type = XPSGTR_TYPE_PCIE_0;
1151                else if (instance_num == 1)
1152                        gtr_phy->type = XPSGTR_TYPE_PCIE_1;
1153                else if (instance_num == 2)
1154                        gtr_phy->type = XPSGTR_TYPE_PCIE_2;
1155                else if (instance_num == 3)
1156                        gtr_phy->type = XPSGTR_TYPE_PCIE_3;
1157                else
1158                        return -EINVAL;
1159                break;
1160        case PHY_TYPE_SGMII:
1161                if (instance_num == 0)
1162                        gtr_phy->type = XPSGTR_TYPE_SGMII0;
1163                else if (instance_num == 1)
1164                        gtr_phy->type = XPSGTR_TYPE_SGMII1;
1165                else if (instance_num == 2)
1166                        gtr_phy->type = XPSGTR_TYPE_SGMII2;
1167                else if (instance_num == 3)
1168                        gtr_phy->type = XPSGTR_TYPE_SGMII3;
1169                else
1170                        return -EINVAL;
1171                break;
1172        default:
1173                return -EINVAL;
1174        }
1175
1176        return 0;
1177}
1178
1179/**
1180 * xpsgtr_xlate - provides a PHY specific to a controller
1181 * @dev: pointer to device
1182 * @args: arguments from dts
1183 *
1184 * Return: pointer to kernel PHY device or error on failure
1185 */
1186static struct phy *xpsgtr_xlate(struct device *dev,
1187                                   struct of_phandle_args *args)
1188{
1189        struct xpsgtr_dev *gtr_dev = dev_get_drvdata(dev);
1190        struct xpsgtr_phy *gtr_phy = NULL;
1191        struct device_node *phynode = args->np;
1192        int index;
1193        int i;
1194        u8 controller;
1195        u8 instance_num;
1196
1197        if (args->args_count != 4) {
1198                dev_err(dev, "Invalid number of cells in 'phy' property\n");
1199                return ERR_PTR(-EINVAL);
1200        }
1201        if (!of_device_is_available(phynode)) {
1202                dev_warn(dev, "requested PHY is disabled\n");
1203                return ERR_PTR(-ENODEV);
1204        }
1205        for (index = 0; index < of_get_child_count(dev->of_node); index++) {
1206                if (phynode == gtr_dev->phys[index]->phy->dev.of_node) {
1207                        gtr_phy = gtr_dev->phys[index];
1208                        break;
1209                }
1210        }
1211        if (!gtr_phy) {
1212                dev_err(dev, "failed to find appropriate phy\n");
1213                return ERR_PTR(-EINVAL);
1214        }
1215
1216        /* get type of controller from phys */
1217        controller = args->args[0];
1218
1219        /* get controller instance number */
1220        instance_num = args->args[1];
1221
1222        /* Check if lane sharing is required */
1223        gtr_phy->share_laneclk = args->args[2];
1224
1225        /* get the required clk rate for controller from phys */
1226        gtr_phy->refclk_rate = args->args[3];
1227
1228        /* derive lane type */
1229        if (xpsgtr_set_lanetype(gtr_phy, controller, instance_num) < 0) {
1230                dev_err(gtr_dev->dev, "Invalid lane type\n");
1231                return ERR_PTR(-EINVAL);
1232        }
1233
1234        /* configures SSC settings for a lane */
1235        if (xpsgtr_configure_lane(gtr_phy) < 0) {
1236                dev_err(gtr_dev->dev, "Invalid clock rate: %d\n",
1237                                                gtr_phy->refclk_rate);
1238                return ERR_PTR(-EINVAL);
1239        }
1240
1241        /*
1242         * Check Interconnect Matrix is obeyed i.e, given lane type
1243         * is allowed to operate on the lane.
1244         */
1245        for (i = 0; i < CONTROLLERS_PER_LANE; i++) {
1246                if (icm_matrix[index][i] == gtr_phy->type)
1247                        return gtr_phy->phy;
1248        }
1249
1250        /* Should not reach here */
1251        return ERR_PTR(-EINVAL);
1252}
1253
1254static struct phy_ops xpsgtr_phyops = {
1255        .init           = xpsgtr_phy_init,
1256        .owner          = THIS_MODULE,
1257};
1258
1259/*
1260 * xpsgtr_get_resets - Gets reset signals based on reset-names property
1261 * @gtr_dev: pointer to structure which stores reset information
1262 *
1263 * Return: 0 on success or error value on failure
1264 */
1265static int xpsgtr_get_resets(struct xpsgtr_dev *gtr_dev)
1266{
1267        char *name;
1268        struct reset_control *rst_temp;
1269
1270        gtr_dev->sata_rst = devm_reset_control_get(gtr_dev->dev, "sata_rst");
1271        if (IS_ERR(gtr_dev->sata_rst)) {
1272                name = "sata_rst";
1273                rst_temp = gtr_dev->sata_rst;
1274                goto error;
1275        }
1276
1277        gtr_dev->dp_rst = devm_reset_control_get(gtr_dev->dev, "dp_rst");
1278        if (IS_ERR(gtr_dev->dp_rst)) {
1279                name = "dp_rst";
1280                rst_temp = gtr_dev->dp_rst;
1281                goto error;
1282        }
1283
1284        gtr_dev->usb0_crst = devm_reset_control_get(gtr_dev->dev, "usb0_crst");
1285        if (IS_ERR(gtr_dev->usb0_crst)) {
1286                name = "usb0_crst";
1287                rst_temp = gtr_dev->usb0_crst;
1288                goto error;
1289        }
1290
1291        gtr_dev->usb1_crst = devm_reset_control_get(gtr_dev->dev, "usb1_crst");
1292        if (IS_ERR(gtr_dev->usb1_crst)) {
1293                name = "usb1_crst";
1294                rst_temp = gtr_dev->usb1_crst;
1295                goto error;
1296        }
1297
1298        gtr_dev->usb0_hibrst = devm_reset_control_get(gtr_dev->dev,
1299                                                        "usb0_hibrst");
1300        if (IS_ERR(gtr_dev->usb0_hibrst)) {
1301                name = "usb0_hibrst";
1302                rst_temp = gtr_dev->usb0_hibrst;
1303                goto error;
1304        }
1305
1306        gtr_dev->usb1_hibrst = devm_reset_control_get(gtr_dev->dev,
1307                                                        "usb1_hibrst");
1308        if (IS_ERR(gtr_dev->usb1_hibrst)) {
1309                name = "usb1_hibrst";
1310                rst_temp = gtr_dev->usb1_hibrst;
1311                goto error;
1312        }
1313
1314        gtr_dev->usb0_apbrst = devm_reset_control_get(gtr_dev->dev,
1315                                                        "usb0_apbrst");
1316        if (IS_ERR(gtr_dev->usb0_apbrst)) {
1317                name = "usb0_apbrst";
1318                rst_temp = gtr_dev->usb0_apbrst;
1319                goto error;
1320        }
1321
1322        gtr_dev->usb1_apbrst = devm_reset_control_get(gtr_dev->dev,
1323                                                        "usb1_apbrst");
1324        if (IS_ERR(gtr_dev->usb1_apbrst)) {
1325                name = "usb1_apbrst";
1326                rst_temp = gtr_dev->usb1_apbrst;
1327                goto error;
1328        }
1329
1330        gtr_dev->gem0_rst = devm_reset_control_get(gtr_dev->dev, "gem0_rst");
1331        if (IS_ERR(gtr_dev->gem0_rst)) {
1332                name = "gem0_rst";
1333                rst_temp = gtr_dev->gem0_rst;
1334                goto error;
1335        }
1336
1337        gtr_dev->gem1_rst = devm_reset_control_get(gtr_dev->dev, "gem1_rst");
1338        if (IS_ERR(gtr_dev->gem1_rst)) {
1339                name = "gem1_rst";
1340                rst_temp = gtr_dev->gem1_rst;
1341                goto error;
1342        }
1343
1344        gtr_dev->gem2_rst = devm_reset_control_get(gtr_dev->dev, "gem2_rst");
1345        if (IS_ERR(gtr_dev->gem2_rst)) {
1346                name = "gem2_rst";
1347                rst_temp = gtr_dev->gem2_rst;
1348                goto error;
1349        }
1350
1351        gtr_dev->gem3_rst = devm_reset_control_get(gtr_dev->dev, "gem3_rst");
1352        if (IS_ERR(gtr_dev->gem3_rst)) {
1353                name = "gem3_rst";
1354                rst_temp = gtr_dev->gem3_rst;
1355                goto error;
1356        }
1357
1358        return 0;
1359error:
1360        dev_err(gtr_dev->dev, "failed to get %s reset signal\n", name);
1361        return PTR_ERR(rst_temp);
1362}
1363
1364/**
1365 * xpsgtr_probe - The device probe function for driver initialization.
1366 * @pdev: pointer to the platform device structure.
1367 *
1368 * Return: 0 for success and error value on failure
1369 */
1370static int xpsgtr_probe(struct platform_device *pdev)
1371{
1372        struct device_node *child, *np = pdev->dev.of_node;
1373        struct xpsgtr_dev *gtr_dev;
1374        struct phy_provider *provider;
1375        struct phy *phy;
1376        struct resource *res;
1377        char *soc_rev;
1378        int lanecount, port = 0, index = 0;
1379        int err;
1380
1381        gtr_dev = devm_kzalloc(&pdev->dev, sizeof(*gtr_dev), GFP_KERNEL);
1382        if (!gtr_dev)
1383                return -ENOMEM;
1384
1385        res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "serdes");
1386        gtr_dev->serdes = devm_ioremap_resource(&pdev->dev, res);
1387        if (IS_ERR(gtr_dev->serdes))
1388                return PTR_ERR(gtr_dev->serdes);
1389
1390        res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "siou");
1391        gtr_dev->siou = devm_ioremap_resource(&pdev->dev, res);
1392        if (IS_ERR(gtr_dev->siou))
1393                return PTR_ERR(gtr_dev->siou);
1394
1395        res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lpd");
1396        gtr_dev->lpd = devm_ioremap_resource(&pdev->dev, res);
1397        if (IS_ERR(gtr_dev->lpd))
1398                return PTR_ERR(gtr_dev->lpd);
1399
1400        lanecount = of_get_child_count(np);
1401        if (lanecount > MAX_LANES || lanecount == 0)
1402                return -EINVAL;
1403
1404        gtr_dev->phys = devm_kzalloc(&pdev->dev, sizeof(phy) * lanecount,
1405                                       GFP_KERNEL);
1406        if (!gtr_dev->phys)
1407                return -ENOMEM;
1408
1409        gtr_dev->dev = &pdev->dev;
1410        platform_set_drvdata(pdev, gtr_dev);
1411        mutex_init(&gtr_dev->gtr_mutex);
1412
1413        /* Deferred probe is also handled if nvmem is not ready */
1414        soc_rev = zynqmp_nvmem_get_silicon_version(&pdev->dev,
1415                                                   "soc_revision");
1416        if (IS_ERR(soc_rev))
1417                return PTR_ERR(soc_rev);
1418
1419        if (*soc_rev == ZYNQMP_SILICON_V1)
1420                gtr_dev->tx_term_fix = true;
1421
1422        kfree(soc_rev);
1423
1424        err = xpsgtr_get_resets(gtr_dev);
1425        if (err) {
1426                dev_err(&pdev->dev, "failed to get resets: %d\n", err);
1427                return err;
1428        }
1429
1430        for_each_child_of_node(np, child) {
1431                struct xpsgtr_phy *gtr_phy;
1432
1433                gtr_phy = devm_kzalloc(&pdev->dev, sizeof(*gtr_phy),
1434                                         GFP_KERNEL);
1435                if (!gtr_phy)
1436                        return -ENOMEM;
1437
1438                /* Assign lane number to gtr_phy instance */
1439                gtr_phy->lane = index;
1440
1441                /* Disable lane sharing as default */
1442                gtr_phy->share_laneclk = -1;
1443
1444                gtr_dev->phys[port] = gtr_phy;
1445                phy = devm_phy_create(&pdev->dev, child, &xpsgtr_phyops);
1446                if (IS_ERR(phy)) {
1447                        dev_err(&pdev->dev, "failed to create PHY\n");
1448                        return PTR_ERR(phy);
1449                }
1450                gtr_dev->phys[port]->phy = phy;
1451                phy_set_drvdata(phy, gtr_dev->phys[port]);
1452                gtr_phy->data = gtr_dev;
1453                port++;
1454                index++;
1455        }
1456        provider = devm_of_phy_provider_register(&pdev->dev, xpsgtr_xlate);
1457        if (IS_ERR(provider)) {
1458                dev_err(&pdev->dev, "registering provider failed\n");
1459                        return PTR_ERR(provider);
1460        }
1461        return 0;
1462}
1463
1464/* Match table for of_platform binding */
1465static const struct of_device_id xpsgtr_of_match[] = {
1466        { .compatible = "xlnx,zynqmp-psgtr", },
1467        {},
1468};
1469MODULE_DEVICE_TABLE(of, xpsgtr_of_match);
1470
1471static struct platform_driver xpsgtr_driver = {
1472        .probe = xpsgtr_probe,
1473        .driver = {
1474                .name = "xilinx-psgtr",
1475                .of_match_table = xpsgtr_of_match,
1476        },
1477};
1478
1479module_platform_driver(xpsgtr_driver);
1480
1481MODULE_AUTHOR("Xilinx Inc.");
1482MODULE_LICENSE("GPL v2");
1483MODULE_DESCRIPTION("Xilinx ZynqMP High speed Gigabit Transceiver");
1484