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