blob: 27dbf4f44b9858f1db68c2c1117e63e44bfd57c2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stefan Roese5ffceb82015-03-26 15:36:56 +01002/*
3 * Copyright (C) Marvell International Ltd. and its affiliates
Stefan Roese5ffceb82015-03-26 15:36:56 +01004 */
5
Stefan Roese5ffceb82015-03-26 15:36:56 +01006#include "ddr3_init.h"
Chris Packham1a07d212018-05-10 13:28:29 +12007#include "mv_ddr_common.h"
Stefan Roese5ffceb82015-03-26 15:36:56 +01008
Chris Packham1a07d212018-05-10 13:28:29 +12009/*
10 * Translates topology map definitions to real memory size in bits
11 * (per values in ddr3_training_ip_def.h)
12 */
Stefan Roese5ffceb82015-03-26 15:36:56 +010013u32 mem_size[] = {
Chris Packham1a07d212018-05-10 13:28:29 +120014 ADDR_SIZE_512MB,
15 ADDR_SIZE_1GB,
16 ADDR_SIZE_2GB,
17 ADDR_SIZE_4GB,
Stefan Roese5ffceb82015-03-26 15:36:56 +010018 ADDR_SIZE_8GB
19};
20
21static char *ddr_type = "DDR3";
22
23/*
Chris Packham1a07d212018-05-10 13:28:29 +120024 * generic_init_controller controls D-unit configuration:
25 * '1' - dynamic D-unit configuration,
Stefan Roese5ffceb82015-03-26 15:36:56 +010026 */
27u8 generic_init_controller = 1;
28
Chris Packham1a07d212018-05-10 13:28:29 +120029static int mv_ddr_training_params_set(u8 dev_num);
Stefan Roese5ffceb82015-03-26 15:36:56 +010030
31/*
Stefan Roese5ffceb82015-03-26 15:36:56 +010032 * Name: ddr3_init - Main DDR3 Init function
33 * Desc: This routine initialize the DDR3 MC and runs HW training.
34 * Args: None.
35 * Notes:
36 * Returns: None.
37 */
38int ddr3_init(void)
39{
Chris Packham1a07d212018-05-10 13:28:29 +120040 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
41 u32 octets_per_if_num;
Stefan Roese5ffceb82015-03-26 15:36:56 +010042 int status;
Chris Packham1a07d212018-05-10 13:28:29 +120043 int is_manual_cal_done;
Stefan Roese5ffceb82015-03-26 15:36:56 +010044
Chris Packham1a07d212018-05-10 13:28:29 +120045 /* Print mv_ddr version */
46 mv_ddr_ver_print();
Stefan Roese5ffceb82015-03-26 15:36:56 +010047
Chris Packham1a07d212018-05-10 13:28:29 +120048 mv_ddr_pre_training_fixup();
Stefan Roese5ffceb82015-03-26 15:36:56 +010049
Chris Packham1a07d212018-05-10 13:28:29 +120050 /* SoC/Board special initializations */
51 mv_ddr_pre_training_soc_config(ddr_type);
Stefan Roese5ffceb82015-03-26 15:36:56 +010052
Chris Packham1a07d212018-05-10 13:28:29 +120053 /* Set log level for training library */
54 mv_ddr_user_log_level_set(DEBUG_BLOCK_ALL);
Stefan Roese5ffceb82015-03-26 15:36:56 +010055
Chris Packham1a07d212018-05-10 13:28:29 +120056 mv_ddr_early_init();
Stefan Roese5ffceb82015-03-26 15:36:56 +010057
Chris Packham1a07d212018-05-10 13:28:29 +120058 if (mv_ddr_topology_map_update() == NULL) {
59 printf("mv_ddr: failed to update topology\n");
60 return MV_FAIL;
Stefan Roese5ffceb82015-03-26 15:36:56 +010061 }
62
Chris Packham1a07d212018-05-10 13:28:29 +120063 if (mv_ddr_early_init2() != MV_OK)
64 return MV_FAIL;
Stefan Roese5ffceb82015-03-26 15:36:56 +010065
Chris Packham1a07d212018-05-10 13:28:29 +120066 /* Set training algorithm's parameters */
67 status = mv_ddr_training_params_set(0);
Stefan Roese5ffceb82015-03-26 15:36:56 +010068 if (MV_OK != status)
69 return status;
Stefan Roese5ffceb82015-03-26 15:36:56 +010070
Stefan Roese5ffceb82015-03-26 15:36:56 +010071
Chris Packham1a07d212018-05-10 13:28:29 +120072 mv_ddr_mc_config();
Stefan Roese5ffceb82015-03-26 15:36:56 +010073
Chris Packham1a07d212018-05-10 13:28:29 +120074 is_manual_cal_done = mv_ddr_manual_cal_do();
Stefan Roese5ffceb82015-03-26 15:36:56 +010075
Chris Packham1a07d212018-05-10 13:28:29 +120076 mv_ddr_mc_init();
Stefan Roese5ffceb82015-03-26 15:36:56 +010077
Chris Packham1a07d212018-05-10 13:28:29 +120078 if (!is_manual_cal_done) {
Stefan Roese5ffceb82015-03-26 15:36:56 +010079 }
80
Stefan Roese5ffceb82015-03-26 15:36:56 +010081
Chris Packham1a07d212018-05-10 13:28:29 +120082 status = ddr3_silicon_post_init();
83 if (MV_OK != status) {
84 printf("DDR3 Post Init - FAILED 0x%x\n", status);
85 return status;
Stefan Roese5ffceb82015-03-26 15:36:56 +010086 }
87
Chris Packham1a07d212018-05-10 13:28:29 +120088 /* PHY initialization (Training) */
89 status = hws_ddr3_tip_run_alg(0, ALGO_TYPE_DYNAMIC);
90 if (MV_OK != status) {
91 printf("%s Training Sequence - FAILED\n", ddr_type);
92 return status;
Stefan Roese5ffceb82015-03-26 15:36:56 +010093 }
94
Chris Packham1a07d212018-05-10 13:28:29 +120095#if defined(CONFIG_PHY_STATIC_PRINT)
96 mv_ddr_phy_static_print();
Stefan Roese5ffceb82015-03-26 15:36:56 +010097#endif
98
Chris Packham1a07d212018-05-10 13:28:29 +120099 /* Post MC/PHY initializations */
100 mv_ddr_post_training_soc_config(ddr_type);
Stefan Roese5ffceb82015-03-26 15:36:56 +0100101
Chris Packham1a07d212018-05-10 13:28:29 +1200102 mv_ddr_post_training_fixup();
Stefan Roese5ffceb82015-03-26 15:36:56 +0100103
Chris Packham1a07d212018-05-10 13:28:29 +1200104 octets_per_if_num = ddr3_tip_dev_attr_get(0, MV_ATTR_OCTET_PER_INTERFACE);
105 if (ddr3_if_ecc_enabled()) {
106 if (MV_DDR_IS_64BIT_DRAM_MODE(tm->bus_act_mask) ||
107 MV_DDR_IS_32BIT_IN_64BIT_DRAM_MODE(tm->bus_act_mask, octets_per_if_num))
108 mv_ddr_mem_scrubbing();
109 else
110 ddr3_new_tip_ecc_scrub();
Stefan Roese5ffceb82015-03-26 15:36:56 +0100111 }
112
Chris Packham1a07d212018-05-10 13:28:29 +1200113 printf("mv_ddr: completed successfully\n");
Stefan Roese5ffceb82015-03-26 15:36:56 +0100114
115 return MV_OK;
116}
117
Chris Packham1a07d212018-05-10 13:28:29 +1200118uint64_t mv_ddr_get_memory_size_per_cs_in_bits(void)
Stefan Roese5ffceb82015-03-26 15:36:56 +0100119{
Chris Packham1a07d212018-05-10 13:28:29 +1200120 uint64_t memory_size_per_cs;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100121
Chris Packham1a07d212018-05-10 13:28:29 +1200122 u32 bus_cnt, num_of_active_bus = 0;
123 u32 num_of_sub_phys_per_ddr_unit = 0;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100124
Chris Packham1a07d212018-05-10 13:28:29 +1200125 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +0100126
Chris Packham1a07d212018-05-10 13:28:29 +1200127 u32 octets_per_if_num = ddr3_tip_dev_attr_get(DEV_NUM_0, MV_ATTR_OCTET_PER_INTERFACE);
128
129 /* count the number of active bus */
130 for (bus_cnt = 0; bus_cnt < octets_per_if_num - 1/* ignore ecc octet */; bus_cnt++) {
131 VALIDATE_BUS_ACTIVE(tm->bus_act_mask, bus_cnt);
132 num_of_active_bus++;
133 }
134
135 /* calculate number of sub-phys per ddr unit */
136 if (tm->interface_params[0].bus_width/* supports only single interface */ == MV_DDR_DEV_WIDTH_16BIT)
137 num_of_sub_phys_per_ddr_unit = TWO_SUB_PHYS;
138 if (tm->interface_params[0].bus_width/* supports only single interface */ == MV_DDR_DEV_WIDTH_8BIT)
139 num_of_sub_phys_per_ddr_unit = SINGLE_SUB_PHY;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100140
Chris Packham1a07d212018-05-10 13:28:29 +1200141 /* calculate dram size per cs */
142 memory_size_per_cs = (uint64_t)mem_size[tm->interface_params[0].memory_size] * (uint64_t)num_of_active_bus
143 / (uint64_t)num_of_sub_phys_per_ddr_unit * (uint64_t)MV_DDR_NUM_BITS_IN_BYTE;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100144
Chris Packham1a07d212018-05-10 13:28:29 +1200145 return memory_size_per_cs;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100146}
147
Chris Packham1a07d212018-05-10 13:28:29 +1200148uint64_t mv_ddr_get_total_memory_size_in_bits(void)
Stefan Roese5ffceb82015-03-26 15:36:56 +0100149{
Chris Packham1a07d212018-05-10 13:28:29 +1200150 uint64_t total_memory_size = 0;
151 uint64_t memory_size_per_cs = 0;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100152
Chris Packham1a07d212018-05-10 13:28:29 +1200153 /* get the number of cs */
154 u32 max_cs = ddr3_tip_max_cs_get(DEV_NUM_0);
Stefan Roese5ffceb82015-03-26 15:36:56 +0100155
Chris Packham1a07d212018-05-10 13:28:29 +1200156 memory_size_per_cs = mv_ddr_get_memory_size_per_cs_in_bits();
157 total_memory_size = (uint64_t)max_cs * memory_size_per_cs;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100158
Chris Packham1a07d212018-05-10 13:28:29 +1200159 return total_memory_size;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100160}
161
Chris Packham1a07d212018-05-10 13:28:29 +1200162int ddr3_if_ecc_enabled(void)
Stefan Roese5ffceb82015-03-26 15:36:56 +0100163{
Chris Packham1a07d212018-05-10 13:28:29 +1200164 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +0100165
Chris Packham1a07d212018-05-10 13:28:29 +1200166 if (DDR3_IS_ECC_PUP4_MODE(tm->bus_act_mask) ||
167 DDR3_IS_ECC_PUP3_MODE(tm->bus_act_mask) ||
168 DDR3_IS_ECC_PUP8_MODE(tm->bus_act_mask))
169 return 1;
170 else
171 return 0;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100172}
173
Stefan Roese5ffceb82015-03-26 15:36:56 +0100174/*
Chris Packham1a07d212018-05-10 13:28:29 +1200175 * Name: mv_ddr_training_params_set
Stefan Roese5ffceb82015-03-26 15:36:56 +0100176 * Desc:
177 * Args:
Chris Packham1a07d212018-05-10 13:28:29 +1200178 * Notes: sets internal training params
Stefan Roese5ffceb82015-03-26 15:36:56 +0100179 * Returns:
180 */
Chris Packham1a07d212018-05-10 13:28:29 +1200181static int mv_ddr_training_params_set(u8 dev_num)
Stefan Roese5ffceb82015-03-26 15:36:56 +0100182{
183 struct tune_train_params params;
184 int status;
Chris Packham1a07d212018-05-10 13:28:29 +1200185 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
186 u32 if_id;
187 u32 cs_num;
188
189 CHECK_STATUS(ddr3_tip_get_first_active_if
190 (dev_num, tm->if_act_mask,
191 &if_id));
192
193 CHECK_STATUS(calc_cs_num(dev_num, if_id, &cs_num));
Stefan Roese5ffceb82015-03-26 15:36:56 +0100194
195 /* NOTE: do not remove any field initilization */
196 params.ck_delay = TUNE_TRAINING_PARAMS_CK_DELAY;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100197 params.phy_reg3_val = TUNE_TRAINING_PARAMS_PHYREG3VAL;
Chris Packham1a07d212018-05-10 13:28:29 +1200198 params.g_zpri_data = TUNE_TRAINING_PARAMS_PRI_DATA;
199 params.g_znri_data = TUNE_TRAINING_PARAMS_NRI_DATA;
200 params.g_zpri_ctrl = TUNE_TRAINING_PARAMS_PRI_CTRL;
201 params.g_znri_ctrl = TUNE_TRAINING_PARAMS_NRI_CTRL;
202 params.g_znodt_data = TUNE_TRAINING_PARAMS_N_ODT_DATA;
203 params.g_zpodt_ctrl = TUNE_TRAINING_PARAMS_P_ODT_CTRL;
204 params.g_znodt_ctrl = TUNE_TRAINING_PARAMS_N_ODT_CTRL;
205
206 params.g_zpodt_data = TUNE_TRAINING_PARAMS_P_ODT_DATA;
207 params.g_dic = TUNE_TRAINING_PARAMS_DIC;
208 params.g_rtt_nom = TUNE_TRAINING_PARAMS_RTT_NOM;
209 if (cs_num == 1) {
210 params.g_rtt_wr = TUNE_TRAINING_PARAMS_RTT_WR_1CS;
211 params.g_odt_config = TUNE_TRAINING_PARAMS_ODT_CONFIG_1CS;
212 } else {
213 params.g_rtt_wr = TUNE_TRAINING_PARAMS_RTT_WR_2CS;
214 params.g_odt_config = TUNE_TRAINING_PARAMS_ODT_CONFIG_2CS;
215 }
Stefan Roese5ffceb82015-03-26 15:36:56 +0100216
217 status = ddr3_tip_tune_training_params(dev_num, &params);
218 if (MV_OK != status) {
219 printf("%s Training Sequence - FAILED\n", ddr_type);
220 return status;
221 }
222
223 return MV_OK;
224}