[rtems commit] bsp/riscv: Add device tree support for console

Sebastian Huber sebh at rtems.org
Fri Jun 29 09:57:33 UTC 2018


Module:    rtems
Branch:    master
Commit:    1232cd46905cc5a59dae2195c39eb5dd5742a754
Changeset: http://git.rtems.org/rtems/commit/?id=1232cd46905cc5a59dae2195c39eb5dd5742a754

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Mon Jun 25 08:44:16 2018 +0200

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 => htif.c} |  90 +++++----------
 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 +
 5 files changed, 222 insertions(+), 65 deletions(-)

diff --git a/bsps/riscv/riscv/console/console-config.c b/bsps/riscv/riscv/console/console-config.c
new file mode 100644
index 0000000..06b5d69
--- /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
+ *  <info at embedded-brains.de>
+ *
+ * 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 <rtems/bspIo.h>
+#include <rtems/console.h>
+#include <rtems/sysinit.h>
+#include <rtems/termiostypes.h>
+
+#include <bsp.h>
+#include <bsp/fdt.h>
+#include <bsp/irq.h>
+
+#include <dev/serial/htif.h>
+
+#include <libfdt.h>
+
+#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/htif.c
similarity index 72%
rename from bsps/riscv/riscv/console/console-io.c
rename to bsps/riscv/riscv/console/htif.c
index 6fb5705..6b9cded 100644
--- a/bsps/riscv/riscv/console/console-io.c
+++ b/bsps/riscv/riscv/console/htif.c
@@ -27,12 +27,9 @@
  * SUCH DAMAGE.
  */
 
-#include <bsp.h>
-#include <bsp/console-polled.h>
-#include <rtems/libio.h>
-#include <stdlib.h>
+#include <dev/serial/htif.h>
+
 #include <assert.h>
-#include <stdio.h>
 
 /* Most of the code below is copied from riscv-pk project */
 # define TOHOST_CMD(dev, cmd, payload) \
@@ -76,7 +73,7 @@ static void __set_tohost(uintptr_t dev, uintptr_t cmd, uintptr_t data)
   tohost = TOHOST_CMD(dev, cmd, data);
 }
 
-static int htif_console_getchar(void)
+int htif_console_poll_char(rtems_termios_device_context *base)
 {
   __check_fromhost();
   int ch = htif_console_buf;
@@ -88,9 +85,17 @@ static int htif_console_getchar(void)
   return ch - 1;
 }
 
-static void htif_console_putchar(uint8_t ch)
+void htif_console_write_polled(
+  rtems_termios_device_context *base,
+  const char *buf,
+  size_t len
+)
 {
-  __set_tohost(1, 1, ch);
+  size_t i;
+
+  for (i = 0; i < len; ++i) {
+    __set_tohost(1, 1, buf[i]);
+  }
 }
 
 void htif_poweroff(void)
@@ -101,62 +106,27 @@ void htif_poweroff(void)
   }
 }
 
-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
+void htif_console_context_init(
+  rtems_termios_device_context *base,
+  int device_tree_node
 )
 {
-  outbyte_console( ch );
+  rtems_termios_device_context_initialize(base, "HTIF");
 }
 
-/*
- *  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 <rtems/bspIo.h>
-
-static void RISCV_output_char(char c)
+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
+)
 {
-  console_outbyte_polled( 0, c );
+  return true;
 }
 
-BSP_output_char_function_type BSP_output_char = RISCV_output_char;
-BSP_polling_getchar_function_type BSP_poll_char =
-  (void *)console_inbyte_nonblocking;
+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 0000000..c200a6f
--- /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 <rtems/termiostypes.h>
+
+#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 bc5355f..c8299f3 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 9d13210..68a7b50 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



More information about the vc mailing list