blob: 27eb3ac17357d731f8a958180b13810f7489ccd0 [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 */
44 mv_ddr_user_log_level_set(DEBUG_BLOCK_ALL);
Stefan Roese5ffceb82015-03-26 15:36:56 +010045
Chris Packham1a07d212018-05-10 13:28:29 +120046 mv_ddr_early_init();
Stefan Roese5ffceb82015-03-26 15:36:56 +010047
Chris Packham4bf81db2018-12-03 14:26:49 +130048 if (mv_ddr_topology_map_update()) {
Chris Packham1a07d212018-05-10 13:28:29 +120049 printf("mv_ddr: failed to update topology\n");
50 return MV_FAIL;
Stefan Roese5ffceb82015-03-26 15:36:56 +010051 }
52
Chris Packham1a07d212018-05-10 13:28:29 +120053 if (mv_ddr_early_init2() != MV_OK)
54 return MV_FAIL;
Stefan Roese5ffceb82015-03-26 15:36:56 +010055
Chris Packham1a07d212018-05-10 13:28:29 +120056 /* Set training algorithm's parameters */
57 status = mv_ddr_training_params_set(0);
Stefan Roese5ffceb82015-03-26 15:36:56 +010058 if (MV_OK != status)
59 return status;
Stefan Roese5ffceb82015-03-26 15:36:56 +010060
Chris Packham1a07d212018-05-10 13:28:29 +120061 mv_ddr_mc_config();
Stefan Roese5ffceb82015-03-26 15:36:56 +010062
Chris Packham1a07d212018-05-10 13:28:29 +120063 is_manual_cal_done = mv_ddr_manual_cal_do();
Stefan Roese5ffceb82015-03-26 15:36:56 +010064
Chris Packham1a07d212018-05-10 13:28:29 +120065 mv_ddr_mc_init();
Stefan Roese5ffceb82015-03-26 15:36:56 +010066
Chris Packham1a07d212018-05-10 13:28:29 +120067 if (!is_manual_cal_done) {
Tony Dinhe2c524b2023-01-18 19:03:04 -080068#if defined(CONFIG_DDR4)
69 status = mv_ddr4_calibration_adjust(0, 1, 0);
70 if (status != MV_OK) {
71 printf("%s: failed (0x%x)\n", __func__, status);
72 return status;
73 }
74#endif
Stefan Roese5ffceb82015-03-26 15:36:56 +010075 }
76
Stefan Roese5ffceb82015-03-26 15:36:56 +010077
Chris Packham1a07d212018-05-10 13:28:29 +120078 status = ddr3_silicon_post_init();
79 if (MV_OK != status) {
80 printf("DDR3 Post Init - FAILED 0x%x\n", status);
81 return status;
Stefan Roese5ffceb82015-03-26 15:36:56 +010082 }
83
Chris Packham1a07d212018-05-10 13:28:29 +120084 /* PHY initialization (Training) */
85 status = hws_ddr3_tip_run_alg(0, ALGO_TYPE_DYNAMIC);
86 if (MV_OK != status) {
87 printf("%s Training Sequence - FAILED\n", ddr_type);
88 return status;
Stefan Roese5ffceb82015-03-26 15:36:56 +010089 }
90
Stefan Roese5ffceb82015-03-26 15:36:56 +010091
Chris Packham1a07d212018-05-10 13:28:29 +120092 /* Post MC/PHY initializations */
93 mv_ddr_post_training_soc_config(ddr_type);
Stefan Roese5ffceb82015-03-26 15:36:56 +010094
Chris Packham1a07d212018-05-10 13:28:29 +120095 mv_ddr_post_training_fixup();
Stefan Roese5ffceb82015-03-26 15:36:56 +010096
Chris Packham4bf81db2018-12-03 14:26:49 +130097 if (mv_ddr_is_ecc_ena())
98 mv_ddr_mem_scrubbing();
Stefan Roese5ffceb82015-03-26 15:36:56 +010099
Chris Packham1a07d212018-05-10 13:28:29 +1200100 printf("mv_ddr: completed successfully\n");
Stefan Roese5ffceb82015-03-26 15:36:56 +0100101
102 return MV_OK;
103}
104
Stefan Roese5ffceb82015-03-26 15:36:56 +0100105/*
Chris Packham1a07d212018-05-10 13:28:29 +1200106 * Name: mv_ddr_training_params_set
Stefan Roese5ffceb82015-03-26 15:36:56 +0100107 * Desc:
108 * Args:
Chris Packham1a07d212018-05-10 13:28:29 +1200109 * Notes: sets internal training params
Stefan Roese5ffceb82015-03-26 15:36:56 +0100110 * Returns:
111 */
Chris Packham1a07d212018-05-10 13:28:29 +1200112static int mv_ddr_training_params_set(u8 dev_num)
Stefan Roese5ffceb82015-03-26 15:36:56 +0100113{
114 struct tune_train_params params;
Baruch Siach4951d422021-02-19 17:11:17 +0100115 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +0100116 int status;
Chris Packham1a07d212018-05-10 13:28:29 +1200117 u32 cs_num;
Chris Packhame422adc2020-01-30 12:50:44 +1300118 int ck_delay;
Chris Packham1a07d212018-05-10 13:28:29 +1200119
Chris Packham4bf81db2018-12-03 14:26:49 +1300120 cs_num = mv_ddr_cs_num_get();
Chris Packhame422adc2020-01-30 12:50:44 +1300121 ck_delay = mv_ddr_ck_delay_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +0100122
123 /* NOTE: do not remove any field initilization */
124 params.ck_delay = TUNE_TRAINING_PARAMS_CK_DELAY;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100125 params.phy_reg3_val = TUNE_TRAINING_PARAMS_PHYREG3VAL;
Chris Packham1a07d212018-05-10 13:28:29 +1200126 params.g_zpri_data = TUNE_TRAINING_PARAMS_PRI_DATA;
127 params.g_znri_data = TUNE_TRAINING_PARAMS_NRI_DATA;
128 params.g_zpri_ctrl = TUNE_TRAINING_PARAMS_PRI_CTRL;
129 params.g_znri_ctrl = TUNE_TRAINING_PARAMS_NRI_CTRL;
130 params.g_znodt_data = TUNE_TRAINING_PARAMS_N_ODT_DATA;
131 params.g_zpodt_ctrl = TUNE_TRAINING_PARAMS_P_ODT_CTRL;
132 params.g_znodt_ctrl = TUNE_TRAINING_PARAMS_N_ODT_CTRL;
133
Tony Dinhe2c524b2023-01-18 19:03:04 -0800134#if defined(CONFIG_DDR4)
135 params.g_zpodt_data = TUNE_TRAINING_PARAMS_P_ODT_DATA_DDR4;
136 params.g_odt_config = TUNE_TRAINING_PARAMS_ODT_CONFIG_DDR4;
137 params.g_rtt_nom = TUNE_TRAINING_PARAMS_RTT_NOM_DDR4;
138 params.g_dic = TUNE_TRAINING_PARAMS_DIC_DDR4;
139 if (cs_num == 1) {
140 params.g_rtt_wr = TUNE_TRAINING_PARAMS_RTT_WR_1CS;
141 params.g_rtt_park = TUNE_TRAINING_PARAMS_RTT_PARK_1CS;
142 } else {
143 params.g_rtt_wr = TUNE_TRAINING_PARAMS_RTT_WR_2CS;
144 params.g_rtt_park = TUNE_TRAINING_PARAMS_RTT_PARK_2CS;
145 }
146#else /* CONFIG_DDR4 */
Chris Packham1a07d212018-05-10 13:28:29 +1200147 params.g_zpodt_data = TUNE_TRAINING_PARAMS_P_ODT_DATA;
148 params.g_dic = TUNE_TRAINING_PARAMS_DIC;
149 params.g_rtt_nom = TUNE_TRAINING_PARAMS_RTT_NOM;
150 if (cs_num == 1) {
151 params.g_rtt_wr = TUNE_TRAINING_PARAMS_RTT_WR_1CS;
152 params.g_odt_config = TUNE_TRAINING_PARAMS_ODT_CONFIG_1CS;
153 } else {
154 params.g_rtt_wr = TUNE_TRAINING_PARAMS_RTT_WR_2CS;
155 params.g_odt_config = TUNE_TRAINING_PARAMS_ODT_CONFIG_2CS;
156 }
Tony Dinhe2c524b2023-01-18 19:03:04 -0800157#endif /* CONFIG_DDR4 */
Stefan Roese5ffceb82015-03-26 15:36:56 +0100158
Chris Packhame422adc2020-01-30 12:50:44 +1300159 if (ck_delay > 0)
160 params.ck_delay = ck_delay;
161
Baruch Siach4951d422021-02-19 17:11:17 +0100162 /* Use platform specific override ODT value */
163 if (tm->odt_config)
164 params.g_odt_config = tm->odt_config;
165
Stefan Roese5ffceb82015-03-26 15:36:56 +0100166 status = ddr3_tip_tune_training_params(dev_num, &params);
167 if (MV_OK != status) {
168 printf("%s Training Sequence - FAILED\n", ddr_type);
169 return status;
170 }
171
172 return MV_OK;
173}