1 // SPDX-License-Identifier: GPL-2.0+
18 #include <linux/ctype.h>
20 #if defined(CONFIG_POST)
26 DECLARE_GLOBAL_DATA_PTR;
29 * Set Keymile specific environment variables
30 * Currently only some memory layout variables are calculated here
31 * ... ------------------------------------------------
32 * ... |@rootfsaddr |@pnvramaddr |@varaddr |@reserved |@END_OF_RAM
33 * ... |<------------------- pram ------------------->|
34 * ... ------------------------------------------------
35 * @END_OF_RAM: denotes the RAM size
36 * @pnvramaddr: Startadress of pseudo non volatile RAM in hex
37 * @pram : preserved ram size in k
38 * @varaddr : startadress for /var mounted into RAM
43 unsigned int pnvramaddr;
46 unsigned int kernelmem;
48 unsigned long rootfssize = 0;
50 pnvramaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM
52 sprintf((char *)buf, "0x%x", pnvramaddr);
53 env_set("pnvramaddr", (char *)buf);
55 /* try to read rootfssize (ram image) from environment */
56 p = env_get("rootfssize");
58 strict_strtoul(p, 16, &rootfssize);
59 pram = (rootfssize + CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM +
60 CONFIG_KM_PNVRAM) / 0x400;
61 sprintf((char *)buf, "0x%x", pram);
62 env_set("pram", (char *)buf);
64 varaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
65 sprintf((char *)buf, "0x%x", varaddr);
66 env_set("varaddr", (char *)buf);
68 kernelmem = gd->ram_size - 0x400 * pram;
69 sprintf((char *)buf, "0x%x", kernelmem);
70 env_set("kernelmem", (char *)buf);
75 #if defined(CONFIG_SYS_I2C_INIT_BOARD)
76 static void i2c_write_start_seq(void)
79 udelay(DELAY_HALF_PERIOD);
81 udelay(DELAY_HALF_PERIOD);
83 udelay(DELAY_HALF_PERIOD);
85 udelay(DELAY_HALF_PERIOD);
89 * I2C is a synchronous protocol and resets of the processor in the middle
90 * of an access can block the I2C Bus until a powerdown of the full unit is
91 * done. This function toggles the SCL until the SCL and SCA line are
92 * released, but max. 16 times, after this a I2C start-sequence is sent.
93 * This I2C Deblocking mechanism was developed by Keymile in association
94 * with Anatech and Atmel in 1998.
96 int i2c_make_abort(void)
108 udelay(DELAY_ABORT_SEQ);
110 udelay(DELAY_ABORT_SEQ);
111 scl_state = get_scl();
112 sda_state = get_sda();
113 if (scl_state && sda_state) {
120 for (i = 0; i < 5; i++)
121 i2c_write_start_seq();
123 /* respect stop setup time */
124 udelay(DELAY_ABORT_SEQ);
126 udelay(DELAY_ABORT_SEQ);
134 * i2c_init_board - reset i2c bus. When the board is powercycled during a
135 * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
137 void i2c_init_board(void)
139 /* Now run the AbortSequence() */
144 #if defined(CONFIG_KM_COMMON_ETH_INIT)
145 int board_eth_init(bd_t *bis)
147 if (ethernet_present())
148 return cpu_eth_init(bis);
155 * do_setboardid command
156 * read out the board id and the hw key from the intventory EEPROM and set
157 * this values as environment variables.
159 static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc,
162 unsigned char buf[32];
165 p = get_local_var("IVM_BoardId");
167 printf("can't get the IVM_Boardid\n");
170 strcpy((char *)buf, p);
171 env_set("boardid", (char *)buf);
172 printf("set boardid=%s\n", buf);
174 p = get_local_var("IVM_HWKey");
176 printf("can't get the IVM_HWKey\n");
179 strcpy((char *)buf, p);
180 env_set("hwkey", (char *)buf);
181 printf("set hwkey=%s\n", buf);
182 printf("Execute manually saveenv for persistent storage.\n");
187 U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
188 "hwkey from IVM and set in environment");
191 * command km_checkbidhwk
192 * if "boardid" and "hwkey" are not already set in the environment, do:
193 * if a "boardIdListHex" exists in the environment:
194 * - read ivm data for boardid and hwkey
195 * - compare each entry of the boardIdListHex with the
198 * set environment variables boardid, boardId,
199 * hwkey, hwKey to the found values
200 * both (boardid and boardId) are set because
201 * they might be used differently in the
202 * application and in the init scripts (?)
203 * return 0 in case of match, 1 if not match or error
205 static int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
208 unsigned long ivmbid = 0, ivmhwkey = 0;
209 unsigned long envbid = 0, envhwkey = 0;
211 int verbose = argc > 1 && *argv[1] == 'v';
215 * first read out the real inventory values, these values are
216 * already stored in the local hush variables
218 p = get_local_var("IVM_BoardId");
220 printf("can't get the IVM_Boardid\n");
223 rc = strict_strtoul(p, 16, &ivmbid);
225 p = get_local_var("IVM_HWKey");
227 printf("can't get the IVM_HWKey\n");
230 rc = strict_strtoul(p, 16, &ivmhwkey);
232 if (!ivmbid || !ivmhwkey) {
233 printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
237 /* now try to read values from environment if available */
238 p = env_get("boardid");
240 rc = strict_strtoul(p, 16, &envbid);
241 p = env_get("hwkey");
243 rc = strict_strtoul(p, 16, &envhwkey);
246 printf("strict_strtoul returns error: %d", rc);
250 if (!envbid || !envhwkey) {
252 * BoardId/HWkey not available in the environment, so try the
253 * environment variable for BoardId/HWkey list
255 char *bidhwklist = env_get("boardIdListHex");
259 char *rest = bidhwklist;
263 printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
265 printf("boardIdHwKeyList: %s\n",
269 /* loop over each bid/hwkey pair in the list */
270 unsigned long bid = 0;
271 unsigned long hwkey = 0;
273 while (*rest && !isxdigit(*rest))
276 * use simple_strtoul because we need &end and
277 * we know we got non numeric char at the end
279 bid = simple_strtoul(rest, &endp, 16);
280 /* BoardId and HWkey are separated with a "_" */
284 * use simple_strtoul because we need
287 hwkey = simple_strtoul(rest, &endp, 16);
289 while (*rest && !isxdigit(*rest))
292 if ((!bid) || (!hwkey)) {
297 printf("trying bid=0x%lX, hwkey=%ld\n",
301 * Compare the values of the found entry in the
302 * list with the valid values which are stored
303 * in the inventory eeprom. If they are equal
304 * set the values in environment variables.
306 if ((bid == ivmbid) && (hwkey == ivmhwkey)) {
312 sprintf(buf, "%lx", bid);
313 env_set("boardid", buf);
314 sprintf(buf, "%lx", hwkey);
315 env_set("hwkey", buf);
317 } /* end while( ! found ) */
321 /* compare now the values */
322 if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) {
323 printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
326 printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
328 printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
329 rc = 1; /* don't match */
334 U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
335 "check boardid and hwkey",
336 "[v]\n - check environment parameter "\
337 "\"boardIdListHex\" against stored boardid and hwkey "\
338 "from the IVM\n v: verbose output"
342 * command km_checktestboot
343 * if the testpin of the board is asserted, return 1
346 static int do_checktestboot(cmd_tbl_t *cmdtp, int flag, int argc,
352 int verbose = argc > 1 && *argv[1] == 'v';
354 #if defined(CONFIG_POST)
355 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"