summaryrefslogtreecommitdiffstats
path: root/drivers/memory/tegra/tegra210-emc-cc-r21021.c
blob: 0ebfa8eccf0cb3047c4fbc2a9619a73e07f7181b (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
238
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
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
575
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
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
824
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
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
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
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
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
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2014-2020, NVIDIA CORPORATION.  All rights reserved.
 */

#include <linux/kernel.h>
#include <linux/io.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/of.h>

#include <soc/tegra/mc.h>

#include "tegra210-emc.h"
#include "tegra210-mc.h"

/*
 * Enable flags for specifying verbosity.
 */
#define INFO            (1 << 0)
#define STEPS           (1 << 1)
#define SUB_STEPS       (1 << 2)
#define PRELOCK         (1 << 3)
#define PRELOCK_STEPS   (1 << 4)
#define ACTIVE_EN       (1 << 5)
#define PRAMP_UP        (1 << 6)
#define PRAMP_DN        (1 << 7)
#define EMA_WRITES      (1 << 10)
#define EMA_UPDATES     (1 << 11)
#define PER_TRAIN       (1 << 16)
#define CC_PRINT        (1 << 17)
#define CCFIFO          (1 << 29)
#define REGS            (1 << 30)
#define REG_LISTS       (1 << 31)

#define emc_dbg(emc, flags, ...) dev_dbg(emc->dev, __VA_ARGS__)

#define DVFS_CLOCK_CHANGE_VERSION	21021
#define EMC_PRELOCK_VERSION		2101

enum {
	DVFS_SEQUENCE = 1,
	WRITE_TRAINING_SEQUENCE = 2,
	PERIODIC_TRAINING_SEQUENCE = 3,
	DVFS_PT1 = 10,
	DVFS_UPDATE = 11,
	TRAINING_PT1 = 12,
	TRAINING_UPDATE = 13,
	PERIODIC_TRAINING_UPDATE = 14
};

/*
 * PTFV defines - basically just indexes into the per table PTFV array.
 */
#define PTFV_DQSOSC_MOVAVG_C0D0U0_INDEX		0
#define PTFV_DQSOSC_MOVAVG_C0D0U1_INDEX		1
#define PTFV_DQSOSC_MOVAVG_C0D1U0_INDEX		2
#define PTFV_DQSOSC_MOVAVG_C0D1U1_INDEX		3
#define PTFV_DQSOSC_MOVAVG_C1D0U0_INDEX		4
#define PTFV_DQSOSC_MOVAVG_C1D0U1_INDEX		5
#define PTFV_DQSOSC_MOVAVG_C1D1U0_INDEX		6
#define PTFV_DQSOSC_MOVAVG_C1D1U1_INDEX		7
#define PTFV_DVFS_SAMPLES_INDEX			9
#define PTFV_MOVAVG_WEIGHT_INDEX		10
#define PTFV_CONFIG_CTRL_INDEX			11

#define PTFV_CONFIG_CTRL_USE_PREVIOUS_EMA	(1 << 0)

/*
 * Do arithmetic in fixed point.
 */
#define MOVAVG_PRECISION_FACTOR		100

/*
 * The division portion of the average operation.
 */
#define __AVERAGE_PTFV(dev)						\
	({ next->ptfv_list[PTFV_DQSOSC_MOVAVG_ ## dev ## _INDEX] =	\
	   next->ptfv_list[PTFV_DQSOSC_MOVAVG_ ## dev ## _INDEX] /	\
	   next->ptfv_list[PTFV_DVFS_SAMPLES_INDEX]; })

/*
 * Convert val to fixed point and add it to the temporary average.
 */
#define __INCREMENT_PTFV(dev, val)					\
	({ next->ptfv_list[PTFV_DQSOSC_MOVAVG_ ## dev ## _INDEX] +=	\
	   ((val) * MOVAVG_PRECISION_FACTOR); })

/*
 * Convert a moving average back to integral form and return the value.
 */
#define __MOVAVG_AC(timing, dev)					\
	((timing)->ptfv_list[PTFV_DQSOSC_MOVAVG_ ## dev ## _INDEX] /	\
	 MOVAVG_PRECISION_FACTOR)

/* Weighted update. */
#define __WEIGHTED_UPDATE_PTFV(dev, nval)				\
	do {								\
		int w = PTFV_MOVAVG_WEIGHT_INDEX;			\
		int dqs = PTFV_DQSOSC_MOVAVG_ ## dev ## _INDEX;		\
									\
		next->ptfv_list[dqs] =					\
			((nval * MOVAVG_PRECISION_FACTOR) +		\
			 (next->ptfv_list[dqs] *			\
			  next->ptfv_list[w])) /			\
			(next->ptfv_list[w] + 1);			\
									\
		emc_dbg(emc, EMA_UPDATES, "%s: (s=%lu) EMA: %u\n",	\
			__stringify(dev), nval, next->ptfv_list[dqs]);	\
	} while (0)

/* Access a particular average. */
#define __MOVAVG(timing, dev)                      \
	((timing)->ptfv_list[PTFV_DQSOSC_MOVAVG_ ## dev ## _INDEX])

static u32 update_clock_tree_delay(struct tegra210_emc *emc, int type)
{
	bool periodic_training_update = type == PERIODIC_TRAINING_UPDATE;
	struct tegra210_emc_timing *last = emc->last;
	struct tegra210_emc_timing *next = emc->next;
	u32 last_timing_rate_mhz = last->rate / 1000;
	u32 next_timing_rate_mhz = next->rate / 1000;
	bool dvfs_update = type == DVFS_UPDATE;
	s32 tdel = 0, tmdel = 0, adel = 0;
	bool dvfs_pt1 = type == DVFS_PT1;
	unsigned long cval = 0;
	u32 temp[2][2], value;
	unsigned int i;

	/*
	 * Dev0 MSB.
	 */
	if (dvfs_pt1 || periodic_training_update) {
		value = tegra210_emc_mrr_read(emc, 2, 19);

		for (i = 0; i < emc->num_channels; i++) {
			temp[i][0] = (value & 0x00ff) << 8;
			temp[i][1] = (value & 0xff00) << 0;
			value >>= 16;
		}

		/*
		 * Dev0 LSB.
		 */
		value = tegra210_emc_mrr_read(emc, 2, 18);

		for (i = 0; i < emc->num_channels; i++) {
			temp[i][0] |= (value & 0x00ff) >> 0;
			temp[i][1] |= (value & 0xff00) >> 8;
			value >>= 16;
		}
	}

	if (dvfs_pt1 || periodic_training_update) {
		cval = tegra210_emc_actual_osc_clocks(last->run_clocks);
		cval *= 1000000;
		cval /= last_timing_rate_mhz * 2 * temp[0][0];
	}

	if (dvfs_pt1)
		__INCREMENT_PTFV(C0D0U0, cval);
	else if (dvfs_update)
		__AVERAGE_PTFV(C0D0U0);
	else if (periodic_training_update)
		__WEIGHTED_UPDATE_PTFV(C0D0U0, cval);

	if (dvfs_update || periodic_training_update) {
		tdel = next->current_dram_clktree[C0D0U0] -
				__MOVAVG_AC(next, C0D0U0);
		tmdel = (tdel < 0) ? -1 * tdel : tdel;
		adel = tmdel;

		if (tmdel * 128 * next_timing_rate_mhz / 1000000 >
		    next->tree_margin)
			next->current_dram_clktree[C0D0U0] =
				__MOVAVG_AC(next, C0D0U0);
	}

	if (dvfs_pt1 || periodic_training_update) {
		cval = tegra210_emc_actual_osc_clocks(last->run_clocks);
		cval *= 1000000;
		cval /= last_timing_rate_mhz * 2 * temp[0][1];
	}

	if (dvfs_pt1)
		__INCREMENT_PTFV(C0D0U1, cval);
	else if (dvfs_update)
		__AVERAGE_PTFV(C0D0U1);
	else if (periodic_training_update)
		__WEIGHTED_UPDATE_PTFV(C0D0U1, cval);

	if (dvfs_update || periodic_training_update) {
		tdel = next->current_dram_clktree[C0D0U1] -
				__MOVAVG_AC(next, C0D0U1);
		tmdel = (tdel < 0) ? -1 * tdel : tdel;

		if (tmdel > adel)
			adel = tmdel;

		if (tmdel * 128 * next_timing_rate_mhz / 1000000 >
		    next->tree_margin)
			next->current_dram_clktree[C0D0U1] =
				__MOVAVG_AC(next, C0D0U1);
	}

	if (emc->num_channels > 1) {
		if (dvfs_pt1 || periodic_training_update) {
			cval = tegra210_emc_actual_osc_clocks(last->run_clocks);
			cval *= 1000000;
			cval /= last_timing_rate_mhz * 2 * temp[1][0];
		}

		if (dvfs_pt1)
			__INCREMENT_PTFV(C1D0U0, cval);
		else if (dvfs_update)
			__AVERAGE_PTFV(C1D0U0);
		else if (periodic_training_update)
			__WEIGHTED_UPDATE_PTFV(C1D0U0, cval);

		if (dvfs_update || periodic_training_update) {
			tdel = next->current_dram_clktree[C1D0U0] -
					__MOVAVG_AC(next, C1D0U0);
			tmdel = (tdel < 0) ? -1 * tdel : tdel;

			if (tmdel > adel)
				adel = tmdel;

			if (tmdel * 128 * next_timing_rate_mhz / 1000000 >
			    next->tree_margin)
				next->current_dram_clktree[C1D0U0] =
					__MOVAVG_AC(next, C1D0U0);
		}

		if (dvfs_pt1 || periodic_training_update) {
			cval = tegra210_emc_actual_osc_clocks(last->run_clocks);
			cval *= 1000000;
			cval /= last_timing_rate_mhz * 2 * temp[1][1];
		}

		if (dvfs_pt1)
			__INCREMENT_PTFV(C1D0U1, cval);
		else if (dvfs_update)
			__AVERAGE_PTFV(C1D0U1);
		else if (periodic_training_update)
			__WEIGHTED_UPDATE_PTFV(C1D0U1, cval);

		if (dvfs_update || periodic_training_update) {
			tdel = next->current_dram_clktree[C1D0U1] -
					__MOVAVG_AC(next, C1D0U1);
			tmdel = (tdel < 0) ? -1 * tdel : tdel;

			if (tmdel > adel)
				adel = tmdel;

			if (tmdel * 128 * next_timing_rate_mhz / 1000000 >
			    next->tree_margin)
				next->current_dram_clktree[C1D0U1] =
					__MOVAVG_AC(next, C1D0U1);
		}
	}

	if (emc->num_devices < 2)
		goto done;

	/*
	 * Dev1 MSB.
	 */
	if (dvfs_pt1 || periodic_training_update) {
		value = tegra210_emc_mrr_read(emc, 1, 19);

		for (i = 0; i < emc->num_channels; i++) {
			temp[i][0] = (value & 0x00ff) << 8;
			temp[i][1] = (value & 0xff00) << 0;
			value >>= 16;
		}

		/*
		 * Dev1 LSB.
		 */
		value = tegra210_emc_mrr_read(emc, 2, 18);

		for (i = 0; i < emc->num_channels; i++) {
			temp[i][0] |= (value & 0x00ff) >> 0;
			temp[i][1] |= (value & 0xff00) >> 8;
			value >>= 16;
		}
	}

	if (dvfs_pt1 || periodic_training_update) {
		cval = tegra210_emc_actual_osc_clocks(last->run_clocks);
		cval *= 1000000;
		cval /= last_timing_rate_mhz * 2 * temp[0][0];
	}

	if (dvfs_pt1)
		__INCREMENT_PTFV(C0D1U0, cval);
	else if (dvfs_update)
		__AVERAGE_PTFV(C0D1U0);
	else if (periodic_training_update)
		__WEIGHTED_UPDATE_PTFV(C0D1U0, cval);

	if (dvfs_update || periodic_training_update) {
		tdel = next->current_dram_clktree[C0D1U0] -
				__MOVAVG_AC(next, C0D1U0);
		tmdel = (tdel < 0) ? -1 * tdel : tdel;

		if (tmdel > adel)
			adel = tmdel;

		if (tmdel * 128 * next_timing_rate_mhz / 1000000 >
		    next->tree_margin)
			next->current_dram_clktree[C0D1U0] =
				__MOVAVG_AC(next, C0D1U0);
	}

	if (dvfs_pt1 || periodic_training_update) {
		cval = tegra210_emc_actual_osc_clocks(last->run_clocks);
		cval *= 1000000;
		cval /= last_timing_rate_mhz * 2 * temp[0][1];
	}

	if (dvfs_pt1)
		__INCREMENT_PTFV(C0D1U1, cval);
	else if (dvfs_update)
		__AVERAGE_PTFV(C0D1U1);
	else if (periodic_training_update)
		__WEIGHTED_UPDATE_PTFV(C0D1U1, cval);

	if (dvfs_update || periodic_training_update) {
		tdel = next->current_dram_clktree[C0D1U1] -
				__MOVAVG_AC(next, C0D1U1);
		tmdel = (tdel < 0) ? -1 * tdel : tdel;

		if (tmdel > adel)
			adel = tmdel;

		if (tmdel * 128 * next_timing_rate_mhz / 1000000 >
		    next->tree_margin)
			next->current_dram_clktree[C0D1U1] =
				__MOVAVG_AC(next, C0D1U1);
	}

	if (emc->num_channels > 1) {
		if (dvfs_pt1 || periodic_training_update) {
			cval = tegra210_emc_actual_osc_clocks(last->run_clocks);
			cval *= 1000000;
			cval /= last_timing_rate_mhz * 2 * temp[1][0];
		}

		if (dvfs_pt1)
			__INCREMENT_PTFV(C1D1U0, cval);
		else if (dvfs_update)
			__AVERAGE_PTFV(C1D1U0);
		else if (periodic_training_update)
			__WEIGHTED_UPDATE_PTFV(C1D1U0, cval);

		if (dvfs_update || periodic_training_update) {
			tdel = next->current_dram_clktree[C1D1U0] -
					__MOVAVG_AC(next, C1D1U0);
			tmdel = (tdel < 0) ? -1 * tdel : tdel;

			if (tmdel > adel)
				adel = tmdel;

			if (tmdel * 128 * next_timing_rate_mhz / 1000000 >
			    next->tree_margin)
				next->current_dram_clktree[C1D1U0] =
					__MOVAVG_AC(next, C1D1U0);
		}

		if (dvfs_pt1 || periodic_training_update) {
			cval = tegra210_emc_actual_osc_clocks(last->run_clocks);
			cval *= 1000000;
			cval /= last_timing_rate_mhz * 2 * temp[1][1];
		}

		if (dvfs_pt1)
			__INCREMENT_PTFV(C1D1U1, cval);
		else if (dvfs_update)
			__AVERAGE_PTFV(C1D1U1);
		else if (periodic_training_update)
			__WEIGHTED_UPDATE_PTFV(C1D1U1, cval);

		if (dvfs_update || periodic_training_update) {
			tdel = next->current_dram_clktree[C1D1U1] -
					__MOVAVG_AC(next, C1D1U1);
			tmdel = (tdel < 0) ? -1 * tdel : tdel;

			if (tmdel > adel)
				adel = tmdel;

			if (tmdel * 128 * next_timing_rate_mhz / 1000000 >
			    next->tree_margin)
				next->current_dram_clktree[C1D1U1] =
					__MOVAVG_AC(next, C1D1U1);
		}
	}

done:
	return adel;
}

static u32 periodic_compensation_handler(struct tegra210_emc *emc, u32 type,
					 struct tegra210_emc_timing *last,
					 struct tegra210_emc_timing *next)
{
#define __COPY_EMA(nt, lt, dev)						\
	({ __MOVAVG(nt, dev) = __MOVAVG(lt, dev) *			\
	   (nt)->ptfv_list[PTFV_DVFS_SAMPLES_INDEX]; })

	u32 i, adel = 0, samples = next->ptfv_list[PTFV_DVFS_SAMPLES_INDEX];
	u32 delay;

	delay = tegra210_emc_actual_osc_clocks(last->run_clocks);
	delay *= 1000;
	delay = 2 + (delay / last->rate);

	if (!next->periodic_training)
		return 0;

	if (type == DVFS_SEQUENCE) {
		if (last->periodic_training &&
		    (next->ptfv_list[PTFV_CONFIG_CTRL_INDEX] &
		     PTFV_CONFIG_CTRL_USE_PREVIOUS_EMA)) {
			/*
			 * If the previous frequency was using periodic
			 * calibration then we can reuse the previous
			 * frequencies EMA data.
			 */
			__COPY_EMA(next, last, C0D0U0);
			__COPY_EMA(next, last, C0D0U1);
			__COPY_EMA(next, last, C1D0U0);
			__COPY_EMA(next, last, C1D0U1);
			__COPY_EMA(next, last, C0D1U0);
			__COPY_EMA(next, last, C0D1U1);
			__COPY_EMA(next, last, C1D1U0);
			__COPY_EMA(next, last, C1D1U1);
		} else {
			/* Reset the EMA.*/
			__MOVAVG(next, C0D0U0) = 0;
			__MOVAVG(next, C0D0U1) = 0;
			__MOVAVG(next, C1D0U0) = 0;
			__MOVAVG(next, C1D0U1) = 0;
			__MOVAVG(next, C0D1U0) = 0;
			__MOVAVG(next, C0D1U1) = 0;
			__MOVAVG(next, C1D1U0) = 0;
			__MOVAVG(next, C1D1U1) = 0;

			for (i = 0; i < samples; i++) {
				tegra210_emc_start_periodic_compensation(emc);
				udelay(delay);

				/*
				 * Generate next sample of data.
				 */
				adel = update_clock_tree_delay(emc, DVFS_PT1);
			}
		}

		/*
		 * Seems like it should be part of the
		 * 'if (last_timing->periodic_training)' conditional
		 * since is already done for the else clause.
		 */
		adel = update_clock_tree_delay(emc, DVFS_UPDATE);
	}

	if (type == PERIODIC_TRAINING_SEQUENCE) {
		tegra210_emc_start_periodic_compensation(emc);
		udelay(delay);

		adel = update_clock_tree_delay(emc, PERIODIC_TRAINING_UPDATE);
	}

	return adel;
}

static u32 tegra210_emc_r21021_periodic_compensation(struct tegra210_emc *emc)
{
	u32 emc_cfg, emc_cfg_o, emc_cfg_update, del, value;
	u32 list[] = {
		EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_0,
		EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_1,
		EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_2,
		EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_3,
		EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_0,
		EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_1,
		EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_2,
		EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_3,
		EMC_DATA_BRLSHFT_0,
		EMC_DATA_BRLSHFT_1
	};
	struct tegra210_emc_timing *last = emc->last;
	unsigned int items = ARRAY_SIZE(list), i;
	unsigned long delay;

	if (last->periodic_training) {
		emc_dbg(emc, PER_TRAIN, "Periodic training starting\n");

		value = emc_readl(emc, EMC_DBG);
		emc_cfg_o = emc_readl(emc, EMC_CFG);
		emc_cfg = emc_cfg_o & ~(EMC_CFG_DYN_SELF_REF |
					EMC_CFG_DRAM_ACPD |
					EMC_CFG_DRAM_CLKSTOP_PD);


		/*
		 * 1. Power optimizations should be off.
		 */
		emc_writel(emc, emc_cfg, EMC_CFG);

		/* Does emc_timing_update() for above changes. */
		tegra210_emc_dll_disable(emc);

		for (i = 0; i < emc->num_channels; i++)
			tegra210_emc_wait_for_update(emc, i, EMC_EMC_STATUS,
						     EMC_EMC_STATUS_DRAM_IN_POWERDOWN_MASK,
						     0);

		for (i = 0; i < emc->num_channels; i++)
			tegra210_emc_wait_for_update(emc, i, EMC_EMC_STATUS,
						     EMC_EMC_STATUS_DRAM_IN_SELF_REFRESH_MASK,
						     0);

		emc_cfg_update = value = emc_readl(emc, EMC_CFG_UPDATE);
		value &= ~EMC_CFG_UPDATE_UPDATE_DLL_IN_UPDATE_MASK;
		value |= (2 << EMC_CFG_UPDATE_UPDATE_DLL_IN_UPDATE_SHIFT);
		emc_writel(emc, value, EMC_CFG_UPDATE);

		/*
		 * 2. osc kick off - this assumes training and dvfs have set
		 *    correct MR23.
		 */
		tegra210_emc_start_periodic_compensation(emc);

		/*
		 * 3. Let dram capture its clock tree delays.
		 */
		delay = tegra210_emc_actual_osc_clocks(last->run_clocks);
		delay *= 1000;
		delay /= last->rate + 1;
		udelay(delay);

		/*
		 * 4. Check delta wrt previous values (save value if margin
		 *    exceeds what is set in table).
		 */
		del = periodic_compensation_handler(emc,
						    PERIODIC_TRAINING_SEQUENCE,
						    last, last);

		/*
		 * 5. Apply compensation w.r.t. trained values (if clock tree
		 *    has drifted more than the set margin).
		 */
		if (last->tree_margin < ((del * 128 * (last->rate / 1000)) / 1000000)) {
			for (i = 0; i < items; i++) {
				value = tegra210_emc_compensate(last, list[i]);
				emc_dbg(emc, EMA_WRITES, "0x%08x <= 0x%08x\n",
					list[i], value);
				emc_writel(emc, value, list[i]);
			}
		}

		emc_writel(emc, emc_cfg_o, EMC_CFG);

		/*
		 * 6. Timing update actally applies the new trimmers.
		 */
		tegra210_emc_timing_update(emc);

		/* 6.1. Restore the UPDATE_DLL_IN_UPDATE field. */
		emc_writel(emc, emc_cfg_update, EMC_CFG_UPDATE);

		/* 6.2. Restore the DLL. */
		tegra210_emc_dll_enable(emc);
	}

	return 0;
}

/*
 * Do the clock change sequence.
 */
static void tegra210_emc_r21021_set_clock(struct tegra210_emc *emc, u32 clksrc)
{
	/* state variables */
	static bool fsp_for_next_freq;
	/* constant configuration parameters */
	const bool save_restore_clkstop_pd = true;
	const u32 zqcal_before_cc_cutoff = 2400;
	const bool cya_allow_ref_cc = false;
	const bool cya_issue_pc_ref = false;
	const bool opt_cc_short_zcal = true;
	const bool ref_b4_sref_en = false;
	const u32 tZQCAL_lpddr4 = 1000000;
	const bool opt_short_zcal = true;
	const bool opt_do_sw_qrst = true;
	const u32 opt_dvfs_mode = MAN_SR;
	/*
	 * This is the timing table for the source frequency. It does _not_
	 * necessarily correspond to the actual timing values in the EMC at the
	 * moment. If the boot BCT differs from the table then this can happen.
	 * However, we need it for accessing the dram_timings (which are not
	 * really registers) array for the current frequency.
	 */
	struct tegra210_emc_timing *fake, *last = emc->last, *next = emc->next;
	u32 tRTM, RP_war, R2P_war, TRPab_war, deltaTWATM, W2P_war, tRPST;
	u32 mr13_flip_fspwr, mr13_flip_fspop, ramp_up_wait, ramp_down_wait;
	u32 zq_wait_long, zq_latch_dvfs_wait_time, tZQCAL_lpddr4_fc_adj;
	u32 emc_auto_cal_config, auto_cal_en, emc_cfg, emc_sel_dpd_ctrl;
	u32 tFC_lpddr4 = 1000 * next->dram_timings[T_FC_LPDDR4];
	u32 bg_reg_mode_change, enable_bglp_reg, enable_bg_reg;
	bool opt_zcal_en_cc = false, is_lpddr3 = false;
	bool compensate_trimmer_applicable = false;
	u32 emc_dbg, emc_cfg_pipe_clk, emc_pin;
	u32 src_clk_period, dst_clk_period; /* in picoseconds */
	bool shared_zq_resistor = false;
	u32 value, dram_type;
	u32 opt_dll_mode = 0;
	unsigned long delay;
	unsigned int i;

	emc_dbg(emc, INFO, "Running clock change.\n");

	/* XXX fake == last */
	fake = tegra210_emc_find_timing(emc, last->rate * 1000UL);
	fsp_for_next_freq = !fsp_for_next_freq;

	value = emc_readl(emc, EMC_FBIO_CFG5) & EMC_FBIO_CFG5_DRAM_TYPE_MASK;
	dram_type = value >> EMC_FBIO_CFG5_DRAM_TYPE_SHIFT;

	if (last->burst_regs[EMC_ZCAL_WAIT_CNT_INDEX] & BIT(31))
		shared_zq_resistor = true;

	if ((next->burst_regs[EMC_ZCAL_INTERVAL_INDEX] != 0 &&
	     last->burst_regs[EMC_ZCAL_INTERVAL_INDEX] == 0) ||
	    dram_type == DRAM_TYPE_LPDDR4)
		opt_zcal_en_cc = true;

	if (dram_type == DRAM_TYPE_DDR3)
		opt_dll_mode = tegra210_emc_get_dll_state(next);

	if ((next->burst_regs[EMC_FBIO_CFG5_INDEX] & BIT(25)) &&
	    (dram_type == DRAM_TYPE_LPDDR2))
		is_lpddr3 = true;

	emc_readl(emc, EMC_CFG);
	emc_readl(emc, EMC_AUTO_CAL_CONFIG);

	src_clk_period = 1000000000 / last->rate;
	dst_clk_period = 1000000000 / next->rate;

	if (dst_clk_period <= zqcal_before_cc_cutoff)
		tZQCAL_lpddr4_fc_adj = tZQCAL_lpddr4 - tFC_lpddr4;
	else
		tZQCAL_lpddr4_fc_adj = tZQCAL_lpddr4;

	tZQCAL_lpddr4_fc_adj /= dst_clk_period;

	emc_dbg = emc_readl(emc, EMC_DBG);
	emc_pin = emc_readl(emc, EMC_PIN);
	emc_cfg_pipe_clk = emc_readl(emc, EMC_CFG_PIPE_CLK);

	emc_cfg = next->burst_regs[EMC_CFG_INDEX];
	emc_cfg &= ~(EMC_CFG_DYN_SELF_REF | EMC_CFG_DRAM_ACPD |
		     EMC_CFG_DRAM_CLKSTOP_SR | EMC_CFG_DRAM_CLKSTOP_PD);
	emc_sel_dpd_ctrl = next->emc_sel_dpd_ctrl;
	emc_sel_dpd_ctrl &= ~(EMC_SEL_DPD_CTRL_CLK_SEL_DPD_EN |
			      EMC_SEL_DPD_CTRL_CA_SEL_DPD_EN |
			      EMC_SEL_DPD_CTRL_RESET_SEL_DPD_EN |
			      EMC_SEL_DPD_CTRL_ODT_SEL_DPD_EN |
			      EMC_SEL_DPD_CTRL_DATA_SEL_DPD_EN);

	emc_dbg(emc, INFO, "Clock change version: %d\n",
		DVFS_CLOCK_CHANGE_VERSION);
	emc_dbg(emc, INFO, "DRAM type = %d\n", dram_type);
	emc_dbg(emc, INFO, "DRAM dev #: %u\n", emc->num_devices);
	emc_dbg(emc, INFO, "Next EMC clksrc: 0x%08x\n", clksrc);
	emc_dbg(emc, INFO, "DLL clksrc:      0x%08x\n", next->dll_clk_src);
	emc_dbg(emc, INFO, "last rate: %u, next rate %u\n", last->rate,
		next->rate);
	emc_dbg(emc, INFO, "last period: %u, next period: %u\n",
		src_clk_period, dst_clk_period);
	emc_dbg(emc, INFO, "  shared_zq_resistor: %d\n", !!shared_zq_resistor);
	emc_dbg(emc, INFO, "  num_channels: %u\n", emc->num_channels);
	emc_dbg(emc, INFO, "  opt_dll_mode: %d\n", opt_dll_mode);

	/*
	 * Step 1:
	 *   Pre DVFS SW sequence.
	 */
	emc_dbg(emc, STEPS, "Step 1\n");
	emc_dbg(emc, STEPS, "Step 1.1: Disable DLL temporarily.\n");

	value = emc_readl(emc, EMC_CFG_DIG_DLL);
	value &= ~EMC_CFG_DIG_DLL_CFG_DLL_EN;
	emc_writel(emc, value, EMC_CFG_DIG_DLL);

	tegra210_emc_timing_update(emc);

	for (i = 0; i < emc->num_channels; i++)
		tegra210_emc_wait_for_update(emc, i, EMC_CFG_DIG_DLL,
					     EMC_CFG_DIG_DLL_CFG_DLL_EN, 0);

	emc_dbg(emc, STEPS, "Step 1.2: Disable AUTOCAL temporarily.\n");

	emc_auto_cal_config = next->emc_auto_cal_config;
	auto_cal_en = emc_auto_cal_config & EMC_AUTO_CAL_CONFIG_AUTO_CAL_ENABLE;
	emc_auto_cal_config &= ~EMC_AUTO_CAL_CONFIG_AUTO_CAL_START;
	emc_auto_cal_config |= EMC_AUTO_CAL_CONFIG_AUTO_CAL_MEASURE_STALL;
	emc_auto_cal_config |= EMC_AUTO_CAL_CONFIG_AUTO_CAL_UPDATE_STALL;
	emc_auto_cal_config |= auto_cal_en;
	emc_writel(emc, emc_auto_cal_config, EMC_AUTO_CAL_CONFIG);
	emc_readl(emc, EMC_AUTO_CAL_CONFIG); /* Flush write. */

	emc_dbg(emc, STEPS, "Step 1.3: Disable other power features.\n");

	tegra210_emc_set_shadow_bypass(emc, ACTIVE);
	emc_writel(emc, emc_cfg, EMC_CFG);
	emc_writel(emc, emc_sel_dpd_ctrl, EMC_SEL_DPD_CTRL);
	tegra210_emc_set_shadow_bypass(emc, ASSEMBLY);

	if (next->periodic_training) {
		tegra210_emc_reset_dram_clktree_values(next);

		for (i = 0; i < emc->num_channels; i++)
			tegra210_emc_wait_for_update(emc, i, EMC_EMC_STATUS,
						     EMC_EMC_STATUS_DRAM_IN_POWERDOWN_MASK,
						     0);

		for (i = 0; i < emc->num_channels; i++)
			tegra210_emc_wait_for_update(emc, i, EMC_EMC_STATUS,
						     EMC_EMC_STATUS_DRAM_IN_SELF_REFRESH_MASK,
						     0);

		tegra210_emc_start_periodic_compensation(emc);

		delay = 1000 * tegra210_emc_actual_osc_clocks(last->run_clocks);
		udelay((delay / last->rate) + 2);

		value = periodic_compensation_handler(emc, DVFS_SEQUENCE, fake,
						      next);
		value = (value * 128 * next->rate / 1000) / 1000000;

		if (next->periodic_training && value > next->tree_margin)
			compensate_trimmer_applicable = true;
	}

	emc_writel(emc, EMC_INTSTATUS_CLKCHANGE_COMPLETE, EMC_INTSTATUS);
	tegra210_emc_set_shadow_bypass(emc, ACTIVE);
	emc_writel(emc, emc_cfg, EMC_CFG);
	emc_writel(emc, emc_sel_dpd_ctrl, EMC_SEL_DPD_CTRL);
	emc_writel(emc, emc_cfg_pipe_clk | EMC_CFG_PIPE_CLK_CLK_ALWAYS_ON,
		   EMC_CFG_PIPE_CLK);
	emc_writel(emc, next->emc_fdpd_ctrl_cmd_no_ramp &
			~EMC_FDPD_CTRL_CMD_NO_RAMP_CMD_DPD_NO_RAMP_ENABLE,
		   EMC_FDPD_CTRL_CMD_NO_RAMP);

	bg_reg_mode_change =
		((next->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
		  EMC_PMACRO_BG_BIAS_CTRL_0_BGLP_E_PWRD) ^
		 (last->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
		  EMC_PMACRO_BG_BIAS_CTRL_0_BGLP_E_PWRD)) ||
		((next->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
		  EMC_PMACRO_BG_BIAS_CTRL_0_BG_E_PWRD) ^
		 (last->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
		  EMC_PMACRO_BG_BIAS_CTRL_0_BG_E_PWRD));
	enable_bglp_reg =
		(next->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
		 EMC_PMACRO_BG_BIAS_CTRL_0_BGLP_E_PWRD) == 0;
	enable_bg_reg =
		(next->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
		 EMC_PMACRO_BG_BIAS_CTRL_0_BG_E_PWRD) == 0;

	if (bg_reg_mode_change) {
		if (enable_bg_reg)
			emc_writel(emc, last->burst_regs
				   [EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
				   ~EMC_PMACRO_BG_BIAS_CTRL_0_BG_E_PWRD,
				   EMC_PMACRO_BG_BIAS_CTRL_0);

		if (enable_bglp_reg)
			emc_writel(emc, last->burst_regs
				   [EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
				   ~EMC_PMACRO_BG_BIAS_CTRL_0_BGLP_E_PWRD,
				   EMC_PMACRO_BG_BIAS_CTRL_0);
	}

	/* Check if we need to turn on VREF generator. */
	if ((((last->burst_regs[EMC_PMACRO_DATA_PAD_TX_CTRL_INDEX] &
	       EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQ_E_IVREF) == 0) &&
	     ((next->burst_regs[EMC_PMACRO_DATA_PAD_TX_CTRL_INDEX] &
	       EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQ_E_IVREF) == 1)) ||
	    (((last->burst_regs[EMC_PMACRO_DATA_PAD_TX_CTRL_INDEX] &
	       EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQS_E_IVREF) == 0) &&
	     ((next->burst_regs[EMC_PMACRO_DATA_PAD_TX_CTRL_INDEX] &
	       EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQS_E_IVREF) != 0))) {
		u32 pad_tx_ctrl =
		    next->burst_regs[EMC_PMACRO_DATA_PAD_TX_CTRL_INDEX];
		u32 last_pad_tx_ctrl =
		    last->burst_regs[EMC_PMACRO_DATA_PAD_TX_CTRL_INDEX];
		u32 next_dq_e_ivref, next_dqs_e_ivref;

		next_dqs_e_ivref = pad_tx_ctrl &
				   EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQS_E_IVREF;
		next_dq_e_ivref = pad_tx_ctrl &
				  EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQ_E_IVREF;
		value = (last_pad_tx_ctrl &
				~EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQ_E_IVREF &
				~EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQS_E_IVREF) |
			next_dq_e_ivref | next_dqs_e_ivref;
		emc_writel(emc, value, EMC_PMACRO_DATA_PAD_TX_CTRL);
		udelay(1);
	} else if (bg_reg_mode_change) {
		udelay(1);
	}

	tegra210_emc_set_shadow_bypass(emc, ASSEMBLY);

	/*
	 * Step 2:
	 *   Prelock the DLL.
	 */
	emc_dbg(emc, STEPS, "Step 2\n");

	if (next->burst_regs[EMC_CFG_DIG_DLL_INDEX] &
	    EMC_CFG_DIG_DLL_CFG_DLL_EN) {
		emc_dbg(emc, INFO, "Prelock enabled for target frequency.\n");
		value = tegra210_emc_dll_prelock(emc, clksrc);
		emc_dbg(emc, INFO, "DLL out: 0x%03x\n", value);
	} else {
		emc_dbg(emc, INFO, "Disabling DLL for target frequency.\n");
		tegra210_emc_dll_disable(emc);
	}

	/*
	 * Step 3:
	 *   Prepare autocal for the clock change.
	 */
	emc_dbg(emc, STEPS, "Step 3\n");

	tegra210_emc_set_shadow_bypass(emc, ACTIVE);
	emc_writel(emc, next->emc_auto_cal_config2, EMC_AUTO_CAL_CONFIG2);
	emc_writel(emc, next->emc_auto_cal_config3, EMC_AUTO_CAL_CONFIG3);
	emc_writel(emc, next->emc_auto_cal_config4, EMC_AUTO_CAL_CONFIG4);
	emc_writel(emc, next->emc_auto_cal_config5, EMC_AUTO_CAL_CONFIG5);
	emc_writel(emc, next->emc_auto_cal_config6, EMC_AUTO_CAL_CONFIG6);
	emc_writel(emc, next->emc_auto_cal_config7, EMC_AUTO_CAL_CONFIG7);
	emc_writel(emc, next->emc_auto_cal_config8, EMC_AUTO_CAL_CONFIG8);
	tegra210_emc_set_shadow_bypass(emc, ASSEMBLY);

	emc_auto_cal_config |= (EMC_AUTO_CAL_CONFIG_AUTO_CAL_COMPUTE_START |
				auto_cal_en);
	emc_writel(emc, emc_auto_cal_config, EMC_AUTO_CAL_CONFIG);

	/*
	 * Step 4:
	 *   Update EMC_CFG. (??)
	 */
	emc_dbg(emc, STEPS, "Step 4\n");

	if (src_clk_period > 50000 && dram_type == DRAM_TYPE_LPDDR4)
		ccfifo_writel(emc, 1, EMC_SELF_REF, 0);
	else
		emc_writel(emc, next->emc_cfg_2, EMC_CFG_2);

	/*
	 * Step 5:
	 *   Prepare reference variables for ZQCAL regs.
	 */
	emc_dbg(emc, STEPS, "Step 5\n");

	if (dram_type == DRAM_TYPE_LPDDR4)
		zq_wait_long = max((u32)1, div_o3(1000000, dst_clk_period));
	else if (dram_type == DRAM_TYPE_LPDDR2 || is_lpddr3)
		zq_wait_long = max(next->min_mrs_wait,
				   div_o3(360000, dst_clk_period)) + 4;
	else if (dram_type == DRAM_TYPE_DDR3)
		zq_wait_long = max((u32)256,
				   div_o3(320000, dst_clk_period) + 2);
	else
		zq_wait_long = 0;

	/*
	 * Step 6:
	 *   Training code - removed.
	 */
	emc_dbg(emc, STEPS, "Step 6\n");

	/*
	 * Step 7:
	 *   Program FSP reference registers and send MRWs to new FSPWR.
	 */
	emc_dbg(emc, STEPS, "Step 7\n");
	emc_dbg(emc, SUB_STEPS, "Step 7.1: Bug 200024907 - Patch RP R2P");

	/* WAR 200024907 */
	if (dram_type == DRAM_TYPE_LPDDR4) {
		u32 nRTP = 16;

		if (src_clk_period >= 1000000 / 1866) /* 535.91 ps */
			nRTP = 14;

		if (src_clk_period >= 1000000 / 1600) /* 625.00 ps */
			nRTP = 12;

		if (src_clk_period >= 1000000 / 1333) /* 750.19 ps */
			nRTP = 10;

		if (src_clk_period >= 1000000 / 1066) /* 938.09 ps */
			nRTP = 8;

		deltaTWATM = max_t(u32, div_o3(7500, src_clk_period), 8);

		/*
		 * Originally there was a + .5 in the tRPST calculation.
		 * However since we can't do FP in the kernel and the tRTM
		 * computation was in a floating point ceiling function, adding
		 * one to tRTP should be ok. There is no other source of non
		 * integer values, so the result was always going to be
		 * something for the form: f_ceil(N + .5) = N + 1;
		 */
		tRPST = (last->emc_mrw & 0x80) >> 7;
		tRTM = fake->dram_timings[RL] + div_o3(3600, src_clk_period) +
			max_t(u32, div_o3(7500, src_clk_period), 8) + tRPST +
			1 + nRTP;

		emc_dbg(emc, INFO, "tRTM = %u, EMC_RP = %u\n", tRTM,
			next->burst_regs[EMC_RP_INDEX]);

		if (last->burst_regs[EMC_RP_INDEX] < tRTM) {
			if (tRTM > (last->burst_regs[EMC_R2P_INDEX] +
				    last->burst_regs[EMC_RP_INDEX])) {
				R2P_war = tRTM - last->burst_regs[EMC_RP_INDEX];
				RP_war = last->burst_regs[EMC_RP_INDEX];
				TRPab_war = last->burst_regs[EMC_TRPAB_INDEX];

				if (R2P_war > 63) {
					RP_war = R2P_war +
						 last->burst_regs[EMC_RP_INDEX] - 63;

					if (TRPab_war < RP_war)
						TRPab_war = RP_war;

					R2P_war = 63;
				}
			} else {
				R2P_war = last->burst_regs[EMC_R2P_INDEX];
				RP_war = last->burst_regs[EMC_RP_INDEX];
				TRPab_war = last->burst_regs[EMC_TRPAB_INDEX];
			}

			if (RP_war < deltaTWATM) {
				W2P_war = last->burst_regs[EMC_W2P_INDEX]
					  + deltaTWATM - RP_war;
				if (W2P_war > 63) {
					RP_war = RP_war + W2P_war - 63;
					if (TRPab_war < RP_war)
						TRPab_war = RP_war;
					W2P_war = 63;
				}
			} else {
				W2P_war = last->burst_regs[
					  EMC_W2P_INDEX];
			}

			if ((last->burst_regs[EMC_W2P_INDEX] ^ W2P_war) ||
			    (last->burst_regs[EMC_R2P_INDEX] ^ R2P_war) ||
			    (last->burst_regs[EMC_RP_INDEX] ^ RP_war) ||
			    (last->burst_regs[EMC_TRPAB_INDEX] ^ TRPab_war)) {
				emc_writel(emc, RP_war, EMC_RP);
				emc_writel(emc, R2P_war, EMC_R2P);
				emc_writel(emc, W2P_war, EMC_W2P);
				emc_writel(emc, TRPab_war, EMC_TRPAB);
			}

			tegra210_emc_timing_update(emc);
		} else {
			emc_dbg(emc, INFO, "Skipped WAR\n");
		}
	}

	if (!fsp_for_next_freq) {
		mr13_flip_fspwr = (next->emc_mrw3 & 0xffffff3f) | 0x80;
		mr13_flip_fspop = (next->emc_mrw3 & 0xffffff3f) | 0x00;
	} else {
		mr13_flip_fspwr = (next->emc_mrw3 & 0xffffff3f) | 0x40;
		mr13_flip_fspop = (next->emc_mrw3 & 0xffffff3f) | 0xc0;
	}

	if (dram_type == DRAM_TYPE_LPDDR4) {
		emc_writel(emc, mr13_flip_fspwr, EMC_MRW3);
		emc_writel(emc, next->emc_mrw, EMC_MRW);
		emc_writel(emc, next->emc_mrw2, EMC_MRW2);
	}

	/*
	 * Step 8:
	 *   Program the shadow registers.
	 */
	emc_dbg(emc, STEPS, "Step 8\n");
	emc_dbg(emc, SUB_STEPS, "Writing burst_regs\n");

	for (i = 0; i < next->num_burst; i++) {
		const u16 *offsets = emc->offsets->burst;
		u16 offset;

		if (!offsets[i])
			continue;

		value = next->burst_regs[i];
		offset = offsets[i];

		if (dram_type != DRAM_TYPE_LPDDR4 &&
		    (offset == EMC_MRW6 || offset == EMC_MRW7 ||
		     offset == EMC_MRW8 || offset == EMC_MRW9 ||
		     offset == EMC_MRW10 || offset == EMC_MRW11 ||
		     offset == EMC_MRW12 || offset == EMC_MRW13 ||
		     offset == EMC_MRW14 || offset == EMC_MRW15 ||
		     offset == EMC_TRAINING_CTRL))
			continue;

		/* Pain... And suffering. */
		if (offset == EMC_CFG) {
			value &= ~EMC_CFG_DRAM_ACPD;
			value &= ~EMC_CFG_DYN_SELF_REF;

			if (dram_type == DRAM_TYPE_LPDDR4) {
				value &= ~EMC_CFG_DRAM_CLKSTOP_SR;
				value &= ~EMC_CFG_DRAM_CLKSTOP_PD;
			}
		} else if (offset == EMC_MRS_WAIT_CNT &&
			   dram_type == DRAM_TYPE_LPDDR2 &&
			   opt_zcal_en_cc && !opt_cc_short_zcal &&
			   opt_short_zcal) {
			value = (value & ~(EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK <<
					   EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT)) |
				((zq_wait_long & EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK) <<
						 EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT);
		} else if (offset == EMC_ZCAL_WAIT_CNT &&
			   dram_type == DRAM_TYPE_DDR3 && opt_zcal_en_cc &&
			   !opt_cc_short_zcal && opt_short_zcal) {
			value = (value & ~(EMC_ZCAL_WAIT_CNT_ZCAL_WAIT_CNT_MASK <<
					   EMC_ZCAL_WAIT_CNT_ZCAL_WAIT_CNT_SHIFT)) |
				((zq_wait_long & EMC_ZCAL_WAIT_CNT_ZCAL_WAIT_CNT_MASK) <<
						 EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT);
		} else if (offset == EMC_ZCAL_INTERVAL && opt_zcal_en_cc) {
			value = 0; /* EMC_ZCAL_INTERVAL reset value. */
		} else if (offset == EMC_PMACRO_AUTOCAL_CFG_COMMON) {
			value |= EMC_PMACRO_AUTOCAL_CFG_COMMON_E_CAL_BYPASS_DVFS;
		} else if (offset == EMC_PMACRO_DATA_PAD_TX_CTRL) {
			value &= ~(EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQSP_TX_E_DCC |
				   EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQSN_TX_E_DCC |
				   EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQ_TX_E_DCC |
				   EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_CMD_TX_E_DCC);
		} else if (offset == EMC_PMACRO_CMD_PAD_TX_CTRL) {
			value |= EMC_PMACRO_CMD_PAD_TX_CTRL_CMD_DQ_TX_DRVFORCEON;
			value &= ~(EMC_PMACRO_CMD_PAD_TX_CTRL_CMD_DQSP_TX_E_DCC |
				   EMC_PMACRO_CMD_PAD_TX_CTRL_CMD_DQSN_TX_E_DCC |
				   EMC_PMACRO_CMD_PAD_TX_CTRL_CMD_DQ_TX_E_DCC |
				   EMC_PMACRO_CMD_PAD_TX_CTRL_CMD_CMD_TX_E_DCC);
		} else if (offset == EMC_PMACRO_BRICK_CTRL_RFU1) {
			value &= 0xf800f800;
		} else if (offset == EMC_PMACRO_COMMON_PAD_TX_CTRL) {
			value &= 0xfffffff0;
		}

		emc_writel(emc, value, offset);
	}

	/* SW addition: do EMC refresh adjustment here. */
	tegra210_emc_adjust_timing(emc, next);

	if (dram_type == DRAM_TYPE_LPDDR4) {
		value = (23 << EMC_MRW_MRW_MA_SHIFT) |
			(next->run_clocks & EMC_MRW_MRW_OP_MASK);
		emc_writel(emc, value, EMC_MRW);
	}

	/* Per channel burst registers. */
	emc_dbg(emc, SUB_STEPS, "Writing burst_regs_per_ch\n");

	for (i = 0; i < next->num_burst_per_ch; i++) {
		const struct tegra210_emc_per_channel_regs *burst =
				emc->offsets->burst_per_channel;

		if (!burst[i].offset)
			continue;

		if (dram_type != DRAM_TYPE_LPDDR4 &&
		    (burst[i].offset == EMC_MRW6 ||
		     burst[i].offset == EMC_MRW7 ||
		     burst[i].offset == EMC_MRW8 ||
		     burst[i].offset == EMC_MRW9 ||
		     burst[i].offset == EMC_MRW10 ||
		     burst[i].offset == EMC_MRW11 ||
		     burst[i].offset == EMC_MRW12 ||
		     burst[i].offset == EMC_MRW13 ||
		     burst[i].offset == EMC_MRW14 ||
		     burst[i].offset == EMC_MRW15))
			continue;

		/* Filter out second channel if not in DUAL_CHANNEL mode. */
		if (emc->num_channels < 2 && burst[i].bank >= 1)
			continue;

		emc_dbg(emc, REG_LISTS, "(%u) 0x%08x => 0x%08x\n", i,
			next->burst_reg_per_ch[i], burst[i].offset);
		emc_channel_writel(emc, burst[i].bank,
				   next->burst_reg_per_ch[i],
				   burst[i].offset);
	}

	/* Vref regs. */
	emc_dbg(emc, SUB_STEPS, "Writing vref_regs\n");

	for (i = 0; i < next->vref_num; i++) {
		const struct tegra210_emc_per_channel_regs *vref =
					emc->offsets->vref_per_channel;

		if (!vref[i].offset)
			continue;

		if (emc->num_channels < 2 && vref[i].bank >= 1)
			continue;

		emc_dbg(emc, REG_LISTS, "(%u) 0x%08x => 0x%08x\n", i,
			next->vref_perch_regs[i], vref[i].offset);
		emc_channel_writel(emc, vref[i].bank, next->vref_perch_regs[i],
				   vref[i].offset);
	}

	/* Trimmers. */
	emc_dbg(emc, SUB_STEPS, "Writing trim_regs\n");

	for (i = 0; i < next->num_trim; i++) {
		const u16 *offsets = emc->offsets->trim;

		if (!offsets[i])
			continue;

		if (compensate_trimmer_applicable &&
		    (offsets[i] == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_0 ||
		     offsets[i] == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_1 ||
		     offsets[i] == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_2 ||
		     offsets[i] == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_3 ||
		     offsets[i] == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_0 ||
		     offsets[i] == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_1 ||
		     offsets[i] == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_2 ||
		     offsets[i] == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_3 ||
		     offsets[i] == EMC_DATA_BRLSHFT_0 ||
		     offsets[i] == EMC_DATA_BRLSHFT_1)) {
			value = tegra210_emc_compensate(next, offsets[i]);
			emc_dbg(emc, REG_LISTS, "(%u) 0x%08x => 0x%08x\n", i,
				value, offsets[i]);
			emc_dbg(emc, EMA_WRITES, "0x%08x <= 0x%08x\n",
				(u32)(u64)offsets[i], value);
			emc_writel(emc, value, offsets[i]);
		} else {
			emc_dbg(emc, REG_LISTS, "(%u) 0x%08x => 0x%08x\n", i,
				next->trim_regs[i], offsets[i]);
			emc_writel(emc, next->trim_regs[i], offsets[i]);
		}
	}

	/* Per channel trimmers. */
	emc_dbg(emc, SUB_STEPS, "Writing trim_regs_per_ch\n");

	for (i = 0; i < next->num_trim_per_ch; i++) {
		const struct tegra210_emc_per_channel_regs *trim =
				&emc->offsets->trim_per_channel[0];
		unsigned int offset;

		if (!trim[i].offset)
			continue;

		if (emc->num_channels < 2 && trim[i].bank >= 1)
			continue;

		offset = trim[i].offset;

		if (compensate_trimmer_applicable &&
		    (offset == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_0 ||
		     offset == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_1 ||
		     offset == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_2 ||
		     offset == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_3 ||
		     offset == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_0 ||
		     offset == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_1 ||
		     offset == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_2 ||
		     offset == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_3 ||
		     offset == EMC_DATA_BRLSHFT_0 ||
		     offset == EMC_DATA_BRLSHFT_1)) {
			value = tegra210_emc_compensate(next, offset);
			emc_dbg(emc, REG_LISTS, "(%u) 0x%08x => 0x%08x\n", i,
				value, offset);
			emc_dbg(emc, EMA_WRITES, "0x%08x <= 0x%08x\n", offset,
				value);
			emc_channel_writel(emc, trim[i].bank, value, offset);
		} else {
			emc_dbg(emc, REG_LISTS, "(%u) 0x%08x => 0x%08x\n", i,
				next->trim_perch_regs[i], offset);
			emc_channel_writel(emc, trim[i].bank,
					   next->trim_perch_regs[i], offset);
		}
	}

	emc_dbg(emc, SUB_STEPS, "Writing burst_mc_regs\n");

	for (i = 0; i < next->num_mc_regs; i++) {
		const u16 *offsets = emc->offsets->burst_mc;
		u32 *values = next->burst_mc_regs;

		emc_dbg(emc, REG_LISTS, "(%u) 0x%08x => 0x%08x\n", i,
			values[i], offsets[i]);
		mc_writel(emc->mc, values[i], offsets[i]);
	}

	/* Registers to be programmed on the faster clock. */
	if (next->rate < last->rate) {
		const u16 *la = emc->offsets->la_scale;

		emc_dbg(emc, SUB_STEPS, "Writing la_scale_regs\n");

		for (i = 0; i < next->num_up_down; i++) {
			emc_dbg(emc, REG_LISTS, "(%u) 0x%08x => 0x%08x\n", i,
				next->la_scale_regs[i], la[i]);
			mc_writel(emc->mc, next->la_scale_regs[i], la[i]);
		}
	}

	/* Flush all the burst register writes. */
	mc_readl(emc->mc, MC_EMEM_ADR_CFG);

	/*
	 * Step 9:
	 *   LPDDR4 section A.
	 */
	emc_dbg(emc, STEPS, "Step 9\n");

	value = next->burst_regs[EMC_ZCAL_WAIT_CNT_INDEX];
	value &= ~EMC_ZCAL_WAIT_CNT_ZCAL_WAIT_CNT_MASK;

	if (dram_type == DRAM_TYPE_LPDDR4) {
		emc_writel(emc, 0, EMC_ZCAL_INTERVAL);
		emc_writel(emc, value, EMC_ZCAL_WAIT_CNT);

		value = emc_dbg | (EMC_DBG_WRITE_MUX_ACTIVE |
				   EMC_DBG_WRITE_ACTIVE_ONLY);

		emc_writel(emc, value, EMC_DBG);
		emc_writel(emc, 0, EMC_ZCAL_INTERVAL);
		emc_writel(emc, emc_dbg, EMC_DBG);
	}

	/*
	 * Step 10:
	 *   LPDDR4 and DDR3 common section.
	 */
	emc_dbg(emc, STEPS, "Step 10\n");

	if (opt_dvfs_mode == MAN_SR || dram_type == DRAM_TYPE_LPDDR4) {
		if (dram_type == DRAM_TYPE_LPDDR4)
			ccfifo_writel(emc, 0x101, EMC_SELF_REF, 0);
		else
			ccfifo_writel(emc, 0x1, EMC_SELF_REF, 0);

		if (dram_type == DRAM_TYPE_LPDDR4 &&
		    dst_clk_period <= zqcal_before_cc_cutoff) {
			ccfifo_writel(emc, mr13_flip_fspwr ^ 0x40, EMC_MRW3, 0);
			ccfifo_writel(emc, (next->burst_regs[EMC_MRW6_INDEX] &
						0xFFFF3F3F) |
					   (last->burst_regs[EMC_MRW6_INDEX] &
						0x0000C0C0), EMC_MRW6, 0);
			ccfifo_writel(emc, (next->burst_regs[EMC_MRW14_INDEX] &
						0xFFFF0707) |
					   (last->burst_regs[EMC_MRW14_INDEX] &
						0x00003838), EMC_MRW14, 0);

			if (emc->num_devices > 1) {
				ccfifo_writel(emc,
				      (next->burst_regs[EMC_MRW7_INDEX] &
				       0xFFFF3F3F) |
				      (last->burst_regs[EMC_MRW7_INDEX] &
				       0x0000C0C0), EMC_MRW7, 0);
				ccfifo_writel(emc,
				     (next->burst_regs[EMC_MRW15_INDEX] &
				      0xFFFF0707) |
				     (last->burst_regs[EMC_MRW15_INDEX] &
				      0x00003838), EMC_MRW15, 0);
			}

			if (opt_zcal_en_cc) {
				if (emc->num_devices < 2)
					ccfifo_writel(emc,
						2UL << EMC_ZQ_CAL_DEV_SEL_SHIFT
						| EMC_ZQ_CAL_ZQ_CAL_CMD,
						EMC_ZQ_CAL, 0);
				else if (shared_zq_resistor)
					ccfifo_writel(emc,
						2UL << EMC_ZQ_CAL_DEV_SEL_SHIFT
						| EMC_ZQ_CAL_ZQ_CAL_CMD,
						EMC_ZQ_CAL, 0);
				else
					ccfifo_writel(emc,
						      EMC_ZQ_CAL_ZQ_CAL_CMD,
						      EMC_ZQ_CAL, 0);
			}
		}
	}

	if (dram_type == DRAM_TYPE_LPDDR4) {
		value = (1000 * fake->dram_timings[T_RP]) / src_clk_period;
		ccfifo_writel(emc, mr13_flip_fspop | 0x8, EMC_MRW3, value);
		ccfifo_writel(emc, 0, 0, tFC_lpddr4 / src_clk_period);
	}

	if (dram_type == DRAM_TYPE_LPDDR4 || opt_dvfs_mode != MAN_SR) {
		delay = 30;

		if (cya_allow_ref_cc) {
			delay += (1000 * fake->dram_timings[T_RP]) /
					src_clk_period;
			delay += 4000 * fake->dram_timings[T_RFC];
		}

		ccfifo_writel(emc, emc_pin & ~(EMC_PIN_PIN_CKE_PER_DEV |
					       EMC_PIN_PIN_CKEB |
					       EMC_PIN_PIN_CKE),
			      EMC_PIN, delay);
	}

	/* calculate reference delay multiplier */
	value = 1;

	if (ref_b4_sref_en)
		value++;

	if (cya_allow_ref_cc)
		value++;

	if (cya_issue_pc_ref)
		value++;

	if (dram_type != DRAM_TYPE_LPDDR4) {
		delay = ((1000 * fake->dram_timings[T_RP] / src_clk_period) +
			 (1000 * fake->dram_timings[T_RFC] / src_clk_period));
		delay = value * delay + 20;
	} else {
		delay = 0;
	}

	/*
	 * Step 11:
	 *   Ramp down.
	 */
	emc_dbg(emc, STEPS, "Step 11\n");

	ccfifo_writel(emc, 0x0, EMC_CFG_SYNC, delay);

	value = emc_dbg | EMC_DBG_WRITE_MUX_ACTIVE | EMC_DBG_WRITE_ACTIVE_ONLY;
	ccfifo_writel(emc, value, EMC_DBG, 0);

	ramp_down_wait = tegra210_emc_dvfs_power_ramp_down(emc, src_clk_period,
							   0);

	/*
	 * Step 12:
	 *   And finally - trigger the clock change.
	 */
	emc_dbg(emc, STEPS, "Step 12\n");

	ccfifo_writel(emc, 1, EMC_STALL_THEN_EXE_AFTER_CLKCHANGE, 0);
	value &= ~EMC_DBG_WRITE_ACTIVE_ONLY;
	ccfifo_writel(emc, value, EMC_DBG, 0);

	/*
	 * Step 13:
	 *   Ramp up.
	 */
	emc_dbg(emc, STEPS, "Step 13\n");

	ramp_up_wait = tegra210_emc_dvfs_power_ramp_up(emc, dst_clk_period, 0);
	ccfifo_writel(emc, emc_dbg, EMC_DBG, 0);

	/*
	 * Step 14:
	 *   Bringup CKE pins.
	 */
	emc_dbg(emc, STEPS, "Step 14\n");

	if (dram_type == DRAM_TYPE_LPDDR4) {
		value = emc_pin | EMC_PIN_PIN_CKE;

		if (emc->num_devices <= 1)
			value &= ~(EMC_PIN_PIN_CKEB | EMC_PIN_PIN_CKE_PER_DEV);
		else
			value |= EMC_PIN_PIN_CKEB | EMC_PIN_PIN_CKE_PER_DEV;

		ccfifo_writel(emc, value, EMC_PIN, 0);
	}

	/*
	 * Step 15: (two step 15s ??)
	 *   Calculate zqlatch wait time; has dependency on ramping times.
	 */
	emc_dbg(emc, STEPS, "Step 15\n");

	if (dst_clk_period <= zqcal_before_cc_cutoff) {
		s32 t = (s32)(ramp_up_wait + ramp_down_wait) /
			(s32)dst_clk_period;
		zq_latch_dvfs_wait_time = (s32)tZQCAL_lpddr4_fc_adj - t;
	} else {
		zq_latch_dvfs_wait_time = tZQCAL_lpddr4_fc_adj -
			div_o3(1000 * next->dram_timings[T_PDEX],
			       dst_clk_period);
	}

	emc_dbg(emc, INFO, "tZQCAL_lpddr4_fc_adj = %u\n", tZQCAL_lpddr4_fc_adj);
	emc_dbg(emc, INFO, "dst_clk_period = %u\n",
		dst_clk_period);
	emc_dbg(emc, INFO, "next->dram_timings[T_PDEX] = %u\n",
		next->dram_timings[T_PDEX]);
	emc_dbg(emc, INFO, "zq_latch_dvfs_wait_time = %d\n",
		max_t(s32, 0, zq_latch_dvfs_wait_time));

	if (dram_type == DRAM_TYPE_LPDDR4 && opt_zcal_en_cc) {
		delay = div_o3(1000 * next->dram_timings[T_PDEX],
			       dst_clk_period);

		if (emc->num_devices < 2) {
			if (dst_clk_period > zqcal_before_cc_cutoff)
				ccfifo_writel(emc,
					      2UL << EMC_ZQ_CAL_DEV_SEL_SHIFT |
					      EMC_ZQ_CAL_ZQ_CAL_CMD, EMC_ZQ_CAL,
					      delay);

			value = (mr13_flip_fspop & 0xfffffff7) | 0x0c000000;
			ccfifo_writel(emc, value, EMC_MRW3, delay);
			ccfifo_writel(emc, 0, EMC_SELF_REF, 0);
			ccfifo_writel(emc, 0, EMC_REF, 0);
			ccfifo_writel(emc, 2UL << EMC_ZQ_CAL_DEV_SEL_SHIFT |
				      EMC_ZQ_CAL_ZQ_LATCH_CMD,
				      EMC_ZQ_CAL,
				      max_t(s32, 0, zq_latch_dvfs_wait_time));
		} else if (shared_zq_resistor) {
			if (dst_clk_period > zqcal_before_cc_cutoff)
				ccfifo_writel(emc,
					      2UL << EMC_ZQ_CAL_DEV_SEL_SHIFT |
					      EMC_ZQ_CAL_ZQ_CAL_CMD, EMC_ZQ_CAL,
					      delay);

			ccfifo_writel(emc, 2UL << EMC_ZQ_CAL_DEV_SEL_SHIFT |
				      EMC_ZQ_CAL_ZQ_LATCH_CMD, EMC_ZQ_CAL,
				      max_t(s32, 0, zq_latch_dvfs_wait_time) +
					delay);
			ccfifo_writel(emc, 1UL << EMC_ZQ_CAL_DEV_SEL_SHIFT |
				      EMC_ZQ_CAL_ZQ_LATCH_CMD,
				      EMC_ZQ_CAL, 0);

			value = (mr13_flip_fspop & 0xfffffff7) | 0x0c000000;
			ccfifo_writel(emc, value, EMC_MRW3, 0);
			ccfifo_writel(emc, 0, EMC_SELF_REF, 0);
			ccfifo_writel(emc, 0, EMC_REF, 0);

			ccfifo_writel(emc, 1UL << EMC_ZQ_CAL_DEV_SEL_SHIFT |
				      EMC_ZQ_CAL_ZQ_LATCH_CMD, EMC_ZQ_CAL,
				      tZQCAL_lpddr4 / dst_clk_period);
		} else {
			if (dst_clk_period > zqcal_before_cc_cutoff)
				ccfifo_writel(emc, EMC_ZQ_CAL_ZQ_CAL_CMD,
					      EMC_ZQ_CAL, delay);

			value = (mr13_flip_fspop & 0xfffffff7) | 0x0c000000;
			ccfifo_writel(emc, value, EMC_MRW3, delay);
			ccfifo_writel(emc, 0, EMC_SELF_REF, 0);
			ccfifo_writel(emc, 0, EMC_REF, 0);

			ccfifo_writel(emc, EMC_ZQ_CAL_ZQ_LATCH_CMD, EMC_ZQ_CAL,
				      max_t(s32, 0, zq_latch_dvfs_wait_time));
		}
	}

	/* WAR: delay for zqlatch */
	ccfifo_writel(emc, 0, 0, 10);

	/*
	 * Step 16:
	 *   LPDDR4 Conditional Training Kickoff. Removed.
	 */

	/*
	 * Step 17:
	 *   MANSR exit self refresh.
	 */
	emc_dbg(emc, STEPS, "Step 17\n");

	if (opt_dvfs_mode == MAN_SR && dram_type != DRAM_TYPE_LPDDR4)
		ccfifo_writel(emc, 0, EMC_SELF_REF, 0);

	/*
	 * Step 18:
	 *   Send MRWs to LPDDR3/DDR3.
	 */
	emc_dbg(emc, STEPS, "Step 18\n");

	if (dram_type == DRAM_TYPE_LPDDR2) {
		ccfifo_writel(emc, next->emc_mrw2, EMC_MRW2, 0);
		ccfifo_writel(emc, next->emc_mrw,  EMC_MRW,  0);
		if (is_lpddr3)
			ccfifo_writel(emc, next->emc_mrw4, EMC_MRW4, 0);
	} else if (dram_type == DRAM_TYPE_DDR3) {
		if (opt_dll_mode)
			ccfifo_writel(emc, next->emc_emrs &
				      ~EMC_EMRS_USE_EMRS_LONG_CNT, EMC_EMRS, 0);
		ccfifo_writel(emc, next->emc_emrs2 &
			      ~EMC_EMRS2_USE_EMRS2_LONG_CNT, EMC_EMRS2, 0);
		ccfifo_writel(emc, next->emc_mrs |
			      EMC_EMRS_USE_EMRS_LONG_CNT, EMC_MRS, 0);
	}

	/*
	 * Step 19:
	 *   ZQCAL for LPDDR3/DDR3
	 */
	emc_dbg(emc, STEPS, "Step 19\n");

	if (opt_zcal_en_cc) {
		if (dram_type == DRAM_TYPE_LPDDR2) {
			value = opt_cc_short_zcal ? 90000 : 360000;
			value = div_o3(value, dst_clk_period);
			value = value <<
				EMC_MRS_WAIT_CNT2_MRS_EXT2_WAIT_CNT_SHIFT |
				value <<
				EMC_MRS_WAIT_CNT2_MRS_EXT1_WAIT_CNT_SHIFT;
			ccfifo_writel(emc, value, EMC_MRS_WAIT_CNT2, 0);

			value = opt_cc_short_zcal ? 0x56 : 0xab;
			ccfifo_writel(emc, 2 << EMC_MRW_MRW_DEV_SELECTN_SHIFT |
					   EMC_MRW_USE_MRW_EXT_CNT |
					   10 << EMC_MRW_MRW_MA_SHIFT |
					   value << EMC_MRW_MRW_OP_SHIFT,
				      EMC_MRW, 0);

			if (emc->num_devices > 1) {
				value = 1 << EMC_MRW_MRW_DEV_SELECTN_SHIFT |
					EMC_MRW_USE_MRW_EXT_CNT |
					10 << EMC_MRW_MRW_MA_SHIFT |
					value << EMC_MRW_MRW_OP_SHIFT;
				ccfifo_writel(emc, value, EMC_MRW, 0);
			}
		} else if (dram_type == DRAM_TYPE_DDR3) {
			value = opt_cc_short_zcal ? 0 : EMC_ZQ_CAL_LONG;

			ccfifo_writel(emc, value |
					   2 << EMC_ZQ_CAL_DEV_SEL_SHIFT |
					   EMC_ZQ_CAL_ZQ_CAL_CMD, EMC_ZQ_CAL,
					   0);

			if (emc->num_devices > 1) {
				value = value | 1 << EMC_ZQ_CAL_DEV_SEL_SHIFT |
						EMC_ZQ_CAL_ZQ_CAL_CMD;
				ccfifo_writel(emc, value, EMC_ZQ_CAL, 0);
			}
		}
	}

	if (bg_reg_mode_change) {
		tegra210_emc_set_shadow_bypass(emc, ACTIVE);

		if (ramp_up_wait <= 1250000)
			delay = (1250000 - ramp_up_wait) / dst_clk_period;
		else
			delay = 0;

		ccfifo_writel(emc,
			      next->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX],
			      EMC_PMACRO_BG_BIAS_CTRL_0, delay);
		tegra210_emc_set_shadow_bypass(emc, ASSEMBLY);
	}

	/*
	 * Step 20:
	 *   Issue ref and optional QRST.
	 */
	emc_dbg(emc, STEPS, "Step 20\n");

	if (dram_type != DRAM_TYPE_LPDDR4)
		ccfifo_writel(emc, 0, EMC_REF, 0);

	if (opt_do_sw_qrst) {
		ccfifo_writel(emc, 1, EMC_ISSUE_QRST, 0);
		ccfifo_writel(emc, 0, EMC_ISSUE_QRST, 2);
	}

	/*
	 * Step 21:
	 *   Restore ZCAL and ZCAL interval.
	 */
	emc_dbg(emc, STEPS, "Step 21\n");

	if (save_restore_clkstop_pd || opt_zcal_en_cc) {
		ccfifo_writel(emc, emc_dbg | EMC_DBG_WRITE_MUX_ACTIVE,
			      EMC_DBG, 0);
		if (opt_zcal_en_cc && dram_type != DRAM_TYPE_LPDDR4)
			ccfifo_writel(emc, next->burst_regs[EMC_ZCAL_INTERVAL_INDEX],
				      EMC_ZCAL_INTERVAL, 0);

		if (save_restore_clkstop_pd)
			ccfifo_writel(emc, next->burst_regs[EMC_CFG_INDEX] &
						~EMC_CFG_DYN_SELF_REF,
				      EMC_CFG, 0);
		ccfifo_writel(emc, emc_dbg, EMC_DBG, 0);
	}

	/*
	 * Step 22:
	 *   Restore EMC_CFG_PIPE_CLK.
	 */
	emc_dbg(emc, STEPS, "Step 22\n");

	ccfifo_writel(emc, emc_cfg_pipe_clk, EMC_CFG_PIPE_CLK, 0);

	if (bg_reg_mode_change) {
		if (enable_bg_reg)
			emc_writel(emc,
				   next->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
					~EMC_PMACRO_BG_BIAS_CTRL_0_BGLP_E_PWRD,
				   EMC_PMACRO_BG_BIAS_CTRL_0);
		else
			emc_writel(emc,
				   next->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
					~EMC_PMACRO_BG_BIAS_CTRL_0_BG_E_PWRD,
				   EMC_PMACRO_BG_BIAS_CTRL_0);
	}

	/*
	 * Step 23:
	 */
	emc_dbg(emc, STEPS, "Step 23\n");

	value = emc_readl(emc, EMC_CFG_DIG_DLL);
	value |= EMC_CFG_DIG_DLL_CFG_DLL_STALL_ALL_TRAFFIC;
	value &= ~EMC_CFG_DIG_DLL_CFG_DLL_STALL_RW_UNTIL_LOCK;
	value &= ~EMC_CFG_DIG_DLL_CFG_DLL_STALL_ALL_UNTIL_LOCK;
	value &= ~EMC_CFG_DIG_DLL_CFG_DLL_EN;
	value = (value & ~EMC_CFG_DIG_DLL_CFG_DLL_MODE_MASK) |
		(2 << EMC_CFG_DIG_DLL_CFG_DLL_MODE_SHIFT);
	emc_writel(emc, value, EMC_CFG_DIG_DLL);

	tegra210_emc_do_clock_change(emc, clksrc);

	/*
	 * Step 24:
	 *   Save training results. Removed.
	 */

	/*
	 * Step 25:
	 *   Program MC updown registers.
	 */
	emc_dbg(emc, STEPS, "Step 25\n");

	if (next->rate > last->rate) {
		for (i = 0; i < next->num_up_down; i++)
			mc_writel(emc->mc, next->la_scale_regs[i],
				  emc->offsets->la_scale[i]);

		tegra210_emc_timing_update(emc);
	}

	/*
	 * Step 26:
	 *   Restore ZCAL registers.
	 */
	emc_dbg(emc, STEPS, "Step 26\n");

	if (dram_type == DRAM_TYPE_LPDDR4) {
		tegra210_emc_set_shadow_bypass(emc, ACTIVE);
		emc_writel(emc, next->burst_regs[EMC_ZCAL_WAIT_CNT_INDEX],
			   EMC_ZCAL_WAIT_CNT);
		emc_writel(emc, next->burst_regs[EMC_ZCAL_INTERVAL_INDEX],
			   EMC_ZCAL_INTERVAL);
		tegra210_emc_set_shadow_bypass(emc, ASSEMBLY);
	}

	if (dram_type != DRAM_TYPE_LPDDR4 && opt_zcal_en_cc &&
	    !opt_short_zcal && opt_cc_short_zcal) {
		udelay(2);

		tegra210_emc_set_shadow_bypass(emc, ACTIVE);
		if (dram_type == DRAM_TYPE_LPDDR2)
			emc_writel(emc, next->burst_regs[EMC_MRS_WAIT_CNT_INDEX],
				   EMC_MRS_WAIT_CNT);
		else if (dram_type == DRAM_TYPE_DDR3)
			emc_writel(emc, next->burst_regs[EMC_ZCAL_WAIT_CNT_INDEX],
				   EMC_ZCAL_WAIT_CNT);
		tegra210_emc_set_shadow_bypass(emc, ASSEMBLY);
	}

	/*
	 * Step 27:
	 *   Restore EMC_CFG, FDPD registers.
	 */
	emc_dbg(emc, STEPS, "Step 27\n");

	tegra210_emc_set_shadow_bypass(emc, ACTIVE);
	emc_writel(emc, next->burst_regs[EMC_CFG_INDEX], EMC_CFG);
	tegra210_emc_set_shadow_bypass(emc, ASSEMBLY);
	emc_writel(emc, next->emc_fdpd_ctrl_cmd_no_ramp,
		   EMC_FDPD_CTRL_CMD_NO_RAMP);
	emc_writel(emc, next->emc_sel_dpd_ctrl, EMC_SEL_DPD_CTRL);

	/*
	 * Step 28:
	 *   Training recover. Removed.
	 */
	emc_dbg(emc, STEPS, "Step 28\n");

	tegra210_emc_set_shadow_bypass(emc, ACTIVE);
	emc_writel(emc,
		   next->burst_regs[EMC_PMACRO_AUTOCAL_CFG_COMMON_INDEX],
		   EMC_PMACRO_AUTOCAL_CFG_COMMON);
	tegra210_emc_set_shadow_bypass(emc, ASSEMBLY);

	/*
	 * Step 29:
	 *   Power fix WAR.
	 */
	emc_dbg(emc, STEPS, "Step 29\n");

	emc_writel(emc, EMC_PMACRO_CFG_PM_GLOBAL_0_DISABLE_CFG_BYTE0 |
		   EMC_PMACRO_CFG_PM_GLOBAL_0_DISABLE_CFG_BYTE1 |
		   EMC_PMACRO_CFG_PM_GLOBAL_0_DISABLE_CFG_BYTE2 |
		   EMC_PMACRO_CFG_PM_GLOBAL_0_DISABLE_CFG_BYTE3 |
		   EMC_PMACRO_CFG_PM_GLOBAL_0_DISABLE_CFG_BYTE4 |
		   EMC_PMACRO_CFG_PM_GLOBAL_0_DISABLE_CFG_BYTE5 |
		   EMC_PMACRO_CFG_PM_GLOBAL_0_DISABLE_CFG_BYTE6 |
		   EMC_PMACRO_CFG_PM_GLOBAL_0_DISABLE_CFG_BYTE7,
		   EMC_PMACRO_CFG_PM_GLOBAL_0);
	emc_writel(emc, EMC_PMACRO_TRAINING_CTRL_0_CH0_TRAINING_E_WRPTR,
		   EMC_PMACRO_TRAINING_CTRL_0);
	emc_writel(emc, EMC_PMACRO_TRAINING_CTRL_1_CH1_TRAINING_E_WRPTR,
		   EMC_PMACRO_TRAINING_CTRL_1);
	emc_writel(emc, 0, EMC_PMACRO_CFG_PM_GLOBAL_0);

	/*
	 * Step 30:
	 *   Re-enable autocal.
	 */
	emc_dbg(emc, STEPS, "Step 30: Re-enable DLL and AUTOCAL\n");

	if (next->burst_regs[EMC_CFG_DIG_DLL_INDEX] & EMC_CFG_DIG_DLL_CFG_DLL_EN) {
		value = emc_readl(emc, EMC_CFG_DIG_DLL);
		value |=  EMC_CFG_DIG_DLL_CFG_DLL_STALL_ALL_TRAFFIC;
		value |=  EMC_CFG_DIG_DLL_CFG_DLL_EN;
		value &= ~EMC_CFG_DIG_DLL_CFG_DLL_STALL_RW_UNTIL_LOCK;
		value &= ~EMC_CFG_DIG_DLL_CFG_DLL_STALL_ALL_UNTIL_LOCK;
		value = (value & ~EMC_CFG_DIG_DLL_CFG_DLL_MODE_MASK) |
			(2 << EMC_CFG_DIG_DLL_CFG_DLL_MODE_SHIFT);
		emc_writel(emc, value, EMC_CFG_DIG_DLL);
		tegra210_emc_timing_update(emc);
	}

	emc_writel(emc, next->emc_auto_cal_config, EMC_AUTO_CAL_CONFIG);

	/* Done! Yay. */
}

const struct tegra210_emc_sequence tegra210_emc_r21021 = {
	.revision = 0x7,
	.set_clock = tegra210_emc_r21021_set_clock,
	.periodic_compensation = tegra210_emc_r21021_periodic_compensation,
};