summaryrefslogtreecommitdiffstats
path: root/drivers/sbus/char/zs.c
blob: 8875f7dc768dec0693faaad1fb60f8771990c441 (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
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
/* $Id: zs.c,v 1.3 1997/09/04 14:57:34 jj Exp $
 * zs.c: Zilog serial port driver for the Sparc.
 *
 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
 * Copyright (C) 1996 Eddie C. Dost   (ecd@skynet.be)
 * Fixes by Pete A. Zaitcev <zaitcev@ipmce.su>.
 */

#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/config.h>
#include <linux/major.h>
#include <linux/string.h>
#include <linux/fcntl.h>
#include <linux/mm.h>
#include <linux/kernel.h>
#include <linux/keyboard.h>
#include <linux/console.h>
#include <linux/delay.h>
#include <linux/version.h>
#include <linux/init.h>

#include <asm/io.h>
#include <asm/irq.h>
#include <asm/oplib.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <asm/bitops.h>
#include <asm/kdebug.h>

#include <asm/sbus.h>
#ifdef __sparc_v9__
#include <asm/fhc.h>
#endif

#include "sunserial.h"
#include "zs.h"
#include "sunkbd.h"
#include "sunmouse.h"

static int num_serial = 2; /* sun4/sun4c/sun4m - Two chips on board. */
#define NUM_SERIAL num_serial
#define NUM_CHANNELS (NUM_SERIAL * 2)

#define KEYBOARD_LINE 0x2
#define MOUSE_LINE    0x3

struct sun_zslayout **zs_chips;
struct sun_zschannel **zs_channels;
struct sun_zschannel *zs_conschan;
struct sun_zschannel *zs_mousechan;
struct sun_zschannel *zs_kbdchan;
struct sun_zschannel *zs_kgdbchan;
int *zs_nodes;

struct sun_serial *zs_soft;
struct sun_serial *zs_chain;  /* IRQ servicing chain */
int zilog_irq;

struct tty_struct *zs_ttys;
/** struct tty_struct *zs_constty; **/

/* Console hooks... */
static int zs_cons_chanout = 0;
static int zs_cons_chanin = 0;
struct sun_serial *zs_consinfo = 0;

static unsigned char kgdb_regs[16] = {
	0, 0, 0,                     /* write 0, 1, 2 */
	(Rx8 | RxENAB),              /* write 3 */
	(X16CLK | SB1 | PAR_EVEN),   /* write 4 */
	(DTR | Tx8 | TxENAB),        /* write 5 */
	0, 0, 0,                     /* write 6, 7, 8 */
	(NV),                        /* write 9 */
	(NRZ),                       /* write 10 */
	(TCBR | RCBR),               /* write 11 */
	0, 0,                        /* BRG time constant, write 12 + 13 */
	(BRSRC | BRENAB),            /* write 14 */
	(DCDIE)                      /* write 15 */
};

static unsigned char zscons_regs[16] = {
	0,                           /* write 0 */
	(EXT_INT_ENAB | INT_ALL_Rx), /* write 1 */
	0,                           /* write 2 */
	(Rx8 | RxENAB),              /* write 3 */
	(X16CLK),                    /* write 4 */
	(DTR | Tx8 | TxENAB),        /* write 5 */
	0, 0, 0,                     /* write 6, 7, 8 */
	(NV | MIE),                  /* write 9 */
	(NRZ),                       /* write 10 */
	(TCBR | RCBR),               /* write 11 */
	0, 0,                        /* BRG time constant, write 12 + 13 */
	(BRSRC | BRENAB),            /* write 14 */
	(DCDIE | CTSIE | TxUIE | BRKIE) /* write 15 */
};

#define ZS_CLOCK         4915200   /* Zilog input clock rate */

DECLARE_TASK_QUEUE(tq_serial);

struct tty_driver serial_driver, callout_driver;
static int serial_refcount;

/* serial subtype definitions */
#define SERIAL_TYPE_NORMAL	1
#define SERIAL_TYPE_CALLOUT	2
  
/* number of characters left in xmit buffer before we ask for more */
#define WAKEUP_CHARS 256

/* Debugging... DEBUG_INTR is bad to use when one of the zs
 * lines is your console ;(
 */
#undef SERIAL_DEBUG_INTR
#undef SERIAL_DEBUG_OPEN
#undef SERIAL_DEBUG_FLOW

#define RS_STROBE_TIME 10
#define RS_ISR_PASS_LIMIT 256

#define _INLINE_ inline

int zs_init(void);
void zs_cons_hook(int, int, int);
void zs_kgdb_hook(int);

static void change_speed(struct sun_serial *info);

static struct tty_struct **serial_table;
static struct termios **serial_termios;
static struct termios **serial_termios_locked;

#ifndef MIN
#define MIN(a,b)	((a) < (b) ? (a) : (b))
#endif

/*
 * tmp_buf is used as a temporary buffer by serial_write.  We need to
 * lock it in case the memcpy_fromfs blocks while swapping in a page,
 * and some other program tries to do a serial write at the same time.
 * Since the lock will only come under contention when the system is
 * swapping and available memory is low, it makes sense to share one
 * buffer across all the serial ports, since it significantly saves
 * memory if large numbers of serial ports are open.
 */
static unsigned char tmp_buf[4096]; /* This is cheating */
static struct semaphore tmp_buf_sem = MUTEX;

static inline int serial_paranoia_check(struct sun_serial *info,
					dev_t device, const char *routine)
{
#ifdef SERIAL_PARANOIA_CHECK
	static const char *badmagic =
		"Warning: bad magic number for serial struct (%d, %d) in %s\n";
	static const char *badinfo =
		"Warning: null sun_serial for (%d, %d) in %s\n";

	if (!info) {
		printk(badinfo, MAJOR(device), MINOR(device), routine);
		return 1;
	}
	if (info->magic != SERIAL_MAGIC) {
		printk(badmagic, MAJOR(device), MINOR(device), routine);
		return 1;
	}
#endif
	return 0;
}

/*
 * This is used to figure out the divisor speeds and the timeouts
 */
static int baud_table[] = {
	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
	9600, 19200, 38400, 76800, 0 };

/* 
 * Reading and writing Zilog8530 registers.  The delays are to make this
 * driver work on the Sun4 which needs a settling delay after each chip
 * register access, other machines handle this in hardware via auxiliary
 * flip-flops which implement the settle time we do in software.
 */
static inline unsigned char read_zsreg(struct sun_zschannel *channel,
				       unsigned char reg)
{
	unsigned char retval;

	channel->control = reg;
	udelay(5);
	retval = channel->control;
	udelay(5);
	return retval;
}

static inline void write_zsreg(struct sun_zschannel *channel,
			       unsigned char reg, unsigned char value)
{
	channel->control = reg;
	udelay(5);
	channel->control = value;
	udelay(5);
}

static inline void load_zsregs(struct sun_serial *info, unsigned char *regs)
{
	unsigned long flags;
	struct sun_zschannel *channel = info->zs_channel;
	unsigned char stat;
	int i;

	for (i = 0; i < 1000; i++) {
		stat = read_zsreg(channel, R1);
		if (stat & ALL_SNT)
			break;
		udelay(100);
	}
	write_zsreg(channel, R3, 0);
	ZS_CLEARSTAT(channel);
	ZS_CLEARERR(channel);
	ZS_CLEARFIFO(channel);

	/* Load 'em up */
	save_flags(flags); cli();
	if (info->channelA)
		write_zsreg(channel, R9, CHRA);
	else
		write_zsreg(channel, R9, CHRB);
	udelay(20);	/* wait for some old sun4's */
	write_zsreg(channel, R4, regs[R4]);
	write_zsreg(channel, R3, regs[R3] & ~RxENAB);
	write_zsreg(channel, R5, regs[R5] & ~TxENAB);
	write_zsreg(channel, R9, regs[R9] & ~MIE);
	write_zsreg(channel, R10, regs[R10]);
	write_zsreg(channel, R11, regs[R11]);
	write_zsreg(channel, R12, regs[R12]);
	write_zsreg(channel, R13, regs[R13]);
	write_zsreg(channel, R14, regs[R14] & ~BRENAB);
	write_zsreg(channel, R14, regs[R14]);
	write_zsreg(channel, R14, (regs[R14] & ~SNRZI) | BRENAB);
	write_zsreg(channel, R3, regs[R3]);
	write_zsreg(channel, R5, regs[R5]);
	write_zsreg(channel, R15, regs[R15]);
	write_zsreg(channel, R0, RES_EXT_INT);
	write_zsreg(channel, R0, ERR_RES);
	write_zsreg(channel, R1, regs[R1]);
	write_zsreg(channel, R9, regs[R9]);
	restore_flags(flags);
}

static inline void zs_put_char(struct sun_zschannel *channel, char ch)
{
	int loops = 0;

	while((channel->control & Tx_BUF_EMP) == 0 && loops < 10000) {
		loops++;
		udelay(5);
	}
	channel->data = ch;
	udelay(5);
}

/* Sets or clears DTR/RTS on the requested line */
static inline void zs_rtsdtr(struct sun_serial *ss, int set)
{
	unsigned long flags;

	save_flags(flags); cli();
	if(set) {
		ss->curregs[5] |= (RTS | DTR);
		write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
	} else {
		ss->curregs[5] &= ~(RTS | DTR);
		write_zsreg(ss->zs_channel, 5, ss->curregs[5]);
	}
	restore_flags(flags);
	return;
}

static inline void kgdb_chaninit(struct sun_serial *ss, int intson, int bps)
{
	int brg;

	if(intson) {
		kgdb_regs[R1] = INT_ALL_Rx;
		kgdb_regs[R9] |= MIE;
	} else {
		kgdb_regs[R1] = 0;
		kgdb_regs[R9] &= ~MIE;
	}
	brg = BPS_TO_BRG(bps, ZS_CLOCK/16);
	kgdb_regs[R12] = (brg & 255);
	kgdb_regs[R13] = ((brg >> 8) & 255);
	load_zsregs(ss, kgdb_regs);
}

/*
 * ------------------------------------------------------------
 * zs_stop() and zs_start()
 *
 * This routines are called before setting or resetting tty->stopped.
 * They enable or disable transmitter interrupts, as necessary.
 * ------------------------------------------------------------
 */
static void zs_stop(struct tty_struct *tty)
{
	struct sun_serial *info = (struct sun_serial *)tty->driver_data;
	unsigned long flags;

	if (serial_paranoia_check(info, tty->device, "zs_stop"))
		return;
	
	save_flags(flags); cli();
	if (info->curregs[5] & TxENAB) {
		info->curregs[5] &= ~TxENAB;
		write_zsreg(info->zs_channel, 5, info->curregs[5]);
	}
	restore_flags(flags);
}

static void zs_start(struct tty_struct *tty)
{
	struct sun_serial *info = (struct sun_serial *)tty->driver_data;
	unsigned long flags;
	
	if (serial_paranoia_check(info, tty->device, "zs_start"))
		return;
	
	save_flags(flags); cli();
	if (info->xmit_cnt && info->xmit_buf && !(info->curregs[5] & TxENAB)) {
		info->curregs[5] |= TxENAB;
		write_zsreg(info->zs_channel, 5, info->curregs[5]);
	}
	restore_flags(flags);
}

/* Drop into either the boot monitor or kadb upon receiving a break
 * from keyboard/console input.
 */
void batten_down_hatches(void)
{
	/* If we are doing kadb, we call the debugger
	 * else we just drop into the boot monitor.
	 * Note that we must flush the user windows
	 * first before giving up control.
	 */
	printk("\n");
	flush_user_windows();
#ifndef __sparc_v9__
	if((((unsigned long)linux_dbvec)>=DEBUG_FIRSTVADDR) &&
	   (((unsigned long)linux_dbvec)<=DEBUG_LASTVADDR))
		sp_enter_debugger();
	else
#endif
		prom_cmdline();

	/* XXX We want to notify the keyboard driver that all
	 * XXX keys are in the up state or else weird things
	 * XXX happen...
	 */

	return;
}


/*
 * ----------------------------------------------------------------------
 *
 * Here starts the interrupt handling routines.  All of the following
 * subroutines are declared as inline and are folded into
 * zs_interrupt().  They were separated out for readability's sake.
 *
 * Note: zs_interrupt() is a "fast" interrupt, which means that it
 * runs with interrupts turned off.  People who may want to modify
 * zs_interrupt() should try to keep the interrupt handler as fast as
 * possible.  After you are done making modifications, it is not a bad
 * idea to do:
 * 
 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
 *
 * and look at the resulting assemble code in serial.s.
 *
 * 				- Ted Ts'o (tytso@mit.edu), 7-Mar-93
 * -----------------------------------------------------------------------
 */

/*
 * This routine is used by the interrupt handler to schedule
 * processing in the software interrupt portion of the driver.
 */
static _INLINE_ void zs_sched_event(struct sun_serial *info,
				  int event)
{
	info->event |= 1 << event;
	queue_task(&info->tqueue, &tq_serial);
	mark_bh(SERIAL_BH);
}

#ifndef __sparc_v9__
extern void breakpoint(void);  /* For the KGDB frame character */
#endif

static _INLINE_ void receive_chars(struct sun_serial *info, struct pt_regs *regs)
{
	struct tty_struct *tty = info->tty;
	unsigned char ch, stat;

	do {
		ch = (info->zs_channel->data) & info->parity_mask;
		udelay(5);

		/* If this is the console keyboard, we need to handle
		 * L1-A's here.
		 */
		if(info->cons_keyb) {
			if(ch == SUNKBD_RESET) {
				l1a_state.kbd_id = 1;
				l1a_state.l1_down = 0;
			} else if(l1a_state.kbd_id) {
				l1a_state.kbd_id = 0;
			} else if(ch == SUNKBD_L1) {
				l1a_state.l1_down = 1;
			} else if(ch == (SUNKBD_L1|SUNKBD_UP)) {
				l1a_state.l1_down = 0;
			} else if(ch == SUNKBD_A && l1a_state.l1_down) {
				/* whee... */
				batten_down_hatches();
				/* Continue execution... */
				l1a_state.l1_down = 0;
				l1a_state.kbd_id = 0;
				return;
			}
			sunkbd_inchar(ch, regs);
			return;
		}
		if(info->cons_mouse) {
			sun_mouse_inbyte(ch);
			return;
		}
		if(info->is_cons) {
			if(ch==0) {
				/* whee, break received */
				batten_down_hatches();
				/* Continue execution... */
				return;
#if 0
			} else if (ch == 1) {
				show_state();
				return;
			} else if (ch == 2) {
				show_buffers();
				return;
#endif
			}
			/* It is a 'keyboard interrupt' ;-) */
			wake_up(&keypress_wait);
		}
#ifndef __sparc_v9__
		/* Look for kgdb 'stop' character, consult the gdb
		 * documentation for remote target debugging and
		 * arch/sparc/kernel/sparc-stub.c to see how all this works.
		 */
		if((info->kgdb_channel) && (ch =='\003')) {
			breakpoint();
			return;
		}
#endif
		if(!tty)
			return;

		if (tty->flip.count >= TTY_FLIPBUF_SIZE)
			break;

		tty->flip.count++;
		*tty->flip.flag_buf_ptr++ = 0;
		*tty->flip.char_buf_ptr++ = ch;

		/* Check if we have another character... */
		stat = info->zs_channel->control;
		udelay(5);
		if (!(stat & Rx_CH_AV))
			break;

		/* ... and see if it is clean. */
		stat = read_zsreg(info->zs_channel, R1);
	} while (!(stat & (PAR_ERR | Rx_OVR | CRC_ERR)));

	queue_task(&tty->flip.tqueue, &tq_timer);
}

static _INLINE_ void transmit_chars(struct sun_serial *info)
{
	struct tty_struct *tty = info->tty;

	if (info->x_char) {
		/* Send next char */
		zs_put_char(info->zs_channel, info->x_char);
		info->x_char = 0;
		return;
	}

	if((info->xmit_cnt <= 0) || (tty != 0 && tty->stopped)) {
		/* That's peculiar... */
		info->zs_channel->control = RES_Tx_P;
		udelay(5);
		return;
	}

	/* Send char */
	zs_put_char(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
	info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
	info->xmit_cnt--;

	if (info->xmit_cnt < WAKEUP_CHARS)
		zs_sched_event(info, RS_EVENT_WRITE_WAKEUP);

	if(info->xmit_cnt <= 0) {
		info->zs_channel->control = RES_Tx_P;
		udelay(5);
	}
}

static _INLINE_ void status_handle(struct sun_serial *info)
{
	unsigned char status;

	/* Get status from Read Register 0 */
	status = info->zs_channel->control;
	udelay(5);
	/* Clear status condition... */
	info->zs_channel->control = RES_EXT_INT;
	udelay(5);

#if 0
	if(status & DCD) {
		if((info->tty->termios->c_cflag & CRTSCTS) &&
		   ((info->curregs[3] & AUTO_ENAB)==0)) {
			info->curregs[3] |= AUTO_ENAB;
			write_zsreg(info->zs_channel, 3, info->curregs[3]);
		}
	} else {
		if((info->curregs[3] & AUTO_ENAB)) {
			info->curregs[3] &= ~AUTO_ENAB;
			write_zsreg(info->zs_channel, 3, info->curregs[3]);
		}
	}
#endif
	/* Whee, if this is console input and this is a
	 * 'break asserted' status change interrupt, call
	 * the boot prom.
	 */
	if((status & BRK_ABRT) && info->break_abort)
		batten_down_hatches();

	/* XXX Whee, put in a buffer somewhere, the status information
	 * XXX whee whee whee... Where does the information go...
	 */
	return;
}

static _INLINE_ void special_receive(struct sun_serial *info)
{
	struct tty_struct *tty = info->tty;
	unsigned char ch, stat;

	stat = read_zsreg(info->zs_channel, R1);
	if (stat & (PAR_ERR | Rx_OVR | CRC_ERR)) {
		ch = info->zs_channel->data;
		udelay(5);
	}

	if (!tty)
		goto clear;

	if (tty->flip.count >= TTY_FLIPBUF_SIZE)
		goto done;

	tty->flip.count++;
	if(stat & PAR_ERR)
		*tty->flip.flag_buf_ptr++ = TTY_PARITY;
	else if(stat & Rx_OVR)
		*tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
	else if(stat & CRC_ERR)
		*tty->flip.flag_buf_ptr++ = TTY_FRAME;

done:
	queue_task(&tty->flip.tqueue, &tq_timer);
clear:
	info->zs_channel->control = ERR_RES;
	udelay(5);
}


/*
 * This is the serial driver's generic interrupt routine
 */
void zs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
{
	struct sun_serial *info;
	unsigned char zs_intreg;
	int i;

	info = (struct sun_serial *)dev_id;
	for (i = 0; i < NUM_SERIAL; i++) {
		zs_intreg = read_zsreg(info->zs_next->zs_channel, 2);
		zs_intreg &= STATUS_MASK;

		/* NOTE: The read register 2, which holds the irq status,
		 *       does so for both channels on each chip.  Although
		 *       the status value itself must be read from the B
		 *       channel and is only valid when read from channel B.
		 *       When read from channel A, read register 2 contains
		 *       the value written to write register 2.
		 */

		/* Channel A -- /dev/ttya or /dev/kbd, could be the console */
		if (zs_intreg == CHA_Rx_AVAIL) {
			receive_chars(info, regs);
			return;
		}
		if(zs_intreg == CHA_Tx_EMPTY) {
			transmit_chars(info);
			return;
		}
		if (zs_intreg == CHA_EXT_STAT) {
			status_handle(info);
			return;
		}
		if (zs_intreg == CHA_SPECIAL) {
			special_receive(info);
			return;
		}

		/* Channel B -- /dev/ttyb or /dev/mouse, could be the console */
		if(zs_intreg == CHB_Rx_AVAIL) {
			receive_chars(info->zs_next, regs);
			return;
		}
		if(zs_intreg == CHB_Tx_EMPTY) {
			transmit_chars(info->zs_next);
			return;
		}
		if (zs_intreg == CHB_EXT_STAT) {
			status_handle(info->zs_next);
			return;
		}

		/* NOTE: The default value for the IRQ status in read register
		 *       2 in channel B is CHB_SPECIAL, so we need to look at
		 *       read register 3 in channel A to check if this is a
		 *       real interrupt, or just the default value.
		 *       Yes... broken hardware...
		 */

		zs_intreg = read_zsreg(info->zs_channel, 3);
		if (zs_intreg & CHBRxIP) {
			special_receive(info->zs_next);
			return;
		}
		info = info->zs_next->zs_next;
	}
}

/*
 * -------------------------------------------------------------------
 * Here ends the serial interrupt routines.
 * -------------------------------------------------------------------
 */

/*
 * This routine is used to handle the "bottom half" processing for the
 * serial driver, known also the "software interrupt" processing.
 * This processing is done at the kernel interrupt level, after the
 * zs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
 * is where time-consuming activities which can not be done in the
 * interrupt driver proper are done; the interrupt driver schedules
 * them using zs_sched_event(), and they get done here.
 */
static void do_serial_bh(void)
{
	run_task_queue(&tq_serial);
}

static void do_softint(void *private_)
{
	struct sun_serial	*info = (struct sun_serial *) private_;
	struct tty_struct	*tty;
	
	tty = info->tty;
	if (!tty)
		return;

	if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
		if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
		    tty->ldisc.write_wakeup)
			(tty->ldisc.write_wakeup)(tty);
		wake_up_interruptible(&tty->write_wait);
	}
}

/*
 * This routine is called from the scheduler tqueue when the interrupt
 * routine has signalled that a hangup has occurred.  The path of
 * hangup processing is:
 *
 * 	serial interrupt routine -> (scheduler tqueue) ->
 * 	do_serial_hangup() -> tty->hangup() -> zs_hangup()
 * 
 */
static void do_serial_hangup(void *private_)
{
	struct sun_serial	*info = (struct sun_serial *) private_;
	struct tty_struct	*tty;
	
	tty = info->tty;
	if (!tty)
		return;
#ifdef SERIAL_DEBUG_OPEN
	printk("do_serial_hangup<%p: tty-%d\n",
		__builtin_return_address(0), info->line);
#endif

	tty_hangup(tty);
}


/*
 * This subroutine is called when the RS_TIMER goes off.  It is used
 * by the serial driver to handle ports that do not have an interrupt
 * (irq=0).  This doesn't work at all for 16450's, as a sun has a Z8530.
 */
 
static void zs_timer(void)
{
	printk("zs_timer called\n");
	prom_halt();
	return;
}

static int startup(struct sun_serial * info)
{
	unsigned long flags;

	if (info->flags & ZILOG_INITIALIZED)
		return 0;

	if (!info->xmit_buf) {
		info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
		if (!info->xmit_buf)
			return -ENOMEM;
	}

	save_flags(flags); cli();

#ifdef SERIAL_DEBUG_OPEN
	printk("Starting up tty-%d (irq %d)...\n", info->line, info->irq);
#endif

	/*
	 * Clear the FIFO buffers and disable them
	 * (they will be reenabled in change_speed())
	 */
	ZS_CLEARFIFO(info->zs_channel);
	info->xmit_fifo_size = 1;

	/*
	 * Clear the interrupt registers.
	 */
	info->zs_channel->control = ERR_RES;
	udelay(5);
	info->zs_channel->control = RES_H_IUS;
	udelay(5);

	/*
	 * Now, initialize the Zilog
	 */
	zs_rtsdtr(info, 1);

	/*
	 * Finally, enable sequencing and interrupts
	 */
	info->curregs[1] |= (info->curregs[1] & ~(RxINT_MASK)) |
				(EXT_INT_ENAB | INT_ALL_Rx);
	info->curregs[3] |= (RxENAB | Rx8);
	/* We enable Tx interrupts as needed. */
	info->curregs[5] |= (TxENAB | Tx8);
	info->curregs[9] |= (NV | MIE);
	write_zsreg(info->zs_channel, 3, info->curregs[3]);
	write_zsreg(info->zs_channel, 5, info->curregs[5]);
	write_zsreg(info->zs_channel, 9, info->curregs[9]);
	
	/*
	 * And clear the interrupt registers again for luck.
	 */
	info->zs_channel->control = ERR_RES;
	udelay(5);
	info->zs_channel->control = RES_H_IUS;
	udelay(5);

	if (info->tty)
		clear_bit(TTY_IO_ERROR, &info->tty->flags);
	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;

	/*
	 * Set up serial timers...
	 */
#if 0  /* Works well and stops the machine. */
	timer_table[RS_TIMER].expires = jiffies + 2;
	timer_active |= 1 << RS_TIMER;
#endif

	/*
	 * and set the speed of the serial port
	 */
	change_speed(info);

	info->flags |= ZILOG_INITIALIZED;
	restore_flags(flags);
	return 0;
}

/*
 * This routine will shutdown a serial port; interrupts are disabled, and
 * DTR is dropped if the hangup on close termio flag is on.
 */
static void shutdown(struct sun_serial * info)
{
	unsigned long	flags;

	if (!(info->flags & ZILOG_INITIALIZED))
		return;

#ifdef SERIAL_DEBUG_OPEN
	printk("Shutting down serial port %d (irq %d)....", info->line,
	       info->irq);
#endif
	
	save_flags(flags); cli(); /* Disable interrupts */
	
	if (info->xmit_buf) {
		free_page((unsigned long) info->xmit_buf);
		info->xmit_buf = 0;
	}

	if (info->tty)
		set_bit(TTY_IO_ERROR, &info->tty->flags);
	
	info->flags &= ~ZILOG_INITIALIZED;
	restore_flags(flags);
}

/*
 * This routine is called to set the UART divisor registers to match
 * the specified baud rate for a serial port.
 */
static void change_speed(struct sun_serial *info)
{
	unsigned short port;
	unsigned cflag;
	int	quot = 0;
	int	i;
	int	brg;

	if (!info->tty || !info->tty->termios)
		return;
	cflag = info->tty->termios->c_cflag;
	if (!(port = info->port))
		return;
	i = cflag & CBAUD;
	if (cflag & CBAUDEX) {
		i &= ~CBAUDEX;
		if (i != 5)
			info->tty->termios->c_cflag &= ~CBAUDEX;
		else
			i = 16;
	}
	if (i == 15) {
		if ((info->flags & ZILOG_SPD_MASK) == ZILOG_SPD_HI)
			i += 1;
		if ((info->flags & ZILOG_SPD_MASK) == ZILOG_SPD_CUST)
			quot = info->custom_divisor;
	}
	if (quot) {
		info->zs_baud = info->baud_base / quot;
		info->clk_divisor = 16;

		info->curregs[4] = X16CLK;
		info->curregs[11] = TCBR | RCBR;
		brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
		info->curregs[12] = (brg & 255);
		info->curregs[13] = ((brg >> 8) & 255);
		info->curregs[14] = BRSRC | BRENAB;
		zs_rtsdtr(info, 1);
	} else if (baud_table[i]) {
		info->zs_baud = baud_table[i];
		info->clk_divisor = 16;

		info->curregs[4] = X16CLK;
		info->curregs[11] = TCBR | RCBR;
		brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
		info->curregs[12] = (brg & 255);
		info->curregs[13] = ((brg >> 8) & 255);
		info->curregs[14] = BRSRC | BRENAB;
		zs_rtsdtr(info, 1);
	} else {
		zs_rtsdtr(info, 0);
		return;
	}

	/* byte size and parity */
	switch (cflag & CSIZE) {
	case CS5:
		info->curregs[3] &= ~(RxN_MASK);
		info->curregs[3] |= Rx5;
		info->curregs[5] &= ~(TxN_MASK);
		info->curregs[5] |= Tx5;
		info->parity_mask = 0x1f;
		break;
	case CS6:
		info->curregs[3] &= ~(RxN_MASK);
		info->curregs[3] |= Rx6;
		info->curregs[5] &= ~(TxN_MASK);
		info->curregs[5] |= Tx6;
		info->parity_mask = 0x3f;
		break;
	case CS7:
		info->curregs[3] &= ~(RxN_MASK);
		info->curregs[3] |= Rx7;
		info->curregs[5] &= ~(TxN_MASK);
		info->curregs[5] |= Tx7;
		info->parity_mask = 0x7f;
		break;
	case CS8:
	default: /* defaults to 8 bits */
		info->curregs[3] &= ~(RxN_MASK);
		info->curregs[3] |= Rx8;
		info->curregs[5] &= ~(TxN_MASK);
		info->curregs[5] |= Tx8;
		info->parity_mask = 0xff;
		break;
	}
	info->curregs[4] &= ~(0x0c);
	if (cflag & CSTOPB) {
		info->curregs[4] |= SB2;
	} else {
		info->curregs[4] |= SB1;
	}
	if (cflag & PARENB) {
		info->curregs[4] |= PAR_ENAB;
	} else {
		info->curregs[4] &= ~PAR_ENAB;
	}
	if (!(cflag & PARODD)) {
		info->curregs[4] |= PAR_EVEN;
	} else {
		info->curregs[4] &= ~PAR_EVEN;
	}

	/* Load up the new values */
	load_zsregs(info, info->curregs);

	return;
}

/* This is for mouse/keyboard output.
 * XXX mouse output??? can we send it commands??? XXX
 */
static void kbd_put_char(unsigned char ch)
{
	struct sun_zschannel *chan = zs_kbdchan;
	unsigned long flags;

	if(!chan)
		return;

	save_flags(flags); cli();
	zs_put_char(chan, ch);
	restore_flags(flags);
}

void mouse_put_char(char ch)
{
	struct sun_zschannel *chan = zs_mousechan;
	unsigned long flags;

	if(!chan)
		return;

	save_flags(flags); cli();
	zs_put_char(chan, ch);
	restore_flags(flags);
}


/* This is for console output over ttya/ttyb */
static void zs_cons_put_char(char ch)
{
	struct sun_zschannel *chan = zs_conschan;
	unsigned long flags;

	if(!chan)
		return;

	save_flags(flags); cli();
	zs_put_char(chan, ch);
	restore_flags(flags);
}

/* These are for receiving and sending characters under the kgdb
 * source level kernel debugger.
 */
void putDebugChar(char kgdb_char)
{
	struct sun_zschannel *chan = zs_kgdbchan;

	while((chan->control & Tx_BUF_EMP)==0)
		udelay(5);
	chan->data = kgdb_char;
}

char getDebugChar(void)
{
	struct sun_zschannel *chan = zs_kgdbchan;

	while((chan->control & Rx_CH_AV)==0)
		barrier();
	return chan->data;
}

/*
 * Fair output driver allows a process to speak.
 */
static void zs_fair_output(void)
{
	int left;		/* Output no more than that */
	unsigned long flags;
	struct sun_serial *info = zs_consinfo;
	char c;

	if (info == 0) return;
	if (info->xmit_buf == 0) return;

	save_flags(flags);  cli();
	left = info->xmit_cnt;
	while (left != 0) {
		c = info->xmit_buf[info->xmit_tail];
		info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
		info->xmit_cnt--;
		restore_flags(flags);

		zs_cons_put_char(c);

		cli();
		left = MIN(info->xmit_cnt, left-1);
	}

	/* Last character is being transmitted now (hopefully). */
	zs_conschan->control = RES_Tx_P;
	udelay(5);

	restore_flags(flags);
	return;
}

/*
 * zs_console_print is registered for printk.
 */
static void zs_console_print(const char *s, unsigned count)
{
	int i;

	for (i = 0; i < count; i++, s++) {
		if(*s == '\n')
			zs_cons_put_char('\r');
		zs_cons_put_char(*s);
	}

	/* Comment this if you want to have a strict interrupt-driven output */
	zs_fair_output();
}

static void zs_console_wait_key(void)
{
	sleep_on(&keypress_wait);
}

static int zs_console_device(void)
{
	extern int serial_console;
	
	return MKDEV(TTYAUX_MAJOR, 64 + serial_console - 1);
}

static void zs_flush_chars(struct tty_struct *tty)
{
	struct sun_serial *info = (struct sun_serial *)tty->driver_data;
	unsigned long flags;

	if (serial_paranoia_check(info, tty->device, "zs_flush_chars"))
		return;

	if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
	    !info->xmit_buf)
		return;

	/* Enable transmitter */
	save_flags(flags); cli();
	info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
	write_zsreg(info->zs_channel, 1, info->curregs[1]);
	info->curregs[5] |= TxENAB;
	write_zsreg(info->zs_channel, 5, info->curregs[5]);

	/*
	 * Send a first (bootstrapping) character. A best solution is
	 * to call transmit_chars() here which handles output in a
	 * generic way. Current transmit_chars() not only transmits,
	 * but resets interrupts also what we do not desire here.
	 * XXX Discuss with David.
	 */
	zs_put_char(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
	info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
	info->xmit_cnt--;

	restore_flags(flags);
}

static int zs_write(struct tty_struct * tty, int from_user,
		    const unsigned char *buf, int count)
{
	int	c, total = 0;
	struct sun_serial *info = (struct sun_serial *)tty->driver_data;
	unsigned long flags;

	if (serial_paranoia_check(info, tty->device, "zs_write"))
		return 0;

	if (!info || !info->xmit_buf)
		return 0;

	save_flags(flags);
	while (1) {
		cli();		
		c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
				   SERIAL_XMIT_SIZE - info->xmit_head));
		if (c <= 0)
			break;

		if (from_user) {
			down(&tmp_buf_sem);
			copy_from_user(tmp_buf, buf, c);
			c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
				       SERIAL_XMIT_SIZE - info->xmit_head));
			memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
			up(&tmp_buf_sem);
		} else
			memcpy(info->xmit_buf + info->xmit_head, buf, c);
		info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
		info->xmit_cnt += c;
		restore_flags(flags);
		buf += c;
		count -= c;
		total += c;
	}

	if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
		/* Enable transmitter */
		info->curregs[1] |= TxINT_ENAB|EXT_INT_ENAB;
		write_zsreg(info->zs_channel, 1, info->curregs[1]);
		info->curregs[5] |= TxENAB;
		write_zsreg(info->zs_channel, 5, info->curregs[5]);
	}
#if 1
	if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
		zs_put_char(info->zs_channel,
			    info->xmit_buf[info->xmit_tail++]);
		info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
		info->xmit_cnt--;
	}
#endif
	restore_flags(flags);
	return total;
}

static int zs_write_room(struct tty_struct *tty)
{
	struct sun_serial *info = (struct sun_serial *)tty->driver_data;
	int	ret;
				
	if (serial_paranoia_check(info, tty->device, "zs_write_room"))
		return 0;
	ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
	if (ret < 0)
		ret = 0;
	return ret;
}

static int zs_chars_in_buffer(struct tty_struct *tty)
{
	struct sun_serial *info = (struct sun_serial *)tty->driver_data;
				
	if (serial_paranoia_check(info, tty->device, "zs_chars_in_buffer"))
		return 0;
	return info->xmit_cnt;
}

static void zs_flush_buffer(struct tty_struct *tty)
{
	struct sun_serial *info = (struct sun_serial *)tty->driver_data;
				
	if (serial_paranoia_check(info, tty->device, "zs_flush_buffer"))
		return;
	cli();
	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
	sti();
	wake_up_interruptible(&tty->write_wait);
	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
	    tty->ldisc.write_wakeup)
		(tty->ldisc.write_wakeup)(tty);
}

/*
 * ------------------------------------------------------------
 * zs_throttle()
 * 
 * This routine is called by the upper-layer tty layer to signal that
 * incoming characters should be throttled.
 * ------------------------------------------------------------
 */
static void zs_throttle(struct tty_struct * tty)
{
	struct sun_serial *info = (struct sun_serial *)tty->driver_data;
#ifdef SERIAL_DEBUG_THROTTLE
	char	buf[64];
	
	printk("throttle %s: %d....\n", _tty_name(tty, buf),
	       tty->ldisc.chars_in_buffer(tty));
#endif

	if (serial_paranoia_check(info, tty->device, "zs_throttle"))
		return;
	
	if (I_IXOFF(tty))
		info->x_char = STOP_CHAR(tty);

	/* Turn off RTS line */
	cli();
	info->curregs[5] &= ~RTS;
	write_zsreg(info->zs_channel, 5, info->curregs[5]);
	sti();
}

static void zs_unthrottle(struct tty_struct * tty)
{
	struct sun_serial *info = (struct sun_serial *)tty->driver_data;
#ifdef SERIAL_DEBUG_THROTTLE
	char	buf[64];
	
	printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
	       tty->ldisc.chars_in_buffer(tty));
#endif

	if (serial_paranoia_check(info, tty->device, "zs_unthrottle"))
		return;
	
	if (I_IXOFF(tty)) {
		if (info->x_char)
			info->x_char = 0;
		else
			info->x_char = START_CHAR(tty);
	}

	/* Assert RTS line */
	cli();
	info->curregs[5] |= RTS;
	write_zsreg(info->zs_channel, 5, info->curregs[5]);
	sti();
}

/*
 * ------------------------------------------------------------
 * zs_ioctl() and friends
 * ------------------------------------------------------------
 */

static int get_serial_info(struct sun_serial * info,
			   struct serial_struct * retinfo)
{
	struct serial_struct tmp;
  
	if (!retinfo)
		return -EFAULT;
	memset(&tmp, 0, sizeof(tmp));
	tmp.type = info->type;
	tmp.line = info->line;
	tmp.port = info->port;
	tmp.irq = info->irq;
	tmp.flags = info->flags;
	tmp.baud_base = info->baud_base;
	tmp.close_delay = info->close_delay;
	tmp.closing_wait = info->closing_wait;
	tmp.custom_divisor = info->custom_divisor;
	copy_to_user_ret(retinfo,&tmp,sizeof(*retinfo), -EFAULT);
	return 0;
}

static int set_serial_info(struct sun_serial * info,
			   struct serial_struct * new_info)
{
	struct serial_struct new_serial;
	struct sun_serial old_info;
	int 			retval = 0;

	if (!new_info || copy_from_user(&new_serial,new_info,sizeof(new_serial)))
		return -EFAULT;
	old_info = *info;

	if (!suser()) {
		if ((new_serial.baud_base != info->baud_base) ||
		    (new_serial.type != info->type) ||
		    (new_serial.close_delay != info->close_delay) ||
		    ((new_serial.flags & ~ZILOG_USR_MASK) !=
		     (info->flags & ~ZILOG_USR_MASK)))
			return -EPERM;
		info->flags = ((info->flags & ~ZILOG_USR_MASK) |
			       (new_serial.flags & ZILOG_USR_MASK));
		info->custom_divisor = new_serial.custom_divisor;
		goto check_and_exit;
	}

	if (info->count > 1)
		return -EBUSY;

	/*
	 * OK, past this point, all the error checking has been done.
	 * At this point, we start making changes.....
	 */

	info->baud_base = new_serial.baud_base;
	info->flags = ((info->flags & ~ZILOG_FLAGS) |
			(new_serial.flags & ZILOG_FLAGS));
	info->custom_divisor = new_serial.custom_divisor;
	info->type = new_serial.type;
	info->close_delay = new_serial.close_delay;
	info->closing_wait = new_serial.closing_wait;

check_and_exit:
	retval = startup(info);
	return retval;
}

/*
 * get_lsr_info - get line status register info
 *
 * Purpose: Let user call ioctl() to get info when the UART physically
 * 	    is emptied.  On bus types like RS485, the transmitter must
 * 	    release the bus after transmitting. This must be done when
 * 	    the transmit shift register is empty, not be done when the
 * 	    transmit holding register is empty.  This functionality
 * 	    allows an RS485 driver to be written in user space. 
 */
static int get_lsr_info(struct sun_serial * info, unsigned int *value)
{
	unsigned char status;

	cli();
	status = info->zs_channel->control;
	sti();
	put_user_ret(status,value, -EFAULT);
	return 0;
}

static int get_modem_info(struct sun_serial * info, unsigned int *value)
{
	unsigned char status;
	unsigned int result;

	cli();
	status = info->zs_channel->control;
	sti();
	result =  ((info->curregs[5] & RTS) ? TIOCM_RTS : 0)
		| ((info->curregs[5] & DTR) ? TIOCM_DTR : 0)
		| ((status  & DCD) ? TIOCM_CAR : 0)
		| ((status  & SYNC) ? TIOCM_DSR : 0)
		| ((status  & CTS) ? TIOCM_CTS : 0);
	put_user_ret(result, value, -EFAULT);
	return 0;
}

static int set_modem_info(struct sun_serial * info, unsigned int cmd,
			  unsigned int *value)
{
	unsigned int arg;

	get_user_ret(arg, value, -EFAULT);
	switch (cmd) {
	case TIOCMBIS: 
		if (arg & TIOCM_RTS)
			info->curregs[5] |= RTS;
		if (arg & TIOCM_DTR)
			info->curregs[5] |= DTR;
		break;
	case TIOCMBIC:
		if (arg & TIOCM_RTS)
			info->curregs[5] &= ~RTS;
		if (arg & TIOCM_DTR)
			info->curregs[5] &= ~DTR;
		break;
	case TIOCMSET:
		info->curregs[5] = ((info->curregs[5] & ~(RTS | DTR))
			     | ((arg & TIOCM_RTS) ? RTS : 0)
			     | ((arg & TIOCM_DTR) ? DTR : 0));
		break;
	default:
		return -EINVAL;
	}
	cli();
	write_zsreg(info->zs_channel, 5, info->curregs[5]);
	sti();
	return 0;
}

/*
 * This routine sends a break character out the serial port.
 */
static void send_break(	struct sun_serial * info, int duration)
{
	if (!info->port)
		return;
	current->state = TASK_INTERRUPTIBLE;
	current->timeout = jiffies + duration;
	cli();
	write_zsreg(info->zs_channel, 5, (info->curregs[5] | SND_BRK));
	schedule();
	write_zsreg(info->zs_channel, 5, info->curregs[5]);
	sti();
}

static int zs_ioctl(struct tty_struct *tty, struct file * file,
		    unsigned int cmd, unsigned long arg)
{
	struct sun_serial * info = (struct sun_serial *)tty->driver_data;
	int retval;

	if (serial_paranoia_check(info, tty->device, "zs_ioctl"))
		return -ENODEV;

	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
	    (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
	    (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
		if (tty->flags & (1 << TTY_IO_ERROR))
		    return -EIO;
	}
	
	switch (cmd) {
		case TCSBRK:	/* SVID version: non-zero arg --> no break */
			retval = tty_check_change(tty);
			if (retval)
				return retval;
			tty_wait_until_sent(tty, 0);
			if (!arg)
				send_break(info, HZ/4);	/* 1/4 second */
			return 0;
		case TCSBRKP:	/* support for POSIX tcsendbreak() */
			retval = tty_check_change(tty);
			if (retval)
				return retval;
			tty_wait_until_sent(tty, 0);
			send_break(info, arg ? arg*(HZ/10) : HZ/4);
			return 0;
		case TIOCGSOFTCAR:
			put_user_ret(C_CLOCAL(tty) ? 1 : 0,
				     (unsigned long *) arg, -EFAULT);
			return 0;
		case TIOCSSOFTCAR:
			get_user_ret(arg, (unsigned long *) arg, -EFAULT);
			tty->termios->c_cflag =
				((tty->termios->c_cflag & ~CLOCAL) |
				 (arg ? CLOCAL : 0));
			return 0;
		case TIOCMGET:
			return get_modem_info(info, (unsigned int *) arg);
		case TIOCMBIS:
		case TIOCMBIC:
		case TIOCMSET:
			return set_modem_info(info, cmd, (unsigned int *) arg);
		case TIOCGSERIAL:
			return get_serial_info(info,
					       (struct serial_struct *) arg);
		case TIOCSSERIAL:
			return set_serial_info(info,
					       (struct serial_struct *) arg);
		case TIOCSERGETLSR: /* Get line status register */
			return get_lsr_info(info, (unsigned int *) arg);

		case TIOCSERGSTRUCT:
			copy_to_user_ret((struct sun_serial *) arg,
				    info, sizeof(struct sun_serial), -EFAULT);
			return 0;
			
		default:
			return -ENOIOCTLCMD;
		}
	return 0;
}

static void zs_set_termios(struct tty_struct *tty, struct termios *old_termios)
{
	struct sun_serial *info = (struct sun_serial *)tty->driver_data;

	if (tty->termios->c_cflag == old_termios->c_cflag)
		return;

	change_speed(info);

	if ((old_termios->c_cflag & CRTSCTS) &&
	    !(tty->termios->c_cflag & CRTSCTS)) {
		tty->hw_stopped = 0;
		zs_start(tty);
	}
}

/*
 * ------------------------------------------------------------
 * zs_close()
 * 
 * This routine is called when the serial port gets closed.  First, we
 * wait for the last remaining data to be sent.  Then, we unlink its
 * ZILOG structure from the interrupt chain if necessary, and we free
 * that IRQ if nothing is left in the chain.
 * ------------------------------------------------------------
 */
static void zs_close(struct tty_struct *tty, struct file * filp)
{
	struct sun_serial * info = (struct sun_serial *)tty->driver_data;
	unsigned long flags;

	if (!info || serial_paranoia_check(info, tty->device, "zs_close"))
		return;
	
	save_flags(flags); cli();
	
	if (tty_hung_up_p(filp)) {
		restore_flags(flags);
		return;
	}
	
#ifdef SERIAL_DEBUG_OPEN
	printk("zs_close tty-%d, count = %d\n", info->line, info->count);
#endif
	if ((tty->count == 1) && (info->count != 1)) {
		/*
		 * Uh, oh.  tty->count is 1, which means that the tty
		 * structure will be freed.  Info->count should always
		 * be one in these conditions.  If it's greater than
		 * one, we've got real problems, since it means the
		 * serial port won't be shutdown.
		 */
		printk("zs_close: bad serial port count; tty->count is 1, "
		       "info->count is %d\n", info->count);
		info->count = 1;
	}
	if (--info->count < 0) {
		printk("zs_close: bad serial port count for ttys%d: %d\n",
		       info->line, info->count);
		info->count = 0;
	}
	if (info->count) {
		restore_flags(flags);
		return;
	}
	info->flags |= ZILOG_CLOSING;
	/*
	 * Save the termios structure, since this port may have
	 * separate termios for callout and dialin.
	 */
	if (info->flags & ZILOG_NORMAL_ACTIVE)
		info->normal_termios = *tty->termios;
	if (info->flags & ZILOG_CALLOUT_ACTIVE)
		info->callout_termios = *tty->termios;
	/*
	 * Now we wait for the transmit buffer to clear; and we notify 
	 * the line discipline to only process XON/XOFF characters.
	 */
	tty->closing = 1;
	if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
		tty_wait_until_sent(tty, info->closing_wait);
	/*
	 * At this point we stop accepting input.  To do this, we
	 * disable the receive line status interrupts, and tell the
	 * interrupt driver to stop checking the data ready bit in the
	 * line status register.
	 */
	/** if (!info->iscons) ... **/
	info->curregs[3] &= ~RxENAB;
	write_zsreg(info->zs_channel, 3, info->curregs[3]);
	info->curregs[1] &= ~(RxINT_MASK);
	write_zsreg(info->zs_channel, 1, info->curregs[1]);
	ZS_CLEARFIFO(info->zs_channel);

	shutdown(info);
	if (tty->driver.flush_buffer)
		tty->driver.flush_buffer(tty);
	if (tty->ldisc.flush_buffer)
		tty->ldisc.flush_buffer(tty);
	tty->closing = 0;
	info->event = 0;
	info->tty = 0;
	if (tty->ldisc.num != ldiscs[N_TTY].num) {
		if (tty->ldisc.close)
			(tty->ldisc.close)(tty);
		tty->ldisc = ldiscs[N_TTY];
		tty->termios->c_line = N_TTY;
		if (tty->ldisc.open)
			(tty->ldisc.open)(tty);
	}
	if (info->blocked_open) {
		if (info->close_delay) {
			current->state = TASK_INTERRUPTIBLE;
			current->timeout = jiffies + info->close_delay;
			schedule();
		}
		wake_up_interruptible(&info->open_wait);
	}
	info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
			 ZILOG_CLOSING);
	wake_up_interruptible(&info->close_wait);
#ifdef SERIAL_DEBUG_OPEN
	printk("zs_close tty-%d exiting, count = %d\n", info->line, info->count);
#endif
	restore_flags(flags);
}

/*
 * zs_hangup() --- called by tty_hangup() when a hangup is signaled.
 */
void zs_hangup(struct tty_struct *tty)
{
	struct sun_serial * info = (struct sun_serial *)tty->driver_data;
	
	if (serial_paranoia_check(info, tty->device, "zs_hangup"))
		return;
	
#ifdef SERIAL_DEBUG_OPEN
	printk("zs_hangup<%p: tty-%d, count = %d bye\n",
		__builtin_return_address(0), info->line, info->count);
#endif

	zs_flush_buffer(tty);
	shutdown(info);
	info->event = 0;
	info->count = 0;
	info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
	info->tty = 0;
	wake_up_interruptible(&info->open_wait);
}

/*
 * ------------------------------------------------------------
 * zs_open() and friends
 * ------------------------------------------------------------
 */
static int block_til_ready(struct tty_struct *tty, struct file * filp,
			   struct sun_serial *info)
{
	struct wait_queue wait = { current, NULL };
	int		retval;
	int		do_clocal = 0;
	unsigned char	r0;

	/*
	 * If the device is in the middle of being closed, then block
	 * until it's done, and then try again.
	 */
	if (info->flags & ZILOG_CLOSING) {
		interruptible_sleep_on(&info->close_wait);
#ifdef SERIAL_DO_RESTART
		if (info->flags & ZILOG_HUP_NOTIFY)
			return -EAGAIN;
		else
			return -ERESTARTSYS;
#else
		return -EAGAIN;
#endif
	}

	/*
	 * If this is a callout device, then just make sure the normal
	 * device isn't being used.
	 */
	if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
		if (info->flags & ZILOG_NORMAL_ACTIVE)
			return -EBUSY;
		if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
		    (info->flags & ZILOG_SESSION_LOCKOUT) &&
		    (info->session != current->session))
		    return -EBUSY;
		if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
		    (info->flags & ZILOG_PGRP_LOCKOUT) &&
		    (info->pgrp != current->pgrp))
		    return -EBUSY;
		info->flags |= ZILOG_CALLOUT_ACTIVE;
		return 0;
	}
	
	/*
	 * If non-blocking mode is set, or the port is not enabled,
	 * then make the check up front and then exit.
	 */
	if ((filp->f_flags & O_NONBLOCK) ||
	    (tty->flags & (1 << TTY_IO_ERROR))) {
		if (info->flags & ZILOG_CALLOUT_ACTIVE)
			return -EBUSY;
		info->flags |= ZILOG_NORMAL_ACTIVE;
		return 0;
	}

	if (info->flags & ZILOG_CALLOUT_ACTIVE) {
		if (info->normal_termios.c_cflag & CLOCAL)
			do_clocal = 1;
	} else {
		if (tty->termios->c_cflag & CLOCAL)
			do_clocal = 1;
	}
	
	/*
	 * Block waiting for the carrier detect and the line to become
	 * free (i.e., not in use by the callout).  While we are in
	 * this loop, info->count is dropped by one, so that
	 * zs_close() knows when to free things.  We restore it upon
	 * exit, either normal or abnormal.
	 */
	retval = 0;
	add_wait_queue(&info->open_wait, &wait);
#ifdef SERIAL_DEBUG_OPEN
	printk("block_til_ready before block: ttys%d, count = %d\n",
	       info->line, info->count);
#endif
	cli();
	if(!tty_hung_up_p(filp))
		info->count--;
	sti();
	info->blocked_open++;
	while (1) {
		cli();
		if (!(info->flags & ZILOG_CALLOUT_ACTIVE))
			zs_rtsdtr(info, 1);
		sti();
		current->state = TASK_INTERRUPTIBLE;
		if (tty_hung_up_p(filp) ||
		    !(info->flags & ZILOG_INITIALIZED)) {
#ifdef SERIAL_DEBUG_OPEN
			printk("block_til_ready hup-ed: ttys%d, count = %d\n",
				info->line, info->count);
#endif
#ifdef SERIAL_DO_RESTART
			if (info->flags & ZILOG_HUP_NOTIFY)
				retval = -EAGAIN;
			else
				retval = -ERESTARTSYS;	
#else
			retval = -EAGAIN;
#endif
			break;
		}

		cli();
		r0 = read_zsreg(info->zs_channel, R0);
		sti();
		if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
		    !(info->flags & ZILOG_CLOSING) &&
		    (do_clocal || (DCD & r0)))
			break;
		if (signal_pending(current)) {
			retval = -ERESTARTSYS;
			break;
		}
#ifdef SERIAL_DEBUG_OPEN
		printk("block_til_ready blocking: ttys%d, count = %d\n",
		       info->line, info->count);
#endif
		schedule();
	}
	current->state = TASK_RUNNING;
	remove_wait_queue(&info->open_wait, &wait);
	if (!tty_hung_up_p(filp))
		info->count++;
	info->blocked_open--;
#ifdef SERIAL_DEBUG_OPEN
	printk("block_til_ready after blocking: ttys%d, count = %d\n",
	       info->line, info->count);
#endif
	if (retval)
		return retval;
	info->flags |= ZILOG_NORMAL_ACTIVE;
	return 0;
}	

/*
 * This routine is called whenever a serial port is opened.  It
 * enables interrupts for a serial port, linking in its ZILOG structure into
 * the IRQ chain.   It also performs the serial-specific
 * initialization for the tty structure.
 */
int zs_open(struct tty_struct *tty, struct file * filp)
{
	struct sun_serial	*info;
	int 			retval, line;

	line = MINOR(tty->device) - tty->driver.minor_start;
	/* The zilog lines for the mouse/keyboard must be
	 * opened using their respective drivers.
	 */
	if ((line < 0) || (line >= NUM_CHANNELS))
		return -ENODEV;
	if((line == KEYBOARD_LINE) || (line == MOUSE_LINE))
		return -ENODEV;
	info = zs_soft + line;
	/* Is the kgdb running over this line? */
	if (info->kgdb_channel)
		return -ENODEV;
	if (serial_paranoia_check(info, tty->device, "zs_open"))
		return -ENODEV;
#ifdef SERIAL_DEBUG_OPEN
	printk("zs_open %s%d, count = %d\n", tty->driver.name, info->line,
	       info->count);
#endif
	if (info->tty != 0 && info->tty != tty) {
		/* Never happen? */
		printk("zs_open %s%d, tty overwrite.\n", tty->driver.name, info->line);
		return -EBUSY;
	}
	info->count++;
	tty->driver_data = info;
	info->tty = tty;

	/*
	 * Start up serial port
	 */
	retval = startup(info);
	if (retval)
		return retval;

	retval = block_til_ready(tty, filp, info);
	if (retval) {
#ifdef SERIAL_DEBUG_OPEN
		printk("zs_open returning after block_til_ready with %d\n",
		       retval);
#endif
		return retval;
	}

	if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
		if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
			*tty->termios = info->normal_termios;
		else 
			*tty->termios = info->callout_termios;
		change_speed(info);
	}

	info->session = current->session;
	info->pgrp = current->pgrp;

#ifdef SERIAL_DEBUG_OPEN
	printk("zs_open ttys%d successful...", info->line);
#endif
	return 0;
}

/* Finally, routines used to initialize the serial driver. */

static void show_serial_version(void)
{
	char *revision = "$Revision: 1.3 $";
	char *version, *p;

	version = strchr(revision, ' ');
	p = strchr(++version, ' ');
	*p = '\0';
	printk("Sparc Zilog8530 serial driver version %s\n", version);
	*p = ' ';
}

/* Probe the PROM for the request zs chip number.
 *
 * Note: The Sun Voyager shows two addresses and two intr for it's
 *       Zilogs, what the second does, I don't know. It does work
 *       with using only the first number of each property.  Also
 *       we have a special version for sun4u.
 */
#ifdef __sparc_v9__
static struct devid_cookie zs_dcookie;
static unsigned long zs_irq_flags;
static struct sun_zslayout *get_zs(int chip)
{
	unsigned int vaddr[2] = { 0, 0 };
	int busnode, seen, zsnode, sun4u_ino;
	static int irq = 0;

	if(chip < 0 || chip >= NUM_SERIAL)
		panic("get_zs bogon zs chip number");

	if(central_bus)
		busnode = central_bus->child->prom_node;
	else
		busnode = prom_searchsiblings(prom_getchild(prom_root_node), "sbus");
	if(busnode == 0 || busnode == -1)
		panic("get_zs: no zs bus to search");

	zsnode = prom_getchild(busnode);
	seen = 0;
	while(zsnode) {
		int slave;

		zsnode = prom_searchsiblings(zsnode, "zs");
		slave = prom_getintdefault(zsnode, "slave", -1);
		if((slave == chip) || (seen == chip)) {
			int len = prom_getproperty(zsnode, "address",
						   (void *) vaddr, sizeof(vaddr));

			if(len % sizeof(unsigned int)) {
				prom_printf("WHOOPS:  proplen for %s "
					    "was %d, need multiple of "
					    "%d\n", "address", len,
					    sizeof(unsigned int));
				panic("zilog: address property");
			}
			zs_nodes[chip] = zsnode;
			len = prom_getproperty(zsnode, "interrupts",
					       (char *) &sun4u_ino,
					       (sizeof(sun4u_ino)));
			if(!irq) {
				irq = zilog_irq = sun4u_ino;

				/* Construct dcookie. */
				if(central_bus) {
				  zs_dcookie.imap =
				    &central_bus->child->fhc_regs.uregs->fhc_uart_imap;
				  zs_dcookie.iclr =
				    &central_bus->child->fhc_regs.uregs->fhc_uart_iclr;
				  zs_dcookie.pil = 12;
				  zs_dcookie.bus_cookie = NULL;
				  zs_irq_flags =
				    (SA_DCOOKIE|SA_INTERRUPT|SA_STATIC_ALLOC|SA_FHC);
				} else {
				  zs_dcookie.imap = zs_dcookie.iclr = NULL;
				  zs_dcookie.pil = -1;
				  zs_dcookie.bus_cookie = SBus_chain;
				  zs_irq_flags =
				    (SA_DCOOKIE|SA_INTERRUPT|SA_STATIC_ALLOC|SA_SBUS);
				}
			} else if(irq != sun4u_ino) {
				panic("zilog: bogon irqs");
			}
			break;
		}
		zsnode = prom_getsibling(zsnode);
		seen++;
	}
	if(!zsnode)
		panic("get_zs: whee chip not found");
	if(!vaddr[0])
		panic("get_zs: whee no serial chip mappable");
	return (struct sun_zslayout *)(unsigned long) vaddr[0];
}
#else /* !(__sparc_v9__) */
static struct sun_zslayout *get_zs(int chip)
{
	struct linux_prom_irqs tmp_irq[2];
	unsigned int paddr = 0;
	unsigned int vaddr[2] = { 0, 0 };
	int zsnode, tmpnode, iospace, slave, len, seen, sun4u_irq;
	static int irq = 0;

#if CONFIG_AP1000
        printk("No zs chip\n");
        return NULL;
#endif

	iospace = 0;
	if(chip < 0 || chip >= NUM_SERIAL)
		panic("get_zs bogon zs chip number");

	if(sparc_cpu_model == sun4) {
		/* Grrr, these have to be hardcoded aieee */
		switch(chip) {
		case 0:
			paddr = 0xf1000000;
			break;
		case 1:
			paddr = 0xf0000000;
			break;
		};
		iospace = 0;
		zs_nodes[chip] = 0;
		if(!irq)
			zilog_irq = irq = 12;
		vaddr[0] = (unsigned long)
				sparc_alloc_io(paddr, 0, 8,
					       "Zilog Serial", iospace, 0);
	} else {
		/* Can use the prom for other machine types */
		zsnode = prom_getchild(prom_root_node);
		if (sparc_cpu_model == sun4d) {
			int board, node;
			
			tmpnode = zsnode;
			while (tmpnode && (tmpnode = prom_searchsiblings(tmpnode, "cpu-unit"))) {
				board = prom_getintdefault (tmpnode, "board#", -1);
				if (board == (chip >> 1)) {
					node = prom_getchild(tmpnode);
					if (node && (node = prom_searchsiblings(node, "bootbus"))) {
						zsnode = node;
						break;
					}
				}
				tmpnode = prom_getsibling(tmpnode);
			}
			if (!tmpnode)
				panic ("get_zs: couldn't find board%d's bootbus\n", chip >> 1);
		} else if (sparc_cpu_model == sun4u) {
			tmpnode = prom_searchsiblings(zsnode, "sbus");
			if(tmpnode)
				zsnode = prom_getchild(tmpnode);
		} else {
			tmpnode = prom_searchsiblings(zsnode, "obio");
			if(tmpnode)
				zsnode = prom_getchild(tmpnode);
		}
		if(!zsnode)
			panic("get_zs no zs serial prom node");
		seen = 0;
		while(zsnode) {
			zsnode = prom_searchsiblings(zsnode, "zs");
			slave = prom_getintdefault(zsnode, "slave", -1);
			if((slave == chip) ||
			   (sparc_cpu_model == sun4u && seen == chip)) {
				/* The one we want */
				len = prom_getproperty(zsnode, "address",
						       (void *) vaddr,
						       sizeof(vaddr));
        			if (len % sizeof(unsigned int)) {
					prom_printf("WHOOPS:  proplen for %s "
						"was %d, need multiple of "
						"%d\n", "address", len,
						sizeof(unsigned int));
					panic("zilog: address property");
				}
				zs_nodes[chip] = zsnode;
				if(sparc_cpu_model == sun4u) {
					len = prom_getproperty(zsnode, "interrupts",
							       (char *) &sun4u_irq,
							       sizeof(tmp_irq));
					tmp_irq[0].pri = sun4u_irq;
				} else {
					len = prom_getproperty(zsnode, "intr",
							       (char *) tmp_irq,
							       sizeof(tmp_irq));
					if (len % sizeof(struct linux_prom_irqs)) {
						prom_printf(
						      "WHOOPS:  proplen for %s "
						      "was %d, need multiple of "
						      "%d\n", "address", len,
						      sizeof(struct linux_prom_irqs));
						panic("zilog: address property");
					}
				}
				if(!irq) {
					irq = zilog_irq = tmp_irq[0].pri;
				} else {
					if(tmp_irq[0].pri != irq)
						panic("zilog: bogon irqs");
				}
				break;
			}
			zsnode = prom_getsibling(zsnode);
			seen++;
		}
		if(!zsnode)
			panic("get_zs whee chip not found");
	}
	if(!vaddr[0])
		panic("get_zs whee no serial chip mappable");

	return (struct sun_zslayout *)(unsigned long) vaddr[0];
}
#endif

static inline void
init_zscons_termios(struct termios *termios)
{
	char mode[16], buf[16];
	char *mode_prop = "ttyX-mode";
	char *cd_prop = "ttyX-ignore-cd";
	char *dtr_prop = "ttyX-rts-dtr-off";
	char *s;
	int baud, bits, cflag;
	char parity;
	int topnd, nd;
	int channel, stop;
	int carrier = 0;
	int rtsdtr = 1;
	extern int serial_console;

	if (!serial_console)
		return;

	if (serial_console == 1) {
		mode_prop[3] = 'a';
		cd_prop[3] = 'a';
		dtr_prop[3] = 'a';
	} else {
		mode_prop[3] = 'b';
		cd_prop[3] = 'b';
		dtr_prop[3] = 'b';
	}

	topnd = prom_getchild(prom_root_node);
	nd = prom_searchsiblings(topnd, "options");
	if (!nd) {
		strcpy(mode, "9600,8,n,1,-");
		goto no_options;
	}

	if (!prom_node_has_property(nd, mode_prop)) {
		strcpy(mode, "9600,8,n,1,-");
		goto no_options;
	}

	memset(mode, 0, sizeof(mode));
	prom_getstring(nd, mode_prop, mode, sizeof(mode));

	if (prom_node_has_property(nd, cd_prop)) {
		memset(buf, 0, sizeof(buf));
		prom_getstring(nd, cd_prop, buf, sizeof(buf));
		if (!strcmp(buf, "false"))
			carrier = 1;

		/* XXX this is unused below. */
	}

	if (prom_node_has_property(nd, cd_prop)) {
		memset(buf, 0, sizeof(buf));
		prom_getstring(nd, cd_prop, buf, sizeof(buf));
		if (!strcmp(buf, "false"))
			rtsdtr = 0;

		/* XXX this is unused below. */
	}

no_options:
	cflag = CREAD | HUPCL | CLOCAL;

	s = mode;
	baud = simple_strtoul(s, 0, 0);
	s = strchr(s, ',');
	bits = simple_strtoul(++s, 0, 0);
	s = strchr(s, ',');
	parity = *(++s);
	s = strchr(s, ',');
	stop = simple_strtoul(++s, 0, 0);
	s = strchr(s, ',');
	/* XXX handshake is not handled here. */

	for (channel = 0; channel < NUM_CHANNELS; channel++)
		if (zs_soft[channel].is_cons)
			break;

	switch (baud) {
		case 150:
			cflag |= B150;
			break;
		case 300:
			cflag |= B300;
			break;
		case 600:
			cflag |= B600;
			break;
		case 1200:
			cflag |= B1200;
			break;
		case 2400:
			cflag |= B2400;
			break;
		case 4800:
			cflag |= B4800;
			break;
		default:
			baud = 9600;
		case 9600:
			cflag |= B9600;
			break;
		case 19200:
			cflag |= B19200;
			break;
		case 38400:
			cflag |= B38400;
			break;
	}
	zs_soft[channel].zs_baud = baud;

	switch (bits) {
		case 5:
			zscons_regs[3] = Rx5 | RxENAB;
			zscons_regs[5] = Tx5 | TxENAB;
			zs_soft[channel].parity_mask = 0x1f;
			cflag |= CS5;
			break;
		case 6:
			zscons_regs[3] = Rx6 | RxENAB;
			zscons_regs[5] = Tx6 | TxENAB;
			zs_soft[channel].parity_mask = 0x3f;
			cflag |= CS6;
			break;
		case 7:
			zscons_regs[3] = Rx7 | RxENAB;
			zscons_regs[5] = Tx7 | TxENAB;
			zs_soft[channel].parity_mask = 0x7f;
			cflag |= CS7;
			break;
		default:
		case 8:
			zscons_regs[3] = Rx8 | RxENAB;
			zscons_regs[5] = Tx8 | TxENAB;
			zs_soft[channel].parity_mask = 0xff;
			cflag |= CS8;
			break;
	}
	zscons_regs[5] |= DTR;

	switch (parity) {
		case 'o':
			zscons_regs[4] |= PAR_ENAB;
			cflag |= (PARENB | PARODD);
			break;
		case 'e':
			zscons_regs[4] |= (PAR_ENAB | PAR_EVEN);
			cflag |= PARENB;
			break;
		default:
		case 'n':
			break;
	}

	switch (stop) {
		default:
		case 1:
			zscons_regs[4] |= SB1;
			break;
		case 2:
			cflag |= CSTOPB;
			zscons_regs[4] |= SB2;
			break;
	}

	termios->c_cflag = cflag;
}

__initfunc(static void serial_finish_init(void (*printfunc)(const char *, unsigned)))
{
	extern unsigned char *linux_serial_image;
	char buffer[2048];
	
	sprintf (buffer, linux_serial_image, UTS_RELEASE);
	(*printfunc)(buffer, strlen(buffer));
}

static inline void
zs_cons_check(struct sun_serial *ss, int channel)
{
	int i, o, io;
	static int consout_registered = 0;
	static int msg_printed = 0;
	static struct console console = {
		zs_console_print, 0,
		zs_console_wait_key, zs_console_device };

	i = o = io = 0;

	/* Is this one of the serial console lines? */
	if((zs_cons_chanout != channel) &&
	   (zs_cons_chanin != channel))
		return;
	zs_conschan = ss->zs_channel;
	zs_consinfo = ss;

	/* Register the console output putchar, if necessary */
	if((zs_cons_chanout == channel)) {
		o = 1;
		/* double whee.. */
		if(!consout_registered) {
			serial_finish_init (zs_console_print);
			register_console(&console);
			consout_registered = 1;
		}
	}

	/* If this is console input, we handle the break received
	 * status interrupt on this line to mean prom_halt().
	 */
	if(zs_cons_chanin == channel) {
		ss->break_abort = 1;
		i = 1;
	}
	if(o && i)
		io = 1;

	/* Set flag variable for this port so that it cannot be
	 * opened for other uses by accident.
	 */
	ss->is_cons = 1;

	if(io) {
		if(!msg_printed) {
			printk("zs%d: console I/O\n", ((channel>>1)&1));
			msg_printed = 1;
		}
	} else {
		printk("zs%d: console %s\n", ((channel>>1)&1),
		       (i==1 ? "input" : (o==1 ? "output" : "WEIRD")));
	}
}

/* This is for the auto baud rate detection in the mouse driver. */
void zs_change_mouse_baud(int newbaud)
{
	int channel = MOUSE_LINE;
	int brg;

	zs_soft[channel].zs_baud = newbaud;
	brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
			 (ZS_CLOCK / zs_soft[channel].clk_divisor));
	write_zsreg(zs_soft[channel].zs_channel, R12, (brg & 0xff));
	write_zsreg(zs_soft[channel].zs_channel, R13, ((brg >> 8) & 0xff));
}

__initfunc(int zs_probe (unsigned long *memory_start))
{
	char *p;
	int node;
	int i;

	if(sparc_cpu_model == sun4)
		goto no_probe;

	node = prom_getchild(prom_root_node);
	if (sparc_cpu_model == sun4d) {
		node = prom_searchsiblings(node, "boards");
		NUM_SERIAL = 0;
		if (!node)
			panic ("Cannot find out count of boards");
		else
			node = prom_getchild(node);
		while (node && (node = prom_searchsiblings(node, "bif"))) {
			NUM_SERIAL += 2;
			node = prom_getsibling(node);
		}
		goto no_probe;
	} else if (sparc_cpu_model == sun4u) {
		node = prom_searchsiblings(node, "sbus");
		if(node)
			node = prom_getchild(node);
		if(!node)
			return -ENODEV;
	} else {
		node = prom_searchsiblings(node, "obio");
		if(node)
			node = prom_getchild(node);
		goto no_probe;
	}

	node = prom_searchsiblings(node, "zs");
	if (!node)
		return -ENODEV;

no_probe:
	p = (char *)((*memory_start + 7) & ~7);
	zs_chips = (struct sun_zslayout **)(p);
	i = NUM_SERIAL * sizeof (struct sun_zslayout *);
	zs_channels = (struct sun_zschannel **)(p + i);
	i += NUM_CHANNELS * sizeof (struct sun_zschannel *);
	zs_nodes = (int *)(p + i);
	i += NUM_SERIAL * sizeof (int);
	zs_soft = (struct sun_serial *)(p + i);
	i += NUM_CHANNELS * sizeof (struct sun_serial);
	zs_ttys = (struct tty_struct *)(p + i);
	i += NUM_CHANNELS * sizeof (struct tty_struct);
	serial_table = (struct tty_struct **)(p + i);
	i += NUM_CHANNELS * sizeof (struct tty_struct *);
	serial_termios = (struct termios **)(p + i);
	i += NUM_CHANNELS * sizeof (struct termios *);
	serial_termios_locked = (struct termios **)(p + i);
	i += NUM_CHANNELS * sizeof (struct termios *);
	memset (p, 0, i);
	*memory_start = (((unsigned long)p) + i + 7) & ~7;

	/* Fill in rs_ops struct... */
	sunserial_setinitfunc(memory_start, zs_init);
	rs_ops.rs_cons_hook = zs_cons_hook;
	rs_ops.rs_kgdb_hook = zs_kgdb_hook;
	rs_ops.rs_change_mouse_baud = zs_change_mouse_baud;

	return 0;
}

__initfunc(int zs_init(void))
{
	int chip, channel, brg, i;
	unsigned long flags;
	struct sun_serial *info;
	char dummy;

#if CONFIG_AP1000
        printk("not doing zs_init()\n");
        return 0;
#endif

#ifdef CONFIG_PCI
	if (prom_searchsiblings(prom_getchild(prom_root_node), "pci"))
		return 0;
#endif

	/* Setup base handler, and timer table. */
	init_bh(SERIAL_BH, do_serial_bh);
	timer_table[RS_TIMER].fn = zs_timer;
	timer_table[RS_TIMER].expires = 0;

	show_serial_version();

	/* Initialize the tty_driver structure */
	/* SPARC: Not all of this is exactly right for us. */
	
	memset(&serial_driver, 0, sizeof(struct tty_driver));
	serial_driver.magic = TTY_DRIVER_MAGIC;
	serial_driver.driver_name = "serial";
	serial_driver.name = "ttyS";
	serial_driver.major = TTY_MAJOR;
	serial_driver.minor_start = 64;
	serial_driver.num = NUM_CHANNELS;
	serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
	serial_driver.subtype = SERIAL_TYPE_NORMAL;
	serial_driver.init_termios = tty_std_termios;

	serial_driver.init_termios.c_cflag =
		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
	serial_driver.flags = TTY_DRIVER_REAL_RAW;
	serial_driver.refcount = &serial_refcount;
	serial_driver.table = serial_table;
	serial_driver.termios = serial_termios;
	serial_driver.termios_locked = serial_termios_locked;

	serial_driver.open = zs_open;
	serial_driver.close = zs_close;
	serial_driver.write = zs_write;
	serial_driver.flush_chars = zs_flush_chars;
	serial_driver.write_room = zs_write_room;
	serial_driver.chars_in_buffer = zs_chars_in_buffer;
	serial_driver.flush_buffer = zs_flush_buffer;
	serial_driver.ioctl = zs_ioctl;
	serial_driver.throttle = zs_throttle;
	serial_driver.unthrottle = zs_unthrottle;
	serial_driver.set_termios = zs_set_termios;
	serial_driver.stop = zs_stop;
	serial_driver.start = zs_start;
	serial_driver.hangup = zs_hangup;

	/* I'm too lazy, someone write versions of this for us. -DaveM */
	serial_driver.read_proc = 0;
	serial_driver.proc_entry = 0;

	init_zscons_termios(&serial_driver.init_termios);

	/*
	 * The callout device is just like normal device except for
	 * major number and the subtype code.
	 */
	callout_driver = serial_driver;
	callout_driver.name = "cua";
	callout_driver.major = TTYAUX_MAJOR;
	callout_driver.subtype = SERIAL_TYPE_CALLOUT;

	if (tty_register_driver(&serial_driver))
		panic("Couldn't register serial driver\n");
	if (tty_register_driver(&callout_driver))
		panic("Couldn't register callout driver\n");
	
	save_flags(flags); cli();

	/* Set up our interrupt linked list */
	zs_chain = &zs_soft[0];
	for(channel = 0; channel < NUM_CHANNELS - 1; channel++)
		zs_soft[channel].zs_next = &zs_soft[channel + 1];
	zs_soft[channel + 1].zs_next = 0;

	/* Initialize Softinfo */
	for(chip = 0; chip < NUM_SERIAL; chip++) {
		/* If we are doing kgdb over one of the channels on
		 * chip zero, kgdb_channel will be set to 1 by the
		 * zs_kgdb_hook() routine below.
		 */
		if(!zs_chips[chip]) {
			zs_chips[chip] = get_zs(chip);
			/* Two channels per chip */
			zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
			zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
			zs_soft[(chip*2)].kgdb_channel = 0;
			zs_soft[(chip*2)+1].kgdb_channel = 0;
		}

		/* First, set up channel A on this chip. */
		channel = chip * 2;
		zs_soft[channel].zs_channel = zs_channels[channel];
		zs_soft[channel].change_needed = 0;
		zs_soft[channel].clk_divisor = 16;
		zs_soft[channel].cons_keyb = 0;
		zs_soft[channel].cons_mouse = 0;
		zs_soft[channel].channelA = 1;

		/* Now, channel B */
		channel++;
		zs_soft[channel].zs_channel = zs_channels[channel];
		zs_soft[channel].change_needed = 0;
		zs_soft[channel].clk_divisor = 16;
		zs_soft[channel].cons_keyb = 0;
		zs_soft[channel].cons_mouse = 0;
		zs_soft[channel].channelA = 0;
	}

	/* Initialize Hardware */
	for(channel = 0; channel < NUM_CHANNELS; channel++) {

		/* Hardware reset each chip */
		if (!(channel & 1)) {
			write_zsreg(zs_soft[channel].zs_channel, R9, FHWRES);
			udelay(20);	/* wait for some old sun4's */
			dummy = read_zsreg(zs_soft[channel].zs_channel, R0);
		}

		if(channel == KEYBOARD_LINE) {
			zs_soft[channel].cons_keyb = 1;
			zs_soft[channel].parity_mask = 0xff;
			zs_kbdchan = zs_soft[channel].zs_channel;

			write_zsreg(zs_soft[channel].zs_channel, R4,
				    (PAR_EVEN | X16CLK | SB1));
			write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
			write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
			write_zsreg(zs_soft[channel].zs_channel, R9, NV);
			write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
			write_zsreg(zs_soft[channel].zs_channel, R11,
				    (TCBR | RCBR));
			zs_soft[channel].zs_baud = 1200;
			brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
					 ZS_CLOCK/zs_soft[channel].clk_divisor);
			write_zsreg(zs_soft[channel].zs_channel, R12,
				    (brg & 0xff));
			write_zsreg(zs_soft[channel].zs_channel, R13,
				    ((brg >> 8) & 0xff));
			write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);

			/* Enable Rx/Tx, IRQs, and inform kbd driver */
			write_zsreg(zs_soft[channel].zs_channel, R14,
				    (BRSRC | BRENAB));
			write_zsreg(zs_soft[channel].zs_channel, R3,
				    (Rx8 | RxENAB));
			write_zsreg(zs_soft[channel].zs_channel, R5,
				    (Tx8 | TxENAB | DTR | RTS));

			write_zsreg(zs_soft[channel].zs_channel, R15,
				    (DCDIE | CTSIE | TxUIE | BRKIE));
			write_zsreg(zs_soft[channel].zs_channel, R0,
				    RES_EXT_INT);
			write_zsreg(zs_soft[channel].zs_channel, R0,
				    RES_EXT_INT);

			write_zsreg(zs_soft[channel].zs_channel, R1,
				    (EXT_INT_ENAB | INT_ALL_Rx));
			write_zsreg(zs_soft[channel].zs_channel, R9,
				    (NV | MIE));
			ZS_CLEARERR(zs_soft[channel].zs_channel);
			ZS_CLEARFIFO(zs_soft[channel].zs_channel);
		} else if(channel == MOUSE_LINE) {
			zs_soft[channel].cons_mouse = 1;
			zs_soft[channel].parity_mask = 0xff;
			zs_mousechan = zs_soft[channel].zs_channel;

			write_zsreg(zs_soft[channel].zs_channel, R4,
				    (PAR_EVEN | X16CLK | SB1));
			write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
			write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
			write_zsreg(zs_soft[channel].zs_channel, R9, NV);
			write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
			write_zsreg(zs_soft[channel].zs_channel, R11,
				    (TCBR | RCBR));

			zs_soft[channel].zs_baud = 4800;
			brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
					 ZS_CLOCK/zs_soft[channel].clk_divisor);
			write_zsreg(zs_soft[channel].zs_channel, R12,
				    (brg & 0xff));
			write_zsreg(zs_soft[channel].zs_channel, R13,
				    ((brg >> 8) & 0xff));
			write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);

			/* Enable Rx, IRQs, and inform mouse driver */
			write_zsreg(zs_soft[channel].zs_channel, R14,
				    (BRSRC | BRENAB));
			write_zsreg(zs_soft[channel].zs_channel, R3,
				    (Rx8 | RxENAB));
			write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);

			write_zsreg(zs_soft[channel].zs_channel, R15,
				    (DCDIE | CTSIE | TxUIE | BRKIE));
			write_zsreg(zs_soft[channel].zs_channel, R0,
				    RES_EXT_INT);
			write_zsreg(zs_soft[channel].zs_channel, R0,
				    RES_EXT_INT);

			write_zsreg(zs_soft[channel].zs_channel, R1,
				    (EXT_INT_ENAB | INT_ALL_Rx));
			write_zsreg(zs_soft[channel].zs_channel, R9,
				    (NV | MIE));

			sun_mouse_zsinit();
		} else if (zs_soft[channel].is_cons) {
			brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
					 ZS_CLOCK/zs_soft[channel].clk_divisor);
			zscons_regs[12] = brg & 0xff;
			zscons_regs[13] = (brg >> 8) & 0xff;

			memcpy(zs_soft[channel].curregs, zscons_regs, sizeof(zscons_regs));
			load_zsregs(&zs_soft[channel], zscons_regs);

			ZS_CLEARERR(zs_soft[channel].zs_channel);
			ZS_CLEARFIFO(zs_soft[channel].zs_channel);
		} else if (zs_soft[channel].kgdb_channel) {
			/* If this is the kgdb line, enable interrupts because
			 * we now want to receive the 'control-c' character
			 * from the client attached to us asynchronously.
			 */
			zs_soft[channel].parity_mask = 0xff;
        		kgdb_chaninit(&zs_soft[channel], 1,
				      zs_soft[channel].zs_baud);
		} else {
			zs_soft[channel].parity_mask = 0xff;
			write_zsreg(zs_soft[channel].zs_channel, R4,
				    (PAR_EVEN | X16CLK | SB1));
			write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
			write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
			write_zsreg(zs_soft[channel].zs_channel, R9, NV);
			write_zsreg(zs_soft[channel].zs_channel, R10, NRZ);
			write_zsreg(zs_soft[channel].zs_channel, R11,
				    (RCBR | TCBR));
			zs_soft[channel].zs_baud = 9600;
			brg = BPS_TO_BRG(zs_soft[channel].zs_baud,
					 ZS_CLOCK/zs_soft[channel].clk_divisor);
			write_zsreg(zs_soft[channel].zs_channel, R12,
				    (brg & 0xff));
			write_zsreg(zs_soft[channel].zs_channel, R13,
				    ((brg >> 8) & 0xff));
			write_zsreg(zs_soft[channel].zs_channel, R14, BRSRC);
			write_zsreg(zs_soft[channel].zs_channel, R14,
				    (BRSRC | BRENAB));
			write_zsreg(zs_soft[channel].zs_channel, R3, Rx8);
			write_zsreg(zs_soft[channel].zs_channel, R5, Tx8);
			write_zsreg(zs_soft[channel].zs_channel, R15, DCDIE);
			write_zsreg(zs_soft[channel].zs_channel, R9, NV | MIE);
			write_zsreg(zs_soft[channel].zs_channel, R0,
				    RES_EXT_INT);
			write_zsreg(zs_soft[channel].zs_channel, R0,
				    RES_EXT_INT);
		}
	}

	for (info = zs_chain, i=0; info; info = info->zs_next, i++) {
		info->magic = SERIAL_MAGIC;
		info->port = (long) info->zs_channel;
		info->line = i;
		info->tty = 0;
		info->irq = zilog_irq;
		info->custom_divisor = 16;
		info->close_delay = 50;
		info->closing_wait = 3000;
		info->x_char = 0;
		info->event = 0;
		info->count = 0;
		info->blocked_open = 0;
		info->tqueue.routine = do_softint;
		info->tqueue.data = info;
		info->tqueue_hangup.routine = do_serial_hangup;
		info->tqueue_hangup.data = info;
		info->callout_termios = callout_driver.init_termios;
		info->normal_termios = serial_driver.init_termios;
		info->open_wait = 0;
		info->close_wait = 0;
		printk("tty%02d at 0x%04x (irq = %d)", info->line, 
		       info->port, info->irq);
		printk(" is a Zilog8530\n");
	}

#ifndef __sparc_v9__
	if (request_irq(zilog_irq, zs_interrupt,
			(SA_INTERRUPT | SA_STATIC_ALLOC),
			"Zilog8530", zs_chain))
		panic("Unable to attach zs intr\n");
#else
	zs_dcookie.real_dev_id = zs_chain;
	if (request_irq(zilog_irq, zs_interrupt,
			zs_irq_flags, "Zilog8530", &zs_dcookie))
		panic("Unable to attach zs intr\n");
#endif
	restore_flags(flags);

	keyboard_zsinit(kbd_put_char);
	return 0;
}

/* Hooks for running a serial console.  con_init() calls this if the
 * console is being run over one of the ttya/ttyb serial ports.
 * 'chip' should be zero, as chip 1 drives the mouse/keyboard.
 * 'channel' is decoded as 0=TTYA 1=TTYB, note that the channels
 * are addressed backwards, channel B is first, then channel A.
 */
void
zs_cons_hook(int chip, int out, int line)
{
	int channel;

#ifdef CONFIG_PCI
	if (prom_searchsiblings(prom_getchild(prom_root_node), "pci"))
		return;
#endif

	if(chip)
		panic("zs_cons_hook called with chip not zero");
	if(line != 1 && line != 2)
		panic("zs_cons_hook called with line not ttya or ttyb");
	channel = line - 1;
	if(!zs_chips[chip]) {
		zs_chips[chip] = get_zs(chip);
		/* Two channels per chip */
		zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
		zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
	}
	zs_soft[channel].zs_channel = zs_channels[channel];
	zs_soft[channel].change_needed = 0;
	zs_soft[channel].clk_divisor = 16;
	if(out)
		zs_cons_chanout = ((chip * 2) + channel);
	else
		zs_cons_chanin = ((chip * 2) + channel);
	zs_cons_check(&zs_soft[channel], channel);
}

/* This is called at boot time to prime the kgdb serial debugging
 * serial line.  The 'tty_num' argument is 0 for /dev/ttya and 1
 * for /dev/ttyb which is determined in setup_arch() from the
 * boot command line flags.
 */
void
zs_kgdb_hook(int tty_num)
{
	int chip = 0;

	if(!zs_chips[chip]) {
		zs_chips[chip] = get_zs(chip);
		/* Two channels per chip */
		zs_channels[(chip*2)] = &zs_chips[chip]->channelA;
		zs_channels[(chip*2)+1] = &zs_chips[chip]->channelB;
	}
	zs_soft[tty_num].zs_channel = zs_channels[tty_num];
	zs_kgdbchan = zs_soft[tty_num].zs_channel;
	zs_soft[tty_num].change_needed = 0;
	zs_soft[tty_num].clk_divisor = 16;
	zs_soft[tty_num].zs_baud = 9600;
	zs_soft[tty_num].kgdb_channel = 1;     /* This runs kgdb */
	zs_soft[tty_num ^ 1].kgdb_channel = 0; /* This does not */
	/* Turn on transmitter/receiver at 8-bits/char */
        kgdb_chaninit(&zs_soft[tty_num], 0, 9600);
        ZS_CLEARERR(zs_kgdbchan);
        ZS_CLEARFIFO(zs_kgdbchan);
}