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
|
/* $NetBSD: atppc.c,v 1.41 2022/11/01 19:45:35 andvar Exp $ */
/*
* Copyright (c) 2001 Alcove - Nicolas Souchu
* Copyright (c) 2003, 2004 Gary Thorpe <gathorpe@users.sourceforge.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* FreeBSD: src/sys/isa/ppc.c,v 1.26.2.5 2001/10/02 05:21:45 nsouch Exp
*
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: atppc.c,v 1.41 2022/11/01 19:45:35 andvar Exp $");
#include "opt_atppc.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/kmem.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/vnode.h>
#include <sys/syslog.h>
#include <sys/bus.h>
/*#include <sys/intr.h>*/
#include <dev/isa/isareg.h>
#include <dev/ic/atppcreg.h>
#include <dev/ic/atppcvar.h>
#include <dev/ppbus/ppbus_conf.h>
#include <dev/ppbus/ppbus_msq.h>
#include <dev/ppbus/ppbus_io.h>
#include <dev/ppbus/ppbus_var.h>
#ifdef ATPPC_DEBUG
int atppc_debug = 1;
#endif
#ifdef ATPPC_VERBOSE
int atppc_verbose = 1;
#endif
/* List of supported chipsets detection routines */
static int (*chipset_detect[])(struct atppc_softc *) = {
/* XXX Add these LATER: maybe as separate devices?
atppc_pc873xx_detect,
atppc_smc37c66xgt_detect,
atppc_w83877f_detect,
atppc_smc37c935_detect,
*/
NULL
};
/* Prototypes for functions. */
/* Print function for config_found() */
static int atppc_print(void *, const char *);
/* Detection routines */
static int atppc_detect_fifo(struct atppc_softc *);
static int atppc_detect_chipset(struct atppc_softc *);
static int atppc_detect_generic(struct atppc_softc *);
/* Routines for ppbus interface (bus + device) */
static int atppc_read(device_t, char *, int, int, size_t *);
static int atppc_write(device_t, char *, int, int, size_t *);
static int atppc_setmode(device_t, int);
static int atppc_getmode(device_t);
static int atppc_check_epp_timeout(device_t);
static void atppc_reset_epp_timeout(device_t);
static void atppc_ecp_sync(device_t);
static int atppc_exec_microseq(device_t, struct ppbus_microseq * *);
static u_int8_t atppc_io(device_t, int, u_char *, int, u_char);
static int atppc_read_ivar(device_t, int, unsigned int *);
static int atppc_write_ivar(device_t, int, unsigned int *);
static int atppc_add_handler(device_t, void (*)(void *), void *);
static int atppc_remove_handler(device_t, void (*)(void *));
/* Utility functions */
/* Functions to read bytes into device's input buffer */
static void atppc_nibble_read(struct atppc_softc * const);
static void atppc_byte_read(struct atppc_softc * const);
static void atppc_epp_read(struct atppc_softc * const);
static void atppc_ecp_read(struct atppc_softc * const);
static void atppc_ecp_read_dma(struct atppc_softc *, unsigned int *,
unsigned char);
static void atppc_ecp_read_pio(struct atppc_softc *, unsigned int *,
unsigned char);
static void atppc_ecp_read_error(struct atppc_softc *);
/* Functions to write bytes to device's output buffer */
static void atppc_std_write(struct atppc_softc * const);
static void atppc_epp_write(struct atppc_softc * const);
static void atppc_fifo_write(struct atppc_softc * const);
static void atppc_fifo_write_dma(struct atppc_softc * const, unsigned char,
unsigned char);
static void atppc_fifo_write_pio(struct atppc_softc * const, unsigned char,
unsigned char);
static void atppc_fifo_write_error(struct atppc_softc * const,
const unsigned int);
/* Miscellaneous */
static int atppc_poll_str(const struct atppc_softc * const, const u_int8_t,
const u_int8_t);
static int atppc_wait_interrupt(struct atppc_softc * const, kcondvar_t *,
const u_int8_t);
/*
* Generic attach and detach functions for atppc device. If sc_dev_ok in soft
* configuration data is not ATPPC_ATTACHED, these should be skipped altogether.
*/
/* Soft configuration attach for atppc */
void
atppc_sc_attach(struct atppc_softc *lsc)
{
/* Adapter used to configure ppbus device */
struct parport_adapter sc_parport_adapter;
char buf[64];
mutex_init(&lsc->sc_lock, MUTEX_DEFAULT, IPL_TTY);
cv_init(&lsc->sc_out_cv, "atppcout");
cv_init(&lsc->sc_in_cv, "atppcin");
/* Probe and set up chipset */
if (atppc_detect_chipset(lsc) != 0) {
if (atppc_detect_generic(lsc) != 0) {
ATPPC_DPRINTF(("%s: Error detecting chipset\n",
device_xname(lsc->sc_dev)));
}
}
/* Probe and setup FIFO queue */
if (atppc_detect_fifo(lsc) == 0) {
printf("%s: FIFO <depth,wthr,rthr>=<%d,%d,%d>\n",
device_xname(lsc->sc_dev), lsc->sc_fifo, lsc->sc_wthr,
lsc->sc_rthr);
}
/* Print out chipset capabilities */
snprintb(buf, sizeof(buf), "\20\1INTR\2DMA\3FIFO\4PS2\5ECP\6EPP",
lsc->sc_has);
printf("%s: capabilities=%s\n", device_xname(lsc->sc_dev), buf);
/* Initialize device's buffer pointers */
lsc->sc_outb = lsc->sc_outbstart = lsc->sc_inb = lsc->sc_inbstart
= NULL;
lsc->sc_inb_nbytes = lsc->sc_outb_nbytes = 0;
/* Last configuration step: set mode to standard mode */
if (atppc_setmode(lsc->sc_dev, PPBUS_COMPATIBLE) != 0) {
ATPPC_DPRINTF(("%s: unable to initialize mode.\n",
device_xname(lsc->sc_dev)));
}
/* Set up parport_adapter structure */
/* Set capabilities */
sc_parport_adapter.capabilities = 0;
if (lsc->sc_has & ATPPC_HAS_INTR) {
sc_parport_adapter.capabilities |= PPBUS_HAS_INTR;
}
if (lsc->sc_has & ATPPC_HAS_DMA) {
sc_parport_adapter.capabilities |= PPBUS_HAS_DMA;
}
if (lsc->sc_has & ATPPC_HAS_FIFO) {
sc_parport_adapter.capabilities |= PPBUS_HAS_FIFO;
}
if (lsc->sc_has & ATPPC_HAS_PS2) {
sc_parport_adapter.capabilities |= PPBUS_HAS_PS2;
}
if (lsc->sc_has & ATPPC_HAS_EPP) {
sc_parport_adapter.capabilities |= PPBUS_HAS_EPP;
}
if (lsc->sc_has & ATPPC_HAS_ECP) {
sc_parport_adapter.capabilities |= PPBUS_HAS_ECP;
}
/* Set function pointers */
sc_parport_adapter.parport_io = atppc_io;
sc_parport_adapter.parport_exec_microseq = atppc_exec_microseq;
sc_parport_adapter.parport_reset_epp_timeout =
atppc_reset_epp_timeout;
sc_parport_adapter.parport_setmode = atppc_setmode;
sc_parport_adapter.parport_getmode = atppc_getmode;
sc_parport_adapter.parport_ecp_sync = atppc_ecp_sync;
sc_parport_adapter.parport_read = atppc_read;
sc_parport_adapter.parport_write = atppc_write;
sc_parport_adapter.parport_read_ivar = atppc_read_ivar;
sc_parport_adapter.parport_write_ivar = atppc_write_ivar;
sc_parport_adapter.parport_dma_malloc = lsc->sc_dma_malloc;
sc_parport_adapter.parport_dma_free = lsc->sc_dma_free;
sc_parport_adapter.parport_add_handler = atppc_add_handler;
sc_parport_adapter.parport_remove_handler = atppc_remove_handler;
/* Initialize handler list, may be added to by grandchildren */
SLIST_INIT(&(lsc->sc_handler_listhead));
/* Initialize interrupt state */
lsc->sc_irqstat = ATPPC_IRQ_NONE;
lsc->sc_ecr_intr = lsc->sc_ctr_intr = lsc->sc_str_intr = 0;
/* Disable DMA/interrupts (each ppbus driver selects usage itself) */
lsc->sc_use = 0;
/* Configure child of the device. */
lsc->child = config_found(lsc->sc_dev, &(sc_parport_adapter),
atppc_print, CFARGS_NONE);
return;
}
/* Soft configuration detach */
int
atppc_sc_detach(struct atppc_softc *lsc, int flag)
{
device_t dev = lsc->sc_dev;
/* Detach children devices */
if (config_detach(lsc->child, flag) && !(flag & DETACH_QUIET)) {
aprint_error_dev(dev, "not able to detach child device, ");
if (!(flag & DETACH_FORCE)) {
printf("cannot detach\n");
return 1;
} else {
printf("continuing (DETACH_FORCE)\n");
}
}
if (!(flag & DETACH_QUIET))
printf("%s detached", device_xname(dev));
return 0;
}
/* Used by config_found() to print out device information */
static int
atppc_print(void *aux, const char *name)
{
/* Print out something on failure. */
if (name != NULL) {
printf("%s: child devices", name);
return UNCONF;
}
return QUIET;
}
/*
* Machine independent detection routines for atppc driver.
*/
/* Detect parallel port I/O port: taken from FreeBSD code directly. */
int
atppc_detect_port(bus_space_tag_t iot, bus_space_handle_t ioh)
{
/*
* Much shorter than scheme used by lpt_isa_probe() and lpt_port_test()
* in original lpt driver.
* Write to data register common to all controllers and read back the
* values. Also tests control and status registers.
*/
/*
* Cannot use convenient macros because the device's config structure
* may not have been created yet: major change from FreeBSD code.
*/
int rval;
u_int8_t ctr_sav, dtr_sav, str_sav;
/* Store writtable registers' values and test if they can be read */
str_sav = bus_space_read_1(iot, ioh, ATPPC_SPP_STR);
ctr_sav = bus_space_read_1(iot, ioh, ATPPC_SPP_CTR);
dtr_sav = bus_space_read_1(iot, ioh, ATPPC_SPP_DTR);
bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
BUS_SPACE_BARRIER_READ);
/*
* Ensure PS2 ports in output mode, also read back value of control
* register.
*/
bus_space_write_1(iot, ioh, ATPPC_SPP_CTR, 0x0c);
bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
BUS_SPACE_BARRIER_WRITE);
if (bus_space_read_1(iot, ioh, ATPPC_SPP_CTR) != 0x0c) {
rval = 0;
} else {
/*
* Test if two values can be written and read from the data
* register.
*/
bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
BUS_SPACE_BARRIER_READ);
bus_space_write_1(iot, ioh, ATPPC_SPP_DTR, 0xaa);
bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
BUS_SPACE_BARRIER_WRITE);
if (bus_space_read_1(iot, ioh, ATPPC_SPP_DTR) != 0xaa) {
rval = 1;
} else {
/* Second value to test */
bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
BUS_SPACE_BARRIER_READ);
bus_space_write_1(iot, ioh, ATPPC_SPP_DTR, 0x55);
bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
BUS_SPACE_BARRIER_WRITE);
if (bus_space_read_1(iot, ioh, ATPPC_SPP_DTR) != 0x55) {
rval = 1;
} else {
rval = 0;
}
}
}
/* Restore registers */
bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
BUS_SPACE_BARRIER_READ);
bus_space_write_1(iot, ioh, ATPPC_SPP_CTR, ctr_sav);
bus_space_write_1(iot, ioh, ATPPC_SPP_DTR, dtr_sav);
bus_space_write_1(iot, ioh, ATPPC_SPP_STR, str_sav);
bus_space_barrier(iot, ioh, 0, IO_LPTSIZE,
BUS_SPACE_BARRIER_WRITE);
return rval;
}
/* Detect parallel port chipset. */
static int
atppc_detect_chipset(struct atppc_softc *atppc)
{
/* Try each detection routine. */
int i, mode;
for (i = 0; chipset_detect[i] != NULL; i++) {
if ((mode = chipset_detect[i](atppc)) != -1) {
atppc->sc_mode = mode;
return 0;
}
}
return 1;
}
/* Detect generic capabilities. */
static int
atppc_detect_generic(struct atppc_softc *atppc)
{
u_int8_t ecr_sav = atppc_r_ecr(atppc);
u_int8_t ctr_sav = atppc_r_ctr(atppc);
u_int8_t str_sav = atppc_r_str(atppc);
u_int8_t tmp;
atppc_barrier_r(atppc);
/* Default to generic */
atppc->sc_type = ATPPC_TYPE_GENERIC;
atppc->sc_model = GENERIC;
/* Check for ECP */
tmp = atppc_r_ecr(atppc);
atppc_barrier_r(atppc);
if ((tmp & ATPPC_FIFO_EMPTY) && !(tmp & ATPPC_FIFO_FULL)) {
atppc_w_ecr(atppc, 0x34);
atppc_barrier_w(atppc);
tmp = atppc_r_ecr(atppc);
atppc_barrier_r(atppc);
if (tmp == 0x35) {
atppc->sc_has |= ATPPC_HAS_ECP;
}
}
/* Allow search for SMC style ECP+EPP mode */
if (atppc->sc_has & ATPPC_HAS_ECP) {
atppc_w_ecr(atppc, ATPPC_ECR_EPP);
atppc_barrier_w(atppc);
}
/* Check for EPP by checking for timeout bit */
if (atppc_check_epp_timeout(atppc->sc_dev) != 0) {
atppc->sc_has |= ATPPC_HAS_EPP;
atppc->sc_epp = ATPPC_EPP_1_9;
if (atppc->sc_has & ATPPC_HAS_ECP) {
/* SMC like chipset found */
atppc->sc_model = SMC_LIKE;
atppc->sc_type = ATPPC_TYPE_SMCLIKE;
}
}
/* Detect PS2 mode */
if (atppc->sc_has & ATPPC_HAS_ECP) {
/* Put ECP port into PS2 mode */
atppc_w_ecr(atppc, ATPPC_ECR_PS2);
atppc_barrier_w(atppc);
}
/* Put PS2 port in input mode: writes should not be readable */
atppc_w_ctr(atppc, 0x20);
atppc_barrier_w(atppc);
/*
* Write two values to data port: if neither are read back,
* bidirectional mode is functional.
*/
atppc_w_dtr(atppc, 0xaa);
atppc_barrier_w(atppc);
tmp = atppc_r_dtr(atppc);
atppc_barrier_r(atppc);
if (tmp != 0xaa) {
atppc_w_dtr(atppc, 0x55);
atppc_barrier_w(atppc);
tmp = atppc_r_dtr(atppc);
atppc_barrier_r(atppc);
if (tmp != 0x55) {
atppc->sc_has |= ATPPC_HAS_PS2;
}
}
/* Restore to previous state */
atppc_w_ecr(atppc, ecr_sav);
atppc_w_ctr(atppc, ctr_sav);
atppc_w_str(atppc, str_sav);
atppc_barrier_w(atppc);
return 0;
}
/*
* Detect parallel port FIFO: taken from FreeBSD code directly.
*/
static int
atppc_detect_fifo(struct atppc_softc *atppc)
{
#ifdef ATPPC_DEBUG
device_t dev = atppc->sc_dev;
#endif
u_int8_t ecr_sav;
u_int8_t ctr_sav;
u_int8_t str_sav;
u_int8_t cc;
short i;
/* If there is no ECP mode, we cannot config a FIFO */
if (!(atppc->sc_has & ATPPC_HAS_ECP)) {
return (EINVAL);
}
/* save registers */
ecr_sav = atppc_r_ecr(atppc);
ctr_sav = atppc_r_ctr(atppc);
str_sav = atppc_r_str(atppc);
atppc_barrier_r(atppc);
/* Enter ECP configuration mode, no interrupt, no DMA */
atppc_w_ecr(atppc, (ATPPC_ECR_CFG | ATPPC_SERVICE_INTR) &
~ATPPC_ENABLE_DMA);
atppc_barrier_w(atppc);
/* read PWord size - transfers in FIFO mode must be PWord aligned */
atppc->sc_pword = (atppc_r_cnfgA(atppc) & ATPPC_PWORD_MASK);
atppc_barrier_r(atppc);
/* XXX 16 and 32 bits implementations not supported */
if (atppc->sc_pword != ATPPC_PWORD_8) {
ATPPC_DPRINTF(("%s(%s): FIFO PWord(%d) not supported.\n",
__func__, device_xname(dev), atppc->sc_pword));
goto error;
}
/* Byte mode, reverse direction, no interrupt, no DMA */
atppc_w_ecr(atppc, ATPPC_ECR_PS2 | ATPPC_SERVICE_INTR);
atppc_w_ctr(atppc, (ctr_sav & ~IRQENABLE) | PCD);
/* enter ECP test mode, no interrupt, no DMA */
atppc_w_ecr(atppc, ATPPC_ECR_TST | ATPPC_SERVICE_INTR);
atppc_barrier_w(atppc);
/* flush the FIFO */
for (i = 0; i < 1024; i++) {
atppc_r_fifo(atppc);
atppc_barrier_r(atppc);
cc = atppc_r_ecr(atppc);
atppc_barrier_r(atppc);
if (cc & ATPPC_FIFO_EMPTY)
break;
}
if (i >= 1024) {
ATPPC_DPRINTF(("%s(%s): cannot flush FIFO.\n", __func__,
device_xname(dev)));
goto error;
}
/* Test mode, enable interrupts, no DMA */
atppc_w_ecr(atppc, ATPPC_ECR_TST);
atppc_barrier_w(atppc);
/* Determine readIntrThreshold - fill FIFO until serviceIntr is set */
for (i = atppc->sc_rthr = atppc->sc_fifo = 0; i < 1024; i++) {
atppc_w_fifo(atppc, (char)i);
atppc_barrier_w(atppc);
cc = atppc_r_ecr(atppc);
atppc_barrier_r(atppc);
if ((atppc->sc_rthr == 0) && (cc & ATPPC_SERVICE_INTR)) {
/* readThreshold reached */
atppc->sc_rthr = i + 1;
}
if (cc & ATPPC_FIFO_FULL) {
atppc->sc_fifo = i + 1;
break;
}
}
if (i >= 1024) {
ATPPC_DPRINTF(("%s(%s): cannot fill FIFO.\n", __func__,
device_xname(dev)));
goto error;
}
/* Change direction */
atppc_w_ctr(atppc, (ctr_sav & ~IRQENABLE) & ~PCD);
atppc_barrier_w(atppc);
/* Clear the serviceIntr bit we've already set in the above loop */
atppc_w_ecr(atppc, ATPPC_ECR_TST);
atppc_barrier_w(atppc);
/* Determine writeIntrThreshold - empty FIFO until serviceIntr is set */
for (atppc->sc_wthr = 0; i > -1; i--) {
cc = atppc_r_fifo(atppc);
atppc_barrier_r(atppc);
if (cc != (char)(atppc->sc_fifo - i - 1)) {
ATPPC_DPRINTF(("%s(%s): invalid data in FIFO.\n",
__func__, device_xname(dev)));
goto error;
}
cc = atppc_r_ecr(atppc);
atppc_barrier_r(atppc);
if ((atppc->sc_wthr == 0) && (cc & ATPPC_SERVICE_INTR)) {
/* writeIntrThreshold reached */
atppc->sc_wthr = atppc->sc_fifo - i;
}
if (i > 0 && (cc & ATPPC_FIFO_EMPTY)) {
/* If FIFO empty before the last byte, error */
ATPPC_DPRINTF(("%s(%s): data lost in FIFO.\n", __func__,
device_xname(dev)));
goto error;
}
}
/* FIFO must be empty after the last byte */
cc = atppc_r_ecr(atppc);
atppc_barrier_r(atppc);
if (!(cc & ATPPC_FIFO_EMPTY)) {
ATPPC_DPRINTF(("%s(%s): cannot empty the FIFO.\n", __func__,
device_xname(dev)));
goto error;
}
/* Restore original registers */
atppc_w_ctr(atppc, ctr_sav);
atppc_w_str(atppc, str_sav);
atppc_w_ecr(atppc, ecr_sav);
atppc_barrier_w(atppc);
/* Update capabilities */
atppc->sc_has |= ATPPC_HAS_FIFO;
return 0;
error:
/* Restore original registers */
atppc_w_ctr(atppc, ctr_sav);
atppc_w_str(atppc, str_sav);
atppc_w_ecr(atppc, ecr_sav);
atppc_barrier_w(atppc);
return (EINVAL);
}
/* Interrupt handler for atppc device: wakes up read/write functions */
int
atppcintr(void *arg)
{
device_t dev = arg;
struct atppc_softc *atppc = device_private(dev);
int claim = 1;
enum { NONE, READER, WRITER } wake_up = NONE;
mutex_enter(&atppc->sc_lock);
/* Record registers' status */
atppc->sc_str_intr = atppc_r_str(atppc);
atppc->sc_ctr_intr = atppc_r_ctr(atppc);
atppc->sc_ecr_intr = atppc_r_ecr(atppc);
atppc_barrier_r(atppc);
/* Determine cause of interrupt and wake up top half */
switch (atppc->sc_mode) {
case ATPPC_MODE_STD:
/* nAck pulsed for 5 usec, too fast to check reliably, assume */
atppc->sc_irqstat = ATPPC_IRQ_nACK;
if (atppc->sc_outb)
wake_up = WRITER;
else
claim = 0;
break;
case ATPPC_MODE_NIBBLE:
case ATPPC_MODE_PS2:
/* nAck is set low by device and then high on ack */
if (!(atppc->sc_str_intr & nACK)) {
claim = 0;
break;
}
atppc->sc_irqstat = ATPPC_IRQ_nACK;
if (atppc->sc_inb)
wake_up = READER;
break;
case ATPPC_MODE_ECP:
case ATPPC_MODE_FAST:
/* Confirm interrupt cause: these are not pulsed as in nAck. */
if (atppc->sc_ecr_intr & ATPPC_SERVICE_INTR) {
if (atppc->sc_ecr_intr & ATPPC_ENABLE_DMA)
atppc->sc_irqstat |= ATPPC_IRQ_DMA;
else
atppc->sc_irqstat |= ATPPC_IRQ_FIFO;
/* Decide where top half will be waiting */
if (atppc->sc_mode & ATPPC_MODE_ECP) {
if (atppc->sc_ctr_intr & PCD) {
if (atppc->sc_inb)
wake_up = READER;
else
claim = 0;
} else {
if (atppc->sc_outb)
wake_up = WRITER;
else
claim = 0;
}
} else {
if (atppc->sc_outb)
wake_up = WRITER;
else
claim = 0;
}
}
/* Determine if nFault has occurred */
if ((atppc->sc_mode & ATPPC_MODE_ECP) &&
(atppc->sc_ecr_intr & ATPPC_nFAULT_INTR) &&
!(atppc->sc_str_intr & nFAULT)) {
/* Device is requesting the channel */
atppc->sc_irqstat |= ATPPC_IRQ_nFAULT;
claim = 1;
}
break;
case ATPPC_MODE_EPP:
/* nAck pulsed for 5 usec, too fast to check reliably */
atppc->sc_irqstat = ATPPC_IRQ_nACK;
if (atppc->sc_inb)
wake_up = WRITER;
else if (atppc->sc_outb)
wake_up = READER;
else
claim = 0;
break;
default:
panic("%s: chipset is in invalid mode.", device_xname(dev));
}
if (claim) {
switch (wake_up) {
case NONE:
break;
case READER:
cv_broadcast(&atppc->sc_in_cv);
break;
case WRITER:
cv_broadcast(&atppc->sc_out_cv);
break;
}
}
/* Call all of the installed handlers */
if (claim) {
struct atppc_handler_node * callback;
SLIST_FOREACH(callback, &(atppc->sc_handler_listhead),
entries) {
(*callback->func)(callback->arg);
}
}
mutex_exit(&atppc->sc_lock);
return claim;
}
/* Functions which support ppbus interface */
/* Check EPP mode timeout */
static int
atppc_check_epp_timeout(device_t dev)
{
struct atppc_softc *atppc = device_private(dev);
int error;
mutex_enter(&atppc->sc_lock);
atppc_reset_epp_timeout(dev);
error = !(atppc_r_str(atppc) & TIMEOUT);
atppc_barrier_r(atppc);
mutex_exit(&atppc->sc_lock);
return (error);
}
/*
* EPP timeout, according to the PC87332 manual
* Semantics of clearing EPP timeout bit.
* PC87332 - reading SPP_STR does it...
* SMC - write 1 to EPP timeout bit XXX
* Others - (?) write 0 to EPP timeout bit
*/
static void
atppc_reset_epp_timeout(device_t dev)
{
struct atppc_softc *atppc = device_private(dev);
register unsigned char r;
r = atppc_r_str(atppc);
atppc_barrier_r(atppc);
atppc_w_str(atppc, r | 0x1);
atppc_barrier_w(atppc);
atppc_w_str(atppc, r & 0xfe);
atppc_barrier_w(atppc);
return;
}
/* Read from atppc device: returns 0 on success. */
static int
atppc_read(device_t dev, char *buf, int len, int ioflag,
size_t *cnt)
{
struct atppc_softc *atppc = device_private(dev);
int error = 0;
mutex_enter(&atppc->sc_lock);
*cnt = 0;
/* Initialize buffer */
atppc->sc_inb = atppc->sc_inbstart = buf;
atppc->sc_inb_nbytes = len;
/* Initialize device input error state for new operation */
atppc->sc_inerr = 0;
/* Call appropriate function to read bytes */
switch(atppc->sc_mode) {
case ATPPC_MODE_STD:
case ATPPC_MODE_FAST:
error = ENODEV;
break;
case ATPPC_MODE_NIBBLE:
atppc_nibble_read(atppc);
break;
case ATPPC_MODE_PS2:
atppc_byte_read(atppc);
break;
case ATPPC_MODE_ECP:
atppc_ecp_read(atppc);
break;
case ATPPC_MODE_EPP:
atppc_epp_read(atppc);
break;
default:
panic("%s(%s): chipset in invalid mode.\n", __func__,
device_xname(dev));
}
/* Update counter*/
*cnt = (atppc->sc_inbstart - atppc->sc_inb);
/* Reset buffer */
atppc->sc_inb = atppc->sc_inbstart = NULL;
atppc->sc_inb_nbytes = 0;
if (!(error))
error = atppc->sc_inerr;
mutex_exit(&atppc->sc_lock);
return (error);
}
/* Write to atppc device: returns 0 on success. */
static int
atppc_write(device_t dev, char *buf, int len, int ioflag, size_t *cnt)
{
struct atppc_softc * const atppc = device_private(dev);
int error = 0;
*cnt = 0;
mutex_enter(&atppc->sc_lock);
/* Set up line buffer */
atppc->sc_outb = atppc->sc_outbstart = buf;
atppc->sc_outb_nbytes = len;
/* Initialize device output error state for new operation */
atppc->sc_outerr = 0;
/* Call appropriate function to write bytes */
switch (atppc->sc_mode) {
case ATPPC_MODE_STD:
atppc_std_write(atppc);
break;
case ATPPC_MODE_NIBBLE:
case ATPPC_MODE_PS2:
error = ENODEV;
break;
case ATPPC_MODE_FAST:
case ATPPC_MODE_ECP:
atppc_fifo_write(atppc);
break;
case ATPPC_MODE_EPP:
atppc_epp_write(atppc);
break;
default:
panic("%s(%s): chipset in invalid mode.\n", __func__,
device_xname(dev));
}
/* Update counter*/
*cnt = (atppc->sc_outbstart - atppc->sc_outb);
/* Reset output buffer */
atppc->sc_outb = atppc->sc_outbstart = NULL;
atppc->sc_outb_nbytes = 0;
if (!(error))
error = atppc->sc_outerr;
mutex_exit(&atppc->sc_lock);
return (error);
}
/*
* Set mode of chipset to mode argument. Modes not supported are ignored. If
* multiple modes are flagged, the mode is not changed. Mode's are those
* defined for ppbus_softc.sc_mode in ppbus_conf.h. Only ECP-capable chipsets
* can change their mode of operation. However, ALL operation modes support
* centronics mode and nibble mode. Modes determine both hardware AND software
* behaviour.
* NOTE: the mode for ECP should only be changed when the channel is in
* forward idle mode. This function does not make sure FIFO's have flushed or
* any consistency checks.
*/
static int
atppc_setmode(device_t dev, int mode)
{
struct atppc_softc *atppc = device_private(dev);
u_int8_t ecr;
u_int8_t chipset_mode;
int rval = 0;
mutex_enter(&atppc->sc_lock);
/* If ECP capable, configure ecr register */
if (atppc->sc_has & ATPPC_HAS_ECP) {
/* Read ECR with mode masked out */
ecr = (atppc_r_ecr(atppc) & 0x1f);
atppc_barrier_r(atppc);
switch (mode) {
case PPBUS_ECP:
/* Set ECP mode */
ecr |= ATPPC_ECR_ECP;
chipset_mode = ATPPC_MODE_ECP;
break;
case PPBUS_EPP:
/* Set EPP mode */
if (atppc->sc_has & ATPPC_HAS_EPP) {
ecr |= ATPPC_ECR_EPP;
chipset_mode = ATPPC_MODE_EPP;
} else {
rval = ENODEV;
goto end;
}
break;
case PPBUS_FAST:
/* Set fast centronics mode */
ecr |= ATPPC_ECR_FIFO;
chipset_mode = ATPPC_MODE_FAST;
break;
case PPBUS_PS2:
/* Set PS2 mode */
ecr |= ATPPC_ECR_PS2;
chipset_mode = ATPPC_MODE_PS2;
break;
case PPBUS_COMPATIBLE:
/* Set standard mode */
ecr |= ATPPC_ECR_STD;
chipset_mode = ATPPC_MODE_STD;
break;
case PPBUS_NIBBLE:
/* Set nibble mode: uses chipset standard mode */
ecr |= ATPPC_ECR_STD;
chipset_mode = ATPPC_MODE_NIBBLE;
break;
default:
/* Invalid mode specified for ECP chip */
ATPPC_DPRINTF(("%s(%s): invalid mode passed as "
"argument.\n", __func__, device_xname(dev)));
rval = ENODEV;
goto end;
}
/* Switch to byte mode to be able to change modes. */
atppc_w_ecr(atppc, ATPPC_ECR_PS2);
atppc_barrier_w(atppc);
/* Update mode */
atppc_w_ecr(atppc, ecr);
atppc_barrier_w(atppc);
} else {
switch (mode) {
case PPBUS_EPP:
if (atppc->sc_has & ATPPC_HAS_EPP) {
chipset_mode = ATPPC_MODE_EPP;
} else {
rval = ENODEV;
goto end;
}
break;
case PPBUS_PS2:
if (atppc->sc_has & ATPPC_HAS_PS2) {
chipset_mode = ATPPC_MODE_PS2;
} else {
rval = ENODEV;
goto end;
}
break;
case PPBUS_NIBBLE:
/* Set nibble mode (virtual) */
chipset_mode = ATPPC_MODE_NIBBLE;
break;
case PPBUS_COMPATIBLE:
chipset_mode = ATPPC_MODE_STD;
break;
case PPBUS_ECP:
rval = ENODEV;
goto end;
default:
ATPPC_DPRINTF(("%s(%s): invalid mode passed as "
"argument.\n", __func__, device_xname(dev)));
rval = ENODEV;
goto end;
}
}
atppc->sc_mode = chipset_mode;
if (chipset_mode == ATPPC_MODE_PS2) {
/* Set direction bit to reverse */
ecr = atppc_r_ctr(atppc);
atppc_barrier_r(atppc);
ecr |= PCD;
atppc_w_ctr(atppc, ecr);
atppc_barrier_w(atppc);
}
end:
mutex_exit(&atppc->sc_lock);
return rval;
}
/* Get the current mode of chipset */
static int
atppc_getmode(device_t dev)
{
struct atppc_softc *atppc = device_private(dev);
int mode;
mutex_enter(&atppc->sc_lock);
/* The chipset can only be in one mode at a time logically */
switch (atppc->sc_mode) {
case ATPPC_MODE_ECP:
mode = PPBUS_ECP;
break;
case ATPPC_MODE_EPP:
mode = PPBUS_EPP;
break;
case ATPPC_MODE_PS2:
mode = PPBUS_PS2;
break;
case ATPPC_MODE_STD:
mode = PPBUS_COMPATIBLE;
break;
case ATPPC_MODE_NIBBLE:
mode = PPBUS_NIBBLE;
break;
case ATPPC_MODE_FAST:
mode = PPBUS_FAST;
break;
default:
panic("%s(%s): device is in invalid mode!", __func__,
device_xname(dev));
break;
}
mutex_exit(&atppc->sc_lock);
return mode;
}
/* Wait for FIFO buffer to empty for ECP-capable chipset */
static void
atppc_ecp_sync(device_t dev)
{
struct atppc_softc *atppc = device_private(dev);
int i;
u_int8_t r;
mutex_enter(&atppc->sc_lock);
/*
* Only wait for FIFO to empty if mode is chipset is ECP-capable AND
* the mode is either ECP or Fast Centronics.
*/
r = atppc_r_ecr(atppc);
atppc_barrier_r(atppc);
r &= 0xe0;
if (!(atppc->sc_has & ATPPC_HAS_ECP) || ((r != ATPPC_ECR_ECP)
&& (r != ATPPC_ECR_FIFO))) {
goto end;
}
/* Wait for FIFO to empty */
for (i = 0; i < ((MAXBUSYWAIT/hz) * 1000000); i += 100) {
r = atppc_r_ecr(atppc);
atppc_barrier_r(atppc);
if (r & ATPPC_FIFO_EMPTY) {
goto end;
}
delay(100); /* Supposed to be a 100 usec delay */
}
ATPPC_DPRINTF(("%s: ECP sync failed, data still in FIFO.\n",
device_xname(dev)));
end:
mutex_exit(&atppc->sc_lock);
return;
}
/* Execute a microsequence to handle fast I/O operations. */
static int
atppc_exec_microseq(device_t dev, struct ppbus_microseq **p_msq)
{
struct atppc_softc *atppc = device_private(dev);
struct ppbus_microseq *mi = *p_msq;
char cc, *p;
int i, iter, len;
int error;
register int reg;
register unsigned char mask;
register int accum = 0;
register char *ptr = NULL;
struct ppbus_microseq *stack = NULL;
mutex_enter(&atppc->sc_lock);
/* microsequence registers are equivalent to PC-like port registers */
#define r_reg(register,atppc) bus_space_read_1((atppc)->sc_iot, \
(atppc)->sc_ioh, (register))
#define w_reg(register, atppc, byte) bus_space_write_1((atppc)->sc_iot, \
(atppc)->sc_ioh, (register), (byte))
/* Loop until microsequence execution finishes (ending op code) */
for (;;) {
switch (mi->opcode) {
case MS_OP_RSET:
cc = r_reg(mi->arg[0].i, atppc);
atppc_barrier_r(atppc);
cc &= (char)mi->arg[2].i; /* clear mask */
cc |= (char)mi->arg[1].i; /* assert mask */
w_reg(mi->arg[0].i, atppc, cc);
atppc_barrier_w(atppc);
mi++;
break;
case MS_OP_RASSERT_P:
reg = mi->arg[1].i;
ptr = atppc->sc_ptr;
if ((len = mi->arg[0].i) == MS_ACCUM) {
accum = atppc->sc_accum;
for (; accum; accum--) {
w_reg(reg, atppc, *ptr++);
atppc_barrier_w(atppc);
}
atppc->sc_accum = accum;
} else {
for (i = 0; i < len; i++) {
w_reg(reg, atppc, *ptr++);
atppc_barrier_w(atppc);
}
}
atppc->sc_ptr = ptr;
mi++;
break;
case MS_OP_RFETCH_P:
reg = mi->arg[1].i;
mask = (char)mi->arg[2].i;
ptr = atppc->sc_ptr;
if ((len = mi->arg[0].i) == MS_ACCUM) {
accum = atppc->sc_accum;
for (; accum; accum--) {
*ptr++ = r_reg(reg, atppc) & mask;
atppc_barrier_r(atppc);
}
atppc->sc_accum = accum;
} else {
for (i = 0; i < len; i++) {
*ptr++ = r_reg(reg, atppc) & mask;
atppc_barrier_r(atppc);
}
}
atppc->sc_ptr = ptr;
mi++;
break;
case MS_OP_RFETCH:
*((char *)mi->arg[2].p) = r_reg(mi->arg[0].i, atppc) &
(char)mi->arg[1].i;
atppc_barrier_r(atppc);
mi++;
break;
case MS_OP_RASSERT:
case MS_OP_DELAY:
/* let's suppose the next instr. is the same */
do {
for (;mi->opcode == MS_OP_RASSERT; mi++) {
w_reg(mi->arg[0].i, atppc,
(char)mi->arg[1].i);
atppc_barrier_w(atppc);
}
for (;mi->opcode == MS_OP_DELAY; mi++) {
delay(mi->arg[0].i);
}
} while (mi->opcode == MS_OP_RASSERT);
break;
case MS_OP_ADELAY:
if (mi->arg[0].i) {
tsleep(atppc, PPBUSPRI, "atppcdelay",
mi->arg[0].i * (hz/1000));
}
mi++;
break;
case MS_OP_TRIG:
reg = mi->arg[0].i;
iter = mi->arg[1].i;
p = (char *)mi->arg[2].p;
/* XXX delay limited to 255 us */
for (i = 0; i < iter; i++) {
w_reg(reg, atppc, *p++);
atppc_barrier_w(atppc);
delay((unsigned char)*p++);
}
mi++;
break;
case MS_OP_SET:
atppc->sc_accum = mi->arg[0].i;
mi++;
break;
case MS_OP_DBRA:
if (--atppc->sc_accum > 0) {
mi += mi->arg[0].i;
}
mi++;
break;
case MS_OP_BRSET:
cc = atppc_r_str(atppc);
atppc_barrier_r(atppc);
if ((cc & (char)mi->arg[0].i) == (char)mi->arg[0].i) {
mi += mi->arg[1].i;
}
mi++;
break;
case MS_OP_BRCLEAR:
cc = atppc_r_str(atppc);
atppc_barrier_r(atppc);
if ((cc & (char)mi->arg[0].i) == 0) {
mi += mi->arg[1].i;
}
mi++;
break;
case MS_OP_BRSTAT:
cc = atppc_r_str(atppc);
atppc_barrier_r(atppc);
if ((cc & ((char)mi->arg[0].i | (char)mi->arg[1].i)) ==
(char)mi->arg[0].i) {
mi += mi->arg[2].i;
}
mi++;
break;
case MS_OP_C_CALL:
/*
* If the C call returns !0 then end the microseq.
* The current state of ptr is passed to the C function
*/
if ((error = mi->arg[0].f(mi->arg[1].p,
atppc->sc_ptr))) {
mutex_exit(&atppc->sc_lock);
return (error);
}
mi++;
break;
case MS_OP_PTR:
atppc->sc_ptr = (char *)mi->arg[0].p;
mi++;
break;
case MS_OP_CALL:
if (stack) {
panic("%s - %s: too much calls", device_xname(dev),
__func__);
}
if (mi->arg[0].p) {
/* store state of the actual microsequence */
stack = mi;
/* jump to the new microsequence */
mi = (struct ppbus_microseq *)mi->arg[0].p;
} else {
mi++;
}
break;
case MS_OP_SUBRET:
/* retrieve microseq and pc state before the call */
mi = stack;
/* reset the stack */
stack = 0;
/* XXX return code */
mi++;
break;
case MS_OP_PUT:
case MS_OP_GET:
case MS_OP_RET:
/*
* Can't return to atppc level during the execution
* of a submicrosequence.
*/
if (stack) {
panic("%s: cannot return to atppc level",
__func__);
}
/* update pc for atppc level of execution */
*p_msq = mi;
mutex_exit(&atppc->sc_lock);
return (0);
break;
default:
panic("%s: unknown microsequence "
"opcode 0x%x", __func__, mi->opcode);
break;
}
}
/* Should not be reached! */
#ifdef ATPPC_DEBUG
panic("%s: unexpected code reached!\n", __func__);
#endif
}
/* General I/O routine */
static u_int8_t
atppc_io(device_t dev, int iop, u_char *addr, int cnt, u_char byte)
{
struct atppc_softc *atppc = device_private(dev);
u_int8_t val = 0;
mutex_enter(&atppc->sc_lock);
switch (iop) {
case PPBUS_OUTSB_EPP:
bus_space_write_multi_1(atppc->sc_iot, atppc->sc_ioh,
ATPPC_EPP_DATA, addr, cnt);
break;
case PPBUS_OUTSW_EPP:
bus_space_write_multi_2(atppc->sc_iot, atppc->sc_ioh,
ATPPC_EPP_DATA, (u_int16_t *)addr, cnt);
break;
case PPBUS_OUTSL_EPP:
bus_space_write_multi_4(atppc->sc_iot, atppc->sc_ioh,
ATPPC_EPP_DATA, (u_int32_t *)addr, cnt);
break;
case PPBUS_INSB_EPP:
bus_space_read_multi_1(atppc->sc_iot, atppc->sc_ioh,
ATPPC_EPP_DATA, addr, cnt);
break;
case PPBUS_INSW_EPP:
bus_space_read_multi_2(atppc->sc_iot, atppc->sc_ioh,
ATPPC_EPP_DATA, (u_int16_t *)addr, cnt);
break;
case PPBUS_INSL_EPP:
bus_space_read_multi_4(atppc->sc_iot, atppc->sc_ioh,
ATPPC_EPP_DATA, (u_int32_t *)addr, cnt);
break;
case PPBUS_RDTR:
val = (atppc_r_dtr(atppc));
break;
case PPBUS_RSTR:
val = (atppc_r_str(atppc));
break;
case PPBUS_RCTR:
val = (atppc_r_ctr(atppc));
break;
case PPBUS_REPP_A:
val = (atppc_r_eppA(atppc));
break;
case PPBUS_REPP_D:
val = (atppc_r_eppD(atppc));
break;
case PPBUS_RECR:
val = (atppc_r_ecr(atppc));
break;
case PPBUS_RFIFO:
val = (atppc_r_fifo(atppc));
break;
case PPBUS_WDTR:
atppc_w_dtr(atppc, byte);
break;
case PPBUS_WSTR:
atppc_w_str(atppc, byte);
break;
case PPBUS_WCTR:
atppc_w_ctr(atppc, byte);
break;
case PPBUS_WEPP_A:
atppc_w_eppA(atppc, byte);
break;
case PPBUS_WEPP_D:
atppc_w_eppD(atppc, byte);
break;
case PPBUS_WECR:
atppc_w_ecr(atppc, byte);
break;
case PPBUS_WFIFO:
atppc_w_fifo(atppc, byte);
break;
default:
panic("%s(%s): unknown I/O operation", device_xname(dev),
__func__);
break;
}
atppc_barrier(atppc);
mutex_exit(&atppc->sc_lock);
return val;
}
/* Read "instance variables" of atppc device */
static int
atppc_read_ivar(device_t dev, int index, unsigned int *val)
{
struct atppc_softc *atppc = device_private(dev);
int rval = 0;
mutex_enter(&atppc->sc_lock);
switch(index) {
case PPBUS_IVAR_EPP_PROTO:
if (atppc->sc_epp == ATPPC_EPP_1_9)
*val = PPBUS_EPP_1_9;
else if (atppc->sc_epp == ATPPC_EPP_1_7)
*val = PPBUS_EPP_1_7;
/* XXX what if not using EPP ? */
break;
case PPBUS_IVAR_INTR:
*val = ((atppc->sc_use & ATPPC_USE_INTR) != 0);
break;
case PPBUS_IVAR_DMA:
*val = ((atppc->sc_use & ATPPC_USE_DMA) != 0);
break;
default:
rval = ENODEV;
}
mutex_exit(&atppc->sc_lock);
return rval;
}
/* Write "instance variables" of atppc device */
static int
atppc_write_ivar(device_t dev, int index, unsigned int *val)
{
struct atppc_softc *atppc = device_private(dev);
int rval = 0;
mutex_enter(&atppc->sc_lock);
switch(index) {
case PPBUS_IVAR_EPP_PROTO:
if (*val == PPBUS_EPP_1_9 || *val == PPBUS_EPP_1_7)
atppc->sc_epp = *val;
else
rval = EINVAL;
break;
case PPBUS_IVAR_INTR:
if (*val == 0)
atppc->sc_use &= ~ATPPC_USE_INTR;
else if (atppc->sc_has & ATPPC_HAS_INTR)
atppc->sc_use |= ATPPC_USE_INTR;
else
rval = ENODEV;
break;
case PPBUS_IVAR_DMA:
if (*val == 0)
atppc->sc_use &= ~ATPPC_USE_DMA;
else if (atppc->sc_has & ATPPC_HAS_DMA)
atppc->sc_use |= ATPPC_USE_DMA;
else
rval = ENODEV;
break;
default:
rval = ENODEV;
}
mutex_exit(&atppc->sc_lock);
return rval;
}
/* Add a handler routine to be called by the interrupt handler */
static int
atppc_add_handler(device_t dev, void (*handler)(void *), void *arg)
{
struct atppc_softc *atppc = device_private(dev);
struct atppc_handler_node *callback;
if (handler == NULL) {
ATPPC_DPRINTF(("%s(%s): attempt to register NULL handler.\n",
__func__, device_xname(dev)));
return EINVAL;
}
callback = kmem_alloc(sizeof(*callback), KM_SLEEP);
callback->func = handler;
callback->arg = arg;
mutex_enter(&atppc->sc_lock);
SLIST_INSERT_HEAD(&(atppc->sc_handler_listhead),
callback, entries);
mutex_exit(&atppc->sc_lock);
return 0;
}
/* Remove a handler added by atppc_add_handler() */
static int
atppc_remove_handler(device_t dev, void (*handler)(void *))
{
struct atppc_softc *atppc = device_private(dev);
struct atppc_handler_node *callback;
int rval = EINVAL;
mutex_enter(&atppc->sc_lock);
if (SLIST_EMPTY(&(atppc->sc_handler_listhead)))
panic("%s(%s): attempt to remove handler from empty list.\n",
__func__, device_xname(dev));
/* Search list for handler */
SLIST_FOREACH(callback, &(atppc->sc_handler_listhead), entries) {
if (callback->func == handler) {
SLIST_REMOVE(&(atppc->sc_handler_listhead), callback,
atppc_handler_node, entries);
rval = 0;
break;
}
}
mutex_exit(&atppc->sc_lock);
if (rval == 0) {
kmem_free(callback, sizeof(*callback));
}
return rval;
}
/* Utility functions */
/*
* Functions that read bytes from port into buffer: called from interrupt
* handler depending on current chipset mode and cause of interrupt. Return
* value: number of bytes moved.
*/
/* Only the lower 4 bits of the final value are valid */
#define nibble2char(s) ((((s) & ~nACK) >> 3) | (~(s) & nBUSY) >> 4)
/* Read bytes in nibble mode */
static void
atppc_nibble_read(struct atppc_softc *atppc)
{
int i;
u_int8_t nibble[2];
u_int8_t ctr;
u_int8_t str;
/* Enable interrupts if needed */
if (atppc->sc_use & ATPPC_USE_INTR) {
ctr = atppc_r_ctr(atppc);
atppc_barrier_r(atppc);
if (!(ctr & IRQENABLE)) {
ctr |= IRQENABLE;
atppc_w_ctr(atppc, ctr);
atppc_barrier_w(atppc);
}
}
while (atppc->sc_inbstart < (atppc->sc_inb + atppc->sc_inb_nbytes)) {
/* Check if device has data to send in idle phase */
str = atppc_r_str(atppc);
atppc_barrier_r(atppc);
if (str & nDATAVAIL) {
return;
}
/* Nibble-mode handshake transfer */
for (i = 0; i < 2; i++) {
/* Event 7 - ready to take data (HOSTBUSY low) */
ctr = atppc_r_ctr(atppc);
atppc_barrier_r(atppc);
ctr |= HOSTBUSY;
atppc_w_ctr(atppc, ctr);
atppc_barrier_w(atppc);
/* Event 8 - peripheral writes the first nibble */
/* Event 9 - peripheral set nAck low */
atppc->sc_inerr = atppc_poll_str(atppc, 0, PTRCLK);
if (atppc->sc_inerr)
return;
/* read nibble */
nibble[i] = atppc_r_str(atppc);
/* Event 10 - ack, nibble received */
ctr &= ~HOSTBUSY;
atppc_w_ctr(atppc, ctr);
/* Event 11 - wait ack from peripheral */
if (atppc->sc_use & ATPPC_USE_INTR)
atppc->sc_inerr = atppc_wait_interrupt(atppc,
&atppc->sc_in_cv, ATPPC_IRQ_nACK);
else
atppc->sc_inerr = atppc_poll_str(atppc, PTRCLK,
PTRCLK);
if (atppc->sc_inerr)
return;
}
/* Store byte transferred */
*(atppc->sc_inbstart) = ((nibble2char(nibble[1]) << 4) & 0xf0) |
(nibble2char(nibble[0]) & 0x0f);
atppc->sc_inbstart++;
}
}
/* Read bytes in bidirectional mode */
static void
atppc_byte_read(struct atppc_softc * const atppc)
{
u_int8_t ctr;
u_int8_t str;
/* Check direction bit */
ctr = atppc_r_ctr(atppc);
atppc_barrier_r(atppc);
if (!(ctr & PCD)) {
ATPPC_DPRINTF(("%s: byte-mode read attempted without direction "
"bit set.", device_xname(atppc->sc_dev)));
atppc->sc_inerr = ENODEV;
return;
}
/* Enable interrupts if needed */
if (atppc->sc_use & ATPPC_USE_INTR) {
if (!(ctr & IRQENABLE)) {
ctr |= IRQENABLE;
atppc_w_ctr(atppc, ctr);
atppc_barrier_w(atppc);
}
}
/* Byte-mode handshake transfer */
while (atppc->sc_inbstart < (atppc->sc_inb + atppc->sc_inb_nbytes)) {
/* Check if device has data to send */
str = atppc_r_str(atppc);
atppc_barrier_r(atppc);
if (str & nDATAVAIL) {
return;
}
/* Event 7 - ready to take data (nAUTO low) */
ctr |= HOSTBUSY;
atppc_w_ctr(atppc, ctr);
atppc_barrier_w(atppc);
/* Event 9 - peripheral set nAck low */
atppc->sc_inerr = atppc_poll_str(atppc, 0, PTRCLK);
if (atppc->sc_inerr)
return;
/* Store byte transferred */
*(atppc->sc_inbstart) = atppc_r_dtr(atppc);
atppc_barrier_r(atppc);
/* Event 10 - data received, can't accept more */
ctr &= ~HOSTBUSY;
atppc_w_ctr(atppc, ctr);
atppc_barrier_w(atppc);
/* Event 11 - peripheral ack */
if (atppc->sc_use & ATPPC_USE_INTR)
atppc->sc_inerr = atppc_wait_interrupt(atppc,
&atppc->sc_in_cv, ATPPC_IRQ_nACK);
else
atppc->sc_inerr = atppc_poll_str(atppc, PTRCLK, PTRCLK);
if (atppc->sc_inerr)
return;
/* Event 16 - strobe */
str |= HOSTCLK;
atppc_w_str(atppc, str);
atppc_barrier_w(atppc);
DELAY(1);
str &= ~HOSTCLK;
atppc_w_str(atppc, str);
atppc_barrier_w(atppc);
/* Update counter */
atppc->sc_inbstart++;
}
}
/* Read bytes in EPP mode */
static void
atppc_epp_read(struct atppc_softc * atppc)
{
if (atppc->sc_epp == ATPPC_EPP_1_9) {
{
uint8_t str;
int i;
atppc_reset_epp_timeout(atppc->sc_dev);
for (i = 0; i < atppc->sc_inb_nbytes; i++) {
*(atppc->sc_inbstart) = atppc_r_eppD(atppc);
atppc_barrier_r(atppc);
str = atppc_r_str(atppc);
atppc_barrier_r(atppc);
if (str & TIMEOUT) {
atppc->sc_inerr = EIO;
break;
}
atppc->sc_inbstart++;
}
}
} else {
/* Read data block from EPP data register */
atppc_r_eppD_multi(atppc, atppc->sc_inbstart,
atppc->sc_inb_nbytes);
atppc_barrier_r(atppc);
/* Update buffer position, byte count and counter */
atppc->sc_inbstart += atppc->sc_inb_nbytes;
}
return;
}
/* Read bytes in ECP mode */
static void
atppc_ecp_read(struct atppc_softc *atppc)
{
u_int8_t ecr;
u_int8_t ctr;
u_int8_t str;
const unsigned char ctr_sav = atppc_r_ctr(atppc);
const unsigned char ecr_sav = atppc_r_ecr(atppc);
unsigned int worklen;
/* Check direction bit */
ctr = ctr_sav;
atppc_barrier_r(atppc);
if (!(ctr & PCD)) {
ATPPC_DPRINTF(("%s: ecp-mode read attempted without direction "
"bit set.", device_xname(atppc->sc_dev)));
atppc->sc_inerr = ENODEV;
goto end;
}
/* Clear device request if any */
if (atppc->sc_use & ATPPC_USE_INTR)
atppc->sc_irqstat &= ~ATPPC_IRQ_nFAULT;
while (atppc->sc_inbstart < (atppc->sc_inb + atppc->sc_inb_nbytes)) {
ecr = atppc_r_ecr(atppc);
atppc_barrier_r(atppc);
if (ecr & ATPPC_FIFO_EMPTY) {
/* Check for invalid state */
if (ecr & ATPPC_FIFO_FULL) {
atppc_ecp_read_error(atppc);
break;
}
/* Check if device has data to send */
str = atppc_r_str(atppc);
atppc_barrier_r(atppc);
if (str & nDATAVAIL) {
break;
}
if (atppc->sc_use & ATPPC_USE_INTR) {
/* Enable interrupts */
ecr &= ~ATPPC_SERVICE_INTR;
atppc_w_ecr(atppc, ecr);
atppc_barrier_w(atppc);
/* Wait for FIFO to fill */
atppc->sc_inerr = atppc_wait_interrupt(atppc,
&atppc->sc_in_cv, ATPPC_IRQ_FIFO);
if (atppc->sc_inerr)
break;
} else {
DELAY(1);
}
continue;
}
else if (ecr & ATPPC_FIFO_FULL) {
/* Transfer sc_fifo bytes */
worklen = atppc->sc_fifo;
}
else if (ecr & ATPPC_SERVICE_INTR) {
/* Transfer sc_rthr bytes */
worklen = atppc->sc_rthr;
} else {
/* At least one byte is in the FIFO */
worklen = 1;
}
if ((atppc->sc_use & ATPPC_USE_INTR) &&
(atppc->sc_use & ATPPC_USE_DMA)) {
atppc_ecp_read_dma(atppc, &worklen, ecr);
} else {
atppc_ecp_read_pio(atppc, &worklen, ecr);
}
if (atppc->sc_inerr) {
atppc_ecp_read_error(atppc);
break;
}
/* Update counter */
atppc->sc_inbstart += worklen;
}
end:
atppc_w_ctr(atppc, ctr_sav);
atppc_w_ecr(atppc, ecr_sav);
atppc_barrier_w(atppc);
}
/* Read bytes in ECP mode using DMA transfers */
static void
atppc_ecp_read_dma(struct atppc_softc *atppc, unsigned int *length,
unsigned char ecr)
{
/* Limit transfer to maximum DMA size and start it */
*length = uimin(*length, atppc->sc_dma_maxsize);
atppc->sc_dmastat = ATPPC_DMA_INIT;
atppc->sc_dma_start(atppc, atppc->sc_inbstart, *length,
ATPPC_DMA_MODE_READ);
atppc->sc_dmastat = ATPPC_DMA_STARTED;
/* Enable interrupts, DMA */
ecr &= ~ATPPC_SERVICE_INTR;
ecr |= ATPPC_ENABLE_DMA;
atppc_w_ecr(atppc, ecr);
atppc_barrier_w(atppc);
/* Wait for DMA completion */
atppc->sc_inerr = atppc_wait_interrupt(atppc, &atppc->sc_in_cv,
ATPPC_IRQ_DMA);
if (atppc->sc_inerr)
return;
/* Get register value recorded by interrupt handler */
ecr = atppc->sc_ecr_intr;
/* Clear DMA programming */
atppc->sc_dma_finish(atppc);
atppc->sc_dmastat = ATPPC_DMA_COMPLETE;
/* Disable DMA */
ecr &= ~ATPPC_ENABLE_DMA;
atppc_w_ecr(atppc, ecr);
atppc_barrier_w(atppc);
}
/* Read bytes in ECP mode using PIO transfers */
static void
atppc_ecp_read_pio(struct atppc_softc *atppc, unsigned int *length,
unsigned char ecr)
{
/* Disable DMA */
ecr &= ~ATPPC_ENABLE_DMA;
atppc_w_ecr(atppc, ecr);
atppc_barrier_w(atppc);
/* Read from FIFO */
atppc_r_fifo_multi(atppc, atppc->sc_inbstart, *length);
}
/* Handle errors for ECP reads */
static void
atppc_ecp_read_error(struct atppc_softc *atppc)
{
unsigned char ecr = atppc_r_ecr(atppc);
/* Abort DMA if not finished */
if (atppc->sc_dmastat == ATPPC_DMA_STARTED) {
atppc->sc_dma_abort(atppc);
ATPPC_DPRINTF(("%s: DMA interrupted.\n", __func__));
}
/* Check for invalid states */
if ((ecr & ATPPC_FIFO_EMPTY) && (ecr & ATPPC_FIFO_FULL)) {
ATPPC_DPRINTF(("%s: FIFO full+empty bits set.\n", __func__));
ATPPC_DPRINTF(("%s: resetting FIFO.\n", __func__));
atppc_w_ecr(atppc, ATPPC_ECR_PS2);
atppc_barrier_w(atppc);
}
}
/*
* Functions that write bytes to port from buffer: called from atppc_write()
* function depending on current chipset mode. Returns number of bytes moved.
*/
/* Write bytes in std/bidirectional mode */
static void
atppc_std_write(struct atppc_softc * const atppc)
{
unsigned char ctr;
ctr = atppc_r_ctr(atppc);
atppc_barrier_r(atppc);
/* Enable interrupts if needed */
if (atppc->sc_use & ATPPC_USE_INTR) {
if (!(ctr & IRQENABLE)) {
ctr |= IRQENABLE;
atppc_w_ctr(atppc, ctr);
atppc_barrier_w(atppc);
}
}
while (atppc->sc_outbstart < (atppc->sc_outb + atppc->sc_outb_nbytes)) {
/* Wait for peripheral to become ready for MAXBUSYWAIT */
atppc->sc_outerr = atppc_poll_str(atppc, SPP_READY, SPP_MASK);
if (atppc->sc_outerr)
return;
/* Put data in data register */
atppc_w_dtr(atppc, *(atppc->sc_outbstart));
atppc_barrier_w(atppc);
DELAY(1);
/* Pulse strobe to indicate valid data on lines */
ctr |= STROBE;
atppc_w_ctr(atppc, ctr);
atppc_barrier_w(atppc);
DELAY(1);
ctr &= ~STROBE;
atppc_w_ctr(atppc, ctr);
atppc_barrier_w(atppc);
/* Wait for nACK for MAXBUSYWAIT */
if (atppc->sc_use & ATPPC_USE_INTR) {
atppc->sc_outerr = atppc_wait_interrupt(atppc,
&atppc->sc_out_cv, ATPPC_IRQ_nACK);
if (atppc->sc_outerr)
return;
} else {
/* Try to catch the pulsed acknowledgement */
atppc->sc_outerr = atppc_poll_str(atppc, 0, nACK);
if (atppc->sc_outerr)
return;
atppc->sc_outerr = atppc_poll_str(atppc, nACK, nACK);
if (atppc->sc_outerr)
return;
}
/* Update buffer position, byte count and counter */
atppc->sc_outbstart++;
}
}
/* Write bytes in EPP mode */
static void
atppc_epp_write(struct atppc_softc *atppc)
{
if (atppc->sc_epp == ATPPC_EPP_1_9) {
{
uint8_t str;
int i;
atppc_reset_epp_timeout(atppc->sc_dev);
for (i = 0; i < atppc->sc_outb_nbytes; i++) {
atppc_w_eppD(atppc, *(atppc->sc_outbstart));
atppc_barrier_w(atppc);
str = atppc_r_str(atppc);
atppc_barrier_r(atppc);
if (str & TIMEOUT) {
atppc->sc_outerr = EIO;
break;
}
atppc->sc_outbstart++;
}
}
} else {
/* Write data block to EPP data register */
atppc_w_eppD_multi(atppc, atppc->sc_outbstart,
atppc->sc_outb_nbytes);
atppc_barrier_w(atppc);
/* Update buffer position, byte count and counter */
atppc->sc_outbstart += atppc->sc_outb_nbytes;
}
return;
}
/* Write bytes in ECP/Fast Centronics mode */
static void
atppc_fifo_write(struct atppc_softc * const atppc)
{
unsigned char ctr;
unsigned char ecr;
const unsigned char ctr_sav = atppc_r_ctr(atppc);
const unsigned char ecr_sav = atppc_r_ecr(atppc);
ctr = ctr_sav;
ecr = ecr_sav;
atppc_barrier_r(atppc);
/* Reset and flush FIFO */
atppc_w_ecr(atppc, ATPPC_ECR_PS2);
atppc_barrier_w(atppc);
/* Disable nAck interrupts and initialize port bits */
ctr &= ~(IRQENABLE | STROBE | AUTOFEED);
atppc_w_ctr(atppc, ctr);
atppc_barrier_w(atppc);
/* Restore mode */
atppc_w_ecr(atppc, ecr);
atppc_barrier_w(atppc);
/* DMA or Programmed IO */
if ((atppc->sc_use & ATPPC_USE_DMA) &&
(atppc->sc_use & ATPPC_USE_INTR)) {
atppc_fifo_write_dma(atppc, ecr, ctr);
} else {
atppc_fifo_write_pio(atppc, ecr, ctr);
}
/* Restore original register values */
atppc_w_ctr(atppc, ctr_sav);
atppc_w_ecr(atppc, ecr_sav);
atppc_barrier_w(atppc);
}
static void
atppc_fifo_write_dma(struct atppc_softc * const atppc, unsigned char ecr,
unsigned char ctr)
{
unsigned int len;
unsigned int worklen;
for (len = (atppc->sc_outb + atppc->sc_outb_nbytes) -
atppc->sc_outbstart; len > 0; len = (atppc->sc_outb +
atppc->sc_outb_nbytes) - atppc->sc_outbstart) {
/* Wait for device to become ready */
atppc->sc_outerr = atppc_poll_str(atppc, SPP_READY, SPP_MASK);
if (atppc->sc_outerr)
return;
/* Reset chipset for next DMA transfer */
atppc_w_ecr(atppc, ATPPC_ECR_PS2);
atppc_barrier_w(atppc);
atppc_w_ecr(atppc, ecr);
atppc_barrier_w(atppc);
/* Limit transfer to maximum DMA size and start it */
worklen = uimin(len, atppc->sc_dma_maxsize);
atppc->sc_dmastat = ATPPC_DMA_INIT;
atppc->sc_dma_start(atppc, atppc->sc_outbstart,
worklen, ATPPC_DMA_MODE_WRITE);
atppc->sc_dmastat = ATPPC_DMA_STARTED;
/* Enable interrupts, DMA */
ecr &= ~ATPPC_SERVICE_INTR;
ecr |= ATPPC_ENABLE_DMA;
atppc_w_ecr(atppc, ecr);
atppc_barrier_w(atppc);
/* Wait for DMA completion */
atppc->sc_outerr = atppc_wait_interrupt(atppc,
&atppc->sc_out_cv, ATPPC_IRQ_DMA);
if (atppc->sc_outerr) {
atppc_fifo_write_error(atppc, worklen);
return;
}
/* Get register value recorded by interrupt handler */
ecr = atppc->sc_ecr_intr;
/* Clear DMA programming */
atppc->sc_dma_finish(atppc);
atppc->sc_dmastat = ATPPC_DMA_COMPLETE;
/* Disable DMA */
ecr &= ~ATPPC_ENABLE_DMA;
atppc_w_ecr(atppc, ecr);
atppc_barrier_w(atppc);
/* Wait for FIFO to empty */
for (;;) {
if (ecr & ATPPC_FIFO_EMPTY) {
if (ecr & ATPPC_FIFO_FULL) {
atppc->sc_outerr = EIO;
atppc_fifo_write_error(atppc, worklen);
return;
} else {
break;
}
}
/* Enable service interrupt */
ecr &= ~ATPPC_SERVICE_INTR;
atppc_w_ecr(atppc, ecr);
atppc_barrier_w(atppc);
atppc->sc_outerr = atppc_wait_interrupt(atppc,
&atppc->sc_out_cv, ATPPC_IRQ_FIFO);
if (atppc->sc_outerr) {
atppc_fifo_write_error(atppc, worklen);
return;
}
/* Get register value recorded by interrupt handler */
ecr = atppc->sc_ecr_intr;
}
/* Update pointer */
atppc->sc_outbstart += worklen;
}
}
static void
atppc_fifo_write_pio(struct atppc_softc * const atppc, unsigned char ecr,
unsigned char ctr)
{
unsigned int len;
unsigned int worklen;
unsigned int timecount;
/* Disable DMA */
ecr &= ~ATPPC_ENABLE_DMA;
atppc_w_ecr(atppc, ecr);
atppc_barrier_w(atppc);
for (len = (atppc->sc_outb + atppc->sc_outb_nbytes) -
atppc->sc_outbstart; len > 0; len = (atppc->sc_outb +
atppc->sc_outb_nbytes) - atppc->sc_outbstart) {
/* Wait for device to become ready */
atppc->sc_outerr = atppc_poll_str(atppc, SPP_READY, SPP_MASK);
if (atppc->sc_outerr)
return;
/* Limit transfer to minimum of space in FIFO and buffer */
worklen = uimin(len, atppc->sc_fifo);
/* Write to FIFO */
atppc_w_fifo_multi(atppc, atppc->sc_outbstart, worklen);
timecount = 0;
if (atppc->sc_use & ATPPC_USE_INTR) {
ecr = atppc_r_ecr(atppc);
atppc_barrier_w(atppc);
/* Wait for interrupt */
for (;;) {
if (ecr & ATPPC_FIFO_EMPTY) {
if (ecr & ATPPC_FIFO_FULL) {
atppc->sc_outerr = EIO;
atppc_fifo_write_error(atppc,
worklen);
return;
} else {
break;
}
}
/* Enable service interrupt */
ecr &= ~ATPPC_SERVICE_INTR;
atppc_w_ecr(atppc, ecr);
atppc_barrier_w(atppc);
atppc->sc_outerr = atppc_wait_interrupt(atppc,
&atppc->sc_out_cv, ATPPC_IRQ_FIFO);
if (atppc->sc_outerr) {
atppc_fifo_write_error(atppc, worklen);
return;
}
/* Get ECR value saved by interrupt handler */
ecr = atppc->sc_ecr_intr;
}
} else {
for (; timecount < ((MAXBUSYWAIT/hz)*1000000);
timecount++) {
ecr = atppc_r_ecr(atppc);
atppc_barrier_r(atppc);
if (ecr & ATPPC_FIFO_EMPTY) {
if (ecr & ATPPC_FIFO_FULL) {
atppc->sc_outerr = EIO;
atppc_fifo_write_error(atppc,
worklen);
return;
} else {
break;
}
}
DELAY(1);
}
if (((timecount*hz)/1000000) >= MAXBUSYWAIT) {
atppc->sc_outerr = EIO;
atppc_fifo_write_error(atppc, worklen);
return;
}
}
/* Update pointer */
atppc->sc_outbstart += worklen;
}
}
static void
atppc_fifo_write_error(struct atppc_softc * const atppc,
const unsigned int worklen)
{
unsigned char ecr = atppc_r_ecr(atppc);
/* Abort DMA if not finished */
if (atppc->sc_dmastat == ATPPC_DMA_STARTED) {
atppc->sc_dma_abort(atppc);
ATPPC_DPRINTF(("%s: DMA interrupted.\n", __func__));
}
/* Check for invalid states */
if ((ecr & ATPPC_FIFO_EMPTY) && (ecr & ATPPC_FIFO_FULL)) {
ATPPC_DPRINTF(("%s: FIFO full+empty bits set.\n", __func__));
} else if (!(ecr & ATPPC_FIFO_EMPTY)) {
unsigned char ctr = atppc_r_ctr(atppc);
int bytes_left;
int i;
ATPPC_DPRINTF(("%s(%s): FIFO not empty.\n", __func__,
device_xname(atppc->sc_dev)));
/* Drive strobe low to stop data transfer */
ctr &= ~STROBE;
atppc_w_ctr(atppc, ctr);
atppc_barrier_w(atppc);
/* Determine how many bytes remain in FIFO */
for (i = 0; i < atppc->sc_fifo; i++) {
atppc_w_fifo(atppc, (unsigned char)i);
ecr = atppc_r_ecr(atppc);
atppc_barrier_r(atppc);
if (ecr & ATPPC_FIFO_FULL)
break;
}
bytes_left = (atppc->sc_fifo) - (i + 1);
ATPPC_DPRINTF(("%s: %d bytes left in FIFO.\n", __func__,
bytes_left));
/* Update counter */
atppc->sc_outbstart += (worklen - bytes_left);
} else {
/* Update counter */
atppc->sc_outbstart += worklen;
}
ATPPC_DPRINTF(("%s: resetting FIFO.\n", __func__));
atppc_w_ecr(atppc, ATPPC_ECR_PS2);
atppc_barrier_w(atppc);
}
/*
* Poll status register using mask and status for MAXBUSYWAIT.
* Returns 0 if device ready, error value otherwise.
*/
static int
atppc_poll_str(const struct atppc_softc * const atppc, const u_int8_t status,
const u_int8_t mask)
{
unsigned int timecount;
u_int8_t str;
int error = EIO;
/* Wait for str to have status for MAXBUSYWAIT */
for (timecount = 0; timecount < ((MAXBUSYWAIT/hz)*1000000);
timecount++) {
str = atppc_r_str(atppc);
atppc_barrier_r(atppc);
if ((str & mask) == status) {
error = 0;
break;
}
DELAY(1);
}
return error;
}
/* Wait for interrupt for MAXBUSYWAIT: returns 0 if acknowledge received. */
static int
atppc_wait_interrupt(struct atppc_softc * const atppc, kcondvar_t *cv,
const u_int8_t irqstat)
{
int error = EIO;
atppc->sc_irqstat &= ~irqstat;
/* Wait for interrupt for MAXBUSYWAIT */
error = cv_timedwait_sig(cv, &atppc->sc_lock, MAXBUSYWAIT);
if (!(error) && (atppc->sc_irqstat & irqstat)) {
atppc->sc_irqstat &= ~irqstat;
error = 0;
}
return error;
}
|