This allows us also to fold uart-tegra and uart-8250, at least for
inmates.

Signed-off-by: Ralf Ramsauer <[email protected]>
---
 hypervisor/arch/arm-common/uart-8250.c  |  1 -
 hypervisor/arch/arm-common/uart-pl011.c |  1 -
 inmates/lib/arm-common/include/uart.h   | 24 ++++++++++++
 inmates/lib/arm/Makefile                |  2 +-
 inmates/lib/arm/Makefile.lib            |  1 +
 inmates/lib/arm/printk.c                | 12 +++---
 inmates/lib/arm/uart-8250.c             | 51 ++++++++++++++++++++-----
 inmates/lib/arm/uart-pl011.c            | 66 +++++++++++++++++++++++++++++----
 inmates/lib/arm/uart-tegra.c            | 23 ------------
 inmates/lib/arm64/Makefile.lib          |  1 +
 10 files changed, 135 insertions(+), 47 deletions(-)
 create mode 100644 inmates/lib/arm-common/include/uart.h
 delete mode 100644 inmates/lib/arm/uart-tegra.c

diff --git a/hypervisor/arch/arm-common/uart-8250.c 
b/hypervisor/arch/arm-common/uart-8250.c
index 6380110673..ef5bc9ecdb 100644
--- a/hypervisor/arch/arm-common/uart-8250.c
+++ b/hypervisor/arch/arm-common/uart-8250.c
@@ -14,7 +14,6 @@
 
 #include <asm/uart-8250.h>
 
-/* All the helpers are in the header, to make them re-usable by the inmates */
 void uart_chip_init(struct uart_chip *chip)
 {
        chip->wait = uart_wait;
diff --git a/hypervisor/arch/arm-common/uart-pl011.c 
b/hypervisor/arch/arm-common/uart-pl011.c
index d3e9a2f5be..6824ff79f6 100644
--- a/hypervisor/arch/arm-common/uart-pl011.c
+++ b/hypervisor/arch/arm-common/uart-pl011.c
@@ -12,7 +12,6 @@
 
 #include <asm/uart-pl011.h>
 
-/* All the helpers are in the header, to make them re-usable by the inmates */
 void uart_chip_init(struct uart_chip *chip)
 {
        chip->wait = uart_wait;
diff --git a/inmates/lib/arm-common/include/uart.h 
b/inmates/lib/arm-common/include/uart.h
new file mode 100644
index 0000000000..8bd9c77529
--- /dev/null
+++ b/inmates/lib/arm-common/include/uart.h
@@ -0,0 +1,24 @@
+/*
+ * Jailhouse, a Linux-based partitioning hypervisor
+ *
+ * Copyright (c) OTH Regensburg, 2016
+ *
+ * Authors:
+ *  Ralf Ramsauer <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ */
+
+struct uart_chip {
+       void *base;
+
+       void *clock_reg;
+       int gate_nr;
+
+       unsigned int divider;
+
+       void (*init)(struct uart_chip*);
+       void (*wait)(struct uart_chip*);
+       void (*write)(struct uart_chip*, char c);
+};
diff --git a/inmates/lib/arm/Makefile b/inmates/lib/arm/Makefile
index 8fce89d211..732948b805 100644
--- a/inmates/lib/arm/Makefile
+++ b/inmates/lib/arm/Makefile
@@ -22,4 +22,4 @@ lib-$(CONFIG_ARM_GIC_V2)      += gic-v2.o
 lib-$(CONFIG_ARM_GIC_V3)       += gic-v3.o
 lib-$(CONFIG_SERIAL_AMBA_PL011)        += uart-pl011.o
 lib-$(CONFIG_SERIAL_8250)      += uart-8250.o
-lib-$(CONFIG_SERIAL_TEGRA)     += uart-tegra.o
+lib-$(CONFIG_SERIAL_TEGRA)     += uart-8250.o
diff --git a/inmates/lib/arm/Makefile.lib b/inmates/lib/arm/Makefile.lib
index 9510ff88cd..ebbad6aa83 100644
--- a/inmates/lib/arm/Makefile.lib
+++ b/inmates/lib/arm/Makefile.lib
@@ -17,6 +17,7 @@
 KBUILD_AFLAGS := $(filter-out -include asm/unified.h,$(KBUILD_AFLAGS))
 
 KBUILD_CFLAGS                          += -I$(INMATES_LIB)/include
+KBUILD_CFLAGS                          += 
-I$(INMATES_LIB)/../arm-common/include
 KBUILD_AFLAGS                          += -I$(INMATES_LIB)/include
 
 define DECLARE_TARGETS =
diff --git a/inmates/lib/arm/printk.c b/inmates/lib/arm/printk.c
index 73de356995..11651f20bf 100644
--- a/inmates/lib/arm/printk.c
+++ b/inmates/lib/arm/printk.c
@@ -11,10 +11,11 @@
  */
 
 #include <inmate.h>
-#include <asm/uart.h>
 #include <stdarg.h>
+#include <uart.h>
+#include <mach/uart.h>
 
-static struct uart_chip chip;
+extern struct uart_chip uart_ops;
 
 static void console_write(const char *msg)
 {
@@ -28,8 +29,8 @@ static void console_write(const char *msg)
                if (!c)
                        break;
 
-               chip.wait(&chip);
-               chip.write(&chip, c);
+               uart_ops.wait(&uart_ops);
+               uart_ops.write(&uart_ops, c);
        }
 }
 
@@ -41,7 +42,8 @@ void printk(const char *fmt, ...)
        va_list ap;
 
        if (!inited) {
-               uart_chip_init(&chip);
+               uart_ops.base = UART_BASE;
+               uart_ops.init(&uart_ops);
                inited = true;
        }
 
diff --git a/inmates/lib/arm/uart-8250.c b/inmates/lib/arm/uart-8250.c
index 29b9c22163..ad9e268292 100644
--- a/inmates/lib/arm/uart-8250.c
+++ b/inmates/lib/arm/uart-8250.c
@@ -11,15 +11,48 @@
  * This work is licensed under the terms of the GNU GPL, version 2.  See
  * the COPYING file in the top-level directory.
  */
-#include <asm/uart-8250.h>
-#include <mach/uart.h>
 
-void uart_chip_init(struct uart_chip *chip)
+#include <jailhouse/mmio.h>
+#include <jailhouse/processor.h>
+#include <uart.h>
+
+#define UART_TX                        0x0
+#define UART_DLL               0x0
+#define UART_DLM               0x4
+#define UART_LCR               0xc
+#define  UART_LCR_8N1          0x03
+#define  UART_LCR_DLAB         0x80
+#define UART_LSR               0x14
+#define  UART_LSR_THRE         0x20
+
+static void uart_init(struct uart_chip *chip)
+{
+       if (chip->clock_reg)
+               mmio_write32(chip->clock_reg,
+                            mmio_read32(chip->clock_reg) |
+                            (1 << chip->gate_nr));
+
+       if (chip->divider) {
+               mmio_write32(chip->base + UART_LCR, UART_LCR_DLAB);
+               mmio_write32(chip->base + UART_DLL, chip->divider);
+               mmio_write32(chip->base + UART_DLM, 0);
+               mmio_write32(chip->base + UART_LCR, UART_LCR_8N1);
+       }
+}
+
+static void uart_wait(struct uart_chip *chip)
 {
-       chip->virt_base = UART_BASE;
-       chip->wait = uart_wait;
-       chip->write = uart_write;
-       chip->clock_reg = UART_CLOCK_REG;
-       chip->gate_nr = UART_GATE_NR;
-       uart_init(chip);
+       while (!(mmio_read32(chip->base + UART_LSR) & UART_LSR_THRE))
+               cpu_relax();
 }
+
+static void uart_write(struct uart_chip *chip, char c)
+{
+       mmio_write32(chip->base + UART_TX, c);
+}
+
+struct uart_chip uart_ops = {
+       .init = uart_init,
+       .wait = uart_wait,
+       .write = uart_write,
+};
diff --git a/inmates/lib/arm/uart-pl011.c b/inmates/lib/arm/uart-pl011.c
index 48b1dee610..8f661eb4e2 100644
--- a/inmates/lib/arm/uart-pl011.c
+++ b/inmates/lib/arm/uart-pl011.c
@@ -9,13 +9,65 @@
  * This work is licensed under the terms of the GNU GPL, version 2.  See
  * the COPYING file in the top-level directory.
  */
-#include <asm/uart-pl011.h>
-#include <mach/uart.h>
 
-void uart_chip_init(struct uart_chip *chip)
+#include <uart.h>
+#include <jailhouse/mmio.h>
+#include <asm/processor.h>
+
+#define UART_CLK       24000000
+
+#define UARTDR         0x00
+#define UARTFR         0x18
+#define UARTIBRD       0x24
+#define UARTLCR_H      0x2c
+#define UARTCR         0x30
+
+#define UARTFR_TXFF    (1 << 5)
+#define UARTFR_BUSY    (1 << 3)
+
+#define UARTCR_Out2    (1 << 13)
+#define UARTCR_Out1    (1 << 12)
+#define UARTCR_RXE     (1 << 9)
+#define UARTCR_TXE     (1 << 8)
+#define UARTCR_EN      (1 << 0)
+
+#define UARTLCR_H_WLEN (3 << 5)
+
+static void uart_init(struct uart_chip *chip)
+{
+#ifdef CONFIG_MACH_VEXPRESS
+       /* 115200 8N1 */
+       /* FIXME: Can be improved with an implementation of __aeabi_uidiv */
+       u32 bauddiv = UART_CLK / (16 * 115200);
+
+       mmio_write16(chip->base + UARTCR, 0);
+       while (mmio_read8(chip->base + UARTFR) & UARTFR_BUSY)
+               cpu_relax();
+
+       mmio_write8(chip->base + UARTLCR_H, UARTLCR_H_WLEN);
+       mmio_write16(chip->base + UARTIBRD, bauddiv);
+       mmio_write16(chip->base + UARTCR, (UARTCR_EN | UARTCR_TXE | UARTCR_RXE |
+                                          UARTCR_Out1 | UARTCR_Out2));
+#endif
+}
+
+static void uart_wait(struct uart_chip *chip)
+{
+       u32 flags;
+
+       do {
+               flags = mmio_read32(chip->base + UARTFR);
+               cpu_relax();
+       } while (flags & (UARTFR_TXFF | UARTFR_BUSY)); /* FIFO full or busy */
+}
+
+static void uart_write(struct uart_chip *chip, char c)
 {
-       chip->virt_base = UART_BASE;
-       chip->wait = uart_wait;
-       chip->write = uart_write;
-       uart_init(chip);
+       mmio_write32(chip->base + UARTDR, c);
 }
+
+struct uart_chip uart_ops = {
+       .init = uart_init,
+       .wait = uart_wait,
+       .write = uart_write,
+};
diff --git a/inmates/lib/arm/uart-tegra.c b/inmates/lib/arm/uart-tegra.c
deleted file mode 100644
index 342667c8a5..0000000000
--- a/inmates/lib/arm/uart-tegra.c
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * Jailhouse, a Linux-based partitioning hypervisor
- *
- * Copyright (c) ARM Limited, 2014
- * Copyright (c) Siemens AG, 2014, 2015
- *
- * Authors:
- *  Jean-Philippe Brucker <[email protected]>
- *  Jan Kiszka <[email protected]>
- *
- * This work is licensed under the terms of the GNU GPL, version 2.  See
- * the COPYING file in the top-level directory.
- */
-#include <asm/uart-tegra.h>
-#include <mach/uart.h>
-
-void uart_chip_init(struct uart_chip *chip)
-{
-       chip->virt_base = UART_BASE;
-       chip->wait = uart_wait;
-       chip->write = uart_write;
-       uart_init(chip);
-}
diff --git a/inmates/lib/arm64/Makefile.lib b/inmates/lib/arm64/Makefile.lib
index f8519d8fa3..45cdfa225f 100644
--- a/inmates/lib/arm64/Makefile.lib
+++ b/inmates/lib/arm64/Makefile.lib
@@ -15,6 +15,7 @@
 -include $(obj)/../../../hypervisor/include/generated/config.mk
 
 KBUILD_CFLAGS                          += -I$(INMATES_LIB)/include
+KBUILD_CFLAGS                          += 
-I$(INMATES_LIB)/../arm-common/include
 KBUILD_AFLAGS                          += -I$(INMATES_LIB)/include
 
 define DECLARE_TARGETS =
-- 
2.11.0.rc2

-- 
You received this message because you are subscribed to the Google Groups 
"Jailhouse" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to