Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2011 |
| 4 | * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | eca86fa | 2014-04-10 20:01:24 -0600 | [diff] [blame] | 8 | #include <cli_hush.h> |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 9 | #include <env.h> |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 10 | #include <i2c.h> |
| 11 | #include "common.h" |
| 12 | |
Valentin Longchamp | 16ac90c | 2015-02-10 17:10:13 +0100 | [diff] [blame] | 13 | #define MAC_STR_SZ 20 |
| 14 | |
Holger Brunck | 283857d | 2013-05-06 15:02:40 +0200 | [diff] [blame] | 15 | static int ivm_calc_crc(unsigned char *buf, int len) |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 16 | { |
| 17 | const unsigned short crc_tab[16] = { |
| 18 | 0x0000, 0xCC01, 0xD801, 0x1400, |
| 19 | 0xF001, 0x3C00, 0x2800, 0xE401, |
| 20 | 0xA001, 0x6C00, 0x7800, 0xB401, |
| 21 | 0x5000, 0x9C01, 0x8801, 0x4400}; |
| 22 | |
| 23 | unsigned short crc = 0; /* final result */ |
| 24 | unsigned short r1 = 0; /* temp */ |
| 25 | unsigned char byte = 0; /* input buffer */ |
| 26 | int i; |
| 27 | |
| 28 | /* calculate CRC from array data */ |
| 29 | for (i = 0; i < len; i++) { |
| 30 | byte = buf[i]; |
| 31 | |
| 32 | /* lower 4 bits */ |
| 33 | r1 = crc_tab[crc & 0xF]; |
| 34 | crc = ((crc) >> 4) & 0x0FFF; |
| 35 | crc = crc ^ r1 ^ crc_tab[byte & 0xF]; |
| 36 | |
| 37 | /* upper 4 bits */ |
| 38 | r1 = crc_tab[crc & 0xF]; |
| 39 | crc = (crc >> 4) & 0x0FFF; |
| 40 | crc = crc ^ r1 ^ crc_tab[(byte >> 4) & 0xF]; |
| 41 | } |
| 42 | return crc; |
| 43 | } |
| 44 | |
| 45 | static int ivm_set_value(char *name, char *value) |
| 46 | { |
| 47 | char tempbuf[256]; |
| 48 | |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 49 | if (value) { |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 50 | sprintf(tempbuf, "%s=%s", name, value); |
| 51 | return set_local_var(tempbuf, 0); |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 52 | } |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 53 | unset_local_var(name); |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int ivm_get_value(unsigned char *buf, int len, char *name, int off, |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 58 | int check) |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 59 | { |
| 60 | unsigned short val; |
| 61 | unsigned char valbuf[30]; |
| 62 | |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 63 | if (buf[off + 0] != buf[off + 2] && |
| 64 | buf[off + 2] != buf[off + 4]) { |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 65 | printf("%s Error corrupted %s\n", __func__, name); |
| 66 | val = -1; |
| 67 | } else { |
| 68 | val = buf[off + 0] + (buf[off + 1] << 8); |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 69 | if (val == 0 && check == 1) |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 70 | val = -1; |
| 71 | } |
| 72 | sprintf((char *)valbuf, "%x", val); |
| 73 | ivm_set_value(name, (char *)valbuf); |
| 74 | return val; |
| 75 | } |
| 76 | |
| 77 | #define INV_BLOCKSIZE 0x100 |
| 78 | #define INV_DATAADDRESS 0x21 |
| 79 | #define INVENTORYDATASIZE (INV_BLOCKSIZE - INV_DATAADDRESS - 3) |
| 80 | |
| 81 | #define IVM_POS_SHORT_TEXT 0 |
| 82 | #define IVM_POS_MANU_ID 1 |
| 83 | #define IVM_POS_MANU_SERIAL 2 |
| 84 | #define IVM_POS_PART_NUMBER 3 |
| 85 | #define IVM_POS_BUILD_STATE 4 |
| 86 | #define IVM_POS_SUPPLIER_PART_NUMBER 5 |
| 87 | #define IVM_POS_DELIVERY_DATE 6 |
| 88 | #define IVM_POS_SUPPLIER_BUILD_STATE 7 |
| 89 | #define IVM_POS_CUSTOMER_ID 8 |
| 90 | #define IVM_POS_CUSTOMER_PROD_ID 9 |
| 91 | #define IVM_POS_HISTORY 10 |
| 92 | #define IVM_POS_SYMBOL_ONLY 11 |
| 93 | |
| 94 | static char convert_char(char c) |
| 95 | { |
| 96 | return (c < ' ' || c > '~') ? '.' : c; |
| 97 | } |
| 98 | |
| 99 | static int ivm_findinventorystring(int type, |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 100 | unsigned char *const string, |
| 101 | unsigned long maxlen, |
| 102 | unsigned char *buf) |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 103 | { |
| 104 | int xcode = 0; |
| 105 | unsigned long cr = 0; |
| 106 | unsigned long addr = INV_DATAADDRESS; |
| 107 | unsigned long size = 0; |
| 108 | unsigned long nr = type; |
| 109 | int stop = 0; /* stop on semicolon */ |
| 110 | |
| 111 | memset(string, '\0', maxlen); |
| 112 | switch (type) { |
| 113 | case IVM_POS_SYMBOL_ONLY: |
| 114 | nr = 0; |
| 115 | stop = 1; |
| 116 | break; |
| 117 | default: |
| 118 | nr = type; |
| 119 | stop = 0; |
| 120 | } |
| 121 | |
| 122 | /* Look for the requested number of CR. */ |
| 123 | while ((cr != nr) && (addr < INVENTORYDATASIZE)) { |
Jeroen Hofstee | 2716077 | 2014-06-11 00:34:39 +0200 | [diff] [blame] | 124 | if (buf[addr] == '\r') |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 125 | cr++; |
| 126 | addr++; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * the expected number of CR was found until the end of the IVM |
| 131 | * content --> fill string |
| 132 | */ |
| 133 | if (addr < INVENTORYDATASIZE) { |
| 134 | /* Copy the IVM string in the corresponding string */ |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 135 | for (; (buf[addr] != '\r') && |
| 136 | ((buf[addr] != ';') || (!stop)) && |
| 137 | (size < (maxlen - 1) && |
| 138 | (addr < INVENTORYDATASIZE)); addr++) { |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 139 | size += sprintf((char *)string + size, "%c", |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 140 | convert_char (buf[addr])); |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* |
| 144 | * copy phase is done: check if everything is ok. If not, |
| 145 | * the inventory data is most probably corrupted: tell |
| 146 | * the world there is a problem! |
| 147 | */ |
| 148 | if (addr == INVENTORYDATASIZE) { |
| 149 | xcode = -1; |
| 150 | printf("Error end of string not found\n"); |
Valentin Longchamp | 62c9b96 | 2012-08-14 01:16:36 +0000 | [diff] [blame] | 151 | } else if ((size > (maxlen - 1)) && |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 152 | (buf[addr] != '\r')) { |
| 153 | xcode = -1; |
| 154 | printf("string too long till next CR\n"); |
| 155 | } |
| 156 | } else { |
| 157 | /* |
| 158 | * some CR are missing... |
| 159 | * the inventory data is most probably corrupted |
| 160 | */ |
| 161 | xcode = -1; |
| 162 | printf("not enough cr found\n"); |
| 163 | } |
| 164 | return xcode; |
| 165 | } |
| 166 | |
| 167 | #define GET_STRING(name, which, len) \ |
| 168 | if (ivm_findinventorystring(which, valbuf, len, buf) == 0) { \ |
| 169 | ivm_set_value(name, (char *)valbuf); \ |
| 170 | } |
| 171 | |
| 172 | static int ivm_check_crc(unsigned char *buf, int block) |
| 173 | { |
| 174 | unsigned long crc; |
| 175 | unsigned long crceeprom; |
| 176 | |
| 177 | crc = ivm_calc_crc(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2); |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 178 | crceeprom = (buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 1] + |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 179 | buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2] * 256); |
| 180 | if (crc != crceeprom) { |
| 181 | if (block == 0) |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 182 | printf("Error CRC Block: %d EEprom: calculated: %lx EEprom: %lx\n", |
| 183 | block, crc, crceeprom); |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 184 | return -1; |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
Valentin Longchamp | 16ac90c | 2015-02-10 17:10:13 +0100 | [diff] [blame] | 189 | /* take care of the possible MAC address offset and the IVM content offset */ |
| 190 | static int process_mac(unsigned char *valbuf, unsigned char *buf, |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 191 | int offset, bool unique) |
Holger Brunck | 322bb20 | 2013-01-21 03:55:17 +0000 | [diff] [blame] | 192 | { |
Valentin Longchamp | 16ac90c | 2015-02-10 17:10:13 +0100 | [diff] [blame] | 193 | unsigned char mac[6]; |
Holger Brunck | 322bb20 | 2013-01-21 03:55:17 +0000 | [diff] [blame] | 194 | unsigned long val = (buf[4] << 16) + (buf[5] << 8) + buf[6]; |
| 195 | |
Valentin Longchamp | 16ac90c | 2015-02-10 17:10:13 +0100 | [diff] [blame] | 196 | /* use an intermediate buffer, to not change IVM content |
| 197 | * MAC address is at offset 1 |
| 198 | */ |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 199 | memcpy(mac, buf + 1, 6); |
Holger Brunck | 322bb20 | 2013-01-21 03:55:17 +0000 | [diff] [blame] | 200 | |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 201 | /* MAC address can be set to locally administred, this is only allowed |
Holger Brunck | b2f2c7be | 2017-07-13 11:15:41 +0200 | [diff] [blame] | 202 | * for interfaces which have now connection to the outside. For these |
| 203 | * addresses we need to set the second bit in the first byte. |
| 204 | */ |
| 205 | if (!unique) |
| 206 | mac[0] |= 0x2; |
| 207 | |
Valentin Longchamp | 16ac90c | 2015-02-10 17:10:13 +0100 | [diff] [blame] | 208 | if (offset) { |
| 209 | val += offset; |
| 210 | mac[3] = (val >> 16) & 0xff; |
| 211 | mac[4] = (val >> 8) & 0xff; |
| 212 | mac[5] = val & 0xff; |
| 213 | } |
| 214 | |
| 215 | sprintf((char *)valbuf, "%pM", mac); |
Holger Brunck | 322bb20 | 2013-01-21 03:55:17 +0000 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 219 | static int ivm_analyze_block2(unsigned char *buf, int len) |
| 220 | { |
Valentin Longchamp | 16ac90c | 2015-02-10 17:10:13 +0100 | [diff] [blame] | 221 | unsigned char valbuf[MAC_STR_SZ]; |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 222 | unsigned long count; |
| 223 | |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 224 | /* IVM_MAC Address begins at offset 1 */ |
Holger Brunck | 6478021 | 2011-09-20 05:05:55 +0000 | [diff] [blame] | 225 | sprintf((char *)valbuf, "%pM", buf + 1); |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 226 | ivm_set_value("IVM_MacAddress", (char *)valbuf); |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 227 | /* IVM_MacCount */ |
| 228 | count = (buf[10] << 24) + |
| 229 | (buf[11] << 16) + |
| 230 | (buf[12] << 8) + |
| 231 | buf[13]; |
| 232 | if (count == 0xffffffff) |
| 233 | count = 1; |
| 234 | sprintf((char *)valbuf, "%lx", count); |
| 235 | ivm_set_value("IVM_MacCount", (char *)valbuf); |
| 236 | return 0; |
| 237 | } |
| 238 | |
Valentin Longchamp | 16ac90c | 2015-02-10 17:10:13 +0100 | [diff] [blame] | 239 | int ivm_analyze_eeprom(unsigned char *buf, int len) |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 240 | { |
| 241 | unsigned short val; |
| 242 | unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN]; |
| 243 | unsigned char *tmp; |
| 244 | |
| 245 | if (ivm_check_crc(buf, 0) != 0) |
| 246 | return -1; |
| 247 | |
| 248 | ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN, |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 249 | "IVM_BoardId", 0, 1); |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 250 | val = ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN, |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 251 | "IVM_HWKey", 6, 1); |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 252 | if (val != 0xffff) { |
| 253 | sprintf((char *)valbuf, "%x", ((val / 100) % 10)); |
| 254 | ivm_set_value("IVM_HWVariant", (char *)valbuf); |
| 255 | sprintf((char *)valbuf, "%x", (val % 100)); |
| 256 | ivm_set_value("IVM_HWVersion", (char *)valbuf); |
| 257 | } |
| 258 | ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN, |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 259 | "IVM_Functions", 12, 0); |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 260 | |
| 261 | GET_STRING("IVM_Symbol", IVM_POS_SYMBOL_ONLY, 8) |
| 262 | GET_STRING("IVM_DeviceName", IVM_POS_SHORT_TEXT, 64) |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 263 | tmp = (unsigned char *)env_get("IVM_DeviceName"); |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 264 | if (tmp) { |
| 265 | int len = strlen((char *)tmp); |
| 266 | int i = 0; |
| 267 | |
| 268 | while (i < len) { |
| 269 | if (tmp[i] == ';') { |
| 270 | ivm_set_value("IVM_ShortText", |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 271 | (char *)&tmp[i + 1]); |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 272 | break; |
| 273 | } |
| 274 | i++; |
| 275 | } |
| 276 | if (i >= len) |
| 277 | ivm_set_value("IVM_ShortText", NULL); |
| 278 | } else { |
| 279 | ivm_set_value("IVM_ShortText", NULL); |
| 280 | } |
| 281 | GET_STRING("IVM_ManufacturerID", IVM_POS_MANU_ID, 32) |
| 282 | GET_STRING("IVM_ManufacturerSerialNumber", IVM_POS_MANU_SERIAL, 20) |
| 283 | GET_STRING("IVM_ManufacturerPartNumber", IVM_POS_PART_NUMBER, 32) |
| 284 | GET_STRING("IVM_ManufacturerBuildState", IVM_POS_BUILD_STATE, 32) |
| 285 | GET_STRING("IVM_SupplierPartNumber", IVM_POS_SUPPLIER_PART_NUMBER, 32) |
| 286 | GET_STRING("IVM_DelieveryDate", IVM_POS_DELIVERY_DATE, 32) |
| 287 | GET_STRING("IVM_SupplierBuildState", IVM_POS_SUPPLIER_BUILD_STATE, 32) |
| 288 | GET_STRING("IVM_CustomerID", IVM_POS_CUSTOMER_ID, 32) |
| 289 | GET_STRING("IVM_CustomerProductID", IVM_POS_CUSTOMER_PROD_ID, 32) |
| 290 | |
| 291 | if (ivm_check_crc(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], 2) != 0) |
| 292 | return 0; |
| 293 | ivm_analyze_block2(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 294 | CONFIG_SYS_IVM_EEPROM_PAGE_LEN); |
Holger Brunck | 4f745bf | 2011-06-05 22:22:18 +0000 | [diff] [blame] | 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
Holger Brunck | fd7c400 | 2019-11-25 17:24:14 +0100 | [diff] [blame] | 299 | static int ivm_populate_env(unsigned char *buf, int len, int mac_address_offset) |
Valentin Longchamp | 16ac90c | 2015-02-10 17:10:13 +0100 | [diff] [blame] | 300 | { |
| 301 | unsigned char *page2; |
| 302 | unsigned char valbuf[MAC_STR_SZ]; |
| 303 | |
| 304 | /* do we have the page 2 filled ? if not return */ |
| 305 | if (ivm_check_crc(buf, 2)) |
| 306 | return 0; |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 307 | page2 = &buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2]; |
Valentin Longchamp | 16ac90c | 2015-02-10 17:10:13 +0100 | [diff] [blame] | 308 | |
Tom Rini | 0ebaa72 | 2022-03-23 17:19:56 -0400 | [diff] [blame] | 309 | if (IS_ENABLED(CONFIG_TARGET_KMTEGR1)) { |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 310 | /* KMTEGR1 has a special setup. eth0 has no connection to the |
| 311 | * outside and gets an locally administred MAC address, eth1 is |
| 312 | * the debug interface and gets the official MAC address from |
| 313 | * the IVM |
| 314 | */ |
| 315 | process_mac(valbuf, page2, mac_address_offset, false); |
| 316 | env_set((char *)"ethaddr", (char *)valbuf); |
| 317 | process_mac(valbuf, page2, mac_address_offset, true); |
| 318 | env_set((char *)"eth1addr", (char *)valbuf); |
Aleksandar Gerasimovski | 91ee547 | 2021-02-22 18:18:11 +0000 | [diff] [blame] | 319 | } else if (IS_ENABLED(CONFIG_ARCH_LS1021A)) { |
| 320 | /* LS102xA has 1xRGMII for debug connection and |
| 321 | * 2xSGMII for back-plane mgmt connection |
| 322 | */ |
| 323 | process_mac(valbuf, page2, 1, true); |
| 324 | env_set((char *)"ethaddr", (char *)valbuf); |
| 325 | process_mac(valbuf, page2, 2, true); |
| 326 | env_set((char *)"eth1addr", (char *)valbuf); |
| 327 | process_mac(valbuf, page2, mac_address_offset, true); |
| 328 | env_set((char *)"eth2addr", (char *)valbuf); |
| 329 | } else { |
| 330 | process_mac(valbuf, page2, mac_address_offset, true); |
| 331 | env_set((char *)"ethaddr", (char *)valbuf); |
Holger Brunck | 7292a18 | 2020-10-30 12:45:49 +0100 | [diff] [blame] | 332 | } |
Niel Fourie | 37bfd9c | 2021-01-21 13:19:20 +0100 | [diff] [blame] | 333 | if (IS_ENABLED(CONFIG_TARGET_KMCENT2)) { |
| 334 | /* 3rd ethernet interface */ |
| 335 | process_mac(valbuf, page2, 2, true); |
| 336 | env_set((char *)"eth4addr", (char *)valbuf); |
| 337 | } |
Valentin Longchamp | 16ac90c | 2015-02-10 17:10:13 +0100 | [diff] [blame] | 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
Holger Brunck | fd7c400 | 2019-11-25 17:24:14 +0100 | [diff] [blame] | 342 | int ivm_read_eeprom(unsigned char *buf, int len, int mac_address_offset) |
Valentin Longchamp | 16ac90c | 2015-02-10 17:10:13 +0100 | [diff] [blame] | 343 | { |
| 344 | int ret; |
Igor Opaniuk | 2147a16 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 345 | #if CONFIG_IS_ENABLED(DM_I2C) |
Holger Brunck | 468ba8d | 2020-02-19 19:55:14 +0100 | [diff] [blame] | 346 | struct udevice *eedev = NULL; |
Valentin Longchamp | 16ac90c | 2015-02-10 17:10:13 +0100 | [diff] [blame] | 347 | |
Holger Brunck | 468ba8d | 2020-02-19 19:55:14 +0100 | [diff] [blame] | 348 | ret = i2c_get_chip_for_busnum(CONFIG_KM_IVM_BUS, |
Tom Rini | d64d153 | 2021-08-17 17:59:39 -0400 | [diff] [blame] | 349 | CONFIG_SYS_IVM_EEPROM_ADR, 1, &eedev); |
Holger Brunck | 468ba8d | 2020-02-19 19:55:14 +0100 | [diff] [blame] | 350 | if (ret) { |
| 351 | printf("failed to get device for EEPROM at address 0x%02x\n", |
Tom Rini | d64d153 | 2021-08-17 17:59:39 -0400 | [diff] [blame] | 352 | CONFIG_SYS_IVM_EEPROM_ADR); |
Holger Brunck | 468ba8d | 2020-02-19 19:55:14 +0100 | [diff] [blame] | 353 | return 1; |
| 354 | } |
| 355 | |
| 356 | ret = dm_i2c_read(eedev, 0, buf, len); |
| 357 | if (ret != 0) { |
| 358 | printf("Error: Unable to read from I2C EEPROM at address %02X:%02X\n", |
Tom Rini | d64d153 | 2021-08-17 17:59:39 -0400 | [diff] [blame] | 359 | CONFIG_SYS_IVM_EEPROM_ADR, 0); |
Holger Brunck | 468ba8d | 2020-02-19 19:55:14 +0100 | [diff] [blame] | 360 | return 1; |
| 361 | } |
| 362 | #else |
Valentin Longchamp | 16ac90c | 2015-02-10 17:10:13 +0100 | [diff] [blame] | 363 | i2c_set_bus_num(CONFIG_KM_IVM_BUS); |
| 364 | /* add deblocking here */ |
| 365 | i2c_make_abort(); |
| 366 | |
| 367 | ret = i2c_read(CONFIG_SYS_IVM_EEPROM_ADR, 0, 1, buf, len); |
| 368 | if (ret != 0) { |
| 369 | printf("Error reading EEprom\n"); |
| 370 | return -2; |
| 371 | } |
Holger Brunck | 468ba8d | 2020-02-19 19:55:14 +0100 | [diff] [blame] | 372 | #endif |
Holger Brunck | fd7c400 | 2019-11-25 17:24:14 +0100 | [diff] [blame] | 373 | return ivm_populate_env(buf, len, mac_address_offset); |
Valentin Longchamp | 16ac90c | 2015-02-10 17:10:13 +0100 | [diff] [blame] | 374 | } |