drivers: serial: add LiteUART driver

Add LiteX UART driver with bindings for this device.

Signed-off-by: Filip Kokosinski <fkokosinski@internships.antmicro.com>
Signed-off-by: Mateusz Holenko <mholenko@antmicro.com>
diff --git a/CODEOWNERS b/CODEOWNERS
index 62e06fa..4689dc7 100644
--- a/CODEOWNERS
+++ b/CODEOWNERS
@@ -137,6 +137,8 @@
 /drivers/sensor/lsm*/                     @avisconti
 /drivers/serial/uart_altera_jtag_hal.c    @wentongwu
 /drivers/serial/*ns16550*                 @gnuless
+/drivers/serial/Kconfig.litex             @mateusz-holenko @kgugala @pgielda
+/drivers/serial/uart_liteuart.c           @mateusz-holenko @kgugala @pgielda
 /drivers/net/                             @jukkar @tbursztyka
 /drivers/ptp_clock/                       @jukkar
 /drivers/spi/                             @tbursztyka
@@ -170,6 +172,7 @@
 /dts/bindings/*/st*                       @erwango
 /dts/bindings/sensor/ams*                 @alexanderwachter
 /dts/bindings/*/sifive*                   @mateusz-holenko @kgugala @pgielda @nategraff-sifive
+/dts/bindings/*/litex*                    @mateusz-holenko @kgugala @pgielda
 /ext/fs/                                  @nashif @wentongwu
 /ext/hal/atmel/asf/sam/include/same70*/   @aurel32
 /ext/hal/atmel/asf/sam0/include/samr21/   @benpicco
diff --git a/drivers/serial/CMakeLists.txt b/drivers/serial/CMakeLists.txt
index 4128acd..7473c51 100644
--- a/drivers/serial/CMakeLists.txt
+++ b/drivers/serial/CMakeLists.txt
@@ -28,6 +28,7 @@
 zephyr_library_sources_if_kconfig(uart_psoc6.c)
 zephyr_library_sources_if_kconfig(uart_pl011.c)
 zephyr_library_sources_if_kconfig(uart_rv32m1_lpuart.c)
+zephyr_library_sources_if_kconfig(uart_liteuart.c)
 
 zephyr_library_sources_ifdef(CONFIG_USERSPACE   uart_handlers.c)
 
diff --git a/drivers/serial/Kconfig b/drivers/serial/Kconfig
index d190610..4e9bb4c 100644
--- a/drivers/serial/Kconfig
+++ b/drivers/serial/Kconfig
@@ -116,4 +116,6 @@
 
 source "drivers/serial/Kconfig.rv32m1_lpuart"
 
+source "drivers/serial/Kconfig.litex"
+
 endif # SERIAL
diff --git a/drivers/serial/Kconfig.litex b/drivers/serial/Kconfig.litex
new file mode 100644
index 0000000..d649adf
--- /dev/null
+++ b/drivers/serial/Kconfig.litex
@@ -0,0 +1,22 @@
+# Kconfig.litex - LiteX UART (LiteUART) configuration option
+#
+# Copyright (c) 2018 - 2019 Antmicro <www.antmicro.com>
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+
+menuconfig UART_LITEUART
+	bool "LiteUART serial driver"
+	depends on SOC_RISCV32_LITEX_VEXRISCV
+	# currently not supporting interrupts due to a bug in LiteUART Tx IRQ
+	depends on !SERIAL_SUPPORT_INTERRUPT
+	select SERIAL_HAS_DRIVER
+	help
+	  This option enables LiteUART serial driver.
+
+menuconfig UART_LITEUART_PORT_0
+	bool "Enable LiteX Port 0"
+	depends on UART_LITEUART
+	help
+	  This tells the driver to configure the UART port at boot, depending on
+	  the additional configuration options below.
diff --git a/drivers/serial/uart_liteuart.c b/drivers/serial/uart_liteuart.c
new file mode 100644
index 0000000..7b2c1fd
--- /dev/null
+++ b/drivers/serial/uart_liteuart.c
@@ -0,0 +1,337 @@
+/*
+ * Copyright (c) 2018 - 2019 Antmicro <www.antmicro.com>
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <kernel.h>
+#include <arch/cpu.h>
+#include <init.h>
+#include <irq.h>
+#include <device.h>
+#include <uart.h>
+#include <zephyr/types.h>
+
+#define UART_EV_TX	(1 << 0)
+#define UART_EV_RX	(1 << 1)
+#define UART_BASE_ADDR	DT_LITEX_UART0_E0001800_BASE_ADDRESS
+#define UART_RXTX	((UART_BASE_ADDR) + 0x00)
+#define UART_TXFULL	((UART_BASE_ADDR) + 0x04)
+#define UART_RXEMPTY	((UART_BASE_ADDR) + 0x08)
+#define UART_EV_STATUS	((UART_BASE_ADDR) + 0x0c)
+#define UART_EV_PENDING	((UART_BASE_ADDR) + 0x10)
+#define UART_EV_ENABLE	((UART_BASE_ADDR) + 0x14)
+#define UART_IRQ	DT_LITEX_UART0_E0001800_IRQ_0
+
+#ifdef CONFIG_UART_INTERRUPT_DRIVEN
+typedef void (*irq_cfg_func_t)(void);
+#endif
+
+struct uart_liteuart_device_config {
+	u32_t port;
+	u32_t sys_clk_freq;
+	u32_t baud_rate;
+#ifdef CONFIG_UART_INTERRUPT_DRIVEN
+	irq_cfg_func_t cfg_func;
+#endif
+};
+
+struct uart_liteuart_data {
+#ifdef CONFIG_UART_INTERRUPT_DRIVEN
+	uart_irq_callback_user_data_t callback;
+	void *cb_data;
+#endif
+};
+
+/**
+ * @brief Output a character in polled mode.
+ *
+ * Writes data to tx register. Waits for space if transmitter is full.
+ *
+ * @param dev UART device struct
+ * @param c Character to send
+ */
+static void uart_liteuart_poll_out(struct device *dev, unsigned char c)
+{
+	/* wait for space */
+	while (sys_read8(UART_TXFULL))
+		;
+
+	sys_write8(c, UART_RXTX);
+}
+
+/**
+ * @brief Poll the device for input.
+ *
+ * @param dev UART device struct
+ * @param c Pointer to character
+ *
+ * @return 0 if a character arrived, -1 if the input buffer if empty.
+ */
+static int uart_liteuart_poll_in(struct device *dev, unsigned char *c)
+{
+	if (!sys_read8(UART_RXEMPTY)) {
+		*c = sys_read8(UART_RXTX);
+
+		/* refresh UART_RXEMPTY by writing UART_EV_RX
+		 * to UART_EV_PENDING
+		 */
+		sys_write8(UART_EV_RX, UART_EV_PENDING);
+		return 0;
+	} else {
+		return -1;
+	}
+}
+
+#ifdef CONFIG_UART_INTERRUPT_DRIVEN
+/**
+ * @brief Enable TX interrupt in event register
+ *
+ * @param dev UART device struct
+ *
+ * @return N/A
+ */
+static void uart_liteuart_irq_tx_enable(struct device *dev)
+{
+	u8_t enable = sys_read8(UART_EV_ENABLE);
+
+	sys_write8(enable | UART_EV_TX, UART_EV_ENABLE);
+}
+
+/**
+ * @brief Disable TX interrupt in event register
+ *
+ * @param dev UART device struct
+ *
+ * @return N/A
+ */
+static void uart_liteuart_irq_tx_disable(struct device *dev)
+{
+	u8_t enable = sys_read8(UART_EV_ENABLE);
+
+	sys_write8(enable & ~(UART_EV_TX), UART_EV_ENABLE);
+}
+
+/**
+ * @brief Enable RX interrupt in event register
+ *
+ * @param dev UART device struct
+ *
+ * @return N/A
+ */
+static void uart_liteuart_irq_rx_enable(struct device *dev)
+{
+	u8_t enable = sys_read8(UART_EV_ENABLE);
+
+	sys_write8(enable | UART_EV_RX, UART_EV_ENABLE);
+}
+
+/**
+ * @brief Disable RX interrupt in event register
+ *
+ * @param dev UART device struct
+ *
+ * @return N/A
+ */
+static void uart_liteuart_irq_rx_disable(struct device *dev)
+{
+	u8_t enable = sys_read8(UART_EV_ENABLE);
+
+	sys_write8(enable & ~(UART_EV_RX), UART_EV_ENABLE);
+}
+
+/**
+ * @brief Check if Tx IRQ has been raised and UART is ready to accept new data
+ *
+ * @param dev UART device struct
+ *
+ * @return 1 if an IRQ has been raised, 0 otherwise
+ */
+static int uart_liteuart_irq_tx_ready(struct device *dev)
+{
+	u8_t val = sys_read8(UART_TXFULL);
+
+	return !val;
+}
+
+/**
+ * @brief Check if Rx IRQ has been raised and there's data to be read from UART
+ *
+ * @param dev UART device struct
+ *
+ * @return 1 if an IRQ has been raised, 0 otherwise
+ */
+static int uart_liteuart_irq_rx_ready(struct device *dev)
+{
+	u8_t pending;
+
+	pending = sys_read8(UART_EV_PENDING);
+
+	if (pending & UART_EV_RX) {
+		return 1;
+	} else {
+		return 0;
+	}
+}
+
+/**
+ * @brief Fill FIFO with data
+ *
+ * @param dev UART device struct
+ * @param tx_data Data to transmit
+ * @param size Number of bytes to send
+ *
+ * @return Number of bytes sent
+ */
+static int uart_liteuart_fifo_fill(struct device *dev,
+		const u8_t *tx_data, int size)
+{
+	int i;
+
+	for (i = 0; i < size && !sys_read8(UART_TXFULL); i++) {
+		sys_write8(tx_data[i], UART_RXTX);
+	}
+
+	return i;
+}
+
+/**
+ * @brief Read data from FIFO
+ *
+ * @param dev UART device struct
+ * @param rxData Data container
+ * @param size Container size
+ *
+ * @return Number of bytes read
+ */
+static int uart_liteuart_fifo_read(struct device *dev,
+		u8_t *rx_data, const int size)
+{
+	int i;
+
+	for (i = 0; i < size && !sys_read8(UART_RXEMPTY); i++) {
+		rx_data[i] = sys_read8(UART_RXTX);
+
+		/* refresh UART_RXEMPTY by writing UART_EV_RX
+		 * to UART_EV_PENDING
+		 */
+		sys_write8(UART_EV_RX, UART_EV_PENDING);
+	}
+
+	return i;
+}
+
+static void uart_liteuart_irq_err(struct device *dev)
+{
+	ARG_UNUSED(dev);
+}
+
+/**
+ * @brief Check if any IRQ is pending
+ *
+ * @param dev UART device struct
+ *
+ * @return 1 if an IRQ is pending, 0 otherwise
+ */
+static int uart_liteuart_irq_is_pending(struct device *dev)
+{
+	u8_t pending;
+
+	pending = sys_read8(UART_EV_PENDING);
+
+	if (pending & (UART_EV_TX | UART_EV_RX)) {
+		return 1;
+	} else {
+		return 0;
+	}
+}
+
+static int uart_liteuart_irq_update(struct device *dev)
+{
+	return 1;
+}
+
+/**
+ * @brief Set the callback function pointer for IRQ.
+ *
+ * @param dev UART device struct
+ * @param cb Callback function pointer.
+ *
+ * @return N/A
+ */
+static void uart_liteuart_irq_callback_set(struct device *dev,
+		uart_irq_callback_user_data_t cb,
+		void *cb_data)
+{
+	struct uart_liteuart_data *data;
+
+	data = (struct uart_liteuart_data *)dev->driver_data;
+	data->callback = cb;
+	data->cb_data = cb_data;
+}
+
+static void liteuart_uart_irq_handler(void *arg)
+{
+	struct device *dev = (struct device *)arg;
+	struct uart_liteuart_data *data = DEV_DATA(dev);
+	int key = irq_lock();
+
+	if (data->callback) {
+		data->callback(data->cb_data);
+	}
+
+	/* clear events */
+	sys_write8(UART_EV_TX | UART_EV_RX, UART_EV_PENDING);
+
+	irq_unlock(key);
+}
+#endif	/* CONFIG_UART_INTERRUPT_DRIVEN */
+
+static const struct uart_driver_api uart_liteuart_driver_api = {
+	.poll_in		= uart_liteuart_poll_in,
+	.poll_out		= uart_liteuart_poll_out,
+	.err_check		= NULL,
+#ifdef CONFIG_UART_INTERRUPT_DRIVEN
+	.fifo_fill		= uart_liteuart_fifo_fill,
+	.fifo_read		= uart_liteuart_fifo_read,
+	.irq_tx_enable		= uart_liteuart_irq_tx_enable,
+	.irq_tx_disable		= uart_liteuart_irq_tx_disable,
+	.irq_tx_ready		= uart_liteuart_irq_tx_ready,
+	.irq_rx_enable		= uart_liteuart_irq_rx_enable,
+	.irq_rx_disable		= uart_liteuart_irq_rx_disable,
+	.irq_rx_ready		= uart_liteuart_irq_rx_ready,
+	.irq_err_enable		= uart_liteuart_irq_err,
+	.irq_err_disable	= uart_liteuart_irq_err,
+	.irq_is_pending		= uart_liteuart_irq_is_pending,
+	.irq_update		= uart_liteuart_irq_update,
+	.irq_callback_set	= uart_liteuart_irq_callback_set
+#endif
+};
+
+static struct uart_liteuart_data uart_liteuart_data_0;
+static int uart_liteuart_init(struct device *dev);
+
+static const struct uart_liteuart_device_config uart_liteuart_dev_cfg_0 = {
+	.port		= UART_BASE_ADDR,
+	.baud_rate	= DT_LITEX_UART0_E0001800_CURRENT_SPEED
+};
+
+DEVICE_AND_API_INIT(uart_liteuart_0, DT_LITEX_UART0_E0001800_LABEL,
+		uart_liteuart_init,
+		&uart_liteuart_data_0, &uart_liteuart_dev_cfg_0,
+		PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
+		(void *)&uart_liteuart_driver_api);
+
+static int uart_liteuart_init(struct device *dev)
+{
+	sys_write8(UART_EV_TX | UART_EV_RX, UART_EV_PENDING);
+
+#ifdef CONFIG_UART_INTERRUPT_DRIVEN
+	IRQ_CONNECT(UART_IRQ, DT_LITEX_UART0_E0001800_IRQ_0_PRIORITY,
+			liteuart_uart_irq_handler, DEVICE_GET(uart_liteuart_0),
+			0);
+	irq_enable(UART_IRQ);
+#endif
+
+	return 0;
+}
diff --git a/dts/bindings/serial/litex,uart0.yaml b/dts/bindings/serial/litex,uart0.yaml
new file mode 100644
index 0000000..bc3e3db
--- /dev/null
+++ b/dts/bindings/serial/litex,uart0.yaml
@@ -0,0 +1,36 @@
+#
+# Copyright (c) 2018 - 2019 Antmicro <www.antmicro.com>
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+---
+title: LiteX UART
+version: 0.1
+
+description: >
+    This binding gives a base representation of the LiteX UART
+
+inherits:
+    !include uart.yaml
+
+properties:
+    compatible:
+      type: string
+      category: required
+      description: compatible strings
+      constraint: "litex,uart0"
+      generation: define
+
+    reg:
+      type: array
+      description: mmio register space
+      generation: define
+      category: required
+
+    interrupts:
+      type: array
+      category: required
+      description: required interrupts
+      generation: define
+
+...