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