FSL DDR: Rewrite the FSL mpc8xxx DDR controller setup code.
[platform/kernel/u-boot.git] / cpu / mpc8xxx / ddr / ctrl_regs.c
1 /*
2  * Copyright 2008 Freescale Semiconductor, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * Version 2 as published by the Free Software Foundation.
7  */
8
9 /*
10  * Generic driver for Freescale DDR/DDR2/DDR3 memory controller.
11  * Based on code from spd_sdram.c
12  * Author: James Yang [at freescale.com]
13  */
14
15 #include <common.h>
16 #include <asm/fsl_ddr_sdram.h>
17
18 #include "ddr.h"
19
20 extern unsigned int picos_to_mclk(unsigned int picos);
21 /*
22  * Determine Rtt value.
23  *
24  * This should likely be either board or controller specific.
25  *
26  * Rtt(nominal):
27  *      0 = Rtt disabled
28  *      1 = 75 ohm
29  *      2 = 150 ohm
30  *      3 = 50 ohm
31  *
32  * FIXME: Apparently 8641 needs a value of 2
33  * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572
34  *
35  * FIXME: There was some effort down this line earlier:
36  *
37  *      unsigned int i;
38  *      for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL/2; i++) {
39  *              if (popts->dimmslot[i].num_valid_cs
40  *                  && (popts->cs_local_opts[2*i].odt_rd_cfg
41  *                      || popts->cs_local_opts[2*i].odt_wr_cfg)) {
42  *                      rtt = 2;
43  *                      break;
44  *              }
45  *      }
46  */
47 static inline int fsl_ddr_get_rtt(void)
48 {
49         int rtt;
50
51 #if defined(CONFIG_FSL_DDR1)
52         rtt = 0;
53 #elif defined(CONFIG_FSL_DDR2)
54         rtt = 3;
55 #else
56 #error "Need Rtt value for DDR3"
57 #endif
58
59         return rtt;
60 }
61
62 /* Chip Select Configuration (CSn_CONFIG) */
63 static void set_csn_config(int i, fsl_ddr_cfg_regs_t *ddr,
64                                const memctl_options_t *popts,
65                                const dimm_params_t *dimm_params)
66 {
67         unsigned int cs_n_en = 0; /* Chip Select enable */
68         unsigned int intlv_en = 0; /* Memory controller interleave enable */
69         unsigned int intlv_ctl = 0; /* Interleaving control */
70         unsigned int ap_n_en = 0; /* Chip select n auto-precharge enable */
71         unsigned int odt_rd_cfg = 0; /* ODT for reads configuration */
72         unsigned int odt_wr_cfg = 0; /* ODT for writes configuration */
73         unsigned int ba_bits_cs_n = 0; /* Num of bank bits for SDRAM on CSn */
74         unsigned int row_bits_cs_n = 0; /* Num of row bits for SDRAM on CSn */
75         unsigned int col_bits_cs_n = 0; /* Num of ocl bits for SDRAM on CSn */
76
77         /* Compute CS_CONFIG only for existing ranks of each DIMM.  */
78         if ((((i&1) == 0)
79             && (dimm_params[i/2].n_ranks == 1))
80             || (dimm_params[i/2].n_ranks == 2)) {
81                 unsigned int n_banks_per_sdram_device;
82                 cs_n_en = 1;
83                 if (i == 0) {
84                         /* These fields only available in CS0_CONFIG */
85                         intlv_en = popts->memctl_interleaving;
86                         intlv_ctl = popts->memctl_interleaving_mode;
87                 }
88                 ap_n_en = popts->cs_local_opts[i].auto_precharge;
89                 odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg;
90                 odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg;
91                 n_banks_per_sdram_device
92                         = dimm_params[i/2].n_banks_per_sdram_device;
93                 ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2;
94                 row_bits_cs_n = dimm_params[i/2].n_row_addr - 12;
95                 col_bits_cs_n = dimm_params[i/2].n_col_addr - 8;
96         }
97
98         /* FIXME: intlv_en, intlv_ctl only on CS0_CONFIG */
99         if (i != 0) {
100                 intlv_en = 0;
101                 intlv_ctl = 0;
102         }
103
104         ddr->cs[i].config = (0
105                 | ((cs_n_en & 0x1) << 31)
106                 | ((intlv_en & 0x3) << 29)
107                 | ((intlv_en & 0xf) << 24)
108                 | ((ap_n_en & 0x1) << 23)
109
110                 /* XXX: some implementation only have 1 bit starting at left */
111                 | ((odt_rd_cfg & 0x7) << 20)
112
113                 /* XXX: Some implementation only have 1 bit starting at left */
114                 | ((odt_wr_cfg & 0x7) << 16)
115
116                 | ((ba_bits_cs_n & 0x3) << 14)
117                 | ((row_bits_cs_n & 0x7) << 8)
118                 | ((col_bits_cs_n & 0x7) << 0)
119                 );
120 }
121
122 /* Chip Select Configuration 2 (CSn_CONFIG_2) */
123 /* FIXME: 8572 */
124 static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
125 {
126         unsigned int pasr_cfg = 0;      /* Partial array self refresh config */
127
128         ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
129 }
130
131 /* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
132
133 #if defined(CONFIG_FSL_DDR2)
134 /*
135  * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
136  *
137  * Avoid writing for DDR I.  The new PQ38 DDR controller
138  * dreams up non-zero default values to be backwards compatible.
139  */
140 static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr)
141 {
142         unsigned char trwt_mclk = 0;   /* Read-to-write turnaround */
143         unsigned char twrt_mclk = 0;   /* Write-to-read turnaround */
144         /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
145         unsigned char trrt_mclk = 0;   /* Read-to-read turnaround */
146         unsigned char twwt_mclk = 0;   /* Write-to-write turnaround */
147
148         /* Active powerdown exit timing (tXARD and tXARDS). */
149         unsigned char act_pd_exit_mclk;
150         /* Precharge powerdown exit timing (tXP). */
151         unsigned char pre_pd_exit_mclk;
152         /* Precharge powerdown exit timing (tAXPD). */
153         unsigned char taxpd_mclk;
154         /* Mode register set cycle time (tMRD). */
155         unsigned char tmrd_mclk;
156
157         /* (tXARD and tXARDS). Empirical? */
158         act_pd_exit_mclk = 2;
159
160         /* XXX:  tXARD = 2, tXARDS = 7 - AL. * Empirical? */
161         pre_pd_exit_mclk = 6;
162
163         /* FIXME:  tXP = 2 on Micron 667 MHz DIMM */
164         taxpd_mclk = 8;
165
166         tmrd_mclk = 2;
167
168         ddr->timing_cfg_0 = (0
169                 | ((trwt_mclk & 0x3) << 30)     /* RWT */
170                 | ((twrt_mclk & 0x3) << 28)     /* WRT */
171                 | ((trrt_mclk & 0x3) << 26)     /* RRT */
172                 | ((twwt_mclk & 0x3) << 24)     /* WWT */
173                 | ((act_pd_exit_mclk & 0x7) << 20)  /* ACT_PD_EXIT */
174                 | ((pre_pd_exit_mclk & 0x7) << 16)  /* PRE_PD_EXIT */
175                 | ((taxpd_mclk & 0xf) << 8)     /* ODT_PD_EXIT */
176                 | ((tmrd_mclk & 0xf) << 0)      /* MRS_CYC */
177                 );
178         debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
179 }
180 #endif  /* defined(CONFIG_FSL_DDR2) */
181
182 /* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
183 static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr,
184                                const common_timing_params_t *common_dimm)
185 {
186         /* Extended Activate to precharge interval (tRAS) */
187         unsigned int ext_acttopre = 0;
188         unsigned int ext_refrec; /* Extended refresh recovery time (tRFC) */
189         unsigned int ext_caslat = 0; /* Extended MCAS latency from READ cmd */
190         unsigned int cntl_adj = 0; /* Control Adjust */
191
192         ext_refrec = (picos_to_mclk(common_dimm->tRFC_ps) - 8) >> 4;
193         ddr->timing_cfg_3 = (0
194                 | ((ext_acttopre & 0x1) << 24)
195                 | ((ext_refrec & 0x7) << 16)
196                 | ((ext_caslat & 0x1) << 12)
197                 | ((cntl_adj & 0x7) << 0)
198                 );
199 }
200
201 /* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
202 static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr,
203                                const common_timing_params_t *common_dimm,
204                                unsigned int cas_latency)
205 {
206         /* Precharge-to-activate interval (tRP) */
207         unsigned char pretoact_mclk;
208         /* Activate to precharge interval (tRAS) */
209         unsigned char acttopre_mclk;
210         /*  Activate to read/write interval (tRCD) */
211         unsigned char acttorw_mclk;
212         /* CASLAT */
213         unsigned char caslat_ctrl;
214         /*  Refresh recovery time (tRFC) ; trfc_low */
215         unsigned char refrec_ctrl;
216         /* Last data to precharge minimum interval (tWR) */
217         unsigned char wrrec_mclk;
218         /* Activate-to-activate interval (tRRD) */
219         unsigned char acttoact_mclk;
220         /* Last write data pair to read command issue interval (tWTR) */
221         unsigned char wrtord_mclk;
222
223         pretoact_mclk = picos_to_mclk(common_dimm->tRP_ps);
224         acttopre_mclk = picos_to_mclk(common_dimm->tRAS_ps);
225         acttorw_mclk = picos_to_mclk(common_dimm->tRCD_ps);
226
227         /*
228          * Translate CAS Latency to a DDR controller field value:
229          *
230          *      CAS Lat DDR I   DDR II  Ctrl
231          *      Clocks  SPD Bit SPD Bit Value
232          *      ------- ------- ------- -----
233          *      1.0     0               0001
234          *      1.5     1               0010
235          *      2.0     2       2       0011
236          *      2.5     3               0100
237          *      3.0     4       3       0101
238          *      3.5     5               0110
239          *      4.0             4       0111
240          *      4.5                     1000
241          *      5.0             5       1001
242          */
243 #if defined(CONFIG_FSL_DDR1)
244         caslat_ctrl = (cas_latency + 1) & 0x07;
245 #elif defined(CONFIG_FSL_DDR2)
246         caslat_ctrl = 2 * cas_latency - 1;
247 #else
248 #error "Need CAS Latency help for DDR3 in fsl_ddr_sdram.c"
249 #endif
250
251         refrec_ctrl = picos_to_mclk(common_dimm->tRFC_ps) - 8;
252         wrrec_mclk = picos_to_mclk(common_dimm->tWR_ps);
253         acttoact_mclk = picos_to_mclk(common_dimm->tRRD_ps);
254         wrtord_mclk = picos_to_mclk(common_dimm->tWTR_ps);
255
256         ddr->timing_cfg_1 = (0
257                 | ((pretoact_mclk & 0x07) << 28)
258                 | ((acttopre_mclk & 0x0F) << 24)
259                 | ((acttorw_mclk & 0x7) << 20)
260                 | ((caslat_ctrl & 0xF) << 16)
261                 | ((refrec_ctrl & 0xF) << 12)
262                 | ((wrrec_mclk & 0x07) << 8)
263                 | ((acttoact_mclk & 0x07) << 4)
264                 | ((wrtord_mclk & 0x07) << 0)
265                 );
266 }
267
268 /* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
269 static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr,
270                                const memctl_options_t *popts,
271                                const common_timing_params_t *common_dimm,
272                                unsigned int cas_latency,
273                                unsigned int additive_latency)
274 {
275         /* Additive latency */
276         unsigned char add_lat_mclk;
277         /* CAS-to-preamble override */
278         unsigned short cpo;
279         /* Write latency */
280         unsigned char wr_lat;
281         /*  Read to precharge (tRTP) */
282         unsigned char rd_to_pre;
283         /* Write command to write data strobe timing adjustment */
284         unsigned char wr_data_delay;
285         /* Minimum CKE pulse width (tCKE) */
286         unsigned char cke_pls;
287         /* Window for four activates (tFAW) */
288         unsigned short four_act;
289
290         /* FIXME add check that this must be less than acttorw_mclk */
291         add_lat_mclk = additive_latency;
292         cpo = popts->cpo_override;
293
294 #if defined(CONFIG_FSL_DDR1)
295         /*
296          * This is a lie.  It should really be 1, but if it is
297          * set to 1, bits overlap into the old controller's
298          * otherwise unused ACSM field.  If we leave it 0, then
299          * the HW will magically treat it as 1 for DDR 1.  Oh Yea.
300          */
301         wr_lat = 0;
302 #elif defined(CONFIG_FSL_DDR2)
303         wr_lat = cas_latency + additive_latency - 1;
304 #else
305 #error "Fix WR_LAT for DDR3"
306 #endif
307
308         rd_to_pre = picos_to_mclk(common_dimm->tRTP_ps);
309         wr_data_delay = popts->write_data_delay;
310         cke_pls = picos_to_mclk(popts->tCKE_clock_pulse_width_ps);
311         four_act = picos_to_mclk(popts->tFAW_window_four_activates_ps);
312
313         ddr->timing_cfg_2 = (0
314                 | ((add_lat_mclk & 0x7) << 28)
315                 | ((cpo & 0x1f) << 23)
316                 | ((wr_lat & 0x7) << 19)
317                 | ((rd_to_pre & 0x7) << 13)
318                 | ((wr_data_delay & 0x7) << 10)
319                 | ((cke_pls & 0x7) << 6)
320                 | ((four_act & 0x1f) << 0)
321                 );
322 }
323
324 /* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
325 static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
326                                const memctl_options_t *popts,
327                                const common_timing_params_t *common_dimm)
328 {
329         unsigned int mem_en;            /* DDR SDRAM interface logic enable */
330         unsigned int sren;              /* Self refresh enable (during sleep) */
331         unsigned int ecc_en;            /* ECC enable. */
332         unsigned int rd_en;             /* Registered DIMM enable */
333         unsigned int sdram_type;        /* Type of SDRAM */
334         unsigned int dyn_pwr;           /* Dynamic power management mode */
335         unsigned int dbw;               /* DRAM dta bus width */
336         unsigned int eight_be;          /* 8-beat burst enable */
337         unsigned int ncap = 0;          /* Non-concurrent auto-precharge */
338         unsigned int threeT_en;         /* Enable 3T timing */
339         unsigned int twoT_en;           /* Enable 2T timing */
340         unsigned int ba_intlv_ctl;      /* Bank (CS) interleaving control */
341         unsigned int x32_en = 0;        /* x32 enable */
342         unsigned int pchb8 = 0;         /* precharge bit 8 enable */
343         unsigned int hse;               /* Global half strength override */
344         unsigned int mem_halt = 0;      /* memory controller halt */
345         unsigned int bi = 0;            /* Bypass initialization */
346
347         mem_en = 1;
348         sren = popts->self_refresh_in_sleep;
349         if (common_dimm->all_DIMMs_ECC_capable) {
350                 /* Allow setting of ECC only if all DIMMs are ECC. */
351                 ecc_en = popts->ECC_mode;
352         } else {
353                 ecc_en = 0;
354         }
355
356         rd_en = (common_dimm->all_DIMMs_registered
357                  && !common_dimm->all_DIMMs_unbuffered);
358
359         sdram_type = CONFIG_FSL_SDRAM_TYPE;
360
361         dyn_pwr = popts->dynamic_power;
362         dbw = popts->data_bus_width;
363         eight_be = 0;           /* always 0 for DDR2 */
364         threeT_en = popts->threeT_en;
365         twoT_en = popts->twoT_en;
366         ba_intlv_ctl = popts->ba_intlv_ctl;
367         hse = popts->half_strength_driver_enable;
368
369         ddr->ddr_sdram_cfg = (0
370                         | ((mem_en & 0x1) << 31)
371                         | ((sren & 0x1) << 30)
372                         | ((ecc_en & 0x1) << 29)
373                         | ((rd_en & 0x1) << 28)
374                         | ((sdram_type & 0x7) << 24)
375                         | ((dyn_pwr & 0x1) << 21)
376                         | ((dbw & 0x3) << 19)
377                         | ((eight_be & 0x1) << 18)
378                         | ((ncap & 0x1) << 17)
379                         | ((threeT_en & 0x1) << 16)
380                         | ((twoT_en & 0x1) << 15)
381                         | ((ba_intlv_ctl & 0x7F) << 8)
382                         | ((x32_en & 0x1) << 5)
383                         | ((pchb8 & 0x1) << 4)
384                         | ((hse & 0x1) << 3)
385                         | ((mem_halt & 0x1) << 1)
386                         | ((bi & 0x1) << 0)
387                         );
388 }
389
390 /* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
391 static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr,
392                                const memctl_options_t *popts)
393 {
394         unsigned int frc_sr = 0;        /* Force self refresh */
395         unsigned int sr_ie = 0;         /* Self-refresh interrupt enable */
396         unsigned int dll_rst_dis;       /* DLL reset disable */
397         unsigned int dqs_cfg;           /* DQS configuration */
398         unsigned int odt_cfg;           /* ODT configuration */
399         unsigned int num_pr;            /* Number of posted refreshes */
400         unsigned int obc_cfg;           /* On-The-Fly Burst Chop Cfg */
401         unsigned int ap_en;             /* Address Parity Enable */
402         unsigned int d_init;            /* DRAM data initialization */
403         unsigned int rcw_en = 0;        /* Register Control Word Enable */
404         unsigned int md_en = 0;         /* Mirrored DIMM Enable */
405
406         dll_rst_dis = 1;        /* Make this configurable */
407         dqs_cfg = popts->DQS_config;
408         if (popts->cs_local_opts[0].odt_rd_cfg
409             || popts->cs_local_opts[0].odt_wr_cfg) {
410                 /* FIXME */
411                 odt_cfg = 2;
412         } else {
413                 odt_cfg = 0;
414         }
415
416         num_pr = 1;     /* Make this configurable */
417
418         /*
419          * 8572 manual says
420          *     {TIMING_CFG_1[PRETOACT]
421          *      + [DDR_SDRAM_CFG_2[NUM_PR]
422          *        * ({EXT_REFREC || REFREC} + 8 + 2)]}
423          *      << DDR_SDRAM_INTERVAL[REFINT]
424          */
425
426         obc_cfg = 0;    /* Make this configurable? */
427         ap_en = 0;      /* Make this configurable? */
428
429 #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
430         /* Use the DDR controller to auto initialize memory. */
431         d_init = 1;
432         ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
433         debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
434 #else
435         /* Memory will be initialized via DMA, or not at all. */
436         d_init = 0;
437 #endif
438
439         ddr->ddr_sdram_cfg_2 = (0
440                 | ((frc_sr & 0x1) << 31)
441                 | ((sr_ie & 0x1) << 30)
442                 | ((dll_rst_dis & 0x1) << 29)
443                 | ((dqs_cfg & 0x3) << 26)
444                 | ((odt_cfg & 0x3) << 21)
445                 | ((num_pr & 0xf) << 12)
446                 | ((obc_cfg & 0x1) << 6)
447                 | ((ap_en & 0x1) << 5)
448                 | ((d_init & 0x1) << 4)
449                 | ((rcw_en & 0x1) << 2)
450                 | ((md_en & 0x1) << 0)
451                 );
452 }
453
454 /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
455 static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr)
456 {
457         unsigned short esdmode2 = 0;    /* Extended SDRAM mode 2 */
458         unsigned short esdmode3 = 0;    /* Extended SDRAM mode 3 */
459
460         ddr->ddr_sdram_mode_2 = (0
461                                  | ((esdmode2 & 0xFFFF) << 16)
462                                  | ((esdmode3 & 0xFFFF) << 0)
463                                  );
464 }
465
466 /* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
467 static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
468                                const memctl_options_t *popts,
469                                const common_timing_params_t *common_dimm)
470 {
471         unsigned int refint;    /* Refresh interval */
472         unsigned int bstopre;   /* Precharge interval */
473
474         refint = picos_to_mclk(common_dimm->refresh_rate_ps);
475
476         bstopre = popts->bstopre;
477
478         /* refint field used 0x3FFF in earlier controllers */
479         ddr->ddr_sdram_interval = (0
480                                    | ((refint & 0xFFFF) << 16)
481                                    | ((bstopre & 0x3FFF) << 0)
482                                    );
483 }
484
485 /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
486 static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
487                                const memctl_options_t *popts,
488                                const common_timing_params_t *common_dimm,
489                                unsigned int cas_latency,
490                                unsigned int additive_latency)
491 {
492         unsigned short esdmode;         /* Extended SDRAM mode */
493         unsigned short sdmode;          /* SDRAM mode */
494
495         /*
496          * FIXME: This ought to be pre-calculated in a
497          * technology-specific routine,
498          * e.g. compute_DDR2_mode_register(), and then the
499          * sdmode and esdmode passed in as part of common_dimm.
500          */
501
502         /* Extended Mode Register */
503         unsigned int mrs = 0;           /* Mode Register Set */
504         unsigned int outputs = 0;       /* 0=Enabled, 1=Disabled */
505         unsigned int rdqs_en = 0;       /* RDQS Enable: 0=no, 1=yes */
506         unsigned int dqs_en = 0;        /* DQS# Enable: 0=enable, 1=disable */
507         unsigned int ocd = 0;           /* 0x0=OCD not supported,
508                                            0x7=OCD default state */
509         unsigned int rtt;
510         unsigned int al;                /* Posted CAS# additive latency (AL) */
511         unsigned int ods = 0;           /* Output Drive Strength:
512                                                 0 = Full strength (18ohm)
513                                                 1 = Reduced strength (4ohm) */
514         unsigned int dll_en = 0;        /* DLL Enable  0=Enable (Normal),
515                                                        1=Disable (Test/Debug) */
516
517         /* Mode Register (MR) */
518         unsigned int mr;        /* Mode Register Definition */
519         unsigned int pd;        /* Power-Down Mode */
520         unsigned int wr;        /* Write Recovery */
521         unsigned int dll_res;   /* DLL Reset */
522         unsigned int mode;      /* Normal=0 or Test=1 */
523         unsigned int caslat;    /* CAS# latency */
524         /* BT: Burst Type (0=Sequential, 1=Interleaved) */
525         unsigned int bt;
526         unsigned int bl;        /* BL: Burst Length */
527
528 #if defined(CONFIG_FSL_DDR2)
529         const unsigned int mclk_ps = get_memory_clk_period_ps();
530 #endif
531
532         rtt = fsl_ddr_get_rtt();
533
534         al = additive_latency;
535
536         esdmode = (0
537                 | ((mrs & 0x3) << 14)
538                 | ((outputs & 0x1) << 12)
539                 | ((rdqs_en & 0x1) << 11)
540                 | ((dqs_en & 0x1) << 10)
541                 | ((ocd & 0x7) << 7)
542                 | ((rtt & 0x2) << 5)   /* rtt field is split */
543                 | ((al & 0x7) << 3)
544                 | ((rtt & 0x1) << 2)   /* rtt field is split */
545                 | ((ods & 0x1) << 1)
546                 | ((dll_en & 0x1) << 0)
547                 );
548
549         mr = 0;          /* FIXME: CHECKME */
550
551         /*
552          * 0 = Fast Exit (Normal)
553          * 1 = Slow Exit (Low Power)
554          */
555         pd = 0;
556
557 #if defined(CONFIG_FSL_DDR1)
558         wr = 0;       /* Historical */
559 #elif defined(CONFIG_FSL_DDR2)
560         wr = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps - 1;
561 #else
562 #error "Write tWR_auto for DDR3"
563 #endif
564         dll_res = 0;
565         mode = 0;
566
567 #if defined(CONFIG_FSL_DDR1)
568         if (1 <= cas_latency && cas_latency <= 4) {
569                 unsigned char mode_caslat_table[4] = {
570                         0x5,    /* 1.5 clocks */
571                         0x2,    /* 2.0 clocks */
572                         0x6,    /* 2.5 clocks */
573                         0x3     /* 3.0 clocks */
574                 };
575         caslat = mode_caslat_table[cas_latency - 1];
576         }
577 #elif defined(CONFIG_FSL_DDR2)
578         caslat = cas_latency;
579 #else
580 #error "Fix the mode CAS Latency for DDR3"
581 #endif
582         bt = 0;
583
584         switch (popts->burst_length) {
585         case 4:
586                 bl = 2;
587                 break;
588         case 8:
589                 bl = 3;
590                 break;
591         default:
592                 printf("Error: invalid burst length of %u specified. "
593                         " Defaulting to 4 beats.\n",
594                         popts->burst_length);
595                 bl = 2;
596                 break;
597         }
598
599         sdmode = (0
600                   | ((mr & 0x3) << 14)
601                   | ((pd & 0x1) << 12)
602                   | ((wr & 0x7) << 9)
603                   | ((dll_res & 0x1) << 8)
604                   | ((mode & 0x1) << 7)
605                   | ((caslat & 0x7) << 4)
606                   | ((bt & 0x1) << 3)
607                   | ((bl & 0x7) << 0)
608                   );
609
610         ddr->ddr_sdram_mode = (0
611                                | ((esdmode & 0xFFFF) << 16)
612                                | ((sdmode & 0xFFFF) << 0)
613                                );
614 }
615
616
617 /* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
618 static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
619 {
620         unsigned int init_value;        /* Initialization value */
621
622         init_value = 0xDEADBEEF;
623         ddr->ddr_data_init = init_value;
624 }
625
626 /*
627  * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
628  * The old controller on the 8540/60 doesn't have this register.
629  * Hope it's OK to set it (to 0) anyway.
630  */
631 static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
632                                          const memctl_options_t *popts)
633 {
634         unsigned int clk_adjust;        /* Clock adjust */
635
636         clk_adjust = popts->clk_adjust;
637         ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
638 }
639
640 /* DDR Initialization Address (DDR_INIT_ADDR) */
641 static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
642 {
643         unsigned int init_addr = 0;     /* Initialization address */
644
645         ddr->ddr_init_addr = init_addr;
646 }
647
648 /* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
649 static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
650 {
651         unsigned int uia = 0;   /* Use initialization address */
652         unsigned int init_ext_addr = 0; /* Initialization address */
653
654         ddr->ddr_init_ext_addr = (0
655                                   | ((uia & 0x1) << 31)
656                                   | (init_ext_addr & 0xF)
657                                   );
658 }
659
660 /* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
661 static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr)
662 {
663         unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
664         unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
665         unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
666         unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
667         unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
668
669         ddr->timing_cfg_4 = (0
670                              | ((rwt & 0xf) << 28)
671                              | ((wrt & 0xf) << 24)
672                              | ((rrt & 0xf) << 20)
673                              | ((wwt & 0xf) << 16)
674                              | (dll_lock & 0x3)
675                              );
676 }
677
678 /* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
679 static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr)
680 {
681         unsigned int rodt_on = 0;       /* Read to ODT on */
682         unsigned int rodt_off = 0;      /* Read to ODT off */
683         unsigned int wodt_on = 0;       /* Write to ODT on */
684         unsigned int wodt_off = 0;      /* Write to ODT off */
685
686         ddr->timing_cfg_5 = (0
687                              | ((rodt_on & 0xf) << 24)
688                              | ((rodt_off & 0xf) << 20)
689                              | ((wodt_on & 0xf) << 12)
690                              | ((wodt_off & 0xf) << 8)
691                              );
692 }
693
694 /* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
695 static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr)
696 {
697         unsigned int zq_en = 0; /* ZQ Calibration Enable */
698         unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
699         /* Normal Operation Full Calibration Time (tZQoper) */
700         unsigned int zqoper = 0;
701         /* Normal Operation Short Calibration Time (tZQCS) */
702         unsigned int zqcs = 0;
703
704         ddr->ddr_zq_cntl = (0
705                             | ((zq_en & 0x1) << 31)
706                             | ((zqinit & 0xF) << 24)
707                             | ((zqoper & 0xF) << 16)
708                             | ((zqcs & 0xF) << 8)
709                             );
710 }
711
712 /* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
713 static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr)
714 {
715         unsigned int wrlvl_en = 0; /* Write Leveling Enable */
716         /*
717          * First DQS pulse rising edge after margining mode
718          * is programmed (tWL_MRD)
719          */
720         unsigned int wrlvl_mrd = 0;
721         /* ODT delay after margining mode is programmed (tWL_ODTEN) */
722         unsigned int wrlvl_odten = 0;
723         /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
724         unsigned int wrlvl_dqsen = 0;
725         /* WRLVL_SMPL: Write leveling sample time */
726         unsigned int wrlvl_smpl = 0;
727         /* WRLVL_WLR: Write leveling repeition time */
728         unsigned int wrlvl_wlr = 0;
729         /* WRLVL_START: Write leveling start time */
730         unsigned int wrlvl_start = 0;
731
732         ddr->ddr_wrlvl_cntl = (0
733                                | ((wrlvl_en & 0x1) << 31)
734                                | ((wrlvl_mrd & 0x7) << 24)
735                                | ((wrlvl_odten & 0x7) << 20)
736                                | ((wrlvl_dqsen & 0x7) << 16)
737                                | ((wrlvl_smpl & 0xf) << 12)
738                                | ((wrlvl_wlr & 0x7) << 8)
739                                | ((wrlvl_start & 0xF) << 0)
740                                );
741 }
742
743 /* DDR Self Refresh Counter (DDR_SR_CNTR) */
744 static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr)
745 {
746         unsigned int sr_it = 0; /* Self Refresh Idle Threshold */
747
748         ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
749 }
750
751 /* DDR Pre-Drive Conditioning Control (DDR_PD_CNTL) */
752 static void set_ddr_pd_cntl(fsl_ddr_cfg_regs_t *ddr)
753 {
754         /* Termination value during pre-drive conditioning */
755         unsigned int tvpd = 0;
756         unsigned int pd_en = 0;         /* Pre-Drive Conditioning Enable */
757         unsigned int pdar = 0;          /* Pre-Drive After Read */
758         unsigned int pdaw = 0;          /* Pre-Drive After Write */
759         unsigned int pd_on = 0;         /* Pre-Drive Conditioning On */
760         unsigned int pd_off = 0;        /* Pre-Drive Conditioning Off */
761
762         ddr->ddr_pd_cntl = (0
763                             | ((pd_en & 0x1) << 31)
764                             | ((tvpd & 0x7) << 28)
765                             | ((pdar & 0x7F) << 20)
766                             | ((pdaw & 0x7F) << 12)
767                             | ((pd_on & 0x1F) << 6)
768                             | ((pd_off & 0x1F) << 0)
769                             );
770 }
771
772
773 /* DDR SDRAM Register Control Word 1 (DDR_SDRAM_RCW_1) */
774 static void set_ddr_sdram_rcw_1(fsl_ddr_cfg_regs_t *ddr)
775 {
776         unsigned int rcw0 = 0;  /* RCW0: Register Control Word 0 */
777         unsigned int rcw1 = 0;  /* RCW1: Register Control Word 1 */
778         unsigned int rcw2 = 0;  /* RCW2: Register Control Word 2 */
779         unsigned int rcw3 = 0;  /* RCW3: Register Control Word 3 */
780         unsigned int rcw4 = 0;  /* RCW4: Register Control Word 4 */
781         unsigned int rcw5 = 0;  /* RCW5: Register Control Word 5 */
782         unsigned int rcw6 = 0;  /* RCW6: Register Control Word 6 */
783         unsigned int rcw7 = 0;  /* RCW7: Register Control Word 7 */
784
785         ddr->ddr_sdram_rcw_1 = (0
786                                 | ((rcw0 & 0xF) << 28)
787                                 | ((rcw1 & 0xF) << 24)
788                                 | ((rcw2 & 0xF) << 20)
789                                 | ((rcw3 & 0xF) << 16)
790                                 | ((rcw4 & 0xF) << 12)
791                                 | ((rcw5 & 0xF) << 8)
792                                 | ((rcw6 & 0xF) << 4)
793                                 | ((rcw7 & 0xF) << 0)
794                                 );
795 }
796
797 /* DDR SDRAM Register Control Word 2 (DDR_SDRAM_RCW_2) */
798 static void set_ddr_sdram_rcw_2(fsl_ddr_cfg_regs_t *ddr)
799 {
800         unsigned int rcw8 = 0;  /* RCW0: Register Control Word 8 */
801         unsigned int rcw9 = 0;  /* RCW1: Register Control Word 9 */
802         unsigned int rcw10 = 0; /* RCW2: Register Control Word 10 */
803         unsigned int rcw11 = 0; /* RCW3: Register Control Word 11 */
804         unsigned int rcw12 = 0; /* RCW4: Register Control Word 12 */
805         unsigned int rcw13 = 0; /* RCW5: Register Control Word 13 */
806         unsigned int rcw14 = 0; /* RCW6: Register Control Word 14 */
807         unsigned int rcw15 = 0; /* RCW7: Register Control Word 15 */
808
809         ddr->ddr_sdram_rcw_2 = (0
810                                 | ((rcw8 & 0xF) << 28)
811                                 | ((rcw9 & 0xF) << 24)
812                                 | ((rcw10 & 0xF) << 20)
813                                 | ((rcw11 & 0xF) << 16)
814                                 | ((rcw12 & 0xF) << 12)
815                                 | ((rcw13 & 0xF) << 8)
816                                 | ((rcw14 & 0xF) << 4)
817                                 | ((rcw15 & 0xF) << 0)
818                                 );
819 }
820
821 unsigned int
822 check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
823 {
824         unsigned int res = 0;
825
826         /*
827          * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
828          * not set at the same time.
829          */
830         if (ddr->ddr_sdram_cfg & 0x10000000
831             && ddr->ddr_sdram_cfg & 0x00008000) {
832                 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
833                                 " should not be set at the same time.\n");
834                 res++;
835         }
836
837         return res;
838 }
839
840 unsigned int
841 compute_fsl_memctl_config_regs(const memctl_options_t *popts,
842                                fsl_ddr_cfg_regs_t *ddr,
843                                const common_timing_params_t *common_dimm,
844                                const dimm_params_t *dimm_params,
845                                unsigned int dbw_cap_adj)
846 {
847         unsigned int i;
848         unsigned int cas_latency;
849         unsigned int additive_latency;
850
851         memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
852
853         if (common_dimm == NULL) {
854                 printf("Error: subset DIMM params struct null pointer\n");
855                 return 1;
856         }
857
858         /*
859          * Process overrides first.
860          *
861          * FIXME: somehow add dereated caslat to this
862          */
863         cas_latency = (popts->cas_latency_override)
864                 ? popts->cas_latency_override_value
865                 : common_dimm->lowest_common_SPD_caslat;
866
867         additive_latency = (popts->additive_latency_override)
868                 ? popts->additive_latency_override_value
869                 : common_dimm->additive_latency;
870
871         /* Chip Select Memory Bounds (CSn_BNDS) */
872         for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
873                 phys_size_t sa = 0;
874                 phys_size_t ea = 0;
875                 if (popts->ba_intlv_ctl && i > 0) {
876                         /* Don't set up boundaries if bank interleaving */
877                         break;
878                 }
879
880                 if (dimm_params[i/2].n_ranks == 0) {
881                         debug("Skipping setup of CS%u "
882                                 "because n_ranks on DIMM %u is 0\n", i, i/2);
883                         continue;
884                 }
885                 if (popts->memctl_interleaving && popts->ba_intlv_ctl) {
886                         /*
887                          * This works superbank 2CS
888                          * There are 2 memory controllers configured
889                          * identically, memory is interleaved between them,
890                          * and each controller uses rank interleaving within
891                          * itself. Therefore the starting and ending address
892                          * on each controller is twice the amount present on
893                          * each controller.
894                          */
895                         ea = (2 * common_dimm->total_mem >> dbw_cap_adj) - 1;
896                 }
897                 else if (!popts->memctl_interleaving && popts->ba_intlv_ctl) {
898                         /*
899                          * If memory interleaving between controllers is NOT
900                          * enabled, the starting address for each memory
901                          * controller is distinct.  However, because rank
902                          * interleaving is enabled, the starting and ending
903                          * addresses of the total memory on that memory
904                          * controller needs to be programmed into its
905                          * respective CS0_BNDS.
906                          */
907                         sa = common_dimm->base_address;
908                         ea = sa + (common_dimm->total_mem >> dbw_cap_adj) - 1;
909                 }
910                 else if (popts->memctl_interleaving && !popts->ba_intlv_ctl) {
911                         /*
912                          * Only the rank on CS0 of each memory controller may
913                          * be used if memory controller interleaving is used
914                          * without rank interleaving within each memory
915                          * controller.  However, the ending address programmed
916                          * into each CS0 must be the sum of the amount of
917                          * memory in the two CS0 ranks.
918                          */
919                         if (i == 0) {
920                                 unsigned long long rank_density
921                                                 = dimm_params[0].rank_density;
922                                 ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
923                         }
924
925                 }
926                 else if (!popts->memctl_interleaving && !popts->ba_intlv_ctl) {
927                         /*
928                          * No rank interleaving and no memory controller
929                          * interleaving.
930                          */
931                         unsigned long long rank_density
932                                                 = dimm_params[i/2].rank_density;
933                         sa = dimm_params[i/2].base_address;
934                         ea = sa + (rank_density >> dbw_cap_adj) - 1;
935                         if (i&1) {
936                                 if ((dimm_params[i/2].n_ranks == 1)) {
937                                         /* Odd chip select, single-rank dimm */
938                                         sa = 0;
939                                         ea = 0;
940                                 } else {
941                                         /* Odd chip select, dual-rank DIMM */
942                                         sa += rank_density >> dbw_cap_adj;
943                                         ea += rank_density >> dbw_cap_adj;
944                                 }
945                         }
946                 }
947
948                 sa >>= 24;
949                 ea >>= 24;
950
951                 ddr->cs[i].bnds = (0
952                         | ((sa & 0xFFF) << 16)  /* starting address MSB */
953                         | ((ea & 0xFFF) << 0)   /* ending address MSB */
954                         );
955
956                 set_csn_config(i, ddr, popts, dimm_params);
957                 set_csn_config_2(i, ddr);
958         }
959
960 #if defined(CONFIG_FSL_DDR2)
961         set_timing_cfg_0(ddr);
962 #endif
963
964         set_timing_cfg_3(ddr, common_dimm);
965         set_timing_cfg_1(ddr, common_dimm, cas_latency);
966         set_timing_cfg_2(ddr, popts, common_dimm,
967                                 cas_latency, additive_latency);
968
969         set_ddr_sdram_cfg(ddr, popts, common_dimm);
970
971         set_ddr_sdram_cfg_2(ddr, popts);
972         set_ddr_sdram_mode(ddr, popts, common_dimm,
973                                 cas_latency, additive_latency);
974         set_ddr_sdram_mode_2(ddr);
975         set_ddr_sdram_interval(ddr, popts, common_dimm);
976         set_ddr_data_init(ddr);
977         set_ddr_sdram_clk_cntl(ddr, popts);
978         set_ddr_init_addr(ddr);
979         set_ddr_init_ext_addr(ddr);
980         set_timing_cfg_4(ddr);
981         set_timing_cfg_5(ddr);
982
983         set_ddr_zq_cntl(ddr);
984         set_ddr_wrlvl_cntl(ddr);
985
986         set_ddr_pd_cntl(ddr);
987         set_ddr_sr_cntr(ddr);
988
989         set_ddr_sdram_rcw_1(ddr);
990         set_ddr_sdram_rcw_2(ddr);
991
992         return check_fsl_memctl_config_regs(ddr);
993 }