Michael Jones | 8ce85da | 2011-11-04 13:53:44 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006, 2008-2009, 2011 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 | |
| 30 | /* #define DEBUG */ |
| 31 | |
| 32 | /* |
| 33 | * static eeprom: EEPROM layout |
| 34 | */ |
| 35 | static struct __attribute__ ((__packed__)) eeprom { |
| 36 | u8 id[16]; /* 0x01 - 0x0F Type e.g. 100wG-5111 */ |
| 37 | u8 sn[10]; /* 0x10 - 0x19 Serial Number */ |
| 38 | u8 date[6]; /* 0x1A - 0x1F Build Date */ |
| 39 | u8 mac[6]; /* 0x20 - 0x25 MAC address */ |
| 40 | u8 reserved[10];/* 0x26 - 0x2f reserved */ |
| 41 | u32 crc; /* x+1 CRC32 checksum */ |
| 42 | } e; |
| 43 | |
| 44 | /* Set to 1 if we've read EEPROM into memory */ |
| 45 | static int has_been_read; |
| 46 | |
| 47 | /** |
| 48 | * show_eeprom - display the contents of the EEPROM |
| 49 | */ |
| 50 | static void show_eeprom(void) |
| 51 | { |
| 52 | unsigned int crc; |
| 53 | char safe_string[16]; |
| 54 | |
| 55 | #ifdef DEBUG |
| 56 | int i; |
| 57 | #endif |
| 58 | u8 *p; |
| 59 | |
| 60 | /* ID */ |
| 61 | strncpy(safe_string, (char *)e.id, sizeof(e.id)); |
| 62 | safe_string[sizeof(e.id)-1] = 0; |
| 63 | printf("ID: mvBlueLYNX-X%s\n", safe_string); |
| 64 | |
| 65 | /* Serial number */ |
| 66 | strncpy(safe_string, (char *)e.sn, sizeof(e.sn)); |
| 67 | safe_string[sizeof(e.sn)-1] = 0; |
| 68 | printf("SN: %s\n", safe_string); |
| 69 | |
| 70 | /* Build date, BCD date values, as YYMMDDhhmmss */ |
| 71 | printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n", |
| 72 | e.date[0], e.date[1], e.date[2], |
| 73 | e.date[3] & 0x7F, e.date[4], e.date[5], |
| 74 | e.date[3] & 0x80 ? "PM" : ""); |
| 75 | |
| 76 | /* Show MAC address */ |
| 77 | p = e.mac; |
| 78 | printf("Eth: %02x:%02x:%02x:%02x:%02x:%02x\n", |
| 79 | p[0], p[1], p[2], p[3], p[4], p[5]); |
| 80 | |
| 81 | crc = crc32(0, (void *)&e, sizeof(e) - 4); |
| 82 | |
| 83 | if (crc == be32_to_cpu(e.crc)) |
| 84 | printf("CRC: %08x\n", be32_to_cpu(e.crc)); |
| 85 | else |
| 86 | printf("CRC: %08x (should be %08x)\n", be32_to_cpu(e.crc), crc); |
| 87 | |
| 88 | #ifdef DEBUG |
| 89 | printf("EEPROM dump: (0x%x bytes)\n", sizeof(e)); |
| 90 | for (i = 0; i < sizeof(e); i++) { |
| 91 | if ((i % 16) == 0) |
| 92 | printf("%02X: ", i); |
| 93 | printf("%02X ", ((u8 *)&e)[i]); |
| 94 | if (((i % 16) == 15) || (i == sizeof(e) - 1)) |
| 95 | printf("\n"); |
| 96 | } |
| 97 | #endif |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * read_eeprom - read the EEPROM into memory |
| 102 | */ |
| 103 | static int read_eeprom(void) |
| 104 | { |
| 105 | int ret; |
| 106 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
| 107 | unsigned int bus; |
| 108 | #endif |
| 109 | |
| 110 | if (has_been_read) |
| 111 | return 0; |
| 112 | |
| 113 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
| 114 | bus = i2c_get_bus_num(); |
| 115 | i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM); |
| 116 | #endif |
| 117 | |
| 118 | ret = eeprom_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, |
| 119 | (uchar *)&e, sizeof(e)); |
| 120 | |
| 121 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
| 122 | i2c_set_bus_num(bus); |
| 123 | #endif |
| 124 | |
| 125 | #ifdef DEBUG |
| 126 | show_eeprom(); |
| 127 | #endif |
| 128 | |
| 129 | has_been_read = (ret == 0) ? 1 : 0; |
| 130 | |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * update_crc - update the CRC |
| 136 | * |
| 137 | * This function should be called after each update to the EEPROM structure, |
| 138 | * to make sure the CRC is always correct. |
| 139 | */ |
| 140 | static void update_crc(void) |
| 141 | { |
| 142 | u32 crc; |
| 143 | |
| 144 | crc = crc32(0, (void *)&e, sizeof(e) - 4); |
| 145 | e.crc = cpu_to_be32(crc); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * prog_eeprom - write the EEPROM from memory |
| 150 | */ |
| 151 | static int prog_eeprom(void) |
| 152 | { |
| 153 | int ret = 0; |
| 154 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
| 155 | unsigned int bus; |
| 156 | #endif |
| 157 | |
| 158 | update_crc(); |
| 159 | |
| 160 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
| 161 | bus = i2c_get_bus_num(); |
| 162 | i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM); |
| 163 | #endif |
| 164 | |
| 165 | ret = eeprom_write(CONFIG_SYS_I2C_EEPROM_ADDR, 0, |
| 166 | (uchar *)&e, sizeof(e)); |
| 167 | |
| 168 | if (!ret) { |
| 169 | /* Verify the write by reading back the EEPROM and comparing */ |
| 170 | struct eeprom e2; |
| 171 | #ifdef DEBUG |
| 172 | printf("%s verifying...\n", __func__); |
| 173 | #endif |
| 174 | ret = eeprom_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, |
| 175 | (uchar *)&e2, sizeof(e2)); |
| 176 | |
| 177 | if (!ret && memcmp(&e, &e2, sizeof(e))) |
| 178 | ret = -1; |
| 179 | } |
| 180 | |
| 181 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
| 182 | i2c_set_bus_num(bus); |
| 183 | #endif |
| 184 | |
| 185 | if (ret) { |
| 186 | printf("Programming failed.\n"); |
| 187 | has_been_read = 0; |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | printf("Programming passed.\n"); |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * h2i - converts hex character into a number |
| 197 | * |
| 198 | * This function takes a hexadecimal character (e.g. '7' or 'C') and returns |
| 199 | * the integer equivalent. |
| 200 | */ |
| 201 | static inline u8 h2i(char p) |
| 202 | { |
| 203 | if ((p >= '0') && (p <= '9')) |
| 204 | return p - '0'; |
| 205 | |
| 206 | if ((p >= 'A') && (p <= 'F')) |
| 207 | return (p - 'A') + 10; |
| 208 | |
| 209 | if ((p >= 'a') && (p <= 'f')) |
| 210 | return (p - 'a') + 10; |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * set_date - stores the build date into the EEPROM |
| 217 | * |
| 218 | * This function takes a pointer to a string in the format "YYMMDDhhmmss" |
| 219 | * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string, |
| 220 | * and stores it in the build date field of the EEPROM local copy. |
| 221 | */ |
| 222 | static void set_date(const char *string) |
| 223 | { |
| 224 | unsigned int i; |
| 225 | |
| 226 | if (strlen(string) != 12) { |
| 227 | printf("Usage: mac date YYMMDDhhmmss\n"); |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | for (i = 0; i < 6; i++) |
| 232 | e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]); |
| 233 | |
| 234 | update_crc(); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * set_mac_address - stores a MAC address into the EEPROM |
| 239 | * |
| 240 | * This function takes a pointer to MAC address string |
| 241 | * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and |
| 242 | * stores it in the MAC address field in the EEPROM local copy. |
| 243 | */ |
| 244 | static void set_mac_address(const char *string) |
| 245 | { |
| 246 | char *p = (char *) string; |
| 247 | unsigned int i; |
| 248 | |
| 249 | for (i = 0; *p && (i < 6); i++) { |
| 250 | e.mac[i] = simple_strtoul(p, &p, 16); |
| 251 | if (*p == ':') |
| 252 | p++; |
| 253 | } |
| 254 | |
| 255 | update_crc(); |
| 256 | } |
| 257 | |
| 258 | int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 259 | { |
| 260 | char cmd; |
| 261 | |
| 262 | if (argc == 1) { |
| 263 | show_eeprom(); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | cmd = argv[1][0]; |
| 268 | |
| 269 | if (cmd == 'r') { |
| 270 | #ifdef DEBUG |
| 271 | printf("%s read\n", __func__); |
| 272 | #endif |
| 273 | read_eeprom(); |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | if (argc == 2) { |
| 278 | switch (cmd) { |
| 279 | case 's': /* save */ |
| 280 | #ifdef DEBUG |
| 281 | printf("%s save\n", __func__); |
| 282 | #endif |
| 283 | prog_eeprom(); |
| 284 | break; |
| 285 | default: |
| 286 | return cmd_usage(cmdtp); |
| 287 | } |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | /* We know we have at least one parameter */ |
| 293 | |
| 294 | switch (cmd) { |
| 295 | case 'n': /* serial number */ |
| 296 | #ifdef DEBUG |
| 297 | printf("%s serial number\n", __func__); |
| 298 | #endif |
| 299 | memset(e.sn, 0, sizeof(e.sn)); |
| 300 | strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1); |
| 301 | update_crc(); |
| 302 | break; |
| 303 | case 'd': /* date BCD format YYMMDDhhmmss */ |
| 304 | set_date(argv[2]); |
| 305 | break; |
| 306 | case 'e': /* errata */ |
| 307 | printf("mac errata not implemented\n"); |
| 308 | break; |
| 309 | case 'i': /* id */ |
| 310 | memset(e.id, 0, sizeof(e.id)); |
| 311 | strncpy((char *)e.id, argv[2], sizeof(e.id) - 1); |
| 312 | update_crc(); |
| 313 | break; |
| 314 | case 'p': /* ports */ |
| 315 | printf("mac ports not implemented (always 1 port)\n"); |
| 316 | break; |
| 317 | case '0' ... '9': |
| 318 | /* we only have "mac 0" but any digit can be used here */ |
| 319 | set_mac_address(argv[2]); |
| 320 | break; |
| 321 | case 'h': /* help */ |
| 322 | default: |
| 323 | return cmd_usage(cmdtp); |
| 324 | } |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
Michael Jones | 284a249 | 2013-02-07 23:53:36 +0000 | [diff] [blame^] | 329 | static inline int is_portrait(void) |
| 330 | { |
| 331 | int i; |
| 332 | unsigned int orient_index = 0; /* idx of char which determines orientation */ |
| 333 | |
| 334 | for (i = sizeof(e.id)/sizeof(*e.id) - 1; i>=0; i--) { |
| 335 | if (e.id[i] == '-') { |
| 336 | orient_index = i+1; |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | return (orient_index && |
| 342 | (e.id[orient_index] >= '5') && (e.id[orient_index] <= '8')); |
| 343 | } |
| 344 | |
Michael Jones | 8ce85da | 2011-11-04 13:53:44 -0400 | [diff] [blame] | 345 | int mac_read_from_eeprom(void) |
| 346 | { |
| 347 | u32 crc, crc_offset = offsetof(struct eeprom, crc); |
| 348 | u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */ |
Michael Jones | 284a249 | 2013-02-07 23:53:36 +0000 | [diff] [blame^] | 349 | #define FILENAME_LANDSCAPE "mvBlueLynx_X.rbf" |
| 350 | #define FILENAME_PORTRAIT "mvBlueLynx_X_sensor_cd.rbf" |
Michael Jones | 8ce85da | 2011-11-04 13:53:44 -0400 | [diff] [blame] | 351 | |
| 352 | if (read_eeprom()) { |
| 353 | printf("EEPROM Read failed.\n"); |
| 354 | return -1; |
| 355 | } |
| 356 | |
| 357 | crc = crc32(0, (void *)&e, crc_offset); |
| 358 | crcp = (void *)&e + crc_offset; |
| 359 | if (crc != be32_to_cpu(*crcp)) { |
| 360 | printf("EEPROM CRC mismatch (%08x != %08x)\n", crc, |
| 361 | be32_to_cpu(e.crc)); |
| 362 | return -1; |
| 363 | } |
| 364 | |
| 365 | if (memcmp(&e.mac, "\0\0\0\0\0\0", 6) && |
| 366 | memcmp(&e.mac, "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) { |
| 367 | char ethaddr[9]; |
| 368 | |
| 369 | sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 370 | e.mac[0], |
| 371 | e.mac[1], |
| 372 | e.mac[2], |
| 373 | e.mac[3], |
| 374 | e.mac[4], |
| 375 | e.mac[5]); |
| 376 | /* Only initialize environment variables that are blank |
| 377 | * (i.e. have not yet been set) |
| 378 | */ |
| 379 | if (!getenv("ethaddr")) |
| 380 | setenv("ethaddr", ethaddr); |
| 381 | } |
| 382 | |
| 383 | if (memcmp(&e.sn, "\0\0\0\0\0\0\0\0\0\0", 10) && |
| 384 | memcmp(&e.sn, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 10)) { |
| 385 | char serial_num[12]; |
| 386 | |
| 387 | strncpy(serial_num, (char *)e.sn, sizeof(e.sn) - 1); |
| 388 | /* Only initialize environment variables that are blank |
| 389 | * (i.e. have not yet been set) |
| 390 | */ |
| 391 | if (!getenv("serial#")) |
| 392 | setenv("serial#", serial_num); |
| 393 | } |
| 394 | |
Michael Jones | 284a249 | 2013-02-07 23:53:36 +0000 | [diff] [blame^] | 395 | /* decide which fpga file to load depending on orientation */ |
| 396 | if (is_portrait()) |
| 397 | setenv("fpgafilename", FILENAME_PORTRAIT); |
| 398 | else |
| 399 | setenv("fpgafilename", FILENAME_LANDSCAPE); |
| 400 | |
Michael Jones | 8ce85da | 2011-11-04 13:53:44 -0400 | [diff] [blame] | 401 | /* TODO should I calculate CRC here? */ |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | #ifdef CONFIG_SERIAL_TAG |
| 406 | void get_board_serial(struct tag_serialnr *serialnr) |
| 407 | { |
| 408 | char *serial = getenv("serial#"); |
| 409 | |
| 410 | if (serial && (strlen(serial) > 3)) { |
| 411 | /* use the numerical part of the serial number LXnnnnnn */ |
| 412 | serialnr->high = 0; |
| 413 | serialnr->low = simple_strtoul(serial + 2, NULL, 10); |
| 414 | } else { |
| 415 | serialnr->high = 0; |
| 416 | serialnr->low = 0; |
| 417 | } |
| 418 | } |
| 419 | #endif |