blob: 3999f487192f49f839dee6220752bb338e9e677e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocherc2485362008-10-15 09:39:08 +02002/*
3 * (C) Copyright 2008
4 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
5 *
Holger Brunck4f745bf2011-06-05 22:22:18 +00006 * (C) Copyright 2011
7 * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com
Heiko Schocherc2485362008-10-15 09:39:08 +02008 */
9
10#include <common.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060011#include <env.h>
Heiko Schocherc2485362008-10-15 09:39:08 +020012#include <ioports.h>
Holger Bruncka9417ce2011-05-04 01:47:30 +000013#include <command.h>
Heiko Schocherc2485362008-10-15 09:39:08 +020014#include <malloc.h>
Simon Glasseca86fa2014-04-10 20:01:24 -060015#include <cli_hush.h>
Heiko Schocher210c8c02008-11-21 08:29:40 +010016#include <net.h>
Heiko Schocher62ddcf02010-02-18 08:08:25 +010017#include <netdev.h>
Simon Glass401d1c42020-10-30 21:38:53 -060018#include <asm/global_data.h>
Heiko Schocher210c8c02008-11-21 08:29:40 +010019#include <asm/io.h>
Thomas Herzmann92c91082011-05-12 19:59:22 +000020#include <linux/ctype.h>
Simon Glassc05ed002020-05-10 11:40:11 -060021#include <linux/delay.h>
Aleksandar Gerasimovskiefe19292021-12-10 11:07:53 +010022#include <linux/bug.h>
23#include <bootcount.h>
Heiko Schocherc2485362008-10-15 09:39:08 +020024
Thomas Herzmannc1b3d842012-05-04 10:55:58 +020025#if defined(CONFIG_POST)
26#include "post.h"
27#endif
Holger Brunck4f745bf2011-06-05 22:22:18 +000028#include "common.h"
Heiko Schocherc2485362008-10-15 09:39:08 +020029#include <i2c.h>
Heiko Schocherc2485362008-10-15 09:39:08 +020030
Heiko Schocherf1fef1d2010-04-26 13:07:28 +020031DECLARE_GLOBAL_DATA_PTR;
Heiko Schocher6c11aea2011-03-08 10:51:15 +010032
Heiko Schocherf1fef1d2010-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 Schocherf1fef1d2010-04-26 13:07:28 +020047 unsigned int pnvramaddr;
48 unsigned int pram;
49 unsigned int varaddr;
Andreas Huber2a7714c2011-09-13 23:06:11 +000050 unsigned int kernelmem;
Andreas Huber2a7714c2011-09-13 23:06:11 +000051 unsigned long rootfssize = 0;
Aleksandar Gerasimovski07536032021-06-04 09:25:00 +000052 char envval[16];
53 char *p;
Heiko Schocherf1fef1d2010-04-26 13:07:28 +020054
Aleksandar Gerasimovskie081d942021-01-19 10:41:00 +000055 pnvramaddr = CONFIG_SYS_SDRAM_BASE + gd->ram_size -
56 CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM - CONFIG_KM_PNVRAM;
Aleksandar Gerasimovski07536032021-06-04 09:25:00 +000057 sprintf(envval, "0x%x", pnvramaddr);
58 env_set("pnvramaddr", envval);
Heiko Schocherf1fef1d2010-04-26 13:07:28 +020059
Robert P. J. Day62a3b7d2016-07-15 13:44:45 -040060 /* try to read rootfssize (ram image) from environment */
Simon Glass00caae62017-08-03 12:22:12 -060061 p = env_get("rootfssize");
Holger Brunck7292a182020-10-30 12:45:49 +010062 if (p)
Andreas Huber2a7714c2011-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 Fouriedf86d322021-01-21 13:19:19 +010066 env_set_ulong("pram", pram);
Heiko Schocherf1fef1d2010-04-26 13:07:28 +020067
Aleksandar Gerasimovskie081d942021-01-19 10:41:00 +000068 varaddr = CONFIG_SYS_SDRAM_BASE + gd->ram_size -
69 CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
Niel Fouriedf86d322021-01-21 13:19:19 +010070 env_set_hex("varaddr", varaddr);
Aleksandar Gerasimovski07536032021-06-04 09:25:00 +000071 sprintf(envval, "0x%x", varaddr);
72 env_set("varaddr", envval);
Andreas Huber2a7714c2011-09-13 23:06:11 +000073
74 kernelmem = gd->ram_size - 0x400 * pram;
Aleksandar Gerasimovski07536032021-06-04 09:25:00 +000075 sprintf(envval, "0x%x", kernelmem);
76 env_set("kernelmem", envval);
Andreas Huber2a7714c2011-09-13 23:06:11 +000077
Heiko Schocherf1fef1d2010-04-26 13:07:28 +020078 return 0;
79}
80
Aleksandar Gerasimovskiefe19292021-12-10 11:07:53 +010081#if CONFIG_IS_ENABLED(PG_WCOM_UBOOT_UPDATE_SUPPORTED)
82#if ((!CONFIG_IS_ENABLED(PG_WCOM_UBOOT_BOOTPACKAGE) && \
83 !CONFIG_IS_ENABLED(PG_WCOM_UBOOT_UPDATE)) || \
84 (CONFIG_IS_ENABLED(PG_WCOM_UBOOT_BOOTPACKAGE) && \
85 CONFIG_IS_ENABLED(PG_WCOM_UBOOT_UPDATE)))
86#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",
119 CONFIG_SYS_TEXT_BASE);
120 } 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",
127 CONFIG_SYS_TEXT_BASE);
128 }
129}
130#endif
131
Holger Bruncke792aff2011-09-14 10:54:12 +0200132#if defined(CONFIG_SYS_I2C_INIT_BOARD)
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100133static void i2c_write_start_seq(void)
Heiko Schocherc2485362008-10-15 09:39:08 +0200134{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100135 set_sda(1);
136 udelay(DELAY_HALF_PERIOD);
137 set_scl(1);
138 udelay(DELAY_HALF_PERIOD);
139 set_sda(0);
140 udelay(DELAY_HALF_PERIOD);
141 set_scl(0);
142 udelay(DELAY_HALF_PERIOD);
Heiko Schocherc2485362008-10-15 09:39:08 +0200143}
144
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100145/*
146 * I2C is a synchronous protocol and resets of the processor in the middle
147 * of an access can block the I2C Bus until a powerdown of the full unit is
148 * done. This function toggles the SCL until the SCL and SCA line are
149 * released, but max. 16 times, after this a I2C start-sequence is sent.
150 * This I2C Deblocking mechanism was developed by Keymile in association
151 * with Anatech and Atmel in 1998.
Heiko Schocherc2485362008-10-15 09:39:08 +0200152 */
Holger Brunck4f745bf2011-06-05 22:22:18 +0000153int i2c_make_abort(void)
Heiko Schocherc2485362008-10-15 09:39:08 +0200154{
155 int scl_state = 0;
156 int sda_state = 0;
157 int i = 0;
158 int ret = 0;
159
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100160 if (!get_sda()) {
Heiko Schocherc2485362008-10-15 09:39:08 +0200161 ret = -1;
162 while (i < 16) {
163 i++;
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100164 set_scl(0);
165 udelay(DELAY_ABORT_SEQ);
166 set_scl(1);
167 udelay(DELAY_ABORT_SEQ);
168 scl_state = get_scl();
169 sda_state = get_sda();
Heiko Schocherc2485362008-10-15 09:39:08 +0200170 if (scl_state && sda_state) {
171 ret = 0;
172 break;
173 }
174 }
175 }
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100176 if (ret == 0)
177 for (i = 0; i < 5; i++)
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100178 i2c_write_start_seq();
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100179
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100180 /* respect stop setup time */
181 udelay(DELAY_ABORT_SEQ);
182 set_scl(1);
183 udelay(DELAY_ABORT_SEQ);
184 set_sda(1);
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100185 get_sda();
Heiko Schocherc2485362008-10-15 09:39:08 +0200186
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100187 return ret;
188}
Heiko Schocher6c11aea2011-03-08 10:51:15 +0100189
190/**
191 * i2c_init_board - reset i2c bus. When the board is powercycled during a
192 * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
193 */
194void i2c_init_board(void)
195{
196 /* Now run the AbortSequence() */
197 i2c_make_abort();
Heiko Schocherc2485362008-10-15 09:39:08 +0200198}
Holger Bruncke792aff2011-09-14 10:54:12 +0200199#endif
200
Valentin Longchamp99f62492013-10-18 11:47:18 +0200201#if defined(CONFIG_KM_COMMON_ETH_INIT)
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900202int board_eth_init(struct bd_info *bis)
Heiko Schocher210c8c02008-11-21 08:29:40 +0100203{
Heiko Schocherb11f53f2011-03-15 16:52:29 +0100204 if (ethernet_present())
Heiko Schocher62ddcf02010-02-18 08:08:25 +0100205 return cpu_eth_init(bis);
206
207 return -1;
Heiko Schocher210c8c02008-11-21 08:29:40 +0100208}
Valentin Longchamp99f62492013-10-18 11:47:18 +0200209#endif
Holger Bruncka9417ce2011-05-04 01:47:30 +0000210
211/*
212 * do_setboardid command
213 * read out the board id and the hw key from the intventory EEPROM and set
214 * this values as environment variables.
215 */
Simon Glass09140112020-05-10 11:40:03 -0600216static int do_setboardid(struct cmd_tbl *cmdtp, int flag, int argc,
217 char *const argv[])
Holger Bruncka9417ce2011-05-04 01:47:30 +0000218{
219 unsigned char buf[32];
220 char *p;
221
222 p = get_local_var("IVM_BoardId");
Holger Brunck7292a182020-10-30 12:45:49 +0100223 if (!p) {
Holger Bruncka9417ce2011-05-04 01:47:30 +0000224 printf("can't get the IVM_Boardid\n");
225 return 1;
226 }
Ben Whitten192bc692015-12-30 13:05:58 +0000227 strcpy((char *)buf, p);
Simon Glass382bee52017-08-03 12:22:09 -0600228 env_set("boardid", (char *)buf);
Holger Brunck9485e772011-07-04 22:23:59 +0000229 printf("set boardid=%s\n", buf);
Holger Bruncka9417ce2011-05-04 01:47:30 +0000230
231 p = get_local_var("IVM_HWKey");
Holger Brunck7292a182020-10-30 12:45:49 +0100232 if (!p) {
Holger Bruncka9417ce2011-05-04 01:47:30 +0000233 printf("can't get the IVM_HWKey\n");
234 return 1;
235 }
Ben Whitten192bc692015-12-30 13:05:58 +0000236 strcpy((char *)buf, p);
Simon Glass382bee52017-08-03 12:22:09 -0600237 env_set("hwkey", (char *)buf);
Holger Brunck9485e772011-07-04 22:23:59 +0000238 printf("set hwkey=%s\n", buf);
239 printf("Execute manually saveenv for persistent storage.\n");
Holger Bruncka9417ce2011-05-04 01:47:30 +0000240
241 return 0;
242}
243
Holger Brunck7292a182020-10-30 12:45:49 +0100244U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid",
245 "read out bid and hwkey from IVM and set in environment");
Thomas Herzmann92c91082011-05-12 19:59:22 +0000246
247/*
248 * command km_checkbidhwk
249 * if "boardid" and "hwkey" are not already set in the environment, do:
250 * if a "boardIdListHex" exists in the environment:
251 * - read ivm data for boardid and hwkey
252 * - compare each entry of the boardIdListHex with the
253 * IVM data:
254 * if match:
255 * set environment variables boardid, boardId,
256 * hwkey, hwKey to the found values
257 * both (boardid and boardId) are set because
258 * they might be used differently in the
259 * application and in the init scripts (?)
260 * return 0 in case of match, 1 if not match or error
261 */
Simon Glass09140112020-05-10 11:40:03 -0600262static int do_checkboardidhwk(struct cmd_tbl *cmdtp, int flag, int argc,
263 char *const argv[])
Thomas Herzmann92c91082011-05-12 19:59:22 +0000264{
265 unsigned long ivmbid = 0, ivmhwkey = 0;
266 unsigned long envbid = 0, envhwkey = 0;
267 char *p;
268 int verbose = argc > 1 && *argv[1] == 'v';
269 int rc = 0;
270
271 /*
272 * first read out the real inventory values, these values are
273 * already stored in the local hush variables
274 */
275 p = get_local_var("IVM_BoardId");
Holger Brunck7292a182020-10-30 12:45:49 +0100276 if (!p) {
Thomas Herzmann92c91082011-05-12 19:59:22 +0000277 printf("can't get the IVM_Boardid\n");
278 return 1;
279 }
280 rc = strict_strtoul(p, 16, &ivmbid);
281
282 p = get_local_var("IVM_HWKey");
Holger Brunck7292a182020-10-30 12:45:49 +0100283 if (!p) {
Thomas Herzmann92c91082011-05-12 19:59:22 +0000284 printf("can't get the IVM_HWKey\n");
285 return 1;
286 }
287 rc = strict_strtoul(p, 16, &ivmhwkey);
288
289 if (!ivmbid || !ivmhwkey) {
290 printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
291 return rc;
292 }
293
294 /* now try to read values from environment if available */
Simon Glass00caae62017-08-03 12:22:12 -0600295 p = env_get("boardid");
Holger Brunck7292a182020-10-30 12:45:49 +0100296 if (p)
Thomas Herzmann92c91082011-05-12 19:59:22 +0000297 rc = strict_strtoul(p, 16, &envbid);
Simon Glass00caae62017-08-03 12:22:12 -0600298 p = env_get("hwkey");
Holger Brunck7292a182020-10-30 12:45:49 +0100299 if (p)
Thomas Herzmann92c91082011-05-12 19:59:22 +0000300 rc = strict_strtoul(p, 16, &envhwkey);
Thomas Herzmann92c91082011-05-12 19:59:22 +0000301 if (rc != 0) {
302 printf("strict_strtoul returns error: %d", rc);
303 return rc;
304 }
305
306 if (!envbid || !envhwkey) {
307 /*
308 * BoardId/HWkey not available in the environment, so try the
309 * environment variable for BoardId/HWkey list
310 */
Simon Glass00caae62017-08-03 12:22:12 -0600311 char *bidhwklist = env_get("boardIdListHex");
Thomas Herzmann92c91082011-05-12 19:59:22 +0000312
313 if (bidhwklist) {
314 int found = 0;
315 char *rest = bidhwklist;
316 char *endp;
317
318 if (verbose) {
319 printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
Holger Brunck7292a182020-10-30 12:45:49 +0100320 ivmbid, ivmhwkey);
321 printf("boardIdHwKeyList: %s\n", bidhwklist);
Thomas Herzmann92c91082011-05-12 19:59:22 +0000322 }
323 while (!found) {
324 /* loop over each bid/hwkey pair in the list */
325 unsigned long bid = 0;
326 unsigned long hwkey = 0;
327
328 while (*rest && !isxdigit(*rest))
329 rest++;
330 /*
331 * use simple_strtoul because we need &end and
332 * we know we got non numeric char at the end
333 */
Simon Glass7e5f4602021-07-24 09:03:29 -0600334 bid = hextoul(rest, &endp);
Thomas Herzmann92c91082011-05-12 19:59:22 +0000335 /* BoardId and HWkey are separated with a "_" */
336 if (*endp == '_') {
337 rest = endp + 1;
338 /*
339 * use simple_strtoul because we need
340 * &end
341 */
Simon Glass7e5f4602021-07-24 09:03:29 -0600342 hwkey = hextoul(rest, &endp);
Thomas Herzmann92c91082011-05-12 19:59:22 +0000343 rest = endp;
344 while (*rest && !isxdigit(*rest))
345 rest++;
346 }
Holger Brunck7292a182020-10-30 12:45:49 +0100347 if (!bid || !hwkey) {
Thomas Herzmann92c91082011-05-12 19:59:22 +0000348 /* end of list */
349 break;
350 }
351 if (verbose) {
352 printf("trying bid=0x%lX, hwkey=%ld\n",
Holger Brunck7292a182020-10-30 12:45:49 +0100353 bid, hwkey);
Thomas Herzmann92c91082011-05-12 19:59:22 +0000354 }
355 /*
356 * Compare the values of the found entry in the
357 * list with the valid values which are stored
358 * in the inventory eeprom. If they are equal
Holger Brunckba8be322011-06-05 22:22:17 +0000359 * set the values in environment variables.
Thomas Herzmann92c91082011-05-12 19:59:22 +0000360 */
Holger Brunck7292a182020-10-30 12:45:49 +0100361 if (bid == ivmbid && hwkey == ivmhwkey) {
Thomas Herzmann92c91082011-05-12 19:59:22 +0000362 found = 1;
363 envbid = bid;
364 envhwkey = hwkey;
Niel Fouriedf86d322021-01-21 13:19:19 +0100365 env_set_hex("boardid", bid);
366 env_set_hex("hwkey", hwkey);
Thomas Herzmann92c91082011-05-12 19:59:22 +0000367 }
368 } /* end while( ! found ) */
369 }
370 }
371
372 /* compare now the values */
Holger Brunck7292a182020-10-30 12:45:49 +0100373 if (ivmbid == envbid && ivmhwkey == envhwkey) {
Thomas Herzmann92c91082011-05-12 19:59:22 +0000374 printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
375 rc = 0; /* match */
376 } else {
Holger Brunck9485e772011-07-04 22:23:59 +0000377 printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
Holger Brunck7292a182020-10-30 12:45:49 +0100378 envhwkey);
Thomas Herzmann92c91082011-05-12 19:59:22 +0000379 printf(" IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
380 rc = 1; /* don't match */
381 }
382 return rc;
383}
384
385U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
Holger Brunck7292a182020-10-30 12:45:49 +0100386 "check boardid and hwkey",
387 "[v]\n - check environment parameter \"boardIdListHex\" against stored boardid and hwkey from the IVM\n v: verbose output"
Thomas Herzmann92c91082011-05-12 19:59:22 +0000388);
Thomas Herzmannc1b3d842012-05-04 10:55:58 +0200389
390/*
391 * command km_checktestboot
392 * if the testpin of the board is asserted, return 1
393 * * else return 0
394 */
Simon Glass09140112020-05-10 11:40:03 -0600395static int do_checktestboot(struct cmd_tbl *cmdtp, int flag, int argc,
396 char *const argv[])
Thomas Herzmannc1b3d842012-05-04 10:55:58 +0200397{
398 int testpin = 0;
399 char *s = NULL;
400 int testboot = 0;
401 int verbose = argc > 1 && *argv[1] == 'v';
402
403#if defined(CONFIG_POST)
404 testpin = post_hotkeys_pressed();
Thomas Herzmannc1b3d842012-05-04 10:55:58 +0200405#endif
Holger Brunck7292a182020-10-30 12:45:49 +0100406
Simon Glass00caae62017-08-03 12:22:12 -0600407 s = env_get("test_bank");
Thomas Herzmannc1b3d842012-05-04 10:55:58 +0200408 /* when test_bank is not set, act as if testpin is not asserted */
409 testboot = (testpin != 0) && (s);
410 if (verbose) {
411 printf("testpin = %d\n", testpin);
Wolfgang Denk00605172014-11-06 14:02:57 +0100412 /* cppcheck-suppress nullPointer */
Thomas Herzmannc1b3d842012-05-04 10:55:58 +0200413 printf("test_bank = %s\n", s ? s : "not set");
414 printf("boot test app : %s\n", (testboot) ? "yes" : "no");
415 }
416 /* return 0 means: testboot, therefore we need the inversion */
417 return !testboot;
418}
419
420U_BOOT_CMD(km_checktestboot, 2, 0, do_checktestboot,
Holger Brunck7292a182020-10-30 12:45:49 +0100421 "check if testpin is asserted",
422 "[v]\n v - verbose output"
Thomas Herzmannc1b3d842012-05-04 10:55:58 +0200423);