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