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;
322 /* Scale up to the LTC resolution is 1/4096V */
323 vdd = (vdd * 4096) / 1000;
325 /* 5-byte buffer which needs to be sent following the
326 * PMBus command PAGE_PLUS_WRITE.
328 u8 buff[5] = {0x04, PWM_CHANNEL0, PMBUS_CMD_VOUT_COMMAND,
329 vdd & 0xFF, (vdd & 0xFF00) >> 8};
331 /* Write the desired voltage code to the regulator */
332 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
333 PMBUS_CMD_PAGE_PLUS_WRITE, 1, (void *)&buff, 5);
335 printf("VID: I2C failed to write to the volatge regulator\n");
339 /* Wait for the volatge to get to the desired value */
341 vdd_last = read_voltage_from_LTC(i2caddress);
343 printf("VID: Couldn't read sensor abort VID adjust\n");
346 } while (vdd_last != vdd_target);
352 static int set_voltage(int i2caddress, int vdd)
356 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
357 vdd_last = set_voltage_to_IR(i2caddress, vdd);
358 #elif defined CONFIG_VOL_MONITOR_LTC3882_SET
359 vdd_last = set_voltage_to_LTC(i2caddress, vdd);
361 #error Specific voltage monitor must be defined
366 #ifdef CONFIG_FSL_LSCH3
367 int adjust_vdd(ulong vdd_override)
369 int re_enable = disable_interrupts();
370 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
372 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
373 defined(CONFIG_VOL_MONITOR_IR36021_READ)
378 int vdd_target, vdd_current, vdd_last;
380 unsigned long vdd_string_override;
382 #ifdef CONFIG_ARCH_LS1088A
383 static const uint16_t vdd[32] = {
419 static const uint16_t vdd[32] = {
459 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
461 debug("VID: I2C failed to switch channel\n");
465 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
466 defined(CONFIG_VOL_MONITOR_IR36021_READ)
467 ret = find_ir_chip_on_i2c();
469 printf("VID: Could not find voltage regulator on I2C.\n");
474 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
477 /* check IR chip work on Intel mode*/
478 ret = i2c_read(i2caddress,
479 IR36021_INTEL_MODE_OOFSET,
482 printf("VID: failed to read IR chip mode.\n");
486 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
487 printf("VID: IR Chip is not used in Intel mode.\n");
493 /* get the voltage ID from fuse status register */
494 fusesr = in_le32(&gur->dcfg_fusesr);
495 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
496 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
497 if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
498 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
499 FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
501 vdd_target = vdd[vid];
503 /* check override variable for overriding VDD */
504 vdd_string = env_get(CONFIG_VID_FLS_ENV);
505 if (vdd_override == 0 && vdd_string &&
506 !strict_strtoul(vdd_string, 10, &vdd_string_override))
507 vdd_override = vdd_string_override;
509 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
510 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
511 debug("VDD override is %lu\n", vdd_override);
512 } else if (vdd_override != 0) {
513 printf("Invalid value.\n");
516 /* divide and round up by 10 to get a value in mV */
517 vdd_target = DIV_ROUND_UP(vdd_target, 10);
518 if (vdd_target == 0) {
519 debug("VID: VID not used\n");
522 } else if (vdd_target < VDD_MV_MIN || vdd_target > VDD_MV_MAX) {
523 /* Check vdd_target is in valid range */
524 printf("VID: Target VID %d mV is not in range.\n",
529 debug("VID: vid = %d mV\n", vdd_target);
533 * Read voltage monitor to check real voltage.
535 vdd_last = read_voltage(i2caddress);
537 printf("VID: Couldn't read sensor abort VID adjustment\n");
541 vdd_current = vdd_last;
542 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
544 #ifdef CONFIG_VOL_MONITOR_LTC3882_SET
545 /* Set the target voltage */
546 vdd_last = vdd_current = set_voltage(i2caddress, vdd_target);
549 * Adjust voltage to at or one step above target.
550 * As measurements are less precise than setting the values
551 * we may run through dummy steps that cancel each other
552 * when stepping up and then down.
554 while (vdd_last > 0 &&
555 vdd_last < vdd_target) {
556 vdd_current += IR_VDD_STEP_UP;
557 vdd_last = set_voltage(i2caddress, vdd_current);
559 while (vdd_last > 0 &&
560 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
561 vdd_current -= IR_VDD_STEP_DOWN;
562 vdd_last = set_voltage(i2caddress, vdd_current);
566 if (board_adjust_vdd(vdd_target) < 0) {
572 printf("VID: Core voltage after adjustment is at %d mV\n",
579 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
582 #else /* !CONFIG_FSL_LSCH3 */
583 int adjust_vdd(ulong vdd_override)
585 int re_enable = disable_interrupts();
586 #if defined(CONFIG_FSL_LSCH2)
587 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
589 ccsr_gur_t __iomem *gur =
590 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
594 int vdd_target, vdd_current, vdd_last;
596 unsigned long vdd_string_override;
598 static const uint16_t vdd[32] = {
631 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
633 debug("VID: I2C failed to switch channel\n");
637 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
638 defined(CONFIG_VOL_MONITOR_IR36021_READ)
639 ret = find_ir_chip_on_i2c();
641 printf("VID: Could not find voltage regulator on I2C.\n");
646 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
649 /* check IR chip work on Intel mode*/
650 ret = i2c_read(i2caddress,
651 IR36021_INTEL_MODE_OOFSET,
654 printf("VID: failed to read IR chip mode.\n");
658 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
659 printf("VID: IR Chip is not used in Intel mode.\n");
665 /* get the voltage ID from fuse status register */
666 fusesr = in_be32(&gur->dcfg_fusesr);
668 * VID is used according to the table below
669 * ---------------------------------------
671 * |-------------------------------------|
672 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
673 * ---------------+---------+-----------------+---------|
674 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
675 * | A |----------+---------+-----------------+---------|
676 * | _ | 5b00001 |VID = | VID = |VID = |
677 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
678 * | _ | 5b11110 | | | |
679 * | A |----------+---------+-----------------+---------|
680 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
682 * ------------------------------------------------------
684 #ifdef CONFIG_FSL_LSCH2
685 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
686 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
687 if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
688 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
689 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
692 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
693 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
694 if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
695 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
696 FSL_CORENET_DCFG_FUSESR_VID_MASK;
699 vdd_target = vdd[vid];
701 /* check override variable for overriding VDD */
702 vdd_string = env_get(CONFIG_VID_FLS_ENV);
703 if (vdd_override == 0 && vdd_string &&
704 !strict_strtoul(vdd_string, 10, &vdd_string_override))
705 vdd_override = vdd_string_override;
706 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
707 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
708 debug("VDD override is %lu\n", vdd_override);
709 } else if (vdd_override != 0) {
710 printf("Invalid value.\n");
712 if (vdd_target == 0) {
713 debug("VID: VID not used\n");
717 /* divide and round up by 10 to get a value in mV */
718 vdd_target = DIV_ROUND_UP(vdd_target, 10);
719 debug("VID: vid = %d mV\n", vdd_target);
723 * Read voltage monitor to check real voltage.
725 vdd_last = read_voltage(i2caddress);
727 printf("VID: Couldn't read sensor abort VID adjustment\n");
731 vdd_current = vdd_last;
732 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
734 * Adjust voltage to at or one step above target.
735 * As measurements are less precise than setting the values
736 * we may run through dummy steps that cancel each other
737 * when stepping up and then down.
739 while (vdd_last > 0 &&
740 vdd_last < vdd_target) {
741 vdd_current += IR_VDD_STEP_UP;
742 vdd_last = set_voltage(i2caddress, vdd_current);
744 while (vdd_last > 0 &&
745 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
746 vdd_current -= IR_VDD_STEP_DOWN;
747 vdd_last = set_voltage(i2caddress, vdd_current);
751 printf("VID: Core voltage after adjustment is at %d mV\n",
759 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
765 static int print_vdd(void)
767 int vdd_last, ret, i2caddress;
769 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
771 debug("VID : I2c failed to switch channel\n");
774 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
775 defined(CONFIG_VOL_MONITOR_IR36021_READ)
776 ret = find_ir_chip_on_i2c();
778 printf("VID: Could not find voltage regulator on I2C.\n");
782 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
787 * Read voltage monitor to check real voltage.
789 vdd_last = read_voltage(i2caddress);
791 printf("VID: Couldn't read sensor abort VID adjustment\n");
794 printf("VID: Core voltage is at %d mV\n", vdd_last);
796 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
798 return ret < 0 ? -1 : 0;
802 static int do_vdd_override(cmd_tbl_t *cmdtp,
809 return CMD_RET_USAGE;
811 if (!strict_strtoul(argv[1], 10, &override))
812 adjust_vdd(override); /* the value is checked by callee */
814 return CMD_RET_USAGE;
818 static int do_vdd_read(cmd_tbl_t *cmdtp,
823 return CMD_RET_USAGE;
830 vdd_override, 2, 0, do_vdd_override,
832 " - override with the voltage specified in mV, eg. 1050"
836 vdd_read, 1, 0, do_vdd_read,
838 " - Read the voltage specified in mV"