blob: b28b2c73a8bd6b9e2f372c9fbc2bf88383278b2b [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
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
Chris Packham1a07d212018-05-10 13:28:29 +120091 /* Post MC/PHY initializations */
92 mv_ddr_post_training_soc_config(ddr_type);
Stefan Roese5ffceb82015-03-26 15:36:56 +010093
Chris Packham1a07d212018-05-10 13:28:29 +120094 mv_ddr_post_training_fixup();
Stefan Roese5ffceb82015-03-26 15:36:56 +010095
Chris Packham4bf81db2018-12-03 14:26:49 +130096 if (mv_ddr_is_ecc_ena())
97 mv_ddr_mem_scrubbing();
Stefan Roese5ffceb82015-03-26 15:36:56 +010098
Chris Packham1a07d212018-05-10 13:28:29 +120099 printf("mv_ddr: completed successfully\n");
Stefan Roese5ffceb82015-03-26 15:36:56 +0100100
101 return MV_OK;
102}
103
Stefan Roese5ffceb82015-03-26 15:36:56 +0100104/*
Chris Packham1a07d212018-05-10 13:28:29 +1200105 * Name: mv_ddr_training_params_set
Stefan Roese5ffceb82015-03-26 15:36:56 +0100106 * Desc:
107 * Args:
Chris Packham1a07d212018-05-10 13:28:29 +1200108 * Notes: sets internal training params
Stefan Roese5ffceb82015-03-26 15:36:56 +0100109 * Returns:
110 */
Chris Packham1a07d212018-05-10 13:28:29 +1200111static int mv_ddr_training_params_set(u8 dev_num)
Stefan Roese5ffceb82015-03-26 15:36:56 +0100112{
113 struct tune_train_params params;
Baruch Siach4951d422021-02-19 17:11:17 +0100114 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +0100115 int status;
Chris Packham1a07d212018-05-10 13:28:29 +1200116 u32 cs_num;
Chris Packhame422adc2020-01-30 12:50:44 +1300117 int ck_delay;
Chris Packham1a07d212018-05-10 13:28:29 +1200118
Chris Packham4bf81db2018-12-03 14:26:49 +1300119 cs_num = mv_ddr_cs_num_get();
Chris Packhame422adc2020-01-30 12:50:44 +1300120 ck_delay = mv_ddr_ck_delay_get();
Stefan Roese5ffceb82015-03-26 15:36:56 +0100121
122 /* NOTE: do not remove any field initilization */
123 params.ck_delay = TUNE_TRAINING_PARAMS_CK_DELAY;
Stefan Roese5ffceb82015-03-26 15:36:56 +0100124 params.phy_reg3_val = TUNE_TRAINING_PARAMS_PHYREG3VAL;
Chris Packham1a07d212018-05-10 13:28:29 +1200125 params.g_zpri_data = TUNE_TRAINING_PARAMS_PRI_DATA;
126 params.g_znri_data = TUNE_TRAINING_PARAMS_NRI_DATA;
127 params.g_zpri_ctrl = TUNE_TRAINING_PARAMS_PRI_CTRL;
128 params.g_znri_ctrl = TUNE_TRAINING_PARAMS_NRI_CTRL;
129 params.g_znodt_data = TUNE_TRAINING_PARAMS_N_ODT_DATA;
130 params.g_zpodt_ctrl = TUNE_TRAINING_PARAMS_P_ODT_CTRL;
131 params.g_znodt_ctrl = TUNE_TRAINING_PARAMS_N_ODT_CTRL;
132
Tony Dinhe2c524b2023-01-18 19:03:04 -0800133#if defined(CONFIG_DDR4)
134 params.g_zpodt_data = TUNE_TRAINING_PARAMS_P_ODT_DATA_DDR4;
135 params.g_odt_config = TUNE_TRAINING_PARAMS_ODT_CONFIG_DDR4;
136 params.g_rtt_nom = TUNE_TRAINING_PARAMS_RTT_NOM_DDR4;
137 params.g_dic = TUNE_TRAINING_PARAMS_DIC_DDR4;
138 if (cs_num == 1) {
139 params.g_rtt_wr = TUNE_TRAINING_PARAMS_RTT_WR_1CS;
140 params.g_rtt_park = TUNE_TRAINING_PARAMS_RTT_PARK_1CS;
141 } else {
142 params.g_rtt_wr = TUNE_TRAINING_PARAMS_RTT_WR_2CS;
143 params.g_rtt_park = TUNE_TRAINING_PARAMS_RTT_PARK_2CS;
144 }
145#else /* CONFIG_DDR4 */
Chris Packham1a07d212018-05-10 13:28:29 +1200146 params.g_zpodt_data = TUNE_TRAINING_PARAMS_P_ODT_DATA;
147 params.g_dic = TUNE_TRAINING_PARAMS_DIC;
148 params.g_rtt_nom = TUNE_TRAINING_PARAMS_RTT_NOM;
149 if (cs_num == 1) {
150 params.g_rtt_wr = TUNE_TRAINING_PARAMS_RTT_WR_1CS;
151 params.g_odt_config = TUNE_TRAINING_PARAMS_ODT_CONFIG_1CS;
152 } else {
153 params.g_rtt_wr = TUNE_TRAINING_PARAMS_RTT_WR_2CS;
154 params.g_odt_config = TUNE_TRAINING_PARAMS_ODT_CONFIG_2CS;
155 }
Tony Dinhe2c524b2023-01-18 19:03:04 -0800156#endif /* CONFIG_DDR4 */
Stefan Roese5ffceb82015-03-26 15:36:56 +0100157
Chris Packhame422adc2020-01-30 12:50:44 +1300158 if (ck_delay > 0)
159 params.ck_delay = ck_delay;
160
Baruch Siach4951d422021-02-19 17:11:17 +0100161 /* Use platform specific override ODT value */
162 if (tm->odt_config)
163 params.g_odt_config = tm->odt_config;
164
Stefan Roese5ffceb82015-03-26 15:36:56 +0100165 status = ddr3_tip_tune_training_params(dev_num, &params);
166 if (MV_OK != status) {
167 printf("%s Training Sequence - FAILED\n", ddr_type);
168 return status;
169 }
170
171 return MV_OK;
172}