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 <msieron@antmicro.com>
This commit is contained in:
Michal Sieron 2023-01-09 16:26:07 +01:00
parent 08d439f021
commit bc592c0f71
2 changed files with 35 additions and 0 deletions

View File

@ -4,6 +4,11 @@
#include <stdio.h>
#include <liblitedram/utils.h>
#include <liblitedram/sdram_spd.h>
#include <libbase/i2c.h>
#include <generated/sdram_phy.h>
#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 */
}

View File

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