1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014-2015 Freescale Semiconductor, Inc.
8 #include <linux/errno.h>
9 #include <asm/arch/fsl_serdes.h>
10 #include <asm/arch/soc.h>
11 #include <fsl-mc/ldpaa_wriop.h>
13 #ifdef CONFIG_SYS_FSL_SRDS_1
14 static u8 serdes1_prtcl_map[SERDES_PRCTL_COUNT];
16 #ifdef CONFIG_SYS_FSL_SRDS_2
17 static u8 serdes2_prtcl_map[SERDES_PRCTL_COUNT];
20 #if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
21 int xfi_dpmac[XFI8 + 1];
22 int sgmii_dpmac[SGMII16 + 1];
25 __weak void wriop_init_dpmac_qsgmii(int sd, int lane_prtcl)
31 *The return value of this func is the serdes protocol used.
32 *Typically this function is called number of times depending
33 *upon the number of serdes blocks in the Silicon.
34 *Zero is used to denote that no serdes was enabled,
35 *this is the case when golden RCW was used where DPAA2 bring was
36 *intentionally removed to achieve boot to prompt
39 __weak int serdes_get_number(int serdes, int cfg)
44 int is_serdes_configured(enum srds_prtcl device)
48 #ifdef CONFIG_SYS_FSL_SRDS_1
49 if (!serdes1_prtcl_map[NONE])
52 ret |= serdes1_prtcl_map[device];
54 #ifdef CONFIG_SYS_FSL_SRDS_2
55 if (!serdes2_prtcl_map[NONE])
58 ret |= serdes2_prtcl_map[device];
64 int serdes_get_first_lane(u32 sd, enum srds_prtcl device)
66 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
71 #ifdef CONFIG_SYS_FSL_SRDS_1
73 cfg = gur_in32(&gur->rcwsr[FSL_CHASSIS3_SRDS1_REGSR - 1]);
74 cfg &= FSL_CHASSIS3_SRDS1_PRTCL_MASK;
75 cfg >>= FSL_CHASSIS3_SRDS1_PRTCL_SHIFT;
78 #ifdef CONFIG_SYS_FSL_SRDS_2
80 cfg = gur_in32(&gur->rcwsr[FSL_CHASSIS3_SRDS2_REGSR - 1]);
81 cfg &= FSL_CHASSIS3_SRDS2_PRTCL_MASK;
82 cfg >>= FSL_CHASSIS3_SRDS2_PRTCL_SHIFT;
86 printf("invalid SerDes%d\n", sd);
90 cfg = serdes_get_number(sd, cfg);
92 /* Is serdes enabled at all? */
96 for (i = 0; i < SRDS_MAX_LANES; i++) {
97 if (serdes_get_prtcl(sd, cfg, i) == device)
104 void serdes_init(u32 sd, u32 sd_addr, u32 rcwsr, u32 sd_prctl_mask,
105 u32 sd_prctl_shift, u8 serdes_prtcl_map[SERDES_PRCTL_COUNT])
107 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
111 if (serdes_prtcl_map[NONE])
114 memset(serdes_prtcl_map, 0, sizeof(u8) * SERDES_PRCTL_COUNT);
116 cfg = gur_in32(&gur->rcwsr[rcwsr - 1]) & sd_prctl_mask;
117 cfg >>= sd_prctl_shift;
119 cfg = serdes_get_number(sd, cfg);
120 printf("Using SERDES%d Protocol: %d (0x%x)\n", sd + 1, cfg, cfg);
122 if (!is_serdes_prtcl_valid(sd, cfg))
123 printf("SERDES%d[PRTCL] = 0x%x is not valid\n", sd + 1, cfg);
125 for (lane = 0; lane < SRDS_MAX_LANES; lane++) {
126 enum srds_prtcl lane_prtcl = serdes_get_prtcl(sd, cfg, lane);
127 if (unlikely(lane_prtcl >= SERDES_PRCTL_COUNT))
128 debug("Unknown SerDes lane protocol %d\n", lane_prtcl);
130 serdes_prtcl_map[lane_prtcl] = 1;
131 #if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
132 switch (lane_prtcl) {
137 wriop_init_dpmac_qsgmii(sd, (int)lane_prtcl);
140 if (lane_prtcl >= XFI1 && lane_prtcl <= XFI8)
142 xfi_dpmac[lane_prtcl],
145 if (lane_prtcl >= SGMII1 &&
146 lane_prtcl <= SGMII16)
147 wriop_init_dpmac(sd, sgmii_dpmac[
156 /* Set the first element to indicate serdes has been initialized */
157 serdes_prtcl_map[NONE] = 1;
160 __weak int get_serdes_volt(void)
165 __weak int set_serdes_volt(int svdd)
170 #define LNAGCR0_RT_RSTB 0x00600000
172 #define RSTCTL_RESET_MASK 0x000000E0
174 #define RSTCTL_RSTREQ 0x80000000
175 #define RSTCTL_RST_DONE 0x40000000
176 #define RSTCTL_RSTERR 0x20000000
178 #define RSTCTL_SDEN 0x00000020
179 #define RSTCTL_SDRST_B 0x00000040
180 #define RSTCTL_PLLRST_B 0x00000080
182 #define TCALCR_CALRST_B 0x08000000
184 struct serdes_prctl_info {
190 struct serdes_prctl_info srds_prctl_info[] = {
191 #ifdef CONFIG_SYS_FSL_SRDS_1
193 .mask = FSL_CHASSIS3_SRDS1_PRTCL_MASK,
194 .shift = FSL_CHASSIS3_SRDS1_PRTCL_SHIFT
198 #ifdef CONFIG_SYS_FSL_SRDS_2
200 .mask = FSL_CHASSIS3_SRDS2_PRTCL_MASK,
201 .shift = FSL_CHASSIS3_SRDS2_PRTCL_SHIFT
207 static int get_serdes_prctl_info_idx(u32 serdes_id)
210 struct serdes_prctl_info *srds_info;
212 /* loop until NULL ENTRY defined by .id=0 */
213 for (srds_info = srds_prctl_info; srds_info->id != 0;
214 srds_info++, pos++) {
215 if (srds_info->id == serdes_id)
222 static void do_enabled_lanes_reset(u32 serdes_id, u32 cfg,
223 struct ccsr_serdes __iomem *serdes_base,
229 pos = get_serdes_prctl_info_idx(serdes_id);
231 printf("invalid serdes_id %d\n", serdes_id);
235 cfg_tmp = cfg & srds_prctl_info[pos].mask;
236 cfg_tmp >>= srds_prctl_info[pos].shift;
238 for (i = 0; i < 4 && cfg_tmp & (0xf << (3 - i)); i++) {
240 setbits_le32(&serdes_base->lane[i].gcr0,
243 clrbits_le32(&serdes_base->lane[i].gcr0,
248 static void do_pll_reset(u32 cfg,
249 struct ccsr_serdes __iomem *serdes_base)
253 for (i = 0; i < 2 && !(cfg & (0x1 << (1 - i))); i++) {
254 clrbits_le32(&serdes_base->bank[i].rstctl,
258 setbits_le32(&serdes_base->bank[i].rstctl,
264 static void do_rx_tx_cal_reset(struct ccsr_serdes __iomem *serdes_base)
266 clrbits_le32(&serdes_base->srdstcalcr, TCALCR_CALRST_B);
267 clrbits_le32(&serdes_base->srdstcalcr, TCALCR_CALRST_B);
270 static void do_rx_tx_cal_reset_comp(u32 cfg, int i,
271 struct ccsr_serdes __iomem *serdes_base)
273 if (!(cfg == 0x3 && i == 1)) {
275 setbits_le32(&serdes_base->srdstcalcr, TCALCR_CALRST_B);
276 setbits_le32(&serdes_base->srdstcalcr, TCALCR_CALRST_B);
281 static void do_pll_reset_done(u32 cfg,
282 struct ccsr_serdes __iomem *serdes_base)
287 for (i = 0; i < 2; i++) {
288 reg = in_le32(&serdes_base->bank[i].pllcr0);
289 if (!(cfg & (0x1 << (1 - i))) && ((reg >> 23) & 0x1)) {
290 setbits_le32(&serdes_base->bank[i].rstctl,
296 static void do_serdes_enable(u32 cfg,
297 struct ccsr_serdes __iomem *serdes_base)
301 for (i = 0; i < 2 && !(cfg & (0x1 << (1 - i))); i++) {
302 setbits_le32(&serdes_base->bank[i].rstctl, RSTCTL_SDEN);
305 setbits_le32(&serdes_base->bank[i].rstctl, RSTCTL_PLLRST_B);
307 /* Take the Rx/Tx calibration out of reset */
308 do_rx_tx_cal_reset_comp(cfg, i, serdes_base);
312 static void do_pll_lock(u32 cfg,
313 struct ccsr_serdes __iomem *serdes_base)
318 for (i = 0; i < 2 && !(cfg & (0x1 << (1 - i))); i++) {
319 /* if the PLL is not locked, set RST_ERR */
320 reg = in_le32(&serdes_base->bank[i].pllcr0);
321 if (!((reg >> 23) & 0x1)) {
322 setbits_le32(&serdes_base->bank[i].rstctl,
326 setbits_le32(&serdes_base->bank[i].rstctl,
333 int setup_serdes_volt(u32 svdd)
335 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
336 struct ccsr_serdes __iomem *serdes1_base =
337 (void *)CONFIG_SYS_FSL_LSCH3_SERDES_ADDR;
338 u32 cfg_rcwsrds1 = gur_in32(&gur->rcwsr[FSL_CHASSIS3_SRDS1_REGSR - 1]);
339 #ifdef CONFIG_SYS_FSL_SRDS_2
340 struct ccsr_serdes __iomem *serdes2_base =
341 (void *)(CONFIG_SYS_FSL_LSCH3_SERDES_ADDR + 0x10000);
342 u32 cfg_rcwsrds2 = gur_in32(&gur->rcwsr[FSL_CHASSIS3_SRDS2_REGSR - 1]);
345 int svdd_cur, svdd_tar;
348 /* Only support switch SVDD to 900mV */
352 /* Scale up to the LTC resolution is 1/4096V */
353 svdd = (svdd * 4096) / 1000;
356 svdd_cur = get_serdes_volt();
360 debug("%s: current SVDD: %x; target SVDD: %x\n",
361 __func__, svdd_cur, svdd_tar);
362 if (svdd_cur == svdd_tar)
365 /* Put the all enabled lanes in reset */
366 #ifdef CONFIG_SYS_FSL_SRDS_1
367 do_enabled_lanes_reset(1, cfg_rcwsrds1, serdes1_base, false);
370 #ifdef CONFIG_SYS_FSL_SRDS_2
371 do_enabled_lanes_reset(2, cfg_rcwsrds2, serdes2_base, false);
374 /* Put the all enabled PLL in reset */
375 #ifdef CONFIG_SYS_FSL_SRDS_1
376 cfg_tmp = cfg_rcwsrds1 & 0x3;
377 do_pll_reset(cfg_tmp, serdes1_base);
380 #ifdef CONFIG_SYS_FSL_SRDS_2
381 cfg_tmp = cfg_rcwsrds1 & 0xC;
383 do_pll_reset(cfg_tmp, serdes2_base);
386 /* Put the Rx/Tx calibration into reset */
387 #ifdef CONFIG_SYS_FSL_SRDS_1
388 do_rx_tx_cal_reset(serdes1_base);
391 #ifdef CONFIG_SYS_FSL_SRDS_2
392 do_rx_tx_cal_reset(serdes2_base);
395 ret = set_serdes_volt(svdd);
397 printf("could not change SVDD\n");
401 /* For each PLL that’s not disabled via RCW enable the SERDES */
402 #ifdef CONFIG_SYS_FSL_SRDS_1
403 cfg_tmp = cfg_rcwsrds1 & 0x3;
404 do_serdes_enable(cfg_tmp, serdes1_base);
406 #ifdef CONFIG_SYS_FSL_SRDS_2
407 cfg_tmp = cfg_rcwsrds1 & 0xC;
409 do_serdes_enable(cfg_tmp, serdes2_base);
412 /* Wait for at at least 625us, ensure the PLLs being reset are locked */
415 #ifdef CONFIG_SYS_FSL_SRDS_1
416 cfg_tmp = cfg_rcwsrds1 & 0x3;
417 do_pll_lock(cfg_tmp, serdes1_base);
420 #ifdef CONFIG_SYS_FSL_SRDS_2
421 cfg_tmp = cfg_rcwsrds1 & 0xC;
423 do_pll_lock(cfg_tmp, serdes2_base);
425 /* Take the all enabled lanes out of reset */
426 #ifdef CONFIG_SYS_FSL_SRDS_1
427 do_enabled_lanes_reset(1, cfg_rcwsrds1, serdes1_base, true);
429 #ifdef CONFIG_SYS_FSL_SRDS_2
430 do_enabled_lanes_reset(2, cfg_rcwsrds2, serdes2_base, true);
433 /* For each PLL being reset, and achieved PLL lock set RST_DONE */
434 #ifdef CONFIG_SYS_FSL_SRDS_1
435 cfg_tmp = cfg_rcwsrds1 & 0x3;
436 do_pll_reset_done(cfg_tmp, serdes1_base);
438 #ifdef CONFIG_SYS_FSL_SRDS_2
439 cfg_tmp = cfg_rcwsrds1 & 0xC;
441 do_pll_reset_done(cfg_tmp, serdes2_base);
447 void fsl_serdes_init(void)
449 #if defined(CONFIG_FSL_MC_ENET) && !defined(CONFIG_SPL_BUILD)
452 for (i = XFI1, j = 1; i <= XFI8; i++, j++)
455 for (i = SGMII1, j = 1; i <= SGMII16; i++, j++)
459 #ifdef CONFIG_SYS_FSL_SRDS_1
460 serdes_init(FSL_SRDS_1,
461 CONFIG_SYS_FSL_LSCH3_SERDES_ADDR,
462 FSL_CHASSIS3_SRDS1_REGSR,
463 FSL_CHASSIS3_SRDS1_PRTCL_MASK,
464 FSL_CHASSIS3_SRDS1_PRTCL_SHIFT,
467 #ifdef CONFIG_SYS_FSL_SRDS_2
468 serdes_init(FSL_SRDS_2,
469 CONFIG_SYS_FSL_LSCH3_SERDES_ADDR + FSL_SRDS_2 * 0x10000,
470 FSL_CHASSIS3_SRDS2_REGSR,
471 FSL_CHASSIS3_SRDS2_PRTCL_MASK,
472 FSL_CHASSIS3_SRDS2_PRTCL_SHIFT,