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