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};
65 /* Check all the address */
66 for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) {
67 i2caddress = ir_i2c_addr[i];
68 ret = i2c_read(i2caddress,
69 IR36021_MFR_ID_OFFSET, 1, (void *)&byte,
71 if ((ret >= 0) && (byte == IR36021_MFR_ID))
78 /* Maximum loop count waiting for new voltage to take effect */
79 #define MAX_LOOP_WAIT_NEW_VOL 100
80 /* Maximum loop count waiting for the voltage to be stable */
81 #define MAX_LOOP_WAIT_VOL_STABLE 100
83 * read_voltage from sensor on I2C bus
84 * We use average of 4 readings, waiting for WAIT_FOR_ADC before
87 #define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
89 /* If an INA220 chip is available, we can use it to read back the voltage
90 * as it may have a higher accuracy than the IR chip for the same purpose
92 #ifdef CONFIG_VOL_MONITOR_INA220
93 #define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
94 #define ADC_MIN_ACCURACY 4
96 #define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
97 #define ADC_MIN_ACCURACY 4
100 #ifdef CONFIG_VOL_MONITOR_INA220
101 static int read_voltage_from_INA220(int i2caddress)
103 int i, ret, voltage_read = 0;
107 for (i = 0; i < NUM_READINGS; i++) {
108 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
109 I2C_VOL_MONITOR_BUS_V_OFFSET, 1,
112 printf("VID: failed to read core voltage\n");
115 vol_mon = (buf[0] << 8) | buf[1];
116 if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
117 printf("VID: Core voltage sensor error\n");
120 debug("VID: bus voltage reads 0x%04x\n", vol_mon);
122 voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
123 udelay(WAIT_FOR_ADC);
125 /* calculate the average */
126 voltage_read /= NUM_READINGS;
132 /* read voltage from IR */
133 #ifdef CONFIG_VOL_MONITOR_IR36021_READ
134 static int read_voltage_from_IR(int i2caddress)
136 int i, ret, voltage_read = 0;
140 for (i = 0; i < NUM_READINGS; i++) {
141 ret = i2c_read(i2caddress,
142 IR36021_LOOP1_VOUT_OFFSET,
145 printf("VID: failed to read vcpu\n");
150 printf("VID: Core voltage sensor error\n");
153 debug("VID: bus voltage reads 0x%02x\n", vol_mon);
154 /* Resolution is 1/128V. We scale up here to get 1/128mV
155 * and divide at the end
157 voltage_read += vol_mon * 1000;
158 udelay(WAIT_FOR_ADC);
160 /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
161 voltage_read = DIV_ROUND_UP(voltage_read, 128);
163 /* calculate the average */
164 voltage_read /= NUM_READINGS;
166 /* Compensate for a board specific voltage drop between regulator and
167 * SoC before converting into an IR VID value
169 voltage_read -= board_vdd_drop_compensation();
175 #ifdef CONFIG_VOL_MONITOR_LTC3882_READ
176 /* read the current value of the LTC Regulator Voltage */
177 static int read_voltage_from_LTC(int i2caddress)
180 u8 chan = PWM_CHANNEL0;
182 /* select the PAGE 0 using PMBus commands PAGE for VDD*/
183 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
184 PMBUS_CMD_PAGE, 1, &chan, 1);
186 printf("VID: failed to select VDD Page 0\n");
190 /*read the output voltage using PMBus command READ_VOUT*/
191 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
192 PMBUS_CMD_READ_VOUT, 1, (void *)&vcode, 2);
194 printf("VID: failed to read the volatge\n");
198 /* Scale down to the real mV as LTC resolution is 1/4096V,rounding up */
199 vcode = DIV_ROUND_UP(vcode * 1000, 4096);
205 static int read_voltage(int i2caddress)
208 #ifdef CONFIG_VOL_MONITOR_INA220
209 voltage_read = read_voltage_from_INA220(i2caddress);
210 #elif defined CONFIG_VOL_MONITOR_IR36021_READ
211 voltage_read = read_voltage_from_IR(i2caddress);
212 #elif defined CONFIG_VOL_MONITOR_LTC3882_READ
213 voltage_read = read_voltage_from_LTC(i2caddress);
220 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
222 * We need to calculate how long before the voltage stops to drop
223 * or increase. It returns with the loop count. Each loop takes
224 * several readings (WAIT_FOR_ADC)
226 static int wait_for_new_voltage(int vdd, int i2caddress)
228 int timeout, vdd_current;
230 vdd_current = read_voltage(i2caddress);
231 /* wait until voltage starts to reach the target. Voltage slew
232 * rates by typical regulators will always lead to stable readings
233 * within each fairly long ADC interval in comparison to the
234 * intended voltage delta change until the target voltage is
235 * reached. The fairly small voltage delta change to any target
236 * VID voltage also means that this function will always complete
237 * within few iterations. If the timeout was ever reached, it would
238 * point to a serious failure in the regulator system.
241 abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
242 timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
243 vdd_current = read_voltage(i2caddress);
245 if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
246 printf("VID: Voltage adjustment timeout\n");
253 * this function keeps reading the voltage until it is stable or until the
256 static int wait_for_voltage_stable(int i2caddress)
258 int timeout, vdd_current, vdd;
260 vdd = read_voltage(i2caddress);
261 udelay(NUM_READINGS * WAIT_FOR_ADC);
263 /* wait until voltage is stable */
264 vdd_current = read_voltage(i2caddress);
265 /* The maximum timeout is
266 * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
268 for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
269 abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
270 timeout > 0; timeout--) {
272 udelay(NUM_READINGS * WAIT_FOR_ADC);
273 vdd_current = read_voltage(i2caddress);
280 /* Set the voltage to the IR chip */
281 static int set_voltage_to_IR(int i2caddress, int vdd)
287 /* Compensate for a board specific voltage drop between regulator and
288 * SoC before converting into an IR VID value
290 vdd += board_vdd_drop_compensation();
291 #ifdef CONFIG_FSL_LSCH2
292 vid = DIV_ROUND_UP(vdd - 265, 5);
294 vid = DIV_ROUND_UP(vdd - 245, 5);
297 ret = i2c_write(i2caddress, IR36021_LOOP1_MANUAL_ID_OFFSET,
298 1, (void *)&vid, sizeof(vid));
300 printf("VID: failed to write VID\n");
303 wait = wait_for_new_voltage(vdd, i2caddress);
306 debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
308 vdd_last = wait_for_voltage_stable(i2caddress);
311 debug("VID: Current voltage is %d mV\n", vdd_last);
317 #ifdef CONFIG_VOL_MONITOR_LTC3882_SET
318 /* this function sets the VDD and returns the value set */
319 static int set_voltage_to_LTC(int i2caddress, int vdd)
321 int ret, vdd_last, vdd_target = vdd;
322 int count = 100, temp = 0;
324 /* Scale up to the LTC resolution is 1/4096V */
325 vdd = (vdd * 4096) / 1000;
327 /* 5-byte buffer which needs to be sent following the
328 * PMBus command PAGE_PLUS_WRITE.
330 u8 buff[5] = {0x04, PWM_CHANNEL0, PMBUS_CMD_VOUT_COMMAND,
331 vdd & 0xFF, (vdd & 0xFF00) >> 8};
333 /* Write the desired voltage code to the regulator */
334 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
335 PMBUS_CMD_PAGE_PLUS_WRITE, 1, (void *)&buff, 5);
337 printf("VID: I2C failed to write to the volatge regulator\n");
341 /* Wait for the volatge to get to the desired value */
343 vdd_last = read_voltage_from_LTC(i2caddress);
345 printf("VID: Couldn't read sensor abort VID adjust\n");
349 temp = vdd_last - vdd_target;
350 } while ((abs(temp) > 2) && (count > 0));
356 static int set_voltage(int i2caddress, int vdd)
360 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
361 vdd_last = set_voltage_to_IR(i2caddress, vdd);
362 #elif defined CONFIG_VOL_MONITOR_LTC3882_SET
363 vdd_last = set_voltage_to_LTC(i2caddress, vdd);
365 #error Specific voltage monitor must be defined
370 #ifdef CONFIG_FSL_LSCH3
371 int adjust_vdd(ulong vdd_override)
373 int re_enable = disable_interrupts();
374 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
376 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
377 defined(CONFIG_VOL_MONITOR_IR36021_READ)
382 int vdd_target, vdd_current, vdd_last;
384 unsigned long vdd_string_override;
386 #ifdef CONFIG_ARCH_LX2160A
387 static const u16 vdd[32] = {
422 #ifdef CONFIG_ARCH_LS1088A
423 static const uint16_t vdd[32] = {
459 static const uint16_t vdd[32] = {
500 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
502 debug("VID: I2C failed to switch channel\n");
506 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
507 defined(CONFIG_VOL_MONITOR_IR36021_READ)
508 ret = find_ir_chip_on_i2c();
510 printf("VID: Could not find voltage regulator on I2C.\n");
515 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
518 /* check IR chip work on Intel mode*/
519 ret = i2c_read(i2caddress,
520 IR36021_INTEL_MODE_OOFSET,
523 printf("VID: failed to read IR chip mode.\n");
527 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
528 printf("VID: IR Chip is not used in Intel mode.\n");
534 /* get the voltage ID from fuse status register */
535 fusesr = in_le32(&gur->dcfg_fusesr);
536 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
537 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
538 if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
539 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
540 FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
542 vdd_target = vdd[vid];
544 /* check override variable for overriding VDD */
545 vdd_string = env_get(CONFIG_VID_FLS_ENV);
546 if (vdd_override == 0 && vdd_string &&
547 !strict_strtoul(vdd_string, 10, &vdd_string_override))
548 vdd_override = vdd_string_override;
550 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
551 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
552 debug("VDD override is %lu\n", vdd_override);
553 } else if (vdd_override != 0) {
554 printf("Invalid value.\n");
557 /* divide and round up by 10 to get a value in mV */
558 vdd_target = DIV_ROUND_UP(vdd_target, 10);
559 if (vdd_target == 0) {
560 debug("VID: VID not used\n");
563 } else if (vdd_target < VDD_MV_MIN || vdd_target > VDD_MV_MAX) {
564 /* Check vdd_target is in valid range */
565 printf("VID: Target VID %d mV is not in range.\n",
570 debug("VID: vid = %d mV\n", vdd_target);
574 * Read voltage monitor to check real voltage.
576 vdd_last = read_voltage(i2caddress);
578 printf("VID: Couldn't read sensor abort VID adjustment\n");
582 vdd_current = vdd_last;
583 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
585 #ifdef CONFIG_VOL_MONITOR_LTC3882_SET
586 /* Set the target voltage */
587 vdd_last = vdd_current = set_voltage(i2caddress, vdd_target);
590 * Adjust voltage to at or one step above target.
591 * As measurements are less precise than setting the values
592 * we may run through dummy steps that cancel each other
593 * when stepping up and then down.
595 while (vdd_last > 0 &&
596 vdd_last < vdd_target) {
597 vdd_current += IR_VDD_STEP_UP;
598 vdd_last = set_voltage(i2caddress, vdd_current);
600 while (vdd_last > 0 &&
601 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
602 vdd_current -= IR_VDD_STEP_DOWN;
603 vdd_last = set_voltage(i2caddress, vdd_current);
607 if (board_adjust_vdd(vdd_target) < 0) {
613 printf("VID: Core voltage after adjustment is at %d mV\n",
620 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
623 #else /* !CONFIG_FSL_LSCH3 */
624 int adjust_vdd(ulong vdd_override)
626 int re_enable = disable_interrupts();
627 #if defined(CONFIG_FSL_LSCH2)
628 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
630 ccsr_gur_t __iomem *gur =
631 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
635 int vdd_target, vdd_current, vdd_last;
637 unsigned long vdd_string_override;
639 static const uint16_t vdd[32] = {
672 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
674 debug("VID: I2C failed to switch channel\n");
678 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
679 defined(CONFIG_VOL_MONITOR_IR36021_READ)
680 ret = find_ir_chip_on_i2c();
682 printf("VID: Could not find voltage regulator on I2C.\n");
687 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
690 /* check IR chip work on Intel mode*/
691 ret = i2c_read(i2caddress,
692 IR36021_INTEL_MODE_OOFSET,
695 printf("VID: failed to read IR chip mode.\n");
699 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
700 printf("VID: IR Chip is not used in Intel mode.\n");
706 /* get the voltage ID from fuse status register */
707 fusesr = in_be32(&gur->dcfg_fusesr);
709 * VID is used according to the table below
710 * ---------------------------------------
712 * |-------------------------------------|
713 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
714 * ---------------+---------+-----------------+---------|
715 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
716 * | A |----------+---------+-----------------+---------|
717 * | _ | 5b00001 |VID = | VID = |VID = |
718 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
719 * | _ | 5b11110 | | | |
720 * | A |----------+---------+-----------------+---------|
721 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
723 * ------------------------------------------------------
725 #ifdef CONFIG_FSL_LSCH2
726 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
727 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
728 if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
729 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
730 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
733 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
734 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
735 if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
736 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
737 FSL_CORENET_DCFG_FUSESR_VID_MASK;
740 vdd_target = vdd[vid];
742 /* check override variable for overriding VDD */
743 vdd_string = env_get(CONFIG_VID_FLS_ENV);
744 if (vdd_override == 0 && vdd_string &&
745 !strict_strtoul(vdd_string, 10, &vdd_string_override))
746 vdd_override = vdd_string_override;
747 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
748 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
749 debug("VDD override is %lu\n", vdd_override);
750 } else if (vdd_override != 0) {
751 printf("Invalid value.\n");
753 if (vdd_target == 0) {
754 debug("VID: VID not used\n");
758 /* divide and round up by 10 to get a value in mV */
759 vdd_target = DIV_ROUND_UP(vdd_target, 10);
760 debug("VID: vid = %d mV\n", vdd_target);
764 * Read voltage monitor to check real voltage.
766 vdd_last = read_voltage(i2caddress);
768 printf("VID: Couldn't read sensor abort VID adjustment\n");
772 vdd_current = vdd_last;
773 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
775 * Adjust voltage to at or one step above target.
776 * As measurements are less precise than setting the values
777 * we may run through dummy steps that cancel each other
778 * when stepping up and then down.
780 while (vdd_last > 0 &&
781 vdd_last < vdd_target) {
782 vdd_current += IR_VDD_STEP_UP;
783 vdd_last = set_voltage(i2caddress, vdd_current);
785 while (vdd_last > 0 &&
786 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
787 vdd_current -= IR_VDD_STEP_DOWN;
788 vdd_last = set_voltage(i2caddress, vdd_current);
792 printf("VID: Core voltage after adjustment is at %d mV\n",
800 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
806 static int print_vdd(void)
808 int vdd_last, ret, i2caddress;
810 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
812 debug("VID : I2c failed to switch channel\n");
815 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
816 defined(CONFIG_VOL_MONITOR_IR36021_READ)
817 ret = find_ir_chip_on_i2c();
819 printf("VID: Could not find voltage regulator on I2C.\n");
823 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
828 * Read voltage monitor to check real voltage.
830 vdd_last = read_voltage(i2caddress);
832 printf("VID: Couldn't read sensor abort VID adjustment\n");
835 printf("VID: Core voltage is at %d mV\n", vdd_last);
837 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
839 return ret < 0 ? -1 : 0;
843 static int do_vdd_override(cmd_tbl_t *cmdtp,
850 return CMD_RET_USAGE;
852 if (!strict_strtoul(argv[1], 10, &override))
853 adjust_vdd(override); /* the value is checked by callee */
855 return CMD_RET_USAGE;
859 static int do_vdd_read(cmd_tbl_t *cmdtp,
864 return CMD_RET_USAGE;
871 vdd_override, 2, 0, do_vdd_override,
873 " - override with the voltage specified in mV, eg. 1050"
877 vdd_read, 1, 0, do_vdd_read,
879 " - Read the voltage specified in mV"