/*
 * MICETEK International Inc., (shanghai) 
 * 	By Qinwei , 2003.05.28 (CopyRight)
 * 
 *  Version 0.1
 */
#include <stdio.h>
#include "uart_ev4510.h"


static unsigned int uart_base;
// static unsigned int SYS_CLK;
static int CurrBaud;

//dev_uart_init returns non-negative number on  succuess, 0 on failing.
int dev_uart_init (int sys_clock,  int reg_base, int ch)
{
        if (reg_base == 0)
                reg_base = 0x3ff0000;
        if (sys_clock == 0)
                sys_clock = 50000000;
        if (ch) /* If is channel 1*/
                uart_base = reg_base + 0xe000;
        else
                uart_base = reg_base + 0xd000;
	
        *((unsigned int *)(uart_base+ULCON)) =  0x00000003 |0x00000000| 0x00000000 |0x00000000 |0x00000040;
        //	WordLength | Parity | StopBit | IRDA_mode, and now 8N1 mode |we use external clock (UCLK) now;
	
        // for 33M UCLK, and 50M MCLK, see Sumsang manual 10-15(Page 369)
        *((unsigned int *)(uart_base+UBRDIV)) = B9600 << 4; // Now set to 9600, and can change later

        // Start channel
        /* Enable the UART in UCON and Unmask the RX interrupt!         */
#define	TxMode_Int			0x00000008    
#define	RxMode_Int			0x00000001
        *((unsigned int *)(uart_base+UCON))  |= (TxMode_Int | RxMode_Int) ; /* Turn on UART & Enable IRQs */ 
	
        CurrBaud = 9600;
        return 1;
}


//crtio returns the old baud rate on success, 0 on failing.
int crtio (int baud)
{
        int bbaud, ret ;
	
        ret = CurrBaud;
        switch (baud) {
        case 9600:
                bbaud = B9600;		
                CurrBaud  = 9600;
                break;
        case 19200:
                bbaud = B19200;	
                CurrBaud  = 19200;	
                break;
        case 38400:
                bbaud = B38400;	
                CurrBaud  = 38400;	
                break;
        case 57600:
                bbaud = B57600;	
                CurrBaud  = 57600;	
                break;
        case 115200:
                bbaud = B115200;
                CurrBaud  = 115200;
                break;
        default:
                bbaud = B9600;
                CurrBaud  = 9600;
                break;
        }
        *((unsigned int *)(uart_base+UBRDIV)) = bbaud << 4; // Now change the baudrate

        return ret;
}

//outbyte returns the written char on success or EOF on failing.
int outbyte (int ch)
{
        volatile unsigned int uart_status;

        while (1) {
                uart_status = (*((unsigned int *)(uart_base+USTAT)) )& 0xff;
                if (uart_status & (TH_empty ))
                        break;
        }

        *((unsigned int *)(uart_base+UTXBUF))  = ch; /*Send the char*/
	
        return ch;
}

//inbyte returns the byte it reads on success or EOF on failing.
int inbyte (void)
{
        unsigned int uart_status = (*((unsigned int *)(uart_base+USTAT)) )& 0xff;
	
        if (uart_status & RDV ) {
                unsigned int ch = (*((unsigned int *)(uart_base+URXBUF)) )& 0xFF;    /* get the data byte */
                return ch;
        }
	
        return EOF;
}


