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