Skip to contentSkip to conversation

serial: implement riscv SBI console support

PATCH In review Marcelo Politzer 2 participants 3 messages Sep 24, 2021 – Sep 27, 2021 1 message not in the archive RISC-V

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/1 serial: implement riscv SBI console support

Implement a console over legacy SBI (version 0.1.0). There is a tiny ringbuffer to simplify checking for presence and reading characters as separate steps.

---
 arch/riscv/cpu/core.c       | 13 ++++++++
 arch/riscv/lib/sbi.c        | 11 +++++++
 drivers/serial/Kconfig      |  8 +++++
 drivers/serial/Makefile     |  1 +
 drivers/serial/serial_sbi.c | 60 +++++++++++++++++++++++++++++++++++++
 5 files changed, 93 insertions(+)
 create mode 100644 drivers/serial/serial_sbi.c
arch/riscv/cpu/core.c +13 -0
index 80730c05b..9226037f3 100644
@@ -24,6 +24,7 @@
 #include <magicvar.h>
 #include <asm/system.h>
 #include <io.h>
+#include <asm/sbi.h>
 
 static int riscv_request_stack(void)
 {
@@ -33,6 +34,7 @@ static int riscv_request_stack(void)
 coredevice_initcall(riscv_request_stack);
 
 static struct device_d timer_dev;
+static struct device_d serial_sbi_dev;
 
 static s64 hartid;
 
@@ -75,6 +77,17 @@ static int riscv_probe(struct device_d *parent)
                        return ret;
        }
 
+       if (IS_ENABLED(CONFIG_SERIAL_SBI) && !serial_sbi_dev.parent) {
+               serial_sbi_dev.id = DEVICE_ID_SINGLE;
+               serial_sbi_dev.device_node = 0;
+               serial_sbi_dev.parent = parent;
+               dev_set_name(&serial_sbi_dev, "riscv-serial-sbi");
+
+               ret = platform_device_register(&serial_sbi_dev);
+               if (ret)
+                       return ret;
+       }
+
        hartid = riscv_hartid();
        if (hartid >= 0)
                globalvar_add_simple_uint64("hartid", &hartid, "%llu");
arch/riscv/lib/sbi.c +10 -0
index 45a04fb82..209069c98 100644
@@ -64,3 +64,14 @@ static int sbi_init(void)
 
 }
 core_initcall(sbi_init);
+
+
+void sbi_console_putchar(int ch)
+{
+       sbi_ecall(SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, ch, 0, 0, 0, 0, 0);
+}
+
+int sbi_console_getchar(void)
+{
+       return sbi_ecall(SBI_EXT_0_1_CONSOLE_GETCHAR, 0, 0, 0, 0, 0,

0, 0).error;
+}

drivers/serial/Kconfig +8 -0
index b9750d177..5e30ea388 100644
@@ -173,4 +173,12 @@ config SERIAL_SIFIVE
          contains a SiFive UART IP block.  This type of UART is present on
          SiFive FU540 SoCs, among others.
 
+config SERIAL_SBI
+       tristate "RISCV Serial support over SBI's HTIF"
+       depends on OFDEVICE
+       depends on RISCV_SBI
+       help
+         Select this option if you are building barebox for a RISCV platform
+         that implements a serial over SBI.
+
 endmenu
drivers/serial/Makefile +0 -0
index 5120b1737..b1de436ed 100644
@@ -24,3 +24,4 @@ obj-$(CONFIG_DRIVER_SERIAL_DIGIC)             +=

serial_digic.o

 obj-$(CONFIG_DRIVER_SERIAL_LPUART)             += serial_lpuart.o
 obj-$(CONFIG_VIRTIO_CONSOLE)                   += virtio_console.o
 obj-$(CONFIG_SERIAL_SIFIVE)                    += serial_sifive.o
+obj-$(CONFIG_SERIAL_SBI)                       += serial_sbi.o
drivers/serial/serial_sbi.c new file +60 -0

60 lines not on this page · raw message

--
2.32.0

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#2 lore raw

On Fri, 24 Sep 2021 10:13:38 -0300
Marcelo Politzer <[email protected]> wrote:

Hi, Marcelo!

Quoted 18 lines

Implement a console over legacy SBI (version 0.1.0). There is a tiny ringbuffer to simplify checking for presence and reading characters as separate steps.

---
 arch/riscv/cpu/core.c       | 13 ++++++++
 arch/riscv/lib/sbi.c        | 11 +++++++
 drivers/serial/Kconfig      |  8 +++++
 drivers/serial/Makefile     |  1 +
 drivers/serial/serial_sbi.c | 60 +++++++++++++++++++++++++++++++++++++
 5 files changed, 93 insertions(+)
 create mode 100644 drivers/serial/serial_sbi.c
arch/riscv/cpu/core.c +0 -0
index 80730c05b..9226037f3 100644

arch/riscv/cpu/core.c line 27

Quoted 4 lines
arch/riscv/cpu/core.c +1 -0
@@ -24,6 +24,7 @@
 #include <magicvar.h>
 #include <asm/system.h>
 #include <io.h>
+#include <asm/sbi.h>

There is no need to include <asm/sbi.h> here.

Quoted 16 lines
arch/riscv/cpu/core.c +2 -0
 static int riscv_request_stack(void)
 {
@@ -33,6 +34,7 @@ static int riscv_request_stack(void)
 coredevice_initcall(riscv_request_stack);
 
 static struct device_d timer_dev;
+static struct device_d serial_sbi_dev;
 
 static s64 hartid;
 
@@ -75,6 +77,17 @@ static int riscv_probe(struct device_d *parent)
                        return ret;
        }
 
+       if (IS_ENABLED(CONFIG_SERIAL_SBI) && !serial_sbi_dev.parent) {

arch/riscv/cpu/core.c static int riscv_probe(struct device_d *parent) · line 90

Quoted 10 lines
arch/riscv/cpu/core.c +10 -0
+               serial_sbi_dev.id = DEVICE_ID_SINGLE;
+               serial_sbi_dev.device_node = 0;
+               serial_sbi_dev.parent = parent;
+               dev_set_name(&serial_sbi_dev, "riscv-serial-sbi");
+
+               ret = platform_device_register(&serial_sbi_dev);
+               if (ret)
+                       return ret;
+       }
+

Please, use tabs for indentation!

arch/riscv/lib/sbi.c static int sbi_init(void) · line 68

Quoted 13 lines
arch/riscv/cpu/core.c +0 -0
        hartid = riscv_hartid();
        if (hartid >= 0)
                globalvar_add_simple_uint64("hartid", &hartid, "%llu");
arch/riscv/lib/sbi.c +2 -0
index 45a04fb82..209069c98 100644
@@ -64,3 +64,14 @@ static int sbi_init(void)
 
 }
 core_initcall(sbi_init);
+
+

No extra emptyline here.

Quoted 43 lines
arch/riscv/lib/sbi.c +8 -0
+void sbi_console_putchar(int ch)
+{
+       sbi_ecall(SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, ch, 0, 0, 0, 0, 0);
+}
+
+int sbi_console_getchar(void)
+{
+       return sbi_ecall(SBI_EXT_0_1_CONSOLE_GETCHAR, 0, 0, 0, 0, 0,

0, 0).error;

arch/riscv/lib/sbi.c +1 -0
+}
drivers/serial/Kconfig +8 -0
index b9750d177..5e30ea388 100644
@@ -173,4 +173,12 @@ config SERIAL_SIFIVE
          contains a SiFive UART IP block.  This type of UART is present on
          SiFive FU540 SoCs, among others.
 
+config SERIAL_SBI
+       tristate "RISCV Serial support over SBI's HTIF"
+       depends on OFDEVICE
+       depends on RISCV_SBI
+       help
+         Select this option if you are building barebox for a RISCV platform
+         that implements a serial over SBI.
+
 endmenu
drivers/serial/Makefile +0 -0
index 5120b1737..b1de436ed 100644
@@ -24,3 +24,4 @@ obj-$(CONFIG_DRIVER_SERIAL_DIGIC)             +=

serial_digic.o

 obj-$(CONFIG_DRIVER_SERIAL_LPUART)             += serial_lpuart.o
 obj-$(CONFIG_VIRTIO_CONSOLE)                   += virtio_console.o
 obj-$(CONFIG_SERIAL_SIFIVE)                    += serial_sifive.o
drivers/serial/Makefile +1 -0
+obj-$(CONFIG_SERIAL_SBI)                       += serial_sbi.o
drivers/serial/serial_sbi.c new file +0 -0
new file mode 100644
index 000000000..7217ce9cc

drivers/serial/serial_sbi.c line 10

Quoted 10 lines
drivers/serial/serial_sbi.c new file +10 -0
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2021 Marcelo Politzer <[email protected]>
+ */
+
+#include <common.h>
+#include <driver.h>
+#include <malloc.h>
+#include <io.h>
+#include <of.h>

Please drop <io.h> and <of.h> inclusion here. These header files are unused.

Quoted 57 lines
drivers/serial/serial_sbi.c new file +50 -1
+#include <asm/sbi.h>
+
+struct sbi_serial_priv {
+       struct console_device cdev;
+       uint8_t b[2], head, tail;
+};
+
+static int sbi_serial_getc(struct console_device *cdev)
+{
+       struct sbi_serial_priv *priv = cdev->dev->priv;
+       if (priv->head == priv->tail)
+               return -1;
+       return priv->b[priv->head++ & 0x1];
+}
+
+static void sbi_serial_putc(struct console_device *cdev, const char ch)
+{
+       sbi_console_putchar(ch);
+}
+
+static int sbi_serial_tstc(struct console_device *cdev)
+{
+       struct sbi_serial_priv *priv = cdev->dev->priv;
+       int c = sbi_console_getchar();
+
+       if (c != -1)
+               priv->b[priv->tail++ & 0x1] = c;
+       return priv->head != priv->tail;
+}
+
+static int sbi_serial_probe(struct device_d *dev)
+{
+       struct sbi_serial_priv *priv;
+
+       priv = dev->priv = xzalloc(sizeof(*priv));
+       priv->cdev.dev    = dev;
+       priv->cdev.putc   = sbi_serial_putc;
+       priv->cdev.getc   = sbi_serial_getc;
+       priv->cdev.tstc   = sbi_serial_tstc;
+       priv->cdev.flush  = 0;
+       priv->cdev.setbrg = 0;
+
+       return console_register(&priv->cdev);
+}
+
+static struct driver_d serial_sbi_driver = {
+       .name   = "riscv-serial-sbi",
+       .probe  = sbi_serial_probe,
+};
+postcore_platform_driver(serial_sbi_driver);
--

2.32.0

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

Signature · 7 lines
Best regards,
  Antony Pavlov

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw

Thank you for taking the time to review,

I've sent a v2 with your suggestions incorporated.

best,
Marcelo

Quoted 187 lines · Antony Pavlov

On Fri, 24 Sep 2021 10:13:38 -0300
Marcelo Politzer <[email protected]> wrote:

Hi, Marcelo!

Quoted 18 lines

Implement a console over legacy SBI (version 0.1.0). There is a tiny ringbuffer to simplify checking for presence and reading characters as separate steps.

---
 arch/riscv/cpu/core.c       | 13 ++++++++
 arch/riscv/lib/sbi.c        | 11 +++++++
 drivers/serial/Kconfig      |  8 +++++
 drivers/serial/Makefile     |  1 +
 drivers/serial/serial_sbi.c | 60 +++++++++++++++++++++++++++++++++++++
 5 files changed, 93 insertions(+)
 create mode 100644 drivers/serial/serial_sbi.c
arch/riscv/cpu/core.c +0 -0
index 80730c05b..9226037f3 100644

arch/riscv/cpu/core.c line 27

Quoted 4 lines
arch/riscv/cpu/core.c +1 -0
@@ -24,6 +24,7 @@
 #include <magicvar.h>
 #include <asm/system.h>
 #include <io.h>
+#include <asm/sbi.h>

There is no need to include <asm/sbi.h> here.

Quoted 16 lines
arch/riscv/cpu/core.c +2 -0
 static int riscv_request_stack(void)
 {
@@ -33,6 +34,7 @@ static int riscv_request_stack(void)
 coredevice_initcall(riscv_request_stack);
 
 static struct device_d timer_dev;
+static struct device_d serial_sbi_dev;
 
 static s64 hartid;
 
@@ -75,6 +77,17 @@ static int riscv_probe(struct device_d *parent)
                        return ret;
        }
 
+       if (IS_ENABLED(CONFIG_SERIAL_SBI) && !serial_sbi_dev.parent) {

arch/riscv/cpu/core.c static int riscv_probe(struct device_d *parent) · line 90

Quoted 10 lines
arch/riscv/cpu/core.c +10 -0
+               serial_sbi_dev.id = DEVICE_ID_SINGLE;
+               serial_sbi_dev.device_node = 0;
+               serial_sbi_dev.parent = parent;
+               dev_set_name(&serial_sbi_dev, "riscv-serial-sbi");
+
+               ret = platform_device_register(&serial_sbi_dev);
+               if (ret)
+                       return ret;
+       }
+

Please, use tabs for indentation!

arch/riscv/lib/sbi.c static int sbi_init(void) · line 68

Quoted 13 lines
arch/riscv/cpu/core.c +0 -0
        hartid = riscv_hartid();
        if (hartid >= 0)
                globalvar_add_simple_uint64("hartid", &hartid, "%llu");
arch/riscv/lib/sbi.c +2 -0
index 45a04fb82..209069c98 100644
@@ -64,3 +64,14 @@ static int sbi_init(void)
 
 }
 core_initcall(sbi_init);
+
+

No extra emptyline here.

Quoted 43 lines
arch/riscv/lib/sbi.c +8 -0
+void sbi_console_putchar(int ch)
+{
+       sbi_ecall(SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, ch, 0, 0, 0, 0, 0);
+}
+
+int sbi_console_getchar(void)
+{
+       return sbi_ecall(SBI_EXT_0_1_CONSOLE_GETCHAR, 0, 0, 0, 0, 0,

0, 0).error;

arch/riscv/lib/sbi.c +1 -0
+}
drivers/serial/Kconfig +8 -0
index b9750d177..5e30ea388 100644
@@ -173,4 +173,12 @@ config SERIAL_SIFIVE
          contains a SiFive UART IP block.  This type of UART is present on
          SiFive FU540 SoCs, among others.
 
+config SERIAL_SBI
+       tristate "RISCV Serial support over SBI's HTIF"
+       depends on OFDEVICE
+       depends on RISCV_SBI
+       help
+         Select this option if you are building barebox for a RISCV platform
+         that implements a serial over SBI.
+
 endmenu
drivers/serial/Makefile +0 -0
index 5120b1737..b1de436ed 100644
@@ -24,3 +24,4 @@ obj-$(CONFIG_DRIVER_SERIAL_DIGIC)             +=

serial_digic.o

 obj-$(CONFIG_DRIVER_SERIAL_LPUART)             += serial_lpuart.o
 obj-$(CONFIG_VIRTIO_CONSOLE)                   += virtio_console.o
 obj-$(CONFIG_SERIAL_SIFIVE)                    += serial_sifive.o
drivers/serial/Makefile +1 -0
+obj-$(CONFIG_SERIAL_SBI)                       += serial_sbi.o
drivers/serial/serial_sbi.c new file +0 -0
new file mode 100644
index 000000000..7217ce9cc

drivers/serial/serial_sbi.c line 10

Quoted 10 lines
drivers/serial/serial_sbi.c new file +10 -0
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2021 Marcelo Politzer <[email protected]>
+ */
+
+#include <common.h>
+#include <driver.h>
+#include <malloc.h>
+#include <io.h>
+#include <of.h>

Please drop <io.h> and <of.h> inclusion here. These header files are unused.

Quoted 53 lines
drivers/serial/serial_sbi.c new file +50 -1
+#include <asm/sbi.h>
+
+struct sbi_serial_priv {
+       struct console_device cdev;
+       uint8_t b[2], head, tail;
+};
+
+static int sbi_serial_getc(struct console_device *cdev)
+{
+       struct sbi_serial_priv *priv = cdev->dev->priv;
+       if (priv->head == priv->tail)
+               return -1;
+       return priv->b[priv->head++ & 0x1];
+}
+
+static void sbi_serial_putc(struct console_device *cdev, const char ch)
+{
+       sbi_console_putchar(ch);
+}
+
+static int sbi_serial_tstc(struct console_device *cdev)
+{
+       struct sbi_serial_priv *priv = cdev->dev->priv;
+       int c = sbi_console_getchar();
+
+       if (c != -1)
+               priv->b[priv->tail++ & 0x1] = c;
+       return priv->head != priv->tail;
+}
+
+static int sbi_serial_probe(struct device_d *dev)
+{
+       struct sbi_serial_priv *priv;
+
+       priv = dev->priv = xzalloc(sizeof(*priv));
+       priv->cdev.dev    = dev;
+       priv->cdev.putc   = sbi_serial_putc;
+       priv->cdev.getc   = sbi_serial_getc;
+       priv->cdev.tstc   = sbi_serial_tstc;
+       priv->cdev.flush  = 0;
+       priv->cdev.setbrg = 0;
+
+       return console_register(&priv->cdev);
+}
+
+static struct driver_d serial_sbi_driver = {
+       .name   = "riscv-serial-sbi",
+       .probe  = sbi_serial_probe,
+};
+postcore_platform_driver(serial_sbi_driver);
--

2.32.0

Quoted 9 lines
Quoted 4 lines

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

--
Best regards,
Antony Pavlov

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox