Subversion Repositories shark

Rev

Rev 714 | Rev 1035 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Project: S.Ha.R.K.
 *
 * Coordinators:
 *   Giorgio Buttazzo    <giorgio@sssup.it>
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *
 * Authors     :
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
 *   Luca Abeni          <luca@gandalf.sssup.it>
 *   (see the web pages for full authors list)
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://shark.sssup.it
 */


/*
 * Copyright (C) 2000 Paolo Gai
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


/* Serial communication device */
/* This implementation is capable of handling 4 distinct COM ports */
/* The COM port settings are the standard PC settings:             */
/* PORT         ADDRESS         IRQ                                */
/*   1           0x3F8           4                                 */
/*   2           0x2F8           3                                 */
/*   3           0x3E8           4                                 */
/*   4           0x2E8           2                                 */

/* By Massy
 * I have modified the fast handler routines to support serial
 * mouse better (see below)
 */


#include <kernel/kern.h>
#include <drivers/scom.h>

/* Base address for each standard COM link */
static unsigned com_base[] = {0x03F8,0x02F8,0x03E8,0x02E8};

/* Used for decoding the IIR status */
const int IIRbits[] = {MS_CHANGED,TX_EMPTY,RX_FULL,LS_CHANGED};

/* Register level access functions */

unsigned com_read(unsigned port, unsigned reg)
{
    unsigned b;
    if (port > 3 || reg > 7) return(0);
    b = ll_in(com_base[port]+reg);
    return(b);
}

void com_write(unsigned port, unsigned reg, unsigned value)
{
    if (port > 3 || reg > 7) return;
    ll_out(com_base[port]+reg,value);
}

/* Polled send/receive */

unsigned com_send(unsigned port, BYTE b)
{
    while ((com_read(port,LSR) & 32) == 0);
    com_write(port,THR,b);
    return 0;
}

unsigned com_receive(unsigned port)
{
    while ((com_read(port,LSR) & 1) == 0);
    return(com_read(port,RBR));
}

/* Initialize a serial channel */

int com_open(unsigned port,DWORD speed,BYTE parity,BYTE len,BYTE stop)
{
    unsigned long div,b_mask;
    SYS_FLAGS f;        

    f = kern_fsave();

    /* Now set up the serial link */
    b_mask = (parity & 3) * 8 + (stop & 1) * 4 + ((len - 5) & 3);
    div = 115200L / speed;
    /* Clear serial interrupt enable register */
    com_write(port,IER,0);
    /* Empty input buffer */
    com_read(port,RBR);
    /* Activate DLAB bit for speed setting */
    com_write(port,LCR,0x80);
    /* Load baud divisor */
    com_write(port,0,div & 0x00FF);
    div >>= 8;
    com_write(port,1,div & 0x00FF);
    /* Load control word (parity,stop bit,bit len) */
    com_write(port,LCR,b_mask);
    /* Attiva OUT1 & OUT2 */
    com_write(port,MCR,0x0C);

    kern_frestore(f);

    return(0);

}

/* Close port channel & release the server */

int com_close(unsigned port)
{    
    SYS_FLAGS f;

    f = kern_fsave();

    com_write(port,IER,0);
    com_read(port,IIR);
    com_read(port,RBR);
   
    kern_frestore(f);

    return(0);
}