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