Merge https://gitlab.denx.de/u-boot/custodians/u-boot-mpc85xx
[oweals/u-boot.git] / board / freescale / t4qds / t4240qds.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2009-2012 Freescale Semiconductor, Inc.
4  * Copyright 2020 NXP
5  */
6
7 #include <common.h>
8 #include <command.h>
9 #include <env.h>
10 #include <fdt_support.h>
11 #include <i2c.h>
12 #include <init.h>
13 #include <irq_func.h>
14 #include <netdev.h>
15 #include <linux/compiler.h>
16 #include <asm/mmu.h>
17 #include <asm/processor.h>
18 #include <asm/cache.h>
19 #include <asm/immap_85xx.h>
20 #include <asm/fsl_law.h>
21 #include <asm/fsl_serdes.h>
22 #include <asm/fsl_liodn.h>
23 #include <fm_eth.h>
24
25 #include "../common/qixis.h"
26 #include "../common/vsc3316_3308.h"
27 #include "t4qds.h"
28 #include "t4240qds_qixis.h"
29
30 DECLARE_GLOBAL_DATA_PTR;
31
32 static int8_t vsc3316_fsm1_tx[8][2] = { {0, 0}, {1, 1}, {6, 6}, {7, 7},
33                                 {8, 8}, {9, 9}, {14, 14}, {15, 15} };
34
35 static int8_t vsc3316_fsm2_tx[8][2] = { {2, 2}, {3, 3}, {4, 4}, {5, 5},
36                                 {10, 10}, {11, 11}, {12, 12}, {13, 13} };
37
38 static int8_t vsc3316_fsm1_rx[8][2] = { {2, 12}, {3, 13}, {4, 5}, {5, 4},
39                                 {10, 11}, {11, 10}, {12, 2}, {13, 3} };
40
41 static int8_t vsc3316_fsm2_rx[8][2] = { {0, 15}, {1, 14}, {6, 7}, {7, 6},
42                                 {8, 9}, {9, 8}, {14, 1}, {15, 0} };
43
44 int checkboard(void)
45 {
46         char buf[64];
47         u8 sw;
48         struct cpu_type *cpu = gd->arch.cpu;
49         unsigned int i;
50
51         printf("Board: %sQDS, ", cpu->name);
52         printf("Sys ID: 0x%02x, Sys Ver: 0x%02x, ",
53                QIXIS_READ(id), QIXIS_READ(arch));
54
55         sw = QIXIS_READ(brdcfg[0]);
56         sw = (sw & QIXIS_LBMAP_MASK) >> QIXIS_LBMAP_SHIFT;
57
58         if (sw < 0x8)
59                 printf("vBank: %d\n", sw);
60         else if (sw == 0x8)
61                 puts("Promjet\n");
62         else if (sw == 0x9)
63                 puts("NAND\n");
64         else
65                 printf("invalid setting of SW%u\n", QIXIS_LBMAP_SWITCH);
66
67         printf("FPGA: v%d (%s), build %d",
68                (int)QIXIS_READ(scver), qixis_read_tag(buf),
69                (int)qixis_read_minor());
70         /* the timestamp string contains "\n" at the end */
71         printf(" on %s", qixis_read_time(buf));
72
73         /*
74          * Display the actual SERDES reference clocks as configured by the
75          * dip switches on the board.  Note that the SWx registers could
76          * technically be set to force the reference clocks to match the
77          * values that the SERDES expects (or vice versa).  For now, however,
78          * we just display both values and hope the user notices when they
79          * don't match.
80          */
81         puts("SERDES Reference Clocks: ");
82         sw = QIXIS_READ(brdcfg[2]);
83         for (i = 0; i < MAX_SERDES; i++) {
84                 static const char * const freq[] = {
85                         "100", "125", "156.25", "161.1328125"};
86                 unsigned int clock = (sw >> (6 - 2 * i)) & 3;
87
88                 printf("SERDES%u=%sMHz ", i+1, freq[clock]);
89         }
90         puts("\n");
91
92         return 0;
93 }
94
95 int select_i2c_ch_pca9547(u8 ch, int bus_num)
96 {
97         int ret;
98
99 #ifdef CONFIG_DM_I2C
100         struct udevice *dev;
101
102         ret = i2c_get_chip_for_busnum(bus_num, I2C_MUX_PCA_ADDR_PRI,
103                                       1, &dev);
104         if (ret) {
105                 printf("%s: Cannot find udev for a bus %d\n", __func__,
106                        bus_num);
107                 return ret;
108         }
109
110         ret = dm_i2c_write(dev, 0, &ch, 1);
111 #else
112         ret = i2c_write(I2C_MUX_PCA_ADDR_PRI, 0, 1, &ch, 1);
113 #endif
114         if (ret) {
115                 puts("PCA: failed to select proper channel\n");
116                 return ret;
117         }
118
119         return 0;
120 }
121
122 /*
123  * read_voltage from sensor on I2C bus
124  * We use average of 4 readings, waiting for 532us befor another reading
125  */
126 #define NUM_READINGS    4       /* prefer to be power of 2 for efficiency */
127 #define WAIT_FOR_ADC    532     /* wait for 532 microseconds for ADC */
128
129 static inline int read_voltage(void)
130 {
131         int i, ret, voltage_read = 0;
132         u16 vol_mon;
133 #ifdef CONFIG_DM_I2C
134         struct udevice *dev;
135         int bus_num = 0;
136 #endif
137
138         for (i = 0; i < NUM_READINGS; i++) {
139 #ifdef CONFIG_DM_I2C
140                 ret = i2c_get_chip_for_busnum(bus_num, I2C_VOL_MONITOR_ADDR,
141                                               1, &dev);
142                 if (ret) {
143                         printf("%s: Cannot find udev for a bus %d\n", __func__,
144                                bus_num);
145                         return ret;
146                 }
147
148                 ret = dm_i2c_read(dev,
149                                   I2C_VOL_MONITOR_BUS_V_OFFSET,
150                                   (void *)&vol_mon, 2);
151 #else
152                 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
153                         I2C_VOL_MONITOR_BUS_V_OFFSET, 1, (void *)&vol_mon, 2);
154 #endif
155                 if (ret) {
156                         printf("VID: failed to read core voltage\n");
157                         return ret;
158                 }
159                 if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
160                         printf("VID: Core voltage sensor error\n");
161                         return -1;
162                 }
163                 debug("VID: bus voltage reads 0x%04x\n", vol_mon);
164                 /* LSB = 4mv */
165                 voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
166                 udelay(WAIT_FOR_ADC);
167         }
168         /* calculate the average */
169         voltage_read /= NUM_READINGS;
170
171         return voltage_read;
172 }
173
174 /*
175  * We need to calculate how long before the voltage starts to drop or increase
176  * It returns with the loop count. Each loop takes several readings (532us)
177  */
178 static inline int wait_for_voltage_change(int vdd_last)
179 {
180         int timeout, vdd_current;
181
182         vdd_current = read_voltage();
183         /* wait until voltage starts to drop */
184         for (timeout = 0; abs(vdd_last - vdd_current) <= 4 &&
185                 timeout < 100; timeout++) {
186                 vdd_current = read_voltage();
187         }
188         if (timeout >= 100) {
189                 printf("VID: Voltage adjustment timeout\n");
190                 return -1;
191         }
192         return timeout;
193 }
194
195 /*
196  * argument 'wait' is the time we know the voltage difference can be measured
197  * this function keeps reading the voltage until it is stable
198  */
199 static inline int wait_for_voltage_stable(int wait)
200 {
201         int timeout, vdd_current, vdd_last;
202
203         vdd_last = read_voltage();
204         udelay(wait * NUM_READINGS * WAIT_FOR_ADC);
205         /* wait until voltage is stable */
206         vdd_current = read_voltage();
207         for (timeout = 0; abs(vdd_last - vdd_current) >= 4 &&
208                 timeout < 100; timeout++) {
209                 vdd_last = vdd_current;
210                 udelay(wait * NUM_READINGS * WAIT_FOR_ADC);
211                 vdd_current = read_voltage();
212         }
213         if (timeout >= 100) {
214                 printf("VID: Voltage adjustment timeout\n");
215                 return -1;
216         }
217
218         return vdd_current;
219 }
220
221 static inline int set_voltage(u8 vid)
222 {
223         int wait, vdd_last;
224
225         vdd_last = read_voltage();
226         QIXIS_WRITE(brdcfg[6], vid);
227         wait = wait_for_voltage_change(vdd_last);
228         if (wait < 0)
229                 return -1;
230         debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
231         wait = wait ? wait : 1;
232
233         vdd_last = wait_for_voltage_stable(wait);
234         if (vdd_last < 0)
235                 return -1;
236         debug("VID: Current voltage is %d mV\n", vdd_last);
237
238         return vdd_last;
239 }
240
241
242 static int adjust_vdd(ulong vdd_override)
243 {
244         int re_enable = disable_interrupts();
245         ccsr_gur_t __iomem *gur =
246                 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
247         u32 fusesr;
248         u8 vid, vid_current;
249         int vdd_target, vdd_current, vdd_last;
250         int ret;
251         unsigned long vdd_string_override;
252         char *vdd_string;
253         static const uint16_t vdd[32] = {
254                 0,      /* unused */
255                 9875,   /* 0.9875V */
256                 9750,
257                 9625,
258                 9500,
259                 9375,
260                 9250,
261                 9125,
262                 9000,
263                 8875,
264                 8750,
265                 8625,
266                 8500,
267                 8375,
268                 8250,
269                 8125,
270                 10000,  /* 1.0000V */
271                 10125,
272                 10250,
273                 10375,
274                 10500,
275                 10625,
276                 10750,
277                 10875,
278                 11000,
279                 0,      /* reserved */
280         };
281         struct vdd_drive {
282                 u8 vid;
283                 unsigned voltage;
284         };
285
286         ret = select_i2c_ch_pca9547(I2C_MUX_CH_VOL_MONITOR, 0);
287         if (ret) {
288                 debug("VID: I2c failed to switch channel\n");
289                 ret = -1;
290                 goto exit;
291         }
292
293         /* get the voltage ID from fuse status register */
294         fusesr = in_be32(&gur->dcfg_fusesr);
295         vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
296                 FSL_CORENET_DCFG_FUSESR_VID_MASK;
297         if (vid == FSL_CORENET_DCFG_FUSESR_VID_MASK) {
298                 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
299                         FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
300         }
301         vdd_target = vdd[vid];
302
303         /* check override variable for overriding VDD */
304         vdd_string = env_get("t4240qds_vdd_mv");
305         if (vdd_override == 0 && vdd_string &&
306             !strict_strtoul(vdd_string, 10, &vdd_string_override))
307                 vdd_override = vdd_string_override;
308         if (vdd_override >= 819 && vdd_override <= 1212) {
309                 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
310                 debug("VDD override is %lu\n", vdd_override);
311         } else if (vdd_override != 0) {
312                 printf("Invalid value.\n");
313         }
314
315         if (vdd_target == 0) {
316                 debug("VID: VID not used\n");
317                 ret = 0;
318                 goto exit;
319         } else {
320                 /* round up and divice by 10 to get a value in mV */
321                 vdd_target = DIV_ROUND_UP(vdd_target, 10);
322                 debug("VID: vid = %d mV\n", vdd_target);
323         }
324
325         /*
326          * Check current board VID setting
327          * Voltage regulator support output to 6.250mv step
328          * The highes voltage allowed for this board is (vid=0x40) 1.21250V
329          * the lowest is (vid=0x7f) 0.81875V
330          */
331         vid_current =  QIXIS_READ(brdcfg[6]);
332         vdd_current = 121250 - (vid_current - 0x40) * 625;
333         debug("VID: Current vid setting is (0x%x) %d mV\n",
334               vid_current, vdd_current/100);
335
336         /*
337          * Read voltage monitor to check real voltage.
338          * Voltage monitor LSB is 4mv.
339          */
340         vdd_last = read_voltage();
341         if (vdd_last < 0) {
342                 printf("VID: Could not read voltage sensor abort VID adjustment\n");
343                 ret = -1;
344                 goto exit;
345         }
346         debug("VID: Core voltage is at %d mV\n", vdd_last);
347         /*
348          * Adjust voltage to at or 8mV above target.
349          * Each step of adjustment is 6.25mV.
350          * Stepping down too fast may cause over current.
351          */
352         while (vdd_last > 0 && vid_current < 0x80 &&
353                 vdd_last > (vdd_target + 8)) {
354                 vid_current++;
355                 vdd_last = set_voltage(vid_current);
356         }
357         /*
358          * Check if we need to step up
359          * This happens when board voltage switch was set too low
360          */
361         while (vdd_last > 0 && vid_current >= 0x40 &&
362                 vdd_last < vdd_target + 2) {
363                 vid_current--;
364                 vdd_last = set_voltage(vid_current);
365         }
366         if (vdd_last > 0)
367                 printf("VID: Core voltage %d mV\n", vdd_last);
368         else
369                 ret = -1;
370
371 exit:
372         if (re_enable)
373                 enable_interrupts();
374         return ret;
375 }
376
377 /* Configure Crossbar switches for Front-Side SerDes Ports */
378 int config_frontside_crossbar_vsc3316(void)
379 {
380         ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
381         u32 srds_prtcl_s1, srds_prtcl_s2;
382         int ret;
383
384         ret = select_i2c_ch_pca9547(I2C_MUX_CH_VSC3316_FS, 0);
385         if (ret)
386                 return ret;
387
388         srds_prtcl_s1 = in_be32(&gur->rcwsr[4]) &
389                         FSL_CORENET2_RCWSR4_SRDS1_PRTCL;
390         srds_prtcl_s1 >>= FSL_CORENET2_RCWSR4_SRDS1_PRTCL_SHIFT;
391         switch (srds_prtcl_s1) {
392         case 37:
393         case 38:
394                 /* swap first lane and third lane on slot1 */
395                 vsc3316_fsm1_tx[0][1] = 14;
396                 vsc3316_fsm1_tx[6][1] = 0;
397                 vsc3316_fsm1_rx[1][1] = 2;
398                 vsc3316_fsm1_rx[6][1] = 13;
399         case 39:
400         case 40:
401         case 45:
402         case 46:
403         case 47:
404         case 48:
405                 /* swap first lane and third lane on slot2 */
406                 vsc3316_fsm1_tx[2][1] = 8;
407                 vsc3316_fsm1_tx[4][1] = 6;
408                 vsc3316_fsm1_rx[2][1] = 10;
409                 vsc3316_fsm1_rx[5][1] = 5;
410         default:
411                 ret = vsc3316_config(VSC3316_FSM_TX_ADDR, vsc3316_fsm1_tx, 8);
412                 if (ret)
413                         return ret;
414                 ret = vsc3316_config(VSC3316_FSM_RX_ADDR, vsc3316_fsm1_rx, 8);
415                 if (ret)
416                         return ret;
417                 break;
418         }
419
420         srds_prtcl_s2 = in_be32(&gur->rcwsr[4]) &
421                                 FSL_CORENET2_RCWSR4_SRDS2_PRTCL;
422         srds_prtcl_s2 >>= FSL_CORENET2_RCWSR4_SRDS2_PRTCL_SHIFT;
423         switch (srds_prtcl_s2) {
424         case 37:
425         case 38:
426                 /* swap first lane and third lane on slot3 */
427                 vsc3316_fsm2_tx[2][1] = 11;
428                 vsc3316_fsm2_tx[5][1] = 4;
429                 vsc3316_fsm2_rx[2][1] = 9;
430                 vsc3316_fsm2_rx[4][1] = 7;
431         case 39:
432         case 40:
433         case 45:
434         case 46:
435         case 47:
436         case 48:
437         case 49:
438         case 50:
439         case 51:
440         case 52:
441         case 53:
442         case 54:
443                 /* swap first lane and third lane on slot4 */
444                 vsc3316_fsm2_tx[6][1] = 3;
445                 vsc3316_fsm2_tx[1][1] = 12;
446                 vsc3316_fsm2_rx[0][1] = 1;
447                 vsc3316_fsm2_rx[6][1] = 15;
448         default:
449                 ret = vsc3316_config(VSC3316_FSM_TX_ADDR, vsc3316_fsm2_tx, 8);
450                 if (ret)
451                         return ret;
452                 ret = vsc3316_config(VSC3316_FSM_RX_ADDR, vsc3316_fsm2_rx, 8);
453                 if (ret)
454                         return ret;
455                 break;
456         }
457
458         return 0;
459 }
460
461 int config_backside_crossbar_mux(void)
462 {
463         ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
464         u32 srds_prtcl_s3, srds_prtcl_s4;
465         u8 brdcfg;
466
467         srds_prtcl_s3 = in_be32(&gur->rcwsr[4]) &
468                         FSL_CORENET2_RCWSR4_SRDS3_PRTCL;
469         srds_prtcl_s3 >>= FSL_CORENET2_RCWSR4_SRDS3_PRTCL_SHIFT;
470         switch (srds_prtcl_s3) {
471         case 0:
472                 /* SerDes3 is not enabled */
473                 break;
474         case 1:
475         case 2:
476         case 9:
477         case 10:
478                 /* SD3(0:7) => SLOT5(0:7) */
479                 brdcfg = QIXIS_READ(brdcfg[12]);
480                 brdcfg &= ~BRDCFG12_SD3MX_MASK;
481                 brdcfg |= BRDCFG12_SD3MX_SLOT5;
482                 QIXIS_WRITE(brdcfg[12], brdcfg);
483                 break;
484         case 3:
485         case 4:
486         case 5:
487         case 6:
488         case 7:
489         case 8:
490         case 11:
491         case 12:
492         case 13:
493         case 14:
494         case 15:
495         case 16:
496         case 17:
497         case 18:
498         case 19:
499         case 20:
500                 /* SD3(4:7) => SLOT6(0:3) */
501                 brdcfg = QIXIS_READ(brdcfg[12]);
502                 brdcfg &= ~BRDCFG12_SD3MX_MASK;
503                 brdcfg |= BRDCFG12_SD3MX_SLOT6;
504                 QIXIS_WRITE(brdcfg[12], brdcfg);
505                 break;
506         default:
507                 printf("WARNING: unsupported for SerDes3 Protocol %d\n",
508                        srds_prtcl_s3);
509                 return -1;
510         }
511
512         srds_prtcl_s4 = in_be32(&gur->rcwsr[4]) &
513                         FSL_CORENET2_RCWSR4_SRDS4_PRTCL;
514         srds_prtcl_s4 >>= FSL_CORENET2_RCWSR4_SRDS4_PRTCL_SHIFT;
515         switch (srds_prtcl_s4) {
516         case 0:
517                 /* SerDes4 is not enabled */
518                 break;
519         case 1:
520         case 2:
521                 /* 10b, SD4(0:7) => SLOT7(0:7) */
522                 brdcfg = QIXIS_READ(brdcfg[12]);
523                 brdcfg &= ~BRDCFG12_SD4MX_MASK;
524                 brdcfg |= BRDCFG12_SD4MX_SLOT7;
525                 QIXIS_WRITE(brdcfg[12], brdcfg);
526                 break;
527         case 3:
528         case 4:
529         case 5:
530         case 6:
531         case 7:
532         case 8:
533                 /* x1b, SD4(4:7) => SLOT8(0:3) */
534                 brdcfg = QIXIS_READ(brdcfg[12]);
535                 brdcfg &= ~BRDCFG12_SD4MX_MASK;
536                 brdcfg |= BRDCFG12_SD4MX_SLOT8;
537                 QIXIS_WRITE(brdcfg[12], brdcfg);
538                 break;
539         case 9:
540         case 10:
541         case 11:
542         case 12:
543         case 13:
544         case 14:
545         case 15:
546         case 16:
547         case 18:
548                 /* 00b, SD4(4:5) => AURORA, SD4(6:7) => SATA */
549                 brdcfg = QIXIS_READ(brdcfg[12]);
550                 brdcfg &= ~BRDCFG12_SD4MX_MASK;
551                 brdcfg |= BRDCFG12_SD4MX_AURO_SATA;
552                 QIXIS_WRITE(brdcfg[12], brdcfg);
553                 break;
554         default:
555                 printf("WARNING: unsupported for SerDes4 Protocol %d\n",
556                        srds_prtcl_s4);
557                 return -1;
558         }
559
560         return 0;
561 }
562
563 int board_early_init_r(void)
564 {
565         const unsigned int flashbase = CONFIG_SYS_FLASH_BASE;
566         int flash_esel = find_tlb_idx((void *)flashbase, 1);
567
568         /*
569          * Remap Boot flash + PROMJET region to caching-inhibited
570          * so that flash can be erased properly.
571          */
572
573         /* Flush d-cache and invalidate i-cache of any FLASH data */
574         flush_dcache();
575         invalidate_icache();
576
577         if (flash_esel == -1) {
578                 /* very unlikely unless something is messed up */
579                 puts("Error: Could not find TLB for FLASH BASE\n");
580                 flash_esel = 2; /* give our best effort to continue */
581         } else {
582                 /* invalidate existing TLB entry for flash + promjet */
583                 disable_tlb(flash_esel);
584         }
585
586         set_tlb(1, flashbase, CONFIG_SYS_FLASH_BASE_PHYS,
587                 MAS3_SX|MAS3_SW|MAS3_SR, MAS2_I|MAS2_G,
588                 0, flash_esel, BOOKE_PAGESZ_256M, 1);
589
590         /* Disable remote I2C connection to qixis fpga */
591         QIXIS_WRITE(brdcfg[5], QIXIS_READ(brdcfg[5]) & ~BRDCFG5_IRE);
592
593         /*
594          * Adjust core voltage according to voltage ID
595          * This function changes I2C mux to channel 2.
596          */
597         if (adjust_vdd(0))
598                 printf("Warning: Adjusting core voltage failed.\n");
599
600         /* Configure board SERDES ports crossbar */
601         config_frontside_crossbar_vsc3316();
602         config_backside_crossbar_mux();
603         select_i2c_ch_pca9547(I2C_MUX_CH_DEFAULT, 0);
604
605         return 0;
606 }
607
608 unsigned long get_board_sys_clk(void)
609 {
610         u8 sysclk_conf = QIXIS_READ(brdcfg[1]);
611 #ifdef CONFIG_FSL_QIXIS_CLOCK_MEASUREMENT
612         /* use accurate clock measurement */
613         int freq = QIXIS_READ(clk_freq[0]) << 8 | QIXIS_READ(clk_freq[1]);
614         int base = QIXIS_READ(clk_base[0]) << 8 | QIXIS_READ(clk_base[1]);
615         u32 val;
616
617         val =  freq * base;
618         if (val) {
619                 debug("SYS Clock measurement is: %d\n", val);
620                 return val;
621         } else {
622                 printf("Warning: SYS clock measurement is invalid, using value from brdcfg1.\n");
623         }
624 #endif
625
626         switch (sysclk_conf & 0x0F) {
627         case QIXIS_SYSCLK_83:
628                 return 83333333;
629         case QIXIS_SYSCLK_100:
630                 return 100000000;
631         case QIXIS_SYSCLK_125:
632                 return 125000000;
633         case QIXIS_SYSCLK_133:
634                 return 133333333;
635         case QIXIS_SYSCLK_150:
636                 return 150000000;
637         case QIXIS_SYSCLK_160:
638                 return 160000000;
639         case QIXIS_SYSCLK_166:
640                 return 166666666;
641         }
642         return 66666666;
643 }
644
645 unsigned long get_board_ddr_clk(void)
646 {
647         u8 ddrclk_conf = QIXIS_READ(brdcfg[1]);
648 #ifdef CONFIG_FSL_QIXIS_CLOCK_MEASUREMENT
649         /* use accurate clock measurement */
650         int freq = QIXIS_READ(clk_freq[2]) << 8 | QIXIS_READ(clk_freq[3]);
651         int base = QIXIS_READ(clk_base[0]) << 8 | QIXIS_READ(clk_base[1]);
652         u32 val;
653
654         val =  freq * base;
655         if (val) {
656                 debug("DDR Clock measurement is: %d\n", val);
657                 return val;
658         } else {
659                 printf("Warning: DDR clock measurement is invalid, using value from brdcfg1.\n");
660         }
661 #endif
662
663         switch ((ddrclk_conf & 0x30) >> 4) {
664         case QIXIS_DDRCLK_100:
665                 return 100000000;
666         case QIXIS_DDRCLK_125:
667                 return 125000000;
668         case QIXIS_DDRCLK_133:
669                 return 133333333;
670         }
671         return 66666666;
672 }
673
674 int misc_init_r(void)
675 {
676         u8 sw;
677         void *srds_base = (void *)CONFIG_SYS_FSL_CORENET_SERDES_ADDR;
678         serdes_corenet_t *srds_regs;
679         u32 actual[MAX_SERDES];
680         u32 pllcr0, expected;
681         unsigned int i;
682
683         sw = QIXIS_READ(brdcfg[2]);
684         for (i = 0; i < MAX_SERDES; i++) {
685                 unsigned int clock = (sw >> (6 - 2 * i)) & 3;
686                 switch (clock) {
687                 case 0:
688                         actual[i] = SRDS_PLLCR0_RFCK_SEL_100;
689                         break;
690                 case 1:
691                         actual[i] = SRDS_PLLCR0_RFCK_SEL_125;
692                         break;
693                 case 2:
694                         actual[i] = SRDS_PLLCR0_RFCK_SEL_156_25;
695                         break;
696                 case 3:
697                         actual[i] = SRDS_PLLCR0_RFCK_SEL_161_13;
698                         break;
699                 }
700         }
701
702         for (i = 0; i < MAX_SERDES; i++) {
703                 srds_regs = srds_base + i * 0x1000;
704                 pllcr0 = srds_regs->bank[0].pllcr0;
705                 expected = pllcr0 & SRDS_PLLCR0_RFCK_SEL_MASK;
706                 if (expected != actual[i]) {
707                         printf("Warning: SERDES%u expects reference clock %sMHz, but actual is %sMHz\n",
708                                i + 1, serdes_clock_to_string(expected),
709                                serdes_clock_to_string(actual[i]));
710                 }
711         }
712
713         return 0;
714 }
715
716 int ft_board_setup(void *blob, bd_t *bd)
717 {
718         phys_addr_t base;
719         phys_size_t size;
720
721         ft_cpu_setup(blob, bd);
722
723         base = env_get_bootm_low();
724         size = env_get_bootm_size();
725
726         fdt_fixup_memory(blob, (u64)base, (u64)size);
727
728 #ifdef CONFIG_PCI
729         pci_of_setup(blob, bd);
730 #endif
731
732         fdt_fixup_liodn(blob);
733         fsl_fdt_fixup_dr_usb(blob, bd);
734
735 #ifdef CONFIG_SYS_DPAA_FMAN
736         fdt_fixup_fman_ethernet(blob);
737         fdt_fixup_board_enet(blob);
738 #endif
739
740         return 0;
741 }
742
743 /*
744  * This function is called by bdinfo to print detail board information.
745  * As an exmaple for future board, we organize the messages into
746  * several sections. If applicable, the message is in the format of
747  * <name>      = <value>
748  * It should aligned with normal output of bdinfo command.
749  *
750  * Voltage: Core, DDR and another configurable voltages
751  * Clock  : Critical clocks which are not printed already
752  * RCW    : RCW source if not printed already
753  * Misc   : Other important information not in above catagories
754  */
755 void board_detail(void)
756 {
757         int i;
758         u8 brdcfg[16], dutcfg[16], rst_ctl;
759         int vdd, rcwsrc;
760         static const char * const clk[] = {"66.67", "100", "125", "133.33"};
761
762         for (i = 0; i < 16; i++) {
763                 brdcfg[i] = qixis_read(offsetof(struct qixis, brdcfg[0]) + i);
764                 dutcfg[i] = qixis_read(offsetof(struct qixis, dutcfg[0]) + i);
765         }
766
767         /* Voltage secion */
768         if (!select_i2c_ch_pca9547(I2C_MUX_CH_VOL_MONITOR, 0)) {
769                 vdd = read_voltage();
770                 if (vdd > 0)
771                         printf("Core voltage= %d mV\n", vdd);
772                 select_i2c_ch_pca9547(I2C_MUX_CH_DEFAULT, 0);
773         }
774
775         printf("XVDD        = 1.%d V\n", ((brdcfg[8] & 0xf) - 4) * 5 + 25);
776
777         /* clock section */
778         printf("SYSCLK      = %s MHz\nDDRCLK      = %s MHz\n",
779                clk[(brdcfg[11] >> 2) & 0x3], clk[brdcfg[11] & 3]);
780
781         /* RCW section */
782         rcwsrc = (dutcfg[0] << 1) + (dutcfg[1] & 1);
783         puts("RCW source  = ");
784         switch (rcwsrc) {
785         case 0x017:
786         case 0x01f:
787                 puts("8-bit NOR\n");
788                 break;
789         case 0x027:
790         case 0x02F:
791                 puts("16-bit NOR\n");
792                 break;
793         case 0x040:
794                 puts("SDHC/eMMC\n");
795                 break;
796         case 0x044:
797                 puts("SPI 16-bit addressing\n");
798                 break;
799         case 0x045:
800                 puts("SPI 24-bit addressing\n");
801                 break;
802         case 0x048:
803                 puts("I2C normal addressing\n");
804                 break;
805         case 0x049:
806                 puts("I2C extended addressing\n");
807                 break;
808         case 0x108:
809         case 0x109:
810         case 0x10a:
811         case 0x10b:
812                 puts("8-bit NAND, 2KB\n");
813                 break;
814         default:
815                 if ((rcwsrc >= 0x080) && (rcwsrc <= 0x09f))
816                         puts("Hard-coded RCW\n");
817                 else if ((rcwsrc >= 0x110) && (rcwsrc <= 0x11f))
818                         puts("8-bit NAND, 4KB\n");
819                 else
820                         puts("unknown\n");
821                 break;
822         }
823
824         /* Misc section */
825         rst_ctl = QIXIS_READ(rst_ctl);
826         puts("HRESET_REQ  = ");
827         switch (rst_ctl & 0x30) {
828         case 0x00:
829                 puts("Ignored\n");
830                 break;
831         case 0x10:
832                 puts("Assert HRESET\n");
833                 break;
834         case 0x30:
835                 puts("Reset system\n");
836                 break;
837         default:
838                 puts("N/A\n");
839                 break;
840         }
841 }
842
843 /*
844  * Reverse engineering switch settings.
845  * Some bits cannot be figured out. They will be displayed as
846  * underscore in binary format. mask[] has those bits.
847  * Some bits are calculated differently than the actual switches
848  * if booting with overriding by FPGA.
849  */
850 void qixis_dump_switch(void)
851 {
852         int i;
853         u8 sw[9];
854
855         /*
856          * Any bit with 1 means that bit cannot be reverse engineered.
857          * It will be displayed as _ in binary format.
858          */
859         static const u8 mask[] = {0, 0, 0, 0, 0, 0x1, 0xcf, 0x3f, 0x1f};
860         char buf[10];
861         u8 brdcfg[16], dutcfg[16];
862
863         for (i = 0; i < 16; i++) {
864                 brdcfg[i] = qixis_read(offsetof(struct qixis, brdcfg[0]) + i);
865                 dutcfg[i] = qixis_read(offsetof(struct qixis, dutcfg[0]) + i);
866         }
867
868         sw[0] = dutcfg[0];
869         sw[1] = (dutcfg[1] << 0x07)             |
870                 ((dutcfg[12] & 0xC0) >> 1)      |
871                 ((dutcfg[11] & 0xE0) >> 3)      |
872                 ((dutcfg[6] & 0x80) >> 6)       |
873                 ((dutcfg[1] & 0x80) >> 7);
874         sw[2] = ((brdcfg[1] & 0x0f) << 4)       |
875                 ((brdcfg[1] & 0x30) >> 2)       |
876                 ((brdcfg[1] & 0x40) >> 5)       |
877                 ((brdcfg[1] & 0x80) >> 7);
878         sw[3] = brdcfg[2];
879         sw[4] = ((dutcfg[2] & 0x01) << 7)       |
880                 ((dutcfg[2] & 0x06) << 4)       |
881                 ((~QIXIS_READ(present)) & 0x10) |
882                 ((brdcfg[3] & 0x80) >> 4)       |
883                 ((brdcfg[3] & 0x01) << 2)       |
884                 ((brdcfg[6] == 0x62) ? 3 :
885                 ((brdcfg[6] == 0x5a) ? 2 :
886                 ((brdcfg[6] == 0x5e) ? 1 : 0)));
887         sw[5] = ((brdcfg[0] & 0x0f) << 4)       |
888                 ((QIXIS_READ(rst_ctl) & 0x30) >> 2) |
889                 ((brdcfg[0] & 0x40) >> 5);
890         sw[6] = (brdcfg[11] & 0x20)             |
891                 ((brdcfg[5] & 0x02) << 3);
892         sw[7] = (((~QIXIS_READ(rst_ctl)) & 0x40) << 1) |
893                 ((brdcfg[5] & 0x10) << 2);
894         sw[8] = ((brdcfg[12] & 0x08) << 4)      |
895                 ((brdcfg[12] & 0x03) << 5);
896
897         puts("DIP switch (reverse-engineering)\n");
898         for (i = 0; i < 9; i++) {
899                 printf("SW%d         = 0b%s (0x%02x)\n",
900                        i + 1, byte_to_binary_mask(sw[i], mask[i], buf), sw[i]);
901         }
902 }
903
904 static int do_vdd_adjust(cmd_tbl_t *cmdtp,
905                          int flag, int argc,
906                          char * const argv[])
907 {
908         ulong override;
909
910         if (argc < 2)
911                 return CMD_RET_USAGE;
912         if (!strict_strtoul(argv[1], 10, &override))
913                 adjust_vdd(override);   /* the value is checked by callee */
914         else
915                 return CMD_RET_USAGE;
916
917         return 0;
918 }
919
920 U_BOOT_CMD(
921         vdd_override, 2, 0, do_vdd_adjust,
922         "Override VDD",
923         "- override with the voltage specified in mV, eg. 1050"
924 );