[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Minios-devel] [UNIKRAFT/PLAT_RASPI PATCH 10/13] plats/raspi: Adding serial console support



Adding serial console support.

Signed-off-by: Santiago Pagani <santiagopagani@xxxxxxxxx>
---
 console.c                      |  77 +++++++++++++++++++
 include/raspi/console.h        |  30 ++++++++
 include/raspi/serial_console.h |  32 ++++++++
 serial_console.c               | 130 +++++++++++++++++++++++++++++++++
 4 files changed, 269 insertions(+)
 create mode 100644 console.c
 create mode 100644 include/raspi/console.h
 create mode 100644 include/raspi/serial_console.h
 create mode 100644 serial_console.c

diff --git a/console.c b/console.c
new file mode 100644
index 0000000..8f4b08c
--- /dev/null
+++ b/console.c
@@ -0,0 +1,77 @@
+/* SPDX-License-Identifier: ISC */
+/*
+ * Authors: Dan Williams
+ *          Martin Lucina
+ *          Felipe Huici <felipe.huici@xxxxxxxxx>
+ *          Florian Schmidt <florian.schmidt@xxxxxxxxx>
+ *          Simon Kuenzer <simon.kuenzer@xxxxxxxxx>
+ *          Santiago Pagani <santiagopagani@xxxxxxxxx>
+ *
+ * Copyright (c) 2015-2017 IBM
+ * Copyright (c) 2016-2017 Docker, Inc.
+ * Copyright (c) 2020, NEC Laboratories Europe GmbH, NEC Corporation.
+ *                     All rights reserved.
+ *
+ * Permission to use, copy, modify, and/or distribute this software
+ * for any purpose with or without fee is hereby granted, provided
+ * that the above copyright notice and this permission notice appear
+ * in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
+ * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+ * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <uk/plat/console.h>
+#include <uk/config.h>
+#include <uk/essentials.h>
+#if (CONFIG_RASPI_PRINTF_SERIAL_CONSOLE || CONFIG_RASPI_DEBUG_SERIAL_CONSOLE 
|| CONFIG_RASPI_KERNEL_SERIAL_CONSOLE)
+#include <raspi/serial_console.h>
+#endif
+
+void _libraspiplat_init_console(void)
+{
+#if (CONFIG_RASPI_PRINTF_SERIAL_CONSOLE || CONFIG_RASPI_DEBUG_SERIAL_CONSOLE 
|| CONFIG_RASPI_KERNEL_SERIAL_CONSOLE)
+       _libraspiplat_init_serial_console();
+#endif
+}
+
+int ukplat_coutd(const char *buf __maybe_unused, unsigned int len)
+{
+       for (unsigned int i = 0; i < len; i++) {
+#if (CONFIG_RASPI_PRINTF_SERIAL_CONSOLE || CONFIG_RASPI_DEBUG_SERIAL_CONSOLE)
+               _libraspiplat_serial_putc(buf[i]);
+#endif
+       }
+       return len;
+}
+
+int ukplat_coutk(const char *buf __maybe_unused, unsigned int len)
+{
+       for (unsigned int i = 0; i < len; i++) {
+#if (CONFIG_RASPI_PRINTF_SERIAL_CONSOLE || CONFIG_RASPI_KERNEL_SERIAL_CONSOLE)
+               _libraspiplat_serial_putc(buf[i]);
+#endif
+       }
+       return len;
+}
+
+int ukplat_cink(char *buf __maybe_unused, unsigned int maxlen __maybe_unused)
+{
+       int ret __maybe_unused;
+       unsigned int num = 0;
+
+#if (CONFIG_RASPI_PRINTF_SERIAL_CONSOLE || CONFIG_RASPI_KERNEL_SERIAL_CONSOLE)
+       while (num < maxlen
+              && (ret = _libraspiplat_serial_getc()) >= 0) {
+               *(buf++) = (char) ret;
+               num++;
+       }
+#endif
+       return (int) num;
+}
diff --git a/include/raspi/console.h b/include/raspi/console.h
new file mode 100644
index 0000000..2e1b94f
--- /dev/null
+++ b/include/raspi/console.h
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: ISC */
+/* Copyright (c) 2015, IBM
+ *           (c) 2020, NEC Laboratories Europe GmbH, NEC Corporation.
+ *                     All rights reserved.
+ *
+ * Author(s): Dan Williams <djwillia@xxxxxxxxxx>
+ *            Simon Kuenzer <simon.kuenzer@xxxxxxxxx>
+ *            Santiago Pagani <santiagopagani@xxxxxxxxx>
+ *
+ * Permission to use, copy, modify, and/or distribute this software
+ * for any purpose with or without fee is hereby granted, provided
+ * that the above copyright notice and this permission notice appear
+ * in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
+ * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+ * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef __RASPI_CONSOLE_H__
+#define __RASPI_CONSOLE_H__
+
+void _libraspiplat_init_console(void);
+
+#endif /* __RASPI_CONSOLE_H__ */
diff --git a/include/raspi/serial_console.h b/include/raspi/serial_console.h
new file mode 100644
index 0000000..75e95c7
--- /dev/null
+++ b/include/raspi/serial_console.h
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: ISC */
+/*
+ * Authors: Dafna Hirschfeld <dafna3@xxxxxxxxx>
+ *          Santiago Pagani <santiagopagani@xxxxxxxxx>
+ *
+ * Copyright (c) 2018 Dafna Hirschfeld <dafna3@xxxxxxxxx>
+ * Copyright (c) 2020, NEC Laboratories Europe GmbH, NEC Corporation.
+ *                     All rights reserved.
+ *
+ * Permission to use, copy, modify, and/or distribute this software
+ * for any purpose with or without fee is hereby granted, provided
+ * that the above copyright notice and this permission notice appear
+ * in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+ * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
+ * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+ * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef __RASPI_SERIAL_CONSOLE_H__
+#define __RASPI_SERIAL_CONSOLE_H__
+
+void _libraspiplat_init_serial_console(void);
+void _libraspiplat_serial_putc(char a);
+int  _libraspiplat_serial_getc(void);
+
+#endif /* __RASPI_SERIAL_CONSOLE_H__ */
diff --git a/serial_console.c b/serial_console.c
new file mode 100644
index 0000000..a3d3da1
--- /dev/null
+++ b/serial_console.c
@@ -0,0 +1,130 @@
+/*
+ * Copyright (C) 2018, bzt (bztsrc@github), 
https://github.com/bztsrc/raspi3-tutorial
+ * Copyright (C) 2020, Santiago Pagani <santiagopagani@xxxxxxxxx>
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#include <raspi/sysregs.h>
+#include <raspi/mbox.h>
+
+/* PL011 UART registers */
+#define UART0_DR        ((volatile unsigned int*)(MMIO_BASE+0x00201000))
+#define UART0_FR        ((volatile unsigned int*)(MMIO_BASE+0x00201018))
+#define UART0_IBRD      ((volatile unsigned int*)(MMIO_BASE+0x00201024))
+#define UART0_FBRD      ((volatile unsigned int*)(MMIO_BASE+0x00201028))
+#define UART0_LCRH      ((volatile unsigned int*)(MMIO_BASE+0x0020102C))
+#define UART0_CR        ((volatile unsigned int*)(MMIO_BASE+0x00201030))
+#define UART0_IMSC      ((volatile unsigned int*)(MMIO_BASE+0x00201038))
+#define UART0_ICR       ((volatile unsigned int*)(MMIO_BASE+0x00201044))
+
+static char prev_sent_char = '\0';
+
+static void wait_cycles(unsigned int n)
+{
+    if (n) {
+               while (n--) {
+                       asm volatile("nop");
+               }
+       }
+}
+
+static unsigned int serial_tx_buffer_full(void)
+{
+       return *UART0_FR&0x20;
+}
+
+static unsigned int serial_rx_buffer_empty(void)
+{
+       return *UART0_FR&0x10;
+}
+
+/**
+ * Set baud rate and characteristics (115200 8N1) and map to GPIO
+ */
+void _libraspiplat_init_serial_console()
+{
+    register unsigned int r;
+
+    /* initialize UART */
+    *UART0_CR = 0;         // turn off UART0
+
+    /* set up clock for consistent divisor values */
+    mbox[0] = 9*4;
+    mbox[1] = MBOX_REQUEST;
+    mbox[2] = MBOX_TAG_SETCLKRATE; // set clock rate
+    mbox[3] = 12;
+    mbox[4] = 8;
+    mbox[5] = 2;           // UART clock
+    mbox[6] = 4000000;     // 4Mhz
+    mbox[7] = 0;           // clear turbo
+    mbox[8] = MBOX_TAG_LAST;
+    mbox_call(MBOX_CH_PROP);
+
+    /* map UART0 to GPIO pins */
+    r=*GPFSEL1;
+    r&=~((7<<12)|(7<<15)); // gpio14, gpio15
+    r|=(4<<12)|(4<<15);    // alt0
+    *GPFSEL1 = r;
+    *GPPUD = 0;            // enable pins 14 and 15
+    wait_cycles(150);
+    *GPPUDCLK0 = (1<<14)|(1<<15);
+    wait_cycles(150);
+    *GPPUDCLK0 = 0;        // flush GPIO setup
+
+    *UART0_ICR = 0x7FF;    // clear interrupts
+    *UART0_IBRD = 2;       // 115200 baud
+    *UART0_FBRD = 0xB;
+    *UART0_LCRH = 0b11<<5; // 8n1
+    *UART0_CR = 0x301;     // enable Tx, Rx, FIFO
+}
+
+/**
+ * Send a character
+ */
+void _libraspiplat_serial_putc(char c)
+{
+       if ((c == '\n') && (prev_sent_char != '\r'))
+               _libraspiplat_serial_putc('\r');
+
+    // Wait until we can send
+    do{
+               asm volatile("nop");
+       } while (serial_tx_buffer_full());
+
+    // Write the character to the buffer
+    *UART0_DR = c;
+       prev_sent_char = c;
+}
+
+/**
+ * Receive a character
+ */
+int  _libraspiplat_serial_getc(void)
+{
+       if (serial_rx_buffer_empty())
+               return -1;
+
+    char r;
+    r = (char)(*UART0_DR);
+    return (int)r;
+}
-- 
2.17.1


_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.