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 | // SPDX-License-Identifier: GPL-2.0 /* * Cadence USBHS-DEV Driver - gadget side. * * Copyright (C) 2023 Cadence Design Systems. * * Authors: Pawel Laszczak <pawell@cadence.com> */ /* * Work around 1: * At some situations, the controller may get stale data address in TRB * at below sequences: * 1. Controller read TRB includes data address * 2. Software updates TRBs includes data address and Cycle bit * 3. Controller read TRB which includes Cycle bit * 4. DMA run with stale data address * * To fix this problem, driver needs to make the first TRB in TD as invalid. * After preparing all TRBs driver needs to check the position of DMA and * if the DMA point to the first just added TRB and doorbell is 1, * then driver must defer making this TRB as valid. This TRB will be make * as valid during adding next TRB only if DMA is stopped or at TRBERR * interrupt. * */ #include <linux/dma-mapping.h> #include <linux/pm_runtime.h> #include <linux/interrupt.h> #include <linux/property.h> #include <linux/dmapool.h> #include <linux/iopoll.h> #include "cdns2-gadget.h" #include "cdns2-trace.h" /** * set_reg_bit_32 - set bit in given 32 bits register. * @ptr: register address. * @mask: bits to set. */ static void set_reg_bit_32(void __iomem *ptr, u32 mask) { mask = readl(ptr) | mask; writel(mask, ptr); } /* * clear_reg_bit_32 - clear bit in given 32 bits register. * @ptr: register address. * @mask: bits to clear. */ static void clear_reg_bit_32(void __iomem *ptr, u32 mask) { mask = readl(ptr) & ~mask; writel(mask, ptr); } /* Clear bit in given 8 bits register. */ static void clear_reg_bit_8(void __iomem *ptr, u8 mask) { mask = readb(ptr) & ~mask; writeb(mask, ptr); } /* Set bit in given 16 bits register. */ void set_reg_bit_8(void __iomem *ptr, u8 mask) { mask = readb(ptr) | mask; writeb(mask, ptr); } static int cdns2_get_dma_pos(struct cdns2_device *pdev, struct cdns2_endpoint *pep) { int dma_index; dma_index = readl(&pdev->adma_regs->ep_traddr) - pep->ring.dma; return dma_index / TRB_SIZE; } /* Get next private request from list. */ struct cdns2_request *cdns2_next_preq(struct list_head *list) { return list_first_entry_or_null(list, struct cdns2_request, list); } void cdns2_select_ep(struct cdns2_device *pdev, u32 ep) { if (pdev->selected_ep == ep) return; pdev->selected_ep = ep; writel(ep, &pdev->adma_regs->ep_sel); } dma_addr_t cdns2_trb_virt_to_dma(struct cdns2_endpoint *pep, struct cdns2_trb *trb) { u32 offset = (char *)trb - (char *)pep->ring.trbs; return pep->ring.dma + offset; } static void cdns2_free_tr_segment(struct cdns2_endpoint *pep) { struct cdns2_device *pdev = pep->pdev; struct cdns2_ring *ring = &pep->ring; if (pep->ring.trbs) { dma_pool_free(pdev->eps_dma_pool, ring->trbs, ring->dma); memset(ring, 0, sizeof(*ring)); } } /* Allocates Transfer Ring segment. */ static int cdns2_alloc_tr_segment(struct cdns2_endpoint *pep) { struct cdns2_device *pdev = pep->pdev; struct cdns2_trb *link_trb; struct cdns2_ring *ring; ring = &pep->ring; if (!ring->trbs) { ring->trbs = dma_pool_alloc(pdev->eps_dma_pool, GFP_DMA32 | GFP_ATOMIC, &ring->dma); if (!ring->trbs) return -ENOMEM; } memset(ring->trbs, 0, TR_SEG_SIZE); if (!pep->num) return 0; /* Initialize the last TRB as Link TRB */ link_trb = (ring->trbs + (TRBS_PER_SEGMENT - 1)); link_trb->buffer = cpu_to_le32(TRB_BUFFER(ring->dma)); link_trb->control = cpu_to_le32(TRB_CYCLE | TRB_TYPE(TRB_LINK) | TRB_TOGGLE); return 0; } /* * Stalls and flushes selected endpoint. * Endpoint must be selected before invoking this function. */ static void cdns2_ep_stall_flush(struct cdns2_endpoint *pep) { struct cdns2_device *pdev = pep->pdev; int val; trace_cdns2_ep_halt(pep, 1, 1); writel(DMA_EP_CMD_DFLUSH, &pdev->adma_regs->ep_cmd); /* Wait for DFLUSH cleared. */ readl_poll_timeout_atomic(&pdev->adma_regs->ep_cmd, val, !(val & DMA_EP_CMD_DFLUSH), 1, 1000); pep->ep_state |= EP_STALLED; pep->ep_state &= ~EP_STALL_PENDING; } /* * Increment a trb index. * * The index should never point to the last link TRB in TR. After incrementing, * if it point to the link TRB, wrap around to the beginning and revert * cycle state bit. The link TRB is always at the last TRB entry. */ static void cdns2_ep_inc_trb(int *index, u8 *cs, int trb_in_seg) { (*index)++; if (*index == (trb_in_seg - 1)) { *index = 0; *cs ^= 1; } } static void cdns2_ep_inc_enq(struct cdns2_ring *ring) { ring->free_trbs--; cdns2_ep_inc_trb(&ring->enqueue, &ring->pcs, TRBS_PER_SEGMENT); } static void cdns2_ep_inc_deq(struct cdns2_ring *ring) { ring->free_trbs++; cdns2_ep_inc_trb(&ring->dequeue, &ring->ccs, TRBS_PER_SEGMENT); } /* * Enable/disable LPM. * * If bit USBCS_LPMNYET is not set and device receive Extended Token packet, * then controller answer with ACK handshake. * If bit USBCS_LPMNYET is set and device receive Extended Token packet, * then controller answer with NYET handshake. */ static void cdns2_enable_l1(struct cdns2_device *pdev, int enable) { if (enable) { clear_reg_bit_8(&pdev->usb_regs->usbcs, USBCS_LPMNYET); writeb(LPMCLOCK_SLEEP_ENTRY, &pdev->usb_regs->lpmclock); } else { set_reg_bit_8(&pdev->usb_regs->usbcs, USBCS_LPMNYET); } } static enum usb_device_speed cdns2_get_speed(struct cdns2_device *pdev) { u8 speed = readb(&pdev->usb_regs->speedctrl); if (speed & SPEEDCTRL_HS) return USB_SPEED_HIGH; else if (speed & SPEEDCTRL_FS) return USB_SPEED_FULL; return USB_SPEED_UNKNOWN; } static struct cdns2_trb *cdns2_next_trb(struct cdns2_endpoint *pep, struct cdns2_trb *trb) { if (trb == (pep->ring.trbs + (TRBS_PER_SEGMENT - 1))) return pep->ring.trbs; else return ++trb; } void cdns2_gadget_giveback(struct cdns2_endpoint *pep, struct cdns2_request *preq, int status) { struct usb_request *request = &preq->request; struct cdns2_device *pdev = pep->pdev; list_del_init(&preq->list); if (request->status == -EINPROGRESS) request->status = status; usb_gadget_unmap_request_by_dev(pdev->dev, request, pep->dir); /* All TRBs have finished, clear the counter. */ preq->finished_trb = 0; trace_cdns2_request_giveback(preq); if (request->complete) { spin_unlock(&pdev->lock); usb_gadget_giveback_request(&pep->endpoint, request); spin_lock(&pdev->lock); } if (request->buf == pdev->zlp_buf) cdns2_gadget_ep_free_request(&pep->endpoint, request); } static void cdns2_wa1_restore_cycle_bit(struct cdns2_endpoint *pep) { /* Work around for stale data address in TRB. */ if (pep->wa1_set) { trace_cdns2_wa1(pep, "restore cycle bit"); pep->wa1_set = 0; pep->wa1_trb_index = 0xFFFF; if (pep->wa1_cycle_bit) pep->wa1_trb->control |= cpu_to_le32(0x1); else pep->wa1_trb->control &= cpu_to_le32(~0x1); } } static int cdns2_wa1_update_guard(struct cdns2_endpoint *pep, struct cdns2_trb *trb) { struct cdns2_device *pdev = pep->pdev; if (!pep->wa1_set) { u32 doorbell; doorbell = !!(readl(&pdev->adma_regs->ep_cmd) & DMA_EP_CMD_DRDY); if (doorbell) { pep->wa1_cycle_bit = pep->ring.pcs ? TRB_CYCLE : 0; pep->wa1_set = 1; pep->wa1_trb = trb; pep->wa1_trb_index = pep->ring.enqueue; trace_cdns2_wa1(pep, "set guard"); return 0; } } return 1; } static void cdns2_wa1_tray_restore_cycle_bit(struct cdns2_device *pdev, struct cdns2_endpoint *pep) { int dma_index; u32 doorbell; doorbell = !!(readl(&pdev->adma_regs->ep_cmd) & DMA_EP_CMD_DRDY); dma_index = cdns2_get_dma_pos(pdev, pep); if (!doorbell || dma_index != pep->wa1_trb_index) cdns2_wa1_restore_cycle_bit(pep); } static int cdns2_prepare_ring(struct cdns2_device *pdev, struct cdns2_endpoint *pep, int num_trbs) { struct cdns2_trb *link_trb = NULL; int doorbell, dma_index; struct cdns2_ring *ring; u32 ch_bit = 0; ring = &pep->ring; if (num_trbs > ring->free_trbs) { pep->ep_state |= EP_RING_FULL; trace_cdns2_no_room_on_ring("Ring full\n"); return -ENOBUFS; } if ((ring->enqueue + num_trbs) >= (TRBS_PER_SEGMENT - 1)) { doorbell = !!(readl(&pdev->adma_regs->ep_cmd) & DMA_EP_CMD_DRDY); dma_index = cdns2_get_dma_pos(pdev, pep); /* Driver can't update LINK TRB if it is current processed. */ if (doorbell && dma_index == TRBS_PER_SEGMENT - 1) { pep->ep_state |= EP_DEFERRED_DRDY; return -ENOBUFS; } /* Update C bt in Link TRB before starting DMA. */ link_trb = ring->trbs + (TRBS_PER_SEGMENT - 1); /* * For TRs size equal 2 enabling TRB_CHAIN for epXin causes * that DMA stuck at the LINK TRB. * On the other hand, removing TRB_CHAIN for longer TRs for * epXout cause that DMA stuck after handling LINK TRB. * To eliminate this strange behavioral driver set TRB_CHAIN * bit only for TR size > 2. */ if (pep->type == USB_ENDPOINT_XFER_ISOC || TRBS_PER_SEGMENT > 2) ch_bit = TRB_CHAIN; link_trb->control = cpu_to_le32(((ring->pcs) ? TRB_CYCLE : 0) | TRB_TYPE(TRB_LINK) | TRB_TOGGLE | ch_bit); } return 0; } static void cdns2_dbg_request_trbs(struct cdns2_endpoint *pep, struct cdns2_request *preq) { struct cdns2_trb *link_trb = pep->ring.trbs + (TRBS_PER_SEGMENT - 1); struct cdns2_trb *trb = preq->trb; int num_trbs = preq->num_of_trb; int i = 0; while (i < num_trbs) { trace_cdns2_queue_trb(pep, trb + i); if (trb + i == link_trb) { trb = pep->ring.trbs; num_trbs = num_trbs - i; i = 0; } else { i++; } } } static unsigned int cdns2_count_trbs(struct cdns2_endpoint *pep, u64 addr, u64 len) { unsigned int num_trbs = 1; if (pep->type == USB_ENDPOINT_XFER_ISOC) { /* * To speed up DMA performance address should not exceed 4KB. * for high bandwidth transfer and driver will split * such buffer into two TRBs. */ num_trbs = DIV_ROUND_UP(len + (addr & (TRB_MAX_ISO_BUFF_SIZE - 1)), TRB_MAX_ISO_BUFF_SIZE); if (pep->interval > 1) num_trbs = pep->dir ? num_trbs * pep->interval : 1; } else if (pep->dir) { /* * One extra link trb for IN direction. * Sometimes DMA doesn't want advance to next TD and transfer * hangs. This extra Link TRB force DMA to advance to next TD. */ num_trbs++; } return num_trbs; } static unsigned int cdns2_count_sg_trbs(struct cdns2_endpoint *pep, struct usb_request *req) { unsigned int i, len, full_len, num_trbs = 0; struct scatterlist *sg; int trb_len = 0; full_len = req->length; for_each_sg(req->sg, sg, req->num_sgs, i) { len = sg_dma_len(sg); num_trbs += cdns2_count_trbs(pep, sg_dma_address(sg), len); len = min(len, full_len); /* * For HS ISO transfer TRBs should not exceed max packet size. * When DMA is working, and data exceed max packet size then * some data will be read in single mode instead burst mode. * This behavior will drastically reduce the copying speed. * To avoid this we need one or two extra TRBs. * This issue occurs for UVC class with sg_supported = 1 * because buffers addresses are not aligned to 1024. */ if (pep->type == USB_ENDPOINT_XFER_ISOC) { u8 temp; trb_len += len; temp = trb_len >> 10; if (temp) { if (trb_len % 1024) num_trbs = num_trbs + temp; else num_trbs = num_trbs + temp - 1; trb_len = trb_len - (temp << 10); } } full_len -= len; if (full_len == 0) break; } return num_trbs; } /* * Function prepares the array with optimized AXI burst value for different * transfer lengths. Controller handles the final data which are less * then AXI burst size as single byte transactions. * e.g.: * Let's assume that driver prepares trb with trb->length 700 and burst size * will be set to 128. In this case the controller will handle a first 512 as * single AXI transaction but the next 188 bytes will be handled * as 47 separate AXI transaction. * The better solution is to use the burst size equal 16 and then we will * have only 25 AXI transaction (10 * 64 + 15 *4). */ static void cdsn2_isoc_burst_opt(struct cdns2_device *pdev) { int axi_burst_option[] = {1, 2, 4, 8, 16, 32, 64, 128}; int best_burst; int array_size; int opt_burst; int trb_size; int i, j; array_size = ARRAY_SIZE(axi_burst_option); for (i = 0; i <= MAX_ISO_SIZE; i++) { trb_size = i / 4; best_burst = trb_size ? trb_size : 1; for (j = 0; j < array_size; j++) { opt_burst = trb_size / axi_burst_option[j]; opt_burst += trb_size % axi_burst_option[j]; if (opt_burst < best_burst) { best_burst = opt_burst; pdev->burst_opt[i] = axi_burst_option[j]; } } } } static void cdns2_ep_tx_isoc(struct cdns2_endpoint *pep, struct cdns2_request *preq, int num_trbs) { struct scatterlist *sg = NULL; u32 remaining_packet_size = 0; struct cdns2_trb *trb; bool first_trb = true; dma_addr_t trb_dma; u32 trb_buff_len; u32 block_length; int td_idx = 0; int split_size; u32 full_len; int enqd_len; int sent_len; int sg_iter; u32 control; int num_tds; u32 length; /* * For OUT direction 1 TD per interval is enough * because TRBs are not dumped by controller. */ num_tds = pep->dir ? pep->interval : 1; split_size = preq->request.num_sgs ? 1024 : 3072; for (td_idx = 0; td_idx < num_tds; td_idx++) { if (preq->request.num_sgs) { sg = preq->request.sg; trb_dma = sg_dma_address(sg); block_length = sg_dma_len(sg); } else { trb_dma = preq->request.dma; block_length = preq->request.length; } full_len = preq->request.length; sg_iter = preq->request.num_sgs ? preq->request.num_sgs : 1; remaining_packet_size = split_size; for (enqd_len = 0; enqd_len < full_len; enqd_len += trb_buff_len) { if (remaining_packet_size == 0) remaining_packet_size = split_size; /* * Calculate TRB length.- buffer can't across 4KB * and max packet size. */ trb_buff_len = TRB_BUFF_LEN_UP_TO_BOUNDARY(trb_dma); trb_buff_len = min(trb_buff_len, remaining_packet_size); trb_buff_len = min(trb_buff_len, block_length); if (trb_buff_len > full_len - enqd_len) trb_buff_len = full_len - enqd_len; control = TRB_TYPE(TRB_NORMAL); /* * For IN direction driver has to set the IOC for * last TRB in last TD. * For OUT direction driver must set IOC and ISP * only for last TRB in each TDs. */ if (enqd_len + trb_buff_len >= full_len || !pep->dir) control |= TRB_IOC | TRB_ISP; /* * Don't give the first TRB to the hardware (by toggling * the cycle bit) until we've finished creating all the * other TRBs. */ if (first_trb) { first_trb = false; if (pep->ring.pcs == 0) control |= TRB_CYCLE; } else { control |= pep->ring.pcs; } if (enqd_len + trb_buff_len < full_len) control |= TRB_CHAIN; length = TRB_LEN(trb_buff_len) | TRB_BURST(pep->pdev->burst_opt[trb_buff_len]); trb = pep->ring.trbs + pep->ring.enqueue; trb->buffer = cpu_to_le32(TRB_BUFFER(trb_dma)); trb->length = cpu_to_le32(length); trb->control = cpu_to_le32(control); trb_dma += trb_buff_len; sent_len = trb_buff_len; if (sg && sent_len >= block_length) { /* New sg entry */ --sg_iter; sent_len -= block_length; if (sg_iter != 0) { sg = sg_next(sg); trb_dma = sg_dma_address(sg); block_length = sg_dma_len(sg); } } remaining_packet_size -= trb_buff_len; block_length -= sent_len; preq->end_trb = pep->ring.enqueue; cdns2_ep_inc_enq(&pep->ring); } } } static void cdns2_ep_tx_bulk(struct cdns2_endpoint *pep, struct cdns2_request *preq, int trbs_per_td) { struct scatterlist *sg = NULL; struct cdns2_ring *ring; struct cdns2_trb *trb; dma_addr_t trb_dma; int sg_iter = 0; u32 control; u32 length; if (preq->request.num_sgs) { sg = preq->request.sg; trb_dma = sg_dma_address(sg); length = sg_dma_len(sg); } else { trb_dma = preq->request.dma; length = preq->request.length; } ring = &pep->ring; for (sg_iter = 0; sg_iter < trbs_per_td; sg_iter++) { control = TRB_TYPE(TRB_NORMAL) | ring->pcs | TRB_ISP; trb = pep->ring.trbs + ring->enqueue; if (pep->dir && sg_iter == trbs_per_td - 1) { preq->end_trb = ring->enqueue; control = ring->pcs | TRB_TYPE(TRB_LINK) | TRB_CHAIN | TRB_IOC; cdns2_ep_inc_enq(&pep->ring); if (ring->enqueue == 0) control |= TRB_TOGGLE; /* Point to next bad TRB. */ trb->buffer = cpu_to_le32(pep->ring.dma + (ring->enqueue * TRB_SIZE)); trb->length = 0; trb->control = cpu_to_le32(control); break; } /* * Don't give the first TRB to the hardware (by toggling * the cycle bit) until we've finished creating all the * other TRBs. */ if (sg_iter == 0) control = control ^ TRB_CYCLE; /* For last TRB in TD. */ if (sg_iter == (trbs_per_td - (pep->dir ? 2 : 1))) control |= TRB_IOC; else control |= TRB_CHAIN; trb->buffer = cpu_to_le32(trb_dma); trb->length = cpu_to_le32(TRB_BURST(pep->trb_burst_size) | TRB_LEN(length)); trb->control = cpu_to_le32(control); if (sg && sg_iter < (trbs_per_td - 1)) { sg = sg_next(sg); trb_dma = sg_dma_address(sg); length = sg_dma_len(sg); } preq->end_trb = ring->enqueue; cdns2_ep_inc_enq(&pep->ring); } } static void cdns2_set_drdy(struct cdns2_device *pdev, struct cdns2_endpoint *pep) { trace_cdns2_ring(pep); /* * Memory barrier - Cycle Bit must be set before doorbell. */ dma_wmb(); /* Clearing TRBERR and DESCMIS before setting DRDY. */ writel(DMA_EP_STS_TRBERR | DMA_EP_STS_DESCMIS, &pdev->adma_regs->ep_sts); writel(DMA_EP_CMD_DRDY, &pdev->adma_regs->ep_cmd); if (readl(&pdev->adma_regs->ep_sts) & DMA_EP_STS_TRBERR) { writel(DMA_EP_STS_TRBERR, &pdev->adma_regs->ep_sts); writel(DMA_EP_CMD_DRDY, &pdev->adma_regs->ep_cmd); } trace_cdns2_doorbell_epx(pep, readl(&pdev->adma_regs->ep_traddr)); } static int cdns2_prepare_first_isoc_transfer(struct cdns2_device *pdev, struct cdns2_endpoint *pep) { struct cdns2_trb *trb; u32 buffer; u8 hw_ccs; if ((readl(&pdev->adma_regs->ep_cmd) & DMA_EP_CMD_DRDY)) return -EBUSY; if (!pep->dir) { set_reg_bit_32(&pdev->adma_regs->ep_cfg, DMA_EP_CFG_ENABLE); writel(pep->ring.dma + pep->ring.dequeue, &pdev->adma_regs->ep_traddr); return 0; } /* * The first packet after doorbell can be corrupted so, * driver prepares 0 length packet as first packet. */ buffer = pep->ring.dma + pep->ring.dequeue * TRB_SIZE; hw_ccs = !!DMA_EP_STS_CCS(readl(&pdev->adma_regs->ep_sts)); trb = &pep->ring.trbs[TRBS_PER_SEGMENT]; trb->length = 0; trb->buffer = cpu_to_le32(TRB_BUFFER(buffer)); trb->control = cpu_to_le32((hw_ccs ? TRB_CYCLE : 0) | TRB_TYPE(TRB_NORMAL)); /* * LINK TRB is used to force updating cycle bit in controller and * move to correct place in transfer ring. */ trb++; trb->length = 0; trb->buffer = cpu_to_le32(TRB_BUFFER(buffer)); trb->control = cpu_to_le32((hw_ccs ? TRB_CYCLE : 0) | TRB_TYPE(TRB_LINK) | TRB_CHAIN); if (hw_ccs != pep->ring.ccs) trb->control |= cpu_to_le32(TRB_TOGGLE); set_reg_bit_32(&pdev->adma_regs->ep_cfg, DMA_EP_CFG_ENABLE); writel(pep->ring.dma + (TRBS_PER_SEGMENT * TRB_SIZE), &pdev->adma_regs->ep_traddr); return 0; } /* Prepare and start transfer on no-default endpoint. */ static int cdns2_ep_run_transfer(struct cdns2_endpoint *pep, struct cdns2_request *preq) { struct cdns2_device *pdev = pep->pdev; struct cdns2_ring *ring; u32 togle_pcs = 1; int num_trbs; int ret; cdns2_select_ep(pdev, pep->endpoint.address); if (preq->request.sg) num_trbs = cdns2_count_sg_trbs(pep, &preq->request); else num_trbs = cdns2_count_trbs(pep, preq->request.dma, preq->request.length); ret = cdns2_prepare_ring(pdev, pep, num_trbs); if (ret) return ret; ring = &pep->ring; preq->start_trb = ring->enqueue; preq->trb = ring->trbs + ring->enqueue; if (usb_endpoint_xfer_isoc(pep->endpoint.desc)) { cdns2_ep_tx_isoc(pep, preq, num_trbs); } else { togle_pcs = cdns2_wa1_update_guard(pep, ring->trbs + ring->enqueue); cdns2_ep_tx_bulk(pep, preq, num_trbs); } preq->num_of_trb = num_trbs; /* * Memory barrier - cycle bit must be set as the last operation. */ dma_wmb(); /* Give the TD to the consumer. */ if (togle_pcs) preq->trb->control = preq->trb->control ^ cpu_to_le32(1); cdns2_wa1_tray_restore_cycle_bit(pdev, pep); cdns2_dbg_request_trbs(pep, preq); if (!pep->wa1_set && !(pep->ep_state & EP_STALLED) && !pep->skip) { if (pep->type == USB_ENDPOINT_XFER_ISOC) { ret = cdns2_prepare_first_isoc_transfer(pdev, pep); if (ret) return 0; } cdns2_set_drdy(pdev, pep); } return 0; } /* Prepare and start transfer for all not started requests. */ static int cdns2_start_all_request(struct cdns2_device *pdev, struct cdns2_endpoint *pep) { struct cdns2_request *preq; int ret; while (!list_empty(&pep->deferred_list)) { preq = cdns2_next_preq(&pep->deferred_list); ret = cdns2_ep_run_transfer(pep, preq); if (ret) return ret; list_move_tail(&preq->list, &pep->pending_list); } pep->ep_state &= ~EP_RING_FULL; return 0; } /* * Check whether trb has been handled by DMA. * * Endpoint must be selected before invoking this function. * * Returns false if request has not been handled by DMA, else returns true. * * SR - start ring * ER - end ring * DQ = ring->dequeue - dequeue position * EQ = ring->enqueue - enqueue position * ST = preq->start_trb - index of first TRB in transfer ring * ET = preq->end_trb - index of last TRB in transfer ring * CI = current_index - index of processed TRB by DMA. * * As first step, we check if the TRB between the ST and ET. * Then, we check if cycle bit for index pep->dequeue * is correct. * * some rules: * 1. ring->dequeue never equals to current_index. * 2 ring->enqueue never exceed ring->dequeue * 3. exception: ring->enqueue == ring->dequeue * and ring->free_trbs is zero. * This case indicate that TR is full. * * At below two cases, the request have been handled. * Case 1 - ring->dequeue < current_index * SR ... EQ ... DQ ... CI ... ER * SR ... DQ ... CI ... EQ ... ER * * Case 2 - ring->dequeue > current_index * This situation takes place when CI go through the LINK TRB at the end of * transfer ring. * SR ... CI ... EQ ... DQ ... ER */ static bool cdns2_trb_handled(struct cdns2_endpoint *pep, struct cdns2_request *preq) { struct cdns2_device *pdev = pep->pdev; struct cdns2_ring *ring; struct cdns2_trb *trb; int current_index = 0; int handled = 0; int doorbell; ring = &pep->ring; current_index = cdns2_get_dma_pos(pdev, pep); doorbell = !!(readl(&pdev->adma_regs->ep_cmd) & DMA_EP_CMD_DRDY); /* * Only ISO transfer can use 2 entries outside the standard * Transfer Ring. First of them is used as zero length packet and the * second as LINK TRB. */ if (current_index >= TRBS_PER_SEGMENT) goto finish; /* Current trb doesn't belong to this request. */ if (preq->start_trb < preq->end_trb) { if (ring->dequeue > preq->end_trb) goto finish; if (ring->dequeue < preq->start_trb) goto finish; } if (preq->start_trb > preq->end_trb && ring->dequeue > preq->end_trb && ring->dequeue < preq->start_trb) goto finish; if (preq->start_trb == preq->end_trb && ring->dequeue != preq->end_trb) goto finish; trb = &ring->trbs[ring->dequeue]; if ((le32_to_cpu(trb->control) & TRB_CYCLE) != ring->ccs) goto finish; if (doorbell == 1 && current_index == ring->dequeue) goto finish; /* The corner case for TRBS_PER_SEGMENT equal 2). */ if (TRBS_PER_SEGMENT == 2 && pep->type != USB_ENDPOINT_XFER_ISOC) { handled = 1; goto finish; } if (ring->enqueue == ring->dequeue && ring->free_trbs == 0) { handled = 1; } else if (ring->dequeue < current_index) { if ((current_index == (TRBS_PER_SEGMENT - 1)) && !ring->dequeue) goto finish; handled = 1; } else if (ring->dequeue > current_index) { handled = 1; } finish: trace_cdns2_request_handled(preq, current_index, handled); return handled; } static void cdns2_skip_isoc_td(struct cdns2_device *pdev, struct cdns2_endpoint *pep, struct cdns2_request *preq) { struct cdns2_trb *trb; int i; trb = pep->ring.trbs + pep->ring.dequeue; for (i = preq->finished_trb ; i < preq->num_of_trb; i++) { preq->finished_trb++; trace_cdns2_complete_trb(pep, trb); cdns2_ep_inc_deq(&pep->ring); trb = cdns2_next_trb(pep, trb); } cdns2_gadget_giveback(pep, preq, 0); cdns2_prepare_first_isoc_transfer(pdev, pep); pep->skip = false; cdns2_set_drdy(pdev, pep); } static void cdns2_transfer_completed(struct cdns2_device *pdev, struct cdns2_endpoint *pep) { struct cdns2_request *preq = NULL; bool request_handled = false; struct cdns2_trb *trb; while (!list_empty(&pep->pending_list)) { preq = cdns2_next_preq(&pep->pending_list); trb = pep->ring.trbs + pep->ring.dequeue; /* * The TRB was changed as link TRB, and the request * was handled at ep_dequeue. */ while (TRB_FIELD_TO_TYPE(le32_to_cpu(trb->control)) == TRB_LINK && le32_to_cpu(trb->length)) { trace_cdns2_complete_trb(pep, trb); cdns2_ep_inc_deq(&pep->ring); trb = pep->ring.trbs + pep->ring.dequeue; } /* * Re-select endpoint. It could be changed by other CPU * during handling usb_gadget_giveback_request. */ cdns2_select_ep(pdev, pep->endpoint.address); while (cdns2_trb_handled(pep, preq)) { preq->finished_trb++; if (preq->finished_trb >= preq->num_of_trb) request_handled = true; trb = pep->ring.trbs + pep->ring.dequeue; trace_cdns2_complete_trb(pep, trb); if (pep->dir && pep->type == USB_ENDPOINT_XFER_ISOC) /* * For ISOC IN controller doens't update the * trb->length. */ preq->request.actual = preq->request.length; else preq->request.actual += TRB_LEN(le32_to_cpu(trb->length)); cdns2_ep_inc_deq(&pep->ring); } if (request_handled) { cdns2_gadget_giveback(pep, preq, 0); request_handled = false; } else { goto prepare_next_td; } if (pep->type != USB_ENDPOINT_XFER_ISOC && TRBS_PER_SEGMENT == 2) break; } prepare_next_td: if (pep->skip && preq) cdns2_skip_isoc_td(pdev, pep, preq); if (!(pep->ep_state & EP_STALLED) && !(pep->ep_state & EP_STALL_PENDING)) cdns2_start_all_request(pdev, pep); } static void cdns2_wakeup(struct cdns2_device *pdev) { if (!pdev->may_wakeup) return; /* Start driving resume signaling to indicate remote wakeup. */ set_reg_bit_8(&pdev->usb_regs->usbcs, USBCS_SIGRSUME); } static void cdns2_rearm_transfer(struct cdns2_endpoint *pep, u8 rearm) { struct cdns2_device *pdev = pep->pdev; cdns2_wa1_restore_cycle_bit(pep); if (rearm) { trace_cdns2_ring(pep); /* Cycle Bit must be updated before arming DMA. */ dma_wmb(); writel(DMA_EP_CMD_DRDY, &pdev->adma_regs->ep_cmd); cdns2_wakeup(pdev); trace_cdns2_doorbell_epx(pep, readl(&pdev->adma_regs->ep_traddr)); } } static void cdns2_handle_epx_interrupt(struct cdns2_endpoint *pep) { struct cdns2_device *pdev = pep->pdev; u8 isoerror = 0; u32 ep_sts_reg; u32 val; cdns2_select_ep(pdev, pep->endpoint.address); trace_cdns2_epx_irq(pdev, pep); ep_sts_reg = readl(&pdev->adma_regs->ep_sts); writel(ep_sts_reg, &pdev->adma_regs->ep_sts); if (pep->type == USB_ENDPOINT_XFER_ISOC) { u8 mult; u8 cs; mult = USB_EP_MAXP_MULT(pep->endpoint.desc->wMaxPacketSize); cs = pep->dir ? readb(&pdev->epx_regs->ep[pep->num - 1].txcs) : readb(&pdev->epx_regs->ep[pep->num - 1].rxcs); if (mult > 0) isoerror = EPX_CS_ERR(cs); } /* * Sometimes ISO Error for mult=1 or mult=2 is not propagated on time * from USB module to DMA module. To protect against this driver * checks also the txcs/rxcs registers. */ if ((ep_sts_reg & DMA_EP_STS_ISOERR) || isoerror) { clear_reg_bit_32(&pdev->adma_regs->ep_cfg, DMA_EP_CFG_ENABLE); /* Wait for DBUSY cleared. */ readl_poll_timeout_atomic(&pdev->adma_regs->ep_sts, val, !(val & DMA_EP_STS_DBUSY), 1, 125); writel(DMA_EP_CMD_DFLUSH, &pep->pdev->adma_regs->ep_cmd); /* Wait for DFLUSH cleared. */ readl_poll_timeout_atomic(&pep->pdev->adma_regs->ep_cmd, val, !(val & DMA_EP_CMD_DFLUSH), 1, 10); pep->skip = true; } if (ep_sts_reg & DMA_EP_STS_TRBERR || pep->skip) { if (pep->ep_state & EP_STALL_PENDING && !(ep_sts_reg & DMA_EP_STS_DESCMIS)) cdns2_ep_stall_flush(pep); /* * For isochronous transfer driver completes request on * IOC or on TRBERR. IOC appears only when device receive * OUT data packet. If host disable stream or lost some packet * then the only way to finish all queued transfer is to do it * on TRBERR event. */ if (pep->type == USB_ENDPOINT_XFER_ISOC && !pep->wa1_set) { if (!pep->dir) clear_reg_bit_32(&pdev->adma_regs->ep_cfg, DMA_EP_CFG_ENABLE); cdns2_transfer_completed(pdev, pep); if (pep->ep_state & EP_DEFERRED_DRDY) { pep->ep_state &= ~EP_DEFERRED_DRDY; cdns2_set_drdy(pdev, pep); } return; } cdns2_transfer_completed(pdev, pep); if (!(pep->ep_state & EP_STALLED) && !(pep->ep_state & EP_STALL_PENDING)) { if (pep->ep_state & EP_DEFERRED_DRDY) { pep->ep_state &= ~EP_DEFERRED_DRDY; cdns2_start_all_request(pdev, pep); } else { cdns2_rearm_transfer(pep, pep->wa1_set); } } return; } if ((ep_sts_reg & DMA_EP_STS_IOC) || (ep_sts_reg & DMA_EP_STS_ISP)) cdns2_transfer_completed(pdev, pep); } static void cdns2_disconnect_gadget(struct cdns2_device *pdev) { if (pdev->gadget_driver && pdev->gadget_driver->disconnect) pdev->gadget_driver->disconnect(&pdev->gadget); } static irqreturn_t cdns2_usb_irq_handler(int irq, void *data) { struct cdns2_device *pdev = data; unsigned long reg_ep_ists; u8 reg_usb_irq_m; u8 reg_ext_irq_m; u8 reg_usb_irq; u8 reg_ext_irq; if (pdev->in_lpm) return IRQ_NONE; reg_usb_irq_m = readb(&pdev->interrupt_regs->usbien); reg_ext_irq_m = readb(&pdev->interrupt_regs->extien); /* Mask all sources of interrupt. */ writeb(0, &pdev->interrupt_regs->usbien); writeb(0, &pdev->interrupt_regs->extien); writel(0, &pdev->adma_regs->ep_ien); /* Clear interrupt sources. */ writel(0, &pdev->adma_regs->ep_sts); writeb(0, &pdev->interrupt_regs->usbirq); writeb(0, &pdev->interrupt_regs->extirq); reg_ep_ists = readl(&pdev->adma_regs->ep_ists); reg_usb_irq = readb(&pdev->interrupt_regs->usbirq); reg_ext_irq = readb(&pdev->interrupt_regs->extirq); if (reg_ep_ists || (reg_usb_irq & reg_usb_irq_m) || (reg_ext_irq & reg_ext_irq_m)) return IRQ_WAKE_THREAD; writeb(USB_IEN_INIT, &pdev->interrupt_regs->usbien); writeb(EXTIRQ_WAKEUP, &pdev->interrupt_regs->extien); writel(~0, &pdev->adma_regs->ep_ien); return IRQ_NONE; } static irqreturn_t cdns2_thread_usb_irq_handler(struct cdns2_device *pdev) { u8 usb_irq, ext_irq; int speed; int i; ext_irq = readb(&pdev->interrupt_regs->extirq) & EXTIRQ_WAKEUP; writeb(ext_irq, &pdev->interrupt_regs->extirq); usb_irq = readb(&pdev->interrupt_regs->usbirq) & USB_IEN_INIT; writeb(usb_irq, &pdev->interrupt_regs->usbirq); if (!ext_irq && !usb_irq) return IRQ_NONE; trace_cdns2_usb_irq(usb_irq, ext_irq); if (ext_irq & EXTIRQ_WAKEUP) { if (pdev->gadget_driver && pdev->gadget_driver->resume) { spin_unlock(&pdev->lock); pdev->gadget_driver->resume(&pdev->gadget); spin_lock(&pdev->lock); } } if (usb_irq & USBIRQ_LPM) { u8 reg = readb(&pdev->usb_regs->lpmctrl); /* LPM1 enter */ if (!(reg & LPMCTRLLH_LPMNYET)) writeb(0, &pdev->usb_regs->sleep_clkgate); } if (usb_irq & USBIRQ_SUSPEND) { if (pdev->gadget_driver && pdev->gadget_driver->suspend) { spin_unlock(&pdev->lock); pdev->gadget_driver->suspend(&pdev->gadget); spin_lock(&pdev->lock); } } if (usb_irq & USBIRQ_URESET) { if (pdev->gadget_driver) { pdev->dev_address = 0; spin_unlock(&pdev->lock); usb_gadget_udc_reset(&pdev->gadget, pdev->gadget_driver); spin_lock(&pdev->lock); /* * The USBIRQ_URESET is reported at the beginning of * reset signal. 100ms is enough time to finish reset * process. For high-speed reset procedure is completed * when controller detect HS mode. */ for (i = 0; i < 100; i++) { mdelay(1); speed = cdns2_get_speed(pdev); if (speed == USB_SPEED_HIGH) break; } pdev->gadget.speed = speed; cdns2_enable_l1(pdev, 0); cdns2_ep0_config(pdev); pdev->may_wakeup = 0; } } if (usb_irq & USBIRQ_SUDAV) { pdev->ep0_stage = CDNS2_SETUP_STAGE; cdns2_handle_setup_packet(pdev); } return IRQ_HANDLED; } /* Deferred USB interrupt handler. */ static irqreturn_t cdns2_thread_irq_handler(int irq, void *data) { struct cdns2_device *pdev = data; unsigned long dma_ep_ists; unsigned long flags; unsigned int bit; local_bh_disable(); spin_lock_irqsave(&pdev->lock, flags); cdns2_thread_usb_irq_handler(pdev); dma_ep_ists = readl(&pdev->adma_regs->ep_ists); if (!dma_ep_ists) goto unlock; trace_cdns2_dma_ep_ists(dma_ep_ists); /* Handle default endpoint OUT. */ if (dma_ep_ists & DMA_EP_ISTS_EP_OUT0) cdns2_handle_ep0_interrupt(pdev, USB_DIR_OUT); /* Handle default endpoint IN. */ if (dma_ep_ists & DMA_EP_ISTS_EP_IN0) cdns2_handle_ep0_interrupt(pdev, USB_DIR_IN); dma_ep_ists &= ~(DMA_EP_ISTS_EP_OUT0 | DMA_EP_ISTS_EP_IN0); for_each_set_bit(bit, &dma_ep_ists, sizeof(u32) * BITS_PER_BYTE) { u8 ep_idx = bit > 16 ? (bit - 16) * 2 : (bit * 2) - 1; /* * Endpoints in pdev->eps[] are held in order: * ep0, ep1out, ep1in, ep2out, ep2in... ep15out, ep15in. * but in dma_ep_ists in order: * ep0 ep1out ep2out ... ep15out ep0in ep1in .. ep15in */ cdns2_handle_epx_interrupt(&pdev->eps[ep_idx]); } unlock: writel(~0, &pdev->adma_regs->ep_ien); writeb(USB_IEN_INIT, &pdev->interrupt_regs->usbien); writeb(EXTIRQ_WAKEUP, &pdev->interrupt_regs->extien); spin_unlock_irqrestore(&pdev->lock, flags); local_bh_enable(); return IRQ_HANDLED; } /* Calculates and assigns onchip memory for endpoints. */ static void cdns2_eps_onchip_buffer_init(struct cdns2_device *pdev) { struct cdns2_endpoint *pep; int min_buf_tx = 0; int min_buf_rx = 0; u16 tx_offset = 0; u16 rx_offset = 0; int free; int i; for (i = 0; i < CDNS2_ENDPOINTS_NUM; i++) { pep = &pdev->eps[i]; if (!(pep->ep_state & EP_CLAIMED)) continue; if (pep->dir) min_buf_tx += pep->buffering; else min_buf_rx += pep->buffering; } for (i = 0; i < CDNS2_ENDPOINTS_NUM; i++) { pep = &pdev->eps[i]; if (!(pep->ep_state & EP_CLAIMED)) continue; if (pep->dir) { free = pdev->onchip_tx_buf - min_buf_tx; if (free + pep->buffering >= 4) free = 4; else free = free + pep->buffering; min_buf_tx = min_buf_tx - pep->buffering + free; pep->buffering = free; writel(tx_offset, &pdev->epx_regs->txstaddr[pep->num - 1]); pdev->epx_regs->txstaddr[pep->num - 1] = tx_offset; dev_dbg(pdev->dev, "%s onchip address %04x, buffering: %d\n", pep->name, tx_offset, pep->buffering); tx_offset += pep->buffering * 1024; } else { free = pdev->onchip_rx_buf - min_buf_rx; if (free + pep->buffering >= 4) free = 4; else free = free + pep->buffering; min_buf_rx = min_buf_rx - pep->buffering + free; pep->buffering = free; writel(rx_offset, &pdev->epx_regs->rxstaddr[pep->num - 1]); dev_dbg(pdev->dev, "%s onchip address %04x, buffering: %d\n", pep->name, rx_offset, pep->buffering); rx_offset += pep->buffering * 1024; } } } /* Configure hardware endpoint. */ static int cdns2_ep_config(struct cdns2_endpoint *pep, bool enable) { bool is_iso_ep = (pep->type == USB_ENDPOINT_XFER_ISOC); struct cdns2_device *pdev = pep->pdev; u32 max_packet_size; u8 dir = 0; u8 ep_cfg; u8 mult; u32 val; int ret; switch (pep->type) { case USB_ENDPOINT_XFER_INT: ep_cfg = EPX_CON_TYPE_INT; break; case USB_ENDPOINT_XFER_BULK: ep_cfg = EPX_CON_TYPE_BULK; break; default: mult = USB_EP_MAXP_MULT(pep->endpoint.desc->wMaxPacketSize); ep_cfg = mult << EPX_CON_ISOD_SHIFT; ep_cfg |= EPX_CON_TYPE_ISOC; if (pep->dir) { set_reg_bit_8(&pdev->epx_regs->isoautoarm, BIT(pep->num)); set_reg_bit_8(&pdev->epx_regs->isoautodump, BIT(pep->num)); set_reg_bit_8(&pdev->epx_regs->isodctrl, BIT(pep->num)); } } switch (pdev->gadget.speed) { case USB_SPEED_FULL: max_packet_size = is_iso_ep ? 1023 : 64; break; case USB_SPEED_HIGH: max_packet_size = is_iso_ep ? 1024 : 512; break; default: /* All other speed are not supported. */ return -EINVAL; } ep_cfg |= (EPX_CON_VAL | (pep->buffering - 1)); if (pep->dir) { dir = FIFOCTRL_IO_TX; writew(max_packet_size, &pdev->epx_regs->txmaxpack[pep->num - 1]); writeb(ep_cfg, &pdev->epx_regs->ep[pep->num - 1].txcon); } else { writew(max_packet_size, &pdev->epx_regs->rxmaxpack[pep->num - 1]); writeb(ep_cfg, &pdev->epx_regs->ep[pep->num - 1].rxcon); } writeb(pep->num | dir | FIFOCTRL_FIFOAUTO, &pdev->usb_regs->fifoctrl); writeb(pep->num | dir, &pdev->epx_regs->endprst); writeb(pep->num | ENDPRST_FIFORST | ENDPRST_TOGRST | dir, &pdev->epx_regs->endprst); if (max_packet_size == 1024) pep->trb_burst_size = 128; else if (max_packet_size >= 512) pep->trb_burst_size = 64; else pep->trb_burst_size = 16; cdns2_select_ep(pdev, pep->num | pep->dir); writel(DMA_EP_CMD_EPRST | DMA_EP_CMD_DFLUSH, &pdev->adma_regs->ep_cmd); ret = readl_poll_timeout_atomic(&pdev->adma_regs->ep_cmd, val, !(val & (DMA_EP_CMD_DFLUSH | DMA_EP_CMD_EPRST)), 1, 1000); if (ret) return ret; writel(DMA_EP_STS_TRBERR | DMA_EP_STS_ISOERR, &pdev->adma_regs->ep_sts_en); if (enable) writel(DMA_EP_CFG_ENABLE, &pdev->adma_regs->ep_cfg); trace_cdns2_epx_hw_cfg(pdev, pep); dev_dbg(pdev->dev, "Configure %s: with MPS: %08x, ep con: %02x\n", pep->name, max_packet_size, ep_cfg); return 0; } struct usb_request *cdns2_gadget_ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags) { struct cdns2_endpoint *pep = ep_to_cdns2_ep(ep); struct cdns2_request *preq; preq = kzalloc(sizeof(*preq), gfp_flags); if (!preq) return NULL; preq->pep = pep; trace_cdns2_alloc_request(preq); return &preq->request; } void cdns2_gadget_ep_free_request(struct usb_ep *ep, struct usb_request *request) { struct cdns2_request *preq = to_cdns2_request(request); trace_cdns2_free_request(preq); kfree(preq); } static int cdns2_gadget_ep_enable(struct usb_ep *ep, const struct usb_endpoint_descriptor *desc) { u32 reg = DMA_EP_STS_EN_TRBERREN; struct cdns2_endpoint *pep; struct cdns2_device *pdev; unsigned long flags; int enable = 1; int ret = 0; if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT || !desc->wMaxPacketSize) { return -EINVAL; } pep = ep_to_cdns2_ep(ep); pdev = pep->pdev; if (dev_WARN_ONCE(pdev->dev, pep->ep_state & EP_ENABLED, "%s is already enabled\n", pep->name)) return 0; spin_lock_irqsave(&pdev->lock, flags); pep->type = usb_endpoint_type(desc); pep->interval = desc->bInterval ? BIT(desc->bInterval - 1) : 0; if (pdev->gadget.speed == USB_SPEED_FULL) if (pep->type == USB_ENDPOINT_XFER_INT) pep->interval = desc->bInterval; if (pep->interval > ISO_MAX_INTERVAL && pep->type == USB_ENDPOINT_XFER_ISOC) { dev_err(pdev->dev, "ISO period is limited to %d (current: %d)\n", ISO_MAX_INTERVAL, pep->interval); ret = -EINVAL; goto exit; } /* * During ISO OUT traffic DMA reads Transfer Ring for the EP which has * never got doorbell. * This issue was detected only on simulation, but to avoid this issue * driver add protection against it. To fix it driver enable ISO OUT * endpoint before setting DRBL. This special treatment of ISO OUT * endpoints are recommended by controller specification. */ if (pep->type == USB_ENDPOINT_XFER_ISOC && !pep->dir) enable = 0; ret = cdns2_alloc_tr_segment(pep); if (ret) goto exit; ret = cdns2_ep_config(pep, enable); if (ret) { cdns2_free_tr_segment(pep); ret = -EINVAL; goto exit; } trace_cdns2_gadget_ep_enable(pep); pep->ep_state &= ~(EP_STALLED | EP_STALL_PENDING); pep->ep_state |= EP_ENABLED; pep->wa1_set = 0; pep->ring.enqueue = 0; pep->ring.dequeue = 0; reg = readl(&pdev->adma_regs->ep_sts); pep->ring.pcs = !!DMA_EP_STS_CCS(reg); pep->ring.ccs = !!DMA_EP_STS_CCS(reg); writel(pep->ring.dma, &pdev->adma_regs->ep_traddr); /* one TRB is reserved for link TRB used in DMULT mode*/ pep->ring.free_trbs = TRBS_PER_SEGMENT - 1; exit: spin_unlock_irqrestore(&pdev->lock, flags); return ret; } static int cdns2_gadget_ep_disable(struct usb_ep *ep) { struct cdns2_endpoint *pep; struct cdns2_request *preq; struct cdns2_device *pdev; unsigned long flags; int val; if (!ep) return -EINVAL; pep = ep_to_cdns2_ep(ep); pdev = pep->pdev; if (dev_WARN_ONCE(pdev->dev, !(pep->ep_state & EP_ENABLED), "%s is already disabled\n", pep->name)) return 0; spin_lock_irqsave(&pdev->lock, flags); trace_cdns2_gadget_ep_disable(pep); cdns2_select_ep(pdev, ep->desc->bEndpointAddress); clear_reg_bit_32(&pdev->adma_regs->ep_cfg, DMA_EP_CFG_ENABLE); /* * Driver needs some time before resetting endpoint. * It need waits for clearing DBUSY bit or for timeout expired. * 10us is enough time for controller to stop transfer. */ readl_poll_timeout_atomic(&pdev->adma_regs->ep_sts, val, !(val & DMA_EP_STS_DBUSY), 1, 10); writel(DMA_EP_CMD_EPRST, &pdev->adma_regs->ep_cmd); readl_poll_timeout_atomic(&pdev->adma_regs->ep_cmd, val, !(val & (DMA_EP_CMD_DFLUSH | DMA_EP_CMD_EPRST)), 1, 1000); while (!list_empty(&pep->pending_list)) { preq = cdns2_next_preq(&pep->pending_list); cdns2_gadget_giveback(pep, preq, -ESHUTDOWN); } while (!list_empty(&pep->deferred_list)) { preq = cdns2_next_preq(&pep->deferred_list); cdns2_gadget_giveback(pep, preq, -ESHUTDOWN); } ep->desc = NULL; pep->ep_state &= ~EP_ENABLED; spin_unlock_irqrestore(&pdev->lock, flags); return 0; } static int cdns2_ep_enqueue(struct cdns2_endpoint *pep, struct cdns2_request *preq, gfp_t gfp_flags) { struct cdns2_device *pdev = pep->pdev; struct usb_request *request; int ret; request = &preq->request; request->actual = 0; request->status = -EINPROGRESS; ret = usb_gadget_map_request_by_dev(pdev->dev, request, pep->dir); if (ret) { trace_cdns2_request_enqueue_error(preq); return ret; } list_add_tail(&preq->list, &pep->deferred_list); trace_cdns2_request_enqueue(preq); if (!(pep->ep_state & EP_STALLED) && !(pep->ep_state & EP_STALL_PENDING)) cdns2_start_all_request(pdev, pep); return 0; } static int cdns2_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request, gfp_t gfp_flags) { struct usb_request *zlp_request; struct cdns2_request *preq; struct cdns2_endpoint *pep; struct cdns2_device *pdev; unsigned long flags; int ret; if (!request || !ep) return -EINVAL; pep = ep_to_cdns2_ep(ep); pdev = pep->pdev; if (!(pep->ep_state & EP_ENABLED)) { dev_err(pdev->dev, "%s: can't queue to disabled endpoint\n", pep->name); return -EINVAL; } spin_lock_irqsave(&pdev->lock, flags); preq = to_cdns2_request(request); ret = cdns2_ep_enqueue(pep, preq, gfp_flags); if (ret == 0 && request->zero && request->length && (request->length % ep->maxpacket == 0)) { struct cdns2_request *preq; zlp_request = cdns2_gadget_ep_alloc_request(ep, GFP_ATOMIC); zlp_request->buf = pdev->zlp_buf; zlp_request->length = 0; preq = to_cdns2_request(zlp_request); ret = cdns2_ep_enqueue(pep, preq, gfp_flags); } spin_unlock_irqrestore(&pdev->lock, flags); return ret; } int cdns2_gadget_ep_dequeue(struct usb_ep *ep, struct usb_request *request) { struct cdns2_request *preq, *preq_temp, *cur_preq; struct cdns2_endpoint *pep; struct cdns2_trb *link_trb; u8 req_on_hw_ring = 0; unsigned long flags; u32 buffer; int val, i; if (!ep || !request || !ep->desc) return -EINVAL; pep = ep_to_cdns2_ep(ep); if (!pep->endpoint.desc) { dev_err(pep->pdev->dev, "%s: can't dequeue to disabled endpoint\n", pep->name); return -ESHUTDOWN; } /* Requests has been dequeued during disabling endpoint. */ if (!(pep->ep_state & EP_ENABLED)) return 0; spin_lock_irqsave(&pep->pdev->lock, flags); cur_preq = to_cdns2_request(request); trace_cdns2_request_dequeue(cur_preq); list_for_each_entry_safe(preq, preq_temp, &pep->pending_list, list) { if (cur_preq == preq) { req_on_hw_ring = 1; goto found; } } list_for_each_entry_safe(preq, preq_temp, &pep->deferred_list, list) { if (cur_preq == preq) goto found; } goto not_found; found: link_trb = preq->trb; /* Update ring only if removed request is on pending_req_list list. */ if (req_on_hw_ring && link_trb) { /* Stop DMA */ writel(DMA_EP_CMD_DFLUSH, &pep->pdev->adma_regs->ep_cmd); /* Wait for DFLUSH cleared. */ readl_poll_timeout_atomic(&pep->pdev->adma_regs->ep_cmd, val, !(val & DMA_EP_CMD_DFLUSH), 1, 1000); buffer = cpu_to_le32(TRB_BUFFER(pep->ring.dma + ((preq->end_trb + 1) * TRB_SIZE))); for (i = 0; i < preq->num_of_trb; i++) { link_trb->buffer = buffer; link_trb->control = cpu_to_le32((le32_to_cpu(link_trb->control) & TRB_CYCLE) | TRB_CHAIN | TRB_TYPE(TRB_LINK)); trace_cdns2_queue_trb(pep, link_trb); link_trb = cdns2_next_trb(pep, link_trb); } if (pep->wa1_trb == preq->trb) cdns2_wa1_restore_cycle_bit(pep); } cdns2_gadget_giveback(pep, cur_preq, -ECONNRESET); preq = cdns2_next_preq(&pep->pending_list); if (preq) cdns2_rearm_transfer(pep, 1); not_found: spin_unlock_irqrestore(&pep->pdev->lock, flags); return 0; } int cdns2_halt_endpoint(struct cdns2_device *pdev, struct cdns2_endpoint *pep, int value) { u8 __iomem *conf; int dir = 0; if (!(pep->ep_state & EP_ENABLED)) return -EPERM; if (pep->dir) { dir = ENDPRST_IO_TX; conf = &pdev->epx_regs->ep[pep->num - 1].txcon; } else { conf = &pdev->epx_regs->ep[pep->num - 1].rxcon; } if (!value) { struct cdns2_trb *trb = NULL; struct cdns2_request *preq; struct cdns2_trb trb_tmp; preq = cdns2_next_preq(&pep->pending_list); if (preq) { trb = preq->trb; if (trb) { trb_tmp = *trb; trb->control = trb->control ^ cpu_to_le32(TRB_CYCLE); } } trace_cdns2_ep_halt(pep, 0, 0); /* Resets Sequence Number */ writeb(dir | pep->num, &pdev->epx_regs->endprst); writeb(dir | ENDPRST_TOGRST | pep->num, &pdev->epx_regs->endprst); clear_reg_bit_8(conf, EPX_CON_STALL); pep->ep_state &= ~(EP_STALLED | EP_STALL_PENDING); if (preq) { if (trb) *trb = trb_tmp; cdns2_rearm_transfer(pep, 1); } cdns2_start_all_request(pdev, pep); } else { trace_cdns2_ep_halt(pep, 1, 0); set_reg_bit_8(conf, EPX_CON_STALL); writeb(dir | pep->num, &pdev->epx_regs->endprst); writeb(dir | ENDPRST_FIFORST | pep->num, &pdev->epx_regs->endprst); pep->ep_state |= EP_STALLED; } return 0; } /* Sets/clears stall on selected endpoint. */ static int cdns2_gadget_ep_set_halt(struct usb_ep *ep, int value) { struct cdns2_endpoint *pep = ep_to_cdns2_ep(ep); struct cdns2_device *pdev = pep->pdev; struct cdns2_request *preq; unsigned long flags = 0; int ret; spin_lock_irqsave(&pdev->lock, flags); preq = cdns2_next_preq(&pep->pending_list); if (value && preq) { trace_cdns2_ep_busy_try_halt_again(pep); ret = -EAGAIN; goto done; } if (!value) pep->ep_state &= ~EP_WEDGE; ret = cdns2_halt_endpoint(pdev, pep, value); done: spin_unlock_irqrestore(&pdev->lock, flags); return ret; } static int cdns2_gadget_ep_set_wedge(struct usb_ep *ep) { struct cdns2_endpoint *pep = ep_to_cdns2_ep(ep); cdns2_gadget_ep_set_halt(ep, 1); pep->ep_state |= EP_WEDGE; return 0; } static struct cdns2_endpoint *cdns2_find_available_ep(struct cdns2_device *pdev, struct usb_endpoint_descriptor *desc) { struct cdns2_endpoint *pep; struct usb_ep *ep; int ep_correct; list_for_each_entry(ep, &pdev->gadget.ep_list, ep_list) { unsigned long num; int ret; /* ep name pattern likes epXin or epXout. */ char c[2] = {ep->name[2], '\0'}; ret = kstrtoul(c, 10, &num); if (ret) return ERR_PTR(ret); pep = ep_to_cdns2_ep(ep); if (pep->num != num) continue; ep_correct = (pep->endpoint.caps.dir_in && usb_endpoint_dir_in(desc)) || (pep->endpoint.caps.dir_out && usb_endpoint_dir_out(desc)); if (ep_correct && !(pep->ep_state & EP_CLAIMED)) return pep; } return ERR_PTR(-ENOENT); } /* * Function used to recognize which endpoints will be used to optimize * on-chip memory usage. */ static struct usb_ep *cdns2_gadget_match_ep(struct usb_gadget *gadget, struct usb_endpoint_descriptor *desc, struct usb_ss_ep_comp_descriptor *comp_desc) { struct cdns2_device *pdev = gadget_to_cdns2_device(gadget); struct cdns2_endpoint *pep; unsigned long flags; pep = cdns2_find_available_ep(pdev, desc); if (IS_ERR(pep)) { dev_err(pdev->dev, "no available ep\n"); return NULL; } spin_lock_irqsave(&pdev->lock, flags); if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_ISOC) pep->buffering = 4; else pep->buffering = 1; pep->ep_state |= EP_CLAIMED; spin_unlock_irqrestore(&pdev->lock, flags); return &pep->endpoint; } static const struct usb_ep_ops cdns2_gadget_ep_ops = { .enable = cdns2_gadget_ep_enable, .disable = cdns2_gadget_ep_disable, .alloc_request = cdns2_gadget_ep_alloc_request, .free_request = cdns2_gadget_ep_free_request, .queue = cdns2_gadget_ep_queue, .dequeue = cdns2_gadget_ep_dequeue, .set_halt = cdns2_gadget_ep_set_halt, .set_wedge = cdns2_gadget_ep_set_wedge, }; static int cdns2_gadget_get_frame(struct usb_gadget *gadget) { struct cdns2_device *pdev = gadget_to_cdns2_device(gadget); return readw(&pdev->usb_regs->frmnr); } static int cdns2_gadget_wakeup(struct usb_gadget *gadget) { struct cdns2_device *pdev = gadget_to_cdns2_device(gadget); unsigned long flags; spin_lock_irqsave(&pdev->lock, flags); cdns2_wakeup(pdev); spin_unlock_irqrestore(&pdev->lock, flags); return 0; } static int cdns2_gadget_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered) { struct cdns2_device *pdev = gadget_to_cdns2_device(gadget); unsigned long flags; spin_lock_irqsave(&pdev->lock, flags); pdev->is_selfpowered = !!is_selfpowered; spin_unlock_irqrestore(&pdev->lock, flags); return 0; } /* Disable interrupts and begin the controller halting process. */ static void cdns2_quiesce(struct cdns2_device *pdev) { set_reg_bit_8(&pdev->usb_regs->usbcs, USBCS_DISCON); /* Disable interrupt. */ writeb(0, &pdev->interrupt_regs->extien), writeb(0, &pdev->interrupt_regs->usbien), writew(0, &pdev->adma_regs->ep_ien); /* Clear interrupt line. */ writeb(0x0, &pdev->interrupt_regs->usbirq); } static void cdns2_gadget_config(struct cdns2_device *pdev) { cdns2_ep0_config(pdev); /* Enable DMA interrupts for all endpoints. */ writel(~0x0, &pdev->adma_regs->ep_ien); cdns2_enable_l1(pdev, 0); writeb(USB_IEN_INIT, &pdev->interrupt_regs->usbien); writeb(EXTIRQ_WAKEUP, &pdev->interrupt_regs->extien); writel(DMA_CONF_DMULT, &pdev->adma_regs->conf); } static int cdns2_gadget_pullup(struct usb_gadget *gadget, int is_on) { struct cdns2_device *pdev = gadget_to_cdns2_device(gadget); unsigned long flags; trace_cdns2_pullup(is_on); /* * Disable events handling while controller is being * enabled/disabled. */ disable_irq(pdev->irq); spin_lock_irqsave(&pdev->lock, flags); if (is_on) { cdns2_gadget_config(pdev); clear_reg_bit_8(&pdev->usb_regs->usbcs, USBCS_DISCON); } else { cdns2_quiesce(pdev); } spin_unlock_irqrestore(&pdev->lock, flags); enable_irq(pdev->irq); return 0; } static int cdns2_gadget_udc_start(struct usb_gadget *gadget, struct usb_gadget_driver *driver) { struct cdns2_device *pdev = gadget_to_cdns2_device(gadget); enum usb_device_speed max_speed = driver->max_speed; unsigned long flags; spin_lock_irqsave(&pdev->lock, flags); pdev->gadget_driver = driver; /* Limit speed if necessary. */ max_speed = min(driver->max_speed, gadget->max_speed); switch (max_speed) { case USB_SPEED_FULL: writeb(SPEEDCTRL_HSDISABLE, &pdev->usb_regs->speedctrl); break; case USB_SPEED_HIGH: writeb(0, &pdev->usb_regs->speedctrl); break; default: dev_err(pdev->dev, "invalid maximum_speed parameter %d\n", max_speed); fallthrough; case USB_SPEED_UNKNOWN: /* Default to highspeed. */ max_speed = USB_SPEED_HIGH; break; } /* Reset all USB endpoints. */ writeb(ENDPRST_IO_TX, &pdev->usb_regs->endprst); writeb(ENDPRST_FIFORST | ENDPRST_TOGRST | ENDPRST_IO_TX, &pdev->usb_regs->endprst); writeb(ENDPRST_FIFORST | ENDPRST_TOGRST, &pdev->usb_regs->endprst); cdns2_eps_onchip_buffer_init(pdev); cdns2_gadget_config(pdev); spin_unlock_irqrestore(&pdev->lock, flags); return 0; } static int cdns2_gadget_udc_stop(struct usb_gadget *gadget) { struct cdns2_device *pdev = gadget_to_cdns2_device(gadget); struct cdns2_endpoint *pep; u32 bEndpointAddress; struct usb_ep *ep; int val; pdev->gadget_driver = NULL; pdev->gadget.speed = USB_SPEED_UNKNOWN; list_for_each_entry(ep, &pdev->gadget.ep_list, ep_list) { pep = ep_to_cdns2_ep(ep); bEndpointAddress = pep->num | pep->dir; cdns2_select_ep(pdev, bEndpointAddress); writel(DMA_EP_CMD_EPRST, &pdev->adma_regs->ep_cmd); readl_poll_timeout_atomic(&pdev->adma_regs->ep_cmd, val, !(val & DMA_EP_CMD_EPRST), 1, 100); } cdns2_quiesce(pdev); writeb(ENDPRST_IO_TX, &pdev->usb_regs->endprst); writeb(ENDPRST_FIFORST | ENDPRST_TOGRST | ENDPRST_IO_TX, &pdev->epx_regs->endprst); writeb(ENDPRST_FIFORST | ENDPRST_TOGRST, &pdev->epx_regs->endprst); return 0; } static const struct usb_gadget_ops cdns2_gadget_ops = { .get_frame = cdns2_gadget_get_frame, .wakeup = cdns2_gadget_wakeup, .set_selfpowered = cdns2_gadget_set_selfpowered, .pullup = cdns2_gadget_pullup, .udc_start = cdns2_gadget_udc_start, .udc_stop = cdns2_gadget_udc_stop, .match_ep = cdns2_gadget_match_ep, }; static void cdns2_free_all_eps(struct cdns2_device *pdev) { int i; for (i = 0; i < CDNS2_ENDPOINTS_NUM; i++) cdns2_free_tr_segment(&pdev->eps[i]); } /* Initializes software endpoints of gadget. */ static int cdns2_init_eps(struct cdns2_device *pdev) { struct cdns2_endpoint *pep; int i; for (i = 0; i < CDNS2_ENDPOINTS_NUM; i++) { bool direction = !(i & 1); /* Start from OUT endpoint. */ u8 epnum = ((i + 1) >> 1); /* * Endpoints are being held in pdev->eps[] in form: * ep0, ep1out, ep1in ... ep15out, ep15in. */ if (!CDNS2_IF_EP_EXIST(pdev, epnum, direction)) continue; pep = &pdev->eps[i]; pep->pdev = pdev; pep->num = epnum; /* 0 for OUT, 1 for IN. */ pep->dir = direction ? USB_DIR_IN : USB_DIR_OUT; pep->idx = i; /* Ep0in and ep0out are represented by pdev->eps[0]. */ if (!epnum) { int ret; snprintf(pep->name, sizeof(pep->name), "ep%d%s", epnum, "BiDir"); cdns2_init_ep0(pdev, pep); ret = cdns2_alloc_tr_segment(pep); if (ret) { dev_err(pdev->dev, "Failed to init ep0\n"); return ret; } } else { snprintf(pep->name, sizeof(pep->name), "ep%d%s", epnum, !!direction ? "in" : "out"); pep->endpoint.name = pep->name; usb_ep_set_maxpacket_limit(&pep->endpoint, 1024); pep->endpoint.ops = &cdns2_gadget_ep_ops; list_add_tail(&pep->endpoint.ep_list, &pdev->gadget.ep_list); pep->endpoint.caps.dir_in = direction; pep->endpoint.caps.dir_out = !direction; pep->endpoint.caps.type_iso = 1; pep->endpoint.caps.type_bulk = 1; pep->endpoint.caps.type_int = 1; } pep->endpoint.name = pep->name; pep->ep_state = 0; dev_dbg(pdev->dev, "Init %s, SupType: CTRL: %s, INT: %s, " "BULK: %s, ISOC %s, SupDir IN: %s, OUT: %s\n", pep->name, (pep->endpoint.caps.type_control) ? "yes" : "no", (pep->endpoint.caps.type_int) ? "yes" : "no", (pep->endpoint.caps.type_bulk) ? "yes" : "no", (pep->endpoint.caps.type_iso) ? "yes" : "no", (pep->endpoint.caps.dir_in) ? "yes" : "no", (pep->endpoint.caps.dir_out) ? "yes" : "no"); INIT_LIST_HEAD(&pep->pending_list); INIT_LIST_HEAD(&pep->deferred_list); } return 0; } static int cdns2_gadget_start(struct cdns2_device *pdev) { u32 max_speed; void *buf; int val; int ret; pdev->usb_regs = pdev->regs; pdev->ep0_regs = pdev->regs; pdev->epx_regs = pdev->regs; pdev->interrupt_regs = pdev->regs; pdev->adma_regs = pdev->regs + CDNS2_ADMA_REGS_OFFSET; /* Reset controller. */ set_reg_bit_8(&pdev->usb_regs->cpuctrl, CPUCTRL_SW_RST); ret = readl_poll_timeout_atomic(&pdev->usb_regs->cpuctrl, val, !(val & CPUCTRL_SW_RST), 1, 10000); if (ret) { dev_err(pdev->dev, "Error: reset controller timeout\n"); return -EINVAL; } usb_initialize_gadget(pdev->dev, &pdev->gadget, NULL); device_property_read_u16(pdev->dev, "cdns,on-chip-tx-buff-size", &pdev->onchip_tx_buf); device_property_read_u16(pdev->dev, "cdns,on-chip-rx-buff-size", &pdev->onchip_rx_buf); device_property_read_u32(pdev->dev, "cdns,avail-endpoints", &pdev->eps_supported); /* * Driver assumes that each USBHS controller has at least * one IN and one OUT non control endpoint. */ if (!pdev->onchip_tx_buf && !pdev->onchip_rx_buf) { ret = -EINVAL; dev_err(pdev->dev, "Invalid on-chip memory configuration\n"); goto put_gadget; } if (!(pdev->eps_supported & ~0x00010001)) { ret = -EINVAL; dev_err(pdev->dev, "No hardware endpoints available\n"); goto put_gadget; } max_speed = usb_get_maximum_speed(pdev->dev); switch (max_speed) { case USB_SPEED_FULL: case USB_SPEED_HIGH: break; default: dev_err(pdev->dev, "invalid maximum_speed parameter %d\n", max_speed); fallthrough; case USB_SPEED_UNKNOWN: max_speed = USB_SPEED_HIGH; break; } pdev->gadget.max_speed = max_speed; pdev->gadget.speed = USB_SPEED_UNKNOWN; pdev->gadget.ops = &cdns2_gadget_ops; pdev->gadget.name = "usbhs-gadget"; pdev->gadget.quirk_avoids_skb_reserve = 1; pdev->gadget.irq = pdev->irq; spin_lock_init(&pdev->lock); INIT_WORK(&pdev->pending_status_wq, cdns2_pending_setup_status_handler); /* Initialize endpoint container. */ INIT_LIST_HEAD(&pdev->gadget.ep_list); pdev->eps_dma_pool = dma_pool_create("cdns2_eps_dma_pool", pdev->dev, TR_SEG_SIZE, 8, 0); if (!pdev->eps_dma_pool) { dev_err(pdev->dev, "Failed to create TRB dma pool\n"); ret = -ENOMEM; goto put_gadget; } ret = cdns2_init_eps(pdev); if (ret) { dev_err(pdev->dev, "Failed to create endpoints\n"); goto destroy_dma_pool; } pdev->gadget.sg_supported = 1; pdev->zlp_buf = kzalloc(CDNS2_EP_ZLP_BUF_SIZE, GFP_KERNEL); if (!pdev->zlp_buf) { ret = -ENOMEM; goto destroy_dma_pool; } /* Allocate memory for setup packet buffer. */ buf = dma_alloc_coherent(pdev->dev, 8, &pdev->ep0_preq.request.dma, GFP_DMA); pdev->ep0_preq.request.buf = buf; if (!pdev->ep0_preq.request.buf) { ret = -ENOMEM; goto free_zlp_buf; } /* Add USB gadget device. */ ret = usb_add_gadget(&pdev->gadget); if (ret < 0) { dev_err(pdev->dev, "Failed to add gadget\n"); goto free_ep0_buf; } return 0; free_ep0_buf: dma_free_coherent(pdev->dev, 8, pdev->ep0_preq.request.buf, pdev->ep0_preq.request.dma); free_zlp_buf: kfree(pdev->zlp_buf); destroy_dma_pool: dma_pool_destroy(pdev->eps_dma_pool); put_gadget: usb_put_gadget(&pdev->gadget); return ret; } int cdns2_gadget_suspend(struct cdns2_device *pdev) { unsigned long flags; cdns2_disconnect_gadget(pdev); spin_lock_irqsave(&pdev->lock, flags); pdev->gadget.speed = USB_SPEED_UNKNOWN; trace_cdns2_device_state("notattached"); usb_gadget_set_state(&pdev->gadget, USB_STATE_NOTATTACHED); cdns2_enable_l1(pdev, 0); /* Disable interrupt for device. */ writeb(0, &pdev->interrupt_regs->usbien); writel(0, &pdev->adma_regs->ep_ien); spin_unlock_irqrestore(&pdev->lock, flags); return 0; } int cdns2_gadget_resume(struct cdns2_device *pdev, bool hibernated) { unsigned long flags; spin_lock_irqsave(&pdev->lock, flags); if (!pdev->gadget_driver) { spin_unlock_irqrestore(&pdev->lock, flags); return 0; } cdns2_gadget_config(pdev); if (hibernated) clear_reg_bit_8(&pdev->usb_regs->usbcs, USBCS_DISCON); spin_unlock_irqrestore(&pdev->lock, flags); return 0; } void cdns2_gadget_remove(struct cdns2_device *pdev) { pm_runtime_mark_last_busy(pdev->dev); pm_runtime_put_autosuspend(pdev->dev); usb_del_gadget(&pdev->gadget); cdns2_free_all_eps(pdev); dma_pool_destroy(pdev->eps_dma_pool); kfree(pdev->zlp_buf); usb_put_gadget(&pdev->gadget); } int cdns2_gadget_init(struct cdns2_device *pdev) { int ret; /* Ensure 32-bit DMA Mask. */ ret = dma_set_mask_and_coherent(pdev->dev, DMA_BIT_MASK(32)); if (ret) { dev_err(pdev->dev, "Failed to set dma mask: %d\n", ret); return ret; } pm_runtime_get_sync(pdev->dev); cdsn2_isoc_burst_opt(pdev); ret = cdns2_gadget_start(pdev); if (ret) { pm_runtime_put_sync(pdev->dev); return ret; } /* * Because interrupt line can be shared with other components in * driver it can't use IRQF_ONESHOT flag here. */ ret = devm_request_threaded_irq(pdev->dev, pdev->irq, cdns2_usb_irq_handler, cdns2_thread_irq_handler, IRQF_SHARED, dev_name(pdev->dev), pdev); if (ret) goto err0; return 0; err0: cdns2_gadget_remove(pdev); return ret; } |