blob: acd7cc9301b3042f159aa71f4b5b63146aed4904 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bo Shenfd12de92013-11-15 11:12:37 +08002/*
3 * Copyright (C) 2013 Atmel Corporation
4 * Bo Shen <voice.shen@atmel.com>
5 *
Wenyou Yang0b326382016-02-01 18:12:16 +08006 * Copyright (C) 2015 Atmel Corporation
7 * Wenyou Yang <wenyou.yang@atmel.com>
Bo Shenfd12de92013-11-15 11:12:37 +08008 */
9
Bo Shenfd12de92013-11-15 11:12:37 +080010#include <asm/io.h>
11#include <asm/arch/atmel_mpddrc.h>
Eugen Hristevf6086772019-08-08 07:48:34 +000012#include <asm/arch/at91_common.h>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
Bo Shenfd12de92013-11-15 11:12:37 +080014
Wenyou Yang2dd7da92016-02-01 18:12:17 +080015#define SAMA5D3_MPDDRC_VERSION 0x140
16
Erik van Luijk59d780a2015-08-13 15:43:18 +020017static inline void atmel_mpddr_op(const struct atmel_mpddr *mpddr,
18 int mode,
19 u32 ram_address)
Bo Shenfd12de92013-11-15 11:12:37 +080020{
Bo Shenfd12de92013-11-15 11:12:37 +080021 writel(mode, &mpddr->mr);
Eugen Hristevf6086772019-08-08 07:48:34 +000022 dmb();
Bo Shenfd12de92013-11-15 11:12:37 +080023 writel(0, ram_address);
24}
25
Wenyou Yang2dd7da92016-02-01 18:12:17 +080026static int ddr2_decodtype_is_seq(const unsigned int base, u32 cr)
Heiko Schocher4e3976b2014-10-31 08:30:58 +010027{
Wenyou Yang2dd7da92016-02-01 18:12:17 +080028 struct atmel_mpddr *mpddr = (struct atmel_mpddr *)base;
29 u16 version = readl(&mpddr->version) & 0xffff;
30
31 if ((version >= SAMA5D3_MPDDRC_VERSION) &&
32 (cr & ATMEL_MPDDRC_CR_DECOD_INTERLEAVED))
Heiko Schocher4e3976b2014-10-31 08:30:58 +010033 return 0;
Wenyou Yang2dd7da92016-02-01 18:12:17 +080034
Heiko Schocher4e3976b2014-10-31 08:30:58 +010035 return 1;
36}
37
Erik van Luijk59d780a2015-08-13 15:43:18 +020038int ddr2_init(const unsigned int base,
39 const unsigned int ram_address,
Wenyou Yangaa0a58d2016-02-01 18:12:15 +080040 const struct atmel_mpddrc_config *mpddr_value)
Bo Shenfd12de92013-11-15 11:12:37 +080041{
Erik van Luijk59d780a2015-08-13 15:43:18 +020042 const struct atmel_mpddr *mpddr = (struct atmel_mpddr *)base;
43
Bo Shenfd12de92013-11-15 11:12:37 +080044 u32 ba_off, cr;
45
46 /* Compute bank offset according to NC in configuration register */
47 ba_off = (mpddr_value->cr & ATMEL_MPDDRC_CR_NC_MASK) + 9;
Wenyou Yang2dd7da92016-02-01 18:12:17 +080048 if (ddr2_decodtype_is_seq(base, mpddr_value->cr))
Heiko Schochera7f517a2014-10-31 08:30:57 +010049 ba_off += ((mpddr_value->cr & ATMEL_MPDDRC_CR_NR_MASK) >> 2) + 11;
Bo Shenfd12de92013-11-15 11:12:37 +080050
51 ba_off += (mpddr_value->md & ATMEL_MPDDRC_MD_DBW_MASK) ? 1 : 2;
52
53 /* Program the memory device type into the memory device register */
54 writel(mpddr_value->md, &mpddr->md);
55
56 /* Program the configuration register */
57 writel(mpddr_value->cr, &mpddr->cr);
58
59 /* Program the timing register */
60 writel(mpddr_value->tpr0, &mpddr->tpr0);
61 writel(mpddr_value->tpr1, &mpddr->tpr1);
62 writel(mpddr_value->tpr2, &mpddr->tpr2);
63
64 /* Issue a NOP command */
Erik van Luijk59d780a2015-08-13 15:43:18 +020065 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
Bo Shenfd12de92013-11-15 11:12:37 +080066
67 /* A 200 us is provided to precede any signal toggle */
68 udelay(200);
69
70 /* Issue a NOP command */
Erik van Luijk59d780a2015-08-13 15:43:18 +020071 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
Bo Shenfd12de92013-11-15 11:12:37 +080072
73 /* Issue an all banks precharge command */
Erik van Luijk59d780a2015-08-13 15:43:18 +020074 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_PRCGALL_CMD, ram_address);
Bo Shenfd12de92013-11-15 11:12:37 +080075
76 /* Issue an extended mode register set(EMRS2) to choose operation */
Erik van Luijk59d780a2015-08-13 15:43:18 +020077 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
Bo Shenfd12de92013-11-15 11:12:37 +080078 ram_address + (0x2 << ba_off));
79
80 /* Issue an extended mode register set(EMRS3) to set EMSR to 0 */
Erik van Luijk59d780a2015-08-13 15:43:18 +020081 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
Bo Shenfd12de92013-11-15 11:12:37 +080082 ram_address + (0x3 << ba_off));
83
84 /*
85 * Issue an extended mode register set(EMRS1) to enable DLL and
86 * program D.I.C (output driver impedance control)
87 */
Erik van Luijk59d780a2015-08-13 15:43:18 +020088 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
Bo Shenfd12de92013-11-15 11:12:37 +080089 ram_address + (0x1 << ba_off));
90
91 /* Enable DLL reset */
92 cr = readl(&mpddr->cr);
93 writel(cr | ATMEL_MPDDRC_CR_DLL_RESET_ENABLED, &mpddr->cr);
94
95 /* A mode register set(MRS) cycle is issued to reset DLL */
Erik van Luijk59d780a2015-08-13 15:43:18 +020096 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LMR_CMD, ram_address);
Bo Shenfd12de92013-11-15 11:12:37 +080097
98 /* Issue an all banks precharge command */
Erik van Luijk59d780a2015-08-13 15:43:18 +020099 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_PRCGALL_CMD, ram_address);
Bo Shenfd12de92013-11-15 11:12:37 +0800100
101 /* Two auto-refresh (CBR) cycles are provided */
Erik van Luijk59d780a2015-08-13 15:43:18 +0200102 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_RFSH_CMD, ram_address);
103 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_RFSH_CMD, ram_address);
Bo Shenfd12de92013-11-15 11:12:37 +0800104
105 /* Disable DLL reset */
106 cr = readl(&mpddr->cr);
107 writel(cr & (~ATMEL_MPDDRC_CR_DLL_RESET_ENABLED), &mpddr->cr);
108
109 /* A mode register set (MRS) cycle is issued to disable DLL reset */
Erik van Luijk59d780a2015-08-13 15:43:18 +0200110 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LMR_CMD, ram_address);
Bo Shenfd12de92013-11-15 11:12:37 +0800111
112 /* Set OCD calibration in default state */
113 cr = readl(&mpddr->cr);
114 writel(cr | ATMEL_MPDDRC_CR_OCD_DEFAULT, &mpddr->cr);
115
116 /*
117 * An extended mode register set (EMRS1) cycle is issued
118 * to OCD default value
119 */
Erik van Luijk59d780a2015-08-13 15:43:18 +0200120 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
Bo Shenfd12de92013-11-15 11:12:37 +0800121 ram_address + (0x1 << ba_off));
122
123 /* OCD calibration mode exit */
124 cr = readl(&mpddr->cr);
125 writel(cr & (~ATMEL_MPDDRC_CR_OCD_DEFAULT), &mpddr->cr);
126
127 /*
128 * An extended mode register set (EMRS1) cycle is issued
129 * to enable OCD exit
130 */
Erik van Luijk59d780a2015-08-13 15:43:18 +0200131 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
Bo Shenfd12de92013-11-15 11:12:37 +0800132 ram_address + (0x1 << ba_off));
133
134 /* A nornal mode command is provided */
Erik van Luijk59d780a2015-08-13 15:43:18 +0200135 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NORMAL_CMD, ram_address);
Bo Shenfd12de92013-11-15 11:12:37 +0800136
137 /* Perform a write access to any DDR2-SDRAM address */
138 writel(0, ram_address);
139
140 /* Write the refresh rate */
141 writel(mpddr_value->rtr, &mpddr->rtr);
142
143 return 0;
144}
Wenyou Yang0b326382016-02-01 18:12:16 +0800145
146int ddr3_init(const unsigned int base,
147 const unsigned int ram_address,
148 const struct atmel_mpddrc_config *mpddr_value)
149{
150 struct atmel_mpddr *mpddr = (struct atmel_mpddr *)base;
151 u32 ba_off;
152
153 /* Compute bank offset according to NC in configuration register */
154 ba_off = (mpddr_value->cr & ATMEL_MPDDRC_CR_NC_MASK) + 9;
Wenyou Yang2dd7da92016-02-01 18:12:17 +0800155 if (ddr2_decodtype_is_seq(base, mpddr_value->cr))
Wenyou Yang0b326382016-02-01 18:12:16 +0800156 ba_off += ((mpddr_value->cr &
157 ATMEL_MPDDRC_CR_NR_MASK) >> 2) + 11;
158
159 ba_off += (mpddr_value->md & ATMEL_MPDDRC_MD_DBW_MASK) ? 1 : 2;
160
161 /* Program the memory device type */
162 writel(mpddr_value->md, &mpddr->md);
163
164 /*
165 * Program features of the DDR3-SDRAM device and timing parameters
166 */
167 writel(mpddr_value->cr, &mpddr->cr);
168
169 writel(mpddr_value->tpr0, &mpddr->tpr0);
170 writel(mpddr_value->tpr1, &mpddr->tpr1);
171 writel(mpddr_value->tpr2, &mpddr->tpr2);
172
173 /* A NOP command is issued to the DDR3-SRAM */
174 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
175
176 /* A pause of at least 500us must be observed before a single toggle. */
177 udelay(500);
178
179 /* A NOP command is issued to the DDR3-SDRAM */
180 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
181
182 /*
183 * An Extended Mode Register Set (EMRS2) cycle is issued to choose
184 * between commercial or high temperature operations.
185 */
186 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
187 ram_address + (0x2 << ba_off));
188 /*
189 * Step 7: An Extended Mode Register Set (EMRS3) cycle is issued to set
190 * the Extended Mode Register to 0.
191 */
192 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
193 ram_address + (0x3 << ba_off));
194 /*
195 * An Extended Mode Register Set (EMRS1) cycle is issued to disable and
196 * to program O.D.S. (Output Driver Strength).
197 */
198 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
199 ram_address + (0x1 << ba_off));
200
201 /*
202 * Write a one to the DLL bit (enable DLL reset) in the MPDDRC
203 * Configuration Register.
204 */
205
206 /* A Mode Register Set (MRS) cycle is issued to reset DLL. */
207 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LMR_CMD, ram_address);
208
209 udelay(50);
210
211 /*
212 * A Calibration command (MRS) is issued to calibrate RTT and RON
213 * values for the Process Voltage Temperature (PVT).
214 */
215 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_DEEP_CMD, ram_address);
216
217 /* A Normal Mode command is provided. */
218 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NORMAL_CMD, ram_address);
219
220 /* Perform a write access to any DDR3-SDRAM address. */
221 writel(0, ram_address);
222
223 /*
224 * Write the refresh rate into the COUNT field in the MPDDRC
225 * Refresh Timer Register (MPDDRC_RTR):
226 */
227 writel(mpddr_value->rtr, &mpddr->rtr);
228
229 return 0;
230}
Eugen Hristevf6086772019-08-08 07:48:34 +0000231
232int lpddr2_init(const unsigned int base,
233 const unsigned int ram_address,
234 const struct atmel_mpddrc_config *mpddr_value)
235{
236 struct atmel_mpddr *mpddr = (struct atmel_mpddr *)base;
237 u32 reg;
238
239 writel(mpddr_value->lpddr23_lpr, &mpddr->lpddr23_lpr);
240
241 writel(mpddr_value->tim_cal, &mpddr->tim_cal);
242
243 /* 1. Program the memory device type */
244 writel(mpddr_value->md, &mpddr->md);
245
246 /*
247 * 2. Program features of the LPDDR2-SDRAM device and timing parameters
248 */
249 writel(mpddr_value->cr, &mpddr->cr);
250
251 writel(mpddr_value->tpr0, &mpddr->tpr0);
252 writel(mpddr_value->tpr1, &mpddr->tpr1);
253 writel(mpddr_value->tpr2, &mpddr->tpr2);
254
255 /* 3. A NOP command is issued to the LPDDR2-SDRAM */
256 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
257
258 /*
259 * 3bis. Add memory barrier then Perform a write access to
260 * any low-power DDR2-SDRAM address to acknowledge the command.
261 */
262
263 dmb();
264 writel(0, ram_address);
265
266 /* 4. A pause of at least 100 ns must be observed before a single toggle */
267 udelay(1);
268
269 /* 5. A NOP command is issued to the LPDDR2-SDRAM */
270 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
271
272 /* 6. A pause of at least 200 us must be observed before a Reset Command */
273 udelay(200);
274
275 /* 7. A Reset command is issued to the low-power DDR2-SDRAM. */
276 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
277 ATMEL_MPDDRC_MR_MRS(63), ram_address);
278
279 /*
280 * 8. A pause of at least tINIT5 must be observed before issuing
281 * any commands
282 */
283 udelay(1);
284
285 /* 9. A Calibration command is issued to the low-power DDR2-SDRAM. */
286 reg = readl(&mpddr->cr);
287 reg &= ~ATMEL_MPDDRC_CR_ZQ_RESET;
288 reg |= ATMEL_MPDDRC_CR_ZQ_RESET;
289 writel(reg, &mpddr->cr);
290
291 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
292 ATMEL_MPDDRC_MR_MRS(10), ram_address);
293
294 /*
295 * 9bis: The ZQ Calibration command is now issued.
296 * Program the type of calibration in the MPDDRC_CR: set the
297 * ZQ field to the SHORT value.
298 */
299 reg = readl(&mpddr->cr);
300 reg &= ~ATMEL_MPDDRC_CR_ZQ_RESET;
301 reg |= ATMEL_MPDDRC_CR_ZQ_SHORT;
302 writel(reg, &mpddr->cr);
303
304 /*
305 * 10: A Mode Register Write command with 1 to the MRS field
306 * is issued to the low-power DDR2-SDRAM.
307 */
308 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
309 ATMEL_MPDDRC_MR_MRS(1), ram_address);
310
311 /*
312 * 11: A Mode Register Write command with 2 to the MRS field
313 * is issued to the low-power DDR2-SDRAM.
314 */
315 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
316 ATMEL_MPDDRC_MR_MRS(2), ram_address);
317
318 /*
319 * 12: A Mode Register Write command with 3 to the MRS field
320 * is issued to the low-power DDR2-SDRAM.
321 */
322 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
323 ATMEL_MPDDRC_MR_MRS(3), ram_address);
324
325 /*
326 * 13: A Mode Register Write command with 16 to the MRS field
327 * is issued to the low-power DDR2-SDRAM.
328 */
329 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
330 ATMEL_MPDDRC_MR_MRS(16), ram_address);
331
332 /*
333 * 14: In the DDR Configuration Register, open the input buffers.
334 */
335#ifdef CONFIG_ATMEL_SFR
336 configure_ddrcfg_input_buffers(true);
337#endif
338
339 /* 15. A NOP command is issued to the LPDDR2-SDRAM */
340 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
341
342 /*
343 * 16: A Mode Register Write command with 5 to the MRS field
344 * is issued to the low-power DDR2-SDRAM.
345 */
346 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
347 ATMEL_MPDDRC_MR_MRS(5), ram_address);
348
349 /*
350 * 17: A Mode Register Write command with 6 to the MRS field
351 * is issued to the low-power DDR2-SDRAM.
352 */
353 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
354 ATMEL_MPDDRC_MR_MRS(6), ram_address);
355
356 /*
357 * 18: A Mode Register Write command with 8 to the MRS field
358 * is issued to the low-power DDR2-SDRAM.
359 */
360 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
361 ATMEL_MPDDRC_MR_MRS(8), ram_address);
362
363 /*
364 * 19: A Mode Register Write command with 0 to the MRS field
365 * is issued to the low-power DDR2-SDRAM.
366 */
367 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
368 ATMEL_MPDDRC_MR_MRS(0), ram_address);
369
370 /*
371 * 20: A Normal Mode command is provided.
372 */
373 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NORMAL_CMD, ram_address);
374
375 /* 21: In the DDR Configuration Register, close the input buffers. */
376#ifdef CONFIG_ATMEL_SFR
377 configure_ddrcfg_input_buffers(false);
378#endif
379
380 /*
381 * 22: Write the refresh rate into the COUNT field in the MPDDRC
382 * Refresh Timer Register.
383 */
384 writel(mpddr_value->rtr, &mpddr->rtr);
385
386 /* 23. Configre CAL MR4 register */
387 writel(mpddr_value->cal_mr4, &mpddr->cal_mr4);
388
389 return 0;
390}