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)
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))
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 static int read_voltage(int i2caddress)
177 #ifdef CONFIG_VOL_MONITOR_INA220
178 voltage_read = read_voltage_from_INA220(i2caddress);
179 #elif defined CONFIG_VOL_MONITOR_IR36021_READ
180 voltage_read = read_voltage_from_IR(i2caddress);
188 * We need to calculate how long before the voltage stops to drop
189 * or increase. It returns with the loop count. Each loop takes
190 * several readings (WAIT_FOR_ADC)
192 static int wait_for_new_voltage(int vdd, int i2caddress)
194 int timeout, vdd_current;
196 vdd_current = read_voltage(i2caddress);
197 /* wait until voltage starts to reach the target. Voltage slew
198 * rates by typical regulators will always lead to stable readings
199 * within each fairly long ADC interval in comparison to the
200 * intended voltage delta change until the target voltage is
201 * reached. The fairly small voltage delta change to any target
202 * VID voltage also means that this function will always complete
203 * within few iterations. If the timeout was ever reached, it would
204 * point to a serious failure in the regulator system.
207 abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
208 timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
209 vdd_current = read_voltage(i2caddress);
211 if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
212 printf("VID: Voltage adjustment timeout\n");
219 * this function keeps reading the voltage until it is stable or until the
222 static int wait_for_voltage_stable(int i2caddress)
224 int timeout, vdd_current, vdd;
226 vdd = read_voltage(i2caddress);
227 udelay(NUM_READINGS * WAIT_FOR_ADC);
229 /* wait until voltage is stable */
230 vdd_current = read_voltage(i2caddress);
231 /* The maximum timeout is
232 * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
234 for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
235 abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
236 timeout > 0; timeout--) {
238 udelay(NUM_READINGS * WAIT_FOR_ADC);
239 vdd_current = read_voltage(i2caddress);
246 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
247 /* Set the voltage to the IR chip */
248 static int set_voltage_to_IR(int i2caddress, int vdd)
254 /* Compensate for a board specific voltage drop between regulator and
255 * SoC before converting into an IR VID value
257 vdd += board_vdd_drop_compensation();
258 #ifdef CONFIG_FSL_LSCH2
259 vid = DIV_ROUND_UP(vdd - 265, 5);
261 vid = DIV_ROUND_UP(vdd - 245, 5);
264 ret = i2c_write(i2caddress, IR36021_LOOP1_MANUAL_ID_OFFSET,
265 1, (void *)&vid, sizeof(vid));
267 printf("VID: failed to write VID\n");
270 wait = wait_for_new_voltage(vdd, i2caddress);
273 debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
275 vdd_last = wait_for_voltage_stable(i2caddress);
278 debug("VID: Current voltage is %d mV\n", vdd_last);
283 static int set_voltage(int i2caddress, int vdd)
287 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
288 vdd_last = set_voltage_to_IR(i2caddress, vdd);
290 #error Specific voltage monitor must be defined
295 #ifdef CONFIG_FSL_LSCH3
296 int adjust_vdd(ulong vdd_override)
298 int re_enable = disable_interrupts();
299 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
302 int vdd_target, vdd_current, vdd_last;
304 unsigned long vdd_string_override;
306 #ifdef CONFIG_ARCH_LS1088A
307 static const uint16_t vdd[32] = {
343 static const uint16_t vdd[32] = {
383 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
385 debug("VID: I2C failed to switch channel\n");
389 ret = find_ir_chip_on_i2c();
391 printf("VID: Could not find voltage regulator on I2C.\n");
396 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
399 /* check IR chip work on Intel mode*/
400 ret = i2c_read(i2caddress,
401 IR36021_INTEL_MODE_OOFSET,
404 printf("VID: failed to read IR chip mode.\n");
408 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
409 printf("VID: IR Chip is not used in Intel mode.\n");
414 /* get the voltage ID from fuse status register */
415 fusesr = in_le32(&gur->dcfg_fusesr);
416 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
417 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
418 if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
419 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
420 FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
422 vdd_target = vdd[vid];
424 /* check override variable for overriding VDD */
425 vdd_string = env_get(CONFIG_VID_FLS_ENV);
426 if (vdd_override == 0 && vdd_string &&
427 !strict_strtoul(vdd_string, 10, &vdd_string_override))
428 vdd_override = vdd_string_override;
430 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
431 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
432 debug("VDD override is %lu\n", vdd_override);
433 } else if (vdd_override != 0) {
434 printf("Invalid value.\n");
437 /* divide and round up by 10 to get a value in mV */
438 vdd_target = DIV_ROUND_UP(vdd_target, 10);
439 if (vdd_target == 0) {
440 debug("VID: VID not used\n");
443 } else if (vdd_target < VDD_MV_MIN || vdd_target > VDD_MV_MAX) {
444 /* Check vdd_target is in valid range */
445 printf("VID: Target VID %d mV is not in range.\n",
450 debug("VID: vid = %d mV\n", vdd_target);
454 * Read voltage monitor to check real voltage.
456 vdd_last = read_voltage(i2caddress);
458 printf("VID: Couldn't read sensor abort VID adjustment\n");
462 vdd_current = vdd_last;
463 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
465 * Adjust voltage to at or one step above target.
466 * As measurements are less precise than setting the values
467 * we may run through dummy steps that cancel each other
468 * when stepping up and then down.
470 while (vdd_last > 0 &&
471 vdd_last < vdd_target) {
472 vdd_current += IR_VDD_STEP_UP;
473 vdd_last = set_voltage(i2caddress, vdd_current);
475 while (vdd_last > 0 &&
476 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
477 vdd_current -= IR_VDD_STEP_DOWN;
478 vdd_last = set_voltage(i2caddress, vdd_current);
481 if (board_adjust_vdd(vdd_target) < 0) {
487 printf("VID: Core voltage after adjustment is at %d mV\n",
494 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
497 #else /* !CONFIG_FSL_LSCH3 */
498 int adjust_vdd(ulong vdd_override)
500 int re_enable = disable_interrupts();
501 #if defined(CONFIG_FSL_LSCH2)
502 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
504 ccsr_gur_t __iomem *gur =
505 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
509 int vdd_target, vdd_current, vdd_last;
511 unsigned long vdd_string_override;
513 static const uint16_t vdd[32] = {
546 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
548 debug("VID: I2C failed to switch channel\n");
552 ret = find_ir_chip_on_i2c();
554 printf("VID: Could not find voltage regulator on I2C.\n");
559 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
562 /* check IR chip work on Intel mode*/
563 ret = i2c_read(i2caddress,
564 IR36021_INTEL_MODE_OOFSET,
567 printf("VID: failed to read IR chip mode.\n");
571 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
572 printf("VID: IR Chip is not used in Intel mode.\n");
577 /* get the voltage ID from fuse status register */
578 fusesr = in_be32(&gur->dcfg_fusesr);
580 * VID is used according to the table below
581 * ---------------------------------------
583 * |-------------------------------------|
584 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
585 * ---------------+---------+-----------------+---------|
586 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
587 * | A |----------+---------+-----------------+---------|
588 * | _ | 5b00001 |VID = | VID = |VID = |
589 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
590 * | _ | 5b11110 | | | |
591 * | A |----------+---------+-----------------+---------|
592 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
594 * ------------------------------------------------------
596 #ifdef CONFIG_FSL_LSCH2
597 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
598 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
599 if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
600 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
601 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
604 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
605 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
606 if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
607 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
608 FSL_CORENET_DCFG_FUSESR_VID_MASK;
611 vdd_target = vdd[vid];
613 /* check override variable for overriding VDD */
614 vdd_string = env_get(CONFIG_VID_FLS_ENV);
615 if (vdd_override == 0 && vdd_string &&
616 !strict_strtoul(vdd_string, 10, &vdd_string_override))
617 vdd_override = vdd_string_override;
618 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
619 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
620 debug("VDD override is %lu\n", vdd_override);
621 } else if (vdd_override != 0) {
622 printf("Invalid value.\n");
624 if (vdd_target == 0) {
625 debug("VID: VID not used\n");
629 /* divide and round up by 10 to get a value in mV */
630 vdd_target = DIV_ROUND_UP(vdd_target, 10);
631 debug("VID: vid = %d mV\n", vdd_target);
635 * Read voltage monitor to check real voltage.
637 vdd_last = read_voltage(i2caddress);
639 printf("VID: Couldn't read sensor abort VID adjustment\n");
643 vdd_current = vdd_last;
644 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
646 * Adjust voltage to at or one step above target.
647 * As measurements are less precise than setting the values
648 * we may run through dummy steps that cancel each other
649 * when stepping up and then down.
651 while (vdd_last > 0 &&
652 vdd_last < vdd_target) {
653 vdd_current += IR_VDD_STEP_UP;
654 vdd_last = set_voltage(i2caddress, vdd_current);
656 while (vdd_last > 0 &&
657 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
658 vdd_current -= IR_VDD_STEP_DOWN;
659 vdd_last = set_voltage(i2caddress, vdd_current);
663 printf("VID: Core voltage after adjustment is at %d mV\n",
671 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
677 static int print_vdd(void)
679 int vdd_last, ret, i2caddress;
681 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
683 debug("VID : I2c failed to switch channel\n");
686 ret = find_ir_chip_on_i2c();
688 printf("VID: Could not find voltage regulator on I2C.\n");
692 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
696 * Read voltage monitor to check real voltage.
698 vdd_last = read_voltage(i2caddress);
700 printf("VID: Couldn't read sensor abort VID adjustment\n");
703 printf("VID: Core voltage is at %d mV\n", vdd_last);
705 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
707 return ret < 0 ? -1 : 0;
711 static int do_vdd_override(cmd_tbl_t *cmdtp,
718 return CMD_RET_USAGE;
720 if (!strict_strtoul(argv[1], 10, &override))
721 adjust_vdd(override); /* the value is checked by callee */
723 return CMD_RET_USAGE;
727 static int do_vdd_read(cmd_tbl_t *cmdtp,
732 return CMD_RET_USAGE;
739 vdd_override, 2, 0, do_vdd_override,
741 " - override with the voltage specified in mV, eg. 1050"
745 vdd_read, 1, 0, do_vdd_read,
747 " - Read the voltage specified in mV"