blob: ba3a599c4a51714ce58b04dd9af2347f1b1d6640 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Vadim Bendeburye53e53b2012-10-12 18:48:48 +00002/*
3 * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
Vadim Bendeburye53e53b2012-10-12 18:48:48 +00004 */
5
6#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -07007#include <console.h>
Simon Glass9a01fd92021-03-15 18:00:18 +13008#include <asm/cb_sysinfo.h>
Vadim Bendeburye53e53b2012-10-12 18:48:48 +00009
Simon Glass0d1e1f72014-07-23 06:54:59 -060010void cbmemc_putc(struct stdio_dev *dev, char data)
Vadim Bendeburye53e53b2012-10-12 18:48:48 +000011{
Simon Glass036ca142023-09-10 13:13:02 -060012 const struct sysinfo_t *sysinfo = cb_get_sysinfo();
13 struct cbmem_console *cons;
14 uint pos, flags;
15
16 if (!sysinfo)
17 return;
18 cons = sysinfo->cbmem_cons;
19 if (!cons)
20 return;
Vadim Bendeburye53e53b2012-10-12 18:48:48 +000021
Simon Glass036ca142023-09-10 13:13:02 -060022 pos = cons->cursor & CBMC_CURSOR_MASK;
23
24 /* preserve the overflow flag if present */
25 flags = cons->cursor & ~CBMC_CURSOR_MASK;
26
27 cons->body[pos++] = data;
28
29 /*
30 * Deal with overflow - the flag may be cleared by another program which
31 * reads the buffer out later, e.g. Linux
32 */
33 if (pos >= cons->size) {
34 pos = 0;
35 flags |= CBMC_OVERFLOW;
36 }
37
38 cons->cursor = flags | pos;
Vadim Bendeburye53e53b2012-10-12 18:48:48 +000039}
40
Simon Glass0d1e1f72014-07-23 06:54:59 -060041void cbmemc_puts(struct stdio_dev *dev, const char *str)
Vadim Bendeburye53e53b2012-10-12 18:48:48 +000042{
43 char c;
44
45 while ((c = *str++) != 0)
Simon Glass0d1e1f72014-07-23 06:54:59 -060046 cbmemc_putc(dev, c);
Vadim Bendeburye53e53b2012-10-12 18:48:48 +000047}
48
49int cbmemc_init(void)
50{
51 int rc;
52 struct stdio_dev cons_dev;
Vadim Bendeburye53e53b2012-10-12 18:48:48 +000053
54 memset(&cons_dev, 0, sizeof(cons_dev));
55
56 strcpy(cons_dev.name, "cbmem");
57 cons_dev.flags = DEV_FLAGS_OUTPUT; /* Output only */
58 cons_dev.putc = cbmemc_putc;
59 cons_dev.puts = cbmemc_puts;
60
61 rc = stdio_register(&cons_dev);
62
63 return (rc == 0) ? 1 : rc;
64}