3d7d003e94e2bdab98272789c009bea4763facf1
[oweals/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
34 #if defined(CONFIG_DDR_ECC)
35 extern void dma_init(void);
36 extern uint dma_check(void);
37 extern int dma_xfer(void *dest, uint count, void *src);
38 #endif
39
40
41 #ifndef CFG_READ_SPD
42 #define CFG_READ_SPD    i2c_read
43 #endif
44
45
46 /*
47  * Convert picoseconds into clock cycles (rounding up if needed).
48  */
49
50 int
51 picos_to_clk(int picos)
52 {
53         int clks;
54
55         clks = picos / (2000000000 / (get_bus_freq(0) / 1000));
56         if (picos % (2000000000 / (get_bus_freq(0) / 1000)) != 0) {
57                 clks++;
58         }
59
60         return clks;
61 }
62
63
64 unsigned int
65 banksize(unsigned char row_dens)
66 {
67         return ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
68 }
69
70
71 long int
72 spd_sdram(void)
73 {
74         volatile immap_t *immap = (immap_t *)CFG_IMMR;
75         volatile ccsr_ddr_t *ddr = &immap->im_ddr;
76         volatile ccsr_local_ecm_t *ecm = &immap->im_local_ecm;
77         spd_eeprom_t spd;
78         unsigned tmp, tmp1;
79         unsigned int memsize;
80         unsigned int tlb_size;
81         unsigned int law_size;
82         unsigned char caslat;
83         unsigned int ram_tlb_index;
84         unsigned int ram_tlb_address;
85
86         CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) & spd, sizeof (spd));
87
88         if (spd.nrows > 2) {
89                 puts("DDR:Only two chip selects are supported on ADS.\n");
90                 return 0;
91         }
92
93         if (spd.nrow_addr < 12
94             || spd.nrow_addr > 14
95             || spd.ncol_addr < 8
96             || spd.ncol_addr > 11) {
97                 puts("DDR:Row or Col number unsupported.\n");
98                 return 0;
99         }
100
101         ddr->cs0_bnds = (banksize(spd.row_dens) >> 24) - 1;
102         ddr->cs0_config = ( 1 << 31
103                             | (spd.nrow_addr - 12) << 8
104                             | (spd.ncol_addr - 8) );
105         debug("\n");
106         debug("cs0_bnds = 0x%08x\n",ddr->cs0_bnds);
107         debug("cs0_config = 0x%08x\n",ddr->cs0_config);
108
109         if (spd.nrows == 2) {
110                 ddr->cs1_bnds = ( (banksize(spd.row_dens) >> 8)
111                                   | ((banksize(spd.row_dens) >> 23) - 1) );
112                 ddr->cs1_config = ( 1<<31
113                                     | (spd.nrow_addr-12) << 8
114                                     | (spd.ncol_addr-8) );
115                 debug("cs1_bnds = 0x%08x\n",ddr->cs1_bnds);
116                 debug("cs1_config = 0x%08x\n",ddr->cs1_config);
117         }
118
119         if (spd.mem_type != 0x07) {
120                 puts("No DDR module found!\n");
121                 return 0;
122         }
123
124         /*
125          * Figure out memory size in Megabytes.
126          */
127         memsize = spd.nrows * banksize(spd.row_dens) / 0x100000;
128
129         /*
130          * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23. Fnord.
131          */
132         law_size = 19 + __ilog2(memsize);
133
134         /*
135          * Determine size of each TLB1 entry.
136          */
137         switch (memsize) {
138         case 16:
139         case 32:
140                 tlb_size = BOOKE_PAGESZ_16M;
141                 break;
142         case 64:
143         case 128:
144                 tlb_size = BOOKE_PAGESZ_64M;
145                 break;
146         case 256:
147         case 512:
148         case 1024:
149         case 2048:
150                 tlb_size = BOOKE_PAGESZ_256M;
151                 break;
152         default:
153                 puts("DDR: only 16M,32M,64M,128M,256M,512M,1G and 2G DDR I are supported.\n");
154                 return 0;
155                 break;
156         }
157
158         /*
159          * Configure DDR TLB1 entries.
160          * Starting at TLB1 8, use no more than 8 TLB1 entries.
161          */
162         ram_tlb_index = 8;
163         ram_tlb_address = (unsigned int)CFG_DDR_SDRAM_BASE;
164         while (ram_tlb_address < (memsize * 1024 * 1024)
165               && ram_tlb_index < 16) {
166                 mtspr(MAS0, TLB1_MAS0(1, ram_tlb_index, 0));
167                 mtspr(MAS1, TLB1_MAS1(1, 1, 0, 0, tlb_size));
168                 mtspr(MAS2, TLB1_MAS2(E500_TLB_EPN(ram_tlb_address),
169                                       0, 0, 0, 0, 0, 0, 0, 0));
170                 mtspr(MAS3, TLB1_MAS3(E500_TLB_RPN(ram_tlb_address),
171                                       0, 0, 0, 0, 0, 1, 0, 1, 0, 1));
172                 asm volatile("isync;msync;tlbwe;isync");
173
174                 debug("DDR:MAS0=0x%08x\n", TLB1_MAS0(1, ram_tlb_index, 0));
175                 debug("DDR:MAS1=0x%08x\n", TLB1_MAS1(1, 1, 0, 0, tlb_size));
176                 debug("DDR:MAS2=0x%08x\n",
177                       TLB1_MAS2(E500_TLB_EPN(ram_tlb_address),
178                                 0, 0, 0, 0, 0, 0, 0, 0));
179                 debug("DDR:MAS3=0x%08x\n",
180                       TLB1_MAS3(E500_TLB_RPN(ram_tlb_address),
181                                 0, 0, 0, 0, 0, 1, 0, 1, 0, 1));
182
183                 ram_tlb_address += (0x1000 << ((tlb_size - 1) * 2));
184                 ram_tlb_index++;
185         }
186
187         /*
188          * Set up LAWBAR for all of DDR.
189          */
190         ecm->lawbar1 = ((CFG_DDR_SDRAM_BASE>>12) & 0xfffff);
191         ecm->lawar1 = (LAWAR_EN | LAWAR_TRGT_IF_DDR | (LAWAR_SIZE & law_size));
192         debug("DDR:LAWBAR1=0x%08x\n", ecm->lawbar1);
193         debug("DDR:LARAR1=0x%08x\n", ecm->lawar1);
194
195         /*
196          * find the largest CAS
197          */
198         if(spd.cas_lat & 0x40) {
199                 caslat = 7;
200         } else if (spd.cas_lat & 0x20) {
201                 caslat = 6;
202         } else if (spd.cas_lat & 0x10) {
203                 caslat = 5;
204         } else if (spd.cas_lat & 0x08) {
205                 caslat = 4;
206         } else if (spd.cas_lat & 0x04) {
207                 caslat = 3;
208         } else if (spd.cas_lat & 0x02) {
209                 caslat = 2;
210         } else if (spd.cas_lat & 0x01) {
211                 caslat = 1;
212         } else {
213                 puts("DDR:no valid CAS Latency information.\n");
214                 return 0;
215         }
216
217         tmp = 20000 / (((spd.clk_cycle & 0xF0) >> 4) * 10
218                        + (spd.clk_cycle & 0x0f));
219         debug("DDR:Module maximum data rate is: %dMhz\n", tmp);
220
221         tmp1 = get_bus_freq(0) / 1000000;
222         if (tmp1 < 230 && tmp1 >= 90 && tmp >= 230) {
223                 /* 90~230 range, treated as DDR 200 */
224                 if (spd.clk_cycle3 == 0xa0)
225                         caslat -= 2;
226                 else if(spd.clk_cycle2 == 0xa0)
227                         caslat--;
228         } else if (tmp1 < 280 && tmp1 >= 230 && tmp >= 280) {
229                 /* 230-280 range, treated as DDR 266 */
230                 if (spd.clk_cycle3 == 0x75)
231                         caslat -= 2;
232                 else if (spd.clk_cycle2 == 0x75)
233                         caslat--;
234         } else if (tmp1 < 350 && tmp1 >= 280 && tmp >= 350) {
235                 /* 280~350 range, treated as DDR 333 */
236                 if (spd.clk_cycle3 == 0x60)
237                         caslat -= 2;
238                 else if (spd.clk_cycle2 == 0x60)
239                         caslat--;
240         } else if (tmp1 < 90 || tmp1 >= 350) {
241                 /* DDR rate out-of-range */
242                 puts("DDR:platform frequency is not fit for DDR rate\n");
243                 return 0;
244         }
245
246         /*
247          * note: caslat must also be programmed into ddr->sdram_mode
248          * register.
249          *
250          * note: WRREC(Twr) and WRTORD(Twtr) are not in SPD,
251          * use conservative value here.
252          */
253         ddr->timing_cfg_1 =
254             (((picos_to_clk(spd.trp * 250) & 0x07) << 28 ) |
255              ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24 ) |
256              ((picos_to_clk(spd.trcd * 250) & 0x07) << 20 ) |
257              ((caslat & 0x07) << 16 ) |
258              (((picos_to_clk(spd.sset[6] * 1000) - 8) & 0x0f) << 12 ) |
259              ( 0x300 ) |
260              ((picos_to_clk(spd.trrd * 250) & 0x07) << 4) | 1);
261
262         ddr->timing_cfg_2 = 0x00000800;
263
264         debug("DDR:timing_cfg_1=0x%08x\n", ddr->timing_cfg_1);
265         debug("DDR:timing_cfg_2=0x%08x\n", ddr->timing_cfg_2);
266
267         /*
268          * Only DDR I is supported
269          * DDR I and II have different mode-register-set definition
270          */
271
272         /* burst length is always 4 */
273         switch(caslat) {
274         case 2:
275                 ddr->sdram_mode = 0x52; /* 1.5 */
276                 break;
277         case 3:
278                 ddr->sdram_mode = 0x22; /* 2.0 */
279                 break;
280         case 4:
281                 ddr->sdram_mode = 0x62; /* 2.5 */
282                 break;
283         case 5:
284                 ddr->sdram_mode = 0x32; /* 3.0 */
285                 break;
286         default:
287                 puts("DDR:only CAS Latency 1.5, 2.0, 2.5, 3.0 is supported.\n");
288                 return 0;
289         }
290         debug("DDR:sdram_mode=0x%08x\n", ddr->sdram_mode);
291
292         switch(spd.refresh) {
293         case 0x00:
294         case 0x80:
295                 tmp = picos_to_clk(15625000);
296                 break;
297         case 0x01:
298         case 0x81:
299                 tmp = picos_to_clk(3900000);
300                 break;
301         case 0x02:
302         case 0x82:
303                 tmp = picos_to_clk(7800000);
304                 break;
305         case 0x03:
306         case 0x83:
307                 tmp = picos_to_clk(31300000);
308                 break;
309         case 0x04:
310         case 0x84:
311                 tmp = picos_to_clk(62500000);
312                 break;
313         case 0x05:
314         case 0x85:
315                 tmp = picos_to_clk(125000000);
316                 break;
317         default:
318                 tmp = 0x512;
319                 break;
320         }
321
322         /*
323          * Set BSTOPRE to 0x100 for page mode
324          * If auto-charge is used, set BSTOPRE = 0
325          */
326         ddr->sdram_interval = ((tmp & 0x3fff) << 16) | 0x100;
327         debug("DDR:sdram_interval=0x%08x\n", ddr->sdram_interval);
328
329         /*
330          * Is this an ECC DDR chip?
331          */
332 #if defined(CONFIG_DDR_ECC)
333         if (spd.config == 0x02) {
334                 ddr->err_disable = 0x0000000d;
335                 ddr->err_sbe = 0x00ff0000;
336         }
337         debug("DDR:err_disable=0x%08x\n", ddr->err_disable);
338         debug("DDR:err_sbe=0x%08x\n", ddr->err_sbe);
339 #endif
340         asm("sync;isync;msync");
341
342         udelay(500);
343
344 #ifdef MPC85xx_DDR_SDRAM_CLK_CNTL
345         /* Setup the clock control (8555 and later)
346          * SDRAM_CLK_CNTL[0] = Source synchronous enable == 1
347          * SDRAM_CLK_CNTL[5-7] = Clock Adjust == 3 (3/4 cycle late)
348          */
349         ddr->sdram_clk_cntl = 0x83000000;
350 #endif
351
352         /*
353          * Figure out the settings for the sdram_cfg register.  Build up
354          * the entire register in 'tmp' before writing since the write into
355          * the register will actually enable the memory controller, and all
356          * settings must be done before enabling.
357          *
358          * sdram_cfg[0]   = 1 (ddr sdram logic enable)
359          * sdram_cfg[1]   = 1 (self-refresh-enable)
360          * sdram_cfg[6:7] = 2 (SDRAM type = DDR SDRAM)
361          */
362         tmp = 0xc2000000;
363
364         /*
365          * sdram_cfg[3] = RD_EN - registered DIMM enable
366          *   A value of 0x26 indicates micron registered DIMMS (micron.com)
367          */
368         if (spd.mod_attr == 0x26) {
369                 tmp |= 0x10000000;
370         }
371
372 #if defined(CONFIG_DDR_ECC)
373         /*
374          * If the user wanted ECC (enabled via sdram_cfg[2])
375          */
376         if (spd.config == 0x02) {
377                 tmp |= 0x20000000;
378         }
379 #endif
380
381         /*
382          * REV1 uses 1T timing.
383          * REV2 may use 1T or 2T as configured by the user.
384          */
385         {
386                 uint pvr = get_pvr();
387
388                 if (pvr != PVR_85xx_REV1) {
389 #if defined(CONFIG_DDR_2T_TIMING)
390                         /*
391                          * Enable 2T timing by setting sdram_cfg[16].
392                          */
393                         tmp |= 0x8000;
394 #endif
395                 }
396         }
397
398         ddr->sdram_cfg = tmp;
399
400         asm("sync;isync;msync");
401         udelay(500);
402
403         debug("DDR:sdram_cfg=0x%08x\n", ddr->sdram_cfg);
404
405         return memsize * 1024 * 1024;
406 }
407
408 #endif /* CONFIG_SPD_EEPROM */
409
410
411 #if defined(CONFIG_DDR_ECC)
412 /*
413  * Initialize all of memory for ECC, then enable errors.
414  */
415
416 void
417 ddr_enable_ecc(unsigned int dram_size)
418 {
419         uint *p = 0;
420         uint i = 0;
421         volatile immap_t *immap = (immap_t *)CFG_IMMR;
422         volatile ccsr_ddr_t *ddr= &immap->im_ddr;
423
424         dma_init();
425
426         for (*p = 0; p < (uint *)(8 * 1024); p++) {
427                 if (((unsigned int)p & 0x1f) == 0) {
428                         ppcDcbz((unsigned long) p);
429                 }
430                 *p = (unsigned int)0xdeadbeef;
431                 if (((unsigned int)p & 0x1c) == 0x1c) {
432                         ppcDcbf((unsigned long) p);
433                 }
434         }
435
436         /* 8K */
437         dma_xfer((uint *)0x2000, 0x2000, (uint *)0);
438         /* 16K */
439         dma_xfer((uint *)0x4000, 0x4000, (uint *)0);
440         /* 32K */
441         dma_xfer((uint *)0x8000, 0x8000, (uint *)0);
442         /* 64K */
443         dma_xfer((uint *)0x10000, 0x10000, (uint *)0);
444         /* 128k */
445         dma_xfer((uint *)0x20000, 0x20000, (uint *)0);
446         /* 256k */
447         dma_xfer((uint *)0x40000, 0x40000, (uint *)0);
448         /* 512k */
449         dma_xfer((uint *)0x80000, 0x80000, (uint *)0);
450         /* 1M */
451         dma_xfer((uint *)0x100000, 0x100000, (uint *)0);
452         /* 2M */
453         dma_xfer((uint *)0x200000, 0x200000, (uint *)0);
454         /* 4M */
455         dma_xfer((uint *)0x400000, 0x400000, (uint *)0);
456
457         for (i = 1; i < dram_size / 0x800000; i++) {
458                 dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
459         }
460
461         /*
462          * Enable errors for ECC.
463          */
464         ddr->err_disable = 0x00000000;
465         asm("sync;isync;msync");
466 }
467
468 #endif  /* CONFIG_DDR_ECC */