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 * Board specific settings for specific voltage value
39 int __weak
board_adjust_vdd(int vdd
)
44 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
45 defined(CONFIG_VOL_MONITOR_IR36021_READ)
47 * Get the i2c address configuration for the IR regulator chip
49 * There are some variance in the RDB HW regarding the I2C address configuration
50 * for the IR regulator chip, which is likely a problem of external resistor
51 * accuracy. So we just check each address in a hopefully non-intrusive mode
52 * and use the first one that seems to work
54 * The IR chip can show up under the following addresses:
55 * 0x08 (Verified on T1040RDB-PA,T4240RDB-PB,X-T4240RDB-16GPA)
56 * 0x09 (Verified on T1040RDB-PA)
57 * 0x38 (Verified on T2080QDS, T2081QDS, T4240RDB)
59 static int find_ir_chip_on_i2c(void)
65 const int ir_i2c_addr
[] = {0x38, 0x08, 0x09};
67 /* Check all the address */
68 for (i
= 0; i
< (sizeof(ir_i2c_addr
)/sizeof(ir_i2c_addr
[0])); i
++) {
69 i2caddress
= ir_i2c_addr
[i
];
70 ret
= i2c_read(i2caddress
,
71 IR36021_MFR_ID_OFFSET
, 1, (void *)&byte
,
73 if ((ret
>= 0) && (byte
== IR36021_MFR_ID
))
80 /* Maximum loop count waiting for new voltage to take effect */
81 #define MAX_LOOP_WAIT_NEW_VOL 100
82 /* Maximum loop count waiting for the voltage to be stable */
83 #define MAX_LOOP_WAIT_VOL_STABLE 100
85 * read_voltage from sensor on I2C bus
86 * We use average of 4 readings, waiting for WAIT_FOR_ADC before
89 #define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
91 /* If an INA220 chip is available, we can use it to read back the voltage
92 * as it may have a higher accuracy than the IR chip for the same purpose
94 #ifdef CONFIG_VOL_MONITOR_INA220
95 #define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
96 #define ADC_MIN_ACCURACY 4
98 #define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
99 #define ADC_MIN_ACCURACY 4
102 #ifdef CONFIG_VOL_MONITOR_INA220
103 static int read_voltage_from_INA220(int i2caddress
)
105 int i
, ret
, voltage_read
= 0;
109 for (i
= 0; i
< NUM_READINGS
; i
++) {
110 ret
= i2c_read(I2C_VOL_MONITOR_ADDR
,
111 I2C_VOL_MONITOR_BUS_V_OFFSET
, 1,
114 printf("VID: failed to read core voltage\n");
117 vol_mon
= (buf
[0] << 8) | buf
[1];
118 if (vol_mon
& I2C_VOL_MONITOR_BUS_V_OVF
) {
119 printf("VID: Core voltage sensor error\n");
122 debug("VID: bus voltage reads 0x%04x\n", vol_mon
);
124 voltage_read
+= (vol_mon
>> I2C_VOL_MONITOR_BUS_V_SHIFT
) * 4;
125 udelay(WAIT_FOR_ADC
);
127 /* calculate the average */
128 voltage_read
/= NUM_READINGS
;
134 /* read voltage from IR */
135 #ifdef CONFIG_VOL_MONITOR_IR36021_READ
136 static int read_voltage_from_IR(int i2caddress
)
138 int i
, ret
, voltage_read
= 0;
142 for (i
= 0; i
< NUM_READINGS
; i
++) {
143 ret
= i2c_read(i2caddress
,
144 IR36021_LOOP1_VOUT_OFFSET
,
147 printf("VID: failed to read vcpu\n");
152 printf("VID: Core voltage sensor error\n");
155 debug("VID: bus voltage reads 0x%02x\n", vol_mon
);
156 /* Resolution is 1/128V. We scale up here to get 1/128mV
157 * and divide at the end
159 voltage_read
+= vol_mon
* 1000;
160 udelay(WAIT_FOR_ADC
);
162 /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
163 voltage_read
= DIV_ROUND_UP(voltage_read
, 128);
165 /* calculate the average */
166 voltage_read
/= NUM_READINGS
;
168 /* Compensate for a board specific voltage drop between regulator and
169 * SoC before converting into an IR VID value
171 voltage_read
-= board_vdd_drop_compensation();
177 static int read_voltage(int i2caddress
)
180 #ifdef CONFIG_VOL_MONITOR_INA220
181 voltage_read
= read_voltage_from_INA220(i2caddress
);
182 #elif defined CONFIG_VOL_MONITOR_IR36021_READ
183 voltage_read
= read_voltage_from_IR(i2caddress
);
190 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
192 * We need to calculate how long before the voltage stops to drop
193 * or increase. It returns with the loop count. Each loop takes
194 * several readings (WAIT_FOR_ADC)
196 static int wait_for_new_voltage(int vdd
, int i2caddress
)
198 int timeout
, vdd_current
;
200 vdd_current
= read_voltage(i2caddress
);
201 /* wait until voltage starts to reach the target. Voltage slew
202 * rates by typical regulators will always lead to stable readings
203 * within each fairly long ADC interval in comparison to the
204 * intended voltage delta change until the target voltage is
205 * reached. The fairly small voltage delta change to any target
206 * VID voltage also means that this function will always complete
207 * within few iterations. If the timeout was ever reached, it would
208 * point to a serious failure in the regulator system.
211 abs(vdd
- vdd_current
) > (IR_VDD_STEP_UP
+ IR_VDD_STEP_DOWN
) &&
212 timeout
< MAX_LOOP_WAIT_NEW_VOL
; timeout
++) {
213 vdd_current
= read_voltage(i2caddress
);
215 if (timeout
>= MAX_LOOP_WAIT_NEW_VOL
) {
216 printf("VID: Voltage adjustment timeout\n");
223 * this function keeps reading the voltage until it is stable or until the
226 static int wait_for_voltage_stable(int i2caddress
)
228 int timeout
, vdd_current
, vdd
;
230 vdd
= read_voltage(i2caddress
);
231 udelay(NUM_READINGS
* WAIT_FOR_ADC
);
233 /* wait until voltage is stable */
234 vdd_current
= read_voltage(i2caddress
);
235 /* The maximum timeout is
236 * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
238 for (timeout
= MAX_LOOP_WAIT_VOL_STABLE
;
239 abs(vdd
- vdd_current
) > ADC_MIN_ACCURACY
&&
240 timeout
> 0; timeout
--) {
242 udelay(NUM_READINGS
* WAIT_FOR_ADC
);
243 vdd_current
= read_voltage(i2caddress
);
250 /* Set the voltage to the IR chip */
251 static int set_voltage_to_IR(int i2caddress
, int vdd
)
257 /* Compensate for a board specific voltage drop between regulator and
258 * SoC before converting into an IR VID value
260 vdd
+= board_vdd_drop_compensation();
261 #ifdef CONFIG_FSL_LSCH2
262 vid
= DIV_ROUND_UP(vdd
- 265, 5);
264 vid
= DIV_ROUND_UP(vdd
- 245, 5);
267 ret
= i2c_write(i2caddress
, IR36021_LOOP1_MANUAL_ID_OFFSET
,
268 1, (void *)&vid
, sizeof(vid
));
270 printf("VID: failed to write VID\n");
273 wait
= wait_for_new_voltage(vdd
, i2caddress
);
276 debug("VID: Waited %d us\n", wait
* NUM_READINGS
* WAIT_FOR_ADC
);
278 vdd_last
= wait_for_voltage_stable(i2caddress
);
281 debug("VID: Current voltage is %d mV\n", vdd_last
);
286 static int set_voltage(int i2caddress
, int vdd
)
290 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
291 vdd_last
= set_voltage_to_IR(i2caddress
, vdd
);
293 #error Specific voltage monitor must be defined
298 #ifdef CONFIG_FSL_LSCH3
299 int adjust_vdd(ulong vdd_override
)
301 int re_enable
= disable_interrupts();
302 struct ccsr_gur
*gur
= (void *)(CONFIG_SYS_FSL_GUTS_ADDR
);
304 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
305 defined(CONFIG_VOL_MONITOR_IR36021_READ)
310 int vdd_target
, vdd_current
, vdd_last
;
312 unsigned long vdd_string_override
;
314 #ifdef CONFIG_ARCH_LS1088A
315 static const uint16_t vdd
[32] = {
351 static const uint16_t vdd
[32] = {
391 ret
= i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR
);
393 debug("VID: I2C failed to switch channel\n");
397 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
398 defined(CONFIG_VOL_MONITOR_IR36021_READ)
399 ret
= find_ir_chip_on_i2c();
401 printf("VID: Could not find voltage regulator on I2C.\n");
406 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress
);
409 /* check IR chip work on Intel mode*/
410 ret
= i2c_read(i2caddress
,
411 IR36021_INTEL_MODE_OOFSET
,
414 printf("VID: failed to read IR chip mode.\n");
418 if ((buf
& IR36021_MODE_MASK
) != IR36021_INTEL_MODE
) {
419 printf("VID: IR Chip is not used in Intel mode.\n");
425 /* get the voltage ID from fuse status register */
426 fusesr
= in_le32(&gur
->dcfg_fusesr
);
427 vid
= (fusesr
>> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT
) &
428 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK
;
429 if ((vid
== 0) || (vid
== FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK
)) {
430 vid
= (fusesr
>> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT
) &
431 FSL_CHASSIS3_DCFG_FUSESR_VID_MASK
;
433 vdd_target
= vdd
[vid
];
435 /* check override variable for overriding VDD */
436 vdd_string
= env_get(CONFIG_VID_FLS_ENV
);
437 if (vdd_override
== 0 && vdd_string
&&
438 !strict_strtoul(vdd_string
, 10, &vdd_string_override
))
439 vdd_override
= vdd_string_override
;
441 if (vdd_override
>= VDD_MV_MIN
&& vdd_override
<= VDD_MV_MAX
) {
442 vdd_target
= vdd_override
* 10; /* convert to 1/10 mV */
443 debug("VDD override is %lu\n", vdd_override
);
444 } else if (vdd_override
!= 0) {
445 printf("Invalid value.\n");
448 /* divide and round up by 10 to get a value in mV */
449 vdd_target
= DIV_ROUND_UP(vdd_target
, 10);
450 if (vdd_target
== 0) {
451 debug("VID: VID not used\n");
454 } else if (vdd_target
< VDD_MV_MIN
|| vdd_target
> VDD_MV_MAX
) {
455 /* Check vdd_target is in valid range */
456 printf("VID: Target VID %d mV is not in range.\n",
461 debug("VID: vid = %d mV\n", vdd_target
);
465 * Read voltage monitor to check real voltage.
467 vdd_last
= read_voltage(i2caddress
);
469 printf("VID: Couldn't read sensor abort VID adjustment\n");
473 vdd_current
= vdd_last
;
474 debug("VID: Core voltage is currently at %d mV\n", vdd_last
);
476 * Adjust voltage to at or one step above target.
477 * As measurements are less precise than setting the values
478 * we may run through dummy steps that cancel each other
479 * when stepping up and then down.
481 while (vdd_last
> 0 &&
482 vdd_last
< vdd_target
) {
483 vdd_current
+= IR_VDD_STEP_UP
;
484 vdd_last
= set_voltage(i2caddress
, vdd_current
);
486 while (vdd_last
> 0 &&
487 vdd_last
> vdd_target
+ (IR_VDD_STEP_DOWN
- 1)) {
488 vdd_current
-= IR_VDD_STEP_DOWN
;
489 vdd_last
= set_voltage(i2caddress
, vdd_current
);
492 if (board_adjust_vdd(vdd_target
) < 0) {
498 printf("VID: Core voltage after adjustment is at %d mV\n",
505 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT
);
508 #else /* !CONFIG_FSL_LSCH3 */
509 int adjust_vdd(ulong vdd_override
)
511 int re_enable
= disable_interrupts();
512 #if defined(CONFIG_FSL_LSCH2)
513 struct ccsr_gur
*gur
= (void *)(CONFIG_SYS_FSL_GUTS_ADDR
);
515 ccsr_gur_t __iomem
*gur
=
516 (void __iomem
*)(CONFIG_SYS_MPC85xx_GUTS_ADDR
);
520 int vdd_target
, vdd_current
, vdd_last
;
522 unsigned long vdd_string_override
;
524 static const uint16_t vdd
[32] = {
557 ret
= i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR
);
559 debug("VID: I2C failed to switch channel\n");
563 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
564 defined(CONFIG_VOL_MONITOR_IR36021_READ)
565 ret
= find_ir_chip_on_i2c();
567 printf("VID: Could not find voltage regulator on I2C.\n");
572 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress
);
575 /* check IR chip work on Intel mode*/
576 ret
= i2c_read(i2caddress
,
577 IR36021_INTEL_MODE_OOFSET
,
580 printf("VID: failed to read IR chip mode.\n");
584 if ((buf
& IR36021_MODE_MASK
) != IR36021_INTEL_MODE
) {
585 printf("VID: IR Chip is not used in Intel mode.\n");
591 /* get the voltage ID from fuse status register */
592 fusesr
= in_be32(&gur
->dcfg_fusesr
);
594 * VID is used according to the table below
595 * ---------------------------------------
597 * |-------------------------------------|
598 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
599 * ---------------+---------+-----------------+---------|
600 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
601 * | A |----------+---------+-----------------+---------|
602 * | _ | 5b00001 |VID = | VID = |VID = |
603 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
604 * | _ | 5b11110 | | | |
605 * | A |----------+---------+-----------------+---------|
606 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
608 * ------------------------------------------------------
610 #ifdef CONFIG_FSL_LSCH2
611 vid
= (fusesr
>> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT
) &
612 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK
;
613 if ((vid
== 0) || (vid
== FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK
)) {
614 vid
= (fusesr
>> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT
) &
615 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK
;
618 vid
= (fusesr
>> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT
) &
619 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK
;
620 if ((vid
== 0) || (vid
== FSL_CORENET_DCFG_FUSESR_ALTVID_MASK
)) {
621 vid
= (fusesr
>> FSL_CORENET_DCFG_FUSESR_VID_SHIFT
) &
622 FSL_CORENET_DCFG_FUSESR_VID_MASK
;
625 vdd_target
= vdd
[vid
];
627 /* check override variable for overriding VDD */
628 vdd_string
= env_get(CONFIG_VID_FLS_ENV
);
629 if (vdd_override
== 0 && vdd_string
&&
630 !strict_strtoul(vdd_string
, 10, &vdd_string_override
))
631 vdd_override
= vdd_string_override
;
632 if (vdd_override
>= VDD_MV_MIN
&& vdd_override
<= VDD_MV_MAX
) {
633 vdd_target
= vdd_override
* 10; /* convert to 1/10 mV */
634 debug("VDD override is %lu\n", vdd_override
);
635 } else if (vdd_override
!= 0) {
636 printf("Invalid value.\n");
638 if (vdd_target
== 0) {
639 debug("VID: VID not used\n");
643 /* divide and round up by 10 to get a value in mV */
644 vdd_target
= DIV_ROUND_UP(vdd_target
, 10);
645 debug("VID: vid = %d mV\n", vdd_target
);
649 * Read voltage monitor to check real voltage.
651 vdd_last
= read_voltage(i2caddress
);
653 printf("VID: Couldn't read sensor abort VID adjustment\n");
657 vdd_current
= vdd_last
;
658 debug("VID: Core voltage is currently at %d mV\n", vdd_last
);
660 * Adjust voltage to at or one step above target.
661 * As measurements are less precise than setting the values
662 * we may run through dummy steps that cancel each other
663 * when stepping up and then down.
665 while (vdd_last
> 0 &&
666 vdd_last
< vdd_target
) {
667 vdd_current
+= IR_VDD_STEP_UP
;
668 vdd_last
= set_voltage(i2caddress
, vdd_current
);
670 while (vdd_last
> 0 &&
671 vdd_last
> vdd_target
+ (IR_VDD_STEP_DOWN
- 1)) {
672 vdd_current
-= IR_VDD_STEP_DOWN
;
673 vdd_last
= set_voltage(i2caddress
, vdd_current
);
677 printf("VID: Core voltage after adjustment is at %d mV\n",
685 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT
);
691 static int print_vdd(void)
693 int vdd_last
, ret
, i2caddress
;
695 ret
= i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR
);
697 debug("VID : I2c failed to switch channel\n");
700 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
701 defined(CONFIG_VOL_MONITOR_IR36021_READ)
702 ret
= find_ir_chip_on_i2c();
704 printf("VID: Could not find voltage regulator on I2C.\n");
708 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress
);
713 * Read voltage monitor to check real voltage.
715 vdd_last
= read_voltage(i2caddress
);
717 printf("VID: Couldn't read sensor abort VID adjustment\n");
720 printf("VID: Core voltage is at %d mV\n", vdd_last
);
722 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT
);
724 return ret
< 0 ? -1 : 0;
728 static int do_vdd_override(cmd_tbl_t
*cmdtp
,
735 return CMD_RET_USAGE
;
737 if (!strict_strtoul(argv
[1], 10, &override
))
738 adjust_vdd(override
); /* the value is checked by callee */
740 return CMD_RET_USAGE
;
744 static int do_vdd_read(cmd_tbl_t
*cmdtp
,
749 return CMD_RET_USAGE
;
756 vdd_override
, 2, 0, do_vdd_override
,
758 " - override with the voltage specified in mV, eg. 1050"
762 vdd_read
, 1, 0, do_vdd_read
,
764 " - Read the voltage specified in mV"