4 * Copyright (C) 2007 OpenWrt.org
5 * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 #include <linux/init.h>
25 #include <linux/types.h>
26 #include <linux/kernel.h>
29 #include <asm/bootinfo.h>
30 #include <asm/addrspace.h>
32 #include <adm5120_info.h>
33 #include <adm5120_defs.h>
34 #include <adm5120_switch.h>
35 #include <adm5120_mpmc.h>
38 # define mem_dbg(f, a...) printk(KERN_INFO "mem_detect: " f, ## a)
40 # define mem_dbg(f, a...)
43 unsigned long adm5120_memsize;
45 #define MEM_READL(a) __raw_readl((void __iomem *)(a))
46 #define MEM_WRITEL(a, v) __raw_writel((v), (void __iomem *)(a))
48 static int __init mem_check_pattern(u8 *addr, unsigned long offs)
50 u32 *p1 = (u32 *)addr;
51 u32 *p2 = (u32 *)(addr+offs);
54 /* save original value */
65 mem_dbg("write 0x%08X to 0x%08lX\n", v, (unsigned long)p1);
71 mem_dbg("pattern at 0x%08lX is 0x%08X\n", (unsigned long)p2, u);
73 /* restore original value */
79 static void __init adm5120_detect_memsize(void)
85 memctrl = SW_READ_REG(MEMCTRL);
86 switch (memctrl & MEMCTRL_SDRS_MASK) {
93 case MEMCTRL_SDRS_16M:
101 mem_dbg("checking for %uMB chip in 1st bank\n", maxsize >> 20);
103 /* detect size of the 1st SDRAM bank */
104 p = (u8 *)KSEG1ADDR(0);
105 for (size = 2<<20; size <= (maxsize >> 1); size <<= 1) {
106 if (mem_check_pattern(p, size)) {
107 /* mirrored address */
108 mem_dbg("mirrored data found at offset 0x%08X\n", size);
113 mem_dbg("chip size in 1st bank is %uMB\n", size >> 20);
114 adm5120_memsize = size;
117 /* 2nd bank is not supported */
120 if ((memctrl & MEMCTRL_SDR1_ENABLE) == 0)
121 /* 2nd bank is disabled */
125 * some bootloaders enable 2nd bank, even if the 2nd SDRAM chip
128 mem_dbg("check presence of 2nd bank\n");
130 p = (u8 *)KSEG1ADDR(maxsize+size-4);
131 if (mem_check_pattern(p, 0))
132 adm5120_memsize += size;
134 if (maxsize != size) {
135 /* adjusting MECTRL register */
136 memctrl &= ~(MEMCTRL_SDRS_MASK);
139 memctrl |= MEMCTRL_SDRS_4M;
142 memctrl |= MEMCTRL_SDRS_8M;
145 memctrl |= MEMCTRL_SDRS_16M;
148 memctrl |= MEMCTRL_SDRS_64M;
151 SW_WRITE_REG(MEMCTRL, memctrl);
155 mem_dbg("%dx%uMB memory found\n", (adm5120_memsize == size) ? 1 : 2 ,
159 void __init adm5120_mem_init(void)
161 adm5120_detect_memsize();
162 add_memory_region(0, adm5120_memsize, BOOT_MEM_RAM);