Merge branch '2019-08-11-master-imports'
[platform/kernel/u-boot.git] / board / freescale / common / vid.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2014 Freescale Semiconductor, Inc.
4  */
5
6 #include <common.h>
7 #include <command.h>
8 #include <env.h>
9 #include <i2c.h>
10 #include <asm/io.h>
11 #ifdef CONFIG_FSL_LSCH2
12 #include <asm/arch/immap_lsch2.h>
13 #elif defined(CONFIG_FSL_LSCH3)
14 #include <asm/arch/immap_lsch3.h>
15 #else
16 #include <asm/immap_85xx.h>
17 #endif
18 #include "vid.h"
19
20 int __weak i2c_multiplexer_select_vid_channel(u8 channel)
21 {
22         return 0;
23 }
24
25 /*
26  * Compensate for a board specific voltage drop between regulator and SoC
27  * return a value in mV
28  */
29 int __weak board_vdd_drop_compensation(void)
30 {
31         return 0;
32 }
33
34 /*
35  * Board specific settings for specific voltage value
36  */
37 int __weak board_adjust_vdd(int vdd)
38 {
39         return 0;
40 }
41
42 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
43         defined(CONFIG_VOL_MONITOR_IR36021_READ)
44 /*
45  * Get the i2c address configuration for the IR regulator chip
46  *
47  * There are some variance in the RDB HW regarding the I2C address configuration
48  * for the IR regulator chip, which is likely a problem of external resistor
49  * accuracy. So we just check each address in a hopefully non-intrusive mode
50  * and use the first one that seems to work
51  *
52  * The IR chip can show up under the following addresses:
53  * 0x08 (Verified on T1040RDB-PA,T4240RDB-PB,X-T4240RDB-16GPA)
54  * 0x09 (Verified on T1040RDB-PA)
55  * 0x38 (Verified on T2080QDS, T2081QDS, T4240RDB)
56  */
57 static int find_ir_chip_on_i2c(void)
58 {
59         int i2caddress;
60         int ret;
61         u8 byte;
62         int i;
63         const int ir_i2c_addr[] = {0x38, 0x08, 0x09};
64
65         /* Check all the address */
66         for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) {
67                 i2caddress = ir_i2c_addr[i];
68                 ret = i2c_read(i2caddress,
69                                IR36021_MFR_ID_OFFSET, 1, (void *)&byte,
70                                sizeof(byte));
71                 if ((ret >= 0) && (byte == IR36021_MFR_ID))
72                         return i2caddress;
73         }
74         return -1;
75 }
76 #endif
77
78 /* Maximum loop count waiting for new voltage to take effect */
79 #define MAX_LOOP_WAIT_NEW_VOL           100
80 /* Maximum loop count waiting for the voltage to be stable */
81 #define MAX_LOOP_WAIT_VOL_STABLE        100
82 /*
83  * read_voltage from sensor on I2C bus
84  * We use average of 4 readings, waiting for WAIT_FOR_ADC before
85  * another reading
86  */
87 #define NUM_READINGS    4       /* prefer to be power of 2 for efficiency */
88
89 /* If an INA220 chip is available, we can use it to read back the voltage
90  * as it may have a higher accuracy than the IR chip for the same purpose
91  */
92 #ifdef CONFIG_VOL_MONITOR_INA220
93 #define WAIT_FOR_ADC    532     /* wait for 532 microseconds for ADC */
94 #define ADC_MIN_ACCURACY        4
95 #else
96 #define WAIT_FOR_ADC    138     /* wait for 138 microseconds for ADC */
97 #define ADC_MIN_ACCURACY        4
98 #endif
99
100 #ifdef CONFIG_VOL_MONITOR_INA220
101 static int read_voltage_from_INA220(int i2caddress)
102 {
103         int i, ret, voltage_read = 0;
104         u16 vol_mon;
105         u8 buf[2];
106
107         for (i = 0; i < NUM_READINGS; i++) {
108                 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
109                                I2C_VOL_MONITOR_BUS_V_OFFSET, 1,
110                                (void *)&buf, 2);
111                 if (ret) {
112                         printf("VID: failed to read core voltage\n");
113                         return ret;
114                 }
115                 vol_mon = (buf[0] << 8) | buf[1];
116                 if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
117                         printf("VID: Core voltage sensor error\n");
118                         return -1;
119                 }
120                 debug("VID: bus voltage reads 0x%04x\n", vol_mon);
121                 /* LSB = 4mv */
122                 voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
123                 udelay(WAIT_FOR_ADC);
124         }
125         /* calculate the average */
126         voltage_read /= NUM_READINGS;
127
128         return voltage_read;
129 }
130 #endif
131
132 /* read voltage from IR */
133 #ifdef CONFIG_VOL_MONITOR_IR36021_READ
134 static int read_voltage_from_IR(int i2caddress)
135 {
136         int i, ret, voltage_read = 0;
137         u16 vol_mon;
138         u8 buf;
139
140         for (i = 0; i < NUM_READINGS; i++) {
141                 ret = i2c_read(i2caddress,
142                                IR36021_LOOP1_VOUT_OFFSET,
143                                1, (void *)&buf, 1);
144                 if (ret) {
145                         printf("VID: failed to read vcpu\n");
146                         return ret;
147                 }
148                 vol_mon = buf;
149                 if (!vol_mon) {
150                         printf("VID: Core voltage sensor error\n");
151                         return -1;
152                 }
153                 debug("VID: bus voltage reads 0x%02x\n", vol_mon);
154                 /* Resolution is 1/128V. We scale up here to get 1/128mV
155                  * and divide at the end
156                  */
157                 voltage_read += vol_mon * 1000;
158                 udelay(WAIT_FOR_ADC);
159         }
160         /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
161         voltage_read = DIV_ROUND_UP(voltage_read, 128);
162
163         /* calculate the average */
164         voltage_read /= NUM_READINGS;
165
166         /* Compensate for a board specific voltage drop between regulator and
167          * SoC before converting into an IR VID value
168          */
169         voltage_read -= board_vdd_drop_compensation();
170
171         return voltage_read;
172 }
173 #endif
174
175 #ifdef CONFIG_VOL_MONITOR_LTC3882_READ
176 /* read the current value of the LTC Regulator Voltage */
177 static int read_voltage_from_LTC(int i2caddress)
178 {
179         int  ret, vcode = 0;
180         u8 chan = PWM_CHANNEL0;
181
182         /* select the PAGE 0 using PMBus commands PAGE for VDD*/
183         ret = i2c_write(I2C_VOL_MONITOR_ADDR,
184                         PMBUS_CMD_PAGE, 1, &chan, 1);
185         if (ret) {
186                 printf("VID: failed to select VDD Page 0\n");
187                 return ret;
188         }
189
190         /*read the output voltage using PMBus command READ_VOUT*/
191         ret = i2c_read(I2C_VOL_MONITOR_ADDR,
192                        PMBUS_CMD_READ_VOUT, 1, (void *)&vcode, 2);
193         if (ret) {
194                 printf("VID: failed to read the volatge\n");
195                 return ret;
196         }
197
198         /* Scale down to the real mV as LTC resolution is 1/4096V,rounding up */
199         vcode = DIV_ROUND_UP(vcode * 1000, 4096);
200
201         return vcode;
202 }
203 #endif
204
205 static int read_voltage(int i2caddress)
206 {
207         int voltage_read;
208 #ifdef CONFIG_VOL_MONITOR_INA220
209         voltage_read = read_voltage_from_INA220(i2caddress);
210 #elif defined CONFIG_VOL_MONITOR_IR36021_READ
211         voltage_read = read_voltage_from_IR(i2caddress);
212 #elif defined CONFIG_VOL_MONITOR_LTC3882_READ
213         voltage_read = read_voltage_from_LTC(i2caddress);
214 #else
215         return -1;
216 #endif
217         return voltage_read;
218 }
219
220 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
221 /*
222  * We need to calculate how long before the voltage stops to drop
223  * or increase. It returns with the loop count. Each loop takes
224  * several readings (WAIT_FOR_ADC)
225  */
226 static int wait_for_new_voltage(int vdd, int i2caddress)
227 {
228         int timeout, vdd_current;
229
230         vdd_current = read_voltage(i2caddress);
231         /* wait until voltage starts to reach the target. Voltage slew
232          * rates by typical regulators will always lead to stable readings
233          * within each fairly long ADC interval in comparison to the
234          * intended voltage delta change until the target voltage is
235          * reached. The fairly small voltage delta change to any target
236          * VID voltage also means that this function will always complete
237          * within few iterations. If the timeout was ever reached, it would
238          * point to a serious failure in the regulator system.
239          */
240         for (timeout = 0;
241              abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
242              timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
243                 vdd_current = read_voltage(i2caddress);
244         }
245         if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
246                 printf("VID: Voltage adjustment timeout\n");
247                 return -1;
248         }
249         return timeout;
250 }
251
252 /*
253  * this function keeps reading the voltage until it is stable or until the
254  * timeout expires
255  */
256 static int wait_for_voltage_stable(int i2caddress)
257 {
258         int timeout, vdd_current, vdd;
259
260         vdd = read_voltage(i2caddress);
261         udelay(NUM_READINGS * WAIT_FOR_ADC);
262
263         /* wait until voltage is stable */
264         vdd_current = read_voltage(i2caddress);
265         /* The maximum timeout is
266          * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
267          */
268         for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
269              abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
270              timeout > 0; timeout--) {
271                 vdd = vdd_current;
272                 udelay(NUM_READINGS * WAIT_FOR_ADC);
273                 vdd_current = read_voltage(i2caddress);
274         }
275         if (timeout == 0)
276                 return -1;
277         return vdd_current;
278 }
279
280 /* Set the voltage to the IR chip */
281 static int set_voltage_to_IR(int i2caddress, int vdd)
282 {
283         int wait, vdd_last;
284         int ret;
285         u8 vid;
286
287         /* Compensate for a board specific voltage drop between regulator and
288          * SoC before converting into an IR VID value
289          */
290         vdd += board_vdd_drop_compensation();
291 #ifdef CONFIG_FSL_LSCH2
292         vid = DIV_ROUND_UP(vdd - 265, 5);
293 #else
294         vid = DIV_ROUND_UP(vdd - 245, 5);
295 #endif
296
297         ret = i2c_write(i2caddress, IR36021_LOOP1_MANUAL_ID_OFFSET,
298                         1, (void *)&vid, sizeof(vid));
299         if (ret) {
300                 printf("VID: failed to write VID\n");
301                 return -1;
302         }
303         wait = wait_for_new_voltage(vdd, i2caddress);
304         if (wait < 0)
305                 return -1;
306         debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
307
308         vdd_last = wait_for_voltage_stable(i2caddress);
309         if (vdd_last < 0)
310                 return -1;
311         debug("VID: Current voltage is %d mV\n", vdd_last);
312         return vdd_last;
313 }
314
315 #endif
316
317 #ifdef CONFIG_VOL_MONITOR_LTC3882_SET
318 /* this function sets the VDD and returns the value set */
319 static int set_voltage_to_LTC(int i2caddress, int vdd)
320 {
321         int ret, vdd_last, vdd_target = vdd;
322         int count = 100, temp = 0;
323
324         /* Scale up to the LTC resolution is 1/4096V */
325         vdd = (vdd * 4096) / 1000;
326
327         /* 5-byte buffer which needs to be sent following the
328          * PMBus command PAGE_PLUS_WRITE.
329          */
330         u8 buff[5] = {0x04, PWM_CHANNEL0, PMBUS_CMD_VOUT_COMMAND,
331                         vdd & 0xFF, (vdd & 0xFF00) >> 8};
332
333         /* Write the desired voltage code to the regulator */
334         ret = i2c_write(I2C_VOL_MONITOR_ADDR,
335                         PMBUS_CMD_PAGE_PLUS_WRITE, 1, (void *)&buff, 5);
336         if (ret) {
337                 printf("VID: I2C failed to write to the volatge regulator\n");
338                 return -1;
339         }
340
341         /* Wait for the volatge to get to the desired value */
342         do {
343                 vdd_last = read_voltage_from_LTC(i2caddress);
344                 if (vdd_last < 0) {
345                         printf("VID: Couldn't read sensor abort VID adjust\n");
346                         return -1;
347                 }
348                 count--;
349                 temp = vdd_last - vdd_target;
350         } while ((abs(temp) > 2)  && (count > 0));
351
352         return vdd_last;
353 }
354 #endif
355
356 static int set_voltage(int i2caddress, int vdd)
357 {
358         int vdd_last = -1;
359
360 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
361         vdd_last = set_voltage_to_IR(i2caddress, vdd);
362 #elif defined CONFIG_VOL_MONITOR_LTC3882_SET
363         vdd_last = set_voltage_to_LTC(i2caddress, vdd);
364 #else
365         #error Specific voltage monitor must be defined
366 #endif
367         return vdd_last;
368 }
369
370 #ifdef CONFIG_FSL_LSCH3
371 int adjust_vdd(ulong vdd_override)
372 {
373         int re_enable = disable_interrupts();
374         struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
375         u32 fusesr;
376 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
377         defined(CONFIG_VOL_MONITOR_IR36021_READ)
378         u8 vid, buf;
379 #else
380         u8 vid;
381 #endif
382         int vdd_target, vdd_current, vdd_last;
383         int ret, i2caddress;
384         unsigned long vdd_string_override;
385         char *vdd_string;
386 #ifdef CONFIG_ARCH_LX2160A
387         static const u16 vdd[32] = {
388                 8250,
389                 7875,
390                 7750,
391                 0,      /* reserved */
392                 0,      /* reserved */
393                 0,      /* reserved */
394                 0,      /* reserved */
395                 0,      /* reserved */
396                 0,      /* reserved */
397                 0,      /* reserved */
398                 0,      /* reserved */
399                 0,      /* reserved */
400                 0,      /* reserved */
401                 0,      /* reserved */
402                 0,      /* reserved */
403                 0,      /* reserved */
404                 8000,
405                 8125,
406                 8250,
407                 0,      /* reserved */
408                 8500,
409                 0,      /* reserved */
410                 0,      /* reserved */
411                 0,      /* reserved */
412                 0,      /* reserved */
413                 0,      /* reserved */
414                 0,      /* reserved */
415                 0,      /* reserved */
416                 0,      /* reserved */
417                 0,      /* reserved */
418                 0,      /* reserved */
419                 0,      /* reserved */
420         };
421 #else
422 #ifdef CONFIG_ARCH_LS1088A
423         static const uint16_t vdd[32] = {
424                 10250,
425                 9875,
426                 9750,
427                 0,      /* reserved */
428                 0,      /* reserved */
429                 0,      /* reserved */
430                 0,      /* reserved */
431                 0,      /* reserved */
432                 9000,
433                 0,      /* reserved */
434                 0,      /* reserved */
435                 0,      /* reserved */
436                 0,      /* reserved */
437                 0,      /* reserved */
438                 0,      /* reserved */
439                 0,      /* reserved */
440                 10000,  /* 1.0000V */
441                 10125,
442                 10250,
443                 0,      /* reserved */
444                 0,      /* reserved */
445                 0,      /* reserved */
446                 0,      /* reserved */
447                 0,      /* reserved */
448                 0,      /* reserved */
449                 0,      /* reserved */
450                 0,      /* reserved */
451                 0,      /* reserved */
452                 0,      /* reserved */
453                 0,      /* reserved */
454                 0,      /* reserved */
455                 0,      /* reserved */
456         };
457
458 #else
459         static const uint16_t vdd[32] = {
460                 10500,
461                 0,      /* reserved */
462                 9750,
463                 0,      /* reserved */
464                 9500,
465                 0,      /* reserved */
466                 0,      /* reserved */
467                 0,      /* reserved */
468                 0,      /* reserved */
469                 0,      /* reserved */
470                 0,      /* reserved */
471                 9000,      /* reserved */
472                 0,      /* reserved */
473                 0,      /* reserved */
474                 0,      /* reserved */
475                 0,      /* reserved */
476                 10000,  /* 1.0000V */
477                 0,      /* reserved */
478                 10250,
479                 0,      /* reserved */
480                 10500,
481                 0,      /* reserved */
482                 0,      /* reserved */
483                 0,      /* reserved */
484                 0,      /* reserved */
485                 0,      /* reserved */
486                 0,      /* reserved */
487                 0,      /* reserved */
488                 0,      /* reserved */
489                 0,      /* reserved */
490                 0,      /* reserved */
491                 0,      /* reserved */
492         };
493 #endif
494 #endif
495         struct vdd_drive {
496                 u8 vid;
497                 unsigned voltage;
498         };
499
500         ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
501         if (ret) {
502                 debug("VID: I2C failed to switch channel\n");
503                 ret = -1;
504                 goto exit;
505         }
506 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
507         defined(CONFIG_VOL_MONITOR_IR36021_READ)
508         ret = find_ir_chip_on_i2c();
509         if (ret < 0) {
510                 printf("VID: Could not find voltage regulator on I2C.\n");
511                 ret = -1;
512                 goto exit;
513         } else {
514                 i2caddress = ret;
515                 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
516         }
517
518         /* check IR chip work on Intel mode*/
519         ret = i2c_read(i2caddress,
520                        IR36021_INTEL_MODE_OOFSET,
521                        1, (void *)&buf, 1);
522         if (ret) {
523                 printf("VID: failed to read IR chip mode.\n");
524                 ret = -1;
525                 goto exit;
526         }
527         if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
528                 printf("VID: IR Chip is not used in Intel mode.\n");
529                 ret = -1;
530                 goto exit;
531         }
532 #endif
533
534         /* get the voltage ID from fuse status register */
535         fusesr = in_le32(&gur->dcfg_fusesr);
536         vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
537                 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
538         if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
539                 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
540                         FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
541         }
542         vdd_target = vdd[vid];
543
544         /* check override variable for overriding VDD */
545         vdd_string = env_get(CONFIG_VID_FLS_ENV);
546         if (vdd_override == 0 && vdd_string &&
547             !strict_strtoul(vdd_string, 10, &vdd_string_override))
548                 vdd_override = vdd_string_override;
549
550         if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
551                 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
552                 debug("VDD override is %lu\n", vdd_override);
553         } else if (vdd_override != 0) {
554                 printf("Invalid value.\n");
555         }
556
557         /* divide and round up by 10 to get a value in mV */
558         vdd_target = DIV_ROUND_UP(vdd_target, 10);
559         if (vdd_target == 0) {
560                 debug("VID: VID not used\n");
561                 ret = 0;
562                 goto exit;
563         } else if (vdd_target < VDD_MV_MIN || vdd_target > VDD_MV_MAX) {
564                 /* Check vdd_target is in valid range */
565                 printf("VID: Target VID %d mV is not in range.\n",
566                        vdd_target);
567                 ret = -1;
568                 goto exit;
569         } else {
570                 debug("VID: vid = %d mV\n", vdd_target);
571         }
572
573         /*
574          * Read voltage monitor to check real voltage.
575          */
576         vdd_last = read_voltage(i2caddress);
577         if (vdd_last < 0) {
578                 printf("VID: Couldn't read sensor abort VID adjustment\n");
579                 ret = -1;
580                 goto exit;
581         }
582         vdd_current = vdd_last;
583         debug("VID: Core voltage is currently at %d mV\n", vdd_last);
584
585 #ifdef CONFIG_VOL_MONITOR_LTC3882_SET
586         /* Set the target voltage */
587         vdd_last = vdd_current = set_voltage(i2caddress, vdd_target);
588 #else
589         /*
590           * Adjust voltage to at or one step above target.
591           * As measurements are less precise than setting the values
592           * we may run through dummy steps that cancel each other
593           * when stepping up and then down.
594           */
595         while (vdd_last > 0 &&
596                vdd_last < vdd_target) {
597                 vdd_current += IR_VDD_STEP_UP;
598                 vdd_last = set_voltage(i2caddress, vdd_current);
599         }
600         while (vdd_last > 0 &&
601                vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
602                 vdd_current -= IR_VDD_STEP_DOWN;
603                 vdd_last = set_voltage(i2caddress, vdd_current);
604         }
605
606 #endif
607         if (board_adjust_vdd(vdd_target) < 0) {
608                 ret = -1;
609                 goto exit;
610         }
611
612         if (vdd_last > 0)
613                 printf("VID: Core voltage after adjustment is at %d mV\n",
614                        vdd_last);
615         else
616                 ret = -1;
617 exit:
618         if (re_enable)
619                 enable_interrupts();
620         i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
621         return ret;
622 }
623 #else /* !CONFIG_FSL_LSCH3 */
624 int adjust_vdd(ulong vdd_override)
625 {
626         int re_enable = disable_interrupts();
627 #if defined(CONFIG_FSL_LSCH2)
628         struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
629 #else
630         ccsr_gur_t __iomem *gur =
631                 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
632 #endif
633         u32 fusesr;
634         u8 vid, buf;
635         int vdd_target, vdd_current, vdd_last;
636         int ret, i2caddress;
637         unsigned long vdd_string_override;
638         char *vdd_string;
639         static const uint16_t vdd[32] = {
640                 0,      /* unused */
641                 9875,   /* 0.9875V */
642                 9750,
643                 9625,
644                 9500,
645                 9375,
646                 9250,
647                 9125,
648                 9000,
649                 8875,
650                 8750,
651                 8625,
652                 8500,
653                 8375,
654                 8250,
655                 8125,
656                 10000,  /* 1.0000V */
657                 10125,
658                 10250,
659                 10375,
660                 10500,
661                 10625,
662                 10750,
663                 10875,
664                 11000,
665                 0,      /* reserved */
666         };
667         struct vdd_drive {
668                 u8 vid;
669                 unsigned voltage;
670         };
671
672         ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
673         if (ret) {
674                 debug("VID: I2C failed to switch channel\n");
675                 ret = -1;
676                 goto exit;
677         }
678 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
679         defined(CONFIG_VOL_MONITOR_IR36021_READ)
680         ret = find_ir_chip_on_i2c();
681         if (ret < 0) {
682                 printf("VID: Could not find voltage regulator on I2C.\n");
683                 ret = -1;
684                 goto exit;
685         } else {
686                 i2caddress = ret;
687                 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
688         }
689
690         /* check IR chip work on Intel mode*/
691         ret = i2c_read(i2caddress,
692                        IR36021_INTEL_MODE_OOFSET,
693                        1, (void *)&buf, 1);
694         if (ret) {
695                 printf("VID: failed to read IR chip mode.\n");
696                 ret = -1;
697                 goto exit;
698         }
699         if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
700                 printf("VID: IR Chip is not used in Intel mode.\n");
701                 ret = -1;
702                 goto exit;
703         }
704 #endif
705
706         /* get the voltage ID from fuse status register */
707         fusesr = in_be32(&gur->dcfg_fusesr);
708         /*
709          * VID is used according to the table below
710          *                ---------------------------------------
711          *                |                DA_V                 |
712          *                |-------------------------------------|
713          *                | 5b00000 | 5b00001-5b11110 | 5b11111 |
714          * ---------------+---------+-----------------+---------|
715          * | D | 5b00000  | NO VID  | VID = DA_V      | NO VID  |
716          * | A |----------+---------+-----------------+---------|
717          * | _ | 5b00001  |VID =    | VID =           |VID =    |
718          * | V |   ~      | DA_V_ALT|   DA_V_ALT      | DA_A_VLT|
719          * | _ | 5b11110  |         |                 |         |
720          * | A |----------+---------+-----------------+---------|
721          * | L | 5b11111  | No VID  | VID = DA_V      | NO VID  |
722          * | T |          |         |                 |         |
723          * ------------------------------------------------------
724          */
725 #ifdef CONFIG_FSL_LSCH2
726         vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
727                 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
728         if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
729                 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
730                         FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
731         }
732 #else
733         vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
734                 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
735         if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
736                 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
737                         FSL_CORENET_DCFG_FUSESR_VID_MASK;
738         }
739 #endif
740         vdd_target = vdd[vid];
741
742         /* check override variable for overriding VDD */
743         vdd_string = env_get(CONFIG_VID_FLS_ENV);
744         if (vdd_override == 0 && vdd_string &&
745             !strict_strtoul(vdd_string, 10, &vdd_string_override))
746                 vdd_override = vdd_string_override;
747         if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
748                 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
749                 debug("VDD override is %lu\n", vdd_override);
750         } else if (vdd_override != 0) {
751                 printf("Invalid value.\n");
752         }
753         if (vdd_target == 0) {
754                 debug("VID: VID not used\n");
755                 ret = 0;
756                 goto exit;
757         } else {
758                 /* divide and round up by 10 to get a value in mV */
759                 vdd_target = DIV_ROUND_UP(vdd_target, 10);
760                 debug("VID: vid = %d mV\n", vdd_target);
761         }
762
763         /*
764          * Read voltage monitor to check real voltage.
765          */
766         vdd_last = read_voltage(i2caddress);
767         if (vdd_last < 0) {
768                 printf("VID: Couldn't read sensor abort VID adjustment\n");
769                 ret = -1;
770                 goto exit;
771         }
772         vdd_current = vdd_last;
773         debug("VID: Core voltage is currently at %d mV\n", vdd_last);
774         /*
775           * Adjust voltage to at or one step above target.
776           * As measurements are less precise than setting the values
777           * we may run through dummy steps that cancel each other
778           * when stepping up and then down.
779           */
780         while (vdd_last > 0 &&
781                vdd_last < vdd_target) {
782                 vdd_current += IR_VDD_STEP_UP;
783                 vdd_last = set_voltage(i2caddress, vdd_current);
784         }
785         while (vdd_last > 0 &&
786                vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
787                 vdd_current -= IR_VDD_STEP_DOWN;
788                 vdd_last = set_voltage(i2caddress, vdd_current);
789         }
790
791         if (vdd_last > 0)
792                 printf("VID: Core voltage after adjustment is at %d mV\n",
793                        vdd_last);
794         else
795                 ret = -1;
796 exit:
797         if (re_enable)
798                 enable_interrupts();
799
800         i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
801
802         return ret;
803 }
804 #endif
805
806 static int print_vdd(void)
807 {
808         int vdd_last, ret, i2caddress;
809
810         ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
811         if (ret) {
812                 debug("VID : I2c failed to switch channel\n");
813                 return -1;
814         }
815 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
816         defined(CONFIG_VOL_MONITOR_IR36021_READ)
817         ret = find_ir_chip_on_i2c();
818         if (ret < 0) {
819                 printf("VID: Could not find voltage regulator on I2C.\n");
820                 goto exit;
821         } else {
822                 i2caddress = ret;
823                 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
824         }
825 #endif
826
827         /*
828          * Read voltage monitor to check real voltage.
829          */
830         vdd_last = read_voltage(i2caddress);
831         if (vdd_last < 0) {
832                 printf("VID: Couldn't read sensor abort VID adjustment\n");
833                 goto exit;
834         }
835         printf("VID: Core voltage is at %d mV\n", vdd_last);
836 exit:
837         i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
838
839         return ret < 0 ? -1 : 0;
840
841 }
842
843 static int do_vdd_override(cmd_tbl_t *cmdtp,
844                            int flag, int argc,
845                            char * const argv[])
846 {
847         ulong override;
848
849         if (argc < 2)
850                 return CMD_RET_USAGE;
851
852         if (!strict_strtoul(argv[1], 10, &override))
853                 adjust_vdd(override);   /* the value is checked by callee */
854         else
855                 return CMD_RET_USAGE;
856         return 0;
857 }
858
859 static int do_vdd_read(cmd_tbl_t *cmdtp,
860                          int flag, int argc,
861                          char * const argv[])
862 {
863         if (argc < 1)
864                 return CMD_RET_USAGE;
865         print_vdd();
866
867         return 0;
868 }
869
870 U_BOOT_CMD(
871         vdd_override, 2, 0, do_vdd_override,
872         "override VDD",
873         " - override with the voltage specified in mV, eg. 1050"
874 );
875
876 U_BOOT_CMD(
877         vdd_read, 1, 0, do_vdd_read,
878         "read VDD",
879         " - Read the voltage specified in mV"
880 )