1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Implements the 'bd' command to show board information
4 *
5 * (C) Copyright 2003
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 */
8
9#include <common.h>
10#include <command.h>
11#include <dm.h>
12#include <env.h>
13#include <getopt.h>
14#include <lmb.h>
15#include <mapmem.h>
16#include <net.h>
17#include <serial.h>
18#include <video.h>
19#include <vsprintf.h>
20#include <asm/cache.h>
21#include <asm/global_data.h>
22#include <display_options.h>
23
24DECLARE_GLOBAL_DATA_PTR;
25
26void bdinfo_print_size(const char *name, uint64_t size)
27{
28	printf("%-12s= ", name);
29	print_size(size, "\n");
30}
31
32void bdinfo_print_str(const char *name, const char *str)
33{
34	printf("%-12s= %s\n", name, str);
35}
36
37void bdinfo_print_num_l(const char *name, ulong value)
38{
39	printf("%-12s= 0x%0*lx\n", name, 2 * (int)sizeof(value), value);
40}
41
42void bdinfo_print_num_ll(const char *name, unsigned long long value)
43{
44	printf("%-12s= 0x%.*llx\n", name, 2 * (int)sizeof(ulong), value);
45}
46
47static void print_eth(void)
48{
49	const int idx = eth_get_dev_index();
50	uchar enetaddr[6];
51	char name[10];
52	int ret;
53
54	if (idx)
55		sprintf(name, "eth%iaddr", idx);
56	else
57		strcpy(name, "ethaddr");
58
59	ret = eth_env_get_enetaddr_by_index("eth", idx, enetaddr);
60
61	printf("current eth = %s\n", eth_get_name());
62	if (!ret)
63		printf("%-12s= (not set)\n", name);
64	else
65		printf("%-12s= %pM\n", name, enetaddr);
66	printf("IP addr     = %s\n", env_get("ipaddr"));
67}
68
69void bdinfo_print_mhz(const char *name, unsigned long hz)
70{
71	char buf[32];
72
73	printf("%-12s= %6s MHz\n", name, strmhz(buf, hz));
74}
75
76static void print_bi_dram(const struct bd_info *bd)
77{
78	int i;
79
80	for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
81		if (bd->bi_dram[i].size) {
82			bdinfo_print_num_l("DRAM bank",	i);
83			bdinfo_print_num_ll("-> start",	bd->bi_dram[i].start);
84			bdinfo_print_num_ll("-> size",	bd->bi_dram[i].size);
85		}
86	}
87}
88
89__weak void arch_print_bdinfo(void)
90{
91}
92
93static void show_video_info(void)
94{
95	const struct udevice *dev;
96	struct uclass *uc;
97
98	uclass_id_foreach_dev(UCLASS_VIDEO, dev, uc) {
99		printf("%-12s= %s %sactive\n", "Video", dev->name,
100		       device_active(dev) ? "" : "in");
101		if (device_active(dev)) {
102			struct video_priv *upriv = dev_get_uclass_priv(dev);
103			struct video_uc_plat *plat = dev_get_uclass_plat(dev);
104
105			bdinfo_print_num_ll("FB base", (ulong)upriv->fb);
106			if (upriv->copy_fb) {
107				bdinfo_print_num_ll("FB copy",
108						    (ulong)upriv->copy_fb);
109				bdinfo_print_num_l(" copy size",
110						   plat->copy_size);
111			}
112			printf("%-12s= %dx%dx%d\n", "FB size", upriv->xsize,
113			       upriv->ysize, 1 << upriv->bpix);
114		}
115	}
116}
117
118static void print_serial(struct udevice *dev)
119{
120	struct serial_device_info info;
121	int ret;
122
123	if (!dev || !IS_ENABLED(CONFIG_DM_SERIAL))
124		return;
125
126	ret = serial_getinfo(dev, &info);
127	if (ret)
128		return;
129
130	bdinfo_print_num_l("serial addr", info.addr);
131	bdinfo_print_num_l(" width", info.reg_width);
132	bdinfo_print_num_l(" shift", info.reg_shift);
133	bdinfo_print_num_l(" offset", info.reg_offset);
134	bdinfo_print_num_l(" clock", info.clock);
135}
136
137static int bdinfo_print_all(struct bd_info *bd)
138{
139#ifdef DEBUG
140	bdinfo_print_num_l("bd address", (ulong)bd);
141#endif
142	bdinfo_print_num_l("boot_params", (ulong)bd->bi_boot_params);
143	print_bi_dram(bd);
144	if (IS_ENABLED(CONFIG_SYS_HAS_SRAM)) {
145		bdinfo_print_num_l("sramstart", (ulong)bd->bi_sramstart);
146		bdinfo_print_num_l("sramsize", (ulong)bd->bi_sramsize);
147	}
148	bdinfo_print_num_l("flashstart", (ulong)bd->bi_flashstart);
149	bdinfo_print_num_l("flashsize", (ulong)bd->bi_flashsize);
150	bdinfo_print_num_l("flashoffset", (ulong)bd->bi_flashoffset);
151	printf("baudrate    = %u bps\n", gd->baudrate);
152	bdinfo_print_num_l("relocaddr", gd->relocaddr);
153	bdinfo_print_num_l("reloc off", gd->reloc_off);
154	printf("%-12s= %u-bit\n", "Build", (uint)sizeof(void *) * 8);
155	if (IS_ENABLED(CONFIG_CMD_NET))
156		print_eth();
157	bdinfo_print_num_l("fdt_blob", (ulong)map_to_sysmem(gd->fdt_blob));
158	bdinfo_print_num_l("new_fdt", (ulong)map_to_sysmem(gd->new_fdt));
159	bdinfo_print_num_l("fdt_size", (ulong)gd->fdt_size);
160	if (IS_ENABLED(CONFIG_VIDEO))
161		show_video_info();
162#if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
163	bdinfo_print_num_l("multi_dtb_fit", (ulong)gd->multi_dtb_fit);
164#endif
165	if (IS_ENABLED(CONFIG_LMB) && gd->fdt_blob) {
166		struct lmb lmb;
167
168		lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
169		lmb_dump_all_force(&lmb);
170		if (IS_ENABLED(CONFIG_OF_REAL))
171			printf("devicetree  = %s\n", fdtdec_get_srcname());
172	}
173	print_serial(gd->cur_serial_dev);
174
175	if (IS_ENABLED(CONFIG_CMD_BDINFO_EXTRA)) {
176		bdinfo_print_num_ll("stack ptr", (ulong)&bd);
177		bdinfo_print_num_ll("ram_top ptr", (ulong)gd->ram_top);
178		bdinfo_print_num_l("malloc base", gd_malloc_start());
179	}
180
181	arch_print_bdinfo();
182
183	return 0;
184}
185
186int do_bdinfo(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
187{
188	struct bd_info *bd = gd->bd;
189	struct getopt_state gs;
190	int opt;
191
192	if (!CONFIG_IS_ENABLED(GETOPT) || argc == 1)
193		return bdinfo_print_all(bd);
194
195	getopt_init_state(&gs);
196	while ((opt = getopt(&gs, argc, argv, "aem")) > 0) {
197		switch (opt) {
198		case 'a':
199			return bdinfo_print_all(bd);
200		case 'e':
201			if (!IS_ENABLED(CONFIG_CMD_NET))
202				return CMD_RET_USAGE;
203			print_eth();
204			return CMD_RET_SUCCESS;
205		case 'm':
206			print_bi_dram(bd);
207			return CMD_RET_SUCCESS;
208		default:
209			return CMD_RET_USAGE;
210		}
211	}
212
213	return CMD_RET_USAGE;
214}
215
216U_BOOT_CMD(
217	bdinfo,	2,	1,	do_bdinfo,
218	"print Board Info structure",
219	""
220);
221