/*********************************************************************** 
* NOTICE
* All files contained on this disk are subject to the licensing conditions
* issued by MOTOROLA Inc.
*
* All files are copyright 1993 by MOTOROLA Inc. 
************************************************************************/


/********************************************************
 * File:	uart_r.c
 *
 * Description:
 *	The driver routines for handling receive
 *	on an UART port.
 *
 * Routines:
 *	uart_rxfr
 *	uart_busy
 *	uart_rxbuf
 *
 * Author:
 *	Jonathan Masel
 ********************************************************/


#include "gct.h"
#include "types.h"
#include "mtypes.h"
#include "msg.h"
#include "uart.h"
#include "bss.h"
#include "quicc.h"
#include "status.h"


extern driver_trace(int type, int id, volatile int par);


/********************************************************
 * routine:	uart_rxfr
 *
 * description:
 *	This is reached after being sent a RX_IND message
 *	by the interrupt routine (interrupt on QUICC
 *	receiving a complete UART frame).
 *	This is assumed to have occured after a
 *	non-rejected control character is received
 *	(this generates a UART_CCR interrupt). Note that
 *	this is only of relevance in the message (frame)
 *	oriented mode of operation: when the driver module
 *	is functioning in a character oriented mode of
 *	operation, the uart_rxbuf routine will be called
 *	for each buffer of data received.
 *	The receive buffer table is flushed of all UART
 *	frames received: these frames are sent to the
 *	port's upper layer.
 *	The RX_IND message sent is only a 32-bit code (not an
 *	allocated MSG structure) and therefore must
 *	not be relm-ed. Its structure corresponds to
 *	the first 32-bits of a normal HDR structure.
 *
 * arguments:
 *	h		the event message received
 *	child		points to the child structure
 *
 * return code:
 *
 * side effects:
 *	Advance quicc_rbd each time a
 *	received buffer is collected.
 *	The ccr interrupt must be re-enabled after
 *	all received messages have been collected.
 *
 ********************************************************/
uart_rxfr(h, child)
HDR *h;
CHILD *child;
{
	GCT *gct;
	register WORK_AREA *wa;
	register unsigned short status;		/* status of BD */
	register R_FRAME *r1, *r2, *r3;
			/* r1 = first r_frame in frame */
			/* r2 = current r_frame */
			/* r3 temporary */
	struct quicc_bd *nextBD;	/* next BD in frame */
	int flen;		/* accumulative frame length */
	MSG *m;
	int eofr;	/* boolean: end of frame recognized */
	int bcount;	/* number of BDs in this frame */
	extern MSG *getm();

	GETGCT(gct);
	wa = (WORK_AREA *)child->work_area;

	/*
	 * clear event bit
	 * this clears indications of old (masked) events
	 */
	wa->regs->scc_scce = (UART_RX | UART_BUSY);

	/*
	 * if receive is disabled,
	 * return immediately
	 */
	if( child->flags & RX_DISABLE )
		if( child->flags & RX_ACTIVATE )
			child->flags &= ~RX_ACTIVATE;
		else
			return;

	if( !wa->rx_q->nextb ){
		wa->regs->scc_sccm |= UART_RX;
		return;
	}

	/*
	 * rbd address next BD for receive
	 */
	nextBD = wa->quicc_rbd;
	status = nextBD->status;
	r1 = r2 = wa->rx_q;
	flen = 0;
	bcount = 0;
	eofr = 0;
	while( !(status & R_E) ){
		/*
		 * first check that r2 has been
		 * linked to the rx BD table
		 */
		if( !(r2->flags & LINKED_TO_TABLE) )
			break;

		bcount++;

		/*
		 * update accumulative frame length
		 * on the last BD in a frame, the length
		 * is the length of the entire UART frame.
		 */
		r2->blen = nextBD->length;
		flen += nextBD->length;

		/*
		 * if end of frame ...
		 * this means anything except an
		 * address character buffer
		 * or anything terminated by an error
		 */
		if( (status & R_ERROR) || !(status & R_A) ){
			/*
			 * update status and length
			 * in new received frame,
			 * zero accumulative length
			 * for next frame
			 */
			r1->flen = flen;	/* frame total length */
			r3 = r2->nextb;	/* for next frame */
			r2->nextb = 0;	/* no more buffs in this frame */

			/*
			 * prepare for next R_FRAME
			 */
			r2 = r3;	/* next R_FRAME for rx */
			flen = 0;

			driver_trace(RX_IND, r1->hdr.id, (int)r1);
			/*
			 * pass r_frame to upper layer
			 * (subject to error mask).
			 * buffers are linked via nextb
			 */
			if( status & R_ERROR ){
				driver_trace(RX_ERROR, r1->hdr.id, (int)nextBD);
				r1->hdr.status = RECEIVE_ERROR;
				r1->hdr.err_info = status & R_ERROR;
				wa->rerr_cnt++;
				if( (status&R_ERROR) & ~wa->rxerr_mask )
					return_to_pool(r1, child);
				else
					if( (*gct->send)(child->upper, r1) )
						return_to_pool(r1, child);
			} else
				if( (*gct->send)(child->upper, r1) )
					return_to_pool(r1, child);
			r1 = r2;
			wa->rcount++;
			eofr = 1;
		} else
			r2 = r2->nextb;	/* next R_FRAME for rx */

		/*
		 * advance rbd (to next Rx BD in table)
		 */
		if( status & R_W )
			nextBD = RBD_ADDR(wa);
		else
			nextBD++;

		if( eofr ){
			wa->rfree += bcount;
			bcount = eofr = 0;
			wa->quicc_rbd = nextBD;
		}

		status = nextBD->status;	/* update status */
	}

	wa->rx_q = r1;

	/*
	 * replenish the rx BD table
	 */
	status = replenish_rxbd(child);
	if( status ){
		driver_trace(BUSY_IND, h->id, status);
		m = getm(BUSY_IND, h->id, 0, 0);
		if( m ){
			m->hdr.status = status;
			if( (*gct->send)( child->upper, m) )
				relm((HDR *)m);
		}
	}

	/*
	 * re-enable interrupts
	 * setting the mask will result in an interrupt
	 * if any frames were received during the time
	 * the mask was zero. therefore, frames will not
	 * go unnoticed if received after exiting the while
	 * loop above and before the mask bit is set.
	 */
	wa->regs->scc_sccm |= UART_RX;
	if( !(child->flags & LOCAL_BUSY) )
		wa->regs->scc_sccm |= UART_BUSY;
}



/********************************************************
 * routine:	uart_rxbuf
 *
 * description:
 *	This routine is called upon reception of
 *	a RX_IND message. The message is sent from
 *	the UART interrupt routine when a receive buffer
 *	has been filled with data in the
 *	character oriented mode of operation.
 *	Note that the event message received is only
 *	a 32-bit value (not a pointer to a MSG structure
 *	which can be released).
 *	In order to handle each of the characters received,
 *	the rxchar routine is called (via the GCT).
 *
 * arguments:
 *	h		the event message received
 *	child		points to the child structure
 *
 * return code:
 *
 * side effects:
 *
 ********************************************************/
uart_rxbuf(h, child)
register HDR *h;
CHILD *child;
{
	GCT *gct;
	register int i;
	register unsigned char *s;
	register WORK_AREA *wa;
	register struct quicc_bd *nextBD;		/* next BD */
	struct quicc_bd *bd;			/* temp */

	GETGCT(gct);
	wa = (WORK_AREA *)child->work_area;

	/*
	 * clear event bit
	 * this clears indications of old (masked) events
	 */
	wa->regs->scc_scce = (UART_RX | UART_BUSY);

	/*
	 * if receive is disabled,
	 * return immediately
	 */
	if( child->flags & RX_DISABLE )
		if( child->flags & RX_ACTIVATE )
			child->flags &= ~RX_ACTIVATE;
		else
			return;

	/*
	 * rbd address next BD for receive
	 */
	nextBD = wa->quicc_rbd;
	while( !(nextBD->status & R_E) ){
		if( !(nextBD->status & R_ERROR) ){
			/*
			 * handle all characters in this buffer
			 */
			i = nextBD->length;
			s = nextBD->buf;
			if( gct->rxchar )
				while( i-- )
					(*gct->rxchar)(*s++);
		}
		else
			driver_trace(RX_ERROR, child->key, (int)nextBD);

		/*
		 * advance rbd (to next Rx BD in table)
		 */
		bd = nextBD;
		if( nextBD->status & R_W )
			nextBD = RBD_ADDR(wa);
		else
			nextBD++;
		wa->quicc_rbd = nextBD;

		/*
		 * mark used BD as empty
		 * (available for use by QUICC)
		 * without destroying the wrap
		 * or external bits.
		 */
		bd->status &= R_W;
		bd->status |= (R_I | R_E);
	}

	/*
	 * re-enable interrupts
	 * setting the mask will result in an interrupt
	 * if any frames were received during the time
	 * the mask was zero. therefore, events will not
	 * go unnoticed if they occur after exiting the while
	 * loop above and before the mask bit is set.
	 */
	wa->regs->scc_sccm |= UART_RX;
	if( !(child->flags & LOCAL_BUSY) )
		wa->regs->scc_sccm |= UART_BUSY;
}

uart_busy()
{
}
