xref: /rk3399_rockchip-uboot/board/ti/common/board_detect.c (revision c6b80b13924adc9b72fb03c368c2e39b85131a91)
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 	return i2c_read(dev_addr, offset, 2, ep, epsize);
87 }
88 
89 /**
90  * ti_eeprom_string_cleanup() - Handle eeprom programming errors
91  * @s:	eeprom string (should be NULL terminated)
92  *
93  * Some Board manufacturers do not add a NULL termination at the
94  * end of string, instead some binary information is kludged in, hence
95  * convert the string to just printable characters of ASCII chart.
96  */
97 static void __maybe_unused ti_eeprom_string_cleanup(char *s)
98 {
99 	int i, l;
100 
101 	l = strlen(s);
102 	for (i = 0; i < l; i++, s++)
103 		if (*s < ' ' || *s > '~') {
104 			*s = 0;
105 			break;
106 		}
107 }
108 
109 __weak void gpi2c_init(void)
110 {
111 }
112 
113 static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr,
114 					    u32 header, u32 size, uint8_t *ep)
115 {
116 	u32 byte, hdr_read;
117 	int rc;
118 
119 	gpi2c_init();
120 	rc = ti_i2c_eeprom_init(bus_addr, dev_addr);
121 	if (rc)
122 		return rc;
123 
124 	/*
125 	 * Read the header first then only read the other contents.
126 	 */
127 	byte = 2;
128 	rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
129 	if (rc)
130 		return rc;
131 
132 	/* Corrupted data??? */
133 	if (hdr_read != header) {
134 		rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
135 		/*
136 		 * read the eeprom header using i2c again, but use only a
137 		 * 1 byte address (some legacy boards need this..)
138 		 */
139 		byte = 1;
140 		if (rc)
141 			rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read,
142 				      4);
143 		if (rc)
144 			return rc;
145 	}
146 	if (hdr_read != header)
147 		return -1;
148 
149 	rc = i2c_read(dev_addr, 0x0, byte, ep, size);
150 	if (rc)
151 		return rc;
152 
153 	return 0;
154 }
155 
156 int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
157 {
158 	int rc;
159 	struct ti_am_eeprom am_ep;
160 	struct ti_common_eeprom *ep;
161 
162 	ep = TI_EEPROM_DATA;
163 #ifndef CONFIG_SPL_BUILD
164 	if (ep->header == TI_EEPROM_HEADER_MAGIC)
165 		return 0; /* EEPROM has already been read */
166 #endif
167 
168 	/* Initialize with a known bad marker for i2c fails.. */
169 	ep->header = TI_DEAD_EEPROM_MAGIC;
170 	ep->name[0] = 0x0;
171 	ep->version[0] = 0x0;
172 	ep->serial[0] = 0x0;
173 	ep->config[0] = 0x0;
174 
175 	rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
176 			       sizeof(am_ep), (uint8_t *)&am_ep);
177 	if (rc)
178 		return rc;
179 
180 	ep->header = am_ep.header;
181 	strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
182 	ti_eeprom_string_cleanup(ep->name);
183 
184 	/* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */
185 	if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 &&
186 	    am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00)
187 		strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1);
188 	else
189 		strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1);
190 	ti_eeprom_string_cleanup(ep->version);
191 	strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
192 	ti_eeprom_string_cleanup(ep->serial);
193 	strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
194 	ti_eeprom_string_cleanup(ep->config);
195 
196 	memcpy(ep->mac_addr, am_ep.mac_addr,
197 	       TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
198 
199 	return 0;
200 }
201 
202 int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
203 {
204 	int rc, offset = 0;
205 	struct dra7_eeprom dra7_ep;
206 	struct ti_common_eeprom *ep;
207 
208 	ep = TI_EEPROM_DATA;
209 #ifndef CONFIG_SPL_BUILD
210 	if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
211 		return 0; /* EEPROM has already been read */
212 #endif
213 
214 	/* Initialize with a known bad marker for i2c fails.. */
215 	ep->header = TI_DEAD_EEPROM_MAGIC;
216 	ep->name[0] = 0x0;
217 	ep->version[0] = 0x0;
218 	ep->serial[0] = 0x0;
219 	ep->config[0] = 0x0;
220 	ep->emif1_size = 0;
221 	ep->emif2_size = 0;
222 
223 	rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
224 			       sizeof(dra7_ep), (uint8_t *)&dra7_ep);
225 	if (rc)
226 		return rc;
227 
228 	ep->header = dra7_ep.header;
229 	strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
230 	ti_eeprom_string_cleanup(ep->name);
231 
232 	offset = dra7_ep.version_major - 1;
233 
234 	/* Rev F is skipped */
235 	if (offset >= 5)
236 		offset = offset + 1;
237 	snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d",
238 		 'A' + offset, dra7_ep.version_minor);
239 	ti_eeprom_string_cleanup(ep->version);
240 	ep->emif1_size = (u64)dra7_ep.emif1_size;
241 	ep->emif2_size = (u64)dra7_ep.emif2_size;
242 	strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
243 	ti_eeprom_string_cleanup(ep->config);
244 
245 	return 0;
246 }
247 
248 bool __maybe_unused board_ti_is(char *name_tag)
249 {
250 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
251 
252 	if (ep->header == TI_DEAD_EEPROM_MAGIC)
253 		return false;
254 	return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
255 }
256 
257 bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
258 {
259 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
260 	int l;
261 
262 	if (ep->header == TI_DEAD_EEPROM_MAGIC)
263 		return false;
264 
265 	l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
266 	return !strncmp(ep->version, rev_tag, l);
267 }
268 
269 char * __maybe_unused board_ti_get_rev(void)
270 {
271 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
272 
273 	/* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
274 	return ep->version;
275 }
276 
277 char * __maybe_unused board_ti_get_config(void)
278 {
279 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
280 
281 	/* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
282 	return ep->config;
283 }
284 
285 char * __maybe_unused board_ti_get_name(void)
286 {
287 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
288 
289 	/* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
290 	return ep->name;
291 }
292 
293 void __maybe_unused
294 board_ti_get_eth_mac_addr(int index,
295 			  u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
296 {
297 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
298 
299 	if (ep->header == TI_DEAD_EEPROM_MAGIC)
300 		goto fail;
301 
302 	if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
303 		goto fail;
304 
305 	memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
306 	return;
307 
308 fail:
309 	memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
310 }
311 
312 u64 __maybe_unused board_ti_get_emif1_size(void)
313 {
314 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
315 
316 	if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
317 		return 0;
318 
319 	return ep->emif1_size;
320 }
321 
322 u64 __maybe_unused board_ti_get_emif2_size(void)
323 {
324 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
325 
326 	if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
327 		return 0;
328 
329 	return ep->emif2_size;
330 }
331 
332 void __maybe_unused set_board_info_env(char *name)
333 {
334 	char *unknown = "unknown";
335 	struct ti_common_eeprom *ep = TI_EEPROM_DATA;
336 
337 	if (name)
338 		setenv("board_name", name);
339 	else if (ep->name)
340 		setenv("board_name", ep->name);
341 	else
342 		setenv("board_name", unknown);
343 
344 	if (ep->version)
345 		setenv("board_rev", ep->version);
346 	else
347 		setenv("board_rev", unknown);
348 
349 	if (ep->serial)
350 		setenv("board_serial", ep->serial);
351 	else
352 		setenv("board_serial", unknown);
353 }
354 
355 static u64 mac_to_u64(u8 mac[6])
356 {
357 	int i;
358 	u64 addr = 0;
359 
360 	for (i = 0; i < 6; i++) {
361 		addr <<= 8;
362 		addr |= mac[i];
363 	}
364 
365 	return addr;
366 }
367 
368 static void u64_to_mac(u64 addr, u8 mac[6])
369 {
370 	mac[5] = addr;
371 	mac[4] = addr >> 8;
372 	mac[3] = addr >> 16;
373 	mac[2] = addr >> 24;
374 	mac[1] = addr >> 32;
375 	mac[0] = addr >> 40;
376 }
377 
378 void board_ti_set_ethaddr(int index)
379 {
380 	uint8_t mac_addr[6];
381 	int i;
382 	u64 mac1, mac2;
383 	u8 mac_addr1[6], mac_addr2[6];
384 	int num_macs;
385 	/*
386 	 * Export any Ethernet MAC addresses from EEPROM.
387 	 * The 2 MAC addresses in EEPROM define the address range.
388 	 */
389 	board_ti_get_eth_mac_addr(0, mac_addr1);
390 	board_ti_get_eth_mac_addr(1, mac_addr2);
391 
392 	if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) {
393 		mac1 = mac_to_u64(mac_addr1);
394 		mac2 = mac_to_u64(mac_addr2);
395 
396 		/* must contain an address range */
397 		num_macs = mac2 - mac1 + 1;
398 		if (num_macs <= 0)
399 			return;
400 
401 		if (num_macs > 50) {
402 			printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
403 			       __func__, num_macs);
404 			num_macs = 50;
405 		}
406 
407 		for (i = 0; i < num_macs; i++) {
408 			u64_to_mac(mac1 + i, mac_addr);
409 			if (is_valid_ethaddr(mac_addr)) {
410 				eth_setenv_enetaddr_by_index("eth", i + index,
411 							     mac_addr);
412 			}
413 		}
414 	}
415 }
416