blob: 81ccd6ab9a1f816159f2419fc6d48cb07530984d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bo Shen9d9289c2013-11-15 11:12:37 +08002/*
3 * Copyright (C) 2013 Atmel Corporation
4 * Bo Shen <voice.shen@atmel.com>
5 *
Wenyou Yangc2ad76c2016-02-01 18:12:16 +08006 * Copyright (C) 2015 Atmel Corporation
7 * Wenyou Yang <wenyou.yang@atmel.com>
Bo Shen9d9289c2013-11-15 11:12:37 +08008 */
9
10#include <common.h>
11#include <asm/io.h>
12#include <asm/arch/atmel_mpddrc.h>
13
Wenyou Yang42457012016-02-01 18:12:17 +080014#define SAMA5D3_MPDDRC_VERSION 0x140
15
Erik van Luijk0c01c3e2015-08-13 15:43:18 +020016static inline void atmel_mpddr_op(const struct atmel_mpddr *mpddr,
17 int mode,
18 u32 ram_address)
Bo Shen9d9289c2013-11-15 11:12:37 +080019{
Bo Shen9d9289c2013-11-15 11:12:37 +080020 writel(mode, &mpddr->mr);
21 writel(0, ram_address);
22}
23
Wenyou Yang42457012016-02-01 18:12:17 +080024static int ddr2_decodtype_is_seq(const unsigned int base, u32 cr)
Heiko Schocher7dd58912014-10-31 08:30:58 +010025{
Wenyou Yang42457012016-02-01 18:12:17 +080026 struct atmel_mpddr *mpddr = (struct atmel_mpddr *)base;
27 u16 version = readl(&mpddr->version) & 0xffff;
28
29 if ((version >= SAMA5D3_MPDDRC_VERSION) &&
30 (cr & ATMEL_MPDDRC_CR_DECOD_INTERLEAVED))
Heiko Schocher7dd58912014-10-31 08:30:58 +010031 return 0;
Wenyou Yang42457012016-02-01 18:12:17 +080032
Heiko Schocher7dd58912014-10-31 08:30:58 +010033 return 1;
34}
35
Erik van Luijk0c01c3e2015-08-13 15:43:18 +020036
37int ddr2_init(const unsigned int base,
38 const unsigned int ram_address,
Wenyou Yang7e8702a2016-02-01 18:12:15 +080039 const struct atmel_mpddrc_config *mpddr_value)
Bo Shen9d9289c2013-11-15 11:12:37 +080040{
Erik van Luijk0c01c3e2015-08-13 15:43:18 +020041 const struct atmel_mpddr *mpddr = (struct atmel_mpddr *)base;
42
Bo Shen9d9289c2013-11-15 11:12:37 +080043 u32 ba_off, cr;
44
45 /* Compute bank offset according to NC in configuration register */
46 ba_off = (mpddr_value->cr & ATMEL_MPDDRC_CR_NC_MASK) + 9;
Wenyou Yang42457012016-02-01 18:12:17 +080047 if (ddr2_decodtype_is_seq(base, mpddr_value->cr))
Heiko Schocher341f5482014-10-31 08:30:57 +010048 ba_off += ((mpddr_value->cr & ATMEL_MPDDRC_CR_NR_MASK) >> 2) + 11;
Bo Shen9d9289c2013-11-15 11:12:37 +080049
50 ba_off += (mpddr_value->md & ATMEL_MPDDRC_MD_DBW_MASK) ? 1 : 2;
51
52 /* Program the memory device type into the memory device register */
53 writel(mpddr_value->md, &mpddr->md);
54
55 /* Program the configuration register */
56 writel(mpddr_value->cr, &mpddr->cr);
57
58 /* Program the timing register */
59 writel(mpddr_value->tpr0, &mpddr->tpr0);
60 writel(mpddr_value->tpr1, &mpddr->tpr1);
61 writel(mpddr_value->tpr2, &mpddr->tpr2);
62
63 /* Issue a NOP command */
Erik van Luijk0c01c3e2015-08-13 15:43:18 +020064 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
Bo Shen9d9289c2013-11-15 11:12:37 +080065
66 /* A 200 us is provided to precede any signal toggle */
67 udelay(200);
68
69 /* Issue a NOP command */
Erik van Luijk0c01c3e2015-08-13 15:43:18 +020070 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
Bo Shen9d9289c2013-11-15 11:12:37 +080071
72 /* Issue an all banks precharge command */
Erik van Luijk0c01c3e2015-08-13 15:43:18 +020073 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_PRCGALL_CMD, ram_address);
Bo Shen9d9289c2013-11-15 11:12:37 +080074
75 /* Issue an extended mode register set(EMRS2) to choose operation */
Erik van Luijk0c01c3e2015-08-13 15:43:18 +020076 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
Bo Shen9d9289c2013-11-15 11:12:37 +080077 ram_address + (0x2 << ba_off));
78
79 /* Issue an extended mode register set(EMRS3) to set EMSR to 0 */
Erik van Luijk0c01c3e2015-08-13 15:43:18 +020080 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
Bo Shen9d9289c2013-11-15 11:12:37 +080081 ram_address + (0x3 << ba_off));
82
83 /*
84 * Issue an extended mode register set(EMRS1) to enable DLL and
85 * program D.I.C (output driver impedance control)
86 */
Erik van Luijk0c01c3e2015-08-13 15:43:18 +020087 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
Bo Shen9d9289c2013-11-15 11:12:37 +080088 ram_address + (0x1 << ba_off));
89
90 /* Enable DLL reset */
91 cr = readl(&mpddr->cr);
92 writel(cr | ATMEL_MPDDRC_CR_DLL_RESET_ENABLED, &mpddr->cr);
93
94 /* A mode register set(MRS) cycle is issued to reset DLL */
Erik van Luijk0c01c3e2015-08-13 15:43:18 +020095 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LMR_CMD, ram_address);
Bo Shen9d9289c2013-11-15 11:12:37 +080096
97 /* Issue an all banks precharge command */
Erik van Luijk0c01c3e2015-08-13 15:43:18 +020098 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_PRCGALL_CMD, ram_address);
Bo Shen9d9289c2013-11-15 11:12:37 +080099
100 /* Two auto-refresh (CBR) cycles are provided */
Erik van Luijk0c01c3e2015-08-13 15:43:18 +0200101 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_RFSH_CMD, ram_address);
102 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_RFSH_CMD, ram_address);
Bo Shen9d9289c2013-11-15 11:12:37 +0800103
104 /* Disable DLL reset */
105 cr = readl(&mpddr->cr);
106 writel(cr & (~ATMEL_MPDDRC_CR_DLL_RESET_ENABLED), &mpddr->cr);
107
108 /* A mode register set (MRS) cycle is issued to disable DLL reset */
Erik van Luijk0c01c3e2015-08-13 15:43:18 +0200109 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LMR_CMD, ram_address);
Bo Shen9d9289c2013-11-15 11:12:37 +0800110
111 /* Set OCD calibration in default state */
112 cr = readl(&mpddr->cr);
113 writel(cr | ATMEL_MPDDRC_CR_OCD_DEFAULT, &mpddr->cr);
114
115 /*
116 * An extended mode register set (EMRS1) cycle is issued
117 * to OCD default value
118 */
Erik van Luijk0c01c3e2015-08-13 15:43:18 +0200119 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
Bo Shen9d9289c2013-11-15 11:12:37 +0800120 ram_address + (0x1 << ba_off));
121
122 /* OCD calibration mode exit */
123 cr = readl(&mpddr->cr);
124 writel(cr & (~ATMEL_MPDDRC_CR_OCD_DEFAULT), &mpddr->cr);
125
126 /*
127 * An extended mode register set (EMRS1) cycle is issued
128 * to enable OCD exit
129 */
Erik van Luijk0c01c3e2015-08-13 15:43:18 +0200130 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
Bo Shen9d9289c2013-11-15 11:12:37 +0800131 ram_address + (0x1 << ba_off));
132
133 /* A nornal mode command is provided */
Erik van Luijk0c01c3e2015-08-13 15:43:18 +0200134 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NORMAL_CMD, ram_address);
Bo Shen9d9289c2013-11-15 11:12:37 +0800135
136 /* Perform a write access to any DDR2-SDRAM address */
137 writel(0, ram_address);
138
139 /* Write the refresh rate */
140 writel(mpddr_value->rtr, &mpddr->rtr);
141
142 return 0;
143}
Wenyou Yangc2ad76c2016-02-01 18:12:16 +0800144
145int ddr3_init(const unsigned int base,
146 const unsigned int ram_address,
147 const struct atmel_mpddrc_config *mpddr_value)
148{
149 struct atmel_mpddr *mpddr = (struct atmel_mpddr *)base;
150 u32 ba_off;
151
152 /* Compute bank offset according to NC in configuration register */
153 ba_off = (mpddr_value->cr & ATMEL_MPDDRC_CR_NC_MASK) + 9;
Wenyou Yang42457012016-02-01 18:12:17 +0800154 if (ddr2_decodtype_is_seq(base, mpddr_value->cr))
Wenyou Yangc2ad76c2016-02-01 18:12:16 +0800155 ba_off += ((mpddr_value->cr &
156 ATMEL_MPDDRC_CR_NR_MASK) >> 2) + 11;
157
158 ba_off += (mpddr_value->md & ATMEL_MPDDRC_MD_DBW_MASK) ? 1 : 2;
159
160 /* Program the memory device type */
161 writel(mpddr_value->md, &mpddr->md);
162
163 /*
164 * Program features of the DDR3-SDRAM device and timing parameters
165 */
166 writel(mpddr_value->cr, &mpddr->cr);
167
168 writel(mpddr_value->tpr0, &mpddr->tpr0);
169 writel(mpddr_value->tpr1, &mpddr->tpr1);
170 writel(mpddr_value->tpr2, &mpddr->tpr2);
171
172 /* A NOP command is issued to the DDR3-SRAM */
173 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
174
175 /* A pause of at least 500us must be observed before a single toggle. */
176 udelay(500);
177
178 /* A NOP command is issued to the DDR3-SDRAM */
179 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
180
181 /*
182 * An Extended Mode Register Set (EMRS2) cycle is issued to choose
183 * between commercial or high temperature operations.
184 */
185 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
186 ram_address + (0x2 << ba_off));
187 /*
188 * Step 7: An Extended Mode Register Set (EMRS3) cycle is issued to set
189 * the Extended Mode Register to 0.
190 */
191 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
192 ram_address + (0x3 << ba_off));
193 /*
194 * An Extended Mode Register Set (EMRS1) cycle is issued to disable and
195 * to program O.D.S. (Output Driver Strength).
196 */
197 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
198 ram_address + (0x1 << ba_off));
199
200 /*
201 * Write a one to the DLL bit (enable DLL reset) in the MPDDRC
202 * Configuration Register.
203 */
204
205 /* A Mode Register Set (MRS) cycle is issued to reset DLL. */
206 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LMR_CMD, ram_address);
207
208 udelay(50);
209
210 /*
211 * A Calibration command (MRS) is issued to calibrate RTT and RON
212 * values for the Process Voltage Temperature (PVT).
213 */
214 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_DEEP_CMD, ram_address);
215
216 /* A Normal Mode command is provided. */
217 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NORMAL_CMD, ram_address);
218
219 /* Perform a write access to any DDR3-SDRAM address. */
220 writel(0, ram_address);
221
222 /*
223 * Write the refresh rate into the COUNT field in the MPDDRC
224 * Refresh Timer Register (MPDDRC_RTR):
225 */
226 writel(mpddr_value->rtr, &mpddr->rtr);
227
228 return 0;
229}