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