Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Vadim Bendebury | e53e53b | 2012-10-12 18:48:48 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 The ChromiumOS Authors. All rights reserved. |
Vadim Bendebury | e53e53b | 2012-10-12 18:48:48 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 7 | #include <console.h> |
Simon Glass | 9a01fd9 | 2021-03-15 18:00:18 +1300 | [diff] [blame] | 8 | #include <asm/cb_sysinfo.h> |
Vadim Bendebury | e53e53b | 2012-10-12 18:48:48 +0000 | [diff] [blame] | 9 | |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 10 | void cbmemc_putc(struct stdio_dev *dev, char data) |
Vadim Bendebury | e53e53b | 2012-10-12 18:48:48 +0000 | [diff] [blame] | 11 | { |
Simon Glass | 036ca14 | 2023-09-10 13:13:02 -0600 | [diff] [blame] | 12 | 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 Bendebury | e53e53b | 2012-10-12 18:48:48 +0000 | [diff] [blame] | 21 | |
Simon Glass | 036ca14 | 2023-09-10 13:13:02 -0600 | [diff] [blame] | 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 Bendebury | e53e53b | 2012-10-12 18:48:48 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 41 | void cbmemc_puts(struct stdio_dev *dev, const char *str) |
Vadim Bendebury | e53e53b | 2012-10-12 18:48:48 +0000 | [diff] [blame] | 42 | { |
| 43 | char c; |
| 44 | |
| 45 | while ((c = *str++) != 0) |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 46 | cbmemc_putc(dev, c); |
Vadim Bendebury | e53e53b | 2012-10-12 18:48:48 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | int cbmemc_init(void) |
| 50 | { |
| 51 | int rc; |
| 52 | struct stdio_dev cons_dev; |
Vadim Bendebury | e53e53b | 2012-10-12 18:48:48 +0000 | [diff] [blame] | 53 | |
| 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 | } |