Subversion Repositories shark

Rev

Rev 715 | Rev 1055 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
538 mauro 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
11
 *   Luca Abeni          <luca@gandalf.sssup.it>
1035 tullio 12
 *   Tullio Facchinetti  <tullio.facchinetti@unipv.it>
538 mauro 13
 *   (see the web pages for full authors list)
14
 *
15
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
16
 *
17
 * http://www.sssup.it
18
 * http://retis.sssup.it
19
 * http://shark.sssup.it
20
 */
21
 
22
/*
23
 * Copyright (C) 2000 Paolo Gai
24
 *
25
 * This program is free software; you can redistribute it and/or modify
26
 * it under the terms of the GNU General Public License as published by
27
 * the Free Software Foundation; either version 2 of the License, or
28
 * (at your option) any later version.
29
 *
30
 * This program is distributed in the hope that it will be useful,
31
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33
 * GNU General Public License for more details.
34
 *
35
 * You should have received a copy of the GNU General Public License
36
 * along with this program; if not, write to the Free Software
37
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
38
 *
39
 */
40
 
41
/* Serial communication device */
42
/* This implementation is capable of handling 4 distinct COM ports */
43
/* The COM port settings are the standard PC settings:             */
44
/* PORT         ADDRESS         IRQ                                */
45
/*   1           0x3F8           4                                 */
46
/*   2           0x2F8           3                                 */
47
/*   3           0x3E8           4                                 */
48
/*   4           0x2E8           2                                 */
49
 
50
/* By Massy
51
 * I have modified the fast handler routines to support serial
52
 * mouse better (see below)
53
 */
54
 
55
#include <kernel/kern.h>
56
#include <drivers/scom.h>
57
 
58
/* Base address for each standard COM link */
59
static unsigned com_base[] = {0x03F8,0x02F8,0x03E8,0x02E8};
60
 
61
/* Used for decoding the IIR status */
62
const int IIRbits[] = {MS_CHANGED,TX_EMPTY,RX_FULL,LS_CHANGED};
63
 
1035 tullio 64
int serial_initialized[4] = {0, 0, 0, 0};
538 mauro 65
 
1035 tullio 66
/**
67
 * Register level access functions.
68
 */
69
unsigned com_read(unsigned port, unsigned reg) {
538 mauro 70
    unsigned b;
1035 tullio 71
    if (port > 3 || reg > 7 || (!serial_initialized[port])) return(0);
538 mauro 72
    b = ll_in(com_base[port]+reg);
73
    return(b);
74
}
75
 
1035 tullio 76
void com_write(unsigned port, unsigned reg, unsigned value) {
77
    if (port > 3 || reg > 7 || (!serial_initialized[port])) return;
538 mauro 78
    ll_out(com_base[port]+reg,value);
79
}
80
 
1035 tullio 81
/**
82
 * Write the character "b" to the port "port".
83
 * If wait is 0 then a busy port makes the fuction to return immediately.
84
 * If wait is 1 loops until the port become free.
85
 * Return 0 if the byte has been sent. Return 1 otherwise.
86
 */
87
unsigned com_send(unsigned port, BYTE b, unsigned wait) {
88
    if (wait) {
89
      while ((com_read(port,LSR) & 32) == 0);
90
    } else {
91
      if ((com_read(port,LSR) & 32) == 0) return(1);
92
    }
538 mauro 93
    com_write(port,THR,b);
1035 tullio 94
    return(0);
538 mauro 95
}
96
 
1035 tullio 97
/**
98
 * Read a character from the port "port".
99
 * Polls the port until a character is ready to be read.
100
 */
101
unsigned com_receive(unsigned port) {
538 mauro 102
    while ((com_read(port,LSR) & 1) == 0);
103
    return(com_read(port,RBR));
104
}
105
 
1035 tullio 106
/**
107
 * Initialize a serial channel.
108
 * Do not do anything if the port has been already initialized.
109
 */
110
int com_open(unsigned port,DWORD speed,BYTE parity,BYTE len,BYTE stop) {
538 mauro 111
    unsigned long div,b_mask;
714 giacomo 112
    SYS_FLAGS f;        
113
 
114
    f = kern_fsave();
1035 tullio 115
 
116
    if (serial_initialized[port]) {
117
      kern_frestore(f);
118
      return(0);
119
    }
714 giacomo 120
 
538 mauro 121
    /* Now set up the serial link */
122
    b_mask = (parity & 3) * 8 + (stop & 1) * 4 + ((len - 5) & 3);
123
    div = 115200L / speed;
124
    /* Clear serial interrupt enable register */
125
    com_write(port,IER,0);
126
    /* Empty input buffer */
127
    com_read(port,RBR);
128
    /* Activate DLAB bit for speed setting */
129
    com_write(port,LCR,0x80);
130
    /* Load baud divisor */
131
    com_write(port,0,div & 0x00FF);
132
    div >>= 8;
133
    com_write(port,1,div & 0x00FF);
134
    /* Load control word (parity,stop bit,bit len) */
135
    com_write(port,LCR,b_mask);
136
    /* Attiva OUT1 & OUT2 */
137
    com_write(port,MCR,0x0C);
1035 tullio 138
 
139
    serial_initialized[port] = 1;
538 mauro 140
 
714 giacomo 141
    kern_frestore(f);
538 mauro 142
 
714 giacomo 143
    return(0);
144
 
538 mauro 145
}
146
 
1035 tullio 147
/**
148
 * Close port channel & release the server.
149
 */
538 mauro 150
int com_close(unsigned port)
151
{    
714 giacomo 152
    SYS_FLAGS f;
538 mauro 153
 
714 giacomo 154
    f = kern_fsave();
538 mauro 155
 
714 giacomo 156
    com_write(port,IER,0);
157
    com_read(port,IIR);
158
    com_read(port,RBR);
538 mauro 159
 
1035 tullio 160
    serial_initialized[port] = 0;
161
 
714 giacomo 162
    kern_frestore(f);
538 mauro 163
 
714 giacomo 164
    return(0);
538 mauro 165
}
166