[dif] Delete common.h

Signed-off-by: Miguel Young de la Sota <mcyoung@google.com>
diff --git a/sw/device/benchmarks/coremark/top_earlgrey/core_portme.h b/sw/device/benchmarks/coremark/top_earlgrey/core_portme.h
index 643c2e0..2fe8bb5 100644
--- a/sw/device/benchmarks/coremark/top_earlgrey/core_portme.h
+++ b/sw/device/benchmarks/coremark/top_earlgrey/core_portme.h
@@ -14,8 +14,6 @@
 
 #include <stddef.h>
 
-#include "sw/device/lib/common.h"
-
 extern unsigned int _stack_start;
 
 /************************/
diff --git a/sw/device/boot_rom/boot_rom.c b/sw/device/boot_rom/boot_rom.c
index dfff4b9..feb4fbe 100644
--- a/sw/device/boot_rom/boot_rom.c
+++ b/sw/device/boot_rom/boot_rom.c
@@ -6,7 +6,6 @@
 #include "sw/device/boot_rom/chip_info.h"  // Generated.
 #include "sw/device/lib/arch/device.h"
 #include "sw/device/lib/base/mmio.h"
-#include "sw/device/lib/common.h"
 #include "sw/device/lib/dif/dif_gpio.h"
 #include "sw/device/lib/dif/dif_uart.h"
 #include "sw/device/lib/pinmux.h"
diff --git a/sw/device/examples/hello_usbdev/hello_usbdev.c b/sw/device/examples/hello_usbdev/hello_usbdev.c
index c8bd0fc..3a5ff5f 100644
--- a/sw/device/examples/hello_usbdev/hello_usbdev.c
+++ b/sw/device/examples/hello_usbdev/hello_usbdev.c
@@ -7,7 +7,6 @@
 
 #include "sw/device/examples/demos.h"
 #include "sw/device/lib/arch/device.h"
-#include "sw/device/lib/common.h"
 #include "sw/device/lib/dif/dif_gpio.h"
 #include "sw/device/lib/dif/dif_spi_device.h"
 #include "sw/device/lib/dif/dif_uart.h"
@@ -26,6 +25,8 @@
 #define USBDEV_BASE_ADDR 0x40150000
 #include "usbdev_regs.h"  // Generated.
 
+#define REG32(add) *((volatile uint32_t *)(add))
+
 /**
  * Configuration values for USB.
  */
diff --git a/sw/device/lib/aes.c b/sw/device/lib/aes.c
index 99920e2..55d8dbc 100644
--- a/sw/device/lib/aes.c
+++ b/sw/device/lib/aes.c
@@ -4,8 +4,6 @@
 
 #include "sw/device/lib/aes.h"
 
-#include "sw/device/lib/common.h"
-
 #include "aes_regs.h"  // Generated.
 #include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"
 
@@ -14,6 +12,8 @@
 #define AES_NUM_REGS_IV 4
 #define AES_NUM_REGS_DATA 4
 
+#define REG32(add) *((volatile uint32_t *)(add))
+
 void aes_init(aes_cfg_t aes_cfg) {
   uint32_t cfg_val =
       (aes_cfg.operation << AES_CTRL_SHADOWED_OPERATION) |
diff --git a/sw/device/lib/common.h b/sw/device/lib/common.h
deleted file mode 100644
index 999cebb..0000000
--- a/sw/device/lib/common.h
+++ /dev/null
@@ -1,15 +0,0 @@
-// Copyright lowRISC contributors.
-// Licensed under the Apache License, Version 2.0, see LICENSE for details.
-// SPDX-License-Identifier: Apache-2.0
-
-#ifndef OPENTITAN_SW_DEVICE_LIB_COMMON_H_
-#define OPENTITAN_SW_DEVICE_LIB_COMMON_H_
-
-#define REG8(add) *((volatile uint8_t *)(add))
-#define REG16(add) *((volatile uint16_t *)(add))
-#define REG32(add) *((volatile uint32_t *)(add))
-#define SETBIT(val, bit) (val | 1 << bit)
-#define CLRBIT(val, bit) (val & ~(1 << bit))
-
-
-#endif  // OPENTITAN_SW_DEVICE_LIB_COMMON_H_
diff --git a/sw/device/lib/dif/dif_pwrmgr.c b/sw/device/lib/dif/dif_pwrmgr.c
index e82c31d..e24a5c4 100644
--- a/sw/device/lib/dif/dif_pwrmgr.c
+++ b/sw/device/lib/dif/dif_pwrmgr.c
@@ -4,7 +4,6 @@
 
 #include "sw/device/lib/dif/dif_pwrmgr.h"
 
-#include "sw/device/lib/common.h"
 
 #include "pwrmgr_regs.h"  // Generated.
 
diff --git a/sw/device/lib/flash_ctrl.c b/sw/device/lib/flash_ctrl.c
index eaa9478..1235a1f 100644
--- a/sw/device/lib/flash_ctrl.c
+++ b/sw/device/lib/flash_ctrl.c
@@ -3,13 +3,16 @@
 // SPDX-License-Identifier: Apache-2.0
 #include "sw/device/lib/flash_ctrl.h"
 
-#include "sw/device/lib/common.h"
 
 #include "flash_ctrl_regs.h"  // Generated.
 #include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"
 
 #define FLASH_CTRL0_BASE_ADDR TOP_EARLGREY_FLASH_CTRL_BASE_ADDR
 
+#define REG32(add) *((volatile uint32_t *)(add))
+#define SETBIT(val, bit) (val | 1 << bit)
+#define CLRBIT(val, bit) (val & ~(1 << bit))
+
 typedef enum flash_op {
   FLASH_READ = 0,
   FLASH_PROG = 1,
diff --git a/sw/device/lib/handler.c b/sw/device/lib/handler.c
index f9617e9..4b6a93c 100644
--- a/sw/device/lib/handler.c
+++ b/sw/device/lib/handler.c
@@ -5,7 +5,6 @@
 #include "sw/device/lib/handler.h"
 
 #include "sw/device/lib/base/stdasm.h"
-#include "sw/device/lib/common.h"
 #include "sw/device/lib/runtime/log.h"
 
 /**
diff --git a/sw/device/lib/hmac.c b/sw/device/lib/hmac.c
index f423500..0647a90 100644
--- a/sw/device/lib/hmac.c
+++ b/sw/device/lib/hmac.c
@@ -4,8 +4,6 @@
 
 #include "sw/device/lib/hmac.h"
 
-#include "sw/device/lib/common.h"
-
 #include "hmac_regs.h"  // Generated.
 #include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"
 
@@ -13,6 +11,8 @@
 #define HMAC_FIFO_MAX 16
 #define HMAC_FIFO_GROUP_SIZE HMAC_FIFO_MAX / 2
 
+#define REG32(add) *((volatile uint32_t *)(add))
+
 void hmac_init(hmac_cfg_t hmac_cfg) {
   REG32(HMAC_CFG(0)) = hmac_cfg.input_endian_swap << HMAC_CFG_ENDIAN_SWAP |
                        1 << hmac_cfg.mode |
@@ -61,7 +61,7 @@
 
   const uint8_t *bp = (const uint8_t *)wp;
   for (; bytes_left_over > 0; --bytes_left_over) {
-    REG8(HMAC_MSG_FIFO(0)) = *bp++;
+    *((volatile uint8_t *)HMAC_MSG_FIFO(0)) = *bp++;
   }
 }
 
diff --git a/sw/device/lib/pinmux.c b/sw/device/lib/pinmux.c
index f29251a..aacf5ad 100644
--- a/sw/device/lib/pinmux.c
+++ b/sw/device/lib/pinmux.c
@@ -4,7 +4,7 @@
 
 #include "sw/device/lib/pinmux.h"
 
-#include "sw/device/lib/common.h"
+#include "sw/device/lib/base/mmio.h"
 #include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"
 
 #include "pinmux_regs.h"  // Generated.
@@ -13,19 +13,20 @@
 
 static void init_pinmux_reg(uint32_t reg, uint32_t size, uint32_t num_fields,
                             uint32_t mask, uint32_t start_v) {
+  mmio_region_t reg32 = mmio_region_from_addr(reg);
   uint32_t reg_value = 0;
   uint32_t reg_offset = 0;
 
   for (uint32_t i = 0; i < NUM_MIO; ++i) {
     reg_value |= ((i + start_v) & mask) << (size * (i % num_fields));
     if ((i % num_fields) == (num_fields - 1)) {
-      REG32(reg + reg_offset) = reg_value;
+      mmio_region_write32(reg32, reg_offset, reg_value);
       reg_value = 0;
       reg_offset += 4;
     }
   }
 
-  REG32(reg + reg_offset) = reg_value;
+  mmio_region_write32(reg32, reg_offset, reg_value);
 }
 
 void pinmux_init(void) {
diff --git a/sw/device/lib/uart.c b/sw/device/lib/uart.c
new file mode 100644
index 0000000..919d86b
--- /dev/null
+++ b/sw/device/lib/uart.c
@@ -0,0 +1,79 @@
+// Copyright lowRISC contributors.
+// Licensed under the Apache License, Version 2.0, see LICENSE for details.
+// SPDX-License-Identifier: Apache-2.0
+
+#include "sw/device/lib/uart.h"
+
+#include "sw/device/lib/arch/device.h"
+#include "sw/device/lib/dif/dif_uart.h"
+#include "sw/device/lib/runtime/ibex.h"
+
+#include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"
+
+static dif_uart_t uart0;
+
+void uart_init(unsigned int baud) {
+  // Note that, due to a GCC bug, we cannot use the standard `(void) expr`
+  // syntax to drop this value on the ground.
+  // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509
+  mmio_region_t base_addr = mmio_region_from_addr(TOP_EARLGREY_UART_BASE_ADDR);
+  if (dif_uart_init((dif_uart_params_t){.base_addr = base_addr}, &uart0)) {
+  }
+  if (dif_uart_configure(&uart0, (dif_uart_config_t){
+                                     .baudrate = baud,
+                                     .clk_freq_hz = kClockFreqPeripheralHz,
+                                     .parity_enable = kDifUartToggleDisabled,
+                                     .parity = kDifUartParityEven,
+                                 })) {
+  }
+}
+
+void uart_send_char(char c) {
+  // Note that, due to a GCC bug, we cannot use the standard `(void) expr`
+  // syntax to drop this value on the ground.
+  // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509
+  if (dif_uart_byte_send_polled(&uart0, (uint8_t)c)) {
+  }
+}
+
+void uart_send_str(char *str) {
+  while (*str != '\0') {
+    uart_send_char(*str++);
+  }
+}
+
+size_t uart_send_buf(void *data, const char *buf, size_t len) {
+  for (size_t i = 0; i < len; ++i) {
+    uart_send_char(buf[i]);
+  }
+  return len;
+}
+
+const buffer_sink_t uart_stdout = {
+    .data = NULL, .sink = &uart_send_buf,
+};
+
+#define hexchar(i) (((i & 0xf) > 9) ? (i & 0xf) - 10 + 'A' : (i & 0xf) + '0')
+
+void uart_send_uint(uint32_t n, int bits) {
+  for (int i = bits - 4; i >= 0; i -= 4) {
+    uart_send_char(hexchar(n >> i));
+  }
+}
+
+int uart_rcv_char(char *c) {
+  size_t num_bytes = 0;
+  if (dif_uart_rx_bytes_available(&uart0, &num_bytes) != kDifUartOk) {
+    return -1;
+  }
+  if (num_bytes == 0) {
+    return -1;
+  }
+  // The pointer cast from char* to uint8_t* is dangerous. This needs to be
+  // revisited.
+  if (dif_uart_bytes_receive(&uart0, 1, (uint8_t *)c, NULL) != kDifUartOk) {
+    return -1;
+  }
+
+  return 0;
+}
diff --git a/sw/device/lib/usb_controlep.c b/sw/device/lib/usb_controlep.c
index d9b2f28..fbb20c2 100644
--- a/sw/device/lib/usb_controlep.c
+++ b/sw/device/lib/usb_controlep.c
@@ -4,7 +4,6 @@
 
 #include "sw/device/lib/usb_controlep.h"
 
-#include "sw/device/lib/common.h"
 #include "sw/device/lib/usb_consts.h"
 #include "sw/device/lib/usbdev.h"
 
diff --git a/sw/device/lib/usb_simpleserial.c b/sw/device/lib/usb_simpleserial.c
index 948db05..b7e874a 100644
--- a/sw/device/lib/usb_simpleserial.c
+++ b/sw/device/lib/usb_simpleserial.c
@@ -4,7 +4,6 @@
 
 #include "sw/device/lib/usb_simpleserial.h"
 
-#include "sw/device/lib/common.h"
 #include "sw/device/lib/usbdev.h"
 
 #define MAX_GATHER 16
diff --git a/sw/device/lib/usb_simpleserial.h b/sw/device/lib/usb_simpleserial.h
index 5929c76..8a81875 100644
--- a/sw/device/lib/usb_simpleserial.h
+++ b/sw/device/lib/usb_simpleserial.h
@@ -8,7 +8,6 @@
 #include <stddef.h>
 #include <stdint.h>
 
-#include "sw/device/lib/common.h"
 #include "sw/device/lib/usbdev.h"
 
 // This is only here because caller of _init needs it
diff --git a/sw/device/lib/usbdev.c b/sw/device/lib/usbdev.c
index 1ba1253..9eb7dc5 100644
--- a/sw/device/lib/usbdev.c
+++ b/sw/device/lib/usbdev.c
@@ -4,7 +4,6 @@
 
 #include "sw/device/lib/usbdev.h"
 
-#include "sw/device/lib/common.h"
 
 #include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"
 #include "usbdev_regs.h"  // Generated.
@@ -13,6 +12,11 @@
 
 #define EXTRACT(n, f) ((n >> USBDEV_##f##_OFFSET) & USBDEV_##f##_MASK)
 
+#define SETBIT(val, bit) (val | 1 << bit)
+#define CLRBIT(val, bit) (val & ~(1 << bit))
+
+#define REG32(add) *((volatile uint32_t *)(add))
+
 // Free buffer pool is held on a simple stack
 // Initalize to all buffer IDs are free
 static void buf_init(usbdev_ctx_t *ctx) {
diff --git a/sw/device/tests/dif/dif_hmac_sanitytest.c b/sw/device/tests/dif/dif_hmac_sanitytest.c
index 9401d7e..9fdac9d 100644
--- a/sw/device/tests/dif/dif_hmac_sanitytest.c
+++ b/sw/device/tests/dif/dif_hmac_sanitytest.c
@@ -5,7 +5,6 @@
 #include "sw/device/lib/dif/dif_hmac.h"
 #include "sw/device/lib/arch/device.h"
 #include "sw/device/lib/base/mmio.h"
-#include "sw/device/lib/common.h"
 #include "sw/device/lib/flash_ctrl.h"
 #include "sw/device/lib/runtime/check.h"
 #include "sw/device/lib/runtime/log.h"
diff --git a/sw/device/tests/flash_ctrl_test.c b/sw/device/tests/flash_ctrl_test.c
index 002ddb2..56c5f04 100644
--- a/sw/device/tests/flash_ctrl_test.c
+++ b/sw/device/tests/flash_ctrl_test.c
@@ -6,7 +6,6 @@
 
 #include "sw/device/lib/base/memory.h"
 #include "sw/device/lib/base/mmio.h"
-#include "sw/device/lib/common.h"
 #include "sw/device/lib/runtime/check.h"
 #include "sw/device/lib/runtime/log.h"
 #include "sw/device/lib/testing/test_main.h"