1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014 Freescale Semiconductor, Inc.
5 * Copyright 2020 Stephen Carlson <stcarlso@linux.microsoft.com>
15 #ifdef CONFIG_FSL_LSCH2
16 #include <asm/arch/immap_lsch2.h>
17 #elif defined(CONFIG_FSL_LSCH3)
18 #include <asm/arch/immap_lsch3.h>
20 #include <asm/immap_85xx.h>
22 #include <linux/delay.h>
25 /* Voltages are generally handled in mV to keep them as integers */
29 * Select the channel on the I2C mux (on some NXP boards) that contains
30 * the voltage regulator to use for VID. Return 0 for success or nonzero
33 int __weak i2c_multiplexer_select_vid_channel(u8 channel)
39 * Compensate for a board specific voltage drop between regulator and SoC.
40 * Returns the voltage offset in mV.
42 int __weak board_vdd_drop_compensation(void)
48 * Performs any board specific adjustments after the VID voltage has been
49 * set. Return 0 for success or nonzero for failure.
51 int __weak board_adjust_vdd(int vdd)
57 * Processor specific method of converting the fuse value read from VID
58 * registers into the core voltage to supply. Return the voltage in mV.
60 u16 __weak soc_get_fuse_vid(int vid_index)
62 /* Default VDD for Layerscape Chassis 1 devices */
63 static const u16 vdd[32] = {
91 return vdd[vid_index];
94 #ifndef I2C_VOL_MONITOR_ADDR
95 #define I2C_VOL_MONITOR_ADDR 0
98 #if CONFIG_IS_ENABLED(DM_I2C)
99 #define DEVICE_HANDLE_T struct udevice *
101 #ifndef I2C_VOL_MONITOR_BUS
102 #define I2C_VOL_MONITOR_BUS 0
105 /* If DM is in use, retrieve the udevice chip for the specified bus number */
106 static int vid_get_device(int address, DEVICE_HANDLE_T *dev)
108 int ret = i2c_get_chip_for_busnum(I2C_VOL_MONITOR_BUS, address, 1, dev);
111 printf("VID: Bus %d has no device with address 0x%02X\n",
112 I2C_VOL_MONITOR_BUS, address);
116 #define I2C_READ(dev, register, data, length) \
117 dm_i2c_read(dev, register, data, length)
118 #define I2C_WRITE(dev, register, data, length) \
119 dm_i2c_write(dev, register, data, length)
121 #define DEVICE_HANDLE_T int
123 /* If DM is not in use, I2C addresses are passed directly */
124 static int vid_get_device(int address, DEVICE_HANDLE_T *dev)
130 #define I2C_READ(dev, register, data, length) \
131 i2c_read(dev, register, 1, data, length)
132 #define I2C_WRITE(dev, register, data, length) \
133 i2c_write(dev, register, 1, data, length)
136 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
137 defined(CONFIG_VOL_MONITOR_IR36021_READ)
139 * Get the i2c address configuration for the IR regulator chip
141 * There are some variance in the RDB HW regarding the I2C address configuration
142 * for the IR regulator chip, which is likely a problem of external resistor
143 * accuracy. So we just check each address in a hopefully non-intrusive mode
144 * and use the first one that seems to work
146 * The IR chip can show up under the following addresses:
147 * 0x08 (Verified on T1040RDB-PA,T4240RDB-PB,X-T4240RDB-16GPA)
148 * 0x09 (Verified on T1040RDB-PA)
149 * 0x38 (Verified on T2080QDS, T2081QDS, T4240RDB)
151 static int find_ir_chip_on_i2c(void)
153 int i2caddress, ret, i;
155 const int ir_i2c_addr[] = {0x38, 0x08, 0x09};
158 /* Check all the address */
159 for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) {
160 i2caddress = ir_i2c_addr[i];
161 ret = vid_get_device(i2caddress, &dev);
163 ret = I2C_READ(dev, IR36021_MFR_ID_OFFSET,
164 (void *)&mfrID, sizeof(mfrID));
165 /* If manufacturer ID matches the IR36021 */
166 if (!ret && mfrID == IR36021_MFR_ID)
174 /* Maximum loop count waiting for new voltage to take effect */
175 #define MAX_LOOP_WAIT_NEW_VOL 100
176 /* Maximum loop count waiting for the voltage to be stable */
177 #define MAX_LOOP_WAIT_VOL_STABLE 100
179 * read_voltage from sensor on I2C bus
180 * We use average of 4 readings, waiting for WAIT_FOR_ADC before
183 #define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
185 /* If an INA220 chip is available, we can use it to read back the voltage
186 * as it may have a higher accuracy than the IR chip for the same purpose
188 #ifdef CONFIG_VOL_MONITOR_INA220
189 #define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
190 #define ADC_MIN_ACCURACY 4
192 #define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
193 #define ADC_MIN_ACCURACY 4
196 #ifdef CONFIG_VOL_MONITOR_INA220
197 static int read_voltage_from_INA220(int i2caddress)
199 int i, ret, voltage_read = 0;
204 /* Open device handle */
205 ret = vid_get_device(i2caddress, &dev);
209 for (i = 0; i < NUM_READINGS; i++) {
210 ret = I2C_READ(dev, I2C_VOL_MONITOR_BUS_V_OFFSET,
211 (void *)&buf[0], sizeof(buf));
213 printf("VID: failed to read core voltage\n");
217 vol_mon = (buf[0] << 8) | buf[1];
218 if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
219 printf("VID: Core voltage sensor error\n");
223 debug("VID: bus voltage reads 0x%04x\n", vol_mon);
225 voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
226 udelay(WAIT_FOR_ADC);
229 /* calculate the average */
230 voltage_read /= NUM_READINGS;
236 #ifdef CONFIG_VOL_MONITOR_IR36021_READ
237 /* read voltage from IR */
238 static int read_voltage_from_IR(int i2caddress)
240 int i, ret, voltage_read = 0;
245 /* Open device handle */
246 ret = vid_get_device(i2caddress, &dev);
250 for (i = 0; i < NUM_READINGS; i++) {
251 ret = I2C_READ(dev, IR36021_LOOP1_VOUT_OFFSET, (void *)&buf,
254 printf("VID: failed to read core voltage\n");
259 printf("VID: Core voltage sensor error\n");
262 debug("VID: bus voltage reads 0x%02x\n", vol_mon);
263 /* Resolution is 1/128V. We scale up here to get 1/128mV
264 * and divide at the end
266 voltage_read += vol_mon * MV_PER_V;
267 udelay(WAIT_FOR_ADC);
269 /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
270 voltage_read = DIV_ROUND_UP(voltage_read, 128);
272 /* calculate the average */
273 voltage_read /= NUM_READINGS;
275 /* Compensate for a board specific voltage drop between regulator and
276 * SoC before converting into an IR VID value
278 voltage_read -= board_vdd_drop_compensation();
284 #if defined(CONFIG_VOL_MONITOR_ISL68233_READ) || \
285 defined(CONFIG_VOL_MONITOR_LTC3882_READ) || \
286 defined(CONFIG_VOL_MONITOR_ISL68233_SET) || \
287 defined(CONFIG_VOL_MONITOR_LTC3882_SET)
290 * The message displayed if the VOUT exponent causes a resolution
291 * worse than 1.0 V (if exponent is >= 0).
293 #define VOUT_WARNING "VID: VOUT_MODE exponent has resolution worse than 1 V!\n"
295 /* Checks the PMBus voltage monitor for the format used for voltage values */
296 static int get_pmbus_multiplier(DEVICE_HANDLE_T dev)
299 int exponent, multiplier, ret;
301 ret = I2C_READ(dev, PMBUS_CMD_VOUT_MODE, &mode, sizeof(mode));
303 printf("VID: unable to determine voltage multiplier\n");
307 /* Upper 3 bits is mode, lower 5 bits is exponent */
308 exponent = (int)mode & 0x1F;
312 /* Linear, 5 bit twos component exponent */
313 if (exponent & 0x10) {
314 multiplier = 1 << (16 - (exponent & 0xF));
316 /* If exponent is >= 0, then resolution is 1 V! */
317 printf(VOUT_WARNING);
322 /* VID code identifier */
323 printf("VID: custom VID codes are not supported\n");
324 multiplier = MV_PER_V;
328 multiplier = MV_PER_V;
332 debug("VID: calculated multiplier is %d\n", multiplier);
337 #if defined(CONFIG_VOL_MONITOR_ISL68233_READ) || \
338 defined(CONFIG_VOL_MONITOR_LTC3882_READ)
339 static int read_voltage_from_pmbus(int i2caddress)
341 int ret, multiplier, vout;
342 u8 channel = PWM_CHANNEL0;
346 /* Open device handle */
347 ret = vid_get_device(i2caddress, &dev);
351 /* Select the right page */
352 ret = I2C_WRITE(dev, PMBUS_CMD_PAGE, &channel, sizeof(channel));
354 printf("VID: failed to select VDD page %d\n", channel);
358 /* VOUT is little endian */
359 ret = I2C_READ(dev, PMBUS_CMD_READ_VOUT, (void *)&vcode, sizeof(vcode));
361 printf("VID: failed to read core voltage\n");
365 /* Scale down to the real mV */
366 multiplier = get_pmbus_multiplier(dev);
368 /* Multiplier 1000 (direct mode) requires no change to convert */
369 if (multiplier != MV_PER_V)
370 vout = DIV_ROUND_UP(vout * MV_PER_V, multiplier);
371 return vout - board_vdd_drop_compensation();
375 static int read_voltage(int i2caddress)
378 #ifdef CONFIG_VOL_MONITOR_INA220
379 voltage_read = read_voltage_from_INA220(I2C_VOL_MONITOR_ADDR);
380 #elif defined CONFIG_VOL_MONITOR_IR36021_READ
381 voltage_read = read_voltage_from_IR(i2caddress);
382 #elif defined(CONFIG_VOL_MONITOR_ISL68233_READ) || \
383 defined(CONFIG_VOL_MONITOR_LTC3882_READ)
384 voltage_read = read_voltage_from_pmbus(i2caddress);
391 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
393 * We need to calculate how long before the voltage stops to drop
394 * or increase. It returns with the loop count. Each loop takes
395 * several readings (WAIT_FOR_ADC)
397 static int wait_for_new_voltage(int vdd, int i2caddress)
399 int timeout, vdd_current;
401 vdd_current = read_voltage(i2caddress);
402 /* wait until voltage starts to reach the target. Voltage slew
403 * rates by typical regulators will always lead to stable readings
404 * within each fairly long ADC interval in comparison to the
405 * intended voltage delta change until the target voltage is
406 * reached. The fairly small voltage delta change to any target
407 * VID voltage also means that this function will always complete
408 * within few iterations. If the timeout was ever reached, it would
409 * point to a serious failure in the regulator system.
412 abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
413 timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
414 vdd_current = read_voltage(i2caddress);
416 if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
417 printf("VID: Voltage adjustment timeout\n");
424 * Blocks and reads the VID voltage until it stabilizes, or the
427 static int wait_for_voltage_stable(int i2caddress)
429 int timeout, vdd_current, vdd;
431 vdd = read_voltage(i2caddress);
432 udelay(NUM_READINGS * WAIT_FOR_ADC);
434 vdd_current = read_voltage(i2caddress);
436 * The maximum timeout is
437 * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
439 for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
440 abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
441 timeout > 0; timeout--) {
443 udelay(NUM_READINGS * WAIT_FOR_ADC);
444 vdd_current = read_voltage(i2caddress);
451 /* Sets the VID voltage using the IR36021 */
452 static int set_voltage_to_IR(int i2caddress, int vdd)
459 /* Open device handle */
460 ret = vid_get_device(i2caddress, &dev);
464 /* Compensate for a board specific voltage drop between regulator and
465 * SoC before converting into an IR VID value
467 vdd += board_vdd_drop_compensation();
468 #ifdef CONFIG_FSL_LSCH2
469 vid = DIV_ROUND_UP(vdd - 265, 5);
471 vid = DIV_ROUND_UP(vdd - 245, 5);
474 ret = I2C_WRITE(dev, IR36021_LOOP1_MANUAL_ID_OFFSET, (void *)&vid,
477 printf("VID: failed to write new voltage\n");
480 wait = wait_for_new_voltage(vdd, i2caddress);
483 debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
485 vdd_last = wait_for_voltage_stable(i2caddress);
488 debug("VID: Current voltage is %d mV\n", vdd_last);
493 #if defined(CONFIG_VOL_MONITOR_ISL68233_SET) || \
494 defined(CONFIG_VOL_MONITOR_LTC3882_SET)
495 static int set_voltage_to_pmbus(int i2caddress, int vdd)
497 int ret, vdd_last, vdd_target = vdd;
498 int count = MAX_LOOP_WAIT_NEW_VOL, temp = 0, multiplier;
501 /* The data to be sent with the PMBus command PAGE_PLUS_WRITE */
502 u8 buffer[5] = { 0x04, PWM_CHANNEL0, PMBUS_CMD_VOUT_COMMAND, 0, 0 };
505 /* Open device handle */
506 ret = vid_get_device(i2caddress, &dev);
510 /* Scale up to the proper value for the VOUT command, little endian */
511 multiplier = get_pmbus_multiplier(dev);
512 vdd += board_vdd_drop_compensation();
513 if (multiplier != MV_PER_V)
514 vdd = DIV_ROUND_UP(vdd * multiplier, MV_PER_V);
515 buffer[3] = vdd & 0xFF;
516 buffer[4] = (vdd & 0xFF00) >> 8;
518 /* Check write protect state */
519 ret = I2C_READ(dev, PMBUS_CMD_WRITE_PROTECT, (void *)&value,
524 if (value != EN_WRITE_ALL_CMD) {
525 value = EN_WRITE_ALL_CMD;
526 ret = I2C_WRITE(dev, PMBUS_CMD_WRITE_PROTECT,
527 (void *)&value, sizeof(value));
532 /* Write the desired voltage code to the regulator */
533 ret = I2C_WRITE(dev, PMBUS_CMD_PAGE_PLUS_WRITE, (void *)&buffer[0],
536 printf("VID: I2C failed to write to the voltage regulator\n");
541 /* Wait for the voltage to get to the desired value */
543 vdd_last = read_voltage_from_pmbus(i2caddress);
545 printf("VID: Couldn't read sensor abort VID adjust\n");
549 temp = vdd_last - vdd_target;
550 } while ((abs(temp) > 2) && (count > 0));
556 static int set_voltage(int i2caddress, int vdd)
560 #ifdef CONFIG_VOL_MONITOR_IR36021_SET
561 vdd_last = set_voltage_to_IR(i2caddress, vdd);
562 #elif defined(CONFIG_VOL_MONITOR_ISL68233_SET) || \
563 defined(CONFIG_VOL_MONITOR_LTC3882_SET)
564 vdd_last = set_voltage_to_pmbus(i2caddress, vdd);
566 #error Specific voltage monitor must be defined
571 int adjust_vdd(ulong vdd_override)
573 int re_enable = disable_interrupts();
574 #if defined(CONFIG_FSL_LSCH2) || defined(CONFIG_FSL_LSCH3)
575 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
577 ccsr_gur_t __iomem *gur =
578 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
582 int vdd_current, vdd_last, vdd_target;
583 int ret, i2caddress = I2C_VOL_MONITOR_ADDR;
584 unsigned long vdd_string_override;
587 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
588 defined(CONFIG_VOL_MONITOR_IR36021_READ)
594 * VID is used according to the table below
595 * ---------------------------------------
597 * |-------------------------------------|
598 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
599 * ---------------+---------+-----------------+---------|
600 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
601 * | A |----------+---------+-----------------+---------|
602 * | _ | 5b00001 |VID = | VID = |VID = |
603 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
604 * | _ | 5b11110 | | | |
605 * | A |----------+---------+-----------------+---------|
606 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
608 * ------------------------------------------------------
610 #if defined(CONFIG_FSL_LSCH3)
611 fusesr = in_le32(&gur->dcfg_fusesr);
612 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
613 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
614 if (vid == 0 || vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK) {
615 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
616 FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
618 #elif defined(CONFIG_FSL_LSCH2)
619 fusesr = in_be32(&gur->dcfg_fusesr);
620 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
621 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
622 if (vid == 0 || vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK) {
623 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
624 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
627 fusesr = in_be32(&gur->dcfg_fusesr);
628 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
629 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
630 if (vid == 0 || vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK) {
631 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
632 FSL_CORENET_DCFG_FUSESR_VID_MASK;
635 vdd_target = soc_get_fuse_vid((int)vid);
637 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
639 debug("VID: I2C failed to switch channel\n");
644 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
645 defined(CONFIG_VOL_MONITOR_IR36021_READ)
646 ret = find_ir_chip_on_i2c();
648 printf("VID: Could not find voltage regulator on I2C.\n");
653 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
656 ret = vid_get_device(i2caddress, &dev);
660 /* check IR chip work on Intel mode */
661 ret = I2C_READ(dev, IR36021_INTEL_MODE_OFFSET, (void *)&buf,
664 printf("VID: failed to read IR chip mode.\n");
668 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
669 printf("VID: IR Chip is not used in Intel mode.\n");
675 /* check override variable for overriding VDD */
676 vdd_string = env_get(CONFIG_VID_FLS_ENV);
677 debug("VID: Initial VDD value is %d mV\n",
678 DIV_ROUND_UP(vdd_target, 10));
679 if (vdd_override == 0 && vdd_string &&
680 !strict_strtoul(vdd_string, 10, &vdd_string_override))
681 vdd_override = vdd_string_override;
682 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
683 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
684 debug("VID: VDD override is %lu\n", vdd_override);
685 } else if (vdd_override != 0) {
686 printf("VID: Invalid VDD value.\n");
688 if (vdd_target == 0) {
689 debug("VID: VID not used\n");
693 /* divide and round up by 10 to get a value in mV */
694 vdd_target = DIV_ROUND_UP(vdd_target, 10);
695 debug("VID: vid = %d mV\n", vdd_target);
699 * Read voltage monitor to check real voltage.
701 vdd_last = read_voltage(i2caddress);
703 printf("VID: Couldn't read sensor abort VID adjustment\n");
707 vdd_current = vdd_last;
708 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
710 #if defined(CONFIG_VOL_MONITOR_LTC3882_SET) || \
711 defined(CONFIG_VOL_MONITOR_ISL68233_SET)
712 /* Set the target voltage */
713 vdd_current = set_voltage(i2caddress, vdd_target);
714 vdd_last = vdd_current;
717 * Adjust voltage to at or one step above target.
718 * As measurements are less precise than setting the values
719 * we may run through dummy steps that cancel each other
720 * when stepping up and then down.
722 while (vdd_last > 0 &&
723 vdd_last < vdd_target) {
724 vdd_current += IR_VDD_STEP_UP;
725 vdd_last = set_voltage(i2caddress, vdd_current);
727 while (vdd_last > 0 &&
728 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
729 vdd_current -= IR_VDD_STEP_DOWN;
730 vdd_last = set_voltage(i2caddress, vdd_current);
734 /* Board specific adjustments */
735 if (board_adjust_vdd(vdd_target) < 0) {
741 printf("VID: Core voltage after adjustment is at %d mV\n",
749 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
754 static int print_vdd(void)
756 int vdd_last, ret, i2caddress = I2C_VOL_MONITOR_ADDR;
758 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
760 debug("VID : I2c failed to switch channel\n");
763 #if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
764 defined(CONFIG_VOL_MONITOR_IR36021_READ)
765 ret = find_ir_chip_on_i2c();
767 printf("VID: Could not find voltage regulator on I2C.\n");
771 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
776 * Read voltage monitor to check real voltage.
778 vdd_last = read_voltage(i2caddress);
780 printf("VID: Couldn't read sensor abort VID adjustment\n");
783 printf("VID: Core voltage is at %d mV\n", vdd_last);
785 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
787 return ret < 0 ? -1 : 0;
791 static int do_vdd_override(struct cmd_tbl *cmdtp,
798 return CMD_RET_USAGE;
800 if (!strict_strtoul(argv[1], 10, &override))
801 adjust_vdd(override); /* the value is checked by callee */
803 return CMD_RET_USAGE;
807 static int do_vdd_read(struct cmd_tbl *cmdtp, int flag, int argc,
811 return CMD_RET_USAGE;
818 vdd_override, 2, 0, do_vdd_override,
820 " - override with the voltage specified in mV, eg. 1050"
824 vdd_read, 1, 0, do_vdd_read,
826 " - Read the voltage specified in mV"