1// SPDX-License-Identifier: GPL-2.0-or-later OR BSD-3-Clause
2/*
3 * Copyright (C) 2023, STMicroelectronics - All Rights Reserved
4 */
5
6#define LOG_CATEGORY LOGC_BOARD
7
8#include <config.h>
9#include <env.h>
10#include <fdt_support.h>
11#include <log.h>
12#include <misc.h>
13#include <asm/global_data.h>
14#include <asm/arch/sys_proto.h>
15#include <dm/device.h>
16#include <dm/ofnode.h>
17#include <dm/uclass.h>
18
19/*
20 * Get a global data pointer
21 */
22DECLARE_GLOBAL_DATA_PTR;
23
24int checkboard(void)
25{
26	int ret;
27	u32 otp;
28	struct udevice *dev;
29	const char *fdt_compat;
30	int fdt_compat_len;
31
32	fdt_compat = ofnode_get_property(ofnode_root(), "compatible", &fdt_compat_len);
33
34	log_info("Board: stm32mp2 (%s)\n", fdt_compat && fdt_compat_len ? fdt_compat : "");
35
36	/* display the STMicroelectronics board identification */
37	if (CONFIG_IS_ENABLED(CMD_STBOARD)) {
38		ret = uclass_get_device_by_driver(UCLASS_MISC,
39						  DM_DRIVER_GET(stm32mp_bsec),
40						  &dev);
41		if (!ret)
42			ret = misc_read(dev, STM32_BSEC_SHADOW(BSEC_OTP_BOARD),
43					&otp, sizeof(otp));
44		if (ret > 0 && otp)
45			log_info("Board: MB%04x Var%d.%d Rev.%c-%02d\n",
46				 otp >> 16,
47				 (otp >> 12) & 0xF,
48				 (otp >> 4) & 0xF,
49				 ((otp >> 8) & 0xF) - 1 + 'A',
50				 otp & 0xF);
51	}
52
53	return 0;
54}
55
56/* board dependent setup after realloc */
57int board_init(void)
58{
59	return 0;
60}
61
62int board_late_init(void)
63{
64	const void *fdt_compat;
65	int fdt_compat_len;
66	char dtb_name[256];
67	int buf_len;
68
69	if (IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)) {
70		fdt_compat = fdt_getprop(gd->fdt_blob, 0, "compatible",
71					 &fdt_compat_len);
72		if (fdt_compat && fdt_compat_len) {
73			if (strncmp(fdt_compat, "st,", 3) != 0) {
74				env_set("board_name", fdt_compat);
75			} else {
76				env_set("board_name", fdt_compat + 3);
77
78				buf_len = sizeof(dtb_name);
79				strlcpy(dtb_name, fdt_compat + 3, buf_len);
80				buf_len -= strlen(fdt_compat + 3);
81				strlcat(dtb_name, ".dtb", buf_len);
82				env_set("fdtfile", dtb_name);
83			}
84		}
85	}
86
87	return 0;
88}
89