Move variables from assembler to C code.
[seabios.git] / src / serial.c
1 // 16bit code to handle serial and printer services.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "biosvar.h" // SET_BDA
9 #include "util.h" // debug_enter
10 #include "bregs.h" // struct bregs
11
12
13 /****************************************************************
14  * COM ports
15  ****************************************************************/
16
17 static u16
18 detect_serial(u16 port, u8 timeout, u8 count)
19 {
20     outb(0x02, port+1);
21     if (inb(port+1) != 0x02)
22         return 0;
23     if (inb(port+2) != 0x02)
24         return 0;
25     outb(0x00, port+1);
26     SET_BDA(port_com[count], port);
27     SET_BDA(com_timeout[count], timeout);
28     return 1;
29 }
30
31 void
32 serial_setup()
33 {
34     if (! CONFIG_SERIAL)
35         return;
36     dprintf(3, "init serial\n");
37
38     u16 count = 0;
39     count += detect_serial(0x3f8, 0x0a, count);
40     count += detect_serial(0x2f8, 0x0a, count);
41     count += detect_serial(0x3e8, 0x0a, count);
42     count += detect_serial(0x2e8, 0x0a, count);
43
44     // Equipment word bits 9..11 determing # serial ports
45     u16 eqb = GET_BDA(equipment_list_flags);
46     SET_BDA(equipment_list_flags, (eqb & 0xf1ff) | (count << 9));
47 }
48
49 static u16
50 getComAddr(struct bregs *regs)
51 {
52     if (regs->dx >= 4) {
53         set_fail(regs);
54         return 0;
55     }
56     u16 addr = GET_BDA(port_com[regs->dx]);
57     if (! addr)
58         set_fail(regs);
59     return addr;
60 }
61
62 // SERIAL - INITIALIZE PORT
63 static void
64 handle_1400(struct bregs *regs)
65 {
66     u16 addr = getComAddr(regs);
67     if (!addr)
68         return;
69     outb(inb(addr+3) | 0x80, addr+3);
70     if ((regs->al & 0xE0) == 0) {
71         outb(0x17, addr);
72         outb(0x04, addr+1);
73     } else {
74         u16 val16 = 0x600 >> ((regs->al & 0xE0) >> 5);
75         outb(val16 & 0xFF, addr);
76         outb(val16 >> 8, addr+1);
77     }
78     outb(regs->al & 0x1F, addr+3);
79     regs->ah = inb(addr+5);
80     regs->al = inb(addr+6);
81     set_success(regs);
82 }
83
84 // SERIAL - WRITE CHARACTER TO PORT
85 static void
86 handle_1401(struct bregs *regs)
87 {
88     u16 addr = getComAddr(regs);
89     if (!addr)
90         return;
91     u16 timer = GET_BDA(timer_counter);
92     u16 timeout = GET_BDA(com_timeout[regs->dx]);
93     while (((inb(addr+5) & 0x60) != 0x60) && (timeout)) {
94         u16 val16 = GET_BDA(timer_counter);
95         if (val16 != timer) {
96             timer = val16;
97             timeout--;
98         }
99     }
100     if (timeout)
101         outb(regs->al, addr);
102     regs->ah = inb(addr+5);
103     if (!timeout)
104         regs->ah |= 0x80;
105     set_success(regs);
106 }
107
108 // SERIAL - READ CHARACTER FROM PORT
109 static void
110 handle_1402(struct bregs *regs)
111 {
112     u16 addr = getComAddr(regs);
113     if (!addr)
114         return;
115     u16 timer = GET_BDA(timer_counter);
116     u16 timeout = GET_BDA(com_timeout[regs->dx]);
117     while (((inb(addr+5) & 0x01) == 0) && (timeout)) {
118         u16 val16 = GET_BDA(timer_counter);
119         if (val16 != timer) {
120             timer = val16;
121             timeout--;
122         }
123     }
124     if (timeout) {
125         regs->ah = 0;
126         regs->al = inb(addr);
127     } else {
128         regs->ah = inb(addr+5);
129     }
130     set_success(regs);
131 }
132
133 // SERIAL - GET PORT STATUS
134 static void
135 handle_1403(struct bregs *regs)
136 {
137     u16 addr = getComAddr(regs);
138     if (!addr)
139         return;
140     regs->ah = inb(addr+5);
141     regs->al = inb(addr+6);
142     set_success(regs);
143 }
144
145 static void
146 handle_14XX(struct bregs *regs)
147 {
148     // Unsupported
149     set_fail(regs);
150 }
151
152 // INT 14h Serial Communications Service Entry Point
153 void VISIBLE16
154 handle_14(struct bregs *regs)
155 {
156     debug_enter(regs, DEBUG_HDL_14);
157     if (! CONFIG_SERIAL) {
158         handle_14XX(regs);
159         return;
160     }
161
162     irq_enable();
163
164     switch (regs->ah) {
165     case 0x00: handle_1400(regs); break;
166     case 0x01: handle_1401(regs); break;
167     case 0x02: handle_1402(regs); break;
168     case 0x03: handle_1403(regs); break;
169     default:   handle_14XX(regs); break;
170     }
171 }
172
173 // XXX - Baud Rate Generator Table
174 u8 BaudTable[16] VAR16FIXED(0xe729);
175
176
177 /****************************************************************
178  * LPT ports
179  ****************************************************************/
180
181 static u16
182 detect_parport(u16 port, u8 timeout, u8 count)
183 {
184     // clear input mode
185     outb(inb(port+2) & 0xdf, port+2);
186
187     outb(0xaa, port);
188     if (inb(port) != 0xaa)
189         // Not present
190         return 0;
191     SET_BDA(port_lpt[count], port);
192     SET_BDA(lpt_timeout[count], timeout);
193     return 1;
194 }
195
196 void
197 lpt_setup()
198 {
199     if (! CONFIG_LPT)
200         return;
201     dprintf(3, "init lpt\n");
202
203     u16 count = 0;
204     count += detect_parport(0x378, 0x14, count);
205     count += detect_parport(0x278, 0x14, count);
206
207     // Equipment word bits 14..15 determing # parallel ports
208     u16 eqb = GET_BDA(equipment_list_flags);
209     SET_BDA(equipment_list_flags, (eqb & 0x3fff) | (count << 14));
210 }
211
212 static u16
213 getLptAddr(struct bregs *regs)
214 {
215     if (regs->dx >= 3) {
216         set_fail(regs);
217         return 0;
218     }
219     u16 addr = GET_BDA(port_lpt[regs->dx]);
220     if (! addr)
221         set_fail(regs);
222     return addr;
223 }
224
225 static void
226 lpt_ret(struct bregs *regs, u16 addr, u16 timeout)
227 {
228     u8 val8 = inb(addr+1);
229     regs->ah = (val8 ^ 0x48);
230     if (!timeout)
231         regs->ah |= 0x01;
232     set_success(regs);
233 }
234
235 // INT 17 - PRINTER - WRITE CHARACTER
236 static void
237 handle_1700(struct bregs *regs)
238 {
239     u16 addr = getLptAddr(regs);
240     if (!addr)
241         return;
242     u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
243
244     outb(regs->al, addr);
245     u8 val8 = inb(addr+2);
246     outb(val8 | 0x01, addr+2); // send strobe
247     nop();
248     outb(val8 & ~0x01, addr+2);
249     // XXX - implement better timeout code.
250     while (((inb(addr+1) & 0x40) == 0x40) && (timeout))
251         timeout--;
252
253     lpt_ret(regs, addr, timeout);
254 }
255
256 // INT 17 - PRINTER - INITIALIZE PORT
257 static void
258 handle_1701(struct bregs *regs)
259 {
260     u16 addr = getLptAddr(regs);
261     if (!addr)
262         return;
263     u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
264
265     u8 val8 = inb(addr+2);
266     outb(val8 & ~0x04, addr+2); // send init
267     nop();
268     outb(val8 | 0x04, addr+2);
269
270     lpt_ret(regs, addr, timeout);
271 }
272
273 // INT 17 - PRINTER - GET STATUS
274 static void
275 handle_1702(struct bregs *regs)
276 {
277     u16 addr = getLptAddr(regs);
278     if (!addr)
279         return;
280     u16 timeout = GET_BDA(lpt_timeout[regs->dx]) << 8;
281
282     lpt_ret(regs, addr, timeout);
283 }
284
285 static void
286 handle_17XX(struct bregs *regs)
287 {
288     // Unsupported
289     set_fail(regs);
290 }
291
292 // INT17h : Printer Service Entry Point
293 void VISIBLE16
294 handle_17(struct bregs *regs)
295 {
296     debug_enter(regs, DEBUG_HDL_17);
297     if (! CONFIG_LPT) {
298         handle_17XX(regs);
299         return;
300     }
301
302     irq_enable();
303
304     switch (regs->ah) {
305     case 0x00: handle_1700(regs); break;
306     case 0x01: handle_1701(regs); break;
307     case 0x02: handle_1702(regs); break;
308     default:   handle_17XX(regs); break;
309     }
310 }