2 * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
10 #include <linux/init.h>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
15 #include <asm/bootinfo.h>
16 #include <asm/addrspace.h>
18 #include <asm/mach-adm5120/adm5120_info.h>
19 #include <asm/mach-adm5120/adm5120_defs.h>
20 #include <asm/mach-adm5120/adm5120_switch.h>
21 #include <asm/mach-adm5120/adm5120_mpmc.h>
24 # define mem_dbg(f, a...) printk(KERN_INFO "mem_detect: " f, ## a)
26 # define mem_dbg(f, a...)
29 unsigned long adm5120_memsize;
31 #define MEM_READL(a) __raw_readl((void __iomem *)(a))
32 #define MEM_WRITEL(a, v) __raw_writel((v), (void __iomem *)(a))
34 static int __init mem_check_pattern(u8 *addr, unsigned long offs)
36 u32 *p1 = (u32 *)addr;
37 u32 *p2 = (u32 *)(addr+offs);
40 /* save original value */
51 mem_dbg("write 0x%08X to 0x%08lX\n", v, (unsigned long)p1);
57 mem_dbg("pattern at 0x%08lX is 0x%08X\n", (unsigned long)p2, u);
59 /* restore original value */
65 static void __init adm5120_detect_memsize(void)
71 memctrl = SW_READ_REG(SWITCH_REG_MEMCTRL);
72 switch (memctrl & MEMCTRL_SDRS_MASK) {
79 case MEMCTRL_SDRS_16M:
87 mem_dbg("checking for %uMB chip in 1st bank\n", maxsize >> 20);
89 /* detect size of the 1st SDRAM bank */
90 p = (u8 *)KSEG1ADDR(0);
91 for (size = 2<<20; size <= (maxsize >> 1); size <<= 1) {
92 if (mem_check_pattern(p, size)) {
93 /* mirrored address */
94 mem_dbg("mirrored data found at offset 0x%08X\n", size);
99 mem_dbg("chip size in 1st bank is %uMB\n", size >> 20);
100 adm5120_memsize = size;
103 /* 2nd bank is not supported */
106 if ((memctrl & MEMCTRL_SDR1_ENABLE) == 0)
107 /* 2nd bank is disabled */
111 * some bootloaders enable 2nd bank, even if the 2nd SDRAM chip
114 mem_dbg("check presence of 2nd bank\n");
116 p = (u8 *)KSEG1ADDR(maxsize+size-4);
117 if (mem_check_pattern(p, 0))
118 adm5120_memsize += size;
120 if (maxsize != size) {
121 /* adjusting MECTRL register */
122 memctrl &= ~(MEMCTRL_SDRS_MASK);
125 memctrl |= MEMCTRL_SDRS_4M;
128 memctrl |= MEMCTRL_SDRS_8M;
131 memctrl |= MEMCTRL_SDRS_16M;
134 memctrl |= MEMCTRL_SDRS_64M;
137 SW_WRITE_REG(SWITCH_REG_MEMCTRL, memctrl);
141 mem_dbg("%dx%uMB memory found\n", (adm5120_memsize == size) ? 1 : 2 ,
145 void __init adm5120_mem_init(void)
147 adm5120_detect_memsize();
148 add_memory_region(0, adm5120_memsize, BOOT_MEM_RAM);