1 // SPDX-License-Identifier: GPL-2.0+
19 #include <linux/ctype.h>
20 #include <linux/delay.h>
22 #if defined(CONFIG_POST)
28 DECLARE_GLOBAL_DATA_PTR;
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
45 unsigned int pnvramaddr;
48 unsigned int kernelmem;
50 unsigned long rootfssize = 0;
52 pnvramaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM
54 sprintf((char *)buf, "0x%x", pnvramaddr);
55 env_set("pnvramaddr", (char *)buf);
57 /* try to read rootfssize (ram image) from environment */
58 p = env_get("rootfssize");
60 strict_strtoul(p, 16, &rootfssize);
61 pram = (rootfssize + CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM +
62 CONFIG_KM_PNVRAM) / 0x400;
63 sprintf((char *)buf, "0x%x", pram);
64 env_set("pram", (char *)buf);
66 varaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
67 sprintf((char *)buf, "0x%x", varaddr);
68 env_set("varaddr", (char *)buf);
70 kernelmem = gd->ram_size - 0x400 * pram;
71 sprintf((char *)buf, "0x%x", kernelmem);
72 env_set("kernelmem", (char *)buf);
77 #if defined(CONFIG_SYS_I2C_INIT_BOARD)
78 static void i2c_write_start_seq(void)
81 udelay(DELAY_HALF_PERIOD);
83 udelay(DELAY_HALF_PERIOD);
85 udelay(DELAY_HALF_PERIOD);
87 udelay(DELAY_HALF_PERIOD);
91 * I2C is a synchronous protocol and resets of the processor in the middle
92 * of an access can block the I2C Bus until a powerdown of the full unit is
93 * done. This function toggles the SCL until the SCL and SCA line are
94 * released, but max. 16 times, after this a I2C start-sequence is sent.
95 * This I2C Deblocking mechanism was developed by Keymile in association
96 * with Anatech and Atmel in 1998.
98 int i2c_make_abort(void)
110 udelay(DELAY_ABORT_SEQ);
112 udelay(DELAY_ABORT_SEQ);
113 scl_state = get_scl();
114 sda_state = get_sda();
115 if (scl_state && sda_state) {
122 for (i = 0; i < 5; i++)
123 i2c_write_start_seq();
125 /* respect stop setup time */
126 udelay(DELAY_ABORT_SEQ);
128 udelay(DELAY_ABORT_SEQ);
136 * i2c_init_board - reset i2c bus. When the board is powercycled during a
137 * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
139 void i2c_init_board(void)
141 /* Now run the AbortSequence() */
146 #if defined(CONFIG_KM_COMMON_ETH_INIT)
147 int board_eth_init(struct bd_info *bis)
149 if (ethernet_present())
150 return cpu_eth_init(bis);
157 * do_setboardid command
158 * read out the board id and the hw key from the intventory EEPROM and set
159 * this values as environment variables.
161 static int do_setboardid(struct cmd_tbl *cmdtp, int flag, int argc,
164 unsigned char buf[32];
167 p = get_local_var("IVM_BoardId");
169 printf("can't get the IVM_Boardid\n");
172 strcpy((char *)buf, p);
173 env_set("boardid", (char *)buf);
174 printf("set boardid=%s\n", buf);
176 p = get_local_var("IVM_HWKey");
178 printf("can't get the IVM_HWKey\n");
181 strcpy((char *)buf, p);
182 env_set("hwkey", (char *)buf);
183 printf("set hwkey=%s\n", buf);
184 printf("Execute manually saveenv for persistent storage.\n");
189 U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid",
190 "read out bid and hwkey from IVM and set in environment");
193 * command km_checkbidhwk
194 * if "boardid" and "hwkey" are not already set in the environment, do:
195 * if a "boardIdListHex" exists in the environment:
196 * - read ivm data for boardid and hwkey
197 * - compare each entry of the boardIdListHex with the
200 * set environment variables boardid, boardId,
201 * hwkey, hwKey to the found values
202 * both (boardid and boardId) are set because
203 * they might be used differently in the
204 * application and in the init scripts (?)
205 * return 0 in case of match, 1 if not match or error
207 static int do_checkboardidhwk(struct cmd_tbl *cmdtp, int flag, int argc,
210 unsigned long ivmbid = 0, ivmhwkey = 0;
211 unsigned long envbid = 0, envhwkey = 0;
213 int verbose = argc > 1 && *argv[1] == 'v';
217 * first read out the real inventory values, these values are
218 * already stored in the local hush variables
220 p = get_local_var("IVM_BoardId");
222 printf("can't get the IVM_Boardid\n");
225 rc = strict_strtoul(p, 16, &ivmbid);
227 p = get_local_var("IVM_HWKey");
229 printf("can't get the IVM_HWKey\n");
232 rc = strict_strtoul(p, 16, &ivmhwkey);
234 if (!ivmbid || !ivmhwkey) {
235 printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
239 /* now try to read values from environment if available */
240 p = env_get("boardid");
242 rc = strict_strtoul(p, 16, &envbid);
243 p = env_get("hwkey");
245 rc = strict_strtoul(p, 16, &envhwkey);
248 printf("strict_strtoul returns error: %d", rc);
252 if (!envbid || !envhwkey) {
254 * BoardId/HWkey not available in the environment, so try the
255 * environment variable for BoardId/HWkey list
257 char *bidhwklist = env_get("boardIdListHex");
261 char *rest = bidhwklist;
265 printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
267 printf("boardIdHwKeyList: %s\n", bidhwklist);
270 /* loop over each bid/hwkey pair in the list */
271 unsigned long bid = 0;
272 unsigned long hwkey = 0;
274 while (*rest && !isxdigit(*rest))
277 * use simple_strtoul because we need &end and
278 * we know we got non numeric char at the end
280 bid = simple_strtoul(rest, &endp, 16);
281 /* BoardId and HWkey are separated with a "_" */
285 * use simple_strtoul because we need
288 hwkey = simple_strtoul(rest, &endp, 16);
290 while (*rest && !isxdigit(*rest))
293 if (!bid || !hwkey) {
298 printf("trying bid=0x%lX, hwkey=%ld\n",
302 * Compare the values of the found entry in the
303 * list with the valid values which are stored
304 * in the inventory eeprom. If they are equal
305 * set the values in environment variables.
307 if (bid == ivmbid && hwkey == ivmhwkey) {
313 sprintf(buf, "%lx", bid);
314 env_set("boardid", buf);
315 sprintf(buf, "%lx", hwkey);
316 env_set("hwkey", buf);
318 } /* end while( ! found ) */
322 /* compare now the values */
323 if (ivmbid == envbid && ivmhwkey == envhwkey) {
324 printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
327 printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
329 printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
330 rc = 1; /* don't match */
335 U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
336 "check boardid and hwkey",
337 "[v]\n - check environment parameter \"boardIdListHex\" against stored boardid and hwkey from the IVM\n v: verbose output"
341 * command km_checktestboot
342 * if the testpin of the board is asserted, return 1
345 static int do_checktestboot(struct cmd_tbl *cmdtp, int flag, int argc,
351 int verbose = argc > 1 && *argv[1] == 'v';
353 #if defined(CONFIG_POST)
354 testpin = post_hotkeys_pressed();
357 s = env_get("test_bank");
358 /* when test_bank is not set, act as if testpin is not asserted */
359 testboot = (testpin != 0) && (s);
361 printf("testpin = %d\n", testpin);
362 /* cppcheck-suppress nullPointer */
363 printf("test_bank = %s\n", s ? s : "not set");
364 printf("boot test app : %s\n", (testboot) ? "yes" : "no");
366 /* return 0 means: testboot, therefore we need the inversion */
370 U_BOOT_CMD(km_checktestboot, 2, 0, do_checktestboot,
371 "check if testpin is asserted",
372 "[v]\n v - verbose output"