[uart] Refactor uart driver to use UART DIF

Signed-off-by: Silvestrs Timofejevs <silvestrst@lowrisc.org>
diff --git a/sw/device/boot_rom/boot_rom.c b/sw/device/boot_rom/boot_rom.c
index 4947233..a5b8644 100644
--- a/sw/device/boot_rom/boot_rom.c
+++ b/sw/device/boot_rom/boot_rom.c
@@ -38,8 +38,6 @@
   }
 
   LOG_INFO("Boot ROM initialisation has completed, jump into flash!\n");
-  while (!uart_tx_empty()) {
-  }
 
   // Jump into flash. At this point, the contents of the flash binary have been
   // verified, and we can transfer control directly to it. It is the
diff --git a/sw/device/lib/meson.build b/sw/device/lib/meson.build
index 6b139c5..3a506c8 100644
--- a/sw/device/lib/meson.build
+++ b/sw/device/lib/meson.build
@@ -4,17 +4,18 @@
 
 subdir('base')
 subdir('runtime')
+subdir('dif')
 
 # UART library (sw_lib_uart)
 sw_lib_uart = declare_dependency(
   sources: [hw_ip_uart_reg_h],
   link_with: static_library(
     'uart_ot',
-    sources: [
-      hw_ip_uart_reg_h,
-      'uart.c',
+    sources: ['uart.c'],
+    dependencies: [
+      sw_lib_runtime_ibex,
+      dif_uart,
     ],
-    dependencies: [sw_lib_runtime_ibex],
   )
 )
 
diff --git a/sw/device/lib/uart.c b/sw/device/lib/uart.c
index ffa936c..4564734 100644
--- a/sw/device/lib/uart.c
+++ b/sw/device/lib/uart.c
@@ -5,39 +5,26 @@
 #include "sw/device/lib/uart.h"
 
 #include "sw/device/lib/common.h"
+#include "sw/device/lib/dif/dif_uart.h"
 #include "sw/device/lib/runtime/ibex.h"
+#define UART0_BASE_ADDR 0x40000000
 
-inline void uart_init(unsigned int baud) {
-  // nco = 2^20 * baud / fclk
-  uint64_t uart_ctrl_nco = ((uint64_t)baud << 20) / kIbexClockFreqHz;
-  REG32(UART_CTRL(0)) =
-      ((uart_ctrl_nco & UART_CTRL_NCO_MASK) << UART_CTRL_NCO_OFFSET) |
-      (1 << UART_CTRL_TX) | (1 << UART_CTRL_RX);
+static dif_uart_t uart0;
 
-  // reset RX/TX FIFOs
-  REG32(UART_FIFO_CTRL(0)) =
-      (1 << UART_FIFO_CTRL_RXRST) | (1 << UART_FIFO_CTRL_TXRST);
+void uart_init(unsigned int baud) {
+  dif_uart_config_t config = {
+      .baudrate = baud,
+      .clk_freq_hz = kIbexClockFreqHz,
+      .parity_enable = kDifUartDisable,
+      .parity = kDifUartParityEven,
+  };
 
-  // disable interrupts
-  REG32(UART_INTR_ENABLE(0)) = 0;
-}
-
-static int uart_tx_rdy(void) {
-  return !(REG32(UART_STATUS(0)) & (1 << UART_STATUS_TXFULL));
+  mmio_region_t base_addr = mmio_region_from_addr(UART0_BASE_ADDR);
+  (void)dif_uart_init(base_addr, &config, &uart0);
 }
 
 void uart_send_char(char c) {
-  while (!uart_tx_rdy()) {
-  }
-  REG32(UART_WDATA(0)) = c;
-}
-
-int uart_tx_empty(void) {
-  return !!(REG32(UART_STATUS(0)) & (1 << UART_STATUS_TXEMPTY));
-}
-
-int uart_tx_idle(void) {
-  return !!(REG32(UART_STATUS(0)) & (1 << UART_STATUS_TXIDLE));
+  (void)dif_uart_byte_send_polled(&uart0, (uint8_t)c);
 }
 
 void uart_send_str(char *str) {
@@ -54,15 +41,6 @@
   }
 }
 
-int uart_rx_empty(void) {
-  return !!(REG32(UART_STATUS(0)) & (1 << UART_STATUS_RXEMPTY));
-}
-
 int uart_rcv_char(char *c) {
-  if (uart_rx_empty()) {
-    return -1;
-  }
-
-  *c = REG32(UART_RDATA(0));
-  return 0;
+  return dif_uart_byte_receive_polled(&uart0, (uint8_t *)c);
 }
diff --git a/sw/device/lib/uart.h b/sw/device/lib/uart.h
index 5c79d68..aa616f6 100644
--- a/sw/device/lib/uart.h
+++ b/sw/device/lib/uart.h
@@ -7,10 +7,6 @@
 
 #include <stdint.h>
 
-#define UART0_BASE_ADDR 0x40000000
-
-#include "uart_regs.h"  // Generated.
-
 void uart_send_char(char c);
 
 /**
@@ -23,9 +19,6 @@
  * Send a NULL-terminated string over UART
  */
 void uart_send_str(char *str);
-int uart_rx_empty(void);
-int uart_tx_empty(void);
-int uart_tx_idle(void);
 
 /**
  * Receive a single character from UART