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