xref: /rk3399_rockchip-uboot/lib/display_options.c (revision efd7c11404e59874d4da86d04cab4acacf77d793)
178acc472SPeter Tyser /*
278acc472SPeter Tyser  * (C) Copyright 2000-2002
378acc472SPeter Tyser  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
478acc472SPeter Tyser  *
578acc472SPeter Tyser  * See file CREDITS for list of people who contributed to this
678acc472SPeter Tyser  * project.
778acc472SPeter Tyser  *
878acc472SPeter Tyser  * This program is free software; you can redistribute it and/or
978acc472SPeter Tyser  * modify it under the terms of the GNU General Public License as
1078acc472SPeter Tyser  * published by the Free Software Foundation; either version 2 of
1178acc472SPeter Tyser  * the License, or (at your option) any later version.
1278acc472SPeter Tyser  *
1378acc472SPeter Tyser  * This program is distributed in the hope that it will be useful,
1478acc472SPeter Tyser  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1578acc472SPeter Tyser  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1678acc472SPeter Tyser  * GNU General Public License for more details.
1778acc472SPeter Tyser  *
1878acc472SPeter Tyser  * You should have received a copy of the GNU General Public License
1978acc472SPeter Tyser  * along with this program; if not, write to the Free Software
2078acc472SPeter Tyser  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
2178acc472SPeter Tyser  * MA 02111-1307 USA
2278acc472SPeter Tyser  */
2378acc472SPeter Tyser 
2478acc472SPeter Tyser #include <config.h>
2578acc472SPeter Tyser #include <common.h>
2609c2e90cSAndreas Bießmann #include <version.h>
2778acc472SPeter Tyser #include <linux/ctype.h>
2878acc472SPeter Tyser #include <asm/io.h>
2978acc472SPeter Tyser 
3078acc472SPeter Tyser int display_options (void)
3178acc472SPeter Tyser {
3278acc472SPeter Tyser #if defined(BUILD_TAG)
3378acc472SPeter Tyser 	printf ("\n\n%s, Build: %s\n\n", version_string, BUILD_TAG);
3478acc472SPeter Tyser #else
3578acc472SPeter Tyser 	printf ("\n\n%s\n\n", version_string);
3678acc472SPeter Tyser #endif
3778acc472SPeter Tyser 	return 0;
3878acc472SPeter Tyser }
3978acc472SPeter Tyser 
4078acc472SPeter Tyser /*
414b42c905STimur Tabi  * print sizes as "xxx KiB", "xxx.y KiB", "xxx MiB", "xxx.y MiB",
424b42c905STimur Tabi  * xxx GiB, xxx.y GiB, etc as needed; allow for optional trailing string
4378acc472SPeter Tyser  * (like "\n")
4478acc472SPeter Tyser  */
454b42c905STimur Tabi void print_size(unsigned long long size, const char *s)
4678acc472SPeter Tyser {
4752dbac69STimur Tabi 	unsigned long m = 0, n;
48f2d76ae4SNick Thompson 	unsigned long long f;
494b42c905STimur Tabi 	static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
50f2d76ae4SNick Thompson 	unsigned long d = 10 * ARRAY_SIZE(names);
514b42c905STimur Tabi 	char c = 0;
524b42c905STimur Tabi 	unsigned int i;
5378acc472SPeter Tyser 
54f2d76ae4SNick Thompson 	for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
55f2d76ae4SNick Thompson 		if (size >> d) {
564b42c905STimur Tabi 			c = names[i];
574b42c905STimur Tabi 			break;
5878acc472SPeter Tyser 		}
5978acc472SPeter Tyser 	}
6078acc472SPeter Tyser 
614b42c905STimur Tabi 	if (!c) {
624b42c905STimur Tabi 		printf("%llu Bytes%s", size, s);
634b42c905STimur Tabi 		return;
644b42c905STimur Tabi 	}
654b42c905STimur Tabi 
66f2d76ae4SNick Thompson 	n = size >> d;
67f2d76ae4SNick Thompson 	f = size & ((1ULL << d) - 1);
6878acc472SPeter Tyser 
6978acc472SPeter Tyser 	/* If there's a remainder, deal with it */
70f2d76ae4SNick Thompson 	if (f) {
71f2d76ae4SNick Thompson 		m = (10ULL * f + (1ULL << (d - 1))) >> d;
7278acc472SPeter Tyser 
7378acc472SPeter Tyser 		if (m >= 10) {
7478acc472SPeter Tyser 			m -= 10;
7578acc472SPeter Tyser 			n += 1;
7678acc472SPeter Tyser 		}
7778acc472SPeter Tyser 	}
7878acc472SPeter Tyser 
794b42c905STimur Tabi 	printf ("%lu", n);
8078acc472SPeter Tyser 	if (m) {
8178acc472SPeter Tyser 		printf (".%ld", m);
8278acc472SPeter Tyser 	}
834b42c905STimur Tabi 	printf (" %ciB%s", c, s);
8478acc472SPeter Tyser }
8578acc472SPeter Tyser 
8678acc472SPeter Tyser /*
8778acc472SPeter Tyser  * Print data buffer in hex and ascii form to the terminal.
8878acc472SPeter Tyser  *
8978acc472SPeter Tyser  * data reads are buffered so that each memory address is only read once.
9078acc472SPeter Tyser  * Useful when displaying the contents of volatile registers.
9178acc472SPeter Tyser  *
9278acc472SPeter Tyser  * parameters:
9378acc472SPeter Tyser  *    addr: Starting address to display at start of line
9478acc472SPeter Tyser  *    data: pointer to data buffer
9578acc472SPeter Tyser  *    width: data value width.  May be 1, 2, or 4.
9678acc472SPeter Tyser  *    count: number of values to display
9778acc472SPeter Tyser  *    linelen: Number of values to print per line; specify 0 for default length
9878acc472SPeter Tyser  */
9978acc472SPeter Tyser #define MAX_LINE_LENGTH_BYTES (64)
10078acc472SPeter Tyser #define DEFAULT_LINE_LENGTH_BYTES (16)
101bda32ffcSSimon Glass int print_buffer(ulong addr, const void *data, uint width, uint count,
102bda32ffcSSimon Glass 		 uint linelen)
10378acc472SPeter Tyser {
104150f7236SReinhard Meyer 	/* linebuf as a union causes proper alignment */
105150f7236SReinhard Meyer 	union linebuf {
106150f7236SReinhard Meyer 		uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1];
107150f7236SReinhard Meyer 		uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1];
108150f7236SReinhard Meyer 		uint8_t  uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1];
109150f7236SReinhard Meyer 	} lb;
11078acc472SPeter Tyser 	int i;
11178acc472SPeter Tyser 
11278acc472SPeter Tyser 	if (linelen*width > MAX_LINE_LENGTH_BYTES)
11378acc472SPeter Tyser 		linelen = MAX_LINE_LENGTH_BYTES / width;
11478acc472SPeter Tyser 	if (linelen < 1)
11578acc472SPeter Tyser 		linelen = DEFAULT_LINE_LENGTH_BYTES / width;
11678acc472SPeter Tyser 
11778acc472SPeter Tyser 	while (count) {
118*efd7c114SAndreas Bießmann 		uint thislinelen = linelen;
11978acc472SPeter Tyser 		printf("%08lx:", addr);
12078acc472SPeter Tyser 
12178acc472SPeter Tyser 		/* check for overflow condition */
122*efd7c114SAndreas Bießmann 		if (count < thislinelen)
123*efd7c114SAndreas Bießmann 			thislinelen = count;
12478acc472SPeter Tyser 
12578acc472SPeter Tyser 		/* Copy from memory into linebuf and print hex values */
126*efd7c114SAndreas Bießmann 		for (i = 0; i < thislinelen; i++) {
12764419e47SMike Frysinger 			uint32_t x;
12864419e47SMike Frysinger 			if (width == 4)
129150f7236SReinhard Meyer 				x = lb.ui[i] = *(volatile uint32_t *)data;
13064419e47SMike Frysinger 			else if (width == 2)
131150f7236SReinhard Meyer 				x = lb.us[i] = *(volatile uint16_t *)data;
13264419e47SMike Frysinger 			else
133150f7236SReinhard Meyer 				x = lb.uc[i] = *(volatile uint8_t *)data;
13464419e47SMike Frysinger 			printf(" %0*x", width * 2, x);
13578acc472SPeter Tyser 			data += width;
13678acc472SPeter Tyser 		}
13778acc472SPeter Tyser 
138*efd7c114SAndreas Bießmann 		while (thislinelen < linelen) {
139*efd7c114SAndreas Bießmann 			/* fill line with whitespace for nice ASCII print */
140*efd7c114SAndreas Bießmann 			for (i=0; i<width*2+1; i++)
141*efd7c114SAndreas Bießmann 				puts(" ");
142*efd7c114SAndreas Bießmann 			linelen--;
143*efd7c114SAndreas Bießmann 		}
144*efd7c114SAndreas Bießmann 
14578acc472SPeter Tyser 		/* Print data in ASCII characters */
146*efd7c114SAndreas Bießmann 		for (i = 0; i < thislinelen * width; i++) {
147150f7236SReinhard Meyer 			if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
148150f7236SReinhard Meyer 				lb.uc[i] = '.';
149150f7236SReinhard Meyer 		}
150150f7236SReinhard Meyer 		lb.uc[i] = '\0';
151150f7236SReinhard Meyer 		printf("    %s\n", lb.uc);
15278acc472SPeter Tyser 
15378acc472SPeter Tyser 		/* update references */
154*efd7c114SAndreas Bießmann 		addr += thislinelen * width;
155*efd7c114SAndreas Bießmann 		count -= thislinelen;
15678acc472SPeter Tyser 
15778acc472SPeter Tyser 		if (ctrlc())
15878acc472SPeter Tyser 			return -1;
15978acc472SPeter Tyser 	}
16078acc472SPeter Tyser 
16178acc472SPeter Tyser 	return 0;
16278acc472SPeter Tyser }
163