csr: use readl()/writel() accessors for accessing mmio
authorSean Cross <sean@xobs.io>
Sat, 22 Sep 2018 14:30:05 +0000 (16:30 +0200)
committerSean Cross <sean@xobs.io>
Sat, 22 Sep 2018 14:54:46 +0000 (16:54 +0200)
Instead of directly dereferencing pointers, use variants on readl()/writel().
This way we can replace these functions with others for remote access
when writing drivers and code outside of the litex environment.

Signed-off-by: Sean Cross <sean@xobs.io>
litex/soc/integration/cpu_interface.py
litex/soc/software/include/hw/common.h

index fb956070640160105128f027ca47044d21c42aa4..e431f363f1996b3695e12717532d149a50285c47 100644 (file)
@@ -123,12 +123,12 @@ def _get_rw_functions_c(reg_name, reg_base, nwords, busword, read_only, with_acc
     if with_access_functions:
         r += "static inline "+ctype+" "+reg_name+"_read(void) {\n"
         if size > 1:
-            r += "\t"+ctype+" r = MMPTR("+hex(reg_base)+");\n"
+            r += "\t"+ctype+" r = csr_readl("+hex(reg_base)+");\n"
             for byte in range(1, nwords):
-                r += "\tr <<= "+str(busword)+";\n\tr |= MMPTR("+hex(reg_base+4*byte)+");\n"
+                r += "\tr <<= "+str(busword)+";\n\tr |= csr_readl("+hex(reg_base+4*byte)+");\n"
             r += "\treturn r;\n}\n"
         else:
-            r += "\treturn MMPTR("+hex(reg_base)+");\n}\n"
+            r += "\treturn csr_readl("+hex(reg_base)+");\n}\n"
 
         if not read_only:
             r += "static inline void "+reg_name+"_write("+ctype+" value) {\n"
@@ -138,7 +138,7 @@ def _get_rw_functions_c(reg_name, reg_base, nwords, busword, read_only, with_acc
                     value_shifted = "value >> "+str(shift)
                 else:
                     value_shifted = "value"
-                r += "\tMMPTR("+hex(reg_base+4*word)+") = "+value_shifted+";\n"
+                r += "\tcsr_writel("+value_shifted+", "+hex(reg_base+4*word)+");\n"
             r += "}\n"
     return r
 
@@ -146,6 +146,7 @@ def _get_rw_functions_c(reg_name, reg_base, nwords, busword, read_only, with_acc
 def get_csr_header(regions, constants, with_access_functions=True, with_shadow_base=True, shadow_base=0x80000000):
     r = "#ifndef __GENERATED_CSR_H\n#define __GENERATED_CSR_H\n"
     if with_access_functions:
+        r += "#include <stdint.h>\n"
         r += "#include <hw/common.h>\n"
     for name, origin, busword, obj in regions:
         if not with_shadow_base:
index e5f9b51eef02e3bebbf49fa69af6a7bea827992e..abef55d65be08d5ab5ba40c7522aeaf76ee72334 100644 (file)
@@ -1,10 +1,42 @@
 #ifndef __HW_COMMON_H
 #define __HW_COMMON_H
 
+#include <stdint.h>
+
 #ifdef __ASSEMBLER__
 #define MMPTR(x) x
-#else
+#else /* ! __ASSEMBLER__ */
 #define MMPTR(x) (*((volatile unsigned int *)(x)))
-#endif
 
-#endif
+static inline void csr_writeb(uint8_t value, uint32_t addr)
+{
+       *((volatile uint8_t *)addr) = value;
+}
+
+static inline uint8_t csr_readb(uint32_t addr)
+{
+       return *(volatile uint8_t *)addr;
+}
+
+static inline void csr_writew(uint16_t value, uint32_t addr)
+{
+       *((volatile uint16_t *)addr) = value;
+}
+
+static inline uint16_t csr_readw(uint32_t addr)
+{
+       return *(volatile uint16_t *)addr;
+}
+
+static inline void csr_writel(uint32_t value, uint32_t addr)
+{
+       *((volatile uint32_t *)addr) = value;
+}
+
+static inline uint32_t csr_readl(uint32_t addr)
+{
+       return *(volatile uint32_t *)addr;
+}
+#endif /* ! __ASSEMBLER__ */
+
+#endif /* __HW_COMMON_H */