blob: 40298112ad78fe1d26c7d02d161662a41c8b9f75 [file] [log] [blame]
Vikas Manocha24796092017-04-10 15:02:51 -07001/*
2 * (C) Copyright 2017
3 * Vikas Manocha, <vikas.manocha@st.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
Vikas Manochaf9c6e6a2017-04-10 15:02:55 -07009#include <clk.h>
Vikas Manochaaa88e1a2017-04-10 15:02:52 -070010#include <dm.h>
11#include <ram.h>
Vikas Manocha24796092017-04-10 15:02:51 -070012#include <asm/io.h>
Vikas Manocha24796092017-04-10 15:02:51 -070013
Vikas Manocha4515ffe2017-04-10 15:02:56 -070014DECLARE_GLOBAL_DATA_PTR;
15
Patrice Chotard7e82a692017-07-18 17:37:24 +020016struct stm32_fmc_regs {
Patrice Chotardf2b80002017-07-18 17:37:25 +020017 /* 0x0 */
18 u32 bcr1; /* NOR/PSRAM Chip select control register 1 */
19 u32 btr1; /* SRAM/NOR-Flash Chip select timing register 1 */
20 u32 bcr2; /* NOR/PSRAM Chip select Control register 2 */
21 u32 btr2; /* SRAM/NOR-Flash Chip select timing register 2 */
22 u32 bcr3; /* NOR/PSRAMChip select Control register 3 */
23 u32 btr3; /* SRAM/NOR-Flash Chip select timing register 3 */
24 u32 bcr4; /* NOR/PSRAM Chip select Control register 4 */
25 u32 btr4; /* SRAM/NOR-Flash Chip select timing register 4 */
26 u32 reserved1[24];
27
28 /* 0x80 */
29 u32 pcr; /* NAND Flash control register */
30 u32 sr; /* FIFO status and interrupt register */
31 u32 pmem; /* Common memory space timing register */
32 u32 patt; /* Attribute memory space timing registers */
33 u32 reserved2[1];
34 u32 eccr; /* ECC result registers */
35 u32 reserved3[27];
Patrice Chotard7e82a692017-07-18 17:37:24 +020036
Patrice Chotardf2b80002017-07-18 17:37:25 +020037 /* 0x104 */
38 u32 bwtr1; /* SRAM/NOR-Flash write timing register 1 */
39 u32 reserved4[1];
40 u32 bwtr2; /* SRAM/NOR-Flash write timing register 2 */
41 u32 reserved5[1];
42 u32 bwtr3; /* SRAM/NOR-Flash write timing register 3 */
43 u32 reserved6[1];
44 u32 bwtr4; /* SRAM/NOR-Flash write timing register 4 */
45 u32 reserved7[8];
46
47 /* 0x140 */
48 u32 sdcr1; /* SDRAM Control register 1 */
49 u32 sdcr2; /* SDRAM Control register 2 */
50 u32 sdtr1; /* SDRAM Timing register 1 */
51 u32 sdtr2; /* SDRAM Timing register 2 */
52 u32 sdcmr; /* SDRAM Mode register */
53 u32 sdrtr; /* SDRAM Refresh timing register */
54 u32 sdsr; /* SDRAM Status register */
55};
Patrice Chotard7e82a692017-07-18 17:37:24 +020056
57/* Control register SDCR */
58#define FMC_SDCR_RPIPE_SHIFT 13 /* RPIPE bit shift */
59#define FMC_SDCR_RBURST_SHIFT 12 /* RBURST bit shift */
60#define FMC_SDCR_SDCLK_SHIFT 10 /* SDRAM clock divisor shift */
61#define FMC_SDCR_WP_SHIFT 9 /* Write protection shift */
62#define FMC_SDCR_CAS_SHIFT 7 /* CAS latency shift */
63#define FMC_SDCR_NB_SHIFT 6 /* Number of banks shift */
64#define FMC_SDCR_MWID_SHIFT 4 /* Memory width shift */
65#define FMC_SDCR_NR_SHIFT 2 /* Number of row address bits shift */
66#define FMC_SDCR_NC_SHIFT 0 /* Number of col address bits shift */
67
68/* Timings register SDTR */
69#define FMC_SDTR_TMRD_SHIFT 0 /* Load mode register to active */
70#define FMC_SDTR_TXSR_SHIFT 4 /* Exit self-refresh time */
71#define FMC_SDTR_TRAS_SHIFT 8 /* Self-refresh time */
72#define FMC_SDTR_TRC_SHIFT 12 /* Row cycle delay */
73#define FMC_SDTR_TWR_SHIFT 16 /* Recovery delay */
74#define FMC_SDTR_TRP_SHIFT 20 /* Row precharge delay */
75#define FMC_SDTR_TRCD_SHIFT 24 /* Row-to-column delay */
76
77#define FMC_SDCMR_NRFS_SHIFT 5
78
79#define FMC_SDCMR_MODE_NORMAL 0
80#define FMC_SDCMR_MODE_START_CLOCK 1
81#define FMC_SDCMR_MODE_PRECHARGE 2
82#define FMC_SDCMR_MODE_AUTOREFRESH 3
83#define FMC_SDCMR_MODE_WRITE_MODE 4
84#define FMC_SDCMR_MODE_SELFREFRESH 5
85#define FMC_SDCMR_MODE_POWERDOWN 6
86
87#define FMC_SDCMR_BANK_1 BIT(4)
88#define FMC_SDCMR_BANK_2 BIT(3)
89
90#define FMC_SDCMR_MODE_REGISTER_SHIFT 9
91
92#define FMC_SDSR_BUSY BIT(5)
93
Patrice Chotardf2b80002017-07-18 17:37:25 +020094#define FMC_BUSY_WAIT(regs) do { \
Patrice Chotard7e82a692017-07-18 17:37:24 +020095 __asm__ __volatile__ ("dsb" : : : "memory"); \
Patrice Chotardf2b80002017-07-18 17:37:25 +020096 while (regs->sdsr & FMC_SDSR_BUSY) \
Patrice Chotard7e82a692017-07-18 17:37:24 +020097 ; \
98 } while (0)
99
Vikas Manocha4515ffe2017-04-10 15:02:56 -0700100struct stm32_sdram_control {
101 u8 no_columns;
102 u8 no_rows;
103 u8 memory_width;
104 u8 no_banks;
105 u8 cas_latency;
Vikas Manocha1c6459d2017-04-10 15:03:03 -0700106 u8 sdclk;
Vikas Manocha4515ffe2017-04-10 15:02:56 -0700107 u8 rd_burst;
108 u8 rd_pipe_delay;
109};
110
111struct stm32_sdram_timing {
112 u8 tmrd;
113 u8 txsr;
114 u8 tras;
115 u8 trc;
116 u8 trp;
Vikas Manocha1c6459d2017-04-10 15:03:03 -0700117 u8 twr;
Vikas Manocha4515ffe2017-04-10 15:02:56 -0700118 u8 trcd;
119};
120struct stm32_sdram_params {
Patrice Chotardf2b80002017-07-18 17:37:25 +0200121 struct stm32_fmc_regs *base;
Vikas Manocha4515ffe2017-04-10 15:02:56 -0700122 u8 no_sdram_banks;
Patrice Chotard8b379222017-07-18 17:37:26 +0200123 struct stm32_sdram_control *sdram_control;
124 struct stm32_sdram_timing *sdram_timing;
Vikas Manocha1c6459d2017-04-10 15:03:03 -0700125 u32 sdram_ref_count;
Vikas Manocha4515ffe2017-04-10 15:02:56 -0700126};
Vikas Manocha24796092017-04-10 15:02:51 -0700127
128#define SDRAM_MODE_BL_SHIFT 0
129#define SDRAM_MODE_CAS_SHIFT 4
130#define SDRAM_MODE_BL 0
Vikas Manocha4515ffe2017-04-10 15:02:56 -0700131
132int stm32_sdram_init(struct udevice *dev)
Vikas Manocha24796092017-04-10 15:02:51 -0700133{
Vikas Manocha4515ffe2017-04-10 15:02:56 -0700134 struct stm32_sdram_params *params = dev_get_platdata(dev);
Patrice Chotardf2b80002017-07-18 17:37:25 +0200135 struct stm32_fmc_regs *regs = params->base;
Patrice Chotard8b379222017-07-18 17:37:26 +0200136 struct stm32_sdram_control *control = params->sdram_control;
137 struct stm32_sdram_timing *timing = params->sdram_timing;
Vikas Manocha24796092017-04-10 15:02:51 -0700138
Patrice Chotard8b379222017-07-18 17:37:26 +0200139 writel(control->sdclk << FMC_SDCR_SDCLK_SHIFT
140 | control->cas_latency << FMC_SDCR_CAS_SHIFT
141 | control->no_banks << FMC_SDCR_NB_SHIFT
142 | control->memory_width << FMC_SDCR_MWID_SHIFT
143 | control->no_rows << FMC_SDCR_NR_SHIFT
144 | control->no_columns << FMC_SDCR_NC_SHIFT
145 | control->rd_pipe_delay << FMC_SDCR_RPIPE_SHIFT
146 | control->rd_burst << FMC_SDCR_RBURST_SHIFT,
Patrice Chotardf2b80002017-07-18 17:37:25 +0200147 &regs->sdcr1);
Vikas Manocha24796092017-04-10 15:02:51 -0700148
Patrice Chotard8b379222017-07-18 17:37:26 +0200149 writel(timing->trcd << FMC_SDTR_TRCD_SHIFT
150 | timing->trp << FMC_SDTR_TRP_SHIFT
151 | timing->twr << FMC_SDTR_TWR_SHIFT
152 | timing->trc << FMC_SDTR_TRC_SHIFT
153 | timing->tras << FMC_SDTR_TRAS_SHIFT
154 | timing->txsr << FMC_SDTR_TXSR_SHIFT
155 | timing->tmrd << FMC_SDTR_TMRD_SHIFT,
Patrice Chotardf2b80002017-07-18 17:37:25 +0200156 &regs->sdtr1);
Vikas Manocha24796092017-04-10 15:02:51 -0700157
158 writel(FMC_SDCMR_BANK_1 | FMC_SDCMR_MODE_START_CLOCK,
Patrice Chotardf2b80002017-07-18 17:37:25 +0200159 &regs->sdcmr);
Vikas Manocha24796092017-04-10 15:02:51 -0700160 udelay(200); /* 200 us delay, page 10, "Power-Up" */
Patrice Chotardf2b80002017-07-18 17:37:25 +0200161 FMC_BUSY_WAIT(regs);
Vikas Manocha24796092017-04-10 15:02:51 -0700162
163 writel(FMC_SDCMR_BANK_1 | FMC_SDCMR_MODE_PRECHARGE,
Patrice Chotardf2b80002017-07-18 17:37:25 +0200164 &regs->sdcmr);
Vikas Manocha24796092017-04-10 15:02:51 -0700165 udelay(100);
Patrice Chotardf2b80002017-07-18 17:37:25 +0200166 FMC_BUSY_WAIT(regs);
Vikas Manocha24796092017-04-10 15:02:51 -0700167
168 writel((FMC_SDCMR_BANK_1 | FMC_SDCMR_MODE_AUTOREFRESH
Patrice Chotardf2b80002017-07-18 17:37:25 +0200169 | 7 << FMC_SDCMR_NRFS_SHIFT), &regs->sdcmr);
Vikas Manocha24796092017-04-10 15:02:51 -0700170 udelay(100);
Patrice Chotardf2b80002017-07-18 17:37:25 +0200171 FMC_BUSY_WAIT(regs);
Vikas Manocha24796092017-04-10 15:02:51 -0700172
173 writel(FMC_SDCMR_BANK_1 | (SDRAM_MODE_BL << SDRAM_MODE_BL_SHIFT
Patrice Chotard8b379222017-07-18 17:37:26 +0200174 | control->cas_latency << SDRAM_MODE_CAS_SHIFT)
Vikas Manocha24796092017-04-10 15:02:51 -0700175 << FMC_SDCMR_MODE_REGISTER_SHIFT | FMC_SDCMR_MODE_WRITE_MODE,
Patrice Chotardf2b80002017-07-18 17:37:25 +0200176 &regs->sdcmr);
Vikas Manocha24796092017-04-10 15:02:51 -0700177 udelay(100);
Patrice Chotardf2b80002017-07-18 17:37:25 +0200178 FMC_BUSY_WAIT(regs);
Vikas Manocha24796092017-04-10 15:02:51 -0700179
180 writel(FMC_SDCMR_BANK_1 | FMC_SDCMR_MODE_NORMAL,
Patrice Chotardf2b80002017-07-18 17:37:25 +0200181 &regs->sdcmr);
182 FMC_BUSY_WAIT(regs);
Vikas Manocha24796092017-04-10 15:02:51 -0700183
184 /* Refresh timer */
Patrice Chotardf2b80002017-07-18 17:37:25 +0200185 writel((params->sdram_ref_count) << 1, &regs->sdrtr);
Vikas Manocha24796092017-04-10 15:02:51 -0700186
187 return 0;
188}
Vikas Manochaaa88e1a2017-04-10 15:02:52 -0700189
Vikas Manocha4515ffe2017-04-10 15:02:56 -0700190static int stm32_fmc_ofdata_to_platdata(struct udevice *dev)
191{
Patrice Chotard8b379222017-07-18 17:37:26 +0200192 ofnode bank_node;
Vikas Manocha4515ffe2017-04-10 15:02:56 -0700193 struct stm32_sdram_params *params = dev_get_platdata(dev);
194
Patrice Chotard8b379222017-07-18 17:37:26 +0200195 params->no_sdram_banks = dev_read_u32_default(dev, "mr-nbanks", 1);
Vikas Manocha4515ffe2017-04-10 15:02:56 -0700196 debug("%s, no of banks = %d\n", __func__, params->no_sdram_banks);
197
Patrice Chotard8b379222017-07-18 17:37:26 +0200198 dev_for_each_subnode(bank_node, dev) {
199 params->sdram_control = (struct stm32_sdram_control *)
200 ofnode_read_u8_array_ptr(bank_node,
201 "st,sdram-control",
202 sizeof(struct stm32_sdram_control));
203
204 if (!params->sdram_control) {
205 error("st,sdram-control not found for device: %s",
206 dev->name);
207 return -EINVAL;
208 }
209
210 params->sdram_timing = (struct stm32_sdram_timing *)
211 ofnode_read_u8_array_ptr(bank_node,
212 "st,sdram-timing",
213 sizeof(struct stm32_sdram_timing));
214
215 if (!params->sdram_timing) {
216 error("st,sdram-timing not found for device: %s",
217 dev->name);
218 return -EINVAL;
219 }
Vikas Manocha1c6459d2017-04-10 15:03:03 -0700220
Patrice Chotard8b379222017-07-18 17:37:26 +0200221 params->sdram_ref_count = ofnode_read_u32_default(bank_node,
Vikas Manocha1c6459d2017-04-10 15:03:03 -0700222 "st,sdram-refcount", 8196);
Vikas Manocha4515ffe2017-04-10 15:02:56 -0700223 }
224
225 return 0;
226}
227
Vikas Manochaaa88e1a2017-04-10 15:02:52 -0700228static int stm32_fmc_probe(struct udevice *dev)
229{
Patrice Chotardf2b80002017-07-18 17:37:25 +0200230 struct stm32_sdram_params *params = dev_get_platdata(dev);
Vikas Manochaf9c6e6a2017-04-10 15:02:55 -0700231 int ret;
Patrice Chotardf2b80002017-07-18 17:37:25 +0200232 fdt_addr_t addr;
233
234 addr = dev_read_addr(dev);
235 if (addr == FDT_ADDR_T_NONE)
236 return -EINVAL;
237
238 params->base = (struct stm32_fmc_regs *)addr;
239
Patrice Chotard4fafb722017-05-30 15:06:31 +0200240#ifdef CONFIG_CLK
Vikas Manochaf9c6e6a2017-04-10 15:02:55 -0700241 struct clk clk;
Vikas Manocha4515ffe2017-04-10 15:02:56 -0700242
Vikas Manochaf9c6e6a2017-04-10 15:02:55 -0700243 ret = clk_get_by_index(dev, 0, &clk);
244 if (ret < 0)
245 return ret;
246
247 ret = clk_enable(&clk);
248
249 if (ret) {
250 dev_err(dev, "failed to enable clock\n");
251 return ret;
252 }
253#endif
Vikas Manocha4515ffe2017-04-10 15:02:56 -0700254 ret = stm32_sdram_init(dev);
255 if (ret)
256 return ret;
257
Vikas Manochaaa88e1a2017-04-10 15:02:52 -0700258 return 0;
259}
260
261static int stm32_fmc_get_info(struct udevice *dev, struct ram_info *info)
262{
Vikas Manochaaa88e1a2017-04-10 15:02:52 -0700263 return 0;
264}
265
266static struct ram_ops stm32_fmc_ops = {
267 .get_info = stm32_fmc_get_info,
268};
269
270static const struct udevice_id stm32_fmc_ids[] = {
271 { .compatible = "st,stm32-fmc" },
272 { }
273};
274
275U_BOOT_DRIVER(stm32_fmc) = {
276 .name = "stm32_fmc",
277 .id = UCLASS_RAM,
278 .of_match = stm32_fmc_ids,
279 .ops = &stm32_fmc_ops,
Vikas Manocha4515ffe2017-04-10 15:02:56 -0700280 .ofdata_to_platdata = stm32_fmc_ofdata_to_platdata,
Vikas Manochaaa88e1a2017-04-10 15:02:52 -0700281 .probe = stm32_fmc_probe,
Vikas Manocha4515ffe2017-04-10 15:02:56 -0700282 .platdata_auto_alloc_size = sizeof(struct stm32_sdram_params),
Vikas Manochaaa88e1a2017-04-10 15:02:52 -0700283};