ARM: Add Support for Marvell Pantheon Familiy SoCs
[oweals/u-boot.git] / arch / arm / cpu / arm926ejs / pantheon / dram.c
1 /*
2  * (C) Copyright 2011
3  * Marvell Semiconductor <www.marvell.com>
4  * Written-by: Lei Wen <leiwen@marvell.com>,
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22  * MA 02110-1301 USA
23  */
24
25 #include <common.h>
26 #include <asm/arch/pantheon.h>
27
28 DECLARE_GLOBAL_DATA_PTR;
29
30 /*
31  * Pantheon DRAM controller supports upto 8 banks
32  * for chip select 0 and 1
33  */
34
35 /*
36  * DDR Memory Control Registers
37  * Refer Datasheet 4.4
38  */
39 struct panthddr_map_registers {
40         u32     cs;     /* Memory Address Map Register -CS */
41         u32     pad[3];
42 };
43
44 struct panthddr_registers {
45         u8      pad[0x100 - 0x000];
46         struct panthddr_map_registers mmap[2];
47 };
48
49 /*
50  * panth_sdram_base - reads SDRAM Base Address Register
51  */
52 u32 panth_sdram_base(int chip_sel)
53 {
54         struct panthddr_registers *ddr_regs =
55                 (struct panthddr_registers *)PANTHEON_DRAM_BASE;
56         u32 result = 0;
57         u32 CS_valid = 0x01 & readl(&ddr_regs->mmap[chip_sel].cs);
58
59         if (!CS_valid)
60                 return 0;
61
62         result = readl(&ddr_regs->mmap[chip_sel].cs) & 0xFF800000;
63         return result;
64 }
65
66 /*
67  * panth_sdram_size - reads SDRAM size
68  */
69 u32 panth_sdram_size(int chip_sel)
70 {
71         struct panthddr_registers *ddr_regs =
72                 (struct panthddr_registers *)PANTHEON_DRAM_BASE;
73         u32 result = 0;
74         u32 CS_valid = 0x01 & readl(&ddr_regs->mmap[chip_sel].cs);
75
76         if (!CS_valid)
77                 return 0;
78
79         result = readl(&ddr_regs->mmap[chip_sel].cs);
80         result = (result >> 16) & 0xF;
81         if (result < 0x7) {
82                 printf("Unknown DRAM Size\n");
83                 return -1;
84         } else {
85                 return ((0x8 << (result - 0x7)) * 1024 * 1024);
86         }
87 }
88
89 #ifndef CONFIG_SYS_BOARD_DRAM_INIT
90 int dram_init(void)
91 {
92         int i;
93
94         gd->ram_size = 0;
95         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
96                 gd->bd->bi_dram[i].start = panth_sdram_base(i);
97                 gd->bd->bi_dram[i].size = panth_sdram_size(i);
98                 /*
99                  * It is assumed that all memory banks are consecutive
100                  * and without gaps.
101                  * If the gap is found, ram_size will be reported for
102                  * consecutive memory only
103                  */
104                 if (gd->bd->bi_dram[i].start != gd->ram_size)
105                         break;
106
107                 gd->ram_size += gd->bd->bi_dram[i].size;
108
109         }
110
111         for (; i < CONFIG_NR_DRAM_BANKS; i++) {
112                 /*
113                  * If above loop terminated prematurely, we need to set
114                  * remaining banks' start address & size as 0. Otherwise other
115                  * u-boot functions and Linux kernel gets wrong values which
116                  * could result in crash
117                  */
118                 gd->bd->bi_dram[i].start = 0;
119                 gd->bd->bi_dram[i].size = 0;
120         }
121         return 0;
122 }
123
124 /*
125  * If this function is not defined here,
126  * board.c alters dram bank zero configuration defined above.
127  */
128 void dram_init_banksize(void)
129 {
130         dram_init();
131 }
132 #endif /* CONFIG_SYS_BOARD_DRAM_INIT */