1 // SPDX-License-Identifier: GPL-2.0+
4 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
7 * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com
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
44 unsigned int pnvramaddr;
47 unsigned int kernelmem;
49 unsigned long rootfssize = 0;
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);
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 env_set_ulong("pram", pram);
63 varaddr = CONFIG_SYS_SDRAM_BASE + gd->ram_size -
64 CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
65 env_set_hex("varaddr", varaddr);
67 kernelmem = gd->ram_size - 0x400 * pram;
68 env_set_hex("kernelmem", kernelmem);
73 #if defined(CONFIG_SYS_I2C_INIT_BOARD)
74 static void i2c_write_start_seq(void)
77 udelay(DELAY_HALF_PERIOD);
79 udelay(DELAY_HALF_PERIOD);
81 udelay(DELAY_HALF_PERIOD);
83 udelay(DELAY_HALF_PERIOD);
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.
94 int i2c_make_abort(void)
106 udelay(DELAY_ABORT_SEQ);
108 udelay(DELAY_ABORT_SEQ);
109 scl_state = get_scl();
110 sda_state = get_sda();
111 if (scl_state && sda_state) {
118 for (i = 0; i < 5; i++)
119 i2c_write_start_seq();
121 /* respect stop setup time */
122 udelay(DELAY_ABORT_SEQ);
124 udelay(DELAY_ABORT_SEQ);
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.
135 void i2c_init_board(void)
137 /* Now run the AbortSequence() */
142 #if defined(CONFIG_KM_COMMON_ETH_INIT)
143 int board_eth_init(struct bd_info *bis)
145 if (ethernet_present())
146 return cpu_eth_init(bis);
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.
157 static int do_setboardid(struct cmd_tbl *cmdtp, int flag, int argc,
160 unsigned char buf[32];
163 p = get_local_var("IVM_BoardId");
165 printf("can't get the IVM_Boardid\n");
168 strcpy((char *)buf, p);
169 env_set("boardid", (char *)buf);
170 printf("set boardid=%s\n", buf);
172 p = get_local_var("IVM_HWKey");
174 printf("can't get the IVM_HWKey\n");
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");
185 U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid",
186 "read out bid and hwkey from IVM and set in environment");
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
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
203 static int do_checkboardidhwk(struct cmd_tbl *cmdtp, int flag, int argc,
206 unsigned long ivmbid = 0, ivmhwkey = 0;
207 unsigned long envbid = 0, envhwkey = 0;
209 int verbose = argc > 1 && *argv[1] == 'v';
213 * first read out the real inventory values, these values are
214 * already stored in the local hush variables
216 p = get_local_var("IVM_BoardId");
218 printf("can't get the IVM_Boardid\n");
221 rc = strict_strtoul(p, 16, &ivmbid);
223 p = get_local_var("IVM_HWKey");
225 printf("can't get the IVM_HWKey\n");
228 rc = strict_strtoul(p, 16, &ivmhwkey);
230 if (!ivmbid || !ivmhwkey) {
231 printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
235 /* now try to read values from environment if available */
236 p = env_get("boardid");
238 rc = strict_strtoul(p, 16, &envbid);
239 p = env_get("hwkey");
241 rc = strict_strtoul(p, 16, &envhwkey);
243 printf("strict_strtoul returns error: %d", rc);
247 if (!envbid || !envhwkey) {
249 * BoardId/HWkey not available in the environment, so try the
250 * environment variable for BoardId/HWkey list
252 char *bidhwklist = env_get("boardIdListHex");
256 char *rest = bidhwklist;
260 printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
262 printf("boardIdHwKeyList: %s\n", bidhwklist);
265 /* loop over each bid/hwkey pair in the list */
266 unsigned long bid = 0;
267 unsigned long hwkey = 0;
269 while (*rest && !isxdigit(*rest))
272 * use simple_strtoul because we need &end and
273 * we know we got non numeric char at the end
275 bid = simple_strtoul(rest, &endp, 16);
276 /* BoardId and HWkey are separated with a "_" */
280 * use simple_strtoul because we need
283 hwkey = simple_strtoul(rest, &endp, 16);
285 while (*rest && !isxdigit(*rest))
288 if (!bid || !hwkey) {
293 printf("trying bid=0x%lX, hwkey=%ld\n",
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.
302 if (bid == ivmbid && hwkey == ivmhwkey) {
306 env_set_hex("boardid", bid);
307 env_set_hex("hwkey", hwkey);
309 } /* end while( ! found ) */
313 /* compare now the values */
314 if (ivmbid == envbid && ivmhwkey == envhwkey) {
315 printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
318 printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
320 printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
321 rc = 1; /* don't match */
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"
332 * command km_checktestboot
333 * if the testpin of the board is asserted, return 1
336 static int do_checktestboot(struct cmd_tbl *cmdtp, int flag, int argc,
342 int verbose = argc > 1 && *argv[1] == 'v';
344 #if defined(CONFIG_POST)
345 testpin = post_hotkeys_pressed();
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);
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");
357 /* return 0 means: testboot, therefore we need the inversion */
361 U_BOOT_CMD(km_checktestboot, 2, 0, do_checktestboot,
362 "check if testpin is asserted",
363 "[v]\n v - verbose output"