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