blob: d05e666a742f5a4e1d6920046d0ab511ebc8b709 [file] [log] [blame]
Chandan Nath98b036e2011-10-14 02:58:24 +00001/*
2 * DDR Configuration for AM33xx devices.
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
Chandan Nath98b036e2011-10-14 02:58:24 +00005 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Chandan Nath98b036e2011-10-14 02:58:24 +00007 */
8
9#include <asm/arch/cpu.h>
10#include <asm/arch/ddr_defs.h>
Satyanarayana, Sandhya11784752012-08-09 18:29:57 +000011#include <asm/arch/sys_proto.h>
Chandan Nath98b036e2011-10-14 02:58:24 +000012#include <asm/io.h>
Tom Rini0d654712012-05-29 09:02:15 -070013#include <asm/emif.h>
Chandan Nath98b036e2011-10-14 02:58:24 +000014
15/**
16 * Base address for EMIF instances
17 */
Matt Porter65991ec2013-03-15 10:07:03 +000018static struct emif_reg_struct *emif_reg[2] = {
19 (struct emif_reg_struct *)EMIF4_0_CFG_BASE,
20 (struct emif_reg_struct *)EMIF4_1_CFG_BASE};
Chandan Nath98b036e2011-10-14 02:58:24 +000021
22/**
Matt Porter65991ec2013-03-15 10:07:03 +000023 * Base addresses for DDR PHY cmd/data regs
Chandan Nath98b036e2011-10-14 02:58:24 +000024 */
Matt Porter65991ec2013-03-15 10:07:03 +000025static struct ddr_cmd_regs *ddr_cmd_reg[2] = {
26 (struct ddr_cmd_regs *)DDR_PHY_CMD_ADDR,
27 (struct ddr_cmd_regs *)DDR_PHY_CMD_ADDR2};
28
29static struct ddr_data_regs *ddr_data_reg[2] = {
30 (struct ddr_data_regs *)DDR_PHY_DATA_ADDR,
31 (struct ddr_data_regs *)DDR_PHY_DATA_ADDR2};
Chandan Nath98b036e2011-10-14 02:58:24 +000032
33/**
34 * Base address for ddr io control instances
35 */
36static struct ddr_cmdtctrl *ioctrl_reg = {
37 (struct ddr_cmdtctrl *)DDR_CONTROL_BASE_ADDR};
38
Lokesh Vutlaa82d4e12013-12-10 15:02:22 +053039static inline u32 get_mr(int nr, u32 cs, u32 mr_addr)
40{
41 u32 mr;
42
43 mr_addr |= cs << EMIF_REG_CS_SHIFT;
44 writel(mr_addr, &emif_reg[nr]->emif_lpddr2_mode_reg_cfg);
45
46 mr = readl(&emif_reg[nr]->emif_lpddr2_mode_reg_data);
47 debug("get_mr: EMIF1 cs %d mr %08x val 0x%x\n", cs, mr_addr, mr);
48 if (((mr & 0x0000ff00) >> 8) == (mr & 0xff) &&
49 ((mr & 0x00ff0000) >> 16) == (mr & 0xff) &&
50 ((mr & 0xff000000) >> 24) == (mr & 0xff))
51 return mr & 0xff;
52 else
53 return mr;
54}
55
56static inline void set_mr(int nr, u32 cs, u32 mr_addr, u32 mr_val)
57{
58 mr_addr |= cs << EMIF_REG_CS_SHIFT;
59 writel(mr_addr, &emif_reg[nr]->emif_lpddr2_mode_reg_cfg);
60 writel(mr_val, &emif_reg[nr]->emif_lpddr2_mode_reg_data);
61}
62
63static void configure_mr(int nr, u32 cs)
64{
65 u32 mr_addr;
66
67 while (get_mr(nr, cs, LPDDR2_MR0) & LPDDR2_MR0_DAI_MASK)
68 ;
69 set_mr(nr, cs, LPDDR2_MR10, 0x56);
70
71 set_mr(nr, cs, LPDDR2_MR1, 0x43);
72 set_mr(nr, cs, LPDDR2_MR2, 0x2);
73
74 mr_addr = LPDDR2_MR2 | EMIF_REG_REFRESH_EN_MASK;
75 set_mr(nr, cs, mr_addr, 0x2);
76}
77
78/*
79 * Configure EMIF4D5 registers and MR registers
80 */
81void config_sdram_emif4d5(const struct emif_regs *regs, int nr)
82{
83 writel(0x0, &emif_reg[nr]->emif_pwr_mgmt_ctrl);
84 writel(0x0, &emif_reg[nr]->emif_pwr_mgmt_ctrl_shdw);
85 writel(0x1, &emif_reg[nr]->emif_iodft_tlgc);
86 writel(regs->zq_config, &emif_reg[nr]->emif_zq_config);
87
88 writel(regs->temp_alert_config, &emif_reg[nr]->emif_temp_alert_config);
89 writel(regs->emif_rd_wr_lvl_rmp_win,
90 &emif_reg[nr]->emif_rd_wr_lvl_rmp_win);
91 writel(regs->emif_rd_wr_lvl_rmp_ctl,
92 &emif_reg[nr]->emif_rd_wr_lvl_rmp_ctl);
93 writel(regs->emif_rd_wr_lvl_ctl, &emif_reg[nr]->emif_rd_wr_lvl_ctl);
94 writel(regs->emif_rd_wr_exec_thresh,
95 &emif_reg[nr]->emif_rd_wr_exec_thresh);
96
97 writel(regs->ref_ctrl, &emif_reg[nr]->emif_sdram_ref_ctrl);
98 writel(regs->sdram_config, &emif_reg[nr]->emif_sdram_config);
99
Lokesh Vutladd0037a2013-12-10 15:02:23 +0530100 if (emif_sdram_type() == EMIF_SDRAM_TYPE_LPDDR2) {
101 configure_mr(nr, 0);
102 configure_mr(nr, 1);
103 }
Lokesh Vutlaa82d4e12013-12-10 15:02:22 +0530104}
105
Chandan Nath98b036e2011-10-14 02:58:24 +0000106/**
Chandan Nath98b036e2011-10-14 02:58:24 +0000107 * Configure SDRAM
108 */
Matt Porter65991ec2013-03-15 10:07:03 +0000109void config_sdram(const struct emif_regs *regs, int nr)
Chandan Nath98b036e2011-10-14 02:58:24 +0000110{
Tom Rini1b669fd2013-02-26 16:35:33 -0500111 if (regs->zq_config) {
112 /*
113 * A value of 0x2800 for the REF CTRL will give us
114 * about 570us for a delay, which will be long enough
115 * to configure things.
116 */
Matt Porter65991ec2013-03-15 10:07:03 +0000117 writel(0x2800, &emif_reg[nr]->emif_sdram_ref_ctrl);
118 writel(regs->zq_config, &emif_reg[nr]->emif_zq_config);
Satyanarayana, Sandhya11784752012-08-09 18:29:57 +0000119 writel(regs->sdram_config, &cstat->secure_emif_sdram_config);
Matt Porter65991ec2013-03-15 10:07:03 +0000120 writel(regs->sdram_config, &emif_reg[nr]->emif_sdram_config);
121 writel(regs->ref_ctrl, &emif_reg[nr]->emif_sdram_ref_ctrl);
122 writel(regs->ref_ctrl, &emif_reg[nr]->emif_sdram_ref_ctrl_shdw);
Satyanarayana, Sandhya11784752012-08-09 18:29:57 +0000123 }
Matt Porter65991ec2013-03-15 10:07:03 +0000124 writel(regs->ref_ctrl, &emif_reg[nr]->emif_sdram_ref_ctrl);
125 writel(regs->ref_ctrl, &emif_reg[nr]->emif_sdram_ref_ctrl_shdw);
126 writel(regs->sdram_config, &emif_reg[nr]->emif_sdram_config);
Chandan Nath98b036e2011-10-14 02:58:24 +0000127}
128
129/**
130 * Set SDRAM timings
131 */
Matt Porter65991ec2013-03-15 10:07:03 +0000132void set_sdram_timings(const struct emif_regs *regs, int nr)
Chandan Nath98b036e2011-10-14 02:58:24 +0000133{
Matt Porter65991ec2013-03-15 10:07:03 +0000134 writel(regs->sdram_tim1, &emif_reg[nr]->emif_sdram_tim_1);
135 writel(regs->sdram_tim1, &emif_reg[nr]->emif_sdram_tim_1_shdw);
136 writel(regs->sdram_tim2, &emif_reg[nr]->emif_sdram_tim_2);
137 writel(regs->sdram_tim2, &emif_reg[nr]->emif_sdram_tim_2_shdw);
138 writel(regs->sdram_tim3, &emif_reg[nr]->emif_sdram_tim_3);
139 writel(regs->sdram_tim3, &emif_reg[nr]->emif_sdram_tim_3_shdw);
Chandan Nath98b036e2011-10-14 02:58:24 +0000140}
141
Lokesh Vutlaa82d4e12013-12-10 15:02:22 +0530142void __weak emif_get_ext_phy_ctrl_const_regs(const u32 **regs, u32 *size)
143{
144}
145
146/*
147 * Configure EXT PHY registers
148 */
149static void ext_phy_settings(const struct emif_regs *regs, int nr)
150{
151 u32 *ext_phy_ctrl_base = 0;
152 u32 *emif_ext_phy_ctrl_base = 0;
153 const u32 *ext_phy_ctrl_const_regs;
154 u32 i = 0;
155 u32 size;
156
157 ext_phy_ctrl_base = (u32 *)&(regs->emif_ddr_ext_phy_ctrl_1);
158 emif_ext_phy_ctrl_base =
159 (u32 *)&(emif_reg[nr]->emif_ddr_ext_phy_ctrl_1);
160
161 /* Configure external phy control timing registers */
162 for (i = 0; i < EMIF_EXT_PHY_CTRL_TIMING_REG; i++) {
163 writel(*ext_phy_ctrl_base, emif_ext_phy_ctrl_base++);
164 /* Update shadow registers */
165 writel(*ext_phy_ctrl_base++, emif_ext_phy_ctrl_base++);
166 }
167
168 /*
169 * external phy 6-24 registers do not change with
170 * ddr frequency
171 */
172 emif_get_ext_phy_ctrl_const_regs(&ext_phy_ctrl_const_regs, &size);
173
174 if (!size)
175 return;
176
177 for (i = 0; i < size; i++) {
178 writel(ext_phy_ctrl_const_regs[i], emif_ext_phy_ctrl_base++);
179 /* Update shadow registers */
180 writel(ext_phy_ctrl_const_regs[i], emif_ext_phy_ctrl_base++);
181 }
182}
183
Chandan Nath98b036e2011-10-14 02:58:24 +0000184/**
185 * Configure DDR PHY
186 */
Matt Porter65991ec2013-03-15 10:07:03 +0000187void config_ddr_phy(const struct emif_regs *regs, int nr)
Chandan Nath98b036e2011-10-14 02:58:24 +0000188{
Lokesh Vutlaa82d4e12013-12-10 15:02:22 +0530189 /*
190 * disable initialization and refreshes for now until we
191 * finish programming EMIF regs.
192 */
193 setbits_le32(&emif_reg[nr]->emif_sdram_ref_ctrl,
194 EMIF_REG_INITREF_DIS_MASK);
195
Matt Porter65991ec2013-03-15 10:07:03 +0000196 writel(regs->emif_ddr_phy_ctlr_1,
197 &emif_reg[nr]->emif_ddr_phy_ctrl_1);
198 writel(regs->emif_ddr_phy_ctlr_1,
199 &emif_reg[nr]->emif_ddr_phy_ctrl_1_shdw);
Lokesh Vutlaa82d4e12013-12-10 15:02:22 +0530200
201 if (get_emif_rev((u32)emif_reg[nr]) == EMIF_4D5)
202 ext_phy_settings(regs, nr);
Chandan Nath98b036e2011-10-14 02:58:24 +0000203}
204
205/**
206 * Configure DDR CMD control registers
207 */
Matt Porter65991ec2013-03-15 10:07:03 +0000208void config_cmd_ctrl(const struct cmd_control *cmd, int nr)
Chandan Nath98b036e2011-10-14 02:58:24 +0000209{
Lokesh Vutla303b2672013-12-10 15:02:21 +0530210 if (!cmd)
211 return;
212
Matt Porter65991ec2013-03-15 10:07:03 +0000213 writel(cmd->cmd0csratio, &ddr_cmd_reg[nr]->cm0csratio);
Matt Porter65991ec2013-03-15 10:07:03 +0000214 writel(cmd->cmd0iclkout, &ddr_cmd_reg[nr]->cm0iclkout);
Chandan Nath98b036e2011-10-14 02:58:24 +0000215
Matt Porter65991ec2013-03-15 10:07:03 +0000216 writel(cmd->cmd1csratio, &ddr_cmd_reg[nr]->cm1csratio);
Matt Porter65991ec2013-03-15 10:07:03 +0000217 writel(cmd->cmd1iclkout, &ddr_cmd_reg[nr]->cm1iclkout);
Chandan Nath98b036e2011-10-14 02:58:24 +0000218
Matt Porter65991ec2013-03-15 10:07:03 +0000219 writel(cmd->cmd2csratio, &ddr_cmd_reg[nr]->cm2csratio);
Matt Porter65991ec2013-03-15 10:07:03 +0000220 writel(cmd->cmd2iclkout, &ddr_cmd_reg[nr]->cm2iclkout);
Chandan Nath98b036e2011-10-14 02:58:24 +0000221}
222
223/**
224 * Configure DDR DATA registers
225 */
Matt Porter65991ec2013-03-15 10:07:03 +0000226void config_ddr_data(const struct ddr_data *data, int nr)
Chandan Nath98b036e2011-10-14 02:58:24 +0000227{
Matt Porter65991ec2013-03-15 10:07:03 +0000228 int i;
229
Lokesh Vutla303b2672013-12-10 15:02:21 +0530230 if (!data)
231 return;
232
Matt Porter65991ec2013-03-15 10:07:03 +0000233 for (i = 0; i < DDR_DATA_REGS_NR; i++) {
234 writel(data->datardsratio0,
235 &(ddr_data_reg[nr]+i)->dt0rdsratio0);
236 writel(data->datawdsratio0,
237 &(ddr_data_reg[nr]+i)->dt0wdsratio0);
238 writel(data->datawiratio0,
239 &(ddr_data_reg[nr]+i)->dt0wiratio0);
240 writel(data->datagiratio0,
241 &(ddr_data_reg[nr]+i)->dt0giratio0);
242 writel(data->datafwsratio0,
243 &(ddr_data_reg[nr]+i)->dt0fwsratio0);
244 writel(data->datawrsratio0,
245 &(ddr_data_reg[nr]+i)->dt0wrsratio0);
Matt Porter65991ec2013-03-15 10:07:03 +0000246 }
Chandan Nath98b036e2011-10-14 02:58:24 +0000247}
248
Lokesh Vutla303b2672013-12-10 15:02:21 +0530249void config_io_ctrl(const struct ctrl_ioregs *ioregs)
Chandan Nath98b036e2011-10-14 02:58:24 +0000250{
Lokesh Vutla303b2672013-12-10 15:02:21 +0530251 if (!ioregs)
252 return;
253
254 writel(ioregs->cm0ioctl, &ioctrl_reg->cm0ioctl);
255 writel(ioregs->cm1ioctl, &ioctrl_reg->cm1ioctl);
256 writel(ioregs->cm2ioctl, &ioctrl_reg->cm2ioctl);
257 writel(ioregs->dt0ioctl, &ioctrl_reg->dt0ioctl);
258 writel(ioregs->dt1ioctl, &ioctrl_reg->dt1ioctl);
259#ifdef CONFIG_AM43XX
260 writel(ioregs->dt2ioctrl, &ioctrl_reg->dt2ioctrl);
261 writel(ioregs->dt3ioctrl, &ioctrl_reg->dt3ioctrl);
262 writel(ioregs->emif_sdram_config_ext,
263 &ioctrl_reg->emif_sdram_config_ext);
264#endif
Chandan Nath98b036e2011-10-14 02:58:24 +0000265}