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