blob: 19e39ca56839f9c6f3a3d7791561eda93f212826 [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]));
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400121 for (;;) {
122 u8 lsr = inb(addr+SEROFF_LSR);
123 if ((lsr & 0x60) == 0x60) {
124 // Success - can write data
125 outb(regs->al, addr+SEROFF_DATA);
126 // XXX - reread lsr?
127 regs->ah = lsr;
128 break;
129 }
130 if (checkTickTimer(&tt)) {
131 // Timed out - can't write data.
132 regs->ah = lsr | 0x80;
133 break;
Kevin O'Connor5c732402008-06-07 10:43:07 -0400134 }
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400135 yield();
Kevin O'Connor5c732402008-06-07 10:43:07 -0400136 }
Kevin O'Connor6c781222008-03-09 12:19:23 -0400137 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500138}
139
Kevin O'Connor1812e202008-05-07 21:29:50 -0400140// SERIAL - READ CHARACTER FROM PORT
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500141static void
142handle_1402(struct bregs *regs)
143{
144 u16 addr = getComAddr(regs);
145 if (!addr)
146 return;
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400147 struct tick_timer_s tt = initTickTimer(GET_BDA(com_timeout[regs->dx]));
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400148 for (;;) {
149 u8 lsr = inb(addr+SEROFF_LSR);
150 if (lsr & 0x01) {
151 // Success - can read data
152 regs->al = inb(addr+SEROFF_DATA);
153 regs->ah = lsr;
154 break;
155 }
156 if (checkTickTimer(&tt)) {
157 // Timed out - can't read data.
158 regs->ah = lsr | 0x80;
159 break;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500160 }
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400161 yield();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500162 }
Kevin O'Connor6c781222008-03-09 12:19:23 -0400163 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500164}
165
Kevin O'Connor1812e202008-05-07 21:29:50 -0400166// SERIAL - GET PORT STATUS
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500167static void
168handle_1403(struct bregs *regs)
169{
170 u16 addr = getComAddr(regs);
171 if (!addr)
172 return;
Kevin O'Connorc151b3b2009-05-12 22:59:41 -0400173 regs->ah = inb(addr+SEROFF_LSR);
174 regs->al = inb(addr+SEROFF_MSR);
Kevin O'Connor6c781222008-03-09 12:19:23 -0400175 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500176}
177
178static void
179handle_14XX(struct bregs *regs)
180{
181 // Unsupported
Kevin O'Connor6c781222008-03-09 12:19:23 -0400182 set_fail(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500183}
184
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500185// INT 14h Serial Communications Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500186void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500187handle_14(struct bregs *regs)
188{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400189 debug_enter(regs, DEBUG_HDL_14);
Kevin O'Connor40967022008-07-21 22:23:05 -0400190 if (! CONFIG_SERIAL) {
191 handle_14XX(regs);
192 return;
193 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500194
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500195 switch (regs->ah) {
196 case 0x00: handle_1400(regs); break;
197 case 0x01: handle_1401(regs); break;
198 case 0x02: handle_1402(regs); break;
199 case 0x03: handle_1403(regs); break;
200 default: handle_14XX(regs); break;
201 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500202}
203
Kevin O'Connor30853762009-01-17 18:49:20 -0500204// XXX - Baud Rate Generator Table
205u8 BaudTable[16] VAR16FIXED(0xe729);
206
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500207
208/****************************************************************
209 * LPT ports
210 ****************************************************************/
211
212static u16
Kevin O'Connor913cc2e2008-04-13 17:31:45 -0400213detect_parport(u16 port, u8 timeout, u8 count)
214{
215 // clear input mode
216 outb(inb(port+2) & 0xdf, port+2);
217
218 outb(0xaa, port);
219 if (inb(port) != 0xaa)
220 // Not present
221 return 0;
222 SET_BDA(port_lpt[count], port);
223 SET_BDA(lpt_timeout[count], timeout);
224 return 1;
225}
226
227void
228lpt_setup()
229{
Kevin O'Connor40967022008-07-21 22:23:05 -0400230 if (! CONFIG_LPT)
231 return;
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400232 dprintf(3, "init lpt\n");
Kevin O'Connor40967022008-07-21 22:23:05 -0400233
Kevin O'Connor913cc2e2008-04-13 17:31:45 -0400234 u16 count = 0;
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400235 count += detect_parport(PORT_LPT1, 0x14, count);
236 count += detect_parport(PORT_LPT2, 0x14, count);
Kevin O'Connor4edb2752009-05-06 23:25:40 -0400237 dprintf(1, "Found %d lpt ports\n", count);
Kevin O'Connor913cc2e2008-04-13 17:31:45 -0400238
239 // Equipment word bits 14..15 determing # parallel ports
240 u16 eqb = GET_BDA(equipment_list_flags);
241 SET_BDA(equipment_list_flags, (eqb & 0x3fff) | (count << 14));
242}
243
244static u16
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500245getLptAddr(struct bregs *regs)
246{
247 if (regs->dx >= 3) {
Kevin O'Connor6c781222008-03-09 12:19:23 -0400248 set_fail(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500249 return 0;
250 }
251 u16 addr = GET_BDA(port_lpt[regs->dx]);
252 if (! addr)
Kevin O'Connor6c781222008-03-09 12:19:23 -0400253 set_fail(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500254 return addr;
255}
256
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500257// INT 17 - PRINTER - WRITE CHARACTER
258static void
259handle_1700(struct bregs *regs)
260{
261 u16 addr = getLptAddr(regs);
262 if (!addr)
263 return;
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400264
265 struct tick_timer_s tt = initTickTimer(GET_BDA(lpt_timeout[regs->dx]));
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500266
267 outb(regs->al, addr);
268 u8 val8 = inb(addr+2);
269 outb(val8 | 0x01, addr+2); // send strobe
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400270 udelay(5);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500271 outb(val8 & ~0x01, addr+2);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500272
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400273 for (;;) {
274 u8 v = inb(addr+1);
275 if (!(v & 0x40)) {
276 // Success
277 regs->ah = v ^ 0x48;
278 break;
279 }
280 if (checkTickTimer(&tt)) {
281 // Timeout
282 regs->ah = (v ^ 0x48) | 0x01;
283 break;
284 }
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400285 yield();
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400286 }
287
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400288 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500289}
290
291// INT 17 - PRINTER - INITIALIZE PORT
292static void
293handle_1701(struct bregs *regs)
294{
295 u16 addr = getLptAddr(regs);
296 if (!addr)
297 return;
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500298
299 u8 val8 = inb(addr+2);
300 outb(val8 & ~0x04, addr+2); // send init
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400301 udelay(5);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500302 outb(val8 | 0x04, addr+2);
303
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400304 regs->ah = inb(addr+1) ^ 0x48;
305 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500306}
307
308// INT 17 - PRINTER - GET STATUS
309static void
310handle_1702(struct bregs *regs)
311{
312 u16 addr = getLptAddr(regs);
313 if (!addr)
314 return;
Kevin O'Connor0c5893d2009-07-13 20:29:07 -0400315 regs->ah = inb(addr+1) ^ 0x48;
316 set_success(regs);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500317}
318
319static void
320handle_17XX(struct bregs *regs)
321{
322 // Unsupported
Kevin O'Connor6c781222008-03-09 12:19:23 -0400323 set_fail(regs);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500324}
325
326// INT17h : Printer Service Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500327void VISIBLE16
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500328handle_17(struct bregs *regs)
329{
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400330 debug_enter(regs, DEBUG_HDL_17);
Kevin O'Connor40967022008-07-21 22:23:05 -0400331 if (! CONFIG_LPT) {
332 handle_17XX(regs);
333 return;
334 }
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500335
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500336 switch (regs->ah) {
337 case 0x00: handle_1700(regs); break;
338 case 0x01: handle_1701(regs); break;
339 case 0x02: handle_1702(regs); break;
340 default: handle_17XX(regs); break;
341 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500342}