2 * Copyright 2014 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/arch/immap_lsch2.h>
14 #include <asm/immap_85xx.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 int __weak i2c_multiplexer_select_vid_channel(u8 channel)
26 * Compensate for a board specific voltage drop between regulator and SoC
27 * return a value in mV
29 int __weak board_vdd_drop_compensation(void)
35 * Get the i2c address configuration for the IR regulator chip
37 * There are some variance in the RDB HW regarding the I2C address configuration
38 * for the IR regulator chip, which is likely a problem of external resistor
39 * accuracy. So we just check each address in a hopefully non-intrusive mode
40 * and use the first one that seems to work
42 * The IR chip can show up under the following addresses:
43 * 0x08 (Verified on T1040RDB-PA,T4240RDB-PB,X-T4240RDB-16GPA)
44 * 0x09 (Verified on T1040RDB-PA)
45 * 0x38 (Verified on T2080QDS, T2081QDS)
47 static int find_ir_chip_on_i2c(void)
53 const int ir_i2c_addr[] = {0x38, 0x08, 0x09};
55 /* Check all the address */
56 for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) {
57 i2caddress = ir_i2c_addr[i];
58 ret = i2c_read(i2caddress,
59 IR36021_MFR_ID_OFFSET, 1, (void *)&byte,
61 if ((ret >= 0) && (byte == IR36021_MFR_ID))
67 /* Maximum loop count waiting for new voltage to take effect */
68 #define MAX_LOOP_WAIT_NEW_VOL 100
69 /* Maximum loop count waiting for the voltage to be stable */
70 #define MAX_LOOP_WAIT_VOL_STABLE 100
72 * read_voltage from sensor on I2C bus
73 * We use average of 4 readings, waiting for WAIT_FOR_ADC before
76 #define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
78 /* If an INA220 chip is available, we can use it to read back the voltage
79 * as it may have a higher accuracy than the IR chip for the same purpose
81 #ifdef CONFIG_VOL_MONITOR_INA220
82 #define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
83 #define ADC_MIN_ACCURACY 4
85 #define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
86 #define ADC_MIN_ACCURACY 4
89 #ifdef CONFIG_VOL_MONITOR_INA220
90 static int read_voltage_from_INA220(int i2caddress)
92 int i, ret, voltage_read = 0;
96 for (i = 0; i < NUM_READINGS; i++) {
97 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
98 I2C_VOL_MONITOR_BUS_V_OFFSET, 1,
101 printf("VID: failed to read core voltage\n");
104 vol_mon = (buf[0] << 8) | buf[1];
105 if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
106 printf("VID: Core voltage sensor error\n");
109 debug("VID: bus voltage reads 0x%04x\n", vol_mon);
111 voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
112 udelay(WAIT_FOR_ADC);
114 /* calculate the average */
115 voltage_read /= NUM_READINGS;
121 /* read voltage from IR */
122 #ifdef CONFIG_VOL_MONITOR_IR36021_READ
123 static int read_voltage_from_IR(int i2caddress)
125 int i, ret, voltage_read = 0;
129 for (i = 0; i < NUM_READINGS; i++) {
130 ret = i2c_read(i2caddress,
131 IR36021_LOOP1_VOUT_OFFSET,
134 printf("VID: failed to read vcpu\n");
139 printf("VID: Core voltage sensor error\n");
142 debug("VID: bus voltage reads 0x%02x\n", vol_mon);
143 /* Resolution is 1/128V. We scale up here to get 1/128mV
144 * and divide at the end
146 voltage_read += vol_mon * 1000;
147 udelay(WAIT_FOR_ADC);
149 /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
150 voltage_read = DIV_ROUND_UP(voltage_read, 128);
152 /* calculate the average */
153 voltage_read /= NUM_READINGS;
155 /* Compensate for a board specific voltage drop between regulator and
156 * SoC before converting into an IR VID value
158 voltage_read -= board_vdd_drop_compensation();
164 static int read_voltage(int i2caddress)
167 #ifdef CONFIG_VOL_MONITOR_INA220
168 voltage_read = read_voltage_from_INA220(i2caddress);
169 #elif defined CONFIG_VOL_MONITOR_IR36021_READ
170 voltage_read = read_voltage_from_IR(i2caddress);
178 * We need to calculate how long before the voltage stops to drop
179 * or increase. It returns with the loop count. Each loop takes
180 * several readings (WAIT_FOR_ADC)
182 static int wait_for_new_voltage(int vdd, int i2caddress)
184 int timeout, vdd_current;
186 vdd_current = read_voltage(i2caddress);
187 /* wait until voltage starts to reach the target. Voltage slew
188 * rates by typical regulators will always lead to stable readings
189 * within each fairly long ADC interval in comparison to the
190 * intended voltage delta change until the target voltage is
191 * reached. The fairly small voltage delta change to any target
192 * VID voltage also means that this function will always complete
193 * within few iterations. If the timeout was ever reached, it would
194 * point to a serious failure in the regulator system.
197 abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
198 timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
199 vdd_current = read_voltage(i2caddress);
201 if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
202 printf("VID: Voltage adjustment timeout\n");
209 * this function keeps reading the voltage until it is stable or until the
212 static int wait_for_voltage_stable(int i2caddress)
214 int timeout, vdd_current, vdd;
216 vdd = read_voltage(i2caddress);
217 udelay(NUM_READINGS * WAIT_FOR_ADC);
219 /* wait until voltage is stable */
220 vdd_current = read_voltage(i2caddress);
221 /* The maximum timeout is
222 * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
224 for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
225 abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
226 timeout > 0; timeout--) {
228 udelay(NUM_READINGS * WAIT_FOR_ADC);
229 vdd_current = read_voltage(i2caddress);
236 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
237 /* Set the voltage to the IR chip */
238 static int set_voltage_to_IR(int i2caddress, int vdd)
244 /* Compensate for a board specific voltage drop between regulator and
245 * SoC before converting into an IR VID value
247 vdd += board_vdd_drop_compensation();
248 #ifdef CONFIG_LS1043A
249 vid = DIV_ROUND_UP(vdd - 265, 5);
251 vid = DIV_ROUND_UP(vdd - 245, 5);
254 ret = i2c_write(i2caddress, IR36021_LOOP1_MANUAL_ID_OFFSET,
255 1, (void *)&vid, sizeof(vid));
257 printf("VID: failed to write VID\n");
260 wait = wait_for_new_voltage(vdd, i2caddress);
263 debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
265 vdd_last = wait_for_voltage_stable(i2caddress);
268 debug("VID: Current voltage is %d mV\n", vdd_last);
273 static int set_voltage(int i2caddress, int vdd)
277 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
278 vdd_last = set_voltage_to_IR(i2caddress, vdd);
280 #error Specific voltage monitor must be defined
285 int adjust_vdd(ulong vdd_override)
287 int re_enable = disable_interrupts();
288 #ifdef CONFIG_LS1043A
289 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
291 ccsr_gur_t __iomem *gur =
292 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
296 int vdd_target, vdd_current, vdd_last;
298 unsigned long vdd_string_override;
300 static const uint16_t vdd[32] = {
333 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
335 debug("VID: I2C failed to switch channel\n");
339 ret = find_ir_chip_on_i2c();
341 printf("VID: Could not find voltage regulator on I2C.\n");
346 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
349 /* get the voltage ID from fuse status register */
350 fusesr = in_be32(&gur->dcfg_fusesr);
352 * VID is used according to the table below
353 * ---------------------------------------
355 * |-------------------------------------|
356 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
357 * ---------------+---------+-----------------+---------|
358 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
359 * | A |----------+---------+-----------------+---------|
360 * | _ | 5b00001 |VID = | VID = |VID = |
361 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
362 * | _ | 5b11110 | | | |
363 * | A |----------+---------+-----------------+---------|
364 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
366 * ------------------------------------------------------
368 #ifdef CONFIG_LS1043A
369 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
370 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
371 if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
372 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
373 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
376 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
377 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
378 if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
379 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
380 FSL_CORENET_DCFG_FUSESR_VID_MASK;
383 vdd_target = vdd[vid];
385 /* check override variable for overriding VDD */
386 vdd_string = getenv(CONFIG_VID_FLS_ENV);
387 if (vdd_override == 0 && vdd_string &&
388 !strict_strtoul(vdd_string, 10, &vdd_string_override))
389 vdd_override = vdd_string_override;
390 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
391 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
392 debug("VDD override is %lu\n", vdd_override);
393 } else if (vdd_override != 0) {
394 printf("Invalid value.\n");
396 if (vdd_target == 0) {
397 debug("VID: VID not used\n");
401 /* divide and round up by 10 to get a value in mV */
402 vdd_target = DIV_ROUND_UP(vdd_target, 10);
403 debug("VID: vid = %d mV\n", vdd_target);
407 * Read voltage monitor to check real voltage.
409 vdd_last = read_voltage(i2caddress);
411 printf("VID: Couldn't read sensor abort VID adjustment\n");
415 vdd_current = vdd_last;
416 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
418 * Adjust voltage to at or one step above target.
419 * As measurements are less precise than setting the values
420 * we may run through dummy steps that cancel each other
421 * when stepping up and then down.
423 while (vdd_last > 0 &&
424 vdd_last < vdd_target) {
425 vdd_current += IR_VDD_STEP_UP;
426 vdd_last = set_voltage(i2caddress, vdd_current);
428 while (vdd_last > 0 &&
429 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
430 vdd_current -= IR_VDD_STEP_DOWN;
431 vdd_last = set_voltage(i2caddress, vdd_current);
435 printf("VID: Core voltage after adjustment is at %d mV\n",
445 static int print_vdd(void)
447 int vdd_last, ret, i2caddress;
449 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
451 debug("VID : I2c failed to switch channel\n");
454 ret = find_ir_chip_on_i2c();
456 printf("VID: Could not find voltage regulator on I2C.\n");
460 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
464 * Read voltage monitor to check real voltage.
466 vdd_last = read_voltage(i2caddress);
468 printf("VID: Couldn't read sensor abort VID adjustment\n");
471 printf("VID: Core voltage is at %d mV\n", vdd_last);
476 static int do_vdd_override(cmd_tbl_t *cmdtp,
483 return CMD_RET_USAGE;
485 if (!strict_strtoul(argv[1], 10, &override))
486 adjust_vdd(override); /* the value is checked by callee */
488 return CMD_RET_USAGE;
492 static int do_vdd_read(cmd_tbl_t *cmdtp,
497 return CMD_RET_USAGE;
504 vdd_override, 2, 0, do_vdd_override,
506 " - override with the voltage specified in mV, eg. 1050"
510 vdd_read, 1, 0, do_vdd_read,
512 " - Read the voltage specified in mV"