]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/char/ip2/i2ellis.c
61ef013b84457887fdf803c0e70fe75b713ff6fb
[linux-2.6-omap-h63xx.git] / drivers / char / ip2 / i2ellis.c
1 /*******************************************************************************
2 *
3 *   (c) 1998 by Computone Corporation
4 *
5 ********************************************************************************
6 *
7 *
8 *   PACKAGE:     Linux tty Device Driver for IntelliPort family of multiport
9 *                serial I/O controllers.
10 *
11 *   DESCRIPTION: Low-level interface code for the device driver
12 *                (This is included source code, not a separate compilation
13 *                module.)
14 *
15 *******************************************************************************/
16 //---------------------------------------------
17 // Function declarations private to this module
18 //---------------------------------------------
19 // Functions called only indirectly through i2eBordStr entries.
20
21 static int iiWriteBuf16(i2eBordStrPtr, unsigned char *, int);
22 static int iiWriteBuf8(i2eBordStrPtr, unsigned char *, int);
23 static int iiReadBuf16(i2eBordStrPtr, unsigned char *, int);
24 static int iiReadBuf8(i2eBordStrPtr, unsigned char *, int);
25
26 static unsigned short iiReadWord16(i2eBordStrPtr);
27 static unsigned short iiReadWord8(i2eBordStrPtr);
28 static void iiWriteWord16(i2eBordStrPtr, unsigned short);
29 static void iiWriteWord8(i2eBordStrPtr, unsigned short);
30
31 static int iiWaitForTxEmptyII(i2eBordStrPtr, int);
32 static int iiWaitForTxEmptyIIEX(i2eBordStrPtr, int);
33 static int iiTxMailEmptyII(i2eBordStrPtr);
34 static int iiTxMailEmptyIIEX(i2eBordStrPtr);
35 static int iiTrySendMailII(i2eBordStrPtr, unsigned char);
36 static int iiTrySendMailIIEX(i2eBordStrPtr, unsigned char);
37
38 static unsigned short iiGetMailII(i2eBordStrPtr);
39 static unsigned short iiGetMailIIEX(i2eBordStrPtr);
40
41 static void iiEnableMailIrqII(i2eBordStrPtr);
42 static void iiEnableMailIrqIIEX(i2eBordStrPtr);
43 static void iiWriteMaskII(i2eBordStrPtr, unsigned char);
44 static void iiWriteMaskIIEX(i2eBordStrPtr, unsigned char);
45
46 static void ii2Nop(void);
47
48 //***************
49 //* Static Data *
50 //***************
51
52 static int ii2Safe;         // Safe I/O address for delay routine
53
54 static int iiDelayed;   // Set when the iiResetDelay function is
55                                                         // called. Cleared when ANY board is reset.
56 static rwlock_t Dl_spinlock;
57
58 //********
59 //* Code *
60 //********
61
62 //=======================================================
63 // Initialization Routines
64 //
65 // iiSetAddress
66 // iiReset
67 // iiResetDelay
68 // iiInitialize
69 //=======================================================
70
71 //******************************************************************************
72 // Function:   iiEllisInit()
73 // Parameters: None
74 //
75 // Returns:    Nothing
76 //
77 // Description:
78 //
79 // This routine performs any required initialization of the iiEllis subsystem.
80 //
81 //******************************************************************************
82 static void
83 iiEllisInit(void)
84 {
85         LOCK_INIT(&Dl_spinlock);
86 }
87
88 //******************************************************************************
89 // Function:   iiEllisCleanup()
90 // Parameters: None
91 //
92 // Returns:    Nothing
93 //
94 // Description:
95 //
96 // This routine performs any required cleanup of the iiEllis subsystem.
97 //
98 //******************************************************************************
99 static void
100 iiEllisCleanup(void)
101 {
102 }
103
104 //******************************************************************************
105 // Function:   iiSetAddress(pB, address, delay)
106 // Parameters: pB      - pointer to the board structure
107 //             address - the purported I/O address of the board
108 //             delay   - pointer to the 1-ms delay function to use
109 //                       in this and any future operations to this board
110 //
111 // Returns:    True if everything appears copacetic.
112 //             False if there is any error: the pB->i2eError field has the error
113 //
114 // Description:
115 //
116 // This routine (roughly) checks for address validity, sets the i2eValid OK and
117 // sets the state to II_STATE_COLD which means that we haven't even sent a reset
118 // yet.
119 //
120 //******************************************************************************
121 static int
122 iiSetAddress( i2eBordStrPtr pB, int address, delayFunc_t delay )
123 {
124         // Should any failure occur before init is finished...
125         pB->i2eValid = I2E_INCOMPLETE;
126
127         // Cannot check upper limit except extremely: Might be microchannel
128         // Address must be on an 8-byte boundary
129
130         if ((unsigned int)address <= 0x100
131                 || (unsigned int)address >= 0xfff8
132                 || (address & 0x7)
133                 )
134         {
135                 COMPLETE(pB,I2EE_BADADDR);
136         }
137
138         // Initialize accelerators
139         pB->i2eBase    = address;
140         pB->i2eData    = address + FIFO_DATA;
141         pB->i2eStatus  = address + FIFO_STATUS;
142         pB->i2ePointer = address + FIFO_PTR;
143         pB->i2eXMail   = address + FIFO_MAIL;
144         pB->i2eXMask   = address + FIFO_MASK;
145
146         // Initialize i/o address for ii2DelayIO
147         ii2Safe = address + FIFO_NOP;
148
149         // Initialize the delay routine
150         pB->i2eDelay = ((delay != (delayFunc_t)NULL) ? delay : (delayFunc_t)ii2Nop);
151
152         pB->i2eValid = I2E_MAGIC;
153         pB->i2eState = II_STATE_COLD;
154
155         COMPLETE(pB, I2EE_GOOD);
156 }
157
158 //******************************************************************************
159 // Function:   iiReset(pB)
160 // Parameters: pB - pointer to the board structure
161 //
162 // Returns:    True if everything appears copacetic.
163 //             False if there is any error: the pB->i2eError field has the error
164 //
165 // Description:
166 //
167 // Attempts to reset the board (see also i2hw.h). Normally, we would use this to
168 // reset a board immediately after iiSetAddress(), but it is valid to reset a
169 // board from any state, say, in order to change or re-load loadware. (Under
170 // such circumstances, no reason to re-run iiSetAddress(), which is why it is a
171 // separate routine and not included in this routine.
172 //
173 //******************************************************************************
174 static int
175 iiReset(i2eBordStrPtr pB)
176 {
177         // Magic number should be set, else even the address is suspect
178         if (pB->i2eValid != I2E_MAGIC)
179         {
180                 COMPLETE(pB, I2EE_BADMAGIC);
181         }
182
183         OUTB(pB->i2eBase + FIFO_RESET, 0);  // Any data will do
184         iiDelay(pB, 50);                    // Pause between resets
185         OUTB(pB->i2eBase + FIFO_RESET, 0);  // Second reset
186
187         // We must wait before even attempting to read anything from the FIFO: the
188         // board's P.O.S.T may actually attempt to read and write its end of the
189         // FIFO in order to check flags, loop back (where supported), etc. On
190         // completion of this testing it would reset the FIFO, and on completion
191         // of all // P.O.S.T., write the message. We must not mistake data which
192         // might have been sent for testing as part of the reset message. To
193         // better utilize time, say, when resetting several boards, we allow the
194         // delay to be performed externally; in this way the caller can reset 
195         // several boards, delay a single time, then call the initialization
196         // routine for all.
197
198         pB->i2eState = II_STATE_RESET;
199
200         iiDelayed = 0;  // i.e., the delay routine hasn't been called since the most
201                                         // recent reset.
202
203         // Ensure anything which would have been of use to standard loadware is
204         // blanked out, since board has now forgotten everything!.
205
206         pB->i2eUsingIrq = IRQ_UNDEFINED; // Not set up to use an interrupt yet
207         pB->i2eWaitingForEmptyFifo = 0;
208         pB->i2eOutMailWaiting = 0;
209         pB->i2eChannelPtr = NULL;
210         pB->i2eChannelCnt = 0;
211
212         pB->i2eLeadoffWord[0] = 0;
213         pB->i2eFifoInInts = 0;
214         pB->i2eFifoOutInts = 0;
215         pB->i2eFatalTrap = NULL;
216         pB->i2eFatal = 0;
217
218         COMPLETE(pB, I2EE_GOOD);
219 }
220
221 //******************************************************************************
222 // Function:   iiResetDelay(pB)
223 // Parameters: pB - pointer to the board structure
224 //
225 // Returns:    True if everything appears copacetic.
226 //             False if there is any error: the pB->i2eError field has the error
227 //
228 // Description:
229 //
230 // Using the delay defined in board structure, waits two seconds (for board to
231 // reset).
232 //
233 //******************************************************************************
234 static int
235 iiResetDelay(i2eBordStrPtr pB)
236 {
237         if (pB->i2eValid != I2E_MAGIC) {
238                 COMPLETE(pB, I2EE_BADMAGIC);
239         }
240         if (pB->i2eState != II_STATE_RESET) {
241                 COMPLETE(pB, I2EE_BADSTATE);
242         }
243         iiDelay(pB,2000);       /* Now we wait for two seconds. */
244         iiDelayed = 1;          /* Delay has been called: ok to initialize */
245         COMPLETE(pB, I2EE_GOOD);
246 }
247
248 //******************************************************************************
249 // Function:   iiInitialize(pB)
250 // Parameters: pB - pointer to the board structure
251 //
252 // Returns:    True if everything appears copacetic.
253 //             False if there is any error: the pB->i2eError field has the error
254 //
255 // Description:
256 //
257 // Attempts to read the Power-on reset message. Initializes any remaining fields
258 // in the pB structure.
259 //
260 // This should be called as the third step of a process beginning with
261 // iiReset(), then iiResetDelay(). This routine checks to see that the structure
262 // is "valid" and in the reset state, also confirms that the delay routine has
263 // been called since the latest reset (to any board! overly strong!).
264 //
265 //******************************************************************************
266 static int
267 iiInitialize(i2eBordStrPtr pB)
268 {
269         int itemp;
270         unsigned char c;
271         unsigned short utemp;
272         unsigned int ilimit;
273
274         if (pB->i2eValid != I2E_MAGIC)
275         {
276                 COMPLETE(pB, I2EE_BADMAGIC);
277         }
278
279         if (pB->i2eState != II_STATE_RESET || !iiDelayed)
280         {
281                 COMPLETE(pB, I2EE_BADSTATE);
282         }
283
284         // In case there is a failure short of our completely reading the power-up
285         // message.
286         pB->i2eValid = I2E_INCOMPLETE;
287
288
289         // Now attempt to read the message.
290
291         for (itemp = 0; itemp < sizeof(porStr); itemp++)
292         {
293                 // We expect the entire message is ready.
294                 if (HAS_NO_INPUT(pB))
295                 {
296                         pB->i2ePomSize = itemp;
297                         COMPLETE(pB, I2EE_PORM_SHORT);
298                 }
299
300                 pB->i2ePom.c[itemp] = c = BYTE_FROM(pB);
301
302                 // We check the magic numbers as soon as they are supposed to be read
303                 // (rather than after) to minimize effect of reading something we
304                 // already suspect can't be "us".
305                 if (  (itemp == POR_1_INDEX && c != POR_MAGIC_1) ||
306                                 (itemp == POR_2_INDEX && c != POR_MAGIC_2))
307                 {
308                         pB->i2ePomSize = itemp+1;
309                         COMPLETE(pB, I2EE_BADMAGIC);
310                 }
311         }
312
313         pB->i2ePomSize = itemp;
314
315         // Ensure that this was all the data...
316         if (HAS_INPUT(pB))
317                 COMPLETE(pB, I2EE_PORM_LONG);
318
319         // For now, we'll fail to initialize if P.O.S.T reports bad chip mapper:
320         // Implying we will not be able to download any code either:  That's ok: the
321         // condition is pretty explicit.
322         if (pB->i2ePom.e.porDiag1 & POR_BAD_MAPPER)
323         {
324                 COMPLETE(pB, I2EE_POSTERR);
325         }
326
327         // Determine anything which must be done differently depending on the family
328         // of boards!
329         switch (pB->i2ePom.e.porID & POR_ID_FAMILY)
330         {
331         case POR_ID_FII:  // IntelliPort-II
332
333                 pB->i2eFifoStyle   = FIFO_II;
334                 pB->i2eFifoSize    = 512;     // 512 bytes, always
335                 pB->i2eDataWidth16 = NO;
336
337                 pB->i2eMaxIrq = 15;     // Because board cannot tell us it is in an 8-bit
338                                                         // slot, we do allow it to be done (documentation!)
339
340                 pB->i2eGoodMap[1] =
341                 pB->i2eGoodMap[2] =
342                 pB->i2eGoodMap[3] =
343                 pB->i2eChannelMap[1] =
344                 pB->i2eChannelMap[2] =
345                 pB->i2eChannelMap[3] = 0;
346
347                 switch (pB->i2ePom.e.porID & POR_ID_SIZE)
348                 {
349                 case POR_ID_II_4:
350                         pB->i2eGoodMap[0] =
351                         pB->i2eChannelMap[0] = 0x0f;  // four-port
352
353                         // Since porPorts1 is based on the Hardware ID register, the numbers
354                         // should always be consistent for IntelliPort-II.  Ditto below...
355                         if (pB->i2ePom.e.porPorts1 != 4)
356                         {
357                                 COMPLETE(pB, I2EE_INCONSIST);
358                         }
359                         break;
360
361                 case POR_ID_II_8:
362                 case POR_ID_II_8R:
363                         pB->i2eGoodMap[0] =
364                         pB->i2eChannelMap[0] = 0xff;  // Eight port
365                         if (pB->i2ePom.e.porPorts1 != 8)
366                         {
367                                 COMPLETE(pB, I2EE_INCONSIST);
368                         }
369                         break;
370
371                 case POR_ID_II_6:
372                         pB->i2eGoodMap[0] =
373                         pB->i2eChannelMap[0] = 0x3f;  // Six Port
374                         if (pB->i2ePom.e.porPorts1 != 6)
375                         {
376                                 COMPLETE(pB, I2EE_INCONSIST);
377                         }
378                         break;
379                 }
380
381                 // Fix up the "good channel list based on any errors reported.
382                 if (pB->i2ePom.e.porDiag1 & POR_BAD_UART1)
383                 {
384                         pB->i2eGoodMap[0] &= ~0x0f;
385                 }
386
387                 if (pB->i2ePom.e.porDiag1 & POR_BAD_UART2)
388                 {
389                         pB->i2eGoodMap[0] &= ~0xf0;
390                 }
391
392                 break;   // POR_ID_FII case
393
394         case POR_ID_FIIEX:   // IntelliPort-IIEX
395
396                 pB->i2eFifoStyle = FIFO_IIEX;
397
398                 itemp = pB->i2ePom.e.porFifoSize;
399
400                 // Implicit assumption that fifo would not grow beyond 32k, 
401                 // nor would ever be less than 256.
402
403                 if (itemp < 8 || itemp > 15)
404                 {
405                         COMPLETE(pB, I2EE_INCONSIST);
406                 }
407                 pB->i2eFifoSize = (1 << itemp);
408
409                 // These are based on what P.O.S.T thinks should be there, based on
410                 // box ID registers
411                 ilimit = pB->i2ePom.e.porNumBoxes;
412                 if (ilimit > ABS_MAX_BOXES)
413                 {
414                         ilimit = ABS_MAX_BOXES;
415                 }
416
417                 // For as many boxes as EXIST, gives the type of box.
418                 // Added 8/6/93: check for the ISA-4 (asic) which looks like an
419                 // expandable but for whom "8 or 16?" is not the right question.
420
421                 utemp = pB->i2ePom.e.porFlags;
422                 if (utemp & POR_CEX4)
423                 {
424                         pB->i2eChannelMap[0] = 0x000f;
425                 } else {
426                         utemp &= POR_BOXES;
427                         for (itemp = 0; itemp < ilimit; itemp++)
428                         {
429                                 pB->i2eChannelMap[itemp] = 
430                                         ((utemp & POR_BOX_16) ? 0xffff : 0x00ff);
431                                 utemp >>= 1;
432                         }
433                 }
434
435                 // These are based on what P.O.S.T actually found.
436
437                 utemp = (pB->i2ePom.e.porPorts2 << 8) + pB->i2ePom.e.porPorts1;
438
439                 for (itemp = 0; itemp < ilimit; itemp++)
440                 {
441                         pB->i2eGoodMap[itemp] = 0;
442                         if (utemp & 1) pB->i2eGoodMap[itemp] |= 0x000f;
443                         if (utemp & 2) pB->i2eGoodMap[itemp] |= 0x00f0;
444                         if (utemp & 4) pB->i2eGoodMap[itemp] |= 0x0f00;
445                         if (utemp & 8) pB->i2eGoodMap[itemp] |= 0xf000;
446                         utemp >>= 4;
447                 }
448
449                 // Now determine whether we should transfer in 8 or 16-bit mode.
450                 switch (pB->i2ePom.e.porBus & (POR_BUS_SLOT16 | POR_BUS_DIP16) )
451                 {
452                 case POR_BUS_SLOT16 | POR_BUS_DIP16:
453                         pB->i2eDataWidth16 = YES;
454                         pB->i2eMaxIrq = 15;
455                         break;
456
457                 case POR_BUS_SLOT16:
458                         pB->i2eDataWidth16 = NO;
459                         pB->i2eMaxIrq = 15;
460                         break;
461
462                 case 0:
463                 case POR_BUS_DIP16:     // In an 8-bit slot, DIP switch don't care.
464                 default:
465                         pB->i2eDataWidth16 = NO;
466                         pB->i2eMaxIrq = 7;
467                         break;
468                 }
469                 break;   // POR_ID_FIIEX case
470
471         default:    // Unknown type of board
472                 COMPLETE(pB, I2EE_BAD_FAMILY);
473                 break;
474         }  // End the switch based on family
475
476         // Temporarily, claim there is no room in the outbound fifo. 
477         // We will maintain this whenever we check for an empty outbound FIFO.
478         pB->i2eFifoRemains = 0;
479
480         // Now, based on the bus type, should we expect to be able to re-configure
481         // interrupts (say, for testing purposes).
482         switch (pB->i2ePom.e.porBus & POR_BUS_TYPE)
483         {
484         case POR_BUS_T_ISA:
485         case POR_BUS_T_UNK:  // If the type of bus is undeclared, assume ok.
486                 pB->i2eChangeIrq = YES;
487                 break;
488         case POR_BUS_T_MCA:
489         case POR_BUS_T_EISA:
490                 pB->i2eChangeIrq = NO;
491                 break;
492         default:
493                 COMPLETE(pB, I2EE_BADBUS);
494         }
495
496         if (pB->i2eDataWidth16 == YES)
497         {
498                 pB->i2eWriteBuf  = iiWriteBuf16;
499                 pB->i2eReadBuf   = iiReadBuf16;
500                 pB->i2eWriteWord = iiWriteWord16;
501                 pB->i2eReadWord  = iiReadWord16;
502         } else {
503                 pB->i2eWriteBuf  = iiWriteBuf8;
504                 pB->i2eReadBuf   = iiReadBuf8;
505                 pB->i2eWriteWord = iiWriteWord8;
506                 pB->i2eReadWord  = iiReadWord8;
507         }
508
509         switch(pB->i2eFifoStyle)
510         {
511         case FIFO_II:
512                 pB->i2eWaitForTxEmpty = iiWaitForTxEmptyII;
513                 pB->i2eTxMailEmpty    = iiTxMailEmptyII;
514                 pB->i2eTrySendMail    = iiTrySendMailII;
515                 pB->i2eGetMail        = iiGetMailII;
516                 pB->i2eEnableMailIrq  = iiEnableMailIrqII;
517                 pB->i2eWriteMask      = iiWriteMaskII;
518
519                 break;
520
521         case FIFO_IIEX:
522                 pB->i2eWaitForTxEmpty = iiWaitForTxEmptyIIEX;
523                 pB->i2eTxMailEmpty    = iiTxMailEmptyIIEX;
524                 pB->i2eTrySendMail    = iiTrySendMailIIEX;
525                 pB->i2eGetMail        = iiGetMailIIEX;
526                 pB->i2eEnableMailIrq  = iiEnableMailIrqIIEX;
527                 pB->i2eWriteMask      = iiWriteMaskIIEX;
528
529                 break;
530
531         default:
532                 COMPLETE(pB, I2EE_INCONSIST);
533         }
534
535         // Initialize state information.
536         pB->i2eState = II_STATE_READY;   // Ready to load loadware.
537
538         // Some Final cleanup:
539         // For some boards, the bootstrap firmware may perform some sort of test
540         // resulting in a stray character pending in the incoming mailbox. If one is
541         // there, it should be read and discarded, especially since for the standard
542         // firmware, it's the mailbox that interrupts the host.
543
544         pB->i2eStartMail = iiGetMail(pB);
545
546         // Throw it away and clear the mailbox structure element
547         pB->i2eStartMail = NO_MAIL_HERE;
548
549         // Everything is ok now, return with good status/
550
551         pB->i2eValid = I2E_MAGIC;
552         COMPLETE(pB, I2EE_GOOD);
553 }
554
555 //******************************************************************************
556 // Function:   ii2DelayTimer(mseconds)
557 // Parameters: mseconds - number of milliseconds to delay
558 //
559 // Returns:    Nothing
560 //
561 // Description:
562 //
563 // This routine delays for approximately mseconds milliseconds and is intended
564 // to be called indirectly through i2Delay field in i2eBordStr. It uses the
565 // Linux timer_list mechanism.
566 //
567 // The Linux timers use a unit called "jiffies" which are 10mS in the Intel
568 // architecture. This function rounds the delay period up to the next "jiffy".
569 // In the Alpha architecture the "jiffy" is 1mS, but this driver is not intended
570 // for Alpha platforms at this time.
571 //
572 //******************************************************************************
573 static void
574 ii2DelayTimer(unsigned int mseconds)
575 {
576         msleep_interruptible(mseconds);
577 }
578
579 #if 0
580 //static void ii2DelayIO(unsigned int);
581 //******************************************************************************
582 // !!! Not Used, this is DOS crap, some of you young folks may be interested in
583 //     in how things were done in the stone age of caculating machines       !!!
584 // Function:   ii2DelayIO(mseconds)
585 // Parameters: mseconds - number of milliseconds to delay
586 //
587 // Returns:    Nothing
588 //
589 // Description:
590 //
591 // This routine delays for approximately mseconds milliseconds and is intended
592 // to be called indirectly through i2Delay field in i2eBordStr. It is intended
593 // for use where a clock-based function is impossible: for example, DOS drivers.
594 //
595 // This function uses the IN instruction to place bounds on the timing and
596 // assumes that ii2Safe has been set. This is because I/O instructions are not
597 // subject to caching and will therefore take a certain minimum time. To ensure
598 // the delay is at least long enough on fast machines, it is based on some
599 // fastest-case calculations.  On slower machines this may cause VERY long
600 // delays. (3 x fastest case). In the fastest case, everything is cached except
601 // the I/O instruction itself.
602 //
603 // Timing calculations:
604 // The fastest bus speed for I/O operations is likely to be 10 MHz. The I/O
605 // operation in question is a byte operation to an odd address. For 8-bit
606 // operations, the architecture generally enforces two wait states. At 10 MHz, a
607 // single cycle time is 100nS. A read operation at two wait states takes 6
608 // cycles for a total time of 600nS. Therefore approximately 1666 iterations
609 // would be required to generate a single millisecond delay. The worst
610 // (reasonable) case would be an 8MHz system with no cacheing. In this case, the
611 // I/O instruction would take 125nS x 6 cyles = 750 nS. More importantly, code
612 // fetch of other instructions in the loop would take time (zero wait states,
613 // however) and would be hard to estimate. This is minimized by using in-line
614 // assembler for the in inner loop of IN instructions. This consists of just a
615 // few bytes. So we'll guess about four code fetches per loop. Each code fetch
616 // should take four cycles, so we have 125nS * 8 = 1000nS. Worst case then is
617 // that what should have taken 1 mS takes instead 1666 * (1750) = 2.9 mS.
618 //
619 // So much for theoretical timings: results using 1666 value on some actual
620 // machines:
621 // IBM      286      6MHz     3.15 mS
622 // Zenith   386      33MHz    2.45 mS
623 // (brandX) 386      33MHz    1.90 mS  (has cache)
624 // (brandY) 486      33MHz    2.35 mS
625 // NCR      486      ??       1.65 mS (microchannel)
626 //
627 // For most machines, it is probably safe to scale this number back (remember,
628 // for robust operation use an actual timed delay if possible), so we are using
629 // a value of 1190. This yields 1.17 mS for the fastest machine in our sample,
630 // 1.75 mS for typical 386 machines, and 2.25 mS the absolute slowest machine.
631 //
632 // 1/29/93:
633 // The above timings are too slow. Actual cycle times might be faster. ISA cycle
634 // times could approach 500 nS, and ...
635 // The IBM model 77 being microchannel has no wait states for 8-bit reads and
636 // seems to be accessing the I/O at 440 nS per access (from start of one to
637 // start of next). This would imply we need 1000/.440 = 2272 iterations to
638 // guarantee we are fast enough. In actual testing, we see that 2 * 1190 are in
639 // fact enough. For diagnostics, we keep the level at 1190, but developers note
640 // this needs tuning.
641 //
642 // Safe assumption:  2270 i/o reads = 1 millisecond
643 //
644 //******************************************************************************
645
646
647 static int ii2DelValue = 1190;  // See timing calculations below
648                                                 // 1666 for fastest theoretical machine
649                                                 // 1190 safe for most fast 386 machines
650                                                 // 1000 for fastest machine tested here
651                                                 //  540 (sic) for AT286/6Mhz
652 static void
653 ii2DelayIO(unsigned int mseconds)
654 {
655         if (!ii2Safe) 
656                 return;   /* Do nothing if this variable uninitialized */
657
658         while(mseconds--) {
659                 int i = ii2DelValue;
660                 while ( i-- ) {
661                         INB ( ii2Safe );
662                 }
663         }
664 }
665 #endif 
666
667 //******************************************************************************
668 // Function:   ii2Nop()
669 // Parameters: None
670 //
671 // Returns:    Nothing
672 //
673 // Description:
674 //
675 // iiInitialize will set i2eDelay to this if the delay parameter is NULL. This
676 // saves checking for a NULL pointer at every call.
677 //******************************************************************************
678 static void
679 ii2Nop(void)
680 {
681         return; // no mystery here
682 }
683
684 //=======================================================
685 // Routines which are available in 8/16-bit versions, or
686 // in different fifo styles. These are ALL called
687 // indirectly through the board structure.
688 //=======================================================
689
690 //******************************************************************************
691 // Function:   iiWriteBuf16(pB, address, count)
692 // Parameters: pB      - pointer to board structure
693 //             address - address of data to write
694 //             count   - number of data bytes to write
695 //
696 // Returns:    True if everything appears copacetic.
697 //             False if there is any error: the pB->i2eError field has the error
698 //
699 // Description:
700 //
701 // Writes 'count' bytes from 'address' to the data fifo specified by the board
702 // structure pointer pB. Should count happen to be odd, an extra pad byte is
703 // sent (identity unknown...). Uses 16-bit (word) operations. Is called
704 // indirectly through pB->i2eWriteBuf.
705 //
706 //******************************************************************************
707 static int
708 iiWriteBuf16(i2eBordStrPtr pB, unsigned char *address, int count)
709 {
710         // Rudimentary sanity checking here.
711         if (pB->i2eValid != I2E_MAGIC)
712                 COMPLETE(pB, I2EE_INVALID);
713
714         OUTSW ( pB->i2eData, address, count);
715
716         COMPLETE(pB, I2EE_GOOD);
717 }
718
719 //******************************************************************************
720 // Function:   iiWriteBuf8(pB, address, count)
721 // Parameters: pB      - pointer to board structure
722 //             address - address of data to write
723 //             count   - number of data bytes to write
724 //
725 // Returns:    True if everything appears copacetic.
726 //             False if there is any error: the pB->i2eError field has the error
727 //
728 // Description:
729 //
730 // Writes 'count' bytes from 'address' to the data fifo specified by the board
731 // structure pointer pB. Should count happen to be odd, an extra pad byte is
732 // sent (identity unknown...). This is to be consistent with the 16-bit version.
733 // Uses 8-bit (byte) operations. Is called indirectly through pB->i2eWriteBuf.
734 //
735 //******************************************************************************
736 static int
737 iiWriteBuf8(i2eBordStrPtr pB, unsigned char *address, int count)
738 {
739         /* Rudimentary sanity checking here */
740         if (pB->i2eValid != I2E_MAGIC)
741                 COMPLETE(pB, I2EE_INVALID);
742
743         OUTSB ( pB->i2eData, address, count );
744
745         COMPLETE(pB, I2EE_GOOD);
746 }
747
748 //******************************************************************************
749 // Function:   iiReadBuf16(pB, address, count)
750 // Parameters: pB      - pointer to board structure
751 //             address - address to put data read
752 //             count   - number of data bytes to read
753 //
754 // Returns:    True if everything appears copacetic.
755 //             False if there is any error: the pB->i2eError field has the error
756 //
757 // Description:
758 //
759 // Reads 'count' bytes into 'address' from the data fifo specified by the board
760 // structure pointer pB. Should count happen to be odd, an extra pad byte is
761 // received (identity unknown...). Uses 16-bit (word) operations. Is called
762 // indirectly through pB->i2eReadBuf.
763 //
764 //******************************************************************************
765 static int
766 iiReadBuf16(i2eBordStrPtr pB, unsigned char *address, int count)
767 {
768         // Rudimentary sanity checking here.
769         if (pB->i2eValid != I2E_MAGIC)
770                 COMPLETE(pB, I2EE_INVALID);
771
772         INSW ( pB->i2eData, address, count);
773
774         COMPLETE(pB, I2EE_GOOD);
775 }
776
777 //******************************************************************************
778 // Function:   iiReadBuf8(pB, address, count)
779 // Parameters: pB      - pointer to board structure
780 //             address - address to put data read
781 //             count   - number of data bytes to read
782 //
783 // Returns:    True if everything appears copacetic.
784 //             False if there is any error: the pB->i2eError field has the error
785 //
786 // Description:
787 //
788 // Reads 'count' bytes into 'address' from the data fifo specified by the board
789 // structure pointer pB. Should count happen to be odd, an extra pad byte is
790 // received (identity unknown...). This to match the 16-bit behaviour. Uses
791 // 8-bit (byte) operations. Is called indirectly through pB->i2eReadBuf.
792 //
793 //******************************************************************************
794 static int
795 iiReadBuf8(i2eBordStrPtr pB, unsigned char *address, int count)
796 {
797         // Rudimentary sanity checking here.
798         if (pB->i2eValid != I2E_MAGIC)
799                 COMPLETE(pB, I2EE_INVALID);
800
801         INSB ( pB->i2eData, address, count);
802
803         COMPLETE(pB, I2EE_GOOD);
804 }
805
806 //******************************************************************************
807 // Function:   iiReadWord16(pB)
808 // Parameters: pB      - pointer to board structure
809 //
810 // Returns:    True if everything appears copacetic.
811 //             False if there is any error: the pB->i2eError field has the error
812 //
813 // Description:
814 //
815 // Returns the word read from the data fifo specified by the board-structure
816 // pointer pB. Uses a 16-bit operation. Is called indirectly through
817 // pB->i2eReadWord.
818 //
819 //******************************************************************************
820 static unsigned short
821 iiReadWord16(i2eBordStrPtr pB)
822 {
823         return (unsigned short)( INW(pB->i2eData) );
824 }
825
826 //******************************************************************************
827 // Function:   iiReadWord8(pB)
828 // Parameters: pB      - pointer to board structure
829 //
830 // Returns:    True if everything appears copacetic.
831 //             False if there is any error: the pB->i2eError field has the error
832 //
833 // Description:
834 //
835 // Returns the word read from the data fifo specified by the board-structure
836 // pointer pB. Uses two 8-bit operations. Bytes are assumed to be LSB first. Is
837 // called indirectly through pB->i2eReadWord.
838 //
839 //******************************************************************************
840 static unsigned short
841 iiReadWord8(i2eBordStrPtr pB)
842 {
843         unsigned short urs;
844
845         urs = INB ( pB->i2eData );
846
847         return ( ( INB ( pB->i2eData ) << 8 ) | urs );
848 }
849
850 //******************************************************************************
851 // Function:   iiWriteWord16(pB, value)
852 // Parameters: pB    - pointer to board structure
853 //             value - data to write
854 //
855 // Returns:    True if everything appears copacetic.
856 //             False if there is any error: the pB->i2eError field has the error
857 //
858 // Description:
859 //
860 // Writes the word 'value' to the data fifo specified by the board-structure
861 // pointer pB. Uses 16-bit operation. Is called indirectly through
862 // pB->i2eWriteWord.
863 //
864 //******************************************************************************
865 static void
866 iiWriteWord16(i2eBordStrPtr pB, unsigned short value)
867 {
868         WORD_TO(pB, (int)value);
869 }
870
871 //******************************************************************************
872 // Function:   iiWriteWord8(pB, value)
873 // Parameters: pB    - pointer to board structure
874 //             value - data to write
875 //
876 // Returns:    True if everything appears copacetic.
877 //             False if there is any error: the pB->i2eError field has the error
878 //
879 // Description:
880 //
881 // Writes the word 'value' to the data fifo specified by the board-structure
882 // pointer pB. Uses two 8-bit operations (writes LSB first). Is called
883 // indirectly through pB->i2eWriteWord.
884 //
885 //******************************************************************************
886 static void
887 iiWriteWord8(i2eBordStrPtr pB, unsigned short value)
888 {
889         BYTE_TO(pB, (char)value);
890         BYTE_TO(pB, (char)(value >> 8) );
891 }
892
893 //******************************************************************************
894 // Function:   iiWaitForTxEmptyII(pB, mSdelay)
895 // Parameters: pB      - pointer to board structure
896 //             mSdelay - period to wait before returning
897 //
898 // Returns:    True if the FIFO is empty.
899 //             False if it not empty in the required time: the pB->i2eError
900 //             field has the error.
901 //
902 // Description:
903 //
904 // Waits up to "mSdelay" milliseconds for the outgoing FIFO to become empty; if
905 // not empty by the required time, returns false and error in pB->i2eError,
906 // otherwise returns true.
907 //
908 // mSdelay == 0 is taken to mean must be empty on the first test.
909 //
910 // This version operates on IntelliPort-II - style FIFO's
911 //
912 // Note this routine is organized so that if status is ok there is no delay at
913 // all called either before or after the test.  Is called indirectly through
914 // pB->i2eWaitForTxEmpty.
915 //
916 //******************************************************************************
917 static int
918 iiWaitForTxEmptyII(i2eBordStrPtr pB, int mSdelay)
919 {
920         unsigned long   flags;
921         int itemp;
922
923         for (;;)
924         {
925                 // This routine hinges on being able to see the "other" status register
926                 // (as seen by the local processor).  His incoming fifo is our outgoing
927                 // FIFO.
928                 //
929                 // By the nature of this routine, you would be using this as part of a
930                 // larger atomic context: i.e., you would use this routine to ensure the
931                 // fifo empty, then act on this information. Between these two halves, 
932                 // you will generally not want to service interrupts or in any way 
933                 // disrupt the assumptions implicit in the larger context.
934                 //
935                 // Even worse, however, this routine "shifts" the status register to 
936                 // point to the local status register which is not the usual situation.
937                 // Therefore for extra safety, we force the critical section to be
938                 // completely atomic, and pick up after ourselves before allowing any
939                 // interrupts of any kind.
940
941
942                 WRITE_LOCK_IRQSAVE(&Dl_spinlock,flags)
943                 OUTB(pB->i2ePointer, SEL_COMMAND);
944                 OUTB(pB->i2ePointer, SEL_CMD_SH);
945
946                 itemp = INB(pB->i2eStatus);
947
948                 OUTB(pB->i2ePointer, SEL_COMMAND);
949                 OUTB(pB->i2ePointer, SEL_CMD_UNSH);
950
951                 if (itemp & ST_IN_EMPTY)
952                 {
953                         UPDATE_FIFO_ROOM(pB);
954                         WRITE_UNLOCK_IRQRESTORE(&Dl_spinlock,flags)
955                         COMPLETE(pB, I2EE_GOOD);
956                 }
957
958                 WRITE_UNLOCK_IRQRESTORE(&Dl_spinlock,flags)
959
960                 if (mSdelay-- == 0)
961                         break;
962
963                 iiDelay(pB, 1);      /* 1 mS granularity on checking condition */
964         }
965         COMPLETE(pB, I2EE_TXE_TIME);
966 }
967
968 //******************************************************************************
969 // Function:   iiWaitForTxEmptyIIEX(pB, mSdelay)
970 // Parameters: pB      - pointer to board structure
971 //             mSdelay - period to wait before returning
972 //
973 // Returns:    True if the FIFO is empty.
974 //             False if it not empty in the required time: the pB->i2eError
975 //             field has the error.
976 //
977 // Description:
978 //
979 // Waits up to "mSdelay" milliseconds for the outgoing FIFO to become empty; if
980 // not empty by the required time, returns false and error in pB->i2eError,
981 // otherwise returns true.
982 //
983 // mSdelay == 0 is taken to mean must be empty on the first test.
984 //
985 // This version operates on IntelliPort-IIEX - style FIFO's
986 //
987 // Note this routine is organized so that if status is ok there is no delay at
988 // all called either before or after the test.  Is called indirectly through
989 // pB->i2eWaitForTxEmpty.
990 //
991 //******************************************************************************
992 static int
993 iiWaitForTxEmptyIIEX(i2eBordStrPtr pB, int mSdelay)
994 {
995         unsigned long   flags;
996
997         for (;;)
998         {
999                 // By the nature of this routine, you would be using this as part of a
1000                 // larger atomic context: i.e., you would use this routine to ensure the
1001                 // fifo empty, then act on this information. Between these two halves,
1002                 // you will generally not want to service interrupts or in any way
1003                 // disrupt the assumptions implicit in the larger context.
1004
1005                 WRITE_LOCK_IRQSAVE(&Dl_spinlock,flags)
1006
1007                 if (INB(pB->i2eStatus) & STE_OUT_MT) {
1008                         UPDATE_FIFO_ROOM(pB);
1009                         WRITE_UNLOCK_IRQRESTORE(&Dl_spinlock,flags)
1010                         COMPLETE(pB, I2EE_GOOD);
1011                 }
1012                 WRITE_UNLOCK_IRQRESTORE(&Dl_spinlock,flags)
1013
1014                 if (mSdelay-- == 0)
1015                         break;
1016
1017                 iiDelay(pB, 1);      // 1 mS granularity on checking condition
1018         }
1019         COMPLETE(pB, I2EE_TXE_TIME);
1020 }
1021
1022 //******************************************************************************
1023 // Function:   iiTxMailEmptyII(pB)
1024 // Parameters: pB      - pointer to board structure
1025 //
1026 // Returns:    True if the transmit mailbox is empty.
1027 //             False if it not empty.
1028 //
1029 // Description:
1030 //
1031 // Returns true or false according to whether the transmit mailbox is empty (and
1032 // therefore able to accept more mail)
1033 //
1034 // This version operates on IntelliPort-II - style FIFO's
1035 //
1036 //******************************************************************************
1037 static int
1038 iiTxMailEmptyII(i2eBordStrPtr pB)
1039 {
1040         int port = pB->i2ePointer;
1041         OUTB ( port, SEL_OUTMAIL );
1042         return ( INB(port) == 0 );
1043 }
1044
1045 //******************************************************************************
1046 // Function:   iiTxMailEmptyIIEX(pB)
1047 // Parameters: pB      - pointer to board structure
1048 //
1049 // Returns:    True if the transmit mailbox is empty.
1050 //             False if it not empty.
1051 //
1052 // Description:
1053 //
1054 // Returns true or false according to whether the transmit mailbox is empty (and
1055 // therefore able to accept more mail)
1056 //
1057 // This version operates on IntelliPort-IIEX - style FIFO's
1058 //
1059 //******************************************************************************
1060 static int
1061 iiTxMailEmptyIIEX(i2eBordStrPtr pB)
1062 {
1063         return !(INB(pB->i2eStatus) & STE_OUT_MAIL);
1064 }
1065
1066 //******************************************************************************
1067 // Function:   iiTrySendMailII(pB,mail)
1068 // Parameters: pB   - pointer to board structure
1069 //             mail - value to write to mailbox
1070 //
1071 // Returns:    True if the transmit mailbox is empty, and mail is sent.
1072 //             False if it not empty.
1073 //
1074 // Description:
1075 //
1076 // If outgoing mailbox is empty, sends mail and returns true. If outgoing
1077 // mailbox is not empty, returns false.
1078 //
1079 // This version operates on IntelliPort-II - style FIFO's
1080 //
1081 //******************************************************************************
1082 static int
1083 iiTrySendMailII(i2eBordStrPtr pB, unsigned char mail)
1084 {
1085         int port = pB->i2ePointer;
1086
1087         OUTB(port, SEL_OUTMAIL);
1088         if (INB(port) == 0) {
1089                 OUTB(port, SEL_OUTMAIL);
1090                 OUTB(port, mail);
1091                 return 1;
1092         }
1093         return 0;
1094 }
1095
1096 //******************************************************************************
1097 // Function:   iiTrySendMailIIEX(pB,mail)
1098 // Parameters: pB   - pointer to board structure
1099 //             mail - value to write to mailbox
1100 //
1101 // Returns:    True if the transmit mailbox is empty, and mail is sent.
1102 //             False if it not empty.
1103 //
1104 // Description:
1105 //
1106 // If outgoing mailbox is empty, sends mail and returns true. If outgoing
1107 // mailbox is not empty, returns false.
1108 //
1109 // This version operates on IntelliPort-IIEX - style FIFO's
1110 //
1111 //******************************************************************************
1112 static int
1113 iiTrySendMailIIEX(i2eBordStrPtr pB, unsigned char mail)
1114 {
1115         if(INB(pB->i2eStatus) & STE_OUT_MAIL) {
1116                 return 0;
1117         }
1118         OUTB(pB->i2eXMail, mail);
1119         return 1;
1120 }
1121
1122 //******************************************************************************
1123 // Function:   iiGetMailII(pB,mail)
1124 // Parameters: pB   - pointer to board structure
1125 //
1126 // Returns:    Mailbox data or NO_MAIL_HERE.
1127 //
1128 // Description:
1129 //
1130 // If no mail available, returns NO_MAIL_HERE otherwise returns the data from
1131 // the mailbox, which is guaranteed != NO_MAIL_HERE.
1132 //
1133 // This version operates on IntelliPort-II - style FIFO's
1134 //
1135 //******************************************************************************
1136 static unsigned short
1137 iiGetMailII(i2eBordStrPtr pB)
1138 {
1139         if (HAS_MAIL(pB)) {
1140                 OUTB(pB->i2ePointer, SEL_INMAIL);
1141                 return INB(pB->i2ePointer);
1142         } else {
1143                 return NO_MAIL_HERE;
1144         }
1145 }
1146
1147 //******************************************************************************
1148 // Function:   iiGetMailIIEX(pB,mail)
1149 // Parameters: pB   - pointer to board structure
1150 //
1151 // Returns:    Mailbox data or NO_MAIL_HERE.
1152 //
1153 // Description:
1154 //
1155 // If no mail available, returns NO_MAIL_HERE otherwise returns the data from
1156 // the mailbox, which is guaranteed != NO_MAIL_HERE.
1157 //
1158 // This version operates on IntelliPort-IIEX - style FIFO's
1159 //
1160 //******************************************************************************
1161 static unsigned short
1162 iiGetMailIIEX(i2eBordStrPtr pB)
1163 {
1164         if (HAS_MAIL(pB)) {
1165                 return INB(pB->i2eXMail);
1166         } else {
1167                 return NO_MAIL_HERE;
1168         }
1169 }
1170
1171 //******************************************************************************
1172 // Function:   iiEnableMailIrqII(pB)
1173 // Parameters: pB - pointer to board structure
1174 //
1175 // Returns:    Nothing
1176 //
1177 // Description:
1178 //
1179 // Enables board to interrupt host (only) by writing to host's in-bound mailbox.
1180 //
1181 // This version operates on IntelliPort-II - style FIFO's
1182 //
1183 //******************************************************************************
1184 static void
1185 iiEnableMailIrqII(i2eBordStrPtr pB)
1186 {
1187         OUTB(pB->i2ePointer, SEL_MASK);
1188         OUTB(pB->i2ePointer, ST_IN_MAIL);
1189 }
1190
1191 //******************************************************************************
1192 // Function:   iiEnableMailIrqIIEX(pB)
1193 // Parameters: pB - pointer to board structure
1194 //
1195 // Returns:    Nothing
1196 //
1197 // Description:
1198 //
1199 // Enables board to interrupt host (only) by writing to host's in-bound mailbox.
1200 //
1201 // This version operates on IntelliPort-IIEX - style FIFO's
1202 //
1203 //******************************************************************************
1204 static void
1205 iiEnableMailIrqIIEX(i2eBordStrPtr pB)
1206 {
1207         OUTB(pB->i2eXMask, MX_IN_MAIL);
1208 }
1209
1210 //******************************************************************************
1211 // Function:   iiWriteMaskII(pB)
1212 // Parameters: pB - pointer to board structure
1213 //
1214 // Returns:    Nothing
1215 //
1216 // Description:
1217 //
1218 // Writes arbitrary value to the mask register.
1219 //
1220 // This version operates on IntelliPort-II - style FIFO's
1221 //
1222 //******************************************************************************
1223 static void
1224 iiWriteMaskII(i2eBordStrPtr pB, unsigned char value)
1225 {
1226         OUTB(pB->i2ePointer, SEL_MASK);
1227         OUTB(pB->i2ePointer, value);
1228 }
1229
1230 //******************************************************************************
1231 // Function:   iiWriteMaskIIEX(pB)
1232 // Parameters: pB - pointer to board structure
1233 //
1234 // Returns:    Nothing
1235 //
1236 // Description:
1237 //
1238 // Writes arbitrary value to the mask register.
1239 //
1240 // This version operates on IntelliPort-IIEX - style FIFO's
1241 //
1242 //******************************************************************************
1243 static void
1244 iiWriteMaskIIEX(i2eBordStrPtr pB, unsigned char value)
1245 {
1246         OUTB(pB->i2eXMask, value);
1247 }
1248
1249 //******************************************************************************
1250 // Function:   iiDownloadBlock(pB, pSource, isStandard)
1251 // Parameters: pB         - pointer to board structure
1252 //             pSource    - loadware block to download
1253 //             isStandard - True if "standard" loadware, else false.
1254 //
1255 // Returns:    Success or Failure
1256 //
1257 // Description:
1258 //
1259 // Downloads a single block (at pSource)to the board referenced by pB. Caller
1260 // sets isStandard to true/false according to whether the "standard" loadware is
1261 // what's being loaded. The normal process, then, is to perform an iiInitialize
1262 // to the board, then perform some number of iiDownloadBlocks using the returned
1263 // state to determine when download is complete.
1264 //
1265 // Possible return values: (see I2ELLIS.H)
1266 // II_DOWN_BADVALID
1267 // II_DOWN_BADFILE
1268 // II_DOWN_CONTINUING
1269 // II_DOWN_GOOD
1270 // II_DOWN_BAD
1271 // II_DOWN_BADSTATE
1272 // II_DOWN_TIMEOUT
1273 //
1274 // Uses the i2eState and i2eToLoad fields (initialized at iiInitialize) to
1275 // determine whether this is the first block, whether to check for magic
1276 // numbers, how many blocks there are to go...
1277 //
1278 //******************************************************************************
1279 static int
1280 iiDownloadBlock ( i2eBordStrPtr pB, loadHdrStrPtr pSource, int isStandard)
1281 {
1282         int itemp;
1283         int loadedFirst;
1284
1285         if (pB->i2eValid != I2E_MAGIC) return II_DOWN_BADVALID;
1286
1287         switch(pB->i2eState)
1288         {
1289         case II_STATE_READY:
1290
1291                 // Loading the first block after reset. Must check the magic number of the
1292                 // loadfile, store the number of blocks we expect to load.
1293                 if (pSource->e.loadMagic != MAGIC_LOADFILE)
1294                 {
1295                         return II_DOWN_BADFILE;
1296                 }
1297
1298                 // Next we store the total number of blocks to load, including this one.
1299                 pB->i2eToLoad = 1 + pSource->e.loadBlocksMore;
1300
1301                 // Set the state, store the version numbers. ('Cause this may have come
1302                 // from a file - we might want to report these versions and revisions in
1303                 // case of an error!
1304                 pB->i2eState = II_STATE_LOADING;
1305                 pB->i2eLVersion = pSource->e.loadVersion;
1306                 pB->i2eLRevision = pSource->e.loadRevision;
1307                 pB->i2eLSub = pSource->e.loadSubRevision;
1308
1309                 // The time and date of compilation is also available but don't bother
1310                 // storing it for normal purposes.
1311                 loadedFirst = 1;
1312                 break;
1313
1314         case II_STATE_LOADING:
1315                 loadedFirst = 0;
1316                 break;
1317
1318         default:
1319                 return II_DOWN_BADSTATE;
1320         }
1321
1322         // Now we must be in the II_STATE_LOADING state, and we assume i2eToLoad
1323         // must be positive still, because otherwise we would have cleaned up last
1324         // time and set the state to II_STATE_LOADED.
1325         if (!iiWaitForTxEmpty(pB, MAX_DLOAD_READ_TIME)) {
1326                 return II_DOWN_TIMEOUT;
1327         }
1328
1329         if (!iiWriteBuf(pB, pSource->c, LOADWARE_BLOCK_SIZE)) {
1330                 return II_DOWN_BADVALID;
1331         }
1332
1333         // If we just loaded the first block, wait for the fifo to empty an extra
1334         // long time to allow for any special startup code in the firmware, like
1335         // sending status messages to the LCD's.
1336
1337         if (loadedFirst) {
1338                 if (!iiWaitForTxEmpty(pB, MAX_DLOAD_START_TIME)) {
1339                         return II_DOWN_TIMEOUT;
1340                 }
1341         }
1342
1343         // Determine whether this was our last block!
1344         if (--(pB->i2eToLoad)) {
1345                 return II_DOWN_CONTINUING;    // more to come...
1346         }
1347
1348         // It WAS our last block: Clean up operations...
1349         // ...Wait for last buffer to drain from the board...
1350         if (!iiWaitForTxEmpty(pB, MAX_DLOAD_READ_TIME)) {
1351                 return II_DOWN_TIMEOUT;
1352         }
1353         // If there were only a single block written, this would come back
1354         // immediately and be harmless, though not strictly necessary.
1355         itemp = MAX_DLOAD_ACK_TIME/10;
1356         while (--itemp) {
1357                 if (HAS_INPUT(pB)) {
1358                         switch(BYTE_FROM(pB))
1359                         {
1360                         case LOADWARE_OK:
1361                                 pB->i2eState =
1362                                         isStandard ? II_STATE_STDLOADED :II_STATE_LOADED;
1363
1364                                 // Some revisions of the bootstrap firmware (e.g. ISA-8 1.0.2)
1365                                 // will, // if there is a debug port attached, require some
1366                                 // time to send information to the debug port now. It will do
1367                                 // this before // executing any of the code we just downloaded.
1368                                 // It may take up to 700 milliseconds.
1369                                 if (pB->i2ePom.e.porDiag2 & POR_DEBUG_PORT) {
1370                                         iiDelay(pB, 700);
1371                                 }
1372
1373                                 return II_DOWN_GOOD;
1374
1375                         case LOADWARE_BAD:
1376                         default:
1377                                 return II_DOWN_BAD;
1378                         }
1379                 }
1380
1381                 iiDelay(pB, 10);      // 10 mS granularity on checking condition
1382         }
1383
1384         // Drop-through --> timed out waiting for firmware confirmation
1385
1386         pB->i2eState = II_STATE_BADLOAD;
1387         return II_DOWN_TIMEOUT;
1388 }
1389
1390 //******************************************************************************
1391 // Function:   iiDownloadAll(pB, pSource, isStandard, size)
1392 // Parameters: pB         - pointer to board structure
1393 //             pSource    - loadware block to download
1394 //             isStandard - True if "standard" loadware, else false.
1395 //             size       - size of data to download (in bytes)
1396 //
1397 // Returns:    Success or Failure
1398 //
1399 // Description:
1400 //
1401 // Given a pointer to a board structure, a pointer to the beginning of some
1402 // loadware, whether it is considered the "standard loadware", and the size of
1403 // the array in bytes loads the entire array to the board as loadware.
1404 //
1405 // Assumes the board has been freshly reset and the power-up reset message read.
1406 // (i.e., in II_STATE_READY). Complains if state is bad, or if there seems to be
1407 // too much or too little data to load, or if iiDownloadBlock complains.
1408 //******************************************************************************
1409 static int
1410 iiDownloadAll(i2eBordStrPtr pB, loadHdrStrPtr pSource, int isStandard, int size)
1411 {
1412         int status;
1413
1414         // We know (from context) board should be ready for the first block of
1415         // download.  Complain if not.
1416         if (pB->i2eState != II_STATE_READY) return II_DOWN_BADSTATE;
1417
1418         while (size > 0) {
1419                 size -= LOADWARE_BLOCK_SIZE;    // How much data should there be left to
1420                                                                                 // load after the following operation ?
1421
1422                 // Note we just bump pSource by "one", because its size is actually that
1423                 // of an entire block, same as LOADWARE_BLOCK_SIZE.
1424                 status = iiDownloadBlock(pB, pSource++, isStandard);
1425
1426                 switch(status)
1427                 {
1428                 case II_DOWN_GOOD:
1429                         return ( (size > 0) ? II_DOWN_OVER : II_DOWN_GOOD);
1430
1431                 case II_DOWN_CONTINUING:
1432                         break;
1433
1434                 default:
1435                         return status;
1436                 }
1437         }
1438
1439         // We shouldn't drop out: it means "while" caught us with nothing left to
1440         // download, yet the previous DownloadBlock did not return complete. Ergo,
1441         // not enough data to match the size byte in the header.
1442         return II_DOWN_UNDER;
1443 }