Michal Simek | 0be35ab | 2022-01-06 09:49:41 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * phy-zynqmp.c - PHY driver for Xilinx ZynqMP GT. |
| 4 | * |
| 5 | * Copyright (C) 2018-2021 Xilinx Inc. |
| 6 | * |
| 7 | * Author: Anurag Kumar Vulisha <anuragku@xilinx.com> |
| 8 | * Author: Subbaraya Sundeep <sundeep.lkml@gmail.com> |
| 9 | * Author: Laurent Pinchart <laurent.pinchart@ideasonboard.com> |
| 10 | */ |
| 11 | |
Michal Simek | 0be35ab | 2022-01-06 09:49:41 +0100 | [diff] [blame] | 12 | #include <clk-uclass.h> |
| 13 | #include <dm.h> |
| 14 | #include <generic-phy.h> |
| 15 | #include <log.h> |
| 16 | #include <power-domain.h> |
| 17 | #include <regmap.h> |
| 18 | #include <syscon.h> |
| 19 | #include <asm/io.h> |
| 20 | #include <asm/arch/sys_proto.h> |
| 21 | #include <asm/arch/hardware.h> |
| 22 | #include <dm/device.h> |
| 23 | #include <dm/device_compat.h> |
| 24 | #include <dm/lists.h> |
| 25 | #include <dt-bindings/phy/phy.h> |
| 26 | #include <linux/bitops.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/err.h> |
| 29 | |
| 30 | /* |
| 31 | * Lane Registers |
| 32 | */ |
| 33 | |
| 34 | /* TX De-emphasis parameters */ |
| 35 | #define L0_TX_ANA_TM_18 0x0048 |
| 36 | #define L0_TX_ANA_TM_118 0x01d8 |
| 37 | #define L0_TX_ANA_TM_118_FORCE_17_0 BIT(0) |
| 38 | |
| 39 | /* DN Resistor calibration code parameters */ |
| 40 | #define L0_TXPMA_ST_3 0x0b0c |
| 41 | #define L0_DN_CALIB_CODE 0x3f |
| 42 | |
| 43 | /* PMA control parameters */ |
| 44 | #define L0_TXPMD_TM_45 0x0cb4 |
| 45 | #define L0_TXPMD_TM_48 0x0cc0 |
| 46 | #define L0_TXPMD_TM_45_OVER_DP_MAIN BIT(0) |
| 47 | #define L0_TXPMD_TM_45_ENABLE_DP_MAIN BIT(1) |
| 48 | #define L0_TXPMD_TM_45_OVER_DP_POST1 BIT(2) |
| 49 | #define L0_TXPMD_TM_45_ENABLE_DP_POST1 BIT(3) |
| 50 | #define L0_TXPMD_TM_45_OVER_DP_POST2 BIT(4) |
| 51 | #define L0_TXPMD_TM_45_ENABLE_DP_POST2 BIT(5) |
| 52 | |
| 53 | /* PCS control parameters */ |
| 54 | #define L0_TM_DIG_6 0x106c |
| 55 | #define L0_TM_DIS_DESCRAMBLE_DECODER 0x0f |
| 56 | #define L0_TX_DIG_61 0x00f4 |
| 57 | #define L0_TM_DISABLE_SCRAMBLE_ENCODER 0x0f |
| 58 | |
| 59 | /* PLL Test Mode register parameters */ |
| 60 | #define L0_TM_PLL_DIG_37 0x2094 |
| 61 | #define L0_TM_COARSE_CODE_LIMIT 0x10 |
| 62 | |
| 63 | /* PLL SSC step size offsets */ |
| 64 | #define L0_PLL_SS_STEPS_0_LSB 0x2368 |
| 65 | #define L0_PLL_SS_STEPS_1_MSB 0x236c |
| 66 | #define L0_PLL_SS_STEP_SIZE_0_LSB 0x2370 |
| 67 | #define L0_PLL_SS_STEP_SIZE_1 0x2374 |
| 68 | #define L0_PLL_SS_STEP_SIZE_2 0x2378 |
| 69 | #define L0_PLL_SS_STEP_SIZE_3_MSB 0x237c |
| 70 | #define L0_PLL_STATUS_READ_1 0x23e4 |
| 71 | |
| 72 | /* SSC step size parameters */ |
| 73 | #define STEP_SIZE_0_MASK 0xff |
| 74 | #define STEP_SIZE_1_MASK 0xff |
| 75 | #define STEP_SIZE_2_MASK 0xff |
| 76 | #define STEP_SIZE_3_MASK 0x3 |
| 77 | #define STEP_SIZE_SHIFT 8 |
| 78 | #define FORCE_STEP_SIZE 0x10 |
| 79 | #define FORCE_STEPS 0x20 |
| 80 | #define STEPS_0_MASK 0xff |
| 81 | #define STEPS_1_MASK 0x07 |
| 82 | |
| 83 | /* Reference clock selection parameters */ |
| 84 | #define L0_Ln_REF_CLK_SEL(n) (0x2860 + (n) * 4) |
| 85 | #define L0_REF_CLK_SEL_MASK 0x8f |
| 86 | |
| 87 | /* Calibration digital logic parameters */ |
| 88 | #define L3_TM_CALIB_DIG19 0xec4c |
| 89 | #define L3_CALIB_DONE_STATUS 0xef14 |
| 90 | #define L3_TM_CALIB_DIG18 0xec48 |
| 91 | #define L3_TM_CALIB_DIG19_NSW 0x07 |
| 92 | #define L3_TM_CALIB_DIG18_NSW 0xe0 |
| 93 | #define L3_TM_OVERRIDE_NSW_CODE 0x20 |
| 94 | #define L3_CALIB_DONE 0x02 |
| 95 | #define L3_NSW_SHIFT 5 |
| 96 | #define L3_NSW_PIPE_SHIFT 4 |
| 97 | #define L3_NSW_CALIB_SHIFT 3 |
| 98 | |
| 99 | #define PHY_REG_OFFSET 0x4000 |
| 100 | |
| 101 | /* |
| 102 | * Global Registers |
| 103 | */ |
| 104 | |
| 105 | /* Refclk selection parameters */ |
| 106 | #define PLL_REF_SEL(n) (0x10000 + (n) * 4) |
| 107 | #define PLL_FREQ_MASK 0x1f |
| 108 | #define PLL_STATUS_LOCKED 0x10 |
| 109 | |
| 110 | /* Inter Connect Matrix parameters */ |
| 111 | #define ICM_CFG0 0x10010 |
| 112 | #define ICM_CFG1 0x10014 |
| 113 | #define ICM_CFG0_L0_MASK 0x07 |
| 114 | #define ICM_CFG0_L1_MASK 0x70 |
| 115 | #define ICM_CFG1_L2_MASK 0x07 |
| 116 | #define ICM_CFG2_L3_MASK 0x70 |
| 117 | #define ICM_CFG_SHIFT 4 |
| 118 | |
| 119 | /* Inter Connect Matrix allowed protocols */ |
| 120 | #define ICM_PROTOCOL_PD 0x0 |
| 121 | #define ICM_PROTOCOL_PCIE 0x1 |
| 122 | #define ICM_PROTOCOL_SATA 0x2 |
| 123 | #define ICM_PROTOCOL_USB 0x3 |
| 124 | #define ICM_PROTOCOL_DP 0x4 |
| 125 | #define ICM_PROTOCOL_SGMII 0x5 |
| 126 | |
| 127 | /* Test Mode common reset control parameters */ |
| 128 | #define TM_CMN_RST 0x10018 |
| 129 | #define TM_CMN_RST_EN 0x1 |
| 130 | #define TM_CMN_RST_SET 0x2 |
| 131 | #define TM_CMN_RST_MASK 0x3 |
| 132 | |
| 133 | /* Bus width parameters */ |
| 134 | #define TX_PROT_BUS_WIDTH 0x10040 |
| 135 | #define RX_PROT_BUS_WIDTH 0x10044 |
| 136 | #define PROT_BUS_WIDTH_10 0x0 |
| 137 | #define PROT_BUS_WIDTH_20 0x1 |
| 138 | #define PROT_BUS_WIDTH_40 0x2 |
| 139 | #define PROT_BUS_WIDTH_MASK 0x3 |
| 140 | #define PROT_BUS_WIDTH_SHIFT 2 |
| 141 | |
| 142 | /* Number of GT lanes */ |
| 143 | #define NUM_LANES 4 |
| 144 | |
| 145 | /* SIOU SATA control register */ |
| 146 | #define SATA_CONTROL_OFFSET 0x0100 |
| 147 | |
| 148 | /* Total number of controllers */ |
| 149 | #define CONTROLLERS_PER_LANE 5 |
| 150 | |
| 151 | /* Protocol Type parameters */ |
| 152 | enum { |
| 153 | XPSGTR_TYPE_USB0 = 0, /* USB controller 0 */ |
| 154 | XPSGTR_TYPE_USB1 = 1, /* USB controller 1 */ |
| 155 | XPSGTR_TYPE_SATA_0 = 2, /* SATA controller lane 0 */ |
| 156 | XPSGTR_TYPE_SATA_1 = 3, /* SATA controller lane 1 */ |
| 157 | XPSGTR_TYPE_PCIE_0 = 4, /* PCIe controller lane 0 */ |
| 158 | XPSGTR_TYPE_PCIE_1 = 5, /* PCIe controller lane 1 */ |
| 159 | XPSGTR_TYPE_PCIE_2 = 6, /* PCIe controller lane 2 */ |
| 160 | XPSGTR_TYPE_PCIE_3 = 7, /* PCIe controller lane 3 */ |
| 161 | XPSGTR_TYPE_DP_0 = 8, /* Display Port controller lane 0 */ |
| 162 | XPSGTR_TYPE_DP_1 = 9, /* Display Port controller lane 1 */ |
| 163 | XPSGTR_TYPE_SGMII0 = 10, /* Ethernet SGMII controller 0 */ |
| 164 | XPSGTR_TYPE_SGMII1 = 11, /* Ethernet SGMII controller 1 */ |
| 165 | XPSGTR_TYPE_SGMII2 = 12, /* Ethernet SGMII controller 2 */ |
| 166 | XPSGTR_TYPE_SGMII3 = 13, /* Ethernet SGMII controller 3 */ |
| 167 | }; |
| 168 | |
| 169 | /* Timeout values */ |
Ashok Reddy Soma | d5a6334 | 2022-05-10 07:12:34 -0600 | [diff] [blame] | 170 | #define TIMEOUT_US 10000 |
Michal Simek | 0be35ab | 2022-01-06 09:49:41 +0100 | [diff] [blame] | 171 | |
| 172 | #define IOU_SLCR_GEM_CLK_CTRL 0x308 |
| 173 | #define GEM_CTRL_GEM_SGMII_MODE BIT(2) |
| 174 | #define GEM_CTRL_GEM_REF_SRC_SEL BIT(1) |
| 175 | |
| 176 | #define IOU_SLCR_GEM_CTRL 0x360 |
| 177 | #define GEM_CTRL_GEM_SGMII_SD BIT(0) |
| 178 | |
| 179 | /** |
| 180 | * struct xpsgtr_ssc - structure to hold SSC settings for a lane |
| 181 | * @refclk_rate: PLL reference clock frequency |
| 182 | * @pll_ref_clk: value to be written to register for corresponding ref clk rate |
| 183 | * @steps: number of steps of SSC (Spread Spectrum Clock) |
| 184 | * @step_size: step size of each step |
| 185 | */ |
| 186 | struct xpsgtr_ssc { |
| 187 | u32 refclk_rate; |
| 188 | u8 pll_ref_clk; |
| 189 | u32 steps; |
| 190 | u32 step_size; |
| 191 | }; |
| 192 | |
| 193 | /** |
| 194 | * struct xpsgtr_phy - representation of a lane |
| 195 | * @dev: pointer to the xpsgtr_dev instance |
| 196 | * @refclk: reference clock index |
| 197 | * @type: controller which uses this lane |
| 198 | * @lane: lane number |
| 199 | * @protocol: protocol in which the lane operates |
| 200 | */ |
| 201 | struct xpsgtr_phy { |
| 202 | struct xpsgtr_dev *dev; |
| 203 | unsigned int refclk; |
| 204 | u8 type; |
| 205 | u8 lane; |
| 206 | u8 protocol; |
| 207 | }; |
| 208 | |
| 209 | /** |
| 210 | * struct xpsgtr_dev - representation of a ZynMP GT device |
| 211 | * @dev: pointer to device |
| 212 | * @serdes: serdes base address |
| 213 | * @siou: siou base address |
| 214 | * @phys: PHY lanes |
| 215 | * @refclk_sscs: spread spectrum settings for the reference clocks |
| 216 | * @clk: reference clocks |
| 217 | */ |
| 218 | struct xpsgtr_dev { |
| 219 | struct udevice *dev; |
| 220 | u8 *serdes; |
| 221 | u8 *siou; |
| 222 | struct xpsgtr_phy phys[NUM_LANES]; |
| 223 | const struct xpsgtr_ssc *refclk_sscs[NUM_LANES]; |
| 224 | struct clk clk[NUM_LANES]; |
| 225 | }; |
| 226 | |
| 227 | /* Configuration Data */ |
| 228 | /* lookup table to hold all settings needed for a ref clock frequency */ |
| 229 | static const struct xpsgtr_ssc ssc_lookup[] = { |
| 230 | { 19200000, 0x05, 608, 264020 }, |
| 231 | { 20000000, 0x06, 634, 243454 }, |
| 232 | { 24000000, 0x07, 760, 168973 }, |
| 233 | { 26000000, 0x08, 824, 143860 }, |
| 234 | { 27000000, 0x09, 856, 86551 }, |
| 235 | { 38400000, 0x0a, 1218, 65896 }, |
| 236 | { 40000000, 0x0b, 634, 243454 }, |
| 237 | { 52000000, 0x0c, 824, 143860 }, |
| 238 | { 100000000, 0x0d, 1058, 87533 }, |
| 239 | { 108000000, 0x0e, 856, 86551 }, |
| 240 | { 125000000, 0x0f, 992, 119497 }, |
| 241 | { 135000000, 0x10, 1070, 55393 }, |
| 242 | { 150000000, 0x11, 792, 187091 } |
| 243 | }; |
| 244 | |
| 245 | /* I/O Accessors */ |
| 246 | static u32 xpsgtr_read(struct xpsgtr_dev *gtr_dev, u32 reg) |
| 247 | { |
| 248 | return readl(gtr_dev->serdes + reg); |
| 249 | } |
| 250 | |
| 251 | static void xpsgtr_write(struct xpsgtr_dev *gtr_dev, u32 reg, u32 value) |
| 252 | { |
| 253 | writel(value, gtr_dev->serdes + reg); |
| 254 | } |
| 255 | |
| 256 | static void xpsgtr_clr_set(struct xpsgtr_dev *gtr_dev, u32 reg, |
| 257 | u32 clr, u32 set) |
| 258 | { |
| 259 | u32 value = xpsgtr_read(gtr_dev, reg); |
| 260 | |
| 261 | value &= ~clr; |
| 262 | value |= set; |
| 263 | xpsgtr_write(gtr_dev, reg, value); |
| 264 | } |
| 265 | |
| 266 | static u32 xpsgtr_read_phy(struct xpsgtr_phy *gtr_phy, u32 reg) |
| 267 | { |
| 268 | void __iomem *addr = gtr_phy->dev->serdes |
| 269 | + gtr_phy->lane * PHY_REG_OFFSET + reg; |
| 270 | |
| 271 | return readl(addr); |
| 272 | } |
| 273 | |
| 274 | static void xpsgtr_write_phy(struct xpsgtr_phy *gtr_phy, |
| 275 | u32 reg, u32 value) |
| 276 | { |
| 277 | void __iomem *addr = gtr_phy->dev->serdes |
| 278 | + gtr_phy->lane * PHY_REG_OFFSET + reg; |
| 279 | |
| 280 | writel(value, addr); |
| 281 | } |
| 282 | |
| 283 | static void xpsgtr_clr_set_phy(struct xpsgtr_phy *gtr_phy, |
| 284 | u32 reg, u32 clr, u32 set) |
| 285 | { |
| 286 | void __iomem *addr = gtr_phy->dev->serdes |
| 287 | + gtr_phy->lane * PHY_REG_OFFSET + reg; |
| 288 | |
| 289 | writel((readl(addr) & ~clr) | set, addr); |
| 290 | } |
| 291 | |
| 292 | /* Configure PLL and spread-sprectrum clock. */ |
| 293 | static void xpsgtr_configure_pll(struct xpsgtr_phy *gtr_phy) |
| 294 | { |
| 295 | const struct xpsgtr_ssc *ssc; |
| 296 | u32 step_size; |
| 297 | |
| 298 | ssc = gtr_phy->dev->refclk_sscs[gtr_phy->refclk]; |
| 299 | step_size = ssc->step_size; |
| 300 | |
| 301 | xpsgtr_clr_set(gtr_phy->dev, PLL_REF_SEL(gtr_phy->lane), |
| 302 | PLL_FREQ_MASK, ssc->pll_ref_clk); |
| 303 | |
| 304 | /* Enable lane clock sharing, if required */ |
| 305 | if (gtr_phy->refclk != gtr_phy->lane) { |
| 306 | /* Lane3 Ref Clock Selection Register */ |
| 307 | xpsgtr_clr_set(gtr_phy->dev, L0_Ln_REF_CLK_SEL(gtr_phy->lane), |
| 308 | L0_REF_CLK_SEL_MASK, 1 << gtr_phy->refclk); |
| 309 | } |
| 310 | |
| 311 | /* SSC step size [7:0] */ |
| 312 | xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEP_SIZE_0_LSB, |
| 313 | STEP_SIZE_0_MASK, step_size & STEP_SIZE_0_MASK); |
| 314 | |
| 315 | /* SSC step size [15:8] */ |
| 316 | step_size >>= STEP_SIZE_SHIFT; |
| 317 | xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEP_SIZE_1, |
| 318 | STEP_SIZE_1_MASK, step_size & STEP_SIZE_1_MASK); |
| 319 | |
| 320 | /* SSC step size [23:16] */ |
| 321 | step_size >>= STEP_SIZE_SHIFT; |
| 322 | xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEP_SIZE_2, |
| 323 | STEP_SIZE_2_MASK, step_size & STEP_SIZE_2_MASK); |
| 324 | |
| 325 | /* SSC steps [7:0] */ |
| 326 | xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEPS_0_LSB, |
| 327 | STEPS_0_MASK, ssc->steps & STEPS_0_MASK); |
| 328 | |
| 329 | /* SSC steps [10:8] */ |
| 330 | xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEPS_1_MSB, |
| 331 | STEPS_1_MASK, |
| 332 | (ssc->steps >> STEP_SIZE_SHIFT) & STEPS_1_MASK); |
| 333 | |
| 334 | /* SSC step size [24:25] */ |
| 335 | step_size >>= STEP_SIZE_SHIFT; |
| 336 | xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEP_SIZE_3_MSB, |
| 337 | STEP_SIZE_3_MASK, (step_size & STEP_SIZE_3_MASK) | |
| 338 | FORCE_STEP_SIZE | FORCE_STEPS); |
| 339 | } |
| 340 | |
| 341 | /* Configure the lane protocol. */ |
| 342 | static void xpsgtr_lane_set_protocol(struct xpsgtr_phy *gtr_phy) |
| 343 | { |
| 344 | struct xpsgtr_dev *gtr_dev = gtr_phy->dev; |
| 345 | u8 protocol = gtr_phy->protocol; |
| 346 | |
| 347 | switch (gtr_phy->lane) { |
| 348 | case 0: |
| 349 | xpsgtr_clr_set(gtr_dev, ICM_CFG0, ICM_CFG0_L0_MASK, protocol); |
| 350 | break; |
| 351 | case 1: |
| 352 | xpsgtr_clr_set(gtr_dev, ICM_CFG0, ICM_CFG0_L1_MASK, |
| 353 | protocol << ICM_CFG_SHIFT); |
| 354 | break; |
| 355 | case 2: |
| 356 | xpsgtr_clr_set(gtr_dev, ICM_CFG1, ICM_CFG0_L0_MASK, protocol); |
| 357 | break; |
| 358 | case 3: |
| 359 | xpsgtr_clr_set(gtr_dev, ICM_CFG1, ICM_CFG0_L1_MASK, |
| 360 | protocol << ICM_CFG_SHIFT); |
| 361 | break; |
| 362 | default: |
| 363 | /* We already checked 0 <= lane <= 3 */ |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /* Bypass (de)scrambler and 8b/10b decoder and encoder. */ |
| 369 | static void xpsgtr_bypass_scrambler_8b10b(struct xpsgtr_phy *gtr_phy) |
| 370 | { |
| 371 | xpsgtr_write_phy(gtr_phy, L0_TM_DIG_6, L0_TM_DIS_DESCRAMBLE_DECODER); |
| 372 | xpsgtr_write_phy(gtr_phy, L0_TX_DIG_61, L0_TM_DISABLE_SCRAMBLE_ENCODER); |
| 373 | } |
| 374 | |
Michal Simek | 1c5e991 | 2022-02-07 10:36:32 +0100 | [diff] [blame] | 375 | /* DP-specific initialization. */ |
| 376 | static void xpsgtr_phy_init_dp(struct xpsgtr_phy *gtr_phy) |
| 377 | { |
| 378 | xpsgtr_write_phy(gtr_phy, L0_TXPMD_TM_45, |
| 379 | L0_TXPMD_TM_45_OVER_DP_MAIN | |
| 380 | L0_TXPMD_TM_45_ENABLE_DP_MAIN | |
| 381 | L0_TXPMD_TM_45_OVER_DP_POST1 | |
| 382 | L0_TXPMD_TM_45_OVER_DP_POST2 | |
| 383 | L0_TXPMD_TM_45_ENABLE_DP_POST2); |
| 384 | xpsgtr_write_phy(gtr_phy, L0_TX_ANA_TM_118, |
| 385 | L0_TX_ANA_TM_118_FORCE_17_0); |
| 386 | } |
| 387 | |
| 388 | /* SATA-specific initialization. */ |
| 389 | static void xpsgtr_phy_init_sata(struct xpsgtr_phy *gtr_phy) |
| 390 | { |
| 391 | struct xpsgtr_dev *gtr_dev = gtr_phy->dev; |
| 392 | |
| 393 | xpsgtr_bypass_scrambler_8b10b(gtr_phy); |
| 394 | |
| 395 | writel(gtr_phy->lane, gtr_dev->siou + SATA_CONTROL_OFFSET); |
| 396 | } |
| 397 | |
Michal Simek | 0be35ab | 2022-01-06 09:49:41 +0100 | [diff] [blame] | 398 | /* SGMII-specific initialization. */ |
| 399 | static void xpsgtr_phy_init_sgmii(struct xpsgtr_phy *gtr_phy) |
| 400 | { |
| 401 | struct xpsgtr_dev *gtr_dev = gtr_phy->dev; |
| 402 | u32 shift = gtr_phy->lane * PROT_BUS_WIDTH_SHIFT; |
| 403 | |
| 404 | /* Set SGMII protocol TX and RX bus width to 10 bits. */ |
| 405 | xpsgtr_clr_set(gtr_dev, TX_PROT_BUS_WIDTH, PROT_BUS_WIDTH_MASK << shift, |
| 406 | PROT_BUS_WIDTH_10 << shift); |
| 407 | |
| 408 | xpsgtr_clr_set(gtr_dev, RX_PROT_BUS_WIDTH, PROT_BUS_WIDTH_MASK << shift, |
| 409 | PROT_BUS_WIDTH_10 << shift); |
| 410 | |
| 411 | xpsgtr_bypass_scrambler_8b10b(gtr_phy); |
| 412 | |
| 413 | /* |
| 414 | * Below code is just temporary solution till we have a way how to |
| 415 | * do it via firmware interface in sync with Linux. Till that happen |
| 416 | * this is the most sensible thing to do here. |
| 417 | */ |
| 418 | /* GEM I/O Clock Control */ |
| 419 | clrsetbits_le32(ZYNQMP_IOU_SLCR_BASEADDR + IOU_SLCR_GEM_CLK_CTRL, |
| 420 | 0xf << shift, |
| 421 | (GEM_CTRL_GEM_SGMII_MODE | GEM_CTRL_GEM_REF_SRC_SEL) << |
| 422 | shift); |
| 423 | |
| 424 | /* Setup signal detect */ |
| 425 | clrsetbits_le32(ZYNQMP_IOU_SLCR_BASEADDR + IOU_SLCR_GEM_CTRL, |
| 426 | PROT_BUS_WIDTH_MASK << shift, |
| 427 | GEM_CTRL_GEM_SGMII_SD << shift); |
| 428 | } |
| 429 | |
| 430 | static int xpsgtr_init(struct phy *x) |
| 431 | { |
| 432 | struct xpsgtr_dev *gtr_dev = dev_get_priv(x->dev); |
| 433 | struct xpsgtr_phy *gtr_phy; |
| 434 | u32 phy_lane = x->id; |
| 435 | |
| 436 | gtr_phy = >r_dev->phys[phy_lane]; |
| 437 | |
| 438 | /* Enable coarse code saturation limiting logic. */ |
| 439 | xpsgtr_write_phy(gtr_phy, L0_TM_PLL_DIG_37, L0_TM_COARSE_CODE_LIMIT); |
| 440 | |
| 441 | /* |
| 442 | * Configure the PLL, the lane protocol, and perform protocol-specific |
| 443 | * initialization. |
| 444 | */ |
| 445 | xpsgtr_configure_pll(gtr_phy); |
| 446 | xpsgtr_lane_set_protocol(gtr_phy); |
| 447 | |
| 448 | switch (gtr_phy->protocol) { |
| 449 | case ICM_PROTOCOL_SGMII: |
| 450 | xpsgtr_phy_init_sgmii(gtr_phy); |
| 451 | break; |
Michal Simek | 0be35ab | 2022-01-06 09:49:41 +0100 | [diff] [blame] | 452 | case ICM_PROTOCOL_SATA: |
Michal Simek | 1c5e991 | 2022-02-07 10:36:32 +0100 | [diff] [blame] | 453 | xpsgtr_phy_init_sata(gtr_phy); |
| 454 | break; |
| 455 | case ICM_PROTOCOL_DP: |
| 456 | xpsgtr_phy_init_dp(gtr_phy); |
| 457 | break; |
Michal Simek | 0be35ab | 2022-01-06 09:49:41 +0100 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | dev_dbg(gtr_dev->dev, "lane %u (type %u, protocol %u): init done\n", |
| 461 | gtr_phy->lane, gtr_phy->type, gtr_phy->protocol); |
| 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | /* Wait for the PLL to lock (with a timeout). */ |
| 467 | static int xpsgtr_wait_pll_lock(struct phy *phy) |
| 468 | { |
| 469 | struct xpsgtr_dev *gtr_dev = dev_get_priv(phy->dev); |
| 470 | struct xpsgtr_phy *gtr_phy; |
| 471 | u32 phy_lane = phy->id; |
| 472 | int ret = 0; |
| 473 | unsigned int timeout = TIMEOUT_US; |
| 474 | |
| 475 | gtr_phy = >r_dev->phys[phy_lane]; |
| 476 | |
| 477 | dev_dbg(gtr_dev->dev, "Waiting for PLL lock\n"); |
| 478 | |
| 479 | while (1) { |
| 480 | u32 reg = xpsgtr_read_phy(gtr_phy, L0_PLL_STATUS_READ_1); |
| 481 | |
| 482 | if ((reg & PLL_STATUS_LOCKED) == PLL_STATUS_LOCKED) { |
| 483 | ret = 0; |
| 484 | break; |
| 485 | } |
| 486 | |
| 487 | if (--timeout == 0) { |
| 488 | ret = -ETIMEDOUT; |
| 489 | break; |
| 490 | } |
| 491 | |
| 492 | udelay(1); |
| 493 | } |
| 494 | |
| 495 | if (ret == -ETIMEDOUT) |
| 496 | dev_err(gtr_dev->dev, |
| 497 | "lane %u (type %u, protocol %u): PLL lock timeout\n", |
| 498 | gtr_phy->lane, gtr_phy->type, gtr_phy->protocol); |
| 499 | |
| 500 | return ret; |
| 501 | } |
| 502 | |
| 503 | static int xpsgtr_power_on(struct phy *phy) |
| 504 | { |
| 505 | struct xpsgtr_dev *gtr_dev = dev_get_priv(phy->dev); |
| 506 | struct xpsgtr_phy *gtr_phy; |
| 507 | u32 phy_lane = phy->id; |
| 508 | int ret = 0; |
| 509 | |
| 510 | gtr_phy = >r_dev->phys[phy_lane]; |
| 511 | |
| 512 | /* |
| 513 | * Wait for the PLL to lock. For DP, only wait on DP0 to avoid |
| 514 | * cumulating waits for both lanes. The user is expected to initialize |
| 515 | * lane 0 last. |
| 516 | */ |
| 517 | if (gtr_phy->protocol != ICM_PROTOCOL_DP || |
| 518 | gtr_phy->type == XPSGTR_TYPE_DP_0) |
| 519 | ret = xpsgtr_wait_pll_lock(phy); |
| 520 | |
| 521 | return ret; |
| 522 | } |
| 523 | |
| 524 | /* |
| 525 | * OF Xlate Support |
| 526 | */ |
| 527 | |
| 528 | /* Set the lane type and protocol based on the PHY type and instance number. */ |
| 529 | static int xpsgtr_set_lane_type(struct xpsgtr_phy *gtr_phy, u8 phy_type, |
| 530 | unsigned int phy_instance) |
| 531 | { |
| 532 | unsigned int num_phy_types; |
| 533 | const int *phy_types; |
| 534 | |
| 535 | switch (phy_type) { |
| 536 | case PHY_TYPE_SATA: { |
| 537 | static const int types[] = { |
| 538 | XPSGTR_TYPE_SATA_0, |
| 539 | XPSGTR_TYPE_SATA_1, |
| 540 | }; |
| 541 | |
| 542 | phy_types = types; |
| 543 | num_phy_types = ARRAY_SIZE(types); |
| 544 | gtr_phy->protocol = ICM_PROTOCOL_SATA; |
| 545 | break; |
| 546 | } |
| 547 | case PHY_TYPE_USB3: { |
| 548 | static const int types[] = { |
| 549 | XPSGTR_TYPE_USB0, |
| 550 | XPSGTR_TYPE_USB1, |
| 551 | }; |
| 552 | |
| 553 | phy_types = types; |
| 554 | num_phy_types = ARRAY_SIZE(types); |
| 555 | gtr_phy->protocol = ICM_PROTOCOL_USB; |
| 556 | break; |
| 557 | } |
| 558 | case PHY_TYPE_DP: { |
| 559 | static const int types[] = { |
| 560 | XPSGTR_TYPE_DP_0, |
| 561 | XPSGTR_TYPE_DP_1, |
| 562 | }; |
| 563 | |
| 564 | phy_types = types; |
| 565 | num_phy_types = ARRAY_SIZE(types); |
| 566 | gtr_phy->protocol = ICM_PROTOCOL_DP; |
| 567 | break; |
| 568 | } |
| 569 | case PHY_TYPE_PCIE: { |
| 570 | static const int types[] = { |
| 571 | XPSGTR_TYPE_PCIE_0, |
| 572 | XPSGTR_TYPE_PCIE_1, |
| 573 | XPSGTR_TYPE_PCIE_2, |
| 574 | XPSGTR_TYPE_PCIE_3, |
| 575 | }; |
| 576 | |
| 577 | phy_types = types; |
| 578 | num_phy_types = ARRAY_SIZE(types); |
| 579 | gtr_phy->protocol = ICM_PROTOCOL_PCIE; |
| 580 | break; |
| 581 | } |
| 582 | case PHY_TYPE_SGMII: { |
| 583 | static const int types[] = { |
| 584 | XPSGTR_TYPE_SGMII0, |
| 585 | XPSGTR_TYPE_SGMII1, |
| 586 | XPSGTR_TYPE_SGMII2, |
| 587 | XPSGTR_TYPE_SGMII3, |
| 588 | }; |
| 589 | |
| 590 | phy_types = types; |
| 591 | num_phy_types = ARRAY_SIZE(types); |
| 592 | gtr_phy->protocol = ICM_PROTOCOL_SGMII; |
| 593 | break; |
| 594 | } |
| 595 | default: |
| 596 | return -EINVAL; |
| 597 | } |
| 598 | |
| 599 | if (phy_instance >= num_phy_types) |
| 600 | return -EINVAL; |
| 601 | |
| 602 | gtr_phy->type = phy_types[phy_instance]; |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | /* |
| 607 | * Valid combinations of controllers and lanes (Interconnect Matrix). |
| 608 | */ |
| 609 | static const unsigned int icm_matrix[NUM_LANES][CONTROLLERS_PER_LANE] = { |
| 610 | { XPSGTR_TYPE_PCIE_0, XPSGTR_TYPE_SATA_0, XPSGTR_TYPE_USB0, |
| 611 | XPSGTR_TYPE_DP_1, XPSGTR_TYPE_SGMII0 }, |
| 612 | { XPSGTR_TYPE_PCIE_1, XPSGTR_TYPE_SATA_1, XPSGTR_TYPE_USB0, |
| 613 | XPSGTR_TYPE_DP_0, XPSGTR_TYPE_SGMII1 }, |
| 614 | { XPSGTR_TYPE_PCIE_2, XPSGTR_TYPE_SATA_0, XPSGTR_TYPE_USB0, |
| 615 | XPSGTR_TYPE_DP_1, XPSGTR_TYPE_SGMII2 }, |
| 616 | { XPSGTR_TYPE_PCIE_3, XPSGTR_TYPE_SATA_1, XPSGTR_TYPE_USB1, |
| 617 | XPSGTR_TYPE_DP_0, XPSGTR_TYPE_SGMII3 } |
| 618 | }; |
| 619 | |
| 620 | /* Translate OF phandle and args to PHY instance. */ |
| 621 | static int xpsgtr_of_xlate(struct phy *x, |
| 622 | struct ofnode_phandle_args *args) |
| 623 | { |
| 624 | struct xpsgtr_dev *gtr_dev = dev_get_priv(x->dev); |
| 625 | struct xpsgtr_phy *gtr_phy; |
| 626 | struct udevice *dev = x->dev; |
| 627 | unsigned int phy_instance; |
| 628 | unsigned int phy_lane; |
| 629 | unsigned int phy_type; |
| 630 | unsigned int refclk; |
| 631 | unsigned int i; |
| 632 | int ret; |
| 633 | |
| 634 | if (args->args_count != 4) { |
| 635 | dev_err(dev, "Invalid number of cells in 'phy' property\n"); |
| 636 | return -EINVAL; |
| 637 | } |
| 638 | |
| 639 | /* |
| 640 | * Get the PHY parameters from the OF arguments and derive the lane |
| 641 | * type. |
| 642 | */ |
| 643 | phy_lane = args->args[0]; |
| 644 | if (phy_lane >= NUM_LANES) { |
| 645 | dev_err(dev, "Invalid lane number %u\n", phy_lane); |
| 646 | return -EINVAL; |
| 647 | } |
| 648 | |
| 649 | gtr_phy = >r_dev->phys[phy_lane]; |
| 650 | phy_type = args->args[1]; |
| 651 | phy_instance = args->args[2]; |
| 652 | |
| 653 | ret = xpsgtr_set_lane_type(gtr_phy, phy_type, phy_instance); |
| 654 | if (ret) { |
| 655 | dev_err(dev, "Invalid PHY type and/or instance\n"); |
| 656 | return ret; |
| 657 | } |
| 658 | |
| 659 | refclk = args->args[3]; |
| 660 | if (refclk >= ARRAY_SIZE(gtr_dev->refclk_sscs) || |
| 661 | !gtr_dev->refclk_sscs[refclk]) { |
| 662 | dev_err(dev, "Invalid reference clock number %u\n", refclk); |
| 663 | return -EINVAL; |
| 664 | } |
| 665 | |
| 666 | gtr_phy->refclk = refclk; |
| 667 | |
| 668 | /* This is difference compare to Linux */ |
| 669 | gtr_phy->dev = gtr_dev; |
| 670 | gtr_phy->lane = phy_lane; |
| 671 | |
| 672 | /* |
| 673 | * Ensure that the Interconnect Matrix is obeyed, i.e a given lane type |
| 674 | * is allowed to operate on the lane. |
| 675 | */ |
| 676 | for (i = 0; i < CONTROLLERS_PER_LANE; i++) { |
| 677 | if (icm_matrix[phy_lane][i] == gtr_phy->type) { |
| 678 | x->id = phy_lane; |
| 679 | return 0; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | return -EINVAL; |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * Probe & Platform Driver |
| 688 | */ |
| 689 | static int xpsgtr_get_ref_clocks(struct udevice *dev) |
| 690 | { |
| 691 | unsigned int refclk; |
| 692 | struct xpsgtr_dev *gtr_dev = dev_get_priv(dev); |
| 693 | int ret; |
| 694 | |
| 695 | for (refclk = 0; refclk < NUM_LANES; ++refclk) { |
| 696 | int i; |
| 697 | u32 rate; |
| 698 | char name[8]; |
| 699 | struct clk *clk = >r_dev->clk[refclk]; |
| 700 | |
| 701 | snprintf(name, sizeof(name), "ref%u", refclk); |
| 702 | dev_dbg(dev, "Checking name: %s\n", name); |
| 703 | ret = clk_get_by_name(dev, name, clk); |
| 704 | if (ret == -ENODATA) { |
| 705 | dev_dbg(dev, "%s clock not specified (err %d)\n", |
| 706 | name, ret); |
| 707 | continue; |
| 708 | } else if (ret) { |
| 709 | dev_dbg(dev, "couldn't get clock %s (err %d)\n", |
| 710 | name, ret); |
| 711 | return ret; |
| 712 | } |
| 713 | |
| 714 | rate = clk_get_rate(clk); |
| 715 | |
| 716 | dev_dbg(dev, "clk rate %d\n", rate); |
| 717 | |
| 718 | ret = clk_enable(clk); |
| 719 | if (ret) { |
| 720 | dev_err(dev, "failed to enable refclk %d clock\n", |
| 721 | refclk); |
| 722 | return ret; |
| 723 | } |
| 724 | |
| 725 | for (i = 0 ; i < ARRAY_SIZE(ssc_lookup); i++) { |
| 726 | if (rate == ssc_lookup[i].refclk_rate) { |
| 727 | gtr_dev->refclk_sscs[refclk] = &ssc_lookup[i]; |
| 728 | dev_dbg(dev, "Found rate %d\n", i); |
| 729 | break; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | if (i == ARRAY_SIZE(ssc_lookup)) { |
| 734 | dev_err(dev, |
| 735 | "Invalid rate %u for reference clock %u\n", |
| 736 | rate, refclk); |
| 737 | return -EINVAL; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static int xpsgtr_probe(struct udevice *dev) |
| 745 | { |
| 746 | struct xpsgtr_dev *gtr_dev = dev_get_priv(dev); |
| 747 | |
| 748 | gtr_dev->serdes = dev_remap_addr_name(dev, "serdes"); |
| 749 | if (!gtr_dev->serdes) |
| 750 | return -EINVAL; |
| 751 | |
| 752 | gtr_dev->siou = dev_remap_addr_name(dev, "siou"); |
| 753 | if (!gtr_dev->siou) |
| 754 | return -EINVAL; |
| 755 | |
| 756 | gtr_dev->dev = dev; |
| 757 | |
| 758 | return xpsgtr_get_ref_clocks(dev); |
| 759 | } |
| 760 | |
| 761 | static const struct udevice_id xpsgtr_phy_ids[] = { |
| 762 | { .compatible = "xlnx,zynqmp-psgtr-v1.1", }, |
| 763 | { } |
| 764 | }; |
| 765 | |
| 766 | static const struct phy_ops xpsgtr_phy_ops = { |
| 767 | .init = xpsgtr_init, |
| 768 | .of_xlate = xpsgtr_of_xlate, |
| 769 | .power_on = xpsgtr_power_on, |
| 770 | }; |
| 771 | |
| 772 | U_BOOT_DRIVER(psgtr_phy) = { |
| 773 | .name = "psgtr_phy", |
| 774 | .id = UCLASS_PHY, |
| 775 | .of_match = xpsgtr_phy_ids, |
| 776 | .ops = &xpsgtr_phy_ops, |
| 777 | .probe = xpsgtr_probe, |
| 778 | .priv_auto = sizeof(struct xpsgtr_dev), |
| 779 | }; |