Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 2 | /* |
Timur Tabi | f098c9c | 2011-02-09 02:00:09 +0000 | [diff] [blame] | 3 | * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 4 | * York Sun (yorksun@freescale.com) |
| 5 | * Haiying Wang (haiying.wang@freescale.com) |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 6 | * Timur Tabi (timur@freescale.com) |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 11 | #include <env.h> |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 12 | #include <i2c.h> |
| 13 | #include <linux/ctype.h> |
| 14 | |
Timur Tabi | bfb7071 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 15 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 16 | #include "../common/eeprom.h" |
Timur Tabi | bfb7071 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 17 | #define MAX_NUM_PORTS 8 |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 18 | #endif |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 19 | |
Timur Tabi | bfb7071 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 20 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Shengzhou Liu | 5536aeb | 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 | 477c894 | 2014-04-25 18:38:44 -0500 | [diff] [blame] | 24 | #define MAX_NUM_PORTS 16 |
Shengzhou Liu | 5536aeb | 2013-09-13 14:46:01 +0800 | [diff] [blame] | 25 | #endif |
Timur Tabi | bfb7071 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 26 | #define NXID_VERSION 1 |
| 27 | #endif |
Haiying Wang | 9a61108 | 2009-06-04 16:12:40 -0400 | [diff] [blame] | 28 | |
Timur Tabi | e2d31fb | 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 | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 35 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
Timur Tabi | e2d31fb | 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 | 9a61108 | 2009-06-04 16:12:40 -0400 | [diff] [blame] | 45 | u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0x71 MAC addresses */ |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 46 | u32 crc; /* 0x72 CRC32 checksum */ |
| 47 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 48 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | e2d31fb | 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 | 477c894 | 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 | e2d31fb | 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 | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 70 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 71 | /* Is this a valid NXID EEPROM? */ |
Kumar Gala | afb0b131 | 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 | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 74 | #endif |
| 75 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 76 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 77 | /* Is this a valid CCID EEPROM? */ |
Kumar Gala | afb0b131 | 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 | e2d31fb | 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 | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 86 | { |
| 87 | int i; |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 88 | unsigned int crc; |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 89 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 90 | /* EEPROM tag ID, either CCID or NXID */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 91 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | e2d31fb | 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 | b8baf46 | 2015-05-28 14:54:03 +0530 | [diff] [blame] | 93 | be32_to_cpu(e.version)); |
Timur Tabi | e2d31fb | 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 | d59feff | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 97 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 98 | /* Serial number */ |
| 99 | printf("SN: %s\n", e.sn); |
Haiying Wang | d59feff | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 100 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 101 | /* Errata level. */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 102 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | e2d31fb | 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 | d59feff | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 109 | |
Timur Tabi | e2d31fb | 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 | d59feff | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 115 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 116 | /* Show MAC addresses */ |
Masahiro Yamada | b414119 | 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 | 9a61108 | 2009-06-04 16:12:40 -0400 | [diff] [blame] | 118 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 119 | u8 *p = e.mac[i]; |
| 120 | |
| 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 | } |
| 124 | |
| 125 | crc = crc32(0, (void *)&e, sizeof(e) - 4); |
| 126 | |
| 127 | if (crc == be32_to_cpu(e.crc)) |
| 128 | printf("CRC: %08x\n", be32_to_cpu(e.crc)); |
Haiying Wang | d59feff | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 129 | else |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 130 | printf("CRC: %08x (should be %08x)\n", |
| 131 | be32_to_cpu(e.crc), crc); |
Haiying Wang | d59feff | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 132 | |
Timur Tabi | e2d31fb | 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"); |
Haiying Wang | d59feff | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 141 | } |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 142 | #endif |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Timur Tabi | e2d31fb | 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 | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 149 | { |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 150 | int ret; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 151 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 152 | #ifndef CONFIG_DM_I2C |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 153 | unsigned int bus; |
| 154 | #endif |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 155 | #endif |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 156 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 157 | if (has_been_read) |
| 158 | return 0; |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 159 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 160 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 161 | #ifndef CONFIG_DM_I2C |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 162 | bus = i2c_get_bus_num(); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 163 | i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 164 | #endif |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 165 | #endif |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 166 | |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 167 | #ifndef CONFIG_DM_I2C |
| 168 | ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, |
| 169 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
| 170 | (void *)&e, sizeof(e)); |
| 171 | #else |
| 172 | struct udevice *dev; |
| 173 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
| 174 | ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM, |
| 175 | CONFIG_SYS_I2C_EEPROM_ADDR, 1, &dev); |
| 176 | #else |
| 177 | ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR, 1, &dev); |
| 178 | #endif |
| 179 | if (!ret) |
| 180 | ret = dm_i2c_read(dev, 0, (void *)&e, sizeof(e)); |
| 181 | #endif |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 182 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 183 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 184 | #ifndef CONFIG_DM_I2C |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 185 | i2c_set_bus_num(bus); |
| 186 | #endif |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 187 | #endif |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 188 | |
| 189 | #ifdef DEBUG |
| 190 | show_eeprom(); |
| 191 | #endif |
| 192 | |
| 193 | has_been_read = (ret == 0) ? 1 : 0; |
| 194 | |
| 195 | return ret; |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 198 | /** |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 199 | * update_crc - update the CRC |
| 200 | * |
| 201 | * This function should be called after each update to the EEPROM structure, |
| 202 | * to make sure the CRC is always correct. |
| 203 | */ |
| 204 | static void update_crc(void) |
| 205 | { |
| 206 | u32 crc; |
| 207 | |
| 208 | crc = crc32(0, (void *)&e, sizeof(e) - 4); |
| 209 | e.crc = cpu_to_be32(crc); |
| 210 | } |
| 211 | |
| 212 | /** |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 213 | * prog_eeprom - write the EEPROM from memory |
| 214 | */ |
| 215 | static int prog_eeprom(void) |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 216 | { |
Timur Tabi | 3addcb9 | 2010-08-02 13:03:23 -0500 | [diff] [blame] | 217 | int ret = 0; |
Anton Vorontsov | 9c671e7 | 2009-09-02 02:17:24 +0400 | [diff] [blame] | 218 | int i; |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 219 | void *p; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 220 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 221 | #ifndef CONFIG_DM_I2C |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 222 | unsigned int bus; |
| 223 | #endif |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 224 | #endif |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 225 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 226 | /* Set the reserved values to 0xFF */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 227 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 228 | e.res_0 = 0xFF; |
| 229 | memset(e.res_1, 0xFF, sizeof(e.res_1)); |
| 230 | #else |
| 231 | memset(e.res_0, 0xFF, sizeof(e.res_0)); |
| 232 | #endif |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 233 | update_crc(); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 234 | |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 235 | #ifndef CONFIG_DM_I2C |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 236 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 237 | bus = i2c_get_bus_num(); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 238 | i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 239 | #endif |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 240 | #endif |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 241 | |
Timur Tabi | 3addcb9 | 2010-08-02 13:03:23 -0500 | [diff] [blame] | 242 | /* |
| 243 | * The AT24C02 datasheet says that data can only be written in page |
| 244 | * mode, which means 8 bytes at a time, and it takes up to 5ms to |
| 245 | * complete a given write. |
| 246 | */ |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 247 | for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) { |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 248 | #ifndef CONFIG_DM_I2C |
| 249 | ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, |
| 250 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
Masahiro Yamada | b414119 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 251 | p, min((int)(sizeof(e) - i), 8)); |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 252 | #else |
| 253 | struct udevice *dev; |
| 254 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
| 255 | ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM, |
| 256 | CONFIG_SYS_I2C_EEPROM_ADDR, |
| 257 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
| 258 | &dev); |
| 259 | #else |
| 260 | ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR, |
| 261 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
| 262 | &dev); |
| 263 | #endif |
| 264 | if (!ret) |
| 265 | ret = dm_i2c_write(dev, i, p, min((int)(sizeof(e) - i), |
| 266 | 8)); |
| 267 | #endif |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 268 | if (ret) |
| 269 | break; |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 270 | udelay(5000); /* 5ms write cycle timing */ |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 271 | } |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 272 | |
Timur Tabi | 3addcb9 | 2010-08-02 13:03:23 -0500 | [diff] [blame] | 273 | if (!ret) { |
| 274 | /* Verify the write by reading back the EEPROM and comparing */ |
| 275 | struct eeprom e2; |
| 276 | |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 277 | #ifndef CONFIG_DM_I2C |
Timur Tabi | 3addcb9 | 2010-08-02 13:03:23 -0500 | [diff] [blame] | 278 | ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 279 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
| 280 | (void *)&e2, sizeof(e2)); |
| 281 | #else |
| 282 | struct udevice *dev; |
| 283 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
| 284 | ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM, |
| 285 | CONFIG_SYS_I2C_EEPROM_ADDR, |
| 286 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
| 287 | &dev); |
| 288 | #else |
| 289 | ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR, |
| 290 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
| 291 | &dev); |
| 292 | #endif |
| 293 | if (!ret) |
| 294 | ret = dm_i2c_read(dev, 0, (void *)&e2, sizeof(e2)); |
| 295 | #endif |
Timur Tabi | 3addcb9 | 2010-08-02 13:03:23 -0500 | [diff] [blame] | 296 | if (!ret && memcmp(&e, &e2, sizeof(e))) |
| 297 | ret = -1; |
| 298 | } |
| 299 | |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 300 | #ifndef CONFIG_DM_I2C |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 301 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 302 | i2c_set_bus_num(bus); |
| 303 | #endif |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 304 | #endif |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 305 | |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 306 | if (ret) { |
| 307 | printf("Programming failed.\n"); |
Timur Tabi | 3addcb9 | 2010-08-02 13:03:23 -0500 | [diff] [blame] | 308 | has_been_read = 0; |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 309 | return -1; |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 310 | } |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 311 | |
| 312 | printf("Programming passed.\n"); |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 313 | return 0; |
| 314 | } |
| 315 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 316 | /** |
| 317 | * h2i - converts hex character into a number |
| 318 | * |
| 319 | * This function takes a hexadecimal character (e.g. '7' or 'C') and returns |
| 320 | * the integer equivalent. |
| 321 | */ |
| 322 | static inline u8 h2i(char p) |
| 323 | { |
| 324 | if ((p >= '0') && (p <= '9')) |
| 325 | return p - '0'; |
| 326 | |
| 327 | if ((p >= 'A') && (p <= 'F')) |
| 328 | return (p - 'A') + 10; |
| 329 | |
| 330 | if ((p >= 'a') && (p <= 'f')) |
| 331 | return (p - 'a') + 10; |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * set_date - stores the build date into the EEPROM |
| 338 | * |
| 339 | * This function takes a pointer to a string in the format "YYMMDDhhmmss" |
| 340 | * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string, |
| 341 | * and stores it in the build date field of the EEPROM local copy. |
| 342 | */ |
| 343 | static void set_date(const char *string) |
| 344 | { |
| 345 | unsigned int i; |
| 346 | |
| 347 | if (strlen(string) != 12) { |
| 348 | printf("Usage: mac date YYMMDDhhmmss\n"); |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | for (i = 0; i < 6; i++) |
| 353 | e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]); |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 354 | |
| 355 | update_crc(); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /** |
| 359 | * set_mac_address - stores a MAC address into the EEPROM |
| 360 | * |
| 361 | * This function takes a pointer to MAC address string |
| 362 | * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and |
| 363 | * stores it in one of the MAC address fields of the EEPROM local copy. |
| 364 | */ |
| 365 | static void set_mac_address(unsigned int index, const char *string) |
| 366 | { |
| 367 | char *p = (char *) string; |
| 368 | unsigned int i; |
| 369 | |
Timur Tabi | bfb7071 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 370 | if ((index >= MAX_NUM_PORTS) || !string) { |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 371 | printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n"); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | for (i = 0; *p && (i < 6); i++) { |
| 376 | e.mac[index][i] = simple_strtoul(p, &p, 16); |
| 377 | if (*p == ':') |
| 378 | p++; |
| 379 | } |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 380 | |
| 381 | update_crc(); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 382 | } |
| 383 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 384 | int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 385 | { |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 386 | char cmd; |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 387 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 388 | if (argc == 1) { |
| 389 | show_eeprom(); |
| 390 | return 0; |
| 391 | } |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 392 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 393 | cmd = argv[1][0]; |
| 394 | |
| 395 | if (cmd == 'r') { |
| 396 | read_eeprom(); |
| 397 | return 0; |
| 398 | } |
| 399 | |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 400 | if (cmd == 'i') { |
| 401 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
| 402 | memcpy(e.id, "NXID", sizeof(e.id)); |
Jaiprakash Singh | b8baf46 | 2015-05-28 14:54:03 +0530 | [diff] [blame] | 403 | e.version = cpu_to_be32(NXID_VERSION); |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 404 | #else |
| 405 | memcpy(e.id, "CCID", sizeof(e.id)); |
| 406 | #endif |
Timur Tabi | 3fee334 | 2011-02-09 13:40:51 -0600 | [diff] [blame] | 407 | update_crc(); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | if (!is_valid) { |
| 412 | printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n"); |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | if (argc == 2) { |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 417 | switch (cmd) { |
Jon Loeliger | 80e955c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 418 | case 's': /* save */ |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 419 | prog_eeprom(); |
Jon Loeliger | 80e955c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 420 | break; |
Jon Loeliger | 80e955c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 421 | default: |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 422 | return cmd_usage(cmdtp); |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 423 | } |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 424 | |
| 425 | return 0; |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 426 | } |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 427 | |
| 428 | /* We know we have at least one parameter */ |
| 429 | |
| 430 | switch (cmd) { |
| 431 | case 'n': /* serial number */ |
| 432 | memset(e.sn, 0, sizeof(e.sn)); |
| 433 | strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1); |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 434 | update_crc(); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 435 | break; |
| 436 | case 'e': /* errata */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 437 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 438 | memset(e.errata, 0, 5); |
| 439 | strncpy((char *)e.errata, argv[2], 4); |
| 440 | #else |
| 441 | e.errata[0] = argv[2][0]; |
| 442 | e.errata[1] = argv[2][1]; |
| 443 | #endif |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 444 | update_crc(); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 445 | break; |
| 446 | case 'd': /* date BCD format YYMMDDhhmmss */ |
| 447 | set_date(argv[2]); |
| 448 | break; |
| 449 | case 'p': /* MAC table size */ |
| 450 | e.mac_count = simple_strtoul(argv[2], NULL, 16); |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 451 | update_crc(); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 452 | break; |
Timur Tabi | bfb7071 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 453 | case '0' ... '9': /* "mac 0" through "mac 22" */ |
| 454 | set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 455 | break; |
| 456 | case 'h': /* help */ |
| 457 | default: |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 458 | return cmd_usage(cmdtp); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 459 | } |
| 460 | |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 461 | return 0; |
| 462 | } |
| 463 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 464 | /** |
| 465 | * mac_read_from_eeprom - read the MAC addresses from EEPROM |
| 466 | * |
| 467 | * This function reads the MAC addresses from EEPROM and sets the |
| 468 | * appropriate environment variables for each one read. |
| 469 | * |
| 470 | * The environment variables are only set if they haven't been set already. |
| 471 | * This ensures that any user-saved variables are never overwritten. |
| 472 | * |
| 473 | * This function must be called after relocation. |
Timur Tabi | f098c9c | 2011-02-09 02:00:09 +0000 | [diff] [blame] | 474 | * |
| 475 | * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0 |
| 476 | * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is |
| 477 | * located at a different offset. |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 478 | */ |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 479 | int mac_read_from_eeprom(void) |
| 480 | { |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 481 | unsigned int i; |
Timur Tabi | f098c9c | 2011-02-09 02:00:09 +0000 | [diff] [blame] | 482 | u32 crc, crc_offset = offsetof(struct eeprom, crc); |
| 483 | u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */ |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 484 | |
| 485 | puts("EEPROM: "); |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 486 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 487 | if (read_eeprom()) { |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 488 | printf("Read failed.\n"); |
York Sun | bffac7a | 2014-04-30 14:43:46 -0700 | [diff] [blame] | 489 | return 0; |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 490 | } |
| 491 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 492 | if (!is_valid) { |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 493 | printf("Invalid ID (%02x %02x %02x %02x)\n", |
| 494 | e.id[0], e.id[1], e.id[2], e.id[3]); |
York Sun | bffac7a | 2014-04-30 14:43:46 -0700 | [diff] [blame] | 495 | return 0; |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 496 | } |
| 497 | |
Timur Tabi | f098c9c | 2011-02-09 02:00:09 +0000 | [diff] [blame] | 498 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
| 499 | /* |
| 500 | * If we've read an NXID v0 EEPROM, then we need to set the CRC offset |
| 501 | * to where it is in v0. |
| 502 | */ |
| 503 | if (e.version == 0) |
| 504 | crc_offset = 0x72; |
| 505 | #endif |
| 506 | |
| 507 | crc = crc32(0, (void *)&e, crc_offset); |
| 508 | crcp = (void *)&e + crc_offset; |
| 509 | if (crc != be32_to_cpu(*crcp)) { |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 510 | printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc)); |
York Sun | bffac7a | 2014-04-30 14:43:46 -0700 | [diff] [blame] | 511 | return 0; |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 512 | } |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 513 | |
Timur Tabi | f098c9c | 2011-02-09 02:00:09 +0000 | [diff] [blame] | 514 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
| 515 | /* |
| 516 | * MAC address #9 in v1 occupies the same position as the CRC in v0. |
| 517 | * Erase it so that it's not mistaken for a MAC address. We'll |
| 518 | * update the CRC later. |
| 519 | */ |
| 520 | if (e.version == 0) |
| 521 | memset(e.mac[8], 0xff, 6); |
| 522 | #endif |
| 523 | |
Masahiro Yamada | b414119 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 524 | for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) { |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 525 | if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) && |
| 526 | memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) { |
| 527 | char ethaddr[18]; |
| 528 | char enetvar[9]; |
| 529 | |
| 530 | sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 531 | e.mac[i][0], |
| 532 | e.mac[i][1], |
| 533 | e.mac[i][2], |
| 534 | e.mac[i][3], |
| 535 | e.mac[i][4], |
| 536 | e.mac[i][5]); |
| 537 | sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i); |
| 538 | /* Only initialize environment variables that are blank |
| 539 | * (i.e. have not yet been set) |
| 540 | */ |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 541 | if (!env_get(enetvar)) |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 542 | env_set(enetvar, ethaddr); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 546 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
| 547 | printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3], |
Jaiprakash Singh | b8baf46 | 2015-05-28 14:54:03 +0530 | [diff] [blame] | 548 | be32_to_cpu(e.version)); |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 549 | #else |
| 550 | printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]); |
| 551 | #endif |
| 552 | |
Timur Tabi | f098c9c | 2011-02-09 02:00:09 +0000 | [diff] [blame] | 553 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
| 554 | /* |
| 555 | * Now we need to upconvert the data into v1 format. We do this last so |
| 556 | * that at boot time, U-Boot will still say "NXID v0". |
| 557 | */ |
| 558 | if (e.version == 0) { |
Jaiprakash Singh | b8baf46 | 2015-05-28 14:54:03 +0530 | [diff] [blame] | 559 | e.version = cpu_to_be32(NXID_VERSION); |
Timur Tabi | f098c9c | 2011-02-09 02:00:09 +0000 | [diff] [blame] | 560 | update_crc(); |
| 561 | } |
| 562 | #endif |
| 563 | |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 564 | return 0; |
| 565 | } |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 566 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 567 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 568 | |
| 569 | /** |
| 570 | * get_cpu_board_revision - get the CPU board revision on 85xx boards |
| 571 | * |
| 572 | * Read the EEPROM to determine the board revision. |
| 573 | * |
| 574 | * This function is called before relocation, so we need to read a private |
| 575 | * copy of the EEPROM into a local variable on the stack. |
| 576 | * |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 577 | * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global |
| 578 | * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM, |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 579 | * so that the SPD code will work. This means that all pre-relocation I2C |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 580 | * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if |
| 581 | * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 582 | * this function is called. Oh well. |
| 583 | */ |
| 584 | unsigned int get_cpu_board_revision(void) |
| 585 | { |
| 586 | struct board_eeprom { |
| 587 | u32 id; /* 0x00 - 0x03 EEPROM Tag 'CCID' */ |
| 588 | u8 major; /* 0x04 Board revision, major */ |
| 589 | u8 minor; /* 0x05 Board revision, minor */ |
| 590 | } be; |
| 591 | |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 592 | #ifndef CONFIG_DM_I2C |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 593 | i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 594 | (void *)&be, sizeof(be)); |
Chuanhua Han | 0eba65d | 2019-07-10 21:00:20 +0800 | [diff] [blame] | 595 | #else |
| 596 | struct udevice *dev; |
| 597 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
| 598 | ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM, |
| 599 | CONFIG_SYS_I2C_EEPROM_ADDR, |
| 600 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
| 601 | &dev); |
| 602 | #else |
| 603 | ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR, |
| 604 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
| 605 | &dev) |
| 606 | #endif |
| 607 | if (!ret) |
| 608 | dm_i2c_read(dev, 0, (void *)&be, sizeof(be)); |
| 609 | #endif |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 610 | |
| 611 | if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D')) |
| 612 | return MPC85XX_CPU_BOARD_REV(0, 0); |
| 613 | |
| 614 | if ((be.major == 0xff) && (be.minor == 0xff)) |
| 615 | return MPC85XX_CPU_BOARD_REV(0, 0); |
| 616 | |
Rafal Czubak | e46c7bf | 2008-10-08 13:41:30 +0200 | [diff] [blame] | 617 | return MPC85XX_CPU_BOARD_REV(be.major, be.minor); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 618 | } |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 619 | #endif |