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