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