xref: /openbmc/u-boot/lib/display_options.c (revision b8098bc1)
1 /*
2  * (C) Copyright 2000-2002
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <config.h>
9 #include <common.h>
10 #include <div64.h>
11 #include <inttypes.h>
12 #include <version.h>
13 #include <linux/ctype.h>
14 #include <asm/io.h>
15 
16 int display_options (void)
17 {
18 #if defined(BUILD_TAG)
19 	printf ("\n\n%s, Build: %s\n\n", version_string, BUILD_TAG);
20 #else
21 	printf ("\n\n%s\n\n", version_string);
22 #endif
23 	return 0;
24 }
25 
26 void print_freq(uint64_t freq, const char *s)
27 {
28 	unsigned long m = 0;
29 #if defined(CONFIG_SPL_SERIAL_SUPPORT)
30 	unsigned long n;
31 #endif
32 	uint32_t f;
33 	static const char names[] = {'G', 'M', 'K'};
34 	unsigned long d = 1e9;
35 	char c = 0;
36 	unsigned int i;
37 
38 	for (i = 0; i < ARRAY_SIZE(names); i++, d /= 1000) {
39 		if (freq >= d) {
40 			c = names[i];
41 			break;
42 		}
43 	}
44 
45 	if (!c) {
46 		printf("%" PRIu64 " Hz%s", freq, s);
47 		return;
48 	}
49 
50 	f = do_div(freq, d);
51 #if defined(CONFIG_SPL_SERIAL_SUPPORT)
52 	n = freq;
53 #endif
54 
55 	/* If there's a remainder, show the first few digits */
56 	if (f) {
57 		m = f;
58 		while (m > 1000)
59 			m /= 10;
60 		while (m && !(m % 10))
61 			m /= 10;
62 		if (m >= 100)
63 			m = (m / 10) + (m % 100 >= 50);
64 	}
65 
66 #if defined(CONFIG_SPL_SERIAL_SUPPORT)
67 	printf("%lu", n);
68 #endif
69 	if (m)
70 		printf(".%ld", m);
71 	printf(" %cHz%s", c, s);
72 }
73 
74 void print_size(uint64_t size, const char *s)
75 {
76 	unsigned long m = 0, n;
77 	uint64_t f;
78 	static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
79 	unsigned long d = 10 * ARRAY_SIZE(names);
80 	char c = 0;
81 	unsigned int i;
82 
83 	for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
84 		if (size >> d) {
85 			c = names[i];
86 			break;
87 		}
88 	}
89 
90 	if (!c) {
91 		printf("%" PRIu64 " Bytes%s", size, s);
92 		return;
93 	}
94 
95 	n = size >> d;
96 	f = size & ((1ULL << d) - 1);
97 
98 	/* If there's a remainder, deal with it */
99 	if (f) {
100 		m = (10ULL * f + (1ULL << (d - 1))) >> d;
101 
102 		if (m >= 10) {
103 			m -= 10;
104 			n += 1;
105 		}
106 	}
107 
108 	printf ("%lu", n);
109 	if (m) {
110 		printf (".%ld", m);
111 	}
112 	printf (" %ciB%s", c, s);
113 }
114 
115 #define MAX_LINE_LENGTH_BYTES (64)
116 #define DEFAULT_LINE_LENGTH_BYTES (16)
117 int print_buffer(ulong addr, const void *data, uint width, uint count,
118 		 uint linelen)
119 {
120 	/* linebuf as a union causes proper alignment */
121 	union linebuf {
122 #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
123 		uint64_t uq[MAX_LINE_LENGTH_BYTES/sizeof(uint64_t) + 1];
124 #endif
125 		uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1];
126 		uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1];
127 		uint8_t  uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1];
128 	} lb;
129 	int i;
130 #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
131 	uint64_t __maybe_unused x;
132 #else
133 	uint32_t __maybe_unused x;
134 #endif
135 
136 	if (linelen*width > MAX_LINE_LENGTH_BYTES)
137 		linelen = MAX_LINE_LENGTH_BYTES / width;
138 	if (linelen < 1)
139 		linelen = DEFAULT_LINE_LENGTH_BYTES / width;
140 
141 	while (count) {
142 		uint thislinelen = linelen;
143 		printf("%08lx:", addr);
144 
145 		/* check for overflow condition */
146 		if (count < thislinelen)
147 			thislinelen = count;
148 
149 		/* Copy from memory into linebuf and print hex values */
150 		for (i = 0; i < thislinelen; i++) {
151 			if (width == 4)
152 				x = lb.ui[i] = *(volatile uint32_t *)data;
153 #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
154 			else if (width == 8)
155 				x = lb.uq[i] = *(volatile uint64_t *)data;
156 #endif
157 			else if (width == 2)
158 				x = lb.us[i] = *(volatile uint16_t *)data;
159 			else
160 				x = lb.uc[i] = *(volatile uint8_t *)data;
161 #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
162 			printf(" %0*llx", width * 2, (long long)x);
163 #else
164 			printf(" %0*x", width * 2, x);
165 #endif
166 			data += width;
167 		}
168 
169 		while (thislinelen < linelen) {
170 			/* fill line with whitespace for nice ASCII print */
171 			for (i=0; i<width*2+1; i++)
172 				puts(" ");
173 			linelen--;
174 		}
175 
176 		/* Print data in ASCII characters */
177 		for (i = 0; i < thislinelen * width; i++) {
178 			if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
179 				lb.uc[i] = '.';
180 		}
181 		lb.uc[i] = '\0';
182 		printf("    %s\n", lb.uc);
183 
184 		/* update references */
185 		addr += thislinelen * width;
186 		count -= thislinelen;
187 
188 		if (ctrlc())
189 			return -1;
190 	}
191 
192 	return 0;
193 }
194