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
|
/* $OpenBSD: dwpcie.c,v 1.39 2022/11/27 22:04:59 kettenis Exp $ */
/*
* Copyright (c) 2018 Mark Kettenis <kettenis@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/evcount.h>
#include <sys/extent.h>
#include <sys/malloc.h>
#include <machine/intr.h>
#include <machine/bus.h>
#include <machine/fdt.h>
#include <dev/pci/pcidevs.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/ppbreg.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_clock.h>
#include <dev/ofw/ofw_gpio.h>
#include <dev/ofw/ofw_misc.h>
#include <dev/ofw/ofw_pinctrl.h>
#include <dev/ofw/ofw_power.h>
#include <dev/ofw/fdt.h>
/* Registers */
#define PCIE_PORT_LINK_CTRL 0x710
#define PCIE_PORT_LINK_CTRL_LANES_MASK (0x3f << 16)
#define PCIE_PORT_LINK_CTRL_LANES_1 (0x1 << 16)
#define PCIE_PORT_LINK_CTRL_LANES_2 (0x3 << 16)
#define PCIE_PORT_LINK_CTRL_LANES_4 (0x7 << 16)
#define PCIE_PORT_LINK_CTRL_LANES_8 (0xf << 16)
#define PCIE_PHY_DEBUG_R1 0x72c
#define PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING (1 << 29)
#define PCIE_PHY_DEBUG_R1_XMLH_LINK_UP (1 << 4)
#define PCIE_LINK_WIDTH_SPEED_CTRL 0x80c
#define PCIE_LINK_WIDTH_SPEED_CTRL_LANES_MASK (0x1f << 8)
#define PCIE_LINK_WIDTH_SPEED_CTRL_LANES_1 (0x1 << 8)
#define PCIE_LINK_WIDTH_SPEED_CTRL_LANES_2 (0x2 << 8)
#define PCIE_LINK_WIDTH_SPEED_CTRL_LANES_4 (0x4 << 8)
#define PCIE_LINK_WIDTH_SPEED_CTRL_LANES_8 (0x8 << 8)
#define PCIE_LINK_WIDTH_SPEED_CTRL_CHANGE (1 << 17)
#define PCIE_MSI_ADDR_LO 0x820
#define PCIE_MSI_ADDR_HI 0x824
#define PCIE_MSI_INTR0_ENABLE 0x828
#define PCIE_MSI_INTR0_MASK 0x82c
#define PCIE_MSI_INTR0_STATUS 0x830
#define MISC_CONTROL_1 0x8bc
#define MISC_CONTROL_1_DBI_RO_WR_EN (1 << 0)
#define IATU_VIEWPORT 0x900
#define IATU_VIEWPORT_INDEX0 0
#define IATU_VIEWPORT_INDEX1 1
#define IATU_VIEWPORT_INDEX2 2
#define IATU_VIEWPORT_INDEX3 3
#define IATU_OFFSET_VIEWPORT 0x904
#define IATU_OFFSET_UNROLL(x) (0x200 * (x))
#define IATU_REGION_CTRL_1 0x000
#define IATU_REGION_CTRL_1_TYPE_MEM 0
#define IATU_REGION_CTRL_1_TYPE_IO 2
#define IATU_REGION_CTRL_1_TYPE_CFG0 4
#define IATU_REGION_CTRL_1_TYPE_CFG1 5
#define IATU_REGION_CTRL_2 0x004
#define IATU_REGION_CTRL_2_REGION_EN (1U << 31)
#define IATU_LWR_BASE_ADDR 0x08
#define IATU_UPPER_BASE_ADDR 0x0c
#define IATU_LIMIT_ADDR 0x10
#define IATU_LWR_TARGET_ADDR 0x14
#define IATU_UPPER_TARGET_ADDR 0x18
#define PCIE_GLOBAL_CTRL 0x8000
#define PCIE_GLOBAL_CTRL_APP_LTSSM_EN (1 << 2)
#define PCIE_GLOBAL_CTRL_DEVICE_TYPE_MASK (0xf << 4)
#define PCIE_GLOBAL_CTRL_DEVICE_TYPE_RC (0x4 << 4)
#define PCIE_GLOBAL_STATUS 0x8008
#define PCIE_GLOBAL_STATUS_RDLH_LINK_UP (1 << 1)
#define PCIE_GLOBAL_STATUS_PHY_LINK_UP (1 << 9)
#define PCIE_PM_STATUS 0x8014
#define PCIE_GLOBAL_INT_CAUSE 0x801c
#define PCIE_GLOBAL_INT_MASK 0x8020
#define PCIE_GLOBAL_INT_MASK_INT_A (1 << 9)
#define PCIE_GLOBAL_INT_MASK_INT_B (1 << 10)
#define PCIE_GLOBAL_INT_MASK_INT_C (1 << 11)
#define PCIE_GLOBAL_INT_MASK_INT_D (1 << 12)
#define PCIE_ARCACHE_TRC 0x8050
#define PCIE_ARCACHE_TRC_DEFAULT 0x3511
#define PCIE_AWCACHE_TRC 0x8054
#define PCIE_AWCACHE_TRC_DEFAULT 0x5311
#define PCIE_ARUSER 0x805c
#define PCIE_AWUSER 0x8060
#define PCIE_AXUSER_DOMAIN_MASK (0x3 << 4)
#define PCIE_AXUSER_DOMAIN_INNER_SHARABLE (0x1 << 4)
#define PCIE_AXUSER_DOMAIN_OUTER_SHARABLE (0x2 << 4)
#define PCIE_STREAMID 0x8064
#define PCIE_STREAMID_FUNC_BITS(x) ((x) << 0)
#define PCIE_STREAMID_DEV_BITS(x) ((x) << 4)
#define PCIE_STREAMID_BUS_BITS(x) ((x) << 8)
#define PCIE_STREAMID_ROOTPORT(x) ((x) << 12)
#define PCIE_STREAMID_8040 \
(PCIE_STREAMID_ROOTPORT(0x80) | PCIE_STREAMID_BUS_BITS(2) | \
PCIE_STREAMID_DEV_BITS(2) | PCIE_STREAMID_FUNC_BITS(3))
/* Amlogic G12A registers */
#define PCIE_CFG0 0x0000
#define PCIE_CFG0_APP_LTSSM_EN (1 << 7)
#define PCIE_STATUS12 0x0030
#define PCIE_STATUS12_RDLH_LINK_UP (1 << 16)
#define PCIE_STATUS12_LTSSM_MASK (0x1f << 10)
#define PCIE_STATUS12_LTSSM_UP (0x11 << 10)
#define PCIE_STATUS12_SMLH_LINK_UP (1 << 6)
/* NXP i.MX8MQ registers */
#define PCIE_RC_LCR 0x7c
#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1
#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2
#define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf
#define PCIE_RC_LCR_L1EL_MASK (0x7 << 15)
#define PCIE_RC_LCR_L1EL_64US (0x6 << 15)
#define IOMUXC_GPR12 0x30
#define IMX8MQ_GPR_PCIE2_DEVICE_TYPE_MASK (0xf << 8)
#define IMX8MQ_GPR_PCIE2_DEVICE_TYPE_RC (0x4 << 8)
#define IMX8MQ_GPR_PCIE1_DEVICE_TYPE_MASK (0xf << 12)
#define IMX8MQ_GPR_PCIE1_DEVICE_TYPE_RC (0x4 << 12)
#define IOMUXC_GPR14 0x38
#define IOMUXC_GPR16 0x40
#define IMX8MQ_GPR_PCIE_REF_USE_PAD (1 << 9)
#define IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN (1 << 10)
#define IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE (1 << 11)
#define IMX8MM_GPR_PCIE_SSC_EN (1 << 16)
#define IMX8MM_GPR_PCIE_POWER_OFF (1 << 17)
#define IMX8MM_GPR_PCIE_CMN_RST (1 << 18)
#define IMX8MM_GPR_PCIE_AUX_EN (1 << 19)
#define IMX8MM_GPR_PCIE_REF_CLK_MASK (0x3 << 24)
#define IMX8MM_GPR_PCIE_REF_CLK_PLL (0x3 << 24)
#define IMX8MM_GPR_PCIE_REF_CLK_EXT (0x2 << 24)
#define IMX8MM_PCIE_PHY_CMN_REG62 0x188
#define IMX8MM_PCIE_PHY_CMN_REG62_PLL_CLK_OUT 0x08
#define IMX8MM_PCIE_PHY_CMN_REG64 0x190
#define IMX8MM_PCIE_PHY_CMN_REG64_AUX_RX_TX_TERM 0x8c
#define IMX8MM_PCIE_PHY_CMN_REG75 0x1d4
#define IMX8MM_PCIE_PHY_CMN_REG75_PLL_DONE 0x3
#define IMX8MM_PCIE_PHY_TRSV_REG5 0x414
#define IMX8MM_PCIE_PHY_TRSV_REG5_GEN1_DEEMP 0x2d
#define IMX8MM_PCIE_PHY_TRSV_REG6 0x418
#define IMX8MM_PCIE_PHY_TRSV_REG6_GEN2_DEEMP 0xf
#define ANATOP_PLLOUT_CTL 0x74
#define ANATOP_PLLOUT_CTL_CKE (1 << 4)
#define ANATOP_PLLOUT_CTL_SEL_SYSPLL1 0xb
#define ANATOP_PLLOUT_CTL_SEL_MASK 0xf
#define ANATOP_PLLOUT_DIV 0x7c
#define ANATOP_PLLOUT_DIV_SYSPLL1 0x7
#define HREAD4(sc, reg) \
(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
#define HWRITE4(sc, reg, val) \
bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
#define HSET4(sc, reg, bits) \
HWRITE4((sc), (reg), HREAD4((sc), (reg)) | (bits))
#define HCLR4(sc, reg, bits) \
HWRITE4((sc), (reg), HREAD4((sc), (reg)) & ~(bits))
struct dwpcie_range {
uint32_t flags;
uint64_t pci_base;
uint64_t phys_base;
uint64_t size;
};
#define DWPCIE_NUM_MSI 32
struct dwpcie_msi {
int (*dm_func)(void *);
void *dm_arg;
int dm_ipl;
int dm_flags;
int dm_vec;
struct evcount dm_count;
char *dm_name;
};
struct dwpcie_softc {
struct device sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
bus_dma_tag_t sc_dmat;
bus_addr_t sc_ctrl_base;
bus_size_t sc_ctrl_size;
bus_addr_t sc_conf_base;
bus_size_t sc_conf_size;
bus_space_handle_t sc_conf_ioh;
bus_addr_t sc_glue_base;
bus_size_t sc_glue_size;
bus_space_handle_t sc_glue_ioh;
bus_addr_t sc_atu_base;
bus_size_t sc_atu_size;
bus_space_handle_t sc_atu_ioh;
bus_addr_t sc_io_base;
bus_addr_t sc_io_bus_addr;
bus_size_t sc_io_size;
bus_addr_t sc_mem_base;
bus_addr_t sc_mem_bus_addr;
bus_size_t sc_mem_size;
bus_addr_t sc_pmem_base;
bus_addr_t sc_pmem_bus_addr;
bus_size_t sc_pmem_size;
int sc_node;
int sc_acells;
int sc_scells;
int sc_pacells;
int sc_pscells;
struct dwpcie_range *sc_ranges;
int sc_nranges;
struct bus_space sc_bus_iot;
struct bus_space sc_bus_memt;
struct machine_pci_chipset sc_pc;
int sc_bus;
int sc_num_viewport;
int sc_atu_unroll;
int sc_atu_viewport;
void *sc_ih;
uint64_t sc_msi_addr;
struct dwpcie_msi sc_msi[DWPCIE_NUM_MSI];
};
struct dwpcie_intr_handle {
struct machine_intr_handle pih_ih;
struct dwpcie_softc *pih_sc;
struct dwpcie_msi *pih_dm;
bus_dma_tag_t pih_dmat;
bus_dmamap_t pih_map;
};
int dwpcie_match(struct device *, void *, void *);
void dwpcie_attach(struct device *, struct device *, void *);
const struct cfattach dwpcie_ca = {
sizeof (struct dwpcie_softc), dwpcie_match, dwpcie_attach
};
struct cfdriver dwpcie_cd = {
NULL, "dwpcie", DV_DULL
};
int
dwpcie_match(struct device *parent, void *match, void *aux)
{
struct fdt_attach_args *faa = aux;
return (OF_is_compatible(faa->fa_node, "amlogic,g12a-pcie") ||
OF_is_compatible(faa->fa_node, "fsl,imx8mm-pcie") ||
OF_is_compatible(faa->fa_node, "fsl,imx8mq-pcie") ||
OF_is_compatible(faa->fa_node, "marvell,armada8k-pcie") ||
OF_is_compatible(faa->fa_node, "qcom,pcie-sc8280xp") ||
OF_is_compatible(faa->fa_node, "rockchip,rk3568-pcie") ||
OF_is_compatible(faa->fa_node, "sifive,fu740-pcie"));
}
void dwpcie_attach_deferred(struct device *);
void dwpcie_atu_disable(struct dwpcie_softc *, int);
void dwpcie_atu_config(struct dwpcie_softc *, int, int,
uint64_t, uint64_t, uint64_t);
void dwpcie_link_config(struct dwpcie_softc *);
int dwpcie_link_up(struct dwpcie_softc *);
int dwpcie_armada8k_init(struct dwpcie_softc *);
int dwpcie_armada8k_link_up(struct dwpcie_softc *);
int dwpcie_armada8k_intr(void *);
int dwpcie_g12a_init(struct dwpcie_softc *);
int dwpcie_g12a_link_up(struct dwpcie_softc *);
int dwpcie_imx8mq_init(struct dwpcie_softc *);
int dwpcie_imx8mq_intr(void *);
int dwpcie_fu740_init(struct dwpcie_softc *);
int dwpcie_rk3568_init(struct dwpcie_softc *);
int dwpcie_sc8280xp_init(struct dwpcie_softc *);
void dwpcie_attach_hook(struct device *, struct device *,
struct pcibus_attach_args *);
int dwpcie_bus_maxdevs(void *, int);
pcitag_t dwpcie_make_tag(void *, int, int, int);
void dwpcie_decompose_tag(void *, pcitag_t, int *, int *, int *);
int dwpcie_conf_size(void *, pcitag_t);
pcireg_t dwpcie_conf_read(void *, pcitag_t, int);
void dwpcie_conf_write(void *, pcitag_t, int, pcireg_t);
int dwpcie_probe_device_hook(void *, struct pci_attach_args *);
int dwpcie_intr_map(struct pci_attach_args *, pci_intr_handle_t *);
const char *dwpcie_intr_string(void *, pci_intr_handle_t);
void *dwpcie_intr_establish(void *, pci_intr_handle_t, int,
struct cpu_info *, int (*)(void *), void *, char *);
void dwpcie_intr_disestablish(void *, void *);
int dwpcie_bs_iomap(bus_space_tag_t, bus_addr_t, bus_size_t, int,
bus_space_handle_t *);
int dwpcie_bs_memmap(bus_space_tag_t, bus_addr_t, bus_size_t, int,
bus_space_handle_t *);
struct interrupt_controller dwpcie_ic = {
.ic_barrier = intr_barrier
};
void
dwpcie_attach(struct device *parent, struct device *self, void *aux)
{
struct dwpcie_softc *sc = (struct dwpcie_softc *)self;
struct fdt_attach_args *faa = aux;
uint32_t *ranges;
int i, j, nranges, rangeslen;
int atu, config, ctrl, glue;
if (faa->fa_nreg < 2) {
printf(": no registers\n");
return;
}
sc->sc_ctrl_base = faa->fa_reg[0].addr;
sc->sc_ctrl_size = faa->fa_reg[0].size;
ctrl = OF_getindex(faa->fa_node, "dbi", "reg-names");
if (ctrl >= 0 && ctrl < faa->fa_nreg) {
sc->sc_ctrl_base = faa->fa_reg[ctrl].addr;
sc->sc_ctrl_size = faa->fa_reg[ctrl].size;
}
config = OF_getindex(faa->fa_node, "config", "reg-names");
if (config < 0 || config >= faa->fa_nreg) {
printf(": no config registers\n");
return;
}
sc->sc_conf_base = faa->fa_reg[config].addr;
sc->sc_conf_size = faa->fa_reg[config].size;
sc->sc_atu_base = sc->sc_ctrl_base + 0x300000;
sc->sc_atu_size = sc->sc_ctrl_size - 0x300000;
atu = OF_getindex(faa->fa_node, "atu", "reg-names");
if (atu >= 0 && atu < faa->fa_nreg) {
sc->sc_atu_base = faa->fa_reg[atu].addr;
sc->sc_atu_size = faa->fa_reg[atu].size;
}
if (OF_is_compatible(faa->fa_node, "amlogic,g12a-pcie")) {
glue = OF_getindex(faa->fa_node, "cfg", "reg-names");
if (glue < 0 || glue >= faa->fa_nreg) {
printf(": no glue registers\n");
return;
}
sc->sc_glue_base = faa->fa_reg[glue].addr;
sc->sc_glue_size = faa->fa_reg[glue].size;
}
sc->sc_iot = faa->fa_iot;
sc->sc_dmat = faa->fa_dmat;
sc->sc_node = faa->fa_node;
sc->sc_acells = OF_getpropint(sc->sc_node, "#address-cells",
faa->fa_acells);
sc->sc_scells = OF_getpropint(sc->sc_node, "#size-cells",
faa->fa_scells);
sc->sc_pacells = faa->fa_acells;
sc->sc_pscells = faa->fa_scells;
rangeslen = OF_getproplen(sc->sc_node, "ranges");
if (rangeslen <= 0 || (rangeslen % sizeof(uint32_t)) ||
(rangeslen / sizeof(uint32_t)) % (sc->sc_acells +
sc->sc_pacells + sc->sc_scells)) {
printf(": invalid ranges property\n");
return;
}
ranges = malloc(rangeslen, M_TEMP, M_WAITOK);
OF_getpropintarray(sc->sc_node, "ranges", ranges,
rangeslen);
nranges = (rangeslen / sizeof(uint32_t)) /
(sc->sc_acells + sc->sc_pacells + sc->sc_scells);
sc->sc_ranges = mallocarray(nranges,
sizeof(struct dwpcie_range), M_TEMP, M_WAITOK);
sc->sc_nranges = nranges;
for (i = 0, j = 0; i < sc->sc_nranges; i++) {
sc->sc_ranges[i].flags = ranges[j++];
sc->sc_ranges[i].pci_base = ranges[j++];
if (sc->sc_acells - 1 == 2) {
sc->sc_ranges[i].pci_base <<= 32;
sc->sc_ranges[i].pci_base |= ranges[j++];
}
sc->sc_ranges[i].phys_base = ranges[j++];
if (sc->sc_pacells == 2) {
sc->sc_ranges[i].phys_base <<= 32;
sc->sc_ranges[i].phys_base |= ranges[j++];
}
sc->sc_ranges[i].size = ranges[j++];
if (sc->sc_scells == 2) {
sc->sc_ranges[i].size <<= 32;
sc->sc_ranges[i].size |= ranges[j++];
}
}
free(ranges, M_TEMP, rangeslen);
if (bus_space_map(sc->sc_iot, sc->sc_ctrl_base,
sc->sc_ctrl_size, 0, &sc->sc_ioh)) {
free(sc->sc_ranges, M_TEMP, sc->sc_nranges *
sizeof(struct dwpcie_range));
printf(": can't map ctrl registers\n");
return;
}
if (bus_space_map(sc->sc_iot, sc->sc_conf_base,
sc->sc_conf_size, 0, &sc->sc_conf_ioh)) {
bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ctrl_size);
free(sc->sc_ranges, M_TEMP, sc->sc_nranges *
sizeof(struct dwpcie_range));
printf(": can't map config registers\n");
return;
}
sc->sc_num_viewport = OF_getpropint(sc->sc_node, "num-viewport", 2);
printf("\n");
pinctrl_byname(sc->sc_node, "default");
clock_set_assigned(sc->sc_node);
config_defer(self, dwpcie_attach_deferred);
}
void
dwpcie_attach_deferred(struct device *self)
{
struct dwpcie_softc *sc = (struct dwpcie_softc *)self;
struct pcibus_attach_args pba;
bus_addr_t iobase, iolimit;
bus_addr_t membase, memlimit;
bus_addr_t pmembase, pmemlimit;
uint32_t bus_range[2];
pcireg_t bir, blr, csr;
int i, error = 0;
if (OF_is_compatible(sc->sc_node, "marvell,armada8k-pcie"))
error = dwpcie_armada8k_init(sc);
if (OF_is_compatible(sc->sc_node, "amlogic,g12a-pcie"))
error = dwpcie_g12a_init(sc);
if (OF_is_compatible(sc->sc_node, "fsl,imx8mm-pcie") ||
OF_is_compatible(sc->sc_node, "fsl,imx8mq-pcie"))
error = dwpcie_imx8mq_init(sc);
if (OF_is_compatible(sc->sc_node, "qcom,pcie-sc8280xp"))
error = dwpcie_sc8280xp_init(sc);
if (OF_is_compatible(sc->sc_node, "rockchip,rk3568-pcie"))
error = dwpcie_rk3568_init(sc);
if (OF_is_compatible(sc->sc_node, "sifive,fu740-pcie"))
error = dwpcie_fu740_init(sc);
if (error != 0) {
bus_space_unmap(sc->sc_iot, sc->sc_conf_ioh, sc->sc_conf_size);
bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ctrl_size);
free(sc->sc_ranges, M_TEMP, sc->sc_nranges *
sizeof(struct dwpcie_range));
printf("%s: can't initialize hardware\n",
sc->sc_dev.dv_xname);
return;
}
sc->sc_atu_viewport = -1;
if (HREAD4(sc, IATU_VIEWPORT) == 0xffffffff) {
sc->sc_atu_unroll = 1;
if (bus_space_map(sc->sc_iot, sc->sc_atu_base,
sc->sc_atu_size, 0, &sc->sc_atu_ioh)) {
bus_space_unmap(sc->sc_iot, sc->sc_conf_ioh,
sc->sc_conf_size);
bus_space_unmap(sc->sc_iot, sc->sc_ioh,
sc->sc_ctrl_size);
free(sc->sc_ranges, M_TEMP, sc->sc_nranges *
sizeof(struct dwpcie_range));
printf("%s: can't map atu registers\n",
sc->sc_dev.dv_xname);
return;
}
}
/* Set up address translation for I/O space. */
for (i = 0; i < sc->sc_nranges; i++) {
if ((sc->sc_ranges[i].flags & 0x03000000) == 0x01000000 &&
sc->sc_ranges[i].size > 0) {
sc->sc_io_base = sc->sc_ranges[i].phys_base;
sc->sc_io_bus_addr = sc->sc_ranges[i].pci_base;
sc->sc_io_size = sc->sc_ranges[i].size;
}
if ((sc->sc_ranges[i].flags & 0x03000000) == 0x02000000 &&
sc->sc_ranges[i].size > 0) {
sc->sc_mem_base = sc->sc_ranges[i].phys_base;
sc->sc_mem_bus_addr = sc->sc_ranges[i].pci_base;
sc->sc_mem_size = sc->sc_ranges[i].size;
}
if ((sc->sc_ranges[i].flags & 0x03000000) == 0x03000000 &&
sc->sc_ranges[i].size > 0) {
sc->sc_pmem_base = sc->sc_ranges[i].phys_base;
sc->sc_pmem_bus_addr = sc->sc_ranges[i].pci_base;
sc->sc_pmem_size = sc->sc_ranges[i].size;
}
}
if (sc->sc_mem_size == 0) {
printf("%s: no memory mapped I/O window\n",
sc->sc_dev.dv_xname);
return;
}
/*
* Disable prefetchable memory mapped I/O window if we don't
* have enough viewports to enable it.
*/
if (sc->sc_num_viewport < 4)
sc->sc_pmem_size = 0;
for (i = 0; i < sc->sc_num_viewport; i++)
dwpcie_atu_disable(sc, i);
dwpcie_atu_config(sc, IATU_VIEWPORT_INDEX0,
IATU_REGION_CTRL_1_TYPE_MEM, sc->sc_mem_base,
sc->sc_mem_bus_addr, sc->sc_mem_size);
if (sc->sc_num_viewport > 2 && sc->sc_io_size > 0)
dwpcie_atu_config(sc, IATU_VIEWPORT_INDEX2,
IATU_REGION_CTRL_1_TYPE_IO, sc->sc_io_base,
sc->sc_io_bus_addr, sc->sc_io_size);
if (sc->sc_num_viewport > 3 && sc->sc_pmem_size > 0)
dwpcie_atu_config(sc, IATU_VIEWPORT_INDEX3,
IATU_REGION_CTRL_1_TYPE_MEM, sc->sc_pmem_base,
sc->sc_pmem_bus_addr, sc->sc_pmem_size);
/* Enable modification of read-only bits. */
HSET4(sc, MISC_CONTROL_1, MISC_CONTROL_1_DBI_RO_WR_EN);
/* A Root Port is a PCI-PCI Bridge. */
HWRITE4(sc, PCI_CLASS_REG,
PCI_CLASS_BRIDGE << PCI_CLASS_SHIFT |
PCI_SUBCLASS_BRIDGE_PCI << PCI_SUBCLASS_SHIFT);
/* Clear BAR as U-Boot seems to leave garbage in it. */
HWRITE4(sc, PCI_MAPREG_START, PCI_MAPREG_MEM_TYPE_64BIT);
HWRITE4(sc, PCI_MAPREG_START + 4, 0);
/* Enable 32-bit I/O addressing. */
HSET4(sc, PPB_REG_IOSTATUS,
PPB_IO_32BIT | (PPB_IO_32BIT << PPB_IOLIMIT_SHIFT));
/* Make sure read-only bits are write-protected. */
HCLR4(sc, MISC_CONTROL_1, MISC_CONTROL_1_DBI_RO_WR_EN);
/* Set up bus range. */
if (OF_getpropintarray(sc->sc_node, "bus-range", bus_range,
sizeof(bus_range)) != sizeof(bus_range) ||
bus_range[0] >= 32 || bus_range[1] >= 32) {
bus_range[0] = 0;
bus_range[1] = 31;
}
sc->sc_bus = bus_range[0];
/* Initialize bus range. */
bir = bus_range[0];
bir |= ((bus_range[0] + 1) << 8);
bir |= (bus_range[1] << 16);
HWRITE4(sc, PPB_REG_BUSINFO, bir);
/* Initialize memory mapped I/O window. */
membase = sc->sc_mem_bus_addr;
memlimit = membase + sc->sc_mem_size - 1;
blr = memlimit & PPB_MEM_MASK;
blr |= (membase >> PPB_MEM_SHIFT);
HWRITE4(sc, PPB_REG_MEM, blr);
/* Initialize I/O window. */
if (sc->sc_io_size > 0) {
iobase = sc->sc_io_bus_addr;
iolimit = iobase + sc->sc_io_size - 1;
blr = iolimit & PPB_IO_MASK;
blr |= (iobase >> PPB_IO_SHIFT);
HWRITE4(sc, PPB_REG_IOSTATUS, blr);
blr = (iobase & 0xffff0000) >> 16;
blr |= iolimit & 0xffff0000;
HWRITE4(sc, PPB_REG_IO_HI, blr);
} else {
HWRITE4(sc, PPB_REG_IOSTATUS, 0x000000ff);
HWRITE4(sc, PPB_REG_IO_HI, 0x0000ffff);
}
/* Initialize prefetchable memory mapped I/O window. */
if (sc->sc_pmem_size > 0) {
pmembase = sc->sc_pmem_bus_addr;
pmemlimit = pmembase + sc->sc_pmem_size - 1;
blr = pmemlimit & PPB_MEM_MASK;
blr |= (pmembase >> PPB_MEM_SHIFT);
HWRITE4(sc, PPB_REG_PREFMEM, blr);
HWRITE4(sc, PPB_REG_PREFBASE_HI32, pmembase >> 32);
HWRITE4(sc, PPB_REG_PREFLIM_HI32, pmemlimit >> 32);
} else {
HWRITE4(sc, PPB_REG_PREFMEM, 0x0000ffff);
HWRITE4(sc, PPB_REG_PREFBASE_HI32, 0);
HWRITE4(sc, PPB_REG_PREFLIM_HI32, 0);
}
csr = PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
if (sc->sc_io_size > 0)
csr |= PCI_COMMAND_IO_ENABLE;
HWRITE4(sc, PCI_COMMAND_STATUS_REG, csr);
memcpy(&sc->sc_bus_iot, sc->sc_iot, sizeof(sc->sc_bus_iot));
sc->sc_bus_iot.bus_private = sc;
sc->sc_bus_iot._space_map = dwpcie_bs_iomap;
memcpy(&sc->sc_bus_memt, sc->sc_iot, sizeof(sc->sc_bus_memt));
sc->sc_bus_memt.bus_private = sc;
sc->sc_bus_memt._space_map = dwpcie_bs_memmap;
sc->sc_pc.pc_conf_v = sc;
sc->sc_pc.pc_attach_hook = dwpcie_attach_hook;
sc->sc_pc.pc_bus_maxdevs = dwpcie_bus_maxdevs;
sc->sc_pc.pc_make_tag = dwpcie_make_tag;
sc->sc_pc.pc_decompose_tag = dwpcie_decompose_tag;
sc->sc_pc.pc_conf_size = dwpcie_conf_size;
sc->sc_pc.pc_conf_read = dwpcie_conf_read;
sc->sc_pc.pc_conf_write = dwpcie_conf_write;
sc->sc_pc.pc_probe_device_hook = dwpcie_probe_device_hook;
sc->sc_pc.pc_intr_v = sc;
sc->sc_pc.pc_intr_map = dwpcie_intr_map;
sc->sc_pc.pc_intr_map_msi = _pci_intr_map_msi;
sc->sc_pc.pc_intr_map_msix = _pci_intr_map_msix;
sc->sc_pc.pc_intr_string = dwpcie_intr_string;
sc->sc_pc.pc_intr_establish = dwpcie_intr_establish;
sc->sc_pc.pc_intr_disestablish = dwpcie_intr_disestablish;
memset(&pba, 0, sizeof(pba));
pba.pba_busname = "pci";
pba.pba_iot = &sc->sc_bus_iot;
pba.pba_memt = &sc->sc_bus_memt;
pba.pba_dmat = sc->sc_dmat;
pba.pba_pc = &sc->sc_pc;
pba.pba_domain = pci_ndomains++;
pba.pba_bus = sc->sc_bus;
if (OF_is_compatible(sc->sc_node, "marvell,armada8k-pcie") ||
OF_is_compatible(sc->sc_node, "rockchip,rk3568-pcie") ||
sc->sc_msi_addr)
pba.pba_flags |= PCI_FLAGS_MSI_ENABLED;
config_found(self, &pba, NULL);
}
void
dwpcie_link_config(struct dwpcie_softc *sc)
{
uint32_t mode, width, reg;
int lanes;
lanes = OF_getpropint(sc->sc_node, "num-lanes", 0);
switch (lanes) {
case 1:
mode = PCIE_PORT_LINK_CTRL_LANES_1;
width = PCIE_LINK_WIDTH_SPEED_CTRL_LANES_1;
break;
case 2:
mode = PCIE_PORT_LINK_CTRL_LANES_2;
width = PCIE_LINK_WIDTH_SPEED_CTRL_LANES_2;
break;
case 4:
mode = PCIE_PORT_LINK_CTRL_LANES_4;
width = PCIE_LINK_WIDTH_SPEED_CTRL_LANES_4;
break;
case 8:
mode = PCIE_PORT_LINK_CTRL_LANES_8;
width = PCIE_LINK_WIDTH_SPEED_CTRL_LANES_8;
break;
default:
printf("%s: %d lanes not supported\n", __func__, lanes);
return;
}
reg = HREAD4(sc, PCIE_PORT_LINK_CTRL);
reg &= ~PCIE_PORT_LINK_CTRL_LANES_MASK;
reg |= mode;
HWRITE4(sc, PCIE_PORT_LINK_CTRL, reg);
reg = HREAD4(sc, PCIE_LINK_WIDTH_SPEED_CTRL);
reg &= ~PCIE_LINK_WIDTH_SPEED_CTRL_LANES_MASK;
reg |= width;
HWRITE4(sc, PCIE_LINK_WIDTH_SPEED_CTRL, reg);
reg = HREAD4(sc, PCIE_LINK_WIDTH_SPEED_CTRL);
reg |= PCIE_LINK_WIDTH_SPEED_CTRL_CHANGE;
HWRITE4(sc, PCIE_LINK_WIDTH_SPEED_CTRL, reg);
}
int
dwpcie_msi_intr(void *arg)
{
struct dwpcie_softc *sc = arg;
struct dwpcie_msi *dm;
uint32_t status;
int vec, s;
status = HREAD4(sc, PCIE_MSI_INTR0_STATUS);
if (status == 0)
return 0;
HWRITE4(sc, PCIE_MSI_INTR0_STATUS, status);
while (status) {
vec = ffs(status) - 1;
status &= ~(1U << vec);
dm = &sc->sc_msi[vec];
if (dm->dm_func == NULL)
continue;
if ((dm->dm_flags & IPL_MPSAFE) == 0)
KERNEL_LOCK();
s = splraise(dm->dm_ipl);
if (dm->dm_func(dm->dm_arg))
dm->dm_count.ec_count++;
splx(s);
if ((dm->dm_flags & IPL_MPSAFE) == 0)
KERNEL_UNLOCK();
}
return 1;
}
int
dwpcie_msi_init(struct dwpcie_softc *sc)
{
bus_dma_segment_t seg;
bus_dmamap_t map;
uint64_t addr;
int error, rseg;
/*
* Allocate some DMA memory such that we have a "safe" target
* address for MSIs.
*/
error = bus_dmamem_alloc(sc->sc_dmat, sizeof(uint32_t),
sizeof(uint32_t), 0, &seg, 1, &rseg, BUS_DMA_WAITOK);
if (error)
return error;
/*
* Translate the CPU address into a bus address that we can
* program into the hardware.
*/
error = bus_dmamap_create(sc->sc_dmat, sizeof(uint32_t), 1,
sizeof(uint32_t), 0, BUS_DMA_WAITOK, &map);
if (error) {
bus_dmamem_free(sc->sc_dmat, &seg, 1);
return error;
}
error = bus_dmamap_load_raw(sc->sc_dmat, map, &seg, 1,
sizeof(uint32_t), BUS_DMA_WAITOK);
if (error) {
bus_dmamap_destroy(sc->sc_dmat, map);
bus_dmamem_free(sc->sc_dmat, &seg, 1);
return error;
}
addr = map->dm_segs[0].ds_addr;
HWRITE4(sc, PCIE_MSI_ADDR_LO, addr);
HWRITE4(sc, PCIE_MSI_ADDR_HI, addr >> 32);
bus_dmamap_unload(sc->sc_dmat, map);
bus_dmamap_destroy(sc->sc_dmat, map);
/* Enable, mask and clear all MSIs. */
HWRITE4(sc, PCIE_MSI_INTR0_ENABLE, 0xffffffff);
HWRITE4(sc, PCIE_MSI_INTR0_MASK, 0xffffffff);
HWRITE4(sc, PCIE_MSI_INTR0_STATUS, 0xffffffff);
KASSERT(sc->sc_ih == NULL);
sc->sc_ih = fdt_intr_establish(sc->sc_node, IPL_BIO | IPL_MPSAFE,
dwpcie_msi_intr, sc, sc->sc_dev.dv_xname);
if (sc->sc_ih == NULL) {
bus_dmamem_free(sc->sc_dmat, &seg, 1);
return EINVAL;
}
/*
* Hold on to the DMA memory such that nobody can use it to
* actually do DMA transfers.
*/
sc->sc_msi_addr = addr;
return 0;
}
int
dwpcie_armada8k_init(struct dwpcie_softc *sc)
{
uint32_t reg;
int timo;
clock_enable_all(sc->sc_node);
dwpcie_link_config(sc);
if (!dwpcie_armada8k_link_up(sc)) {
reg = HREAD4(sc, PCIE_GLOBAL_CTRL);
reg &= ~PCIE_GLOBAL_CTRL_APP_LTSSM_EN;
HWRITE4(sc, PCIE_GLOBAL_CTRL, reg);
}
/*
* Setup Requester-ID to Stream-ID mapping
* XXX: TF-A is supposed to set this up, but doesn't!
*/
HWRITE4(sc, PCIE_STREAMID, PCIE_STREAMID_8040);
/* Enable Root Complex mode. */
reg = HREAD4(sc, PCIE_GLOBAL_CTRL);
reg &= ~PCIE_GLOBAL_CTRL_DEVICE_TYPE_MASK;
reg |= PCIE_GLOBAL_CTRL_DEVICE_TYPE_RC;
HWRITE4(sc, PCIE_GLOBAL_CTRL, reg);
HWRITE4(sc, PCIE_ARCACHE_TRC, PCIE_ARCACHE_TRC_DEFAULT);
HWRITE4(sc, PCIE_AWCACHE_TRC, PCIE_AWCACHE_TRC_DEFAULT);
reg = HREAD4(sc, PCIE_ARUSER);
reg &= ~PCIE_AXUSER_DOMAIN_MASK;
reg |= PCIE_AXUSER_DOMAIN_OUTER_SHARABLE;
HWRITE4(sc, PCIE_ARUSER, reg);
reg = HREAD4(sc, PCIE_AWUSER);
reg &= ~PCIE_AXUSER_DOMAIN_MASK;
reg |= PCIE_AXUSER_DOMAIN_OUTER_SHARABLE;
HWRITE4(sc, PCIE_AWUSER, reg);
if (!dwpcie_armada8k_link_up(sc)) {
reg = HREAD4(sc, PCIE_GLOBAL_CTRL);
reg |= PCIE_GLOBAL_CTRL_APP_LTSSM_EN;
HWRITE4(sc, PCIE_GLOBAL_CTRL, reg);
}
for (timo = 40; timo > 0; timo--) {
if (dwpcie_armada8k_link_up(sc))
break;
delay(1000);
}
if (timo == 0)
return ETIMEDOUT;
sc->sc_ih = fdt_intr_establish(sc->sc_node, IPL_AUDIO | IPL_MPSAFE,
dwpcie_armada8k_intr, sc, sc->sc_dev.dv_xname);
/* Unmask INTx interrupts. */
HWRITE4(sc, PCIE_GLOBAL_INT_MASK,
PCIE_GLOBAL_INT_MASK_INT_A | PCIE_GLOBAL_INT_MASK_INT_B |
PCIE_GLOBAL_INT_MASK_INT_C | PCIE_GLOBAL_INT_MASK_INT_D);
return 0;
}
int
dwpcie_armada8k_link_up(struct dwpcie_softc *sc)
{
uint32_t reg, mask;
mask = PCIE_GLOBAL_STATUS_RDLH_LINK_UP;
mask |= PCIE_GLOBAL_STATUS_PHY_LINK_UP;
reg = HREAD4(sc, PCIE_GLOBAL_STATUS);
return ((reg & mask) == mask);
}
int
dwpcie_armada8k_intr(void *arg)
{
struct dwpcie_softc *sc = arg;
uint32_t cause;
/* Acknowledge interrupts. */
cause = HREAD4(sc, PCIE_GLOBAL_INT_CAUSE);
HWRITE4(sc, PCIE_GLOBAL_INT_CAUSE, cause);
/* INTx interrupt, so not really ours. */
return 0;
}
int
dwpcie_g12a_init(struct dwpcie_softc *sc)
{
uint32_t *reset_gpio;
ssize_t reset_gpiolen;
uint32_t reg;
int error, timo;
reset_gpiolen = OF_getproplen(sc->sc_node, "reset-gpios");
if (reset_gpiolen <= 0)
return ENXIO;
if (bus_space_map(sc->sc_iot, sc->sc_glue_base,
sc->sc_glue_size, 0, &sc->sc_glue_ioh))
return ENOMEM;
power_domain_enable(sc->sc_node);
phy_enable(sc->sc_node, "pcie");
reset_assert_all(sc->sc_node);
delay(500);
reset_deassert_all(sc->sc_node);
delay(500);
clock_set_frequency(sc->sc_node, "port", 100000000UL);
clock_enable_all(sc->sc_node);
reset_gpio = malloc(reset_gpiolen, M_TEMP, M_WAITOK);
OF_getpropintarray(sc->sc_node, "reset-gpios", reset_gpio,
reset_gpiolen);
gpio_controller_config_pin(reset_gpio, GPIO_CONFIG_OUTPUT);
gpio_controller_set_pin(reset_gpio, 1);
dwpcie_link_config(sc);
reg = bus_space_read_4(sc->sc_iot, sc->sc_glue_ioh, PCIE_CFG0);
reg |= PCIE_CFG0_APP_LTSSM_EN;
bus_space_write_4(sc->sc_iot, sc->sc_glue_ioh, PCIE_CFG0, reg);
gpio_controller_set_pin(reset_gpio, 1);
delay(500);
gpio_controller_set_pin(reset_gpio, 0);
free(reset_gpio, M_TEMP, reset_gpiolen);
for (timo = 40; timo > 0; timo--) {
if (dwpcie_g12a_link_up(sc))
break;
delay(1000);
}
if (timo == 0)
return ETIMEDOUT;
error = dwpcie_msi_init(sc);
if (error)
return error;
return 0;
}
int
dwpcie_g12a_link_up(struct dwpcie_softc *sc)
{
uint32_t reg;
reg = bus_space_read_4(sc->sc_iot, sc->sc_glue_ioh, PCIE_STATUS12);
if ((reg & PCIE_STATUS12_SMLH_LINK_UP) &&
(reg & PCIE_STATUS12_RDLH_LINK_UP) &&
(reg & PCIE_STATUS12_LTSSM_MASK) == PCIE_STATUS12_LTSSM_UP)
return 1;
return 0;
}
int
dwpcie_imx8mq_init(struct dwpcie_softc *sc)
{
uint32_t *clkreq_gpio, *disable_gpio, *reset_gpio;
ssize_t clkreq_gpiolen, disable_gpiolen, reset_gpiolen;
struct regmap *anatop, *gpr, *phy;
uint32_t off, reg;
int error, timo;
if (OF_is_compatible(sc->sc_node, "fsl,imx8mm-pcie")) {
anatop = regmap_bycompatible("fsl,imx8mm-anatop");
gpr = regmap_bycompatible("fsl,imx8mm-iomuxc-gpr");
phy = regmap_bycompatible("fsl,imx7d-pcie-phy");
KASSERT(phy != NULL);
} else {
anatop = regmap_bycompatible("fsl,imx8mq-anatop");
gpr = regmap_bycompatible("fsl,imx8mq-iomuxc-gpr");
}
KASSERT(anatop != NULL);
KASSERT(gpr != NULL);
clkreq_gpiolen = OF_getproplen(sc->sc_node, "clkreq-gpio");
disable_gpiolen = OF_getproplen(sc->sc_node, "disable-gpio");
reset_gpiolen = OF_getproplen(sc->sc_node, "reset-gpio");
if (clkreq_gpiolen > 0) {
clkreq_gpio = malloc(clkreq_gpiolen, M_TEMP, M_WAITOK);
OF_getpropintarray(sc->sc_node, "clkreq-gpio", clkreq_gpio,
clkreq_gpiolen);
gpio_controller_config_pin(clkreq_gpio, GPIO_CONFIG_OUTPUT);
gpio_controller_set_pin(clkreq_gpio, 1);
}
if (disable_gpiolen > 0) {
disable_gpio = malloc(disable_gpiolen, M_TEMP, M_WAITOK);
OF_getpropintarray(sc->sc_node, "disable-gpio", disable_gpio,
disable_gpiolen);
gpio_controller_config_pin(disable_gpio, GPIO_CONFIG_OUTPUT);
gpio_controller_set_pin(disable_gpio, 0);
}
if (reset_gpiolen > 0) {
reset_gpio = malloc(reset_gpiolen, M_TEMP, M_WAITOK);
OF_getpropintarray(sc->sc_node, "reset-gpio", reset_gpio,
reset_gpiolen);
gpio_controller_config_pin(reset_gpio, GPIO_CONFIG_OUTPUT);
gpio_controller_set_pin(reset_gpio, 1);
}
power_domain_enable(sc->sc_node);
reset_assert(sc->sc_node, "pciephy");
reset_assert(sc->sc_node, "apps");
reg = regmap_read_4(gpr, IOMUXC_GPR12);
if (OF_getpropint(sc->sc_node, "ctrl-id", 0) == 0) {
off = IOMUXC_GPR14;
reg &= ~IMX8MQ_GPR_PCIE1_DEVICE_TYPE_MASK;
reg |= IMX8MQ_GPR_PCIE1_DEVICE_TYPE_RC;
} else {
off = IOMUXC_GPR16;
reg &= ~IMX8MQ_GPR_PCIE2_DEVICE_TYPE_MASK;
reg |= IMX8MQ_GPR_PCIE2_DEVICE_TYPE_RC;
}
regmap_write_4(gpr, IOMUXC_GPR12, reg);
if (OF_is_compatible(sc->sc_node, "fsl,imx8mm-pcie")) {
if (OF_getproplen(sc->sc_node, "ext_osc") == 0 ||
OF_getpropint(sc->sc_node, "ext_osc", 0)) {
reg = regmap_read_4(gpr, off);
reg &= ~(IMX8MQ_GPR_PCIE_REF_USE_PAD |
IMX8MM_GPR_PCIE_SSC_EN |
IMX8MM_GPR_PCIE_POWER_OFF |
IMX8MM_GPR_PCIE_REF_CLK_MASK);
reg |= (IMX8MM_GPR_PCIE_AUX_EN |
IMX8MM_GPR_PCIE_REF_CLK_EXT);
regmap_write_4(gpr, off, reg);
delay(100);
reg = regmap_read_4(gpr, off);
reg |= IMX8MM_GPR_PCIE_CMN_RST;
regmap_write_4(gpr, off, reg);
delay(200);
} else {
reg = regmap_read_4(gpr, off);
reg &= ~(IMX8MQ_GPR_PCIE_REF_USE_PAD |
IMX8MM_GPR_PCIE_SSC_EN |
IMX8MM_GPR_PCIE_POWER_OFF |
IMX8MM_GPR_PCIE_REF_CLK_MASK);
reg |= (IMX8MM_GPR_PCIE_AUX_EN |
IMX8MM_GPR_PCIE_REF_CLK_PLL);
regmap_write_4(gpr, off, reg);
delay(100);
regmap_write_4(phy, IMX8MM_PCIE_PHY_CMN_REG62,
IMX8MM_PCIE_PHY_CMN_REG62_PLL_CLK_OUT);
regmap_write_4(phy, IMX8MM_PCIE_PHY_CMN_REG64,
IMX8MM_PCIE_PHY_CMN_REG64_AUX_RX_TX_TERM);
reg = regmap_read_4(gpr, off);
reg |= IMX8MM_GPR_PCIE_CMN_RST;
regmap_write_4(gpr, off, reg);
delay(200);
regmap_write_4(phy, IMX8MM_PCIE_PHY_TRSV_REG5,
IMX8MM_PCIE_PHY_TRSV_REG5_GEN1_DEEMP);
regmap_write_4(phy, IMX8MM_PCIE_PHY_TRSV_REG6,
IMX8MM_PCIE_PHY_TRSV_REG6_GEN2_DEEMP);
}
} else {
if (OF_getproplen(sc->sc_node, "ext_osc") == 0 ||
OF_getpropint(sc->sc_node, "ext_osc", 0)) {
reg = regmap_read_4(gpr, off);
reg |= IMX8MQ_GPR_PCIE_REF_USE_PAD;
regmap_write_4(gpr, off, reg);
} else {
reg = regmap_read_4(gpr, off);
reg &= ~IMX8MQ_GPR_PCIE_REF_USE_PAD;
regmap_write_4(gpr, off, reg);
regmap_write_4(anatop, ANATOP_PLLOUT_CTL,
ANATOP_PLLOUT_CTL_CKE |
ANATOP_PLLOUT_CTL_SEL_SYSPLL1);
regmap_write_4(anatop, ANATOP_PLLOUT_DIV,
ANATOP_PLLOUT_DIV_SYSPLL1);
}
}
clock_enable(sc->sc_node, "pcie_phy");
clock_enable(sc->sc_node, "pcie_bus");
clock_enable(sc->sc_node, "pcie");
clock_enable(sc->sc_node, "pcie_aux");
/* Allow clocks to stabilize. */
delay(200);
if (reset_gpiolen > 0) {
gpio_controller_set_pin(reset_gpio, 1);
delay(100000);
gpio_controller_set_pin(reset_gpio, 0);
}
reset_deassert(sc->sc_node, "pciephy");
if (OF_is_compatible(sc->sc_node, "fsl,imx8mm-pcie")) {
for (timo = 2000; timo > 0; timo--) {
if (regmap_read_4(phy, IMX8MM_PCIE_PHY_CMN_REG75) ==
IMX8MM_PCIE_PHY_CMN_REG75_PLL_DONE)
break;
delay(10);
}
if (timo == 0) {
error = ETIMEDOUT;
goto err;
}
}
reg = HREAD4(sc, 0x100000 + PCIE_RC_LCR);
reg &= ~PCIE_RC_LCR_L1EL_MASK;
reg |= PCIE_RC_LCR_L1EL_64US;
HWRITE4(sc, 0x100000 + PCIE_RC_LCR, reg);
dwpcie_link_config(sc);
reg = HREAD4(sc, PCIE_RC_LCR);
reg &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
reg |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
HWRITE4(sc, PCIE_RC_LCR, reg);
reset_deassert(sc->sc_node, "apps");
for (timo = 20000; timo > 0; timo--) {
if (dwpcie_link_up(sc))
break;
delay(10);
}
if (timo == 0) {
error = ETIMEDOUT;
goto err;
}
if (OF_getpropint(sc->sc_node, "fsl,max-link-speed", 1) >= 2) {
reg = HREAD4(sc, PCIE_RC_LCR);
reg &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
reg |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
HWRITE4(sc, PCIE_RC_LCR, reg);
reg = HREAD4(sc, PCIE_LINK_WIDTH_SPEED_CTRL);
reg |= PCIE_LINK_WIDTH_SPEED_CTRL_CHANGE;
HWRITE4(sc, PCIE_LINK_WIDTH_SPEED_CTRL, reg);
for (timo = 20000; timo > 0; timo--) {
if (dwpcie_link_up(sc))
break;
delay(10);
}
if (timo == 0) {
error = ETIMEDOUT;
goto err;
}
}
sc->sc_ih = fdt_intr_establish(sc->sc_node, IPL_AUDIO | IPL_MPSAFE,
dwpcie_imx8mq_intr, sc, sc->sc_dev.dv_xname);
/* Unmask INTx interrupts. */
HWRITE4(sc, PCIE_GLOBAL_INT_MASK,
PCIE_GLOBAL_INT_MASK_INT_A | PCIE_GLOBAL_INT_MASK_INT_B |
PCIE_GLOBAL_INT_MASK_INT_C | PCIE_GLOBAL_INT_MASK_INT_D);
error = 0;
err:
if (clkreq_gpiolen > 0)
free(clkreq_gpio, M_TEMP, clkreq_gpiolen);
if (disable_gpiolen > 0)
free(disable_gpio, M_TEMP, disable_gpiolen);
if (reset_gpiolen > 0)
free(reset_gpio, M_TEMP, reset_gpiolen);
return error;
}
int
dwpcie_imx8mq_intr(void *arg)
{
struct dwpcie_softc *sc = arg;
uint32_t cause;
/* Acknowledge interrupts. */
cause = HREAD4(sc, PCIE_GLOBAL_INT_CAUSE);
HWRITE4(sc, PCIE_GLOBAL_INT_CAUSE, cause);
/* INTx interrupt, so not really ours. */
return 0;
}
int
dwpcie_fu740_init(struct dwpcie_softc *sc)
{
sc->sc_num_viewport = 8;
return 0;
}
int
dwpcie_rk3568_init(struct dwpcie_softc *sc)
{
sc->sc_num_viewport = 8;
return 0;
}
int
dwpcie_sc8280xp_init(struct dwpcie_softc *sc)
{
sc->sc_num_viewport = 8;
return 0;
}
void
dwpcie_atu_write(struct dwpcie_softc *sc, int index, off_t reg,
uint32_t val)
{
if (sc->sc_atu_unroll) {
bus_space_write_4(sc->sc_iot, sc->sc_atu_ioh,
IATU_OFFSET_UNROLL(index) + reg, val);
return;
}
if (sc->sc_atu_viewport != index) {
HWRITE4(sc, IATU_VIEWPORT, index);
sc->sc_atu_viewport = index;
}
HWRITE4(sc, IATU_OFFSET_VIEWPORT + reg, val);
}
uint32_t
dwpcie_atu_read(struct dwpcie_softc *sc, int index, off_t reg)
{
if (sc->sc_atu_unroll) {
return bus_space_read_4(sc->sc_iot, sc->sc_atu_ioh,
IATU_OFFSET_UNROLL(index) + reg);
}
if (sc->sc_atu_viewport != index) {
HWRITE4(sc, IATU_VIEWPORT, index);
sc->sc_atu_viewport = index;
}
return HREAD4(sc, IATU_OFFSET_VIEWPORT + reg);
}
void
dwpcie_atu_disable(struct dwpcie_softc *sc, int index)
{
dwpcie_atu_write(sc, index, IATU_REGION_CTRL_2, 0);
}
void
dwpcie_atu_config(struct dwpcie_softc *sc, int index, int type,
uint64_t cpu_addr, uint64_t pci_addr, uint64_t size)
{
uint32_t reg;
int timo;
dwpcie_atu_write(sc, index, IATU_LWR_BASE_ADDR, cpu_addr);
dwpcie_atu_write(sc, index, IATU_UPPER_BASE_ADDR, cpu_addr >> 32);
dwpcie_atu_write(sc, index, IATU_LIMIT_ADDR, cpu_addr + size - 1);
dwpcie_atu_write(sc, index, IATU_LWR_TARGET_ADDR, pci_addr);
dwpcie_atu_write(sc, index, IATU_UPPER_TARGET_ADDR, pci_addr >> 32);
dwpcie_atu_write(sc, index, IATU_REGION_CTRL_1, type);
dwpcie_atu_write(sc, index, IATU_REGION_CTRL_2,
IATU_REGION_CTRL_2_REGION_EN);
for (timo = 5; timo > 0; timo--) {
reg = dwpcie_atu_read(sc, index, IATU_REGION_CTRL_2);
if (reg & IATU_REGION_CTRL_2_REGION_EN)
break;
delay(9000);
}
if (timo == 0)
printf("%s:%d: timeout\n", __func__, __LINE__);
}
int
dwpcie_link_up(struct dwpcie_softc *sc)
{
uint32_t reg;
reg = HREAD4(sc, PCIE_PHY_DEBUG_R1);
if ((reg & PCIE_PHY_DEBUG_R1_XMLH_LINK_UP) != 0 &&
(reg & PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING) == 0)
return 1;
return 0;
}
void
dwpcie_attach_hook(struct device *parent, struct device *self,
struct pcibus_attach_args *pba)
{
}
int
dwpcie_bus_maxdevs(void *v, int bus)
{
struct dwpcie_softc *sc = v;
if (bus == sc->sc_bus || bus == sc->sc_bus + 1)
return 1;
return 32;
}
pcitag_t
dwpcie_make_tag(void *v, int bus, int device, int function)
{
return ((bus << 24) | (device << 19) | (function << 16));
}
void
dwpcie_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
{
if (bp != NULL)
*bp = (tag >> 24) & 0xff;
if (dp != NULL)
*dp = (tag >> 19) & 0x1f;
if (fp != NULL)
*fp = (tag >> 16) & 0x7;
}
int
dwpcie_conf_size(void *v, pcitag_t tag)
{
return PCIE_CONFIG_SPACE_SIZE;
}
pcireg_t
dwpcie_conf_read(void *v, pcitag_t tag, int reg)
{
struct dwpcie_softc *sc = v;
int bus, dev, fn;
uint32_t ret;
dwpcie_decompose_tag(sc, tag, &bus, &dev, &fn);
if (bus == sc->sc_bus) {
KASSERT(dev == 0);
return HREAD4(sc, tag | reg);
}
if (bus == sc->sc_bus + 1) {
dwpcie_atu_config(sc, IATU_VIEWPORT_INDEX1,
IATU_REGION_CTRL_1_TYPE_CFG0,
sc->sc_conf_base, tag, sc->sc_conf_size);
} else {
dwpcie_atu_config(sc, IATU_VIEWPORT_INDEX1,
IATU_REGION_CTRL_1_TYPE_CFG1,
sc->sc_conf_base, tag, sc->sc_conf_size);
}
ret = bus_space_read_4(sc->sc_iot, sc->sc_conf_ioh, reg);
if (sc->sc_num_viewport <= 2 && sc->sc_io_size > 0) {
dwpcie_atu_config(sc, IATU_VIEWPORT_INDEX1,
IATU_REGION_CTRL_1_TYPE_IO, sc->sc_io_base,
sc->sc_io_bus_addr, sc->sc_io_size);
}
return ret;
}
void
dwpcie_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
{
struct dwpcie_softc *sc = v;
int bus, dev, fn;
dwpcie_decompose_tag(sc, tag, &bus, &dev, &fn);
if (bus == sc->sc_bus) {
KASSERT(dev == 0);
HWRITE4(sc, tag | reg, data);
return;
}
if (bus == sc->sc_bus + 1) {
dwpcie_atu_config(sc, IATU_VIEWPORT_INDEX1,
IATU_REGION_CTRL_1_TYPE_CFG0,
sc->sc_conf_base, tag, sc->sc_conf_size);
} else {
dwpcie_atu_config(sc, IATU_VIEWPORT_INDEX1,
IATU_REGION_CTRL_1_TYPE_CFG1,
sc->sc_conf_base, tag, sc->sc_conf_size);
}
bus_space_write_4(sc->sc_iot, sc->sc_conf_ioh, reg, data);
if (sc->sc_num_viewport <= 2 && sc->sc_io_size > 0) {
dwpcie_atu_config(sc, IATU_VIEWPORT_INDEX1,
IATU_REGION_CTRL_1_TYPE_IO, sc->sc_io_base,
sc->sc_io_bus_addr, sc->sc_io_size);
}
}
int
dwpcie_probe_device_hook(void *v, struct pci_attach_args *pa)
{
struct dwpcie_softc *sc = v;
uint16_t rid;
int i;
rid = pci_requester_id(pa->pa_pc, pa->pa_tag);
pa->pa_dmat = iommu_device_map_pci(sc->sc_node, rid, pa->pa_dmat);
for (i = 0; i < sc->sc_nranges; i++) {
iommu_reserve_region_pci(sc->sc_node, rid,
sc->sc_ranges[i].pci_base, sc->sc_ranges[i].size);
}
return 0;
}
int
dwpcie_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
{
int pin = pa->pa_rawintrpin;
if (pin == 0 || pin > PCI_INTERRUPT_PIN_MAX)
return -1;
if (pa->pa_tag == 0)
return -1;
ihp->ih_pc = pa->pa_pc;
ihp->ih_tag = pa->pa_intrtag;
ihp->ih_intrpin = pa->pa_intrpin;
ihp->ih_type = PCI_INTX;
return 0;
}
const char *
dwpcie_intr_string(void *v, pci_intr_handle_t ih)
{
switch (ih.ih_type) {
case PCI_MSI:
return "msi";
case PCI_MSIX:
return "msix";
}
return "intx";
}
struct dwpcie_msi *
dwpcie_msi_establish(struct dwpcie_softc *sc, int level,
int (*func)(void *), void *arg, char *name)
{
struct dwpcie_msi *dm;
int vec;
for (vec = 0; vec < DWPCIE_NUM_MSI; vec++) {
dm = &sc->sc_msi[vec];
if (dm->dm_func == NULL)
break;
}
if (vec == DWPCIE_NUM_MSI)
return NULL;
dm->dm_func = func;
dm->dm_arg = arg;
dm->dm_ipl = level & IPL_IRQMASK;
dm->dm_flags = level & IPL_FLAGMASK;
dm->dm_vec = vec;
dm->dm_name = name;
if (name != NULL)
evcount_attach(&dm->dm_count, name, &dm->dm_vec);
/* Unmask the MSI. */
HCLR4(sc, PCIE_MSI_INTR0_MASK, (1U << vec));
return dm;
}
void
dwpcie_msi_disestablish(struct dwpcie_softc *sc, struct dwpcie_msi *dm)
{
/* Mask the MSI. */
HSET4(sc, PCIE_MSI_INTR0_MASK, (1U << dm->dm_vec));
if (dm->dm_name)
evcount_detach(&dm->dm_count);
dm->dm_func = NULL;
}
void *
dwpcie_intr_establish(void *v, pci_intr_handle_t ih, int level,
struct cpu_info *ci, int (*func)(void *), void *arg, char *name)
{
struct dwpcie_softc *sc = v;
struct dwpcie_intr_handle *pih;
void *cookie = NULL;
KASSERT(ih.ih_type != PCI_NONE);
if (ih.ih_type != PCI_INTX) {
struct dwpcie_msi *dm = NULL;
bus_dma_tag_t dmat = ih.ih_dmat;
bus_dma_segment_t seg;
bus_dmamap_t map;
uint64_t addr, data;
if (sc->sc_msi_addr) {
dm = dwpcie_msi_establish(sc, level, func, arg, name);
if (dm == NULL)
return NULL;
addr = sc->sc_msi_addr;
data = dm->dm_vec;
} else {
/*
* Assume hardware passes Requester ID as
* sideband data.
*/
data = pci_requester_id(ih.ih_pc, ih.ih_tag);
cookie = fdt_intr_establish_msi_cpu(sc->sc_node, &addr,
&data, level, ci, func, arg, (void *)name);
if (cookie == NULL)
return NULL;
}
pih = malloc(sizeof(*pih), M_DEVBUF, M_WAITOK | M_ZERO);
pih->pih_ih.ih_ic = &dwpcie_ic;
pih->pih_ih.ih_ih = cookie;
pih->pih_sc = sc;
pih->pih_dm = dm;
if (sc->sc_msi_addr == 0) {
if (bus_dmamap_create(dmat, sizeof(uint32_t), 1,
sizeof(uint32_t), 0, BUS_DMA_WAITOK, &map)) {
free(pih, M_DEVBUF, sizeof(*pih));
fdt_intr_disestablish(cookie);
return NULL;
}
memset(&seg, 0, sizeof(seg));
seg.ds_addr = addr;
seg.ds_len = sizeof(uint32_t);
if (bus_dmamap_load_raw(dmat, map, &seg, 1,
sizeof(uint32_t), BUS_DMA_WAITOK)) {
bus_dmamap_destroy(dmat, map);
free(pih, M_DEVBUF, sizeof(*pih));
fdt_intr_disestablish(cookie);
return NULL;
}
addr = map->dm_segs[0].ds_addr;
pih->pih_dmat = dmat;
pih->pih_map = map;
}
if (ih.ih_type == PCI_MSIX) {
pci_msix_enable(ih.ih_pc, ih.ih_tag,
&sc->sc_bus_memt, ih.ih_intrpin, addr, data);
} else
pci_msi_enable(ih.ih_pc, ih.ih_tag, addr, data);
} else {
int bus, dev, fn;
uint32_t reg[4];
dwpcie_decompose_tag(sc, ih.ih_tag, &bus, &dev, &fn);
reg[0] = bus << 16 | dev << 11 | fn << 8;
reg[1] = reg[2] = 0;
reg[3] = ih.ih_intrpin;
cookie = fdt_intr_establish_imap_cpu(sc->sc_node, reg,
sizeof(reg), level, ci, func, arg, name);
if (cookie == NULL)
return NULL;
pih = malloc(sizeof(*pih), M_DEVBUF, M_WAITOK | M_ZERO);
pih->pih_ih.ih_ic = &dwpcie_ic;
pih->pih_ih.ih_ih = cookie;
}
return pih;
}
void
dwpcie_intr_disestablish(void *v, void *cookie)
{
struct dwpcie_intr_handle *pih = cookie;
if (pih->pih_dm)
dwpcie_msi_disestablish(pih->pih_sc, pih->pih_dm);
else
fdt_intr_disestablish(pih->pih_ih.ih_ih);
if (pih->pih_dmat) {
bus_dmamap_unload(pih->pih_dmat, pih->pih_map);
bus_dmamap_destroy(pih->pih_dmat, pih->pih_map);
}
free(pih, M_DEVBUF, sizeof(*pih));
}
int
dwpcie_bs_iomap(bus_space_tag_t t, bus_addr_t addr, bus_size_t size,
int flags, bus_space_handle_t *bshp)
{
struct dwpcie_softc *sc = t->bus_private;
int i;
for (i = 0; i < sc->sc_nranges; i++) {
uint64_t pci_start = sc->sc_ranges[i].pci_base;
uint64_t pci_end = pci_start + sc->sc_ranges[i].size;
uint64_t phys_start = sc->sc_ranges[i].phys_base;
if ((sc->sc_ranges[i].flags & 0x03000000) == 0x01000000 &&
addr >= pci_start && addr + size <= pci_end) {
return bus_space_map(sc->sc_iot,
addr - pci_start + phys_start, size, flags, bshp);
}
}
return ENXIO;
}
int
dwpcie_bs_memmap(bus_space_tag_t t, bus_addr_t addr, bus_size_t size,
int flags, bus_space_handle_t *bshp)
{
struct dwpcie_softc *sc = t->bus_private;
int i;
for (i = 0; i < sc->sc_nranges; i++) {
uint64_t pci_start = sc->sc_ranges[i].pci_base;
uint64_t pci_end = pci_start + sc->sc_ranges[i].size;
uint64_t phys_start = sc->sc_ranges[i].phys_base;
if ((sc->sc_ranges[i].flags & 0x02000000) == 0x02000000 &&
addr >= pci_start && addr + size <= pci_end) {
return bus_space_map(sc->sc_iot,
addr - pci_start + phys_start, size, flags, bshp);
}
}
return ENXIO;
}
|