blob: 067a6751c087192d723e88df1bccd7e63dc02d59 [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);
1578 mmio_setbits_32(PHY_REG(i, 927), (1 << 22));
Caesar Wanga8456902016-10-27 01:12:34 +08001579
1580 if (timing_config->dram_type == DDR3) {
1581 mem_delay_ps = 0;
1582 trpre_min_ps = 1000;
1583 } else if (timing_config->dram_type == LPDDR4) {
1584 mem_delay_ps = 1500;
1585 trpre_min_ps = 900;
1586 } else if (timing_config->dram_type == LPDDR3) {
1587 mem_delay_ps = 2500;
1588 trpre_min_ps = 900;
1589 } else {
1590 ERROR("gen_rk3399_phy_params:dramtype unsupport\n");
1591 return;
1592 }
1593 total_delay_ps = mem_delay_ps + pad_delay_ps;
Caesar Wang8bc16672016-10-27 01:12:47 +08001594 delay_frac_ps = 1000 * total_delay_ps /
1595 (1000000 / pdram_timing->mhz);
Caesar Wanga8456902016-10-27 01:12:34 +08001596 gate_delay_ps = delay_frac_ps + 1000 - (trpre_min_ps / 2);
Caesar Wang8bc16672016-10-27 01:12:47 +08001597 gate_delay_frac_ps = gate_delay_ps % 1000;
Caesar Wanga8456902016-10-27 01:12:34 +08001598 tmp = gate_delay_frac_ps * 0x200 / 1000;
Caesar Wanga8456902016-10-27 01:12:34 +08001599 /* PHY_RDDQS_GATE_SLAVE_DELAY */
1600 /* DENALI_PHY_77/205/333/461 10bits offset_16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001601 mmio_clrsetbits_32(PHY_REG(i, 77), 0x2ff << 16, tmp << 16);
1602 mmio_clrsetbits_32(PHY_REG(i, 205), 0x2ff << 16, tmp << 16);
1603 mmio_clrsetbits_32(PHY_REG(i, 333), 0x2ff << 16, tmp << 16);
1604 mmio_clrsetbits_32(PHY_REG(i, 461), 0x2ff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001605
1606 tmp = gate_delay_ps / 1000;
1607 /* PHY_LP4_BOOT_RDDQS_LATENCY_ADJUST */
1608 /* DENALI_PHY_10/138/266/394 4bit offset_0 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001609 mmio_clrsetbits_32(PHY_REG(i, 10), 0xf, tmp);
1610 mmio_clrsetbits_32(PHY_REG(i, 138), 0xf, tmp);
1611 mmio_clrsetbits_32(PHY_REG(i, 266), 0xf, tmp);
1612 mmio_clrsetbits_32(PHY_REG(i, 394), 0xf, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001613 /* PHY_GTLVL_LAT_ADJ_START */
1614 /* DENALI_PHY_80/208/336/464 4bits offset_16 */
1615 tmp = delay_frac_ps / 1000;
Caesar Wang8bc16672016-10-27 01:12:47 +08001616 mmio_clrsetbits_32(PHY_REG(i, 80), 0xf << 16, tmp << 16);
1617 mmio_clrsetbits_32(PHY_REG(i, 208), 0xf << 16, tmp << 16);
1618 mmio_clrsetbits_32(PHY_REG(i, 336), 0xf << 16, tmp << 16);
1619 mmio_clrsetbits_32(PHY_REG(i, 464), 0xf << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001620
1621 cas_lat = pdram_timing->cl + PI_ADD_LATENCY;
1622 rddata_en_ie_dly = ie_enable / (1000000 / pdram_timing->mhz);
1623 if ((ie_enable % (1000000 / pdram_timing->mhz)) != 0)
1624 rddata_en_ie_dly++;
1625 rddata_en_ie_dly = rddata_en_ie_dly - 1;
1626 tsel_adder = tsel_enable / (1000000 / pdram_timing->mhz);
1627 if ((tsel_enable % (1000000 / pdram_timing->mhz)) != 0)
1628 tsel_adder++;
1629 if (rddata_en_ie_dly > tsel_adder)
1630 extra_adder = rddata_en_ie_dly - tsel_adder;
1631 else
1632 extra_adder = 0;
1633 delta = cas_lat - rddata_en_ie_dly;
1634 if (PI_REGS_DIMM_SUPPORT && PI_DOUBLEFREEK)
1635 hs_offset = 2;
1636 else
1637 hs_offset = 1;
Caesar Wang8bc16672016-10-27 01:12:47 +08001638 if (rddata_en_ie_dly > (cas_lat - 1 - hs_offset))
Caesar Wanga8456902016-10-27 01:12:34 +08001639 tmp = 0;
Caesar Wang8bc16672016-10-27 01:12:47 +08001640 else if ((delta == 2) || (delta == 1))
1641 tmp = rddata_en_ie_dly - 0 - extra_adder;
1642 else
1643 tmp = extra_adder;
Caesar Wanga8456902016-10-27 01:12:34 +08001644 /* PHY_LP4_BOOT_RDDATA_EN_TSEL_DLY */
1645 /* DENALI_PHY_9/137/265/393 4bit offset_16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001646 mmio_clrsetbits_32(PHY_REG(i, 9), 0xf << 16, tmp << 16);
1647 mmio_clrsetbits_32(PHY_REG(i, 137), 0xf << 16, tmp << 16);
1648 mmio_clrsetbits_32(PHY_REG(i, 265), 0xf << 16, tmp << 16);
1649 mmio_clrsetbits_32(PHY_REG(i, 393), 0xf << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001650 /* PHY_RDDATA_EN_TSEL_DLY */
1651 /* DENALI_PHY_86/214/342/470 4bit offset_0 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001652 mmio_clrsetbits_32(PHY_REG(i, 86), 0xf, tmp);
1653 mmio_clrsetbits_32(PHY_REG(i, 214), 0xf, tmp);
1654 mmio_clrsetbits_32(PHY_REG(i, 342), 0xf, tmp);
1655 mmio_clrsetbits_32(PHY_REG(i, 470), 0xf, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001656
1657 if (tsel_adder > rddata_en_ie_dly)
1658 extra_adder = tsel_adder - rddata_en_ie_dly;
1659 else
1660 extra_adder = 0;
1661 if (rddata_en_ie_dly > (cas_lat - 1 - hs_offset))
1662 tmp = tsel_adder;
1663 else
1664 tmp = rddata_en_ie_dly - 0 + extra_adder;
1665 /* PHY_LP4_BOOT_RDDATA_EN_DLY */
1666 /* DENALI_PHY_9/137/265/393 4bit offset_8 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001667 mmio_clrsetbits_32(PHY_REG(i, 9), 0xf << 8, tmp << 8);
1668 mmio_clrsetbits_32(PHY_REG(i, 137), 0xf << 8, tmp << 8);
1669 mmio_clrsetbits_32(PHY_REG(i, 265), 0xf << 8, tmp << 8);
1670 mmio_clrsetbits_32(PHY_REG(i, 393), 0xf << 8, tmp << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001671 /* PHY_RDDATA_EN_DLY */
1672 /* DENALI_PHY_85/213/341/469 4bit offset_24 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001673 mmio_clrsetbits_32(PHY_REG(i, 85), 0xf << 24, tmp << 24);
1674 mmio_clrsetbits_32(PHY_REG(i, 213), 0xf << 24, tmp << 24);
1675 mmio_clrsetbits_32(PHY_REG(i, 341), 0xf << 24, tmp << 24);
1676 mmio_clrsetbits_32(PHY_REG(i, 469), 0xf << 24, tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001677
1678 if (pdram_timing->mhz <= ENPER_CS_TRAINING_FREQ) {
Caesar Wanga8456902016-10-27 01:12:34 +08001679 /*
1680 * Note:Per-CS Training is not compatible at speeds
1681 * under 533 MHz. If the PHY is running at a speed
1682 * less than 533MHz, all phy_per_cs_training_en_X
1683 * parameters must be cleared to 0.
1684 */
1685
1686 /*DENALI_PHY_84/212/340/468 1bit offset_16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001687 mmio_clrbits_32(PHY_REG(i, 84), 0x1 << 16);
1688 mmio_clrbits_32(PHY_REG(i, 212), 0x1 << 16);
1689 mmio_clrbits_32(PHY_REG(i, 340), 0x1 << 16);
1690 mmio_clrbits_32(PHY_REG(i, 468), 0x1 << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001691 } else {
Caesar Wang8bc16672016-10-27 01:12:47 +08001692 mmio_setbits_32(PHY_REG(i, 84), 0x1 << 16);
1693 mmio_setbits_32(PHY_REG(i, 212), 0x1 << 16);
1694 mmio_setbits_32(PHY_REG(i, 340), 0x1 << 16);
1695 mmio_setbits_32(PHY_REG(i, 468), 0x1 << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001696 }
Derek Basehoreb1065122016-10-20 22:09:22 -07001697 gen_rk3399_phy_dll_bypass(pdram_timing->mhz, i, fn,
1698 timing_config->dram_type);
Caesar Wanga8456902016-10-27 01:12:34 +08001699 }
1700}
1701
1702static int to_get_clk_index(unsigned int mhz)
1703{
1704 int pll_cnt, i;
1705
1706 pll_cnt = ARRAY_SIZE(dpll_rates_table);
1707
1708 /* Assumming rate_table is in descending order */
1709 for (i = 0; i < pll_cnt; i++) {
1710 if (mhz >= dpll_rates_table[i].mhz)
1711 break;
1712 }
1713
1714 /* if mhz lower than lowest frequency in table, use lowest frequency */
1715 if (i == pll_cnt)
1716 i = pll_cnt - 1;
1717
1718 return i;
1719}
1720
Caesar Wanga8456902016-10-27 01:12:34 +08001721uint32_t ddr_get_rate(void)
1722{
1723 uint32_t refdiv, postdiv1, fbdiv, postdiv2;
1724
1725 refdiv = mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 1)) & 0x3f;
1726 fbdiv = mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 0)) & 0xfff;
1727 postdiv1 =
1728 (mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 1)) >> 8) & 0x7;
1729 postdiv2 =
1730 (mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 1)) >> 12) & 0x7;
1731
1732 return (24 / refdiv * fbdiv / postdiv1 / postdiv2) * 1000 * 1000;
1733}
1734
1735/*
1736 * return: bit12: channel 1, external self-refresh
1737 * bit11: channel 1, stdby_mode
1738 * bit10: channel 1, self-refresh with controller and memory clock gate
1739 * bit9: channel 1, self-refresh
1740 * bit8: channel 1, power-down
1741 *
1742 * bit4: channel 1, external self-refresh
1743 * bit3: channel 0, stdby_mode
1744 * bit2: channel 0, self-refresh with controller and memory clock gate
1745 * bit1: channel 0, self-refresh
1746 * bit0: channel 0, power-down
1747 */
1748uint32_t exit_low_power(void)
1749{
Caesar Wanga8456902016-10-27 01:12:34 +08001750 uint32_t low_power = 0;
1751 uint32_t channel_mask;
Caesar Wang8bc16672016-10-27 01:12:47 +08001752 uint32_t tmp, i;
Caesar Wanga8456902016-10-27 01:12:34 +08001753
Caesar Wang8bc16672016-10-27 01:12:47 +08001754 channel_mask = (mmio_read_32(PMUGRF_BASE + PMUGRF_OSREG(2)) >> 28) &
1755 0x3;
1756 for (i = 0; i < 2; i++) {
1757 if (!(channel_mask & (1 << i)))
Caesar Wanga8456902016-10-27 01:12:34 +08001758 continue;
1759
1760 /* exit stdby mode */
Caesar Wang8bc16672016-10-27 01:12:47 +08001761 mmio_write_32(CIC_BASE + CIC_CTRL1,
1762 (1 << (i + 16)) | (0 << i));
Caesar Wanga8456902016-10-27 01:12:34 +08001763 /* exit external self-refresh */
Caesar Wang8bc16672016-10-27 01:12:47 +08001764 tmp = i ? 12 : 8;
1765 low_power |= ((mmio_read_32(PMU_BASE + PMU_SFT_CON) >> tmp) &
1766 0x1) << (4 + 8 * i);
1767 mmio_clrbits_32(PMU_BASE + PMU_SFT_CON, 1 << tmp);
1768 while (!(mmio_read_32(PMU_BASE + PMU_DDR_SREF_ST) & (1 << i)))
Caesar Wanga8456902016-10-27 01:12:34 +08001769 ;
1770 /* exit auto low-power */
Caesar Wang8bc16672016-10-27 01:12:47 +08001771 mmio_clrbits_32(CTL_REG(i, 101), 0x7);
Caesar Wanga8456902016-10-27 01:12:34 +08001772 /* lp_cmd to exit */
Caesar Wang8bc16672016-10-27 01:12:47 +08001773 if (((mmio_read_32(CTL_REG(i, 100)) >> 24) & 0x7f) !=
1774 0x40) {
1775 while (mmio_read_32(CTL_REG(i, 200)) & 0x1)
Caesar Wanga8456902016-10-27 01:12:34 +08001776 ;
Caesar Wang8bc16672016-10-27 01:12:47 +08001777 mmio_clrsetbits_32(CTL_REG(i, 93), 0xff << 24,
1778 0x69 << 24);
1779 while (((mmio_read_32(CTL_REG(i, 100)) >> 24) & 0x7f) !=
1780 0x40)
Caesar Wanga8456902016-10-27 01:12:34 +08001781 ;
1782 }
1783 }
1784 return low_power;
1785}
1786
1787void resume_low_power(uint32_t low_power)
1788{
Caesar Wanga8456902016-10-27 01:12:34 +08001789 uint32_t channel_mask;
Caesar Wang8bc16672016-10-27 01:12:47 +08001790 uint32_t tmp, i, val;
Caesar Wanga8456902016-10-27 01:12:34 +08001791
Caesar Wang8bc16672016-10-27 01:12:47 +08001792 channel_mask = (mmio_read_32(PMUGRF_BASE + PMUGRF_OSREG(2)) >> 28) &
1793 0x3;
1794 for (i = 0; i < 2; i++) {
1795 if (!(channel_mask & (1 << i)))
Caesar Wanga8456902016-10-27 01:12:34 +08001796 continue;
1797
1798 /* resume external self-refresh */
Caesar Wang8bc16672016-10-27 01:12:47 +08001799 tmp = i ? 12 : 8;
1800 val = (low_power >> (4 + 8 * i)) & 0x1;
1801 mmio_setbits_32(PMU_BASE + PMU_SFT_CON, val << tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001802 /* resume auto low-power */
Caesar Wang8bc16672016-10-27 01:12:47 +08001803 val = (low_power >> (8 * i)) & 0x7;
1804 mmio_setbits_32(CTL_REG(i, 101), val);
Caesar Wanga8456902016-10-27 01:12:34 +08001805 /* resume stdby mode */
Caesar Wang8bc16672016-10-27 01:12:47 +08001806 val = (low_power >> (3 + 8 * i)) & 0x1;
1807 mmio_write_32(CIC_BASE + CIC_CTRL1,
1808 (1 << (i + 16)) | (val << i));
Caesar Wanga8456902016-10-27 01:12:34 +08001809 }
1810}
1811
Derek Basehoreff461d02016-10-20 20:46:43 -07001812static void dram_low_power_config(void)
Caesar Wanga8456902016-10-27 01:12:34 +08001813{
Derek Basehoreff461d02016-10-20 20:46:43 -07001814 uint32_t tmp, i;
Caesar Wanga8456902016-10-27 01:12:34 +08001815 uint32_t ch_cnt = rk3399_dram_status.timing_config.ch_cnt;
1816 uint32_t dram_type = rk3399_dram_status.timing_config.dram_type;
Caesar Wanga8456902016-10-27 01:12:34 +08001817
1818 if (dram_type == DDR3)
Derek Basehoreff461d02016-10-20 20:46:43 -07001819 tmp = (2 << 16) | (0x7 << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001820 else
Derek Basehoreff461d02016-10-20 20:46:43 -07001821 tmp = (3 << 16) | (0x7 << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001822
Derek Basehoreff461d02016-10-20 20:46:43 -07001823 for (i = 0; i < ch_cnt; i++)
1824 mmio_clrsetbits_32(CTL_REG(i, 101), 0x70f0f, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001825
1826 /* standby idle */
Caesar Wang8bc16672016-10-27 01:12:47 +08001827 mmio_write_32(CIC_BASE + CIC_CG_WAIT_TH, 0x640008);
Caesar Wanga8456902016-10-27 01:12:34 +08001828
1829 if (ch_cnt == 2) {
Caesar Wang8bc16672016-10-27 01:12:47 +08001830 mmio_write_32(GRF_BASE + GRF_DDRC1_CON1,
1831 (((0x1<<4) | (0x1<<5) | (0x1<<6) |
1832 (0x1<<7)) << 16) |
1833 ((0x1<<4) | (0x0<<5) | (0x1<<6) | (0x1<<7)));
Derek Basehoreff461d02016-10-20 20:46:43 -07001834 mmio_write_32(CIC_BASE + CIC_CTRL1, 0x002a0028);
Caesar Wanga8456902016-10-27 01:12:34 +08001835 }
1836
Caesar Wang8bc16672016-10-27 01:12:47 +08001837 mmio_write_32(GRF_BASE + GRF_DDRC0_CON1,
1838 (((0x1<<4) | (0x1<<5) | (0x1<<6) | (0x1<<7)) << 16) |
1839 ((0x1<<4) | (0x0<<5) | (0x1<<6) | (0x1<<7)));
Derek Basehoreff461d02016-10-20 20:46:43 -07001840 mmio_write_32(CIC_BASE + CIC_CTRL1, 0x00150014);
Caesar Wanga8456902016-10-27 01:12:34 +08001841}
1842
Derek Basehoreff461d02016-10-20 20:46:43 -07001843void dram_dfs_init(void)
Caesar Wanga8456902016-10-27 01:12:34 +08001844{
Derek Basehoree13bc542017-02-24 14:31:36 +08001845 uint32_t trefi0, trefi1, boot_freq;
Caesar Wanga8456902016-10-27 01:12:34 +08001846
1847 /* get sdram config for os reg */
Derek Basehoreff461d02016-10-20 20:46:43 -07001848 get_dram_drv_odt_val(sdram_config.dramtype,
1849 &rk3399_dram_status.drv_odt_lp_cfg);
Caesar Wanga8456902016-10-27 01:12:34 +08001850 sdram_timing_cfg_init(&rk3399_dram_status.timing_config,
1851 &sdram_config,
1852 &rk3399_dram_status.drv_odt_lp_cfg);
1853
Caesar Wang8bc16672016-10-27 01:12:47 +08001854 trefi0 = ((mmio_read_32(CTL_REG(0, 48)) >> 16) & 0xffff) + 8;
1855 trefi1 = ((mmio_read_32(CTL_REG(0, 49)) >> 16) & 0xffff) + 8;
Caesar Wanga8456902016-10-27 01:12:34 +08001856
1857 rk3399_dram_status.index_freq[0] = trefi0 * 10 / 39;
1858 rk3399_dram_status.index_freq[1] = trefi1 * 10 / 39;
1859 rk3399_dram_status.current_index =
Caesar Wang8bc16672016-10-27 01:12:47 +08001860 (mmio_read_32(CTL_REG(0, 111)) >> 16) & 0x3;
Caesar Wanga8456902016-10-27 01:12:34 +08001861 if (rk3399_dram_status.timing_config.dram_type == DDR3) {
1862 rk3399_dram_status.index_freq[0] /= 2;
1863 rk3399_dram_status.index_freq[1] /= 2;
1864 }
Derek Basehoree13bc542017-02-24 14:31:36 +08001865 boot_freq =
1866 rk3399_dram_status.index_freq[rk3399_dram_status.current_index];
1867 boot_freq = dpll_rates_table[to_get_clk_index(boot_freq)].mhz;
1868 rk3399_dram_status.boot_freq = boot_freq;
1869 rk3399_dram_status.index_freq[rk3399_dram_status.current_index] =
1870 boot_freq;
1871 rk3399_dram_status.index_freq[(rk3399_dram_status.current_index + 1) &
1872 0x1] = 0;
1873 rk3399_dram_status.low_power_stat = 0;
Xing Zheng93280b72016-10-26 21:25:26 +08001874 /*
1875 * following register decide if NOC stall the access request
1876 * or return error when NOC being idled. when doing ddr frequency
1877 * scaling in M0 or DCF, we need to make sure noc stall the access
1878 * request, if return error cpu may data abort when ddr frequency
1879 * changing. it don't need to set this register every times,
1880 * so we init this register in function dram_dfs_init().
1881 */
1882 mmio_write_32(GRF_BASE + GRF_SOC_CON(0), 0xffffffff);
1883 mmio_write_32(GRF_BASE + GRF_SOC_CON(1), 0xffffffff);
1884 mmio_write_32(GRF_BASE + GRF_SOC_CON(2), 0xffffffff);
1885 mmio_write_32(GRF_BASE + GRF_SOC_CON(3), 0xffffffff);
1886 mmio_write_32(GRF_BASE + GRF_SOC_CON(4), 0x70007000);
1887
Derek Basehoree13bc542017-02-24 14:31:36 +08001888 /* Disable multicast */
1889 mmio_clrbits_32(PHY_REG(0, 896), 1);
1890 mmio_clrbits_32(PHY_REG(1, 896), 1);
1891
Derek Basehoreff461d02016-10-20 20:46:43 -07001892 dram_low_power_config();
1893}
Caesar Wanga8456902016-10-27 01:12:34 +08001894
Derek Basehoreff461d02016-10-20 20:46:43 -07001895/*
1896 * arg0: bit0-7: sr_idle; bit8-15:sr_mc_gate_idle; bit16-31: standby idle
1897 * arg1: bit0-11: pd_idle; bit 16-27: srpd_lite_idle
1898 * arg2: bit0: if odt en
1899 */
1900uint32_t dram_set_odt_pd(uint32_t arg0, uint32_t arg1, uint32_t arg2)
1901{
1902 struct drv_odt_lp_config *lp_cfg = &rk3399_dram_status.drv_odt_lp_cfg;
1903 uint32_t *low_power = &rk3399_dram_status.low_power_stat;
1904 uint32_t dram_type, ch_count, pd_tmp, sr_tmp, i;
1905
1906 dram_type = rk3399_dram_status.timing_config.dram_type;
1907 ch_count = rk3399_dram_status.timing_config.ch_cnt;
1908
1909 lp_cfg->sr_idle = arg0 & 0xff;
1910 lp_cfg->sr_mc_gate_idle = (arg0 >> 8) & 0xff;
1911 lp_cfg->standby_idle = (arg0 >> 16) & 0xffff;
1912 lp_cfg->pd_idle = arg1 & 0xfff;
1913 lp_cfg->srpd_lite_idle = (arg1 >> 16) & 0xfff;
1914
1915 rk3399_dram_status.timing_config.odt = arg2 & 0x1;
1916
1917 exit_low_power();
1918
1919 *low_power = 0;
Caesar Wanga8456902016-10-27 01:12:34 +08001920
Derek Basehoreff461d02016-10-20 20:46:43 -07001921 /* pd_idle en */
1922 if (lp_cfg->pd_idle)
1923 *low_power |= ((1 << 0) | (1 << 8));
1924 /* sr_idle en srpd_lite_idle */
1925 if (lp_cfg->sr_idle | lp_cfg->srpd_lite_idle)
1926 *low_power |= ((1 << 1) | (1 << 9));
1927 /* sr_mc_gate_idle */
1928 if (lp_cfg->sr_mc_gate_idle)
1929 *low_power |= ((1 << 2) | (1 << 10));
1930 /* standbyidle */
1931 if (lp_cfg->standby_idle) {
1932 if (rk3399_dram_status.timing_config.ch_cnt == 2)
1933 *low_power |= ((1 << 3) | (1 << 11));
1934 else
1935 *low_power |= (1 << 3);
Caesar Wanga8456902016-10-27 01:12:34 +08001936 }
1937
Derek Basehoreff461d02016-10-20 20:46:43 -07001938 pd_tmp = arg1;
1939 if (dram_type != LPDDR4)
1940 pd_tmp = arg1 & 0xfff;
1941 sr_tmp = arg0 & 0xffff;
1942 for (i = 0; i < ch_count; i++) {
1943 mmio_write_32(CTL_REG(i, 102), pd_tmp);
1944 mmio_clrsetbits_32(CTL_REG(i, 103), 0xffff, sr_tmp);
1945 }
1946 mmio_write_32(CIC_BASE + CIC_IDLE_TH, (arg0 >> 16) & 0xffff);
1947
1948 return 0;
Caesar Wanga8456902016-10-27 01:12:34 +08001949}
1950
Xing Zheng93280b72016-10-26 21:25:26 +08001951static void m0_configure_ddr(struct pll_div pll_div, uint32_t ddr_index)
1952{
1953 /* set PARAM to M0_FUNC_DRAM */
1954 mmio_write_32(M0_PARAM_ADDR + PARAM_M0_FUNC, M0_FUNC_DRAM);
1955
1956 mmio_write_32(M0_PARAM_ADDR + PARAM_DPLL_CON0, FBDIV(pll_div.fbdiv));
1957 mmio_write_32(M0_PARAM_ADDR + PARAM_DPLL_CON1,
1958 POSTDIV2(pll_div.postdiv2) | POSTDIV1(pll_div.postdiv1) |
1959 REFDIV(pll_div.refdiv));
1960
1961 mmio_write_32(M0_PARAM_ADDR + PARAM_DRAM_FREQ, pll_div.mhz);
1962
1963 mmio_write_32(M0_PARAM_ADDR + PARAM_FREQ_SELECT, ddr_index << 4);
Lin Huangb4a76762016-12-12 15:18:08 +08001964 dmbst();
Xing Zheng93280b72016-10-26 21:25:26 +08001965}
1966
Caesar Wanga8456902016-10-27 01:12:34 +08001967static uint32_t prepare_ddr_timing(uint32_t mhz)
1968{
1969 uint32_t index;
1970 struct dram_timing_t dram_timing;
1971
1972 rk3399_dram_status.timing_config.freq = mhz;
1973
Derek Basehoreff461d02016-10-20 20:46:43 -07001974 if (mhz < 300)
Caesar Wanga8456902016-10-27 01:12:34 +08001975 rk3399_dram_status.timing_config.dllbp = 1;
1976 else
1977 rk3399_dram_status.timing_config.dllbp = 0;
Derek Basehoreff461d02016-10-20 20:46:43 -07001978
1979 if (rk3399_dram_status.timing_config.odt == 1)
Caesar Wanga8456902016-10-27 01:12:34 +08001980 gen_rk3399_set_odt(1);
Caesar Wanga8456902016-10-27 01:12:34 +08001981
1982 index = (rk3399_dram_status.current_index + 1) & 0x1;
Caesar Wanga8456902016-10-27 01:12:34 +08001983
1984 /*
1985 * checking if having available gate traiing timing for
1986 * target freq.
1987 */
1988 dram_get_parameter(&rk3399_dram_status.timing_config, &dram_timing);
1989 gen_rk3399_ctl_params(&rk3399_dram_status.timing_config,
1990 &dram_timing, index);
1991 gen_rk3399_pi_params(&rk3399_dram_status.timing_config,
1992 &dram_timing, index);
1993 gen_rk3399_phy_params(&rk3399_dram_status.timing_config,
1994 &rk3399_dram_status.drv_odt_lp_cfg,
1995 &dram_timing, index);
1996 rk3399_dram_status.index_freq[index] = mhz;
1997
Caesar Wanga8456902016-10-27 01:12:34 +08001998 return index;
1999}
2000
2001void print_dram_status_info(void)
2002{
2003 uint32_t *p;
2004 uint32_t i;
2005
2006 p = (uint32_t *) &rk3399_dram_status.timing_config;
2007 INFO("rk3399_dram_status.timing_config:\n");
2008 for (i = 0; i < sizeof(struct timing_related_config) / 4; i++)
2009 tf_printf("%u\n", p[i]);
2010 p = (uint32_t *) &rk3399_dram_status.drv_odt_lp_cfg;
2011 INFO("rk3399_dram_status.drv_odt_lp_cfg:\n");
2012 for (i = 0; i < sizeof(struct drv_odt_lp_config) / 4; i++)
2013 tf_printf("%u\n", p[i]);
2014}
2015
2016uint32_t ddr_set_rate(uint32_t hz)
2017{
Xing Zheng93280b72016-10-26 21:25:26 +08002018 uint32_t low_power, index, ddr_index;
Caesar Wanga8456902016-10-27 01:12:34 +08002019 uint32_t mhz = hz / (1000 * 1000);
2020
2021 if (mhz ==
2022 rk3399_dram_status.index_freq[rk3399_dram_status.current_index])
2023 goto out;
2024
2025 index = to_get_clk_index(mhz);
2026 mhz = dpll_rates_table[index].mhz;
2027
Xing Zheng93280b72016-10-26 21:25:26 +08002028 ddr_index = prepare_ddr_timing(mhz);
Derek Basehoree13bc542017-02-24 14:31:36 +08002029 gen_rk3399_enable_training(rk3399_dram_status.timing_config.ch_cnt,
2030 mhz);
Xing Zheng93280b72016-10-26 21:25:26 +08002031 if (ddr_index > 1)
Caesar Wanga8456902016-10-27 01:12:34 +08002032 goto out;
2033
Lin Huangb4a76762016-12-12 15:18:08 +08002034 /*
2035 * Make sure the clock is enabled. The M0 clocks should be on all of the
2036 * time during S0.
2037 */
Xing Zheng93280b72016-10-26 21:25:26 +08002038 m0_configure_ddr(dpll_rates_table[index], ddr_index);
2039 m0_start();
2040 m0_wait_done();
2041 m0_stop();
2042
Caesar Wanga8456902016-10-27 01:12:34 +08002043 if (rk3399_dram_status.timing_config.odt == 0)
2044 gen_rk3399_set_odt(0);
2045
Xing Zheng93280b72016-10-26 21:25:26 +08002046 rk3399_dram_status.current_index = ddr_index;
Derek Basehoreff461d02016-10-20 20:46:43 -07002047 low_power = rk3399_dram_status.low_power_stat;
Caesar Wanga8456902016-10-27 01:12:34 +08002048 resume_low_power(low_power);
2049out:
2050 return mhz;
2051}
2052
2053uint32_t ddr_round_rate(uint32_t hz)
2054{
2055 int index;
2056 uint32_t mhz = hz / (1000 * 1000);
2057
2058 index = to_get_clk_index(mhz);
2059
2060 return dpll_rates_table[index].mhz * 1000 * 1000;
2061}
Derek Basehoree13bc542017-02-24 14:31:36 +08002062
2063void ddr_prepare_for_sys_suspend(void)
2064{
2065 uint32_t mhz =
2066 rk3399_dram_status.index_freq[rk3399_dram_status.current_index];
2067
2068 /*
2069 * If we're not currently at the boot (assumed highest) frequency, we
2070 * need to change frequencies to configure out current index.
2071 */
2072 rk3399_suspend_status.freq = mhz;
2073 exit_low_power();
2074 rk3399_suspend_status.low_power_stat =
2075 rk3399_dram_status.low_power_stat;
2076 rk3399_suspend_status.odt = rk3399_dram_status.timing_config.odt;
2077 rk3399_dram_status.low_power_stat = 0;
2078 rk3399_dram_status.timing_config.odt = 1;
2079 if (mhz != rk3399_dram_status.boot_freq)
2080 ddr_set_rate(rk3399_dram_status.boot_freq * 1000 * 1000);
2081
2082 /*
2083 * This will configure the other index to be the same frequency as the
2084 * current one. We retrain both indices on resume, so both have to be
2085 * setup for the same frequency.
2086 */
2087 prepare_ddr_timing(rk3399_dram_status.boot_freq);
2088}
2089
2090void ddr_prepare_for_sys_resume(void)
2091{
2092 /* Disable multicast */
2093 mmio_clrbits_32(PHY_REG(0, 896), 1);
2094 mmio_clrbits_32(PHY_REG(1, 896), 1);
2095
2096 /* The suspend code changes the current index, so reset it now. */
2097 rk3399_dram_status.current_index =
2098 (mmio_read_32(CTL_REG(0, 111)) >> 16) & 0x3;
2099 rk3399_dram_status.low_power_stat =
2100 rk3399_suspend_status.low_power_stat;
2101 rk3399_dram_status.timing_config.odt = rk3399_suspend_status.odt;
2102
2103 /*
2104 * Set the saved frequency from suspend if it's different than the
2105 * current frequency.
2106 */
2107 if (rk3399_suspend_status.freq !=
2108 rk3399_dram_status.index_freq[rk3399_dram_status.current_index]) {
2109 ddr_set_rate(rk3399_suspend_status.freq * 1000 * 1000);
2110 return;
2111 }
2112
2113 gen_rk3399_set_odt(rk3399_dram_status.timing_config.odt);
2114 resume_low_power(rk3399_dram_status.low_power_stat);
2115}