1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Freescale Semiconductor, Inc.
10 #ifdef CONFIG_FSL_LSCH2
11 #include <asm/arch/immap_lsch2.h>
12 #elif defined(CONFIG_FSL_LSCH3)
13 #include <asm/arch/immap_lsch3.h>
15 #include <asm/immap_85xx.h>
19 int __weak i2c_multiplexer_select_vid_channel(u8 channel)
25 * Compensate for a board specific voltage drop between regulator and SoC
26 * return a value in mV
28 int __weak board_vdd_drop_compensation(void)
34 * Board specific settings for specific voltage value
36 int __weak board_adjust_vdd(int vdd)
41 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
42 defined(CONFIG_VOL_MONITOR_IR36021_READ)
44 * Get the i2c address configuration for the IR regulator chip
46 * There are some variance in the RDB HW regarding the I2C address configuration
47 * for the IR regulator chip, which is likely a problem of external resistor
48 * accuracy. So we just check each address in a hopefully non-intrusive mode
49 * and use the first one that seems to work
51 * The IR chip can show up under the following addresses:
52 * 0x08 (Verified on T1040RDB-PA,T4240RDB-PB,X-T4240RDB-16GPA)
53 * 0x09 (Verified on T1040RDB-PA)
54 * 0x38 (Verified on T2080QDS, T2081QDS, T4240RDB)
56 static int find_ir_chip_on_i2c(void)
62 const int ir_i2c_addr[] = {0x38, 0x08, 0x09};
64 /* Check all the address */
65 for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) {
66 i2caddress = ir_i2c_addr[i];
67 ret = i2c_read(i2caddress,
68 IR36021_MFR_ID_OFFSET, 1, (void *)&byte,
70 if ((ret >= 0) && (byte == IR36021_MFR_ID))
77 /* Maximum loop count waiting for new voltage to take effect */
78 #define MAX_LOOP_WAIT_NEW_VOL 100
79 /* Maximum loop count waiting for the voltage to be stable */
80 #define MAX_LOOP_WAIT_VOL_STABLE 100
82 * read_voltage from sensor on I2C bus
83 * We use average of 4 readings, waiting for WAIT_FOR_ADC before
86 #define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
88 /* If an INA220 chip is available, we can use it to read back the voltage
89 * as it may have a higher accuracy than the IR chip for the same purpose
91 #ifdef CONFIG_VOL_MONITOR_INA220
92 #define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
93 #define ADC_MIN_ACCURACY 4
95 #define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
96 #define ADC_MIN_ACCURACY 4
99 #ifdef CONFIG_VOL_MONITOR_INA220
100 static int read_voltage_from_INA220(int i2caddress)
102 int i, ret, voltage_read = 0;
106 for (i = 0; i < NUM_READINGS; i++) {
107 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
108 I2C_VOL_MONITOR_BUS_V_OFFSET, 1,
111 printf("VID: failed to read core voltage\n");
114 vol_mon = (buf[0] << 8) | buf[1];
115 if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
116 printf("VID: Core voltage sensor error\n");
119 debug("VID: bus voltage reads 0x%04x\n", vol_mon);
121 voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
122 udelay(WAIT_FOR_ADC);
124 /* calculate the average */
125 voltage_read /= NUM_READINGS;
131 /* read voltage from IR */
132 #ifdef CONFIG_VOL_MONITOR_IR36021_READ
133 static int read_voltage_from_IR(int i2caddress)
135 int i, ret, voltage_read = 0;
139 for (i = 0; i < NUM_READINGS; i++) {
140 ret = i2c_read(i2caddress,
141 IR36021_LOOP1_VOUT_OFFSET,
144 printf("VID: failed to read vcpu\n");
149 printf("VID: Core voltage sensor error\n");
152 debug("VID: bus voltage reads 0x%02x\n", vol_mon);
153 /* Resolution is 1/128V. We scale up here to get 1/128mV
154 * and divide at the end
156 voltage_read += vol_mon * 1000;
157 udelay(WAIT_FOR_ADC);
159 /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
160 voltage_read = DIV_ROUND_UP(voltage_read, 128);
162 /* calculate the average */
163 voltage_read /= NUM_READINGS;
165 /* Compensate for a board specific voltage drop between regulator and
166 * SoC before converting into an IR VID value
168 voltage_read -= board_vdd_drop_compensation();
174 #ifdef CONFIG_VOL_MONITOR_LTC3882_READ
175 /* read the current value of the LTC Regulator Voltage */
176 static int read_voltage_from_LTC(int i2caddress)
179 u8 chan = PWM_CHANNEL0;
181 /* select the PAGE 0 using PMBus commands PAGE for VDD*/
182 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
183 PMBUS_CMD_PAGE, 1, &chan, 1);
185 printf("VID: failed to select VDD Page 0\n");
189 /*read the output voltage using PMBus command READ_VOUT*/
190 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
191 PMBUS_CMD_READ_VOUT, 1, (void *)&vcode, 2);
193 printf("VID: failed to read the volatge\n");
197 /* Scale down to the real mV as LTC resolution is 1/4096V,rounding up */
198 vcode = DIV_ROUND_UP(vcode * 1000, 4096);
204 static int read_voltage(int i2caddress)
207 #ifdef CONFIG_VOL_MONITOR_INA220
208 voltage_read = read_voltage_from_INA220(i2caddress);
209 #elif defined CONFIG_VOL_MONITOR_IR36021_READ
210 voltage_read = read_voltage_from_IR(i2caddress);
211 #elif defined CONFIG_VOL_MONITOR_LTC3882_READ
212 voltage_read = read_voltage_from_LTC(i2caddress);
219 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
221 * We need to calculate how long before the voltage stops to drop
222 * or increase. It returns with the loop count. Each loop takes
223 * several readings (WAIT_FOR_ADC)
225 static int wait_for_new_voltage(int vdd, int i2caddress)
227 int timeout, vdd_current;
229 vdd_current = read_voltage(i2caddress);
230 /* wait until voltage starts to reach the target. Voltage slew
231 * rates by typical regulators will always lead to stable readings
232 * within each fairly long ADC interval in comparison to the
233 * intended voltage delta change until the target voltage is
234 * reached. The fairly small voltage delta change to any target
235 * VID voltage also means that this function will always complete
236 * within few iterations. If the timeout was ever reached, it would
237 * point to a serious failure in the regulator system.
240 abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
241 timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
242 vdd_current = read_voltage(i2caddress);
244 if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
245 printf("VID: Voltage adjustment timeout\n");
252 * this function keeps reading the voltage until it is stable or until the
255 static int wait_for_voltage_stable(int i2caddress)
257 int timeout, vdd_current, vdd;
259 vdd = read_voltage(i2caddress);
260 udelay(NUM_READINGS * WAIT_FOR_ADC);
262 /* wait until voltage is stable */
263 vdd_current = read_voltage(i2caddress);
264 /* The maximum timeout is
265 * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
267 for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
268 abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
269 timeout > 0; timeout--) {
271 udelay(NUM_READINGS * WAIT_FOR_ADC);
272 vdd_current = read_voltage(i2caddress);
279 /* Set the voltage to the IR chip */
280 static int set_voltage_to_IR(int i2caddress, int vdd)
286 /* Compensate for a board specific voltage drop between regulator and
287 * SoC before converting into an IR VID value
289 vdd += board_vdd_drop_compensation();
290 #ifdef CONFIG_FSL_LSCH2
291 vid = DIV_ROUND_UP(vdd - 265, 5);
293 vid = DIV_ROUND_UP(vdd - 245, 5);
296 ret = i2c_write(i2caddress, IR36021_LOOP1_MANUAL_ID_OFFSET,
297 1, (void *)&vid, sizeof(vid));
299 printf("VID: failed to write VID\n");
302 wait = wait_for_new_voltage(vdd, i2caddress);
305 debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
307 vdd_last = wait_for_voltage_stable(i2caddress);
310 debug("VID: Current voltage is %d mV\n", vdd_last);
316 #ifdef CONFIG_VOL_MONITOR_LTC3882_SET
317 /* this function sets the VDD and returns the value set */
318 static int set_voltage_to_LTC(int i2caddress, int vdd)
320 int ret, vdd_last, vdd_target = vdd;
321 int count = 100, temp = 0;
323 /* Scale up to the LTC resolution is 1/4096V */
324 vdd = (vdd * 4096) / 1000;
326 /* 5-byte buffer which needs to be sent following the
327 * PMBus command PAGE_PLUS_WRITE.
329 u8 buff[5] = {0x04, PWM_CHANNEL0, PMBUS_CMD_VOUT_COMMAND,
330 vdd & 0xFF, (vdd & 0xFF00) >> 8};
332 /* Write the desired voltage code to the regulator */
333 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
334 PMBUS_CMD_PAGE_PLUS_WRITE, 1, (void *)&buff, 5);
336 printf("VID: I2C failed to write to the volatge regulator\n");
340 /* Wait for the volatge to get to the desired value */
342 vdd_last = read_voltage_from_LTC(i2caddress);
344 printf("VID: Couldn't read sensor abort VID adjust\n");
348 temp = vdd_last - vdd_target;
349 } while ((abs(temp) > 2) && (count > 0));
355 static int set_voltage(int i2caddress, int vdd)
359 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
360 vdd_last = set_voltage_to_IR(i2caddress, vdd);
361 #elif defined CONFIG_VOL_MONITOR_LTC3882_SET
362 vdd_last = set_voltage_to_LTC(i2caddress, vdd);
364 #error Specific voltage monitor must be defined
369 #ifdef CONFIG_FSL_LSCH3
370 int adjust_vdd(ulong vdd_override)
372 int re_enable = disable_interrupts();
373 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
375 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
376 defined(CONFIG_VOL_MONITOR_IR36021_READ)
381 int vdd_target, vdd_current, vdd_last;
383 unsigned long vdd_string_override;
385 #ifdef CONFIG_ARCH_LX2160A
386 static const u16 vdd[32] = {
421 #ifdef CONFIG_ARCH_LS1088A
422 static const uint16_t vdd[32] = {
458 static const uint16_t vdd[32] = {
499 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
501 debug("VID: I2C failed to switch channel\n");
505 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
506 defined(CONFIG_VOL_MONITOR_IR36021_READ)
507 ret = find_ir_chip_on_i2c();
509 printf("VID: Could not find voltage regulator on I2C.\n");
514 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
517 /* check IR chip work on Intel mode*/
518 ret = i2c_read(i2caddress,
519 IR36021_INTEL_MODE_OOFSET,
522 printf("VID: failed to read IR chip mode.\n");
526 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
527 printf("VID: IR Chip is not used in Intel mode.\n");
533 /* get the voltage ID from fuse status register */
534 fusesr = in_le32(&gur->dcfg_fusesr);
535 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
536 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
537 if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
538 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
539 FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
541 vdd_target = vdd[vid];
543 /* check override variable for overriding VDD */
544 vdd_string = env_get(CONFIG_VID_FLS_ENV);
545 if (vdd_override == 0 && vdd_string &&
546 !strict_strtoul(vdd_string, 10, &vdd_string_override))
547 vdd_override = vdd_string_override;
549 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
550 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
551 debug("VDD override is %lu\n", vdd_override);
552 } else if (vdd_override != 0) {
553 printf("Invalid value.\n");
556 /* divide and round up by 10 to get a value in mV */
557 vdd_target = DIV_ROUND_UP(vdd_target, 10);
558 if (vdd_target == 0) {
559 debug("VID: VID not used\n");
562 } else if (vdd_target < VDD_MV_MIN || vdd_target > VDD_MV_MAX) {
563 /* Check vdd_target is in valid range */
564 printf("VID: Target VID %d mV is not in range.\n",
569 debug("VID: vid = %d mV\n", vdd_target);
573 * Read voltage monitor to check real voltage.
575 vdd_last = read_voltage(i2caddress);
577 printf("VID: Couldn't read sensor abort VID adjustment\n");
581 vdd_current = vdd_last;
582 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
584 #ifdef CONFIG_VOL_MONITOR_LTC3882_SET
585 /* Set the target voltage */
586 vdd_last = vdd_current = set_voltage(i2caddress, vdd_target);
589 * Adjust voltage to at or one step above target.
590 * As measurements are less precise than setting the values
591 * we may run through dummy steps that cancel each other
592 * when stepping up and then down.
594 while (vdd_last > 0 &&
595 vdd_last < vdd_target) {
596 vdd_current += IR_VDD_STEP_UP;
597 vdd_last = set_voltage(i2caddress, vdd_current);
599 while (vdd_last > 0 &&
600 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
601 vdd_current -= IR_VDD_STEP_DOWN;
602 vdd_last = set_voltage(i2caddress, vdd_current);
606 if (board_adjust_vdd(vdd_target) < 0) {
612 printf("VID: Core voltage after adjustment is at %d mV\n",
619 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
622 #else /* !CONFIG_FSL_LSCH3 */
623 int adjust_vdd(ulong vdd_override)
625 int re_enable = disable_interrupts();
626 #if defined(CONFIG_FSL_LSCH2)
627 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
629 ccsr_gur_t __iomem *gur =
630 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
634 int vdd_target, vdd_current, vdd_last;
636 unsigned long vdd_string_override;
638 static const uint16_t vdd[32] = {
671 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
673 debug("VID: I2C failed to switch channel\n");
677 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
678 defined(CONFIG_VOL_MONITOR_IR36021_READ)
679 ret = find_ir_chip_on_i2c();
681 printf("VID: Could not find voltage regulator on I2C.\n");
686 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
689 /* check IR chip work on Intel mode*/
690 ret = i2c_read(i2caddress,
691 IR36021_INTEL_MODE_OOFSET,
694 printf("VID: failed to read IR chip mode.\n");
698 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
699 printf("VID: IR Chip is not used in Intel mode.\n");
705 /* get the voltage ID from fuse status register */
706 fusesr = in_be32(&gur->dcfg_fusesr);
708 * VID is used according to the table below
709 * ---------------------------------------
711 * |-------------------------------------|
712 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
713 * ---------------+---------+-----------------+---------|
714 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
715 * | A |----------+---------+-----------------+---------|
716 * | _ | 5b00001 |VID = | VID = |VID = |
717 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
718 * | _ | 5b11110 | | | |
719 * | A |----------+---------+-----------------+---------|
720 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
722 * ------------------------------------------------------
724 #ifdef CONFIG_FSL_LSCH2
725 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
726 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
727 if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
728 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
729 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
732 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
733 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
734 if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
735 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
736 FSL_CORENET_DCFG_FUSESR_VID_MASK;
739 vdd_target = vdd[vid];
741 /* check override variable for overriding VDD */
742 vdd_string = env_get(CONFIG_VID_FLS_ENV);
743 if (vdd_override == 0 && vdd_string &&
744 !strict_strtoul(vdd_string, 10, &vdd_string_override))
745 vdd_override = vdd_string_override;
746 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
747 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
748 debug("VDD override is %lu\n", vdd_override);
749 } else if (vdd_override != 0) {
750 printf("Invalid value.\n");
752 if (vdd_target == 0) {
753 debug("VID: VID not used\n");
757 /* divide and round up by 10 to get a value in mV */
758 vdd_target = DIV_ROUND_UP(vdd_target, 10);
759 debug("VID: vid = %d mV\n", vdd_target);
763 * Read voltage monitor to check real voltage.
765 vdd_last = read_voltage(i2caddress);
767 printf("VID: Couldn't read sensor abort VID adjustment\n");
771 vdd_current = vdd_last;
772 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
774 * Adjust voltage to at or one step above target.
775 * As measurements are less precise than setting the values
776 * we may run through dummy steps that cancel each other
777 * when stepping up and then down.
779 while (vdd_last > 0 &&
780 vdd_last < vdd_target) {
781 vdd_current += IR_VDD_STEP_UP;
782 vdd_last = set_voltage(i2caddress, vdd_current);
784 while (vdd_last > 0 &&
785 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
786 vdd_current -= IR_VDD_STEP_DOWN;
787 vdd_last = set_voltage(i2caddress, vdd_current);
791 printf("VID: Core voltage after adjustment is at %d mV\n",
799 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
805 static int print_vdd(void)
807 int vdd_last, ret, i2caddress;
809 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
811 debug("VID : I2c failed to switch channel\n");
814 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
815 defined(CONFIG_VOL_MONITOR_IR36021_READ)
816 ret = find_ir_chip_on_i2c();
818 printf("VID: Could not find voltage regulator on I2C.\n");
822 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
827 * Read voltage monitor to check real voltage.
829 vdd_last = read_voltage(i2caddress);
831 printf("VID: Couldn't read sensor abort VID adjustment\n");
834 printf("VID: Core voltage is at %d mV\n", vdd_last);
836 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
838 return ret < 0 ? -1 : 0;
842 static int do_vdd_override(cmd_tbl_t *cmdtp,
849 return CMD_RET_USAGE;
851 if (!strict_strtoul(argv[1], 10, &override))
852 adjust_vdd(override); /* the value is checked by callee */
854 return CMD_RET_USAGE;
858 static int do_vdd_read(cmd_tbl_t *cmdtp,
863 return CMD_RET_USAGE;
870 vdd_override, 2, 0, do_vdd_override,
872 " - override with the voltage specified in mV, eg. 1050"
876 vdd_read, 1, 0, do_vdd_read,
878 " - Read the voltage specified in mV"