1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 MediaTek Inc.
5 * Author: Weijie Gao <weijie.gao@mediatek.com>
9 #include <linux/bitops.h>
10 #include <linux/delay.h>
12 #include <linux/sizes.h>
16 #define DDR_BW_TEST_PAT 0xaa5555aa
18 static const u32 dram_size[] = {
23 [DRAM_128MB] = SZ_128M,
24 [DRAM_256MB] = SZ_256M,
27 static void dram_test_write(u32 addr, u32 val)
29 volatile ulong *target = (volatile ulong *)(KSEG1 + addr);
36 static u32 dram_test_read(u32 addr)
38 volatile ulong *target = (volatile ulong *)(KSEG1 + addr);
48 static int dram_addr_test_bit(u32 bit)
52 dram_test_write(0, 0);
53 dram_test_write(BIT(bit), DDR_BW_TEST_PAT);
54 val = dram_test_read(0);
56 if (val == DDR_BW_TEST_PAT)
62 static void mc_ddr_init(void __iomem *memc, const struct mc_ddr_cfg *cfg,
63 u32 dq_dly, u32 dqs_dly, mc_reset_t mc_reset, u32 bw)
71 clrbits_32(memc + MEMCTL_SDRAM_CFG1_REG, RBC_MAPPING);
73 writel(cfg->cfg2, memc + MEMCTL_DDR_CFG2_REG);
74 writel(cfg->cfg3, memc + MEMCTL_DDR_CFG3_REG);
75 writel(cfg->cfg4, memc + MEMCTL_DDR_CFG4_REG);
76 writel(dq_dly, memc + MEMCTL_DDR_DQ_DLY_REG);
77 writel(dqs_dly, memc + MEMCTL_DDR_DQS_DLY_REG);
79 writel(cfg->cfg0, memc + MEMCTL_DDR_CFG0_REG);
83 val &= ~IND_SDRAM_WIDTH_M;
84 val |= (bw << IND_SDRAM_WIDTH_S) & IND_SDRAM_WIDTH_M;
87 writel(val, memc + MEMCTL_DDR_CFG1_REG);
89 clrsetbits_32(memc + MEMCTL_PWR_SAVE_CNT_REG, SR_TAR_CNT_M,
92 setbits_32(memc + MEMCTL_DDR_SELF_REFRESH_REG, SR_AUTO_EN);
95 void ddr1_init(struct mc_ddr_init_param *param)
100 /* First initialization, determine bus width */
101 mc_ddr_init(param->memc, ¶m->cfgs[DRAM_8MB], param->dq_dly,
102 param->dqs_dly, param->mc_reset, IND_SDRAM_WIDTH_16BIT);
105 dram_test_write(0, DDR_BW_TEST_PAT);
106 if (dram_test_read(0) == DDR_BW_TEST_PAT)
107 bw = IND_SDRAM_WIDTH_16BIT;
109 bw = IND_SDRAM_WIDTH_8BIT;
111 /* Second initialization, determine DDR capacity */
112 mc_ddr_init(param->memc, ¶m->cfgs[DRAM_128MB], param->dq_dly,
113 param->dqs_dly, param->mc_reset, bw);
115 if (dram_addr_test_bit(9)) {
118 if (dram_addr_test_bit(10)) {
119 if (dram_addr_test_bit(23))
124 if (dram_addr_test_bit(24))
131 /* Final initialization, with DDR calibration */
132 mc_ddr_init(param->memc, ¶m->cfgs[sz], param->dq_dly,
133 param->dqs_dly, param->mc_reset, bw);
135 /* Return actual DDR configuration */
136 param->memsize = dram_size[sz];
137 param->bus_width = bw;
140 void ddr2_init(struct mc_ddr_init_param *param)
142 enum mc_dram_size sz;
145 /* First initialization, determine bus width */
146 mc_ddr_init(param->memc, ¶m->cfgs[DRAM_32MB], param->dq_dly,
147 param->dqs_dly, param->mc_reset, IND_SDRAM_WIDTH_16BIT);
150 dram_test_write(0, DDR_BW_TEST_PAT);
151 if (dram_test_read(0) == DDR_BW_TEST_PAT)
152 bw = IND_SDRAM_WIDTH_16BIT;
154 bw = IND_SDRAM_WIDTH_8BIT;
156 /* Second initialization, determine DDR capacity */
157 mc_ddr_init(param->memc, ¶m->cfgs[DRAM_256MB], param->dq_dly,
158 param->dqs_dly, param->mc_reset, bw);
160 if (bw == IND_SDRAM_WIDTH_16BIT) {
161 if (dram_addr_test_bit(10)) {
164 if (dram_addr_test_bit(24)) {
165 if (dram_addr_test_bit(27))
174 if (dram_addr_test_bit(23)) {
177 if (dram_addr_test_bit(24)) {
178 if (dram_addr_test_bit(27))
188 /* Final initialization, with DDR calibration */
189 mc_ddr_init(param->memc, ¶m->cfgs[sz], param->dq_dly,
190 param->dqs_dly, param->mc_reset, bw);
192 /* Return actual DDR configuration */
193 param->memsize = dram_size[sz];
194 param->bus_width = bw;