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