[PATCH] turn many #if $undefined_string into #ifdef $undefined_string
[safe/jmp/linux-2.6] / drivers / char / rio / rioinit.c
1 /*
2 ** -----------------------------------------------------------------------------
3 **
4 **  Perle Specialix driver for Linux
5 **  Ported from existing RIO Driver for SCO sources.
6  *
7  *  (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8  *
9  *      This program is free software; you can redistribute it and/or modify
10  *      it under the terms of the GNU General Public License as published by
11  *      the Free Software Foundation; either version 2 of the License, or
12  *      (at your option) any later version.
13  *
14  *      This program is distributed in the hope that it will be useful,
15  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *      GNU General Public License for more details.
18  *
19  *      You should have received a copy of the GNU General Public License
20  *      along with this program; if not, write to the Free Software
21  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 **
23 **      Module          : rioinit.c
24 **      SID             : 1.3
25 **      Last Modified   : 11/6/98 10:33:43
26 **      Retrieved       : 11/6/98 10:33:49
27 **
28 **  ident @(#)rioinit.c 1.3
29 **
30 ** -----------------------------------------------------------------------------
31 */
32 #ifdef SCCS_LABELS
33 static char *_rioinit_c_sccs_ = "@(#)rioinit.c  1.3";
34 #endif
35
36 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/slab.h>
39 #include <linux/errno.h>
40 #include <linux/delay.h>
41 #include <asm/io.h>
42 #include <asm/system.h>
43 #include <asm/string.h>
44 #include <asm/semaphore.h>
45 #include <asm/uaccess.h>
46
47 #include <linux/termios.h>
48 #include <linux/serial.h>
49
50 #include <linux/generic_serial.h>
51
52
53 #include "linux_compat.h"
54 #include "typdef.h"
55 #include "pkt.h"
56 #include "daemon.h"
57 #include "rio.h"
58 #include "riospace.h"
59 #include "top.h"
60 #include "cmdpkt.h"
61 #include "map.h"
62 #include "riotypes.h"
63 #include "rup.h"
64 #include "port.h"
65 #include "riodrvr.h"
66 #include "rioinfo.h"
67 #include "func.h"
68 #include "errors.h"
69 #include "pci.h"
70
71 #include "parmmap.h"
72 #include "unixrup.h"
73 #include "board.h"
74 #include "host.h"
75 #include "error.h"
76 #include "phb.h"
77 #include "link.h"
78 #include "cmdblk.h"
79 #include "route.h"
80 #include "control.h"
81 #include "cirrus.h"
82 #include "rioioctl.h"
83 #include "rio_linux.h"
84
85 #undef bcopy
86 #define bcopy rio_pcicopy
87
88 int RIOPCIinit(struct rio_info *p, int Mode);
89
90 #if 0
91 static void RIOAllocateInterrupts(struct rio_info *);
92 static int RIOReport(struct rio_info *);
93 static void RIOStopInterrupts(struct rio_info *, int, int);
94 #endif
95
96 static int RIOScrub(int, BYTE *, int);
97
98 #if 0
99 extern int      rio_intr();
100
101 /*
102 **      Init time code.
103 */
104 void
105 rioinit( p, info )
106 struct rio_info         * p;
107 struct RioHostInfo      * info;
108 {
109         /*
110         ** Multi-Host card support - taking the easy way out - sorry !
111         ** We allocate and set up the Host and Port structs when the
112         ** driver is called to 'install' the first host.
113         ** We check for this first 'call' by testing the RIOPortp pointer.
114         */
115         if ( !p->RIOPortp )
116         {
117                 rio_dprintk (RIO_DEBUG_INIT,  "Allocating and setting up driver data structures\n");
118
119                 RIOAllocDataStructs(p);         /* allocate host/port structs */
120                 RIOSetupDataStructs(p);         /* setup topology structs */
121         }
122
123         RIOInitHosts( p, info );        /* hunt down the hardware */
124
125         RIOAllocateInterrupts(p);       /* allocate interrupts */
126         RIOReport(p);                   /* show what we found */
127 }
128
129 /*
130 ** Initialise the Cards 
131 */ 
132 void
133 RIOInitHosts(p, info)
134 struct rio_info         * p;
135 struct RioHostInfo      * info;
136 {
137 /*
138 ** 15.10.1998 ARG - ESIL 0762 part fix
139 ** If there is no ISA card definition - we always look for PCI cards.
140 ** As we currently only support one host card this lets an ISA card
141 ** definition take precedence over PLUG and PLAY.
142 ** No ISA card - we are PLUG and PLAY with PCI.
143 */
144
145         /*
146         ** Note - for PCI both these will be zero, that's okay because
147         ** RIOPCIInit() fills them in if a card is found.
148         */
149         p->RIOHosts[p->RIONumHosts].Ivec        = info->vector;
150         p->RIOHosts[p->RIONumHosts].PaddrP      = info->location;
151
152         /*
153         ** Check that we are able to accommodate another host
154         */
155         if ( p->RIONumHosts >= RIO_HOSTS )
156         {
157                 p->RIOFailed++;
158                 return;
159         }
160
161         if ( info->bus & ISA_BUS )
162         {
163                 rio_dprintk (RIO_DEBUG_INIT,  "initialising card %d (ISA)\n", p->RIONumHosts);
164                 RIOISAinit(p, p->mode);
165         }
166         else
167         {
168                 rio_dprintk (RIO_DEBUG_INIT,  "initialising card %d (PCI)\n", p->RIONumHosts);
169                 RIOPCIinit(p, RIO_PCI_DEFAULT_MODE);
170         }
171
172         rio_dprintk (RIO_DEBUG_INIT,  "Total hosts initialised so far : %d\n", p->RIONumHosts);
173
174
175 #ifdef FUTURE_RELEASE
176         if (p->bus & EISA_BUS)
177                 /* EISA card */
178                 RIOEISAinit(p, RIO_EISA_DEFAULT_MODE);
179
180         if (p->bus & MCA_BUS)
181                 /* MCA card */
182                 RIOMCAinit(p, RIO_MCA_DEFAULT_MODE);
183 #endif
184 }
185
186 /*
187 ** go through memory for an AT host that we pass in the device info
188 ** structure and initialise
189 */
190 void
191 RIOISAinit(p, mode)
192 struct rio_info *       p;
193 int                                     mode;
194 {
195
196   /* XXX Need to implement this. */
197 #if 0
198         p->intr_tid = iointset(p->RIOHosts[p->RIONumHosts].Ivec,
199                                         (int (*)())rio_intr, (char*)p->RIONumHosts);
200
201         rio_dprintk (RIO_DEBUG_INIT,  "Set interrupt handler, intr_tid = 0x%x\n", p->intr_tid );
202
203         if (RIODoAT(p, p->RIOHosts[p->RIONumHosts].PaddrP, mode)) {
204                 return;
205         }
206         else {
207                 rio_dprintk (RIO_DEBUG_INIT, "RIODoAT failed\n");
208                 p->RIOFailed++;
209         }
210 #endif
211
212 }
213
214 /*
215 ** RIODoAT :
216 **
217 ** Map in a boards physical address, check that the board is there,
218 ** test the board and if everything is okay assign the board an entry
219 ** in the Rio Hosts structure.
220 */
221 int
222 RIODoAT(p, Base, mode)
223 struct rio_info *       p;
224 int             Base;
225 int             mode;
226 {
227 #define FOUND           1
228 #define NOT_FOUND       0
229
230         caddr_t         cardAddr;
231
232         /*
233         ** Check to see if we actually have a board at this physical address.
234         */
235         if ((cardAddr = RIOCheckForATCard(Base)) != 0) {
236                 /*
237                 ** Now test the board to see if it is working.
238                 */
239                 if (RIOBoardTest(Base, cardAddr, RIO_AT, 0) == RIO_SUCCESS) {
240                         /*
241                         ** Fill out a slot in the Rio host structure.
242                         */
243                         if (RIOAssignAT(p, Base, cardAddr, mode)) {
244                                 return(FOUND);
245                         }
246                 }
247                 RIOMapout(Base, RIO_AT_MEM_SIZE, cardAddr);
248         }
249         return(NOT_FOUND);
250 }
251
252 caddr_t
253 RIOCheckForATCard(Base)
254 int             Base;
255 {
256         int                             off;
257         struct DpRam    *cardp;         /* (Points at the host) */
258         caddr_t                 virtAddr;
259         unsigned char                   RIOSigTab[24];
260 /*
261 ** Table of values to search for as prom signature of a host card
262 */
263         strcpy(RIOSigTab, "JBJGPGGHINSMJPJR");
264
265         /*
266         ** Hey! Yes, You reading this code! Yo, grab a load a this:
267         **
268         ** IF the card is using WORD MODE rather than BYTE MODE
269         ** then it will occupy 128K of PHYSICAL memory area. So,
270         ** you might think that the following Mapin is wrong. Well,
271         ** it isn't, because the SECOND 64K of occupied space is an
272         ** EXACT COPY of the FIRST 64K. (good?), so, we need only
273         ** map it in in one 64K block.
274         */
275         if (RIOMapin(Base, RIO_AT_MEM_SIZE, &virtAddr) == -1) {
276                 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Couldn't map the board in!\n");
277                 return((caddr_t)0);
278         }
279
280         /*
281         ** virtAddr points to the DP ram of the system.
282         ** We now cast this to a pointer to a RIO Host,
283         ** and have a rummage about in the PROM.
284         */
285         cardp = (struct DpRam *)virtAddr;
286
287         for (off=0; RIOSigTab[off]; off++) {
288                 if ((RBYTE(cardp->DpSignature[off]) & 0xFF) != RIOSigTab[off]) {
289                         /*
290                         ** Signature mismatch - card not at this address
291                         */
292                         RIOMapout(Base, RIO_AT_MEM_SIZE, virtAddr);
293                         rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Couldn't match the signature 0x%x 0x%x!\n",
294                                                 (int)cardp, off);
295                         return((caddr_t)0);
296                 }
297         }
298
299         /*
300         ** If we get here then we must have found a valid board so return
301         ** its virtual address.
302         */
303         return(virtAddr);
304 }
305 #endif
306
307 /**
308 ** RIOAssignAT :
309 **
310 ** Fill out the fields in the p->RIOHosts structure now we know we know
311 ** we have a board present.
312 **
313 ** bits < 0 indicates 8 bit operation requested,
314 ** bits > 0 indicates 16 bit operation.
315 */
316 int
317 RIOAssignAT(p, Base, virtAddr, mode)
318 struct rio_info *       p;
319 int             Base;
320 caddr_t virtAddr;
321 int             mode;
322 {
323         int             bits;
324         struct DpRam *cardp = (struct DpRam *)virtAddr;
325
326         if ((Base < ONE_MEG) || (mode & BYTE_ACCESS_MODE))
327                 bits = BYTE_OPERATION;
328         else
329                 bits = WORD_OPERATION;
330
331         /*
332         ** Board has passed its scrub test. Fill in all the
333         ** transient stuff.
334         */
335         p->RIOHosts[p->RIONumHosts].Caddr       = virtAddr;
336         p->RIOHosts[p->RIONumHosts].CardP       = (struct DpRam *)virtAddr;
337
338         /*
339         ** Revision 01 AT host cards don't support WORD operations,
340         */
341         if ( RBYTE(cardp->DpRevision) == 01 )
342                 bits = BYTE_OPERATION;
343
344         p->RIOHosts[p->RIONumHosts].Type = RIO_AT;
345         p->RIOHosts[p->RIONumHosts].Copy = bcopy;
346                                                                                         /* set this later */
347         p->RIOHosts[p->RIONumHosts].Slot = -1;
348         p->RIOHosts[p->RIONumHosts].Mode = SLOW_LINKS | SLOW_AT_BUS | bits;
349         WBYTE(p->RIOHosts[p->RIONumHosts].Control, 
350                         BOOT_FROM_RAM | EXTERNAL_BUS_OFF | 
351                         p->RIOHosts[p->RIONumHosts].Mode | 
352                         INTERRUPT_DISABLE );
353         WBYTE(p->RIOHosts[p->RIONumHosts].ResetInt,0xff);
354         WBYTE(p->RIOHosts[p->RIONumHosts].Control,
355                         BOOT_FROM_RAM | EXTERNAL_BUS_OFF | 
356                         p->RIOHosts[p->RIONumHosts].Mode |
357                         INTERRUPT_DISABLE );
358         WBYTE(p->RIOHosts[p->RIONumHosts].ResetInt,0xff);
359         p->RIOHosts[p->RIONumHosts].UniqueNum =
360                 ((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[0])&0xFF)<<0)|
361                 ((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[1])&0xFF)<<8)|
362                 ((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[2])&0xFF)<<16)|
363                 ((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[3])&0xFF)<<24);
364         rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Uniquenum 0x%x\n",p->RIOHosts[p->RIONumHosts].UniqueNum);
365
366         p->RIONumHosts++;
367         rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Tests Passed at 0x%x\n", Base);
368         return(1);
369 }
370 #if 0
371 #ifdef FUTURE_RELEASE
372 int RIOMCAinit(int Mode)
373 {
374         uchar SlotNumber;
375         caddr_t Caddr;
376         uint    Paddr;
377         uint    Ivec;
378         int      Handle;
379         int      ret = 0;
380
381         /*
382         ** Valid mode information for MCA cards
383         ** is only FAST LINKS
384         */
385         Mode = (Mode & FAST_LINKS) ? McaTpFastLinks : McaTpSlowLinks;
386         rio_dprintk (RIO_DEBUG_INIT, "RIOMCAinit(%d)\n",Mode);
387
388
389         /*
390         ** Check out each of the slots
391         */
392         for (SlotNumber = 0; SlotNumber < McaMaxSlots; SlotNumber++) {
393         /*
394         ** Enable the slot we want to talk to
395         */
396         outb( McaSlotSelect, SlotNumber | McaSlotEnable );
397
398         /*
399         ** Read the ID word from the slot
400         */
401         if (((inb(McaIdHigh)<< 8)|inb(McaIdLow)) == McaRIOId)
402         {
403                 rio_dprintk (RIO_DEBUG_INIT, "Potential MCA card in slot %d\n", SlotNumber);
404
405                 /*
406                 ** Card appears to be a RIO MCA card!
407                 */
408                 RIOMachineType |= (1<<RIO_MCA);
409
410                 /*
411                 ** Just check we haven't found too many wonderful objects
412                 */
413                 if ( RIONumHosts >= RIO_HOSTS )
414                 {
415                 Rprintf(RIOMesgTooManyCards);
416                 return(ret);
417                 }
418
419                 /*
420                 ** McaIrqEnable contains the interrupt vector, and a card
421                 ** enable bit.
422                 */
423                 Ivec = inb(McaIrqEnable);
424
425                 rio_dprintk (RIO_DEBUG_INIT, "Ivec is %x\n", Ivec);
426
427                 switch ( Ivec & McaIrqMask )
428                 {
429                 case McaIrq9:
430                 rio_dprintk (RIO_DEBUG_INIT, "IRQ9\n");
431                 break;
432                 case McaIrq3:
433                 rio_dprintk (RIO_DEBUG_INIT, "IRQ3\n");
434                 break;
435                 case McaIrq4:
436                 rio_dprintk (RIO_DEBUG_INIT, "IRQ4\n");
437                 break;
438                 case McaIrq7:
439                 rio_dprintk (RIO_DEBUG_INIT, "IRQ7\n");
440                 break;
441                 case McaIrq10:
442                 rio_dprintk (RIO_DEBUG_INIT, "IRQ10\n");
443                 break;
444                 case McaIrq11:
445                 rio_dprintk (RIO_DEBUG_INIT, "IRQ11\n");
446                 break;
447                 case McaIrq12:
448                 rio_dprintk (RIO_DEBUG_INIT, "IRQ12\n");
449                 break;
450                 case McaIrq15:
451                 rio_dprintk (RIO_DEBUG_INIT, "IRQ15\n");
452                 break;
453                 }
454
455                 /*
456                 ** If the card enable bit isn't set, then set it!
457                 */
458                 if ((Ivec & McaCardEnable) != McaCardEnable) {
459                         rio_dprintk (RIO_DEBUG_INIT, "McaCardEnable not set - setting!\n");
460                         outb(McaIrqEnable,Ivec|McaCardEnable);
461                 } else
462                         rio_dprintk (RIO_DEBUG_INIT, "McaCardEnable already set\n");
463
464                 /*
465                 ** Convert the IRQ enable mask into something useful
466                 */
467                 Ivec = RIOMcaToIvec[Ivec & McaIrqMask];
468
469                 /*
470                 ** Find the physical address
471                 */
472                 rio_dprintk (RIO_DEBUG_INIT, "inb(McaMemory) is %x\n", inb(McaMemory));
473                 Paddr = McaAddress(inb(McaMemory));
474
475                 rio_dprintk (RIO_DEBUG_INIT, "MCA card has Ivec %d Addr %x\n", Ivec, Paddr);
476
477                 if ( Paddr != 0 )
478                 {
479
480                 /*
481                 ** Tell the memory mapper that we want to talk to it
482                 */
483                 Handle = RIOMapin( Paddr, RIO_MCA_MEM_SIZE, &Caddr );
484
485                 if ( Handle == -1 ) {
486                         rio_dprintk (RIO_DEBUG_INIT, "Couldn't map %d bytes at %x\n", RIO_MCA_MEM_SIZE, Paddr;
487                         continue;
488                 }
489
490                 rio_dprintk (RIO_DEBUG_INIT, "Board mapped to vaddr 0x%x\n", Caddr);
491
492                 /*
493                 ** And check that it is actually there!
494                 */
495                 if ( RIOBoardTest( Paddr,Caddr,RIO_MCA,SlotNumber ) == RIO_SUCCESS )
496                 {
497                         rio_dprintk (RIO_DEBUG_INIT, "Board has passed test\n");
498                         rio_dprintk (RIO_DEBUG_INIT, "Slot %d. Type %d. Paddr 0x%x. Caddr 0x%x. Mode 0x%x.\n",
499                                                     SlotNumber, RIO_MCA, Paddr, Caddr, Mode);
500
501                         /*
502                         ** Board has passed its scrub test. Fill in all the
503                         ** transient stuff.
504                         */
505                         p->RIOHosts[RIONumHosts].Slot    = SlotNumber;
506                         p->RIOHosts[RIONumHosts].Ivec    = Ivec;
507                         p->RIOHosts[RIONumHosts].Type    = RIO_MCA;
508                         p->RIOHosts[RIONumHosts].Copy    = bcopy;
509                         p->RIOHosts[RIONumHosts].PaddrP   = Paddr;
510                         p->RIOHosts[RIONumHosts].Caddr  = Caddr;
511                         p->RIOHosts[RIONumHosts].CardP  = (struct DpRam *)Caddr;
512                         p->RIOHosts[RIONumHosts].Mode    = Mode;
513                         WBYTE(p->RIOHosts[p->RIONumHosts].ResetInt , 0xff);
514                         p->RIOHosts[RIONumHosts].UniqueNum =
515                         ((RBYTE(p->RIOHosts[RIONumHosts].Unique[0])&0xFF)<<0)|
516                                                 ((RBYTE(p->RIOHosts[RIONumHosts].Unique[1])&0xFF)<<8)|
517                         ((RBYTE(p->RIOHosts[RIONumHosts].Unique[2])&0xFF)<<16)|
518                         ((RBYTE(p->RIOHosts[RIONumHosts].Unique[3])&0xFF)<<24);
519                         RIONumHosts++;
520                         ret++;
521                 }
522                 else
523                 {
524                         /*
525                         ** It failed the test, so ignore it.
526                         */
527                         rio_dprintk (RIO_DEBUG_INIT, "TEST FAILED\n");
528                         RIOMapout(Paddr, RIO_MCA_MEM_SIZE, Caddr );
529                 }
530                 }
531                 else
532                 {
533                 rio_dprintk (RIO_DEBUG_INIT, "Slot %d - Paddr zero!\n", SlotNumber);
534                 }
535         }
536         else
537         {
538                 rio_dprintk (RIO_DEBUG_INIT, "Slot %d NOT RIO\n", SlotNumber);
539         }
540         }
541         /*
542         ** Now we have checked all the slots, turn off the MCA slot selector
543         */
544         outb(McaSlotSelect,0);
545         rio_dprintk (RIO_DEBUG_INIT, "Slot %d NOT RIO\n", SlotNumber);
546         return ret;
547 }
548
549 int RIOEISAinit( int Mode )
550 {
551         static int EISADone = 0;
552         uint Paddr;
553         int PollIntMixMsgDone = 0;
554         caddr_t Caddr;
555         ushort Ident;
556         uchar EisaSlot;
557         uchar Ivec;
558         int ret = 0;
559
560         /*
561         ** The only valid mode information for EISA hosts is fast or slow
562         ** links.
563         */
564         Mode = (Mode & FAST_LINKS) ? EISA_TP_FAST_LINKS : EISA_TP_SLOW_LINKS;
565
566         if ( EISADone )
567         {
568                 rio_dprintk (RIO_DEBUG_INIT, "RIOEISAinit() - already done, return.\n");
569                 return(0);
570         }
571
572         EISADone++;
573
574         rio_dprintk (RIO_DEBUG_INIT, "RIOEISAinit()\n");
575
576
577         /*
578         ** First check all cards to see if ANY are set for polled mode operation.
579         ** If so, set ALL to polled.
580         */
581
582         for ( EisaSlot=1; EisaSlot<=RIO_MAX_EISA_SLOTS; EisaSlot++ )
583         {
584         Ident = (INBZ(EisaSlot,EISA_PRODUCT_IDENT_HI)<<8) |
585                  INBZ(EisaSlot,EISA_PRODUCT_IDENT_LO);
586
587         if ( Ident == RIO_EISA_IDENT )
588         {
589                 rio_dprintk (RIO_DEBUG_INIT, "Found Specialix product\n");
590
591                 if ( INBZ(EisaSlot,EISA_PRODUCT_NUMBER) != RIO_EISA_PRODUCT_CODE )
592                 {
593                 rio_dprintk (RIO_DEBUG_INIT, "Not Specialix RIO - Product number %x\n",
594                                                 INBZ(EisaSlot, EISA_PRODUCT_NUMBER));
595                 continue;  /* next slot */
596                 }
597                 /*
598                 ** Its a Specialix RIO!
599                 */
600                 rio_dprintk (RIO_DEBUG_INIT, "RIO Revision %d\n",
601                                         INBZ(EisaSlot, EISA_REVISION_NUMBER));
602                 
603                 RIOMachineType |= (1<<RIO_EISA);
604
605                 /*
606                 ** Just check we haven't found too many wonderful objects
607                 */
608                 if ( RIONumHosts >= RIO_HOSTS )
609                 {
610                 Rprintf(RIOMesgTooManyCards);
611                 return 0;
612                 }
613
614                 /*
615                 ** Ensure that the enable bit is set!
616                 */
617                 OUTBZ( EisaSlot, EISA_ENABLE, RIO_EISA_ENABLE_BIT );
618
619                 /*
620                 ** EISA_INTERRUPT_VEC contains the interrupt vector.
621                 */
622                 Ivec = INBZ(EisaSlot,EISA_INTERRUPT_VEC);
623
624 #ifdef RIODEBUG
625                 switch ( Ivec & EISA_INTERRUPT_MASK )
626                 {
627                 case EISA_IRQ_3:
628                         rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 3\n");
629                 break;
630                 case EISA_IRQ_4:
631                         rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 4\n");
632                 break;
633                 case EISA_IRQ_5:
634                         rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 5\n");
635                 break;
636                 case EISA_IRQ_6:
637                         rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 6\n");
638                 break;
639                 case EISA_IRQ_7:
640                         rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 7\n");
641                 break;
642                 case EISA_IRQ_9:
643                         rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 9\n");
644                 break;
645                 case EISA_IRQ_10:
646                         rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 10\n");
647                 break;
648                 case EISA_IRQ_11:
649                         rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 11\n");
650                 break;
651                 case EISA_IRQ_12:
652                         rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 12\n");
653                 break;
654                 case EISA_IRQ_14:
655                         rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 14\n");
656                 break;
657                 case EISA_IRQ_15:
658                         rio_dprintk (RIO_DEBUG_INIT, "EISA IRQ 15\n");
659                 break;
660                 case EISA_POLLED:
661                         rio_dprintk (RIO_DEBUG_INIT, "EISA POLLED\n");
662                 break;
663                 default:
664                         rio_dprintk (RIO_DEBUG_INIT, NULL,DBG_INIT|DBG_FAIL,"Shagged interrupt number!\n");
665                 Ivec &= EISA_CONTROL_MASK;
666                 }
667 #endif
668
669                 if ( (Ivec & EISA_INTERRUPT_MASK) ==
670                  EISA_POLLED )
671                 {
672                 RIOWillPoll = 1;
673                 break;          /* From EisaSlot loop */
674                 }
675         }
676         }
677
678         /*
679         ** Do it all again now we know whether to change all cards to polled
680         ** mode or not
681         */
682
683         for ( EisaSlot=1; EisaSlot<=RIO_MAX_EISA_SLOTS; EisaSlot++ )
684         {
685         Ident = (INBZ(EisaSlot,EISA_PRODUCT_IDENT_HI)<<8) |
686                  INBZ(EisaSlot,EISA_PRODUCT_IDENT_LO);
687
688         if ( Ident == RIO_EISA_IDENT )
689         {
690                 if ( INBZ(EisaSlot,EISA_PRODUCT_NUMBER) != RIO_EISA_PRODUCT_CODE )
691                 continue;  /* next slot */
692
693                 /*
694                 ** Its a Specialix RIO!
695                 */
696                 
697                 /*
698                 ** Ensure that the enable bit is set!
699                 */
700                 OUTBZ( EisaSlot, EISA_ENABLE, RIO_EISA_ENABLE_BIT );
701
702                 /*
703                 ** EISA_INTERRUPT_VEC contains the interrupt vector.
704                 */
705                 Ivec = INBZ(EisaSlot,EISA_INTERRUPT_VEC);
706
707                 if ( RIOWillPoll )
708                 {
709                         /*
710                         ** If we are going to operate in polled mode, but this
711                         ** board is configured to be interrupt driven, display
712                         ** the message explaining the situation to the punter,
713                         ** assuming we haven't already done so.
714                         */
715
716                         if ( !PollIntMixMsgDone &&
717                          (Ivec & EISA_INTERRUPT_MASK) != EISA_POLLED )
718                         {
719                         Rprintf(RIOMesgAllPolled);
720                         PollIntMixMsgDone = 1;
721                         }
722
723                         /*
724                         ** Ungraciously ignore whatever the board reports as its
725                         ** interrupt vector...
726                         */
727
728                         Ivec &= ~EISA_INTERRUPT_MASK;
729
730                         /*
731                         ** ...and force it to dance to the poll tune.
732                         */
733
734                         Ivec |= EISA_POLLED;
735                 }
736
737                 /*
738                 ** Convert the IRQ enable mask into something useful (0-15)
739                 */
740                 Ivec = RIOEisaToIvec(Ivec);
741
742                 rio_dprintk (RIO_DEBUG_INIT, "EISA host in slot %d has Ivec 0x%x\n",
743                  EisaSlot, Ivec);
744
745                 /*
746                 ** Find the physical address
747                 */
748                 Paddr = (INBZ(EisaSlot,EISA_MEMORY_BASE_HI)<<24) |
749                                 (INBZ(EisaSlot,EISA_MEMORY_BASE_LO)<<16);
750
751                 rio_dprintk (RIO_DEBUG_INIT, "EISA card has Ivec %d Addr %x\n", Ivec, Paddr);
752
753                 if ( Paddr == 0 )
754                 {
755                 rio_dprintk (RIO_DEBUG_INIT,
756                  "Board in slot %d configured for address zero!\n", EisaSlot);
757                 continue;
758                 }
759
760                 /*
761                 ** Tell the memory mapper that we want to talk to it
762                 */
763                 rio_dprintk (RIO_DEBUG_INIT, "About to map EISA card \n");
764
765                 if (RIOMapin( Paddr, RIO_EISA_MEM_SIZE, &Caddr) == -1) {
766                 rio_dprintk (RIO_DEBUG_INIT, "Couldn't map %d bytes at %x\n",
767                                                         RIO_EISA_MEM_SIZE,Paddr);
768                 continue;
769                 }
770
771                 rio_dprintk (RIO_DEBUG_INIT, "Board mapped to vaddr 0x%x\n", Caddr);
772
773                 /*
774                 ** And check that it is actually there!
775                 */
776                 if ( RIOBoardTest( Paddr,Caddr,RIO_EISA,EisaSlot) == RIO_SUCCESS )
777                         {
778                 rio_dprintk (RIO_DEBUG_INIT, "Board has passed test\n");
779                 rio_dprintk (RIO_DEBUG_INIT, 
780                 "Slot %d. Ivec %d. Type %d. Paddr 0x%x. Caddr 0x%x. Mode 0x%x.\n",
781                         EisaSlot,Ivec,RIO_EISA,Paddr,Caddr,Mode);
782
783                 /*
784                 ** Board has passed its scrub test. Fill in all the
785                 ** transient stuff.
786                 */
787                 p->RIOHosts[RIONumHosts].Slot    = EisaSlot;
788                 p->RIOHosts[RIONumHosts].Ivec    = Ivec;
789                 p->RIOHosts[RIONumHosts].Type    = RIO_EISA;
790                 p->RIOHosts[RIONumHosts].Copy    = bcopy;
791                                 p->RIOHosts[RIONumHosts].PaddrP   = Paddr;
792                                 p->RIOHosts[RIONumHosts].Caddr  = Caddr;
793                 p->RIOHosts[RIONumHosts].CardP  = (struct DpRam *)Caddr;
794                                 p->RIOHosts[RIONumHosts].Mode    = Mode;
795                 /*
796                 ** because the EISA prom is mapped into IO space, we
797                 ** need to copy the unqiue number into the memory area
798                 ** that it would have occupied, so that the download
799                 ** code can determine its ID and card type.
800                 */
801          WBYTE(p->RIOHosts[RIONumHosts].Unique[0],INBZ(EisaSlot,EISA_UNIQUE_NUM_0));
802          WBYTE(p->RIOHosts[RIONumHosts].Unique[1],INBZ(EisaSlot,EISA_UNIQUE_NUM_1));
803          WBYTE(p->RIOHosts[RIONumHosts].Unique[2],INBZ(EisaSlot,EISA_UNIQUE_NUM_2));
804          WBYTE(p->RIOHosts[RIONumHosts].Unique[3],INBZ(EisaSlot,EISA_UNIQUE_NUM_3));
805                 p->RIOHosts[RIONumHosts].UniqueNum =
806                         ((RBYTE(p->RIOHosts[RIONumHosts].Unique[0])&0xFF)<<0)|
807                                                 ((RBYTE(p->RIOHosts[RIONumHosts].Unique[1])&0xFF)<<8)|
808                         ((RBYTE(p->RIOHosts[RIONumHosts].Unique[2])&0xFF)<<16)|
809                         ((RBYTE(p->RIOHosts[RIONumHosts].Unique[3])&0xFF)<<24);
810                 INBZ(EisaSlot,EISA_INTERRUPT_RESET);
811                                 RIONumHosts++;
812                 ret++;
813                         }
814                 else
815                 {
816                 /*
817                 ** It failed the test, so ignore it.
818                 */
819                 rio_dprintk (RIO_DEBUG_INIT, "TEST FAILED\n");
820
821                 RIOMapout(Paddr, RIO_EISA_MEM_SIZE, Caddr );
822                 }
823         }
824         }
825         if (RIOMachineType & RIO_EISA)
826         return ret+1;
827         return ret;
828 }
829 #endif
830
831
832 #ifndef linux
833
834 #define CONFIG_ADDRESS  0xcf8
835 #define CONFIG_DATA             0xcfc
836 #define FORWARD_REG             0xcfa
837
838
839 static int
840 read_config(int bus_number, int device_num, int r_number) 
841 {
842         unsigned int cav;
843         unsigned int val;
844
845 /*
846    Build config_address_value:
847
848       31        24 23        16 15      11 10  8 7        0 
849       ------------------------------------------------------
850       |1| 0000000 | bus_number | device # | 000 | register |
851       ------------------------------------------------------
852 */
853
854         cav = r_number & 0xff;
855         cav |= ((device_num & 0x1f) << 11);
856         cav |= ((bus_number & 0xff) << 16);
857         cav |= 0x80000000; /* Enable bit */
858         outpd(CONFIG_ADDRESS,cav);
859         val = inpd(CONFIG_DATA);
860         outpd(CONFIG_ADDRESS,0);
861         return val;
862 }
863
864 static
865 write_config(bus_number,device_num,r_number,val) 
866 {
867         unsigned int cav;
868
869 /*
870    Build config_address_value:
871
872       31        24 23        16 15      11 10  8 7        0 
873       ------------------------------------------------------
874       |1| 0000000 | bus_number | device # | 000 | register |
875       ------------------------------------------------------
876 */
877
878         cav = r_number & 0xff;
879         cav |= ((device_num & 0x1f) << 11);
880         cav |= ((bus_number & 0xff) << 16);
881         cav |= 0x80000000; /* Enable bit */
882         outpd(CONFIG_ADDRESS, cav);
883         outpd(CONFIG_DATA, val);
884         outpd(CONFIG_ADDRESS, 0);
885         return val;
886 }
887 #else
888 /* XXX Implement these... */
889 static int
890 read_config(int bus_number, int device_num, int r_number) 
891 {
892   return 0;
893 }
894
895 static int
896 write_config(int bus_number, int device_num, int r_number) 
897 {
898   return 0;
899 }
900
901 #endif
902
903 int
904 RIOPCIinit(p, Mode)
905 struct rio_info *p;
906 int             Mode;
907 {
908         #define MAX_PCI_SLOT            32
909         #define RIO_PCI_JET_CARD        0x200011CB
910
911         static int      slot;   /* count of machine's PCI slots searched so far */
912         caddr_t         Caddr;  /* Virtual address of the current PCI host card. */
913         unsigned char   Ivec;   /* interrupt vector for the current PCI host */
914         unsigned long   Paddr;  /* Physical address for the current PCI host */
915         int             Handle; /* Handle to Virtual memory allocated for current PCI host */
916
917
918         rio_dprintk (RIO_DEBUG_INIT,  "Search for a RIO PCI card - start at slot %d\n", slot);
919
920         /*
921         ** Initialise the search status
922         */
923         p->RIOLastPCISearch     = RIO_FAIL;
924
925         while ( (slot < MAX_PCI_SLOT) & (p->RIOLastPCISearch != RIO_SUCCESS) )
926         {
927                 rio_dprintk (RIO_DEBUG_INIT,  "Currently testing slot %d\n", slot);
928
929                 if (read_config(0,slot,0) == RIO_PCI_JET_CARD) {
930                         p->RIOHosts[p->RIONumHosts].Ivec = 0;
931                         Paddr = read_config(0,slot,0x18);
932                         Paddr = Paddr - (Paddr & 0x1); /* Mask off the io bit */
933
934                         if ( (Paddr == 0) || ((Paddr & 0xffff0000) == 0xffff0000) ) {
935                                 rio_dprintk (RIO_DEBUG_INIT,  "Goofed up slot\n");      /* what! */
936                                 slot++;
937                                 continue;
938                         }
939
940                         p->RIOHosts[p->RIONumHosts].PaddrP = Paddr;
941                         Ivec = (read_config(0,slot,0x3c) & 0xff);
942
943                         rio_dprintk (RIO_DEBUG_INIT,  "PCI Host at 0x%x, Intr %d\n", (int)Paddr, Ivec);
944
945                         Handle = RIOMapin( Paddr, RIO_PCI_MEM_SIZE, &Caddr );
946                         if (Handle == -1) {
947                                 rio_dprintk (RIO_DEBUG_INIT,  "Couldn't map %d bytes at 0x%x\n", RIO_PCI_MEM_SIZE, (int)Paddr);
948                                 slot++;
949                                 continue;
950                         }
951                         p->RIOHosts[p->RIONumHosts].Ivec = Ivec + 32;
952                         p->intr_tid = iointset(p->RIOHosts[p->RIONumHosts].Ivec,
953                                                 (int (*)())rio_intr, (char *)p->RIONumHosts);
954                         if (RIOBoardTest( Paddr, Caddr, RIO_PCI, 0 ) == RIO_SUCCESS) {
955                                 rio_dprintk (RIO_DEBUG_INIT, ("Board has passed test\n");
956                                 rio_dprintk (RIO_DEBUG_INIT, ("Paddr 0x%x. Caddr 0x%x. Mode 0x%x.\n", Paddr, Caddr, Mode);
957
958                                 /*
959                                 ** Board has passed its scrub test. Fill in all the
960                                 ** transient stuff.
961                                 */
962                                 p->RIOHosts[p->RIONumHosts].Slot           = 0;
963                                 p->RIOHosts[p->RIONumHosts].Ivec           = Ivec + 32;
964                                 p->RIOHosts[p->RIONumHosts].Type           = RIO_PCI;
965                                 p->RIOHosts[p->RIONumHosts].Copy           = rio_pcicopy; 
966                                 p->RIOHosts[p->RIONumHosts].PaddrP         = Paddr;
967                                 p->RIOHosts[p->RIONumHosts].Caddr          = Caddr;
968                                 p->RIOHosts[p->RIONumHosts].CardP          = (struct DpRam *)Caddr;
969                                 p->RIOHosts[p->RIONumHosts].Mode           = Mode;
970
971 #if 0
972                                 WBYTE(p->RIOHosts[p->RIONumHosts].Control, 
973                                                 BOOT_FROM_RAM | EXTERNAL_BUS_OFF | 
974                                                 p->RIOHosts[p->RIONumHosts].Mode | 
975                                                 INTERRUPT_DISABLE );
976                                 WBYTE(p->RIOHosts[p->RIONumHosts].ResetInt,0xff);
977                                 WBYTE(p->RIOHosts[p->RIONumHosts].Control,
978                                                 BOOT_FROM_RAM | EXTERNAL_BUS_OFF | 
979                                                 p->RIOHosts[p->RIONumHosts].Mode |
980                                                 INTERRUPT_DISABLE );
981                                 WBYTE(p->RIOHosts[p->RIONumHosts].ResetInt,0xff);
982 #else
983                                 WBYTE(p->RIOHosts[p->RIONumHosts].ResetInt, 0xff);
984 #endif
985                                 p->RIOHosts[p->RIONumHosts].UniqueNum  =
986                                         ((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[0])&0xFF)<<0)|
987                                         ((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[1])&0xFF)<<8)|
988                                         ((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[2])&0xFF)<<16)|
989                                         ((RBYTE(p->RIOHosts[p->RIONumHosts].Unique[3])&0xFF)<<24);
990
991                                 rio_dprintk (RIO_DEBUG_INIT, "Unique no 0x%x.\n", 
992                                     p->RIOHosts[p->RIONumHosts].UniqueNum);
993
994                                 p->RIOLastPCISearch = RIO_SUCCESS;
995                                 p->RIONumHosts++;
996                         }
997                 }
998                 slot++;
999         }
1000
1001         if ( slot >= MAX_PCI_SLOT ) {
1002                 rio_dprintk (RIO_DEBUG_INIT,  "All %d PCI slots have tested for RIO cards !!!\n",
1003                              MAX_PCI_SLOT);
1004         }
1005
1006
1007         /*
1008         ** I don't think we want to do this anymore
1009         **
1010
1011         if (!p->RIOLastPCISearch == RIO_FAIL ) {
1012                 p->RIOFailed++;
1013         }
1014
1015         **
1016         */
1017 }
1018
1019 #ifdef FUTURE_RELEASE
1020 void riohalt( void )
1021 {
1022         int host;
1023         for ( host=0; host<p->RIONumHosts; host++ )
1024         {
1025                 rio_dprintk (RIO_DEBUG_INIT, "Stop host %d\n", host);
1026                 (void)RIOBoardTest( p->RIOHosts[host].PaddrP, p->RIOHosts[host].Caddr, p->RIOHosts[host].Type,p->RIOHosts[host].Slot );
1027         }
1028 }
1029 #endif
1030 #endif
1031
1032 static  uchar   val[] = {
1033 #ifdef VERY_LONG_TEST
1034           0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
1035           0xa5, 0xff, 0x5a, 0x00, 0xff, 0xc9, 0x36, 
1036 #endif
1037           0xff, 0x00, 0x00 };
1038
1039 #define TEST_END sizeof(val)
1040
1041 /*
1042 ** RAM test a board. 
1043 ** Nothing too complicated, just enough to check it out.
1044 */
1045 int
1046 RIOBoardTest(paddr, caddr, type, slot)
1047 paddr_t paddr;
1048 caddr_t caddr;
1049 uchar   type;
1050 int             slot;
1051 {
1052         struct DpRam *DpRam = (struct DpRam *)caddr;
1053         char *ram[4];
1054         int  size[4];
1055         int  op, bank;
1056         int  nbanks;
1057
1058         rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Reset host type=%d, DpRam=0x%x, slot=%d\n",
1059                         type,(int)DpRam, slot);
1060
1061         RIOHostReset(type, DpRam, slot);
1062
1063         /*
1064         ** Scrub the memory. This comes in several banks:
1065         ** DPsram1      - 7000h bytes
1066         ** DPsram2      - 200h  bytes
1067         ** DPsram3      - 7000h bytes
1068         ** scratch      - 1000h bytes
1069         */
1070
1071         rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Setup ram/size arrays\n");
1072
1073         size[0] = DP_SRAM1_SIZE;
1074         size[1] = DP_SRAM2_SIZE;
1075         size[2] = DP_SRAM3_SIZE;
1076         size[3] = DP_SCRATCH_SIZE;
1077
1078         ram[0] = (char *)&DpRam->DpSram1[0];
1079         ram[1] = (char *)&DpRam->DpSram2[0];
1080         ram[2] = (char *)&DpRam->DpSram3[0];
1081         nbanks = (type == RIO_PCI) ? 3 : 4;
1082         if (nbanks == 4)
1083                 ram[3] = (char *)&DpRam->DpScratch[0];
1084
1085
1086         if (nbanks == 3) {
1087                 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Memory: 0x%x(0x%x), 0x%x(0x%x), 0x%x(0x%x)\n",
1088                                 (int)ram[0], size[0], (int)ram[1], size[1], (int)ram[2], size[2]);
1089         } else {
1090                 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: 0x%x(0x%x), 0x%x(0x%x), 0x%x(0x%x), 0x%x(0x%x)\n",
1091                         (int)ram[0], size[0], (int)ram[1], size[1], (int)ram[2], size[2], (int)ram[3], 
1092                                         size[3]);
1093         }
1094
1095         /*
1096         ** This scrub operation will test for crosstalk between
1097         ** banks. TEST_END is a magic number, and relates to the offset
1098         ** within the 'val' array used by Scrub.
1099         */
1100         for (op=0; op<TEST_END; op++) {
1101                 for (bank=0; bank<nbanks; bank++) {
1102                         if (RIOScrub(op, (BYTE *)ram[bank], size[bank]) == RIO_FAIL) {
1103                                 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: RIOScrub band %d, op %d failed\n", 
1104                                                         bank, op);
1105                                 return RIO_FAIL;
1106                         }
1107                 }
1108         }
1109
1110         rio_dprintk (RIO_DEBUG_INIT, "Test completed\n");
1111         return RIO_SUCCESS;
1112 }
1113
1114
1115 /*
1116 ** Scrub an area of RAM.
1117 ** Define PRETEST and POSTTEST for a more thorough checking of the
1118 ** state of the memory.
1119 ** Call with op set to an index into the above 'val' array to determine
1120 ** which value will be written into memory.
1121 ** Call with op set to zero means that the RAM will not be read and checked
1122 ** before it is written.
1123 ** Call with op not zero, and the RAM will be read and compated with val[op-1]
1124 ** to check that the data from the previous phase was retained.
1125 */
1126 static int
1127 RIOScrub(op, ram, size)
1128 int             op;
1129 BYTE *  ram;
1130 int             size; 
1131 {
1132         int                             off;
1133         unsigned char   oldbyte;
1134         unsigned char   newbyte;
1135         unsigned char   invbyte;
1136         unsigned short  oldword;
1137         unsigned short  newword;
1138         unsigned short  invword;
1139         unsigned short  swapword;
1140
1141         if (op) {
1142                 oldbyte = val[op-1];
1143                 oldword = oldbyte | (oldbyte<<8);
1144         } else
1145           oldbyte = oldword = 0; /* Tell the compiler we've initilalized them. */
1146         newbyte = val[op];
1147         newword = newbyte | (newbyte<<8);
1148         invbyte = ~newbyte;
1149         invword = invbyte | (invbyte<<8);
1150
1151         /*
1152         ** Check that the RAM contains the value that should have been left there
1153         ** by the previous test (not applicable for pass zero)
1154         */
1155         if (op) {
1156                 for (off=0; off<size; off++) {
1157                         if (RBYTE(ram[off]) != oldbyte) {
1158                                 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]));
1159                                 return RIO_FAIL;
1160                         }
1161                 }
1162                 for (off=0; off<size; off+=2) {
1163                         if (*(ushort *)&ram[off] != oldword) {
1164                                 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]);
1165                                 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]));
1166                                 return RIO_FAIL;
1167                         }
1168                 }
1169         }
1170
1171         /*
1172         ** Now write the INVERSE of the test data into every location, using
1173         ** BYTE write operations, first checking before each byte is written
1174         ** that the location contains the old value still, and checking after
1175         ** the write that the location contains the data specified - this is
1176         ** the BYTE read/write test.
1177         */
1178         for (off=0; off<size; off++) {
1179                 if (op && (RBYTE(ram[off]) != oldbyte)) {
1180                         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]));
1181                         return RIO_FAIL;
1182                 }
1183                 WBYTE(ram[off],invbyte);
1184                 if (RBYTE(ram[off]) != invbyte) {
1185                         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]));
1186                         return RIO_FAIL;
1187                 }
1188         }
1189
1190         /*
1191         ** now, use WORD operations to write the test value into every location,
1192         ** check as before that the location contains the previous test value
1193         ** before overwriting, and that it contains the data value written
1194         ** afterwards.
1195         ** This is the WORD operation test.
1196         */
1197         for (off=0; off<size; off+=2) {
1198                 if (*(ushort *)&ram[off] != invword) {
1199                         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]);
1200                 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]));
1201                         return RIO_FAIL;
1202                 }
1203
1204                 *(ushort *)&ram[off] = newword;
1205                 if ( *(ushort *)&ram[off] != newword ) {
1206                         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]);
1207                         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]));
1208                         return RIO_FAIL;
1209                 }
1210         }
1211
1212         /*
1213         ** now run through the block of memory again, first in byte mode
1214         ** then in word mode, and check that all the locations contain the
1215         ** required test data.
1216         */
1217         for (off=0; off<size; off++) {
1218                 if (RBYTE(ram[off]) != newbyte) {
1219                         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]));
1220                         return RIO_FAIL;
1221                 }
1222         }
1223
1224         for (off=0; off<size; off+=2) {
1225                 if ( *(ushort *)&ram[off] != newword ) {
1226                         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]);
1227                         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]));
1228                         return RIO_FAIL;
1229                 }
1230         }
1231
1232         /*
1233         ** time to check out byte swapping errors
1234         */
1235         swapword = invbyte | (newbyte << 8);
1236
1237         for (off=0; off<size; off+=2) {
1238                 WBYTE(ram[off],invbyte);
1239                 WBYTE(ram[off+1],newbyte);
1240         }
1241
1242         for ( off=0; off<size; off+=2 ) {
1243                 if (*(ushort *)&ram[off] != swapword) {
1244                         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]));
1245                         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]));
1246                         return RIO_FAIL;
1247                 }
1248                 *((ushort *)&ram[off]) = ~swapword;
1249         }
1250
1251         for (off=0; off<size; off+=2) {
1252                 if (RBYTE(ram[off]) != newbyte) {
1253                         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]));
1254                         return RIO_FAIL;
1255                 }
1256                 if (RBYTE(ram[off+1]) != invbyte) {
1257                         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]));
1258                         return RIO_FAIL;
1259                 }
1260                 *((ushort *)&ram[off]) = newword;
1261         }
1262         return RIO_SUCCESS;
1263 }
1264
1265 /*
1266 ** try to ensure that every host is either in polled mode
1267 ** or is in interrupt mode. Only allow interrupt mode if
1268 ** all hosts can interrupt (why?)
1269 ** and force into polled mode if told to. Patch up the
1270 ** interrupt vector & salute The Queen when you've done.
1271 */
1272 #if 0
1273 static void
1274 RIOAllocateInterrupts(p)
1275 struct rio_info *       p;
1276 {
1277         int Host;
1278
1279         /*
1280         ** Easy case - if we have been told to poll, then we poll.
1281         */
1282         if (p->mode & POLLED_MODE) {
1283                 RIOStopInterrupts(p, 0, 0);
1284                 return;
1285         }
1286
1287         /*
1288         ** check - if any host has been set to polled mode, then all must be.
1289         */
1290         for (Host=0; Host<p->RIONumHosts; Host++) {
1291                 if ( (p->RIOHosts[Host].Type != RIO_AT) &&
1292                                 (p->RIOHosts[Host].Ivec == POLLED) ) {
1293                         RIOStopInterrupts(p, 1, Host );
1294                         return;
1295                 }
1296         }
1297         for (Host=0; Host<p->RIONumHosts; Host++) {
1298                 if (p->RIOHosts[Host].Type == RIO_AT) {
1299                         if ( (p->RIOHosts[Host].Ivec - 32) == 0) {
1300                                 RIOStopInterrupts(p, 2, Host );
1301                                 return;
1302                         }
1303                 }
1304         }
1305 }
1306
1307 /*
1308 ** something has decided that we can't be doing with these
1309 ** new-fangled interrupt thingies. Set everything up to just
1310 ** poll.
1311 */
1312 static void
1313 RIOStopInterrupts(p, Reason, Host)
1314 struct rio_info *       p;
1315 int     Reason;
1316 int     Host; 
1317 {
1318 #ifdef FUTURE_RELEASE
1319         switch (Reason) {
1320                 case 0: /* forced into polling by rio_polled */
1321                         break;
1322                 case 1: /* SCU has set 'Host' into polled mode */
1323                         break;
1324                 case 2: /* there aren't enough interrupt vectors for 'Host' */
1325                         break;
1326         }
1327 #endif
1328
1329         for (Host=0; Host<p->RIONumHosts; Host++ ) {
1330                 struct Host *HostP = &p->RIOHosts[Host];
1331
1332                 switch (HostP->Type) {
1333                         case RIO_AT:
1334                                 /*
1335                                 ** The AT host has it's interrupts disabled by clearing the
1336                                 ** int_enable bit.
1337                                 */
1338                                 HostP->Mode &= ~INTERRUPT_ENABLE;
1339                                 HostP->Ivec = POLLED;
1340                                 break;
1341 #ifdef FUTURE_RELEASE
1342                         case RIO_EISA:
1343                                 /*
1344                                 ** The EISA host has it's interrupts disabled by setting the
1345                                 ** Ivec to zero
1346                                 */
1347                                 HostP->Ivec = POLLED;
1348                                 break;
1349 #endif
1350                         case RIO_PCI:
1351                                 /*
1352                                 ** The PCI host has it's interrupts disabled by clearing the
1353                                 ** int_enable bit, like a regular host card.
1354                                 */
1355                                 HostP->Mode &= ~RIO_PCI_INT_ENABLE;
1356                                 HostP->Ivec = POLLED;
1357                                 break;
1358 #ifdef FUTURE_RELEASE
1359                         case RIO_MCA:
1360                                 /*
1361                                 ** There's always one, isn't there?
1362                                 ** The MCA host card cannot have it's interrupts disabled.
1363                                 */
1364                                 RIOPatchVec(HostP);
1365                                 break;
1366 #endif
1367                 }
1368         }
1369 }
1370
1371 /*
1372 ** This function is called at init time to setup the data structures.
1373 */
1374 void
1375 RIOAllocDataStructs(p)
1376 struct rio_info *       p;
1377 {
1378         int     port,
1379                 host,
1380                 tm;
1381
1382         p->RIOPortp = (struct Port *)sysbrk(RIO_PORTS * sizeof(struct Port));
1383         if (!p->RIOPortp) {
1384                 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: No memory for port structures\n");
1385                 p->RIOFailed++;
1386                 return;
1387         } 
1388         bzero( p->RIOPortp, sizeof(struct Port) * RIO_PORTS );
1389         rio_dprintk (RIO_DEBUG_INIT,  "RIO-init: allocated and cleared memory for port structs\n");
1390         rio_dprintk (RIO_DEBUG_INIT,  "First RIO port struct @0x%x, size=0x%x bytes\n",
1391             (int)p->RIOPortp, sizeof(struct Port));
1392
1393         for( port=0; port<RIO_PORTS; port++ ) {
1394                 p->RIOPortp[port].PortNum = port;
1395                 p->RIOPortp[port].TtyP = &p->channel[port];
1396                 sreset (p->RIOPortp[port].InUse);       /* Let the first guy uses it */
1397                 p->RIOPortp[port].portSem = -1; /* Let the first guy takes it */
1398                 p->RIOPortp[port].ParamSem = -1;        /* Let the first guy takes it */
1399                 p->RIOPortp[port].timeout_id = 0;       /* Let the first guy takes it */
1400         }
1401
1402         p->RIOHosts = (struct Host *)sysbrk(RIO_HOSTS * sizeof(struct Host));
1403         if (!p->RIOHosts) {
1404                 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: No memory for host structures\n");
1405                 p->RIOFailed++;
1406                 return;
1407         }
1408         bzero(p->RIOHosts, sizeof(struct Host)*RIO_HOSTS);
1409         rio_dprintk (RIO_DEBUG_INIT,  "RIO-init: allocated and cleared memory for host structs\n");
1410         rio_dprintk (RIO_DEBUG_INIT,  "First RIO host struct @0x%x, size=0x%x bytes\n",
1411             (int)p->RIOHosts, sizeof(struct Host));
1412
1413         for( host=0; host<RIO_HOSTS; host++ ) {
1414                 spin_lock_init (&p->RIOHosts[host].HostLock);
1415                 p->RIOHosts[host].timeout_id = 0; /* Let the first guy takes it */
1416         }
1417         /*
1418         ** check that the buffer size is valid, round down to the next power of
1419         ** two if necessary; if the result is zero, then, hey, no double buffers.
1420         */
1421         for ( tm = 1; tm && tm <= p->RIOConf.BufferSize; tm <<= 1 )
1422                 ;
1423         tm >>= 1;
1424         p->RIOBufferSize = tm;
1425         p->RIOBufferMask = tm ? tm - 1 : 0;
1426 }
1427
1428 /*
1429 ** this function gets called whenever the data structures need to be
1430 ** re-setup, for example, after a riohalt (why did I ever invent it?)
1431 */
1432 void
1433 RIOSetupDataStructs(p)
1434 struct rio_info * p;
1435 {
1436         int host, entry, rup;
1437
1438         for ( host=0; host<RIO_HOSTS; host++ ) {
1439                 struct Host *HostP = &p->RIOHosts[host];
1440                 for ( entry=0; entry<LINKS_PER_UNIT; entry++ ) {
1441                         HostP->Topology[entry].Unit = ROUTE_DISCONNECT;
1442                         HostP->Topology[entry].Link = NO_LINK;
1443                 }
1444                 bcopy("HOST X", HostP->Name, 7);
1445                 HostP->Name[5] = '1'+host;
1446                 for (rup=0; rup<(MAX_RUP + LINKS_PER_UNIT); rup++) {
1447                         if (rup < MAX_RUP) {
1448                                 for (entry=0; entry<LINKS_PER_UNIT; entry++ ) {
1449                                         HostP->Mapping[rup].Topology[entry].Unit = ROUTE_DISCONNECT;
1450                                         HostP->Mapping[rup].Topology[entry].Link = NO_LINK;
1451                                 }
1452                                 RIODefaultName(p, HostP, rup);
1453                         }
1454                         spin_lock_init(&HostP->UnixRups[rup].RupLock);
1455                 }
1456         }
1457 }
1458 #endif
1459
1460 int
1461 RIODefaultName(p, HostP, UnitId)
1462 struct rio_info *       p;
1463 struct Host *   HostP;
1464 uint                    UnitId;
1465 {
1466 #ifdef CHECK
1467         CheckHost( Host );
1468         CheckUnitId( UnitId );
1469 #endif
1470         bcopy("UNKNOWN RTA X-XX",HostP->Mapping[UnitId].Name,17);
1471         HostP->Mapping[UnitId].Name[12]='1'+(HostP-p->RIOHosts);
1472         if ((UnitId+1) > 9) {
1473                 HostP->Mapping[UnitId].Name[14]='0'+((UnitId+1)/10);
1474                 HostP->Mapping[UnitId].Name[15]='0'+((UnitId+1)%10);
1475         }
1476         else {
1477                 HostP->Mapping[UnitId].Name[14]='1'+UnitId;
1478                 HostP->Mapping[UnitId].Name[15]=0;
1479         }
1480         return 0;
1481 }
1482
1483 #define RIO_RELEASE     "Linux"
1484 #define RELEASE_ID      "1.0"
1485
1486 #if 0
1487 static int
1488 RIOReport(p)
1489 struct rio_info *       p;
1490 {
1491         char *  RIORelease = RIO_RELEASE;
1492         char *  RIORelID = RELEASE_ID;
1493         int             host;
1494
1495         rio_dprintk (RIO_DEBUG_INIT, "RIO : Release: %s ID: %s\n", RIORelease, RIORelID);
1496
1497         if ( p->RIONumHosts==0 ) {
1498                 rio_dprintk (RIO_DEBUG_INIT, "\nNo Hosts configured\n");
1499                 return(0);
1500         }
1501
1502         for ( host=0; host < p->RIONumHosts; host++ ) {
1503                 struct Host *HostP = &p->RIOHosts[host];
1504                 switch ( HostP->Type ) {
1505                         case RIO_AT:
1506                                 rio_dprintk (RIO_DEBUG_INIT, "AT BUS : found the card at 0x%x\n", HostP->PaddrP);
1507                 }
1508         }
1509         return 0;
1510 }
1511 #endif
1512
1513 static struct rioVersion        stVersion;
1514
1515 struct rioVersion *
1516 RIOVersid(void)
1517 {
1518     strlcpy(stVersion.version, "RIO driver for linux V1.0",
1519             sizeof(stVersion.version));
1520     strlcpy(stVersion.buildDate, __DATE__,
1521             sizeof(stVersion.buildDate));
1522
1523     return &stVersion;
1524 }
1525
1526 #if 0
1527 int
1528 RIOMapin(paddr, size, vaddr)
1529 paddr_t         paddr;
1530 int                     size;
1531 caddr_t *       vaddr;
1532 {
1533         *vaddr = (caddr_t)permap( (long)paddr, size);
1534         return ((int)*vaddr);
1535 }
1536
1537 void
1538 RIOMapout(paddr, size, vaddr)
1539 paddr_t         paddr;
1540 long            size;
1541 caddr_t         vaddr;
1542 {
1543 }
1544 #endif
1545
1546
1547 void
1548 RIOHostReset(Type, DpRamP, Slot)
1549 uint Type;
1550 volatile struct DpRam *DpRamP;
1551 uint Slot; 
1552 {
1553         /*
1554         ** Reset the Tpu
1555         */
1556         rio_dprintk (RIO_DEBUG_INIT,  "RIOHostReset: type 0x%x", Type);
1557         switch ( Type ) {
1558                 case RIO_AT:
1559                         rio_dprintk (RIO_DEBUG_INIT, " (RIO_AT)\n");
1560                         WBYTE(DpRamP->DpControl,  BOOT_FROM_RAM | EXTERNAL_BUS_OFF | 
1561                                           INTERRUPT_DISABLE | BYTE_OPERATION |
1562                                           SLOW_LINKS | SLOW_AT_BUS);
1563                         WBYTE(DpRamP->DpResetTpu, 0xFF);
1564                         udelay(3);
1565
1566                         rio_dprintk (RIO_DEBUG_INIT,  "RIOHostReset: Don't know if it worked. Try reset again\n");
1567                         WBYTE(DpRamP->DpControl,  BOOT_FROM_RAM | EXTERNAL_BUS_OFF |
1568                                           INTERRUPT_DISABLE | BYTE_OPERATION |
1569                                           SLOW_LINKS | SLOW_AT_BUS);
1570                         WBYTE(DpRamP->DpResetTpu, 0xFF);
1571                         udelay(3);
1572                         break;
1573 #ifdef FUTURE_RELEASE
1574         case RIO_EISA:
1575         /*
1576         ** Bet this doesn't work!
1577         */
1578         OUTBZ( Slot, EISA_CONTROL_PORT,
1579                 EISA_TP_RUN             | EISA_TP_BUS_DISABLE   |
1580                 EISA_TP_SLOW_LINKS | EISA_TP_BOOT_FROM_RAM );
1581         OUTBZ( Slot, EISA_CONTROL_PORT,
1582                 EISA_TP_RESET     | EISA_TP_BUS_DISABLE   | 
1583                 EISA_TP_SLOW_LINKS | EISA_TP_BOOT_FROM_RAM );
1584         suspend( 3 );
1585         OUTBZ( Slot, EISA_CONTROL_PORT,
1586                 EISA_TP_RUN             | EISA_TP_BUS_DISABLE   | 
1587                 EISA_TP_SLOW_LINKS | EISA_TP_BOOT_FROM_RAM );
1588         break;
1589         case RIO_MCA:
1590         WBYTE(DpRamP->DpControl  , McaTpBootFromRam | McaTpBusDisable );
1591         WBYTE(DpRamP->DpResetTpu , 0xFF );
1592         suspend( 3 );
1593         WBYTE(DpRamP->DpControl  , McaTpBootFromRam | McaTpBusDisable );
1594         WBYTE(DpRamP->DpResetTpu , 0xFF );
1595         suspend( 3 );
1596                 break;
1597 #endif
1598         case RIO_PCI:
1599                 rio_dprintk (RIO_DEBUG_INIT, " (RIO_PCI)\n");
1600                 DpRamP->DpControl  = RIO_PCI_BOOT_FROM_RAM;
1601                 DpRamP->DpResetInt = 0xFF;
1602                 DpRamP->DpResetTpu = 0xFF;
1603                 udelay(100);
1604                 /* for (i=0; i<6000; i++);  */
1605                 /* suspend( 3 ); */
1606                 break;
1607 #ifdef FUTURE_RELEASE
1608         default:
1609         Rprintf(RIOMesgNoSupport,Type,DpRamP,Slot);
1610         return;
1611 #endif
1612
1613         default:
1614                 rio_dprintk (RIO_DEBUG_INIT, " (UNKNOWN)\n");
1615                 break;
1616         }
1617         return;
1618 }