keymile: common: update to set_env_hex(), fix "pram" radix
[platform/kernel/u-boot.git] / board / keymile / common / common.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2008
4  * Heiko Schocher, DENX Software Engineering, hs@denx.de.
5  *
6  * (C) Copyright 2011
7  * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com
8  */
9
10 #include <common.h>
11 #include <env.h>
12 #include <ioports.h>
13 #include <command.h>
14 #include <malloc.h>
15 #include <cli_hush.h>
16 #include <net.h>
17 #include <netdev.h>
18 #include <asm/io.h>
19 #include <linux/ctype.h>
20 #include <linux/delay.h>
21
22 #if defined(CONFIG_POST)
23 #include "post.h"
24 #endif
25 #include "common.h"
26 #include <i2c.h>
27
28 DECLARE_GLOBAL_DATA_PTR;
29
30 /*
31  * Set Keymile specific environment variables
32  * Currently only some memory layout variables are calculated here
33  * ... ------------------------------------------------
34  * ... |@rootfsaddr |@pnvramaddr |@varaddr |@reserved |@END_OF_RAM
35  * ... |<------------------- pram ------------------->|
36  * ... ------------------------------------------------
37  * @END_OF_RAM: denotes the RAM size
38  * @pnvramaddr: Startadress of pseudo non volatile RAM in hex
39  * @pram      : preserved ram size in k
40  * @varaddr   : startadress for /var mounted into RAM
41  */
42 int set_km_env(void)
43 {
44         unsigned int pnvramaddr;
45         unsigned int pram;
46         unsigned int varaddr;
47         unsigned int kernelmem;
48         char *p;
49         unsigned long rootfssize = 0;
50
51         pnvramaddr = CONFIG_SYS_SDRAM_BASE + gd->ram_size -
52                 CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM - CONFIG_KM_PNVRAM;
53         env_set_hex("pnvramaddr", pnvramaddr);
54
55         /* try to read rootfssize (ram image) from environment */
56         p = env_get("rootfssize");
57         if (p)
58                 strict_strtoul(p, 16, &rootfssize);
59         pram = (rootfssize + CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM +
60                 CONFIG_KM_PNVRAM) / 0x400;
61         env_set_ulong("pram", pram);
62
63         varaddr = CONFIG_SYS_SDRAM_BASE + gd->ram_size -
64                 CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
65         env_set_hex("varaddr", varaddr);
66
67         kernelmem = gd->ram_size - 0x400 * pram;
68         env_set_hex("kernelmem", kernelmem);
69
70         return 0;
71 }
72
73 #if defined(CONFIG_SYS_I2C_INIT_BOARD)
74 static void i2c_write_start_seq(void)
75 {
76         set_sda(1);
77         udelay(DELAY_HALF_PERIOD);
78         set_scl(1);
79         udelay(DELAY_HALF_PERIOD);
80         set_sda(0);
81         udelay(DELAY_HALF_PERIOD);
82         set_scl(0);
83         udelay(DELAY_HALF_PERIOD);
84 }
85
86 /*
87  * I2C is a synchronous protocol and resets of the processor in the middle
88  * of an access can block the I2C Bus until a powerdown of the full unit is
89  * done. This function toggles the SCL until the SCL and SCA line are
90  * released, but max. 16 times, after this a I2C start-sequence is sent.
91  * This I2C Deblocking mechanism was developed by Keymile in association
92  * with Anatech and Atmel in 1998.
93  */
94 int i2c_make_abort(void)
95 {
96         int     scl_state = 0;
97         int     sda_state = 0;
98         int     i = 0;
99         int     ret = 0;
100
101         if (!get_sda()) {
102                 ret = -1;
103                 while (i < 16) {
104                         i++;
105                         set_scl(0);
106                         udelay(DELAY_ABORT_SEQ);
107                         set_scl(1);
108                         udelay(DELAY_ABORT_SEQ);
109                         scl_state = get_scl();
110                         sda_state = get_sda();
111                         if (scl_state && sda_state) {
112                                 ret = 0;
113                                 break;
114                         }
115                 }
116         }
117         if (ret == 0)
118                 for (i = 0; i < 5; i++)
119                         i2c_write_start_seq();
120
121         /* respect stop setup time */
122         udelay(DELAY_ABORT_SEQ);
123         set_scl(1);
124         udelay(DELAY_ABORT_SEQ);
125         set_sda(1);
126         get_sda();
127
128         return ret;
129 }
130
131 /**
132  * i2c_init_board - reset i2c bus. When the board is powercycled during a
133  * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
134  */
135 void i2c_init_board(void)
136 {
137         /* Now run the AbortSequence() */
138         i2c_make_abort();
139 }
140 #endif
141
142 #if defined(CONFIG_KM_COMMON_ETH_INIT)
143 int board_eth_init(struct bd_info *bis)
144 {
145         if (ethernet_present())
146                 return cpu_eth_init(bis);
147
148         return -1;
149 }
150 #endif
151
152 /*
153  * do_setboardid command
154  * read out the board id and the hw key from the intventory EEPROM and set
155  * this values as environment variables.
156  */
157 static int do_setboardid(struct cmd_tbl *cmdtp, int flag, int argc,
158                          char *const argv[])
159 {
160         unsigned char buf[32];
161         char *p;
162
163         p = get_local_var("IVM_BoardId");
164         if (!p) {
165                 printf("can't get the IVM_Boardid\n");
166                 return 1;
167         }
168         strcpy((char *)buf, p);
169         env_set("boardid", (char *)buf);
170         printf("set boardid=%s\n", buf);
171
172         p = get_local_var("IVM_HWKey");
173         if (!p) {
174                 printf("can't get the IVM_HWKey\n");
175                 return 1;
176         }
177         strcpy((char *)buf, p);
178         env_set("hwkey", (char *)buf);
179         printf("set hwkey=%s\n", buf);
180         printf("Execute manually saveenv for persistent storage.\n");
181
182         return 0;
183 }
184
185 U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid",
186            "read out bid and hwkey from IVM and set in environment");
187
188 /*
189  * command km_checkbidhwk
190  *      if "boardid" and "hwkey" are not already set in the environment, do:
191  *              if a "boardIdListHex" exists in the environment:
192  *                      - read ivm data for boardid and hwkey
193  *                      - compare each entry of the boardIdListHex with the
194  *                              IVM data:
195  *                      if match:
196  *                              set environment variables boardid, boardId,
197  *                              hwkey, hwKey to the found values
198  *                              both (boardid and boardId) are set because
199  *                              they might be used differently in the
200  *                              application and in the init scripts (?)
201  *      return 0 in case of match, 1 if not match or error
202  */
203 static int do_checkboardidhwk(struct cmd_tbl *cmdtp, int flag, int argc,
204                               char *const argv[])
205 {
206         unsigned long ivmbid = 0, ivmhwkey = 0;
207         unsigned long envbid = 0, envhwkey = 0;
208         char *p;
209         int verbose = argc > 1 && *argv[1] == 'v';
210         int rc = 0;
211
212         /*
213          * first read out the real inventory values, these values are
214          * already stored in the local hush variables
215          */
216         p = get_local_var("IVM_BoardId");
217         if (!p) {
218                 printf("can't get the IVM_Boardid\n");
219                 return 1;
220         }
221         rc = strict_strtoul(p, 16, &ivmbid);
222
223         p = get_local_var("IVM_HWKey");
224         if (!p) {
225                 printf("can't get the IVM_HWKey\n");
226                 return 1;
227         }
228         rc = strict_strtoul(p, 16, &ivmhwkey);
229
230         if (!ivmbid || !ivmhwkey) {
231                 printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
232                 return rc;
233         }
234
235         /* now try to read values from environment if available */
236         p = env_get("boardid");
237         if (p)
238                 rc = strict_strtoul(p, 16, &envbid);
239         p = env_get("hwkey");
240         if (p)
241                 rc = strict_strtoul(p, 16, &envhwkey);
242         if (rc != 0) {
243                 printf("strict_strtoul returns error: %d", rc);
244                 return rc;
245         }
246
247         if (!envbid || !envhwkey) {
248                 /*
249                  * BoardId/HWkey not available in the environment, so try the
250                  * environment variable for BoardId/HWkey list
251                  */
252                 char *bidhwklist = env_get("boardIdListHex");
253
254                 if (bidhwklist) {
255                         int found = 0;
256                         char *rest = bidhwklist;
257                         char *endp;
258
259                         if (verbose) {
260                                 printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
261                                        ivmbid, ivmhwkey);
262                                 printf("boardIdHwKeyList: %s\n", bidhwklist);
263                         }
264                         while (!found) {
265                                 /* loop over each bid/hwkey pair in the list */
266                                 unsigned long bid   = 0;
267                                 unsigned long hwkey = 0;
268
269                                 while (*rest && !isxdigit(*rest))
270                                         rest++;
271                                 /*
272                                  * use simple_strtoul because we need &end and
273                                  * we know we got non numeric char at the end
274                                  */
275                                 bid = simple_strtoul(rest, &endp, 16);
276                                 /* BoardId and HWkey are separated with a "_" */
277                                 if (*endp == '_') {
278                                         rest  = endp + 1;
279                                         /*
280                                          * use simple_strtoul because we need
281                                          * &end
282                                          */
283                                         hwkey = simple_strtoul(rest, &endp, 16);
284                                         rest  = endp;
285                                         while (*rest && !isxdigit(*rest))
286                                                 rest++;
287                                 }
288                                 if (!bid || !hwkey) {
289                                         /* end of list */
290                                         break;
291                                 }
292                                 if (verbose) {
293                                         printf("trying bid=0x%lX, hwkey=%ld\n",
294                                                bid, hwkey);
295                                 }
296                                 /*
297                                  * Compare the values of the found entry in the
298                                  * list with the valid values which are stored
299                                  * in the inventory eeprom. If they are equal
300                                  * set the values in environment variables.
301                                  */
302                                 if (bid == ivmbid && hwkey == ivmhwkey) {
303                                         found = 1;
304                                         envbid   = bid;
305                                         envhwkey = hwkey;
306                                         env_set_hex("boardid", bid);
307                                         env_set_hex("hwkey", hwkey);
308                                 }
309                         } /* end while( ! found ) */
310                 }
311         }
312
313         /* compare now the values */
314         if (ivmbid == envbid && ivmhwkey == envhwkey) {
315                 printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
316                 rc = 0; /* match */
317         } else {
318                 printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
319                        envhwkey);
320                 printf("       IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
321                 rc = 1; /* don't match */
322         }
323         return rc;
324 }
325
326 U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
327            "check boardid and hwkey",
328            "[v]\n  - check environment parameter \"boardIdListHex\" against stored boardid and hwkey from the IVM\n    v: verbose output"
329 );
330
331 /*
332  * command km_checktestboot
333  *  if the testpin of the board is asserted, return 1
334  *  *   else return 0
335  */
336 static int do_checktestboot(struct cmd_tbl *cmdtp, int flag, int argc,
337                             char *const argv[])
338 {
339         int testpin = 0;
340         char *s = NULL;
341         int testboot = 0;
342         int verbose = argc > 1 && *argv[1] == 'v';
343
344 #if defined(CONFIG_POST)
345         testpin = post_hotkeys_pressed();
346 #endif
347
348         s = env_get("test_bank");
349         /* when test_bank is not set, act as if testpin is not asserted */
350         testboot = (testpin != 0) && (s);
351         if (verbose) {
352                 printf("testpin   = %d\n", testpin);
353                 /* cppcheck-suppress nullPointer */
354                 printf("test_bank = %s\n", s ? s : "not set");
355                 printf("boot test app : %s\n", (testboot) ? "yes" : "no");
356         }
357         /* return 0 means: testboot, therefore we need the inversion */
358         return !testboot;
359 }
360
361 U_BOOT_CMD(km_checktestboot, 2, 0, do_checktestboot,
362            "check if testpin is asserted",
363            "[v]\n  v - verbose output"
364 );