3 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
6 * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com
8 * SPDX-License-Identifier: GPL-2.0+
19 #include <linux/ctype.h>
21 #if defined(CONFIG_POST)
27 #if !defined(CONFIG_MPC83xx)
28 static void i2c_write_start_seq(void);
31 DECLARE_GLOBAL_DATA_PTR;
34 * Set Keymile specific environment variables
35 * Currently only some memory layout variables are calculated here
36 * ... ------------------------------------------------
37 * ... |@rootfsaddr |@pnvramaddr |@varaddr |@reserved |@END_OF_RAM
38 * ... |<------------------- pram ------------------->|
39 * ... ------------------------------------------------
40 * @END_OF_RAM: denotes the RAM size
41 * @pnvramaddr: Startadress of pseudo non volatile RAM in hex
42 * @pram : preserved ram size in k
43 * @varaddr : startadress for /var mounted into RAM
48 unsigned int pnvramaddr;
51 unsigned int kernelmem;
53 unsigned long rootfssize = 0;
55 pnvramaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM
57 sprintf((char *)buf, "0x%x", pnvramaddr);
58 setenv("pnvramaddr", (char *)buf);
60 /* try to read rootfssize (ram image) from envrionment */
61 p = getenv("rootfssize");
63 strict_strtoul(p, 16, &rootfssize);
64 pram = (rootfssize + CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM +
65 CONFIG_KM_PNVRAM) / 0x400;
66 sprintf((char *)buf, "0x%x", pram);
67 setenv("pram", (char *)buf);
69 varaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
70 sprintf((char *)buf, "0x%x", varaddr);
71 setenv("varaddr", (char *)buf);
73 kernelmem = gd->ram_size - 0x400 * pram;
74 sprintf((char *)buf, "0x%x", kernelmem);
75 setenv("kernelmem", (char *)buf);
80 #if defined(CONFIG_SYS_I2C_INIT_BOARD)
81 #if !defined(CONFIG_MPC83xx)
82 static void i2c_write_start_seq(void)
85 udelay(DELAY_HALF_PERIOD);
87 udelay(DELAY_HALF_PERIOD);
89 udelay(DELAY_HALF_PERIOD);
91 udelay(DELAY_HALF_PERIOD);
95 * I2C is a synchronous protocol and resets of the processor in the middle
96 * of an access can block the I2C Bus until a powerdown of the full unit is
97 * done. This function toggles the SCL until the SCL and SCA line are
98 * released, but max. 16 times, after this a I2C start-sequence is sent.
99 * This I2C Deblocking mechanism was developed by Keymile in association
100 * with Anatech and Atmel in 1998.
102 int i2c_make_abort(void)
105 #if defined(CONFIG_HARD_I2C) && !defined(MACH_TYPE_KM_KIRKWOOD)
106 immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
107 i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
110 * disable I2C controller first, otherwhise it thinks we want to
111 * talk to the slave port...
113 clrbits_8(&i2c->i2c_i2mod, 0x01);
115 /* Set the PortPins to GPIO */
129 udelay(DELAY_ABORT_SEQ);
131 udelay(DELAY_ABORT_SEQ);
132 scl_state = get_scl();
133 sda_state = get_sda();
134 if (scl_state && sda_state) {
141 for (i = 0; i < 5; i++)
142 i2c_write_start_seq();
144 /* respect stop setup time */
145 udelay(DELAY_ABORT_SEQ);
147 udelay(DELAY_ABORT_SEQ);
151 #if defined(CONFIG_HARD_I2C)
152 /* Set the PortPins back to use for I2C */
160 * i2c_init_board - reset i2c bus. When the board is powercycled during a
161 * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
163 void i2c_init_board(void)
165 /* Now run the AbortSequence() */
170 int board_eth_init(bd_t *bis)
172 if (ethernet_present())
173 return cpu_eth_init(bis);
179 * do_setboardid command
180 * read out the board id and the hw key from the intventory EEPROM and set
181 * this values as environment variables.
183 static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc,
186 unsigned char buf[32];
189 p = get_local_var("IVM_BoardId");
191 printf("can't get the IVM_Boardid\n");
194 sprintf((char *)buf, "%s", p);
195 setenv("boardid", (char *)buf);
196 printf("set boardid=%s\n", buf);
198 p = get_local_var("IVM_HWKey");
200 printf("can't get the IVM_HWKey\n");
203 sprintf((char *)buf, "%s", p);
204 setenv("hwkey", (char *)buf);
205 printf("set hwkey=%s\n", buf);
206 printf("Execute manually saveenv for persistent storage.\n");
211 U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
212 "hwkey from IVM and set in environment");
215 * command km_checkbidhwk
216 * if "boardid" and "hwkey" are not already set in the environment, do:
217 * if a "boardIdListHex" exists in the environment:
218 * - read ivm data for boardid and hwkey
219 * - compare each entry of the boardIdListHex with the
222 * set environment variables boardid, boardId,
223 * hwkey, hwKey to the found values
224 * both (boardid and boardId) are set because
225 * they might be used differently in the
226 * application and in the init scripts (?)
227 * return 0 in case of match, 1 if not match or error
229 int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
232 unsigned long ivmbid = 0, ivmhwkey = 0;
233 unsigned long envbid = 0, envhwkey = 0;
235 int verbose = argc > 1 && *argv[1] == 'v';
239 * first read out the real inventory values, these values are
240 * already stored in the local hush variables
242 p = get_local_var("IVM_BoardId");
244 printf("can't get the IVM_Boardid\n");
247 rc = strict_strtoul(p, 16, &ivmbid);
249 p = get_local_var("IVM_HWKey");
251 printf("can't get the IVM_HWKey\n");
254 rc = strict_strtoul(p, 16, &ivmhwkey);
256 if (!ivmbid || !ivmhwkey) {
257 printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
261 /* now try to read values from environment if available */
262 p = getenv("boardid");
264 rc = strict_strtoul(p, 16, &envbid);
267 rc = strict_strtoul(p, 16, &envhwkey);
270 printf("strict_strtoul returns error: %d", rc);
274 if (!envbid || !envhwkey) {
276 * BoardId/HWkey not available in the environment, so try the
277 * environment variable for BoardId/HWkey list
279 char *bidhwklist = getenv("boardIdListHex");
283 char *rest = bidhwklist;
287 printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
289 printf("boardIdHwKeyList: %s\n",
293 /* loop over each bid/hwkey pair in the list */
294 unsigned long bid = 0;
295 unsigned long hwkey = 0;
297 while (*rest && !isxdigit(*rest))
300 * use simple_strtoul because we need &end and
301 * we know we got non numeric char at the end
303 bid = simple_strtoul(rest, &endp, 16);
304 /* BoardId and HWkey are separated with a "_" */
308 * use simple_strtoul because we need
311 hwkey = simple_strtoul(rest, &endp, 16);
313 while (*rest && !isxdigit(*rest))
316 if ((!bid) || (!hwkey)) {
321 printf("trying bid=0x%lX, hwkey=%ld\n",
325 * Compare the values of the found entry in the
326 * list with the valid values which are stored
327 * in the inventory eeprom. If they are equal
328 * set the values in environment variables.
330 if ((bid == ivmbid) && (hwkey == ivmhwkey)) {
336 sprintf(buf, "%lx", bid);
337 setenv("boardid", buf);
338 sprintf(buf, "%lx", hwkey);
339 setenv("hwkey", buf);
341 } /* end while( ! found ) */
345 /* compare now the values */
346 if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) {
347 printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
350 printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
352 printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
353 rc = 1; /* don't match */
358 U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
359 "check boardid and hwkey",
360 "[v]\n - check environment parameter "\
361 "\"boardIdListHex\" against stored boardid and hwkey "\
362 "from the IVM\n v: verbose output"
366 * command km_checktestboot
367 * if the testpin of the board is asserted, return 1
370 int do_checktestboot(cmd_tbl_t *cmdtp, int flag, int argc,
376 int verbose = argc > 1 && *argv[1] == 'v';
378 #if defined(CONFIG_POST)
379 testpin = post_hotkeys_pressed();
380 s = getenv("test_bank");
382 /* when test_bank is not set, act as if testpin is not asserted */
383 testboot = (testpin != 0) && (s);
385 printf("testpin = %d\n", testpin);
386 printf("test_bank = %s\n", s ? s : "not set");
387 printf("boot test app : %s\n", (testboot) ? "yes" : "no");
389 /* return 0 means: testboot, therefore we need the inversion */
393 U_BOOT_CMD(km_checktestboot, 2, 0, do_checktestboot,
394 "check if testpin is asserted",
395 "[v]\n v - verbose output"