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