xref: /openbmc/u-boot/board/keymile/common/common.c (revision 5187d8dd)
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 #include "common.h"
38 #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
39 #include <i2c.h>
40 #endif
41 
42 static void i2c_write_start_seq(void);
43 DECLARE_GLOBAL_DATA_PTR;
44 
45 /*
46  * Set Keymile specific environment variables
47  * Currently only some memory layout variables are calculated here
48  * ... ------------------------------------------------
49  * ... |@rootfsaddr |@pnvramaddr |@varaddr |@reserved |@END_OF_RAM
50  * ... |<------------------- pram ------------------->|
51  * ... ------------------------------------------------
52  * @END_OF_RAM: denotes the RAM size
53  * @pnvramaddr: Startadress of pseudo non volatile RAM in hex
54  * @pram      : preserved ram size in k
55  * @varaddr   : startadress for /var mounted into RAM
56  */
57 int set_km_env(void)
58 {
59 	uchar buf[32];
60 	unsigned int pnvramaddr;
61 	unsigned int pram;
62 	unsigned int varaddr;
63 	unsigned int kernelmem;
64 	char *p;
65 	unsigned long rootfssize = 0;
66 
67 	pnvramaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM
68 			- CONFIG_KM_PNVRAM;
69 	sprintf((char *)buf, "0x%x", pnvramaddr);
70 	setenv("pnvramaddr", (char *)buf);
71 
72 	/* try to read rootfssize (ram image) from envrionment */
73 	p = getenv("rootfssize");
74 	if (p != NULL)
75 		strict_strtoul(p, 16, &rootfssize);
76 	pram = (rootfssize + CONFIG_KM_RESERVED_PRAM + CONFIG_KM_PHRAM +
77 		CONFIG_KM_PNVRAM) / 0x400;
78 	sprintf((char *)buf, "0x%x", pram);
79 	setenv("pram", (char *)buf);
80 
81 	varaddr = gd->ram_size - CONFIG_KM_RESERVED_PRAM - CONFIG_KM_PHRAM;
82 	sprintf((char *)buf, "0x%x", varaddr);
83 	setenv("varaddr", (char *)buf);
84 
85 	kernelmem = gd->ram_size - 0x400 * pram;
86 	sprintf((char *)buf, "0x%x", kernelmem);
87 	setenv("kernelmem", (char *)buf);
88 
89 	return 0;
90 }
91 
92 #define DELAY_ABORT_SEQ		62  /* @200kHz 9 clocks = 44us, 62us is ok */
93 #define DELAY_HALF_PERIOD	(500 / (CONFIG_SYS_I2C_SPEED / 1000))
94 
95 #if !defined(CONFIG_MPC83xx)
96 static void i2c_write_start_seq(void)
97 {
98 	set_sda(1);
99 	udelay(DELAY_HALF_PERIOD);
100 	set_scl(1);
101 	udelay(DELAY_HALF_PERIOD);
102 	set_sda(0);
103 	udelay(DELAY_HALF_PERIOD);
104 	set_scl(0);
105 	udelay(DELAY_HALF_PERIOD);
106 }
107 
108 /*
109  * I2C is a synchronous protocol and resets of the processor in the middle
110  * of an access can block the I2C Bus until a powerdown of the full unit is
111  * done. This function toggles the SCL until the SCL and SCA line are
112  * released, but max. 16 times, after this a I2C start-sequence is sent.
113  * This I2C Deblocking mechanism was developed by Keymile in association
114  * with Anatech and Atmel in 1998.
115  */
116 int i2c_make_abort(void)
117 {
118 
119 #if defined(CONFIG_HARD_I2C) && !defined(MACH_TYPE_KM_KIRKWOOD)
120 	immap_t *immap = (immap_t *)CONFIG_SYS_IMMR ;
121 	i2c8260_t *i2c	= (i2c8260_t *)&immap->im_i2c;
122 
123 	/*
124 	 * disable I2C controller first, otherwhise it thinks we want to
125 	 * talk to the slave port...
126 	 */
127 	clrbits_8(&i2c->i2c_i2mod, 0x01);
128 
129 	/* Set the PortPins to GPIO */
130 	setports(1);
131 #endif
132 
133 	int	scl_state = 0;
134 	int	sda_state = 0;
135 	int	i = 0;
136 	int	ret = 0;
137 
138 	if (!get_sda()) {
139 		ret = -1;
140 		while (i < 16) {
141 			i++;
142 			set_scl(0);
143 			udelay(DELAY_ABORT_SEQ);
144 			set_scl(1);
145 			udelay(DELAY_ABORT_SEQ);
146 			scl_state = get_scl();
147 			sda_state = get_sda();
148 			if (scl_state && sda_state) {
149 				ret = 0;
150 				printf("[INFO] i2c abort after %d clocks\n", i);
151 				break;
152 			}
153 		}
154 	}
155 	if (ret == 0)
156 		for (i = 0; i < 5; i++)
157 			i2c_write_start_seq();
158 	else
159 		printf("[ERROR] i2c abort failed\n");
160 
161 	/* respect stop setup time */
162 	udelay(DELAY_ABORT_SEQ);
163 	set_scl(1);
164 	udelay(DELAY_ABORT_SEQ);
165 	set_sda(1);
166 	get_sda();
167 
168 #if defined(CONFIG_HARD_I2C)
169 	/* Set the PortPins back to use for I2C */
170 	setports(0);
171 #endif
172 	return ret;
173 }
174 #endif /* !MPC83xx */
175 
176 #if defined(CONFIG_MPC83xx)
177 static void i2c_write_start_seq(void)
178 {
179 	struct fsl_i2c *dev;
180 	dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
181 	udelay(DELAY_ABORT_SEQ);
182 	out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
183 	udelay(DELAY_ABORT_SEQ);
184 	out_8(&dev->cr, (I2C_CR_MEN));
185 }
186 
187 int i2c_make_abort(void)
188 {
189 	struct fsl_i2c *dev;
190 	dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
191 	uchar	dummy;
192 	uchar   last;
193 	int     nbr_read = 0;
194 	int     i = 0;
195 	int	    ret = 0;
196 
197 	/* wait after each operation to finsh with a delay */
198 	out_8(&dev->cr, (I2C_CR_MSTA));
199 	udelay(DELAY_ABORT_SEQ);
200 	out_8(&dev->cr, (I2C_CR_MEN | I2C_CR_MSTA));
201 	udelay(DELAY_ABORT_SEQ);
202 	dummy = in_8(&dev->dr);
203 	udelay(DELAY_ABORT_SEQ);
204 	last = in_8(&dev->dr);
205 	nbr_read++;
206 
207 	/*
208 	 * do read until the last bit is 1, but stop if the full eeprom is
209 	 * read.
210 	 */
211 	while (((last & 0x01) != 0x01) &&
212 		(nbr_read < CONFIG_SYS_IVM_EEPROM_MAX_LEN)) {
213 		udelay(DELAY_ABORT_SEQ);
214 		last = in_8(&dev->dr);
215 		nbr_read++;
216 	}
217 	if ((last & 0x01) != 0x01)
218 		ret = -2;
219 	if ((last != 0xff) || (nbr_read > 1))
220 		printf("[INFO] i2c abort after %d bytes (0x%02x)\n",
221 			nbr_read, last);
222 	udelay(DELAY_ABORT_SEQ);
223 	out_8(&dev->cr, (I2C_CR_MEN));
224 	udelay(DELAY_ABORT_SEQ);
225 	/* clear status reg */
226 	out_8(&dev->sr, 0);
227 
228 	for (i = 0; i < 5; i++)
229 		i2c_write_start_seq();
230 	if (ret != 0)
231 		printf("[ERROR] i2c abort failed after %d bytes (0x%02x)\n",
232 			nbr_read, last);
233 
234 	return ret;
235 }
236 #endif
237 
238 /**
239  * i2c_init_board - reset i2c bus. When the board is powercycled during a
240  * bus transfer it might hang; for details see doc/I2C_Edge_Conditions.
241  */
242 void i2c_init_board(void)
243 {
244 	/* Now run the AbortSequence() */
245 	i2c_make_abort();
246 }
247 
248 #if !defined(MACH_TYPE_KM_KIRKWOOD)
249 int ethernet_present(void)
250 {
251 	struct km_bec_fpga *base =
252 		(struct km_bec_fpga *)CONFIG_SYS_KMBEC_FPGA_BASE;
253 
254 	return in_8(&base->bprth) & PIGGY_PRESENT;
255 }
256 #endif
257 
258 int board_eth_init(bd_t *bis)
259 {
260 	if (ethernet_present())
261 		return cpu_eth_init(bis);
262 
263 	return -1;
264 }
265 
266 /*
267  * do_setboardid command
268  * read out the board id and the hw key from the intventory EEPROM and set
269  * this values as environment variables.
270  */
271 static int do_setboardid(cmd_tbl_t *cmdtp, int flag, int argc,
272 				char *const argv[])
273 {
274 	unsigned char buf[32];
275 	char *p;
276 
277 	p = get_local_var("IVM_BoardId");
278 	if (p == NULL) {
279 		printf("can't get the IVM_Boardid\n");
280 		return 1;
281 	}
282 	sprintf((char *)buf, "%s", p);
283 	setenv("boardid", (char *)buf);
284 	printf("set boardid=%s\n", buf);
285 
286 	p = get_local_var("IVM_HWKey");
287 	if (p == NULL) {
288 		printf("can't get the IVM_HWKey\n");
289 		return 1;
290 	}
291 	sprintf((char *)buf, "%s", p);
292 	setenv("hwkey", (char *)buf);
293 	printf("set hwkey=%s\n", buf);
294 	printf("Execute manually saveenv for persistent storage.\n");
295 
296 	return 0;
297 }
298 
299 U_BOOT_CMD(km_setboardid, 1, 0, do_setboardid, "setboardid", "read out bid and "
300 				 "hwkey from IVM and set in environment");
301 
302 /*
303  * command km_checkbidhwk
304  *	if "boardid" and "hwkey" are not already set in the environment, do:
305  *		if a "boardIdListHex" exists in the environment:
306  *			- read ivm data for boardid and hwkey
307  *			- compare each entry of the boardIdListHex with the
308  *				IVM data:
309  *			if match:
310  *				set environment variables boardid, boardId,
311  *				hwkey, hwKey to	the found values
312  *				both (boardid and boardId) are set because
313  *				they might be used differently in the
314  *				application and in the init scripts (?)
315  *	return 0 in case of match, 1 if not match or error
316  */
317 int do_checkboardidhwk(cmd_tbl_t *cmdtp, int flag, int argc,
318 			char *const argv[])
319 {
320 	unsigned long ivmbid = 0, ivmhwkey = 0;
321 	unsigned long envbid = 0, envhwkey = 0;
322 	char *p;
323 	int verbose = argc > 1 && *argv[1] == 'v';
324 	int rc = 0;
325 
326 	/*
327 	 * first read out the real inventory values, these values are
328 	 * already stored in the local hush variables
329 	 */
330 	p = get_local_var("IVM_BoardId");
331 	if (p == NULL) {
332 		printf("can't get the IVM_Boardid\n");
333 		return 1;
334 	}
335 	rc = strict_strtoul(p, 16, &ivmbid);
336 
337 	p = get_local_var("IVM_HWKey");
338 	if (p == NULL) {
339 		printf("can't get the IVM_HWKey\n");
340 		return 1;
341 	}
342 	rc = strict_strtoul(p, 16, &ivmhwkey);
343 
344 	if (!ivmbid || !ivmhwkey) {
345 		printf("Error: IVM_BoardId and/or IVM_HWKey not set!\n");
346 		return rc;
347 	}
348 
349 	/* now try to read values from environment if available */
350 	p = getenv("boardid");
351 	if (p != NULL)
352 		rc = strict_strtoul(p, 16, &envbid);
353 	p = getenv("hwkey");
354 	if (p != NULL)
355 		rc = strict_strtoul(p, 16, &envhwkey);
356 
357 	if (rc != 0) {
358 		printf("strict_strtoul returns error: %d", rc);
359 		return rc;
360 	}
361 
362 	if (!envbid || !envhwkey) {
363 		/*
364 		 * BoardId/HWkey not available in the environment, so try the
365 		 * environment variable for BoardId/HWkey list
366 		 */
367 		char *bidhwklist = getenv("boardIdListHex");
368 
369 		if (bidhwklist) {
370 			int found = 0;
371 			char *rest = bidhwklist;
372 			char *endp;
373 
374 			if (verbose) {
375 				printf("IVM_BoardId: %ld, IVM_HWKey=%ld\n",
376 					ivmbid, ivmhwkey);
377 				printf("boardIdHwKeyList: %s\n",
378 					bidhwklist);
379 			}
380 			while (!found) {
381 				/* loop over each bid/hwkey pair in the list */
382 				unsigned long bid   = 0;
383 				unsigned long hwkey = 0;
384 
385 				while (*rest && !isxdigit(*rest))
386 					rest++;
387 				/*
388 				 * use simple_strtoul because we need &end and
389 				 * we know we got non numeric char at the end
390 				 */
391 				bid = simple_strtoul(rest, &endp, 16);
392 				/* BoardId and HWkey are separated with a "_" */
393 				if (*endp == '_') {
394 					rest  = endp + 1;
395 					/*
396 					 * use simple_strtoul because we need
397 					 * &end
398 					 */
399 					hwkey = simple_strtoul(rest, &endp, 16);
400 					rest  = endp;
401 					while (*rest && !isxdigit(*rest))
402 						rest++;
403 				}
404 				if ((!bid) || (!hwkey)) {
405 					/* end of list */
406 					break;
407 				}
408 				if (verbose) {
409 					printf("trying bid=0x%lX, hwkey=%ld\n",
410 						bid, hwkey);
411 				}
412 				/*
413 				 * Compare the values of the found entry in the
414 				 * list with the valid values which are stored
415 				 * in the inventory eeprom. If they are equal
416 				 * set the values in environment variables.
417 				 */
418 				if ((bid == ivmbid) && (hwkey == ivmhwkey)) {
419 					char buf[10];
420 
421 					found = 1;
422 					envbid   = bid;
423 					envhwkey = hwkey;
424 					sprintf(buf, "%lx", bid);
425 					setenv("boardid", buf);
426 					sprintf(buf, "%lx", hwkey);
427 					setenv("hwkey", buf);
428 				}
429 			} /* end while( ! found ) */
430 		}
431 	}
432 
433 	/* compare now the values */
434 	if ((ivmbid == envbid) && (ivmhwkey == envhwkey)) {
435 		printf("boardid=0x%3lX, hwkey=%ld\n", envbid, envhwkey);
436 		rc = 0; /* match */
437 	} else {
438 		printf("Error: env boardid=0x%3lX, hwkey=%ld\n", envbid,
439 			envhwkey);
440 		printf("       IVM bId=0x%3lX, hwKey=%ld\n", ivmbid, ivmhwkey);
441 		rc = 1; /* don't match */
442 	}
443 	return rc;
444 }
445 
446 U_BOOT_CMD(km_checkbidhwk, 2, 0, do_checkboardidhwk,
447 		"check boardid and hwkey",
448 		"[v]\n  - check environment parameter "\
449 		"\"boardIdListHex\" against stored boardid and hwkey "\
450 		"from the IVM\n    v: verbose output"
451 );
452