blob: f6d9b97bb4dcb6df17380c675ecaf96d3afe08ae [file] [log] [blame]
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -04001/*
2 * Functions related to OMAP3 SDRC.
3 *
4 * This file has been created after exctracting and consolidating
5 * the SDRC related content from mem.c and board.c, also created
6 * generic init function (mem_init).
7 *
8 * Copyright (C) 2004-2010
9 * Texas Instruments Incorporated - http://www.ti.com/
10 *
Simon Schwarzee50ca92011-09-14 15:15:37 -040011 * Copyright (C) 2011
12 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
13 *
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -040014 * Author :
15 * Vaibhav Hiremath <hvaibhav@ti.com>
16 *
17 * Original implementation by (mem.c, board.c) :
18 * Sunil Kumar <sunilsaini05@gmail.com>
19 * Shashi Ranjan <shashiranjanmca05@gmail.com>
20 * Manikandan Pillai <mani.pillai@ti.com>
21 *
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License as
24 * published by the Free Software Foundation; either version 2 of
25 * the License, or (at your option) any later version.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
35 * MA 02111-1307 USA
36 */
37
38#include <common.h>
39#include <asm/io.h>
40#include <asm/arch/mem.h>
41#include <asm/arch/sys_proto.h>
42
Nishanth Menonedded0e42010-12-11 11:41:42 -050043DECLARE_GLOBAL_DATA_PTR;
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -040044extern omap3_sysinfo sysinfo;
45
46static struct sdrc *sdrc_base = (struct sdrc *)OMAP34XX_SDRC_BASE;
47
48/*
49 * is_mem_sdr -
50 * - Return 1 if mem type in use is SDR
51 */
52u32 is_mem_sdr(void)
53{
54 if (readl(&sdrc_base->cs[CS0].mr) == SDRC_MR_0_SDR)
55 return 1;
56 return 0;
57}
58
59/*
60 * make_cs1_contiguous -
Tom Rini858df782011-11-18 12:47:59 +000061 * - When we have CS1 populated we want to have it mapped after cs0 to allow
62 * command line mem=xyz use all memory with out discontinuous support
63 * compiled in. We could do it in the ATAG, but there really is two banks...
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -040064 */
65void make_cs1_contiguous(void)
66{
67 u32 size, a_add_low, a_add_high;
68
69 size = get_sdr_cs_size(CS0);
70 size >>= 25; /* divide by 32 MiB to find size to offset CS1 */
71 a_add_high = (size & 3) << 8; /* set up low field */
72 a_add_low = (size & 0x3C) >> 2; /* set up high field */
73 writel((a_add_high | a_add_low), &sdrc_base->cs_cfg);
74
75}
76
77
78/*
79 * get_sdr_cs_size -
80 * - Get size of chip select 0/1
81 */
82u32 get_sdr_cs_size(u32 cs)
83{
84 u32 size;
85
86 /* get ram size field */
87 size = readl(&sdrc_base->cs[cs].mcfg) >> 8;
88 size &= 0x3FF; /* remove unwanted bits */
89 size <<= 21; /* multiply by 2 MiB to find size in MB */
90 return size;
91}
92
93/*
94 * get_sdr_cs_offset -
95 * - Get offset of cs from cs0 start
96 */
97u32 get_sdr_cs_offset(u32 cs)
98{
99 u32 offset;
100
101 if (!cs)
102 return 0;
103
104 offset = readl(&sdrc_base->cs_cfg);
Tom Rinid639cf52012-01-18 08:28:50 +0000105 offset = (offset & 15) << 27 | (offset & 0x300) << 17;
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -0400106
107 return offset;
108}
109
110/*
Tom Rini4633d8e2011-11-18 12:48:00 +0000111 * write_sdrc_timings -
112 * - Takes CS and associated timings and initalize SDRAM
113 * - Test CS to make sure it's OK for use
114 */
115static void write_sdrc_timings(u32 cs, struct sdrc_actim *sdrc_actim_base,
116 u32 mcfg, u32 ctrla, u32 ctrlb, u32 rfr_ctrl, u32 mr)
117{
118 /* Setup timings we got from the board. */
119 writel(mcfg, &sdrc_base->cs[cs].mcfg);
120 writel(ctrla, &sdrc_actim_base->ctrla);
121 writel(ctrlb, &sdrc_actim_base->ctrlb);
122 writel(rfr_ctrl, &sdrc_base->cs[cs].rfr_ctrl);
123 writel(CMD_NOP, &sdrc_base->cs[cs].manual);
124 writel(CMD_PRECHARGE, &sdrc_base->cs[cs].manual);
125 writel(CMD_AUTOREFRESH, &sdrc_base->cs[cs].manual);
126 writel(CMD_AUTOREFRESH, &sdrc_base->cs[cs].manual);
127 writel(mr, &sdrc_base->cs[cs].mr);
128
129 /*
130 * Test ram in this bank
131 * Disable if bad or not present
132 */
133 if (!mem_ok(cs))
134 writel(0, &sdrc_base->cs[cs].mcfg);
135}
136
137/*
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -0400138 * do_sdrc_init -
Tom Rini4633d8e2011-11-18 12:48:00 +0000139 * - Code called once in C-Stack only context for CS0 and with early being
140 * true and a possible 2nd time depending on memory configuration from
141 * stack+global context.
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -0400142 */
143void do_sdrc_init(u32 cs, u32 early)
144{
Steve Sakomana7561cc2010-08-19 20:09:57 -0700145 struct sdrc_actim *sdrc_actim_base0, *sdrc_actim_base1;
Tom Rini4633d8e2011-11-18 12:48:00 +0000146 u32 mcfg, ctrla, ctrlb, rfr_ctrl, mr;
147
148 sdrc_actim_base0 = (struct sdrc_actim *)SDRC_ACTIM_CTRL0_BASE;
149 sdrc_actim_base1 = (struct sdrc_actim *)SDRC_ACTIM_CTRL1_BASE;
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -0400150
Tom Rini05800b92011-11-18 12:48:06 +0000151 /*
152 * When called in the early context this may be SPL and we will
153 * need to set all of the timings. This ends up being board
154 * specific so we call a helper function to take care of this
155 * for us. Otherwise, to be safe, we need to copy the settings
156 * from the first bank to the second. We will setup CS0,
157 * then set cs_cfg to the appropriate value then try and
158 * setup CS1.
159 */
160#ifdef CONFIG_SPL_BUILD
161 get_board_mem_timings(&mcfg, &ctrla, &ctrlb, &rfr_ctrl, &mr);
162#endif
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -0400163 if (early) {
164 /* reset sdrc controller */
165 writel(SOFTRESET, &sdrc_base->sysconfig);
166 wait_on_value(RESETDONE, RESETDONE, &sdrc_base->status,
167 12000000);
168 writel(0, &sdrc_base->sysconfig);
169
170 /* setup sdrc to ball mux */
171 writel(SDRC_SHARING, &sdrc_base->sharing);
172
Tom Rini4633d8e2011-11-18 12:48:00 +0000173 /* Disable Power Down of CKE because of 1 CKE on combo part */
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -0400174 writel(WAKEUPPROC | SRFRONRESET | PAGEPOLICY_HIGH,
175 &sdrc_base->power);
176
177 writel(ENADLL | DLLPHASE_90, &sdrc_base->dlla_ctrl);
178 sdelay(0x20000);
Simon Schwarzee50ca92011-09-14 15:15:37 -0400179#ifdef CONFIG_SPL_BUILD
Tom Rini4633d8e2011-11-18 12:48:00 +0000180 write_sdrc_timings(CS0, sdrc_actim_base0, mcfg, ctrla, ctrlb,
181 rfr_ctrl, mr);
Tom Rini05800b92011-11-18 12:48:06 +0000182 make_cs1_contiguous();
Tom Rini9ad40d02012-03-16 07:55:32 +0000183 write_sdrc_timings(CS1, sdrc_actim_base1, mcfg, ctrla, ctrlb,
Tom Rini05800b92011-11-18 12:48:06 +0000184 rfr_ctrl, mr);
Tom Rini4633d8e2011-11-18 12:48:00 +0000185#endif
Simon Schwarzee50ca92011-09-14 15:15:37 -0400186
Simon Schwarzee50ca92011-09-14 15:15:37 -0400187 }
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -0400188
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -0400189 /*
Tom Rini4633d8e2011-11-18 12:48:00 +0000190 * If we aren't using SPL we have been loaded by some
191 * other means which may not have correctly initialized
192 * both CS0 and CS1 (such as some older versions of x-loader)
193 * so we may be asked now to setup CS1.
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -0400194 */
Simon Schwarzee50ca92011-09-14 15:15:37 -0400195 if (cs == CS1) {
Tom Rini4633d8e2011-11-18 12:48:00 +0000196 mcfg = readl(&sdrc_base->cs[CS0].mcfg),
197 rfr_ctrl = readl(&sdrc_base->cs[CS0].rfr_ctrl);
198 ctrla = readl(&sdrc_actim_base0->ctrla),
199 ctrlb = readl(&sdrc_actim_base0->ctrlb);
200 mr = readl(&sdrc_base->cs[CS0].mr);
201 write_sdrc_timings(cs, sdrc_actim_base1, mcfg, ctrla, ctrlb,
202 rfr_ctrl, mr);
Heiko Schochera1e1aec2010-11-04 16:05:25 -0400203
Steve Sakomana7561cc2010-08-19 20:09:57 -0700204 }
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -0400205}
206
207/*
208 * dram_init -
209 * - Sets uboots idea of sdram size
210 */
211int dram_init(void)
212{
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -0400213 unsigned int size0 = 0, size1 = 0;
214
215 size0 = get_sdr_cs_size(CS0);
216 /*
Tom Rini858df782011-11-18 12:47:59 +0000217 * We always need to have cs_cfg point at where the second
218 * bank would be, if present. Failure to do so can lead to
219 * strange situations where memory isn't detected and
220 * configured correctly. CS0 will already have been setup
221 * at this point.
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -0400222 */
Tom Rini858df782011-11-18 12:47:59 +0000223 make_cs1_contiguous();
224 do_sdrc_init(CS1, NOT_EARLY);
225 size1 = get_sdr_cs_size(CS1);
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -0400226
Heiko Schocher56d0a4d2010-09-17 13:10:41 +0200227 gd->ram_size = size0 + size1;
228
229 return 0;
230}
231
232void dram_init_banksize (void)
233{
Heiko Schocher56d0a4d2010-09-17 13:10:41 +0200234 unsigned int size0 = 0, size1 = 0;
235
236 size0 = get_sdr_cs_size(CS0);
237 size1 = get_sdr_cs_size(CS1);
238
239 gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
240 gd->bd->bi_dram[0].size = size0;
241 gd->bd->bi_dram[1].start = PHYS_SDRAM_1 + get_sdr_cs_offset(CS1);
242 gd->bd->bi_dram[1].size = size1;
243}
Vaibhav Hiremath558d23d2010-06-07 15:20:34 -0400244
245/*
246 * mem_init -
247 * - Init the sdrc chip,
248 * - Selects CS0 and CS1,
249 */
250void mem_init(void)
251{
252 /* only init up first bank here */
253 do_sdrc_init(CS0, EARLY_INIT);
254}