1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Freescale Semiconductor, Inc.
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 int __weak i2c_multiplexer_select_vid_channel(u8 channel)
26 * Compensate for a board specific voltage drop between regulator and SoC
27 * return a value in mV
29 int __weak board_vdd_drop_compensation(void)
35 * Board specific settings for specific voltage value
37 int __weak board_adjust_vdd(int vdd)
42 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
43 defined(CONFIG_VOL_MONITOR_IR36021_READ)
45 * Get the i2c address configuration for the IR regulator chip
47 * There are some variance in the RDB HW regarding the I2C address configuration
48 * for the IR regulator chip, which is likely a problem of external resistor
49 * accuracy. So we just check each address in a hopefully non-intrusive mode
50 * and use the first one that seems to work
52 * The IR chip can show up under the following addresses:
53 * 0x08 (Verified on T1040RDB-PA,T4240RDB-PB,X-T4240RDB-16GPA)
54 * 0x09 (Verified on T1040RDB-PA)
55 * 0x38 (Verified on T2080QDS, T2081QDS, T4240RDB)
57 static int find_ir_chip_on_i2c(void)
63 const int ir_i2c_addr[] = {0x38, 0x08, 0x09};
68 /* Check all the address */
69 for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) {
70 i2caddress = ir_i2c_addr[i];
72 ret = i2c_read(i2caddress,
73 IR36021_MFR_ID_OFFSET, 1, (void *)&byte,
76 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
78 ret = dm_i2c_read(dev, IR36021_MFR_ID_OFFSET,
79 (void *)&byte, sizeof(byte));
81 if ((ret >= 0) && (byte == IR36021_MFR_ID))
88 /* Maximum loop count waiting for new voltage to take effect */
89 #define MAX_LOOP_WAIT_NEW_VOL 100
90 /* Maximum loop count waiting for the voltage to be stable */
91 #define MAX_LOOP_WAIT_VOL_STABLE 100
93 * read_voltage from sensor on I2C bus
94 * We use average of 4 readings, waiting for WAIT_FOR_ADC before
97 #define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
99 /* If an INA220 chip is available, we can use it to read back the voltage
100 * as it may have a higher accuracy than the IR chip for the same purpose
102 #ifdef CONFIG_VOL_MONITOR_INA220
103 #define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
104 #define ADC_MIN_ACCURACY 4
106 #define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
107 #define ADC_MIN_ACCURACY 4
110 #ifdef CONFIG_VOL_MONITOR_INA220
111 static int read_voltage_from_INA220(int i2caddress)
113 int i, ret, voltage_read = 0;
120 for (i = 0; i < NUM_READINGS; i++) {
121 #ifndef CONFIG_DM_I2C
122 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
123 I2C_VOL_MONITOR_BUS_V_OFFSET, 1,
126 ret = i2c_get_chip_for_busnum(0, I2C_VOL_MONITOR_ADDR, 1, &dev);
128 ret = dm_i2c_read(dev, I2C_VOL_MONITOR_BUS_V_OFFSET,
132 printf("VID: failed to read core voltage\n");
135 vol_mon = (buf[0] << 8) | buf[1];
136 if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
137 printf("VID: Core voltage sensor error\n");
140 debug("VID: bus voltage reads 0x%04x\n", vol_mon);
142 voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
143 udelay(WAIT_FOR_ADC);
145 /* calculate the average */
146 voltage_read /= NUM_READINGS;
152 /* read voltage from IR */
153 #ifdef CONFIG_VOL_MONITOR_IR36021_READ
154 static int read_voltage_from_IR(int i2caddress)
156 int i, ret, voltage_read = 0;
163 for (i = 0; i < NUM_READINGS; i++) {
164 #ifndef CONFIG_DM_I2C
165 ret = i2c_read(i2caddress,
166 IR36021_LOOP1_VOUT_OFFSET,
169 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
171 ret = dm_i2c_read(dev, IR36021_LOOP1_VOUT_OFFSET,
175 printf("VID: failed to read vcpu\n");
180 printf("VID: Core voltage sensor error\n");
183 debug("VID: bus voltage reads 0x%02x\n", vol_mon);
184 /* Resolution is 1/128V. We scale up here to get 1/128mV
185 * and divide at the end
187 voltage_read += vol_mon * 1000;
188 udelay(WAIT_FOR_ADC);
190 /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
191 voltage_read = DIV_ROUND_UP(voltage_read, 128);
193 /* calculate the average */
194 voltage_read /= NUM_READINGS;
196 /* Compensate for a board specific voltage drop between regulator and
197 * SoC before converting into an IR VID value
199 voltage_read -= board_vdd_drop_compensation();
205 #ifdef CONFIG_VOL_MONITOR_LTC3882_READ
206 /* read the current value of the LTC Regulator Voltage */
207 static int read_voltage_from_LTC(int i2caddress)
210 u8 chan = PWM_CHANNEL0;
212 #ifndef CONFIG_DM_I2C
213 /* select the PAGE 0 using PMBus commands PAGE for VDD*/
214 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
215 PMBUS_CMD_PAGE, 1, &chan, 1);
219 ret = i2c_get_chip_for_busnum(0, I2C_VOL_MONITOR_ADDR, 1, &dev);
221 ret = dm_i2c_write(dev, PMBUS_CMD_PAGE, &chan, 1);
224 printf("VID: failed to select VDD Page 0\n");
228 #ifndef CONFIG_DM_I2C
229 /*read the output voltage using PMBus command READ_VOUT*/
230 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
231 PMBUS_CMD_READ_VOUT, 1, (void *)&vcode, 2);
233 ret = dm_i2c_read(dev, PMBUS_CMD_READ_VOUT, (void *)&vcode, 2);
235 printf("VID: failed to read the volatge\n");
240 printf("VID: failed to read the volatge\n");
244 /* Scale down to the real mV as LTC resolution is 1/4096V,rounding up */
245 vcode = DIV_ROUND_UP(vcode * 1000, 4096);
251 static int read_voltage(int i2caddress)
254 #ifdef CONFIG_VOL_MONITOR_INA220
255 voltage_read = read_voltage_from_INA220(i2caddress);
256 #elif defined CONFIG_VOL_MONITOR_IR36021_READ
257 voltage_read = read_voltage_from_IR(i2caddress);
258 #elif defined CONFIG_VOL_MONITOR_LTC3882_READ
259 voltage_read = read_voltage_from_LTC(i2caddress);
266 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
268 * We need to calculate how long before the voltage stops to drop
269 * or increase. It returns with the loop count. Each loop takes
270 * several readings (WAIT_FOR_ADC)
272 static int wait_for_new_voltage(int vdd, int i2caddress)
274 int timeout, vdd_current;
276 vdd_current = read_voltage(i2caddress);
277 /* wait until voltage starts to reach the target. Voltage slew
278 * rates by typical regulators will always lead to stable readings
279 * within each fairly long ADC interval in comparison to the
280 * intended voltage delta change until the target voltage is
281 * reached. The fairly small voltage delta change to any target
282 * VID voltage also means that this function will always complete
283 * within few iterations. If the timeout was ever reached, it would
284 * point to a serious failure in the regulator system.
287 abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
288 timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
289 vdd_current = read_voltage(i2caddress);
291 if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
292 printf("VID: Voltage adjustment timeout\n");
299 * this function keeps reading the voltage until it is stable or until the
302 static int wait_for_voltage_stable(int i2caddress)
304 int timeout, vdd_current, vdd;
306 vdd = read_voltage(i2caddress);
307 udelay(NUM_READINGS * WAIT_FOR_ADC);
309 /* wait until voltage is stable */
310 vdd_current = read_voltage(i2caddress);
311 /* The maximum timeout is
312 * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
314 for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
315 abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
316 timeout > 0; timeout--) {
318 udelay(NUM_READINGS * WAIT_FOR_ADC);
319 vdd_current = read_voltage(i2caddress);
326 /* Set the voltage to the IR chip */
327 static int set_voltage_to_IR(int i2caddress, int vdd)
333 /* Compensate for a board specific voltage drop between regulator and
334 * SoC before converting into an IR VID value
336 vdd += board_vdd_drop_compensation();
337 #ifdef CONFIG_FSL_LSCH2
338 vid = DIV_ROUND_UP(vdd - 265, 5);
340 vid = DIV_ROUND_UP(vdd - 245, 5);
343 #ifndef CONFIG_DM_I2C
344 ret = i2c_write(i2caddress, IR36021_LOOP1_MANUAL_ID_OFFSET,
345 1, (void *)&vid, sizeof(vid));
349 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
351 ret = dm_i2c_write(dev, IR36021_LOOP1_MANUAL_ID_OFFSET,
352 (void *)&vid, sizeof(vid));
356 printf("VID: failed to write VID\n");
359 wait = wait_for_new_voltage(vdd, i2caddress);
362 debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
364 vdd_last = wait_for_voltage_stable(i2caddress);
367 debug("VID: Current voltage is %d mV\n", vdd_last);
373 #ifdef CONFIG_VOL_MONITOR_LTC3882_SET
374 /* this function sets the VDD and returns the value set */
375 static int set_voltage_to_LTC(int i2caddress, int vdd)
377 int ret, vdd_last, vdd_target = vdd;
378 int count = 100, temp = 0;
380 /* Scale up to the LTC resolution is 1/4096V */
381 vdd = (vdd * 4096) / 1000;
383 /* 5-byte buffer which needs to be sent following the
384 * PMBus command PAGE_PLUS_WRITE.
386 u8 buff[5] = {0x04, PWM_CHANNEL0, PMBUS_CMD_VOUT_COMMAND,
387 vdd & 0xFF, (vdd & 0xFF00) >> 8};
389 /* Write the desired voltage code to the regulator */
390 #ifndef CONFIG_DM_I2C
391 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
392 PMBUS_CMD_PAGE_PLUS_WRITE, 1, (void *)&buff, 5);
396 ret = i2c_get_chip_for_busnum(0, I2C_VOL_MONITOR_ADDR, 1, &dev);
398 ret = dm_i2c_write(dev, PMBUS_CMD_PAGE_PLUS_WRITE,
402 printf("VID: I2C failed to write to the volatge regulator\n");
406 /* Wait for the volatge to get to the desired value */
408 vdd_last = read_voltage_from_LTC(i2caddress);
410 printf("VID: Couldn't read sensor abort VID adjust\n");
414 temp = vdd_last - vdd_target;
415 } while ((abs(temp) > 2) && (count > 0));
421 static int set_voltage(int i2caddress, int vdd)
425 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
426 vdd_last = set_voltage_to_IR(i2caddress, vdd);
427 #elif defined CONFIG_VOL_MONITOR_LTC3882_SET
428 vdd_last = set_voltage_to_LTC(i2caddress, vdd);
430 #error Specific voltage monitor must be defined
435 #ifdef CONFIG_FSL_LSCH3
436 int adjust_vdd(ulong vdd_override)
438 int re_enable = disable_interrupts();
439 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
441 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
442 defined(CONFIG_VOL_MONITOR_IR36021_READ)
447 int vdd_target, vdd_current, vdd_last;
449 unsigned long vdd_string_override;
451 #ifdef CONFIG_ARCH_LX2160A
452 static const u16 vdd[32] = {
487 #ifdef CONFIG_ARCH_LS1088A
488 static const uint16_t vdd[32] = {
524 static const uint16_t vdd[32] = {
565 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
567 debug("VID: I2C failed to switch channel\n");
571 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
572 defined(CONFIG_VOL_MONITOR_IR36021_READ)
573 ret = find_ir_chip_on_i2c();
575 printf("VID: Could not find voltage regulator on I2C.\n");
580 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
583 /* check IR chip work on Intel mode*/
584 #ifndef CONFIG_DM_I2C
585 ret = i2c_read(i2caddress,
586 IR36021_INTEL_MODE_OOFSET,
591 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
593 ret = dm_i2c_read(dev, IR36021_INTEL_MODE_OOFSET,
597 printf("VID: failed to read IR chip mode.\n");
602 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
603 printf("VID: IR Chip is not used in Intel mode.\n");
609 /* get the voltage ID from fuse status register */
610 fusesr = in_le32(&gur->dcfg_fusesr);
611 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
612 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
613 if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
614 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
615 FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
617 vdd_target = vdd[vid];
619 /* check override variable for overriding VDD */
620 vdd_string = env_get(CONFIG_VID_FLS_ENV);
621 if (vdd_override == 0 && vdd_string &&
622 !strict_strtoul(vdd_string, 10, &vdd_string_override))
623 vdd_override = vdd_string_override;
625 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
626 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
627 debug("VDD override is %lu\n", vdd_override);
628 } else if (vdd_override != 0) {
629 printf("Invalid value.\n");
632 /* divide and round up by 10 to get a value in mV */
633 vdd_target = DIV_ROUND_UP(vdd_target, 10);
634 if (vdd_target == 0) {
635 debug("VID: VID not used\n");
638 } else if (vdd_target < VDD_MV_MIN || vdd_target > VDD_MV_MAX) {
639 /* Check vdd_target is in valid range */
640 printf("VID: Target VID %d mV is not in range.\n",
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 #ifdef CONFIG_VOL_MONITOR_LTC3882_SET
661 /* Set the target voltage */
662 vdd_last = vdd_current = set_voltage(i2caddress, vdd_target);
665 * Adjust voltage to at or one step above target.
666 * As measurements are less precise than setting the values
667 * we may run through dummy steps that cancel each other
668 * when stepping up and then down.
670 while (vdd_last > 0 &&
671 vdd_last < vdd_target) {
672 vdd_current += IR_VDD_STEP_UP;
673 vdd_last = set_voltage(i2caddress, vdd_current);
675 while (vdd_last > 0 &&
676 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
677 vdd_current -= IR_VDD_STEP_DOWN;
678 vdd_last = set_voltage(i2caddress, vdd_current);
682 if (board_adjust_vdd(vdd_target) < 0) {
688 printf("VID: Core voltage after adjustment is at %d mV\n",
695 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
698 #else /* !CONFIG_FSL_LSCH3 */
699 int adjust_vdd(ulong vdd_override)
701 int re_enable = disable_interrupts();
702 #if defined(CONFIG_FSL_LSCH2)
703 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
705 ccsr_gur_t __iomem *gur =
706 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
710 int vdd_target, vdd_current, vdd_last;
712 unsigned long vdd_string_override;
714 static const uint16_t vdd[32] = {
747 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
749 debug("VID: I2C failed to switch channel\n");
753 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
754 defined(CONFIG_VOL_MONITOR_IR36021_READ)
755 ret = find_ir_chip_on_i2c();
757 printf("VID: Could not find voltage regulator on I2C.\n");
762 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
765 /* check IR chip work on Intel mode*/
766 #ifndef CONFIG_DM_I2C
767 ret = i2c_read(i2caddress,
768 IR36021_INTEL_MODE_OOFSET,
773 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
775 ret = dm_i2c_read(dev, IR36021_INTEL_MODE_OOFSET,
779 printf("VID: failed to read IR chip mode.\n");
783 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
784 printf("VID: IR Chip is not used in Intel mode.\n");
790 /* get the voltage ID from fuse status register */
791 fusesr = in_be32(&gur->dcfg_fusesr);
793 * VID is used according to the table below
794 * ---------------------------------------
796 * |-------------------------------------|
797 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
798 * ---------------+---------+-----------------+---------|
799 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
800 * | A |----------+---------+-----------------+---------|
801 * | _ | 5b00001 |VID = | VID = |VID = |
802 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
803 * | _ | 5b11110 | | | |
804 * | A |----------+---------+-----------------+---------|
805 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
807 * ------------------------------------------------------
809 #ifdef CONFIG_FSL_LSCH2
810 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
811 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
812 if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
813 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
814 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
817 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
818 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
819 if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
820 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
821 FSL_CORENET_DCFG_FUSESR_VID_MASK;
824 vdd_target = vdd[vid];
826 /* check override variable for overriding VDD */
827 vdd_string = env_get(CONFIG_VID_FLS_ENV);
828 if (vdd_override == 0 && vdd_string &&
829 !strict_strtoul(vdd_string, 10, &vdd_string_override))
830 vdd_override = vdd_string_override;
831 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
832 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
833 debug("VDD override is %lu\n", vdd_override);
834 } else if (vdd_override != 0) {
835 printf("Invalid value.\n");
837 if (vdd_target == 0) {
838 debug("VID: VID not used\n");
842 /* divide and round up by 10 to get a value in mV */
843 vdd_target = DIV_ROUND_UP(vdd_target, 10);
844 debug("VID: vid = %d mV\n", vdd_target);
848 * Read voltage monitor to check real voltage.
850 vdd_last = read_voltage(i2caddress);
852 printf("VID: Couldn't read sensor abort VID adjustment\n");
856 vdd_current = vdd_last;
857 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
859 * Adjust voltage to at or one step above target.
860 * As measurements are less precise than setting the values
861 * we may run through dummy steps that cancel each other
862 * when stepping up and then down.
864 while (vdd_last > 0 &&
865 vdd_last < vdd_target) {
866 vdd_current += IR_VDD_STEP_UP;
867 vdd_last = set_voltage(i2caddress, vdd_current);
869 while (vdd_last > 0 &&
870 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
871 vdd_current -= IR_VDD_STEP_DOWN;
872 vdd_last = set_voltage(i2caddress, vdd_current);
876 printf("VID: Core voltage after adjustment is at %d mV\n",
884 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
890 static int print_vdd(void)
892 int vdd_last, ret, i2caddress;
894 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
896 debug("VID : I2c failed to switch channel\n");
899 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
900 defined(CONFIG_VOL_MONITOR_IR36021_READ)
901 ret = find_ir_chip_on_i2c();
903 printf("VID: Could not find voltage regulator on I2C.\n");
907 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
912 * Read voltage monitor to check real voltage.
914 vdd_last = read_voltage(i2caddress);
916 printf("VID: Couldn't read sensor abort VID adjustment\n");
919 printf("VID: Core voltage is at %d mV\n", vdd_last);
921 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
923 return ret < 0 ? -1 : 0;
927 static int do_vdd_override(cmd_tbl_t *cmdtp,
934 return CMD_RET_USAGE;
936 if (!strict_strtoul(argv[1], 10, &override))
937 adjust_vdd(override); /* the value is checked by callee */
939 return CMD_RET_USAGE;
943 static int do_vdd_read(cmd_tbl_t *cmdtp,
948 return CMD_RET_USAGE;
955 vdd_override, 2, 0, do_vdd_override,
957 " - override with the voltage specified in mV, eg. 1050"
961 vdd_read, 1, 0, do_vdd_read,
963 " - Read the voltage specified in mV"