85xx allow debugger to configure ddr.
[oweals/u-boot.git] / cpu / mpc85xx / spd_sdram.c
1 /*
2  * Copyright 2004, 2007 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
32 #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
33 extern void dma_init(void);
34 extern uint dma_check(void);
35 extern int dma_xfer(void *dest, uint count, void *src);
36 #endif
37
38 #ifdef CONFIG_SPD_EEPROM
39
40 #ifndef CFG_READ_SPD
41 #define CFG_READ_SPD    i2c_read
42 #endif
43
44 static unsigned int setup_laws_and_tlbs(unsigned int memsize);
45
46
47 /*
48  * Convert picoseconds into clock cycles (rounding up if needed).
49  */
50
51 int
52 picos_to_clk(int picos)
53 {
54         int clks;
55
56         clks = picos / (2000000000 / (get_bus_freq(0) / 1000));
57         if (picos % (2000000000 / (get_bus_freq(0) / 1000)) != 0) {
58                 clks++;
59         }
60
61         return clks;
62 }
63
64
65 /*
66  * Calculate the Density of each Physical Rank.
67  * Returned size is in bytes.
68  *
69  * Study these table from Byte 31 of JEDEC SPD Spec.
70  *
71  *              DDR I   DDR II
72  *      Bit     Size    Size
73  *      ---     -----   ------
74  *      7 high  512MB   512MB
75  *      6       256MB   256MB
76  *      5       128MB   128MB
77  *      4        64MB    16GB
78  *      3        32MB     8GB
79  *      2        16MB     4GB
80  *      1         2GB     2GB
81  *      0 low     1GB     1GB
82  *
83  * Reorder Table to be linear by stripping the bottom
84  * 2 or 5 bits off and shifting them up to the top.
85  */
86
87 unsigned int
88 compute_banksize(unsigned int mem_type, unsigned char row_dens)
89 {
90         unsigned int bsize;
91
92         if (mem_type == SPD_MEMTYPE_DDR) {
93                 /* Bottom 2 bits up to the top. */
94                 bsize = ((row_dens >> 2) | ((row_dens & 3) << 6)) << 24;
95                 debug("DDR: DDR I rank density = 0x%08x\n", bsize);
96         } else {
97                 /* Bottom 5 bits up to the top. */
98                 bsize = ((row_dens >> 5) | ((row_dens & 31) << 3)) << 27;
99                 debug("DDR: DDR II rank density = 0x%08x\n", bsize);
100         }
101         return bsize;
102 }
103
104
105 /*
106  * Convert a two-nibble BCD value into a cycle time.
107  * While the spec calls for nano-seconds, picos are returned.
108  *
109  * This implements the tables for bytes 9, 23 and 25 for both
110  * DDR I and II.  No allowance for distinguishing the invalid
111  * fields absent for DDR I yet present in DDR II is made.
112  * (That is, cycle times of .25, .33, .66 and .75 ns are
113  * allowed for both DDR II and I.)
114  */
115
116 unsigned int
117 convert_bcd_tenths_to_cycle_time_ps(unsigned int spd_val)
118 {
119         /*
120          * Table look up the lower nibble, allow DDR I & II.
121          */
122         unsigned int tenths_ps[16] = {
123                 0,
124                 100,
125                 200,
126                 300,
127                 400,
128                 500,
129                 600,
130                 700,
131                 800,
132                 900,
133                 250,
134                 330,
135                 660,
136                 750,
137                 0,      /* undefined */
138                 0       /* undefined */
139         };
140
141         unsigned int whole_ns = (spd_val & 0xF0) >> 4;
142         unsigned int tenth_ns = spd_val & 0x0F;
143         unsigned int ps = whole_ns * 1000 + tenths_ps[tenth_ns];
144
145         return ps;
146 }
147
148
149 /*
150  * Determine Refresh Rate.  Ignore self refresh bit on DDR I.
151  * Table from SPD Spec, Byte 12, converted to picoseconds and
152  * filled in with "default" normal values.
153  */
154 unsigned int determine_refresh_rate(unsigned int spd_refresh)
155 {
156         unsigned int refresh_time_ns[8] = {
157                 15625000,       /* 0 Normal    1.00x */
158                 3900000,        /* 1 Reduced    .25x */
159                 7800000,        /* 2 Extended   .50x */
160                 31300000,       /* 3 Extended  2.00x */
161                 62500000,       /* 4 Extended  4.00x */
162                 125000000,      /* 5 Extended  8.00x */
163                 15625000,       /* 6 Normal    1.00x  filler */
164                 15625000,       /* 7 Normal    1.00x  filler */
165         };
166
167         return picos_to_clk(refresh_time_ns[spd_refresh & 0x7]);
168 }
169
170
171 long int
172 spd_sdram(void)
173 {
174         volatile immap_t *immap = (immap_t *)CFG_IMMR;
175         volatile ccsr_ddr_t *ddr = &immap->im_ddr;
176         spd_eeprom_t spd;
177         unsigned int n_ranks;
178         unsigned int rank_density;
179         unsigned int odt_rd_cfg, odt_wr_cfg;
180         unsigned int odt_cfg, mode_odt_enable;
181         unsigned int refresh_clk;
182 #ifdef MPC85xx_DDR_SDRAM_CLK_CNTL
183         unsigned char clk_adjust;
184 #endif
185         unsigned int dqs_cfg;
186         unsigned char twr_clk, twtr_clk, twr_auto_clk;
187         unsigned int tCKmin_ps, tCKmax_ps;
188         unsigned int max_data_rate, effective_data_rate;
189         unsigned int busfreq;
190         unsigned sdram_cfg;
191         unsigned int memsize = 0;
192         unsigned char caslat, caslat_ctrl;
193         unsigned int trfc, trfc_clk, trfc_low, trfc_high;
194         unsigned int trcd_clk;
195         unsigned int trtp_clk;
196         unsigned char cke_min_clk;
197         unsigned char add_lat;
198         unsigned char wr_lat;
199         unsigned char wr_data_delay;
200         unsigned char four_act;
201         unsigned char cpo;
202         unsigned char burst_len;
203         unsigned int mode_caslat;
204         unsigned char sdram_type;
205         unsigned char d_init;
206         unsigned int bnds;
207
208         /*
209          * Skip configuration if already configured.
210          * memsize is determined from last configured chip select.
211          */
212         if (ddr->cs0_config & 0x80000000) {
213                 debug(" cs0 already configured, bnds=%x\n",ddr->cs0_bnds);
214                 bnds = 0xfff & ddr->cs0_bnds;
215                 if (bnds < 0xff) { /* do not add if at top of 4G */
216                         memsize = (bnds + 1) << 4;
217                 }
218         }
219         if (ddr->cs1_config & 0x80000000) {
220                 debug(" cs1 already configured, bnds=%x\n",ddr->cs1_bnds);
221                 bnds = 0xfff & ddr->cs1_bnds;
222                 if (bnds < 0xff) { /* do not add if at top of 4G */
223                         memsize = (bnds + 1) << 4; /* assume ordered bnds */
224                 }
225         }
226         if (ddr->cs2_config & 0x80000000) {
227                 debug(" cs2 already configured, bnds=%x\n",ddr->cs2_bnds);
228                 bnds = 0xfff & ddr->cs2_bnds;
229                 if (bnds < 0xff) { /* do not add if at top of 4G */
230                         memsize = (bnds + 1) << 4;
231                 }
232         }
233         if (ddr->cs3_config & 0x80000000) {
234                 debug(" cs3 already configured, bnds=%x\n",ddr->cs3_bnds);
235                 bnds = 0xfff & ddr->cs3_bnds;
236                 if (bnds < 0xff) { /* do not add if at top of 4G */
237                         memsize = (bnds + 1) << 4;
238                 }
239         }
240
241         if (memsize) {
242                 printf("       Reusing current %dMB configuration\n",memsize);
243                 memsize = setup_laws_and_tlbs(memsize);
244                 return memsize << 20;
245         }
246
247         /*
248          * Read SPD information.
249          */
250         CFG_READ_SPD(SPD_EEPROM_ADDRESS, 0, 1, (uchar *) &spd, sizeof(spd));
251
252         /*
253          * Check for supported memory module types.
254          */
255         if (spd.mem_type != SPD_MEMTYPE_DDR &&
256             spd.mem_type != SPD_MEMTYPE_DDR2) {
257                 printf("Unable to locate DDR I or DDR II module.\n"
258                        "    Fundamental memory type is 0x%0x\n",
259                        spd.mem_type);
260                 return 0;
261         }
262
263         /*
264          * These test gloss over DDR I and II differences in interpretation
265          * of bytes 3 and 4, but irrelevantly.  Multiple asymmetric banks
266          * are not supported on DDR I; and not encoded on DDR II.
267          *
268          * Also note that the 8548 controller can support:
269          *    12 <= nrow <= 16
270          * and
271          *     8 <= ncol <= 11 (still, for DDR)
272          *     6 <= ncol <=  9 (for FCRAM)
273          */
274         if (spd.nrow_addr < 12 || spd.nrow_addr > 14) {
275                 printf("DDR: Unsupported number of Row Addr lines: %d.\n",
276                        spd.nrow_addr);
277                 return 0;
278         }
279         if (spd.ncol_addr < 8 || spd.ncol_addr > 11) {
280                 printf("DDR: Unsupported number of Column Addr lines: %d.\n",
281                        spd.ncol_addr);
282                 return 0;
283         }
284
285         /*
286          * Determine the number of physical banks controlled by
287          * different Chip Select signals.  This is not quite the
288          * same as the number of DIMM modules on the board.  Feh.
289          */
290         if (spd.mem_type == SPD_MEMTYPE_DDR) {
291                 n_ranks = spd.nrows;
292         } else {
293                 n_ranks = (spd.nrows & 0x7) + 1;
294         }
295
296         debug("DDR: number of ranks = %d\n", n_ranks);
297
298         if (n_ranks > 2) {
299                 printf("DDR: Only 2 chip selects are supported: %d\n",
300                        n_ranks);
301                 return 0;
302         }
303
304 #ifdef CONFIG_MPC8548
305         /*
306          * Adjust DDR II IO voltage biasing.
307          * Only 8548 rev 1 needs the fix
308          */
309         if ((SVR_VER(get_svr()) == SVR_8548_E) &&
310                         (SVR_MJREV(get_svr()) == 1) &&
311                         (spd.mem_type == SPD_MEMTYPE_DDR2)) {
312                 volatile ccsr_gur_t *gur = &immap->im_gur;
313                 gur->ddrioovcr = (0x80000000    /* Enable */
314                                   | 0x10000000);/* VSEL to 1.8V */
315         }
316 #endif
317
318         /*
319          * Determine the size of each Rank in bytes.
320          */
321         rank_density = compute_banksize(spd.mem_type, spd.row_dens);
322
323
324         /*
325          * Eg: Bounds: 0x0000_0000 to 0x0f000_0000      first 256 Meg
326          */
327         ddr->cs0_bnds = (rank_density >> 24) - 1;
328
329         /*
330          * ODT configuration recommendation from DDR Controller Chapter.
331          */
332         odt_rd_cfg = 0;                 /* Never assert ODT */
333         odt_wr_cfg = 0;                 /* Never assert ODT */
334         if (spd.mem_type == SPD_MEMTYPE_DDR2) {
335                 odt_wr_cfg = 1;         /* Assert ODT on writes to CS0 */
336 #if 0
337                 /* FIXME: How to determine the number of dimm modules? */
338                 if (n_dimm_modules == 2) {
339                         odt_rd_cfg = 1; /* Assert ODT on reads to CS0 */
340                 }
341 #endif
342         }
343
344         ddr->cs0_config = ( 1 << 31
345                             | (odt_rd_cfg << 20)
346                             | (odt_wr_cfg << 16)
347                             | (spd.nrow_addr - 12) << 8
348                             | (spd.ncol_addr - 8) );
349         debug("\n");
350         debug("DDR: cs0_bnds   = 0x%08x\n", ddr->cs0_bnds);
351         debug("DDR: cs0_config = 0x%08x\n", ddr->cs0_config);
352
353         if (n_ranks == 2) {
354                 /*
355                  * Eg: Bounds: 0x0f00_0000 to 0x1e0000_0000, second 256 Meg
356                  */
357                 ddr->cs1_bnds = ( (rank_density >> 8)
358                                   | ((rank_density >> (24 - 1)) - 1) );
359                 ddr->cs1_config = ( 1<<31
360                                     | (odt_rd_cfg << 20)
361                                     | (odt_wr_cfg << 16)
362                                     | (spd.nrow_addr - 12) << 8
363                                     | (spd.ncol_addr - 8) );
364                 debug("DDR: cs1_bnds   = 0x%08x\n", ddr->cs1_bnds);
365                 debug("DDR: cs1_config = 0x%08x\n", ddr->cs1_config);
366         }
367
368
369         /*
370          * Find the largest CAS by locating the highest 1 bit
371          * in the spd.cas_lat field.  Translate it to a DDR
372          * controller field value:
373          *
374          *      CAS Lat DDR I   DDR II  Ctrl
375          *      Clocks  SPD Bit SPD Bit Value
376          *      ------- ------- ------- -----
377          *      1.0     0               0001
378          *      1.5     1               0010
379          *      2.0     2       2       0011
380          *      2.5     3               0100
381          *      3.0     4       3       0101
382          *      3.5     5               0110
383          *      4.0             4       0111
384          *      4.5                     1000
385          *      5.0             5       1001
386          */
387         caslat = __ilog2(spd.cas_lat);
388         if ((spd.mem_type == SPD_MEMTYPE_DDR)
389             && (caslat > 5)) {
390                 printf("DDR I: Invalid SPD CAS Latency: 0x%x.\n", spd.cas_lat);
391                 return 0;
392
393         } else if (spd.mem_type == SPD_MEMTYPE_DDR2
394                    && (caslat < 2 || caslat > 5)) {
395                 printf("DDR II: Invalid SPD CAS Latency: 0x%x.\n",
396                        spd.cas_lat);
397                 return 0;
398         }
399         debug("DDR: caslat SPD bit is %d\n", caslat);
400
401         /*
402          * Calculate the Maximum Data Rate based on the Minimum Cycle time.
403          * The SPD clk_cycle field (tCKmin) is measured in tenths of
404          * nanoseconds and represented as BCD.
405          */
406         tCKmin_ps = convert_bcd_tenths_to_cycle_time_ps(spd.clk_cycle);
407         debug("DDR: tCKmin = %d ps\n", tCKmin_ps);
408
409         /*
410          * Double-data rate, scaled 1000 to picoseconds, and back down to MHz.
411          */
412         max_data_rate = 2 * 1000 * 1000 / tCKmin_ps;
413         debug("DDR: Module max data rate = %d Mhz\n", max_data_rate);
414
415
416         /*
417          * Adjust the CAS Latency to allow for bus speeds that
418          * are slower than the DDR module.
419          */
420         busfreq = get_bus_freq(0) / 1000000;    /* MHz */
421
422         effective_data_rate = max_data_rate;
423         if (busfreq < 90) {
424                 /* DDR rate out-of-range */
425                 puts("DDR: platform frequency is not fit for DDR rate\n");
426                 return 0;
427
428         } else if (90 <= busfreq && busfreq < 230 && max_data_rate >= 230) {
429                 /*
430                  * busfreq 90~230 range, treated as DDR 200.
431                  */
432                 effective_data_rate = 200;
433                 if (spd.clk_cycle3 == 0xa0)     /* 10 ns */
434                         caslat -= 2;
435                 else if (spd.clk_cycle2 == 0xa0)
436                         caslat--;
437
438         } else if (230 <= busfreq && busfreq < 280 && max_data_rate >= 280) {
439                 /*
440                  * busfreq 230~280 range, treated as DDR 266.
441                  */
442                 effective_data_rate = 266;
443                 if (spd.clk_cycle3 == 0x75)     /* 7.5 ns */
444                         caslat -= 2;
445                 else if (spd.clk_cycle2 == 0x75)
446                         caslat--;
447
448         } else if (280 <= busfreq && busfreq < 350 && max_data_rate >= 350) {
449                 /*
450                  * busfreq 280~350 range, treated as DDR 333.
451                  */
452                 effective_data_rate = 333;
453                 if (spd.clk_cycle3 == 0x60)     /* 6.0 ns */
454                         caslat -= 2;
455                 else if (spd.clk_cycle2 == 0x60)
456                         caslat--;
457
458         } else if (350 <= busfreq && busfreq < 460 && max_data_rate >= 460) {
459                 /*
460                  * busfreq 350~460 range, treated as DDR 400.
461                  */
462                 effective_data_rate = 400;
463                 if (spd.clk_cycle3 == 0x50)     /* 5.0 ns */
464                         caslat -= 2;
465                 else if (spd.clk_cycle2 == 0x50)
466                         caslat--;
467
468         } else if (460 <= busfreq && busfreq < 560 && max_data_rate >= 560) {
469                 /*
470                  * busfreq 460~560 range, treated as DDR 533.
471                  */
472                 effective_data_rate = 533;
473                 if (spd.clk_cycle3 == 0x3D)     /* 3.75 ns */
474                         caslat -= 2;
475                 else if (spd.clk_cycle2 == 0x3D)
476                         caslat--;
477
478         } else if (560 <= busfreq && busfreq < 700 && max_data_rate >= 700) {
479                 /*
480                  * busfreq 560~700 range, treated as DDR 667.
481                  */
482                 effective_data_rate = 667;
483                 if (spd.clk_cycle3 == 0x30)     /* 3.0 ns */
484                         caslat -= 2;
485                 else if (spd.clk_cycle2 == 0x30)
486                         caslat--;
487
488         } else if (700 <= busfreq) {
489                 /*
490                  * DDR rate out-of-range
491                  */
492                 printf("DDR: Bus freq %d MHz is not fit for DDR rate %d MHz\n",
493                      busfreq, max_data_rate);
494                 return 0;
495         }
496
497
498         /*
499          * Convert caslat clocks to DDR controller value.
500          * Force caslat_ctrl to be DDR Controller field-sized.
501          */
502         if (spd.mem_type == SPD_MEMTYPE_DDR) {
503                 caslat_ctrl = (caslat + 1) & 0x07;
504         } else {
505                 caslat_ctrl =  (2 * caslat - 1) & 0x0f;
506         }
507
508         debug("DDR: effective data rate is %d MHz\n", effective_data_rate);
509         debug("DDR: caslat SPD bit is %d, controller field is 0x%x\n",
510               caslat, caslat_ctrl);
511
512         /*
513          * Timing Config 0.
514          * Avoid writing for DDR I.  The new PQ38 DDR controller
515          * dreams up non-zero default values to be backwards compatible.
516          */
517         if (spd.mem_type == SPD_MEMTYPE_DDR2) {
518                 unsigned char taxpd_clk = 8;            /* By the book. */
519                 unsigned char tmrd_clk = 2;             /* By the book. */
520                 unsigned char act_pd_exit = 2;          /* Empirical? */
521                 unsigned char pre_pd_exit = 6;          /* Empirical? */
522
523                 ddr->timing_cfg_0 = (0
524                         | ((act_pd_exit & 0x7) << 20)   /* ACT_PD_EXIT */
525                         | ((pre_pd_exit & 0x7) << 16)   /* PRE_PD_EXIT */
526                         | ((taxpd_clk & 0xf) << 8)      /* ODT_PD_EXIT */
527                         | ((tmrd_clk & 0xf) << 0)       /* MRS_CYC */
528                         );
529 #if 0
530                 ddr->timing_cfg_0 |= 0xaa000000;        /* extra cycles */
531 #endif
532                 debug("DDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
533
534         } else {
535 #if 0
536                 /*
537                  * Force extra cycles with 0xaa bits.
538                  * Incidentally supply the dreamt-up backwards compat value!
539                  */
540                 ddr->timing_cfg_0 = 0x00110105; /* backwards compat value */
541                 ddr->timing_cfg_0 |= 0xaa000000;        /* extra cycles */
542                 debug("DDR: HACK timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
543 #endif
544         }
545
546
547         /*
548          * Some Timing Config 1 values now.
549          * Sneak Extended Refresh Recovery in here too.
550          */
551
552         /*
553          * For DDR I, WRREC(Twr) and WRTORD(Twtr) are not in SPD,
554          * use conservative value.
555          * For DDR II, they are bytes 36 and 37, in quarter nanos.
556          */
557
558         if (spd.mem_type == SPD_MEMTYPE_DDR) {
559                 twr_clk = 3;    /* Clocks */
560                 twtr_clk = 1;   /* Clocks */
561         } else {
562                 twr_clk = picos_to_clk(spd.twr * 250);
563                 twtr_clk = picos_to_clk(spd.twtr * 250);
564         }
565
566         /*
567          * Calculate Trfc, in picos.
568          * DDR I:  Byte 42 straight up in ns.
569          * DDR II: Byte 40 and 42 swizzled some, in ns.
570          */
571         if (spd.mem_type == SPD_MEMTYPE_DDR) {
572                 trfc = spd.trfc * 1000;         /* up to ps */
573         } else {
574                 unsigned int byte40_table_ps[8] = {
575                         0,
576                         250,
577                         330,
578                         500,
579                         660,
580                         750,
581                         0,
582                         0
583                 };
584
585                 trfc = (((spd.trctrfc_ext & 0x1) * 256) + spd.trfc) * 1000
586                         + byte40_table_ps[(spd.trctrfc_ext >> 1) & 0x7];
587         }
588         trfc_clk = picos_to_clk(trfc);
589
590         /*
591          * Trcd, Byte 29, from quarter nanos to ps and clocks.
592          */
593         trcd_clk = picos_to_clk(spd.trcd * 250) & 0x7;
594
595         /*
596          * Convert trfc_clk to DDR controller fields.  DDR I should
597          * fit in the REFREC field (16-19) of TIMING_CFG_1, but the
598          * 8548 controller has an extended REFREC field of three bits.
599          * The controller automatically adds 8 clocks to this value,
600          * so preadjust it down 8 first before splitting it up.
601          */
602         trfc_low = (trfc_clk - 8) & 0xf;
603         trfc_high = ((trfc_clk - 8) >> 4) & 0x3;
604
605         /*
606          * Sneak in some Extended Refresh Recovery.
607          */
608         ddr->ext_refrec = (trfc_high << 16);
609         debug("DDR: ext_refrec = 0x%08x\n", ddr->ext_refrec);
610
611         ddr->timing_cfg_1 =
612             (0
613              | ((picos_to_clk(spd.trp * 250) & 0x07) << 28)     /* PRETOACT */
614              | ((picos_to_clk(spd.tras * 1000) & 0x0f ) << 24)  /* ACTTOPRE */
615              | (trcd_clk << 20)                                 /* ACTTORW */
616              | (caslat_ctrl << 16)                              /* CASLAT */
617              | (trfc_low << 12)                                 /* REFEC */
618              | ((twr_clk & 0x07) << 8)                          /* WRRREC */
619              | ((picos_to_clk(spd.trrd * 250) & 0x07) << 4)     /* ACTTOACT */
620              | ((twtr_clk & 0x07) << 0)                         /* WRTORD */
621              );
622
623         debug("DDR: timing_cfg_1  = 0x%08x\n", ddr->timing_cfg_1);
624
625
626         /*
627          * Timing_Config_2
628          * Was: 0x00000800;
629          */
630
631         /*
632          * Additive Latency
633          * For DDR I, 0.
634          * For DDR II, with ODT enabled, use "a value" less than ACTTORW,
635          * which comes from Trcd, and also note that:
636          *      add_lat + caslat must be >= 4
637          */
638         add_lat = 0;
639         if (spd.mem_type == SPD_MEMTYPE_DDR2
640             && (odt_wr_cfg || odt_rd_cfg)
641             && (caslat < 4)) {
642                 add_lat = 4 - caslat;
643                 if (add_lat > trcd_clk) {
644                         add_lat = trcd_clk - 1;
645                 }
646         }
647
648         /*
649          * Write Data Delay
650          * Historically 0x2 == 4/8 clock delay.
651          * Empirically, 0x3 == 6/8 clock delay is suggested for DDR I 266.
652          */
653         wr_data_delay = 3;
654
655         /*
656          * Write Latency
657          * Read to Precharge
658          * Minimum CKE Pulse Width.
659          * Four Activate Window
660          */
661         if (spd.mem_type == SPD_MEMTYPE_DDR) {
662                 /*
663                  * This is a lie.  It should really be 1, but if it is
664                  * set to 1, bits overlap into the old controller's
665                  * otherwise unused ACSM field.  If we leave it 0, then
666                  * the HW will magically treat it as 1 for DDR 1.  Oh Yea.
667                  */
668                 wr_lat = 0;
669
670                 trtp_clk = 2;           /* By the book. */
671                 cke_min_clk = 1;        /* By the book. */
672                 four_act = 1;           /* By the book. */
673
674         } else {
675                 wr_lat = caslat - 1;
676
677                 /* Convert SPD value from quarter nanos to picos. */
678                 trtp_clk = picos_to_clk(spd.trtp * 250);
679
680                 cke_min_clk = 3;        /* By the book. */
681                 four_act = picos_to_clk(37500); /* By the book. 1k pages? */
682         }
683
684         /*
685          * Empirically set ~MCAS-to-preamble override for DDR 2.
686          * Your milage will vary.
687          */
688         cpo = 0;
689         if (spd.mem_type == SPD_MEMTYPE_DDR2) {
690                 if (effective_data_rate == 266 || effective_data_rate == 333) {
691                         cpo = 0x7;              /* READ_LAT + 5/4 */
692                 } else if (effective_data_rate == 400) {
693                         cpo = 0x9;              /* READ_LAT + 7/4 */
694                 } else {
695                         /* Pure speculation */
696                         cpo = 0xb;
697                 }
698         }
699
700         ddr->timing_cfg_2 = (0
701                 | ((add_lat & 0x7) << 28)               /* ADD_LAT */
702                 | ((cpo & 0x1f) << 23)                  /* CPO */
703                 | ((wr_lat & 0x7) << 19)                /* WR_LAT */
704                 | ((trtp_clk & 0x7) << 13)              /* RD_TO_PRE */
705                 | ((wr_data_delay & 0x7) << 10)         /* WR_DATA_DELAY */
706                 | ((cke_min_clk & 0x7) << 6)            /* CKE_PLS */
707                 | ((four_act & 0x1f) << 0)              /* FOUR_ACT */
708                 );
709
710         debug("DDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
711
712
713         /*
714          * Determine the Mode Register Set.
715          *
716          * This is nominally part specific, but it appears to be
717          * consistent for all DDR I devices, and for all DDR II devices.
718          *
719          *     caslat must be programmed
720          *     burst length is always 4
721          *     burst type is sequential
722          *
723          * For DDR I:
724          *     operating mode is "normal"
725          *
726          * For DDR II:
727          *     other stuff
728          */
729
730         mode_caslat = 0;
731
732         /*
733          * Table lookup from DDR I or II Device Operation Specs.
734          */
735         if (spd.mem_type == SPD_MEMTYPE_DDR) {
736                 if (1 <= caslat && caslat <= 4) {
737                         unsigned char mode_caslat_table[4] = {
738                                 0x5,    /* 1.5 clocks */
739                                 0x2,    /* 2.0 clocks */
740                                 0x6,    /* 2.5 clocks */
741                                 0x3     /* 3.0 clocks */
742                         };
743                         mode_caslat = mode_caslat_table[caslat - 1];
744                 } else {
745                         puts("DDR I: Only CAS Latencies of 1.5, 2.0, "
746                              "2.5 and 3.0 clocks are supported.\n");
747                         return 0;
748                 }
749
750         } else {
751                 if (2 <= caslat && caslat <= 5) {
752                         mode_caslat = caslat;
753                 } else {
754                         puts("DDR II: Only CAS Latencies of 2.0, 3.0, "
755                              "4.0 and 5.0 clocks are supported.\n");
756                         return 0;
757                 }
758         }
759
760         /*
761          * Encoded Burst Lenght of 4.
762          */
763         burst_len = 2;                  /* Fiat. */
764
765         if (spd.mem_type == SPD_MEMTYPE_DDR) {
766                 twr_auto_clk = 0;       /* Historical */
767         } else {
768                 /*
769                  * Determine tCK max in picos.  Grab tWR and convert to picos.
770                  * Auto-precharge write recovery is:
771                  *      WR = roundup(tWR_ns/tCKmax_ns).
772                  *
773                  * Ponder: Is twr_auto_clk different than twr_clk?
774                  */
775                 tCKmax_ps = convert_bcd_tenths_to_cycle_time_ps(spd.tckmax);
776                 twr_auto_clk = (spd.twr * 250 + tCKmax_ps - 1) / tCKmax_ps;
777         }
778
779
780         /*
781          * Mode Reg in bits 16 ~ 31,
782          * Extended Mode Reg 1 in bits 0 ~ 15.
783          */
784         mode_odt_enable = 0x0;                  /* Default disabled */
785         if (odt_wr_cfg || odt_rd_cfg) {
786                 /*
787                  * Bits 6 and 2 in Extended MRS(1)
788                  * Bit 2 == 0x04 == 75 Ohm, with 2 DIMM modules.
789                  * Bit 6 == 0x40 == 150 Ohm, with 1 DIMM module.
790                  */
791                 mode_odt_enable = 0x40;         /* 150 Ohm */
792         }
793
794         ddr->sdram_mode =
795                 (0
796                  | (add_lat << (16 + 3))        /* Additive Latency in EMRS1 */
797                  | (mode_odt_enable << 16)      /* ODT Enable in EMRS1 */
798                  | (twr_auto_clk << 9)          /* Write Recovery Autopre */
799                  | (mode_caslat << 4)           /* caslat */
800                  | (burst_len << 0)             /* Burst length */
801                  );
802
803         debug("DDR: sdram_mode   = 0x%08x\n", ddr->sdram_mode);
804
805
806         /*
807          * Clear EMRS2 and EMRS3.
808          */
809         ddr->sdram_mode_2 = 0;
810         debug("DDR: sdram_mode_2 = 0x%08x\n", ddr->sdram_mode_2);
811
812         /*
813          * Determine Refresh Rate.
814          */
815         refresh_clk = determine_refresh_rate(spd.refresh & 0x7);
816
817         /*
818          * Set BSTOPRE to 0x100 for page mode
819          * If auto-charge is used, set BSTOPRE = 0
820          */
821         ddr->sdram_interval =
822             (0
823              | (refresh_clk & 0x3fff) << 16
824              | 0x100
825              );
826         debug("DDR: sdram_interval = 0x%08x\n", ddr->sdram_interval);
827
828         /*
829          * Is this an ECC DDR chip?
830          * But don't mess with it if the DDR controller will init mem.
831          */
832 #ifdef CONFIG_DDR_ECC
833         if (spd.config == 0x02) {
834 #ifndef CONFIG_ECC_INIT_VIA_DDRCONTROLLER
835                 ddr->err_disable = 0x0000000d;
836 #endif
837                 ddr->err_sbe = 0x00ff0000;
838         }
839
840         debug("DDR: err_disable = 0x%08x\n", ddr->err_disable);
841         debug("DDR: err_sbe = 0x%08x\n", ddr->err_sbe);
842 #endif /* CONFIG_DDR_ECC */
843
844         asm("sync;isync;msync");
845         udelay(500);
846
847         /*
848          * SDRAM Cfg 2
849          */
850
851         /*
852          * When ODT is enabled, Chap 9 suggests asserting ODT to
853          * internal IOs only during reads.
854          */
855         odt_cfg = 0;
856         if (odt_rd_cfg | odt_wr_cfg) {
857                 odt_cfg = 0x2;          /* ODT to IOs during reads */
858         }
859
860         /*
861          * Try to use differential DQS with DDR II.
862          */
863         if (spd.mem_type == SPD_MEMTYPE_DDR) {
864                 dqs_cfg = 0;            /* No Differential DQS for DDR I */
865         } else {
866                 dqs_cfg = 0x1;          /* Differential DQS for DDR II */
867         }
868
869 #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
870         /*
871          * Use the DDR controller to auto initialize memory.
872          */
873         d_init = 1;
874         ddr->sdram_data_init = CONFIG_MEM_INIT_VALUE;
875         debug("DDR: ddr_data_init = 0x%08x\n", ddr->sdram_data_init);
876 #else
877         /*
878          * Memory will be initialized via DMA, or not at all.
879          */
880         d_init = 0;
881 #endif
882
883         ddr->sdram_cfg_2 = (0
884                             | (dqs_cfg << 26)   /* Differential DQS */
885                             | (odt_cfg << 21)   /* ODT */
886                             | (d_init << 4)     /* D_INIT auto init DDR */
887                             );
888
889         debug("DDR: sdram_cfg_2  = 0x%08x\n", ddr->sdram_cfg_2);
890
891
892 #ifdef MPC85xx_DDR_SDRAM_CLK_CNTL
893         /*
894          * Setup the clock control.
895          * SDRAM_CLK_CNTL[0] = Source synchronous enable == 1
896          * SDRAM_CLK_CNTL[5-7] = Clock Adjust
897          *      0110    3/4 cycle late
898          *      0111    7/8 cycle late
899          */
900         if (spd.mem_type == SPD_MEMTYPE_DDR)
901                 clk_adjust = 0x6;
902         else
903                 clk_adjust = 0x7;
904
905         ddr->sdram_clk_cntl = (0
906                                | 0x80000000
907                                | (clk_adjust << 23)
908                                );
909         debug("DDR: sdram_clk_cntl = 0x%08x\n", ddr->sdram_clk_cntl);
910 #endif
911
912         /*
913          * Figure out the settings for the sdram_cfg register.
914          * Build up the entire register in 'sdram_cfg' before writing
915          * since the write into the register will actually enable the
916          * memory controller; all settings must be done before enabling.
917          *
918          * sdram_cfg[0]   = 1 (ddr sdram logic enable)
919          * sdram_cfg[1]   = 1 (self-refresh-enable)
920          * sdram_cfg[5:7] = (SDRAM type = DDR SDRAM)
921          *                      010 DDR 1 SDRAM
922          *                      011 DDR 2 SDRAM
923          */
924         sdram_type = (spd.mem_type == SPD_MEMTYPE_DDR) ? 2 : 3;
925         sdram_cfg = (0
926                      | (1 << 31)                        /* Enable */
927                      | (1 << 30)                        /* Self refresh */
928                      | (sdram_type << 24)               /* SDRAM type */
929                      );
930
931         /*
932          * sdram_cfg[3] = RD_EN - registered DIMM enable
933          *   A value of 0x26 indicates micron registered DIMMS (micron.com)
934          */
935         if (spd.mem_type == SPD_MEMTYPE_DDR && spd.mod_attr == 0x26) {
936                 sdram_cfg |= 0x10000000;                /* RD_EN */
937         }
938
939 #if defined(CONFIG_DDR_ECC)
940         /*
941          * If the user wanted ECC (enabled via sdram_cfg[2])
942          */
943         if (spd.config == 0x02) {
944                 sdram_cfg |= 0x20000000;                /* ECC_EN */
945         }
946 #endif
947
948         /*
949          * REV1 uses 1T timing.
950          * REV2 may use 1T or 2T as configured by the user.
951          */
952         {
953                 uint pvr = get_pvr();
954
955                 if (pvr != PVR_85xx_REV1) {
956 #if defined(CONFIG_DDR_2T_TIMING)
957                         /*
958                          * Enable 2T timing by setting sdram_cfg[16].
959                          */
960                         sdram_cfg |= 0x8000;            /* 2T_EN */
961 #endif
962                 }
963         }
964
965         /*
966          * 200 painful micro-seconds must elapse between
967          * the DDR clock setup and the DDR config enable.
968          */
969         udelay(200);
970
971         /*
972          * Go!
973          */
974         ddr->sdram_cfg = sdram_cfg;
975
976         asm("sync;isync;msync");
977         udelay(500);
978
979         debug("DDR: sdram_cfg   = 0x%08x\n", ddr->sdram_cfg);
980
981
982 #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
983         /*
984          * Poll until memory is initialized.
985          * 512 Meg at 400 might hit this 200 times or so.
986          */
987         while ((ddr->sdram_cfg_2 & (d_init << 4)) != 0) {
988                 udelay(1000);
989         }
990 #endif
991
992
993         /*
994          * Figure out memory size in Megabytes.
995          */
996         memsize = n_ranks * rank_density / 0x100000;
997
998         /*
999          * Establish Local Access Window and TLB mappings for DDR memory.
1000          */
1001         memsize = setup_laws_and_tlbs(memsize);
1002         if (memsize == 0) {
1003                 return 0;
1004         }
1005
1006         return memsize * 1024 * 1024;
1007 }
1008
1009
1010 /*
1011  * Setup Local Access Window and TLB1 mappings for the requested
1012  * amount of memory.  Returns the amount of memory actually mapped
1013  * (usually the original request size), or 0 on error.
1014  */
1015
1016 static unsigned int
1017 setup_laws_and_tlbs(unsigned int memsize)
1018 {
1019         volatile immap_t *immap = (immap_t *)CFG_IMMR;
1020         volatile ccsr_local_ecm_t *ecm = &immap->im_local_ecm;
1021         unsigned int tlb_size;
1022         unsigned int law_size;
1023         unsigned int ram_tlb_index;
1024         unsigned int ram_tlb_address;
1025
1026         /*
1027          * Determine size of each TLB1 entry.
1028          */
1029         switch (memsize) {
1030         case 16:
1031         case 32:
1032                 tlb_size = BOOKE_PAGESZ_16M;
1033                 break;
1034         case 64:
1035         case 128:
1036                 tlb_size = BOOKE_PAGESZ_64M;
1037                 break;
1038         case 256:
1039         case 512:
1040                 tlb_size = BOOKE_PAGESZ_256M;
1041                 break;
1042         case 1024:
1043         case 2048:
1044                 if (PVR_VER(get_pvr()) > PVR_VER(PVR_85xx))
1045                         tlb_size = BOOKE_PAGESZ_1G;
1046                 else
1047                         tlb_size = BOOKE_PAGESZ_256M;
1048                 break;
1049         default:
1050                 puts("DDR: only 16M,32M,64M,128M,256M,512M,1G and 2G are supported.\n");
1051
1052                 /*
1053                  * The memory was not able to be mapped.
1054                  * Default to a small size.
1055                  */
1056                 tlb_size = BOOKE_PAGESZ_64M;
1057                 memsize=64;
1058                 break;
1059         }
1060
1061         /*
1062          * Configure DDR TLB1 entries.
1063          * Starting at TLB1 8, use no more than 8 TLB1 entries.
1064          */
1065         ram_tlb_index = 8;
1066         ram_tlb_address = (unsigned int)CFG_DDR_SDRAM_BASE;
1067         while (ram_tlb_address < (memsize * 1024 * 1024)
1068               && ram_tlb_index < 16) {
1069                 mtspr(MAS0, TLB1_MAS0(1, ram_tlb_index, 0));
1070                 mtspr(MAS1, TLB1_MAS1(1, 1, 0, 0, tlb_size));
1071                 mtspr(MAS2, TLB1_MAS2(E500_TLB_EPN(ram_tlb_address),
1072                                       0, 0, 0, 0, 0, 0, 0, 0));
1073                 mtspr(MAS3, TLB1_MAS3(E500_TLB_RPN(ram_tlb_address),
1074                                       0, 0, 0, 0, 0, 1, 0, 1, 0, 1));
1075                 asm volatile("isync;msync;tlbwe;isync");
1076
1077                 debug("DDR: MAS0=0x%08x\n", TLB1_MAS0(1, ram_tlb_index, 0));
1078                 debug("DDR: MAS1=0x%08x\n", TLB1_MAS1(1, 1, 0, 0, tlb_size));
1079                 debug("DDR: MAS2=0x%08x\n",
1080                       TLB1_MAS2(E500_TLB_EPN(ram_tlb_address),
1081                                 0, 0, 0, 0, 0, 0, 0, 0));
1082                 debug("DDR: MAS3=0x%08x\n",
1083                       TLB1_MAS3(E500_TLB_RPN(ram_tlb_address),
1084                                 0, 0, 0, 0, 0, 1, 0, 1, 0, 1));
1085
1086                 ram_tlb_address += (0x1000 << ((tlb_size - 1) * 2));
1087                 ram_tlb_index++;
1088         }
1089
1090
1091         /*
1092          * First supported LAW size is 16M, at LAWAR_SIZE_16M == 23.  Fnord.
1093          */
1094         law_size = 19 + __ilog2(memsize);
1095
1096         /*
1097          * Set up LAWBAR for all of DDR.
1098          */
1099         ecm->lawbar1 = ((CFG_DDR_SDRAM_BASE >> 12) & 0xfffff);
1100         ecm->lawar1 = (LAWAR_EN
1101                        | LAWAR_TRGT_IF_DDR
1102                        | (LAWAR_SIZE & law_size));
1103         debug("DDR: LAWBAR1=0x%08x\n", ecm->lawbar1);
1104         debug("DDR: LARAR1=0x%08x\n", ecm->lawar1);
1105
1106         /*
1107          * Confirm that the requested amount of memory was mapped.
1108          */
1109         return memsize;
1110 }
1111
1112 #endif /* CONFIG_SPD_EEPROM */
1113
1114
1115 #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
1116
1117 /*
1118  * Initialize all of memory for ECC, then enable errors.
1119  */
1120
1121 void
1122 ddr_enable_ecc(unsigned int dram_size)
1123 {
1124         uint *p = 0;
1125         uint i = 0;
1126         volatile immap_t *immap = (immap_t *)CFG_IMMR;
1127         volatile ccsr_ddr_t *ddr= &immap->im_ddr;
1128
1129         dma_init();
1130
1131         for (*p = 0; p < (uint *)(8 * 1024); p++) {
1132                 if (((unsigned int)p & 0x1f) == 0) {
1133                         ppcDcbz((unsigned long) p);
1134                 }
1135                 *p = (unsigned int)CONFIG_MEM_INIT_VALUE;
1136                 if (((unsigned int)p & 0x1c) == 0x1c) {
1137                         ppcDcbf((unsigned long) p);
1138                 }
1139         }
1140
1141         dma_xfer((uint *)0x002000, 0x002000, (uint *)0); /* 8K */
1142         dma_xfer((uint *)0x004000, 0x004000, (uint *)0); /* 16K */
1143         dma_xfer((uint *)0x008000, 0x008000, (uint *)0); /* 32K */
1144         dma_xfer((uint *)0x010000, 0x010000, (uint *)0); /* 64K */
1145         dma_xfer((uint *)0x020000, 0x020000, (uint *)0); /* 128k */
1146         dma_xfer((uint *)0x040000, 0x040000, (uint *)0); /* 256k */
1147         dma_xfer((uint *)0x080000, 0x080000, (uint *)0); /* 512k */
1148         dma_xfer((uint *)0x100000, 0x100000, (uint *)0); /* 1M */
1149         dma_xfer((uint *)0x200000, 0x200000, (uint *)0); /* 2M */
1150         dma_xfer((uint *)0x400000, 0x400000, (uint *)0); /* 4M */
1151
1152         for (i = 1; i < dram_size / 0x800000; i++) {
1153                 dma_xfer((uint *)(0x800000*i), 0x800000, (uint *)0);
1154         }
1155
1156         /*
1157          * Enable errors for ECC.
1158          */
1159         debug("DMA DDR: err_disable = 0x%08x\n", ddr->err_disable);
1160         ddr->err_disable = 0x00000000;
1161         asm("sync;isync;msync");
1162         debug("DMA DDR: err_disable = 0x%08x\n", ddr->err_disable);
1163 }
1164
1165 #endif  /* CONFIG_DDR_ECC  && ! CONFIG_ECC_INIT_VIA_DDRCONTROLLER */