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