blob: 5a1258e002d2e7eb2416bb5bcca473f0101a7d17 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tim Harvey8ab871b2014-06-02 16:13:23 -07002/*
3 * Copyright (C) 2014 Gateworks Corporation
4 * Author: Tim Harvey <tharvey@gateworks.com>
Tim Harvey8ab871b2014-06-02 16:13:23 -07005 */
6
Simon Glassf11478f2019-12-28 10:45:07 -07007#include <hang.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glassdbd79542020-05-10 11:40:11 -06009#include <linux/delay.h>
Tim Harvey8ab871b2014-06-02 16:13:23 -070010#include <linux/types.h>
Peng Fanda7ada02015-08-17 16:11:04 +080011#include <asm/arch/clock.h>
Tim Harvey8ab871b2014-06-02 16:13:23 -070012#include <asm/arch/mx6-ddr.h>
13#include <asm/arch/sys_proto.h>
14#include <asm/io.h>
15#include <asm/types.h>
Marek Vasut23023572016-03-02 14:49:51 +010016#include <wait_bit.h>
Tim Harvey8ab871b2014-06-02 16:13:23 -070017
Eric Nelsonc448df72016-10-30 16:33:50 -070018#if defined(CONFIG_MX6_DDRCAL)
Marek Vasutab257ed2015-12-16 15:40:06 +010019static void reset_read_data_fifos(void)
20{
21 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
22
23 /* Reset data FIFOs twice. */
24 setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010025 wait_for_bit_le32(&mmdc0->mpdgctrl0, 1 << 31, 0, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +010026
27 setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010028 wait_for_bit_le32(&mmdc0->mpdgctrl0, 1 << 31, 0, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +010029}
30
31static void precharge_all(const bool cs0_enable, const bool cs1_enable)
32{
33 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
34
35 /*
36 * Issue the Precharge-All command to the DDR device for both
37 * chip selects. Note, CON_REQ bit should also remain set. If
38 * only using one chip select, then precharge only the desired
39 * chip select.
40 */
41 if (cs0_enable) { /* CS0 */
42 writel(0x04008050, &mmdc0->mdscr);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010043 wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 1, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +010044 }
45
46 if (cs1_enable) { /* CS1 */
47 writel(0x04008058, &mmdc0->mdscr);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010048 wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 1, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +010049 }
50}
51
52static void force_delay_measurement(int bus_size)
53{
54 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
55 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
56
57 writel(0x800, &mmdc0->mpmur0);
58 if (bus_size == 0x2)
59 writel(0x800, &mmdc1->mpmur0);
60}
61
62static void modify_dg_result(u32 *reg_st0, u32 *reg_st1, u32 *reg_ctrl)
63{
64 u32 dg_tmp_val, dg_dl_abs_offset, dg_hc_del, val_ctrl;
65
66 /*
67 * DQS gating absolute offset should be modified from reflecting
68 * (HW_DG_LOWx + HW_DG_UPx)/2 to reflecting (HW_DG_UPx - 0x80)
69 */
70
71 val_ctrl = readl(reg_ctrl);
72 val_ctrl &= 0xf0000000;
73
74 dg_tmp_val = ((readl(reg_st0) & 0x07ff0000) >> 16) - 0xc0;
75 dg_dl_abs_offset = dg_tmp_val & 0x7f;
76 dg_hc_del = (dg_tmp_val & 0x780) << 1;
77
78 val_ctrl |= dg_dl_abs_offset + dg_hc_del;
79
80 dg_tmp_val = ((readl(reg_st1) & 0x07ff0000) >> 16) - 0xc0;
81 dg_dl_abs_offset = dg_tmp_val & 0x7f;
82 dg_hc_del = (dg_tmp_val & 0x780) << 1;
83
84 val_ctrl |= (dg_dl_abs_offset + dg_hc_del) << 16;
85
86 writel(val_ctrl, reg_ctrl);
87}
88
Marek Vasuta694dac2018-03-30 03:04:43 +020089static void correct_mpwldectr_result(void *reg)
90{
91 /* Limit is 200/256 of CK, which is WL_HC_DELx | 0x48. */
92 const unsigned int limit = 0x148;
93 u32 val = readl(reg);
94 u32 old = val;
95
96 if ((val & 0x17f) > limit)
97 val &= 0xffff << 16;
98
99 if (((val >> 16) & 0x17f) > limit)
100 val &= 0xffff;
101
102 if (old != val)
103 writel(val, reg);
104}
105
Eric Nelsona09d68a2016-10-30 16:33:48 -0700106int mmdc_do_write_level_calibration(struct mx6_ddr_sysinfo const *sysinfo)
Marek Vasutab257ed2015-12-16 15:40:06 +0100107{
108 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
109 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
Francesco Dolcini015104e2022-04-06 13:53:24 +0200110 u32 esdmisc_val, zq_val, mdmisc_val;
Marek Vasutab257ed2015-12-16 15:40:06 +0100111 u32 errors = 0;
Eric Nelsona09d68a2016-10-30 16:33:48 -0700112 u32 ldectrl[4] = {0};
Marek Vasutab257ed2015-12-16 15:40:06 +0100113 u32 ddr_mr1 = 0x4;
Eric Nelsona09d68a2016-10-30 16:33:48 -0700114 u32 rwalat_max;
Marek Vasutab257ed2015-12-16 15:40:06 +0100115
116 /*
117 * Stash old values in case calibration fails,
118 * we need to restore them
119 */
120 ldectrl[0] = readl(&mmdc0->mpwldectrl0);
121 ldectrl[1] = readl(&mmdc0->mpwldectrl1);
Eric Nelsona09d68a2016-10-30 16:33:48 -0700122 if (sysinfo->dsize == 2) {
123 ldectrl[2] = readl(&mmdc1->mpwldectrl0);
124 ldectrl[3] = readl(&mmdc1->mpwldectrl1);
125 }
Marek Vasutab257ed2015-12-16 15:40:06 +0100126
127 /* disable DDR logic power down timer */
128 clrbits_le32(&mmdc0->mdpdc, 0xff00);
129
130 /* disable Adopt power down timer */
131 setbits_le32(&mmdc0->mapsr, 0x1);
132
Francesco Dolcini015104e2022-04-06 13:53:24 +0200133 /* Save old RALAT and WALAT values */
134 mdmisc_val = readl(&mmdc0->mdmisc);
135
Marek Vasutab257ed2015-12-16 15:40:06 +0100136 debug("Starting write leveling calibration.\n");
137
138 /*
139 * 2. disable auto refresh and ZQ calibration
140 * before proceeding with Write Leveling calibration
141 */
142 esdmisc_val = readl(&mmdc0->mdref);
143 writel(0x0000C000, &mmdc0->mdref);
144 zq_val = readl(&mmdc0->mpzqhwctrl);
145 writel(zq_val & ~0x3, &mmdc0->mpzqhwctrl);
146
147 /* 3. increase walat and ralat to maximum */
Eric Nelsona09d68a2016-10-30 16:33:48 -0700148 rwalat_max = (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17);
149 setbits_le32(&mmdc0->mdmisc, rwalat_max);
150 if (sysinfo->dsize == 2)
151 setbits_le32(&mmdc1->mdmisc, rwalat_max);
Marek Vasutab257ed2015-12-16 15:40:06 +0100152 /*
153 * 4 & 5. Configure the external DDR device to enter write-leveling
154 * mode through Load Mode Register command.
155 * Register setting:
156 * Bits[31:16] MR1 value (0x0080 write leveling enable)
157 * Bit[9] set WL_EN to enable MMDC DQS output
158 * Bits[6:4] set CMD bits for Load Mode Register programming
159 * Bits[2:0] set CMD_BA to 0x1 for DDR MR1 programming
160 */
161 writel(0x00808231, &mmdc0->mdscr);
162
163 /* 6. Activate automatic calibration by setting MPWLGCR[HW_WL_EN] */
164 writel(0x00000001, &mmdc0->mpwlgcr);
165
166 /*
167 * 7. Upon completion of this process the MMDC de-asserts
168 * the MPWLGCR[HW_WL_EN]
169 */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100170 wait_for_bit_le32(&mmdc0->mpwlgcr, 1 << 0, 0, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +0100171
172 /*
173 * 8. check for any errors: check both PHYs for x64 configuration,
174 * if x32, check only PHY0
175 */
176 if (readl(&mmdc0->mpwlgcr) & 0x00000F00)
177 errors |= 1;
Eric Nelsona09d68a2016-10-30 16:33:48 -0700178 if (sysinfo->dsize == 2)
179 if (readl(&mmdc1->mpwlgcr) & 0x00000F00)
180 errors |= 2;
Marek Vasutab257ed2015-12-16 15:40:06 +0100181
182 debug("Ending write leveling calibration. Error mask: 0x%x\n", errors);
183
184 /* check to see if cal failed */
185 if ((readl(&mmdc0->mpwldectrl0) == 0x001F001F) &&
186 (readl(&mmdc0->mpwldectrl1) == 0x001F001F) &&
Eric Nelsona09d68a2016-10-30 16:33:48 -0700187 ((sysinfo->dsize < 2) ||
188 ((readl(&mmdc1->mpwldectrl0) == 0x001F001F) &&
189 (readl(&mmdc1->mpwldectrl1) == 0x001F001F)))) {
Marek Vasutab257ed2015-12-16 15:40:06 +0100190 debug("Cal seems to have soft-failed due to memory not supporting write leveling on all channels. Restoring original write leveling values.\n");
191 writel(ldectrl[0], &mmdc0->mpwldectrl0);
192 writel(ldectrl[1], &mmdc0->mpwldectrl1);
Eric Nelsona09d68a2016-10-30 16:33:48 -0700193 if (sysinfo->dsize == 2) {
194 writel(ldectrl[2], &mmdc1->mpwldectrl0);
195 writel(ldectrl[3], &mmdc1->mpwldectrl1);
196 }
Marek Vasutab257ed2015-12-16 15:40:06 +0100197 errors |= 4;
198 }
199
Marek Vasuta694dac2018-03-30 03:04:43 +0200200 correct_mpwldectr_result(&mmdc0->mpwldectrl0);
201 correct_mpwldectr_result(&mmdc0->mpwldectrl1);
202 if (sysinfo->dsize == 2) {
203 correct_mpwldectr_result(&mmdc1->mpwldectrl0);
204 correct_mpwldectr_result(&mmdc1->mpwldectrl1);
205 }
206
Marek Vasutab257ed2015-12-16 15:40:06 +0100207 /*
208 * User should issue MRS command to exit write leveling mode
209 * through Load Mode Register command
210 * Register setting:
211 * Bits[31:16] MR1 value "ddr_mr1" value from initialization
212 * Bit[9] clear WL_EN to disable MMDC DQS output
213 * Bits[6:4] set CMD bits for Load Mode Register programming
214 * Bits[2:0] set CMD_BA to 0x1 for DDR MR1 programming
215 */
216 writel((ddr_mr1 << 16) + 0x8031, &mmdc0->mdscr);
217
218 /* re-enable auto refresh and zq cal */
219 writel(esdmisc_val, &mmdc0->mdref);
220 writel(zq_val, &mmdc0->mpzqhwctrl);
221
Francesco Dolcini015104e2022-04-06 13:53:24 +0200222 /* restore WALAT/RALAT */
223 writel(mdmisc_val, &mmdc0->mdmisc);
224
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100225 debug("\tMMDC_MPWLDECTRL0 after write level cal: 0x%08x\n",
Marek Vasutab257ed2015-12-16 15:40:06 +0100226 readl(&mmdc0->mpwldectrl0));
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100227 debug("\tMMDC_MPWLDECTRL1 after write level cal: 0x%08x\n",
Marek Vasutab257ed2015-12-16 15:40:06 +0100228 readl(&mmdc0->mpwldectrl1));
Eric Nelsona09d68a2016-10-30 16:33:48 -0700229 if (sysinfo->dsize == 2) {
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100230 debug("\tMMDC_MPWLDECTRL0 after write level cal: 0x%08x\n",
Eric Nelsona09d68a2016-10-30 16:33:48 -0700231 readl(&mmdc1->mpwldectrl0));
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100232 debug("\tMMDC_MPWLDECTRL1 after write level cal: 0x%08x\n",
Eric Nelsona09d68a2016-10-30 16:33:48 -0700233 readl(&mmdc1->mpwldectrl1));
234 }
Marek Vasutab257ed2015-12-16 15:40:06 +0100235
236 /* We must force a readback of these values, to get them to stick */
237 readl(&mmdc0->mpwldectrl0);
238 readl(&mmdc0->mpwldectrl1);
Eric Nelsona09d68a2016-10-30 16:33:48 -0700239 if (sysinfo->dsize == 2) {
240 readl(&mmdc1->mpwldectrl0);
241 readl(&mmdc1->mpwldectrl1);
242 }
Marek Vasutab257ed2015-12-16 15:40:06 +0100243
244 /* enable DDR logic power down timer: */
245 setbits_le32(&mmdc0->mdpdc, 0x00005500);
246
247 /* Enable Adopt power down timer: */
248 clrbits_le32(&mmdc0->mapsr, 0x1);
249
250 /* Clear CON_REQ */
251 writel(0, &mmdc0->mdscr);
252
253 return errors;
254}
255
Marek Vasut49fad0e2019-11-26 09:34:50 +0100256static void mmdc_set_sdqs(bool set)
257{
Marek Vasut5f07dd32020-09-13 01:35:08 +0200258 struct mx6sdl_iomux_ddr_regs *mx6sdl_ddr_iomux =
259 (struct mx6sdl_iomux_ddr_regs *)MX6SDL_IOM_DDR_BASE;
Marek Vasut04ab1612019-11-26 09:34:52 +0100260 struct mx6dq_iomux_ddr_regs *mx6dq_ddr_iomux =
Marek Vasut49fad0e2019-11-26 09:34:50 +0100261 (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
Marek Vasut04ab1612019-11-26 09:34:52 +0100262 struct mx6sx_iomux_ddr_regs *mx6sx_ddr_iomux =
263 (struct mx6sx_iomux_ddr_regs *)MX6SX_IOM_DDR_BASE;
Marek Vasut5f07dd32020-09-13 01:35:08 +0200264 struct mx6sl_iomux_ddr_regs *mx6sl_ddr_iomux =
265 (struct mx6sl_iomux_ddr_regs *)MX6SL_IOM_DDR_BASE;
266 struct mx6ul_iomux_ddr_regs *mx6ul_ddr_iomux =
267 (struct mx6ul_iomux_ddr_regs *)MX6UL_IOM_DDR_BASE;
Marek Vasut04ab1612019-11-26 09:34:52 +0100268 int i, sdqs_cnt;
269 u32 sdqs;
270
271 if (is_mx6sx()) {
272 sdqs = (u32)(&mx6sx_ddr_iomux->dram_sdqs0);
273 sdqs_cnt = 2;
Marek Vasut5f07dd32020-09-13 01:35:08 +0200274 } else if (is_mx6sl()) {
275 sdqs = (u32)(&mx6sl_ddr_iomux->dram_sdqs0);
276 sdqs_cnt = 2;
277 } else if (is_mx6ul() || is_mx6ull()) {
278 sdqs = (u32)(&mx6ul_ddr_iomux->dram_sdqs0);
279 sdqs_cnt = 2;
280 } else if (is_mx6sdl()) {
281 sdqs = (u32)(&mx6sdl_ddr_iomux->dram_sdqs0);
282 sdqs_cnt = 8;
Marek Vasut04ab1612019-11-26 09:34:52 +0100283 } else { /* MX6DQ */
284 sdqs = (u32)(&mx6dq_ddr_iomux->dram_sdqs0);
285 sdqs_cnt = 8;
286 }
Marek Vasut49fad0e2019-11-26 09:34:50 +0100287
Marek Vasut04ab1612019-11-26 09:34:52 +0100288 for (i = 0; i < sdqs_cnt; i++) {
Marek Vasut85edd7f2019-11-26 09:34:51 +0100289 if (set)
290 setbits_le32(sdqs + (4 * i), 0x7000);
291 else
292 clrbits_le32(sdqs + (4 * i), 0x7000);
Marek Vasut49fad0e2019-11-26 09:34:50 +0100293 }
294}
295
Eric Nelsona09d68a2016-10-30 16:33:48 -0700296int mmdc_do_dqs_calibration(struct mx6_ddr_sysinfo const *sysinfo)
Marek Vasutab257ed2015-12-16 15:40:06 +0100297{
298 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
299 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
Marek Vasutab257ed2015-12-16 15:40:06 +0100300 bool cs0_enable;
301 bool cs1_enable;
302 bool cs0_enable_initial;
303 bool cs1_enable_initial;
304 u32 esdmisc_val;
Marek Vasutab257ed2015-12-16 15:40:06 +0100305 u32 temp_ref;
306 u32 pddword = 0x00ffff00; /* best so far, place into MPPDCMPR1 */
307 u32 errors = 0;
308 u32 initdelay = 0x40404040;
309
310 /* check to see which chip selects are enabled */
311 cs0_enable_initial = readl(&mmdc0->mdctl) & 0x80000000;
312 cs1_enable_initial = readl(&mmdc0->mdctl) & 0x40000000;
313
314 /* disable DDR logic power down timer: */
315 clrbits_le32(&mmdc0->mdpdc, 0xff00);
316
317 /* disable Adopt power down timer: */
318 setbits_le32(&mmdc0->mapsr, 0x1);
319
320 /* set DQS pull ups */
Marek Vasut49fad0e2019-11-26 09:34:50 +0100321 mmdc_set_sdqs(true);
Marek Vasutab257ed2015-12-16 15:40:06 +0100322
323 /* Save old RALAT and WALAT values */
324 esdmisc_val = readl(&mmdc0->mdmisc);
325
326 setbits_le32(&mmdc0->mdmisc,
327 (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17));
328
329 /* Disable auto refresh before proceeding with calibration */
330 temp_ref = readl(&mmdc0->mdref);
331 writel(0x0000c000, &mmdc0->mdref);
332
333 /*
334 * Per the ref manual, issue one refresh cycle MDSCR[CMD]= 0x2,
335 * this also sets the CON_REQ bit.
336 */
337 if (cs0_enable_initial)
338 writel(0x00008020, &mmdc0->mdscr);
339 if (cs1_enable_initial)
340 writel(0x00008028, &mmdc0->mdscr);
341
342 /* poll to make sure the con_ack bit was asserted */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100343 wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 1, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +0100344
345 /*
346 * Check MDMISC register CALIB_PER_CS to see which CS calibration
347 * is targeted to (under normal cases, it should be cleared
348 * as this is the default value, indicating calibration is directed
349 * to CS0).
350 * Disable the other chip select not being target for calibration
351 * to avoid any potential issues. This will get re-enabled at end
352 * of calibration.
353 */
354 if ((readl(&mmdc0->mdmisc) & 0x00100000) == 0)
355 clrbits_le32(&mmdc0->mdctl, 1 << 30); /* clear SDE_1 */
356 else
357 clrbits_le32(&mmdc0->mdctl, 1 << 31); /* clear SDE_0 */
358
359 /*
360 * Check to see which chip selects are now enabled for
361 * the remainder of the calibration.
362 */
363 cs0_enable = readl(&mmdc0->mdctl) & 0x80000000;
364 cs1_enable = readl(&mmdc0->mdctl) & 0x40000000;
365
Marek Vasutab257ed2015-12-16 15:40:06 +0100366 precharge_all(cs0_enable, cs1_enable);
367
368 /* Write the pre-defined value into MPPDCMPR1 */
369 writel(pddword, &mmdc0->mppdcmpr1);
370
371 /*
372 * Issue a write access to the external DDR device by setting
373 * the bit SW_DUMMY_WR (bit 0) in the MPSWDAR0 and then poll
374 * this bit until it clears to indicate completion of the write access.
375 */
376 setbits_le32(&mmdc0->mpswdar0, 1);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100377 wait_for_bit_le32(&mmdc0->mpswdar0, 1 << 0, 0, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +0100378
379 /* Set the RD_DL_ABS# bits to their default values
380 * (will be calibrated later in the read delay-line calibration).
381 * Both PHYs for x64 configuration, if x32, do only PHY0.
382 */
383 writel(initdelay, &mmdc0->mprddlctl);
Eric Nelsona09d68a2016-10-30 16:33:48 -0700384 if (sysinfo->dsize == 0x2)
Marek Vasutab257ed2015-12-16 15:40:06 +0100385 writel(initdelay, &mmdc1->mprddlctl);
386
387 /* Force a measurment, for previous delay setup to take effect. */
Eric Nelsona09d68a2016-10-30 16:33:48 -0700388 force_delay_measurement(sysinfo->dsize);
Marek Vasutab257ed2015-12-16 15:40:06 +0100389
390 /*
391 * ***************************
392 * Read DQS Gating calibration
393 * ***************************
394 */
395 debug("Starting Read DQS Gating calibration.\n");
396
397 /*
398 * Reset the read data FIFOs (two resets); only need to issue reset
399 * to PHY0 since in x64 mode, the reset will also go to PHY1.
400 */
401 reset_read_data_fifos();
402
403 /*
404 * Start the automatic read DQS gating calibration process by
405 * asserting MPDGCTRL0[HW_DG_EN] and MPDGCTRL0[DG_CMP_CYC]
406 * and then poll MPDGCTRL0[HW_DG_EN]] until this bit clears
407 * to indicate completion.
408 * Also, ensure that MPDGCTRL0[HW_DG_ERR] is clear to indicate
409 * no errors were seen during calibration.
410 */
411
412 /*
413 * Set bit 30: chooses option to wait 32 cycles instead of
414 * 16 before comparing read data.
415 */
416 setbits_le32(&mmdc0->mpdgctrl0, 1 << 30);
Eric Nelson4285a532016-10-30 16:33:47 -0700417 if (sysinfo->dsize == 2)
418 setbits_le32(&mmdc1->mpdgctrl0, 1 << 30);
Marek Vasutab257ed2015-12-16 15:40:06 +0100419
420 /* Set bit 28 to start automatic read DQS gating calibration */
421 setbits_le32(&mmdc0->mpdgctrl0, 5 << 28);
422
423 /* Poll for completion. MPDGCTRL0[HW_DG_EN] should be 0 */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100424 wait_for_bit_le32(&mmdc0->mpdgctrl0, 1 << 28, 0, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +0100425
426 /*
427 * Check to see if any errors were encountered during calibration
428 * (check MPDGCTRL0[HW_DG_ERR]).
429 * Check both PHYs for x64 configuration, if x32, check only PHY0.
430 */
431 if (readl(&mmdc0->mpdgctrl0) & 0x00001000)
432 errors |= 1;
433
Eric Nelsona09d68a2016-10-30 16:33:48 -0700434 if ((sysinfo->dsize == 0x2) && (readl(&mmdc1->mpdgctrl0) & 0x00001000))
Marek Vasutab257ed2015-12-16 15:40:06 +0100435 errors |= 2;
436
Eric Nelson4285a532016-10-30 16:33:47 -0700437 /* now disable mpdgctrl0[DG_CMP_CYC] */
438 clrbits_le32(&mmdc0->mpdgctrl0, 1 << 30);
439 if (sysinfo->dsize == 2)
440 clrbits_le32(&mmdc1->mpdgctrl0, 1 << 30);
441
Marek Vasutab257ed2015-12-16 15:40:06 +0100442 /*
443 * DQS gating absolute offset should be modified from
444 * reflecting (HW_DG_LOWx + HW_DG_UPx)/2 to
445 * reflecting (HW_DG_UPx - 0x80)
446 */
447 modify_dg_result(&mmdc0->mpdghwst0, &mmdc0->mpdghwst1,
448 &mmdc0->mpdgctrl0);
449 modify_dg_result(&mmdc0->mpdghwst2, &mmdc0->mpdghwst3,
450 &mmdc0->mpdgctrl1);
Eric Nelsona09d68a2016-10-30 16:33:48 -0700451 if (sysinfo->dsize == 0x2) {
Marek Vasutab257ed2015-12-16 15:40:06 +0100452 modify_dg_result(&mmdc1->mpdghwst0, &mmdc1->mpdghwst1,
453 &mmdc1->mpdgctrl0);
454 modify_dg_result(&mmdc1->mpdghwst2, &mmdc1->mpdghwst3,
455 &mmdc1->mpdgctrl1);
456 }
457 debug("Ending Read DQS Gating calibration. Error mask: 0x%x\n", errors);
458
459 /*
460 * **********************
461 * Read Delay calibration
462 * **********************
463 */
464 debug("Starting Read Delay calibration.\n");
465
466 reset_read_data_fifos();
467
468 /*
469 * 4. Issue the Precharge-All command to the DDR device for both
470 * chip selects. If only using one chip select, then precharge
471 * only the desired chip select.
472 */
473 precharge_all(cs0_enable, cs1_enable);
474
475 /*
476 * 9. Read delay-line calibration
477 * Start the automatic read calibration process by asserting
478 * MPRDDLHWCTL[HW_RD_DL_EN].
479 */
480 writel(0x00000030, &mmdc0->mprddlhwctl);
481
482 /*
483 * 10. poll for completion
484 * MMDC indicates that the write data calibration had finished by
485 * setting MPRDDLHWCTL[HW_RD_DL_EN] = 0. Also, ensure that
486 * no error bits were set.
487 */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100488 wait_for_bit_le32(&mmdc0->mprddlhwctl, 1 << 4, 0, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +0100489
490 /* check both PHYs for x64 configuration, if x32, check only PHY0 */
491 if (readl(&mmdc0->mprddlhwctl) & 0x0000000f)
492 errors |= 4;
493
Eric Nelsona09d68a2016-10-30 16:33:48 -0700494 if ((sysinfo->dsize == 0x2) &&
495 (readl(&mmdc1->mprddlhwctl) & 0x0000000f))
Marek Vasutab257ed2015-12-16 15:40:06 +0100496 errors |= 8;
497
498 debug("Ending Read Delay calibration. Error mask: 0x%x\n", errors);
499
500 /*
501 * ***********************
502 * Write Delay Calibration
503 * ***********************
504 */
505 debug("Starting Write Delay calibration.\n");
506
507 reset_read_data_fifos();
508
509 /*
510 * 4. Issue the Precharge-All command to the DDR device for both
511 * chip selects. If only using one chip select, then precharge
512 * only the desired chip select.
513 */
514 precharge_all(cs0_enable, cs1_enable);
515
516 /*
517 * 8. Set the WR_DL_ABS# bits to their default values.
518 * Both PHYs for x64 configuration, if x32, do only PHY0.
519 */
520 writel(initdelay, &mmdc0->mpwrdlctl);
Eric Nelsona09d68a2016-10-30 16:33:48 -0700521 if (sysinfo->dsize == 0x2)
Marek Vasutab257ed2015-12-16 15:40:06 +0100522 writel(initdelay, &mmdc1->mpwrdlctl);
523
524 /*
525 * XXX This isn't in the manual. Force a measurement,
526 * for previous delay setup to effect.
527 */
Eric Nelsona09d68a2016-10-30 16:33:48 -0700528 force_delay_measurement(sysinfo->dsize);
Marek Vasutab257ed2015-12-16 15:40:06 +0100529
530 /*
531 * 9. 10. Start the automatic write calibration process
532 * by asserting MPWRDLHWCTL0[HW_WR_DL_EN].
533 */
534 writel(0x00000030, &mmdc0->mpwrdlhwctl);
535
536 /*
537 * Poll for completion.
538 * MMDC indicates that the write data calibration had finished
539 * by setting MPWRDLHWCTL[HW_WR_DL_EN] = 0.
540 * Also, ensure that no error bits were set.
541 */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100542 wait_for_bit_le32(&mmdc0->mpwrdlhwctl, 1 << 4, 0, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +0100543
544 /* Check both PHYs for x64 configuration, if x32, check only PHY0 */
545 if (readl(&mmdc0->mpwrdlhwctl) & 0x0000000f)
546 errors |= 16;
547
Eric Nelsona09d68a2016-10-30 16:33:48 -0700548 if ((sysinfo->dsize == 0x2) &&
549 (readl(&mmdc1->mpwrdlhwctl) & 0x0000000f))
Marek Vasutab257ed2015-12-16 15:40:06 +0100550 errors |= 32;
551
552 debug("Ending Write Delay calibration. Error mask: 0x%x\n", errors);
553
554 reset_read_data_fifos();
555
556 /* Enable DDR logic power down timer */
557 setbits_le32(&mmdc0->mdpdc, 0x00005500);
558
559 /* Enable Adopt power down timer */
560 clrbits_le32(&mmdc0->mapsr, 0x1);
561
562 /* Restore MDMISC value (RALAT, WALAT) to MMDCP1 */
563 writel(esdmisc_val, &mmdc0->mdmisc);
564
565 /* Clear DQS pull ups */
Marek Vasut49fad0e2019-11-26 09:34:50 +0100566 mmdc_set_sdqs(false);
Marek Vasutab257ed2015-12-16 15:40:06 +0100567
568 /* Re-enable SDE (chip selects) if they were set initially */
569 if (cs1_enable_initial)
570 /* Set SDE_1 */
571 setbits_le32(&mmdc0->mdctl, 1 << 30);
572
573 if (cs0_enable_initial)
574 /* Set SDE_0 */
575 setbits_le32(&mmdc0->mdctl, 1 << 31);
576
577 /* Re-enable to auto refresh */
578 writel(temp_ref, &mmdc0->mdref);
579
580 /* Clear the MDSCR (including the con_req bit) */
581 writel(0x0, &mmdc0->mdscr); /* CS0 */
582
583 /* Poll to make sure the con_ack bit is clear */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100584 wait_for_bit_le32(&mmdc0->mdscr, 1 << 14, 0, 100, 0);
Marek Vasutab257ed2015-12-16 15:40:06 +0100585
586 /*
587 * Print out the registers that were updated as a result
588 * of the calibration process.
589 */
590 debug("MMDC registers updated from calibration\n");
591 debug("Read DQS gating calibration:\n");
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100592 debug("\tMPDGCTRL0 PHY0 = 0x%08x\n", readl(&mmdc0->mpdgctrl0));
593 debug("\tMPDGCTRL1 PHY0 = 0x%08x\n", readl(&mmdc0->mpdgctrl1));
Eric Nelsona09d68a2016-10-30 16:33:48 -0700594 if (sysinfo->dsize == 2) {
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100595 debug("\tMPDGCTRL0 PHY1 = 0x%08x\n", readl(&mmdc1->mpdgctrl0));
596 debug("\tMPDGCTRL1 PHY1 = 0x%08x\n", readl(&mmdc1->mpdgctrl1));
Eric Nelsona09d68a2016-10-30 16:33:48 -0700597 }
Marek Vasutab257ed2015-12-16 15:40:06 +0100598 debug("Read calibration:\n");
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100599 debug("\tMPRDDLCTL PHY0 = 0x%08x\n", readl(&mmdc0->mprddlctl));
Eric Nelsona09d68a2016-10-30 16:33:48 -0700600 if (sysinfo->dsize == 2)
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100601 debug("\tMPRDDLCTL PHY1 = 0x%08x\n", readl(&mmdc1->mprddlctl));
Marek Vasutab257ed2015-12-16 15:40:06 +0100602 debug("Write calibration:\n");
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100603 debug("\tMPWRDLCTL PHY0 = 0x%08x\n", readl(&mmdc0->mpwrdlctl));
Eric Nelsona09d68a2016-10-30 16:33:48 -0700604 if (sysinfo->dsize == 2)
Marek Vasut1b8e5dc2019-11-26 09:34:49 +0100605 debug("\tMPWRDLCTL PHY1 = 0x%08x\n", readl(&mmdc1->mpwrdlctl));
Marek Vasutab257ed2015-12-16 15:40:06 +0100606
607 /*
608 * Registers below are for debugging purposes. These print out
609 * the upper and lower boundaries captured during
610 * read DQS gating calibration.
611 */
612 debug("Status registers bounds for read DQS gating:\n");
613 debug("\tMPDGHWST0 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst0));
614 debug("\tMPDGHWST1 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst1));
615 debug("\tMPDGHWST2 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst2));
616 debug("\tMPDGHWST3 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst3));
Eric Nelsona09d68a2016-10-30 16:33:48 -0700617 if (sysinfo->dsize == 2) {
618 debug("\tMPDGHWST0 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst0));
619 debug("\tMPDGHWST1 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst1));
620 debug("\tMPDGHWST2 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst2));
621 debug("\tMPDGHWST3 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst3));
622 }
Marek Vasutab257ed2015-12-16 15:40:06 +0100623
624 debug("Final do_dqs_calibration error mask: 0x%x\n", errors);
625
626 return errors;
627}
628#endif
629
Peng Fan2ecdd022014-12-30 17:24:01 +0800630#if defined(CONFIG_MX6SX)
631/* Configure MX6SX mmdc iomux */
632void mx6sx_dram_iocfg(unsigned width,
633 const struct mx6sx_iomux_ddr_regs *ddr,
634 const struct mx6sx_iomux_grp_regs *grp)
635{
636 struct mx6sx_iomux_ddr_regs *mx6_ddr_iomux;
637 struct mx6sx_iomux_grp_regs *mx6_grp_iomux;
638
639 mx6_ddr_iomux = (struct mx6sx_iomux_ddr_regs *)MX6SX_IOM_DDR_BASE;
640 mx6_grp_iomux = (struct mx6sx_iomux_grp_regs *)MX6SX_IOM_GRP_BASE;
641
642 /* DDR IO TYPE */
643 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
644 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
645
646 /* CLOCK */
647 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
648
649 /* ADDRESS */
650 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
651 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
652 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
653
654 /* Control */
655 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
656 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
657 writel(ddr->dram_sdcke0, &mx6_ddr_iomux->dram_sdcke0);
658 writel(ddr->dram_sdcke1, &mx6_ddr_iomux->dram_sdcke1);
659 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
660 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
661 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
662
663 /* Data Strobes */
664 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
665 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
666 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
667 if (width >= 32) {
668 writel(ddr->dram_sdqs2, &mx6_ddr_iomux->dram_sdqs2);
669 writel(ddr->dram_sdqs3, &mx6_ddr_iomux->dram_sdqs3);
670 }
671
672 /* Data */
673 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
674 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
675 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
676 if (width >= 32) {
677 writel(grp->grp_b2ds, &mx6_grp_iomux->grp_b2ds);
678 writel(grp->grp_b3ds, &mx6_grp_iomux->grp_b3ds);
679 }
680 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
681 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
682 if (width >= 32) {
683 writel(ddr->dram_dqm2, &mx6_ddr_iomux->dram_dqm2);
684 writel(ddr->dram_dqm3, &mx6_ddr_iomux->dram_dqm3);
685 }
686}
687#endif
688
Fabio Estevam1b691df2018-01-03 12:33:05 -0200689#if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
Peng Fan98f11a12015-07-20 19:28:33 +0800690void mx6ul_dram_iocfg(unsigned width,
691 const struct mx6ul_iomux_ddr_regs *ddr,
692 const struct mx6ul_iomux_grp_regs *grp)
693{
694 struct mx6ul_iomux_ddr_regs *mx6_ddr_iomux;
695 struct mx6ul_iomux_grp_regs *mx6_grp_iomux;
696
697 mx6_ddr_iomux = (struct mx6ul_iomux_ddr_regs *)MX6UL_IOM_DDR_BASE;
698 mx6_grp_iomux = (struct mx6ul_iomux_grp_regs *)MX6UL_IOM_GRP_BASE;
699
700 /* DDR IO TYPE */
701 writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
702 writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
703
704 /* CLOCK */
705 writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
706
707 /* ADDRESS */
708 writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
709 writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
710 writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
711
712 /* Control */
713 writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
714 writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
715 writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
716 writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
717 writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
718
719 /* Data Strobes */
720 writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
721 writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
722 writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
723
724 /* Data */
725 writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
726 writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
727 writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
728 writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
729 writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
730}
731#endif
732
Peng Fand226fac2015-08-17 16:11:00 +0800733#if defined(CONFIG_MX6SL)
734void mx6sl_dram_iocfg(unsigned width,
735 const struct mx6sl_iomux_ddr_regs *ddr,
736 const struct mx6sl_iomux_grp_regs *grp)
737{
738 struct mx6sl_iomux_ddr_regs *mx6_ddr_iomux;
739 struct mx6sl_iomux_grp_regs *mx6_grp_iomux;
740
741 mx6_ddr_iomux = (struct mx6sl_iomux_ddr_regs *)MX6SL_IOM_DDR_BASE;
742 mx6_grp_iomux = (struct mx6sl_iomux_grp_regs *)MX6SL_IOM_GRP_BASE;
743
744 /* DDR IO TYPE */
745 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
746 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
747
748 /* CLOCK */
749 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
750
751 /* ADDRESS */
752 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
753 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
754 mx6_grp_iomux->grp_addds = grp->grp_addds;
755
756 /* Control */
757 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
758 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
759 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
760
761 /* Data Strobes */
762 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
763 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
764 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
765 if (width >= 32) {
766 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
767 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
768 }
769
770 /* Data */
771 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
772 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
773 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
774 if (width >= 32) {
775 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
776 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
777 }
778
779 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
780 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
781 if (width >= 32) {
782 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
783 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
784 }
785}
786#endif
787
Tim Harvey8ab871b2014-06-02 16:13:23 -0700788#if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6Q) || defined(CONFIG_MX6D)
789/* Configure MX6DQ mmdc iomux */
790void mx6dq_dram_iocfg(unsigned width,
791 const struct mx6dq_iomux_ddr_regs *ddr,
792 const struct mx6dq_iomux_grp_regs *grp)
793{
794 volatile struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux;
795 volatile struct mx6dq_iomux_grp_regs *mx6_grp_iomux;
796
797 mx6_ddr_iomux = (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
798 mx6_grp_iomux = (struct mx6dq_iomux_grp_regs *)MX6DQ_IOM_GRP_BASE;
799
800 /* DDR IO Type */
801 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
802 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
803
804 /* Clock */
805 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
806 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
807
808 /* Address */
809 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
810 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
811 mx6_grp_iomux->grp_addds = grp->grp_addds;
812
813 /* Control */
814 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
815 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
816 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
817 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
818 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
819 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
820 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
821
822 /* Data Strobes */
823 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
824 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
825 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
826 if (width >= 32) {
827 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
828 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
829 }
830 if (width >= 64) {
831 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
832 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
833 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
834 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
835 }
836
837 /* Data */
838 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
839 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
840 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
841 if (width >= 32) {
842 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
843 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
844 }
845 if (width >= 64) {
846 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
847 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
848 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
849 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
850 }
851 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
852 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
853 if (width >= 32) {
854 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
855 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
856 }
857 if (width >= 64) {
858 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
859 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
860 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
861 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
862 }
863}
864#endif
865
866#if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6DL) || defined(CONFIG_MX6S)
867/* Configure MX6SDL mmdc iomux */
868void mx6sdl_dram_iocfg(unsigned width,
869 const struct mx6sdl_iomux_ddr_regs *ddr,
870 const struct mx6sdl_iomux_grp_regs *grp)
871{
872 volatile struct mx6sdl_iomux_ddr_regs *mx6_ddr_iomux;
873 volatile struct mx6sdl_iomux_grp_regs *mx6_grp_iomux;
874
875 mx6_ddr_iomux = (struct mx6sdl_iomux_ddr_regs *)MX6SDL_IOM_DDR_BASE;
876 mx6_grp_iomux = (struct mx6sdl_iomux_grp_regs *)MX6SDL_IOM_GRP_BASE;
877
878 /* DDR IO Type */
879 mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
880 mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
881
882 /* Clock */
883 mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
884 mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
885
886 /* Address */
887 mx6_ddr_iomux->dram_cas = ddr->dram_cas;
888 mx6_ddr_iomux->dram_ras = ddr->dram_ras;
889 mx6_grp_iomux->grp_addds = grp->grp_addds;
890
891 /* Control */
892 mx6_ddr_iomux->dram_reset = ddr->dram_reset;
893 mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
894 mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
895 mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
896 mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
897 mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
898 mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
899
900 /* Data Strobes */
901 mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
902 mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
903 mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
904 if (width >= 32) {
905 mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
906 mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
907 }
908 if (width >= 64) {
909 mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
910 mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
911 mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
912 mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
913 }
914
915 /* Data */
916 mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
917 mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
918 mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
919 if (width >= 32) {
920 mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
921 mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
922 }
923 if (width >= 64) {
924 mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
925 mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
926 mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
927 mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
928 }
929 mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
930 mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
931 if (width >= 32) {
932 mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
933 mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
934 }
935 if (width >= 64) {
936 mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
937 mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
938 mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
939 mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
940 }
941}
942#endif
943
944/*
945 * Configure mx6 mmdc registers based on:
946 * - board-specific memory configuration
947 * - board-specific calibration data
Peng Fanda7ada02015-08-17 16:11:04 +0800948 * - ddr3/lpddr2 chip details
Tim Harvey8ab871b2014-06-02 16:13:23 -0700949 *
950 * The various calculations here are derived from the Freescale
Peng Fanda7ada02015-08-17 16:11:04 +0800951 * 1. i.Mx6DQSDL DDR3 Script Aid spreadsheet (DOC-94917) designed to generate
952 * MMDC configuration registers based on memory system and memory chip
953 * parameters.
954 *
955 * 2. i.Mx6SL LPDDR2 Script Aid spreadsheet V0.04 designed to generate MMDC
956 * configuration registers based on memory system and memory chip
957 * parameters.
Tim Harvey8ab871b2014-06-02 16:13:23 -0700958 *
959 * The defaults here are those which were specified in the spreadsheet.
960 * For details on each register, refer to the IMX6DQRM and/or IMX6SDLRM
Peng Fanda7ada02015-08-17 16:11:04 +0800961 * and/or IMX6SLRM section titled MMDC initialization.
Tim Harvey8ab871b2014-06-02 16:13:23 -0700962 */
963#define MR(val, ba, cmd, cs1) \
964 ((val << 16) | (1 << 15) | (cmd << 4) | (cs1 << 3) | ba)
Peng Fan98f11a12015-07-20 19:28:33 +0800965#define MMDC1(entry, value) do { \
Fabio Estevam8548f972018-01-01 22:51:45 -0200966 if (!is_mx6sx() && !is_mx6ul() && !is_mx6ull() && !is_mx6sl()) \
Peng Fan98f11a12015-07-20 19:28:33 +0800967 mmdc1->entry = value; \
968 } while (0)
969
Bernhard Messerklinger9de2cb82020-03-09 10:55:34 +0100970/* see BOOT_CFG3 description Table 5-4. EIM Boot Fusemap */
971#define BOOT_CFG3_DDR_MASK 0x30
972#define BOOT_CFG3_EXT_DDR_MASK 0x33
973
974#define DDR_MMAP_NOC_SINGLE 0
975#define DDR_MMAP_NOC_DUAL 0x31
976
977/* NoC ACTIVATE shifts */
978#define NOC_RD_SHIFT 0
979#define NOC_FAW_PERIOD_SHIFT 4
980#define NOC_FAW_BANKS_SHIFT 10
981
982/* NoC DdrTiming shifts */
983#define NOC_ACT_TO_ACT_SHIFT 0
984#define NOC_RD_TO_MISS_SHIFT 6
985#define NOC_WR_TO_MISS_SHIFT 12
986#define NOC_BURST_LEN_SHIFT 18
987#define NOC_RD_TO_WR_SHIFT 21
988#define NOC_WR_TO_RD_SHIFT 26
989#define NOC_BW_RATIO_SHIFT 31
990
Peng Fanda7ada02015-08-17 16:11:04 +0800991/*
992 * According JESD209-2B-LPDDR2: Table 103
993 * WL: write latency
994 */
995static int lpddr2_wl(uint32_t mem_speed)
996{
997 switch (mem_speed) {
998 case 1066:
999 case 933:
1000 return 4;
1001 case 800:
1002 return 3;
1003 case 677:
1004 case 533:
1005 return 2;
1006 case 400:
1007 case 333:
1008 return 1;
1009 default:
1010 puts("invalid memory speed\n");
1011 hang();
1012 }
1013
1014 return 0;
1015}
1016
1017/*
1018 * According JESD209-2B-LPDDR2: Table 103
1019 * RL: read latency
1020 */
1021static int lpddr2_rl(uint32_t mem_speed)
1022{
1023 switch (mem_speed) {
1024 case 1066:
1025 return 8;
1026 case 933:
1027 return 7;
1028 case 800:
1029 return 6;
1030 case 677:
1031 return 5;
1032 case 533:
1033 return 4;
1034 case 400:
1035 case 333:
1036 return 3;
1037 default:
1038 puts("invalid memory speed\n");
1039 hang();
1040 }
1041
1042 return 0;
1043}
1044
1045void mx6_lpddr2_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1046 const struct mx6_mmdc_calibration *calib,
1047 const struct mx6_lpddr2_cfg *lpddr2_cfg)
1048{
1049 volatile struct mmdc_p_regs *mmdc0;
1050 u32 val;
1051 u8 tcke, tcksrx, tcksre, trrd;
1052 u8 twl, txp, tfaw, tcl;
1053 u16 tras, twr, tmrd, trtp, twtr, trfc, txsr;
1054 u16 trcd_lp, trppb_lp, trpab_lp, trc_lp;
1055 u16 cs0_end;
1056 u8 coladdr;
1057 int clkper; /* clock period in picoseconds */
1058 int clock; /* clock freq in mHz */
1059 int cs;
1060
1061 /* only support 16/32 bits */
1062 if (sysinfo->dsize > 1)
1063 hang();
1064
1065 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
1066
1067 clock = mxc_get_clock(MXC_DDR_CLK) / 1000000U;
1068 clkper = (1000 * 1000) / clock; /* pico seconds */
1069
1070 twl = lpddr2_wl(lpddr2_cfg->mem_speed) - 1;
1071
1072 /* LPDDR2-S2 and LPDDR2-S4 have the same tRFC value. */
1073 switch (lpddr2_cfg->density) {
1074 case 1:
1075 case 2:
1076 case 4:
1077 trfc = DIV_ROUND_UP(130000, clkper) - 1;
1078 txsr = DIV_ROUND_UP(140000, clkper) - 1;
1079 break;
1080 case 8:
1081 trfc = DIV_ROUND_UP(210000, clkper) - 1;
1082 txsr = DIV_ROUND_UP(220000, clkper) - 1;
1083 break;
1084 default:
1085 /*
1086 * 64Mb, 128Mb, 256Mb, 512Mb are not supported currently.
1087 */
1088 hang();
1089 break;
1090 }
1091 /*
1092 * txpdll, txpr, taonpd and taofpd are not relevant in LPDDR2 mode,
1093 * set them to 0. */
1094 txp = DIV_ROUND_UP(7500, clkper) - 1;
1095 tcke = 3;
1096 if (lpddr2_cfg->mem_speed == 333)
1097 tfaw = DIV_ROUND_UP(60000, clkper) - 1;
1098 else
1099 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
1100 trrd = DIV_ROUND_UP(10000, clkper) - 1;
1101
1102 /* tckesr for LPDDR2 */
1103 tcksre = DIV_ROUND_UP(15000, clkper);
1104 tcksrx = tcksre;
1105 twr = DIV_ROUND_UP(15000, clkper) - 1;
1106 /*
1107 * tMRR: 2, tMRW: 5
1108 * tMRD should be set to max(tMRR, tMRW)
1109 */
1110 tmrd = 5;
1111 tras = DIV_ROUND_UP(lpddr2_cfg->trasmin, clkper / 10) - 1;
1112 /* LPDDR2 mode use tRCD_LP filed in MDCFG3. */
1113 trcd_lp = DIV_ROUND_UP(lpddr2_cfg->trcd_lp, clkper / 10) - 1;
1114 trc_lp = DIV_ROUND_UP(lpddr2_cfg->trasmin + lpddr2_cfg->trppb_lp,
1115 clkper / 10) - 1;
1116 trppb_lp = DIV_ROUND_UP(lpddr2_cfg->trppb_lp, clkper / 10) - 1;
1117 trpab_lp = DIV_ROUND_UP(lpddr2_cfg->trpab_lp, clkper / 10) - 1;
1118 /* To LPDDR2, CL in MDCFG0 refers to RL */
1119 tcl = lpddr2_rl(lpddr2_cfg->mem_speed) - 3;
1120 twtr = DIV_ROUND_UP(7500, clkper) - 1;
1121 trtp = DIV_ROUND_UP(7500, clkper) - 1;
1122
1123 cs0_end = 4 * sysinfo->cs_density - 1;
1124
1125 debug("density:%d Gb (%d Gb per chip)\n",
1126 sysinfo->cs_density, lpddr2_cfg->density);
1127 debug("clock: %dMHz (%d ps)\n", clock, clkper);
1128 debug("memspd:%d\n", lpddr2_cfg->mem_speed);
1129 debug("trcd_lp=%d\n", trcd_lp);
1130 debug("trppb_lp=%d\n", trppb_lp);
1131 debug("trpab_lp=%d\n", trpab_lp);
1132 debug("trc_lp=%d\n", trc_lp);
1133 debug("tcke=%d\n", tcke);
1134 debug("tcksrx=%d\n", tcksrx);
1135 debug("tcksre=%d\n", tcksre);
1136 debug("trfc=%d\n", trfc);
1137 debug("txsr=%d\n", txsr);
1138 debug("txp=%d\n", txp);
1139 debug("tfaw=%d\n", tfaw);
1140 debug("tcl=%d\n", tcl);
1141 debug("tras=%d\n", tras);
1142 debug("twr=%d\n", twr);
1143 debug("tmrd=%d\n", tmrd);
1144 debug("twl=%d\n", twl);
1145 debug("trtp=%d\n", trtp);
1146 debug("twtr=%d\n", twtr);
1147 debug("trrd=%d\n", trrd);
1148 debug("cs0_end=%d\n", cs0_end);
1149 debug("ncs=%d\n", sysinfo->ncs);
1150
1151 /*
1152 * board-specific configuration:
1153 * These values are determined empirically and vary per board layout
1154 */
1155 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1156 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1157 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1158 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1159 mmdc0->mprddlctl = calib->p0_mprddlctl;
1160 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1161 mmdc0->mpzqlp2ctl = calib->mpzqlp2ctl;
1162
1163 /* Read data DQ Byte0-3 delay */
1164 mmdc0->mprddqby0dl = 0x33333333;
1165 mmdc0->mprddqby1dl = 0x33333333;
1166 if (sysinfo->dsize > 0) {
1167 mmdc0->mprddqby2dl = 0x33333333;
1168 mmdc0->mprddqby3dl = 0x33333333;
1169 }
1170
1171 /* Write data DQ Byte0-3 delay */
1172 mmdc0->mpwrdqby0dl = 0xf3333333;
1173 mmdc0->mpwrdqby1dl = 0xf3333333;
1174 if (sysinfo->dsize > 0) {
1175 mmdc0->mpwrdqby2dl = 0xf3333333;
1176 mmdc0->mpwrdqby3dl = 0xf3333333;
1177 }
1178
1179 /*
1180 * In LPDDR2 mode this register should be cleared,
1181 * so no termination will be activated.
1182 */
1183 mmdc0->mpodtctrl = 0;
1184
1185 /* complete calibration */
1186 val = (1 << 11); /* Force measurement on delay-lines */
1187 mmdc0->mpmur0 = val;
1188
1189 /* Step 1: configuration request */
1190 mmdc0->mdscr = (u32)(1 << 15); /* config request */
1191
1192 /* Step 2: Timing configuration */
1193 mmdc0->mdcfg0 = (trfc << 24) | (txsr << 16) | (txp << 13) |
1194 (tfaw << 4) | tcl;
1195 mmdc0->mdcfg1 = (tras << 16) | (twr << 9) | (tmrd << 5) | twl;
1196 mmdc0->mdcfg2 = (trtp << 6) | (twtr << 3) | trrd;
1197 mmdc0->mdcfg3lp = (trc_lp << 16) | (trcd_lp << 8) |
1198 (trppb_lp << 4) | trpab_lp;
1199 mmdc0->mdotc = 0;
1200
1201 mmdc0->mdasp = cs0_end; /* CS addressing */
1202
1203 /* Step 3: Configure DDR type */
1204 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1205 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1206 (sysinfo->ralat << 6) | (1 << 3);
1207
1208 /* Step 4: Configure delay while leaving reset */
1209 mmdc0->mdor = (sysinfo->sde_to_rst << 8) |
1210 (sysinfo->rst_to_cke << 0);
1211
1212 /* Step 5: Configure DDR physical parameters (density and burst len) */
1213 coladdr = lpddr2_cfg->coladdr;
1214 if (lpddr2_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
1215 coladdr += 4;
1216 else if (lpddr2_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
1217 coladdr += 1;
1218 mmdc0->mdctl = (lpddr2_cfg->rowaddr - 11) << 24 | /* ROW */
1219 (coladdr - 9) << 20 | /* COL */
1220 (0 << 19) | /* Burst Length = 4 for LPDDR2 */
1221 (sysinfo->dsize << 16); /* DDR data bus size */
1222
1223 /* Step 6: Perform ZQ calibration */
1224 val = 0xa1390003; /* one-time HW ZQ calib */
1225 mmdc0->mpzqhwctrl = val;
1226
1227 /* Step 7: Enable MMDC with desired chip select */
1228 mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
1229 ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
1230
1231 /* Step 8: Write Mode Registers to Init LPDDR2 devices */
1232 for (cs = 0; cs < sysinfo->ncs; cs++) {
1233 /* MR63: reset */
1234 mmdc0->mdscr = MR(63, 0, 3, cs);
1235 /* MR10: calibration,
1236 * 0xff is calibration command after intilization.
1237 */
1238 val = 0xA | (0xff << 8);
1239 mmdc0->mdscr = MR(val, 0, 3, cs);
1240 /* MR1 */
1241 val = 0x1 | (0x82 << 8);
1242 mmdc0->mdscr = MR(val, 0, 3, cs);
1243 /* MR2 */
1244 val = 0x2 | (0x04 << 8);
1245 mmdc0->mdscr = MR(val, 0, 3, cs);
1246 /* MR3 */
1247 val = 0x3 | (0x02 << 8);
1248 mmdc0->mdscr = MR(val, 0, 3, cs);
1249 }
1250
1251 /* Step 10: Power down control and self-refresh */
1252 mmdc0->mdpdc = (tcke & 0x7) << 16 |
1253 5 << 12 | /* PWDT_1: 256 cycles */
1254 5 << 8 | /* PWDT_0: 256 cycles */
1255 1 << 6 | /* BOTH_CS_PD */
1256 (tcksrx & 0x7) << 3 |
1257 (tcksre & 0x7);
1258 mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
1259
1260 /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
1261 val = 0xa1310003;
1262 mmdc0->mpzqhwctrl = val;
1263
1264 /* Step 12: Configure and activate periodic refresh */
Fabio Estevamcb3c1212016-08-29 20:37:15 -03001265 mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
Peng Fanda7ada02015-08-17 16:11:04 +08001266
1267 /* Step 13: Deassert config request - init complete */
1268 mmdc0->mdscr = 0x00000000;
1269
1270 /* wait for auto-ZQ calibration to complete */
1271 mdelay(1);
1272}
1273
Peng Fan77e86952015-08-17 16:11:03 +08001274void mx6_ddr3_cfg(const struct mx6_ddr_sysinfo *sysinfo,
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001275 const struct mx6_mmdc_calibration *calib,
1276 const struct mx6_ddr3_cfg *ddr3_cfg)
Tim Harvey8ab871b2014-06-02 16:13:23 -07001277{
1278 volatile struct mmdc_p_regs *mmdc0;
1279 volatile struct mmdc_p_regs *mmdc1;
Bernhard Messerklinger9de2cb82020-03-09 10:55:34 +01001280 struct src *src_regs = (struct src *)SRC_BASE_ADDR;
1281 u8 soc_boot_cfg3 = (readl(&src_regs->sbmr1) >> 16) & 0xff;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001282 u32 val;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001283 u8 tcke, tcksrx, tcksre, txpdll, taofpd, taonpd, trrd;
1284 u8 todtlon, taxpd, tanpd, tcwl, txp, tfaw, tcl;
1285 u8 todt_idle_off = 0x4; /* from DDR3 Script Aid spreadsheet */
1286 u16 trcd, trc, tras, twr, tmrd, trtp, trp, twtr, trfc, txs, txpr;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001287 u16 cs0_end;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001288 u16 tdllk = 0x1ff; /* DLL locking time: 512 cycles (JEDEC DDR3) */
Marek Vasut4a463602014-08-04 01:47:10 +02001289 u8 coladdr;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001290 int clkper; /* clock period in picoseconds */
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001291 int clock; /* clock freq in MHz */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001292 int cs;
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001293 u16 mem_speed = ddr3_cfg->mem_speed;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001294
1295 mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
Fabio Estevam8548f972018-01-01 22:51:45 -02001296 if (!is_mx6sx() && !is_mx6ul() && !is_mx6ull() && !is_mx6sl())
Peng Fan98f11a12015-07-20 19:28:33 +08001297 mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001298
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001299 /* Limit mem_speed for MX6D/MX6Q */
Peng Fan9dba13b2016-05-23 18:35:57 +08001300 if (is_mx6dq() || is_mx6dqp()) {
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001301 if (mem_speed > 1066)
1302 mem_speed = 1066; /* 1066 MT/s */
1303
Tim Harvey8ab871b2014-06-02 16:13:23 -07001304 tcwl = 4;
1305 }
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001306 /* Limit mem_speed for MX6S/MX6DL */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001307 else {
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001308 if (mem_speed > 800)
1309 mem_speed = 800; /* 800 MT/s */
1310
Tim Harvey8ab871b2014-06-02 16:13:23 -07001311 tcwl = 3;
1312 }
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001313
1314 clock = mem_speed / 2;
1315 /*
1316 * Data rate of 1066 MT/s requires 533 MHz DDR3 clock, but MX6D/Q supports
1317 * up to 528 MHz, so reduce the clock to fit chip specs
1318 */
Peng Fan9dba13b2016-05-23 18:35:57 +08001319 if (is_mx6dq() || is_mx6dqp()) {
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001320 if (clock > 528)
1321 clock = 528; /* 528 MHz */
1322 }
1323
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001324 clkper = (1000 * 1000) / clock; /* pico seconds */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001325 todtlon = tcwl;
1326 taxpd = tcwl;
1327 tanpd = tcwl;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001328
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001329 switch (ddr3_cfg->density) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001330 case 1: /* 1Gb per chip */
1331 trfc = DIV_ROUND_UP(110000, clkper) - 1;
1332 txs = DIV_ROUND_UP(120000, clkper) - 1;
1333 break;
1334 case 2: /* 2Gb per chip */
1335 trfc = DIV_ROUND_UP(160000, clkper) - 1;
1336 txs = DIV_ROUND_UP(170000, clkper) - 1;
1337 break;
1338 case 4: /* 4Gb per chip */
Peng Fanb96b74c2015-09-01 11:03:14 +08001339 trfc = DIV_ROUND_UP(260000, clkper) - 1;
1340 txs = DIV_ROUND_UP(270000, clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001341 break;
1342 case 8: /* 8Gb per chip */
1343 trfc = DIV_ROUND_UP(350000, clkper) - 1;
1344 txs = DIV_ROUND_UP(360000, clkper) - 1;
1345 break;
1346 default:
1347 /* invalid density */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001348 puts("invalid chip density\n");
Tim Harvey8ab871b2014-06-02 16:13:23 -07001349 hang();
1350 break;
1351 }
1352 txpr = txs;
1353
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001354 switch (mem_speed) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001355 case 800:
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001356 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1357 tcke = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001358 if (ddr3_cfg->pagesz == 1) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001359 tfaw = DIV_ROUND_UP(40000, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001360 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001361 } else {
1362 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001363 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001364 }
1365 break;
1366 case 1066:
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001367 txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
1368 tcke = DIV_ROUND_UP(max(3 * clkper, 5625), clkper) - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001369 if (ddr3_cfg->pagesz == 1) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001370 tfaw = DIV_ROUND_UP(37500, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001371 trrd = DIV_ROUND_UP(max(4 * clkper, 7500), clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001372 } else {
1373 tfaw = DIV_ROUND_UP(50000, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001374 trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001375 }
1376 break;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001377 default:
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001378 puts("invalid memory speed\n");
Tim Harvey8ab871b2014-06-02 16:13:23 -07001379 hang();
1380 break;
1381 }
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001382 txpdll = DIV_ROUND_UP(max(10 * clkper, 24000), clkper) - 1;
1383 tcksre = DIV_ROUND_UP(max(5 * clkper, 10000), clkper);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001384 taonpd = DIV_ROUND_UP(2000, clkper) - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001385 tcksrx = tcksre;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001386 taofpd = taonpd;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001387 twr = DIV_ROUND_UP(15000, clkper) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001388 tmrd = DIV_ROUND_UP(max(12 * clkper, 15000), clkper) - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001389 trc = DIV_ROUND_UP(ddr3_cfg->trcmin, clkper / 10) - 1;
1390 tras = DIV_ROUND_UP(ddr3_cfg->trasmin, clkper / 10) - 1;
1391 tcl = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 3;
1392 trp = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 1;
Masahiro Yamadab62b39b2014-09-18 13:28:06 +09001393 twtr = ROUND(max(4 * clkper, 7500) / clkper, 1) - 1;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001394 trcd = trp;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001395 trtp = twtr;
Nikita Kiryanov4a50ec22014-08-20 15:08:58 +03001396 cs0_end = 4 * sysinfo->cs_density - 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001397
1398 debug("density:%d Gb (%d Gb per chip)\n",
1399 sysinfo->cs_density, ddr3_cfg->density);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001400 debug("clock: %dMHz (%d ps)\n", clock, clkper);
Nikolay Dimitrov99c25ff2015-04-22 18:37:31 +03001401 debug("memspd:%d\n", mem_speed);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001402 debug("tcke=%d\n", tcke);
1403 debug("tcksrx=%d\n", tcksrx);
1404 debug("tcksre=%d\n", tcksre);
1405 debug("taofpd=%d\n", taofpd);
1406 debug("taonpd=%d\n", taonpd);
1407 debug("todtlon=%d\n", todtlon);
1408 debug("tanpd=%d\n", tanpd);
1409 debug("taxpd=%d\n", taxpd);
1410 debug("trfc=%d\n", trfc);
1411 debug("txs=%d\n", txs);
1412 debug("txp=%d\n", txp);
1413 debug("txpdll=%d\n", txpdll);
1414 debug("tfaw=%d\n", tfaw);
1415 debug("tcl=%d\n", tcl);
1416 debug("trcd=%d\n", trcd);
1417 debug("trp=%d\n", trp);
1418 debug("trc=%d\n", trc);
1419 debug("tras=%d\n", tras);
1420 debug("twr=%d\n", twr);
1421 debug("tmrd=%d\n", tmrd);
1422 debug("tcwl=%d\n", tcwl);
1423 debug("tdllk=%d\n", tdllk);
1424 debug("trtp=%d\n", trtp);
1425 debug("twtr=%d\n", twtr);
1426 debug("trrd=%d\n", trrd);
1427 debug("txpr=%d\n", txpr);
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001428 debug("cs0_end=%d\n", cs0_end);
1429 debug("ncs=%d\n", sysinfo->ncs);
1430 debug("Rtt_wr=%d\n", sysinfo->rtt_wr);
1431 debug("Rtt_nom=%d\n", sysinfo->rtt_nom);
1432 debug("SRT=%d\n", ddr3_cfg->SRT);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001433 debug("twr=%d\n", twr);
1434
1435 /*
1436 * board-specific configuration:
1437 * These values are determined empirically and vary per board layout
1438 * see:
1439 * appnote, ddr3 spreadsheet
1440 */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001441 mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
1442 mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
1443 mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
1444 mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
1445 mmdc0->mprddlctl = calib->p0_mprddlctl;
1446 mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
1447 if (sysinfo->dsize > 1) {
Peng Fan2ecdd022014-12-30 17:24:01 +08001448 MMDC1(mpwldectrl0, calib->p1_mpwldectrl0);
1449 MMDC1(mpwldectrl1, calib->p1_mpwldectrl1);
1450 MMDC1(mpdgctrl0, calib->p1_mpdgctrl0);
1451 MMDC1(mpdgctrl1, calib->p1_mpdgctrl1);
1452 MMDC1(mprddlctl, calib->p1_mprddlctl);
1453 MMDC1(mpwrdlctl, calib->p1_mpwrdlctl);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001454 }
1455
1456 /* Read data DQ Byte0-3 delay */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001457 mmdc0->mprddqby0dl = 0x33333333;
1458 mmdc0->mprddqby1dl = 0x33333333;
1459 if (sysinfo->dsize > 0) {
1460 mmdc0->mprddqby2dl = 0x33333333;
1461 mmdc0->mprddqby3dl = 0x33333333;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001462 }
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001463
1464 if (sysinfo->dsize > 1) {
Peng Fan2ecdd022014-12-30 17:24:01 +08001465 MMDC1(mprddqby0dl, 0x33333333);
1466 MMDC1(mprddqby1dl, 0x33333333);
1467 MMDC1(mprddqby2dl, 0x33333333);
1468 MMDC1(mprddqby3dl, 0x33333333);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001469 }
1470
Francesco Dolcini4fb1f872022-06-24 12:33:35 +02001471 /*
1472 * MMDC Termination: rtt_nom:2 RZQ/2(120ohm),
1473 * rtt_nom:1 RZQ/4(60ohm),
1474 * rtt_nom:0 Disabled
1475 */
1476 if (sysinfo->rtt_nom == 0)
1477 val = 0x00000000;
1478 else if (sysinfo->rtt_nom == 2)
1479 val = 0x00011117;
1480 else
1481 val = 0x00022227;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001482 mmdc0->mpodtctrl = val;
1483 if (sysinfo->dsize > 1)
Peng Fan2ecdd022014-12-30 17:24:01 +08001484 MMDC1(mpodtctrl, val);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001485
1486 /* complete calibration */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001487 val = (1 << 11); /* Force measurement on delay-lines */
1488 mmdc0->mpmur0 = val;
1489 if (sysinfo->dsize > 1)
Peng Fan2ecdd022014-12-30 17:24:01 +08001490 MMDC1(mpmur0, val);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001491
1492 /* Step 1: configuration request */
1493 mmdc0->mdscr = (u32)(1 << 15); /* config request */
1494
1495 /* Step 2: Timing configuration */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001496 mmdc0->mdcfg0 = (trfc << 24) | (txs << 16) | (txp << 13) |
1497 (txpdll << 9) | (tfaw << 4) | tcl;
1498 mmdc0->mdcfg1 = (trcd << 29) | (trp << 26) | (trc << 21) |
1499 (tras << 16) | (1 << 15) /* trpa */ |
1500 (twr << 9) | (tmrd << 5) | tcwl;
1501 mmdc0->mdcfg2 = (tdllk << 16) | (trtp << 6) | (twtr << 3) | trrd;
1502 mmdc0->mdotc = (taofpd << 27) | (taonpd << 24) | (tanpd << 20) |
1503 (taxpd << 16) | (todtlon << 12) | (todt_idle_off << 4);
1504 mmdc0->mdasp = cs0_end; /* CS addressing */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001505
1506 /* Step 3: Configure DDR type */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001507 mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
1508 (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
1509 (sysinfo->ralat << 6);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001510
1511 /* Step 4: Configure delay while leaving reset */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001512 mmdc0->mdor = (txpr << 16) | (sysinfo->sde_to_rst << 8) |
1513 (sysinfo->rst_to_cke << 0);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001514
1515 /* Step 5: Configure DDR physical parameters (density and burst len) */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001516 coladdr = ddr3_cfg->coladdr;
1517 if (ddr3_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
Marek Vasut4a463602014-08-04 01:47:10 +02001518 coladdr += 4;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001519 else if (ddr3_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
Marek Vasut4a463602014-08-04 01:47:10 +02001520 coladdr += 1;
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001521 mmdc0->mdctl = (ddr3_cfg->rowaddr - 11) << 24 | /* ROW */
1522 (coladdr - 9) << 20 | /* COL */
1523 (1 << 19) | /* Burst Length = 8 for DDR3 */
1524 (sysinfo->dsize << 16); /* DDR data bus size */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001525
1526 /* Step 6: Perform ZQ calibration */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001527 val = 0xa1390001; /* one-time HW ZQ calib */
1528 mmdc0->mpzqhwctrl = val;
1529 if (sysinfo->dsize > 1)
Peng Fan2ecdd022014-12-30 17:24:01 +08001530 MMDC1(mpzqhwctrl, val);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001531
1532 /* Step 7: Enable MMDC with desired chip select */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001533 mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
1534 ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001535
1536 /* Step 8: Write Mode Registers to Init DDR3 devices */
Francesco Dolcini084f8a42022-04-06 13:53:25 +02001537 mdelay(1); /* Wait before issuing the first MRS command.
1538 * Minimum wait time is (tXPR + 500us),
1539 * with max tXPR value 360ns, and 500us wait required after
1540 * RESET_n is de-asserted.
1541 */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001542 for (cs = 0; cs < sysinfo->ncs; cs++) {
Tim Harvey8ab871b2014-06-02 16:13:23 -07001543 /* MR2 */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001544 val = (sysinfo->rtt_wr & 3) << 9 | (ddr3_cfg->SRT & 1) << 7 |
Tim Harvey8ab871b2014-06-02 16:13:23 -07001545 ((tcwl - 3) & 3) << 3;
Tim Harveyfe1723f2015-04-03 16:52:52 -07001546 debug("MR2 CS%d: 0x%08x\n", cs, (u32)MR(val, 2, 3, cs));
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001547 mmdc0->mdscr = MR(val, 2, 3, cs);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001548 /* MR3 */
Tim Harveyfe1723f2015-04-03 16:52:52 -07001549 debug("MR3 CS%d: 0x%08x\n", cs, (u32)MR(0, 3, 3, cs));
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001550 mmdc0->mdscr = MR(0, 3, 3, cs);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001551 /* MR1 */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001552 val = ((sysinfo->rtt_nom & 1) ? 1 : 0) << 2 |
1553 ((sysinfo->rtt_nom & 2) ? 1 : 0) << 6;
Tim Harveyfe1723f2015-04-03 16:52:52 -07001554 debug("MR1 CS%d: 0x%08x\n", cs, (u32)MR(val, 1, 3, cs));
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001555 mmdc0->mdscr = MR(val, 1, 3, cs);
1556 /* MR0 */
1557 val = ((tcl - 1) << 4) | /* CAS */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001558 (1 << 8) | /* DLL Reset */
Tim Harvey591fe972015-05-18 07:07:02 -07001559 ((twr - 3) << 9) | /* Write Recovery */
1560 (sysinfo->pd_fast_exit << 12); /* Precharge PD PLL on */
Tim Harveyfe1723f2015-04-03 16:52:52 -07001561 debug("MR0 CS%d: 0x%08x\n", cs, (u32)MR(val, 0, 3, cs));
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001562 mmdc0->mdscr = MR(val, 0, 3, cs);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001563 /* ZQ calibration */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001564 val = (1 << 10);
1565 mmdc0->mdscr = MR(val, 0, 4, cs);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001566 }
1567
1568 /* Step 10: Power down control and self-refresh */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001569 mmdc0->mdpdc = (tcke & 0x7) << 16 |
1570 5 << 12 | /* PWDT_1: 256 cycles */
1571 5 << 8 | /* PWDT_0: 256 cycles */
1572 1 << 6 | /* BOTH_CS_PD */
1573 (tcksrx & 0x7) << 3 |
1574 (tcksre & 0x7);
Tim Harveyfe1723f2015-04-03 16:52:52 -07001575 if (!sysinfo->pd_fast_exit)
1576 mmdc0->mdpdc |= (1 << 7); /* SLOW_PD */
Nikita Kiryanov6816f712014-08-20 15:08:56 +03001577 mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
Tim Harvey8ab871b2014-06-02 16:13:23 -07001578
1579 /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001580 val = 0xa1390003;
1581 mmdc0->mpzqhwctrl = val;
1582 if (sysinfo->dsize > 1)
Peng Fan2ecdd022014-12-30 17:24:01 +08001583 MMDC1(mpzqhwctrl, val);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001584
1585 /* Step 12: Configure and activate periodic refresh */
Fabio Estevamcb3c1212016-08-29 20:37:15 -03001586 mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
Tim Harvey8ab871b2014-06-02 16:13:23 -07001587
Bernhard Messerklinger9de2cb82020-03-09 10:55:34 +01001588 /*
1589 * Step 13: i.MX6DQP only: If the NoC scheduler is enabled,
1590 * configure it and disable MMDC arbitration/reordering (see EB828)
1591 */
1592 if (is_mx6dqp() &&
1593 ((soc_boot_cfg3 & BOOT_CFG3_DDR_MASK) == DDR_MMAP_NOC_SINGLE ||
1594 (soc_boot_cfg3 & BOOT_CFG3_EXT_DDR_MASK) == DDR_MMAP_NOC_DUAL)) {
1595 struct mx6dqp_noc_sched_regs *noc_sched =
1596 (struct mx6dqp_noc_sched_regs *)MX6DQP_NOC_SCHED_BASE;
1597
1598 /*
1599 * These values are fixed based on integration parameters and
1600 * should not be modified
1601 */
1602 noc_sched->rlat = 0x00000040;
1603 noc_sched->ipu1 = 0x00000020;
1604 noc_sched->ipu2 = 0x00000020;
1605
1606 noc_sched->activate = (1 << NOC_FAW_BANKS_SHIFT) |
1607 (tfaw << NOC_FAW_PERIOD_SHIFT) |
1608 (trrd << NOC_RD_SHIFT);
1609 noc_sched->ddrtiming = (((sysinfo->dsize == 1) ? 1 : 0)
1610 << NOC_BW_RATIO_SHIFT) |
1611 ((tcwl + twtr) << NOC_WR_TO_RD_SHIFT) |
1612 ((tcl - tcwl + 2) << NOC_RD_TO_WR_SHIFT) |
1613 (4 << NOC_BURST_LEN_SHIFT) | /* BL8 */
1614 ((tcwl + twr + trp + trcd)
1615 << NOC_WR_TO_MISS_SHIFT) |
1616 ((trtp + trp + trcd - 4)
1617 << NOC_RD_TO_MISS_SHIFT) |
1618 (trc << NOC_ACT_TO_ACT_SHIFT);
1619
1620 if (sysinfo->dsize == 2) {
1621 if (ddr3_cfg->coladdr == 10) {
1622 if (ddr3_cfg->rowaddr == 15 &&
1623 sysinfo->ncs == 2)
1624 noc_sched->ddrconf = 4;
1625 else
1626 noc_sched->ddrconf = 0;
1627 } else if (ddr3_cfg->coladdr == 11) {
1628 noc_sched->ddrconf = 1;
1629 }
1630 } else {
1631 if (ddr3_cfg->coladdr == 9) {
1632 if (ddr3_cfg->rowaddr == 13)
1633 noc_sched->ddrconf = 2;
1634 else if (ddr3_cfg->rowaddr == 14)
1635 noc_sched->ddrconf = 15;
1636 } else if (ddr3_cfg->coladdr == 10) {
1637 if (ddr3_cfg->rowaddr == 14 &&
1638 sysinfo->ncs == 2)
1639 noc_sched->ddrconf = 14;
1640 else if (ddr3_cfg->rowaddr == 15 &&
1641 sysinfo->ncs == 2)
1642 noc_sched->ddrconf = 9;
1643 else
1644 noc_sched->ddrconf = 3;
1645 } else if (ddr3_cfg->coladdr == 11) {
1646 if (ddr3_cfg->rowaddr == 15 &&
1647 sysinfo->ncs == 2)
1648 noc_sched->ddrconf = 4;
1649 else
1650 noc_sched->ddrconf = 0;
1651 } else if (ddr3_cfg->coladdr == 12) {
1652 if (ddr3_cfg->rowaddr == 14)
1653 noc_sched->ddrconf = 1;
1654 }
1655 }
1656
1657 /* Disable MMDC arbitration/reordering */
1658 mmdc0->maarcr = 0x14420000;
1659 }
1660
Tim Harvey8ab871b2014-06-02 16:13:23 -07001661 /* Step 13: Deassert config request - init complete */
Nikita Kiryanovc4753462014-09-07 18:58:11 +03001662 mmdc0->mdscr = 0x00000000;
Tim Harvey8ab871b2014-06-02 16:13:23 -07001663
1664 /* wait for auto-ZQ calibration to complete */
1665 mdelay(1);
1666}
Peng Fan77e86952015-08-17 16:11:03 +08001667
Eric Nelsonec4fe262016-10-30 16:33:49 -07001668void mmdc_read_calibration(struct mx6_ddr_sysinfo const *sysinfo,
1669 struct mx6_mmdc_calibration *calib)
1670{
1671 struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
1672 struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
1673
1674 calib->p0_mpwldectrl0 = readl(&mmdc0->mpwldectrl0);
1675 calib->p0_mpwldectrl1 = readl(&mmdc0->mpwldectrl1);
1676 calib->p0_mpdgctrl0 = readl(&mmdc0->mpdgctrl0);
1677 calib->p0_mpdgctrl1 = readl(&mmdc0->mpdgctrl1);
1678 calib->p0_mprddlctl = readl(&mmdc0->mprddlctl);
1679 calib->p0_mpwrdlctl = readl(&mmdc0->mpwrdlctl);
1680
1681 if (sysinfo->dsize == 2) {
1682 calib->p1_mpwldectrl0 = readl(&mmdc1->mpwldectrl0);
1683 calib->p1_mpwldectrl1 = readl(&mmdc1->mpwldectrl1);
1684 calib->p1_mpdgctrl0 = readl(&mmdc1->mpdgctrl0);
1685 calib->p1_mpdgctrl1 = readl(&mmdc1->mpdgctrl1);
1686 calib->p1_mprddlctl = readl(&mmdc1->mprddlctl);
1687 calib->p1_mpwrdlctl = readl(&mmdc1->mpwrdlctl);
1688 }
1689}
1690
Peng Fan77e86952015-08-17 16:11:03 +08001691void mx6_dram_cfg(const struct mx6_ddr_sysinfo *sysinfo,
1692 const struct mx6_mmdc_calibration *calib,
1693 const void *ddr_cfg)
1694{
1695 if (sysinfo->ddr_type == DDR_TYPE_DDR3) {
1696 mx6_ddr3_cfg(sysinfo, calib, ddr_cfg);
Peng Fanda7ada02015-08-17 16:11:04 +08001697 } else if (sysinfo->ddr_type == DDR_TYPE_LPDDR2) {
1698 mx6_lpddr2_cfg(sysinfo, calib, ddr_cfg);
Peng Fan77e86952015-08-17 16:11:03 +08001699 } else {
1700 puts("Unsupported ddr type\n");
1701 hang();
1702 }
1703}