Linux Audio

Check our new training course

Loading...
   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
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
// SPDX-License-Identifier: GPL-2.0
/*
 * Renesas USBF USB Function driver
 *
 * Copyright 2022 Schneider Electric
 * Author: Herve Codina <herve.codina@bootlin.com>
 */

#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/interrupt.h>
#include <linux/iopoll.h>
#include <linux/kernel.h>
#include <linux/kfifo.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/types.h>
#include <linux/usb/composite.h>
#include <linux/usb/gadget.h>
#include <linux/usb/role.h>

#define USBF_NUM_ENDPOINTS	16
#define USBF_EP0_MAX_PCKT_SIZE	64

/* EPC registers */
#define USBF_REG_USB_CONTROL	0x000
#define     USBF_USB_PUE2		BIT(2)
#define     USBF_USB_CONNECTB		BIT(3)
#define     USBF_USB_DEFAULT		BIT(4)
#define     USBF_USB_CONF		BIT(5)
#define     USBF_USB_SUSPEND		BIT(6)
#define     USBF_USB_RSUM_IN		BIT(7)
#define     USBF_USB_SOF_RCV		BIT(8)
#define     USBF_USB_FORCEFS		BIT(9)
#define     USBF_USB_INT_SEL		BIT(10)
#define     USBF_USB_SOF_CLK_MODE	BIT(11)

#define USBF_REG_USB_STATUS	0x004
#define     USBF_USB_RSUM_OUT		BIT(1)
#define     USBF_USB_SPND_OUT		BIT(2)
#define     USBF_USB_USB_RST		BIT(3)
#define     USBF_USB_DEFAULT_ST		BIT(4)
#define     USBF_USB_CONF_ST		BIT(5)
#define     USBF_USB_SPEED_MODE		BIT(6)
#define     USBF_USB_SOF_DELAY_STATUS	BIT(31)

#define USBF_REG_USB_ADDRESS	0x008
#define     USBF_USB_SOF_STATUS		BIT(15)
#define     USBF_USB_SET_USB_ADDR(_a)	((_a) << 16)
#define     USBF_USB_GET_FRAME(_r)	((_r) & 0x7FF)

#define USBF_REG_SETUP_DATA0	0x018
#define USBF_REG_SETUP_DATA1	0x01C
#define USBF_REG_USB_INT_STA	0x020
#define     USBF_USB_RSUM_INT		BIT(1)
#define     USBF_USB_SPND_INT		BIT(2)
#define     USBF_USB_USB_RST_INT	BIT(3)
#define     USBF_USB_SOF_INT		BIT(4)
#define     USBF_USB_SOF_ERROR_INT	BIT(5)
#define     USBF_USB_SPEED_MODE_INT	BIT(6)
#define     USBF_USB_EPN_INT(_n)	(BIT(8) << (_n)) /* n=0..15 */

#define USBF_REG_USB_INT_ENA	0x024
#define     USBF_USB_RSUM_EN		BIT(1)
#define     USBF_USB_SPND_EN		BIT(2)
#define     USBF_USB_USB_RST_EN		BIT(3)
#define     USBF_USB_SOF_EN		BIT(4)
#define     USBF_USB_SOF_ERROR_EN	BIT(5)
#define     USBF_USB_SPEED_MODE_EN	BIT(6)
#define     USBF_USB_EPN_EN(_n)		(BIT(8) << (_n)) /* n=0..15 */

#define USBF_BASE_EP0		0x028
/* EP0 registers offsets from Base + USBF_BASE_EP0 (EP0 regs area) */
#define     USBF_REG_EP0_CONTROL	0x00
#define         USBF_EP0_ONAK			BIT(0)
#define         USBF_EP0_INAK			BIT(1)
#define         USBF_EP0_STL			BIT(2)
#define         USBF_EP0_PERR_NAK_CLR		BIT(3)
#define         USBF_EP0_INAK_EN		BIT(4)
#define         USBF_EP0_DW_MASK		(0x3 << 5)
#define         USBF_EP0_DW(_s)			((_s) << 5)
#define         USBF_EP0_DEND			BIT(7)
#define         USBF_EP0_BCLR			BIT(8)
#define         USBF_EP0_PIDCLR			BIT(9)
#define         USBF_EP0_AUTO			BIT(16)
#define         USBF_EP0_OVERSEL		BIT(17)
#define         USBF_EP0_STGSEL			BIT(18)

#define     USBF_REG_EP0_STATUS		0x04
#define         USBF_EP0_SETUP_INT		BIT(0)
#define         USBF_EP0_STG_START_INT		BIT(1)
#define         USBF_EP0_STG_END_INT		BIT(2)
#define         USBF_EP0_STALL_INT		BIT(3)
#define         USBF_EP0_IN_INT			BIT(4)
#define         USBF_EP0_OUT_INT		BIT(5)
#define         USBF_EP0_OUT_OR_INT		BIT(6)
#define         USBF_EP0_OUT_NULL_INT		BIT(7)
#define         USBF_EP0_IN_EMPTY		BIT(8)
#define         USBF_EP0_IN_FULL		BIT(9)
#define         USBF_EP0_IN_DATA		BIT(10)
#define         USBF_EP0_IN_NAK_INT		BIT(11)
#define         USBF_EP0_OUT_EMPTY		BIT(12)
#define         USBF_EP0_OUT_FULL		BIT(13)
#define         USBF_EP0_OUT_NULL		BIT(14)
#define         USBF_EP0_OUT_NAK_INT		BIT(15)
#define         USBF_EP0_PERR_NAK_INT		BIT(16)
#define         USBF_EP0_PERR_NAK		BIT(17)
#define         USBF_EP0_PID			BIT(18)

#define     USBF_REG_EP0_INT_ENA	0x08
#define         USBF_EP0_SETUP_EN		BIT(0)
#define         USBF_EP0_STG_START_EN		BIT(1)
#define         USBF_EP0_STG_END_EN		BIT(2)
#define         USBF_EP0_STALL_EN		BIT(3)
#define         USBF_EP0_IN_EN			BIT(4)
#define         USBF_EP0_OUT_EN			BIT(5)
#define         USBF_EP0_OUT_OR_EN		BIT(6)
#define         USBF_EP0_OUT_NULL_EN		BIT(7)
#define         USBF_EP0_IN_NAK_EN		BIT(11)
#define         USBF_EP0_OUT_NAK_EN		BIT(15)
#define         USBF_EP0_PERR_NAK_EN		BIT(16)

#define     USBF_REG_EP0_LENGTH		0x0C
#define         USBF_EP0_LDATA			(0x7FF << 0)
#define     USBF_REG_EP0_READ		0x10
#define     USBF_REG_EP0_WRITE		0x14

#define USBF_BASE_EPN(_n)	(0x040 + (_n) * 0x020)
/* EPn registers offsets from Base + USBF_BASE_EPN(n-1). n=1..15 */
#define     USBF_REG_EPN_CONTROL	0x000
#define         USBF_EPN_ONAK			BIT(0)
#define         USBF_EPN_OSTL			BIT(2)
#define         USBF_EPN_ISTL			BIT(3)
#define         USBF_EPN_OSTL_EN		BIT(4)
#define         USBF_EPN_DW_MASK		(0x3 << 5)
#define         USBF_EPN_DW(_s)			((_s) << 5)
#define         USBF_EPN_DEND			BIT(7)
#define         USBF_EPN_CBCLR			BIT(8)
#define         USBF_EPN_BCLR			BIT(9)
#define         USBF_EPN_OPIDCLR		BIT(10)
#define         USBF_EPN_IPIDCLR		BIT(11)
#define         USBF_EPN_AUTO			BIT(16)
#define         USBF_EPN_OVERSEL		BIT(17)
#define         USBF_EPN_MODE_MASK		(0x3 << 24)
#define         USBF_EPN_MODE_BULK		(0x0 << 24)
#define         USBF_EPN_MODE_INTR		(0x1 << 24)
#define         USBF_EPN_MODE_ISO		(0x2 << 24)
#define         USBF_EPN_DIR0			BIT(26)
#define         USBF_EPN_BUF_TYPE_DOUBLE	BIT(30)
#define         USBF_EPN_EN			BIT(31)

#define     USBF_REG_EPN_STATUS		0x004
#define         USBF_EPN_IN_EMPTY		BIT(0)
#define         USBF_EPN_IN_FULL		BIT(1)
#define         USBF_EPN_IN_DATA		BIT(2)
#define         USBF_EPN_IN_INT			BIT(3)
#define         USBF_EPN_IN_STALL_INT		BIT(4)
#define         USBF_EPN_IN_NAK_ERR_INT		BIT(5)
#define         USBF_EPN_IN_END_INT		BIT(7)
#define         USBF_EPN_IPID			BIT(10)
#define         USBF_EPN_OUT_EMPTY		BIT(16)
#define         USBF_EPN_OUT_FULL		BIT(17)
#define         USBF_EPN_OUT_NULL_INT		BIT(18)
#define         USBF_EPN_OUT_INT		BIT(19)
#define         USBF_EPN_OUT_STALL_INT		BIT(20)
#define         USBF_EPN_OUT_NAK_ERR_INT	BIT(21)
#define         USBF_EPN_OUT_OR_INT		BIT(22)
#define         USBF_EPN_OUT_END_INT		BIT(23)
#define         USBF_EPN_ISO_CRC		BIT(24)
#define         USBF_EPN_ISO_OR			BIT(26)
#define         USBF_EPN_OUT_NOTKN		BIT(27)
#define         USBF_EPN_ISO_OPID		BIT(28)
#define         USBF_EPN_ISO_PIDERR		BIT(29)

#define     USBF_REG_EPN_INT_ENA	0x008
#define         USBF_EPN_IN_EN			BIT(3)
#define         USBF_EPN_IN_STALL_EN		BIT(4)
#define         USBF_EPN_IN_NAK_ERR_EN		BIT(5)
#define         USBF_EPN_IN_END_EN		BIT(7)
#define         USBF_EPN_OUT_NULL_EN		BIT(18)
#define         USBF_EPN_OUT_EN			BIT(19)
#define         USBF_EPN_OUT_STALL_EN		BIT(20)
#define         USBF_EPN_OUT_NAK_ERR_EN		BIT(21)
#define         USBF_EPN_OUT_OR_EN		BIT(22)
#define         USBF_EPN_OUT_END_EN		BIT(23)

#define     USBF_REG_EPN_DMA_CTRL	0x00C
#define         USBF_EPN_DMAMODE0		BIT(0)
#define         USBF_EPN_DMA_EN			BIT(4)
#define         USBF_EPN_STOP_SET		BIT(8)
#define         USBF_EPN_BURST_SET		BIT(9)
#define         USBF_EPN_DEND_SET		BIT(10)
#define         USBF_EPN_STOP_MODE		BIT(11)

#define     USBF_REG_EPN_PCKT_ADRS	0x010
#define         USBF_EPN_MPKT(_l)		((_l) << 0)
#define         USBF_EPN_BASEAD(_a)		((_a) << 16)

#define     USBF_REG_EPN_LEN_DCNT	0x014
#define         USBF_EPN_GET_LDATA(_r)		((_r) & 0x7FF)
#define         USBF_EPN_SET_DMACNT(_c)		((_c) << 16)
#define         USBF_EPN_GET_DMACNT(_r)		(((_r) >> 16) & 0x1ff)

#define     USBF_REG_EPN_READ		0x018
#define     USBF_REG_EPN_WRITE		0x01C

/* AHB-EPC Bridge registers */
#define USBF_REG_AHBSCTR	0x1000
#define USBF_REG_AHBMCTR	0x1004
#define     USBF_SYS_WBURST_TYPE	BIT(2)
#define     USBF_SYS_ARBITER_CTR	BIT(31)

#define USBF_REG_AHBBINT	0x1008
#define     USBF_SYS_ERR_MASTER		 (0x0F << 0)
#define     USBF_SYS_SBUS_ERRINT0	 BIT(4)
#define     USBF_SYS_SBUS_ERRINT1	 BIT(5)
#define     USBF_SYS_MBUS_ERRINT	 BIT(6)
#define     USBF_SYS_VBUS_INT		 BIT(13)
#define     USBF_SYS_DMA_ENDINT_EPN(_n)	 (BIT(16) << (_n)) /* _n=1..15 */

#define USBF_REG_AHBBINTEN	0x100C
#define     USBF_SYS_SBUS_ERRINT0EN	  BIT(4)
#define     USBF_SYS_SBUS_ERRINT1EN	  BIT(5)
#define     USBF_SYS_MBUS_ERRINTEN	  BIT(6)
#define     USBF_SYS_VBUS_INTEN		  BIT(13)
#define     USBF_SYS_DMA_ENDINTEN_EPN(_n) (BIT(16) << (_n)) /* _n=1..15 */

#define USBF_REG_EPCTR		0x1010
#define     USBF_SYS_EPC_RST		BIT(0)
#define     USBF_SYS_PLL_RST		BIT(2)
#define     USBF_SYS_PLL_LOCK		BIT(4)
#define     USBF_SYS_PLL_RESUME		BIT(5)
#define     USBF_SYS_VBUS_LEVEL		BIT(8)
#define     USBF_SYS_DIRPD		BIT(12)

#define USBF_REG_USBSSVER	0x1020
#define USBF_REG_USBSSCONF	0x1024
#define    USBF_SYS_DMA_AVAILABLE(_n)	(BIT(0) << (_n)) /* _n=0..15 */
#define    USBF_SYS_EP_AVAILABLE(_n)	(BIT(16) << (_n)) /* _n=0..15 */

#define USBF_BASE_DMA_EPN(_n)	(0x1110 + (_n) * 0x010)
/* EPn DMA registers offsets from Base USBF_BASE_DMA_EPN(n-1). n=1..15*/
#define     USBF_REG_DMA_EPN_DCR1	0x00
#define         USBF_SYS_EPN_REQEN		BIT(0)
#define         USBF_SYS_EPN_DIR0		BIT(1)
#define         USBF_SYS_EPN_SET_DMACNT(_c)	((_c) << 16)
#define         USBF_SYS_EPN_GET_DMACNT(_r)	(((_r) >> 16) & 0x0FF)

#define     USBF_REG_DMA_EPN_DCR2	0x04
#define         USBF_SYS_EPN_MPKT(_s)		((_s) << 0)
#define         USBF_SYS_EPN_LMPKT(_l)		((_l) << 16)

#define     USBF_REG_DMA_EPN_TADR	0x08

/* USB request */
struct usbf_req {
	struct usb_request	req;
	struct list_head	queue;
	unsigned int		is_zero_sent : 1;
	unsigned int		is_mapped : 1;
	enum {
		USBF_XFER_START,
		USBF_XFER_WAIT_DMA,
		USBF_XFER_SEND_NULL,
		USBF_XFER_WAIT_END,
		USBF_XFER_WAIT_DMA_SHORT,
		USBF_XFER_WAIT_BRIDGE,
	}			xfer_step;
	size_t			dma_size;
};

/* USB Endpoint */
struct usbf_ep {
	struct usb_ep		ep;
	char			name[32];
	struct list_head	queue;
	unsigned int		is_processing : 1;
	unsigned int		is_in : 1;
	struct			usbf_udc *udc;
	void __iomem		*regs;
	void __iomem		*dma_regs;
	unsigned int		id : 8;
	unsigned int		disabled : 1;
	unsigned int		is_wedged : 1;
	unsigned int		delayed_status : 1;
	u32			status;
	void			(*bridge_on_dma_end)(struct usbf_ep *ep);
};

enum usbf_ep0state {
	EP0_IDLE,
	EP0_IN_DATA_PHASE,
	EP0_OUT_DATA_PHASE,
	EP0_OUT_STATUS_START_PHASE,
	EP0_OUT_STATUS_PHASE,
	EP0_OUT_STATUS_END_PHASE,
	EP0_IN_STATUS_START_PHASE,
	EP0_IN_STATUS_PHASE,
	EP0_IN_STATUS_END_PHASE,
};

struct usbf_udc {
	struct usb_gadget		gadget;
	struct usb_gadget_driver	*driver;
	struct device			*dev;
	void __iomem			*regs;
	spinlock_t			lock;
	bool				is_remote_wakeup;
	bool				is_usb_suspended;
	struct usbf_ep			ep[USBF_NUM_ENDPOINTS];
	/* for EP0 control messages */
	enum usbf_ep0state		ep0state;
	struct usbf_req			setup_reply;
	u8				ep0_buf[USBF_EP0_MAX_PCKT_SIZE];
};

struct usbf_ep_info {
	const char		*name;
	struct usb_ep_caps	caps;
	u16			base_addr;
	unsigned int		is_double : 1;
	u16			maxpacket_limit;
};

#define USBF_SINGLE_BUFFER 0
#define USBF_DOUBLE_BUFFER 1
#define USBF_EP_INFO(_name, _caps, _base_addr, _is_double, _maxpacket_limit)  \
	{                                                                     \
		.name            = _name,                                     \
		.caps            = _caps,                                     \
		.base_addr       = _base_addr,                                \
		.is_double       = _is_double,                                \
		.maxpacket_limit = _maxpacket_limit,                          \
	}

/* This table is computed from the recommended values provided in the SOC
 * datasheet. The buffer type (single/double) and the endpoint type cannot
 * be changed. The mapping in internal RAM (base_addr and number of words)
 * for each endpoints depends on the max packet size and the buffer type.
 */
static const struct usbf_ep_info usbf_ep_info[USBF_NUM_ENDPOINTS] = {
	/* ep0: buf @0x0000 64 bytes, fixed 32 words */
	[0] = USBF_EP_INFO("ep0-ctrl",
			   USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL,
				       USB_EP_CAPS_DIR_ALL),
			   0x0000, USBF_SINGLE_BUFFER, USBF_EP0_MAX_PCKT_SIZE),
	/* ep1: buf @0x0020, 2 buffers 512 bytes -> (512 * 2 / 4) words */
	[1] = USBF_EP_INFO("ep1-bulk",
			   USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
				       USB_EP_CAPS_DIR_ALL),
			   0x0020, USBF_DOUBLE_BUFFER, 512),
	/* ep2: buf @0x0120, 2 buffers 512 bytes -> (512 * 2 / 4) words */
	[2] = USBF_EP_INFO("ep2-bulk",
			   USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
				       USB_EP_CAPS_DIR_ALL),
			   0x0120, USBF_DOUBLE_BUFFER, 512),
	/* ep3: buf @0x0220, 1 buffer 512 bytes -> (512 * 2 / 4) words */
	[3] = USBF_EP_INFO("ep3-bulk",
			   USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
				       USB_EP_CAPS_DIR_ALL),
			   0x0220, USBF_SINGLE_BUFFER, 512),
	/* ep4: buf @0x02A0, 1 buffer 512 bytes -> (512 * 1 / 4) words */
	[4] = USBF_EP_INFO("ep4-bulk",
			   USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
				       USB_EP_CAPS_DIR_ALL),
			   0x02A0, USBF_SINGLE_BUFFER, 512),
	/* ep5: buf @0x0320, 1 buffer 512 bytes -> (512 * 2 / 4) words */
	[5] = USBF_EP_INFO("ep5-bulk",
			   USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
				       USB_EP_CAPS_DIR_ALL),
			   0x0320, USBF_SINGLE_BUFFER, 512),
	/* ep6: buf @0x03A0, 1 buffer 1024 bytes -> (1024 * 1 / 4) words */
	[6] = USBF_EP_INFO("ep6-int",
			   USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
				       USB_EP_CAPS_DIR_ALL),
			   0x03A0, USBF_SINGLE_BUFFER, 1024),
	/* ep7: buf @0x04A0, 1 buffer 1024 bytes -> (1024 * 1 / 4) words */
	[7] = USBF_EP_INFO("ep7-int",
			   USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
				       USB_EP_CAPS_DIR_ALL),
			   0x04A0, USBF_SINGLE_BUFFER, 1024),
	/* ep8: buf @0x0520, 1 buffer 1024 bytes -> (1024 * 1 / 4) words */
	[8] = USBF_EP_INFO("ep8-int",
			   USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
				       USB_EP_CAPS_DIR_ALL),
			   0x0520, USBF_SINGLE_BUFFER, 1024),
	/* ep9: buf @0x0620, 1 buffer 1024 bytes -> (1024 * 1 / 4) words */
	[9] = USBF_EP_INFO("ep9-int",
			   USB_EP_CAPS(USB_EP_CAPS_TYPE_INT,
				       USB_EP_CAPS_DIR_ALL),
			   0x0620, USBF_SINGLE_BUFFER, 1024),
	/* ep10: buf @0x0720, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
	[10] = USBF_EP_INFO("ep10-iso",
			    USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
					USB_EP_CAPS_DIR_ALL),
			    0x0720, USBF_DOUBLE_BUFFER, 1024),
	/* ep11: buf @0x0920, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
	[11] = USBF_EP_INFO("ep11-iso",
			    USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
					USB_EP_CAPS_DIR_ALL),
			    0x0920, USBF_DOUBLE_BUFFER, 1024),
	/* ep12: buf @0x0B20, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
	[12] = USBF_EP_INFO("ep12-iso",
			    USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
					USB_EP_CAPS_DIR_ALL),
			    0x0B20, USBF_DOUBLE_BUFFER, 1024),
	/* ep13: buf @0x0D20, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
	[13] = USBF_EP_INFO("ep13-iso",
			    USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
					USB_EP_CAPS_DIR_ALL),
			    0x0D20, USBF_DOUBLE_BUFFER, 1024),
	/* ep14: buf @0x0F20, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
	[14] = USBF_EP_INFO("ep14-iso",
			    USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
					USB_EP_CAPS_DIR_ALL),
			    0x0F20, USBF_DOUBLE_BUFFER, 1024),
	/* ep15: buf @0x1120, 2 buffers 1024 bytes -> (1024 * 2 / 4) words */
	[15] = USBF_EP_INFO("ep15-iso",
			    USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
					USB_EP_CAPS_DIR_ALL),
			    0x1120, USBF_DOUBLE_BUFFER, 1024),
};

static inline u32 usbf_reg_readl(struct usbf_udc *udc, uint offset)
{
	return readl(udc->regs + offset);
}

static inline void usbf_reg_writel(struct usbf_udc *udc, uint offset, u32 val)
{
	writel(val, udc->regs + offset);
}

static inline void usbf_reg_bitset(struct usbf_udc *udc, uint offset, u32 set)
{
	u32 tmp;

	tmp = usbf_reg_readl(udc, offset);
	tmp |= set;
	usbf_reg_writel(udc, offset, tmp);
}

static inline void usbf_reg_bitclr(struct usbf_udc *udc, uint offset, u32 clr)
{
	u32 tmp;

	tmp = usbf_reg_readl(udc, offset);
	tmp &= ~clr;
	usbf_reg_writel(udc, offset, tmp);
}

static inline void usbf_reg_clrset(struct usbf_udc *udc, uint offset,
				   u32 clr, u32 set)
{
	u32 tmp;

	tmp = usbf_reg_readl(udc, offset);
	tmp &= ~clr;
	tmp |= set;
	usbf_reg_writel(udc, offset, tmp);
}

static inline u32 usbf_ep_reg_readl(struct usbf_ep *ep, uint offset)
{
	return readl(ep->regs + offset);
}

static inline void usbf_ep_reg_read_rep(struct usbf_ep *ep, uint offset,
				       void *dst, uint count)
{
	readsl(ep->regs + offset, dst, count);
}

static inline void usbf_ep_reg_writel(struct usbf_ep *ep, uint offset, u32 val)
{
	writel(val, ep->regs + offset);
}

static inline void usbf_ep_reg_write_rep(struct usbf_ep *ep, uint offset,
					 const void *src, uint count)
{
	writesl(ep->regs + offset, src, count);
}

static inline void usbf_ep_reg_bitset(struct usbf_ep *ep, uint offset, u32 set)
{
	u32 tmp;

	tmp = usbf_ep_reg_readl(ep, offset);
	tmp |= set;
	usbf_ep_reg_writel(ep, offset, tmp);
}

static inline void usbf_ep_reg_bitclr(struct usbf_ep *ep, uint offset, u32 clr)
{
	u32 tmp;

	tmp = usbf_ep_reg_readl(ep, offset);
	tmp &= ~clr;
	usbf_ep_reg_writel(ep, offset, tmp);
}

static inline void usbf_ep_reg_clrset(struct usbf_ep *ep, uint offset,
				      u32 clr, u32 set)
{
	u32 tmp;

	tmp = usbf_ep_reg_readl(ep, offset);
	tmp &= ~clr;
	tmp |= set;
	usbf_ep_reg_writel(ep, offset, tmp);
}

static inline u32 usbf_ep_dma_reg_readl(struct usbf_ep *ep, uint offset)
{
	return readl(ep->dma_regs + offset);
}

static inline void usbf_ep_dma_reg_writel(struct usbf_ep *ep, uint offset,
					  u32 val)
{
	writel(val, ep->dma_regs + offset);
}

static inline void usbf_ep_dma_reg_bitset(struct usbf_ep *ep, uint offset,
					  u32 set)
{
	u32 tmp;

	tmp = usbf_ep_dma_reg_readl(ep, offset);
	tmp |= set;
	usbf_ep_dma_reg_writel(ep, offset, tmp);
}

static inline void usbf_ep_dma_reg_bitclr(struct usbf_ep *ep, uint offset,
					  u32 clr)
{
	u32 tmp;

	tmp = usbf_ep_dma_reg_readl(ep, offset);
	tmp &= ~clr;
	usbf_ep_dma_reg_writel(ep, offset, tmp);
}

static void usbf_ep0_send_null(struct usbf_ep *ep0, bool is_data1)
{
	u32 set;

	set = USBF_EP0_DEND;
	if (is_data1)
		set |= USBF_EP0_PIDCLR;

	usbf_ep_reg_bitset(ep0, USBF_REG_EP0_CONTROL, set);
}

static int usbf_ep0_pio_in(struct usbf_ep *ep0, struct usbf_req *req)
{
	unsigned int left;
	unsigned int nb;
	const void *buf;
	u32 ctrl;
	u32 last;

	left = req->req.length - req->req.actual;

	if (left == 0) {
		if (!req->is_zero_sent) {
			if (req->req.length == 0) {
				dev_dbg(ep0->udc->dev, "ep0 send null\n");
				usbf_ep0_send_null(ep0, false);
				req->is_zero_sent = 1;
				return -EINPROGRESS;
			}
			if ((req->req.actual % ep0->ep.maxpacket) == 0) {
				if (req->req.zero) {
					dev_dbg(ep0->udc->dev, "ep0 send null\n");
					usbf_ep0_send_null(ep0, false);
					req->is_zero_sent = 1;
					return -EINPROGRESS;
				}
			}
		}
		return 0;
	}

	if (left > ep0->ep.maxpacket)
		left = ep0->ep.maxpacket;

	buf = req->req.buf;
	buf += req->req.actual;

	nb = left / sizeof(u32);
	if (nb) {
		usbf_ep_reg_write_rep(ep0, USBF_REG_EP0_WRITE, buf, nb);
		buf += (nb * sizeof(u32));
		req->req.actual += (nb * sizeof(u32));
		left -= (nb * sizeof(u32));
	}
	ctrl = usbf_ep_reg_readl(ep0, USBF_REG_EP0_CONTROL);
	ctrl &= ~USBF_EP0_DW_MASK;
	if (left) {
		memcpy(&last, buf, left);
		usbf_ep_reg_writel(ep0, USBF_REG_EP0_WRITE, last);
		ctrl |= USBF_EP0_DW(left);
		req->req.actual += left;
	}
	usbf_ep_reg_writel(ep0, USBF_REG_EP0_CONTROL, ctrl | USBF_EP0_DEND);

	dev_dbg(ep0->udc->dev, "ep0 send %u/%u\n",
		req->req.actual, req->req.length);

	return -EINPROGRESS;
}

static int usbf_ep0_pio_out(struct usbf_ep *ep0, struct usbf_req *req)
{
	int req_status = 0;
	unsigned int count;
	unsigned int recv;
	unsigned int left;
	unsigned int nb;
	void *buf;
	u32 last;

	if (ep0->status & USBF_EP0_OUT_INT) {
		recv = usbf_ep_reg_readl(ep0, USBF_REG_EP0_LENGTH) & USBF_EP0_LDATA;
		count = recv;

		buf = req->req.buf;
		buf += req->req.actual;

		left = req->req.length - req->req.actual;

		dev_dbg(ep0->udc->dev, "ep0 recv %u, left %u\n", count, left);

		if (left > ep0->ep.maxpacket)
			left = ep0->ep.maxpacket;

		if (count > left) {
			req_status = -EOVERFLOW;
			count = left;
		}

		if (count) {
			nb = count / sizeof(u32);
			if (nb) {
				usbf_ep_reg_read_rep(ep0, USBF_REG_EP0_READ,
					buf, nb);
				buf += (nb * sizeof(u32));
				req->req.actual += (nb * sizeof(u32));
				count -= (nb * sizeof(u32));
			}
			if (count) {
				last = usbf_ep_reg_readl(ep0, USBF_REG_EP0_READ);
				memcpy(buf, &last, count);
				req->req.actual += count;
			}
		}
		dev_dbg(ep0->udc->dev, "ep0 recv %u/%u\n",
			req->req.actual, req->req.length);

		if (req_status) {
			dev_dbg(ep0->udc->dev, "ep0 req.status=%d\n", req_status);
			req->req.status = req_status;
			return 0;
		}

		if (recv < ep0->ep.maxpacket) {
			dev_dbg(ep0->udc->dev, "ep0 short packet\n");
			/* This is a short packet -> It is the end */
			req->req.status = 0;
			return 0;
		}

		/* The Data stage of a control transfer from an endpoint to the
		 * host is complete when the endpoint does one of the following:
		 * - Has transferred exactly the expected amount of data
		 * - Transfers a packet with a payload size less than
		 *   wMaxPacketSize or transfers a zero-length packet
		 */
		if (req->req.actual == req->req.length) {
			req->req.status = 0;
			return 0;
		}
	}

	if (ep0->status & USBF_EP0_OUT_NULL_INT) {
		/* NULL packet received */
		dev_dbg(ep0->udc->dev, "ep0 null packet\n");
		if (req->req.actual != req->req.length) {
			req->req.status = req->req.short_not_ok ?
					  -EREMOTEIO : 0;
		} else {
			req->req.status = 0;
		}
		return 0;
	}

	return -EINPROGRESS;
}

static void usbf_ep0_fifo_flush(struct usbf_ep *ep0)
{
	u32 sts;
	int ret;

	usbf_ep_reg_bitset(ep0, USBF_REG_EP0_CONTROL, USBF_EP0_BCLR);

	ret = readl_poll_timeout_atomic(ep0->regs + USBF_REG_EP0_STATUS, sts,
		(sts & (USBF_EP0_IN_DATA | USBF_EP0_IN_EMPTY)) == USBF_EP0_IN_EMPTY,
		0,  10000);
	if (ret)
		dev_err(ep0->udc->dev, "ep0 flush fifo timed out\n");

}

static void usbf_epn_send_null(struct usbf_ep *epn)
{
	usbf_ep_reg_bitset(epn, USBF_REG_EPN_CONTROL, USBF_EPN_DEND);
}

static void usbf_epn_send_residue(struct usbf_ep *epn, const void *buf,
				  unsigned int size)
{
	u32 tmp;

	memcpy(&tmp, buf, size);
	usbf_ep_reg_writel(epn, USBF_REG_EPN_WRITE, tmp);

	usbf_ep_reg_clrset(epn, USBF_REG_EPN_CONTROL,
				USBF_EPN_DW_MASK,
				USBF_EPN_DW(size) | USBF_EPN_DEND);
}

static int usbf_epn_pio_in(struct usbf_ep *epn, struct usbf_req *req)
{
	unsigned int left;
	unsigned int nb;
	const void *buf;

	left = req->req.length - req->req.actual;

	if (left == 0) {
		if (!req->is_zero_sent) {
			if (req->req.length == 0) {
				dev_dbg(epn->udc->dev, "ep%u send_null\n", epn->id);
				usbf_epn_send_null(epn);
				req->is_zero_sent = 1;
				return -EINPROGRESS;
			}
			if ((req->req.actual % epn->ep.maxpacket) == 0) {
				if (req->req.zero) {
					dev_dbg(epn->udc->dev, "ep%u send_null\n",
						epn->id);
					usbf_epn_send_null(epn);
					req->is_zero_sent = 1;
					return -EINPROGRESS;
				}
			}
		}
		return 0;
	}

	if (left > epn->ep.maxpacket)
		left = epn->ep.maxpacket;

	buf = req->req.buf;
	buf += req->req.actual;

	nb = left / sizeof(u32);
	if (nb) {
		usbf_ep_reg_write_rep(epn, USBF_REG_EPN_WRITE, buf, nb);
		buf += (nb * sizeof(u32));
		req->req.actual += (nb * sizeof(u32));
		left -= (nb * sizeof(u32));
	}

	if (left) {
		usbf_epn_send_residue(epn, buf, left);
		req->req.actual += left;
	} else {
		usbf_ep_reg_clrset(epn, USBF_REG_EPN_CONTROL,
					USBF_EPN_DW_MASK,
					USBF_EPN_DEND);
	}

	dev_dbg(epn->udc->dev, "ep%u send %u/%u\n", epn->id, req->req.actual,
		req->req.length);

	return -EINPROGRESS;
}

static void usbf_epn_enable_in_end_int(struct usbf_ep *epn)
{
	usbf_ep_reg_bitset(epn, USBF_REG_EPN_INT_ENA, USBF_EPN_IN_END_EN);
}

static int usbf_epn_dma_in(struct usbf_ep *epn, struct usbf_req *req)
{
	unsigned int left;
	u32 npkt;
	u32 lastpkt;
	int ret;

	if (!IS_ALIGNED((uintptr_t)req->req.buf, 4)) {
		dev_dbg(epn->udc->dev, "ep%u buf unaligned -> fallback pio\n",
			epn->id);
		return usbf_epn_pio_in(epn, req);
	}

	left = req->req.length - req->req.actual;

	switch (req->xfer_step) {
	default:
	case USBF_XFER_START:
		if (left == 0) {
			dev_dbg(epn->udc->dev, "ep%u send null\n", epn->id);
			usbf_epn_send_null(epn);
			req->xfer_step = USBF_XFER_WAIT_END;
			break;
		}
		if (left < 4) {
			dev_dbg(epn->udc->dev, "ep%u send residue %u\n", epn->id,
				left);
			usbf_epn_send_residue(epn,
				req->req.buf + req->req.actual, left);
			req->req.actual += left;
			req->xfer_step = USBF_XFER_WAIT_END;
			break;
		}

		ret = usb_gadget_map_request(&epn->udc->gadget, &req->req, 1);
		if (ret < 0) {
			dev_err(epn->udc->dev, "usb_gadget_map_request failed (%d)\n",
				ret);
			return ret;
		}
		req->is_mapped = 1;

		npkt = DIV_ROUND_UP(left, epn->ep.maxpacket);
		lastpkt = (left % epn->ep.maxpacket);
		if (lastpkt == 0)
			lastpkt = epn->ep.maxpacket;
		lastpkt &= ~0x3; /* DMA is done on 32bit units */

		usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_DCR2,
			USBF_SYS_EPN_MPKT(epn->ep.maxpacket) | USBF_SYS_EPN_LMPKT(lastpkt));
		usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_TADR,
			req->req.dma);
		usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_DCR1,
			USBF_SYS_EPN_SET_DMACNT(npkt));
		usbf_ep_dma_reg_bitset(epn, USBF_REG_DMA_EPN_DCR1,
			USBF_SYS_EPN_REQEN);

		usbf_ep_reg_writel(epn, USBF_REG_EPN_LEN_DCNT, USBF_EPN_SET_DMACNT(npkt));

		usbf_ep_reg_bitset(epn, USBF_REG_EPN_CONTROL, USBF_EPN_AUTO);

		/* The end of DMA transfer at the USBF level needs to be handle
		 * after the detection of the end of DMA transfer at the brige
		 * level.
		 * To force this sequence, EPN_IN_END_EN will be set by the
		 * detection of the end of transfer at bridge level (ie. bridge
		 * interrupt).
		 */
		usbf_ep_reg_bitclr(epn, USBF_REG_EPN_INT_ENA,
			USBF_EPN_IN_EN | USBF_EPN_IN_END_EN);
		epn->bridge_on_dma_end = usbf_epn_enable_in_end_int;

		/* Clear any pending IN_END interrupt */
		usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS, ~(u32)USBF_EPN_IN_END_INT);

		usbf_ep_reg_writel(epn, USBF_REG_EPN_DMA_CTRL,
			USBF_EPN_BURST_SET | USBF_EPN_DMAMODE0);
		usbf_ep_reg_bitset(epn, USBF_REG_EPN_DMA_CTRL,
			USBF_EPN_DMA_EN);

		req->dma_size = (npkt - 1) * epn->ep.maxpacket + lastpkt;

		dev_dbg(epn->udc->dev, "ep%u dma xfer %zu\n", epn->id,
			req->dma_size);

		req->xfer_step = USBF_XFER_WAIT_DMA;
		break;

	case USBF_XFER_WAIT_DMA:
		if (!(epn->status & USBF_EPN_IN_END_INT)) {
			dev_dbg(epn->udc->dev, "ep%u dma not done\n", epn->id);
			break;
		}
		dev_dbg(epn->udc->dev, "ep%u dma done\n", epn->id);

		usb_gadget_unmap_request(&epn->udc->gadget, &req->req, 1);
		req->is_mapped = 0;

		usbf_ep_reg_bitclr(epn, USBF_REG_EPN_CONTROL, USBF_EPN_AUTO);

		usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
			USBF_EPN_IN_END_EN,
			USBF_EPN_IN_EN);

		req->req.actual += req->dma_size;

		left = req->req.length - req->req.actual;
		if (left) {
			usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS, ~(u32)USBF_EPN_IN_INT);

			dev_dbg(epn->udc->dev, "ep%u send residue %u\n", epn->id,
				left);
			usbf_epn_send_residue(epn,
				req->req.buf + req->req.actual, left);
			req->req.actual += left;
			req->xfer_step = USBF_XFER_WAIT_END;
			break;
		}

		if (req->req.actual % epn->ep.maxpacket) {
			/* last packet was a short packet. Tell the hardware to
			 * send it right now.
			 */
			dev_dbg(epn->udc->dev, "ep%u send short\n", epn->id);
			usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS,
				~(u32)USBF_EPN_IN_INT);
			usbf_ep_reg_bitset(epn, USBF_REG_EPN_CONTROL,
				USBF_EPN_DEND);

			req->xfer_step = USBF_XFER_WAIT_END;
			break;
		}

		/* Last packet size was a maxpacket size
		 * Send null packet if needed
		 */
		if (req->req.zero) {
			req->xfer_step = USBF_XFER_SEND_NULL;
			break;
		}

		/* No more action to do. Wait for the end of the USB transfer */
		req->xfer_step = USBF_XFER_WAIT_END;
		break;

	case USBF_XFER_SEND_NULL:
		dev_dbg(epn->udc->dev, "ep%u send null\n", epn->id);
		usbf_epn_send_null(epn);
		req->xfer_step = USBF_XFER_WAIT_END;
		break;

	case USBF_XFER_WAIT_END:
		if (!(epn->status & USBF_EPN_IN_INT)) {
			dev_dbg(epn->udc->dev, "ep%u end not done\n", epn->id);
			break;
		}
		dev_dbg(epn->udc->dev, "ep%u send done %u/%u\n", epn->id,
			req->req.actual, req->req.length);
		req->xfer_step = USBF_XFER_START;
		return 0;
	}

	return -EINPROGRESS;
}

static void usbf_epn_recv_residue(struct usbf_ep *epn, void *buf,
				  unsigned int size)
{
	u32 last;

	last = usbf_ep_reg_readl(epn, USBF_REG_EPN_READ);
	memcpy(buf, &last, size);
}

static int usbf_epn_pio_out(struct usbf_ep *epn, struct usbf_req *req)
{
	int req_status = 0;
	unsigned int count;
	unsigned int recv;
	unsigned int left;
	unsigned int nb;
	void *buf;

	if (epn->status & USBF_EPN_OUT_INT) {
		recv = USBF_EPN_GET_LDATA(
			usbf_ep_reg_readl(epn, USBF_REG_EPN_LEN_DCNT));
		count = recv;

		buf = req->req.buf;
		buf += req->req.actual;

		left = req->req.length - req->req.actual;

		dev_dbg(epn->udc->dev, "ep%u recv %u, left %u, mpkt %u\n", epn->id,
			recv, left, epn->ep.maxpacket);

		if (left > epn->ep.maxpacket)
			left = epn->ep.maxpacket;

		if (count > left) {
			req_status = -EOVERFLOW;
			count = left;
		}

		if (count) {
			nb = count / sizeof(u32);
			if (nb) {
				usbf_ep_reg_read_rep(epn, USBF_REG_EPN_READ,
					buf, nb);
				buf += (nb * sizeof(u32));
				req->req.actual += (nb * sizeof(u32));
				count -= (nb * sizeof(u32));
			}
			if (count) {
				usbf_epn_recv_residue(epn, buf, count);
				req->req.actual += count;
			}
		}
		dev_dbg(epn->udc->dev, "ep%u recv %u/%u\n", epn->id,
			req->req.actual, req->req.length);

		if (req_status) {
			dev_dbg(epn->udc->dev, "ep%u req.status=%d\n", epn->id,
				req_status);
			req->req.status = req_status;
			return 0;
		}

		if (recv < epn->ep.maxpacket) {
			dev_dbg(epn->udc->dev, "ep%u short packet\n", epn->id);
			/* This is a short packet -> It is the end */
			req->req.status = 0;
			return 0;
		}

		/* Request full -> complete */
		if (req->req.actual == req->req.length) {
			req->req.status = 0;
			return 0;
		}
	}

	if (epn->status & USBF_EPN_OUT_NULL_INT) {
		/* NULL packet received */
		dev_dbg(epn->udc->dev, "ep%u null packet\n", epn->id);
		if (req->req.actual != req->req.length) {
			req->req.status = req->req.short_not_ok ?
					  -EREMOTEIO : 0;
		} else {
			req->req.status = 0;
		}
		return 0;
	}

	return -EINPROGRESS;
}

static void usbf_epn_enable_out_end_int(struct usbf_ep *epn)
{
	usbf_ep_reg_bitset(epn, USBF_REG_EPN_INT_ENA, USBF_EPN_OUT_END_EN);
}

static void usbf_epn_process_queue(struct usbf_ep *epn);

static void usbf_epn_dma_out_send_dma(struct usbf_ep *epn, dma_addr_t addr, u32 npkt, bool is_short)
{
	usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_DCR2, USBF_SYS_EPN_MPKT(epn->ep.maxpacket));
	usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_TADR, addr);

	if (is_short) {
		usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_DCR1,
				USBF_SYS_EPN_SET_DMACNT(1) | USBF_SYS_EPN_DIR0);
		usbf_ep_dma_reg_bitset(epn, USBF_REG_DMA_EPN_DCR1,
				USBF_SYS_EPN_REQEN);

		usbf_ep_reg_writel(epn, USBF_REG_EPN_LEN_DCNT,
				USBF_EPN_SET_DMACNT(0));

		/* The end of DMA transfer at the USBF level needs to be handled
		 * after the detection of the end of DMA transfer at the brige
		 * level.
		 * To force this sequence, enabling the OUT_END interrupt will
		 * be donee by the detection of the end of transfer at bridge
		 * level (ie. bridge interrupt).
		 */
		usbf_ep_reg_bitclr(epn, USBF_REG_EPN_INT_ENA,
			USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN | USBF_EPN_OUT_END_EN);
		epn->bridge_on_dma_end = usbf_epn_enable_out_end_int;

		/* Clear any pending OUT_END interrupt */
		usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS,
			~(u32)USBF_EPN_OUT_END_INT);

		usbf_ep_reg_writel(epn, USBF_REG_EPN_DMA_CTRL,
			USBF_EPN_STOP_MODE | USBF_EPN_STOP_SET | USBF_EPN_DMAMODE0);
		usbf_ep_reg_bitset(epn, USBF_REG_EPN_DMA_CTRL,
			USBF_EPN_DMA_EN);
		return;
	}

	usbf_ep_dma_reg_writel(epn, USBF_REG_DMA_EPN_DCR1,
		USBF_SYS_EPN_SET_DMACNT(npkt) | USBF_SYS_EPN_DIR0);
	usbf_ep_dma_reg_bitset(epn, USBF_REG_DMA_EPN_DCR1,
		USBF_SYS_EPN_REQEN);

	usbf_ep_reg_writel(epn, USBF_REG_EPN_LEN_DCNT,
		USBF_EPN_SET_DMACNT(npkt));

	/* Here, the bridge may or may not generate an interrupt to signal the
	 * end of DMA transfer.
	 * Keep only OUT_END interrupt and let handle the bridge later during
	 * the OUT_END processing.
	 */
	usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
		USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN,
		USBF_EPN_OUT_END_EN);

	/* Disable bridge interrupt. It will be renabled later */
	usbf_reg_bitclr(epn->udc, USBF_REG_AHBBINTEN,
		USBF_SYS_DMA_ENDINTEN_EPN(epn->id));

	/* Clear any pending DMA_END interrupt at bridge level */
	usbf_reg_writel(epn->udc, USBF_REG_AHBBINT,
		USBF_SYS_DMA_ENDINT_EPN(epn->id));

	/* Clear any pending OUT_END interrupt */
	usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS,
		~(u32)USBF_EPN_OUT_END_INT);

	usbf_ep_reg_writel(epn, USBF_REG_EPN_DMA_CTRL,
		USBF_EPN_STOP_MODE | USBF_EPN_STOP_SET | USBF_EPN_DMAMODE0 | USBF_EPN_BURST_SET);
	usbf_ep_reg_bitset(epn, USBF_REG_EPN_DMA_CTRL,
		USBF_EPN_DMA_EN);
}

static size_t usbf_epn_dma_out_complete_dma(struct usbf_ep *epn, bool is_short)
{
	u32 dmacnt;
	u32 tmp;
	int ret;

	/* Restore interrupt mask */
	usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
		USBF_EPN_OUT_END_EN,
		USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN);

	if (is_short) {
		/* Nothing more to do when the DMA was for a short packet */
		return 0;
	}

	/* Enable the bridge interrupt */
	usbf_reg_bitset(epn->udc, USBF_REG_AHBBINTEN,
		USBF_SYS_DMA_ENDINTEN_EPN(epn->id));

	tmp = usbf_ep_reg_readl(epn, USBF_REG_EPN_LEN_DCNT);
	dmacnt = USBF_EPN_GET_DMACNT(tmp);

	if (dmacnt) {
		/* Some packet were not received (halted by a short or a null
		 * packet.
		 * The bridge never raises an interrupt in this case.
		 * Wait for the end of transfer at bridge level
		 */
		ret = readl_poll_timeout_atomic(
			epn->dma_regs + USBF_REG_DMA_EPN_DCR1,
			tmp, (USBF_SYS_EPN_GET_DMACNT(tmp) == dmacnt),
			0,  10000);
		if (ret) {
			dev_err(epn->udc->dev, "ep%u wait bridge timed out\n",
				epn->id);
		}

		usbf_ep_dma_reg_bitclr(epn, USBF_REG_DMA_EPN_DCR1,
			USBF_SYS_EPN_REQEN);

		/* The dmacnt value tells how many packet were not transferred
		 * from the maximum number of packet we set for the DMA transfer.
		 * Compute the left DMA size based on this value.
		 */
		return dmacnt * epn->ep.maxpacket;
	}

	return 0;
}

static int usbf_epn_dma_out(struct usbf_ep *epn, struct usbf_req *req)
{
	unsigned int dma_left;
	unsigned int count;
	unsigned int recv;
	unsigned int left;
	u32 npkt;
	int ret;

	if (!IS_ALIGNED((uintptr_t)req->req.buf, 4)) {
		dev_dbg(epn->udc->dev, "ep%u buf unaligned -> fallback pio\n",
			epn->id);
		return usbf_epn_pio_out(epn, req);
	}

	switch (req->xfer_step) {
	default:
	case USBF_XFER_START:
		if (epn->status & USBF_EPN_OUT_NULL_INT) {
			dev_dbg(epn->udc->dev, "ep%u null packet\n", epn->id);
			if (req->req.actual != req->req.length) {
				req->req.status = req->req.short_not_ok ?
					-EREMOTEIO : 0;
			} else {
				req->req.status = 0;
			}
			return 0;
		}

		if (!(epn->status & USBF_EPN_OUT_INT)) {
			dev_dbg(epn->udc->dev, "ep%u OUT_INT not set -> spurious\n",
				epn->id);
			break;
		}

		recv = USBF_EPN_GET_LDATA(
			usbf_ep_reg_readl(epn, USBF_REG_EPN_LEN_DCNT));
		if (!recv) {
			dev_dbg(epn->udc->dev, "ep%u recv = 0 -> spurious\n",
				epn->id);
			break;
		}

		left = req->req.length - req->req.actual;

		dev_dbg(epn->udc->dev, "ep%u recv %u, left %u, mpkt %u\n", epn->id,
			recv, left, epn->ep.maxpacket);

		if (recv > left) {
			dev_err(epn->udc->dev, "ep%u overflow (%u/%u)\n",
				epn->id, recv, left);
			req->req.status = -EOVERFLOW;
			return -EOVERFLOW;
		}

		if (recv < epn->ep.maxpacket) {
			/* Short packet received */
			dev_dbg(epn->udc->dev, "ep%u short packet\n", epn->id);
			if (recv <= 3) {
				usbf_epn_recv_residue(epn,
					req->req.buf + req->req.actual, recv);
				req->req.actual += recv;

				dev_dbg(epn->udc->dev, "ep%u recv done %u/%u\n",
					epn->id, req->req.actual, req->req.length);

				req->xfer_step = USBF_XFER_START;
				return 0;
			}

			ret = usb_gadget_map_request(&epn->udc->gadget, &req->req, 0);
			if (ret < 0) {
				dev_err(epn->udc->dev, "map request failed (%d)\n",
					ret);
				return ret;
			}
			req->is_mapped = 1;

			usbf_epn_dma_out_send_dma(epn,
				req->req.dma + req->req.actual,
				1, true);
			req->dma_size = recv & ~0x3;

			dev_dbg(epn->udc->dev, "ep%u dma short xfer %zu\n", epn->id,
				req->dma_size);

			req->xfer_step = USBF_XFER_WAIT_DMA_SHORT;
			break;
		}

		ret = usb_gadget_map_request(&epn->udc->gadget, &req->req, 0);
		if (ret < 0) {
			dev_err(epn->udc->dev, "map request failed (%d)\n",
				ret);
			return ret;
		}
		req->is_mapped = 1;

		/* Use the maximum DMA size according to the request buffer.
		 * We will adjust the received size later at the end of the DMA
		 * transfer with the left size computed from
		 * usbf_epn_dma_out_complete_dma().
		 */
		npkt = left / epn->ep.maxpacket;
		usbf_epn_dma_out_send_dma(epn,
				req->req.dma + req->req.actual,
				npkt, false);
		req->dma_size = npkt * epn->ep.maxpacket;

		dev_dbg(epn->udc->dev, "ep%u dma xfer %zu (%u)\n", epn->id,
			req->dma_size, npkt);

		req->xfer_step = USBF_XFER_WAIT_DMA;
		break;

	case USBF_XFER_WAIT_DMA_SHORT:
		if (!(epn->status & USBF_EPN_OUT_END_INT)) {
			dev_dbg(epn->udc->dev, "ep%u dma short not done\n", epn->id);
			break;
		}
		dev_dbg(epn->udc->dev, "ep%u dma short done\n", epn->id);

		usbf_epn_dma_out_complete_dma(epn, true);

		usb_gadget_unmap_request(&epn->udc->gadget, &req->req, 0);
		req->is_mapped = 0;

		req->req.actual += req->dma_size;

		recv = USBF_EPN_GET_LDATA(
			usbf_ep_reg_readl(epn, USBF_REG_EPN_LEN_DCNT));

		count = recv & 0x3;
		if (count) {
			dev_dbg(epn->udc->dev, "ep%u recv residue %u\n", epn->id,
				count);
			usbf_epn_recv_residue(epn,
				req->req.buf + req->req.actual, count);
			req->req.actual += count;
		}

		dev_dbg(epn->udc->dev, "ep%u recv done %u/%u\n", epn->id,
			req->req.actual, req->req.length);

		req->xfer_step = USBF_XFER_START;
		return 0;

	case USBF_XFER_WAIT_DMA:
		if (!(epn->status & USBF_EPN_OUT_END_INT)) {
			dev_dbg(epn->udc->dev, "ep%u dma not done\n", epn->id);
			break;
		}
		dev_dbg(epn->udc->dev, "ep%u dma done\n", epn->id);

		dma_left = usbf_epn_dma_out_complete_dma(epn, false);
		if (dma_left) {
			/* Adjust the final DMA size with */
			count = req->dma_size - dma_left;

			dev_dbg(epn->udc->dev, "ep%u dma xfer done %u\n", epn->id,
				count);

			req->req.actual += count;

			if (epn->status & USBF_EPN_OUT_NULL_INT) {
				/* DMA was stopped by a null packet reception */
				dev_dbg(epn->udc->dev, "ep%u dma stopped by null pckt\n",
					epn->id);
				usb_gadget_unmap_request(&epn->udc->gadget,
							 &req->req, 0);
				req->is_mapped = 0;

				usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS,
					~(u32)USBF_EPN_OUT_NULL_INT);

				if (req->req.actual != req->req.length) {
					req->req.status = req->req.short_not_ok ?
						  -EREMOTEIO : 0;
				} else {
					req->req.status = 0;
				}
				dev_dbg(epn->udc->dev, "ep%u recv done %u/%u\n",
					epn->id, req->req.actual, req->req.length);
				req->xfer_step = USBF_XFER_START;
				return 0;
			}

			recv = USBF_EPN_GET_LDATA(
				usbf_ep_reg_readl(epn, USBF_REG_EPN_LEN_DCNT));
			left = req->req.length - req->req.actual;
			if (recv > left) {
				dev_err(epn->udc->dev,
					"ep%u overflow (%u/%u)\n", epn->id,
					recv, left);
				req->req.status = -EOVERFLOW;
				usb_gadget_unmap_request(&epn->udc->gadget,
							 &req->req, 0);
				req->is_mapped = 0;

				req->xfer_step = USBF_XFER_START;
				return -EOVERFLOW;
			}

			if (recv > 3) {
				usbf_epn_dma_out_send_dma(epn,
					req->req.dma + req->req.actual,
					1, true);
				req->dma_size = recv & ~0x3;

				dev_dbg(epn->udc->dev, "ep%u dma short xfer %zu\n",
					epn->id, req->dma_size);

				req->xfer_step = USBF_XFER_WAIT_DMA_SHORT;
				break;
			}

			usb_gadget_unmap_request(&epn->udc->gadget, &req->req, 0);
			req->is_mapped = 0;

			count = recv & 0x3;
			if (count) {
				dev_dbg(epn->udc->dev, "ep%u recv residue %u\n",
					epn->id, count);
				usbf_epn_recv_residue(epn,
					req->req.buf + req->req.actual, count);
				req->req.actual += count;
			}

			dev_dbg(epn->udc->dev, "ep%u recv done %u/%u\n", epn->id,
				req->req.actual, req->req.length);

			req->xfer_step = USBF_XFER_START;
			return 0;
		}

		/* Process queue at bridge interrupt only */
		usbf_ep_reg_bitclr(epn, USBF_REG_EPN_INT_ENA,
			USBF_EPN_OUT_END_EN | USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN);
		epn->status = 0;
		epn->bridge_on_dma_end = usbf_epn_process_queue;

		req->xfer_step = USBF_XFER_WAIT_BRIDGE;
		break;

	case USBF_XFER_WAIT_BRIDGE:
		dev_dbg(epn->udc->dev, "ep%u bridge transfers done\n", epn->id);

		/* Restore interrupt mask */
		usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
			USBF_EPN_OUT_END_EN,
			USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN);

		usb_gadget_unmap_request(&epn->udc->gadget, &req->req, 0);
		req->is_mapped = 0;

		req->req.actual += req->dma_size;

		req->xfer_step = USBF_XFER_START;
		left = req->req.length - req->req.actual;
		if (!left) {
			/* No more data can be added to the buffer */
			dev_dbg(epn->udc->dev, "ep%u recv done %u/%u\n", epn->id,
				req->req.actual, req->req.length);
			return 0;
		}
		dev_dbg(epn->udc->dev, "ep%u recv done %u/%u, wait more data\n",
			epn->id, req->req.actual, req->req.length);
		break;
	}

	return -EINPROGRESS;
}

static void usbf_epn_dma_stop(struct usbf_ep *epn)
{
	usbf_ep_dma_reg_bitclr(epn, USBF_REG_DMA_EPN_DCR1, USBF_SYS_EPN_REQEN);

	/* In the datasheet:
	 *   If EP[m]_REQEN = 0b is set during DMA transfer, AHB-EPC stops DMA
	 *   after 1 packet transfer completed.
	 *   Therefore, wait sufficient time for ensuring DMA transfer
	 *   completion. The WAIT time depends on the system, especially AHB
	 *   bus activity
	 * So arbitrary 10ms would be sufficient.
	 */
	mdelay(10);

	usbf_ep_reg_bitclr(epn, USBF_REG_EPN_DMA_CTRL, USBF_EPN_DMA_EN);
}

static void usbf_epn_dma_abort(struct usbf_ep *epn,  struct usbf_req *req)
{
	dev_dbg(epn->udc->dev, "ep%u %s dma abort\n", epn->id,
		epn->is_in ? "in" : "out");

	epn->bridge_on_dma_end = NULL;

	usbf_epn_dma_stop(epn);

	usb_gadget_unmap_request(&epn->udc->gadget, &req->req,
				 epn->is_in ? 1 : 0);
	req->is_mapped = 0;

	usbf_ep_reg_bitclr(epn, USBF_REG_EPN_CONTROL, USBF_EPN_AUTO);

	if (epn->is_in) {
		usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
			USBF_EPN_IN_END_EN,
			USBF_EPN_IN_EN);
	} else {
		usbf_ep_reg_clrset(epn, USBF_REG_EPN_INT_ENA,
			USBF_EPN_OUT_END_EN,
			USBF_EPN_OUT_EN | USBF_EPN_OUT_NULL_EN);
	}

	/* As dma is stopped, be sure that no DMA interrupt are pending */
	usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS,
		USBF_EPN_IN_END_INT | USBF_EPN_OUT_END_INT);

	usbf_reg_writel(epn->udc, USBF_REG_AHBBINT, USBF_SYS_DMA_ENDINT_EPN(epn->id));

	/* Enable DMA interrupt the bridge level */
	usbf_reg_bitset(epn->udc, USBF_REG_AHBBINTEN,
		USBF_SYS_DMA_ENDINTEN_EPN(epn->id));

	/* Reset transfer step */
	req->xfer_step = USBF_XFER_START;
}

static void usbf_epn_fifo_flush(struct usbf_ep *epn)
{
	u32 ctrl;
	u32 sts;
	int ret;

	dev_dbg(epn->udc->dev, "ep%u %s fifo flush\n", epn->id,
		epn->is_in ? "in" : "out");

	ctrl = usbf_ep_reg_readl(epn, USBF_REG_EPN_CONTROL);
	usbf_ep_reg_writel(epn, USBF_REG_EPN_CONTROL, ctrl | USBF_EPN_BCLR);

	if (ctrl & USBF_EPN_DIR0)
		return;

	ret = readl_poll_timeout_atomic(epn->regs + USBF_REG_EPN_STATUS, sts,
		(sts & (USBF_EPN_IN_DATA | USBF_EPN_IN_EMPTY)) == USBF_EPN_IN_EMPTY,
		0,  10000);
	if (ret)
		dev_err(epn->udc->dev, "ep%u flush fifo timed out\n", epn->id);
}

static void usbf_ep_req_done(struct usbf_ep *ep, struct usbf_req *req,
			     int status)
{
	list_del_init(&req->queue);

	if (status) {
		req->req.status = status;
	} else {
		if (req->req.status == -EINPROGRESS)
			req->req.status = status;
	}

	dev_dbg(ep->udc->dev, "ep%u %s req done length %u/%u, status=%d\n", ep->id,
		ep->is_in ? "in" : "out",
		req->req.actual, req->req.length, req->req.status);

	if (req->is_mapped)
		usbf_epn_dma_abort(ep, req);

	spin_unlock(&ep->udc->lock);
	usb_gadget_giveback_request(&ep->ep, &req->req);
	spin_lock(&ep->udc->lock);
}

static void usbf_ep_nuke(struct usbf_ep *ep, int status)
{
	struct usbf_req *req;

	dev_dbg(ep->udc->dev, "ep%u %s nuke status %d\n", ep->id,
		ep->is_in ? "in" : "out",
		status);

	while (!list_empty(&ep->queue)) {
		req = list_first_entry(&ep->queue, struct usbf_req, queue);
		usbf_ep_req_done(ep, req, status);
	}

	if (ep->id == 0)
		usbf_ep0_fifo_flush(ep);
	else
		usbf_epn_fifo_flush(ep);
}

static bool usbf_ep_is_stalled(struct usbf_ep *ep)
{
	u32 ctrl;

	if (ep->id == 0) {
		ctrl = usbf_ep_reg_readl(ep, USBF_REG_EP0_CONTROL);
		return (ctrl & USBF_EP0_STL) ? true : false;
	}

	ctrl = usbf_ep_reg_readl(ep, USBF_REG_EPN_CONTROL);
	if (ep->is_in)
		return (ctrl & USBF_EPN_ISTL) ? true : false;

	return (ctrl & USBF_EPN_OSTL) ? true : false;
}

static int usbf_epn_start_queue(struct usbf_ep *epn)
{
	struct usbf_req *req;
	int ret;

	if (usbf_ep_is_stalled(epn))
		return 0;

	req = list_first_entry_or_null(&epn->queue, struct usbf_req, queue);

	if (epn->is_in) {
		if (req && !epn->is_processing) {
			ret = epn->dma_regs ?
				usbf_epn_dma_in(epn, req) :
				usbf_epn_pio_in(epn, req);
			if (ret != -EINPROGRESS) {
				dev_err(epn->udc->dev,
					"queued next request not in progress\n");
					/* The request cannot be completed (ie
					 * ret == 0) on the first call.
					 * stall and nuke the endpoint
					 */
				return ret ? ret : -EIO;
			}
		}
	} else {
		if (req) {
			/* Clear ONAK to accept OUT tokens */
			usbf_ep_reg_bitclr(epn, USBF_REG_EPN_CONTROL,
				USBF_EPN_ONAK);

			/* Enable interrupts */
			usbf_ep_reg_bitset(epn, USBF_REG_EPN_INT_ENA,
				USBF_EPN_OUT_INT | USBF_EPN_OUT_NULL_INT);
		} else {
			/* Disable incoming data and interrupt.
			 * They will be enable on next usb_eb_queue call
			 */
			usbf_ep_reg_bitset(epn, USBF_REG_EPN_CONTROL,
				USBF_EPN_ONAK);
			usbf_ep_reg_bitclr(epn, USBF_REG_EPN_INT_ENA,
				USBF_EPN_OUT_INT | USBF_EPN_OUT_NULL_INT);
		}
	}
	return 0;
}

static int usbf_ep_process_queue(struct usbf_ep *ep)
{
	int (*usbf_ep_xfer)(struct usbf_ep *ep, struct usbf_req *req);
	struct usbf_req *req;
	int is_processing;
	int ret;

	if (ep->is_in) {
		usbf_ep_xfer = usbf_ep0_pio_in;
		if (ep->id) {
			usbf_ep_xfer = ep->dma_regs ?
					usbf_epn_dma_in : usbf_epn_pio_in;
		}
	} else {
		usbf_ep_xfer = usbf_ep0_pio_out;
		if (ep->id) {
			usbf_ep_xfer = ep->dma_regs ?
					usbf_epn_dma_out : usbf_epn_pio_out;
		}
	}

	req = list_first_entry_or_null(&ep->queue, struct usbf_req, queue);
	if (!req) {
		dev_err(ep->udc->dev,
			"no request available for ep%u %s process\n", ep->id,
			ep->is_in ? "in" : "out");
		return -ENOENT;
	}

	do {
		/* Were going to read the FIFO for this current request.
		 * NAK any other incoming data to avoid a race condition if no
		 * more request are available.
		 */
		if (!ep->is_in && ep->id != 0) {
			usbf_ep_reg_bitset(ep, USBF_REG_EPN_CONTROL,
				USBF_EPN_ONAK);
		}

		ret = usbf_ep_xfer(ep, req);
		if (ret == -EINPROGRESS) {
			if (!ep->is_in && ep->id != 0) {
				/* The current request needs more data.
				 * Allow incoming data
				 */
				usbf_ep_reg_bitclr(ep, USBF_REG_EPN_CONTROL,
					USBF_EPN_ONAK);
			}
			return ret;
		}

		is_processing = ep->is_processing;
		ep->is_processing = 1;
		usbf_ep_req_done(ep, req, ret);
		ep->is_processing = is_processing;

		if (ret) {
			/* An error was detected during the request transfer.
			 * Any pending DMA transfers were aborted by the
			 * usbf_ep_req_done() call.
			 * It's time to flush the fifo
			 */
			if (ep->id == 0)
				usbf_ep0_fifo_flush(ep);
			else
				usbf_epn_fifo_flush(ep);
		}

		req = list_first_entry_or_null(&ep->queue, struct usbf_req,
					       queue);

		if (ep->is_in)
			continue;

		if (ep->id != 0) {
			if (req) {
				/* An other request is available.
				 * Allow incoming data
				 */
				usbf_ep_reg_bitclr(ep, USBF_REG_EPN_CONTROL,
					USBF_EPN_ONAK);
			} else {
				/* No request queued. Disable interrupts.
				 * They will be enabled on usb_ep_queue
				 */
				usbf_ep_reg_bitclr(ep, USBF_REG_EPN_INT_ENA,
					USBF_EPN_OUT_INT | USBF_EPN_OUT_NULL_INT);
			}
		}
		/* Do not recall usbf_ep_xfer() */
		return req ? -EINPROGRESS : 0;

	} while (req);

	return 0;
}

static void usbf_ep_stall(struct usbf_ep *ep, bool stall)
{
	struct usbf_req *first;

	dev_dbg(ep->udc->dev, "ep%u %s %s\n", ep->id,
		ep->is_in ? "in" : "out",
		stall ? "stall" : "unstall");

	if (ep->id == 0) {
		if (stall)
			usbf_ep_reg_bitset(ep, USBF_REG_EP0_CONTROL, USBF_EP0_STL);
		else
			usbf_ep_reg_bitclr(ep, USBF_REG_EP0_CONTROL, USBF_EP0_STL);
		return;
	}

	if (stall) {
		if (ep->is_in)
			usbf_ep_reg_bitset(ep, USBF_REG_EPN_CONTROL,
				USBF_EPN_ISTL);
		else
			usbf_ep_reg_bitset(ep, USBF_REG_EPN_CONTROL,
				USBF_EPN_OSTL | USBF_EPN_OSTL_EN);
	} else {
		first = list_first_entry_or_null(&ep->queue, struct usbf_req, queue);
		if (first && first->is_mapped) {
			/* This can appear if the host halts an endpoint using
			 * SET_FEATURE and then un-halts the endpoint
			 */
			usbf_epn_dma_abort(ep, first);
		}
		usbf_epn_fifo_flush(ep);
		if (ep->is_in) {
			usbf_ep_reg_clrset(ep, USBF_REG_EPN_CONTROL,
				USBF_EPN_ISTL,
				USBF_EPN_IPIDCLR);
		} else {
			usbf_ep_reg_clrset(ep, USBF_REG_EPN_CONTROL,
				USBF_EPN_OSTL,
				USBF_EPN_OSTL_EN | USBF_EPN_OPIDCLR);
		}
		usbf_epn_start_queue(ep);
	}
}

static void usbf_ep0_enable(struct usbf_ep *ep0)
{
	usbf_ep_reg_writel(ep0, USBF_REG_EP0_CONTROL, USBF_EP0_INAK_EN | USBF_EP0_BCLR);

	usbf_ep_reg_writel(ep0, USBF_REG_EP0_INT_ENA,
		USBF_EP0_SETUP_EN | USBF_EP0_STG_START_EN | USBF_EP0_STG_END_EN |
		USBF_EP0_OUT_EN | USBF_EP0_OUT_NULL_EN | USBF_EP0_IN_EN);

	ep0->udc->ep0state = EP0_IDLE;
	ep0->disabled = 0;

	/* enable interrupts for the ep0 */
	usbf_reg_bitset(ep0->udc, USBF_REG_USB_INT_ENA, USBF_USB_EPN_EN(0));
}

static int usbf_epn_enable(struct usbf_ep *epn)
{
	u32 base_addr;
	u32 ctrl;

	base_addr = usbf_ep_info[epn->id].base_addr;
	usbf_ep_reg_writel(epn, USBF_REG_EPN_PCKT_ADRS,
		USBF_EPN_BASEAD(base_addr) | USBF_EPN_MPKT(epn->ep.maxpacket));

	/* OUT transfer interrupt are enabled during usb_ep_queue */
	if (epn->is_in) {
		/* Will be changed in DMA processing */
		usbf_ep_reg_writel(epn, USBF_REG_EPN_INT_ENA, USBF_EPN_IN_EN);
	}

	/* Clear, set endpoint direction, set IN/OUT STL, and enable
	 * Send NAK for Data out as request are not queued yet
	 */
	ctrl = USBF_EPN_EN | USBF_EPN_BCLR;
	if (epn->is_in)
		ctrl |= USBF_EPN_OSTL | USBF_EPN_OSTL_EN;
	else
		ctrl |= USBF_EPN_DIR0 | USBF_EPN_ISTL | USBF_EPN_OSTL_EN | USBF_EPN_ONAK;
	usbf_ep_reg_writel(epn, USBF_REG_EPN_CONTROL, ctrl);

	return 0;
}

static int usbf_ep_enable(struct usb_ep *_ep,
			  const struct usb_endpoint_descriptor *desc)
{
	struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
	struct usbf_udc *udc = ep->udc;
	unsigned long flags;
	int ret;

	if (ep->id == 0)
		return -EINVAL;

	if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
		return -EINVAL;

	dev_dbg(ep->udc->dev, "ep%u %s mpkts %d\n", ep->id,
		usb_endpoint_dir_in(desc) ? "in" : "out",
		usb_endpoint_maxp(desc));

	spin_lock_irqsave(&ep->udc->lock, flags);
	ep->is_in = usb_endpoint_dir_in(desc);
	ep->ep.maxpacket = usb_endpoint_maxp(desc);

	ret = usbf_epn_enable(ep);
	if (ret)
		goto end;

	ep->disabled = 0;

	/* enable interrupts for this endpoint */
	usbf_reg_bitset(udc, USBF_REG_USB_INT_ENA, USBF_USB_EPN_EN(ep->id));

	/* enable DMA interrupt at bridge level if DMA is used */
	if (ep->dma_regs) {
		ep->bridge_on_dma_end = NULL;
		usbf_reg_bitset(udc, USBF_REG_AHBBINTEN,
			USBF_SYS_DMA_ENDINTEN_EPN(ep->id));
	}

	ret = 0;
end:
	spin_unlock_irqrestore(&ep->udc->lock, flags);
	return ret;
}

static int usbf_epn_disable(struct usbf_ep *epn)
{
	/* Disable interrupts */
	usbf_ep_reg_writel(epn, USBF_REG_EPN_INT_ENA, 0);

	/* Disable endpoint */
	usbf_ep_reg_bitclr(epn, USBF_REG_EPN_CONTROL, USBF_EPN_EN);

	/* remove anything that was pending */
	usbf_ep_nuke(epn, -ESHUTDOWN);

	return 0;
}

static int usbf_ep_disable(struct usb_ep *_ep)
{
	struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
	struct usbf_udc *udc = ep->udc;
	unsigned long flags;
	int ret;

	if (ep->id == 0)
		return -EINVAL;

	dev_dbg(ep->udc->dev, "ep%u %s mpkts %d\n", ep->id,
		ep->is_in ? "in" : "out", ep->ep.maxpacket);

	spin_lock_irqsave(&ep->udc->lock, flags);
	ep->disabled = 1;
	/* Disable DMA interrupt */
	if (ep->dma_regs) {
		usbf_reg_bitclr(udc, USBF_REG_AHBBINTEN,
			USBF_SYS_DMA_ENDINTEN_EPN(ep->id));
		ep->bridge_on_dma_end = NULL;
	}
	/* disable interrupts for this endpoint */
	usbf_reg_bitclr(udc, USBF_REG_USB_INT_ENA, USBF_USB_EPN_EN(ep->id));
	/* and the endpoint itself */
	ret = usbf_epn_disable(ep);
	spin_unlock_irqrestore(&ep->udc->lock, flags);

	return ret;
}

static int usbf_ep0_queue(struct usbf_ep *ep0, struct usbf_req *req,
			  gfp_t gfp_flags)
{
	int ret;

	req->req.actual = 0;
	req->req.status = -EINPROGRESS;
	req->is_zero_sent = 0;

	list_add_tail(&req->queue, &ep0->queue);

	if (ep0->udc->ep0state == EP0_IN_STATUS_START_PHASE)
		return 0;

	if (!ep0->is_in)
		return 0;

	if (ep0->udc->ep0state == EP0_IN_STATUS_PHASE) {
		if (req->req.length) {
			dev_err(ep0->udc->dev,
				"request lng %u for ep0 in status phase\n",
				req->req.length);
			return -EINVAL;
		}
		ep0->delayed_status = 0;
	}
	if (!ep0->is_processing) {
		ret = usbf_ep0_pio_in(ep0, req);
		if (ret != -EINPROGRESS) {
			dev_err(ep0->udc->dev,
				"queued request not in progress\n");
			/* The request cannot be completed (ie
			 * ret == 0) on the first call
			 */
			return ret ? ret : -EIO;
		}
	}

	return 0;
}

static int usbf_epn_queue(struct usbf_ep *ep, struct usbf_req *req,
			  gfp_t gfp_flags)
{
	int was_empty;
	int ret;

	if (ep->disabled) {
		dev_err(ep->udc->dev, "ep%u request queue while disable\n",
			ep->id);
		return -ESHUTDOWN;
	}

	req->req.actual = 0;
	req->req.status = -EINPROGRESS;
	req->is_zero_sent = 0;
	req->xfer_step = USBF_XFER_START;

	was_empty = list_empty(&ep->queue);
	list_add_tail(&req->queue, &ep->queue);
	if (was_empty) {
		ret = usbf_epn_start_queue(ep);
		if (ret)
			return ret;
	}
	return 0;
}

static int usbf_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
			 gfp_t gfp_flags)
{
	struct usbf_req *req = container_of(_req, struct usbf_req, req);
	struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
	struct usbf_udc *udc = ep->udc;
	unsigned long flags;
	int ret;

	if (!_req || !_req->buf)
		return -EINVAL;

	if (!udc || !udc->driver)
		return -EINVAL;

	dev_dbg(ep->udc->dev, "ep%u %s req queue length %u, zero %u, short_not_ok %u\n",
		ep->id, ep->is_in ? "in" : "out",
		req->req.length, req->req.zero, req->req.short_not_ok);

	spin_lock_irqsave(&ep->udc->lock, flags);
	if (ep->id == 0)
		ret = usbf_ep0_queue(ep, req, gfp_flags);
	else
		ret = usbf_epn_queue(ep, req, gfp_flags);
	spin_unlock_irqrestore(&ep->udc->lock, flags);
	return ret;
}

static int usbf_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
{
	struct usbf_req *req = container_of(_req, struct usbf_req, req);
	struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
	unsigned long flags;
	int is_processing;
	int first;
	int ret;

	spin_lock_irqsave(&ep->udc->lock, flags);

	dev_dbg(ep->udc->dev, "ep%u %s req dequeue length %u/%u\n",
		ep->id, ep->is_in ? "in" : "out",
		req->req.actual, req->req.length);

	first = list_is_first(&req->queue, &ep->queue);

	/* Complete the request but avoid any operation that could be done
	 * if a new request is queued during the request completion
	 */
	is_processing = ep->is_processing;
	ep->is_processing = 1;
	usbf_ep_req_done(ep, req, -ECONNRESET);
	ep->is_processing = is_processing;

	if (first) {
		/* The first item in the list was dequeued.
		 * This item could already be submitted to the hardware.
		 * So, flush the fifo
		 */
		if (ep->id)
			usbf_epn_fifo_flush(ep);
		else
			usbf_ep0_fifo_flush(ep);
	}

	if (ep->id == 0) {
		/* We dequeue a request on ep0. On this endpoint, we can have
		 * 1 request related to the data stage and/or 1 request
		 * related to the status stage.
		 * We dequeue one of them and so the USB control transaction
		 * is no more coherent. The simple way to be consistent after
		 * dequeuing is to stall and nuke the endpoint and wait the
		 * next SETUP packet.
		 */
		usbf_ep_stall(ep, true);
		usbf_ep_nuke(ep, -ECONNRESET);
		ep->udc->ep0state = EP0_IDLE;
		goto end;
	}

	if (!first)
		goto end;

	ret = usbf_epn_start_queue(ep);
	if (ret) {
		usbf_ep_stall(ep, true);
		usbf_ep_nuke(ep, -EIO);
	}
end:
	spin_unlock_irqrestore(&ep->udc->lock, flags);
	return 0;
}

static struct usb_request *usbf_ep_alloc_request(struct usb_ep *_ep,
						 gfp_t gfp_flags)
{
	struct usbf_req *req;

	if (!_ep)
		return NULL;

	req = kzalloc(sizeof(*req), gfp_flags);
	if (!req)
		return NULL;

	INIT_LIST_HEAD(&req->queue);

	return &req->req;
}

static void usbf_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
{
	struct usbf_req *req;
	unsigned long flags;
	struct usbf_ep *ep;

	if (!_ep || !_req)
		return;

	req = container_of(_req, struct usbf_req, req);
	ep = container_of(_ep, struct usbf_ep, ep);

	spin_lock_irqsave(&ep->udc->lock, flags);
	list_del_init(&req->queue);
	spin_unlock_irqrestore(&ep->udc->lock, flags);
	kfree(req);
}

static int usbf_ep_set_halt(struct usb_ep *_ep, int halt)
{
	struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
	unsigned long flags;
	int ret;

	if (ep->id == 0)
		return -EINVAL;

	spin_lock_irqsave(&ep->udc->lock, flags);

	if (!list_empty(&ep->queue)) {
		ret = -EAGAIN;
		goto end;
	}

	usbf_ep_stall(ep, halt);
	if (!halt)
		ep->is_wedged = 0;

	ret = 0;
end:
	spin_unlock_irqrestore(&ep->udc->lock, flags);

	return ret;
}

static int usbf_ep_set_wedge(struct usb_ep *_ep)
{
	struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);
	unsigned long flags;
	int ret;

	if (ep->id == 0)
		return -EINVAL;

	spin_lock_irqsave(&ep->udc->lock, flags);
	if (!list_empty(&ep->queue)) {
		ret = -EAGAIN;
		goto end;
	}
	usbf_ep_stall(ep, 1);
	ep->is_wedged = 1;

	ret = 0;
end:
	spin_unlock_irqrestore(&ep->udc->lock, flags);
	return ret;
}

static struct usb_ep_ops usbf_ep_ops = {
	.enable = usbf_ep_enable,
	.disable = usbf_ep_disable,
	.queue = usbf_ep_queue,
	.dequeue = usbf_ep_dequeue,
	.set_halt = usbf_ep_set_halt,
	.set_wedge = usbf_ep_set_wedge,
	.alloc_request = usbf_ep_alloc_request,
	.free_request = usbf_ep_free_request,
};

static void usbf_ep0_req_complete(struct usb_ep *_ep, struct usb_request *_req)
{
}

static void usbf_ep0_fill_req(struct usbf_ep *ep0, struct usbf_req *req,
			      void *buf, unsigned int length,
			      void (*complete)(struct usb_ep *_ep,
					       struct usb_request *_req))
{
	if (buf && length)
		memcpy(ep0->udc->ep0_buf, buf, length);

	req->req.buf = ep0->udc->ep0_buf;
	req->req.length = length;
	req->req.dma = 0;
	req->req.zero = true;
	req->req.complete = complete ? complete : usbf_ep0_req_complete;
	req->req.status = -EINPROGRESS;
	req->req.context = NULL;
	req->req.actual = 0;
}

static struct usbf_ep *usbf_get_ep_by_addr(struct usbf_udc *udc, u8 address)
{
	struct usbf_ep *ep;
	unsigned int i;

	if ((address & USB_ENDPOINT_NUMBER_MASK) == 0)
		return &udc->ep[0];

	for (i = 1; i < ARRAY_SIZE(udc->ep); i++) {
		ep = &udc->ep[i];

		if (!ep->ep.desc)
			continue;

		if (ep->ep.desc->bEndpointAddress == address)
			return ep;
	}

	return NULL;
}

static int usbf_req_delegate(struct usbf_udc *udc,
			     const struct usb_ctrlrequest *ctrlrequest)
{
	int ret;

	spin_unlock(&udc->lock);
	ret = udc->driver->setup(&udc->gadget, ctrlrequest);
	spin_lock(&udc->lock);
	if (ret < 0) {
		dev_dbg(udc->dev, "udc driver setup failed %d\n", ret);
		return ret;
	}
	if (ret == USB_GADGET_DELAYED_STATUS) {
		dev_dbg(udc->dev, "delayed status set\n");
		udc->ep[0].delayed_status = 1;
		return 0;
	}
	return ret;
}

static int usbf_req_get_status(struct usbf_udc *udc,
			       const struct usb_ctrlrequest *ctrlrequest)
{
	struct usbf_ep *ep;
	u16 status_data;
	u16 wLength;
	u16 wValue;
	u16 wIndex;

	wValue  = le16_to_cpu(ctrlrequest->wValue);
	wLength = le16_to_cpu(ctrlrequest->wLength);
	wIndex  = le16_to_cpu(ctrlrequest->wIndex);

	switch (ctrlrequest->bRequestType) {
	case USB_DIR_IN | USB_RECIP_DEVICE | USB_TYPE_STANDARD:
		if ((wValue != 0) || (wIndex != 0) || (wLength != 2))
			goto delegate;

		status_data = 0;
		if (udc->gadget.is_selfpowered)
			status_data |= BIT(USB_DEVICE_SELF_POWERED);

		if (udc->is_remote_wakeup)
			status_data |= BIT(USB_DEVICE_REMOTE_WAKEUP);

		break;

	case USB_DIR_IN | USB_RECIP_ENDPOINT | USB_TYPE_STANDARD:
		if ((wValue != 0) || (wLength != 2))
			goto delegate;

		ep = usbf_get_ep_by_addr(udc, wIndex);
		if (!ep)
			return -EINVAL;

		status_data = 0;
		if (usbf_ep_is_stalled(ep))
			status_data |= cpu_to_le16(1);
		break;

	case USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_STANDARD:
		if ((wValue != 0) || (wLength != 2))
			goto delegate;
		status_data = 0;
		break;

	default:
		goto delegate;
	}

	usbf_ep0_fill_req(&udc->ep[0], &udc->setup_reply, &status_data,
			  sizeof(status_data), NULL);
	usbf_ep0_queue(&udc->ep[0], &udc->setup_reply, GFP_ATOMIC);

	return 0;

delegate:
	return usbf_req_delegate(udc, ctrlrequest);
}

static int usbf_req_clear_set_feature(struct usbf_udc *udc,
				      const struct usb_ctrlrequest *ctrlrequest,
				      bool is_set)
{
	struct usbf_ep *ep;
	u16 wLength;
	u16 wValue;
	u16 wIndex;

	wValue  = le16_to_cpu(ctrlrequest->wValue);
	wLength = le16_to_cpu(ctrlrequest->wLength);
	wIndex  = le16_to_cpu(ctrlrequest->wIndex);

	switch (ctrlrequest->bRequestType) {
	case USB_DIR_OUT | USB_RECIP_DEVICE:
		if ((wIndex != 0) || (wLength != 0))
			goto delegate;

		if (wValue != cpu_to_le16(USB_DEVICE_REMOTE_WAKEUP))
			goto delegate;

		udc->is_remote_wakeup = is_set;
		break;

	case USB_DIR_OUT | USB_RECIP_ENDPOINT:
		if (wLength != 0)
			goto delegate;

		ep = usbf_get_ep_by_addr(udc, wIndex);
		if (!ep)
			return -EINVAL;

		if ((ep->id == 0) && is_set) {
			/* Endpoint 0 cannot be halted (stalled)
			 * Returning an error code leads to a STALL on this ep0
			 * but keep the automate in a consistent state.
			 */
			return -EINVAL;
		}
		if (ep->is_wedged && !is_set) {
			/* Ignore CLEAR_FEATURE(HALT ENDPOINT) when the
			 * endpoint is wedged
			 */
			break;
		}
		usbf_ep_stall(ep, is_set);
		break;

	default:
		goto delegate;
	}

	return 0;

delegate:
	return usbf_req_delegate(udc, ctrlrequest);
}

static void usbf_ep0_req_set_address_complete(struct usb_ep *_ep,
					      struct usb_request *_req)
{
	struct usbf_ep *ep = container_of(_ep, struct usbf_ep, ep);

	/* The status phase of the SET_ADDRESS request is completed ... */
	if (_req->status == 0) {
		/* ... without any errors -> Signaled the state to the core. */
		usb_gadget_set_state(&ep->udc->gadget, USB_STATE_ADDRESS);
	}

	/* In case of request failure, there is no need to revert the address
	 * value set to the hardware as the hardware will take care of the
	 * value only if the status stage is completed normally.
	 */
}

static int usbf_req_set_address(struct usbf_udc *udc,
				const struct usb_ctrlrequest *ctrlrequest)
{
	u16 wLength;
	u16 wValue;
	u16 wIndex;
	u32 addr;

	wValue  = le16_to_cpu(ctrlrequest->wValue);
	wLength = le16_to_cpu(ctrlrequest->wLength);
	wIndex  = le16_to_cpu(ctrlrequest->wIndex);

	if (ctrlrequest->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE))
		goto delegate;

	if ((wIndex != 0) || (wLength != 0) || (wValue > 127))
		return -EINVAL;

	addr = wValue;
	/* The hardware will take care of this USB address after the status
	 * stage of the SET_ADDRESS request is completed normally.
	 * It is safe to write it now
	 */
	usbf_reg_writel(udc, USBF_REG_USB_ADDRESS, USBF_USB_SET_USB_ADDR(addr));

	/* Queued the status request */
	usbf_ep0_fill_req(&udc->ep[0], &udc->setup_reply, NULL, 0,
			  usbf_ep0_req_set_address_complete);
	usbf_ep0_queue(&udc->ep[0], &udc->setup_reply, GFP_ATOMIC);

	return 0;

delegate:
	return usbf_req_delegate(udc, ctrlrequest);
}

static int usbf_req_set_configuration(struct usbf_udc *udc,
				      const struct usb_ctrlrequest *ctrlrequest)
{
	u16 wLength;
	u16 wValue;
	u16 wIndex;
	int ret;

	ret = usbf_req_delegate(udc, ctrlrequest);
	if (ret)
		return ret;

	wValue  = le16_to_cpu(ctrlrequest->wValue);
	wLength = le16_to_cpu(ctrlrequest->wLength);
	wIndex  = le16_to_cpu(ctrlrequest->wIndex);

	if ((ctrlrequest->bRequestType != (USB_DIR_OUT | USB_RECIP_DEVICE)) ||
	    (wIndex != 0) || (wLength != 0)) {
		/* No error detected by driver->setup() but it is not an USB2.0
		 * Ch9 SET_CONFIGURATION.
		 * Nothing more to do
		 */
		return 0;
	}

	if (wValue & 0x00FF) {
		usbf_reg_bitset(udc, USBF_REG_USB_CONTROL, USBF_USB_CONF);
	} else {
		usbf_reg_bitclr(udc, USBF_REG_USB_CONTROL, USBF_USB_CONF);
		/* Go back to Address State */
		spin_unlock(&udc->lock);
		usb_gadget_set_state(&udc->gadget, USB_STATE_ADDRESS);
		spin_lock(&udc->lock);
	}

	return 0;
}

static int usbf_handle_ep0_setup(struct usbf_ep *ep0)
{
	union {
		struct usb_ctrlrequest ctrlreq;
		u32 raw[2];
	} crq;
	struct usbf_udc *udc = ep0->udc;
	int ret;

	/* Read setup data (ie the USB control request) */
	crq.raw[0] = usbf_reg_readl(udc, USBF_REG_SETUP_DATA0);
	crq.raw[1] = usbf_reg_readl(udc, USBF_REG_SETUP_DATA1);

	dev_dbg(ep0->udc->dev,
		"ep0 req%02x.%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\n",
		crq.ctrlreq.bRequestType, crq.ctrlreq.bRequest,
		crq.ctrlreq.wValue, crq.ctrlreq.wIndex, crq.ctrlreq.wLength);

	/* Set current EP0 state according to the received request */
	if (crq.ctrlreq.wLength) {
		if (crq.ctrlreq.bRequestType & USB_DIR_IN) {
			udc->ep0state = EP0_IN_DATA_PHASE;
			usbf_ep_reg_clrset(ep0, USBF_REG_EP0_CONTROL,
				USBF_EP0_INAK,
				USBF_EP0_INAK_EN);
			ep0->is_in = 1;
		} else {
			udc->ep0state = EP0_OUT_DATA_PHASE;
			usbf_ep_reg_bitclr(ep0, USBF_REG_EP0_CONTROL,
				USBF_EP0_ONAK);
			ep0->is_in = 0;
		}
	} else {
		udc->ep0state = EP0_IN_STATUS_START_PHASE;
		ep0->is_in = 1;
	}

	/* We starts a new control transfer -> Clear the delayed status flag */
	ep0->delayed_status = 0;

	if ((crq.ctrlreq.bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD) {
		/* This is not a USB standard request -> delelate */
		goto delegate;
	}

	switch (crq.ctrlreq.bRequest) {
	case USB_REQ_GET_STATUS:
		ret = usbf_req_get_status(udc, &crq.ctrlreq);
		break;

	case USB_REQ_CLEAR_FEATURE:
		ret = usbf_req_clear_set_feature(udc, &crq.ctrlreq, false);
		break;

	case USB_REQ_SET_FEATURE:
		ret = usbf_req_clear_set_feature(udc, &crq.ctrlreq, true);
		break;

	case USB_REQ_SET_ADDRESS:
		ret = usbf_req_set_address(udc, &crq.ctrlreq);
		break;

	case USB_REQ_SET_CONFIGURATION:
		ret = usbf_req_set_configuration(udc, &crq.ctrlreq);
		break;

	default:
		goto delegate;
	}

	return ret;

delegate:
	return usbf_req_delegate(udc, &crq.ctrlreq);
}

static int usbf_handle_ep0_data_status(struct usbf_ep *ep0,
				  const char *ep0state_name,
				  enum usbf_ep0state next_ep0state)
{
	struct usbf_udc *udc = ep0->udc;
	int ret;

	ret = usbf_ep_process_queue(ep0);
	switch (ret) {
	case -ENOENT:
		dev_err(udc->dev,
			"no request available for ep0 %s phase\n",
			ep0state_name);
		break;
	case -EINPROGRESS:
		/* More data needs to be processed */
		ret = 0;
		break;
	case 0:
		/* All requests in the queue are processed */
		udc->ep0state = next_ep0state;
		break;
	default:
		dev_err(udc->dev,
			"process queue failed for ep0 %s phase (%d)\n",
			ep0state_name, ret);
		break;
	}
	return ret;
}

static int usbf_handle_ep0_out_status_start(struct usbf_ep *ep0)
{
	struct usbf_udc *udc = ep0->udc;
	struct usbf_req *req;

	usbf_ep_reg_clrset(ep0, USBF_REG_EP0_CONTROL,
				USBF_EP0_ONAK,
				USBF_EP0_PIDCLR);
	ep0->is_in = 0;

	req = list_first_entry_or_null(&ep0->queue, struct usbf_req, queue);
	if (!req) {
		usbf_ep0_fill_req(ep0, &udc->setup_reply, NULL, 0, NULL);
		usbf_ep0_queue(ep0, &udc->setup_reply, GFP_ATOMIC);
	} else {
		if (req->req.length) {
			dev_err(udc->dev,
				"queued request length %u for ep0 out status phase\n",
				req->req.length);
		}
	}
	udc->ep0state = EP0_OUT_STATUS_PHASE;
	return 0;
}

static int usbf_handle_ep0_in_status_start(struct usbf_ep *ep0)
{
	struct usbf_udc *udc = ep0->udc;
	struct usbf_req *req;
	int ret;

	usbf_ep_reg_clrset(ep0, USBF_REG_EP0_CONTROL,
				USBF_EP0_INAK,
				USBF_EP0_INAK_EN | USBF_EP0_PIDCLR);
	ep0->is_in = 1;

	/* Queue request for status if needed */
	req = list_first_entry_or_null(&ep0->queue, struct usbf_req, queue);
	if (!req) {
		if (ep0->delayed_status) {
			dev_dbg(ep0->udc->dev,
				"EP0_IN_STATUS_START_PHASE ep0->delayed_status set\n");
			udc->ep0state = EP0_IN_STATUS_PHASE;
			return 0;
		}

		usbf_ep0_fill_req(ep0, &udc->setup_reply, NULL,
			  0, NULL);
		usbf_ep0_queue(ep0, &udc->setup_reply,
			       GFP_ATOMIC);

		req = list_first_entry_or_null(&ep0->queue, struct usbf_req, queue);
	} else {
		if (req->req.length) {
			dev_err(udc->dev,
				"queued request length %u for ep0 in status phase\n",
				req->req.length);
		}
	}

	ret = usbf_ep0_pio_in(ep0, req);
	if (ret != -EINPROGRESS) {
		usbf_ep_req_done(ep0, req, ret);
		udc->ep0state = EP0_IN_STATUS_END_PHASE;
		return 0;
	}

	udc->ep0state = EP0_IN_STATUS_PHASE;
	return 0;
}

static void usbf_ep0_interrupt(struct usbf_ep *ep0)
{
	struct usbf_udc *udc = ep0->udc;
	u32 sts, prev_sts;
	int prev_ep0state;
	int ret;

	ep0->status = usbf_ep_reg_readl(ep0, USBF_REG_EP0_STATUS);
	usbf_ep_reg_writel(ep0, USBF_REG_EP0_STATUS, ~ep0->status);

	dev_dbg(ep0->udc->dev, "ep0 status=0x%08x, enable=%08x\n, ctrl=0x%08x\n",
		ep0->status,
		usbf_ep_reg_readl(ep0, USBF_REG_EP0_INT_ENA),
		usbf_ep_reg_readl(ep0, USBF_REG_EP0_CONTROL));

	sts = ep0->status & (USBF_EP0_SETUP_INT | USBF_EP0_IN_INT | USBF_EP0_OUT_INT |
			     USBF_EP0_OUT_NULL_INT | USBF_EP0_STG_START_INT |
			     USBF_EP0_STG_END_INT);

	ret = 0;
	do {
		dev_dbg(ep0->udc->dev, "udc->ep0state=%d\n", udc->ep0state);

		prev_sts = sts;
		prev_ep0state = udc->ep0state;
		switch (udc->ep0state) {
		case EP0_IDLE:
			if (!(sts & USBF_EP0_SETUP_INT))
				break;

			sts &= ~USBF_EP0_SETUP_INT;
			dev_dbg(ep0->udc->dev, "ep0 handle setup\n");
			ret = usbf_handle_ep0_setup(ep0);
			break;

		case EP0_IN_DATA_PHASE:
			if (!(sts & USBF_EP0_IN_INT))
				break;

			sts &= ~USBF_EP0_IN_INT;
			dev_dbg(ep0->udc->dev, "ep0 handle in data phase\n");
			ret = usbf_handle_ep0_data_status(ep0,
				"in data", EP0_OUT_STATUS_START_PHASE);
			break;

		case EP0_OUT_STATUS_START_PHASE:
			if (!(sts & USBF_EP0_STG_START_INT))
				break;

			sts &= ~USBF_EP0_STG_START_INT;
			dev_dbg(ep0->udc->dev, "ep0 handle out status start phase\n");
			ret = usbf_handle_ep0_out_status_start(ep0);
			break;

		case EP0_OUT_STATUS_PHASE:
			if (!(sts & (USBF_EP0_OUT_INT | USBF_EP0_OUT_NULL_INT)))
				break;

			sts &= ~(USBF_EP0_OUT_INT | USBF_EP0_OUT_NULL_INT);
			dev_dbg(ep0->udc->dev, "ep0 handle out status phase\n");
			ret = usbf_handle_ep0_data_status(ep0,
				"out status",
				EP0_OUT_STATUS_END_PHASE);
			break;

		case EP0_OUT_STATUS_END_PHASE:
			if (!(sts & (USBF_EP0_STG_END_INT | USBF_EP0_SETUP_INT)))
				break;

			sts &= ~USBF_EP0_STG_END_INT;
			dev_dbg(ep0->udc->dev, "ep0 handle out status end phase\n");
			udc->ep0state = EP0_IDLE;
			break;

		case EP0_OUT_DATA_PHASE:
			if (!(sts & (USBF_EP0_OUT_INT | USBF_EP0_OUT_NULL_INT)))
				break;

			sts &= ~(USBF_EP0_OUT_INT | USBF_EP0_OUT_NULL_INT);
			dev_dbg(ep0->udc->dev, "ep0 handle out data phase\n");
			ret = usbf_handle_ep0_data_status(ep0,
				"out data", EP0_IN_STATUS_START_PHASE);
			break;

		case EP0_IN_STATUS_START_PHASE:
			if (!(sts & USBF_EP0_STG_START_INT))
				break;

			sts &= ~USBF_EP0_STG_START_INT;
			dev_dbg(ep0->udc->dev, "ep0 handle in status start phase\n");
			ret = usbf_handle_ep0_in_status_start(ep0);
			break;

		case EP0_IN_STATUS_PHASE:
			if (!(sts & USBF_EP0_IN_INT))
				break;

			sts &= ~USBF_EP0_IN_INT;
			dev_dbg(ep0->udc->dev, "ep0 handle in status phase\n");
			ret = usbf_handle_ep0_data_status(ep0,
				"in status", EP0_IN_STATUS_END_PHASE);
			break;

		case EP0_IN_STATUS_END_PHASE:
			if (!(sts & (USBF_EP0_STG_END_INT | USBF_EP0_SETUP_INT)))
				break;

			sts &= ~USBF_EP0_STG_END_INT;
			dev_dbg(ep0->udc->dev, "ep0 handle in status end\n");
			udc->ep0state = EP0_IDLE;
			break;

		default:
			udc->ep0state = EP0_IDLE;
			break;
		}

		if (ret) {
			dev_dbg(ep0->udc->dev, "ep0 failed (%d)\n", ret);
			/* Failure -> stall.
			 * This stall state will be automatically cleared when
			 * the IP receives the next SETUP packet
			 */
			usbf_ep_stall(ep0, true);

			/* Remove anything that was pending */
			usbf_ep_nuke(ep0, -EPROTO);

			udc->ep0state = EP0_IDLE;
			break;
		}

	} while ((prev_ep0state != udc->ep0state) || (prev_sts != sts));

	dev_dbg(ep0->udc->dev, "ep0 done udc->ep0state=%d, status=0x%08x. next=0x%08x\n",
		udc->ep0state, sts,
		usbf_ep_reg_readl(ep0, USBF_REG_EP0_STATUS));
}

static void usbf_epn_process_queue(struct usbf_ep *epn)
{
	int ret;

	ret = usbf_ep_process_queue(epn);
	switch (ret) {
	case -ENOENT:
		dev_warn(epn->udc->dev, "ep%u %s, no request available\n",
			epn->id, epn->is_in ? "in" : "out");
		break;
	case -EINPROGRESS:
		/* More data needs to be processed */
		ret = 0;
		break;
	case 0:
		/* All requests in the queue are processed */
		break;
	default:
		dev_err(epn->udc->dev, "ep%u %s, process queue failed (%d)\n",
			epn->id, epn->is_in ? "in" : "out", ret);
		break;
	}

	if (ret) {
		dev_dbg(epn->udc->dev, "ep%u %s failed (%d)\n", epn->id,
			epn->is_in ? "in" : "out", ret);
		usbf_ep_stall(epn, true);
		usbf_ep_nuke(epn, ret);
	}
}

static void usbf_epn_interrupt(struct usbf_ep *epn)
{
	u32 sts;
	u32 ena;

	epn->status = usbf_ep_reg_readl(epn, USBF_REG_EPN_STATUS);
	ena = usbf_ep_reg_readl(epn, USBF_REG_EPN_INT_ENA);
	usbf_ep_reg_writel(epn, USBF_REG_EPN_STATUS, ~(epn->status & ena));

	dev_dbg(epn->udc->dev, "ep%u %s status=0x%08x, enable=%08x\n, ctrl=0x%08x\n",
		epn->id, epn->is_in ? "in" : "out", epn->status, ena,
		usbf_ep_reg_readl(epn, USBF_REG_EPN_CONTROL));

	if (epn->disabled) {
		dev_warn(epn->udc->dev, "ep%u %s, interrupt while disabled\n",
			epn->id, epn->is_in ? "in" : "out");
		return;
	}

	sts = epn->status & ena;

	if (sts & (USBF_EPN_IN_END_INT | USBF_EPN_IN_INT)) {
		sts &= ~(USBF_EPN_IN_END_INT | USBF_EPN_IN_INT);
		dev_dbg(epn->udc->dev, "ep%u %s process queue (in interrupts)\n",
			epn->id, epn->is_in ? "in" : "out");
		usbf_epn_process_queue(epn);
	}

	if (sts & (USBF_EPN_OUT_END_INT | USBF_EPN_OUT_INT | USBF_EPN_OUT_NULL_INT)) {
		sts &= ~(USBF_EPN_OUT_END_INT | USBF_EPN_OUT_INT | USBF_EPN_OUT_NULL_INT);
		dev_dbg(epn->udc->dev, "ep%u %s process queue (out interrupts)\n",
			epn->id, epn->is_in ? "in" : "out");
		usbf_epn_process_queue(epn);
	}

	dev_dbg(epn->udc->dev, "ep%u %s done status=0x%08x. next=0x%08x\n",
		epn->id, epn->is_in ? "in" : "out",
		sts, usbf_ep_reg_readl(epn, USBF_REG_EPN_STATUS));
}

static void usbf_ep_reset(struct usbf_ep *ep)
{
	ep->status = 0;
	/* Remove anything that was pending */
	usbf_ep_nuke(ep, -ESHUTDOWN);
}

static void usbf_reset(struct usbf_udc *udc)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(udc->ep); i++) {
		if (udc->ep[i].disabled)
			continue;

		usbf_ep_reset(&udc->ep[i]);
	}

	if (usbf_reg_readl(udc, USBF_REG_USB_STATUS) & USBF_USB_SPEED_MODE)
		udc->gadget.speed = USB_SPEED_HIGH;
	else
		udc->gadget.speed = USB_SPEED_FULL;

	/* Remote wakeup feature must be disabled on USB bus reset */
	udc->is_remote_wakeup = false;

	/* Enable endpoint zero */
	usbf_ep0_enable(&udc->ep[0]);

	if (udc->driver) {
		/* Signal the reset */
		spin_unlock(&udc->lock);
		usb_gadget_udc_reset(&udc->gadget, udc->driver);
		spin_lock(&udc->lock);
	}
}

static void usbf_driver_suspend(struct usbf_udc *udc)
{
	if (udc->is_usb_suspended) {
		dev_dbg(udc->dev, "already suspended\n");
		return;
	}

	dev_dbg(udc->dev, "do usb suspend\n");
	udc->is_usb_suspended = true;

	if (udc->driver && udc->driver->suspend) {
		spin_unlock(&udc->lock);
		udc->driver->suspend(&udc->gadget);
		spin_lock(&udc->lock);

		/* The datasheet tells to set the USB_CONTROL register SUSPEND
		 * bit when the USB bus suspend is detected.
		 * This bit stops the clocks (clocks for EPC, SIE, USBPHY) but
		 * these clocks seems not used only by the USB device. Some
		 * UARTs can be lost ...
		 * So, do not set the USB_CONTROL register SUSPEND bit.
		 */
	}
}

static void usbf_driver_resume(struct usbf_udc *udc)
{
	if (!udc->is_usb_suspended)
		return;

	dev_dbg(udc->dev, "do usb resume\n");
	udc->is_usb_suspended = false;

	if (udc->driver && udc->driver->resume) {
		spin_unlock(&udc->lock);
		udc->driver->resume(&udc->gadget);
		spin_lock(&udc->lock);
	}
}

static irqreturn_t usbf_epc_irq(int irq, void *_udc)
{
	struct usbf_udc *udc = (struct usbf_udc *)_udc;
	unsigned long flags;
	struct usbf_ep *ep;
	u32 int_sts;
	u32 int_en;
	int i;

	spin_lock_irqsave(&udc->lock, flags);

	int_en = usbf_reg_readl(udc, USBF_REG_USB_INT_ENA);
	int_sts = usbf_reg_readl(udc, USBF_REG_USB_INT_STA) & int_en;
	usbf_reg_writel(udc, USBF_REG_USB_INT_STA, ~int_sts);

	dev_dbg(udc->dev, "int_sts=0x%08x\n", int_sts);

	if (int_sts & USBF_USB_RSUM_INT) {
		dev_dbg(udc->dev, "handle resume\n");
		usbf_driver_resume(udc);
	}

	if (int_sts & USBF_USB_USB_RST_INT) {
		dev_dbg(udc->dev, "handle bus reset\n");
		usbf_driver_resume(udc);
		usbf_reset(udc);
	}

	if (int_sts & USBF_USB_SPEED_MODE_INT) {
		if (usbf_reg_readl(udc, USBF_REG_USB_STATUS) & USBF_USB_SPEED_MODE)
			udc->gadget.speed = USB_SPEED_HIGH;
		else
			udc->gadget.speed = USB_SPEED_FULL;
		dev_dbg(udc->dev, "handle speed change (%s)\n",
			udc->gadget.speed == USB_SPEED_HIGH ? "High" : "Full");
	}

	if (int_sts & USBF_USB_EPN_INT(0)) {
		usbf_driver_resume(udc);
		usbf_ep0_interrupt(&udc->ep[0]);
	}

	for (i = 1; i < ARRAY_SIZE(udc->ep); i++) {
		ep = &udc->ep[i];

		if (int_sts & USBF_USB_EPN_INT(i)) {
			usbf_driver_resume(udc);
			usbf_epn_interrupt(ep);
		}
	}

	if (int_sts & USBF_USB_SPND_INT) {
		dev_dbg(udc->dev, "handle suspend\n");
		usbf_driver_suspend(udc);
	}

	spin_unlock_irqrestore(&udc->lock, flags);

	return IRQ_HANDLED;
}

static irqreturn_t usbf_ahb_epc_irq(int irq, void *_udc)
{
	struct usbf_udc *udc = (struct usbf_udc *)_udc;
	unsigned long flags;
	struct usbf_ep *epn;
	u32 sysbint;
	void (*ep_action)(struct usbf_ep *epn);
	int i;

	spin_lock_irqsave(&udc->lock, flags);

	/* Read and ack interrupts */
	sysbint = usbf_reg_readl(udc, USBF_REG_AHBBINT);
	usbf_reg_writel(udc, USBF_REG_AHBBINT, sysbint);

	if ((sysbint & USBF_SYS_VBUS_INT) == USBF_SYS_VBUS_INT) {
		if (usbf_reg_readl(udc, USBF_REG_EPCTR) & USBF_SYS_VBUS_LEVEL) {
			dev_dbg(udc->dev, "handle vbus (1)\n");
			spin_unlock(&udc->lock);
			usb_udc_vbus_handler(&udc->gadget, true);
			usb_gadget_set_state(&udc->gadget, USB_STATE_POWERED);
			spin_lock(&udc->lock);
		} else {
			dev_dbg(udc->dev, "handle vbus (0)\n");
			udc->is_usb_suspended = false;
			spin_unlock(&udc->lock);
			usb_udc_vbus_handler(&udc->gadget, false);
			usb_gadget_set_state(&udc->gadget,
					     USB_STATE_NOTATTACHED);
			spin_lock(&udc->lock);
		}
	}

	for (i = 1; i < ARRAY_SIZE(udc->ep); i++) {
		if (sysbint & USBF_SYS_DMA_ENDINT_EPN(i)) {
			epn = &udc->ep[i];
			dev_dbg(epn->udc->dev,
				"ep%u handle DMA complete. action=%ps\n",
				epn->id, epn->bridge_on_dma_end);
			ep_action = epn->bridge_on_dma_end;
			if (ep_action) {
				epn->bridge_on_dma_end = NULL;
				ep_action(epn);
			}
		}
	}

	spin_unlock_irqrestore(&udc->lock, flags);

	return IRQ_HANDLED;
}

static int usbf_udc_start(struct usb_gadget *gadget,
			  struct usb_gadget_driver *driver)
{
	struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);
	unsigned long flags;

	dev_info(udc->dev, "start (driver '%s')\n", driver->driver.name);

	spin_lock_irqsave(&udc->lock, flags);

	/* hook up the driver */
	udc->driver = driver;

	/* Enable VBUS interrupt */
	usbf_reg_writel(udc, USBF_REG_AHBBINTEN, USBF_SYS_VBUS_INTEN);

	spin_unlock_irqrestore(&udc->lock, flags);

	return 0;
}

static int usbf_udc_stop(struct usb_gadget *gadget)
{
	struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);
	unsigned long flags;

	spin_lock_irqsave(&udc->lock, flags);

	/* Disable VBUS interrupt */
	usbf_reg_writel(udc, USBF_REG_AHBBINTEN, 0);

	udc->driver = NULL;

	spin_unlock_irqrestore(&udc->lock, flags);

	dev_info(udc->dev, "stopped\n");

	return 0;
}

static int usbf_get_frame(struct usb_gadget *gadget)
{
	struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);

	return USBF_USB_GET_FRAME(usbf_reg_readl(udc, USBF_REG_USB_ADDRESS));
}

static void usbf_attach(struct usbf_udc *udc)
{
	/* Enable USB signal to Function PHY
	 * D+ signal Pull-up
	 * Disable endpoint 0, it will be automatically enable when a USB reset
	 * is received.
	 * Disable the other endpoints
	 */
	usbf_reg_clrset(udc, USBF_REG_USB_CONTROL,
		USBF_USB_CONNECTB | USBF_USB_DEFAULT | USBF_USB_CONF,
		USBF_USB_PUE2);

	/* Enable reset and mode change interrupts */
	usbf_reg_bitset(udc, USBF_REG_USB_INT_ENA,
		USBF_USB_USB_RST_EN | USBF_USB_SPEED_MODE_EN | USBF_USB_RSUM_EN | USBF_USB_SPND_EN);
}

static void usbf_detach(struct usbf_udc *udc)
{
	int i;

	/* Disable interrupts */
	usbf_reg_writel(udc, USBF_REG_USB_INT_ENA, 0);

	for (i = 0; i < ARRAY_SIZE(udc->ep); i++) {
		if (udc->ep[i].disabled)
			continue;

		usbf_ep_reset(&udc->ep[i]);
	}

	/* Disable USB signal to Function PHY
	 * Do not Pull-up D+ signal
	 * Disable endpoint 0
	 * Disable the other endpoints
	 */
	usbf_reg_clrset(udc, USBF_REG_USB_CONTROL,
		USBF_USB_PUE2 | USBF_USB_DEFAULT | USBF_USB_CONF,
		USBF_USB_CONNECTB);
}

static int usbf_pullup(struct usb_gadget *gadget, int is_on)
{
	struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);
	unsigned long flags;

	dev_dbg(udc->dev, "pullup %d\n", is_on);

	spin_lock_irqsave(&udc->lock, flags);
	if (is_on)
		usbf_attach(udc);
	else
		usbf_detach(udc);
	spin_unlock_irqrestore(&udc->lock, flags);

	return 0;
}

static int usbf_udc_set_selfpowered(struct usb_gadget *gadget,
				    int is_selfpowered)
{
	struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);
	unsigned long flags;

	spin_lock_irqsave(&udc->lock, flags);
	gadget->is_selfpowered = (is_selfpowered != 0);
	spin_unlock_irqrestore(&udc->lock, flags);

	return 0;
}

static int usbf_udc_wakeup(struct usb_gadget *gadget)
{
	struct usbf_udc *udc = container_of(gadget, struct usbf_udc, gadget);
	unsigned long flags;
	int ret;

	spin_lock_irqsave(&udc->lock, flags);

	if (!udc->is_remote_wakeup) {
		dev_dbg(udc->dev, "remote wakeup not allowed\n");
		ret = -EINVAL;
		goto end;
	}

	dev_dbg(udc->dev, "do wakeup\n");

	/* Send the resume signal */
	usbf_reg_bitset(udc, USBF_REG_USB_CONTROL, USBF_USB_RSUM_IN);
	usbf_reg_bitclr(udc, USBF_REG_USB_CONTROL, USBF_USB_RSUM_IN);

	ret = 0;
end:
	spin_unlock_irqrestore(&udc->lock, flags);
	return ret;
}

static struct usb_gadget_ops usbf_gadget_ops = {
	.get_frame = usbf_get_frame,
	.pullup = usbf_pullup,
	.udc_start = usbf_udc_start,
	.udc_stop = usbf_udc_stop,
	.set_selfpowered = usbf_udc_set_selfpowered,
	.wakeup = usbf_udc_wakeup,
};

static int usbf_epn_check(struct usbf_ep *epn)
{
	const char *type_txt;
	const char *buf_txt;
	int ret = 0;
	u32 ctrl;

	ctrl = usbf_ep_reg_readl(epn, USBF_REG_EPN_CONTROL);

	switch (ctrl & USBF_EPN_MODE_MASK) {
	case USBF_EPN_MODE_BULK:
		type_txt = "bulk";
		if (epn->ep.caps.type_control || epn->ep.caps.type_iso ||
		    !epn->ep.caps.type_bulk || epn->ep.caps.type_int) {
			dev_err(epn->udc->dev,
				"ep%u caps mismatch, bulk expected\n", epn->id);
			ret = -EINVAL;
		}
		break;
	case USBF_EPN_MODE_INTR:
		type_txt = "intr";
		if (epn->ep.caps.type_control || epn->ep.caps.type_iso ||
		    epn->ep.caps.type_bulk || !epn->ep.caps.type_int) {
			dev_err(epn->udc->dev,
				"ep%u caps mismatch, int expected\n", epn->id);
			ret = -EINVAL;
		}
		break;
	case USBF_EPN_MODE_ISO:
		type_txt = "iso";
		if (epn->ep.caps.type_control || !epn->ep.caps.type_iso ||
		    epn->ep.caps.type_bulk || epn->ep.caps.type_int) {
			dev_err(epn->udc->dev,
				"ep%u caps mismatch, iso expected\n", epn->id);
			ret = -EINVAL;
		}
		break;
	default:
		type_txt = "unknown";
		dev_err(epn->udc->dev, "ep%u unknown type\n", epn->id);
		ret = -EINVAL;
		break;
	}

	if (ctrl & USBF_EPN_BUF_TYPE_DOUBLE) {
		buf_txt = "double";
		if (!usbf_ep_info[epn->id].is_double) {
			dev_err(epn->udc->dev,
				"ep%u buffer mismatch, double expected\n",
				epn->id);
			ret = -EINVAL;
		}
	} else {
		buf_txt = "single";
		if (usbf_ep_info[epn->id].is_double) {
			dev_err(epn->udc->dev,
				"ep%u buffer mismatch, single expected\n",
				epn->id);
			ret = -EINVAL;
		}
	}

	dev_dbg(epn->udc->dev, "ep%u (%s) %s, %s buffer %u, checked %s\n",
		 epn->id, epn->ep.name, type_txt, buf_txt,
		 epn->ep.maxpacket_limit, ret ? "failed" : "ok");

	return ret;
}

static int usbf_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct usbf_udc *udc;
	struct usbf_ep *ep;
	unsigned int i;
	int irq;
	int ret;

	udc = devm_kzalloc(dev, sizeof(*udc), GFP_KERNEL);
	if (!udc)
		return -ENOMEM;
	platform_set_drvdata(pdev, udc);

	udc->dev = dev;
	spin_lock_init(&udc->lock);

	udc->regs = devm_platform_ioremap_resource(pdev, 0);
	if (IS_ERR(udc->regs))
		return PTR_ERR(udc->regs);

	devm_pm_runtime_enable(&pdev->dev);
	ret = pm_runtime_resume_and_get(&pdev->dev);
	if (ret < 0)
		return ret;

	dev_info(dev, "USBF version: %08x\n",
		usbf_reg_readl(udc, USBF_REG_USBSSVER));

	/* Resetting the PLL is handled via the clock driver as it has common
	 * registers with USB Host
	 */
	usbf_reg_bitclr(udc, USBF_REG_EPCTR, USBF_SYS_EPC_RST);

	/* modify in register gadget process */
	udc->gadget.speed = USB_SPEED_FULL;
	udc->gadget.max_speed = USB_SPEED_HIGH;
	udc->gadget.ops = &usbf_gadget_ops;

	udc->gadget.name = dev->driver->name;
	udc->gadget.dev.parent = dev;
	udc->gadget.ep0 = &udc->ep[0].ep;

	/* The hardware DMA controller needs dma addresses aligned on 32bit.
	 * A fallback to pio is done if DMA addresses are not aligned.
	 */
	udc->gadget.quirk_avoids_skb_reserve = 1;

	INIT_LIST_HEAD(&udc->gadget.ep_list);
	/* we have a canned request structure to allow sending packets as reply
	 * to get_status requests
	 */
	INIT_LIST_HEAD(&udc->setup_reply.queue);

	for (i = 0; i < ARRAY_SIZE(udc->ep); i++) {
		ep = &udc->ep[i];

		if (!(usbf_reg_readl(udc, USBF_REG_USBSSCONF) &
		      USBF_SYS_EP_AVAILABLE(i))) {
			continue;
		}

		INIT_LIST_HEAD(&ep->queue);

		ep->id = i;
		ep->disabled = 1;
		ep->udc = udc;
		ep->ep.ops = &usbf_ep_ops;
		ep->ep.name = usbf_ep_info[i].name;
		ep->ep.caps = usbf_ep_info[i].caps;
		usb_ep_set_maxpacket_limit(&ep->ep,
					   usbf_ep_info[i].maxpacket_limit);

		if (ep->id == 0) {
			ep->regs = ep->udc->regs + USBF_BASE_EP0;
		} else {
			ep->regs = ep->udc->regs + USBF_BASE_EPN(ep->id - 1);
			ret = usbf_epn_check(ep);
			if (ret)
				return ret;
			if (usbf_reg_readl(udc, USBF_REG_USBSSCONF) &
			    USBF_SYS_DMA_AVAILABLE(i)) {
				ep->dma_regs = ep->udc->regs +
					       USBF_BASE_DMA_EPN(ep->id - 1);
			}
			list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
		}
	}

	irq = platform_get_irq(pdev, 0);
	if (irq < 0)
		return irq;
	ret = devm_request_irq(dev, irq, usbf_epc_irq, 0, "usbf-epc", udc);
	if (ret) {
		dev_err(dev, "cannot request irq %d err %d\n", irq, ret);
		return ret;
	}

	irq = platform_get_irq(pdev, 1);
	if (irq < 0)
		return irq;
	ret = devm_request_irq(dev, irq, usbf_ahb_epc_irq, 0, "usbf-ahb-epc", udc);
	if (ret) {
		dev_err(dev, "cannot request irq %d err %d\n", irq, ret);
		return ret;
	}

	usbf_reg_bitset(udc, USBF_REG_AHBMCTR, USBF_SYS_WBURST_TYPE);

	usbf_reg_bitset(udc, USBF_REG_USB_CONTROL,
		USBF_USB_INT_SEL | USBF_USB_SOF_RCV | USBF_USB_SOF_CLK_MODE);

	ret = usb_add_gadget_udc(dev, &udc->gadget);
	if (ret)
		return ret;

	return 0;
}

static void usbf_remove(struct platform_device *pdev)
{
	struct usbf_udc *udc = platform_get_drvdata(pdev);

	usb_del_gadget_udc(&udc->gadget);

	pm_runtime_put(&pdev->dev);
}

static const struct of_device_id usbf_match[] = {
	{ .compatible = "renesas,rzn1-usbf" },
	{} /* sentinel */
};
MODULE_DEVICE_TABLE(of, usbf_match);

static struct platform_driver udc_driver = {
	.driver = {
		.name = "usbf_renesas",
		.of_match_table = usbf_match,
	},
	.probe          = usbf_probe,
	.remove_new     = usbf_remove,
};

module_platform_driver(udc_driver);

MODULE_AUTHOR("Herve Codina <herve.codina@bootlin.com>");
MODULE_DESCRIPTION("Renesas R-Car Gen3 & RZ/N1 USB Function driver");
MODULE_LICENSE("GPL");