3 * Vikas Manocha, <vikas.manocha@st.com>
5 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/arch/fmc.h>
14 #include <asm/arch/stm32.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 struct stm32_sdram_control {
29 struct stm32_sdram_timing {
38 struct stm32_sdram_params {
40 struct stm32_sdram_control sdram_control;
41 struct stm32_sdram_timing sdram_timing;
45 #define SDRAM_MODE_BL_SHIFT 0
46 #define SDRAM_MODE_CAS_SHIFT 4
47 #define SDRAM_MODE_BL 0
49 int stm32_sdram_init(struct udevice *dev)
51 struct stm32_sdram_params *params = dev_get_platdata(dev);
53 writel(params->sdram_control.sdclk << FMC_SDCR_SDCLK_SHIFT
54 | params->sdram_control.cas_latency << FMC_SDCR_CAS_SHIFT
55 | params->sdram_control.no_banks << FMC_SDCR_NB_SHIFT
56 | params->sdram_control.memory_width << FMC_SDCR_MWID_SHIFT
57 | params->sdram_control.no_rows << FMC_SDCR_NR_SHIFT
58 | params->sdram_control.no_columns << FMC_SDCR_NC_SHIFT
59 | params->sdram_control.rd_pipe_delay << FMC_SDCR_RPIPE_SHIFT
60 | params->sdram_control.rd_burst << FMC_SDCR_RBURST_SHIFT,
61 &STM32_SDRAM_FMC->sdcr1);
63 writel(params->sdram_timing.trcd << FMC_SDTR_TRCD_SHIFT
64 | params->sdram_timing.trp << FMC_SDTR_TRP_SHIFT
65 | params->sdram_timing.twr << FMC_SDTR_TWR_SHIFT
66 | params->sdram_timing.trc << FMC_SDTR_TRC_SHIFT
67 | params->sdram_timing.tras << FMC_SDTR_TRAS_SHIFT
68 | params->sdram_timing.txsr << FMC_SDTR_TXSR_SHIFT
69 | params->sdram_timing.tmrd << FMC_SDTR_TMRD_SHIFT,
70 &STM32_SDRAM_FMC->sdtr1);
72 writel(FMC_SDCMR_BANK_1 | FMC_SDCMR_MODE_START_CLOCK,
73 &STM32_SDRAM_FMC->sdcmr);
74 udelay(200); /* 200 us delay, page 10, "Power-Up" */
77 writel(FMC_SDCMR_BANK_1 | FMC_SDCMR_MODE_PRECHARGE,
78 &STM32_SDRAM_FMC->sdcmr);
82 writel((FMC_SDCMR_BANK_1 | FMC_SDCMR_MODE_AUTOREFRESH
83 | 7 << FMC_SDCMR_NRFS_SHIFT), &STM32_SDRAM_FMC->sdcmr);
87 writel(FMC_SDCMR_BANK_1 | (SDRAM_MODE_BL << SDRAM_MODE_BL_SHIFT
88 | params->sdram_control.cas_latency << SDRAM_MODE_CAS_SHIFT)
89 << FMC_SDCMR_MODE_REGISTER_SHIFT | FMC_SDCMR_MODE_WRITE_MODE,
90 &STM32_SDRAM_FMC->sdcmr);
94 writel(FMC_SDCMR_BANK_1 | FMC_SDCMR_MODE_NORMAL,
95 &STM32_SDRAM_FMC->sdcmr);
99 writel((params->sdram_ref_count) << 1, &STM32_SDRAM_FMC->sdrtr);
104 static int stm32_fmc_ofdata_to_platdata(struct udevice *dev)
107 int node = dev_of_offset(dev);
108 const void *blob = gd->fdt_blob;
109 struct stm32_sdram_params *params = dev_get_platdata(dev);
111 params->no_sdram_banks = fdtdec_get_uint(blob, node, "mr-nbanks", 1);
112 debug("%s, no of banks = %d\n", __func__, params->no_sdram_banks);
114 fdt_for_each_subnode(node, blob, node) {
115 ret = fdtdec_get_byte_array(blob, node, "st,sdram-control",
116 (u8 *)¶ms->sdram_control,
117 sizeof(params->sdram_control));
120 ret = fdtdec_get_byte_array(blob, node, "st,sdram-timing",
121 (u8 *)¶ms->sdram_timing,
122 sizeof(params->sdram_timing));
126 params->sdram_ref_count = fdtdec_get_int(blob, node,
127 "st,sdram-refcount", 8196);
133 static int stm32_fmc_probe(struct udevice *dev)
139 ret = clk_get_by_index(dev, 0, &clk);
143 ret = clk_enable(&clk);
146 dev_err(dev, "failed to enable clock\n");
150 ret = stm32_sdram_init(dev);
157 static int stm32_fmc_get_info(struct udevice *dev, struct ram_info *info)
162 static struct ram_ops stm32_fmc_ops = {
163 .get_info = stm32_fmc_get_info,
166 static const struct udevice_id stm32_fmc_ids[] = {
167 { .compatible = "st,stm32-fmc" },
171 U_BOOT_DRIVER(stm32_fmc) = {
174 .of_match = stm32_fmc_ids,
175 .ops = &stm32_fmc_ops,
176 .ofdata_to_platdata = stm32_fmc_ofdata_to_platdata,
177 .probe = stm32_fmc_probe,
178 .platdata_auto_alloc_size = sizeof(struct stm32_sdram_params),