Rev 716 | Rev 721 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
714 | giacomo | 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 | * (see the web pages for full authors list) |
||
11 | * |
||
12 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
13 | * |
||
14 | * http://www.sssup.it |
||
15 | * http://retis.sssup.it |
||
16 | * http://shark.sssup.it |
||
17 | */ |
||
18 | |||
19 | /* |
||
20 | * Copyright (C) 2000 Michele Cirinei |
||
21 | * |
||
22 | * This program is free software; you can redistribute it and/or modify |
||
23 | * it under the terms of the GNU General Public License as published by |
||
24 | * the Free Software Foundation; either version 2 of the License, or |
||
25 | * (at your option) any later version. |
||
26 | * |
||
27 | * This program is distributed in the hope that it will be useful, |
||
28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
30 | * GNU General Public License for more details. |
||
31 | * |
||
32 | * You should have received a copy of the GNU General Public License |
||
33 | * along with this program; if not, write to the Free Software |
||
34 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
35 | */ |
||
36 | |||
37 | #include <kernel/kern.h> |
||
38 | #include <drivers/scomirq.h> |
||
39 | |||
40 | /**************************** |
||
41 | Variables |
||
42 | ****************************/ |
||
43 | static unsigned com_irq[] = {COM1_IRQ, COM2_IRQ, COM3_IRQ, COM4_IRQ}; |
||
44 | |||
45 | struct SCom_Access_Type SCom_Access; |
||
46 | |||
47 | unsigned SCom_Error; |
||
48 | |||
49 | struct TX_data_t{ |
||
50 | int ptr; |
||
51 | int len; |
||
52 | BYTE buf[100]; |
||
53 | }; |
||
54 | |||
55 | static struct TX_data_t SCom_TX_data; |
||
56 | |||
57 | /***************************** |
||
58 | * Functions |
||
59 | *****************************/ |
||
60 | |||
61 | void RBRF_handler(unsigned port); |
||
62 | |||
63 | void THRE_handler(unsigned port); |
||
64 | |||
65 | void LSR_handler(unsigned port); |
||
66 | |||
67 | void com_send_msg(unsigned port, BYTE len, BYTE *m); |
||
68 | |||
69 | void com_irq_sel(int no) |
||
70 | { |
||
71 | BYTE b; |
||
72 | unsigned i; |
||
73 | |||
718 | giacomo | 74 | cprintf("(IRQ:%d)",no); |
75 | |||
714 | giacomo | 76 | for(i=0;i<4;i++) |
77 | if (com_irq[i] == no) break; |
||
78 | |||
79 | b = DECODE(com_read(i, IIR)); |
||
80 | |||
81 | switch (b) |
||
82 | { |
||
83 | case LS_CHANGED: |
||
84 | LSR_handler(i); |
||
85 | break; |
||
86 | |||
87 | case RX_FULL: |
||
88 | RBRF_handler(i); |
||
89 | break; |
||
90 | |||
91 | case TX_EMPTY: |
||
92 | THRE_handler(i); |
||
93 | break; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | void com_irq_enable(unsigned port, unsigned irq) |
||
98 | { |
||
99 | SYS_FLAGS f; |
||
100 | |||
101 | f = kern_fsave(); |
||
102 | |||
103 | com_read(port, LSR); |
||
104 | com_read(port, IIR); |
||
105 | com_write(port, IER, com_read(port, IER) | irq); /* Enable irq */ |
||
106 | |||
107 | kern_frestore(f); |
||
108 | |||
109 | } |
||
110 | |||
111 | void com_irq_disable(unsigned port, unsigned irq) |
||
112 | { |
||
113 | |||
114 | SYS_FLAGS f; |
||
115 | |||
116 | f = kern_fsave(); |
||
117 | |||
118 | com_read(port, LSR); |
||
119 | com_read(port, IIR); |
||
120 | com_write(port, IER, com_read(port, IER) & ~irq); /* Disable irq */ |
||
121 | |||
122 | kern_frestore(f); |
||
123 | |||
124 | } |
||
125 | |||
716 | giacomo | 126 | void com_close_irq(unsigned port) |
714 | giacomo | 127 | { |
128 | SYS_FLAGS f; |
||
129 | |||
130 | f = kern_fsave(); |
||
131 | |||
132 | com_write(port, IER, 0); |
||
133 | com_read(port, LSR); |
||
134 | com_read(port, IIR); |
||
135 | |||
716 | giacomo | 136 | handler_remove(com_irq[port]); |
137 | |||
714 | giacomo | 138 | kern_frestore(f); |
139 | |||
140 | } |
||
141 | |||
716 | giacomo | 142 | void com_init_irq(unsigned port) |
714 | giacomo | 143 | { |
144 | SYS_FLAGS f; |
||
145 | |||
146 | f = kern_fsave(); |
||
147 | |||
148 | SCom_Error = 0; |
||
149 | handler_set(com_irq[port], com_irq_sel, NIL, TRUE); |
||
150 | com_irq_disable(port, ALL_IRQ); /* Disable interrupts */ |
||
151 | |||
152 | kern_frestore(f); |
||
153 | |||
154 | } |
||
155 | |||
156 | void com_set_functions(void (*confirm)(unsigned port, BYTE msg_status),void (*indication)(unsigned port, BYTE data)) |
||
157 | { |
||
158 | SYS_FLAGS f; |
||
159 | |||
160 | f = kern_fsave(); |
||
161 | |||
162 | SCom_Access.confirm = confirm; |
||
163 | SCom_Access.request = com_send_msg; |
||
164 | SCom_Access.indication = indication; |
||
165 | |||
166 | kern_frestore(f); |
||
167 | |||
168 | } |
||
169 | |||
170 | void com_send_msg(unsigned port, BYTE len, BYTE *m) |
||
171 | { |
||
172 | SYS_FLAGS f; |
||
173 | |||
718 | giacomo | 174 | cprintf("(SEND:COM%d:%d)",port,len); |
175 | |||
714 | giacomo | 176 | f = kern_fsave(); |
177 | |||
178 | com_irq_disable(port, ALL_IRQ); /* Disable interrupts */ |
||
179 | |||
180 | /* Transmit first byte of message */ |
||
181 | SCom_TX_data.ptr = 0; |
||
182 | SCom_TX_data.len = len; |
||
183 | if (len < 100) { |
||
184 | memcpy(SCom_TX_data.buf, m, len); |
||
185 | com_write(port, THR, SCom_TX_data.buf[SCom_TX_data.ptr++]); |
||
186 | com_irq_enable(port, THRE_IRQ); /* Enable THRE */ |
||
187 | } |
||
188 | |||
189 | kern_frestore(f); |
||
190 | |||
191 | } |
||
192 | |||
716 | giacomo | 193 | __inline__ void com_irq_send(unsigned port, BYTE len, BYTE *m) { |
194 | |||
195 | com_send_msg(port, len, m); |
||
196 | |||
197 | } |
||
198 | |||
714 | giacomo | 199 | void THRE_handler (unsigned port) /* Transmit Data Register Ready for next byte */ |
200 | { |
||
201 | if (SCom_TX_data.ptr < SCom_TX_data.len) |
||
202 | com_write(port, THR, SCom_TX_data.buf[SCom_TX_data.ptr++]); |
||
203 | else |
||
204 | { |
||
205 | com_irq_disable(port, THRE_IRQ); |
||
206 | while((com_read(port,LSR) & bit6) == 0); /* Wait until last byte sent */ |
||
207 | SCom_Access.confirm(port, COM_OK); |
||
208 | } |
||
209 | } |
||
210 | |||
211 | void RBRF_handler (unsigned port) /* Receive Data Register Full */ |
||
212 | { |
||
213 | SCom_Access.indication(port, com_read(port, RBR)); |
||
214 | } |
||
215 | |||
216 | void LSR_handler (unsigned port) /* break = 8, frame = 4, parity = 2, RBR overrun = 1*/ |
||
217 | { |
||
218 | SCom_Error = (com_read(port, LSR) & (bit4|bit3|bit2|bit1) ) >> 1; |
||
219 | com_irq_disable(port, ALL_IRQ); /* Disable all interrupts */ |
||
220 | SCom_Access.confirm(port, COM_ERROR); |
||
221 | } |
||
222 |