1 /*
2  * Copyright 2006, 2008-2009 Freescale Semiconductor
3  * York Sun (yorksun@freescale.com)
4  * Haiying Wang (haiying.wang@freescale.com)
5  * Timur Tabi (timur@freescale.com)
6  *
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  */
25 
26 #include <common.h>
27 #include <command.h>
28 #include <i2c.h>
29 #include <linux/ctype.h>
30 
31 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
32 #include "../common/eeprom.h"
33 #define MAX_NUM_PORTS	8
34 #endif
35 
36 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
37 #define MAX_NUM_PORTS	8
38 #define NXID_VERSION	0
39 #endif
40 
41 #ifdef CONFIG_SYS_I2C_EEPROM_NXID_1
42 #define CONFIG_SYS_I2C_EEPROM_NXID
43 #define MAX_NUM_PORTS	23
44 #define NXID_VERSION	1
45 #endif
46 
47 /**
48  * static eeprom: EEPROM layout for CCID or NXID formats
49  *
50  * See application note AN3638 for details.
51  */
52 static struct __attribute__ ((__packed__)) eeprom {
53 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
54 	u8 id[4];         /* 0x00 - 0x03 EEPROM Tag 'CCID' */
55 	u8 major;         /* 0x04        Board revision, major */
56 	u8 minor;         /* 0x05        Board revision, minor */
57 	u8 sn[10];        /* 0x06 - 0x0F Serial Number*/
58 	u8 errata[2];     /* 0x10 - 0x11 Errata Level */
59 	u8 date[6];       /* 0x12 - 0x17 Build Date */
60 	u8 res_0[40];     /* 0x18 - 0x3f Reserved */
61 	u8 mac_count;     /* 0x40        Number of MAC addresses */
62 	u8 mac_flag;      /* 0x41        MAC table flags */
63 	u8 mac[MAX_NUM_PORTS][6];     /* 0x42 - 0x71 MAC addresses */
64 	u32 crc;          /* 0x72        CRC32 checksum */
65 #endif
66 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
67 	u8 id[4];         /* 0x00 - 0x03 EEPROM Tag 'NXID' */
68 	u8 sn[12];        /* 0x04 - 0x0F Serial Number */
69 	u8 errata[5];     /* 0x10 - 0x14 Errata Level */
70 	u8 date[6];       /* 0x15 - 0x1a Build Date */
71 	u8 res_0;         /* 0x1b        Reserved */
72 	u32 version;      /* 0x1c - 0x1f NXID Version */
73 	u8 tempcal[8];    /* 0x20 - 0x27 Temperature Calibration Factors */
74 	u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */
75 	u8 tempcalflags;  /* 0x2a        Temperature Calibration Flags */
76 	u8 res_1[21];     /* 0x2b - 0x3f Reserved */
77 	u8 mac_count;     /* 0x40        Number of MAC addresses */
78 	u8 mac_flag;      /* 0x41        MAC table flags */
79 	u8 mac[MAX_NUM_PORTS][6];     /* 0x42 - x MAC addresses */
80 	u32 crc;          /* x+1         CRC32 checksum */
81 #endif
82 } e;
83 
84 /* Set to 1 if we've read EEPROM into memory */
85 static int has_been_read = 0;
86 
87 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
88 /* Is this a valid NXID EEPROM? */
89 #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
90 		  (e.id[2] == 'I') || (e.id[3] == 'D'))
91 #endif
92 
93 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
94 /* Is this a valid CCID EEPROM? */
95 #define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
96 		  (e.id[2] == 'I') || (e.id[3] == 'D'))
97 #endif
98 
99 /**
100  * show_eeprom - display the contents of the EEPROM
101  */
102 static void show_eeprom(void)
103 {
104 	int i;
105 	unsigned int crc;
106 
107 	/* EEPROM tag ID, either CCID or NXID */
108 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
109 	printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
110 		be32_to_cpu(e.version));
111 #else
112 	printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
113 #endif
114 
115 	/* Serial number */
116 	printf("SN: %s\n", e.sn);
117 
118 	/* Errata level. */
119 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
120 	printf("Errata: %s\n", e.errata);
121 #else
122 	printf("Errata: %c%c\n",
123 		e.errata[0] ? e.errata[0] : '.',
124 		e.errata[1] ? e.errata[1] : '.');
125 #endif
126 
127 	/* Build date, BCD date values, as YYMMDDhhmmss */
128 	printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
129 		e.date[0], e.date[1], e.date[2],
130 		e.date[3] & 0x7F, e.date[4], e.date[5],
131 		e.date[3] & 0x80 ? "PM" : "");
132 
133 	/* Show MAC addresses  */
134 	for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
135 
136 		u8 *p = e.mac[i];
137 
138 		printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
139 			p[0], p[1], p[2], p[3],	p[4], p[5]);
140 	}
141 
142 	crc = crc32(0, (void *)&e, sizeof(e) - 4);
143 
144 	if (crc == be32_to_cpu(e.crc))
145 		printf("CRC: %08x\n", be32_to_cpu(e.crc));
146 	else
147 		printf("CRC: %08x (should be %08x)\n",
148 			be32_to_cpu(e.crc), crc);
149 
150 #ifdef DEBUG
151 	printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
152 	for (i = 0; i < sizeof(e); i++) {
153 		if ((i % 16) == 0)
154 			printf("%02X: ", i);
155 		printf("%02X ", ((u8 *)&e)[i]);
156 		if (((i % 16) == 15) || (i == sizeof(e) - 1))
157 			printf("\n");
158 	}
159 #endif
160 }
161 
162 /**
163  * read_eeprom - read the EEPROM into memory
164  */
165 static int read_eeprom(void)
166 {
167 	int ret;
168 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
169 	unsigned int bus;
170 #endif
171 
172 	if (has_been_read)
173 		return 0;
174 
175 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
176 	bus = i2c_get_bus_num();
177 	i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
178 #endif
179 
180 	ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
181 		(void *)&e, sizeof(e));
182 
183 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
184 	i2c_set_bus_num(bus);
185 #endif
186 
187 #ifdef DEBUG
188 	show_eeprom();
189 #endif
190 
191 	has_been_read = (ret == 0) ? 1 : 0;
192 
193 	return ret;
194 }
195 
196 /**
197  *  update_crc - update the CRC
198  *
199  *  This function should be called after each update to the EEPROM structure,
200  *  to make sure the CRC is always correct.
201  */
202 static void update_crc(void)
203 {
204 	u32 crc;
205 
206 	crc = crc32(0, (void *)&e, sizeof(e) - 4);
207 	e.crc = cpu_to_be32(crc);
208 }
209 
210 /**
211  * prog_eeprom - write the EEPROM from memory
212  */
213 static int prog_eeprom(void)
214 {
215 	int ret = 0;
216 	int i;
217 	void *p;
218 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
219 	unsigned int bus;
220 #endif
221 
222 	/* Set the reserved values to 0xFF   */
223 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
224 	e.res_0 = 0xFF;
225 	memset(e.res_1, 0xFF, sizeof(e.res_1));
226 #else
227 	memset(e.res_0, 0xFF, sizeof(e.res_0));
228 #endif
229 	update_crc();
230 
231 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
232 	bus = i2c_get_bus_num();
233 	i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
234 #endif
235 
236 	/*
237 	 * The AT24C02 datasheet says that data can only be written in page
238 	 * mode, which means 8 bytes at a time, and it takes up to 5ms to
239 	 * complete a given write.
240 	 */
241 	for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
242 		ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
243 			p, min((sizeof(e) - i), 8));
244 		if (ret)
245 			break;
246 		udelay(5000);	/* 5ms write cycle timing */
247 	}
248 
249 	if (!ret) {
250 		/* Verify the write by reading back the EEPROM and comparing */
251 		struct eeprom e2;
252 
253 		ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
254 			CONFIG_SYS_I2C_EEPROM_ADDR_LEN, (void *)&e2, sizeof(e2));
255 		if (!ret && memcmp(&e, &e2, sizeof(e)))
256 			ret = -1;
257 	}
258 
259 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
260 	i2c_set_bus_num(bus);
261 #endif
262 
263 	if (ret) {
264 		printf("Programming failed.\n");
265 		has_been_read = 0;
266 		return -1;
267 	}
268 
269 	printf("Programming passed.\n");
270 	return 0;
271 }
272 
273 /**
274  * h2i - converts hex character into a number
275  *
276  * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
277  * the integer equivalent.
278  */
279 static inline u8 h2i(char p)
280 {
281 	if ((p >= '0') && (p <= '9'))
282 		return p - '0';
283 
284 	if ((p >= 'A') && (p <= 'F'))
285 		return (p - 'A') + 10;
286 
287 	if ((p >= 'a') && (p <= 'f'))
288 		return (p - 'a') + 10;
289 
290 	return 0;
291 }
292 
293 /**
294  * set_date - stores the build date into the EEPROM
295  *
296  * This function takes a pointer to a string in the format "YYMMDDhhmmss"
297  * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
298  * and stores it in the build date field of the EEPROM local copy.
299  */
300 static void set_date(const char *string)
301 {
302 	unsigned int i;
303 
304 	if (strlen(string) != 12) {
305 		printf("Usage: mac date YYMMDDhhmmss\n");
306 		return;
307 	}
308 
309 	for (i = 0; i < 6; i++)
310 		e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
311 
312 	update_crc();
313 }
314 
315 /**
316  * set_mac_address - stores a MAC address into the EEPROM
317  *
318  * This function takes a pointer to MAC address string
319  * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
320  * stores it in one of the MAC address fields of the EEPROM local copy.
321  */
322 static void set_mac_address(unsigned int index, const char *string)
323 {
324 	char *p = (char *) string;
325 	unsigned int i;
326 
327 	if ((index >= MAX_NUM_PORTS) || !string) {
328 		printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
329 		return;
330 	}
331 
332 	for (i = 0; *p && (i < 6); i++) {
333 		e.mac[index][i] = simple_strtoul(p, &p, 16);
334 		if (*p == ':')
335 			p++;
336 	}
337 
338 	update_crc();
339 }
340 
341 int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
342 {
343 	char cmd;
344 
345 	if (argc == 1) {
346 		show_eeprom();
347 		return 0;
348 	}
349 
350 	cmd = argv[1][0];
351 
352 	if (cmd == 'r') {
353 		read_eeprom();
354 		return 0;
355 	}
356 
357 	if (cmd == 'i') {
358 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
359 		memcpy(e.id, "NXID", sizeof(e.id));
360 		e.version = NXID_VERSION;
361 #else
362 		memcpy(e.id, "CCID", sizeof(e.id));
363 #endif
364 		return 0;
365 	}
366 
367 	if (!is_valid) {
368 		printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
369 		return 0;
370 	}
371 
372 	if (argc == 2) {
373 		switch (cmd) {
374 		case 's':	/* save */
375 			prog_eeprom();
376 			break;
377 		default:
378 			return cmd_usage(cmdtp);
379 		}
380 
381 		return 0;
382 	}
383 
384 	/* We know we have at least one parameter  */
385 
386 	switch (cmd) {
387 	case 'n':	/* serial number */
388 		memset(e.sn, 0, sizeof(e.sn));
389 		strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
390 		update_crc();
391 		break;
392 	case 'e':	/* errata */
393 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
394 		memset(e.errata, 0, 5);
395 		strncpy((char *)e.errata, argv[2], 4);
396 #else
397 		e.errata[0] = argv[2][0];
398 		e.errata[1] = argv[2][1];
399 #endif
400 		update_crc();
401 		break;
402 	case 'd':	/* date BCD format YYMMDDhhmmss */
403 		set_date(argv[2]);
404 		break;
405 	case 'p':	/* MAC table size */
406 		e.mac_count = simple_strtoul(argv[2], NULL, 16);
407 		update_crc();
408 		break;
409 	case '0' ... '9':	/* "mac 0" through "mac 22" */
410 		set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
411 		break;
412 	case 'h':	/* help */
413 	default:
414 		return cmd_usage(cmdtp);
415 	}
416 
417 	return 0;
418 }
419 
420 /**
421  * mac_read_from_eeprom - read the MAC addresses from EEPROM
422  *
423  * This function reads the MAC addresses from EEPROM and sets the
424  * appropriate environment variables for each one read.
425  *
426  * The environment variables are only set if they haven't been set already.
427  * This ensures that any user-saved variables are never overwritten.
428  *
429  * This function must be called after relocation.
430  */
431 int mac_read_from_eeprom(void)
432 {
433 	unsigned int i;
434 	u32 crc;
435 
436 	puts("EEPROM: ");
437 
438 	if (read_eeprom()) {
439 		printf("Read failed.\n");
440 		return -1;
441 	}
442 
443 	if (!is_valid) {
444 		printf("Invalid ID (%02x %02x %02x %02x)\n",
445 		       e.id[0], e.id[1], e.id[2], e.id[3]);
446 		return -1;
447 	}
448 
449 	crc = crc32(0, (void *)&e, sizeof(e) - 4);
450 	if (crc != be32_to_cpu(e.crc)) {
451 		printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
452 		return -1;
453 	}
454 
455 	for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
456 		if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
457 		    memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
458 			char ethaddr[18];
459 			char enetvar[9];
460 
461 			sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
462 				e.mac[i][0],
463 				e.mac[i][1],
464 				e.mac[i][2],
465 				e.mac[i][3],
466 				e.mac[i][4],
467 				e.mac[i][5]);
468 			sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
469 			/* Only initialize environment variables that are blank
470 			 * (i.e. have not yet been set)
471 			 */
472 			if (!getenv(enetvar))
473 				setenv(enetvar, ethaddr);
474 		}
475 	}
476 
477 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
478 	printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
479 		be32_to_cpu(e.version));
480 #else
481 	printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
482 #endif
483 
484 	return 0;
485 }
486 
487 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
488 
489 /**
490  * get_cpu_board_revision - get the CPU board revision on 85xx boards
491  *
492  * Read the EEPROM to determine the board revision.
493  *
494  * This function is called before relocation, so we need to read a private
495  * copy of the EEPROM into a local variable on the stack.
496  *
497  * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM.  The global
498  * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM,
499  * so that the SPD code will work.  This means that all pre-relocation I2C
500  * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus.  So if
501  * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when
502  * this function is called.  Oh well.
503  */
504 unsigned int get_cpu_board_revision(void)
505 {
506 	struct board_eeprom {
507 		u32 id;           /* 0x00 - 0x03 EEPROM Tag 'CCID' */
508 		u8 major;         /* 0x04        Board revision, major */
509 		u8 minor;         /* 0x05        Board revision, minor */
510 	} be;
511 
512 	i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
513 		(void *)&be, sizeof(be));
514 
515 	if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
516 		return MPC85XX_CPU_BOARD_REV(0, 0);
517 
518 	if ((be.major == 0xff) && (be.minor == 0xff))
519 		return MPC85XX_CPU_BOARD_REV(0, 0);
520 
521 	return MPC85XX_CPU_BOARD_REV(be.major, be.minor);
522 }
523 #endif
524