blob: 5d8e33d0119742862bed6b80c18cf7f5e75914d4 [file] [log] [blame]
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -08001/*
Stefan Reinauer08dc3572013-05-14 16:57:50 -07002 * This file is part of the coreboot project.
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -08003 *
4 * Copyright (C) 2012 Samsung Electronics
5 *
Stefan Reinauer08dc3572013-05-14 16:57:50 -07006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -08009 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080014 */
15
Stefan Reinauer08dc3572013-05-14 16:57:50 -070016/* DDR3 mem setup file for SMDK5250 board based on EXYNOS5 */
17
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080018#include <arch/io.h>
Julius Wernerfa938c72013-08-29 14:17:36 -070019#include <console/console.h>
Julius Werner1ed0c8c2014-10-20 13:16:29 -070020#include <delay.h>
21#include <soc/clk.h>
22#include <soc/cpu.h>
23#include <soc/dmc.h>
24#include <soc/setup.h>
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080025
26#define RDLVL_COMPLETE_TIMEOUT 10000
27
28static void reset_phy_ctrl(void)
29{
Julius Werner2f37bd62015-02-19 14:51:15 -080030 write32(&exynos_clock->lpddr3phy_ctrl,
31 LPDDR3PHY_CTRL_PHY_RESET_ENABLE);
32 write32(&exynos_clock->lpddr3phy_ctrl,
33 LPDDR3PHY_CTRL_PHY_RESET_DISABLE);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080034
David Hendricks1fb11d12013-04-12 15:11:05 -070035#if 0
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080036 /*
37 * For proper memory initialization there should be a minimum delay of
38 * 500us after the LPDDR3PHY_CTRL_PHY_RESET signal.
39 * The below value is an approximate value whose calculation in done
40 * considering that sdelay takes 2 instruction for every 1 delay cycle.
41 * And assuming each instruction takes 1 clock cycle i.e 1/(1.7 Ghz)sec
42 * So for 500 usec, the number of delay cycle should be
43 * (500 * 10^-6) * (1.7 * 10^9) / 2 = 425000
44 *
45 * TODO(hatim.rv@samsung.com): Implement the delay using timer/counter
46 */
47 sdelay(425000);
David Hendricks1fb11d12013-04-12 15:11:05 -070048#endif
49 udelay(500);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080050}
51
Hung-Te Lin55c753d2013-04-25 16:14:19 +080052int ddr3_mem_ctrl_init(struct mem_timings *mem, unsigned long mem_iv_size,
53 int mem_reset)
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080054{
55 unsigned int val;
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080056 int i;
57
Ronald G. Minnich6da70462013-06-26 09:51:21 -070058 if (mem_reset)
Hung-Te Lin55c753d2013-04-25 16:14:19 +080059 reset_phy_ctrl();
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080060
61 /* Set Impedance Output Driver */
62 val = (mem->impedance << CA_CK_DRVR_DS_OFFSET) |
63 (mem->impedance << CA_CKE_DRVR_DS_OFFSET) |
64 (mem->impedance << CA_CS_DRVR_DS_OFFSET) |
65 (mem->impedance << CA_ADR_DRVR_DS_OFFSET);
Julius Werner2f37bd62015-02-19 14:51:15 -080066 write32(&exynos_phy0_control->phy_con39, val);
67 write32(&exynos_phy1_control->phy_con39, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080068
69 /* Set Read Latency and Burst Length for PHY0 and PHY1 */
70 val = (mem->ctrl_bstlen << PHY_CON42_CTRL_BSTLEN_SHIFT) |
71 (mem->ctrl_rdlat << PHY_CON42_CTRL_RDLAT_SHIFT);
Julius Werner2f37bd62015-02-19 14:51:15 -080072 write32(&exynos_phy0_control->phy_con42, val);
73 write32(&exynos_phy1_control->phy_con42, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080074
75 /* ZQ Calibration */
Julius Wernerfa938c72013-08-29 14:17:36 -070076 if (dmc_config_zq(mem, exynos_phy0_control, exynos_phy1_control)){
Ronald G. Minnich6da70462013-06-26 09:51:21 -070077 printk(BIOS_EMERG, "DRAM ZQ CALIBRATION FAILURE\n");
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080078 return SETUP_ERR_ZQ_CALIBRATION_FAILURE;
Ronald G. Minnich6da70462013-06-26 09:51:21 -070079 }
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080080
81 /* DQ Signal */
Julius Werner2f37bd62015-02-19 14:51:15 -080082 write32(&exynos_phy0_control->phy_con14, mem->phy0_pulld_dqs);
83 write32(&exynos_phy1_control->phy_con14, mem->phy1_pulld_dqs);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080084
Julius Werner94184762015-02-19 20:19:23 -080085 write32(&exynos_dmc->concontrol, mem->concontrol |
86 (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT) |
87 (mem->dfi_init_start << CONCONTROL_DFI_INIT_START_SHIFT));
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080088
Julius Wernerfa938c72013-08-29 14:17:36 -070089 update_reset_dll(exynos_dmc, DDR_MODE_DDR3);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080090
91 /* DQS Signal */
Julius Werner2f37bd62015-02-19 14:51:15 -080092 write32(&exynos_phy0_control->phy_con4, mem->phy0_dqs);
93 write32(&exynos_phy1_control->phy_con4, mem->phy1_dqs);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080094
Julius Werner2f37bd62015-02-19 14:51:15 -080095 write32(&exynos_phy0_control->phy_con6, mem->phy0_dq);
96 write32(&exynos_phy1_control->phy_con6, mem->phy1_dq);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -080097
Julius Werner2f37bd62015-02-19 14:51:15 -080098 write32(&exynos_phy0_control->phy_con10, mem->phy0_tFS);
99 write32(&exynos_phy1_control->phy_con10, mem->phy1_tFS);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800100
101 val = (mem->ctrl_start_point << PHY_CON12_CTRL_START_POINT_SHIFT) |
102 (mem->ctrl_inc << PHY_CON12_CTRL_INC_SHIFT) |
103 (mem->ctrl_dll_on << PHY_CON12_CTRL_DLL_ON_SHIFT) |
104 (mem->ctrl_ref << PHY_CON12_CTRL_REF_SHIFT);
Julius Werner2f37bd62015-02-19 14:51:15 -0800105 write32(&exynos_phy0_control->phy_con12, val);
106 write32(&exynos_phy1_control->phy_con12, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800107
108 /* Start DLL locking */
Julius Werner2f37bd62015-02-19 14:51:15 -0800109 write32(&exynos_phy0_control->phy_con12,
110 val | (mem->ctrl_start << PHY_CON12_CTRL_START_SHIFT));
111 write32(&exynos_phy1_control->phy_con12,
112 val | (mem->ctrl_start << PHY_CON12_CTRL_START_SHIFT));
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800113
Julius Wernerfa938c72013-08-29 14:17:36 -0700114 update_reset_dll(exynos_dmc, DDR_MODE_DDR3);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800115
Julius Werner2f37bd62015-02-19 14:51:15 -0800116 write32(&exynos_dmc->concontrol,
117 mem->concontrol | (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT));
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800118
Julius Wernerfa938c72013-08-29 14:17:36 -0700119 /* Memory Channel Inteleaving Size */
Julius Werner2f37bd62015-02-19 14:51:15 -0800120 write32(&exynos_dmc->ivcontrol, mem->iv_size);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800121
122 /* Set DMC MEMCONTROL register */
123 val = mem->memcontrol & ~DMC_MEMCONTROL_DSREF_ENABLE;
Julius Werner2f37bd62015-02-19 14:51:15 -0800124 write32(&exynos_dmc->memcontrol, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800125
Julius Werner2f37bd62015-02-19 14:51:15 -0800126 write32(&exynos_dmc->memconfig0, mem->memconfig);
127 write32(&exynos_dmc->memconfig1, mem->memconfig);
128 write32(&exynos_dmc->membaseconfig0, mem->membaseconfig0);
129 write32(&exynos_dmc->membaseconfig1, mem->membaseconfig1);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800130
131 /* Precharge Configuration */
Julius Werner2f37bd62015-02-19 14:51:15 -0800132 write32(&exynos_dmc->prechconfig,
133 mem->prechconfig_tp_cnt << PRECHCONFIG_TP_CNT_SHIFT);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800134
135 /* Power Down mode Configuration */
Julius Werner2f37bd62015-02-19 14:51:15 -0800136 write32(&exynos_dmc->pwrdnconfig,
Julius Werner94184762015-02-19 20:19:23 -0800137 mem->dpwrdn_cyc << PWRDNCONFIG_DPWRDN_CYC_SHIFT |
138 mem->dsref_cyc << PWRDNCONFIG_DSREF_CYC_SHIFT);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800139
140 /* TimingRow, TimingData, TimingPower and Timingaref
141 * values as per Memory AC parameters
142 */
Julius Werner2f37bd62015-02-19 14:51:15 -0800143 write32(&exynos_dmc->timingref, mem->timing_ref);
144 write32(&exynos_dmc->timingrow, mem->timing_row);
145 write32(&exynos_dmc->timingdata, mem->timing_data);
146 write32(&exynos_dmc->timingpower, mem->timing_power);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800147
148 /* Send PALL command */
Julius Wernerfa938c72013-08-29 14:17:36 -0700149 dmc_config_prech(mem, exynos_dmc);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800150
Hung-Te Linc0491d42013-08-06 10:48:48 +0800151 if (mem_reset) {
152 /* Send NOP, MRS and ZQINIT commands.
153 * Sending MRS command will reset the DRAM. We should not be
154 * reseting the DRAM after resume, this will lead to memory
155 * corruption as DRAM content is lost after DRAM reset
156 */
Julius Wernerfa938c72013-08-29 14:17:36 -0700157 dmc_config_mrs(mem, exynos_dmc);
Hung-Te Linc0491d42013-08-06 10:48:48 +0800158 }
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800159
160 if (mem->gate_leveling_enable) {
161 val = PHY_CON0_RESET_VAL;
162 val |= P0_CMD_EN;
Julius Werner2f37bd62015-02-19 14:51:15 -0800163 write32(&exynos_phy0_control->phy_con0, val);
164 write32(&exynos_phy1_control->phy_con0, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800165
166 val = PHY_CON2_RESET_VAL;
167 val |= INIT_DESKEW_EN;
Julius Werner2f37bd62015-02-19 14:51:15 -0800168 write32(&exynos_phy0_control->phy_con2, val);
169 write32(&exynos_phy1_control->phy_con2, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800170
171 val = PHY_CON0_RESET_VAL;
172 val |= P0_CMD_EN;
173 val |= BYTE_RDLVL_EN;
Julius Werner2f37bd62015-02-19 14:51:15 -0800174 write32(&exynos_phy0_control->phy_con0, val);
175 write32(&exynos_phy1_control->phy_con0, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800176
177 val = (mem->ctrl_start_point <<
178 PHY_CON12_CTRL_START_POINT_SHIFT) |
179 (mem->ctrl_inc << PHY_CON12_CTRL_INC_SHIFT) |
180 (mem->ctrl_force << PHY_CON12_CTRL_FORCE_SHIFT) |
181 (mem->ctrl_start << PHY_CON12_CTRL_START_SHIFT) |
182 (mem->ctrl_ref << PHY_CON12_CTRL_REF_SHIFT);
Julius Werner2f37bd62015-02-19 14:51:15 -0800183 write32(&exynos_phy0_control->phy_con12, val);
184 write32(&exynos_phy1_control->phy_con12, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800185
186 val = PHY_CON2_RESET_VAL;
187 val |= INIT_DESKEW_EN;
188 val |= RDLVL_GATE_EN;
Julius Werner2f37bd62015-02-19 14:51:15 -0800189 write32(&exynos_phy0_control->phy_con2, val);
190 write32(&exynos_phy1_control->phy_con2, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800191
192 val = PHY_CON0_RESET_VAL;
193 val |= P0_CMD_EN;
194 val |= BYTE_RDLVL_EN;
195 val |= CTRL_SHGATE;
Julius Werner2f37bd62015-02-19 14:51:15 -0800196 write32(&exynos_phy0_control->phy_con0, val);
197 write32(&exynos_phy1_control->phy_con0, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800198
199 val = PHY_CON1_RESET_VAL;
200 val &= ~(CTRL_GATEDURADJ_MASK);
Julius Werner2f37bd62015-02-19 14:51:15 -0800201 write32(&exynos_phy0_control->phy_con1, val);
202 write32(&exynos_phy1_control->phy_con1, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800203
Julius Werner2f37bd62015-02-19 14:51:15 -0800204 write32(&exynos_dmc->rdlvl_config, CTRL_RDLVL_GATE_ENABLE);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800205 i = RDLVL_COMPLETE_TIMEOUT;
Julius Werner2f37bd62015-02-19 14:51:15 -0800206 while ((read32(&exynos_dmc->phystatus) &
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800207 (RDLVL_COMPLETE_CHO | RDLVL_COMPLETE_CH1)) !=
208 (RDLVL_COMPLETE_CHO | RDLVL_COMPLETE_CH1) && i > 0) {
209 /*
210 * TODO(waihong): Comment on how long this take to
211 * timeout
212 */
David Hendricks1fb11d12013-04-12 15:11:05 -0700213 udelay(1);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800214 i--;
215 }
Ronald G. Minnich88e46912013-01-29 14:16:35 -0800216 if (!i){
Ronald G. Minnich6da70462013-06-26 09:51:21 -0700217 printk(BIOS_EMERG, "Timeout on RDLVL. No DRAM.\n");
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800218 return SETUP_ERR_RDLV_COMPLETE_TIMEOUT;
Ronald G. Minnich88e46912013-01-29 14:16:35 -0800219 }
Julius Werner2f37bd62015-02-19 14:51:15 -0800220 write32(&exynos_dmc->rdlvl_config, CTRL_RDLVL_GATE_DISABLE);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800221
Julius Werner2f37bd62015-02-19 14:51:15 -0800222 write32(&exynos_phy0_control->phy_con14, 0);
223 write32(&exynos_phy1_control->phy_con14, 0);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800224
225 val = (mem->ctrl_start_point <<
226 PHY_CON12_CTRL_START_POINT_SHIFT) |
227 (mem->ctrl_inc << PHY_CON12_CTRL_INC_SHIFT) |
228 (mem->ctrl_force << PHY_CON12_CTRL_FORCE_SHIFT) |
229 (mem->ctrl_start << PHY_CON12_CTRL_START_SHIFT) |
230 (mem->ctrl_dll_on << PHY_CON12_CTRL_DLL_ON_SHIFT) |
231 (mem->ctrl_ref << PHY_CON12_CTRL_REF_SHIFT);
Julius Werner2f37bd62015-02-19 14:51:15 -0800232 write32(&exynos_phy0_control->phy_con12, val);
233 write32(&exynos_phy1_control->phy_con12, val);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800234
Julius Wernerfa938c72013-08-29 14:17:36 -0700235 update_reset_dll(exynos_dmc, DDR_MODE_DDR3);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800236 }
237
238 /* Send PALL command */
Julius Wernerfa938c72013-08-29 14:17:36 -0700239 dmc_config_prech(mem, exynos_dmc);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800240
Julius Werner2f37bd62015-02-19 14:51:15 -0800241 write32(&exynos_dmc->memcontrol, mem->memcontrol);
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800242
243 /* Set DMC Concontrol and enable auto-refresh counter */
Julius Werner94184762015-02-19 20:19:23 -0800244 write32(&exynos_dmc->concontrol, mem->concontrol |
245 (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT) |
246 (mem->aref_en << CONCONTROL_AREF_EN_SHIFT));
Stefan Reinauer9fe20cb2012-12-07 17:18:43 -0800247 return 0;
248}