1 // SPDX-License-Identifier: GPL-2.0+
9 #include <dm/device_compat.h>
14 #include <linux/bitops.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/printk.h>
19 /* SDRAM Command Code */
20 #define SD_CC_ARD 0x0 /* Master Bus (AXI) command - Read */
21 #define SD_CC_AWR 0x1 /* Master Bus (AXI) command - Write */
22 #define SD_CC_IRD 0x8 /* IP command - Read */
23 #define SD_CC_IWR 0x9 /* IP command - Write */
24 #define SD_CC_IMS 0xA /* IP command - Set Mode Register */
25 #define SD_CC_IACT 0xB /* IP command - ACTIVE */
26 #define SD_CC_IAF 0xC /* IP command - Auto Refresh */
27 #define SD_CC_ISF 0xD /* IP Command - Self Refresh */
28 #define SD_CC_IPRE 0xE /* IP command - Precharge */
29 #define SD_CC_IPREA 0xF /* IP command - Precharge ALL */
31 #define SEMC_MCR_MDIS BIT(1)
32 #define SEMC_MCR_DQSMD BIT(2)
34 #define SEMC_INTR_IPCMDERR BIT(1)
35 #define SEMC_INTR_IPCMDDONE BIT(0)
37 #define SEMC_IPCMD_KEY 0xA55A0000
39 struct imxrt_semc_regs {
90 #if !defined(TARGET_IMXRT1170_EVK)
91 #define SEMC_IOCR_MUX_A8_SHIFT 0
92 #define SEMC_IOCR_MUX_CSX0_SHIFT 3
93 #define SEMC_IOCR_MUX_CSX1_SHIFT 6
94 #define SEMC_IOCR_MUX_CSX2_SHIFT 9
95 #define SEMC_IOCR_MUX_CSX3_SHIFT 12
96 #define SEMC_IOCR_MUX_RDY_SHIFT 15
98 #define SEMC_IOCR_MUX_A8_SHIFT 0
99 #define SEMC_IOCR_MUX_CSX0_SHIFT 4
100 #define SEMC_IOCR_MUX_CSX1_SHIFT 8
101 #define SEMC_IOCR_MUX_CSX2_SHIFT 12
102 #define SEMC_IOCR_MUX_CSX3_SHIFT 16
103 #define SEMC_IOCR_MUX_RDY_SHIFT 20
106 struct imxrt_sdram_mux {
115 #define SEMC_SDRAMCR0_PS_SHIFT 0
116 #define SEMC_SDRAMCR0_BL_SHIFT 4
117 #define SEMC_SDRAMCR0_COL_SHIFT 8
118 #define SEMC_SDRAMCR0_CL_SHIFT 10
120 struct imxrt_sdram_control {
127 #define SEMC_SDRAMCR1_PRE2ACT_SHIFT 0
128 #define SEMC_SDRAMCR1_ACT2RW_SHIFT 4
129 #define SEMC_SDRAMCR1_RFRC_SHIFT 8
130 #define SEMC_SDRAMCR1_WRC_SHIFT 13
131 #define SEMC_SDRAMCR1_CKEOFF_SHIFT 16
132 #define SEMC_SDRAMCR1_ACT2PRE_SHIFT 20
134 #define SEMC_SDRAMCR2_SRRC_SHIFT 0
135 #define SEMC_SDRAMCR2_REF2REF_SHIFT 8
136 #define SEMC_SDRAMCR2_ACT2ACT_SHIFT 16
137 #define SEMC_SDRAMCR2_ITO_SHIFT 24
139 #define SEMC_SDRAMCR3_REN BIT(0)
140 #define SEMC_SDRAMCR3_REBL_SHIFT 1
141 #define SEMC_SDRAMCR3_PRESCALE_SHIFT 8
142 #define SEMC_SDRAMCR3_RT_SHIFT 16
143 #define SEMC_SDRAMCR3_UT_SHIFT 24
145 struct imxrt_sdram_timing {
164 enum imxrt_semc_bank {
172 #define SEMC_BR_VLD_MASK 1
173 #define SEMC_BR_MS_SHIFT 1
176 enum imxrt_semc_bank target_bank;
181 struct imxrt_sdram_params {
182 struct imxrt_semc_regs *base;
184 struct imxrt_sdram_mux *sdram_mux;
185 struct imxrt_sdram_control *sdram_control;
186 struct imxrt_sdram_timing *sdram_timing;
188 struct bank_params bank_params[MAX_SDRAM_BANK];
192 static int imxrt_sdram_wait_ipcmd_done(struct imxrt_semc_regs *regs)
197 if (regs->intr & SEMC_INTR_IPCMDDONE)
199 if (regs->intr & SEMC_INTR_IPCMDERR)
206 static int imxrt_sdram_ipcmd(struct imxrt_semc_regs *regs, u32 mem_addr,
207 u32 ipcmd, u32 wd, u32 *rd)
211 if (ipcmd == SD_CC_IWR || ipcmd == SD_CC_IMS)
212 writel(wd, ®s->iptxdat);
214 /* set slave address for every command as specified on RM */
215 writel(mem_addr, ®s->ipcr0);
217 /* execute command */
218 writel(SEMC_IPCMD_KEY | ipcmd, ®s->ipcmd);
220 ret = imxrt_sdram_wait_ipcmd_done(regs);
224 if (ipcmd == SD_CC_IRD) {
228 *rd = readl(®s->iprxdat);
234 int imxrt_sdram_init(struct udevice *dev)
236 struct imxrt_sdram_params *params = dev_get_plat(dev);
237 struct imxrt_sdram_mux *mux = params->sdram_mux;
238 struct imxrt_sdram_control *ctrl = params->sdram_control;
239 struct imxrt_sdram_timing *time = params->sdram_timing;
240 struct imxrt_semc_regs *regs = params->base;
241 struct bank_params *bank_params;
245 /* enable the SEMC controller */
246 clrbits_le32(®s->mcr, SEMC_MCR_MDIS);
247 /* set DQS mode from DQS pad */
248 setbits_le32(®s->mcr, SEMC_MCR_DQSMD);
250 for (i = 0, bank_params = params->bank_params;
251 i < params->no_sdram_banks; bank_params++,
253 writel((bank_params->base_address & 0xfffff000)
254 | bank_params->memory_size << SEMC_BR_MS_SHIFT
256 ®s->br[bank_params->target_bank]);
258 writel(mux->a8 << SEMC_IOCR_MUX_A8_SHIFT
259 | mux->csx0 << SEMC_IOCR_MUX_CSX0_SHIFT
260 | mux->csx1 << SEMC_IOCR_MUX_CSX1_SHIFT
261 | mux->csx2 << SEMC_IOCR_MUX_CSX2_SHIFT
262 | mux->csx3 << SEMC_IOCR_MUX_CSX3_SHIFT
263 | mux->rdy << SEMC_IOCR_MUX_RDY_SHIFT,
266 writel(ctrl->memory_width << SEMC_SDRAMCR0_PS_SHIFT
267 | ctrl->burst_len << SEMC_SDRAMCR0_BL_SHIFT
268 | ctrl->no_columns << SEMC_SDRAMCR0_COL_SHIFT
269 | ctrl->cas_latency << SEMC_SDRAMCR0_CL_SHIFT,
272 writel(time->pre2act << SEMC_SDRAMCR1_PRE2ACT_SHIFT
273 | time->act2rw << SEMC_SDRAMCR1_ACT2RW_SHIFT
274 | time->rfrc << SEMC_SDRAMCR1_RFRC_SHIFT
275 | time->wrc << SEMC_SDRAMCR1_WRC_SHIFT
276 | time->ckeoff << SEMC_SDRAMCR1_CKEOFF_SHIFT
277 | time->act2pre << SEMC_SDRAMCR1_ACT2PRE_SHIFT,
280 writel(time->srrc << SEMC_SDRAMCR2_SRRC_SHIFT
281 | time->ref2ref << SEMC_SDRAMCR2_REF2REF_SHIFT
282 | time->act2act << SEMC_SDRAMCR2_ACT2ACT_SHIFT
283 | time->ito << SEMC_SDRAMCR2_ITO_SHIFT,
286 writel(time->rebl << SEMC_SDRAMCR3_REBL_SHIFT
287 | time->prescale << SEMC_SDRAMCR3_PRESCALE_SHIFT
288 | time->rt << SEMC_SDRAMCR3_RT_SHIFT
289 | time->ut << SEMC_SDRAMCR3_UT_SHIFT
293 writel(2, ®s->ipcr1);
295 for (i = 0, bank_params = params->bank_params;
296 i < params->no_sdram_banks; bank_params++,
299 imxrt_sdram_ipcmd(regs, bank_params->base_address, SD_CC_IPREA,
301 imxrt_sdram_ipcmd(regs, bank_params->base_address, SD_CC_IAF,
303 imxrt_sdram_ipcmd(regs, bank_params->base_address, SD_CC_IAF,
305 imxrt_sdram_ipcmd(regs, bank_params->base_address, SD_CC_IMS,
306 ctrl->burst_len | (ctrl->cas_latency << 4),
314 static int imxrt_semc_of_to_plat(struct udevice *dev)
316 struct imxrt_sdram_params *params = dev_get_plat(dev);
321 (struct imxrt_sdram_mux *)
322 dev_read_u8_array_ptr(dev,
324 sizeof(struct imxrt_sdram_mux));
325 if (!params->sdram_mux) {
326 pr_err("fsl,sdram-mux not found");
330 params->sdram_control =
331 (struct imxrt_sdram_control *)
332 dev_read_u8_array_ptr(dev,
334 sizeof(struct imxrt_sdram_control));
335 if (!params->sdram_control) {
336 pr_err("fsl,sdram-control not found");
340 params->sdram_timing =
341 (struct imxrt_sdram_timing *)
342 dev_read_u8_array_ptr(dev,
344 sizeof(struct imxrt_sdram_timing));
345 if (!params->sdram_timing) {
346 pr_err("fsl,sdram-timing not found");
350 dev_for_each_subnode(bank_node, dev) {
351 struct bank_params *bank_params;
355 /* extract the bank index from DT */
356 bank_name = (char *)ofnode_get_name(bank_node);
357 strsep(&bank_name, "@");
359 pr_err("missing sdram bank index");
363 bank_params = ¶ms->bank_params[bank];
364 strict_strtoul(bank_name, 10,
365 (unsigned long *)&bank_params->target_bank);
366 if (bank_params->target_bank >= MAX_SDRAM_BANK) {
367 pr_err("Found bank %d , but only bank 0,1,2,3 are supported",
368 bank_params->target_bank);
372 ret = ofnode_read_u32(bank_node,
374 &bank_params->memory_size);
376 pr_err("fsl,memory-size not found");
380 ret = ofnode_read_u32(bank_node,
382 &bank_params->base_address);
384 pr_err("fsl,base-address not found");
388 debug("Found bank %s %u\n", bank_name,
389 bank_params->target_bank);
393 params->no_sdram_banks = bank;
394 debug("%s, no of banks = %d\n", __func__, params->no_sdram_banks);
399 static int imxrt_semc_probe(struct udevice *dev)
401 struct imxrt_sdram_params *params = dev_get_plat(dev);
405 addr = dev_read_addr(dev);
406 if (addr == FDT_ADDR_T_NONE)
409 params->base = (struct imxrt_semc_regs *)addr;
414 ret = clk_get_by_index(dev, 0, &clk);
418 ret = clk_enable(&clk);
421 dev_err(dev, "failed to enable clock\n");
425 ret = imxrt_sdram_init(dev);
432 static int imxrt_semc_get_info(struct udevice *dev, struct ram_info *info)
437 static struct ram_ops imxrt_semc_ops = {
438 .get_info = imxrt_semc_get_info,
441 static const struct udevice_id imxrt_semc_ids[] = {
442 { .compatible = "fsl,imxrt-semc", .data = 0 },
446 U_BOOT_DRIVER(imxrt_semc) = {
447 .name = "imxrt_semc",
449 .of_match = imxrt_semc_ids,
450 .ops = &imxrt_semc_ops,
451 .of_to_plat = imxrt_semc_of_to_plat,
452 .probe = imxrt_semc_probe,
453 .plat_auto = sizeof(struct imxrt_sdram_params),