Merge branch 'master' of git://git.denx.de/u-boot-i2c
[platform/kernel/u-boot.git] / board / ti / common / board_detect.c
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_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;
183 #ifndef CONFIG_SPL_BUILD
184         if (ep->header == TI_EEPROM_HEADER_MAGIC)
185                 return 0; /* EEPROM has already been read */
186 #endif
187
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;
193         ep->config[0] = 0x0;
194
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
219         return 0;
220 }
221
222 int __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;
229 #ifndef CONFIG_SPL_BUILD
230         if (ep->header == DRA7_EEPROM_HEADER_MAGIC)
231                 return 0; /* EEPROM has already been read */
232 #endif
233
234         /* Initialize with a known bad marker for i2c fails.. */
235         ep->header = TI_DEAD_EEPROM_MAGIC;
236         ep->name[0] = 0x0;
237         ep->version[0] = 0x0;
238         ep->serial[0] = 0x0;
239         ep->config[0] = 0x0;
240         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
265         return 0;
266 }
267
268 bool __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
277 bool __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
289 char * __maybe_unused board_ti_get_rev(void)
290 {
291         struct ti_common_eeprom *ep = TI_EEPROM_DATA;
292
293         /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
294         return ep->version;
295 }
296
297 char * __maybe_unused board_ti_get_config(void)
298 {
299         struct ti_common_eeprom *ep = TI_EEPROM_DATA;
300
301         /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
302         return ep->config;
303 }
304
305 char * __maybe_unused board_ti_get_name(void)
306 {
307         struct ti_common_eeprom *ep = TI_EEPROM_DATA;
308
309         /* if ep->header == TI_DEAD_EEPROM_MAGIC, this is empty already */
310         return ep->name;
311 }
312
313 void __maybe_unused
314 board_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
328 fail:
329         memset(mac_addr, 0, TI_EEPROM_HDR_ETH_ALEN);
330 }
331
332 u64 __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
342 u64 __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
352 void __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 }
374
375 static 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
388 static 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
398 void 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 }