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