Merge branch 'origin'
[platform/kernel/u-boot.git] / cpu / ppc4xx / sdram.c
1 /*
2  * (C) Copyright 2005
3  * Stefan Roese, DENX Software Engineering, sr@denx.de.
4  *
5  * (C) Copyright 2002-2004
6  * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
7  *
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of
14  * the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24  * MA 02111-1307 USA
25  */
26
27 #include <common.h>
28 #include <ppc4xx.h>
29 #include <asm/processor.h>
30
31
32 #ifdef CONFIG_SDRAM_BANK0
33
34
35 #define mtsdram0(reg, data)  mtdcr(memcfga,reg);mtdcr(memcfgd,data)
36
37
38 struct sdram_conf_s {
39         unsigned long size;
40         unsigned long reg;
41 };
42
43 typedef struct sdram_conf_s sdram_conf_t;
44
45 #ifndef CFG_SDRAM_TABLE
46 sdram_conf_t mb0cf[] = {
47         {(128 << 20), 0x000A4001},      /* (0-128MB) Address Mode 3, 13x10(4) */
48         {(64 << 20),  0x00084001},      /* (0-64MB) Address Mode 3, 13x9(4)   */
49         {(32 << 20),  0x00062001},      /* (0-32MB) Address Mode 2, 12x9(4)   */
50         {(16 << 20),  0x00046001},      /* (0-16MB) Address Mode 4, 12x8(4)   */
51         {(4 << 20),   0x00008001},      /* (0-4MB) Address Mode 5, 11x8(2)    */
52 };
53 #else
54 sdram_conf_t mb0cf[] = CFG_SDRAM_TABLE;
55 #endif
56
57 #define N_MB0CF (sizeof(mb0cf) / sizeof(mb0cf[0]))
58
59
60 #ifndef CONFIG_440
61
62 /*
63  * Autodetect onboard SDRAM on 405 platforms
64  */
65 void sdram_init(void)
66 {
67         ulong sdtr1;
68         ulong rtr;
69         int i;
70
71         /*
72          * Support for 100MHz and 133MHz SDRAM
73          */
74         if (get_bus_freq(0) > 100000000) {
75                 /*
76                  * 133 MHz SDRAM
77                  */
78                 sdtr1 = 0x01074015;
79                 rtr = 0x07f00000;
80         } else {
81                 /*
82                  * default: 100 MHz SDRAM
83                  */
84                 sdtr1 = 0x0086400d;
85                 rtr = 0x05f00000;
86         }
87
88         for (i=0; i<N_MB0CF; i++) {
89                 /*
90                  * Disable memory controller.
91                  */
92                 mtsdram0(mem_mcopt1, 0x00000000);
93
94                 /*
95                  * Set MB0CF for bank 0.
96                  */
97                 mtsdram0(mem_mb0cf, mb0cf[i].reg);
98                 mtsdram0(mem_sdtr1, sdtr1);
99                 mtsdram0(mem_rtr, rtr);
100
101                 udelay(200);
102
103                 /*
104                  * Set memory controller options reg, MCOPT1.
105                  * Set DC_EN to '1' and BRD_PRF to '01' for 16 byte PLB Burst
106                  * read/prefetch.
107                  */
108                 mtsdram0(mem_mcopt1, 0x80800000);
109
110                 udelay(10000);
111
112                 if (get_ram_size(0, mb0cf[i].size) == mb0cf[i].size) {
113                         /*
114                          * OK, size detected -> all done
115                          */
116                         return;
117                 }
118         }
119 }
120
121 #else /* CONFIG_440 */
122
123 /*
124  * Autodetect onboard DDR SDRAM on 440 platforms
125  *
126  * NOTE: Some of the hardcoded values are hardware dependant,
127  *       so this should be extended for other future boards
128  *       using this routine!
129  */
130 long int initdram(int board_type)
131 {
132         int i;
133
134         for (i=0; i<N_MB0CF; i++) {
135                 /*
136                  * Disable memory controller.
137                  */
138                 mtsdram(mem_cfg0, 0x00000000);
139
140                 /*
141                  * Setup some default
142                  */
143                 mtsdram(mem_uabba, 0x00000000); /* ubba=0 (default)             */
144                 mtsdram(mem_slio, 0x00000000);  /* rdre=0 wrre=0 rarw=0         */
145                 mtsdram(mem_devopt, 0x00000000); /* dll=0 ds=0 (normal)         */
146                 mtsdram(mem_wddctr, 0x00000000); /* wrcp=0 dcd=0                */
147                 mtsdram(mem_clktr, 0x40000000); /* clkp=1 (90 deg wr) dcdt=0    */
148
149                 /*
150                  * Following for CAS Latency = 2.5 @ 133 MHz PLB
151                  */
152                 mtsdram(mem_b0cr, mb0cf[i].reg);
153                 mtsdram(mem_tr0, 0x41094012);
154                 mtsdram(mem_tr1, 0x80800800);   /* SS=T2 SL=STAGE 3 CD=1 CT=0x00*/
155                 mtsdram(mem_rtr, 0x7e000000);   /* Interval 15.20µs @ 133MHz PLB*/
156                 mtsdram(mem_cfg1, 0x00000000);  /* Self-refresh exit, disable PM*/
157                 udelay(400);                    /* Delay 200 usecs (min)        */
158
159                 /*
160                  * Enable the controller, then wait for DCEN to complete
161                  */
162                 mtsdram(mem_cfg0, 0x86000000);  /* DCEN=1, PMUD=1, 64-bit       */
163                 udelay(10000);
164
165                 if (get_ram_size(0, mb0cf[i].size) == mb0cf[i].size) {
166                         /*
167                          * OK, size detected -> all done
168                          */
169                         return mb0cf[i].size;
170                 }
171         }
172
173         return 0;                               /* nothing found !              */
174 }
175
176 #endif /* CONFIG_440 */
177
178 #endif /* CONFIG_SDRAM_BANK0 */