xref: /openbmc/u-boot/board/keymile/common/common.c (revision ea818dbb)
1 /*
2  * (C) Copyright 2008
3  * Heiko Schocher, DENX Software Engineering, hs@denx.de.
4  *
5  * (C) Copyright 2011
6  * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com
7  *
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of
14  * the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24  * MA 02111-1307 USA
25  */
26 
27 #include <common.h>
28 #include <ioports.h>
29 #include <command.h>
30 #include <malloc.h>
31 #include <hush.h>
32 #include <net.h>
33 #include <netdev.h>
34 #include <asm/io.h>
35 #include <linux/ctype.h>
36 
37 #if defined(CONFIG_POST)
38 #include "post.h"
39 #endif
40 #include "common.h"
41 #include <i2c.h>
42 
43 #if !defined(CONFIG_MPC83xx)
44 static void i2c_write_start_seq(void);
45 #endif
46 
47 DECLARE_GLOBAL_DATA_PTR;
48 
49 /*
50  * Set Keymile specific environment variables
51  * Currently only some memory layout variables are calculated here
52  * ... ------------------------------------------------
53  * ... |@rootfsaddr |@pnvramaddr |@varaddr |@reserved |@END_OF_RAM
54  * ... |<------------------- pram ------------------->|
55  * ... ------------------------------------------------
56  * @END_OF_RAM: denotes the RAM size
57  * @pnvramaddr: Startadress of pseudo non volatile RAM in hex
58  * @pram      : preserved ram size in k
59  * @varaddr   : startadress for /var mounted into RAM
60  */
61 int set_km_env(void)
62 {
63 	uchar buf[32];
64 	unsigned int pnvramaddr;
65 	unsigned int pram;
66 	unsigned int varaddr;
67 	unsigned int kernelmem;
68 	char *p;
69 	unsigned long rootfssize = 0;
70 
71 	pnvramaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM
72 			- CONFIG_KM_PNVRAM;
73 	sprintf((char *)buf, "0x%x", pnvramaddr);
74 	setenv("pnvramaddr", (char *)buf);
75 
76 	/* try to read rootfssize (ram image) from envrionment */
77 	p = getenv("rootfssize");
78 	if (p != NULL)
79 		strict_strtoul(p, 16, &rootfssize);
80 	pram = (rootfssize + CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM +
81 		CONFIG_KM_PNVRAM) / 0x400;
82 	sprintf((char *)buf, "0x%x", pram);
83 	setenv("pram", (char *)buf);
84 
85 	varaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
86 	sprintf((char *)buf, "0x%x", varaddr);
87 	setenv("varaddr", (char *)buf);
88 
89 	kernelmem = gd->ram_size - 0x400 * pram;
90 	sprintf((char *)buf, "0x%x", kernelmem);
91 	setenv("kernelmem", (char *)buf);
92 
93 	return 0;
94 }
95 
96 #if defined(CONFIG_SYS_I2C_INIT_BOARD)
97 #if !defined(CONFIG_MPC83xx)
98 static void i2c_write_start_seq(void)
99 {
100 	set_sda(1);
101 	udelay(DELAY_HALF_PERIOD);
102 	set_scl(1);
103 	udelay(DELAY_HALF_PERIOD);
104 	set_sda(0);
105 	udelay(DELAY_HALF_PERIOD);
106 	set_scl(0);
107 	udelay(DELAY_HALF_PERIOD);
108 }
109 
110 /*
111  * I2C is a synchronous protocol and resets of the processor in the middle
112  * of an access can block the I2C Bus until a powerdown of the full unit is
113  * done. This function toggles the SCL until the SCL and SCA line are
114  * released, but max. 16 times, after this a I2C start-sequence is sent.
115  * This I2C Deblocking mechanism was developed by Keymile in association
116  * with Anatech and Atmel in 1998.
117  */
118 int i2c_make_abort(void)
119 {
120 
121 #if defined(CONFIG_HARD_I2C) && !defined(MACH_TYPE_KM_KIRKWOOD)
122 	immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
123 	i2c8260_t *i2c	= (i2c8260_t *)&immap->im_i2c;
124 
125 	/*
126 	 * disable I2C controller first, otherwhise it thinks we want to
127 	 * talk to the slave port...
128 	 */
129 	clrbits_8(&i2c->i2c_i2mod, 0x01);
130 
131 	/* Set the PortPins to GPIO */
132 	setports(1);
133 #endif
134 
135 	int	scl_state = 0;
136 	int	sda_state = 0;
137 	int	i = 0;
138 	int	ret = 0;
139 
140 	if (!get_sda()) {
141 		ret = -1;
142 		while (i < 16) {
143 			i++;
144 			set_scl(0);
145 			udelay(DELAY_ABORT_SEQ);
146 			set_scl(1);
147 			udelay(DELAY_ABORT_SEQ);
148 			scl_state = get_scl();
149 			sda_state = get_sda();
150 			if (scl_state && sda_state) {
151 				ret = 0;
152 				break;
153 			}
154 		}
155 	}
156 	if (ret == 0)
157 		for (i = 0; i < 5; i++)
158 			i2c_write_start_seq();
159 
160 	/* respect stop setup time */
161 	udelay(DELAY_ABORT_SEQ);
162 	set_scl(1);
163 	udelay(DELAY_ABORT_SEQ);
164 	set_sda(1);
165 	get_sda();
166 
167 #if defined(CONFIG_HARD_I2C)
168 	/* Set the PortPins back to use for I2C */
169 	setports(0);
170 #endif
171 	return ret;
172 }
173 #endif
174 
175 /**
176  * i2c_init_board - reset i2c bus. When the board is powercycled during a
177  * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
178  */
179 void i2c_init_board(void)
180 {
181 	/* Now run the AbortSequence() */
182 	i2c_make_abort();
183 }
184 #endif
185 
186 int board_eth_init(bd_t *bis)
187 {
188 	if (ethernet_present())
189 		return cpu_eth_init(bis);
190 
191 	return -1;
192 }
193 
194 /*
195  * do_setboardid command
196  * read out the board id and the hw key from the intventory EEPROM and set
197  * this values as environment variables.
198  */
199 static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc,
200 				char *const argv[])
201 {
202 	unsigned char buf[32];
203 	char *p;
204 
205 	p = get_local_var("IVM_BoardId");
206 	if (p == NULL) {
207 		printf("can't get the IVM_Boardid\n");
208 		return 1;
209 	}
210 	sprintf((char *)buf, "%s", p);
211 	setenv("boardid", (char *)buf);
212 	printf("set boardid=%s\n", buf);
213 
214 	p = get_local_var("IVM_HWKey");
215 	if (p == NULL) {
216 		printf("can't get the IVM_HWKey\n");
217 		return 1;
218 	}
219 	sprintf((char *)buf, "%s", p);
220 	setenv("hwkey", (char *)buf);
221 	printf("set hwkey=%s\n", buf);
222 	printf("Execute manually saveenv for persistent storage.\n");
223 
224 	return 0;
225 }
226 
227 U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
228 				 "hwkey from IVM and set in environment");
229 
230 /*
231  * command km_checkbidhwk
232  *	if "boardid" and "hwkey" are not already set in the environment, do:
233  *		if a "boardIdListHex" exists in the environment:
234  *			- read ivm data for boardid and hwkey
235  *			- compare each entry of the boardIdListHex with the
236  *				IVM data:
237  *			if match:
238  *				set environment variables boardid, boardId,
239  *				hwkey, hwKey to	the found values
240  *				both (boardid and boardId) are set because
241  *				they might be used differently in the
242  *				application and in the init scripts (?)
243  *	return 0 in case of match, 1 if not match or error
244  */
245 int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
246 			char *const argv[])
247 {
248 	unsigned long ivmbid = 0, ivmhwkey = 0;
249 	unsigned long envbid = 0, envhwkey = 0;
250 	char *p;
251 	int verbose = argc > 1 && *argv[1] == 'v';
252 	int rc = 0;
253 
254 	/*
255 	 * first read out the real inventory values, these values are
256 	 * already stored in the local hush variables
257 	 */
258 	p = get_local_var("IVM_BoardId");
259 	if (p == NULL) {
260 		printf("can't get the IVM_Boardid\n");
261 		return 1;
262 	}
263 	rc = strict_strtoul(p, 16, &ivmbid);
264 
265 	p = get_local_var("IVM_HWKey");
266 	if (p == NULL) {
267 		printf("can't get the IVM_HWKey\n");
268 		return 1;
269 	}
270 	rc = strict_strtoul(p, 16, &ivmhwkey);
271 
272 	if (!ivmbid || !ivmhwkey) {
273 		printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
274 		return rc;
275 	}
276 
277 	/* now try to read values from environment if available */
278 	p = getenv("boardid");
279 	if (p != NULL)
280 		rc = strict_strtoul(p, 16, &envbid);
281 	p = getenv("hwkey");
282 	if (p != NULL)
283 		rc = strict_strtoul(p, 16, &envhwkey);
284 
285 	if (rc != 0) {
286 		printf("strict_strtoul returns error: %d", rc);
287 		return rc;
288 	}
289 
290 	if (!envbid || !envhwkey) {
291 		/*
292 		 * BoardId/HWkey not available in the environment, so try the
293 		 * environment variable for BoardId/HWkey list
294 		 */
295 		char *bidhwklist = getenv("boardIdListHex");
296 
297 		if (bidhwklist) {
298 			int found = 0;
299 			char *rest = bidhwklist;
300 			char *endp;
301 
302 			if (verbose) {
303 				printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
304 					ivmbid, ivmhwkey);
305 				printf("boardIdHwKeyList: %s\n",
306 					bidhwklist);
307 			}
308 			while (!found) {
309 				/* loop over each bid/hwkey pair in the list */
310 				unsigned long bid   = 0;
311 				unsigned long hwkey = 0;
312 
313 				while (*rest && !isxdigit(*rest))
314 					rest++;
315 				/*
316 				 * use simple_strtoul because we need &end and
317 				 * we know we got non numeric char at the end
318 				 */
319 				bid = simple_strtoul(rest, &endp, 16);
320 				/* BoardId and HWkey are separated with a "_" */
321 				if (*endp == '_') {
322 					rest  = endp + 1;
323 					/*
324 					 * use simple_strtoul because we need
325 					 * &end
326 					 */
327 					hwkey = simple_strtoul(rest, &endp, 16);
328 					rest  = endp;
329 					while (*rest && !isxdigit(*rest))
330 						rest++;
331 				}
332 				if ((!bid) || (!hwkey)) {
333 					/* end of list */
334 					break;
335 				}
336 				if (verbose) {
337 					printf("trying bid=0x%lX, hwkey=%ld\n",
338 						bid, hwkey);
339 				}
340 				/*
341 				 * Compare the values of the found entry in the
342 				 * list with the valid values which are stored
343 				 * in the inventory eeprom. If they are equal
344 				 * set the values in environment variables.
345 				 */
346 				if ((bid == ivmbid) && (hwkey == ivmhwkey)) {
347 					char buf[10];
348 
349 					found = 1;
350 					envbid   = bid;
351 					envhwkey = hwkey;
352 					sprintf(buf, "%lx", bid);
353 					setenv("boardid", buf);
354 					sprintf(buf, "%lx", hwkey);
355 					setenv("hwkey", buf);
356 				}
357 			} /* end while( ! found ) */
358 		}
359 	}
360 
361 	/* compare now the values */
362 	if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) {
363 		printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
364 		rc = 0; /* match */
365 	} else {
366 		printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
367 			envhwkey);
368 		printf("       IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
369 		rc = 1; /* don't match */
370 	}
371 	return rc;
372 }
373 
374 U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
375 		"check boardid and hwkey",
376 		"[v]\n  - check environment parameter "\
377 		"\"boardIdListHex\" against stored boardid and hwkey "\
378 		"from the IVM\n    v: verbose output"
379 );
380 
381 /*
382  * command km_checktestboot
383  *  if the testpin of the board is asserted, return 1
384  *  *	else return 0
385  */
386 int do_checktestboot(cmd_tbl_t *cmdtp, int flag, int argc,
387 			char *const argv[])
388 {
389 	int testpin = 0;
390 	char *s = NULL;
391 	int testboot = 0;
392 	int verbose = argc > 1 && *argv[1] == 'v';
393 
394 #if defined(CONFIG_POST)
395 	testpin = post_hotkeys_pressed();
396 	s = getenv("test_bank");
397 #endif
398 	/* when test_bank is not set, act as if testpin is not asserted */
399 	testboot = (testpin != 0) && (s);
400 	if (verbose) {
401 		printf("testpin   = %d\n", testpin);
402 		printf("test_bank = %s\n", s ? s : "not set");
403 		printf("boot test app : %s\n", (testboot) ? "yes" : "no");
404 	}
405 	/* return 0 means: testboot, therefore we need the inversion */
406 	return !testboot;
407 }
408 
409 U_BOOT_CMD(km_checktestboot, 2, 0, do_checktestboot,
410 		"check if testpin is asserted",
411 		"[v]\n  v - verbose output"
412 );
413