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