Guest User

Untitled

a guest
May 13th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.72 KB | None | 0 0
  1. --- a/litex/soc/software/include/hw/common.h
  2. +++ b/litex/soc/software/include/hw/common.h
  3. @@ -34,9 +34,6 @@
  4.   * #endif
  5.   */
  6.  
  7. -/* CSR data width (subreg. width) in bytes, for direct comparson to sizeof() */
  8. -#define CSR_DW_BYTES (CONFIG_CSR_DATA_WIDTH/8)
  9. -
  10.  /* CSR subregisters (a.k.a. "simple CSRs") are embedded inside native CPU-word
  11.   * aligned locations: */
  12.  #define MMPTR(a) (*((volatile unsigned long *)(a)))
  13. @@ -51,6 +48,20 @@ static inline unsigned long csr_read_simple(unsigned long a)
  14.         return MMPTR(a);
  15.  }
  16.  
  17. +#endif /* ! __ASSEMBLER__ */
  18. +#else
  19. +
  20. +//extern void csr_write_simple(unsigned long v, unsigned long a);
  21. +//extern unsigned long csr_read_simple(unsigned long a);
  22. +
  23. +#endif /* ! CSR_ACCESSORS_DEFINED */
  24. +
  25. +/* CSR data width (subreg. width) in bytes, for direct comparson to sizeof() */
  26. +#define CSR_DW_BYTES     (CONFIG_CSR_DATA_WIDTH/8)
  27. +#define CSR_OFFSET_BYTES (CONFIG_CSR_ALIGNMENT/8)
  28. +
  29. +#ifndef __ASSEMBLER__
  30. +
  31.  /* Number of subregs required for various total byte sizes, by subreg width:
  32.   * NOTE: 1, 2, 4, and 8 bytes represent uint[8|16|32|64]_t C types; However,
  33.   *       CSRs of intermediate byte sizes (24, 40, 48, and 56) are NOT padded
  34. @@ -70,64 +81,67 @@ static inline int num_subregs(int csr_bytes)
  35.  }
  36.  
  37.  /* Read a CSR of size 'csr_bytes' located at address 'a'. */
  38. -static inline uint64_t _csr_rd(volatile unsigned long *a, int csr_bytes)
  39. +static inline uint64_t _csr_rd(unsigned long a, int csr_bytes)
  40.  {
  41. -       uint64_t r = a[0];
  42. +       uint64_t r = csr_read_simple(a);
  43.         for (int i = 1; i < num_subregs(csr_bytes); i++) {
  44.                 r <<= CONFIG_CSR_DATA_WIDTH;
  45. -               r |= a[i];
  46. +               a += CSR_OFFSET_BYTES;
  47. +               r |= csr_read_simple(a);
  48.         }
  49.         return r;
  50.  }
  51.  
  52.  /* Write value 'v' to a CSR of size 'csr_bytes' located at address 'a'. */
  53. -static inline void _csr_wr(volatile unsigned long *a, uint64_t v, int csr_bytes)
  54. +static inline void _csr_wr(unsigned long a, uint64_t v, int csr_bytes)
  55.  {
  56.         int ns = num_subregs(csr_bytes);
  57. -       for (int i = 0; i < ns; i++)
  58. -               a[i] = v >> (CONFIG_CSR_DATA_WIDTH * (ns - 1 - i));
  59. +       for (int i = 0; i < ns; i++) {
  60. +               csr_write_simple(v >> (CONFIG_CSR_DATA_WIDTH * (ns - 1 - i)), a);
  61. +               a += CSR_OFFSET_BYTES;
  62. +       }
  63.  }
  64.  
  65.  // FIXME: - should we provide 24, 40, 48, and 56 bit csr_[rd|wr] methods?
  66.  
  67.  static inline uint8_t csr_rd_uint8(unsigned long a)
  68.  {
  69. -       return _csr_rd((volatile unsigned long *)a, sizeof(uint8_t));
  70. +       return _csr_rd(a, sizeof(uint8_t));
  71.  }
  72.  
  73.  static inline void csr_wr_uint8(uint8_t v, unsigned long a)
  74.  {
  75. -       _csr_wr((volatile unsigned long *)a, v, sizeof(uint8_t));
  76. +       _csr_wr(a, v, sizeof(uint8_t));
  77.  }
  78.  
  79.  static inline uint16_t csr_rd_uint16(unsigned long a)
  80.  {
  81. -       return _csr_rd((volatile unsigned long *)a, sizeof(uint16_t));
  82. +       return _csr_rd(a, sizeof(uint16_t));
  83.  }
  84.  
  85.  static inline void csr_wr_uint16(uint16_t v, unsigned long a)
  86.  {
  87. -       _csr_wr((volatile unsigned long *)a, v, sizeof(uint16_t));
  88. +       _csr_wr(a, v, sizeof(uint16_t));
  89.  }
  90.  
  91.  static inline uint32_t csr_rd_uint32(unsigned long a)
  92.  {
  93. -       return _csr_rd((volatile unsigned long *)a, sizeof(uint32_t));
  94. +       return _csr_rd(a, sizeof(uint32_t));
  95.  }
  96.  
  97.  static inline void csr_wr_uint32(uint32_t v, unsigned long a)
  98.  {
  99. -       _csr_wr((volatile unsigned long *)a, v, sizeof(uint32_t));
  100. +       _csr_wr(a, v, sizeof(uint32_t));
  101.  }
  102.  
  103.  static inline uint64_t csr_rd_uint64(unsigned long a)
  104.  {
  105. -       return _csr_rd((volatile unsigned long *)a, sizeof(uint64_t));
  106. +       return _csr_rd(a, sizeof(uint64_t));
  107.  }
  108.  
  109.  static inline void csr_wr_uint64(uint64_t v, unsigned long a)
  110.  {
  111. -       _csr_wr((volatile unsigned long *)a, v, sizeof(uint64_t));
  112. +       _csr_wr(a, v, sizeof(uint64_t));
  113.  }
  114.  
  115.  /* Read a CSR located at address 'a' into an array 'buf' of 'cnt' elements.
  116. @@ -145,25 +159,25 @@ static inline void csr_wr_uint64(uint64_t v, unsigned long a)
  117.  #define _csr_rd_buf(a, buf, cnt) \
  118.  { \
  119.         int i, j, nsubs, n_sub_elem; \
  120. -       volatile unsigned long *addr = (volatile unsigned long *)(a); \
  121.         uint64_t r; \
  122.         if (sizeof(buf[0]) >= CSR_DW_BYTES) { \
  123.                 /* one or more subregisters per element */ \
  124.                 for (i = 0; i < cnt; i++) { \
  125. -                       buf[i] = _csr_rd(addr, sizeof(buf[0])); \
  126. -                       addr += num_subregs(sizeof(buf[0])); \
  127. +                       buf[i] = _csr_rd(a, sizeof(buf[0])); \
  128. +                       a += CSR_OFFSET_BYTES * num_subregs(sizeof(buf[0])); \
  129.                 } \
  130.         } else { \
  131.                 /* multiple elements per subregister (2, 4, or 8) */ \
  132.                 nsubs = num_subregs(sizeof(buf[0]) * cnt); \
  133.                 n_sub_elem = CSR_DW_BYTES / sizeof(buf[0]); \
  134.                 for (i = 0; i < nsubs; i++) { \
  135. -                       r = addr[i]; \
  136. +                       r = csr_read_simple(a);         \
  137.                         for (j = n_sub_elem - 1; j >= 0; j--) { \
  138.                                 if (i * n_sub_elem + j < cnt) \
  139.                                         buf[i * n_sub_elem + j] = r; \
  140.                                 r >>= sizeof(buf[0]) * 8; \
  141.                         } \
  142. +                       a += CSR_OFFSET_BYTES;  \
  143.                 } \
  144.         } \
  145.  }
  146. @@ -176,13 +190,12 @@ static inline void csr_wr_uint64(uint64_t v, unsigned long a)
  147.  #define _csr_wr_buf(a, buf, cnt) \
  148.  { \
  149.         int i, j, nsubs, n_sub_elem; \
  150. -       volatile unsigned long *addr = (volatile unsigned long *)(a); \
  151.         uint64_t v; \
  152.         if (sizeof(buf[0]) >= CSR_DW_BYTES) { \
  153.                 /* one or more subregisters per element */ \
  154.                 for (i = 0; i < cnt; i++) { \
  155. -                       _csr_wr(addr, buf[i], sizeof(buf[0])); \
  156. -                       addr += num_subregs(sizeof(buf[0])); \
  157. +                       _csr_wr(a, buf[i], sizeof(buf[0])); \
  158. +                       a += CSR_OFFSET_BYTES * num_subregs(sizeof(buf[0])); \
  159.                 } \
  160.         } else { \
  161.                 /* multiple elements per subregister (2, 4, or 8) */ \
  162. @@ -196,7 +209,8 @@ static inline void csr_wr_uint64(uint64_t v, unsigned long a)
  163.                                 v <<= sizeof(buf[0]) * 8; \
  164.                                 v |= buf[i * n_sub_elem + j]; \
  165.                         } \
  166. -                       addr[i] = v; \
  167. +                       csr_write_simple(v, a); \
  168. +                       a += CSR_OFFSET_BYTES;  \
  169.                 } \
  170.         } \
  171.  }
  172. @@ -252,6 +266,4 @@ static inline void csr_wr_buf_uint64(unsigned long a,
  173.  
  174.  #endif /* ! __ASSEMBLER__ */
  175.  
  176. -#endif /* ! CSR_ACCESSORS_DEFINED */
  177. -
  178.  #endif /* __HW_COMMON_H */
Advertisement
Add Comment
Please, Sign In to add comment