1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2013 Atmel Corporation
4 * Bo Shen <voice.shen@atmel.com>
6 * Copyright (C) 2015 Atmel Corporation
7 * Wenyou Yang <wenyou.yang@atmel.com>
11 #include <asm/arch/atmel_mpddrc.h>
12 #include <asm/arch/at91_common.h>
13 #include <linux/delay.h>
15 #define SAMA5D3_MPDDRC_VERSION 0x140
17 static inline void atmel_mpddr_op(const struct atmel_mpddr *mpddr,
21 writel(mode, &mpddr->mr);
23 writel(0, ram_address);
26 static int ddr2_decodtype_is_seq(const unsigned int base, u32 cr)
28 struct atmel_mpddr *mpddr = (struct atmel_mpddr *)base;
29 u16 version = readl(&mpddr->version) & 0xffff;
31 if ((version >= SAMA5D3_MPDDRC_VERSION) &&
32 (cr & ATMEL_MPDDRC_CR_DECOD_INTERLEAVED))
38 int ddr2_init(const unsigned int base,
39 const unsigned int ram_address,
40 const struct atmel_mpddrc_config *mpddr_value)
42 const struct atmel_mpddr *mpddr = (struct atmel_mpddr *)base;
46 /* Compute bank offset according to NC in configuration register */
47 ba_off = (mpddr_value->cr & ATMEL_MPDDRC_CR_NC_MASK) + 9;
48 if (ddr2_decodtype_is_seq(base, mpddr_value->cr))
49 ba_off += ((mpddr_value->cr & ATMEL_MPDDRC_CR_NR_MASK) >> 2) + 11;
51 ba_off += (mpddr_value->md & ATMEL_MPDDRC_MD_DBW_MASK) ? 1 : 2;
53 /* Program the memory device type into the memory device register */
54 writel(mpddr_value->md, &mpddr->md);
56 /* Program the configuration register */
57 writel(mpddr_value->cr, &mpddr->cr);
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);
64 /* Issue a NOP command */
65 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
67 /* A 200 us is provided to precede any signal toggle */
70 /* Issue a NOP command */
71 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
73 /* Issue an all banks precharge command */
74 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_PRCGALL_CMD, ram_address);
76 /* Issue an extended mode register set(EMRS2) to choose operation */
77 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
78 ram_address + (0x2 << ba_off));
80 /* Issue an extended mode register set(EMRS3) to set EMSR to 0 */
81 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
82 ram_address + (0x3 << ba_off));
85 * Issue an extended mode register set(EMRS1) to enable DLL and
86 * program D.I.C (output driver impedance control)
88 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
89 ram_address + (0x1 << ba_off));
91 /* Enable DLL reset */
92 cr = readl(&mpddr->cr);
93 writel(cr | ATMEL_MPDDRC_CR_DLL_RESET_ENABLED, &mpddr->cr);
95 /* A mode register set(MRS) cycle is issued to reset DLL */
96 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LMR_CMD, ram_address);
98 /* Issue an all banks precharge command */
99 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_PRCGALL_CMD, ram_address);
101 /* Two auto-refresh (CBR) cycles are provided */
102 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);
105 /* Disable DLL reset */
106 cr = readl(&mpddr->cr);
107 writel(cr & (~ATMEL_MPDDRC_CR_DLL_RESET_ENABLED), &mpddr->cr);
109 /* A mode register set (MRS) cycle is issued to disable DLL reset */
110 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LMR_CMD, ram_address);
112 /* Set OCD calibration in default state */
113 cr = readl(&mpddr->cr);
114 writel(cr | ATMEL_MPDDRC_CR_OCD_DEFAULT, &mpddr->cr);
117 * An extended mode register set (EMRS1) cycle is issued
118 * to OCD default value
120 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
121 ram_address + (0x1 << ba_off));
123 /* OCD calibration mode exit */
124 cr = readl(&mpddr->cr);
125 writel(cr & (~ATMEL_MPDDRC_CR_OCD_DEFAULT), &mpddr->cr);
128 * An extended mode register set (EMRS1) cycle is issued
131 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
132 ram_address + (0x1 << ba_off));
134 /* A nornal mode command is provided */
135 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NORMAL_CMD, ram_address);
137 /* Perform a write access to any DDR2-SDRAM address */
138 writel(0, ram_address);
140 /* Write the refresh rate */
141 writel(mpddr_value->rtr, &mpddr->rtr);
146 int ddr3_init(const unsigned int base,
147 const unsigned int ram_address,
148 const struct atmel_mpddrc_config *mpddr_value)
150 struct atmel_mpddr *mpddr = (struct atmel_mpddr *)base;
153 /* Compute bank offset according to NC in configuration register */
154 ba_off = (mpddr_value->cr & ATMEL_MPDDRC_CR_NC_MASK) + 9;
155 if (ddr2_decodtype_is_seq(base, mpddr_value->cr))
156 ba_off += ((mpddr_value->cr &
157 ATMEL_MPDDRC_CR_NR_MASK) >> 2) + 11;
159 ba_off += (mpddr_value->md & ATMEL_MPDDRC_MD_DBW_MASK) ? 1 : 2;
161 /* Program the memory device type */
162 writel(mpddr_value->md, &mpddr->md);
165 * Program features of the DDR3-SDRAM device and timing parameters
167 writel(mpddr_value->cr, &mpddr->cr);
169 writel(mpddr_value->tpr0, &mpddr->tpr0);
170 writel(mpddr_value->tpr1, &mpddr->tpr1);
171 writel(mpddr_value->tpr2, &mpddr->tpr2);
173 /* A NOP command is issued to the DDR3-SRAM */
174 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
176 /* A pause of at least 500us must be observed before a single toggle. */
179 /* A NOP command is issued to the DDR3-SDRAM */
180 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
183 * An Extended Mode Register Set (EMRS2) cycle is issued to choose
184 * between commercial or high temperature operations.
186 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
187 ram_address + (0x2 << ba_off));
189 * Step 7: An Extended Mode Register Set (EMRS3) cycle is issued to set
190 * the Extended Mode Register to 0.
192 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
193 ram_address + (0x3 << ba_off));
195 * An Extended Mode Register Set (EMRS1) cycle is issued to disable and
196 * to program O.D.S. (Output Driver Strength).
198 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD,
199 ram_address + (0x1 << ba_off));
202 * Write a one to the DLL bit (enable DLL reset) in the MPDDRC
203 * Configuration Register.
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);
212 * A Calibration command (MRS) is issued to calibrate RTT and RON
213 * values for the Process Voltage Temperature (PVT).
215 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_DEEP_CMD, ram_address);
217 /* A Normal Mode command is provided. */
218 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NORMAL_CMD, ram_address);
220 /* Perform a write access to any DDR3-SDRAM address. */
221 writel(0, ram_address);
224 * Write the refresh rate into the COUNT field in the MPDDRC
225 * Refresh Timer Register (MPDDRC_RTR):
227 writel(mpddr_value->rtr, &mpddr->rtr);
232 int lpddr2_init(const unsigned int base,
233 const unsigned int ram_address,
234 const struct atmel_mpddrc_config *mpddr_value)
236 struct atmel_mpddr *mpddr = (struct atmel_mpddr *)base;
239 writel(mpddr_value->lpddr23_lpr, &mpddr->lpddr23_lpr);
241 writel(mpddr_value->tim_cal, &mpddr->tim_cal);
243 /* 1. Program the memory device type */
244 writel(mpddr_value->md, &mpddr->md);
247 * 2. Program features of the LPDDR2-SDRAM device and timing parameters
249 writel(mpddr_value->cr, &mpddr->cr);
251 writel(mpddr_value->tpr0, &mpddr->tpr0);
252 writel(mpddr_value->tpr1, &mpddr->tpr1);
253 writel(mpddr_value->tpr2, &mpddr->tpr2);
255 /* 3. A NOP command is issued to the LPDDR2-SDRAM */
256 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
259 * 3bis. Add memory barrier then Perform a write access to
260 * any low-power DDR2-SDRAM address to acknowledge the command.
264 writel(0, ram_address);
266 /* 4. A pause of at least 100 ns must be observed before a single toggle */
269 /* 5. A NOP command is issued to the LPDDR2-SDRAM */
270 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
272 /* 6. A pause of at least 200 us must be observed before a Reset Command */
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);
280 * 8. A pause of at least tINIT5 must be observed before issuing
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);
291 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
292 ATMEL_MPDDRC_MR_MRS(10), ram_address);
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.
299 reg = readl(&mpddr->cr);
300 reg &= ~ATMEL_MPDDRC_CR_ZQ_RESET;
301 reg |= ATMEL_MPDDRC_CR_ZQ_SHORT;
302 writel(reg, &mpddr->cr);
305 * 10: A Mode Register Write command with 1 to the MRS field
306 * is issued to the low-power DDR2-SDRAM.
308 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
309 ATMEL_MPDDRC_MR_MRS(1), ram_address);
312 * 11: A Mode Register Write command with 2 to the MRS field
313 * is issued to the low-power DDR2-SDRAM.
315 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
316 ATMEL_MPDDRC_MR_MRS(2), ram_address);
319 * 12: A Mode Register Write command with 3 to the MRS field
320 * is issued to the low-power DDR2-SDRAM.
322 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
323 ATMEL_MPDDRC_MR_MRS(3), ram_address);
326 * 13: A Mode Register Write command with 16 to the MRS field
327 * is issued to the low-power DDR2-SDRAM.
329 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
330 ATMEL_MPDDRC_MR_MRS(16), ram_address);
333 * 14: In the DDR Configuration Register, open the input buffers.
335 #ifdef CONFIG_ATMEL_SFR
336 configure_ddrcfg_input_buffers(true);
339 /* 15. A NOP command is issued to the LPDDR2-SDRAM */
340 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address);
343 * 16: A Mode Register Write command with 5 to the MRS field
344 * is issued to the low-power DDR2-SDRAM.
346 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
347 ATMEL_MPDDRC_MR_MRS(5), ram_address);
350 * 17: A Mode Register Write command with 6 to the MRS field
351 * is issued to the low-power DDR2-SDRAM.
353 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
354 ATMEL_MPDDRC_MR_MRS(6), ram_address);
357 * 18: A Mode Register Write command with 8 to the MRS field
358 * is issued to the low-power DDR2-SDRAM.
360 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
361 ATMEL_MPDDRC_MR_MRS(8), ram_address);
364 * 19: A Mode Register Write command with 0 to the MRS field
365 * is issued to the low-power DDR2-SDRAM.
367 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LPDDR2_CMD |
368 ATMEL_MPDDRC_MR_MRS(0), ram_address);
371 * 20: A Normal Mode command is provided.
373 atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NORMAL_CMD, ram_address);
375 /* 21: In the DDR Configuration Register, close the input buffers. */
376 #ifdef CONFIG_ATMEL_SFR
377 configure_ddrcfg_input_buffers(false);
381 * 22: Write the refresh rate into the COUNT field in the MPDDRC
382 * Refresh Timer Register.
384 writel(mpddr_value->rtr, &mpddr->rtr);
386 /* 23. Configre CAL MR4 register */
387 writel(mpddr_value->cal_mr4, &mpddr->cal_mr4);