blob: 92d4a6ff614956fd5b649813cb4c739593efa8a3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher5e51b482008-10-15 09:39:08 +02002/*
3 * (C) Copyright 2008
4 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
5 *
Holger Brunckbf20a882011-06-05 22:22:18 +00006 * (C) Copyright 2011
7 * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com
Heiko Schocher5e51b482008-10-15 09:39:08 +02008 */
9
10#include <common.h>
Heiko Schocher5e51b482008-10-15 09:39:08 +020011#include <ioports.h>
Holger Brunck30664922011-05-04 01:47:30 +000012#include <command.h>
Heiko Schocher5e51b482008-10-15 09:39:08 +020013#include <malloc.h>
Simon Glass299f0be2014-04-10 20:01:24 -060014#include <cli_hush.h>
Heiko Schocherd19a6ec2008-11-21 08:29:40 +010015#include <net.h>
Heiko Schocher466924f2010-02-18 08:08:25 +010016#include <netdev.h>
Heiko Schocherd19a6ec2008-11-21 08:29:40 +010017#include <asm/io.h>
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +000018#include <linux/ctype.h>
Heiko Schocher5e51b482008-10-15 09:39:08 +020019
Thomas Herzmannb2c31602012-05-04 10:55:58 +020020#if defined(CONFIG_POST)
21#include "post.h"
22#endif
Holger Brunckbf20a882011-06-05 22:22:18 +000023#include "common.h"
Heiko Schocher5e51b482008-10-15 09:39:08 +020024#include <i2c.h>
Heiko Schocher5e51b482008-10-15 09:39:08 +020025
Heiko Schochercfc58042010-04-26 13:07:28 +020026DECLARE_GLOBAL_DATA_PTR;
Heiko Schocherd9e10952011-03-08 10:51:15 +010027
Heiko Schochercfc58042010-04-26 13:07:28 +020028/*
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
39 */
40int set_km_env(void)
41{
42 uchar buf[32];
43 unsigned int pnvramaddr;
44 unsigned int pram;
45 unsigned int varaddr;
Andreas Huber28c63c72011-09-13 23:06:11 +000046 unsigned int kernelmem;
47 char *p;
48 unsigned long rootfssize = 0;
Heiko Schochercfc58042010-04-26 13:07:28 +020049
50 pnvramaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM
51 - CONFIG_KM_PNVRAM;
52 sprintf((char *)buf, "0x%x", pnvramaddr);
Simon Glass6a38e412017-08-03 12:22:09 -060053 env_set("pnvramaddr", (char *)buf);
Heiko Schochercfc58042010-04-26 13:07:28 +020054
Robert P. J. Day8d56db92016-07-15 13:44:45 -040055 /* try to read rootfssize (ram image) from environment */
Simon Glass64b723f2017-08-03 12:22:12 -060056 p = env_get("rootfssize");
Andreas Huber28c63c72011-09-13 23:06:11 +000057 if (p != NULL)
58 strict_strtoul(p, 16, &rootfssize);
59 pram = (rootfssize + CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM +
60 CONFIG_KM_PNVRAM) / 0x400;
Heiko Schochercfc58042010-04-26 13:07:28 +020061 sprintf((char *)buf, "0x%x", pram);
Simon Glass6a38e412017-08-03 12:22:09 -060062 env_set("pram", (char *)buf);
Heiko Schochercfc58042010-04-26 13:07:28 +020063
64 varaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
65 sprintf((char *)buf, "0x%x", varaddr);
Simon Glass6a38e412017-08-03 12:22:09 -060066 env_set("varaddr", (char *)buf);
Andreas Huber28c63c72011-09-13 23:06:11 +000067
68 kernelmem = gd->ram_size - 0x400 * pram;
69 sprintf((char *)buf, "0x%x", kernelmem);
Simon Glass6a38e412017-08-03 12:22:09 -060070 env_set("kernelmem", (char *)buf);
Andreas Huber28c63c72011-09-13 23:06:11 +000071
Heiko Schochercfc58042010-04-26 13:07:28 +020072 return 0;
73}
74
Holger Brunck015a60f2011-09-14 10:54:12 +020075#if defined(CONFIG_SYS_I2C_INIT_BOARD)
Heiko Schocherd9e10952011-03-08 10:51:15 +010076static void i2c_write_start_seq(void)
Heiko Schocher5e51b482008-10-15 09:39:08 +020077{
Heiko Schocher8ce3dd52011-03-15 16:52:29 +010078 set_sda(1);
79 udelay(DELAY_HALF_PERIOD);
80 set_scl(1);
81 udelay(DELAY_HALF_PERIOD);
82 set_sda(0);
83 udelay(DELAY_HALF_PERIOD);
84 set_scl(0);
85 udelay(DELAY_HALF_PERIOD);
Heiko Schocher5e51b482008-10-15 09:39:08 +020086}
87
Heiko Schocher8ce3dd52011-03-15 16:52:29 +010088/*
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.
Heiko Schocher5e51b482008-10-15 09:39:08 +020095 */
Holger Brunckbf20a882011-06-05 22:22:18 +000096int i2c_make_abort(void)
Heiko Schocher5e51b482008-10-15 09:39:08 +020097{
98 int scl_state = 0;
99 int sda_state = 0;
100 int i = 0;
101 int ret = 0;
102
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100103 if (!get_sda()) {
Heiko Schocher5e51b482008-10-15 09:39:08 +0200104 ret = -1;
105 while (i < 16) {
106 i++;
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100107 set_scl(0);
108 udelay(DELAY_ABORT_SEQ);
109 set_scl(1);
110 udelay(DELAY_ABORT_SEQ);
111 scl_state = get_scl();
112 sda_state = get_sda();
Heiko Schocher5e51b482008-10-15 09:39:08 +0200113 if (scl_state && sda_state) {
114 ret = 0;
115 break;
116 }
117 }
118 }
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100119 if (ret == 0)
120 for (i = 0; i < 5; i++)
Heiko Schocherd9e10952011-03-08 10:51:15 +0100121 i2c_write_start_seq();
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100122
Heiko Schocherd9e10952011-03-08 10:51:15 +0100123 /* respect stop setup time */
124 udelay(DELAY_ABORT_SEQ);
125 set_scl(1);
126 udelay(DELAY_ABORT_SEQ);
127 set_sda(1);
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100128 get_sda();
Heiko Schocherd9e10952011-03-08 10:51:15 +0100129
Heiko Schocher5e51b482008-10-15 09:39:08 +0200130 return ret;
131}
Heiko Schocher5e51b482008-10-15 09:39:08 +0200132
Heiko Schocherd9e10952011-03-08 10:51:15 +0100133/**
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.
136 */
137void i2c_init_board(void)
138{
Heiko Schocher5e51b482008-10-15 09:39:08 +0200139 /* Now run the AbortSequence() */
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100140 i2c_make_abort();
Heiko Schocher5e51b482008-10-15 09:39:08 +0200141}
Holger Brunck015a60f2011-09-14 10:54:12 +0200142#endif
143
Valentin Longchamp391cc9b2013-10-18 11:47:18 +0200144#if defined(CONFIG_KM_COMMON_ETH_INIT)
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100145int board_eth_init(bd_t *bis)
Heiko Schocherd19a6ec2008-11-21 08:29:40 +0100146{
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100147 if (ethernet_present())
Heiko Schocher466924f2010-02-18 08:08:25 +0100148 return cpu_eth_init(bis);
149
150 return -1;
Heiko Schocherd19a6ec2008-11-21 08:29:40 +0100151}
Valentin Longchamp391cc9b2013-10-18 11:47:18 +0200152#endif
Holger Brunck30664922011-05-04 01:47:30 +0000153
154/*
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.
158 */
159static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc,
160 char *const argv[])
161{
162 unsigned char buf[32];
163 char *p;
164
165 p = get_local_var("IVM_BoardId");
166 if (p == NULL) {
167 printf("can't get the IVM_Boardid\n");
168 return 1;
169 }
Ben Whitten34fd6c92015-12-30 13:05:58 +0000170 strcpy((char *)buf, p);
Simon Glass6a38e412017-08-03 12:22:09 -0600171 env_set("boardid", (char *)buf);
Holger Brunckde799842011-07-04 22:23:59 +0000172 printf("set boardid=%s\n", buf);
Holger Brunck30664922011-05-04 01:47:30 +0000173
174 p = get_local_var("IVM_HWKey");
175 if (p == NULL) {
176 printf("can't get the IVM_HWKey\n");
177 return 1;
178 }
Ben Whitten34fd6c92015-12-30 13:05:58 +0000179 strcpy((char *)buf, p);
Simon Glass6a38e412017-08-03 12:22:09 -0600180 env_set("hwkey", (char *)buf);
Holger Brunckde799842011-07-04 22:23:59 +0000181 printf("set hwkey=%s\n", buf);
182 printf("Execute manually saveenv for persistent storage.\n");
Holger Brunck30664922011-05-04 01:47:30 +0000183
184 return 0;
185}
186
187U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
188 "hwkey from IVM and set in environment");
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000189
190/*
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
196 * IVM data:
197 * if match:
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
204 */
Holger Brunck828411f2013-05-06 15:02:40 +0200205static int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000206 char *const argv[])
207{
208 unsigned long ivmbid = 0, ivmhwkey = 0;
209 unsigned long envbid = 0, envhwkey = 0;
210 char *p;
211 int verbose = argc > 1 && *argv[1] == 'v';
212 int rc = 0;
213
214 /*
215 * first read out the real inventory values, these values are
216 * already stored in the local hush variables
217 */
218 p = get_local_var("IVM_BoardId");
219 if (p == NULL) {
220 printf("can't get the IVM_Boardid\n");
221 return 1;
222 }
223 rc = strict_strtoul(p, 16, &ivmbid);
224
225 p = get_local_var("IVM_HWKey");
226 if (p == NULL) {
227 printf("can't get the IVM_HWKey\n");
228 return 1;
229 }
230 rc = strict_strtoul(p, 16, &ivmhwkey);
231
232 if (!ivmbid || !ivmhwkey) {
233 printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
234 return rc;
235 }
236
237 /* now try to read values from environment if available */
Simon Glass64b723f2017-08-03 12:22:12 -0600238 p = env_get("boardid");
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000239 if (p != NULL)
240 rc = strict_strtoul(p, 16, &envbid);
Simon Glass64b723f2017-08-03 12:22:12 -0600241 p = env_get("hwkey");
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000242 if (p != NULL)
243 rc = strict_strtoul(p, 16, &envhwkey);
244
245 if (rc != 0) {
246 printf("strict_strtoul returns error: %d", rc);
247 return rc;
248 }
249
250 if (!envbid || !envhwkey) {
251 /*
252 * BoardId/HWkey not available in the environment, so try the
253 * environment variable for BoardId/HWkey list
254 */
Simon Glass64b723f2017-08-03 12:22:12 -0600255 char *bidhwklist = env_get("boardIdListHex");
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000256
257 if (bidhwklist) {
258 int found = 0;
259 char *rest = bidhwklist;
260 char *endp;
261
262 if (verbose) {
263 printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
264 ivmbid, ivmhwkey);
265 printf("boardIdHwKeyList: %s\n",
266 bidhwklist);
267 }
268 while (!found) {
269 /* loop over each bid/hwkey pair in the list */
270 unsigned long bid = 0;
271 unsigned long hwkey = 0;
272
273 while (*rest && !isxdigit(*rest))
274 rest++;
275 /*
276 * use simple_strtoul because we need &end and
277 * we know we got non numeric char at the end
278 */
279 bid = simple_strtoul(rest, &endp, 16);
280 /* BoardId and HWkey are separated with a "_" */
281 if (*endp == '_') {
282 rest = endp + 1;
283 /*
284 * use simple_strtoul because we need
285 * &end
286 */
287 hwkey = simple_strtoul(rest, &endp, 16);
288 rest = endp;
289 while (*rest && !isxdigit(*rest))
290 rest++;
291 }
292 if ((!bid) || (!hwkey)) {
293 /* end of list */
294 break;
295 }
296 if (verbose) {
297 printf("trying bid=0x%lX, hwkey=%ld\n",
298 bid, hwkey);
299 }
300 /*
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
Holger Brunckbc4e15f2011-06-05 22:22:17 +0000304 * set the values in environment variables.
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000305 */
306 if ((bid == ivmbid) && (hwkey == ivmhwkey)) {
307 char buf[10];
308
309 found = 1;
310 envbid = bid;
311 envhwkey = hwkey;
312 sprintf(buf, "%lx", bid);
Simon Glass6a38e412017-08-03 12:22:09 -0600313 env_set("boardid", buf);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000314 sprintf(buf, "%lx", hwkey);
Simon Glass6a38e412017-08-03 12:22:09 -0600315 env_set("hwkey", buf);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000316 }
317 } /* end while( ! found ) */
318 }
319 }
320
321 /* compare now the values */
322 if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) {
323 printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
324 rc = 0; /* match */
325 } else {
Holger Brunckde799842011-07-04 22:23:59 +0000326 printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
327 envhwkey);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000328 printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
329 rc = 1; /* don't match */
330 }
331 return rc;
332}
333
334U_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"
339);
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200340
341/*
342 * command km_checktestboot
343 * if the testpin of the board is asserted, return 1
344 * * else return 0
345 */
Holger Brunck828411f2013-05-06 15:02:40 +0200346static int do_checktestboot(cmd_tbl_t *cmdtp, int flag, int argc,
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200347 char *const argv[])
348{
349 int testpin = 0;
350 char *s = NULL;
351 int testboot = 0;
352 int verbose = argc > 1 && *argv[1] == 'v';
353
354#if defined(CONFIG_POST)
355 testpin = post_hotkeys_pressed();
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200356#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600357 s = env_get("test_bank");
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200358 /* when test_bank is not set, act as if testpin is not asserted */
359 testboot = (testpin != 0) && (s);
360 if (verbose) {
361 printf("testpin = %d\n", testpin);
Wolfgang Denk6ae80832014-11-06 14:02:57 +0100362 /* cppcheck-suppress nullPointer */
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200363 printf("test_bank = %s\n", s ? s : "not set");
364 printf("boot test app : %s\n", (testboot) ? "yes" : "no");
365 }
366 /* return 0 means: testboot, therefore we need the inversion */
367 return !testboot;
368}
369
370U_BOOT_CMD(km_checktestboot, 2, 0, do_checktestboot,
371 "check if testpin is asserted",
372 "[v]\n v - verbose output"
373);