blob: c15528e84a3c1c003d906fce765959b520cb58ba [file] [log] [blame]
Caesar Wanga8456902016-10-27 01:12:34 +08001/*
2 * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
Xing Zheng93280b72016-10-26 21:25:26 +080031#include <arch_helpers.h>
Caesar Wanga8456902016-10-27 01:12:34 +080032#include <debug.h>
33#include <mmio.h>
Xing Zheng93280b72016-10-26 21:25:26 +080034#include <m0_ctl.h>
Caesar Wanga8456902016-10-27 01:12:34 +080035#include <plat_private.h>
36#include "dfs.h"
37#include "dram.h"
38#include "dram_spec_timing.h"
39#include "string.h"
40#include "soc.h"
41#include "pmu.h"
42
43#include <delay_timer.h>
44
Derek Basehore0e8909d2016-11-09 18:28:19 -080045#define ENPER_CS_TRAINING_FREQ (666)
46#define TDFI_LAT_THRESHOLD_FREQ (928)
Derek Basehoreb1065122016-10-20 22:09:22 -070047#define PHY_DLL_BYPASS_FREQ (260)
Caesar Wanga8456902016-10-27 01:12:34 +080048
Caesar Wanga8456902016-10-27 01:12:34 +080049static const struct pll_div dpll_rates_table[] = {
50
51 /* _mhz, _refdiv, _fbdiv, _postdiv1, _postdiv2 */
Xing Zheng93280b72016-10-26 21:25:26 +080052 {.mhz = 928, .refdiv = 1, .fbdiv = 116, .postdiv1 = 3, .postdiv2 = 1},
Caesar Wanga8456902016-10-27 01:12:34 +080053 {.mhz = 800, .refdiv = 1, .fbdiv = 100, .postdiv1 = 3, .postdiv2 = 1},
54 {.mhz = 732, .refdiv = 1, .fbdiv = 61, .postdiv1 = 2, .postdiv2 = 1},
55 {.mhz = 666, .refdiv = 1, .fbdiv = 111, .postdiv1 = 4, .postdiv2 = 1},
56 {.mhz = 600, .refdiv = 1, .fbdiv = 50, .postdiv1 = 2, .postdiv2 = 1},
57 {.mhz = 528, .refdiv = 1, .fbdiv = 66, .postdiv1 = 3, .postdiv2 = 1},
58 {.mhz = 400, .refdiv = 1, .fbdiv = 50, .postdiv1 = 3, .postdiv2 = 1},
59 {.mhz = 300, .refdiv = 1, .fbdiv = 50, .postdiv1 = 4, .postdiv2 = 1},
60 {.mhz = 200, .refdiv = 1, .fbdiv = 50, .postdiv1 = 3, .postdiv2 = 2},
61};
62
Caesar Wanga8456902016-10-27 01:12:34 +080063struct rk3399_dram_status {
64 uint32_t current_index;
65 uint32_t index_freq[2];
Derek Basehoree13bc542017-02-24 14:31:36 +080066 uint32_t boot_freq;
Caesar Wanga8456902016-10-27 01:12:34 +080067 uint32_t low_power_stat;
68 struct timing_related_config timing_config;
69 struct drv_odt_lp_config drv_odt_lp_cfg;
70};
71
Derek Basehoree13bc542017-02-24 14:31:36 +080072struct rk3399_saved_status {
73 uint32_t freq;
74 uint32_t low_power_stat;
75 uint32_t odt;
76};
77
Caesar Wanga8456902016-10-27 01:12:34 +080078static struct rk3399_dram_status rk3399_dram_status;
Derek Basehoree13bc542017-02-24 14:31:36 +080079static struct rk3399_saved_status rk3399_suspend_status;
Derek Basehoreb1065122016-10-20 22:09:22 -070080static uint32_t wrdqs_delay_val[2][2][4];
Caesar Wanga8456902016-10-27 01:12:34 +080081
82static struct rk3399_sdram_default_config ddr3_default_config = {
83 .bl = 8,
84 .ap = 0,
Caesar Wanga8456902016-10-27 01:12:34 +080085 .burst_ref_cnt = 1,
86 .zqcsi = 0
87};
88
Caesar Wanga8456902016-10-27 01:12:34 +080089static struct rk3399_sdram_default_config lpddr3_default_config = {
90 .bl = 8,
91 .ap = 0,
Caesar Wanga8456902016-10-27 01:12:34 +080092 .burst_ref_cnt = 1,
93 .zqcsi = 0
94};
95
Caesar Wanga8456902016-10-27 01:12:34 +080096static struct rk3399_sdram_default_config lpddr4_default_config = {
97 .bl = 16,
98 .ap = 0,
Caesar Wanga8456902016-10-27 01:12:34 +080099 .caodt = 240,
100 .burst_ref_cnt = 1,
101 .zqcsi = 0
102};
103
Caesar Wanga8456902016-10-27 01:12:34 +0800104static uint32_t get_cs_die_capability(struct rk3399_sdram_params *sdram_config,
105 uint8_t channel, uint8_t cs)
106{
107 struct rk3399_sdram_channel *ch = &sdram_config->ch[channel];
108 uint32_t bandwidth;
109 uint32_t die_bandwidth;
110 uint32_t die;
111 uint32_t cs_cap;
112 uint32_t row;
113
114 row = cs == 0 ? ch->cs0_row : ch->cs1_row;
115 bandwidth = 8 * (1 << ch->bw);
116 die_bandwidth = 8 * (1 << ch->dbw);
117 die = bandwidth / die_bandwidth;
118 cs_cap = (1 << (row + ((1 << ch->bk) / 4 + 1) + ch->col +
119 (bandwidth / 16)));
120 if (ch->row_3_4)
121 cs_cap = cs_cap * 3 / 4;
122
123 return (cs_cap / die);
124}
125
Derek Basehoreff461d02016-10-20 20:46:43 -0700126static void get_dram_drv_odt_val(uint32_t dram_type,
Caesar Wanga8456902016-10-27 01:12:34 +0800127 struct drv_odt_lp_config *drv_config)
128{
Derek Basehoreff461d02016-10-20 20:46:43 -0700129 uint32_t tmp;
130 uint32_t mr1_val, mr3_val, mr11_val;
Caesar Wanga8456902016-10-27 01:12:34 +0800131
132 switch (dram_type) {
133 case DDR3:
Derek Basehoreff461d02016-10-20 20:46:43 -0700134 mr1_val = (mmio_read_32(CTL_REG(0, 133)) >> 16) & 0xffff;
135 tmp = ((mr1_val >> 1) & 1) | ((mr1_val >> 4) & 1);
136 if (tmp)
137 drv_config->dram_side_drv = 34;
138 else
139 drv_config->dram_side_drv = 40;
140 tmp = ((mr1_val >> 2) & 1) | ((mr1_val >> 5) & 1) |
141 ((mr1_val >> 7) & 1);
142 if (tmp == 0)
143 drv_config->dram_side_dq_odt = 0;
144 else if (tmp == 1)
145 drv_config->dram_side_dq_odt = 60;
146 else if (tmp == 3)
147 drv_config->dram_side_dq_odt = 40;
148 else
149 drv_config->dram_side_dq_odt = 120;
Caesar Wanga8456902016-10-27 01:12:34 +0800150 break;
151 case LPDDR3:
Derek Basehoreff461d02016-10-20 20:46:43 -0700152 mr3_val = mmio_read_32(CTL_REG(0, 138)) & 0xf;
153 mr11_val = (mmio_read_32(CTL_REG(0, 139)) >> 24) & 0x3;
154 if (mr3_val == 0xb)
155 drv_config->dram_side_drv = 3448;
156 else if (mr3_val == 0xa)
157 drv_config->dram_side_drv = 4048;
158 else if (mr3_val == 0x9)
159 drv_config->dram_side_drv = 3440;
160 else if (mr3_val == 0x4)
161 drv_config->dram_side_drv = 60;
162 else if (mr3_val == 0x3)
163 drv_config->dram_side_drv = 48;
164 else if (mr3_val == 0x2)
165 drv_config->dram_side_drv = 40;
166 else
167 drv_config->dram_side_drv = 34;
Caesar Wanga8456902016-10-27 01:12:34 +0800168
Derek Basehoreff461d02016-10-20 20:46:43 -0700169 if (mr11_val == 1)
170 drv_config->dram_side_dq_odt = 60;
171 else if (mr11_val == 2)
172 drv_config->dram_side_dq_odt = 120;
173 else if (mr11_val == 0)
174 drv_config->dram_side_dq_odt = 0;
175 else
176 drv_config->dram_side_dq_odt = 240;
Caesar Wanga8456902016-10-27 01:12:34 +0800177 break;
178 case LPDDR4:
179 default:
Derek Basehoreff461d02016-10-20 20:46:43 -0700180 mr3_val = (mmio_read_32(CTL_REG(0, 138)) >> 3) & 0x7;
181 mr11_val = (mmio_read_32(CTL_REG(0, 139)) >> 24) & 0xff;
Caesar Wanga8456902016-10-27 01:12:34 +0800182
Derek Basehoreff461d02016-10-20 20:46:43 -0700183 if ((mr3_val == 0) || (mr3_val == 7))
184 drv_config->dram_side_drv = 40;
185 else
186 drv_config->dram_side_drv = 240 / mr3_val;
Caesar Wanga8456902016-10-27 01:12:34 +0800187
Derek Basehoreff461d02016-10-20 20:46:43 -0700188 tmp = mr11_val & 0x7;
189 if ((tmp == 7) || (tmp == 0))
190 drv_config->dram_side_dq_odt = 0;
191 else
192 drv_config->dram_side_dq_odt = 240 / tmp;
Caesar Wanga8456902016-10-27 01:12:34 +0800193
Derek Basehoreff461d02016-10-20 20:46:43 -0700194 tmp = (mr11_val >> 4) & 0x7;
195 if ((tmp == 7) || (tmp == 0))
196 drv_config->dram_side_ca_odt = 0;
197 else
198 drv_config->dram_side_ca_odt = 240 / tmp;
Caesar Wanga8456902016-10-27 01:12:34 +0800199 break;
200 }
201}
202
203static void sdram_timing_cfg_init(struct timing_related_config *ptiming_config,
204 struct rk3399_sdram_params *sdram_params,
205 struct drv_odt_lp_config *drv_config)
206{
207 uint32_t i, j;
208
209 for (i = 0; i < sdram_params->num_channels; i++) {
Derek Basehoreff461d02016-10-20 20:46:43 -0700210 ptiming_config->dram_info[i].speed_rate = DDR3_DEFAULT;
Caesar Wanga8456902016-10-27 01:12:34 +0800211 ptiming_config->dram_info[i].cs_cnt = sdram_params->ch[i].rank;
212 for (j = 0; j < sdram_params->ch[i].rank; j++) {
213 ptiming_config->dram_info[i].per_die_capability[j] =
214 get_cs_die_capability(sdram_params, i, j);
215 }
216 }
217 ptiming_config->dram_type = sdram_params->dramtype;
218 ptiming_config->ch_cnt = sdram_params->num_channels;
219 switch (sdram_params->dramtype) {
220 case DDR3:
221 ptiming_config->bl = ddr3_default_config.bl;
222 ptiming_config->ap = ddr3_default_config.ap;
223 break;
224 case LPDDR3:
225 ptiming_config->bl = lpddr3_default_config.bl;
226 ptiming_config->ap = lpddr3_default_config.ap;
227 break;
228 case LPDDR4:
229 ptiming_config->bl = lpddr4_default_config.bl;
230 ptiming_config->ap = lpddr4_default_config.ap;
231 ptiming_config->rdbi = 0;
232 ptiming_config->wdbi = 0;
233 break;
234 }
235 ptiming_config->dramds = drv_config->dram_side_drv;
236 ptiming_config->dramodt = drv_config->dram_side_dq_odt;
237 ptiming_config->caodt = drv_config->dram_side_ca_odt;
Derek Basehoree13bc542017-02-24 14:31:36 +0800238 ptiming_config->odt = (mmio_read_32(PHY_REG(0, 5)) >> 16) & 0x1;
Caesar Wanga8456902016-10-27 01:12:34 +0800239}
240
241struct lat_adj_pair {
242 uint32_t cl;
243 uint32_t rdlat_adj;
244 uint32_t cwl;
245 uint32_t wrlat_adj;
246};
247
248const struct lat_adj_pair ddr3_lat_adj[] = {
249 {6, 5, 5, 4},
250 {8, 7, 6, 5},
251 {10, 9, 7, 6},
252 {11, 9, 8, 7},
253 {13, 0xb, 9, 8},
254 {14, 0xb, 0xa, 9}
255};
256
257const struct lat_adj_pair lpddr3_lat_adj[] = {
258 {3, 2, 1, 0},
259 {6, 5, 3, 2},
260 {8, 7, 4, 3},
261 {9, 8, 5, 4},
262 {10, 9, 6, 5},
263 {11, 9, 6, 5},
264 {12, 0xa, 6, 5},
265 {14, 0xc, 8, 7},
266 {16, 0xd, 8, 7}
267};
268
269const struct lat_adj_pair lpddr4_lat_adj[] = {
270 {6, 5, 4, 2},
271 {10, 9, 6, 4},
272 {14, 0xc, 8, 6},
273 {20, 0x11, 0xa, 8},
274 {24, 0x15, 0xc, 0xa},
275 {28, 0x18, 0xe, 0xc},
276 {32, 0x1b, 0x10, 0xe},
277 {36, 0x1e, 0x12, 0x10}
278};
279
280static uint32_t get_rdlat_adj(uint32_t dram_type, uint32_t cl)
281{
282 const struct lat_adj_pair *p;
283 uint32_t cnt;
284 uint32_t i;
285
286 if (dram_type == DDR3) {
287 p = ddr3_lat_adj;
288 cnt = ARRAY_SIZE(ddr3_lat_adj);
289 } else if (dram_type == LPDDR3) {
290 p = lpddr3_lat_adj;
291 cnt = ARRAY_SIZE(lpddr3_lat_adj);
292 } else {
293 p = lpddr4_lat_adj;
294 cnt = ARRAY_SIZE(lpddr4_lat_adj);
295 }
296
297 for (i = 0; i < cnt; i++) {
298 if (cl == p[i].cl)
299 return p[i].rdlat_adj;
300 }
301 /* fail */
302 return 0xff;
303}
304
305static uint32_t get_wrlat_adj(uint32_t dram_type, uint32_t cwl)
306{
307 const struct lat_adj_pair *p;
308 uint32_t cnt;
309 uint32_t i;
310
311 if (dram_type == DDR3) {
312 p = ddr3_lat_adj;
313 cnt = ARRAY_SIZE(ddr3_lat_adj);
314 } else if (dram_type == LPDDR3) {
315 p = lpddr3_lat_adj;
316 cnt = ARRAY_SIZE(lpddr3_lat_adj);
317 } else {
318 p = lpddr4_lat_adj;
319 cnt = ARRAY_SIZE(lpddr4_lat_adj);
320 }
321
322 for (i = 0; i < cnt; i++) {
323 if (cwl == p[i].cwl)
324 return p[i].wrlat_adj;
325 }
326 /* fail */
327 return 0xff;
328}
329
330#define PI_REGS_DIMM_SUPPORT (0)
331#define PI_ADD_LATENCY (0)
332#define PI_DOUBLEFREEK (1)
333
334#define PI_PAD_DELAY_PS_VALUE (1000)
335#define PI_IE_ENABLE_VALUE (3000)
336#define PI_TSEL_ENABLE_VALUE (700)
337
338static uint32_t get_pi_rdlat_adj(struct dram_timing_t *pdram_timing)
339{
340 /*[DLLSUBTYPE2] == "STD_DENALI_HS" */
341 uint32_t rdlat, delay_adder, ie_enable, hs_offset, tsel_adder,
342 extra_adder, tsel_enable;
343
344 ie_enable = PI_IE_ENABLE_VALUE;
345 tsel_enable = PI_TSEL_ENABLE_VALUE;
346
347 rdlat = pdram_timing->cl + PI_ADD_LATENCY;
348 delay_adder = ie_enable / (1000000 / pdram_timing->mhz);
349 if ((ie_enable % (1000000 / pdram_timing->mhz)) != 0)
350 delay_adder++;
351 hs_offset = 0;
352 tsel_adder = 0;
353 extra_adder = 0;
354 /* rdlat = rdlat - (PREAMBLE_SUPPORT & 0x1); */
355 tsel_adder = tsel_enable / (1000000 / pdram_timing->mhz);
356 if ((tsel_enable % (1000000 / pdram_timing->mhz)) != 0)
357 tsel_adder++;
358 delay_adder = delay_adder - 1;
359 if (tsel_adder > delay_adder)
360 extra_adder = tsel_adder - delay_adder;
361 else
362 extra_adder = 0;
363 if (PI_REGS_DIMM_SUPPORT && PI_DOUBLEFREEK)
364 hs_offset = 2;
365 else
366 hs_offset = 1;
367
368 if (delay_adder > (rdlat - 1 - hs_offset)) {
369 rdlat = rdlat - tsel_adder;
370 } else {
371 if ((rdlat - delay_adder) < 2)
372 rdlat = 2;
373 else
374 rdlat = rdlat - delay_adder - extra_adder;
375 }
376
377 return rdlat;
378}
379
380static uint32_t get_pi_wrlat(struct dram_timing_t *pdram_timing,
381 struct timing_related_config *timing_config)
382{
383 uint32_t tmp;
384
385 if (timing_config->dram_type == LPDDR3) {
386 tmp = pdram_timing->cl;
387 if (tmp >= 14)
388 tmp = 8;
389 else if (tmp >= 10)
390 tmp = 6;
391 else if (tmp == 9)
392 tmp = 5;
393 else if (tmp == 8)
394 tmp = 4;
395 else if (tmp == 6)
396 tmp = 3;
397 else
398 tmp = 1;
399 } else {
400 tmp = 1;
401 }
402
403 return tmp;
404}
405
406static uint32_t get_pi_wrlat_adj(struct dram_timing_t *pdram_timing,
407 struct timing_related_config *timing_config)
408{
409 return get_pi_wrlat(pdram_timing, timing_config) + PI_ADD_LATENCY - 1;
410}
411
412static uint32_t get_pi_tdfi_phy_rdlat(struct dram_timing_t *pdram_timing,
413 struct timing_related_config *timing_config)
414{
415 /* [DLLSUBTYPE2] == "STD_DENALI_HS" */
416 uint32_t cas_lat, delay_adder, ie_enable, hs_offset, ie_delay_adder;
417 uint32_t mem_delay_ps, round_trip_ps;
418 uint32_t phy_internal_delay, lpddr_adder, dfi_adder, rdlat_delay;
419
420 ie_enable = PI_IE_ENABLE_VALUE;
421
422 delay_adder = ie_enable / (1000000 / pdram_timing->mhz);
423 if ((ie_enable % (1000000 / pdram_timing->mhz)) != 0)
424 delay_adder++;
425 delay_adder = delay_adder - 1;
426 if (PI_REGS_DIMM_SUPPORT && PI_DOUBLEFREEK)
427 hs_offset = 2;
428 else
429 hs_offset = 1;
430
431 cas_lat = pdram_timing->cl + PI_ADD_LATENCY;
432
433 if (delay_adder > (cas_lat - 1 - hs_offset)) {
434 ie_delay_adder = 0;
435 } else {
436 ie_delay_adder = ie_enable / (1000000 / pdram_timing->mhz);
437 if ((ie_enable % (1000000 / pdram_timing->mhz)) != 0)
438 ie_delay_adder++;
439 }
440
441 if (timing_config->dram_type == DDR3) {
442 mem_delay_ps = 0;
443 } else if (timing_config->dram_type == LPDDR4) {
444 mem_delay_ps = 3600;
445 } else if (timing_config->dram_type == LPDDR3) {
446 mem_delay_ps = 5500;
447 } else {
448 printf("get_pi_tdfi_phy_rdlat:dramtype unsupport\n");
449 return 0;
450 }
451 round_trip_ps = 1100 + 500 + mem_delay_ps + 500 + 600;
452 delay_adder = round_trip_ps / (1000000 / pdram_timing->mhz);
453 if ((round_trip_ps % (1000000 / pdram_timing->mhz)) != 0)
454 delay_adder++;
455
456 phy_internal_delay = 5 + 2 + 4;
457 lpddr_adder = mem_delay_ps / (1000000 / pdram_timing->mhz);
458 if ((mem_delay_ps % (1000000 / pdram_timing->mhz)) != 0)
459 lpddr_adder++;
460 dfi_adder = 0;
461 phy_internal_delay = phy_internal_delay + 2;
462 rdlat_delay = delay_adder + phy_internal_delay +
463 ie_delay_adder + lpddr_adder + dfi_adder;
464
465 rdlat_delay = rdlat_delay + 2;
466 return rdlat_delay;
467}
468
469static uint32_t get_pi_todtoff_min(struct dram_timing_t *pdram_timing,
470 struct timing_related_config *timing_config)
471{
472 uint32_t tmp, todtoff_min_ps;
473
474 if (timing_config->dram_type == LPDDR3)
475 todtoff_min_ps = 2500;
476 else if (timing_config->dram_type == LPDDR4)
477 todtoff_min_ps = 1500;
478 else
479 todtoff_min_ps = 0;
480 /* todtoff_min */
481 tmp = todtoff_min_ps / (1000000 / pdram_timing->mhz);
482 if ((todtoff_min_ps % (1000000 / pdram_timing->mhz)) != 0)
483 tmp++;
484 return tmp;
485}
486
487static uint32_t get_pi_todtoff_max(struct dram_timing_t *pdram_timing,
488 struct timing_related_config *timing_config)
489{
490 uint32_t tmp, todtoff_max_ps;
491
492 if ((timing_config->dram_type == LPDDR4)
493 || (timing_config->dram_type == LPDDR3))
494 todtoff_max_ps = 3500;
495 else
496 todtoff_max_ps = 0;
497
498 /* todtoff_max */
499 tmp = todtoff_max_ps / (1000000 / pdram_timing->mhz);
500 if ((todtoff_max_ps % (1000000 / pdram_timing->mhz)) != 0)
501 tmp++;
502 return tmp;
503}
504
505static void gen_rk3399_ctl_params_f0(struct timing_related_config
506 *timing_config,
507 struct dram_timing_t *pdram_timing)
508{
509 uint32_t i;
510 uint32_t tmp, tmp1;
511
512 for (i = 0; i < timing_config->ch_cnt; i++) {
513 if (timing_config->dram_type == DDR3) {
514 tmp = ((700000 + 10) * timing_config->freq +
515 999) / 1000;
516 tmp += pdram_timing->txsnr + (pdram_timing->tmrd * 3) +
517 pdram_timing->tmod + pdram_timing->tzqinit;
Caesar Wang8bc16672016-10-27 01:12:47 +0800518 mmio_write_32(CTL_REG(i, 5), tmp);
Caesar Wanga8456902016-10-27 01:12:34 +0800519
Caesar Wang8bc16672016-10-27 01:12:47 +0800520 mmio_clrsetbits_32(CTL_REG(i, 22), 0xffff,
521 pdram_timing->tdllk);
Caesar Wanga8456902016-10-27 01:12:34 +0800522
Caesar Wang8bc16672016-10-27 01:12:47 +0800523 mmio_write_32(CTL_REG(i, 32),
524 (pdram_timing->tmod << 8) |
525 pdram_timing->tmrd);
Caesar Wanga8456902016-10-27 01:12:34 +0800526
Caesar Wang8bc16672016-10-27 01:12:47 +0800527 mmio_clrsetbits_32(CTL_REG(i, 59), 0xffff << 16,
528 (pdram_timing->txsr -
529 pdram_timing->trcd) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800530 } else if (timing_config->dram_type == LPDDR4) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800531 mmio_write_32(CTL_REG(i, 5), pdram_timing->tinit1 +
532 pdram_timing->tinit3);
533 mmio_write_32(CTL_REG(i, 32),
534 (pdram_timing->tmrd << 8) |
535 pdram_timing->tmrd);
536 mmio_clrsetbits_32(CTL_REG(i, 59), 0xffff << 16,
537 pdram_timing->txsr << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800538 } else {
Caesar Wang8bc16672016-10-27 01:12:47 +0800539 mmio_write_32(CTL_REG(i, 5), pdram_timing->tinit1);
540 mmio_write_32(CTL_REG(i, 7), pdram_timing->tinit4);
541 mmio_write_32(CTL_REG(i, 32),
542 (pdram_timing->tmrd << 8) |
543 pdram_timing->tmrd);
544 mmio_clrsetbits_32(CTL_REG(i, 59), 0xffff << 16,
545 pdram_timing->txsr << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800546 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800547 mmio_write_32(CTL_REG(i, 6), pdram_timing->tinit3);
548 mmio_write_32(CTL_REG(i, 8), pdram_timing->tinit5);
549 mmio_clrsetbits_32(CTL_REG(i, 23), (0x7f << 16),
550 ((pdram_timing->cl * 2) << 16));
551 mmio_clrsetbits_32(CTL_REG(i, 23), (0x1f << 24),
552 (pdram_timing->cwl << 24));
553 mmio_clrsetbits_32(CTL_REG(i, 24), 0x3f, pdram_timing->al);
554 mmio_clrsetbits_32(CTL_REG(i, 26), 0xffff << 16,
555 (pdram_timing->trc << 24) |
556 (pdram_timing->trrd << 16));
557 mmio_write_32(CTL_REG(i, 27),
558 (pdram_timing->tfaw << 24) |
559 (pdram_timing->trppb << 16) |
560 (pdram_timing->twtr << 8) |
561 pdram_timing->tras_min);
Caesar Wanga8456902016-10-27 01:12:34 +0800562
Caesar Wang8bc16672016-10-27 01:12:47 +0800563 mmio_clrsetbits_32(CTL_REG(i, 31), 0xff << 24,
564 max(4, pdram_timing->trtp) << 24);
565 mmio_write_32(CTL_REG(i, 33), (pdram_timing->tcke << 24) |
566 pdram_timing->tras_max);
567 mmio_clrsetbits_32(CTL_REG(i, 34), 0xff,
568 max(1, pdram_timing->tckesr));
569 mmio_clrsetbits_32(CTL_REG(i, 39),
570 (0x3f << 16) | (0xff << 8),
571 (pdram_timing->twr << 16) |
572 (pdram_timing->trcd << 8));
573 mmio_clrsetbits_32(CTL_REG(i, 42), 0x1f << 16,
574 pdram_timing->tmrz << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800575 tmp = pdram_timing->tdal ? pdram_timing->tdal :
Caesar Wang8bc16672016-10-27 01:12:47 +0800576 (pdram_timing->twr + pdram_timing->trp);
577 mmio_clrsetbits_32(CTL_REG(i, 44), 0xff, tmp);
578 mmio_clrsetbits_32(CTL_REG(i, 45), 0xff, pdram_timing->trp);
579 mmio_write_32(CTL_REG(i, 48),
580 ((pdram_timing->trefi - 8) << 16) |
581 pdram_timing->trfc);
582 mmio_clrsetbits_32(CTL_REG(i, 52), 0xffff, pdram_timing->txp);
583 mmio_clrsetbits_32(CTL_REG(i, 53), 0xffff << 16,
584 pdram_timing->txpdll << 16);
585 mmio_clrsetbits_32(CTL_REG(i, 55), 0xf << 24,
586 pdram_timing->tcscke << 24);
587 mmio_clrsetbits_32(CTL_REG(i, 55), 0xff, pdram_timing->tmrri);
588 mmio_write_32(CTL_REG(i, 56),
589 (pdram_timing->tzqcke << 24) |
590 (pdram_timing->tmrwckel << 16) |
591 (pdram_timing->tckehcs << 8) |
592 pdram_timing->tckelcs);
593 mmio_clrsetbits_32(CTL_REG(i, 60), 0xffff, pdram_timing->txsnr);
594 mmio_clrsetbits_32(CTL_REG(i, 62), 0xffff << 16,
595 (pdram_timing->tckehcmd << 24) |
596 (pdram_timing->tckelcmd << 16));
597 mmio_write_32(CTL_REG(i, 63),
598 (pdram_timing->tckelpd << 24) |
599 (pdram_timing->tescke << 16) |
600 (pdram_timing->tsr << 8) |
601 pdram_timing->tckckel);
602 mmio_clrsetbits_32(CTL_REG(i, 64), 0xfff,
603 (pdram_timing->tcmdcke << 8) |
604 pdram_timing->tcsckeh);
605 mmio_clrsetbits_32(CTL_REG(i, 92), 0xffff << 8,
606 (pdram_timing->tcksrx << 16) |
607 (pdram_timing->tcksre << 8));
608 mmio_clrsetbits_32(CTL_REG(i, 108), 0x1 << 24,
609 (timing_config->dllbp << 24));
610 mmio_clrsetbits_32(CTL_REG(i, 122), 0x3ff << 16,
611 (pdram_timing->tvrcg_enable << 16));
612 mmio_write_32(CTL_REG(i, 123), (pdram_timing->tfc_long << 16) |
613 pdram_timing->tvrcg_disable);
614 mmio_write_32(CTL_REG(i, 124),
615 (pdram_timing->tvref_long << 16) |
616 (pdram_timing->tckfspx << 8) |
617 pdram_timing->tckfspe);
618 mmio_write_32(CTL_REG(i, 133), (pdram_timing->mr[1] << 16) |
619 pdram_timing->mr[0]);
620 mmio_clrsetbits_32(CTL_REG(i, 134), 0xffff,
621 pdram_timing->mr[2]);
622 mmio_clrsetbits_32(CTL_REG(i, 138), 0xffff,
623 pdram_timing->mr[3]);
624 mmio_clrsetbits_32(CTL_REG(i, 139), 0xff << 24,
625 pdram_timing->mr11 << 24);
626 mmio_write_32(CTL_REG(i, 147),
627 (pdram_timing->mr[1] << 16) |
628 pdram_timing->mr[0]);
629 mmio_clrsetbits_32(CTL_REG(i, 148), 0xffff,
630 pdram_timing->mr[2]);
631 mmio_clrsetbits_32(CTL_REG(i, 152), 0xffff,
632 pdram_timing->mr[3]);
633 mmio_clrsetbits_32(CTL_REG(i, 153), 0xff << 24,
634 pdram_timing->mr11 << 24);
Caesar Wanga8456902016-10-27 01:12:34 +0800635 if (timing_config->dram_type == LPDDR4) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800636 mmio_clrsetbits_32(CTL_REG(i, 140), 0xffff << 16,
637 pdram_timing->mr12 << 16);
638 mmio_clrsetbits_32(CTL_REG(i, 142), 0xffff << 16,
639 pdram_timing->mr14 << 16);
640 mmio_clrsetbits_32(CTL_REG(i, 145), 0xffff << 16,
641 pdram_timing->mr22 << 16);
642 mmio_clrsetbits_32(CTL_REG(i, 154), 0xffff << 16,
643 pdram_timing->mr12 << 16);
644 mmio_clrsetbits_32(CTL_REG(i, 156), 0xffff << 16,
645 pdram_timing->mr14 << 16);
646 mmio_clrsetbits_32(CTL_REG(i, 159), 0xffff << 16,
647 pdram_timing->mr22 << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800648 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800649 mmio_clrsetbits_32(CTL_REG(i, 179), 0xfff << 8,
650 pdram_timing->tzqinit << 8);
651 mmio_write_32(CTL_REG(i, 180), (pdram_timing->tzqcs << 16) |
652 (pdram_timing->tzqinit / 2));
653 mmio_write_32(CTL_REG(i, 181), (pdram_timing->tzqlat << 16) |
654 pdram_timing->tzqcal);
655 mmio_clrsetbits_32(CTL_REG(i, 212), 0xff << 8,
656 pdram_timing->todton << 8);
Caesar Wanga8456902016-10-27 01:12:34 +0800657
658 if (timing_config->odt) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800659 mmio_setbits_32(CTL_REG(i, 213), 1 << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800660 if (timing_config->freq < 400)
661 tmp = 4 << 24;
662 else
663 tmp = 8 << 24;
664 } else {
Caesar Wang8bc16672016-10-27 01:12:47 +0800665 mmio_clrbits_32(CTL_REG(i, 213), 1 << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800666 tmp = 2 << 24;
667 }
668
Caesar Wang8bc16672016-10-27 01:12:47 +0800669 mmio_clrsetbits_32(CTL_REG(i, 216), 0x1f << 24, tmp);
670 mmio_clrsetbits_32(CTL_REG(i, 221), (0x3 << 16) | (0xf << 8),
671 (pdram_timing->tdqsck << 16) |
672 (pdram_timing->tdqsck_max << 8));
Caesar Wanga8456902016-10-27 01:12:34 +0800673 tmp =
674 (get_wrlat_adj(timing_config->dram_type, pdram_timing->cwl)
675 << 8) | get_rdlat_adj(timing_config->dram_type,
676 pdram_timing->cl);
Caesar Wang8bc16672016-10-27 01:12:47 +0800677 mmio_clrsetbits_32(CTL_REG(i, 284), 0xffff, tmp);
678 mmio_clrsetbits_32(CTL_REG(i, 82), 0xffff << 16,
679 (4 * pdram_timing->trefi) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800680
Caesar Wang8bc16672016-10-27 01:12:47 +0800681 mmio_clrsetbits_32(CTL_REG(i, 83), 0xffff,
682 (2 * pdram_timing->trefi) & 0xffff);
Caesar Wanga8456902016-10-27 01:12:34 +0800683
684 if ((timing_config->dram_type == LPDDR3) ||
685 (timing_config->dram_type == LPDDR4)) {
686 tmp = get_pi_wrlat(pdram_timing, timing_config);
687 tmp1 = get_pi_todtoff_max(pdram_timing, timing_config);
688 tmp = (tmp > tmp1) ? (tmp - tmp1) : 0;
689 } else {
690 tmp = 0;
691 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800692 mmio_clrsetbits_32(CTL_REG(i, 214), 0x3f << 16,
693 (tmp & 0x3f) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800694
695 if ((timing_config->dram_type == LPDDR3) ||
696 (timing_config->dram_type == LPDDR4)) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800697 /* min_rl_preamble = cl+TDQSCK_MIN -1 */
Caesar Wanga8456902016-10-27 01:12:34 +0800698 tmp = pdram_timing->cl +
699 get_pi_todtoff_min(pdram_timing, timing_config) - 1;
700 /* todtoff_max */
701 tmp1 = get_pi_todtoff_max(pdram_timing, timing_config);
702 tmp = (tmp > tmp1) ? (tmp - tmp1) : 0;
703 } else {
704 tmp = pdram_timing->cl - pdram_timing->cwl;
705 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800706 mmio_clrsetbits_32(CTL_REG(i, 215), 0x3f << 8,
707 (tmp & 0x3f) << 8);
Caesar Wanga8456902016-10-27 01:12:34 +0800708
Caesar Wang8bc16672016-10-27 01:12:47 +0800709 mmio_clrsetbits_32(CTL_REG(i, 275), 0xff << 16,
710 (get_pi_tdfi_phy_rdlat(pdram_timing,
711 timing_config) &
712 0xff) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800713
Caesar Wang8bc16672016-10-27 01:12:47 +0800714 mmio_clrsetbits_32(CTL_REG(i, 277), 0xffff,
715 (2 * pdram_timing->trefi) & 0xffff);
Caesar Wanga8456902016-10-27 01:12:34 +0800716
Caesar Wang8bc16672016-10-27 01:12:47 +0800717 mmio_clrsetbits_32(CTL_REG(i, 282), 0xffff,
718 (2 * pdram_timing->trefi) & 0xffff);
Caesar Wanga8456902016-10-27 01:12:34 +0800719
Caesar Wang8bc16672016-10-27 01:12:47 +0800720 mmio_write_32(CTL_REG(i, 283), 20 * pdram_timing->trefi);
Caesar Wanga8456902016-10-27 01:12:34 +0800721
722 /* CTL_308 TDFI_CALVL_CAPTURE_F0:RW:16:10 */
723 tmp1 = 20000 / (1000000 / pdram_timing->mhz) + 1;
724 if ((20000 % (1000000 / pdram_timing->mhz)) != 0)
725 tmp1++;
726 tmp = (tmp1 >> 1) + (tmp1 % 2) + 5;
Caesar Wang8bc16672016-10-27 01:12:47 +0800727 mmio_clrsetbits_32(CTL_REG(i, 308), 0x3ff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800728
729 /* CTL_308 TDFI_CALVL_CC_F0:RW:0:10 */
730 tmp = tmp + 18;
Caesar Wang8bc16672016-10-27 01:12:47 +0800731 mmio_clrsetbits_32(CTL_REG(i, 308), 0x3ff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +0800732
733 /* CTL_314 TDFI_WRCSLAT_F0:RW:8:8 */
734 tmp1 = get_pi_wrlat_adj(pdram_timing, timing_config);
Derek Basehore0e8909d2016-11-09 18:28:19 -0800735 if (timing_config->freq <= TDFI_LAT_THRESHOLD_FREQ) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800736 if (tmp1 == 0)
737 tmp = 0;
738 else if (tmp1 < 5)
739 tmp = tmp1 - 1;
740 else
Caesar Wanga8456902016-10-27 01:12:34 +0800741 tmp = tmp1 - 5;
Caesar Wanga8456902016-10-27 01:12:34 +0800742 } else {
743 tmp = tmp1 - 2;
744 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800745 mmio_clrsetbits_32(CTL_REG(i, 314), 0xff << 8, tmp << 8);
Caesar Wanga8456902016-10-27 01:12:34 +0800746
747 /* CTL_314 TDFI_RDCSLAT_F0:RW:0:8 */
Derek Basehore0e8909d2016-11-09 18:28:19 -0800748 if ((timing_config->freq <= TDFI_LAT_THRESHOLD_FREQ) &&
Caesar Wang8bc16672016-10-27 01:12:47 +0800749 (pdram_timing->cl >= 5))
Caesar Wanga8456902016-10-27 01:12:34 +0800750 tmp = pdram_timing->cl - 5;
751 else
752 tmp = pdram_timing->cl - 2;
Caesar Wang8bc16672016-10-27 01:12:47 +0800753 mmio_clrsetbits_32(CTL_REG(i, 314), 0xff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +0800754 }
755}
756
757static void gen_rk3399_ctl_params_f1(struct timing_related_config
758 *timing_config,
759 struct dram_timing_t *pdram_timing)
760{
761 uint32_t i;
762 uint32_t tmp, tmp1;
763
764 for (i = 0; i < timing_config->ch_cnt; i++) {
765 if (timing_config->dram_type == DDR3) {
766 tmp =
Caesar Wang8bc16672016-10-27 01:12:47 +0800767 ((700000 + 10) * timing_config->freq + 999) / 1000;
768 tmp += pdram_timing->txsnr + (pdram_timing->tmrd * 3) +
769 pdram_timing->tmod + pdram_timing->tzqinit;
770 mmio_write_32(CTL_REG(i, 9), tmp);
771 mmio_clrsetbits_32(CTL_REG(i, 22), 0xffff << 16,
772 pdram_timing->tdllk << 16);
773 mmio_clrsetbits_32(CTL_REG(i, 34), 0xffffff00,
774 (pdram_timing->tmod << 24) |
775 (pdram_timing->tmrd << 16) |
776 (pdram_timing->trtp << 8));
777 mmio_clrsetbits_32(CTL_REG(i, 60), 0xffff << 16,
778 (pdram_timing->txsr -
779 pdram_timing->trcd) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800780 } else if (timing_config->dram_type == LPDDR4) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800781 mmio_write_32(CTL_REG(i, 9), pdram_timing->tinit1 +
782 pdram_timing->tinit3);
783 mmio_clrsetbits_32(CTL_REG(i, 34), 0xffffff00,
784 (pdram_timing->tmrd << 24) |
785 (pdram_timing->tmrd << 16) |
786 (pdram_timing->trtp << 8));
787 mmio_clrsetbits_32(CTL_REG(i, 60), 0xffff << 16,
788 pdram_timing->txsr << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800789 } else {
Caesar Wang8bc16672016-10-27 01:12:47 +0800790 mmio_write_32(CTL_REG(i, 9), pdram_timing->tinit1);
791 mmio_write_32(CTL_REG(i, 11), pdram_timing->tinit4);
792 mmio_clrsetbits_32(CTL_REG(i, 34), 0xffffff00,
793 (pdram_timing->tmrd << 24) |
794 (pdram_timing->tmrd << 16) |
795 (pdram_timing->trtp << 8));
796 mmio_clrsetbits_32(CTL_REG(i, 60), 0xffff << 16,
797 pdram_timing->txsr << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800798 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800799 mmio_write_32(CTL_REG(i, 10), pdram_timing->tinit3);
800 mmio_write_32(CTL_REG(i, 12), pdram_timing->tinit5);
801 mmio_clrsetbits_32(CTL_REG(i, 24), (0x7f << 8),
802 ((pdram_timing->cl * 2) << 8));
803 mmio_clrsetbits_32(CTL_REG(i, 24), (0x1f << 16),
804 (pdram_timing->cwl << 16));
805 mmio_clrsetbits_32(CTL_REG(i, 24), 0x3f << 24,
806 pdram_timing->al << 24);
807 mmio_clrsetbits_32(CTL_REG(i, 28), 0xffffff00,
808 (pdram_timing->tras_min << 24) |
809 (pdram_timing->trc << 16) |
810 (pdram_timing->trrd << 8));
811 mmio_clrsetbits_32(CTL_REG(i, 29), 0xffffff,
812 (pdram_timing->tfaw << 16) |
813 (pdram_timing->trppb << 8) |
814 pdram_timing->twtr);
815 mmio_write_32(CTL_REG(i, 35), (pdram_timing->tcke << 24) |
816 pdram_timing->tras_max);
817 mmio_clrsetbits_32(CTL_REG(i, 36), 0xff,
818 max(1, pdram_timing->tckesr));
819 mmio_clrsetbits_32(CTL_REG(i, 39), (0xff << 24),
820 (pdram_timing->trcd << 24));
821 mmio_clrsetbits_32(CTL_REG(i, 40), 0x3f, pdram_timing->twr);
822 mmio_clrsetbits_32(CTL_REG(i, 42), 0x1f << 24,
823 pdram_timing->tmrz << 24);
Caesar Wanga8456902016-10-27 01:12:34 +0800824 tmp = pdram_timing->tdal ? pdram_timing->tdal :
Caesar Wang8bc16672016-10-27 01:12:47 +0800825 (pdram_timing->twr + pdram_timing->trp);
826 mmio_clrsetbits_32(CTL_REG(i, 44), 0xff << 8, tmp << 8);
827 mmio_clrsetbits_32(CTL_REG(i, 45), 0xff << 8,
828 pdram_timing->trp << 8);
829 mmio_write_32(CTL_REG(i, 49),
830 ((pdram_timing->trefi - 8) << 16) |
831 pdram_timing->trfc);
832 mmio_clrsetbits_32(CTL_REG(i, 52), 0xffff << 16,
833 pdram_timing->txp << 16);
834 mmio_clrsetbits_32(CTL_REG(i, 54), 0xffff,
835 pdram_timing->txpdll);
836 mmio_clrsetbits_32(CTL_REG(i, 55), 0xff << 8,
837 pdram_timing->tmrri << 8);
838 mmio_write_32(CTL_REG(i, 57), (pdram_timing->tmrwckel << 24) |
839 (pdram_timing->tckehcs << 16) |
840 (pdram_timing->tckelcs << 8) |
841 pdram_timing->tcscke);
842 mmio_clrsetbits_32(CTL_REG(i, 58), 0xf, pdram_timing->tzqcke);
843 mmio_clrsetbits_32(CTL_REG(i, 61), 0xffff, pdram_timing->txsnr);
844 mmio_clrsetbits_32(CTL_REG(i, 64), 0xffff << 16,
845 (pdram_timing->tckehcmd << 24) |
846 (pdram_timing->tckelcmd << 16));
847 mmio_write_32(CTL_REG(i, 65), (pdram_timing->tckelpd << 24) |
848 (pdram_timing->tescke << 16) |
849 (pdram_timing->tsr << 8) |
850 pdram_timing->tckckel);
851 mmio_clrsetbits_32(CTL_REG(i, 66), 0xfff,
852 (pdram_timing->tcmdcke << 8) |
853 pdram_timing->tcsckeh);
854 mmio_clrsetbits_32(CTL_REG(i, 92), (0xff << 24),
855 (pdram_timing->tcksre << 24));
856 mmio_clrsetbits_32(CTL_REG(i, 93), 0xff,
857 pdram_timing->tcksrx);
858 mmio_clrsetbits_32(CTL_REG(i, 108), (0x1 << 25),
859 (timing_config->dllbp << 25));
860 mmio_write_32(CTL_REG(i, 125),
861 (pdram_timing->tvrcg_disable << 16) |
862 pdram_timing->tvrcg_enable);
863 mmio_write_32(CTL_REG(i, 126), (pdram_timing->tckfspx << 24) |
864 (pdram_timing->tckfspe << 16) |
865 pdram_timing->tfc_long);
866 mmio_clrsetbits_32(CTL_REG(i, 127), 0xffff,
867 pdram_timing->tvref_long);
868 mmio_clrsetbits_32(CTL_REG(i, 134), 0xffff << 16,
869 pdram_timing->mr[0] << 16);
870 mmio_write_32(CTL_REG(i, 135), (pdram_timing->mr[2] << 16) |
871 pdram_timing->mr[1]);
872 mmio_clrsetbits_32(CTL_REG(i, 138), 0xffff << 16,
873 pdram_timing->mr[3] << 16);
874 mmio_clrsetbits_32(CTL_REG(i, 140), 0xff, pdram_timing->mr11);
875 mmio_clrsetbits_32(CTL_REG(i, 148), 0xffff << 16,
876 pdram_timing->mr[0] << 16);
877 mmio_write_32(CTL_REG(i, 149), (pdram_timing->mr[2] << 16) |
878 pdram_timing->mr[1]);
879 mmio_clrsetbits_32(CTL_REG(i, 152), 0xffff << 16,
880 pdram_timing->mr[3] << 16);
881 mmio_clrsetbits_32(CTL_REG(i, 154), 0xff, pdram_timing->mr11);
Caesar Wanga8456902016-10-27 01:12:34 +0800882 if (timing_config->dram_type == LPDDR4) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800883 mmio_clrsetbits_32(CTL_REG(i, 141), 0xffff,
884 pdram_timing->mr12);
885 mmio_clrsetbits_32(CTL_REG(i, 143), 0xffff,
886 pdram_timing->mr14);
887 mmio_clrsetbits_32(CTL_REG(i, 146), 0xffff,
888 pdram_timing->mr22);
889 mmio_clrsetbits_32(CTL_REG(i, 155), 0xffff,
890 pdram_timing->mr12);
891 mmio_clrsetbits_32(CTL_REG(i, 157), 0xffff,
892 pdram_timing->mr14);
893 mmio_clrsetbits_32(CTL_REG(i, 160), 0xffff,
894 pdram_timing->mr22);
Caesar Wanga8456902016-10-27 01:12:34 +0800895 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800896 mmio_write_32(CTL_REG(i, 182),
897 ((pdram_timing->tzqinit / 2) << 16) |
898 pdram_timing->tzqinit);
899 mmio_write_32(CTL_REG(i, 183), (pdram_timing->tzqcal << 16) |
900 pdram_timing->tzqcs);
901 mmio_clrsetbits_32(CTL_REG(i, 184), 0x3f, pdram_timing->tzqlat);
902 mmio_clrsetbits_32(CTL_REG(i, 188), 0xfff,
903 pdram_timing->tzqreset);
904 mmio_clrsetbits_32(CTL_REG(i, 212), 0xff << 16,
905 pdram_timing->todton << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800906
907 if (timing_config->odt) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800908 mmio_setbits_32(CTL_REG(i, 213), (1 << 24));
Caesar Wanga8456902016-10-27 01:12:34 +0800909 if (timing_config->freq < 400)
910 tmp = 4 << 24;
911 else
912 tmp = 8 << 24;
913 } else {
Caesar Wang8bc16672016-10-27 01:12:47 +0800914 mmio_clrbits_32(CTL_REG(i, 213), (1 << 24));
Caesar Wanga8456902016-10-27 01:12:34 +0800915 tmp = 2 << 24;
916 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800917 mmio_clrsetbits_32(CTL_REG(i, 217), 0x1f << 24, tmp);
918 mmio_clrsetbits_32(CTL_REG(i, 221), 0xf << 24,
919 (pdram_timing->tdqsck_max << 24));
920 mmio_clrsetbits_32(CTL_REG(i, 222), 0x3, pdram_timing->tdqsck);
921 mmio_clrsetbits_32(CTL_REG(i, 291), 0xffff,
922 (get_wrlat_adj(timing_config->dram_type,
923 pdram_timing->cwl) << 8) |
924 get_rdlat_adj(timing_config->dram_type,
925 pdram_timing->cl));
Caesar Wanga8456902016-10-27 01:12:34 +0800926
Caesar Wang8bc16672016-10-27 01:12:47 +0800927 mmio_clrsetbits_32(CTL_REG(i, 84), 0xffff,
928 (4 * pdram_timing->trefi) & 0xffff);
Caesar Wanga8456902016-10-27 01:12:34 +0800929
Caesar Wang8bc16672016-10-27 01:12:47 +0800930 mmio_clrsetbits_32(CTL_REG(i, 84), 0xffff << 16,
931 ((2 * pdram_timing->trefi) & 0xffff) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800932
933 if ((timing_config->dram_type == LPDDR3) ||
934 (timing_config->dram_type == LPDDR4)) {
935 tmp = get_pi_wrlat(pdram_timing, timing_config);
936 tmp1 = get_pi_todtoff_max(pdram_timing, timing_config);
937 tmp = (tmp > tmp1) ? (tmp - tmp1) : 0;
938 } else {
939 tmp = 0;
940 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800941 mmio_clrsetbits_32(CTL_REG(i, 214), 0x3f << 24,
942 (tmp & 0x3f) << 24);
Caesar Wanga8456902016-10-27 01:12:34 +0800943
944 if ((timing_config->dram_type == LPDDR3) ||
945 (timing_config->dram_type == LPDDR4)) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800946 /* min_rl_preamble = cl + TDQSCK_MIN - 1 */
Caesar Wanga8456902016-10-27 01:12:34 +0800947 tmp = pdram_timing->cl +
Caesar Wang8bc16672016-10-27 01:12:47 +0800948 get_pi_todtoff_min(pdram_timing, timing_config);
949 tmp--;
Caesar Wanga8456902016-10-27 01:12:34 +0800950 /* todtoff_max */
951 tmp1 = get_pi_todtoff_max(pdram_timing, timing_config);
952 tmp = (tmp > tmp1) ? (tmp - tmp1) : 0;
953 } else {
954 tmp = pdram_timing->cl - pdram_timing->cwl;
955 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800956 mmio_clrsetbits_32(CTL_REG(i, 215), 0x3f << 16,
957 (tmp & 0x3f) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800958
Caesar Wang8bc16672016-10-27 01:12:47 +0800959 mmio_clrsetbits_32(CTL_REG(i, 275), 0xff << 24,
960 (get_pi_tdfi_phy_rdlat(pdram_timing,
961 timing_config) &
962 0xff) << 24);
Caesar Wanga8456902016-10-27 01:12:34 +0800963
Caesar Wang8bc16672016-10-27 01:12:47 +0800964 mmio_clrsetbits_32(CTL_REG(i, 284), 0xffff << 16,
965 ((2 * pdram_timing->trefi) & 0xffff) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800966
Caesar Wang8bc16672016-10-27 01:12:47 +0800967 mmio_clrsetbits_32(CTL_REG(i, 289), 0xffff,
968 (2 * pdram_timing->trefi) & 0xffff);
Caesar Wanga8456902016-10-27 01:12:34 +0800969
Caesar Wang8bc16672016-10-27 01:12:47 +0800970 mmio_write_32(CTL_REG(i, 290), 20 * pdram_timing->trefi);
Caesar Wanga8456902016-10-27 01:12:34 +0800971
972 /* CTL_309 TDFI_CALVL_CAPTURE_F1:RW:16:10 */
973 tmp1 = 20000 / (1000000 / pdram_timing->mhz) + 1;
974 if ((20000 % (1000000 / pdram_timing->mhz)) != 0)
975 tmp1++;
976 tmp = (tmp1 >> 1) + (tmp1 % 2) + 5;
Caesar Wang8bc16672016-10-27 01:12:47 +0800977 mmio_clrsetbits_32(CTL_REG(i, 309), 0x3ff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800978
979 /* CTL_309 TDFI_CALVL_CC_F1:RW:0:10 */
980 tmp = tmp + 18;
Caesar Wang8bc16672016-10-27 01:12:47 +0800981 mmio_clrsetbits_32(CTL_REG(i, 309), 0x3ff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +0800982
983 /* CTL_314 TDFI_WRCSLAT_F1:RW:24:8 */
984 tmp1 = get_pi_wrlat_adj(pdram_timing, timing_config);
Derek Basehore0e8909d2016-11-09 18:28:19 -0800985 if (timing_config->freq <= TDFI_LAT_THRESHOLD_FREQ) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800986 if (tmp1 == 0)
987 tmp = 0;
988 else if (tmp1 < 5)
989 tmp = tmp1 - 1;
990 else
Caesar Wanga8456902016-10-27 01:12:34 +0800991 tmp = tmp1 - 5;
Caesar Wanga8456902016-10-27 01:12:34 +0800992 } else {
993 tmp = tmp1 - 2;
994 }
995
Caesar Wang8bc16672016-10-27 01:12:47 +0800996 mmio_clrsetbits_32(CTL_REG(i, 314), 0xff << 24, tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +0800997
998 /* CTL_314 TDFI_RDCSLAT_F1:RW:16:8 */
Derek Basehore0e8909d2016-11-09 18:28:19 -0800999 if ((timing_config->freq <= TDFI_LAT_THRESHOLD_FREQ) &&
Caesar Wang8bc16672016-10-27 01:12:47 +08001000 (pdram_timing->cl >= 5))
Caesar Wanga8456902016-10-27 01:12:34 +08001001 tmp = pdram_timing->cl - 5;
1002 else
1003 tmp = pdram_timing->cl - 2;
Caesar Wang8bc16672016-10-27 01:12:47 +08001004 mmio_clrsetbits_32(CTL_REG(i, 314), 0xff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001005 }
1006}
1007
Derek Basehoreb1065122016-10-20 22:09:22 -07001008static void gen_rk3399_enable_training(uint32_t ch_cnt, uint32_t nmhz)
1009{
1010 uint32_t i, tmp;
1011
1012 if (nmhz <= PHY_DLL_BYPASS_FREQ)
1013 tmp = 0;
1014 else
1015 tmp = 1;
1016
1017 for (i = 0; i < ch_cnt; i++) {
1018 mmio_clrsetbits_32(CTL_REG(i, 305), 1 << 16, tmp << 16);
1019 mmio_clrsetbits_32(CTL_REG(i, 71), 1, tmp);
Lin Huangdc8e82e2016-12-16 13:59:07 +08001020 mmio_clrsetbits_32(CTL_REG(i, 70), 1 << 8, 1 << 8);
Derek Basehoreb1065122016-10-20 22:09:22 -07001021 }
1022}
1023
Caesar Wanga8456902016-10-27 01:12:34 +08001024static void gen_rk3399_ctl_params(struct timing_related_config *timing_config,
1025 struct dram_timing_t *pdram_timing,
1026 uint32_t fn)
1027{
1028 if (fn == 0)
1029 gen_rk3399_ctl_params_f0(timing_config, pdram_timing);
1030 else
1031 gen_rk3399_ctl_params_f1(timing_config, pdram_timing);
Caesar Wanga8456902016-10-27 01:12:34 +08001032}
1033
1034static void gen_rk3399_pi_params_f0(struct timing_related_config *timing_config,
1035 struct dram_timing_t *pdram_timing)
1036{
1037 uint32_t tmp, tmp1, tmp2;
1038 uint32_t i;
1039
1040 for (i = 0; i < timing_config->ch_cnt; i++) {
1041 /* PI_02 PI_TDFI_PHYMSTR_MAX_F0:RW:0:32 */
1042 tmp = 4 * pdram_timing->trefi;
Caesar Wang8bc16672016-10-27 01:12:47 +08001043 mmio_write_32(PI_REG(i, 2), tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001044 /* PI_03 PI_TDFI_PHYMSTR_RESP_F0:RW:0:16 */
1045 tmp = 2 * pdram_timing->trefi;
Caesar Wang8bc16672016-10-27 01:12:47 +08001046 mmio_clrsetbits_32(PI_REG(i, 3), 0xffff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001047 /* PI_07 PI_TDFI_PHYUPD_RESP_F0:RW:16:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001048 mmio_clrsetbits_32(PI_REG(i, 7), 0xffff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001049
1050 /* PI_42 PI_TDELAY_RDWR_2_BUS_IDLE_F0:RW:0:8 */
1051 if (timing_config->dram_type == LPDDR4)
1052 tmp = 2;
1053 else
1054 tmp = 0;
1055 tmp = (pdram_timing->bl / 2) + 4 +
Caesar Wang8bc16672016-10-27 01:12:47 +08001056 (get_pi_rdlat_adj(pdram_timing) - 2) + tmp +
1057 get_pi_tdfi_phy_rdlat(pdram_timing, timing_config);
1058 mmio_clrsetbits_32(PI_REG(i, 42), 0xff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001059 /* PI_43 PI_WRLAT_F0:RW:0:5 */
1060 if (timing_config->dram_type == LPDDR3) {
1061 tmp = get_pi_wrlat(pdram_timing, timing_config);
Caesar Wang8bc16672016-10-27 01:12:47 +08001062 mmio_clrsetbits_32(PI_REG(i, 43), 0x1f, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001063 }
1064 /* PI_43 PI_ADDITIVE_LAT_F0:RW:8:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001065 mmio_clrsetbits_32(PI_REG(i, 43), 0x3f << 8,
1066 PI_ADD_LATENCY << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001067
1068 /* PI_43 PI_CASLAT_LIN_F0:RW:16:7 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001069 mmio_clrsetbits_32(PI_REG(i, 43), 0x7f << 16,
1070 (pdram_timing->cl * 2) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001071 /* PI_46 PI_TREF_F0:RW:16:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001072 mmio_clrsetbits_32(PI_REG(i, 46), 0xffff << 16,
1073 pdram_timing->trefi << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001074 /* PI_46 PI_TRFC_F0:RW:0:10 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001075 mmio_clrsetbits_32(PI_REG(i, 46), 0x3ff, pdram_timing->trfc);
Caesar Wanga8456902016-10-27 01:12:34 +08001076 /* PI_66 PI_TODTL_2CMD_F0:RW:24:8 */
1077 if (timing_config->dram_type == LPDDR3) {
1078 tmp = get_pi_todtoff_max(pdram_timing, timing_config);
Caesar Wang8bc16672016-10-27 01:12:47 +08001079 mmio_clrsetbits_32(PI_REG(i, 66), 0xff << 24,
1080 tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001081 }
1082 /* PI_72 PI_WR_TO_ODTH_F0:RW:16:6 */
1083 if ((timing_config->dram_type == LPDDR3) ||
1084 (timing_config->dram_type == LPDDR4)) {
1085 tmp1 = get_pi_wrlat(pdram_timing, timing_config);
1086 tmp2 = get_pi_todtoff_max(pdram_timing, timing_config);
1087 if (tmp1 > tmp2)
1088 tmp = tmp1 - tmp2;
1089 else
1090 tmp = 0;
1091 } else if (timing_config->dram_type == DDR3) {
1092 tmp = 0;
1093 }
Caesar Wang8bc16672016-10-27 01:12:47 +08001094 mmio_clrsetbits_32(PI_REG(i, 72), 0x3f << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001095 /* PI_73 PI_RD_TO_ODTH_F0:RW:8:6 */
1096 if ((timing_config->dram_type == LPDDR3) ||
1097 (timing_config->dram_type == LPDDR4)) {
Caesar Wang8bc16672016-10-27 01:12:47 +08001098 /* min_rl_preamble = cl + TDQSCK_MIN - 1 */
1099 tmp1 = pdram_timing->cl;
1100 tmp1 += get_pi_todtoff_min(pdram_timing, timing_config);
1101 tmp1--;
Caesar Wanga8456902016-10-27 01:12:34 +08001102 /* todtoff_max */
1103 tmp2 = get_pi_todtoff_max(pdram_timing, timing_config);
1104 if (tmp1 > tmp2)
1105 tmp = tmp1 - tmp2;
1106 else
1107 tmp = 0;
1108 } else if (timing_config->dram_type == DDR3) {
1109 tmp = pdram_timing->cl - pdram_timing->cwl;
1110 }
Caesar Wang8bc16672016-10-27 01:12:47 +08001111 mmio_clrsetbits_32(PI_REG(i, 73), 0x3f << 8, tmp << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001112 /* PI_89 PI_RDLAT_ADJ_F0:RW:16:8 */
1113 tmp = get_pi_rdlat_adj(pdram_timing);
Caesar Wang8bc16672016-10-27 01:12:47 +08001114 mmio_clrsetbits_32(PI_REG(i, 89), 0xff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001115 /* PI_90 PI_WRLAT_ADJ_F0:RW:16:8 */
1116 tmp = get_pi_wrlat_adj(pdram_timing, timing_config);
Caesar Wang8bc16672016-10-27 01:12:47 +08001117 mmio_clrsetbits_32(PI_REG(i, 90), 0xff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001118 /* PI_91 PI_TDFI_WRCSLAT_F0:RW:16:8 */
1119 tmp1 = tmp;
Caesar Wang8bc16672016-10-27 01:12:47 +08001120 if (tmp1 == 0)
1121 tmp = 0;
1122 else if (tmp1 < 5)
1123 tmp = tmp1 - 1;
1124 else
Caesar Wanga8456902016-10-27 01:12:34 +08001125 tmp = tmp1 - 5;
Caesar Wang8bc16672016-10-27 01:12:47 +08001126 mmio_clrsetbits_32(PI_REG(i, 91), 0xff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001127 /* PI_95 PI_TDFI_CALVL_CAPTURE_F0:RW:16:10 */
1128 tmp1 = 20000 / (1000000 / pdram_timing->mhz) + 1;
1129 if ((20000 % (1000000 / pdram_timing->mhz)) != 0)
1130 tmp1++;
1131 tmp = (tmp1 >> 1) + (tmp1 % 2) + 5;
Caesar Wang8bc16672016-10-27 01:12:47 +08001132 mmio_clrsetbits_32(PI_REG(i, 95), 0x3ff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001133 /* PI_95 PI_TDFI_CALVL_CC_F0:RW:0:10 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001134 mmio_clrsetbits_32(PI_REG(i, 95), 0x3ff, tmp + 18);
Caesar Wanga8456902016-10-27 01:12:34 +08001135 /* PI_102 PI_TMRZ_F0:RW:8:5 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001136 mmio_clrsetbits_32(PI_REG(i, 102), 0x1f << 8,
1137 pdram_timing->tmrz << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001138 /* PI_111 PI_TDFI_CALVL_STROBE_F0:RW:8:4 */
1139 tmp1 = 2 * 1000 / (1000000 / pdram_timing->mhz);
1140 if ((2 * 1000 % (1000000 / pdram_timing->mhz)) != 0)
1141 tmp1++;
1142 /* pi_tdfi_calvl_strobe=tds_train+5 */
1143 tmp = tmp1 + 5;
Caesar Wang8bc16672016-10-27 01:12:47 +08001144 mmio_clrsetbits_32(PI_REG(i, 111), 0xf << 8, tmp << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001145 /* PI_116 PI_TCKEHDQS_F0:RW:16:6 */
1146 tmp = 10000 / (1000000 / pdram_timing->mhz);
1147 if ((10000 % (1000000 / pdram_timing->mhz)) != 0)
1148 tmp++;
1149 if (pdram_timing->mhz <= 100)
1150 tmp = tmp + 1;
1151 else
1152 tmp = tmp + 8;
Caesar Wang8bc16672016-10-27 01:12:47 +08001153 mmio_clrsetbits_32(PI_REG(i, 116), 0x3f << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001154 /* PI_125 PI_MR1_DATA_F0_0:RW+:8:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001155 mmio_clrsetbits_32(PI_REG(i, 125), 0xffff << 8,
1156 pdram_timing->mr[1] << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001157 /* PI_133 PI_MR1_DATA_F0_1:RW+:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001158 mmio_clrsetbits_32(PI_REG(i, 133), 0xffff, pdram_timing->mr[1]);
Caesar Wanga8456902016-10-27 01:12:34 +08001159 /* PI_140 PI_MR1_DATA_F0_2:RW+:16:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001160 mmio_clrsetbits_32(PI_REG(i, 140), 0xffff << 16,
1161 pdram_timing->mr[1] << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001162 /* PI_148 PI_MR1_DATA_F0_3:RW+:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001163 mmio_clrsetbits_32(PI_REG(i, 148), 0xffff, pdram_timing->mr[1]);
Caesar Wanga8456902016-10-27 01:12:34 +08001164 /* PI_126 PI_MR2_DATA_F0_0:RW+:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001165 mmio_clrsetbits_32(PI_REG(i, 126), 0xffff, pdram_timing->mr[2]);
Caesar Wanga8456902016-10-27 01:12:34 +08001166 /* PI_133 PI_MR2_DATA_F0_1:RW+:16:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001167 mmio_clrsetbits_32(PI_REG(i, 133), 0xffff << 16,
1168 pdram_timing->mr[2] << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001169 /* PI_141 PI_MR2_DATA_F0_2:RW+:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001170 mmio_clrsetbits_32(PI_REG(i, 141), 0xffff, pdram_timing->mr[2]);
Caesar Wanga8456902016-10-27 01:12:34 +08001171 /* PI_148 PI_MR2_DATA_F0_3:RW+:16:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001172 mmio_clrsetbits_32(PI_REG(i, 148), 0xffff << 16,
1173 pdram_timing->mr[2] << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001174 /* PI_156 PI_TFC_F0:RW:0:10 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001175 mmio_clrsetbits_32(PI_REG(i, 156), 0x3ff, pdram_timing->trfc);
Caesar Wanga8456902016-10-27 01:12:34 +08001176 /* PI_158 PI_TWR_F0:RW:24:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001177 mmio_clrsetbits_32(PI_REG(i, 158), 0x3f << 24,
1178 pdram_timing->twr << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001179 /* PI_158 PI_TWTR_F0:RW:16:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001180 mmio_clrsetbits_32(PI_REG(i, 158), 0x3f << 16,
1181 pdram_timing->twtr << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001182 /* PI_158 PI_TRCD_F0:RW:8:8 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001183 mmio_clrsetbits_32(PI_REG(i, 158), 0xff << 8,
1184 pdram_timing->trcd << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001185 /* PI_158 PI_TRP_F0:RW:0:8 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001186 mmio_clrsetbits_32(PI_REG(i, 158), 0xff, pdram_timing->trp);
Caesar Wanga8456902016-10-27 01:12:34 +08001187 /* PI_157 PI_TRTP_F0:RW:24:8 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001188 mmio_clrsetbits_32(PI_REG(i, 157), 0xff << 24,
1189 pdram_timing->trtp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001190 /* PI_159 PI_TRAS_MIN_F0:RW:24:8 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001191 mmio_clrsetbits_32(PI_REG(i, 159), 0xff << 24,
1192 pdram_timing->tras_min << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001193 /* PI_159 PI_TRAS_MAX_F0:RW:0:17 */
1194 tmp = pdram_timing->tras_max * 99 / 100;
Caesar Wang8bc16672016-10-27 01:12:47 +08001195 mmio_clrsetbits_32(PI_REG(i, 159), 0x1ffff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001196 /* PI_160 PI_TMRD_F0:RW:16:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001197 mmio_clrsetbits_32(PI_REG(i, 160), 0x3f << 16,
1198 pdram_timing->tmrd << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001199 /*PI_160 PI_TDQSCK_MAX_F0:RW:0:4 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001200 mmio_clrsetbits_32(PI_REG(i, 160), 0xf,
1201 pdram_timing->tdqsck_max);
Caesar Wanga8456902016-10-27 01:12:34 +08001202 /* PI_187 PI_TDFI_CTRLUPD_MAX_F0:RW:8:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001203 mmio_clrsetbits_32(PI_REG(i, 187), 0xffff << 8,
1204 (2 * pdram_timing->trefi) << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001205 /* PI_188 PI_TDFI_CTRLUPD_INTERVAL_F0:RW:0:32 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001206 mmio_clrsetbits_32(PI_REG(i, 188), 0xffffffff,
1207 20 * pdram_timing->trefi);
Caesar Wanga8456902016-10-27 01:12:34 +08001208 }
1209}
1210
1211static void gen_rk3399_pi_params_f1(struct timing_related_config *timing_config,
1212 struct dram_timing_t *pdram_timing)
1213{
1214 uint32_t tmp, tmp1, tmp2;
1215 uint32_t i;
1216
1217 for (i = 0; i < timing_config->ch_cnt; i++) {
1218 /* PI_04 PI_TDFI_PHYMSTR_MAX_F1:RW:0:32 */
1219 tmp = 4 * pdram_timing->trefi;
Caesar Wang8bc16672016-10-27 01:12:47 +08001220 mmio_write_32(PI_REG(i, 4), tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001221 /* PI_05 PI_TDFI_PHYMSTR_RESP_F1:RW:0:16 */
1222 tmp = 2 * pdram_timing->trefi;
Caesar Wang8bc16672016-10-27 01:12:47 +08001223 mmio_clrsetbits_32(PI_REG(i, 5), 0xffff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001224 /* PI_12 PI_TDFI_PHYUPD_RESP_F1:RW:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001225 mmio_clrsetbits_32(PI_REG(i, 12), 0xffff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001226
1227 /* PI_42 PI_TDELAY_RDWR_2_BUS_IDLE_F1:RW:8:8 */
1228 if (timing_config->dram_type == LPDDR4)
1229 tmp = 2;
1230 else
1231 tmp = 0;
1232 tmp = (pdram_timing->bl / 2) + 4 +
Caesar Wang8bc16672016-10-27 01:12:47 +08001233 (get_pi_rdlat_adj(pdram_timing) - 2) + tmp +
1234 get_pi_tdfi_phy_rdlat(pdram_timing, timing_config);
1235 mmio_clrsetbits_32(PI_REG(i, 42), 0xff << 8, tmp << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001236 /* PI_43 PI_WRLAT_F1:RW:24:5 */
1237 if (timing_config->dram_type == LPDDR3) {
1238 tmp = get_pi_wrlat(pdram_timing, timing_config);
Caesar Wang8bc16672016-10-27 01:12:47 +08001239 mmio_clrsetbits_32(PI_REG(i, 43), 0x1f << 24,
1240 tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001241 }
1242 /* PI_44 PI_ADDITIVE_LAT_F1:RW:0:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001243 mmio_clrsetbits_32(PI_REG(i, 44), 0x3f, PI_ADD_LATENCY);
Caesar Wanga8456902016-10-27 01:12:34 +08001244 /* PI_44 PI_CASLAT_LIN_F1:RW:8:7:=0x18 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001245 mmio_clrsetbits_32(PI_REG(i, 44), 0x7f << 8,
1246 pdram_timing->cl * 2);
Caesar Wanga8456902016-10-27 01:12:34 +08001247 /* PI_47 PI_TREF_F1:RW:16:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001248 mmio_clrsetbits_32(PI_REG(i, 47), 0xffff << 16,
1249 pdram_timing->trefi << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001250 /* PI_47 PI_TRFC_F1:RW:0:10 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001251 mmio_clrsetbits_32(PI_REG(i, 47), 0x3ff, pdram_timing->trfc);
Caesar Wanga8456902016-10-27 01:12:34 +08001252 /* PI_67 PI_TODTL_2CMD_F1:RW:8:8 */
1253 if (timing_config->dram_type == LPDDR3) {
1254 tmp = get_pi_todtoff_max(pdram_timing, timing_config);
Caesar Wang8bc16672016-10-27 01:12:47 +08001255 mmio_clrsetbits_32(PI_REG(i, 67), 0xff << 8, tmp << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001256 }
1257 /* PI_72 PI_WR_TO_ODTH_F1:RW:24:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001258 if ((timing_config->dram_type == LPDDR3) ||
1259 (timing_config->dram_type == LPDDR4)) {
Caesar Wanga8456902016-10-27 01:12:34 +08001260 tmp1 = get_pi_wrlat(pdram_timing, timing_config);
1261 tmp2 = get_pi_todtoff_max(pdram_timing, timing_config);
1262 if (tmp1 > tmp2)
1263 tmp = tmp1 - tmp2;
1264 else
1265 tmp = 0;
1266 } else if (timing_config->dram_type == DDR3) {
1267 tmp = 0;
1268 }
Caesar Wang8bc16672016-10-27 01:12:47 +08001269 mmio_clrsetbits_32(PI_REG(i, 72), 0x3f << 24, tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001270 /* PI_73 PI_RD_TO_ODTH_F1:RW:16:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001271 if ((timing_config->dram_type == LPDDR3) ||
1272 (timing_config->dram_type == LPDDR4)) {
1273 /* min_rl_preamble = cl + TDQSCK_MIN - 1 */
1274 tmp1 = pdram_timing->cl +
1275 get_pi_todtoff_min(pdram_timing, timing_config);
1276 tmp1--;
Caesar Wanga8456902016-10-27 01:12:34 +08001277 /* todtoff_max */
1278 tmp2 = get_pi_todtoff_max(pdram_timing, timing_config);
1279 if (tmp1 > tmp2)
1280 tmp = tmp1 - tmp2;
1281 else
1282 tmp = 0;
Caesar Wang8bc16672016-10-27 01:12:47 +08001283 } else if (timing_config->dram_type == DDR3)
Caesar Wanga8456902016-10-27 01:12:34 +08001284 tmp = pdram_timing->cl - pdram_timing->cwl;
Caesar Wang8bc16672016-10-27 01:12:47 +08001285
1286 mmio_clrsetbits_32(PI_REG(i, 73), 0x3f << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001287 /*P I_89 PI_RDLAT_ADJ_F1:RW:24:8 */
1288 tmp = get_pi_rdlat_adj(pdram_timing);
Caesar Wang8bc16672016-10-27 01:12:47 +08001289 mmio_clrsetbits_32(PI_REG(i, 89), 0xff << 24, tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001290 /* PI_90 PI_WRLAT_ADJ_F1:RW:24:8 */
1291 tmp = get_pi_wrlat_adj(pdram_timing, timing_config);
Caesar Wang8bc16672016-10-27 01:12:47 +08001292 mmio_clrsetbits_32(PI_REG(i, 90), 0xff << 24, tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001293 /* PI_91 PI_TDFI_WRCSLAT_F1:RW:24:8 */
1294 tmp1 = tmp;
Caesar Wang8bc16672016-10-27 01:12:47 +08001295 if (tmp1 == 0)
1296 tmp = 0;
1297 else if (tmp1 < 5)
1298 tmp = tmp1 - 1;
1299 else
Caesar Wanga8456902016-10-27 01:12:34 +08001300 tmp = tmp1 - 5;
Caesar Wang8bc16672016-10-27 01:12:47 +08001301 mmio_clrsetbits_32(PI_REG(i, 91), 0xff << 24, tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001302 /*PI_96 PI_TDFI_CALVL_CAPTURE_F1:RW:16:10 */
1303 /* tadr=20ns */
1304 tmp1 = 20000 / (1000000 / pdram_timing->mhz) + 1;
1305 if ((20000 % (1000000 / pdram_timing->mhz)) != 0)
1306 tmp1++;
1307 tmp = (tmp1 >> 1) + (tmp1 % 2) + 5;
Caesar Wang8bc16672016-10-27 01:12:47 +08001308 mmio_clrsetbits_32(PI_REG(i, 96), 0x3ff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001309 /* PI_96 PI_TDFI_CALVL_CC_F1:RW:0:10 */
1310 tmp = tmp + 18;
Caesar Wang8bc16672016-10-27 01:12:47 +08001311 mmio_clrsetbits_32(PI_REG(i, 96), 0x3ff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001312 /*PI_103 PI_TMRZ_F1:RW:0:5 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001313 mmio_clrsetbits_32(PI_REG(i, 103), 0x1f, pdram_timing->tmrz);
Caesar Wanga8456902016-10-27 01:12:34 +08001314 /*PI_111 PI_TDFI_CALVL_STROBE_F1:RW:16:4 */
1315 /* tds_train=ceil(2/ns) */
1316 tmp1 = 2 * 1000 / (1000000 / pdram_timing->mhz);
1317 if ((2 * 1000 % (1000000 / pdram_timing->mhz)) != 0)
1318 tmp1++;
1319 /* pi_tdfi_calvl_strobe=tds_train+5 */
1320 tmp = tmp1 + 5;
Caesar Wang8bc16672016-10-27 01:12:47 +08001321 mmio_clrsetbits_32(PI_REG(i, 111), 0xf << 16,
1322 tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001323 /* PI_116 PI_TCKEHDQS_F1:RW:24:6 */
1324 tmp = 10000 / (1000000 / pdram_timing->mhz);
1325 if ((10000 % (1000000 / pdram_timing->mhz)) != 0)
1326 tmp++;
1327 if (pdram_timing->mhz <= 100)
1328 tmp = tmp + 1;
1329 else
1330 tmp = tmp + 8;
Caesar Wang8bc16672016-10-27 01:12:47 +08001331 mmio_clrsetbits_32(PI_REG(i, 116), 0x3f << 24,
1332 tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001333 /* PI_128 PI_MR1_DATA_F1_0:RW+:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001334 mmio_clrsetbits_32(PI_REG(i, 128), 0xffff, pdram_timing->mr[1]);
Caesar Wanga8456902016-10-27 01:12:34 +08001335 /* PI_135 PI_MR1_DATA_F1_1:RW+:8:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001336 mmio_clrsetbits_32(PI_REG(i, 135), 0xffff << 8,
1337 pdram_timing->mr[1] << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001338 /* PI_143 PI_MR1_DATA_F1_2:RW+:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001339 mmio_clrsetbits_32(PI_REG(i, 143), 0xffff, pdram_timing->mr[1]);
Caesar Wanga8456902016-10-27 01:12:34 +08001340 /* PI_150 PI_MR1_DATA_F1_3:RW+:8:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001341 mmio_clrsetbits_32(PI_REG(i, 150), 0xffff << 8,
1342 pdram_timing->mr[1] << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001343 /* PI_128 PI_MR2_DATA_F1_0:RW+:16:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001344 mmio_clrsetbits_32(PI_REG(i, 128), 0xffff << 16,
1345 pdram_timing->mr[2] << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001346 /* PI_136 PI_MR2_DATA_F1_1:RW+:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001347 mmio_clrsetbits_32(PI_REG(i, 136), 0xffff, pdram_timing->mr[2]);
Caesar Wanga8456902016-10-27 01:12:34 +08001348 /* PI_143 PI_MR2_DATA_F1_2:RW+:16:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001349 mmio_clrsetbits_32(PI_REG(i, 143), 0xffff << 16,
1350 pdram_timing->mr[2] << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001351 /* PI_151 PI_MR2_DATA_F1_3:RW+:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001352 mmio_clrsetbits_32(PI_REG(i, 151), 0xffff, pdram_timing->mr[2]);
Caesar Wanga8456902016-10-27 01:12:34 +08001353 /* PI_156 PI_TFC_F1:RW:16:10 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001354 mmio_clrsetbits_32(PI_REG(i, 156), 0x3ff << 16,
1355 pdram_timing->trfc << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001356 /* PI_162 PI_TWR_F1:RW:8:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001357 mmio_clrsetbits_32(PI_REG(i, 162), 0x3f << 8,
1358 pdram_timing->twr << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001359 /* PI_162 PI_TWTR_F1:RW:0:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001360 mmio_clrsetbits_32(PI_REG(i, 162), 0x3f, pdram_timing->twtr);
Caesar Wanga8456902016-10-27 01:12:34 +08001361 /* PI_161 PI_TRCD_F1:RW:24:8 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001362 mmio_clrsetbits_32(PI_REG(i, 161), 0xff << 24,
1363 pdram_timing->trcd << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001364 /* PI_161 PI_TRP_F1:RW:16:8 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001365 mmio_clrsetbits_32(PI_REG(i, 161), 0xff << 16,
1366 pdram_timing->trp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001367 /* PI_161 PI_TRTP_F1:RW:8:8 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001368 mmio_clrsetbits_32(PI_REG(i, 161), 0xff << 8,
1369 pdram_timing->trtp << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001370 /* PI_163 PI_TRAS_MIN_F1:RW:24:8 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001371 mmio_clrsetbits_32(PI_REG(i, 163), 0xff << 24,
1372 pdram_timing->tras_min << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001373 /* PI_163 PI_TRAS_MAX_F1:RW:0:17 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001374 mmio_clrsetbits_32(PI_REG(i, 163), 0x1ffff,
1375 pdram_timing->tras_max * 99 / 100);
Caesar Wanga8456902016-10-27 01:12:34 +08001376 /* PI_164 PI_TMRD_F1:RW:16:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001377 mmio_clrsetbits_32(PI_REG(i, 164), 0x3f << 16,
1378 pdram_timing->tmrd << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001379 /* PI_164 PI_TDQSCK_MAX_F1:RW:0:4 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001380 mmio_clrsetbits_32(PI_REG(i, 164), 0xf,
1381 pdram_timing->tdqsck_max);
Caesar Wanga8456902016-10-27 01:12:34 +08001382 /* PI_189 PI_TDFI_CTRLUPD_MAX_F1:RW:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001383 mmio_clrsetbits_32(PI_REG(i, 189), 0xffff,
1384 2 * pdram_timing->trefi);
Caesar Wanga8456902016-10-27 01:12:34 +08001385 /* PI_190 PI_TDFI_CTRLUPD_INTERVAL_F1:RW:0:32 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001386 mmio_clrsetbits_32(PI_REG(i, 190), 0xffffffff,
1387 20 * pdram_timing->trefi);
Caesar Wanga8456902016-10-27 01:12:34 +08001388 }
1389}
1390
1391static void gen_rk3399_pi_params(struct timing_related_config *timing_config,
1392 struct dram_timing_t *pdram_timing,
1393 uint32_t fn)
1394{
1395 if (fn == 0)
1396 gen_rk3399_pi_params_f0(timing_config, pdram_timing);
1397 else
1398 gen_rk3399_pi_params_f1(timing_config, pdram_timing);
Caesar Wanga8456902016-10-27 01:12:34 +08001399}
1400
1401static void gen_rk3399_set_odt(uint32_t odt_en)
1402{
1403 uint32_t drv_odt_val;
1404 uint32_t i;
1405
1406 for (i = 0; i < rk3399_dram_status.timing_config.ch_cnt; i++) {
1407 drv_odt_val = (odt_en | (0 << 1) | (0 << 2)) << 16;
Caesar Wang8bc16672016-10-27 01:12:47 +08001408 mmio_clrsetbits_32(PHY_REG(i, 5), 0x7 << 16, drv_odt_val);
1409 mmio_clrsetbits_32(PHY_REG(i, 133), 0x7 << 16, drv_odt_val);
1410 mmio_clrsetbits_32(PHY_REG(i, 261), 0x7 << 16, drv_odt_val);
1411 mmio_clrsetbits_32(PHY_REG(i, 389), 0x7 << 16, drv_odt_val);
Caesar Wanga8456902016-10-27 01:12:34 +08001412 drv_odt_val = (odt_en | (0 << 1) | (0 << 2)) << 24;
Caesar Wang8bc16672016-10-27 01:12:47 +08001413 mmio_clrsetbits_32(PHY_REG(i, 6), 0x7 << 24, drv_odt_val);
1414 mmio_clrsetbits_32(PHY_REG(i, 134), 0x7 << 24, drv_odt_val);
1415 mmio_clrsetbits_32(PHY_REG(i, 262), 0x7 << 24, drv_odt_val);
1416 mmio_clrsetbits_32(PHY_REG(i, 390), 0x7 << 24, drv_odt_val);
Caesar Wanga8456902016-10-27 01:12:34 +08001417 }
Derek Basehoreb1065122016-10-20 22:09:22 -07001418}
1419
1420static void gen_rk3399_phy_dll_bypass(uint32_t mhz, uint32_t ch,
1421 uint32_t index, uint32_t dram_type)
1422{
1423 uint32_t sw_master_mode = 0;
1424 uint32_t rddqs_gate_delay, rddqs_latency, total_delay;
1425 uint32_t i;
1426
1427 if (dram_type == DDR3)
1428 total_delay = PI_PAD_DELAY_PS_VALUE;
1429 else if (dram_type == LPDDR3)
1430 total_delay = PI_PAD_DELAY_PS_VALUE + 2500;
1431 else
1432 total_delay = PI_PAD_DELAY_PS_VALUE + 1500;
1433 /* total_delay + 0.55tck */
1434 total_delay += (55 * 10000)/mhz;
1435 rddqs_latency = total_delay * mhz / 1000000;
1436 total_delay -= rddqs_latency * 1000000 / mhz;
1437 rddqs_gate_delay = total_delay * 0x200 * mhz / 1000000;
1438 if (mhz <= PHY_DLL_BYPASS_FREQ) {
1439 sw_master_mode = 0xc;
1440 mmio_setbits_32(PHY_REG(ch, 514), 1);
1441 mmio_setbits_32(PHY_REG(ch, 642), 1);
1442 mmio_setbits_32(PHY_REG(ch, 770), 1);
1443
1444 /* setting bypass mode slave delay */
1445 for (i = 0; i < 4; i++) {
1446 /* wr dq delay = -180deg + (0x60 / 4) * 20ps */
1447 mmio_clrsetbits_32(PHY_REG(ch, 1 + 128 * i), 0x7ff << 8,
1448 0x4a0 << 8);
1449 /* rd dqs/dq delay = (0x60 / 4) * 20ps */
1450 mmio_clrsetbits_32(PHY_REG(ch, 11 + 128 * i), 0x3ff,
1451 0xa0);
1452 /* rd rddqs_gate delay */
1453 mmio_clrsetbits_32(PHY_REG(ch, 2 + 128 * i), 0x3ff,
1454 rddqs_gate_delay);
1455 mmio_clrsetbits_32(PHY_REG(ch, 78 + 128 * i), 0xf,
1456 rddqs_latency);
1457 }
1458 for (i = 0; i < 3; i++)
1459 /* adr delay */
1460 mmio_clrsetbits_32(PHY_REG(ch, 513 + 128 * i),
1461 0x7ff << 16, 0x80 << 16);
1462
1463 if ((mmio_read_32(PHY_REG(ch, 86)) & 0xc00) == 0) {
1464 /*
1465 * old status is normal mode,
1466 * and saving the wrdqs slave delay
1467 */
1468 for (i = 0; i < 4; i++) {
1469 /* save and clear wr dqs slave delay */
1470 wrdqs_delay_val[ch][index][i] = 0x3ff &
1471 (mmio_read_32(PHY_REG(ch, 63 + i * 128))
1472 >> 16);
1473 mmio_clrsetbits_32(PHY_REG(ch, 63 + i * 128),
1474 0x03ff << 16, 0 << 16);
1475 /*
1476 * in normal mode the cmd may delay 1cycle by
1477 * wrlvl and in bypass mode making dqs also
1478 * delay 1cycle.
1479 */
1480 mmio_clrsetbits_32(PHY_REG(ch, 78 + i * 128),
1481 0x07 << 8, 0x1 << 8);
1482 }
1483 }
1484 } else if (mmio_read_32(PHY_REG(ch, 86)) & 0xc00) {
1485 /* old status is bypass mode and restore wrlvl resume */
1486 for (i = 0; i < 4; i++) {
1487 mmio_clrsetbits_32(PHY_REG(ch, 63 + i * 128),
1488 0x03ff << 16,
1489 (wrdqs_delay_val[ch][index][i] &
1490 0x3ff) << 16);
1491 /* resume phy_write_path_lat_add */
1492 mmio_clrbits_32(PHY_REG(ch, 78 + i * 128), 0x07 << 8);
1493 }
1494 }
1495
1496 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
1497 mmio_clrsetbits_32(PHY_REG(ch, 86), 0xf << 8, sw_master_mode << 8);
1498 mmio_clrsetbits_32(PHY_REG(ch, 214), 0xf << 8, sw_master_mode << 8);
1499 mmio_clrsetbits_32(PHY_REG(ch, 342), 0xf << 8, sw_master_mode << 8);
1500 mmio_clrsetbits_32(PHY_REG(ch, 470), 0xf << 8, sw_master_mode << 8);
1501
1502 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
1503 mmio_clrsetbits_32(PHY_REG(ch, 547), 0xf << 16, sw_master_mode << 16);
1504 mmio_clrsetbits_32(PHY_REG(ch, 675), 0xf << 16, sw_master_mode << 16);
1505 mmio_clrsetbits_32(PHY_REG(ch, 803), 0xf << 16, sw_master_mode << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001506}
1507
Caesar Wanga8456902016-10-27 01:12:34 +08001508static void gen_rk3399_phy_params(struct timing_related_config *timing_config,
1509 struct drv_odt_lp_config *drv_config,
1510 struct dram_timing_t *pdram_timing,
1511 uint32_t fn)
1512{
1513 uint32_t tmp, i, div, j;
1514 uint32_t mem_delay_ps, pad_delay_ps, total_delay_ps, delay_frac_ps;
1515 uint32_t trpre_min_ps, gate_delay_ps, gate_delay_frac_ps;
1516 uint32_t ie_enable, tsel_enable, cas_lat, rddata_en_ie_dly, tsel_adder;
1517 uint32_t extra_adder, delta, hs_offset;
1518
1519 for (i = 0; i < timing_config->ch_cnt; i++) {
1520
1521 pad_delay_ps = PI_PAD_DELAY_PS_VALUE;
1522 ie_enable = PI_IE_ENABLE_VALUE;
1523 tsel_enable = PI_TSEL_ENABLE_VALUE;
1524
Caesar Wang8bc16672016-10-27 01:12:47 +08001525 mmio_clrsetbits_32(PHY_REG(i, 896), (0x3 << 8) | 1, fn << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001526
1527 /* PHY_LOW_FREQ_SEL */
1528 /* DENALI_PHY_913 1bit offset_0 */
1529 if (timing_config->freq > 400)
Caesar Wang8bc16672016-10-27 01:12:47 +08001530 mmio_clrbits_32(PHY_REG(i, 913), 1);
Caesar Wanga8456902016-10-27 01:12:34 +08001531 else
Caesar Wang8bc16672016-10-27 01:12:47 +08001532 mmio_setbits_32(PHY_REG(i, 913), 1);
Caesar Wanga8456902016-10-27 01:12:34 +08001533
1534 /* PHY_RPTR_UPDATE_x */
1535 /* DENALI_PHY_87/215/343/471 4bit offset_16 */
1536 tmp = 2500 / (1000000 / pdram_timing->mhz) + 3;
1537 if ((2500 % (1000000 / pdram_timing->mhz)) != 0)
1538 tmp++;
Caesar Wang8bc16672016-10-27 01:12:47 +08001539 mmio_clrsetbits_32(PHY_REG(i, 87), 0xf << 16, tmp << 16);
1540 mmio_clrsetbits_32(PHY_REG(i, 215), 0xf << 16, tmp << 16);
1541 mmio_clrsetbits_32(PHY_REG(i, 343), 0xf << 16, tmp << 16);
1542 mmio_clrsetbits_32(PHY_REG(i, 471), 0xf << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001543
1544 /* PHY_PLL_CTRL */
1545 /* DENALI_PHY_911 13bits offset_0 */
1546 /* PHY_LP4_BOOT_PLL_CTRL */
1547 /* DENALI_PHY_919 13bits offset_0 */
Lin Huang52512c22016-12-15 15:08:47 +08001548 tmp = (1 << 12) | (2 << 7) | (1 << 1);
Caesar Wang8bc16672016-10-27 01:12:47 +08001549 mmio_clrsetbits_32(PHY_REG(i, 911), 0x1fff, tmp);
1550 mmio_clrsetbits_32(PHY_REG(i, 919), 0x1fff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001551
1552 /* PHY_PLL_CTRL_CA */
1553 /* DENALI_PHY_911 13bits offset_16 */
1554 /* PHY_LP4_BOOT_PLL_CTRL_CA */
1555 /* DENALI_PHY_919 13bits offset_16 */
Lin Huang52512c22016-12-15 15:08:47 +08001556 tmp = (2 << 7) | (1 << 5) | (1 << 1);
Caesar Wang8bc16672016-10-27 01:12:47 +08001557 mmio_clrsetbits_32(PHY_REG(i, 911), 0x1fff << 16, tmp << 16);
1558 mmio_clrsetbits_32(PHY_REG(i, 919), 0x1fff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001559
1560 /* PHY_TCKSRE_WAIT */
1561 /* DENALI_PHY_922 4bits offset_24 */
1562 if (pdram_timing->mhz <= 400)
1563 tmp = 1;
1564 else if (pdram_timing->mhz <= 800)
1565 tmp = 3;
1566 else if (pdram_timing->mhz <= 1000)
1567 tmp = 4;
1568 else
1569 tmp = 5;
Caesar Wang8bc16672016-10-27 01:12:47 +08001570 mmio_clrsetbits_32(PHY_REG(i, 922), 0xf << 24, tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001571 /* PHY_CAL_CLK_SELECT_0:RW8:3 */
1572 div = pdram_timing->mhz / (2 * 20);
1573 for (j = 2, tmp = 1; j <= 128; j <<= 1, tmp++) {
1574 if (div < j)
1575 break;
1576 }
Caesar Wang8bc16672016-10-27 01:12:47 +08001577 mmio_clrsetbits_32(PHY_REG(i, 947), 0x7 << 8, tmp << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001578
1579 if (timing_config->dram_type == DDR3) {
1580 mem_delay_ps = 0;
1581 trpre_min_ps = 1000;
1582 } else if (timing_config->dram_type == LPDDR4) {
1583 mem_delay_ps = 1500;
1584 trpre_min_ps = 900;
1585 } else if (timing_config->dram_type == LPDDR3) {
1586 mem_delay_ps = 2500;
1587 trpre_min_ps = 900;
1588 } else {
1589 ERROR("gen_rk3399_phy_params:dramtype unsupport\n");
1590 return;
1591 }
1592 total_delay_ps = mem_delay_ps + pad_delay_ps;
Caesar Wang8bc16672016-10-27 01:12:47 +08001593 delay_frac_ps = 1000 * total_delay_ps /
1594 (1000000 / pdram_timing->mhz);
Caesar Wanga8456902016-10-27 01:12:34 +08001595 gate_delay_ps = delay_frac_ps + 1000 - (trpre_min_ps / 2);
Caesar Wang8bc16672016-10-27 01:12:47 +08001596 gate_delay_frac_ps = gate_delay_ps % 1000;
Caesar Wanga8456902016-10-27 01:12:34 +08001597 tmp = gate_delay_frac_ps * 0x200 / 1000;
Caesar Wanga8456902016-10-27 01:12:34 +08001598 /* PHY_RDDQS_GATE_SLAVE_DELAY */
1599 /* DENALI_PHY_77/205/333/461 10bits offset_16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001600 mmio_clrsetbits_32(PHY_REG(i, 77), 0x2ff << 16, tmp << 16);
1601 mmio_clrsetbits_32(PHY_REG(i, 205), 0x2ff << 16, tmp << 16);
1602 mmio_clrsetbits_32(PHY_REG(i, 333), 0x2ff << 16, tmp << 16);
1603 mmio_clrsetbits_32(PHY_REG(i, 461), 0x2ff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001604
1605 tmp = gate_delay_ps / 1000;
1606 /* PHY_LP4_BOOT_RDDQS_LATENCY_ADJUST */
1607 /* DENALI_PHY_10/138/266/394 4bit offset_0 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001608 mmio_clrsetbits_32(PHY_REG(i, 10), 0xf, tmp);
1609 mmio_clrsetbits_32(PHY_REG(i, 138), 0xf, tmp);
1610 mmio_clrsetbits_32(PHY_REG(i, 266), 0xf, tmp);
1611 mmio_clrsetbits_32(PHY_REG(i, 394), 0xf, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001612 /* PHY_GTLVL_LAT_ADJ_START */
1613 /* DENALI_PHY_80/208/336/464 4bits offset_16 */
1614 tmp = delay_frac_ps / 1000;
Caesar Wang8bc16672016-10-27 01:12:47 +08001615 mmio_clrsetbits_32(PHY_REG(i, 80), 0xf << 16, tmp << 16);
1616 mmio_clrsetbits_32(PHY_REG(i, 208), 0xf << 16, tmp << 16);
1617 mmio_clrsetbits_32(PHY_REG(i, 336), 0xf << 16, tmp << 16);
1618 mmio_clrsetbits_32(PHY_REG(i, 464), 0xf << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001619
1620 cas_lat = pdram_timing->cl + PI_ADD_LATENCY;
1621 rddata_en_ie_dly = ie_enable / (1000000 / pdram_timing->mhz);
1622 if ((ie_enable % (1000000 / pdram_timing->mhz)) != 0)
1623 rddata_en_ie_dly++;
1624 rddata_en_ie_dly = rddata_en_ie_dly - 1;
1625 tsel_adder = tsel_enable / (1000000 / pdram_timing->mhz);
1626 if ((tsel_enable % (1000000 / pdram_timing->mhz)) != 0)
1627 tsel_adder++;
1628 if (rddata_en_ie_dly > tsel_adder)
1629 extra_adder = rddata_en_ie_dly - tsel_adder;
1630 else
1631 extra_adder = 0;
1632 delta = cas_lat - rddata_en_ie_dly;
1633 if (PI_REGS_DIMM_SUPPORT && PI_DOUBLEFREEK)
1634 hs_offset = 2;
1635 else
1636 hs_offset = 1;
Caesar Wang8bc16672016-10-27 01:12:47 +08001637 if (rddata_en_ie_dly > (cas_lat - 1 - hs_offset))
Caesar Wanga8456902016-10-27 01:12:34 +08001638 tmp = 0;
Caesar Wang8bc16672016-10-27 01:12:47 +08001639 else if ((delta == 2) || (delta == 1))
1640 tmp = rddata_en_ie_dly - 0 - extra_adder;
1641 else
1642 tmp = extra_adder;
Caesar Wanga8456902016-10-27 01:12:34 +08001643 /* PHY_LP4_BOOT_RDDATA_EN_TSEL_DLY */
1644 /* DENALI_PHY_9/137/265/393 4bit offset_16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001645 mmio_clrsetbits_32(PHY_REG(i, 9), 0xf << 16, tmp << 16);
1646 mmio_clrsetbits_32(PHY_REG(i, 137), 0xf << 16, tmp << 16);
1647 mmio_clrsetbits_32(PHY_REG(i, 265), 0xf << 16, tmp << 16);
1648 mmio_clrsetbits_32(PHY_REG(i, 393), 0xf << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001649 /* PHY_RDDATA_EN_TSEL_DLY */
1650 /* DENALI_PHY_86/214/342/470 4bit offset_0 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001651 mmio_clrsetbits_32(PHY_REG(i, 86), 0xf, tmp);
1652 mmio_clrsetbits_32(PHY_REG(i, 214), 0xf, tmp);
1653 mmio_clrsetbits_32(PHY_REG(i, 342), 0xf, tmp);
1654 mmio_clrsetbits_32(PHY_REG(i, 470), 0xf, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001655
1656 if (tsel_adder > rddata_en_ie_dly)
1657 extra_adder = tsel_adder - rddata_en_ie_dly;
1658 else
1659 extra_adder = 0;
1660 if (rddata_en_ie_dly > (cas_lat - 1 - hs_offset))
1661 tmp = tsel_adder;
1662 else
1663 tmp = rddata_en_ie_dly - 0 + extra_adder;
1664 /* PHY_LP4_BOOT_RDDATA_EN_DLY */
1665 /* DENALI_PHY_9/137/265/393 4bit offset_8 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001666 mmio_clrsetbits_32(PHY_REG(i, 9), 0xf << 8, tmp << 8);
1667 mmio_clrsetbits_32(PHY_REG(i, 137), 0xf << 8, tmp << 8);
1668 mmio_clrsetbits_32(PHY_REG(i, 265), 0xf << 8, tmp << 8);
1669 mmio_clrsetbits_32(PHY_REG(i, 393), 0xf << 8, tmp << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001670 /* PHY_RDDATA_EN_DLY */
1671 /* DENALI_PHY_85/213/341/469 4bit offset_24 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001672 mmio_clrsetbits_32(PHY_REG(i, 85), 0xf << 24, tmp << 24);
1673 mmio_clrsetbits_32(PHY_REG(i, 213), 0xf << 24, tmp << 24);
1674 mmio_clrsetbits_32(PHY_REG(i, 341), 0xf << 24, tmp << 24);
1675 mmio_clrsetbits_32(PHY_REG(i, 469), 0xf << 24, tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001676
1677 if (pdram_timing->mhz <= ENPER_CS_TRAINING_FREQ) {
Caesar Wanga8456902016-10-27 01:12:34 +08001678 /*
1679 * Note:Per-CS Training is not compatible at speeds
1680 * under 533 MHz. If the PHY is running at a speed
1681 * less than 533MHz, all phy_per_cs_training_en_X
1682 * parameters must be cleared to 0.
1683 */
1684
1685 /*DENALI_PHY_84/212/340/468 1bit offset_16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001686 mmio_clrbits_32(PHY_REG(i, 84), 0x1 << 16);
1687 mmio_clrbits_32(PHY_REG(i, 212), 0x1 << 16);
1688 mmio_clrbits_32(PHY_REG(i, 340), 0x1 << 16);
1689 mmio_clrbits_32(PHY_REG(i, 468), 0x1 << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001690 } else {
Caesar Wang8bc16672016-10-27 01:12:47 +08001691 mmio_setbits_32(PHY_REG(i, 84), 0x1 << 16);
1692 mmio_setbits_32(PHY_REG(i, 212), 0x1 << 16);
1693 mmio_setbits_32(PHY_REG(i, 340), 0x1 << 16);
1694 mmio_setbits_32(PHY_REG(i, 468), 0x1 << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001695 }
Derek Basehoreb1065122016-10-20 22:09:22 -07001696 gen_rk3399_phy_dll_bypass(pdram_timing->mhz, i, fn,
1697 timing_config->dram_type);
Caesar Wanga8456902016-10-27 01:12:34 +08001698 }
1699}
1700
1701static int to_get_clk_index(unsigned int mhz)
1702{
1703 int pll_cnt, i;
1704
1705 pll_cnt = ARRAY_SIZE(dpll_rates_table);
1706
1707 /* Assumming rate_table is in descending order */
1708 for (i = 0; i < pll_cnt; i++) {
1709 if (mhz >= dpll_rates_table[i].mhz)
1710 break;
1711 }
1712
1713 /* if mhz lower than lowest frequency in table, use lowest frequency */
1714 if (i == pll_cnt)
1715 i = pll_cnt - 1;
1716
1717 return i;
1718}
1719
Caesar Wanga8456902016-10-27 01:12:34 +08001720uint32_t ddr_get_rate(void)
1721{
1722 uint32_t refdiv, postdiv1, fbdiv, postdiv2;
1723
1724 refdiv = mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 1)) & 0x3f;
1725 fbdiv = mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 0)) & 0xfff;
1726 postdiv1 =
1727 (mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 1)) >> 8) & 0x7;
1728 postdiv2 =
1729 (mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 1)) >> 12) & 0x7;
1730
1731 return (24 / refdiv * fbdiv / postdiv1 / postdiv2) * 1000 * 1000;
1732}
1733
1734/*
1735 * return: bit12: channel 1, external self-refresh
1736 * bit11: channel 1, stdby_mode
1737 * bit10: channel 1, self-refresh with controller and memory clock gate
1738 * bit9: channel 1, self-refresh
1739 * bit8: channel 1, power-down
1740 *
1741 * bit4: channel 1, external self-refresh
1742 * bit3: channel 0, stdby_mode
1743 * bit2: channel 0, self-refresh with controller and memory clock gate
1744 * bit1: channel 0, self-refresh
1745 * bit0: channel 0, power-down
1746 */
1747uint32_t exit_low_power(void)
1748{
Caesar Wanga8456902016-10-27 01:12:34 +08001749 uint32_t low_power = 0;
1750 uint32_t channel_mask;
Caesar Wang8bc16672016-10-27 01:12:47 +08001751 uint32_t tmp, i;
Caesar Wanga8456902016-10-27 01:12:34 +08001752
Caesar Wang8bc16672016-10-27 01:12:47 +08001753 channel_mask = (mmio_read_32(PMUGRF_BASE + PMUGRF_OSREG(2)) >> 28) &
1754 0x3;
1755 for (i = 0; i < 2; i++) {
1756 if (!(channel_mask & (1 << i)))
Caesar Wanga8456902016-10-27 01:12:34 +08001757 continue;
1758
1759 /* exit stdby mode */
Caesar Wang8bc16672016-10-27 01:12:47 +08001760 mmio_write_32(CIC_BASE + CIC_CTRL1,
1761 (1 << (i + 16)) | (0 << i));
Caesar Wanga8456902016-10-27 01:12:34 +08001762 /* exit external self-refresh */
Caesar Wang8bc16672016-10-27 01:12:47 +08001763 tmp = i ? 12 : 8;
1764 low_power |= ((mmio_read_32(PMU_BASE + PMU_SFT_CON) >> tmp) &
1765 0x1) << (4 + 8 * i);
1766 mmio_clrbits_32(PMU_BASE + PMU_SFT_CON, 1 << tmp);
1767 while (!(mmio_read_32(PMU_BASE + PMU_DDR_SREF_ST) & (1 << i)))
Caesar Wanga8456902016-10-27 01:12:34 +08001768 ;
1769 /* exit auto low-power */
Caesar Wang8bc16672016-10-27 01:12:47 +08001770 mmio_clrbits_32(CTL_REG(i, 101), 0x7);
Caesar Wanga8456902016-10-27 01:12:34 +08001771 /* lp_cmd to exit */
Caesar Wang8bc16672016-10-27 01:12:47 +08001772 if (((mmio_read_32(CTL_REG(i, 100)) >> 24) & 0x7f) !=
1773 0x40) {
1774 while (mmio_read_32(CTL_REG(i, 200)) & 0x1)
Caesar Wanga8456902016-10-27 01:12:34 +08001775 ;
Caesar Wang8bc16672016-10-27 01:12:47 +08001776 mmio_clrsetbits_32(CTL_REG(i, 93), 0xff << 24,
1777 0x69 << 24);
1778 while (((mmio_read_32(CTL_REG(i, 100)) >> 24) & 0x7f) !=
1779 0x40)
Caesar Wanga8456902016-10-27 01:12:34 +08001780 ;
1781 }
1782 }
1783 return low_power;
1784}
1785
1786void resume_low_power(uint32_t low_power)
1787{
Caesar Wanga8456902016-10-27 01:12:34 +08001788 uint32_t channel_mask;
Caesar Wang8bc16672016-10-27 01:12:47 +08001789 uint32_t tmp, i, val;
Caesar Wanga8456902016-10-27 01:12:34 +08001790
Caesar Wang8bc16672016-10-27 01:12:47 +08001791 channel_mask = (mmio_read_32(PMUGRF_BASE + PMUGRF_OSREG(2)) >> 28) &
1792 0x3;
1793 for (i = 0; i < 2; i++) {
1794 if (!(channel_mask & (1 << i)))
Caesar Wanga8456902016-10-27 01:12:34 +08001795 continue;
1796
1797 /* resume external self-refresh */
Caesar Wang8bc16672016-10-27 01:12:47 +08001798 tmp = i ? 12 : 8;
1799 val = (low_power >> (4 + 8 * i)) & 0x1;
1800 mmio_setbits_32(PMU_BASE + PMU_SFT_CON, val << tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001801 /* resume auto low-power */
Caesar Wang8bc16672016-10-27 01:12:47 +08001802 val = (low_power >> (8 * i)) & 0x7;
1803 mmio_setbits_32(CTL_REG(i, 101), val);
Caesar Wanga8456902016-10-27 01:12:34 +08001804 /* resume stdby mode */
Caesar Wang8bc16672016-10-27 01:12:47 +08001805 val = (low_power >> (3 + 8 * i)) & 0x1;
1806 mmio_write_32(CIC_BASE + CIC_CTRL1,
1807 (1 << (i + 16)) | (val << i));
Caesar Wanga8456902016-10-27 01:12:34 +08001808 }
1809}
1810
Derek Basehoreff461d02016-10-20 20:46:43 -07001811static void dram_low_power_config(void)
Caesar Wanga8456902016-10-27 01:12:34 +08001812{
Derek Basehoreff461d02016-10-20 20:46:43 -07001813 uint32_t tmp, i;
Caesar Wanga8456902016-10-27 01:12:34 +08001814 uint32_t ch_cnt = rk3399_dram_status.timing_config.ch_cnt;
1815 uint32_t dram_type = rk3399_dram_status.timing_config.dram_type;
Caesar Wanga8456902016-10-27 01:12:34 +08001816
1817 if (dram_type == DDR3)
Derek Basehoreff461d02016-10-20 20:46:43 -07001818 tmp = (2 << 16) | (0x7 << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001819 else
Derek Basehoreff461d02016-10-20 20:46:43 -07001820 tmp = (3 << 16) | (0x7 << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001821
Derek Basehoreff461d02016-10-20 20:46:43 -07001822 for (i = 0; i < ch_cnt; i++)
1823 mmio_clrsetbits_32(CTL_REG(i, 101), 0x70f0f, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001824
1825 /* standby idle */
Caesar Wang8bc16672016-10-27 01:12:47 +08001826 mmio_write_32(CIC_BASE + CIC_CG_WAIT_TH, 0x640008);
Caesar Wanga8456902016-10-27 01:12:34 +08001827
1828 if (ch_cnt == 2) {
Caesar Wang8bc16672016-10-27 01:12:47 +08001829 mmio_write_32(GRF_BASE + GRF_DDRC1_CON1,
1830 (((0x1<<4) | (0x1<<5) | (0x1<<6) |
1831 (0x1<<7)) << 16) |
1832 ((0x1<<4) | (0x0<<5) | (0x1<<6) | (0x1<<7)));
Derek Basehoreff461d02016-10-20 20:46:43 -07001833 mmio_write_32(CIC_BASE + CIC_CTRL1, 0x002a0028);
Caesar Wanga8456902016-10-27 01:12:34 +08001834 }
1835
Caesar Wang8bc16672016-10-27 01:12:47 +08001836 mmio_write_32(GRF_BASE + GRF_DDRC0_CON1,
1837 (((0x1<<4) | (0x1<<5) | (0x1<<6) | (0x1<<7)) << 16) |
1838 ((0x1<<4) | (0x0<<5) | (0x1<<6) | (0x1<<7)));
Derek Basehoreff461d02016-10-20 20:46:43 -07001839 mmio_write_32(CIC_BASE + CIC_CTRL1, 0x00150014);
Caesar Wanga8456902016-10-27 01:12:34 +08001840}
1841
Derek Basehoreff461d02016-10-20 20:46:43 -07001842void dram_dfs_init(void)
Caesar Wanga8456902016-10-27 01:12:34 +08001843{
Derek Basehoree13bc542017-02-24 14:31:36 +08001844 uint32_t trefi0, trefi1, boot_freq;
Caesar Wanga8456902016-10-27 01:12:34 +08001845
1846 /* get sdram config for os reg */
Derek Basehoreff461d02016-10-20 20:46:43 -07001847 get_dram_drv_odt_val(sdram_config.dramtype,
1848 &rk3399_dram_status.drv_odt_lp_cfg);
Caesar Wanga8456902016-10-27 01:12:34 +08001849 sdram_timing_cfg_init(&rk3399_dram_status.timing_config,
1850 &sdram_config,
1851 &rk3399_dram_status.drv_odt_lp_cfg);
1852
Caesar Wang8bc16672016-10-27 01:12:47 +08001853 trefi0 = ((mmio_read_32(CTL_REG(0, 48)) >> 16) & 0xffff) + 8;
1854 trefi1 = ((mmio_read_32(CTL_REG(0, 49)) >> 16) & 0xffff) + 8;
Caesar Wanga8456902016-10-27 01:12:34 +08001855
1856 rk3399_dram_status.index_freq[0] = trefi0 * 10 / 39;
1857 rk3399_dram_status.index_freq[1] = trefi1 * 10 / 39;
1858 rk3399_dram_status.current_index =
Caesar Wang8bc16672016-10-27 01:12:47 +08001859 (mmio_read_32(CTL_REG(0, 111)) >> 16) & 0x3;
Caesar Wanga8456902016-10-27 01:12:34 +08001860 if (rk3399_dram_status.timing_config.dram_type == DDR3) {
1861 rk3399_dram_status.index_freq[0] /= 2;
1862 rk3399_dram_status.index_freq[1] /= 2;
1863 }
Derek Basehoree13bc542017-02-24 14:31:36 +08001864 boot_freq =
1865 rk3399_dram_status.index_freq[rk3399_dram_status.current_index];
1866 boot_freq = dpll_rates_table[to_get_clk_index(boot_freq)].mhz;
1867 rk3399_dram_status.boot_freq = boot_freq;
1868 rk3399_dram_status.index_freq[rk3399_dram_status.current_index] =
1869 boot_freq;
1870 rk3399_dram_status.index_freq[(rk3399_dram_status.current_index + 1) &
1871 0x1] = 0;
1872 rk3399_dram_status.low_power_stat = 0;
Xing Zheng93280b72016-10-26 21:25:26 +08001873 /*
1874 * following register decide if NOC stall the access request
1875 * or return error when NOC being idled. when doing ddr frequency
1876 * scaling in M0 or DCF, we need to make sure noc stall the access
1877 * request, if return error cpu may data abort when ddr frequency
1878 * changing. it don't need to set this register every times,
1879 * so we init this register in function dram_dfs_init().
1880 */
1881 mmio_write_32(GRF_BASE + GRF_SOC_CON(0), 0xffffffff);
1882 mmio_write_32(GRF_BASE + GRF_SOC_CON(1), 0xffffffff);
1883 mmio_write_32(GRF_BASE + GRF_SOC_CON(2), 0xffffffff);
1884 mmio_write_32(GRF_BASE + GRF_SOC_CON(3), 0xffffffff);
1885 mmio_write_32(GRF_BASE + GRF_SOC_CON(4), 0x70007000);
1886
Derek Basehoree13bc542017-02-24 14:31:36 +08001887 /* Disable multicast */
1888 mmio_clrbits_32(PHY_REG(0, 896), 1);
1889 mmio_clrbits_32(PHY_REG(1, 896), 1);
1890
Derek Basehoreff461d02016-10-20 20:46:43 -07001891 dram_low_power_config();
1892}
Caesar Wanga8456902016-10-27 01:12:34 +08001893
Derek Basehoreff461d02016-10-20 20:46:43 -07001894/*
1895 * arg0: bit0-7: sr_idle; bit8-15:sr_mc_gate_idle; bit16-31: standby idle
1896 * arg1: bit0-11: pd_idle; bit 16-27: srpd_lite_idle
1897 * arg2: bit0: if odt en
1898 */
1899uint32_t dram_set_odt_pd(uint32_t arg0, uint32_t arg1, uint32_t arg2)
1900{
1901 struct drv_odt_lp_config *lp_cfg = &rk3399_dram_status.drv_odt_lp_cfg;
1902 uint32_t *low_power = &rk3399_dram_status.low_power_stat;
1903 uint32_t dram_type, ch_count, pd_tmp, sr_tmp, i;
1904
1905 dram_type = rk3399_dram_status.timing_config.dram_type;
1906 ch_count = rk3399_dram_status.timing_config.ch_cnt;
1907
1908 lp_cfg->sr_idle = arg0 & 0xff;
1909 lp_cfg->sr_mc_gate_idle = (arg0 >> 8) & 0xff;
1910 lp_cfg->standby_idle = (arg0 >> 16) & 0xffff;
1911 lp_cfg->pd_idle = arg1 & 0xfff;
1912 lp_cfg->srpd_lite_idle = (arg1 >> 16) & 0xfff;
1913
1914 rk3399_dram_status.timing_config.odt = arg2 & 0x1;
1915
1916 exit_low_power();
1917
1918 *low_power = 0;
Caesar Wanga8456902016-10-27 01:12:34 +08001919
Derek Basehoreff461d02016-10-20 20:46:43 -07001920 /* pd_idle en */
1921 if (lp_cfg->pd_idle)
1922 *low_power |= ((1 << 0) | (1 << 8));
1923 /* sr_idle en srpd_lite_idle */
1924 if (lp_cfg->sr_idle | lp_cfg->srpd_lite_idle)
1925 *low_power |= ((1 << 1) | (1 << 9));
1926 /* sr_mc_gate_idle */
1927 if (lp_cfg->sr_mc_gate_idle)
1928 *low_power |= ((1 << 2) | (1 << 10));
1929 /* standbyidle */
1930 if (lp_cfg->standby_idle) {
1931 if (rk3399_dram_status.timing_config.ch_cnt == 2)
1932 *low_power |= ((1 << 3) | (1 << 11));
1933 else
1934 *low_power |= (1 << 3);
Caesar Wanga8456902016-10-27 01:12:34 +08001935 }
1936
Derek Basehoreff461d02016-10-20 20:46:43 -07001937 pd_tmp = arg1;
1938 if (dram_type != LPDDR4)
1939 pd_tmp = arg1 & 0xfff;
1940 sr_tmp = arg0 & 0xffff;
1941 for (i = 0; i < ch_count; i++) {
1942 mmio_write_32(CTL_REG(i, 102), pd_tmp);
1943 mmio_clrsetbits_32(CTL_REG(i, 103), 0xffff, sr_tmp);
1944 }
1945 mmio_write_32(CIC_BASE + CIC_IDLE_TH, (arg0 >> 16) & 0xffff);
1946
1947 return 0;
Caesar Wanga8456902016-10-27 01:12:34 +08001948}
1949
Xing Zheng93280b72016-10-26 21:25:26 +08001950static void m0_configure_ddr(struct pll_div pll_div, uint32_t ddr_index)
1951{
1952 /* set PARAM to M0_FUNC_DRAM */
1953 mmio_write_32(M0_PARAM_ADDR + PARAM_M0_FUNC, M0_FUNC_DRAM);
1954
1955 mmio_write_32(M0_PARAM_ADDR + PARAM_DPLL_CON0, FBDIV(pll_div.fbdiv));
1956 mmio_write_32(M0_PARAM_ADDR + PARAM_DPLL_CON1,
1957 POSTDIV2(pll_div.postdiv2) | POSTDIV1(pll_div.postdiv1) |
1958 REFDIV(pll_div.refdiv));
1959
1960 mmio_write_32(M0_PARAM_ADDR + PARAM_DRAM_FREQ, pll_div.mhz);
1961
1962 mmio_write_32(M0_PARAM_ADDR + PARAM_FREQ_SELECT, ddr_index << 4);
Lin Huangb4a76762016-12-12 15:18:08 +08001963 dmbst();
Xing Zheng93280b72016-10-26 21:25:26 +08001964}
1965
Caesar Wanga8456902016-10-27 01:12:34 +08001966static uint32_t prepare_ddr_timing(uint32_t mhz)
1967{
1968 uint32_t index;
1969 struct dram_timing_t dram_timing;
1970
1971 rk3399_dram_status.timing_config.freq = mhz;
1972
Derek Basehoreff461d02016-10-20 20:46:43 -07001973 if (mhz < 300)
Caesar Wanga8456902016-10-27 01:12:34 +08001974 rk3399_dram_status.timing_config.dllbp = 1;
1975 else
1976 rk3399_dram_status.timing_config.dllbp = 0;
Derek Basehoreff461d02016-10-20 20:46:43 -07001977
1978 if (rk3399_dram_status.timing_config.odt == 1)
Caesar Wanga8456902016-10-27 01:12:34 +08001979 gen_rk3399_set_odt(1);
Caesar Wanga8456902016-10-27 01:12:34 +08001980
1981 index = (rk3399_dram_status.current_index + 1) & 0x1;
Caesar Wanga8456902016-10-27 01:12:34 +08001982
1983 /*
1984 * checking if having available gate traiing timing for
1985 * target freq.
1986 */
1987 dram_get_parameter(&rk3399_dram_status.timing_config, &dram_timing);
1988 gen_rk3399_ctl_params(&rk3399_dram_status.timing_config,
1989 &dram_timing, index);
1990 gen_rk3399_pi_params(&rk3399_dram_status.timing_config,
1991 &dram_timing, index);
1992 gen_rk3399_phy_params(&rk3399_dram_status.timing_config,
1993 &rk3399_dram_status.drv_odt_lp_cfg,
1994 &dram_timing, index);
1995 rk3399_dram_status.index_freq[index] = mhz;
1996
Caesar Wanga8456902016-10-27 01:12:34 +08001997 return index;
1998}
1999
2000void print_dram_status_info(void)
2001{
2002 uint32_t *p;
2003 uint32_t i;
2004
2005 p = (uint32_t *) &rk3399_dram_status.timing_config;
2006 INFO("rk3399_dram_status.timing_config:\n");
2007 for (i = 0; i < sizeof(struct timing_related_config) / 4; i++)
2008 tf_printf("%u\n", p[i]);
2009 p = (uint32_t *) &rk3399_dram_status.drv_odt_lp_cfg;
2010 INFO("rk3399_dram_status.drv_odt_lp_cfg:\n");
2011 for (i = 0; i < sizeof(struct drv_odt_lp_config) / 4; i++)
2012 tf_printf("%u\n", p[i]);
2013}
2014
2015uint32_t ddr_set_rate(uint32_t hz)
2016{
Xing Zheng93280b72016-10-26 21:25:26 +08002017 uint32_t low_power, index, ddr_index;
Caesar Wanga8456902016-10-27 01:12:34 +08002018 uint32_t mhz = hz / (1000 * 1000);
2019
2020 if (mhz ==
2021 rk3399_dram_status.index_freq[rk3399_dram_status.current_index])
2022 goto out;
2023
2024 index = to_get_clk_index(mhz);
2025 mhz = dpll_rates_table[index].mhz;
2026
Xing Zheng93280b72016-10-26 21:25:26 +08002027 ddr_index = prepare_ddr_timing(mhz);
Derek Basehoree13bc542017-02-24 14:31:36 +08002028 gen_rk3399_enable_training(rk3399_dram_status.timing_config.ch_cnt,
2029 mhz);
Xing Zheng93280b72016-10-26 21:25:26 +08002030 if (ddr_index > 1)
Caesar Wanga8456902016-10-27 01:12:34 +08002031 goto out;
2032
Lin Huangb4a76762016-12-12 15:18:08 +08002033 /*
2034 * Make sure the clock is enabled. The M0 clocks should be on all of the
2035 * time during S0.
2036 */
Xing Zheng93280b72016-10-26 21:25:26 +08002037 m0_configure_ddr(dpll_rates_table[index], ddr_index);
2038 m0_start();
2039 m0_wait_done();
2040 m0_stop();
2041
Caesar Wanga8456902016-10-27 01:12:34 +08002042 if (rk3399_dram_status.timing_config.odt == 0)
2043 gen_rk3399_set_odt(0);
2044
Xing Zheng93280b72016-10-26 21:25:26 +08002045 rk3399_dram_status.current_index = ddr_index;
Derek Basehoreff461d02016-10-20 20:46:43 -07002046 low_power = rk3399_dram_status.low_power_stat;
Caesar Wanga8456902016-10-27 01:12:34 +08002047 resume_low_power(low_power);
2048out:
2049 return mhz;
2050}
2051
2052uint32_t ddr_round_rate(uint32_t hz)
2053{
2054 int index;
2055 uint32_t mhz = hz / (1000 * 1000);
2056
2057 index = to_get_clk_index(mhz);
2058
2059 return dpll_rates_table[index].mhz * 1000 * 1000;
2060}
Derek Basehoree13bc542017-02-24 14:31:36 +08002061
2062void ddr_prepare_for_sys_suspend(void)
2063{
2064 uint32_t mhz =
2065 rk3399_dram_status.index_freq[rk3399_dram_status.current_index];
2066
2067 /*
2068 * If we're not currently at the boot (assumed highest) frequency, we
2069 * need to change frequencies to configure out current index.
2070 */
2071 rk3399_suspend_status.freq = mhz;
2072 exit_low_power();
2073 rk3399_suspend_status.low_power_stat =
2074 rk3399_dram_status.low_power_stat;
2075 rk3399_suspend_status.odt = rk3399_dram_status.timing_config.odt;
2076 rk3399_dram_status.low_power_stat = 0;
2077 rk3399_dram_status.timing_config.odt = 1;
2078 if (mhz != rk3399_dram_status.boot_freq)
2079 ddr_set_rate(rk3399_dram_status.boot_freq * 1000 * 1000);
2080
2081 /*
2082 * This will configure the other index to be the same frequency as the
2083 * current one. We retrain both indices on resume, so both have to be
2084 * setup for the same frequency.
2085 */
2086 prepare_ddr_timing(rk3399_dram_status.boot_freq);
2087}
2088
2089void ddr_prepare_for_sys_resume(void)
2090{
2091 /* Disable multicast */
2092 mmio_clrbits_32(PHY_REG(0, 896), 1);
2093 mmio_clrbits_32(PHY_REG(1, 896), 1);
2094
2095 /* The suspend code changes the current index, so reset it now. */
2096 rk3399_dram_status.current_index =
2097 (mmio_read_32(CTL_REG(0, 111)) >> 16) & 0x3;
2098 rk3399_dram_status.low_power_stat =
2099 rk3399_suspend_status.low_power_stat;
2100 rk3399_dram_status.timing_config.odt = rk3399_suspend_status.odt;
2101
2102 /*
2103 * Set the saved frequency from suspend if it's different than the
2104 * current frequency.
2105 */
2106 if (rk3399_suspend_status.freq !=
2107 rk3399_dram_status.index_freq[rk3399_dram_status.current_index]) {
2108 ddr_set_rate(rk3399_suspend_status.freq * 1000 * 1000);
2109 return;
2110 }
2111
2112 gen_rk3399_set_odt(rk3399_dram_status.timing_config.odt);
2113 resume_low_power(rk3399_dram_status.low_power_stat);
2114}