diff options
author | Daniel Cederman <cederman@gaisler.com> | 2021-04-30 09:35:56 +0300 |
---|---|---|
committer | Anup Patel <anup@brainfault.org> | 2021-05-06 11:51:07 +0300 |
commit | 117fb6dcb147913034fe82d0ed038fa7a05f1a1d (patch) | |
tree | 9724a5e0e5d52bcc1557fcedb83d359b7172de43 /lib/utils/serial | |
parent | 632e27bb91b824974b88dd46f8419729d7f9825d (diff) | |
download | opensbi-117fb6dcb147913034fe82d0ed038fa7a05f1a1d.tar.xz |
lib: utils/serial: Add support for Gaisler APBUART
This patch adds support for the UART used by the NOEL-V processor.
Cobham Gaisler's NOEL-V RISC-V processor IP is available under GPL
and commercial license and is described in more detail at
https://www.gaisler.com/noelv.
Signed-off-by: Daniel Cederman <cederman@gaisler.com>
Reviewed-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
Diffstat (limited to 'lib/utils/serial')
-rw-r--r-- | lib/utils/serial/fdt_serial.c | 2 | ||||
-rw-r--r-- | lib/utils/serial/fdt_serial_gaisler.c | 35 | ||||
-rw-r--r-- | lib/utils/serial/gaisler-uart.c | 86 | ||||
-rw-r--r-- | lib/utils/serial/objects.mk | 2 |
4 files changed, 125 insertions, 0 deletions
diff --git a/lib/utils/serial/fdt_serial.c b/lib/utils/serial/fdt_serial.c index 43c55e8..bad3387 100644 --- a/lib/utils/serial/fdt_serial.c +++ b/lib/utils/serial/fdt_serial.c @@ -16,12 +16,14 @@ extern struct fdt_serial fdt_serial_uart8250; extern struct fdt_serial fdt_serial_sifive; extern struct fdt_serial fdt_serial_htif; extern struct fdt_serial fdt_serial_shakti; +extern struct fdt_serial fdt_serial_gaisler; static struct fdt_serial *serial_drivers[] = { &fdt_serial_uart8250, &fdt_serial_sifive, &fdt_serial_htif, &fdt_serial_shakti, + &fdt_serial_gaisler }; static struct fdt_serial dummy = { diff --git a/lib/utils/serial/fdt_serial_gaisler.c b/lib/utils/serial/fdt_serial_gaisler.c new file mode 100644 index 0000000..9a9f9a8 --- /dev/null +++ b/lib/utils/serial/fdt_serial_gaisler.c @@ -0,0 +1,35 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2021 Cobham Gaisler AB. + * + * Authors: + * Daniel Cederman <cederman@gaisler.com> + */ + +#include <sbi_utils/fdt/fdt_helper.h> +#include <sbi_utils/serial/fdt_serial.h> +#include <sbi_utils/serial/gaisler-uart.h> + +static int serial_gaisler_init(void *fdt, int nodeoff, + const struct fdt_match *match) +{ + int rc; + struct platform_uart_data uart; + + rc = fdt_parse_gaisler_uart_node(fdt, nodeoff, &uart); + if (rc) + return rc; + + return gaisler_uart_init(uart.addr, uart.freq, uart.baud); +} + +static const struct fdt_match serial_gaisler_match[] = { + { .compatible = "gaisler,apbuart" }, + {}, +}; + +struct fdt_serial fdt_serial_gaisler = { + .match_table = serial_gaisler_match, + .init = serial_gaisler_init +}; diff --git a/lib/utils/serial/gaisler-uart.c b/lib/utils/serial/gaisler-uart.c new file mode 100644 index 0000000..49298e9 --- /dev/null +++ b/lib/utils/serial/gaisler-uart.c @@ -0,0 +1,86 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2021 Cobham Gaisler AB. + * + * Authors: + * Daniel Cederman <cederman@gaisler.com> + */ + +#include <sbi/riscv_io.h> +#include <sbi/sbi_console.h> +#include <sbi_utils/serial/gaisler-uart.h> + +/* clang-format off */ + +#define UART_REG_DATA 0 +#define UART_REG_STATUS 1 +#define UART_REG_CTRL 2 +#define UART_REG_SCALER 3 + +#define UART_DATA_DATA 0x000000ff +#define UART_STATUS_FIFOFULL 0x00000200 +#define UART_STATUS_DATAREADY 0x00000001 + +#define UART_CTRL_DB (1<<11) +#define UART_CTRL_FL (1<<6) +#define UART_CTRL_TE (1<<1) +#define UART_CTRL_RE (1<<0) + +/* clang-format on */ + +static volatile void *uart_base; + +static u32 get_reg(u32 num) +{ + return readl(uart_base + (num * 0x4)); +} + +static void set_reg(u32 num, u32 val) +{ + writel(val, uart_base + (num * 0x4)); +} + +static void gaisler_uart_putc(char ch) +{ + while (get_reg(UART_REG_STATUS) & UART_STATUS_FIFOFULL) + ; + + set_reg(UART_REG_DATA, ch); +} + +static int gaisler_uart_getc(void) +{ + u32 ret = get_reg(UART_REG_STATUS); + if (!(ret & UART_STATUS_DATAREADY)) + return -1; + return get_reg(UART_REG_DATA) & UART_DATA_DATA; +} + +static struct sbi_console_device gaisler_console = { + .name = "gaisler_uart", + .console_putc = gaisler_uart_putc, + .console_getc = gaisler_uart_getc +}; + +int gaisler_uart_init(unsigned long base, u32 in_freq, u32 baudrate) +{ + u32 ctrl; + + uart_base = (volatile void *)base; + + /* Configure baudrate */ + if (in_freq) + set_reg(UART_REG_SCALER, in_freq / (baudrate * 8 + 7)); + + ctrl = get_reg(UART_REG_CTRL); + /* Preserve debug mode and flow control */ + ctrl &= (UART_CTRL_DB | UART_CTRL_FL); + /* Enable TX and RX */ + ctrl |= UART_CTRL_TE | UART_CTRL_RE; + set_reg(UART_REG_CTRL, ctrl); + + sbi_console_set_device(&gaisler_console); + + return 0; +} diff --git a/lib/utils/serial/objects.mk b/lib/utils/serial/objects.mk index c0746f0..9fb0902 100644 --- a/lib/utils/serial/objects.mk +++ b/lib/utils/serial/objects.mk @@ -8,10 +8,12 @@ # libsbiutils-objs-y += serial/fdt_serial.o +libsbiutils-objs-y += serial/fdt_serial_gaisler.o libsbiutils-objs-y += serial/fdt_serial_htif.o libsbiutils-objs-y += serial/fdt_serial_shakti.o libsbiutils-objs-y += serial/fdt_serial_sifive.o libsbiutils-objs-y += serial/fdt_serial_uart8250.o +libsbiutils-objs-y += serial/gaisler-uart.o libsbiutils-objs-y += serial/shakti-uart.o libsbiutils-objs-y += serial/sifive-uart.o libsbiutils-objs-y += serial/uart8250.o |