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