dfs.c 68.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of ARM nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

31
#include <arch_helpers.h>
32
33
#include <debug.h>
#include <mmio.h>
34
#include <m0_ctl.h>
35
36
37
38
39
40
41
42
43
44
#include <plat_private.h>
#include "dfs.h"
#include "dram.h"
#include "dram_spec_timing.h"
#include "string.h"
#include "soc.h"
#include "pmu.h"

#include <delay_timer.h>

45
46
#define ENPER_CS_TRAINING_FREQ	(666)
#define TDFI_LAT_THRESHOLD_FREQ	(928)
47
#define PHY_DLL_BYPASS_FREQ	(260)
48
49
50
51

static const struct pll_div dpll_rates_table[] = {

	/* _mhz, _refdiv, _fbdiv, _postdiv1, _postdiv2 */
52
	{.mhz = 928, .refdiv = 1, .fbdiv = 116, .postdiv1 = 3, .postdiv2 = 1},
53
54
55
56
57
58
59
60
61
62
63
64
65
	{.mhz = 800, .refdiv = 1, .fbdiv = 100, .postdiv1 = 3, .postdiv2 = 1},
	{.mhz = 732, .refdiv = 1, .fbdiv = 61, .postdiv1 = 2, .postdiv2 = 1},
	{.mhz = 666, .refdiv = 1, .fbdiv = 111, .postdiv1 = 4, .postdiv2 = 1},
	{.mhz = 600, .refdiv = 1, .fbdiv = 50, .postdiv1 = 2, .postdiv2 = 1},
	{.mhz = 528, .refdiv = 1, .fbdiv = 66, .postdiv1 = 3, .postdiv2 = 1},
	{.mhz = 400, .refdiv = 1, .fbdiv = 50, .postdiv1 = 3, .postdiv2 = 1},
	{.mhz = 300, .refdiv = 1, .fbdiv = 50, .postdiv1 = 4, .postdiv2 = 1},
	{.mhz = 200, .refdiv = 1, .fbdiv = 50, .postdiv1 = 3, .postdiv2 = 2},
};

struct rk3399_dram_status {
	uint32_t current_index;
	uint32_t index_freq[2];
66
	uint32_t boot_freq;
67
68
69
70
71
	uint32_t low_power_stat;
	struct timing_related_config timing_config;
	struct drv_odt_lp_config drv_odt_lp_cfg;
};

72
73
74
75
76
77
struct rk3399_saved_status {
	uint32_t freq;
	uint32_t low_power_stat;
	uint32_t odt;
};

78
static struct rk3399_dram_status rk3399_dram_status;
79
static struct rk3399_saved_status rk3399_suspend_status;
80
static uint32_t wrdqs_delay_val[2][2][4];
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

static struct rk3399_sdram_default_config ddr3_default_config = {
	.bl = 8,
	.ap = 0,
	.burst_ref_cnt = 1,
	.zqcsi = 0
};

static struct rk3399_sdram_default_config lpddr3_default_config = {
	.bl = 8,
	.ap = 0,
	.burst_ref_cnt = 1,
	.zqcsi = 0
};

static struct rk3399_sdram_default_config lpddr4_default_config = {
	.bl = 16,
	.ap = 0,
	.caodt = 240,
	.burst_ref_cnt = 1,
	.zqcsi = 0
};

static uint32_t get_cs_die_capability(struct rk3399_sdram_params *sdram_config,
		uint8_t channel, uint8_t cs)
{
	struct rk3399_sdram_channel *ch = &sdram_config->ch[channel];
	uint32_t bandwidth;
	uint32_t die_bandwidth;
	uint32_t die;
	uint32_t cs_cap;
	uint32_t row;

	row = cs == 0 ? ch->cs0_row : ch->cs1_row;
	bandwidth = 8 * (1 << ch->bw);
	die_bandwidth = 8 * (1 << ch->dbw);
	die = bandwidth / die_bandwidth;
	cs_cap = (1 << (row + ((1 << ch->bk) / 4 + 1) + ch->col +
		  (bandwidth / 16)));
	if (ch->row_3_4)
		cs_cap = cs_cap * 3 / 4;

	return (cs_cap / die);
}

126
static void get_dram_drv_odt_val(uint32_t dram_type,
127
128
				struct drv_odt_lp_config *drv_config)
{
129
130
	uint32_t tmp;
	uint32_t mr1_val, mr3_val, mr11_val;
131
132
133

	switch (dram_type) {
	case DDR3:
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
		mr1_val = (mmio_read_32(CTL_REG(0, 133)) >> 16) & 0xffff;
		tmp = ((mr1_val >> 1) & 1) | ((mr1_val >> 4) & 1);
		if (tmp)
			drv_config->dram_side_drv = 34;
		else
			drv_config->dram_side_drv = 40;
		tmp = ((mr1_val >> 2) & 1) | ((mr1_val >> 5) & 1) |
		      ((mr1_val >> 7) & 1);
		if (tmp == 0)
			drv_config->dram_side_dq_odt = 0;
		else if (tmp == 1)
			drv_config->dram_side_dq_odt = 60;
		else if (tmp == 3)
			drv_config->dram_side_dq_odt = 40;
		else
			drv_config->dram_side_dq_odt = 120;
150
151
		break;
	case LPDDR3:
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
		mr3_val = mmio_read_32(CTL_REG(0, 138)) & 0xf;
		mr11_val = (mmio_read_32(CTL_REG(0, 139)) >> 24) & 0x3;
		if (mr3_val == 0xb)
			drv_config->dram_side_drv = 3448;
		else if (mr3_val == 0xa)
			drv_config->dram_side_drv = 4048;
		else if (mr3_val == 0x9)
			drv_config->dram_side_drv = 3440;
		else if (mr3_val == 0x4)
			drv_config->dram_side_drv = 60;
		else if (mr3_val == 0x3)
			drv_config->dram_side_drv = 48;
		else if (mr3_val == 0x2)
			drv_config->dram_side_drv = 40;
		else
			drv_config->dram_side_drv = 34;

		if (mr11_val == 1)
			drv_config->dram_side_dq_odt = 60;
		else if (mr11_val == 2)
			drv_config->dram_side_dq_odt = 120;
		else if (mr11_val == 0)
			drv_config->dram_side_dq_odt = 0;
		else
			drv_config->dram_side_dq_odt = 240;
177
178
179
		break;
	case LPDDR4:
	default:
180
181
		mr3_val = (mmio_read_32(CTL_REG(0, 138)) >> 3) & 0x7;
		mr11_val = (mmio_read_32(CTL_REG(0, 139)) >> 24) & 0xff;
182

183
184
185
186
		if ((mr3_val == 0) || (mr3_val == 7))
			drv_config->dram_side_drv = 40;
		else
			drv_config->dram_side_drv = 240 / mr3_val;
187

188
189
190
191
192
		tmp = mr11_val & 0x7;
		if ((tmp == 7) || (tmp == 0))
			drv_config->dram_side_dq_odt = 0;
		else
			drv_config->dram_side_dq_odt = 240 / tmp;
193

194
195
196
197
198
		tmp = (mr11_val >> 4) & 0x7;
		if ((tmp == 7) || (tmp == 0))
			drv_config->dram_side_ca_odt = 0;
		else
			drv_config->dram_side_ca_odt = 240 / tmp;
199
200
201
202
203
204
205
206
207
208
209
		break;
	}
}

static void sdram_timing_cfg_init(struct timing_related_config *ptiming_config,
				  struct rk3399_sdram_params *sdram_params,
				  struct drv_odt_lp_config *drv_config)
{
	uint32_t i, j;

	for (i = 0; i < sdram_params->num_channels; i++) {
210
		ptiming_config->dram_info[i].speed_rate = DDR3_DEFAULT;
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
		ptiming_config->dram_info[i].cs_cnt = sdram_params->ch[i].rank;
		for (j = 0; j < sdram_params->ch[i].rank; j++) {
			ptiming_config->dram_info[i].per_die_capability[j] =
			    get_cs_die_capability(sdram_params, i, j);
		}
	}
	ptiming_config->dram_type = sdram_params->dramtype;
	ptiming_config->ch_cnt = sdram_params->num_channels;
	switch (sdram_params->dramtype) {
	case DDR3:
		ptiming_config->bl = ddr3_default_config.bl;
		ptiming_config->ap = ddr3_default_config.ap;
		break;
	case LPDDR3:
		ptiming_config->bl = lpddr3_default_config.bl;
		ptiming_config->ap = lpddr3_default_config.ap;
		break;
	case LPDDR4:
		ptiming_config->bl = lpddr4_default_config.bl;
		ptiming_config->ap = lpddr4_default_config.ap;
		ptiming_config->rdbi = 0;
		ptiming_config->wdbi = 0;
		break;
	}
	ptiming_config->dramds = drv_config->dram_side_drv;
	ptiming_config->dramodt = drv_config->dram_side_dq_odt;
	ptiming_config->caodt = drv_config->dram_side_ca_odt;
238
	ptiming_config->odt = (mmio_read_32(PHY_REG(0, 5)) >> 16) & 0x1;
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
}

struct lat_adj_pair {
	uint32_t cl;
	uint32_t rdlat_adj;
	uint32_t cwl;
	uint32_t wrlat_adj;
};

const struct lat_adj_pair ddr3_lat_adj[] = {
	{6, 5, 5, 4},
	{8, 7, 6, 5},
	{10, 9, 7, 6},
	{11, 9, 8, 7},
	{13, 0xb, 9, 8},
	{14, 0xb, 0xa, 9}
};

const struct lat_adj_pair lpddr3_lat_adj[] = {
	{3, 2, 1, 0},
	{6, 5, 3, 2},
	{8, 7, 4, 3},
	{9, 8, 5, 4},
	{10, 9, 6, 5},
	{11, 9, 6, 5},
	{12, 0xa, 6, 5},
	{14, 0xc, 8, 7},
	{16, 0xd, 8, 7}
};

const struct lat_adj_pair lpddr4_lat_adj[] = {
	{6, 5, 4, 2},
	{10, 9, 6, 4},
	{14, 0xc, 8, 6},
	{20, 0x11, 0xa, 8},
	{24, 0x15, 0xc, 0xa},
	{28, 0x18, 0xe, 0xc},
	{32, 0x1b, 0x10, 0xe},
	{36, 0x1e, 0x12, 0x10}
};

static uint32_t get_rdlat_adj(uint32_t dram_type, uint32_t cl)
{
	const struct lat_adj_pair *p;
	uint32_t cnt;
	uint32_t i;

	if (dram_type == DDR3) {
		p = ddr3_lat_adj;
		cnt = ARRAY_SIZE(ddr3_lat_adj);
	} else if (dram_type == LPDDR3) {
		p = lpddr3_lat_adj;
		cnt = ARRAY_SIZE(lpddr3_lat_adj);
	} else {
		p = lpddr4_lat_adj;
		cnt = ARRAY_SIZE(lpddr4_lat_adj);
	}

	for (i = 0; i < cnt; i++) {
		if (cl == p[i].cl)
			return p[i].rdlat_adj;
	}
	/* fail */
	return 0xff;
}

static uint32_t get_wrlat_adj(uint32_t dram_type, uint32_t cwl)
{
	const struct lat_adj_pair *p;
	uint32_t cnt;
	uint32_t i;

	if (dram_type == DDR3) {
		p = ddr3_lat_adj;
		cnt = ARRAY_SIZE(ddr3_lat_adj);
	} else if (dram_type == LPDDR3) {
		p = lpddr3_lat_adj;
		cnt = ARRAY_SIZE(lpddr3_lat_adj);
	} else {
		p = lpddr4_lat_adj;
		cnt = ARRAY_SIZE(lpddr4_lat_adj);
	}

	for (i = 0; i < cnt; i++) {
		if (cwl == p[i].cwl)
			return p[i].wrlat_adj;
	}
	/* fail */
	return 0xff;
}

#define PI_REGS_DIMM_SUPPORT	(0)
#define PI_ADD_LATENCY	(0)
#define PI_DOUBLEFREEK	(1)

#define PI_PAD_DELAY_PS_VALUE	(1000)
#define PI_IE_ENABLE_VALUE	(3000)
#define PI_TSEL_ENABLE_VALUE	(700)

static uint32_t get_pi_rdlat_adj(struct dram_timing_t *pdram_timing)
{
	/*[DLLSUBTYPE2] == "STD_DENALI_HS" */
	uint32_t rdlat, delay_adder, ie_enable, hs_offset, tsel_adder,
	    extra_adder, tsel_enable;

	ie_enable = PI_IE_ENABLE_VALUE;
	tsel_enable = PI_TSEL_ENABLE_VALUE;

	rdlat = pdram_timing->cl + PI_ADD_LATENCY;
	delay_adder = ie_enable / (1000000 / pdram_timing->mhz);
	if ((ie_enable % (1000000 / pdram_timing->mhz)) != 0)
		delay_adder++;
	hs_offset = 0;
	tsel_adder = 0;
	extra_adder = 0;
	/* rdlat = rdlat - (PREAMBLE_SUPPORT & 0x1); */
	tsel_adder = tsel_enable / (1000000 / pdram_timing->mhz);
	if ((tsel_enable % (1000000 / pdram_timing->mhz)) != 0)
		tsel_adder++;
	delay_adder = delay_adder - 1;
	if (tsel_adder > delay_adder)
		extra_adder = tsel_adder - delay_adder;
	else
		extra_adder = 0;
	if (PI_REGS_DIMM_SUPPORT && PI_DOUBLEFREEK)
		hs_offset = 2;
	else
		hs_offset = 1;

	if (delay_adder > (rdlat - 1 - hs_offset)) {
		rdlat = rdlat - tsel_adder;
	} else {
		if ((rdlat - delay_adder) < 2)
			rdlat = 2;
		else
			rdlat = rdlat - delay_adder - extra_adder;
	}

	return rdlat;
}

static uint32_t get_pi_wrlat(struct dram_timing_t *pdram_timing,
			     struct timing_related_config *timing_config)
{
	uint32_t tmp;

	if (timing_config->dram_type == LPDDR3) {
		tmp = pdram_timing->cl;
		if (tmp >= 14)
			tmp = 8;
		else if (tmp >= 10)
			tmp = 6;
		else if (tmp == 9)
			tmp = 5;
		else if (tmp == 8)
			tmp = 4;
		else if (tmp == 6)
			tmp = 3;
		else
			tmp = 1;
	} else {
		tmp = 1;
	}

	return tmp;
}

static uint32_t get_pi_wrlat_adj(struct dram_timing_t *pdram_timing,
				 struct timing_related_config *timing_config)
{
	return get_pi_wrlat(pdram_timing, timing_config) + PI_ADD_LATENCY - 1;
}

static uint32_t get_pi_tdfi_phy_rdlat(struct dram_timing_t *pdram_timing,
			struct timing_related_config *timing_config)
{
	/* [DLLSUBTYPE2] == "STD_DENALI_HS" */
	uint32_t cas_lat, delay_adder, ie_enable, hs_offset, ie_delay_adder;
	uint32_t mem_delay_ps, round_trip_ps;
	uint32_t phy_internal_delay, lpddr_adder, dfi_adder, rdlat_delay;

	ie_enable = PI_IE_ENABLE_VALUE;

	delay_adder = ie_enable / (1000000 / pdram_timing->mhz);
	if ((ie_enable % (1000000 / pdram_timing->mhz)) != 0)
		delay_adder++;
	delay_adder = delay_adder - 1;
	if (PI_REGS_DIMM_SUPPORT && PI_DOUBLEFREEK)
		hs_offset = 2;
	else
		hs_offset = 1;

	cas_lat = pdram_timing->cl + PI_ADD_LATENCY;

	if (delay_adder > (cas_lat - 1 - hs_offset)) {
		ie_delay_adder = 0;
	} else {
		ie_delay_adder = ie_enable / (1000000 / pdram_timing->mhz);
		if ((ie_enable % (1000000 / pdram_timing->mhz)) != 0)
			ie_delay_adder++;
	}

	if (timing_config->dram_type == DDR3) {
		mem_delay_ps = 0;
	} else if (timing_config->dram_type == LPDDR4) {
		mem_delay_ps = 3600;
	} else if (timing_config->dram_type == LPDDR3) {
		mem_delay_ps = 5500;
	} else {
		printf("get_pi_tdfi_phy_rdlat:dramtype unsupport\n");
		return 0;
	}
	round_trip_ps = 1100 + 500 + mem_delay_ps + 500 + 600;
	delay_adder = round_trip_ps / (1000000 / pdram_timing->mhz);
	if ((round_trip_ps % (1000000 / pdram_timing->mhz)) != 0)
		delay_adder++;

	phy_internal_delay = 5 + 2 + 4;
	lpddr_adder = mem_delay_ps / (1000000 / pdram_timing->mhz);
	if ((mem_delay_ps % (1000000 / pdram_timing->mhz)) != 0)
		lpddr_adder++;
	dfi_adder = 0;
	phy_internal_delay = phy_internal_delay + 2;
	rdlat_delay = delay_adder + phy_internal_delay +
	    ie_delay_adder + lpddr_adder + dfi_adder;

	rdlat_delay = rdlat_delay + 2;
	return rdlat_delay;
}

static uint32_t get_pi_todtoff_min(struct dram_timing_t *pdram_timing,
				   struct timing_related_config *timing_config)
{
	uint32_t tmp, todtoff_min_ps;

	if (timing_config->dram_type == LPDDR3)
		todtoff_min_ps = 2500;
	else if (timing_config->dram_type == LPDDR4)
		todtoff_min_ps = 1500;
	else
		todtoff_min_ps = 0;
	/* todtoff_min */
	tmp = todtoff_min_ps / (1000000 / pdram_timing->mhz);
	if ((todtoff_min_ps % (1000000 / pdram_timing->mhz)) != 0)
		tmp++;
	return tmp;
}

static uint32_t get_pi_todtoff_max(struct dram_timing_t *pdram_timing,
				   struct timing_related_config *timing_config)
{
	uint32_t tmp, todtoff_max_ps;

	if ((timing_config->dram_type == LPDDR4)
	    || (timing_config->dram_type == LPDDR3))
		todtoff_max_ps = 3500;
	else
		todtoff_max_ps = 0;

	/* todtoff_max */
	tmp = todtoff_max_ps / (1000000 / pdram_timing->mhz);
	if ((todtoff_max_ps % (1000000 / pdram_timing->mhz)) != 0)
		tmp++;
	return tmp;
}

static void gen_rk3399_ctl_params_f0(struct timing_related_config
				     *timing_config,
				     struct dram_timing_t *pdram_timing)
{
	uint32_t i;
	uint32_t tmp, tmp1;

	for (i = 0; i < timing_config->ch_cnt; i++) {
		if (timing_config->dram_type == DDR3) {
			tmp = ((700000 + 10) * timing_config->freq +
				999) / 1000;
			tmp += pdram_timing->txsnr + (pdram_timing->tmrd * 3) +
			    pdram_timing->tmod + pdram_timing->tzqinit;
518
			mmio_write_32(CTL_REG(i, 5), tmp);
519

520
521
			mmio_clrsetbits_32(CTL_REG(i, 22), 0xffff,
					   pdram_timing->tdllk);
522

523
524
525
			mmio_write_32(CTL_REG(i, 32),
				      (pdram_timing->tmod << 8) |
				       pdram_timing->tmrd);
526

527
528
529
			mmio_clrsetbits_32(CTL_REG(i, 59), 0xffff << 16,
					   (pdram_timing->txsr -
					    pdram_timing->trcd) << 16);
530
		} else if (timing_config->dram_type == LPDDR4) {
531
532
533
534
535
536
537
			mmio_write_32(CTL_REG(i, 5), pdram_timing->tinit1 +
						     pdram_timing->tinit3);
			mmio_write_32(CTL_REG(i, 32),
				      (pdram_timing->tmrd << 8) |
				      pdram_timing->tmrd);
			mmio_clrsetbits_32(CTL_REG(i, 59), 0xffff << 16,
					   pdram_timing->txsr << 16);
538
		} else {
539
540
541
542
543
544
545
			mmio_write_32(CTL_REG(i, 5), pdram_timing->tinit1);
			mmio_write_32(CTL_REG(i, 7), pdram_timing->tinit4);
			mmio_write_32(CTL_REG(i, 32),
				      (pdram_timing->tmrd << 8) |
				      pdram_timing->tmrd);
			mmio_clrsetbits_32(CTL_REG(i, 59), 0xffff << 16,
					   pdram_timing->txsr << 16);
546
		}
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
		mmio_write_32(CTL_REG(i, 6), pdram_timing->tinit3);
		mmio_write_32(CTL_REG(i, 8), pdram_timing->tinit5);
		mmio_clrsetbits_32(CTL_REG(i, 23), (0x7f << 16),
				   ((pdram_timing->cl * 2) << 16));
		mmio_clrsetbits_32(CTL_REG(i, 23), (0x1f << 24),
				   (pdram_timing->cwl << 24));
		mmio_clrsetbits_32(CTL_REG(i, 24), 0x3f, pdram_timing->al);
		mmio_clrsetbits_32(CTL_REG(i, 26), 0xffff << 16,
				   (pdram_timing->trc << 24) |
				   (pdram_timing->trrd << 16));
		mmio_write_32(CTL_REG(i, 27),
			      (pdram_timing->tfaw << 24) |
			      (pdram_timing->trppb << 16) |
			      (pdram_timing->twtr << 8) |
			      pdram_timing->tras_min);

		mmio_clrsetbits_32(CTL_REG(i, 31), 0xff << 24,
				   max(4, pdram_timing->trtp) << 24);
		mmio_write_32(CTL_REG(i, 33), (pdram_timing->tcke << 24) |
					      pdram_timing->tras_max);
		mmio_clrsetbits_32(CTL_REG(i, 34), 0xff,
				   max(1, pdram_timing->tckesr));
		mmio_clrsetbits_32(CTL_REG(i, 39),
				   (0x3f << 16) | (0xff << 8),
				   (pdram_timing->twr << 16) |
				   (pdram_timing->trcd << 8));
		mmio_clrsetbits_32(CTL_REG(i, 42), 0x1f << 16,
				   pdram_timing->tmrz << 16);
575
		tmp = pdram_timing->tdal ? pdram_timing->tdal :
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
		      (pdram_timing->twr + pdram_timing->trp);
		mmio_clrsetbits_32(CTL_REG(i, 44), 0xff, tmp);
		mmio_clrsetbits_32(CTL_REG(i, 45), 0xff, pdram_timing->trp);
		mmio_write_32(CTL_REG(i, 48),
			      ((pdram_timing->trefi - 8) << 16) |
			      pdram_timing->trfc);
		mmio_clrsetbits_32(CTL_REG(i, 52), 0xffff, pdram_timing->txp);
		mmio_clrsetbits_32(CTL_REG(i, 53), 0xffff << 16,
				   pdram_timing->txpdll << 16);
		mmio_clrsetbits_32(CTL_REG(i, 55), 0xf << 24,
				   pdram_timing->tcscke << 24);
		mmio_clrsetbits_32(CTL_REG(i, 55), 0xff, pdram_timing->tmrri);
		mmio_write_32(CTL_REG(i, 56),
			      (pdram_timing->tzqcke << 24) |
			      (pdram_timing->tmrwckel << 16) |
			      (pdram_timing->tckehcs << 8) |
			      pdram_timing->tckelcs);
		mmio_clrsetbits_32(CTL_REG(i, 60), 0xffff, pdram_timing->txsnr);
		mmio_clrsetbits_32(CTL_REG(i, 62), 0xffff << 16,
				   (pdram_timing->tckehcmd << 24) |
				   (pdram_timing->tckelcmd << 16));
		mmio_write_32(CTL_REG(i, 63),
			      (pdram_timing->tckelpd << 24) |
			      (pdram_timing->tescke << 16) |
			      (pdram_timing->tsr << 8) |
			      pdram_timing->tckckel);
		mmio_clrsetbits_32(CTL_REG(i, 64), 0xfff,
				   (pdram_timing->tcmdcke << 8) |
				   pdram_timing->tcsckeh);
		mmio_clrsetbits_32(CTL_REG(i, 92), 0xffff << 8,
				   (pdram_timing->tcksrx << 16) |
				   (pdram_timing->tcksre << 8));
		mmio_clrsetbits_32(CTL_REG(i, 108), 0x1 << 24,
				   (timing_config->dllbp << 24));
		mmio_clrsetbits_32(CTL_REG(i, 122), 0x3ff << 16,
				   (pdram_timing->tvrcg_enable << 16));
		mmio_write_32(CTL_REG(i, 123), (pdram_timing->tfc_long << 16) |
					       pdram_timing->tvrcg_disable);
		mmio_write_32(CTL_REG(i, 124),
			      (pdram_timing->tvref_long << 16) |
			      (pdram_timing->tckfspx << 8) |
			      pdram_timing->tckfspe);
		mmio_write_32(CTL_REG(i, 133), (pdram_timing->mr[1] << 16) |
					       pdram_timing->mr[0]);
		mmio_clrsetbits_32(CTL_REG(i, 134), 0xffff,
				   pdram_timing->mr[2]);
		mmio_clrsetbits_32(CTL_REG(i, 138), 0xffff,
				   pdram_timing->mr[3]);
		mmio_clrsetbits_32(CTL_REG(i, 139), 0xff << 24,
				   pdram_timing->mr11 << 24);
		mmio_write_32(CTL_REG(i, 147),
			      (pdram_timing->mr[1] << 16) |
			      pdram_timing->mr[0]);
		mmio_clrsetbits_32(CTL_REG(i, 148), 0xffff,
				   pdram_timing->mr[2]);
		mmio_clrsetbits_32(CTL_REG(i, 152), 0xffff,
				   pdram_timing->mr[3]);
		mmio_clrsetbits_32(CTL_REG(i, 153), 0xff << 24,
				   pdram_timing->mr11 << 24);
635
		if (timing_config->dram_type == LPDDR4) {
636
637
638
639
640
641
642
643
644
645
646
647
			mmio_clrsetbits_32(CTL_REG(i, 140), 0xffff << 16,
					   pdram_timing->mr12 << 16);
			mmio_clrsetbits_32(CTL_REG(i, 142), 0xffff << 16,
					   pdram_timing->mr14 << 16);
			mmio_clrsetbits_32(CTL_REG(i, 145), 0xffff << 16,
					   pdram_timing->mr22 << 16);
			mmio_clrsetbits_32(CTL_REG(i, 154), 0xffff << 16,
					   pdram_timing->mr12 << 16);
			mmio_clrsetbits_32(CTL_REG(i, 156), 0xffff << 16,
					   pdram_timing->mr14 << 16);
			mmio_clrsetbits_32(CTL_REG(i, 159), 0xffff << 16,
					   pdram_timing->mr22 << 16);
648
		}
649
650
651
652
653
654
655
656
		mmio_clrsetbits_32(CTL_REG(i, 179), 0xfff << 8,
				   pdram_timing->tzqinit << 8);
		mmio_write_32(CTL_REG(i, 180), (pdram_timing->tzqcs << 16) |
					       (pdram_timing->tzqinit / 2));
		mmio_write_32(CTL_REG(i, 181), (pdram_timing->tzqlat << 16) |
					       pdram_timing->tzqcal);
		mmio_clrsetbits_32(CTL_REG(i, 212), 0xff << 8,
				   pdram_timing->todton << 8);
657
658

		if (timing_config->odt) {
659
			mmio_setbits_32(CTL_REG(i, 213), 1 << 16);
660
661
662
663
664
			if (timing_config->freq < 400)
				tmp = 4 << 24;
			else
				tmp = 8 << 24;
		} else {
665
			mmio_clrbits_32(CTL_REG(i, 213), 1 << 16);
666
667
668
			tmp = 2 << 24;
		}

669
670
671
672
		mmio_clrsetbits_32(CTL_REG(i, 216), 0x1f << 24, tmp);
		mmio_clrsetbits_32(CTL_REG(i, 221), (0x3 << 16) | (0xf << 8),
				   (pdram_timing->tdqsck << 16) |
				   (pdram_timing->tdqsck_max << 8));
673
674
675
676
		tmp =
		    (get_wrlat_adj(timing_config->dram_type, pdram_timing->cwl)
		     << 8) | get_rdlat_adj(timing_config->dram_type,
					   pdram_timing->cl);
677
678
679
		mmio_clrsetbits_32(CTL_REG(i, 284), 0xffff, tmp);
		mmio_clrsetbits_32(CTL_REG(i, 82), 0xffff << 16,
				   (4 * pdram_timing->trefi) << 16);
680

681
682
		mmio_clrsetbits_32(CTL_REG(i, 83), 0xffff,
				   (2 * pdram_timing->trefi) & 0xffff);
683
684
685
686
687
688
689
690
691

		if ((timing_config->dram_type == LPDDR3) ||
		    (timing_config->dram_type == LPDDR4)) {
			tmp = get_pi_wrlat(pdram_timing, timing_config);
			tmp1 = get_pi_todtoff_max(pdram_timing, timing_config);
			tmp = (tmp > tmp1) ? (tmp - tmp1) : 0;
		} else {
			tmp = 0;
		}
692
693
		mmio_clrsetbits_32(CTL_REG(i, 214), 0x3f << 16,
				   (tmp & 0x3f) << 16);
694
695
696

		if ((timing_config->dram_type == LPDDR3) ||
		    (timing_config->dram_type == LPDDR4)) {
697
			/* min_rl_preamble = cl+TDQSCK_MIN -1 */
698
699
700
701
702
703
704
705
			tmp = pdram_timing->cl +
			    get_pi_todtoff_min(pdram_timing, timing_config) - 1;
			/* todtoff_max */
			tmp1 = get_pi_todtoff_max(pdram_timing, timing_config);
			tmp = (tmp > tmp1) ? (tmp - tmp1) : 0;
		} else {
			tmp = pdram_timing->cl - pdram_timing->cwl;
		}
706
707
		mmio_clrsetbits_32(CTL_REG(i, 215), 0x3f << 8,
				   (tmp & 0x3f) << 8);
708

709
710
711
712
		mmio_clrsetbits_32(CTL_REG(i, 275), 0xff << 16,
				   (get_pi_tdfi_phy_rdlat(pdram_timing,
							  timing_config) &
				    0xff) << 16);
713

714
715
		mmio_clrsetbits_32(CTL_REG(i, 277), 0xffff,
				   (2 * pdram_timing->trefi) & 0xffff);
716

717
718
		mmio_clrsetbits_32(CTL_REG(i, 282), 0xffff,
				   (2 * pdram_timing->trefi) & 0xffff);
719

720
		mmio_write_32(CTL_REG(i, 283), 20 * pdram_timing->trefi);
721
722
723
724
725
726

		/* CTL_308 TDFI_CALVL_CAPTURE_F0:RW:16:10 */
		tmp1 = 20000 / (1000000 / pdram_timing->mhz) + 1;
		if ((20000 % (1000000 / pdram_timing->mhz)) != 0)
			tmp1++;
		tmp = (tmp1 >> 1) + (tmp1 % 2) + 5;
727
		mmio_clrsetbits_32(CTL_REG(i, 308), 0x3ff << 16, tmp << 16);
728
729
730

		/* CTL_308 TDFI_CALVL_CC_F0:RW:0:10 */
		tmp = tmp + 18;
731
		mmio_clrsetbits_32(CTL_REG(i, 308), 0x3ff, tmp);
732
733
734

		/* CTL_314 TDFI_WRCSLAT_F0:RW:8:8 */
		tmp1 = get_pi_wrlat_adj(pdram_timing, timing_config);
735
		if (timing_config->freq <= TDFI_LAT_THRESHOLD_FREQ) {
736
737
738
739
740
			if (tmp1 == 0)
				tmp = 0;
			else if (tmp1 < 5)
				tmp = tmp1 - 1;
			else
741
742
743
744
				tmp = tmp1 - 5;
		} else {
			tmp = tmp1 - 2;
		}
745
		mmio_clrsetbits_32(CTL_REG(i, 314), 0xff << 8, tmp << 8);
746
747

		/* CTL_314 TDFI_RDCSLAT_F0:RW:0:8 */
748
		if ((timing_config->freq <= TDFI_LAT_THRESHOLD_FREQ) &&
749
		    (pdram_timing->cl >= 5))
750
751
752
			tmp = pdram_timing->cl - 5;
		else
			tmp = pdram_timing->cl - 2;
753
		mmio_clrsetbits_32(CTL_REG(i, 314), 0xff, tmp);
754
755
756
757
758
759
760
761
762
763
764
765
766
	}
}

static void gen_rk3399_ctl_params_f1(struct timing_related_config
				     *timing_config,
				     struct dram_timing_t *pdram_timing)
{
	uint32_t i;
	uint32_t tmp, tmp1;

	for (i = 0; i < timing_config->ch_cnt; i++) {
		if (timing_config->dram_type == DDR3) {
			tmp =
767
768
769
770
771
772
773
774
775
776
777
778
779
			    ((700000 + 10) * timing_config->freq + 999) / 1000;
			tmp += pdram_timing->txsnr + (pdram_timing->tmrd * 3) +
			       pdram_timing->tmod + pdram_timing->tzqinit;
			mmio_write_32(CTL_REG(i, 9), tmp);
			mmio_clrsetbits_32(CTL_REG(i, 22), 0xffff << 16,
					   pdram_timing->tdllk << 16);
			mmio_clrsetbits_32(CTL_REG(i, 34), 0xffffff00,
					   (pdram_timing->tmod << 24) |
					   (pdram_timing->tmrd << 16) |
					   (pdram_timing->trtp << 8));
			mmio_clrsetbits_32(CTL_REG(i, 60), 0xffff << 16,
					   (pdram_timing->txsr -
					    pdram_timing->trcd) << 16);
780
		} else if (timing_config->dram_type == LPDDR4) {
781
782
783
784
785
786
787
788
			mmio_write_32(CTL_REG(i, 9), pdram_timing->tinit1 +
						     pdram_timing->tinit3);
			mmio_clrsetbits_32(CTL_REG(i, 34), 0xffffff00,
					   (pdram_timing->tmrd << 24) |
					   (pdram_timing->tmrd << 16) |
					   (pdram_timing->trtp << 8));
			mmio_clrsetbits_32(CTL_REG(i, 60), 0xffff << 16,
					   pdram_timing->txsr << 16);
789
		} else {
790
791
792
793
794
795
796
797
			mmio_write_32(CTL_REG(i, 9), pdram_timing->tinit1);
			mmio_write_32(CTL_REG(i, 11), pdram_timing->tinit4);
			mmio_clrsetbits_32(CTL_REG(i, 34), 0xffffff00,
					   (pdram_timing->tmrd << 24) |
					   (pdram_timing->tmrd << 16) |
					   (pdram_timing->trtp << 8));
			mmio_clrsetbits_32(CTL_REG(i, 60), 0xffff << 16,
					   pdram_timing->txsr << 16);
798
		}
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
		mmio_write_32(CTL_REG(i, 10), pdram_timing->tinit3);
		mmio_write_32(CTL_REG(i, 12), pdram_timing->tinit5);
		mmio_clrsetbits_32(CTL_REG(i, 24), (0x7f << 8),
				   ((pdram_timing->cl * 2) << 8));
		mmio_clrsetbits_32(CTL_REG(i, 24), (0x1f << 16),
				   (pdram_timing->cwl << 16));
		mmio_clrsetbits_32(CTL_REG(i, 24), 0x3f << 24,
				   pdram_timing->al << 24);
		mmio_clrsetbits_32(CTL_REG(i, 28), 0xffffff00,
				   (pdram_timing->tras_min << 24) |
				   (pdram_timing->trc << 16) |
				   (pdram_timing->trrd << 8));
		mmio_clrsetbits_32(CTL_REG(i, 29), 0xffffff,
				   (pdram_timing->tfaw << 16) |
				   (pdram_timing->trppb << 8) |
				   pdram_timing->twtr);
		mmio_write_32(CTL_REG(i, 35), (pdram_timing->tcke << 24) |
					      pdram_timing->tras_max);
		mmio_clrsetbits_32(CTL_REG(i, 36), 0xff,
				   max(1, pdram_timing->tckesr));
		mmio_clrsetbits_32(CTL_REG(i, 39), (0xff << 24),
				   (pdram_timing->trcd << 24));
		mmio_clrsetbits_32(CTL_REG(i, 40), 0x3f, pdram_timing->twr);
		mmio_clrsetbits_32(CTL_REG(i, 42), 0x1f << 24,
				   pdram_timing->tmrz << 24);
824
		tmp = pdram_timing->tdal ? pdram_timing->tdal :
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
		      (pdram_timing->twr + pdram_timing->trp);
		mmio_clrsetbits_32(CTL_REG(i, 44), 0xff << 8, tmp << 8);
		mmio_clrsetbits_32(CTL_REG(i, 45), 0xff << 8,
				   pdram_timing->trp << 8);
		mmio_write_32(CTL_REG(i, 49),
			      ((pdram_timing->trefi - 8) << 16) |
			      pdram_timing->trfc);
		mmio_clrsetbits_32(CTL_REG(i, 52), 0xffff << 16,
				   pdram_timing->txp << 16);
		mmio_clrsetbits_32(CTL_REG(i, 54), 0xffff,
				   pdram_timing->txpdll);
		mmio_clrsetbits_32(CTL_REG(i, 55), 0xff << 8,
				   pdram_timing->tmrri << 8);
		mmio_write_32(CTL_REG(i, 57), (pdram_timing->tmrwckel << 24) |
					      (pdram_timing->tckehcs << 16) |
					      (pdram_timing->tckelcs << 8) |
					      pdram_timing->tcscke);
		mmio_clrsetbits_32(CTL_REG(i, 58), 0xf, pdram_timing->tzqcke);
		mmio_clrsetbits_32(CTL_REG(i, 61), 0xffff, pdram_timing->txsnr);
		mmio_clrsetbits_32(CTL_REG(i, 64), 0xffff << 16,
				   (pdram_timing->tckehcmd << 24) |
				   (pdram_timing->tckelcmd << 16));
		mmio_write_32(CTL_REG(i, 65), (pdram_timing->tckelpd << 24) |
					      (pdram_timing->tescke << 16) |
					      (pdram_timing->tsr << 8) |
					      pdram_timing->tckckel);
		mmio_clrsetbits_32(CTL_REG(i, 66), 0xfff,
				   (pdram_timing->tcmdcke << 8) |
				   pdram_timing->tcsckeh);
		mmio_clrsetbits_32(CTL_REG(i, 92), (0xff << 24),
				   (pdram_timing->tcksre << 24));
		mmio_clrsetbits_32(CTL_REG(i, 93), 0xff,
				   pdram_timing->tcksrx);
		mmio_clrsetbits_32(CTL_REG(i, 108), (0x1 << 25),
				   (timing_config->dllbp << 25));
		mmio_write_32(CTL_REG(i, 125),
			      (pdram_timing->tvrcg_disable << 16) |
			      pdram_timing->tvrcg_enable);
		mmio_write_32(CTL_REG(i, 126), (pdram_timing->tckfspx << 24) |
					       (pdram_timing->tckfspe << 16) |
					       pdram_timing->tfc_long);
		mmio_clrsetbits_32(CTL_REG(i, 127), 0xffff,
				   pdram_timing->tvref_long);
		mmio_clrsetbits_32(CTL_REG(i, 134), 0xffff << 16,
				   pdram_timing->mr[0] << 16);
		mmio_write_32(CTL_REG(i, 135), (pdram_timing->mr[2] << 16) |
					       pdram_timing->mr[1]);
		mmio_clrsetbits_32(CTL_REG(i, 138), 0xffff << 16,
				   pdram_timing->mr[3] << 16);
		mmio_clrsetbits_32(CTL_REG(i, 140), 0xff, pdram_timing->mr11);
		mmio_clrsetbits_32(CTL_REG(i, 148), 0xffff << 16,
				   pdram_timing->mr[0] << 16);
		mmio_write_32(CTL_REG(i, 149), (pdram_timing->mr[2] << 16) |
					       pdram_timing->mr[1]);
		mmio_clrsetbits_32(CTL_REG(i, 152), 0xffff << 16,
				   pdram_timing->mr[3] << 16);
		mmio_clrsetbits_32(CTL_REG(i, 154), 0xff, pdram_timing->mr11);
882
		if (timing_config->dram_type == LPDDR4) {
883
884
885
886
887
888
889
890
891
892
893
894
			mmio_clrsetbits_32(CTL_REG(i, 141), 0xffff,
					   pdram_timing->mr12);
			mmio_clrsetbits_32(CTL_REG(i, 143), 0xffff,
					   pdram_timing->mr14);
			mmio_clrsetbits_32(CTL_REG(i, 146), 0xffff,
					   pdram_timing->mr22);
			mmio_clrsetbits_32(CTL_REG(i, 155), 0xffff,
					   pdram_timing->mr12);
			mmio_clrsetbits_32(CTL_REG(i, 157), 0xffff,
					   pdram_timing->mr14);
			mmio_clrsetbits_32(CTL_REG(i, 160), 0xffff,
					   pdram_timing->mr22);
895
		}
896
897
898
899
900
901
902
903
904
905
		mmio_write_32(CTL_REG(i, 182),
			      ((pdram_timing->tzqinit / 2) << 16) |
			      pdram_timing->tzqinit);
		mmio_write_32(CTL_REG(i, 183), (pdram_timing->tzqcal << 16) |
					       pdram_timing->tzqcs);
		mmio_clrsetbits_32(CTL_REG(i, 184), 0x3f, pdram_timing->tzqlat);
		mmio_clrsetbits_32(CTL_REG(i, 188), 0xfff,
				   pdram_timing->tzqreset);
		mmio_clrsetbits_32(CTL_REG(i, 212), 0xff << 16,
				   pdram_timing->todton << 16);
906
907

		if (timing_config->odt) {
908
			mmio_setbits_32(CTL_REG(i, 213), (1 << 24));
909
910
911
912
913
			if (timing_config->freq < 400)
				tmp = 4 << 24;
			else
				tmp = 8 << 24;
		} else {
914
			mmio_clrbits_32(CTL_REG(i, 213), (1 << 24));
915
916
			tmp = 2 << 24;
		}
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
		mmio_clrsetbits_32(CTL_REG(i, 217), 0x1f << 24, tmp);
		mmio_clrsetbits_32(CTL_REG(i, 221), 0xf << 24,
				   (pdram_timing->tdqsck_max << 24));
		mmio_clrsetbits_32(CTL_REG(i, 222), 0x3, pdram_timing->tdqsck);
		mmio_clrsetbits_32(CTL_REG(i, 291), 0xffff,
				   (get_wrlat_adj(timing_config->dram_type,
						  pdram_timing->cwl) << 8) |
				   get_rdlat_adj(timing_config->dram_type,
						 pdram_timing->cl));

		mmio_clrsetbits_32(CTL_REG(i, 84), 0xffff,
				   (4 * pdram_timing->trefi) & 0xffff);

		mmio_clrsetbits_32(CTL_REG(i, 84), 0xffff << 16,
				   ((2 * pdram_timing->trefi) & 0xffff) << 16);
932
933
934
935
936
937
938
939
940

		if ((timing_config->dram_type == LPDDR3) ||
		    (timing_config->dram_type == LPDDR4)) {
			tmp = get_pi_wrlat(pdram_timing, timing_config);
			tmp1 = get_pi_todtoff_max(pdram_timing, timing_config);
			tmp = (tmp > tmp1) ? (tmp - tmp1) : 0;
		} else {
			tmp = 0;
		}
941
942
		mmio_clrsetbits_32(CTL_REG(i, 214), 0x3f << 24,
				   (tmp & 0x3f) << 24);
943
944
945

		if ((timing_config->dram_type == LPDDR3) ||
		    (timing_config->dram_type == LPDDR4)) {
946
			/* min_rl_preamble = cl + TDQSCK_MIN - 1 */
947
			tmp = pdram_timing->cl +
948
949
			      get_pi_todtoff_min(pdram_timing, timing_config);
			tmp--;
950
951
952
953
954
955
			/* todtoff_max */
			tmp1 = get_pi_todtoff_max(pdram_timing, timing_config);
			tmp = (tmp > tmp1) ? (tmp - tmp1) : 0;
		} else {
			tmp = pdram_timing->cl - pdram_timing->cwl;
		}
956
957
		mmio_clrsetbits_32(CTL_REG(i, 215), 0x3f << 16,
				   (tmp & 0x3f) << 16);
958

959
960
961
962
		mmio_clrsetbits_32(CTL_REG(i, 275), 0xff << 24,
				   (get_pi_tdfi_phy_rdlat(pdram_timing,
							  timing_config) &
				    0xff) << 24);
963

964
965
		mmio_clrsetbits_32(CTL_REG(i, 284), 0xffff << 16,
				   ((2 * pdram_timing->trefi) & 0xffff) << 16);
966

967
968
		mmio_clrsetbits_32(CTL_REG(i, 289), 0xffff,
				   (2 * pdram_timing->trefi) & 0xffff);
969

970
		mmio_write_32(CTL_REG(i, 290), 20 * pdram_timing->trefi);
971
972
973
974
975
976

		/* CTL_309 TDFI_CALVL_CAPTURE_F1:RW:16:10 */
		tmp1 = 20000 / (1000000 / pdram_timing->mhz) + 1;
		if ((20000 % (1000000 / pdram_timing->mhz)) != 0)
			tmp1++;
		tmp = (tmp1 >> 1) + (tmp1 % 2) + 5;
977
		mmio_clrsetbits_32(CTL_REG(i, 309), 0x3ff << 16, tmp << 16);
978
979
980

		/* CTL_309 TDFI_CALVL_CC_F1:RW:0:10 */
		tmp = tmp + 18;
981
		mmio_clrsetbits_32(CTL_REG(i, 309), 0x3ff, tmp);
982
983
984

		/* CTL_314 TDFI_WRCSLAT_F1:RW:24:8 */
		tmp1 = get_pi_wrlat_adj(pdram_timing, timing_config);
985
		if (timing_config->freq <= TDFI_LAT_THRESHOLD_FREQ) {
986
987
988
989
990
			if (tmp1 == 0)
				tmp = 0;
			else if (tmp1 < 5)
				tmp = tmp1 - 1;
			else
991
992
993
994
995
				tmp = tmp1 - 5;
		} else {
			tmp = tmp1 - 2;
		}

996
		mmio_clrsetbits_32(CTL_REG(i, 314), 0xff << 24, tmp << 24);
997
998

		/* CTL_314 TDFI_RDCSLAT_F1:RW:16:8 */
999
		if ((timing_config->freq <= TDFI_LAT_THRESHOLD_FREQ) &&
1000
		    (pdram_timing->cl >= 5))
1001
1002
1003
			tmp = pdram_timing->cl - 5;
		else
			tmp = pdram_timing->cl - 2;
1004
		mmio_clrsetbits_32(CTL_REG(i, 314), 0xff << 16, tmp << 16);
1005
1006
1007
	}
}

1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
static void gen_rk3399_enable_training(uint32_t ch_cnt, uint32_t nmhz)
{
		uint32_t i, tmp;

		if (nmhz <= PHY_DLL_BYPASS_FREQ)
			tmp = 0;
		else
			tmp = 1;

		for (i = 0; i < ch_cnt; i++) {
			mmio_clrsetbits_32(CTL_REG(i, 305), 1 << 16, tmp << 16);
			mmio_clrsetbits_32(CTL_REG(i, 71), 1, tmp);
1020
			mmio_clrsetbits_32(CTL_REG(i, 70), 1 << 8, 1 << 8);
1021
1022
1023
		}
}

1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
static void gen_rk3399_disable_training(uint32_t ch_cnt)
{
	uint32_t i;

	for (i = 0; i < ch_cnt; i++) {
		mmio_clrbits_32(CTL_REG(i, 305), 1 << 16);
		mmio_clrbits_32(CTL_REG(i, 71), 1);
		mmio_clrbits_32(CTL_REG(i, 70), 1 << 8);
	}
}

1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
static void gen_rk3399_ctl_params(struct timing_related_config *timing_config,
				  struct dram_timing_t *pdram_timing,
				  uint32_t fn)
{
	if (fn == 0)
		gen_rk3399_ctl_params_f0(timing_config, pdram_timing);
	else
		gen_rk3399_ctl_params_f1(timing_config, pdram_timing);
}

static void gen_rk3399_pi_params_f0(struct timing_related_config *timing_config,
				    struct dram_timing_t *pdram_timing)
{
	uint32_t tmp, tmp1, tmp2;
	uint32_t i;

	for (i = 0; i < timing_config->ch_cnt; i++) {
		/* PI_02 PI_TDFI_PHYMSTR_MAX_F0:RW:0:32 */
		tmp = 4 * pdram_timing->trefi;
1054
		mmio_write_32(PI_REG(i, 2), tmp);
1055
1056
		/* PI_03 PI_TDFI_PHYMSTR_RESP_F0:RW:0:16 */
		tmp = 2 * pdram_timing->trefi;
1057
		mmio_clrsetbits_32(PI_REG(i, 3), 0xffff, tmp);
1058
		/* PI_07 PI_TDFI_PHYUPD_RESP_F0:RW:16:16 */
1059
		mmio_clrsetbits_32(PI_REG(i, 7), 0xffff << 16, tmp << 16);
1060
1061
1062
1063
1064
1065
1066

		/* PI_42 PI_TDELAY_RDWR_2_BUS_IDLE_F0:RW:0:8 */
		if (timing_config->dram_type == LPDDR4)
			tmp = 2;
		else
			tmp = 0;
		tmp = (pdram_timing->bl / 2) + 4 +
1067
1068
1069
		      (get_pi_rdlat_adj(pdram_timing) - 2) + tmp +
		      get_pi_tdfi_phy_rdlat(pdram_timing, timing_config);
		mmio_clrsetbits_32(PI_REG(i, 42), 0xff, tmp);
1070
1071
1072
		/* PI_43 PI_WRLAT_F0:RW:0:5 */
		if (timing_config->dram_type == LPDDR3) {
			tmp = get_pi_wrlat(pdram_timing, timing_config);
1073
			mmio_clrsetbits_32(PI_REG(i, 43), 0x1f, tmp);
1074
1075
		}
		/* PI_43 PI_ADDITIVE_LAT_F0:RW:8:6 */
1076
1077
		mmio_clrsetbits_32(PI_REG(i, 43), 0x3f << 8,
				   PI_ADD_LATENCY << 8);
1078
1079

		/* PI_43 PI_CASLAT_LIN_F0:RW:16:7 */
1080
1081
		mmio_clrsetbits_32(PI_REG(i, 43), 0x7f << 16,
				   (pdram_timing->cl * 2) << 16);
1082
		/* PI_46 PI_TREF_F0:RW:16:16 */
1083
1084
		mmio_clrsetbits_32(PI_REG(i, 46), 0xffff << 16,
				   pdram_timing->trefi << 16);
1085
		/* PI_46 PI_TRFC_F0:RW:0:10 */
1086
		mmio_clrsetbits_32(PI_REG(i, 46), 0x3ff, pdram_timing->trfc);
1087
1088
1089
		/* PI_66 PI_TODTL_2CMD_F0:RW:24:8 */
		if (timing_config->dram_type == LPDDR3) {
			tmp = get_pi_todtoff_max(pdram_timing, timing_config);
1090
1091
			mmio_clrsetbits_32(PI_REG(i, 66), 0xff << 24,
					   tmp << 24);
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
		}
		/* PI_72 PI_WR_TO_ODTH_F0:RW:16:6 */
		if ((timing_config->dram_type == LPDDR3) ||
		    (timing_config->dram_type == LPDDR4)) {
			tmp1 = get_pi_wrlat(pdram_timing, timing_config);
			tmp2 = get_pi_todtoff_max(pdram_timing, timing_config);
			if (tmp1 > tmp2)
				tmp = tmp1 - tmp2;
			else
				tmp = 0;
		} else if (timing_config->dram_type == DDR3) {
			tmp = 0;
		}
1105
		mmio_clrsetbits_32(PI_REG(i, 72), 0x3f << 16, tmp << 16);
1106
1107
1108
		/* PI_73 PI_RD_TO_ODTH_F0:RW:8:6 */
		if ((timing_config->dram_type == LPDDR3) ||
		    (timing_config->dram_type == LPDDR4)) {
1109
1110
1111
1112
			/* min_rl_preamble = cl + TDQSCK_MIN - 1 */
			tmp1 = pdram_timing->cl;
			tmp1 += get_pi_todtoff_min(pdram_timing, timing_config);
			tmp1--;
1113
1114
1115
1116
1117
1118
1119
1120
1121
			/* todtoff_max */
			tmp2 = get_pi_todtoff_max(pdram_timing, timing_config);
			if (tmp1 > tmp2)
				tmp = tmp1 - tmp2;
			else
				tmp = 0;
		} else if (timing_config->dram_type == DDR3) {
			tmp = pdram_timing->cl - pdram_timing->cwl;
		}
1122
		mmio_clrsetbits_32(PI_REG(i, 73), 0x3f << 8, tmp << 8);
1123
1124
		/* PI_89 PI_RDLAT_ADJ_F0:RW:16:8 */
		tmp = get_pi_rdlat_adj(pdram_timing);
1125
		mmio_clrsetbits_32(PI_REG(i, 89), 0xff << 16, tmp << 16);
1126
1127
		/* PI_90 PI_WRLAT_ADJ_F0:RW:16:8 */
		tmp = get_pi_wrlat_adj(pdram_timing, timing_config);
1128
		mmio_clrsetbits_32(PI_REG(i, 90), 0xff << 16, tmp << 16);
1129
1130
		/* PI_91 PI_TDFI_WRCSLAT_F0:RW:16:8 */
		tmp1 = tmp;
1131
1132
1133
1134
1135
		if (tmp1 == 0)
			tmp = 0;
		else if (tmp1 < 5)
			tmp = tmp1 - 1;
		else
1136
			tmp = tmp1 - 5;
1137
		mmio_clrsetbits_32(PI_REG(i, 91), 0xff << 16, tmp << 16);
1138
1139
1140
1141
1142
		/* PI_95 PI_TDFI_CALVL_CAPTURE_F0:RW:16:10 */
		tmp1 = 20000 / (1000000 / pdram_timing->mhz) + 1;
		if ((20000 % (1000000 / pdram_timing->mhz)) != 0)
			tmp1++;
		tmp = (tmp1 >> 1) + (tmp1 % 2) + 5;
1143
		mmio_clrsetbits_32(PI_REG(i, 95), 0x3ff << 16, tmp << 16);
1144
		/* PI_95 PI_TDFI_CALVL_CC_F0:RW:0:10 */
1145
		mmio_clrsetbits_32(PI_REG(i, 95), 0x3ff, tmp + 18);
1146
		/* PI_102 PI_TMRZ_F0:RW:8:5 */
1147
1148
		mmio_clrsetbits_32(PI_REG(i, 102), 0x1f << 8,
				   pdram_timing->tmrz << 8);
1149
1150
1151
1152
1153
1154
		/* PI_111 PI_TDFI_CALVL_STROBE_F0:RW:8:4 */
		tmp1 = 2 * 1000 / (1000000 / pdram_timing->mhz);
		if ((2 * 1000 % (1000000 / pdram_timing->mhz)) != 0)
			tmp1++;
		/* pi_tdfi_calvl_strobe=tds_train+5 */
		tmp = tmp1 + 5;
1155
		mmio_clrsetbits_32(PI_REG(i, 111), 0xf << 8, tmp << 8);
1156
1157
1158
1159
1160
1161
1162
1163
		/* PI_116 PI_TCKEHDQS_F0:RW:16:6 */
		tmp = 10000 / (1000000 / pdram_timing->mhz);
		if ((10000 % (1000000 / pdram_timing->mhz)) != 0)
			tmp++;
		if (pdram_timing->mhz <= 100)
			tmp = tmp + 1;
		else
			tmp = tmp + 8;
1164
		mmio_clrsetbits_32(PI_REG(i, 116), 0x3f << 16, tmp << 16);
1165
		/* PI_125 PI_MR1_DATA_F0_0:RW+:8:16 */
1166
1167
		mmio_clrsetbits_32(PI_REG(i, 125), 0xffff << 8,
				   pdram_timing->mr[1] << 8);
1168
		/* PI_133 PI_MR1_DATA_F0_1:RW+:0:16 */
1169
		mmio_clrsetbits_32(PI_REG(i, 133), 0xffff, pdram_timing->mr[1]);
1170
		/* PI_140 PI_MR1_DATA_F0_2:RW+:16:16 */
1171
1172
		mmio_clrsetbits_32(PI_REG(i, 140), 0xffff << 16,
				   pdram_timing->mr[1] << 16);
1173
		/* PI_148 PI_MR1_DATA_F0_3:RW+:0:16 */
1174
		mmio_clrsetbits_32(PI_REG(i, 148), 0xffff, pdram_timing->mr[1]);
1175
		/* PI_126 PI_MR2_DATA_F0_0:RW+:0:16 */
1176
		mmio_clrsetbits_32(PI_REG(i, 126), 0xffff, pdram_timing->mr[2]);
1177
		/* PI_133 PI_MR2_DATA_F0_1:RW+:16:16 */
1178
1179
		mmio_clrsetbits_32(PI_REG(i, 133), 0xffff << 16,
				   pdram_timing->mr[2] << 16);
1180
		/* PI_141 PI_MR2_DATA_F0_2:RW+:0:16 */
1181
		mmio_clrsetbits_32(PI_REG(i, 141), 0xffff, pdram_timing->mr[2]);
1182
		/* PI_148 PI_MR2_DATA_F0_3:RW+:16:16 */
1183
1184
		mmio_clrsetbits_32(PI_REG(i, 148), 0xffff << 16,
				   pdram_timing->mr[2] << 16);
1185
		/* PI_156 PI_TFC_F0:RW:0:10 */
1186
		mmio_clrsetbits_32(PI_REG(i, 156), 0x3ff, pdram_timing->trfc);
1187
		/* PI_158 PI_TWR_F0:RW:24:6 */
1188
1189
		mmio_clrsetbits_32(PI_REG(i, 158), 0x3f << 24,
				   pdram_timing->twr << 24);
1190
		/* PI_158 PI_TWTR_F0:RW:16:6 */
1191
1192
		mmio_clrsetbits_32(PI_REG(i, 158), 0x3f << 16,
				   pdram_timing->twtr << 16);
1193
		/* PI_158 PI_TRCD_F0:RW:8:8 */
1194
1195
		mmio_clrsetbits_32(PI_REG(i, 158), 0xff << 8,
				   pdram_timing->trcd << 8);
1196
		/* PI_158 PI_TRP_F0:RW:0:8 */
1197
		mmio_clrsetbits_32(PI_REG(i, 158), 0xff, pdram_timing->trp);
1198
		/* PI_157 PI_TRTP_F0:RW:24:8 */
1199
1200
		mmio_clrsetbits_32(PI_REG(i, 157), 0xff << 24,
				   pdram_timing->trtp << 24);
1201
		/* PI_159 PI_TRAS_MIN_F0:RW:24:8 */
1202
1203
		mmio_clrsetbits_32(PI_REG(i, 159), 0xff << 24,
				   pdram_timing->tras_min << 24);
1204
1205
		/* PI_159 PI_TRAS_MAX_F0:RW:0:17 */
		tmp = pdram_timing->tras_max * 99 / 100;
1206
		mmio_clrsetbits_32(PI_REG(i, 159), 0x1ffff, tmp);
1207
		/* PI_160 PI_TMRD_F0:RW:16:6 */
1208
1209
		mmio_clrsetbits_32(PI_REG(i, 160), 0x3f << 16,
				   pdram_timing->tmrd << 16);
1210
		/*PI_160 PI_TDQSCK_MAX_F0:RW:0:4 */
1211
1212
		mmio_clrsetbits_32(PI_REG(i, 160), 0xf,
				   pdram_timing->tdqsck_max);
1213
		/* PI_187 PI_TDFI_CTRLUPD_MAX_F0:RW:8:16 */
1214
1215
		mmio_clrsetbits_32(PI_REG(i, 187), 0xffff << 8,
				   (2 * pdram_timing->trefi) << 8);
1216
		/* PI_188 PI_TDFI_CTRLUPD_INTERVAL_F0:RW:0:32 */
1217
1218
		mmio_clrsetbits_32(PI_REG(i, 188), 0xffffffff,
				   20 * pdram_timing->trefi);
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
	}
}

static void gen_rk3399_pi_params_f1(struct timing_related_config *timing_config,
				    struct dram_timing_t *pdram_timing)
{
	uint32_t tmp, tmp1, tmp2;
	uint32_t i;

	for (i = 0; i < timing_config->ch_cnt; i++) {
		/* PI_04 PI_TDFI_PHYMSTR_MAX_F1:RW:0:32 */
		tmp = 4 * pdram_timing->trefi;
1231
		mmio_write_32(PI_REG(i, 4), tmp);
1232
1233
		/* PI_05 PI_TDFI_PHYMSTR_RESP_F1:RW:0:16 */
		tmp = 2 * pdram_timing->trefi;
1234
		mmio_clrsetbits_32(PI_REG(i, 5), 0xffff, tmp);
1235
		/* PI_12 PI_TDFI_PHYUPD_RESP_F1:RW:0:16 */
1236
		mmio_clrsetbits_32(PI_REG(i, 12), 0xffff, tmp);
1237
1238
1239
1240
1241
1242
1243

		/* PI_42 PI_TDELAY_RDWR_2_BUS_IDLE_F1:RW:8:8 */
		if (timing_config->dram_type == LPDDR4)
			tmp = 2;
		else
			tmp = 0;
		tmp = (pdram_timing->bl / 2) + 4 +
1244
1245
1246
		      (get_pi_rdlat_adj(pdram_timing) - 2) + tmp +
		      get_pi_tdfi_phy_rdlat(pdram_timing, timing_config);
		mmio_clrsetbits_32(PI_REG(i, 42), 0xff << 8, tmp << 8);
1247
1248
1249
		/* PI_43 PI_WRLAT_F1:RW:24:5 */
		if (timing_config->dram_type == LPDDR3) {
			tmp = get_pi_wrlat(pdram_timing, timing_config);
1250
1251
			mmio_clrsetbits_32(PI_REG(i, 43), 0x1f << 24,
					   tmp << 24);
1252
1253
		}
		/* PI_44 PI_ADDITIVE_LAT_F1:RW:0:6 */
1254
		mmio_clrsetbits_32(PI_REG(i, 44), 0x3f, PI_ADD_LATENCY);
1255
		/* PI_44 PI_CASLAT_LIN_F1:RW:8:7:=0x18 */
1256
		mmio_clrsetbits_32(PI_REG(i, 44), 0x7f << 8,
1257
				   (pdram_timing->cl * 2) << 8);
1258
		/* PI_47 PI_TREF_F1:RW:16:16 */
1259
1260
		mmio_clrsetbits_32(PI_REG(i, 47), 0xffff << 16,
				   pdram_timing->trefi << 16);
1261
		/* PI_47 PI_TRFC_F1:RW:0:10 */
1262
		mmio_clrsetbits_32(PI_REG(i, 47), 0x3ff, pdram_timing->trfc);
1263
1264
1265
		/* PI_67 PI_TODTL_2CMD_F1:RW:8:8 */
		if (timing_config->dram_type == LPDDR3) {
			tmp = get_pi_todtoff_max(pdram_timing, timing_config);
1266
			mmio_clrsetbits_32(PI_REG(i, 67), 0xff << 8, tmp << 8);
1267
1268
		}
		/* PI_72 PI_WR_TO_ODTH_F1:RW:24:6 */
1269
1270
		if ((timing_config->dram_type == LPDDR3) ||
		    (timing_config->dram_type == LPDDR4)) {
1271
1272
1273
1274
1275
1276
1277
1278
1279
			tmp1 = get_pi_wrlat(pdram_timing, timing_config);
			tmp2 = get_pi_todtoff_max(pdram_timing, timing_config);
			if (tmp1 > tmp2)
				tmp = tmp1 - tmp2;
			else
				tmp = 0;
		} else if (timing_config->dram_type == DDR3) {
			tmp = 0;
		}
1280
		mmio_clrsetbits_32(PI_REG(i, 72), 0x3f << 24, tmp << 24);
1281
		/* PI_73 PI_RD_TO_ODTH_F1:RW:16:6 */
1282
1283
1284
1285
1286
1287
		if ((timing_config->dram_type == LPDDR3) ||
		    (timing_config->dram_type == LPDDR4)) {
			/* min_rl_preamble = cl + TDQSCK_MIN - 1 */
			tmp1 = pdram_timing->cl +
			       get_pi_todtoff_min(pdram_timing, timing_config);
			tmp1--;
1288
1289
1290
1291
1292
1293
			/* todtoff_max */
			tmp2 = get_pi_todtoff_max(pdram_timing, timing_config);
			if (tmp1 > tmp2)
				tmp = tmp1 - tmp2;
			else
				tmp = 0;
1294
		} else if (timing_config->dram_type == DDR3)
1295
			tmp = pdram_timing->cl - pdram_timing->cwl;
1296
1297

		mmio_clrsetbits_32(PI_REG(i, 73), 0x3f << 16, tmp << 16);
1298
1299
		/*P I_89 PI_RDLAT_ADJ_F1:RW:24:8 */
		tmp = get_pi_rdlat_adj(pdram_timing);
1300
		mmio_clrsetbits_32(PI_REG(i, 89), 0xff << 24, tmp << 24);
1301
1302
		/* PI_90 PI_WRLAT_ADJ_F1:RW:24:8 */
		tmp = get_pi_wrlat_adj(pdram_timing, timing_config);
1303
		mmio_clrsetbits_32(PI_REG(i, 90), 0xff << 24, tmp << 24);
1304
1305
		/* PI_91 PI_TDFI_WRCSLAT_F1:RW:24:8 */
		tmp1 = tmp;
1306
1307
1308
1309
1310
		if (tmp1 == 0)
			tmp = 0;
		else if (tmp1 < 5)
			tmp = tmp1 - 1;
		else
1311
			tmp = tmp1 - 5;
1312
		mmio_clrsetbits_32(PI_REG(i, 91), 0xff << 24, tmp << 24);
1313
1314
1315
1316
1317
1318
		/*PI_96 PI_TDFI_CALVL_CAPTURE_F1:RW:16:10 */
		/* tadr=20ns */
		tmp1 = 20000 / (1000000 / pdram_timing->mhz) + 1;
		if ((20000 % (1000000 / pdram_timing->mhz)) != 0)
			tmp1++;
		tmp = (tmp1 >> 1) + (tmp1 % 2) + 5;
1319
		mmio_clrsetbits_32(PI_REG(i, 96), 0x3ff << 16, tmp << 16);
1320
1321
		/* PI_96 PI_TDFI_CALVL_CC_F1:RW:0:10 */
		tmp = tmp + 18;
1322
		mmio_clrsetbits_32(PI_REG(i, 96), 0x3ff, tmp);
1323
		/*PI_103 PI_TMRZ_F1:RW:0:5 */
1324
		mmio_clrsetbits_32(PI_REG(i, 103), 0x1f, pdram_timing->tmrz);
1325
1326
1327
1328
1329
1330
1331
		/*PI_111 PI_TDFI_CALVL_STROBE_F1:RW:16:4 */
		/* tds_train=ceil(2/ns) */
		tmp1 = 2 * 1000 / (1000000 / pdram_timing->mhz);
		if ((2 * 1000 % (1000000 / pdram_timing->mhz)) != 0)
			tmp1++;
		/* pi_tdfi_calvl_strobe=tds_train+5 */
		tmp = tmp1 + 5;
1332
1333
		mmio_clrsetbits_32(PI_REG(i, 111), 0xf << 16,
				   tmp << 16);
1334
1335
1336
1337
1338
1339
1340
1341
		/* PI_116 PI_TCKEHDQS_F1:RW:24:6 */
		tmp = 10000 / (1000000 / pdram_timing->mhz);
		if ((10000 % (1000000 / pdram_timing->mhz)) != 0)
			tmp++;
		if (pdram_timing->mhz <= 100)
			tmp = tmp + 1;
		else
			tmp = tmp + 8;
1342
1343
		mmio_clrsetbits_32(PI_REG(i, 116), 0x3f << 24,
				   tmp << 24);
1344
		/* PI_128 PI_MR1_DATA_F1_0:RW+:0:16 */
1345
		mmio_clrsetbits_32(PI_REG(i, 128), 0xffff, pdram_timing->mr[1]);
1346
		/* PI_135 PI_MR1_DATA_F1_1:RW+:8:16 */
1347
1348
		mmio_clrsetbits_32(PI_REG(i, 135), 0xffff << 8,
				   pdram_timing->mr[1] << 8);
1349
		/* PI_143 PI_MR1_DATA_F1_2:RW+:0:16 */
1350
		mmio_clrsetbits_32(PI_REG(i, 143), 0xffff, pdram_timing->mr[1]);
1351
		/* PI_150 PI_MR1_DATA_F1_3:RW+:8:16 */
1352
1353
		mmio_clrsetbits_32(PI_REG(i, 150), 0xffff << 8,
				   pdram_timing->mr[1] << 8);
1354
		/* PI_128 PI_MR2_DATA_F1_0:RW+:16:16 */
1355
1356
		mmio_clrsetbits_32(PI_REG(i, 128), 0xffff << 16,
				   pdram_timing->mr[2] << 16);
1357
		/* PI_136 PI_MR2_DATA_F1_1:RW+:0:16 */
1358
		mmio_clrsetbits_32(PI_REG(i, 136), 0xffff, pdram_timing->mr[2]);
1359
		/* PI_143 PI_MR2_DATA_F1_2:RW+:16:16 */
1360
1361
		mmio_clrsetbits_32(PI_REG(i, 143), 0xffff << 16,
				   pdram_timing->mr[2] << 16);
1362
		/* PI_151 PI_MR2_DATA_F1_3:RW+:0:16 */
1363
		mmio_clrsetbits_32(PI_REG(i, 151), 0xffff, pdram_timing->mr[2]);
1364
		/* PI_156 PI_TFC_F1:RW:16:10 */
1365
1366
		mmio_clrsetbits_32(PI_REG(i, 156), 0x3ff << 16,
				   pdram_timing->trfc << 16);
1367
		/* PI_162 PI_TWR_F1:RW:8:6 */
1368
1369
		mmio_clrsetbits_32(PI_REG(i, 162), 0x3f << 8,
				   pdram_timing->twr << 8);
1370
		/* PI_162 PI_TWTR_F1:RW:0:6 */
1371
		mmio_clrsetbits_32(PI_REG(i, 162), 0x3f, pdram_timing->twtr);
1372
		/* PI_161 PI_TRCD_F1:RW:24:8 */
1373
1374
		mmio_clrsetbits_32(PI_REG(i, 161), 0xff << 24,
				   pdram_timing->trcd << 24);
1375
		/* PI_161 PI_TRP_F1:RW:16:8 */
1376
1377
		mmio_clrsetbits_32(PI_REG(i, 161), 0xff << 16,
				   pdram_timing->trp << 16);
1378
		/* PI_161 PI_TRTP_F1:RW:8:8 */
1379
1380
		mmio_clrsetbits_32(PI_REG(i, 161), 0xff << 8,
				   pdram_timing->trtp << 8);
1381
		/* PI_163 PI_TRAS_MIN_F1:RW:24:8 */
1382
1383
		mmio_clrsetbits_32(PI_REG(i, 163), 0xff << 24,
				   pdram_timing->tras_min << 24);
1384
		/* PI_163 PI_TRAS_MAX_F1:RW:0:17 */
1385
1386
		mmio_clrsetbits_32(PI_REG(i, 163), 0x1ffff,
				   pdram_timing->tras_max * 99 / 100);
1387
		/* PI_164 PI_TMRD_F1:RW:16:6 */
1388
1389
		mmio_clrsetbits_32(PI_REG(i, 164), 0x3f << 16,
				   pdram_timing->tmrd << 16);
1390
		/* PI_164 PI_TDQSCK_MAX_F1:RW:0:4 */
1391
1392
		mmio_clrsetbits_32(PI_REG(i, 164), 0xf,
				   pdram_timing->tdqsck_max);
1393
		/* PI_189 PI_TDFI_CTRLUPD_MAX_F1:RW:0:16 */
1394
1395
		mmio_clrsetbits_32(PI_REG(i, 189), 0xffff,
				   2 * pdram_timing->trefi);
1396
		/* PI_190 PI_TDFI_CTRLUPD_INTERVAL_F1:RW:0:32 */
1397
1398
		mmio_clrsetbits_32(PI_REG(i, 190), 0xffffffff,
				   20 * pdram_timing->trefi);
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
	}
}

static void gen_rk3399_pi_params(struct timing_related_config *timing_config,
				 struct dram_timing_t *pdram_timing,
				 uint32_t fn)
{
	if (fn == 0)
		gen_rk3399_pi_params_f0(timing_config, pdram_timing);
	else
		gen_rk3399_pi_params_f1(timing_config, pdram_timing);
}

static void gen_rk3399_set_odt(uint32_t odt_en)
{
	uint32_t drv_odt_val;
	uint32_t i;

	for (i = 0; i < rk3399_dram_status.timing_config.ch_cnt; i++) {
		drv_odt_val = (odt_en | (0 << 1) | (0 << 2)) << 16;
1419
1420
1421
1422
		mmio_clrsetbits_32(PHY_REG(i, 5), 0x7 << 16, drv_odt_val);
		mmio_clrsetbits_32(PHY_REG(i, 133), 0x7 << 16, drv_odt_val);
		mmio_clrsetbits_32(PHY_REG(i, 261), 0x7 << 16, drv_odt_val);
		mmio_clrsetbits_32(PHY_REG(i, 389), 0x7 << 16, drv_odt_val);
1423
		drv_odt_val = (odt_en | (0 << 1) | (0 << 2)) << 24;
1424
1425
1426
1427
		mmio_clrsetbits_32(PHY_REG(i, 6), 0x7 << 24, drv_odt_val);
		mmio_clrsetbits_32(PHY_REG(i, 134), 0x7 << 24, drv_odt_val);
		mmio_clrsetbits_32(PHY_REG(i, 262), 0x7 << 24, drv_odt_val);
		mmio_clrsetbits_32(PHY_REG(i, 390), 0x7 << 24, drv_odt_val);
1428
1429
1430
	}
}

1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
static void gen_rk3399_phy_dll_bypass(uint32_t mhz, uint32_t ch,
		uint32_t index, uint32_t dram_type)
{
	uint32_t sw_master_mode = 0;
	uint32_t rddqs_gate_delay, rddqs_latency, total_delay;
	uint32_t i;

	if (dram_type == DDR3)
		total_delay = PI_PAD_DELAY_PS_VALUE;
	else if (dram_type == LPDDR3)
		total_delay = PI_PAD_DELAY_PS_VALUE + 2500;
	else
		total_delay = PI_PAD_DELAY_PS_VALUE + 1500;
	/* total_delay + 0.55tck */
	total_delay +=  (55 * 10000)/mhz;
	rddqs_latency = total_delay * mhz / 1000000;
	total_delay -= rddqs_latency * 1000000 / mhz;
	rddqs_gate_delay = total_delay * 0x200 * mhz / 1000000;
	if (mhz <= PHY_DLL_BYPASS_FREQ) {
		sw_master_mode = 0xc;
		mmio_setbits_32(PHY_REG(ch, 514), 1);
		mmio_setbits_32(PHY_REG(ch, 642), 1);
		mmio_setbits_32(PHY_REG(ch, 770), 1);

		/* setting bypass mode slave delay */
		for (i = 0; i < 4; i++) {
			/* wr dq delay = -180deg + (0x60 / 4) * 20ps */
			mmio_clrsetbits_32(PHY_REG(ch, 1 + 128 * i), 0x7ff << 8,
					   0x4a0 << 8);
			/* rd dqs/dq delay = (0x60 / 4) * 20ps */
			mmio_clrsetbits_32(PHY_REG(ch, 11 + 128 * i), 0x3ff,
					   0xa0);
			/* rd rddqs_gate delay */
			mmio_clrsetbits_32(PHY_REG(ch, 2 + 128 * i), 0x3ff,
					   rddqs_gate_delay);
			mmio_clrsetbits_32(PHY_REG(ch, 78 + 128 * i), 0xf,
					   rddqs_latency);
		}
		for (i = 0; i < 3; i++)
			/* adr delay */
			mmio_clrsetbits_32(PHY_REG(ch, 513 + 128 * i),
					   0x7ff << 16, 0x80 << 16);

		if ((mmio_read_32(PHY_REG(ch, 86)) & 0xc00) == 0) {
			/*
			 * old status is normal mode,
			 * and saving the wrdqs slave delay
			 */
			for (i = 0; i < 4; i++) {
				/* save and clear wr dqs slave delay */
				wrdqs_delay_val[ch][index][i] = 0x3ff &
					(mmio_read_32(PHY_REG(ch, 63 + i * 128))
					>> 16);
				mmio_clrsetbits_32(PHY_REG(ch, 63 + i * 128),
						   0x03ff << 16, 0 << 16);
				/*
				 * in normal mode the cmd may delay 1cycle by
				 * wrlvl and in bypass mode making dqs also
				 * delay 1cycle.
				 */
				mmio_clrsetbits_32(PHY_REG(ch, 78 + i * 128),
						   0x07 << 8, 0x1 << 8);
			}
		}
	} else if (mmio_read_32(PHY_REG(ch, 86)) & 0xc00) {
		/* old status is bypass mode and restore wrlvl resume */
		for (i = 0; i < 4; i++) {
			mmio_clrsetbits_32(PHY_REG(ch, 63 + i * 128),
					   0x03ff << 16,
					   (wrdqs_delay_val[ch][index][i] &
					    0x3ff) << 16);
			/* resume phy_write_path_lat_add */
			mmio_clrbits_32(PHY_REG(ch, 78 + i * 128), 0x07 << 8);
		}
	}

	/* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
	mmio_clrsetbits_32(PHY_REG(ch, 86), 0xf << 8, sw_master_mode << 8);
	mmio_clrsetbits_32(PHY_REG(ch, 214), 0xf << 8, sw_master_mode << 8);
	mmio_clrsetbits_32(PHY_REG(ch, 342), 0xf << 8, sw_master_mode << 8);
	mmio_clrsetbits_32(PHY_REG(ch, 470), 0xf << 8, sw_master_mode << 8);

	/* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
	mmio_clrsetbits_32(PHY_REG(ch, 547), 0xf << 16, sw_master_mode << 16);
	mmio_clrsetbits_32(PHY_REG(ch, 675), 0xf << 16, sw_master_mode << 16);
	mmio_clrsetbits_32(PHY_REG(ch, 803), 0xf << 16, sw_master_mode << 16);
}

1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
static void gen_rk3399_phy_params(struct timing_related_config *timing_config,
				  struct drv_odt_lp_config *drv_config,
				  struct dram_timing_t *pdram_timing,
				  uint32_t fn)
{
	uint32_t tmp, i, div, j;
	uint32_t mem_delay_ps, pad_delay_ps, total_delay_ps, delay_frac_ps;
	uint32_t trpre_min_ps, gate_delay_ps, gate_delay_frac_ps;
	uint32_t ie_enable, tsel_enable, cas_lat, rddata_en_ie_dly, tsel_adder;
	uint32_t extra_adder, delta, hs_offset;

	for (i = 0; i < timing_config->ch_cnt; i++) {

		pad_delay_ps = PI_PAD_DELAY_PS_VALUE;
		ie_enable = PI_IE_ENABLE_VALUE;
		tsel_enable = PI_TSEL_ENABLE_VALUE;

1536
		mmio_clrsetbits_32(PHY_REG(i, 896), (0x3 << 8) | 1, fn << 8);
1537
1538
1539
1540

		/* PHY_LOW_FREQ_SEL */
		/* DENALI_PHY_913 1bit offset_0 */
		if (timing_config->freq > 400)
1541
			mmio_clrbits_32(PHY_REG(i, 913), 1);
1542
		else
1543
			mmio_setbits_32(PHY_REG(i, 913), 1);
1544
1545
1546
1547
1548
1549

		/* PHY_RPTR_UPDATE_x */
		/* DENALI_PHY_87/215/343/471 4bit offset_16 */
		tmp = 2500 / (1000000 / pdram_timing->mhz) + 3;
		if ((2500 % (1000000 / pdram_timing->mhz)) != 0)
			tmp++;
1550
1551
1552
1553
		mmio_clrsetbits_32(PHY_REG(i, 87), 0xf << 16, tmp << 16);
		mmio_clrsetbits_32(PHY_REG(i, 215), 0xf << 16, tmp << 16);
		mmio_clrsetbits_32(PHY_REG(i, 343), 0xf << 16, tmp << 16);
		mmio_clrsetbits_32(PHY_REG(i, 471), 0xf << 16, tmp << 16);
1554
1555
1556
1557
1558

		/* PHY_PLL_CTRL */
		/* DENALI_PHY_911 13bits offset_0 */
		/* PHY_LP4_BOOT_PLL_CTRL */
		/* DENALI_PHY_919 13bits offset_0 */
1559
		tmp = (1 << 12) | (2 << 7) | (1 << 1);
1560
1561
		mmio_clrsetbits_32(PHY_REG(i, 911), 0x1fff, tmp);
		mmio_clrsetbits_32(PHY_REG(i, 919), 0x1fff, tmp);
1562
1563
1564
1565
1566

		/* PHY_PLL_CTRL_CA */
		/* DENALI_PHY_911 13bits offset_16 */
		/* PHY_LP4_BOOT_PLL_CTRL_CA */
		/* DENALI_PHY_919 13bits offset_16 */
1567
		tmp = (2 << 7) | (1 << 5) | (1 << 1);
1568
1569
		mmio_clrsetbits_32(PHY_REG(i, 911), 0x1fff << 16, tmp << 16);
		mmio_clrsetbits_32(PHY_REG(i, 919), 0x1fff << 16, tmp << 16);
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580

		/* PHY_TCKSRE_WAIT */
		/* DENALI_PHY_922 4bits offset_24 */
		if (pdram_timing->mhz <= 400)
			tmp = 1;
		else if (pdram_timing->mhz <= 800)
			tmp = 3;
		else if (pdram_timing->mhz <= 1000)
			tmp = 4;
		else
			tmp = 5;
1581
		mmio_clrsetbits_32(PHY_REG(i, 922), 0xf << 24, tmp << 24);
1582
1583
1584
1585
1586
1587
		/* PHY_CAL_CLK_SELECT_0:RW8:3 */
		div = pdram_timing->mhz / (2 * 20);
		for (j = 2, tmp = 1; j <= 128; j <<= 1, tmp++) {
			if (div < j)
				break;
		}
1588
		mmio_clrsetbits_32(PHY_REG(i, 947), 0x7 << 8, tmp << 8);
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603

		if (timing_config->dram_type == DDR3) {
			mem_delay_ps = 0;
			trpre_min_ps = 1000;
		} else if (timing_config->dram_type == LPDDR4) {
			mem_delay_ps = 1500;
			trpre_min_ps = 900;
		} else if (timing_config->dram_type == LPDDR3) {
			mem_delay_ps = 2500;
			trpre_min_ps = 900;
		} else {
			ERROR("gen_rk3399_phy_params:dramtype unsupport\n");
			return;
		}
		total_delay_ps = mem_delay_ps + pad_delay_ps;
1604
1605
		delay_frac_ps = 1000 * total_delay_ps /
				(1000000 / pdram_timing->mhz);
1606
		gate_delay_ps = delay_frac_ps + 1000 - (trpre_min_ps / 2);
1607
		gate_delay_frac_ps = gate_delay_ps % 1000;
1608
1609
1610
		tmp = gate_delay_frac_ps * 0x200 / 1000;
		/* PHY_RDDQS_GATE_SLAVE_DELAY */
		/* DENALI_PHY_77/205/333/461 10bits offset_16 */
1611
1612
1613
1614
		mmio_clrsetbits_32(PHY_REG(i, 77), 0x2ff << 16, tmp << 16);
		mmio_clrsetbits_32(PHY_REG(i, 205), 0x2ff << 16, tmp << 16);
		mmio_clrsetbits_32(PHY_REG(i, 333), 0x2ff << 16, tmp << 16);
		mmio_clrsetbits_32(PHY_REG(i, 461), 0x2ff << 16, tmp << 16);
1615
1616
1617
1618

		tmp = gate_delay_ps / 1000;
		/* PHY_LP4_BOOT_RDDQS_LATENCY_ADJUST */
		/* DENALI_PHY_10/138/266/394 4bit offset_0 */
1619
1620
1621
1622
		mmio_clrsetbits_32(PHY_REG(i, 10), 0xf, tmp);
		mmio_clrsetbits_32(PHY_REG(i, 138), 0xf, tmp);
		mmio_clrsetbits_32(PHY_REG(i, 266), 0xf, tmp);
		mmio_clrsetbits_32(PHY_REG(i, 394), 0xf, tmp);
1623
1624
1625
		/* PHY_GTLVL_LAT_ADJ_START */
		/* DENALI_PHY_80/208/336/464 4bits offset_16 */
		tmp = delay_frac_ps / 1000;
1626
1627
1628
1629
		mmio_clrsetbits_32(PHY_REG(i, 80), 0xf << 16, tmp << 16);
		mmio_clrsetbits_32(PHY_REG(i, 208), 0xf << 16, tmp << 16);
		mmio_clrsetbits_32(PHY_REG(i, 336), 0xf << 16, tmp << 16);
		mmio_clrsetbits_32(PHY_REG(i, 464), 0xf << 16, tmp << 16);
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647

		cas_lat = pdram_timing->cl + PI_ADD_LATENCY;
		rddata_en_ie_dly = ie_enable / (1000000 / pdram_timing->mhz);
		if ((ie_enable % (1000000 / pdram_timing->mhz)) != 0)
			rddata_en_ie_dly++;
		rddata_en_ie_dly = rddata_en_ie_dly - 1;
		tsel_adder = tsel_enable / (1000000 / pdram_timing->mhz);
		if ((tsel_enable % (1000000 / pdram_timing->mhz)) != 0)
			tsel_adder++;
		if (rddata_en_ie_dly > tsel_adder)
			extra_adder = rddata_en_ie_dly - tsel_adder;
		else
			extra_adder = 0;
		delta = cas_lat - rddata_en_ie_dly;
		if (PI_REGS_DIMM_SUPPORT && PI_DOUBLEFREEK)
			hs_offset = 2;
		else
			hs_offset = 1;
1648
		if (rddata_en_ie_dly > (cas_lat - 1 - hs_offset))
1649
			tmp = 0;
1650
1651
1652
1653
		else if ((delta == 2) || (delta == 1))
			tmp = rddata_en_ie_dly - 0 - extra_adder;
		else
			tmp = extra_adder;
1654
1655
		/* PHY_LP4_BOOT_RDDATA_EN_TSEL_DLY */
		/* DENALI_PHY_9/137/265/393 4bit offset_16 */
1656
1657
1658
1659
		mmio_clrsetbits_32(PHY_REG(i, 9), 0xf << 16, tmp << 16);
		mmio_clrsetbits_32(PHY_REG(i, 137), 0xf << 16, tmp << 16);
		mmio_clrsetbits_32(PHY_REG(i, 265), 0xf << 16, tmp << 16);
		mmio_clrsetbits_32(PHY_REG(i, 393), 0xf << 16, tmp << 16);
1660
1661
		/* PHY_RDDATA_EN_TSEL_DLY */
		/* DENALI_PHY_86/214/342/470 4bit offset_0 */
1662
1663
1664
1665
		mmio_clrsetbits_32(PHY_REG(i, 86), 0xf, tmp);
		mmio_clrsetbits_32(PHY_REG(i, 214), 0xf, tmp);
		mmio_clrsetbits_32(PHY_REG(i, 342), 0xf, tmp);
		mmio_clrsetbits_32(PHY_REG(i, 470), 0xf, tmp);
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676

		if (tsel_adder > rddata_en_ie_dly)
			extra_adder = tsel_adder - rddata_en_ie_dly;
		else
			extra_adder = 0;
		if (rddata_en_ie_dly > (cas_lat - 1 - hs_offset))
			tmp = tsel_adder;
		else
			tmp = rddata_en_ie_dly - 0 + extra_adder;
		/* PHY_LP4_BOOT_RDDATA_EN_DLY */
		/* DENALI_PHY_9/137/265/393 4bit offset_8 */
1677
1678
1679
1680
		mmio_clrsetbits_32(PHY_REG(i, 9), 0xf << 8, tmp << 8);
		mmio_clrsetbits_32(PHY_REG(i, 137), 0xf << 8, tmp << 8);
		mmio_clrsetbits_32(PHY_REG(i, 265), 0xf << 8, tmp << 8);
		mmio_clrsetbits_32(PHY_REG(i, 393), 0xf << 8, tmp << 8);
1681
1682
		/* PHY_RDDATA_EN_DLY */
		/* DENALI_PHY_85/213/341/469 4bit offset_24 */
1683
1684
1685
1686
		mmio_clrsetbits_32(PHY_REG(i, 85), 0xf << 24, tmp << 24);
		mmio_clrsetbits_32(PHY_REG(i, 213), 0xf << 24, tmp << 24);
		mmio_clrsetbits_32(PHY_REG(i, 341), 0xf << 24, tmp << 24);
		mmio_clrsetbits_32(PHY_REG(i, 469), 0xf << 24, tmp << 24);
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696

		if (pdram_timing->mhz <= ENPER_CS_TRAINING_FREQ) {
			/*
			 * Note:Per-CS Training is not compatible at speeds
			 * under 533 MHz. If the PHY is running at a speed
			 * less than 533MHz, all phy_per_cs_training_en_X
			 * parameters must be cleared to 0.
			 */

			/*DENALI_PHY_84/212/340/468 1bit offset_16 */
1697
1698
1699
1700
			mmio_clrbits_32(PHY_REG(i, 84), 0x1 << 16);
			mmio_clrbits_32(PHY_REG(i, 212), 0x1 << 16);
			mmio_clrbits_32(PHY_REG(i, 340), 0x1 << 16);
			mmio_clrbits_32(PHY_REG(i, 468), 0x1 << 16);
1701
		} else {
1702
1703
1704
1705
			mmio_setbits_32(PHY_REG(i, 84), 0x1 << 16);
			mmio_setbits_32(PHY_REG(i, 212), 0x1 << 16);
			mmio_setbits_32(PHY_REG(i, 340), 0x1 << 16);
			mmio_setbits_32(PHY_REG(i, 468), 0x1 << 16);
1706
		}
1707
1708
		gen_rk3399_phy_dll_bypass(pdram_timing->mhz, i, fn,
					  timing_config->dram_type);
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
	}
}

static int to_get_clk_index(unsigned int mhz)
{
	int pll_cnt, i;

	pll_cnt = ARRAY_SIZE(dpll_rates_table);

	/* Assumming rate_table is in descending order */
	for (i = 0; i < pll_cnt; i++) {
		if (mhz >= dpll_rates_table[i].mhz)
			break;
	}

	/* if mhz lower than lowest frequency in table, use lowest frequency */
	if (i == pll_cnt)
		i = pll_cnt - 1;

	return i;
}

uint32_t ddr_get_rate(void)
{
	uint32_t refdiv, postdiv1, fbdiv, postdiv2;

	refdiv = mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 1)) & 0x3f;
	fbdiv = mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 0)) & 0xfff;
	postdiv1 =
		(mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 1)) >> 8) & 0x7;
	postdiv2 =
		(mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 1)) >> 12) & 0x7;

	return (24 / refdiv * fbdiv / postdiv1 / postdiv2) * 1000 * 1000;
}

/*
 * return: bit12: channel 1, external self-refresh
 *         bit11: channel 1, stdby_mode
 *         bit10: channel 1, self-refresh with controller and memory clock gate
 *         bit9: channel 1, self-refresh
 *         bit8: channel 1, power-down
 *
 *         bit4: channel 1, external self-refresh
 *         bit3: channel 0, stdby_mode
 *         bit2: channel 0, self-refresh with controller and memory clock gate
 *         bit1: channel 0, self-refresh
 *         bit0: channel 0, power-down
 */
uint32_t exit_low_power(void)
{
	uint32_t low_power = 0;
	uint32_t channel_mask;
1762
	uint32_t tmp, i;
1763

1764
1765
1766
1767
	channel_mask = (mmio_read_32(PMUGRF_BASE + PMUGRF_OSREG(2)) >> 28) &
			0x3;
	for (i = 0; i < 2; i++) {
		if (!(channel_mask & (1 << i)))
1768
1769
1770
			continue;

		/* exit stdby mode */
1771
1772
		mmio_write_32(CIC_BASE + CIC_CTRL1,
			      (1 << (i + 16)) | (0 << i));
1773
		/* exit external self-refresh */
1774
1775
1776
1777
1778
		tmp = i ? 12 : 8;
		low_power |= ((mmio_read_32(PMU_BASE + PMU_SFT_CON) >> tmp) &
			      0x1) << (4 + 8 * i);
		mmio_clrbits_32(PMU_BASE + PMU_SFT_CON, 1 << tmp);
		while (!(mmio_read_32(PMU_BASE + PMU_DDR_SREF_ST) & (1 << i)))
1779
1780
			;
		/* exit auto low-power */
1781
		mmio_clrbits_32(CTL_REG(i, 101), 0x7);
1782
		/* lp_cmd to exit */
1783
1784
1785
		if (((mmio_read_32(CTL_REG(i, 100)) >> 24) & 0x7f) !=
		    0x40) {
			while (mmio_read_32(CTL_REG(i, 200)) & 0x1)
1786
				;
1787
1788
1789
1790
			mmio_clrsetbits_32(CTL_REG(i, 93), 0xff << 24,
					   0x69 << 24);
			while (((mmio_read_32(CTL_REG(i, 100)) >> 24) & 0x7f) !=
			       0x40)
1791
1792
1793
1794
1795
1796
1797
1798
1799
				;
		}
	}
	return low_power;
}

void resume_low_power(uint32_t low_power)
{
	uint32_t channel_mask;
1800
	uint32_t tmp, i, val;
1801

1802
1803
1804
1805
	channel_mask = (mmio_read_32(PMUGRF_BASE + PMUGRF_OSREG(2)) >> 28) &
		       0x3;
	for (i = 0; i < 2; i++) {
		if (!(channel_mask & (1 << i)))
1806
1807
1808
			continue;

		/* resume external self-refresh */
1809
1810
1811
		tmp = i ? 12 : 8;
		val = (low_power >> (4 + 8 * i)) & 0x1;
		mmio_setbits_32(PMU_BASE + PMU_SFT_CON, val << tmp);
1812
		/* resume auto low-power */
1813
1814
		val = (low_power >> (8 * i)) & 0x7;
		mmio_setbits_32(CTL_REG(i, 101), val);
1815
		/* resume stdby mode */
1816
1817
1818
		val = (low_power >> (3 + 8 * i)) & 0x1;
		mmio_write_32(CIC_BASE + CIC_CTRL1,
			      (1 << (i + 16)) | (val << i));
1819
1820
1821
	}
}

1822
static void dram_low_power_config(void)
1823
{
1824
	uint32_t tmp, i;
1825
1826
1827
1828
	uint32_t ch_cnt = rk3399_dram_status.timing_config.ch_cnt;
	uint32_t dram_type = rk3399_dram_status.timing_config.dram_type;

	if (dram_type == DDR3)
1829
		tmp = (2 << 16) | (0x7 << 8);
1830
	else
1831
		tmp = (3 << 16) | (0x7 << 8);
1832

1833
1834
	for (i = 0; i < ch_cnt; i++)
		mmio_clrsetbits_32(CTL_REG(i, 101), 0x70f0f, tmp);
1835
1836

	/* standby idle */
1837
	mmio_write_32(CIC_BASE + CIC_CG_WAIT_TH, 0x640008);
1838
1839

	if (ch_cnt == 2) {
1840
1841
1842
1843
		mmio_write_32(GRF_BASE + GRF_DDRC1_CON1,
			      (((0x1<<4) | (0x1<<5) | (0x1<<6) |
				(0x1<<7)) << 16) |
			      ((0x1<<4) | (0x0<<5) | (0x1<<6) | (0x1<<7)));
1844
		mmio_write_32(CIC_BASE + CIC_CTRL1, 0x002a0028);
1845
1846
	}

1847
1848
1849
	mmio_write_32(GRF_BASE + GRF_DDRC0_CON1,
		      (((0x1<<4) | (0x1<<5) | (0x1<<6) | (0x1<<7)) << 16) |
		      ((0x1<<4) | (0x0<<5) | (0x1<<6) | (0x1<<7)));
1850
	mmio_write_32(CIC_BASE + CIC_CTRL1, 0x00150014);
1851
1852
}

1853
void dram_dfs_init(void)
1854
{
1855
	uint32_t trefi0, trefi1, boot_freq;
1856
1857

	/* get sdram config for os reg */
1858
1859
	get_dram_drv_odt_val(sdram_config.dramtype,
			     &rk3399_dram_status.drv_odt_lp_cfg);
1860
1861
1862
1863
	sdram_timing_cfg_init(&rk3399_dram_status.timing_config,
			      &sdram_config,
			      &rk3399_dram_status.drv_odt_lp_cfg);

1864
1865
	trefi0 = ((mmio_read_32(CTL_REG(0, 48)) >> 16) & 0xffff) + 8;
	trefi1 = ((mmio_read_32(CTL_REG(0, 49)) >> 16) & 0xffff) + 8;
1866
1867
1868
1869

	rk3399_dram_status.index_freq[0] = trefi0 * 10 / 39;
	rk3399_dram_status.index_freq[1] = trefi1 * 10 / 39;
	rk3399_dram_status.current_index =
1870
		(mmio_read_32(CTL_REG(0, 111)) >> 16) & 0x3;
1871
1872
1873
1874
	if (rk3399_dram_status.timing_config.dram_type == DDR3) {
		rk3399_dram_status.index_freq[0] /= 2;
		rk3399_dram_status.index_freq[1] /= 2;
	}
1875
1876
1877
1878
1879
1880
1881
1882
1883
	boot_freq =
		rk3399_dram_status.index_freq[rk3399_dram_status.current_index];
	boot_freq = dpll_rates_table[to_get_clk_index(boot_freq)].mhz;
	rk3399_dram_status.boot_freq = boot_freq;
	rk3399_dram_status.index_freq[rk3399_dram_status.current_index] =
		boot_freq;
	rk3399_dram_status.index_freq[(rk3399_dram_status.current_index + 1) &
				      0x1] = 0;
	rk3399_dram_status.low_power_stat = 0;
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
	/*
	 * following register decide if NOC stall the access request
	 * or return error when NOC being idled. when doing ddr frequency
	 * scaling in M0 or DCF, we need to make sure noc stall the access
	 * request, if return error cpu may data abort when ddr frequency
	 * changing. it don't need to set this register every times,
	 * so we init this register in function dram_dfs_init().
	 */
	mmio_write_32(GRF_BASE + GRF_SOC_CON(0), 0xffffffff);
	mmio_write_32(GRF_BASE + GRF_SOC_CON(1), 0xffffffff);
	mmio_write_32(GRF_BASE + GRF_SOC_CON(2), 0xffffffff);
	mmio_write_32(GRF_BASE + GRF_SOC_CON(3), 0xffffffff);
	mmio_write_32(GRF_BASE + GRF_SOC_CON(4), 0x70007000);

1898
1899
1900
1901
	/* Disable multicast */
	mmio_clrbits_32(PHY_REG(0, 896), 1);
	mmio_clrbits_32(PHY_REG(1, 896), 1);

1902
1903
	dram_low_power_config();
}
1904

1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
/*
 * arg0: bit0-7: sr_idle; bit8-15:sr_mc_gate_idle; bit16-31: standby idle
 * arg1: bit0-11: pd_idle; bit 16-27: srpd_lite_idle
 * arg2: bit0: if odt en
 */
uint32_t dram_set_odt_pd(uint32_t arg0, uint32_t arg1, uint32_t arg2)
{
	struct drv_odt_lp_config *lp_cfg = &rk3399_dram_status.drv_odt_lp_cfg;
	uint32_t *low_power = &rk3399_dram_status.low_power_stat;
	uint32_t dram_type, ch_count, pd_tmp, sr_tmp, i;

	dram_type = rk3399_dram_status.timing_config.dram_type;
	ch_count = rk3399_dram_status.timing_config.ch_cnt;

	lp_cfg->sr_idle = arg0 & 0xff;
	lp_cfg->sr_mc_gate_idle = (arg0 >> 8) & 0xff;
	lp_cfg->standby_idle = (arg0 >> 16) & 0xffff;
	lp_cfg->pd_idle = arg1 & 0xfff;
	lp_cfg->srpd_lite_idle = (arg1 >> 16) & 0xfff;

	rk3399_dram_status.timing_config.odt = arg2 & 0x1;

	exit_low_power();

	*low_power = 0;

	/* pd_idle en */
	if (lp_cfg->pd_idle)
		*low_power |= ((1 << 0) | (1 << 8));
	/* sr_idle en srpd_lite_idle */
	if (lp_cfg->sr_idle | lp_cfg->srpd_lite_idle)
		*low_power |= ((1 << 1) | (1 << 9));
	/* sr_mc_gate_idle */
	if (lp_cfg->sr_mc_gate_idle)
		*low_power |= ((1 << 2) | (1 << 10));
	/* standbyidle */
	if (lp_cfg->standby_idle) {
		if (rk3399_dram_status.timing_config.ch_cnt == 2)
			*low_power |= ((1 << 3) | (1 << 11));
		else
			*low_power |= (1 << 3);
1946
1947
	}

1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
	pd_tmp = arg1;
	if (dram_type != LPDDR4)
		pd_tmp = arg1 & 0xfff;
	sr_tmp = arg0 & 0xffff;
	for (i = 0; i < ch_count; i++) {
		mmio_write_32(CTL_REG(i, 102), pd_tmp);
		mmio_clrsetbits_32(CTL_REG(i, 103), 0xffff, sr_tmp);
	}
	mmio_write_32(CIC_BASE + CIC_IDLE_TH, (arg0 >> 16) & 0xffff);

	return 0;
1959
1960
}

1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
static void m0_configure_ddr(struct pll_div pll_div, uint32_t ddr_index)
{
	/* set PARAM to M0_FUNC_DRAM */
	mmio_write_32(M0_PARAM_ADDR + PARAM_M0_FUNC, M0_FUNC_DRAM);

	mmio_write_32(M0_PARAM_ADDR + PARAM_DPLL_CON0, FBDIV(pll_div.fbdiv));
	mmio_write_32(M0_PARAM_ADDR + PARAM_DPLL_CON1,
		      POSTDIV2(pll_div.postdiv2) | POSTDIV1(pll_div.postdiv1) |
		      REFDIV(pll_div.refdiv));

	mmio_write_32(M0_PARAM_ADDR + PARAM_DRAM_FREQ, pll_div.mhz);

	mmio_write_32(M0_PARAM_ADDR + PARAM_FREQ_SELECT, ddr_index << 4);
1974
	dmbst();
1975
1976
}

1977
1978
1979
1980
1981
1982
1983
static uint32_t prepare_ddr_timing(uint32_t mhz)
{
	uint32_t index;
	struct dram_timing_t dram_timing;

	rk3399_dram_status.timing_config.freq = mhz;

1984
	if (mhz < 300)
1985
1986
1987
		rk3399_dram_status.timing_config.dllbp = 1;
	else
		rk3399_dram_status.timing_config.dllbp = 0;
1988
1989

	if (rk3399_dram_status.timing_config.odt == 1)
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
		gen_rk3399_set_odt(1);

	index = (rk3399_dram_status.current_index + 1) & 0x1;

	/*
	 * checking if having available gate traiing timing for
	 * target freq.
	 */
	dram_get_parameter(&rk3399_dram_status.timing_config, &dram_timing);
	gen_rk3399_ctl_params(&rk3399_dram_status.timing_config,
			      &dram_timing, index);
	gen_rk3399_pi_params(&rk3399_dram_status.timing_config,
			     &dram_timing, index);
	gen_rk3399_phy_params(&rk3399_dram_status.timing_config,
			      &rk3399_dram_status.drv_odt_lp_cfg,
			      &dram_timing, index);
	rk3399_dram_status.index_freq[index] = mhz;

	return index;
}

void print_dram_status_info(void)
{
	uint32_t *p;
	uint32_t i;

	p = (uint32_t *) &rk3399_dram_status.timing_config;
	INFO("rk3399_dram_status.timing_config:\n");
	for (i = 0; i < sizeof(struct timing_related_config) / 4; i++)
		tf_printf("%u\n", p[i]);
	p = (uint32_t *) &rk3399_dram_status.drv_odt_lp_cfg;
	INFO("rk3399_dram_status.drv_odt_lp_cfg:\n");
	for (i = 0; i < sizeof(struct drv_odt_lp_config) / 4; i++)
		tf_printf("%u\n", p[i]);
}

uint32_t ddr_set_rate(uint32_t hz)
{
2028
	uint32_t low_power, index, ddr_index;
2029
2030
2031
2032
	uint32_t mhz = hz / (1000 * 1000);

	if (mhz ==
	    rk3399_dram_status.index_freq[rk3399_dram_status.current_index])
2033
		return mhz;
2034
2035
2036
2037

	index = to_get_clk_index(mhz);
	mhz = dpll_rates_table[index].mhz;

2038
	ddr_index = prepare_ddr_timing(mhz);
2039
2040
	gen_rk3399_enable_training(rk3399_dram_status.timing_config.ch_cnt,
				   mhz);
2041
	if (ddr_index > 1)
2042
2043
		goto out;

2044
2045
2046
2047
	/*
	 * Make sure the clock is enabled. The M0 clocks should be on all of the
	 * time during S0.
	 */
2048
2049
2050
2051
2052
	m0_configure_ddr(dpll_rates_table[index], ddr_index);
	m0_start();
	m0_wait_done();
	m0_stop();

2053
2054
2055
	if (rk3399_dram_status.timing_config.odt == 0)
		gen_rk3399_set_odt(0);

2056
	rk3399_dram_status.current_index = ddr_index;
2057
	low_power = rk3399_dram_status.low_power_stat;
2058
2059
	resume_low_power(low_power);
out:
2060
	gen_rk3399_disable_training(rk3399_dram_status.timing_config.ch_cnt);
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
	return mhz;
}

uint32_t ddr_round_rate(uint32_t hz)
{
	int index;
	uint32_t mhz = hz / (1000 * 1000);

	index = to_get_clk_index(mhz);

	return dpll_rates_table[index].mhz * 1000 * 1000;
}
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126

void ddr_prepare_for_sys_suspend(void)
{
	uint32_t mhz =
		rk3399_dram_status.index_freq[rk3399_dram_status.current_index];

	/*
	 * If we're not currently at the boot (assumed highest) frequency, we
	 * need to change frequencies to configure out current index.
	 */
	rk3399_suspend_status.freq = mhz;
	exit_low_power();
	rk3399_suspend_status.low_power_stat =
		rk3399_dram_status.low_power_stat;
	rk3399_suspend_status.odt = rk3399_dram_status.timing_config.odt;
	rk3399_dram_status.low_power_stat = 0;
	rk3399_dram_status.timing_config.odt = 1;
	if (mhz != rk3399_dram_status.boot_freq)
		ddr_set_rate(rk3399_dram_status.boot_freq * 1000 * 1000);

	/*
	 * This will configure the other index to be the same frequency as the
	 * current one. We retrain both indices on resume, so both have to be
	 * setup for the same frequency.
	 */
	prepare_ddr_timing(rk3399_dram_status.boot_freq);
}

void ddr_prepare_for_sys_resume(void)
{
	/* Disable multicast */
	mmio_clrbits_32(PHY_REG(0, 896), 1);
	mmio_clrbits_32(PHY_REG(1, 896), 1);

	/* The suspend code changes the current index, so reset it now. */
	rk3399_dram_status.current_index =
		(mmio_read_32(CTL_REG(0, 111)) >> 16) & 0x3;
	rk3399_dram_status.low_power_stat =
		rk3399_suspend_status.low_power_stat;
	rk3399_dram_status.timing_config.odt = rk3399_suspend_status.odt;

	/*
	 * Set the saved frequency from suspend if it's different than the
	 * current frequency.
	 */
	if (rk3399_suspend_status.freq !=
	    rk3399_dram_status.index_freq[rk3399_dram_status.current_index]) {
		ddr_set_rate(rk3399_suspend_status.freq * 1000 * 1000);
		return;
	}

	gen_rk3399_set_odt(rk3399_dram_status.timing_config.odt);
	resume_low_power(rk3399_dram_status.low_power_stat);
}