1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2d81b27a2SStefano Babic /*
3d81b27a2SStefano Babic * Copyright (C) 2012, Stefano Babic <sbabic@denx.de>
4d81b27a2SStefano Babic */
5d81b27a2SStefano Babic
6d81b27a2SStefano Babic #include <asm/io.h>
71221ce45SMasahiro Yamada #include <linux/errno.h>
8d81b27a2SStefano Babic #include <asm/arch/imx-regs.h>
9d81b27a2SStefano Babic #include <linux/types.h>
10d81b27a2SStefano Babic #include <asm/arch/sys_proto.h>
11d81b27a2SStefano Babic
12d81b27a2SStefano Babic #define ESDCTL_DDR2_EMR2 0x04000000
13d81b27a2SStefano Babic #define ESDCTL_DDR2_EMR3 0x06000000
14d81b27a2SStefano Babic #define ESDCTL_PRECHARGE 0x00000400
15d81b27a2SStefano Babic #define ESDCTL_DDR2_EN_DLL 0x02000400
16d81b27a2SStefano Babic #define ESDCTL_DDR2_RESET_DLL 0x00000333
17d81b27a2SStefano Babic #define ESDCTL_DDR2_MR 0x00000233
18d81b27a2SStefano Babic #define ESDCTL_DDR2_OCD_DEFAULT 0x02000780
19d81b27a2SStefano Babic
20d81b27a2SStefano Babic enum {
21d81b27a2SStefano Babic SMODE_NORMAL = 0,
22d81b27a2SStefano Babic SMODE_PRECHARGE,
23d81b27a2SStefano Babic SMODE_AUTO_REFRESH,
24d81b27a2SStefano Babic SMODE_LOAD_REG,
25d81b27a2SStefano Babic SMODE_MANUAL_REFRESH
26d81b27a2SStefano Babic };
27d81b27a2SStefano Babic
28d81b27a2SStefano Babic #define set_mode(x, en, m) (x | (en << 31) | (m << 28))
29d81b27a2SStefano Babic
dram_wait(unsigned int count)30d81b27a2SStefano Babic static inline void dram_wait(unsigned int count)
31d81b27a2SStefano Babic {
32d81b27a2SStefano Babic volatile unsigned int wait = count;
33d81b27a2SStefano Babic
34d81b27a2SStefano Babic while (wait--)
35d81b27a2SStefano Babic ;
36d81b27a2SStefano Babic
37d81b27a2SStefano Babic }
38d81b27a2SStefano Babic
mx3_setup_sdram_bank(u32 start_address,u32 ddr2_config,u32 row,u32 col,u32 dsize,u32 refresh)39d81b27a2SStefano Babic void mx3_setup_sdram_bank(u32 start_address, u32 ddr2_config,
40d81b27a2SStefano Babic u32 row, u32 col, u32 dsize, u32 refresh)
41d81b27a2SStefano Babic {
42d81b27a2SStefano Babic struct esdc_regs *esdc = (struct esdc_regs *)ESDCTL_BASE_ADDR;
43d81b27a2SStefano Babic u32 *cfg_reg, *ctl_reg;
44d81b27a2SStefano Babic u32 val;
45d81b27a2SStefano Babic u32 ctlval;
46d81b27a2SStefano Babic
47d81b27a2SStefano Babic switch (start_address) {
48d81b27a2SStefano Babic case CSD0_BASE_ADDR:
49d81b27a2SStefano Babic cfg_reg = &esdc->esdcfg0;
50d81b27a2SStefano Babic ctl_reg = &esdc->esdctl0;
51d81b27a2SStefano Babic break;
52d81b27a2SStefano Babic case CSD1_BASE_ADDR:
53d81b27a2SStefano Babic cfg_reg = &esdc->esdcfg1;
54d81b27a2SStefano Babic ctl_reg = &esdc->esdctl1;
55d81b27a2SStefano Babic break;
56d81b27a2SStefano Babic default:
57d81b27a2SStefano Babic return;
58d81b27a2SStefano Babic }
59d81b27a2SStefano Babic
60d81b27a2SStefano Babic /* The MX35 supports 11 up to 14 rows */
61d81b27a2SStefano Babic if (row < 11 || row > 14 || col < 8 || col > 10)
62d81b27a2SStefano Babic return;
63d81b27a2SStefano Babic ctlval = (row - 11) << 24 | (col - 8) << 20 | (dsize << 16);
64d81b27a2SStefano Babic
65d81b27a2SStefano Babic /* Initialize MISC register for DDR2 */
66d81b27a2SStefano Babic val = ESDC_MISC_RST | ESDC_MISC_MDDR_EN | ESDC_MISC_MDDR_DL_RST |
67d81b27a2SStefano Babic ESDC_MISC_DDR_EN | ESDC_MISC_DDR2_EN;
68d81b27a2SStefano Babic writel(val, &esdc->esdmisc);
69d81b27a2SStefano Babic val &= ~(ESDC_MISC_RST | ESDC_MISC_MDDR_DL_RST);
70d81b27a2SStefano Babic writel(val, &esdc->esdmisc);
71d81b27a2SStefano Babic
72d81b27a2SStefano Babic /*
73d81b27a2SStefano Babic * according to DDR2 specs, wait a while before
74d81b27a2SStefano Babic * the PRECHARGE_ALL command
75d81b27a2SStefano Babic */
76d81b27a2SStefano Babic dram_wait(0x20000);
77d81b27a2SStefano Babic
78d81b27a2SStefano Babic /* Load DDR2 config and timing */
79d81b27a2SStefano Babic writel(ddr2_config, cfg_reg);
80d81b27a2SStefano Babic
81d81b27a2SStefano Babic /* Precharge ALL */
82d81b27a2SStefano Babic writel(set_mode(ctlval, 1, SMODE_PRECHARGE),
83d81b27a2SStefano Babic ctl_reg);
84d81b27a2SStefano Babic writel(0xda, start_address + ESDCTL_PRECHARGE);
85d81b27a2SStefano Babic
86d81b27a2SStefano Babic /* Load mode */
87d81b27a2SStefano Babic writel(set_mode(ctlval, 1, SMODE_LOAD_REG),
88d81b27a2SStefano Babic ctl_reg);
89d81b27a2SStefano Babic writeb(0xda, start_address + ESDCTL_DDR2_EMR2); /* EMRS2 */
90d81b27a2SStefano Babic writeb(0xda, start_address + ESDCTL_DDR2_EMR3); /* EMRS3 */
91d81b27a2SStefano Babic writeb(0xda, start_address + ESDCTL_DDR2_EN_DLL); /* Enable DLL */
92d81b27a2SStefano Babic writeb(0xda, start_address + ESDCTL_DDR2_RESET_DLL); /* Reset DLL */
93d81b27a2SStefano Babic
94d81b27a2SStefano Babic /* Precharge ALL */
95d81b27a2SStefano Babic writel(set_mode(ctlval, 1, SMODE_PRECHARGE),
96d81b27a2SStefano Babic ctl_reg);
97d81b27a2SStefano Babic writel(0xda, start_address + ESDCTL_PRECHARGE);
98d81b27a2SStefano Babic
99d81b27a2SStefano Babic /* Set mode auto refresh : at least two refresh are required */
100d81b27a2SStefano Babic writel(set_mode(ctlval, 1, SMODE_AUTO_REFRESH),
101d81b27a2SStefano Babic ctl_reg);
102d81b27a2SStefano Babic writel(0xda, start_address);
103d81b27a2SStefano Babic writel(0xda, start_address);
104d81b27a2SStefano Babic
105d81b27a2SStefano Babic writel(set_mode(ctlval, 1, SMODE_LOAD_REG),
106d81b27a2SStefano Babic ctl_reg);
107d81b27a2SStefano Babic writeb(0xda, start_address + ESDCTL_DDR2_MR);
108d81b27a2SStefano Babic writeb(0xda, start_address + ESDCTL_DDR2_OCD_DEFAULT);
109d81b27a2SStefano Babic
110d81b27a2SStefano Babic /* OCD mode exit */
111d81b27a2SStefano Babic writeb(0xda, start_address + ESDCTL_DDR2_EN_DLL); /* Enable DLL */
112d81b27a2SStefano Babic
113d81b27a2SStefano Babic /* Set normal mode */
114d81b27a2SStefano Babic writel(set_mode(ctlval, 1, SMODE_NORMAL) | refresh,
115d81b27a2SStefano Babic ctl_reg);
116d81b27a2SStefano Babic
117d81b27a2SStefano Babic dram_wait(0x20000);
118d81b27a2SStefano Babic
119d81b27a2SStefano Babic /* Do not set delay lines, only for MDDR */
120d81b27a2SStefano Babic }
121