blob: 1ea1b88a10470efcdf0c362c3c2f6b69a48a6bda [file] [log] [blame]
Ying Zhang3ad27372014-10-31 18:06:18 +08001/*
2 * Copyright 2014 Freescale Semiconductor, Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <command.h>
9#include <i2c.h>
Shaohui Xie02b5d2e2015-11-11 17:58:37 +080010#include <asm/io.h>
11#ifdef CONFIG_LS1043A
12#include <asm/arch/immap_lsch2.h>
13#else
Ying Zhang3ad27372014-10-31 18:06:18 +080014#include <asm/immap_85xx.h>
Shaohui Xie02b5d2e2015-11-11 17:58:37 +080015#endif
Ying Zhang3ad27372014-10-31 18:06:18 +080016#include "vid.h"
17
18DECLARE_GLOBAL_DATA_PTR;
19
20int __weak i2c_multiplexer_select_vid_channel(u8 channel)
21{
22 return 0;
23}
24
25/*
26 * Compensate for a board specific voltage drop between regulator and SoC
27 * return a value in mV
28 */
29int __weak board_vdd_drop_compensation(void)
30{
31 return 0;
32}
33
34/*
35 * Get the i2c address configuration for the IR regulator chip
36 *
37 * There are some variance in the RDB HW regarding the I2C address configuration
38 * for the IR regulator chip, which is likely a problem of external resistor
39 * accuracy. So we just check each address in a hopefully non-intrusive mode
40 * and use the first one that seems to work
41 *
42 * The IR chip can show up under the following addresses:
43 * 0x08 (Verified on T1040RDB-PA,T4240RDB-PB,X-T4240RDB-16GPA)
44 * 0x09 (Verified on T1040RDB-PA)
45 * 0x38 (Verified on T2080QDS, T2081QDS)
46 */
47static int find_ir_chip_on_i2c(void)
48{
49 int i2caddress;
50 int ret;
51 u8 byte;
52 int i;
53 const int ir_i2c_addr[] = {0x38, 0x08, 0x09};
54
55 /* Check all the address */
56 for (i = 0; i < (sizeof(ir_i2c_addr)/sizeof(ir_i2c_addr[0])); i++) {
57 i2caddress = ir_i2c_addr[i];
58 ret = i2c_read(i2caddress,
59 IR36021_MFR_ID_OFFSET, 1, (void *)&byte,
60 sizeof(byte));
61 if ((ret >= 0) && (byte == IR36021_MFR_ID))
62 return i2caddress;
63 }
64 return -1;
65}
66
67/* Maximum loop count waiting for new voltage to take effect */
68#define MAX_LOOP_WAIT_NEW_VOL 100
69/* Maximum loop count waiting for the voltage to be stable */
70#define MAX_LOOP_WAIT_VOL_STABLE 100
71/*
72 * read_voltage from sensor on I2C bus
73 * We use average of 4 readings, waiting for WAIT_FOR_ADC before
74 * another reading
75 */
76#define NUM_READINGS 4 /* prefer to be power of 2 for efficiency */
77
78/* If an INA220 chip is available, we can use it to read back the voltage
79 * as it may have a higher accuracy than the IR chip for the same purpose
80 */
81#ifdef CONFIG_VOL_MONITOR_INA220
82#define WAIT_FOR_ADC 532 /* wait for 532 microseconds for ADC */
83#define ADC_MIN_ACCURACY 4
84#else
85#define WAIT_FOR_ADC 138 /* wait for 138 microseconds for ADC */
86#define ADC_MIN_ACCURACY 4
87#endif
88
89#ifdef CONFIG_VOL_MONITOR_INA220
90static int read_voltage_from_INA220(int i2caddress)
91{
92 int i, ret, voltage_read = 0;
93 u16 vol_mon;
94 u8 buf[2];
95
96 for (i = 0; i < NUM_READINGS; i++) {
97 ret = i2c_read(I2C_VOL_MONITOR_ADDR,
98 I2C_VOL_MONITOR_BUS_V_OFFSET, 1,
99 (void *)&buf, 2);
100 if (ret) {
101 printf("VID: failed to read core voltage\n");
102 return ret;
103 }
104 vol_mon = (buf[0] << 8) | buf[1];
105 if (vol_mon & I2C_VOL_MONITOR_BUS_V_OVF) {
106 printf("VID: Core voltage sensor error\n");
107 return -1;
108 }
109 debug("VID: bus voltage reads 0x%04x\n", vol_mon);
110 /* LSB = 4mv */
111 voltage_read += (vol_mon >> I2C_VOL_MONITOR_BUS_V_SHIFT) * 4;
112 udelay(WAIT_FOR_ADC);
113 }
114 /* calculate the average */
115 voltage_read /= NUM_READINGS;
116
117 return voltage_read;
118}
119#endif
120
121/* read voltage from IR */
122#ifdef CONFIG_VOL_MONITOR_IR36021_READ
123static int read_voltage_from_IR(int i2caddress)
124{
125 int i, ret, voltage_read = 0;
126 u16 vol_mon;
127 u8 buf;
128
129 for (i = 0; i < NUM_READINGS; i++) {
130 ret = i2c_read(i2caddress,
131 IR36021_LOOP1_VOUT_OFFSET,
132 1, (void *)&buf, 1);
133 if (ret) {
134 printf("VID: failed to read vcpu\n");
135 return ret;
136 }
137 vol_mon = buf;
138 if (!vol_mon) {
139 printf("VID: Core voltage sensor error\n");
140 return -1;
141 }
142 debug("VID: bus voltage reads 0x%02x\n", vol_mon);
143 /* Resolution is 1/128V. We scale up here to get 1/128mV
144 * and divide at the end
145 */
146 voltage_read += vol_mon * 1000;
147 udelay(WAIT_FOR_ADC);
148 }
149 /* Scale down to the real mV as IR resolution is 1/128V, rounding up */
150 voltage_read = DIV_ROUND_UP(voltage_read, 128);
151
152 /* calculate the average */
153 voltage_read /= NUM_READINGS;
154
155 /* Compensate for a board specific voltage drop between regulator and
156 * SoC before converting into an IR VID value
157 */
158 voltage_read -= board_vdd_drop_compensation();
159
160 return voltage_read;
161}
162#endif
163
164static int read_voltage(int i2caddress)
165{
166 int voltage_read;
167#ifdef CONFIG_VOL_MONITOR_INA220
168 voltage_read = read_voltage_from_INA220(i2caddress);
169#elif defined CONFIG_VOL_MONITOR_IR36021_READ
170 voltage_read = read_voltage_from_IR(i2caddress);
171#else
172 return -1;
173#endif
174 return voltage_read;
175}
176
177/*
178 * We need to calculate how long before the voltage stops to drop
179 * or increase. It returns with the loop count. Each loop takes
180 * several readings (WAIT_FOR_ADC)
181 */
182static int wait_for_new_voltage(int vdd, int i2caddress)
183{
184 int timeout, vdd_current;
185
186 vdd_current = read_voltage(i2caddress);
187 /* wait until voltage starts to reach the target. Voltage slew
188 * rates by typical regulators will always lead to stable readings
189 * within each fairly long ADC interval in comparison to the
190 * intended voltage delta change until the target voltage is
191 * reached. The fairly small voltage delta change to any target
192 * VID voltage also means that this function will always complete
193 * within few iterations. If the timeout was ever reached, it would
194 * point to a serious failure in the regulator system.
195 */
196 for (timeout = 0;
197 abs(vdd - vdd_current) > (IR_VDD_STEP_UP + IR_VDD_STEP_DOWN) &&
198 timeout < MAX_LOOP_WAIT_NEW_VOL; timeout++) {
199 vdd_current = read_voltage(i2caddress);
200 }
201 if (timeout >= MAX_LOOP_WAIT_NEW_VOL) {
202 printf("VID: Voltage adjustment timeout\n");
203 return -1;
204 }
205 return timeout;
206}
207
208/*
209 * this function keeps reading the voltage until it is stable or until the
210 * timeout expires
211 */
212static int wait_for_voltage_stable(int i2caddress)
213{
214 int timeout, vdd_current, vdd;
215
216 vdd = read_voltage(i2caddress);
217 udelay(NUM_READINGS * WAIT_FOR_ADC);
218
219 /* wait until voltage is stable */
220 vdd_current = read_voltage(i2caddress);
221 /* The maximum timeout is
222 * MAX_LOOP_WAIT_VOL_STABLE * NUM_READINGS * WAIT_FOR_ADC
223 */
224 for (timeout = MAX_LOOP_WAIT_VOL_STABLE;
225 abs(vdd - vdd_current) > ADC_MIN_ACCURACY &&
226 timeout > 0; timeout--) {
227 vdd = vdd_current;
228 udelay(NUM_READINGS * WAIT_FOR_ADC);
229 vdd_current = read_voltage(i2caddress);
230 }
231 if (timeout == 0)
232 return -1;
233 return vdd_current;
234}
235
236#ifdef CONFIG_VOL_MONITOR_IR36021_SET
237/* Set the voltage to the IR chip */
238static int set_voltage_to_IR(int i2caddress, int vdd)
239{
240 int wait, vdd_last;
241 int ret;
242 u8 vid;
243
244 /* Compensate for a board specific voltage drop between regulator and
245 * SoC before converting into an IR VID value
246 */
247 vdd += board_vdd_drop_compensation();
Shaohui Xie02b5d2e2015-11-11 17:58:37 +0800248#ifdef CONFIG_LS1043A
249 vid = DIV_ROUND_UP(vdd - 265, 5);
250#else
Ying Zhang3ad27372014-10-31 18:06:18 +0800251 vid = DIV_ROUND_UP(vdd - 245, 5);
Shaohui Xie02b5d2e2015-11-11 17:58:37 +0800252#endif
Ying Zhang3ad27372014-10-31 18:06:18 +0800253
254 ret = i2c_write(i2caddress, IR36021_LOOP1_MANUAL_ID_OFFSET,
255 1, (void *)&vid, sizeof(vid));
256 if (ret) {
257 printf("VID: failed to write VID\n");
258 return -1;
259 }
260 wait = wait_for_new_voltage(vdd, i2caddress);
261 if (wait < 0)
262 return -1;
263 debug("VID: Waited %d us\n", wait * NUM_READINGS * WAIT_FOR_ADC);
264
265 vdd_last = wait_for_voltage_stable(i2caddress);
266 if (vdd_last < 0)
267 return -1;
268 debug("VID: Current voltage is %d mV\n", vdd_last);
269 return vdd_last;
270}
271#endif
272
273static int set_voltage(int i2caddress, int vdd)
274{
275 int vdd_last = -1;
276
277#ifdef CONFIG_VOL_MONITOR_IR36021_SET
278 vdd_last = set_voltage_to_IR(i2caddress, vdd);
279#else
280 #error Specific voltage monitor must be defined
281#endif
282 return vdd_last;
283}
284
285int adjust_vdd(ulong vdd_override)
286{
287 int re_enable = disable_interrupts();
Shaohui Xie02b5d2e2015-11-11 17:58:37 +0800288#ifdef CONFIG_LS1043A
289 struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
290#else
Ying Zhang3ad27372014-10-31 18:06:18 +0800291 ccsr_gur_t __iomem *gur =
292 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
Shaohui Xie02b5d2e2015-11-11 17:58:37 +0800293#endif
Ying Zhang3ad27372014-10-31 18:06:18 +0800294 u32 fusesr;
Ying Zhangcabe4d22016-01-22 12:15:12 +0800295 u8 vid, buf;
Ying Zhang3ad27372014-10-31 18:06:18 +0800296 int vdd_target, vdd_current, vdd_last;
297 int ret, i2caddress;
298 unsigned long vdd_string_override;
299 char *vdd_string;
300 static const uint16_t vdd[32] = {
301 0, /* unused */
302 9875, /* 0.9875V */
303 9750,
304 9625,
305 9500,
306 9375,
307 9250,
308 9125,
309 9000,
310 8875,
311 8750,
312 8625,
313 8500,
314 8375,
315 8250,
316 8125,
317 10000, /* 1.0000V */
318 10125,
319 10250,
320 10375,
321 10500,
322 10625,
323 10750,
324 10875,
325 11000,
326 0, /* reserved */
327 };
328 struct vdd_drive {
329 u8 vid;
330 unsigned voltage;
331 };
332
333 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
334 if (ret) {
335 debug("VID: I2C failed to switch channel\n");
336 ret = -1;
337 goto exit;
338 }
339 ret = find_ir_chip_on_i2c();
340 if (ret < 0) {
341 printf("VID: Could not find voltage regulator on I2C.\n");
342 ret = -1;
343 goto exit;
344 } else {
345 i2caddress = ret;
346 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
347 }
348
Ying Zhangcabe4d22016-01-22 12:15:12 +0800349 /* check IR chip work on Intel mode*/
350 ret = i2c_read(i2caddress,
351 IR36021_INTEL_MODE_OOFSET,
352 1, (void *)&buf, 1);
353 if (ret) {
354 printf("VID: failed to read IR chip mode.\n");
355 ret = -1;
356 goto exit;
357 }
358 if ((buf & IR36021_MODE_MASK) != IR36021_INTEL_MODE) {
359 printf("VID: IR Chip is not used in Intel mode.\n");
360 ret = -1;
361 goto exit;
362 }
363
Ying Zhang3ad27372014-10-31 18:06:18 +0800364 /* get the voltage ID from fuse status register */
365 fusesr = in_be32(&gur->dcfg_fusesr);
366 /*
367 * VID is used according to the table below
368 * ---------------------------------------
369 * | DA_V |
370 * |-------------------------------------|
371 * | 5b00000 | 5b00001-5b11110 | 5b11111 |
372 * ---------------+---------+-----------------+---------|
373 * | D | 5b00000 | NO VID | VID = DA_V | NO VID |
374 * | A |----------+---------+-----------------+---------|
375 * | _ | 5b00001 |VID = | VID = |VID = |
376 * | V | ~ | DA_V_ALT| DA_V_ALT | DA_A_VLT|
377 * | _ | 5b11110 | | | |
378 * | A |----------+---------+-----------------+---------|
379 * | L | 5b11111 | No VID | VID = DA_V | NO VID |
380 * | T | | | | |
381 * ------------------------------------------------------
382 */
Shaohui Xie02b5d2e2015-11-11 17:58:37 +0800383#ifdef CONFIG_LS1043A
384 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
385 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
386 if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
387 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
388 FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
389 }
390#else
Ying Zhang3ad27372014-10-31 18:06:18 +0800391 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
392 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
393 if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
394 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
395 FSL_CORENET_DCFG_FUSESR_VID_MASK;
396 }
Shaohui Xie02b5d2e2015-11-11 17:58:37 +0800397#endif
Ying Zhang3ad27372014-10-31 18:06:18 +0800398 vdd_target = vdd[vid];
399
400 /* check override variable for overriding VDD */
401 vdd_string = getenv(CONFIG_VID_FLS_ENV);
402 if (vdd_override == 0 && vdd_string &&
403 !strict_strtoul(vdd_string, 10, &vdd_string_override))
404 vdd_override = vdd_string_override;
405 if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
406 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
407 debug("VDD override is %lu\n", vdd_override);
408 } else if (vdd_override != 0) {
409 printf("Invalid value.\n");
410 }
411 if (vdd_target == 0) {
412 debug("VID: VID not used\n");
413 ret = 0;
414 goto exit;
415 } else {
416 /* divide and round up by 10 to get a value in mV */
417 vdd_target = DIV_ROUND_UP(vdd_target, 10);
418 debug("VID: vid = %d mV\n", vdd_target);
419 }
420
421 /*
422 * Read voltage monitor to check real voltage.
423 */
424 vdd_last = read_voltage(i2caddress);
425 if (vdd_last < 0) {
426 printf("VID: Couldn't read sensor abort VID adjustment\n");
427 ret = -1;
428 goto exit;
429 }
430 vdd_current = vdd_last;
431 debug("VID: Core voltage is currently at %d mV\n", vdd_last);
432 /*
433 * Adjust voltage to at or one step above target.
434 * As measurements are less precise than setting the values
435 * we may run through dummy steps that cancel each other
436 * when stepping up and then down.
437 */
438 while (vdd_last > 0 &&
439 vdd_last < vdd_target) {
440 vdd_current += IR_VDD_STEP_UP;
441 vdd_last = set_voltage(i2caddress, vdd_current);
442 }
443 while (vdd_last > 0 &&
444 vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
445 vdd_current -= IR_VDD_STEP_DOWN;
446 vdd_last = set_voltage(i2caddress, vdd_current);
447 }
448
449 if (vdd_last > 0)
450 printf("VID: Core voltage after adjustment is at %d mV\n",
451 vdd_last);
452 else
453 ret = -1;
454exit:
455 if (re_enable)
456 enable_interrupts();
457 return ret;
458}
459
460static int print_vdd(void)
461{
462 int vdd_last, ret, i2caddress;
463
464 ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
465 if (ret) {
466 debug("VID : I2c failed to switch channel\n");
467 return -1;
468 }
469 ret = find_ir_chip_on_i2c();
470 if (ret < 0) {
471 printf("VID: Could not find voltage regulator on I2C.\n");
472 return -1;
473 } else {
474 i2caddress = ret;
475 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
476 }
477
478 /*
479 * Read voltage monitor to check real voltage.
480 */
481 vdd_last = read_voltage(i2caddress);
482 if (vdd_last < 0) {
483 printf("VID: Couldn't read sensor abort VID adjustment\n");
484 return -1;
485 }
486 printf("VID: Core voltage is at %d mV\n", vdd_last);
487
488 return 0;
489}
490
491static int do_vdd_override(cmd_tbl_t *cmdtp,
492 int flag, int argc,
493 char * const argv[])
494{
495 ulong override;
496
497 if (argc < 2)
498 return CMD_RET_USAGE;
499
500 if (!strict_strtoul(argv[1], 10, &override))
501 adjust_vdd(override); /* the value is checked by callee */
502 else
503 return CMD_RET_USAGE;
504 return 0;
505}
506
507static int do_vdd_read(cmd_tbl_t *cmdtp,
508 int flag, int argc,
509 char * const argv[])
510{
511 if (argc < 1)
512 return CMD_RET_USAGE;
513 print_vdd();
514
515 return 0;
516}
517
518U_BOOT_CMD(
519 vdd_override, 2, 0, do_vdd_override,
520 "override VDD",
521 " - override with the voltage specified in mV, eg. 1050"
522);
523
524U_BOOT_CMD(
525 vdd_read, 1, 0, do_vdd_read,
526 "read VDD",
527 " - Read the voltage specified in mV"
528)