blob: 8bcd2fb5a7a97fa32e909ef9ddcd3a0e5dedf2c7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Mingkai Huf354b532011-07-07 12:29:15 +08002/*
3 * Copyright 2011 Freescale Semiconductor, Inc.
Mingkai Huf354b532011-07-07 12:29:15 +08004 */
5
6#include <common.h>
7#include <i2c.h>
8#include <hwconfig.h>
9#include <asm/mmu.h>
York Sunf0626592013-09-30 09:22:09 -070010#include <fsl_ddr_sdram.h>
11#include <fsl_ddr_dimm_params.h>
Mingkai Huf354b532011-07-07 12:29:15 +080012#include <asm/fsl_law.h>
13
Simon Glass39f90ba2017-03-31 08:40:25 -060014DECLARE_GLOBAL_DATA_PTR;
15
York Sunb6d2ef62011-10-03 09:19:53 -070016struct board_specific_parameters {
Mingkai Huf354b532011-07-07 12:29:15 +080017 u32 n_ranks;
York Sunb6d2ef62011-10-03 09:19:53 -070018 u32 datarate_mhz_high;
Mingkai Huf354b532011-07-07 12:29:15 +080019 u32 clk_adjust;
20 u32 wrlvl_start;
21 u32 cpo;
22 u32 write_data_delay;
Priyanka Jain4a717412013-09-25 10:41:19 +053023 u32 force_2t;
York Sunb6d2ef62011-10-03 09:19:53 -070024};
Mingkai Huf354b532011-07-07 12:29:15 +080025
26/*
York Sunb6d2ef62011-10-03 09:19:53 -070027 * This table contains all valid speeds we want to override with board
28 * specific parameters. datarate_mhz_high values need to be in ascending order
29 * for each n_ranks group.
30 *
Mingkai Huf354b532011-07-07 12:29:15 +080031 * ranges for parameters:
32 * wr_data_delay = 0-6
33 * clk adjust = 0-8
34 * cpo 2-0x1E (30)
35 */
York Sunb6d2ef62011-10-03 09:19:53 -070036static const struct board_specific_parameters dimm0[] = {
Mingkai Huf354b532011-07-07 12:29:15 +080037 /*
38 * memory controller 0
York Sunb6d2ef62011-10-03 09:19:53 -070039 * num| hi| clk| wrlvl | cpo |wrdata|2T
40 * ranks| mhz|adjst| start | delay|
Mingkai Huf354b532011-07-07 12:29:15 +080041 */
York Sunb6d2ef62011-10-03 09:19:53 -070042 {2, 750, 3, 5, 0xff, 2, 0},
43 {2, 1250, 4, 6, 0xff, 2, 0},
44 {2, 1350, 5, 7, 0xff, 2, 0},
45 {2, 1666, 5, 8, 0xff, 2, 0},
46 {}
Mingkai Huf354b532011-07-07 12:29:15 +080047};
48
49void fsl_ddr_board_options(memctl_options_t *popts,
50 dimm_params_t *pdimm,
51 unsigned int ctrl_num)
52{
York Sunb6d2ef62011-10-03 09:19:53 -070053 const struct board_specific_parameters *pbsp, *pbsp_highest = NULL;
Mingkai Huf354b532011-07-07 12:29:15 +080054 ulong ddr_freq;
55
York Sunb6d2ef62011-10-03 09:19:53 -070056 if (ctrl_num) {
57 printf("Wrong parameter for controller number %d", ctrl_num);
58 return;
59 }
60 if (!pdimm->n_ranks)
61 return;
62
63 pbsp = dimm0;
64
Mingkai Huf354b532011-07-07 12:29:15 +080065 /*
66 * Get clk_adjust, cpo, write_data_delay,2T, according to the board ddr
67 * freqency and n_banks specified in board_specific_parameters table.
68 */
69 ddr_freq = get_ddr_freq(0) / 1000000;
York Sunb6d2ef62011-10-03 09:19:53 -070070 while (pbsp->datarate_mhz_high) {
71 if (pbsp->n_ranks == pdimm->n_ranks) {
72 if (ddr_freq <= pbsp->datarate_mhz_high) {
73 popts->cpo_override = pbsp->cpo;
74 popts->write_data_delay =
75 pbsp->write_data_delay;
76 popts->clk_adjust = pbsp->clk_adjust;
77 popts->wrlvl_start = pbsp->wrlvl_start;
Priyanka Jain4a717412013-09-25 10:41:19 +053078 popts->twot_en = pbsp->force_2t;
York Sunb6d2ef62011-10-03 09:19:53 -070079 goto found;
80 }
81 pbsp_highest = pbsp;
Mingkai Huf354b532011-07-07 12:29:15 +080082 }
83 pbsp++;
84 }
85
York Sunb6d2ef62011-10-03 09:19:53 -070086 if (pbsp_highest) {
87 printf("Error: board specific timing not found "
88 "for data rate %lu MT/s!\n"
89 "Trying to use the highest speed (%u) parameters\n",
90 ddr_freq, pbsp_highest->datarate_mhz_high);
91 popts->cpo_override = pbsp_highest->cpo;
92 popts->write_data_delay = pbsp_highest->write_data_delay;
93 popts->clk_adjust = pbsp_highest->clk_adjust;
94 popts->wrlvl_start = pbsp_highest->wrlvl_start;
Priyanka Jain4a717412013-09-25 10:41:19 +053095 popts->twot_en = pbsp_highest->force_2t;
York Sunb6d2ef62011-10-03 09:19:53 -070096 } else {
97 panic("DIMM is not supported by this board");
Mingkai Huf354b532011-07-07 12:29:15 +080098 }
99
York Sunb6d2ef62011-10-03 09:19:53 -0700100found:
Mingkai Huf354b532011-07-07 12:29:15 +0800101 /*
102 * Factors to consider for half-strength driver enable:
103 * - number of DIMMs installed
104 */
105 popts->half_strength_driver_enable = 0;
106 /* Write leveling override */
107 popts->wrlvl_override = 1;
108 popts->wrlvl_sample = 0xf;
109
110 /* Rtt and Rtt_WR override */
111 popts->rtt_override = 0;
112
113 /* Enable ZQ calibration */
114 popts->zq_en = 1;
115
116 /* DHC_EN =1, ODT = 60 Ohm */
117 popts->ddr_cdr1 = DDR_CDR1_DHC_EN;
118}
119
Simon Glassd35f3382017-04-06 12:47:05 -0600120int dram_init(void)
Mingkai Huf354b532011-07-07 12:29:15 +0800121{
122 phys_size_t dram_size = 0;
123
124 puts("Initializing....");
125
126 if (fsl_use_spd()) {
127 puts("using SPD\n");
128 dram_size = fsl_ddr_sdram();
129 } else {
130 puts("no SPD and fixed parameters\n");
Simon Glass39f90ba2017-03-31 08:40:25 -0600131 return -ENXIO;
Mingkai Huf354b532011-07-07 12:29:15 +0800132 }
133
134 dram_size = setup_ddr_tlbs(dram_size / 0x100000);
135 dram_size *= 0x100000;
136
Wolfgang Denkf2bbb532011-07-25 10:13:53 +0200137 debug(" DDR: ");
Simon Glass39f90ba2017-03-31 08:40:25 -0600138 gd->ram_size = dram_size;
139
140 return 0;
Mingkai Huf354b532011-07-07 12:29:15 +0800141}