blob: 5435de1249c82c56368bca3890906eca85848bad [file] [log] [blame]
Niklaus Giger9618e8b2007-07-27 11:31:22 +02001/*
2 * (C) Copyright 2007
3 * Niklaus Giger (Niklaus.Giger@netstal.com)
4 * (C) Copyright 2006
5 * Sylvie Gohl, AMCC/IBM, gohl.sylvie@fr.ibm.com
6 * Jacqueline Pira-Ferriol, AMCC/IBM, jpira-ferriol@fr.ibm.com
7 * Thierry Roman, AMCC/IBM, thierry_roman@fr.ibm.com
8 * Alain Saurel, AMCC/IBM, alain.saurel@fr.ibm.com
9 * Robert Snyder, AMCC/IBM, rob.snyder@fr.ibm.com
10 *
11 * (C) Copyright 2006
12 * Stefan Roese, DENX Software Engineering, sr@denx.de.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * MA 02111-1307 USA
28 */
29
30/* define DEBUG for debug output */
31#undef DEBUG
32
33#include <common.h>
34#include <asm/processor.h>
35#include <asm/io.h>
36#include <asm/mmu.h>
37#include <ppc440.h>
38
Niklaus Giger771180f2007-08-16 15:16:02 +020039void hcu_led_set(u32 value);
Niklaus Giger9618e8b2007-07-27 11:31:22 +020040void dcbz_area(u32 start_address, u32 num_bytes);
41void dflush(void);
42
43#define DDR_DCR_BASE 0x10
44#define ddrcfga (DDR_DCR_BASE+0x0) /* DDR configuration address reg */
45#define ddrcfgd (DDR_DCR_BASE+0x1) /* DDR configuration data reg */
46
47#define DDR0_01_INT_MASK_MASK 0x000000FF
48#define DDR0_00_INT_ACK_ALL 0x7F000000
49#define DDR0_01_INT_MASK_ALL_ON 0x000000FF
50#define DDR0_01_INT_MASK_ALL_OFF 0x00000000
51
52#define DDR0_17_DLLLOCKREG_MASK 0x00010000 /* Read only */
53#define DDR0_17_DLLLOCKREG_UNLOCKED 0x00000000
54#define DDR0_17_DLLLOCKREG_LOCKED 0x00010000
55
56#define DDR0_22 0x16
57/* ECC */
58#define DDR0_22_CTRL_RAW_MASK 0x03000000
59#define DDR0_22_CTRL_RAW_ECC_DISABLE 0x00000000 /* ECC not enabled */
60#define DDR0_22_CTRL_RAW_ECC_CHECK_ONLY 0x01000000 /* ECC no correction */
61#define DDR0_22_CTRL_RAW_NO_ECC_RAM 0x02000000 /* Not a ECC RAM*/
62#define DDR0_22_CTRL_RAW_ECC_ENABLE 0x03000000 /* ECC correcting on */
63#define DDR0_03_CASLAT_DECODE(n) ((((unsigned long)(n))>>16)&0x7)
64
Stefan Roese9153cd32008-01-17 14:29:04 +010065#define ECC_RAM 0x03267F0B
66#define NO_ECC_RAM 0x00267F0B
67
68#define HCU_HW_SDRAM_CONFIG_MASK 0x7
69
Niklaus Gigerc623bb02008-01-16 18:39:18 +010070#define MY_TLB_WORD2_I_ENABLE TLB_WORD2_I_ENABLE
71 /* disable caching on DDR2 */
Niklaus Giger9618e8b2007-07-27 11:31:22 +020072
73void program_tlb(u32 phys_addr, u32 virt_addr, u32 size, u32 tlb_word2_i_value);
74
Niklaus Giger9618e8b2007-07-27 11:31:22 +020075void board_add_ram_info(int use_default)
76{
Stefan Roeseedd73f22007-10-21 08:12:41 +020077 PPC4xx_SYS_INFO board_cfg;
Niklaus Giger9618e8b2007-07-27 11:31:22 +020078 u32 val;
Stefan Roese9153cd32008-01-17 14:29:04 +010079
Niklaus Giger9618e8b2007-07-27 11:31:22 +020080 mfsdram(DDR0_22, val);
81 val &= DDR0_22_CTRL_RAW_MASK;
82 switch (val) {
83 case DDR0_22_CTRL_RAW_ECC_DISABLE:
84 puts(" (ECC disabled");
85 break;
86 case DDR0_22_CTRL_RAW_ECC_CHECK_ONLY:
87 puts(" (ECC check only");
88 break;
89 case DDR0_22_CTRL_RAW_NO_ECC_RAM:
90 puts(" (no ECC ram");
91 break;
92 case DDR0_22_CTRL_RAW_ECC_ENABLE:
93 puts(" (ECC enabled");
94 break;
95 }
96
97 get_sys_info(&board_cfg);
98 printf(", %d MHz", (board_cfg.freqPLB * 2) / 1000000);
99
100 mfsdram(DDR0_03, val);
101 val = DDR0_03_CASLAT_DECODE(val);
102 printf(", CL%d)", val);
103}
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200104
105/*--------------------------------------------------------------------
106 * wait_for_dlllock.
107 *--------------------------------------------------------------------*/
108static int wait_for_dlllock(void)
109{
110 unsigned long val;
111 int wait = 0;
112
113 /* -----------------------------------------------------------+
114 * Wait for the DCC master delay line to finish calibration
115 * ----------------------------------------------------------*/
116 mtdcr(ddrcfga, DDR0_17);
117 val = DDR0_17_DLLLOCKREG_UNLOCKED;
118
119 while (wait != 0xffff) {
120 val = mfdcr(ddrcfgd);
121 if ((val & DDR0_17_DLLLOCKREG_MASK) ==
122 DDR0_17_DLLLOCKREG_LOCKED)
123 /* dlllockreg bit on */
124 return 0;
125 else
126 wait++;
127 }
128 debug("0x%04x: DDR0_17 Value (dlllockreg bit): 0x%08x\n", wait, val);
129 debug("Waiting for dlllockreg bit to raise\n");
130
131 return -1;
132}
133
134/***********************************************************************
135 *
136 * sdram_panic -- Panic if we cannot configure the sdram correctly
137 *
138 ************************************************************************/
139void sdram_panic(const char *reason)
140{
141 printf("\n%s: reason %s", __FUNCTION__, reason);
Niklaus Giger771180f2007-08-16 15:16:02 +0200142 hcu_led_set(0xff);
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200143 while (1) {
144 }
145 /* Never return */
146}
147
148#ifdef CONFIG_DDR_ECC
149static void blank_string(int size)
150{
151 int i;
152
153 for (i=0; i<size; i++)
154 putc('\b');
155 for (i=0; i<size; i++)
156 putc(' ');
157 for (i=0; i<size; i++)
158 putc('\b');
159}
160/*---------------------------------------------------------------------------+
161 * program_ecc.
162 *---------------------------------------------------------------------------*/
Niklaus Gigerc623bb02008-01-16 18:39:18 +0100163static void program_ecc(unsigned long start_address, unsigned long num_bytes)
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200164{
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200165 u32 val;
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200166 char str[] = "ECC generation -";
Niklaus Gigerc623bb02008-01-16 18:39:18 +0100167#if defined(CONFIG_PRAM)
168 u32 *magic;
169
170 /* Check whether vxWorks is using EDR logging, if yes zero */
171 /* also PostMortem and user reserved memory */
Stefan Roese9153cd32008-01-17 14:29:04 +0100172 magic = (u32 *)in_be32((u32 *)(start_address + num_bytes -
173 (CONFIG_PRAM*1024) + sizeof(u32)));
Niklaus Gigerc623bb02008-01-16 18:39:18 +0100174
175 debug("\n%s: CONFIG_PRAM %d kB magic 0x%x 0x%p -> 0x%x\n", __FUNCTION__,
176 CONFIG_PRAM,
Stefan Roese9153cd32008-01-17 14:29:04 +0100177 start_address + num_bytes - (CONFIG_PRAM*1024) + sizeof(u32),
Niklaus Gigerc623bb02008-01-16 18:39:18 +0100178 magic, in_be32(magic));
179 if (in_be32(magic) == 0xbeefbabe)
Stefan Roese9153cd32008-01-17 14:29:04 +0100180 num_bytes -= (CONFIG_PRAM*1024) - PM_RESERVED_MEM;
Niklaus Gigerc623bb02008-01-16 18:39:18 +0100181#endif
182
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200183 sync();
184 eieio();
185
186 puts(str);
187
Niklaus Gigerc623bb02008-01-16 18:39:18 +0100188 /* ECC bit set method for cached memory */
189 /* Fast method, no noticeable delay */
190 dcbz_area(start_address, num_bytes);
191 dflush();
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200192 blank_string(strlen(str));
193
194 /* Clear error status */
195 mfsdram(DDR0_00, val);
196 mtsdram(DDR0_00, val | DDR0_00_INT_ACK_ALL);
197
Niklaus Giger771180f2007-08-16 15:16:02 +0200198 /*
Niklaus Gigerc623bb02008-01-16 18:39:18 +0100199 * Clear possible ECC errors
Niklaus Giger771180f2007-08-16 15:16:02 +0200200 * If not done, then we could get an interrupt later on when
201 * exceptions are enabled.
202 */
203 mtspr(mcsr, mfspr(mcsr));
204
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200205 /* Set 'int_mask' parameter to functionnal value */
206 mfsdram(DDR0_01, val);
207 mtsdram(DDR0_01, ((val &~ DDR0_01_INT_MASK_MASK) |
208 DDR0_01_INT_MASK_ALL_OFF));
209
210 return;
211}
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200212#endif
213
Niklaus Gigerc623bb02008-01-16 18:39:18 +0100214
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200215/***********************************************************************
216 *
217 * initdram -- 440EPx's DDR controller is a DENALI Core
218 *
219 ************************************************************************/
220long int initdram (int board_type)
221{
Stefan Roese0a677fd2007-08-10 10:42:25 +0200222 unsigned int dram_size = 0;
223
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200224 mtsdram(DDR0_02, 0x00000000);
225
226 /* Values must be kept in sync with Excel-table <<A0001492.>> ! */
227 mtsdram(DDR0_00, 0x0000190A);
228 mtsdram(DDR0_01, 0x01000000);
229 mtsdram(DDR0_03, 0x02030602);
230 mtsdram(DDR0_04, 0x0A020200);
231 mtsdram(DDR0_05, 0x02020307);
Stefan Roese9153cd32008-01-17 14:29:04 +0100232 switch (in_be16((u16 *)HCU_HW_VERSION_REGISTER) & HCU_HW_SDRAM_CONFIG_MASK) {
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200233 case 1:
234 dram_size = 256 * 1024 * 1024 ;
Stefan Roese0a677fd2007-08-10 10:42:25 +0200235 mtsdram(DDR0_06, 0x0102C812); /* 256MB RAM */
236 mtsdram(DDR0_11, 0x0014C800); /* 256MB RAM */
237 mtsdram(DDR0_43, 0x030A0200); /* 256MB RAM */
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200238 break;
Niklaus Gigerc623bb02008-01-16 18:39:18 +0100239 case 0:
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200240 default:
Niklaus Gigerc623bb02008-01-16 18:39:18 +0100241 dram_size = 128 * 1024 * 1024 ;
242 mtsdram(DDR0_06, 0x0102C80D); /* 128MB RAM */
243 mtsdram(DDR0_11, 0x000FC800); /* 128MB RAM */
244 mtsdram(DDR0_43, 0x030A0300); /* 128MB RAM */
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200245 break;
246 }
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200247 mtsdram(DDR0_07, 0x00090100);
Niklaus Gigerc623bb02008-01-16 18:39:18 +0100248
Stefan Roese0a677fd2007-08-10 10:42:25 +0200249 /*
250 * TCPD=200 cycles of clock input is required to lock the DLL.
251 * CKE must be HIGH the entire time.mtsdram(DDR0_08, 0x02C80001);
252 */
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200253 mtsdram(DDR0_08, 0x02C80001);
254 mtsdram(DDR0_09, 0x00011D5F);
255 mtsdram(DDR0_10, 0x00000100);
256 mtsdram(DDR0_12, 0x00000003);
257 mtsdram(DDR0_14, 0x00000000);
258 mtsdram(DDR0_17, 0x1D000000);
259 mtsdram(DDR0_18, 0x1D1D1D1D);
260 mtsdram(DDR0_19, 0x1D1D1D1D);
261 mtsdram(DDR0_20, 0x0B0B0B0B);
262 mtsdram(DDR0_21, 0x0B0B0B0B);
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200263#ifdef CONFIG_DDR_ECC
264 mtsdram(DDR0_22, ECC_RAM);
265#else
266 mtsdram(DDR0_22, NO_ECC_RAM);
267#endif
268
269 mtsdram(DDR0_23, 0x00000000);
270 mtsdram(DDR0_24, 0x01020001);
271 mtsdram(DDR0_26, 0x2D930517);
272 mtsdram(DDR0_27, 0x00008236);
273 mtsdram(DDR0_28, 0x00000000);
274 mtsdram(DDR0_31, 0x00000000);
275 mtsdram(DDR0_42, 0x01000006);
276 mtsdram(DDR0_44, 0x00000003);
277 mtsdram(DDR0_02, 0x00000001);
278 wait_for_dlllock();
279 mtsdram(DDR0_00, 0x40000000); /* Zero init bit */
280
281 /*
282 * Program tlb entries for this size (dynamic)
283 */
Niklaus Giger771180f2007-08-16 15:16:02 +0200284 remove_tlb(CFG_SDRAM_BASE, 256 << 20);
Niklaus Gigerc623bb02008-01-16 18:39:18 +0100285 program_tlb(0, 0, dram_size, TLB_WORD2_W_ENABLE | TLB_WORD2_I_ENABLE);
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200286
287 /*
288 * Setup 2nd TLB with same physical address but different virtual
289 * address with cache enabled. This is done for fast ECC generation.
290 */
291 program_tlb(0, CFG_DDR_CACHED_ADDR, dram_size, 0);
292
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200293#ifdef CONFIG_DDR_ECC
294 /*
295 * If ECC is enabled, initialize the parity bits.
296 */
Niklaus Gigerc623bb02008-01-16 18:39:18 +0100297 program_ecc(CFG_DDR_CACHED_ADDR, dram_size);
Niklaus Giger9618e8b2007-07-27 11:31:22 +0200298#endif
299
300 return (dram_size);
301}