blob: 9c51f5026083a4adcefbec101fb467b1b95abd31 [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.
Ying Zhang8876a512014-10-31 18:06:18 +08004 */
5
6#include <common.h>
7#include <command.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -06008#include <env.h>
Ying Zhang8876a512014-10-31 18:06:18 +08009#include <i2c.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070010#include <irq_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Shaohui Xiedd335672015-11-11 17:58:37 +080012#include <asm/io.h>
Shaohui Xie085ac1c2016-09-07 17:56:14 +080013#ifdef CONFIG_FSL_LSCH2
Shaohui Xiedd335672015-11-11 17:58:37 +080014#include <asm/arch/immap_lsch2.h>
Rai Harninder6aa1f3b2016-03-23 17:04:38 +053015#elif defined(CONFIG_FSL_LSCH3)
16#include <asm/arch/immap_lsch3.h>
Shaohui Xiedd335672015-11-11 17:58:37 +080017#else
Ying Zhang8876a512014-10-31 18:06:18 +080018#include <asm/immap_85xx.h>
Shaohui Xiedd335672015-11-11 17:58:37 +080019#endif
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
Ying Zhang8876a512014-10-31 18:06:18 +080021#include "vid.h"
22
Ying Zhang8876a512014-10-31 18:06:18 +080023int __weak i2c_multiplexer_select_vid_channel(u8 channel)
24{
25 return 0;
26}
27
28/*
29 * Compensate for a board specific voltage drop between regulator and SoC
30 * return a value in mV
31 */
32int __weak board_vdd_drop_compensation(void)
33{
34 return 0;
35}
36
37/*
Rajesh Bhagatc3a662f2018-01-17 16:13:02 +053038 * Board specific settings for specific voltage value
39 */
40int __weak board_adjust_vdd(int vdd)
41{
42 return 0;
43}
44
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +053045#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
46 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Rajesh Bhagatc3a662f2018-01-17 16:13:02 +053047/*
Ying Zhang8876a512014-10-31 18:06:18 +080048 * Get the i2c address configuration for the IR regulator chip
49 *
50 * There are some variance in the RDB HW regarding the I2C address configuration
51 * for the IR regulator chip, which is likely a problem of external resistor
52 * accuracy. So we just check each address in a hopefully non-intrusive mode
53 * and use the first one that seems to work
54 *
55 * The IR chip can show up under the following addresses:
56 * 0x08 (Verified on T1040RDB-PA,T4240RDB-PB,X-T4240RDB-16GPA)
57 * 0x09 (Verified on T1040RDB-PA)
Ying Zhangff779052016-01-22 12:15:13 +080058 * 0x38 (Verified on T2080QDS, T2081QDS, T4240RDB)
Ying Zhang8876a512014-10-31 18:06:18 +080059 */
60static int find_ir_chip_on_i2c(void)
61{
62 int i2caddress;
63 int ret;
64 u8 byte;
65 int i;
66 const int ir_i2c_addr[] = {0x38, 0x08, 0x09};
Chuanhua Han37c2c5e2019-07-10 21:00:20 +080067#ifdef CONFIG_DM_I2C
68 struct udevice *dev;
69#endif
Ying Zhang8876a512014-10-31 18:06:18 +080070
71 /* Check all the address */
72 for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) {
73 i2caddress = ir_i2c_addr[i];
Chuanhua Han37c2c5e2019-07-10 21:00:20 +080074#ifndef CONFIG_DM_I2C
Ying Zhang8876a512014-10-31 18:06:18 +080075 ret = i2c_read(i2caddress,
76 IR36021_MFR_ID_OFFSET, 1, (void *)&byte,
77 sizeof(byte));
Chuanhua Han37c2c5e2019-07-10 21:00:20 +080078#else
79 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
80 if (!ret)
81 ret = dm_i2c_read(dev, IR36021_MFR_ID_OFFSET,
82 (void *)&byte, sizeof(byte));
83#endif
Ying Zhang8876a512014-10-31 18:06:18 +080084 if ((ret >= 0) && (byte == IR36021_MFR_ID))
85 return i2caddress;
86 }
87 return -1;
88}
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +053089#endif
Ying Zhang8876a512014-10-31 18:06:18 +080090
91/* Maximum loop count waiting for new voltage to take effect */
92#define MAX_LOOP_WAIT_NEW_VOL 100
93/* Maximum loop count waiting for the voltage to be stable */
94#define MAX_LOOP_WAIT_VOL_STABLE 100
95/*
96 * read_voltage from sensor on I2C bus
97 * We use average of 4 readings, waiting for WAIT_FOR_ADC before
98 * another reading
99 */
100#define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
101
102/* If an INA220 chip is available, we can use it to read back the voltage
103 * as it may have a higher accuracy than the IR chip for the same purpose
104 */
105#ifdef CONFIG_VOL_MONITOR_INA220
106#define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
107#define ADC_MIN_ACCURACY 4
108#else
109#define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
110#define ADC_MIN_ACCURACY 4
111#endif
112
113#ifdef CONFIG_VOL_MONITOR_INA220
114static int read_voltage_from_INA220(int i2caddress)
115{
116 int i, ret, voltage_read = 0;
117 u16 vol_mon;
118 u8 buf[2];
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800119#ifdef CONFIG_DM_I2C
120 struct udevice *dev;
121#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800122
123 for (i = 0; i < NUM_READINGS; i++) {
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800124#ifndef CONFIG_DM_I2C
Ying Zhang8876a512014-10-31 18:06:18 +0800125 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
126 I2C_VOL_MONITOR_BUS_V_OFFSET, 1,
127 (void *)&buf, 2);
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800128#else
129 ret = i2c_get_chip_for_busnum(0, I2C_VOL_MONITOR_ADDR, 1, &dev);
130 if (!ret)
131 ret = dm_i2c_read(dev, I2C_VOL_MONITOR_BUS_V_OFFSET,
132 (void *)&buf, 2);
133#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800134 if (ret) {
135 printf("VID: failed to read core voltage\n");
136 return ret;
137 }
138 vol_mon = (buf[0] << 8) | buf[1];
139 if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
140 printf("VID: Core voltage sensor error\n");
141 return -1;
142 }
143 debug("VID: bus voltage reads 0x%04x\n", vol_mon);
144 /* LSB = 4mv */
145 voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
146 udelay(WAIT_FOR_ADC);
147 }
148 /* calculate the average */
149 voltage_read /= NUM_READINGS;
150
151 return voltage_read;
152}
153#endif
154
155/* read voltage from IR */
156#ifdef CONFIG_VOL_MONITOR_IR36021_READ
157static int read_voltage_from_IR(int i2caddress)
158{
159 int i, ret, voltage_read = 0;
160 u16 vol_mon;
161 u8 buf;
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800162#ifdef CONFIG_DM_I2C
163 struct udevice *dev;
164#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800165
166 for (i = 0; i < NUM_READINGS; i++) {
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800167#ifndef CONFIG_DM_I2C
Ying Zhang8876a512014-10-31 18:06:18 +0800168 ret = i2c_read(i2caddress,
169 IR36021_LOOP1_VOUT_OFFSET,
170 1, (void *)&buf, 1);
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800171#else
172 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
173 if (!ret)
174 ret = dm_i2c_read(dev, IR36021_LOOP1_VOUT_OFFSET,
175 (void *)&buf, 1);
176#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800177 if (ret) {
178 printf("VID: failed to read vcpu\n");
179 return ret;
180 }
181 vol_mon = buf;
182 if (!vol_mon) {
183 printf("VID: Core voltage sensor error\n");
184 return -1;
185 }
186 debug("VID: bus voltage reads 0x%02x\n", vol_mon);
187 /* Resolution is 1/128V. We scale up here to get 1/128mV
188 * and divide at the end
189 */
190 voltage_read += vol_mon * 1000;
191 udelay(WAIT_FOR_ADC);
192 }
193 /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
194 voltage_read = DIV_ROUND_UP(voltage_read, 128);
195
196 /* calculate the average */
197 voltage_read /= NUM_READINGS;
198
199 /* Compensate for a board specific voltage drop between regulator and
200 * SoC before converting into an IR VID value
201 */
202 voltage_read -= board_vdd_drop_compensation();
203
204 return voltage_read;
205}
206#endif
207
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530208#ifdef CONFIG_VOL_MONITOR_LTC3882_READ
209/* read the current value of the LTC Regulator Voltage */
210static int read_voltage_from_LTC(int i2caddress)
211{
212 int ret, vcode = 0;
213 u8 chan = PWM_CHANNEL0;
214
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800215#ifndef CONFIG_DM_I2C
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530216 /* select the PAGE 0 using PMBus commands PAGE for VDD*/
217 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
218 PMBUS_CMD_PAGE, 1, &chan, 1);
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800219#else
220 struct udevice *dev;
221
222 ret = i2c_get_chip_for_busnum(0, I2C_VOL_MONITOR_ADDR, 1, &dev);
223 if (!ret)
224 ret = dm_i2c_write(dev, PMBUS_CMD_PAGE, &chan, 1);
225#endif
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530226 if (ret) {
227 printf("VID: failed to select VDD Page 0\n");
228 return ret;
229 }
230
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800231#ifndef CONFIG_DM_I2C
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530232 /*read the output voltage using PMBus command READ_VOUT*/
233 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
234 PMBUS_CMD_READ_VOUT, 1, (void *)&vcode, 2);
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800235#else
236 ret = dm_i2c_read(dev, PMBUS_CMD_READ_VOUT, (void *)&vcode, 2);
237 if (ret) {
238 printf("VID: failed to read the volatge\n");
239 return ret;
240 }
241#endif
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530242 if (ret) {
243 printf("VID: failed to read the volatge\n");
244 return ret;
245 }
246
247 /* Scale down to the real mV as LTC resolution is 1/4096V,rounding up */
248 vcode = DIV_ROUND_UP(vcode * 1000, 4096);
249
250 return vcode;
251}
252#endif
253
Ying Zhang8876a512014-10-31 18:06:18 +0800254static int read_voltage(int i2caddress)
255{
256 int voltage_read;
257#ifdef CONFIG_VOL_MONITOR_INA220
258 voltage_read = read_voltage_from_INA220(i2caddress);
259#elif defined CONFIG_VOL_MONITOR_IR36021_READ
260 voltage_read = read_voltage_from_IR(i2caddress);
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530261#elif defined CONFIG_VOL_MONITOR_LTC3882_READ
262 voltage_read = read_voltage_from_LTC(i2caddress);
Ying Zhang8876a512014-10-31 18:06:18 +0800263#else
264 return -1;
265#endif
266 return voltage_read;
267}
268
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530269#ifdef CONFIG_VOL_MONITOR_IR36021_SET
Ying Zhang8876a512014-10-31 18:06:18 +0800270/*
271 * We need to calculate how long before the voltage stops to drop
272 * or increase. It returns with the loop count. Each loop takes
273 * several readings (WAIT_FOR_ADC)
274 */
275static int wait_for_new_voltage(int vdd, int i2caddress)
276{
277 int timeout, vdd_current;
278
279 vdd_current = read_voltage(i2caddress);
280 /* wait until voltage starts to reach the target. Voltage slew
281 * rates by typical regulators will always lead to stable readings
282 * within each fairly long ADC interval in comparison to the
283 * intended voltage delta change until the target voltage is
284 * reached. The fairly small voltage delta change to any target
285 * VID voltage also means that this function will always complete
286 * within few iterations. If the timeout was ever reached, it would
287 * point to a serious failure in the regulator system.
288 */
289 for (timeout = 0;
290 abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
291 timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
292 vdd_current = read_voltage(i2caddress);
293 }
294 if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
295 printf("VID: Voltage adjustment timeout\n");
296 return -1;
297 }
298 return timeout;
299}
300
301/*
302 * this function keeps reading the voltage until it is stable or until the
303 * timeout expires
304 */
305static int wait_for_voltage_stable(int i2caddress)
306{
307 int timeout, vdd_current, vdd;
308
309 vdd = read_voltage(i2caddress);
310 udelay(NUM_READINGS * WAIT_FOR_ADC);
311
312 /* wait until voltage is stable */
313 vdd_current = read_voltage(i2caddress);
314 /* The maximum timeout is
315 * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
316 */
317 for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
318 abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
319 timeout > 0; timeout--) {
320 vdd = vdd_current;
321 udelay(NUM_READINGS * WAIT_FOR_ADC);
322 vdd_current = read_voltage(i2caddress);
323 }
324 if (timeout == 0)
325 return -1;
326 return vdd_current;
327}
328
Ying Zhang8876a512014-10-31 18:06:18 +0800329/* Set the voltage to the IR chip */
330static int set_voltage_to_IR(int i2caddress, int vdd)
331{
332 int wait, vdd_last;
333 int ret;
334 u8 vid;
335
336 /* Compensate for a board specific voltage drop between regulator and
337 * SoC before converting into an IR VID value
338 */
339 vdd += board_vdd_drop_compensation();
Shaohui Xie085ac1c2016-09-07 17:56:14 +0800340#ifdef CONFIG_FSL_LSCH2
Shaohui Xiedd335672015-11-11 17:58:37 +0800341 vid = DIV_ROUND_UP(vdd - 265, 5);
342#else
Ying Zhang8876a512014-10-31 18:06:18 +0800343 vid = DIV_ROUND_UP(vdd - 245, 5);
Shaohui Xiedd335672015-11-11 17:58:37 +0800344#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800345
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800346#ifndef CONFIG_DM_I2C
Ying Zhang8876a512014-10-31 18:06:18 +0800347 ret = i2c_write(i2caddress, IR36021_LOOP1_MANUAL_ID_OFFSET,
348 1, (void *)&vid, sizeof(vid));
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800349#else
350 struct udevice *dev;
351
352 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
353 if (!ret)
354 ret = dm_i2c_write(dev, IR36021_LOOP1_MANUAL_ID_OFFSET,
355 (void *)&vid, sizeof(vid));
356
357#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800358 if (ret) {
359 printf("VID: failed to write VID\n");
360 return -1;
361 }
362 wait = wait_for_new_voltage(vdd, i2caddress);
363 if (wait < 0)
364 return -1;
365 debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
366
367 vdd_last = wait_for_voltage_stable(i2caddress);
368 if (vdd_last < 0)
369 return -1;
370 debug("VID: Current voltage is %d mV\n", vdd_last);
371 return vdd_last;
372}
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530373
374#endif
375
376#ifdef CONFIG_VOL_MONITOR_LTC3882_SET
377/* this function sets the VDD and returns the value set */
378static int set_voltage_to_LTC(int i2caddress, int vdd)
379{
380 int ret, vdd_last, vdd_target = vdd;
Priyanka Jainaf89d242018-10-11 05:11:23 +0000381 int count = 100, temp = 0;
Biwen Li71ecd382020-10-12 20:07:35 +0800382 unsigned char value;
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530383
384 /* Scale up to the LTC resolution is 1/4096V */
385 vdd = (vdd * 4096) / 1000;
386
387 /* 5-byte buffer which needs to be sent following the
388 * PMBus command PAGE_PLUS_WRITE.
389 */
390 u8 buff[5] = {0x04, PWM_CHANNEL0, PMBUS_CMD_VOUT_COMMAND,
391 vdd & 0xFF, (vdd & 0xFF00) >> 8};
392
393 /* Write the desired voltage code to the regulator */
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800394#ifndef CONFIG_DM_I2C
Biwen Li71ecd382020-10-12 20:07:35 +0800395 /* Check write protect state */
396 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
397 PMBUS_CMD_WRITE_PROTECT, 1,
398 (void *)&value, sizeof(value));
399 if (ret)
400 goto exit;
401
402 if (value != EN_WRITE_ALL_CMD) {
403 value = EN_WRITE_ALL_CMD;
404 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
405 PMBUS_CMD_WRITE_PROTECT, 1,
406 (void *)&value, sizeof(value));
407 if (ret)
408 goto exit;
409 }
410
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530411 ret = i2c_write(I2C_VOL_MONITOR_ADDR,
Biwen Li71ecd382020-10-12 20:07:35 +0800412 PMBUS_CMD_PAGE_PLUS_WRITE, 1,
413 (void *)&buff, sizeof(buff));
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800414#else
415 struct udevice *dev;
416
417 ret = i2c_get_chip_for_busnum(0, I2C_VOL_MONITOR_ADDR, 1, &dev);
Biwen Li71ecd382020-10-12 20:07:35 +0800418 if (!ret) {
419 /* Check write protect state */
420 ret = dm_i2c_read(dev,
421 PMBUS_CMD_WRITE_PROTECT,
422 (void *)&value, sizeof(value));
423 if (ret)
424 goto exit;
425
426 if (value != EN_WRITE_ALL_CMD) {
427 value = EN_WRITE_ALL_CMD;
428 ret = dm_i2c_write(dev,
429 PMBUS_CMD_WRITE_PROTECT,
430 (void *)&value, sizeof(value));
431 if (ret)
432 goto exit;
433 }
434
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800435 ret = dm_i2c_write(dev, PMBUS_CMD_PAGE_PLUS_WRITE,
Biwen Li71ecd382020-10-12 20:07:35 +0800436 (void *)&buff, sizeof(buff));
437 }
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800438#endif
Biwen Li71ecd382020-10-12 20:07:35 +0800439exit:
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530440 if (ret) {
441 printf("VID: I2C failed to write to the volatge regulator\n");
442 return -1;
443 }
444
445 /* Wait for the volatge to get to the desired value */
446 do {
447 vdd_last = read_voltage_from_LTC(i2caddress);
448 if (vdd_last < 0) {
449 printf("VID: Couldn't read sensor abort VID adjust\n");
450 return -1;
451 }
Priyanka Jainaf89d242018-10-11 05:11:23 +0000452 count--;
453 temp = vdd_last - vdd_target;
454 } while ((abs(temp) > 2) && (count > 0));
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530455
456 return vdd_last;
457}
Ying Zhang8876a512014-10-31 18:06:18 +0800458#endif
459
460static int set_voltage(int i2caddress, int vdd)
461{
462 int vdd_last = -1;
463
464#ifdef CONFIG_VOL_MONITOR_IR36021_SET
465 vdd_last = set_voltage_to_IR(i2caddress, vdd);
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530466#elif defined CONFIG_VOL_MONITOR_LTC3882_SET
467 vdd_last = set_voltage_to_LTC(i2caddress, vdd);
Ying Zhang8876a512014-10-31 18:06:18 +0800468#else
469 #error Specific voltage monitor must be defined
470#endif
471 return vdd_last;
472}
473
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530474#ifdef CONFIG_FSL_LSCH3
475int adjust_vdd(ulong vdd_override)
476{
477 int re_enable = disable_interrupts();
478 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
479 u32 fusesr;
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530480#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
481 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530482 u8 vid, buf;
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530483#else
484 u8 vid;
485#endif
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530486 int vdd_target, vdd_current, vdd_last;
487 int ret, i2caddress;
488 unsigned long vdd_string_override;
489 char *vdd_string;
Priyanka Jainc6a6fac2018-10-11 05:22:34 +0000490#ifdef CONFIG_ARCH_LX2160A
491 static const u16 vdd[32] = {
492 8250,
493 7875,
494 7750,
495 0, /* reserved */
496 0, /* reserved */
497 0, /* reserved */
498 0, /* reserved */
499 0, /* reserved */
500 0, /* reserved */
501 0, /* reserved */
502 0, /* reserved */
503 0, /* reserved */
504 0, /* reserved */
505 0, /* reserved */
506 0, /* reserved */
507 0, /* reserved */
508 8000,
509 8125,
510 8250,
511 0, /* reserved */
512 8500,
513 0, /* reserved */
514 0, /* reserved */
515 0, /* reserved */
516 0, /* reserved */
517 0, /* reserved */
518 0, /* reserved */
519 0, /* reserved */
520 0, /* reserved */
521 0, /* reserved */
522 0, /* reserved */
523 0, /* reserved */
524 };
525#else
Rajesh Bhagatd600b312018-01-17 16:13:01 +0530526#ifdef CONFIG_ARCH_LS1088A
527 static const uint16_t vdd[32] = {
528 10250,
529 9875,
530 9750,
531 0, /* reserved */
532 0, /* reserved */
533 0, /* reserved */
534 0, /* reserved */
535 0, /* reserved */
536 9000,
537 0, /* reserved */
538 0, /* reserved */
539 0, /* reserved */
540 0, /* reserved */
541 0, /* reserved */
542 0, /* reserved */
543 0, /* reserved */
544 10000, /* 1.0000V */
545 10125,
546 10250,
547 0, /* reserved */
548 0, /* reserved */
549 0, /* reserved */
550 0, /* reserved */
551 0, /* reserved */
552 0, /* reserved */
553 0, /* reserved */
554 0, /* reserved */
555 0, /* reserved */
556 0, /* reserved */
557 0, /* reserved */
558 0, /* reserved */
559 0, /* reserved */
560 };
561
562#else
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530563 static const uint16_t vdd[32] = {
564 10500,
565 0, /* reserved */
566 9750,
567 0, /* reserved */
568 9500,
569 0, /* reserved */
570 0, /* reserved */
571 0, /* reserved */
Martin Kaistraffcbff72020-08-21 13:44:13 +0200572 9000, /* reserved */
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530573 0, /* reserved */
574 0, /* reserved */
575 0, /* reserved */
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530576 0, /* reserved */
577 0, /* reserved */
578 0, /* reserved */
579 0, /* reserved */
580 10000, /* 1.0000V */
581 0, /* reserved */
582 10250,
583 0, /* reserved */
584 10500,
585 0, /* reserved */
586 0, /* reserved */
587 0, /* reserved */
588 0, /* reserved */
589 0, /* reserved */
590 0, /* reserved */
591 0, /* reserved */
592 0, /* reserved */
593 0, /* reserved */
594 0, /* reserved */
595 0, /* reserved */
596 };
Rajesh Bhagatd600b312018-01-17 16:13:01 +0530597#endif
Priyanka Jainc6a6fac2018-10-11 05:22:34 +0000598#endif
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530599 struct vdd_drive {
600 u8 vid;
601 unsigned voltage;
602 };
603
604 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
605 if (ret) {
606 debug("VID: I2C failed to switch channel\n");
607 ret = -1;
608 goto exit;
609 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530610#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
611 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530612 ret = find_ir_chip_on_i2c();
613 if (ret < 0) {
614 printf("VID: Could not find voltage regulator on I2C.\n");
615 ret = -1;
616 goto exit;
617 } else {
618 i2caddress = ret;
619 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
620 }
621
622 /* check IR chip work on Intel mode*/
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800623#ifndef CONFIG_DM_I2C
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530624 ret = i2c_read(i2caddress,
625 IR36021_INTEL_MODE_OOFSET,
626 1, (void *)&buf, 1);
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800627#else
628 struct udevice *dev;
629
630 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
631 if (!ret)
632 ret = dm_i2c_read(dev, IR36021_INTEL_MODE_OOFSET,
633 (void *)&buf, 1);
634#endif
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530635 if (ret) {
636 printf("VID: failed to read IR chip mode.\n");
637 ret = -1;
638 goto exit;
639 }
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800640
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530641 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
642 printf("VID: IR Chip is not used in Intel mode.\n");
643 ret = -1;
644 goto exit;
645 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530646#endif
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530647
648 /* get the voltage ID from fuse status register */
649 fusesr = in_le32(&gur->dcfg_fusesr);
650 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_ALTVID_SHIFT) &
651 FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK;
652 if ((vid == 0) || (vid == FSL_CHASSIS3_DCFG_FUSESR_ALTVID_MASK)) {
653 vid = (fusesr >> FSL_CHASSIS3_DCFG_FUSESR_VID_SHIFT) &
654 FSL_CHASSIS3_DCFG_FUSESR_VID_MASK;
655 }
656 vdd_target = vdd[vid];
657
658 /* check override variable for overriding VDD */
Simon Glass64b723f2017-08-03 12:22:12 -0600659 vdd_string = env_get(CONFIG_VID_FLS_ENV);
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530660 if (vdd_override == 0 && vdd_string &&
661 !strict_strtoul(vdd_string, 10, &vdd_string_override))
662 vdd_override = vdd_string_override;
663
664 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
665 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
666 debug("VDD override is %lu\n", vdd_override);
667 } else if (vdd_override != 0) {
668 printf("Invalid value.\n");
669 }
670
671 /* divide and round up by 10 to get a value in mV */
672 vdd_target = DIV_ROUND_UP(vdd_target, 10);
673 if (vdd_target == 0) {
674 debug("VID: VID not used\n");
675 ret = 0;
676 goto exit;
677 } else if (vdd_target < VDD_MV_MIN || vdd_target > VDD_MV_MAX) {
678 /* Check vdd_target is in valid range */
679 printf("VID: Target VID %d mV is not in range.\n",
680 vdd_target);
681 ret = -1;
682 goto exit;
683 } else {
684 debug("VID: vid = %d mV\n", vdd_target);
685 }
686
687 /*
688 * Read voltage monitor to check real voltage.
689 */
690 vdd_last = read_voltage(i2caddress);
691 if (vdd_last < 0) {
692 printf("VID: Couldn't read sensor abort VID adjustment\n");
693 ret = -1;
694 goto exit;
695 }
696 vdd_current = vdd_last;
697 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530698
699#ifdef CONFIG_VOL_MONITOR_LTC3882_SET
700 /* Set the target voltage */
701 vdd_last = vdd_current = set_voltage(i2caddress, vdd_target);
702#else
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530703 /*
704 * Adjust voltage to at or one step above target.
705 * As measurements are less precise than setting the values
706 * we may run through dummy steps that cancel each other
707 * when stepping up and then down.
708 */
709 while (vdd_last > 0 &&
710 vdd_last < vdd_target) {
711 vdd_current += IR_VDD_STEP_UP;
712 vdd_last = set_voltage(i2caddress, vdd_current);
713 }
714 while (vdd_last > 0 &&
715 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
716 vdd_current -= IR_VDD_STEP_DOWN;
717 vdd_last = set_voltage(i2caddress, vdd_current);
718 }
719
Rajesh Bhagat170eecf2018-01-17 16:13:05 +0530720#endif
Rajesh Bhagatc3a662f2018-01-17 16:13:02 +0530721 if (board_adjust_vdd(vdd_target) < 0) {
722 ret = -1;
723 goto exit;
724 }
725
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530726 if (vdd_last > 0)
727 printf("VID: Core voltage after adjustment is at %d mV\n",
728 vdd_last);
729 else
730 ret = -1;
731exit:
732 if (re_enable)
733 enable_interrupts();
734 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
735 return ret;
736}
737#else /* !CONFIG_FSL_LSCH3 */
Ying Zhang8876a512014-10-31 18:06:18 +0800738int adjust_vdd(ulong vdd_override)
739{
740 int re_enable = disable_interrupts();
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530741#if defined(CONFIG_FSL_LSCH2)
Shaohui Xiedd335672015-11-11 17:58:37 +0800742 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
743#else
Ying Zhang8876a512014-10-31 18:06:18 +0800744 ccsr_gur_t __iomem *gur =
745 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
Shaohui Xiedd335672015-11-11 17:58:37 +0800746#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800747 u32 fusesr;
Ying Zhang7ad5eff2016-01-22 12:15:12 +0800748 u8 vid, buf;
Ying Zhang8876a512014-10-31 18:06:18 +0800749 int vdd_target, vdd_current, vdd_last;
750 int ret, i2caddress;
751 unsigned long vdd_string_override;
752 char *vdd_string;
753 static const uint16_t vdd[32] = {
754 0, /* unused */
755 9875, /* 0.9875V */
756 9750,
757 9625,
758 9500,
759 9375,
760 9250,
761 9125,
762 9000,
763 8875,
764 8750,
765 8625,
766 8500,
767 8375,
768 8250,
769 8125,
770 10000, /* 1.0000V */
771 10125,
772 10250,
773 10375,
774 10500,
775 10625,
776 10750,
777 10875,
778 11000,
779 0, /* reserved */
780 };
781 struct vdd_drive {
782 u8 vid;
783 unsigned voltage;
784 };
785
786 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
787 if (ret) {
788 debug("VID: I2C failed to switch channel\n");
789 ret = -1;
790 goto exit;
791 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530792#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
793 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Ying Zhang8876a512014-10-31 18:06:18 +0800794 ret = find_ir_chip_on_i2c();
795 if (ret < 0) {
796 printf("VID: Could not find voltage regulator on I2C.\n");
797 ret = -1;
798 goto exit;
799 } else {
800 i2caddress = ret;
801 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
802 }
803
Ying Zhang7ad5eff2016-01-22 12:15:12 +0800804 /* check IR chip work on Intel mode*/
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800805#ifndef CONFIG_DM_I2C
Ying Zhang7ad5eff2016-01-22 12:15:12 +0800806 ret = i2c_read(i2caddress,
807 IR36021_INTEL_MODE_OOFSET,
808 1, (void *)&buf, 1);
Chuanhua Han37c2c5e2019-07-10 21:00:20 +0800809#else
810 struct udevice *dev;
811
812 ret = i2c_get_chip_for_busnum(0, i2caddress, 1, &dev);
813 if (!ret)
814 ret = dm_i2c_read(dev, IR36021_INTEL_MODE_OOFSET,
815 (void *)&buf, 1);
816#endif
Ying Zhang7ad5eff2016-01-22 12:15:12 +0800817 if (ret) {
818 printf("VID: failed to read IR chip mode.\n");
819 ret = -1;
820 goto exit;
821 }
822 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
823 printf("VID: IR Chip is not used in Intel mode.\n");
824 ret = -1;
825 goto exit;
826 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530827#endif
Ying Zhang7ad5eff2016-01-22 12:15:12 +0800828
Ying Zhang8876a512014-10-31 18:06:18 +0800829 /* get the voltage ID from fuse status register */
830 fusesr = in_be32(&gur->dcfg_fusesr);
831 /*
832 * VID is used according to the table below
833 * ---------------------------------------
834 * | DA_V |
835 * |-------------------------------------|
836 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
837 * ---------------+---------+-----------------+---------|
838 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
839 * | A |----------+---------+-----------------+---------|
840 * | _ | 5b00001 |VID = | VID = |VID = |
841 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
842 * | _ | 5b11110 | | | |
843 * | A |----------+---------+-----------------+---------|
844 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
845 * | T | | | | |
846 * ------------------------------------------------------
847 */
Shaohui Xie085ac1c2016-09-07 17:56:14 +0800848#ifdef CONFIG_FSL_LSCH2
Shaohui Xiedd335672015-11-11 17:58:37 +0800849 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
850 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
851 if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
852 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
853 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
854 }
855#else
Ying Zhang8876a512014-10-31 18:06:18 +0800856 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
857 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
858 if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
859 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
860 FSL_CORENET_DCFG_FUSESR_VID_MASK;
861 }
Shaohui Xiedd335672015-11-11 17:58:37 +0800862#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800863 vdd_target = vdd[vid];
864
865 /* check override variable for overriding VDD */
Simon Glass64b723f2017-08-03 12:22:12 -0600866 vdd_string = env_get(CONFIG_VID_FLS_ENV);
Ying Zhang8876a512014-10-31 18:06:18 +0800867 if (vdd_override == 0 && vdd_string &&
868 !strict_strtoul(vdd_string, 10, &vdd_string_override))
869 vdd_override = vdd_string_override;
870 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
871 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
872 debug("VDD override is %lu\n", vdd_override);
873 } else if (vdd_override != 0) {
874 printf("Invalid value.\n");
875 }
876 if (vdd_target == 0) {
877 debug("VID: VID not used\n");
878 ret = 0;
879 goto exit;
880 } else {
881 /* divide and round up by 10 to get a value in mV */
882 vdd_target = DIV_ROUND_UP(vdd_target, 10);
883 debug("VID: vid = %d mV\n", vdd_target);
884 }
885
886 /*
887 * Read voltage monitor to check real voltage.
888 */
889 vdd_last = read_voltage(i2caddress);
890 if (vdd_last < 0) {
891 printf("VID: Couldn't read sensor abort VID adjustment\n");
892 ret = -1;
893 goto exit;
894 }
895 vdd_current = vdd_last;
896 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
897 /*
898 * Adjust voltage to at or one step above target.
899 * As measurements are less precise than setting the values
900 * we may run through dummy steps that cancel each other
901 * when stepping up and then down.
902 */
903 while (vdd_last > 0 &&
904 vdd_last < vdd_target) {
905 vdd_current += IR_VDD_STEP_UP;
906 vdd_last = set_voltage(i2caddress, vdd_current);
907 }
908 while (vdd_last > 0 &&
909 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
910 vdd_current -= IR_VDD_STEP_DOWN;
911 vdd_last = set_voltage(i2caddress, vdd_current);
912 }
913
914 if (vdd_last > 0)
915 printf("VID: Core voltage after adjustment is at %d mV\n",
916 vdd_last);
917 else
918 ret = -1;
919exit:
920 if (re_enable)
921 enable_interrupts();
Wenbin Song44ad75c2016-03-09 13:38:23 +0800922
923 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
924
Ying Zhang8876a512014-10-31 18:06:18 +0800925 return ret;
926}
Priyanka Jainf9088dd2017-01-19 11:12:27 +0530927#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800928
929static int print_vdd(void)
930{
Priyanka Singh2e93fde2020-10-16 14:45:44 +0530931 int vdd_last, ret, i2caddress = 0;
Ying Zhang8876a512014-10-31 18:06:18 +0800932
933 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
934 if (ret) {
935 debug("VID : I2c failed to switch channel\n");
936 return -1;
937 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530938#if defined(CONFIG_VOL_MONITOR_IR36021_SET) || \
939 defined(CONFIG_VOL_MONITOR_IR36021_READ)
Ying Zhang8876a512014-10-31 18:06:18 +0800940 ret = find_ir_chip_on_i2c();
941 if (ret < 0) {
942 printf("VID: Could not find voltage regulator on I2C.\n");
Wenbin Song44ad75c2016-03-09 13:38:23 +0800943 goto exit;
Ying Zhang8876a512014-10-31 18:06:18 +0800944 } else {
945 i2caddress = ret;
946 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
947 }
Rajesh Bhagate6cd8d52018-01-17 16:13:03 +0530948#endif
Ying Zhang8876a512014-10-31 18:06:18 +0800949
950 /*
951 * Read voltage monitor to check real voltage.
952 */
953 vdd_last = read_voltage(i2caddress);
954 if (vdd_last < 0) {
955 printf("VID: Couldn't read sensor abort VID adjustment\n");
Wenbin Song44ad75c2016-03-09 13:38:23 +0800956 goto exit;
Ying Zhang8876a512014-10-31 18:06:18 +0800957 }
958 printf("VID: Core voltage is at %d mV\n", vdd_last);
Wenbin Song44ad75c2016-03-09 13:38:23 +0800959exit:
960 i2c_multiplexer_select_vid_channel(I2C_MUX_CH_DEFAULT);
961
962 return ret < 0 ? -1 : 0;
Ying Zhang8876a512014-10-31 18:06:18 +0800963
Ying Zhang8876a512014-10-31 18:06:18 +0800964}
965
Simon Glassed38aef2020-05-10 11:40:03 -0600966static int do_vdd_override(struct cmd_tbl *cmdtp,
Ying Zhang8876a512014-10-31 18:06:18 +0800967 int flag, int argc,
Simon Glassed38aef2020-05-10 11:40:03 -0600968 char *const argv[])
Ying Zhang8876a512014-10-31 18:06:18 +0800969{
970 ulong override;
971
972 if (argc < 2)
973 return CMD_RET_USAGE;
974
975 if (!strict_strtoul(argv[1], 10, &override))
976 adjust_vdd(override); /* the value is checked by callee */
977 else
978 return CMD_RET_USAGE;
979 return 0;
980}
981
Simon Glassed38aef2020-05-10 11:40:03 -0600982static int do_vdd_read(struct cmd_tbl *cmdtp, int flag, int argc,
983 char *const argv[])
Ying Zhang8876a512014-10-31 18:06:18 +0800984{
985 if (argc < 1)
986 return CMD_RET_USAGE;
987 print_vdd();
988
989 return 0;
990}
991
992U_BOOT_CMD(
993 vdd_override, 2, 0, do_vdd_override,
994 "override VDD",
995 " - override with the voltage specified in mV, eg. 1050"
996);
997
998U_BOOT_CMD(
999 vdd_read, 1, 0, do_vdd_read,
1000 "read VDD",
1001 " - Read the voltage specified in mV"
1002)