blob: d98e59d1147613c9b0dd5b48d6c4c19ddac77e1f [file] [log] [blame]
Heiko Schocherc0dcece2013-08-19 16:39:01 +02001/*
2 *
3 * Read FactorySet information from EEPROM into global structure.
4 * (C) Copyright 2013 Siemens Schweiz AG
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#if !defined(CONFIG_SPL_BUILD)
10
11#include <common.h>
12#include <i2c.h>
13#include <asm/io.h>
14#include <asm/arch/cpu.h>
15#include <asm/arch/sys_proto.h>
16#include <asm/unaligned.h>
17#include <net.h>
Samuel Egli56eb3da2013-11-04 14:05:03 +010018#include <errno.h>
19#include <g_dnl.h>
Heiko Schocherc0dcece2013-08-19 16:39:01 +020020#include "factoryset.h"
21
22#define EEPR_PG_SZ 0x80
23#define EEPROM_FATORYSET_OFFSET 0x400
24#define OFF_PG EEPROM_FATORYSET_OFFSET/EEPR_PG_SZ
25
26/* Global variable that contains necessary information from FactorySet */
27struct factorysetcontainer factory_dat;
28
29#define fact_get_char(i) *((char *)&eeprom_buf[i])
30
31static int fact_match(unsigned char *eeprom_buf, uchar *s1, int i2)
32{
33 if (s1 == NULL)
34 return -1;
35
36 while (*s1 == fact_get_char(i2++))
37 if (*s1++ == '=')
38 return i2;
39
40 if (*s1 == '\0' && fact_get_char(i2-1) == '=')
41 return i2;
42
43 return -1;
44}
45
46static int get_factory_val(unsigned char *eeprom_buf, int size, uchar *name,
47 uchar *buf, int len)
48{
49 int i, nxt = 0;
50
51 for (i = 0; fact_get_char(i) != '\0'; i = nxt + 1) {
52 int val, n;
53
54 for (nxt = i; fact_get_char(nxt) != '\0'; ++nxt) {
55 if (nxt >= size)
56 return -1;
57 }
58
59 val = fact_match(eeprom_buf, (uchar *)name, i);
60 if (val < 0)
61 continue;
62
63 /* found; copy out */
64 for (n = 0; n < len; ++n, ++buf) {
65 *buf = fact_get_char(val++);
66 if (*buf == '\0')
67 return n;
68 }
69
70 if (n)
71 *--buf = '\0';
72
73 printf("env_buf [%d bytes] too small for value of \"%s\"\n",
74 len, name);
75
76 return n;
77 }
78 return -1;
79}
80
81static
82int get_factory_record_val(unsigned char *eeprom_buf, int size, uchar *record,
83 uchar *name, uchar *buf, int len)
84{
85 int ret = -1;
86 int i, nxt = 0;
87 int c;
88 unsigned char end = 0xff;
Heiko Schocher7dc60d82014-11-18 11:51:04 +010089 unsigned char tmp;
Heiko Schocherc0dcece2013-08-19 16:39:01 +020090
91 for (i = 0; fact_get_char(i) != end; i = nxt) {
92 nxt = i + 1;
93 if (fact_get_char(i) == '>') {
94 int pos;
95 int endpos;
96 int z;
Heiko Schocher7dc60d82014-11-18 11:51:04 +010097 int level = 0;
Heiko Schocherc0dcece2013-08-19 16:39:01 +020098
99 c = strncmp((char *)&eeprom_buf[i + 1], (char *)record,
100 strlen((char *)record));
101 if (c == 0) {
102 /* record found */
103 pos = i + strlen((char *)record) + 2;
104 nxt = pos;
105 /* search for "<" */
106 c = -1;
107 for (z = pos; fact_get_char(z) != end; z++) {
Heiko Schocher7dc60d82014-11-18 11:51:04 +0100108 if (fact_get_char(z) == '<') {
109 if (level == 0) {
110 endpos = z;
111 nxt = endpos;
112 c = 0;
113 break;
114 } else {
115 level--;
116 }
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200117 }
Heiko Schocher7dc60d82014-11-18 11:51:04 +0100118 if (fact_get_char(z) == '>')
119 level++;
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200120 }
Heiko Schocher7dc60d82014-11-18 11:51:04 +0100121 } else {
122 continue;
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200123 }
124 if (c == 0) {
125 /* end found -> call get_factory_val */
Heiko Schocher7dc60d82014-11-18 11:51:04 +0100126 tmp = eeprom_buf[endpos];
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200127 eeprom_buf[endpos] = end;
128 ret = get_factory_val(&eeprom_buf[pos],
Heiko Schocher7dc60d82014-11-18 11:51:04 +0100129 endpos - pos, name, buf, len);
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200130 /* fix buffer */
Heiko Schocher7dc60d82014-11-18 11:51:04 +0100131 eeprom_buf[endpos] = tmp;
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200132 debug("%s: %s.%s = %s\n",
133 __func__, record, name, buf);
134 return ret;
135 }
136 }
137 }
138 return ret;
139}
140
141int factoryset_read_eeprom(int i2c_addr)
142{
143 int i, pages = 0, size = 0;
144 unsigned char eeprom_buf[0x3c00], hdr[4], buf[MAX_STRING_LENGTH];
145 unsigned char *cp, *cp1;
146
147#if defined(CONFIG_DFU_FUNCTION)
148 factory_dat.usb_vendor_id = CONFIG_G_DNL_VENDOR_NUM;
149 factory_dat.usb_product_id = CONFIG_G_DNL_PRODUCT_NUM;
150#endif
151 if (i2c_probe(i2c_addr))
152 goto err;
153
154 if (i2c_read(i2c_addr, EEPROM_FATORYSET_OFFSET, 2, hdr, sizeof(hdr)))
155 goto err;
156
157 if ((hdr[0] != 0x99) || (hdr[1] != 0x80)) {
158 printf("FactorySet is not right in eeprom.\n");
159 return 1;
160 }
161
162 /* get FactorySet size */
163 size = (hdr[2] << 8) + hdr[3] + sizeof(hdr);
164 if (size > 0x3bfa)
165 size = 0x3bfa;
166
167 pages = size / EEPR_PG_SZ;
168
169 /*
170 * read the eeprom using i2c
171 * I can not read entire eeprom in once, so separate into several
172 * times. Furthermore, fetch eeprom take longer time, so we fetch
173 * data after every time we got a record from eeprom
174 */
175 debug("Read eeprom page :\n");
176 for (i = 0; i < pages; i++)
177 if (i2c_read(i2c_addr, (OFF_PG + i) * EEPR_PG_SZ, 2,
178 eeprom_buf + (i * EEPR_PG_SZ), EEPR_PG_SZ))
179 goto err;
180
181 if (size % EEPR_PG_SZ)
182 if (i2c_read(i2c_addr, (OFF_PG + pages) * EEPR_PG_SZ, 2,
183 eeprom_buf + (pages * EEPR_PG_SZ),
184 (size % EEPR_PG_SZ)))
185 goto err;
186
187 /* we do below just for eeprom align */
188 for (i = 0; i < size; i++)
189 if (eeprom_buf[i] == '\n')
190 eeprom_buf[i] = 0;
191
192 /* skip header */
193 size -= sizeof(hdr);
194 cp = (uchar *)eeprom_buf + sizeof(hdr);
195
196 /* get mac address */
197 get_factory_record_val(cp, size, (uchar *)"ETH1", (uchar *)"mac",
198 buf, MAX_STRING_LENGTH);
199 cp1 = buf;
200 for (i = 0; i < 6; i++) {
201 factory_dat.mac[i] = simple_strtoul((char *)cp1, NULL, 16);
202 cp1 += 3;
203 }
204
205#if defined(CONFIG_DFU_FUNCTION)
206 /* read vid and pid for dfu mode */
207 if (0 <= get_factory_record_val(cp, size, (uchar *)"USBD1",
208 (uchar *)"vid", buf,
209 MAX_STRING_LENGTH)) {
210 factory_dat.usb_vendor_id = simple_strtoul((char *)buf,
211 NULL, 16);
212 }
213
214 if (0 <= get_factory_record_val(cp, size, (uchar *)"USBD1",
215 (uchar *)"pid", buf,
216 MAX_STRING_LENGTH)) {
217 factory_dat.usb_product_id = simple_strtoul((char *)buf,
218 NULL, 16);
219 }
220 printf("DFU USB: VID = 0x%4x, PID = 0x%4x\n", factory_dat.usb_vendor_id,
221 factory_dat.usb_product_id);
222#endif
223 if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV",
224 (uchar *)"id", buf,
225 MAX_STRING_LENGTH)) {
226 if (strncmp((const char *)buf, "PXM50", 5) == 0)
227 factory_dat.pxm50 = 1;
228 else
229 factory_dat.pxm50 = 0;
230 }
231 debug("PXM50: %d\n", factory_dat.pxm50);
232#if defined(CONFIG_VIDEO)
233 if (0 <= get_factory_record_val(cp, size, (uchar *)"DISP1",
234 (uchar *)"name", factory_dat.disp_name,
235 MAX_STRING_LENGTH)) {
236 debug("display name: %s\n", factory_dat.disp_name);
237 }
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200238#endif
Samuel Egli56eb3da2013-11-04 14:05:03 +0100239 if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV",
240 (uchar *)"num", factory_dat.serial,
241 MAX_STRING_LENGTH)) {
242 debug("serial number: %s\n", factory_dat.serial);
243 }
244 if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV",
245 (uchar *)"ver", buf,
246 MAX_STRING_LENGTH)) {
247 factory_dat.version = simple_strtoul((char *)buf,
248 NULL, 16);
249 debug("version number: %d\n", factory_dat.version);
250 }
251
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200252 return 0;
253
254err:
255 printf("Could not read the EEPROM; something fundamentally wrong on the I2C bus.\n");
256 return 1;
257}
258
259static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
260
261static int factoryset_mac_setenv(void)
262{
263 uint8_t mac_addr[6];
264
265 debug("FactorySet: Set mac address\n");
266 if (is_valid_ether_addr(factory_dat.mac)) {
267 memcpy(mac_addr, factory_dat.mac, 6);
268 } else {
269 uint32_t mac_hi, mac_lo;
270
271 debug("Warning: FactorySet: <ethaddr> not set. Fallback to E-fuse\n");
272 mac_lo = readl(&cdev->macid0l);
273 mac_hi = readl(&cdev->macid0h);
274
275 mac_addr[0] = mac_hi & 0xFF;
276 mac_addr[1] = (mac_hi & 0xFF00) >> 8;
277 mac_addr[2] = (mac_hi & 0xFF0000) >> 16;
278 mac_addr[3] = (mac_hi & 0xFF000000) >> 24;
279 mac_addr[4] = mac_lo & 0xFF;
280 mac_addr[5] = (mac_lo & 0xFF00) >> 8;
281 if (!is_valid_ether_addr(mac_addr)) {
282 printf("Warning: ethaddr not set by FactorySet or E-fuse. Set <ethaddr> variable to overcome this.\n");
283 return -1;
284 }
285 }
286
287 eth_setenv_enetaddr("ethaddr", mac_addr);
288 return 0;
289}
290
291int factoryset_setenv(void)
292{
293 int ret = 0;
294
295 if (factoryset_mac_setenv() < 0)
296 ret = -1;
297
298 return ret;
299}
300
Lukasz Majewskid6eae7b2013-10-08 14:30:41 +0200301int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200302{
303 put_unaligned(factory_dat.usb_vendor_id, &dev->idVendor);
304 put_unaligned(factory_dat.usb_product_id, &dev->idProduct);
Samuel Egli56eb3da2013-11-04 14:05:03 +0100305 g_dnl_set_serialnumber((char *)factory_dat.serial);
306
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200307 return 0;
308}
Samuel Egli56eb3da2013-11-04 14:05:03 +0100309
310int g_dnl_get_board_bcd_device_number(int gcnum)
311{
312 return factory_dat.version;
313}
Heiko Schocherc0dcece2013-08-19 16:39:01 +0200314#endif /* defined(CONFIG_SPL_BUILD) */