1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2008
4  * Heiko Schocher, DENX Software Engineering, hs@denx.de.
5  *
6  * (C) Copyright 2011
7  * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com
8  */
9 
10 #include <common.h>
11 #include <env.h>
12 #include <ioports.h>
13 #include <command.h>
14 #include <malloc.h>
15 #include <cli_hush.h>
16 #include <net.h>
17 #include <netdev.h>
18 #include <asm/global_data.h>
19 #include <asm/io.h>
20 #include <linux/ctype.h>
21 #include <linux/delay.h>
22 
23 #if defined(CONFIG_POST)
24 #include "post.h"
25 #endif
26 #include "common.h"
27 #include <i2c.h>
28 
29 DECLARE_GLOBAL_DATA_PTR;
30 
31 /*
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  */
set_km_env(void)43 int set_km_env(void)
44 {
45 	unsigned int pnvramaddr;
46 	unsigned int pram;
47 	unsigned int varaddr;
48 	unsigned int kernelmem;
49 	unsigned long rootfssize = 0;
50 	char envval[16];
51 	char *p;
52 
53 	pnvramaddr = CONFIG_SYS_SDRAM_BASE + gd->ram_size -
54 		CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM - CONFIG_KM_PNVRAM;
55 	sprintf(envval, "0x%x", pnvramaddr);
56 	env_set("pnvramaddr", envval);
57 
58 	/* try to read rootfssize (ram image) from environment */
59 	p = env_get("rootfssize");
60 	if (p)
61 		strict_strtoul(p, 16, &rootfssize);
62 	pram = (rootfssize + CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM +
63 		CONFIG_KM_PNVRAM) / 0x400;
64 	env_set_ulong("pram", pram);
65 
66 	varaddr = CONFIG_SYS_SDRAM_BASE + gd->ram_size -
67 		CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
68 	env_set_hex("varaddr", varaddr);
69 	sprintf(envval, "0x%x", varaddr);
70 	env_set("varaddr", envval);
71 
72 	kernelmem = gd->ram_size - 0x400 * pram;
73 	sprintf(envval, "0x%x", kernelmem);
74 	env_set("kernelmem", envval);
75 
76 	return 0;
77 }
78 
79 #if defined(CONFIG_SYS_I2C_INIT_BOARD)
i2c_write_start_seq(void)80 static void i2c_write_start_seq(void)
81 {
82 	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);
90 }
91 
92 /*
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.
99  */
i2c_make_abort(void)100 int i2c_make_abort(void)
101 {
102 	int	scl_state = 0;
103 	int	sda_state = 0;
104 	int	i = 0;
105 	int	ret = 0;
106 
107 	if (!get_sda()) {
108 		ret = -1;
109 		while (i < 16) {
110 			i++;
111 			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();
117 			if (scl_state && sda_state) {
118 				ret = 0;
119 				break;
120 			}
121 		}
122 	}
123 	if (ret == 0)
124 		for (i = 0; i < 5; i++)
125 			i2c_write_start_seq();
126 
127 	/* respect stop setup time */
128 	udelay(DELAY_ABORT_SEQ);
129 	set_scl(1);
130 	udelay(DELAY_ABORT_SEQ);
131 	set_sda(1);
132 	get_sda();
133 
134 	return ret;
135 }
136 
137 /**
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  */
i2c_init_board(void)141 void i2c_init_board(void)
142 {
143 	/* Now run the AbortSequence() */
144 	i2c_make_abort();
145 }
146 #endif
147 
148 #if defined(CONFIG_KM_COMMON_ETH_INIT)
board_eth_init(struct bd_info * bis)149 int board_eth_init(struct bd_info *bis)
150 {
151 	if (ethernet_present())
152 		return cpu_eth_init(bis);
153 
154 	return -1;
155 }
156 #endif
157 
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  */
do_setboardid(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])163 static int do_setboardid(struct cmd_tbl *cmdtp, int flag, int argc,
164 			 char *const argv[])
165 {
166 	unsigned char buf[32];
167 	char *p;
168 
169 	p = get_local_var("IVM_BoardId");
170 	if (!p) {
171 		printf("can't get the IVM_Boardid\n");
172 		return 1;
173 	}
174 	strcpy((char *)buf, p);
175 	env_set("boardid", (char *)buf);
176 	printf("set boardid=%s\n", buf);
177 
178 	p = get_local_var("IVM_HWKey");
179 	if (!p) {
180 		printf("can't get the IVM_HWKey\n");
181 		return 1;
182 	}
183 	strcpy((char *)buf, p);
184 	env_set("hwkey", (char *)buf);
185 	printf("set hwkey=%s\n", buf);
186 	printf("Execute manually saveenv for persistent storage.\n");
187 
188 	return 0;
189 }
190 
191 U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid",
192 	   "read out bid and hwkey from IVM and set in environment");
193 
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  */
do_checkboardidhwk(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])209 static int do_checkboardidhwk(struct cmd_tbl *cmdtp, int flag, int argc,
210 			      char *const argv[])
211 {
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");
223 	if (!p) {
224 		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");
230 	if (!p) {
231 		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 */
242 	p = env_get("boardid");
243 	if (p)
244 		rc = strict_strtoul(p, 16, &envbid);
245 	p = env_get("hwkey");
246 	if (p)
247 		rc = strict_strtoul(p, 16, &envhwkey);
248 	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 		 */
258 		char *bidhwklist = env_get("boardIdListHex");
259 
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",
267 				       ivmbid, ivmhwkey);
268 				printf("boardIdHwKeyList: %s\n", bidhwklist);
269 			}
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 				 */
281 				bid = simple_strtoul(rest, &endp, 16);
282 				/* 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 					 */
289 					hwkey = simple_strtoul(rest, &endp, 16);
290 					rest  = endp;
291 					while (*rest && !isxdigit(*rest))
292 						rest++;
293 				}
294 				if (!bid || !hwkey) {
295 					/* end of list */
296 					break;
297 				}
298 				if (verbose) {
299 					printf("trying bid=0x%lX, hwkey=%ld\n",
300 					       bid, hwkey);
301 				}
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
306 				 * set the values in environment variables.
307 				 */
308 				if (bid == ivmbid && hwkey == ivmhwkey) {
309 					found = 1;
310 					envbid   = bid;
311 					envhwkey = hwkey;
312 					env_set_hex("boardid", bid);
313 					env_set_hex("hwkey", hwkey);
314 				}
315 			} /* end while( ! found ) */
316 		}
317 	}
318 
319 	/* compare now the values */
320 	if (ivmbid == envbid && ivmhwkey == envhwkey) {
321 		printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
322 		rc = 0; /* match */
323 	} else {
324 		printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
325 		       envhwkey);
326 		printf("       IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
327 		rc = 1; /* don't match */
328 	}
329 	return rc;
330 }
331 
332 U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
333 	   "check boardid and hwkey",
334 	   "[v]\n  - check environment parameter \"boardIdListHex\" against stored boardid and hwkey from the IVM\n    v: verbose output"
335 );
336 
337 /*
338  * command km_checktestboot
339  *  if the testpin of the board is asserted, return 1
340  *  *	else return 0
341  */
do_checktestboot(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])342 static int do_checktestboot(struct cmd_tbl *cmdtp, int flag, int argc,
343 			    char *const argv[])
344 {
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();
352 #endif
353 
354 	s = env_get("test_bank");
355 	/* 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);
359 		/* cppcheck-suppress nullPointer */
360 		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 
367 U_BOOT_CMD(km_checktestboot, 2, 0, do_checktestboot,
368 	   "check if testpin is asserted",
369 	   "[v]\n  v - verbose output"
370 );
371