Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / src / serial.c
1 // 16bit code to handle serial and printer services.
2 //
3 // Copyright (C) 2008,2009  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 "bregs.h" // struct bregs
10 #include "hw/serialio.h" // SEROFF_IER
11 #include "output.h" // debug_enter
12 #include "romfile.h" // romfile_loadint
13 #include "stacks.h" // yield
14 #include "util.h" // serial_setup
15
16
17 /****************************************************************
18  * COM ports
19  ****************************************************************/
20
21 static u16
22 detect_serial(u16 port, u8 timeout, u8 count)
23 {
24     if (CONFIG_DEBUG_SERIAL && port == CONFIG_DEBUG_SERIAL_PORT
25         && !romfile_loadint("etc/advertise-serial-debug-port", 1))
26         return 0;
27     outb(0x02, port+SEROFF_IER);
28     u8 ier = inb(port+SEROFF_IER);
29     if (ier != 0x02)
30         return 0;
31     u8 iir = inb(port+SEROFF_IIR);
32     if ((iir & 0x3f) != 0x02)
33         return 0;
34
35     outb(0x00, port+SEROFF_IER);
36     SET_BDA(port_com[count], port);
37     SET_BDA(com_timeout[count], timeout);
38     return 1;
39 }
40
41 void
42 serial_setup(void)
43 {
44     if (! CONFIG_SERIAL)
45         return;
46     dprintf(3, "init serial\n");
47
48     u16 count = 0;
49     count += detect_serial(PORT_SERIAL1, 0x0a, count);
50     count += detect_serial(PORT_SERIAL2, 0x0a, count);
51     count += detect_serial(PORT_SERIAL3, 0x0a, count);
52     count += detect_serial(PORT_SERIAL4, 0x0a, count);
53     dprintf(1, "Found %d serial ports\n", count);
54
55     // Equipment word bits 9..11 determing # serial ports
56     set_equipment_flags(0xe00, count << 9);
57 }
58
59 static u16
60 getComAddr(struct bregs *regs)
61 {
62     if (regs->dx >= 4) {
63         set_invalid(regs);
64         return 0;
65     }
66     u16 addr = GET_BDA(port_com[regs->dx]);
67     if (! addr)
68         set_invalid(regs);
69     return addr;
70 }
71
72 // SERIAL - INITIALIZE PORT
73 static void
74 handle_1400(struct bregs *regs)
75 {
76     u16 addr = getComAddr(regs);
77     if (!addr)
78         return;
79     outb(inb(addr+SEROFF_LCR) | 0x80, addr+SEROFF_LCR);
80     if ((regs->al & 0xE0) == 0) {
81         outb(0x17, addr+SEROFF_DLL);
82         outb(0x04, addr+SEROFF_DLH);
83     } else {
84         u16 val16 = 0x600 >> ((regs->al & 0xE0) >> 5);
85         outb(val16 & 0xFF, addr+SEROFF_DLL);
86         outb(val16 >> 8, addr+SEROFF_DLH);
87     }
88     outb(regs->al & 0x1F, addr+SEROFF_LCR);
89     regs->ah = inb(addr+SEROFF_LSR);
90     regs->al = inb(addr+SEROFF_MSR);
91     set_success(regs);
92 }
93
94 // SERIAL - WRITE CHARACTER TO PORT
95 static void
96 handle_1401(struct bregs *regs)
97 {
98     u16 addr = getComAddr(regs);
99     if (!addr)
100         return;
101     u32 end = irqtimer_calc_ticks(GET_BDA(com_timeout[regs->dx]));
102     for (;;) {
103         u8 lsr = inb(addr+SEROFF_LSR);
104         if ((lsr & 0x60) == 0x60) {
105             // Success - can write data
106             outb(regs->al, addr+SEROFF_DATA);
107             // XXX - reread lsr?
108             regs->ah = lsr;
109             break;
110         }
111         if (irqtimer_check(end)) {
112             // Timed out - can't write data.
113             regs->ah = lsr | 0x80;
114             break;
115         }
116         yield();
117     }
118     set_success(regs);
119 }
120
121 // SERIAL - READ CHARACTER FROM PORT
122 static void
123 handle_1402(struct bregs *regs)
124 {
125     u16 addr = getComAddr(regs);
126     if (!addr)
127         return;
128     u32 end = irqtimer_calc_ticks(GET_BDA(com_timeout[regs->dx]));
129     for (;;) {
130         u8 lsr = inb(addr+SEROFF_LSR);
131         if (lsr & 0x01) {
132             // Success - can read data
133             regs->al = inb(addr+SEROFF_DATA);
134             regs->ah = lsr;
135             break;
136         }
137         if (irqtimer_check(end)) {
138             // Timed out - can't read data.
139             regs->ah = lsr | 0x80;
140             break;
141         }
142         yield();
143     }
144     set_success(regs);
145 }
146
147 // SERIAL - GET PORT STATUS
148 static void
149 handle_1403(struct bregs *regs)
150 {
151     u16 addr = getComAddr(regs);
152     if (!addr)
153         return;
154     regs->ah = inb(addr+SEROFF_LSR);
155     regs->al = inb(addr+SEROFF_MSR);
156     set_success(regs);
157 }
158
159 static void
160 handle_14XX(struct bregs *regs)
161 {
162     set_unimplemented(regs);
163 }
164
165 // INT 14h Serial Communications Service Entry Point
166 void VISIBLE16
167 handle_14(struct bregs *regs)
168 {
169     debug_enter(regs, DEBUG_HDL_14);
170     if (! CONFIG_SERIAL) {
171         handle_14XX(regs);
172         return;
173     }
174
175     switch (regs->ah) {
176     case 0x00: handle_1400(regs); break;
177     case 0x01: handle_1401(regs); break;
178     case 0x02: handle_1402(regs); break;
179     case 0x03: handle_1403(regs); break;
180     default:   handle_14XX(regs); break;
181     }
182 }
183
184
185 /****************************************************************
186  * LPT ports
187  ****************************************************************/
188
189 static u16
190 detect_parport(u16 port, u8 timeout, u8 count)
191 {
192     // clear input mode
193     outb(inb(port+2) & 0xdf, port+2);
194
195     outb(0xaa, port);
196     if (inb(port) != 0xaa)
197         // Not present
198         return 0;
199     SET_BDA(port_lpt[count], port);
200     SET_BDA(lpt_timeout[count], timeout);
201     return 1;
202 }
203
204 void
205 lpt_setup(void)
206 {
207     if (! CONFIG_LPT)
208         return;
209     dprintf(3, "init lpt\n");
210
211     u16 count = 0;
212     count += detect_parport(PORT_LPT1, 0x14, count);
213     count += detect_parport(PORT_LPT2, 0x14, count);
214     dprintf(1, "Found %d lpt ports\n", count);
215
216     // Equipment word bits 14..15 determing # parallel ports
217     set_equipment_flags(0xc000, count << 14);
218 }
219
220 static u16
221 getLptAddr(struct bregs *regs)
222 {
223     if (regs->dx >= 3) {
224         set_invalid(regs);
225         return 0;
226     }
227     u16 addr = GET_BDA(port_lpt[regs->dx]);
228     if (! addr)
229         set_invalid(regs);
230     return addr;
231 }
232
233 // INT 17 - PRINTER - WRITE CHARACTER
234 static void
235 handle_1700(struct bregs *regs)
236 {
237     u16 addr = getLptAddr(regs);
238     if (!addr)
239         return;
240
241     u32 end = irqtimer_calc_ticks(GET_BDA(lpt_timeout[regs->dx]));
242
243     outb(regs->al, addr);
244     u8 val8 = inb(addr+2);
245     outb(val8 | 0x01, addr+2); // send strobe
246     udelay(5);
247     outb(val8 & ~0x01, addr+2);
248
249     for (;;) {
250         u8 v = inb(addr+1);
251         if (!(v & 0x40)) {
252             // Success
253             regs->ah = v ^ 0x48;
254             break;
255         }
256         if (irqtimer_check(end)) {
257             // Timeout
258             regs->ah = (v ^ 0x48) | 0x01;
259             break;
260         }
261         yield();
262     }
263
264     set_success(regs);
265 }
266
267 // INT 17 - PRINTER - INITIALIZE PORT
268 static void
269 handle_1701(struct bregs *regs)
270 {
271     u16 addr = getLptAddr(regs);
272     if (!addr)
273         return;
274
275     u8 val8 = inb(addr+2);
276     outb(val8 & ~0x04, addr+2); // send init
277     udelay(5);
278     outb(val8 | 0x04, addr+2);
279
280     regs->ah = inb(addr+1) ^ 0x48;
281     set_success(regs);
282 }
283
284 // INT 17 - PRINTER - GET STATUS
285 static void
286 handle_1702(struct bregs *regs)
287 {
288     u16 addr = getLptAddr(regs);
289     if (!addr)
290         return;
291     regs->ah = inb(addr+1) ^ 0x48;
292     set_success(regs);
293 }
294
295 static void
296 handle_17XX(struct bregs *regs)
297 {
298     set_unimplemented(regs);
299 }
300
301 // INT17h : Printer Service Entry Point
302 void VISIBLE16
303 handle_17(struct bregs *regs)
304 {
305     debug_enter(regs, DEBUG_HDL_17);
306     if (! CONFIG_LPT) {
307         handle_17XX(regs);
308         return;
309     }
310
311     switch (regs->ah) {
312     case 0x00: handle_1700(regs); break;
313     case 0x01: handle_1701(regs); break;
314     case 0x02: handle_1702(regs); break;
315     default:   handle_17XX(regs); break;
316     }
317 }