]> git.sur5r.net Git - u-boot/blob - arch/powerpc/cpu/mpc8xxx/ddr/main.c
Merge branch 'master' of git://git.denx.de/u-boot-mpc85xx
[u-boot] / arch / powerpc / cpu / mpc8xxx / ddr / main.c
1 /*
2  * Copyright 2008-2011 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 void fsl_ddr_set_lawbar(
21                 const common_timing_params_t *memctl_common_params,
22                 unsigned int memctl_interleaved,
23                 unsigned int ctrl_num);
24
25 /* processor specific function */
26 extern void fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t *regs,
27                                    unsigned int ctrl_num);
28
29 /* Board-specific functions defined in each board's ddr.c */
30 extern void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
31                            unsigned int ctrl_num);
32
33 /*
34  * ASSUMPTIONS:
35  *    - Same number of CONFIG_DIMM_SLOTS_PER_CTLR on each controller
36  *    - Same memory data bus width on all controllers
37  *
38  * NOTES:
39  *
40  * The memory controller and associated documentation use confusing
41  * terminology when referring to the orgranization of DRAM.
42  *
43  * Here is a terminology translation table:
44  *
45  * memory controller/documention  |industry   |this code  |signals
46  * -------------------------------|-----------|-----------|-----------------
47  * physical bank/bank             |rank       |rank       |chip select (CS)
48  * logical bank/sub-bank          |bank       |bank       |bank address (BA)
49  * page/row                       |row        |page       |row address
50  * ???                            |column     |column     |column address
51  *
52  * The naming confusion is further exacerbated by the descriptions of the
53  * memory controller interleaving feature, where accesses are interleaved
54  * _BETWEEN_ two seperate memory controllers.  This is configured only in
55  * CS0_CONFIG[INTLV_CTL] of each memory controller.
56  *
57  * memory controller documentation | number of chip selects
58  *                                 | per memory controller supported
59  * --------------------------------|-----------------------------------------
60  * cache line interleaving         | 1 (CS0 only)
61  * page interleaving               | 1 (CS0 only)
62  * bank interleaving               | 1 (CS0 only)
63  * superbank interleraving         | depends on bank (chip select)
64  *                                 |   interleraving [rank interleaving]
65  *                                 |   mode used on every memory controller
66  *
67  * Even further confusing is the existence of the interleaving feature
68  * _WITHIN_ each memory controller.  The feature is referred to in
69  * documentation as chip select interleaving or bank interleaving,
70  * although it is configured in the DDR_SDRAM_CFG field.
71  *
72  * Name of field                | documentation name    | this code
73  * -----------------------------|-----------------------|------------------
74  * DDR_SDRAM_CFG[BA_INTLV_CTL]  | Bank (chip select)    | rank interleaving
75  *                              |  interleaving
76  */
77
78 #ifdef DEBUG
79 const char *step_string_tbl[] = {
80         "STEP_GET_SPD",
81         "STEP_COMPUTE_DIMM_PARMS",
82         "STEP_COMPUTE_COMMON_PARMS",
83         "STEP_GATHER_OPTS",
84         "STEP_ASSIGN_ADDRESSES",
85         "STEP_COMPUTE_REGS",
86         "STEP_PROGRAM_REGS",
87         "STEP_ALL"
88 };
89
90 const char * step_to_string(unsigned int step) {
91
92         unsigned int s = __ilog2(step);
93
94         if ((1 << s) != step)
95                 return step_string_tbl[7];
96
97         return step_string_tbl[s];
98 }
99 #endif
100
101 int step_assign_addresses(fsl_ddr_info_t *pinfo,
102                           unsigned int dbw_cap_adj[],
103                           unsigned int *all_memctl_interleaving,
104                           unsigned int *all_ctlr_rank_interleaving)
105 {
106         int i, j;
107
108         /*
109          * If a reduced data width is requested, but the SPD
110          * specifies a physically wider device, adjust the
111          * computed dimm capacities accordingly before
112          * assigning addresses.
113          */
114         for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
115                 unsigned int found = 0;
116
117                 switch (pinfo->memctl_opts[i].data_bus_width) {
118                 case 2:
119                         /* 16-bit */
120                         printf("can't handle 16-bit mode yet\n");
121                         break;
122
123                 case 1:
124                         /* 32-bit */
125                         for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
126                                 unsigned int dw;
127                                 dw = pinfo->dimm_params[i][j].data_width;
128                                 if (pinfo->dimm_params[i][j].n_ranks
129                                     && (dw == 72 || dw == 64)) {
130                                         /*
131                                          * FIXME: can't really do it
132                                          * like this because this just
133                                          * further reduces the memory
134                                          */
135                                         found = 1;
136                                         break;
137                                 }
138                         }
139                         if (found) {
140                                 dbw_cap_adj[i] = 1;
141                         }
142                         break;
143
144                 case 0:
145                         /* 64-bit */
146                         break;
147
148                 default:
149                         printf("unexpected data bus width "
150                                 "specified controller %u\n", i);
151                         return 1;
152                 }
153         }
154
155         j = 0;
156         for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
157                 if (pinfo->memctl_opts[i].memctl_interleaving)
158                         j++;
159         /*
160          * Not support less than all memory controllers interleaving
161          * if more than two controllers
162          */
163         if (j == CONFIG_NUM_DDR_CONTROLLERS)
164                 *all_memctl_interleaving = 1;
165
166         /* Check that all controllers are rank interleaving. */
167         j = 0;
168         for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
169                 if (pinfo->memctl_opts[i].ba_intlv_ctl)
170                         j++;
171         /*
172          * All memory controllers must be populated to qualify for
173          * all controller rank interleaving
174          */
175          if (j == CONFIG_NUM_DDR_CONTROLLERS)
176                 *all_ctlr_rank_interleaving = 1;
177
178         if (*all_memctl_interleaving) {
179                 unsigned long long addr, total_mem_per_ctlr = 0;
180                 /*
181                  * If interleaving between memory controllers,
182                  * make each controller start at a base address
183                  * of 0.
184                  *
185                  * Also, if bank interleaving (chip select
186                  * interleaving) is enabled on each memory
187                  * controller, CS0 needs to be programmed to
188                  * cover the entire memory range on that memory
189                  * controller
190                  *
191                  * Bank interleaving also implies that each
192                  * addressed chip select is identical in size.
193                  */
194
195                 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
196                         addr = 0;
197                         pinfo->common_timing_params[i].base_address = 0ull;
198                         for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
199                                 unsigned long long cap
200                                         = pinfo->dimm_params[i][j].capacity;
201
202                                 pinfo->dimm_params[i][j].base_address = addr;
203                                 addr += cap >> dbw_cap_adj[i];
204                                 total_mem_per_ctlr += cap >> dbw_cap_adj[i];
205                         }
206                 }
207                 pinfo->common_timing_params[0].total_mem = total_mem_per_ctlr;
208         } else {
209                 /*
210                  * Simple linear assignment if memory
211                  * controllers are not interleaved.
212                  */
213                 unsigned long long cur_memsize = 0;
214                 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
215                         u64 total_mem_per_ctlr = 0;
216                         pinfo->common_timing_params[i].base_address =
217                                                 cur_memsize;
218                         for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
219                                 /* Compute DIMM base addresses. */
220                                 unsigned long long cap =
221                                         pinfo->dimm_params[i][j].capacity;
222                                 pinfo->dimm_params[i][j].base_address =
223                                         cur_memsize;
224                                 cur_memsize += cap >> dbw_cap_adj[i];
225                                 total_mem_per_ctlr += cap >> dbw_cap_adj[i];
226                         }
227                         pinfo->common_timing_params[i].total_mem =
228                                                         total_mem_per_ctlr;
229                 }
230         }
231
232         return 0;
233 }
234
235 unsigned long long
236 fsl_ddr_compute(fsl_ddr_info_t *pinfo, unsigned int start_step,
237                                        unsigned int size_only)
238 {
239         unsigned int i, j;
240         unsigned int all_controllers_memctl_interleaving = 0;
241         unsigned int all_controllers_rank_interleaving = 0;
242         unsigned long long total_mem = 0;
243
244         fsl_ddr_cfg_regs_t *ddr_reg = pinfo->fsl_ddr_config_reg;
245         common_timing_params_t *timing_params = pinfo->common_timing_params;
246
247         /* data bus width capacity adjust shift amount */
248         unsigned int dbw_capacity_adjust[CONFIG_NUM_DDR_CONTROLLERS];
249
250         for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
251                 dbw_capacity_adjust[i] = 0;
252         }
253
254         debug("starting at step %u (%s)\n",
255               start_step, step_to_string(start_step));
256
257         switch (start_step) {
258         case STEP_GET_SPD:
259                 /* STEP 1:  Gather all DIMM SPD data */
260                 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
261                         fsl_ddr_get_spd(pinfo->spd_installed_dimms[i], i);
262                 }
263
264         case STEP_COMPUTE_DIMM_PARMS:
265                 /* STEP 2:  Compute DIMM parameters from SPD data */
266
267                 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
268                         for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
269                                 unsigned int retval;
270                                 generic_spd_eeprom_t *spd =
271                                         &(pinfo->spd_installed_dimms[i][j]);
272                                 dimm_params_t *pdimm =
273                                         &(pinfo->dimm_params[i][j]);
274
275                                 retval = compute_dimm_parameters(spd, pdimm, i);
276                                 if (retval == 2) {
277                                         printf("Error: compute_dimm_parameters"
278                                         " non-zero returned FATAL value "
279                                         "for memctl=%u dimm=%u\n", i, j);
280                                         return 0;
281                                 }
282                                 if (retval) {
283                                         debug("Warning: compute_dimm_parameters"
284                                         " non-zero return value for memctl=%u "
285                                         "dimm=%u\n", i, j);
286                                 }
287                         }
288                 }
289
290         case STEP_COMPUTE_COMMON_PARMS:
291                 /*
292                  * STEP 3: Compute a common set of timing parameters
293                  * suitable for all of the DIMMs on each memory controller
294                  */
295                 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
296                         debug("Computing lowest common DIMM"
297                                 " parameters for memctl=%u\n", i);
298                         compute_lowest_common_dimm_parameters(
299                                 pinfo->dimm_params[i],
300                                 &timing_params[i],
301                                 CONFIG_DIMM_SLOTS_PER_CTLR);
302                 }
303
304         case STEP_GATHER_OPTS:
305                 /* STEP 4:  Gather configuration requirements from user */
306                 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
307                         debug("Reloading memory controller "
308                                 "configuration options for memctl=%u\n", i);
309                         /*
310                          * This "reloads" the memory controller options
311                          * to defaults.  If the user "edits" an option,
312                          * next_step points to the step after this,
313                          * which is currently STEP_ASSIGN_ADDRESSES.
314                          */
315                         populate_memctl_options(
316                                         timing_params[i].all_DIMMs_registered,
317                                         &pinfo->memctl_opts[i],
318                                         pinfo->dimm_params[i], i);
319                 }
320                 check_interleaving_options(pinfo);
321         case STEP_ASSIGN_ADDRESSES:
322                 /* STEP 5:  Assign addresses to chip selects */
323                 step_assign_addresses(pinfo,
324                                 dbw_capacity_adjust,
325                                 &all_controllers_memctl_interleaving,
326                                 &all_controllers_rank_interleaving);
327
328         case STEP_COMPUTE_REGS:
329                 /* STEP 6:  compute controller register values */
330                 debug("FSL Memory ctrl cg register computation\n");
331                 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
332                         if (timing_params[i].ndimms_present == 0) {
333                                 memset(&ddr_reg[i], 0,
334                                         sizeof(fsl_ddr_cfg_regs_t));
335                                 continue;
336                         }
337
338                         compute_fsl_memctl_config_regs(
339                                         &pinfo->memctl_opts[i],
340                                         &ddr_reg[i], &timing_params[i],
341                                         pinfo->dimm_params[i],
342                                         dbw_capacity_adjust[i],
343                                         size_only);
344                 }
345
346         default:
347                 break;
348         }
349
350         /* Compute the total amount of memory. */
351
352         /*
353          * If bank interleaving but NOT memory controller interleaving
354          * CS_BNDS describe the quantity of memory on each memory
355          * controller, so the total is the sum across.
356          */
357         if (!all_controllers_memctl_interleaving
358             && all_controllers_rank_interleaving) {
359                 total_mem = 0;
360                 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
361                         total_mem += timing_params[i].total_mem;
362                 }
363
364         } else {
365                 /*
366                  * Compute the amount of memory available just by
367                  * looking for the highest valid CSn_BNDS value.
368                  * This allows us to also experiment with using
369                  * only CS0 when using dual-rank DIMMs.
370                  */
371                 unsigned int max_end = 0;
372
373                 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
374                         for (j = 0; j < CONFIG_CHIP_SELECTS_PER_CTRL; j++) {
375                                 fsl_ddr_cfg_regs_t *reg = &ddr_reg[i];
376                                 if (reg->cs[j].config & 0x80000000) {
377                                         unsigned int end;
378                                         end = reg->cs[j].bnds & 0xFFF;
379                                         if (end > max_end) {
380                                                 max_end = end;
381                                         }
382                                 }
383                         }
384                 }
385
386                 total_mem = 1 + (((unsigned long long)max_end << 24ULL)
387                                     | 0xFFFFFFULL);
388         }
389
390         return total_mem;
391 }
392
393 /*
394  * fsl_ddr_sdram() -- this is the main function to be called by
395  *      initdram() in the board file.
396  *
397  * It returns amount of memory configured in bytes.
398  */
399 phys_size_t fsl_ddr_sdram(void)
400 {
401         unsigned int i;
402         unsigned int memctl_interleaved;
403         unsigned long long total_memory;
404         fsl_ddr_info_t info;
405
406         /* Reset info structure. */
407         memset(&info, 0, sizeof(fsl_ddr_info_t));
408
409         /* Compute it once normally. */
410         total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 0);
411
412         /* Check for memory controller interleaving. */
413         memctl_interleaved = 0;
414         for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
415                 memctl_interleaved +=
416                         info.memctl_opts[i].memctl_interleaving;
417         }
418
419         if (memctl_interleaved) {
420                 if (memctl_interleaved == CONFIG_NUM_DDR_CONTROLLERS) {
421                         debug("memctl interleaving\n");
422                         /*
423                          * Change the meaning of memctl_interleaved
424                          * to be "boolean".
425                          */
426                         memctl_interleaved = 1;
427                 } else {
428                         printf("Warning: memctl interleaving not "
429                                 "properly configured on all controllers\n");
430                         memctl_interleaved = 0;
431                         for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
432                                 info.memctl_opts[i].memctl_interleaving = 0;
433                         debug("Recomputing with memctl_interleaving off.\n");
434                         total_memory = fsl_ddr_compute(&info,
435                                                        STEP_ASSIGN_ADDRESSES,
436                                                        0);
437                 }
438         }
439
440         /* Program configuration registers. */
441         for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
442                 debug("Programming controller %u\n", i);
443                 if (info.common_timing_params[i].ndimms_present == 0) {
444                         debug("No dimms present on controller %u; "
445                                         "skipping programming\n", i);
446                         continue;
447                 }
448
449                 fsl_ddr_set_memctl_regs(&(info.fsl_ddr_config_reg[i]), i);
450         }
451
452         if (memctl_interleaved) {
453                 const unsigned int ctrl_num = 0;
454
455                 /* Only set LAWBAR1 if memory controller interleaving is on. */
456                 fsl_ddr_set_lawbar(&info.common_timing_params[0],
457                                          memctl_interleaved, ctrl_num);
458         } else {
459                 /*
460                  * Memory controller interleaving is NOT on;
461                  * set each lawbar individually.
462                  */
463                 for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
464                         fsl_ddr_set_lawbar(&info.common_timing_params[i],
465                                                  0, i);
466                 }
467         }
468
469         debug("total_memory = %llu\n", total_memory);
470
471 #if !defined(CONFIG_PHYS_64BIT)
472         /* Check for 4G or more.  Bad. */
473         if (total_memory >= (1ull << 32)) {
474                 printf("Detected %lld MB of memory\n", total_memory >> 20);
475                 printf("       This U-Boot only supports < 4G of DDR\n");
476                 printf("       You could rebuild it with CONFIG_PHYS_64BIT\n");
477                 printf("       "); /* re-align to match init_func_ram print */
478                 total_memory = CONFIG_MAX_MEM_MAPPED;
479         }
480 #endif
481
482         return total_memory;
483 }
484
485 /*
486  * fsl_ddr_sdram_size() - This function only returns the size of the total
487  * memory without setting ddr control registers.
488  */
489 phys_size_t
490 fsl_ddr_sdram_size(void)
491 {
492         fsl_ddr_info_t  info;
493         unsigned long long total_memory = 0;
494
495         memset(&info, 0 , sizeof(fsl_ddr_info_t));
496
497         /* Compute it once normally. */
498         total_memory = fsl_ddr_compute(&info, STEP_GET_SPD, 1);
499
500         return total_memory;
501 }