From bc592c0f715c49d20175ab17c1c64060daba3c6d Mon Sep 17 00:00:00 2001 From: Michal Sieron Date: Mon, 9 Jan 2023 16:26:07 +0100 Subject: [PATCH] liblitedram/utils: add get_supported_memory Add `get_supported_memory` function that reads SPD to calculate supported memory from the SDRAM. When it's not possible to read from the SPD (no I2C) or there are errors with the readout, it defaults to `SDRAM_PHY_SUPPORTED_MEMORY` defined in `generated/sdram_phy.h` by `litedram/init.py`. Signed-off-by: Michal Sieron --- litex/soc/software/liblitedram/utils.c | 33 ++++++++++++++++++++++++++ litex/soc/software/liblitedram/utils.h | 2 ++ 2 files changed, 35 insertions(+) diff --git a/litex/soc/software/liblitedram/utils.c b/litex/soc/software/liblitedram/utils.c index 7ffa5c749..480565163 100644 --- a/litex/soc/software/liblitedram/utils.c +++ b/litex/soc/software/liblitedram/utils.c @@ -4,6 +4,11 @@ #include #include +#include + +#include + +#include #define KIB 1024 #define MIB (KIB*1024) @@ -26,3 +31,31 @@ void print_progress(const char * header, uint64_t origin, uint64_t size) print_size(size); printf(" \r"); } + +uint64_t sdram_get_supported_memory(void) { +#ifdef CONFIG_HAS_I2C + +#if defined(SDRAM_PHY_DDR3) || defined(SDRAM_PHY_DDR4) + uint8_t buf; + + if (!sdram_read_spd(0x0, 4, &buf, 1, true)) { + printf("Couldn't read SDRAM size from the SPD, defaulting to 256 MB.\n"); + return 256 << 20; + } + + /* minimal supported is 256 Mb */ + uint64_t single_die_capacity = 256 << 20; + single_die_capacity <<= buf & 0x7; + + /* convert from bits to bytes (divide by 8) */ + single_die_capacity >>= 3; + + return SDRAM_PHY_MODULES * single_die_capacity; +#else + return SDRAM_PHY_SUPPORTED_MEMORY; +#endif + +#else /* no CONFIG_HAS_I2C */ + return SDRAM_PHY_SUPPORTED_MEMORY; +#endif /* CONFIG_HAS_I2C */ +} diff --git a/litex/soc/software/liblitedram/utils.h b/litex/soc/software/liblitedram/utils.h index 68235563b..09ff07091 100644 --- a/litex/soc/software/liblitedram/utils.h +++ b/litex/soc/software/liblitedram/utils.h @@ -9,4 +9,6 @@ void print_size(uint64_t size); void print_progress(const char * header, uint64_t origin, uint64_t size); +uint64_t sdram_get_supported_memory(void); + #endif /* __SDRAM_UTILS_H */