summaryrefslogtreecommitdiff
path: root/bsp/rockchip/common/rk_hal/lib/hal/src/hal_pl330.c
blob: c01849ddd58df2488077caa90e21be6314ce0a5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
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
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
/* SPDX-License-Identifier: BSD-3-Clause */
/*
 * Copyright (c) 2020-2021 Rockchip Electronics Co., Ltd.
 */

#include "hal_base.h"

#ifdef HAL_PL330_MODULE_ENABLED

/** @addtogroup RK_HAL_Driver
 *  @{
 */

/** @addtogroup PL330
 *  @{
 */

/** @defgroup PL330_How_To_Use How To Use
 *  @{

 The PL330 driver can be used as follows:

 - Invoke HAL_PL330_Init to initialize pl330.
 - Invoke HAL_PL330_RequestChannel to request a available dma channel.
 - Invoke HAL_PL330_Config to config dma transfer config.
 - Invoke HAL_PL330_PrepDmaSingle/Cyclic for single/cyclic transfer.
 - Invoke HAL_PL330_Start to start a ready dma transfer.
 - Invoke HAL_PL330_Stop to stop the dma channel.
 - Invoke HAL_PL330_ReleaseChannel to release the dma channel.
 - Invoke HAL_PL330_DeInit to deinitialize pl330.
 - More details refer to APIs' descriptions as below.

 @} */

/** @defgroup PL330_Private_Definition Private Definition
 *  @{
 */
/********************* Private MACRO Definition ******************************/

#ifdef PL330_DEBUG_MCGEN
static uint32_t s_cmdLine;
#define PL330_DBGCMD_DUMP(off, x...) \
    do {                             \
        HAL_DBG("%lx:", s_cmdLine);  \
        HAL_DBG(x);                  \
        s_cmdLine += off;            \
    } while (0)
#define PL330_DBGMC_START(addr) (s_cmdLine = addr)
#else
#define PL330_DBGCMD_DUMP(off, x...) \
    do {                             \
    } while (0)
#define PL330_DBGMC_START(addr) \
    do {                        \
    } while (0)
#endif

#ifndef PL330_MAX_WAIT
#define PL330_MAX_WAIT 4000
#endif

#define DMAC_MODE_NS HAL_BIT(0)

#define DS_ST_STOP   0x0
#define DS_ST_EXEC   0x1
#define DS_ST_CMISS  0x2
#define DS_ST_UPDTPC 0x3
#define DS_ST_WFE    0x4
#define DS_ST_ATBRR  0x5
#define DS_ST_QBUSY  0x6
#define DS_ST_WFP    0x7
#define DS_ST_KILL   0x8
#define DS_ST_CMPLT  0x9
#define DS_ST_FLTCMP 0xe
#define DS_ST_FAULT  0xf

#define CC_SRCINC           HAL_BIT(0)
#define CC_DSTINC           HAL_BIT(14)
#define CC_SRCPRI           HAL_BIT(8)
#define CC_DSTPRI           HAL_BIT(22)
#define CC_SRCNS            HAL_BIT(9)
#define CC_DSTNS            HAL_BIT(23)
#define CC_SRCIA            HAL_BIT(10)
#define CC_DSTIA            HAL_BIT(24)
#define CC_SRCBRSTLEN_SHFT  4
#define CC_DSTBRSTLEN_SHFT  18
#define CC_SRCBRSTSIZE_SHFT 1
#define CC_DSTBRSTSIZE_SHFT 15
#define CC_SRCCCTRL_SHFT    11
#define CC_SRCCCTRL_MASK    0x7
#define CC_DSTCCTRL_SHFT    25
#define CC_DRCCCTRL_MASK    0x7
#define CC_SWAP_SHFT        28

#define CR0_PERIPH_REQ_SET   HAL_BIT(0)
#define CR0_BOOT_EN_SET      HAL_BIT(1)
#define CR0_BOOT_MAN_NS      HAL_BIT(2)
#define CR0_NUM_CHANS_SHIFT  4
#define CR0_NUM_CHANS_MASK   0x7
#define CR0_NUM_PERIPH_SHIFT 12
#define CR0_NUM_PERIPH_MASK  0x1f
#define CR0_NUM_EVENTS_SHIFT 17
#define CR0_NUM_EVENTS_MASK  0x1f

#define CR1_ICACHE_LEN_SHIFT      0
#define CR1_ICACHE_LEN_MASK       0x7
#define CR1_NUM_ICACHELINES_SHIFT 4
#define CR1_NUM_ICACHELINES_MASK  0xf

#define CRD_DATA_WIDTH_SHIFT 0
#define CRD_DATA_WIDTH_MASK  0x7
#define CRD_WR_CAP_SHIFT     4
#define CRD_WR_CAP_MASK      0x7
#define CRD_WR_Q_DEP_SHIFT   8
#define CRD_WR_Q_DEP_MASK    0xf
#define CRD_RD_CAP_SHIFT     12
#define CRD_RD_CAP_MASK      0x7
#define CRD_RD_Q_DEP_SHIFT   16
#define CRD_RD_Q_DEP_MASK    0xf
#define CRD_DATA_BUFF_SHIFT  20
#define CRD_DATA_BUFF_MASK   0x3ff

#define PART          0x330
#define DESIGNER      0x41
#define REVISION      0x0
#define INTEG_CFG     0x0
#define PERIPH_ID_VAL ((PART << 0) | (DESIGNER << 12))

#define PL330_DS_DMA_STATUS         0x0f
#define PL330_DS_DMA_STATUS_STOPPED 0x00
#define PL330_DBGSTATUS_BUSY        0x01
#define PL330_CS_ACTIVE_MASK        0x07
#define PL330_CR1_I_CACHE_LEN_MASK  0x07
#define PL330_INTCLR_ALL_MASK       0xff

#define CMD_DMAADDH   0x54
#define CMD_DMAEND    0x00
#define CMD_DMAFLUSHP 0x35
#define CMD_DMAGO     0xa0
#define CMD_DMALD     0x04
#define CMD_DMALDP    0x25
#define CMD_DMALP     0x20
#define CMD_DMALPEND  0x28
#define CMD_DMAKILL   0x01
#define CMD_DMAMOV    0xbc
#define CMD_DMANOP    0x18
#define CMD_DMARMB    0x12
#define CMD_DMASEV    0x34
#define CMD_DMAST     0x08
#define CMD_DMASTP    0x29
#define CMD_DMASTZ    0x0c
#define CMD_DMAWFE    0x36
#define CMD_DMAWFP    0x30
#define CMD_DMAWMB    0x13

#define SZ_DMAADDH   3
#define SZ_DMAEND    1
#define SZ_DMAFLUSHP 2
#define SZ_DMALD     1
#define SZ_DMALDP    2
#define SZ_DMALP     2
#define SZ_DMALPEND  2
#define SZ_DMAKILL   1
#define SZ_DMAMOV    6
#define SZ_DMANOP    1
#define SZ_DMARMB    1
#define SZ_DMASEV    2
#define SZ_DMAST     1
#define SZ_DMASTP    2
#define SZ_DMASTZ    1
#define SZ_DMAWFE    2
#define SZ_DMAWFP    2
#define SZ_DMAWMB    1
#define SZ_DMAGO     6

#define BRST_LEN(ccr)  ((((ccr) >> CC_SRCBRSTLEN_SHFT) & 0xf) + 1)
#define BRST_SIZE(ccr) (1 << (((ccr) >> CC_SRCBRSTSIZE_SHFT) & 0x7))

#define BYTE_TO_BURST(b, ccr)      ((b) / BRST_SIZE(ccr) / BRST_LEN(ccr))
#define BURST_TO_BYTE(c, ccr)      ((c) * BRST_SIZE(ccr) * BRST_LEN(ccr))
#define BYTE_MOD_BURST_LEN(b, ccr) (((b) / BRST_SIZE(ccr)) % BRST_LEN(ccr))

/*
 * PL330_DBGINST0 - constructs the word for the Debug Instruction-0 Register.
 * @b1: Instruction byte 1
 * @b0: Instruction byte 0
 * @ch: channel number
 * @dbg_th: Debug thread encoding: 0 = DMA manager thread, 1 = DMA channel
 */
#define PL330_DBGINST0(b1, b0, ch, dbg_th) \
    (((b1) << 24) | ((b0) << 16) | (((ch) & 0x7) << 8) | ((dbg_th & 0x1)))

/********************* Private Structure Definition **************************/

typedef enum {
    SAR = 0,
    CCR,
    DAR,
} eDMAMOV_DST;

typedef enum {
    SRC = 0,
    DST,
} ePL330_DST;

/********************* Private Variable Definition ***************************/

/********************* Private Function Definition ***************************/

__STATIC_INLINE int PL330_Instr_DMAADDH(uint8_t dryRun, char *buf,
                                        ePL330_DST da, uint16_t val)
{
    if (dryRun) {
        return SZ_DMAADDH;
    }

    *buf = CMD_DMAADDH;
    *buf |= (da << 1);
    *((uint16_t *)(buf + 1)) = val;

    PL330_DBGCMD_DUMP(SZ_DMAADDH, "\tDMAADDH %s %u\n",
                      da == DST ? "DA" : "SA", val);

    return SZ_DMAADDH;
}

__STATIC_INLINE int PL330_Instr_DMAEND(uint8_t dryRun, char *buf)
{
    if (dryRun) {
        return SZ_DMAEND;
    }
    /*
     * DMAEND encoding:
     * 7 6 5 4 3 2 1 0
     * 0 0 0 0 0 0 0 0
     */
    *buf = CMD_DMAEND;

    PL330_DBGCMD_DUMP(SZ_DMAEND, "\tDMAEND\n");

    return SZ_DMAEND;
}

__STATIC_INLINE void PL330_Memcpy4(char *dst, char *src)
{
    *dst = *src;
    *(dst + 1) = *(src + 1);
    *(dst + 2) = *(src + 2);
    *(dst + 3) = *(src + 3);
}

__STATIC_INLINE int PL330_Instr_DMAGO(uint8_t dryRun, char *buf, uint8_t cn,
                                      uint32_t imm, uint8_t ns)
{
    if (dryRun) {
        return SZ_DMAGO;
    }
    /*
     * DMAGO encoding:
     * 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
     *  0  0  0  0  0 |cn[2:0]| 1  0  1  0  0  0 ns  0
     *
     * 47 ... 16
     *  imm[32:0]
     */
    *buf = CMD_DMAGO | ((ns << 1) & 0x02);

    *(buf + 1) = (uint8_t)(cn & 0x07);

    PL330_Memcpy4(buf + 2, (char *)&imm);

    return SZ_DMAGO;
}

__STATIC_INLINE int PL330_Instr_DMALP(uint8_t dryRun, char *buf, uint8_t lc,
                                      uint16_t loops)
{
    if (dryRun) {
        return SZ_DMALP;
    }
    /*
     * DMALP encoding
     * 15   ...   8 7 6 5 4 3 2 1  0
     * | iter[7:0] |0 0 1 0 0 0 lc 0
     */
    *buf = (uint8_t)(CMD_DMALP | ((lc & 1) << 1));
    *(buf + 1) = (uint8_t)(loops - 1);

    PL330_DBGCMD_DUMP(SZ_DMALP, "\tDMALP_%c %u\n", lc ? '1' : '0', loops - 1);

    return SZ_DMALP;
}

__STATIC_INLINE int PL330_Instr_DMAMOV(uint8_t dryRun, char *buf, uint8_t rd,
                                       uint32_t imm)
{
    if (dryRun) {
        return SZ_DMAMOV;
    }
    /*
     * DMAMOV encoding
     * 15 4 3 2 1 10 ... 8 7 6 5 4 3 2 1 0
     *  0 0 0 0 0 |rd[2:0]|1 0 1 1 1 1 0 0
     *
     * 47 ... 16
     *  imm[32:0]
     *
     * rd: b000 for SAR, b001 CCR, b010 DAR
     */
    *buf = CMD_DMAMOV;
    *(buf + 1) = rd & 0x7;
    PL330_Memcpy4(buf + 2, (char *)&imm);

    PL330_DBGCMD_DUMP(SZ_DMAMOV, "\tDMAMOV %s 0x%lx\n",
                      rd == SAR ? "SAR" : (rd == DAR ? "DAR" : "CCR"), imm);

    return SZ_DMAMOV;
}

HAL_UNUSED __STATIC_INLINE int PL330_Instr_DMANOP(uint8_t dryRun, char *buf)
{
    if (dryRun) {
        return SZ_DMANOP;
    }
    /*
     * DMANOP encoding
     * 7 6 5 4 3 2 1 0
     * 0 0 0 1 1 0 0 0
     */
    *buf = CMD_DMANOP;

    PL330_DBGCMD_DUMP(SZ_DMANOP, "\tDMANOP\n");

    return SZ_DMANOP;
}

HAL_UNUSED __STATIC_INLINE int PL330_Instr_DMARMB(uint8_t dryRun, char *buf)
{
    if (dryRun) {
        return SZ_DMARMB;
    }
    /*
     * DMARMB encoding
     * 7 6 5 4 3 2 1 0
     * 0 0 0 1 0 0 1 0
     */
    *buf = CMD_DMARMB;

    PL330_DBGCMD_DUMP(SZ_DMARMB, "\tDMARMB\n");

    return SZ_DMARMB;
}

__STATIC_INLINE int PL330_Instr_DMASEV(uint8_t dryRun, char *buf, uint8_t event)
{
    if (dryRun) {
        return SZ_DMASEV;
    }
    /*
     * DMASEV encoding
     * 15 4 3 2 1  10 9 8 7 6 5 4 3 2 1 0
     * |event[4:0]| 0 0 0 0 0 1 1 0 1 0 0
     */
    *buf = CMD_DMASEV;
    *(buf + 1) = (uint8_t)(event << 3);

    PL330_DBGCMD_DUMP(SZ_DMASEV, "\tDMASEV %u\n", event);

    return SZ_DMASEV;
}

HAL_UNUSED __STATIC_INLINE int PL330_Instr_DMAWMB(uint8_t dryRun, char *buf)
{
    if (dryRun) {
        return SZ_DMAWMB;
    }
    /*
     * DMAWMB encoding
     * 7 6 5 4 3 2 1 0
     * 0 0 0 1 0 0 1 0
     */
    *buf = CMD_DMAWMB;

    PL330_DBGCMD_DUMP(SZ_DMAWMB, "\tDMAWMB\n");

    return SZ_DMAWMB;
}

__STATIC_INLINE int PL330_Instr_DMAFLUSHP(uint8_t dryRun, char *buf,
                                          uint8_t peri)
{
    if (dryRun) {
        return SZ_DMAFLUSHP;
    }
    /*
     * DMAFLUSHP encoding
     * 15 4 3 2 1  10 9 8 7 6 5 4 3 2 1 0
     * |perip[4:0]| 0 0 0 0 0 1 1 0 1 0 1
     */
    *buf = CMD_DMAFLUSHP;

    peri &= 0x1f;
    peri <<= 3;
    *(buf + 1) = peri;

    PL330_DBGCMD_DUMP(SZ_DMAFLUSHP, "\tDMAFLUSHP %u\n", peri >> 3);

    return SZ_DMAFLUSHP;
}

__STATIC_INLINE int PL330_Instr_DMALD(uint8_t dryRun, char *buf,
                                      ePL330_COND cond)
{
    if (dryRun) {
        return SZ_DMALD;
    }
    /*
     * DMALD encoding
     * 7 6 5 4 3 2 1  0
     * 0 0 0 0 0 1 bs x
     */
    *buf = CMD_DMALD;

    if (cond == SINGLE) {
        *buf |= (0 << 1) | (1 << 0);
    } else if (cond == BURST) {
        *buf |= (1 << 1) | (1 << 0);
    }

    PL330_DBGCMD_DUMP(SZ_DMALD, "\tDMALD%c\n",
                      cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));

    return SZ_DMALD;
}

__STATIC_INLINE int PL330_Instr_DMALDP(uint8_t dryRun, char *buf,
                                       ePL330_COND cond, uint8_t peri)
{
    if (dryRun) {
        return SZ_DMALDP;
    }
    /*
     * DMALDP encoding
     * 15 4 3 2 1  10 9 8 7 6 5 4 3 2 1  0
     * |perip[4:0]| 0 0 0 0 0 1 0 0 1 bs 1
     */
    *buf = CMD_DMALDP;

    if (cond == BURST) {
        *buf |= (1 << 1);
    }

    peri &= 0x1f;
    peri <<= 3;
    *(buf + 1) = peri;

    PL330_DBGCMD_DUMP(SZ_DMALDP, "\tDMALDP%c %u\n", cond == SINGLE ? 'S' : 'B',
                      peri >> 3);

    return SZ_DMALDP;
}

__STATIC_INLINE int PL330_Instr_DMALPEND(uint8_t dryRun, char *buf,
                                         ePL330_COND cond, bool forever,
                                         uint32_t loop, uint8_t bjump)
{
    if (dryRun) {
        return SZ_DMALPEND;
    }
    /*
     * DMALPEND encoding
     * 15       ...        8 7 6 5 4  3 2  1  0
     * | backward_jump[7:0] |0 0 1 nf 1 lc bs x
     */
    *buf = CMD_DMALPEND;

    if (loop) {
        *buf |= (1 << 2);
    }

    if (!forever) {
        *buf |= (1 << 4);
    }

    if (cond == SINGLE) {
        *buf |= (0 << 1) | (1 << 0);
    } else if (cond == BURST) {
        *buf |= (1 << 1) | (1 << 0);
    }

    *(buf + 1) = bjump;

    PL330_DBGCMD_DUMP(SZ_DMALPEND, "\tDMALP%s%c_%c bjmpto_%x\n",
                      forever ? "FE" : "END",
                      cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'),
                      loop ? '1' : '0', bjump);

    return SZ_DMALPEND;
}

HAL_UNUSED __STATIC_INLINE int PL330_Instr_DMAKILL(uint8_t dryRun, char *buf)
{
    if (dryRun) {
        return SZ_DMAKILL;
    }
    /*
     * DMAKILL encoding
     * 7 6 5 4 3 2 1 0
     * 0 0 0 0 0 0 0 1
     */
    *buf = CMD_DMAKILL;

    return SZ_DMAKILL;
}

__STATIC_INLINE int PL330_Instr_DMAST(uint8_t dryRun, char *buf,
                                      ePL330_COND cond)
{
    if (dryRun) {
        return SZ_DMAST;
    }
    /*
     * DMAST encoding
     * 7 6 5 4 3 2 1  0
     * 0 0 0 0 1 0 bs x
     */
    *buf = CMD_DMAST;

    if (cond == SINGLE) {
        *buf |= (0 << 1) | (1 << 0);
    } else if (cond == BURST) {
        *buf |= (1 << 1) | (1 << 0);
    }

    PL330_DBGCMD_DUMP(SZ_DMAST, "\tDMAST%c\n",
                      cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'));

    return SZ_DMAST;
}

__STATIC_INLINE int PL330_Instr_DMASTP(uint8_t dryRun, char *buf,
                                       ePL330_COND cond, uint8_t peri)
{
    if (dryRun) {
        return SZ_DMASTP;
    }
    /*
     * DMASTP encoding
     * 15 4 3 2 1  10 9 8 7 6 5 4 3 2 1  0
     * |perip[4:0]| 0 0 0 0 0 1 0 1 0 bs 1
     */
    *buf = CMD_DMASTP;

    if (cond == BURST) {
        *buf |= (1 << 1);
    }

    peri &= 0x1f;
    peri <<= 3;
    *(buf + 1) = peri;

    PL330_DBGCMD_DUMP(SZ_DMASTP, "\tDMASTP%c %u\n", cond == SINGLE ? 'S' : 'B',
                      peri >> 3);

    return SZ_DMASTP;
}

HAL_UNUSED __STATIC_INLINE int PL330_Instr_DMASTZ(uint8_t dryRun, char *buf)
{
    if (dryRun) {
        return SZ_DMASTZ;
    }
    /*
     * DMASTZ encoding
     * 7 6 5 4 3 2 1 0
     * 0 0 0 1 1 1 0 0
     */
    *buf = CMD_DMASTZ;

    PL330_DBGCMD_DUMP(SZ_DMASTZ, "\tDMASTZ\n");

    return SZ_DMASTZ;
}

HAL_UNUSED __STATIC_INLINE int PL330_Instr_DMAWFE(uint8_t dryRun, char *buf, uint8_t ev,
                                                  uint32_t invalidate)
{
    if (dryRun) {
        return SZ_DMAWFE;
    }
    /*
     * DMAWFE encoding
     * 15 4 3 2 1  10 9 8 7 6 5 4 3 2 1 0
     * |event[4:0]| 0 i 0 0 0 1 1 0 1 1 0
     */
    *buf = CMD_DMAWFE;

    ev &= 0x1f;
    ev <<= 3;
    *(buf + 1) = ev;

    if (invalidate) {
        *(buf + 1) |= (1 << 1);
    }

    PL330_DBGCMD_DUMP(SZ_DMAWFE, "\tDMAWFE %u%s\n", ev >> 3,
                      invalidate ? ", I" : "");

    return SZ_DMAWFE;
}

__STATIC_INLINE int PL330_Instr_DMAWFP(uint8_t dryRun, char *buf,
                                       ePL330_COND cond, uint8_t peri)
{
    if (dryRun) {
        return SZ_DMAWFP;
    }
    /*
     * DMAWFP encoding
     * 15 4 3 2 1  10 9 8 7 6 5 4 3 2 1  0
     * |perip[4:0]| 0 0 0 0 0 1 1 0 0 bs p
     */
    *buf = CMD_DMAWFP;

    if (cond == SINGLE) {
        *buf |= (0 << 1) | (0 << 0);
    } else if (cond == BURST) {
        *buf |= (1 << 1) | (0 << 0);
    } else {
        *buf |= (0 << 1) | (1 << 0);
    }

    peri &= 0x1f;
    peri <<= 3;
    *(buf + 1) = peri;

    PL330_DBGCMD_DUMP(SZ_DMAWFP, "\tDMAWFP%c %u\n",
                      cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'P'),
                      peri >> 3);

    return SZ_DMAWFP;
}

static int _LDST_MemToMem(uint8_t dryRun, char *buf, int cyc)
{
    int off = 0;

    while (cyc--) {
        off += PL330_Instr_DMALD(dryRun, &buf[off], ALWAYS);
        off += PL330_Instr_DMAST(dryRun, &buf[off], ALWAYS);
    }

    return off;
}

static int _LDST_DevToMem(uint8_t dryRun, struct HAL_PL330_DEV *pl330,
                          char *buf, struct PL330_XFER_SPEC *pxs, int cyc)
{
    int off = 0;
    ePL330_COND cond = pl330->peripReqType;

    while (cyc--) {
        off += PL330_Instr_DMAWFP(dryRun, &buf[off], cond, pxs->desc->peri);
        off += PL330_Instr_DMALDP(dryRun, &buf[off], cond, pxs->desc->peri);
        off += PL330_Instr_DMAST(dryRun, &buf[off], ALWAYS);
        off += PL330_Instr_DMAFLUSHP(dryRun, &buf[off], pxs->desc->peri);
        if (pxs->desc->dstInterlaceSize) {
            off += PL330_Instr_DMAADDH(dryRun, &buf[off], DST,
                                       pxs->desc->dstInterlaceSize);
        }
    }

    return off;
}

static int _LDST_MemToDev(uint8_t dryRun, struct HAL_PL330_DEV *pl330,
                          char *buf, struct PL330_XFER_SPEC *pxs, int cyc)
{
    int off = 0;
    ePL330_COND cond = pl330->peripReqType;

    while (cyc--) {
        off += PL330_Instr_DMAWFP(dryRun, &buf[off], cond, pxs->desc->peri);
        off += PL330_Instr_DMALD(dryRun, &buf[off], ALWAYS);
        off += PL330_Instr_DMASTP(dryRun, &buf[off], cond, pxs->desc->peri);
        off += PL330_Instr_DMAFLUSHP(dryRun, &buf[off], pxs->desc->peri);
        if (pxs->desc->srcInterlaceSize) {
            off += PL330_Instr_DMAADDH(dryRun, &buf[off], SRC,
                                       pxs->desc->srcInterlaceSize);
        }
    }

    return off;
}

static int _Bursts(uint8_t dryRun, struct HAL_PL330_DEV *pl330,
                   char *buf, struct PL330_XFER_SPEC *pxs, int cyc)
{
    int off = 0;

    switch (pxs->desc->dir) {
    case DMA_MEM_TO_DEV:
        off += _LDST_MemToDev(dryRun, pl330, &buf[off], pxs, cyc);
        break;
    case DMA_DEV_TO_MEM:
        off += _LDST_DevToMem(dryRun, pl330, &buf[off], pxs, cyc);
        break;
    case DMA_MEM_TO_MEM:
        off += _LDST_MemToMem(dryRun, &buf[off], cyc);
        break;
    default:
        off += 0x40000000; /* Scare off the Client */
        break;
    }

    return off;
}

/* Returns bytes consumed and updates bursts */
static int _Loop(uint8_t dryRun, struct HAL_PL330_DEV *pl330,
                 char *buf, unsigned long *bursts,
                 struct PL330_XFER_SPEC *pxs)
{
    int cyc, cycmax, szlp, szlpend, szbrst, off;
    uint32_t lcnt0, lcnt1, ljmp0, ljmp1;

    if (*bursts == 1) {
        return _Bursts(dryRun, pl330, buf, pxs, 1);
    }

    /* Max iterations possible in DMALP is 256 */
    if (*bursts >= 256 * 256) {
        lcnt1 = 256;
        lcnt0 = 256;
        cyc = *bursts / lcnt1 / lcnt0;
    } else if (*bursts > 256) {
        lcnt1 = 256;
        lcnt0 = *bursts / lcnt1;
        cyc = 1;
    } else {
        lcnt1 = *bursts;
        lcnt0 = 0;
        cyc = 1;
    }

    szlp = PL330_Instr_DMALP(1, buf, 0, 0);
    szbrst = _Bursts(1, pl330, buf, pxs, 1);
    szlpend = PL330_Instr_DMALPEND(1, buf, ALWAYS, false, 0, 0);

    if (lcnt0) {
        szlp *= 2;
        szlpend *= 2;
    }

    /*
     * Max bursts that we can unroll due to limit on the
     * size of backward jump that can be encoded in DMALPEND
     * which is 8-bits and hence 255
     */
    cycmax = (255 - (szlp + szlpend)) / szbrst;
    cyc = (cycmax < cyc) ? cycmax : cyc;
    off = 0;

    if (lcnt0) {
        off += PL330_Instr_DMALP(dryRun, &buf[off], 0, lcnt0);
        ljmp0 = off;
    }

    off += PL330_Instr_DMALP(dryRun, &buf[off], 1, lcnt1);
    ljmp1 = off;

    off += _Bursts(dryRun, pl330, &buf[off], pxs, cyc);

    off +=
        PL330_Instr_DMALPEND(dryRun, &buf[off], ALWAYS, false, 1, off - ljmp1);

    if (lcnt0) {
        off += PL330_Instr_DMALPEND(dryRun, &buf[off], ALWAYS, false, 0,
                                    off - ljmp0);
    }
    *bursts = lcnt1 * cyc;
    if (lcnt0) {
        *bursts *= lcnt0;
    }

    return off;
}

static int _Period(uint8_t dryRun, struct HAL_PL330_DEV *pl330, char *buf,
                   unsigned long bursts, struct PL330_XFER_SPEC *pxs, int ev)
{
    unsigned int lcnt1, ljmp1;
    int cyc, off = 0;
    struct PL330_XFER *x = &pxs->desc->px;

    if (bursts > 256) {
        lcnt1 = 256;
        cyc = bursts / 256;
    } else {
        lcnt1 = bursts;
        cyc = 1;
    }

    /* loop1 */
    off += PL330_Instr_DMALP(dryRun, &buf[off], 1, lcnt1);
    ljmp1 = off;
    off += _Bursts(dryRun, pl330, &buf[off], pxs, cyc);
    off +=
        PL330_Instr_DMALPEND(dryRun, &buf[off], ALWAYS, false, 1, off - ljmp1);

    /* remainder */
    lcnt1 = bursts - (lcnt1 * cyc);

    if (lcnt1) {
        off += PL330_Instr_DMALP(dryRun, &buf[off], 1, lcnt1);
        ljmp1 = off;
        off += _Bursts(dryRun, pl330, &buf[off], pxs, 1);
        off += PL330_Instr_DMALPEND(dryRun, &buf[off], ALWAYS, false, 1,
                                    off - ljmp1);
    }

    if (!pxs->desc->srcInterlaceSize &&
        !pxs->desc->dstInterlaceSize &&
        pl330->peripReqType == BURST) {
        unsigned int ccr = pxs->ccr;
        unsigned long c = 0;

        c = BYTE_MOD_BURST_LEN(x->length, pxs->ccr);

        if (c) {
            ccr &= ~(0xf << CC_SRCBRSTLEN_SHFT);
            ccr &= ~(0xf << CC_DSTBRSTLEN_SHFT);
            off += PL330_Instr_DMAMOV(dryRun, &buf[off], CCR, ccr);
            off += PL330_Instr_DMALP(dryRun, &buf[off], 1, c);
            ljmp1 = off;
            off += _Bursts(dryRun, pl330, &buf[off], pxs, 1);
            off += PL330_Instr_DMALPEND(dryRun, &buf[off], ALWAYS, false, 1,
                                        off - ljmp1);
            off += PL330_Instr_DMAMOV(dryRun, &buf[off], CCR, pxs->ccr);
        }
    }

    off += PL330_Instr_DMASEV(dryRun, &buf[off], ev);

    return off;
}

static int _Loop_Cyclic(uint8_t dryRun, struct HAL_PL330_DEV *pl330,
                        char *buf, unsigned long bursts,
                        struct PL330_XFER_SPEC *pxs, int ev)
{
    int off, periods, residue, i;
    unsigned int lcnt0, ljmp0, ljmpfe;
    struct PL330_XFER *x = &pxs->desc->px;

    off = 0;
    ljmpfe = off;
    lcnt0 = pxs->desc->numPeriods;
    periods = 1;

    while (lcnt0 > 256) {
        periods++;
        lcnt0 = pxs->desc->numPeriods / periods;
    }

    residue = pxs->desc->numPeriods % periods;

    /* forever loop */
    off += PL330_Instr_DMAMOV(dryRun, &buf[off], SAR, x->srcAddr);
    off += PL330_Instr_DMAMOV(dryRun, &buf[off], DAR, x->dstAddr);
    off += PL330_Instr_DMAFLUSHP(dryRun, &buf[off], pxs->desc->peri);
    /* loop0 */
    off += PL330_Instr_DMALP(dryRun, &buf[off], 0, lcnt0);
    ljmp0 = off;

    for (i = 0; i < periods; i++) {
        off += _Period(dryRun, pl330, &buf[off], bursts, pxs, ev);
    }

    off +=
        PL330_Instr_DMALPEND(dryRun, &buf[off], ALWAYS, false, 0, off - ljmp0);

    for (i = 0; i < residue; i++) {
        off += _Period(dryRun, pl330, &buf[off], bursts, pxs, ev);
    }

    off +=
        PL330_Instr_DMALPEND(dryRun, &buf[off], ALWAYS, true, 1, off - ljmpfe);

    return off;
}

static int _Setup_Loops(uint8_t dryRun, struct HAL_PL330_DEV *pl330,
                        char *buf, struct PL330_XFER_SPEC *pxs)
{
    struct PL330_XFER *x = &pxs->desc->px;
    uint32_t ccr = pxs->ccr;
    unsigned long c, bursts = BYTE_TO_BURST(x->length, ccr);
    int off = 0;

    if (HAL_DMA_IsSlaveDirection(pxs->desc->dir)) {
        off += PL330_Instr_DMAFLUSHP(dryRun, &buf[off], pxs->desc->peri);
    }

    if (pxs->desc->dir == DMA_DEV_TO_MEM) {
        bursts = x->length / (BRST_SIZE(ccr) * BRST_LEN(ccr) +
                              pxs->desc->dstInterlaceSize);
    } else if (pxs->desc->dir == DMA_MEM_TO_DEV) {
        bursts = x->length / (BRST_SIZE(ccr) * BRST_LEN(ccr) +
                              pxs->desc->srcInterlaceSize);
    }

    while (bursts) {
        c = bursts;
        off += _Loop(dryRun, pl330, &buf[off], &c, pxs);
        bursts -= c;
    }

    return off;
}

static int _Setup_Xfer(uint8_t dryRun, struct HAL_PL330_DEV *pl330,
                       char *buf, struct PL330_XFER_SPEC *pxs)
{
    struct PL330_XFER *x = &pxs->desc->px;
    int off = 0;

    /* DMAMOV SAR, x->src_addr */
    off += PL330_Instr_DMAMOV(dryRun, &buf[off], SAR, x->srcAddr);
    /* DMAMOV DAR, x->dst_addr */
    off += PL330_Instr_DMAMOV(dryRun, &buf[off], DAR, x->dstAddr);

    /* Setup Loop(s) */
    off += _Setup_Loops(dryRun, pl330, &buf[off], pxs);

    if (!pxs->desc->srcInterlaceSize &&
        !pxs->desc->dstInterlaceSize &&
        pl330->peripReqType == BURST) {
        unsigned int ccr = pxs->ccr;
        unsigned long c = 0;

        c = BYTE_MOD_BURST_LEN(x->length, pxs->ccr);

        if (c) {
            ccr &= ~(0xf << CC_SRCBRSTLEN_SHFT);
            ccr &= ~(0xf << CC_DSTBRSTLEN_SHFT);
            off += PL330_Instr_DMAMOV(dryRun, &buf[off], CCR, ccr);
            off += _Loop(dryRun, pl330, &buf[off], &c, pxs);
        }
    }

    return off;
}

static int _Setup_Xfer_Cyclic(uint8_t dryRun, struct HAL_PL330_DEV *pl330,
                              char *buf, struct PL330_XFER_SPEC *pxs, int ev)
{
    struct PL330_XFER *x = &pxs->desc->px;
    uint32_t ccr = pxs->ccr;
    unsigned long bursts = BYTE_TO_BURST(x->length, ccr);
    int off = 0;

    if (pxs->desc->dir == DMA_DEV_TO_MEM) {
        bursts = x->length / (BRST_SIZE(ccr) * BRST_LEN(ccr)
                              + pxs->desc->dstInterlaceSize);
    } else if (pxs->desc->dir == DMA_MEM_TO_DEV) {
        bursts = x->length / (BRST_SIZE(ccr) * BRST_LEN(ccr)
                              + pxs->desc->srcInterlaceSize);
    }

    /* Setup Loop(s) */
    off += _Loop_Cyclic(dryRun, pl330, &buf[off], bursts, pxs, ev);

    return off;
}

/**
 * @brief Conversion from the endian swap size to the bit encoding of the CCR
 *
 * @param endianSwapSize: the endian swap size, in terms of bits, it
 *        could be 8, 16, 32, 64, or 128(We are using DMA assembly syntax)
 *
 * @return The endian swap size bit encoding for the CCR.
 */
HAL_UNUSED __STATIC_INLINE uint32_t PL330_ToendianSwapSizeBits(uint32_t endianSwapSize)
{
    switch (endianSwapSize) {
    case 0:
    case 8:

        return 0;
    case 16:

        return 1;
    case 32:

        return 2;
    case 64:

        return 3;
    case 128:

        return 4;
    default:

        return 0;
    }
}

/**
 * @brief Conversion from the burst size to the bit encoding of the CCR
 *
 * @param burstSize: is the burst size. It's the data width.
 *        In terms of bytes, it could be 1, 2, 4, 8, 16, 32, 64, or 128.
 *        It must be no larger than the bus width.
 *
 * @return The burstSize bit encoding for the CCR.
 */
static uint32_t PL330_ToBurstSizeBits(uint32_t burstSize)
{
    switch (burstSize) {
    case 1:

        return 0;
    case 2:

        return 1;
    case 4:

        return 2;
    case 8:

        return 3;
    case 16:

        return 4;
    case 32:

        return 5;
    case 64:

        return 6;
    case 128:

        return 7;
    default:

        return 0;
    }
}

/**
 * @brief Conversion from PL330 bus transfer descriptors to CCR value.
 *
 * @param rqc: request config.
 *
 * @return The 32-bit CCR value.
 */
static uint32_t _Prepare_CCR(struct PL330_REQCFG *rqc)
{
    uint32_t ccr = 0;

    if (rqc->srcInc) {
        ccr |= CC_SRCINC;
    }

    if (rqc->dstInc) {
        ccr |= CC_DSTINC;
    }

    /* We set same protection levels for Src and DST for now */
    if (rqc->privileged) {
        ccr |= CC_SRCPRI | CC_DSTPRI;
    }
    if (rqc->nonsecure) {
        ccr |= CC_SRCNS | CC_DSTNS;
    }
    if (rqc->insnaccess) {
        ccr |= CC_SRCIA | CC_DSTIA;
    }

    ccr |= (((rqc->brstLen - 1) & 0xf) << CC_SRCBRSTLEN_SHFT);
    ccr |= (((rqc->brstLen - 1) & 0xf) << CC_DSTBRSTLEN_SHFT);

    ccr |= (rqc->brstSize << CC_SRCBRSTSIZE_SHFT);
    ccr |= (rqc->brstSize << CC_DSTBRSTSIZE_SHFT);

    ccr |= (rqc->scctl << CC_SRCCCTRL_SHFT);
    ccr |= (rqc->dcctl << CC_DSTCCTRL_SHFT);

    ccr |= (rqc->swap << CC_SWAP_SHFT);

    return ccr;
}

/* Call after fixing burst size */
static int getBurstLen(struct PL330_DESC *desc, struct HAL_PL330_DEV *pl330,
                       uint32_t len)
{
    int burstLen;

    burstLen = pl330->pcfg.dataBusWidth / 8;
    burstLen *= pl330->pcfg.dataBufDep / pl330->pcfg.numChan;
    burstLen >>= desc->rqcfg.brstSize;

    /* src/dst_burst_len can't be more than 16 */
    if (burstLen > 16) {
        burstLen = 16;
    }

    while (burstLen > 1) {
        if (!(len % (burstLen << desc->rqcfg.brstSize))) {
            break;
        }
        burstLen--;
    }

    return burstLen;
}

/**
 * @brief Use the debug registers to kill the DMA thread.
 *
 * @param reg: the DMA_REG info.
 * @param channel: the DMA channel number.
 * @param thread: the Debug thread encoding.
 *        0: DMA manager thread, 1: DMA channel.
 *
 * @return 0 on success, -1 on time out
 */
static int PL330_Exec_DMAKILL(struct DMA_REG *reg, uint32_t channel, uint32_t thread)
{
    uint32_t dbgInst0;
    int waitCount;

    dbgInst0 = PL330_DBGINST0(0, 0x01, channel, thread);

    /* wait while debug status is busy */
    waitCount = 0;
    while ((READ_REG(reg->DBGSTATUS) & PL330_DBGSTATUS_BUSY) &&
           (waitCount < PL330_MAX_WAIT)) {
        waitCount++;
    }

    if (waitCount >= PL330_MAX_WAIT) {
        /* wait time out */
        HAL_DBG("PL330 device at %p debug status busy time out\n", reg);

        return -1;
    }

    /* write debug instruction 0 */
    WRITE_REG(reg->DBGINST[0], dbgInst0);
    WRITE_REG(reg->DBGINST[1], 0);

    /* run the command in DbgInst0 and DbgInst1 */
    WRITE_REG(reg->DBGCMD, 0);

    return 0;
}

/**
 * @brief Read soc pl330 config.
 *
 * @param pl330: the handle of PL330.
 *
 * return None
 */
static void PL330_Read_Config(struct HAL_PL330_DEV *pl330)
{
    uint32_t val, crdn, cr;
    struct PL330_CONFIG *pcfg = &pl330->pcfg;
    struct DMA_REG *reg = pl330->pReg;

    crdn = READ_REG(reg->CRDN);
    cr = READ_REG(reg->CR[0]);

    val = (crdn >> CRD_DATA_WIDTH_SHIFT) & CRD_DATA_WIDTH_MASK;
    pcfg->dataBusWidth = 8 * (1 << val);

    pcfg->dataBufDep = ((crdn >> CRD_DATA_BUFF_SHIFT) & CRD_DATA_BUFF_MASK) + 1;
    pcfg->numChan = ((cr >> CR0_NUM_CHANS_SHIFT) & CR0_NUM_CHANS_MASK) + 1;

    if (cr & CR0_PERIPH_REQ_SET) {
        pcfg->numPeri = ((cr >> CR0_NUM_PERIPH_SHIFT) & CR0_NUM_PERIPH_MASK) + 1;
        pcfg->periNs = READ_REG(reg->CR[4]);
    } else {
        pcfg->numPeri = 0;
    }

    if (cr & CR0_BOOT_MAN_NS) {
        pcfg->mode |= DMAC_MODE_NS;
    } else {
        pcfg->mode &= ~DMAC_MODE_NS;
    }

    pcfg->numEvents = ((cr >> CR0_NUM_EVENTS_SHIFT) & CR0_NUM_EVENTS_MASK) + 1;
    pcfg->irqNs = READ_REG(reg->CR[3]);
}

/**
 * @brief Construct the DMA program based on the DMA transfer.
 *
 * @param pl330: the handle of PL330.
 * @param pxs: pl330 xfer spec.
 * @param channel: the DMA channel number
 *
 * @return The number of bytes for the program.
 */
static int PL330_BuildDmaProg(uint8_t dryRun, struct HAL_PL330_DEV *pl330,
                              struct PL330_XFER_SPEC *pxs, uint32_t channel)
{
    char *buf = (char *)pxs->desc->mcBuf;
    struct PL330_XFER *x;
    int off = 0;

    PL330_DBGMC_START((uint32_t)buf);

    /* DMAMOV CCR, ccr */
    off += PL330_Instr_DMAMOV(dryRun, &buf[off], CCR, pxs->ccr);

    x = &pxs->desc->px;

    if (pl330->peripReqType != BURST) {
        /* Error if xfer length is not aligned at burst size */
        if (x->length % (BRST_SIZE(pxs->ccr) * BRST_LEN(pxs->ccr))) {
            return HAL_ERROR;
        }
    }

    if (!pxs->desc->cyclic) {
        off += _Setup_Xfer(dryRun, pl330, &buf[off], pxs);
        /* DMASEV peripheral/event */
        off += PL330_Instr_DMASEV(dryRun, &buf[off], channel);
        /* DMAEND */
        off += PL330_Instr_DMAEND(dryRun, &buf[off]);
    } else {
        off += _Setup_Xfer_Cyclic(dryRun, pl330, &buf[off], pxs, channel);
    }

    /* make sure the buf and bufsize is cache line aligned. */
    if (!dryRun) {
        HAL_ASSERT(HAL_IS_CACHELINE_ALIGNED((uint32_t)buf));
        HAL_DCACHE_CleanByRange((uint32_t)buf, PL330_CHAN_BUF_LEN);
    }

    return off;
}

/**
 * @brief Generate a DMA program for the DMA command.
 *
 * @param pl330: the handle of PL330.
 * @param pxs: pl330 xfer spec.
 * @param channel: the DMA channel number
 *
 * @return - HAL_OK on success.
 *         - HAL_ERROR on fail.
 */
static HAL_Status PL330_GenDmaProg(struct HAL_PL330_DEV *pl330, struct PL330_XFER_SPEC *pxs,
                                   uint32_t channel)
{
    HAL_UNUSED struct PL330_DESC *desc = pxs->desc;
    int len;

    HAL_ASSERT(pl330 != NULL);
    HAL_ASSERT(pxs != NULL);
    HAL_ASSERT(desc != NULL);
    HAL_ASSERT(desc->mcBuf != NULL);

    len = PL330_BuildDmaProg(1, pl330, pxs, channel);
    if (len < 0 || len > PL330_CHAN_BUF_LEN) {
        HAL_DBG_ERR("xfer size is too large, try to increase mc size\n");

        return HAL_ERROR;
    }

    PL330_BuildDmaProg(0, pl330, pxs, channel);

    return HAL_OK;
}

/**
 * @brief Execute the DMAGO to start a channel.
 *
 * @param reg: the DMA_REG info.
 * @param channel: the DMA channel number.
 * @param addr: DMA program starting address, this should be DMA address
 *
 * @return HAL_OK on success, HAL_TIMEOUT on time out
 */
static HAL_Status PL330_Exec_DMAGO(struct DMA_REG *reg, uint32_t channel, uint32_t addr)
{
    char buf[8];
    uint32_t dbgInst0;
    uint32_t dbgInst1;
    int waitCount;

    PL330_Instr_DMAGO(0, buf, channel, addr, 1);

    dbgInst0 = PL330_DBGINST0(buf[1], buf[0], 0, 0);
    dbgInst1 = (uint32_t)addr;

    /* wait while debug status is busy */
    waitCount = 0;
    while ((READ_REG(reg->DBGSTATUS) & PL330_DBGSTATUS_BUSY) &&
           (waitCount < PL330_MAX_WAIT)) {
        waitCount++;
    }

    if (waitCount >= PL330_MAX_WAIT) {
        HAL_DBG("PL330 device at %p debug status busy time out\r\n", reg);

        return HAL_TIMEOUT;
    }

    /* write debug instruction 0 */
    WRITE_REG(reg->DBGINST[0], dbgInst0);
    /* write debug instruction 1 */
    WRITE_REG(reg->DBGINST[1], dbgInst1);

    /* wait while the DMA Manager is busy */
    waitCount = 0;
    while ((READ_REG(reg->DSR) & PL330_DS_DMA_STATUS) !=
           PL330_DS_DMA_STATUS_STOPPED &&
           waitCount <= PL330_MAX_WAIT) {
        waitCount++;
    }

    if (waitCount >= PL330_MAX_WAIT) {
        HAL_DBG("PL330 device at %p debug status busy time out\r\n", reg);

        return HAL_TIMEOUT;
    }

    /* run the command in DbgInst0 and DbgInst1 */
    WRITE_REG(reg->DBGCMD, 0);

    return HAL_OK;
}

static void PL330_CleanInvalidateDataBuf(struct PL330_DESC *desc)
{
    /* make sure the buf and bufsize cache line aligned. */
    if (desc->rqcfg.srcInc) {
        HAL_ASSERT(HAL_IS_CACHELINE_ALIGNED(desc->px.srcAddr));
        HAL_ASSERT(HAL_IS_CACHELINE_ALIGNED(desc->px.length));
        HAL_DCACHE_CleanByRange(desc->px.srcAddr, desc->px.length);
    }

    if (desc->rqcfg.dstInc) {
        HAL_ASSERT(HAL_IS_CACHELINE_ALIGNED(desc->px.dstAddr));
        HAL_ASSERT(HAL_IS_CACHELINE_ALIGNED(desc->px.length));
        HAL_DCACHE_InvalidateByRange(desc->px.dstAddr, desc->px.length);
    }
}

/** @} */
/********************* Public Function Definition ****************************/

/** @defgroup PL330_Exported_Functions_Group2 State and Errors Functions

 This section provides functions allowing to get the status of the module:

 *  @{
 */

/**
 * @brief PL330 Get Raw IrqStatus
 *
 * @param pl330: the handle of pl330.
 *
 * @return raw irq status
 */
uint32_t HAL_PL330_GetRawIrqStatus(struct HAL_PL330_DEV *pl330)
{
    struct DMA_REG *reg = pl330->pReg;

    return READ_REG(reg->EVENT_RIS);
}

/**
 * @brief PL330 Clear Irq
 *
 * @param pl330: the handle of pl330.
 * @param irq:   the channel irq.
 *
 * @return HAL_Status
 */
HAL_Status HAL_PL330_ClearIrq(struct HAL_PL330_DEV *pl330, uint32_t irq)
{
    struct DMA_REG *reg = pl330->pReg;

    WRITE_REG(reg->INTCLR, 1 << irq);

    return HAL_OK;
}

/**
 * @brief get the position
 *
 * @param pchan: the handle of struct PL330_CHAN.
 *
 * @return the size dma transferred.
 */
int HAL_PL330_GetPosition(struct PL330_CHAN *pchan)
{
    uint32_t val = 0, addr = 0;
    struct HAL_PL330_DEV *pl330 = pchan->pl330;
    struct PL330_DESC *desc = &pchan->desc;
    struct DMA_REG *reg = pl330->pReg;
    int transferred;

    if (desc->rqcfg.srcInc) {
        val = READ_REG(reg->CHAN_CFG[pchan->chanId].SAR);
        addr = desc->px.srcAddr;
    } else {
        val = READ_REG(reg->CHAN_CFG[pchan->chanId].DAR);
        addr = desc->px.dstAddr;
    }

    transferred = val - addr;

    return transferred;
}

/** @} */

/** @defgroup PL330_Exported_Functions_Group4 Init and DeInit Functions

 This section provides functions allowing to init and deinit the module:

 *  @{
 */

/**
 * @brief Initializes a specific PL330 dmac.
 *
 * @param pl330: the handle of PL330.
 *
 * @return
 *        - HAL_OK on success.
 *        - HAL_ERROR on fail.
 */
HAL_Status HAL_PL330_Init(struct HAL_PL330_DEV *pl330)
{
    struct PL330_CHAN *pchan;
    uint8_t channel;

    HAL_ASSERT(pl330 != NULL);

    memset(pl330->chans, 0, sizeof(*pchan) * PL330_CHANNELS_PER_DEV);

    for (channel = 0; channel < PL330_CHANNELS_PER_DEV; channel++) {
        pchan = pl330->chans + channel;
        pchan->chanId = channel;
    }

    PL330_Read_Config(pl330);

    return HAL_OK;
}

/**
 * @brief DeInitializes a specific PL330 dmac.
 *
 * @param pl330: the handle of pl330.
 *
 * @return
 *        - HAL_OK on success.
 *        - HAL_ERROR on fail.
 */
HAL_Status HAL_PL330_DeInit(struct HAL_PL330_DEV *pl330)
{
    uint32_t dbgInst;
    uint32_t waitCount = 0;
    uint32_t i;
    struct DMA_REG *reg = pl330->pReg;

    /* Disable all the interrupts */
    WRITE_REG(reg->INTEN, 0x00);
    /* Clear the interrupts */
    WRITE_REG(reg->INTCLR, PL330_INTCLR_ALL_MASK);
    /* Kill the dma channel threads */
    for (i = 0; i < PL330_CHANNELS_PER_DEV; i++) {
        while ((READ_REG(reg->DBGSTATUS) & PL330_DBGSTATUS_BUSY) &&
               (waitCount < PL330_MAX_WAIT)) {
            waitCount++;
        }

        dbgInst = PL330_DBGINST0(0, 0x01, i, 1);
        WRITE_REG(reg->DBGINST[0], dbgInst);
        WRITE_REG(reg->DBGINST[1], 0x0);
        WRITE_REG(reg->DBGCMD, 0x0);
    }
    /* Kill the manager thread    */
    dbgInst = PL330_DBGINST0(0, 0x01, 0, 0);
    WRITE_REG(reg->DBGINST[0], dbgInst);
    WRITE_REG(reg->DBGINST[1], 0x0);
    WRITE_REG(reg->DBGCMD, 0x0);

    return HAL_OK;
}

/** @} */

/** @defgroup PL330_Exported_Functions_Group3 IO Functions

 This section provides functions allowing to IO controlling:

 *  @{
 */

/**
 * @brief Start a DMA channel with the desc request.
 *
 * @param pchan: the hanlde of struct PL330_CHAN.
 *
 * @return
 *        - HAL_OK on success
 *        - HAL_BUSY if DMA is busy
 *        - HAL_ERROR on other failures
 */
HAL_Status HAL_PL330_Start(struct PL330_CHAN *pchan)
{
    HAL_Status ret = HAL_OK;
    uint32_t ccr;
    struct PL330_XFER_SPEC xs;
    uint32_t channel = pchan->chanId;
    struct HAL_PL330_DEV *pl330 = pchan->pl330;
    struct PL330_DESC *desc = &pchan->desc;
    struct DMA_REG *reg = pl330->pReg;

    HAL_ASSERT(pl330 != NULL);
    HAL_ASSERT(pchan->mcBuf);

    if (pl330->pcfg.mode & DMAC_MODE_NS) {
        desc->rqcfg.nonsecure = 1;
    } else {
        desc->rqcfg.nonsecure = 0;
    }

    ccr = _Prepare_CCR(&desc->rqcfg);

    xs.ccr = ccr;
    xs.desc = desc;
    desc->mcBuf = pchan->mcBuf;

    ret = PL330_GenDmaProg(pl330, &xs, channel);
    if (ret) {
        return HAL_ERROR;
    }

    /* enable the interrupt */
    SET_BIT(reg->INTEN, 0x01 << channel);

    return PL330_Exec_DMAGO(pl330->pReg, channel, (uint32_t)desc->mcBuf);
}

/**
 * @brief Stop a DMA channel.
 *
 * @param pchan: the hanlde of struct PL330_CHAN.
 *
 * @return
 *        - HAL_OK on success
 *        - HAL_BUSY if DMA is busy
 *        - HAL_ERROR on other failures
 */
HAL_Status HAL_PL330_Stop(struct PL330_CHAN *pchan)
{
    uint32_t intEn = READ_REG(pchan->pl330->pReg->INTEN);

    HAL_ASSERT(pchan != NULL);

    PL330_Exec_DMAKILL(pchan->pl330->pReg, pchan->chanId, 1);

    if (intEn & (1 << pchan->chanId)) {
        WRITE_REG(pchan->pl330->pReg->INTCLR, 1 << pchan->chanId);
    }

    WRITE_REG(pchan->pl330->pReg->INTEN, intEn & ~(1 << pchan->chanId));

    return HAL_OK;
}

/**
 * @brief PL330 IrqHandler
 *
 * @param pl330: the handle of pl330.
 *
 * @return: raw irq status
 */
uint32_t HAL_PL330_IrqHandler(struct HAL_PL330_DEV *pl330)
{
    struct DMA_REG *reg = pl330->pReg;
    uint32_t val, inten;
    unsigned int ev, i = 0;

    val = READ_REG(reg->FSRD) & 0x1;
    if (val) {
        /*
         * if DMA manager is fault
         */
        HAL_DBG("Fault Type: 0x%lx\n", READ_REG(reg->FTRD));
        HAL_DBG("Fault PC 0x%lx\n", READ_REG(reg->DPC));
        /* kill the DMA manager thread */
        /* Should we disable interrupt?*/
        PL330_Exec_DMAKILL(pl330->pReg, 0, 0);
    }

    val = READ_REG(reg->FSRC) & ((1 << pl330->pcfg.numChan) - 1);
    if (val) {
        while (i < pl330->pcfg.numChan) {
            if (val & (1 << i)) {
                HAL_DBG("Reset Channel-%d\t CS-%lx\n",
                        i, READ_REG(reg->CHAN_STS[i].CSR));
                HAL_DBG("Reset Channel-%d\t FTC-%lx\n",
                        i, READ_REG(reg->FTR[i]));
                /* kill the channel thread */
                /* Should we disable interrupt? */
                PL330_Exec_DMAKILL(pl330->pReg, i, 1);
            }
            i++;
        }
    }

    /* Check which event happened i.e, thread notified */
    val = READ_REG(reg->EVENT_RIS);
    inten = READ_REG(reg->INTEN);

    for (ev = 0; ev < PL330_CHANNELS_PER_DEV; ev++) {
        if (val & (1 << ev)) { /* Event occurred */
            /* Clear the event */
            if (inten & (1 << ev)) {
                WRITE_REG(reg->INTCLR, 1 << ev);
            }
        }
    }

    /* return raw irq status */
    return val;
}

/**
 * @brief Set mc buf resource for chan
 *
 * @param pchan: the handle of struct PL330_CHAN.
 * @param buf: mc buf addr.
 *
 * @return HAL_Status.
 *
 * @note must be invoked before function HAL_PL330_PrepDmaXXX.
 */
HAL_Status HAL_PL330_SetMcBuf(struct PL330_CHAN *pchan, void *buf)
{
    HAL_ASSERT(pchan);
    HAL_ASSERT(buf);

    pchan->mcBuf = buf;

    return HAL_OK;
}

/**
 * @brief Get mc buf resource of chan
 *
 * @param pchan: the handle of struct PL330_CHAN.
 *
 * @return mc buf addr.
 */
void *HAL_PL330_GetMcBuf(struct PL330_CHAN *pchan)
{
    HAL_ASSERT(pchan);

    return pchan->mcBuf;
}

/**
 * @brief Get desc resource of chan
 *
 * @param pchan: the handle of struct PL330_CHAN.
 *
 * @return desc ptr.
 */
const struct PL330_DESC *HAL_PL330_GetDesc(struct PL330_CHAN *pchan)
{
    HAL_ASSERT(pchan);

    return &pchan->desc;
}

/**
 * @brief Request a dma channel
 *
 * @param pl330: the handle of struct HAL_PL330_DEV.
 * @param id: the peri id.
 *
 * @return a idle dma channel.
 */
struct PL330_CHAN *HAL_PL330_RequestChannel(struct HAL_PL330_DEV *pl330, DMA_REQ_Type id)
{
    int i = 0;
    struct PL330_CHAN *pchan = NULL;

    for (i = 0; i < PL330_CHANNELS_PER_DEV; i++) {
        if (pl330->chans[i].used) {
            continue;
        }
        pchan = &pl330->chans[i];
        pchan->used = true;
        pchan->periId = id;
        pchan->pl330 = pl330;
        break;
    }

    return pchan;
}

/**
 * @brief Release a dma channel
 *
 * @param pchan: the handle of struct PL330_CHAN.
 *
 * @return
 *        - HAL_OK on success.
 *        - HAL_ERROR on fail.
 */
HAL_Status HAL_PL330_ReleaseChannel(struct PL330_CHAN *pchan)
{
    HAL_ASSERT(pchan);

    pchan->periId = 0;
    pchan->used = false;

    return HAL_OK;
}

/**
 * @brief Config a pl330 dma channel
 *
 * @param pchan: the handle of struct PL330_CHAN.
 * @param config: the peri req config.
 *
 * @return
 *        - HAL_OK on success.
 *        - HAL_ERROR on fail.
 */
HAL_Status HAL_PL330_Config(struct PL330_CHAN *pchan, struct DMA_SLAVE_CONFIG *config)
{
    HAL_ASSERT(pchan);

    if (config->direction == DMA_MEM_TO_DEV) {
        if (config->dstAddr) {
            pchan->fifoAddr = config->dstAddr;
        }
        if (config->dstAddrWidth) {
            pchan->brstSz = PL330_ToBurstSizeBits(config->dstAddrWidth);
        }
        if (config->dstMaxBurst) {
            pchan->brstLen = config->dstMaxBurst;
        }
        if (config->srcInterlaceSize) {
            pchan->srcInterlaceSize = config->srcInterlaceSize;
        }
    } else if (config->direction == DMA_DEV_TO_MEM) {
        if (config->srcAddr) {
            pchan->fifoAddr = config->srcAddr;
        }
        if (config->srcAddrWidth) {
            pchan->brstSz = PL330_ToBurstSizeBits(config->srcAddrWidth);
        }
        if (config->srcMaxBurst) {
            pchan->brstLen = config->srcMaxBurst;
        }
        if (config->dstInterlaceSize) {
            pchan->dstInterlaceSize = config->dstInterlaceSize;
        }
    }

    return HAL_OK;
}

/**
 * @brief Prepare a cyclic dma transfer for the channel
 *
 * @param pchan: tthe handle of struct PL330_CHAN.
 * @param dmaAddr: the memory addr.
 * @param len: data len.
 * @param periodLen: periodic len.
 * @param direction: transfer direction.
 * @param callback: callback function.
 * @param cparam: callback param.
 *
 * @return
 *        - HAL_OK on success.
 *        - HAL_ERROR on fail.
 */
HAL_Status HAL_PL330_PrepDmaCyclic(struct PL330_CHAN *pchan, uint32_t dmaAddr,
                                   uint32_t len, uint32_t periodLen,
                                   eDMA_TRANSFER_DIRECTION direction,
                                   PL330_Callback callback, void *cparam)
{
    struct HAL_PL330_DEV *pl330 = pchan->pl330;
    struct PL330_DESC *desc = &pchan->desc;
    uint32_t dst;
    uint32_t src;

    HAL_ASSERT(pl330 != NULL);
    HAL_ASSERT(len % periodLen == 0);
    HAL_ASSERT(direction == DMA_MEM_TO_DEV || direction == DMA_DEV_TO_MEM);

    memset(desc, 0x0, sizeof(*desc));

    switch (direction) {
    case DMA_MEM_TO_DEV:
        desc->rqcfg.srcInc = 1;
        desc->rqcfg.dstInc = 0;
        src = dmaAddr;
        dst = pchan->fifoAddr;
        break;
    case DMA_DEV_TO_MEM:
        desc->rqcfg.srcInc = 0;
        desc->rqcfg.dstInc = 1;
        src = pchan->fifoAddr;
        dst = dmaAddr;
        break;
    default:

        return HAL_ERROR;
    }

    desc->peri = pchan->periId;
    desc->dir = direction;
    desc->rqcfg.brstSize = pchan->brstSz;

    if (pl330->peripReqType == BURST) {
        desc->rqcfg.brstLen = pchan->brstLen;
    } else {
        desc->rqcfg.brstLen = 1;
    }

    desc->bytesReq = len;
    desc->px.srcAddr = src;
    desc->px.dstAddr = dst;
    desc->px.length = periodLen;

    desc->cyclic = true;
    desc->numPeriods = len / periodLen;

    desc->callback = callback;
    desc->cparam = cparam;

    desc->srcInterlaceSize = pchan->srcInterlaceSize;
    desc->dstInterlaceSize = pchan->dstInterlaceSize;

    HAL_DBG("%s: srcInterlaceSize: %d, dstInterlaceSize: %d\n", __func__,
            desc->srcInterlaceSize, desc->dstInterlaceSize);

    return HAL_OK;
}

/**
 * @brief Prepare a single dma transfer for the channel
 *
 * @param pchan: the handle of struct PL330_CHAN.
 * @param dmaAddr: the memory addr.
 * @param len: data len.
 * @param direction: transfer direction.
 * @param callback: callback function.
 * @param cparam: callback param.
 *
 * @return
 *        - HAL_OK on success.
 *        - HAL_ERROR on fail.
 */
HAL_Status HAL_PL330_PrepDmaSingle(struct PL330_CHAN *pchan, uint32_t dmaAddr,
                                   uint32_t len,
                                   eDMA_TRANSFER_DIRECTION direction,
                                   PL330_Callback callback, void *cparam)
{
    struct HAL_PL330_DEV *pl330 = pchan->pl330;
    struct PL330_DESC *desc = &pchan->desc;
    uint32_t dst;
    uint32_t src;

    HAL_ASSERT(pl330 != NULL);
    HAL_ASSERT(direction == DMA_MEM_TO_DEV || direction == DMA_DEV_TO_MEM);

    memset(desc, 0x0, sizeof(*desc));

    switch (direction) {
    case DMA_MEM_TO_DEV:
        desc->rqcfg.srcInc = 1;
        desc->rqcfg.dstInc = 0;
        src = dmaAddr;
        dst = pchan->fifoAddr;
        break;
    case DMA_DEV_TO_MEM:
        desc->rqcfg.srcInc = 0;
        desc->rqcfg.dstInc = 1;
        src = pchan->fifoAddr;
        dst = dmaAddr;
        break;
    default:

        return HAL_ERROR;
    }

    desc->peri = pchan->periId;
    desc->dir = direction;
    desc->rqcfg.brstSize = pchan->brstSz;

    if (pl330->peripReqType == BURST) {
        desc->rqcfg.brstLen = pchan->brstLen;
    } else {
        desc->rqcfg.brstLen = 1;
    }

    desc->bytesReq = len;
    desc->px.srcAddr = src;
    desc->px.dstAddr = dst;
    desc->px.length = len;

    desc->cyclic = false;
    desc->numPeriods = 0;

    desc->callback = callback;
    desc->cparam = cparam;

    PL330_CleanInvalidateDataBuf(desc);

    return HAL_OK;
}

/**
 * @brief Prepare a dma memcpy
 *
 * @param pchan: the handle of struct PL330_CHAN.
 * @param dst: the memory dst addr.
 * @param src: the memory src addr.
 * @param len: data len.
 * @param callback: callback function.
 * @param cparam: callback param.
 *
 * @return
 *        - HAL_OK on success.
 *        - HAL_ERROR on fail.
 */
HAL_Status HAL_PL330_PrepDmaMemcpy(struct PL330_CHAN *pchan, uint32_t dst,
                                   uint32_t src, uint32_t len,
                                   PL330_Callback callback, void *cparam)
{
    struct HAL_PL330_DEV *pl330 = pchan->pl330;
    struct PL330_DESC *desc = &pchan->desc;
    int burst;

    HAL_ASSERT(pl330 != NULL);
    HAL_ASSERT(len > 0);

    memset(desc, 0x0, sizeof(*desc));

    desc->px.srcAddr = src;
    desc->px.dstAddr = dst;
    desc->px.length = len;
    desc->rqcfg.srcInc = 1;
    desc->rqcfg.dstInc = 1;
    desc->dir = DMA_MEM_TO_MEM;

    /* Select max possible burst size */
    burst = pl330->pcfg.dataBusWidth / 8;

    /*
     * Make sure we use a burst size that aligns with all the memcpy
     * parameters because our DMA programming algorithm doesn't cope with
     * transfers which straddle an entry in the DMA device's MFIFO.
     */
    while ((src | dst | len) & (burst - 1)) {
        burst /= 2;
    }

    desc->rqcfg.brstSize = 0;
    while (burst != (1 << desc->rqcfg.brstSize)) {
        desc->rqcfg.brstSize++;
    }

    /*
     * If burst size is smaller than bus width then make sure we only
     * transfer one at a time to avoid a burst stradling an MFIFO entry.
     */
    if (desc->rqcfg.brstSize * 8 < pl330->pcfg.dataBusWidth) {
        desc->rqcfg.brstLen = 1;
    }

    desc->rqcfg.brstLen = getBurstLen(desc, pl330, len);
    desc->bytesReq = len;

    desc->callback = callback;
    desc->cparam = cparam;

    PL330_CleanInvalidateDataBuf(desc);

    return HAL_OK;
}

/** @} */

/** @} */

/** @} */

#endif /* HAL_PL330_MODULE_ENABLED */