blob: ba3a599c4a51714ce58b04dd9af2347f1b1d6640 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Vadim Bendeburyb012bc92012-10-12 18:48:48 +00002/*
3 * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved.
Vadim Bendeburyb012bc92012-10-12 18:48:48 +00004 */
5
6#include <common.h>
Simon Glass24b852a2015-11-08 23:47:45 -07007#include <console.h>
Simon Glasse35b6492021-03-15 18:00:18 +13008#include <asm/cb_sysinfo.h>
Vadim Bendeburyb012bc92012-10-12 18:48:48 +00009
Simon Glass709ea542014-07-23 06:54:59 -060010void cbmemc_putc(struct stdio_dev *dev, char data)
Vadim Bendeburyb012bc92012-10-12 18:48:48 +000011{
Simon Glassfb5cfbe2023-09-10 13:13:02 -060012 const struct sysinfo_t *sysinfo = cb_get_sysinfo();
13 struct cbmem_console *cons;
14 uint pos, flags;
Vadim Bendeburyb012bc92012-10-12 18:48:48 +000015
Simon Glassfb5cfbe2023-09-10 13:13:02 -060016 if (!sysinfo)
17 return;
18 cons = sysinfo->cbmem_cons;
19 if (!cons)
20 return;
21
22 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 Bendeburyb012bc92012-10-12 18:48:48 +000039}
40
Simon Glass709ea542014-07-23 06:54:59 -060041void cbmemc_puts(struct stdio_dev *dev, const char *str)
Vadim Bendeburyb012bc92012-10-12 18:48:48 +000042{
43 char c;
44
45 while ((c = *str++) != 0)
Simon Glass709ea542014-07-23 06:54:59 -060046 cbmemc_putc(dev, c);
Vadim Bendeburyb012bc92012-10-12 18:48:48 +000047}
48
49int cbmemc_init(void)
50{
51 int rc;
52 struct stdio_dev cons_dev;
Vadim Bendeburyb012bc92012-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}