blob: 9649e660220abcf87c1c550b3aa7bed0e8a47186 [file] [log] [blame]
Michal Simek0be35ab2022-01-06 09:49:41 +01001// 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 Simek0be35ab2022-01-06 09:49:41 +010012#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
Frantisek Bohacek22bc8ae2025-05-22 08:07:03 +0200141#define GEM_CLK_CTRL_WIDTH_SHIFT 5
Michal Simek0be35ab2022-01-06 09:49:41 +0100142
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 */
153enum {
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 */
Ashok Reddy Somad5a63342022-05-10 07:12:34 -0600171#define TIMEOUT_US 10000
Michal Simek0be35ab2022-01-06 09:49:41 +0100172
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 */
187struct 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 */
202struct 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 */
219struct 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 */
230static 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 */
247static u32 xpsgtr_read(struct xpsgtr_dev *gtr_dev, u32 reg)
248{
249 return readl(gtr_dev->serdes + reg);
250}
251
252static void xpsgtr_write(struct xpsgtr_dev *gtr_dev, u32 reg, u32 value)
253{
254 writel(value, gtr_dev->serdes + reg);
255}
256
257static 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
267static 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
275static 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
284static 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. */
294static 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. */
343static 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. */
370static 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
Michal Simek1c5e9912022-02-07 10:36:32 +0100376/* DP-specific initialization. */
377static void xpsgtr_phy_init_dp(struct xpsgtr_phy *gtr_phy)
378{
379 xpsgtr_write_phy(gtr_phy, L0_TXPMD_TM_45,
380 L0_TXPMD_TM_45_OVER_DP_MAIN |
381 L0_TXPMD_TM_45_ENABLE_DP_MAIN |
382 L0_TXPMD_TM_45_OVER_DP_POST1 |
383 L0_TXPMD_TM_45_OVER_DP_POST2 |
384 L0_TXPMD_TM_45_ENABLE_DP_POST2);
385 xpsgtr_write_phy(gtr_phy, L0_TX_ANA_TM_118,
386 L0_TX_ANA_TM_118_FORCE_17_0);
387}
388
389/* SATA-specific initialization. */
390static void xpsgtr_phy_init_sata(struct xpsgtr_phy *gtr_phy)
391{
392 struct xpsgtr_dev *gtr_dev = gtr_phy->dev;
393
394 xpsgtr_bypass_scrambler_8b10b(gtr_phy);
395
396 writel(gtr_phy->lane, gtr_dev->siou + SATA_CONTROL_OFFSET);
397}
398
Michal Simek0be35ab2022-01-06 09:49:41 +0100399/* SGMII-specific initialization. */
400static void xpsgtr_phy_init_sgmii(struct xpsgtr_phy *gtr_phy)
401{
402 struct xpsgtr_dev *gtr_dev = gtr_phy->dev;
403 u32 shift = gtr_phy->lane * PROT_BUS_WIDTH_SHIFT;
Frantisek Bohacek22bc8ae2025-05-22 08:07:03 +0200404 u32 clk_ctrl_shift = gtr_phy->lane * GEM_CLK_CTRL_WIDTH_SHIFT;
Michal Simek0be35ab2022-01-06 09:49:41 +0100405
406 /* Set SGMII protocol TX and RX bus width to 10 bits. */
407 xpsgtr_clr_set(gtr_dev, TX_PROT_BUS_WIDTH, PROT_BUS_WIDTH_MASK << shift,
408 PROT_BUS_WIDTH_10 << shift);
409
410 xpsgtr_clr_set(gtr_dev, RX_PROT_BUS_WIDTH, PROT_BUS_WIDTH_MASK << shift,
411 PROT_BUS_WIDTH_10 << shift);
412
413 xpsgtr_bypass_scrambler_8b10b(gtr_phy);
414
415 /*
416 * Below code is just temporary solution till we have a way how to
417 * do it via firmware interface in sync with Linux. Till that happen
418 * this is the most sensible thing to do here.
419 */
420 /* GEM I/O Clock Control */
421 clrsetbits_le32(ZYNQMP_IOU_SLCR_BASEADDR + IOU_SLCR_GEM_CLK_CTRL,
Frantisek Bohacek22bc8ae2025-05-22 08:07:03 +0200422 0xf << clk_ctrl_shift,
Michal Simek0be35ab2022-01-06 09:49:41 +0100423 (GEM_CTRL_GEM_SGMII_MODE | GEM_CTRL_GEM_REF_SRC_SEL) <<
Frantisek Bohacek22bc8ae2025-05-22 08:07:03 +0200424 clk_ctrl_shift);
Michal Simek0be35ab2022-01-06 09:49:41 +0100425
426 /* Setup signal detect */
427 clrsetbits_le32(ZYNQMP_IOU_SLCR_BASEADDR + IOU_SLCR_GEM_CTRL,
428 PROT_BUS_WIDTH_MASK << shift,
429 GEM_CTRL_GEM_SGMII_SD << shift);
430}
431
432static int xpsgtr_init(struct phy *x)
433{
434 struct xpsgtr_dev *gtr_dev = dev_get_priv(x->dev);
435 struct xpsgtr_phy *gtr_phy;
436 u32 phy_lane = x->id;
437
438 gtr_phy = &gtr_dev->phys[phy_lane];
439
440 /* Enable coarse code saturation limiting logic. */
441 xpsgtr_write_phy(gtr_phy, L0_TM_PLL_DIG_37, L0_TM_COARSE_CODE_LIMIT);
442
443 /*
444 * Configure the PLL, the lane protocol, and perform protocol-specific
445 * initialization.
446 */
447 xpsgtr_configure_pll(gtr_phy);
448 xpsgtr_lane_set_protocol(gtr_phy);
449
450 switch (gtr_phy->protocol) {
451 case ICM_PROTOCOL_SGMII:
452 xpsgtr_phy_init_sgmii(gtr_phy);
453 break;
Michal Simek0be35ab2022-01-06 09:49:41 +0100454 case ICM_PROTOCOL_SATA:
Michal Simek1c5e9912022-02-07 10:36:32 +0100455 xpsgtr_phy_init_sata(gtr_phy);
456 break;
457 case ICM_PROTOCOL_DP:
458 xpsgtr_phy_init_dp(gtr_phy);
459 break;
Michal Simek0be35ab2022-01-06 09:49:41 +0100460 }
461
462 dev_dbg(gtr_dev->dev, "lane %u (type %u, protocol %u): init done\n",
463 gtr_phy->lane, gtr_phy->type, gtr_phy->protocol);
464
465 return 0;
466}
467
468/* Wait for the PLL to lock (with a timeout). */
469static int xpsgtr_wait_pll_lock(struct phy *phy)
470{
471 struct xpsgtr_dev *gtr_dev = dev_get_priv(phy->dev);
472 struct xpsgtr_phy *gtr_phy;
473 u32 phy_lane = phy->id;
474 int ret = 0;
475 unsigned int timeout = TIMEOUT_US;
476
477 gtr_phy = &gtr_dev->phys[phy_lane];
478
479 dev_dbg(gtr_dev->dev, "Waiting for PLL lock\n");
480
481 while (1) {
482 u32 reg = xpsgtr_read_phy(gtr_phy, L0_PLL_STATUS_READ_1);
483
484 if ((reg & PLL_STATUS_LOCKED) == PLL_STATUS_LOCKED) {
485 ret = 0;
486 break;
487 }
488
489 if (--timeout == 0) {
490 ret = -ETIMEDOUT;
491 break;
492 }
493
494 udelay(1);
495 }
496
497 if (ret == -ETIMEDOUT)
498 dev_err(gtr_dev->dev,
499 "lane %u (type %u, protocol %u): PLL lock timeout\n",
500 gtr_phy->lane, gtr_phy->type, gtr_phy->protocol);
501
502 return ret;
503}
504
505static int xpsgtr_power_on(struct phy *phy)
506{
507 struct xpsgtr_dev *gtr_dev = dev_get_priv(phy->dev);
508 struct xpsgtr_phy *gtr_phy;
509 u32 phy_lane = phy->id;
510 int ret = 0;
511
512 gtr_phy = &gtr_dev->phys[phy_lane];
513
514 /*
515 * Wait for the PLL to lock. For DP, only wait on DP0 to avoid
516 * cumulating waits for both lanes. The user is expected to initialize
517 * lane 0 last.
518 */
519 if (gtr_phy->protocol != ICM_PROTOCOL_DP ||
520 gtr_phy->type == XPSGTR_TYPE_DP_0)
521 ret = xpsgtr_wait_pll_lock(phy);
522
523 return ret;
524}
525
526/*
527 * OF Xlate Support
528 */
529
530/* Set the lane type and protocol based on the PHY type and instance number. */
531static int xpsgtr_set_lane_type(struct xpsgtr_phy *gtr_phy, u8 phy_type,
532 unsigned int phy_instance)
533{
534 unsigned int num_phy_types;
535 const int *phy_types;
536
537 switch (phy_type) {
538 case PHY_TYPE_SATA: {
539 static const int types[] = {
540 XPSGTR_TYPE_SATA_0,
541 XPSGTR_TYPE_SATA_1,
542 };
543
544 phy_types = types;
545 num_phy_types = ARRAY_SIZE(types);
546 gtr_phy->protocol = ICM_PROTOCOL_SATA;
547 break;
548 }
549 case PHY_TYPE_USB3: {
550 static const int types[] = {
551 XPSGTR_TYPE_USB0,
552 XPSGTR_TYPE_USB1,
553 };
554
555 phy_types = types;
556 num_phy_types = ARRAY_SIZE(types);
557 gtr_phy->protocol = ICM_PROTOCOL_USB;
558 break;
559 }
560 case PHY_TYPE_DP: {
561 static const int types[] = {
562 XPSGTR_TYPE_DP_0,
563 XPSGTR_TYPE_DP_1,
564 };
565
566 phy_types = types;
567 num_phy_types = ARRAY_SIZE(types);
568 gtr_phy->protocol = ICM_PROTOCOL_DP;
569 break;
570 }
571 case PHY_TYPE_PCIE: {
572 static const int types[] = {
573 XPSGTR_TYPE_PCIE_0,
574 XPSGTR_TYPE_PCIE_1,
575 XPSGTR_TYPE_PCIE_2,
576 XPSGTR_TYPE_PCIE_3,
577 };
578
579 phy_types = types;
580 num_phy_types = ARRAY_SIZE(types);
581 gtr_phy->protocol = ICM_PROTOCOL_PCIE;
582 break;
583 }
584 case PHY_TYPE_SGMII: {
585 static const int types[] = {
586 XPSGTR_TYPE_SGMII0,
587 XPSGTR_TYPE_SGMII1,
588 XPSGTR_TYPE_SGMII2,
589 XPSGTR_TYPE_SGMII3,
590 };
591
592 phy_types = types;
593 num_phy_types = ARRAY_SIZE(types);
594 gtr_phy->protocol = ICM_PROTOCOL_SGMII;
595 break;
596 }
597 default:
598 return -EINVAL;
599 }
600
601 if (phy_instance >= num_phy_types)
602 return -EINVAL;
603
604 gtr_phy->type = phy_types[phy_instance];
605 return 0;
606}
607
608/*
609 * Valid combinations of controllers and lanes (Interconnect Matrix).
610 */
611static const unsigned int icm_matrix[NUM_LANES][CONTROLLERS_PER_LANE] = {
612 { XPSGTR_TYPE_PCIE_0, XPSGTR_TYPE_SATA_0, XPSGTR_TYPE_USB0,
613 XPSGTR_TYPE_DP_1, XPSGTR_TYPE_SGMII0 },
614 { XPSGTR_TYPE_PCIE_1, XPSGTR_TYPE_SATA_1, XPSGTR_TYPE_USB0,
615 XPSGTR_TYPE_DP_0, XPSGTR_TYPE_SGMII1 },
616 { XPSGTR_TYPE_PCIE_2, XPSGTR_TYPE_SATA_0, XPSGTR_TYPE_USB0,
617 XPSGTR_TYPE_DP_1, XPSGTR_TYPE_SGMII2 },
618 { XPSGTR_TYPE_PCIE_3, XPSGTR_TYPE_SATA_1, XPSGTR_TYPE_USB1,
619 XPSGTR_TYPE_DP_0, XPSGTR_TYPE_SGMII3 }
620};
621
622/* Translate OF phandle and args to PHY instance. */
623static int xpsgtr_of_xlate(struct phy *x,
624 struct ofnode_phandle_args *args)
625{
626 struct xpsgtr_dev *gtr_dev = dev_get_priv(x->dev);
627 struct xpsgtr_phy *gtr_phy;
628 struct udevice *dev = x->dev;
629 unsigned int phy_instance;
630 unsigned int phy_lane;
631 unsigned int phy_type;
632 unsigned int refclk;
633 unsigned int i;
634 int ret;
635
636 if (args->args_count != 4) {
637 dev_err(dev, "Invalid number of cells in 'phy' property\n");
638 return -EINVAL;
639 }
640
641 /*
642 * Get the PHY parameters from the OF arguments and derive the lane
643 * type.
644 */
645 phy_lane = args->args[0];
646 if (phy_lane >= NUM_LANES) {
647 dev_err(dev, "Invalid lane number %u\n", phy_lane);
648 return -EINVAL;
649 }
650
651 gtr_phy = &gtr_dev->phys[phy_lane];
652 phy_type = args->args[1];
653 phy_instance = args->args[2];
654
655 ret = xpsgtr_set_lane_type(gtr_phy, phy_type, phy_instance);
656 if (ret) {
657 dev_err(dev, "Invalid PHY type and/or instance\n");
658 return ret;
659 }
660
661 refclk = args->args[3];
662 if (refclk >= ARRAY_SIZE(gtr_dev->refclk_sscs) ||
663 !gtr_dev->refclk_sscs[refclk]) {
664 dev_err(dev, "Invalid reference clock number %u\n", refclk);
665 return -EINVAL;
666 }
667
668 gtr_phy->refclk = refclk;
669
670 /* This is difference compare to Linux */
671 gtr_phy->dev = gtr_dev;
672 gtr_phy->lane = phy_lane;
673
674 /*
675 * Ensure that the Interconnect Matrix is obeyed, i.e a given lane type
676 * is allowed to operate on the lane.
677 */
678 for (i = 0; i < CONTROLLERS_PER_LANE; i++) {
679 if (icm_matrix[phy_lane][i] == gtr_phy->type) {
680 x->id = phy_lane;
681 return 0;
682 }
683 }
684
685 return -EINVAL;
686}
687
688/*
689 * Probe & Platform Driver
690 */
691static int xpsgtr_get_ref_clocks(struct udevice *dev)
692{
693 unsigned int refclk;
694 struct xpsgtr_dev *gtr_dev = dev_get_priv(dev);
695 int ret;
696
697 for (refclk = 0; refclk < NUM_LANES; ++refclk) {
698 int i;
699 u32 rate;
700 char name[8];
701 struct clk *clk = &gtr_dev->clk[refclk];
702
703 snprintf(name, sizeof(name), "ref%u", refclk);
704 dev_dbg(dev, "Checking name: %s\n", name);
705 ret = clk_get_by_name(dev, name, clk);
706 if (ret == -ENODATA) {
707 dev_dbg(dev, "%s clock not specified (err %d)\n",
708 name, ret);
709 continue;
710 } else if (ret) {
711 dev_dbg(dev, "couldn't get clock %s (err %d)\n",
712 name, ret);
713 return ret;
714 }
715
716 rate = clk_get_rate(clk);
717
718 dev_dbg(dev, "clk rate %d\n", rate);
719
720 ret = clk_enable(clk);
721 if (ret) {
722 dev_err(dev, "failed to enable refclk %d clock\n",
723 refclk);
724 return ret;
725 }
726
727 for (i = 0 ; i < ARRAY_SIZE(ssc_lookup); i++) {
728 if (rate == ssc_lookup[i].refclk_rate) {
729 gtr_dev->refclk_sscs[refclk] = &ssc_lookup[i];
730 dev_dbg(dev, "Found rate %d\n", i);
731 break;
732 }
733 }
734
735 if (i == ARRAY_SIZE(ssc_lookup)) {
736 dev_err(dev,
737 "Invalid rate %u for reference clock %u\n",
738 rate, refclk);
739 return -EINVAL;
740 }
741 }
742
743 return 0;
744}
745
746static int xpsgtr_probe(struct udevice *dev)
747{
748 struct xpsgtr_dev *gtr_dev = dev_get_priv(dev);
749
750 gtr_dev->serdes = dev_remap_addr_name(dev, "serdes");
751 if (!gtr_dev->serdes)
752 return -EINVAL;
753
754 gtr_dev->siou = dev_remap_addr_name(dev, "siou");
755 if (!gtr_dev->siou)
756 return -EINVAL;
757
758 gtr_dev->dev = dev;
759
760 return xpsgtr_get_ref_clocks(dev);
761}
762
763static const struct udevice_id xpsgtr_phy_ids[] = {
764 { .compatible = "xlnx,zynqmp-psgtr-v1.1", },
765 { }
766};
767
768static const struct phy_ops xpsgtr_phy_ops = {
769 .init = xpsgtr_init,
770 .of_xlate = xpsgtr_of_xlate,
771 .power_on = xpsgtr_power_on,
772};
773
774U_BOOT_DRIVER(psgtr_phy) = {
775 .name = "psgtr_phy",
776 .id = UCLASS_PHY,
777 .of_match = xpsgtr_phy_ids,
778 .ops = &xpsgtr_phy_ops,
779 .probe = xpsgtr_probe,
780 .priv_auto = sizeof(struct xpsgtr_dev),
781};