Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 1 | /* |
Haiying Wang | e560754 | 2009-06-04 16:12:40 -0400 | [diff] [blame] | 2 | * Copyright 2006, 2008-2009 Freescale Semiconductor |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 3 | * York Sun (yorksun@freescale.com) |
| 4 | * Haiying Wang (haiying.wang@freescale.com) |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 5 | * Timur Tabi (timur@freescale.com) |
Haiying Wang | 34c6878 | 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 | 11e98e7 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 31 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 32 | #include "../common/eeprom.h" |
Timur Tabi | 11e98e7 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 33 | #define MAX_NUM_PORTS 8 |
| 34 | #endif |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 35 | |
Timur Tabi | 11e98e7 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 36 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
| 37 | #define MAX_NUM_PORTS 8 |
| 38 | #define NXID_VERSION 0 |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 39 | #endif |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 40 | |
Timur Tabi | 11e98e7 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 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 |
Haiying Wang | e560754 | 2009-06-04 16:12:40 -0400 | [diff] [blame] | 46 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 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 { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 53 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 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 */ |
Haiying Wang | e560754 | 2009-06-04 16:12:40 -0400 | [diff] [blame] | 63 | u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0x71 MAC addresses */ |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 64 | u32 crc; /* 0x72 CRC32 checksum */ |
| 65 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 66 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 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 */ |
Timur Tabi | 11e98e7 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 79 | u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - x MAC addresses */ |
| 80 | u32 crc; /* x+1 CRC32 checksum */ |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 81 | #endif |
| 82 | } e; |
| 83 | |
| 84 | /* Set to 1 if we've read EEPROM into memory */ |
| 85 | static int has_been_read = 0; |
| 86 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 87 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 88 | /* Is this a valid NXID EEPROM? */ |
Kumar Gala | 4dc6245 | 2009-07-03 12:45:44 -0500 | [diff] [blame] | 89 | #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \ |
| 90 | (e.id[2] == 'I') || (e.id[3] == 'D')) |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 91 | #endif |
| 92 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 93 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 94 | /* Is this a valid CCID EEPROM? */ |
Kumar Gala | 4dc6245 | 2009-07-03 12:45:44 -0500 | [diff] [blame] | 95 | #define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \ |
| 96 | (e.id[2] == 'I') || (e.id[3] == 'D')) |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 97 | #endif |
| 98 | |
| 99 | /** |
| 100 | * show_eeprom - display the contents of the EEPROM |
| 101 | */ |
| 102 | static void show_eeprom(void) |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 103 | { |
| 104 | int i; |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 105 | unsigned int crc; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 106 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 107 | /* EEPROM tag ID, either CCID or NXID */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 108 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 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 |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 114 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 115 | /* Serial number */ |
| 116 | printf("SN: %s\n", e.sn); |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 117 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 118 | /* Errata level. */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 119 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 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 |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 126 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 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" : ""); |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 132 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 133 | /* Show MAC addresses */ |
Haiying Wang | e560754 | 2009-06-04 16:12:40 -0400 | [diff] [blame] | 134 | for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) { |
| 135 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 136 | u8 *p = e.mac[i]; |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 137 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 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 | } |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 141 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 142 | crc = crc32(0, (void *)&e, sizeof(e) - 4); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 143 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 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); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 149 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 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 |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 162 | /** |
| 163 | * read_eeprom - read the EEPROM into memory |
| 164 | */ |
| 165 | static int read_eeprom(void) |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 166 | { |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 167 | int ret; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 168 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 169 | unsigned int bus; |
| 170 | #endif |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 171 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 172 | if (has_been_read) |
| 173 | return 0; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 174 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 175 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 176 | bus = i2c_get_bus_num(); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 177 | i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM); |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 178 | #endif |
| 179 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 180 | ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 181 | (void *)&e, sizeof(e)); |
| 182 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 183 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 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; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 194 | } |
| 195 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 196 | /** |
Timur Tabi | 13c312b | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 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 | /** |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 211 | * prog_eeprom - write the EEPROM from memory |
| 212 | */ |
| 213 | static int prog_eeprom(void) |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 214 | { |
Timur Tabi | a58e880 | 2010-08-02 13:03:23 -0500 | [diff] [blame] | 215 | int ret = 0; |
Anton Vorontsov | c33e3b8 | 2009-09-02 02:17:24 +0400 | [diff] [blame] | 216 | int i; |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 217 | void *p; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 218 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 219 | unsigned int bus; |
| 220 | #endif |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 221 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 222 | /* Set the reserved values to 0xFF */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 223 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 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 |
Timur Tabi | 13c312b | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 229 | update_crc(); |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 230 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 231 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 232 | bus = i2c_get_bus_num(); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 233 | i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM); |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 234 | #endif |
| 235 | |
Timur Tabi | a58e880 | 2010-08-02 13:03:23 -0500 | [diff] [blame] | 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 | */ |
Timur Tabi | 13c312b | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 241 | for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 242 | ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
Timur Tabi | 13c312b | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 243 | p, min((sizeof(e) - i), 8)); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 244 | if (ret) |
| 245 | break; |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 246 | udelay(5000); /* 5ms write cycle timing */ |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 247 | } |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 248 | |
Timur Tabi | a58e880 | 2010-08-02 13:03:23 -0500 | [diff] [blame] | 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 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 259 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 260 | i2c_set_bus_num(bus); |
| 261 | #endif |
| 262 | |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 263 | if (ret) { |
| 264 | printf("Programming failed.\n"); |
Timur Tabi | a58e880 | 2010-08-02 13:03:23 -0500 | [diff] [blame] | 265 | has_been_read = 0; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 266 | return -1; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 267 | } |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 268 | |
| 269 | printf("Programming passed.\n"); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 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]); |
Timur Tabi | 13c312b | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 311 | |
| 312 | update_crc(); |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 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 | |
Timur Tabi | 11e98e7 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 327 | if ((index >= MAX_NUM_PORTS) || !string) { |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 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 | } |
Timur Tabi | 13c312b | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 337 | |
| 338 | update_crc(); |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 339 | } |
| 340 | |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 341 | int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 342 | { |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 343 | char cmd; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 344 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 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 | } |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 356 | |
Timur Tabi | 13c312b | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 357 | if (cmd == 'i') { |
| 358 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
| 359 | memcpy(e.id, "NXID", sizeof(e.id)); |
Timur Tabi | 11e98e7 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 360 | e.version = NXID_VERSION; |
Timur Tabi | 13c312b | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 361 | #else |
| 362 | memcpy(e.id, "CCID", sizeof(e.id)); |
| 363 | #endif |
Timur Tabi | 2f9f65f | 2011-02-09 13:40:51 -0600 | [diff] [blame^] | 364 | update_crc(); |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | if (!is_valid) { |
| 369 | printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n"); |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | if (argc == 2) { |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 374 | switch (cmd) { |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 375 | case 's': /* save */ |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 376 | prog_eeprom(); |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 377 | break; |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 378 | default: |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 379 | return cmd_usage(cmdtp); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 380 | } |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | /* We know we have at least one parameter */ |
| 386 | |
| 387 | switch (cmd) { |
| 388 | case 'n': /* serial number */ |
| 389 | memset(e.sn, 0, sizeof(e.sn)); |
| 390 | strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1); |
Timur Tabi | 13c312b | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 391 | update_crc(); |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 392 | break; |
| 393 | case 'e': /* errata */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 394 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 395 | memset(e.errata, 0, 5); |
| 396 | strncpy((char *)e.errata, argv[2], 4); |
| 397 | #else |
| 398 | e.errata[0] = argv[2][0]; |
| 399 | e.errata[1] = argv[2][1]; |
| 400 | #endif |
Timur Tabi | 13c312b | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 401 | update_crc(); |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 402 | break; |
| 403 | case 'd': /* date BCD format YYMMDDhhmmss */ |
| 404 | set_date(argv[2]); |
| 405 | break; |
| 406 | case 'p': /* MAC table size */ |
| 407 | e.mac_count = simple_strtoul(argv[2], NULL, 16); |
Timur Tabi | 13c312b | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 408 | update_crc(); |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 409 | break; |
Timur Tabi | 11e98e7 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 410 | case '0' ... '9': /* "mac 0" through "mac 22" */ |
| 411 | set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]); |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 412 | break; |
| 413 | case 'h': /* help */ |
| 414 | default: |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 415 | return cmd_usage(cmdtp); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 416 | } |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 417 | |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 418 | return 0; |
| 419 | } |
| 420 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 421 | /** |
| 422 | * mac_read_from_eeprom - read the MAC addresses from EEPROM |
| 423 | * |
| 424 | * This function reads the MAC addresses from EEPROM and sets the |
| 425 | * appropriate environment variables for each one read. |
| 426 | * |
| 427 | * The environment variables are only set if they haven't been set already. |
| 428 | * This ensures that any user-saved variables are never overwritten. |
| 429 | * |
| 430 | * This function must be called after relocation. |
| 431 | */ |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 432 | int mac_read_from_eeprom(void) |
| 433 | { |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 434 | unsigned int i; |
Timur Tabi | 13c312b | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 435 | u32 crc; |
| 436 | |
| 437 | puts("EEPROM: "); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 438 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 439 | if (read_eeprom()) { |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 440 | printf("Read failed.\n"); |
| 441 | return -1; |
| 442 | } |
| 443 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 444 | if (!is_valid) { |
Timur Tabi | 13c312b | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 445 | printf("Invalid ID (%02x %02x %02x %02x)\n", |
| 446 | e.id[0], e.id[1], e.id[2], e.id[3]); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 447 | return -1; |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 448 | } |
| 449 | |
Timur Tabi | 13c312b | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 450 | crc = crc32(0, (void *)&e, sizeof(e) - 4); |
| 451 | if (crc != be32_to_cpu(e.crc)) { |
| 452 | printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc)); |
| 453 | return -1; |
Haiying Wang | e560754 | 2009-06-04 16:12:40 -0400 | [diff] [blame] | 454 | } |
| 455 | |
Timur Tabi | 13c312b | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 456 | for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) { |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 457 | if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) && |
| 458 | memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) { |
| 459 | char ethaddr[18]; |
| 460 | char enetvar[9]; |
| 461 | |
| 462 | sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 463 | e.mac[i][0], |
| 464 | e.mac[i][1], |
| 465 | e.mac[i][2], |
| 466 | e.mac[i][3], |
| 467 | e.mac[i][4], |
| 468 | e.mac[i][5]); |
| 469 | sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i); |
| 470 | /* Only initialize environment variables that are blank |
| 471 | * (i.e. have not yet been set) |
| 472 | */ |
| 473 | if (!getenv(enetvar)) |
| 474 | setenv(enetvar, ethaddr); |
| 475 | } |
| 476 | } |
| 477 | |
Timur Tabi | 13c312b | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 478 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
| 479 | printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3], |
| 480 | be32_to_cpu(e.version)); |
| 481 | #else |
| 482 | printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]); |
| 483 | #endif |
| 484 | |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 485 | return 0; |
| 486 | } |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 487 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 488 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 489 | |
| 490 | /** |
| 491 | * get_cpu_board_revision - get the CPU board revision on 85xx boards |
| 492 | * |
| 493 | * Read the EEPROM to determine the board revision. |
| 494 | * |
| 495 | * This function is called before relocation, so we need to read a private |
| 496 | * copy of the EEPROM into a local variable on the stack. |
| 497 | * |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 498 | * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global |
| 499 | * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM, |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 500 | * so that the SPD code will work. This means that all pre-relocation I2C |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 501 | * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if |
| 502 | * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 503 | * this function is called. Oh well. |
| 504 | */ |
| 505 | unsigned int get_cpu_board_revision(void) |
| 506 | { |
| 507 | struct board_eeprom { |
| 508 | u32 id; /* 0x00 - 0x03 EEPROM Tag 'CCID' */ |
| 509 | u8 major; /* 0x04 Board revision, major */ |
| 510 | u8 minor; /* 0x05 Board revision, minor */ |
| 511 | } be; |
| 512 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 513 | i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 514 | (void *)&be, sizeof(be)); |
| 515 | |
| 516 | if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D')) |
| 517 | return MPC85XX_CPU_BOARD_REV(0, 0); |
| 518 | |
| 519 | if ((be.major == 0xff) && (be.minor == 0xff)) |
| 520 | return MPC85XX_CPU_BOARD_REV(0, 0); |
| 521 | |
Rafal Czubak | 5c50d14 | 2008-10-08 13:41:30 +0200 | [diff] [blame] | 522 | return MPC85XX_CPU_BOARD_REV(be.major, be.minor); |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 523 | } |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 524 | #endif |