|
5 | 5 |
|
6 | 6 | #include <arch/byteorder.h>
|
7 | 7 | #include <stdint.h>
|
| 8 | +#include <string.h> |
8 | 9 | #include <swab.h>
|
9 | 10 |
|
10 | 11 | #if defined(__LITTLE_ENDIAN)
|
|
67 | 68 | #define clrsetbits_le16(addr, clear, set) __clrsetbits(le, 16, addr, clear, set)
|
68 | 69 | #define clrsetbits_be16(addr, clear, set) __clrsetbits(be, 16, addr, clear, set)
|
69 | 70 |
|
70 |
| -/* be16dec/be32dec/be64dec/le16dec/le32dec/le64dec family of functions. */ |
| 71 | +/* |
| 72 | + * be16dec/be32dec/be64dec/le16dec/le32dec/le64dec family of functions. |
| 73 | + * RISC-V doesn't support misaligned access so decode it byte by byte. |
| 74 | + */ |
71 | 75 | #define DEFINE_ENDIAN_DEC(endian, width) \
|
72 | 76 | static inline uint##width##_t endian##width##dec(const void *p) \
|
73 | 77 | { \
|
74 |
| - return endian##width##_to_cpu(*(uint##width##_t *)p); \ |
| 78 | + if (ENV_RISCV) { \ |
| 79 | + uint##width##_t val; \ |
| 80 | + memcpy(&val, p, sizeof(val)); \ |
| 81 | + return endian##width##_to_cpu(val); \ |
| 82 | + } else { \ |
| 83 | + return endian##width##_to_cpu(*(uint##width##_t *)p); \ |
| 84 | + } \ |
75 | 85 | }
|
| 86 | + |
76 | 87 | DEFINE_ENDIAN_DEC(be, 16)
|
77 | 88 | DEFINE_ENDIAN_DEC(be, 32)
|
78 | 89 | DEFINE_ENDIAN_DEC(be, 64)
|
79 | 90 | DEFINE_ENDIAN_DEC(le, 16)
|
80 | 91 | DEFINE_ENDIAN_DEC(le, 32)
|
81 | 92 | DEFINE_ENDIAN_DEC(le, 64)
|
82 | 93 |
|
83 |
| -/* be16enc/be32enc/be64enc/le16enc/le32enc/le64enc family of functions. */ |
| 94 | +/* |
| 95 | + * be16enc/be32enc/be64enc/le16enc/le32enc/le64enc family of functions. |
| 96 | + * RISC-V doesn't support misaligned access so encode it byte by byte. |
| 97 | + */ |
84 | 98 | #define DEFINE_ENDIAN_ENC(endian, width) \
|
85 | 99 | static inline void endian##width##enc(void *p, uint##width##_t u) \
|
86 | 100 | { \
|
87 |
| - *(uint##width##_t *)p = cpu_to_##endian##width(u); \ |
| 101 | + if (ENV_RISCV) { \ |
| 102 | + uint##width##_t val = cpu_to_##endian##width(u); \ |
| 103 | + memcpy(p, &val, sizeof(val)); \ |
| 104 | + } else { \ |
| 105 | + *(uint##width##_t *)p = cpu_to_##endian##width(u); \ |
| 106 | + } \ |
88 | 107 | }
|
| 108 | + |
89 | 109 | DEFINE_ENDIAN_ENC(be, 16)
|
90 | 110 | DEFINE_ENDIAN_ENC(be, 32)
|
91 | 111 | DEFINE_ENDIAN_ENC(be, 64)
|
|
0 commit comments