]> git.ipfire.org Git - thirdparty/u-boot.git/blame - lib/display_options.c
watchdog: cadence: Remove driver specific "timeout-sec" handling
[thirdparty/u-boot.git] / lib / display_options.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
20e0e233
WD
2/*
3 * (C) Copyright 2000-2002
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
20e0e233
WD
5 */
6
7#include <common.h>
24b852a7 8#include <console.h>
33eac2dc 9#include <div64.h>
09c2e90c 10#include <version.h>
c95c4280
GL
11#include <linux/ctype.h>
12#include <asm/io.h>
20e0e233 13
6c519f2d
SG
14char *display_options_get_banner_priv(bool newlines, const char *build_tag,
15 char *buf, int size)
20e0e233 16{
6c519f2d
SG
17 int len;
18
19 len = snprintf(buf, size, "%s%s", newlines ? "\n\n" : "",
20 version_string);
21 if (build_tag && len < size)
22 len += snprintf(buf + len, size - len, ", Build: %s",
23 build_tag);
24 if (len > size - 3)
25 len = size - 3;
26 strcpy(buf + len, "\n\n");
27
28 return buf;
29}
30
31#ifndef BUILD_TAG
32#define BUILD_TAG NULL
20e0e233 33#endif
6c519f2d
SG
34
35char *display_options_get_banner(bool newlines, char *buf, int size)
36{
37 return display_options_get_banner_priv(newlines, BUILD_TAG, buf, size);
38}
39
40int display_options(void)
41{
42 char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
43
44 display_options_get_banner(true, buf, sizeof(buf));
45 printf("%s", buf);
46
20e0e233
WD
47 return 0;
48}
49
33eac2dc
SG
50void print_freq(uint64_t freq, const char *s)
51{
80402f34 52 unsigned long m = 0;
33eac2dc
SG
53 uint32_t f;
54 static const char names[] = {'G', 'M', 'K'};
55 unsigned long d = 1e9;
56 char c = 0;
57 unsigned int i;
58
59 for (i = 0; i < ARRAY_SIZE(names); i++, d /= 1000) {
60 if (freq >= d) {
61 c = names[i];
62 break;
63 }
64 }
65
66 if (!c) {
dee37fc9 67 printf("%llu Hz%s", freq, s);
33eac2dc
SG
68 return;
69 }
70
71 f = do_div(freq, d);
33eac2dc
SG
72
73 /* If there's a remainder, show the first few digits */
74 if (f) {
75 m = f;
76 while (m > 1000)
77 m /= 10;
78 while (m && !(m % 10))
79 m /= 10;
80 if (m >= 100)
81 m = (m / 10) + (m % 100 >= 50);
82 }
83
e9015b30 84 printf("%lu", (unsigned long) freq);
33eac2dc
SG
85 if (m)
86 printf(".%ld", m);
87 printf(" %cHz%s", c, s);
88}
89
c6da9ae8 90void print_size(uint64_t size, const char *s)
20e0e233 91{
52dbac69 92 unsigned long m = 0, n;
c6da9ae8 93 uint64_t f;
4b42c905 94 static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
f2d76ae4 95 unsigned long d = 10 * ARRAY_SIZE(names);
4b42c905
TT
96 char c = 0;
97 unsigned int i;
20e0e233 98
f2d76ae4
NT
99 for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
100 if (size >> d) {
4b42c905
TT
101 c = names[i];
102 break;
417faf28 103 }
20e0e233
WD
104 }
105
4b42c905 106 if (!c) {
dee37fc9 107 printf("%llu Bytes%s", size, s);
4b42c905
TT
108 return;
109 }
110
f2d76ae4
NT
111 n = size >> d;
112 f = size & ((1ULL << d) - 1);
20e0e233 113
417faf28 114 /* If there's a remainder, deal with it */
f2d76ae4
NT
115 if (f) {
116 m = (10ULL * f + (1ULL << (d - 1))) >> d;
20e0e233 117
417faf28
BB
118 if (m >= 10) {
119 m -= 10;
120 n += 1;
121 }
0d498393
WD
122 }
123
4b42c905 124 printf ("%lu", n);
20e0e233
WD
125 if (m) {
126 printf (".%ld", m);
127 }
4b42c905 128 printf (" %ciB%s", c, s);
20e0e233 129}
c95c4280 130
c95c4280
GL
131#define MAX_LINE_LENGTH_BYTES (64)
132#define DEFAULT_LINE_LENGTH_BYTES (16)
bda32ffc
SG
133int print_buffer(ulong addr, const void *data, uint width, uint count,
134 uint linelen)
c95c4280 135{
150f7236
RM
136 /* linebuf as a union causes proper alignment */
137 union linebuf {
4d1fd7f1
YS
138#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
139 uint64_t uq[MAX_LINE_LENGTH_BYTES/sizeof(uint64_t) + 1];
140#endif
150f7236
RM
141 uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1];
142 uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1];
143 uint8_t uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1];
144 } lb;
c95c4280 145 int i;
4d1fd7f1 146#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
80402f34 147 uint64_t __maybe_unused x;
4d1fd7f1 148#else
80402f34 149 uint32_t __maybe_unused x;
4d1fd7f1 150#endif
c95c4280
GL
151
152 if (linelen*width > MAX_LINE_LENGTH_BYTES)
153 linelen = MAX_LINE_LENGTH_BYTES / width;
154 if (linelen < 1)
155 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
156
157 while (count) {
efd7c114 158 uint thislinelen = linelen;
c95c4280
GL
159 printf("%08lx:", addr);
160
161 /* check for overflow condition */
efd7c114
AB
162 if (count < thislinelen)
163 thislinelen = count;
c95c4280
GL
164
165 /* Copy from memory into linebuf and print hex values */
efd7c114 166 for (i = 0; i < thislinelen; i++) {
64419e47 167 if (width == 4)
150f7236 168 x = lb.ui[i] = *(volatile uint32_t *)data;
4d1fd7f1
YS
169#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
170 else if (width == 8)
171 x = lb.uq[i] = *(volatile uint64_t *)data;
172#endif
64419e47 173 else if (width == 2)
150f7236 174 x = lb.us[i] = *(volatile uint16_t *)data;
64419e47 175 else
150f7236 176 x = lb.uc[i] = *(volatile uint8_t *)data;
f60662de
SG
177#if defined(CONFIG_SPL_BUILD)
178 printf(" %x", (uint)x);
179#elif defined(CONFIG_SYS_SUPPORT_64BIT_DATA)
66da9beb 180 printf(" %0*llx", width * 2, (long long)x);
4d1fd7f1 181#else
64419e47 182 printf(" %0*x", width * 2, x);
4d1fd7f1 183#endif
c95c4280
GL
184 data += width;
185 }
186
efd7c114
AB
187 while (thislinelen < linelen) {
188 /* fill line with whitespace for nice ASCII print */
189 for (i=0; i<width*2+1; i++)
190 puts(" ");
191 linelen--;
192 }
193
c95c4280 194 /* Print data in ASCII characters */
efd7c114 195 for (i = 0; i < thislinelen * width; i++) {
150f7236
RM
196 if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
197 lb.uc[i] = '.';
198 }
199 lb.uc[i] = '\0';
200 printf(" %s\n", lb.uc);
c95c4280
GL
201
202 /* update references */
efd7c114
AB
203 addr += thislinelen * width;
204 count -= thislinelen;
c95c4280
GL
205
206 if (ctrlc())
207 return -1;
208 }
209
210 return 0;
211}