summaryrefslogtreecommitdiffstats
path: root/drivers/char/rio/rioinit.c
blob: 898a126ae3e654d0087c41184259127fdc347f97 (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
/*
** -----------------------------------------------------------------------------
**
**  Perle Specialix driver for Linux
**  Ported from existing RIO Driver for SCO sources.
 *
 *  (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
**	Module		: rioinit.c
**	SID		: 1.3
**	Last Modified	: 11/6/98 10:33:43
**	Retrieved	: 11/6/98 10:33:49
**
**  ident @(#)rioinit.c	1.3
**
** -----------------------------------------------------------------------------
*/
#ifdef SCCS_LABELS
static char *_rioinit_c_sccs_ = "@(#)rioinit.c	1.3";
#endif

#include <linux/config.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/delay.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/string.h>
#include <asm/semaphore.h>
#include <asm/uaccess.h>

#include <linux/termios.h>
#include <linux/serial.h>

#include <linux/generic_serial.h>


#include "linux_compat.h"
#include "typdef.h"
#include "pkt.h"
#include "daemon.h"
#include "rio.h"
#include "riospace.h"
#include "top.h"
#include "cmdpkt.h"
#include "map.h"
#include "riotypes.h"
#include "rup.h"
#include "port.h"
#include "riodrvr.h"
#include "rioinfo.h"
#include "func.h"
#include "errors.h"
#include "pci.h"

#include "parmmap.h"
#include "unixrup.h"
#include "board.h"
#include "host.h"
#include "error.h"
#include "phb.h"
#include "link.h"
#include "cmdblk.h"
#include "route.h"
#include "control.h"
#include "cirrus.h"
#include "rioioctl.h"
#include "rio_linux.h"

#undef bcopy
#define bcopy rio_pcicopy

int RIOPCIinit(struct rio_info *p, int Mode);

#if 0
static void RIOAllocateInterrupts(struct rio_info *);
static int RIOReport(struct rio_info *);
static void RIOStopInterrupts(struct rio_info *, int, int);
#endif

static int RIOScrub(int, BYTE *, int);

#if 0
extern int	rio_intr();

/*
**	Init time code.
*/
void
rioinit( p, info )
struct rio_info		* p;
struct RioHostInfo	* info;
{
	/*
	** Multi-Host card support - taking the easy way out - sorry !
	** We allocate and set up the Host and Port structs when the
	** driver is called to 'install' the first host.
	** We check for this first 'call' by testing the RIOPortp pointer.
	*/
	if ( !p->RIOPortp )
	{
		rio_dprintk (RIO_DEBUG_INIT,  "Allocating and setting up driver data structures\n");

		RIOAllocDataStructs(p);		/* allocate host/port structs */
		RIOSetupDataStructs(p);		/* setup topology structs */
	}

	RIOInitHosts( p, info );	/* hunt down the hardware */

	RIOAllocateInterrupts(p);	/* allocate interrupts */
	RIOReport(p);			/* show what we found */
}

/*
** Initialise the Cards 
*/ 
void
RIOInitHosts(p, info)
struct rio_info		* p;
struct RioHostInfo	* info;
{
/*
** 15.10.1998 ARG - ESIL 0762 part fix
** If there is no ISA card definition - we always look for PCI cards.
** As we currently only support one host card this lets an ISA card
** definition take precedence over PLUG and PLAY.
** No ISA card - we are PLUG and PLAY with PCI.
*/

	/*
	** Note - for PCI both these will be zero, that's okay because
	** RIOPCIInit() fills them in if a card is found.
	*/
	p->RIOHosts[p->RIONumHosts].Ivec	= info->vector;
	p->RIOHosts[p->RIONumHosts].PaddrP	= info->location;

	/*
	** Check that we are able to accommodate another host
	*/
	if ( p->RIONumHosts >= RIO_HOSTS )
	{
		p->RIOFailed++;
		return;
	}

	if ( info->bus & ISA_BUS )
	{
		rio_dprintk (RIO_DEBUG_INIT,  "initialising card %d (ISA)\n", p->RIONumHosts);
		RIOISAinit(p, p->mode);
	}
	else
	{
		rio_dprintk (RIO_DEBUG_INIT,  "initialising card %d (PCI)\n", p->RIONumHosts);
		RIOPCIinit(p, RIO_PCI_DEFAULT_MODE);
	}

	rio_dprintk (RIO_DEBUG_INIT,  "Total hosts initialised so far : %d\n", p->RIONumHosts);


#ifdef FUTURE_RELEASE
	if (p->bus & EISA_BUS)
		/* EISA card */
		RIOEISAinit(p, RIO_EISA_DEFAULT_MODE);

	if (p->bus & MCA_BUS)
		/* MCA card */
		RIOMCAinit(p, RIO_MCA_DEFAULT_MODE);
#endif
}

/*
** go through memory for an AT host that we pass in the device info
** structure and initialise
*/
void
RIOISAinit(p, mode)
struct rio_info *	p;
int					mode;
{

  /* XXX Need to implement this. */
#if 0
	p->intr_tid = iointset(p->RIOHosts[p->RIONumHosts].Ivec,
					(int (*)())rio_intr, (char*)p->RIONumHosts);

	rio_dprintk (RIO_DEBUG_INIT,  "Set interrupt handler, intr_tid = 0x%x\n", p->intr_tid );

	if (RIODoAT(p, p->RIOHosts[p->RIONumHosts].PaddrP, mode)) {
		return;
	}
	else {
		rio_dprintk (RIO_DEBUG_INIT, "RIODoAT failed\n");
		p->RIOFailed++;
	}
#endif

}

/*
** RIODoAT :
**
** Map in a boards physical address, check that the board is there,
** test the board and if everything is okay assign the board an entry
** in the Rio Hosts structure.
*/
int
RIODoAT(p, Base, mode)
struct rio_info *	p;
int		Base;
int		mode;
{
#define	FOUND		1
#define NOT_FOUND	0

	caddr_t		cardAddr;

	/*
	** Check to see if we actually have a board at this physical address.
	*/
	if ((cardAddr = RIOCheckForATCard(Base)) != 0) {
		/*
		** Now test the board to see if it is working.
		*/
		if (RIOBoardTest(Base, cardAddr, RIO_AT, 0) == RIO_SUCCESS) {
			/*
			** Fill out a slot in the Rio host structure.
			*/
			if (RIOAssignAT(p, Base, cardAddr, mode)) {
				return(FOUND);
			}
		}
		RIOMapout(Base, RIO_AT_MEM_SIZE, cardAddr);
	}
	return(NOT_FOUND);
}

caddr_t
RIOCheckForATCard(Base)
int		Base;
{
	int				off;
	struct DpRam	*cardp;		/* (Points at the host) */
	caddr_t			virtAddr;
	unsigned char			RIOSigTab[24];
/*
** Table of values to search for as prom signature of a host card
*/
	strcpy(RIOSigTab, "JBJGPGGHINSMJPJR");

	/*
	** Hey! Yes, You reading this code! Yo, grab a load a this:
	**
	** IF the card is using WORD MODE rather than BYTE MODE
	** then it will occupy 128K of PHYSICAL memory area. So,
	** you might think that the following Mapin is wrong. Well,
	** it isn't, because the SECOND 64K of occupied space is an
	** EXACT COPY of the FIRST 64K. (good?), so, we need only
	** map it in in one 64K block.
	*/
	if (RIOMapin(Base, RIO_AT_MEM_SIZE, &virtAddr) == -1) {
		rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Couldn't map the board in!\n");
		return((caddr_t)0);
	}

	/*
	** virtAddr points to the DP ram of the system.
	** We now cast this to a pointer to a RIO Host,
	** and have a rummage about in the PROM.
	*/
	cardp = (struct DpRam *)virtAddr;

	for (off=0; RIOSigTab[off]; off++) {
		if ((RBYTE(cardp->DpSignature[off]) & 0xFF) != RIOSigTab[off]) {
			/*
			** Signature mismatch - card not at this address
			*/
			RIOMapout(Base, RIO_AT_MEM_SIZE, virtAddr);
			rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Couldn't match the signature 0x%x 0x%x!\n",
						(int)cardp, off);
			return((caddr_t)0);
		}
	}

	/*
	** If we get here then we must have found a valid board so return
	** its virtual address.
	*/
	return(virtAddr);
}
#endif

/**
** RIOAssignAT :
**
** Fill out the fields in the p->RIOHosts structure now we know we know
** we have a board present.
**
** bits < 0 indicates 8 bit operation requested,
** bits > 0 indicates 16 bit operation.
*/
int
RIOAssignAT(p, Base, virtAddr, mode)
struct rio_info *	p;
int		Base;
caddr_t	virtAddr;
int		mode;
{
	int		bits;
	struct DpRam *cardp = (struct DpRam *)virtAddr;

	if ((Base < ONE_MEG) || (mode & BYTE_ACCESS_MODE))
		bits = BYTE_OPERATION;
	else
		bits = WORD_OPERATION;

	/*
	** Board has passed its scrub test. Fill in all the
	** transient stuff.
	*/
	p->RIOHosts[p->RIONumHosts].Caddr	= virtAddr;
	p->RIOHosts[p->RIONumHosts].CardP	= (struct DpRam *)virtAddr;

	/*
	** Revision 01 AT host cards don't support WORD operations,
	*/
	if ( RBYTE(cardp->DpRevision) == 01 )
		bits = BYTE_OPERATION;

	p->RIOHosts[p->RIONumHosts].Type = RIO_AT;
	p->RIOHosts[p->RIONumHosts].Copy = bcopy;
											/* set this later */
	p->RIOHosts[p->RIONumHosts].Slot = -1;
	p->RIOHosts[p->RIONumHosts].Mode = SLOW_LINKS | SLOW_AT_BUS | bits;
	WBYTE(p->RIOHosts[p->RIONumHosts].Control, 
			BOOT_FROM_RAM | EXTERNAL_BUS_OFF | 
			p->RIOHosts[p->RIONumHosts].Mode | 
			INTERRUPT_DISABLE );
	WBYTE(p->RIOHosts[p->RIONumHosts].ResetInt,0xff);
	WBYTE(p->RIOHosts[p->RIONumHosts].Control,
			BOOT_FROM_RAM | EXTERNAL_BUS_OFF | 
			p->RIOHosts[p->RIONumHosts].Mode |
			INTERRUPT_DISABLE );
	WBYTE(p->RIOHosts[p->RIONumHosts].ResetInt,0xff);
	p->RIOHosts[p->RIONumHosts].UniqueNum =
		((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[0])&0xFF)<<0)|
		((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[1])&0xFF)<<8)|
		((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[2])&0xFF)<<16)|
		((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[3])&0xFF)<<24);
	rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Uniquenum 0x%x\n",p->RIOHosts[p->RIONumHosts].UniqueNum);

	p->RIONumHosts++;
	rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Tests Passed at 0x%x\n", Base);
	return(1);
}
#if 0
#ifdef FUTURE_RELEASE
int RIOMCAinit(int Mode)
{
	uchar SlotNumber;
	caddr_t Caddr;
	uint	Paddr;
	uint	Ivec;
	int	 Handle;
	int	 ret = 0;

	/*
	** Valid mode information for MCA cards
	** is only FAST LINKS
	*/
	Mode = (Mode & FAST_LINKS) ? McaTpFastLinks : McaTpSlowLinks;
	rio_dprintk (RIO_DEBUG_INIT, "RIOMCAinit(%d)\n",Mode);


	/*
	** Check out each of the slots
	*/
	for (SlotNumber = 0; SlotNumber < McaMaxSlots; SlotNumber++) {
	/*
	** Enable the slot we want to talk to
	*/
	outb( McaSlotSelect, SlotNumber | McaSlotEnable );

	/*
	** Read the ID word from the slot
	*/
	if (((inb(McaIdHigh)<< 8)|inb(McaIdLow)) == McaRIOId)
	{
		rio_dprintk (RIO_DEBUG_INIT, "Potential MCA card in slot %d\n", SlotNumber);

		/*
		** Card appears to be a RIO MCA card!
		*/
		RIOMachineType |= (1<<RIO_MCA);

		/*
		** Just check we haven't found too many wonderful objects
		*/
		if ( RIONumHosts >= RIO_HOSTS )
		{
		Rprintf(RIOMesgTooManyCards);
		return(ret);
		}

		/*
		** McaIrqEnable contains the interrupt vector, and a card
		** enable bit.
		*/
		Ivec = inb(McaIrqEnable);

		rio_dprintk (RIO_DEBUG_INIT, "Ivec is %x\n", Ivec);

		switch ( Ivec & McaIrqMask )
		{
		case McaIrq9:
		rio_dprintk (RIO_DEBUG_INIT, "IRQ9\n");
		break;
		case McaIrq3:
		rio_dprintk (RIO_DEBUG_INIT, "IRQ3\n");
		break;
		case McaIrq4:
		rio_dprintk (RIO_DEBUG_INIT, "IRQ4\n");
		break;
		case McaIrq7:
		rio_dprintk (RIO_DEBUG_INIT, "IRQ7\n");
		break;
		case McaIrq10:
		rio_dprintk (RIO_DEBUG_INIT, "IRQ10\n");
		break;
		case McaIrq11:
		rio_dprintk (RIO_DEBUG_INIT, "IRQ11\n");
		break;
		case McaIrq12:
		rio_dprintk (RIO_DEBUG_INIT, "IRQ12\n");
		break;
		case McaIrq15:
		rio_dprintk (RIO_DEBUG_INIT, "IRQ15\n");
		break;
		}

		/*
		** If the card enable bit isn't set, then set it!
		*/
		if ((Ivec & McaCardEnable) != McaCardEnable) {
			rio_dprintk (RIO_DEBUG_INIT, "McaCardEnable not set - setting!\n");
			outb(McaIrqEnable,Ivec|McaCardEnable);
		} else
			rio_dprintk (RIO_DEBUG_INIT, "McaCardEnable already set\n");

		/*
		** Convert the IRQ enable mask into something useful
		*/
		Ivec = RIOMcaToIvec[Ivec & McaIrqMask];

		/*
		** Find the physical address
		*/
		rio_dprintk (RIO_DEBUG_INIT, "inb(McaMemory) is %x\n", inb(McaMemory));
		Paddr = McaAddress(inb(McaMemory));

		rio_dprintk (RIO_DEBUG_INIT, "MCA card has Ivec %d Addr %x\n", Ivec, Paddr);

		if ( Paddr != 0 )
		{

		/*
		** Tell the memory mapper that we want to talk to it
		*/
		Handle = RIOMapin( Paddr, RIO_MCA_MEM_SIZE, &Caddr );

		if ( Handle == -1 ) {
			rio_dprintk (RIO_DEBUG_INIT, "Couldn't map %d bytes at %x\n", RIO_MCA_MEM_SIZE, Paddr;
			continue;
		}

		rio_dprintk (RIO_DEBUG_INIT, "Board mapped to vaddr 0x%x\n", Caddr);

		/*
		** And check that it is actually there!
		*/
		if ( RIOBoardTest( Paddr,Caddr,RIO_MCA,SlotNumber ) == RIO_SUCCESS )
		{
			rio_dprintk (RIO_DEBUG_INIT, "Board has passed test\n");
			rio_dprintk (RIO_DEBUG_INIT, "Slot %d. Type %d. Paddr 0x%x. Caddr 0x%x. Mode 0x%x.\n",
			                            SlotNumber, RIO_MCA, Paddr, Caddr, Mode);

			/*
			** Board has passed its scrub test. Fill in all the
			** transient stuff.
			*/
			p->RIOHosts[RIONumHosts].Slot	 = SlotNumber;
			p->RIOHosts[RIONumHosts].Ivec	 = Ivec;
			p->RIOHosts[RIONumHosts].Type	 = RIO_MCA;
			p->RIOHosts[RIONumHosts].Copy	 = bcopy;
			p->RIOHosts[RIONumHosts].PaddrP   = Paddr;
			p->RIOHosts[RIONumHosts].Caddr	= Caddr;
			p->RIOHosts[RIONumHosts].CardP	= (struct DpRam *)Caddr;
			p->RIOHosts[RIONumHosts].Mode	 = Mode;
			WBYTE(p->RIOHosts[p->RIONumHosts].ResetInt , 0xff);
			p->RIOHosts[RIONumHosts].UniqueNum =
			((RBYTE(p->RIOHosts[RIONumHosts].Unique[0])&0xFF)<<0)|
						((RBYTE(p->RIOHosts[RIONumHosts].Unique[1])&0xFF)<<8)|
			((RBYTE(p->RIOHosts[RIONumHosts].Unique[2])&0xFF)<<16)|
			((RBYTE(p->RIOHosts[RIONumHosts].Unique[3])&0xFF)<<24);
			RIONumHosts++;
			ret++;
		}
		else
		{
			/*
			** It failed the test, so ignore it.
			*/
			rio_dprintk (RIO_DEBUG_INIT, "TEST FAILED\n");
			RIOMapout(Paddr, RIO_MCA_MEM_SIZE, Caddr );
		}
		}
		else
		{
		rio_dprintk (RIO_DEBUG_INIT, "Slot %d - Paddr zero!\n", SlotNumber);
		}
	}
	else
	{
		rio_dprintk (RIO_DEBUG_INIT, "Slot %d NOT RIO\n", SlotNumber);
	}
	}
	/*
	** Now we have checked all the slots, turn off the MCA slot selector
	*/
	outb(McaSlotSelect,0);
	rio_dprintk (RIO_DEBUG_INIT, "Slot %d NOT RIO\n", SlotNumber);
	return ret;
}

int RIOEISAinit( int Mode )
{
	static int EISADone = 0;
	uint Paddr;
	int PollIntMixMsgDone = 0;
	caddr_t Caddr;
	ushort Ident;
	uchar EisaSlot;
	uchar Ivec;
	int ret = 0;

	/*
	** The only valid mode information for EISA hosts is fast or slow
	** links.
	*/
	Mode = (Mode & FAST_LINKS) ? EISA_TP_FAST_LINKS : EISA_TP_SLOW_LINKS;

	if ( EISADone )
	{
		rio_dprintk (RIO_DEBUG_INIT, "RIOEISAinit() - already done, return.\n");
		return(0);
	}

	EISADone++;

	rio_dprintk (RIO_DEBUG_INIT, "RIOEISAinit()\n");


	/*
	** First check all cards to see if ANY are set for polled mode operation.
	** If so, set ALL to polled.
	*/

	for ( EisaSlot=1; EisaSlot<=RIO_MAX_EISA_SLOTS; EisaSlot++ )
	{
	Ident = (INBZ(EisaSlot,EISA_PRODUCT_IDENT_HI)<<8) |
		 INBZ(EisaSlot,EISA_PRODUCT_IDENT_LO);

	if ( Ident == RIO_EISA_IDENT )
	{
		rio_dprintk (RIO_DEBUG_INIT, "Found Specialix product\n");

		if ( INBZ(EisaSlot,EISA_PRODUCT_NUMBER) != RIO_EISA_PRODUCT_CODE )
		{
		rio_dprintk (RIO_DEBUG_INIT, "Not Specialix RIO - Product number %x\n",
						INBZ(EisaSlot, EISA_PRODUCT_NUMBER));
		continue;  /* next slot */
		}
		/*
		** Its a Specialix RIO!
		*/
		rio_dprintk (RIO_DEBUG_INIT, "RIO Revision %d\n",
					INBZ(EisaSlot, EISA_REVISION_NUMBER));
		
		RIOMachineType |= (1<<RIO_EISA);

		/*
		** Just check we haven't found too many wonderful objects
		*/
		if ( RIONumHosts >= RIO_HOSTS )
		{
		Rprintf(RIOMesgTooManyCards);
		return 0;
		}

		/*
		** Ensure that the enable bit is set!
		*/
		OUTBZ( EisaSlot, EISA_ENABLE, RIO_EISA_ENABLE_BIT );

		/*
		** EISA_INTERRUPT_VEC contains the interrupt vector.
		*/
		Ivec = INBZ(EisaSlot,EISA_INTERRUPT_VEC);

#ifdef RIODEBUG
		switch ( Ivec & EISA_INTERRUPT_MASK )
		{
		case EISA_IRQ_3:
			rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 3\n");
		break;
		case EISA_IRQ_4:
			rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 4\n");
		break;
		case EISA_IRQ_5:
			rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 5\n");
		break;
		case EISA_IRQ_6:
			rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 6\n");
		break;
		case EISA_IRQ_7:
			rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 7\n");
		break;
		case EISA_IRQ_9:
			rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 9\n");
		break;
		case EISA_IRQ_10:
			rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 10\n");
		break;
		case EISA_IRQ_11:
			rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 11\n");
		break;
		case EISA_IRQ_12:
			rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 12\n");
		break;
		case EISA_IRQ_14:
			rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 14\n");
		break;
		case EISA_IRQ_15:
			rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 15\n");
		break;
		case EISA_POLLED:
			rio_dprintk (RIO_DEBUG_INIT, "EISA POLLED\n");
		break;
		default:
			rio_dprintk (RIO_DEBUG_INIT, NULL,DBG_INIT|DBG_FAIL,"Shagged interrupt number!\n");
		Ivec &= EISA_CONTROL_MASK;
		}
#endif

		if ( (Ivec & EISA_INTERRUPT_MASK) ==
		 EISA_POLLED )
		{
		RIOWillPoll = 1;
		break;		/* From EisaSlot loop */
		}
	}
	}

	/*
	** Do it all again now we know whether to change all cards to polled
	** mode or not
	*/

	for ( EisaSlot=1; EisaSlot<=RIO_MAX_EISA_SLOTS; EisaSlot++ )
	{
	Ident = (INBZ(EisaSlot,EISA_PRODUCT_IDENT_HI)<<8) |
		 INBZ(EisaSlot,EISA_PRODUCT_IDENT_LO);

	if ( Ident == RIO_EISA_IDENT )
	{
		if ( INBZ(EisaSlot,EISA_PRODUCT_NUMBER) != RIO_EISA_PRODUCT_CODE )
		continue;  /* next slot */

		/*
		** Its a Specialix RIO!
		*/
		
		/*
		** Ensure that the enable bit is set!
		*/
		OUTBZ( EisaSlot, EISA_ENABLE, RIO_EISA_ENABLE_BIT );

		/*
		** EISA_INTERRUPT_VEC contains the interrupt vector.
		*/
		Ivec = INBZ(EisaSlot,EISA_INTERRUPT_VEC);

		if ( RIOWillPoll )
		{
			/*
			** If we are going to operate in polled mode, but this
			** board is configured to be interrupt driven, display
			** the message explaining the situation to the punter,
			** assuming we haven't already done so.
			*/

			if ( !PollIntMixMsgDone &&
			 (Ivec & EISA_INTERRUPT_MASK) != EISA_POLLED )
			{
			Rprintf(RIOMesgAllPolled);
			PollIntMixMsgDone = 1;
			}

			/*
			** Ungraciously ignore whatever the board reports as its
			** interrupt vector...
			*/

			Ivec &= ~EISA_INTERRUPT_MASK;

			/*
			** ...and force it to dance to the poll tune.
			*/

			Ivec |= EISA_POLLED;
		}

		/*
		** Convert the IRQ enable mask into something useful (0-15)
		*/
		Ivec = RIOEisaToIvec(Ivec);

		rio_dprintk (RIO_DEBUG_INIT, "EISA host in slot %d has Ivec 0x%x\n",
		 EisaSlot, Ivec);

		/*
		** Find the physical address
		*/
		Paddr = (INBZ(EisaSlot,EISA_MEMORY_BASE_HI)<<24) |
				(INBZ(EisaSlot,EISA_MEMORY_BASE_LO)<<16);

		rio_dprintk (RIO_DEBUG_INIT, "EISA card has Ivec %d Addr %x\n", Ivec, Paddr);

		if ( Paddr == 0 )
		{
		rio_dprintk (RIO_DEBUG_INIT,
		 "Board in slot %d configured for address zero!\n", EisaSlot);
		continue;
		}

		/*
		** Tell the memory mapper that we want to talk to it
		*/
		rio_dprintk (RIO_DEBUG_INIT, "About to map EISA card \n");

		if (RIOMapin( Paddr, RIO_EISA_MEM_SIZE, &Caddr) == -1) {
		rio_dprintk (RIO_DEBUG_INIT, "Couldn't map %d bytes at %x\n",
							RIO_EISA_MEM_SIZE,Paddr);
		continue;
		}

		rio_dprintk (RIO_DEBUG_INIT, "Board mapped to vaddr 0x%x\n", Caddr);

		/*
		** And check that it is actually there!
		*/
		if ( RIOBoardTest( Paddr,Caddr,RIO_EISA,EisaSlot) == RIO_SUCCESS )
			{
		rio_dprintk (RIO_DEBUG_INIT, "Board has passed test\n");
		rio_dprintk (RIO_DEBUG_INIT, 
		"Slot %d. Ivec %d. Type %d. Paddr 0x%x. Caddr 0x%x. Mode 0x%x.\n",
			EisaSlot,Ivec,RIO_EISA,Paddr,Caddr,Mode);

		/*
		** Board has passed its scrub test. Fill in all the
		** transient stuff.
		*/
		p->RIOHosts[RIONumHosts].Slot	 = EisaSlot;
		p->RIOHosts[RIONumHosts].Ivec	 = Ivec;
		p->RIOHosts[RIONumHosts].Type	 = RIO_EISA;
		p->RIOHosts[RIONumHosts].Copy	 = bcopy;
				p->RIOHosts[RIONumHosts].PaddrP   = Paddr;
				p->RIOHosts[RIONumHosts].Caddr	= Caddr;
		p->RIOHosts[RIONumHosts].CardP	= (struct DpRam *)Caddr;
				p->RIOHosts[RIONumHosts].Mode	 = Mode;
		/*
		** because the EISA prom is mapped into IO space, we
		** need to copy the unqiue number into the memory area
		** that it would have occupied, so that the download
		** code can determine its ID and card type.
		*/
	 WBYTE(p->RIOHosts[RIONumHosts].Unique[0],INBZ(EisaSlot,EISA_UNIQUE_NUM_0));
	 WBYTE(p->RIOHosts[RIONumHosts].Unique[1],INBZ(EisaSlot,EISA_UNIQUE_NUM_1));
	 WBYTE(p->RIOHosts[RIONumHosts].Unique[2],INBZ(EisaSlot,EISA_UNIQUE_NUM_2));
	 WBYTE(p->RIOHosts[RIONumHosts].Unique[3],INBZ(EisaSlot,EISA_UNIQUE_NUM_3));
		p->RIOHosts[RIONumHosts].UniqueNum =
			((RBYTE(p->RIOHosts[RIONumHosts].Unique[0])&0xFF)<<0)|
						((RBYTE(p->RIOHosts[RIONumHosts].Unique[1])&0xFF)<<8)|
			((RBYTE(p->RIOHosts[RIONumHosts].Unique[2])&0xFF)<<16)|
			((RBYTE(p->RIOHosts[RIONumHosts].Unique[3])&0xFF)<<24);
		INBZ(EisaSlot,EISA_INTERRUPT_RESET);
				RIONumHosts++;
		ret++;
			}
		else
		{
		/*
		** It failed the test, so ignore it.
		*/
		rio_dprintk (RIO_DEBUG_INIT, "TEST FAILED\n");

		RIOMapout(Paddr, RIO_EISA_MEM_SIZE, Caddr );
		}
	}
	}
	if (RIOMachineType & RIO_EISA)
	return ret+1;
	return ret;
}
#endif


#ifndef linux

#define CONFIG_ADDRESS	0xcf8
#define CONFIG_DATA		0xcfc
#define FORWARD_REG		0xcfa


static int
read_config(int bus_number, int device_num, int r_number) 
{
	unsigned int cav;
	unsigned int val;

/*
   Build config_address_value:

      31        24 23        16 15      11 10  8 7        0 
      ------------------------------------------------------
      |1| 0000000 | bus_number | device # | 000 | register |
      ------------------------------------------------------
*/

	cav = r_number & 0xff;
	cav |= ((device_num & 0x1f) << 11);
	cav |= ((bus_number & 0xff) << 16);
	cav |= 0x80000000; /* Enable bit */
	outpd(CONFIG_ADDRESS,cav);
	val = inpd(CONFIG_DATA);
	outpd(CONFIG_ADDRESS,0);
	return val;
}

static
write_config(bus_number,device_num,r_number,val) 
{
	unsigned int cav;

/*
   Build config_address_value:

      31        24 23        16 15      11 10  8 7        0 
      ------------------------------------------------------
      |1| 0000000 | bus_number | device # | 000 | register |
      ------------------------------------------------------
*/

	cav = r_number & 0xff;
	cav |= ((device_num & 0x1f) << 11);
	cav |= ((bus_number & 0xff) << 16);
	cav |= 0x80000000; /* Enable bit */
	outpd(CONFIG_ADDRESS, cav);
	outpd(CONFIG_DATA, val);
	outpd(CONFIG_ADDRESS, 0);
	return val;
}
#else
/* XXX Implement these... */
static int
read_config(int bus_number, int device_num, int r_number) 
{
  return 0;
}

static int
write_config(int bus_number, int device_num, int r_number) 
{
  return 0;
}

#endif

int
RIOPCIinit(p, Mode)
struct rio_info	*p;
int 		Mode;
{
	#define MAX_PCI_SLOT		32
	#define RIO_PCI_JET_CARD	0x200011CB

	static int	slot;	/* count of machine's PCI slots searched so far */
	caddr_t		Caddr;	/* Virtual address of the current PCI host card. */
	unsigned char	Ivec;	/* interrupt vector for the current PCI host */
	unsigned long	Paddr;	/* Physical address for the current PCI host */
	int		Handle;	/* Handle to Virtual memory allocated for current PCI host */


	rio_dprintk (RIO_DEBUG_INIT,  "Search for a RIO PCI card - start at slot %d\n", slot);

	/*
	** Initialise the search status
	*/
	p->RIOLastPCISearch	= RIO_FAIL;

	while ( (slot < MAX_PCI_SLOT) & (p->RIOLastPCISearch != RIO_SUCCESS) )
	{
		rio_dprintk (RIO_DEBUG_INIT,  "Currently testing slot %d\n", slot);

		if (read_config(0,slot,0) == RIO_PCI_JET_CARD) {
			p->RIOHosts[p->RIONumHosts].Ivec = 0;
			Paddr = read_config(0,slot,0x18);
			Paddr = Paddr - (Paddr & 0x1); /* Mask off the io bit */

			if ( (Paddr == 0) || ((Paddr & 0xffff0000) == 0xffff0000) ) {
				rio_dprintk (RIO_DEBUG_INIT,  "Goofed up slot\n");	/* what! */
				slot++;
				continue;
			}

			p->RIOHosts[p->RIONumHosts].PaddrP = Paddr;
			Ivec = (read_config(0,slot,0x3c) & 0xff);

			rio_dprintk (RIO_DEBUG_INIT,  "PCI Host at 0x%x, Intr %d\n", (int)Paddr, Ivec);

			Handle = RIOMapin( Paddr, RIO_PCI_MEM_SIZE, &Caddr );
			if (Handle == -1) {
				rio_dprintk (RIO_DEBUG_INIT,  "Couldn't map %d bytes at 0x%x\n", RIO_PCI_MEM_SIZE, (int)Paddr);
				slot++;
				continue;
			}
			p->RIOHosts[p->RIONumHosts].Ivec = Ivec + 32;
			p->intr_tid = iointset(p->RIOHosts[p->RIONumHosts].Ivec,
						(int (*)())rio_intr, (char *)p->RIONumHosts);
			if (RIOBoardTest( Paddr, Caddr, RIO_PCI, 0 ) == RIO_SUCCESS) {
				rio_dprintk (RIO_DEBUG_INIT, ("Board has passed test\n");
				rio_dprintk (RIO_DEBUG_INIT, ("Paddr 0x%x. Caddr 0x%x. Mode 0x%x.\n", Paddr, Caddr, Mode);

				/*
				** Board has passed its scrub test. Fill in all the
				** transient stuff.
				*/
				p->RIOHosts[p->RIONumHosts].Slot	   = 0;
				p->RIOHosts[p->RIONumHosts].Ivec	   = Ivec + 32;
				p->RIOHosts[p->RIONumHosts].Type	   = RIO_PCI;
				p->RIOHosts[p->RIONumHosts].Copy	   = rio_pcicopy; 
				p->RIOHosts[p->RIONumHosts].PaddrP	   = Paddr;
				p->RIOHosts[p->RIONumHosts].Caddr	   = Caddr;
				p->RIOHosts[p->RIONumHosts].CardP	   = (struct DpRam *)Caddr;
				p->RIOHosts[p->RIONumHosts].Mode	   = Mode;

#if 0
				WBYTE(p->RIOHosts[p->RIONumHosts].Control, 
						BOOT_FROM_RAM | EXTERNAL_BUS_OFF | 
						p->RIOHosts[p->RIONumHosts].Mode | 
						INTERRUPT_DISABLE );
				WBYTE(p->RIOHosts[p->RIONumHosts].ResetInt,0xff);
				WBYTE(p->RIOHosts[p->RIONumHosts].Control,
						BOOT_FROM_RAM | EXTERNAL_BUS_OFF | 
						p->RIOHosts[p->RIONumHosts].Mode |
						INTERRUPT_DISABLE );
				WBYTE(p->RIOHosts[p->RIONumHosts].ResetInt,0xff);
#else
				WBYTE(p->RIOHosts[p->RIONumHosts].ResetInt, 0xff);
#endif
				p->RIOHosts[p->RIONumHosts].UniqueNum  =
					((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[0])&0xFF)<<0)|
					((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[1])&0xFF)<<8)|
					((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[2])&0xFF)<<16)|
					((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[3])&0xFF)<<24);

				rio_dprintk (RIO_DEBUG_INIT, "Unique no 0x%x.\n", 
				    p->RIOHosts[p->RIONumHosts].UniqueNum);

				p->RIOLastPCISearch = RIO_SUCCESS;
				p->RIONumHosts++;
			}
		}
		slot++;
	}

	if ( slot >= MAX_PCI_SLOT ) {
		rio_dprintk (RIO_DEBUG_INIT,  "All %d PCI slots have tested for RIO cards !!!\n",
			     MAX_PCI_SLOT);
	}


	/*
	** I don't think we want to do this anymore
	**

	if (!p->RIOLastPCISearch == RIO_FAIL ) {
		p->RIOFailed++;
	}

	**
	*/
}

#ifdef FUTURE_RELEASE
void riohalt( void )
{
	int host;
	for ( host=0; host<p->RIONumHosts; host++ )
	{
		rio_dprintk (RIO_DEBUG_INIT, "Stop host %d\n", host);
		(void)RIOBoardTest( p->RIOHosts[host].PaddrP, p->RIOHosts[host].Caddr, p->RIOHosts[host].Type,p->RIOHosts[host].Slot );
	}
}
#endif
#endif

static	uchar	val[] = {
#ifdef VERY_LONG_TEST
	  0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
	  0xa5, 0xff, 0x5a, 0x00, 0xff, 0xc9, 0x36, 
#endif
	  0xff, 0x00, 0x00 };

#define	TEST_END sizeof(val)

/*
** RAM test a board. 
** Nothing too complicated, just enough to check it out.
*/
int
RIOBoardTest(paddr, caddr, type, slot)
paddr_t	paddr;
caddr_t	caddr;
uchar	type;
int		slot;
{
	struct DpRam *DpRam = (struct DpRam *)caddr;
	char *ram[4];
	int  size[4];
	int  op, bank;
	int  nbanks;

	rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Reset host type=%d, DpRam=0x%x, slot=%d\n",
			type,(int)DpRam, slot);

	RIOHostReset(type, DpRam, slot);

	/*
	** Scrub the memory. This comes in several banks:
	** DPsram1	- 7000h bytes
	** DPsram2	- 200h  bytes
	** DPsram3	- 7000h bytes
	** scratch	- 1000h bytes
	*/

	rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Setup ram/size arrays\n");

	size[0] = DP_SRAM1_SIZE;
	size[1] = DP_SRAM2_SIZE;
	size[2] = DP_SRAM3_SIZE;
	size[3] = DP_SCRATCH_SIZE;

	ram[0] = (char *)&DpRam->DpSram1[0];
	ram[1] = (char *)&DpRam->DpSram2[0];
	ram[2] = (char *)&DpRam->DpSram3[0];
	nbanks = (type == RIO_PCI) ? 3 : 4;
	if (nbanks == 4)
		ram[3] = (char *)&DpRam->DpScratch[0];


	if (nbanks == 3) {
		rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Memory: 0x%x(0x%x), 0x%x(0x%x), 0x%x(0x%x)\n",
				(int)ram[0], size[0], (int)ram[1], size[1], (int)ram[2], size[2]);
	} else {
		rio_dprintk (RIO_DEBUG_INIT, "RIO-init: 0x%x(0x%x), 0x%x(0x%x), 0x%x(0x%x), 0x%x(0x%x)\n",
			(int)ram[0], size[0], (int)ram[1], size[1], (int)ram[2], size[2], (int)ram[3], 
					size[3]);
	}

	/*
	** This scrub operation will test for crosstalk between
	** banks. TEST_END is a magic number, and relates to the offset
	** within the 'val' array used by Scrub.
	*/
	for (op=0; op<TEST_END; op++) {
		for (bank=0; bank<nbanks; bank++) {
			if (RIOScrub(op, (BYTE *)ram[bank], size[bank]) == RIO_FAIL) {
				rio_dprintk (RIO_DEBUG_INIT, "RIO-init: RIOScrub band %d, op %d failed\n", 
							bank, op);
				return RIO_FAIL;
			}
		}
	}

	rio_dprintk (RIO_DEBUG_INIT, "Test completed\n");
	return RIO_SUCCESS;
}


/*
** Scrub an area of RAM.
** Define PRETEST and POSTTEST for a more thorough checking of the
** state of the memory.
** Call with op set to an index into the above 'val' array to determine
** which value will be written into memory.
** Call with op set to zero means that the RAM will not be read and checked
** before it is written.
** Call with op not zero, and the RAM will be read and compated with val[op-1]
** to check that the data from the previous phase was retained.
*/
static int
RIOScrub(op, ram, size)
int		op;
BYTE *	ram;
int		size; 
{
	int				off;
	unsigned char	oldbyte;
	unsigned char	newbyte;
	unsigned char	invbyte;
	unsigned short	oldword;
	unsigned short	newword;
	unsigned short	invword;
	unsigned short	swapword;

	if (op) {
		oldbyte = val[op-1];
		oldword = oldbyte | (oldbyte<<8);
	} else
	  oldbyte = oldword = 0; /* Tell the compiler we've initilalized them. */
	newbyte = val[op];
	newword = newbyte | (newbyte<<8);
	invbyte = ~newbyte;
	invword = invbyte | (invbyte<<8);

	/*
	** Check that the RAM contains the value that should have been left there
	** by the previous test (not applicable for pass zero)
	*/
	if (op) {
		for (off=0; off<size; off++) {
			if (RBYTE(ram[off]) != oldbyte) {
				rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Byte Pre Check 1: BYTE at offset 0x%x should have been=%x, was=%x\n", off, oldbyte, RBYTE(ram[off]));
				return RIO_FAIL;
			}
		}
		for (off=0; off<size; off+=2) {
			if (*(ushort *)&ram[off] != oldword) {
				rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Word Pre Check: WORD at offset 0x%x should have been=%x, was=%x\n",off,oldword,*(ushort *)&ram[off]);
				rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Word Pre Check: BYTE at offset 0x%x is %x BYTE at offset 0x%x is %x\n", off, RBYTE(ram[off]), off+1, RBYTE(ram[off+1]));
				return RIO_FAIL;
			}
		}
	}

	/*
	** Now write the INVERSE of the test data into every location, using
	** BYTE write operations, first checking before each byte is written
	** that the location contains the old value still, and checking after
	** the write that the location contains the data specified - this is
	** the BYTE read/write test.
	*/
	for (off=0; off<size; off++) {
		if (op && (RBYTE(ram[off]) != oldbyte)) {
			rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Byte Pre Check 2: BYTE at offset 0x%x should have been=%x, was=%x\n", off, oldbyte, RBYTE(ram[off]));
			return RIO_FAIL;
		}
		WBYTE(ram[off],invbyte);
		if (RBYTE(ram[off]) != invbyte) {
			rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Byte Inv Check: BYTE at offset 0x%x should have been=%x, was=%x\n", off, invbyte, RBYTE(ram[off]));
			return RIO_FAIL;
		}
	}

	/*
	** now, use WORD operations to write the test value into every location,
	** check as before that the location contains the previous test value
	** before overwriting, and that it contains the data value written
	** afterwards.
	** This is the WORD operation test.
	*/
	for (off=0; off<size; off+=2) {
		if (*(ushort *)&ram[off] != invword) {
			rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Word Inv Check: WORD at offset 0x%x should have been=%x, was=%x\n", off, invword, *(ushort *)&ram[off]);
		rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Word Inv Check: BYTE at offset 0x%x is %x BYTE at offset 0x%x is %x\n", off, RBYTE(ram[off]), off+1, RBYTE(ram[off+1]));
			return RIO_FAIL;
		}

		*(ushort *)&ram[off] = newword;
		if ( *(ushort *)&ram[off] != newword ) {
			rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Post Word Check 1: WORD at offset 0x%x should have been=%x, was=%x\n", off, newword, *(ushort *)&ram[off]);
			rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Post Word Check 1: BYTE at offset 0x%x is %x BYTE at offset 0x%x is %x\n", off, RBYTE(ram[off]), off+1, RBYTE(ram[off+1]));
			return RIO_FAIL;
		}
	}

	/*
	** now run through the block of memory again, first in byte mode
	** then in word mode, and check that all the locations contain the
	** required test data.
	*/
	for (off=0; off<size; off++) {
		if (RBYTE(ram[off]) != newbyte) {
			rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Post Byte Check: BYTE at offset 0x%x should have been=%x, was=%x\n", off, newbyte, RBYTE(ram[off]));
			return RIO_FAIL;
		}
	}

	for (off=0; off<size; off+=2) {
		if ( *(ushort *)&ram[off] != newword ) {
			rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Post Word Check 2: WORD at offset 0x%x should have been=%x, was=%x\n", off, newword, *(ushort *)&ram[off]);
			rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Post Word Check 2: BYTE at offset 0x%x is %x BYTE at offset 0x%x is %x\n", off, RBYTE(ram[off]), off+1, RBYTE(ram[off+1]));
			return RIO_FAIL;
		}
	}

	/*
	** time to check out byte swapping errors
	*/
	swapword = invbyte | (newbyte << 8);

	for (off=0; off<size; off+=2) {
		WBYTE(ram[off],invbyte);
		WBYTE(ram[off+1],newbyte);
	}

	for ( off=0; off<size; off+=2 ) {
		if (*(ushort *)&ram[off] != swapword) {
			rio_dprintk (RIO_DEBUG_INIT, "RIO-init: SwapWord Check 1: WORD at offset 0x%x should have been=%x, was=%x\n", off, swapword, *((ushort *)&ram[off]));
			rio_dprintk (RIO_DEBUG_INIT, "RIO-init: SwapWord Check 1: BYTE at offset 0x%x is %x BYTE at offset 0x%x is %x\n", off, RBYTE(ram[off]), off+1, RBYTE(ram[off+1]));
			return RIO_FAIL;
		}
		*((ushort *)&ram[off]) = ~swapword;
	}

	for (off=0; off<size; off+=2) {
		if (RBYTE(ram[off]) != newbyte) {
			rio_dprintk (RIO_DEBUG_INIT, "RIO-init: SwapWord Check 2: BYTE at offset 0x%x should have been=%x, was=%x\n", off, newbyte, RBYTE(ram[off]));
			return RIO_FAIL;
		}
		if (RBYTE(ram[off+1]) != invbyte) {
			rio_dprintk (RIO_DEBUG_INIT, "RIO-init: SwapWord Check 2: BYTE at offset 0x%x should have been=%x, was=%x\n", off+1, invbyte, RBYTE(ram[off+1]));
			return RIO_FAIL;
		}
		*((ushort *)&ram[off]) = newword;
	}
	return RIO_SUCCESS;
}

/*
** try to ensure that every host is either in polled mode
** or is in interrupt mode. Only allow interrupt mode if
** all hosts can interrupt (why?)
** and force into polled mode if told to. Patch up the
** interrupt vector & salute The Queen when you've done.
*/
#if 0
static void
RIOAllocateInterrupts(p)
struct rio_info *	p;
{
	int Host;

	/*
	** Easy case - if we have been told to poll, then we poll.
	*/
	if (p->mode & POLLED_MODE) {
		RIOStopInterrupts(p, 0, 0);
		return;
	}

	/*
	** check - if any host has been set to polled mode, then all must be.
	*/
	for (Host=0; Host<p->RIONumHosts; Host++) {
		if ( (p->RIOHosts[Host].Type != RIO_AT) &&
				(p->RIOHosts[Host].Ivec == POLLED) ) {
			RIOStopInterrupts(p, 1, Host );
			return;
		}
	}
	for (Host=0; Host<p->RIONumHosts; Host++) {
		if (p->RIOHosts[Host].Type == RIO_AT) {
			if ( (p->RIOHosts[Host].Ivec - 32) == 0) {
				RIOStopInterrupts(p, 2, Host );
				return;
			}
		}
	}
}

/*
** something has decided that we can't be doing with these
** new-fangled interrupt thingies. Set everything up to just
** poll.
*/
static void
RIOStopInterrupts(p, Reason, Host)
struct rio_info *	p;
int	Reason;
int	Host; 
{
#ifdef FUTURE_RELEASE
	switch (Reason) {
		case 0:	/* forced into polling by rio_polled */
			break;
		case 1:	/* SCU has set 'Host' into polled mode */
			break;
		case 2:	/* there aren't enough interrupt vectors for 'Host' */
			break;
	}
#endif

	for (Host=0; Host<p->RIONumHosts; Host++ ) {
		struct Host *HostP = &p->RIOHosts[Host];

		switch (HostP->Type) {
			case RIO_AT:
				/*
				** The AT host has it's interrupts disabled by clearing the
				** int_enable bit.
				*/
				HostP->Mode &= ~INTERRUPT_ENABLE;
				HostP->Ivec = POLLED;
				break;
#ifdef FUTURE_RELEASE
			case RIO_EISA:
				/*
				** The EISA host has it's interrupts disabled by setting the
				** Ivec to zero
				*/
				HostP->Ivec = POLLED;
				break;
#endif
			case RIO_PCI:
				/*
				** The PCI host has it's interrupts disabled by clearing the
				** int_enable bit, like a regular host card.
				*/
				HostP->Mode &= ~RIO_PCI_INT_ENABLE;
				HostP->Ivec = POLLED;
				break;
#ifdef FUTURE_RELEASE
			case RIO_MCA:
				/*
				** There's always one, isn't there?
				** The MCA host card cannot have it's interrupts disabled.
				*/
				RIOPatchVec(HostP);
				break;
#endif
		}
	}
}

/*
** This function is called at init time to setup the data structures.
*/
void
RIOAllocDataStructs(p)
struct rio_info *	p;
{
	int	port,
		host,
		tm;

	p->RIOPortp = (struct Port *)sysbrk(RIO_PORTS * sizeof(struct Port));
	if (!p->RIOPortp) {
		rio_dprintk (RIO_DEBUG_INIT, "RIO-init: No memory for port structures\n");
		p->RIOFailed++;
		return;
	} 
	bzero( p->RIOPortp, sizeof(struct Port) * RIO_PORTS );
	rio_dprintk (RIO_DEBUG_INIT,  "RIO-init: allocated and cleared memory for port structs\n");
	rio_dprintk (RIO_DEBUG_INIT,  "First RIO port struct @0x%x, size=0x%x bytes\n",
	    (int)p->RIOPortp, sizeof(struct Port));

	for( port=0; port<RIO_PORTS; port++ ) {
		p->RIOPortp[port].PortNum = port;
		p->RIOPortp[port].TtyP = &p->channel[port];
		sreset (p->RIOPortp[port].InUse);	/* Let the first guy uses it */
		p->RIOPortp[port].portSem = -1;	/* Let the first guy takes it */
		p->RIOPortp[port].ParamSem = -1;	/* Let the first guy takes it */
		p->RIOPortp[port].timeout_id = 0;	/* Let the first guy takes it */
	}

	p->RIOHosts = (struct Host *)sysbrk(RIO_HOSTS * sizeof(struct Host));
	if (!p->RIOHosts) {
		rio_dprintk (RIO_DEBUG_INIT, "RIO-init: No memory for host structures\n");
		p->RIOFailed++;
		return;
	}
	bzero(p->RIOHosts, sizeof(struct Host)*RIO_HOSTS);
	rio_dprintk (RIO_DEBUG_INIT,  "RIO-init: allocated and cleared memory for host structs\n");
	rio_dprintk (RIO_DEBUG_INIT,  "First RIO host struct @0x%x, size=0x%x bytes\n",
	    (int)p->RIOHosts, sizeof(struct Host));

	for( host=0; host<RIO_HOSTS; host++ ) {
		spin_lock_init (&p->RIOHosts[host].HostLock);
		p->RIOHosts[host].timeout_id = 0; /* Let the first guy takes it */
	}
	/*
	** check that the buffer size is valid, round down to the next power of
	** two if necessary; if the result is zero, then, hey, no double buffers.
	*/
	for ( tm = 1; tm && tm <= p->RIOConf.BufferSize; tm <<= 1 )
		;
	tm >>= 1;
	p->RIOBufferSize = tm;
	p->RIOBufferMask = tm ? tm - 1 : 0;
}

/*
** this function gets called whenever the data structures need to be
** re-setup, for example, after a riohalt (why did I ever invent it?)
*/
void
RIOSetupDataStructs(p)
struct rio_info	* p;
{
	int host, entry, rup;

	for ( host=0; host<RIO_HOSTS; host++ ) {
		struct Host *HostP = &p->RIOHosts[host];
		for ( entry=0; entry<LINKS_PER_UNIT; entry++ ) {
			HostP->Topology[entry].Unit = ROUTE_DISCONNECT;
			HostP->Topology[entry].Link = NO_LINK;
		}
		bcopy("HOST X", HostP->Name, 7);
		HostP->Name[5] = '1'+host;
		for (rup=0; rup<(MAX_RUP + LINKS_PER_UNIT); rup++) {
			if (rup < MAX_RUP) {
				for (entry=0; entry<LINKS_PER_UNIT; entry++ ) {
					HostP->Mapping[rup].Topology[entry].Unit = ROUTE_DISCONNECT;
					HostP->Mapping[rup].Topology[entry].Link = NO_LINK;
				}
				RIODefaultName(p, HostP, rup);
			}
			spin_lock_init(&HostP->UnixRups[rup].RupLock);
		}
	}
}
#endif

int
RIODefaultName(p, HostP, UnitId)
struct rio_info *	p;
struct Host *	HostP;
uint			UnitId;
{
#ifdef CHECK
	CheckHost( Host );
	CheckUnitId( UnitId );
#endif
	bcopy("UNKNOWN RTA X-XX",HostP->Mapping[UnitId].Name,17);
	HostP->Mapping[UnitId].Name[12]='1'+(HostP-p->RIOHosts);
	if ((UnitId+1) > 9) {
		HostP->Mapping[UnitId].Name[14]='0'+((UnitId+1)/10);
		HostP->Mapping[UnitId].Name[15]='0'+((UnitId+1)%10);
	}
	else {
		HostP->Mapping[UnitId].Name[14]='1'+UnitId;
		HostP->Mapping[UnitId].Name[15]=0;
	}
	return 0;
}

#define RIO_RELEASE	"Linux"
#define RELEASE_ID	"1.0"

#if 0
static int
RIOReport(p)
struct rio_info *	p;
{
	char *	RIORelease = RIO_RELEASE;
	char *	RIORelID = RELEASE_ID;
	int		host;

	rio_dprintk (RIO_DEBUG_INIT, "RIO : Release: %s ID: %s\n", RIORelease, RIORelID);

	if ( p->RIONumHosts==0 ) {
		rio_dprintk (RIO_DEBUG_INIT, "\nNo Hosts configured\n");
		return(0);
	}

	for ( host=0; host < p->RIONumHosts; host++ ) {
		struct Host *HostP = &p->RIOHosts[host];
		switch ( HostP->Type ) {
			case RIO_AT:
				rio_dprintk (RIO_DEBUG_INIT, "AT BUS : found the card at 0x%x\n", HostP->PaddrP);
		}
	}
	return 0;
}
#endif

static struct rioVersion	stVersion;

struct rioVersion *
RIOVersid(void)
{
    strlcpy(stVersion.version, "RIO driver for linux V1.0",
	    sizeof(stVersion.version));
    strlcpy(stVersion.buildDate, __DATE__,
	    sizeof(stVersion.buildDate));

    return &stVersion;
}

#if 0
int
RIOMapin(paddr, size, vaddr)
paddr_t		paddr;
int			size;
caddr_t *	vaddr;
{
	*vaddr = (caddr_t)permap( (long)paddr, size);
	return ((int)*vaddr);
}

void
RIOMapout(paddr, size, vaddr)
paddr_t		paddr;
long		size;
caddr_t 	vaddr;
{
}
#endif


void
RIOHostReset(Type, DpRamP, Slot)
uint Type;
volatile struct DpRam *DpRamP;
uint Slot; 
{
	/*
	** Reset the Tpu
	*/
	rio_dprintk (RIO_DEBUG_INIT,  "RIOHostReset: type 0x%x", Type);
	switch ( Type ) {
		case RIO_AT:
			rio_dprintk (RIO_DEBUG_INIT, " (RIO_AT)\n");
			WBYTE(DpRamP->DpControl,  BOOT_FROM_RAM | EXTERNAL_BUS_OFF | 
					  INTERRUPT_DISABLE | BYTE_OPERATION |
					  SLOW_LINKS | SLOW_AT_BUS);
			WBYTE(DpRamP->DpResetTpu, 0xFF);
			udelay(3);

			rio_dprintk (RIO_DEBUG_INIT,  "RIOHostReset: Don't know if it worked. Try reset again\n");
			WBYTE(DpRamP->DpControl,  BOOT_FROM_RAM | EXTERNAL_BUS_OFF |
					  INTERRUPT_DISABLE | BYTE_OPERATION |
					  SLOW_LINKS | SLOW_AT_BUS);
			WBYTE(DpRamP->DpResetTpu, 0xFF);
			udelay(3);
			break;
#ifdef FUTURE_RELEASE
	case RIO_EISA:
	/*
	** Bet this doesn't work!
	*/
	OUTBZ( Slot, EISA_CONTROL_PORT,
		EISA_TP_RUN		| EISA_TP_BUS_DISABLE   |
		EISA_TP_SLOW_LINKS | EISA_TP_BOOT_FROM_RAM );
	OUTBZ( Slot, EISA_CONTROL_PORT,
		EISA_TP_RESET	  | EISA_TP_BUS_DISABLE   | 
		EISA_TP_SLOW_LINKS | EISA_TP_BOOT_FROM_RAM );
	suspend( 3 );
	OUTBZ( Slot, EISA_CONTROL_PORT,
		EISA_TP_RUN		| EISA_TP_BUS_DISABLE   | 
		EISA_TP_SLOW_LINKS | EISA_TP_BOOT_FROM_RAM );
	break;
	case RIO_MCA:
	WBYTE(DpRamP->DpControl  , McaTpBootFromRam | McaTpBusDisable );
	WBYTE(DpRamP->DpResetTpu , 0xFF );
	suspend( 3 );
	WBYTE(DpRamP->DpControl  , McaTpBootFromRam | McaTpBusDisable );
	WBYTE(DpRamP->DpResetTpu , 0xFF );
	suspend( 3 );
		break;
#endif
	case RIO_PCI:
		rio_dprintk (RIO_DEBUG_INIT, " (RIO_PCI)\n");
		DpRamP->DpControl  = RIO_PCI_BOOT_FROM_RAM;
		DpRamP->DpResetInt = 0xFF;
		DpRamP->DpResetTpu = 0xFF;
		udelay(100);
		/* for (i=0; i<6000; i++);  */
		/* suspend( 3 ); */
		break;
#ifdef FUTURE_RELEASE
	default:
	Rprintf(RIOMesgNoSupport,Type,DpRamP,Slot);
	return;
#endif

	default:
		rio_dprintk (RIO_DEBUG_INIT, " (UNKNOWN)\n");
		break;
	}
	return;
}