blob: f4150203ea982bbc14f667394c33d4731e74f5fb [file] [log] [blame]
Angel Pons1ddb8942020-04-04 18:51:26 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -08002
Stefan Reinauer08dc3572013-05-14 16:57:50 -07003/* DDR3 mem setup file for SMDK5250 board based on EXYNOS5 */
4
Kyösti Mälkki13f66502019-03-03 08:01:05 +02005#include <device/mmio.h>
Julius Wernerfa938c72013-08-29 14:17:36 -07006#include <console/console.h>
Julius Werner1ed0c8c2014-10-20 13:16:29 -07007#include <delay.h>
8#include <soc/clk.h>
9#include <soc/cpu.h>
10#include <soc/dmc.h>
11#include <soc/setup.h>
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080012
13#define RDLVL_COMPLETE_TIMEOUT 10000
14
15static void reset_phy_ctrl(void)
16{
Julius Werner2f37bd62015-02-19 14:51:15 -080017 write32(&exynos_clock->lpddr3phy_ctrl,
18 LPDDR3PHY_CTRL_PHY_RESET_ENABLE);
19 write32(&exynos_clock->lpddr3phy_ctrl,
20 LPDDR3PHY_CTRL_PHY_RESET_DISABLE);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080021
David Hendricks1fb11d12013-04-12 15:11:05 -070022 udelay(500);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080023}
24
Hung-Te Lin55c753d2013-04-25 16:14:19 +080025int ddr3_mem_ctrl_init(struct mem_timings *mem, unsigned long mem_iv_size,
26 int mem_reset)
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080027{
28 unsigned int val;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080029 int i;
30
Ronald G. Minnich6da70462013-06-26 09:51:21 -070031 if (mem_reset)
Hung-Te Lin55c753d2013-04-25 16:14:19 +080032 reset_phy_ctrl();
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080033
34 /* Set Impedance Output Driver */
35 val = (mem->impedance << CA_CK_DRVR_DS_OFFSET) |
36 (mem->impedance << CA_CKE_DRVR_DS_OFFSET) |
37 (mem->impedance << CA_CS_DRVR_DS_OFFSET) |
38 (mem->impedance << CA_ADR_DRVR_DS_OFFSET);
Julius Werner2f37bd62015-02-19 14:51:15 -080039 write32(&exynos_phy0_control->phy_con39, val);
40 write32(&exynos_phy1_control->phy_con39, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080041
42 /* Set Read Latency and Burst Length for PHY0 and PHY1 */
43 val = (mem->ctrl_bstlen << PHY_CON42_CTRL_BSTLEN_SHIFT) |
44 (mem->ctrl_rdlat << PHY_CON42_CTRL_RDLAT_SHIFT);
Julius Werner2f37bd62015-02-19 14:51:15 -080045 write32(&exynos_phy0_control->phy_con42, val);
46 write32(&exynos_phy1_control->phy_con42, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080047
48 /* ZQ Calibration */
Julius Wernerfa938c72013-08-29 14:17:36 -070049 if (dmc_config_zq(mem, exynos_phy0_control, exynos_phy1_control)){
Ronald G. Minnich6da70462013-06-26 09:51:21 -070050 printk(BIOS_EMERG, "DRAM ZQ CALIBRATION FAILURE\n");
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080051 return SETUP_ERR_ZQ_CALIBRATION_FAILURE;
Ronald G. Minnich6da70462013-06-26 09:51:21 -070052 }
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080053
54 /* DQ Signal */
Julius Werner2f37bd62015-02-19 14:51:15 -080055 write32(&exynos_phy0_control->phy_con14, mem->phy0_pulld_dqs);
56 write32(&exynos_phy1_control->phy_con14, mem->phy1_pulld_dqs);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080057
Julius Werner94184762015-02-19 20:19:23 -080058 write32(&exynos_dmc->concontrol, mem->concontrol |
59 (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT) |
60 (mem->dfi_init_start << CONCONTROL_DFI_INIT_START_SHIFT));
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080061
Julius Wernerfa938c72013-08-29 14:17:36 -070062 update_reset_dll(exynos_dmc, DDR_MODE_DDR3);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080063
64 /* DQS Signal */
Julius Werner2f37bd62015-02-19 14:51:15 -080065 write32(&exynos_phy0_control->phy_con4, mem->phy0_dqs);
66 write32(&exynos_phy1_control->phy_con4, mem->phy1_dqs);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080067
Julius Werner2f37bd62015-02-19 14:51:15 -080068 write32(&exynos_phy0_control->phy_con6, mem->phy0_dq);
69 write32(&exynos_phy1_control->phy_con6, mem->phy1_dq);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080070
Julius Werner2f37bd62015-02-19 14:51:15 -080071 write32(&exynos_phy0_control->phy_con10, mem->phy0_tFS);
72 write32(&exynos_phy1_control->phy_con10, mem->phy1_tFS);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080073
74 val = (mem->ctrl_start_point << PHY_CON12_CTRL_START_POINT_SHIFT) |
75 (mem->ctrl_inc << PHY_CON12_CTRL_INC_SHIFT) |
76 (mem->ctrl_dll_on << PHY_CON12_CTRL_DLL_ON_SHIFT) |
77 (mem->ctrl_ref << PHY_CON12_CTRL_REF_SHIFT);
Julius Werner2f37bd62015-02-19 14:51:15 -080078 write32(&exynos_phy0_control->phy_con12, val);
79 write32(&exynos_phy1_control->phy_con12, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080080
81 /* Start DLL locking */
Julius Werner2f37bd62015-02-19 14:51:15 -080082 write32(&exynos_phy0_control->phy_con12,
83 val | (mem->ctrl_start << PHY_CON12_CTRL_START_SHIFT));
84 write32(&exynos_phy1_control->phy_con12,
85 val | (mem->ctrl_start << PHY_CON12_CTRL_START_SHIFT));
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080086
Julius Wernerfa938c72013-08-29 14:17:36 -070087 update_reset_dll(exynos_dmc, DDR_MODE_DDR3);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080088
Julius Werner2f37bd62015-02-19 14:51:15 -080089 write32(&exynos_dmc->concontrol,
90 mem->concontrol | (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT));
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080091
Julius Wernerfa938c72013-08-29 14:17:36 -070092 /* Memory Channel Inteleaving Size */
Julius Werner2f37bd62015-02-19 14:51:15 -080093 write32(&exynos_dmc->ivcontrol, mem->iv_size);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080094
95 /* Set DMC MEMCONTROL register */
96 val = mem->memcontrol & ~DMC_MEMCONTROL_DSREF_ENABLE;
Julius Werner2f37bd62015-02-19 14:51:15 -080097 write32(&exynos_dmc->memcontrol, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080098
Julius Werner2f37bd62015-02-19 14:51:15 -080099 write32(&exynos_dmc->memconfig0, mem->memconfig);
100 write32(&exynos_dmc->memconfig1, mem->memconfig);
101 write32(&exynos_dmc->membaseconfig0, mem->membaseconfig0);
102 write32(&exynos_dmc->membaseconfig1, mem->membaseconfig1);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800103
104 /* Precharge Configuration */
Julius Werner2f37bd62015-02-19 14:51:15 -0800105 write32(&exynos_dmc->prechconfig,
106 mem->prechconfig_tp_cnt << PRECHCONFIG_TP_CNT_SHIFT);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800107
108 /* Power Down mode Configuration */
Julius Werner2f37bd62015-02-19 14:51:15 -0800109 write32(&exynos_dmc->pwrdnconfig,
Julius Werner94184762015-02-19 20:19:23 -0800110 mem->dpwrdn_cyc << PWRDNCONFIG_DPWRDN_CYC_SHIFT |
111 mem->dsref_cyc << PWRDNCONFIG_DSREF_CYC_SHIFT);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800112
113 /* TimingRow, TimingData, TimingPower and Timingaref
114 * values as per Memory AC parameters
115 */
Julius Werner2f37bd62015-02-19 14:51:15 -0800116 write32(&exynos_dmc->timingref, mem->timing_ref);
117 write32(&exynos_dmc->timingrow, mem->timing_row);
118 write32(&exynos_dmc->timingdata, mem->timing_data);
119 write32(&exynos_dmc->timingpower, mem->timing_power);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800120
121 /* Send PALL command */
Julius Wernerfa938c72013-08-29 14:17:36 -0700122 dmc_config_prech(mem, exynos_dmc);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800123
Hung-Te Linc0491d42013-08-06 10:48:48 +0800124 if (mem_reset) {
125 /* Send NOP, MRS and ZQINIT commands.
126 * Sending MRS command will reset the DRAM. We should not be
127 * reseting the DRAM after resume, this will lead to memory
128 * corruption as DRAM content is lost after DRAM reset
129 */
Julius Wernerfa938c72013-08-29 14:17:36 -0700130 dmc_config_mrs(mem, exynos_dmc);
Hung-Te Linc0491d42013-08-06 10:48:48 +0800131 }
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800132
133 if (mem->gate_leveling_enable) {
134 val = PHY_CON0_RESET_VAL;
135 val |= P0_CMD_EN;
Julius Werner2f37bd62015-02-19 14:51:15 -0800136 write32(&exynos_phy0_control->phy_con0, val);
137 write32(&exynos_phy1_control->phy_con0, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800138
139 val = PHY_CON2_RESET_VAL;
140 val |= INIT_DESKEW_EN;
Julius Werner2f37bd62015-02-19 14:51:15 -0800141 write32(&exynos_phy0_control->phy_con2, val);
142 write32(&exynos_phy1_control->phy_con2, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800143
144 val = PHY_CON0_RESET_VAL;
145 val |= P0_CMD_EN;
146 val |= BYTE_RDLVL_EN;
Julius Werner2f37bd62015-02-19 14:51:15 -0800147 write32(&exynos_phy0_control->phy_con0, val);
148 write32(&exynos_phy1_control->phy_con0, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800149
150 val = (mem->ctrl_start_point <<
151 PHY_CON12_CTRL_START_POINT_SHIFT) |
152 (mem->ctrl_inc << PHY_CON12_CTRL_INC_SHIFT) |
153 (mem->ctrl_force << PHY_CON12_CTRL_FORCE_SHIFT) |
154 (mem->ctrl_start << PHY_CON12_CTRL_START_SHIFT) |
155 (mem->ctrl_ref << PHY_CON12_CTRL_REF_SHIFT);
Julius Werner2f37bd62015-02-19 14:51:15 -0800156 write32(&exynos_phy0_control->phy_con12, val);
157 write32(&exynos_phy1_control->phy_con12, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800158
159 val = PHY_CON2_RESET_VAL;
160 val |= INIT_DESKEW_EN;
161 val |= RDLVL_GATE_EN;
Julius Werner2f37bd62015-02-19 14:51:15 -0800162 write32(&exynos_phy0_control->phy_con2, val);
163 write32(&exynos_phy1_control->phy_con2, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800164
165 val = PHY_CON0_RESET_VAL;
166 val |= P0_CMD_EN;
167 val |= BYTE_RDLVL_EN;
168 val |= CTRL_SHGATE;
Julius Werner2f37bd62015-02-19 14:51:15 -0800169 write32(&exynos_phy0_control->phy_con0, val);
170 write32(&exynos_phy1_control->phy_con0, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800171
172 val = PHY_CON1_RESET_VAL;
173 val &= ~(CTRL_GATEDURADJ_MASK);
Julius Werner2f37bd62015-02-19 14:51:15 -0800174 write32(&exynos_phy0_control->phy_con1, val);
175 write32(&exynos_phy1_control->phy_con1, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800176
Julius Werner2f37bd62015-02-19 14:51:15 -0800177 write32(&exynos_dmc->rdlvl_config, CTRL_RDLVL_GATE_ENABLE);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800178 i = RDLVL_COMPLETE_TIMEOUT;
Julius Werner2f37bd62015-02-19 14:51:15 -0800179 while ((read32(&exynos_dmc->phystatus) &
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800180 (RDLVL_COMPLETE_CHO | RDLVL_COMPLETE_CH1)) !=
181 (RDLVL_COMPLETE_CHO | RDLVL_COMPLETE_CH1) && i > 0) {
182 /*
183 * TODO(waihong): Comment on how long this take to
184 * timeout
185 */
David Hendricks1fb11d12013-04-12 15:11:05 -0700186 udelay(1);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800187 i--;
188 }
Ronald G. Minnich88e46912013-01-29 14:16:35 -0800189 if (!i){
Ronald G. Minnich6da70462013-06-26 09:51:21 -0700190 printk(BIOS_EMERG, "Timeout on RDLVL. No DRAM.\n");
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800191 return SETUP_ERR_RDLV_COMPLETE_TIMEOUT;
Ronald G. Minnich88e46912013-01-29 14:16:35 -0800192 }
Julius Werner2f37bd62015-02-19 14:51:15 -0800193 write32(&exynos_dmc->rdlvl_config, CTRL_RDLVL_GATE_DISABLE);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800194
Julius Werner2f37bd62015-02-19 14:51:15 -0800195 write32(&exynos_phy0_control->phy_con14, 0);
196 write32(&exynos_phy1_control->phy_con14, 0);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800197
198 val = (mem->ctrl_start_point <<
199 PHY_CON12_CTRL_START_POINT_SHIFT) |
200 (mem->ctrl_inc << PHY_CON12_CTRL_INC_SHIFT) |
201 (mem->ctrl_force << PHY_CON12_CTRL_FORCE_SHIFT) |
202 (mem->ctrl_start << PHY_CON12_CTRL_START_SHIFT) |
203 (mem->ctrl_dll_on << PHY_CON12_CTRL_DLL_ON_SHIFT) |
204 (mem->ctrl_ref << PHY_CON12_CTRL_REF_SHIFT);
Julius Werner2f37bd62015-02-19 14:51:15 -0800205 write32(&exynos_phy0_control->phy_con12, val);
206 write32(&exynos_phy1_control->phy_con12, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800207
Julius Wernerfa938c72013-08-29 14:17:36 -0700208 update_reset_dll(exynos_dmc, DDR_MODE_DDR3);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800209 }
210
211 /* Send PALL command */
Julius Wernerfa938c72013-08-29 14:17:36 -0700212 dmc_config_prech(mem, exynos_dmc);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800213
Julius Werner2f37bd62015-02-19 14:51:15 -0800214 write32(&exynos_dmc->memcontrol, mem->memcontrol);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800215
216 /* Set DMC Concontrol and enable auto-refresh counter */
Julius Werner94184762015-02-19 20:19:23 -0800217 write32(&exynos_dmc->concontrol, mem->concontrol |
218 (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT) |
219 (mem->aref_en << CONCONTROL_AREF_EN_SHIFT));
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800220 return 0;
221}