1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2012 Freescale Semiconductor, Inc.
8 #include <asm/fsl_serdes.h>
9 #include <asm/immap_85xx.h>
11 #include <asm/processor.h>
12 #include <asm/fsl_law.h>
13 #include <linux/delay.h>
14 #include <linux/errno.h>
15 #include <fsl_errata.h>
16 #include "fsl_corenet2_serdes.h"
18 #ifdef CONFIG_SYS_FSL_SRDS_1
19 static u8 serdes1_prtcl_map[SERDES_PRCTL_COUNT];
21 #ifdef CONFIG_SYS_FSL_SRDS_2
22 static u8 serdes2_prtcl_map[SERDES_PRCTL_COUNT];
24 #ifdef CONFIG_SYS_FSL_SRDS_3
25 static u8 serdes3_prtcl_map[SERDES_PRCTL_COUNT];
27 #ifdef CONFIG_SYS_FSL_SRDS_4
28 static u8 serdes4_prtcl_map[SERDES_PRCTL_COUNT];
32 static const char *serdes_prtcl_str[] = {
42 [SGMII_FM1_DTSEC1] = "SGMII_FM1_DTSEC1",
43 [SGMII_FM1_DTSEC2] = "SGMII_FM1_DTSEC2",
44 [SGMII_FM1_DTSEC3] = "SGMII_FM1_DTSEC3",
45 [SGMII_FM1_DTSEC4] = "SGMII_FM1_DTSEC4",
46 [SGMII_FM1_DTSEC5] = "SGMII_FM1_DTSEC5",
47 [SGMII_FM1_DTSEC6] = "SGMII_FM1_DTSEC6",
48 [SGMII_FM2_DTSEC1] = "SGMII_FM2_DTSEC1",
49 [SGMII_FM2_DTSEC2] = "SGMII_FM2_DTSEC2",
50 [SGMII_FM2_DTSEC3] = "SGMII_FM2_DTSEC3",
51 [SGMII_FM2_DTSEC4] = "SGMII_FM2_DTSEC4",
52 [XAUI_FM1] = "XAUI_FM1",
53 [XAUI_FM2] = "XAUI_FM2",
63 [XAUI_FM1_MAC9] = "XAUI_FM1_MAC9",
64 [XAUI_FM1_MAC10] = "XAUI_FM1_MAC10",
65 [XAUI_FM2_MAC9] = "XAUI_FM2_MAC9",
66 [XAUI_FM2_MAC10] = "XAUI_FM2_MAC10",
67 [HIGIG_FM1_MAC9] = "HiGig_FM1_MAC9",
68 [HIGIG_FM1_MAC10] = "HiGig_FM1_MAC10",
69 [HIGIG_FM2_MAC9] = "HiGig_FM2_MAC9",
70 [HIGIG_FM2_MAC10] = "HiGig_FM2_MAC10",
71 [QSGMII_FM1_A] = "QSGMII_FM1_A",
72 [QSGMII_FM1_B] = "QSGMII_FM1_B",
73 [QSGMII_FM2_A] = "QSGMII_FM2_A",
74 [QSGMII_FM2_B] = "QSGMII_FM2_B",
75 [XFI_FM1_MAC9] = "XFI_FM1_MAC9",
76 [XFI_FM1_MAC10] = "XFI_FM1_MAC10",
77 [XFI_FM2_MAC9] = "XFI_FM2_MAC9",
78 [XFI_FM2_MAC10] = "XFI_FM2_MAC10",
79 [INTERLAKEN] = "INTERLAKEN",
80 [QSGMII_SW1_A] = "QSGMII_SW1_A",
81 [QSGMII_SW1_B] = "QSGMII_SW1_B",
82 [SGMII_SW1_MAC1] = "SGMII_SW1_MAC1",
83 [SGMII_SW1_MAC2] = "SGMII_SW1_MAC2",
84 [SGMII_SW1_MAC3] = "SGMII_SW1_MAC3",
85 [SGMII_SW1_MAC4] = "SGMII_SW1_MAC4",
86 [SGMII_SW1_MAC5] = "SGMII_SW1_MAC5",
87 [SGMII_SW1_MAC6] = "SGMII_SW1_MAC6",
91 int is_serdes_configured(enum srds_prtcl device)
95 #ifdef CONFIG_SYS_FSL_SRDS_1
96 if (!serdes1_prtcl_map[NONE])
99 ret |= serdes1_prtcl_map[device];
101 #ifdef CONFIG_SYS_FSL_SRDS_2
102 if (!serdes2_prtcl_map[NONE])
105 ret |= serdes2_prtcl_map[device];
107 #ifdef CONFIG_SYS_FSL_SRDS_3
108 if (!serdes3_prtcl_map[NONE])
111 ret |= serdes3_prtcl_map[device];
113 #ifdef CONFIG_SYS_FSL_SRDS_4
114 if (!serdes4_prtcl_map[NONE])
117 ret |= serdes4_prtcl_map[device];
123 int serdes_get_first_lane(u32 sd, enum srds_prtcl device)
125 const ccsr_gur_t *gur = (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
126 u32 cfg = in_be32(&gur->rcwsr[4]);
130 #ifdef CONFIG_SYS_FSL_SRDS_1
132 cfg &= FSL_CORENET2_RCWSR4_SRDS1_PRTCL;
133 cfg >>= FSL_CORENET2_RCWSR4_SRDS1_PRTCL_SHIFT;
136 #ifdef CONFIG_SYS_FSL_SRDS_2
138 cfg &= FSL_CORENET2_RCWSR4_SRDS2_PRTCL;
139 cfg >>= FSL_CORENET2_RCWSR4_SRDS2_PRTCL_SHIFT;
142 #ifdef CONFIG_SYS_FSL_SRDS_3
144 cfg &= FSL_CORENET2_RCWSR4_SRDS3_PRTCL;
145 cfg >>= FSL_CORENET2_RCWSR4_SRDS3_PRTCL_SHIFT;
148 #ifdef CONFIG_SYS_FSL_SRDS_4
150 cfg &= FSL_CORENET2_RCWSR4_SRDS4_PRTCL;
151 cfg >>= FSL_CORENET2_RCWSR4_SRDS4_PRTCL_SHIFT;
155 printf("invalid SerDes%d\n", sd);
158 /* Is serdes enabled at all? */
159 if (unlikely(cfg == 0))
162 for (i = 0; i < SRDS_MAX_LANES; i++) {
163 if (serdes_get_prtcl(sd, cfg, i) == device)
183 #define FUSE_VAL_MASK 0x00000003
184 #define FUSE_VAL_SHIFT 30
185 #define CR0_DCBIAS_SHIFT 5
186 #define CR1_FCAP_SHIFT 15
187 #define CR1_BCAP_SHIFT 29
188 #define FCAP_MASK 0x001F8000
189 #define BCAP_MASK 0x20000000
190 #define BCAP_OVD_MASK 0x10000000
191 #define BYP_CAL_MASK 0x02000000
193 void serdes_init(u32 sd, u32 sd_addr, u32 sd_prctl_mask, u32 sd_prctl_shift,
194 u8 serdes_prtcl_map[SERDES_PRCTL_COUNT])
196 ccsr_gur_t *gur = (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
200 if (serdes_prtcl_map[NONE])
203 memset(serdes_prtcl_map, 0, sizeof(u8) * SERDES_PRCTL_COUNT);
204 #ifdef CONFIG_SYS_FSL_ERRATUM_A007186
205 struct ccsr_sfp_regs __iomem *sfp_regs =
206 (struct ccsr_sfp_regs __iomem *)(CONFIG_SYS_SFP_ADDR);
207 u32 pll_num, pll_status, bc, dc, fc, pll_cr_upd, pll_cr0, pll_cr1;
208 u32 bc_status, fc_status, dc_status, pll_sr2;
209 serdes_corenet_t __iomem *srds_regs = (void *)sd_addr;
213 cfg = in_be32(&gur->rcwsr[4]) & sd_prctl_mask;
216 * Freescale Scratch Pad Fuse Register n (SFP_FSPFR0)
217 * The workaround requires factory pre-set SerDes calibration values to be
218 * read from a fuse block(Freescale Scratch Pad Fuse Register SFP_FSPFR0)
219 * These values have been shown to work across the
220 * entire temperature range for all SerDes. These values are then written into
221 * the SerDes registers to calibrate the SerDes PLL.
223 * This workaround for the protocols and rates that only have the Ring VCO.
225 #ifdef CONFIG_SYS_FSL_ERRATUM_A007186
226 sfp_spfr0 = in_be32(&sfp_regs->fsl_spfr0);
227 debug("A007186: sfp_spfr0= %x\n", sfp_spfr0);
229 sel = (sfp_spfr0 >> FUSE_VAL_SHIFT) & FUSE_VAL_MASK;
231 if (has_erratum_a007186() && (sel == 0x01 || sel == 0x02)) {
232 for (pll_num = 0; pll_num < SRDS_MAX_BANK; pll_num++) {
233 pll_status = in_be32(&srds_regs->bank[pll_num].pllcr0);
234 debug("A007186: pll_num=%x pllcr0=%x\n",
235 pll_num, pll_status);
237 /* Read factory pre-set SerDes calibration values
238 * from fuse block(SFP scratch register-sfp_spfr0)
240 switch (pll_status & SRDS_PLLCR0_FRATE_SEL_MASK) {
241 case SRDS_PLLCR0_FRATE_SEL_3_0:
242 case SRDS_PLLCR0_FRATE_SEL_3_072:
243 debug("A007186: 3.0/3.072 protocol rate\n");
244 bc = (sfp_spfr0 >> BC1_SHIFT) & BC_MASK;
245 dc = (sfp_spfr0 >> DC1_SHIFT) & DC_MASK;
246 fc = (sfp_spfr0 >> FC1_SHIFT) & FC_MASK;
248 case SRDS_PLLCR0_FRATE_SEL_3_125:
249 debug("A007186: 3.125 protocol rate\n");
250 bc = (sfp_spfr0 >> BC2_SHIFT) & BC_MASK;
251 dc = (sfp_spfr0 >> DC2_SHIFT) & DC_MASK;
252 fc = (sfp_spfr0 >> FC2_SHIFT) & FC_MASK;
254 case SRDS_PLLCR0_FRATE_SEL_3_75:
255 debug("A007186: 3.75 protocol rate\n");
256 bc = (sfp_spfr0 >> BC1_SHIFT) & BC_MASK;
257 dc = (sfp_spfr0 >> DC1_SHIFT) & DC_MASK;
258 fc = (sfp_spfr0 >> FC1_SHIFT) & FC_MASK;
265 /* Write SRDSxPLLnCR1[11:16] = FC
266 * Write SRDSxPLLnCR1[2] = BC
268 pll_cr1 = in_be32(&srds_regs->bank[pll_num].pllcr1);
269 pll_cr_upd = (((bc << CR1_BCAP_SHIFT) & BCAP_MASK) |
270 ((fc << CR1_FCAP_SHIFT) & FCAP_MASK));
271 out_be32(&srds_regs->bank[pll_num].pllcr1,
272 (pll_cr_upd | pll_cr1));
273 debug("A007186: pll_num=%x Updated PLLCR1=%x\n",
274 pll_num, (pll_cr_upd | pll_cr1));
275 /* Write SRDSxPLLnCR0[24:26] = DC
277 pll_cr0 = in_be32(&srds_regs->bank[pll_num].pllcr0);
278 out_be32(&srds_regs->bank[pll_num].pllcr0,
279 pll_cr0 | (dc << CR0_DCBIAS_SHIFT));
280 debug("A007186: pll_num=%x, Updated PLLCR0=%x\n",
281 pll_num, (pll_cr0 | (dc << CR0_DCBIAS_SHIFT)));
282 /* Write SRDSxPLLnCR1[3] = 1
283 * Write SRDSxPLLnCR1[6] = 1
285 pll_cr1 = in_be32(&srds_regs->bank[pll_num].pllcr1);
286 pll_cr_upd = (BCAP_OVD_MASK | BYP_CAL_MASK);
287 out_be32(&srds_regs->bank[pll_num].pllcr1,
288 (pll_cr_upd | pll_cr1));
289 debug("A007186: pll_num=%x Updated PLLCR1=%x\n",
290 pll_num, (pll_cr_upd | pll_cr1));
293 /* Read the status Registers */
294 /* Verify SRDSxPLLnSR2[8] = BC */
295 pll_sr2 = in_be32(&srds_regs->bank[pll_num].pllsr2);
296 debug("A007186: pll_num=%x pllsr2=%x\n",
298 bc_status = (pll_sr2 >> 23) & BC_MASK;
300 debug("BC mismatch\n");
301 fc_status = (pll_sr2 >> 16) & FC_MASK;
303 debug("FC mismatch\n");
304 pll_cr0 = in_be32(&srds_regs->bank[pll_num].pllcr0);
305 out_be32(&srds_regs->bank[pll_num].pllcr0, pll_cr0 |
307 pll_sr2 = in_be32(&srds_regs->bank[pll_num].pllsr2);
308 dc_status = (pll_sr2 >> 17) & DC_MASK;
310 debug("DC mismatch\n");
311 pll_cr0 = in_be32(&srds_regs->bank[pll_num].pllcr0);
312 out_be32(&srds_regs->bank[pll_num].pllcr0, pll_cr0 &
316 /* Wait 750us to verify the PLL is locked
317 * by checking SRDSxPLLnCR0[8] = 1.
320 pll_status = in_be32(&srds_regs->bank[pll_num].pllcr0);
321 debug("A007186: pll_num=%x pllcr0=%x\n",
322 pll_num, pll_status);
324 if ((pll_status & SRDS_PLLCR0_PLL_LCK) == 0)
325 printf("A007186 Serdes PLL not locked\n");
327 debug("A007186 Serdes PLL locked\n");
332 cfg >>= sd_prctl_shift;
333 printf("Using SERDES%d Protocol: %d (0x%x)\n", sd + 1, cfg, cfg);
334 if (!is_serdes_prtcl_valid(sd, cfg))
335 printf("SERDES%d[PRTCL] = 0x%x is not valid\n", sd + 1, cfg);
337 for (lane = 0; lane < SRDS_MAX_LANES; lane++) {
338 enum srds_prtcl lane_prtcl = serdes_get_prtcl(sd, cfg, lane);
339 if (unlikely(lane_prtcl >= SERDES_PRCTL_COUNT))
340 debug("Unknown SerDes lane protocol %d\n", lane_prtcl);
342 serdes_prtcl_map[lane_prtcl] = 1;
345 /* Set the first element to indicate serdes has been initialized */
346 serdes_prtcl_map[NONE] = 1;
349 void fsl_serdes_init(void)
352 #ifdef CONFIG_SYS_FSL_SRDS_1
353 serdes_init(FSL_SRDS_1,
354 CONFIG_SYS_FSL_CORENET_SERDES_ADDR,
355 FSL_CORENET2_RCWSR4_SRDS1_PRTCL,
356 FSL_CORENET2_RCWSR4_SRDS1_PRTCL_SHIFT,
359 #ifdef CONFIG_SYS_FSL_SRDS_2
360 serdes_init(FSL_SRDS_2,
361 CONFIG_SYS_FSL_CORENET_SERDES_ADDR + FSL_SRDS_2 * 0x1000,
362 FSL_CORENET2_RCWSR4_SRDS2_PRTCL,
363 FSL_CORENET2_RCWSR4_SRDS2_PRTCL_SHIFT,
366 #ifdef CONFIG_SYS_FSL_SRDS_3
367 serdes_init(FSL_SRDS_3,
368 CONFIG_SYS_FSL_CORENET_SERDES_ADDR + FSL_SRDS_3 * 0x1000,
369 FSL_CORENET2_RCWSR4_SRDS3_PRTCL,
370 FSL_CORENET2_RCWSR4_SRDS3_PRTCL_SHIFT,
373 #ifdef CONFIG_SYS_FSL_SRDS_4
374 serdes_init(FSL_SRDS_4,
375 CONFIG_SYS_FSL_CORENET_SERDES_ADDR + FSL_SRDS_4 * 0x1000,
376 FSL_CORENET2_RCWSR4_SRDS4_PRTCL,
377 FSL_CORENET2_RCWSR4_SRDS4_PRTCL_SHIFT,
383 const char *serdes_clock_to_string(u32 clock)
386 case SRDS_PLLCR0_RFCK_SEL_100:
388 case SRDS_PLLCR0_RFCK_SEL_125:
390 case SRDS_PLLCR0_RFCK_SEL_156_25:
392 case SRDS_PLLCR0_RFCK_SEL_161_13:
393 return "161.1328123";
395 #if defined(CONFIG_TARGET_T4240QDS)