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>
22 #include <cpu/x86/car.h>
26 * Structure describing console buffer. It is overlaid on a flat memory area,
27 * whith buffer_body covering the extent of the memory. Once the buffer is
28 * full, the cursor keeps going but the data is dropped on the floor. This
29 * allows to tell how much data was lost in the process.
31 struct cbmem_console {
35 } __attribute__ ((__packed__));
39 * While running from ROM, before DRAM is initialized, some area in cache as
40 * ram space is used for the console buffer storage. The size and location of
41 * the area are defined in the config.
44 static struct cbmem_console car_cbmem_console CAR_CBMEM;
45 #define cbmem_console_p (&car_cbmem_console)
48 * Once DRAM is initialized and the cache as ram mode is disabled, while still
49 * running from ROM, the console buffer in the cache as RAM area becomes
52 * By this time the console log buffer is already available in
53 * CBMEM. The location at 0x600 is used as the redirect pointer allowing to
54 * find out where the actual console log buffer is.
56 #define CBMEM_CONSOLE_REDIRECT (*((struct cbmem_console **)0x600))
60 * When running from RAM, a lot of console output is generated before CBMEM is
61 * reinitialized. This static buffer is used to store that output temporarily,
62 * to be concatenated with the CBMEM console buffer contents accumulated
63 * during the ROM stage, once CBMEM becomes available at RAM stage.
65 static u8 static_console[40000];
66 static struct cbmem_console *cbmem_console_p;
69 void cbmemc_init(void)
72 cbmem_console_p->buffer_size = CONFIG_CONSOLE_CAR_BUFFER_SIZE -
73 sizeof(struct cbmem_console);
76 * Initializing before CBMEM is available, use static buffer to store
79 cbmem_console_p = (struct cbmem_console *) static_console;
80 cbmem_console_p->buffer_size = sizeof(static_console) -
81 sizeof(struct cbmem_console);
83 cbmem_console_p->buffer_cursor = 0;
86 void cbmemc_tx_byte(unsigned char data)
88 struct cbmem_console *cbm_cons_p = cbmem_console_p;
92 * This check allows to tell if the cache as RAM mode has been exited
93 * or not. If it has been exited, the real memory is being used
94 * (resulting in the variable on the stack located below
95 * DCACHE_RAM_BASE), use the redirect pointer to find out where the
96 * actual console buffer is.
98 if ((uintptr_t)&cursor < (uintptr_t)&car_cbmem_console)
99 cbm_cons_p = CBMEM_CONSOLE_REDIRECT;
104 cursor = cbm_cons_p->buffer_cursor++;
105 if (cursor < cbm_cons_p->buffer_size)
106 cbm_cons_p->buffer_body[cursor] = data;
110 * Copy the current console buffer (either from the cache as RAM area, or from
111 * the static buffer, pointed at by cbmem_console_p) into the CBMEM console
112 * buffer space (pointed at by new_cons_p), concatenating the copied data with
113 * the CBMEM console buffer contents.
115 * If there is overflow - add to the destination area a string, reporting the
116 * overflow and the number of dropped charactes.
118 static void copy_console_buffer(struct cbmem_console *new_cons_p)
121 u32 cursor = new_cons_p->buffer_cursor;
122 int overflow = cbmem_console_p->buffer_cursor >
123 cbmem_console_p->buffer_size;
125 copy_size = overflow ?
126 cbmem_console_p->buffer_size : cbmem_console_p->buffer_cursor;
128 memcpy(new_cons_p->buffer_body + cursor,
129 cbmem_console_p->buffer_body,
135 const char loss_str1[] = "\n\n*** Log truncated, ";
136 const char loss_str2[] = " characters dropped. ***\n\n";
137 u32 dropped_chars = cbmem_console_p->buffer_cursor - copy_size;
140 * When running from ROM sprintf is not available, a simple
141 * itoa implementation is used instead.
143 int got_first_digit = 0;
145 /* Way more than possible number of dropped characters. */
148 strcpy((char *)new_cons_p->buffer_body + cursor, loss_str1);
149 cursor += sizeof(loss_str1) - 1;
152 int digit = dropped_chars / mult;
153 if (got_first_digit || digit) {
154 new_cons_p->buffer_body[cursor++] = digit + '0';
155 dropped_chars %= mult;
156 /* Excessive, but keeps it simple */
162 strcpy((char *)new_cons_p->buffer_body + cursor, loss_str2);
163 cursor += sizeof(loss_str2) - 1;
165 new_cons_p->buffer_cursor = cursor;
168 void cbmemc_reinit(void)
170 struct cbmem_console *cbm_cons_p;
173 cbm_cons_p = cbmem_add(CBMEM_ID_CONSOLE,
174 CONFIG_CONSOLE_CBMEM_BUFFER_SIZE);
176 CBMEM_CONSOLE_REDIRECT = NULL;
180 cbm_cons_p->buffer_size = CONFIG_CONSOLE_CBMEM_BUFFER_SIZE -
181 sizeof(struct cbmem_console);
183 cbm_cons_p->buffer_cursor = 0;
185 copy_console_buffer(cbm_cons_p);
187 CBMEM_CONSOLE_REDIRECT = cbm_cons_p;
189 cbm_cons_p = cbmem_find(CBMEM_ID_CONSOLE);
194 copy_console_buffer(cbm_cons_p);
196 cbmem_console_p = cbm_cons_p;