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