blob: a24f83f48d029204170774f9b1e4c6987e26aed4 [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// 16bit code to handle serial and printer services.
2//
Kevin O'Connor4edb2752009-05-06 23:25:40 -04003// Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05004// Copyright (C) 2002 MandrakeSoft S.A.
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05007
Kevin O'Connor9521e262008-07-04 13:04:29 -04008#include "biosvar.h" // SET_BDA
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05009#include "util.h" // debug_enter
Kevin O'Connor9521e262008-07-04 13:04:29 -040010#include "bregs.h" // struct bregs
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050011
Kevin O'Connor0c5893d2009-07-13 20:29:07 -040012// Timers based on 18.2Hz clock irq.
13struct tick_timer_s {
14 u16 last_tick, remaining;
15};
16
17struct tick_timer_s
18initTickTimer(u16 count)
19{
20 struct tick_timer_s tt = {GET_BDA(timer_counter), count};
21 return tt;
22}
23
24int
25checkTickTimer(struct tick_timer_s *tt)
26{
27 u16 timer = GET_BDA(timer_counter);
28 if (tt->last_tick != timer) {
29 tt->last_tick = timer;
30 tt->last_tick--;
31 if (!tt->last_tick)
32 return 1;
33 }
34 return 0;
35}
36
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050037
38/****************************************************************
39 * COM ports
40 ****************************************************************/
41
42static u16
Kevin O'Connor913cc2e2008-04-13 17:31:45 -040043detect_serial(u16 port, u8 timeout, u8 count)
44{
Kevin O'Connorc151b3b2009-05-12 22:59:41 -040045 outb(0x02, port+SEROFF_IER);
46 u8 ier = inb(port+SEROFF_IER);
47 if (ier != 0x02)
Kevin O'Connor913cc2e2008-04-13 17:31:45 -040048 return 0;
Kevin O'Connorc151b3b2009-05-12 22:59:41 -040049 u8 iir = inb(port+SEROFF_IIR);
50 if ((iir & 0x3f) != 0x02)
Kevin O'Connor913cc2e2008-04-13 17:31:45 -040051 return 0;
Kevin O'Connorc151b3b2009-05-12 22:59:41 -040052
53 outb(0x00, port+SEROFF_IER);
Kevin O'Connor913cc2e2008-04-13 17:31:45 -040054 SET_BDA(port_com[count], port);
55 SET_BDA(com_timeout[count], timeout);
56 return 1;
57}
58
59void
60serial_setup()
61{
Kevin O'Connor40967022008-07-21 22:23:05 -040062 if (! CONFIG_SERIAL)
63 return;
Kevin O'Connor35192dd2008-06-08 19:18:33 -040064 dprintf(3, "init serial\n");
Kevin O'Connor40967022008-07-21 22:23:05 -040065
Kevin O'Connor913cc2e2008-04-13 17:31:45 -040066 u16 count = 0;
Kevin O'Connorc151b3b2009-05-12 22:59:41 -040067 count += detect_serial(PORT_SERIAL1, 0x0a, count);
68 count += detect_serial(PORT_SERIAL2, 0x0a, count);
69 count += detect_serial(PORT_SERIAL3, 0x0a, count);
70 count += detect_serial(PORT_SERIAL4, 0x0a, count);
Kevin O'Connor4edb2752009-05-06 23:25:40 -040071 dprintf(1, "Found %d serial ports\n", count);
Kevin O'Connor913cc2e2008-04-13 17:31:45 -040072
73 // Equipment word bits 9..11 determing # serial ports
74 u16 eqb = GET_BDA(equipment_list_flags);
75 SET_BDA(equipment_list_flags, (eqb & 0xf1ff) | (count << 9));
76}
77
78static u16
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050079getComAddr(struct bregs *regs)
80{
81 if (regs->dx >= 4) {
Kevin O'Connor6c781222008-03-09 12:19:23 -040082 set_fail(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050083 return 0;
84 }
85 u16 addr = GET_BDA(port_com[regs->dx]);
86 if (! addr)
Kevin O'Connor6c781222008-03-09 12:19:23 -040087 set_fail(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050088 return addr;
89}
90
Kevin O'Connor1812e202008-05-07 21:29:50 -040091// SERIAL - INITIALIZE PORT
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050092static void
93handle_1400(struct bregs *regs)
94{
95 u16 addr = getComAddr(regs);
96 if (!addr)
97 return;
Kevin O'Connorc151b3b2009-05-12 22:59:41 -040098 outb(inb(addr+SEROFF_LCR) | 0x80, addr+SEROFF_LCR);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050099 if ((regs->al & 0xE0) == 0) {
Kevin O'Connorc151b3b2009-05-12 22:59:41 -0400100 outb(0x17, addr+SEROFF_DLL);
101 outb(0x04, addr+SEROFF_DLH);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500102 } else {
103 u16 val16 = 0x600 >> ((regs->al & 0xE0) >> 5);
Kevin O'Connorc151b3b2009-05-12 22:59:41 -0400104 outb(val16 & 0xFF, addr+SEROFF_DLL);
105 outb(val16 >> 8, addr+SEROFF_DLH);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500106 }
Kevin O'Connorc151b3b2009-05-12 22:59:41 -0400107 outb(regs->al & 0x1F, addr+SEROFF_LCR);
108 regs->ah = inb(addr+SEROFF_LSR);
109 regs->al = inb(addr+SEROFF_MSR);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400110 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500111}
112
Kevin O'Connor1812e202008-05-07 21:29:50 -0400113// SERIAL - WRITE CHARACTER TO PORT
114static void
115handle_1401(struct bregs *regs)
116{
117 u16 addr = getComAddr(regs);
118 if (!addr)
119 return;
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400120 struct tick_timer_s tt = initTickTimer(GET_BDA(com_timeout[regs->dx]));
121 irq_enable();
122 for (;;) {
123 u8 lsr = inb(addr+SEROFF_LSR);
124 if ((lsr & 0x60) == 0x60) {
125 // Success - can write data
126 outb(regs->al, addr+SEROFF_DATA);
127 // XXX - reread lsr?
128 regs->ah = lsr;
129 break;
130 }
131 if (checkTickTimer(&tt)) {
132 // Timed out - can't write data.
133 regs->ah = lsr | 0x80;
134 break;
Kevin O'Connor5c732402008-06-07 10:43:07 -0400135 }
136 }
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400137 irq_disable();
Kevin O'Connor6c781222008-03-09 12:19:23 -0400138 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500139}
140
Kevin O'Connor1812e202008-05-07 21:29:50 -0400141// SERIAL - READ CHARACTER FROM PORT
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500142static void
143handle_1402(struct bregs *regs)
144{
145 u16 addr = getComAddr(regs);
146 if (!addr)
147 return;
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400148 struct tick_timer_s tt = initTickTimer(GET_BDA(com_timeout[regs->dx]));
149 irq_enable();
150 for (;;) {
151 u8 lsr = inb(addr+SEROFF_LSR);
152 if (lsr & 0x01) {
153 // Success - can read data
154 regs->al = inb(addr+SEROFF_DATA);
155 regs->ah = lsr;
156 break;
157 }
158 if (checkTickTimer(&tt)) {
159 // Timed out - can't read data.
160 regs->ah = lsr | 0x80;
161 break;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500162 }
163 }
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400164 irq_disable();
Kevin O'Connor6c781222008-03-09 12:19:23 -0400165 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500166}
167
Kevin O'Connor1812e202008-05-07 21:29:50 -0400168// SERIAL - GET PORT STATUS
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500169static void
170handle_1403(struct bregs *regs)
171{
172 u16 addr = getComAddr(regs);
173 if (!addr)
174 return;
Kevin O'Connorc151b3b2009-05-12 22:59:41 -0400175 regs->ah = inb(addr+SEROFF_LSR);
176 regs->al = inb(addr+SEROFF_MSR);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400177 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500178}
179
180static void
181handle_14XX(struct bregs *regs)
182{
183 // Unsupported
Kevin O'Connor6c781222008-03-09 12:19:23 -0400184 set_fail(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500185}
186
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500187// INT 14h Serial Communications Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500188void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500189handle_14(struct bregs *regs)
190{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400191 debug_enter(regs, DEBUG_HDL_14);
Kevin O'Connor40967022008-07-21 22:23:05 -0400192 if (! CONFIG_SERIAL) {
193 handle_14XX(regs);
194 return;
195 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500196
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500197 switch (regs->ah) {
198 case 0x00: handle_1400(regs); break;
199 case 0x01: handle_1401(regs); break;
200 case 0x02: handle_1402(regs); break;
201 case 0x03: handle_1403(regs); break;
202 default: handle_14XX(regs); break;
203 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500204}
205
Kevin O'Connor30853762009-01-17 18:49:20 -0500206// XXX - Baud Rate Generator Table
207u8 BaudTable[16] VAR16FIXED(0xe729);
208
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500209
210/****************************************************************
211 * LPT ports
212 ****************************************************************/
213
214static u16
Kevin O'Connor913cc2e2008-04-13 17:31:45 -0400215detect_parport(u16 port, u8 timeout, u8 count)
216{
217 // clear input mode
218 outb(inb(port+2) & 0xdf, port+2);
219
220 outb(0xaa, port);
221 if (inb(port) != 0xaa)
222 // Not present
223 return 0;
224 SET_BDA(port_lpt[count], port);
225 SET_BDA(lpt_timeout[count], timeout);
226 return 1;
227}
228
229void
230lpt_setup()
231{
Kevin O'Connor40967022008-07-21 22:23:05 -0400232 if (! CONFIG_LPT)
233 return;
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400234 dprintf(3, "init lpt\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400235
Kevin O'Connor913cc2e2008-04-13 17:31:45 -0400236 u16 count = 0;
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400237 count += detect_parport(PORT_LPT1, 0x14, count);
238 count += detect_parport(PORT_LPT2, 0x14, count);
Kevin O'Connor4edb2752009-05-06 23:25:40 -0400239 dprintf(1, "Found %d lpt ports\n", count);
Kevin O'Connor913cc2e2008-04-13 17:31:45 -0400240
241 // Equipment word bits 14..15 determing # parallel ports
242 u16 eqb = GET_BDA(equipment_list_flags);
243 SET_BDA(equipment_list_flags, (eqb & 0x3fff) | (count << 14));
244}
245
246static u16
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500247getLptAddr(struct bregs *regs)
248{
249 if (regs->dx >= 3) {
Kevin O'Connor6c781222008-03-09 12:19:23 -0400250 set_fail(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500251 return 0;
252 }
253 u16 addr = GET_BDA(port_lpt[regs->dx]);
254 if (! addr)
Kevin O'Connor6c781222008-03-09 12:19:23 -0400255 set_fail(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500256 return addr;
257}
258
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500259// INT 17 - PRINTER - WRITE CHARACTER
260static void
261handle_1700(struct bregs *regs)
262{
263 u16 addr = getLptAddr(regs);
264 if (!addr)
265 return;
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400266
267 struct tick_timer_s tt = initTickTimer(GET_BDA(lpt_timeout[regs->dx]));
268 irq_enable();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500269
270 outb(regs->al, addr);
271 u8 val8 = inb(addr+2);
272 outb(val8 | 0x01, addr+2); // send strobe
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400273 udelay(5);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500274 outb(val8 & ~0x01, addr+2);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500275
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400276 for (;;) {
277 u8 v = inb(addr+1);
278 if (!(v & 0x40)) {
279 // Success
280 regs->ah = v ^ 0x48;
281 break;
282 }
283 if (checkTickTimer(&tt)) {
284 // Timeout
285 regs->ah = (v ^ 0x48) | 0x01;
286 break;
287 }
288 }
289
290 irq_disable();
291 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500292}
293
294// INT 17 - PRINTER - INITIALIZE PORT
295static void
296handle_1701(struct bregs *regs)
297{
298 u16 addr = getLptAddr(regs);
299 if (!addr)
300 return;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500301
302 u8 val8 = inb(addr+2);
303 outb(val8 & ~0x04, addr+2); // send init
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400304 udelay(5);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500305 outb(val8 | 0x04, addr+2);
306
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400307 regs->ah = inb(addr+1) ^ 0x48;
308 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500309}
310
311// INT 17 - PRINTER - GET STATUS
312static void
313handle_1702(struct bregs *regs)
314{
315 u16 addr = getLptAddr(regs);
316 if (!addr)
317 return;
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400318 regs->ah = inb(addr+1) ^ 0x48;
319 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500320}
321
322static void
323handle_17XX(struct bregs *regs)
324{
325 // Unsupported
Kevin O'Connor6c781222008-03-09 12:19:23 -0400326 set_fail(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500327}
328
329// INT17h : Printer Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500330void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500331handle_17(struct bregs *regs)
332{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400333 debug_enter(regs, DEBUG_HDL_17);
Kevin O'Connor40967022008-07-21 22:23:05 -0400334 if (! CONFIG_LPT) {
335 handle_17XX(regs);
336 return;
337 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500338
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500339 switch (regs->ah) {
340 case 0x00: handle_1700(regs); break;
341 case 0x01: handle_1701(regs); break;
342 case 0x02: handle_1702(regs); break;
343 default: handle_17XX(regs); break;
344 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500345}