icE1usb fw: Add GPIO access functions

The 32 bit register is split in different fields to speed up
access to the 'in' field. The oe/out fields can't be split because
all writes are 32 bit wide (i.e. no support for byte enable in the
gateware)

Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
Change-Id: Ic25377bb777e5d25939f0e8cfe6b7c6ef8641f6d
diff --git a/firmware/ice40-riscv/icE1usb/misc.c b/firmware/ice40-riscv/icE1usb/misc.c
index bd9198a..50aec43 100644
--- a/firmware/ice40-riscv/icE1usb/misc.c
+++ b/firmware/ice40-riscv/icE1usb/misc.c
@@ -15,7 +15,11 @@
 
 struct misc {
 	uint32_t warmboot;
-	uint32_t gpio;
+	struct {
+		uint16_t oe_out;
+		uint8_t  in;
+		uint8_t  _rsvd;
+	} gpio;
 	uint32_t e1_led;
 	uint32_t _rsvd;
 	struct {
@@ -47,6 +51,35 @@
 
 
 void
+gpio_dir(int n, bool output)
+{
+	uint16_t mask = 256 << n;
+
+	if (output)
+		misc_regs->gpio.oe_out |=  mask;
+	else
+		misc_regs->gpio.oe_out &= ~mask;
+}
+
+void
+gpio_out(int n, bool val)
+{
+	uint16_t mask = 1 << n;
+
+	if (val)
+		misc_regs->gpio.oe_out |=  mask;
+	else
+		misc_regs->gpio.oe_out &= ~mask;
+}
+
+bool
+gpio_in(int n)
+{
+	return (misc_regs->gpio.in & (1 << n)) != 0;
+}
+
+
+void
 e1_led_run(void)
 {
 	misc_regs->e1_led |= 0x100;
diff --git a/firmware/ice40-riscv/icE1usb/misc.h b/firmware/ice40-riscv/icE1usb/misc.h
index b4baef1..76d5a7d 100644
--- a/firmware/ice40-riscv/icE1usb/misc.h
+++ b/firmware/ice40-riscv/icE1usb/misc.h
@@ -26,6 +26,10 @@
 
 void pdm_set(int chan, bool enable, unsigned value, bool normalize);
 
+void gpio_dir(int n, bool output);
+void gpio_out(int n, bool val);
+bool gpio_in(int n);
+
 void e1_led_run(void);
 void e1_led_pause(void);
 void e1_led_set(bool enable, uint8_t cfg);