aa2240b8523b9c8498ff74a07dfe39ff287e40f5
[oweals/u-boot.git] / board / ti / j721e / evm.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Board specific initialization for J721E EVM
4  *
5  * Copyright (C) 2018-2019 Texas Instruments Incorporated - http://www.ti.com/
6  *      Lokesh Vutla <lokeshvutla@ti.com>
7  *
8  */
9
10 #include <common.h>
11 #include <init.h>
12 #include <asm/arch/sys_proto.h>
13 #include <asm/arch/hardware.h>
14 #include <asm/gpio.h>
15 #include <asm/io.h>
16 #include <spl.h>
17 #include <asm/arch/sys_proto.h>
18
19 #include "../common/board_detect.h"
20
21 #define board_is_j721e_som()    (board_ti_k3_is("J721EX-PM1-SOM") || \
22                                  board_ti_k3_is("J721EX-PM2-SOM"))
23
24 /* Max number of MAC addresses that are parsed/processed per daughter card */
25 #define DAUGHTER_CARD_NO_OF_MAC_ADDR    8
26
27 DECLARE_GLOBAL_DATA_PTR;
28
29 int board_init(void)
30 {
31         return 0;
32 }
33
34 int dram_init(void)
35 {
36 #ifdef CONFIG_PHYS_64BIT
37         gd->ram_size = 0x100000000;
38 #else
39         gd->ram_size = 0x80000000;
40 #endif
41
42         return 0;
43 }
44
45 ulong board_get_usable_ram_top(ulong total_size)
46 {
47 #ifdef CONFIG_PHYS_64BIT
48         /* Limit RAM used by U-Boot to the DDR low region */
49         if (gd->ram_top > 0x100000000)
50                 return 0x100000000;
51 #endif
52
53         return gd->ram_top;
54 }
55
56 int dram_init_banksize(void)
57 {
58         /* Bank 0 declares the memory available in the DDR low region */
59         gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
60         gd->bd->bi_dram[0].size = 0x80000000;
61         gd->ram_size = 0x80000000;
62
63 #ifdef CONFIG_PHYS_64BIT
64         /* Bank 1 declares the memory available in the DDR high region */
65         gd->bd->bi_dram[1].start = CONFIG_SYS_SDRAM_BASE1;
66         gd->bd->bi_dram[1].size = 0x80000000;
67         gd->ram_size = 0x100000000;
68 #endif
69
70         return 0;
71 }
72
73 #ifdef CONFIG_SPL_LOAD_FIT
74 int board_fit_config_name_match(const char *name)
75 {
76         if (!strcmp(name, "k3-j721e-common-proc-board"))
77                 return 0;
78
79         return -1;
80 }
81 #endif
82
83 #if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
84 int ft_board_setup(void *blob, bd_t *bd)
85 {
86         int ret;
87
88         ret = fdt_fixup_msmc_ram(blob, "/interconnect@100000", "sram@70000000");
89         if (ret)
90                 printf("%s: fixing up msmc ram failed %d\n", __func__, ret);
91
92         return ret;
93 }
94 #endif
95
96 int do_board_detect(void)
97 {
98         int ret;
99
100         ret = ti_i2c_eeprom_am6_get_base(CONFIG_EEPROM_BUS_ADDRESS,
101                                          CONFIG_EEPROM_CHIP_ADDRESS);
102         if (ret)
103                 pr_err("Reading on-board EEPROM at 0x%02x failed %d\n",
104                        CONFIG_EEPROM_CHIP_ADDRESS, ret);
105
106         return ret;
107 }
108
109 int checkboard(void)
110 {
111         struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
112
113         if (do_board_detect())
114                 /* EEPROM not populated */
115                 printf("Board: %s rev %s\n", "J721EX-PM1-SOM", "E2");
116         else
117                 printf("Board: %s rev %s\n", ep->name, ep->version);
118
119         return 0;
120 }
121
122 static void setup_board_eeprom_env(void)
123 {
124         char *name = "j721e";
125
126         if (do_board_detect())
127                 goto invalid_eeprom;
128
129         if (board_is_j721e_som())
130                 name = "j721e";
131         else
132                 printf("Unidentified board claims %s in eeprom header\n",
133                        board_ti_get_name());
134
135 invalid_eeprom:
136         set_board_info_env_am6(name);
137 }
138
139 static void setup_serial(void)
140 {
141         struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
142         unsigned long board_serial;
143         char *endp;
144         char serial_string[17] = { 0 };
145
146         if (env_get("serial#"))
147                 return;
148
149         board_serial = simple_strtoul(ep->serial, &endp, 16);
150         if (*endp != '\0') {
151                 pr_err("Error: Can't set serial# to %s\n", ep->serial);
152                 return;
153         }
154
155         snprintf(serial_string, sizeof(serial_string), "%016lx", board_serial);
156         env_set("serial#", serial_string);
157 }
158
159 /*
160  * Declaration of daughtercards to probe. Note that when adding more
161  * cards they should be grouped by the 'i2c_addr' field to allow for a
162  * more efficient probing process.
163  */
164 static const struct {
165         u8 i2c_addr;            /* I2C address of card EEPROM */
166         char *card_name;        /* EEPROM-programmed card name */
167         char *dtbo_name;        /* Device tree overlay to apply */
168         u8 eth_offset;          /* ethXaddr MAC address index offset */
169 } ext_cards[] = {
170         {
171                 0x51,
172                 "J7X-BASE-CPB",
173                 "",             /* No dtbo for this board */
174                 0,
175         },
176         {
177                 0x52,
178                 "J7X-INFOTAN-EXP",
179                 "",             /* No dtbo for this board */
180                 0,
181         },
182         {
183                 0x52,
184                 "J7X-GESI-EXP",
185                 "",             /* No dtbo for this board */
186                 5,              /* Start populating from eth5addr */
187         },
188         {
189                 0x54,
190                 "J7X-VSC8514-ETH",
191                 "",             /* No dtbo for this board */
192                 1,              /* Start populating from eth1addr */
193         },
194 };
195
196 static bool daughter_card_detect_flags[ARRAY_SIZE(ext_cards)];
197
198 const char *board_fit_get_additionnal_images(int index, const char *type)
199 {
200         int i, j;
201
202         if (strcmp(type, FIT_FDT_PROP))
203                 return NULL;
204
205         j = 0;
206         for (i = 0; i < ARRAY_SIZE(ext_cards); i++) {
207                 if (daughter_card_detect_flags[i]) {
208                         if (j == index) {
209                                 /*
210                                  * Return dtbo name only if populated,
211                                  * otherwise stop parsing here.
212                                  */
213                                 if (strlen(ext_cards[i].dtbo_name))
214                                         return ext_cards[i].dtbo_name;
215                                 else
216                                         return NULL;
217                         };
218
219                         j++;
220                 }
221         }
222
223         return NULL;
224 }
225
226 static int probe_daughtercards(void)
227 {
228         char mac_addr[DAUGHTER_CARD_NO_OF_MAC_ADDR][TI_EEPROM_HDR_ETH_ALEN];
229         bool eeprom_read_success;
230         struct ti_am6_eeprom ep;
231         u8 previous_i2c_addr;
232         u8 mac_addr_cnt;
233         int i;
234         int ret;
235
236         /* Mark previous I2C address variable as not populated */
237         previous_i2c_addr = 0xff;
238
239         /* No EEPROM data was read yet */
240         eeprom_read_success = false;
241
242         /* Iterate through list of daughtercards */
243         for (i = 0; i < ARRAY_SIZE(ext_cards); i++) {
244                 /* Obtain card-specific I2C address */
245                 u8 i2c_addr = ext_cards[i].i2c_addr;
246
247                 /* Read card EEPROM if not already read previously */
248                 if (i2c_addr != previous_i2c_addr) {
249                         /* Store I2C address so we can avoid reading twice */
250                         previous_i2c_addr = i2c_addr;
251
252                         /* Get and parse the daughter card EEPROM record */
253                         ret = ti_i2c_eeprom_am6_get(CONFIG_EEPROM_BUS_ADDRESS,
254                                                     i2c_addr,
255                                                     &ep,
256                                                     (char **)mac_addr,
257                                                     DAUGHTER_CARD_NO_OF_MAC_ADDR,
258                                                     &mac_addr_cnt);
259                         if (ret) {
260                                 debug("%s: No daughtercard EEPROM at 0x%02x found %d\n",
261                                       __func__, i2c_addr, ret);
262                                 eeprom_read_success = false;
263                                 /* Skip to the next daughtercard to probe */
264                                 continue;
265                         }
266
267                         /* EEPROM read successful, okay to further process. */
268                         eeprom_read_success = true;
269                 }
270
271                 /* Only continue processing if EEPROM data was read */
272                 if (!eeprom_read_success)
273                         continue;
274
275                 /* Only process the parsed data if we found a match */
276                 if (strncmp(ep.name, ext_cards[i].card_name, sizeof(ep.name)))
277                         continue;
278
279                 printf("Detected: %s rev %s\n", ep.name, ep.version);
280                 daughter_card_detect_flags[i] = true;
281
282 #ifndef CONFIG_SPL_BUILD
283                 int j;
284                 /*
285                  * Populate any MAC addresses from daughtercard into the U-Boot
286                  * environment, starting with a card-specific offset so we can
287                  * have multiple ext_cards contribute to the MAC pool in a well-
288                  * defined manner.
289                  */
290                 for (j = 0; j < mac_addr_cnt; j++) {
291                         if (!is_valid_ethaddr((u8 *)mac_addr[j]))
292                                 continue;
293
294                         eth_env_set_enetaddr_by_index("eth",
295                                                       ext_cards[i].eth_offset + j,
296                                                       (uchar *)mac_addr[j]);
297                 }
298 #endif
299         }
300 #ifndef CONFIG_SPL_BUILD
301         char name_overlays[1024] = { 0 };
302
303         for (i = 0; i < ARRAY_SIZE(ext_cards); i++) {
304                 if (!daughter_card_detect_flags[i])
305                         continue;
306
307                 /* Skip if no overlays are to be added */
308                 if (!strlen(ext_cards[i].dtbo_name))
309                         continue;
310
311                 /*
312                  * Make sure we are not running out of buffer space by checking
313                  * if we can fit the new overlay, a trailing space to be used
314                  * as a separator, plus the terminating zero.
315                  */
316                 if (strlen(name_overlays) + strlen(ext_cards[i].dtbo_name) + 2 >
317                     sizeof(name_overlays))
318                         return -ENOMEM;
319
320                 /* Append to our list of overlays */
321                 strcat(name_overlays, ext_cards[i].dtbo_name);
322                 strcat(name_overlays, " ");
323         }
324
325         /* Apply device tree overlay(s) to the U-Boot environment, if any */
326         if (strlen(name_overlays))
327                 return env_set("name_overlays", name_overlays);
328 #endif
329
330         return 0;
331 }
332
333 int board_late_init(void)
334 {
335         setup_board_eeprom_env();
336         setup_serial();
337
338         /* Check for and probe any plugged-in daughtercards */
339         probe_daughtercards();
340
341         return 0;
342 }
343
344 void spl_board_init(void)
345 {
346         probe_daughtercards();
347 }