xref: /openbmc/u-boot/drivers/misc/cbmem_console.c (revision 7682a998)
1 /*
2  * Copyright (C) 2011 The ChromiumOS Authors.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
16  */
17 
18 #include <common.h>
19 
20 #ifndef CONFIG_SYS_COREBOOT
21 #error This driver requires coreboot
22 #endif
23 
24 #include <asm/arch/sysinfo.h>
25 
26 struct cbmem_console {
27 	u32 buffer_size;
28 	u32 buffer_cursor;
29 	u8  buffer_body[0];
30 }  __attribute__ ((__packed__));
31 
32 static struct cbmem_console *cbmem_console_p;
33 
34 void cbmemc_putc(struct stdio_dev *dev, char data)
35 {
36 	int cursor;
37 
38 	cursor = cbmem_console_p->buffer_cursor++;
39 	if (cursor < cbmem_console_p->buffer_size)
40 		cbmem_console_p->buffer_body[cursor] = data;
41 }
42 
43 void cbmemc_puts(struct stdio_dev *dev, const char *str)
44 {
45 	char c;
46 
47 	while ((c = *str++) != 0)
48 		cbmemc_putc(dev, c);
49 }
50 
51 int cbmemc_init(void)
52 {
53 	int rc;
54 	struct stdio_dev cons_dev;
55 	cbmem_console_p = lib_sysinfo.cbmem_cons;
56 
57 	memset(&cons_dev, 0, sizeof(cons_dev));
58 
59 	strcpy(cons_dev.name, "cbmem");
60 	cons_dev.flags = DEV_FLAGS_OUTPUT; /* Output only */
61 	cons_dev.putc  = cbmemc_putc;
62 	cons_dev.puts  = cbmemc_puts;
63 
64 	rc = stdio_register(&cons_dev);
65 
66 	return (rc == 0) ? 1 : rc;
67 }
68