ppc4xx: Netstal HCU5 board: added various fixes and POST
[platform/kernel/u-boot.git] / board / netstal / hcu5 / sdram.c
1 /*
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
39 void hcu_led_set(u32 value);
40 void dcbz_area(u32 start_address, u32 num_bytes);
41 void 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
65 #define MY_TLB_WORD2_I_ENABLE TLB_WORD2_I_ENABLE
66         /* disable caching on DDR2 */
67
68 void program_tlb(u32 phys_addr, u32 virt_addr, u32 size, u32 tlb_word2_i_value);
69
70 void board_add_ram_info(int use_default)
71 {
72         PPC4xx_SYS_INFO board_cfg;
73         u32 val;
74         mfsdram(DDR0_22, val);
75         val &= DDR0_22_CTRL_RAW_MASK;
76         switch (val) {
77         case DDR0_22_CTRL_RAW_ECC_DISABLE:
78                 puts(" (ECC disabled");
79                 break;
80         case DDR0_22_CTRL_RAW_ECC_CHECK_ONLY:
81                 puts(" (ECC check only");
82                 break;
83         case DDR0_22_CTRL_RAW_NO_ECC_RAM:
84                 puts(" (no ECC ram");
85                 break;
86         case DDR0_22_CTRL_RAW_ECC_ENABLE:
87                 puts(" (ECC enabled");
88                 break;
89         }
90
91         get_sys_info(&board_cfg);
92         printf(", %d MHz", (board_cfg.freqPLB * 2) / 1000000);
93
94         mfsdram(DDR0_03, val);
95         val = DDR0_03_CASLAT_DECODE(val);
96         printf(", CL%d)", val);
97 }
98
99 /*--------------------------------------------------------------------
100  * wait_for_dlllock.
101  *--------------------------------------------------------------------*/
102 static int wait_for_dlllock(void)
103 {
104         unsigned long val;
105         int wait = 0;
106
107         /* -----------------------------------------------------------+
108          * Wait for the DCC master delay line to finish calibration
109          * ----------------------------------------------------------*/
110         mtdcr(ddrcfga, DDR0_17);
111         val = DDR0_17_DLLLOCKREG_UNLOCKED;
112
113         while (wait != 0xffff) {
114                 val = mfdcr(ddrcfgd);
115                 if ((val & DDR0_17_DLLLOCKREG_MASK) ==
116                     DDR0_17_DLLLOCKREG_LOCKED)
117                         /* dlllockreg bit on */
118                         return 0;
119                 else
120                         wait++;
121         }
122         debug("0x%04x: DDR0_17 Value (dlllockreg bit): 0x%08x\n", wait, val);
123         debug("Waiting for dlllockreg bit to raise\n");
124
125         return -1;
126 }
127
128 /***********************************************************************
129  *
130  * sdram_panic -- Panic if we cannot configure the sdram correctly
131  *
132  ************************************************************************/
133 void sdram_panic(const char *reason)
134 {
135         printf("\n%s: reason %s",  __FUNCTION__,  reason);
136         hcu_led_set(0xff);
137         while (1) {
138         }
139         /* Never return */
140 }
141
142 #ifdef CONFIG_DDR_ECC
143 static void blank_string(int size)
144 {
145         int i;
146
147         for (i=0; i<size; i++)
148                 putc('\b');
149         for (i=0; i<size; i++)
150                 putc(' ');
151         for (i=0; i<size; i++)
152                 putc('\b');
153 }
154 /*---------------------------------------------------------------------------+
155  * program_ecc.
156  *---------------------------------------------------------------------------*/
157 static void program_ecc(unsigned long start_address, unsigned long num_bytes)
158 {
159         u32 val;
160         char str[] = "ECC generation -";
161 #if defined(CONFIG_PRAM)
162         u32 *magic;
163
164         /* Check whether vxWorks is using EDR logging, if yes zero */
165         /* also PostMortem and user reserved memory */
166         magic= in_be32(start_address  + num_bytes -
167                         (CONFIG_PRAM*1024) + sizeof(u32));
168
169         debug("\n%s:  CONFIG_PRAM %d kB magic 0x%x 0x%p -> 0x%x\n", __FUNCTION__,
170                CONFIG_PRAM,
171                start_address  + num_bytes - (CONFIG_PRAM*1024) + sizeof(u32),
172                magic, in_be32(magic));
173         if (in_be32(magic) == 0xbeefbabe)
174                 num_bytes -= (CONFIG_PRAM*1024)  - PM_RESERVED_MEM;
175 #endif
176
177
178         sync();
179         eieio();
180
181         puts(str);
182
183         /* ECC bit set method for cached memory */
184         /* Fast method, no noticeable delay */
185         dcbz_area(start_address, num_bytes);
186         dflush();
187         blank_string(strlen(str));
188
189         /* Clear error status */
190         mfsdram(DDR0_00, val);
191         mtsdram(DDR0_00, val | DDR0_00_INT_ACK_ALL);
192
193         /*
194          * Clear possible ECC errors
195          * If not done, then we could get an interrupt later on when
196          * exceptions are enabled.
197          */
198         mtspr(mcsr, mfspr(mcsr));
199
200         /* Set 'int_mask' parameter to functionnal value */
201         mfsdram(DDR0_01, val);
202         mtsdram(DDR0_01, ((val &~ DDR0_01_INT_MASK_MASK) |
203                           DDR0_01_INT_MASK_ALL_OFF));
204
205         return;
206 }
207
208 #endif
209
210
211 /***********************************************************************
212  *
213  * initdram -- 440EPx's DDR controller is a DENALI Core
214  *
215  ************************************************************************/
216 long int initdram (int board_type)
217 {
218 #define HCU_HW_SDRAM_CONFIG_MASK 0x7
219 #define INVALID_HW_CONFIG   "Invalid HW-Config"
220         u16 *hwVersReg = (u16 *) HCU_HW_VERSION_REGISTER;
221         unsigned int dram_size = 0;
222
223         mtsdram(DDR0_02, 0x00000000);
224
225         /* Values must be kept in sync with Excel-table <<A0001492.>> ! */
226         mtsdram(DDR0_00, 0x0000190A);
227         mtsdram(DDR0_01, 0x01000000);
228         mtsdram(DDR0_03, 0x02030602);
229         mtsdram(DDR0_04, 0x0A020200);
230         mtsdram(DDR0_05, 0x02020307);
231         switch (*hwVersReg & HCU_HW_SDRAM_CONFIG_MASK) {
232         case 1:
233                 dram_size = 256 * 1024 * 1024 ;
234                 mtsdram(DDR0_06, 0x0102C812);  /* 256MB RAM */
235                 mtsdram(DDR0_11, 0x0014C800);  /* 256MB RAM */
236                 mtsdram(DDR0_43, 0x030A0200);  /* 256MB RAM */
237                 break;
238         case 0:
239         default:
240                 dram_size = 128 * 1024 * 1024 ;
241                 mtsdram(DDR0_06, 0x0102C80D);  /* 128MB RAM */
242                 mtsdram(DDR0_11, 0x000FC800);  /* 128MB RAM */
243                 mtsdram(DDR0_43, 0x030A0300);  /* 128MB RAM */
244                 break;
245         }
246         mtsdram(DDR0_07, 0x00090100);
247
248         /*
249          * TCPD=200 cycles of clock input is required to lock the DLL.
250          * CKE must be HIGH the entire time.mtsdram(DDR0_08, 0x02C80001);
251          */
252         mtsdram(DDR0_08, 0x02C80001);
253         mtsdram(DDR0_09, 0x00011D5F);
254         mtsdram(DDR0_10, 0x00000100);
255         mtsdram(DDR0_12, 0x00000003);
256         mtsdram(DDR0_14, 0x00000000);
257         mtsdram(DDR0_17, 0x1D000000);
258         mtsdram(DDR0_18, 0x1D1D1D1D);
259         mtsdram(DDR0_19, 0x1D1D1D1D);
260         mtsdram(DDR0_20, 0x0B0B0B0B);
261         mtsdram(DDR0_21, 0x0B0B0B0B);
262         #define ECC_RAM  0x03267F0B
263         #define NO_ECC_RAM  0x00267F0B
264 #ifdef CONFIG_DDR_ECC
265         mtsdram(DDR0_22, ECC_RAM);
266 #else
267         mtsdram(DDR0_22, NO_ECC_RAM);
268 #endif
269
270         mtsdram(DDR0_23, 0x00000000);
271         mtsdram(DDR0_24, 0x01020001);
272         mtsdram(DDR0_26, 0x2D930517);
273         mtsdram(DDR0_27, 0x00008236);
274         mtsdram(DDR0_28, 0x00000000);
275         mtsdram(DDR0_31, 0x00000000);
276         mtsdram(DDR0_42, 0x01000006);
277         mtsdram(DDR0_44, 0x00000003);
278         mtsdram(DDR0_02, 0x00000001);
279         wait_for_dlllock();
280         mtsdram(DDR0_00, 0x40000000);  /* Zero init bit */
281
282         /*
283          * Program tlb entries for this size (dynamic)
284          */
285         remove_tlb(CFG_SDRAM_BASE, 256 << 20);
286         program_tlb(0, 0, dram_size, TLB_WORD2_W_ENABLE | TLB_WORD2_I_ENABLE);
287
288         /*
289          * Setup 2nd TLB with same physical address but different virtual
290          * address with cache enabled. This is done for fast ECC generation.
291          */
292         program_tlb(0, CFG_DDR_CACHED_ADDR, dram_size, 0);
293
294 #ifdef CONFIG_DDR_ECC
295         /*
296          * If ECC is enabled, initialize the parity bits.
297          */
298         program_ecc(CFG_DDR_CACHED_ADDR, dram_size);
299 #endif
300
301         return (dram_size);
302 }