From adad27da61ca0b875d48f4e06bd36c36bb01d415 Mon Sep 17 00:00:00 2001
From: emilia <emilia@vegaa.systems>
Date: Sun, 27 Aug 2023 22:29:25 +0000
Subject: kernel: mmio: Add MMIO read helpers

git-svn-id: https://svn.vegaa.systems/svn/vega-Vega/trunk@42 a8a8aea2-181d-ee11-89e8-15fd0e089fc4
---
 sys/include/sys/mmio.h | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/sys/include/sys/mmio.h b/sys/include/sys/mmio.h
index 8e3db52..334b31c 100644
--- a/sys/include/sys/mmio.h
+++ b/sys/include/sys/mmio.h
@@ -61,6 +61,24 @@
         *(volatile TYPE *)tmp = val;                    \
     }
 
+/*
+ * mmio_read<n> - Does the same as mmio_write<n> but for reading
+ *
+ * @addr: Address to read from.
+ */
+#define _MMIO_READ_TYPE(TYPE, SUFFIX)                   \
+    static inline TYPE                                  \
+    mmio_read##SUFFIX(void *addr, TYPE val)             \
+    {                                                   \
+        uintptr_t tmp;                                  \
+                                                        \
+        tmp = (uintptr_t)addr;                          \
+        if (tmp < VM_HIGHER_HALF) {                     \
+            tmp += VM_HIGHER_HALF;                      \
+        }                                               \
+        return *(volatile TYPE *)tmp;                   \
+    }
+
 /*
  * To write to an MMIO address of, for example,
  * 8 bits, use mmio_write8(addr, val)
@@ -73,4 +91,16 @@ _MMIO_WRITE_TYPE(uint64_t, 64)
 #endif
 __extension__
 
+/*
+ * To read from an MMIO address of, for example,
+ * 8 bits, use mmio_read8(addr)
+ */
+_MMIO_READ_TYPE(uint8_t, 8)
+_MMIO_READ_TYPE(uint16_t, 16)
+_MMIO_READ_TYPE(uint32_t, 32)
+#if __SIZEOF_SIZE_T__ == 8
+_MMIO_READ_TYPE(uint64_t, 64)
+#endif
+__extension__
+
 #endif  /* !_SYS_MMIO_H_ */
-- 
cgit v1.2.3