blob: b8049719816f8c756684f4b9b25c44cf3db18b95 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ying Zhang8876a512014-10-31 18:06:18 +08002/*
3 * Copyright 2014 Freescale Semiconductor, Inc.
Ying Zhang8876a512014-10-31 18:06:18 +08004 */
5
6#include <common.h>
7#include <command.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -06008#include <env.h>
Ying Zhang8876a512014-10-31 18:06:18 +08009#include <i2c.h>
Shaohui Xiedd335672015-11-11 17:58:37 +080010#include <asm/io.h>
Shaohui Xie085ac1c2016-09-07 17:56:14 +080011#ifdef CONFIG_FSL_LSCH2
Shaohui Xiedd335672015-11-11 17:58:37 +080012#include <asm/arch/immap_lsch2.h>
Rai Harninder6aa1f3b2016-03-23 17:04:38 +053013#elif defined(CONFIG_FSL_LSCH3)
14#include <asm/arch/immap_lsch3.h>
Shaohui Xiedd335672015-11-11 17:58:37 +080015#else
Ying Zhang8876a512014-10-31 18:06:18 +080016#include <asm/immap_85xx.h>
Shaohui Xiedd335672015-11-11 17:58:37 +080017#endif
Ying Zhang8876a512014-10-31 18:06:18 +080018#include "vid.h"
19
Ying Zhang8876a512014-10-31 18:06:18 +080020int __weak i2c_multiplexer_select_vid_channel(u8 channel)
21{
22 return 0;
23}
24
25/*
26 * Compensate for a board specific voltage drop between regulator and SoC
27 * return a value in mV
28 */
29int __weak board_vdd_drop_compensation(void)
30{
31 return 0;
32}
33
34/*
Rajesh Bhagatc3a662f2018-01-17 16:13:02 +053035 * Board specific settings for specific voltage value
36 */
37int __weak board_adjust_vdd(int vdd)
38{
39 return 0;
40}
41
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +053042#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
43 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Rajesh Bhagatc3a662f2018-01-17 16:13:02 +053044/*
Ying Zhang8876a512014-10-31 18:06:18 +080045 * Get the i2c address configuration for the IR regulator chip
46 *
47 * There are some variance in the RDB HW regarding the I2C address configuration
48 * for the IR regulator chip, which is likely a problem of external resistor
49 * accuracy. So we just check each address in a hopefully non-intrusive mode
50 * and use the first one that seems to work
51 *
52 * The IR chip can show up under the following addresses:
53 * 0x08 (Verified on T1040RDB-PA,T4240RDB-PB,X-T4240RDB-16GPA)
54 * 0x09 (Verified on T1040RDB-PA)
Ying Zhangff779052016-01-22 12:15:13 +080055 * 0x38 (Verified on T2080QDS, T2081QDS, T4240RDB)
Ying Zhang8876a512014-10-31 18:06:18 +080056 */
57static int find_ir_chip_on_i2c(void)
58{
59 int i2caddress;
60 int ret;
61 u8 byte;
62 int i;
63 const int ir_i2c_addr[] = {0x38, 0x08, 0x09};
64
65 /* Check all the address */
66 for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) {
67 i2caddress = ir_i2c_addr[i];
68 ret = i2c_read(i2caddress,
69 IR36021_MFR_ID_OFFSET, 1, (void *)&byte,
70 sizeof(byte));
71 if ((ret >= 0) && (byte == IR36021_MFR_ID))
72 return i2caddress;
73 }
74 return -1;
75}
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +053076#endif
Ying Zhang8876a512014-10-31 18:06:18 +080077
78/* Maximum loop count waiting for new voltage to take effect */
79#define MAX_LOOP_WAIT_NEW_VOL 100
80/* Maximum loop count waiting for the voltage to be stable */
81#define MAX_LOOP_WAIT_VOL_STABLE 100
82/*
83 * read_voltage from sensor on I2C bus
84 * We use average of 4 readings, waiting for WAIT_FOR_ADC before
85 * another reading
86 */
87#define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
88
89/* If an INA220 chip is available, we can use it to read back the voltage
90 * as it may have a higher accuracy than the IR chip for the same purpose
91 */
92#ifdef CONFIG_VOL_MONITOR_INA220
93#define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
94#define ADC_MIN_ACCURACY 4
95#else
96#define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
97#define ADC_MIN_ACCURACY 4
98#endif
99
100#ifdef CONFIG_VOL_MONITOR_INA220
101static int read_voltage_from_INA220(int i2caddress)
102{
103 int i, ret, voltage_read = 0;
104 u16 vol_mon;
105 u8 buf[2];
106
107 for (i = 0; i < NUM_READINGS; i++) {
108 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
109 I2C_VOL_MONITOR_BUS_V_OFFSET, 1,
110 (void *)&buf, 2);
111 if (ret) {
112 printf("VID: failed to read core voltage\n");
113 return ret;
114 }
115 vol_mon = (buf[0] << 8) | buf[1];
116 if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
117 printf("VID: Core voltage sensor error\n");
118 return -1;
119 }
120 debug("VID: bus voltage reads 0x%04x\n", vol_mon);
121 /* LSB = 4mv */
122 voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
123 udelay(WAIT_FOR_ADC);
124 }
125 /* calculate the average */
126 voltage_read /= NUM_READINGS;
127
128 return voltage_read;
129}
130#endif
131
132/* read voltage from IR */
133#ifdef CONFIG_VOL_MONITOR_IR36021_READ
134static int read_voltage_from_IR(int i2caddress)
135{
136 int i, ret, voltage_read = 0;
137 u16 vol_mon;
138 u8 buf;
139
140 for (i = 0; i < NUM_READINGS; i++) {
141 ret = i2c_read(i2caddress,
142 IR36021_LOOP1_VOUT_OFFSET,
143 1, (void *)&buf, 1);
144 if (ret) {
145 printf("VID: failed to read vcpu\n");
146 return ret;
147 }
148 vol_mon = buf;
149 if (!vol_mon) {
150 printf("VID: Core voltage sensor error\n");
151 return -1;
152 }
153 debug("VID: bus voltage reads 0x%02x\n", vol_mon);
154 /* Resolution is 1/128V. We scale up here to get 1/128mV
155 * and divide at the end
156 */
157 voltage_read += vol_mon * 1000;
158 udelay(WAIT_FOR_ADC);
159 }
160 /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
161 voltage_read = DIV_ROUND_UP(voltage_read, 128);
162
163 /* calculate the average */
164 voltage_read /= NUM_READINGS;
165
166 /* Compensate for a board specific voltage drop between regulator and
167 * SoC before converting into an IR VID value
168 */
169 voltage_read -= board_vdd_drop_compensation();
170
171 return voltage_read;
172}
173#endif
174
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530175#ifdef CONFIG_VOL_MONITOR_LTC3882_READ
176/* read the current value of the LTC Regulator Voltage */
177static int read_voltage_from_LTC(int i2caddress)
178{
179 int ret, vcode = 0;
180 u8 chan = PWM_CHANNEL0;
181
182 /* select the PAGE 0 using PMBus commands PAGE for VDD*/
183 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
184 PMBUS_CMD_PAGE, 1, &chan, 1);
185 if (ret) {
186 printf("VID: failed to select VDD Page 0\n");
187 return ret;
188 }
189
190 /*read the output voltage using PMBus command READ_VOUT*/
191 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
192 PMBUS_CMD_READ_VOUT, 1, (void *)&vcode, 2);
193 if (ret) {
194 printf("VID: failed to read the volatge\n");
195 return ret;
196 }
197
198 /* Scale down to the real mV as LTC resolution is 1/4096V,rounding up */
199 vcode = DIV_ROUND_UP(vcode * 1000, 4096);
200
201 return vcode;
202}
203#endif
204
Ying Zhang8876a512014-10-31 18:06:18 +0800205static int read_voltage(int i2caddress)
206{
207 int voltage_read;
208#ifdef CONFIG_VOL_MONITOR_INA220
209 voltage_read = read_voltage_from_INA220(i2caddress);
210#elif defined CONFIG_VOL_MONITOR_IR36021_READ
211 voltage_read = read_voltage_from_IR(i2caddress);
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530212#elif defined CONFIG_VOL_MONITOR_LTC3882_READ
213 voltage_read = read_voltage_from_LTC(i2caddress);
Ying Zhang8876a512014-10-31 18:06:18 +0800214#else
215 return -1;
216#endif
217 return voltage_read;
218}
219
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530220#ifdef CONFIG_VOL_MONITOR_IR36021_SET
Ying Zhang8876a512014-10-31 18:06:18 +0800221/*
222 * We need to calculate how long before the voltage stops to drop
223 * or increase. It returns with the loop count. Each loop takes
224 * several readings (WAIT_FOR_ADC)
225 */
226static int wait_for_new_voltage(int vdd, int i2caddress)
227{
228 int timeout, vdd_current;
229
230 vdd_current = read_voltage(i2caddress);
231 /* wait until voltage starts to reach the target. Voltage slew
232 * rates by typical regulators will always lead to stable readings
233 * within each fairly long ADC interval in comparison to the
234 * intended voltage delta change until the target voltage is
235 * reached. The fairly small voltage delta change to any target
236 * VID voltage also means that this function will always complete
237 * within few iterations. If the timeout was ever reached, it would
238 * point to a serious failure in the regulator system.
239 */
240 for (timeout = 0;
241 abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
242 timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
243 vdd_current = read_voltage(i2caddress);
244 }
245 if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
246 printf("VID: Voltage adjustment timeout\n");
247 return -1;
248 }
249 return timeout;
250}
251
252/*
253 * this function keeps reading the voltage until it is stable or until the
254 * timeout expires
255 */
256static int wait_for_voltage_stable(int i2caddress)
257{
258 int timeout, vdd_current, vdd;
259
260 vdd = read_voltage(i2caddress);
261 udelay(NUM_READINGS * WAIT_FOR_ADC);
262
263 /* wait until voltage is stable */
264 vdd_current = read_voltage(i2caddress);
265 /* The maximum timeout is
266 * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
267 */
268 for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
269 abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
270 timeout > 0; timeout--) {
271 vdd = vdd_current;
272 udelay(NUM_READINGS * WAIT_FOR_ADC);
273 vdd_current = read_voltage(i2caddress);
274 }
275 if (timeout == 0)
276 return -1;
277 return vdd_current;
278}
279
Ying Zhang8876a512014-10-31 18:06:18 +0800280/* Set the voltage to the IR chip */
281static int set_voltage_to_IR(int i2caddress, int vdd)
282{
283 int wait, vdd_last;
284 int ret;
285 u8 vid;
286
287 /* Compensate for a board specific voltage drop between regulator and
288 * SoC before converting into an IR VID value
289 */
290 vdd += board_vdd_drop_compensation();
Shaohui Xie085ac1c2016-09-07 17:56:14 +0800291#ifdef CONFIG_FSL_LSCH2
Shaohui Xiedd335672015-11-11 17:58:37 +0800292 vid = DIV_ROUND_UP(vdd - 265, 5);
293#else
Ying Zhang8876a512014-10-31 18:06:18 +0800294 vid = DIV_ROUND_UP(vdd - 245, 5);
Shaohui Xiedd335672015-11-11 17:58:37 +0800295#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800296
297 ret = i2c_write(i2caddress, IR36021_LOOP1_MANUAL_ID_OFFSET,
298 1, (void *)&vid, sizeof(vid));
299 if (ret) {
300 printf("VID: failed to write VID\n");
301 return -1;
302 }
303 wait = wait_for_new_voltage(vdd, i2caddress);
304 if (wait < 0)
305 return -1;
306 debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
307
308 vdd_last = wait_for_voltage_stable(i2caddress);
309 if (vdd_last < 0)
310 return -1;
311 debug("VID: Current voltage is %d mV\n", vdd_last);
312 return vdd_last;
313}
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530314
315#endif
316
317#ifdef CONFIG_VOL_MONITOR_LTC3882_SET
318/* this function sets the VDD and returns the value set */
319static int set_voltage_to_LTC(int i2caddress, int vdd)
320{
321 int ret, vdd_last, vdd_target = vdd;
Priyanka Jainaf89d242018-10-11 05:11:23 +0000322 int count = 100, temp = 0;
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530323
324 /* Scale up to the LTC resolution is 1/4096V */
325 vdd = (vdd * 4096) / 1000;
326
327 /* 5-byte buffer which needs to be sent following the
328 * PMBus command PAGE_PLUS_WRITE.
329 */
330 u8 buff[5] = {0x04, PWM_CHANNEL0, PMBUS_CMD_VOUT_COMMAND,
331 vdd & 0xFF, (vdd & 0xFF00) >> 8};
332
333 /* Write the desired voltage code to the regulator */
334 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
335 PMBUS_CMD_PAGE_PLUS_WRITE, 1, (void *)&buff, 5);
336 if (ret) {
337 printf("VID: I2C failed to write to the volatge regulator\n");
338 return -1;
339 }
340
341 /* Wait for the volatge to get to the desired value */
342 do {
343 vdd_last = read_voltage_from_LTC(i2caddress);
344 if (vdd_last < 0) {
345 printf("VID: Couldn't read sensor abort VID adjust\n");
346 return -1;
347 }
Priyanka Jainaf89d242018-10-11 05:11:23 +0000348 count--;
349 temp = vdd_last - vdd_target;
350 } while ((abs(temp) > 2) && (count > 0));
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530351
352 return vdd_last;
353}
Ying Zhang8876a512014-10-31 18:06:18 +0800354#endif
355
356static int set_voltage(int i2caddress, int vdd)
357{
358 int vdd_last = -1;
359
360#ifdef CONFIG_VOL_MONITOR_IR36021_SET
361 vdd_last = set_voltage_to_IR(i2caddress, vdd);
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530362#elif defined CONFIG_VOL_MONITOR_LTC3882_SET
363 vdd_last = set_voltage_to_LTC(i2caddress, vdd);
Ying Zhang8876a512014-10-31 18:06:18 +0800364#else
365 #error Specific voltage monitor must be defined
366#endif
367 return vdd_last;
368}
369
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530370#ifdef CONFIG_FSL_LSCH3
371int adjust_vdd(ulong vdd_override)
372{
373 int re_enable = disable_interrupts();
374 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
375 u32 fusesr;
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530376#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
377 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530378 u8 vid, buf;
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530379#else
380 u8 vid;
381#endif
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530382 int vdd_target, vdd_current, vdd_last;
383 int ret, i2caddress;
384 unsigned long vdd_string_override;
385 char *vdd_string;
Priyanka Jainc6a6fac2018-10-11 05:22:34 +0000386#ifdef CONFIG_ARCH_LX2160A
387 static const u16 vdd[32] = {
388 8250,
389 7875,
390 7750,
391 0, /* reserved */
392 0, /* reserved */
393 0, /* reserved */
394 0, /* reserved */
395 0, /* reserved */
396 0, /* reserved */
397 0, /* reserved */
398 0, /* reserved */
399 0, /* reserved */
400 0, /* reserved */
401 0, /* reserved */
402 0, /* reserved */
403 0, /* reserved */
404 8000,
405 8125,
406 8250,
407 0, /* reserved */
408 8500,
409 0, /* reserved */
410 0, /* reserved */
411 0, /* reserved */
412 0, /* reserved */
413 0, /* reserved */
414 0, /* reserved */
415 0, /* reserved */
416 0, /* reserved */
417 0, /* reserved */
418 0, /* reserved */
419 0, /* reserved */
420 };
421#else
Rajesh Bhagatd600b312018-01-17 16:13:01 +0530422#ifdef CONFIG_ARCH_LS1088A
423 static const uint16_t vdd[32] = {
424 10250,
425 9875,
426 9750,
427 0, /* reserved */
428 0, /* reserved */
429 0, /* reserved */
430 0, /* reserved */
431 0, /* reserved */
432 9000,
433 0, /* reserved */
434 0, /* reserved */
435 0, /* reserved */
436 0, /* reserved */
437 0, /* reserved */
438 0, /* reserved */
439 0, /* reserved */
440 10000, /* 1.0000V */
441 10125,
442 10250,
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 0, /* reserved */
453 0, /* reserved */
454 0, /* reserved */
455 0, /* reserved */
456 };
457
458#else
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530459 static const uint16_t vdd[32] = {
460 10500,
461 0, /* reserved */
462 9750,
463 0, /* reserved */
464 9500,
465 0, /* reserved */
466 0, /* reserved */
467 0, /* reserved */
468 0, /* reserved */
469 0, /* reserved */
470 0, /* reserved */
Priyanka Jainfd9b7422018-05-09 12:54:38 +0530471 9000, /* reserved */
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530472 0, /* reserved */
473 0, /* reserved */
474 0, /* reserved */
475 0, /* reserved */
476 10000, /* 1.0000V */
477 0, /* reserved */
478 10250,
479 0, /* reserved */
480 10500,
481 0, /* reserved */
482 0, /* reserved */
483 0, /* reserved */
484 0, /* reserved */
485 0, /* reserved */
486 0, /* reserved */
487 0, /* reserved */
488 0, /* reserved */
489 0, /* reserved */
490 0, /* reserved */
491 0, /* reserved */
492 };
Rajesh Bhagatd600b312018-01-17 16:13:01 +0530493#endif
Priyanka Jainc6a6fac2018-10-11 05:22:34 +0000494#endif
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530495 struct vdd_drive {
496 u8 vid;
497 unsigned voltage;
498 };
499
500 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
501 if (ret) {
502 debug("VID: I2C failed to switch channel\n");
503 ret = -1;
504 goto exit;
505 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530506#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
507 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530508 ret = find_ir_chip_on_i2c();
509 if (ret < 0) {
510 printf("VID: Could not find voltage regulator on I2C.\n");
511 ret = -1;
512 goto exit;
513 } else {
514 i2caddress = ret;
515 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
516 }
517
518 /* check IR chip work on Intel mode*/
519 ret = i2c_read(i2caddress,
520 IR36021_INTEL_MODE_OOFSET,
521 1, (void *)&buf, 1);
522 if (ret) {
523 printf("VID: failed to read IR chip mode.\n");
524 ret = -1;
525 goto exit;
526 }
527 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
528 printf("VID: IR Chip is not used in Intel mode.\n");
529 ret = -1;
530 goto exit;
531 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530532#endif
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530533
534 /* get the voltage ID from fuse status register */
535 fusesr = in_le32(&gur->dcfg_fusesr);
536 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
537 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
538 if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
539 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
540 FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
541 }
542 vdd_target = vdd[vid];
543
544 /* check override variable for overriding VDD */
Simon Glass64b723f2017-08-03 12:22:12 -0600545 vdd_string = env_get(CONFIG_VID_FLS_ENV);
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530546 if (vdd_override == 0 && vdd_string &&
547 !strict_strtoul(vdd_string, 10, &vdd_string_override))
548 vdd_override = vdd_string_override;
549
550 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
551 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
552 debug("VDD override is %lu\n", vdd_override);
553 } else if (vdd_override != 0) {
554 printf("Invalid value.\n");
555 }
556
557 /* divide and round up by 10 to get a value in mV */
558 vdd_target = DIV_ROUND_UP(vdd_target, 10);
559 if (vdd_target == 0) {
560 debug("VID: VID not used\n");
561 ret = 0;
562 goto exit;
563 } else if (vdd_target < VDD_MV_MIN || vdd_target > VDD_MV_MAX) {
564 /* Check vdd_target is in valid range */
565 printf("VID: Target VID %d mV is not in range.\n",
566 vdd_target);
567 ret = -1;
568 goto exit;
569 } else {
570 debug("VID: vid = %d mV\n", vdd_target);
571 }
572
573 /*
574 * Read voltage monitor to check real voltage.
575 */
576 vdd_last = read_voltage(i2caddress);
577 if (vdd_last < 0) {
578 printf("VID: Couldn't read sensor abort VID adjustment\n");
579 ret = -1;
580 goto exit;
581 }
582 vdd_current = vdd_last;
583 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530584
585#ifdef CONFIG_VOL_MONITOR_LTC3882_SET
586 /* Set the target voltage */
587 vdd_last = vdd_current = set_voltage(i2caddress, vdd_target);
588#else
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530589 /*
590 * Adjust voltage to at or one step above target.
591 * As measurements are less precise than setting the values
592 * we may run through dummy steps that cancel each other
593 * when stepping up and then down.
594 */
595 while (vdd_last > 0 &&
596 vdd_last < vdd_target) {
597 vdd_current += IR_VDD_STEP_UP;
598 vdd_last = set_voltage(i2caddress, vdd_current);
599 }
600 while (vdd_last > 0 &&
601 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
602 vdd_current -= IR_VDD_STEP_DOWN;
603 vdd_last = set_voltage(i2caddress, vdd_current);
604 }
605
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530606#endif
Rajesh Bhagatc3a662f2018-01-17 16:13:02 +0530607 if (board_adjust_vdd(vdd_target) < 0) {
608 ret = -1;
609 goto exit;
610 }
611
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530612 if (vdd_last > 0)
613 printf("VID: Core voltage after adjustment is at %d mV\n",
614 vdd_last);
615 else
616 ret = -1;
617exit:
618 if (re_enable)
619 enable_interrupts();
620 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
621 return ret;
622}
623#else /* !CONFIG_FSL_LSCH3 */
Ying Zhang8876a512014-10-31 18:06:18 +0800624int adjust_vdd(ulong vdd_override)
625{
626 int re_enable = disable_interrupts();
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530627#if defined(CONFIG_FSL_LSCH2)
Shaohui Xiedd335672015-11-11 17:58:37 +0800628 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
629#else
Ying Zhang8876a512014-10-31 18:06:18 +0800630 ccsr_gur_t __iomem *gur =
631 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
Shaohui Xiedd335672015-11-11 17:58:37 +0800632#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800633 u32 fusesr;
Ying Zhang7ad5eff2016-01-22 12:15:12 +0800634 u8 vid, buf;
Ying Zhang8876a512014-10-31 18:06:18 +0800635 int vdd_target, vdd_current, vdd_last;
636 int ret, i2caddress;
637 unsigned long vdd_string_override;
638 char *vdd_string;
639 static const uint16_t vdd[32] = {
640 0, /* unused */
641 9875, /* 0.9875V */
642 9750,
643 9625,
644 9500,
645 9375,
646 9250,
647 9125,
648 9000,
649 8875,
650 8750,
651 8625,
652 8500,
653 8375,
654 8250,
655 8125,
656 10000, /* 1.0000V */
657 10125,
658 10250,
659 10375,
660 10500,
661 10625,
662 10750,
663 10875,
664 11000,
665 0, /* reserved */
666 };
667 struct vdd_drive {
668 u8 vid;
669 unsigned voltage;
670 };
671
672 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
673 if (ret) {
674 debug("VID: I2C failed to switch channel\n");
675 ret = -1;
676 goto exit;
677 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530678#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
679 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Ying Zhang8876a512014-10-31 18:06:18 +0800680 ret = find_ir_chip_on_i2c();
681 if (ret < 0) {
682 printf("VID: Could not find voltage regulator on I2C.\n");
683 ret = -1;
684 goto exit;
685 } else {
686 i2caddress = ret;
687 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
688 }
689
Ying Zhang7ad5eff2016-01-22 12:15:12 +0800690 /* check IR chip work on Intel mode*/
691 ret = i2c_read(i2caddress,
692 IR36021_INTEL_MODE_OOFSET,
693 1, (void *)&buf, 1);
694 if (ret) {
695 printf("VID: failed to read IR chip mode.\n");
696 ret = -1;
697 goto exit;
698 }
699 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
700 printf("VID: IR Chip is not used in Intel mode.\n");
701 ret = -1;
702 goto exit;
703 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530704#endif
Ying Zhang7ad5eff2016-01-22 12:15:12 +0800705
Ying Zhang8876a512014-10-31 18:06:18 +0800706 /* get the voltage ID from fuse status register */
707 fusesr = in_be32(&gur->dcfg_fusesr);
708 /*
709 * VID is used according to the table below
710 * ---------------------------------------
711 * | DA_V |
712 * |-------------------------------------|
713 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
714 * ---------------+---------+-----------------+---------|
715 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
716 * | A |----------+---------+-----------------+---------|
717 * | _ | 5b00001 |VID = | VID = |VID = |
718 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
719 * | _ | 5b11110 | | | |
720 * | A |----------+---------+-----------------+---------|
721 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
722 * | T | | | | |
723 * ------------------------------------------------------
724 */
Shaohui Xie085ac1c2016-09-07 17:56:14 +0800725#ifdef CONFIG_FSL_LSCH2
Shaohui Xiedd335672015-11-11 17:58:37 +0800726 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
727 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
728 if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
729 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
730 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
731 }
732#else
Ying Zhang8876a512014-10-31 18:06:18 +0800733 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
734 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
735 if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
736 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
737 FSL_CORENET_DCFG_FUSESR_VID_MASK;
738 }
Shaohui Xiedd335672015-11-11 17:58:37 +0800739#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800740 vdd_target = vdd[vid];
741
742 /* check override variable for overriding VDD */
Simon Glass64b723f2017-08-03 12:22:12 -0600743 vdd_string = env_get(CONFIG_VID_FLS_ENV);
Ying Zhang8876a512014-10-31 18:06:18 +0800744 if (vdd_override == 0 && vdd_string &&
745 !strict_strtoul(vdd_string, 10, &vdd_string_override))
746 vdd_override = vdd_string_override;
747 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
748 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
749 debug("VDD override is %lu\n", vdd_override);
750 } else if (vdd_override != 0) {
751 printf("Invalid value.\n");
752 }
753 if (vdd_target == 0) {
754 debug("VID: VID not used\n");
755 ret = 0;
756 goto exit;
757 } else {
758 /* divide and round up by 10 to get a value in mV */
759 vdd_target = DIV_ROUND_UP(vdd_target, 10);
760 debug("VID: vid = %d mV\n", vdd_target);
761 }
762
763 /*
764 * Read voltage monitor to check real voltage.
765 */
766 vdd_last = read_voltage(i2caddress);
767 if (vdd_last < 0) {
768 printf("VID: Couldn't read sensor abort VID adjustment\n");
769 ret = -1;
770 goto exit;
771 }
772 vdd_current = vdd_last;
773 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
774 /*
775 * Adjust voltage to at or one step above target.
776 * As measurements are less precise than setting the values
777 * we may run through dummy steps that cancel each other
778 * when stepping up and then down.
779 */
780 while (vdd_last > 0 &&
781 vdd_last < vdd_target) {
782 vdd_current += IR_VDD_STEP_UP;
783 vdd_last = set_voltage(i2caddress, vdd_current);
784 }
785 while (vdd_last > 0 &&
786 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
787 vdd_current -= IR_VDD_STEP_DOWN;
788 vdd_last = set_voltage(i2caddress, vdd_current);
789 }
790
791 if (vdd_last > 0)
792 printf("VID: Core voltage after adjustment is at %d mV\n",
793 vdd_last);
794 else
795 ret = -1;
796exit:
797 if (re_enable)
798 enable_interrupts();
Wenbin Song44ad75c2016-03-09 13:38:23 +0800799
800 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
801
Ying Zhang8876a512014-10-31 18:06:18 +0800802 return ret;
803}
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530804#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800805
806static int print_vdd(void)
807{
808 int vdd_last, ret, i2caddress;
809
810 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
811 if (ret) {
812 debug("VID : I2c failed to switch channel\n");
813 return -1;
814 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530815#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
816 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Ying Zhang8876a512014-10-31 18:06:18 +0800817 ret = find_ir_chip_on_i2c();
818 if (ret < 0) {
819 printf("VID: Could not find voltage regulator on I2C.\n");
Wenbin Song44ad75c2016-03-09 13:38:23 +0800820 goto exit;
Ying Zhang8876a512014-10-31 18:06:18 +0800821 } else {
822 i2caddress = ret;
823 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
824 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530825#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800826
827 /*
828 * Read voltage monitor to check real voltage.
829 */
830 vdd_last = read_voltage(i2caddress);
831 if (vdd_last < 0) {
832 printf("VID: Couldn't read sensor abort VID adjustment\n");
Wenbin Song44ad75c2016-03-09 13:38:23 +0800833 goto exit;
Ying Zhang8876a512014-10-31 18:06:18 +0800834 }
835 printf("VID: Core voltage is at %d mV\n", vdd_last);
Wenbin Song44ad75c2016-03-09 13:38:23 +0800836exit:
837 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
838
839 return ret < 0 ? -1 : 0;
Ying Zhang8876a512014-10-31 18:06:18 +0800840
Ying Zhang8876a512014-10-31 18:06:18 +0800841}
842
843static int do_vdd_override(cmd_tbl_t *cmdtp,
844 int flag, int argc,
845 char * const argv[])
846{
847 ulong override;
848
849 if (argc < 2)
850 return CMD_RET_USAGE;
851
852 if (!strict_strtoul(argv[1], 10, &override))
853 adjust_vdd(override); /* the value is checked by callee */
854 else
855 return CMD_RET_USAGE;
856 return 0;
857}
858
859static int do_vdd_read(cmd_tbl_t *cmdtp,
860 int flag, int argc,
861 char * const argv[])
862{
863 if (argc < 1)
864 return CMD_RET_USAGE;
865 print_vdd();
866
867 return 0;
868}
869
870U_BOOT_CMD(
871 vdd_override, 2, 0, do_vdd_override,
872 "override VDD",
873 " - override with the voltage specified in mV, eg. 1050"
874);
875
876U_BOOT_CMD(
877 vdd_read, 1, 0, do_vdd_read,
878 "read VDD",
879 " - Read the voltage specified in mV"
880)