blob: 11b0373a79349a3085db62fc5f9a9d30f8b5ac24 [file] [log] [blame]
Caesar Wanga8456902016-10-27 01:12:34 +08001/*
Justin Chadwell7d0e3ba2019-09-17 15:21:50 +01002 * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
Caesar Wanga8456902016-10-27 01:12:34 +08003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Caesar Wanga8456902016-10-27 01:12:34 +08005 */
6
Xing Zheng93280b72016-10-26 21:25:26 +08007#include <arch_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008#include <common/debug.h>
9#include <drivers/delay_timer.h>
10#include <lib/mmio.h>
11
Xing Zheng93280b72016-10-26 21:25:26 +080012#include <m0_ctl.h>
Caesar Wanga8456902016-10-27 01:12:34 +080013#include <plat_private.h>
14#include "dfs.h"
15#include "dram.h"
16#include "dram_spec_timing.h"
Caesar Wanga8456902016-10-27 01:12:34 +080017#include "pmu.h"
Isla Mitchelle3631462017-07-14 10:46:32 +010018#include "soc.h"
19#include "string.h"
Caesar Wanga8456902016-10-27 01:12:34 +080020
Derek Basehore0e8909d2016-11-09 18:28:19 -080021#define ENPER_CS_TRAINING_FREQ (666)
22#define TDFI_LAT_THRESHOLD_FREQ (928)
Derek Basehoreb1065122016-10-20 22:09:22 -070023#define PHY_DLL_BYPASS_FREQ (260)
Caesar Wanga8456902016-10-27 01:12:34 +080024
Caesar Wanga8456902016-10-27 01:12:34 +080025static const struct pll_div dpll_rates_table[] = {
26
27 /* _mhz, _refdiv, _fbdiv, _postdiv1, _postdiv2 */
Xing Zheng93280b72016-10-26 21:25:26 +080028 {.mhz = 928, .refdiv = 1, .fbdiv = 116, .postdiv1 = 3, .postdiv2 = 1},
Caesar Wanga8456902016-10-27 01:12:34 +080029 {.mhz = 800, .refdiv = 1, .fbdiv = 100, .postdiv1 = 3, .postdiv2 = 1},
30 {.mhz = 732, .refdiv = 1, .fbdiv = 61, .postdiv1 = 2, .postdiv2 = 1},
31 {.mhz = 666, .refdiv = 1, .fbdiv = 111, .postdiv1 = 4, .postdiv2 = 1},
32 {.mhz = 600, .refdiv = 1, .fbdiv = 50, .postdiv1 = 2, .postdiv2 = 1},
33 {.mhz = 528, .refdiv = 1, .fbdiv = 66, .postdiv1 = 3, .postdiv2 = 1},
34 {.mhz = 400, .refdiv = 1, .fbdiv = 50, .postdiv1 = 3, .postdiv2 = 1},
35 {.mhz = 300, .refdiv = 1, .fbdiv = 50, .postdiv1 = 4, .postdiv2 = 1},
36 {.mhz = 200, .refdiv = 1, .fbdiv = 50, .postdiv1 = 3, .postdiv2 = 2},
37};
38
Caesar Wanga8456902016-10-27 01:12:34 +080039struct rk3399_dram_status {
40 uint32_t current_index;
41 uint32_t index_freq[2];
Derek Basehoree13bc542017-02-24 14:31:36 +080042 uint32_t boot_freq;
Caesar Wanga8456902016-10-27 01:12:34 +080043 uint32_t low_power_stat;
44 struct timing_related_config timing_config;
45 struct drv_odt_lp_config drv_odt_lp_cfg;
46};
47
Derek Basehoree13bc542017-02-24 14:31:36 +080048struct rk3399_saved_status {
49 uint32_t freq;
50 uint32_t low_power_stat;
51 uint32_t odt;
52};
53
Caesar Wanga8456902016-10-27 01:12:34 +080054static struct rk3399_dram_status rk3399_dram_status;
Derek Basehoree13bc542017-02-24 14:31:36 +080055static struct rk3399_saved_status rk3399_suspend_status;
Derek Basehoreb1065122016-10-20 22:09:22 -070056static uint32_t wrdqs_delay_val[2][2][4];
Lin Huange22d31a2017-02-22 18:24:55 +080057static uint32_t rddqs_delay_ps;
Caesar Wanga8456902016-10-27 01:12:34 +080058
59static struct rk3399_sdram_default_config ddr3_default_config = {
60 .bl = 8,
61 .ap = 0,
Caesar Wanga8456902016-10-27 01:12:34 +080062 .burst_ref_cnt = 1,
63 .zqcsi = 0
64};
65
Caesar Wanga8456902016-10-27 01:12:34 +080066static struct rk3399_sdram_default_config lpddr3_default_config = {
67 .bl = 8,
68 .ap = 0,
Caesar Wanga8456902016-10-27 01:12:34 +080069 .burst_ref_cnt = 1,
70 .zqcsi = 0
71};
72
Caesar Wanga8456902016-10-27 01:12:34 +080073static struct rk3399_sdram_default_config lpddr4_default_config = {
74 .bl = 16,
75 .ap = 0,
Caesar Wanga8456902016-10-27 01:12:34 +080076 .caodt = 240,
77 .burst_ref_cnt = 1,
78 .zqcsi = 0
79};
80
Justin Chadwell7d0e3ba2019-09-17 15:21:50 +010081static uint32_t get_cs_die_capability(struct rk3399_sdram_params *ram_config,
Caesar Wanga8456902016-10-27 01:12:34 +080082 uint8_t channel, uint8_t cs)
83{
Justin Chadwell7d0e3ba2019-09-17 15:21:50 +010084 struct rk3399_sdram_channel *ch = &ram_config->ch[channel];
Caesar Wanga8456902016-10-27 01:12:34 +080085 uint32_t bandwidth;
86 uint32_t die_bandwidth;
87 uint32_t die;
88 uint32_t cs_cap;
89 uint32_t row;
90
91 row = cs == 0 ? ch->cs0_row : ch->cs1_row;
92 bandwidth = 8 * (1 << ch->bw);
93 die_bandwidth = 8 * (1 << ch->dbw);
94 die = bandwidth / die_bandwidth;
95 cs_cap = (1 << (row + ((1 << ch->bk) / 4 + 1) + ch->col +
96 (bandwidth / 16)));
97 if (ch->row_3_4)
98 cs_cap = cs_cap * 3 / 4;
99
100 return (cs_cap / die);
101}
102
Derek Basehoreff461d02016-10-20 20:46:43 -0700103static void get_dram_drv_odt_val(uint32_t dram_type,
Caesar Wanga8456902016-10-27 01:12:34 +0800104 struct drv_odt_lp_config *drv_config)
105{
Derek Basehoreff461d02016-10-20 20:46:43 -0700106 uint32_t tmp;
107 uint32_t mr1_val, mr3_val, mr11_val;
Caesar Wanga8456902016-10-27 01:12:34 +0800108
109 switch (dram_type) {
110 case DDR3:
Derek Basehoreff461d02016-10-20 20:46:43 -0700111 mr1_val = (mmio_read_32(CTL_REG(0, 133)) >> 16) & 0xffff;
112 tmp = ((mr1_val >> 1) & 1) | ((mr1_val >> 4) & 1);
113 if (tmp)
114 drv_config->dram_side_drv = 34;
115 else
116 drv_config->dram_side_drv = 40;
117 tmp = ((mr1_val >> 2) & 1) | ((mr1_val >> 5) & 1) |
118 ((mr1_val >> 7) & 1);
119 if (tmp == 0)
120 drv_config->dram_side_dq_odt = 0;
121 else if (tmp == 1)
122 drv_config->dram_side_dq_odt = 60;
123 else if (tmp == 3)
124 drv_config->dram_side_dq_odt = 40;
125 else
126 drv_config->dram_side_dq_odt = 120;
Caesar Wanga8456902016-10-27 01:12:34 +0800127 break;
128 case LPDDR3:
Derek Basehoreff461d02016-10-20 20:46:43 -0700129 mr3_val = mmio_read_32(CTL_REG(0, 138)) & 0xf;
130 mr11_val = (mmio_read_32(CTL_REG(0, 139)) >> 24) & 0x3;
131 if (mr3_val == 0xb)
132 drv_config->dram_side_drv = 3448;
133 else if (mr3_val == 0xa)
134 drv_config->dram_side_drv = 4048;
135 else if (mr3_val == 0x9)
136 drv_config->dram_side_drv = 3440;
137 else if (mr3_val == 0x4)
138 drv_config->dram_side_drv = 60;
139 else if (mr3_val == 0x3)
140 drv_config->dram_side_drv = 48;
141 else if (mr3_val == 0x2)
142 drv_config->dram_side_drv = 40;
143 else
144 drv_config->dram_side_drv = 34;
Caesar Wanga8456902016-10-27 01:12:34 +0800145
Derek Basehoreff461d02016-10-20 20:46:43 -0700146 if (mr11_val == 1)
147 drv_config->dram_side_dq_odt = 60;
148 else if (mr11_val == 2)
149 drv_config->dram_side_dq_odt = 120;
150 else if (mr11_val == 0)
151 drv_config->dram_side_dq_odt = 0;
152 else
153 drv_config->dram_side_dq_odt = 240;
Caesar Wanga8456902016-10-27 01:12:34 +0800154 break;
155 case LPDDR4:
156 default:
Derek Basehoreff461d02016-10-20 20:46:43 -0700157 mr3_val = (mmio_read_32(CTL_REG(0, 138)) >> 3) & 0x7;
158 mr11_val = (mmio_read_32(CTL_REG(0, 139)) >> 24) & 0xff;
Caesar Wanga8456902016-10-27 01:12:34 +0800159
Derek Basehoreff461d02016-10-20 20:46:43 -0700160 if ((mr3_val == 0) || (mr3_val == 7))
161 drv_config->dram_side_drv = 40;
162 else
163 drv_config->dram_side_drv = 240 / mr3_val;
Caesar Wanga8456902016-10-27 01:12:34 +0800164
Derek Basehoreff461d02016-10-20 20:46:43 -0700165 tmp = mr11_val & 0x7;
166 if ((tmp == 7) || (tmp == 0))
167 drv_config->dram_side_dq_odt = 0;
168 else
169 drv_config->dram_side_dq_odt = 240 / tmp;
Caesar Wanga8456902016-10-27 01:12:34 +0800170
Derek Basehoreff461d02016-10-20 20:46:43 -0700171 tmp = (mr11_val >> 4) & 0x7;
172 if ((tmp == 7) || (tmp == 0))
173 drv_config->dram_side_ca_odt = 0;
174 else
175 drv_config->dram_side_ca_odt = 240 / tmp;
Caesar Wanga8456902016-10-27 01:12:34 +0800176 break;
177 }
178}
179
180static void sdram_timing_cfg_init(struct timing_related_config *ptiming_config,
181 struct rk3399_sdram_params *sdram_params,
182 struct drv_odt_lp_config *drv_config)
183{
184 uint32_t i, j;
185
186 for (i = 0; i < sdram_params->num_channels; i++) {
Derek Basehoreff461d02016-10-20 20:46:43 -0700187 ptiming_config->dram_info[i].speed_rate = DDR3_DEFAULT;
Caesar Wanga8456902016-10-27 01:12:34 +0800188 ptiming_config->dram_info[i].cs_cnt = sdram_params->ch[i].rank;
189 for (j = 0; j < sdram_params->ch[i].rank; j++) {
190 ptiming_config->dram_info[i].per_die_capability[j] =
191 get_cs_die_capability(sdram_params, i, j);
192 }
193 }
194 ptiming_config->dram_type = sdram_params->dramtype;
195 ptiming_config->ch_cnt = sdram_params->num_channels;
196 switch (sdram_params->dramtype) {
197 case DDR3:
198 ptiming_config->bl = ddr3_default_config.bl;
199 ptiming_config->ap = ddr3_default_config.ap;
200 break;
201 case LPDDR3:
202 ptiming_config->bl = lpddr3_default_config.bl;
203 ptiming_config->ap = lpddr3_default_config.ap;
204 break;
205 case LPDDR4:
206 ptiming_config->bl = lpddr4_default_config.bl;
207 ptiming_config->ap = lpddr4_default_config.ap;
208 ptiming_config->rdbi = 0;
209 ptiming_config->wdbi = 0;
210 break;
Jonathan Wrightff957ed2018-03-14 15:24:00 +0000211 default:
212 /* Do nothing in default case */
213 break;
Caesar Wanga8456902016-10-27 01:12:34 +0800214 }
215 ptiming_config->dramds = drv_config->dram_side_drv;
216 ptiming_config->dramodt = drv_config->dram_side_dq_odt;
217 ptiming_config->caodt = drv_config->dram_side_ca_odt;
Derek Basehoree13bc542017-02-24 14:31:36 +0800218 ptiming_config->odt = (mmio_read_32(PHY_REG(0, 5)) >> 16) & 0x1;
Caesar Wanga8456902016-10-27 01:12:34 +0800219}
220
221struct lat_adj_pair {
222 uint32_t cl;
223 uint32_t rdlat_adj;
224 uint32_t cwl;
225 uint32_t wrlat_adj;
226};
227
228const struct lat_adj_pair ddr3_lat_adj[] = {
229 {6, 5, 5, 4},
230 {8, 7, 6, 5},
231 {10, 9, 7, 6},
232 {11, 9, 8, 7},
233 {13, 0xb, 9, 8},
234 {14, 0xb, 0xa, 9}
235};
236
237const struct lat_adj_pair lpddr3_lat_adj[] = {
238 {3, 2, 1, 0},
239 {6, 5, 3, 2},
240 {8, 7, 4, 3},
241 {9, 8, 5, 4},
242 {10, 9, 6, 5},
243 {11, 9, 6, 5},
244 {12, 0xa, 6, 5},
245 {14, 0xc, 8, 7},
246 {16, 0xd, 8, 7}
247};
248
249const struct lat_adj_pair lpddr4_lat_adj[] = {
250 {6, 5, 4, 2},
251 {10, 9, 6, 4},
252 {14, 0xc, 8, 6},
253 {20, 0x11, 0xa, 8},
254 {24, 0x15, 0xc, 0xa},
255 {28, 0x18, 0xe, 0xc},
256 {32, 0x1b, 0x10, 0xe},
257 {36, 0x1e, 0x12, 0x10}
258};
259
260static uint32_t get_rdlat_adj(uint32_t dram_type, uint32_t cl)
261{
262 const struct lat_adj_pair *p;
263 uint32_t cnt;
264 uint32_t i;
265
266 if (dram_type == DDR3) {
267 p = ddr3_lat_adj;
268 cnt = ARRAY_SIZE(ddr3_lat_adj);
269 } else if (dram_type == LPDDR3) {
270 p = lpddr3_lat_adj;
271 cnt = ARRAY_SIZE(lpddr3_lat_adj);
272 } else {
273 p = lpddr4_lat_adj;
274 cnt = ARRAY_SIZE(lpddr4_lat_adj);
275 }
276
277 for (i = 0; i < cnt; i++) {
278 if (cl == p[i].cl)
279 return p[i].rdlat_adj;
280 }
281 /* fail */
282 return 0xff;
283}
284
285static uint32_t get_wrlat_adj(uint32_t dram_type, uint32_t cwl)
286{
287 const struct lat_adj_pair *p;
288 uint32_t cnt;
289 uint32_t i;
290
291 if (dram_type == DDR3) {
292 p = ddr3_lat_adj;
293 cnt = ARRAY_SIZE(ddr3_lat_adj);
294 } else if (dram_type == LPDDR3) {
295 p = lpddr3_lat_adj;
296 cnt = ARRAY_SIZE(lpddr3_lat_adj);
297 } else {
298 p = lpddr4_lat_adj;
299 cnt = ARRAY_SIZE(lpddr4_lat_adj);
300 }
301
302 for (i = 0; i < cnt; i++) {
303 if (cwl == p[i].cwl)
304 return p[i].wrlat_adj;
305 }
306 /* fail */
307 return 0xff;
308}
309
310#define PI_REGS_DIMM_SUPPORT (0)
311#define PI_ADD_LATENCY (0)
312#define PI_DOUBLEFREEK (1)
313
314#define PI_PAD_DELAY_PS_VALUE (1000)
315#define PI_IE_ENABLE_VALUE (3000)
316#define PI_TSEL_ENABLE_VALUE (700)
317
318static uint32_t get_pi_rdlat_adj(struct dram_timing_t *pdram_timing)
319{
320 /*[DLLSUBTYPE2] == "STD_DENALI_HS" */
321 uint32_t rdlat, delay_adder, ie_enable, hs_offset, tsel_adder,
322 extra_adder, tsel_enable;
323
324 ie_enable = PI_IE_ENABLE_VALUE;
325 tsel_enable = PI_TSEL_ENABLE_VALUE;
326
327 rdlat = pdram_timing->cl + PI_ADD_LATENCY;
328 delay_adder = ie_enable / (1000000 / pdram_timing->mhz);
329 if ((ie_enable % (1000000 / pdram_timing->mhz)) != 0)
330 delay_adder++;
331 hs_offset = 0;
332 tsel_adder = 0;
333 extra_adder = 0;
334 /* rdlat = rdlat - (PREAMBLE_SUPPORT & 0x1); */
335 tsel_adder = tsel_enable / (1000000 / pdram_timing->mhz);
336 if ((tsel_enable % (1000000 / pdram_timing->mhz)) != 0)
337 tsel_adder++;
338 delay_adder = delay_adder - 1;
339 if (tsel_adder > delay_adder)
340 extra_adder = tsel_adder - delay_adder;
341 else
342 extra_adder = 0;
343 if (PI_REGS_DIMM_SUPPORT && PI_DOUBLEFREEK)
344 hs_offset = 2;
345 else
346 hs_offset = 1;
347
348 if (delay_adder > (rdlat - 1 - hs_offset)) {
349 rdlat = rdlat - tsel_adder;
350 } else {
351 if ((rdlat - delay_adder) < 2)
352 rdlat = 2;
353 else
354 rdlat = rdlat - delay_adder - extra_adder;
355 }
356
357 return rdlat;
358}
359
360static uint32_t get_pi_wrlat(struct dram_timing_t *pdram_timing,
361 struct timing_related_config *timing_config)
362{
363 uint32_t tmp;
364
365 if (timing_config->dram_type == LPDDR3) {
366 tmp = pdram_timing->cl;
367 if (tmp >= 14)
368 tmp = 8;
369 else if (tmp >= 10)
370 tmp = 6;
371 else if (tmp == 9)
372 tmp = 5;
373 else if (tmp == 8)
374 tmp = 4;
375 else if (tmp == 6)
376 tmp = 3;
377 else
378 tmp = 1;
379 } else {
380 tmp = 1;
381 }
382
383 return tmp;
384}
385
386static uint32_t get_pi_wrlat_adj(struct dram_timing_t *pdram_timing,
387 struct timing_related_config *timing_config)
388{
389 return get_pi_wrlat(pdram_timing, timing_config) + PI_ADD_LATENCY - 1;
390}
391
392static uint32_t get_pi_tdfi_phy_rdlat(struct dram_timing_t *pdram_timing,
393 struct timing_related_config *timing_config)
394{
395 /* [DLLSUBTYPE2] == "STD_DENALI_HS" */
396 uint32_t cas_lat, delay_adder, ie_enable, hs_offset, ie_delay_adder;
397 uint32_t mem_delay_ps, round_trip_ps;
398 uint32_t phy_internal_delay, lpddr_adder, dfi_adder, rdlat_delay;
399
400 ie_enable = PI_IE_ENABLE_VALUE;
401
402 delay_adder = ie_enable / (1000000 / pdram_timing->mhz);
403 if ((ie_enable % (1000000 / pdram_timing->mhz)) != 0)
404 delay_adder++;
405 delay_adder = delay_adder - 1;
406 if (PI_REGS_DIMM_SUPPORT && PI_DOUBLEFREEK)
407 hs_offset = 2;
408 else
409 hs_offset = 1;
410
411 cas_lat = pdram_timing->cl + PI_ADD_LATENCY;
412
413 if (delay_adder > (cas_lat - 1 - hs_offset)) {
414 ie_delay_adder = 0;
415 } else {
416 ie_delay_adder = ie_enable / (1000000 / pdram_timing->mhz);
417 if ((ie_enable % (1000000 / pdram_timing->mhz)) != 0)
418 ie_delay_adder++;
419 }
420
421 if (timing_config->dram_type == DDR3) {
422 mem_delay_ps = 0;
423 } else if (timing_config->dram_type == LPDDR4) {
424 mem_delay_ps = 3600;
425 } else if (timing_config->dram_type == LPDDR3) {
426 mem_delay_ps = 5500;
427 } else {
Caesar Wang509f3792017-04-06 08:40:24 +0800428 NOTICE("get_pi_tdfi_phy_rdlat:dramtype unsupport\n");
Caesar Wanga8456902016-10-27 01:12:34 +0800429 return 0;
430 }
431 round_trip_ps = 1100 + 500 + mem_delay_ps + 500 + 600;
432 delay_adder = round_trip_ps / (1000000 / pdram_timing->mhz);
433 if ((round_trip_ps % (1000000 / pdram_timing->mhz)) != 0)
434 delay_adder++;
435
436 phy_internal_delay = 5 + 2 + 4;
437 lpddr_adder = mem_delay_ps / (1000000 / pdram_timing->mhz);
438 if ((mem_delay_ps % (1000000 / pdram_timing->mhz)) != 0)
439 lpddr_adder++;
440 dfi_adder = 0;
441 phy_internal_delay = phy_internal_delay + 2;
442 rdlat_delay = delay_adder + phy_internal_delay +
443 ie_delay_adder + lpddr_adder + dfi_adder;
444
445 rdlat_delay = rdlat_delay + 2;
446 return rdlat_delay;
447}
448
449static uint32_t get_pi_todtoff_min(struct dram_timing_t *pdram_timing,
450 struct timing_related_config *timing_config)
451{
452 uint32_t tmp, todtoff_min_ps;
453
454 if (timing_config->dram_type == LPDDR3)
455 todtoff_min_ps = 2500;
456 else if (timing_config->dram_type == LPDDR4)
457 todtoff_min_ps = 1500;
458 else
459 todtoff_min_ps = 0;
460 /* todtoff_min */
461 tmp = todtoff_min_ps / (1000000 / pdram_timing->mhz);
462 if ((todtoff_min_ps % (1000000 / pdram_timing->mhz)) != 0)
463 tmp++;
464 return tmp;
465}
466
467static uint32_t get_pi_todtoff_max(struct dram_timing_t *pdram_timing,
468 struct timing_related_config *timing_config)
469{
470 uint32_t tmp, todtoff_max_ps;
471
472 if ((timing_config->dram_type == LPDDR4)
473 || (timing_config->dram_type == LPDDR3))
474 todtoff_max_ps = 3500;
475 else
476 todtoff_max_ps = 0;
477
478 /* todtoff_max */
479 tmp = todtoff_max_ps / (1000000 / pdram_timing->mhz);
480 if ((todtoff_max_ps % (1000000 / pdram_timing->mhz)) != 0)
481 tmp++;
482 return tmp;
483}
484
485static void gen_rk3399_ctl_params_f0(struct timing_related_config
486 *timing_config,
487 struct dram_timing_t *pdram_timing)
488{
489 uint32_t i;
490 uint32_t tmp, tmp1;
491
492 for (i = 0; i < timing_config->ch_cnt; i++) {
493 if (timing_config->dram_type == DDR3) {
494 tmp = ((700000 + 10) * timing_config->freq +
495 999) / 1000;
496 tmp += pdram_timing->txsnr + (pdram_timing->tmrd * 3) +
497 pdram_timing->tmod + pdram_timing->tzqinit;
Caesar Wang8bc16672016-10-27 01:12:47 +0800498 mmio_write_32(CTL_REG(i, 5), tmp);
Caesar Wanga8456902016-10-27 01:12:34 +0800499
Caesar Wang8bc16672016-10-27 01:12:47 +0800500 mmio_clrsetbits_32(CTL_REG(i, 22), 0xffff,
501 pdram_timing->tdllk);
Caesar Wanga8456902016-10-27 01:12:34 +0800502
Caesar Wang8bc16672016-10-27 01:12:47 +0800503 mmio_write_32(CTL_REG(i, 32),
504 (pdram_timing->tmod << 8) |
505 pdram_timing->tmrd);
Caesar Wanga8456902016-10-27 01:12:34 +0800506
Justin Chadwell2b558a62019-07-03 14:11:28 +0100507 mmio_clrsetbits_32(CTL_REG(i, 59), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800508 (pdram_timing->txsr -
509 pdram_timing->trcd) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800510 } else if (timing_config->dram_type == LPDDR4) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800511 mmio_write_32(CTL_REG(i, 5), pdram_timing->tinit1 +
512 pdram_timing->tinit3);
513 mmio_write_32(CTL_REG(i, 32),
514 (pdram_timing->tmrd << 8) |
515 pdram_timing->tmrd);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100516 mmio_clrsetbits_32(CTL_REG(i, 59), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800517 pdram_timing->txsr << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800518 } else {
Caesar Wang8bc16672016-10-27 01:12:47 +0800519 mmio_write_32(CTL_REG(i, 5), pdram_timing->tinit1);
520 mmio_write_32(CTL_REG(i, 7), pdram_timing->tinit4);
521 mmio_write_32(CTL_REG(i, 32),
522 (pdram_timing->tmrd << 8) |
523 pdram_timing->tmrd);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100524 mmio_clrsetbits_32(CTL_REG(i, 59), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800525 pdram_timing->txsr << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800526 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800527 mmio_write_32(CTL_REG(i, 6), pdram_timing->tinit3);
528 mmio_write_32(CTL_REG(i, 8), pdram_timing->tinit5);
529 mmio_clrsetbits_32(CTL_REG(i, 23), (0x7f << 16),
530 ((pdram_timing->cl * 2) << 16));
531 mmio_clrsetbits_32(CTL_REG(i, 23), (0x1f << 24),
532 (pdram_timing->cwl << 24));
533 mmio_clrsetbits_32(CTL_REG(i, 24), 0x3f, pdram_timing->al);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100534 mmio_clrsetbits_32(CTL_REG(i, 26), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800535 (pdram_timing->trc << 24) |
536 (pdram_timing->trrd << 16));
537 mmio_write_32(CTL_REG(i, 27),
538 (pdram_timing->tfaw << 24) |
539 (pdram_timing->trppb << 16) |
540 (pdram_timing->twtr << 8) |
541 pdram_timing->tras_min);
Caesar Wanga8456902016-10-27 01:12:34 +0800542
Justin Chadwell2b558a62019-07-03 14:11:28 +0100543 mmio_clrsetbits_32(CTL_REG(i, 31), 0xffu << 24,
Caesar Wang8bc16672016-10-27 01:12:47 +0800544 max(4, pdram_timing->trtp) << 24);
545 mmio_write_32(CTL_REG(i, 33), (pdram_timing->tcke << 24) |
546 pdram_timing->tras_max);
547 mmio_clrsetbits_32(CTL_REG(i, 34), 0xff,
548 max(1, pdram_timing->tckesr));
549 mmio_clrsetbits_32(CTL_REG(i, 39),
550 (0x3f << 16) | (0xff << 8),
551 (pdram_timing->twr << 16) |
552 (pdram_timing->trcd << 8));
553 mmio_clrsetbits_32(CTL_REG(i, 42), 0x1f << 16,
554 pdram_timing->tmrz << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800555 tmp = pdram_timing->tdal ? pdram_timing->tdal :
Caesar Wang8bc16672016-10-27 01:12:47 +0800556 (pdram_timing->twr + pdram_timing->trp);
557 mmio_clrsetbits_32(CTL_REG(i, 44), 0xff, tmp);
558 mmio_clrsetbits_32(CTL_REG(i, 45), 0xff, pdram_timing->trp);
559 mmio_write_32(CTL_REG(i, 48),
560 ((pdram_timing->trefi - 8) << 16) |
561 pdram_timing->trfc);
562 mmio_clrsetbits_32(CTL_REG(i, 52), 0xffff, pdram_timing->txp);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100563 mmio_clrsetbits_32(CTL_REG(i, 53), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800564 pdram_timing->txpdll << 16);
565 mmio_clrsetbits_32(CTL_REG(i, 55), 0xf << 24,
566 pdram_timing->tcscke << 24);
567 mmio_clrsetbits_32(CTL_REG(i, 55), 0xff, pdram_timing->tmrri);
568 mmio_write_32(CTL_REG(i, 56),
569 (pdram_timing->tzqcke << 24) |
570 (pdram_timing->tmrwckel << 16) |
571 (pdram_timing->tckehcs << 8) |
572 pdram_timing->tckelcs);
573 mmio_clrsetbits_32(CTL_REG(i, 60), 0xffff, pdram_timing->txsnr);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100574 mmio_clrsetbits_32(CTL_REG(i, 62), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800575 (pdram_timing->tckehcmd << 24) |
576 (pdram_timing->tckelcmd << 16));
577 mmio_write_32(CTL_REG(i, 63),
578 (pdram_timing->tckelpd << 24) |
579 (pdram_timing->tescke << 16) |
580 (pdram_timing->tsr << 8) |
581 pdram_timing->tckckel);
582 mmio_clrsetbits_32(CTL_REG(i, 64), 0xfff,
583 (pdram_timing->tcmdcke << 8) |
584 pdram_timing->tcsckeh);
585 mmio_clrsetbits_32(CTL_REG(i, 92), 0xffff << 8,
586 (pdram_timing->tcksrx << 16) |
587 (pdram_timing->tcksre << 8));
588 mmio_clrsetbits_32(CTL_REG(i, 108), 0x1 << 24,
589 (timing_config->dllbp << 24));
590 mmio_clrsetbits_32(CTL_REG(i, 122), 0x3ff << 16,
591 (pdram_timing->tvrcg_enable << 16));
592 mmio_write_32(CTL_REG(i, 123), (pdram_timing->tfc_long << 16) |
593 pdram_timing->tvrcg_disable);
594 mmio_write_32(CTL_REG(i, 124),
595 (pdram_timing->tvref_long << 16) |
596 (pdram_timing->tckfspx << 8) |
597 pdram_timing->tckfspe);
598 mmio_write_32(CTL_REG(i, 133), (pdram_timing->mr[1] << 16) |
599 pdram_timing->mr[0]);
600 mmio_clrsetbits_32(CTL_REG(i, 134), 0xffff,
601 pdram_timing->mr[2]);
602 mmio_clrsetbits_32(CTL_REG(i, 138), 0xffff,
603 pdram_timing->mr[3]);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100604 mmio_clrsetbits_32(CTL_REG(i, 139), 0xffu << 24,
Caesar Wang8bc16672016-10-27 01:12:47 +0800605 pdram_timing->mr11 << 24);
606 mmio_write_32(CTL_REG(i, 147),
607 (pdram_timing->mr[1] << 16) |
608 pdram_timing->mr[0]);
609 mmio_clrsetbits_32(CTL_REG(i, 148), 0xffff,
610 pdram_timing->mr[2]);
611 mmio_clrsetbits_32(CTL_REG(i, 152), 0xffff,
612 pdram_timing->mr[3]);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100613 mmio_clrsetbits_32(CTL_REG(i, 153), 0xffu << 24,
Caesar Wang8bc16672016-10-27 01:12:47 +0800614 pdram_timing->mr11 << 24);
Caesar Wanga8456902016-10-27 01:12:34 +0800615 if (timing_config->dram_type == LPDDR4) {
Justin Chadwell2b558a62019-07-03 14:11:28 +0100616 mmio_clrsetbits_32(CTL_REG(i, 140), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800617 pdram_timing->mr12 << 16);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100618 mmio_clrsetbits_32(CTL_REG(i, 142), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800619 pdram_timing->mr14 << 16);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100620 mmio_clrsetbits_32(CTL_REG(i, 145), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800621 pdram_timing->mr22 << 16);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100622 mmio_clrsetbits_32(CTL_REG(i, 154), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800623 pdram_timing->mr12 << 16);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100624 mmio_clrsetbits_32(CTL_REG(i, 156), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800625 pdram_timing->mr14 << 16);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100626 mmio_clrsetbits_32(CTL_REG(i, 159), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800627 pdram_timing->mr22 << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800628 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800629 mmio_clrsetbits_32(CTL_REG(i, 179), 0xfff << 8,
630 pdram_timing->tzqinit << 8);
631 mmio_write_32(CTL_REG(i, 180), (pdram_timing->tzqcs << 16) |
632 (pdram_timing->tzqinit / 2));
633 mmio_write_32(CTL_REG(i, 181), (pdram_timing->tzqlat << 16) |
634 pdram_timing->tzqcal);
635 mmio_clrsetbits_32(CTL_REG(i, 212), 0xff << 8,
636 pdram_timing->todton << 8);
Caesar Wanga8456902016-10-27 01:12:34 +0800637
638 if (timing_config->odt) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800639 mmio_setbits_32(CTL_REG(i, 213), 1 << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800640 if (timing_config->freq < 400)
641 tmp = 4 << 24;
642 else
643 tmp = 8 << 24;
644 } else {
Caesar Wang8bc16672016-10-27 01:12:47 +0800645 mmio_clrbits_32(CTL_REG(i, 213), 1 << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800646 tmp = 2 << 24;
647 }
648
Caesar Wang8bc16672016-10-27 01:12:47 +0800649 mmio_clrsetbits_32(CTL_REG(i, 216), 0x1f << 24, tmp);
650 mmio_clrsetbits_32(CTL_REG(i, 221), (0x3 << 16) | (0xf << 8),
651 (pdram_timing->tdqsck << 16) |
652 (pdram_timing->tdqsck_max << 8));
Caesar Wanga8456902016-10-27 01:12:34 +0800653 tmp =
654 (get_wrlat_adj(timing_config->dram_type, pdram_timing->cwl)
655 << 8) | get_rdlat_adj(timing_config->dram_type,
656 pdram_timing->cl);
Caesar Wang8bc16672016-10-27 01:12:47 +0800657 mmio_clrsetbits_32(CTL_REG(i, 284), 0xffff, tmp);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100658 mmio_clrsetbits_32(CTL_REG(i, 82), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800659 (4 * pdram_timing->trefi) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800660
Caesar Wang8bc16672016-10-27 01:12:47 +0800661 mmio_clrsetbits_32(CTL_REG(i, 83), 0xffff,
662 (2 * pdram_timing->trefi) & 0xffff);
Caesar Wanga8456902016-10-27 01:12:34 +0800663
664 if ((timing_config->dram_type == LPDDR3) ||
665 (timing_config->dram_type == LPDDR4)) {
666 tmp = get_pi_wrlat(pdram_timing, timing_config);
667 tmp1 = get_pi_todtoff_max(pdram_timing, timing_config);
668 tmp = (tmp > tmp1) ? (tmp - tmp1) : 0;
669 } else {
670 tmp = 0;
671 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800672 mmio_clrsetbits_32(CTL_REG(i, 214), 0x3f << 16,
673 (tmp & 0x3f) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800674
675 if ((timing_config->dram_type == LPDDR3) ||
676 (timing_config->dram_type == LPDDR4)) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800677 /* min_rl_preamble = cl+TDQSCK_MIN -1 */
Caesar Wanga8456902016-10-27 01:12:34 +0800678 tmp = pdram_timing->cl +
679 get_pi_todtoff_min(pdram_timing, timing_config) - 1;
680 /* todtoff_max */
681 tmp1 = get_pi_todtoff_max(pdram_timing, timing_config);
682 tmp = (tmp > tmp1) ? (tmp - tmp1) : 0;
683 } else {
684 tmp = pdram_timing->cl - pdram_timing->cwl;
685 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800686 mmio_clrsetbits_32(CTL_REG(i, 215), 0x3f << 8,
687 (tmp & 0x3f) << 8);
Caesar Wanga8456902016-10-27 01:12:34 +0800688
Caesar Wang8bc16672016-10-27 01:12:47 +0800689 mmio_clrsetbits_32(CTL_REG(i, 275), 0xff << 16,
690 (get_pi_tdfi_phy_rdlat(pdram_timing,
691 timing_config) &
692 0xff) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800693
Caesar Wang8bc16672016-10-27 01:12:47 +0800694 mmio_clrsetbits_32(CTL_REG(i, 277), 0xffff,
695 (2 * pdram_timing->trefi) & 0xffff);
Caesar Wanga8456902016-10-27 01:12:34 +0800696
Caesar Wang8bc16672016-10-27 01:12:47 +0800697 mmio_clrsetbits_32(CTL_REG(i, 282), 0xffff,
698 (2 * pdram_timing->trefi) & 0xffff);
Caesar Wanga8456902016-10-27 01:12:34 +0800699
Caesar Wang8bc16672016-10-27 01:12:47 +0800700 mmio_write_32(CTL_REG(i, 283), 20 * pdram_timing->trefi);
Caesar Wanga8456902016-10-27 01:12:34 +0800701
702 /* CTL_308 TDFI_CALVL_CAPTURE_F0:RW:16:10 */
703 tmp1 = 20000 / (1000000 / pdram_timing->mhz) + 1;
704 if ((20000 % (1000000 / pdram_timing->mhz)) != 0)
705 tmp1++;
706 tmp = (tmp1 >> 1) + (tmp1 % 2) + 5;
Caesar Wang8bc16672016-10-27 01:12:47 +0800707 mmio_clrsetbits_32(CTL_REG(i, 308), 0x3ff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800708
709 /* CTL_308 TDFI_CALVL_CC_F0:RW:0:10 */
710 tmp = tmp + 18;
Caesar Wang8bc16672016-10-27 01:12:47 +0800711 mmio_clrsetbits_32(CTL_REG(i, 308), 0x3ff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +0800712
713 /* CTL_314 TDFI_WRCSLAT_F0:RW:8:8 */
714 tmp1 = get_pi_wrlat_adj(pdram_timing, timing_config);
Derek Basehore0e8909d2016-11-09 18:28:19 -0800715 if (timing_config->freq <= TDFI_LAT_THRESHOLD_FREQ) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800716 if (tmp1 == 0)
717 tmp = 0;
718 else if (tmp1 < 5)
719 tmp = tmp1 - 1;
720 else
Caesar Wanga8456902016-10-27 01:12:34 +0800721 tmp = tmp1 - 5;
Caesar Wanga8456902016-10-27 01:12:34 +0800722 } else {
723 tmp = tmp1 - 2;
724 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800725 mmio_clrsetbits_32(CTL_REG(i, 314), 0xff << 8, tmp << 8);
Caesar Wanga8456902016-10-27 01:12:34 +0800726
727 /* CTL_314 TDFI_RDCSLAT_F0:RW:0:8 */
Derek Basehore0e8909d2016-11-09 18:28:19 -0800728 if ((timing_config->freq <= TDFI_LAT_THRESHOLD_FREQ) &&
Caesar Wang8bc16672016-10-27 01:12:47 +0800729 (pdram_timing->cl >= 5))
Caesar Wanga8456902016-10-27 01:12:34 +0800730 tmp = pdram_timing->cl - 5;
731 else
732 tmp = pdram_timing->cl - 2;
Caesar Wang8bc16672016-10-27 01:12:47 +0800733 mmio_clrsetbits_32(CTL_REG(i, 314), 0xff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +0800734 }
735}
736
737static void gen_rk3399_ctl_params_f1(struct timing_related_config
738 *timing_config,
739 struct dram_timing_t *pdram_timing)
740{
741 uint32_t i;
742 uint32_t tmp, tmp1;
743
744 for (i = 0; i < timing_config->ch_cnt; i++) {
745 if (timing_config->dram_type == DDR3) {
746 tmp =
Caesar Wang8bc16672016-10-27 01:12:47 +0800747 ((700000 + 10) * timing_config->freq + 999) / 1000;
748 tmp += pdram_timing->txsnr + (pdram_timing->tmrd * 3) +
749 pdram_timing->tmod + pdram_timing->tzqinit;
750 mmio_write_32(CTL_REG(i, 9), tmp);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100751 mmio_clrsetbits_32(CTL_REG(i, 22), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800752 pdram_timing->tdllk << 16);
753 mmio_clrsetbits_32(CTL_REG(i, 34), 0xffffff00,
754 (pdram_timing->tmod << 24) |
755 (pdram_timing->tmrd << 16) |
756 (pdram_timing->trtp << 8));
Justin Chadwell2b558a62019-07-03 14:11:28 +0100757 mmio_clrsetbits_32(CTL_REG(i, 60), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800758 (pdram_timing->txsr -
759 pdram_timing->trcd) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800760 } else if (timing_config->dram_type == LPDDR4) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800761 mmio_write_32(CTL_REG(i, 9), pdram_timing->tinit1 +
762 pdram_timing->tinit3);
763 mmio_clrsetbits_32(CTL_REG(i, 34), 0xffffff00,
764 (pdram_timing->tmrd << 24) |
765 (pdram_timing->tmrd << 16) |
766 (pdram_timing->trtp << 8));
Justin Chadwell2b558a62019-07-03 14:11:28 +0100767 mmio_clrsetbits_32(CTL_REG(i, 60), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800768 pdram_timing->txsr << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800769 } else {
Caesar Wang8bc16672016-10-27 01:12:47 +0800770 mmio_write_32(CTL_REG(i, 9), pdram_timing->tinit1);
771 mmio_write_32(CTL_REG(i, 11), pdram_timing->tinit4);
772 mmio_clrsetbits_32(CTL_REG(i, 34), 0xffffff00,
773 (pdram_timing->tmrd << 24) |
774 (pdram_timing->tmrd << 16) |
775 (pdram_timing->trtp << 8));
Justin Chadwell2b558a62019-07-03 14:11:28 +0100776 mmio_clrsetbits_32(CTL_REG(i, 60), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800777 pdram_timing->txsr << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800778 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800779 mmio_write_32(CTL_REG(i, 10), pdram_timing->tinit3);
780 mmio_write_32(CTL_REG(i, 12), pdram_timing->tinit5);
781 mmio_clrsetbits_32(CTL_REG(i, 24), (0x7f << 8),
782 ((pdram_timing->cl * 2) << 8));
783 mmio_clrsetbits_32(CTL_REG(i, 24), (0x1f << 16),
784 (pdram_timing->cwl << 16));
785 mmio_clrsetbits_32(CTL_REG(i, 24), 0x3f << 24,
786 pdram_timing->al << 24);
787 mmio_clrsetbits_32(CTL_REG(i, 28), 0xffffff00,
788 (pdram_timing->tras_min << 24) |
789 (pdram_timing->trc << 16) |
790 (pdram_timing->trrd << 8));
791 mmio_clrsetbits_32(CTL_REG(i, 29), 0xffffff,
792 (pdram_timing->tfaw << 16) |
793 (pdram_timing->trppb << 8) |
794 pdram_timing->twtr);
795 mmio_write_32(CTL_REG(i, 35), (pdram_timing->tcke << 24) |
796 pdram_timing->tras_max);
797 mmio_clrsetbits_32(CTL_REG(i, 36), 0xff,
798 max(1, pdram_timing->tckesr));
Justin Chadwell2b558a62019-07-03 14:11:28 +0100799 mmio_clrsetbits_32(CTL_REG(i, 39), (0xffu << 24),
Caesar Wang8bc16672016-10-27 01:12:47 +0800800 (pdram_timing->trcd << 24));
801 mmio_clrsetbits_32(CTL_REG(i, 40), 0x3f, pdram_timing->twr);
802 mmio_clrsetbits_32(CTL_REG(i, 42), 0x1f << 24,
803 pdram_timing->tmrz << 24);
Caesar Wanga8456902016-10-27 01:12:34 +0800804 tmp = pdram_timing->tdal ? pdram_timing->tdal :
Caesar Wang8bc16672016-10-27 01:12:47 +0800805 (pdram_timing->twr + pdram_timing->trp);
806 mmio_clrsetbits_32(CTL_REG(i, 44), 0xff << 8, tmp << 8);
807 mmio_clrsetbits_32(CTL_REG(i, 45), 0xff << 8,
808 pdram_timing->trp << 8);
809 mmio_write_32(CTL_REG(i, 49),
810 ((pdram_timing->trefi - 8) << 16) |
811 pdram_timing->trfc);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100812 mmio_clrsetbits_32(CTL_REG(i, 52), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800813 pdram_timing->txp << 16);
814 mmio_clrsetbits_32(CTL_REG(i, 54), 0xffff,
815 pdram_timing->txpdll);
816 mmio_clrsetbits_32(CTL_REG(i, 55), 0xff << 8,
817 pdram_timing->tmrri << 8);
818 mmio_write_32(CTL_REG(i, 57), (pdram_timing->tmrwckel << 24) |
819 (pdram_timing->tckehcs << 16) |
820 (pdram_timing->tckelcs << 8) |
821 pdram_timing->tcscke);
822 mmio_clrsetbits_32(CTL_REG(i, 58), 0xf, pdram_timing->tzqcke);
823 mmio_clrsetbits_32(CTL_REG(i, 61), 0xffff, pdram_timing->txsnr);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100824 mmio_clrsetbits_32(CTL_REG(i, 64), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800825 (pdram_timing->tckehcmd << 24) |
826 (pdram_timing->tckelcmd << 16));
827 mmio_write_32(CTL_REG(i, 65), (pdram_timing->tckelpd << 24) |
828 (pdram_timing->tescke << 16) |
829 (pdram_timing->tsr << 8) |
830 pdram_timing->tckckel);
831 mmio_clrsetbits_32(CTL_REG(i, 66), 0xfff,
832 (pdram_timing->tcmdcke << 8) |
833 pdram_timing->tcsckeh);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100834 mmio_clrsetbits_32(CTL_REG(i, 92), (0xffu << 24),
Caesar Wang8bc16672016-10-27 01:12:47 +0800835 (pdram_timing->tcksre << 24));
836 mmio_clrsetbits_32(CTL_REG(i, 93), 0xff,
837 pdram_timing->tcksrx);
838 mmio_clrsetbits_32(CTL_REG(i, 108), (0x1 << 25),
839 (timing_config->dllbp << 25));
840 mmio_write_32(CTL_REG(i, 125),
841 (pdram_timing->tvrcg_disable << 16) |
842 pdram_timing->tvrcg_enable);
843 mmio_write_32(CTL_REG(i, 126), (pdram_timing->tckfspx << 24) |
844 (pdram_timing->tckfspe << 16) |
845 pdram_timing->tfc_long);
846 mmio_clrsetbits_32(CTL_REG(i, 127), 0xffff,
847 pdram_timing->tvref_long);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100848 mmio_clrsetbits_32(CTL_REG(i, 134), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800849 pdram_timing->mr[0] << 16);
850 mmio_write_32(CTL_REG(i, 135), (pdram_timing->mr[2] << 16) |
851 pdram_timing->mr[1]);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100852 mmio_clrsetbits_32(CTL_REG(i, 138), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800853 pdram_timing->mr[3] << 16);
854 mmio_clrsetbits_32(CTL_REG(i, 140), 0xff, pdram_timing->mr11);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100855 mmio_clrsetbits_32(CTL_REG(i, 148), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800856 pdram_timing->mr[0] << 16);
857 mmio_write_32(CTL_REG(i, 149), (pdram_timing->mr[2] << 16) |
858 pdram_timing->mr[1]);
Justin Chadwell2b558a62019-07-03 14:11:28 +0100859 mmio_clrsetbits_32(CTL_REG(i, 152), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800860 pdram_timing->mr[3] << 16);
861 mmio_clrsetbits_32(CTL_REG(i, 154), 0xff, pdram_timing->mr11);
Caesar Wanga8456902016-10-27 01:12:34 +0800862 if (timing_config->dram_type == LPDDR4) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800863 mmio_clrsetbits_32(CTL_REG(i, 141), 0xffff,
864 pdram_timing->mr12);
865 mmio_clrsetbits_32(CTL_REG(i, 143), 0xffff,
866 pdram_timing->mr14);
867 mmio_clrsetbits_32(CTL_REG(i, 146), 0xffff,
868 pdram_timing->mr22);
869 mmio_clrsetbits_32(CTL_REG(i, 155), 0xffff,
870 pdram_timing->mr12);
871 mmio_clrsetbits_32(CTL_REG(i, 157), 0xffff,
872 pdram_timing->mr14);
873 mmio_clrsetbits_32(CTL_REG(i, 160), 0xffff,
874 pdram_timing->mr22);
Caesar Wanga8456902016-10-27 01:12:34 +0800875 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800876 mmio_write_32(CTL_REG(i, 182),
877 ((pdram_timing->tzqinit / 2) << 16) |
878 pdram_timing->tzqinit);
879 mmio_write_32(CTL_REG(i, 183), (pdram_timing->tzqcal << 16) |
880 pdram_timing->tzqcs);
881 mmio_clrsetbits_32(CTL_REG(i, 184), 0x3f, pdram_timing->tzqlat);
882 mmio_clrsetbits_32(CTL_REG(i, 188), 0xfff,
883 pdram_timing->tzqreset);
884 mmio_clrsetbits_32(CTL_REG(i, 212), 0xff << 16,
885 pdram_timing->todton << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800886
887 if (timing_config->odt) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800888 mmio_setbits_32(CTL_REG(i, 213), (1 << 24));
Caesar Wanga8456902016-10-27 01:12:34 +0800889 if (timing_config->freq < 400)
890 tmp = 4 << 24;
891 else
892 tmp = 8 << 24;
893 } else {
Caesar Wang8bc16672016-10-27 01:12:47 +0800894 mmio_clrbits_32(CTL_REG(i, 213), (1 << 24));
Caesar Wanga8456902016-10-27 01:12:34 +0800895 tmp = 2 << 24;
896 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800897 mmio_clrsetbits_32(CTL_REG(i, 217), 0x1f << 24, tmp);
898 mmio_clrsetbits_32(CTL_REG(i, 221), 0xf << 24,
899 (pdram_timing->tdqsck_max << 24));
900 mmio_clrsetbits_32(CTL_REG(i, 222), 0x3, pdram_timing->tdqsck);
901 mmio_clrsetbits_32(CTL_REG(i, 291), 0xffff,
902 (get_wrlat_adj(timing_config->dram_type,
903 pdram_timing->cwl) << 8) |
904 get_rdlat_adj(timing_config->dram_type,
905 pdram_timing->cl));
Caesar Wanga8456902016-10-27 01:12:34 +0800906
Caesar Wang8bc16672016-10-27 01:12:47 +0800907 mmio_clrsetbits_32(CTL_REG(i, 84), 0xffff,
908 (4 * pdram_timing->trefi) & 0xffff);
Caesar Wanga8456902016-10-27 01:12:34 +0800909
Justin Chadwell2b558a62019-07-03 14:11:28 +0100910 mmio_clrsetbits_32(CTL_REG(i, 84), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800911 ((2 * pdram_timing->trefi) & 0xffff) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800912
913 if ((timing_config->dram_type == LPDDR3) ||
914 (timing_config->dram_type == LPDDR4)) {
915 tmp = get_pi_wrlat(pdram_timing, timing_config);
916 tmp1 = get_pi_todtoff_max(pdram_timing, timing_config);
917 tmp = (tmp > tmp1) ? (tmp - tmp1) : 0;
918 } else {
919 tmp = 0;
920 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800921 mmio_clrsetbits_32(CTL_REG(i, 214), 0x3f << 24,
922 (tmp & 0x3f) << 24);
Caesar Wanga8456902016-10-27 01:12:34 +0800923
924 if ((timing_config->dram_type == LPDDR3) ||
925 (timing_config->dram_type == LPDDR4)) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800926 /* min_rl_preamble = cl + TDQSCK_MIN - 1 */
Caesar Wanga8456902016-10-27 01:12:34 +0800927 tmp = pdram_timing->cl +
Caesar Wang8bc16672016-10-27 01:12:47 +0800928 get_pi_todtoff_min(pdram_timing, timing_config);
929 tmp--;
Caesar Wanga8456902016-10-27 01:12:34 +0800930 /* todtoff_max */
931 tmp1 = get_pi_todtoff_max(pdram_timing, timing_config);
932 tmp = (tmp > tmp1) ? (tmp - tmp1) : 0;
933 } else {
934 tmp = pdram_timing->cl - pdram_timing->cwl;
935 }
Caesar Wang8bc16672016-10-27 01:12:47 +0800936 mmio_clrsetbits_32(CTL_REG(i, 215), 0x3f << 16,
937 (tmp & 0x3f) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800938
Justin Chadwell2b558a62019-07-03 14:11:28 +0100939 mmio_clrsetbits_32(CTL_REG(i, 275), 0xffu << 24,
Caesar Wang8bc16672016-10-27 01:12:47 +0800940 (get_pi_tdfi_phy_rdlat(pdram_timing,
941 timing_config) &
942 0xff) << 24);
Caesar Wanga8456902016-10-27 01:12:34 +0800943
Justin Chadwell2b558a62019-07-03 14:11:28 +0100944 mmio_clrsetbits_32(CTL_REG(i, 284), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +0800945 ((2 * pdram_timing->trefi) & 0xffff) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800946
Caesar Wang8bc16672016-10-27 01:12:47 +0800947 mmio_clrsetbits_32(CTL_REG(i, 289), 0xffff,
948 (2 * pdram_timing->trefi) & 0xffff);
Caesar Wanga8456902016-10-27 01:12:34 +0800949
Caesar Wang8bc16672016-10-27 01:12:47 +0800950 mmio_write_32(CTL_REG(i, 290), 20 * pdram_timing->trefi);
Caesar Wanga8456902016-10-27 01:12:34 +0800951
952 /* CTL_309 TDFI_CALVL_CAPTURE_F1:RW:16:10 */
953 tmp1 = 20000 / (1000000 / pdram_timing->mhz) + 1;
954 if ((20000 % (1000000 / pdram_timing->mhz)) != 0)
955 tmp1++;
956 tmp = (tmp1 >> 1) + (tmp1 % 2) + 5;
Caesar Wang8bc16672016-10-27 01:12:47 +0800957 mmio_clrsetbits_32(CTL_REG(i, 309), 0x3ff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800958
959 /* CTL_309 TDFI_CALVL_CC_F1:RW:0:10 */
960 tmp = tmp + 18;
Caesar Wang8bc16672016-10-27 01:12:47 +0800961 mmio_clrsetbits_32(CTL_REG(i, 309), 0x3ff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +0800962
963 /* CTL_314 TDFI_WRCSLAT_F1:RW:24:8 */
964 tmp1 = get_pi_wrlat_adj(pdram_timing, timing_config);
Derek Basehore0e8909d2016-11-09 18:28:19 -0800965 if (timing_config->freq <= TDFI_LAT_THRESHOLD_FREQ) {
Caesar Wang8bc16672016-10-27 01:12:47 +0800966 if (tmp1 == 0)
967 tmp = 0;
968 else if (tmp1 < 5)
969 tmp = tmp1 - 1;
970 else
Caesar Wanga8456902016-10-27 01:12:34 +0800971 tmp = tmp1 - 5;
Caesar Wanga8456902016-10-27 01:12:34 +0800972 } else {
973 tmp = tmp1 - 2;
974 }
975
Justin Chadwell2b558a62019-07-03 14:11:28 +0100976 mmio_clrsetbits_32(CTL_REG(i, 314), 0xffu << 24, tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +0800977
978 /* CTL_314 TDFI_RDCSLAT_F1:RW:16:8 */
Derek Basehore0e8909d2016-11-09 18:28:19 -0800979 if ((timing_config->freq <= TDFI_LAT_THRESHOLD_FREQ) &&
Caesar Wang8bc16672016-10-27 01:12:47 +0800980 (pdram_timing->cl >= 5))
Caesar Wanga8456902016-10-27 01:12:34 +0800981 tmp = pdram_timing->cl - 5;
982 else
983 tmp = pdram_timing->cl - 2;
Caesar Wang8bc16672016-10-27 01:12:47 +0800984 mmio_clrsetbits_32(CTL_REG(i, 314), 0xff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +0800985 }
986}
987
Derek Basehoreb1065122016-10-20 22:09:22 -0700988static void gen_rk3399_enable_training(uint32_t ch_cnt, uint32_t nmhz)
989{
990 uint32_t i, tmp;
991
992 if (nmhz <= PHY_DLL_BYPASS_FREQ)
993 tmp = 0;
994 else
995 tmp = 1;
996
997 for (i = 0; i < ch_cnt; i++) {
998 mmio_clrsetbits_32(CTL_REG(i, 305), 1 << 16, tmp << 16);
999 mmio_clrsetbits_32(CTL_REG(i, 71), 1, tmp);
Lin Huangdc8e82e2016-12-16 13:59:07 +08001000 mmio_clrsetbits_32(CTL_REG(i, 70), 1 << 8, 1 << 8);
Derek Basehoreb1065122016-10-20 22:09:22 -07001001 }
1002}
1003
Xing Zheng035d6d62017-02-09 14:51:38 +08001004static void gen_rk3399_disable_training(uint32_t ch_cnt)
1005{
1006 uint32_t i;
1007
1008 for (i = 0; i < ch_cnt; i++) {
1009 mmio_clrbits_32(CTL_REG(i, 305), 1 << 16);
1010 mmio_clrbits_32(CTL_REG(i, 71), 1);
1011 mmio_clrbits_32(CTL_REG(i, 70), 1 << 8);
1012 }
1013}
1014
Caesar Wanga8456902016-10-27 01:12:34 +08001015static void gen_rk3399_ctl_params(struct timing_related_config *timing_config,
1016 struct dram_timing_t *pdram_timing,
1017 uint32_t fn)
1018{
1019 if (fn == 0)
1020 gen_rk3399_ctl_params_f0(timing_config, pdram_timing);
1021 else
1022 gen_rk3399_ctl_params_f1(timing_config, pdram_timing);
Caesar Wanga8456902016-10-27 01:12:34 +08001023}
1024
1025static void gen_rk3399_pi_params_f0(struct timing_related_config *timing_config,
1026 struct dram_timing_t *pdram_timing)
1027{
1028 uint32_t tmp, tmp1, tmp2;
1029 uint32_t i;
1030
1031 for (i = 0; i < timing_config->ch_cnt; i++) {
1032 /* PI_02 PI_TDFI_PHYMSTR_MAX_F0:RW:0:32 */
1033 tmp = 4 * pdram_timing->trefi;
Caesar Wang8bc16672016-10-27 01:12:47 +08001034 mmio_write_32(PI_REG(i, 2), tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001035 /* PI_03 PI_TDFI_PHYMSTR_RESP_F0:RW:0:16 */
1036 tmp = 2 * pdram_timing->trefi;
Caesar Wang8bc16672016-10-27 01:12:47 +08001037 mmio_clrsetbits_32(PI_REG(i, 3), 0xffff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001038 /* PI_07 PI_TDFI_PHYUPD_RESP_F0:RW:16:16 */
Justin Chadwell2b558a62019-07-03 14:11:28 +01001039 mmio_clrsetbits_32(PI_REG(i, 7), 0xffffu << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001040
1041 /* PI_42 PI_TDELAY_RDWR_2_BUS_IDLE_F0:RW:0:8 */
1042 if (timing_config->dram_type == LPDDR4)
1043 tmp = 2;
1044 else
1045 tmp = 0;
1046 tmp = (pdram_timing->bl / 2) + 4 +
Caesar Wang8bc16672016-10-27 01:12:47 +08001047 (get_pi_rdlat_adj(pdram_timing) - 2) + tmp +
1048 get_pi_tdfi_phy_rdlat(pdram_timing, timing_config);
1049 mmio_clrsetbits_32(PI_REG(i, 42), 0xff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001050 /* PI_43 PI_WRLAT_F0:RW:0:5 */
1051 if (timing_config->dram_type == LPDDR3) {
1052 tmp = get_pi_wrlat(pdram_timing, timing_config);
Caesar Wang8bc16672016-10-27 01:12:47 +08001053 mmio_clrsetbits_32(PI_REG(i, 43), 0x1f, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001054 }
1055 /* PI_43 PI_ADDITIVE_LAT_F0:RW:8:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001056 mmio_clrsetbits_32(PI_REG(i, 43), 0x3f << 8,
1057 PI_ADD_LATENCY << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001058
1059 /* PI_43 PI_CASLAT_LIN_F0:RW:16:7 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001060 mmio_clrsetbits_32(PI_REG(i, 43), 0x7f << 16,
1061 (pdram_timing->cl * 2) << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001062 /* PI_46 PI_TREF_F0:RW:16:16 */
Justin Chadwell2b558a62019-07-03 14:11:28 +01001063 mmio_clrsetbits_32(PI_REG(i, 46), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +08001064 pdram_timing->trefi << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001065 /* PI_46 PI_TRFC_F0:RW:0:10 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001066 mmio_clrsetbits_32(PI_REG(i, 46), 0x3ff, pdram_timing->trfc);
Caesar Wanga8456902016-10-27 01:12:34 +08001067 /* PI_66 PI_TODTL_2CMD_F0:RW:24:8 */
1068 if (timing_config->dram_type == LPDDR3) {
1069 tmp = get_pi_todtoff_max(pdram_timing, timing_config);
Justin Chadwell2b558a62019-07-03 14:11:28 +01001070 mmio_clrsetbits_32(PI_REG(i, 66), 0xffu << 24,
Caesar Wang8bc16672016-10-27 01:12:47 +08001071 tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001072 }
1073 /* PI_72 PI_WR_TO_ODTH_F0:RW:16:6 */
1074 if ((timing_config->dram_type == LPDDR3) ||
1075 (timing_config->dram_type == LPDDR4)) {
1076 tmp1 = get_pi_wrlat(pdram_timing, timing_config);
1077 tmp2 = get_pi_todtoff_max(pdram_timing, timing_config);
1078 if (tmp1 > tmp2)
1079 tmp = tmp1 - tmp2;
1080 else
1081 tmp = 0;
1082 } else if (timing_config->dram_type == DDR3) {
1083 tmp = 0;
1084 }
Caesar Wang8bc16672016-10-27 01:12:47 +08001085 mmio_clrsetbits_32(PI_REG(i, 72), 0x3f << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001086 /* PI_73 PI_RD_TO_ODTH_F0:RW:8:6 */
1087 if ((timing_config->dram_type == LPDDR3) ||
1088 (timing_config->dram_type == LPDDR4)) {
Caesar Wang8bc16672016-10-27 01:12:47 +08001089 /* min_rl_preamble = cl + TDQSCK_MIN - 1 */
1090 tmp1 = pdram_timing->cl;
1091 tmp1 += get_pi_todtoff_min(pdram_timing, timing_config);
1092 tmp1--;
Caesar Wanga8456902016-10-27 01:12:34 +08001093 /* todtoff_max */
1094 tmp2 = get_pi_todtoff_max(pdram_timing, timing_config);
1095 if (tmp1 > tmp2)
1096 tmp = tmp1 - tmp2;
1097 else
1098 tmp = 0;
1099 } else if (timing_config->dram_type == DDR3) {
1100 tmp = pdram_timing->cl - pdram_timing->cwl;
1101 }
Caesar Wang8bc16672016-10-27 01:12:47 +08001102 mmio_clrsetbits_32(PI_REG(i, 73), 0x3f << 8, tmp << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001103 /* PI_89 PI_RDLAT_ADJ_F0:RW:16:8 */
1104 tmp = get_pi_rdlat_adj(pdram_timing);
Caesar Wang8bc16672016-10-27 01:12:47 +08001105 mmio_clrsetbits_32(PI_REG(i, 89), 0xff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001106 /* PI_90 PI_WRLAT_ADJ_F0:RW:16:8 */
1107 tmp = get_pi_wrlat_adj(pdram_timing, timing_config);
Caesar Wang8bc16672016-10-27 01:12:47 +08001108 mmio_clrsetbits_32(PI_REG(i, 90), 0xff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001109 /* PI_91 PI_TDFI_WRCSLAT_F0:RW:16:8 */
1110 tmp1 = tmp;
Caesar Wang8bc16672016-10-27 01:12:47 +08001111 if (tmp1 == 0)
1112 tmp = 0;
1113 else if (tmp1 < 5)
1114 tmp = tmp1 - 1;
1115 else
Caesar Wanga8456902016-10-27 01:12:34 +08001116 tmp = tmp1 - 5;
Caesar Wang8bc16672016-10-27 01:12:47 +08001117 mmio_clrsetbits_32(PI_REG(i, 91), 0xff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001118 /* PI_95 PI_TDFI_CALVL_CAPTURE_F0:RW:16:10 */
1119 tmp1 = 20000 / (1000000 / pdram_timing->mhz) + 1;
1120 if ((20000 % (1000000 / pdram_timing->mhz)) != 0)
1121 tmp1++;
1122 tmp = (tmp1 >> 1) + (tmp1 % 2) + 5;
Caesar Wang8bc16672016-10-27 01:12:47 +08001123 mmio_clrsetbits_32(PI_REG(i, 95), 0x3ff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001124 /* PI_95 PI_TDFI_CALVL_CC_F0:RW:0:10 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001125 mmio_clrsetbits_32(PI_REG(i, 95), 0x3ff, tmp + 18);
Caesar Wanga8456902016-10-27 01:12:34 +08001126 /* PI_102 PI_TMRZ_F0:RW:8:5 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001127 mmio_clrsetbits_32(PI_REG(i, 102), 0x1f << 8,
1128 pdram_timing->tmrz << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001129 /* PI_111 PI_TDFI_CALVL_STROBE_F0:RW:8:4 */
1130 tmp1 = 2 * 1000 / (1000000 / pdram_timing->mhz);
1131 if ((2 * 1000 % (1000000 / pdram_timing->mhz)) != 0)
1132 tmp1++;
1133 /* pi_tdfi_calvl_strobe=tds_train+5 */
1134 tmp = tmp1 + 5;
Caesar Wang8bc16672016-10-27 01:12:47 +08001135 mmio_clrsetbits_32(PI_REG(i, 111), 0xf << 8, tmp << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001136 /* PI_116 PI_TCKEHDQS_F0:RW:16:6 */
1137 tmp = 10000 / (1000000 / pdram_timing->mhz);
1138 if ((10000 % (1000000 / pdram_timing->mhz)) != 0)
1139 tmp++;
1140 if (pdram_timing->mhz <= 100)
1141 tmp = tmp + 1;
1142 else
1143 tmp = tmp + 8;
Caesar Wang8bc16672016-10-27 01:12:47 +08001144 mmio_clrsetbits_32(PI_REG(i, 116), 0x3f << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001145 /* PI_125 PI_MR1_DATA_F0_0:RW+:8:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001146 mmio_clrsetbits_32(PI_REG(i, 125), 0xffff << 8,
1147 pdram_timing->mr[1] << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001148 /* PI_133 PI_MR1_DATA_F0_1:RW+:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001149 mmio_clrsetbits_32(PI_REG(i, 133), 0xffff, pdram_timing->mr[1]);
Caesar Wanga8456902016-10-27 01:12:34 +08001150 /* PI_140 PI_MR1_DATA_F0_2:RW+:16:16 */
Justin Chadwell2b558a62019-07-03 14:11:28 +01001151 mmio_clrsetbits_32(PI_REG(i, 140), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +08001152 pdram_timing->mr[1] << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001153 /* PI_148 PI_MR1_DATA_F0_3:RW+:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001154 mmio_clrsetbits_32(PI_REG(i, 148), 0xffff, pdram_timing->mr[1]);
Caesar Wanga8456902016-10-27 01:12:34 +08001155 /* PI_126 PI_MR2_DATA_F0_0:RW+:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001156 mmio_clrsetbits_32(PI_REG(i, 126), 0xffff, pdram_timing->mr[2]);
Caesar Wanga8456902016-10-27 01:12:34 +08001157 /* PI_133 PI_MR2_DATA_F0_1:RW+:16:16 */
Justin Chadwell2b558a62019-07-03 14:11:28 +01001158 mmio_clrsetbits_32(PI_REG(i, 133), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +08001159 pdram_timing->mr[2] << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001160 /* PI_141 PI_MR2_DATA_F0_2:RW+:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001161 mmio_clrsetbits_32(PI_REG(i, 141), 0xffff, pdram_timing->mr[2]);
Caesar Wanga8456902016-10-27 01:12:34 +08001162 /* PI_148 PI_MR2_DATA_F0_3:RW+:16:16 */
Justin Chadwell2b558a62019-07-03 14:11:28 +01001163 mmio_clrsetbits_32(PI_REG(i, 148), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +08001164 pdram_timing->mr[2] << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001165 /* PI_156 PI_TFC_F0:RW:0:10 */
Derek Basehorebe2a8952017-02-09 22:08:48 -08001166 mmio_clrsetbits_32(PI_REG(i, 156), 0x3ff,
1167 pdram_timing->tfc_long);
Caesar Wanga8456902016-10-27 01:12:34 +08001168 /* PI_158 PI_TWR_F0:RW:24:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001169 mmio_clrsetbits_32(PI_REG(i, 158), 0x3f << 24,
1170 pdram_timing->twr << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001171 /* PI_158 PI_TWTR_F0:RW:16:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001172 mmio_clrsetbits_32(PI_REG(i, 158), 0x3f << 16,
1173 pdram_timing->twtr << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001174 /* PI_158 PI_TRCD_F0:RW:8:8 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001175 mmio_clrsetbits_32(PI_REG(i, 158), 0xff << 8,
1176 pdram_timing->trcd << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001177 /* PI_158 PI_TRP_F0:RW:0:8 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001178 mmio_clrsetbits_32(PI_REG(i, 158), 0xff, pdram_timing->trp);
Caesar Wanga8456902016-10-27 01:12:34 +08001179 /* PI_157 PI_TRTP_F0:RW:24:8 */
Justin Chadwell2b558a62019-07-03 14:11:28 +01001180 mmio_clrsetbits_32(PI_REG(i, 157), 0xffu << 24,
Caesar Wang8bc16672016-10-27 01:12:47 +08001181 pdram_timing->trtp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001182 /* PI_159 PI_TRAS_MIN_F0:RW:24:8 */
Justin Chadwell2b558a62019-07-03 14:11:28 +01001183 mmio_clrsetbits_32(PI_REG(i, 159), 0xffu << 24,
Caesar Wang8bc16672016-10-27 01:12:47 +08001184 pdram_timing->tras_min << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001185 /* PI_159 PI_TRAS_MAX_F0:RW:0:17 */
1186 tmp = pdram_timing->tras_max * 99 / 100;
Caesar Wang8bc16672016-10-27 01:12:47 +08001187 mmio_clrsetbits_32(PI_REG(i, 159), 0x1ffff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001188 /* PI_160 PI_TMRD_F0:RW:16:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001189 mmio_clrsetbits_32(PI_REG(i, 160), 0x3f << 16,
1190 pdram_timing->tmrd << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001191 /*PI_160 PI_TDQSCK_MAX_F0:RW:0:4 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001192 mmio_clrsetbits_32(PI_REG(i, 160), 0xf,
1193 pdram_timing->tdqsck_max);
Caesar Wanga8456902016-10-27 01:12:34 +08001194 /* PI_187 PI_TDFI_CTRLUPD_MAX_F0:RW:8:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001195 mmio_clrsetbits_32(PI_REG(i, 187), 0xffff << 8,
1196 (2 * pdram_timing->trefi) << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001197 /* PI_188 PI_TDFI_CTRLUPD_INTERVAL_F0:RW:0:32 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001198 mmio_clrsetbits_32(PI_REG(i, 188), 0xffffffff,
1199 20 * pdram_timing->trefi);
Caesar Wanga8456902016-10-27 01:12:34 +08001200 }
1201}
1202
1203static void gen_rk3399_pi_params_f1(struct timing_related_config *timing_config,
1204 struct dram_timing_t *pdram_timing)
1205{
1206 uint32_t tmp, tmp1, tmp2;
1207 uint32_t i;
1208
1209 for (i = 0; i < timing_config->ch_cnt; i++) {
1210 /* PI_04 PI_TDFI_PHYMSTR_MAX_F1:RW:0:32 */
1211 tmp = 4 * pdram_timing->trefi;
Caesar Wang8bc16672016-10-27 01:12:47 +08001212 mmio_write_32(PI_REG(i, 4), tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001213 /* PI_05 PI_TDFI_PHYMSTR_RESP_F1:RW:0:16 */
1214 tmp = 2 * pdram_timing->trefi;
Caesar Wang8bc16672016-10-27 01:12:47 +08001215 mmio_clrsetbits_32(PI_REG(i, 5), 0xffff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001216 /* PI_12 PI_TDFI_PHYUPD_RESP_F1:RW:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001217 mmio_clrsetbits_32(PI_REG(i, 12), 0xffff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001218
1219 /* PI_42 PI_TDELAY_RDWR_2_BUS_IDLE_F1:RW:8:8 */
1220 if (timing_config->dram_type == LPDDR4)
1221 tmp = 2;
1222 else
1223 tmp = 0;
1224 tmp = (pdram_timing->bl / 2) + 4 +
Caesar Wang8bc16672016-10-27 01:12:47 +08001225 (get_pi_rdlat_adj(pdram_timing) - 2) + tmp +
1226 get_pi_tdfi_phy_rdlat(pdram_timing, timing_config);
1227 mmio_clrsetbits_32(PI_REG(i, 42), 0xff << 8, tmp << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001228 /* PI_43 PI_WRLAT_F1:RW:24:5 */
1229 if (timing_config->dram_type == LPDDR3) {
1230 tmp = get_pi_wrlat(pdram_timing, timing_config);
Caesar Wang8bc16672016-10-27 01:12:47 +08001231 mmio_clrsetbits_32(PI_REG(i, 43), 0x1f << 24,
1232 tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001233 }
1234 /* PI_44 PI_ADDITIVE_LAT_F1:RW:0:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001235 mmio_clrsetbits_32(PI_REG(i, 44), 0x3f, PI_ADD_LATENCY);
Caesar Wanga8456902016-10-27 01:12:34 +08001236 /* PI_44 PI_CASLAT_LIN_F1:RW:8:7:=0x18 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001237 mmio_clrsetbits_32(PI_REG(i, 44), 0x7f << 8,
Derek Basehoreb07dfeb2017-02-09 22:02:42 -08001238 (pdram_timing->cl * 2) << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001239 /* PI_47 PI_TREF_F1:RW:16:16 */
Justin Chadwell2b558a62019-07-03 14:11:28 +01001240 mmio_clrsetbits_32(PI_REG(i, 47), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +08001241 pdram_timing->trefi << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001242 /* PI_47 PI_TRFC_F1:RW:0:10 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001243 mmio_clrsetbits_32(PI_REG(i, 47), 0x3ff, pdram_timing->trfc);
Caesar Wanga8456902016-10-27 01:12:34 +08001244 /* PI_67 PI_TODTL_2CMD_F1:RW:8:8 */
1245 if (timing_config->dram_type == LPDDR3) {
1246 tmp = get_pi_todtoff_max(pdram_timing, timing_config);
Caesar Wang8bc16672016-10-27 01:12:47 +08001247 mmio_clrsetbits_32(PI_REG(i, 67), 0xff << 8, tmp << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001248 }
1249 /* PI_72 PI_WR_TO_ODTH_F1:RW:24:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001250 if ((timing_config->dram_type == LPDDR3) ||
1251 (timing_config->dram_type == LPDDR4)) {
Caesar Wanga8456902016-10-27 01:12:34 +08001252 tmp1 = get_pi_wrlat(pdram_timing, timing_config);
1253 tmp2 = get_pi_todtoff_max(pdram_timing, timing_config);
1254 if (tmp1 > tmp2)
1255 tmp = tmp1 - tmp2;
1256 else
1257 tmp = 0;
1258 } else if (timing_config->dram_type == DDR3) {
1259 tmp = 0;
1260 }
Caesar Wang8bc16672016-10-27 01:12:47 +08001261 mmio_clrsetbits_32(PI_REG(i, 72), 0x3f << 24, tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001262 /* PI_73 PI_RD_TO_ODTH_F1:RW:16:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001263 if ((timing_config->dram_type == LPDDR3) ||
1264 (timing_config->dram_type == LPDDR4)) {
1265 /* min_rl_preamble = cl + TDQSCK_MIN - 1 */
1266 tmp1 = pdram_timing->cl +
1267 get_pi_todtoff_min(pdram_timing, timing_config);
1268 tmp1--;
Caesar Wanga8456902016-10-27 01:12:34 +08001269 /* todtoff_max */
1270 tmp2 = get_pi_todtoff_max(pdram_timing, timing_config);
1271 if (tmp1 > tmp2)
1272 tmp = tmp1 - tmp2;
1273 else
1274 tmp = 0;
Caesar Wang8bc16672016-10-27 01:12:47 +08001275 } else if (timing_config->dram_type == DDR3)
Caesar Wanga8456902016-10-27 01:12:34 +08001276 tmp = pdram_timing->cl - pdram_timing->cwl;
Caesar Wang8bc16672016-10-27 01:12:47 +08001277
1278 mmio_clrsetbits_32(PI_REG(i, 73), 0x3f << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001279 /*P I_89 PI_RDLAT_ADJ_F1:RW:24:8 */
1280 tmp = get_pi_rdlat_adj(pdram_timing);
Justin Chadwell2b558a62019-07-03 14:11:28 +01001281 mmio_clrsetbits_32(PI_REG(i, 89), 0xffu << 24, tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001282 /* PI_90 PI_WRLAT_ADJ_F1:RW:24:8 */
1283 tmp = get_pi_wrlat_adj(pdram_timing, timing_config);
Justin Chadwell2b558a62019-07-03 14:11:28 +01001284 mmio_clrsetbits_32(PI_REG(i, 90), 0xffu << 24, tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001285 /* PI_91 PI_TDFI_WRCSLAT_F1:RW:24:8 */
1286 tmp1 = tmp;
Caesar Wang8bc16672016-10-27 01:12:47 +08001287 if (tmp1 == 0)
1288 tmp = 0;
1289 else if (tmp1 < 5)
1290 tmp = tmp1 - 1;
1291 else
Caesar Wanga8456902016-10-27 01:12:34 +08001292 tmp = tmp1 - 5;
Justin Chadwell2b558a62019-07-03 14:11:28 +01001293 mmio_clrsetbits_32(PI_REG(i, 91), 0xffu << 24, tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001294 /*PI_96 PI_TDFI_CALVL_CAPTURE_F1:RW:16:10 */
1295 /* tadr=20ns */
1296 tmp1 = 20000 / (1000000 / pdram_timing->mhz) + 1;
1297 if ((20000 % (1000000 / pdram_timing->mhz)) != 0)
1298 tmp1++;
1299 tmp = (tmp1 >> 1) + (tmp1 % 2) + 5;
Caesar Wang8bc16672016-10-27 01:12:47 +08001300 mmio_clrsetbits_32(PI_REG(i, 96), 0x3ff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001301 /* PI_96 PI_TDFI_CALVL_CC_F1:RW:0:10 */
1302 tmp = tmp + 18;
Caesar Wang8bc16672016-10-27 01:12:47 +08001303 mmio_clrsetbits_32(PI_REG(i, 96), 0x3ff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001304 /*PI_103 PI_TMRZ_F1:RW:0:5 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001305 mmio_clrsetbits_32(PI_REG(i, 103), 0x1f, pdram_timing->tmrz);
Caesar Wanga8456902016-10-27 01:12:34 +08001306 /*PI_111 PI_TDFI_CALVL_STROBE_F1:RW:16:4 */
1307 /* tds_train=ceil(2/ns) */
1308 tmp1 = 2 * 1000 / (1000000 / pdram_timing->mhz);
1309 if ((2 * 1000 % (1000000 / pdram_timing->mhz)) != 0)
1310 tmp1++;
1311 /* pi_tdfi_calvl_strobe=tds_train+5 */
1312 tmp = tmp1 + 5;
Caesar Wang8bc16672016-10-27 01:12:47 +08001313 mmio_clrsetbits_32(PI_REG(i, 111), 0xf << 16,
1314 tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001315 /* PI_116 PI_TCKEHDQS_F1:RW:24:6 */
1316 tmp = 10000 / (1000000 / pdram_timing->mhz);
1317 if ((10000 % (1000000 / pdram_timing->mhz)) != 0)
1318 tmp++;
1319 if (pdram_timing->mhz <= 100)
1320 tmp = tmp + 1;
1321 else
1322 tmp = tmp + 8;
Caesar Wang8bc16672016-10-27 01:12:47 +08001323 mmio_clrsetbits_32(PI_REG(i, 116), 0x3f << 24,
1324 tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001325 /* PI_128 PI_MR1_DATA_F1_0:RW+:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001326 mmio_clrsetbits_32(PI_REG(i, 128), 0xffff, pdram_timing->mr[1]);
Caesar Wanga8456902016-10-27 01:12:34 +08001327 /* PI_135 PI_MR1_DATA_F1_1:RW+:8:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001328 mmio_clrsetbits_32(PI_REG(i, 135), 0xffff << 8,
1329 pdram_timing->mr[1] << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001330 /* PI_143 PI_MR1_DATA_F1_2:RW+:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001331 mmio_clrsetbits_32(PI_REG(i, 143), 0xffff, pdram_timing->mr[1]);
Caesar Wanga8456902016-10-27 01:12:34 +08001332 /* PI_150 PI_MR1_DATA_F1_3:RW+:8:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001333 mmio_clrsetbits_32(PI_REG(i, 150), 0xffff << 8,
1334 pdram_timing->mr[1] << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001335 /* PI_128 PI_MR2_DATA_F1_0:RW+:16:16 */
Justin Chadwell2b558a62019-07-03 14:11:28 +01001336 mmio_clrsetbits_32(PI_REG(i, 128), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +08001337 pdram_timing->mr[2] << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001338 /* PI_136 PI_MR2_DATA_F1_1:RW+:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001339 mmio_clrsetbits_32(PI_REG(i, 136), 0xffff, pdram_timing->mr[2]);
Caesar Wanga8456902016-10-27 01:12:34 +08001340 /* PI_143 PI_MR2_DATA_F1_2:RW+:16:16 */
Justin Chadwell2b558a62019-07-03 14:11:28 +01001341 mmio_clrsetbits_32(PI_REG(i, 143), 0xffffu << 16,
Caesar Wang8bc16672016-10-27 01:12:47 +08001342 pdram_timing->mr[2] << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001343 /* PI_151 PI_MR2_DATA_F1_3:RW+:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001344 mmio_clrsetbits_32(PI_REG(i, 151), 0xffff, pdram_timing->mr[2]);
Caesar Wanga8456902016-10-27 01:12:34 +08001345 /* PI_156 PI_TFC_F1:RW:16:10 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001346 mmio_clrsetbits_32(PI_REG(i, 156), 0x3ff << 16,
Derek Basehorebe2a8952017-02-09 22:08:48 -08001347 pdram_timing->tfc_long << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001348 /* PI_162 PI_TWR_F1:RW:8:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001349 mmio_clrsetbits_32(PI_REG(i, 162), 0x3f << 8,
1350 pdram_timing->twr << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001351 /* PI_162 PI_TWTR_F1:RW:0:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001352 mmio_clrsetbits_32(PI_REG(i, 162), 0x3f, pdram_timing->twtr);
Caesar Wanga8456902016-10-27 01:12:34 +08001353 /* PI_161 PI_TRCD_F1:RW:24:8 */
Justin Chadwell2b558a62019-07-03 14:11:28 +01001354 mmio_clrsetbits_32(PI_REG(i, 161), 0xffu << 24,
Caesar Wang8bc16672016-10-27 01:12:47 +08001355 pdram_timing->trcd << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001356 /* PI_161 PI_TRP_F1:RW:16:8 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001357 mmio_clrsetbits_32(PI_REG(i, 161), 0xff << 16,
1358 pdram_timing->trp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001359 /* PI_161 PI_TRTP_F1:RW:8:8 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001360 mmio_clrsetbits_32(PI_REG(i, 161), 0xff << 8,
1361 pdram_timing->trtp << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001362 /* PI_163 PI_TRAS_MIN_F1:RW:24:8 */
Justin Chadwell2b558a62019-07-03 14:11:28 +01001363 mmio_clrsetbits_32(PI_REG(i, 163), 0xffu << 24,
Caesar Wang8bc16672016-10-27 01:12:47 +08001364 pdram_timing->tras_min << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001365 /* PI_163 PI_TRAS_MAX_F1:RW:0:17 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001366 mmio_clrsetbits_32(PI_REG(i, 163), 0x1ffff,
1367 pdram_timing->tras_max * 99 / 100);
Caesar Wanga8456902016-10-27 01:12:34 +08001368 /* PI_164 PI_TMRD_F1:RW:16:6 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001369 mmio_clrsetbits_32(PI_REG(i, 164), 0x3f << 16,
1370 pdram_timing->tmrd << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001371 /* PI_164 PI_TDQSCK_MAX_F1:RW:0:4 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001372 mmio_clrsetbits_32(PI_REG(i, 164), 0xf,
1373 pdram_timing->tdqsck_max);
Caesar Wanga8456902016-10-27 01:12:34 +08001374 /* PI_189 PI_TDFI_CTRLUPD_MAX_F1:RW:0:16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001375 mmio_clrsetbits_32(PI_REG(i, 189), 0xffff,
1376 2 * pdram_timing->trefi);
Caesar Wanga8456902016-10-27 01:12:34 +08001377 /* PI_190 PI_TDFI_CTRLUPD_INTERVAL_F1:RW:0:32 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001378 mmio_clrsetbits_32(PI_REG(i, 190), 0xffffffff,
1379 20 * pdram_timing->trefi);
Caesar Wanga8456902016-10-27 01:12:34 +08001380 }
1381}
1382
1383static void gen_rk3399_pi_params(struct timing_related_config *timing_config,
1384 struct dram_timing_t *pdram_timing,
1385 uint32_t fn)
1386{
1387 if (fn == 0)
1388 gen_rk3399_pi_params_f0(timing_config, pdram_timing);
1389 else
1390 gen_rk3399_pi_params_f1(timing_config, pdram_timing);
Caesar Wanga8456902016-10-27 01:12:34 +08001391}
1392
1393static void gen_rk3399_set_odt(uint32_t odt_en)
1394{
1395 uint32_t drv_odt_val;
1396 uint32_t i;
1397
1398 for (i = 0; i < rk3399_dram_status.timing_config.ch_cnt; i++) {
1399 drv_odt_val = (odt_en | (0 << 1) | (0 << 2)) << 16;
Caesar Wang8bc16672016-10-27 01:12:47 +08001400 mmio_clrsetbits_32(PHY_REG(i, 5), 0x7 << 16, drv_odt_val);
1401 mmio_clrsetbits_32(PHY_REG(i, 133), 0x7 << 16, drv_odt_val);
1402 mmio_clrsetbits_32(PHY_REG(i, 261), 0x7 << 16, drv_odt_val);
1403 mmio_clrsetbits_32(PHY_REG(i, 389), 0x7 << 16, drv_odt_val);
Caesar Wanga8456902016-10-27 01:12:34 +08001404 drv_odt_val = (odt_en | (0 << 1) | (0 << 2)) << 24;
Caesar Wang8bc16672016-10-27 01:12:47 +08001405 mmio_clrsetbits_32(PHY_REG(i, 6), 0x7 << 24, drv_odt_val);
1406 mmio_clrsetbits_32(PHY_REG(i, 134), 0x7 << 24, drv_odt_val);
1407 mmio_clrsetbits_32(PHY_REG(i, 262), 0x7 << 24, drv_odt_val);
1408 mmio_clrsetbits_32(PHY_REG(i, 390), 0x7 << 24, drv_odt_val);
Caesar Wanga8456902016-10-27 01:12:34 +08001409 }
Derek Basehoreb1065122016-10-20 22:09:22 -07001410}
1411
1412static void gen_rk3399_phy_dll_bypass(uint32_t mhz, uint32_t ch,
1413 uint32_t index, uint32_t dram_type)
1414{
1415 uint32_t sw_master_mode = 0;
1416 uint32_t rddqs_gate_delay, rddqs_latency, total_delay;
1417 uint32_t i;
1418
1419 if (dram_type == DDR3)
1420 total_delay = PI_PAD_DELAY_PS_VALUE;
1421 else if (dram_type == LPDDR3)
1422 total_delay = PI_PAD_DELAY_PS_VALUE + 2500;
1423 else
1424 total_delay = PI_PAD_DELAY_PS_VALUE + 1500;
1425 /* total_delay + 0.55tck */
1426 total_delay += (55 * 10000)/mhz;
1427 rddqs_latency = total_delay * mhz / 1000000;
1428 total_delay -= rddqs_latency * 1000000 / mhz;
1429 rddqs_gate_delay = total_delay * 0x200 * mhz / 1000000;
1430 if (mhz <= PHY_DLL_BYPASS_FREQ) {
1431 sw_master_mode = 0xc;
1432 mmio_setbits_32(PHY_REG(ch, 514), 1);
1433 mmio_setbits_32(PHY_REG(ch, 642), 1);
1434 mmio_setbits_32(PHY_REG(ch, 770), 1);
1435
1436 /* setting bypass mode slave delay */
1437 for (i = 0; i < 4; i++) {
1438 /* wr dq delay = -180deg + (0x60 / 4) * 20ps */
1439 mmio_clrsetbits_32(PHY_REG(ch, 1 + 128 * i), 0x7ff << 8,
1440 0x4a0 << 8);
1441 /* rd dqs/dq delay = (0x60 / 4) * 20ps */
1442 mmio_clrsetbits_32(PHY_REG(ch, 11 + 128 * i), 0x3ff,
1443 0xa0);
1444 /* rd rddqs_gate delay */
1445 mmio_clrsetbits_32(PHY_REG(ch, 2 + 128 * i), 0x3ff,
1446 rddqs_gate_delay);
1447 mmio_clrsetbits_32(PHY_REG(ch, 78 + 128 * i), 0xf,
1448 rddqs_latency);
1449 }
1450 for (i = 0; i < 3; i++)
1451 /* adr delay */
1452 mmio_clrsetbits_32(PHY_REG(ch, 513 + 128 * i),
1453 0x7ff << 16, 0x80 << 16);
1454
1455 if ((mmio_read_32(PHY_REG(ch, 86)) & 0xc00) == 0) {
1456 /*
1457 * old status is normal mode,
1458 * and saving the wrdqs slave delay
1459 */
1460 for (i = 0; i < 4; i++) {
1461 /* save and clear wr dqs slave delay */
1462 wrdqs_delay_val[ch][index][i] = 0x3ff &
1463 (mmio_read_32(PHY_REG(ch, 63 + i * 128))
1464 >> 16);
1465 mmio_clrsetbits_32(PHY_REG(ch, 63 + i * 128),
1466 0x03ff << 16, 0 << 16);
1467 /*
1468 * in normal mode the cmd may delay 1cycle by
1469 * wrlvl and in bypass mode making dqs also
1470 * delay 1cycle.
1471 */
1472 mmio_clrsetbits_32(PHY_REG(ch, 78 + i * 128),
1473 0x07 << 8, 0x1 << 8);
1474 }
1475 }
1476 } else if (mmio_read_32(PHY_REG(ch, 86)) & 0xc00) {
1477 /* old status is bypass mode and restore wrlvl resume */
1478 for (i = 0; i < 4; i++) {
1479 mmio_clrsetbits_32(PHY_REG(ch, 63 + i * 128),
1480 0x03ff << 16,
1481 (wrdqs_delay_val[ch][index][i] &
1482 0x3ff) << 16);
1483 /* resume phy_write_path_lat_add */
1484 mmio_clrbits_32(PHY_REG(ch, 78 + i * 128), 0x07 << 8);
1485 }
1486 }
1487
1488 /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
1489 mmio_clrsetbits_32(PHY_REG(ch, 86), 0xf << 8, sw_master_mode << 8);
1490 mmio_clrsetbits_32(PHY_REG(ch, 214), 0xf << 8, sw_master_mode << 8);
1491 mmio_clrsetbits_32(PHY_REG(ch, 342), 0xf << 8, sw_master_mode << 8);
1492 mmio_clrsetbits_32(PHY_REG(ch, 470), 0xf << 8, sw_master_mode << 8);
1493
1494 /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
1495 mmio_clrsetbits_32(PHY_REG(ch, 547), 0xf << 16, sw_master_mode << 16);
1496 mmio_clrsetbits_32(PHY_REG(ch, 675), 0xf << 16, sw_master_mode << 16);
1497 mmio_clrsetbits_32(PHY_REG(ch, 803), 0xf << 16, sw_master_mode << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001498}
1499
Caesar Wanga8456902016-10-27 01:12:34 +08001500static void gen_rk3399_phy_params(struct timing_related_config *timing_config,
1501 struct drv_odt_lp_config *drv_config,
1502 struct dram_timing_t *pdram_timing,
1503 uint32_t fn)
1504{
1505 uint32_t tmp, i, div, j;
1506 uint32_t mem_delay_ps, pad_delay_ps, total_delay_ps, delay_frac_ps;
1507 uint32_t trpre_min_ps, gate_delay_ps, gate_delay_frac_ps;
1508 uint32_t ie_enable, tsel_enable, cas_lat, rddata_en_ie_dly, tsel_adder;
1509 uint32_t extra_adder, delta, hs_offset;
1510
1511 for (i = 0; i < timing_config->ch_cnt; i++) {
1512
1513 pad_delay_ps = PI_PAD_DELAY_PS_VALUE;
1514 ie_enable = PI_IE_ENABLE_VALUE;
1515 tsel_enable = PI_TSEL_ENABLE_VALUE;
1516
Caesar Wang8bc16672016-10-27 01:12:47 +08001517 mmio_clrsetbits_32(PHY_REG(i, 896), (0x3 << 8) | 1, fn << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001518
1519 /* PHY_LOW_FREQ_SEL */
1520 /* DENALI_PHY_913 1bit offset_0 */
1521 if (timing_config->freq > 400)
Caesar Wang8bc16672016-10-27 01:12:47 +08001522 mmio_clrbits_32(PHY_REG(i, 913), 1);
Caesar Wanga8456902016-10-27 01:12:34 +08001523 else
Caesar Wang8bc16672016-10-27 01:12:47 +08001524 mmio_setbits_32(PHY_REG(i, 913), 1);
Caesar Wanga8456902016-10-27 01:12:34 +08001525
1526 /* PHY_RPTR_UPDATE_x */
1527 /* DENALI_PHY_87/215/343/471 4bit offset_16 */
1528 tmp = 2500 / (1000000 / pdram_timing->mhz) + 3;
1529 if ((2500 % (1000000 / pdram_timing->mhz)) != 0)
1530 tmp++;
Caesar Wang8bc16672016-10-27 01:12:47 +08001531 mmio_clrsetbits_32(PHY_REG(i, 87), 0xf << 16, tmp << 16);
1532 mmio_clrsetbits_32(PHY_REG(i, 215), 0xf << 16, tmp << 16);
1533 mmio_clrsetbits_32(PHY_REG(i, 343), 0xf << 16, tmp << 16);
1534 mmio_clrsetbits_32(PHY_REG(i, 471), 0xf << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001535
1536 /* PHY_PLL_CTRL */
1537 /* DENALI_PHY_911 13bits offset_0 */
1538 /* PHY_LP4_BOOT_PLL_CTRL */
1539 /* DENALI_PHY_919 13bits offset_0 */
Lin Huang52512c22016-12-15 15:08:47 +08001540 tmp = (1 << 12) | (2 << 7) | (1 << 1);
Caesar Wang8bc16672016-10-27 01:12:47 +08001541 mmio_clrsetbits_32(PHY_REG(i, 911), 0x1fff, tmp);
1542 mmio_clrsetbits_32(PHY_REG(i, 919), 0x1fff, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001543
1544 /* PHY_PLL_CTRL_CA */
1545 /* DENALI_PHY_911 13bits offset_16 */
1546 /* PHY_LP4_BOOT_PLL_CTRL_CA */
1547 /* DENALI_PHY_919 13bits offset_16 */
Lin Huang52512c22016-12-15 15:08:47 +08001548 tmp = (2 << 7) | (1 << 5) | (1 << 1);
Caesar Wang8bc16672016-10-27 01:12:47 +08001549 mmio_clrsetbits_32(PHY_REG(i, 911), 0x1fff << 16, tmp << 16);
1550 mmio_clrsetbits_32(PHY_REG(i, 919), 0x1fff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001551
1552 /* PHY_TCKSRE_WAIT */
1553 /* DENALI_PHY_922 4bits offset_24 */
1554 if (pdram_timing->mhz <= 400)
1555 tmp = 1;
1556 else if (pdram_timing->mhz <= 800)
1557 tmp = 3;
1558 else if (pdram_timing->mhz <= 1000)
1559 tmp = 4;
1560 else
1561 tmp = 5;
Caesar Wang8bc16672016-10-27 01:12:47 +08001562 mmio_clrsetbits_32(PHY_REG(i, 922), 0xf << 24, tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001563 /* PHY_CAL_CLK_SELECT_0:RW8:3 */
1564 div = pdram_timing->mhz / (2 * 20);
1565 for (j = 2, tmp = 1; j <= 128; j <<= 1, tmp++) {
1566 if (div < j)
1567 break;
1568 }
Caesar Wang8bc16672016-10-27 01:12:47 +08001569 mmio_clrsetbits_32(PHY_REG(i, 947), 0x7 << 8, tmp << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001570
1571 if (timing_config->dram_type == DDR3) {
1572 mem_delay_ps = 0;
1573 trpre_min_ps = 1000;
1574 } else if (timing_config->dram_type == LPDDR4) {
1575 mem_delay_ps = 1500;
1576 trpre_min_ps = 900;
1577 } else if (timing_config->dram_type == LPDDR3) {
1578 mem_delay_ps = 2500;
1579 trpre_min_ps = 900;
1580 } else {
1581 ERROR("gen_rk3399_phy_params:dramtype unsupport\n");
1582 return;
1583 }
1584 total_delay_ps = mem_delay_ps + pad_delay_ps;
Caesar Wang8bc16672016-10-27 01:12:47 +08001585 delay_frac_ps = 1000 * total_delay_ps /
1586 (1000000 / pdram_timing->mhz);
Caesar Wanga8456902016-10-27 01:12:34 +08001587 gate_delay_ps = delay_frac_ps + 1000 - (trpre_min_ps / 2);
Caesar Wang8bc16672016-10-27 01:12:47 +08001588 gate_delay_frac_ps = gate_delay_ps % 1000;
Caesar Wanga8456902016-10-27 01:12:34 +08001589 tmp = gate_delay_frac_ps * 0x200 / 1000;
Caesar Wanga8456902016-10-27 01:12:34 +08001590 /* PHY_RDDQS_GATE_SLAVE_DELAY */
1591 /* DENALI_PHY_77/205/333/461 10bits offset_16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001592 mmio_clrsetbits_32(PHY_REG(i, 77), 0x2ff << 16, tmp << 16);
1593 mmio_clrsetbits_32(PHY_REG(i, 205), 0x2ff << 16, tmp << 16);
1594 mmio_clrsetbits_32(PHY_REG(i, 333), 0x2ff << 16, tmp << 16);
1595 mmio_clrsetbits_32(PHY_REG(i, 461), 0x2ff << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001596
1597 tmp = gate_delay_ps / 1000;
1598 /* PHY_LP4_BOOT_RDDQS_LATENCY_ADJUST */
1599 /* DENALI_PHY_10/138/266/394 4bit offset_0 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001600 mmio_clrsetbits_32(PHY_REG(i, 10), 0xf, tmp);
1601 mmio_clrsetbits_32(PHY_REG(i, 138), 0xf, tmp);
1602 mmio_clrsetbits_32(PHY_REG(i, 266), 0xf, tmp);
1603 mmio_clrsetbits_32(PHY_REG(i, 394), 0xf, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001604 /* PHY_GTLVL_LAT_ADJ_START */
1605 /* DENALI_PHY_80/208/336/464 4bits offset_16 */
Lin Huange22d31a2017-02-22 18:24:55 +08001606 tmp = rddqs_delay_ps / (1000000 / pdram_timing->mhz) + 2;
Caesar Wang8bc16672016-10-27 01:12:47 +08001607 mmio_clrsetbits_32(PHY_REG(i, 80), 0xf << 16, tmp << 16);
1608 mmio_clrsetbits_32(PHY_REG(i, 208), 0xf << 16, tmp << 16);
1609 mmio_clrsetbits_32(PHY_REG(i, 336), 0xf << 16, tmp << 16);
1610 mmio_clrsetbits_32(PHY_REG(i, 464), 0xf << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001611
1612 cas_lat = pdram_timing->cl + PI_ADD_LATENCY;
1613 rddata_en_ie_dly = ie_enable / (1000000 / pdram_timing->mhz);
1614 if ((ie_enable % (1000000 / pdram_timing->mhz)) != 0)
1615 rddata_en_ie_dly++;
1616 rddata_en_ie_dly = rddata_en_ie_dly - 1;
1617 tsel_adder = tsel_enable / (1000000 / pdram_timing->mhz);
1618 if ((tsel_enable % (1000000 / pdram_timing->mhz)) != 0)
1619 tsel_adder++;
1620 if (rddata_en_ie_dly > tsel_adder)
1621 extra_adder = rddata_en_ie_dly - tsel_adder;
1622 else
1623 extra_adder = 0;
1624 delta = cas_lat - rddata_en_ie_dly;
1625 if (PI_REGS_DIMM_SUPPORT && PI_DOUBLEFREEK)
1626 hs_offset = 2;
1627 else
1628 hs_offset = 1;
Caesar Wang8bc16672016-10-27 01:12:47 +08001629 if (rddata_en_ie_dly > (cas_lat - 1 - hs_offset))
Caesar Wanga8456902016-10-27 01:12:34 +08001630 tmp = 0;
Caesar Wang8bc16672016-10-27 01:12:47 +08001631 else if ((delta == 2) || (delta == 1))
1632 tmp = rddata_en_ie_dly - 0 - extra_adder;
1633 else
1634 tmp = extra_adder;
Caesar Wanga8456902016-10-27 01:12:34 +08001635 /* PHY_LP4_BOOT_RDDATA_EN_TSEL_DLY */
1636 /* DENALI_PHY_9/137/265/393 4bit offset_16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001637 mmio_clrsetbits_32(PHY_REG(i, 9), 0xf << 16, tmp << 16);
1638 mmio_clrsetbits_32(PHY_REG(i, 137), 0xf << 16, tmp << 16);
1639 mmio_clrsetbits_32(PHY_REG(i, 265), 0xf << 16, tmp << 16);
1640 mmio_clrsetbits_32(PHY_REG(i, 393), 0xf << 16, tmp << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001641 /* PHY_RDDATA_EN_TSEL_DLY */
1642 /* DENALI_PHY_86/214/342/470 4bit offset_0 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001643 mmio_clrsetbits_32(PHY_REG(i, 86), 0xf, tmp);
1644 mmio_clrsetbits_32(PHY_REG(i, 214), 0xf, tmp);
1645 mmio_clrsetbits_32(PHY_REG(i, 342), 0xf, tmp);
1646 mmio_clrsetbits_32(PHY_REG(i, 470), 0xf, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001647
1648 if (tsel_adder > rddata_en_ie_dly)
1649 extra_adder = tsel_adder - rddata_en_ie_dly;
1650 else
1651 extra_adder = 0;
1652 if (rddata_en_ie_dly > (cas_lat - 1 - hs_offset))
1653 tmp = tsel_adder;
1654 else
1655 tmp = rddata_en_ie_dly - 0 + extra_adder;
1656 /* PHY_LP4_BOOT_RDDATA_EN_DLY */
1657 /* DENALI_PHY_9/137/265/393 4bit offset_8 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001658 mmio_clrsetbits_32(PHY_REG(i, 9), 0xf << 8, tmp << 8);
1659 mmio_clrsetbits_32(PHY_REG(i, 137), 0xf << 8, tmp << 8);
1660 mmio_clrsetbits_32(PHY_REG(i, 265), 0xf << 8, tmp << 8);
1661 mmio_clrsetbits_32(PHY_REG(i, 393), 0xf << 8, tmp << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001662 /* PHY_RDDATA_EN_DLY */
1663 /* DENALI_PHY_85/213/341/469 4bit offset_24 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001664 mmio_clrsetbits_32(PHY_REG(i, 85), 0xf << 24, tmp << 24);
1665 mmio_clrsetbits_32(PHY_REG(i, 213), 0xf << 24, tmp << 24);
1666 mmio_clrsetbits_32(PHY_REG(i, 341), 0xf << 24, tmp << 24);
1667 mmio_clrsetbits_32(PHY_REG(i, 469), 0xf << 24, tmp << 24);
Caesar Wanga8456902016-10-27 01:12:34 +08001668
1669 if (pdram_timing->mhz <= ENPER_CS_TRAINING_FREQ) {
Caesar Wanga8456902016-10-27 01:12:34 +08001670 /*
1671 * Note:Per-CS Training is not compatible at speeds
1672 * under 533 MHz. If the PHY is running at a speed
1673 * less than 533MHz, all phy_per_cs_training_en_X
1674 * parameters must be cleared to 0.
1675 */
1676
1677 /*DENALI_PHY_84/212/340/468 1bit offset_16 */
Caesar Wang8bc16672016-10-27 01:12:47 +08001678 mmio_clrbits_32(PHY_REG(i, 84), 0x1 << 16);
1679 mmio_clrbits_32(PHY_REG(i, 212), 0x1 << 16);
1680 mmio_clrbits_32(PHY_REG(i, 340), 0x1 << 16);
1681 mmio_clrbits_32(PHY_REG(i, 468), 0x1 << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001682 } else {
Caesar Wang8bc16672016-10-27 01:12:47 +08001683 mmio_setbits_32(PHY_REG(i, 84), 0x1 << 16);
1684 mmio_setbits_32(PHY_REG(i, 212), 0x1 << 16);
1685 mmio_setbits_32(PHY_REG(i, 340), 0x1 << 16);
1686 mmio_setbits_32(PHY_REG(i, 468), 0x1 << 16);
Caesar Wanga8456902016-10-27 01:12:34 +08001687 }
Derek Basehoreb1065122016-10-20 22:09:22 -07001688 gen_rk3399_phy_dll_bypass(pdram_timing->mhz, i, fn,
1689 timing_config->dram_type);
Caesar Wanga8456902016-10-27 01:12:34 +08001690 }
1691}
1692
1693static int to_get_clk_index(unsigned int mhz)
1694{
1695 int pll_cnt, i;
1696
1697 pll_cnt = ARRAY_SIZE(dpll_rates_table);
1698
Elyes Haouas2be03c02023-02-13 09:14:48 +01001699 /* Assuming rate_table is in descending order */
Caesar Wanga8456902016-10-27 01:12:34 +08001700 for (i = 0; i < pll_cnt; i++) {
1701 if (mhz >= dpll_rates_table[i].mhz)
1702 break;
1703 }
1704
1705 /* if mhz lower than lowest frequency in table, use lowest frequency */
1706 if (i == pll_cnt)
1707 i = pll_cnt - 1;
1708
1709 return i;
1710}
1711
Caesar Wanga8456902016-10-27 01:12:34 +08001712uint32_t ddr_get_rate(void)
1713{
1714 uint32_t refdiv, postdiv1, fbdiv, postdiv2;
1715
1716 refdiv = mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 1)) & 0x3f;
1717 fbdiv = mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 0)) & 0xfff;
1718 postdiv1 =
1719 (mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 1)) >> 8) & 0x7;
1720 postdiv2 =
1721 (mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 1)) >> 12) & 0x7;
1722
1723 return (24 / refdiv * fbdiv / postdiv1 / postdiv2) * 1000 * 1000;
1724}
1725
1726/*
1727 * return: bit12: channel 1, external self-refresh
1728 * bit11: channel 1, stdby_mode
1729 * bit10: channel 1, self-refresh with controller and memory clock gate
1730 * bit9: channel 1, self-refresh
1731 * bit8: channel 1, power-down
1732 *
1733 * bit4: channel 1, external self-refresh
1734 * bit3: channel 0, stdby_mode
1735 * bit2: channel 0, self-refresh with controller and memory clock gate
1736 * bit1: channel 0, self-refresh
1737 * bit0: channel 0, power-down
1738 */
1739uint32_t exit_low_power(void)
1740{
Caesar Wanga8456902016-10-27 01:12:34 +08001741 uint32_t low_power = 0;
1742 uint32_t channel_mask;
Caesar Wang8bc16672016-10-27 01:12:47 +08001743 uint32_t tmp, i;
Caesar Wanga8456902016-10-27 01:12:34 +08001744
Caesar Wang8bc16672016-10-27 01:12:47 +08001745 channel_mask = (mmio_read_32(PMUGRF_BASE + PMUGRF_OSREG(2)) >> 28) &
1746 0x3;
1747 for (i = 0; i < 2; i++) {
1748 if (!(channel_mask & (1 << i)))
Caesar Wanga8456902016-10-27 01:12:34 +08001749 continue;
1750
1751 /* exit stdby mode */
Caesar Wang8bc16672016-10-27 01:12:47 +08001752 mmio_write_32(CIC_BASE + CIC_CTRL1,
1753 (1 << (i + 16)) | (0 << i));
Caesar Wanga8456902016-10-27 01:12:34 +08001754 /* exit external self-refresh */
Caesar Wang8bc16672016-10-27 01:12:47 +08001755 tmp = i ? 12 : 8;
1756 low_power |= ((mmio_read_32(PMU_BASE + PMU_SFT_CON) >> tmp) &
1757 0x1) << (4 + 8 * i);
1758 mmio_clrbits_32(PMU_BASE + PMU_SFT_CON, 1 << tmp);
1759 while (!(mmio_read_32(PMU_BASE + PMU_DDR_SREF_ST) & (1 << i)))
Caesar Wanga8456902016-10-27 01:12:34 +08001760 ;
1761 /* exit auto low-power */
Caesar Wang8bc16672016-10-27 01:12:47 +08001762 mmio_clrbits_32(CTL_REG(i, 101), 0x7);
Caesar Wanga8456902016-10-27 01:12:34 +08001763 /* lp_cmd to exit */
Caesar Wang8bc16672016-10-27 01:12:47 +08001764 if (((mmio_read_32(CTL_REG(i, 100)) >> 24) & 0x7f) !=
1765 0x40) {
1766 while (mmio_read_32(CTL_REG(i, 200)) & 0x1)
Caesar Wanga8456902016-10-27 01:12:34 +08001767 ;
Justin Chadwell2b558a62019-07-03 14:11:28 +01001768 mmio_clrsetbits_32(CTL_REG(i, 93), 0xffu << 24,
Caesar Wang8bc16672016-10-27 01:12:47 +08001769 0x69 << 24);
1770 while (((mmio_read_32(CTL_REG(i, 100)) >> 24) & 0x7f) !=
1771 0x40)
Caesar Wanga8456902016-10-27 01:12:34 +08001772 ;
1773 }
1774 }
1775 return low_power;
1776}
1777
1778void resume_low_power(uint32_t low_power)
1779{
Caesar Wanga8456902016-10-27 01:12:34 +08001780 uint32_t channel_mask;
Caesar Wang8bc16672016-10-27 01:12:47 +08001781 uint32_t tmp, i, val;
Caesar Wanga8456902016-10-27 01:12:34 +08001782
Caesar Wang8bc16672016-10-27 01:12:47 +08001783 channel_mask = (mmio_read_32(PMUGRF_BASE + PMUGRF_OSREG(2)) >> 28) &
1784 0x3;
1785 for (i = 0; i < 2; i++) {
1786 if (!(channel_mask & (1 << i)))
Caesar Wanga8456902016-10-27 01:12:34 +08001787 continue;
1788
1789 /* resume external self-refresh */
Caesar Wang8bc16672016-10-27 01:12:47 +08001790 tmp = i ? 12 : 8;
1791 val = (low_power >> (4 + 8 * i)) & 0x1;
1792 mmio_setbits_32(PMU_BASE + PMU_SFT_CON, val << tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001793 /* resume auto low-power */
Caesar Wang8bc16672016-10-27 01:12:47 +08001794 val = (low_power >> (8 * i)) & 0x7;
1795 mmio_setbits_32(CTL_REG(i, 101), val);
Caesar Wanga8456902016-10-27 01:12:34 +08001796 /* resume stdby mode */
Caesar Wang8bc16672016-10-27 01:12:47 +08001797 val = (low_power >> (3 + 8 * i)) & 0x1;
1798 mmio_write_32(CIC_BASE + CIC_CTRL1,
1799 (1 << (i + 16)) | (val << i));
Caesar Wanga8456902016-10-27 01:12:34 +08001800 }
1801}
1802
Derek Basehoreff461d02016-10-20 20:46:43 -07001803static void dram_low_power_config(void)
Caesar Wanga8456902016-10-27 01:12:34 +08001804{
Derek Basehoreff461d02016-10-20 20:46:43 -07001805 uint32_t tmp, i;
Caesar Wanga8456902016-10-27 01:12:34 +08001806 uint32_t ch_cnt = rk3399_dram_status.timing_config.ch_cnt;
1807 uint32_t dram_type = rk3399_dram_status.timing_config.dram_type;
Caesar Wanga8456902016-10-27 01:12:34 +08001808
1809 if (dram_type == DDR3)
Derek Basehoreff461d02016-10-20 20:46:43 -07001810 tmp = (2 << 16) | (0x7 << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001811 else
Derek Basehoreff461d02016-10-20 20:46:43 -07001812 tmp = (3 << 16) | (0x7 << 8);
Caesar Wanga8456902016-10-27 01:12:34 +08001813
Derek Basehoreff461d02016-10-20 20:46:43 -07001814 for (i = 0; i < ch_cnt; i++)
1815 mmio_clrsetbits_32(CTL_REG(i, 101), 0x70f0f, tmp);
Caesar Wanga8456902016-10-27 01:12:34 +08001816
1817 /* standby idle */
Caesar Wang8bc16672016-10-27 01:12:47 +08001818 mmio_write_32(CIC_BASE + CIC_CG_WAIT_TH, 0x640008);
Caesar Wanga8456902016-10-27 01:12:34 +08001819
1820 if (ch_cnt == 2) {
Caesar Wang8bc16672016-10-27 01:12:47 +08001821 mmio_write_32(GRF_BASE + GRF_DDRC1_CON1,
1822 (((0x1<<4) | (0x1<<5) | (0x1<<6) |
1823 (0x1<<7)) << 16) |
1824 ((0x1<<4) | (0x0<<5) | (0x1<<6) | (0x1<<7)));
Derek Basehoreff461d02016-10-20 20:46:43 -07001825 mmio_write_32(CIC_BASE + CIC_CTRL1, 0x002a0028);
Caesar Wanga8456902016-10-27 01:12:34 +08001826 }
1827
Caesar Wang8bc16672016-10-27 01:12:47 +08001828 mmio_write_32(GRF_BASE + GRF_DDRC0_CON1,
1829 (((0x1<<4) | (0x1<<5) | (0x1<<6) | (0x1<<7)) << 16) |
1830 ((0x1<<4) | (0x0<<5) | (0x1<<6) | (0x1<<7)));
Derek Basehoreff461d02016-10-20 20:46:43 -07001831 mmio_write_32(CIC_BASE + CIC_CTRL1, 0x00150014);
Caesar Wanga8456902016-10-27 01:12:34 +08001832}
1833
Derek Basehoreff461d02016-10-20 20:46:43 -07001834void dram_dfs_init(void)
Caesar Wanga8456902016-10-27 01:12:34 +08001835{
Derek Basehoree13bc542017-02-24 14:31:36 +08001836 uint32_t trefi0, trefi1, boot_freq;
Lin Huange22d31a2017-02-22 18:24:55 +08001837 uint32_t rddqs_adjust, rddqs_slave;
Caesar Wanga8456902016-10-27 01:12:34 +08001838
1839 /* get sdram config for os reg */
Derek Basehoreff461d02016-10-20 20:46:43 -07001840 get_dram_drv_odt_val(sdram_config.dramtype,
1841 &rk3399_dram_status.drv_odt_lp_cfg);
Caesar Wanga8456902016-10-27 01:12:34 +08001842 sdram_timing_cfg_init(&rk3399_dram_status.timing_config,
1843 &sdram_config,
1844 &rk3399_dram_status.drv_odt_lp_cfg);
1845
Caesar Wang8bc16672016-10-27 01:12:47 +08001846 trefi0 = ((mmio_read_32(CTL_REG(0, 48)) >> 16) & 0xffff) + 8;
1847 trefi1 = ((mmio_read_32(CTL_REG(0, 49)) >> 16) & 0xffff) + 8;
Caesar Wanga8456902016-10-27 01:12:34 +08001848
1849 rk3399_dram_status.index_freq[0] = trefi0 * 10 / 39;
1850 rk3399_dram_status.index_freq[1] = trefi1 * 10 / 39;
1851 rk3399_dram_status.current_index =
Caesar Wang8bc16672016-10-27 01:12:47 +08001852 (mmio_read_32(CTL_REG(0, 111)) >> 16) & 0x3;
Caesar Wanga8456902016-10-27 01:12:34 +08001853 if (rk3399_dram_status.timing_config.dram_type == DDR3) {
1854 rk3399_dram_status.index_freq[0] /= 2;
1855 rk3399_dram_status.index_freq[1] /= 2;
1856 }
Derek Basehoree13bc542017-02-24 14:31:36 +08001857 boot_freq =
1858 rk3399_dram_status.index_freq[rk3399_dram_status.current_index];
1859 boot_freq = dpll_rates_table[to_get_clk_index(boot_freq)].mhz;
1860 rk3399_dram_status.boot_freq = boot_freq;
1861 rk3399_dram_status.index_freq[rk3399_dram_status.current_index] =
1862 boot_freq;
1863 rk3399_dram_status.index_freq[(rk3399_dram_status.current_index + 1) &
1864 0x1] = 0;
1865 rk3399_dram_status.low_power_stat = 0;
Xing Zheng93280b72016-10-26 21:25:26 +08001866 /*
1867 * following register decide if NOC stall the access request
1868 * or return error when NOC being idled. when doing ddr frequency
1869 * scaling in M0 or DCF, we need to make sure noc stall the access
1870 * request, if return error cpu may data abort when ddr frequency
1871 * changing. it don't need to set this register every times,
1872 * so we init this register in function dram_dfs_init().
1873 */
1874 mmio_write_32(GRF_BASE + GRF_SOC_CON(0), 0xffffffff);
1875 mmio_write_32(GRF_BASE + GRF_SOC_CON(1), 0xffffffff);
1876 mmio_write_32(GRF_BASE + GRF_SOC_CON(2), 0xffffffff);
1877 mmio_write_32(GRF_BASE + GRF_SOC_CON(3), 0xffffffff);
1878 mmio_write_32(GRF_BASE + GRF_SOC_CON(4), 0x70007000);
1879
Derek Basehoree13bc542017-02-24 14:31:36 +08001880 /* Disable multicast */
1881 mmio_clrbits_32(PHY_REG(0, 896), 1);
1882 mmio_clrbits_32(PHY_REG(1, 896), 1);
Derek Basehoreff461d02016-10-20 20:46:43 -07001883 dram_low_power_config();
Lin Huange22d31a2017-02-22 18:24:55 +08001884
1885 /*
1886 * If boot_freq isn't in the bypass mode, it can get the
1887 * rddqs_delay_ps from the result of gate training
1888 */
1889 if (((mmio_read_32(PHY_REG(0, 86)) >> 8) & 0xf) != 0xc) {
1890
1891 /*
1892 * Select PHY's frequency set to current_index
1893 * index for get the result of gate Training
1894 * from registers
1895 */
1896 mmio_clrsetbits_32(PHY_REG(0, 896), 0x3 << 8,
1897 rk3399_dram_status.current_index << 8);
1898 rddqs_slave = (mmio_read_32(PHY_REG(0, 77)) >> 16) & 0x3ff;
1899 rddqs_slave = rddqs_slave * 1000000 / boot_freq / 512;
1900
1901 rddqs_adjust = mmio_read_32(PHY_REG(0, 78)) & 0xf;
1902 rddqs_adjust = rddqs_adjust * 1000000 / boot_freq;
1903 rddqs_delay_ps = rddqs_slave + rddqs_adjust -
1904 (1000000 / boot_freq / 2);
1905 } else {
1906 rddqs_delay_ps = 3500;
1907 }
Derek Basehoreff461d02016-10-20 20:46:43 -07001908}
Caesar Wanga8456902016-10-27 01:12:34 +08001909
Derek Basehoreff461d02016-10-20 20:46:43 -07001910/*
1911 * arg0: bit0-7: sr_idle; bit8-15:sr_mc_gate_idle; bit16-31: standby idle
1912 * arg1: bit0-11: pd_idle; bit 16-27: srpd_lite_idle
1913 * arg2: bit0: if odt en
1914 */
1915uint32_t dram_set_odt_pd(uint32_t arg0, uint32_t arg1, uint32_t arg2)
1916{
1917 struct drv_odt_lp_config *lp_cfg = &rk3399_dram_status.drv_odt_lp_cfg;
1918 uint32_t *low_power = &rk3399_dram_status.low_power_stat;
1919 uint32_t dram_type, ch_count, pd_tmp, sr_tmp, i;
1920
1921 dram_type = rk3399_dram_status.timing_config.dram_type;
1922 ch_count = rk3399_dram_status.timing_config.ch_cnt;
1923
1924 lp_cfg->sr_idle = arg0 & 0xff;
1925 lp_cfg->sr_mc_gate_idle = (arg0 >> 8) & 0xff;
1926 lp_cfg->standby_idle = (arg0 >> 16) & 0xffff;
1927 lp_cfg->pd_idle = arg1 & 0xfff;
1928 lp_cfg->srpd_lite_idle = (arg1 >> 16) & 0xfff;
1929
1930 rk3399_dram_status.timing_config.odt = arg2 & 0x1;
1931
1932 exit_low_power();
1933
1934 *low_power = 0;
Caesar Wanga8456902016-10-27 01:12:34 +08001935
Derek Basehoreff461d02016-10-20 20:46:43 -07001936 /* pd_idle en */
1937 if (lp_cfg->pd_idle)
1938 *low_power |= ((1 << 0) | (1 << 8));
1939 /* sr_idle en srpd_lite_idle */
1940 if (lp_cfg->sr_idle | lp_cfg->srpd_lite_idle)
1941 *low_power |= ((1 << 1) | (1 << 9));
1942 /* sr_mc_gate_idle */
1943 if (lp_cfg->sr_mc_gate_idle)
1944 *low_power |= ((1 << 2) | (1 << 10));
1945 /* standbyidle */
1946 if (lp_cfg->standby_idle) {
1947 if (rk3399_dram_status.timing_config.ch_cnt == 2)
1948 *low_power |= ((1 << 3) | (1 << 11));
1949 else
1950 *low_power |= (1 << 3);
Caesar Wanga8456902016-10-27 01:12:34 +08001951 }
1952
Derek Basehoreff461d02016-10-20 20:46:43 -07001953 pd_tmp = arg1;
1954 if (dram_type != LPDDR4)
1955 pd_tmp = arg1 & 0xfff;
1956 sr_tmp = arg0 & 0xffff;
1957 for (i = 0; i < ch_count; i++) {
1958 mmio_write_32(CTL_REG(i, 102), pd_tmp);
1959 mmio_clrsetbits_32(CTL_REG(i, 103), 0xffff, sr_tmp);
1960 }
1961 mmio_write_32(CIC_BASE + CIC_IDLE_TH, (arg0 >> 16) & 0xffff);
1962
1963 return 0;
Caesar Wanga8456902016-10-27 01:12:34 +08001964}
1965
Xing Zheng93280b72016-10-26 21:25:26 +08001966static void m0_configure_ddr(struct pll_div pll_div, uint32_t ddr_index)
1967{
Xing Zheng93280b72016-10-26 21:25:26 +08001968 mmio_write_32(M0_PARAM_ADDR + PARAM_DPLL_CON0, FBDIV(pll_div.fbdiv));
1969 mmio_write_32(M0_PARAM_ADDR + PARAM_DPLL_CON1,
1970 POSTDIV2(pll_div.postdiv2) | POSTDIV1(pll_div.postdiv1) |
1971 REFDIV(pll_div.refdiv));
1972
1973 mmio_write_32(M0_PARAM_ADDR + PARAM_DRAM_FREQ, pll_div.mhz);
1974
1975 mmio_write_32(M0_PARAM_ADDR + PARAM_FREQ_SELECT, ddr_index << 4);
Lin Huangb4a76762016-12-12 15:18:08 +08001976 dmbst();
Lin Huang00960ba2018-04-20 15:55:21 +08001977 m0_configure_execute_addr(M0_BINCODE_BASE);
Xing Zheng93280b72016-10-26 21:25:26 +08001978}
1979
Caesar Wanga8456902016-10-27 01:12:34 +08001980static uint32_t prepare_ddr_timing(uint32_t mhz)
1981{
1982 uint32_t index;
1983 struct dram_timing_t dram_timing;
1984
1985 rk3399_dram_status.timing_config.freq = mhz;
1986
Derek Basehoreff461d02016-10-20 20:46:43 -07001987 if (mhz < 300)
Caesar Wanga8456902016-10-27 01:12:34 +08001988 rk3399_dram_status.timing_config.dllbp = 1;
1989 else
1990 rk3399_dram_status.timing_config.dllbp = 0;
Derek Basehoreff461d02016-10-20 20:46:43 -07001991
1992 if (rk3399_dram_status.timing_config.odt == 1)
Caesar Wanga8456902016-10-27 01:12:34 +08001993 gen_rk3399_set_odt(1);
Caesar Wanga8456902016-10-27 01:12:34 +08001994
1995 index = (rk3399_dram_status.current_index + 1) & 0x1;
Caesar Wanga8456902016-10-27 01:12:34 +08001996
1997 /*
1998 * checking if having available gate traiing timing for
1999 * target freq.
2000 */
2001 dram_get_parameter(&rk3399_dram_status.timing_config, &dram_timing);
2002 gen_rk3399_ctl_params(&rk3399_dram_status.timing_config,
2003 &dram_timing, index);
2004 gen_rk3399_pi_params(&rk3399_dram_status.timing_config,
2005 &dram_timing, index);
2006 gen_rk3399_phy_params(&rk3399_dram_status.timing_config,
2007 &rk3399_dram_status.drv_odt_lp_cfg,
2008 &dram_timing, index);
2009 rk3399_dram_status.index_freq[index] = mhz;
2010
Caesar Wanga8456902016-10-27 01:12:34 +08002011 return index;
2012}
2013
Caesar Wanga8456902016-10-27 01:12:34 +08002014uint32_t ddr_set_rate(uint32_t hz)
2015{
Xing Zheng93280b72016-10-26 21:25:26 +08002016 uint32_t low_power, index, ddr_index;
Caesar Wanga8456902016-10-27 01:12:34 +08002017 uint32_t mhz = hz / (1000 * 1000);
2018
2019 if (mhz ==
2020 rk3399_dram_status.index_freq[rk3399_dram_status.current_index])
Xing Zheng035d6d62017-02-09 14:51:38 +08002021 return mhz;
Caesar Wanga8456902016-10-27 01:12:34 +08002022
2023 index = to_get_clk_index(mhz);
2024 mhz = dpll_rates_table[index].mhz;
2025
Xing Zheng93280b72016-10-26 21:25:26 +08002026 ddr_index = prepare_ddr_timing(mhz);
Derek Basehoree13bc542017-02-24 14:31:36 +08002027 gen_rk3399_enable_training(rk3399_dram_status.timing_config.ch_cnt,
2028 mhz);
Xing Zheng93280b72016-10-26 21:25:26 +08002029 if (ddr_index > 1)
Caesar Wanga8456902016-10-27 01:12:34 +08002030 goto out;
2031
Lin Huangb4a76762016-12-12 15:18:08 +08002032 /*
2033 * Make sure the clock is enabled. The M0 clocks should be on all of the
2034 * time during S0.
2035 */
Xing Zheng93280b72016-10-26 21:25:26 +08002036 m0_configure_ddr(dpll_rates_table[index], ddr_index);
2037 m0_start();
2038 m0_wait_done();
2039 m0_stop();
2040
Caesar Wanga8456902016-10-27 01:12:34 +08002041 if (rk3399_dram_status.timing_config.odt == 0)
2042 gen_rk3399_set_odt(0);
2043
Xing Zheng93280b72016-10-26 21:25:26 +08002044 rk3399_dram_status.current_index = ddr_index;
Derek Basehoreff461d02016-10-20 20:46:43 -07002045 low_power = rk3399_dram_status.low_power_stat;
Caesar Wanga8456902016-10-27 01:12:34 +08002046 resume_low_power(low_power);
2047out:
Xing Zheng035d6d62017-02-09 14:51:38 +08002048 gen_rk3399_disable_training(rk3399_dram_status.timing_config.ch_cnt);
Caesar Wanga8456902016-10-27 01:12:34 +08002049 return mhz;
2050}
2051
2052uint32_t ddr_round_rate(uint32_t hz)
2053{
2054 int index;
2055 uint32_t mhz = hz / (1000 * 1000);
2056
2057 index = to_get_clk_index(mhz);
2058
2059 return dpll_rates_table[index].mhz * 1000 * 1000;
2060}
Derek Basehoree13bc542017-02-24 14:31:36 +08002061
2062void ddr_prepare_for_sys_suspend(void)
2063{
2064 uint32_t mhz =
2065 rk3399_dram_status.index_freq[rk3399_dram_status.current_index];
2066
2067 /*
2068 * If we're not currently at the boot (assumed highest) frequency, we
2069 * need to change frequencies to configure out current index.
2070 */
2071 rk3399_suspend_status.freq = mhz;
2072 exit_low_power();
2073 rk3399_suspend_status.low_power_stat =
2074 rk3399_dram_status.low_power_stat;
2075 rk3399_suspend_status.odt = rk3399_dram_status.timing_config.odt;
2076 rk3399_dram_status.low_power_stat = 0;
2077 rk3399_dram_status.timing_config.odt = 1;
2078 if (mhz != rk3399_dram_status.boot_freq)
2079 ddr_set_rate(rk3399_dram_status.boot_freq * 1000 * 1000);
2080
2081 /*
2082 * This will configure the other index to be the same frequency as the
2083 * current one. We retrain both indices on resume, so both have to be
2084 * setup for the same frequency.
2085 */
2086 prepare_ddr_timing(rk3399_dram_status.boot_freq);
2087}
2088
2089void ddr_prepare_for_sys_resume(void)
2090{
2091 /* Disable multicast */
2092 mmio_clrbits_32(PHY_REG(0, 896), 1);
2093 mmio_clrbits_32(PHY_REG(1, 896), 1);
2094
2095 /* The suspend code changes the current index, so reset it now. */
2096 rk3399_dram_status.current_index =
2097 (mmio_read_32(CTL_REG(0, 111)) >> 16) & 0x3;
2098 rk3399_dram_status.low_power_stat =
2099 rk3399_suspend_status.low_power_stat;
2100 rk3399_dram_status.timing_config.odt = rk3399_suspend_status.odt;
2101
2102 /*
2103 * Set the saved frequency from suspend if it's different than the
2104 * current frequency.
2105 */
2106 if (rk3399_suspend_status.freq !=
2107 rk3399_dram_status.index_freq[rk3399_dram_status.current_index]) {
2108 ddr_set_rate(rk3399_suspend_status.freq * 1000 * 1000);
2109 return;
2110 }
2111
2112 gen_rk3399_set_odt(rk3399_dram_status.timing_config.odt);
2113 resume_low_power(rk3399_dram_status.low_power_stat);
2114}