0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <linux/clk.h>
0017 #include <linux/delay.h>
0018 #include <linux/io.h>
0019 #include <linux/kernel.h>
0020 #include <linux/module.h>
0021 #include <linux/of.h>
0022 #include <linux/phy/phy.h>
0023 #include <linux/platform_device.h>
0024 #include <linux/slab.h>
0025
0026 #include <dt-bindings/phy/phy.h>
0027
0028
0029
0030
0031
0032
0033 #define L0_TX_ANA_TM_18 0x0048
0034 #define L0_TX_ANA_TM_118 0x01d8
0035 #define L0_TX_ANA_TM_118_FORCE_17_0 BIT(0)
0036
0037
0038 #define L0_TXPMA_ST_3 0x0b0c
0039 #define L0_DN_CALIB_CODE 0x3f
0040
0041
0042 #define L0_TXPMD_TM_45 0x0cb4
0043 #define L0_TXPMD_TM_48 0x0cc0
0044 #define L0_TXPMD_TM_45_OVER_DP_MAIN BIT(0)
0045 #define L0_TXPMD_TM_45_ENABLE_DP_MAIN BIT(1)
0046 #define L0_TXPMD_TM_45_OVER_DP_POST1 BIT(2)
0047 #define L0_TXPMD_TM_45_ENABLE_DP_POST1 BIT(3)
0048 #define L0_TXPMD_TM_45_OVER_DP_POST2 BIT(4)
0049 #define L0_TXPMD_TM_45_ENABLE_DP_POST2 BIT(5)
0050
0051
0052 #define L0_TM_DIG_6 0x106c
0053 #define L0_TM_DIS_DESCRAMBLE_DECODER 0x0f
0054 #define L0_TX_DIG_61 0x00f4
0055 #define L0_TM_DISABLE_SCRAMBLE_ENCODER 0x0f
0056
0057
0058 #define L0_TM_PLL_DIG_37 0x2094
0059 #define L0_TM_COARSE_CODE_LIMIT 0x10
0060
0061
0062 #define L0_PLL_SS_STEPS_0_LSB 0x2368
0063 #define L0_PLL_SS_STEPS_1_MSB 0x236c
0064 #define L0_PLL_SS_STEP_SIZE_0_LSB 0x2370
0065 #define L0_PLL_SS_STEP_SIZE_1 0x2374
0066 #define L0_PLL_SS_STEP_SIZE_2 0x2378
0067 #define L0_PLL_SS_STEP_SIZE_3_MSB 0x237c
0068 #define L0_PLL_STATUS_READ_1 0x23e4
0069
0070
0071 #define STEP_SIZE_0_MASK 0xff
0072 #define STEP_SIZE_1_MASK 0xff
0073 #define STEP_SIZE_2_MASK 0xff
0074 #define STEP_SIZE_3_MASK 0x3
0075 #define STEP_SIZE_SHIFT 8
0076 #define FORCE_STEP_SIZE 0x10
0077 #define FORCE_STEPS 0x20
0078 #define STEPS_0_MASK 0xff
0079 #define STEPS_1_MASK 0x07
0080
0081
0082 #define L0_Ln_REF_CLK_SEL(n) (0x2860 + (n) * 4)
0083 #define L0_REF_CLK_SEL_MASK 0x8f
0084
0085
0086 #define L3_TM_CALIB_DIG19 0xec4c
0087 #define L3_CALIB_DONE_STATUS 0xef14
0088 #define L3_TM_CALIB_DIG18 0xec48
0089 #define L3_TM_CALIB_DIG19_NSW 0x07
0090 #define L3_TM_CALIB_DIG18_NSW 0xe0
0091 #define L3_TM_OVERRIDE_NSW_CODE 0x20
0092 #define L3_CALIB_DONE 0x02
0093 #define L3_NSW_SHIFT 5
0094 #define L3_NSW_PIPE_SHIFT 4
0095 #define L3_NSW_CALIB_SHIFT 3
0096
0097 #define PHY_REG_OFFSET 0x4000
0098
0099
0100
0101
0102
0103
0104 #define PLL_REF_SEL(n) (0x10000 + (n) * 4)
0105 #define PLL_FREQ_MASK 0x1f
0106 #define PLL_STATUS_LOCKED 0x10
0107
0108
0109 #define ICM_CFG0 0x10010
0110 #define ICM_CFG1 0x10014
0111 #define ICM_CFG0_L0_MASK 0x07
0112 #define ICM_CFG0_L1_MASK 0x70
0113 #define ICM_CFG1_L2_MASK 0x07
0114 #define ICM_CFG2_L3_MASK 0x70
0115 #define ICM_CFG_SHIFT 4
0116
0117
0118 #define ICM_PROTOCOL_PD 0x0
0119 #define ICM_PROTOCOL_PCIE 0x1
0120 #define ICM_PROTOCOL_SATA 0x2
0121 #define ICM_PROTOCOL_USB 0x3
0122 #define ICM_PROTOCOL_DP 0x4
0123 #define ICM_PROTOCOL_SGMII 0x5
0124
0125
0126 #define TM_CMN_RST 0x10018
0127 #define TM_CMN_RST_EN 0x1
0128 #define TM_CMN_RST_SET 0x2
0129 #define TM_CMN_RST_MASK 0x3
0130
0131
0132 #define TX_PROT_BUS_WIDTH 0x10040
0133 #define RX_PROT_BUS_WIDTH 0x10044
0134 #define PROT_BUS_WIDTH_10 0x0
0135 #define PROT_BUS_WIDTH_20 0x1
0136 #define PROT_BUS_WIDTH_40 0x2
0137 #define PROT_BUS_WIDTH_SHIFT(n) ((n) * 2)
0138 #define PROT_BUS_WIDTH_MASK(n) GENMASK((n) * 2 + 1, (n) * 2)
0139
0140
0141 #define NUM_LANES 4
0142
0143
0144 #define SATA_CONTROL_OFFSET 0x0100
0145
0146
0147 #define CONTROLLERS_PER_LANE 5
0148
0149
0150 #define XPSGTR_TYPE_USB0 0
0151 #define XPSGTR_TYPE_USB1 1
0152 #define XPSGTR_TYPE_SATA_0 2
0153 #define XPSGTR_TYPE_SATA_1 3
0154 #define XPSGTR_TYPE_PCIE_0 4
0155 #define XPSGTR_TYPE_PCIE_1 5
0156 #define XPSGTR_TYPE_PCIE_2 6
0157 #define XPSGTR_TYPE_PCIE_3 7
0158 #define XPSGTR_TYPE_DP_0 8
0159 #define XPSGTR_TYPE_DP_1 9
0160 #define XPSGTR_TYPE_SGMII0 10
0161 #define XPSGTR_TYPE_SGMII1 11
0162 #define XPSGTR_TYPE_SGMII2 12
0163 #define XPSGTR_TYPE_SGMII3 13
0164
0165
0166 #define TIMEOUT_US 1000
0167
0168 struct xpsgtr_dev;
0169
0170
0171
0172
0173
0174
0175
0176
0177 struct xpsgtr_ssc {
0178 u32 refclk_rate;
0179 u8 pll_ref_clk;
0180 u32 steps;
0181 u32 step_size;
0182 };
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194 struct xpsgtr_phy {
0195 struct phy *phy;
0196 u8 type;
0197 u8 lane;
0198 u8 protocol;
0199 bool skip_phy_init;
0200 struct xpsgtr_dev *dev;
0201 unsigned int refclk;
0202 };
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217 struct xpsgtr_dev {
0218 struct device *dev;
0219 void __iomem *serdes;
0220 void __iomem *siou;
0221 struct mutex gtr_mutex;
0222 struct xpsgtr_phy phys[NUM_LANES];
0223 const struct xpsgtr_ssc *refclk_sscs[NUM_LANES];
0224 struct clk *clk[NUM_LANES];
0225 bool tx_term_fix;
0226 unsigned int saved_icm_cfg0;
0227 unsigned int saved_icm_cfg1;
0228 };
0229
0230
0231
0232
0233
0234
0235 static const struct xpsgtr_ssc ssc_lookup[] = {
0236 { 19200000, 0x05, 608, 264020 },
0237 { 20000000, 0x06, 634, 243454 },
0238 { 24000000, 0x07, 760, 168973 },
0239 { 26000000, 0x08, 824, 143860 },
0240 { 27000000, 0x09, 856, 86551 },
0241 { 38400000, 0x0a, 1218, 65896 },
0242 { 40000000, 0x0b, 634, 243454 },
0243 { 52000000, 0x0c, 824, 143860 },
0244 { 100000000, 0x0d, 1058, 87533 },
0245 { 108000000, 0x0e, 856, 86551 },
0246 { 125000000, 0x0f, 992, 119497 },
0247 { 135000000, 0x10, 1070, 55393 },
0248 { 150000000, 0x11, 792, 187091 }
0249 };
0250
0251
0252
0253
0254
0255 static inline u32 xpsgtr_read(struct xpsgtr_dev *gtr_dev, u32 reg)
0256 {
0257 return readl(gtr_dev->serdes + reg);
0258 }
0259
0260 static inline void xpsgtr_write(struct xpsgtr_dev *gtr_dev, u32 reg, u32 value)
0261 {
0262 writel(value, gtr_dev->serdes + reg);
0263 }
0264
0265 static inline void xpsgtr_clr_set(struct xpsgtr_dev *gtr_dev, u32 reg,
0266 u32 clr, u32 set)
0267 {
0268 u32 value = xpsgtr_read(gtr_dev, reg);
0269
0270 value &= ~clr;
0271 value |= set;
0272 xpsgtr_write(gtr_dev, reg, value);
0273 }
0274
0275 static inline u32 xpsgtr_read_phy(struct xpsgtr_phy *gtr_phy, u32 reg)
0276 {
0277 void __iomem *addr = gtr_phy->dev->serdes
0278 + gtr_phy->lane * PHY_REG_OFFSET + reg;
0279
0280 return readl(addr);
0281 }
0282
0283 static inline void xpsgtr_write_phy(struct xpsgtr_phy *gtr_phy,
0284 u32 reg, u32 value)
0285 {
0286 void __iomem *addr = gtr_phy->dev->serdes
0287 + gtr_phy->lane * PHY_REG_OFFSET + reg;
0288
0289 writel(value, addr);
0290 }
0291
0292 static inline void xpsgtr_clr_set_phy(struct xpsgtr_phy *gtr_phy,
0293 u32 reg, u32 clr, u32 set)
0294 {
0295 void __iomem *addr = gtr_phy->dev->serdes
0296 + gtr_phy->lane * PHY_REG_OFFSET + reg;
0297
0298 writel((readl(addr) & ~clr) | set, addr);
0299 }
0300
0301
0302
0303
0304
0305
0306 static int xpsgtr_wait_pll_lock(struct phy *phy)
0307 {
0308 struct xpsgtr_phy *gtr_phy = phy_get_drvdata(phy);
0309 struct xpsgtr_dev *gtr_dev = gtr_phy->dev;
0310 unsigned int timeout = TIMEOUT_US;
0311 int ret;
0312
0313 dev_dbg(gtr_dev->dev, "Waiting for PLL lock\n");
0314
0315 while (1) {
0316 u32 reg = xpsgtr_read_phy(gtr_phy, L0_PLL_STATUS_READ_1);
0317
0318 if ((reg & PLL_STATUS_LOCKED) == PLL_STATUS_LOCKED) {
0319 ret = 0;
0320 break;
0321 }
0322
0323 if (--timeout == 0) {
0324 ret = -ETIMEDOUT;
0325 break;
0326 }
0327
0328 udelay(1);
0329 }
0330
0331 if (ret == -ETIMEDOUT)
0332 dev_err(gtr_dev->dev,
0333 "lane %u (type %u, protocol %u): PLL lock timeout\n",
0334 gtr_phy->lane, gtr_phy->type, gtr_phy->protocol);
0335
0336 return ret;
0337 }
0338
0339
0340 static void xpsgtr_configure_pll(struct xpsgtr_phy *gtr_phy)
0341 {
0342 const struct xpsgtr_ssc *ssc;
0343 u32 step_size;
0344
0345 ssc = gtr_phy->dev->refclk_sscs[gtr_phy->refclk];
0346 step_size = ssc->step_size;
0347
0348 xpsgtr_clr_set(gtr_phy->dev, PLL_REF_SEL(gtr_phy->lane),
0349 PLL_FREQ_MASK, ssc->pll_ref_clk);
0350
0351
0352 if (gtr_phy->refclk != gtr_phy->lane) {
0353
0354 xpsgtr_clr_set(gtr_phy->dev, L0_Ln_REF_CLK_SEL(gtr_phy->lane),
0355 L0_REF_CLK_SEL_MASK, 1 << gtr_phy->refclk);
0356 }
0357
0358
0359 xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEP_SIZE_0_LSB,
0360 STEP_SIZE_0_MASK, step_size & STEP_SIZE_0_MASK);
0361
0362
0363 step_size >>= STEP_SIZE_SHIFT;
0364 xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEP_SIZE_1,
0365 STEP_SIZE_1_MASK, step_size & STEP_SIZE_1_MASK);
0366
0367
0368 step_size >>= STEP_SIZE_SHIFT;
0369 xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEP_SIZE_2,
0370 STEP_SIZE_2_MASK, step_size & STEP_SIZE_2_MASK);
0371
0372
0373 xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEPS_0_LSB,
0374 STEPS_0_MASK, ssc->steps & STEPS_0_MASK);
0375
0376
0377 xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEPS_1_MSB,
0378 STEPS_1_MASK,
0379 (ssc->steps >> STEP_SIZE_SHIFT) & STEPS_1_MASK);
0380
0381
0382 step_size >>= STEP_SIZE_SHIFT;
0383 xpsgtr_clr_set_phy(gtr_phy, L0_PLL_SS_STEP_SIZE_3_MSB,
0384 STEP_SIZE_3_MASK, (step_size & STEP_SIZE_3_MASK) |
0385 FORCE_STEP_SIZE | FORCE_STEPS);
0386 }
0387
0388
0389 static void xpsgtr_lane_set_protocol(struct xpsgtr_phy *gtr_phy)
0390 {
0391 struct xpsgtr_dev *gtr_dev = gtr_phy->dev;
0392 u8 protocol = gtr_phy->protocol;
0393
0394 switch (gtr_phy->lane) {
0395 case 0:
0396 xpsgtr_clr_set(gtr_dev, ICM_CFG0, ICM_CFG0_L0_MASK, protocol);
0397 break;
0398 case 1:
0399 xpsgtr_clr_set(gtr_dev, ICM_CFG0, ICM_CFG0_L1_MASK,
0400 protocol << ICM_CFG_SHIFT);
0401 break;
0402 case 2:
0403 xpsgtr_clr_set(gtr_dev, ICM_CFG1, ICM_CFG0_L0_MASK, protocol);
0404 break;
0405 case 3:
0406 xpsgtr_clr_set(gtr_dev, ICM_CFG1, ICM_CFG0_L1_MASK,
0407 protocol << ICM_CFG_SHIFT);
0408 break;
0409 default:
0410
0411 break;
0412 }
0413 }
0414
0415
0416 static void xpsgtr_bypass_scrambler_8b10b(struct xpsgtr_phy *gtr_phy)
0417 {
0418 xpsgtr_write_phy(gtr_phy, L0_TM_DIG_6, L0_TM_DIS_DESCRAMBLE_DECODER);
0419 xpsgtr_write_phy(gtr_phy, L0_TX_DIG_61, L0_TM_DISABLE_SCRAMBLE_ENCODER);
0420 }
0421
0422
0423 static void xpsgtr_phy_init_dp(struct xpsgtr_phy *gtr_phy)
0424 {
0425 xpsgtr_write_phy(gtr_phy, L0_TXPMD_TM_45,
0426 L0_TXPMD_TM_45_OVER_DP_MAIN |
0427 L0_TXPMD_TM_45_ENABLE_DP_MAIN |
0428 L0_TXPMD_TM_45_OVER_DP_POST1 |
0429 L0_TXPMD_TM_45_OVER_DP_POST2 |
0430 L0_TXPMD_TM_45_ENABLE_DP_POST2);
0431 xpsgtr_write_phy(gtr_phy, L0_TX_ANA_TM_118,
0432 L0_TX_ANA_TM_118_FORCE_17_0);
0433 }
0434
0435
0436 static void xpsgtr_phy_init_sata(struct xpsgtr_phy *gtr_phy)
0437 {
0438 struct xpsgtr_dev *gtr_dev = gtr_phy->dev;
0439
0440 xpsgtr_bypass_scrambler_8b10b(gtr_phy);
0441
0442 writel(gtr_phy->lane, gtr_dev->siou + SATA_CONTROL_OFFSET);
0443 }
0444
0445
0446 static void xpsgtr_phy_init_sgmii(struct xpsgtr_phy *gtr_phy)
0447 {
0448 struct xpsgtr_dev *gtr_dev = gtr_phy->dev;
0449 u32 mask = PROT_BUS_WIDTH_MASK(gtr_phy->lane);
0450 u32 val = PROT_BUS_WIDTH_10 << PROT_BUS_WIDTH_SHIFT(gtr_phy->lane);
0451
0452
0453 xpsgtr_clr_set(gtr_dev, TX_PROT_BUS_WIDTH, mask, val);
0454 xpsgtr_clr_set(gtr_dev, RX_PROT_BUS_WIDTH, mask, val);
0455
0456 xpsgtr_bypass_scrambler_8b10b(gtr_phy);
0457 }
0458
0459
0460 static void xpsgtr_phy_configure_dp(struct xpsgtr_phy *gtr_phy, unsigned int pre,
0461 unsigned int voltage)
0462 {
0463 static const u8 voltage_swing[4][4] = {
0464 { 0x2a, 0x27, 0x24, 0x20 },
0465 { 0x27, 0x23, 0x20, 0xff },
0466 { 0x24, 0x20, 0xff, 0xff },
0467 { 0xff, 0xff, 0xff, 0xff }
0468 };
0469 static const u8 pre_emphasis[4][4] = {
0470 { 0x02, 0x02, 0x02, 0x02 },
0471 { 0x01, 0x01, 0x01, 0xff },
0472 { 0x00, 0x00, 0xff, 0xff },
0473 { 0xff, 0xff, 0xff, 0xff }
0474 };
0475
0476 xpsgtr_write_phy(gtr_phy, L0_TXPMD_TM_48, voltage_swing[pre][voltage]);
0477 xpsgtr_write_phy(gtr_phy, L0_TX_ANA_TM_18, pre_emphasis[pre][voltage]);
0478 }
0479
0480
0481
0482
0483
0484 static bool xpsgtr_phy_init_required(struct xpsgtr_phy *gtr_phy)
0485 {
0486
0487
0488
0489
0490
0491
0492
0493 if (gtr_phy->protocol == ICM_PROTOCOL_USB && gtr_phy->skip_phy_init)
0494 return false;
0495 else
0496 return true;
0497 }
0498
0499
0500
0501
0502
0503
0504 static int xpsgtr_phy_tx_term_fix(struct xpsgtr_phy *gtr_phy)
0505 {
0506 struct xpsgtr_dev *gtr_dev = gtr_phy->dev;
0507 u32 timeout = TIMEOUT_US;
0508 u32 nsw;
0509
0510
0511 xpsgtr_clr_set(gtr_dev, TM_CMN_RST, TM_CMN_RST_MASK, TM_CMN_RST_SET);
0512
0513
0514 xpsgtr_clr_set(gtr_dev, TM_CMN_RST, TM_CMN_RST_MASK, TM_CMN_RST_EN);
0515
0516 xpsgtr_write(gtr_dev, L3_TM_CALIB_DIG18, 0x00);
0517 xpsgtr_write(gtr_dev, L3_TM_CALIB_DIG19, L3_TM_OVERRIDE_NSW_CODE);
0518
0519
0520
0521
0522
0523
0524 xpsgtr_lane_set_protocol(gtr_phy);
0525
0526
0527 xpsgtr_clr_set(gtr_dev, TM_CMN_RST, TM_CMN_RST_MASK, TM_CMN_RST_SET);
0528
0529 dev_dbg(gtr_dev->dev, "calibrating...\n");
0530
0531 do {
0532 u32 reg = xpsgtr_read(gtr_dev, L3_CALIB_DONE_STATUS);
0533
0534 if ((reg & L3_CALIB_DONE) == L3_CALIB_DONE)
0535 break;
0536
0537 if (!--timeout) {
0538 dev_err(gtr_dev->dev, "calibration time out\n");
0539 return -ETIMEDOUT;
0540 }
0541
0542 udelay(1);
0543 } while (timeout > 0);
0544
0545 dev_dbg(gtr_dev->dev, "calibration done\n");
0546
0547
0548 nsw = xpsgtr_read(gtr_dev, L0_TXPMA_ST_3) & L0_DN_CALIB_CODE;
0549
0550
0551 xpsgtr_clr_set(gtr_dev, TM_CMN_RST, TM_CMN_RST_MASK, TM_CMN_RST_EN);
0552
0553
0554 xpsgtr_write(gtr_dev, L3_TM_CALIB_DIG19, nsw >> L3_NSW_CALIB_SHIFT);
0555
0556
0557 xpsgtr_write(gtr_dev, L3_TM_CALIB_DIG18,
0558 ((nsw & L3_TM_CALIB_DIG19_NSW) << L3_NSW_SHIFT) |
0559 (1 << L3_NSW_PIPE_SHIFT));
0560
0561
0562 xpsgtr_clr_set(gtr_dev, TM_CMN_RST, TM_CMN_RST_MASK, TM_CMN_RST_SET);
0563
0564 return 0;
0565 }
0566
0567 static int xpsgtr_phy_init(struct phy *phy)
0568 {
0569 struct xpsgtr_phy *gtr_phy = phy_get_drvdata(phy);
0570 struct xpsgtr_dev *gtr_dev = gtr_phy->dev;
0571 int ret = 0;
0572
0573 mutex_lock(>r_dev->gtr_mutex);
0574
0575
0576 if (!xpsgtr_phy_init_required(gtr_phy))
0577 goto out;
0578
0579 if (gtr_dev->tx_term_fix) {
0580 ret = xpsgtr_phy_tx_term_fix(gtr_phy);
0581 if (ret < 0)
0582 goto out;
0583
0584 gtr_dev->tx_term_fix = false;
0585 }
0586
0587
0588 xpsgtr_write_phy(gtr_phy, L0_TM_PLL_DIG_37, L0_TM_COARSE_CODE_LIMIT);
0589
0590
0591
0592
0593
0594 xpsgtr_configure_pll(gtr_phy);
0595 xpsgtr_lane_set_protocol(gtr_phy);
0596
0597 switch (gtr_phy->protocol) {
0598 case ICM_PROTOCOL_DP:
0599 xpsgtr_phy_init_dp(gtr_phy);
0600 break;
0601
0602 case ICM_PROTOCOL_SATA:
0603 xpsgtr_phy_init_sata(gtr_phy);
0604 break;
0605
0606 case ICM_PROTOCOL_SGMII:
0607 xpsgtr_phy_init_sgmii(gtr_phy);
0608 break;
0609 }
0610
0611 out:
0612 mutex_unlock(>r_dev->gtr_mutex);
0613 return ret;
0614 }
0615
0616 static int xpsgtr_phy_exit(struct phy *phy)
0617 {
0618 struct xpsgtr_phy *gtr_phy = phy_get_drvdata(phy);
0619
0620 gtr_phy->skip_phy_init = false;
0621
0622 return 0;
0623 }
0624
0625 static int xpsgtr_phy_power_on(struct phy *phy)
0626 {
0627 struct xpsgtr_phy *gtr_phy = phy_get_drvdata(phy);
0628 int ret = 0;
0629
0630
0631 if (!xpsgtr_phy_init_required(gtr_phy))
0632 return ret;
0633
0634
0635
0636
0637
0638 if (gtr_phy->protocol != ICM_PROTOCOL_DP ||
0639 gtr_phy->type == XPSGTR_TYPE_DP_0)
0640 ret = xpsgtr_wait_pll_lock(phy);
0641
0642 return ret;
0643 }
0644
0645 static int xpsgtr_phy_configure(struct phy *phy, union phy_configure_opts *opts)
0646 {
0647 struct xpsgtr_phy *gtr_phy = phy_get_drvdata(phy);
0648
0649 if (gtr_phy->protocol != ICM_PROTOCOL_DP)
0650 return 0;
0651
0652 xpsgtr_phy_configure_dp(gtr_phy, opts->dp.pre[0], opts->dp.voltage[0]);
0653
0654 return 0;
0655 }
0656
0657 static const struct phy_ops xpsgtr_phyops = {
0658 .init = xpsgtr_phy_init,
0659 .exit = xpsgtr_phy_exit,
0660 .power_on = xpsgtr_phy_power_on,
0661 .configure = xpsgtr_phy_configure,
0662 .owner = THIS_MODULE,
0663 };
0664
0665
0666
0667
0668
0669
0670 static int xpsgtr_set_lane_type(struct xpsgtr_phy *gtr_phy, u8 phy_type,
0671 unsigned int phy_instance)
0672 {
0673 unsigned int num_phy_types;
0674 const int *phy_types;
0675
0676 switch (phy_type) {
0677 case PHY_TYPE_SATA: {
0678 static const int types[] = {
0679 XPSGTR_TYPE_SATA_0,
0680 XPSGTR_TYPE_SATA_1,
0681 };
0682
0683 phy_types = types;
0684 num_phy_types = ARRAY_SIZE(types);
0685 gtr_phy->protocol = ICM_PROTOCOL_SATA;
0686 break;
0687 }
0688 case PHY_TYPE_USB3: {
0689 static const int types[] = {
0690 XPSGTR_TYPE_USB0,
0691 XPSGTR_TYPE_USB1,
0692 };
0693
0694 phy_types = types;
0695 num_phy_types = ARRAY_SIZE(types);
0696 gtr_phy->protocol = ICM_PROTOCOL_USB;
0697 break;
0698 }
0699 case PHY_TYPE_DP: {
0700 static const int types[] = {
0701 XPSGTR_TYPE_DP_0,
0702 XPSGTR_TYPE_DP_1,
0703 };
0704
0705 phy_types = types;
0706 num_phy_types = ARRAY_SIZE(types);
0707 gtr_phy->protocol = ICM_PROTOCOL_DP;
0708 break;
0709 }
0710 case PHY_TYPE_PCIE: {
0711 static const int types[] = {
0712 XPSGTR_TYPE_PCIE_0,
0713 XPSGTR_TYPE_PCIE_1,
0714 XPSGTR_TYPE_PCIE_2,
0715 XPSGTR_TYPE_PCIE_3,
0716 };
0717
0718 phy_types = types;
0719 num_phy_types = ARRAY_SIZE(types);
0720 gtr_phy->protocol = ICM_PROTOCOL_PCIE;
0721 break;
0722 }
0723 case PHY_TYPE_SGMII: {
0724 static const int types[] = {
0725 XPSGTR_TYPE_SGMII0,
0726 XPSGTR_TYPE_SGMII1,
0727 XPSGTR_TYPE_SGMII2,
0728 XPSGTR_TYPE_SGMII3,
0729 };
0730
0731 phy_types = types;
0732 num_phy_types = ARRAY_SIZE(types);
0733 gtr_phy->protocol = ICM_PROTOCOL_SGMII;
0734 break;
0735 }
0736 default:
0737 return -EINVAL;
0738 }
0739
0740 if (phy_instance >= num_phy_types)
0741 return -EINVAL;
0742
0743 gtr_phy->type = phy_types[phy_instance];
0744 return 0;
0745 }
0746
0747
0748
0749
0750 static const unsigned int icm_matrix[NUM_LANES][CONTROLLERS_PER_LANE] = {
0751 { XPSGTR_TYPE_PCIE_0, XPSGTR_TYPE_SATA_0, XPSGTR_TYPE_USB0,
0752 XPSGTR_TYPE_DP_1, XPSGTR_TYPE_SGMII0 },
0753 { XPSGTR_TYPE_PCIE_1, XPSGTR_TYPE_SATA_1, XPSGTR_TYPE_USB0,
0754 XPSGTR_TYPE_DP_0, XPSGTR_TYPE_SGMII1 },
0755 { XPSGTR_TYPE_PCIE_2, XPSGTR_TYPE_SATA_0, XPSGTR_TYPE_USB0,
0756 XPSGTR_TYPE_DP_1, XPSGTR_TYPE_SGMII2 },
0757 { XPSGTR_TYPE_PCIE_3, XPSGTR_TYPE_SATA_1, XPSGTR_TYPE_USB1,
0758 XPSGTR_TYPE_DP_0, XPSGTR_TYPE_SGMII3 }
0759 };
0760
0761
0762 static struct phy *xpsgtr_xlate(struct device *dev,
0763 struct of_phandle_args *args)
0764 {
0765 struct xpsgtr_dev *gtr_dev = dev_get_drvdata(dev);
0766 struct xpsgtr_phy *gtr_phy;
0767 unsigned int phy_instance;
0768 unsigned int phy_lane;
0769 unsigned int phy_type;
0770 unsigned int refclk;
0771 unsigned int i;
0772 int ret;
0773
0774 if (args->args_count != 4) {
0775 dev_err(dev, "Invalid number of cells in 'phy' property\n");
0776 return ERR_PTR(-EINVAL);
0777 }
0778
0779
0780
0781
0782
0783 phy_lane = args->args[0];
0784 if (phy_lane >= ARRAY_SIZE(gtr_dev->phys)) {
0785 dev_err(dev, "Invalid lane number %u\n", phy_lane);
0786 return ERR_PTR(-ENODEV);
0787 }
0788
0789 gtr_phy = >r_dev->phys[phy_lane];
0790 phy_type = args->args[1];
0791 phy_instance = args->args[2];
0792
0793 ret = xpsgtr_set_lane_type(gtr_phy, phy_type, phy_instance);
0794 if (ret < 0) {
0795 dev_err(gtr_dev->dev, "Invalid PHY type and/or instance\n");
0796 return ERR_PTR(ret);
0797 }
0798
0799 refclk = args->args[3];
0800 if (refclk >= ARRAY_SIZE(gtr_dev->refclk_sscs) ||
0801 !gtr_dev->refclk_sscs[refclk]) {
0802 dev_err(dev, "Invalid reference clock number %u\n", refclk);
0803 return ERR_PTR(-EINVAL);
0804 }
0805
0806 gtr_phy->refclk = refclk;
0807
0808
0809
0810
0811
0812 for (i = 0; i < CONTROLLERS_PER_LANE; i++) {
0813 if (icm_matrix[phy_lane][i] == gtr_phy->type)
0814 return gtr_phy->phy;
0815 }
0816
0817 return ERR_PTR(-EINVAL);
0818 }
0819
0820
0821
0822
0823
0824 static int __maybe_unused xpsgtr_suspend(struct device *dev)
0825 {
0826 struct xpsgtr_dev *gtr_dev = dev_get_drvdata(dev);
0827 unsigned int i;
0828
0829
0830 gtr_dev->saved_icm_cfg0 = xpsgtr_read(gtr_dev, ICM_CFG0);
0831 gtr_dev->saved_icm_cfg1 = xpsgtr_read(gtr_dev, ICM_CFG1);
0832
0833 for (i = 0; i < ARRAY_SIZE(gtr_dev->clk); i++)
0834 clk_disable_unprepare(gtr_dev->clk[i]);
0835
0836 return 0;
0837 }
0838
0839 static int __maybe_unused xpsgtr_resume(struct device *dev)
0840 {
0841 struct xpsgtr_dev *gtr_dev = dev_get_drvdata(dev);
0842 unsigned int icm_cfg0, icm_cfg1;
0843 unsigned int i;
0844 bool skip_phy_init;
0845 int err;
0846
0847 for (i = 0; i < ARRAY_SIZE(gtr_dev->clk); i++) {
0848 err = clk_prepare_enable(gtr_dev->clk[i]);
0849 if (err)
0850 goto err_clk_put;
0851 }
0852
0853 icm_cfg0 = xpsgtr_read(gtr_dev, ICM_CFG0);
0854 icm_cfg1 = xpsgtr_read(gtr_dev, ICM_CFG1);
0855
0856
0857 if (!gtr_dev->saved_icm_cfg0 && !gtr_dev->saved_icm_cfg1)
0858 return 0;
0859
0860
0861 if (icm_cfg0 == gtr_dev->saved_icm_cfg0 &&
0862 icm_cfg1 == gtr_dev->saved_icm_cfg1)
0863 skip_phy_init = true;
0864 else
0865 skip_phy_init = false;
0866
0867
0868 for (i = 0; i < ARRAY_SIZE(gtr_dev->phys); i++)
0869 gtr_dev->phys[i].skip_phy_init = skip_phy_init;
0870
0871 return 0;
0872
0873 err_clk_put:
0874 while (i--)
0875 clk_disable_unprepare(gtr_dev->clk[i]);
0876
0877 return err;
0878 }
0879
0880 static const struct dev_pm_ops xpsgtr_pm_ops = {
0881 SET_SYSTEM_SLEEP_PM_OPS(xpsgtr_suspend, xpsgtr_resume)
0882 };
0883
0884
0885
0886
0887
0888 static int xpsgtr_get_ref_clocks(struct xpsgtr_dev *gtr_dev)
0889 {
0890 unsigned int refclk;
0891 int ret;
0892
0893 for (refclk = 0; refclk < ARRAY_SIZE(gtr_dev->refclk_sscs); ++refclk) {
0894 unsigned long rate;
0895 unsigned int i;
0896 struct clk *clk;
0897 char name[8];
0898
0899 snprintf(name, sizeof(name), "ref%u", refclk);
0900 clk = devm_clk_get_optional(gtr_dev->dev, name);
0901 if (IS_ERR(clk)) {
0902 ret = dev_err_probe(gtr_dev->dev, PTR_ERR(clk),
0903 "Failed to get reference clock %u\n",
0904 refclk);
0905 goto err_clk_put;
0906 }
0907
0908 if (!clk)
0909 continue;
0910
0911 ret = clk_prepare_enable(clk);
0912 if (ret)
0913 goto err_clk_put;
0914
0915 gtr_dev->clk[refclk] = clk;
0916
0917
0918
0919
0920
0921 rate = clk_get_rate(clk);
0922
0923 for (i = 0 ; i < ARRAY_SIZE(ssc_lookup); i++) {
0924 if (rate == ssc_lookup[i].refclk_rate) {
0925 gtr_dev->refclk_sscs[refclk] = &ssc_lookup[i];
0926 break;
0927 }
0928 }
0929
0930 if (i == ARRAY_SIZE(ssc_lookup)) {
0931 dev_err(gtr_dev->dev,
0932 "Invalid rate %lu for reference clock %u\n",
0933 rate, refclk);
0934 ret = -EINVAL;
0935 goto err_clk_put;
0936 }
0937 }
0938
0939 return 0;
0940
0941 err_clk_put:
0942 while (refclk--)
0943 clk_disable_unprepare(gtr_dev->clk[refclk]);
0944
0945 return ret;
0946 }
0947
0948 static int xpsgtr_probe(struct platform_device *pdev)
0949 {
0950 struct device_node *np = pdev->dev.of_node;
0951 struct xpsgtr_dev *gtr_dev;
0952 struct phy_provider *provider;
0953 unsigned int port;
0954 unsigned int i;
0955 int ret;
0956
0957 gtr_dev = devm_kzalloc(&pdev->dev, sizeof(*gtr_dev), GFP_KERNEL);
0958 if (!gtr_dev)
0959 return -ENOMEM;
0960
0961 gtr_dev->dev = &pdev->dev;
0962 platform_set_drvdata(pdev, gtr_dev);
0963
0964 mutex_init(>r_dev->gtr_mutex);
0965
0966 if (of_device_is_compatible(np, "xlnx,zynqmp-psgtr"))
0967 gtr_dev->tx_term_fix =
0968 of_property_read_bool(np, "xlnx,tx-termination-fix");
0969
0970
0971 gtr_dev->serdes = devm_platform_ioremap_resource_byname(pdev, "serdes");
0972 if (IS_ERR(gtr_dev->serdes))
0973 return PTR_ERR(gtr_dev->serdes);
0974
0975 gtr_dev->siou = devm_platform_ioremap_resource_byname(pdev, "siou");
0976 if (IS_ERR(gtr_dev->siou))
0977 return PTR_ERR(gtr_dev->siou);
0978
0979 ret = xpsgtr_get_ref_clocks(gtr_dev);
0980 if (ret)
0981 return ret;
0982
0983
0984 for (port = 0; port < ARRAY_SIZE(gtr_dev->phys); ++port) {
0985 struct xpsgtr_phy *gtr_phy = >r_dev->phys[port];
0986 struct phy *phy;
0987
0988 gtr_phy->lane = port;
0989 gtr_phy->dev = gtr_dev;
0990
0991 phy = devm_phy_create(&pdev->dev, np, &xpsgtr_phyops);
0992 if (IS_ERR(phy)) {
0993 dev_err(&pdev->dev, "failed to create PHY\n");
0994 ret = PTR_ERR(phy);
0995 goto err_clk_put;
0996 }
0997
0998 gtr_phy->phy = phy;
0999 phy_set_drvdata(phy, gtr_phy);
1000 }
1001
1002
1003 provider = devm_of_phy_provider_register(&pdev->dev, xpsgtr_xlate);
1004 if (IS_ERR(provider)) {
1005 dev_err(&pdev->dev, "registering provider failed\n");
1006 ret = PTR_ERR(provider);
1007 goto err_clk_put;
1008 }
1009 return 0;
1010
1011 err_clk_put:
1012 for (i = 0; i < ARRAY_SIZE(gtr_dev->clk); i++)
1013 clk_disable_unprepare(gtr_dev->clk[i]);
1014
1015 return ret;
1016 }
1017
1018 static const struct of_device_id xpsgtr_of_match[] = {
1019 { .compatible = "xlnx,zynqmp-psgtr", },
1020 { .compatible = "xlnx,zynqmp-psgtr-v1.1", },
1021 {},
1022 };
1023 MODULE_DEVICE_TABLE(of, xpsgtr_of_match);
1024
1025 static struct platform_driver xpsgtr_driver = {
1026 .probe = xpsgtr_probe,
1027 .driver = {
1028 .name = "xilinx-psgtr",
1029 .of_match_table = xpsgtr_of_match,
1030 .pm = &xpsgtr_pm_ops,
1031 },
1032 };
1033
1034 module_platform_driver(xpsgtr_driver);
1035
1036 MODULE_AUTHOR("Xilinx Inc.");
1037 MODULE_LICENSE("GPL v2");
1038 MODULE_DESCRIPTION("Xilinx ZynqMP High speed Gigabit Transceiver");