summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/1394/adapters/hci1394_ohci.c
blob: e0d9dbdd9c93adc6a9ee5bfbdbe437318926dd5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright (c) 2016 by Delphix. All rights reserved.
 */

/*
 * hci1394_ohci.c
 *    Provides access routines to the OpenHCI HW.
 */

#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/modctl.h>
#include <sys/sunddi.h>
#include <sys/types.h>
#include <sys/mkdev.h>
#include <sys/kmem.h>
#include <sys/pci.h>

#include <sys/1394/adapters/hci1394.h>
#include <sys/1394/adapters/hci1394_extern.h>


/*
 * Data swap macros used to swap config rom data that is going to be placed
 * in OpenHCI registers.  The config rom is treated like a byte stream.  When
 * the services layer calls into us to update the config rom, they pass us a
 * byte stream of data.  This works well except for the the fact that the
 * hardware uses its internal registers for the first 5 quadlets.  We have to
 * copy the cfgrom header and bus options into their corresponding OpenHCI
 * registers.  On an x86 machine, this means we have to byte swap them first.
 */
#ifdef _LITTLE_ENDIAN
#define	OHCI_SWAP32(DATA)	(ddi_swap32(DATA))
#else
#define	OHCI_SWAP32(DATA)	(DATA)
#endif


static int hci1394_ohci_selfid_init(hci1394_ohci_handle_t ohci_hdl);
static int hci1394_ohci_cfgrom_init(hci1394_ohci_handle_t ohci_hdl);
static int hci1394_ohci_chip_init(hci1394_ohci_handle_t ohci_hdl);
static int hci1394_ohci_phy_resume(hci1394_ohci_handle_t ohci_hdl);
static int hci1394_ohci_1394a_init(hci1394_ohci_handle_t ohci_hdl);
static int hci1394_ohci_1394a_resume(hci1394_ohci_handle_t ohci_hdl);
static int hci1394_ohci_phy_read_no_lock(hci1394_ohci_handle_t ohci_hdl,
    uint_t address, uint_t *data);
static int hci1394_ohci_phy_write_no_lock(hci1394_ohci_handle_t ohci_hdl,
    uint_t address, uint_t data);


/*
 * hci1394_ohci_init()
 *    Initialize the OpenHCI hardware.
 */
int
hci1394_ohci_init(hci1394_state_t *soft_state, hci1394_drvinfo_t *drvinfo,
    hci1394_ohci_handle_t *ohci_hdl)
{
	int status;
	uint32_t version;
	hci1394_ohci_t *ohci;
#if defined(__x86)
	uint16_t cmdreg;
#endif


	ASSERT(ohci_hdl != NULL);

	/* alloc the space for ohci */
	ohci = kmem_alloc(sizeof (hci1394_ohci_t), KM_SLEEP);
	*ohci_hdl = ohci;

	/*
	 * Start with the cycle timer rollover interrupt disabled.  When it is
	 * enabled, we will get an interrupt every 64 seconds, even if we have
	 * nothing plugged into the bus.  This interrupt is used to keep track
	 * of the bus time.  We will enable the interrupt when the bus manager
	 * writes to the bus_time CSR register (Currently there are not known
	 * implementations that write to the bus_time register)
	 */
	ohci->ohci_bustime_enabled = B_FALSE;
	ohci->ohci_bustime_count = 0;

	ohci->ohci_set_root_holdoff = B_FALSE;
	ohci->ohci_set_gap_count = B_FALSE;
	ohci->ohci_gap_count = 0;

	mutex_init(&ohci->ohci_mutex, NULL, MUTEX_DRIVER,
	    drvinfo->di_iblock_cookie);

	/* Map OpenHCI Registers */
	status = ddi_regs_map_setup(drvinfo->di_dip, OHCI_REG_SET,
	    (caddr_t *)&ohci->ohci_regs, 0, 0, &drvinfo->di_reg_attr,
	    &ohci->ohci_reg_handle);
	if (status != DDI_SUCCESS) {
		mutex_destroy(&ohci->ohci_mutex);
		kmem_free(ohci, sizeof (hci1394_ohci_t));
		*ohci_hdl = NULL;
		return (DDI_FAILURE);
	}

	ohci->soft_state = soft_state;
	ohci->ohci_drvinfo = drvinfo;

	/*
	 * make sure PCI Master and PCI Memory Access are enabled on x86
	 * platforms. This may not be the case if plug and play OS is
	 * set in the BIOS
	 */
#if defined(__x86)
	cmdreg = pci_config_get16(soft_state->pci_config, PCI_CONF_COMM);
	if ((cmdreg & (PCI_COMM_MAE | PCI_COMM_ME)) != (PCI_COMM_MAE |
	    PCI_COMM_ME)) {
		cmdreg |= PCI_COMM_MAE | PCI_COMM_ME;
		pci_config_put16(soft_state->pci_config, PCI_CONF_COMM, cmdreg);
	}
#endif

	/*
	 * Initialize the openHCI chip.  This is broken out because we need to
	 * do this when resuming too.
	 */
	status = hci1394_ohci_chip_init(ohci);
	if (status != DDI_SUCCESS) {
		ddi_regs_map_free(&ohci->ohci_reg_handle);
		mutex_destroy(&ohci->ohci_mutex);
		kmem_free(ohci, sizeof (hci1394_ohci_t));
		*ohci_hdl = NULL;
		return (DDI_FAILURE);
	}

	/* Init the 1394 PHY */
	status = hci1394_ohci_phy_init(ohci);
	if (status != DDI_SUCCESS) {
		(void) hci1394_ohci_soft_reset(ohci);
		ddi_regs_map_free(&ohci->ohci_reg_handle);
		mutex_destroy(&ohci->ohci_mutex);
		kmem_free(ohci, sizeof (hci1394_ohci_t));
		*ohci_hdl = NULL;
		return (DDI_FAILURE);
	}

	/* Init 1394a features if present */
	if (ohci->ohci_phy == H1394_PHY_1394A) {
		status = hci1394_ohci_1394a_init(ohci);
		if (status != DDI_SUCCESS) {
			(void) hci1394_ohci_soft_reset(ohci);
			ddi_regs_map_free(&ohci->ohci_reg_handle);
			mutex_destroy(&ohci->ohci_mutex);
			kmem_free(ohci, sizeof (hci1394_ohci_t));
			*ohci_hdl = NULL;
			return (DDI_FAILURE);
		}
	}

	/* save away guid, phy type, and vendor info */
	soft_state->halinfo.guid = hci1394_ohci_guid(ohci);
	soft_state->halinfo.phy = ohci->ohci_phy;
	soft_state->vendor_info.ohci_vendor_id =
	    ddi_get32(ohci->ohci_reg_handle, &ohci->ohci_regs->vendor_id);
	version = ddi_get32(ohci->ohci_reg_handle, &ohci->ohci_regs->version);
	soft_state->vendor_info.ohci_version = version;

	/* We do not support version < 1.0 */
	if (OHCI_VERSION(version) == 0) {
		cmn_err(CE_NOTE,
		    "hci1394(%d): OpenHCI version %x.%x is not supported",
		    drvinfo->di_instance, OHCI_VERSION(version),
		    OHCI_REVISION(version));
		(void) hci1394_ohci_soft_reset(ohci);
		ddi_regs_map_free(&ohci->ohci_reg_handle);
		mutex_destroy(&ohci->ohci_mutex);
		kmem_free(ohci, sizeof (hci1394_ohci_t));
		*ohci_hdl = NULL;
		return (DDI_FAILURE);
	}

	/* Initialize the selfid buffer */
	status = hci1394_ohci_selfid_init(ohci);
	if (status != DDI_SUCCESS) {
		(void) hci1394_ohci_soft_reset(ohci);
		ddi_regs_map_free(&ohci->ohci_reg_handle);
		mutex_destroy(&ohci->ohci_mutex);
		kmem_free(ohci, sizeof (hci1394_ohci_t));
		*ohci_hdl = NULL;
		return (DDI_FAILURE);
	}

	/* Initialize the config rom buffer */
	status = hci1394_ohci_cfgrom_init(ohci);
	if (status != DDI_SUCCESS) {
		(void) hci1394_ohci_soft_reset(ohci);
		hci1394_buf_free(&ohci->ohci_selfid_handle);
		ddi_regs_map_free(&ohci->ohci_reg_handle);
		mutex_destroy(&ohci->ohci_mutex);
		kmem_free(ohci, sizeof (hci1394_ohci_t));
		*ohci_hdl = NULL;
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_fini()
 *    Cleanup after OpenHCI init.  This should be called during detach.
 */
void
hci1394_ohci_fini(hci1394_ohci_handle_t *ohci_hdl)
{
	hci1394_ohci_t *ohci;


	ASSERT(ohci_hdl != NULL);

	ohci = *ohci_hdl;

	/* reset chip */
	(void) hci1394_ohci_soft_reset(ohci);

	/* Free config rom space */
	hci1394_buf_free(&ohci->ohci_cfgrom_handle);

	/* Free selfid buffer space */
	hci1394_buf_free(&ohci->ohci_selfid_handle);

	/* Free up the OpenHCI registers */
	ddi_regs_map_free(&ohci->ohci_reg_handle);

	mutex_destroy(&ohci->ohci_mutex);

	/* Free the OpenHCI state space */
	kmem_free(ohci, sizeof (hci1394_ohci_t));
	*ohci_hdl = NULL;
}


/*
 * hci1394_ohci_chip_init()
 *    Initialize the OpenHCI registers.  This contains the bulk of the initial
 *    register setup.
 */
static int
hci1394_ohci_chip_init(hci1394_ohci_handle_t ohci_hdl)
{
	int status;


	ASSERT(ohci_hdl != NULL);

	/* Reset 1394 OHCI HW */
	status = hci1394_ohci_soft_reset(ohci_hdl);
	if (status != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}

	/*
	 * Setup Host Control Register. The software reset does not put all
	 * registers in a known state. The Host Control Register is one of these
	 * registers. First make sure noByteSwapData and postedWriteEnable and
	 * are cleared.
	 */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->hc_ctrl_clr, OHCI_HC_NO_BSWAP |
	    OHCI_HC_POSTWR_ENBL);

	/*
	 * the determination if we should swap data is made during the PCI
	 * initialization.
	 */
	if (ohci_hdl->soft_state->swap_data == B_FALSE) {
		/*
		 * most hba's don't swap data.  It will be swapped in the
		 * global swap for SPARC.  Enable Link Power(LPS). Enable
		 * Posted Writes
		 */
		ddi_put32(ohci_hdl->ohci_reg_handle,
		    &ohci_hdl->ohci_regs->hc_ctrl_set, OHCI_HC_NO_BSWAP |
		    OHCI_HC_LPS | OHCI_HC_POSTWR_ENBL);
	} else {
		/*
		 * Swap Data. Enable Link Power(LPS). Enable Posted Writes
		 */
		ddi_put32(ohci_hdl->ohci_reg_handle,
		    &ohci_hdl->ohci_regs->hc_ctrl_set, OHCI_HC_LPS |
		    OHCI_HC_POSTWR_ENBL);
	}

	/*
	 * Wait for PHY to come up. There does not seem to be standard time for
	 * how long wait for the PHY to come up. The problem is that the PHY
	 * provides a clock to the link layer and if that is not stable, we
	 * could get a PCI timeout error when reading/writing a phy register
	 * (and maybe an OpenHCI register?)  This used to be set to 10mS which
	 * works for just about every adapter we tested on.  We got a new TI
	 * adapter which would crash the system once in a while if nothing
	 * (1394 device) was pluged into the adapter.  Changing this delay to
	 * 50mS made that problem go away. This value is set via a patchable
	 * variable located in hci1394_extern.c
	 */
	delay(drv_usectohz(hci1394_phy_stabilization_delay_uS));

	/* Clear Isochrounous receive multi-chan mode registers */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ir_multi_maskhi_clr, 0xFFFFFFFF);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ir_multi_masklo_clr, 0xFFFFFFFF);

	/*
	 * Setup async retry on busy or ack_data_error
	 *   secondlimit = 0 <= bits 31-29
	 *   cycleLimit = 0 <= bits 28-16
	 *   maxPhysRespRetries = 0 <= bits 11-8
	 *   maxARRespRetries = 0 <= bits 7-4
	 *   maxATReqRetries = 2 <= bits 3-0
	 */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->at_retries, 0x00000002);

	/*
	 * Setup Link Control
	 *   Enable cycleMaster, cycleTimerEnable, and rcvPhyPkt.
	 */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->link_ctrl_clr, 0xFFFFFFFF);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->link_ctrl_set, OHCI_LC_CYC_MAST |
	    OHCI_LC_CTIME_ENBL | OHCI_LC_RCV_PHY);

	/*
	 * Set the Physical address map boundary to 0x0000FFFFFFFF. The
	 * phys_upper_bound is the upper 32-bits of the 48-bit 1394 address. The
	 * lower 16 bits are assumed to be 0xFFFF.
	 */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->phys_upper_bound, (uint32_t)0x0000FFFF);

	/*
	 * Enable all async requests.
	 * The asyncReqResourceAll bit (0x80000000) does not get cleared during
	 * a bus reset.  If this code is changed to selectively allow nodes to
	 * perform ARREQ's, the ARREQ filter bits will need to be updated after
	 * every bus reset.
	 */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ar_req_filterhi_set, (uint32_t)0x80000000);

	/*
	 * clear isochronous interrupt event and mask registers clearing the
	 * mask registers disable all isoc tx & rx ints
	 */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->it_intr_event_clr, (uint32_t)0xFFFFFFFF);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->it_intr_mask_clr, (uint32_t)0xFFFFFFFF);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ir_intr_event_clr, (uint32_t)0xFFFFFFFF);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ir_intr_mask_clr, (uint32_t)0xFFFFFFFF);

	/* Clear interrupt event/mask register */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->intr_event_clr, (uint32_t)0xFFFFFFFF);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->intr_mask_clr, (uint32_t)0xFFFFFFFF);

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_soft_reset()
 *    Reset OpenHCI HW.
 */
int
hci1394_ohci_soft_reset(hci1394_ohci_handle_t ohci_hdl)
{
	uint32_t resetStatus;


	ASSERT(ohci_hdl != NULL);

	/* Reset 1394 HW - Reset is bit 16 in HCControl */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->hc_ctrl_set, OHCI_HC_SOFT_RESET);

	/* Wait for reset to complete */
	drv_usecwait(OHCI_CHIP_RESET_TIME_IN_uSEC);

	/* Verify reset is complete */
	resetStatus = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->hc_ctrl_set);
	resetStatus = resetStatus & OHCI_HC_SOFT_RESET;
	if (resetStatus != 0) {
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_reg_read()
 *    Read OpenHCI register.  This is called from the test ioctl interface
 *    through devctl.
 */
void
hci1394_ohci_reg_read(hci1394_ohci_handle_t ohci_hdl,
    uint_t offset, uint32_t *data)
{
	uint32_t *addr;


	ASSERT(ohci_hdl != NULL);
	ASSERT(data != NULL);

	addr = (uint32_t *)((uintptr_t)ohci_hdl->ohci_regs +
	    (uintptr_t)(offset & OHCI_REG_ADDR_MASK));
	*data = ddi_get32(ohci_hdl->ohci_reg_handle, addr);
}


/*
 * hci1394_ohci_reg_write()
 *    Write OpenHCI register.  This is called from the test ioctl interface
 *    through devctl.
 */
void
hci1394_ohci_reg_write(hci1394_ohci_handle_t ohci_hdl,
    uint_t offset, uint32_t data)
{
	uint32_t *addr;


	ASSERT(ohci_hdl != NULL);

	addr = (uint32_t *)((uintptr_t)ohci_hdl->ohci_regs +
	    (uintptr_t)(offset & OHCI_REG_ADDR_MASK));
	ddi_put32(ohci_hdl->ohci_reg_handle, addr, data);
}


/*
 * hci1394_ohci_intr_master_enable()
 *    Enable interrupts to be passed on from OpenHCI.  This is a global mask.
 *    Individual interrupts still need to be enabled for interrupts to be
 *    generated.
 */
void
hci1394_ohci_intr_master_enable(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->intr_mask_set, OHCI_INTR_MASTER_INTR_ENBL);
}


/*
 * hci1394_ohci_intr_master_disable()
 *    Disable all OpenHCI interrupts from being passed on.  This does not affect
 *    the individual interrupt mask settings.  When interrupts are enabled
 *    again, the same individual interrupts will still be enabled.
 */
void
hci1394_ohci_intr_master_disable(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->intr_mask_clr, OHCI_INTR_MASTER_INTR_ENBL);
}


/*
 * hci1394_ohci_intr_asserted()
 *    Return which ENABLED interrupts are asserted.  If an interrupt is disabled
 *    via its mask bit, it will not be returned from here.
 *
 * NOTE: we may want to make this a macro at some point.
 */
uint32_t
hci1394_ohci_intr_asserted(hci1394_ohci_handle_t ohci_hdl)
{
	uint32_t interrupts_asserted;

	ASSERT(ohci_hdl != NULL);

	/*
	 * Only look at interrupts which are enabled by reading the
	 * intr_event_clr register.
	 */
	interrupts_asserted = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->intr_event_clr);

	return (interrupts_asserted);
}


/*
 * hci1394_ohci_intr_enable()
 *    Enable an individual interrupt or set of interrupts. This does not affect
 *    the global interrupt mask.
 */
void
hci1394_ohci_intr_enable(hci1394_ohci_handle_t ohci_hdl,
    uint32_t interrupt_mask)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->intr_mask_set, interrupt_mask);
}


/*
 * hci1394_ohci_intr_disable()
 *    Disable an individual interrupt or set of interrupts. This does not affect
 *    the global interrupt mask.
 */
void
hci1394_ohci_intr_disable(hci1394_ohci_handle_t ohci_hdl,
    uint32_t interrupt_mask)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->intr_mask_clr, interrupt_mask);
}


/*
 * hci1394_ohci_intr_clear()
 *    Clear a set of interrupts so that they are not asserted anymore.
 *
 * NOTE: we may want to make this a macro at some point.
 */
void
hci1394_ohci_intr_clear(hci1394_ohci_handle_t ohci_hdl,
    uint32_t interrupt_mask)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->intr_event_clr, interrupt_mask);
}


/*
 * hci1394_ohci_it_intr_asserted()
 *    Return which ENABLED isoch TX interrupts are asserted.  If an interrupt is
 *    disabled via its mask bit, it will not be returned from here.
 *
 * NOTE: we may want to make this a macro at some point.
 */
uint32_t
hci1394_ohci_it_intr_asserted(hci1394_ohci_handle_t ohci_hdl)
{
	uint32_t interrupts_asserted;

	ASSERT(ohci_hdl != NULL);

	/* Only look at interrupts which are enabled */
	interrupts_asserted = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->it_intr_event_clr);

	return (interrupts_asserted);
}


/*
 * hci1394_ohci_it_intr_enable()
 *    Enable an individual isoch TX interrupt. This does not affect the general
 *    isoch interrupt mask in the OpenHCI Mask register.  That is enabled/
 *    disabled via hci1394_ohci_intr_enable/hci1394_ohci_intr_disable.
 */
void
hci1394_ohci_it_intr_enable(hci1394_ohci_handle_t ohci_hdl,
    uint32_t interrupt_mask)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->it_intr_mask_set, interrupt_mask);
}


/*
 * hci1394_ohci_it_intr_disable()
 *    Disable an individual isoch TX interrupt. This does not affect the general
 *    isoch interrupt mask in the OpenHCI Mask register.  That is enabled/
 *    disabled via hci1394_ohci_intr_enable/hci1394_ohci_intr_disable.
 */
void
hci1394_ohci_it_intr_disable(hci1394_ohci_handle_t ohci_hdl,
    uint32_t interrupt_mask)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->it_intr_mask_clr, interrupt_mask);
}


/*
 * hci1394_ohci_it_intr_clear()
 *    Clear an individual isoch TX interrupt so that it is not asserted anymore.
 *
 * NOTE: we may want to make this a macro at some point.
 */
void
hci1394_ohci_it_intr_clear(hci1394_ohci_handle_t ohci_hdl,
    uint32_t interrupt_mask)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->it_intr_event_clr, interrupt_mask);
}


/*
 * hci1394_ohci_it_ctxt_count_get()
 *    Determine the number of supported isochronous transmit contexts.
 */
int
hci1394_ohci_it_ctxt_count_get(hci1394_ohci_handle_t ohci_hdl)
{
	uint32_t channel_mask;
	int count;

	ASSERT(ohci_hdl != NULL);

	/*
	 * hw is required to support contexts 0 to N, where N <= 31
	 * the interrupt mask bits are wired to ground for unsupported
	 * contexts.  Write 1's to all it mask bits, then read the mask.
	 * Implemented contexts will read (sequentially) as 1
	 */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->it_intr_mask_set, 0xFFFFFFFF);
	channel_mask = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->it_intr_mask_set);
	count = 0;
	while (channel_mask != 0) {
		channel_mask = channel_mask >> 1;
		count++;
	}

	return (count);
}


/*
 * hci1394_ohci_it_cmd_ptr_set()
 *    Set the context pointer for a given isoch TX context.  This is the IO
 *    address for the HW to fetch the first descriptor.  The context should
 *    not be running when this routine is called.
 */
void
hci1394_ohci_it_cmd_ptr_set(hci1394_ohci_handle_t ohci_hdl,
    uint_t context_number, uint32_t io_addr)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->it[context_number].cmd_ptrlo,
	    io_addr);
}


/*
 * hci1394_ohci_ir_intr_asserted()
 *    Return which ENABLED isoch RX interrupts are asserted.  If an interrupt is
 *    disabled via its mask bit, it will not be returned from here.
 *
 * NOTE: we may want to make this a macro at some point.
 */
uint32_t
hci1394_ohci_ir_intr_asserted(hci1394_ohci_handle_t ohci_hdl)
{
	uint32_t interrupts_asserted;

	ASSERT(ohci_hdl != NULL);

	/* Only look at interrupts which are enabled */
	interrupts_asserted = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ir_intr_event_clr);

	return (interrupts_asserted);
}


/*
 * hci1394_ohci_ir_intr_enable()
 *    Enable an individual isoch RX interrupt. This does not affect the isoch
 *    interrupt mask in the OpenHCI Mask register.  That is enabled/disabled
 *    via hci1394_ohci_intr_enable/hci1394_ohci_intr_disable.
 */
void
hci1394_ohci_ir_intr_enable(hci1394_ohci_handle_t ohci_hdl,
    uint32_t interrupt_mask)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ir_intr_mask_set, interrupt_mask);
}


/*
 * hci1394_ohci_ir_intr_disable()
 *    Disable an individual isoch RX interrupt. This does not affect the isoch
 *    interrupt mask in the OpenHCI Mask register.  That is enabled/disabled
 *    via hci1394_ohci_intr_enable/hci1394_ohci_intr_disable.
 */
void
hci1394_ohci_ir_intr_disable(hci1394_ohci_handle_t ohci_hdl,
    uint32_t interrupt_mask)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ir_intr_mask_clr, interrupt_mask);
}


/*
 * hci1394_ohci_ir_intr_clear()
 *    Clear an individual isoch RX interrupt so that it is not asserted anymore.
 *
 * NOTE: we may want to make this a macro at some point.
 */
void
hci1394_ohci_ir_intr_clear(hci1394_ohci_handle_t ohci_hdl,
    uint32_t interrupt_mask)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ir_intr_event_clr, interrupt_mask);
}


/*
 * hci1394_ohci_ir_ctxt_count_get()
 *    Determine the number of supported isochronous receive contexts.
 */
int
hci1394_ohci_ir_ctxt_count_get(hci1394_ohci_handle_t ohci_hdl)
{
	uint32_t channel_mask;
	int count;

	ASSERT(ohci_hdl != NULL);

	/*
	 * hw is required to support contexts 0 to N, where N <= 31
	 * the interrupt mask bits are wired to ground for unsupported
	 * contexts.  Write 1's to all ir mask bits, then read the mask.
	 * Implemented contexts will read (sequentially) as 1
	 */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ir_intr_mask_set, 0xFFFFFFFF);
	channel_mask = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ir_intr_mask_set);
	count = 0;
	while (channel_mask != 0) {
		channel_mask = channel_mask >> 1;
		count++;
	}

	return (count);
}


/*
 * hci1394_ohci_ir_cmd_ptr_set()
 *    Set the context pointer for a given isoch RX context.  This is the IO
 *    address for the HW to fetch the first descriptor.  The context should
 *    not be running when this routine is called.
 */
void
hci1394_ohci_ir_cmd_ptr_set(hci1394_ohci_handle_t ohci_hdl,
    uint_t context_number, uint32_t io_addr)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ir[context_number].cmd_ptrlo,
	    io_addr);
}


/*
 * hci1394_ohci_link_enable()
 *    Enable the 1394 link layer.  When the link is enabled, the PHY will pass
 *    up any 1394 bus transactions which would normally come up to the link.
 */
void
hci1394_ohci_link_enable(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->hc_ctrl_set, OHCI_HC_LINK_ENBL);
}


/*
 * hci1394_ohci_link_disable()
 *    Disable the 1394 link layer.  When the link is disabled, the PHY will NOT
 *    pass up any 1394 bus transactions which would normally come up to the
 *    link.  This "logically" disconnects us from the 1394 bus.
 */
void
hci1394_ohci_link_disable(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->hc_ctrl_clr, OHCI_HC_LINK_ENBL);
}


/*
 * hci1394_ohci_bus_reset()
 *     Reset the 1394 bus. This performs a "long" bus reset and can be called
 *     when the adapter has either a 1394-1995 or 1394A PHY.
 */
int
hci1394_ohci_bus_reset(hci1394_ohci_handle_t ohci_hdl)
{
	int status;
	uint_t reg;


	ASSERT(ohci_hdl != NULL);

	/*
	 * We want to reset the bus.  We also handle the root_holdoff and gap
	 * count cacheing explained at the top of this file.
	 */
	reg = OHCI_PHY_IBR;
	if (ohci_hdl->ohci_set_root_holdoff == B_TRUE) {
		reg = reg | OHCI_PHY_RHB;
	}
	if (ohci_hdl->ohci_set_gap_count == B_TRUE) {
		reg = reg | ohci_hdl->ohci_gap_count;
	} else {
		reg = reg | OHCI_PHY_MAX_GAP;
	}

	/*
	 * Reset the bus. We intentionally do NOT do a PHY read here.  A PHY
	 * read could introduce race conditions and would be more likely to fail
	 * due to a timeout.
	 */
	status = hci1394_ohci_phy_write(ohci_hdl, 0x1, reg);
	if (status != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}

	/* clear the root holdoff and gap count state bits */
	ohci_hdl->ohci_set_root_holdoff = B_FALSE;
	ohci_hdl->ohci_set_gap_count = B_FALSE;

	return (DDI_SUCCESS);
}

/*
 *
 * hci1394_ohci_bus_reset_nroot()
 *     Reset the 1394 bus. This performs a "long" bus reset with out a root.
 */
int
hci1394_ohci_bus_reset_nroot(hci1394_ohci_handle_t ohci_hdl)
{
	int status;
	uint_t reg;

	ASSERT(ohci_hdl != NULL);

	/*
	 * We want to reset the bus.  We don't care about any holdoff
	 * we are suspending need no root...
	 */
	(void) hci1394_ohci_phy_read(ohci_hdl, 0x1, &reg);
	reg = reg | OHCI_PHY_IBR;
	reg = reg & ~OHCI_PHY_RHB;

	/*
	 * Reset the bus. We intentionally do NOT do a PHY read here.  A PHY
	 * read could introduce race conditions and would be more likely to fail
	 * due to a timeout.
	 */
	status = hci1394_ohci_phy_write(ohci_hdl, 0x1, reg);
	if (status != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

/*
 * hci1394_ohci_phy_init()
 *    Setup the PHY.  This should be called during attach and performs any PHY
 *    initialization required including figuring out what kind of PHY we have.
 */
int
hci1394_ohci_phy_init(hci1394_ohci_handle_t ohci_hdl)
{
	int status;
	uint_t phy_reg;


	ASSERT(ohci_hdl != NULL);

	/*
	 * if the phy has extended set to 7, the phy is a not a 1394-1995 PHY.
	 * It could be a 1394a phy or beyond.  The PHY type can be found in PHY
	 * register page 1 in the compliance_level register.
	 *
	 * Since there are not any current standards beyond 1394A, we are going
	 * to consider the PHY to be a 1394A phy if the extended bit is set.
	 *
	 * phy registers are byte wide registers and are addressed as 0, 1, 2,
	 * 3, ...  Phy register 0 may not be read or written.
	 *
	 * Phy register 0x2 (bit 0 MSB, 7 LSB)
	 *   Extended    - bits 0 - 2
	 *   Total Ports - bits 4 - 7
	 */
	status = hci1394_ohci_phy_read(ohci_hdl, 2, &phy_reg);
	if (status != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}

	if ((phy_reg & OHCI_PHY_EXTND_MASK) != OHCI_PHY_EXTND) {
		/*
		 * if the extended bit is not set, we have to be a 1394-1995
		 * PHY
		 */
		ohci_hdl->ohci_phy = H1394_PHY_1995;
	} else {
		/* Treat all other PHY's as a 1394A PHY */
		ohci_hdl->ohci_phy = H1394_PHY_1394A;
	}

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_phy_resume()
 *    re-initialize the PHY. This routine should be called during a resume after
 *    a successful suspend has been done.
 */
/* ARGSUSED */
static int
hci1394_ohci_phy_resume(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);

	/* There is currently nothing to re-initialize here */
	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_phy_set()
 *    Perform bitset operation on PHY register.
 */
int
hci1394_ohci_phy_set(hci1394_ohci_handle_t ohci_hdl, uint_t address,
    uint_t bits)
{
	int status;
	uint_t reg;


	ASSERT(ohci_hdl != NULL);

	mutex_enter(&ohci_hdl->ohci_mutex);

	/* read the PHY register */
	status = hci1394_ohci_phy_read_no_lock(ohci_hdl, address, &reg);
	if (status != DDI_SUCCESS) {
		mutex_exit(&ohci_hdl->ohci_mutex);
		return (DDI_FAILURE);
	}

	/* Set the bits and write the result back */
	reg = reg | bits;
	status = hci1394_ohci_phy_write_no_lock(ohci_hdl, address, reg);
	if (status != DDI_SUCCESS) {
		mutex_exit(&ohci_hdl->ohci_mutex);
		return (DDI_FAILURE);
	}

	mutex_exit(&ohci_hdl->ohci_mutex);

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_phy_clr()
 *    Perform bitclr operation on PHY register.
 */
int
hci1394_ohci_phy_clr(hci1394_ohci_handle_t ohci_hdl, uint_t address,
    uint_t bits)
{
	int status;
	uint_t reg;


	ASSERT(ohci_hdl != NULL);

	mutex_enter(&ohci_hdl->ohci_mutex);

	/* read the PHY register */
	status = hci1394_ohci_phy_read_no_lock(ohci_hdl, address, &reg);
	if (status != DDI_SUCCESS) {
		mutex_exit(&ohci_hdl->ohci_mutex);
		return (DDI_FAILURE);
	}

	/* Set the bits and write the result back */
	reg = reg & ~bits;
	status = hci1394_ohci_phy_write_no_lock(ohci_hdl, address, reg);
	if (status != DDI_SUCCESS) {
		mutex_exit(&ohci_hdl->ohci_mutex);
		return (DDI_FAILURE);
	}

	mutex_exit(&ohci_hdl->ohci_mutex);
	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_phy_read()
 *    Atomic PHY register read
 */
int
hci1394_ohci_phy_read(hci1394_ohci_handle_t ohci_hdl, uint_t address,
    uint_t *data)
{
	int status;

	ASSERT(ohci_hdl != NULL);
	mutex_enter(&ohci_hdl->ohci_mutex);
	status = hci1394_ohci_phy_read_no_lock(ohci_hdl, address, data);
	mutex_exit(&ohci_hdl->ohci_mutex);

	return (status);
}


/*
 * hci1394_ohci_phy_write()
 *    Atomic PHY register write
 */
int
hci1394_ohci_phy_write(hci1394_ohci_handle_t ohci_hdl, uint_t address,
    uint_t data)
{
	int status;

	ASSERT(ohci_hdl != NULL);
	mutex_enter(&ohci_hdl->ohci_mutex);
	status = hci1394_ohci_phy_write_no_lock(ohci_hdl, address, data);
	mutex_exit(&ohci_hdl->ohci_mutex);

	return (status);
}


/*
 * hci1394_ohci_phy_read_no_lock()
 *    This routine actually performs the PHY register read.  It is seperated
 *    out from phy_read so set & clr lock can perform an atomic PHY register
 *    operation.  It assumes the OpenHCI mutex is held.
 */
static int
hci1394_ohci_phy_read_no_lock(hci1394_ohci_handle_t ohci_hdl, uint_t address,
    uint_t *data)
{
	uint32_t ohci_reg;
	int count;


	ASSERT(ohci_hdl != NULL);
	ASSERT(data != NULL);
	ASSERT(MUTEX_HELD(&ohci_hdl->ohci_mutex));

	/* You can't read or write PHY register #0 */
	if (address == 0) {
		return (DDI_FAILURE);
	}

	/* Verify phy access not in progress */
	ohci_reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->phy_ctrl);
	if ((ohci_reg & (OHCI_PHYC_RDREG | OHCI_PHYC_WRREG)) != 0) {
		return (DDI_FAILURE);
	}

	/* Start the PHY register read */
	ohci_reg = OHCI_PHYC_RDREG | ((address & 0xF) <<
	    OHCI_PHYC_REGADDR_SHIFT);
	ddi_put32(ohci_hdl->ohci_reg_handle, &ohci_hdl->ohci_regs->phy_ctrl,
	    ohci_reg);

	/*
	 * The PHY read usually takes less than 1uS.  It is not worth having
	 * this be interrupt driven. Having this be interrupt driven would also
	 * make the bus reset and self id processing much more complex for
	 * 1995 PHY's.  We will wait up to hci1394_phy_delay_uS for the read
	 * to complete (this was initially set to 10).  I have yet to see
	 * count > 1.  The delay is a patchable variable.
	 */
	count = 0;
	while (count < hci1394_phy_delay_uS) {
		/* See if the read is done yet */
		ohci_reg = ddi_get32(ohci_hdl->ohci_reg_handle,
		    &ohci_hdl->ohci_regs->phy_ctrl);
		if ((ohci_reg & OHCI_PHYC_RDDONE) != 0) {
			/*
			 * The read is done. clear the phyRegRecv interrupt. We
			 * do not have this interrupt enabled but this keeps
			 * things clean in case someone in the future does.
			 * Break out of the loop, we are done.
			 */
			ddi_put32(ohci_hdl->ohci_reg_handle,
			    &ohci_hdl->ohci_regs->intr_event_clr,
			    OHCI_INTR_PHY_REG_RCVD);
			break;
		}

		/*
		 * the phy read did not yet complete, wait 1uS, increment the
		 * count and try again.
		 */
		drv_usecwait(1);
		count++;
	}

	/* Check to see if we timed out */
	if (count >= hci1394_phy_delay_uS) {
		/* we timed out, return failure */
		*data = 0;
		return (DDI_FAILURE);
	}

	/* setup the PHY read data to be returned */
	*data = (ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->phy_ctrl) & OHCI_PHYC_RDDATA_MASK) >>
	    OHCI_PHYC_RDDATA_SHIFT;

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_phy_write_no_lock()
 *    This routine actually performs the PHY register write.  It is separated
 *    out from phy_write so set & clr lock can perform an atomic PHY register
 *    operation.  It assumes the OpenHCI mutex is held.
 */
static int
hci1394_ohci_phy_write_no_lock(hci1394_ohci_handle_t ohci_hdl, uint_t address,
    uint_t data)
{
	uint32_t ohci_reg;
	int count;


	ASSERT(ohci_hdl != NULL);
	ASSERT(MUTEX_HELD(&ohci_hdl->ohci_mutex));

	/* You can't read or write PHY register #0 */
	if (address == 0) {
		return (DDI_FAILURE);
	}

	/* Verify phy access not in progress */
	ohci_reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->phy_ctrl);
	if ((ohci_reg & (OHCI_PHYC_RDREG | OHCI_PHYC_WRREG)) != 0) {
		return (DDI_FAILURE);
	}

	/* Start the PHY register write */
	ohci_reg = OHCI_PHYC_WRREG | ((address & 0xF) <<
	    OHCI_PHYC_REGADDR_SHIFT) | (data & OHCI_PHYC_WRDATA_MASK);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->phy_ctrl, ohci_reg);

	/*
	 * The PHY write usually takes less than 1uS.  It is not worth having
	 * this be interrupt driven. Having this be interrupt driven would also
	 * make the bus reset and self id processing much more complex. We will
	 * wait up to hci1394_phy_delay_uS for the write to complete (this was
	 * initially set to 10).  I have yet to see count > 0.  The delay is a
	 * patchable variable.
	 */
	count = 0;
	while (count < hci1394_phy_delay_uS) {
		/* See if the write is done yet */
		ohci_reg = ddi_get32(ohci_hdl->ohci_reg_handle,
		    &ohci_hdl->ohci_regs->phy_ctrl);
		if ((ohci_reg & OHCI_PHYC_WRREG) == 0) {
			/*
			 * The write completed. Break out of the loop, we are
			 * done.
			 */
			break;
		}

		/*
		 * the phy write did not yet complete, wait 1uS, increment the
		 * count and try again.
		 */
		drv_usecwait(1);
		count++;
	}

	/* Check to see if we timed out */
	if (count >= hci1394_phy_delay_uS) {
		/* we timed out, return failure */
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_phy_info()
 *    Return selfid word for our PHY.  This routine should ONLY be called for
 *    adapters with a 1394-1995 PHY. These PHY's do not embed their own selfid
 *    information in the selfid buffer so we need to do it for them in the
 *    selfid complete interrupt handler.  This routine only supports building
 *    selfid info for a 3 port PHY.  Since we will probably not ever see a
 *    1394-1995 PHY in any production system, and if we do it will have 3 ports
 *    or less, this is a pretty safe assumption.
 */
int
hci1394_ohci_phy_info(hci1394_ohci_handle_t ohci_hdl, uint32_t *info)
{
	int status;
	uint32_t phy_info;
	uint32_t reg;
	int index;
	int num_ports;
	int count;
	uint32_t port_status;


	ASSERT(ohci_hdl != NULL);
	ASSERT(info != NULL);
	ASSERT(ohci_hdl->ohci_phy == H1394_PHY_1995);

	/*
	 * Set Link on. We are using power class 0 since we have no idea what
	 * our real power class is.
	 */
	phy_info = 0x80400000;

	/* Add in Physical ID */
	reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->node_id);
	phy_info = phy_info | ((reg << IEEE1394_SELFID_PHYID_SHIFT) &
	    IEEE1394_SELFID_PHYID_MASK);

	/* Add in Gap Count */
	status = hci1394_ohci_phy_read(ohci_hdl, 1, &reg);
	if (status != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}
	phy_info = phy_info | ((reg << IEEE1394_SELFID_GAP_CNT_SHIFT) &
	    IEEE1394_SELFID_GAP_CNT_MASK);

	/* Add in speed & ports */
	status = hci1394_ohci_phy_read(ohci_hdl, 2, &reg);
	if (status != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}
	phy_info = phy_info | ((reg & 0xC0) << 8);
	num_ports = reg & 0x1F;

	/* PHY reports that it has 0 ports?? */
	if (num_ports == 0) {
		return (DDI_FAILURE);
	}

	/* Build up the port information for each port in the PHY */
	count = 0;
	for (index = 0; index < 3; index++) {
		if (num_ports > 0) {
			status = hci1394_ohci_phy_read(ohci_hdl,
			    count + 3, &reg);
			if (status != DDI_SUCCESS) {
				return (DDI_FAILURE);
			}
			/* if port is not connected */
			if ((reg & 0x04) == 0) {
				port_status =
				    IEEE1394_SELFID_PORT_NOT_CONNECTED;

			/* else if port is connected to parent */
			} else if ((reg & 0x08) == 0) {
				port_status = IEEE1394_SELFID_PORT_TO_PARENT;

			/* else port is connected to child */
			} else {
				port_status = IEEE1394_SELFID_PORT_TO_CHILD;
			}

			num_ports--;
		} else {
			port_status = IEEE1394_SELFID_PORT_NO_PORT;
		}

		/* add in the port information */
		phy_info = phy_info | (port_status << (6 - (index * 2)));
		count++;
	}

	/* Copy the PHY selfid info to the return parameter */
	*info = phy_info;

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_current_busgen()
 *    return the current bus generation.
 */
uint_t
hci1394_ohci_current_busgen(hci1394_ohci_handle_t ohci_hdl)
{
	uint32_t reg;
	uint_t generation_count;


	ASSERT(ohci_hdl != NULL);

	reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->self_id_count);
	generation_count = (reg & OHCI_SLFC_GEN_MASK) >> OHCI_SLFC_GEN_SHIFT;

	return (generation_count);
}


/*
 * hci1394_ohci_startup()
 *    Startup the 1394 nexus driver.  This is called after all of the HW has
 *    been initialized (in both attach and resume) and we are ready to
 *    participate on the bus.
 */
int
hci1394_ohci_startup(hci1394_ohci_handle_t ohci_hdl)
{
	int status;


	ASSERT(ohci_hdl != NULL);

	/*
	 * Turn on 1394 link. This allows us to receive 1394 traffic off the
	 * bus
	 */
	hci1394_ohci_link_enable(ohci_hdl);

	/*
	 * Reset the 1394 Bus.
	 * Need to do this so that the link layer can collect all of the self-id
	 * packets.  The Interrupt routine will cause further initialization
	 * after the bus reset has completed
	 */
	status = hci1394_ohci_bus_reset(ohci_hdl);
	if (status != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}

	/* setup out initial interrupt mask and enable interrupts */
	hci1394_isr_mask_setup(ohci_hdl->soft_state);
	hci1394_ohci_intr_master_enable(ohci_hdl);

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_postwr_addr()
 *    Read the Posted Write Address registers.  This should be read when a
 *    posted write error is detected to find out what transaction had an error.
 */
void
hci1394_ohci_postwr_addr(hci1394_ohci_handle_t ohci_hdl, uint64_t *addr)
{
	uint32_t reg;


	ASSERT(ohci_hdl != NULL);
	ASSERT(addr != NULL);

	/* read in the errored address */
	reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->posted_write_addrhi);
	*addr = ((uint64_t)reg) << 32;
	reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->posted_write_addrlo);
	*addr = *addr | (uint64_t)reg;

	/*
	 * Interrupt should be cleared after reading the posted write address.
	 * See 13.2.8.1 in OpenHCI spec v1.0.
	 */
	hci1394_ohci_intr_clear(ohci_hdl, OHCI_INTR_POST_WR_ERR);
}


/*
 * hci1394_ohci_guid()
 *    Return the adapter's GUID
 */
uint64_t
hci1394_ohci_guid(hci1394_ohci_handle_t ohci_hdl)
{
	uint32_t reg;
	uint64_t guid;


	ASSERT(ohci_hdl != NULL);

	reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->guid_hi);
	guid = ((uint64_t)reg) << 32;
	reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->guid_lo);
	guid = guid | (uint64_t)reg;

	return (guid);
}


/*
 * hci1394_ohci_csr_read()
 *    Read one of the HW implemented CSR registers.  These include
 *    bus_manager_id, bandwidth_available, channels_available_hi, and
 *    channels_available_lo. Offset should be set to
 *    OHCI_CSR_SEL_BUS_MGR_ID, OHCI_CSR_SEL_BANDWIDTH_AVAIL
 *    OHCI_CSR_SEL_CHANS_AVAIL_HI, or OHCI_CSR_SEL_CHANS_AVAIL_LO.
 */
int
hci1394_ohci_csr_read(hci1394_ohci_handle_t ohci_hdl, uint_t offset,
    uint32_t *data)
{
	uint_t generation;
	int status;


	ASSERT(ohci_hdl != NULL);
	ASSERT(data != NULL);

	/*
	 * read the CSR register by doing a cswap with the same compare and
	 * swap value.
	 */
	generation = hci1394_ohci_current_busgen(ohci_hdl);
	status = hci1394_ohci_csr_cswap(ohci_hdl, generation, offset, 0, 0,
	    data);
	if (status != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_csr_cswap()
 *    Perform a compare/swap on one of the HW implemented CSR registers. These
 *    include bus_manager_id, bandwidth_available, channels_available_hi, and
 *    channels_available_lo. Offset should be set to
 *    OHCI_CSR_SEL_BUS_MGR_ID, OHCI_CSR_SEL_BANDWIDTH_AVAIL
 *    OHCI_CSR_SEL_CHANS_AVAIL_HI, or OHCI_CSR_SEL_CHANS_AVAIL_LO.
 */
int
hci1394_ohci_csr_cswap(hci1394_ohci_handle_t ohci_hdl, uint_t generation,
    uint_t offset, uint32_t compare, uint32_t swap, uint32_t *old)
{
	int count;
	uint32_t ohci_reg;


	ASSERT(ohci_hdl != NULL);
	ASSERT(old != NULL);

	/*
	 * Make sure we have not gotten a bus reset since this action was
	 * started.
	 */
	if (generation != hci1394_ohci_current_busgen(ohci_hdl)) {
		return (DDI_FAILURE);
	}

	mutex_enter(&ohci_hdl->ohci_mutex);

	/* init csrData and csrCompare */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->csr_data, swap);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->csr_compare_data, compare);

	/* start the compare swap */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->csr_ctrl, offset & OHCI_CSR_SELECT);

	/*
	 * The CSR access should be immediate.  There in nothing that officially
	 * states this so we will wait up to 2uS just in case before we timeout.
	 * We actually perform a compare swap with both compare and swap set
	 * to the same value.  This will return the old value which is in
	 * essence, a read.
	 */
	count = 0;
	while (count < 2) {
		/* See if the compare swap is done */
		ohci_reg = ddi_get32(ohci_hdl->ohci_reg_handle,
		    &ohci_hdl->ohci_regs->csr_ctrl);
		if ((ohci_reg & OHCI_CSR_DONE) != 0) {
			/* The compare swap is done, break out of the loop */
			break;
		}
		/*
		 * The compare swap has not completed yet, wait 1uS, increment
		 * the count and try again
		 */
		drv_usecwait(1);
		count++;
	}

	/* If we timed out, return an error */
	if (count >= 2) {
		*old = 0;
		mutex_exit(&ohci_hdl->ohci_mutex);
		return (DDI_FAILURE);
	}

	/* Copy the old data into the return parameter */
	*old = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->csr_data);

	mutex_exit(&ohci_hdl->ohci_mutex);

	/*
	 * There is a race condition in the OpenHCI design here. After checking
	 * the generation and before performing the cswap, we could get a bus
	 * reset and incorrectly set something like the bus manager.  This would
	 * put us into a condition where we would not have a bus manager and
	 * we would think there was one. If it is possible that this race
	 * condition occured, we will reset the bus to clean things up. We only
	 * care about this if the compare swap was successful.
	 */
	if (generation != hci1394_ohci_current_busgen(ohci_hdl)) {
		if (*old == compare) {
			(void) hci1394_ohci_bus_reset(ohci_hdl);
			return (DDI_FAILURE);
		}
	}

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_contender_enable()
 *    Set the contender bit in the PHY.  This routine should only be called
 *    if our PHY is 1394A compliant. (i.e. this routine should not be called
 *    for a 1394-1995 PHY).
 */
int
hci1394_ohci_contender_enable(hci1394_ohci_handle_t ohci_hdl)
{
	int status;


	ASSERT(ohci_hdl != NULL);

	/*
	 * Make sure that phy is not a 1394-1995 phy. Those phy's do not have a
	 * contender bit to set.
	 */
	if (ohci_hdl->ohci_phy == H1394_PHY_1995) {
		return (DDI_FAILURE);
	}

	/* Set the Contender Bit */
	status = hci1394_ohci_phy_set(ohci_hdl, 0x4, OHCI_PHY_CNTDR);
	if (status != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_root_holdoff_enable()
 *    Set the root holdoff bit in the PHY. Since there are race conditions when
 *    writing to PHY register 1 (which can get updated from a PHY packet off the
 *    bus), we cache this state until a "long" bus reset is issued.
 */
int
hci1394_ohci_root_holdoff_enable(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);

	ohci_hdl->ohci_set_root_holdoff = B_TRUE;

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_gap_count_set()
 *    Set the gap count in the PHY. Since there are race conditions when writing
 *    to PHY register 1 (which can get updated from a PHY packet off the bus),
 *    we cache this gap count until a "long" bus reset is issued.
 */
int
hci1394_ohci_gap_count_set(hci1394_ohci_handle_t ohci_hdl, uint_t gap_count)
{
	ASSERT(ohci_hdl != NULL);

	ohci_hdl->ohci_set_gap_count = B_TRUE;
	ohci_hdl->ohci_gap_count = gap_count & OHCI_PHY_MAX_GAP;

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_phy_filter_set()
 *    Enable a node (or nodes) to perform transactions to our physical
 *    memory. OpenHCI allows you to disable/enable physical requests on a node
 *    per node basis.  A physical request is basically a read/write to 1394
 *    address space 0x0 - 0xFFFFFFFF.  This address goes out to the IO MMU (in
 *    the case of a SPARC machine).  The HAL starts with all nodes unable to
 *    read/write physical memory.  The Services Layer will call down and enable
 *    nodes via setting a physical filter bit for that given node.  Since node
 *    numbers change every bus reset, the services layer has to call down after
 *    every bus reset to re-enable physical accesses. (NOTE: the hardware
 *    automatically clears these bits.
 */
int
hci1394_ohci_phy_filter_set(hci1394_ohci_handle_t ohci_hdl, uint64_t mask,
    uint_t generation)
{
	uint32_t data;


	ASSERT(ohci_hdl != NULL);

	/*
	 * Make sure we have not gotten a bus reset since this action was
	 * started.
	 */
	if (generation != hci1394_ohci_current_busgen(ohci_hdl)) {
		return (DDI_FAILURE);
	}

	data = (uint32_t)((mask >> 32) & 0xFFFFFFFF);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->phys_req_filterhi_set, data);
	data = (uint32_t)(mask & 0xFFFFFFFF);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->phys_req_filterlo_set, data);

	/*
	 * There is a race condition in the OpenHCI design here. After checking
	 * the generation and before setting the physical filter bits, we could
	 * get a bus reset and incorrectly set the physical filter bits.  If it
	 * is possible that this race condition occured, we will reset the bus
	 * to clean things up.
	 */
	if (generation != hci1394_ohci_current_busgen(ohci_hdl)) {
		(void) hci1394_ohci_bus_reset(ohci_hdl);
		return (DDI_SUCCESS);
	}

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_phy_filter_clr()
 *    Disable a node (or nodes) from performing transactions to our physical
 *    memory. See hci1394_ohci_phy_filter_set() above for more info.
 */
int
hci1394_ohci_phy_filter_clr(hci1394_ohci_handle_t ohci_hdl,
    uint64_t mask, uint_t generation)
{
	uint32_t data;


	ASSERT(ohci_hdl != NULL);

	/*
	 * Make sure we have not gotten a bus reset since this action was
	 * started.
	 */
	if (generation != hci1394_ohci_current_busgen(ohci_hdl)) {
		return (DDI_FAILURE);
	}

	data = (uint32_t)((mask >> 32) & 0xFFFFFFFF);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->phys_req_filterhi_clr, data);
	data = (uint32_t)(mask & 0xFFFFFFFF);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->phys_req_filterlo_clr, data);

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_bus_reset_short()
 *    Perform a 1394A short bus reset.  This function should only be called
 *    on an adapter with a 1394A PHY (or later).
 */
int
hci1394_ohci_bus_reset_short(hci1394_ohci_handle_t ohci_hdl)
{
	int status;

	ASSERT(ohci_hdl != NULL);

	/*
	 * Make sure that phy is not a 1394-1995 phy. Those phy's do not have a
	 * contender bit to set.
	 */
	if (ohci_hdl->ohci_phy == H1394_PHY_1995) {
		return (DDI_FAILURE);
	}

	/* Initiate the short bus reset */
	status = hci1394_ohci_phy_set(ohci_hdl, 0x5, OHCI_PHY_ISBR);
	if (status != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}

	return (status);
}


/*
 * hci1394_ohci_cfgrom_update()
 *    Update the config rom with the provided contents.  The config rom is
 *    provided as a byte stream which is multiple of 4 bytes large.  The
 *    size is passed as a quadlet (4 bytes) count.  The entire contents
 *    of the config rom is updated at once.  We do not provide a partial
 *    update interface.
 */
void
hci1394_ohci_cfgrom_update(hci1394_ohci_handle_t ohci_hdl, void *local_buf,
    uint_t quadlet_count)
{
	uint32_t *data;


	ASSERT(ohci_hdl != NULL);
	ASSERT(local_buf != NULL);

	data = (uint32_t *)local_buf;

	/* zero out the config ROM header to start */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->config_rom_hdr, 0);

	/* copy Services Layer buffer into config rom buffer */
	ddi_rep_put8(ohci_hdl->ohci_cfgrom.bi_handle, local_buf,
	    (uint8_t *)ohci_hdl->ohci_cfgrom.bi_kaddr, quadlet_count << 2,
	    DDI_DEV_AUTOINCR);

	(void) ddi_dma_sync(ohci_hdl->ohci_cfgrom.bi_dma_handle, 0,
	    quadlet_count << 2, DDI_DMA_SYNC_FORDEV);

	/*
	 * setup OHCI bus options and config rom hdr registers. We need to swap
	 * the config rom header and bus options on an X86 machine since the
	 * data is provided to us as a byte stream and the OHCI registers expect
	 * a big endian 32-bit number.
	 */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->bus_options, OHCI_SWAP32(data[2]));
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->config_rom_hdr, OHCI_SWAP32(data[0]));
}


/*
 * hci1394_ohci_nodeid_get()
 *    Return our current nodeid (bus #/Node #)
 */
void
hci1394_ohci_nodeid_get(hci1394_ohci_handle_t ohci_hdl, uint_t *nodeid)
{
	uint32_t reg;

	ASSERT(ohci_hdl != NULL);
	ASSERT(nodeid != NULL);
	reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->node_id);
	*nodeid = (reg & 0xFFFF) << 16;
}


/*
 * hci1394_ohci_nodeid_set()
 *    Set our current nodeid (bus #/Node #).  This actually sets our bus number.
 *    Our node number cannot be set by software.  This is usually trigered via
 *    a write to the CSR NODEIDS register.
 */
void
hci1394_ohci_nodeid_set(hci1394_ohci_handle_t ohci_hdl, uint_t nodeid)
{
	uint32_t reg;

	ASSERT(ohci_hdl != NULL);

	reg = ((nodeid & 0xFFC00000) >> 16);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->node_id, reg);
}


/*
 * hci1394_ohci_nodeid_info()
 *    Return our current nodeid (bus #/Node #).  This also returns whether or
 *    not our nodeid error bit is set.  This is useful in determining if the
 *    bus reset completed without errors in the selfid complete interrupt
 *    processing.
 */
void
hci1394_ohci_nodeid_info(hci1394_ohci_handle_t ohci_hdl, uint_t *nodeid,
    boolean_t *error)
{
	uint32_t reg;

	ASSERT(ohci_hdl != NULL);
	ASSERT(nodeid != NULL);

	reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->node_id);
	*nodeid = reg & 0xFFFF;
	if ((reg & OHCI_NDID_IDVALID) == 0) {
		*error = B_TRUE;
	} else {
		*error = B_FALSE;
	}
}


/*
 * hci1394_ohci_cycletime_get()
 *    Return the current cycle time
 */
void
hci1394_ohci_cycletime_get(hci1394_ohci_handle_t ohci_hdl,
    uint32_t *cycle_time)
{
	ASSERT(ohci_hdl != NULL);
	ASSERT(cycle_time != NULL);
	*cycle_time = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->isoch_cycle_timer);
}


/*
 * hci1394_ohci_cycletime_get()
 *    Set the cycle time
 */
void
hci1394_ohci_cycletime_set(hci1394_ohci_handle_t ohci_hdl,
    uint32_t cycle_time)
{
	ASSERT(ohci_hdl != NULL);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->isoch_cycle_timer, cycle_time);
}


/*
 * hci1394_ohci_bustime_get()
 *    Return the current bus time.
 */
void
hci1394_ohci_bustime_get(hci1394_ohci_handle_t ohci_hdl, uint32_t *bus_time)
{
	uint32_t bus_time1;
	uint32_t bus_time2;
	uint32_t cycle_time;


	ASSERT(ohci_hdl != NULL);
	ASSERT(bus_time != NULL);

	/*
	 * The bus time is composed of a portion of the cycle time and the
	 * cycle time rollover count (ohci_bustime_count). There is a race
	 * condition where we read the rollover count and then the cycle
	 * timer rolls over.  This is the reason for the double read of the
	 * rollover count.
	 */
	do {
		bus_time1 = ohci_hdl->ohci_bustime_count;
		cycle_time = ddi_get32(ohci_hdl->ohci_reg_handle,
		    &ohci_hdl->ohci_regs->isoch_cycle_timer);
		bus_time2 = ohci_hdl->ohci_bustime_count;
	} while (bus_time1 != bus_time2);

	*bus_time = (bus_time2 << 7) | (cycle_time >> 25);
}


/*
 * hci1394_ohci_bustime_set()
 *    Set the cycle timer rollover portion of the bus time.
 */
void
hci1394_ohci_bustime_set(hci1394_ohci_handle_t ohci_hdl, uint32_t bus_time)
{
	ASSERT(ohci_hdl != NULL);

	/*
	 * we will start with the cycle 64 seconds interrupt disabled. If this
	 * is the first write to bus time, enable the interrupt.
	 */
	if (ohci_hdl->ohci_bustime_enabled == B_FALSE) {
		ohci_hdl->ohci_bustime_enabled = B_TRUE;
		/* Clear the cycle64Seconds interrupt then enable it */
		hci1394_ohci_intr_clear(ohci_hdl, OHCI_INTR_CYC_64_SECS);
		hci1394_ohci_intr_enable(ohci_hdl, OHCI_INTR_CYC_64_SECS);
	}
	ohci_hdl->ohci_bustime_count = (bus_time >> 7);
}


/*
 * hci1394_ohci_atreq_retries_get()
 *    Get the number of atreq retries we will perform.
 */
void
hci1394_ohci_atreq_retries_get(hci1394_ohci_handle_t ohci_hdl,
    uint_t *atreq_retries)
{
	uint32_t reg;

	ASSERT(ohci_hdl != NULL);
	ASSERT(atreq_retries != NULL);
	reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->at_retries);
	*atreq_retries = reg & OHCI_RET_MAX_ATREQ_MASK;
}


/*
 * hci1394_ohci_atreq_retries_get()
 *    Set the number of atreq retries we will perform.
 */
void
hci1394_ohci_atreq_retries_set(hci1394_ohci_handle_t ohci_hdl,
    uint_t atreq_retries)
{
	uint32_t reg;


	ASSERT(ohci_hdl != NULL);

	mutex_enter(&ohci_hdl->ohci_mutex);
	reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->at_retries);
	reg = reg & ~OHCI_RET_MAX_ATREQ_MASK;
	reg = reg | (atreq_retries & OHCI_RET_MAX_ATREQ_MASK);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->at_retries, reg);
	mutex_exit(&ohci_hdl->ohci_mutex);
}


/*
 * hci1394_ohci_isr_cycle64seconds()
 *    Interrupt handler for the cycle64seconds interrupt.
 */
void
hci1394_ohci_isr_cycle64seconds(hci1394_ohci_handle_t ohci_hdl)
{
	uint32_t cycle_time;

	ASSERT(ohci_hdl != NULL);

	hci1394_ohci_intr_clear(ohci_hdl, OHCI_INTR_CYC_64_SECS);
	cycle_time = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->isoch_cycle_timer);

	/*
	 * cycle64second interrupts when the MSBit in the cycle timer changes
	 * state.  We only care about rollover so we will increment only when
	 * the MSBit is set to 0.
	 */
	if ((cycle_time & 0x80000000) == 0) {
		ohci_hdl->ohci_bustime_count++;
	}
}


/*
 * hci1394_ohci_isr_phy()
 *    Interrupt handler for a PHY event
 */
void
hci1394_ohci_isr_phy(hci1394_ohci_handle_t ohci_hdl)
{
	uint_t phy_status;
	int status;


	ASSERT(ohci_hdl != NULL);

	/* clear the interrupt */
	hci1394_ohci_intr_clear(ohci_hdl, OHCI_INTR_PHY);

	/* increment the statistics count */
	ohci_hdl->ohci_drvinfo->di_stats.st_phy_isr++;

	/*
	 * If the PHY is a 1995 phy, just return since there are no status bits
	 * to read.
	 */
	if (ohci_hdl->ohci_phy == H1394_PHY_1995) {
		return;
	}

	/* See why we got this interrupt */
	status = hci1394_ohci_phy_read(ohci_hdl, 5, &phy_status);
	if (status != DDI_SUCCESS) {
		return;
	}

	if (phy_status & OHCI_PHY_LOOP_ERR) {
		ohci_hdl->ohci_drvinfo->di_stats.st_phy_loop_err++;
		cmn_err(CE_NOTE, "hci1394(%d): ERROR - bus loop detected",
		    ohci_hdl->ohci_drvinfo->di_instance);
	}
	if (phy_status & OHCI_PHY_PWRFAIL_ERR) {
		ohci_hdl->ohci_drvinfo->di_stats.st_phy_pwrfail_err++;
	}
	if (phy_status & OHCI_PHY_TIMEOUT_ERR) {
		ohci_hdl->ohci_drvinfo->di_stats.st_phy_timeout_err++;
	}
	if (phy_status & OHCI_PHY_PORTEVT_ERR) {
		ohci_hdl->ohci_drvinfo->di_stats.st_phy_portevt_err++;
	}

	/* clear any set status bits */
	status = hci1394_ohci_phy_write(ohci_hdl, 5, phy_status);
	if (status != DDI_SUCCESS) {
		return;
	}

	/*
	 * Disable the PHY interrupt. We are getting stuck in this ISR in
	 * certain PHY implementations so we will disable the interrupt until
	 * we see a selfid complete.
	 */
	hci1394_ohci_intr_disable(ohci_hdl, OHCI_INTR_PHY);
}


/*
 * hci1394_ohci_root_check
 *    Returns status about if we are currently the root node on the 1394 bus.
 *    returns B_TRUE if we are the root,  B_FALSE if we are not the root.
 */
boolean_t
hci1394_ohci_root_check(hci1394_ohci_handle_t ohci_hdl)
{
	uint32_t reg;
	int status;

	ASSERT(ohci_hdl != NULL);
	reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->node_id);
	if ((reg & OHCI_REG_NODEID_ROOT) && (reg & OHCI_NDID_IDVALID)) {
		status = B_TRUE;
	} else {
		status = B_FALSE;
	}

	return (status);
}


/*
 * hci1394_ohci_cmc_check()
 *    Returns status about if we are cycle master capable. Returns
 *    B_TRUE if we are the cycle master capable, B_FALSE if we are not the cycle
 *    master capable.
 */
boolean_t
hci1394_ohci_cmc_check(hci1394_ohci_handle_t ohci_hdl)
{
	uint32_t reg;
	int status;

	ASSERT(ohci_hdl != NULL);
	reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->bus_options);
	if (reg & OHCI_REG_BUSOPTIONS_CMC) {
		status = B_TRUE;
	} else {
		status = B_FALSE;
	}

	return (status);
}


/*
 * hci1394_ohci_cycle_master_enable()
 *    Enables us to be cycle master.  If we are root, we will start generating
 *    cycle start packets.
 */
void
hci1394_ohci_cycle_master_enable(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);

	/* First make sure that cycleTooLong is clear */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->intr_event_clr, OHCI_INTR_CYC_TOO_LONG);

	/* Enable Cycle Master */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->link_ctrl_set, OHCI_LC_CYC_MAST);
}


/*
 * hci1394_ohci_cycle_master_disable()
 *    Disabled us from being cycle master. If we are root, we will stop
 *    generating cycle start packets.
 */
void
hci1394_ohci_cycle_master_disable(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);

	/* disable cycle master */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->link_ctrl_clr, OHCI_LC_CYC_MAST);
}


/*
 * hci1394_ohci_resume()
 *    Re-initialize the openHCI HW during a resume. (after a power suspend)
 */
int
hci1394_ohci_resume(hci1394_ohci_handle_t ohci_hdl)
{
	uint32_t quadlet;
	int status;


	ASSERT(ohci_hdl != NULL);

	/* Re-initialize the OpenHCI chip */
	status = hci1394_ohci_chip_init(ohci_hdl);
	if (status != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}

	/* Re-initialize the PHY */
	status = hci1394_ohci_phy_resume(ohci_hdl);
	if (status != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}

	/* Re-initialize any 1394A features we are using */
	status = hci1394_ohci_1394a_resume(ohci_hdl);
	if (status != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}

	/* Tell OpenHCI where the Config ROM buffer is */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->config_rom_maplo,
	    (uint32_t)ohci_hdl->ohci_cfgrom.bi_cookie.dmac_address);

	/* Tell OpenHCI where the SelfId buffer is */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->self_id_buflo,
	    (uint32_t)ohci_hdl->ohci_selfid.bi_cookie.dmac_address);

	/* Enable selfid DMA engine */
	hci1394_ohci_selfid_enable(ohci_hdl);

	/*
	 * re-setup OHCI bus options and config rom hdr registers. We need to
	 * read from the config rom using ddi_rep_get8 since it is stored as
	 * a byte stream. We need to swap the config rom header and bus options
	 * on an X86 machine since the data is a byte stream and the OHCI
	 *  registers expect a big endian 32-bit number.
	 */
	ddi_rep_get8(ohci_hdl->ohci_cfgrom.bi_handle, (uint8_t *)&quadlet,
	    &((uint8_t *)ohci_hdl->ohci_cfgrom.bi_kaddr)[8], 4,
	    DDI_DEV_AUTOINCR);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->bus_options, OHCI_SWAP32(quadlet));
	ddi_rep_get8(ohci_hdl->ohci_cfgrom.bi_handle, (uint8_t *)&quadlet,
	    &((uint8_t *)ohci_hdl->ohci_cfgrom.bi_kaddr)[0], 4,
	    DDI_DEV_AUTOINCR);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->config_rom_hdr, OHCI_SWAP32(quadlet));

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_selfid_init()
 *    Initialize the selfid buffer
 */
static int
hci1394_ohci_selfid_init(hci1394_ohci_handle_t ohci_hdl)
{
	hci1394_buf_parms_t parms;
	int status;


	ASSERT(ohci_hdl != NULL);

	/*
	 * Setup for 2K buffer, aligned on a 2Kbyte address boundary. Make sure
	 * that the buffer is not broken up into multiple cookies.  OpenHCI can
	 * only handle one address for the selfid buffer location.
	 */
	parms.bp_length = 2048;
	parms.bp_max_cookies = 1;
	parms.bp_alignment = 2048;
	status = hci1394_buf_alloc(ohci_hdl->ohci_drvinfo, &parms,
	    &ohci_hdl->ohci_selfid, &ohci_hdl->ohci_selfid_handle);
	if (status != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}

	/* Tell OpenHCI where the buffer is */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->self_id_buflo,
	    (uint32_t)ohci_hdl->ohci_selfid.bi_cookie.dmac_address);

	/* Enable selfid DMA engine */
	hci1394_ohci_selfid_enable(ohci_hdl);

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_selfid_enable()
 *    Allow selfid packets to be placed into the selfid buffer.  This should be
 *    called after the selfid buffer address has been setup in the HW.
 */
void
hci1394_ohci_selfid_enable(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);

	/*
	 * Allow selfid packets to be received.  This should be called during
	 * driver attach after the selfid buffer address has been initialized.
	 *
	 * Link Control Register
	 *   rscSelfId = 1 <= bit 9
	 */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->link_ctrl_set, OHCI_LC_RCV_SELF);
}


/*
 * hci1394_ohci_selfid_read()
 *    Read a word out of the selfid buffer.
 */
void
hci1394_ohci_selfid_read(hci1394_ohci_handle_t ohci_hdl, uint_t offset,
    uint32_t *data)
{
	ASSERT(ohci_hdl != NULL);
	ASSERT(data != NULL);
	*data = ddi_get32(ohci_hdl->ohci_selfid.bi_handle,
	    &((uint32_t *)ohci_hdl->ohci_selfid.bi_kaddr)[offset]);
}


/*
 * hci1394_ohci_selfid_info()
 *    Return the current bus generation, the number of bytes currently in the
 *    selfid buffer, and if we have seen any selfid errors.
 */
void
hci1394_ohci_selfid_info(hci1394_ohci_handle_t ohci_hdl, uint_t *busgen,
    uint_t *size, boolean_t *error)
{
	uint32_t reg;


	ASSERT(ohci_hdl != NULL);
	ASSERT(busgen != NULL);
	ASSERT(size != NULL);
	ASSERT(error != NULL);

	reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->self_id_count);
	*busgen = (reg & OHCI_SLFC_GEN_MASK) >> OHCI_SLFC_GEN_SHIFT;
	*size = reg & OHCI_SLFC_NUM_QUADS_MASK;
	if ((reg & OHCI_SLFC_ERROR) == 0) {
		*error = B_FALSE;
	} else {
		*error = B_TRUE;
	}
}


/*
 * hci1394_ohci_selfid_buf_current()
 *    Test if the selfid buffer is current.  Return B_TRUE if it is current and
 *    B_FALSE if it is not current.
 */
boolean_t
hci1394_ohci_selfid_buf_current(hci1394_ohci_handle_t ohci_hdl)
{
	uint32_t reg;
	int status;

	ASSERT(ohci_hdl != NULL);

	/*
	 * if the generation stored in the selfid buffer is not equal to the
	 * generation we have previously stored, the selfid buffer is not
	 * current. (It maybe older or it maybe newer)
	 */
	reg = ddi_get32(ohci_hdl->ohci_selfid.bi_handle,
	    &((uint32_t *)ohci_hdl->ohci_selfid.bi_kaddr)[0]);
	if (ohci_hdl->ohci_drvinfo->di_gencnt != ((reg & OHCI_SLFC_GEN_MASK) >>
	    OHCI_SLFC_GEN_SHIFT)) {
		status = B_FALSE;
	} else {
		status = B_TRUE;
	}

	return (status);
}


/*
 * hci1394_ohci_selfid_sync()
 *    Perform a ddi_dma_sync on the selfid buffer
 */
void
hci1394_ohci_selfid_sync(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);
	(void) ddi_dma_sync(ohci_hdl->ohci_selfid.bi_dma_handle, 0,
	    ohci_hdl->ohci_selfid.bi_length, DDI_DMA_SYNC_FORKERNEL);
}


/*
 * hci1394_ohci_cfgrom_init()
 *    Initialize the configuration ROM buffer
 */
static int
hci1394_ohci_cfgrom_init(hci1394_ohci_handle_t ohci_hdl)
{
	hci1394_buf_parms_t parms;
	int status;


	ASSERT(ohci_hdl != NULL);

	/*
	 * Setup for 1K buffer, aligned at 1K address boundary, and allow no
	 * less than 4 byte data transfers. Create the Buffer.  Make sure that
	 * the buffer is not broken up into multiple cookies.  OpenHCI can only
	 * handle one address for the config ROM buffer location.
	 */
	parms.bp_length = 1024;
	parms.bp_max_cookies = 1;
	parms.bp_alignment = 1024;
	status = hci1394_buf_alloc(ohci_hdl->ohci_drvinfo, &parms,
	    &ohci_hdl->ohci_cfgrom, &ohci_hdl->ohci_cfgrom_handle);
	if (status != DDI_SUCCESS) {
		return (DDI_FAILURE);
	}

	/* Tell OpenHCI where the buffer is */
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->config_rom_maplo,
	    (uint32_t)ohci_hdl->ohci_cfgrom.bi_cookie.dmac_address);

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_bus_capabilities()
 *    Return our current bus capabilities
 */
void
hci1394_ohci_bus_capabilities(hci1394_ohci_handle_t ohci_hdl,
    uint32_t *bus_capabilities)
{
	ASSERT(ohci_hdl != NULL);

	/*
	 * read in the bus options register.  Set bits saying that we are isoch
	 * resource manager capable, Cycle master capable, and Isoch capable
	 */
	*bus_capabilities = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->bus_options) | (OHCI_BOPT_IRMC |
	    OHCI_BOPT_CMC | OHCI_BOPT_ISC);
}


/*
 * hci1394_ohci_at_active()
 *    Returns status one if either of the AT engines are active.  If either AT
 *    engine is active, we return B_TRUE.  If both AT engines are not active, we
 *    return B_FALSE.
 */
boolean_t
hci1394_ohci_at_active(hci1394_ohci_handle_t ohci_hdl)
{
	uint32_t reg;


	ASSERT(ohci_hdl != NULL);

	/* see if atreq active bit set */
	reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->at_req.ctxt_ctrl_set);
	if (reg & OHCI_CC_ACTIVE_MASK) {
		/* atreq engine is still active */
		return (B_TRUE);
	}

	/* see if atresp active bit set */
	reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->at_resp.ctxt_ctrl_set);
	if (reg & OHCI_CC_ACTIVE_MASK) {
		/* atresp engine is still active */
		return (B_TRUE);
	}

	/* both atreq and atresp active bits are cleared */
	return (B_FALSE);
}


/*
 * hci1394_ohci_atreq_start()
 *    Start the atreq dma engine.  Set the address of the first descriptor
 *    to read in equal to cmdptr.
 */
void
hci1394_ohci_atreq_start(hci1394_ohci_handle_t ohci_hdl, uint32_t cmdptr)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->at_req.cmd_ptrlo, cmdptr);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->at_req.ctxt_ctrl_set, OHCI_CC_RUN_MASK);
}


/*
 * hci1394_ohci_atreq_wake()
 *    Wake up the atreq dma engine.  This should be called when a new descriptor
 *    is added to the Q and the dma engine has already be started.  It it OK to
 *    call this when the DMA engine is active.
 */
void
hci1394_ohci_atreq_wake(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->at_req.ctxt_ctrl_set, OHCI_CC_WAKE_MASK);
}


/*
 * hci1394_ohci_atreq_stop()
 *    Stop the atreq dma engine.  No further descriptors will be read until
 *    it dma engine is started again.
 */
void
hci1394_ohci_atreq_stop(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->at_req.ctxt_ctrl_clr, OHCI_CC_RUN_MASK);
}


/*
 * hci1394_ohci_arresp_start()
 *    Start the arresp dma engine.  Set the address of the first descriptor
 *    to read in equal to cmdptr.
 */
void
hci1394_ohci_arresp_start(hci1394_ohci_handle_t ohci_hdl, uint32_t cmdptr)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ar_resp.cmd_ptrlo, cmdptr);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ar_resp.ctxt_ctrl_set, OHCI_CC_RUN_MASK);
}


/*
 * hci1394_ohci_arresp_wake()
 *    Wake up the arresp dma engine.  This should be called when a new
 *    descriptor is added to the Q and the dma engine has already be started.
 *    It is OK to call this when the DMA engine is active.
 */
void
hci1394_ohci_arresp_wake(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ar_resp.ctxt_ctrl_set, OHCI_CC_WAKE_MASK);
}


/*
 * hci1394_ohci_atreq_stop()
 *    Stop the arresp dma engine.  No further data will be received after any
 *    current packets being received have finished.
 */
void
hci1394_ohci_arresp_stop(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ar_resp.ctxt_ctrl_clr, OHCI_CC_RUN_MASK);
}


/*
 * hci1394_ohci_arreq_start()
 *    Start the arreq dma engine.  Set the address of the first descriptor
 *    to read in equal to cmdptr.
 */
void
hci1394_ohci_arreq_start(hci1394_ohci_handle_t ohci_hdl, uint32_t cmdptr)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ar_req.cmd_ptrlo, cmdptr);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ar_req.ctxt_ctrl_set, OHCI_CC_RUN_MASK);
}


/*
 * hci1394_ohci_arreq_wake()
 *    Wake up the arreq dma engine.  This should be called when a new descriptor
 *    is added to the Q and the dma engine has already be started.  It is OK to
 *    call this when the DMA engine is active.
 */
void
hci1394_ohci_arreq_wake(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ar_req.ctxt_ctrl_set, OHCI_CC_WAKE_MASK);
}


/*
 * hci1394_ohci_arreq_stop()
 *    Stop the arreq dma engine.  No further data will be received after any
 *    current packets being received have finished.
 */
void
hci1394_ohci_arreq_stop(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->ar_req.ctxt_ctrl_clr, OHCI_CC_RUN_MASK);
}


/*
 * hci1394_ohci_atresp_start()
 *    Start the atresp dma engine.  Set the address of the first descriptor
 *    to read in equal to cmdptr.
 */
void
hci1394_ohci_atresp_start(hci1394_ohci_handle_t ohci_hdl, uint32_t cmdptr)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->at_resp.cmd_ptrlo, cmdptr);
	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->at_resp.ctxt_ctrl_set, OHCI_CC_RUN_MASK);
}


/*
 * hci1394_ohci_atresp_wake()
 *    Wake up the atresp dma engine.  This should be called when a new
 *    descriptor is added to the Q and the dma engine has already be started.
 *    It is OK to call this when the DMA engine is active.
 */
void
hci1394_ohci_atresp_wake(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->at_resp.ctxt_ctrl_set, OHCI_CC_WAKE_MASK);
}


/*
 * hci1394_ohci_atresp_stop()
 *    Stop the atresp dma engine.  No further descriptors will be read until
 *    it dma engine is started again.
 */
void
hci1394_ohci_atresp_stop(hci1394_ohci_handle_t ohci_hdl)
{
	ASSERT(ohci_hdl != NULL);

	ddi_put32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->at_resp.ctxt_ctrl_clr, OHCI_CC_RUN_MASK);
}


/*
 * hci1394_ohci_1394a_init()
 *    Initialize any 1394a features that we are using.
 */
/* ARGSUSED */
int
hci1394_ohci_1394a_init(hci1394_ohci_handle_t ohci_hdl)
{
	uint32_t reg;
	int status;

	ASSERT(ohci_hdl != NULL);

	reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->hc_ctrl_set);
	if (reg & OHCI_HC_PROG_PHY_ENBL) {
		ddi_put32(ohci_hdl->ohci_reg_handle,
		    &ohci_hdl->ohci_regs->hc_ctrl_set, OHCI_HC_APHY_ENBL);
		status = hci1394_ohci_phy_set(ohci_hdl, 5,
		    (OHCI_PHY_ENBL_ACCEL | OHCI_PHY_ENBL_MULTI));
		if (status != DDI_SUCCESS) {
			return (DDI_FAILURE);
		}
	}

	return (DDI_SUCCESS);
}


/*
 * hci1394_ohci_1394a_init()
 *    Re-initialize any 1394a features that we are using.
 */
/* ARGSUSED */
int
hci1394_ohci_1394a_resume(hci1394_ohci_handle_t ohci_hdl)
{
	uint32_t reg;
	int status;

	ASSERT(ohci_hdl != NULL);

	reg = ddi_get32(ohci_hdl->ohci_reg_handle,
	    &ohci_hdl->ohci_regs->hc_ctrl_set);
	if (reg & OHCI_HC_PROG_PHY_ENBL) {
		ddi_put32(ohci_hdl->ohci_reg_handle,
		    &ohci_hdl->ohci_regs->hc_ctrl_set, OHCI_HC_APHY_ENBL);
		status = hci1394_ohci_phy_set(ohci_hdl, 5,
		    (OHCI_PHY_ENBL_ACCEL | OHCI_PHY_ENBL_MULTI));
		if (status != DDI_SUCCESS) {
			return (DDI_FAILURE);
		}
	}

	return (DDI_SUCCESS);
}