coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
dramc_pi_basic_api.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <device/mmio.h>
4 #include <assert.h>
5 #include <console/console.h>
6 #include <delay.h>
7 #include <soc/addressmap.h>
8 #include <soc/dramc_common.h>
9 #include <soc/dramc_register.h>
10 #include <soc/dramc_pi_api.h>
11 #include <soc/dramc_soc.h>
12 #include <soc/emi.h>
13 #include <soc/mt6391.h>
14 #include <soc/pll.h>
15 #include <soc/spm.h>
16 #include <types.h>
17 
18 struct mem_pll {
22 };
23 
24 inline u8 is_dual_rank(u32 channel,
25  const struct mt8173_sdram_params *sdram_params)
26 {
27  /* judge ranks from EMI_CONA[17] (cha) and EMI_CONA[16] (chb) */
28  return (sdram_params->emi_set.cona & (1 << (17 - channel))) ? 1 : 0;
29 }
30 
31 static void mem_pll_pre_init(u32 channel)
32 {
33  write32(&ch[channel].ddrphy_regs->lpddr2_3, 0x1 << 29 | 0x1 << 25 |
34  0xf << 16 | 0xffff);
35 
36  write32(&ch[channel].ddrphy_regs->lpddr2_4, 0x1 << 29 | 0x1 << 25 |
37  0xf << 16 | 0xffff);
38 
39  /* adjust DQS/DQM phase to get best margin */
40  write32(&ch[channel].ddrphy_regs->selph12, 0x1 << 28 | 0xf << 20 |
41  0x1 << 12 | 0xf << 4);
42  /* adjust DQ phase to get best margin */
43  write32(&ch[channel].ddrphy_regs->selph13, 0xffffffff << 0);
44  write32(&ch[channel].ddrphy_regs->selph14, 0xffffffff << 0);
45 
46  /* fix OCV effect */
47  write32(&ch[channel].ddrphy_regs->selph15, 0x1 << 4 | 0xf << 0);
48 
49  /* pll register control by CPU and select internal pipe path */
50  write32(&ch[channel].ddrphy_regs->peri[2], 0x11 << 24 | 0x11 << 16 |
51  0xff << 8 | 0x11 << 0);
52  write32(&ch[channel].ddrphy_regs->peri[3], 0x11 << 24 | 0x51 << 16 |
53  0x11 << 8 | 0x11 << 0);
54 
55  /* enable clock sync and spm control clock */
56  write32(&ch[channel].ddrphy_regs->mempll_divider, 0x9 << 24 |
57  0x1 << 15 |
58  0x2 << 4 |
59  0x1 << 1 |
60  0x1 << 0);
61  /* pll2 enable from CPU control */
62  write32(&ch[channel].ddrphy_regs->mempll05_divider, 0x1 << 27);
63 
64  /* enable chip top memory clock */
65  setbits32(&ch[channel].ddrphy_regs->mempll_divider, 0x1 << 4);
66 
67  /* disable C/A and DQ M_CK clock gating */
68  clrbits32(&ch[channel].ddrphy_regs->ddrphy_cg_ctrl, 0x1 << 2 |
69  0x1 << 1);
70 
71  /* enable spm control clock */
72  clrbits32(&ch[channel].ddrphy_regs->mempll_divider, 0x1 << 15 |
73  0x1 << 0);
74  /* enable dramc 2X mode */
75  setbits32(&ch[channel].ao_regs->ddr2ctl, 1 << 0);
76 
77  /* select internal clock path */
78  write32(&ch[channel].ddrphy_regs->peri[0], 0x21 << 24 | 0x27 << 16 |
79  0x1b << 8 | 0x3 << 0);
80 
81  write32(&ch[channel].ddrphy_regs->peri[1], 0x50 << 24 | 0x96 << 16 |
82  0x6 << 8 | 0x1e << 0);
83 
84  /* trigger to make memory clock correct phase */
85  setbits32(&ch[channel].ddrphy_regs->mempll_divider, 0x1 << 24 |
86  0x1 << 7);
87 
88  if (channel == CHANNEL_A) {
89  /* select memory clock sync for channel A (internal source) */
90  clrbits32(&ch[channel].ddrphy_regs->mempll_divider, 0x1 << 3);
91  }
92 }
93 
94 static void mem_pll_init_set_params(u32 channel)
95 {
96  u32 pattern1, pattern2, pattern3;
97  u32 mempll_ic_3_0, mempll_bp_3_0;
98  u32 mempll_fbdiv_6_0, mempll_m4pdiv_1_0;
99  u32 mempll_br_1_0, mempll_bc_1_0, mempll_ir_3_0;
100 
101  mempll_fbdiv_6_0 = 0x7 << 16;
102  mempll_br_1_0 = 0x1 << 10;
103  mempll_bc_1_0 = 0x0 << 8;
104  mempll_ir_3_0 = 0xc << 28;
105  mempll_ic_3_0 = 0x6 << 8;
106  mempll_bp_3_0 = 0x1 << 12;
107  mempll_m4pdiv_1_0 = 0x0 << 28;
108 
109  write32(&ch[channel].ddrphy_regs->mempll[14], 0x0);
110 
111  write32(&ch[channel].ddrphy_regs->mempll[3], 0x3 << 30 |
112  0x1 << 28);
113  /* mempll 2 config */
114  pattern1 = mempll_ir_3_0 | mempll_fbdiv_6_0 | mempll_ic_3_0;
115  pattern2 = mempll_m4pdiv_1_0;
116  pattern3 = mempll_bp_3_0 | mempll_br_1_0 | mempll_bc_1_0;
117 
118  /* mempll2_autok_en = 1, mempll2_autok_load = 1 */
119  write32(&ch[channel].ddrphy_regs->mempll[5], 0x1 << 26 | 0x3 << 24 |
120  0x1 << 23 | pattern1);
121  write32(&ch[channel].ddrphy_regs->mempll[6], 0x1 << 30 | 0x3 << 26 |
122  0x3 << 14 | pattern2);
123  write32(&ch[channel].ddrphy_regs->mempll[7], 0x1 << 17 | 0x1 << 0 |
124  pattern3);
125  /* mempll 4 */
126  write32(&ch[channel].ddrphy_regs->mempll[11], 0x1 << 26 | 0x3 << 24 |
127  0x1 << 23 | pattern1);
128  write32(&ch[channel].ddrphy_regs->mempll[12], 0x1 << 30 | 0x3 << 26 |
129  0x3 << 14 | pattern2);
130  write32(&ch[channel].ddrphy_regs->mempll[13], 0x1 << 0 | pattern3);
131 
132  /* mempll 3 - enable signal tie together */
133  write32(&ch[channel].ddrphy_regs->mempll[8], 0x1 << 26 | 0x3 << 24 |
134  0x1 << 23 | pattern1);
135  write32(&ch[channel].ddrphy_regs->mempll[9], 0x1 << 30 | 0x3 << 26 |
136  0x3 << 14 | pattern2);
137  write32(&ch[channel].ddrphy_regs->mempll[10], 0x1 << 17 | 0x1 << 0 |
138  pattern3);
139 }
140 
141 static void mem_pll_init_phase_sync(u32 channel)
142 {
143  write32(&ch[channel].ddrphy_regs->mempll_divider, BIT(27) | BIT(24) |
144  BIT(7) | BIT(5) |
145  BIT(4) | BIT(0));
146  /* spm control clock enable */
148  BIT(1));
149 
151  BIT(0));
152 }
153 
154 static void pll_phase_adjust(u32 channel, struct mem_pll *mempll, int reg_offs)
155 {
156  switch (mempll->phase) {
157 
158  case MEMPLL_INIT:
159  /* initial phase: zero out RG_MEPLL(2,3,4)_(REF_DL,FB)_DL */
160  clrbits32(&ch[channel].ddrphy_regs->mempll[reg_offs],
161  0x1f << MEMPLL_REF_DL_SHIFT |
162  0x1f << MEMPLL_FB_DL_SHIFT);
163  break;
164 
165  case MEMPLL_REF_LAG:
166  /* REF lag FBK, delay FBK */
167  clrsetbits32(&ch[channel].ddrphy_regs->mempll[reg_offs],
168  0x1f << MEMPLL_REF_DL_SHIFT |
169  0x1f << MEMPLL_FB_DL_SHIFT,
170  mempll->delay << MEMPLL_FB_DL_SHIFT);
171  break;
172 
173  case MEMPLL_REF_LEAD:
174  /* REF lead FBK, delay REF */
175  clrsetbits32(&ch[channel].ddrphy_regs->mempll[reg_offs],
176  0x1f << MEMPLL_REF_DL_SHIFT |
177  0x1f << MEMPLL_FB_DL_SHIFT,
178  mempll->delay << MEMPLL_REF_DL_SHIFT);
179  };
180 }
181 
182 static void pll_phase_check(u32 channel, struct mem_pll *mempll, int idx)
183 {
184  u32 value = read32(&ch[channel].ddrphy_regs->jmeter_pll_st[idx]);
185  u16 one_count = (u16)((value >> 16) & 0xffff);
186  u16 zero_count = (u16)(value & 0xffff);
187 
188  dramc_dbg("PLL %d, phase %d, one_count %d, zero_count %d\n",
189  (idx + 2), mempll->phase, one_count, zero_count);
190 
191  switch (mempll->phase) {
192 
193  case MEMPLL_INIT:
194  if ((one_count - zero_count) > JMETER_COUNT_N) {
195  /* REF lag FBK */
196  mempll->phase = MEMPLL_REF_LAG;
197  mempll->delay++;
198  } else if ((zero_count - one_count) > JMETER_COUNT_N) {
199  /* REF lead FBK */
200  mempll->phase = MEMPLL_REF_LEAD;
201  mempll->delay++;
202  } else {
203  /* in-phase at initial */
204  mempll->done = 1;
205  }
206  break;
207 
208  case MEMPLL_REF_LAG:
209  if (JMETER_COUNT_N >= (one_count - zero_count)) {
210  mempll->done = 1;
211  } else {
212  mempll->delay++;
213  }
214  break;
215 
216  case MEMPLL_REF_LEAD:
217  if (JMETER_COUNT_N >= (zero_count - one_count)) {
218  mempll->done = 1;
219  } else {
220  mempll->delay++;
221  }
222  }
223 }
224 
225 static void mem_pll_phase_cali(u32 channel)
226 {
227  u32 i;
228 
229  struct mem_pll mempll[3] =
230  {
231  {0, 0, 0},
232  {0, 0, 0},
233  {0, 0, 0},
234  };
235 
236  dramc_dbg("[PLL_Phase_Calib] ===== PLL Phase Calibration: ");
237  dramc_dbg("CHANNEL %d (0: CHA, 1: CHB) =====\n", channel);
238 
239  /* 1. set jitter meter count number to 1024 for mempll 2 3 4 */
240  for (i = 0; i < 3; i++)
241  clrsetbits32(&ch[channel].ddrphy_regs->jmeter[i],
244 
245  while (1) {
246 
247  for (i = 0; i < 3; i++) {
248  if (!mempll[i].done) {
249  pll_phase_adjust(channel, &mempll[i], (i + 2) * 3);
250  }
251  }
252 
253  udelay(20); /* delay 20us for external loop pll stable */
254 
255  /* 2. enable mempll 2 3 4 jitter meter */
256  for (i = 0; i < 3; i++)
257  setbits32(&ch[channel].ddrphy_regs->jmeter[i],
258  JMETER_EN_BIT);
259 
260  /* 3. wait for jitter meter complete */
262 
263  /* 4. check jitter meter counter value for mempll 2 3 4 */
264  for (i = 0; i < 3; i++) {
265  if (!mempll[i].done) {
266  pll_phase_check(channel, &mempll[i], i);
267  }
268  }
269 
270  /* 5. disable mempll 2 3 4 jitter meter */
271  for (i = 0; i < 3; i++)
272  clrbits32(&ch[channel].ddrphy_regs->jmeter[i],
273  JMETER_EN_BIT);
274 
275  /* 6. all done early break */
276  if (mempll[0].done && mempll[1].done && mempll[2].done)
277  break;
278 
279  /* 7. delay line overflow break */
280  for (i = 0; i < 3; i++) {
281  if (mempll[i].delay >= 32) {
282  die("MEMPLL calibration fail\n");
283  }
284  }
285  }
286 
287  dramc_dbg("pll done: ");
288 
289  dramc_dbg("%d, %d, %d\n",
290  mempll[0].done, mempll[1].done, mempll[2].done);
291  dramc_dbg("pll dl: %d, %d, %d\n",
292  mempll[0].delay, mempll[1].delay, mempll[2].delay);
293 }
294 
296 {
297  u32 channel;
298 
299  /* udelay waits for PLL to stabilize in this function */
300  printk(BIOS_DEBUG, "[PLL] mempll_init and cali\n");
301 
302  /* mempll pre_init for two channels */
303  for (channel = 0; channel < CHANNEL_NUM; channel++)
304  mem_pll_pre_init(channel);
305 
306  /* only set once in MPLL */
308 
309  for (channel = 0; channel < CHANNEL_NUM; channel++)
310  mem_pll_init_set_params(channel);
311 
312  udelay(1); /* wait after da_mpll_sdm_iso_en goes low */
313 
314  /* only set once in MPLL */
316 
317  udelay(100);
318 
319  for (channel = 0; channel < CHANNEL_NUM; channel++) {
320 
321  /* mempll_bias_en */
322  write32(&ch[channel].ddrphy_regs->mempll[3], 0xd << 28 |
323  0x1 << 6);
324  udelay(2);
325 
326  /* mempll2_en -> mempll4_en -> mempll3_en */
327  setbits32(&ch[channel].ddrphy_regs->mempll[5], 1 << 0);
328  setbits32(&ch[channel].ddrphy_regs->mempll[11], 1 << 0);
329  setbits32(&ch[channel].ddrphy_regs->mempll[8], 1 << 0);
330 
331  udelay(100);
332 
333  /* mempll_bias_lpf_en */
334  setbits32(&ch[channel].ddrphy_regs->mempll[3], 1 << 7);
335 
336  udelay(30);
337 
338  /* select mempll4 band register */
339  setbits32(&ch[channel].ddrphy_regs->mempll[4], 1 << 26);
340  clrbits32(&ch[channel].ddrphy_regs->mempll[4], 1 << 26);
341 
342  /* PLL ready */
343 
344  /* disable mempll2_en -> mempll4_en -> mempll3_en */
345  clrbits32(&ch[channel].ddrphy_regs->mempll[5], 1 << 0);
346  clrbits32(&ch[channel].ddrphy_regs->mempll[11], 1 << 0);
347  clrbits32(&ch[channel].ddrphy_regs->mempll[8], 1 << 0);
348 
349  /* disable autok mempll2_en -> mempll4_en -> mempll3_en */
350  clrbits32(&ch[channel].ddrphy_regs->mempll[5], 1 << 23);
351  clrbits32(&ch[channel].ddrphy_regs->mempll[11], 1 << 23);
352  clrbits32(&ch[channel].ddrphy_regs->mempll[8], 1 << 23);
353 
354  udelay(1);
355 
356  /* mempll[2->4->3]_fb_mck_sel=1 (switch to outer loop) */
357  setbits32(&ch[channel].ddrphy_regs->mempll[6], 1 << 25);
358  setbits32(&ch[channel].ddrphy_regs->mempll[12], 1 << 25);
359  setbits32(&ch[channel].ddrphy_regs->mempll[9], 1 << 25);
360 
361  udelay(1);
362 
363  /* enable mempll2_en -> mempll4_en -> mempll3_en */
364  setbits32(&ch[channel].ddrphy_regs->mempll[5], 1 << 0);
365  setbits32(&ch[channel].ddrphy_regs->mempll[11], 1 << 0);
366  setbits32(&ch[channel].ddrphy_regs->mempll[8], 1 << 0);
367  }
368 
369  /* mempll new power-on */
370  write32(&mtk_spm->poweron_config_set, 0x1 << 0 |
371  SPM_PROJECT_CODE << 16);
372  /* request mempll reset/pdn mode */
373  setbits32(&mtk_spm->power_on_val0, 0x1 << 27);
374 
375  udelay(2);
376 
377  /* unrequest mempll reset/pdn mode and wait settle */
378  clrbits32(&mtk_spm->power_on_val0, 0x1 << 27);
379 
380  udelay(31); /* PLL ready */
381 
382  for (channel = 0; channel < CHANNEL_NUM; channel++)
383  mem_pll_init_phase_sync(channel);
384 
385  udelay(1);
386 
387  /* mempll calibration for two channels */
388  for (channel = 0; channel < CHANNEL_NUM; channel++)
389  mem_pll_phase_cali(channel);
390 
391  div2_phase_sync(); /* phase sync for channel B */
392 
393  mt_mem_pll_mux();
394 }
395 
397 {
398  /* txdly_cs, txdly_cs1 */
399  write32(&ch[channel].ao_regs->selph1, 0x0);
400  /* txdly_dqsgate, txdly_dqsgate_p1 */
401  write32(&ch[channel].ao_regs->selph2, 0x3 << 20 | 0x2 << 12);
402  /* txldy_ra* */
403  write32(&ch[channel].ao_regs->selph3, 0x0);
404  /* txldy_ra* */
405  write32(&ch[channel].ao_regs->selph4, 0x0);
406 
407  /* setting of write latency (WL=8) */
408  write32(&ch[channel].ao_regs->selph7, 0x3333 << 16 | 0x3333);
409  write32(&ch[channel].ao_regs->selph8, 0x3333 << 16 | 0x3333);
410  write32(&ch[channel].ao_regs->selph9, 0x3333 << 16 | 0x3333);
411  write32(&ch[channel].ao_regs->selph10, 0x5555 << 16 | 0xffff);
412  write32(&ch[channel].ao_regs->selph11, 0x55 << 16 | 0xff);
413 
414  write32(&ch[channel].ao_regs->selph5, 0x1 << 26 | 0x2 << 22 |
415  0x1 << 20 | 0x5 << 16 |
416  0x5555);
417 
418  write32(&ch[channel].ao_regs->selph6_1, 0x4 << 8 | 0x3 << 4 |
419  0x2 << 0);
420 
421  write32(&ch[channel].ao_regs->ac_time_05t,
422  sdram_params->ac_timing.actim05t);
423 }
424 
425 static void mrs_write(int channel, int rank, u32 mrs_value, unsigned int dly)
426 {
427  write32(&ch[channel].ao_regs->mrs, rank << 28 | mrs_value);
428 
429  write32(&ch[channel].ao_regs->spcmd, 0x1);
430  udelay(dly);
431  write32(&ch[channel].ao_regs->spcmd, 0x0);
432 }
433 
434 static void dramc_set_mrs_value(int channel, int rank,
435  const struct mt8173_sdram_params *sdram_params)
436 {
437  /* MR63 -> Reset, Wait >=10us if not check DAI */
438  mrs_write(channel, rank, sdram_params->mrs_set.mrs_63, 10);
439  /* MR10 -> ZQ Init, tZQINIT>=1us */
440  mrs_write(channel, rank, sdram_params->mrs_set.mrs_10, 1);
441  /* MR3 driving strength set to max */
442  mrs_write(channel, rank, sdram_params->mrs_set.mrs_3, 1);
443  /* MR1 */
444  mrs_write(channel, rank, sdram_params->mrs_set.mrs_1, 1);
445  /* MR2 */
446  mrs_write(channel, rank, sdram_params->mrs_set.mrs_2, 1);
447  /* MR11 ODT disable */
448  mrs_write(channel, rank, sdram_params->mrs_set.mrs_11, 1);
449 }
450 
451 void dramc_init(u32 channel, const struct mt8173_sdram_params *sdram_params)
452 {
453  u32 bit, dual_rank_set;
454 
455  const struct mt8173_calib_params *calib_params;
456 
457  dual_rank_set = is_dual_rank(channel, sdram_params);
458  calib_params = &sdram_params->calib_params;
459 
460  write32(&ch[channel].ddrphy_regs->peri[2], 0x1 << 12 |
461  0x1 << 4);
462 
463  write32(&ch[channel].ddrphy_regs->peri[3], 0x0);
464 
465  write32(&ch[channel].ao_regs->test2_4,
466  sdram_params->ac_timing.test2_4);
467 
468  write32(&ch[channel].ao_regs->clk1delay, 0x1 << 23 |
469  0x1 << 22 |
470  0x1 << 21);
471 
472  /* rank config */
473  assert((sdram_params->ac_timing.rkcfg & 0x1) == dual_rank_set);
474  write32(&ch[channel].ao_regs->rkcfg,
475  sdram_params->ac_timing.rkcfg);
476 
477  /* pimux */
478  write32(&ch[channel].ao_regs->mckdly, 0x1 << 30 |
479  0x1 << 20 |
480  0x1 << 4);
481 
482  write32(&ch[channel].ddrphy_regs->mckdly, 0x1 << 8);
483 
484  write32(&ch[channel].ao_regs->padctl4, 0x1 << 0);
485 
486  /* tCKEH/tCKEL extend 1T */
487  write32(&ch[channel].ao_regs->dummy, 0x1 << 31 |
488  0x3 << 10 |
489  0x1 << 4);
490 
491  /* driving control */
492  write32(&ch[channel].ao_regs->iodrv6, DEFAULT_DRIVING |
493  DRIVING_DS2_0 << 20 |
494  DRIVING_DS2_0 << 4);
495 
497  DRIVING_DS2_0 << 20);
498 
500  DRIVING_DS2_0 << 4);
501 
502  /* enable dqs signal output */
503  write32(&ch[channel].ddrphy_regs->ioctl, 0x0);
504 
505  /* rank 0 dqs gating delay */
506  write32(&ch[channel].ao_regs->dqsien[0], 0x40 << 24 |
507  0x40 << 16 |
508  0x40 << 8 |
509  0x40 << 0);
510 
511  write32(&ch[channel].ao_regs->dqsctl1, 0x1 << 28 |
512  0x5 << 24);
513 
514  write32(&ch[channel].ao_regs->dqsctl2, 0x5 << 0);
515  write32(&ch[channel].ao_regs->phyctl1, 0x1 << 25);
516  write32(&ch[channel].ao_regs->gddr3ctl1, 0x1 << 24);
517  write32(&ch[channel].ddrphy_regs->gddr3ctl1, 0x1 << 28);
518  write32(&ch[channel].ao_regs->arbctl0, 0x80 << 0);
519 
520  /* enable clock pad 0 */
521  write32(&ch[channel].ao_regs->clkctl, 0x1 << 28);
522 
523  udelay(1);
524 
525  write32(&ch[channel].ao_regs->conf1,
526  sdram_params->ac_timing.conf1);
527 
528  /* bit 17,18 would bypass some dummy path */
529  write32(&ch[channel].ddrphy_regs->dqsgctl, 0x1 << 31 |
530  0x1 << 30 |
531  0x1 << 17 |
532  0x1 << 18 |
533  0x1 << 4 |
534  0x1 << 0);
535 
536  write32(&ch[channel].ao_regs->dqscal0, 0x0);
537  write32(&ch[channel].ddrphy_regs->dqscal0, 0x0);
538 
539  write32(&ch[channel].ao_regs->actim0,
540  sdram_params->ac_timing.actim);
541 
542  write32(&ch[channel].ao_regs->misctl0,
543  sdram_params->ac_timing.misctl0);
544  write32(&ch[channel].ddrphy_regs->misctl0,
545  sdram_params->ac_timing.misctl0);
546 
547  write32(&ch[channel].ao_regs->perfctl0, 0x1 << 20);
548 
549  write32(&ch[channel].ao_regs->ddr2ctl,
550  sdram_params->ac_timing.ddr2ctl);
551  write32(&ch[channel].ddrphy_regs->ddr2ctl,
552  sdram_params->ac_timing.ddr2ctl);
553 
554  write32(&ch[channel].ao_regs->misc, 0xb << 8 |
555  0x1 << 7 |
556  0x1 << 6 |
557  0x1 << 5);
558 
559  write32(&ch[channel].ao_regs->dllconf, 0xf << 28 |
560  0x1 << 24);
561 
562  write32(&ch[channel].ao_regs->actim1,
563  sdram_params->ac_timing.actim1);
564 
565  write32(&ch[channel].ddrphy_regs->dqsisel, 0x0);
566 
567  /* disable ODT before ZQ calibration */
568  write32(&ch[channel].ao_regs->wodt, 0x1 << 0);
569 
570  write32(&ch[channel].ao_regs->padctl4, 0x1 << 2 |
571  0x1 << 0);
572 
573  udelay(200); /* tINIT3 > 200us */
574 
575  write32(&ch[channel].ao_regs->gddr3ctl1, 0x1 << 24 |
576  0x1 << 20);
577 
578  write32(&ch[channel].ddrphy_regs->gddr3ctl1, 0x1 << 28);
579 
580  /* set mode register value */
581  dramc_set_mrs_value(channel, 0, sdram_params);
582 
583  if (dual_rank_set)
584  dramc_set_mrs_value(channel, 1, sdram_params);
585 
586  write32(&ch[channel].ao_regs->gddr3ctl1,
587  sdram_params->ac_timing.gddr3ctl1);
588  write32(&ch[channel].ddrphy_regs->gddr3ctl1,
589  sdram_params->ac_timing.gddr3ctl1);
590 
591  write32(&ch[channel].ao_regs->dramc_pd_ctrl,
592  sdram_params->ac_timing.pd_ctrl);
593 
594  write32(&ch[channel].ao_regs->padctl4, 0x1 << 0);
595  write32(&ch[channel].ao_regs->perfctl0, 0x1 << 20 | 0x1 << 0);
596  write32(&ch[channel].ao_regs->zqcs, 0xa << 8 | 0x56 << 0);
597  write32(&ch[channel].ddrphy_regs->padctl1, 0x0);
598 
599  write32(&ch[channel].ao_regs->test2_3,
600  sdram_params->ac_timing.test2_3);
601 
602  write32(&ch[channel].ao_regs->conf2,
603  sdram_params->ac_timing.conf2);
604 
605  write32(&ch[channel].ddrphy_regs->padctl2, 0x0);
606 
607  /* DISABLE_DRVREF */
608  write32(&ch[channel].ao_regs->ocdk, 0x0);
609  write32(&ch[channel].ddrphy_regs->ocdk, 0x0);
610 
611  write32(&ch[channel].ao_regs->r1deldly, 0x12 << 24 |
612  0x12 << 16 |
613  0x12 << 8 |
614  0x12 << 0);
615 
616  write32(&ch[channel].ao_regs->padctl7, 0x0);
617 
618  /* CLKTDN, DS0TDN, DS1TDN, DS2TDN, DS3TDN */
619  setbits32(&ch[channel].ddrphy_regs->tdsel[2], 0x1 << 31 |
620  0x1 << 29 |
621  0x1 << 27 |
622  0x1 << 25 |
623  0x1 << 1);
624  /* DISABLE_PERBANK_REFRESH */
625  clrbits32(&ch[channel].ao_regs->rkcfg, 0x1 << 7);
626 
627  /* clear R_DMREFTHD to reduce MR4 wait refresh queue time */
628  clrbits32(&ch[channel].ao_regs->conf2, 0x7 << 24);
629 
630  /* duty default value */
631  write32(&ch[channel].ddrphy_regs->phyclkduty, 0x1 << 28 |
632  0x1 << 16);
633 
634  if (!dual_rank_set) {
635  /* single rank, CKE1 always off */
636  setbits32(&ch[channel].ao_regs->gddr3ctl1, 0x1 << 21);
637  }
638 
639  /* default dqs rx perbit input delay */
640  write32(&ch[channel].ao_regs->r0deldly,
641  calib_params->rx_dqs_dly[channel]);
642 
643  write32(&ch[channel].ao_regs->r1deldly,
644  calib_params->rx_dqs_dly[channel]);
645 
646  for (bit = 0; bit < DQS_BIT_NUMBER; bit++)
647  write32(&ch[channel].ao_regs->dqidly[bit],
648  calib_params->rx_dq_dly[channel][bit]);
649 }
650 
651 void div2_phase_sync(void)
652 {
654  1 << MEMCLKENB_SHIFT);
655  udelay(1);
656 
658  1 << MEMCLKENB_SHIFT);
659 }
660 
661 void dramc_phy_reset(u32 channel)
662 {
663  /* reset phy */
664  setbits32(&ch[channel].ddrphy_regs->phyctl1,
665  1 << PHYCTL1_PHYRST_SHIFT);
666 
667  /* read data counter reset */
668  setbits32(&ch[channel].ao_regs->gddr3ctl1,
670 
671  udelay(1); /* delay 1ns */
672 
673  clrbits32(&ch[channel].ao_regs->gddr3ctl1,
675 
676  clrbits32(&ch[channel].ddrphy_regs->phyctl1,
677  1 << PHYCTL1_PHYRST_SHIFT);
678 }
679 
681  const struct mt8173_sdram_params *sdram_params)
682 {
683  setbits32(&ch[channel].ddrphy_regs->dqsgctl,
684  BIT(17)|BIT(18));
685 
686  /* enable hw gating */
687  setbits32(&ch[channel].ao_regs->dqscal0,
689 
690  /* if frequency >1600, tCKE should >7 clk */
691  setbits32(&ch[channel].ao_regs->dummy, 0x1 << 4);
692 
693  if (sdram_params->dram_freq * 2 < 1600 * MHz)
694  die("set tCKE error in runtime config");
695 
696  /* DDRPHY C/A and DQ M_CK clock gating enable */
697  setbits32(&ch[channel].ddrphy_regs->ddrphy_cg_ctrl, 0x1 << 2 |
698  0x1 << 1);
699 
700  setbits32(&ch[channel].ao_regs->perfctl0, BIT(19) | BIT(14) |
701  BIT(11) | BIT(10) |
702  BIT(9) | BIT(8) |
703  BIT(4) | BIT(0));
704  /* ZQCS_ENABLE */
705  if (sdram_params->emi_set.cona & 0x1) {
706  /* dual channel, clear ZQCSCNT */
707  clrbits32(&ch[channel].ao_regs->spcmd, 0xff << 16);
708  /* set ZQCSMASK for different channels */
709  if (channel == CHANNEL_A) {
710  clrbits32(&ch[channel].ao_regs->perfctl0, 0x1 << 24);
711  } else {
712  setbits32(&ch[channel].ao_regs->perfctl0, 0x1 << 24);
713  }
714  /* enable ZQCSDUAL */
715  setbits32(&ch[channel].ao_regs->perfctl0, 0x1 << 25);
716  } else {
717  /* single channel, set ZQCSCNT */
718  setbits32(&ch[channel].ao_regs->spcmd, 0x8 << 16);
719  }
720 }
721 
723 {
724  u32 msk;
725 
726  msk = BIT(7) | BIT(11) | BIT(15);
727  clrbits32(&mtk_apmixed->ap_pll_con3, msk);
728 
729  msk = BIT(0) | BIT(4) | BIT(8);
730  clrbits32(&ch[CHANNEL_A].ddrphy_regs->peri[3], msk);
731 
732  msk = BIT(0) | BIT(8);
733  clrbits32(&ch[CHANNEL_B].ddrphy_regs->peri[3], msk);
734 
735  msk = BIT(0) | BIT(9) | BIT(10) | BIT(11) | BIT(16) | BIT(24);
736  clrbits32(&ch[CHANNEL_A].ddrphy_regs->peri[2], msk);
737  clrbits32(&ch[CHANNEL_B].ddrphy_regs->peri[2], msk);
738 }
739 
741 {
742  u32 val;
743 
744  val = BIT(7) | BIT(11) | BIT(15);
745  setbits32(&mtk_apmixed->ap_pll_con3, val);
746 
747  val = BIT(0) | BIT(4) | BIT(8);
749 
750  val = BIT(0) | BIT(8);
752 
753  val = BIT(0) | BIT(9) | BIT(10) | BIT(11) | BIT(16) | BIT(24);
756 }
757 
758 u32 dramc_engine2(u32 channel, enum dram_tw_op wr, u32 test2_1, u32 test2_2,
759  u8 testaudpat, u8 log2loopcount)
760 {
761  u32 value;
762 
763  if (log2loopcount > 15)
764  die("Invalid loopcount of engine2!");
765 
766  /* Disable Test Agent1, Test Agent2 write/read */
767  clrbits32(&ch[channel].ao_regs->conf2, CONF2_TEST1_EN |
770 
771  /* 1. set pattern, base address, offset address */
772  write32(&ch[channel].nao_regs->test2_1, test2_1);
773  write32(&ch[channel].nao_regs->test2_2, test2_2);
774 
775  /* 2. select test pattern */
776  /* TESTXTALKPAT | TESTAUDPAT
777  * ISI 0 | 0
778  * AUD 0 | 1
779  * XTALK 1 | 0
780  * UNKNOWN 1 | 1
781  */
782  switch (testaudpat) {
783  case XTALK:
784  /* TESTAUDPAT = 0 */
785  clrbits32(&ch[channel].ao_regs->test2_3,
787  /* TESTXTALKPAT = 1, select xtalk pattern
788  * TESTAUDMODE = 0, read only
789  * TESTAUDBITINV = 0, no bit inversion
790  */
791  clrsetbits32(&ch[channel].ao_regs->test2_4,
795  break;
796  case AUDIO:
797  /* TESTAUDPAT = 1 */
798  setbits32(&ch[channel].ao_regs->test2_3,
800  /* TESTXTALKPAT = 0
801  * TESTAUDINIT = 0x11
802  * TESTAUDINC = 0x0d
803  * TESTAUDBITINV = 1
804  * TESTAUDMODE = 1
805  */
806  clrsetbits32(&ch[channel].ao_regs->test2_4,
812  0x11 << TEST2_4_TESTAUDINIT_SHIFT |
813  0xd << TEST2_4_TESTAUDINC_SHIFT);
814 
815  break;
816  case ISI:
817  /* TESTAUDPAT = 0 */
818  clrbits32(&ch[channel].ao_regs->test2_3,
820  /* TESTXTALKPAT = 0 */
821  clrbits32(&ch[channel].ao_regs->test2_4,
823  }
824 
825  /* 3. set loop number */
827  log2loopcount << TEST2_3_TESTCNT_SHIFT);
828 
829  /* 4. enable read/write test */
830  if (wr == TE_OP_READ_CHECK) {
831  if ((testaudpat == 1) || (testaudpat == 2)) {
832  /* if audio pattern, enable read only */
833  /* (disable write after read), */
834  /* AUDMODE=0x48[15]=0 */
835  clrbits32(&ch[channel].ao_regs->test2_4,
837  }
838 
839  /* enable read, 0x008[30:30] */
841  } else if (wr == TE_OP_WRITE_READ_CHECK) {
842  /* enable write, 0x008[31:31] */
844 
845  /* check "read data compare ready" bit */
846  do {
847  value = read32(&ch[channel].nao_regs->testrpt);
848  } while ((value & (1 << TESTRPT_DM_CMP_CPT_SHIFT)) == 0);
849 
850  /* Disable Test Agent2 write and enable Test Agent2 read */
853  }
854 
855  /* 5 check "read data compare ready" bit */
856  do {
857  value = read32(&ch[channel].nao_regs->testrpt);
858  } while ((value & (1 << TESTRPT_DM_CMP_CPT_SHIFT)) == 0);
859 
860  /* delay 10ns after ready check from DE suggestion (1us here) */
861  udelay(1);
862 
863  /* read CMP_ERR result */
864  value = read32(&ch[channel].nao_regs->cmp_err);
865 
866  /* 6 disable read */
868 
869  /* return CMP_ERR result, pass: 0, failure: otherwise */
870  return value;
871 }
pte_t value
Definition: mmu.c:91
static void write32(void *addr, uint32_t val)
Definition: mmio.h:40
static uint32_t read32(const void *addr)
Definition: mmio.h:22
#define assert(statement)
Definition: assert.h:74
#define MHz
Definition: helpers.h:80
#define printk(level,...)
Definition: stdlib.h:16
void __noreturn die(const char *fmt,...)
Definition: die.c:17
void delay(unsigned int secs)
Definition: delay.c:8
#define dramc_dbg(_x_...)
Definition: dramc_common.h:11
#define BIT(nr)
Definition: ec_commands.h:45
#define setbits32(addr, set)
Definition: mmio.h:21
#define clrsetbits32(addr, clear, set)
Definition: mmio.h:16
#define clrbits32(addr, clear)
Definition: mmio.h:26
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
void transfer_to_reg_control(void)
static void mem_pll_pre_init(u32 channel)
void mem_pll_init(const struct mt8173_sdram_params *sdram_params)
static void mem_pll_init_set_params(u32 channel)
void dramc_init(u32 channel, const struct mt8173_sdram_params *sdram_params)
static void mrs_write(int channel, int rank, u32 mrs_value, unsigned int dly)
void dramc_pre_init(u32 channel, const struct mt8173_sdram_params *sdram_params)
u8 is_dual_rank(u32 channel, const struct mt8173_sdram_params *sdram_params)
void div2_phase_sync(void)
static void mem_pll_phase_cali(u32 channel)
void transfer_to_spm_control(void)
void dramc_phy_reset(u32 channel)
u32 dramc_engine2(u32 channel, enum dram_tw_op wr, u32 test2_1, u32 test2_2, u8 testaudpat, u8 log2loopcount)
static void pll_phase_adjust(u32 channel, struct mem_pll *mempll, int reg_offs)
static void dramc_set_mrs_value(int channel, int rank, const struct mt8173_sdram_params *sdram_params)
void dramc_runtime_config(u32 channel, const struct mt8173_sdram_params *sdram_params)
static void pll_phase_check(u32 channel, struct mem_pll *mempll, int idx)
static void mem_pll_init_phase_sync(u32 channel)
@ MEMPLL_REF_LEAD
Definition: dramc_pi_api.h:57
@ MEMPLL_REF_LAG
Definition: dramc_pi_api.h:56
@ MEMPLL_INIT
Definition: dramc_pi_api.h:55
@ ISI
Definition: dramc_pi_api.h:51
@ XTALK
Definition: dramc_pi_api.h:50
@ AUDIO
Definition: dramc_pi_api.h:49
dram_tw_op
Definition: dramc_pi_api.h:83
@ TE_OP_WRITE_READ_CHECK
Definition: dramc_pi_api.h:84
@ TE_OP_READ_CHECK
Definition: dramc_pi_api.h:85
@ JMETER_WAIT_DONE_US
Definition: dramc_pi_api.h:19
@ JMETER_COUNT_N
Definition: dramc_pi_api.h:17
@ JMETER_COUNT
Definition: dramc_pi_api.h:16
struct dramc_ddrphy_regs * ddrphy_regs
struct dramc_nao_regs * nao_regs
#define DEFAULT_DRIVING
Definition: dramc_register.h:9
#define DRIVING_DS2_0
Definition: dramc_register.h:8
struct dramc_ao_regs * ao_regs
static struct dramc_channel const ch[2]
@ GDDR3CTL1_RDATRST_SHIFT
@ JMETER_COUNTER_SHIFT
@ TEST2_4_TESTAUDBITINV_EN
@ TEST2_4_TESTAUDMODE_EN
@ TEST2_4_TESTAUDINC_SHIFT
@ TEST2_3_TESTCNT_SHIFT
@ CONF2_TEST2W_EN
@ PHYCTL1_PHYRST_SHIFT
@ JMETER_COUNTER_MASK
@ TEST2_3_TESTAUDPAT_EN
@ CONF2_TEST1_EN
@ JMETER_EN_BIT
@ MEMPLL_REF_DL_SHIFT
@ TEST2_4_TESTAUDINIT_SHIFT
@ DQSCAL0_STBCALEN_SHIFT
@ TEST2_4_TESTAUDINC_MASK
@ CONF2_TEST2R_EN
@ MEMCLKENB_SHIFT
@ TESTRPT_DM_CMP_CPT_SHIFT
@ TEST2_4_TESTAUDINIT_MASK
@ TEST2_4_TESTXTALKPAT_EN
@ MEMPLL_FB_DL_SHIFT
@ TEST2_3_TESTCNT_MASK
@ CHANNEL_A
Definition: dramc_soc.h:7
@ CHANNEL_NUM
Definition: dramc_soc.h:9
@ CHANNEL_B
Definition: dramc_soc.h:8
void mt_mem_pll_config_pre(const struct mt8173_sdram_params *sdram_params)
Definition: pll.c:425
void mt_mem_pll_mux(void)
Definition: pll.c:445
void mt_mem_pll_config_post(void)
Definition: pll.c:439
static struct mtk_spm_regs *const mtk_spm
Definition: spm.h:154
@ SPM_PROJECT_CODE
Definition: spm.h:11
#define DQS_BIT_NUMBER
Definition: dramc_soc.h:53
#define mtk_apmixed
Definition: pll_common.h:12
uint32_t u32
Definition: stdint.h:51
uint16_t u16
Definition: stdint.h:48
uint8_t u8
Definition: stdint.h:45
uint32_t dqscal0
uint32_t dqidly[9]
uint32_t r1deldly
uint32_t test2_3
uint32_t selph6_1
uint32_t dqsien[2]
uint32_t selph10
uint32_t test2_4
uint32_t dqsctl2
uint32_t perfctl0
uint32_t arbctl0
uint32_t clk1delay
uint32_t phyctl1
uint32_t ac_time_05t
uint32_t drvctl1
uint32_t dqsctl1
uint32_t r0deldly
uint32_t selph11
uint32_t misctl0
uint32_t gddr3ctl1
uint32_t dramc_pd_ctrl
uint32_t ddr2ctl
uint32_t padctl7
uint32_t dllconf
uint32_t padctl4
uint32_t jmeter_pll_st[3]
uint32_t jmeter[3]
uint32_t mempll[15]
uint32_t mempll05_divider
u32 rx_dq_dly[CHANNEL_NUM][DQS_BIT_NUMBER]
Definition: emi.h:56
u32 rx_dqs_dly[CHANNEL_NUM]
Definition: emi.h:55
u32 power_on_val0
Definition: spm.h:26
u32 poweron_config_set
Definition: spm.h:24
Defines the SDRAM parameter structure.
Definition: emi.h:15
u8 val
Definition: sys.c:300
void udelay(uint32_t us)
Definition: udelay.c:15