From 1232cd46905cc5a59dae2195c39eb5dd5742a754 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Mon, 25 Jun 2018 08:44:16 +0200 Subject: bsp/riscv: Add device tree support for console Update #3433. --- bsps/riscv/riscv/console/console-config.c | 127 ++++++++++++++++++++++ bsps/riscv/riscv/console/console-io.c | 162 ----------------------------- bsps/riscv/riscv/console/htif.c | 132 +++++++++++++++++++++++ bsps/riscv/riscv/include/dev/serial/htif.h | 58 +++++++++++ c/src/lib/libbsp/riscv/riscv/Makefile.am | 9 +- c/src/lib/libbsp/riscv/riscv/configure.ac | 3 + 6 files changed, 324 insertions(+), 167 deletions(-) create mode 100644 bsps/riscv/riscv/console/console-config.c delete mode 100644 bsps/riscv/riscv/console/console-io.c create mode 100644 bsps/riscv/riscv/console/htif.c create mode 100644 bsps/riscv/riscv/include/dev/serial/htif.h diff --git a/bsps/riscv/riscv/console/console-config.c b/bsps/riscv/riscv/console/console-config.c new file mode 100644 index 0000000000..06b5d69662 --- /dev/null +++ b/bsps/riscv/riscv/console/console-config.c @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2018 embedded brains GmbH. All rights reserved. + * + * embedded brains GmbH + * Dornierstr. 4 + * 82178 Puchheim + * Germany + * + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.org/license/LICENSE. + */ + +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include + +#if RISCV_ENABLE_HTIF_SUPPORT > 0 +static htif_console_context htif_console_instance; +#endif + +static struct { + rtems_termios_device_context *context; + void (*write_polled)( + rtems_termios_device_context *base, + const char *buf, + size_t len + ); + int (*poll_char)(rtems_termios_device_context *base); +} riscv_console; + +static void riscv_output_char(char c) +{ + (*riscv_console.write_polled)(riscv_console.context, &c, 1); +} + +static int riscv_get_console_node(const void *fdt) +{ + const char *stdout_path; + int node; + + node = fdt_path_offset(fdt, "/chosen"); + + stdout_path = fdt_getprop(fdt, node, "stdout-path", NULL); + if (stdout_path == NULL) { + stdout_path = ""; + } + + return fdt_path_offset(fdt, stdout_path); +} + +static void riscv_console_probe(void) +{ + const void *fdt; + int node; + int console_node; + + fdt = bsp_fdt_get(); + console_node = riscv_get_console_node(fdt); + + node = fdt_next_node(fdt, -1, NULL); + + while (node >= 0) { +#if RISCV_ENABLE_HTIF_SUPPORT + if (fdt_node_check_compatible(fdt, node, "ucb,htif0") == 0) { + htif_console_context_init(&htif_console_instance.base, node); + + riscv_console.context = &htif_console_instance.base; + riscv_console.write_polled = htif_console_write_polled; + riscv_console.poll_char = htif_console_poll_char; + }; +#endif + + node = fdt_next_node(fdt, node, NULL); + } + + BSP_output_char = riscv_output_char; +} + +static void riscv_output_char_init(char c) +{ + riscv_console_probe(); + riscv_output_char(c); +} + +BSP_output_char_function_type BSP_output_char = riscv_output_char_init; + +BSP_polling_getchar_function_type BSP_poll_char = NULL; + +rtems_status_code console_initialize( + rtems_device_major_number major, + rtems_device_minor_number minor, + void *arg +) +{ + rtems_termios_device_context *base; + char htif_path[] = "/dev/ttyShtif"; + + rtems_termios_initialize(); + +#if RISCV_ENABLE_HTIF_SUPPORT + base = &htif_console_instance.base; + rtems_termios_device_install(htif_path, &htif_console_handler, NULL, base); + + if (base == riscv_console.context) { + link(htif_path, CONSOLE_DEVICE_NAME); + } +#endif + + return RTEMS_SUCCESSFUL; +} + +RTEMS_SYSINIT_ITEM( + riscv_console_probe, + RTEMS_SYSINIT_BSP_START, + RTEMS_SYSINIT_ORDER_LAST +); diff --git a/bsps/riscv/riscv/console/console-io.c b/bsps/riscv/riscv/console/console-io.c deleted file mode 100644 index 6fb5705467..0000000000 --- a/bsps/riscv/riscv/console/console-io.c +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright (c) 2015 University of York. - * Hesham Almatary - * - * Copyright (c) 2013, The Regents of the University of California (Regents). - * All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include - -/* Most of the code below is copied from riscv-pk project */ -# define TOHOST_CMD(dev, cmd, payload) \ - (((uint64_t)(dev) << 56) | ((uint64_t)(cmd) << 48) | (uint64_t)(payload)) - -#define FROMHOST_DEV(fromhost_value) ((uint64_t)(fromhost_value) >> 56) -#define FROMHOST_CMD(fromhost_value) ((uint64_t)(fromhost_value) << 8 >> 56) -#define FROMHOST_DATA(fromhost_value) ((uint64_t)(fromhost_value) << 16 >> 16) - -volatile uint64_t tohost __attribute__((section(".htif"))); -volatile uint64_t fromhost __attribute__((section(".htif"))); -volatile uint64_t riscv_fill_up_htif_section[510] __attribute__((section(".htif"))); -volatile int htif_console_buf; - -static void __check_fromhost(void) -{ - uint64_t fh = fromhost; - if (!fh) { - return; - } - fromhost = 0; - - // this should be from the console - assert(FROMHOST_DEV(fh) == 1); - switch (FROMHOST_CMD(fh)) { - case 0: - htif_console_buf = 1 + (uint8_t)FROMHOST_DATA(fh); - break; - case 1: - break; - default: - assert(0); - } -} - -static void __set_tohost(uintptr_t dev, uintptr_t cmd, uintptr_t data) -{ - while (tohost) { - __check_fromhost(); - } - tohost = TOHOST_CMD(dev, cmd, data); -} - -static int htif_console_getchar(void) -{ - __check_fromhost(); - int ch = htif_console_buf; - if (ch >= 0) { - htif_console_buf = -1; - __set_tohost(1, 0, 0); - } - - return ch - 1; -} - -static void htif_console_putchar(uint8_t ch) -{ - __set_tohost(1, 1, ch); -} - -void htif_poweroff(void) -{ - while (1) { - fromhost = 0; - tohost = 1; - } -} - -void console_initialize_hardware(void) -{ - /* Do nothing */ -} - -static void outbyte_console(char ch) -{ - htif_console_putchar(ch); -} - -static char inbyte_console(void) -{ - return htif_console_getchar(); -} - -/* - * console_outbyte_polled - * - * This routine transmits a character using polling. - */ -void console_outbyte_polled( - int port, - char ch -) -{ - outbyte_console( ch ); -} - -/* - * console_inbyte_nonblocking - * - * This routine polls for a character. - */ - -int console_inbyte_nonblocking(int port) -{ - char c; - - c = inbyte_console(); - if (!c) { - return -1; - } - return (int) c; -} - -/* - * To support printk - */ - -#include - -static void RISCV_output_char(char c) -{ - console_outbyte_polled( 0, c ); -} - -BSP_output_char_function_type BSP_output_char = RISCV_output_char; -BSP_polling_getchar_function_type BSP_poll_char = - (void *)console_inbyte_nonblocking; diff --git a/bsps/riscv/riscv/console/htif.c b/bsps/riscv/riscv/console/htif.c new file mode 100644 index 0000000000..6b9cdedc21 --- /dev/null +++ b/bsps/riscv/riscv/console/htif.c @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2015 University of York. + * Hesham Almatary + * + * Copyright (c) 2013, The Regents of the University of California (Regents). + * All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include + +/* Most of the code below is copied from riscv-pk project */ +# define TOHOST_CMD(dev, cmd, payload) \ + (((uint64_t)(dev) << 56) | ((uint64_t)(cmd) << 48) | (uint64_t)(payload)) + +#define FROMHOST_DEV(fromhost_value) ((uint64_t)(fromhost_value) >> 56) +#define FROMHOST_CMD(fromhost_value) ((uint64_t)(fromhost_value) << 8 >> 56) +#define FROMHOST_DATA(fromhost_value) ((uint64_t)(fromhost_value) << 16 >> 16) + +volatile uint64_t tohost __attribute__((section(".htif"))); +volatile uint64_t fromhost __attribute__((section(".htif"))); +volatile uint64_t riscv_fill_up_htif_section[510] __attribute__((section(".htif"))); +volatile int htif_console_buf; + +static void __check_fromhost(void) +{ + uint64_t fh = fromhost; + if (!fh) { + return; + } + fromhost = 0; + + // this should be from the console + assert(FROMHOST_DEV(fh) == 1); + switch (FROMHOST_CMD(fh)) { + case 0: + htif_console_buf = 1 + (uint8_t)FROMHOST_DATA(fh); + break; + case 1: + break; + default: + assert(0); + } +} + +static void __set_tohost(uintptr_t dev, uintptr_t cmd, uintptr_t data) +{ + while (tohost) { + __check_fromhost(); + } + tohost = TOHOST_CMD(dev, cmd, data); +} + +int htif_console_poll_char(rtems_termios_device_context *base) +{ + __check_fromhost(); + int ch = htif_console_buf; + if (ch >= 0) { + htif_console_buf = -1; + __set_tohost(1, 0, 0); + } + + return ch - 1; +} + +void htif_console_write_polled( + rtems_termios_device_context *base, + const char *buf, + size_t len +) +{ + size_t i; + + for (i = 0; i < len; ++i) { + __set_tohost(1, 1, buf[i]); + } +} + +void htif_poweroff(void) +{ + while (1) { + fromhost = 0; + tohost = 1; + } +} + +void htif_console_context_init( + rtems_termios_device_context *base, + int device_tree_node +) +{ + rtems_termios_device_context_initialize(base, "HTIF"); +} + +static bool htif_console_first_open( + struct rtems_termios_tty *tty, + rtems_termios_device_context *base, + struct termios *term, + rtems_libio_open_close_args_t *args +) +{ + return true; +} + +const rtems_termios_device_handler htif_console_handler = { + .first_open = htif_console_first_open, + .write = htif_console_write_polled, + .poll_read = htif_console_poll_char, + .mode = TERMIOS_POLLED +}; diff --git a/bsps/riscv/riscv/include/dev/serial/htif.h b/bsps/riscv/riscv/include/dev/serial/htif.h new file mode 100644 index 0000000000..c200a6f388 --- /dev/null +++ b/bsps/riscv/riscv/include/dev/serial/htif.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2018 embedded brains GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#ifndef DEV_SERIAL_HTIF_H +#define DEV_SERIAL_HTIF_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef struct { + rtems_termios_device_context base; +} htif_console_context; + +void htif_console_context_init( + rtems_termios_device_context *base, + int device_tree_node +); + +void htif_console_write_polled( + rtems_termios_device_context *base, + const char *buf, + size_t len +); + +int htif_console_poll_char(rtems_termios_device_context *base); + +const rtems_termios_device_handler htif_console_handler; + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* DEV_SERIAL_HTIF_H */ diff --git a/c/src/lib/libbsp/riscv/riscv/Makefile.am b/c/src/lib/libbsp/riscv/riscv/Makefile.am index bc5355f585..c8299f3e72 100644 --- a/c/src/lib/libbsp/riscv/riscv/Makefile.am +++ b/c/src/lib/libbsp/riscv/riscv/Makefile.am @@ -49,9 +49,6 @@ librtemsbsp_a_SOURCES +=../../../../../../bsps/riscv/riscv/clock/clockdrv.c # Timer librtemsbsp_a_SOURCES += ../../../../../../bsps/riscv/riscv/btimer/btimer.c -# console -librtemsbsp_a_SOURCES += ../../../../../../bsps/shared/dev/serial/console-polled.c - # IRQ librtemsbsp_a_SOURCES += ../../../../../../bsps/shared/irq/irq-default-handler.c librtemsbsp_a_SOURCES += ../../../../../../bsps/riscv/riscv/irq/irq.c @@ -59,8 +56,10 @@ librtemsbsp_a_SOURCES += ../../../../../../bsps/riscv/riscv/irq/irq.c # Cache librtemsbsp_a_SOURCES += ../../../../../../bsps/shared/cache/nocache.c -# debugio -librtemsbsp_a_SOURCES += ../../../../../../bsps/riscv/riscv/console/console-io.c +# Console +librtemsbsp_a_SOURCES += ../../../../../../bsps/shared/dev/serial/console-termios.c +librtemsbsp_a_SOURCES += ../../../../../../bsps/riscv/riscv/console/console-config.c +librtemsbsp_a_SOURCES += ../../../../../../bsps/riscv/riscv/console/htif.c if HAS_SMP librtemsbsp_a_SOURCES += ../../../../../../bsps/shared/start/bspsmp-dummy.c diff --git a/c/src/lib/libbsp/riscv/riscv/configure.ac b/c/src/lib/libbsp/riscv/riscv/configure.ac index 9d13210cd7..68a7b50871 100644 --- a/c/src/lib/libbsp/riscv/riscv/configure.ac +++ b/c/src/lib/libbsp/riscv/riscv/configure.ac @@ -27,6 +27,9 @@ RTEMS_BSPOPTS_HELP([BSP_FDT_BLOB_READ_ONLY],[place the FDT blob into the read-on RTEMS_BSPOPTS_SET([BSP_FDT_BLOB_COPY_TO_READ_ONLY_LOAD_AREA],[*],[1]) RTEMS_BSPOPTS_HELP([BSP_FDT_BLOB_COPY_TO_READ_ONLY_LOAD_AREA],[copy the FDT blob into the read-only load area via bsp_fdt_copy()]) +RTEMS_BSPOPTS_SET([RISCV_ENABLE_HTIF_SUPPORT],[*],[1]) +RTEMS_BSPOPTS_HELP([RISCV_ENABLE_HTIF_SUPPORT],[enables the HTIF support if defined to a non-zero value, otherwise it is disabled (enabled by default)]) + RTEMS_BSP_CLEANUP_OPTIONS case "${RTEMS_BSP}" in -- cgit v1.2.3