Remove existing GPIO driver and update call-sites to use dif_gpio

Signed-off-by: Alphan Ulusoy <alphan@google.com>
diff --git a/sw/device/boot_rom/boot_rom.c b/sw/device/boot_rom/boot_rom.c
index d8b49ea..7f86a2c 100644
--- a/sw/device/boot_rom/boot_rom.c
+++ b/sw/device/boot_rom/boot_rom.c
@@ -7,8 +7,8 @@
 #include "sw/device/lib/arch/device.h"
 #include "sw/device/lib/base/stdasm.h"
 #include "sw/device/lib/common.h"
+#include "sw/device/lib/dif/dif_gpio.h"
 #include "sw/device/lib/flash_ctrl.h"
-#include "sw/device/lib/gpio.h"
 #include "sw/device/lib/log.h"
 #include "sw/device/lib/pinmux.h"
 #include "sw/device/lib/spi_device.h"
diff --git a/sw/device/boot_rom/bootstrap.c b/sw/device/boot_rom/bootstrap.c
index 30258fe..c9c7e6e 100644
--- a/sw/device/boot_rom/bootstrap.c
+++ b/sw/device/boot_rom/bootstrap.c
@@ -6,12 +6,15 @@
 
 #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/flash_ctrl.h"
-#include "sw/device/lib/gpio.h"
 #include "sw/device/lib/hw_sha256.h"
 #include "sw/device/lib/spi_device.h"
 #include "sw/device/lib/uart.h"  // TODO: Wrap uart in DEBUG macros.
 
+#define GPIO0_BASE_ADDR 0x40010000u
+#define GPIO_BOOTSTRAP_BIT_MASK 0x00020000u
+
 /* Checks if flash is blank to determine if bootstrap is needed. */
 /* TODO: Update this to check bootstrap pin instead in Verilator. */
 static int bootstrap_requested(void) {
@@ -21,7 +24,15 @@
     return !!(REG32(FLASH_MEM_BASE_ADDR) == 0 ||
               REG32(FLASH_MEM_BASE_ADDR) == 0xFFFFFFFF);
   }
-  return !!(gpio_read() & GPIO_BOOTSTRAP_BIT_MASK);
+  // Initialize GPIO device
+  dif_gpio_t gpio;
+  dif_gpio_config_t gpio_config = {.base_addr =
+                                       mmio_region_from_addr(GPIO0_BASE_ADDR)};
+  dif_gpio_init(&gpio_config, &gpio);
+  // Read pin
+  uint32_t gpio_in;
+  dif_gpio_all_read(&gpio, &gpio_in);
+  return !!(gpio_in & GPIO_BOOTSTRAP_BIT_MASK);
 }
 
 /* Erase all flash, and verify blank. */
diff --git a/sw/device/boot_rom/meson.build b/sw/device/boot_rom/meson.build
index 25ab123..fa46e6c 100644
--- a/sw/device/boot_rom/meson.build
+++ b/sw/device/boot_rom/meson.build
@@ -38,7 +38,7 @@
       sw_lib_runtime_hart,
       sw_lib_flash_ctrl,
       sw_lib_pinmux,
-      sw_lib_gpio,
+      sw_lib_dif_gpio,
       sw_lib_hmac,
       sw_lib_spi_device,
       sw_lib_uart,
diff --git a/sw/device/examples/hello_usbdev/hello_usbdev.c b/sw/device/examples/hello_usbdev/hello_usbdev.c
index 8994c2b..add8070 100644
--- a/sw/device/examples/hello_usbdev/hello_usbdev.c
+++ b/sw/device/examples/hello_usbdev/hello_usbdev.c
@@ -7,7 +7,7 @@
 #include "sw/device/lib/arch/device.h"
 #include "sw/device/lib/base/stdasm.h"
 #include "sw/device/lib/common.h"
-#include "sw/device/lib/gpio.h"
+#include "sw/device/lib/dif/dif_gpio.h"
 #include "sw/device/lib/pinmux.h"
 #include "sw/device/lib/runtime/hart.h"
 #include "sw/device/lib/spi_device.h"
@@ -21,6 +21,9 @@
 #include "usbdev_regs.h"  // Generated.
 
 #define SPI_MAX 32
+#define GPIO0_BASE_ADDR 0x40010000u
+
+static dif_gpio_t gpio;
 
 // called from ctr0 when something bad happens
 // char I=illegal instruction, A=lsu error (address), E=ecall
@@ -28,9 +31,9 @@
   uart_send_char(c);
   uart_send_uint(mepc, 32);
   while (1) {
-    gpio_write_all(0xAA00);  // pattern
+    dif_gpio_all_write(&gpio, 0xAA00);  // pattern
     usleep(200 * 1000);
-    gpio_write_all(0x5500);  // pattern
+    dif_gpio_all_write(&gpio, 0x5500);  // pattern
     usleep(100 * 1000);
   }
 }
@@ -76,12 +79,14 @@
 
 int main(int argc, char **argv) {
   uart_init(kUartBaudrate);
-
   pinmux_init();
-  // Enable GPIO: 0-7 and 16 is input, 8-15 is output
-  gpio_init(0xFF00);
-
   spid_init();
+  // Enable GPIO: 0-7 and 16 is input, 8-15 is output
+  dif_gpio_config_t gpio_config = {.base_addr =
+                                       mmio_region_from_addr(GPIO0_BASE_ADDR)};
+  dif_gpio_init(&gpio_config, &gpio);
+  dif_gpio_output_mode_all_set(&gpio, 0xFF00);
+
   // Add DATE and TIME because I keep fooling myself with old versions
   uart_send_str(
       "Hello USB! "__DATE__
@@ -91,9 +96,9 @@
   uart_send_str("Characters from USB go to UART\r\n");
 
   // Give a LED pattern as startup indicator for 5 seconds
-  gpio_write_all(0xAA00);  // pattern
+  dif_gpio_all_write(&gpio, 0xAA00);  // pattern
   usleep(1000);
-  gpio_write_all(0x5500);  // pattern
+  dif_gpio_all_write(&gpio, 0x5500);  // pattern
   // usbdev_init here so dpi code will not start until simulation
   // got through all the printing (which takes a while if --trace)
   usbdev_init(&usbdev_ctx);
@@ -114,7 +119,8 @@
     usbdev_poll(&usbdev_ctx);
 
     // report changed switches over UART
-    gpio_in = gpio_read() & 0x100FF;  // 0-7 is switch input, 16 is FTDI
+    dif_gpio_all_read(&gpio, &gpio_in);
+    gpio_in &= 0x100FF;  // 0-7 is switch input, 16 is FTDI
     gpio_in_changes = (gpio_in & ~gpio_in_prev) | (~gpio_in & gpio_in_prev);
     for (int b = 0; b < 8; b++) {
       if (gpio_in_changes & (1 << b)) {
@@ -147,7 +153,7 @@
     char rcv_char;
     while (uart_rcv_char(&rcv_char) != -1) {
       uart_send_char(rcv_char);
-      gpio_write_all(rcv_char << 8);
+      dif_gpio_all_write(&gpio, rcv_char << 8);
       if (rcv_char == '/') {
         uart_send_char('I');
         uart_send_uint(REG32(USBDEV_INTR_STATE()), 16);
diff --git a/sw/device/examples/hello_usbdev/meson.build b/sw/device/examples/hello_usbdev/meson.build
index 149b52d..9c23020 100644
--- a/sw/device/examples/hello_usbdev/meson.build
+++ b/sw/device/examples/hello_usbdev/meson.build
@@ -10,7 +10,7 @@
     dependencies: [
     sw_lib_runtime_hart,
     sw_lib_pinmux,
-    sw_lib_gpio,
+    sw_lib_dif_gpio,
     sw_lib_irq,
     sw_lib_spi_device,
     sw_lib_uart,
diff --git a/sw/device/examples/hello_world/hello_world.c b/sw/device/examples/hello_world/hello_world.c
index 4afaea7..de94d6d 100644
--- a/sw/device/examples/hello_world/hello_world.c
+++ b/sw/device/examples/hello_world/hello_world.c
@@ -5,13 +5,16 @@
 #include "sw/device/lib/arch/device.h"
 #include "sw/device/lib/base/stdasm.h"
 #include "sw/device/lib/common.h"
-#include "sw/device/lib/gpio.h"
+#include "sw/device/lib/dif/dif_gpio.h"
 #include "sw/device/lib/pinmux.h"
 #include "sw/device/lib/runtime/hart.h"
 #include "sw/device/lib/spi_device.h"
 #include "sw/device/lib/uart.h"
 
 #define SPI_MAX 32
+#define GPIO0_BASE_ADDR 0x40010000u
+
+static dif_gpio_t gpio;
 
 // called from ctr0 when something bad happens
 // char I=illegal instruction, A=lsu error (address), E=ecall
@@ -19,9 +22,9 @@
   uart_send_char(c);
   uart_send_uint(mepc, 32);
   while (1) {
-    gpio_write_all(0xAA00);  // pattern
+    dif_gpio_all_write(&gpio, 0xAA00);  // pattern
     usleep(200 * 1000);
-    gpio_write_all(0x5500);  // pattern
+    dif_gpio_all_write(&gpio, 0x5500);  // pattern
     usleep(100 * 1000);
   }
 }
@@ -30,12 +33,13 @@
 
 int main(int argc, char **argv) {
   uart_init(kUartBaudrate);
-
   pinmux_init();
-  // Enable GPIO: 0-7 and 16 is input, 8-15 is output
-  gpio_init(0xFF00);
-
   spid_init();
+  // Enable GPIO: 0-7 and 16 is input, 8-15 is output
+  dif_gpio_config_t gpio_config = {.base_addr =
+                                       mmio_region_from_addr(GPIO0_BASE_ADDR)};
+  dif_gpio_init(&gpio_config, &gpio);
+  dif_gpio_output_mode_all_set(&gpio, 0xFF00);
   // Add DATE and TIME because I keep fooling myself with old versions
   uart_send_str(
       "Hello World! "__DATE__
@@ -44,16 +48,16 @@
   uart_send_str("Watch the LEDs!\r\n");
 
   // Give a LED pattern as startup indicator for 5 seconds
-  gpio_write_all(0xFF00);  // all LEDs on
+  dif_gpio_all_write(&gpio, 0xFF00);  // all LEDs on
   for (int i = 0; i < 32; i++) {
     usleep(100 * 1000);  // 100 ms
 
-    gpio_write_bit(8 + (i % 8), (i / 8));
+    dif_gpio_pin_write(&gpio, 8 + (i % 8), (i / 8) % 2);
   }
 
   // Now have UART <-> Buttons/LEDs demo
   // all LEDs off
-  gpio_write_all(0x0000);
+  dif_gpio_all_write(&gpio, 0x0000);
   uart_send_str("Try out the switches on the board\r\n");
   uart_send_str("or type anything into the console window.\r\n");
   uart_send_str("The LEDs show the ASCII code of the last character.\r\n");
@@ -70,7 +74,8 @@
     usleep(10 * 1000);  // 10 ms
 
     // report changed switches over UART
-    gpio_in = gpio_read() & 0x100FF;  // 0-7 is switch input, 16 is FTDI
+    dif_gpio_all_read(&gpio, &gpio_in);
+    gpio_in &= 0x100FF;  // 0-7 is switch input, 16 is FTDI
     gpio_in_changes = (gpio_in & ~gpio_in_prev) | (~gpio_in & gpio_in_prev);
     for (int b = 0; b < 8; b++) {
       if (gpio_in_changes & (1 << b)) {
@@ -103,7 +108,7 @@
     char rcv_char;
     while (uart_rcv_char(&rcv_char) != -1) {
       uart_send_char(rcv_char);
-      gpio_write_all(rcv_char << 8);
+      dif_gpio_all_write(&gpio, rcv_char << 8);
     }
   }
 }
diff --git a/sw/device/examples/hello_world/meson.build b/sw/device/examples/hello_world/meson.build
index b02d3c9..e709464 100644
--- a/sw/device/examples/hello_world/meson.build
+++ b/sw/device/examples/hello_world/meson.build
@@ -10,7 +10,7 @@
     dependencies: [
       sw_lib_runtime_hart,
       sw_lib_pinmux,
-      sw_lib_gpio,
+      sw_lib_dif_gpio,
       sw_lib_irq,
       sw_lib_spi_device,
       sw_lib_uart,
diff --git a/sw/device/lib/gpio.c b/sw/device/lib/gpio.c
deleted file mode 100644
index 8c1d737..0000000
--- a/sw/device/lib/gpio.c
+++ /dev/null
@@ -1,35 +0,0 @@
-// 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/gpio.h"
-
-#include "sw/device/lib/base/memory.h"
-#include "sw/device/lib/common.h"
-#include "sw/device/lib/runtime/hart.h"
-
-void gpio_init(uint32_t oe) { REG32(GPIO_DIRECT_OE(0)) = oe; }
-
-void gpio_write_bit(unsigned int bit, unsigned int val) {
-  uint32_t mask = 0;
-  uint32_t reg_val = 0;
-  volatile uint32_t *gpio_masked_out_reg = 0;
-
-  if (bit < 16) {
-    gpio_masked_out_reg = (uint32_t *)GPIO_MASKED_OUT_LOWER(0);
-  } else if (bit < 32) {
-    gpio_masked_out_reg = (uint32_t *)GPIO_MASKED_OUT_UPPER(0);
-  } else {
-    /* bit must be less then 32 */
-    abort();
-  }
-  bit %= 16;
-
-  mask = (1 << bit);
-  reg_val = (mask << 16) | ((val & 0x01) << bit);
-  *gpio_masked_out_reg = reg_val;
-}
-
-void gpio_write_all(uint32_t val) { REG32(GPIO_DIRECT_OUT(0)) = val; }
-
-uint32_t gpio_read(void) { return REG32(GPIO_DATA_IN(0)); }
diff --git a/sw/device/lib/gpio.h b/sw/device/lib/gpio.h
deleted file mode 100644
index b2ea274..0000000
--- a/sw/device/lib/gpio.h
+++ /dev/null
@@ -1,25 +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_GPIO_H_
-#define OPENTITAN_SW_DEVICE_LIB_GPIO_H_
-
-#include <stdint.h>
-
-#include "gpio_regs.h"  // Generated.
-
-#define GPIO0_BASE_ADDR 0x40010000
-
-#define GPIO_BOOTSTRAP_BIT_MASK 0x20000
-
-/**
- * @param oe bits to use as output
- */
-void gpio_init(uint32_t oe);
-
-void gpio_write_bit(unsigned int bit, unsigned int val);
-void gpio_write_all(uint32_t val);
-uint32_t gpio_read(void);
-
-#endif  // OPENTITAN_SW_DEVICE_LIB_GPIO_H_
diff --git a/sw/device/lib/meson.build b/sw/device/lib/meson.build
index 07e1d43..cacb167 100644
--- a/sw/device/lib/meson.build
+++ b/sw/device/lib/meson.build
@@ -32,19 +32,6 @@
   )
 )
 
-# GPIO library (sw_lib_gpio)
-sw_lib_gpio = declare_dependency(
-  sources: [hw_ip_gpio_reg_h],
-  link_with: static_library(
-    'gpio_ot',
-    sources: [
-      hw_ip_gpio_reg_h,
-      'gpio.c',
-    ],
-    dependencies: [sw_lib_runtime_hart],
-  )
-)
-
 # PINMUX library (sw_lib_pinmux)
 sw_lib_pinmux = declare_dependency(
   sources: [hw_top_earlgrey_pinmux_h],
diff --git a/sw/device/tests/flash_ctrl/flash_test.c b/sw/device/tests/flash_ctrl/flash_test.c
index 08cdd5a..0a8820c 100644
--- a/sw/device/tests/flash_ctrl/flash_test.c
+++ b/sw/device/tests/flash_ctrl/flash_test.c
@@ -5,8 +5,8 @@
 #include "sw/device/lib/arch/device.h"
 #include "sw/device/lib/base/stdasm.h"
 #include "sw/device/lib/common.h"
+#include "sw/device/lib/dif/dif_gpio.h"
 #include "sw/device/lib/flash_ctrl.h"
-#include "sw/device/lib/gpio.h"
 #include "sw/device/lib/runtime/hart.h"
 #include "sw/device/lib/uart.h"
 
diff --git a/sw/device/tests/flash_ctrl/meson.build b/sw/device/tests/flash_ctrl/meson.build
index 429e7ef..6c5f11d 100644
--- a/sw/device/tests/flash_ctrl/meson.build
+++ b/sw/device/tests/flash_ctrl/meson.build
@@ -10,7 +10,7 @@
     dependencies: [
       sw_lib_runtime_hart,
       sw_lib_flash_ctrl,
-      sw_lib_gpio,
+      sw_lib_dif_gpio,
       sw_lib_irq,
       sw_lib_uart,
       riscv_crt,
diff --git a/sw/device/tests/rv_timer/meson.build b/sw/device/tests/rv_timer/meson.build
index 4e1177d..6292dbd 100644
--- a/sw/device/tests/rv_timer/meson.build
+++ b/sw/device/tests/rv_timer/meson.build
@@ -11,7 +11,7 @@
       sw_lib_irq,
       sw_lib_rv_timer,
       sw_lib_uart,
-      sw_lib_gpio,
+      sw_lib_dif_gpio,
       sw_lib_pinmux,
       riscv_crt,
       sw_lib_irq_handlers,
@@ -35,4 +35,4 @@
     build_always_stale: true,
     build_by_default: true,
   )
-endforeach
\ No newline at end of file
+endforeach
diff --git a/sw/device/tests/rv_timer/rv_timer_test.c b/sw/device/tests/rv_timer/rv_timer_test.c
index 79d739e..9c98e50 100644
--- a/sw/device/tests/rv_timer/rv_timer_test.c
+++ b/sw/device/tests/rv_timer/rv_timer_test.c
@@ -6,11 +6,14 @@
 
 #include "sw/device/lib/arch/device.h"
 #include "sw/device/lib/common.h"
-#include "sw/device/lib/gpio.h"
+#include "sw/device/lib/dif/dif_gpio.h"
 #include "sw/device/lib/irq.h"
 #include "sw/device/lib/pinmux.h"
 #include "sw/device/lib/uart.h"
 
+#define GPIO0_BASE_ADDR 0x40010000u
+
+static dif_gpio_t gpio;
 static uint32_t intr_handling_success = 0;
 static const uint32_t hart = 0;
 
@@ -21,7 +24,10 @@
 
   pinmux_init();
   // Enable GPIO: 0-7 and 16 is input, 8-15 is output
-  gpio_init(0xFF00);
+  dif_gpio_config_t gpio_config = {.base_addr =
+                                       mmio_region_from_addr(GPIO0_BASE_ADDR)};
+  dif_gpio_init(&gpio_config, &gpio);
+  dif_gpio_output_mode_all_set(&gpio, 0xFF00);
 
   irq_global_ctrl(true);
   irq_timer_ctrl(true);
@@ -30,7 +36,7 @@
   rv_timer_ctrl(hart, true);
   rv_timer_intr_enable(hart, true);
 
-  gpio_write_all(0xFF00);  // all LEDs on
+  dif_gpio_all_write(&gpio, 0xFF00);  // all LEDs on
 
   while (1) {
     if (intr_handling_success) {
@@ -38,7 +44,7 @@
     }
   }
 
-  gpio_write_all(0xAA00);  // Test Completed
+  dif_gpio_all_write(&gpio, 0xAA00);  // Test Completed
 
   uart_send_str("PASS!\r\n");
 }