blob: 3640a47957400bb805731c88488ed7c3cc80d269 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Shengzhou Liu49912402014-11-24 17:11:56 +08002/*
3 * Copyright 2014 Freescale Semiconductor, Inc.
Shengzhou Liu49912402014-11-24 17:11:56 +08004 */
5
6#include <common.h>
7#include <i2c.h>
8#include <hwconfig.h>
Simon Glass97589732020-05-10 11:40:02 -06009#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Shengzhou Liu49912402014-11-24 17:11:56 +080011#include <asm/mmu.h>
12#include <fsl_ddr_sdram.h>
13#include <fsl_ddr_dimm_params.h>
14#include <asm/fsl_law.h>
tang yuantian8dc02f32014-12-17 15:42:54 +080015#include <asm/mpc85xx_gpio.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
Shengzhou Liu49912402014-11-24 17:11:56 +080017
18DECLARE_GLOBAL_DATA_PTR;
19
20struct board_specific_parameters {
21 u32 n_ranks;
22 u32 datarate_mhz_high;
23 u32 rank_gb;
24 u32 clk_adjust;
25 u32 wrlvl_start;
26 u32 wrlvl_ctl_2;
27 u32 wrlvl_ctl_3;
28};
29
30/*
31 * datarate_mhz_high values need to be in ascending order
32 */
33static const struct board_specific_parameters udimm0[] = {
34 /*
35 * memory controller 0
36 * num| hi| rank| clk| wrlvl | wrlvl | wrlvl |
37 * ranks| mhz| GB |adjst| start | ctl2 | ctl3 |
38 */
Shengzhou Liuf1510e62016-05-04 10:20:22 +080039 {2, 833, 0, 8, 6, 0x06060607, 0x08080807,},
40 {2, 1350, 0, 8, 7, 0x0708080A, 0x0A0B0C09,},
41 {2, 1666, 0, 8, 7, 0x0808090B, 0x0C0D0E0A,},
42 {1, 833, 0, 8, 6, 0x06060607, 0x08080807,},
43 {1, 1350, 0, 8, 7, 0x0708080A, 0x0A0B0C09,},
44 {1, 1666, 0, 8, 7, 0x0808090B, 0x0C0D0E0A,},
Shengzhou Liu49912402014-11-24 17:11:56 +080045 {}
46};
47
48static const struct board_specific_parameters *udimms[] = {
49 udimm0,
50};
51
52void fsl_ddr_board_options(memctl_options_t *popts,
53 dimm_params_t *pdimm,
54 unsigned int ctrl_num)
55{
56 const struct board_specific_parameters *pbsp, *pbsp_highest = NULL;
57 ulong ddr_freq;
58 struct cpu_type *cpu = gd->arch.cpu;
59
60 if (ctrl_num > 1) {
61 printf("Not supported controller number %d\n", ctrl_num);
62 return;
63 }
64 if (!pdimm->n_ranks)
65 return;
66
67 pbsp = udimms[0];
68
69 /* Get clk_adjust according to the board ddr freqency and n_banks
70 * specified in board_specific_parameters table.
71 */
72 ddr_freq = get_ddr_freq(0) / 1000000;
73 while (pbsp->datarate_mhz_high) {
74 if (pbsp->n_ranks == pdimm->n_ranks &&
75 (pdimm->rank_density >> 30) >= pbsp->rank_gb) {
76 if (ddr_freq <= pbsp->datarate_mhz_high) {
77 popts->clk_adjust = pbsp->clk_adjust;
78 popts->wrlvl_start = pbsp->wrlvl_start;
79 popts->wrlvl_ctl_2 = pbsp->wrlvl_ctl_2;
80 popts->wrlvl_ctl_3 = pbsp->wrlvl_ctl_3;
81 goto found;
82 }
83 pbsp_highest = pbsp;
84 }
85 pbsp++;
86 }
87
88 if (pbsp_highest) {
89 printf("Error: board specific timing not found\n");
90 printf("for data rate %lu MT/s\n", ddr_freq);
91 printf("Trying to use the highest speed (%u) parameters\n",
92 pbsp_highest->datarate_mhz_high);
93 popts->clk_adjust = pbsp_highest->clk_adjust;
94 popts->wrlvl_start = pbsp_highest->wrlvl_start;
95 popts->wrlvl_ctl_2 = pbsp->wrlvl_ctl_2;
96 popts->wrlvl_ctl_3 = pbsp->wrlvl_ctl_3;
97 } else {
98 panic("DIMM is not supported by this board");
99 }
100found:
101 debug("Found timing match: n_ranks %d, data rate %d, rank_gb %d\n",
102 pbsp->n_ranks, pbsp->datarate_mhz_high, pbsp->rank_gb);
103 debug("\tclk_adjust %d, wrlvl_start %d, wrlvl_ctrl_2 0x%x, ",
104 pbsp->clk_adjust, pbsp->wrlvl_start, pbsp->wrlvl_ctl_2);
105 debug("wrlvl_ctrl_3 0x%x\n", pbsp->wrlvl_ctl_3);
106
107 /*
108 * Factors to consider for half-strength driver enable:
109 * - number of DIMMs installed
110 */
111 popts->half_strength_driver_enable = 0;
112 /*
113 * Write leveling override
114 */
115 popts->wrlvl_override = 1;
116 popts->wrlvl_sample = 0xf;
117
118 /*
119 * rtt and rtt_wr override
120 */
121 popts->rtt_override = 0;
122
123 /* Enable ZQ calibration */
124 popts->zq_en = 1;
125
126 /* DHC_EN =1, ODT = 75 Ohm */
127 popts->ddr_cdr1 = DDR_CDR1_DHC_EN | DDR_CDR1_ODT(DDR_CDR_ODT_OFF);
128 popts->ddr_cdr2 = DDR_CDR2_ODT(DDR_CDR_ODT_OFF);
129
130 /* T1023 supports max DDR bus 32bit width, T1024 supports DDR 64bit,
131 * force DDR bus width to 32bit for T1023
132 */
133 if (cpu->soc_ver == SVR_T1023)
134 popts->data_bus_width = DDR_DATA_BUS_WIDTH_32;
135
136#ifdef CONFIG_FORCE_DDR_DATA_BUS_WIDTH_32
137 /* for DDR bus 32bit test on T1024 */
138 popts->data_bus_width = DDR_DATA_BUS_WIDTH_32;
139#endif
Shengzhou Liu00d7e5b2015-03-27 15:48:34 +0800140
York Sun940ee4a2016-12-28 08:43:33 -0800141#ifdef CONFIG_TARGET_T1023RDB
Shengzhou Liu00d7e5b2015-03-27 15:48:34 +0800142 popts->wrlvl_ctl_2 = 0x07070606;
143 popts->half_strength_driver_enable = 1;
Shengzhou Liu29a53012016-11-15 17:15:21 +0800144 popts->cpo_sample = 0x43;
York Sunf9a03632016-12-28 08:43:34 -0800145#elif defined(CONFIG_TARGET_T1024RDB)
Shengzhou Liu29a53012016-11-15 17:15:21 +0800146 /* optimize cpo for erratum A-009942 */
147 popts->cpo_sample = 0x52;
Shengzhou Liu00d7e5b2015-03-27 15:48:34 +0800148#endif
Shengzhou Liu49912402014-11-24 17:11:56 +0800149}
150
Shengzhou Liu00d7e5b2015-03-27 15:48:34 +0800151#ifdef CONFIG_SYS_DDR_RAW_TIMING
152/* 2GB discrete DDR4 MT40A512M8HX on T1023RDB */
153dimm_params_t ddr_raw_timing = {
154 .n_ranks = 1,
155 .rank_density = 0x80000000,
156 .capacity = 0x80000000,
157 .primary_sdram_width = 32,
158 .ec_sdram_width = 8,
159 .registered_dimm = 0,
160 .mirrored_dimm = 0,
161 .n_row_addr = 15,
162 .n_col_addr = 10,
163 .bank_addr_bits = 2,
164 .bank_group_bits = 2,
165 .edc_config = 0,
166 .burst_lengths_bitmask = 0x0c,
167 .tckmin_x_ps = 938,
168 .tckmax_ps = 1500,
169 .caslat_x = 0x000DFA00,
170 .taa_ps = 13500,
171 .trcd_ps = 13500,
172 .trp_ps = 13500,
173 .tras_ps = 33000,
174 .trc_ps = 46500,
175 .trfc1_ps = 260000,
176 .trfc2_ps = 160000,
177 .trfc4_ps = 110000,
178 .tfaw_ps = 25000,
179 .trrds_ps = 3700,
180 .trrdl_ps = 5300,
181 .tccdl_ps = 5355,
182 .refresh_rate_ps = 7800000,
183 .dq_mapping[0] = 0x0,
184 .dq_mapping[1] = 0x0,
185 .dq_mapping[2] = 0x0,
186 .dq_mapping[3] = 0x0,
187 .dq_mapping[4] = 0x0,
188 .dq_mapping[5] = 0x0,
189 .dq_mapping[6] = 0x0,
190 .dq_mapping[7] = 0x0,
191 .dq_mapping[8] = 0x0,
192 .dq_mapping[9] = 0x0,
193 .dq_mapping[10] = 0x0,
194 .dq_mapping[11] = 0x0,
195 .dq_mapping[12] = 0x0,
196 .dq_mapping[13] = 0x0,
197 .dq_mapping[14] = 0x0,
198 .dq_mapping[15] = 0x0,
199 .dq_mapping[16] = 0x0,
200 .dq_mapping[17] = 0x0,
201 .dq_mapping_ors = 1,
202};
203
204int fsl_ddr_get_dimm_params(dimm_params_t *pdimm,
205 unsigned int controller_number,
206 unsigned int dimm_number)
207{
208 const char dimm_model[] = "Fixed DDR4 on board";
209
210 if (((controller_number == 0) && (dimm_number == 0)) ||
211 ((controller_number == 1) && (dimm_number == 0))) {
212 memcpy(pdimm, &ddr_raw_timing, sizeof(dimm_params_t));
213 memset(pdimm->mpart, 0, sizeof(pdimm->mpart));
214 memcpy(pdimm->mpart, dimm_model, sizeof(dimm_model) - 1);
215 }
216
217 return 0;
218}
219#endif
220
tang yuantian8dc02f32014-12-17 15:42:54 +0800221#if defined(CONFIG_DEEP_SLEEP)
222void board_mem_sleep_setup(void)
223{
224 void __iomem *cpld_base = (void *)CONFIG_SYS_CPLD_BASE;
225
226 /* does not provide HW signals for power management */
227 clrbits_8(cpld_base + 0x17, 0x40);
228 /* Disable MCKE isolation */
229 gpio_set_value(2, 0);
230 udelay(1);
231}
232#endif
233
Simon Glassd35f3382017-04-06 12:47:05 -0600234int dram_init(void)
Shengzhou Liu49912402014-11-24 17:11:56 +0800235{
236 phys_size_t dram_size;
237
238#if defined(CONFIG_SPL_BUILD) || !defined(CONFIG_RAMBOOT_PBL)
Shengzhou Liu00d7e5b2015-03-27 15:48:34 +0800239#ifndef CONFIG_SYS_DDR_RAW_TIMING
Shengzhou Liu49912402014-11-24 17:11:56 +0800240 puts("Initializing....using SPD\n");
Shengzhou Liu00d7e5b2015-03-27 15:48:34 +0800241#endif
Shengzhou Liu49912402014-11-24 17:11:56 +0800242 dram_size = fsl_ddr_sdram();
Shengzhou Liu49912402014-11-24 17:11:56 +0800243#else
244 /* DDR has been initialised by first stage boot loader */
245 dram_size = fsl_ddr_sdram_size();
246#endif
Shengzhou Liu0246ade2016-05-31 15:39:06 +0800247 dram_size = setup_ddr_tlbs(dram_size / 0x100000);
248 dram_size *= 0x100000;
tang yuantian8dc02f32014-12-17 15:42:54 +0800249
250#if defined(CONFIG_DEEP_SLEEP) && !defined(CONFIG_SPL_BUILD)
251 fsl_dp_resume();
252#endif
253
Simon Glass39f90ba2017-03-31 08:40:25 -0600254 gd->ram_size = dram_size;
255
256 return 0;
Shengzhou Liu49912402014-11-24 17:11:56 +0800257}