7327c96b31865f0d3a2ecab353de14ce5cc488bc
[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 static void setup_board_eeprom_env(void)
110 {
111         char *name = "j721e";
112
113         if (do_board_detect())
114                 goto invalid_eeprom;
115
116         if (board_is_j721e_som())
117                 name = "j721e";
118         else
119                 printf("Unidentified board claims %s in eeprom header\n",
120                        board_ti_get_name());
121
122 invalid_eeprom:
123         set_board_info_env_am6(name);
124 }
125
126 static void setup_serial(void)
127 {
128         struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
129         unsigned long board_serial;
130         char *endp;
131         char serial_string[17] = { 0 };
132
133         if (env_get("serial#"))
134                 return;
135
136         board_serial = simple_strtoul(ep->serial, &endp, 16);
137         if (*endp != '\0') {
138                 pr_err("Error: Can't set serial# to %s\n", ep->serial);
139                 return;
140         }
141
142         snprintf(serial_string, sizeof(serial_string), "%016lx", board_serial);
143         env_set("serial#", serial_string);
144 }
145
146 /*
147  * Declaration of daughtercards to probe. Note that when adding more
148  * cards they should be grouped by the 'i2c_addr' field to allow for a
149  * more efficient probing process.
150  */
151 static const struct {
152         u8 i2c_addr;            /* I2C address of card EEPROM */
153         char *card_name;        /* EEPROM-programmed card name */
154         char *dtbo_name;        /* Device tree overlay to apply */
155         u8 eth_offset;          /* ethXaddr MAC address index offset */
156 } ext_cards[] = {
157         {
158                 0x51,
159                 "J7X-BASE-CPB",
160                 "",             /* No dtbo for this board */
161                 0,
162         },
163         {
164                 0x52,
165                 "J7X-INFOTAN-EXP",
166                 "",             /* No dtbo for this board */
167                 0,
168         },
169         {
170                 0x52,
171                 "J7X-GESI-EXP",
172                 "",             /* No dtbo for this board */
173                 5,              /* Start populating from eth5addr */
174         },
175         {
176                 0x54,
177                 "J7X-VSC8514-ETH",
178                 "",             /* No dtbo for this board */
179                 1,              /* Start populating from eth1addr */
180         },
181 };
182
183 static bool daughter_card_detect_flags[ARRAY_SIZE(ext_cards)];
184
185 const char *board_fit_get_additionnal_images(int index, const char *type)
186 {
187         int i, j;
188
189         if (strcmp(type, FIT_FDT_PROP))
190                 return NULL;
191
192         j = 0;
193         for (i = 0; i < ARRAY_SIZE(ext_cards); i++) {
194                 if (daughter_card_detect_flags[i]) {
195                         if (j == index) {
196                                 /*
197                                  * Return dtbo name only if populated,
198                                  * otherwise stop parsing here.
199                                  */
200                                 if (strlen(ext_cards[i].dtbo_name))
201                                         return ext_cards[i].dtbo_name;
202                                 else
203                                         return NULL;
204                         };
205
206                         j++;
207                 }
208         }
209
210         return NULL;
211 }
212
213 static int probe_daughtercards(void)
214 {
215         char mac_addr[DAUGHTER_CARD_NO_OF_MAC_ADDR][TI_EEPROM_HDR_ETH_ALEN];
216         bool eeprom_read_success;
217         struct ti_am6_eeprom ep;
218         u8 previous_i2c_addr;
219         u8 mac_addr_cnt;
220         int i;
221         int ret;
222
223         /* Mark previous I2C address variable as not populated */
224         previous_i2c_addr = 0xff;
225
226         /* No EEPROM data was read yet */
227         eeprom_read_success = false;
228
229         /* Iterate through list of daughtercards */
230         for (i = 0; i < ARRAY_SIZE(ext_cards); i++) {
231                 /* Obtain card-specific I2C address */
232                 u8 i2c_addr = ext_cards[i].i2c_addr;
233
234                 /* Read card EEPROM if not already read previously */
235                 if (i2c_addr != previous_i2c_addr) {
236                         /* Store I2C address so we can avoid reading twice */
237                         previous_i2c_addr = i2c_addr;
238
239                         /* Get and parse the daughter card EEPROM record */
240                         ret = ti_i2c_eeprom_am6_get(CONFIG_EEPROM_BUS_ADDRESS,
241                                                     i2c_addr,
242                                                     &ep,
243                                                     (char **)mac_addr,
244                                                     DAUGHTER_CARD_NO_OF_MAC_ADDR,
245                                                     &mac_addr_cnt);
246                         if (ret) {
247                                 debug("%s: No daughtercard EEPROM at 0x%02x found %d\n",
248                                       __func__, i2c_addr, ret);
249                                 eeprom_read_success = false;
250                                 /* Skip to the next daughtercard to probe */
251                                 continue;
252                         }
253
254                         /* EEPROM read successful, okay to further process. */
255                         eeprom_read_success = true;
256                 }
257
258                 /* Only continue processing if EEPROM data was read */
259                 if (!eeprom_read_success)
260                         continue;
261
262                 /* Only process the parsed data if we found a match */
263                 if (strncmp(ep.name, ext_cards[i].card_name, sizeof(ep.name)))
264                         continue;
265
266                 printf("Detected: %s rev %s\n", ep.name, ep.version);
267                 daughter_card_detect_flags[i] = true;
268
269 #ifndef CONFIG_SPL_BUILD
270                 int j;
271                 /*
272                  * Populate any MAC addresses from daughtercard into the U-Boot
273                  * environment, starting with a card-specific offset so we can
274                  * have multiple ext_cards contribute to the MAC pool in a well-
275                  * defined manner.
276                  */
277                 for (j = 0; j < mac_addr_cnt; j++) {
278                         if (!is_valid_ethaddr((u8 *)mac_addr[j]))
279                                 continue;
280
281                         eth_env_set_enetaddr_by_index("eth",
282                                                       ext_cards[i].eth_offset + j,
283                                                       (uchar *)mac_addr[j]);
284                 }
285 #endif
286         }
287 #ifndef CONFIG_SPL_BUILD
288         char name_overlays[1024] = { 0 };
289
290         for (i = 0; i < ARRAY_SIZE(ext_cards); i++) {
291                 if (!daughter_card_detect_flags[i])
292                         continue;
293
294                 /* Skip if no overlays are to be added */
295                 if (!strlen(ext_cards[i].dtbo_name))
296                         continue;
297
298                 /*
299                  * Make sure we are not running out of buffer space by checking
300                  * if we can fit the new overlay, a trailing space to be used
301                  * as a separator, plus the terminating zero.
302                  */
303                 if (strlen(name_overlays) + strlen(ext_cards[i].dtbo_name) + 2 >
304                     sizeof(name_overlays))
305                         return -ENOMEM;
306
307                 /* Append to our list of overlays */
308                 strcat(name_overlays, ext_cards[i].dtbo_name);
309                 strcat(name_overlays, " ");
310         }
311
312         /* Apply device tree overlay(s) to the U-Boot environment, if any */
313         if (strlen(name_overlays))
314                 return env_set("name_overlays", name_overlays);
315 #endif
316
317         return 0;
318 }
319
320 int board_late_init(void)
321 {
322         setup_board_eeprom_env();
323         setup_serial();
324
325         /* Check for and probe any plugged-in daughtercards */
326         probe_daughtercards();
327
328         return 0;
329 }
330
331 void spl_board_init(void)
332 {
333         probe_daughtercards();
334 }