2 * Copyright 2014 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
11 #ifdef CONFIG_FSL_LSCH2
12 #include <asm/arch/immap_lsch2.h>
13 #elif defined(CONFIG_FSL_LSCH3)
14 #include <asm/arch/immap_lsch3.h>
16 #include <asm/immap_85xx.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 int __weak i2c_multiplexer_select_vid_channel(u8 channel)
28 * Compensate for a board specific voltage drop between regulator and SoC
29 * return a value in mV
31 int __weak board_vdd_drop_compensation(void)
37 * Get the i2c address configuration for the IR regulator chip
39 * There are some variance in the RDB HW regarding the I2C address configuration
40 * for the IR regulator chip, which is likely a problem of external resistor
41 * accuracy. So we just check each address in a hopefully non-intrusive mode
42 * and use the first one that seems to work
44 * The IR chip can show up under the following addresses:
45 * 0x08 (Verified on T1040RDB-PA,T4240RDB-PB,X-T4240RDB-16GPA)
46 * 0x09 (Verified on T1040RDB-PA)
47 * 0x38 (Verified on T2080QDS, T2081QDS, T4240RDB)
49 static int find_ir_chip_on_i2c(void)
55 const int ir_i2c_addr[] = {0x38, 0x08, 0x09};
57 /* Check all the address */
58 for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) {
59 i2caddress = ir_i2c_addr[i];
60 ret = i2c_read(i2caddress,
61 IR36021_MFR_ID_OFFSET, 1, (void *)&byte,
63 if ((ret >= 0) && (byte == IR36021_MFR_ID))
69 /* Maximum loop count waiting for new voltage to take effect */
70 #define MAX_LOOP_WAIT_NEW_VOL 100
71 /* Maximum loop count waiting for the voltage to be stable */
72 #define MAX_LOOP_WAIT_VOL_STABLE 100
74 * read_voltage from sensor on I2C bus
75 * We use average of 4 readings, waiting for WAIT_FOR_ADC before
78 #define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
80 /* If an INA220 chip is available, we can use it to read back the voltage
81 * as it may have a higher accuracy than the IR chip for the same purpose
83 #ifdef CONFIG_VOL_MONITOR_INA220
84 #define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
85 #define ADC_MIN_ACCURACY 4
87 #define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
88 #define ADC_MIN_ACCURACY 4
91 #ifdef CONFIG_VOL_MONITOR_INA220
92 static int read_voltage_from_INA220(int i2caddress)
94 int i, ret, voltage_read = 0;
98 for (i = 0; i < NUM_READINGS; i++) {
99 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
100 I2C_VOL_MONITOR_BUS_V_OFFSET, 1,
103 printf("VID: failed to read core voltage\n");
106 vol_mon = (buf[0] << 8) | buf[1];
107 if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
108 printf("VID: Core voltage sensor error\n");
111 debug("VID: bus voltage reads 0x%04x\n", vol_mon);
113 voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
114 udelay(WAIT_FOR_ADC);
116 /* calculate the average */
117 voltage_read /= NUM_READINGS;
123 /* read voltage from IR */
124 #ifdef CONFIG_VOL_MONITOR_IR36021_READ
125 static int read_voltage_from_IR(int i2caddress)
127 int i, ret, voltage_read = 0;
131 for (i = 0; i < NUM_READINGS; i++) {
132 ret = i2c_read(i2caddress,
133 IR36021_LOOP1_VOUT_OFFSET,
136 printf("VID: failed to read vcpu\n");
141 printf("VID: Core voltage sensor error\n");
144 debug("VID: bus voltage reads 0x%02x\n", vol_mon);
145 /* Resolution is 1/128V. We scale up here to get 1/128mV
146 * and divide at the end
148 voltage_read += vol_mon * 1000;
149 udelay(WAIT_FOR_ADC);
151 /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
152 voltage_read = DIV_ROUND_UP(voltage_read, 128);
154 /* calculate the average */
155 voltage_read /= NUM_READINGS;
157 /* Compensate for a board specific voltage drop between regulator and
158 * SoC before converting into an IR VID value
160 voltage_read -= board_vdd_drop_compensation();
166 static int read_voltage(int i2caddress)
169 #ifdef CONFIG_VOL_MONITOR_INA220
170 voltage_read = read_voltage_from_INA220(i2caddress);
171 #elif defined CONFIG_VOL_MONITOR_IR36021_READ
172 voltage_read = read_voltage_from_IR(i2caddress);
180 * We need to calculate how long before the voltage stops to drop
181 * or increase. It returns with the loop count. Each loop takes
182 * several readings (WAIT_FOR_ADC)
184 static int wait_for_new_voltage(int vdd, int i2caddress)
186 int timeout, vdd_current;
188 vdd_current = read_voltage(i2caddress);
189 /* wait until voltage starts to reach the target. Voltage slew
190 * rates by typical regulators will always lead to stable readings
191 * within each fairly long ADC interval in comparison to the
192 * intended voltage delta change until the target voltage is
193 * reached. The fairly small voltage delta change to any target
194 * VID voltage also means that this function will always complete
195 * within few iterations. If the timeout was ever reached, it would
196 * point to a serious failure in the regulator system.
199 abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
200 timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
201 vdd_current = read_voltage(i2caddress);
203 if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
204 printf("VID: Voltage adjustment timeout\n");
211 * this function keeps reading the voltage until it is stable or until the
214 static int wait_for_voltage_stable(int i2caddress)
216 int timeout, vdd_current, vdd;
218 vdd = read_voltage(i2caddress);
219 udelay(NUM_READINGS * WAIT_FOR_ADC);
221 /* wait until voltage is stable */
222 vdd_current = read_voltage(i2caddress);
223 /* The maximum timeout is
224 * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
226 for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
227 abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
228 timeout > 0; timeout--) {
230 udelay(NUM_READINGS * WAIT_FOR_ADC);
231 vdd_current = read_voltage(i2caddress);
238 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
239 /* Set the voltage to the IR chip */
240 static int set_voltage_to_IR(int i2caddress, int vdd)
246 /* Compensate for a board specific voltage drop between regulator and
247 * SoC before converting into an IR VID value
249 vdd += board_vdd_drop_compensation();
250 #ifdef CONFIG_FSL_LSCH2
251 vid = DIV_ROUND_UP(vdd - 265, 5);
253 vid = DIV_ROUND_UP(vdd - 245, 5);
256 ret = i2c_write(i2caddress, IR36021_LOOP1_MANUAL_ID_OFFSET,
257 1, (void *)&vid, sizeof(vid));
259 printf("VID: failed to write VID\n");
262 wait = wait_for_new_voltage(vdd, i2caddress);
265 debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
267 vdd_last = wait_for_voltage_stable(i2caddress);
270 debug("VID: Current voltage is %d mV\n", vdd_last);
275 static int set_voltage(int i2caddress, int vdd)
279 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
280 vdd_last = set_voltage_to_IR(i2caddress, vdd);
282 #error Specific voltage monitor must be defined
287 #ifdef CONFIG_FSL_LSCH3
288 int adjust_vdd(ulong vdd_override)
290 int re_enable = disable_interrupts();
291 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
294 int vdd_target, vdd_current, vdd_last;
296 unsigned long vdd_string_override;
298 static const uint16_t vdd[32] = {
337 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
339 debug("VID: I2C failed to switch channel\n");
343 ret = find_ir_chip_on_i2c();
345 printf("VID: Could not find voltage regulator on I2C.\n");
350 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
353 /* check IR chip work on Intel mode*/
354 ret = i2c_read(i2caddress,
355 IR36021_INTEL_MODE_OOFSET,
358 printf("VID: failed to read IR chip mode.\n");
362 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
363 printf("VID: IR Chip is not used in Intel mode.\n");
368 /* get the voltage ID from fuse status register */
369 fusesr = in_le32(&gur->dcfg_fusesr);
370 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
371 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
372 if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
373 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
374 FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
376 vdd_target = vdd[vid];
378 /* check override variable for overriding VDD */
379 vdd_string = getenv(CONFIG_VID_FLS_ENV);
380 if (vdd_override == 0 && vdd_string &&
381 !strict_strtoul(vdd_string, 10, &vdd_string_override))
382 vdd_override = vdd_string_override;
384 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
385 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
386 debug("VDD override is %lu\n", vdd_override);
387 } else if (vdd_override != 0) {
388 printf("Invalid value.\n");
391 /* divide and round up by 10 to get a value in mV */
392 vdd_target = DIV_ROUND_UP(vdd_target, 10);
393 if (vdd_target == 0) {
394 debug("VID: VID not used\n");
397 } else if (vdd_target < VDD_MV_MIN || vdd_target > VDD_MV_MAX) {
398 /* Check vdd_target is in valid range */
399 printf("VID: Target VID %d mV is not in range.\n",
404 debug("VID: vid = %d mV\n", vdd_target);
408 * Read voltage monitor to check real voltage.
410 vdd_last = read_voltage(i2caddress);
412 printf("VID: Couldn't read sensor abort VID adjustment\n");
416 vdd_current = vdd_last;
417 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
419 * Adjust voltage to at or one step above target.
420 * As measurements are less precise than setting the values
421 * we may run through dummy steps that cancel each other
422 * when stepping up and then down.
424 while (vdd_last > 0 &&
425 vdd_last < vdd_target) {
426 vdd_current += IR_VDD_STEP_UP;
427 vdd_last = set_voltage(i2caddress, vdd_current);
429 while (vdd_last > 0 &&
430 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
431 vdd_current -= IR_VDD_STEP_DOWN;
432 vdd_last = set_voltage(i2caddress, vdd_current);
436 printf("VID: Core voltage after adjustment is at %d mV\n",
443 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
446 #else /* !CONFIG_FSL_LSCH3 */
447 int adjust_vdd(ulong vdd_override)
449 int re_enable = disable_interrupts();
450 #if defined(CONFIG_FSL_LSCH2)
451 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
453 ccsr_gur_t __iomem *gur =
454 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
458 int vdd_target, vdd_current, vdd_last;
460 unsigned long vdd_string_override;
462 static const uint16_t vdd[32] = {
495 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
497 debug("VID: I2C failed to switch channel\n");
501 ret = find_ir_chip_on_i2c();
503 printf("VID: Could not find voltage regulator on I2C.\n");
508 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
511 /* check IR chip work on Intel mode*/
512 ret = i2c_read(i2caddress,
513 IR36021_INTEL_MODE_OOFSET,
516 printf("VID: failed to read IR chip mode.\n");
520 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
521 printf("VID: IR Chip is not used in Intel mode.\n");
526 /* get the voltage ID from fuse status register */
527 fusesr = in_be32(&gur->dcfg_fusesr);
529 * VID is used according to the table below
530 * ---------------------------------------
532 * |-------------------------------------|
533 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
534 * ---------------+---------+-----------------+---------|
535 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
536 * | A |----------+---------+-----------------+---------|
537 * | _ | 5b00001 |VID = | VID = |VID = |
538 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
539 * | _ | 5b11110 | | | |
540 * | A |----------+---------+-----------------+---------|
541 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
543 * ------------------------------------------------------
545 #ifdef CONFIG_FSL_LSCH2
546 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
547 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
548 if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
549 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
550 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
553 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
554 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
555 if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
556 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
557 FSL_CORENET_DCFG_FUSESR_VID_MASK;
560 vdd_target = vdd[vid];
562 /* check override variable for overriding VDD */
563 vdd_string = getenv(CONFIG_VID_FLS_ENV);
564 if (vdd_override == 0 && vdd_string &&
565 !strict_strtoul(vdd_string, 10, &vdd_string_override))
566 vdd_override = vdd_string_override;
567 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
568 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
569 debug("VDD override is %lu\n", vdd_override);
570 } else if (vdd_override != 0) {
571 printf("Invalid value.\n");
573 if (vdd_target == 0) {
574 debug("VID: VID not used\n");
578 /* divide and round up by 10 to get a value in mV */
579 vdd_target = DIV_ROUND_UP(vdd_target, 10);
580 debug("VID: vid = %d mV\n", vdd_target);
584 * Read voltage monitor to check real voltage.
586 vdd_last = read_voltage(i2caddress);
588 printf("VID: Couldn't read sensor abort VID adjustment\n");
592 vdd_current = vdd_last;
593 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
595 * Adjust voltage to at or one step above target.
596 * As measurements are less precise than setting the values
597 * we may run through dummy steps that cancel each other
598 * when stepping up and then down.
600 while (vdd_last > 0 &&
601 vdd_last < vdd_target) {
602 vdd_current += IR_VDD_STEP_UP;
603 vdd_last = set_voltage(i2caddress, vdd_current);
605 while (vdd_last > 0 &&
606 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
607 vdd_current -= IR_VDD_STEP_DOWN;
608 vdd_last = set_voltage(i2caddress, vdd_current);
612 printf("VID: Core voltage after adjustment is at %d mV\n",
620 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
626 static int print_vdd(void)
628 int vdd_last, ret, i2caddress;
630 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
632 debug("VID : I2c failed to switch channel\n");
635 ret = find_ir_chip_on_i2c();
637 printf("VID: Could not find voltage regulator on I2C.\n");
641 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
645 * Read voltage monitor to check real voltage.
647 vdd_last = read_voltage(i2caddress);
649 printf("VID: Couldn't read sensor abort VID adjustment\n");
652 printf("VID: Core voltage is at %d mV\n", vdd_last);
654 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
656 return ret < 0 ? -1 : 0;
660 static int do_vdd_override(cmd_tbl_t *cmdtp,
667 return CMD_RET_USAGE;
669 if (!strict_strtoul(argv[1], 10, &override))
670 adjust_vdd(override); /* the value is checked by callee */
672 return CMD_RET_USAGE;
676 static int do_vdd_read(cmd_tbl_t *cmdtp,
681 return CMD_RET_USAGE;
688 vdd_override, 2, 0, do_vdd_override,
690 " - override with the voltage specified in mV, eg. 1050"
694 vdd_read, 1, 0, do_vdd_read,
696 " - Read the voltage specified in mV"