blob: ff07260194c554c68378dc0b42d9202ea6cde49a [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>
Simon Glass5e6201b2019-08-01 09:46:51 -060011#include <env.h>
Heiko Schocher5e51b482008-10-15 09:39:08 +020012#include <ioports.h>
Holger Brunck30664922011-05-04 01:47:30 +000013#include <command.h>
Heiko Schocher5e51b482008-10-15 09:39:08 +020014#include <malloc.h>
Simon Glass299f0be2014-04-10 20:01:24 -060015#include <cli_hush.h>
Heiko Schocherd19a6ec2008-11-21 08:29:40 +010016#include <net.h>
Heiko Schocher466924f2010-02-18 08:08:25 +010017#include <netdev.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Heiko Schocherd19a6ec2008-11-21 08:29:40 +010019#include <asm/io.h>
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +000020#include <linux/ctype.h>
Simon Glassdbd79542020-05-10 11:40:11 -060021#include <linux/delay.h>
Heiko Schocher5e51b482008-10-15 09:39:08 +020022
Thomas Herzmannb2c31602012-05-04 10:55:58 +020023#if defined(CONFIG_POST)
24#include "post.h"
25#endif
Holger Brunckbf20a882011-06-05 22:22:18 +000026#include "common.h"
Heiko Schocher5e51b482008-10-15 09:39:08 +020027#include <i2c.h>
Heiko Schocher5e51b482008-10-15 09:39:08 +020028
Heiko Schochercfc58042010-04-26 13:07:28 +020029DECLARE_GLOBAL_DATA_PTR;
Heiko Schocherd9e10952011-03-08 10:51:15 +010030
Heiko Schochercfc58042010-04-26 13:07:28 +020031/*
32 * Set Keymile specific environment variables
33 * Currently only some memory layout variables are calculated here
34 * ... ------------------------------------------------
35 * ... |@rootfsaddr |@pnvramaddr |@varaddr |@reserved |@END_OF_RAM
36 * ... |<------------------- pram ------------------->|
37 * ... ------------------------------------------------
38 * @END_OF_RAM: denotes the RAM size
39 * @pnvramaddr: Startadress of pseudo non volatile RAM in hex
40 * @pram : preserved ram size in k
41 * @varaddr : startadress for /var mounted into RAM
42 */
43int set_km_env(void)
44{
Heiko Schochercfc58042010-04-26 13:07:28 +020045 unsigned int pnvramaddr;
46 unsigned int pram;
47 unsigned int varaddr;
Andreas Huber28c63c72011-09-13 23:06:11 +000048 unsigned int kernelmem;
Andreas Huber28c63c72011-09-13 23:06:11 +000049 unsigned long rootfssize = 0;
Aleksandar Gerasimovski41683c42021-06-04 09:25:00 +000050 char envval[16];
51 char *p;
Heiko Schochercfc58042010-04-26 13:07:28 +020052
Aleksandar Gerasimovskif9da5262021-01-19 10:41:00 +000053 pnvramaddr = CONFIG_SYS_SDRAM_BASE + gd->ram_size -
54 CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM - CONFIG_KM_PNVRAM;
Aleksandar Gerasimovski41683c42021-06-04 09:25:00 +000055 sprintf(envval, "0x%x", pnvramaddr);
56 env_set("pnvramaddr", envval);
Heiko Schochercfc58042010-04-26 13:07:28 +020057
Robert P. J. Day8d56db92016-07-15 13:44:45 -040058 /* try to read rootfssize (ram image) from environment */
Simon Glass64b723f2017-08-03 12:22:12 -060059 p = env_get("rootfssize");
Holger Brunckff41a752020-10-30 12:45:49 +010060 if (p)
Andreas Huber28c63c72011-09-13 23:06:11 +000061 strict_strtoul(p, 16, &rootfssize);
62 pram = (rootfssize + CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM +
63 CONFIG_KM_PNVRAM) / 0x400;
Niel Fouriede553f52021-01-21 13:19:19 +010064 env_set_ulong("pram", pram);
Heiko Schochercfc58042010-04-26 13:07:28 +020065
Aleksandar Gerasimovskif9da5262021-01-19 10:41:00 +000066 varaddr = CONFIG_SYS_SDRAM_BASE + gd->ram_size -
67 CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
Niel Fouriede553f52021-01-21 13:19:19 +010068 env_set_hex("varaddr", varaddr);
Aleksandar Gerasimovski41683c42021-06-04 09:25:00 +000069 sprintf(envval, "0x%x", varaddr);
70 env_set("varaddr", envval);
Andreas Huber28c63c72011-09-13 23:06:11 +000071
72 kernelmem = gd->ram_size - 0x400 * pram;
Aleksandar Gerasimovski41683c42021-06-04 09:25:00 +000073 sprintf(envval, "0x%x", kernelmem);
74 env_set("kernelmem", envval);
Andreas Huber28c63c72011-09-13 23:06:11 +000075
Heiko Schochercfc58042010-04-26 13:07:28 +020076 return 0;
77}
78
Holger Brunck015a60f2011-09-14 10:54:12 +020079#if defined(CONFIG_SYS_I2C_INIT_BOARD)
Heiko Schocherd9e10952011-03-08 10:51:15 +010080static void i2c_write_start_seq(void)
Heiko Schocher5e51b482008-10-15 09:39:08 +020081{
Heiko Schocher8ce3dd52011-03-15 16:52:29 +010082 set_sda(1);
83 udelay(DELAY_HALF_PERIOD);
84 set_scl(1);
85 udelay(DELAY_HALF_PERIOD);
86 set_sda(0);
87 udelay(DELAY_HALF_PERIOD);
88 set_scl(0);
89 udelay(DELAY_HALF_PERIOD);
Heiko Schocher5e51b482008-10-15 09:39:08 +020090}
91
Heiko Schocher8ce3dd52011-03-15 16:52:29 +010092/*
93 * I2C is a synchronous protocol and resets of the processor in the middle
94 * of an access can block the I2C Bus until a powerdown of the full unit is
95 * done. This function toggles the SCL until the SCL and SCA line are
96 * released, but max. 16 times, after this a I2C start-sequence is sent.
97 * This I2C Deblocking mechanism was developed by Keymile in association
98 * with Anatech and Atmel in 1998.
Heiko Schocher5e51b482008-10-15 09:39:08 +020099 */
Holger Brunckbf20a882011-06-05 22:22:18 +0000100int i2c_make_abort(void)
Heiko Schocher5e51b482008-10-15 09:39:08 +0200101{
102 int scl_state = 0;
103 int sda_state = 0;
104 int i = 0;
105 int ret = 0;
106
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100107 if (!get_sda()) {
Heiko Schocher5e51b482008-10-15 09:39:08 +0200108 ret = -1;
109 while (i < 16) {
110 i++;
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100111 set_scl(0);
112 udelay(DELAY_ABORT_SEQ);
113 set_scl(1);
114 udelay(DELAY_ABORT_SEQ);
115 scl_state = get_scl();
116 sda_state = get_sda();
Heiko Schocher5e51b482008-10-15 09:39:08 +0200117 if (scl_state && sda_state) {
118 ret = 0;
119 break;
120 }
121 }
122 }
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100123 if (ret == 0)
124 for (i = 0; i < 5; i++)
Heiko Schocherd9e10952011-03-08 10:51:15 +0100125 i2c_write_start_seq();
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100126
Heiko Schocherd9e10952011-03-08 10:51:15 +0100127 /* respect stop setup time */
128 udelay(DELAY_ABORT_SEQ);
129 set_scl(1);
130 udelay(DELAY_ABORT_SEQ);
131 set_sda(1);
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100132 get_sda();
Heiko Schocherd9e10952011-03-08 10:51:15 +0100133
Heiko Schocher5e51b482008-10-15 09:39:08 +0200134 return ret;
135}
Heiko Schocher5e51b482008-10-15 09:39:08 +0200136
Heiko Schocherd9e10952011-03-08 10:51:15 +0100137/**
138 * i2c_init_board - reset i2c bus. When the board is powercycled during a
139 * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
140 */
141void i2c_init_board(void)
142{
Heiko Schocher5e51b482008-10-15 09:39:08 +0200143 /* Now run the AbortSequence() */
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100144 i2c_make_abort();
Heiko Schocher5e51b482008-10-15 09:39:08 +0200145}
Holger Brunck015a60f2011-09-14 10:54:12 +0200146#endif
147
Valentin Longchamp391cc9b2013-10-18 11:47:18 +0200148#if defined(CONFIG_KM_COMMON_ETH_INIT)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900149int board_eth_init(struct bd_info *bis)
Heiko Schocherd19a6ec2008-11-21 08:29:40 +0100150{
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100151 if (ethernet_present())
Heiko Schocher466924f2010-02-18 08:08:25 +0100152 return cpu_eth_init(bis);
153
154 return -1;
Heiko Schocherd19a6ec2008-11-21 08:29:40 +0100155}
Valentin Longchamp391cc9b2013-10-18 11:47:18 +0200156#endif
Holger Brunck30664922011-05-04 01:47:30 +0000157
158/*
159 * do_setboardid command
160 * read out the board id and the hw key from the intventory EEPROM and set
161 * this values as environment variables.
162 */
Simon Glassed38aef2020-05-10 11:40:03 -0600163static int do_setboardid(struct cmd_tbl *cmdtp, int flag, int argc,
164 char *const argv[])
Holger Brunck30664922011-05-04 01:47:30 +0000165{
166 unsigned char buf[32];
167 char *p;
168
169 p = get_local_var("IVM_BoardId");
Holger Brunckff41a752020-10-30 12:45:49 +0100170 if (!p) {
Holger Brunck30664922011-05-04 01:47:30 +0000171 printf("can't get the IVM_Boardid\n");
172 return 1;
173 }
Ben Whitten34fd6c92015-12-30 13:05:58 +0000174 strcpy((char *)buf, p);
Simon Glass6a38e412017-08-03 12:22:09 -0600175 env_set("boardid", (char *)buf);
Holger Brunckde799842011-07-04 22:23:59 +0000176 printf("set boardid=%s\n", buf);
Holger Brunck30664922011-05-04 01:47:30 +0000177
178 p = get_local_var("IVM_HWKey");
Holger Brunckff41a752020-10-30 12:45:49 +0100179 if (!p) {
Holger Brunck30664922011-05-04 01:47:30 +0000180 printf("can't get the IVM_HWKey\n");
181 return 1;
182 }
Ben Whitten34fd6c92015-12-30 13:05:58 +0000183 strcpy((char *)buf, p);
Simon Glass6a38e412017-08-03 12:22:09 -0600184 env_set("hwkey", (char *)buf);
Holger Brunckde799842011-07-04 22:23:59 +0000185 printf("set hwkey=%s\n", buf);
186 printf("Execute manually saveenv for persistent storage.\n");
Holger Brunck30664922011-05-04 01:47:30 +0000187
188 return 0;
189}
190
Holger Brunckff41a752020-10-30 12:45:49 +0100191U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid",
192 "read out bid and hwkey from IVM and set in environment");
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000193
194/*
195 * command km_checkbidhwk
196 * if "boardid" and "hwkey" are not already set in the environment, do:
197 * if a "boardIdListHex" exists in the environment:
198 * - read ivm data for boardid and hwkey
199 * - compare each entry of the boardIdListHex with the
200 * IVM data:
201 * if match:
202 * set environment variables boardid, boardId,
203 * hwkey, hwKey to the found values
204 * both (boardid and boardId) are set because
205 * they might be used differently in the
206 * application and in the init scripts (?)
207 * return 0 in case of match, 1 if not match or error
208 */
Simon Glassed38aef2020-05-10 11:40:03 -0600209static int do_checkboardidhwk(struct cmd_tbl *cmdtp, int flag, int argc,
210 char *const argv[])
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000211{
212 unsigned long ivmbid = 0, ivmhwkey = 0;
213 unsigned long envbid = 0, envhwkey = 0;
214 char *p;
215 int verbose = argc > 1 && *argv[1] == 'v';
216 int rc = 0;
217
218 /*
219 * first read out the real inventory values, these values are
220 * already stored in the local hush variables
221 */
222 p = get_local_var("IVM_BoardId");
Holger Brunckff41a752020-10-30 12:45:49 +0100223 if (!p) {
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000224 printf("can't get the IVM_Boardid\n");
225 return 1;
226 }
227 rc = strict_strtoul(p, 16, &ivmbid);
228
229 p = get_local_var("IVM_HWKey");
Holger Brunckff41a752020-10-30 12:45:49 +0100230 if (!p) {
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000231 printf("can't get the IVM_HWKey\n");
232 return 1;
233 }
234 rc = strict_strtoul(p, 16, &ivmhwkey);
235
236 if (!ivmbid || !ivmhwkey) {
237 printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
238 return rc;
239 }
240
241 /* now try to read values from environment if available */
Simon Glass64b723f2017-08-03 12:22:12 -0600242 p = env_get("boardid");
Holger Brunckff41a752020-10-30 12:45:49 +0100243 if (p)
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000244 rc = strict_strtoul(p, 16, &envbid);
Simon Glass64b723f2017-08-03 12:22:12 -0600245 p = env_get("hwkey");
Holger Brunckff41a752020-10-30 12:45:49 +0100246 if (p)
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000247 rc = strict_strtoul(p, 16, &envhwkey);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000248 if (rc != 0) {
249 printf("strict_strtoul returns error: %d", rc);
250 return rc;
251 }
252
253 if (!envbid || !envhwkey) {
254 /*
255 * BoardId/HWkey not available in the environment, so try the
256 * environment variable for BoardId/HWkey list
257 */
Simon Glass64b723f2017-08-03 12:22:12 -0600258 char *bidhwklist = env_get("boardIdListHex");
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000259
260 if (bidhwklist) {
261 int found = 0;
262 char *rest = bidhwklist;
263 char *endp;
264
265 if (verbose) {
266 printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
Holger Brunckff41a752020-10-30 12:45:49 +0100267 ivmbid, ivmhwkey);
268 printf("boardIdHwKeyList: %s\n", bidhwklist);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000269 }
270 while (!found) {
271 /* loop over each bid/hwkey pair in the list */
272 unsigned long bid = 0;
273 unsigned long hwkey = 0;
274
275 while (*rest && !isxdigit(*rest))
276 rest++;
277 /*
278 * use simple_strtoul because we need &end and
279 * we know we got non numeric char at the end
280 */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600281 bid = hextoul(rest, &endp);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000282 /* BoardId and HWkey are separated with a "_" */
283 if (*endp == '_') {
284 rest = endp + 1;
285 /*
286 * use simple_strtoul because we need
287 * &end
288 */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600289 hwkey = hextoul(rest, &endp);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000290 rest = endp;
291 while (*rest && !isxdigit(*rest))
292 rest++;
293 }
Holger Brunckff41a752020-10-30 12:45:49 +0100294 if (!bid || !hwkey) {
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000295 /* end of list */
296 break;
297 }
298 if (verbose) {
299 printf("trying bid=0x%lX, hwkey=%ld\n",
Holger Brunckff41a752020-10-30 12:45:49 +0100300 bid, hwkey);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000301 }
302 /*
303 * Compare the values of the found entry in the
304 * list with the valid values which are stored
305 * in the inventory eeprom. If they are equal
Holger Brunckbc4e15f2011-06-05 22:22:17 +0000306 * set the values in environment variables.
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000307 */
Holger Brunckff41a752020-10-30 12:45:49 +0100308 if (bid == ivmbid && hwkey == ivmhwkey) {
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000309 found = 1;
310 envbid = bid;
311 envhwkey = hwkey;
Niel Fouriede553f52021-01-21 13:19:19 +0100312 env_set_hex("boardid", bid);
313 env_set_hex("hwkey", hwkey);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000314 }
315 } /* end while( ! found ) */
316 }
317 }
318
319 /* compare now the values */
Holger Brunckff41a752020-10-30 12:45:49 +0100320 if (ivmbid == envbid && ivmhwkey == envhwkey) {
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000321 printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
322 rc = 0; /* match */
323 } else {
Holger Brunckde799842011-07-04 22:23:59 +0000324 printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
Holger Brunckff41a752020-10-30 12:45:49 +0100325 envhwkey);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000326 printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
327 rc = 1; /* don't match */
328 }
329 return rc;
330}
331
332U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
Holger Brunckff41a752020-10-30 12:45:49 +0100333 "check boardid and hwkey",
334 "[v]\n - check environment parameter \"boardIdListHex\" against stored boardid and hwkey from the IVM\n v: verbose output"
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000335);
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200336
337/*
338 * command km_checktestboot
339 * if the testpin of the board is asserted, return 1
340 * * else return 0
341 */
Simon Glassed38aef2020-05-10 11:40:03 -0600342static int do_checktestboot(struct cmd_tbl *cmdtp, int flag, int argc,
343 char *const argv[])
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200344{
345 int testpin = 0;
346 char *s = NULL;
347 int testboot = 0;
348 int verbose = argc > 1 && *argv[1] == 'v';
349
350#if defined(CONFIG_POST)
351 testpin = post_hotkeys_pressed();
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200352#endif
Holger Brunckff41a752020-10-30 12:45:49 +0100353
Simon Glass64b723f2017-08-03 12:22:12 -0600354 s = env_get("test_bank");
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200355 /* when test_bank is not set, act as if testpin is not asserted */
356 testboot = (testpin != 0) && (s);
357 if (verbose) {
358 printf("testpin = %d\n", testpin);
Wolfgang Denk6ae80832014-11-06 14:02:57 +0100359 /* cppcheck-suppress nullPointer */
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200360 printf("test_bank = %s\n", s ? s : "not set");
361 printf("boot test app : %s\n", (testboot) ? "yes" : "no");
362 }
363 /* return 0 means: testboot, therefore we need the inversion */
364 return !testboot;
365}
366
367U_BOOT_CMD(km_checktestboot, 2, 0, do_checktestboot,
Holger Brunckff41a752020-10-30 12:45:49 +0100368 "check if testpin is asserted",
369 "[v]\n v - verbose output"
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200370);