blob: 910058cefe16004a84708233f89cc9db0a0f8a06 [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>
Simon Glass97589732020-05-10 11:40:02 -06009#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Mingkai Huf354b532011-07-07 12:29:15 +080012#include <asm/mmu.h>
York Sunf0626592013-09-30 09:22:09 -070013#include <fsl_ddr_sdram.h>
14#include <fsl_ddr_dimm_params.h>
Mingkai Huf354b532011-07-07 12:29:15 +080015#include <asm/fsl_law.h>
16
Simon Glass39f90ba2017-03-31 08:40:25 -060017DECLARE_GLOBAL_DATA_PTR;
18
York Sunb6d2ef62011-10-03 09:19:53 -070019struct board_specific_parameters {
Mingkai Huf354b532011-07-07 12:29:15 +080020 u32 n_ranks;
York Sunb6d2ef62011-10-03 09:19:53 -070021 u32 datarate_mhz_high;
Mingkai Huf354b532011-07-07 12:29:15 +080022 u32 clk_adjust;
23 u32 wrlvl_start;
24 u32 cpo;
25 u32 write_data_delay;
Priyanka Jain4a717412013-09-25 10:41:19 +053026 u32 force_2t;
York Sunb6d2ef62011-10-03 09:19:53 -070027};
Mingkai Huf354b532011-07-07 12:29:15 +080028
29/*
York Sunb6d2ef62011-10-03 09:19:53 -070030 * This table contains all valid speeds we want to override with board
31 * specific parameters. datarate_mhz_high values need to be in ascending order
32 * for each n_ranks group.
33 *
Mingkai Huf354b532011-07-07 12:29:15 +080034 * ranges for parameters:
35 * wr_data_delay = 0-6
36 * clk adjust = 0-8
37 * cpo 2-0x1E (30)
38 */
York Sunb6d2ef62011-10-03 09:19:53 -070039static const struct board_specific_parameters dimm0[] = {
Mingkai Huf354b532011-07-07 12:29:15 +080040 /*
41 * memory controller 0
York Sunb6d2ef62011-10-03 09:19:53 -070042 * num| hi| clk| wrlvl | cpo |wrdata|2T
43 * ranks| mhz|adjst| start | delay|
Mingkai Huf354b532011-07-07 12:29:15 +080044 */
York Sunb6d2ef62011-10-03 09:19:53 -070045 {2, 750, 3, 5, 0xff, 2, 0},
46 {2, 1250, 4, 6, 0xff, 2, 0},
47 {2, 1350, 5, 7, 0xff, 2, 0},
48 {2, 1666, 5, 8, 0xff, 2, 0},
49 {}
Mingkai Huf354b532011-07-07 12:29:15 +080050};
51
52void fsl_ddr_board_options(memctl_options_t *popts,
53 dimm_params_t *pdimm,
54 unsigned int ctrl_num)
55{
York Sunb6d2ef62011-10-03 09:19:53 -070056 const struct board_specific_parameters *pbsp, *pbsp_highest = NULL;
Mingkai Huf354b532011-07-07 12:29:15 +080057 ulong ddr_freq;
58
York Sunb6d2ef62011-10-03 09:19:53 -070059 if (ctrl_num) {
60 printf("Wrong parameter for controller number %d", ctrl_num);
61 return;
62 }
63 if (!pdimm->n_ranks)
64 return;
65
66 pbsp = dimm0;
67
Mingkai Huf354b532011-07-07 12:29:15 +080068 /*
69 * Get clk_adjust, cpo, write_data_delay,2T, according to the board ddr
70 * freqency and n_banks specified in board_specific_parameters table.
71 */
72 ddr_freq = get_ddr_freq(0) / 1000000;
York Sunb6d2ef62011-10-03 09:19:53 -070073 while (pbsp->datarate_mhz_high) {
74 if (pbsp->n_ranks == pdimm->n_ranks) {
75 if (ddr_freq <= pbsp->datarate_mhz_high) {
76 popts->cpo_override = pbsp->cpo;
77 popts->write_data_delay =
78 pbsp->write_data_delay;
79 popts->clk_adjust = pbsp->clk_adjust;
80 popts->wrlvl_start = pbsp->wrlvl_start;
Priyanka Jain4a717412013-09-25 10:41:19 +053081 popts->twot_en = pbsp->force_2t;
York Sunb6d2ef62011-10-03 09:19:53 -070082 goto found;
83 }
84 pbsp_highest = pbsp;
Mingkai Huf354b532011-07-07 12:29:15 +080085 }
86 pbsp++;
87 }
88
York Sunb6d2ef62011-10-03 09:19:53 -070089 if (pbsp_highest) {
90 printf("Error: board specific timing not found "
91 "for data rate %lu MT/s!\n"
92 "Trying to use the highest speed (%u) parameters\n",
93 ddr_freq, pbsp_highest->datarate_mhz_high);
94 popts->cpo_override = pbsp_highest->cpo;
95 popts->write_data_delay = pbsp_highest->write_data_delay;
96 popts->clk_adjust = pbsp_highest->clk_adjust;
97 popts->wrlvl_start = pbsp_highest->wrlvl_start;
Priyanka Jain4a717412013-09-25 10:41:19 +053098 popts->twot_en = pbsp_highest->force_2t;
York Sunb6d2ef62011-10-03 09:19:53 -070099 } else {
100 panic("DIMM is not supported by this board");
Mingkai Huf354b532011-07-07 12:29:15 +0800101 }
102
York Sunb6d2ef62011-10-03 09:19:53 -0700103found:
Mingkai Huf354b532011-07-07 12:29:15 +0800104 /*
105 * Factors to consider for half-strength driver enable:
106 * - number of DIMMs installed
107 */
108 popts->half_strength_driver_enable = 0;
109 /* Write leveling override */
110 popts->wrlvl_override = 1;
111 popts->wrlvl_sample = 0xf;
112
113 /* Rtt and Rtt_WR override */
114 popts->rtt_override = 0;
115
116 /* Enable ZQ calibration */
117 popts->zq_en = 1;
118
119 /* DHC_EN =1, ODT = 60 Ohm */
120 popts->ddr_cdr1 = DDR_CDR1_DHC_EN;
121}
122
Simon Glassd35f3382017-04-06 12:47:05 -0600123int dram_init(void)
Mingkai Huf354b532011-07-07 12:29:15 +0800124{
125 phys_size_t dram_size = 0;
126
127 puts("Initializing....");
128
129 if (fsl_use_spd()) {
130 puts("using SPD\n");
131 dram_size = fsl_ddr_sdram();
132 } else {
133 puts("no SPD and fixed parameters\n");
Simon Glass39f90ba2017-03-31 08:40:25 -0600134 return -ENXIO;
Mingkai Huf354b532011-07-07 12:29:15 +0800135 }
136
137 dram_size = setup_ddr_tlbs(dram_size / 0x100000);
138 dram_size *= 0x100000;
139
Wolfgang Denkf2bbb532011-07-25 10:13:53 +0200140 debug(" DDR: ");
Simon Glass39f90ba2017-03-31 08:40:25 -0600141 gd->ram_size = dram_size;
142
143 return 0;
Mingkai Huf354b532011-07-07 12:29:15 +0800144}