blob: 7a291318ab0722e67b6a4c1624c9de260a9e5855 [file] [log] [blame]
Vaibhav Hiremathcae377b2010-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 Schwarzb88e4252011-09-14 15:15:37 -040011 * Copyright (C) 2011
12 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
13 *
Vaibhav Hiremathcae377b2010-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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020022 * SPDX-License-Identifier: GPL-2.0+
Vaibhav Hiremathcae377b2010-06-07 15:20:34 -040023 */
24
25#include <common.h>
26#include <asm/io.h>
27#include <asm/arch/mem.h>
28#include <asm/arch/sys_proto.h>
29
Nishanth Menonee3894c2010-12-11 11:41:42 -050030DECLARE_GLOBAL_DATA_PTR;
Vaibhav Hiremathcae377b2010-06-07 15:20:34 -040031extern omap3_sysinfo sysinfo;
32
33static struct sdrc *sdrc_base = (struct sdrc *)OMAP34XX_SDRC_BASE;
34
35/*
36 * is_mem_sdr -
37 * - Return 1 if mem type in use is SDR
38 */
39u32 is_mem_sdr(void)
40{
41 if (readl(&sdrc_base->cs[CS0].mr) == SDRC_MR_0_SDR)
42 return 1;
43 return 0;
44}
45
46/*
47 * make_cs1_contiguous -
Tom Rini5f862b72011-11-18 12:47:59 +000048 * - When we have CS1 populated we want to have it mapped after cs0 to allow
49 * command line mem=xyz use all memory with out discontinuous support
50 * compiled in. We could do it in the ATAG, but there really is two banks...
Vaibhav Hiremathcae377b2010-06-07 15:20:34 -040051 */
52void make_cs1_contiguous(void)
53{
54 u32 size, a_add_low, a_add_high;
55
56 size = get_sdr_cs_size(CS0);
57 size >>= 25; /* divide by 32 MiB to find size to offset CS1 */
58 a_add_high = (size & 3) << 8; /* set up low field */
59 a_add_low = (size & 0x3C) >> 2; /* set up high field */
60 writel((a_add_high | a_add_low), &sdrc_base->cs_cfg);
61
62}
63
64
65/*
66 * get_sdr_cs_size -
67 * - Get size of chip select 0/1
68 */
69u32 get_sdr_cs_size(u32 cs)
70{
71 u32 size;
72
73 /* get ram size field */
74 size = readl(&sdrc_base->cs[cs].mcfg) >> 8;
75 size &= 0x3FF; /* remove unwanted bits */
76 size <<= 21; /* multiply by 2 MiB to find size in MB */
77 return size;
78}
79
80/*
81 * get_sdr_cs_offset -
82 * - Get offset of cs from cs0 start
83 */
84u32 get_sdr_cs_offset(u32 cs)
85{
86 u32 offset;
87
88 if (!cs)
89 return 0;
90
91 offset = readl(&sdrc_base->cs_cfg);
Tom Rini0ae05652012-01-18 08:28:50 +000092 offset = (offset & 15) << 27 | (offset & 0x300) << 17;
Vaibhav Hiremathcae377b2010-06-07 15:20:34 -040093
94 return offset;
95}
96
97/*
Tom Rini2a04e852011-11-18 12:48:00 +000098 * write_sdrc_timings -
99 * - Takes CS and associated timings and initalize SDRAM
100 * - Test CS to make sure it's OK for use
101 */
102static void write_sdrc_timings(u32 cs, struct sdrc_actim *sdrc_actim_base,
Peter Barada8c4445d2012-11-13 07:40:28 +0000103 struct board_sdrc_timings *timings)
Tom Rini2a04e852011-11-18 12:48:00 +0000104{
105 /* Setup timings we got from the board. */
Peter Barada8c4445d2012-11-13 07:40:28 +0000106 writel(timings->mcfg, &sdrc_base->cs[cs].mcfg);
107 writel(timings->ctrla, &sdrc_actim_base->ctrla);
108 writel(timings->ctrlb, &sdrc_actim_base->ctrlb);
109 writel(timings->rfr_ctrl, &sdrc_base->cs[cs].rfr_ctrl);
Tom Rini2a04e852011-11-18 12:48:00 +0000110 writel(CMD_NOP, &sdrc_base->cs[cs].manual);
111 writel(CMD_PRECHARGE, &sdrc_base->cs[cs].manual);
112 writel(CMD_AUTOREFRESH, &sdrc_base->cs[cs].manual);
113 writel(CMD_AUTOREFRESH, &sdrc_base->cs[cs].manual);
Peter Barada8c4445d2012-11-13 07:40:28 +0000114 writel(timings->mr, &sdrc_base->cs[cs].mr);
Tom Rini2a04e852011-11-18 12:48:00 +0000115
116 /*
117 * Test ram in this bank
118 * Disable if bad or not present
119 */
120 if (!mem_ok(cs))
121 writel(0, &sdrc_base->cs[cs].mcfg);
122}
123
124/*
Vaibhav Hiremathcae377b2010-06-07 15:20:34 -0400125 * do_sdrc_init -
Tom Rini2a04e852011-11-18 12:48:00 +0000126 * - Code called once in C-Stack only context for CS0 and with early being
127 * true and a possible 2nd time depending on memory configuration from
128 * stack+global context.
Vaibhav Hiremathcae377b2010-06-07 15:20:34 -0400129 */
130void do_sdrc_init(u32 cs, u32 early)
131{
Steve Sakoman3667cbe2010-08-19 20:09:57 -0700132 struct sdrc_actim *sdrc_actim_base0, *sdrc_actim_base1;
Peter Barada8c4445d2012-11-13 07:40:28 +0000133 struct board_sdrc_timings timings;
Tom Rini2a04e852011-11-18 12:48:00 +0000134
135 sdrc_actim_base0 = (struct sdrc_actim *)SDRC_ACTIM_CTRL0_BASE;
136 sdrc_actim_base1 = (struct sdrc_actim *)SDRC_ACTIM_CTRL1_BASE;
Vaibhav Hiremathcae377b2010-06-07 15:20:34 -0400137
Tom Rini9ae0d552011-11-18 12:48:06 +0000138 /*
139 * When called in the early context this may be SPL and we will
140 * need to set all of the timings. This ends up being board
141 * specific so we call a helper function to take care of this
142 * for us. Otherwise, to be safe, we need to copy the settings
143 * from the first bank to the second. We will setup CS0,
144 * then set cs_cfg to the appropriate value then try and
145 * setup CS1.
146 */
147#ifdef CONFIG_SPL_BUILD
Peter Barada8c4445d2012-11-13 07:40:28 +0000148 get_board_mem_timings(&timings);
Tom Rini9ae0d552011-11-18 12:48:06 +0000149#endif
Vaibhav Hiremathcae377b2010-06-07 15:20:34 -0400150 if (early) {
151 /* reset sdrc controller */
152 writel(SOFTRESET, &sdrc_base->sysconfig);
153 wait_on_value(RESETDONE, RESETDONE, &sdrc_base->status,
154 12000000);
155 writel(0, &sdrc_base->sysconfig);
156
157 /* setup sdrc to ball mux */
158 writel(SDRC_SHARING, &sdrc_base->sharing);
159
Tom Rini2a04e852011-11-18 12:48:00 +0000160 /* Disable Power Down of CKE because of 1 CKE on combo part */
Vaibhav Hiremathcae377b2010-06-07 15:20:34 -0400161 writel(WAKEUPPROC | SRFRONRESET | PAGEPOLICY_HIGH,
162 &sdrc_base->power);
163
164 writel(ENADLL | DLLPHASE_90, &sdrc_base->dlla_ctrl);
165 sdelay(0x20000);
Simon Schwarzb88e4252011-09-14 15:15:37 -0400166#ifdef CONFIG_SPL_BUILD
Peter Barada8c4445d2012-11-13 07:40:28 +0000167 write_sdrc_timings(CS0, sdrc_actim_base0, &timings);
Tom Rini9ae0d552011-11-18 12:48:06 +0000168 make_cs1_contiguous();
Peter Barada8c4445d2012-11-13 07:40:28 +0000169 write_sdrc_timings(CS1, sdrc_actim_base1, &timings);
Simon Schwarzb88e4252011-09-14 15:15:37 -0400170#endif
171
Steve Sakoman3667cbe2010-08-19 20:09:57 -0700172 }
Vaibhav Hiremathcae377b2010-06-07 15:20:34 -0400173
174 /*
Tom Rini2a04e852011-11-18 12:48:00 +0000175 * If we aren't using SPL we have been loaded by some
176 * other means which may not have correctly initialized
177 * both CS0 and CS1 (such as some older versions of x-loader)
178 * so we may be asked now to setup CS1.
Vaibhav Hiremathcae377b2010-06-07 15:20:34 -0400179 */
Tom Rini2a04e852011-11-18 12:48:00 +0000180 if (cs == CS1) {
Peter Barada8c4445d2012-11-13 07:40:28 +0000181 timings.mcfg = readl(&sdrc_base->cs[CS0].mcfg),
182 timings.rfr_ctrl = readl(&sdrc_base->cs[CS0].rfr_ctrl);
183 timings.ctrla = readl(&sdrc_actim_base0->ctrla);
184 timings.ctrlb = readl(&sdrc_actim_base0->ctrlb);
185 timings.mr = readl(&sdrc_base->cs[CS0].mr);
186 write_sdrc_timings(cs, sdrc_actim_base1, &timings);
Tom Rini2a04e852011-11-18 12:48:00 +0000187 }
Vaibhav Hiremathcae377b2010-06-07 15:20:34 -0400188}
189
190/*
191 * dram_init -
192 * - Sets uboots idea of sdram size
193 */
Heiko Schocher561142a2010-09-17 13:10:41 +0200194int dram_init(void)
195{
Heiko Schocher561142a2010-09-17 13:10:41 +0200196 unsigned int size0 = 0, size1 = 0;
197
198 size0 = get_sdr_cs_size(CS0);
199 /*
Tom Rini5f862b72011-11-18 12:47:59 +0000200 * We always need to have cs_cfg point at where the second
201 * bank would be, if present. Failure to do so can lead to
202 * strange situations where memory isn't detected and
203 * configured correctly. CS0 will already have been setup
204 * at this point.
Heiko Schocher561142a2010-09-17 13:10:41 +0200205 */
Tom Rini5f862b72011-11-18 12:47:59 +0000206 make_cs1_contiguous();
207 do_sdrc_init(CS1, NOT_EARLY);
208 size1 = get_sdr_cs_size(CS1);
Heiko Schocher561142a2010-09-17 13:10:41 +0200209
Heiko Schocher561142a2010-09-17 13:10:41 +0200210 gd->ram_size = size0 + size1;
211
212 return 0;
213}
214
215void dram_init_banksize (void)
216{
Heiko Schocher561142a2010-09-17 13:10:41 +0200217 unsigned int size0 = 0, size1 = 0;
218
219 size0 = get_sdr_cs_size(CS0);
220 size1 = get_sdr_cs_size(CS1);
221
222 gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
223 gd->bd->bi_dram[0].size = size0;
224 gd->bd->bi_dram[1].start = PHYS_SDRAM_1 + get_sdr_cs_offset(CS1);
225 gd->bd->bi_dram[1].size = size1;
226}
Vaibhav Hiremathcae377b2010-06-07 15:20:34 -0400227
228/*
229 * mem_init -
230 * - Init the sdrc chip,
231 * - Selects CS0 and CS1,
232 */
233void mem_init(void)
234{
235 /* only init up first bank here */
236 do_sdrc_init(CS0, EARLY_INIT);
237}