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
|
/*
* OMAP4 EMIF platform driver
*
* Copyright (C) 2010 Texas Instruments, Inc.
*
* Santosh Shilimkar <santosh.shilimkar@ti.com>
* Aneesh V <aneesh@ti.com>
* Vibhore Vardhan <vvardhan@ti.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/err.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <plat/omap_hwmod.h>
#include <plat/omap_device.h>
#include <mach/emif-44xx.h>
#include <mach/emif.h>
#include <mach/lpddr2-jedec.h>
#include <mach/omap4-common.h>
#include "voltage.h"
/* Utility macro for masking and setting a field in a register/variable */
#define mask_n_set(reg, shift, msk, val) \
(reg) = (((reg) & ~(msk))|(((val) << (shift)) & msk))
struct emif_instance {
void __iomem *base;
u16 irq;
struct platform_device *pdev;
bool ddr_refresh_disabled;
};
static struct emif_instance emif[EMIF_NUM_INSTANCES];
static struct emif_regs *emif_curr_regs[EMIF_NUM_INSTANCES];
static struct emif_regs *emif1_regs_cache[EMIF_MAX_NUM_FREQUENCIES];
static struct emif_regs *emif2_regs_cache[EMIF_MAX_NUM_FREQUENCIES];
static struct emif_device_details *emif_devices[2];
static u32 emif_temperature_level[EMIF_NUM_INSTANCES] = { SDRAM_TEMP_NOMINAL,
SDRAM_TEMP_NOMINAL
};
static u32 emif_notify_pending;
static u32 emif_thermal_handling_pending;
static u32 T_den, T_num;
static struct omap_device_pm_latency omap_emif_latency[] = {
[0] = {
.deactivate_func = omap_device_idle_hwmods,
.activate_func = omap_device_enable_hwmods,
.flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
},
};
static void do_cancel_out(u32 *num, u32 *den, u32 factor)
{
while (1) {
if (((*num) / factor * factor == (*num)) &&
((*den) / factor * factor == (*den))) {
(*num) /= factor;
(*den) /= factor;
} else
break;
}
}
static void cancel_out(u32 *num, u32 *den)
{
do_cancel_out(num, den, 2);
do_cancel_out(num, den, 3);
do_cancel_out(num, den, 5);
do_cancel_out(num, den, 7);
do_cancel_out(num, den, 11);
do_cancel_out(num, den, 13);
do_cancel_out(num, den, 17);
}
/*
* Get the period in ns (in fraction form) for a given frequency:
* Getting it in fraction form is for better accuracy in integer arithmetics
* freq_hz - input: frequency in Hertz
* den_limit - input: upper limit for denominator. see the description of
* EMIF_PERIOD_DEN_LIMIT for more details
* period_den - output: pointer to denominator of period in ns
* period_num - output: pointer to numerator of period in ns
*/
static void get_period(u32 freq_hz, u32 den_limit, u32 *period_num,
u32 *period_den)
{
*period_num = 1000000000; /* 10^9 to convert the period to 'ns' */
*period_den = freq_hz;
cancel_out(period_num, period_den);
/* make sure den <= den_limit at the cost of some accuracy */
while ((*period_den) > den_limit) {
*period_num /= 2;
*period_den /= 2;
}
}
/*
* Calculate the period of DDR clock from frequency value and set the
* denominator and numerator in global variables for easy access later
*/
static void set_ddr_clk_period(u32 freq)
{
get_period(freq, EMIF_PERIOD_DEN_LIMIT, &T_num, &T_den);
}
/*
* Convert time in nano seconds to number of cycles of DDR clock
*/
static u32 ns_2_cycles(u32 ns)
{
return ((ns * T_den) + T_num - 1) / T_num;
}
/*
* ns_2_cycles with the difference that the time passed is 2 times the actual
* value(to avoid fractions). The cycles returned is for the original value of
* the timing parameter
*/
static u32 ns_x2_2_cycles(u32 ns)
{
return ((ns * T_den) + T_num * 2 - 1) / (T_num * 2);
}
/*
* Find addressing table index based on the device's type(S2 or S4) and
* density
*/
static s8 addressing_table_index(u8 type, u8 density, u8 width)
{
u8 index;
if (unlikely((density > LPDDR2_DENSITY_8Gb) ||
(width == LPDDR2_IO_WIDTH_8)))
return -1;
/*
* Look at the way ADDR_TABLE_INDEX* values have been defined
* in emif.h compared to LPDDR2_DENSITY_* values
* The table is layed out in the increasing order of density
* (ignoring type). The exceptions 1GS2 and 2GS2 have been placed
* at the end
*/
if ((type == LPDDR2_TYPE_S2) && (density == LPDDR2_DENSITY_1Gb))
index = ADDR_TABLE_INDEX1GS2;
else if ((type == LPDDR2_TYPE_S2) && (density == LPDDR2_DENSITY_2Gb))
index = ADDR_TABLE_INDEX2GS2;
else
index = density;
pr_debug("emif: addressing table index %d", index);
return index;
}
/*
* Find the the right timing table from the array of timing
* tables of the device using DDR clock frequency
*/
static const struct lpddr2_timings *get_timings_table(
const struct lpddr2_timings * const *device_timings, u32 freq)
{
u32 i, temp, freq_nearest;
const struct lpddr2_timings *timings = NULL;
emif_assert(freq <= MAX_LPDDR2_FREQ);
emif_assert(device_timings);
/*
* Start with the maximum allowed frequency - that is always safe
*/
freq_nearest = MAX_LPDDR2_FREQ;
/*
* Find the timings table that has the max frequency value:
* i. Above or equal to the DDR frequency - safe
* ii. The lowest that satisfies condition (i) - optimal
*/
for (i = 0; i < MAX_NUM_SPEEDBINS; i++) {
if (device_timings[i]) {
temp = device_timings[i]->max_freq;
if ((temp >= freq) && (temp <= freq_nearest)) {
freq_nearest = temp;
timings = device_timings[i];
}
}
}
pr_debug("emif: timings table: %d", freq_nearest);
return timings;
}
/*
* Finds the value of emif_sdram_config_reg
* All parameters are programmed based on the device on CS0.
* If there is a device on CS1, it will be same as that on CS0 or
* it will be NVM. We don't support NVM yet.
* If cs1_device pointer is NULL it is assumed that there is no device
* on CS1
*/
static u32 get_sdram_config_reg(const struct lpddr2_device_info *cs0_device,
const struct lpddr2_device_info *cs1_device,
const struct lpddr2_addressing *addressing,
u8 RL)
{
u32 config_reg = 0;
mask_n_set(config_reg, OMAP44XX_REG_SDRAM_TYPE_SHIFT,
OMAP44XX_REG_SDRAM_TYPE_MASK, cs0_device->type + 4);
mask_n_set(config_reg, OMAP44XX_REG_IBANK_POS_SHIFT,
OMAP44XX_REG_IBANK_POS_MASK,
EMIF_INTERLEAVING_POLICY_MAX_INTERLEAVING);
mask_n_set(config_reg, OMAP44XX_REG_NARROW_MODE_SHIFT,
OMAP44XX_REG_NARROW_MODE_MASK, cs0_device->io_width);
mask_n_set(config_reg, OMAP44XX_REG_CL_SHIFT, OMAP44XX_REG_CL_MASK, RL);
mask_n_set(config_reg, OMAP44XX_REG_ROWSIZE_SHIFT,
OMAP44XX_REG_ROWSIZE_MASK,
addressing->row_sz[cs0_device->io_width]);
mask_n_set(config_reg, OMAP44XX_REG_IBANK_SHIFT,
OMAP44XX_REG_IBANK_MASK, addressing->num_banks);
mask_n_set(config_reg, OMAP44XX_REG_EBANK_SHIFT,
OMAP44XX_REG_EBANK_MASK,
(cs1_device ? EBANK_CS1_EN : EBANK_CS1_DIS));
mask_n_set(config_reg, OMAP44XX_REG_PAGESIZE_SHIFT,
OMAP44XX_REG_PAGESIZE_MASK,
addressing->col_sz[cs0_device->io_width]);
return config_reg;
}
static u32 get_sdram_ref_ctrl(u32 freq,
const struct lpddr2_addressing *addressing)
{
u32 ref_ctrl = 0, val = 0, freq_khz;
freq_khz = freq / 1000;
/*
* refresh rate to be set is 'tREFI * freq in MHz
* division by 10000 to account for khz and x10 in t_REFI_us_x10
*/
val = addressing->t_REFI_us_x10 * freq_khz / 10000;
mask_n_set(ref_ctrl, OMAP44XX_REG_REFRESH_RATE_SHIFT,
OMAP44XX_REG_REFRESH_RATE_MASK, val);
/* enable refresh */
mask_n_set(ref_ctrl, OMAP44XX_REG_INITREF_DIS_SHIFT,
OMAP44XX_REG_INITREF_DIS_MASK, 1);
return ref_ctrl;
}
static u32 get_sdram_tim_1_reg(const struct lpddr2_timings *timings,
const struct lpddr2_min_tck *min_tck,
const struct lpddr2_addressing *addressing)
{
u32 tim1 = 0, val = 0;
val = max(min_tck->tWTR, ns_x2_2_cycles(timings->tWTRx2)) - 1;
mask_n_set(tim1, OMAP44XX_REG_T_WTR_SHIFT, OMAP44XX_REG_T_WTR_MASK,
val);
if (addressing->num_banks == BANKS8)
val = (timings->tFAW * T_den + 4 * T_num - 1) / (4 * T_num) - 1;
else
val = max(min_tck->tRRD, ns_2_cycles(timings->tRRD)) - 1;
mask_n_set(tim1, OMAP44XX_REG_T_RRD_SHIFT, OMAP44XX_REG_T_RRD_MASK,
val);
val = ns_2_cycles(timings->tRASmin + timings->tRPab) - 1;
mask_n_set(tim1, OMAP44XX_REG_T_RC_SHIFT, OMAP44XX_REG_T_RC_MASK, val);
val = max(min_tck->tRAS_MIN, ns_2_cycles(timings->tRASmin)) - 1;
mask_n_set(tim1, OMAP44XX_REG_T_RAS_SHIFT, OMAP44XX_REG_T_RAS_MASK,
val);
val = max(min_tck->tWR, ns_2_cycles(timings->tWR)) - 1;
mask_n_set(tim1, OMAP44XX_REG_T_WR_SHIFT, OMAP44XX_REG_T_WR_MASK, val);
val = max(min_tck->tRCD, ns_2_cycles(timings->tRCD)) - 1;
mask_n_set(tim1, OMAP44XX_REG_T_RCD_SHIFT, OMAP44XX_REG_T_RCD_MASK,
val);
val = max(min_tck->tRP_AB, ns_2_cycles(timings->tRPab)) - 1;
mask_n_set(tim1, OMAP44XX_REG_T_RP_SHIFT, OMAP44XX_REG_T_RP_MASK, val);
return tim1;
}
/*
* Finds the de-rated value for EMIF_SDRAM_TIM1 register
* All the de-rated timings are limited to this register
* Adds 2ns instead of 1.875ns to the affected timings as
* we can not use float.
*/
static u32 get_sdram_tim_1_reg_derated(const struct lpddr2_timings *timings,
const struct lpddr2_min_tck *min_tck,
const struct lpddr2_addressing
*addressing)
{
u32 tim1 = 0, val = 0;
val = max(min_tck->tWTR, ns_x2_2_cycles(timings->tWTRx2)) - 1;
mask_n_set(tim1, OMAP44XX_REG_T_WTR_SHIFT, OMAP44XX_REG_T_WTR_MASK,
val);
if (addressing->num_banks == BANKS8)
/*
* tFAW is approximately 4 times tRRD. So add 1.875*4 = 7.5 ~ 8
* to tFAW for de-rating
*/
val = ((timings->tFAW + 8) * T_den + 4 * T_num - 1)
/ (4 * T_num) - 1;
else
val = max(min_tck->tRRD, ns_2_cycles(timings->tRRD + 2)) - 1;
mask_n_set(tim1, OMAP44XX_REG_T_RRD_SHIFT, OMAP44XX_REG_T_RRD_MASK,
val);
val = ns_2_cycles(timings->tRASmin + timings->tRPab + 2) - 1;
mask_n_set(tim1, OMAP44XX_REG_T_RC_SHIFT, OMAP44XX_REG_T_RC_MASK, val);
val = max(min_tck->tRAS_MIN, ns_2_cycles(timings->tRASmin + 2)) - 1;
mask_n_set(tim1, OMAP44XX_REG_T_RAS_SHIFT, OMAP44XX_REG_T_RAS_MASK,
val);
val = max(min_tck->tWR, ns_2_cycles(timings->tWR)) - 1;
mask_n_set(tim1, OMAP44XX_REG_T_WR_SHIFT, OMAP44XX_REG_T_WR_MASK, val);
val = max(min_tck->tRCD, ns_2_cycles(timings->tRCD + 2)) - 1;
mask_n_set(tim1, OMAP44XX_REG_T_RCD_SHIFT, OMAP44XX_REG_T_RCD_MASK,
val);
val = max(min_tck->tRP_AB, ns_2_cycles(timings->tRPab + 2)) - 1;
mask_n_set(tim1, OMAP44XX_REG_T_RP_SHIFT, OMAP44XX_REG_T_RP_MASK, val);
return tim1;
}
static u32 get_sdram_tim_2_reg(const struct lpddr2_timings *timings,
const struct lpddr2_min_tck *min_tck)
{
u32 tim2 = 0, val = 0;
val = max(min_tck->tCKE, timings->tCKE) - 1;
mask_n_set(tim2, OMAP44XX_REG_T_CKE_SHIFT, OMAP44XX_REG_T_CKE_MASK,
val);
val = max(min_tck->tRTP, ns_x2_2_cycles(timings->tRTPx2)) - 1;
mask_n_set(tim2, OMAP44XX_REG_T_RTP_SHIFT, OMAP44XX_REG_T_RTP_MASK,
val);
/*
* tXSRD = tRFCab + 10 ns. XSRD and XSNR should have the
* same value
*/
val = ns_2_cycles(timings->tXSR) - 1;
mask_n_set(tim2, OMAP44XX_REG_T_XSRD_SHIFT, OMAP44XX_REG_T_XSRD_MASK,
val);
mask_n_set(tim2, OMAP44XX_REG_T_XSNR_SHIFT, OMAP44XX_REG_T_XSNR_MASK,
val);
val = max(min_tck->tXP, ns_x2_2_cycles(timings->tXPx2)) - 1;
mask_n_set(tim2, OMAP44XX_REG_T_XP_SHIFT, OMAP44XX_REG_T_XP_MASK, val);
return tim2;
}
static u32 get_sdram_tim_3_reg(const struct lpddr2_timings *timings,
const struct lpddr2_min_tck *min_tck,
const struct lpddr2_addressing *addressing)
{
u32 tim3 = 0, val = 0;
val = min(timings->tRASmax * 10 / addressing->t_REFI_us_x10 - 1, 0xF);
mask_n_set(tim3, OMAP44XX_REG_T_RAS_MAX_SHIFT,
OMAP44XX_REG_T_RAS_MAX_MASK, val);
val = ns_2_cycles(timings->tRFCab) - 1;
mask_n_set(tim3, OMAP44XX_REG_T_RFC_SHIFT, OMAP44XX_REG_T_RFC_MASK,
val);
val = ns_x2_2_cycles(timings->tDQSCKMAXx2) - 1;
mask_n_set(tim3, OMAP44XX_REG_T_TDQSCKMAX_SHIFT,
OMAP44XX_REG_T_TDQSCKMAX_MASK, val);
val = ns_2_cycles(timings->tZQCS) - 1;
mask_n_set(tim3, OMAP44XX_REG_ZQ_ZQCS_SHIFT,
OMAP44XX_REG_ZQ_ZQCS_MASK, val);
val = max(min_tck->tCKESR, ns_2_cycles(timings->tCKESR)) - 1;
mask_n_set(tim3, OMAP44XX_REG_T_CKESR_SHIFT,
OMAP44XX_REG_T_CKESR_MASK, val);
return tim3;
}
static u32 get_zq_config_reg(const struct lpddr2_device_info *cs1_device,
const struct lpddr2_addressing *addressing,
bool volt_ramp)
{
u32 zq = 0, val = 0;
if (volt_ramp)
val =
EMIF_ZQCS_INTERVAL_DVFS_IN_US * 10 /
addressing->t_REFI_us_x10;
else
val =
EMIF_ZQCS_INTERVAL_NORMAL_IN_US * 10 /
addressing->t_REFI_us_x10;
mask_n_set(zq, OMAP44XX_REG_ZQ_REFINTERVAL_SHIFT,
OMAP44XX_REG_ZQ_REFINTERVAL_MASK, val);
mask_n_set(zq, OMAP44XX_REG_ZQ_ZQCL_MULT_SHIFT,
OMAP44XX_REG_ZQ_ZQCL_MULT_MASK, REG_ZQ_ZQCL_MULT - 1);
mask_n_set(zq, OMAP44XX_REG_ZQ_ZQINIT_MULT_SHIFT,
OMAP44XX_REG_ZQ_ZQINIT_MULT_MASK, REG_ZQ_ZQINIT_MULT - 1);
mask_n_set(zq, OMAP44XX_REG_ZQ_SFEXITEN_SHIFT,
OMAP44XX_REG_ZQ_SFEXITEN_MASK, REG_ZQ_SFEXITEN_ENABLE);
/*
* Assuming that two chipselects have a single calibration resistor
* If there are indeed two calibration resistors, then this flag should
* be enabled to take advantage of dual calibration feature.
* This data should ideally come from board files. But considering
* that none of the boards today have calibration resistors per CS,
* it would be an unnecessary overhead.
*/
mask_n_set(zq, OMAP44XX_REG_ZQ_DUALCALEN_SHIFT,
OMAP44XX_REG_ZQ_DUALCALEN_MASK, REG_ZQ_DUALCALEN_DISABLE);
mask_n_set(zq, OMAP44XX_REG_ZQ_CS0EN_SHIFT,
OMAP44XX_REG_ZQ_CS0EN_MASK, REG_ZQ_CS0EN_ENABLE);
mask_n_set(zq, OMAP44XX_REG_ZQ_CS1EN_SHIFT,
OMAP44XX_REG_ZQ_CS1EN_MASK, (cs1_device ? 1 : 0));
return zq;
}
static u32 get_temp_alert_config(const struct lpddr2_device_info *cs1_device,
const struct lpddr2_addressing *addressing,
bool is_derated)
{
u32 alert = 0, interval;
interval =
TEMP_ALERT_POLL_INTERVAL_MS * 10000 / addressing->t_REFI_us_x10;
if (is_derated)
interval *= 4;
mask_n_set(alert, OMAP44XX_REG_TA_REFINTERVAL_SHIFT,
OMAP44XX_REG_TA_REFINTERVAL_MASK, interval);
mask_n_set(alert, OMAP44XX_REG_TA_DEVCNT_SHIFT,
OMAP44XX_REG_TA_DEVCNT_MASK, TEMP_ALERT_CONFIG_DEVCT_1);
mask_n_set(alert, OMAP44XX_REG_TA_DEVWDT_SHIFT,
OMAP44XX_REG_TA_DEVWDT_MASK, TEMP_ALERT_CONFIG_DEVWDT_32);
mask_n_set(alert, OMAP44XX_REG_TA_SFEXITEN_SHIFT,
OMAP44XX_REG_TA_SFEXITEN_MASK, 1);
mask_n_set(alert, OMAP44XX_REG_TA_CS0EN_SHIFT,
OMAP44XX_REG_TA_CS0EN_MASK, 1);
mask_n_set(alert, OMAP44XX_REG_TA_CS1EN_SHIFT,
OMAP44XX_REG_TA_CS1EN_MASK, (cs1_device ? 1 : 0));
return alert;
}
static u32 get_read_idle_ctrl_reg(bool volt_ramp)
{
u32 idle = 0, val = 0;
if (volt_ramp)
val = ns_2_cycles(READ_IDLE_INTERVAL_DVFS) / 64 - 1;
else
/*Maximum value in normal conditions - suggested by hw team */
val = 0x1FF;
mask_n_set(idle, OMAP44XX_REG_READ_IDLE_INTERVAL_SHIFT,
OMAP44XX_REG_READ_IDLE_INTERVAL_MASK, val);
mask_n_set(idle, OMAP44XX_REG_READ_IDLE_LEN_SHIFT,
OMAP44XX_REG_READ_IDLE_LEN_MASK, EMIF_REG_READ_IDLE_LEN_VAL);
return idle;
}
static u32 get_ddr_phy_ctrl_1(u32 freq, u8 RL)
{
u32 phy = 0, val = 0;
mask_n_set(phy, OMAP44XX_REG_READ_LATENCY_SHIFT,
OMAP44XX_REG_READ_LATENCY_MASK, RL + 2);
if (freq <= 100000000)
val = EMIF_DLL_SLAVE_DLY_CTRL_100_MHZ_AND_LESS;
else if (freq <= 200000000)
val = EMIF_DLL_SLAVE_DLY_CTRL_200_MHZ;
else
val = EMIF_DLL_SLAVE_DLY_CTRL_400_MHZ;
mask_n_set(phy, OMAP44XX_REG_DLL_SLAVE_DLY_CTRL_SHIFT,
OMAP44XX_REG_DLL_SLAVE_DLY_CTRL_MASK, val);
/* Other fields are constant magic values. Hardcode them together */
mask_n_set(phy, OMAP44XX_EMIF_DDR_PHY_CTRL_1_BASE_VAL_SHIFT,
OMAP44XX_EMIF_DDR_PHY_CTRL_1_BASE_VAL_MASK,
EMIF_DDR_PHY_CTRL_1_BASE_VAL);
phy >>= OMAP44XX_REG_DDR_PHY_CTRL_1_SHIFT;
return phy;
}
/*
* get_lp_mode - Get the LP Mode of a EMIF instance.
*
* It returns the REG_LP_MODE of EMIF_PWR_MGMT_CTRL[10:8]
* for a EMIF.
*
*/
static u32 get_lp_mode(u32 emif_nr)
{
u32 temp, lpmode;
void __iomem *base = emif[emif_nr].base;
temp = readl(base + OMAP44XX_EMIF_PWR_MGMT_CTRL);
lpmode = (temp & OMAP44XX_REG_LP_MODE_MASK) >>
OMAP44XX_REG_LP_MODE_SHIFT;
return lpmode;
}
/*
* set_lp_mode - Set the LP Mode of a EMIF instance.
*
* It replaces the REG_LP_MODE of EMIF_PWR_MGMT_CTRL[10:8]
* with the new value for a EMIF.
*
*/
static void set_lp_mode(u32 emif_nr, u32 lpmode)
{
u32 temp;
void __iomem *base = emif[emif_nr].base;
/* Extract current lp mode value */
temp = readl(base + OMAP44XX_EMIF_PWR_MGMT_CTRL);
/* Write out the new lp mode value */
temp &= ~OMAP44XX_REG_LP_MODE_MASK;
temp |= lpmode << OMAP44XX_REG_LP_MODE_SHIFT;
writel(temp, base + OMAP44XX_EMIF_PWR_MGMT_CTRL);
}
/*
* Get the temperature level of the EMIF instance:
* Reads the MR4 register of attached SDRAM parts to find out the temperature
* level. If there are two parts attached(one on each CS), then the temperature
* level for the EMIF instance is the higher of the two temperatures.
*/
static u32 get_temperature_level(u32 emif_nr)
{
u32 temp, tmp_temperature_level;
bool cs1_used;
void __iomem *base;
base = emif[emif_nr].base;
temp = __raw_readl(base + OMAP44XX_EMIF_SDRAM_CONFIG);
cs1_used = (temp & OMAP44XX_REG_EBANK_MASK) ? true : false;
/* Read mode register 4 */
__raw_writel(LPDDR2_MR4, base + OMAP44XX_EMIF_LPDDR2_MODE_REG_CFG);
tmp_temperature_level = __raw_readl(base +
OMAP44XX_EMIF_LPDDR2_MODE_REG_DATA);
tmp_temperature_level = (tmp_temperature_level &
MR4_SDRAM_REF_RATE_MASK) >>
MR4_SDRAM_REF_RATE_SHIFT;
if (cs1_used) {
__raw_writel(LPDDR2_MR4 | OMAP44XX_REG_CS_MASK,
base + OMAP44XX_EMIF_LPDDR2_MODE_REG_CFG);
temp = __raw_readl(base + OMAP44XX_EMIF_LPDDR2_MODE_REG_DATA);
temp = (temp & MR4_SDRAM_REF_RATE_MASK)
>> MR4_SDRAM_REF_RATE_SHIFT;
tmp_temperature_level = max(temp, tmp_temperature_level);
}
/* treat everything less than nominal(3) in MR4 as nominal */
if (unlikely(tmp_temperature_level < SDRAM_TEMP_NOMINAL))
tmp_temperature_level = SDRAM_TEMP_NOMINAL;
/* if we get reserved value in MR4 persist with the existing value */
if (unlikely(tmp_temperature_level == SDRAM_TEMP_RESERVED_4))
tmp_temperature_level = emif_temperature_level[emif_nr];
return tmp_temperature_level;
}
/*
* Program EMIF shadow registers:
* Sets the shadow registers using pre-caulated register values
* When volt_state indicates that this function is called just before
* a voltage scaling, set only the registers relevant for voltage scaling
* Otherwise, set all the registers relevant for a frequency change
*/
static void setup_registers(u32 emif_nr, struct emif_regs *regs, u32 volt_state)
{
u32 temp,read_idle;
void __iomem *base = emif[emif_nr].base;
__raw_writel(regs->ref_ctrl, base + OMAP44XX_EMIF_SDRAM_REF_CTRL_SHDW);
__raw_writel(regs->sdram_tim2, base + OMAP44XX_EMIF_SDRAM_TIM_2_SHDW);
__raw_writel(regs->sdram_tim3, base + OMAP44XX_EMIF_SDRAM_TIM_3_SHDW);
/*
* Do not change the RL part in PHY CTRL register
* RL is not changed during DVFS
*/
temp = __raw_readl(base + OMAP44XX_EMIF_DDR_PHY_CTRL_1_SHDW);
mask_n_set(temp, OMAP44XX_REG_DDR_PHY_CTRL_1_SHDW_SHIFT,
OMAP44XX_REG_DDR_PHY_CTRL_1_SHDW_MASK,
regs->emif_ddr_phy_ctlr_1_final);
__raw_writel(temp, base + OMAP44XX_EMIF_DDR_PHY_CTRL_1_SHDW);
__raw_writel(regs->temp_alert_config,
base + OMAP44XX_EMIF_TEMP_ALERT_CONFIG);
/*
* When voltage ramps forced read idle should
* happen more often.
*/
if (volt_state == LPDDR2_VOLTAGE_RAMPING)
read_idle = regs->read_idle_ctrl_volt_ramp;
else
read_idle = regs->read_idle_ctrl_normal;
__raw_writel(read_idle, base + OMAP44XX_EMIF_READ_IDLE_CTRL_SHDW);
/*
* Reading back the last written register to ensure all writes are
* complete
*/
temp = __raw_readl(base + OMAP44XX_EMIF_READ_IDLE_CTRL_SHDW);
}
/*
* setup_temperature_sensitive_regs() - set the timings for temperature
* sensitive registers. This happens once at initialization time based
* on the temperature at boot time and subsequently based on the temperature
* alert interrupt. Temperature alert can happen when the temperature
* increases or drops. So this function can have the effect of either
* derating the timings or going back to nominal values.
*/
static void setup_temperature_sensitive_regs(u32 emif_nr,
struct emif_regs *regs)
{
u32 tim1, ref_ctrl, temp_alert_cfg;
void __iomem *base = emif[emif_nr].base;
u32 temperature = emif_temperature_level[emif_nr];
if (unlikely(temperature == SDRAM_TEMP_HIGH_DERATE_REFRESH)) {
tim1 = regs->sdram_tim1;
ref_ctrl = regs->ref_ctrl_derated;
temp_alert_cfg = regs->temp_alert_config_derated;
} else if (unlikely(temperature ==
SDRAM_TEMP_HIGH_DERATE_REFRESH_AND_TIMINGS)) {
tim1 = regs->sdram_tim1_derated;
ref_ctrl = regs->ref_ctrl_derated;
temp_alert_cfg = regs->temp_alert_config_derated;
} else {
/*
* Nominal timings - you may switch back to the
* nominal timings if the temperature falls
*/
tim1 = regs->sdram_tim1;
ref_ctrl = regs->ref_ctrl;
temp_alert_cfg = regs->temp_alert_config;
}
__raw_writel(tim1, base + OMAP44XX_EMIF_SDRAM_TIM_1_SHDW);
__raw_writel(temp_alert_cfg, base + OMAP44XX_EMIF_TEMP_ALERT_CONFIG);
__raw_writel(ref_ctrl, base + OMAP44XX_EMIF_SDRAM_REF_CTRL_SHDW);
/* read back last written register to ensure write is complete */
__raw_readl(base + OMAP44XX_EMIF_SDRAM_REF_CTRL_SHDW);
}
static irqreturn_t handle_temp_alert(void __iomem *base, u32 emif_nr)
{
u32 old_temperature_level;
old_temperature_level = emif_temperature_level[emif_nr];
emif_temperature_level[emif_nr] = get_temperature_level(emif_nr);
if (unlikely(emif_temperature_level[emif_nr] == old_temperature_level))
return IRQ_HANDLED;
emif_notify_pending |= (1 << emif_nr);
if (likely(emif_temperature_level[emif_nr] < old_temperature_level)) {
/* Temperature coming down - defer handling to thread */
emif_thermal_handling_pending |= (1 << emif_nr);
} else if (likely(emif_temperature_level[emif_nr] !=
SDRAM_TEMP_VERY_HIGH_SHUTDOWN)) {
/* Temperature is going up - handle immediately */
setup_temperature_sensitive_regs(emif_nr,
emif_curr_regs[emif_nr]);
/*
* EMIF de-rated timings register needs to be setup using
* freq update method only
*/
omap4_prcm_freq_update();
}
return IRQ_WAKE_THREAD;
}
static void setup_volt_sensitive_registers(u32 emif_nr, struct emif_regs *regs,
u32 volt_state)
{
u32 read_idle;
void __iomem *base = emif[emif_nr].base;
/*
* When voltage ramps forced read idle should
* happen more often.
*/
if (volt_state == LPDDR2_VOLTAGE_RAMPING)
read_idle = regs->read_idle_ctrl_volt_ramp;
else
read_idle = regs->read_idle_ctrl_normal;
__raw_writel(read_idle, base + OMAP44XX_EMIF_READ_IDLE_CTRL_SHDW);
/* read back last written register to ensure write is complete */
__raw_readl(base + OMAP44XX_EMIF_READ_IDLE_CTRL_SHDW);
return;
}
/*
* Interrupt Handler for EMIF1 and EMIF2
*/
static irqreturn_t emif_interrupt_handler(int irq, void *dev_id)
{
void __iomem *base;
irqreturn_t ret = IRQ_HANDLED;
u32 sys, ll;
u8 emif_nr = EMIF1;
if (emif[EMIF2].irq == irq)
emif_nr = EMIF2;
base = emif[emif_nr].base;
/* Save the status and clear it */
sys = __raw_readl(base + OMAP44XX_EMIF_IRQSTATUS_SYS);
ll = __raw_readl(base + OMAP44XX_EMIF_IRQSTATUS_LL);
__raw_writel(sys, base + OMAP44XX_EMIF_IRQSTATUS_SYS);
__raw_writel(ll, base + OMAP44XX_EMIF_IRQSTATUS_LL);
/*
* Handle temperature alert
* Temperature alert should be same for both ports
* So, it's enough to process it for only one of the ports
*/
if (sys & OMAP44XX_REG_TA_SYS_MASK)
ret = handle_temp_alert(base, emif_nr);
if (sys & OMAP44XX_REG_ERR_SYS_MASK)
pr_err("EMIF: Access error from EMIF%d SYS port - %x",
emif_nr, sys);
if (ll & OMAP44XX_REG_ERR_LL_MASK)
pr_err("EMIF Error: Access error from EMIF%d LL port - %x",
emif_nr, ll);
return ret;
}
static irqreturn_t emif_threaded_isr(int irq, void *dev_id)
{
u8 emif_nr = EMIF1;
if (emif[EMIF2].irq == irq)
emif_nr = EMIF2;
if (emif_thermal_handling_pending & (1 << emif_nr)) {
setup_temperature_sensitive_regs(emif_nr,
emif_curr_regs[emif_nr]);
/*
* EMIF de-rated timings register needs to be setup using
* freq update method only
*/
omap4_prcm_freq_update();
/* clear the bit */
emif_thermal_handling_pending &= ~(1 << emif_nr);
}
if (emif_notify_pending & (1 << emif_nr)) {
sysfs_notify(&(emif[emif_nr].pdev->dev.kobj), NULL,
"temperature");
kobject_uevent(&(emif[emif_nr].pdev->dev.kobj), KOBJ_CHANGE);
/* clear the bit */
emif_notify_pending &= ~(1 << emif_nr);
}
return IRQ_HANDLED;
}
static int __init setup_emif_interrupts(u32 emif_nr)
{
u32 temp;
void __iomem *base = emif[emif_nr].base;
int r;
/* Clear any pendining interrupts */
__raw_writel(0xFFFFFFFF, base + OMAP44XX_EMIF_IRQSTATUS_SYS);
__raw_writel(0xFFFFFFFF, base + OMAP44XX_EMIF_IRQSTATUS_LL);
/* Enable the relevant interrupts for both LL and SYS */
temp = OMAP44XX_REG_EN_TA_SYS_MASK | OMAP44XX_REG_EN_ERR_SYS_MASK;
__raw_writel(temp, base + OMAP44XX_EMIF_IRQENABLE_SET_SYS);
__raw_writel(temp, base + OMAP44XX_EMIF_IRQENABLE_SET_LL);
/* Dummy read to make sure writes are complete */
__raw_readl(base + OMAP44XX_EMIF_IRQENABLE_SET_LL);
/* setup IRQ handlers */
r = request_threaded_irq(emif[emif_nr].irq,
emif_interrupt_handler,
emif_threaded_isr,
IRQF_SHARED, emif[emif_nr].pdev->name,
emif[emif_nr].pdev);
if (r) {
pr_err("%s: Failed: request_irq emif[%d] IRQ%d:%d\n",
__func__, emif_nr, emif[emif_nr].irq, r);
return r;
}
/*
* Even if we fail to make the irq wakeup capable, we are at risk only
* while going to suspend where the device is cooler, we might lose a
* bit of power due to pending interrupt preventing core from hitting
* low power state but we can continue to handle events in active use
* cases. So don't free interrupt on failure of marking wakeup capable,
* just warn and continue.
*/
if (enable_irq_wake(emif[emif_nr].irq))
pr_err("%s: Failed: wakeupen emif[%d] IRQ%d\n", __func__,
emif_nr, emif[emif_nr].irq);
return 0;
}
static ssize_t emif_temperature_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
u32 temperature;
if (dev == &(emif[EMIF1].pdev->dev))
temperature = emif_temperature_level[EMIF1];
else if (dev == &(emif[EMIF2].pdev->dev))
temperature = emif_temperature_level[EMIF2];
else
return 0;
return snprintf(buf, 20, "%u\n", temperature);
}
static DEVICE_ATTR(temperature, S_IRUGO, emif_temperature_show, NULL);
static int __devinit omap_emif_probe(struct platform_device *pdev)
{
int id;
struct resource *res;
if (!pdev)
return -EINVAL;
id = pdev->id;
emif[id].pdev = pdev;
res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
if (!res) {
pr_err("EMIF %i Invalid IRQ resource\n", id);
return -ENODEV;
}
emif[id].irq = res->start;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res) {
pr_err("EMIF%i Invalid mem resource\n", id);
return -ENODEV;
}
emif[id].base = ioremap(res->start, SZ_1M);
if (!emif[id].base) {
pr_err("Could not ioremap EMIF%i\n", id);
return -ENOMEM;
}
pr_info("EMIF%d is enabled with IRQ%d\n", id, emif[id].irq);
emif[id].ddr_refresh_disabled = false;
return 0;
}
static int emif_init(struct omap_hwmod *oh, void *user)
{
char *name = "omap_emif";
struct omap_device *od;
static int id;
od = omap_device_build(name, id, oh, NULL, 0, omap_emif_latency,
ARRAY_SIZE(omap_emif_latency), false);
WARN(IS_ERR(od), "Can't build omap_device for %s:%s.\n",
name, oh->name);
id++;
return 0;
}
static void emif_calculate_regs(const struct emif_device_details *devices,
u32 freq, struct emif_regs *regs)
{
u32 temp;
const struct lpddr2_addressing *addressing;
const struct lpddr2_timings *timings;
const struct lpddr2_min_tck *min_tck;
const struct lpddr2_device_info *cs0_device = devices->cs0_device;
const struct lpddr2_device_info *cs1_device = devices->cs1_device;
emif_assert(devices);
emif_assert(regs);
/*
* You can not have a device on CS1 without one on CS0
* So configuring EMIF without a device on CS0 doesn't
* make sense
*/
emif_assert(cs0_device);
emif_assert(cs0_device->type != LPDDR2_TYPE_NVM);
/*
* If there is a device on CS1 it should be same type as CS0
* (or NVM. But NVM is not supported in this driver yet)
*/
emif_assert((cs1_device == NULL) ||
(cs1_device->type == LPDDR2_TYPE_NVM) ||
(cs0_device->type == cs1_device->type));
emif_assert(freq <= MAX_LPDDR2_FREQ);
set_ddr_clk_period(freq);
/*
* The device on CS0 is used for all timing calculations
* There is only one set of registers for timings per EMIF. So, if the
* second CS(CS1) has a device, it should have the same timings as the
* device on CS0
*/
timings = get_timings_table(cs0_device->device_timings, freq);
emif_assert(timings);
min_tck = cs0_device->min_tck;
temp =
addressing_table_index(cs0_device->type, cs0_device->density,
cs0_device->io_width);
emif_assert((temp >= 0));
addressing = &(lpddr2_jedec_addressing_table[temp]);
emif_assert(addressing);
regs->RL_final = timings->RL;
/*
* Initial value of EMIF_SDRAM_CONFIG corresponds to the base
* frequency - 19.2 MHz
*/
regs->sdram_config_init =
get_sdram_config_reg(cs0_device, cs1_device, addressing,
RL_19_2_MHZ);
regs->sdram_config_final = regs->sdram_config_init;
mask_n_set(regs->sdram_config_final, OMAP44XX_REG_CL_SHIFT,
OMAP44XX_REG_CL_MASK, timings->RL);
regs->ref_ctrl = get_sdram_ref_ctrl(freq, addressing);
regs->ref_ctrl_derated = regs->ref_ctrl / 4;
regs->sdram_tim1 = get_sdram_tim_1_reg(timings, min_tck, addressing);
regs->sdram_tim1_derated =
get_sdram_tim_1_reg_derated(timings, min_tck, addressing);
regs->sdram_tim2 = get_sdram_tim_2_reg(timings, min_tck);
regs->sdram_tim3 = get_sdram_tim_3_reg(timings, min_tck, addressing);
regs->read_idle_ctrl_normal =
get_read_idle_ctrl_reg(LPDDR2_VOLTAGE_STABLE);
regs->read_idle_ctrl_volt_ramp =
get_read_idle_ctrl_reg(LPDDR2_VOLTAGE_RAMPING);
regs->zq_config_normal =
get_zq_config_reg(cs1_device, addressing, LPDDR2_VOLTAGE_STABLE);
regs->zq_config_volt_ramp =
get_zq_config_reg(cs1_device, addressing, LPDDR2_VOLTAGE_RAMPING);
regs->temp_alert_config =
get_temp_alert_config(cs1_device, addressing, false);
regs->temp_alert_config_derated =
get_temp_alert_config(cs1_device, addressing, true);
regs->emif_ddr_phy_ctlr_1_init =
get_ddr_phy_ctrl_1(EMIF_FREQ_19_2_MHZ, RL_19_2_MHZ);
regs->emif_ddr_phy_ctlr_1_final =
get_ddr_phy_ctrl_1(freq, regs->RL_final);
/* save the frequency in the struct to act as a tag when cached */
regs->freq = freq;
pr_debug("Calculated EMIF configuration register values "
"for %d MHz", freq / 1000000);
pr_debug("sdram_config_init\t\t: 0x%08x\n", regs->sdram_config_init);
pr_debug("sdram_config_final\t\t: 0x%08x\n", regs->sdram_config_final);
pr_debug("sdram_ref_ctrl\t\t: 0x%08x\n", regs->ref_ctrl);
pr_debug("sdram_ref_ctrl_derated\t\t: 0x%08x\n",
regs->ref_ctrl_derated);
pr_debug("sdram_tim_1_reg\t\t: 0x%08x\n", regs->sdram_tim1);
pr_debug("sdram_tim_1_reg_derated\t\t: 0x%08x\n",
regs->sdram_tim1_derated);
pr_debug("sdram_tim_2_reg\t\t: 0x%08x\n", regs->sdram_tim2);
pr_debug("sdram_tim_3_reg\t\t: 0x%08x\n", regs->sdram_tim3);
pr_debug("emif_read_idle_ctrl_normal\t: 0x%08x\n",
regs->read_idle_ctrl_normal);
pr_debug("emif_read_idle_ctrl_dvfs\t: 0x%08x\n",
regs->read_idle_ctrl_volt_ramp);
pr_debug("zq_config_reg_normal\t: 0x%08x\n", regs->zq_config_normal);
pr_debug("zq_config_reg_dvfs\t\t: 0x%08x\n", regs->zq_config_volt_ramp);
pr_debug("temp_alert_config\t: 0x%08x\n", regs->temp_alert_config);
pr_debug("emif_ddr_phy_ctlr_1_init\t: 0x%08x\n",
regs->emif_ddr_phy_ctlr_1_init);
pr_debug("emif_ddr_phy_ctlr_1_final\t: 0x%08x\n",
regs->emif_ddr_phy_ctlr_1_final);
}
/*
* get_regs() - gets the cached emif_regs structure for a given EMIF instance
* (emif_nr) for a given frequency(freq):
*
* As an optimization, only one cache array(that of EMIF1) if both EMIF1 and
* EMIF2 has identical devices
*
* If we do not have an entry corresponding to the frequency given, we
* allocate a new entry and calculate the values
*/
static struct emif_regs *get_regs(u32 emif_nr, u32 freq)
{
int i;
struct emif_regs **regs_cache;
struct emif_regs *regs = NULL;
/*
* If EMIF2 has the same devices as EMIF1 use the register
* cache of EMIF1
*/
if ((emif_nr == EMIF1) ||
((emif_nr == EMIF2)
&& (emif_devices[EMIF1] == emif_devices[EMIF2])))
regs_cache = emif1_regs_cache;
else
regs_cache = emif2_regs_cache;
for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++) {
if (regs_cache[i]->freq == freq) {
regs = regs_cache[i];
break;
}
}
/*
* If we don't have an entry for this frequency in the cache create one
* and calculate the values
*/
if (!regs) {
regs = kmalloc(sizeof(struct emif_regs), GFP_ATOMIC);
if (!regs)
return NULL;
emif_calculate_regs(emif_devices[emif_nr], freq, regs);
/*
* Now look for an un-used entry in the cache and save the
* newly created struct. If there are no free entries
* over-write the last entry
*/
for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++)
;
if (i >= EMIF_MAX_NUM_FREQUENCIES) {
pr_warning("emif: emif regs_cache full - more number"
" of frequencies used than expected!!");
i = EMIF_MAX_NUM_FREQUENCIES - 1;
kfree(regs_cache[i]);
}
regs_cache[i] = regs;
}
return regs;
}
static int do_emif_setup_registers(u32 emif_nr, u32 freq, u32 volt_state)
{
struct emif_regs *regs;
regs = get_regs(emif_nr, freq);
if (!regs)
return -ENOMEM;
emif_curr_regs[emif_nr] = regs;
setup_registers(emif_nr, regs, volt_state);
setup_temperature_sensitive_regs(emif_nr, regs);
return 0;
}
static int do_setup_device_details(u32 emif_nr,
const struct emif_device_details *devices)
{
if (!emif_devices[emif_nr]) {
emif_devices[emif_nr] =
kmalloc(sizeof(struct emif_device_details), GFP_KERNEL);
if (!emif_devices[emif_nr])
return -ENOMEM;
*emif_devices[emif_nr] = *devices;
}
return 0;
}
/*
* Initialize the temperature level and setup the sysfs nodes
* and uvent for temperature monitoring
*/
static void init_temperature(u32 emif_nr)
{
if (!emif_devices[emif_nr])
return;
emif_temperature_level[emif_nr] = get_temperature_level(emif_nr);
WARN_ON(device_create_file(&(emif[emif_nr].pdev->dev),
&dev_attr_temperature));
kobject_uevent(&(emif[emif_nr].pdev->dev.kobj), KOBJ_ADD);
if (emif_temperature_level[emif_nr] == SDRAM_TEMP_VERY_HIGH_SHUTDOWN)
pr_emerg("EMIF %d: SDRAM temperature exceeds operating"
"limit.. Needs shut down!!!", emif_nr + 1);
}
/*
* omap_emif_device_init needs to be done before
* ddr reconfigure function call.
* Hence omap_emif_device_init is a postcore_initcall.
*/
static int __init omap_emif_device_init(void)
{
/*
* To avoid code running on other OMAPs in
* multi-omap builds
*/
if (!cpu_is_omap44xx())
return -ENODEV;
return omap_hwmod_for_each_by_class("emif", emif_init, NULL);
}
postcore_initcall(omap_emif_device_init);
/* We need to disable interrupts of the EMIF
* module, because in a warm reboot scenario, there
* may be a pending irq that is not serviced and emif
* is stuck in transition. On the next boot HW mod
* fails emif inizalization with a timeout.
*/
void emif_clear_irq(int emif_id)
{
u32 irq_mask = 0;
u32 base = 0;
u32 reg = 0;
if (emif_id == 0)
base = OMAP44XX_EMIF1_VIRT;
else
base = OMAP44XX_EMIF2_VIRT;
/* Disable the relevant interrupts for both LL and SYS */
irq_mask = OMAP44XX_REG_EN_TA_SYS_MASK | OMAP44XX_REG_EN_ERR_SYS_MASK
| OMAP44XX_REG_EN_DNV_SYS_MASK;
__raw_writel(irq_mask, base + OMAP44XX_EMIF_IRQENABLE_CLR_SYS);
__raw_writel(irq_mask, base + OMAP44XX_EMIF_IRQENABLE_CLR_LL);
/* Clear any pendining interrupts without overwritng reserved bits*/
reg = __raw_readl(base + OMAP44XX_EMIF_IRQSTATUS_SYS);
reg |= irq_mask;
__raw_writel(reg, base + OMAP44XX_EMIF_IRQSTATUS_SYS);
reg = __raw_readl(base + OMAP44XX_EMIF_IRQSTATUS_LL);
reg |= irq_mask;
__raw_writel(reg, base + OMAP44XX_EMIF_IRQSTATUS_LL);
/* Dummy read to make sure writes are complete */
__raw_readl(base + OMAP44XX_EMIF_IRQENABLE_SET_LL);
return;
}
void emif_driver_shutdown(struct platform_device *pdev)
{
emif_clear_irq(pdev->id);
}
static struct platform_driver omap_emif_driver = {
.probe = omap_emif_probe,
.driver = {
.name = "omap_emif",
},
.shutdown = emif_driver_shutdown,
};
static int __init omap_emif_register(void)
{
return platform_driver_register(&omap_emif_driver);
}
postcore_initcall(omap_emif_register);
/*
* omap_emif_notify_voltage - setup the voltage sensitive
* registers based on the voltage situation (voltage ramping or stable)
* read_idle_ctrl and zq_config are the registers that are voltage sensitive
* They need to have a very safe value(more frequent zq calibration and
* read idle forcing) when voltage is scaling and can have a more relaxed
* nominal value(frequency dependent) when voltage is stable
*/
int omap_emif_notify_voltage(struct notifier_block *nb,
unsigned long val, void *data)
{
u32 volt_state;
if (val == OMAP_VOLTAGE_PRECHANGE)
volt_state = LPDDR2_VOLTAGE_RAMPING;
else
volt_state = LPDDR2_VOLTAGE_STABLE;
if (likely(emif_curr_regs[EMIF1]))
setup_volt_sensitive_registers(EMIF1, emif_curr_regs[EMIF1],
volt_state);
if (likely(emif_curr_regs[EMIF2]))
setup_volt_sensitive_registers(EMIF2, emif_curr_regs[EMIF2],
volt_state);
if (unlikely(!emif_curr_regs[EMIF1] && !emif_curr_regs[EMIF2])) {
pr_err_once("emif: voltage state notification came before the"
" initial setup - ignoring the notification");
return -EINVAL;
}
/*
* EMIF read-idle control needs to be setup using
* freq update method only
*/
return omap4_prcm_freq_update();
}
static struct notifier_block emif_volt_notifier_block = {
.notifier_call = omap_emif_notify_voltage,
};
static int __init omap_emif_late_init(void)
{
struct voltagedomain *voltdm = voltdm_lookup("core");
if (!voltdm) {
pr_err("CORE voltage domain lookup failed\n");
return -EINVAL;
}
voltdm_register_notifier(voltdm, &emif_volt_notifier_block);
return 0;
}
late_initcall(omap_emif_late_init);
/*
* omap_emif_setup_registers - setup the shadow registers for a given
* frequency. This will be typically followed by a FREQ_UPDATE procedure
* to lock at the new frequency and this will update the EMIF main registers
* with shadow register values
*/
int omap_emif_setup_registers(u32 freq, u32 volt_state)
{
int err = 0;
if (likely(emif_devices[EMIF1]))
err = do_emif_setup_registers(EMIF1, freq, volt_state);
if (likely(!err && emif_devices[EMIF2]))
err = do_emif_setup_registers(EMIF2, freq, volt_state);
return err;
}
/*
* omap_emif_frequency_pre_notify - Disable DDR self refresh of both EMIFs
*
* It disables the LP mode if the LP mode of EMIFs was LP_MODE_SELF_REFRESH.
*
* It should be called before any PRCM frequency update sequence.
* After the frequency update sequence, omap_emif_frequency_post_notify
* should be called to restore the original LP MODE setting of the EMIFs.
*
*/
void omap_emif_frequency_pre_notify(void)
{
int emif_num;
for (emif_num = EMIF1; emif_num < EMIF_NUM_INSTANCES; emif_num++) {
/*
* Only disable ddr self-refresh
* if ddr self-refresh was enabled
*/
if (likely(LP_MODE_SELF_REFRESH == get_lp_mode(emif_num))) {
set_lp_mode(emif_num, LP_MODE_DISABLE);
emif[emif_num].ddr_refresh_disabled = true;
}
}
}
/*
* omap_emif_frequency_post_notify - Enable DDR self refresh of both EMIFs
*
* It restores the LP mode of the EMIFs back to LP_MODE_SELF_REFRESH if it
* was previously disabled by omap_emif_frequency_pre_notify()
*
*/
void omap_emif_frequency_post_notify(void)
{
int emif_num;
for (emif_num = EMIF1; emif_num < EMIF_NUM_INSTANCES; emif_num++) {
/*
* Only re-enable ddr self-refresh
* if ddr self-refresh was disabled
*/
if (likely(emif[emif_num].ddr_refresh_disabled)) {
set_lp_mode(emif_num, LP_MODE_SELF_REFRESH);
emif[emif_num].ddr_refresh_disabled = false;
}
}
}
/*
* omap_emif_setup_device_details - save the SDRAM device details passed
* from the board file
*/
int omap_emif_setup_device_details(const struct emif_device_details
*emif1_devices,
const struct emif_device_details
*emif2_devices)
{
if (emif1_devices)
BUG_ON(do_setup_device_details(EMIF1, emif1_devices));
/*
* If memory devices connected to both the EMIFs are identical
* (which is normally the case), then no need to calculate the
* registers again for EMIF1 and allocate the structure for registers
*/
if (emif2_devices && (emif1_devices != emif2_devices))
BUG_ON(do_setup_device_details(EMIF2, emif2_devices));
else if (emif2_devices) {
emif_devices[EMIF2] = emif_devices[EMIF1];
/* call for temperature related setup */
BUG_ON(do_setup_device_details(EMIF2, emif2_devices));
}
return 0;
}
static void __init setup_lowpower_regs(u32 emif_nr,
struct emif_device_details *emif_dev)
{
u32 temp;
void __iomem *base = emif[emif_nr].base;
const struct lpddr2_device_info *dev;
if (!emif_dev) {
pr_err("%s: no emif %d\n", __func__, emif_nr);
return;
}
/*
* All devices on this specific EMIF should have the same Selfrefresh
* timing, so use cs0
*/
dev = emif_dev->cs0_device;
if (!dev) {
pr_err("%s: no CS0 device in emif %d\n", __func__, emif_nr);
return;
}
if (dev->emif_ddr_selfrefresh_cycles >= 0) {
u32 num_cycles, ddr_sr_timer;
/* Enable self refresh if not already configured */
temp = __raw_readl(base + OMAP44XX_EMIF_PWR_MGMT_CTRL) &
OMAP44XX_REG_LP_MODE_MASK;
/*
* Configure the self refresh timing
* base value starts at 16 cycles mapped to 1( __fls(16) = 4)
*/
num_cycles = dev->emif_ddr_selfrefresh_cycles;
if (num_cycles >= 16)
ddr_sr_timer = __fls(num_cycles) - 3;
else
ddr_sr_timer = 0;
/* Program the idle delay */
temp = __raw_readl(base + OMAP44XX_EMIF_PWR_MGMT_CTRL_SHDW);
mask_n_set(temp, OMAP44XX_REG_SR_TIM_SHDW_SHIFT,
OMAP44XX_REG_SR_TIM_SHDW_MASK, ddr_sr_timer);
/*
* Some weird magic number to a field which should'nt impact..
* but seems to make this work..
*/
mask_n_set(temp, OMAP44XX_REG_CS_TIM_SHDW_SHIFT,
OMAP44XX_REG_CS_TIM_SHDW_MASK, 0xf);
__raw_writel(temp, base + OMAP44XX_EMIF_PWR_MGMT_CTRL_SHDW);
/* Enable Self Refresh */
temp = __raw_readl(base + OMAP44XX_EMIF_PWR_MGMT_CTRL);
mask_n_set(temp, OMAP44XX_REG_LP_MODE_SHIFT,
OMAP44XX_REG_LP_MODE_MASK, LP_MODE_SELF_REFRESH);
__raw_writel(temp, base + OMAP44XX_EMIF_PWR_MGMT_CTRL);
} else {
/* Disable Automatic power management if < 0 and not disabled */
temp = __raw_readl(base + OMAP44XX_EMIF_PWR_MGMT_CTRL) &
OMAP44XX_REG_LP_MODE_MASK;
temp = __raw_readl(base + OMAP44XX_EMIF_PWR_MGMT_CTRL_SHDW);
mask_n_set(temp, OMAP44XX_REG_SR_TIM_SHDW_SHIFT,
OMAP44XX_REG_SR_TIM_SHDW_MASK, 0x0);
__raw_writel(temp, base + OMAP44XX_EMIF_PWR_MGMT_CTRL_SHDW);
temp = __raw_readl(base + OMAP44XX_EMIF_PWR_MGMT_CTRL);
mask_n_set(temp, OMAP44XX_REG_LP_MODE_SHIFT,
OMAP44XX_REG_LP_MODE_MASK, LP_MODE_DISABLE);
__raw_writel(temp, base + OMAP44XX_EMIF_PWR_MGMT_CTRL);
}
}
/*
* omap_init_emif_timings - reprogram EMIF timing parameters
*
* Sets the CORE DPLL3 M2 divider to the same value that it's at
* currently. This has the effect of setting the EMIF DDR AC timing
* registers to the values currently defined by the kernel.
*/
static int __init omap_init_emif_timings(void)
{
struct clk *dpll_core_m2_clk;
int ret;
long rate;
/*
* Setup the initial temperatures sysfs nodes etc.
* Subsequent updates to temperature is done through interrupts
*/
init_temperature(EMIF1);
init_temperature(EMIF2);
/* FREQ_UPDATE sequence isn't supported on early vesion */
if (omap_rev() == OMAP4430_REV_ES1_0)
return -EINVAL;
dpll_core_m2_clk = clk_get(NULL, "dpll_core_m2_ck");
if (!dpll_core_m2_clk)
pr_err("Could not get LPDDR2 clock - dpll_core_m2_ck\n");
rate = clk_get_rate(dpll_core_m2_clk);
pr_info("Reprogramming LPDDR2 timings to %ld Hz\n", rate >> 1);
ret = clk_set_rate(dpll_core_m2_clk, rate);
if (ret)
pr_err("Unable to set LPDDR2 rate to %ld:\n", rate);
/* registers are setup correctly - now enable interrupts */
if (emif_devices[EMIF1]) {
ret = setup_emif_interrupts(EMIF1);
setup_lowpower_regs(EMIF1, emif_devices[EMIF1]);
}
if (!ret && emif_devices[EMIF2]) {
ret = setup_emif_interrupts(EMIF2);
setup_lowpower_regs(EMIF2, emif_devices[EMIF2]);
}
clk_put(dpll_core_m2_clk);
return ret;
}
late_initcall(omap_init_emif_timings);
|