2 * Copyright 2014 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
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 DECLARE_GLOBAL_DATA_PTR;
22 int __weak i2c_multiplexer_select_vid_channel(u8 channel)
28 * Compensate for a board specific voltage drop between regulator and SoC
29 * return a value in mV
31 int __weak board_vdd_drop_compensation(void)
37 * Board specific settings for specific voltage value
39 int __weak board_adjust_vdd(int vdd)
44 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
45 defined(CONFIG_VOL_MONITOR_IR36021_READ)
47 * Get the i2c address configuration for the IR regulator chip
49 * There are some variance in the RDB HW regarding the I2C address configuration
50 * for the IR regulator chip, which is likely a problem of external resistor
51 * accuracy. So we just check each address in a hopefully non-intrusive mode
52 * and use the first one that seems to work
54 * The IR chip can show up under the following addresses:
55 * 0x08 (Verified on T1040RDB-PA,T4240RDB-PB,X-T4240RDB-16GPA)
56 * 0x09 (Verified on T1040RDB-PA)
57 * 0x38 (Verified on T2080QDS, T2081QDS, T4240RDB)
59 static int find_ir_chip_on_i2c(void)
65 const int ir_i2c_addr[] = {0x38, 0x08, 0x09};
67 /* Check all the address */
68 for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) {
69 i2caddress = ir_i2c_addr[i];
70 ret = i2c_read(i2caddress,
71 IR36021_MFR_ID_OFFSET, 1, (void *)&byte,
73 if ((ret >= 0) && (byte == IR36021_MFR_ID))
80 /* Maximum loop count waiting for new voltage to take effect */
81 #define MAX_LOOP_WAIT_NEW_VOL 100
82 /* Maximum loop count waiting for the voltage to be stable */
83 #define MAX_LOOP_WAIT_VOL_STABLE 100
85 * read_voltage from sensor on I2C bus
86 * We use average of 4 readings, waiting for WAIT_FOR_ADC before
89 #define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
91 /* If an INA220 chip is available, we can use it to read back the voltage
92 * as it may have a higher accuracy than the IR chip for the same purpose
94 #ifdef CONFIG_VOL_MONITOR_INA220
95 #define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
96 #define ADC_MIN_ACCURACY 4
98 #define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
99 #define ADC_MIN_ACCURACY 4
102 #ifdef CONFIG_VOL_MONITOR_INA220
103 static int read_voltage_from_INA220(int i2caddress)
105 int i, ret, voltage_read = 0;
109 for (i = 0; i < NUM_READINGS; i++) {
110 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
111 I2C_VOL_MONITOR_BUS_V_OFFSET, 1,
114 printf("VID: failed to read core voltage\n");
117 vol_mon = (buf[0] << 8) | buf[1];
118 if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
119 printf("VID: Core voltage sensor error\n");
122 debug("VID: bus voltage reads 0x%04x\n", vol_mon);
124 voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
125 udelay(WAIT_FOR_ADC);
127 /* calculate the average */
128 voltage_read /= NUM_READINGS;
134 /* read voltage from IR */
135 #ifdef CONFIG_VOL_MONITOR_IR36021_READ
136 static int read_voltage_from_IR(int i2caddress)
138 int i, ret, voltage_read = 0;
142 for (i = 0; i < NUM_READINGS; i++) {
143 ret = i2c_read(i2caddress,
144 IR36021_LOOP1_VOUT_OFFSET,
147 printf("VID: failed to read vcpu\n");
152 printf("VID: Core voltage sensor error\n");
155 debug("VID: bus voltage reads 0x%02x\n", vol_mon);
156 /* Resolution is 1/128V. We scale up here to get 1/128mV
157 * and divide at the end
159 voltage_read += vol_mon * 1000;
160 udelay(WAIT_FOR_ADC);
162 /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
163 voltage_read = DIV_ROUND_UP(voltage_read, 128);
165 /* calculate the average */
166 voltage_read /= NUM_READINGS;
168 /* Compensate for a board specific voltage drop between regulator and
169 * SoC before converting into an IR VID value
171 voltage_read -= board_vdd_drop_compensation();
177 #ifdef CONFIG_VOL_MONITOR_LTC3882_READ
178 /* read the current value of the LTC Regulator Voltage */
179 static int read_voltage_from_LTC(int i2caddress)
182 u8 chan = PWM_CHANNEL0;
184 /* select the PAGE 0 using PMBus commands PAGE for VDD*/
185 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
186 PMBUS_CMD_PAGE, 1, &chan, 1);
188 printf("VID: failed to select VDD Page 0\n");
192 /*read the output voltage using PMBus command READ_VOUT*/
193 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
194 PMBUS_CMD_READ_VOUT, 1, (void *)&vcode, 2);
196 printf("VID: failed to read the volatge\n");
200 /* Scale down to the real mV as LTC resolution is 1/4096V,rounding up */
201 vcode = DIV_ROUND_UP(vcode * 1000, 4096);
207 static int read_voltage(int i2caddress)
210 #ifdef CONFIG_VOL_MONITOR_INA220
211 voltage_read = read_voltage_from_INA220(i2caddress);
212 #elif defined CONFIG_VOL_MONITOR_IR36021_READ
213 voltage_read = read_voltage_from_IR(i2caddress);
214 #elif defined CONFIG_VOL_MONITOR_LTC3882_READ
215 voltage_read = read_voltage_from_LTC(i2caddress);
222 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
224 * We need to calculate how long before the voltage stops to drop
225 * or increase. It returns with the loop count. Each loop takes
226 * several readings (WAIT_FOR_ADC)
228 static int wait_for_new_voltage(int vdd, int i2caddress)
230 int timeout, vdd_current;
232 vdd_current = read_voltage(i2caddress);
233 /* wait until voltage starts to reach the target. Voltage slew
234 * rates by typical regulators will always lead to stable readings
235 * within each fairly long ADC interval in comparison to the
236 * intended voltage delta change until the target voltage is
237 * reached. The fairly small voltage delta change to any target
238 * VID voltage also means that this function will always complete
239 * within few iterations. If the timeout was ever reached, it would
240 * point to a serious failure in the regulator system.
243 abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
244 timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
245 vdd_current = read_voltage(i2caddress);
247 if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
248 printf("VID: Voltage adjustment timeout\n");
255 * this function keeps reading the voltage until it is stable or until the
258 static int wait_for_voltage_stable(int i2caddress)
260 int timeout, vdd_current, vdd;
262 vdd = read_voltage(i2caddress);
263 udelay(NUM_READINGS * WAIT_FOR_ADC);
265 /* wait until voltage is stable */
266 vdd_current = read_voltage(i2caddress);
267 /* The maximum timeout is
268 * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
270 for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
271 abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
272 timeout > 0; timeout--) {
274 udelay(NUM_READINGS * WAIT_FOR_ADC);
275 vdd_current = read_voltage(i2caddress);
282 /* Set the voltage to the IR chip */
283 static int set_voltage_to_IR(int i2caddress, int vdd)
289 /* Compensate for a board specific voltage drop between regulator and
290 * SoC before converting into an IR VID value
292 vdd += board_vdd_drop_compensation();
293 #ifdef CONFIG_FSL_LSCH2
294 vid = DIV_ROUND_UP(vdd - 265, 5);
296 vid = DIV_ROUND_UP(vdd - 245, 5);
299 ret = i2c_write(i2caddress, IR36021_LOOP1_MANUAL_ID_OFFSET,
300 1, (void *)&vid, sizeof(vid));
302 printf("VID: failed to write VID\n");
305 wait = wait_for_new_voltage(vdd, i2caddress);
308 debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
310 vdd_last = wait_for_voltage_stable(i2caddress);
313 debug("VID: Current voltage is %d mV\n", vdd_last);
319 #ifdef CONFIG_VOL_MONITOR_LTC3882_SET
320 /* this function sets the VDD and returns the value set */
321 static int set_voltage_to_LTC(int i2caddress, int vdd)
323 int ret, vdd_last, vdd_target = vdd;
325 /* Scale up to the LTC resolution is 1/4096V */
326 vdd = (vdd * 4096) / 1000;
328 /* 5-byte buffer which needs to be sent following the
329 * PMBus command PAGE_PLUS_WRITE.
331 u8 buff[5] = {0x04, PWM_CHANNEL0, PMBUS_CMD_VOUT_COMMAND,
332 vdd & 0xFF, (vdd & 0xFF00) >> 8};
334 /* Write the desired voltage code to the regulator */
335 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
336 PMBUS_CMD_PAGE_PLUS_WRITE, 1, (void *)&buff, 5);
338 printf("VID: I2C failed to write to the volatge regulator\n");
342 /* Wait for the volatge to get to the desired value */
344 vdd_last = read_voltage_from_LTC(i2caddress);
346 printf("VID: Couldn't read sensor abort VID adjust\n");
349 } while (vdd_last != vdd_target);
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_LS1088A
386 static const uint16_t vdd[32] = {
422 static const uint16_t vdd[32] = {
462 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
464 debug("VID: I2C failed to switch channel\n");
468 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
469 defined(CONFIG_VOL_MONITOR_IR36021_READ)
470 ret = find_ir_chip_on_i2c();
472 printf("VID: Could not find voltage regulator on I2C.\n");
477 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
480 /* check IR chip work on Intel mode*/
481 ret = i2c_read(i2caddress,
482 IR36021_INTEL_MODE_OOFSET,
485 printf("VID: failed to read IR chip mode.\n");
489 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
490 printf("VID: IR Chip is not used in Intel mode.\n");
496 /* get the voltage ID from fuse status register */
497 fusesr = in_le32(&gur->dcfg_fusesr);
498 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
499 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
500 if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
501 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
502 FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
504 vdd_target = vdd[vid];
506 /* check override variable for overriding VDD */
507 vdd_string = env_get(CONFIG_VID_FLS_ENV);
508 if (vdd_override == 0 && vdd_string &&
509 !strict_strtoul(vdd_string, 10, &vdd_string_override))
510 vdd_override = vdd_string_override;
512 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
513 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
514 debug("VDD override is %lu\n", vdd_override);
515 } else if (vdd_override != 0) {
516 printf("Invalid value.\n");
519 /* divide and round up by 10 to get a value in mV */
520 vdd_target = DIV_ROUND_UP(vdd_target, 10);
521 if (vdd_target == 0) {
522 debug("VID: VID not used\n");
525 } else if (vdd_target < VDD_MV_MIN || vdd_target > VDD_MV_MAX) {
526 /* Check vdd_target is in valid range */
527 printf("VID: Target VID %d mV is not in range.\n",
532 debug("VID: vid = %d mV\n", vdd_target);
536 * Read voltage monitor to check real voltage.
538 vdd_last = read_voltage(i2caddress);
540 printf("VID: Couldn't read sensor abort VID adjustment\n");
544 vdd_current = vdd_last;
545 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
547 #ifdef CONFIG_VOL_MONITOR_LTC3882_SET
548 /* Set the target voltage */
549 vdd_last = vdd_current = set_voltage(i2caddress, vdd_target);
552 * Adjust voltage to at or one step above target.
553 * As measurements are less precise than setting the values
554 * we may run through dummy steps that cancel each other
555 * when stepping up and then down.
557 while (vdd_last > 0 &&
558 vdd_last < vdd_target) {
559 vdd_current += IR_VDD_STEP_UP;
560 vdd_last = set_voltage(i2caddress, vdd_current);
562 while (vdd_last > 0 &&
563 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
564 vdd_current -= IR_VDD_STEP_DOWN;
565 vdd_last = set_voltage(i2caddress, vdd_current);
569 if (board_adjust_vdd(vdd_target) < 0) {
575 printf("VID: Core voltage after adjustment is at %d mV\n",
582 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
585 #else /* !CONFIG_FSL_LSCH3 */
586 int adjust_vdd(ulong vdd_override)
588 int re_enable = disable_interrupts();
589 #if defined(CONFIG_FSL_LSCH2)
590 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
592 ccsr_gur_t __iomem *gur =
593 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
597 int vdd_target, vdd_current, vdd_last;
599 unsigned long vdd_string_override;
601 static const uint16_t vdd[32] = {
634 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
636 debug("VID: I2C failed to switch channel\n");
640 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
641 defined(CONFIG_VOL_MONITOR_IR36021_READ)
642 ret = find_ir_chip_on_i2c();
644 printf("VID: Could not find voltage regulator on I2C.\n");
649 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
652 /* check IR chip work on Intel mode*/
653 ret = i2c_read(i2caddress,
654 IR36021_INTEL_MODE_OOFSET,
657 printf("VID: failed to read IR chip mode.\n");
661 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
662 printf("VID: IR Chip is not used in Intel mode.\n");
668 /* get the voltage ID from fuse status register */
669 fusesr = in_be32(&gur->dcfg_fusesr);
671 * VID is used according to the table below
672 * ---------------------------------------
674 * |-------------------------------------|
675 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
676 * ---------------+---------+-----------------+---------|
677 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
678 * | A |----------+---------+-----------------+---------|
679 * | _ | 5b00001 |VID = | VID = |VID = |
680 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
681 * | _ | 5b11110 | | | |
682 * | A |----------+---------+-----------------+---------|
683 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
685 * ------------------------------------------------------
687 #ifdef CONFIG_FSL_LSCH2
688 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
689 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
690 if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
691 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
692 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
695 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
696 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
697 if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
698 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
699 FSL_CORENET_DCFG_FUSESR_VID_MASK;
702 vdd_target = vdd[vid];
704 /* check override variable for overriding VDD */
705 vdd_string = env_get(CONFIG_VID_FLS_ENV);
706 if (vdd_override == 0 && vdd_string &&
707 !strict_strtoul(vdd_string, 10, &vdd_string_override))
708 vdd_override = vdd_string_override;
709 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
710 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
711 debug("VDD override is %lu\n", vdd_override);
712 } else if (vdd_override != 0) {
713 printf("Invalid value.\n");
715 if (vdd_target == 0) {
716 debug("VID: VID not used\n");
720 /* divide and round up by 10 to get a value in mV */
721 vdd_target = DIV_ROUND_UP(vdd_target, 10);
722 debug("VID: vid = %d mV\n", vdd_target);
726 * Read voltage monitor to check real voltage.
728 vdd_last = read_voltage(i2caddress);
730 printf("VID: Couldn't read sensor abort VID adjustment\n");
734 vdd_current = vdd_last;
735 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
737 * Adjust voltage to at or one step above target.
738 * As measurements are less precise than setting the values
739 * we may run through dummy steps that cancel each other
740 * when stepping up and then down.
742 while (vdd_last > 0 &&
743 vdd_last < vdd_target) {
744 vdd_current += IR_VDD_STEP_UP;
745 vdd_last = set_voltage(i2caddress, vdd_current);
747 while (vdd_last > 0 &&
748 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
749 vdd_current -= IR_VDD_STEP_DOWN;
750 vdd_last = set_voltage(i2caddress, vdd_current);
754 printf("VID: Core voltage after adjustment is at %d mV\n",
762 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
768 static int print_vdd(void)
770 int vdd_last, ret, i2caddress;
772 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
774 debug("VID : I2c failed to switch channel\n");
777 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
778 defined(CONFIG_VOL_MONITOR_IR36021_READ)
779 ret = find_ir_chip_on_i2c();
781 printf("VID: Could not find voltage regulator on I2C.\n");
785 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
790 * Read voltage monitor to check real voltage.
792 vdd_last = read_voltage(i2caddress);
794 printf("VID: Couldn't read sensor abort VID adjustment\n");
797 printf("VID: Core voltage is at %d mV\n", vdd_last);
799 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
801 return ret < 0 ? -1 : 0;
805 static int do_vdd_override(cmd_tbl_t *cmdtp,
812 return CMD_RET_USAGE;
814 if (!strict_strtoul(argv[1], 10, &override))
815 adjust_vdd(override); /* the value is checked by callee */
817 return CMD_RET_USAGE;
821 static int do_vdd_read(cmd_tbl_t *cmdtp,
826 return CMD_RET_USAGE;
833 vdd_override, 2, 0, do_vdd_override,
835 " - override with the voltage specified in mV, eg. 1050"
839 vdd_read, 1, 0, do_vdd_read,
841 " - Read the voltage specified in mV"