1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2012, Stefano Babic <sbabic@denx.de>
7 #include <linux/errno.h>
8 #include <asm/arch/imx-regs.h>
9 #include <linux/types.h>
10 #include <asm/arch/sys_proto.h>
12 #define ESDCTL_DDR2_EMR2 0x04000000
13 #define ESDCTL_DDR2_EMR3 0x06000000
14 #define ESDCTL_PRECHARGE 0x00000400
15 #define ESDCTL_DDR2_EN_DLL 0x02000400
16 #define ESDCTL_DDR2_RESET_DLL 0x00000333
17 #define ESDCTL_DDR2_MR 0x00000233
18 #define ESDCTL_DDR2_OCD_DEFAULT 0x02000780
28 #define set_mode(x, en, m) (x | (en << 31) | (m << 28))
30 static inline void dram_wait(unsigned int count)
32 volatile unsigned int wait = count;
39 void mx3_setup_sdram_bank(u32 start_address, u32 ddr2_config,
40 u32 row, u32 col, u32 dsize, u32 refresh)
42 struct esdc_regs *esdc = (struct esdc_regs *)ESDCTL_BASE_ADDR;
43 u32 *cfg_reg, *ctl_reg;
47 switch (start_address) {
49 cfg_reg = &esdc->esdcfg0;
50 ctl_reg = &esdc->esdctl0;
53 cfg_reg = &esdc->esdcfg1;
54 ctl_reg = &esdc->esdctl1;
60 /* The MX35 supports 11 up to 14 rows */
61 if (row < 11 || row > 14 || col < 8 || col > 10)
63 ctlval = (row - 11) << 24 | (col - 8) << 20 | (dsize << 16);
65 /* Initialize MISC register for DDR2 */
66 val = ESDC_MISC_RST | ESDC_MISC_MDDR_EN | ESDC_MISC_MDDR_DL_RST |
67 ESDC_MISC_DDR_EN | ESDC_MISC_DDR2_EN;
68 writel(val, &esdc->esdmisc);
69 val &= ~(ESDC_MISC_RST | ESDC_MISC_MDDR_DL_RST);
70 writel(val, &esdc->esdmisc);
73 * according to DDR2 specs, wait a while before
74 * the PRECHARGE_ALL command
78 /* Load DDR2 config and timing */
79 writel(ddr2_config, cfg_reg);
82 writel(set_mode(ctlval, 1, SMODE_PRECHARGE),
84 writel(0xda, start_address + ESDCTL_PRECHARGE);
87 writel(set_mode(ctlval, 1, SMODE_LOAD_REG),
89 writeb(0xda, start_address + ESDCTL_DDR2_EMR2); /* EMRS2 */
90 writeb(0xda, start_address + ESDCTL_DDR2_EMR3); /* EMRS3 */
91 writeb(0xda, start_address + ESDCTL_DDR2_EN_DLL); /* Enable DLL */
92 writeb(0xda, start_address + ESDCTL_DDR2_RESET_DLL); /* Reset DLL */
95 writel(set_mode(ctlval, 1, SMODE_PRECHARGE),
97 writel(0xda, start_address + ESDCTL_PRECHARGE);
99 /* Set mode auto refresh : at least two refresh are required */
100 writel(set_mode(ctlval, 1, SMODE_AUTO_REFRESH),
102 writel(0xda, start_address);
103 writel(0xda, start_address);
105 writel(set_mode(ctlval, 1, SMODE_LOAD_REG),
107 writeb(0xda, start_address + ESDCTL_DDR2_MR);
108 writeb(0xda, start_address + ESDCTL_DDR2_OCD_DEFAULT);
111 writeb(0xda, start_address + ESDCTL_DDR2_EN_DLL); /* Enable DLL */
113 /* Set normal mode */
114 writel(set_mode(ctlval, 1, SMODE_NORMAL) | refresh,
119 /* Do not set delay lines, only for MDDR */