clk: fixed-rate: Enable DM_FLAG_PRE_RELOC flag
[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_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
121 int ft_board_setup(void *blob, struct bd_info *bd)
122 {
123         int ret;
124
125         ret = fdt_fixup_msmc_ram(blob, "/bus@100000", "sram@70000000");
126         if (ret < 0)
127                 ret = fdt_fixup_msmc_ram(blob, "/interconnect@100000",
128                                          "sram@70000000");
129         if (ret)
130                 printf("%s: fixing up msmc ram failed %d\n", __func__, ret);
131
132         detect_enable_hyperflash(blob);
133
134         return ret;
135 }
136 #endif
137
138 #ifdef CONFIG_TI_I2C_BOARD_DETECT
139 int do_board_detect(void)
140 {
141         int ret;
142
143         ret = ti_i2c_eeprom_am6_get_base(CONFIG_EEPROM_BUS_ADDRESS,
144                                          CONFIG_EEPROM_CHIP_ADDRESS);
145         if (ret)
146                 pr_err("Reading on-board EEPROM at 0x%02x failed %d\n",
147                        CONFIG_EEPROM_CHIP_ADDRESS, ret);
148
149         return ret;
150 }
151
152 int checkboard(void)
153 {
154         struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
155
156         if (do_board_detect())
157                 /* EEPROM not populated */
158                 printf("Board: %s rev %s\n", "J721EX-PM1-SOM", "E2");
159         else
160                 printf("Board: %s rev %s\n", ep->name, ep->version);
161
162         return 0;
163 }
164
165 static void setup_board_eeprom_env(void)
166 {
167         char *name = "j721e";
168
169         if (do_board_detect())
170                 goto invalid_eeprom;
171
172         if (board_is_j721e_som())
173                 name = "j721e";
174         else if (board_is_j7200_som())
175                 name = "j7200";
176         else
177                 printf("Unidentified board claims %s in eeprom header\n",
178                        board_ti_get_name());
179
180 invalid_eeprom:
181         set_board_info_env_am6(name);
182 }
183
184 static void setup_serial(void)
185 {
186         struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
187         unsigned long board_serial;
188         char *endp;
189         char serial_string[17] = { 0 };
190
191         if (env_get("serial#"))
192                 return;
193
194         board_serial = simple_strtoul(ep->serial, &endp, 16);
195         if (*endp != '\0') {
196                 pr_err("Error: Can't set serial# to %s\n", ep->serial);
197                 return;
198         }
199
200         snprintf(serial_string, sizeof(serial_string), "%016lx", board_serial);
201         env_set("serial#", serial_string);
202 }
203
204 /*
205  * Declaration of daughtercards to probe. Note that when adding more
206  * cards they should be grouped by the 'i2c_addr' field to allow for a
207  * more efficient probing process.
208  */
209 static const struct {
210         u8 i2c_addr;            /* I2C address of card EEPROM */
211         char *card_name;        /* EEPROM-programmed card name */
212         char *dtbo_name;        /* Device tree overlay to apply */
213         u8 eth_offset;          /* ethXaddr MAC address index offset */
214 } ext_cards[] = {
215         {
216                 0x51,
217                 "J7X-BASE-CPB",
218                 "",             /* No dtbo for this board */
219                 0,
220         },
221         {
222                 0x52,
223                 "J7X-INFOTAN-EXP",
224                 "",             /* No dtbo for this board */
225                 0,
226         },
227         {
228                 0x52,
229                 "J7X-GESI-EXP",
230                 "",             /* No dtbo for this board */
231                 5,              /* Start populating from eth5addr */
232         },
233         {
234                 0x54,
235                 "J7X-VSC8514-ETH",
236                 "",             /* No dtbo for this board */
237                 1,              /* Start populating from eth1addr */
238         },
239 };
240
241 static bool daughter_card_detect_flags[ARRAY_SIZE(ext_cards)];
242
243 const char *board_fit_get_additionnal_images(int index, const char *type)
244 {
245         int i, j;
246
247         if (strcmp(type, FIT_FDT_PROP))
248                 return NULL;
249
250         j = 0;
251         for (i = 0; i < ARRAY_SIZE(ext_cards); i++) {
252                 if (daughter_card_detect_flags[i]) {
253                         if (j == index) {
254                                 /*
255                                  * Return dtbo name only if populated,
256                                  * otherwise stop parsing here.
257                                  */
258                                 if (strlen(ext_cards[i].dtbo_name))
259                                         return ext_cards[i].dtbo_name;
260                                 else
261                                         return NULL;
262                         };
263
264                         j++;
265                 }
266         }
267
268         return NULL;
269 }
270
271 static int probe_daughtercards(void)
272 {
273         char mac_addr[DAUGHTER_CARD_NO_OF_MAC_ADDR][TI_EEPROM_HDR_ETH_ALEN];
274         bool eeprom_read_success;
275         struct ti_am6_eeprom ep;
276         u8 previous_i2c_addr;
277         u8 mac_addr_cnt;
278         int i;
279         int ret;
280
281         /* Mark previous I2C address variable as not populated */
282         previous_i2c_addr = 0xff;
283
284         /* No EEPROM data was read yet */
285         eeprom_read_success = false;
286
287         /* Iterate through list of daughtercards */
288         for (i = 0; i < ARRAY_SIZE(ext_cards); i++) {
289                 /* Obtain card-specific I2C address */
290                 u8 i2c_addr = ext_cards[i].i2c_addr;
291
292                 /* Read card EEPROM if not already read previously */
293                 if (i2c_addr != previous_i2c_addr) {
294                         /* Store I2C address so we can avoid reading twice */
295                         previous_i2c_addr = i2c_addr;
296
297                         /* Get and parse the daughter card EEPROM record */
298                         ret = ti_i2c_eeprom_am6_get(CONFIG_EEPROM_BUS_ADDRESS,
299                                                     i2c_addr,
300                                                     &ep,
301                                                     (char **)mac_addr,
302                                                     DAUGHTER_CARD_NO_OF_MAC_ADDR,
303                                                     &mac_addr_cnt);
304                         if (ret) {
305                                 debug("%s: No daughtercard EEPROM at 0x%02x found %d\n",
306                                       __func__, i2c_addr, ret);
307                                 eeprom_read_success = false;
308                                 /* Skip to the next daughtercard to probe */
309                                 continue;
310                         }
311
312                         /* EEPROM read successful, okay to further process. */
313                         eeprom_read_success = true;
314                 }
315
316                 /* Only continue processing if EEPROM data was read */
317                 if (!eeprom_read_success)
318                         continue;
319
320                 /* Only process the parsed data if we found a match */
321                 if (strncmp(ep.name, ext_cards[i].card_name, sizeof(ep.name)))
322                         continue;
323
324                 printf("Detected: %s rev %s\n", ep.name, ep.version);
325                 daughter_card_detect_flags[i] = true;
326
327 #ifndef CONFIG_SPL_BUILD
328                 int j;
329                 /*
330                  * Populate any MAC addresses from daughtercard into the U-Boot
331                  * environment, starting with a card-specific offset so we can
332                  * have multiple ext_cards contribute to the MAC pool in a well-
333                  * defined manner.
334                  */
335                 for (j = 0; j < mac_addr_cnt; j++) {
336                         if (!is_valid_ethaddr((u8 *)mac_addr[j]))
337                                 continue;
338
339                         eth_env_set_enetaddr_by_index("eth",
340                                                       ext_cards[i].eth_offset + j,
341                                                       (uchar *)mac_addr[j]);
342                 }
343 #endif
344         }
345 #ifndef CONFIG_SPL_BUILD
346         char name_overlays[1024] = { 0 };
347
348         for (i = 0; i < ARRAY_SIZE(ext_cards); i++) {
349                 if (!daughter_card_detect_flags[i])
350                         continue;
351
352                 /* Skip if no overlays are to be added */
353                 if (!strlen(ext_cards[i].dtbo_name))
354                         continue;
355
356                 /*
357                  * Make sure we are not running out of buffer space by checking
358                  * if we can fit the new overlay, a trailing space to be used
359                  * as a separator, plus the terminating zero.
360                  */
361                 if (strlen(name_overlays) + strlen(ext_cards[i].dtbo_name) + 2 >
362                     sizeof(name_overlays))
363                         return -ENOMEM;
364
365                 /* Append to our list of overlays */
366                 strcat(name_overlays, ext_cards[i].dtbo_name);
367                 strcat(name_overlays, " ");
368         }
369
370         /* Apply device tree overlay(s) to the U-Boot environment, if any */
371         if (strlen(name_overlays))
372                 return env_set("name_overlays", name_overlays);
373 #endif
374
375         return 0;
376 }
377 #endif
378
379 int board_late_init(void)
380 {
381         if (IS_ENABLED(CONFIG_TI_I2C_BOARD_DETECT)) {
382                 setup_board_eeprom_env();
383                 setup_serial();
384
385                 /* Check for and probe any plugged-in daughtercards */
386                 probe_daughtercards();
387         }
388
389         return 0;
390 }
391
392 void spl_board_init(void)
393 {
394 #if defined(CONFIG_ESM_K3) || defined(CONFIG_ESM_PMIC)
395         struct udevice *dev;
396         int ret;
397 #endif
398
399         if ((IS_ENABLED(CONFIG_TARGET_J721E_A72_EVM) ||
400              IS_ENABLED(CONFIG_TARGET_J7200_A72_EVM)) &&
401             IS_ENABLED(CONFIG_TI_I2C_BOARD_DETECT))
402                 probe_daughtercards();
403
404 #ifdef CONFIG_ESM_K3
405         if (board_ti_k3_is("J721EX-PM2-SOM")) {
406                 ret = uclass_get_device_by_driver(UCLASS_MISC,
407                                                   DM_GET_DRIVER(k3_esm), &dev);
408                 if (ret)
409                         printf("ESM init failed: %d\n", ret);
410         }
411 #endif
412
413 #ifdef CONFIG_ESM_PMIC
414         if (board_ti_k3_is("J721EX-PM2-SOM")) {
415                 ret = uclass_get_device_by_driver(UCLASS_MISC,
416                                                   DM_GET_DRIVER(pmic_esm),
417                                                   &dev);
418                 if (ret)
419                         printf("ESM PMIC init failed: %d\n", ret);
420         }
421 #endif
422 }