2 * Copyright 2004 Freescale Semiconductor.
3 * (C) Copyright 2003 Motorola Inc.
4 * Xianghua Xiao (X.Xiao@motorola.com)
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 #include <asm/processor.h>
31 #ifdef CONFIG_SPD_EEPROM
33 #define ns2clk(ns) ((ns) / (2000000000 /get_bus_freq(0) + 1) + 1)
35 long int spd_sdram(void) {
36 volatile immap_t *immap = (immap_t *)CFG_IMMR;
37 volatile ccsr_ddr_t *ddr = &immap->im_ddr;
38 volatile ccsr_local_ecm_t *ecm = &immap->im_local_ecm;
40 unsigned int memsize,tmp,tmp1,tmp2;
43 i2c_read (SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd));
45 if ( spd.nrows > 2 ) {
46 printf("DDR:Only two chip selects are supported on ADS.\n");
50 if ( spd.nrow_addr < 12 || spd.nrow_addr > 14 || spd.ncol_addr < 8 || spd.ncol_addr > 11) {
51 printf("DDR:Row or Col number unsupported.\n");
55 ddr->cs0_bnds = ((spd.row_dens>>2) - 1);
56 ddr->cs0_config = ( 1<<31 | (spd.nrow_addr-12)<<8 | (spd.ncol_addr-8) );
58 debug ("cs0_bnds = 0x%08x\n",ddr->cs0_bnds);
59 debug ("cs0_config = 0x%08x\n",ddr->cs0_config);
60 if ( spd.nrows == 2 ) {
61 ddr->cs1_bnds = ((spd.row_dens<<14) | ((spd.row_dens>>1) - 1));
62 ddr->cs1_config = ( 1<<31 | (spd.nrow_addr-12)<<8 | (spd.ncol_addr-8) );
63 debug ("cs1_bnds = 0x%08x\n",ddr->cs1_bnds);
64 debug ("cs1_config = 0x%08x\n",ddr->cs1_config);
67 memsize = spd.nrows * (4 * spd.row_dens);
68 if( spd.mem_type != 0x07 ) {
69 printf("No DDR module found!\n");
75 tmp = 7; /* TLB size */
76 tmp1 = 1; /* TLB entry number */
77 tmp2 = 23; /* Local Access Window size */
110 printf ("DDR:we only added support 16M,32M,64M,128M,256M,512M and 1G DDR I.\n");
115 /* configure DDR TLB to TLB1 Entry 4,5 */
116 mtspr(MAS0, TLB1_MAS0(1,4,0));
117 mtspr(MAS1, TLB1_MAS1(1,1,0,0,tmp));
118 mtspr(MAS2, TLB1_MAS2(((CFG_DDR_SDRAM_BASE>>12) & 0xfffff),0,0,0,0,0,0,0,0));
119 mtspr(MAS3, TLB1_MAS3(((CFG_DDR_SDRAM_BASE>>12) & 0xfffff),0,0,0,0,0,1,0,1,0,1));
120 asm volatile("isync;msync;tlbwe;isync");
121 debug ("DDR:MAS0=0x%08x\n",TLB1_MAS0(1,4,0));
122 debug ("DDR:MAS1=0x%08x\n",TLB1_MAS1(1,1,0,0,tmp));
123 debug ("DDR:MAS2=0x%08x\n",TLB1_MAS2(((CFG_DDR_SDRAM_BASE>>12) \
124 & 0xfffff),0,0,0,0,0,0,0,0));
125 debug ("DDR:MAS3=0x%08x\n",TLB1_MAS3(((CFG_DDR_SDRAM_BASE>>12) \
126 & 0xfffff),0,0,0,0,0,1,0,1,0,1));
129 mtspr(MAS0, TLB1_MAS0(1,5,0));
130 mtspr(MAS1, TLB1_MAS1(1,1,0,0,tmp));
131 mtspr(MAS2, TLB1_MAS2((((CFG_DDR_SDRAM_BASE+(memsize*1024*1024)/2)>>12) \
132 & 0xfffff),0,0,0,0,0,0,0,0));
133 mtspr(MAS3, TLB1_MAS3((((CFG_DDR_SDRAM_BASE+(memsize*1024*1024)/2)>>12) \
134 & 0xfffff),0,0,0,0,0,1,0,1,0,1));
135 asm volatile("isync;msync;tlbwe;isync");
136 debug ("DDR:MAS0=0x%08x\n",TLB1_MAS0(1,5,0));
137 debug ("DDR:MAS1=0x%08x\n",TLB1_MAS1(1,1,0,0,tmp));
138 debug ("DDR:MAS2=0x%08x\n",TLB1_MAS2((((CFG_DDR_SDRAM_BASE \
139 +(memsize*1024*1024)/2)>>12) & 0xfffff),0,0,0,0,0,0,0,0));
140 debug ("DDR:MAS3=0x%08x\n",TLB1_MAS3((((CFG_DDR_SDRAM_BASE \
141 +(memsize*1024*1024)/2)>>12) & 0xfffff),0,0,0,0,0,1,0,1,0,1));
144 #if defined(CONFIG_RAM_AS_FLASH)
145 ecm->lawbar2 = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff);
146 ecm->lawar2 = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & tmp2));
147 debug ("DDR:LAWBAR2=0x%08x\n",ecm->lawbar2);
148 debug ("DDR:LARAR2=0x%08x\n",ecm->lawar2);
150 ecm->lawbar1 = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff);
151 ecm->lawar1 = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & tmp2));
152 debug ("DDR:LAWBAR1=0x%08x\n",ecm->lawbar1);
153 debug ("DDR:LARAR1=0x%08x\n",ecm->lawar1);
156 tmp = 20000/(((spd.clk_cycle & 0xF0) >> 4) * 10 + (spd.clk_cycle & 0x0f));
157 debug ("DDR:Module maximum data rate is: %dMhz\n",tmp);
159 /* find the largest CAS */
160 if(spd.cas_lat & 0x40) {
162 } else if (spd.cas_lat & 0x20) {
164 } else if (spd.cas_lat & 0x10) {
166 } else if (spd.cas_lat & 0x08) {
168 } else if (spd.cas_lat & 0x04) {
170 } else if (spd.cas_lat & 0x02) {
172 } else if (spd.cas_lat & 0x01) {
175 printf("DDR:no valid CAS Latency information.\n");
179 tmp1 = get_bus_freq(0)/1000000;
180 if(tmp1<230 && tmp1>=90 && tmp>=230) {
181 /* 90~230 range, treated as DDR 200 */
182 if(spd.clk_cycle3 == 0xa0) caslat -= 2;
183 else if(spd.clk_cycle2 == 0xa0) caslat--;
184 } else if(tmp1<280 && tmp1>=230 && tmp>=280) {
185 /* 230-280 range, treated as DDR 266 */
186 if(spd.clk_cycle3 == 0x75) caslat -= 2;
187 else if(spd.clk_cycle2 == 0x75) caslat--;
188 } else if(tmp1<350 && tmp1>=280 && tmp>=350) {
189 /* 280~350 range, treated as DDR 333 */
190 if(spd.clk_cycle3 == 0x60) caslat -= 2;
191 else if(spd.clk_cycle2 == 0x60) caslat--;
192 } else if(tmp1<90 || tmp1 >=350) { /* DDR rate out-of-range */
193 printf("DDR:platform frequency is not fit for DDR rate\n");
197 /* note: caslat must also be programmed into ddr->sdram_mode
199 /* note: WRREC(Twr) and WRTORD(Twtr) are not in SPD,use
200 conservative value here */
201 ddr->timing_cfg_1 = (((ns2clk(spd.trp/4) & 0x07) << 28 ) | \
202 ((ns2clk(spd.tras) & 0x0f ) << 24 ) | \
203 ((ns2clk(spd.trcd/4) & 0x07) << 20 ) | \
204 ((caslat & 0x07)<< 16 ) | \
205 (((ns2clk(spd.sset[6]) - 8) & 0x0f) << 12 ) | \
207 ((ns2clk(spd.trrd/4) & 0x07) << 4) | 1);
209 debug ("DDR:timing_cfg_1=0x%08x\n",ddr->timing_cfg_1);
211 ddr->timing_cfg_2 = 0x00000800;
212 debug ("DDR:timing_cfg_2=0x%08x\n",ddr->timing_cfg_2);
214 /* only DDR I is supported, DDR I and II have different mode-register-set definition */
215 /* burst length is always 4 */
218 ddr->sdram_mode = 0x52; /* 1.5 */
221 ddr->sdram_mode = 0x22; /* 2.0 */
224 ddr->sdram_mode = 0x62; /* 2.5 */
227 ddr->sdram_mode = 0x32; /* 3.0 */
230 printf("DDR:only CAS Latency 1.5,2.0,2.5,3.0 is supported.\n");
233 debug ("DDR:sdram_mode=0x%08x\n",ddr->sdram_mode);
235 switch(spd.refresh) {
258 tmp = ns2clk(125000);
265 /* set BSTOPRE to 0x100 for page mode, if auto-charge is used, set BSTOPRE = 0 */
266 ddr->sdram_interval = ((tmp & 0x3fff) << 16) | 0x100;
267 debug ("DDR:sdram_interval=0x%08x\n",ddr->sdram_interval);
269 /* is this an ECC DDR chip? */
270 #if defined(CONFIG_DDR_ECC)
271 if(spd.config == 0x02) {
272 ddr->err_disable = 0x0000000d;
273 ddr->err_sbe = 0x00ff0000;
275 debug ("DDR:err_disable=0x%08x\n",ddr->err_disable);
276 debug ("DDR:err_sbe=0x%08x\n",ddr->err_sbe);
278 asm("sync;isync;msync");
282 #ifdef MPC85xx_DDR_SDRAM_CLK_CNTL
283 /* Setup the clock control (8555 and later)
284 * SDRAM_CLK_CNTL[0] = Source synchronous enable == 1
285 * SDRAM_CLK_CNTL[5-7] = Clock Adjust == 3 (3/4 cycle late)
287 ddr->sdram_clk_cntl = 0x83000000;
290 /* Figure out the settings for the sdram_cfg register. Build up
291 * the entire register in 'tmp' before writing since the write into
292 * the register will actually enable the memory controller, and all
293 * settings must be done before enabling.
295 * sdram_cfg[0] = 1 (ddr sdram logic enable)
296 * sdram_cfg[1] = 1 (self-refresh-enable)
297 * sdram_cfg[6:7] = 2 (SDRAM type = DDR SDRAM)
301 /* sdram_cfg[3] = RD_EN - registered DIMM enable
302 * A value of 0x26 indicates micron registered DIMMS (micron.com)
304 if (spd.mod_attr == 0x26) {
308 #if defined(CONFIG_DDR_ECC)
309 /* If the user wanted ECC (enabled via sdram_cfg[2]) */
310 if (spd.config == 0x02) {
317 * REV1 uses 1T timing.
318 * REV2 may use 1T or 2T as configured by the user.
321 uint pvr = get_pvr();
323 if (pvr != PVR_85xx_REV1) {
324 #if defined(CONFIG_DDR_2T_TIMING)
326 * Enable 2T timing by setting sdram_cfg[16].
333 ddr->sdram_cfg = tmp;
335 asm("sync;isync;msync");
339 debug ("DDR:sdram_cfg=0x%08x\n",ddr->sdram_cfg);
341 return (memsize*1024*1024);
344 #endif /* CONFIG_SPD_EEPROM */