blob: 1220cd84519bb9bf6cc5da7cbf6d997cdede0a95 [file] [log] [blame]
David Huang5d9fadf2022-01-25 20:56:37 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Board specific initialization for J721S2 EVM
4 *
Nishanth Menona94a4072023-11-01 15:56:03 -05005 * Copyright (C) 2021 Texas Instruments Incorporated - https://www.ti.com/
David Huang5d9fadf2022-01-25 20:56:37 +05306 * David Huang <d-huang@ti.com>
7 *
8 */
9
David Huang5d9fadf2022-01-25 20:56:37 +053010#include <env.h>
11#include <fdt_support.h>
12#include <generic-phy.h>
13#include <image.h>
14#include <init.h>
15#include <log.h>
16#include <net.h>
David Huang5d9fadf2022-01-25 20:56:37 +053017#include <asm/arch/hardware.h>
18#include <asm/gpio.h>
19#include <asm/io.h>
20#include <spl.h>
David Huang5d9fadf2022-01-25 20:56:37 +053021#include <dm.h>
22#include <dm/uclass-internal.h>
Sinthu Raja554f8e82023-01-10 21:17:54 +053023#include <dm/root.h>
David Huang5d9fadf2022-01-25 20:56:37 +053024
25#include "../common/board_detect.h"
26
David Huang5d9fadf2022-01-25 20:56:37 +053027DECLARE_GLOBAL_DATA_PTR;
28
29int board_init(void)
30{
31 return 0;
32}
33
34int dram_init(void)
35{
36#ifdef CONFIG_PHYS_64BIT
37 gd->ram_size = 0x100000000;
38#else
39 gd->ram_size = 0x80000000;
40#endif
41
42 return 0;
43}
44
Heinrich Schuchardtd768dd82023-08-12 20:16:58 +020045phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
David Huang5d9fadf2022-01-25 20:56:37 +053046{
47#ifdef CONFIG_PHYS_64BIT
48 /* Limit RAM used by U-Boot to the DDR low region */
49 if (gd->ram_top > 0x100000000)
50 return 0x100000000;
51#endif
52
53 return gd->ram_top;
54}
55
56int dram_init_banksize(void)
57{
58 /* Bank 0 declares the memory available in the DDR low region */
Andrew Davisa2132892023-11-30 08:49:11 -060059 gd->bd->bi_dram[0].start = 0x80000000;
David Huang5d9fadf2022-01-25 20:56:37 +053060 gd->bd->bi_dram[0].size = 0x7fffffff;
61 gd->ram_size = 0x80000000;
62
63#ifdef CONFIG_PHYS_64BIT
64 /* Bank 1 declares the memory available in the DDR high region */
Andrew Davisa2132892023-11-30 08:49:11 -060065 gd->bd->bi_dram[1].start = 0x880000000;
David Huang5d9fadf2022-01-25 20:56:37 +053066 gd->bd->bi_dram[1].size = 0x37fffffff;
67 gd->ram_size = 0x400000000;
68#endif
69
70 return 0;
71}
72
David Huang5d9fadf2022-01-25 20:56:37 +053073#ifdef CONFIG_TI_I2C_BOARD_DETECT
Sinthu Raja24bbe092023-01-10 21:17:50 +053074/*
75 * Functions specific to EVM and SK designs of J721S2/AM68 family.
76 */
77
78#define board_is_j721s2_som() board_ti_k3_is("J721S2X-PM1-SOM")
79
80#define board_is_am68_sk_som() board_ti_k3_is("AM68-SK-SOM")
81
David Huang5d9fadf2022-01-25 20:56:37 +053082int do_board_detect(void)
83{
84 int ret;
85
Sinthu Raja1aaf9df2023-01-10 21:17:52 +053086 if (board_ti_was_eeprom_read())
87 return 0;
88
David Huang5d9fadf2022-01-25 20:56:37 +053089 ret = ti_i2c_eeprom_am6_get_base(CONFIG_EEPROM_BUS_ADDRESS,
90 CONFIG_EEPROM_CHIP_ADDRESS);
Sinthu Rajaad45a172023-01-10 21:17:51 +053091 if (ret) {
92 printf("EEPROM not available at 0x%02x, trying to read at 0x%02x\n",
93 CONFIG_EEPROM_CHIP_ADDRESS, CONFIG_EEPROM_CHIP_ADDRESS + 1);
94 ret = ti_i2c_eeprom_am6_get_base(CONFIG_EEPROM_BUS_ADDRESS,
95 CONFIG_EEPROM_CHIP_ADDRESS + 1);
96 if (ret)
97 pr_err("Reading on-board EEPROM at 0x%02x failed %d\n",
98 CONFIG_EEPROM_CHIP_ADDRESS + 1, ret);
99 }
David Huang5d9fadf2022-01-25 20:56:37 +0530100
101 return ret;
102}
103
104int checkboard(void)
105{
106 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
107
108 if (do_board_detect())
109 /* EEPROM not populated */
110 printf("Board: %s rev %s\n", "J721S2X-PM1-SOM", "E1");
111 else
112 printf("Board: %s rev %s\n", ep->name, ep->version);
113
114 return 0;
115}
116
117static void setup_board_eeprom_env(void)
118{
119 char *name = "j721s2";
120
121 if (do_board_detect())
122 goto invalid_eeprom;
123
124 if (board_is_j721s2_som())
125 name = "j721s2";
Sinthu Raja24bbe092023-01-10 21:17:50 +0530126 else if (board_is_am68_sk_som())
127 name = "am68-sk";
David Huang5d9fadf2022-01-25 20:56:37 +0530128 else
129 printf("Unidentified board claims %s in eeprom header\n",
130 board_ti_get_name());
131
132invalid_eeprom:
133 set_board_info_env_am6(name);
134}
135
136static void setup_serial(void)
137{
138 struct ti_am6_eeprom *ep = TI_AM6_EEPROM_DATA;
139 unsigned long board_serial;
140 char *endp;
141 char serial_string[17] = { 0 };
142
143 if (env_get("serial#"))
144 return;
145
146 board_serial = simple_strtoul(ep->serial, &endp, 16);
147 if (*endp != '\0') {
148 pr_err("Error: Can't set serial# to %s\n", ep->serial);
149 return;
150 }
151
152 snprintf(serial_string, sizeof(serial_string), "%016lx", board_serial);
153 env_set("serial#", serial_string);
154}
Kishon Vijay Abraham I7bf341a2023-04-10 11:40:15 +0530155
156/*
157 * Declaration of daughtercards to probe. Note that when adding more
158 * cards they should be grouped by the 'i2c_addr' field to allow for a
159 * more efficient probing process.
160 */
161static const struct {
162 u8 i2c_addr; /* I2C address of card EEPROM */
163 char *card_name; /* EEPROM-programmed card name */
164 char *dtbo_name; /* Device tree overlay to apply */
165 u8 eth_offset; /* ethXaddr MAC address index offset */
166} ext_cards[] = {
167 {
168 0x52,
169 "J7X-GESI-EXP",
170 "k3-j721s2-gesi-exp-board.dtbo",
171 1, /* Start populating from eth1addr */
172 },
173};
174
175#define DAUGHTER_CARD_NO_OF_MAC_ADDR 5
176static bool daughter_card_detect_flags[ARRAY_SIZE(ext_cards)];
177
178static int probe_daughtercards(void)
179{
180 char mac_addr[DAUGHTER_CARD_NO_OF_MAC_ADDR][TI_EEPROM_HDR_ETH_ALEN];
181 bool eeprom_read_success;
182 struct ti_am6_eeprom ep;
183 u8 previous_i2c_addr;
184 u8 mac_addr_cnt;
185 int i;
186 int ret;
187
188 /* Mark previous I2C address variable as not populated */
189 previous_i2c_addr = 0xff;
190
191 /* No EEPROM data was read yet */
192 eeprom_read_success = false;
193
194 /* Iterate through list of daughtercards */
195 for (i = 0; i < ARRAY_SIZE(ext_cards); i++) {
196 /* Obtain card-specific I2C address */
197 u8 i2c_addr = ext_cards[i].i2c_addr;
198
199 /* Read card EEPROM if not already read previously */
200 if (i2c_addr != previous_i2c_addr) {
201 /* Store I2C address so we can avoid reading twice */
202 previous_i2c_addr = i2c_addr;
203
204 /* Get and parse the daughter card EEPROM record */
205 ret = ti_i2c_eeprom_am6_get(CONFIG_EEPROM_BUS_ADDRESS,
206 i2c_addr,
207 &ep,
208 (char **)mac_addr,
209 DAUGHTER_CARD_NO_OF_MAC_ADDR,
210 &mac_addr_cnt);
211 if (ret) {
212 debug("%s: No daughtercard EEPROM at 0x%02x found %d\n",
213 __func__, i2c_addr, ret);
214 eeprom_read_success = false;
215 /* Skip to the next daughtercard to probe */
216 continue;
217 }
218
219 /* EEPROM read successful, okay to further process. */
220 eeprom_read_success = true;
221 }
222
223 /* Only continue processing if EEPROM data was read */
224 if (!eeprom_read_success)
225 continue;
226
227 /* Only process the parsed data if we found a match */
228 if (strncmp(ep.name, ext_cards[i].card_name, sizeof(ep.name)))
229 continue;
230
231 printf("Detected: %s rev %s\n", ep.name, ep.version);
232 daughter_card_detect_flags[i] = true;
233
234 if (!IS_ENABLED(CONFIG_SPL_BUILD)) {
235 int j;
236 /*
237 * Populate any MAC addresses from daughtercard into the U-Boot
238 * environment, starting with a card-specific offset so we can
239 * have multiple ext_cards contribute to the MAC pool in a well-
240 * defined manner.
241 */
242 for (j = 0; j < mac_addr_cnt; j++) {
243 if (!is_valid_ethaddr((u8 *)mac_addr[j]))
244 continue;
245
246 eth_env_set_enetaddr_by_index("eth", ext_cards[i].eth_offset + j,
247 (uchar *)mac_addr[j]);
248 }
249 }
250 }
251
252 if (!IS_ENABLED(CONFIG_SPL_BUILD)) {
253 char name_overlays[1024] = { 0 };
254
255 for (i = 0; i < ARRAY_SIZE(ext_cards); i++) {
256 if (!daughter_card_detect_flags[i])
257 continue;
258
259 /* Skip if no overlays are to be added */
260 if (!strlen(ext_cards[i].dtbo_name))
261 continue;
262
263 /*
264 * Make sure we are not running out of buffer space by checking
265 * if we can fit the new overlay, a trailing space to be used
266 * as a separator, plus the terminating zero.
267 */
268 if (strlen(name_overlays) + strlen(ext_cards[i].dtbo_name) + 2 >
269 sizeof(name_overlays))
270 return -ENOMEM;
271
272 /* Append to our list of overlays */
273 strcat(name_overlays, ext_cards[i].dtbo_name);
274 strcat(name_overlays, " ");
275 }
276
277 /* Apply device tree overlay(s) to the U-Boot environment, if any */
278 if (strlen(name_overlays))
279 return env_set("name_overlays", name_overlays);
280 }
281
282 return 0;
283}
David Huang5d9fadf2022-01-25 20:56:37 +0530284#endif
285
Sinthu Raja1aaf9df2023-01-10 21:17:52 +0530286/*
287 * This function chooses the right dtb based on the board name read from
288 * EEPROM if the EEPROM is programmed. Also, by default the boot chooses
289 * the EVM DTB if there is no EEPROM is programmed or not detected.
290 */
291#ifdef CONFIG_SPL_LOAD_FIT
292int board_fit_config_name_match(const char *name)
293{
294 bool eeprom_read = board_ti_was_eeprom_read();
295
296 if (!eeprom_read || board_is_j721s2_som()) {
297 if (!strcmp(name, "k3-j721s2-common-proc-board"))
298 return 0;
299 } else if (!eeprom_read || board_is_am68_sk_som()) {
300 if (!strcmp(name, "k3-am68-sk-base-board"))
301 return 0;
302 }
303
304 return -1;
305}
306#endif
307
David Huang5d9fadf2022-01-25 20:56:37 +0530308int board_late_init(void)
309{
310 if (IS_ENABLED(CONFIG_TI_I2C_BOARD_DETECT)) {
311 setup_board_eeprom_env();
312 setup_serial();
Kishon Vijay Abraham I7bf341a2023-04-10 11:40:15 +0530313 probe_daughtercards();
David Huang5d9fadf2022-01-25 20:56:37 +0530314 }
315
316 return 0;
317}
318
319void spl_board_init(void)
320{
321}