blob: 25897af2d8a4b732ce1067b0dfdfc731bb246e11 [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
Tom Rinidec7ea02024-05-20 13:35:03 -060010#include <config.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>
Aleksandar Gerasimovski197b9482021-12-10 11:07:53 +010022#include <linux/bug.h>
23#include <bootcount.h>
Heiko Schocher5e51b482008-10-15 09:39:08 +020024
Thomas Herzmannb2c31602012-05-04 10:55:58 +020025#if defined(CONFIG_POST)
26#include "post.h"
27#endif
Holger Brunckbf20a882011-06-05 22:22:18 +000028#include "common.h"
Heiko Schocher5e51b482008-10-15 09:39:08 +020029#include <i2c.h>
Heiko Schocher5e51b482008-10-15 09:39:08 +020030
Heiko Schochercfc58042010-04-26 13:07:28 +020031DECLARE_GLOBAL_DATA_PTR;
Heiko Schocherd9e10952011-03-08 10:51:15 +010032
Heiko Schochercfc58042010-04-26 13:07:28 +020033/*
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
44 */
45int set_km_env(void)
46{
Heiko Schochercfc58042010-04-26 13:07:28 +020047 unsigned int pnvramaddr;
48 unsigned int pram;
49 unsigned int varaddr;
Andreas Huber28c63c72011-09-13 23:06:11 +000050 unsigned int kernelmem;
Andreas Huber28c63c72011-09-13 23:06:11 +000051 unsigned long rootfssize = 0;
Aleksandar Gerasimovski41683c42021-06-04 09:25:00 +000052 char envval[16];
53 char *p;
Heiko Schochercfc58042010-04-26 13:07:28 +020054
Tom Rinibb4dd962022-11-16 13:10:37 -050055 pnvramaddr = CFG_SYS_SDRAM_BASE + gd->ram_size -
Aleksandar Gerasimovskif9da5262021-01-19 10:41:00 +000056 CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM - CONFIG_KM_PNVRAM;
Aleksandar Gerasimovski41683c42021-06-04 09:25:00 +000057 sprintf(envval, "0x%x", pnvramaddr);
58 env_set("pnvramaddr", envval);
Heiko Schochercfc58042010-04-26 13:07:28 +020059
Robert P. J. Day8d56db92016-07-15 13:44:45 -040060 /* try to read rootfssize (ram image) from environment */
Simon Glass64b723f2017-08-03 12:22:12 -060061 p = env_get("rootfssize");
Holger Brunckff41a752020-10-30 12:45:49 +010062 if (p)
Andreas Huber28c63c72011-09-13 23:06:11 +000063 strict_strtoul(p, 16, &rootfssize);
64 pram = (rootfssize + CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM +
65 CONFIG_KM_PNVRAM) / 0x400;
Niel Fouriede553f52021-01-21 13:19:19 +010066 env_set_ulong("pram", pram);
Heiko Schochercfc58042010-04-26 13:07:28 +020067
Tom Rinibb4dd962022-11-16 13:10:37 -050068 varaddr = CFG_SYS_SDRAM_BASE + gd->ram_size -
Aleksandar Gerasimovskif9da5262021-01-19 10:41:00 +000069 CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
Niel Fouriede553f52021-01-21 13:19:19 +010070 env_set_hex("varaddr", varaddr);
Aleksandar Gerasimovski41683c42021-06-04 09:25:00 +000071 sprintf(envval, "0x%x", varaddr);
72 env_set("varaddr", envval);
Andreas Huber28c63c72011-09-13 23:06:11 +000073
74 kernelmem = gd->ram_size - 0x400 * pram;
Aleksandar Gerasimovski41683c42021-06-04 09:25:00 +000075 sprintf(envval, "0x%x", kernelmem);
76 env_set("kernelmem", envval);
Andreas Huber28c63c72011-09-13 23:06:11 +000077
Heiko Schochercfc58042010-04-26 13:07:28 +020078 return 0;
79}
80
Simon Glass7fe53042023-02-22 09:33:50 -070081#if IS_ENABLED(CONFIG_PG_WCOM_UBOOT_UPDATE_SUPPORTED)
Simon Glass83d6e1e2023-02-05 15:40:29 -070082#if ((!IS_ENABLED(CONFIG_PG_WCOM_UBOOT_BOOTPACKAGE) && \
Simon Glass1873c722023-02-05 15:40:30 -070083 !IS_ENABLED(CONFIG_PG_WCOM_UBOOT_UPDATE)) || \
Simon Glass83d6e1e2023-02-05 15:40:29 -070084 (IS_ENABLED(CONFIG_PG_WCOM_UBOOT_BOOTPACKAGE) && \
Simon Glass1873c722023-02-05 15:40:30 -070085 IS_ENABLED(CONFIG_PG_WCOM_UBOOT_UPDATE)))
Aleksandar Gerasimovski197b9482021-12-10 11:07:53 +010086#error "It has to be either bootpackage or update u-boot image!"
87#endif
88void check_for_uboot_update(void)
89{
90 void (*uboot_update_entry)(void) =
91 (void (*)(void)) CONFIG_PG_WCOM_UBOOT_UPDATE_TEXT_BASE;
92 char *isupdated = env_get("updateduboot");
93 ulong bootcount = bootcount_load();
94 ulong ebootcount = 0;
95
96 if (IS_ENABLED(CONFIG_PG_WCOM_UBOOT_BOOTPACKAGE)) {
97 /*
98 * When running in factory burned u-boot move to the updated
99 * u-boot version only if updateduboot envvar is set to 'yes'
100 * and bootcount limit is not exceeded.
101 * Board must be able to start in factory bootloader mode!
102 */
103 if (isupdated && !strncmp(isupdated, "yes", 3) &&
104 bootcount <= CONFIG_BOOTCOUNT_BOOTLIMIT) {
105 printf("Check update: update detected, ");
106 printf("starting new image @%08x ...\n",
107 CONFIG_PG_WCOM_UBOOT_UPDATE_TEXT_BASE);
108 ebootcount = early_bootcount_load();
109 if (ebootcount <= CONFIG_BOOTCOUNT_BOOTLIMIT) {
110 early_bootcount_store(++ebootcount);
111 uboot_update_entry();
112 } else {
113 printf("Check update: warning: ");
114 printf("early bootcount exceeded (%lu)\n",
115 ebootcount);
116 }
117 }
118 printf("Check update: starting factory image @%08x ...\n",
Simon Glass72cc5382022-10-20 18:22:39 -0600119 CONFIG_TEXT_BASE);
Aleksandar Gerasimovski197b9482021-12-10 11:07:53 +0100120 } else if (IS_ENABLED(CONFIG_PG_WCOM_UBOOT_UPDATE)) {
121 /*
122 * When running in field updated u-boot, make sure that
123 * bootcount limit is never exceeded. Must never happen!
124 */
125 WARN_ON(bootcount > CONFIG_BOOTCOUNT_BOOTLIMIT);
126 printf("Check update: updated u-boot starting @%08x ...\n",
Simon Glass72cc5382022-10-20 18:22:39 -0600127 CONFIG_TEXT_BASE);
Aleksandar Gerasimovski197b9482021-12-10 11:07:53 +0100128 }
129}
130#endif
131
Heiko Schocherd9e10952011-03-08 10:51:15 +0100132static void i2c_write_start_seq(void)
Heiko Schocher5e51b482008-10-15 09:39:08 +0200133{
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100134 set_sda(1);
135 udelay(DELAY_HALF_PERIOD);
136 set_scl(1);
137 udelay(DELAY_HALF_PERIOD);
138 set_sda(0);
139 udelay(DELAY_HALF_PERIOD);
140 set_scl(0);
141 udelay(DELAY_HALF_PERIOD);
Heiko Schocher5e51b482008-10-15 09:39:08 +0200142}
143
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100144/*
145 * I2C is a synchronous protocol and resets of the processor in the middle
146 * of an access can block the I2C Bus until a powerdown of the full unit is
147 * done. This function toggles the SCL until the SCL and SCA line are
148 * released, but max. 16 times, after this a I2C start-sequence is sent.
149 * This I2C Deblocking mechanism was developed by Keymile in association
150 * with Anatech and Atmel in 1998.
Heiko Schocher5e51b482008-10-15 09:39:08 +0200151 */
Holger Brunckbf20a882011-06-05 22:22:18 +0000152int i2c_make_abort(void)
Heiko Schocher5e51b482008-10-15 09:39:08 +0200153{
154 int scl_state = 0;
155 int sda_state = 0;
156 int i = 0;
157 int ret = 0;
158
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100159 if (!get_sda()) {
Heiko Schocher5e51b482008-10-15 09:39:08 +0200160 ret = -1;
161 while (i < 16) {
162 i++;
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100163 set_scl(0);
164 udelay(DELAY_ABORT_SEQ);
165 set_scl(1);
166 udelay(DELAY_ABORT_SEQ);
167 scl_state = get_scl();
168 sda_state = get_sda();
Heiko Schocher5e51b482008-10-15 09:39:08 +0200169 if (scl_state && sda_state) {
170 ret = 0;
171 break;
172 }
173 }
174 }
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100175 if (ret == 0)
176 for (i = 0; i < 5; i++)
Heiko Schocherd9e10952011-03-08 10:51:15 +0100177 i2c_write_start_seq();
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100178
Heiko Schocherd9e10952011-03-08 10:51:15 +0100179 /* respect stop setup time */
180 udelay(DELAY_ABORT_SEQ);
181 set_scl(1);
182 udelay(DELAY_ABORT_SEQ);
183 set_sda(1);
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100184 get_sda();
Heiko Schocherd9e10952011-03-08 10:51:15 +0100185
Heiko Schocher5e51b482008-10-15 09:39:08 +0200186 return ret;
187}
Heiko Schocher5e51b482008-10-15 09:39:08 +0200188
Valentin Longchamp391cc9b2013-10-18 11:47:18 +0200189#if defined(CONFIG_KM_COMMON_ETH_INIT)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900190int board_eth_init(struct bd_info *bis)
Heiko Schocherd19a6ec2008-11-21 08:29:40 +0100191{
Heiko Schocher8ce3dd52011-03-15 16:52:29 +0100192 if (ethernet_present())
Heiko Schocher466924f2010-02-18 08:08:25 +0100193 return cpu_eth_init(bis);
194
195 return -1;
Heiko Schocherd19a6ec2008-11-21 08:29:40 +0100196}
Valentin Longchamp391cc9b2013-10-18 11:47:18 +0200197#endif
Holger Brunck30664922011-05-04 01:47:30 +0000198
199/*
200 * do_setboardid command
201 * read out the board id and the hw key from the intventory EEPROM and set
202 * this values as environment variables.
203 */
Simon Glassed38aef2020-05-10 11:40:03 -0600204static int do_setboardid(struct cmd_tbl *cmdtp, int flag, int argc,
205 char *const argv[])
Holger Brunck30664922011-05-04 01:47:30 +0000206{
207 unsigned char buf[32];
208 char *p;
209
210 p = get_local_var("IVM_BoardId");
Holger Brunckff41a752020-10-30 12:45:49 +0100211 if (!p) {
Holger Brunck30664922011-05-04 01:47:30 +0000212 printf("can't get the IVM_Boardid\n");
213 return 1;
214 }
Ben Whitten34fd6c92015-12-30 13:05:58 +0000215 strcpy((char *)buf, p);
Simon Glass6a38e412017-08-03 12:22:09 -0600216 env_set("boardid", (char *)buf);
Holger Brunckde799842011-07-04 22:23:59 +0000217 printf("set boardid=%s\n", buf);
Holger Brunck30664922011-05-04 01:47:30 +0000218
219 p = get_local_var("IVM_HWKey");
Holger Brunckff41a752020-10-30 12:45:49 +0100220 if (!p) {
Holger Brunck30664922011-05-04 01:47:30 +0000221 printf("can't get the IVM_HWKey\n");
222 return 1;
223 }
Ben Whitten34fd6c92015-12-30 13:05:58 +0000224 strcpy((char *)buf, p);
Simon Glass6a38e412017-08-03 12:22:09 -0600225 env_set("hwkey", (char *)buf);
Holger Brunckde799842011-07-04 22:23:59 +0000226 printf("set hwkey=%s\n", buf);
227 printf("Execute manually saveenv for persistent storage.\n");
Holger Brunck30664922011-05-04 01:47:30 +0000228
229 return 0;
230}
231
Holger Brunckff41a752020-10-30 12:45:49 +0100232U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid",
233 "read out bid and hwkey from IVM and set in environment");
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000234
235/*
236 * command km_checkbidhwk
237 * if "boardid" and "hwkey" are not already set in the environment, do:
238 * if a "boardIdListHex" exists in the environment:
239 * - read ivm data for boardid and hwkey
240 * - compare each entry of the boardIdListHex with the
241 * IVM data:
242 * if match:
243 * set environment variables boardid, boardId,
244 * hwkey, hwKey to the found values
245 * both (boardid and boardId) are set because
246 * they might be used differently in the
247 * application and in the init scripts (?)
248 * return 0 in case of match, 1 if not match or error
249 */
Simon Glassed38aef2020-05-10 11:40:03 -0600250static int do_checkboardidhwk(struct cmd_tbl *cmdtp, int flag, int argc,
251 char *const argv[])
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000252{
253 unsigned long ivmbid = 0, ivmhwkey = 0;
254 unsigned long envbid = 0, envhwkey = 0;
255 char *p;
256 int verbose = argc > 1 && *argv[1] == 'v';
257 int rc = 0;
258
259 /*
260 * first read out the real inventory values, these values are
261 * already stored in the local hush variables
262 */
263 p = get_local_var("IVM_BoardId");
Holger Brunckff41a752020-10-30 12:45:49 +0100264 if (!p) {
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000265 printf("can't get the IVM_Boardid\n");
266 return 1;
267 }
268 rc = strict_strtoul(p, 16, &ivmbid);
269
270 p = get_local_var("IVM_HWKey");
Holger Brunckff41a752020-10-30 12:45:49 +0100271 if (!p) {
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000272 printf("can't get the IVM_HWKey\n");
273 return 1;
274 }
275 rc = strict_strtoul(p, 16, &ivmhwkey);
276
277 if (!ivmbid || !ivmhwkey) {
278 printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
279 return rc;
280 }
281
282 /* now try to read values from environment if available */
Simon Glass64b723f2017-08-03 12:22:12 -0600283 p = env_get("boardid");
Holger Brunckff41a752020-10-30 12:45:49 +0100284 if (p)
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000285 rc = strict_strtoul(p, 16, &envbid);
Simon Glass64b723f2017-08-03 12:22:12 -0600286 p = env_get("hwkey");
Holger Brunckff41a752020-10-30 12:45:49 +0100287 if (p)
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000288 rc = strict_strtoul(p, 16, &envhwkey);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000289 if (rc != 0) {
290 printf("strict_strtoul returns error: %d", rc);
291 return rc;
292 }
293
294 if (!envbid || !envhwkey) {
295 /*
296 * BoardId/HWkey not available in the environment, so try the
297 * environment variable for BoardId/HWkey list
298 */
Simon Glass64b723f2017-08-03 12:22:12 -0600299 char *bidhwklist = env_get("boardIdListHex");
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000300
301 if (bidhwklist) {
302 int found = 0;
303 char *rest = bidhwklist;
304 char *endp;
305
306 if (verbose) {
307 printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
Holger Brunckff41a752020-10-30 12:45:49 +0100308 ivmbid, ivmhwkey);
309 printf("boardIdHwKeyList: %s\n", bidhwklist);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000310 }
311 while (!found) {
312 /* loop over each bid/hwkey pair in the list */
313 unsigned long bid = 0;
314 unsigned long hwkey = 0;
315
316 while (*rest && !isxdigit(*rest))
317 rest++;
318 /*
319 * use simple_strtoul because we need &end and
320 * we know we got non numeric char at the end
321 */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600322 bid = hextoul(rest, &endp);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000323 /* BoardId and HWkey are separated with a "_" */
324 if (*endp == '_') {
325 rest = endp + 1;
326 /*
327 * use simple_strtoul because we need
328 * &end
329 */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600330 hwkey = hextoul(rest, &endp);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000331 rest = endp;
332 while (*rest && !isxdigit(*rest))
333 rest++;
334 }
Holger Brunckff41a752020-10-30 12:45:49 +0100335 if (!bid || !hwkey) {
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000336 /* end of list */
337 break;
338 }
339 if (verbose) {
340 printf("trying bid=0x%lX, hwkey=%ld\n",
Holger Brunckff41a752020-10-30 12:45:49 +0100341 bid, hwkey);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000342 }
343 /*
344 * Compare the values of the found entry in the
345 * list with the valid values which are stored
346 * in the inventory eeprom. If they are equal
Holger Brunckbc4e15f2011-06-05 22:22:17 +0000347 * set the values in environment variables.
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000348 */
Holger Brunckff41a752020-10-30 12:45:49 +0100349 if (bid == ivmbid && hwkey == ivmhwkey) {
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000350 found = 1;
351 envbid = bid;
352 envhwkey = hwkey;
Niel Fouriede553f52021-01-21 13:19:19 +0100353 env_set_hex("boardid", bid);
354 env_set_hex("hwkey", hwkey);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000355 }
356 } /* end while( ! found ) */
357 }
358 }
359
360 /* compare now the values */
Holger Brunckff41a752020-10-30 12:45:49 +0100361 if (ivmbid == envbid && ivmhwkey == envhwkey) {
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000362 printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
363 rc = 0; /* match */
364 } else {
Holger Brunckde799842011-07-04 22:23:59 +0000365 printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
Holger Brunckff41a752020-10-30 12:45:49 +0100366 envhwkey);
Thomas Herzmann2c8a0c82011-05-12 19:59:22 +0000367 printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
368 rc = 1; /* don't match */
369 }
370 return rc;
371}
372
373U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
Holger Brunckff41a752020-10-30 12:45:49 +0100374 "check boardid and hwkey",
375 "[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 +0000376);
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200377
378/*
379 * command km_checktestboot
380 * if the testpin of the board is asserted, return 1
381 * * else return 0
382 */
Simon Glassed38aef2020-05-10 11:40:03 -0600383static int do_checktestboot(struct cmd_tbl *cmdtp, int flag, int argc,
384 char *const argv[])
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200385{
386 int testpin = 0;
387 char *s = NULL;
388 int testboot = 0;
389 int verbose = argc > 1 && *argv[1] == 'v';
390
391#if defined(CONFIG_POST)
392 testpin = post_hotkeys_pressed();
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200393#endif
Holger Brunckff41a752020-10-30 12:45:49 +0100394
Simon Glass64b723f2017-08-03 12:22:12 -0600395 s = env_get("test_bank");
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200396 /* when test_bank is not set, act as if testpin is not asserted */
397 testboot = (testpin != 0) && (s);
398 if (verbose) {
399 printf("testpin = %d\n", testpin);
Wolfgang Denk6ae80832014-11-06 14:02:57 +0100400 /* cppcheck-suppress nullPointer */
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200401 printf("test_bank = %s\n", s ? s : "not set");
402 printf("boot test app : %s\n", (testboot) ? "yes" : "no");
403 }
404 /* return 0 means: testboot, therefore we need the inversion */
405 return !testboot;
406}
407
408U_BOOT_CMD(km_checktestboot, 2, 0, do_checktestboot,
Holger Brunckff41a752020-10-30 12:45:49 +0100409 "check if testpin is asserted",
410 "[v]\n v - verbose output"
Thomas Herzmannb2c31602012-05-04 10:55:58 +0200411);