Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 2 | /* |
| 3 | * |
| 4 | * Read FactorySet information from EEPROM into global structure. |
| 5 | * (C) Copyright 2013 Siemens Schweiz AG |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #if !defined(CONFIG_SPL_BUILD) |
| 9 | |
| 10 | #include <common.h> |
Anatolij Gustschin | d0d75f5 | 2019-07-10 19:44:15 +0200 | [diff] [blame^] | 11 | #include <dm.h> |
Alex Kiernan | 9c21549 | 2018-04-01 09:22:38 +0000 | [diff] [blame] | 12 | #include <environment.h> |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 13 | #include <i2c.h> |
| 14 | #include <asm/io.h> |
| 15 | #include <asm/arch/cpu.h> |
| 16 | #include <asm/arch/sys_proto.h> |
| 17 | #include <asm/unaligned.h> |
| 18 | #include <net.h> |
Samuel Egli | 8069bfe | 2013-11-04 14:05:03 +0100 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <g_dnl.h> |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 21 | #include "factoryset.h" |
| 22 | |
| 23 | #define EEPR_PG_SZ 0x80 |
| 24 | #define EEPROM_FATORYSET_OFFSET 0x400 |
| 25 | #define OFF_PG EEPROM_FATORYSET_OFFSET/EEPR_PG_SZ |
| 26 | |
| 27 | /* Global variable that contains necessary information from FactorySet */ |
| 28 | struct factorysetcontainer factory_dat; |
| 29 | |
| 30 | #define fact_get_char(i) *((char *)&eeprom_buf[i]) |
| 31 | |
| 32 | static int fact_match(unsigned char *eeprom_buf, uchar *s1, int i2) |
| 33 | { |
| 34 | if (s1 == NULL) |
| 35 | return -1; |
| 36 | |
| 37 | while (*s1 == fact_get_char(i2++)) |
| 38 | if (*s1++ == '=') |
| 39 | return i2; |
| 40 | |
| 41 | if (*s1 == '\0' && fact_get_char(i2-1) == '=') |
| 42 | return i2; |
| 43 | |
| 44 | return -1; |
| 45 | } |
| 46 | |
| 47 | static int get_factory_val(unsigned char *eeprom_buf, int size, uchar *name, |
| 48 | uchar *buf, int len) |
| 49 | { |
| 50 | int i, nxt = 0; |
| 51 | |
| 52 | for (i = 0; fact_get_char(i) != '\0'; i = nxt + 1) { |
| 53 | int val, n; |
| 54 | |
| 55 | for (nxt = i; fact_get_char(nxt) != '\0'; ++nxt) { |
| 56 | if (nxt >= size) |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | val = fact_match(eeprom_buf, (uchar *)name, i); |
| 61 | if (val < 0) |
| 62 | continue; |
| 63 | |
| 64 | /* found; copy out */ |
| 65 | for (n = 0; n < len; ++n, ++buf) { |
| 66 | *buf = fact_get_char(val++); |
| 67 | if (*buf == '\0') |
| 68 | return n; |
| 69 | } |
| 70 | |
| 71 | if (n) |
| 72 | *--buf = '\0'; |
| 73 | |
| 74 | printf("env_buf [%d bytes] too small for value of \"%s\"\n", |
| 75 | len, name); |
| 76 | |
| 77 | return n; |
| 78 | } |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | static |
| 83 | int get_factory_record_val(unsigned char *eeprom_buf, int size, uchar *record, |
| 84 | uchar *name, uchar *buf, int len) |
| 85 | { |
| 86 | int ret = -1; |
| 87 | int i, nxt = 0; |
| 88 | int c; |
| 89 | unsigned char end = 0xff; |
Heiko Schocher | 6596b85 | 2014-11-18 11:51:04 +0100 | [diff] [blame] | 90 | unsigned char tmp; |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 91 | |
| 92 | for (i = 0; fact_get_char(i) != end; i = nxt) { |
| 93 | nxt = i + 1; |
| 94 | if (fact_get_char(i) == '>') { |
| 95 | int pos; |
| 96 | int endpos; |
| 97 | int z; |
Heiko Schocher | 6596b85 | 2014-11-18 11:51:04 +0100 | [diff] [blame] | 98 | int level = 0; |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 99 | |
| 100 | c = strncmp((char *)&eeprom_buf[i + 1], (char *)record, |
| 101 | strlen((char *)record)); |
| 102 | if (c == 0) { |
| 103 | /* record found */ |
| 104 | pos = i + strlen((char *)record) + 2; |
| 105 | nxt = pos; |
| 106 | /* search for "<" */ |
| 107 | c = -1; |
| 108 | for (z = pos; fact_get_char(z) != end; z++) { |
Heiko Schocher | 6596b85 | 2014-11-18 11:51:04 +0100 | [diff] [blame] | 109 | if (fact_get_char(z) == '<') { |
| 110 | if (level == 0) { |
| 111 | endpos = z; |
| 112 | nxt = endpos; |
| 113 | c = 0; |
| 114 | break; |
| 115 | } else { |
| 116 | level--; |
| 117 | } |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 118 | } |
Heiko Schocher | 6596b85 | 2014-11-18 11:51:04 +0100 | [diff] [blame] | 119 | if (fact_get_char(z) == '>') |
| 120 | level++; |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 121 | } |
Heiko Schocher | 6596b85 | 2014-11-18 11:51:04 +0100 | [diff] [blame] | 122 | } else { |
| 123 | continue; |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 124 | } |
| 125 | if (c == 0) { |
| 126 | /* end found -> call get_factory_val */ |
Heiko Schocher | 6596b85 | 2014-11-18 11:51:04 +0100 | [diff] [blame] | 127 | tmp = eeprom_buf[endpos]; |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 128 | eeprom_buf[endpos] = end; |
| 129 | ret = get_factory_val(&eeprom_buf[pos], |
Heiko Schocher | 6596b85 | 2014-11-18 11:51:04 +0100 | [diff] [blame] | 130 | endpos - pos, name, buf, len); |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 131 | /* fix buffer */ |
Heiko Schocher | 6596b85 | 2014-11-18 11:51:04 +0100 | [diff] [blame] | 132 | eeprom_buf[endpos] = tmp; |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 133 | debug("%s: %s.%s = %s\n", |
| 134 | __func__, record, name, buf); |
| 135 | return ret; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | int factoryset_read_eeprom(int i2c_addr) |
| 143 | { |
| 144 | int i, pages = 0, size = 0; |
| 145 | unsigned char eeprom_buf[0x3c00], hdr[4], buf[MAX_STRING_LENGTH]; |
| 146 | unsigned char *cp, *cp1; |
Anatolij Gustschin | d0d75f5 | 2019-07-10 19:44:15 +0200 | [diff] [blame^] | 147 | #if CONFIG_IS_ENABLED(DM_I2C) |
| 148 | struct udevice *bus, *dev; |
| 149 | int ret; |
| 150 | #endif |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 151 | |
Marek Vasut | 7f8d436 | 2018-02-16 16:41:18 +0100 | [diff] [blame] | 152 | #if defined(CONFIG_DFU_OVER_USB) |
Maxime Ripard | 7f78b9d | 2017-09-07 08:58:08 +0200 | [diff] [blame] | 153 | factory_dat.usb_vendor_id = CONFIG_USB_GADGET_VENDOR_NUM; |
| 154 | factory_dat.usb_product_id = CONFIG_USB_GADGET_PRODUCT_NUM; |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 155 | #endif |
Anatolij Gustschin | d0d75f5 | 2019-07-10 19:44:15 +0200 | [diff] [blame^] | 156 | |
| 157 | #if CONFIG_IS_ENABLED(DM_I2C) |
| 158 | ret = uclass_get_device_by_seq(UCLASS_I2C, EEPROM_I2C_BUS, &bus); |
| 159 | if (ret) |
| 160 | goto err; |
| 161 | |
| 162 | ret = dm_i2c_probe(bus, i2c_addr, 0, &dev); |
| 163 | if (ret) |
| 164 | goto err; |
| 165 | |
| 166 | ret = i2c_set_chip_offset_len(dev, 2); |
| 167 | if (ret) |
| 168 | goto err; |
| 169 | |
| 170 | ret = dm_i2c_read(dev, EEPROM_FATORYSET_OFFSET, hdr, sizeof(hdr)); |
| 171 | if (ret) |
| 172 | goto err; |
| 173 | #else |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 174 | if (i2c_probe(i2c_addr)) |
| 175 | goto err; |
| 176 | |
| 177 | if (i2c_read(i2c_addr, EEPROM_FATORYSET_OFFSET, 2, hdr, sizeof(hdr))) |
| 178 | goto err; |
Anatolij Gustschin | d0d75f5 | 2019-07-10 19:44:15 +0200 | [diff] [blame^] | 179 | #endif |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 180 | |
| 181 | if ((hdr[0] != 0x99) || (hdr[1] != 0x80)) { |
| 182 | printf("FactorySet is not right in eeprom.\n"); |
| 183 | return 1; |
| 184 | } |
| 185 | |
| 186 | /* get FactorySet size */ |
| 187 | size = (hdr[2] << 8) + hdr[3] + sizeof(hdr); |
| 188 | if (size > 0x3bfa) |
| 189 | size = 0x3bfa; |
| 190 | |
| 191 | pages = size / EEPR_PG_SZ; |
| 192 | |
| 193 | /* |
| 194 | * read the eeprom using i2c |
| 195 | * I can not read entire eeprom in once, so separate into several |
| 196 | * times. Furthermore, fetch eeprom take longer time, so we fetch |
| 197 | * data after every time we got a record from eeprom |
| 198 | */ |
| 199 | debug("Read eeprom page :\n"); |
Anatolij Gustschin | d0d75f5 | 2019-07-10 19:44:15 +0200 | [diff] [blame^] | 200 | for (i = 0; i < pages; i++) { |
| 201 | #if CONFIG_IS_ENABLED(DM_I2C) |
| 202 | ret = dm_i2c_read(dev, (OFF_PG + i) * EEPR_PG_SZ, |
| 203 | eeprom_buf + (i * EEPR_PG_SZ), EEPR_PG_SZ); |
| 204 | if (ret) |
| 205 | goto err; |
| 206 | #else |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 207 | if (i2c_read(i2c_addr, (OFF_PG + i) * EEPR_PG_SZ, 2, |
| 208 | eeprom_buf + (i * EEPR_PG_SZ), EEPR_PG_SZ)) |
| 209 | goto err; |
Anatolij Gustschin | d0d75f5 | 2019-07-10 19:44:15 +0200 | [diff] [blame^] | 210 | #endif |
| 211 | } |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 212 | |
Anatolij Gustschin | d0d75f5 | 2019-07-10 19:44:15 +0200 | [diff] [blame^] | 213 | if (size % EEPR_PG_SZ) { |
| 214 | #if CONFIG_IS_ENABLED(DM_I2C) |
| 215 | ret = dm_i2c_read(dev, (OFF_PG + pages) * EEPR_PG_SZ, |
| 216 | eeprom_buf + (pages * EEPR_PG_SZ), |
| 217 | size % EEPR_PG_SZ); |
| 218 | if (ret) |
| 219 | goto err; |
| 220 | #else |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 221 | if (i2c_read(i2c_addr, (OFF_PG + pages) * EEPR_PG_SZ, 2, |
| 222 | eeprom_buf + (pages * EEPR_PG_SZ), |
| 223 | (size % EEPR_PG_SZ))) |
| 224 | goto err; |
Anatolij Gustschin | d0d75f5 | 2019-07-10 19:44:15 +0200 | [diff] [blame^] | 225 | #endif |
| 226 | } |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 227 | |
| 228 | /* we do below just for eeprom align */ |
| 229 | for (i = 0; i < size; i++) |
| 230 | if (eeprom_buf[i] == '\n') |
| 231 | eeprom_buf[i] = 0; |
| 232 | |
| 233 | /* skip header */ |
| 234 | size -= sizeof(hdr); |
| 235 | cp = (uchar *)eeprom_buf + sizeof(hdr); |
| 236 | |
| 237 | /* get mac address */ |
| 238 | get_factory_record_val(cp, size, (uchar *)"ETH1", (uchar *)"mac", |
| 239 | buf, MAX_STRING_LENGTH); |
| 240 | cp1 = buf; |
| 241 | for (i = 0; i < 6; i++) { |
| 242 | factory_dat.mac[i] = simple_strtoul((char *)cp1, NULL, 16); |
| 243 | cp1 += 3; |
| 244 | } |
| 245 | |
Marek Vasut | 7f8d436 | 2018-02-16 16:41:18 +0100 | [diff] [blame] | 246 | #if defined(CONFIG_DFU_OVER_USB) |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 247 | /* read vid and pid for dfu mode */ |
| 248 | if (0 <= get_factory_record_val(cp, size, (uchar *)"USBD1", |
| 249 | (uchar *)"vid", buf, |
| 250 | MAX_STRING_LENGTH)) { |
| 251 | factory_dat.usb_vendor_id = simple_strtoul((char *)buf, |
| 252 | NULL, 16); |
| 253 | } |
| 254 | |
| 255 | if (0 <= get_factory_record_val(cp, size, (uchar *)"USBD1", |
| 256 | (uchar *)"pid", buf, |
| 257 | MAX_STRING_LENGTH)) { |
| 258 | factory_dat.usb_product_id = simple_strtoul((char *)buf, |
| 259 | NULL, 16); |
| 260 | } |
| 261 | printf("DFU USB: VID = 0x%4x, PID = 0x%4x\n", factory_dat.usb_vendor_id, |
| 262 | factory_dat.usb_product_id); |
| 263 | #endif |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 264 | #if defined(CONFIG_VIDEO) |
| 265 | if (0 <= get_factory_record_val(cp, size, (uchar *)"DISP1", |
| 266 | (uchar *)"name", factory_dat.disp_name, |
| 267 | MAX_STRING_LENGTH)) { |
| 268 | debug("display name: %s\n", factory_dat.disp_name); |
| 269 | } |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 270 | #endif |
Samuel Egli | 8069bfe | 2013-11-04 14:05:03 +0100 | [diff] [blame] | 271 | if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV", |
| 272 | (uchar *)"num", factory_dat.serial, |
| 273 | MAX_STRING_LENGTH)) { |
| 274 | debug("serial number: %s\n", factory_dat.serial); |
| 275 | } |
| 276 | if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV", |
| 277 | (uchar *)"ver", buf, |
| 278 | MAX_STRING_LENGTH)) { |
| 279 | factory_dat.version = simple_strtoul((char *)buf, |
| 280 | NULL, 16); |
| 281 | debug("version number: %d\n", factory_dat.version); |
| 282 | } |
Heiko Schocher | faf2dc6 | 2014-11-18 11:51:06 +0100 | [diff] [blame] | 283 | /* Get ASN from factory set if available */ |
| 284 | if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV", |
| 285 | (uchar *)"id", factory_dat.asn, |
| 286 | MAX_STRING_LENGTH)) { |
| 287 | debug("factoryset asn: %s\n", factory_dat.asn); |
| 288 | } else { |
| 289 | factory_dat.asn[0] = 0; |
| 290 | } |
Heiko Schocher | 7b90565 | 2014-11-18 11:51:05 +0100 | [diff] [blame] | 291 | /* Get COMP/ver from factory set if available */ |
| 292 | if (0 <= get_factory_record_val(cp, size, (uchar *)"COMP", |
| 293 | (uchar *)"ver", |
| 294 | factory_dat.comp_version, |
| 295 | MAX_STRING_LENGTH)) { |
| 296 | debug("factoryset COMP/ver: %s\n", factory_dat.comp_version); |
| 297 | } else { |
| 298 | strcpy((char *)factory_dat.comp_version, "1.0"); |
| 299 | } |
| 300 | |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 301 | return 0; |
| 302 | |
| 303 | err: |
| 304 | printf("Could not read the EEPROM; something fundamentally wrong on the I2C bus.\n"); |
| 305 | return 1; |
| 306 | } |
| 307 | |
| 308 | static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE; |
| 309 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 310 | static int factoryset_mac_env_set(void) |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 311 | { |
| 312 | uint8_t mac_addr[6]; |
| 313 | |
| 314 | debug("FactorySet: Set mac address\n"); |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 315 | if (is_valid_ethaddr(factory_dat.mac)) { |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 316 | memcpy(mac_addr, factory_dat.mac, 6); |
| 317 | } else { |
| 318 | uint32_t mac_hi, mac_lo; |
| 319 | |
| 320 | debug("Warning: FactorySet: <ethaddr> not set. Fallback to E-fuse\n"); |
| 321 | mac_lo = readl(&cdev->macid0l); |
| 322 | mac_hi = readl(&cdev->macid0h); |
| 323 | |
| 324 | mac_addr[0] = mac_hi & 0xFF; |
| 325 | mac_addr[1] = (mac_hi & 0xFF00) >> 8; |
| 326 | mac_addr[2] = (mac_hi & 0xFF0000) >> 16; |
| 327 | mac_addr[3] = (mac_hi & 0xFF000000) >> 24; |
| 328 | mac_addr[4] = mac_lo & 0xFF; |
| 329 | mac_addr[5] = (mac_lo & 0xFF00) >> 8; |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 330 | if (!is_valid_ethaddr(mac_addr)) { |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 331 | printf("Warning: ethaddr not set by FactorySet or E-fuse. Set <ethaddr> variable to overcome this.\n"); |
| 332 | return -1; |
| 333 | } |
| 334 | } |
| 335 | |
Simon Glass | 8551d55 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 336 | eth_env_set_enetaddr("ethaddr", mac_addr); |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 340 | int factoryset_env_set(void) |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 341 | { |
| 342 | int ret = 0; |
| 343 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 344 | if (factoryset_mac_env_set() < 0) |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 345 | ret = -1; |
| 346 | |
| 347 | return ret; |
| 348 | } |
| 349 | |
Lukasz Majewski | 44b5b38 | 2013-10-08 14:30:41 +0200 | [diff] [blame] | 350 | int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name) |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 351 | { |
| 352 | put_unaligned(factory_dat.usb_vendor_id, &dev->idVendor); |
| 353 | put_unaligned(factory_dat.usb_product_id, &dev->idProduct); |
Samuel Egli | 8069bfe | 2013-11-04 14:05:03 +0100 | [diff] [blame] | 354 | g_dnl_set_serialnumber((char *)factory_dat.serial); |
| 355 | |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 356 | return 0; |
| 357 | } |
Samuel Egli | 8069bfe | 2013-11-04 14:05:03 +0100 | [diff] [blame] | 358 | |
| 359 | int g_dnl_get_board_bcd_device_number(int gcnum) |
| 360 | { |
| 361 | return factory_dat.version; |
| 362 | } |
Heiko Schocher | 499c498 | 2013-08-19 16:39:01 +0200 | [diff] [blame] | 363 | #endif /* defined(CONFIG_SPL_BUILD) */ |