armv8/ls1043aqds: add LS1043AQDS board support
[platform/kernel/u-boot.git] / board / freescale / common / vid.c
1 /*
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>
10 #include <asm/io.h>
11 #ifdef CONFIG_LS1043A
12 #include <asm/arch/immap_lsch2.h>
13 #else
14 #include <asm/immap_85xx.h>
15 #endif
16 #include "vid.h"
17
18 DECLARE_GLOBAL_DATA_PTR;
19
20 int __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  */
29 int __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  */
47 static 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
90 static 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
123 static 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
164 static 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  */
182 static 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  */
212 static 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 */
238 static 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();
248 #ifdef CONFIG_LS1043A
249         vid = DIV_ROUND_UP(vdd - 265, 5);
250 #else
251         vid = DIV_ROUND_UP(vdd - 245, 5);
252 #endif
253
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
273 static 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
285 int adjust_vdd(ulong vdd_override)
286 {
287         int re_enable = disable_interrupts();
288 #ifdef CONFIG_LS1043A
289         struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
290 #else
291         ccsr_gur_t __iomem *gur =
292                 (void __iomem *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
293 #endif
294         u32 fusesr;
295         u8 vid;
296         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
349         /* get the voltage ID from fuse status register */
350         fusesr = in_be32(&gur->dcfg_fusesr);
351         /*
352          * VID is used according to the table below
353          *                ---------------------------------------
354          *                |                DA_V                 |
355          *                |-------------------------------------|
356          *                | 5b00000 | 5b00001-5b11110 | 5b11111 |
357          * ---------------+---------+-----------------+---------|
358          * | D | 5b00000  | NO VID  | VID = DA_V      | NO VID  |
359          * | A |----------+---------+-----------------+---------|
360          * | _ | 5b00001  |VID =    | VID =           |VID =    |
361          * | V |   ~      | DA_V_ALT|   DA_V_ALT      | DA_A_VLT|
362          * | _ | 5b11110  |         |                 |         |
363          * | A |----------+---------+-----------------+---------|
364          * | L | 5b11111  | No VID  | VID = DA_V      | NO VID  |
365          * | T |          |         |                 |         |
366          * ------------------------------------------------------
367          */
368 #ifdef CONFIG_LS1043A
369         vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_ALTVID_SHIFT) &
370                 FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK;
371         if ((vid == 0) || (vid == FSL_CHASSIS2_DCFG_FUSESR_ALTVID_MASK)) {
372                 vid = (fusesr >> FSL_CHASSIS2_DCFG_FUSESR_VID_SHIFT) &
373                         FSL_CHASSIS2_DCFG_FUSESR_VID_MASK;
374         }
375 #else
376         vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_ALTVID_SHIFT) &
377                 FSL_CORENET_DCFG_FUSESR_ALTVID_MASK;
378         if ((vid == 0) || (vid == FSL_CORENET_DCFG_FUSESR_ALTVID_MASK)) {
379                 vid = (fusesr >> FSL_CORENET_DCFG_FUSESR_VID_SHIFT) &
380                         FSL_CORENET_DCFG_FUSESR_VID_MASK;
381         }
382 #endif
383         vdd_target = vdd[vid];
384
385         /* check override variable for overriding VDD */
386         vdd_string = getenv(CONFIG_VID_FLS_ENV);
387         if (vdd_override == 0 && vdd_string &&
388             !strict_strtoul(vdd_string, 10, &vdd_string_override))
389                 vdd_override = vdd_string_override;
390         if (vdd_override >= VDD_MV_MIN && vdd_override <= VDD_MV_MAX) {
391                 vdd_target = vdd_override * 10; /* convert to 1/10 mV */
392                 debug("VDD override is %lu\n", vdd_override);
393         } else if (vdd_override != 0) {
394                 printf("Invalid value.\n");
395         }
396         if (vdd_target == 0) {
397                 debug("VID: VID not used\n");
398                 ret = 0;
399                 goto exit;
400         } else {
401                 /* divide and round up by 10 to get a value in mV */
402                 vdd_target = DIV_ROUND_UP(vdd_target, 10);
403                 debug("VID: vid = %d mV\n", vdd_target);
404         }
405
406         /*
407          * Read voltage monitor to check real voltage.
408          */
409         vdd_last = read_voltage(i2caddress);
410         if (vdd_last < 0) {
411                 printf("VID: Couldn't read sensor abort VID adjustment\n");
412                 ret = -1;
413                 goto exit;
414         }
415         vdd_current = vdd_last;
416         debug("VID: Core voltage is currently at %d mV\n", vdd_last);
417         /*
418           * Adjust voltage to at or one step above target.
419           * As measurements are less precise than setting the values
420           * we may run through dummy steps that cancel each other
421           * when stepping up and then down.
422           */
423         while (vdd_last > 0 &&
424                vdd_last < vdd_target) {
425                 vdd_current += IR_VDD_STEP_UP;
426                 vdd_last = set_voltage(i2caddress, vdd_current);
427         }
428         while (vdd_last > 0 &&
429                vdd_last > vdd_target + (IR_VDD_STEP_DOWN - 1)) {
430                 vdd_current -= IR_VDD_STEP_DOWN;
431                 vdd_last = set_voltage(i2caddress, vdd_current);
432         }
433
434         if (vdd_last > 0)
435                 printf("VID: Core voltage after adjustment is at %d mV\n",
436                        vdd_last);
437         else
438                 ret = -1;
439 exit:
440         if (re_enable)
441                 enable_interrupts();
442         return ret;
443 }
444
445 static int print_vdd(void)
446 {
447         int vdd_last, ret, i2caddress;
448
449         ret = i2c_multiplexer_select_vid_channel(I2C_MUX_CH_VOL_MONITOR);
450         if (ret) {
451                 debug("VID : I2c failed to switch channel\n");
452                 return -1;
453         }
454         ret = find_ir_chip_on_i2c();
455         if (ret < 0) {
456                 printf("VID: Could not find voltage regulator on I2C.\n");
457                 return -1;
458         } else {
459                 i2caddress = ret;
460                 debug("VID: IR Chip found on I2C address 0x%02x\n", i2caddress);
461         }
462
463         /*
464          * Read voltage monitor to check real voltage.
465          */
466         vdd_last = read_voltage(i2caddress);
467         if (vdd_last < 0) {
468                 printf("VID: Couldn't read sensor abort VID adjustment\n");
469                 return -1;
470         }
471         printf("VID: Core voltage is at %d mV\n", vdd_last);
472
473         return 0;
474 }
475
476 static int do_vdd_override(cmd_tbl_t *cmdtp,
477                            int flag, int argc,
478                            char * const argv[])
479 {
480         ulong override;
481
482         if (argc < 2)
483                 return CMD_RET_USAGE;
484
485         if (!strict_strtoul(argv[1], 10, &override))
486                 adjust_vdd(override);   /* the value is checked by callee */
487         else
488                 return CMD_RET_USAGE;
489         return 0;
490 }
491
492 static int do_vdd_read(cmd_tbl_t *cmdtp,
493                          int flag, int argc,
494                          char * const argv[])
495 {
496         if (argc < 1)
497                 return CMD_RET_USAGE;
498         print_vdd();
499
500         return 0;
501 }
502
503 U_BOOT_CMD(
504         vdd_override, 2, 0, do_vdd_override,
505         "override VDD",
506         " - override with the voltage specified in mV, eg. 1050"
507 );
508
509 U_BOOT_CMD(
510         vdd_read, 1, 0, do_vdd_read,
511         "read VDD",
512         " - Read the voltage specified in mV"
513 )