Skip to content

Instantly share code, notes, and snippets.

@jernejsk
Created January 8, 2025 20:53
Show Gist options
  • Select an option

  • Save jernejsk/648220b08c73ced183eee4d1197d8756 to your computer and use it in GitHub Desktop.

Select an option

Save jernejsk/648220b08c73ced183eee4d1197d8756 to your computer and use it in GitHub Desktop.
diff --git a/arch/arm/mach-sunxi/dram_sun50i_h616.c b/arch/arm/mach-sunxi/dram_sun50i_h616.c
index 37c139e0eea5..d19812c6e3f1 100644
--- a/arch/arm/mach-sunxi/dram_sun50i_h616.c
+++ b/arch/arm/mach-sunxi/dram_sun50i_h616.c
@@ -1318,31 +1318,72 @@ static void mctl_auto_detect_rank_width(const struct dram_para *para,
panic("This DRAM setup is currently not supported.\n");
}
+static bool compare_mem(ulong offset)
+{
+ unsigned int i;
+ u32 *ptr, val;
+
+ ptr = (u32*)CFG_SYS_SDRAM_BASE;
+ for (i = 0; i < 16; ptr++, i++) {
+ if (i & 1)
+ val = ~(ulong)ptr;
+ else
+ val = (ulong)ptr;
+ if (val != *(ptr + offset / 4))
+ return false;
+ }
+
+ return true;
+}
+
+static void init_mem(void)
+{
+ unsigned int i;
+ u32 *ptr, val;
+
+ ptr = (u32*)CFG_SYS_SDRAM_BASE;
+ for (i = 0; i < 16; ptr++, i++) {
+ if (i & 1)
+ val = ~(ulong)ptr;
+ else
+ val = (ulong)ptr;
+ writel(val, ptr);
+ }
+}
+
static void mctl_auto_detect_dram_size(const struct dram_para *para,
struct dram_config *config)
{
- /* detect row address bits */
- config->cols = 8;
- config->rows = 18;
- mctl_core_init(para, config);
+ unsigned int shift;
- for (config->rows = 13; config->rows < 18; config->rows++) {
- /* 8 banks, 8 bit per byte and 16/32 bit width */
- if (mctl_mem_matches((1 << (config->rows + config->cols +
- 4 + config->bus_full_width))))
- break;
- }
-
- /* detect column address bits */
+ /* max. config for columns, but not rows */
config->cols = 11;
+ config->rows = 13;
mctl_core_init(para, config);
+ init_mem();
+
+ /* detect column address bits */
+ shift = config->bus_full_width + 1;
for (config->cols = 8; config->cols < 11; config->cols++) {
- /* 8 bits per byte and 16/32 bit width */
- if (mctl_mem_matches(1 << (config->cols + 1 +
- config->bus_full_width)))
+ if (compare_mem(1ULL << (config->cols + shift)))
break;
}
+ debug("detected %u columns\n", config->cols);
+
+ /* reconfigure to make sure that all active rows are accessible */
+ config->rows = 17;
+ mctl_core_init(para, config);
+
+ init_mem();
+
+ /* detect row address bits */
+ shift = config->bus_full_width + 1 + 3 + config->cols;
+ for (config->rows = 13; config->rows < 17; config->rows++) {
+ if (compare_mem(1ULL << (config->rows + shift)))
+ break;
+ }
+ debug("detected %u rows\n", config->rows);
}
static unsigned long mctl_calc_size(const struct dram_config *config)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment