2 * This file is part of the coreboot project.
4 * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
20 #include <console/console.h>
25 * Structure describing console buffer. It is overlaid on a flat memory area,
26 * whith buffer_body covering the extent of the memory. Once the buffer is
27 * full, the cursor keeps going but the data is dropped on the floor. This
28 * allows to tell how much data was lost in the process.
30 struct cbmem_console {
34 } __attribute__ ((__packed__));
38 * While running from ROM, before DRAM is initialized, some area in cache as
39 * ram space is used for the console buffer storage. The size and location of
40 * the area are defined in the config.
42 #define cbmem_console_p ((struct cbmem_console *)CONFIG_DCACHE_RAM_BASE)
45 * Once DRAM is initialized and the cache as ram mode is disabled, while still
46 * running from ROM, the console buffer in the cache as RAM area becomes
49 * By this time the console log buffer is already available in
50 * CBMEM. The location at 0x600 is used as the redirect pointer allowing to
51 * find out where the actual console log buffer is.
53 #define CBMEM_CONSOLE_REDIRECT (*((struct cbmem_console **)0x600))
57 * When running from RAM, a lot of console output is generated before CBMEM is
58 * reinitialized. This static buffer is used to store that output temporarily,
59 * to be concatenated with the CBMEM console buffer contents accumulated
60 * during the ROM stage, once CBMEM becomes available at RAM stage.
62 static u8 static_console[40000];
63 static struct cbmem_console *cbmem_console_p;
66 void cbmemc_init(void)
69 cbmem_console_p->buffer_size = CONFIG_CONSOLE_CAR_BUFFER_SIZE -
70 sizeof(struct cbmem_console);
73 * Initializing before CBMEM is available, use static buffer to store
76 cbmem_console_p = (struct cbmem_console *) static_console;
77 cbmem_console_p->buffer_size = sizeof(static_console) -
78 sizeof(struct cbmem_console);
80 cbmem_console_p->buffer_cursor = 0;
83 void cbmemc_tx_byte(unsigned char data)
85 struct cbmem_console *cbm_cons_p = cbmem_console_p;
89 * This check allows to tell if the cache as RAM mode has been exited
90 * or not. If it has been exited, the real memory is being used
91 * (resulting in the variable on the stack located below
92 * DCACHE_RAM_BASE), use the redirect pointer to find out where the
93 * actual console buffer is.
95 if ((u32)&cursor < (u32)CONFIG_DCACHE_RAM_BASE)
96 cbm_cons_p = CBMEM_CONSOLE_REDIRECT;
101 cursor = cbm_cons_p->buffer_cursor++;
102 if (cursor < cbm_cons_p->buffer_size)
103 cbm_cons_p->buffer_body[cursor] = data;
107 * Copy the current console buffer (either from the cache as RAM area, or from
108 * the static buffer, pointed at by cbmem_console_p) into the CBMEM console
109 * buffer space (pointed at by new_cons_p), concatenating the copied data with
110 * the CBMEM console buffer contents.
112 * If there is overflow - add to the destination area a string, reporting the
113 * overflow and the number of dropped charactes.
115 static void copy_console_buffer(struct cbmem_console *new_cons_p)
118 u32 cursor = new_cons_p->buffer_cursor;
119 int overflow = cbmem_console_p->buffer_cursor >
120 cbmem_console_p->buffer_size;
122 copy_size = overflow ?
123 cbmem_console_p->buffer_size : cbmem_console_p->buffer_cursor;
125 memcpy(new_cons_p->buffer_body + cursor,
126 cbmem_console_p->buffer_body,
132 const char loss_str1[] = "\n\n*** Log truncated, ";
133 const char loss_str2[] = " characters dropped. ***\n\n";
134 u32 dropped_chars = cbmem_console_p->buffer_cursor - copy_size;
137 * When running from ROM sprintf is not available, a simple
138 * itoa implementation is used instead.
140 int got_first_digit = 0;
142 /* Way more than possible number of dropped characters. */
145 strcpy((char *)new_cons_p->buffer_body + cursor, loss_str1);
146 cursor += sizeof(loss_str1) - 1;
149 int digit = dropped_chars / mult;
150 if (got_first_digit || digit) {
151 new_cons_p->buffer_body[cursor++] = digit + '0';
152 dropped_chars %= mult;
153 /* Excessive, but keeps it simple */
159 strcpy((char *)new_cons_p->buffer_body + cursor, loss_str2);
160 cursor += sizeof(loss_str2) - 1;
162 new_cons_p->buffer_cursor = cursor;
165 void cbmemc_reinit(void)
167 struct cbmem_console *cbm_cons_p;
170 cbm_cons_p = cbmem_add(CBMEM_ID_CONSOLE,
171 CONFIG_CONSOLE_CBMEM_BUFFER_SIZE);
173 CBMEM_CONSOLE_REDIRECT = NULL;
177 cbm_cons_p->buffer_size = CONFIG_CONSOLE_CBMEM_BUFFER_SIZE -
178 sizeof(struct cbmem_console);
180 cbm_cons_p->buffer_cursor = 0;
182 copy_console_buffer(cbm_cons_p);
184 CBMEM_CONSOLE_REDIRECT = cbm_cons_p;
186 cbm_cons_p = cbmem_find(CBMEM_ID_CONSOLE);
191 copy_console_buffer(cbm_cons_p);
193 cbmem_console_p = cbm_cons_p;