blob: af8c1427d2edd02b3ccf5728a89247a01e5fd550 [file] [log] [blame]
Jacky Baid62ddc12019-08-08 09:59:08 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2018-2019 NXP
4 */
5
6#include <common.h>
7#include <errno.h>
8#include <asm/io.h>
9#include <asm/arch/ddr.h>
10#include <asm/arch/clock.h>
11#include <asm/arch/sys_proto.h>
12
13void ddr_cfg_umctl2(struct dram_cfg_param *ddrc_cfg, int num)
14{
15 int i = 0;
16
17 for (i = 0; i < num; i++) {
18 reg32_write(ddrc_cfg->reg, ddrc_cfg->val);
19 ddrc_cfg++;
20 }
21}
22
Frieder Schrempf2a9b1f52019-12-11 10:01:19 +000023int ddr_init(struct dram_timing_info *dram_timing)
Jacky Baid62ddc12019-08-08 09:59:08 +000024{
25 unsigned int tmp, initial_drate, target_freq;
Frieder Schrempf2a9b1f52019-12-11 10:01:19 +000026 int ret;
Jacky Baid62ddc12019-08-08 09:59:08 +000027
Fabio Estevam9f7ee332019-12-11 17:37:09 -030028 debug("DDRINFO: start DRAM init\n");
Jacky Baid62ddc12019-08-08 09:59:08 +000029
30 /* Step1: Follow the power up procedure */
31 if (is_imx8mq()) {
32 reg32_write(SRC_DDRC_RCR_ADDR + 0x04, 0x8F00000F);
33 reg32_write(SRC_DDRC_RCR_ADDR, 0x8F00000F);
34 reg32_write(SRC_DDRC_RCR_ADDR + 0x04, 0x8F000000);
35 } else {
36 reg32_write(SRC_DDRC_RCR_ADDR, 0x8F00001F);
37 reg32_write(SRC_DDRC_RCR_ADDR, 0x8F00000F);
38 }
39
40 debug("DDRINFO: cfg clk\n");
41 /* change the clock source of dram_apb_clk_root: source 4 800MHz /4 = 200MHz */
42 clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON | CLK_ROOT_SOURCE_SEL(4) |
43 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV4));
44
Jacky Baid62ddc12019-08-08 09:59:08 +000045 /* disable iso */
46 reg32_write(0x303A00EC, 0x0000ffff); /* PGC_CPU_MAPPING */
47 reg32setbit(0x303A00F8, 5); /* PU_PGC_SW_PUP_REQ */
48
Jacky Bai828d41b2019-08-08 09:59:11 +000049 initial_drate = dram_timing->fsp_msg[0].drate;
50 /* default to the frequency point 0 clock */
51 ddrphy_init_set_dfi_clk(initial_drate);
52
Jacky Baid62ddc12019-08-08 09:59:08 +000053 /* D-aasert the presetn */
54 reg32_write(SRC_DDRC_RCR_ADDR, 0x8F000006);
55
56 /* Step2: Program the dwc_ddr_umctl2 registers */
57 debug("DDRINFO: ddrc config start\n");
58 ddr_cfg_umctl2(dram_timing->ddrc_cfg, dram_timing->ddrc_cfg_num);
59 debug("DDRINFO: ddrc config done\n");
60
61 /* Step3: De-assert reset signal(core_ddrc_rstn & aresetn_n) */
62 reg32_write(SRC_DDRC_RCR_ADDR, 0x8F000004);
63 reg32_write(SRC_DDRC_RCR_ADDR, 0x8F000000);
64
65 /*
66 * Step4: Disable auto-refreshes, self-refresh, powerdown, and
67 * assertion of dfi_dram_clk_disable by setting RFSHCTL3.dis_auto_refresh = 1,
68 * PWRCTL.powerdown_en = 0, and PWRCTL.selfref_en = 0, PWRCTL.en_dfi_dram_clk_disable = 0
69 */
70 reg32_write(DDRC_DBG1(0), 0x00000000);
71 reg32_write(DDRC_RFSHCTL3(0), 0x0000001);
72 reg32_write(DDRC_PWRCTL(0), 0xa0);
73
74 /* if ddr type is LPDDR4, do it */
75 tmp = reg32_read(DDRC_MSTR(0));
76 if (tmp & (0x1 << 5))
77 reg32_write(DDRC_DDR_SS_GPR0, 0x01); /* LPDDR4 mode */
78
79 /* determine the initial boot frequency */
80 target_freq = reg32_read(DDRC_MSTR2(0)) & 0x3;
81 target_freq = (tmp & (0x1 << 29)) ? target_freq : 0x0;
82
83 /* Step5: Set SWCT.sw_done to 0 */
84 reg32_write(DDRC_SWCTL(0), 0x00000000);
85
86 /* Set the default boot frequency point */
87 clrsetbits_le32(DDRC_DFIMISC(0), (0x1f << 8), target_freq << 8);
88 /* Step6: Set DFIMISC.dfi_init_complete_en to 0 */
89 clrbits_le32(DDRC_DFIMISC(0), 0x1);
90
91 /* Step7: Set SWCTL.sw_done to 1; need to polling SWSTAT.sw_done_ack */
92 reg32_write(DDRC_SWCTL(0), 0x00000001);
93 do {
94 tmp = reg32_read(DDRC_SWSTAT(0));
95 } while ((tmp & 0x1) == 0x0);
96
97 /*
98 * Step8 ~ Step13: Start PHY initialization and training by
99 * accessing relevant PUB registers
100 */
101 debug("DDRINFO:ddrphy config start\n");
Frieder Schrempf2a9b1f52019-12-11 10:01:19 +0000102
103 ret = ddr_cfg_phy(dram_timing);
104 if (ret)
105 return ret;
106
Jacky Baid62ddc12019-08-08 09:59:08 +0000107 debug("DDRINFO: ddrphy config done\n");
108
109 /*
110 * step14 CalBusy.0 =1, indicates the calibrator is actively
111 * calibrating. Wait Calibrating done.
112 */
113 do {
114 tmp = reg32_read(DDRPHY_CalBusy(0));
115 } while ((tmp & 0x1));
116
Fabio Estevam9f7ee332019-12-11 17:37:09 -0300117 debug("DDRINFO:ddrphy calibration done\n");
Jacky Baid62ddc12019-08-08 09:59:08 +0000118
119 /* Step15: Set SWCTL.sw_done to 0 */
120 reg32_write(DDRC_SWCTL(0), 0x00000000);
121
122 /* Step16: Set DFIMISC.dfi_init_start to 1 */
123 setbits_le32(DDRC_DFIMISC(0), (0x1 << 5));
124
125 /* Step17: Set SWCTL.sw_done to 1; need to polling SWSTAT.sw_done_ack */
126 reg32_write(DDRC_SWCTL(0), 0x00000001);
127 do {
128 tmp = reg32_read(DDRC_SWSTAT(0));
129 } while ((tmp & 0x1) == 0x0);
130
131 /* Step18: Polling DFISTAT.dfi_init_complete = 1 */
132 do {
133 tmp = reg32_read(DDRC_DFISTAT(0));
134 } while ((tmp & 0x1) == 0x0);
135
136 /* Step19: Set SWCTL.sw_done to 0 */
137 reg32_write(DDRC_SWCTL(0), 0x00000000);
138
139 /* Step20: Set DFIMISC.dfi_init_start to 0 */
140 clrbits_le32(DDRC_DFIMISC(0), (0x1 << 5));
141
142 /* Step21: optional */
143
144 /* Step22: Set DFIMISC.dfi_init_complete_en to 1 */
145 setbits_le32(DDRC_DFIMISC(0), 0x1);
146
147 /* Step23: Set PWRCTL.selfref_sw to 0 */
148 clrbits_le32(DDRC_PWRCTL(0), (0x1 << 5));
149
150 /* Step24: Set SWCTL.sw_done to 1; need polling SWSTAT.sw_done_ack */
151 reg32_write(DDRC_SWCTL(0), 0x00000001);
152 do {
153 tmp = reg32_read(DDRC_SWSTAT(0));
154 } while ((tmp & 0x1) == 0x0);
155
156 /* Step25: Wait for dwc_ddr_umctl2 to move to normal operating mode by monitoring
157 * STAT.operating_mode signal */
158 do {
159 tmp = reg32_read(DDRC_STAT(0));
160 } while ((tmp & 0x3) != 0x1);
161
162 /* Step26: Set back register in Step4 to the original values if desired */
163 reg32_write(DDRC_RFSHCTL3(0), 0x0000000);
164 /* enable selfref_en by default */
165 setbits_le32(DDRC_PWRCTL(0), 0x1 << 3);
166
167 /* enable port 0 */
168 reg32_write(DDRC_PCTRL_0(0), 0x00000001);
Fabio Estevam9f7ee332019-12-11 17:37:09 -0300169 debug("DDRINFO: ddrmix config done\n");
Jacky Baid62ddc12019-08-08 09:59:08 +0000170
171 /* save the dram timing config into memory */
172 dram_config_save(dram_timing, CONFIG_SAVED_DRAM_TIMING_BASE);
Frieder Schrempf2a9b1f52019-12-11 10:01:19 +0000173
174 return 0;
Jacky Baid62ddc12019-08-08 09:59:08 +0000175}