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