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