blob: 7c5147f4745759455ba92f6022cb41e82ebfb7b8 [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
Tony Dinhe2c524b2023-01-18 19:03:04 -08009#if defined(CONFIG_DDR4)
10static char *ddr_type = "DDR4";
11#else /* CONFIG_DDR4 */
Stefan Roese5ffceb82015-03-26 15:36:56 +010012static char *ddr_type = "DDR3";
Tony Dinhe2c524b2023-01-18 19:03:04 -080013#endif /* CONFIG_DDR4 */
Stefan Roese5ffceb82015-03-26 15:36:56 +010014
15/*
Chris Packham1a07d212018-05-10 13:28:29 +120016 * generic_init_controller controls D-unit configuration:
17 * '1' - dynamic D-unit configuration,
Stefan Roese5ffceb82015-03-26 15:36:56 +010018 */
19u8 generic_init_controller = 1;
20
Chris Packham1a07d212018-05-10 13:28:29 +120021static int mv_ddr_training_params_set(u8 dev_num);
Stefan Roese5ffceb82015-03-26 15:36:56 +010022
23/*
Stefan Roese5ffceb82015-03-26 15:36:56 +010024 * Name: ddr3_init - Main DDR3 Init function
25 * Desc: This routine initialize the DDR3 MC and runs HW training.
26 * Args: None.
27 * Notes:
28 * Returns: None.
29 */
30int ddr3_init(void)
31{
Stefan Roese5ffceb82015-03-26 15:36:56 +010032 int status;
Chris Packham1a07d212018-05-10 13:28:29 +120033 int is_manual_cal_done;
Stefan Roese5ffceb82015-03-26 15:36:56 +010034
Chris Packham1a07d212018-05-10 13:28:29 +120035 /* Print mv_ddr version */
36 mv_ddr_ver_print();
Stefan Roese5ffceb82015-03-26 15:36:56 +010037
Chris Packham1a07d212018-05-10 13:28:29 +120038 mv_ddr_pre_training_fixup();
Stefan Roese5ffceb82015-03-26 15:36:56 +010039
Chris Packham1a07d212018-05-10 13:28:29 +120040 /* SoC/Board special initializations */
41 mv_ddr_pre_training_soc_config(ddr_type);
Stefan Roese5ffceb82015-03-26 15:36:56 +010042
Chris Packham1a07d212018-05-10 13:28:29 +120043 /* Set log level for training library */
Marek BehĂșne8bd7582024-06-18 17:34:28 +020044 if (!IS_ENABLED(CONFIG_DDR_IMMUTABLE_DEBUG_SETTINGS))
45 mv_ddr_user_log_level_set(DEBUG_BLOCK_ALL);
Stefan Roese5ffceb82015-03-26 15:36:56 +010046
Chris Packham1a07d212018-05-10 13:28:29 +120047 mv_ddr_early_init();
Stefan Roese5ffceb82015-03-26 15:36:56 +010048
Chris Packham4bf81db2018-12-03 14:26:49 +130049 if (mv_ddr_topology_map_update()) {
Chris Packham1a07d212018-05-10 13:28:29 +120050 printf("mv_ddr: failed to update topology\n");
51 return MV_FAIL;
Stefan Roese5ffceb82015-03-26 15:36:56 +010052 }
53
Chris Packham1a07d212018-05-10 13:28:29 +120054 if (mv_ddr_early_init2() != MV_OK)
55 return MV_FAIL;
Stefan Roese5ffceb82015-03-26 15:36:56 +010056
Chris Packham1a07d212018-05-10 13:28:29 +120057 /* Set training algorithm's parameters */
58 status = mv_ddr_training_params_set(0);
Stefan Roese5ffceb82015-03-26 15:36:56 +010059 if (MV_OK != status)
60 return status;
Stefan Roese5ffceb82015-03-26 15:36:56 +010061
Chris Packham1a07d212018-05-10 13:28:29 +120062 mv_ddr_mc_config();
Stefan Roese5ffceb82015-03-26 15:36:56 +010063
Chris Packham1a07d212018-05-10 13:28:29 +120064 is_manual_cal_done = mv_ddr_manual_cal_do();
Stefan Roese5ffceb82015-03-26 15:36:56 +010065
Chris Packham1a07d212018-05-10 13:28:29 +120066 mv_ddr_mc_init();
Stefan Roese5ffceb82015-03-26 15:36:56 +010067
Chris Packham1a07d212018-05-10 13:28:29 +120068 if (!is_manual_cal_done) {
Tony Dinhe2c524b2023-01-18 19:03:04 -080069#if defined(CONFIG_DDR4)
70 status = mv_ddr4_calibration_adjust(0, 1, 0);
71 if (status != MV_OK) {
72 printf("%s: failed (0x%x)\n", __func__, status);
73 return status;
74 }
75#endif
Stefan Roese5ffceb82015-03-26 15:36:56 +010076 }
77
Stefan Roese5ffceb82015-03-26 15:36:56 +010078
Chris Packham1a07d212018-05-10 13:28:29 +120079 status = ddr3_silicon_post_init();
80 if (MV_OK != status) {
81 printf("DDR3 Post Init - FAILED 0x%x\n", status);
82 return status;
Stefan Roese5ffceb82015-03-26 15:36:56 +010083 }
84
Chris Packham1a07d212018-05-10 13:28:29 +120085 /* PHY initialization (Training) */
86 status = hws_ddr3_tip_run_alg(0, ALGO_TYPE_DYNAMIC);
87 if (MV_OK != status) {
88 printf("%s Training Sequence - FAILED\n", ddr_type);
89 return status;
Stefan Roese5ffceb82015-03-26 15:36:56 +010090 }
91
Stefan Roese5ffceb82015-03-26 15:36:56 +010092
Chris Packham1a07d212018-05-10 13:28:29 +120093 /* Post MC/PHY initializations */
94 mv_ddr_post_training_soc_config(ddr_type);
Stefan Roese5ffceb82015-03-26 15:36:56 +010095
Chris Packham1a07d212018-05-10 13:28:29 +120096 mv_ddr_post_training_fixup();
Stefan Roese5ffceb82015-03-26 15:36:56 +010097
Chris Packham4bf81db2018-12-03 14:26:49 +130098 if (mv_ddr_is_ecc_ena())
99 mv_ddr_mem_scrubbing();
Stefan Roese5ffceb82015-03-26 15:36:56 +0100100
Chris Packham1a07d212018-05-10 13:28:29 +1200101 printf("mv_ddr: completed successfully\n");
Stefan Roese5ffceb82015-03-26 15:36:56 +0100102
103 return MV_OK;
104}
105
Stefan Roese5ffceb82015-03-26 15:36:56 +0100106/*
Chris Packham1a07d212018-05-10 13:28:29 +1200107 * Name: mv_ddr_training_params_set
Stefan Roese5ffceb82015-03-26 15:36:56 +0100108 * Desc:
109 * Args:
Chris Packham1a07d212018-05-10 13:28:29 +1200110 * Notes: sets internal training params
Stefan Roese5ffceb82015-03-26 15:36:56 +0100111 * Returns:
112 */
Chris Packham1a07d212018-05-10 13:28:29 +1200113static int mv_ddr_training_params_set(u8 dev_num)
Stefan Roese5ffceb82015-03-26 15:36:56 +0100114{
115 struct tune_train_params params;
Baruch Siach4951d422021-02-19 17:11:17 +0100116 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +0100117 int status;
Chris Packham1a07d212018-05-10 13:28:29 +1200118 u32 cs_num;
Chris Packhame422adc2020-01-30 12:50:44 +1300119 int ck_delay;
Chris Packham1a07d212018-05-10 13:28:29 +1200120
Chris Packham4bf81db2018-12-03 14:26:49 +1300121 cs_num = mv_ddr_cs_num_get();
Chris Packhame422adc2020-01-30 12:50:44 +1300122 ck_delay = mv_ddr_ck_delay_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +0100123
124 /* NOTE: do not remove any field initilization */
125 params.ck_delay = TUNE_TRAINING_PARAMS_CK_DELAY;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100126 params.phy_reg3_val = TUNE_TRAINING_PARAMS_PHYREG3VAL;
Chris Packham1a07d212018-05-10 13:28:29 +1200127 params.g_zpri_data = TUNE_TRAINING_PARAMS_PRI_DATA;
128 params.g_znri_data = TUNE_TRAINING_PARAMS_NRI_DATA;
129 params.g_zpri_ctrl = TUNE_TRAINING_PARAMS_PRI_CTRL;
130 params.g_znri_ctrl = TUNE_TRAINING_PARAMS_NRI_CTRL;
131 params.g_znodt_data = TUNE_TRAINING_PARAMS_N_ODT_DATA;
132 params.g_zpodt_ctrl = TUNE_TRAINING_PARAMS_P_ODT_CTRL;
133 params.g_znodt_ctrl = TUNE_TRAINING_PARAMS_N_ODT_CTRL;
134
Tony Dinhe2c524b2023-01-18 19:03:04 -0800135#if defined(CONFIG_DDR4)
136 params.g_zpodt_data = TUNE_TRAINING_PARAMS_P_ODT_DATA_DDR4;
137 params.g_odt_config = TUNE_TRAINING_PARAMS_ODT_CONFIG_DDR4;
138 params.g_rtt_nom = TUNE_TRAINING_PARAMS_RTT_NOM_DDR4;
139 params.g_dic = TUNE_TRAINING_PARAMS_DIC_DDR4;
140 if (cs_num == 1) {
141 params.g_rtt_wr = TUNE_TRAINING_PARAMS_RTT_WR_1CS;
142 params.g_rtt_park = TUNE_TRAINING_PARAMS_RTT_PARK_1CS;
143 } else {
144 params.g_rtt_wr = TUNE_TRAINING_PARAMS_RTT_WR_2CS;
145 params.g_rtt_park = TUNE_TRAINING_PARAMS_RTT_PARK_2CS;
146 }
147#else /* CONFIG_DDR4 */
Chris Packham1a07d212018-05-10 13:28:29 +1200148 params.g_zpodt_data = TUNE_TRAINING_PARAMS_P_ODT_DATA;
149 params.g_dic = TUNE_TRAINING_PARAMS_DIC;
150 params.g_rtt_nom = TUNE_TRAINING_PARAMS_RTT_NOM;
151 if (cs_num == 1) {
152 params.g_rtt_wr = TUNE_TRAINING_PARAMS_RTT_WR_1CS;
153 params.g_odt_config = TUNE_TRAINING_PARAMS_ODT_CONFIG_1CS;
154 } else {
155 params.g_rtt_wr = TUNE_TRAINING_PARAMS_RTT_WR_2CS;
156 params.g_odt_config = TUNE_TRAINING_PARAMS_ODT_CONFIG_2CS;
157 }
Tony Dinhe2c524b2023-01-18 19:03:04 -0800158#endif /* CONFIG_DDR4 */
Stefan Roese5ffceb82015-03-26 15:36:56 +0100159
Chris Packhame422adc2020-01-30 12:50:44 +1300160 if (ck_delay > 0)
161 params.ck_delay = ck_delay;
162
Baruch Siach4951d422021-02-19 17:11:17 +0100163 /* Use platform specific override ODT value */
164 if (tm->odt_config)
165 params.g_odt_config = tm->odt_config;
166
Stefan Roese5ffceb82015-03-26 15:36:56 +0100167 status = ddr3_tip_tune_training_params(dev_num, &params);
168 if (MV_OK != status) {
169 printf("%s Training Sequence - FAILED\n", ddr_type);
170 return status;
171 }
172
173 return MV_OK;
174}