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 * Get the i2c address configuration for the IR regulator chip
39 * There are some variance in the RDB HW regarding the I2C address configuration
40 * for the IR regulator chip, which is likely a problem of external resistor
41 * accuracy. So we just check each address in a hopefully non-intrusive mode
42 * and use the first one that seems to work
44 * The IR chip can show up under the following addresses:
45 * 0x08 (Verified on T1040RDB-PA,T4240RDB-PB,X-T4240RDB-16GPA)
46 * 0x09 (Verified on T1040RDB-PA)
47 * 0x38 (Verified on T2080QDS, T2081QDS, T4240RDB)
49 static int find_ir_chip_on_i2c(void)
55 const int ir_i2c_addr[] = {0x38, 0x08, 0x09};
57 /* Check all the address */
58 for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) {
59 i2caddress = ir_i2c_addr[i];
60 ret = i2c_read(i2caddress,
61 IR36021_MFR_ID_OFFSET, 1, (void *)&byte,
63 if ((ret >= 0) && (byte == IR36021_MFR_ID))
69 /* Maximum loop count waiting for new voltage to take effect */
70 #define MAX_LOOP_WAIT_NEW_VOL 100
71 /* Maximum loop count waiting for the voltage to be stable */
72 #define MAX_LOOP_WAIT_VOL_STABLE 100
74 * read_voltage from sensor on I2C bus
75 * We use average of 4 readings, waiting for WAIT_FOR_ADC before
78 #define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
80 /* If an INA220 chip is available, we can use it to read back the voltage
81 * as it may have a higher accuracy than the IR chip for the same purpose
83 #ifdef CONFIG_VOL_MONITOR_INA220
84 #define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
85 #define ADC_MIN_ACCURACY 4
87 #define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
88 #define ADC_MIN_ACCURACY 4
91 #ifdef CONFIG_VOL_MONITOR_INA220
92 static int read_voltage_from_INA220(int i2caddress)
94 int i, ret, voltage_read = 0;
98 for (i = 0; i < NUM_READINGS; i++) {
99 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
100 I2C_VOL_MONITOR_BUS_V_OFFSET, 1,
103 printf("VID: failed to read core voltage\n");
106 vol_mon = (buf[0] << 8) | buf[1];
107 if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
108 printf("VID: Core voltage sensor error\n");
111 debug("VID: bus voltage reads 0x%04x\n", vol_mon);
113 voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
114 udelay(WAIT_FOR_ADC);
116 /* calculate the average */
117 voltage_read /= NUM_READINGS;
123 /* read voltage from IR */
124 #ifdef CONFIG_VOL_MONITOR_IR36021_READ
125 static int read_voltage_from_IR(int i2caddress)
127 int i, ret, voltage_read = 0;
131 for (i = 0; i < NUM_READINGS; i++) {
132 ret = i2c_read(i2caddress,
133 IR36021_LOOP1_VOUT_OFFSET,
136 printf("VID: failed to read vcpu\n");
141 printf("VID: Core voltage sensor error\n");
144 debug("VID: bus voltage reads 0x%02x\n", vol_mon);
145 /* Resolution is 1/128V. We scale up here to get 1/128mV
146 * and divide at the end
148 voltage_read += vol_mon * 1000;
149 udelay(WAIT_FOR_ADC);
151 /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
152 voltage_read = DIV_ROUND_UP(voltage_read, 128);
154 /* calculate the average */
155 voltage_read /= NUM_READINGS;
157 /* Compensate for a board specific voltage drop between regulator and
158 * SoC before converting into an IR VID value
160 voltage_read -= board_vdd_drop_compensation();
166 static int read_voltage(int i2caddress)
169 #ifdef CONFIG_VOL_MONITOR_INA220
170 voltage_read = read_voltage_from_INA220(i2caddress);
171 #elif defined CONFIG_VOL_MONITOR_IR36021_READ
172 voltage_read = read_voltage_from_IR(i2caddress);
180 * We need to calculate how long before the voltage stops to drop
181 * or increase. It returns with the loop count. Each loop takes
182 * several readings (WAIT_FOR_ADC)
184 static int wait_for_new_voltage(int vdd, int i2caddress)
186 int timeout, vdd_current;
188 vdd_current = read_voltage(i2caddress);
189 /* wait until voltage starts to reach the target. Voltage slew
190 * rates by typical regulators will always lead to stable readings
191 * within each fairly long ADC interval in comparison to the
192 * intended voltage delta change until the target voltage is
193 * reached. The fairly small voltage delta change to any target
194 * VID voltage also means that this function will always complete
195 * within few iterations. If the timeout was ever reached, it would
196 * point to a serious failure in the regulator system.
199 abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
200 timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
201 vdd_current = read_voltage(i2caddress);
203 if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
204 printf("VID: Voltage adjustment timeout\n");
211 * this function keeps reading the voltage until it is stable or until the
214 static int wait_for_voltage_stable(int i2caddress)
216 int timeout, vdd_current, vdd;
218 vdd = read_voltage(i2caddress);
219 udelay(NUM_READINGS * WAIT_FOR_ADC);
221 /* wait until voltage is stable */
222 vdd_current = read_voltage(i2caddress);
223 /* The maximum timeout is
224 * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
226 for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
227 abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
228 timeout > 0; timeout--) {
230 udelay(NUM_READINGS * WAIT_FOR_ADC);
231 vdd_current = read_voltage(i2caddress);
238 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
239 /* Set the voltage to the IR chip */
240 static int set_voltage_to_IR(int i2caddress, int vdd)
246 /* Compensate for a board specific voltage drop between regulator and
247 * SoC before converting into an IR VID value
249 vdd += board_vdd_drop_compensation();
250 #ifdef CONFIG_FSL_LSCH2
251 vid = DIV_ROUND_UP(vdd - 265, 5);
253 vid = DIV_ROUND_UP(vdd - 245, 5);
256 ret = i2c_write(i2caddress, IR36021_LOOP1_MANUAL_ID_OFFSET,
257 1, (void *)&vid, sizeof(vid));
259 printf("VID: failed to write VID\n");
262 wait = wait_for_new_voltage(vdd, i2caddress);
265 debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
267 vdd_last = wait_for_voltage_stable(i2caddress);
270 debug("VID: Current voltage is %d mV\n", vdd_last);
275 static int set_voltage(int i2caddress, int vdd)
279 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
280 vdd_last = set_voltage_to_IR(i2caddress, vdd);
282 #error Specific voltage monitor must be defined
287 #ifdef CONFIG_FSL_LSCH3
288 int adjust_vdd(ulong vdd_override)
290 int re_enable = disable_interrupts();
291 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
294 int vdd_target, vdd_current, vdd_last;
296 unsigned long vdd_string_override;
298 #ifdef CONFIG_ARCH_LS1088A
299 static const uint16_t vdd[32] = {
335 static const uint16_t vdd[32] = {
375 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
377 debug("VID: I2C failed to switch channel\n");
381 ret = find_ir_chip_on_i2c();
383 printf("VID: Could not find voltage regulator on I2C.\n");
388 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
391 /* check IR chip work on Intel mode*/
392 ret = i2c_read(i2caddress,
393 IR36021_INTEL_MODE_OOFSET,
396 printf("VID: failed to read IR chip mode.\n");
400 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
401 printf("VID: IR Chip is not used in Intel mode.\n");
406 /* get the voltage ID from fuse status register */
407 fusesr = in_le32(&gur->dcfg_fusesr);
408 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
409 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
410 if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
411 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
412 FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
414 vdd_target = vdd[vid];
416 /* check override variable for overriding VDD */
417 vdd_string = env_get(CONFIG_VID_FLS_ENV);
418 if (vdd_override == 0 && vdd_string &&
419 !strict_strtoul(vdd_string, 10, &vdd_string_override))
420 vdd_override = vdd_string_override;
422 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
423 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
424 debug("VDD override is %lu\n", vdd_override);
425 } else if (vdd_override != 0) {
426 printf("Invalid value.\n");
429 /* divide and round up by 10 to get a value in mV */
430 vdd_target = DIV_ROUND_UP(vdd_target, 10);
431 if (vdd_target == 0) {
432 debug("VID: VID not used\n");
435 } else if (vdd_target < VDD_MV_MIN || vdd_target > VDD_MV_MAX) {
436 /* Check vdd_target is in valid range */
437 printf("VID: Target VID %d mV is not in range.\n",
442 debug("VID: vid = %d mV\n", vdd_target);
446 * Read voltage monitor to check real voltage.
448 vdd_last = read_voltage(i2caddress);
450 printf("VID: Couldn't read sensor abort VID adjustment\n");
454 vdd_current = vdd_last;
455 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
457 * Adjust voltage to at or one step above target.
458 * As measurements are less precise than setting the values
459 * we may run through dummy steps that cancel each other
460 * when stepping up and then down.
462 while (vdd_last > 0 &&
463 vdd_last < vdd_target) {
464 vdd_current += IR_VDD_STEP_UP;
465 vdd_last = set_voltage(i2caddress, vdd_current);
467 while (vdd_last > 0 &&
468 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
469 vdd_current -= IR_VDD_STEP_DOWN;
470 vdd_last = set_voltage(i2caddress, vdd_current);
474 printf("VID: Core voltage after adjustment is at %d mV\n",
481 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
484 #else /* !CONFIG_FSL_LSCH3 */
485 int adjust_vdd(ulong vdd_override)
487 int re_enable = disable_interrupts();
488 #if defined(CONFIG_FSL_LSCH2)
489 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
491 ccsr_gur_t __iomem *gur =
492 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
496 int vdd_target, vdd_current, vdd_last;
498 unsigned long vdd_string_override;
500 static const uint16_t vdd[32] = {
533 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
535 debug("VID: I2C failed to switch channel\n");
539 ret = find_ir_chip_on_i2c();
541 printf("VID: Could not find voltage regulator on I2C.\n");
546 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
549 /* check IR chip work on Intel mode*/
550 ret = i2c_read(i2caddress,
551 IR36021_INTEL_MODE_OOFSET,
554 printf("VID: failed to read IR chip mode.\n");
558 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
559 printf("VID: IR Chip is not used in Intel mode.\n");
564 /* get the voltage ID from fuse status register */
565 fusesr = in_be32(&gur->dcfg_fusesr);
567 * VID is used according to the table below
568 * ---------------------------------------
570 * |-------------------------------------|
571 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
572 * ---------------+---------+-----------------+---------|
573 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
574 * | A |----------+---------+-----------------+---------|
575 * | _ | 5b00001 |VID = | VID = |VID = |
576 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
577 * | _ | 5b11110 | | | |
578 * | A |----------+---------+-----------------+---------|
579 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
581 * ------------------------------------------------------
583 #ifdef CONFIG_FSL_LSCH2
584 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
585 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
586 if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
587 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
588 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
591 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
592 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
593 if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
594 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
595 FSL_CORENET_DCFG_FUSESR_VID_MASK;
598 vdd_target = vdd[vid];
600 /* check override variable for overriding VDD */
601 vdd_string = env_get(CONFIG_VID_FLS_ENV);
602 if (vdd_override == 0 && vdd_string &&
603 !strict_strtoul(vdd_string, 10, &vdd_string_override))
604 vdd_override = vdd_string_override;
605 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
606 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
607 debug("VDD override is %lu\n", vdd_override);
608 } else if (vdd_override != 0) {
609 printf("Invalid value.\n");
611 if (vdd_target == 0) {
612 debug("VID: VID not used\n");
616 /* divide and round up by 10 to get a value in mV */
617 vdd_target = DIV_ROUND_UP(vdd_target, 10);
618 debug("VID: vid = %d mV\n", vdd_target);
622 * Read voltage monitor to check real voltage.
624 vdd_last = read_voltage(i2caddress);
626 printf("VID: Couldn't read sensor abort VID adjustment\n");
630 vdd_current = vdd_last;
631 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
633 * Adjust voltage to at or one step above target.
634 * As measurements are less precise than setting the values
635 * we may run through dummy steps that cancel each other
636 * when stepping up and then down.
638 while (vdd_last > 0 &&
639 vdd_last < vdd_target) {
640 vdd_current += IR_VDD_STEP_UP;
641 vdd_last = set_voltage(i2caddress, vdd_current);
643 while (vdd_last > 0 &&
644 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
645 vdd_current -= IR_VDD_STEP_DOWN;
646 vdd_last = set_voltage(i2caddress, vdd_current);
650 printf("VID: Core voltage after adjustment is at %d mV\n",
658 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
664 static int print_vdd(void)
666 int vdd_last, ret, i2caddress;
668 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
670 debug("VID : I2c failed to switch channel\n");
673 ret = find_ir_chip_on_i2c();
675 printf("VID: Could not find voltage regulator on I2C.\n");
679 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
683 * Read voltage monitor to check real voltage.
685 vdd_last = read_voltage(i2caddress);
687 printf("VID: Couldn't read sensor abort VID adjustment\n");
690 printf("VID: Core voltage is at %d mV\n", vdd_last);
692 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
694 return ret < 0 ? -1 : 0;
698 static int do_vdd_override(cmd_tbl_t *cmdtp,
705 return CMD_RET_USAGE;
707 if (!strict_strtoul(argv[1], 10, &override))
708 adjust_vdd(override); /* the value is checked by callee */
710 return CMD_RET_USAGE;
714 static int do_vdd_read(cmd_tbl_t *cmdtp,
719 return CMD_RET_USAGE;
726 vdd_override, 2, 0, do_vdd_override,
728 " - override with the voltage specified in mV, eg. 1050"
732 vdd_read, 1, 0, do_vdd_read,
734 " - Read the voltage specified in mV"