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
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) STMicroelectronics 2020
*/
#include <linux/bitfield.h>
#include <linux/clk.h>
#include <linux/mfd/syscon.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/pinctrl/consumer.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/reset.h>
/* FMC2 Controller Registers */
#define FMC2_BCR1 0x0
#define FMC2_BTR1 0x4
#define FMC2_BCR(x) ((x) * 0x8 + FMC2_BCR1)
#define FMC2_BTR(x) ((x) * 0x8 + FMC2_BTR1)
#define FMC2_PCSCNTR 0x20
#define FMC2_CFGR 0x20
#define FMC2_SR 0x84
#define FMC2_BWTR1 0x104
#define FMC2_BWTR(x) ((x) * 0x8 + FMC2_BWTR1)
#define FMC2_SECCFGR 0x300
#define FMC2_CIDCFGR0 0x30c
#define FMC2_CIDCFGR(x) ((x) * 0x8 + FMC2_CIDCFGR0)
#define FMC2_SEMCR0 0x310
#define FMC2_SEMCR(x) ((x) * 0x8 + FMC2_SEMCR0)
/* Register: FMC2_BCR1 */
#define FMC2_BCR1_CCLKEN BIT(20)
#define FMC2_BCR1_FMC2EN BIT(31)
/* Register: FMC2_BCRx */
#define FMC2_BCR_MBKEN BIT(0)
#define FMC2_BCR_MUXEN BIT(1)
#define FMC2_BCR_MTYP GENMASK(3, 2)
#define FMC2_BCR_MWID GENMASK(5, 4)
#define FMC2_BCR_FACCEN BIT(6)
#define FMC2_BCR_BURSTEN BIT(8)
#define FMC2_BCR_WAITPOL BIT(9)
#define FMC2_BCR_WAITCFG BIT(11)
#define FMC2_BCR_WREN BIT(12)
#define FMC2_BCR_WAITEN BIT(13)
#define FMC2_BCR_EXTMOD BIT(14)
#define FMC2_BCR_ASYNCWAIT BIT(15)
#define FMC2_BCR_CPSIZE GENMASK(18, 16)
#define FMC2_BCR_CBURSTRW BIT(19)
#define FMC2_BCR_CSCOUNT GENMASK(21, 20)
#define FMC2_BCR_NBLSET GENMASK(23, 22)
/* Register: FMC2_BTRx/FMC2_BWTRx */
#define FMC2_BXTR_ADDSET GENMASK(3, 0)
#define FMC2_BXTR_ADDHLD GENMASK(7, 4)
#define FMC2_BXTR_DATAST GENMASK(15, 8)
#define FMC2_BXTR_BUSTURN GENMASK(19, 16)
#define FMC2_BTR_CLKDIV GENMASK(23, 20)
#define FMC2_BTR_DATLAT GENMASK(27, 24)
#define FMC2_BXTR_ACCMOD GENMASK(29, 28)
#define FMC2_BXTR_DATAHLD GENMASK(31, 30)
/* Register: FMC2_PCSCNTR */
#define FMC2_PCSCNTR_CSCOUNT GENMASK(15, 0)
#define FMC2_PCSCNTR_CNTBEN(x) BIT((x) + 16)
/* Register: FMC2_CFGR */
#define FMC2_CFGR_CLKDIV GENMASK(19, 16)
#define FMC2_CFGR_CCLKEN BIT(20)
#define FMC2_CFGR_FMC2EN BIT(31)
/* Register: FMC2_SR */
#define FMC2_SR_ISOST GENMASK(1, 0)
/* Register: FMC2_CIDCFGR */
#define FMC2_CIDCFGR_CFEN BIT(0)
#define FMC2_CIDCFGR_SEMEN BIT(1)
#define FMC2_CIDCFGR_SCID GENMASK(6, 4)
#define FMC2_CIDCFGR_SEMWLC1 BIT(17)
/* Register: FMC2_SEMCR */
#define FMC2_SEMCR_SEM_MUTEX BIT(0)
#define FMC2_SEMCR_SEMCID GENMASK(6, 4)
#define FMC2_MAX_EBI_CE 4
#define FMC2_MAX_BANKS 5
#define FMC2_MAX_RESOURCES 6
#define FMC2_CID1 1
#define FMC2_BCR_CPSIZE_0 0x0
#define FMC2_BCR_CPSIZE_128 0x1
#define FMC2_BCR_CPSIZE_256 0x2
#define FMC2_BCR_CPSIZE_512 0x3
#define FMC2_BCR_CPSIZE_1024 0x4
#define FMC2_BCR_MWID_8 0x0
#define FMC2_BCR_MWID_16 0x1
#define FMC2_BCR_MTYP_SRAM 0x0
#define FMC2_BCR_MTYP_PSRAM 0x1
#define FMC2_BCR_MTYP_NOR 0x2
#define FMC2_BCR_CSCOUNT_0 0x0
#define FMC2_BCR_CSCOUNT_1 0x1
#define FMC2_BCR_CSCOUNT_64 0x2
#define FMC2_BCR_CSCOUNT_256 0x3
#define FMC2_BXTR_EXTMOD_A 0x0
#define FMC2_BXTR_EXTMOD_B 0x1
#define FMC2_BXTR_EXTMOD_C 0x2
#define FMC2_BXTR_EXTMOD_D 0x3
#define FMC2_BCR_NBLSET_MAX 0x3
#define FMC2_BXTR_ADDSET_MAX 0xf
#define FMC2_BXTR_ADDHLD_MAX 0xf
#define FMC2_BXTR_DATAST_MAX 0xff
#define FMC2_BXTR_BUSTURN_MAX 0xf
#define FMC2_BXTR_DATAHLD_MAX 0x3
#define FMC2_BTR_CLKDIV_MAX 0xf
#define FMC2_BTR_DATLAT_MAX 0xf
#define FMC2_PCSCNTR_CSCOUNT_MAX 0xff
#define FMC2_CFGR_CLKDIV_MAX 0xf
enum stm32_fmc2_ebi_bank {
FMC2_EBI1 = 0,
FMC2_EBI2,
FMC2_EBI3,
FMC2_EBI4,
FMC2_NAND
};
enum stm32_fmc2_ebi_register_type {
FMC2_REG_BCR = 1,
FMC2_REG_BTR,
FMC2_REG_BWTR,
FMC2_REG_PCSCNTR,
FMC2_REG_CFGR
};
enum stm32_fmc2_ebi_transaction_type {
FMC2_ASYNC_MODE_1_SRAM = 0,
FMC2_ASYNC_MODE_1_PSRAM,
FMC2_ASYNC_MODE_A_SRAM,
FMC2_ASYNC_MODE_A_PSRAM,
FMC2_ASYNC_MODE_2_NOR,
FMC2_ASYNC_MODE_B_NOR,
FMC2_ASYNC_MODE_C_NOR,
FMC2_ASYNC_MODE_D_NOR,
FMC2_SYNC_READ_SYNC_WRITE_PSRAM,
FMC2_SYNC_READ_ASYNC_WRITE_PSRAM,
FMC2_SYNC_READ_SYNC_WRITE_NOR,
FMC2_SYNC_READ_ASYNC_WRITE_NOR
};
enum stm32_fmc2_ebi_buswidth {
FMC2_BUSWIDTH_8 = 8,
FMC2_BUSWIDTH_16 = 16
};
enum stm32_fmc2_ebi_cpsize {
FMC2_CPSIZE_0 = 0,
FMC2_CPSIZE_128 = 128,
FMC2_CPSIZE_256 = 256,
FMC2_CPSIZE_512 = 512,
FMC2_CPSIZE_1024 = 1024
};
enum stm32_fmc2_ebi_cscount {
FMC2_CSCOUNT_0 = 0,
FMC2_CSCOUNT_1 = 1,
FMC2_CSCOUNT_64 = 64,
FMC2_CSCOUNT_256 = 256
};
struct stm32_fmc2_ebi;
struct stm32_fmc2_ebi_data {
const struct stm32_fmc2_prop *child_props;
unsigned int nb_child_props;
u32 fmc2_enable_reg;
u32 fmc2_enable_bit;
int (*nwait_used_by_ctrls)(struct stm32_fmc2_ebi *ebi);
void (*set_setup)(struct stm32_fmc2_ebi *ebi);
int (*save_setup)(struct stm32_fmc2_ebi *ebi);
int (*check_rif)(struct stm32_fmc2_ebi *ebi, u32 resource);
void (*put_sems)(struct stm32_fmc2_ebi *ebi);
void (*get_sems)(struct stm32_fmc2_ebi *ebi);
};
struct stm32_fmc2_ebi {
struct device *dev;
struct clk *clk;
struct regmap *regmap;
const struct stm32_fmc2_ebi_data *data;
u8 bank_assigned;
u8 sem_taken;
bool access_granted;
u32 bcr[FMC2_MAX_EBI_CE];
u32 btr[FMC2_MAX_EBI_CE];
u32 bwtr[FMC2_MAX_EBI_CE];
u32 pcscntr;
u32 cfgr;
};
/*
* struct stm32_fmc2_prop - STM32 FMC2 EBI property
* @name: the device tree binding name of the property
* @bprop: indicate that it is a boolean property
* @mprop: indicate that it is a mandatory property
* @reg_type: the register that have to be modified
* @reg_mask: the bit that have to be modified in the selected register
* in case of it is a boolean property
* @reset_val: the default value that have to be set in case the property
* has not been defined in the device tree
* @check: this callback ckecks that the property is compliant with the
* transaction type selected
* @calculate: this callback is called to calculate for exemple a timing
* set in nanoseconds in the device tree in clock cycles or in
* clock period
* @set: this callback applies the values in the registers
*/
struct stm32_fmc2_prop {
const char *name;
bool bprop;
bool mprop;
int reg_type;
u32 reg_mask;
u32 reset_val;
int (*check)(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop, int cs);
u32 (*calculate)(struct stm32_fmc2_ebi *ebi, int cs, u32 setup);
int (*set)(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs, u32 setup);
};
static int stm32_fmc2_ebi_check_mux(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs)
{
u32 bcr;
int ret;
ret = regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
if (ret)
return ret;
if (bcr & FMC2_BCR_MTYP)
return 0;
return -EINVAL;
}
static int stm32_fmc2_ebi_check_waitcfg(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs)
{
u32 bcr, val = FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_NOR);
int ret;
ret = regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
if (ret)
return ret;
if ((bcr & FMC2_BCR_MTYP) == val && bcr & FMC2_BCR_BURSTEN)
return 0;
return -EINVAL;
}
static int stm32_fmc2_ebi_check_sync_trans(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs)
{
u32 bcr;
int ret;
ret = regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
if (ret)
return ret;
if (bcr & FMC2_BCR_BURSTEN)
return 0;
return -EINVAL;
}
static int stm32_fmc2_ebi_mp25_check_cclk(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs)
{
if (!ebi->access_granted)
return -EACCES;
return stm32_fmc2_ebi_check_sync_trans(ebi, prop, cs);
}
static int stm32_fmc2_ebi_mp25_check_clk_period(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs)
{
u32 cfgr;
int ret;
ret = regmap_read(ebi->regmap, FMC2_CFGR, &cfgr);
if (ret)
return ret;
if (cfgr & FMC2_CFGR_CCLKEN && !ebi->access_granted)
return -EACCES;
return stm32_fmc2_ebi_check_sync_trans(ebi, prop, cs);
}
static int stm32_fmc2_ebi_check_async_trans(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs)
{
u32 bcr;
int ret;
ret = regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
if (ret)
return ret;
if (!(bcr & FMC2_BCR_BURSTEN) || !(bcr & FMC2_BCR_CBURSTRW))
return 0;
return -EINVAL;
}
static int stm32_fmc2_ebi_check_cpsize(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs)
{
u32 bcr, val = FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_PSRAM);
int ret;
ret = regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
if (ret)
return ret;
if ((bcr & FMC2_BCR_MTYP) == val && bcr & FMC2_BCR_BURSTEN)
return 0;
return -EINVAL;
}
static int stm32_fmc2_ebi_check_address_hold(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs)
{
u32 bcr, bxtr, val = FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_D);
int ret;
ret = regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
if (ret)
return ret;
if (prop->reg_type == FMC2_REG_BWTR)
ret = regmap_read(ebi->regmap, FMC2_BWTR(cs), &bxtr);
else
ret = regmap_read(ebi->regmap, FMC2_BTR(cs), &bxtr);
if (ret)
return ret;
if ((!(bcr & FMC2_BCR_BURSTEN) || !(bcr & FMC2_BCR_CBURSTRW)) &&
((bxtr & FMC2_BXTR_ACCMOD) == val || bcr & FMC2_BCR_MUXEN))
return 0;
return -EINVAL;
}
static int stm32_fmc2_ebi_check_clk_period(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs)
{
u32 bcr, bcr1;
int ret;
ret = regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
if (ret)
return ret;
if (cs) {
ret = regmap_read(ebi->regmap, FMC2_BCR1, &bcr1);
if (ret)
return ret;
} else {
bcr1 = bcr;
}
if (bcr & FMC2_BCR_BURSTEN && (!cs || !(bcr1 & FMC2_BCR1_CCLKEN)))
return 0;
return -EINVAL;
}
static int stm32_fmc2_ebi_check_cclk(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs)
{
if (cs)
return -EINVAL;
return stm32_fmc2_ebi_check_sync_trans(ebi, prop, cs);
}
static u32 stm32_fmc2_ebi_ns_to_clock_cycles(struct stm32_fmc2_ebi *ebi,
int cs, u32 setup)
{
unsigned long hclk = clk_get_rate(ebi->clk);
unsigned long hclkp = NSEC_PER_SEC / (hclk / 1000);
return DIV_ROUND_UP(setup * 1000, hclkp);
}
static u32 stm32_fmc2_ebi_ns_to_clk_period(struct stm32_fmc2_ebi *ebi,
int cs, u32 setup)
{
u32 nb_clk_cycles = stm32_fmc2_ebi_ns_to_clock_cycles(ebi, cs, setup);
u32 bcr, btr, clk_period;
int ret;
ret = regmap_read(ebi->regmap, FMC2_BCR1, &bcr);
if (ret)
return ret;
if (bcr & FMC2_BCR1_CCLKEN || !cs)
ret = regmap_read(ebi->regmap, FMC2_BTR1, &btr);
else
ret = regmap_read(ebi->regmap, FMC2_BTR(cs), &btr);
if (ret)
return ret;
clk_period = FIELD_GET(FMC2_BTR_CLKDIV, btr) + 1;
return DIV_ROUND_UP(nb_clk_cycles, clk_period);
}
static u32 stm32_fmc2_ebi_mp25_ns_to_clk_period(struct stm32_fmc2_ebi *ebi,
int cs, u32 setup)
{
u32 nb_clk_cycles = stm32_fmc2_ebi_ns_to_clock_cycles(ebi, cs, setup);
u32 cfgr, btr, clk_period;
int ret;
ret = regmap_read(ebi->regmap, FMC2_CFGR, &cfgr);
if (ret)
return ret;
if (cfgr & FMC2_CFGR_CCLKEN) {
clk_period = FIELD_GET(FMC2_CFGR_CLKDIV, cfgr) + 1;
} else {
ret = regmap_read(ebi->regmap, FMC2_BTR(cs), &btr);
if (ret)
return ret;
clk_period = FIELD_GET(FMC2_BTR_CLKDIV, btr) + 1;
}
return DIV_ROUND_UP(nb_clk_cycles, clk_period);
}
static int stm32_fmc2_ebi_get_reg(int reg_type, int cs, u32 *reg)
{
switch (reg_type) {
case FMC2_REG_BCR:
*reg = FMC2_BCR(cs);
break;
case FMC2_REG_BTR:
*reg = FMC2_BTR(cs);
break;
case FMC2_REG_BWTR:
*reg = FMC2_BWTR(cs);
break;
case FMC2_REG_PCSCNTR:
*reg = FMC2_PCSCNTR;
break;
case FMC2_REG_CFGR:
*reg = FMC2_CFGR;
break;
default:
return -EINVAL;
}
return 0;
}
static int stm32_fmc2_ebi_set_bit_field(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs, u32 setup)
{
u32 reg;
int ret;
ret = stm32_fmc2_ebi_get_reg(prop->reg_type, cs, ®);
if (ret)
return ret;
regmap_update_bits(ebi->regmap, reg, prop->reg_mask,
setup ? prop->reg_mask : 0);
return 0;
}
static int stm32_fmc2_ebi_set_trans_type(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs, u32 setup)
{
u32 bcr_mask, bcr = FMC2_BCR_WREN;
u32 btr_mask, btr = 0;
u32 bwtr_mask, bwtr = 0;
bwtr_mask = FMC2_BXTR_ACCMOD;
btr_mask = FMC2_BXTR_ACCMOD;
bcr_mask = FMC2_BCR_MUXEN | FMC2_BCR_MTYP | FMC2_BCR_FACCEN |
FMC2_BCR_WREN | FMC2_BCR_WAITEN | FMC2_BCR_BURSTEN |
FMC2_BCR_EXTMOD | FMC2_BCR_CBURSTRW;
switch (setup) {
case FMC2_ASYNC_MODE_1_SRAM:
bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_SRAM);
/*
* MUXEN = 0, MTYP = 0, FACCEN = 0, BURSTEN = 0, WAITEN = 0,
* WREN = 1, EXTMOD = 0, CBURSTRW = 0, ACCMOD = 0
*/
break;
case FMC2_ASYNC_MODE_1_PSRAM:
/*
* MUXEN = 0, MTYP = 1, FACCEN = 0, BURSTEN = 0, WAITEN = 0,
* WREN = 1, EXTMOD = 0, CBURSTRW = 0, ACCMOD = 0
*/
bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_PSRAM);
break;
case FMC2_ASYNC_MODE_A_SRAM:
/*
* MUXEN = 0, MTYP = 0, FACCEN = 0, BURSTEN = 0, WAITEN = 0,
* WREN = 1, EXTMOD = 1, CBURSTRW = 0, ACCMOD = 0
*/
bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_SRAM);
bcr |= FMC2_BCR_EXTMOD;
btr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_A);
bwtr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_A);
break;
case FMC2_ASYNC_MODE_A_PSRAM:
/*
* MUXEN = 0, MTYP = 1, FACCEN = 0, BURSTEN = 0, WAITEN = 0,
* WREN = 1, EXTMOD = 1, CBURSTRW = 0, ACCMOD = 0
*/
bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_PSRAM);
bcr |= FMC2_BCR_EXTMOD;
btr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_A);
bwtr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_A);
break;
case FMC2_ASYNC_MODE_2_NOR:
/*
* MUXEN = 0, MTYP = 2, FACCEN = 1, BURSTEN = 0, WAITEN = 0,
* WREN = 1, EXTMOD = 0, CBURSTRW = 0, ACCMOD = 0
*/
bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_NOR);
bcr |= FMC2_BCR_FACCEN;
break;
case FMC2_ASYNC_MODE_B_NOR:
/*
* MUXEN = 0, MTYP = 2, FACCEN = 1, BURSTEN = 0, WAITEN = 0,
* WREN = 1, EXTMOD = 1, CBURSTRW = 0, ACCMOD = 1
*/
bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_NOR);
bcr |= FMC2_BCR_FACCEN | FMC2_BCR_EXTMOD;
btr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_B);
bwtr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_B);
break;
case FMC2_ASYNC_MODE_C_NOR:
/*
* MUXEN = 0, MTYP = 2, FACCEN = 1, BURSTEN = 0, WAITEN = 0,
* WREN = 1, EXTMOD = 1, CBURSTRW = 0, ACCMOD = 2
*/
bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_NOR);
bcr |= FMC2_BCR_FACCEN | FMC2_BCR_EXTMOD;
btr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_C);
bwtr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_C);
break;
case FMC2_ASYNC_MODE_D_NOR:
/*
* MUXEN = 0, MTYP = 2, FACCEN = 1, BURSTEN = 0, WAITEN = 0,
* WREN = 1, EXTMOD = 1, CBURSTRW = 0, ACCMOD = 3
*/
bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_NOR);
bcr |= FMC2_BCR_FACCEN | FMC2_BCR_EXTMOD;
btr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_D);
bwtr |= FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_D);
break;
case FMC2_SYNC_READ_SYNC_WRITE_PSRAM:
/*
* MUXEN = 0, MTYP = 1, FACCEN = 0, BURSTEN = 1, WAITEN = 0,
* WREN = 1, EXTMOD = 0, CBURSTRW = 1, ACCMOD = 0
*/
bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_PSRAM);
bcr |= FMC2_BCR_BURSTEN | FMC2_BCR_CBURSTRW;
break;
case FMC2_SYNC_READ_ASYNC_WRITE_PSRAM:
/*
* MUXEN = 0, MTYP = 1, FACCEN = 0, BURSTEN = 1, WAITEN = 0,
* WREN = 1, EXTMOD = 0, CBURSTRW = 0, ACCMOD = 0
*/
bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_PSRAM);
bcr |= FMC2_BCR_BURSTEN;
break;
case FMC2_SYNC_READ_SYNC_WRITE_NOR:
/*
* MUXEN = 0, MTYP = 2, FACCEN = 1, BURSTEN = 1, WAITEN = 0,
* WREN = 1, EXTMOD = 0, CBURSTRW = 1, ACCMOD = 0
*/
bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_NOR);
bcr |= FMC2_BCR_FACCEN | FMC2_BCR_BURSTEN | FMC2_BCR_CBURSTRW;
break;
case FMC2_SYNC_READ_ASYNC_WRITE_NOR:
/*
* MUXEN = 0, MTYP = 2, FACCEN = 1, BURSTEN = 1, WAITEN = 0,
* WREN = 1, EXTMOD = 0, CBURSTRW = 0, ACCMOD = 0
*/
bcr |= FIELD_PREP(FMC2_BCR_MTYP, FMC2_BCR_MTYP_NOR);
bcr |= FMC2_BCR_FACCEN | FMC2_BCR_BURSTEN;
break;
default:
/* Type of transaction not supported */
return -EINVAL;
}
if (bcr & FMC2_BCR_EXTMOD)
regmap_update_bits(ebi->regmap, FMC2_BWTR(cs),
bwtr_mask, bwtr);
regmap_update_bits(ebi->regmap, FMC2_BTR(cs), btr_mask, btr);
regmap_update_bits(ebi->regmap, FMC2_BCR(cs), bcr_mask, bcr);
return 0;
}
static int stm32_fmc2_ebi_set_buswidth(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs, u32 setup)
{
u32 val;
switch (setup) {
case FMC2_BUSWIDTH_8:
val = FIELD_PREP(FMC2_BCR_MWID, FMC2_BCR_MWID_8);
break;
case FMC2_BUSWIDTH_16:
val = FIELD_PREP(FMC2_BCR_MWID, FMC2_BCR_MWID_16);
break;
default:
/* Buswidth not supported */
return -EINVAL;
}
regmap_update_bits(ebi->regmap, FMC2_BCR(cs), FMC2_BCR_MWID, val);
return 0;
}
static int stm32_fmc2_ebi_set_cpsize(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs, u32 setup)
{
u32 val;
switch (setup) {
case FMC2_CPSIZE_0:
val = FIELD_PREP(FMC2_BCR_CPSIZE, FMC2_BCR_CPSIZE_0);
break;
case FMC2_CPSIZE_128:
val = FIELD_PREP(FMC2_BCR_CPSIZE, FMC2_BCR_CPSIZE_128);
break;
case FMC2_CPSIZE_256:
val = FIELD_PREP(FMC2_BCR_CPSIZE, FMC2_BCR_CPSIZE_256);
break;
case FMC2_CPSIZE_512:
val = FIELD_PREP(FMC2_BCR_CPSIZE, FMC2_BCR_CPSIZE_512);
break;
case FMC2_CPSIZE_1024:
val = FIELD_PREP(FMC2_BCR_CPSIZE, FMC2_BCR_CPSIZE_1024);
break;
default:
/* Cpsize not supported */
return -EINVAL;
}
regmap_update_bits(ebi->regmap, FMC2_BCR(cs), FMC2_BCR_CPSIZE, val);
return 0;
}
static int stm32_fmc2_ebi_set_bl_setup(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs, u32 setup)
{
u32 val;
val = min_t(u32, setup, FMC2_BCR_NBLSET_MAX);
val = FIELD_PREP(FMC2_BCR_NBLSET, val);
regmap_update_bits(ebi->regmap, FMC2_BCR(cs), FMC2_BCR_NBLSET, val);
return 0;
}
static int stm32_fmc2_ebi_set_address_setup(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs, u32 setup)
{
u32 bcr, bxtr, reg;
u32 val = FIELD_PREP(FMC2_BXTR_ACCMOD, FMC2_BXTR_EXTMOD_D);
int ret;
ret = stm32_fmc2_ebi_get_reg(prop->reg_type, cs, ®);
if (ret)
return ret;
ret = regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
if (ret)
return ret;
if (prop->reg_type == FMC2_REG_BWTR)
ret = regmap_read(ebi->regmap, FMC2_BWTR(cs), &bxtr);
else
ret = regmap_read(ebi->regmap, FMC2_BTR(cs), &bxtr);
if (ret)
return ret;
if ((bxtr & FMC2_BXTR_ACCMOD) == val || bcr & FMC2_BCR_MUXEN)
val = clamp_val(setup, 1, FMC2_BXTR_ADDSET_MAX);
else
val = min_t(u32, setup, FMC2_BXTR_ADDSET_MAX);
val = FIELD_PREP(FMC2_BXTR_ADDSET, val);
regmap_update_bits(ebi->regmap, reg, FMC2_BXTR_ADDSET, val);
return 0;
}
static int stm32_fmc2_ebi_set_address_hold(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs, u32 setup)
{
u32 val, reg;
int ret;
ret = stm32_fmc2_ebi_get_reg(prop->reg_type, cs, ®);
if (ret)
return ret;
val = clamp_val(setup, 1, FMC2_BXTR_ADDHLD_MAX);
val = FIELD_PREP(FMC2_BXTR_ADDHLD, val);
regmap_update_bits(ebi->regmap, reg, FMC2_BXTR_ADDHLD, val);
return 0;
}
static int stm32_fmc2_ebi_set_data_setup(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs, u32 setup)
{
u32 val, reg;
int ret;
ret = stm32_fmc2_ebi_get_reg(prop->reg_type, cs, ®);
if (ret)
return ret;
val = clamp_val(setup, 1, FMC2_BXTR_DATAST_MAX);
val = FIELD_PREP(FMC2_BXTR_DATAST, val);
regmap_update_bits(ebi->regmap, reg, FMC2_BXTR_DATAST, val);
return 0;
}
static int stm32_fmc2_ebi_set_bus_turnaround(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs, u32 setup)
{
u32 val, reg;
int ret;
ret = stm32_fmc2_ebi_get_reg(prop->reg_type, cs, ®);
if (ret)
return ret;
val = setup ? min_t(u32, setup - 1, FMC2_BXTR_BUSTURN_MAX) : 0;
val = FIELD_PREP(FMC2_BXTR_BUSTURN, val);
regmap_update_bits(ebi->regmap, reg, FMC2_BXTR_BUSTURN, val);
return 0;
}
static int stm32_fmc2_ebi_set_data_hold(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs, u32 setup)
{
u32 val, reg;
int ret;
ret = stm32_fmc2_ebi_get_reg(prop->reg_type, cs, ®);
if (ret)
return ret;
if (prop->reg_type == FMC2_REG_BWTR)
val = setup ? min_t(u32, setup - 1, FMC2_BXTR_DATAHLD_MAX) : 0;
else
val = min_t(u32, setup, FMC2_BXTR_DATAHLD_MAX);
val = FIELD_PREP(FMC2_BXTR_DATAHLD, val);
regmap_update_bits(ebi->regmap, reg, FMC2_BXTR_DATAHLD, val);
return 0;
}
static int stm32_fmc2_ebi_set_clk_period(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs, u32 setup)
{
u32 val;
val = setup ? clamp_val(setup - 1, 1, FMC2_BTR_CLKDIV_MAX) : 1;
val = FIELD_PREP(FMC2_BTR_CLKDIV, val);
regmap_update_bits(ebi->regmap, FMC2_BTR(cs), FMC2_BTR_CLKDIV, val);
return 0;
}
static int stm32_fmc2_ebi_mp25_set_clk_period(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs, u32 setup)
{
u32 val, cfgr;
int ret;
ret = regmap_read(ebi->regmap, FMC2_CFGR, &cfgr);
if (ret)
return ret;
if (cfgr & FMC2_CFGR_CCLKEN) {
val = setup ? clamp_val(setup - 1, 1, FMC2_CFGR_CLKDIV_MAX) : 1;
val = FIELD_PREP(FMC2_CFGR_CLKDIV, val);
regmap_update_bits(ebi->regmap, FMC2_CFGR, FMC2_CFGR_CLKDIV, val);
} else {
val = setup ? clamp_val(setup - 1, 1, FMC2_BTR_CLKDIV_MAX) : 1;
val = FIELD_PREP(FMC2_BTR_CLKDIV, val);
regmap_update_bits(ebi->regmap, FMC2_BTR(cs), FMC2_BTR_CLKDIV, val);
}
return 0;
}
static int stm32_fmc2_ebi_set_data_latency(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs, u32 setup)
{
u32 val;
val = setup > 1 ? min_t(u32, setup - 2, FMC2_BTR_DATLAT_MAX) : 0;
val = FIELD_PREP(FMC2_BTR_DATLAT, val);
regmap_update_bits(ebi->regmap, FMC2_BTR(cs), FMC2_BTR_DATLAT, val);
return 0;
}
static int stm32_fmc2_ebi_set_max_low_pulse(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs, u32 setup)
{
u32 old_val, new_val, pcscntr;
int ret;
if (setup < 1)
return 0;
ret = regmap_read(ebi->regmap, FMC2_PCSCNTR, &pcscntr);
if (ret)
return ret;
/* Enable counter for the bank */
regmap_update_bits(ebi->regmap, FMC2_PCSCNTR,
FMC2_PCSCNTR_CNTBEN(cs),
FMC2_PCSCNTR_CNTBEN(cs));
new_val = min_t(u32, setup - 1, FMC2_PCSCNTR_CSCOUNT_MAX);
old_val = FIELD_GET(FMC2_PCSCNTR_CSCOUNT, pcscntr);
if (old_val && new_val > old_val)
/* Keep current counter value */
return 0;
new_val = FIELD_PREP(FMC2_PCSCNTR_CSCOUNT, new_val);
regmap_update_bits(ebi->regmap, FMC2_PCSCNTR,
FMC2_PCSCNTR_CSCOUNT, new_val);
return 0;
}
static int stm32_fmc2_ebi_mp25_set_max_low_pulse(struct stm32_fmc2_ebi *ebi,
const struct stm32_fmc2_prop *prop,
int cs, u32 setup)
{
u32 val;
if (setup == FMC2_CSCOUNT_0)
val = FIELD_PREP(FMC2_BCR_CSCOUNT, FMC2_BCR_CSCOUNT_0);
else if (setup == FMC2_CSCOUNT_1)
val = FIELD_PREP(FMC2_BCR_CSCOUNT, FMC2_BCR_CSCOUNT_1);
else if (setup <= FMC2_CSCOUNT_64)
val = FIELD_PREP(FMC2_BCR_CSCOUNT, FMC2_BCR_CSCOUNT_64);
else
val = FIELD_PREP(FMC2_BCR_CSCOUNT, FMC2_BCR_CSCOUNT_256);
regmap_update_bits(ebi->regmap, FMC2_BCR(cs),
FMC2_BCR_CSCOUNT, val);
return 0;
}
static const struct stm32_fmc2_prop stm32_fmc2_child_props[] = {
/* st,fmc2-ebi-cs-trans-type must be the first property */
{
.name = "st,fmc2-ebi-cs-transaction-type",
.mprop = true,
.set = stm32_fmc2_ebi_set_trans_type,
},
{
.name = "st,fmc2-ebi-cs-cclk-enable",
.bprop = true,
.reg_type = FMC2_REG_BCR,
.reg_mask = FMC2_BCR1_CCLKEN,
.check = stm32_fmc2_ebi_check_cclk,
.set = stm32_fmc2_ebi_set_bit_field,
},
{
.name = "st,fmc2-ebi-cs-mux-enable",
.bprop = true,
.reg_type = FMC2_REG_BCR,
.reg_mask = FMC2_BCR_MUXEN,
.check = stm32_fmc2_ebi_check_mux,
.set = stm32_fmc2_ebi_set_bit_field,
},
{
.name = "st,fmc2-ebi-cs-buswidth",
.reset_val = FMC2_BUSWIDTH_16,
.set = stm32_fmc2_ebi_set_buswidth,
},
{
.name = "st,fmc2-ebi-cs-waitpol-high",
.bprop = true,
.reg_type = FMC2_REG_BCR,
.reg_mask = FMC2_BCR_WAITPOL,
.set = stm32_fmc2_ebi_set_bit_field,
},
{
.name = "st,fmc2-ebi-cs-waitcfg-enable",
.bprop = true,
.reg_type = FMC2_REG_BCR,
.reg_mask = FMC2_BCR_WAITCFG,
.check = stm32_fmc2_ebi_check_waitcfg,
.set = stm32_fmc2_ebi_set_bit_field,
},
{
.name = "st,fmc2-ebi-cs-wait-enable",
.bprop = true,
.reg_type = FMC2_REG_BCR,
.reg_mask = FMC2_BCR_WAITEN,
.check = stm32_fmc2_ebi_check_sync_trans,
.set = stm32_fmc2_ebi_set_bit_field,
},
{
.name = "st,fmc2-ebi-cs-asyncwait-enable",
.bprop = true,
.reg_type = FMC2_REG_BCR,
.reg_mask = FMC2_BCR_ASYNCWAIT,
.check = stm32_fmc2_ebi_check_async_trans,
.set = stm32_fmc2_ebi_set_bit_field,
},
{
.name = "st,fmc2-ebi-cs-cpsize",
.check = stm32_fmc2_ebi_check_cpsize,
.set = stm32_fmc2_ebi_set_cpsize,
},
{
.name = "st,fmc2-ebi-cs-byte-lane-setup-ns",
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_bl_setup,
},
{
.name = "st,fmc2-ebi-cs-address-setup-ns",
.reg_type = FMC2_REG_BTR,
.reset_val = FMC2_BXTR_ADDSET_MAX,
.check = stm32_fmc2_ebi_check_async_trans,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_address_setup,
},
{
.name = "st,fmc2-ebi-cs-address-hold-ns",
.reg_type = FMC2_REG_BTR,
.reset_val = FMC2_BXTR_ADDHLD_MAX,
.check = stm32_fmc2_ebi_check_address_hold,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_address_hold,
},
{
.name = "st,fmc2-ebi-cs-data-setup-ns",
.reg_type = FMC2_REG_BTR,
.reset_val = FMC2_BXTR_DATAST_MAX,
.check = stm32_fmc2_ebi_check_async_trans,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_data_setup,
},
{
.name = "st,fmc2-ebi-cs-bus-turnaround-ns",
.reg_type = FMC2_REG_BTR,
.reset_val = FMC2_BXTR_BUSTURN_MAX + 1,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_bus_turnaround,
},
{
.name = "st,fmc2-ebi-cs-data-hold-ns",
.reg_type = FMC2_REG_BTR,
.check = stm32_fmc2_ebi_check_async_trans,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_data_hold,
},
{
.name = "st,fmc2-ebi-cs-clk-period-ns",
.reset_val = FMC2_BTR_CLKDIV_MAX + 1,
.check = stm32_fmc2_ebi_check_clk_period,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_clk_period,
},
{
.name = "st,fmc2-ebi-cs-data-latency-ns",
.check = stm32_fmc2_ebi_check_sync_trans,
.calculate = stm32_fmc2_ebi_ns_to_clk_period,
.set = stm32_fmc2_ebi_set_data_latency,
},
{
.name = "st,fmc2-ebi-cs-write-address-setup-ns",
.reg_type = FMC2_REG_BWTR,
.reset_val = FMC2_BXTR_ADDSET_MAX,
.check = stm32_fmc2_ebi_check_async_trans,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_address_setup,
},
{
.name = "st,fmc2-ebi-cs-write-address-hold-ns",
.reg_type = FMC2_REG_BWTR,
.reset_val = FMC2_BXTR_ADDHLD_MAX,
.check = stm32_fmc2_ebi_check_address_hold,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_address_hold,
},
{
.name = "st,fmc2-ebi-cs-write-data-setup-ns",
.reg_type = FMC2_REG_BWTR,
.reset_val = FMC2_BXTR_DATAST_MAX,
.check = stm32_fmc2_ebi_check_async_trans,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_data_setup,
},
{
.name = "st,fmc2-ebi-cs-write-bus-turnaround-ns",
.reg_type = FMC2_REG_BWTR,
.reset_val = FMC2_BXTR_BUSTURN_MAX + 1,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_bus_turnaround,
},
{
.name = "st,fmc2-ebi-cs-write-data-hold-ns",
.reg_type = FMC2_REG_BWTR,
.check = stm32_fmc2_ebi_check_async_trans,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_data_hold,
},
{
.name = "st,fmc2-ebi-cs-max-low-pulse-ns",
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_max_low_pulse,
},
};
static const struct stm32_fmc2_prop stm32_fmc2_mp25_child_props[] = {
/* st,fmc2-ebi-cs-trans-type must be the first property */
{
.name = "st,fmc2-ebi-cs-transaction-type",
.mprop = true,
.set = stm32_fmc2_ebi_set_trans_type,
},
{
.name = "st,fmc2-ebi-cs-cclk-enable",
.bprop = true,
.reg_type = FMC2_REG_CFGR,
.reg_mask = FMC2_CFGR_CCLKEN,
.check = stm32_fmc2_ebi_mp25_check_cclk,
.set = stm32_fmc2_ebi_set_bit_field,
},
{
.name = "st,fmc2-ebi-cs-mux-enable",
.bprop = true,
.reg_type = FMC2_REG_BCR,
.reg_mask = FMC2_BCR_MUXEN,
.check = stm32_fmc2_ebi_check_mux,
.set = stm32_fmc2_ebi_set_bit_field,
},
{
.name = "st,fmc2-ebi-cs-buswidth",
.reset_val = FMC2_BUSWIDTH_16,
.set = stm32_fmc2_ebi_set_buswidth,
},
{
.name = "st,fmc2-ebi-cs-waitpol-high",
.bprop = true,
.reg_type = FMC2_REG_BCR,
.reg_mask = FMC2_BCR_WAITPOL,
.set = stm32_fmc2_ebi_set_bit_field,
},
{
.name = "st,fmc2-ebi-cs-waitcfg-enable",
.bprop = true,
.reg_type = FMC2_REG_BCR,
.reg_mask = FMC2_BCR_WAITCFG,
.check = stm32_fmc2_ebi_check_waitcfg,
.set = stm32_fmc2_ebi_set_bit_field,
},
{
.name = "st,fmc2-ebi-cs-wait-enable",
.bprop = true,
.reg_type = FMC2_REG_BCR,
.reg_mask = FMC2_BCR_WAITEN,
.check = stm32_fmc2_ebi_check_sync_trans,
.set = stm32_fmc2_ebi_set_bit_field,
},
{
.name = "st,fmc2-ebi-cs-asyncwait-enable",
.bprop = true,
.reg_type = FMC2_REG_BCR,
.reg_mask = FMC2_BCR_ASYNCWAIT,
.check = stm32_fmc2_ebi_check_async_trans,
.set = stm32_fmc2_ebi_set_bit_field,
},
{
.name = "st,fmc2-ebi-cs-cpsize",
.check = stm32_fmc2_ebi_check_cpsize,
.set = stm32_fmc2_ebi_set_cpsize,
},
{
.name = "st,fmc2-ebi-cs-byte-lane-setup-ns",
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_bl_setup,
},
{
.name = "st,fmc2-ebi-cs-address-setup-ns",
.reg_type = FMC2_REG_BTR,
.reset_val = FMC2_BXTR_ADDSET_MAX,
.check = stm32_fmc2_ebi_check_async_trans,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_address_setup,
},
{
.name = "st,fmc2-ebi-cs-address-hold-ns",
.reg_type = FMC2_REG_BTR,
.reset_val = FMC2_BXTR_ADDHLD_MAX,
.check = stm32_fmc2_ebi_check_address_hold,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_address_hold,
},
{
.name = "st,fmc2-ebi-cs-data-setup-ns",
.reg_type = FMC2_REG_BTR,
.reset_val = FMC2_BXTR_DATAST_MAX,
.check = stm32_fmc2_ebi_check_async_trans,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_data_setup,
},
{
.name = "st,fmc2-ebi-cs-bus-turnaround-ns",
.reg_type = FMC2_REG_BTR,
.reset_val = FMC2_BXTR_BUSTURN_MAX + 1,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_bus_turnaround,
},
{
.name = "st,fmc2-ebi-cs-data-hold-ns",
.reg_type = FMC2_REG_BTR,
.check = stm32_fmc2_ebi_check_async_trans,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_data_hold,
},
{
.name = "st,fmc2-ebi-cs-clk-period-ns",
.reset_val = FMC2_CFGR_CLKDIV_MAX + 1,
.check = stm32_fmc2_ebi_mp25_check_clk_period,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_mp25_set_clk_period,
},
{
.name = "st,fmc2-ebi-cs-data-latency-ns",
.check = stm32_fmc2_ebi_check_sync_trans,
.calculate = stm32_fmc2_ebi_mp25_ns_to_clk_period,
.set = stm32_fmc2_ebi_set_data_latency,
},
{
.name = "st,fmc2-ebi-cs-write-address-setup-ns",
.reg_type = FMC2_REG_BWTR,
.reset_val = FMC2_BXTR_ADDSET_MAX,
.check = stm32_fmc2_ebi_check_async_trans,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_address_setup,
},
{
.name = "st,fmc2-ebi-cs-write-address-hold-ns",
.reg_type = FMC2_REG_BWTR,
.reset_val = FMC2_BXTR_ADDHLD_MAX,
.check = stm32_fmc2_ebi_check_address_hold,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_address_hold,
},
{
.name = "st,fmc2-ebi-cs-write-data-setup-ns",
.reg_type = FMC2_REG_BWTR,
.reset_val = FMC2_BXTR_DATAST_MAX,
.check = stm32_fmc2_ebi_check_async_trans,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_data_setup,
},
{
.name = "st,fmc2-ebi-cs-write-bus-turnaround-ns",
.reg_type = FMC2_REG_BWTR,
.reset_val = FMC2_BXTR_BUSTURN_MAX + 1,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_bus_turnaround,
},
{
.name = "st,fmc2-ebi-cs-write-data-hold-ns",
.reg_type = FMC2_REG_BWTR,
.check = stm32_fmc2_ebi_check_async_trans,
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_set_data_hold,
},
{
.name = "st,fmc2-ebi-cs-max-low-pulse-ns",
.calculate = stm32_fmc2_ebi_ns_to_clock_cycles,
.set = stm32_fmc2_ebi_mp25_set_max_low_pulse,
},
};
static int stm32_fmc2_ebi_mp25_check_rif(struct stm32_fmc2_ebi *ebi, u32 resource)
{
u32 seccfgr, cidcfgr, semcr;
int cid, ret;
if (resource >= FMC2_MAX_RESOURCES)
return -EINVAL;
ret = regmap_read(ebi->regmap, FMC2_SECCFGR, &seccfgr);
if (ret)
return ret;
if (seccfgr & BIT(resource)) {
if (resource)
dev_err(ebi->dev, "resource %d is configured as secure\n",
resource);
return -EACCES;
}
ret = regmap_read(ebi->regmap, FMC2_CIDCFGR(resource), &cidcfgr);
if (ret)
return ret;
if (!(cidcfgr & FMC2_CIDCFGR_CFEN))
/* CID filtering is turned off: access granted */
return 0;
if (!(cidcfgr & FMC2_CIDCFGR_SEMEN)) {
/* Static CID mode */
cid = FIELD_GET(FMC2_CIDCFGR_SCID, cidcfgr);
if (cid != FMC2_CID1) {
if (resource)
dev_err(ebi->dev, "static CID%d set for resource %d\n",
cid, resource);
return -EACCES;
}
return 0;
}
/* Pass-list with semaphore mode */
if (!(cidcfgr & FMC2_CIDCFGR_SEMWLC1)) {
if (resource)
dev_err(ebi->dev, "CID1 is block-listed for resource %d\n",
resource);
return -EACCES;
}
ret = regmap_read(ebi->regmap, FMC2_SEMCR(resource), &semcr);
if (ret)
return ret;
if (!(semcr & FMC2_SEMCR_SEM_MUTEX)) {
regmap_update_bits(ebi->regmap, FMC2_SEMCR(resource),
FMC2_SEMCR_SEM_MUTEX, FMC2_SEMCR_SEM_MUTEX);
ret = regmap_read(ebi->regmap, FMC2_SEMCR(resource), &semcr);
if (ret)
return ret;
}
cid = FIELD_GET(FMC2_SEMCR_SEMCID, semcr);
if (cid != FMC2_CID1) {
if (resource)
dev_err(ebi->dev, "resource %d is already used by CID%d\n",
resource, cid);
return -EACCES;
}
ebi->sem_taken |= BIT(resource);
return 0;
}
static void stm32_fmc2_ebi_mp25_put_sems(struct stm32_fmc2_ebi *ebi)
{
unsigned int resource;
for (resource = 0; resource < FMC2_MAX_RESOURCES; resource++) {
if (!(ebi->sem_taken & BIT(resource)))
continue;
regmap_update_bits(ebi->regmap, FMC2_SEMCR(resource),
FMC2_SEMCR_SEM_MUTEX, 0);
}
}
static void stm32_fmc2_ebi_mp25_get_sems(struct stm32_fmc2_ebi *ebi)
{
unsigned int resource;
for (resource = 0; resource < FMC2_MAX_RESOURCES; resource++) {
if (!(ebi->sem_taken & BIT(resource)))
continue;
regmap_update_bits(ebi->regmap, FMC2_SEMCR(resource),
FMC2_SEMCR_SEM_MUTEX, FMC2_SEMCR_SEM_MUTEX);
}
}
static int stm32_fmc2_ebi_parse_prop(struct stm32_fmc2_ebi *ebi,
struct device_node *dev_node,
const struct stm32_fmc2_prop *prop,
int cs)
{
struct device *dev = ebi->dev;
u32 setup = 0;
if (!prop->set) {
dev_err(dev, "property %s is not well defined\n", prop->name);
return -EINVAL;
}
if (prop->check && prop->check(ebi, prop, cs))
/* Skeep this property */
return 0;
if (prop->bprop) {
bool bprop;
bprop = of_property_read_bool(dev_node, prop->name);
if (prop->mprop && !bprop) {
dev_err(dev, "mandatory property %s not defined in the device tree\n",
prop->name);
return -EINVAL;
}
if (bprop)
setup = 1;
} else {
u32 val;
int ret;
ret = of_property_read_u32(dev_node, prop->name, &val);
if (prop->mprop && ret) {
dev_err(dev, "mandatory property %s not defined in the device tree\n",
prop->name);
return ret;
}
if (ret)
setup = prop->reset_val;
else if (prop->calculate)
setup = prop->calculate(ebi, cs, val);
else
setup = val;
}
return prop->set(ebi, prop, cs, setup);
}
static void stm32_fmc2_ebi_enable_bank(struct stm32_fmc2_ebi *ebi, int cs)
{
regmap_update_bits(ebi->regmap, FMC2_BCR(cs),
FMC2_BCR_MBKEN, FMC2_BCR_MBKEN);
}
static void stm32_fmc2_ebi_disable_bank(struct stm32_fmc2_ebi *ebi, int cs)
{
regmap_update_bits(ebi->regmap, FMC2_BCR(cs), FMC2_BCR_MBKEN, 0);
}
static int stm32_fmc2_ebi_save_setup(struct stm32_fmc2_ebi *ebi)
{
unsigned int cs;
int ret;
for (cs = 0; cs < FMC2_MAX_EBI_CE; cs++) {
if (!(ebi->bank_assigned & BIT(cs)))
continue;
ret = regmap_read(ebi->regmap, FMC2_BCR(cs), &ebi->bcr[cs]);
ret |= regmap_read(ebi->regmap, FMC2_BTR(cs), &ebi->btr[cs]);
ret |= regmap_read(ebi->regmap, FMC2_BWTR(cs), &ebi->bwtr[cs]);
if (ret)
return ret;
}
return 0;
}
static int stm32_fmc2_ebi_mp1_save_setup(struct stm32_fmc2_ebi *ebi)
{
int ret;
ret = stm32_fmc2_ebi_save_setup(ebi);
if (ret)
return ret;
return regmap_read(ebi->regmap, FMC2_PCSCNTR, &ebi->pcscntr);
}
static int stm32_fmc2_ebi_mp25_save_setup(struct stm32_fmc2_ebi *ebi)
{
int ret;
ret = stm32_fmc2_ebi_save_setup(ebi);
if (ret)
return ret;
if (ebi->access_granted)
ret = regmap_read(ebi->regmap, FMC2_CFGR, &ebi->cfgr);
return ret;
}
static void stm32_fmc2_ebi_set_setup(struct stm32_fmc2_ebi *ebi)
{
unsigned int cs;
for (cs = 0; cs < FMC2_MAX_EBI_CE; cs++) {
if (!(ebi->bank_assigned & BIT(cs)))
continue;
regmap_write(ebi->regmap, FMC2_BCR(cs), ebi->bcr[cs]);
regmap_write(ebi->regmap, FMC2_BTR(cs), ebi->btr[cs]);
regmap_write(ebi->regmap, FMC2_BWTR(cs), ebi->bwtr[cs]);
}
}
static void stm32_fmc2_ebi_mp1_set_setup(struct stm32_fmc2_ebi *ebi)
{
stm32_fmc2_ebi_set_setup(ebi);
regmap_write(ebi->regmap, FMC2_PCSCNTR, ebi->pcscntr);
}
static void stm32_fmc2_ebi_mp25_set_setup(struct stm32_fmc2_ebi *ebi)
{
stm32_fmc2_ebi_set_setup(ebi);
if (ebi->access_granted)
regmap_write(ebi->regmap, FMC2_CFGR, ebi->cfgr);
}
static void stm32_fmc2_ebi_disable_banks(struct stm32_fmc2_ebi *ebi)
{
unsigned int cs;
for (cs = 0; cs < FMC2_MAX_EBI_CE; cs++) {
if (!(ebi->bank_assigned & BIT(cs)))
continue;
stm32_fmc2_ebi_disable_bank(ebi, cs);
}
}
/* NWAIT signal can not be connected to EBI controller and NAND controller */
static int stm32_fmc2_ebi_nwait_used_by_ctrls(struct stm32_fmc2_ebi *ebi)
{
struct device *dev = ebi->dev;
unsigned int cs;
u32 bcr;
int ret;
for (cs = 0; cs < FMC2_MAX_EBI_CE; cs++) {
if (!(ebi->bank_assigned & BIT(cs)))
continue;
ret = regmap_read(ebi->regmap, FMC2_BCR(cs), &bcr);
if (ret)
return ret;
if ((bcr & FMC2_BCR_WAITEN || bcr & FMC2_BCR_ASYNCWAIT) &&
ebi->bank_assigned & BIT(FMC2_NAND)) {
dev_err(dev, "NWAIT signal connected to EBI and NAND controllers\n");
return -EINVAL;
}
}
return 0;
}
static void stm32_fmc2_ebi_enable(struct stm32_fmc2_ebi *ebi)
{
if (!ebi->access_granted)
return;
regmap_update_bits(ebi->regmap, ebi->data->fmc2_enable_reg,
ebi->data->fmc2_enable_bit,
ebi->data->fmc2_enable_bit);
}
static void stm32_fmc2_ebi_disable(struct stm32_fmc2_ebi *ebi)
{
if (!ebi->access_granted)
return;
regmap_update_bits(ebi->regmap, ebi->data->fmc2_enable_reg,
ebi->data->fmc2_enable_bit, 0);
}
static int stm32_fmc2_ebi_setup_cs(struct stm32_fmc2_ebi *ebi,
struct device_node *dev_node,
u32 cs)
{
unsigned int i;
int ret;
stm32_fmc2_ebi_disable_bank(ebi, cs);
for (i = 0; i < ebi->data->nb_child_props; i++) {
const struct stm32_fmc2_prop *p = &ebi->data->child_props[i];
ret = stm32_fmc2_ebi_parse_prop(ebi, dev_node, p, cs);
if (ret) {
dev_err(ebi->dev, "property %s could not be set: %d\n",
p->name, ret);
return ret;
}
}
stm32_fmc2_ebi_enable_bank(ebi, cs);
return 0;
}
static int stm32_fmc2_ebi_parse_dt(struct stm32_fmc2_ebi *ebi)
{
struct device *dev = ebi->dev;
bool child_found = false;
u32 bank;
int ret;
for_each_available_child_of_node_scoped(dev->of_node, child) {
ret = of_property_read_u32(child, "reg", &bank);
if (ret)
return dev_err_probe(dev, ret, "could not retrieve reg property\n");
if (bank >= FMC2_MAX_BANKS) {
dev_err(dev, "invalid reg value: %d\n", bank);
return -EINVAL;
}
if (ebi->bank_assigned & BIT(bank)) {
dev_err(dev, "bank already assigned: %d\n", bank);
return -EINVAL;
}
if (ebi->data->check_rif) {
ret = ebi->data->check_rif(ebi, bank + 1);
if (ret) {
dev_err(dev, "bank access failed: %d\n", bank);
return ret;
}
}
if (bank < FMC2_MAX_EBI_CE) {
ret = stm32_fmc2_ebi_setup_cs(ebi, child, bank);
if (ret)
return dev_err_probe(dev, ret,
"setup chip select %d failed\n", bank);
}
ebi->bank_assigned |= BIT(bank);
child_found = true;
}
if (!child_found) {
dev_warn(dev, "no subnodes found, disable the driver.\n");
return -ENODEV;
}
if (ebi->data->nwait_used_by_ctrls) {
ret = ebi->data->nwait_used_by_ctrls(ebi);
if (ret)
return ret;
}
stm32_fmc2_ebi_enable(ebi);
return of_platform_populate(dev->of_node, NULL, NULL, dev);
}
static int stm32_fmc2_ebi_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct stm32_fmc2_ebi *ebi;
struct reset_control *rstc;
int ret;
ebi = devm_kzalloc(&pdev->dev, sizeof(*ebi), GFP_KERNEL);
if (!ebi)
return -ENOMEM;
ebi->dev = dev;
platform_set_drvdata(pdev, ebi);
ebi->data = of_device_get_match_data(dev);
if (!ebi->data)
return -EINVAL;
ebi->regmap = device_node_to_regmap(dev->of_node);
if (IS_ERR(ebi->regmap))
return PTR_ERR(ebi->regmap);
ebi->clk = devm_clk_get(dev, NULL);
if (IS_ERR(ebi->clk))
return PTR_ERR(ebi->clk);
rstc = devm_reset_control_get(dev, NULL);
if (PTR_ERR(rstc) == -EPROBE_DEFER)
return -EPROBE_DEFER;
ret = devm_pm_runtime_enable(dev);
if (ret)
return ret;
ret = pm_runtime_resume_and_get(dev);
if (ret < 0)
return ret;
if (!IS_ERR(rstc)) {
reset_control_assert(rstc);
reset_control_deassert(rstc);
}
/* Check if CFGR register can be modified */
ebi->access_granted = true;
if (ebi->data->check_rif) {
ret = ebi->data->check_rif(ebi, 0);
if (ret) {
u32 sr;
ebi->access_granted = false;
ret = regmap_read(ebi->regmap, FMC2_SR, &sr);
if (ret)
goto err_release;
/* In case of CFGR is secure, just check that the FMC2 is enabled */
if (sr & FMC2_SR_ISOST) {
dev_err(dev, "FMC2 is not ready to be used.\n");
ret = -EACCES;
goto err_release;
}
}
}
ret = stm32_fmc2_ebi_parse_dt(ebi);
if (ret)
goto err_release;
ret = ebi->data->save_setup(ebi);
if (ret)
goto err_release;
return 0;
err_release:
stm32_fmc2_ebi_disable_banks(ebi);
stm32_fmc2_ebi_disable(ebi);
if (ebi->data->put_sems)
ebi->data->put_sems(ebi);
pm_runtime_put_sync_suspend(dev);
return ret;
}
static void stm32_fmc2_ebi_remove(struct platform_device *pdev)
{
struct stm32_fmc2_ebi *ebi = platform_get_drvdata(pdev);
of_platform_depopulate(&pdev->dev);
stm32_fmc2_ebi_disable_banks(ebi);
stm32_fmc2_ebi_disable(ebi);
if (ebi->data->put_sems)
ebi->data->put_sems(ebi);
pm_runtime_put_sync_suspend(&pdev->dev);
}
static int __maybe_unused stm32_fmc2_ebi_runtime_suspend(struct device *dev)
{
struct stm32_fmc2_ebi *ebi = dev_get_drvdata(dev);
clk_disable_unprepare(ebi->clk);
return 0;
}
static int __maybe_unused stm32_fmc2_ebi_runtime_resume(struct device *dev)
{
struct stm32_fmc2_ebi *ebi = dev_get_drvdata(dev);
return clk_prepare_enable(ebi->clk);
}
static int __maybe_unused stm32_fmc2_ebi_suspend(struct device *dev)
{
struct stm32_fmc2_ebi *ebi = dev_get_drvdata(dev);
stm32_fmc2_ebi_disable(ebi);
if (ebi->data->put_sems)
ebi->data->put_sems(ebi);
pm_runtime_put_sync_suspend(dev);
pinctrl_pm_select_sleep_state(dev);
return 0;
}
static int __maybe_unused stm32_fmc2_ebi_resume(struct device *dev)
{
struct stm32_fmc2_ebi *ebi = dev_get_drvdata(dev);
int ret;
pinctrl_pm_select_default_state(dev);
ret = pm_runtime_resume_and_get(dev);
if (ret < 0)
return ret;
if (ebi->data->get_sems)
ebi->data->get_sems(ebi);
ebi->data->set_setup(ebi);
stm32_fmc2_ebi_enable(ebi);
return 0;
}
static const struct dev_pm_ops stm32_fmc2_ebi_pm_ops = {
SET_RUNTIME_PM_OPS(stm32_fmc2_ebi_runtime_suspend,
stm32_fmc2_ebi_runtime_resume, NULL)
SET_SYSTEM_SLEEP_PM_OPS(stm32_fmc2_ebi_suspend, stm32_fmc2_ebi_resume)
};
static const struct stm32_fmc2_ebi_data stm32_fmc2_ebi_mp1_data = {
.child_props = stm32_fmc2_child_props,
.nb_child_props = ARRAY_SIZE(stm32_fmc2_child_props),
.fmc2_enable_reg = FMC2_BCR1,
.fmc2_enable_bit = FMC2_BCR1_FMC2EN,
.nwait_used_by_ctrls = stm32_fmc2_ebi_nwait_used_by_ctrls,
.set_setup = stm32_fmc2_ebi_mp1_set_setup,
.save_setup = stm32_fmc2_ebi_mp1_save_setup,
};
static const struct stm32_fmc2_ebi_data stm32_fmc2_ebi_mp25_data = {
.child_props = stm32_fmc2_mp25_child_props,
.nb_child_props = ARRAY_SIZE(stm32_fmc2_mp25_child_props),
.fmc2_enable_reg = FMC2_CFGR,
.fmc2_enable_bit = FMC2_CFGR_FMC2EN,
.set_setup = stm32_fmc2_ebi_mp25_set_setup,
.save_setup = stm32_fmc2_ebi_mp25_save_setup,
.check_rif = stm32_fmc2_ebi_mp25_check_rif,
.put_sems = stm32_fmc2_ebi_mp25_put_sems,
.get_sems = stm32_fmc2_ebi_mp25_get_sems,
};
static const struct of_device_id stm32_fmc2_ebi_match[] = {
{
.compatible = "st,stm32mp1-fmc2-ebi",
.data = &stm32_fmc2_ebi_mp1_data,
},
{
.compatible = "st,stm32mp25-fmc2-ebi",
.data = &stm32_fmc2_ebi_mp25_data,
},
{}
};
MODULE_DEVICE_TABLE(of, stm32_fmc2_ebi_match);
static struct platform_driver stm32_fmc2_ebi_driver = {
.probe = stm32_fmc2_ebi_probe,
.remove_new = stm32_fmc2_ebi_remove,
.driver = {
.name = "stm32_fmc2_ebi",
.of_match_table = stm32_fmc2_ebi_match,
.pm = &stm32_fmc2_ebi_pm_ops,
},
};
module_platform_driver(stm32_fmc2_ebi_driver);
MODULE_ALIAS("platform:stm32_fmc2_ebi");
MODULE_AUTHOR("Christophe Kerello <christophe.kerello@st.com>");
MODULE_DESCRIPTION("STMicroelectronics STM32 FMC2 ebi driver");
MODULE_LICENSE("GPL v2");
|