blob: b0e1caa356b6982924b11aad371a7f420ea57d75 [file] [log] [blame]
Aneesh Vcc565582011-07-21 09:10:09 -04001/*
2 * EMIF programming
3 *
4 * (C) Copyright 2010
5 * Texas Instruments, <www.ti.com>
6 *
7 * Aneesh V <aneesh@ti.com>
8 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Aneesh Vcc565582011-07-21 09:10:09 -040010 */
11
12#include <common.h>
Sricharan62a86502011-11-15 09:50:00 -050013#include <asm/emif.h>
Lokesh Vutla61c517f2013-05-30 02:54:32 +000014#include <asm/arch/clock.h>
Aneesh Vcc565582011-07-21 09:10:09 -040015#include <asm/arch/sys_proto.h>
16#include <asm/omap_common.h>
17#include <asm/utils.h>
SRICHARAN Rb9f10a52012-06-04 03:40:23 +000018#include <linux/compiler.h>
Aneesh Vcc565582011-07-21 09:10:09 -040019
Lokesh Vutla80242592012-11-15 21:06:33 +000020static int emif1_enabled = -1, emif2_enabled = -1;
21
Lokesh Vutlaba873772012-05-29 19:26:43 +000022void set_lpmode_selfrefresh(u32 base)
23{
24 struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
25 u32 reg;
26
27 reg = readl(&emif->emif_pwr_mgmt_ctrl);
28 reg &= ~EMIF_REG_LP_MODE_MASK;
29 reg |= LP_MODE_SELF_REFRESH << EMIF_REG_LP_MODE_SHIFT;
30 reg &= ~EMIF_REG_SR_TIM_MASK;
31 writel(reg, &emif->emif_pwr_mgmt_ctrl);
32
33 /* dummy read for the new SR_TIM to be loaded */
34 readl(&emif->emif_pwr_mgmt_ctrl);
35}
36
37void force_emif_self_refresh()
38{
39 set_lpmode_selfrefresh(EMIF1_BASE);
40 set_lpmode_selfrefresh(EMIF2_BASE);
41}
42
Sricharan62a86502011-11-15 09:50:00 -050043inline u32 emif_num(u32 base)
Aneesh Vcc565582011-07-21 09:10:09 -040044{
Sricharan62a86502011-11-15 09:50:00 -050045 if (base == EMIF1_BASE)
Aneesh Vcc565582011-07-21 09:10:09 -040046 return 1;
Sricharan62a86502011-11-15 09:50:00 -050047 else if (base == EMIF2_BASE)
Aneesh Vcc565582011-07-21 09:10:09 -040048 return 2;
49 else
50 return 0;
51}
52
Lokesh Vutlafef54c32013-02-04 04:21:59 +000053/*
54 * Get SDRAM type connected to EMIF.
55 * Assuming similar SDRAM parts are connected to both EMIF's
56 * which is typically the case. So it is sufficient to get
57 * SDRAM type from EMIF1.
58 */
59u32 emif_sdram_type()
60{
61 struct emif_reg_struct *emif = (struct emif_reg_struct *)EMIF1_BASE;
62
63 return (readl(&emif->emif_sdram_config) &
64 EMIF_REG_SDRAM_TYPE_MASK) >> EMIF_REG_SDRAM_TYPE_SHIFT;
65}
Sricharan62a86502011-11-15 09:50:00 -050066
Aneesh Vcc565582011-07-21 09:10:09 -040067static inline u32 get_mr(u32 base, u32 cs, u32 mr_addr)
68{
69 u32 mr;
70 struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
71
Sricharan62a86502011-11-15 09:50:00 -050072 mr_addr |= cs << EMIF_REG_CS_SHIFT;
Aneesh Vcc565582011-07-21 09:10:09 -040073 writel(mr_addr, &emif->emif_lpddr2_mode_reg_cfg);
74 if (omap_revision() == OMAP4430_ES2_0)
75 mr = readl(&emif->emif_lpddr2_mode_reg_data_es2);
76 else
77 mr = readl(&emif->emif_lpddr2_mode_reg_data);
78 debug("get_mr: EMIF%d cs %d mr %08x val 0x%x\n", emif_num(base),
79 cs, mr_addr, mr);
Steve Sakoman3dab3f62012-05-30 07:38:07 +000080 if (((mr & 0x0000ff00) >> 8) == (mr & 0xff) &&
81 ((mr & 0x00ff0000) >> 16) == (mr & 0xff) &&
82 ((mr & 0xff000000) >> 24) == (mr & 0xff))
83 return mr & 0xff;
84 else
85 return mr;
Aneesh Vcc565582011-07-21 09:10:09 -040086}
87
88static inline void set_mr(u32 base, u32 cs, u32 mr_addr, u32 mr_val)
89{
90 struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
91
Sricharan62a86502011-11-15 09:50:00 -050092 mr_addr |= cs << EMIF_REG_CS_SHIFT;
Aneesh Vcc565582011-07-21 09:10:09 -040093 writel(mr_addr, &emif->emif_lpddr2_mode_reg_cfg);
94 writel(mr_val, &emif->emif_lpddr2_mode_reg_data);
95}
96
97void emif_reset_phy(u32 base)
98{
99 struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
100 u32 iodft;
101
102 iodft = readl(&emif->emif_iodft_tlgc);
Sricharan62a86502011-11-15 09:50:00 -0500103 iodft |= EMIF_REG_RESET_PHY_MASK;
Aneesh Vcc565582011-07-21 09:10:09 -0400104 writel(iodft, &emif->emif_iodft_tlgc);
105}
106
107static void do_lpddr2_init(u32 base, u32 cs)
108{
109 u32 mr_addr;
Lokesh Vutla05dab552013-02-04 04:22:03 +0000110 const struct lpddr2_mr_regs *mr_regs;
Aneesh Vcc565582011-07-21 09:10:09 -0400111
Lokesh Vutla05dab552013-02-04 04:22:03 +0000112 get_lpddr2_mr_regs(&mr_regs);
Aneesh Vcc565582011-07-21 09:10:09 -0400113 /* Wait till device auto initialization is complete */
114 while (get_mr(base, cs, LPDDR2_MR0) & LPDDR2_MR0_DAI_MASK)
115 ;
Lokesh Vutla05dab552013-02-04 04:22:03 +0000116 set_mr(base, cs, LPDDR2_MR10, mr_regs->mr10);
Aneesh Vcc565582011-07-21 09:10:09 -0400117 /*
118 * tZQINIT = 1 us
119 * Enough loops assuming a maximum of 2GHz
120 */
SRICHARAN R3d534962012-03-12 02:25:37 +0000121
Aneesh Vcc565582011-07-21 09:10:09 -0400122 sdelay(2000);
SRICHARAN R3d534962012-03-12 02:25:37 +0000123
Lokesh Vutla05dab552013-02-04 04:22:03 +0000124 set_mr(base, cs, LPDDR2_MR1, mr_regs->mr1);
125 set_mr(base, cs, LPDDR2_MR16, mr_regs->mr16);
SRICHARAN R3d534962012-03-12 02:25:37 +0000126
Aneesh Vcc565582011-07-21 09:10:09 -0400127 /*
128 * Enable refresh along with writing MR2
129 * Encoding of RL in MR2 is (RL - 2)
130 */
Sricharan62a86502011-11-15 09:50:00 -0500131 mr_addr = LPDDR2_MR2 | EMIF_REG_REFRESH_EN_MASK;
Lokesh Vutla05dab552013-02-04 04:22:03 +0000132 set_mr(base, cs, mr_addr, mr_regs->mr2);
SRICHARAN R3d534962012-03-12 02:25:37 +0000133
Lokesh Vutla05dab552013-02-04 04:22:03 +0000134 if (mr_regs->mr3 > 0)
135 set_mr(base, cs, LPDDR2_MR3, mr_regs->mr3);
Aneesh Vcc565582011-07-21 09:10:09 -0400136}
137
138static void lpddr2_init(u32 base, const struct emif_regs *regs)
139{
140 struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
141
142 /* Not NVM */
Sricharan62a86502011-11-15 09:50:00 -0500143 clrbits_le32(&emif->emif_lpddr2_nvm_config, EMIF_REG_CS1NVMEN_MASK);
Aneesh Vcc565582011-07-21 09:10:09 -0400144
145 /*
146 * Keep REG_INITREF_DIS = 1 to prevent re-initialization of SDRAM
147 * when EMIF_SDRAM_CONFIG register is written
148 */
Sricharan62a86502011-11-15 09:50:00 -0500149 setbits_le32(&emif->emif_sdram_ref_ctrl, EMIF_REG_INITREF_DIS_MASK);
Aneesh Vcc565582011-07-21 09:10:09 -0400150
151 /*
152 * Set the SDRAM_CONFIG and PHY_CTRL for the
153 * un-locked frequency & default RL
154 */
155 writel(regs->sdram_config_init, &emif->emif_sdram_config);
Taras Kondratiuk50535eb2013-08-06 16:16:50 +0300156 writel(regs->emif_ddr_phy_ctlr_1_init, &emif->emif_ddr_phy_ctrl_1);
SRICHARAN R3d534962012-03-12 02:25:37 +0000157
SRICHARAN Rb9f10a52012-06-04 03:40:23 +0000158 do_ext_phy_settings(base, regs);
Aneesh Vcc565582011-07-21 09:10:09 -0400159
160 do_lpddr2_init(base, CS0);
Sricharan62a86502011-11-15 09:50:00 -0500161 if (regs->sdram_config & EMIF_REG_EBANK_MASK)
Aneesh Vcc565582011-07-21 09:10:09 -0400162 do_lpddr2_init(base, CS1);
163
164 writel(regs->sdram_config, &emif->emif_sdram_config);
165 writel(regs->emif_ddr_phy_ctlr_1, &emif->emif_ddr_phy_ctrl_1);
166
167 /* Enable refresh now */
Sricharan62a86502011-11-15 09:50:00 -0500168 clrbits_le32(&emif->emif_sdram_ref_ctrl, EMIF_REG_INITREF_DIS_MASK);
Aneesh Vcc565582011-07-21 09:10:09 -0400169
SRICHARAN Rb9f10a52012-06-04 03:40:23 +0000170 }
171
172__weak void do_ext_phy_settings(u32 base, const struct emif_regs *regs)
173{
Aneesh Vcc565582011-07-21 09:10:09 -0400174}
175
Sricharan62a86502011-11-15 09:50:00 -0500176void emif_update_timings(u32 base, const struct emif_regs *regs)
Aneesh Vcc565582011-07-21 09:10:09 -0400177{
178 struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
179
180 writel(regs->ref_ctrl, &emif->emif_sdram_ref_ctrl_shdw);
181 writel(regs->sdram_tim1, &emif->emif_sdram_tim_1_shdw);
182 writel(regs->sdram_tim2, &emif->emif_sdram_tim_2_shdw);
183 writel(regs->sdram_tim3, &emif->emif_sdram_tim_3_shdw);
184 if (omap_revision() == OMAP4430_ES1_0) {
185 /* ES1 bug EMIF should be in force idle during freq_update */
186 writel(0, &emif->emif_pwr_mgmt_ctrl);
187 } else {
188 writel(EMIF_PWR_MGMT_CTRL, &emif->emif_pwr_mgmt_ctrl);
189 writel(EMIF_PWR_MGMT_CTRL_SHDW, &emif->emif_pwr_mgmt_ctrl_shdw);
190 }
191 writel(regs->read_idle_ctrl, &emif->emif_read_idlectrl_shdw);
192 writel(regs->zq_config, &emif->emif_zq_config);
193 writel(regs->temp_alert_config, &emif->emif_temp_alert_config);
194 writel(regs->emif_ddr_phy_ctlr_1, &emif->emif_ddr_phy_ctrl_1_shdw);
Aneesh V639cfb62011-07-21 09:29:26 -0400195
Sricharan Rffa98182013-05-30 03:19:39 +0000196 if ((omap_revision() >= OMAP5430_ES1_0) ||
197 (omap_revision() == DRA752_ES1_0)) {
Sricharan62a86502011-11-15 09:50:00 -0500198 writel(EMIF_L3_CONFIG_VAL_SYS_10_MPU_5_LL_0,
199 &emif->emif_l3_config);
200 } else if (omap_revision() >= OMAP4460_ES1_0) {
Aneesh V639cfb62011-07-21 09:29:26 -0400201 writel(EMIF_L3_CONFIG_VAL_SYS_10_MPU_3_LL_0,
202 &emif->emif_l3_config);
203 } else {
204 writel(EMIF_L3_CONFIG_VAL_SYS_10_LL_0,
205 &emif->emif_l3_config);
Aneesh Vcc565582011-07-21 09:10:09 -0400206 }
207}
208
Lokesh Vutla0f42de62012-05-22 00:03:25 +0000209static void ddr3_leveling(u32 base, const struct emif_regs *regs)
210{
211 struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
212
213 /* keep sdram in self-refresh */
214 writel(((LP_MODE_SELF_REFRESH << EMIF_REG_LP_MODE_SHIFT)
215 & EMIF_REG_LP_MODE_MASK), &emif->emif_pwr_mgmt_ctrl);
216 __udelay(130);
217
218 /*
219 * Set invert_clkout (if activated)--DDR_PHYCTRL_1
220 * Invert clock adds an additional half cycle delay on the command
221 * interface. The additional half cycle, is usually meant to enable
222 * leveling in the situation that DQS is later than CK on the board.It
223 * also helps provide some additional margin for leveling.
224 */
225 writel(regs->emif_ddr_phy_ctlr_1, &emif->emif_ddr_phy_ctrl_1);
226 writel(regs->emif_ddr_phy_ctlr_1, &emif->emif_ddr_phy_ctrl_1_shdw);
227 __udelay(130);
228
229 writel(((LP_MODE_DISABLE << EMIF_REG_LP_MODE_SHIFT)
230 & EMIF_REG_LP_MODE_MASK), &emif->emif_pwr_mgmt_ctrl);
231
232 /* Launch Full leveling */
233 writel(DDR3_FULL_LVL, &emif->emif_rd_wr_lvl_ctl);
234
235 /* Wait till full leveling is complete */
236 readl(&emif->emif_rd_wr_lvl_ctl);
237 __udelay(130);
238
239 /* Read data eye leveling no of samples */
240 config_data_eye_leveling_samples(base);
241
242 /* Launch 8 incremental WR_LVL- to compensate for PHY limitation */
243 writel(0x2 << EMIF_REG_WRLVLINC_INT_SHIFT, &emif->emif_rd_wr_lvl_ctl);
244 __udelay(130);
245
246 /* Launch Incremental leveling */
247 writel(DDR3_INC_LVL, &emif->emif_rd_wr_lvl_ctl);
248 __udelay(130);
249}
250
Sricharan Rffa98182013-05-30 03:19:39 +0000251static void ddr3_sw_leveling(u32 base, const struct emif_regs *regs)
252{
253 struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
254
255 writel(regs->emif_ddr_phy_ctlr_1, &emif->emif_ddr_phy_ctrl_1);
256 writel(regs->emif_ddr_phy_ctlr_1, &emif->emif_ddr_phy_ctrl_1_shdw);
257 config_data_eye_leveling_samples(base);
258
259 writel(regs->emif_rd_wr_lvl_ctl, &emif->emif_rd_wr_lvl_ctl);
260 writel(regs->sdram_config, &emif->emif_sdram_config);
261}
262
Lokesh Vutla0f42de62012-05-22 00:03:25 +0000263static void ddr3_init(u32 base, const struct emif_regs *regs)
264{
265 struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
Lokesh Vutla0f42de62012-05-22 00:03:25 +0000266
267 /*
268 * Set SDRAM_CONFIG and PHY control registers to locked frequency
269 * and RL =7. As the default values of the Mode Registers are not
270 * defined, contents of mode Registers must be fully initialized.
271 * H/W takes care of this initialization
272 */
Sricharan Rffa98182013-05-30 03:19:39 +0000273 writel(regs->sdram_config2, &emif->emif_lpddr2_nvm_config);
Lokesh Vutla0f42de62012-05-22 00:03:25 +0000274 writel(regs->sdram_config_init, &emif->emif_sdram_config);
275
276 writel(regs->emif_ddr_phy_ctlr_1_init, &emif->emif_ddr_phy_ctrl_1);
277
278 /* Update timing registers */
279 writel(regs->sdram_tim1, &emif->emif_sdram_tim_1);
280 writel(regs->sdram_tim2, &emif->emif_sdram_tim_2);
281 writel(regs->sdram_tim3, &emif->emif_sdram_tim_3);
282
283 writel(regs->ref_ctrl, &emif->emif_sdram_ref_ctrl);
284 writel(regs->read_idle_ctrl, &emif->emif_read_idlectrl);
285
Lokesh Vutla05dab552013-02-04 04:22:03 +0000286 do_ext_phy_settings(base, regs);
Lokesh Vutla0f42de62012-05-22 00:03:25 +0000287
288 /* enable leveling */
289 writel(regs->emif_rd_wr_lvl_rmp_ctl, &emif->emif_rd_wr_lvl_rmp_ctl);
290
Sricharan Rffa98182013-05-30 03:19:39 +0000291 if (omap_revision() == DRA752_ES1_0)
292 ddr3_sw_leveling(base, regs);
293 else
294 ddr3_leveling(base, regs);
Lokesh Vutla0f42de62012-05-22 00:03:25 +0000295}
296
Aneesh Vc0e88522011-07-21 09:10:12 -0400297#ifndef CONFIG_SYS_EMIF_PRECALCULATED_TIMING_REGS
298#define print_timing_reg(reg) debug(#reg" - 0x%08x\n", (reg))
299
Aneesh Vc0e88522011-07-21 09:10:12 -0400300/*
301 * Organization and refresh requirements for LPDDR2 devices of different
302 * types and densities. Derived from JESD209-2 section 2.4
303 */
304const struct lpddr2_addressing addressing_table[] = {
305 /* Banks tREFIx10 rowx32,rowx16 colx32,colx16 density */
306 {BANKS4, T_REFI_15_6, {ROW_12, ROW_12}, {COL_7, COL_8} },/*64M */
307 {BANKS4, T_REFI_15_6, {ROW_12, ROW_12}, {COL_8, COL_9} },/*128M */
308 {BANKS4, T_REFI_7_8, {ROW_13, ROW_13}, {COL_8, COL_9} },/*256M */
309 {BANKS4, T_REFI_7_8, {ROW_13, ROW_13}, {COL_9, COL_10} },/*512M */
310 {BANKS8, T_REFI_7_8, {ROW_13, ROW_13}, {COL_9, COL_10} },/*1GS4 */
311 {BANKS8, T_REFI_3_9, {ROW_14, ROW_14}, {COL_9, COL_10} },/*2GS4 */
312 {BANKS8, T_REFI_3_9, {ROW_14, ROW_14}, {COL_10, COL_11} },/*4G */
313 {BANKS8, T_REFI_3_9, {ROW_15, ROW_15}, {COL_10, COL_11} },/*8G */
314 {BANKS4, T_REFI_7_8, {ROW_14, ROW_14}, {COL_9, COL_10} },/*1GS2 */
315 {BANKS4, T_REFI_3_9, {ROW_15, ROW_15}, {COL_9, COL_10} },/*2GS2 */
316};
317
318static const u32 lpddr2_density_2_size_in_mbytes[] = {
319 8, /* 64Mb */
320 16, /* 128Mb */
321 32, /* 256Mb */
322 64, /* 512Mb */
323 128, /* 1Gb */
324 256, /* 2Gb */
325 512, /* 4Gb */
326 1024, /* 8Gb */
327 2048, /* 16Gb */
328 4096 /* 32Gb */
329};
330
331/*
332 * Calculate the period of DDR clock from frequency value and set the
333 * denominator and numerator in global variables for easy access later
334 */
335static void set_ddr_clk_period(u32 freq)
336{
337 /*
338 * period = 1/freq
339 * period_in_ns = 10^9/freq
340 */
341 *T_num = 1000000000;
342 *T_den = freq;
343 cancel_out(T_num, T_den, 200);
344
345}
346
347/*
348 * Convert time in nano seconds to number of cycles of DDR clock
349 */
350static inline u32 ns_2_cycles(u32 ns)
351{
352 return ((ns * (*T_den)) + (*T_num) - 1) / (*T_num);
353}
354
355/*
356 * ns_2_cycles with the difference that the time passed is 2 times the actual
357 * value(to avoid fractions). The cycles returned is for the original value of
358 * the timing parameter
359 */
360static inline u32 ns_x2_2_cycles(u32 ns)
361{
362 return ((ns * (*T_den)) + (*T_num) * 2 - 1) / ((*T_num) * 2);
363}
364
365/*
366 * Find addressing table index based on the device's type(S2 or S4) and
367 * density
368 */
369s8 addressing_table_index(u8 type, u8 density, u8 width)
370{
371 u8 index;
372 if ((density > LPDDR2_DENSITY_8Gb) || (width == LPDDR2_IO_WIDTH_8))
373 return -1;
374
375 /*
376 * Look at the way ADDR_TABLE_INDEX* values have been defined
377 * in emif.h compared to LPDDR2_DENSITY_* values
378 * The table is layed out in the increasing order of density
379 * (ignoring type). The exceptions 1GS2 and 2GS2 have been placed
380 * at the end
381 */
382 if ((type == LPDDR2_TYPE_S2) && (density == LPDDR2_DENSITY_1Gb))
383 index = ADDR_TABLE_INDEX1GS2;
384 else if ((type == LPDDR2_TYPE_S2) && (density == LPDDR2_DENSITY_2Gb))
385 index = ADDR_TABLE_INDEX2GS2;
386 else
387 index = density;
388
389 debug("emif: addressing table index %d\n", index);
390
391 return index;
392}
393
394/*
395 * Find the the right timing table from the array of timing
396 * tables of the device using DDR clock frequency
397 */
398static const struct lpddr2_ac_timings *get_timings_table(const struct
399 lpddr2_ac_timings const *const *device_timings,
400 u32 freq)
401{
402 u32 i, temp, freq_nearest;
403 const struct lpddr2_ac_timings *timings = 0;
404
405 emif_assert(freq <= MAX_LPDDR2_FREQ);
406 emif_assert(device_timings);
407
408 /*
409 * Start with the maximum allowed frequency - that is always safe
410 */
411 freq_nearest = MAX_LPDDR2_FREQ;
412 /*
413 * Find the timings table that has the max frequency value:
414 * i. Above or equal to the DDR frequency - safe
415 * ii. The lowest that satisfies condition (i) - optimal
416 */
417 for (i = 0; (i < MAX_NUM_SPEEDBINS) && device_timings[i]; i++) {
418 temp = device_timings[i]->max_freq;
419 if ((temp >= freq) && (temp <= freq_nearest)) {
420 freq_nearest = temp;
421 timings = device_timings[i];
422 }
423 }
424 debug("emif: timings table: %d\n", freq_nearest);
425 return timings;
426}
427
428/*
429 * Finds the value of emif_sdram_config_reg
430 * All parameters are programmed based on the device on CS0.
431 * If there is a device on CS1, it will be same as that on CS0 or
432 * it will be NVM. We don't support NVM yet.
433 * If cs1_device pointer is NULL it is assumed that there is no device
434 * on CS1
435 */
436static u32 get_sdram_config_reg(const struct lpddr2_device_details *cs0_device,
437 const struct lpddr2_device_details *cs1_device,
438 const struct lpddr2_addressing *addressing,
439 u8 RL)
440{
441 u32 config_reg = 0;
442
Sricharan62a86502011-11-15 09:50:00 -0500443 config_reg |= (cs0_device->type + 4) << EMIF_REG_SDRAM_TYPE_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400444 config_reg |= EMIF_INTERLEAVING_POLICY_MAX_INTERLEAVING <<
Sricharan62a86502011-11-15 09:50:00 -0500445 EMIF_REG_IBANK_POS_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400446
Sricharan62a86502011-11-15 09:50:00 -0500447 config_reg |= cs0_device->io_width << EMIF_REG_NARROW_MODE_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400448
Sricharan62a86502011-11-15 09:50:00 -0500449 config_reg |= RL << EMIF_REG_CL_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400450
451 config_reg |= addressing->row_sz[cs0_device->io_width] <<
Sricharan62a86502011-11-15 09:50:00 -0500452 EMIF_REG_ROWSIZE_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400453
Sricharan62a86502011-11-15 09:50:00 -0500454 config_reg |= addressing->num_banks << EMIF_REG_IBANK_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400455
456 config_reg |= (cs1_device ? EBANK_CS1_EN : EBANK_CS1_DIS) <<
Sricharan62a86502011-11-15 09:50:00 -0500457 EMIF_REG_EBANK_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400458
459 config_reg |= addressing->col_sz[cs0_device->io_width] <<
Sricharan62a86502011-11-15 09:50:00 -0500460 EMIF_REG_PAGESIZE_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400461
462 return config_reg;
463}
464
465static u32 get_sdram_ref_ctrl(u32 freq,
466 const struct lpddr2_addressing *addressing)
467{
468 u32 ref_ctrl = 0, val = 0, freq_khz;
469 freq_khz = freq / 1000;
470 /*
471 * refresh rate to be set is 'tREFI * freq in MHz
472 * division by 10000 to account for khz and x10 in t_REFI_us_x10
473 */
474 val = addressing->t_REFI_us_x10 * freq_khz / 10000;
Sricharan62a86502011-11-15 09:50:00 -0500475 ref_ctrl |= val << EMIF_REG_REFRESH_RATE_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400476
477 return ref_ctrl;
478}
479
480static u32 get_sdram_tim_1_reg(const struct lpddr2_ac_timings *timings,
481 const struct lpddr2_min_tck *min_tck,
482 const struct lpddr2_addressing *addressing)
483{
484 u32 tim1 = 0, val = 0;
485 val = max(min_tck->tWTR, ns_x2_2_cycles(timings->tWTRx2)) - 1;
Sricharan62a86502011-11-15 09:50:00 -0500486 tim1 |= val << EMIF_REG_T_WTR_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400487
488 if (addressing->num_banks == BANKS8)
489 val = (timings->tFAW * (*T_den) + 4 * (*T_num) - 1) /
490 (4 * (*T_num)) - 1;
491 else
492 val = max(min_tck->tRRD, ns_2_cycles(timings->tRRD)) - 1;
493
Sricharan62a86502011-11-15 09:50:00 -0500494 tim1 |= val << EMIF_REG_T_RRD_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400495
496 val = ns_2_cycles(timings->tRASmin + timings->tRPab) - 1;
Sricharan62a86502011-11-15 09:50:00 -0500497 tim1 |= val << EMIF_REG_T_RC_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400498
499 val = max(min_tck->tRAS_MIN, ns_2_cycles(timings->tRASmin)) - 1;
Sricharan62a86502011-11-15 09:50:00 -0500500 tim1 |= val << EMIF_REG_T_RAS_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400501
502 val = max(min_tck->tWR, ns_2_cycles(timings->tWR)) - 1;
Sricharan62a86502011-11-15 09:50:00 -0500503 tim1 |= val << EMIF_REG_T_WR_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400504
505 val = max(min_tck->tRCD, ns_2_cycles(timings->tRCD)) - 1;
Sricharan62a86502011-11-15 09:50:00 -0500506 tim1 |= val << EMIF_REG_T_RCD_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400507
508 val = max(min_tck->tRP_AB, ns_2_cycles(timings->tRPab)) - 1;
Sricharan62a86502011-11-15 09:50:00 -0500509 tim1 |= val << EMIF_REG_T_RP_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400510
511 return tim1;
512}
513
514static u32 get_sdram_tim_2_reg(const struct lpddr2_ac_timings *timings,
515 const struct lpddr2_min_tck *min_tck)
516{
517 u32 tim2 = 0, val = 0;
518 val = max(min_tck->tCKE, timings->tCKE) - 1;
Sricharan62a86502011-11-15 09:50:00 -0500519 tim2 |= val << EMIF_REG_T_CKE_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400520
521 val = max(min_tck->tRTP, ns_x2_2_cycles(timings->tRTPx2)) - 1;
Sricharan62a86502011-11-15 09:50:00 -0500522 tim2 |= val << EMIF_REG_T_RTP_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400523
524 /*
525 * tXSRD = tRFCab + 10 ns. XSRD and XSNR should have the
526 * same value
527 */
528 val = ns_2_cycles(timings->tXSR) - 1;
Sricharan62a86502011-11-15 09:50:00 -0500529 tim2 |= val << EMIF_REG_T_XSRD_SHIFT;
530 tim2 |= val << EMIF_REG_T_XSNR_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400531
532 val = max(min_tck->tXP, ns_x2_2_cycles(timings->tXPx2)) - 1;
Sricharan62a86502011-11-15 09:50:00 -0500533 tim2 |= val << EMIF_REG_T_XP_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400534
535 return tim2;
536}
537
538static u32 get_sdram_tim_3_reg(const struct lpddr2_ac_timings *timings,
539 const struct lpddr2_min_tck *min_tck,
540 const struct lpddr2_addressing *addressing)
541{
542 u32 tim3 = 0, val = 0;
543 val = min(timings->tRASmax * 10 / addressing->t_REFI_us_x10 - 1, 0xF);
Sricharan62a86502011-11-15 09:50:00 -0500544 tim3 |= val << EMIF_REG_T_RAS_MAX_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400545
546 val = ns_2_cycles(timings->tRFCab) - 1;
Sricharan62a86502011-11-15 09:50:00 -0500547 tim3 |= val << EMIF_REG_T_RFC_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400548
549 val = ns_x2_2_cycles(timings->tDQSCKMAXx2) - 1;
Sricharan62a86502011-11-15 09:50:00 -0500550 tim3 |= val << EMIF_REG_T_TDQSCKMAX_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400551
552 val = ns_2_cycles(timings->tZQCS) - 1;
Sricharan62a86502011-11-15 09:50:00 -0500553 tim3 |= val << EMIF_REG_ZQ_ZQCS_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400554
555 val = max(min_tck->tCKESR, ns_2_cycles(timings->tCKESR)) - 1;
Sricharan62a86502011-11-15 09:50:00 -0500556 tim3 |= val << EMIF_REG_T_CKESR_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400557
558 return tim3;
559}
560
561static u32 get_zq_config_reg(const struct lpddr2_device_details *cs1_device,
562 const struct lpddr2_addressing *addressing,
563 u8 volt_ramp)
564{
565 u32 zq = 0, val = 0;
566 if (volt_ramp)
567 val =
568 EMIF_ZQCS_INTERVAL_DVFS_IN_US * 10 /
569 addressing->t_REFI_us_x10;
570 else
571 val =
572 EMIF_ZQCS_INTERVAL_NORMAL_IN_US * 10 /
573 addressing->t_REFI_us_x10;
Sricharan62a86502011-11-15 09:50:00 -0500574 zq |= val << EMIF_REG_ZQ_REFINTERVAL_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400575
Sricharan62a86502011-11-15 09:50:00 -0500576 zq |= (REG_ZQ_ZQCL_MULT - 1) << EMIF_REG_ZQ_ZQCL_MULT_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400577
Sricharan62a86502011-11-15 09:50:00 -0500578 zq |= (REG_ZQ_ZQINIT_MULT - 1) << EMIF_REG_ZQ_ZQINIT_MULT_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400579
Sricharan62a86502011-11-15 09:50:00 -0500580 zq |= REG_ZQ_SFEXITEN_ENABLE << EMIF_REG_ZQ_SFEXITEN_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400581
582 /*
583 * Assuming that two chipselects have a single calibration resistor
584 * If there are indeed two calibration resistors, then this flag should
585 * be enabled to take advantage of dual calibration feature.
586 * This data should ideally come from board files. But considering
587 * that none of the boards today have calibration resistors per CS,
588 * it would be an unnecessary overhead.
589 */
Sricharan62a86502011-11-15 09:50:00 -0500590 zq |= REG_ZQ_DUALCALEN_DISABLE << EMIF_REG_ZQ_DUALCALEN_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400591
Sricharan62a86502011-11-15 09:50:00 -0500592 zq |= REG_ZQ_CS0EN_ENABLE << EMIF_REG_ZQ_CS0EN_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400593
Sricharan62a86502011-11-15 09:50:00 -0500594 zq |= (cs1_device ? 1 : 0) << EMIF_REG_ZQ_CS1EN_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400595
596 return zq;
597}
598
599static u32 get_temp_alert_config(const struct lpddr2_device_details *cs1_device,
600 const struct lpddr2_addressing *addressing,
601 u8 is_derated)
602{
603 u32 alert = 0, interval;
604 interval =
605 TEMP_ALERT_POLL_INTERVAL_MS * 10000 / addressing->t_REFI_us_x10;
606 if (is_derated)
607 interval *= 4;
Sricharan62a86502011-11-15 09:50:00 -0500608 alert |= interval << EMIF_REG_TA_REFINTERVAL_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400609
Sricharan62a86502011-11-15 09:50:00 -0500610 alert |= TEMP_ALERT_CONFIG_DEVCT_1 << EMIF_REG_TA_DEVCNT_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400611
Sricharan62a86502011-11-15 09:50:00 -0500612 alert |= TEMP_ALERT_CONFIG_DEVWDT_32 << EMIF_REG_TA_DEVWDT_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400613
Sricharan62a86502011-11-15 09:50:00 -0500614 alert |= 1 << EMIF_REG_TA_SFEXITEN_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400615
Sricharan62a86502011-11-15 09:50:00 -0500616 alert |= 1 << EMIF_REG_TA_CS0EN_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400617
Sricharan62a86502011-11-15 09:50:00 -0500618 alert |= (cs1_device ? 1 : 0) << EMIF_REG_TA_CS1EN_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400619
620 return alert;
621}
622
623static u32 get_read_idle_ctrl_reg(u8 volt_ramp)
624{
625 u32 idle = 0, val = 0;
626 if (volt_ramp)
Aneesh V639cfb62011-07-21 09:29:26 -0400627 val = ns_2_cycles(READ_IDLE_INTERVAL_DVFS) / 64 - 1;
Aneesh Vc0e88522011-07-21 09:10:12 -0400628 else
629 /*Maximum value in normal conditions - suggested by hw team */
630 val = 0x1FF;
Sricharan62a86502011-11-15 09:50:00 -0500631 idle |= val << EMIF_REG_READ_IDLE_INTERVAL_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400632
Sricharan62a86502011-11-15 09:50:00 -0500633 idle |= EMIF_REG_READ_IDLE_LEN_VAL << EMIF_REG_READ_IDLE_LEN_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400634
635 return idle;
636}
637
638static u32 get_ddr_phy_ctrl_1(u32 freq, u8 RL)
639{
640 u32 phy = 0, val = 0;
641
Sricharan62a86502011-11-15 09:50:00 -0500642 phy |= (RL + 2) << EMIF_REG_READ_LATENCY_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400643
644 if (freq <= 100000000)
645 val = EMIF_DLL_SLAVE_DLY_CTRL_100_MHZ_AND_LESS;
646 else if (freq <= 200000000)
647 val = EMIF_DLL_SLAVE_DLY_CTRL_200_MHZ;
648 else
649 val = EMIF_DLL_SLAVE_DLY_CTRL_400_MHZ;
Sricharan62a86502011-11-15 09:50:00 -0500650 phy |= val << EMIF_REG_DLL_SLAVE_DLY_CTRL_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400651
652 /* Other fields are constant magic values. Hardcode them together */
653 phy |= EMIF_DDR_PHY_CTRL_1_BASE_VAL <<
Sricharan62a86502011-11-15 09:50:00 -0500654 EMIF_EMIF_DDR_PHY_CTRL_1_BASE_VAL_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -0400655
656 return phy;
657}
658
Lokesh Vutlac323bd22013-04-04 19:51:14 +0000659static u32 get_emif_mem_size(u32 base)
Aneesh Vc0e88522011-07-21 09:10:12 -0400660{
661 u32 size_mbytes = 0, temp;
Lokesh Vutlac323bd22013-04-04 19:51:14 +0000662 struct emif_device_details dev_details;
663 struct lpddr2_device_details cs0_dev_details, cs1_dev_details;
664 u32 emif_nr = emif_num(base);
Aneesh Vc0e88522011-07-21 09:10:12 -0400665
Lokesh Vutlac323bd22013-04-04 19:51:14 +0000666 emif_reset_phy(base);
667 dev_details.cs0_device_details = emif_get_device_details(emif_nr, CS0,
668 &cs0_dev_details);
669 dev_details.cs1_device_details = emif_get_device_details(emif_nr, CS1,
670 &cs1_dev_details);
671 emif_reset_phy(base);
Aneesh Vc0e88522011-07-21 09:10:12 -0400672
Lokesh Vutlac323bd22013-04-04 19:51:14 +0000673 if (dev_details.cs0_device_details) {
674 temp = dev_details.cs0_device_details->density;
Aneesh Vc0e88522011-07-21 09:10:12 -0400675 size_mbytes += lpddr2_density_2_size_in_mbytes[temp];
676 }
677
Lokesh Vutlac323bd22013-04-04 19:51:14 +0000678 if (dev_details.cs1_device_details) {
679 temp = dev_details.cs1_device_details->density;
Aneesh Vc0e88522011-07-21 09:10:12 -0400680 size_mbytes += lpddr2_density_2_size_in_mbytes[temp];
681 }
682 /* convert to bytes */
683 return size_mbytes << 20;
684}
685
686/* Gets the encoding corresponding to a given DMM section size */
687u32 get_dmm_section_size_map(u32 section_size)
688{
689 /*
690 * Section size mapping:
691 * 0x0: 16-MiB section
692 * 0x1: 32-MiB section
693 * 0x2: 64-MiB section
694 * 0x3: 128-MiB section
695 * 0x4: 256-MiB section
696 * 0x5: 512-MiB section
697 * 0x6: 1-GiB section
698 * 0x7: 2-GiB section
699 */
700 section_size >>= 24; /* divide by 16 MB */
701 return log_2_n_round_down(section_size);
702}
703
704static void emif_calculate_regs(
705 const struct emif_device_details *emif_dev_details,
706 u32 freq, struct emif_regs *regs)
707{
708 u32 temp, sys_freq;
709 const struct lpddr2_addressing *addressing;
710 const struct lpddr2_ac_timings *timings;
711 const struct lpddr2_min_tck *min_tck;
712 const struct lpddr2_device_details *cs0_dev_details =
713 emif_dev_details->cs0_device_details;
714 const struct lpddr2_device_details *cs1_dev_details =
715 emif_dev_details->cs1_device_details;
716 const struct lpddr2_device_timings *cs0_dev_timings =
717 emif_dev_details->cs0_device_timings;
718
719 emif_assert(emif_dev_details);
720 emif_assert(regs);
721 /*
722 * You can not have a device on CS1 without one on CS0
723 * So configuring EMIF without a device on CS0 doesn't
724 * make sense
725 */
726 emif_assert(cs0_dev_details);
727 emif_assert(cs0_dev_details->type != LPDDR2_TYPE_NVM);
728 /*
729 * If there is a device on CS1 it should be same type as CS0
730 * (or NVM. But NVM is not supported in this driver yet)
731 */
732 emif_assert((cs1_dev_details == NULL) ||
733 (cs1_dev_details->type == LPDDR2_TYPE_NVM) ||
734 (cs0_dev_details->type == cs1_dev_details->type));
735 emif_assert(freq <= MAX_LPDDR2_FREQ);
736
737 set_ddr_clk_period(freq);
738
739 /*
740 * The device on CS0 is used for all timing calculations
741 * There is only one set of registers for timings per EMIF. So, if the
742 * second CS(CS1) has a device, it should have the same timings as the
743 * device on CS0
744 */
745 timings = get_timings_table(cs0_dev_timings->ac_timings, freq);
746 emif_assert(timings);
747 min_tck = cs0_dev_timings->min_tck;
748
749 temp = addressing_table_index(cs0_dev_details->type,
750 cs0_dev_details->density,
751 cs0_dev_details->io_width);
752
753 emif_assert((temp >= 0));
754 addressing = &(addressing_table[temp]);
755 emif_assert(addressing);
756
757 sys_freq = get_sys_clk_freq();
758
759 regs->sdram_config_init = get_sdram_config_reg(cs0_dev_details,
760 cs1_dev_details,
761 addressing, RL_BOOT);
762
763 regs->sdram_config = get_sdram_config_reg(cs0_dev_details,
764 cs1_dev_details,
765 addressing, RL_FINAL);
766
767 regs->ref_ctrl = get_sdram_ref_ctrl(freq, addressing);
768
769 regs->sdram_tim1 = get_sdram_tim_1_reg(timings, min_tck, addressing);
770
771 regs->sdram_tim2 = get_sdram_tim_2_reg(timings, min_tck);
772
773 regs->sdram_tim3 = get_sdram_tim_3_reg(timings, min_tck, addressing);
774
775 regs->read_idle_ctrl = get_read_idle_ctrl_reg(LPDDR2_VOLTAGE_STABLE);
776
777 regs->temp_alert_config =
778 get_temp_alert_config(cs1_dev_details, addressing, 0);
779
780 regs->zq_config = get_zq_config_reg(cs1_dev_details, addressing,
781 LPDDR2_VOLTAGE_STABLE);
782
783 regs->emif_ddr_phy_ctlr_1_init =
784 get_ddr_phy_ctrl_1(sys_freq / 2, RL_BOOT);
785
786 regs->emif_ddr_phy_ctlr_1 =
787 get_ddr_phy_ctrl_1(freq, RL_FINAL);
788
789 regs->freq = freq;
790
791 print_timing_reg(regs->sdram_config_init);
792 print_timing_reg(regs->sdram_config);
793 print_timing_reg(regs->ref_ctrl);
794 print_timing_reg(regs->sdram_tim1);
795 print_timing_reg(regs->sdram_tim2);
796 print_timing_reg(regs->sdram_tim3);
797 print_timing_reg(regs->read_idle_ctrl);
798 print_timing_reg(regs->temp_alert_config);
799 print_timing_reg(regs->zq_config);
800 print_timing_reg(regs->emif_ddr_phy_ctlr_1);
801 print_timing_reg(regs->emif_ddr_phy_ctlr_1_init);
802}
803#endif /* CONFIG_SYS_EMIF_PRECALCULATED_TIMING_REGS */
804
Aneesh Vced762a2011-07-21 09:10:15 -0400805#ifdef CONFIG_SYS_AUTOMATIC_SDRAM_DETECTION
806const char *get_lpddr2_type(u8 type_id)
807{
808 switch (type_id) {
809 case LPDDR2_TYPE_S4:
810 return "LPDDR2-S4";
811 case LPDDR2_TYPE_S2:
812 return "LPDDR2-S2";
813 default:
814 return NULL;
815 }
816}
817
818const char *get_lpddr2_io_width(u8 width_id)
819{
820 switch (width_id) {
821 case LPDDR2_IO_WIDTH_8:
822 return "x8";
823 case LPDDR2_IO_WIDTH_16:
824 return "x16";
825 case LPDDR2_IO_WIDTH_32:
826 return "x32";
827 default:
828 return NULL;
829 }
830}
831
832const char *get_lpddr2_manufacturer(u32 manufacturer)
833{
834 switch (manufacturer) {
835 case LPDDR2_MANUFACTURER_SAMSUNG:
836 return "Samsung";
837 case LPDDR2_MANUFACTURER_QIMONDA:
838 return "Qimonda";
839 case LPDDR2_MANUFACTURER_ELPIDA:
840 return "Elpida";
841 case LPDDR2_MANUFACTURER_ETRON:
842 return "Etron";
843 case LPDDR2_MANUFACTURER_NANYA:
844 return "Nanya";
845 case LPDDR2_MANUFACTURER_HYNIX:
846 return "Hynix";
847 case LPDDR2_MANUFACTURER_MOSEL:
848 return "Mosel";
849 case LPDDR2_MANUFACTURER_WINBOND:
850 return "Winbond";
851 case LPDDR2_MANUFACTURER_ESMT:
852 return "ESMT";
853 case LPDDR2_MANUFACTURER_SPANSION:
854 return "Spansion";
855 case LPDDR2_MANUFACTURER_SST:
856 return "SST";
857 case LPDDR2_MANUFACTURER_ZMOS:
858 return "ZMOS";
859 case LPDDR2_MANUFACTURER_INTEL:
860 return "Intel";
861 case LPDDR2_MANUFACTURER_NUMONYX:
862 return "Numonyx";
863 case LPDDR2_MANUFACTURER_MICRON:
864 return "Micron";
865 default:
866 return NULL;
867 }
868}
869
870static void display_sdram_details(u32 emif_nr, u32 cs,
871 struct lpddr2_device_details *device)
872{
873 const char *mfg_str;
874 const char *type_str;
875 char density_str[10];
876 u32 density;
877
878 debug("EMIF%d CS%d\t", emif_nr, cs);
879
880 if (!device) {
881 debug("None\n");
882 return;
883 }
884
885 mfg_str = get_lpddr2_manufacturer(device->manufacturer);
886 type_str = get_lpddr2_type(device->type);
887
888 density = lpddr2_density_2_size_in_mbytes[device->density];
889 if ((density / 1024 * 1024) == density) {
890 density /= 1024;
891 sprintf(density_str, "%d GB", density);
892 } else
893 sprintf(density_str, "%d MB", density);
894 if (mfg_str && type_str)
895 debug("%s\t\t%s\t%s\n", mfg_str, type_str, density_str);
896}
897
898static u8 is_lpddr2_sdram_present(u32 base, u32 cs,
899 struct lpddr2_device_details *lpddr2_device)
900{
901 u32 mr = 0, temp;
902
903 mr = get_mr(base, cs, LPDDR2_MR0);
904 if (mr > 0xFF) {
905 /* Mode register value bigger than 8 bit */
906 return 0;
907 }
908
909 temp = (mr & LPDDR2_MR0_DI_MASK) >> LPDDR2_MR0_DI_SHIFT;
910 if (temp) {
911 /* Not SDRAM */
912 return 0;
913 }
914 temp = (mr & LPDDR2_MR0_DNVI_MASK) >> LPDDR2_MR0_DNVI_SHIFT;
915
916 if (temp) {
917 /* DNV supported - But DNV is only supported for NVM */
918 return 0;
919 }
920
921 mr = get_mr(base, cs, LPDDR2_MR4);
922 if (mr > 0xFF) {
923 /* Mode register value bigger than 8 bit */
924 return 0;
925 }
926
927 mr = get_mr(base, cs, LPDDR2_MR5);
Steve Sakomance515a62012-05-30 07:38:08 +0000928 if (mr > 0xFF) {
Aneesh Vced762a2011-07-21 09:10:15 -0400929 /* Mode register value bigger than 8 bit */
930 return 0;
931 }
932
933 if (!get_lpddr2_manufacturer(mr)) {
934 /* Manufacturer not identified */
935 return 0;
936 }
937 lpddr2_device->manufacturer = mr;
938
939 mr = get_mr(base, cs, LPDDR2_MR6);
940 if (mr >= 0xFF) {
941 /* Mode register value bigger than 8 bit */
942 return 0;
943 }
944
945 mr = get_mr(base, cs, LPDDR2_MR7);
946 if (mr >= 0xFF) {
947 /* Mode register value bigger than 8 bit */
948 return 0;
949 }
950
951 mr = get_mr(base, cs, LPDDR2_MR8);
952 if (mr >= 0xFF) {
953 /* Mode register value bigger than 8 bit */
954 return 0;
955 }
956
957 temp = (mr & MR8_TYPE_MASK) >> MR8_TYPE_SHIFT;
958 if (!get_lpddr2_type(temp)) {
959 /* Not SDRAM */
960 return 0;
961 }
962 lpddr2_device->type = temp;
963
964 temp = (mr & MR8_DENSITY_MASK) >> MR8_DENSITY_SHIFT;
965 if (temp > LPDDR2_DENSITY_32Gb) {
966 /* Density not supported */
967 return 0;
968 }
969 lpddr2_device->density = temp;
970
971 temp = (mr & MR8_IO_WIDTH_MASK) >> MR8_IO_WIDTH_SHIFT;
972 if (!get_lpddr2_io_width(temp)) {
973 /* IO width unsupported value */
974 return 0;
975 }
976 lpddr2_device->io_width = temp;
977
978 /*
979 * If all the above tests pass we should
980 * have a device on this chip-select
981 */
982 return 1;
983}
984
Aneesh V14f821a2011-09-08 11:05:53 -0400985struct lpddr2_device_details *emif_get_device_details(u32 emif_nr, u8 cs,
Aneesh Vced762a2011-07-21 09:10:15 -0400986 struct lpddr2_device_details *lpddr2_dev_details)
987{
988 u32 phy;
Sricharan62a86502011-11-15 09:50:00 -0500989 u32 base = (emif_nr == 1) ? EMIF1_BASE : EMIF2_BASE;
990
Aneesh Vced762a2011-07-21 09:10:15 -0400991 struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
992
993 if (!lpddr2_dev_details)
994 return NULL;
995
996 /* Do the minimum init for mode register accesses */
Lokesh Vutlaae642392012-05-29 19:26:42 +0000997 if (!(running_from_sdram() || warm_reset())) {
Aneesh Vced762a2011-07-21 09:10:15 -0400998 phy = get_ddr_phy_ctrl_1(get_sys_clk_freq() / 2, RL_BOOT);
999 writel(phy, &emif->emif_ddr_phy_ctrl_1);
1000 }
1001
1002 if (!(is_lpddr2_sdram_present(base, cs, lpddr2_dev_details)))
1003 return NULL;
1004
1005 display_sdram_details(emif_num(base), cs, lpddr2_dev_details);
1006
1007 return lpddr2_dev_details;
1008}
Aneesh Vced762a2011-07-21 09:10:15 -04001009#endif /* CONFIG_SYS_AUTOMATIC_SDRAM_DETECTION */
1010
Aneesh Vcc565582011-07-21 09:10:09 -04001011static void do_sdram_init(u32 base)
1012{
1013 const struct emif_regs *regs;
1014 u32 in_sdram, emif_nr;
1015
1016 debug(">>do_sdram_init() %x\n", base);
1017
1018 in_sdram = running_from_sdram();
Sricharan62a86502011-11-15 09:50:00 -05001019 emif_nr = (base == EMIF1_BASE) ? 1 : 2;
Aneesh Vcc565582011-07-21 09:10:09 -04001020
Aneesh Vc0e88522011-07-21 09:10:12 -04001021#ifdef CONFIG_SYS_EMIF_PRECALCULATED_TIMING_REGS
Aneesh Vcc565582011-07-21 09:10:09 -04001022 emif_get_reg_dump(emif_nr, &regs);
1023 if (!regs) {
1024 debug("EMIF: reg dump not provided\n");
1025 return;
1026 }
Aneesh Vc0e88522011-07-21 09:10:12 -04001027#else
1028 /*
1029 * The user has not provided the register values. We need to
1030 * calculate it based on the timings and the DDR frequency
1031 */
1032 struct emif_device_details dev_details;
1033 struct emif_regs calculated_regs;
1034
1035 /*
1036 * Get device details:
1037 * - Discovered if CONFIG_SYS_AUTOMATIC_SDRAM_DETECTION is set
1038 * - Obtained from user otherwise
1039 */
1040 struct lpddr2_device_details cs0_dev_details, cs1_dev_details;
Aneesh V14f821a2011-09-08 11:05:53 -04001041 emif_reset_phy(base);
Aneesh V7d9fa572011-11-21 23:39:02 +00001042 dev_details.cs0_device_details = emif_get_device_details(emif_nr, CS0,
Aneesh V14f821a2011-09-08 11:05:53 -04001043 &cs0_dev_details);
Aneesh V7d9fa572011-11-21 23:39:02 +00001044 dev_details.cs1_device_details = emif_get_device_details(emif_nr, CS1,
Aneesh V14f821a2011-09-08 11:05:53 -04001045 &cs1_dev_details);
1046 emif_reset_phy(base);
Aneesh Vc0e88522011-07-21 09:10:12 -04001047
1048 /* Return if no devices on this EMIF */
1049 if (!dev_details.cs0_device_details &&
1050 !dev_details.cs1_device_details) {
Aneesh Vc0e88522011-07-21 09:10:12 -04001051 return;
1052 }
Aneesh Vcc565582011-07-21 09:10:09 -04001053
Aneesh Vc0e88522011-07-21 09:10:12 -04001054 /*
1055 * Get device timings:
1056 * - Default timings specified by JESD209-2 if
1057 * CONFIG_SYS_DEFAULT_LPDDR2_TIMINGS is set
1058 * - Obtained from user otherwise
1059 */
1060 emif_get_device_timings(emif_nr, &dev_details.cs0_device_timings,
1061 &dev_details.cs1_device_timings);
1062
1063 /* Calculate the register values */
Sricharan9784f1f2011-11-15 09:49:58 -05001064 emif_calculate_regs(&dev_details, omap_ddr_clk(), &calculated_regs);
Aneesh Vc0e88522011-07-21 09:10:12 -04001065 regs = &calculated_regs;
1066#endif /* CONFIG_SYS_EMIF_PRECALCULATED_TIMING_REGS */
1067
Aneesh Vcc565582011-07-21 09:10:09 -04001068 /*
1069 * Initializing the LPDDR2 device can not happen from SDRAM.
1070 * Changing the timing registers in EMIF can happen(going from one
1071 * OPP to another)
1072 */
Lokesh Vutlaae642392012-05-29 19:26:42 +00001073 if (!(in_sdram || warm_reset())) {
Lokesh Vutlafef54c32013-02-04 04:21:59 +00001074 if (emif_sdram_type() == EMIF_SDRAM_TYPE_LPDDR2)
Lokesh Vutla0f42de62012-05-22 00:03:25 +00001075 lpddr2_init(base, regs);
1076 else
1077 ddr3_init(base, regs);
1078 }
Lokesh Vutlab66f3dc2013-03-27 20:24:42 +00001079 if (warm_reset() && (emif_sdram_type() == EMIF_SDRAM_TYPE_DDR3)) {
1080 set_lpmode_selfrefresh(base);
1081 emif_reset_phy(base);
Sricharan Rffa98182013-05-30 03:19:39 +00001082 if (omap_revision() == DRA752_ES1_0)
1083 ddr3_sw_leveling(base, regs);
1084 else
1085 ddr3_leveling(base, regs);
Lokesh Vutlab66f3dc2013-03-27 20:24:42 +00001086 }
Aneesh Vcc565582011-07-21 09:10:09 -04001087
1088 /* Write to the shadow registers */
1089 emif_update_timings(base, regs);
1090
1091 debug("<<do_sdram_init() %x\n", base);
1092}
1093
Sricharan62a86502011-11-15 09:50:00 -05001094void emif_post_init_config(u32 base)
Aneesh Vcc565582011-07-21 09:10:09 -04001095{
1096 struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
Sricharan62a86502011-11-15 09:50:00 -05001097 u32 omap_rev = omap_revision();
1098
Aneesh Vcc565582011-07-21 09:10:09 -04001099 /* reset phy on ES2.0 */
Sricharan62a86502011-11-15 09:50:00 -05001100 if (omap_rev == OMAP4430_ES2_0)
Aneesh Vcc565582011-07-21 09:10:09 -04001101 emif_reset_phy(base);
1102
1103 /* Put EMIF back in smart idle on ES1.0 */
Sricharan62a86502011-11-15 09:50:00 -05001104 if (omap_rev == OMAP4430_ES1_0)
Aneesh Vcc565582011-07-21 09:10:09 -04001105 writel(0x80000000, &emif->emif_pwr_mgmt_ctrl);
1106}
1107
Sricharan62a86502011-11-15 09:50:00 -05001108void dmm_init(u32 base)
Aneesh Vcc565582011-07-21 09:10:09 -04001109{
1110 const struct dmm_lisa_map_regs *lisa_map_regs;
Lokesh Vutla80242592012-11-15 21:06:33 +00001111 u32 i, section, valid;
Aneesh Vcc565582011-07-21 09:10:09 -04001112
Aneesh Vc0e88522011-07-21 09:10:12 -04001113#ifdef CONFIG_SYS_EMIF_PRECALCULATED_TIMING_REGS
Aneesh Vcc565582011-07-21 09:10:09 -04001114 emif_get_dmm_regs(&lisa_map_regs);
Aneesh Vc0e88522011-07-21 09:10:12 -04001115#else
1116 u32 emif1_size, emif2_size, mapped_size, section_map = 0;
1117 u32 section_cnt, sys_addr;
1118 struct dmm_lisa_map_regs lis_map_regs_calculated = {0};
1119
1120 mapped_size = 0;
1121 section_cnt = 3;
1122 sys_addr = CONFIG_SYS_SDRAM_BASE;
Lokesh Vutlac323bd22013-04-04 19:51:14 +00001123 emif1_size = get_emif_mem_size(EMIF1_BASE);
1124 emif2_size = get_emif_mem_size(EMIF2_BASE);
Aneesh Vc0e88522011-07-21 09:10:12 -04001125 debug("emif1_size 0x%x emif2_size 0x%x\n", emif1_size, emif2_size);
1126
1127 if (!emif1_size && !emif2_size)
1128 return;
1129
1130 /* symmetric interleaved section */
1131 if (emif1_size && emif2_size) {
1132 mapped_size = min(emif1_size, emif2_size);
1133 section_map = DMM_LISA_MAP_INTERLEAVED_BASE_VAL;
Sricharan62a86502011-11-15 09:50:00 -05001134 section_map |= 0 << EMIF_SDRC_ADDR_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -04001135 /* only MSB */
1136 section_map |= (sys_addr >> 24) <<
Sricharan62a86502011-11-15 09:50:00 -05001137 EMIF_SYS_ADDR_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -04001138 section_map |= get_dmm_section_size_map(mapped_size * 2)
Sricharan62a86502011-11-15 09:50:00 -05001139 << EMIF_SYS_SIZE_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -04001140 lis_map_regs_calculated.dmm_lisa_map_3 = section_map;
1141 emif1_size -= mapped_size;
1142 emif2_size -= mapped_size;
1143 sys_addr += (mapped_size * 2);
1144 section_cnt--;
1145 }
1146
1147 /*
1148 * Single EMIF section(we can have a maximum of 1 single EMIF
1149 * section- either EMIF1 or EMIF2 or none, but not both)
1150 */
1151 if (emif1_size) {
1152 section_map = DMM_LISA_MAP_EMIF1_ONLY_BASE_VAL;
1153 section_map |= get_dmm_section_size_map(emif1_size)
Sricharan62a86502011-11-15 09:50:00 -05001154 << EMIF_SYS_SIZE_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -04001155 /* only MSB */
1156 section_map |= (mapped_size >> 24) <<
Sricharan62a86502011-11-15 09:50:00 -05001157 EMIF_SDRC_ADDR_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -04001158 /* only MSB */
Sricharan62a86502011-11-15 09:50:00 -05001159 section_map |= (sys_addr >> 24) << EMIF_SYS_ADDR_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -04001160 section_cnt--;
1161 }
1162 if (emif2_size) {
1163 section_map = DMM_LISA_MAP_EMIF2_ONLY_BASE_VAL;
1164 section_map |= get_dmm_section_size_map(emif2_size) <<
Sricharan62a86502011-11-15 09:50:00 -05001165 EMIF_SYS_SIZE_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -04001166 /* only MSB */
Sricharan62a86502011-11-15 09:50:00 -05001167 section_map |= mapped_size >> 24 << EMIF_SDRC_ADDR_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -04001168 /* only MSB */
Sricharan62a86502011-11-15 09:50:00 -05001169 section_map |= sys_addr >> 24 << EMIF_SYS_ADDR_SHIFT;
Aneesh Vc0e88522011-07-21 09:10:12 -04001170 section_cnt--;
1171 }
1172
1173 if (section_cnt == 2) {
1174 /* Only 1 section - either symmetric or single EMIF */
1175 lis_map_regs_calculated.dmm_lisa_map_3 = section_map;
1176 lis_map_regs_calculated.dmm_lisa_map_2 = 0;
1177 lis_map_regs_calculated.dmm_lisa_map_1 = 0;
1178 } else {
1179 /* 2 sections - 1 symmetric, 1 single EMIF */
1180 lis_map_regs_calculated.dmm_lisa_map_2 = section_map;
1181 lis_map_regs_calculated.dmm_lisa_map_1 = 0;
1182 }
1183
1184 /* TRAP for invalid TILER mappings in section 0 */
1185 lis_map_regs_calculated.dmm_lisa_map_0 = DMM_LISA_MAP_0_INVAL_ADDR_TRAP;
Aneesh Vcc565582011-07-21 09:10:09 -04001186
Lokesh Vutlaba66ce22013-06-19 10:50:45 +05301187 if (omap_revision() >= OMAP4460_ES1_0)
1188 lis_map_regs_calculated.is_ma_present = 1;
1189
Aneesh Vc0e88522011-07-21 09:10:12 -04001190 lisa_map_regs = &lis_map_regs_calculated;
1191#endif
Aneesh Vcc565582011-07-21 09:10:09 -04001192 struct dmm_lisa_map_regs *hw_lisa_map_regs =
1193 (struct dmm_lisa_map_regs *)base;
1194
1195 writel(0, &hw_lisa_map_regs->dmm_lisa_map_3);
1196 writel(0, &hw_lisa_map_regs->dmm_lisa_map_2);
1197 writel(0, &hw_lisa_map_regs->dmm_lisa_map_1);
1198 writel(0, &hw_lisa_map_regs->dmm_lisa_map_0);
1199
1200 writel(lisa_map_regs->dmm_lisa_map_3,
1201 &hw_lisa_map_regs->dmm_lisa_map_3);
1202 writel(lisa_map_regs->dmm_lisa_map_2,
1203 &hw_lisa_map_regs->dmm_lisa_map_2);
1204 writel(lisa_map_regs->dmm_lisa_map_1,
1205 &hw_lisa_map_regs->dmm_lisa_map_1);
1206 writel(lisa_map_regs->dmm_lisa_map_0,
1207 &hw_lisa_map_regs->dmm_lisa_map_0);
Aneesh V639cfb62011-07-21 09:29:26 -04001208
Lokesh Vutla8caa56c2013-02-12 21:29:07 +00001209 if (lisa_map_regs->is_ma_present) {
Aneesh V639cfb62011-07-21 09:29:26 -04001210 hw_lisa_map_regs =
Sricharan62a86502011-11-15 09:50:00 -05001211 (struct dmm_lisa_map_regs *)MA_BASE;
Aneesh V639cfb62011-07-21 09:29:26 -04001212
1213 writel(lisa_map_regs->dmm_lisa_map_3,
1214 &hw_lisa_map_regs->dmm_lisa_map_3);
1215 writel(lisa_map_regs->dmm_lisa_map_2,
1216 &hw_lisa_map_regs->dmm_lisa_map_2);
1217 writel(lisa_map_regs->dmm_lisa_map_1,
1218 &hw_lisa_map_regs->dmm_lisa_map_1);
1219 writel(lisa_map_regs->dmm_lisa_map_0,
1220 &hw_lisa_map_regs->dmm_lisa_map_0);
1221 }
Lokesh Vutla80242592012-11-15 21:06:33 +00001222
1223 /*
1224 * EMIF should be configured only when
1225 * memory is mapped on it. Using emif1_enabled
1226 * and emif2_enabled variables for this.
1227 */
1228 emif1_enabled = 0;
1229 emif2_enabled = 0;
1230 for (i = 0; i < 4; i++) {
1231 section = __raw_readl(DMM_BASE + i*4);
1232 valid = (section & EMIF_SDRC_MAP_MASK) >>
1233 (EMIF_SDRC_MAP_SHIFT);
1234 if (valid == 3) {
1235 emif1_enabled = 1;
1236 emif2_enabled = 1;
1237 break;
1238 } else if (valid == 1) {
1239 emif1_enabled = 1;
1240 } else if (valid == 2) {
1241 emif2_enabled = 1;
1242 }
1243 }
1244
Aneesh Vcc565582011-07-21 09:10:09 -04001245}
1246
1247/*
1248 * SDRAM initialization:
1249 * SDRAM initialization has two parts:
1250 * 1. Configuring the SDRAM device
1251 * 2. Update the AC timings related parameters in the EMIF module
1252 * (1) should be done only once and should not be done while we are
1253 * running from SDRAM.
1254 * (2) can and should be done more than once if OPP changes.
1255 * Particularly, this may be needed when we boot without SPL and
1256 * and using Configuration Header(CH). ROM code supports only at 50% OPP
1257 * at boot (low power boot). So u-boot has to switch to OPP100 and update
1258 * the frequency. So,
1259 * Doing (1) and (2) makes sense - first time initialization
1260 * Doing (2) and not (1) makes sense - OPP change (when using CH)
1261 * Doing (1) and not (2) doen't make sense
1262 * See do_sdram_init() for the details
1263 */
1264void sdram_init(void)
1265{
1266 u32 in_sdram, size_prog, size_detect;
Lokesh Vutlafef54c32013-02-04 04:21:59 +00001267 u32 sdram_type = emif_sdram_type();
Aneesh Vcc565582011-07-21 09:10:09 -04001268
1269 debug(">>sdram_init()\n");
1270
Sricharan9310ff72011-11-15 09:49:55 -05001271 if (omap_hw_init_context() == OMAP_INIT_CONTEXT_UBOOT_AFTER_SPL)
Aneesh Vcc565582011-07-21 09:10:09 -04001272 return;
1273
1274 in_sdram = running_from_sdram();
1275 debug("in_sdram = %d\n", in_sdram);
1276
Lokesh Vutlab66f3dc2013-03-27 20:24:42 +00001277 if (!in_sdram) {
1278 if ((sdram_type == EMIF_SDRAM_TYPE_LPDDR2) && !warm_reset())
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +00001279 bypass_dpll((*prcm)->cm_clkmode_dpll_core);
Lokesh Vutlab66f3dc2013-03-27 20:24:42 +00001280 else if (sdram_type == EMIF_SDRAM_TYPE_DDR3)
SRICHARAN Rfb6aa1f2013-02-04 04:22:00 +00001281 writel(CM_DLL_CTRL_NO_OVERRIDE, (*prcm)->cm_dll_ctrl);
Lokesh Vutlacdfc4ea2012-05-22 00:03:26 +00001282 }
Aneesh Vcc565582011-07-21 09:10:09 -04001283
Lokesh Vutlaae642392012-05-29 19:26:42 +00001284 if (!in_sdram)
Sricharan62a86502011-11-15 09:50:00 -05001285 dmm_init(DMM_BASE);
Lokesh Vutlaae642392012-05-29 19:26:42 +00001286
Lokesh Vutla80242592012-11-15 21:06:33 +00001287 if (emif1_enabled)
1288 do_sdram_init(EMIF1_BASE);
1289
1290 if (emif2_enabled)
1291 do_sdram_init(EMIF2_BASE);
1292
Lokesh Vutlaae642392012-05-29 19:26:42 +00001293 if (!(in_sdram || warm_reset())) {
Lokesh Vutla80242592012-11-15 21:06:33 +00001294 if (emif1_enabled)
1295 emif_post_init_config(EMIF1_BASE);
1296 if (emif2_enabled)
1297 emif_post_init_config(EMIF2_BASE);
Aneesh Vcc565582011-07-21 09:10:09 -04001298 }
1299
1300 /* for the shadow registers to take effect */
Lokesh Vutlafef54c32013-02-04 04:21:59 +00001301 if (sdram_type == EMIF_SDRAM_TYPE_LPDDR2)
Lokesh Vutlacdfc4ea2012-05-22 00:03:26 +00001302 freq_update_core();
Aneesh Vcc565582011-07-21 09:10:09 -04001303
1304 /* Do some testing after the init */
1305 if (!in_sdram) {
Sricharan9310ff72011-11-15 09:49:55 -05001306 size_prog = omap_sdram_size();
SRICHARAN Rb8a0bca2012-05-17 00:12:08 +00001307 size_prog = log_2_n_round_down(size_prog);
1308 size_prog = (1 << size_prog);
1309
Aneesh Vcc565582011-07-21 09:10:09 -04001310 size_detect = get_ram_size((long *)CONFIG_SYS_SDRAM_BASE,
1311 size_prog);
1312 /* Compare with the size programmed */
1313 if (size_detect != size_prog) {
1314 printf("SDRAM: identified size not same as expected"
1315 " size identified: %x expected: %x\n",
1316 size_detect,
1317 size_prog);
1318 } else
1319 debug("get_ram_size() successful");
1320 }
1321
1322 debug("<<sdram_init()\n");
1323}