Skip to content

Commit 5098aaa

Browse files
michalsieroncarlescufi
authored andcommitted
hwinfo: hwinfo_litex: Make compatible with both 8 and 32-bit CSRs
LiteX CSRs can only be accessed on addresses aligned to 4 bytes. That's why in 32-bit CSRs case there is bit shifting needed. Signed-off-by: Michal Sieron <[email protected]>
1 parent 2485c02 commit 5098aaa

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

drivers/hwinfo/hwinfo_litex.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@
1414

1515
ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length)
1616
{
17-
uint32_t volatile *ptr = (uint32_t volatile *)(DT_INST_REG_ADDR(0));
18-
ssize_t end = MIN(length, (DT_INST_REG_SIZE(0) / sizeof(uint32_t)));
19-
17+
uint32_t addr = DT_INST_REG_ADDR(0);
18+
ssize_t end = MIN(length, DT_INST_REG_ADDR(0) / 4 *
19+
CONFIG_LITEX_CSR_DATA_WIDTH / 8);
2020
for (int i = 0; i < end; i++) {
21-
/* In LiteX even though registers are 32-bit wide, each one
22-
contains meaningful data only in the lowest 8 bits */
23-
buffer[i] = (uint8_t)(ptr[i] & 0xff);
21+
#if CONFIG_LITEX_CSR_DATA_WIDTH == 8
22+
buffer[i] = litex_read8(addr);
23+
addr += 4;
24+
#elif CONFIG_LITEX_CSR_DATA_WIDTH == 32
25+
buffer[i] = (uint8_t)(litex_read32(addr) >> (addr % 4 * 8));
26+
addr += 1;
27+
#else
28+
#error Unsupported CSR data width
29+
#endif
2430
}
25-
2631
return end;
2732
}

0 commit comments

Comments
 (0)