blob: 6fdcb6172c0dea5ff2ce01cbdfd570fa45a1e1be [file] [log] [blame]
Lokesh Vutla0bea8132016-02-24 12:30:54 -06001/*
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>
Cooper Jr., Franklinc6b80b12017-04-20 10:25:44 -050013#include <dm/uclass.h>
Lokesh Vutla0bea8132016-02-24 12:30:54 -060014#include <i2c.h>
15
16#include "board_detect.h"
17
Cooper Jr., Franklinc6b80b12017-04-20 10:25:44 -050018#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 */
29int __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
48int __maybe_unused ti_i2c_set_alen(int bus_addr, int dev_addr, int alen)
49{
50 return 0;
51}
52#endif
53
Lokesh Vutla0bea8132016-02-24 12:30:54 -060054/**
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 */
61static 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 */
83static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset,
84 uchar *ep, int epsize)
85{
Cooper Jr., Frankline25ae322017-04-20 10:25:45 -050086 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);
Lokesh Vutla0bea8132016-02-24 12:30:54 -060097}
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 */
107static 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
123static 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;
Cooper Jr., Frankline25ae322017-04-20 10:25:45 -0500138
139 rc = ti_i2c_set_alen(bus_addr, dev_addr, byte);
140 if (rc)
141 return rc;
142
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600143 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;
Cooper Jr., Frankline25ae322017-04-20 10:25:45 -0500155 if (rc) {
156 rc = ti_i2c_set_alen(bus_addr, dev_addr, byte);
157 if (rc)
158 return rc;
159
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600160 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read,
161 4);
Cooper Jr., Frankline25ae322017-04-20 10:25:45 -0500162 }
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600163 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
176int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
177{
178 int rc;
179 struct ti_am_eeprom am_ep;
180 struct ti_common_eeprom *ep;
181
182 ep = TI_EEPROM_DATA;
Jean-Jacques Hiblota3a23c92016-12-01 10:37:03 +0100183#ifndef CONFIG_SPL_BUILD
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600184 if (ep->header == TI_EEPROM_HEADER_MAGIC)
Jean-Jacques Hiblota3a23c92016-12-01 10:37:03 +0100185 return 0; /* EEPROM has already been read */
186#endif
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600187
188 /* Initialize with a known bad marker for i2c fails.. */
189 ep->header = TI_DEAD_EEPROM_MAGIC;
190 ep->name[0] = 0x0;
191 ep->version[0] = 0x0;
192 ep->serial[0] = 0x0;
Nishanth Menon2a78c9e2016-10-11 12:39:04 -0500193 ep->config[0] = 0x0;
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600194
195 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
196 sizeof(am_ep), (uint8_t *)&am_ep);
197 if (rc)
198 return rc;
199
200 ep->header = am_ep.header;
201 strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
202 ti_eeprom_string_cleanup(ep->name);
203
204 /* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */
205 if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 &&
206 am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00)
207 strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1);
208 else
209 strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1);
210 ti_eeprom_string_cleanup(ep->version);
211 strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
212 ti_eeprom_string_cleanup(ep->serial);
213 strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
214 ti_eeprom_string_cleanup(ep->config);
215
216 memcpy(ep->mac_addr, am_ep.mac_addr,
217 TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
218
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600219 return 0;
220}
221
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530222int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
223{
224 int rc, offset = 0;
225 struct dra7_eeprom dra7_ep;
226 struct ti_common_eeprom *ep;
227
228 ep = TI_EEPROM_DATA;
Jean-Jacques Hiblota3a23c92016-12-01 10:37:03 +0100229#ifndef CONFIG_SPL_BUILD
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530230 if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
Jean-Jacques Hiblota3a23c92016-12-01 10:37:03 +0100231 return 0; /* EEPROM has already been read */
232#endif
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530233
234 /* Initialize with a known bad marker for i2c fails.. */
Nishanth Menon28d624b2016-10-11 12:39:03 -0500235 ep->header = TI_DEAD_EEPROM_MAGIC;
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530236 ep->name[0] = 0x0;
237 ep->version[0] = 0x0;
238 ep->serial[0] = 0x0;
Nishanth Menon2a78c9e2016-10-11 12:39:04 -0500239 ep->config[0] = 0x0;
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530240 ep->emif1_size = 0;
241 ep->emif2_size = 0;
242
243 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
244 sizeof(dra7_ep), (uint8_t *)&dra7_ep);
245 if (rc)
246 return rc;
247
248 ep->header = dra7_ep.header;
249 strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
250 ti_eeprom_string_cleanup(ep->name);
251
252 offset = dra7_ep.version_major - 1;
253
254 /* Rev F is skipped */
255 if (offset >= 5)
256 offset = offset + 1;
257 snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d",
258 'A' + offset, dra7_ep.version_minor);
259 ti_eeprom_string_cleanup(ep->version);
260 ep->emif1_size = (u64)dra7_ep.emif1_size;
261 ep->emif2_size = (u64)dra7_ep.emif2_size;
262 strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
263 ti_eeprom_string_cleanup(ep->config);
264
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530265 return 0;
266}
267
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600268bool __maybe_unused board_ti_is(char *name_tag)
269{
270 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
271
272 if (ep->header == TI_DEAD_EEPROM_MAGIC)
273 return false;
274 return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
275}
276
277bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
278{
279 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
280 int l;
281
282 if (ep->header == TI_DEAD_EEPROM_MAGIC)
283 return false;
284
285 l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
286 return !strncmp(ep->version, rev_tag, l);
287}
288
289char * __maybe_unused board_ti_get_rev(void)
290{
291 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
292
Nishanth Menon7774e972016-10-11 12:39:05 -0500293 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600294 return ep->version;
295}
296
297char * __maybe_unused board_ti_get_config(void)
298{
299 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
300
Nishanth Menon7774e972016-10-11 12:39:05 -0500301 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600302 return ep->config;
303}
304
305char * __maybe_unused board_ti_get_name(void)
306{
307 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
308
Nishanth Menon7774e972016-10-11 12:39:05 -0500309 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600310 return ep->name;
311}
312
313void __maybe_unused
314board_ti_get_eth_mac_addr(int index,
315 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
316{
317 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
318
319 if (ep->header == TI_DEAD_EEPROM_MAGIC)
320 goto fail;
321
322 if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
323 goto fail;
324
325 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
326 return;
327
328fail:
329 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
330}
331
Lokesh Vutlad3b98a92016-03-08 09:18:04 +0530332u64 __maybe_unused board_ti_get_emif1_size(void)
333{
334 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
335
336 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
337 return 0;
338
339 return ep->emif1_size;
340}
341
342u64 __maybe_unused board_ti_get_emif2_size(void)
343{
344 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
345
346 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
347 return 0;
348
349 return ep->emif2_size;
350}
351
Lokesh Vutla0bea8132016-02-24 12:30:54 -0600352void __maybe_unused set_board_info_env(char *name)
353{
354 char *unknown = "unknown";
355 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
356
357 if (name)
358 setenv("board_name", name);
359 else if (ep->name)
360 setenv("board_name", ep->name);
361 else
362 setenv("board_name", unknown);
363
364 if (ep->version)
365 setenv("board_rev", ep->version);
366 else
367 setenv("board_rev", unknown);
368
369 if (ep->serial)
370 setenv("board_serial", ep->serial);
371 else
372 setenv("board_serial", unknown);
373}
Roger Quadros38f719e2017-03-14 15:04:19 +0200374
375static u64 mac_to_u64(u8 mac[6])
376{
377 int i;
378 u64 addr = 0;
379
380 for (i = 0; i < 6; i++) {
381 addr <<= 8;
382 addr |= mac[i];
383 }
384
385 return addr;
386}
387
388static void u64_to_mac(u64 addr, u8 mac[6])
389{
390 mac[5] = addr;
391 mac[4] = addr >> 8;
392 mac[3] = addr >> 16;
393 mac[2] = addr >> 24;
394 mac[1] = addr >> 32;
395 mac[0] = addr >> 40;
396}
397
398void board_ti_set_ethaddr(int index)
399{
400 uint8_t mac_addr[6];
401 int i;
402 u64 mac1, mac2;
403 u8 mac_addr1[6], mac_addr2[6];
404 int num_macs;
405 /*
406 * Export any Ethernet MAC addresses from EEPROM.
407 * The 2 MAC addresses in EEPROM define the address range.
408 */
409 board_ti_get_eth_mac_addr(0, mac_addr1);
410 board_ti_get_eth_mac_addr(1, mac_addr2);
411
412 if (is_valid_ethaddr(mac_addr1) && is_valid_ethaddr(mac_addr2)) {
413 mac1 = mac_to_u64(mac_addr1);
414 mac2 = mac_to_u64(mac_addr2);
415
416 /* must contain an address range */
417 num_macs = mac2 - mac1 + 1;
418 if (num_macs <= 0)
419 return;
420
421 if (num_macs > 50) {
422 printf("%s: Too many MAC addresses: %d. Limiting to 50\n",
423 __func__, num_macs);
424 num_macs = 50;
425 }
426
427 for (i = 0; i < num_macs; i++) {
428 u64_to_mac(mac1 + i, mac_addr);
429 if (is_valid_ethaddr(mac_addr)) {
430 eth_setenv_enetaddr_by_index("eth", i + index,
431 mac_addr);
432 }
433 }
434 }
435}