blob: 6e7ca9196d22dde2a641b75d37e25002437575a8 [file] [log] [blame]
Lokesh Vutla35f7a972016-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>
13#include <i2c.h>
14
15#include "board_detect.h"
16
17/**
18 * ti_i2c_eeprom_init - Initialize an i2c bus and probe for a device
19 * @i2c_bus: i2c bus number to initialize
20 * @dev_addr: Device address to probe for
21 *
22 * Return: 0 on success or corresponding error on failure.
23 */
24static int __maybe_unused ti_i2c_eeprom_init(int i2c_bus, int dev_addr)
25{
26 int rc;
27
28 if (i2c_bus >= 0) {
29 rc = i2c_set_bus_num(i2c_bus);
30 if (rc)
31 return rc;
32 }
33
34 return i2c_probe(dev_addr);
35}
36
37/**
38 * ti_i2c_eeprom_read - Read data from an EEPROM
39 * @dev_addr: The device address of the EEPROM
40 * @offset: Offset to start reading in the EEPROM
41 * @ep: Pointer to a buffer to read into
42 * @epsize: Size of buffer
43 *
44 * Return: 0 on success or corresponding result of i2c_read
45 */
46static int __maybe_unused ti_i2c_eeprom_read(int dev_addr, int offset,
47 uchar *ep, int epsize)
48{
49 return i2c_read(dev_addr, offset, 2, ep, epsize);
50}
51
52/**
53 * ti_eeprom_string_cleanup() - Handle eeprom programming errors
54 * @s: eeprom string (should be NULL terminated)
55 *
56 * Some Board manufacturers do not add a NULL termination at the
57 * end of string, instead some binary information is kludged in, hence
58 * convert the string to just printable characters of ASCII chart.
59 */
60static void __maybe_unused ti_eeprom_string_cleanup(char *s)
61{
62 int i, l;
63
64 l = strlen(s);
65 for (i = 0; i < l; i++, s++)
66 if (*s < ' ' || *s > '~') {
67 *s = 0;
68 break;
69 }
70}
71
72__weak void gpi2c_init(void)
73{
74}
75
76static int __maybe_unused ti_i2c_eeprom_get(int bus_addr, int dev_addr,
77 u32 header, u32 size, uint8_t *ep)
78{
79 u32 byte, hdr_read;
80 int rc;
81
82 gpi2c_init();
83 rc = ti_i2c_eeprom_init(bus_addr, dev_addr);
84 if (rc)
85 return rc;
86
87 /*
88 * Read the header first then only read the other contents.
89 */
90 byte = 2;
91 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
92 if (rc)
93 return rc;
94
95 /* Corrupted data??? */
96 if (hdr_read != header) {
97 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read, 4);
98 /*
99 * read the eeprom header using i2c again, but use only a
100 * 1 byte address (some legacy boards need this..)
101 */
102 byte = 1;
103 if (rc)
104 rc = i2c_read(dev_addr, 0x0, byte, (uint8_t *)&hdr_read,
105 4);
106 if (rc)
107 return rc;
108 }
109 if (hdr_read != header)
110 return -1;
111
112 rc = i2c_read(dev_addr, 0x0, byte, ep, size);
113 if (rc)
114 return rc;
115
116 return 0;
117}
118
119int __maybe_unused ti_i2c_eeprom_am_get(int bus_addr, int dev_addr)
120{
121 int rc;
122 struct ti_am_eeprom am_ep;
123 struct ti_common_eeprom *ep;
124
125 ep = TI_EEPROM_DATA;
126 if (ep->header == TI_EEPROM_HEADER_MAGIC)
127 goto already_read;
128
129 /* Initialize with a known bad marker for i2c fails.. */
130 ep->header = TI_DEAD_EEPROM_MAGIC;
131 ep->name[0] = 0x0;
132 ep->version[0] = 0x0;
133 ep->serial[0] = 0x0;
Nishanth Menone2b0a522016-10-11 12:39:04 -0500134 ep->config[0] = 0x0;
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600135
136 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, TI_EEPROM_HEADER_MAGIC,
137 sizeof(am_ep), (uint8_t *)&am_ep);
138 if (rc)
139 return rc;
140
141 ep->header = am_ep.header;
142 strlcpy(ep->name, am_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
143 ti_eeprom_string_cleanup(ep->name);
144
145 /* BeagleBone Green '1' eeprom, board_rev: 0x1a 0x00 0x00 0x00 */
146 if (am_ep.version[0] == 0x1a && am_ep.version[1] == 0x00 &&
147 am_ep.version[2] == 0x00 && am_ep.version[3] == 0x00)
148 strlcpy(ep->version, "BBG1", TI_EEPROM_HDR_REV_LEN + 1);
149 else
150 strlcpy(ep->version, am_ep.version, TI_EEPROM_HDR_REV_LEN + 1);
151 ti_eeprom_string_cleanup(ep->version);
152 strlcpy(ep->serial, am_ep.serial, TI_EEPROM_HDR_SERIAL_LEN + 1);
153 ti_eeprom_string_cleanup(ep->serial);
154 strlcpy(ep->config, am_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
155 ti_eeprom_string_cleanup(ep->config);
156
157 memcpy(ep->mac_addr, am_ep.mac_addr,
158 TI_EEPROM_HDR_NO_OF_MAC_ADDR * TI_EEPROM_HDR_ETH_ALEN);
159
160already_read:
161 return 0;
162}
163
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530164int __maybe_unused ti_i2c_eeprom_dra7_get(int bus_addr, int dev_addr)
165{
166 int rc, offset = 0;
167 struct dra7_eeprom dra7_ep;
168 struct ti_common_eeprom *ep;
169
170 ep = TI_EEPROM_DATA;
171 if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
172 goto already_read;
173
174 /* Initialize with a known bad marker for i2c fails.. */
Nishanth Menon21e54c02016-10-11 12:39:03 -0500175 ep->header = TI_DEAD_EEPROM_MAGIC;
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530176 ep->name[0] = 0x0;
177 ep->version[0] = 0x0;
178 ep->serial[0] = 0x0;
Nishanth Menone2b0a522016-10-11 12:39:04 -0500179 ep->config[0] = 0x0;
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530180 ep->emif1_size = 0;
181 ep->emif2_size = 0;
182
183 rc = ti_i2c_eeprom_get(bus_addr, dev_addr, DRA7_EEPROM_HEADER_MAGIC,
184 sizeof(dra7_ep), (uint8_t *)&dra7_ep);
185 if (rc)
186 return rc;
187
188 ep->header = dra7_ep.header;
189 strlcpy(ep->name, dra7_ep.name, TI_EEPROM_HDR_NAME_LEN + 1);
190 ti_eeprom_string_cleanup(ep->name);
191
192 offset = dra7_ep.version_major - 1;
193
194 /* Rev F is skipped */
195 if (offset >= 5)
196 offset = offset + 1;
197 snprintf(ep->version, TI_EEPROM_HDR_REV_LEN + 1, "%c.%d",
198 'A' + offset, dra7_ep.version_minor);
199 ti_eeprom_string_cleanup(ep->version);
200 ep->emif1_size = (u64)dra7_ep.emif1_size;
201 ep->emif2_size = (u64)dra7_ep.emif2_size;
202 strlcpy(ep->config, dra7_ep.config, TI_EEPROM_HDR_CONFIG_LEN + 1);
203 ti_eeprom_string_cleanup(ep->config);
204
205already_read:
206 return 0;
207}
208
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600209bool __maybe_unused board_ti_is(char *name_tag)
210{
211 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
212
213 if (ep->header == TI_DEAD_EEPROM_MAGIC)
214 return false;
215 return !strncmp(ep->name, name_tag, TI_EEPROM_HDR_NAME_LEN);
216}
217
218bool __maybe_unused board_ti_rev_is(char *rev_tag, int cmp_len)
219{
220 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
221 int l;
222
223 if (ep->header == TI_DEAD_EEPROM_MAGIC)
224 return false;
225
226 l = cmp_len > TI_EEPROM_HDR_REV_LEN ? TI_EEPROM_HDR_REV_LEN : cmp_len;
227 return !strncmp(ep->version, rev_tag, l);
228}
229
230char * __maybe_unused board_ti_get_rev(void)
231{
232 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
233
Nishanth Menon23afc8a2016-10-11 12:39:05 -0500234 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600235 return ep->version;
236}
237
238char * __maybe_unused board_ti_get_config(void)
239{
240 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
241
Nishanth Menon23afc8a2016-10-11 12:39:05 -0500242 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600243 return ep->config;
244}
245
246char * __maybe_unused board_ti_get_name(void)
247{
248 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
249
Nishanth Menon23afc8a2016-10-11 12:39:05 -0500250 /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600251 return ep->name;
252}
253
254void __maybe_unused
255board_ti_get_eth_mac_addr(int index,
256 u8 mac_addr[TI_EEPROM_HDR_ETH_ALEN])
257{
258 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
259
260 if (ep->header == TI_DEAD_EEPROM_MAGIC)
261 goto fail;
262
263 if (index < 0 || index >= TI_EEPROM_HDR_NO_OF_MAC_ADDR)
264 goto fail;
265
266 memcpy(mac_addr, ep->mac_addr[index], TI_EEPROM_HDR_ETH_ALEN);
267 return;
268
269fail:
270 memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
271}
272
Lokesh Vutla7c08a782016-03-08 09:18:04 +0530273u64 __maybe_unused board_ti_get_emif1_size(void)
274{
275 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
276
277 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
278 return 0;
279
280 return ep->emif1_size;
281}
282
283u64 __maybe_unused board_ti_get_emif2_size(void)
284{
285 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
286
287 if (ep->header != DRA7_EEPROM_HEADER_MAGIC)
288 return 0;
289
290 return ep->emif2_size;
291}
292
Lokesh Vutla35f7a972016-02-24 12:30:54 -0600293void __maybe_unused set_board_info_env(char *name)
294{
295 char *unknown = "unknown";
296 struct ti_common_eeprom *ep = TI_EEPROM_DATA;
297
298 if (name)
299 setenv("board_name", name);
300 else if (ep->name)
301 setenv("board_name", ep->name);
302 else
303 setenv("board_name", unknown);
304
305 if (ep->version)
306 setenv("board_rev", ep->version);
307 else
308 setenv("board_rev", unknown);
309
310 if (ep->serial)
311 setenv("board_serial", ep->serial);
312 else
313 setenv("board_serial", unknown);
314}