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
|
/*
* Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/malloc.h>
#include <ipxe/pci.h>
#include <ipxe/usb.h>
#include <ipxe/init.h>
#include "ehci.h"
/** @file
*
* USB Enhanced Host Controller Interface (EHCI) driver
*
*/
/**
* Construct error code from transfer descriptor status
*
* @v status Transfer descriptor status
* @ret rc Error code
*
* Bits 2-5 of the status code provide some indication as to the root
* cause of the error. We incorporate these into the error code as
* reported to usb_complete_err().
*/
#define EIO_STATUS( status ) EUNIQ ( EINFO_EIO, ( ( (status) >> 2 ) & 0xf ) )
/******************************************************************************
*
* Register access
*
******************************************************************************
*/
/**
* Initialise device
*
* @v ehci EHCI device
* @v regs MMIO registers
*/
static void ehci_init ( struct ehci_device *ehci, void *regs ) {
uint32_t hcsparams;
uint32_t hccparams;
size_t caplength;
/* Locate capability and operational registers */
ehci->cap = regs;
caplength = readb ( ehci->cap + EHCI_CAP_CAPLENGTH );
ehci->op = ( ehci->cap + caplength );
DBGC2 ( ehci, "EHCI %s cap %08lx op %08lx\n", ehci->name,
virt_to_phys ( ehci->cap ), virt_to_phys ( ehci->op ) );
/* Read structural parameters */
hcsparams = readl ( ehci->cap + EHCI_CAP_HCSPARAMS );
ehci->ports = EHCI_HCSPARAMS_PORTS ( hcsparams );
DBGC ( ehci, "EHCI %s has %d ports\n", ehci->name, ehci->ports );
/* Read capability parameters 1 */
hccparams = readl ( ehci->cap + EHCI_CAP_HCCPARAMS );
ehci->addr64 = EHCI_HCCPARAMS_ADDR64 ( hccparams );
ehci->flsize = ( EHCI_HCCPARAMS_FLSIZE ( hccparams ) ?
EHCI_FLSIZE_SMALL : EHCI_FLSIZE_DEFAULT );
ehci->eecp = EHCI_HCCPARAMS_EECP ( hccparams );
DBGC2 ( ehci, "EHCI %s %d-bit flsize %d\n", ehci->name,
( ehci->addr64 ? 64 : 32 ), ehci->flsize );
}
/**
* Find extended capability
*
* @v ehci EHCI device
* @v pci PCI device
* @v id Capability ID
* @v offset Offset to previous extended capability instance, or zero
* @ret offset Offset to extended capability, or zero if not found
*/
static unsigned int ehci_extended_capability ( struct ehci_device *ehci,
struct pci_device *pci,
unsigned int id,
unsigned int offset ) {
uint32_t eecp;
/* Locate the extended capability */
while ( 1 ) {
/* Locate first or next capability as applicable */
if ( offset ) {
pci_read_config_dword ( pci, offset, &eecp );
offset = EHCI_EECP_NEXT ( eecp );
} else {
offset = ehci->eecp;
}
if ( ! offset )
return 0;
/* Check if this is the requested capability */
pci_read_config_dword ( pci, offset, &eecp );
if ( EHCI_EECP_ID ( eecp ) == id )
return offset;
}
}
/**
* Calculate buffer alignment
*
* @v len Length
* @ret align Buffer alignment
*
* Determine alignment required for a buffer which must be aligned to
* at least EHCI_MIN_ALIGN and which must not cross a page boundary.
*/
static inline size_t ehci_align ( size_t len ) {
size_t align;
/* Align to own length (rounded up to a power of two) */
align = ( 1 << fls ( len - 1 ) );
/* Round up to EHCI_MIN_ALIGN if needed */
if ( align < EHCI_MIN_ALIGN )
align = EHCI_MIN_ALIGN;
return align;
}
/**
* Check control data structure reachability
*
* @v ehci EHCI device
* @v ptr Data structure pointer
* @ret rc Return status code
*/
static int ehci_ctrl_reachable ( struct ehci_device *ehci, void *ptr ) {
physaddr_t phys = virt_to_phys ( ptr );
uint32_t segment;
/* Always reachable in a 32-bit build */
if ( sizeof ( physaddr_t ) <= sizeof ( uint32_t ) )
return 0;
/* Reachable only if control segment matches in a 64-bit build */
segment = ( ( ( uint64_t ) phys ) >> 32 );
if ( segment == ehci->ctrldssegment )
return 0;
return -ENOTSUP;
}
/******************************************************************************
*
* USB legacy support
*
******************************************************************************
*/
/** Prevent the release of ownership back to BIOS */
static int ehci_legacy_prevent_release;
/**
* Initialise USB legacy support
*
* @v ehci EHCI device
* @v pci PCI device
*/
static void ehci_legacy_init ( struct ehci_device *ehci,
struct pci_device *pci ) {
unsigned int legacy;
uint8_t bios;
/* Locate USB legacy support capability (if present) */
legacy = ehci_extended_capability ( ehci, pci, EHCI_EECP_ID_LEGACY, 0 );
if ( ! legacy ) {
/* Not an error; capability may not be present */
DBGC ( ehci, "EHCI %s has no USB legacy support capability\n",
ehci->name );
return;
}
/* Check if legacy USB support is enabled */
pci_read_config_byte ( pci, ( legacy + EHCI_USBLEGSUP_BIOS ), &bios );
if ( ! ( bios & EHCI_USBLEGSUP_BIOS_OWNED ) ) {
/* Not an error; already owned by OS */
DBGC ( ehci, "EHCI %s USB legacy support already disabled\n",
ehci->name );
return;
}
/* Record presence of USB legacy support capability */
ehci->legacy = legacy;
}
/**
* Claim ownership from BIOS
*
* @v ehci EHCI device
* @v pci PCI device
*/
static void ehci_legacy_claim ( struct ehci_device *ehci,
struct pci_device *pci ) {
unsigned int legacy = ehci->legacy;
uint32_t ctlsts;
uint8_t bios;
unsigned int i;
/* Do nothing unless legacy support capability is present */
if ( ! legacy )
return;
/* Claim ownership */
pci_write_config_byte ( pci, ( legacy + EHCI_USBLEGSUP_OS ),
EHCI_USBLEGSUP_OS_OWNED );
/* Wait for BIOS to release ownership */
for ( i = 0 ; i < EHCI_USBLEGSUP_MAX_WAIT_MS ; i++ ) {
/* Check if BIOS has released ownership */
pci_read_config_byte ( pci, ( legacy + EHCI_USBLEGSUP_BIOS ),
&bios );
if ( ! ( bios & EHCI_USBLEGSUP_BIOS_OWNED ) ) {
DBGC ( ehci, "EHCI %s claimed ownership from BIOS\n",
ehci->name );
pci_read_config_dword ( pci, ( legacy +
EHCI_USBLEGSUP_CTLSTS ),
&ctlsts );
if ( ctlsts ) {
DBGC ( ehci, "EHCI %s warning: BIOS retained "
"SMIs: %08x\n", ehci->name, ctlsts );
}
return;
}
/* Delay */
mdelay ( 1 );
}
/* BIOS did not release ownership. Claim it forcibly by
* disabling all SMIs.
*/
DBGC ( ehci, "EHCI %s could not claim ownership from BIOS: forcibly "
"disabling SMIs\n", ehci->name );
pci_write_config_dword ( pci, ( legacy + EHCI_USBLEGSUP_CTLSTS ), 0 );
}
/**
* Release ownership back to BIOS
*
* @v ehci EHCI device
* @v pci PCI device
*/
static void ehci_legacy_release ( struct ehci_device *ehci,
struct pci_device *pci ) {
/* Do nothing unless legacy support capability is present */
if ( ! ehci->legacy )
return;
/* Do nothing if releasing ownership is prevented */
if ( ehci_legacy_prevent_release ) {
DBGC ( ehci, "EHCI %s not releasing ownership to BIOS\n",
ehci->name );
return;
}
/* Release ownership */
pci_write_config_byte ( pci, ( ehci->legacy + EHCI_USBLEGSUP_OS ), 0 );
DBGC ( ehci, "EHCI %s released ownership to BIOS\n", ehci->name );
}
/******************************************************************************
*
* Companion controllers
*
******************************************************************************
*/
/**
* Poll child companion controllers
*
* @v ehci EHCI device
*/
static void ehci_poll_companions ( struct ehci_device *ehci ) {
struct usb_bus *bus;
struct device_description *desc;
/* Poll any USB buses belonging to child companion controllers */
for_each_usb_bus ( bus ) {
/* Get underlying devices description */
desc = &bus->dev->desc;
/* Skip buses that are not PCI devices */
if ( desc->bus_type != BUS_TYPE_PCI )
continue;
/* Skip buses that are not part of the same PCI device */
if ( PCI_FIRST_FUNC ( desc->location ) !=
PCI_FIRST_FUNC ( ehci->bus->dev->desc.location ) )
continue;
/* Skip buses that are not UHCI or OHCI PCI devices */
if ( ( desc->class != PCI_CLASS ( PCI_CLASS_SERIAL,
PCI_CLASS_SERIAL_USB,
PCI_CLASS_SERIAL_USB_UHCI ))&&
( desc->class != PCI_CLASS ( PCI_CLASS_SERIAL,
PCI_CLASS_SERIAL_USB,
PCI_CLASS_SERIAL_USB_OHCI ) ))
continue;
/* Poll child companion controller bus */
DBGC2 ( ehci, "EHCI %s polling companion %s\n",
ehci->name, bus->name );
usb_poll ( bus );
}
}
/**
* Locate EHCI companion controller
*
* @v pci PCI device
* @ret busdevfn EHCI companion controller bus:dev.fn (if any)
*/
unsigned int ehci_companion ( struct pci_device *pci ) {
struct pci_device tmp;
unsigned int busdevfn;
int rc;
/* Look for an EHCI function on the same PCI device */
busdevfn = pci->busdevfn;
while ( ++busdevfn <= PCI_LAST_FUNC ( pci->busdevfn ) ) {
pci_init ( &tmp, busdevfn );
if ( ( rc = pci_read_config ( &tmp ) ) != 0 )
continue;
if ( tmp.class == PCI_CLASS ( PCI_CLASS_SERIAL,
PCI_CLASS_SERIAL_USB,
PCI_CLASS_SERIAL_USB_EHCI ) )
return busdevfn;
}
return 0;
}
/******************************************************************************
*
* Run / stop / reset
*
******************************************************************************
*/
/**
* Start EHCI device
*
* @v ehci EHCI device
*/
static void ehci_run ( struct ehci_device *ehci ) {
uint32_t usbcmd;
/* Set run/stop bit */
usbcmd = readl ( ehci->op + EHCI_OP_USBCMD );
usbcmd &= ~EHCI_USBCMD_FLSIZE_MASK;
usbcmd |= ( EHCI_USBCMD_RUN | EHCI_USBCMD_FLSIZE ( ehci->flsize ) |
EHCI_USBCMD_PERIODIC | EHCI_USBCMD_ASYNC );
writel ( usbcmd, ehci->op + EHCI_OP_USBCMD );
}
/**
* Stop EHCI device
*
* @v ehci EHCI device
* @ret rc Return status code
*/
static int ehci_stop ( struct ehci_device *ehci ) {
uint32_t usbcmd;
uint32_t usbsts;
unsigned int i;
/* Clear run/stop bit */
usbcmd = readl ( ehci->op + EHCI_OP_USBCMD );
usbcmd &= ~( EHCI_USBCMD_RUN | EHCI_USBCMD_PERIODIC |
EHCI_USBCMD_ASYNC );
writel ( usbcmd, ehci->op + EHCI_OP_USBCMD );
/* Wait for device to stop */
for ( i = 0 ; i < EHCI_STOP_MAX_WAIT_MS ; i++ ) {
/* Check if device is stopped */
usbsts = readl ( ehci->op + EHCI_OP_USBSTS );
if ( usbsts & EHCI_USBSTS_HCH )
return 0;
/* Delay */
mdelay ( 1 );
}
DBGC ( ehci, "EHCI %s timed out waiting for stop\n", ehci->name );
return -ETIMEDOUT;
}
/**
* Reset EHCI device
*
* @v ehci EHCI device
* @ret rc Return status code
*/
static int ehci_reset ( struct ehci_device *ehci ) {
uint32_t usbcmd;
unsigned int i;
int rc;
/* The EHCI specification states that resetting a running
* device may result in undefined behaviour, so try stopping
* it first.
*/
if ( ( rc = ehci_stop ( ehci ) ) != 0 ) {
/* Ignore errors and attempt to reset the device anyway */
}
/* Reset device */
writel ( EHCI_USBCMD_HCRST, ehci->op + EHCI_OP_USBCMD );
/* Wait for reset to complete */
for ( i = 0 ; i < EHCI_RESET_MAX_WAIT_MS ; i++ ) {
/* Check if reset is complete */
usbcmd = readl ( ehci->op + EHCI_OP_USBCMD );
if ( ! ( usbcmd & EHCI_USBCMD_HCRST ) )
return 0;
/* Delay */
mdelay ( 1 );
}
DBGC ( ehci, "EHCI %s timed out waiting for reset\n", ehci->name );
return -ETIMEDOUT;
}
/******************************************************************************
*
* Transfer descriptor rings
*
******************************************************************************
*/
/**
* Allocate transfer descriptor ring
*
* @v ehci EHCI device
* @v ring Transfer descriptor ring
* @ret rc Return status code
*/
static int ehci_ring_alloc ( struct ehci_device *ehci,
struct ehci_ring *ring ) {
struct ehci_transfer_descriptor *desc;
struct ehci_transfer_descriptor *next;
unsigned int i;
size_t len;
uint32_t link;
int rc;
/* Initialise structure */
memset ( ring, 0, sizeof ( *ring ) );
/* Allocate I/O buffers */
ring->iobuf = zalloc ( EHCI_RING_COUNT * sizeof ( ring->iobuf[0] ) );
if ( ! ring->iobuf ) {
rc = -ENOMEM;
goto err_alloc_iobuf;
}
/* Allocate queue head */
ring->head = malloc_dma ( sizeof ( *ring->head ),
ehci_align ( sizeof ( *ring->head ) ) );
if ( ! ring->head ) {
rc = -ENOMEM;
goto err_alloc_queue;
}
if ( ( rc = ehci_ctrl_reachable ( ehci, ring->head ) ) != 0 ) {
DBGC ( ehci, "EHCI %s queue head unreachable\n", ehci->name );
goto err_unreachable_queue;
}
memset ( ring->head, 0, sizeof ( *ring->head ) );
/* Allocate transfer descriptors */
len = ( EHCI_RING_COUNT * sizeof ( ring->desc[0] ) );
ring->desc = malloc_dma ( len, sizeof ( ring->desc[0] ) );
if ( ! ring->desc ) {
rc = -ENOMEM;
goto err_alloc_desc;
}
memset ( ring->desc, 0, len );
/* Initialise transfer descriptors */
for ( i = 0 ; i < EHCI_RING_COUNT ; i++ ) {
desc = &ring->desc[i];
if ( ( rc = ehci_ctrl_reachable ( ehci, desc ) ) != 0 ) {
DBGC ( ehci, "EHCI %s descriptor unreachable\n",
ehci->name );
goto err_unreachable_desc;
}
next = &ring->desc[ ( i + 1 ) % EHCI_RING_COUNT ];
link = virt_to_phys ( next );
desc->next = cpu_to_le32 ( link );
desc->alt = cpu_to_le32 ( link );
}
/* Initialise queue head */
link = virt_to_phys ( &ring->desc[0] );
ring->head->cache.next = cpu_to_le32 ( link );
return 0;
err_unreachable_desc:
free_dma ( ring->desc, len );
err_alloc_desc:
err_unreachable_queue:
free_dma ( ring->head, sizeof ( *ring->head ) );
err_alloc_queue:
free ( ring->iobuf );
err_alloc_iobuf:
return rc;
}
/**
* Free transfer descriptor ring
*
* @v ring Transfer descriptor ring
*/
static void ehci_ring_free ( struct ehci_ring *ring ) {
unsigned int i;
/* Sanity checks */
assert ( ehci_ring_fill ( ring ) == 0 );
for ( i = 0 ; i < EHCI_RING_COUNT ; i++ )
assert ( ring->iobuf[i] == NULL );
/* Free transfer descriptors */
free_dma ( ring->desc, ( EHCI_RING_COUNT * sizeof ( ring->desc[0] ) ) );
/* Free queue head */
free_dma ( ring->head, sizeof ( *ring->head ) );
/* Free I/O buffers */
free ( ring->iobuf );
}
/**
* Enqueue transfer descriptors
*
* @v ehci EHCI device
* @v ring Transfer descriptor ring
* @v iobuf I/O buffer
* @v xfers Transfers
* @v count Number of transfers
* @ret rc Return status code
*/
static int ehci_enqueue ( struct ehci_device *ehci, struct ehci_ring *ring,
struct io_buffer *iobuf,
const struct ehci_transfer *xfer,
unsigned int count ) {
struct ehci_transfer_descriptor *desc;
physaddr_t phys;
void *data;
size_t len;
size_t offset;
size_t frag_len;
unsigned int toggle;
unsigned int index;
unsigned int i;
/* Sanity check */
assert ( iobuf != NULL );
assert ( count > 0 );
/* Fail if ring does not have sufficient space */
if ( ehci_ring_remaining ( ring ) < count )
return -ENOBUFS;
/* Fail if any portion is unreachable */
for ( i = 0 ; i < count ; i++ ) {
phys = ( virt_to_phys ( xfer[i].data ) + xfer[i].len - 1 );
if ( ( phys > 0xffffffffUL ) && ( ! ehci->addr64 ) )
return -ENOTSUP;
}
/* Enqueue each transfer, recording the I/O buffer with the last */
for ( ; count ; ring->prod++, xfer++ ) {
/* Populate descriptor header */
index = ( ring->prod % EHCI_RING_COUNT );
desc = &ring->desc[index];
toggle = ( xfer->flags & EHCI_FL_TOGGLE );
assert ( xfer->len <= EHCI_LEN_MASK );
assert ( EHCI_FL_TOGGLE == EHCI_LEN_TOGGLE );
desc->len = cpu_to_le16 ( xfer->len | toggle );
desc->flags = ( xfer->flags | EHCI_FL_CERR_MAX );
/* Populate buffer pointers */
data = xfer->data;
len = xfer->len;
for ( i = 0 ; len ; i++ ) {
/* Calculate length of this fragment */
phys = virt_to_phys ( data );
offset = ( phys & ( EHCI_PAGE_ALIGN - 1 ) );
frag_len = ( EHCI_PAGE_ALIGN - offset );
if ( frag_len > len )
frag_len = len;
/* Sanity checks */
assert ( ( i == 0 ) || ( offset == 0 ) );
assert ( i < ( sizeof ( desc->low ) /
sizeof ( desc->low[0] ) ) );
/* Populate buffer pointer */
desc->low[i] = cpu_to_le32 ( phys );
if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) ) {
desc->high[i] =
cpu_to_le32 ( ((uint64_t) phys) >> 32 );
}
/* Move to next fragment */
data += frag_len;
len -= frag_len;
}
/* Ensure everything is valid before activating descriptor */
wmb();
desc->status = EHCI_STATUS_ACTIVE;
/* Record I/O buffer against last ring index */
if ( --count == 0 )
ring->iobuf[index] = iobuf;
}
return 0;
}
/**
* Dequeue a transfer descriptor
*
* @v ring Transfer descriptor ring
* @ret iobuf I/O buffer (or NULL)
*/
static struct io_buffer * ehci_dequeue ( struct ehci_ring *ring ) {
struct ehci_transfer_descriptor *desc;
struct io_buffer *iobuf;
unsigned int index = ( ring->cons % EHCI_RING_COUNT );
/* Sanity check */
assert ( ehci_ring_fill ( ring ) > 0 );
/* Mark descriptor as inactive (and not halted) */
desc = &ring->desc[index];
desc->status = 0;
/* Retrieve I/O buffer */
iobuf = ring->iobuf[index];
ring->iobuf[index] = NULL;
/* Update consumer counter */
ring->cons++;
return iobuf;
}
/******************************************************************************
*
* Schedule management
*
******************************************************************************
*/
/**
* Get link value for a queue head
*
* @v queue Queue head
* @ret link Link value
*/
static inline uint32_t ehci_link_qh ( struct ehci_queue_head *queue ) {
return ( virt_to_phys ( queue ) | EHCI_LINK_TYPE_QH );
}
/**
* (Re)build asynchronous schedule
*
* @v ehci EHCI device
*/
static void ehci_async_schedule ( struct ehci_device *ehci ) {
struct ehci_endpoint *endpoint;
struct ehci_queue_head *queue;
uint32_t link;
/* Build schedule in reverse order of execution. Provided
* that we only ever add or remove single endpoints, this can
* safely run concurrently with hardware execution of the
* schedule.
*/
link = ehci_link_qh ( ehci->head );
list_for_each_entry_reverse ( endpoint, &ehci->async, schedule ) {
queue = endpoint->ring.head;
queue->link = cpu_to_le32 ( link );
wmb();
link = ehci_link_qh ( queue );
}
ehci->head->link = cpu_to_le32 ( link );
wmb();
}
/**
* Add endpoint to asynchronous schedule
*
* @v endpoint Endpoint
*/
static void ehci_async_add ( struct ehci_endpoint *endpoint ) {
struct ehci_device *ehci = endpoint->ehci;
/* Add to end of schedule */
list_add_tail ( &endpoint->schedule, &ehci->async );
/* Rebuild schedule */
ehci_async_schedule ( ehci );
}
/**
* Remove endpoint from asynchronous schedule
*
* @v endpoint Endpoint
* @ret rc Return status code
*/
static int ehci_async_del ( struct ehci_endpoint *endpoint ) {
struct ehci_device *ehci = endpoint->ehci;
uint32_t usbcmd;
uint32_t usbsts;
unsigned int i;
/* Remove from schedule */
list_check_contains_entry ( endpoint, &ehci->async, schedule );
list_del ( &endpoint->schedule );
/* Rebuild schedule */
ehci_async_schedule ( ehci );
/* Request notification when asynchronous schedule advances */
usbcmd = readl ( ehci->op + EHCI_OP_USBCMD );
usbcmd |= EHCI_USBCMD_ASYNC_ADVANCE;
writel ( usbcmd, ehci->op + EHCI_OP_USBCMD );
/* Wait for asynchronous schedule to advance */
for ( i = 0 ; i < EHCI_ASYNC_ADVANCE_MAX_WAIT_MS ; i++ ) {
/* Check for asynchronous schedule advancing */
usbsts = readl ( ehci->op + EHCI_OP_USBSTS );
if ( usbsts & EHCI_USBSTS_ASYNC_ADVANCE ) {
usbsts &= ~EHCI_USBSTS_CHANGE;
usbsts |= EHCI_USBSTS_ASYNC_ADVANCE;
writel ( usbsts, ehci->op + EHCI_OP_USBSTS );
return 0;
}
/* Delay */
mdelay ( 1 );
}
/* Bad things will probably happen now */
DBGC ( ehci, "EHCI %s timed out waiting for asynchronous schedule "
"to advance\n", ehci->name );
return -ETIMEDOUT;
}
/**
* (Re)build periodic schedule
*
* @v ehci EHCI device
*/
static void ehci_periodic_schedule ( struct ehci_device *ehci ) {
struct ehci_endpoint *endpoint;
struct ehci_queue_head *queue;
uint32_t link;
unsigned int frames;
unsigned int max_interval;
unsigned int i;
/* Build schedule in reverse order of execution. Provided
* that we only ever add or remove single endpoints, this can
* safely run concurrently with hardware execution of the
* schedule.
*/
DBGCP ( ehci, "EHCI %s periodic schedule: ", ehci->name );
link = EHCI_LINK_TERMINATE;
list_for_each_entry_reverse ( endpoint, &ehci->periodic, schedule ) {
queue = endpoint->ring.head;
queue->link = cpu_to_le32 ( link );
wmb();
DBGCP ( ehci, "%s%d",
( ( link == EHCI_LINK_TERMINATE ) ? "" : "<-" ),
endpoint->ep->interval );
link = ehci_link_qh ( queue );
}
DBGCP ( ehci, "\n" );
/* Populate periodic frame list */
DBGCP ( ehci, "EHCI %s periodic frame list:", ehci->name );
frames = EHCI_PERIODIC_FRAMES ( ehci->flsize );
for ( i = 0 ; i < frames ; i++ ) {
/* Calculate maximum interval (in microframes) which
* may appear as part of this frame list.
*/
if ( i == 0 ) {
/* Start of list: include all endpoints */
max_interval = -1U;
} else {
/* Calculate highest power-of-two frame interval */
max_interval = ( 1 << ( ffs ( i ) - 1 ) );
/* Convert to microframes */
max_interval <<= 3;
/* Round up to nearest 2^n-1 */
max_interval = ( ( max_interval << 1 ) - 1 );
}
/* Find first endpoint in schedule satisfying this
* maximum interval constraint.
*/
link = EHCI_LINK_TERMINATE;
list_for_each_entry ( endpoint, &ehci->periodic, schedule ) {
if ( endpoint->ep->interval <= max_interval ) {
queue = endpoint->ring.head;
link = ehci_link_qh ( queue );
DBGCP ( ehci, " %d:%d",
i, endpoint->ep->interval );
break;
}
}
ehci->frame[i].link = cpu_to_le32 ( link );
}
wmb();
DBGCP ( ehci, "\n" );
}
/**
* Add endpoint to periodic schedule
*
* @v endpoint Endpoint
*/
static void ehci_periodic_add ( struct ehci_endpoint *endpoint ) {
struct ehci_device *ehci = endpoint->ehci;
struct ehci_endpoint *before;
unsigned int interval = endpoint->ep->interval;
/* Find first endpoint with a smaller interval */
list_for_each_entry ( before, &ehci->periodic, schedule ) {
if ( before->ep->interval < interval )
break;
}
list_add_tail ( &endpoint->schedule, &before->schedule );
/* Rebuild schedule */
ehci_periodic_schedule ( ehci );
}
/**
* Remove endpoint from periodic schedule
*
* @v endpoint Endpoint
* @ret rc Return status code
*/
static int ehci_periodic_del ( struct ehci_endpoint *endpoint ) {
struct ehci_device *ehci = endpoint->ehci;
/* Remove from schedule */
list_check_contains_entry ( endpoint, &ehci->periodic, schedule );
list_del ( &endpoint->schedule );
/* Rebuild schedule */
ehci_periodic_schedule ( ehci );
/* Delay for a whole USB frame (with a 100% safety margin) */
mdelay ( 2 );
return 0;
}
/**
* Add endpoint to appropriate schedule
*
* @v endpoint Endpoint
*/
static void ehci_schedule_add ( struct ehci_endpoint *endpoint ) {
struct usb_endpoint *ep = endpoint->ep;
unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
if ( attr == USB_ENDPOINT_ATTR_INTERRUPT ) {
ehci_periodic_add ( endpoint );
} else {
ehci_async_add ( endpoint );
}
}
/**
* Remove endpoint from appropriate schedule
*
* @v endpoint Endpoint
* @ret rc Return status code
*/
static int ehci_schedule_del ( struct ehci_endpoint *endpoint ) {
struct usb_endpoint *ep = endpoint->ep;
unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
if ( attr == USB_ENDPOINT_ATTR_INTERRUPT ) {
return ehci_periodic_del ( endpoint );
} else {
return ehci_async_del ( endpoint );
}
}
/******************************************************************************
*
* Endpoint operations
*
******************************************************************************
*/
/**
* Determine endpoint characteristics
*
* @v ep USB endpoint
* @ret chr Endpoint characteristics
*/
static uint32_t ehci_endpoint_characteristics ( struct usb_endpoint *ep ) {
struct usb_device *usb = ep->usb;
unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
uint32_t chr;
/* Determine basic characteristics */
chr = ( EHCI_CHR_ADDRESS ( usb->address ) |
EHCI_CHR_ENDPOINT ( ep->address ) |
EHCI_CHR_MAX_LEN ( ep->mtu ) );
/* Control endpoints require manual control of the data toggle */
if ( attr == USB_ENDPOINT_ATTR_CONTROL )
chr |= EHCI_CHR_TOGGLE;
/* Determine endpoint speed */
if ( usb->port->speed == USB_SPEED_HIGH ) {
chr |= EHCI_CHR_EPS_HIGH;
} else {
if ( usb->port->speed == USB_SPEED_FULL ) {
chr |= EHCI_CHR_EPS_FULL;
} else {
chr |= EHCI_CHR_EPS_LOW;
}
if ( attr == USB_ENDPOINT_ATTR_CONTROL )
chr |= EHCI_CHR_CONTROL;
}
return chr;
}
/**
* Determine endpoint capabilities
*
* @v ep USB endpoint
* @ret cap Endpoint capabilities
*/
static uint32_t ehci_endpoint_capabilities ( struct usb_endpoint *ep ) {
struct usb_device *usb = ep->usb;
struct usb_port *tt = usb_transaction_translator ( usb );
unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
uint32_t cap;
unsigned int i;
/* Determine basic capabilities */
cap = EHCI_CAP_MULT ( ep->burst + 1 );
/* Determine interrupt schedule mask, if applicable */
if ( ( attr == USB_ENDPOINT_ATTR_INTERRUPT ) &&
( ( ep->interval != 0 ) /* avoid infinite loop */ ) ) {
for ( i = 0 ; i < 8 /* microframes per frame */ ;
i += ep->interval ) {
cap |= EHCI_CAP_INTR_SCHED ( i );
}
}
/* Set transaction translator hub address and port, if applicable */
if ( tt ) {
assert ( tt->hub->usb );
cap |= ( EHCI_CAP_TT_HUB ( tt->hub->usb->address ) |
EHCI_CAP_TT_PORT ( tt->address ) );
if ( attr == USB_ENDPOINT_ATTR_INTERRUPT )
cap |= EHCI_CAP_SPLIT_SCHED_DEFAULT;
}
return cap;
}
/**
* Update endpoint characteristics and capabilities
*
* @v ep USB endpoint
*/
static void ehci_endpoint_update ( struct usb_endpoint *ep ) {
struct ehci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
struct ehci_queue_head *head;
/* Update queue characteristics and capabilities */
head = endpoint->ring.head;
head->chr = cpu_to_le32 ( ehci_endpoint_characteristics ( ep ) );
head->cap = cpu_to_le32 ( ehci_endpoint_capabilities ( ep ) );
}
/**
* Open endpoint
*
* @v ep USB endpoint
* @ret rc Return status code
*/
static int ehci_endpoint_open ( struct usb_endpoint *ep ) {
struct usb_device *usb = ep->usb;
struct ehci_device *ehci = usb_get_hostdata ( usb );
struct ehci_endpoint *endpoint;
int rc;
/* Allocate and initialise structure */
endpoint = zalloc ( sizeof ( *endpoint ) );
if ( ! endpoint ) {
rc = -ENOMEM;
goto err_alloc;
}
endpoint->ehci = ehci;
endpoint->ep = ep;
usb_endpoint_set_hostdata ( ep, endpoint );
/* Initialise descriptor ring */
if ( ( rc = ehci_ring_alloc ( ehci, &endpoint->ring ) ) != 0 )
goto err_ring_alloc;
/* Update queue characteristics and capabilities */
ehci_endpoint_update ( ep );
/* Add to list of endpoints */
list_add_tail ( &endpoint->list, &ehci->endpoints );
/* Add to schedule */
ehci_schedule_add ( endpoint );
return 0;
ehci_ring_free ( &endpoint->ring );
err_ring_alloc:
free ( endpoint );
err_alloc:
return rc;
}
/**
* Close endpoint
*
* @v ep USB endpoint
*/
static void ehci_endpoint_close ( struct usb_endpoint *ep ) {
struct ehci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
struct ehci_device *ehci = endpoint->ehci;
struct usb_device *usb = ep->usb;
struct io_buffer *iobuf;
int rc;
/* Remove from schedule */
if ( ( rc = ehci_schedule_del ( endpoint ) ) != 0 ) {
/* No way to prevent hardware from continuing to
* access the memory, so leak it.
*/
DBGC ( ehci, "EHCI %s %s could not unschedule: %s\n",
usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
return;
}
/* Cancel any incomplete transfers */
while ( ehci_ring_fill ( &endpoint->ring ) ) {
iobuf = ehci_dequeue ( &endpoint->ring );
if ( iobuf )
usb_complete_err ( ep, iobuf, -ECANCELED );
}
/* Remove from list of endpoints */
list_del ( &endpoint->list );
/* Free descriptor ring */
ehci_ring_free ( &endpoint->ring );
/* Free endpoint */
free ( endpoint );
}
/**
* Reset endpoint
*
* @v ep USB endpoint
* @ret rc Return status code
*/
static int ehci_endpoint_reset ( struct usb_endpoint *ep ) {
struct ehci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
struct ehci_ring *ring = &endpoint->ring;
struct ehci_transfer_descriptor *cache = &ring->head->cache;
uint32_t link;
/* Sanity checks */
assert ( ! ( cache->status & EHCI_STATUS_ACTIVE ) );
assert ( cache->status & EHCI_STATUS_HALTED );
/* Reset residual count */
ring->residual = 0;
/* Reset data toggle */
cache->len = 0;
/* Prepare to restart at next unconsumed descriptor */
link = virt_to_phys ( &ring->desc[ ring->cons % EHCI_RING_COUNT ] );
cache->next = cpu_to_le32 ( link );
/* Restart ring */
wmb();
cache->status = 0;
return 0;
}
/**
* Update MTU
*
* @v ep USB endpoint
* @ret rc Return status code
*/
static int ehci_endpoint_mtu ( struct usb_endpoint *ep ) {
/* Update endpoint characteristics and capabilities */
ehci_endpoint_update ( ep );
return 0;
}
/**
* Enqueue message transfer
*
* @v ep USB endpoint
* @v iobuf I/O buffer
* @ret rc Return status code
*/
static int ehci_endpoint_message ( struct usb_endpoint *ep,
struct io_buffer *iobuf ) {
struct ehci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
struct ehci_device *ehci = endpoint->ehci;
struct usb_setup_packet *packet;
unsigned int input;
struct ehci_transfer xfers[3];
struct ehci_transfer *xfer = xfers;
size_t len;
int rc;
/* Construct setup stage */
assert ( iob_len ( iobuf ) >= sizeof ( *packet ) );
packet = iobuf->data;
iob_pull ( iobuf, sizeof ( *packet ) );
xfer->data = packet;
xfer->len = sizeof ( *packet );
xfer->flags = EHCI_FL_PID_SETUP;
xfer++;
/* Construct data stage, if applicable */
len = iob_len ( iobuf );
input = ( packet->request & cpu_to_le16 ( USB_DIR_IN ) );
if ( len ) {
xfer->data = iobuf->data;
xfer->len = len;
xfer->flags = ( EHCI_FL_TOGGLE |
( input ? EHCI_FL_PID_IN : EHCI_FL_PID_OUT ) );
xfer++;
}
/* Construct status stage */
xfer->data = NULL;
xfer->len = 0;
xfer->flags = ( EHCI_FL_TOGGLE | EHCI_FL_IOC |
( ( len && input ) ? EHCI_FL_PID_OUT : EHCI_FL_PID_IN));
xfer++;
/* Enqueue transfer */
if ( ( rc = ehci_enqueue ( ehci, &endpoint->ring, iobuf, xfers,
( xfer - xfers ) ) ) != 0 )
return rc;
return 0;
}
/**
* Enqueue stream transfer
*
* @v ep USB endpoint
* @v iobuf I/O buffer
* @v terminate Terminate using a short packet
* @ret rc Return status code
*/
static int ehci_endpoint_stream ( struct usb_endpoint *ep,
struct io_buffer *iobuf, int terminate ) {
struct ehci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
struct ehci_device *ehci = endpoint->ehci;
unsigned int input = ( ep->address & USB_DIR_IN );
struct ehci_transfer xfers[2];
struct ehci_transfer *xfer = xfers;
size_t len = iob_len ( iobuf );
int rc;
/* Create transfer */
xfer->data = iobuf->data;
xfer->len = len;
xfer->flags = ( EHCI_FL_IOC |
( input ? EHCI_FL_PID_IN : EHCI_FL_PID_OUT ) );
xfer++;
if ( terminate && ( ( len & ( ep->mtu - 1 ) ) == 0 ) ) {
xfer->data = NULL;
xfer->len = 0;
assert ( ! input );
xfer->flags = ( EHCI_FL_IOC | EHCI_FL_PID_OUT );
xfer++;
}
/* Enqueue transfer */
if ( ( rc = ehci_enqueue ( ehci, &endpoint->ring, iobuf, xfers,
( xfer - xfers ) ) ) != 0 )
return rc;
return 0;
}
/**
* Poll for completions
*
* @v endpoint Endpoint
*/
static void ehci_endpoint_poll ( struct ehci_endpoint *endpoint ) {
struct ehci_device *ehci = endpoint->ehci;
struct ehci_ring *ring = &endpoint->ring;
struct ehci_transfer_descriptor *desc;
struct usb_endpoint *ep = endpoint->ep;
struct usb_device *usb = ep->usb;
struct io_buffer *iobuf;
unsigned int index;
unsigned int status;
int rc;
/* Consume all completed descriptors */
while ( ehci_ring_fill ( &endpoint->ring ) ) {
/* Stop if we reach an uncompleted descriptor */
rmb();
index = ( ring->cons % EHCI_RING_COUNT );
desc = &ring->desc[index];
status = desc->status;
if ( status & EHCI_STATUS_ACTIVE )
break;
/* Consume this descriptor */
iobuf = ehci_dequeue ( ring );
/* If we have encountered an error, then consume all
* remaining descriptors in this transaction, report
* the error to the USB core, and stop further
* processing.
*/
if ( status & EHCI_STATUS_HALTED ) {
rc = -EIO_STATUS ( status );
DBGC ( ehci, "EHCI %s %s completion %d failed (status "
"%02x): %s\n", usb->name,
usb_endpoint_name ( ep ), index, status,
strerror ( rc ) );
while ( ! iobuf )
iobuf = ehci_dequeue ( ring );
usb_complete_err ( endpoint->ep, iobuf, rc );
return;
}
/* Accumulate residual data count */
ring->residual += ( le16_to_cpu ( desc->len ) & EHCI_LEN_MASK );
/* If this is not the end of a transaction (i.e. has
* no I/O buffer), then continue to next descriptor.
*/
if ( ! iobuf )
continue;
/* Update I/O buffer length */
iob_unput ( iobuf, ring->residual );
ring->residual = 0;
/* Report completion to USB core */
usb_complete ( endpoint->ep, iobuf );
}
}
/******************************************************************************
*
* Device operations
*
******************************************************************************
*/
/**
* Open device
*
* @v usb USB device
* @ret rc Return status code
*/
static int ehci_device_open ( struct usb_device *usb ) {
struct ehci_device *ehci = usb_bus_get_hostdata ( usb->port->hub->bus );
usb_set_hostdata ( usb, ehci );
return 0;
}
/**
* Close device
*
* @v usb USB device
*/
static void ehci_device_close ( struct usb_device *usb ) {
struct ehci_device *ehci = usb_get_hostdata ( usb );
struct usb_bus *bus = ehci->bus;
/* Free device address, if assigned */
if ( usb->address )
usb_free_address ( bus, usb->address );
}
/**
* Assign device address
*
* @v usb USB device
* @ret rc Return status code
*/
static int ehci_device_address ( struct usb_device *usb ) {
struct ehci_device *ehci = usb_get_hostdata ( usb );
struct usb_bus *bus = ehci->bus;
struct usb_endpoint *ep0 = usb_endpoint ( usb, USB_EP0_ADDRESS );
int address;
int rc;
/* Sanity checks */
assert ( usb->address == 0 );
assert ( ep0 != NULL );
/* Allocate device address */
address = usb_alloc_address ( bus );
if ( address < 0 ) {
rc = address;
DBGC ( ehci, "EHCI %s could not allocate address: %s\n",
usb->name, strerror ( rc ) );
goto err_alloc_address;
}
/* Set address */
if ( ( rc = usb_set_address ( usb, address ) ) != 0 )
goto err_set_address;
/* Update device address */
usb->address = address;
/* Update control endpoint characteristics and capabilities */
ehci_endpoint_update ( ep0 );
return 0;
err_set_address:
usb_free_address ( bus, address );
err_alloc_address:
return rc;
}
/******************************************************************************
*
* Hub operations
*
******************************************************************************
*/
/**
* Open hub
*
* @v hub USB hub
* @ret rc Return status code
*/
static int ehci_hub_open ( struct usb_hub *hub __unused ) {
/* Nothing to do */
return 0;
}
/**
* Close hub
*
* @v hub USB hub
*/
static void ehci_hub_close ( struct usb_hub *hub __unused ) {
/* Nothing to do */
}
/******************************************************************************
*
* Root hub operations
*
******************************************************************************
*/
/**
* Open root hub
*
* @v hub USB hub
* @ret rc Return status code
*/
static int ehci_root_open ( struct usb_hub *hub ) {
struct usb_bus *bus = hub->bus;
struct ehci_device *ehci = usb_bus_get_hostdata ( bus );
uint32_t portsc;
unsigned int i;
/* Route all ports to EHCI controller */
writel ( EHCI_CONFIGFLAG_CF, ehci->op + EHCI_OP_CONFIGFLAG );
/* Enable power to all ports */
for ( i = 1 ; i <= ehci->ports ; i++ ) {
portsc = readl ( ehci->op + EHCI_OP_PORTSC ( i ) );
portsc &= ~EHCI_PORTSC_CHANGE;
portsc |= EHCI_PORTSC_PP;
writel ( portsc, ehci->op + EHCI_OP_PORTSC ( i ) );
}
/* Wait 20ms after potentially enabling power to a port */
mdelay ( EHCI_PORT_POWER_DELAY_MS );
/* Record hub driver private data */
usb_hub_set_drvdata ( hub, ehci );
return 0;
}
/**
* Close root hub
*
* @v hub USB hub
*/
static void ehci_root_close ( struct usb_hub *hub ) {
struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
/* Route all ports back to companion controllers */
writel ( 0, ehci->op + EHCI_OP_CONFIGFLAG );
/* Clear hub driver private data */
usb_hub_set_drvdata ( hub, NULL );
}
/**
* Enable port
*
* @v hub USB hub
* @v port USB port
* @ret rc Return status code
*/
static int ehci_root_enable ( struct usb_hub *hub, struct usb_port *port ) {
struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
uint32_t portsc;
unsigned int line;
unsigned int i;
/* Check for a low-speed device */
portsc = readl ( ehci->op + EHCI_OP_PORTSC ( port->address ) );
line = EHCI_PORTSC_LINE_STATUS ( portsc );
if ( line == EHCI_PORTSC_LINE_STATUS_LOW ) {
DBGC ( ehci, "EHCI %s-%d detected low-speed device: "
"disowning\n", ehci->name, port->address );
goto disown;
}
/* Reset port */
portsc &= ~( EHCI_PORTSC_PED | EHCI_PORTSC_CHANGE );
portsc |= EHCI_PORTSC_PR;
writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
mdelay ( USB_RESET_DELAY_MS );
portsc &= ~EHCI_PORTSC_PR;
writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
/* Wait for reset to complete */
for ( i = 0 ; i < EHCI_PORT_RESET_MAX_WAIT_MS ; i++ ) {
/* Check port status */
portsc = readl ( ehci->op + EHCI_OP_PORTSC ( port->address ) );
if ( ! ( portsc & EHCI_PORTSC_PR ) ) {
if ( portsc & EHCI_PORTSC_PED )
return 0;
DBGC ( ehci, "EHCI %s-%d not enabled after reset: "
"disowning\n", ehci->name, port->address );
goto disown;
}
/* Delay */
mdelay ( 1 );
}
DBGC ( ehci, "EHCI %s-%d timed out waiting for port to reset\n",
ehci->name, port->address );
return -ETIMEDOUT;
disown:
/* Disown port */
portsc &= ~EHCI_PORTSC_CHANGE;
portsc |= EHCI_PORTSC_OWNER;
writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
/* Delay to allow child companion controllers to settle */
mdelay ( EHCI_DISOWN_DELAY_MS );
/* Poll child companion controllers */
ehci_poll_companions ( ehci );
return -ENODEV;
}
/**
* Disable port
*
* @v hub USB hub
* @v port USB port
* @ret rc Return status code
*/
static int ehci_root_disable ( struct usb_hub *hub, struct usb_port *port ) {
struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
uint32_t portsc;
/* Disable port */
portsc = readl ( ehci->op + EHCI_OP_PORTSC ( port->address ) );
portsc &= ~( EHCI_PORTSC_PED | EHCI_PORTSC_CHANGE );
writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
return 0;
}
/**
* Update root hub port speed
*
* @v hub USB hub
* @v port USB port
* @ret rc Return status code
*/
static int ehci_root_speed ( struct usb_hub *hub, struct usb_port *port ) {
struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
uint32_t portsc;
unsigned int speed;
unsigned int line;
int ccs;
int csc;
int ped;
/* Read port status */
portsc = readl ( ehci->op + EHCI_OP_PORTSC ( port->address ) );
DBGC2 ( ehci, "EHCI %s-%d status is %08x\n",
ehci->name, port->address, portsc );
ccs = ( portsc & EHCI_PORTSC_CCS );
csc = ( portsc & EHCI_PORTSC_CSC );
ped = ( portsc & EHCI_PORTSC_PED );
line = EHCI_PORTSC_LINE_STATUS ( portsc );
/* Record disconnections and clear changes */
port->disconnected |= csc;
writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
/* Determine port speed */
if ( ! ccs ) {
/* Port not connected */
speed = USB_SPEED_NONE;
} else if ( line == EHCI_PORTSC_LINE_STATUS_LOW ) {
/* Detected as low-speed */
speed = USB_SPEED_LOW;
} else if ( ped ) {
/* Port already enabled: must be high-speed */
speed = USB_SPEED_HIGH;
} else {
/* Not low-speed and not yet enabled. Could be either
* full-speed or high-speed; we can't yet tell.
*/
speed = USB_SPEED_FULL;
}
port->speed = speed;
return 0;
}
/**
* Clear transaction translator buffer
*
* @v hub USB hub
* @v port USB port
* @v ep USB endpoint
* @ret rc Return status code
*/
static int ehci_root_clear_tt ( struct usb_hub *hub, struct usb_port *port,
struct usb_endpoint *ep ) {
struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
/* Should never be called; this is a root hub */
DBGC ( ehci, "EHCI %s-%d nonsensical CLEAR_TT for %s %s\n", ehci->name,
port->address, ep->usb->name, usb_endpoint_name ( ep ) );
return -ENOTSUP;
}
/**
* Poll for port status changes
*
* @v hub USB hub
* @v port USB port
*/
static void ehci_root_poll ( struct usb_hub *hub, struct usb_port *port ) {
struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
uint32_t portsc;
uint32_t change;
/* Do nothing unless something has changed */
portsc = readl ( ehci->op + EHCI_OP_PORTSC ( port->address ) );
change = ( portsc & EHCI_PORTSC_CHANGE );
if ( ! change )
return;
/* Record disconnections and clear changes */
port->disconnected |= ( portsc & EHCI_PORTSC_CSC );
writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
/* Report port status change */
usb_port_changed ( port );
}
/******************************************************************************
*
* Bus operations
*
******************************************************************************
*/
/**
* Open USB bus
*
* @v bus USB bus
* @ret rc Return status code
*/
static int ehci_bus_open ( struct usb_bus *bus ) {
struct ehci_device *ehci = usb_bus_get_hostdata ( bus );
unsigned int frames;
size_t len;
int rc;
/* Sanity checks */
assert ( list_empty ( &ehci->async ) );
assert ( list_empty ( &ehci->periodic ) );
/* Allocate and initialise asynchronous queue head */
ehci->head = malloc_dma ( sizeof ( *ehci->head ),
ehci_align ( sizeof ( *ehci->head ) ) );
if ( ! ehci->head ) {
rc = -ENOMEM;
goto err_alloc_head;
}
memset ( ehci->head, 0, sizeof ( *ehci->head ) );
ehci->head->chr = cpu_to_le32 ( EHCI_CHR_HEAD );
ehci->head->cache.next = cpu_to_le32 ( EHCI_LINK_TERMINATE );
ehci->head->cache.status = EHCI_STATUS_HALTED;
ehci_async_schedule ( ehci );
writel ( virt_to_phys ( ehci->head ),
ehci->op + EHCI_OP_ASYNCLISTADDR );
/* Use async queue head to determine control data structure segment */
ehci->ctrldssegment =
( ( ( uint64_t ) virt_to_phys ( ehci->head ) ) >> 32 );
if ( ehci->addr64 ) {
writel ( ehci->ctrldssegment, ehci->op + EHCI_OP_CTRLDSSEGMENT);
} else if ( ehci->ctrldssegment ) {
DBGC ( ehci, "EHCI %s CTRLDSSEGMENT not supported\n",
ehci->name );
rc = -ENOTSUP;
goto err_ctrldssegment;
}
/* Allocate periodic frame list */
frames = EHCI_PERIODIC_FRAMES ( ehci->flsize );
len = ( frames * sizeof ( ehci->frame[0] ) );
ehci->frame = malloc_dma ( len, EHCI_PAGE_ALIGN );
if ( ! ehci->frame ) {
rc = -ENOMEM;
goto err_alloc_frame;
}
if ( ( rc = ehci_ctrl_reachable ( ehci, ehci->frame ) ) != 0 ) {
DBGC ( ehci, "EHCI %s frame list unreachable\n", ehci->name );
goto err_unreachable_frame;
}
ehci_periodic_schedule ( ehci );
writel ( virt_to_phys ( ehci->frame ),
ehci->op + EHCI_OP_PERIODICLISTBASE );
/* Start controller */
ehci_run ( ehci );
return 0;
ehci_stop ( ehci );
err_unreachable_frame:
free_dma ( ehci->frame, len );
err_alloc_frame:
err_ctrldssegment:
free_dma ( ehci->head, sizeof ( *ehci->head ) );
err_alloc_head:
return rc;
}
/**
* Close USB bus
*
* @v bus USB bus
*/
static void ehci_bus_close ( struct usb_bus *bus ) {
struct ehci_device *ehci = usb_bus_get_hostdata ( bus );
unsigned int frames = EHCI_PERIODIC_FRAMES ( ehci->flsize );
/* Sanity checks */
assert ( list_empty ( &ehci->async ) );
assert ( list_empty ( &ehci->periodic ) );
/* Stop controller */
ehci_stop ( ehci );
/* Free periodic frame list */
free_dma ( ehci->frame, ( frames * sizeof ( ehci->frame[0] ) ) );
/* Free asynchronous schedule */
free_dma ( ehci->head, sizeof ( *ehci->head ) );
}
/**
* Poll USB bus
*
* @v bus USB bus
*/
static void ehci_bus_poll ( struct usb_bus *bus ) {
struct ehci_device *ehci = usb_bus_get_hostdata ( bus );
struct usb_hub *hub = bus->hub;
struct ehci_endpoint *endpoint;
unsigned int i;
uint32_t usbsts;
uint32_t change;
/* Do nothing unless something has changed */
usbsts = readl ( ehci->op + EHCI_OP_USBSTS );
assert ( usbsts & EHCI_USBSTS_ASYNC );
assert ( usbsts & EHCI_USBSTS_PERIODIC );
assert ( ! ( usbsts & EHCI_USBSTS_HCH ) );
change = ( usbsts & EHCI_USBSTS_CHANGE );
if ( ! change )
return;
/* Acknowledge changes */
writel ( usbsts, ehci->op + EHCI_OP_USBSTS );
/* Process completions, if applicable */
if ( change & ( EHCI_USBSTS_USBINT | EHCI_USBSTS_USBERRINT ) ) {
/* Iterate over all endpoints looking for completed
* descriptors. We trust that completion handlers are
* minimal and will not do anything that could
* plausibly affect the endpoint list itself.
*/
list_for_each_entry ( endpoint, &ehci->endpoints, list )
ehci_endpoint_poll ( endpoint );
}
/* Process port status changes, if applicable */
if ( change & EHCI_USBSTS_PORT ) {
/* Iterate over all ports looking for status changes */
for ( i = 1 ; i <= ehci->ports ; i++ )
ehci_root_poll ( hub, usb_port ( hub, i ) );
}
/* Report fatal errors */
if ( change & EHCI_USBSTS_SYSERR )
DBGC ( ehci, "EHCI %s host system error\n", ehci->name );
}
/******************************************************************************
*
* PCI interface
*
******************************************************************************
*/
/** USB host controller operations */
static struct usb_host_operations ehci_operations = {
.endpoint = {
.open = ehci_endpoint_open,
.close = ehci_endpoint_close,
.reset = ehci_endpoint_reset,
.mtu = ehci_endpoint_mtu,
.message = ehci_endpoint_message,
.stream = ehci_endpoint_stream,
},
.device = {
.open = ehci_device_open,
.close = ehci_device_close,
.address = ehci_device_address,
},
.bus = {
.open = ehci_bus_open,
.close = ehci_bus_close,
.poll = ehci_bus_poll,
},
.hub = {
.open = ehci_hub_open,
.close = ehci_hub_close,
},
.root = {
.open = ehci_root_open,
.close = ehci_root_close,
.enable = ehci_root_enable,
.disable = ehci_root_disable,
.speed = ehci_root_speed,
.clear_tt = ehci_root_clear_tt,
},
};
/**
* Probe PCI device
*
* @v pci PCI device
* @ret rc Return status code
*/
static int ehci_probe ( struct pci_device *pci ) {
struct ehci_device *ehci;
struct usb_port *port;
unsigned long bar_start;
size_t bar_size;
unsigned int i;
int rc;
/* Allocate and initialise structure */
ehci = zalloc ( sizeof ( *ehci ) );
if ( ! ehci ) {
rc = -ENOMEM;
goto err_alloc;
}
ehci->name = pci->dev.name;
INIT_LIST_HEAD ( &ehci->endpoints );
INIT_LIST_HEAD ( &ehci->async );
INIT_LIST_HEAD ( &ehci->periodic );
/* Fix up PCI device */
adjust_pci_device ( pci );
/* Map registers */
bar_start = pci_bar_start ( pci, EHCI_BAR );
bar_size = pci_bar_size ( pci, EHCI_BAR );
ehci->regs = ioremap ( bar_start, bar_size );
if ( ! ehci->regs ) {
rc = -ENODEV;
goto err_ioremap;
}
/* Initialise EHCI device */
ehci_init ( ehci, ehci->regs );
/* Initialise USB legacy support and claim ownership */
ehci_legacy_init ( ehci, pci );
ehci_legacy_claim ( ehci, pci );
/* Reset device */
if ( ( rc = ehci_reset ( ehci ) ) != 0 )
goto err_reset;
/* Allocate USB bus */
ehci->bus = alloc_usb_bus ( &pci->dev, ehci->ports, EHCI_MTU,
&ehci_operations );
if ( ! ehci->bus ) {
rc = -ENOMEM;
goto err_alloc_bus;
}
usb_bus_set_hostdata ( ehci->bus, ehci );
usb_hub_set_drvdata ( ehci->bus->hub, ehci );
/* Set port protocols */
for ( i = 1 ; i <= ehci->ports ; i++ ) {
port = usb_port ( ehci->bus->hub, i );
port->protocol = USB_PROTO_2_0;
}
/* Register USB bus */
if ( ( rc = register_usb_bus ( ehci->bus ) ) != 0 )
goto err_register;
pci_set_drvdata ( pci, ehci );
return 0;
unregister_usb_bus ( ehci->bus );
err_register:
free_usb_bus ( ehci->bus );
err_alloc_bus:
ehci_reset ( ehci );
err_reset:
ehci_legacy_release ( ehci, pci );
iounmap ( ehci->regs );
err_ioremap:
free ( ehci );
err_alloc:
return rc;
}
/**
* Remove PCI device
*
* @v pci PCI device
*/
static void ehci_remove ( struct pci_device *pci ) {
struct ehci_device *ehci = pci_get_drvdata ( pci );
struct usb_bus *bus = ehci->bus;
unregister_usb_bus ( bus );
assert ( list_empty ( &ehci->async ) );
assert ( list_empty ( &ehci->periodic ) );
free_usb_bus ( bus );
ehci_reset ( ehci );
ehci_legacy_release ( ehci, pci );
iounmap ( ehci->regs );
free ( ehci );
}
/** EHCI PCI device IDs */
static struct pci_device_id ehci_ids[] = {
PCI_ROM ( 0xffff, 0xffff, "ehci", "EHCI", 0 ),
};
/** EHCI PCI driver */
struct pci_driver ehci_driver __pci_driver = {
.ids = ehci_ids,
.id_count = ( sizeof ( ehci_ids ) / sizeof ( ehci_ids[0] ) ),
.class = PCI_CLASS_ID ( PCI_CLASS_SERIAL, PCI_CLASS_SERIAL_USB,
PCI_CLASS_SERIAL_USB_EHCI ),
.probe = ehci_probe,
.remove = ehci_remove,
};
/**
* Prepare for exit
*
* @v booting System is shutting down for OS boot
*/
static void ehci_shutdown ( int booting ) {
/* If we are shutting down to boot an OS, then prevent the
* release of ownership back to BIOS.
*/
ehci_legacy_prevent_release = booting;
}
/** Startup/shutdown function */
struct startup_fn ehci_startup __startup_fn ( STARTUP_LATE ) = {
.shutdown = ehci_shutdown,
};
|