xref: /openbmc/u-boot/board/ti/common/board_detect.c (revision cf0bcd7d)
1 /*
2  * Library to support early TI EVM EEPROM handling
3  *
4  * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
5  *	Lokesh Vutla
6  *	Steve Kipisz
7  *
8  * SPDX-License-Identifier:    GPL-2.0+
9  */
10 
11 #include <common.h>
12 #include <asm/omap_common.h>
13 #include <dm/uclass.h>
14 #include <i2c.h>
15 
16 #include "board_detect.h"
17 
18 #if defined(CONFIG_DM_I2C_COMPAT)
19 /**
20  * ti_i2c_set_alen - Set chip's i2c address length
21  * @bus_addr - I2C bus number
22  * @dev_addr - I2C eeprom id
23  * @alen     - I2C address length in bytes
24  *
25  * DM_I2C by default sets the address length to be used to 1. This
26  * function allows this address length to be changed to match the
27  * eeprom used for board detection.
28  */
29 int __maybe_unused ti_i2c_set_alen(int bus_addr, int dev_addr, int alen)
30 {
31 	struct udevice *dev;
32 	struct udevice *bus;
33 	int rc;
34 
35 	rc = uclass_get_device_by_seq(UCLASS_I2C, bus_addr, &bus);
36 	if (rc)
37 		return rc;
38 	rc = i2c_get_chip(bus, dev_addr, 1, &dev);
39 	if (rc)
40 		return rc;
41 	rc = i2c_set_chip_offset_len(dev, alen);
42 	if (rc)
43 		return rc;
44 
45 	return 0;
46 }
47 #else
48 int __maybe_unused ti_i2c_set_alen(int bus_addr, int dev_addr, int alen)
49 {
50 	return 0;
51 }
52 #endif
53 
54 /**
55  * ti_i2c_eeprom_init - Initialize an i2c bus and probe for a device
56  * @i2c_bus: i2c bus number to initialize
57  * @dev_addr: Device address to probe for
58  *
59  * Return: 0 on success or corresponding error on failure.
60  */
61 static int __maybe_unused ti_i2c_eeprom_init(int i2c_bus, int dev_addr)
62 {
63 	int rc;
64 
65 	if (i2c_bus >= 0) {
66 		rc = i2c_set_bus_num(i2c_bus);
67 		if (rc)
68 			return rc;
69 	}
70 
71 	return i2c_probe(dev_addr);
72 }
73 
74 /**
75  * ti_i2c_eeprom_read - Read data from an EEPROM
76  * @dev_addr: The device address of the EEPROM
77  * @offset: Offset to start reading in the EEPROM
78  * @ep: Pointer to a buffer to read into
79  * @epsize: Size of buffer
80  *
81  * Return: 0 on success or corresponding result of i2c_read
82  */
83 static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset,
84 					     uchar *ep, int epsize)
85 {
86 	int bus_num, rc, alen;
87 
88 	bus_num = i2c_get_bus_num();
89 
90 	alen = 2;
91 
92 	rc = ti_i2c_set_alen(bus_num, dev_addr, alen);
93 	if (rc)
94 		return rc;
95 
96 	return i2c_read(dev_addr, offset, alen, ep, epsize);
97 }
98 
99 /**
100  * ti_eeprom_string_cleanup() - Handle eeprom programming errors
101  * @s:	eeprom string (should be NULL terminated)
102  *
103  * Some Board manufacturers do not add a NULL termination at the
104  * end of string, instead some binary information is kludged in, hence
105  * convert the string to just printable characters of ASCII chart.
106  */
107 static void __maybe_unused ti_eeprom_string_cleanup(char *s)
108 {
109 	int i, l;
110 
111 	l = strlen(s);
112 	for (i = 0; i < l; i++, s++)
113 		if (*s < ' ' || *s > '~') {
114 			*s = 0;
115 			break;
116 		}
117 }
118 
119 __weak void gpi2c_init(void)
120 {
121 }
122 
123 static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr,
124 					    u32 header, u32 size, uint8_t *ep)
125 {
126 	u32 byte, hdr_read;
127 	int rc;
128 
129 	gpi2c_init();
130 	rc = ti_i2c_eeprom_init(bus_addr, dev_addr);
131 	if (rc)
132 		return rc;
133 
134 	/*
135 	 * Read the header first then only read the other contents.
136 	 */
137 	byte = 2;
138 
139 	rc = ti_i2c_set_alen(bus_addr, dev_addr, byte);
140 	if (rc)
141 		return rc;
142 
143 	rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
144 	if (rc)
145 		return rc;
146 
147 	/* Corrupted data??? */
148 	if (hdr_read != header) {
149 		rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
150 		/*
151 		 * read the eeprom header using i2c again, but use only a
152 		 * 1 byte address (some legacy boards need this..)
153 		 */
154 		byte = 1;
155 		if (rc) {
156 			rc = ti_i2c_set_alen(bus_addr, dev_addr, byte);
157 			if (rc)
158 				return rc;
159 
160 			rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read,
161 				      4);
162 		}
163 		if (rc)
164 			return rc;
165 	}
166 	if (hdr_read != header)
167 		return -1;
168 
169 	rc = i2c_read(dev_addr, 0x0, byte, ep, size);
170 	if (rc)
171 		return rc;
172 
173 	return 0;
174 }
175 
176 int __maybe_unused ti_i2c_eeprom_am_set(const char *name, const char *rev)
177 {
178 	struct ti_common_eeprom *ep;
179 
180 	if (!name || !rev)
181 		return -1;
182 
183 	ep = TI_EEPROM_DATA;
184 	if (ep->header == TI_EEPROM_HEADER_MAGIC)
185 		goto already_set;
186 
187 	/* Set to 0 all fields */
188 	memset(ep, 0, sizeof(*ep));
189 	strncpy(ep->name, name, TI_EEPROM_HDR_NAME_LEN);
190 	strncpy(ep->version, rev, TI_EEPROM_HDR_REV_LEN);
191 	/* Some dummy serial number to identify the platform */
192 	strncpy(ep->serial, "0000", TI_EEPROM_HDR_SERIAL_LEN);
193 	/* Mark it with a valid header */
194 	ep->header = TI_EEPROM_HEADER_MAGIC;
195 
196 already_set:
197 	return 0;
198 }
199 
200 int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
201 {
202 	int rc;
203 	struct ti_am_eeprom am_ep;
204 	struct ti_common_eeprom *ep;
205 
206 	ep = TI_EEPROM_DATA;
207 #ifndef CONFIG_SPL_BUILD
208 	if (ep->header == TI_EEPROM_HEADER_MAGIC)
209 		return 0; /* EEPROM has already been read */
210 #endif
211 
212 	/* Initialize with a known bad marker for i2c fails.. */
213 	ep->header = TI_DEAD_EEPROM_MAGIC;
214 	ep->name[0] = 0x0;
215 	ep->version[0] = 0x0;
216 	ep->serial[0] = 0x0;
217 	ep->config[0] = 0x0;
218 
219 	rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
220 			       sizeof(am_ep), (uint8_t *)&am_ep);
221 	if (rc)
222 		return rc;
223 
224 	ep->header = am_ep.header;
225 	strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
226 	ti_eeprom_string_cleanup(ep->name);
227 
228 	/* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */
229 	if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 &&
230 	    am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00)
231 		strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1);
232 	else
233 		strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1);
234 	ti_eeprom_string_cleanup(ep->version);
235 	strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
236 	ti_eeprom_string_cleanup(ep->serial);
237 	strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
238 	ti_eeprom_string_cleanup(ep->config);
239 
240 	memcpy(ep->mac_addr, am_ep.mac_addr,
241 	       TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
242 
243 	return 0;
244 }
245 
246 int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
247 {
248 	int rc, offset = 0;
249 	struct dra7_eeprom dra7_ep;
250 	struct ti_common_eeprom *ep;
251 
252 	ep = TI_EEPROM_DATA;
253 #ifndef CONFIG_SPL_BUILD
254 	if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
255 		return 0; /* EEPROM has already been read */
256 #endif
257 
258 	/* Initialize with a known bad marker for i2c fails.. */
259 	ep->header = TI_DEAD_EEPROM_MAGIC;
260 	ep->name[0] = 0x0;
261 	ep->version[0] = 0x0;
262 	ep->serial[0] = 0x0;
263 	ep->config[0] = 0x0;
264 	ep->emif1_size = 0;
265 	ep->emif2_size = 0;
266 
267 	rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
268 			       sizeof(dra7_ep), (uint8_t *)&dra7_ep);
269 	if (rc)
270 		return rc;
271 
272 	ep->header = dra7_ep.header;
273 	strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
274 	ti_eeprom_string_cleanup(ep->name);
275 
276 	offset = dra7_ep.version_major - 1;
277 
278 	/* Rev F is skipped */
279 	if (offset >= 5)
280 		offset = offset + 1;
281 	snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d",
282 		 'A' + offset, dra7_ep.version_minor);
283 	ti_eeprom_string_cleanup(ep->version);
284 	ep->emif1_size = (u64)dra7_ep.emif1_size;
285 	ep->emif2_size = (u64)dra7_ep.emif2_size;
286 	strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
287 	ti_eeprom_string_cleanup(ep->config);
288 
289 	return 0;
290 }
291 
292 bool __maybe_unused board_ti_is(char *name_tag)
293 {
294 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
295 
296 	if (ep->header == TI_DEAD_EEPROM_MAGIC)
297 		return false;
298 	return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
299 }
300 
301 bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
302 {
303 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
304 	int l;
305 
306 	if (ep->header == TI_DEAD_EEPROM_MAGIC)
307 		return false;
308 
309 	l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
310 	return !strncmp(ep->version, rev_tag, l);
311 }
312 
313 char * __maybe_unused board_ti_get_rev(void)
314 {
315 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
316 
317 	/* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
318 	return ep->version;
319 }
320 
321 char * __maybe_unused board_ti_get_config(void)
322 {
323 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
324 
325 	/* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
326 	return ep->config;
327 }
328 
329 char * __maybe_unused board_ti_get_name(void)
330 {
331 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
332 
333 	/* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
334 	return ep->name;
335 }
336 
337 void __maybe_unused
338 board_ti_get_eth_mac_addr(int index,
339 			  u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
340 {
341 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
342 
343 	if (ep->header == TI_DEAD_EEPROM_MAGIC)
344 		goto fail;
345 
346 	if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
347 		goto fail;
348 
349 	memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
350 	return;
351 
352 fail:
353 	memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
354 }
355 
356 u64 __maybe_unused board_ti_get_emif1_size(void)
357 {
358 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
359 
360 	if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
361 		return 0;
362 
363 	return ep->emif1_size;
364 }
365 
366 u64 __maybe_unused board_ti_get_emif2_size(void)
367 {
368 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
369 
370 	if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
371 		return 0;
372 
373 	return ep->emif2_size;
374 }
375 
376 void __maybe_unused set_board_info_env(char *name)
377 {
378 	char *unknown = "unknown";
379 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
380 
381 	if (name)
382 		env_set("board_name", name);
383 	else if (ep->name)
384 		env_set("board_name", ep->name);
385 	else
386 		env_set("board_name", unknown);
387 
388 	if (ep->version)
389 		env_set("board_rev", ep->version);
390 	else
391 		env_set("board_rev", unknown);
392 
393 	if (ep->serial)
394 		env_set("board_serial", ep->serial);
395 	else
396 		env_set("board_serial", unknown);
397 }
398 
399 static u64 mac_to_u64(u8 mac[6])
400 {
401 	int i;
402 	u64 addr = 0;
403 
404 	for (i = 0; i < 6; i++) {
405 		addr <<= 8;
406 		addr |= mac[i];
407 	}
408 
409 	return addr;
410 }
411 
412 static void u64_to_mac(u64 addr, u8 mac[6])
413 {
414 	mac[5] = addr;
415 	mac[4] = addr >> 8;
416 	mac[3] = addr >> 16;
417 	mac[2] = addr >> 24;
418 	mac[1] = addr >> 32;
419 	mac[0] = addr >> 40;
420 }
421 
422 void board_ti_set_ethaddr(int index)
423 {
424 	uint8_t mac_addr[6];
425 	int i;
426 	u64 mac1, mac2;
427 	u8 mac_addr1[6], mac_addr2[6];
428 	int num_macs;
429 	/*
430 	 * Export any Ethernet MAC addresses from EEPROM.
431 	 * The 2 MAC addresses in EEPROM define the address range.
432 	 */
433 	board_ti_get_eth_mac_addr(0, mac_addr1);
434 	board_ti_get_eth_mac_addr(1, mac_addr2);
435 
436 	if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) {
437 		mac1 = mac_to_u64(mac_addr1);
438 		mac2 = mac_to_u64(mac_addr2);
439 
440 		/* must contain an address range */
441 		num_macs = mac2 - mac1 + 1;
442 		if (num_macs <= 0)
443 			return;
444 
445 		if (num_macs > 50) {
446 			printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
447 			       __func__, num_macs);
448 			num_macs = 50;
449 		}
450 
451 		for (i = 0; i < num_macs; i++) {
452 			u64_to_mac(mac1 + i, mac_addr);
453 			if (is_valid_ethaddr(mac_addr)) {
454 				eth_env_set_enetaddr_by_index("eth", i + index,
455 							      mac_addr);
456 			}
457 		}
458 	}
459 }
460 
461 bool __maybe_unused board_ti_was_eeprom_read(void)
462 {
463 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
464 
465 	if (ep->header == TI_EEPROM_HEADER_MAGIC)
466 		return true;
467 	else
468 		return false;
469 }
470