e6d4707e3e99b03cfa9ef75904e7b832ab853b1b
[oweals/u-boot.git] / arch / powerpc / cpu / mpc85xx / fsl_corenet_serdes.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2009-2011 Freescale Semiconductor, Inc.
4  */
5
6 #include <common.h>
7 #include <env.h>
8 #include <log.h>
9 #include <time.h>
10 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
11 #include <hwconfig.h>
12 #endif
13 #include <asm/fsl_serdes.h>
14 #include <asm/immap_85xx.h>
15 #include <asm/io.h>
16 #include <asm/processor.h>
17 #include <asm/fsl_law.h>
18 #include <linux/errno.h>
19 #include "fsl_corenet_serdes.h"
20
21 /*
22  * The work-arounds for erratum SERDES8 and SERDES-A001 are linked together.
23  * The code is already very complicated as it is, and separating the two
24  * completely would just make things worse.  We try to keep them as separate
25  * as possible, but for now we require SERDES8 if SERDES_A001 is defined.
26  */
27 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES_A001
28 #ifndef CONFIG_SYS_P4080_ERRATUM_SERDES8
29 #error "CONFIG_SYS_P4080_ERRATUM_SERDES_A001 requires CONFIG_SYS_P4080_ERRATUM_SERDES8"
30 #endif
31 #endif
32
33 static u32 serdes_prtcl_map;
34
35 #ifdef DEBUG
36 static const char *serdes_prtcl_str[] = {
37         [NONE] = "NA",
38         [PCIE1] = "PCIE1",
39         [PCIE2] = "PCIE2",
40         [PCIE3] = "PCIE3",
41         [PCIE4] = "PCIE4",
42         [SATA1] = "SATA1",
43         [SATA2] = "SATA2",
44         [SRIO1] = "SRIO1",
45         [SRIO2] = "SRIO2",
46         [SGMII_FM1_DTSEC1] = "SGMII_FM1_DTSEC1",
47         [SGMII_FM1_DTSEC2] = "SGMII_FM1_DTSEC2",
48         [SGMII_FM1_DTSEC3] = "SGMII_FM1_DTSEC3",
49         [SGMII_FM1_DTSEC4] = "SGMII_FM1_DTSEC4",
50         [SGMII_FM1_DTSEC5] = "SGMII_FM1_DTSEC5",
51         [SGMII_FM2_DTSEC1] = "SGMII_FM2_DTSEC1",
52         [SGMII_FM2_DTSEC2] = "SGMII_FM2_DTSEC2",
53         [SGMII_FM2_DTSEC3] = "SGMII_FM2_DTSEC3",
54         [SGMII_FM2_DTSEC4] = "SGMII_FM2_DTSEC4",
55         [SGMII_FM2_DTSEC5] = "SGMII_FM2_DTSEC5",
56         [XAUI_FM1] = "XAUI_FM1",
57         [XAUI_FM2] = "XAUI_FM2",
58         [AURORA] = "DEBUG",
59 };
60 #endif
61
62 static const struct {
63         int idx;
64         unsigned int lpd; /* RCW lane powerdown bit */
65         int bank;
66 } lanes[SRDS_MAX_LANES] = {
67         { 0, 152, FSL_SRDS_BANK_1 },
68         { 1, 153, FSL_SRDS_BANK_1 },
69         { 2, 154, FSL_SRDS_BANK_1 },
70         { 3, 155, FSL_SRDS_BANK_1 },
71         { 4, 156, FSL_SRDS_BANK_1 },
72         { 5, 157, FSL_SRDS_BANK_1 },
73         { 6, 158, FSL_SRDS_BANK_1 },
74         { 7, 159, FSL_SRDS_BANK_1 },
75         { 8, 160, FSL_SRDS_BANK_1 },
76         { 9, 161, FSL_SRDS_BANK_1 },
77         { 16, 162, FSL_SRDS_BANK_2 },
78         { 17, 163, FSL_SRDS_BANK_2 },
79         { 18, 164, FSL_SRDS_BANK_2 },
80         { 19, 165, FSL_SRDS_BANK_2 },
81 #ifdef CONFIG_ARCH_P4080
82         { 20, 170, FSL_SRDS_BANK_3 },
83         { 21, 171, FSL_SRDS_BANK_3 },
84         { 22, 172, FSL_SRDS_BANK_3 },
85         { 23, 173, FSL_SRDS_BANK_3 },
86 #else
87         { 20, 166, FSL_SRDS_BANK_3 },
88         { 21, 167, FSL_SRDS_BANK_3 },
89         { 22, 168, FSL_SRDS_BANK_3 },
90         { 23, 169, FSL_SRDS_BANK_3 },
91 #endif
92 #if SRDS_MAX_BANK > 3
93         { 24, 175, FSL_SRDS_BANK_4 },
94         { 25, 176, FSL_SRDS_BANK_4 },
95 #endif
96 };
97
98 int serdes_get_lane_idx(int lane)
99 {
100         return lanes[lane].idx;
101 }
102
103 int serdes_get_bank_by_lane(int lane)
104 {
105         return lanes[lane].bank;
106 }
107
108 int serdes_lane_enabled(int lane)
109 {
110         ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
111         serdes_corenet_t *regs = (void *)CONFIG_SYS_FSL_CORENET_SERDES_ADDR;
112
113         int bank = lanes[lane].bank;
114         int word = lanes[lane].lpd / 32;
115         int bit = lanes[lane].lpd % 32;
116
117         if (in_be32(&regs->bank[bank].rstctl) & SRDS_RSTCTL_SDPD)
118                 return 0;
119
120 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
121         /*
122          * For banks two and three, use the srds_lpd_b[] array instead of the
123          * RCW, because this array contains the real values of SRDS_LPD_B2 and
124          * SRDS_LPD_B3.
125          */
126         if (bank > 0)
127                 return !(srds_lpd_b[bank] & (8 >> (lane - (6 + 4 * bank))));
128 #endif
129
130         return !(in_be32(&gur->rcwsr[word]) & (0x80000000 >> bit));
131 }
132
133 int is_serdes_configured(enum srds_prtcl device)
134 {
135         ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
136
137         /* Is serdes enabled at all? */
138         if (!(in_be32(&gur->rcwsr[5]) & FSL_CORENET_RCWSR5_SRDS_EN))
139                 return 0;
140
141         if (!(serdes_prtcl_map & (1 << NONE)))
142                 fsl_serdes_init();
143
144         return (1 << device) & serdes_prtcl_map;
145 }
146
147 static int __serdes_get_first_lane(uint32_t prtcl, enum srds_prtcl device)
148 {
149         int i;
150
151         for (i = 0; i < SRDS_MAX_LANES; i++) {
152                 if (serdes_get_prtcl(prtcl, i) == device)
153                         return i;
154         }
155
156         return -ENODEV;
157 }
158
159 /*
160  * Returns the SERDES lane (0..SRDS_MAX_LANES-1) that routes to the given
161  * device. This depends on the current SERDES protocol, as defined in the RCW.
162  *
163  * Returns a negative error code if SERDES is disabled or the given device is
164  * not supported in the current SERDES protocol.
165  */
166 int serdes_get_first_lane(enum srds_prtcl device)
167 {
168         u32 prtcl;
169         const ccsr_gur_t *gur;
170
171         gur = (typeof(gur))CONFIG_SYS_MPC85xx_GUTS_ADDR;
172
173         /* Is serdes enabled at all? */
174         if (unlikely((in_be32(&gur->rcwsr[5]) & 0x2000) == 0))
175                 return -ENODEV;
176
177         prtcl = (in_be32(&gur->rcwsr[4]) & FSL_CORENET_RCWSR4_SRDS_PRTCL) >> 26;
178
179         return __serdes_get_first_lane(prtcl, device);
180 }
181
182 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES9
183 /*
184  * Returns the SERDES bank (1, 2, or 3) that a given device is on for a given
185  * SERDES protocol.
186  *
187  * Returns a negative error code if the given device is not supported for the
188  * given SERDES protocol.
189  */
190 static int serdes_get_bank_by_device(uint32_t prtcl, enum srds_prtcl device)
191 {
192         int lane;
193
194         lane = __serdes_get_first_lane(prtcl, device);
195         if (unlikely(lane < 0))
196                 return lane;
197
198         return serdes_get_bank_by_lane(lane);
199 }
200
201 static uint32_t __serdes_get_lane_count(uint32_t prtcl, enum srds_prtcl device,
202                                         int first)
203 {
204         int lane;
205
206         for (lane = first; lane < SRDS_MAX_LANES; lane++) {
207                 if (serdes_get_prtcl(prtcl, lane) != device)
208                         break;
209         }
210
211         return lane - first;
212 }
213
214 static void __serdes_reset_rx(serdes_corenet_t *regs,
215                               uint32_t prtcl,
216                               enum srds_prtcl device)
217 {
218         int lane, idx, first, last;
219
220         lane = __serdes_get_first_lane(prtcl, device);
221         if (unlikely(lane < 0))
222                 return;
223         first = serdes_get_lane_idx(lane);
224         last = first + __serdes_get_lane_count(prtcl, device, lane);
225
226         /*
227          * Set BnGCRy0[RRST] = 0 for each lane in the each bank that is
228          * selected as XAUI to place the lane into reset.
229         */
230         for (idx = first; idx < last; idx++)
231                 clrbits_be32(&regs->lane[idx].gcr0, SRDS_GCR0_RRST);
232
233         /* Wait at least 250 ns */
234         udelay(1);
235
236         /*
237          * Set BnGCRy0[RRST] = 1 for each lane in the each bank that is
238          * selected as XAUI to bring the lane out of reset.
239          */
240         for (idx = first; idx < last; idx++)
241                 setbits_be32(&regs->lane[idx].gcr0, SRDS_GCR0_RRST);
242 }
243
244 void serdes_reset_rx(enum srds_prtcl device)
245 {
246         u32 prtcl;
247         const ccsr_gur_t *gur;
248         serdes_corenet_t *regs;
249
250         if (unlikely(device == NONE))
251                 return;
252
253         gur = (typeof(gur))CONFIG_SYS_MPC85xx_GUTS_ADDR;
254
255         /* Is serdes enabled at all? */
256         if (unlikely((in_be32(&gur->rcwsr[5]) & 0x2000) == 0))
257                 return;
258
259         regs = (typeof(regs))CONFIG_SYS_FSL_CORENET_SERDES_ADDR;
260         prtcl = (in_be32(&gur->rcwsr[4]) & FSL_CORENET_RCWSR4_SRDS_PRTCL) >> 26;
261
262         __serdes_reset_rx(regs, prtcl, device);
263 }
264 #endif
265
266 #ifndef CONFIG_SYS_DCSRBAR_PHYS
267 #define CONFIG_SYS_DCSRBAR_PHYS 0x80000000 /* Must be 1GB-aligned for rev1.0 */
268 #define CONFIG_SYS_DCSRBAR      0x80000000
269 #define __DCSR_NOT_DEFINED_BY_CONFIG
270 #endif
271
272 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
273 /*
274  * Enable a SERDES bank that was disabled via the RCW
275  *
276  * We only call this function for SERDES8 and SERDES-A001 in cases we really
277  * want to enable the bank, whether we actually want to use the lanes or not,
278  * so make sure at least one lane is enabled.  We're only enabling this one
279  * lane to satisfy errata requirements that the bank be enabled.
280  *
281  * We use a local variable instead of srds_lpd_b[] because we want drivers to
282  * think that the lanes actually are disabled.
283  */
284 static void enable_bank(ccsr_gur_t *gur, int bank)
285 {
286         u32 rcw5;
287         u32 temp_lpd_b = srds_lpd_b[bank];
288
289         /*
290          * If we're asked to disable all lanes, just pretend we're doing
291          * that.
292          */
293         if (temp_lpd_b == 0xF)
294                 temp_lpd_b = 0xE;
295
296         /*
297          * Enable the lanes SRDS_LPD_Bn.  The RCW bits are read-only in
298          * CCSR, and read/write in DSCR.
299          */
300         rcw5 = in_be32(gur->rcwsr + 5);
301         if (bank == FSL_SRDS_BANK_2) {
302                 rcw5 &= ~FSL_CORENET_RCWSRn_SRDS_LPD_B2;
303                 rcw5 |= temp_lpd_b << 26;
304         } else if (bank == FSL_SRDS_BANK_3) {
305                 rcw5 &= ~FSL_CORENET_RCWSRn_SRDS_LPD_B3;
306                 rcw5 |= temp_lpd_b << 18;
307         } else {
308                 printf("SERDES: enable_bank: bad bank %d\n", bank + 1);
309                 return;
310         }
311
312         /* See similar code in cpu/mpc85xx/cpu_init.c for an explanation
313          * of the DCSR mapping.
314          */
315         {
316 #ifdef __DCSR_NOT_DEFINED_BY_CONFIG
317                 struct law_entry law = find_law(CONFIG_SYS_DCSRBAR_PHYS);
318                 int law_index;
319                 if (law.index == -1)
320                         law_index = set_next_law(CONFIG_SYS_DCSRBAR_PHYS,
321                                                  LAW_SIZE_1M, LAW_TRGT_IF_DCSR);
322                 else
323                         set_law(law.index, CONFIG_SYS_DCSRBAR_PHYS, LAW_SIZE_1M,
324                                 LAW_TRGT_IF_DCSR);
325 #endif
326                 u32 *p = (void *)CONFIG_SYS_DCSRBAR + 0x20114;
327                 out_be32(p, rcw5);
328 #ifdef __DCSR_NOT_DEFINED_BY_CONFIG
329                 if (law.index == -1)
330                         disable_law(law_index);
331                 else
332                         set_law(law.index, law.addr, law.size, law.trgt_id);
333 #endif
334         }
335 }
336
337 /*
338  * To avoid problems with clock jitter, rev 2 p4080 uses the pll from
339  * bank 3 to clock banks 2 and 3, as well as a limited selection of
340  * protocol configurations.  This requires that banks 2 and 3's lanes be
341  * disabled in the RCW, and enabled with some fixup here to re-enable
342  * them, and to configure bank 2's clock parameters in bank 3's pll in
343  * cases where they differ.
344  */
345 static void p4080_erratum_serdes8(serdes_corenet_t *regs, ccsr_gur_t *gur,
346                                   u32 devdisr, u32 devdisr2, int cfg)
347 {
348         int srds_ratio_b2;
349         int rfck_sel;
350
351         /*
352          * The disabled lanes of bank 2 will cause the associated
353          * logic blocks to be disabled in DEVDISR.  We reverse that here.
354          *
355          * Note that normally it is not permitted to clear DEVDISR bits
356          * once the device has been disabled, but the hardware people
357          * say that this special case is OK.
358          */
359         clrbits_be32(&gur->devdisr, devdisr);
360         clrbits_be32(&gur->devdisr2, devdisr2);
361
362         /*
363          * Some protocols require special handling.  There are a few
364          * additional protocol configurations that can be used, which are
365          * not listed here.  See app note 4065 for supported protocol
366          * configurations.
367          */
368         switch (cfg) {
369         case 0x19:
370                 /*
371                  * Bank 2 has PCIe which wants BWSEL -- tell bank 3's PLL.
372                  * SGMII on bank 3 should still be usable.
373                  */
374                 setbits_be32(&regs->bank[FSL_SRDS_BANK_3].pllcr1,
375                              SRDS_PLLCR1_PLL_BWSEL);
376                 break;
377
378         case 0x0f:
379         case 0x10:
380                 /*
381                  * Banks 2 (XAUI) and 3 (SGMII) have different clocking
382                  * requirements in these configurations.  Bank 3 cannot
383                  * be used and should have its lanes (but not the bank
384                  * itself) disabled in the RCW.  We set up bank 3's pll
385                  * for bank 2's needs here.
386                  */
387                 srds_ratio_b2 = (in_be32(&gur->rcwsr[4]) >> 13) & 7;
388
389                 /* Determine refclock from XAUI ratio */
390                 switch (srds_ratio_b2) {
391                 case 1: /* 20:1 */
392                         rfck_sel = SRDS_PLLCR0_RFCK_SEL_156_25;
393                         break;
394                 case 2: /* 25:1 */
395                         rfck_sel = SRDS_PLLCR0_RFCK_SEL_125;
396                         break;
397                 default:
398                         printf("SERDES: bad SRDS_RATIO_B2 %d\n",
399                                srds_ratio_b2);
400                         return;
401                 }
402
403                 clrsetbits_be32(&regs->bank[FSL_SRDS_BANK_3].pllcr0,
404                                 SRDS_PLLCR0_RFCK_SEL_MASK, rfck_sel);
405
406                 clrsetbits_be32(&regs->bank[FSL_SRDS_BANK_3].pllcr0,
407                                 SRDS_PLLCR0_FRATE_SEL_MASK,
408                                 SRDS_PLLCR0_FRATE_SEL_6_25);
409                 break;
410         }
411
412         enable_bank(gur, FSL_SRDS_BANK_3);
413 }
414 #endif
415
416 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES_A005
417 /*
418  * If PCIe is not selected as a protocol for any lanes driven by a given PLL,
419  * that PLL should have SRDSBnPLLCR1[PLLBW_SEL] = 0.
420  */
421 static void p4080_erratum_serdes_a005(serdes_corenet_t *regs, unsigned int cfg)
422 {
423         enum srds_prtcl device;
424
425         switch (cfg) {
426         case 0x13:
427         case 0x16:
428                 /*
429                  * If SRDS_PRTCL = 0x13 or 0x16, set SRDSB1PLLCR1[PLLBW_SEL]
430                  * to 0.
431                  */
432                 clrbits_be32(&regs->bank[FSL_SRDS_BANK_1].pllcr1,
433                              SRDS_PLLCR1_PLL_BWSEL);
434                 break;
435         case 0x19:
436                 /*
437                  * If SRDS_PRTCL = 0x19, set SRDSB1PLLCR1[PLLBW_SEL] to 0 and
438                  * SRDSB3PLLCR1[PLLBW_SEL] to 1.
439                  */
440                 clrbits_be32(&regs->bank[FSL_SRDS_BANK_1].pllcr1,
441                              SRDS_PLLCR1_PLL_BWSEL);
442                 setbits_be32(&regs->bank[FSL_SRDS_BANK_3].pllcr1,
443                              SRDS_PLLCR1_PLL_BWSEL);
444                 break;
445         }
446
447         /*
448          * Set SRDSBnPLLCR1[PLLBW_SEL] to 0 for each bank that selects XAUI
449          * before XAUI is initialized.
450          */
451         for (device = XAUI_FM1; device <= XAUI_FM2; device++) {
452                 if (is_serdes_configured(device)) {
453                         int bank = serdes_get_bank_by_device(cfg, device);
454
455                         clrbits_be32(&regs->bank[bank].pllcr1,
456                                      SRDS_PLLCR1_PLL_BWSEL);
457                 }
458         }
459 }
460 #endif
461
462 /*
463  * Wait for the RSTDONE bit to get set, or a one-second timeout.
464  */
465 static void wait_for_rstdone(unsigned int bank)
466 {
467         serdes_corenet_t *srds_regs =
468                 (void *)CONFIG_SYS_FSL_CORENET_SERDES_ADDR;
469         unsigned long long end_tick;
470         u32 rstctl;
471
472         /* wait for reset complete or 1-second timeout */
473         end_tick = usec2ticks(1000000) + get_ticks();
474         do {
475                 rstctl = in_be32(&srds_regs->bank[bank].rstctl);
476                 if (rstctl & SRDS_RSTCTL_RSTDONE)
477                         break;
478         } while (end_tick > get_ticks());
479
480         if (!(rstctl & SRDS_RSTCTL_RSTDONE))
481                 printf("SERDES: timeout resetting bank %u\n", bank + 1);
482 }
483
484
485 static void __soc_serdes_init(void)
486 {
487         /* Allow for SoC-specific initialization in <SOC>_serdes.c  */
488 };
489 void soc_serdes_init(void) __attribute__((weak, alias("__soc_serdes_init")));
490
491 void fsl_serdes_init(void)
492 {
493         ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
494         int cfg;
495         serdes_corenet_t *srds_regs;
496 #ifdef CONFIG_ARCH_P5040
497         serdes_corenet_t *srds2_regs;
498 #endif
499         int lane, bank, idx;
500         int have_bank[SRDS_MAX_BANK] = {};
501 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
502         u32 serdes8_devdisr = 0;
503         u32 serdes8_devdisr2 = 0;
504         char srds_lpd_opt[16];
505         const char *srds_lpd_arg;
506         size_t arglen;
507 #endif
508 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES_A001
509         int need_serdes_a001;   /* true == need work-around for SERDES A001 */
510 #endif
511 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
512         char buffer[HWCONFIG_BUFFER_SIZE];
513         char *buf = NULL;
514
515         /*
516          * Extract hwconfig from environment since we have not properly setup
517          * the environment but need it for ddr config params
518          */
519         if (env_get_f("hwconfig", buffer, sizeof(buffer)) > 0)
520                 buf = buffer;
521 #endif
522         if (serdes_prtcl_map & (1 << NONE))
523                 return;
524
525         /* Is serdes enabled at all? */
526         if (!(in_be32(&gur->rcwsr[5]) & FSL_CORENET_RCWSR5_SRDS_EN))
527                 return;
528
529         srds_regs = (void *)(CONFIG_SYS_FSL_CORENET_SERDES_ADDR);
530         cfg = (in_be32(&gur->rcwsr[4]) & FSL_CORENET_RCWSR4_SRDS_PRTCL) >> 26;
531         debug("Using SERDES configuration 0x%x, lane settings:\n", cfg);
532
533         if (!is_serdes_prtcl_valid(cfg)) {
534                 printf("SERDES[PRTCL] = 0x%x is not valid\n", cfg);
535                 return;
536         }
537
538 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
539         /*
540          * Display a warning if banks two and three are not disabled in the RCW,
541          * since our work-around for SERDES8 depends on these banks being
542          * disabled at power-on.
543          */
544 #define B2_B3 (FSL_CORENET_RCWSRn_SRDS_LPD_B2 | FSL_CORENET_RCWSRn_SRDS_LPD_B3)
545         if ((in_be32(&gur->rcwsr[5]) & B2_B3) != B2_B3) {
546                 printf("Warning: SERDES8 requires banks two and "
547                        "three to be disabled in the RCW\n");
548         }
549
550         /*
551          * Store the values of the fsl_srds_lpd_b2 and fsl_srds_lpd_b3
552          * hwconfig options into the srds_lpd_b[] array.  See README.p4080ds
553          * for a description of these options.
554          */
555         for (bank = 1; bank < ARRAY_SIZE(srds_lpd_b); bank++) {
556                 sprintf(srds_lpd_opt, "fsl_srds_lpd_b%u", bank + 1);
557                 srds_lpd_arg =
558                         hwconfig_subarg_f("serdes", srds_lpd_opt, &arglen, buf);
559                 if (srds_lpd_arg)
560                         srds_lpd_b[bank] =
561                                 simple_strtoul(srds_lpd_arg, NULL, 0) & 0xf;
562         }
563
564         if ((cfg == 0xf) || (cfg == 0x10)) {
565                 /*
566                  * For SERDES protocols 0xF and 0x10, force bank 3 to be
567                  * disabled, because it is not supported.
568                  */
569                 srds_lpd_b[FSL_SRDS_BANK_3] = 0xF;
570         }
571 #endif
572
573         /* Look for banks with all lanes disabled, and power down the bank. */
574         for (lane = 0; lane < SRDS_MAX_LANES; lane++) {
575                 enum srds_prtcl lane_prtcl = serdes_get_prtcl(cfg, lane);
576                 if (serdes_lane_enabled(lane)) {
577                         have_bank[serdes_get_bank_by_lane(lane)] = 1;
578                         serdes_prtcl_map |= (1 << lane_prtcl);
579                 }
580         }
581
582 #ifdef CONFIG_ARCH_P5040
583         /*
584          * Lanes on bank 4 on P5040 are commented-out, but for some SERDES
585          * protocols, these lanes are routed to SATA.  We use serdes_prtcl_map
586          * to decide whether a protocol is supported on a given lane, so SATA
587          * will be identified as not supported, and therefore not initialized.
588          * So for protocols which use SATA on bank4, we add SATA support in
589          * serdes_prtcl_map.
590          */
591         switch (cfg) {
592         case 0x0:
593         case 0x1:
594         case 0x2:
595         case 0x3:
596         case 0x4:
597         case 0x5:
598         case 0x6:
599         case 0x7:
600                 serdes_prtcl_map |= 1 << SATA1 | 1 << SATA2;
601                 break;
602         default:
603                 srds2_regs = (void *)CONFIG_SYS_FSL_CORENET_SERDES2_ADDR;
604
605                 /* We don't need bank 4, so power it down */
606                 setbits_be32(&srds2_regs->bank[0].rstctl, SRDS_RSTCTL_SDPD);
607         }
608 #endif
609
610         soc_serdes_init();
611
612         /* Set the first bit to indicate serdes has been initialized */
613         serdes_prtcl_map |= (1 << NONE);
614
615 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
616         /*
617          * Bank two uses the clock from bank three, so if bank two is enabled,
618          * then bank three must also be enabled.
619          */
620         if (have_bank[FSL_SRDS_BANK_2])
621                 have_bank[FSL_SRDS_BANK_3] = 1;
622 #endif
623
624 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES_A001
625         /*
626          * The work-aroud for erratum SERDES-A001 is needed only if bank two
627          * is disabled and bank three is enabled.  The converse is also true,
628          * but SERDES8 ensures that bank 3 is always enabled if bank 2 is
629          * enabled, so there's no point in complicating the code to handle
630          * that situation.
631          */
632         need_serdes_a001 =
633                 !have_bank[FSL_SRDS_BANK_2] && have_bank[FSL_SRDS_BANK_3];
634 #endif
635
636         /* Power down the banks we're not interested in */
637         for (bank = 0; bank < SRDS_MAX_BANK; bank++) {
638                 if (!have_bank[bank]) {
639                         printf("SERDES: bank %d disabled\n", bank + 1);
640 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES_A001
641                         /*
642                          * Erratum SERDES-A001 says bank two needs to be powered
643                          * down after bank three is powered up, so don't power
644                          * down bank two here.
645                          */
646                         if (!need_serdes_a001 || (bank != FSL_SRDS_BANK_2))
647                                 setbits_be32(&srds_regs->bank[bank].rstctl,
648                                              SRDS_RSTCTL_SDPD);
649 #else
650                         setbits_be32(&srds_regs->bank[bank].rstctl,
651                                      SRDS_RSTCTL_SDPD);
652 #endif
653                 }
654         }
655
656 #ifdef CONFIG_SYS_FSL_ERRATUM_A004699
657         /*
658          * To avoid the situation that resulted in the P4080 erratum
659          * SERDES-8, a given SerDes bank will use the PLLs from the previous
660          * bank if one of the PLL frequencies is a multiple of the other.  For
661          * instance, if bank 3 is running at 2.5GHz and bank 2 is at 1.25GHz,
662          * then bank 3 will use bank 2's PLL.  P5040 Erratum A-004699 says
663          * that, in this situation, lane synchronization is not initiated.  So
664          * when we detect a bank with a "borrowed" PLL, we have to manually
665          * initiate lane synchronization.
666          */
667         for (bank = FSL_SRDS_BANK_2; bank <= FSL_SRDS_BANK_3; bank++) {
668                 /* Determine the first lane for this bank */
669                 unsigned int lane;
670
671                 for (lane = 0; lane < SRDS_MAX_LANES; lane++)
672                         if (lanes[lane].bank == bank)
673                                 break;
674                 idx = lanes[lane].idx;
675
676                 /*
677                  * Check if the PLL for the bank is borrowed.  The UOTHL
678                  * bit of the first lane will tell us that.
679                  */
680                 if (in_be32(&srds_regs->lane[idx].gcr0) & SRDS_GCR0_UOTHL) {
681                         /* Manually start lane synchronization */
682                         setbits_be32(&srds_regs->bank[bank].pllcr0,
683                                      SRDS_PLLCR0_PVCOCNT_EN);
684                 }
685         }
686 #endif
687
688 #if defined(CONFIG_SYS_P4080_ERRATUM_SERDES8) || defined (CONFIG_SYS_P4080_ERRATUM_SERDES9)
689         for (lane = 0; lane < SRDS_MAX_LANES; lane++) {
690                 enum srds_prtcl lane_prtcl;
691
692                 idx = serdes_get_lane_idx(lane);
693                 lane_prtcl = serdes_get_prtcl(cfg, lane);
694
695 #ifdef DEBUG
696                 switch (lane) {
697                 case 0:
698                         puts("Bank1: ");
699                         break;
700                 case 10:
701                         puts("\nBank2: ");
702                         break;
703                 case 14:
704                         puts("\nBank3: ");
705                         break;
706                 default:
707                         break;
708                 }
709
710                 printf("%s ", serdes_prtcl_str[lane_prtcl]);
711 #endif
712
713 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES9
714                 /*
715                  * Set BnTTLCRy0[FLT_SEL] = 011011 and set BnTTLCRy0[31] = 1
716                  * for each of the SerDes lanes selected as SGMII, XAUI, SRIO,
717                  * or AURORA before the device is initialized.
718                  *
719                  * Note that this part of the SERDES-9 work-around is
720                  * redundant if the work-around for A-4580 has already been
721                  * applied via PBI.
722                  */
723                 switch (lane_prtcl) {
724                 case SGMII_FM1_DTSEC1:
725                 case SGMII_FM1_DTSEC2:
726                 case SGMII_FM1_DTSEC3:
727                 case SGMII_FM1_DTSEC4:
728                 case SGMII_FM2_DTSEC1:
729                 case SGMII_FM2_DTSEC2:
730                 case SGMII_FM2_DTSEC3:
731                 case SGMII_FM2_DTSEC4:
732                 case SGMII_FM2_DTSEC5:
733                 case XAUI_FM1:
734                 case XAUI_FM2:
735                 case SRIO1:
736                 case SRIO2:
737                 case AURORA:
738                         out_be32(&srds_regs->lane[idx].ttlcr0,
739                                  SRDS_TTLCR0_FLT_SEL_KFR_26 |
740                                  SRDS_TTLCR0_FLT_SEL_KPH_28 |
741                                  SRDS_TTLCR0_FLT_SEL_750PPM |
742                                  SRDS_TTLCR0_FREQOVD_EN);
743                         break;
744                 default:
745                         break;
746                 }
747 #endif
748
749 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
750                 switch (lane_prtcl) {
751                 case PCIE1:
752                 case PCIE2:
753                 case PCIE3:
754                         serdes8_devdisr |= FSL_CORENET_DEVDISR_PCIE1 >>
755                                            (lane_prtcl - PCIE1);
756                         break;
757                 case SRIO1:
758                 case SRIO2:
759                         serdes8_devdisr |= FSL_CORENET_DEVDISR_SRIO1 >>
760                                            (lane_prtcl - SRIO1);
761                         break;
762                 case SGMII_FM1_DTSEC1:
763                         serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM1 |
764                                             FSL_CORENET_DEVDISR2_DTSEC1_1;
765                         break;
766                 case SGMII_FM1_DTSEC2:
767                         serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM1 |
768                                             FSL_CORENET_DEVDISR2_DTSEC1_2;
769                         break;
770                 case SGMII_FM1_DTSEC3:
771                         serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM1 |
772                                             FSL_CORENET_DEVDISR2_DTSEC1_3;
773                         break;
774                 case SGMII_FM1_DTSEC4:
775                         serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM1 |
776                                             FSL_CORENET_DEVDISR2_DTSEC1_4;
777                         break;
778                 case SGMII_FM2_DTSEC1:
779                         serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM2 |
780                                             FSL_CORENET_DEVDISR2_DTSEC2_1;
781                         break;
782                 case SGMII_FM2_DTSEC2:
783                         serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM2 |
784                                             FSL_CORENET_DEVDISR2_DTSEC2_2;
785                         break;
786                 case SGMII_FM2_DTSEC3:
787                         serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM2 |
788                                             FSL_CORENET_DEVDISR2_DTSEC2_3;
789                         break;
790                 case SGMII_FM2_DTSEC4:
791                         serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM2 |
792                                             FSL_CORENET_DEVDISR2_DTSEC2_4;
793                         break;
794                 case SGMII_FM2_DTSEC5:
795                         serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM2 |
796                                             FSL_CORENET_DEVDISR2_DTSEC2_5;
797                         break;
798                 case XAUI_FM1:
799                         serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM1    |
800                                             FSL_CORENET_DEVDISR2_10GEC1;
801                         break;
802                 case XAUI_FM2:
803                         serdes8_devdisr2 |= FSL_CORENET_DEVDISR2_FM2    |
804                                             FSL_CORENET_DEVDISR2_10GEC2;
805                         break;
806                 case AURORA:
807                         break;
808                 default:
809                         break;
810                 }
811
812 #endif
813         }
814 #endif
815
816 #ifdef DEBUG
817         puts("\n");
818 #endif
819
820 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES_A005
821         p4080_erratum_serdes_a005(srds_regs, cfg);
822 #endif
823
824         for (idx = 0; idx < SRDS_MAX_BANK; idx++) {
825                 bank = idx;
826
827 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
828                 /*
829                  * Change bank init order to 0, 2, 1, so that the third bank's
830                  * PLL is established before we start the second bank.  The
831                  * second bank uses the third bank's PLL.
832                  */
833
834                 if (idx == 1)
835                         bank = FSL_SRDS_BANK_3;
836                 else if (idx == 2)
837                         bank = FSL_SRDS_BANK_2;
838 #endif
839
840                 /* Skip disabled banks */
841                 if (!have_bank[bank])
842                         continue;
843
844 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES8
845                 if (idx == 1) {
846                         /*
847                          * Re-enable devices on banks two and three that were
848                          * disabled by the RCW, and then enable bank three. The
849                          * devices need to be enabled before either bank is
850                          * powered up.
851                          */
852                         p4080_erratum_serdes8(srds_regs, gur, serdes8_devdisr,
853                                               serdes8_devdisr2, cfg);
854                 } else if (idx == 2) {
855                         /* Enable bank two now that bank three is enabled. */
856                         enable_bank(gur, FSL_SRDS_BANK_2);
857                 }
858 #endif
859
860                 wait_for_rstdone(bank);
861         }
862
863 #ifdef CONFIG_SYS_P4080_ERRATUM_SERDES_A001
864         if (need_serdes_a001) {
865                 /* Bank 3 has been enabled, so now we can disable bank 2 */
866                 setbits_be32(&srds_regs->bank[FSL_SRDS_BANK_2].rstctl,
867                              SRDS_RSTCTL_SDPD);
868         }
869 #endif
870 }
871
872 const char *serdes_clock_to_string(u32 clock)
873 {
874         switch (clock) {
875         case SRDS_PLLCR0_RFCK_SEL_100:
876                 return "100";
877         case SRDS_PLLCR0_RFCK_SEL_125:
878                 return "125";
879         case SRDS_PLLCR0_RFCK_SEL_156_25:
880                 return "156.25";
881         case SRDS_PLLCR0_RFCK_SEL_161_13:
882                 return "161.1328123";
883         default:
884                 return "150";
885         }
886 }
887