02b29ad62d3826619d7dc8e485b825895ef7a920
[kernel/u-boot.git] / cpu / mpc85xx / spd_sdram.c
1 /*
2  * Copyright 2004 Freescale Semiconductor.
3  * (C) Copyright 2003 Motorola Inc.
4  * Xianghua Xiao (X.Xiao@motorola.com)
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
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.
13  *
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.
18  *
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,
22  * MA 02111-1307 USA
23  */
24
25 #include <common.h>
26 #include <asm/processor.h>
27 #include <i2c.h>
28 #include <spd.h>
29 #include <asm/mmu.h>
30
31 #ifdef CONFIG_SPD_EEPROM
32
33 #define ns2clk(ns) ((ns) / (2000000000 /get_bus_freq(0) + 1) + 1)
34
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;
39         spd_eeprom_t spd;
40         unsigned int memsize,tmp,tmp1,tmp2;
41         unsigned char caslat;
42
43         i2c_read (SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd));
44
45         if ( spd.nrows > 2 ) {
46                 printf("DDR:Only two chip selects are supported on ADS.\n");
47                 return 0;
48         }
49
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");
52                 return 0;
53         }
54
55         ddr->cs0_bnds = ((spd.row_dens>>2) - 1);
56         ddr->cs0_config = ( 1<<31 | (spd.nrow_addr-12)<<8 | (spd.ncol_addr-8) );
57         debug ("\n");
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);
65         }
66
67         memsize = spd.nrows * (4 * spd.row_dens);
68         if( spd.mem_type != 0x07 ) {
69                 printf("No DDR module found!\n");
70                 return 0;
71         }
72
73         switch (memsize) {
74         case 16:
75                 tmp = 7;                /* TLB size */
76                 tmp1 = 1;               /* TLB entry number */
77                 tmp2 = 23;              /* Local Access Window size */
78                 break;
79         case 32:
80                 tmp = 7;
81                 tmp1 = 2;
82                 tmp2 = 24;
83                 break;
84         case 64:
85                 tmp = 8;
86                 tmp1 = 1;
87                 tmp2 = 25;
88                 break;
89         case 128:
90                 tmp = 8;
91                 tmp1 = 2;
92                 tmp2 = 26;
93                 break;
94         case 256:
95                 tmp = 9;
96                 tmp1 = 1;
97                 tmp2 = 27;
98                 break;
99         case 512:
100                 tmp = 9;
101                 tmp1 = 2;
102                 tmp2 = 28;
103                 break;
104         case 1024:
105                 tmp = 10;
106                 tmp1 = 1;
107                 tmp2 = 29;
108                 break;
109         default:
110                 printf ("DDR:we only added support 16M,32M,64M,128M,256M,512M and 1G DDR I.\n");
111                 return 0;
112                 break;
113         }
114
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));
127
128         if(tmp1 == 2) {
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));
142         }
143
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);
149 #else
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);
154 #endif
155
156         tmp = 20000/(((spd.clk_cycle & 0xF0) >> 4) * 10 + (spd.clk_cycle & 0x0f));
157         debug ("DDR:Module maximum data rate is: %dMhz\n",tmp);
158
159         /* find the largest CAS */
160         if(spd.cas_lat & 0x40) {
161                 caslat = 7;
162         } else if (spd.cas_lat & 0x20) {
163                 caslat = 6;
164         } else if (spd.cas_lat & 0x10) {
165                 caslat = 5;
166         } else if (spd.cas_lat & 0x08) {
167                 caslat = 4;
168         } else if (spd.cas_lat & 0x04) {
169                 caslat = 3;
170         } else if (spd.cas_lat & 0x02) {
171                 caslat = 2;
172         } else if (spd.cas_lat & 0x01) {
173                 caslat = 1;
174         } else {
175                 printf("DDR:no valid CAS Latency information.\n");
176                 return 0;
177         }
178
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");
194                 return 0;
195         }
196
197         /* note: caslat must also be programmed into ddr->sdram_mode
198            register */
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 ) | \
206                                 ( 0x300 ) | \
207                                 ((ns2clk(spd.trrd/4) & 0x07) << 4) | 1);
208
209         debug ("DDR:timing_cfg_1=0x%08x\n",ddr->timing_cfg_1);
210
211         ddr->timing_cfg_2 = 0x00000800;
212         debug ("DDR:timing_cfg_2=0x%08x\n",ddr->timing_cfg_2);
213
214         /* only DDR I is supported, DDR I and II have different mode-register-set definition */
215         /* burst length is always 4 */
216         switch(caslat) {
217         case 2:
218                 ddr->sdram_mode = 0x52; /* 1.5 */
219                 break;
220         case 3:
221                 ddr->sdram_mode = 0x22; /* 2.0 */
222                 break;
223         case 4:
224                 ddr->sdram_mode = 0x62; /* 2.5 */
225                 break;
226         case 5:
227                 ddr->sdram_mode = 0x32; /* 3.0 */
228                 break;
229         default:
230                 printf("DDR:only CAS Latency 1.5,2.0,2.5,3.0 is supported.\n");
231                 return 0;
232         }
233         debug ("DDR:sdram_mode=0x%08x\n",ddr->sdram_mode);
234
235         switch(spd.refresh) {
236         case 0x00:
237         case 0x80:
238                 tmp = ns2clk(15625);
239                 break;
240         case 0x01:
241         case 0x81:
242                 tmp = ns2clk(3900);
243                 break;
244         case 0x02:
245         case 0x82:
246                 tmp = ns2clk(7800);
247                 break;
248         case 0x03:
249         case 0x83:
250                 tmp = ns2clk(31300);
251                 break;
252         case 0x04:
253         case 0x84:
254                 tmp = ns2clk(62500);
255                 break;
256         case 0x05:
257         case 0x85:
258                 tmp = ns2clk(125000);
259                 break;
260         default:
261                 tmp = 0x512;
262                 break;
263         }
264
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);
268
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;
274         }
275         debug ("DDR:err_disable=0x%08x\n",ddr->err_disable);
276         debug ("DDR:err_sbe=0x%08x\n",ddr->err_sbe);
277 #endif
278         asm("sync;isync;msync");
279
280         udelay(500);
281
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)
286          */
287         ddr->sdram_clk_cntl = 0x83000000;
288 #endif
289
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.
294          *
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)
298          */
299         tmp = 0xc2000000;
300
301         /* sdram_cfg[3] = RD_EN - registered DIMM enable
302          *   A value of 0x26 indicates micron registered DIMMS (micron.com)
303          */
304         if (spd.mod_attr == 0x26) {
305                 tmp |= 0x10000000;
306         }
307
308 #if defined(CONFIG_DDR_ECC)
309         /* If the user wanted ECC (enabled via sdram_cfg[2]) */
310         if (spd.config == 0x02) {
311                 tmp |= 0x20000000;
312         }
313 #endif
314
315
316         /*
317          * REV1 uses 1T timing.
318          * REV2 may use 1T or 2T as configured by the user.
319          */
320         {
321                 uint pvr = get_pvr();
322
323                 if (pvr != PVR_85xx_REV1) {
324 #if defined(CONFIG_DDR_2T_TIMING)
325                         /*
326                          * Enable 2T timing by setting sdram_cfg[16].
327                          */
328                         tmp |= 0x8000;
329 #endif
330                 }
331         }
332
333         ddr->sdram_cfg = tmp;
334
335         asm("sync;isync;msync");
336
337         udelay(500);
338
339         debug ("DDR:sdram_cfg=0x%08x\n",ddr->sdram_cfg);
340
341         return (memsize*1024*1024);
342 }
343
344 #endif /* CONFIG_SPD_EEPROM */