blob: eb25f5e23f810236564669847f8fac9072f4a138 [file] [log] [blame]
Ying Zhang8876a512014-10-31 18:06:18 +08001/*
2 * Copyright 2014 Freescale Semiconductor, Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <command.h>
9#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;
322
323 /* Scale up to the LTC resolution is 1/4096V */
324 vdd = (vdd * 4096) / 1000;
325
326 /* 5-byte buffer which needs to be sent following the
327 * PMBus command PAGE_PLUS_WRITE.
328 */
329 u8 buff[5] = {0x04, PWM_CHANNEL0, PMBUS_CMD_VOUT_COMMAND,
330 vdd & 0xFF, (vdd & 0xFF00) >> 8};
331
332 /* Write the desired voltage code to the regulator */
333 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
334 PMBUS_CMD_PAGE_PLUS_WRITE, 1, (void *)&buff, 5);
335 if (ret) {
336 printf("VID: I2C failed to write to the volatge regulator\n");
337 return -1;
338 }
339
340 /* Wait for the volatge to get to the desired value */
341 do {
342 vdd_last = read_voltage_from_LTC(i2caddress);
343 if (vdd_last < 0) {
344 printf("VID: Couldn't read sensor abort VID adjust\n");
345 return -1;
346 }
347 } while (vdd_last != vdd_target);
348
349 return vdd_last;
350}
Ying Zhang8876a512014-10-31 18:06:18 +0800351#endif
352
353static int set_voltage(int i2caddress, int vdd)
354{
355 int vdd_last = -1;
356
357#ifdef CONFIG_VOL_MONITOR_IR36021_SET
358 vdd_last = set_voltage_to_IR(i2caddress, vdd);
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530359#elif defined CONFIG_VOL_MONITOR_LTC3882_SET
360 vdd_last = set_voltage_to_LTC(i2caddress, vdd);
Ying Zhang8876a512014-10-31 18:06:18 +0800361#else
362 #error Specific voltage monitor must be defined
363#endif
364 return vdd_last;
365}
366
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530367#ifdef CONFIG_FSL_LSCH3
368int adjust_vdd(ulong vdd_override)
369{
370 int re_enable = disable_interrupts();
371 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
372 u32 fusesr;
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530373#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
374 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530375 u8 vid, buf;
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530376#else
377 u8 vid;
378#endif
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530379 int vdd_target, vdd_current, vdd_last;
380 int ret, i2caddress;
381 unsigned long vdd_string_override;
382 char *vdd_string;
Rajesh Bhagatd600b312018-01-17 16:13:01 +0530383#ifdef CONFIG_ARCH_LS1088A
384 static const uint16_t vdd[32] = {
385 10250,
386 9875,
387 9750,
388 0, /* reserved */
389 0, /* reserved */
390 0, /* reserved */
391 0, /* reserved */
392 0, /* reserved */
393 9000,
394 0, /* reserved */
395 0, /* reserved */
396 0, /* reserved */
397 0, /* reserved */
398 0, /* reserved */
399 0, /* reserved */
400 0, /* reserved */
401 10000, /* 1.0000V */
402 10125,
403 10250,
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 0, /* reserved */
417 };
418
419#else
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530420 static const uint16_t vdd[32] = {
421 10500,
422 0, /* reserved */
423 9750,
424 0, /* reserved */
425 9500,
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 0, /* reserved */
437 10000, /* 1.0000V */
438 0, /* reserved */
439 10250,
440 0, /* reserved */
441 10500,
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 0, /* reserved */
453 };
Rajesh Bhagatd600b312018-01-17 16:13:01 +0530454#endif
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530455 struct vdd_drive {
456 u8 vid;
457 unsigned voltage;
458 };
459
460 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
461 if (ret) {
462 debug("VID: I2C failed to switch channel\n");
463 ret = -1;
464 goto exit;
465 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530466#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
467 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530468 ret = find_ir_chip_on_i2c();
469 if (ret < 0) {
470 printf("VID: Could not find voltage regulator on I2C.\n");
471 ret = -1;
472 goto exit;
473 } else {
474 i2caddress = ret;
475 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
476 }
477
478 /* check IR chip work on Intel mode*/
479 ret = i2c_read(i2caddress,
480 IR36021_INTEL_MODE_OOFSET,
481 1, (void *)&buf, 1);
482 if (ret) {
483 printf("VID: failed to read IR chip mode.\n");
484 ret = -1;
485 goto exit;
486 }
487 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
488 printf("VID: IR Chip is not used in Intel mode.\n");
489 ret = -1;
490 goto exit;
491 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530492#endif
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530493
494 /* get the voltage ID from fuse status register */
495 fusesr = in_le32(&gur->dcfg_fusesr);
496 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
497 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
498 if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
499 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
500 FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
501 }
502 vdd_target = vdd[vid];
503
504 /* check override variable for overriding VDD */
Simon Glass64b723f2017-08-03 12:22:12 -0600505 vdd_string = env_get(CONFIG_VID_FLS_ENV);
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530506 if (vdd_override == 0 && vdd_string &&
507 !strict_strtoul(vdd_string, 10, &vdd_string_override))
508 vdd_override = vdd_string_override;
509
510 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
511 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
512 debug("VDD override is %lu\n", vdd_override);
513 } else if (vdd_override != 0) {
514 printf("Invalid value.\n");
515 }
516
517 /* divide and round up by 10 to get a value in mV */
518 vdd_target = DIV_ROUND_UP(vdd_target, 10);
519 if (vdd_target == 0) {
520 debug("VID: VID not used\n");
521 ret = 0;
522 goto exit;
523 } else if (vdd_target < VDD_MV_MIN || vdd_target > VDD_MV_MAX) {
524 /* Check vdd_target is in valid range */
525 printf("VID: Target VID %d mV is not in range.\n",
526 vdd_target);
527 ret = -1;
528 goto exit;
529 } else {
530 debug("VID: vid = %d mV\n", vdd_target);
531 }
532
533 /*
534 * Read voltage monitor to check real voltage.
535 */
536 vdd_last = read_voltage(i2caddress);
537 if (vdd_last < 0) {
538 printf("VID: Couldn't read sensor abort VID adjustment\n");
539 ret = -1;
540 goto exit;
541 }
542 vdd_current = vdd_last;
543 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530544
545#ifdef CONFIG_VOL_MONITOR_LTC3882_SET
546 /* Set the target voltage */
547 vdd_last = vdd_current = set_voltage(i2caddress, vdd_target);
548#else
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530549 /*
550 * Adjust voltage to at or one step above target.
551 * As measurements are less precise than setting the values
552 * we may run through dummy steps that cancel each other
553 * when stepping up and then down.
554 */
555 while (vdd_last > 0 &&
556 vdd_last < vdd_target) {
557 vdd_current += IR_VDD_STEP_UP;
558 vdd_last = set_voltage(i2caddress, vdd_current);
559 }
560 while (vdd_last > 0 &&
561 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
562 vdd_current -= IR_VDD_STEP_DOWN;
563 vdd_last = set_voltage(i2caddress, vdd_current);
564 }
565
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530566#endif
Rajesh Bhagatc3a662f2018-01-17 16:13:02 +0530567 if (board_adjust_vdd(vdd_target) < 0) {
568 ret = -1;
569 goto exit;
570 }
571
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530572 if (vdd_last > 0)
573 printf("VID: Core voltage after adjustment is at %d mV\n",
574 vdd_last);
575 else
576 ret = -1;
577exit:
578 if (re_enable)
579 enable_interrupts();
580 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
581 return ret;
582}
583#else /* !CONFIG_FSL_LSCH3 */
Ying Zhang8876a512014-10-31 18:06:18 +0800584int adjust_vdd(ulong vdd_override)
585{
586 int re_enable = disable_interrupts();
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530587#if defined(CONFIG_FSL_LSCH2)
Shaohui Xiedd335672015-11-11 17:58:37 +0800588 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
589#else
Ying Zhang8876a512014-10-31 18:06:18 +0800590 ccsr_gur_t __iomem *gur =
591 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
Shaohui Xiedd335672015-11-11 17:58:37 +0800592#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800593 u32 fusesr;
Ying Zhang7ad5eff2016-01-22 12:15:12 +0800594 u8 vid, buf;
Ying Zhang8876a512014-10-31 18:06:18 +0800595 int vdd_target, vdd_current, vdd_last;
596 int ret, i2caddress;
597 unsigned long vdd_string_override;
598 char *vdd_string;
599 static const uint16_t vdd[32] = {
600 0, /* unused */
601 9875, /* 0.9875V */
602 9750,
603 9625,
604 9500,
605 9375,
606 9250,
607 9125,
608 9000,
609 8875,
610 8750,
611 8625,
612 8500,
613 8375,
614 8250,
615 8125,
616 10000, /* 1.0000V */
617 10125,
618 10250,
619 10375,
620 10500,
621 10625,
622 10750,
623 10875,
624 11000,
625 0, /* reserved */
626 };
627 struct vdd_drive {
628 u8 vid;
629 unsigned voltage;
630 };
631
632 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
633 if (ret) {
634 debug("VID: I2C failed to switch channel\n");
635 ret = -1;
636 goto exit;
637 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530638#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
639 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Ying Zhang8876a512014-10-31 18:06:18 +0800640 ret = find_ir_chip_on_i2c();
641 if (ret < 0) {
642 printf("VID: Could not find voltage regulator on I2C.\n");
643 ret = -1;
644 goto exit;
645 } else {
646 i2caddress = ret;
647 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
648 }
649
Ying Zhang7ad5eff2016-01-22 12:15:12 +0800650 /* check IR chip work on Intel mode*/
651 ret = i2c_read(i2caddress,
652 IR36021_INTEL_MODE_OOFSET,
653 1, (void *)&buf, 1);
654 if (ret) {
655 printf("VID: failed to read IR chip mode.\n");
656 ret = -1;
657 goto exit;
658 }
659 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
660 printf("VID: IR Chip is not used in Intel mode.\n");
661 ret = -1;
662 goto exit;
663 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530664#endif
Ying Zhang7ad5eff2016-01-22 12:15:12 +0800665
Ying Zhang8876a512014-10-31 18:06:18 +0800666 /* get the voltage ID from fuse status register */
667 fusesr = in_be32(&gur->dcfg_fusesr);
668 /*
669 * VID is used according to the table below
670 * ---------------------------------------
671 * | DA_V |
672 * |-------------------------------------|
673 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
674 * ---------------+---------+-----------------+---------|
675 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
676 * | A |----------+---------+-----------------+---------|
677 * | _ | 5b00001 |VID = | VID = |VID = |
678 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
679 * | _ | 5b11110 | | | |
680 * | A |----------+---------+-----------------+---------|
681 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
682 * | T | | | | |
683 * ------------------------------------------------------
684 */
Shaohui Xie085ac1c2016-09-07 17:56:14 +0800685#ifdef CONFIG_FSL_LSCH2
Shaohui Xiedd335672015-11-11 17:58:37 +0800686 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
687 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
688 if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
689 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
690 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
691 }
692#else
Ying Zhang8876a512014-10-31 18:06:18 +0800693 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
694 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
695 if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
696 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
697 FSL_CORENET_DCFG_FUSESR_VID_MASK;
698 }
Shaohui Xiedd335672015-11-11 17:58:37 +0800699#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800700 vdd_target = vdd[vid];
701
702 /* check override variable for overriding VDD */
Simon Glass64b723f2017-08-03 12:22:12 -0600703 vdd_string = env_get(CONFIG_VID_FLS_ENV);
Ying Zhang8876a512014-10-31 18:06:18 +0800704 if (vdd_override == 0 && vdd_string &&
705 !strict_strtoul(vdd_string, 10, &vdd_string_override))
706 vdd_override = vdd_string_override;
707 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
708 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
709 debug("VDD override is %lu\n", vdd_override);
710 } else if (vdd_override != 0) {
711 printf("Invalid value.\n");
712 }
713 if (vdd_target == 0) {
714 debug("VID: VID not used\n");
715 ret = 0;
716 goto exit;
717 } else {
718 /* divide and round up by 10 to get a value in mV */
719 vdd_target = DIV_ROUND_UP(vdd_target, 10);
720 debug("VID: vid = %d mV\n", vdd_target);
721 }
722
723 /*
724 * Read voltage monitor to check real voltage.
725 */
726 vdd_last = read_voltage(i2caddress);
727 if (vdd_last < 0) {
728 printf("VID: Couldn't read sensor abort VID adjustment\n");
729 ret = -1;
730 goto exit;
731 }
732 vdd_current = vdd_last;
733 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
734 /*
735 * Adjust voltage to at or one step above target.
736 * As measurements are less precise than setting the values
737 * we may run through dummy steps that cancel each other
738 * when stepping up and then down.
739 */
740 while (vdd_last > 0 &&
741 vdd_last < vdd_target) {
742 vdd_current += IR_VDD_STEP_UP;
743 vdd_last = set_voltage(i2caddress, vdd_current);
744 }
745 while (vdd_last > 0 &&
746 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
747 vdd_current -= IR_VDD_STEP_DOWN;
748 vdd_last = set_voltage(i2caddress, vdd_current);
749 }
750
751 if (vdd_last > 0)
752 printf("VID: Core voltage after adjustment is at %d mV\n",
753 vdd_last);
754 else
755 ret = -1;
756exit:
757 if (re_enable)
758 enable_interrupts();
Wenbin Song44ad75c2016-03-09 13:38:23 +0800759
760 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
761
Ying Zhang8876a512014-10-31 18:06:18 +0800762 return ret;
763}
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530764#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800765
766static int print_vdd(void)
767{
768 int vdd_last, ret, i2caddress;
769
770 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
771 if (ret) {
772 debug("VID : I2c failed to switch channel\n");
773 return -1;
774 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530775#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
776 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Ying Zhang8876a512014-10-31 18:06:18 +0800777 ret = find_ir_chip_on_i2c();
778 if (ret < 0) {
779 printf("VID: Could not find voltage regulator on I2C.\n");
Wenbin Song44ad75c2016-03-09 13:38:23 +0800780 goto exit;
Ying Zhang8876a512014-10-31 18:06:18 +0800781 } else {
782 i2caddress = ret;
783 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
784 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530785#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800786
787 /*
788 * Read voltage monitor to check real voltage.
789 */
790 vdd_last = read_voltage(i2caddress);
791 if (vdd_last < 0) {
792 printf("VID: Couldn't read sensor abort VID adjustment\n");
Wenbin Song44ad75c2016-03-09 13:38:23 +0800793 goto exit;
Ying Zhang8876a512014-10-31 18:06:18 +0800794 }
795 printf("VID: Core voltage is at %d mV\n", vdd_last);
Wenbin Song44ad75c2016-03-09 13:38:23 +0800796exit:
797 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
798
799 return ret < 0 ? -1 : 0;
Ying Zhang8876a512014-10-31 18:06:18 +0800800
Ying Zhang8876a512014-10-31 18:06:18 +0800801}
802
803static int do_vdd_override(cmd_tbl_t *cmdtp,
804 int flag, int argc,
805 char * const argv[])
806{
807 ulong override;
808
809 if (argc < 2)
810 return CMD_RET_USAGE;
811
812 if (!strict_strtoul(argv[1], 10, &override))
813 adjust_vdd(override); /* the value is checked by callee */
814 else
815 return CMD_RET_USAGE;
816 return 0;
817}
818
819static int do_vdd_read(cmd_tbl_t *cmdtp,
820 int flag, int argc,
821 char * const argv[])
822{
823 if (argc < 1)
824 return CMD_RET_USAGE;
825 print_vdd();
826
827 return 0;
828}
829
830U_BOOT_CMD(
831 vdd_override, 2, 0, do_vdd_override,
832 "override VDD",
833 " - override with the voltage specified in mV, eg. 1050"
834);
835
836U_BOOT_CMD(
837 vdd_read, 1, 0, do_vdd_read,
838 "read VDD",
839 " - Read the voltage specified in mV"
840)