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 */