1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Freescale Semiconductor, Inc.
12 #ifdef CONFIG_FSL_LSCH2
13 #include <asm/arch/immap_lsch2.h>
14 #elif defined(CONFIG_FSL_LSCH3)
15 #include <asm/arch/immap_lsch3.h>
17 #include <asm/immap_85xx.h>
21 int __weak i2c_multiplexer_select_vid_channel(u8 channel)
27 * Compensate for a board specific voltage drop between regulator and SoC
28 * return a value in mV
30 int __weak board_vdd_drop_compensation(void)
36 * Board specific settings for specific voltage value
38 int __weak board_adjust_vdd(int vdd)
43 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
44 defined(CONFIG_VOL_MONITOR_IR36021_READ)
46 * Get the i2c address configuration for the IR regulator chip
48 * There are some variance in the RDB HW regarding the I2C address configuration
49 * for the IR regulator chip, which is likely a problem of external resistor
50 * accuracy. So we just check each address in a hopefully non-intrusive mode
51 * and use the first one that seems to work
53 * The IR chip can show up under the following addresses:
54 * 0x08 (Verified on T1040RDB-PA,T4240RDB-PB,X-T4240RDB-16GPA)
55 * 0x09 (Verified on T1040RDB-PA)
56 * 0x38 (Verified on T2080QDS, T2081QDS, T4240RDB)
58 static int find_ir_chip_on_i2c(void)
64 const int ir_i2c_addr[] = {0x38, 0x08, 0x09};
69 /* Check all the address */
70 for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) {
71 i2caddress = ir_i2c_addr[i];
73 ret = i2c_read(i2caddress,
74 IR36021_MFR_ID_OFFSET, 1, (void *)&byte,
77 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
79 ret = dm_i2c_read(dev, IR36021_MFR_ID_OFFSET,
80 (void *)&byte, sizeof(byte));
82 if ((ret >= 0) && (byte == IR36021_MFR_ID))
89 /* Maximum loop count waiting for new voltage to take effect */
90 #define MAX_LOOP_WAIT_NEW_VOL 100
91 /* Maximum loop count waiting for the voltage to be stable */
92 #define MAX_LOOP_WAIT_VOL_STABLE 100
94 * read_voltage from sensor on I2C bus
95 * We use average of 4 readings, waiting for WAIT_FOR_ADC before
98 #define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
100 /* If an INA220 chip is available, we can use it to read back the voltage
101 * as it may have a higher accuracy than the IR chip for the same purpose
103 #ifdef CONFIG_VOL_MONITOR_INA220
104 #define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
105 #define ADC_MIN_ACCURACY 4
107 #define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
108 #define ADC_MIN_ACCURACY 4
111 #ifdef CONFIG_VOL_MONITOR_INA220
112 static int read_voltage_from_INA220(int i2caddress)
114 int i, ret, voltage_read = 0;
121 for (i = 0; i < NUM_READINGS; i++) {
122 #ifndef CONFIG_DM_I2C
123 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
124 I2C_VOL_MONITOR_BUS_V_OFFSET, 1,
127 ret = i2c_get_chip_for_busnum(0, I2C_VOL_MONITOR_ADDR, 1, &dev);
129 ret = dm_i2c_read(dev, I2C_VOL_MONITOR_BUS_V_OFFSET,
133 printf("VID: failed to read core voltage\n");
136 vol_mon = (buf[0] << 8) | buf[1];
137 if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
138 printf("VID: Core voltage sensor error\n");
141 debug("VID: bus voltage reads 0x%04x\n", vol_mon);
143 voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
144 udelay(WAIT_FOR_ADC);
146 /* calculate the average */
147 voltage_read /= NUM_READINGS;
153 /* read voltage from IR */
154 #ifdef CONFIG_VOL_MONITOR_IR36021_READ
155 static int read_voltage_from_IR(int i2caddress)
157 int i, ret, voltage_read = 0;
164 for (i = 0; i < NUM_READINGS; i++) {
165 #ifndef CONFIG_DM_I2C
166 ret = i2c_read(i2caddress,
167 IR36021_LOOP1_VOUT_OFFSET,
170 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
172 ret = dm_i2c_read(dev, IR36021_LOOP1_VOUT_OFFSET,
176 printf("VID: failed to read vcpu\n");
181 printf("VID: Core voltage sensor error\n");
184 debug("VID: bus voltage reads 0x%02x\n", vol_mon);
185 /* Resolution is 1/128V. We scale up here to get 1/128mV
186 * and divide at the end
188 voltage_read += vol_mon * 1000;
189 udelay(WAIT_FOR_ADC);
191 /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
192 voltage_read = DIV_ROUND_UP(voltage_read, 128);
194 /* calculate the average */
195 voltage_read /= NUM_READINGS;
197 /* Compensate for a board specific voltage drop between regulator and
198 * SoC before converting into an IR VID value
200 voltage_read -= board_vdd_drop_compensation();
206 #ifdef CONFIG_VOL_MONITOR_LTC3882_READ
207 /* read the current value of the LTC Regulator Voltage */
208 static int read_voltage_from_LTC(int i2caddress)
211 u8 chan = PWM_CHANNEL0;
213 #ifndef CONFIG_DM_I2C
214 /* select the PAGE 0 using PMBus commands PAGE for VDD*/
215 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
216 PMBUS_CMD_PAGE, 1, &chan, 1);
220 ret = i2c_get_chip_for_busnum(0, I2C_VOL_MONITOR_ADDR, 1, &dev);
222 ret = dm_i2c_write(dev, PMBUS_CMD_PAGE, &chan, 1);
225 printf("VID: failed to select VDD Page 0\n");
229 #ifndef CONFIG_DM_I2C
230 /*read the output voltage using PMBus command READ_VOUT*/
231 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
232 PMBUS_CMD_READ_VOUT, 1, (void *)&vcode, 2);
234 ret = dm_i2c_read(dev, PMBUS_CMD_READ_VOUT, (void *)&vcode, 2);
236 printf("VID: failed to read the volatge\n");
241 printf("VID: failed to read the volatge\n");
245 /* Scale down to the real mV as LTC resolution is 1/4096V,rounding up */
246 vcode = DIV_ROUND_UP(vcode * 1000, 4096);
252 static int read_voltage(int i2caddress)
255 #ifdef CONFIG_VOL_MONITOR_INA220
256 voltage_read = read_voltage_from_INA220(i2caddress);
257 #elif defined CONFIG_VOL_MONITOR_IR36021_READ
258 voltage_read = read_voltage_from_IR(i2caddress);
259 #elif defined CONFIG_VOL_MONITOR_LTC3882_READ
260 voltage_read = read_voltage_from_LTC(i2caddress);
267 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
269 * We need to calculate how long before the voltage stops to drop
270 * or increase. It returns with the loop count. Each loop takes
271 * several readings (WAIT_FOR_ADC)
273 static int wait_for_new_voltage(int vdd, int i2caddress)
275 int timeout, vdd_current;
277 vdd_current = read_voltage(i2caddress);
278 /* wait until voltage starts to reach the target. Voltage slew
279 * rates by typical regulators will always lead to stable readings
280 * within each fairly long ADC interval in comparison to the
281 * intended voltage delta change until the target voltage is
282 * reached. The fairly small voltage delta change to any target
283 * VID voltage also means that this function will always complete
284 * within few iterations. If the timeout was ever reached, it would
285 * point to a serious failure in the regulator system.
288 abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
289 timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
290 vdd_current = read_voltage(i2caddress);
292 if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
293 printf("VID: Voltage adjustment timeout\n");
300 * this function keeps reading the voltage until it is stable or until the
303 static int wait_for_voltage_stable(int i2caddress)
305 int timeout, vdd_current, vdd;
307 vdd = read_voltage(i2caddress);
308 udelay(NUM_READINGS * WAIT_FOR_ADC);
310 /* wait until voltage is stable */
311 vdd_current = read_voltage(i2caddress);
312 /* The maximum timeout is
313 * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
315 for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
316 abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
317 timeout > 0; timeout--) {
319 udelay(NUM_READINGS * WAIT_FOR_ADC);
320 vdd_current = read_voltage(i2caddress);
327 /* Set the voltage to the IR chip */
328 static int set_voltage_to_IR(int i2caddress, int vdd)
334 /* Compensate for a board specific voltage drop between regulator and
335 * SoC before converting into an IR VID value
337 vdd += board_vdd_drop_compensation();
338 #ifdef CONFIG_FSL_LSCH2
339 vid = DIV_ROUND_UP(vdd - 265, 5);
341 vid = DIV_ROUND_UP(vdd - 245, 5);
344 #ifndef CONFIG_DM_I2C
345 ret = i2c_write(i2caddress, IR36021_LOOP1_MANUAL_ID_OFFSET,
346 1, (void *)&vid, sizeof(vid));
350 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
352 ret = dm_i2c_write(dev, IR36021_LOOP1_MANUAL_ID_OFFSET,
353 (void *)&vid, sizeof(vid));
357 printf("VID: failed to write VID\n");
360 wait = wait_for_new_voltage(vdd, i2caddress);
363 debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
365 vdd_last = wait_for_voltage_stable(i2caddress);
368 debug("VID: Current voltage is %d mV\n", vdd_last);
374 #ifdef CONFIG_VOL_MONITOR_LTC3882_SET
375 /* this function sets the VDD and returns the value set */
376 static int set_voltage_to_LTC(int i2caddress, int vdd)
378 int ret, vdd_last, vdd_target = vdd;
379 int count = 100, temp = 0;
381 /* Scale up to the LTC resolution is 1/4096V */
382 vdd = (vdd * 4096) / 1000;
384 /* 5-byte buffer which needs to be sent following the
385 * PMBus command PAGE_PLUS_WRITE.
387 u8 buff[5] = {0x04, PWM_CHANNEL0, PMBUS_CMD_VOUT_COMMAND,
388 vdd & 0xFF, (vdd & 0xFF00) >> 8};
390 /* Write the desired voltage code to the regulator */
391 #ifndef CONFIG_DM_I2C
392 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
393 PMBUS_CMD_PAGE_PLUS_WRITE, 1, (void *)&buff, 5);
397 ret = i2c_get_chip_for_busnum(0, I2C_VOL_MONITOR_ADDR, 1, &dev);
399 ret = dm_i2c_write(dev, PMBUS_CMD_PAGE_PLUS_WRITE,
403 printf("VID: I2C failed to write to the volatge regulator\n");
407 /* Wait for the volatge to get to the desired value */
409 vdd_last = read_voltage_from_LTC(i2caddress);
411 printf("VID: Couldn't read sensor abort VID adjust\n");
415 temp = vdd_last - vdd_target;
416 } while ((abs(temp) > 2) && (count > 0));
422 static int set_voltage(int i2caddress, int vdd)
426 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
427 vdd_last = set_voltage_to_IR(i2caddress, vdd);
428 #elif defined CONFIG_VOL_MONITOR_LTC3882_SET
429 vdd_last = set_voltage_to_LTC(i2caddress, vdd);
431 #error Specific voltage monitor must be defined
436 #ifdef CONFIG_FSL_LSCH3
437 int adjust_vdd(ulong vdd_override)
439 int re_enable = disable_interrupts();
440 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
442 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
443 defined(CONFIG_VOL_MONITOR_IR36021_READ)
448 int vdd_target, vdd_current, vdd_last;
450 unsigned long vdd_string_override;
452 #ifdef CONFIG_ARCH_LX2160A
453 static const u16 vdd[32] = {
488 #ifdef CONFIG_ARCH_LS1088A
489 static const uint16_t vdd[32] = {
525 static const uint16_t vdd[32] = {
566 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
568 debug("VID: I2C failed to switch channel\n");
572 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
573 defined(CONFIG_VOL_MONITOR_IR36021_READ)
574 ret = find_ir_chip_on_i2c();
576 printf("VID: Could not find voltage regulator on I2C.\n");
581 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
584 /* check IR chip work on Intel mode*/
585 #ifndef CONFIG_DM_I2C
586 ret = i2c_read(i2caddress,
587 IR36021_INTEL_MODE_OOFSET,
592 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
594 ret = dm_i2c_read(dev, IR36021_INTEL_MODE_OOFSET,
598 printf("VID: failed to read IR chip mode.\n");
603 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
604 printf("VID: IR Chip is not used in Intel mode.\n");
610 /* get the voltage ID from fuse status register */
611 fusesr = in_le32(&gur->dcfg_fusesr);
612 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
613 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
614 if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
615 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
616 FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
618 vdd_target = vdd[vid];
620 /* check override variable for overriding VDD */
621 vdd_string = env_get(CONFIG_VID_FLS_ENV);
622 if (vdd_override == 0 && vdd_string &&
623 !strict_strtoul(vdd_string, 10, &vdd_string_override))
624 vdd_override = vdd_string_override;
626 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
627 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
628 debug("VDD override is %lu\n", vdd_override);
629 } else if (vdd_override != 0) {
630 printf("Invalid value.\n");
633 /* divide and round up by 10 to get a value in mV */
634 vdd_target = DIV_ROUND_UP(vdd_target, 10);
635 if (vdd_target == 0) {
636 debug("VID: VID not used\n");
639 } else if (vdd_target < VDD_MV_MIN || vdd_target > VDD_MV_MAX) {
640 /* Check vdd_target is in valid range */
641 printf("VID: Target VID %d mV is not in range.\n",
646 debug("VID: vid = %d mV\n", vdd_target);
650 * Read voltage monitor to check real voltage.
652 vdd_last = read_voltage(i2caddress);
654 printf("VID: Couldn't read sensor abort VID adjustment\n");
658 vdd_current = vdd_last;
659 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
661 #ifdef CONFIG_VOL_MONITOR_LTC3882_SET
662 /* Set the target voltage */
663 vdd_last = vdd_current = set_voltage(i2caddress, vdd_target);
666 * Adjust voltage to at or one step above target.
667 * As measurements are less precise than setting the values
668 * we may run through dummy steps that cancel each other
669 * when stepping up and then down.
671 while (vdd_last > 0 &&
672 vdd_last < vdd_target) {
673 vdd_current += IR_VDD_STEP_UP;
674 vdd_last = set_voltage(i2caddress, vdd_current);
676 while (vdd_last > 0 &&
677 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
678 vdd_current -= IR_VDD_STEP_DOWN;
679 vdd_last = set_voltage(i2caddress, vdd_current);
683 if (board_adjust_vdd(vdd_target) < 0) {
689 printf("VID: Core voltage after adjustment is at %d mV\n",
696 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
699 #else /* !CONFIG_FSL_LSCH3 */
700 int adjust_vdd(ulong vdd_override)
702 int re_enable = disable_interrupts();
703 #if defined(CONFIG_FSL_LSCH2)
704 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
706 ccsr_gur_t __iomem *gur =
707 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
711 int vdd_target, vdd_current, vdd_last;
713 unsigned long vdd_string_override;
715 static const uint16_t vdd[32] = {
748 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
750 debug("VID: I2C failed to switch channel\n");
754 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
755 defined(CONFIG_VOL_MONITOR_IR36021_READ)
756 ret = find_ir_chip_on_i2c();
758 printf("VID: Could not find voltage regulator on I2C.\n");
763 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
766 /* check IR chip work on Intel mode*/
767 #ifndef CONFIG_DM_I2C
768 ret = i2c_read(i2caddress,
769 IR36021_INTEL_MODE_OOFSET,
774 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
776 ret = dm_i2c_read(dev, IR36021_INTEL_MODE_OOFSET,
780 printf("VID: failed to read IR chip mode.\n");
784 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
785 printf("VID: IR Chip is not used in Intel mode.\n");
791 /* get the voltage ID from fuse status register */
792 fusesr = in_be32(&gur->dcfg_fusesr);
794 * VID is used according to the table below
795 * ---------------------------------------
797 * |-------------------------------------|
798 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
799 * ---------------+---------+-----------------+---------|
800 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
801 * | A |----------+---------+-----------------+---------|
802 * | _ | 5b00001 |VID = | VID = |VID = |
803 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
804 * | _ | 5b11110 | | | |
805 * | A |----------+---------+-----------------+---------|
806 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
808 * ------------------------------------------------------
810 #ifdef CONFIG_FSL_LSCH2
811 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
812 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
813 if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
814 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
815 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
818 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
819 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
820 if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
821 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
822 FSL_CORENET_DCFG_FUSESR_VID_MASK;
825 vdd_target = vdd[vid];
827 /* check override variable for overriding VDD */
828 vdd_string = env_get(CONFIG_VID_FLS_ENV);
829 if (vdd_override == 0 && vdd_string &&
830 !strict_strtoul(vdd_string, 10, &vdd_string_override))
831 vdd_override = vdd_string_override;
832 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
833 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
834 debug("VDD override is %lu\n", vdd_override);
835 } else if (vdd_override != 0) {
836 printf("Invalid value.\n");
838 if (vdd_target == 0) {
839 debug("VID: VID not used\n");
843 /* divide and round up by 10 to get a value in mV */
844 vdd_target = DIV_ROUND_UP(vdd_target, 10);
845 debug("VID: vid = %d mV\n", vdd_target);
849 * Read voltage monitor to check real voltage.
851 vdd_last = read_voltage(i2caddress);
853 printf("VID: Couldn't read sensor abort VID adjustment\n");
857 vdd_current = vdd_last;
858 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
860 * Adjust voltage to at or one step above target.
861 * As measurements are less precise than setting the values
862 * we may run through dummy steps that cancel each other
863 * when stepping up and then down.
865 while (vdd_last > 0 &&
866 vdd_last < vdd_target) {
867 vdd_current += IR_VDD_STEP_UP;
868 vdd_last = set_voltage(i2caddress, vdd_current);
870 while (vdd_last > 0 &&
871 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
872 vdd_current -= IR_VDD_STEP_DOWN;
873 vdd_last = set_voltage(i2caddress, vdd_current);
877 printf("VID: Core voltage after adjustment is at %d mV\n",
885 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
891 static int print_vdd(void)
893 int vdd_last, ret, i2caddress;
895 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
897 debug("VID : I2c failed to switch channel\n");
900 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
901 defined(CONFIG_VOL_MONITOR_IR36021_READ)
902 ret = find_ir_chip_on_i2c();
904 printf("VID: Could not find voltage regulator on I2C.\n");
908 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
913 * Read voltage monitor to check real voltage.
915 vdd_last = read_voltage(i2caddress);
917 printf("VID: Couldn't read sensor abort VID adjustment\n");
920 printf("VID: Core voltage is at %d mV\n", vdd_last);
922 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
924 return ret < 0 ? -1 : 0;
928 static int do_vdd_override(cmd_tbl_t *cmdtp,
935 return CMD_RET_USAGE;
937 if (!strict_strtoul(argv[1], 10, &override))
938 adjust_vdd(override); /* the value is checked by callee */
940 return CMD_RET_USAGE;
944 static int do_vdd_read(cmd_tbl_t *cmdtp,
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"