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