blob: 6e8296293b89fe276dd89bf9a636d1b1faba5c33 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ying Zhang8876a512014-10-31 18:06:18 +08002/*
3 * Copyright 2014 Freescale Semiconductor, Inc.
Meenakshi Aggarwalccb5d5d2020-10-29 19:16:16 +05304 * Copyright 2020 NXP
Stephen Carlsonc3301a22021-02-08 11:11:29 +01005 * Copyright 2020 Stephen Carlson <stcarlso@linux.microsoft.com>
Ying Zhang8876a512014-10-31 18:06:18 +08006 */
7
8#include <common.h>
9#include <command.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060010#include <env.h>
Ying Zhang8876a512014-10-31 18:06:18 +080011#include <i2c.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070012#include <irq_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Shaohui Xiedd335672015-11-11 17:58:37 +080014#include <asm/io.h>
Shaohui Xie085ac1c2016-09-07 17:56:14 +080015#ifdef CONFIG_FSL_LSCH2
Shaohui Xiedd335672015-11-11 17:58:37 +080016#include <asm/arch/immap_lsch2.h>
Rai Harninder6aa1f3b2016-03-23 17:04:38 +053017#elif defined(CONFIG_FSL_LSCH3)
18#include <asm/arch/immap_lsch3.h>
Shaohui Xiedd335672015-11-11 17:58:37 +080019#else
Ying Zhang8876a512014-10-31 18:06:18 +080020#include <asm/immap_85xx.h>
Shaohui Xiedd335672015-11-11 17:58:37 +080021#endif
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
Ying Zhang8876a512014-10-31 18:06:18 +080023#include "vid.h"
24
Stephen Carlsonc3301a22021-02-08 11:11:29 +010025/* Voltages are generally handled in mV to keep them as integers */
26#define MV_PER_V 1000
27
28/*
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
31 * for failure.
32 */
Ying Zhang8876a512014-10-31 18:06:18 +080033int __weak i2c_multiplexer_select_vid_channel(u8 channel)
34{
35 return 0;
36}
37
38/*
Stephen Carlsonc3301a22021-02-08 11:11:29 +010039 * Compensate for a board specific voltage drop between regulator and SoC.
40 * Returns the voltage offset in mV.
Ying Zhang8876a512014-10-31 18:06:18 +080041 */
42int __weak board_vdd_drop_compensation(void)
43{
44 return 0;
45}
46
47/*
Stephen Carlsonc3301a22021-02-08 11:11:29 +010048 * Performs any board specific adjustments after the VID voltage has been
49 * set. Return 0 for success or nonzero for failure.
Rajesh Bhagatc3a662f2018-01-17 16:13:02 +053050 */
51int __weak board_adjust_vdd(int vdd)
52{
Stephen Carlsonc3301a22021-02-08 11:11:29 +010053 return 0;
54}
55
56/*
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.
59 */
60u16 __weak soc_get_fuse_vid(int vid_index)
61{
62 /* Default VDD for Layerscape Chassis 1 devices */
63 static const u16 vdd[32] = {
64 0, /* unused */
65 9875, /* 0.9875V */
66 9750,
67 9625,
68 9500,
69 9375,
70 9250,
71 9125,
72 9000,
73 8875,
74 8750,
75 8625,
76 8500,
77 8375,
78 8250,
79 8125,
80 10000, /* 1.0000V */
81 10125,
82 10250,
83 10375,
84 10500,
85 10625,
86 10750,
87 10875,
88 11000,
89 0, /* reserved */
90 };
91 return vdd[vid_index];
92}
93
94#ifndef I2C_VOL_MONITOR_ADDR
95#define I2C_VOL_MONITOR_ADDR 0
96#endif
97
98#if CONFIG_IS_ENABLED(DM_I2C)
99#define DEVICE_HANDLE_T struct udevice *
100
101#ifndef I2C_VOL_MONITOR_BUS
102#define I2C_VOL_MONITOR_BUS 0
103#endif
104
105/* If DM is in use, retrieve the udevice chip for the specified bus number */
106static int vid_get_device(int address, DEVICE_HANDLE_T *dev)
107{
108 int ret = i2c_get_chip_for_busnum(I2C_VOL_MONITOR_BUS, address, 1, dev);
109
110 if (ret)
111 printf("VID: Bus %d has no device with address 0x%02X\n",
112 I2C_VOL_MONITOR_BUS, address);
113 return ret;
114}
115
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)
120#else
121#define DEVICE_HANDLE_T int
122
123/* If DM is not in use, I2C addresses are passed directly */
124static int vid_get_device(int address, DEVICE_HANDLE_T *dev)
125{
126 *dev = address;
Rajesh Bhagatc3a662f2018-01-17 16:13:02 +0530127 return 0;
128}
129
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100130#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)
134#endif
135
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530136#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
137 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Rajesh Bhagatc3a662f2018-01-17 16:13:02 +0530138/*
Ying Zhang8876a512014-10-31 18:06:18 +0800139 * Get the i2c address configuration for the IR regulator chip
140 *
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
145 *
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)
Ying Zhangff779052016-01-22 12:15:13 +0800149 * 0x38 (Verified on T2080QDS, T2081QDS, T4240RDB)
Ying Zhang8876a512014-10-31 18:06:18 +0800150 */
151static int find_ir_chip_on_i2c(void)
152{
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100153 int i2caddress, ret, i;
154 u8 mfrID;
Ying Zhang8876a512014-10-31 18:06:18 +0800155 const int ir_i2c_addr[] = {0x38, 0x08, 0x09};
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100156 DEVICE_HANDLE_T dev;
Ying Zhang8876a512014-10-31 18:06:18 +0800157
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];
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100161 ret = vid_get_device(i2caddress, &dev);
162 if (!ret) {
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)
167 return i2caddress;
168 }
Ying Zhang8876a512014-10-31 18:06:18 +0800169 }
170 return -1;
171}
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530172#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800173
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
178/*
179 * read_voltage from sensor on I2C bus
180 * We use average of 4 readings, waiting for WAIT_FOR_ADC before
181 * another reading
182 */
183#define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
184
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
187 */
188#ifdef CONFIG_VOL_MONITOR_INA220
189#define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
190#define ADC_MIN_ACCURACY 4
191#else
192#define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
193#define ADC_MIN_ACCURACY 4
194#endif
195
196#ifdef CONFIG_VOL_MONITOR_INA220
197static int read_voltage_from_INA220(int i2caddress)
198{
199 int i, ret, voltage_read = 0;
200 u16 vol_mon;
201 u8 buf[2];
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100202 DEVICE_HANDLE_T dev;
203
204 /* Open device handle */
205 ret = vid_get_device(i2caddress, &dev);
206 if (ret)
207 return ret;
Ying Zhang8876a512014-10-31 18:06:18 +0800208
209 for (i = 0; i < NUM_READINGS; i++) {
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100210 ret = I2C_READ(dev, I2C_VOL_MONITOR_BUS_V_OFFSET,
211 (void *)&buf[0], sizeof(buf));
Ying Zhang8876a512014-10-31 18:06:18 +0800212 if (ret) {
213 printf("VID: failed to read core voltage\n");
214 return ret;
215 }
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100216
Ying Zhang8876a512014-10-31 18:06:18 +0800217 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");
220 return -1;
221 }
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100222
Ying Zhang8876a512014-10-31 18:06:18 +0800223 debug("VID: bus voltage reads 0x%04x\n", vol_mon);
224 /* LSB = 4mv */
225 voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
226 udelay(WAIT_FOR_ADC);
227 }
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100228
Ying Zhang8876a512014-10-31 18:06:18 +0800229 /* calculate the average */
230 voltage_read /= NUM_READINGS;
231
232 return voltage_read;
233}
234#endif
235
Ying Zhang8876a512014-10-31 18:06:18 +0800236#ifdef CONFIG_VOL_MONITOR_IR36021_READ
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100237/* read voltage from IR */
Ying Zhang8876a512014-10-31 18:06:18 +0800238static int read_voltage_from_IR(int i2caddress)
239{
240 int i, ret, voltage_read = 0;
241 u16 vol_mon;
242 u8 buf;
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100243 DEVICE_HANDLE_T dev;
244
245 /* Open device handle */
246 ret = vid_get_device(i2caddress, &dev);
247 if (ret)
248 return ret;
Ying Zhang8876a512014-10-31 18:06:18 +0800249
250 for (i = 0; i < NUM_READINGS; i++) {
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100251 ret = I2C_READ(dev, IR36021_LOOP1_VOUT_OFFSET, (void *)&buf,
252 sizeof(buf));
Ying Zhang8876a512014-10-31 18:06:18 +0800253 if (ret) {
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100254 printf("VID: failed to read core voltage\n");
Ying Zhang8876a512014-10-31 18:06:18 +0800255 return ret;
256 }
257 vol_mon = buf;
258 if (!vol_mon) {
259 printf("VID: Core voltage sensor error\n");
260 return -1;
261 }
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
265 */
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100266 voltage_read += vol_mon * MV_PER_V;
Ying Zhang8876a512014-10-31 18:06:18 +0800267 udelay(WAIT_FOR_ADC);
268 }
269 /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
270 voltage_read = DIV_ROUND_UP(voltage_read, 128);
271
272 /* calculate the average */
273 voltage_read /= NUM_READINGS;
274
275 /* Compensate for a board specific voltage drop between regulator and
276 * SoC before converting into an IR VID value
277 */
278 voltage_read -= board_vdd_drop_compensation();
279
280 return voltage_read;
281}
282#endif
283
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100284#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)
288
289/*
290 * The message displayed if the VOUT exponent causes a resolution
291 * worse than 1.0 V (if exponent is >= 0).
292 */
293#define VOUT_WARNING "VID: VOUT_MODE exponent has resolution worse than 1 V!\n"
294
295/* Checks the PMBus voltage monitor for the format used for voltage values */
296static int get_pmbus_multiplier(DEVICE_HANDLE_T dev)
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530297{
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100298 u8 mode;
299 int exponent, multiplier, ret;
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530300
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100301 ret = I2C_READ(dev, PMBUS_CMD_VOUT_MODE, &mode, sizeof(mode));
302 if (ret) {
303 printf("VID: unable to determine voltage multiplier\n");
304 return 1;
305 }
306
307 /* Upper 3 bits is mode, lower 5 bits is exponent */
308 exponent = (int)mode & 0x1F;
309 mode >>= 5;
310 switch (mode) {
311 case 0:
312 /* Linear, 5 bit twos component exponent */
313 if (exponent & 0x10) {
314 multiplier = 1 << (16 - (exponent & 0xF));
315 } else {
316 /* If exponent is >= 0, then resolution is 1 V! */
317 printf(VOUT_WARNING);
318 multiplier = 1;
319 }
320 break;
321 case 1:
322 /* VID code identifier */
323 printf("VID: custom VID codes are not supported\n");
324 multiplier = MV_PER_V;
325 break;
326 default:
327 /* Direct, in mV */
328 multiplier = MV_PER_V;
329 break;
330 }
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800331
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100332 debug("VID: calculated multiplier is %d\n", multiplier);
333 return multiplier;
334}
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800335#endif
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100336
337#if defined(CONFIG_VOL_MONITOR_ISL68233_READ) || \
338 defined(CONFIG_VOL_MONITOR_LTC3882_READ)
339static int read_voltage_from_pmbus(int i2caddress)
340{
341 int ret, multiplier, vout;
342 u8 channel = PWM_CHANNEL0;
343 u16 vcode;
344 DEVICE_HANDLE_T dev;
345
346 /* Open device handle */
347 ret = vid_get_device(i2caddress, &dev);
348 if (ret)
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530349 return ret;
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530350
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100351 /* Select the right page */
352 ret = I2C_WRITE(dev, PMBUS_CMD_PAGE, &channel, sizeof(channel));
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800353 if (ret) {
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100354 printf("VID: failed to select VDD page %d\n", channel);
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800355 return ret;
356 }
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100357
358 /* VOUT is little endian */
359 ret = I2C_READ(dev, PMBUS_CMD_READ_VOUT, (void *)&vcode, sizeof(vcode));
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530360 if (ret) {
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100361 printf("VID: failed to read core voltage\n");
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530362 return ret;
363 }
364
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100365 /* Scale down to the real mV */
366 multiplier = get_pmbus_multiplier(dev);
367 vout = (int)vcode;
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();
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530372}
373#endif
374
Ying Zhang8876a512014-10-31 18:06:18 +0800375static int read_voltage(int i2caddress)
376{
377 int voltage_read;
378#ifdef CONFIG_VOL_MONITOR_INA220
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100379 voltage_read = read_voltage_from_INA220(I2C_VOL_MONITOR_ADDR);
Ying Zhang8876a512014-10-31 18:06:18 +0800380#elif defined CONFIG_VOL_MONITOR_IR36021_READ
381 voltage_read = read_voltage_from_IR(i2caddress);
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100382#elif defined(CONFIG_VOL_MONITOR_ISL68233_READ) || \
383 defined(CONFIG_VOL_MONITOR_LTC3882_READ)
384 voltage_read = read_voltage_from_pmbus(i2caddress);
Ying Zhang8876a512014-10-31 18:06:18 +0800385#else
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100386 voltage_read = -1;
Ying Zhang8876a512014-10-31 18:06:18 +0800387#endif
388 return voltage_read;
389}
390
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530391#ifdef CONFIG_VOL_MONITOR_IR36021_SET
Ying Zhang8876a512014-10-31 18:06:18 +0800392/*
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)
396 */
397static int wait_for_new_voltage(int vdd, int i2caddress)
398{
399 int timeout, vdd_current;
400
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.
410 */
411 for (timeout = 0;
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);
415 }
416 if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
417 printf("VID: Voltage adjustment timeout\n");
418 return -1;
419 }
420 return timeout;
421}
422
423/*
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100424 * Blocks and reads the VID voltage until it stabilizes, or the
Ying Zhang8876a512014-10-31 18:06:18 +0800425 * timeout expires
426 */
427static int wait_for_voltage_stable(int i2caddress)
428{
429 int timeout, vdd_current, vdd;
430
431 vdd = read_voltage(i2caddress);
432 udelay(NUM_READINGS * WAIT_FOR_ADC);
433
Ying Zhang8876a512014-10-31 18:06:18 +0800434 vdd_current = read_voltage(i2caddress);
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100435 /*
436 * The maximum timeout is
Ying Zhang8876a512014-10-31 18:06:18 +0800437 * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
438 */
439 for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
440 abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
441 timeout > 0; timeout--) {
442 vdd = vdd_current;
443 udelay(NUM_READINGS * WAIT_FOR_ADC);
444 vdd_current = read_voltage(i2caddress);
445 }
446 if (timeout == 0)
447 return -1;
448 return vdd_current;
449}
450
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100451/* Sets the VID voltage using the IR36021 */
Ying Zhang8876a512014-10-31 18:06:18 +0800452static int set_voltage_to_IR(int i2caddress, int vdd)
453{
454 int wait, vdd_last;
455 int ret;
456 u8 vid;
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100457 DEVICE_HANDLE_T dev;
458
459 /* Open device handle */
460 ret = vid_get_device(i2caddress, &dev);
461 if (ret)
462 return ret;
Ying Zhang8876a512014-10-31 18:06:18 +0800463
464 /* Compensate for a board specific voltage drop between regulator and
465 * SoC before converting into an IR VID value
466 */
467 vdd += board_vdd_drop_compensation();
Shaohui Xie085ac1c2016-09-07 17:56:14 +0800468#ifdef CONFIG_FSL_LSCH2
Shaohui Xiedd335672015-11-11 17:58:37 +0800469 vid = DIV_ROUND_UP(vdd - 265, 5);
470#else
Ying Zhang8876a512014-10-31 18:06:18 +0800471 vid = DIV_ROUND_UP(vdd - 245, 5);
Shaohui Xiedd335672015-11-11 17:58:37 +0800472#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800473
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100474 ret = I2C_WRITE(dev, IR36021_LOOP1_MANUAL_ID_OFFSET, (void *)&vid,
475 sizeof(vid));
Ying Zhang8876a512014-10-31 18:06:18 +0800476 if (ret) {
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100477 printf("VID: failed to write new voltage\n");
Ying Zhang8876a512014-10-31 18:06:18 +0800478 return -1;
479 }
480 wait = wait_for_new_voltage(vdd, i2caddress);
481 if (wait < 0)
482 return -1;
483 debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
484
485 vdd_last = wait_for_voltage_stable(i2caddress);
486 if (vdd_last < 0)
487 return -1;
488 debug("VID: Current voltage is %d mV\n", vdd_last);
489 return vdd_last;
490}
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530491#endif
492
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100493#if defined(CONFIG_VOL_MONITOR_ISL68233_SET) || \
494 defined(CONFIG_VOL_MONITOR_LTC3882_SET)
495static int set_voltage_to_pmbus(int i2caddress, int vdd)
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530496{
497 int ret, vdd_last, vdd_target = vdd;
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100498 int count = MAX_LOOP_WAIT_NEW_VOL, temp = 0, multiplier;
Biwen Li71ecd382020-10-12 20:07:35 +0800499 unsigned char value;
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530500
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100501 /* 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 };
503 DEVICE_HANDLE_T dev;
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530504
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100505 /* Open device handle */
506 ret = vid_get_device(i2caddress, &dev);
507 if (ret)
508 return ret;
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530509
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100510 /* 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;
517
Biwen Li71ecd382020-10-12 20:07:35 +0800518 /* Check write protect state */
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100519 ret = I2C_READ(dev, PMBUS_CMD_WRITE_PROTECT, (void *)&value,
520 sizeof(value));
Biwen Li71ecd382020-10-12 20:07:35 +0800521 if (ret)
522 goto exit;
523
524 if (value != EN_WRITE_ALL_CMD) {
525 value = EN_WRITE_ALL_CMD;
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100526 ret = I2C_WRITE(dev, PMBUS_CMD_WRITE_PROTECT,
Biwen Li71ecd382020-10-12 20:07:35 +0800527 (void *)&value, sizeof(value));
528 if (ret)
529 goto exit;
530 }
531
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100532 /* Write the desired voltage code to the regulator */
533 ret = I2C_WRITE(dev, PMBUS_CMD_PAGE_PLUS_WRITE, (void *)&buffer[0],
534 sizeof(buffer));
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530535 if (ret) {
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100536 printf("VID: I2C failed to write to the voltage regulator\n");
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530537 return -1;
538 }
539
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100540exit:
541 /* Wait for the voltage to get to the desired value */
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530542 do {
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100543 vdd_last = read_voltage_from_pmbus(i2caddress);
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530544 if (vdd_last < 0) {
545 printf("VID: Couldn't read sensor abort VID adjust\n");
546 return -1;
547 }
Priyanka Jainaf89d242018-10-11 05:11:23 +0000548 count--;
549 temp = vdd_last - vdd_target;
550 } while ((abs(temp) > 2) && (count > 0));
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530551
552 return vdd_last;
553}
Ying Zhang8876a512014-10-31 18:06:18 +0800554#endif
555
556static int set_voltage(int i2caddress, int vdd)
557{
558 int vdd_last = -1;
559
560#ifdef CONFIG_VOL_MONITOR_IR36021_SET
561 vdd_last = set_voltage_to_IR(i2caddress, vdd);
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100562#elif defined(CONFIG_VOL_MONITOR_ISL68233_SET) || \
563 defined(CONFIG_VOL_MONITOR_LTC3882_SET)
564 vdd_last = set_voltage_to_pmbus(i2caddress, vdd);
Ying Zhang8876a512014-10-31 18:06:18 +0800565#else
566 #error Specific voltage monitor must be defined
567#endif
568 return vdd_last;
569}
570
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530571int adjust_vdd(ulong vdd_override)
572{
573 int re_enable = disable_interrupts();
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100574#if defined(CONFIG_FSL_LSCH2) || defined(CONFIG_FSL_LSCH3)
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530575 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530576#else
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100577 ccsr_gur_t __iomem *gur =
578 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530579#endif
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100580 u8 vid;
581 u32 fusesr;
582 int vdd_current, vdd_last, vdd_target;
583 int ret, i2caddress = I2C_VOL_MONITOR_ADDR;
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530584 unsigned long vdd_string_override;
585 char *vdd_string;
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530586
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530587#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
588 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100589 u8 buf;
590 DEVICE_HANDLE_T dev;
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530591#endif
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530592
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100593 /*
594 * VID is used according to the table below
595 * ---------------------------------------
596 * | DA_V |
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 |
607 * | T | | | | |
608 * ------------------------------------------------------
609 */
610#if defined(CONFIG_FSL_LSCH3)
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530611 fusesr = in_le32(&gur->dcfg_fusesr);
612 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100613 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
614 if (vid == 0 || vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK) {
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530615 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100616 FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530617 }
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100618#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;
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530625 }
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530626#else
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100627 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;
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530633 }
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530634#endif
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100635 vdd_target = soc_get_fuse_vid((int)vid);
Ying Zhang8876a512014-10-31 18:06:18 +0800636
637 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
638 if (ret) {
639 debug("VID: I2C failed to switch channel\n");
640 ret = -1;
641 goto exit;
642 }
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100643
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530644#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
645 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Ying Zhang8876a512014-10-31 18:06:18 +0800646 ret = find_ir_chip_on_i2c();
647 if (ret < 0) {
648 printf("VID: Could not find voltage regulator on I2C.\n");
649 ret = -1;
650 goto exit;
651 } else {
652 i2caddress = ret;
653 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
654 }
655
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100656 ret = vid_get_device(i2caddress, &dev);
657 if (ret)
658 return ret;
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800659
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100660 /* check IR chip work on Intel mode */
661 ret = I2C_READ(dev, IR36021_INTEL_MODE_OFFSET, (void *)&buf,
662 sizeof(buf));
Ying Zhang7ad5eff2016-01-22 12:15:12 +0800663 if (ret) {
664 printf("VID: failed to read IR chip mode.\n");
665 ret = -1;
666 goto exit;
667 }
668 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
669 printf("VID: IR Chip is not used in Intel mode.\n");
670 ret = -1;
671 goto exit;
672 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530673#endif
Ying Zhang7ad5eff2016-01-22 12:15:12 +0800674
Ying Zhang8876a512014-10-31 18:06:18 +0800675 /* check override variable for overriding VDD */
Simon Glass64b723f2017-08-03 12:22:12 -0600676 vdd_string = env_get(CONFIG_VID_FLS_ENV);
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100677 debug("VID: Initial VDD value is %d mV\n",
678 DIV_ROUND_UP(vdd_target, 10));
Ying Zhang8876a512014-10-31 18:06:18 +0800679 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 */
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100684 debug("VID: VDD override is %lu\n", vdd_override);
Ying Zhang8876a512014-10-31 18:06:18 +0800685 } else if (vdd_override != 0) {
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100686 printf("VID: Invalid VDD value.\n");
Ying Zhang8876a512014-10-31 18:06:18 +0800687 }
688 if (vdd_target == 0) {
689 debug("VID: VID not used\n");
690 ret = 0;
691 goto exit;
692 } else {
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);
696 }
697
698 /*
699 * Read voltage monitor to check real voltage.
700 */
701 vdd_last = read_voltage(i2caddress);
702 if (vdd_last < 0) {
703 printf("VID: Couldn't read sensor abort VID adjustment\n");
704 ret = -1;
705 goto exit;
706 }
707 vdd_current = vdd_last;
708 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100709
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;
715#else
Ying Zhang8876a512014-10-31 18:06:18 +0800716 /*
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.
721 */
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);
726 }
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);
731 }
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100732#endif
733
734 /* Board specific adjustments */
735 if (board_adjust_vdd(vdd_target) < 0) {
736 ret = -1;
737 goto exit;
738 }
Ying Zhang8876a512014-10-31 18:06:18 +0800739
740 if (vdd_last > 0)
741 printf("VID: Core voltage after adjustment is at %d mV\n",
742 vdd_last);
743 else
744 ret = -1;
745exit:
746 if (re_enable)
747 enable_interrupts();
Wenbin Song44ad75c2016-03-09 13:38:23 +0800748
749 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
750
Ying Zhang8876a512014-10-31 18:06:18 +0800751 return ret;
752}
753
754static int print_vdd(void)
755{
Stephen Carlsonc3301a22021-02-08 11:11:29 +0100756 int vdd_last, ret, i2caddress = I2C_VOL_MONITOR_ADDR;
Ying Zhang8876a512014-10-31 18:06:18 +0800757
758 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
759 if (ret) {
760 debug("VID : I2c failed to switch channel\n");
761 return -1;
762 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530763#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
764 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Ying Zhang8876a512014-10-31 18:06:18 +0800765 ret = find_ir_chip_on_i2c();
766 if (ret < 0) {
767 printf("VID: Could not find voltage regulator on I2C.\n");
Wenbin Song44ad75c2016-03-09 13:38:23 +0800768 goto exit;
Ying Zhang8876a512014-10-31 18:06:18 +0800769 } else {
770 i2caddress = ret;
771 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
772 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530773#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800774
775 /*
776 * Read voltage monitor to check real voltage.
777 */
778 vdd_last = read_voltage(i2caddress);
779 if (vdd_last < 0) {
780 printf("VID: Couldn't read sensor abort VID adjustment\n");
Wenbin Song44ad75c2016-03-09 13:38:23 +0800781 goto exit;
Ying Zhang8876a512014-10-31 18:06:18 +0800782 }
783 printf("VID: Core voltage is at %d mV\n", vdd_last);
Wenbin Song44ad75c2016-03-09 13:38:23 +0800784exit:
785 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
786
787 return ret < 0 ? -1 : 0;
Ying Zhang8876a512014-10-31 18:06:18 +0800788
Ying Zhang8876a512014-10-31 18:06:18 +0800789}
790
Simon Glassed38aef2020-05-10 11:40:03 -0600791static int do_vdd_override(struct cmd_tbl *cmdtp,
Ying Zhang8876a512014-10-31 18:06:18 +0800792 int flag, int argc,
Simon Glassed38aef2020-05-10 11:40:03 -0600793 char *const argv[])
Ying Zhang8876a512014-10-31 18:06:18 +0800794{
795 ulong override;
796
797 if (argc < 2)
798 return CMD_RET_USAGE;
799
800 if (!strict_strtoul(argv[1], 10, &override))
801 adjust_vdd(override); /* the value is checked by callee */
802 else
803 return CMD_RET_USAGE;
804 return 0;
805}
806
Simon Glassed38aef2020-05-10 11:40:03 -0600807static int do_vdd_read(struct cmd_tbl *cmdtp, int flag, int argc,
808 char *const argv[])
Ying Zhang8876a512014-10-31 18:06:18 +0800809{
810 if (argc < 1)
811 return CMD_RET_USAGE;
812 print_vdd();
813
814 return 0;
815}
816
817U_BOOT_CMD(
818 vdd_override, 2, 0, do_vdd_override,
819 "override VDD",
820 " - override with the voltage specified in mV, eg. 1050"
821);
822
823U_BOOT_CMD(
824 vdd_read, 1, 0, do_vdd_read,
825 "read VDD",
826 " - Read the voltage specified in mV"
827)