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