blob: 0f1e99eeb039ac2adf5ca5913affdf7ad7349f2b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Kumar Galad5a1fb92008-08-26 21:34:55 -05002/*
3 * Copyright 2008 Freescale Semiconductor, Inc.
Kumar Galad5a1fb92008-08-26 21:34:55 -05004 */
5
6#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Kumar Galad5a1fb92008-08-26 21:34:55 -05008#include <asm/io.h>
York Sunf0626592013-09-30 09:22:09 -07009#include <fsl_ddr_sdram.h>
Simon Glassdbd79542020-05-10 11:40:11 -060010#include <linux/delay.h>
Kumar Galad5a1fb92008-08-26 21:34:55 -050011
12#if (CONFIG_CHIP_SELECTS_PER_CTRL > 4)
13#error Invalid setting for CONFIG_CHIP_SELECTS_PER_CTRL
14#endif
15
16void fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t *regs,
York Sun5e155552013-06-25 11:37:48 -070017 unsigned int ctrl_num, int step)
Kumar Galad5a1fb92008-08-26 21:34:55 -050018{
19 unsigned int i;
York Suna21803d2013-11-18 10:29:32 -080020 struct ccsr_ddr __iomem *ddr =
Tom Rini376b88a2022-10-28 20:27:13 -040021 (struct ccsr_ddr __iomem *)CFG_SYS_FSL_DDR_ADDR;
Kumar Galad5a1fb92008-08-26 21:34:55 -050022
23 if (ctrl_num != 0) {
24 printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
25 return;
26 }
27
28 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
29 if (i == 0) {
30 out_be32(&ddr->cs0_bnds, regs->cs[i].bnds);
31 out_be32(&ddr->cs0_config, regs->cs[i].config);
32
33 } else if (i == 1) {
34 out_be32(&ddr->cs1_bnds, regs->cs[i].bnds);
35 out_be32(&ddr->cs1_config, regs->cs[i].config);
36
37 } else if (i == 2) {
38 out_be32(&ddr->cs2_bnds, regs->cs[i].bnds);
39 out_be32(&ddr->cs2_config, regs->cs[i].config);
40
41 } else if (i == 3) {
42 out_be32(&ddr->cs3_bnds, regs->cs[i].bnds);
43 out_be32(&ddr->cs3_config, regs->cs[i].config);
44 }
45 }
46
47 out_be32(&ddr->timing_cfg_1, regs->timing_cfg_1);
48 out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2);
49 out_be32(&ddr->sdram_mode, regs->ddr_sdram_mode);
50 out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval);
Kumar Galad5a1fb92008-08-26 21:34:55 -050051
52 /*
53 * 200 painful micro-seconds must elapse between
54 * the DDR clock setup and the DDR config enable.
55 */
56 udelay(200);
57 asm volatile("sync;isync");
58
59 out_be32(&ddr->sdram_cfg, regs->ddr_sdram_cfg);
60
61 asm("sync;isync;msync");
62 udelay(500);
63}
64
65#if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
Kumar Galad5a1fb92008-08-26 21:34:55 -050066/*
67 * Initialize all of memory for ECC, then enable errors.
68 */
69
70void
71ddr_enable_ecc(unsigned int dram_size)
72{
York Suna21803d2013-11-18 10:29:32 -080073 struct ccsr_ddr __iomem *ddr =
Tom Rini376b88a2022-10-28 20:27:13 -040074 (struct ccsr_ddr __iomem *)(CFG_SYS_FSL_DDR_ADDR);
Kumar Galad5a1fb92008-08-26 21:34:55 -050075
Peter Tyser4e928b52009-06-30 17:15:48 -050076 dma_meminit(CONFIG_MEM_INIT_VALUE, dram_size);
Kumar Galad5a1fb92008-08-26 21:34:55 -050077
78 /*
79 * Enable errors for ECC.
80 */
81 debug("DMA DDR: err_disable = 0x%08x\n", ddr->err_disable);
82 ddr->err_disable = 0x00000000;
83 asm("sync;isync;msync");
84 debug("DMA DDR: err_disable = 0x%08x\n", ddr->err_disable);
85}
86
87#endif /* CONFIG_DDR_ECC && ! CONFIG_ECC_INIT_VIA_DDRCONTROLLER */