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