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