]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
bea3f28d | 2 | /* |
f098c9c8 | 3 | * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor |
bea3f28d HW |
4 | * York Sun ([email protected]) |
5 | * Haiying Wang ([email protected]) | |
e2d31fb3 | 6 | * Timur Tabi ([email protected]) |
bea3f28d HW |
7 | */ |
8 | ||
9 | #include <common.h> | |
10 | #include <command.h> | |
9fb625ce | 11 | #include <env.h> |
bea3f28d | 12 | #include <i2c.h> |
691d719d | 13 | #include <init.h> |
bea3f28d | 14 | #include <linux/ctype.h> |
c05ed00a | 15 | #include <linux/delay.h> |
3db71108 | 16 | #include <u-boot/crc.h> |
bea3f28d | 17 | |
bfb70719 | 18 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
e2d31fb3 | 19 | #include "../common/eeprom.h" |
bfb70719 TT |
20 | #define MAX_NUM_PORTS 8 |
21 | #endif | |
e2d31fb3 | 22 | |
bfb70719 | 23 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
5536aeb0 SL |
24 | /* some boards with non-256-bytes EEPROM have special define */ |
25 | /* for MAX_NUM_PORTS in board-specific file */ | |
26 | #ifndef MAX_NUM_PORTS | |
477c894f | 27 | #define MAX_NUM_PORTS 16 |
5536aeb0 | 28 | #endif |
bfb70719 TT |
29 | #define NXID_VERSION 1 |
30 | #endif | |
9a611089 | 31 | |
e2d31fb3 TT |
32 | /** |
33 | * static eeprom: EEPROM layout for CCID or NXID formats | |
34 | * | |
35 | * See application note AN3638 for details. | |
36 | */ | |
37 | static struct __attribute__ ((__packed__)) eeprom { | |
6d0f6bcf | 38 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
e2d31fb3 TT |
39 | u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'CCID' */ |
40 | u8 major; /* 0x04 Board revision, major */ | |
41 | u8 minor; /* 0x05 Board revision, minor */ | |
42 | u8 sn[10]; /* 0x06 - 0x0F Serial Number*/ | |
43 | u8 errata[2]; /* 0x10 - 0x11 Errata Level */ | |
44 | u8 date[6]; /* 0x12 - 0x17 Build Date */ | |
45 | u8 res_0[40]; /* 0x18 - 0x3f Reserved */ | |
46 | u8 mac_count; /* 0x40 Number of MAC addresses */ | |
47 | u8 mac_flag; /* 0x41 MAC table flags */ | |
9a611089 | 48 | u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0x71 MAC addresses */ |
e2d31fb3 TT |
49 | u32 crc; /* 0x72 CRC32 checksum */ |
50 | #endif | |
6d0f6bcf | 51 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
e2d31fb3 TT |
52 | u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */ |
53 | u8 sn[12]; /* 0x04 - 0x0F Serial Number */ | |
54 | u8 errata[5]; /* 0x10 - 0x14 Errata Level */ | |
55 | u8 date[6]; /* 0x15 - 0x1a Build Date */ | |
56 | u8 res_0; /* 0x1b Reserved */ | |
57 | u32 version; /* 0x1c - 0x1f NXID Version */ | |
58 | u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */ | |
59 | u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */ | |
60 | u8 tempcalflags; /* 0x2a Temperature Calibration Flags */ | |
61 | u8 res_1[21]; /* 0x2b - 0x3f Reserved */ | |
62 | u8 mac_count; /* 0x40 Number of MAC addresses */ | |
63 | u8 mac_flag; /* 0x41 MAC table flags */ | |
477c894f | 64 | u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0xa1 MAC addresses */ |
0a50b3c9 | 65 | u8 res_2[90]; /* 0xa2 - 0xfb Reserved */ |
477c894f | 66 | u32 crc; /* 0xfc - 0xff CRC32 checksum */ |
e2d31fb3 TT |
67 | #endif |
68 | } e; | |
69 | ||
70 | /* Set to 1 if we've read EEPROM into memory */ | |
71 | static int has_been_read = 0; | |
72 | ||
6d0f6bcf | 73 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
e2d31fb3 | 74 | /* Is this a valid NXID EEPROM? */ |
afb0b131 KG |
75 | #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \ |
76 | (e.id[2] == 'I') || (e.id[3] == 'D')) | |
e2d31fb3 TT |
77 | #endif |
78 | ||
6d0f6bcf | 79 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
e2d31fb3 | 80 | /* Is this a valid CCID EEPROM? */ |
afb0b131 KG |
81 | #define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \ |
82 | (e.id[2] == 'I') || (e.id[3] == 'D')) | |
e2d31fb3 TT |
83 | #endif |
84 | ||
85 | /** | |
86 | * show_eeprom - display the contents of the EEPROM | |
87 | */ | |
88 | static void show_eeprom(void) | |
bea3f28d HW |
89 | { |
90 | int i; | |
e2d31fb3 TT |
91 | unsigned int crc; |
92 | ||
93 | /* EEPROM tag ID, either CCID or NXID */ | |
6d0f6bcf | 94 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
e2d31fb3 | 95 | printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3], |
b8baf460 | 96 | be32_to_cpu(e.version)); |
e2d31fb3 TT |
97 | #else |
98 | printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]); | |
99 | #endif | |
100 | ||
101 | /* Serial number */ | |
102 | printf("SN: %s\n", e.sn); | |
103 | ||
104 | /* Errata level. */ | |
6d0f6bcf | 105 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
e2d31fb3 TT |
106 | printf("Errata: %s\n", e.errata); |
107 | #else | |
108 | printf("Errata: %c%c\n", | |
109 | e.errata[0] ? e.errata[0] : '.', | |
110 | e.errata[1] ? e.errata[1] : '.'); | |
111 | #endif | |
112 | ||
113 | /* Build date, BCD date values, as YYMMDDhhmmss */ | |
114 | printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n", | |
115 | e.date[0], e.date[1], e.date[2], | |
116 | e.date[3] & 0x7F, e.date[4], e.date[5], | |
117 | e.date[3] & 0x80 ? "PM" : ""); | |
118 | ||
119 | /* Show MAC addresses */ | |
b4141195 | 120 | for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) { |
9a611089 | 121 | |
e2d31fb3 | 122 | u8 *p = e.mac[i]; |
d59feffb | 123 | |
e2d31fb3 TT |
124 | printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i, |
125 | p[0], p[1], p[2], p[3], p[4], p[5]); | |
d59feffb | 126 | } |
bea3f28d | 127 | |
e2d31fb3 TT |
128 | crc = crc32(0, (void *)&e, sizeof(e) - 4); |
129 | ||
130 | if (crc == be32_to_cpu(e.crc)) | |
131 | printf("CRC: %08x\n", be32_to_cpu(e.crc)); | |
132 | else | |
133 | printf("CRC: %08x (should be %08x)\n", | |
134 | be32_to_cpu(e.crc), crc); | |
135 | ||
136 | #ifdef DEBUG | |
137 | printf("EEPROM dump: (0x%x bytes)\n", sizeof(e)); | |
138 | for (i = 0; i < sizeof(e); i++) { | |
139 | if ((i % 16) == 0) | |
140 | printf("%02X: ", i); | |
141 | printf("%02X ", ((u8 *)&e)[i]); | |
142 | if (((i % 16) == 15) || (i == sizeof(e) - 1)) | |
143 | printf("\n"); | |
144 | } | |
145 | #endif | |
bea3f28d HW |
146 | } |
147 | ||
e2d31fb3 TT |
148 | /** |
149 | * read_eeprom - read the EEPROM into memory | |
150 | */ | |
151 | static int read_eeprom(void) | |
bea3f28d | 152 | { |
e2d31fb3 | 153 | int ret; |
6d0f6bcf | 154 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
2147a169 | 155 | #if !CONFIG_IS_ENABLED(DM_I2C) |
e2d31fb3 | 156 | unsigned int bus; |
0eba65d2 | 157 | #endif |
e2d31fb3 | 158 | #endif |
bea3f28d | 159 | |
e2d31fb3 TT |
160 | if (has_been_read) |
161 | return 0; | |
bea3f28d | 162 | |
6d0f6bcf | 163 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
2147a169 | 164 | #if !CONFIG_IS_ENABLED(DM_I2C) |
e2d31fb3 | 165 | bus = i2c_get_bus_num(); |
6d0f6bcf | 166 | i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM); |
e2d31fb3 | 167 | #endif |
0eba65d2 | 168 | #endif |
e2d31fb3 | 169 | |
2147a169 | 170 | #if !CONFIG_IS_ENABLED(DM_I2C) |
0eba65d2 CH |
171 | ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, |
172 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, | |
173 | (void *)&e, sizeof(e)); | |
174 | #else | |
175 | struct udevice *dev; | |
176 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM | |
177 | ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM, | |
e343ae7c BL |
178 | CONFIG_SYS_I2C_EEPROM_ADDR, |
179 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, &dev); | |
0eba65d2 | 180 | #else |
e343ae7c BL |
181 | ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR, |
182 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, &dev); | |
0eba65d2 CH |
183 | #endif |
184 | if (!ret) | |
185 | ret = dm_i2c_read(dev, 0, (void *)&e, sizeof(e)); | |
186 | #endif | |
e2d31fb3 | 187 | |
6d0f6bcf | 188 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
2147a169 | 189 | #if !CONFIG_IS_ENABLED(DM_I2C) |
e2d31fb3 TT |
190 | i2c_set_bus_num(bus); |
191 | #endif | |
0eba65d2 | 192 | #endif |
e2d31fb3 TT |
193 | |
194 | #ifdef DEBUG | |
195 | show_eeprom(); | |
196 | #endif | |
197 | ||
198 | has_been_read = (ret == 0) ? 1 : 0; | |
199 | ||
200 | return ret; | |
bea3f28d HW |
201 | } |
202 | ||
2d04db08 TT |
203 | /** |
204 | * update_crc - update the CRC | |
205 | * | |
206 | * This function should be called after each update to the EEPROM structure, | |
207 | * to make sure the CRC is always correct. | |
208 | */ | |
209 | static void update_crc(void) | |
210 | { | |
211 | u32 crc; | |
212 | ||
213 | crc = crc32(0, (void *)&e, sizeof(e) - 4); | |
214 | e.crc = cpu_to_be32(crc); | |
215 | } | |
216 | ||
e2d31fb3 TT |
217 | /** |
218 | * prog_eeprom - write the EEPROM from memory | |
219 | */ | |
220 | static int prog_eeprom(void) | |
bea3f28d | 221 | { |
3addcb93 | 222 | int ret = 0; |
9c671e70 | 223 | int i; |
e2d31fb3 | 224 | void *p; |
6d0f6bcf | 225 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
2147a169 | 226 | #if !CONFIG_IS_ENABLED(DM_I2C) |
e2d31fb3 | 227 | unsigned int bus; |
0eba65d2 | 228 | #endif |
e2d31fb3 TT |
229 | #endif |
230 | ||
231 | /* Set the reserved values to 0xFF */ | |
6d0f6bcf | 232 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
e2d31fb3 TT |
233 | e.res_0 = 0xFF; |
234 | memset(e.res_1, 0xFF, sizeof(e.res_1)); | |
235 | #else | |
236 | memset(e.res_0, 0xFF, sizeof(e.res_0)); | |
237 | #endif | |
2d04db08 | 238 | update_crc(); |
e2d31fb3 | 239 | |
2147a169 | 240 | #if !CONFIG_IS_ENABLED(DM_I2C) |
6d0f6bcf | 241 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
e2d31fb3 | 242 | bus = i2c_get_bus_num(); |
6d0f6bcf | 243 | i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM); |
0eba65d2 | 244 | #endif |
e2d31fb3 TT |
245 | #endif |
246 | ||
3addcb93 TT |
247 | /* |
248 | * The AT24C02 datasheet says that data can only be written in page | |
249 | * mode, which means 8 bytes at a time, and it takes up to 5ms to | |
250 | * complete a given write. | |
251 | */ | |
2d04db08 | 252 | for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) { |
2147a169 | 253 | #if !CONFIG_IS_ENABLED(DM_I2C) |
0eba65d2 CH |
254 | ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, |
255 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, | |
b4141195 | 256 | p, min((int)(sizeof(e) - i), 8)); |
0eba65d2 CH |
257 | #else |
258 | struct udevice *dev; | |
259 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM | |
260 | ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM, | |
261 | CONFIG_SYS_I2C_EEPROM_ADDR, | |
262 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, | |
263 | &dev); | |
264 | #else | |
265 | ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR, | |
266 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, | |
267 | &dev); | |
268 | #endif | |
269 | if (!ret) | |
270 | ret = dm_i2c_write(dev, i, p, min((int)(sizeof(e) - i), | |
271 | 8)); | |
272 | #endif | |
bea3f28d HW |
273 | if (ret) |
274 | break; | |
e2d31fb3 | 275 | udelay(5000); /* 5ms write cycle timing */ |
bea3f28d | 276 | } |
e2d31fb3 | 277 | |
3addcb93 TT |
278 | if (!ret) { |
279 | /* Verify the write by reading back the EEPROM and comparing */ | |
280 | struct eeprom e2; | |
281 | ||
2147a169 | 282 | #if !CONFIG_IS_ENABLED(DM_I2C) |
3addcb93 | 283 | ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, |
0eba65d2 CH |
284 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
285 | (void *)&e2, sizeof(e2)); | |
286 | #else | |
287 | struct udevice *dev; | |
288 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM | |
289 | ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM, | |
290 | CONFIG_SYS_I2C_EEPROM_ADDR, | |
291 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, | |
292 | &dev); | |
293 | #else | |
294 | ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR, | |
295 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, | |
296 | &dev); | |
297 | #endif | |
298 | if (!ret) | |
299 | ret = dm_i2c_read(dev, 0, (void *)&e2, sizeof(e2)); | |
300 | #endif | |
3addcb93 TT |
301 | if (!ret && memcmp(&e, &e2, sizeof(e))) |
302 | ret = -1; | |
303 | } | |
304 | ||
2147a169 | 305 | #if !CONFIG_IS_ENABLED(DM_I2C) |
6d0f6bcf | 306 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
e2d31fb3 | 307 | i2c_set_bus_num(bus); |
0eba65d2 | 308 | #endif |
e2d31fb3 TT |
309 | #endif |
310 | ||
bea3f28d HW |
311 | if (ret) { |
312 | printf("Programming failed.\n"); | |
3addcb93 | 313 | has_been_read = 0; |
bea3f28d | 314 | return -1; |
bea3f28d | 315 | } |
e2d31fb3 TT |
316 | |
317 | printf("Programming passed.\n"); | |
bea3f28d HW |
318 | return 0; |
319 | } | |
320 | ||
e2d31fb3 TT |
321 | /** |
322 | * h2i - converts hex character into a number | |
323 | * | |
324 | * This function takes a hexadecimal character (e.g. '7' or 'C') and returns | |
325 | * the integer equivalent. | |
326 | */ | |
327 | static inline u8 h2i(char p) | |
328 | { | |
329 | if ((p >= '0') && (p <= '9')) | |
330 | return p - '0'; | |
331 | ||
332 | if ((p >= 'A') && (p <= 'F')) | |
333 | return (p - 'A') + 10; | |
334 | ||
335 | if ((p >= 'a') && (p <= 'f')) | |
336 | return (p - 'a') + 10; | |
337 | ||
338 | return 0; | |
339 | } | |
340 | ||
341 | /** | |
342 | * set_date - stores the build date into the EEPROM | |
343 | * | |
344 | * This function takes a pointer to a string in the format "YYMMDDhhmmss" | |
345 | * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string, | |
346 | * and stores it in the build date field of the EEPROM local copy. | |
347 | */ | |
348 | static void set_date(const char *string) | |
349 | { | |
350 | unsigned int i; | |
351 | ||
352 | if (strlen(string) != 12) { | |
353 | printf("Usage: mac date YYMMDDhhmmss\n"); | |
354 | return; | |
355 | } | |
356 | ||
357 | for (i = 0; i < 6; i++) | |
358 | e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]); | |
2d04db08 TT |
359 | |
360 | update_crc(); | |
e2d31fb3 TT |
361 | } |
362 | ||
363 | /** | |
364 | * set_mac_address - stores a MAC address into the EEPROM | |
365 | * | |
366 | * This function takes a pointer to MAC address string | |
367 | * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and | |
368 | * stores it in one of the MAC address fields of the EEPROM local copy. | |
369 | */ | |
370 | static void set_mac_address(unsigned int index, const char *string) | |
371 | { | |
372 | char *p = (char *) string; | |
373 | unsigned int i; | |
374 | ||
bfb70719 | 375 | if ((index >= MAX_NUM_PORTS) || !string) { |
e2d31fb3 TT |
376 | printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n"); |
377 | return; | |
378 | } | |
379 | ||
380 | for (i = 0; *p && (i < 6); i++) { | |
7e5f460e | 381 | e.mac[index][i] = hextoul(p, &p); |
e2d31fb3 TT |
382 | if (*p == ':') |
383 | p++; | |
384 | } | |
2d04db08 TT |
385 | |
386 | update_crc(); | |
e2d31fb3 TT |
387 | } |
388 | ||
09140113 | 389 | int do_mac(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
bea3f28d | 390 | { |
e2d31fb3 | 391 | char cmd; |
bea3f28d | 392 | |
e2d31fb3 TT |
393 | if (argc == 1) { |
394 | show_eeprom(); | |
395 | return 0; | |
396 | } | |
397 | ||
398 | cmd = argv[1][0]; | |
399 | ||
400 | if (cmd == 'r') { | |
401 | read_eeprom(); | |
402 | return 0; | |
403 | } | |
bea3f28d | 404 | |
2d04db08 TT |
405 | if (cmd == 'i') { |
406 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID | |
407 | memcpy(e.id, "NXID", sizeof(e.id)); | |
b8baf460 | 408 | e.version = cpu_to_be32(NXID_VERSION); |
2d04db08 TT |
409 | #else |
410 | memcpy(e.id, "CCID", sizeof(e.id)); | |
411 | #endif | |
3fee334c | 412 | update_crc(); |
e2d31fb3 TT |
413 | return 0; |
414 | } | |
415 | ||
416 | if (!is_valid) { | |
417 | printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n"); | |
418 | return 0; | |
419 | } | |
420 | ||
421 | if (argc == 2) { | |
bea3f28d | 422 | switch (cmd) { |
80e955c7 | 423 | case 's': /* save */ |
e2d31fb3 | 424 | prog_eeprom(); |
80e955c7 | 425 | break; |
80e955c7 | 426 | default: |
f5e921e9 | 427 | return CMD_RET_USAGE; |
bea3f28d | 428 | } |
e2d31fb3 TT |
429 | |
430 | return 0; | |
431 | } | |
432 | ||
433 | /* We know we have at least one parameter */ | |
434 | ||
435 | switch (cmd) { | |
436 | case 'n': /* serial number */ | |
437 | memset(e.sn, 0, sizeof(e.sn)); | |
438 | strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1); | |
2d04db08 | 439 | update_crc(); |
e2d31fb3 TT |
440 | break; |
441 | case 'e': /* errata */ | |
6d0f6bcf | 442 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
e2d31fb3 TT |
443 | memset(e.errata, 0, 5); |
444 | strncpy((char *)e.errata, argv[2], 4); | |
445 | #else | |
446 | e.errata[0] = argv[2][0]; | |
447 | e.errata[1] = argv[2][1]; | |
448 | #endif | |
2d04db08 | 449 | update_crc(); |
e2d31fb3 TT |
450 | break; |
451 | case 'd': /* date BCD format YYMMDDhhmmss */ | |
452 | set_date(argv[2]); | |
453 | break; | |
454 | case 'p': /* MAC table size */ | |
7e5f460e | 455 | e.mac_count = hextoul(argv[2], NULL); |
2d04db08 | 456 | update_crc(); |
e2d31fb3 | 457 | break; |
bfb70719 | 458 | case '0' ... '9': /* "mac 0" through "mac 22" */ |
0b1284eb | 459 | set_mac_address(dectoul(argv[1], NULL), argv[2]); |
e2d31fb3 TT |
460 | break; |
461 | case 'h': /* help */ | |
462 | default: | |
47e26b1b | 463 | return cmd_usage(cmdtp); |
bea3f28d | 464 | } |
e2d31fb3 | 465 | |
bea3f28d HW |
466 | return 0; |
467 | } | |
468 | ||
e2d31fb3 TT |
469 | /** |
470 | * mac_read_from_eeprom - read the MAC addresses from EEPROM | |
471 | * | |
472 | * This function reads the MAC addresses from EEPROM and sets the | |
473 | * appropriate environment variables for each one read. | |
474 | * | |
475 | * The environment variables are only set if they haven't been set already. | |
476 | * This ensures that any user-saved variables are never overwritten. | |
477 | * | |
478 | * This function must be called after relocation. | |
f098c9c8 TT |
479 | * |
480 | * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0 | |
481 | * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is | |
482 | * located at a different offset. | |
e2d31fb3 | 483 | */ |
bea3f28d HW |
484 | int mac_read_from_eeprom(void) |
485 | { | |
e2d31fb3 | 486 | unsigned int i; |
f098c9c8 TT |
487 | u32 crc, crc_offset = offsetof(struct eeprom, crc); |
488 | u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */ | |
2d04db08 TT |
489 | |
490 | puts("EEPROM: "); | |
e2d31fb3 TT |
491 | |
492 | if (read_eeprom()) { | |
bea3f28d | 493 | printf("Read failed.\n"); |
bffac7ae | 494 | return 0; |
bea3f28d HW |
495 | } |
496 | ||
e2d31fb3 | 497 | if (!is_valid) { |
2d04db08 TT |
498 | printf("Invalid ID (%02x %02x %02x %02x)\n", |
499 | e.id[0], e.id[1], e.id[2], e.id[3]); | |
bffac7ae | 500 | return 0; |
e2d31fb3 TT |
501 | } |
502 | ||
f098c9c8 TT |
503 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
504 | /* | |
505 | * If we've read an NXID v0 EEPROM, then we need to set the CRC offset | |
506 | * to where it is in v0. | |
507 | */ | |
508 | if (e.version == 0) | |
509 | crc_offset = 0x72; | |
510 | #endif | |
511 | ||
512 | crc = crc32(0, (void *)&e, crc_offset); | |
513 | crcp = (void *)&e + crc_offset; | |
514 | if (crc != be32_to_cpu(*crcp)) { | |
2d04db08 | 515 | printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc)); |
bffac7ae | 516 | return 0; |
9a611089 HW |
517 | } |
518 | ||
f098c9c8 TT |
519 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
520 | /* | |
521 | * MAC address #9 in v1 occupies the same position as the CRC in v0. | |
522 | * Erase it so that it's not mistaken for a MAC address. We'll | |
523 | * update the CRC later. | |
524 | */ | |
525 | if (e.version == 0) | |
526 | memset(e.mac[8], 0xff, 6); | |
527 | #endif | |
528 | ||
b4141195 | 529 | for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) { |
e2d31fb3 TT |
530 | if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) && |
531 | memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) { | |
532 | char ethaddr[18]; | |
533 | char enetvar[9]; | |
534 | ||
535 | sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X", | |
536 | e.mac[i][0], | |
537 | e.mac[i][1], | |
538 | e.mac[i][2], | |
539 | e.mac[i][3], | |
540 | e.mac[i][4], | |
541 | e.mac[i][5]); | |
542 | sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i); | |
543 | /* Only initialize environment variables that are blank | |
544 | * (i.e. have not yet been set) | |
545 | */ | |
00caae6d | 546 | if (!env_get(enetvar)) |
382bee57 | 547 | env_set(enetvar, ethaddr); |
e2d31fb3 TT |
548 | } |
549 | } | |
550 | ||
2d04db08 TT |
551 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
552 | printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3], | |
b8baf460 | 553 | be32_to_cpu(e.version)); |
2d04db08 TT |
554 | #else |
555 | printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]); | |
556 | #endif | |
557 | ||
f098c9c8 TT |
558 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
559 | /* | |
560 | * Now we need to upconvert the data into v1 format. We do this last so | |
561 | * that at boot time, U-Boot will still say "NXID v0". | |
562 | */ | |
563 | if (e.version == 0) { | |
b8baf460 | 564 | e.version = cpu_to_be32(NXID_VERSION); |
f098c9c8 TT |
565 | update_crc(); |
566 | } | |
567 | #endif | |
568 | ||
bea3f28d HW |
569 | return 0; |
570 | } | |
e2d31fb3 | 571 | |
6d0f6bcf | 572 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
e2d31fb3 TT |
573 | |
574 | /** | |
575 | * get_cpu_board_revision - get the CPU board revision on 85xx boards | |
576 | * | |
577 | * Read the EEPROM to determine the board revision. | |
578 | * | |
579 | * This function is called before relocation, so we need to read a private | |
580 | * copy of the EEPROM into a local variable on the stack. | |
581 | * | |
6d0f6bcf JCPV |
582 | * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global |
583 | * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM, | |
e2d31fb3 | 584 | * so that the SPD code will work. This means that all pre-relocation I2C |
6d0f6bcf JCPV |
585 | * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if |
586 | * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when | |
e2d31fb3 TT |
587 | * this function is called. Oh well. |
588 | */ | |
589 | unsigned int get_cpu_board_revision(void) | |
590 | { | |
591 | struct board_eeprom { | |
592 | u32 id; /* 0x00 - 0x03 EEPROM Tag 'CCID' */ | |
593 | u8 major; /* 0x04 Board revision, major */ | |
594 | u8 minor; /* 0x05 Board revision, minor */ | |
595 | } be; | |
596 | ||
2147a169 | 597 | #if !CONFIG_IS_ENABLED(DM_I2C) |
6d0f6bcf | 598 | i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
e2d31fb3 | 599 | (void *)&be, sizeof(be)); |
0eba65d2 CH |
600 | #else |
601 | struct udevice *dev; | |
01d97d5f | 602 | int ret; |
0eba65d2 CH |
603 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
604 | ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM, | |
605 | CONFIG_SYS_I2C_EEPROM_ADDR, | |
606 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, | |
607 | &dev); | |
608 | #else | |
609 | ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR, | |
610 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, | |
01d97d5f | 611 | &dev); |
0eba65d2 CH |
612 | #endif |
613 | if (!ret) | |
614 | dm_i2c_read(dev, 0, (void *)&be, sizeof(be)); | |
615 | #endif | |
e2d31fb3 TT |
616 | |
617 | if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D')) | |
618 | return MPC85XX_CPU_BOARD_REV(0, 0); | |
619 | ||
620 | if ((be.major == 0xff) && (be.minor == 0xff)) | |
621 | return MPC85XX_CPU_BOARD_REV(0, 0); | |
622 | ||
e46c7bfb | 623 | return MPC85XX_CPU_BOARD_REV(be.major, be.minor); |
e2d31fb3 | 624 | } |
e2d31fb3 | 625 | #endif |
8780cd82 | 626 | |
3616218b | 627 | U_BOOT_LONGHELP(mac, |
8780cd82 HS |
628 | "[read|save|id|num|errata|date|ports|port_number]\n" |
629 | "mac read\n" | |
630 | " - read EEPROM content into memory data structure\n" | |
631 | "mac save\n" | |
632 | " - save memory data structure to the EEPROM\n" | |
633 | "mac id\n" | |
634 | " - program system id per hard coded value\n" | |
635 | "mac num string\n" | |
636 | " - program system serial number to value string\n" | |
637 | "mac errata string\n" | |
638 | " - program errata data to value string\n" | |
639 | "mac date YYMMDDhhmmss\n" | |
640 | " - program date to string value YYMMDDhhmmss\n" | |
641 | "mac ports N\n" | |
642 | " - program the number of network ports to integer N\n" | |
643 | "mac X string\n" | |
3616218b | 644 | " - program MAC addr for port X [X=0,1..] to colon separated string"); |
f5e921e9 HS |
645 | |
646 | U_BOOT_CMD( | |
647 | mac, 3, 1, do_mac, | |
648 | "display and program the system ID and MAC addresses in EEPROM", | |
3616218b | 649 | mac_help_text); |