2 * Copyright 2014 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/immap_85xx.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 int __weak i2c_multiplexer_select_vid_channel(u8 channel)
21 * Compensate for a board specific voltage drop between regulator and SoC
22 * return a value in mV
24 int __weak board_vdd_drop_compensation(void)
30 * Get the i2c address configuration for the IR regulator chip
32 * There are some variance in the RDB HW regarding the I2C address configuration
33 * for the IR regulator chip, which is likely a problem of external resistor
34 * accuracy. So we just check each address in a hopefully non-intrusive mode
35 * and use the first one that seems to work
37 * The IR chip can show up under the following addresses:
38 * 0x08 (Verified on T1040RDB-PA,T4240RDB-PB,X-T4240RDB-16GPA)
39 * 0x09 (Verified on T1040RDB-PA)
40 * 0x38 (Verified on T2080QDS, T2081QDS)
42 static int find_ir_chip_on_i2c(void)
48 const int ir_i2c_addr[] = {0x38, 0x08, 0x09};
50 /* Check all the address */
51 for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) {
52 i2caddress = ir_i2c_addr[i];
53 ret = i2c_read(i2caddress,
54 IR36021_MFR_ID_OFFSET, 1, (void *)&byte,
56 if ((ret >= 0) && (byte == IR36021_MFR_ID))
62 /* Maximum loop count waiting for new voltage to take effect */
63 #define MAX_LOOP_WAIT_NEW_VOL 100
64 /* Maximum loop count waiting for the voltage to be stable */
65 #define MAX_LOOP_WAIT_VOL_STABLE 100
67 * read_voltage from sensor on I2C bus
68 * We use average of 4 readings, waiting for WAIT_FOR_ADC before
71 #define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
73 /* If an INA220 chip is available, we can use it to read back the voltage
74 * as it may have a higher accuracy than the IR chip for the same purpose
76 #ifdef CONFIG_VOL_MONITOR_INA220
77 #define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
78 #define ADC_MIN_ACCURACY 4
80 #define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
81 #define ADC_MIN_ACCURACY 4
84 #ifdef CONFIG_VOL_MONITOR_INA220
85 static int read_voltage_from_INA220(int i2caddress)
87 int i, ret, voltage_read = 0;
91 for (i = 0; i < NUM_READINGS; i++) {
92 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
93 I2C_VOL_MONITOR_BUS_V_OFFSET, 1,
96 printf("VID: failed to read core voltage\n");
99 vol_mon = (buf[0] << 8) | buf[1];
100 if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
101 printf("VID: Core voltage sensor error\n");
104 debug("VID: bus voltage reads 0x%04x\n", vol_mon);
106 voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
107 udelay(WAIT_FOR_ADC);
109 /* calculate the average */
110 voltage_read /= NUM_READINGS;
116 /* read voltage from IR */
117 #ifdef CONFIG_VOL_MONITOR_IR36021_READ
118 static int read_voltage_from_IR(int i2caddress)
120 int i, ret, voltage_read = 0;
124 for (i = 0; i < NUM_READINGS; i++) {
125 ret = i2c_read(i2caddress,
126 IR36021_LOOP1_VOUT_OFFSET,
129 printf("VID: failed to read vcpu\n");
134 printf("VID: Core voltage sensor error\n");
137 debug("VID: bus voltage reads 0x%02x\n", vol_mon);
138 /* Resolution is 1/128V. We scale up here to get 1/128mV
139 * and divide at the end
141 voltage_read += vol_mon * 1000;
142 udelay(WAIT_FOR_ADC);
144 /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
145 voltage_read = DIV_ROUND_UP(voltage_read, 128);
147 /* calculate the average */
148 voltage_read /= NUM_READINGS;
150 /* Compensate for a board specific voltage drop between regulator and
151 * SoC before converting into an IR VID value
153 voltage_read -= board_vdd_drop_compensation();
159 static int read_voltage(int i2caddress)
162 #ifdef CONFIG_VOL_MONITOR_INA220
163 voltage_read = read_voltage_from_INA220(i2caddress);
164 #elif defined CONFIG_VOL_MONITOR_IR36021_READ
165 voltage_read = read_voltage_from_IR(i2caddress);
173 * We need to calculate how long before the voltage stops to drop
174 * or increase. It returns with the loop count. Each loop takes
175 * several readings (WAIT_FOR_ADC)
177 static int wait_for_new_voltage(int vdd, int i2caddress)
179 int timeout, vdd_current;
181 vdd_current = read_voltage(i2caddress);
182 /* wait until voltage starts to reach the target. Voltage slew
183 * rates by typical regulators will always lead to stable readings
184 * within each fairly long ADC interval in comparison to the
185 * intended voltage delta change until the target voltage is
186 * reached. The fairly small voltage delta change to any target
187 * VID voltage also means that this function will always complete
188 * within few iterations. If the timeout was ever reached, it would
189 * point to a serious failure in the regulator system.
192 abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
193 timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
194 vdd_current = read_voltage(i2caddress);
196 if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
197 printf("VID: Voltage adjustment timeout\n");
204 * this function keeps reading the voltage until it is stable or until the
207 static int wait_for_voltage_stable(int i2caddress)
209 int timeout, vdd_current, vdd;
211 vdd = read_voltage(i2caddress);
212 udelay(NUM_READINGS * WAIT_FOR_ADC);
214 /* wait until voltage is stable */
215 vdd_current = read_voltage(i2caddress);
216 /* The maximum timeout is
217 * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
219 for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
220 abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
221 timeout > 0; timeout--) {
223 udelay(NUM_READINGS * WAIT_FOR_ADC);
224 vdd_current = read_voltage(i2caddress);
231 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
232 /* Set the voltage to the IR chip */
233 static int set_voltage_to_IR(int i2caddress, int vdd)
239 /* Compensate for a board specific voltage drop between regulator and
240 * SoC before converting into an IR VID value
242 vdd += board_vdd_drop_compensation();
243 vid = DIV_ROUND_UP(vdd - 245, 5);
245 ret = i2c_write(i2caddress, IR36021_LOOP1_MANUAL_ID_OFFSET,
246 1, (void *)&vid, sizeof(vid));
248 printf("VID: failed to write VID\n");
251 wait = wait_for_new_voltage(vdd, i2caddress);
254 debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
256 vdd_last = wait_for_voltage_stable(i2caddress);
259 debug("VID: Current voltage is %d mV\n", vdd_last);
264 static int set_voltage(int i2caddress, int vdd)
268 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
269 vdd_last = set_voltage_to_IR(i2caddress, vdd);
271 #error Specific voltage monitor must be defined
276 int adjust_vdd(ulong vdd_override)
278 int re_enable = disable_interrupts();
279 ccsr_gur_t __iomem *gur =
280 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
283 int vdd_target, vdd_current, vdd_last;
285 unsigned long vdd_string_override;
287 static const uint16_t vdd[32] = {
320 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
322 debug("VID: I2C failed to switch channel\n");
326 ret = find_ir_chip_on_i2c();
328 printf("VID: Could not find voltage regulator on I2C.\n");
333 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
336 /* get the voltage ID from fuse status register */
337 fusesr = in_be32(&gur->dcfg_fusesr);
339 * VID is used according to the table below
340 * ---------------------------------------
342 * |-------------------------------------|
343 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
344 * ---------------+---------+-----------------+---------|
345 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
346 * | A |----------+---------+-----------------+---------|
347 * | _ | 5b00001 |VID = | VID = |VID = |
348 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
349 * | _ | 5b11110 | | | |
350 * | A |----------+---------+-----------------+---------|
351 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
353 * ------------------------------------------------------
355 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
356 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
357 if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
358 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
359 FSL_CORENET_DCFG_FUSESR_VID_MASK;
361 vdd_target = vdd[vid];
363 /* check override variable for overriding VDD */
364 vdd_string = getenv(CONFIG_VID_FLS_ENV);
365 if (vdd_override == 0 && vdd_string &&
366 !strict_strtoul(vdd_string, 10, &vdd_string_override))
367 vdd_override = vdd_string_override;
368 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
369 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
370 debug("VDD override is %lu\n", vdd_override);
371 } else if (vdd_override != 0) {
372 printf("Invalid value.\n");
374 if (vdd_target == 0) {
375 debug("VID: VID not used\n");
379 /* divide and round up by 10 to get a value in mV */
380 vdd_target = DIV_ROUND_UP(vdd_target, 10);
381 debug("VID: vid = %d mV\n", vdd_target);
385 * Read voltage monitor to check real voltage.
387 vdd_last = read_voltage(i2caddress);
389 printf("VID: Couldn't read sensor abort VID adjustment\n");
393 vdd_current = vdd_last;
394 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
396 * Adjust voltage to at or one step above target.
397 * As measurements are less precise than setting the values
398 * we may run through dummy steps that cancel each other
399 * when stepping up and then down.
401 while (vdd_last > 0 &&
402 vdd_last < vdd_target) {
403 vdd_current += IR_VDD_STEP_UP;
404 vdd_last = set_voltage(i2caddress, vdd_current);
406 while (vdd_last > 0 &&
407 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
408 vdd_current -= IR_VDD_STEP_DOWN;
409 vdd_last = set_voltage(i2caddress, vdd_current);
413 printf("VID: Core voltage after adjustment is at %d mV\n",
423 static int print_vdd(void)
425 int vdd_last, ret, i2caddress;
427 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
429 debug("VID : I2c failed to switch channel\n");
432 ret = find_ir_chip_on_i2c();
434 printf("VID: Could not find voltage regulator on I2C.\n");
438 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
442 * Read voltage monitor to check real voltage.
444 vdd_last = read_voltage(i2caddress);
446 printf("VID: Couldn't read sensor abort VID adjustment\n");
449 printf("VID: Core voltage is at %d mV\n", vdd_last);
454 static int do_vdd_override(cmd_tbl_t *cmdtp,
461 return CMD_RET_USAGE;
463 if (!strict_strtoul(argv[1], 10, &override))
464 adjust_vdd(override); /* the value is checked by callee */
466 return CMD_RET_USAGE;
470 static int do_vdd_read(cmd_tbl_t *cmdtp,
475 return CMD_RET_USAGE;
482 vdd_override, 2, 0, do_vdd_override,
484 " - override with the voltage specified in mV, eg. 1050"
488 vdd_read, 1, 0, do_vdd_read,
490 " - Read the voltage specified in mV"