1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Freescale Semiconductor, Inc.
13 #ifdef CONFIG_FSL_LSCH2
14 #include <asm/arch/immap_lsch2.h>
15 #elif defined(CONFIG_FSL_LSCH3)
16 #include <asm/arch/immap_lsch3.h>
18 #include <asm/immap_85xx.h>
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};
70 /* Check all the address */
71 for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) {
72 i2caddress = ir_i2c_addr[i];
74 ret = i2c_read(i2caddress,
75 IR36021_MFR_ID_OFFSET, 1, (void *)&byte,
78 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
80 ret = dm_i2c_read(dev, IR36021_MFR_ID_OFFSET,
81 (void *)&byte, sizeof(byte));
83 if ((ret >= 0) && (byte == IR36021_MFR_ID))
90 /* Maximum loop count waiting for new voltage to take effect */
91 #define MAX_LOOP_WAIT_NEW_VOL 100
92 /* Maximum loop count waiting for the voltage to be stable */
93 #define MAX_LOOP_WAIT_VOL_STABLE 100
95 * read_voltage from sensor on I2C bus
96 * We use average of 4 readings, waiting for WAIT_FOR_ADC before
99 #define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
101 /* If an INA220 chip is available, we can use it to read back the voltage
102 * as it may have a higher accuracy than the IR chip for the same purpose
104 #ifdef CONFIG_VOL_MONITOR_INA220
105 #define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
106 #define ADC_MIN_ACCURACY 4
108 #define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
109 #define ADC_MIN_ACCURACY 4
112 #ifdef CONFIG_VOL_MONITOR_INA220
113 static int read_voltage_from_INA220(int i2caddress)
115 int i, ret, voltage_read = 0;
122 for (i = 0; i < NUM_READINGS; i++) {
123 #ifndef CONFIG_DM_I2C
124 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
125 I2C_VOL_MONITOR_BUS_V_OFFSET, 1,
128 ret = i2c_get_chip_for_busnum(0, I2C_VOL_MONITOR_ADDR, 1, &dev);
130 ret = dm_i2c_read(dev, I2C_VOL_MONITOR_BUS_V_OFFSET,
134 printf("VID: failed to read core voltage\n");
137 vol_mon = (buf[0] << 8) | buf[1];
138 if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
139 printf("VID: Core voltage sensor error\n");
142 debug("VID: bus voltage reads 0x%04x\n", vol_mon);
144 voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
145 udelay(WAIT_FOR_ADC);
147 /* calculate the average */
148 voltage_read /= NUM_READINGS;
154 /* read voltage from IR */
155 #ifdef CONFIG_VOL_MONITOR_IR36021_READ
156 static int read_voltage_from_IR(int i2caddress)
158 int i, ret, voltage_read = 0;
165 for (i = 0; i < NUM_READINGS; i++) {
166 #ifndef CONFIG_DM_I2C
167 ret = i2c_read(i2caddress,
168 IR36021_LOOP1_VOUT_OFFSET,
171 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
173 ret = dm_i2c_read(dev, IR36021_LOOP1_VOUT_OFFSET,
177 printf("VID: failed to read vcpu\n");
182 printf("VID: Core voltage sensor error\n");
185 debug("VID: bus voltage reads 0x%02x\n", vol_mon);
186 /* Resolution is 1/128V. We scale up here to get 1/128mV
187 * and divide at the end
189 voltage_read += vol_mon * 1000;
190 udelay(WAIT_FOR_ADC);
192 /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
193 voltage_read = DIV_ROUND_UP(voltage_read, 128);
195 /* calculate the average */
196 voltage_read /= NUM_READINGS;
198 /* Compensate for a board specific voltage drop between regulator and
199 * SoC before converting into an IR VID value
201 voltage_read -= board_vdd_drop_compensation();
207 #ifdef CONFIG_VOL_MONITOR_LTC3882_READ
208 /* read the current value of the LTC Regulator Voltage */
209 static int read_voltage_from_LTC(int i2caddress)
212 u8 chan = PWM_CHANNEL0;
214 #ifndef CONFIG_DM_I2C
215 /* select the PAGE 0 using PMBus commands PAGE for VDD*/
216 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
217 PMBUS_CMD_PAGE, 1, &chan, 1);
221 ret = i2c_get_chip_for_busnum(0, I2C_VOL_MONITOR_ADDR, 1, &dev);
223 ret = dm_i2c_write(dev, PMBUS_CMD_PAGE, &chan, 1);
226 printf("VID: failed to select VDD Page 0\n");
230 #ifndef CONFIG_DM_I2C
231 /*read the output voltage using PMBus command READ_VOUT*/
232 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
233 PMBUS_CMD_READ_VOUT, 1, (void *)&vcode, 2);
235 ret = dm_i2c_read(dev, PMBUS_CMD_READ_VOUT, (void *)&vcode, 2);
237 printf("VID: failed to read the volatge\n");
242 printf("VID: failed to read the volatge\n");
246 /* Scale down to the real mV as LTC resolution is 1/4096V,rounding up */
247 vcode = DIV_ROUND_UP(vcode * 1000, 4096);
253 static int read_voltage(int i2caddress)
256 #ifdef CONFIG_VOL_MONITOR_INA220
257 voltage_read = read_voltage_from_INA220(i2caddress);
258 #elif defined CONFIG_VOL_MONITOR_IR36021_READ
259 voltage_read = read_voltage_from_IR(i2caddress);
260 #elif defined CONFIG_VOL_MONITOR_LTC3882_READ
261 voltage_read = read_voltage_from_LTC(i2caddress);
268 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
270 * We need to calculate how long before the voltage stops to drop
271 * or increase. It returns with the loop count. Each loop takes
272 * several readings (WAIT_FOR_ADC)
274 static int wait_for_new_voltage(int vdd, int i2caddress)
276 int timeout, vdd_current;
278 vdd_current = read_voltage(i2caddress);
279 /* wait until voltage starts to reach the target. Voltage slew
280 * rates by typical regulators will always lead to stable readings
281 * within each fairly long ADC interval in comparison to the
282 * intended voltage delta change until the target voltage is
283 * reached. The fairly small voltage delta change to any target
284 * VID voltage also means that this function will always complete
285 * within few iterations. If the timeout was ever reached, it would
286 * point to a serious failure in the regulator system.
289 abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
290 timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
291 vdd_current = read_voltage(i2caddress);
293 if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
294 printf("VID: Voltage adjustment timeout\n");
301 * this function keeps reading the voltage until it is stable or until the
304 static int wait_for_voltage_stable(int i2caddress)
306 int timeout, vdd_current, vdd;
308 vdd = read_voltage(i2caddress);
309 udelay(NUM_READINGS * WAIT_FOR_ADC);
311 /* wait until voltage is stable */
312 vdd_current = read_voltage(i2caddress);
313 /* The maximum timeout is
314 * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
316 for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
317 abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
318 timeout > 0; timeout--) {
320 udelay(NUM_READINGS * WAIT_FOR_ADC);
321 vdd_current = read_voltage(i2caddress);
328 /* Set the voltage to the IR chip */
329 static int set_voltage_to_IR(int i2caddress, int vdd)
335 /* Compensate for a board specific voltage drop between regulator and
336 * SoC before converting into an IR VID value
338 vdd += board_vdd_drop_compensation();
339 #ifdef CONFIG_FSL_LSCH2
340 vid = DIV_ROUND_UP(vdd - 265, 5);
342 vid = DIV_ROUND_UP(vdd - 245, 5);
345 #ifndef CONFIG_DM_I2C
346 ret = i2c_write(i2caddress, IR36021_LOOP1_MANUAL_ID_OFFSET,
347 1, (void *)&vid, sizeof(vid));
351 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
353 ret = dm_i2c_write(dev, IR36021_LOOP1_MANUAL_ID_OFFSET,
354 (void *)&vid, sizeof(vid));
358 printf("VID: failed to write VID\n");
361 wait = wait_for_new_voltage(vdd, i2caddress);
364 debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
366 vdd_last = wait_for_voltage_stable(i2caddress);
369 debug("VID: Current voltage is %d mV\n", vdd_last);
375 #ifdef CONFIG_VOL_MONITOR_LTC3882_SET
376 /* this function sets the VDD and returns the value set */
377 static int set_voltage_to_LTC(int i2caddress, int vdd)
379 int ret, vdd_last, vdd_target = vdd;
380 int count = 100, temp = 0;
382 /* Scale up to the LTC resolution is 1/4096V */
383 vdd = (vdd * 4096) / 1000;
385 /* 5-byte buffer which needs to be sent following the
386 * PMBus command PAGE_PLUS_WRITE.
388 u8 buff[5] = {0x04, PWM_CHANNEL0, PMBUS_CMD_VOUT_COMMAND,
389 vdd & 0xFF, (vdd & 0xFF00) >> 8};
391 /* Write the desired voltage code to the regulator */
392 #ifndef CONFIG_DM_I2C
393 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
394 PMBUS_CMD_PAGE_PLUS_WRITE, 1, (void *)&buff, 5);
398 ret = i2c_get_chip_for_busnum(0, I2C_VOL_MONITOR_ADDR, 1, &dev);
400 ret = dm_i2c_write(dev, PMBUS_CMD_PAGE_PLUS_WRITE,
404 printf("VID: I2C failed to write to the volatge regulator\n");
408 /* Wait for the volatge to get to the desired value */
410 vdd_last = read_voltage_from_LTC(i2caddress);
412 printf("VID: Couldn't read sensor abort VID adjust\n");
416 temp = vdd_last - vdd_target;
417 } while ((abs(temp) > 2) && (count > 0));
423 static int set_voltage(int i2caddress, int vdd)
427 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
428 vdd_last = set_voltage_to_IR(i2caddress, vdd);
429 #elif defined CONFIG_VOL_MONITOR_LTC3882_SET
430 vdd_last = set_voltage_to_LTC(i2caddress, vdd);
432 #error Specific voltage monitor must be defined
437 #ifdef CONFIG_FSL_LSCH3
438 int adjust_vdd(ulong vdd_override)
440 int re_enable = disable_interrupts();
441 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
443 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
444 defined(CONFIG_VOL_MONITOR_IR36021_READ)
449 int vdd_target, vdd_current, vdd_last;
451 unsigned long vdd_string_override;
453 #ifdef CONFIG_ARCH_LX2160A
454 static const u16 vdd[32] = {
489 #ifdef CONFIG_ARCH_LS1088A
490 static const uint16_t vdd[32] = {
526 static const uint16_t vdd[32] = {
567 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
569 debug("VID: I2C failed to switch channel\n");
573 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
574 defined(CONFIG_VOL_MONITOR_IR36021_READ)
575 ret = find_ir_chip_on_i2c();
577 printf("VID: Could not find voltage regulator on I2C.\n");
582 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
585 /* check IR chip work on Intel mode*/
586 #ifndef CONFIG_DM_I2C
587 ret = i2c_read(i2caddress,
588 IR36021_INTEL_MODE_OOFSET,
593 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
595 ret = dm_i2c_read(dev, IR36021_INTEL_MODE_OOFSET,
599 printf("VID: failed to read IR chip mode.\n");
604 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
605 printf("VID: IR Chip is not used in Intel mode.\n");
611 /* get the voltage ID from fuse status register */
612 fusesr = in_le32(&gur->dcfg_fusesr);
613 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
614 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
615 if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
616 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
617 FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
619 vdd_target = vdd[vid];
621 /* check override variable for overriding VDD */
622 vdd_string = env_get(CONFIG_VID_FLS_ENV);
623 if (vdd_override == 0 && vdd_string &&
624 !strict_strtoul(vdd_string, 10, &vdd_string_override))
625 vdd_override = vdd_string_override;
627 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
628 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
629 debug("VDD override is %lu\n", vdd_override);
630 } else if (vdd_override != 0) {
631 printf("Invalid value.\n");
634 /* divide and round up by 10 to get a value in mV */
635 vdd_target = DIV_ROUND_UP(vdd_target, 10);
636 if (vdd_target == 0) {
637 debug("VID: VID not used\n");
640 } else if (vdd_target < VDD_MV_MIN || vdd_target > VDD_MV_MAX) {
641 /* Check vdd_target is in valid range */
642 printf("VID: Target VID %d mV is not in range.\n",
647 debug("VID: vid = %d mV\n", vdd_target);
651 * Read voltage monitor to check real voltage.
653 vdd_last = read_voltage(i2caddress);
655 printf("VID: Couldn't read sensor abort VID adjustment\n");
659 vdd_current = vdd_last;
660 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
662 #ifdef CONFIG_VOL_MONITOR_LTC3882_SET
663 /* Set the target voltage */
664 vdd_last = vdd_current = set_voltage(i2caddress, vdd_target);
667 * Adjust voltage to at or one step above target.
668 * As measurements are less precise than setting the values
669 * we may run through dummy steps that cancel each other
670 * when stepping up and then down.
672 while (vdd_last > 0 &&
673 vdd_last < vdd_target) {
674 vdd_current += IR_VDD_STEP_UP;
675 vdd_last = set_voltage(i2caddress, vdd_current);
677 while (vdd_last > 0 &&
678 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
679 vdd_current -= IR_VDD_STEP_DOWN;
680 vdd_last = set_voltage(i2caddress, vdd_current);
684 if (board_adjust_vdd(vdd_target) < 0) {
690 printf("VID: Core voltage after adjustment is at %d mV\n",
697 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
700 #else /* !CONFIG_FSL_LSCH3 */
701 int adjust_vdd(ulong vdd_override)
703 int re_enable = disable_interrupts();
704 #if defined(CONFIG_FSL_LSCH2)
705 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
707 ccsr_gur_t __iomem *gur =
708 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
712 int vdd_target, vdd_current, vdd_last;
714 unsigned long vdd_string_override;
716 static const uint16_t vdd[32] = {
749 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
751 debug("VID: I2C failed to switch channel\n");
755 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
756 defined(CONFIG_VOL_MONITOR_IR36021_READ)
757 ret = find_ir_chip_on_i2c();
759 printf("VID: Could not find voltage regulator on I2C.\n");
764 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
767 /* check IR chip work on Intel mode*/
768 #ifndef CONFIG_DM_I2C
769 ret = i2c_read(i2caddress,
770 IR36021_INTEL_MODE_OOFSET,
775 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
777 ret = dm_i2c_read(dev, IR36021_INTEL_MODE_OOFSET,
781 printf("VID: failed to read IR chip mode.\n");
785 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
786 printf("VID: IR Chip is not used in Intel mode.\n");
792 /* get the voltage ID from fuse status register */
793 fusesr = in_be32(&gur->dcfg_fusesr);
795 * VID is used according to the table below
796 * ---------------------------------------
798 * |-------------------------------------|
799 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
800 * ---------------+---------+-----------------+---------|
801 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
802 * | A |----------+---------+-----------------+---------|
803 * | _ | 5b00001 |VID = | VID = |VID = |
804 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
805 * | _ | 5b11110 | | | |
806 * | A |----------+---------+-----------------+---------|
807 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
809 * ------------------------------------------------------
811 #ifdef CONFIG_FSL_LSCH2
812 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
813 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
814 if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
815 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
816 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
819 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
820 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
821 if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
822 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
823 FSL_CORENET_DCFG_FUSESR_VID_MASK;
826 vdd_target = vdd[vid];
828 /* check override variable for overriding VDD */
829 vdd_string = env_get(CONFIG_VID_FLS_ENV);
830 if (vdd_override == 0 && vdd_string &&
831 !strict_strtoul(vdd_string, 10, &vdd_string_override))
832 vdd_override = vdd_string_override;
833 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
834 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
835 debug("VDD override is %lu\n", vdd_override);
836 } else if (vdd_override != 0) {
837 printf("Invalid value.\n");
839 if (vdd_target == 0) {
840 debug("VID: VID not used\n");
844 /* divide and round up by 10 to get a value in mV */
845 vdd_target = DIV_ROUND_UP(vdd_target, 10);
846 debug("VID: vid = %d mV\n", vdd_target);
850 * Read voltage monitor to check real voltage.
852 vdd_last = read_voltage(i2caddress);
854 printf("VID: Couldn't read sensor abort VID adjustment\n");
858 vdd_current = vdd_last;
859 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
861 * Adjust voltage to at or one step above target.
862 * As measurements are less precise than setting the values
863 * we may run through dummy steps that cancel each other
864 * when stepping up and then down.
866 while (vdd_last > 0 &&
867 vdd_last < vdd_target) {
868 vdd_current += IR_VDD_STEP_UP;
869 vdd_last = set_voltage(i2caddress, vdd_current);
871 while (vdd_last > 0 &&
872 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
873 vdd_current -= IR_VDD_STEP_DOWN;
874 vdd_last = set_voltage(i2caddress, vdd_current);
878 printf("VID: Core voltage after adjustment is at %d mV\n",
886 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
892 static int print_vdd(void)
894 int vdd_last, ret, i2caddress;
896 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
898 debug("VID : I2c failed to switch channel\n");
901 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
902 defined(CONFIG_VOL_MONITOR_IR36021_READ)
903 ret = find_ir_chip_on_i2c();
905 printf("VID: Could not find voltage regulator on I2C.\n");
909 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
914 * Read voltage monitor to check real voltage.
916 vdd_last = read_voltage(i2caddress);
918 printf("VID: Couldn't read sensor abort VID adjustment\n");
921 printf("VID: Core voltage is at %d mV\n", vdd_last);
923 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
925 return ret < 0 ? -1 : 0;
929 static int do_vdd_override(struct cmd_tbl *cmdtp,
936 return CMD_RET_USAGE;
938 if (!strict_strtoul(argv[1], 10, &override))
939 adjust_vdd(override); /* the value is checked by callee */
941 return CMD_RET_USAGE;
945 static int do_vdd_read(struct cmd_tbl *cmdtp, int flag, int argc,
949 return CMD_RET_USAGE;
956 vdd_override, 2, 0, do_vdd_override,
958 " - override with the voltage specified in mV, eg. 1050"
962 vdd_read, 1, 0, do_vdd_read,
964 " - Read the voltage specified in mV"