3 * Copyright (C) 2001 Brian Palmer <brianp@sginet.com>
4 * Copyright (C) 2001 Eric Kohl
5 * Copyright (C) 2001 Emanuele Aliberti
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* MACROS *******************************************************************/
28 #define DEFAULT_BAUD_RATE 19200
30 #define SER_RBR(x) ((x)+0)
31 #define SER_THR(x) ((x)+0)
32 #define SER_DLL(x) ((x)+0)
33 #define SER_IER(x) ((x)+1)
34 #define SER_DLM(x) ((x)+1)
35 #define SER_IIR(x) ((x)+2)
36 #define SER_LCR(x) ((x)+3)
37 #define SR_LCR_CS5 0x00
38 #define SR_LCR_CS6 0x01
39 #define SR_LCR_CS7 0x02
40 #define SR_LCR_CS8 0x03
41 #define SR_LCR_ST1 0x00
42 #define SR_LCR_ST2 0x04
43 #define SR_LCR_PNO 0x00
44 #define SR_LCR_POD 0x08
45 #define SR_LCR_PEV 0x18
46 #define SR_LCR_PMK 0x28
47 #define SR_LCR_PSP 0x38
48 #define SR_LCR_BRK 0x40
49 #define SR_LCR_DLAB 0x80
50 #define SER_MCR(x) ((x)+4)
51 #define SR_MCR_DTR 0x01
52 #define SR_MCR_RTS 0x02
53 #define SER_LSR(x) ((x)+5)
54 #define SR_LSR_DR 0x01
55 #define SR_LSR_TBE 0x20
56 #define SER_MSR(x) ((x)+6)
57 #define SR_MSR_CTS 0x10
58 #define SR_MSR_DSR 0x20
59 #define SER_SCR(x) ((x)+7)
61 /* STATIC VARIABLES *********************************************************/
63 static ULONG Rs232ComPort
= 0;
64 static ULONG Rs232BaudRate
= 0;
65 static PUCHAR Rs232PortBase
= (PUCHAR
)0;
67 /* The com port must only be initialized once! */
68 static BOOLEAN PortInitialized
= FALSE
;
70 /* STATIC FUNCTIONS *********************************************************/
72 static BOOLEAN
Rs232DoesComPortExist(PUCHAR BaseAddress
)
80 /* save Modem Control Register (MCR) */
81 mcr
= READ_PORT_UCHAR (SER_MCR(BaseAddress
));
83 /* enable loop mode (set Bit 4 of the MCR) */
84 WRITE_PORT_UCHAR (SER_MCR(BaseAddress
), 0x10);
86 /* clear all modem output bits */
87 WRITE_PORT_UCHAR (SER_MCR(BaseAddress
), 0x10);
89 /* read the Modem Status Register */
90 msr
= READ_PORT_UCHAR (SER_MSR(BaseAddress
));
93 * the upper nibble of the MSR (modem output bits) must be
94 * equal to the lower nibble of the MCR (modem input bits)
96 if ((msr
& 0xF0) == 0x00)
98 /* set all modem output bits */
99 WRITE_PORT_UCHAR (SER_MCR(BaseAddress
), 0x1F);
101 /* read the Modem Status Register */
102 msr
= READ_PORT_UCHAR (SER_MSR(BaseAddress
));
105 * the upper nibble of the MSR (modem output bits) must be
106 * equal to the lower nibble of the MCR (modem input bits)
108 if ((msr
& 0xF0) == 0xF0)
113 WRITE_PORT_UCHAR (SER_MCR(BaseAddress
), mcr
);
118 /* FUNCTIONS *********************************************************/
120 BOOLEAN
Rs232PortInitialize(ULONG ComPort
, ULONG BaudRate
)
122 ULONG BaseArray
[5] = {0, 0x3F8, 0x2F8, 0x3E8, 0x2E8};
127 if (PortInitialized
== FALSE
)
131 Rs232BaudRate
= BaudRate
;
135 Rs232BaudRate
= DEFAULT_BAUD_RATE
;
140 if (Rs232DoesComPortExist ((PUCHAR
)BaseArray
[2]))
142 Rs232PortBase
= (PUCHAR
)BaseArray
[2];
146 "\nSerial port COM%ld found at 0x%lx\n",
149 HalDisplayString (buffer);
150 #endif*/ /* NDEBUG */
152 else if (Rs232DoesComPortExist ((PUCHAR
)BaseArray
[1]))
154 Rs232PortBase
= (PUCHAR
)BaseArray
[1];
158 "\nSerial port COM%ld found at 0x%lx\n",
161 HalDisplayString (buffer);
162 #endif*/ /* NDEBUG */
167 "\nKernel Debugger: No COM port found!!!\n\n");
168 HalDisplayString (buffer);*/
174 if (Rs232DoesComPortExist ((PUCHAR
)BaseArray
[ComPort
]))
176 Rs232PortBase
= (PUCHAR
)BaseArray
[ComPort
];
177 Rs232ComPort
= ComPort
;
180 "\nSerial port COM%ld found at 0x%lx\n",
183 HalDisplayString (buffer);
184 #endif*/ /* NDEBUG */
189 "\nKernel Debugger: No serial port found!!!\n\n");
190 HalDisplayString (buffer);*/
195 PortInitialized
= TRUE
;
199 * set baud rate and data format (8N1)
202 /* turn on DTR and RTS */
203 WRITE_PORT_UCHAR (SER_MCR(Rs232PortBase
), SR_MCR_DTR
| SR_MCR_RTS
);
206 lcr
= READ_PORT_UCHAR (SER_LCR(Rs232PortBase
)) | SR_LCR_DLAB
;
207 WRITE_PORT_UCHAR (SER_LCR(Rs232PortBase
), lcr
);
210 divisor
= 115200 / BaudRate
;
211 WRITE_PORT_UCHAR (SER_DLL(Rs232PortBase
), divisor
& 0xff);
212 WRITE_PORT_UCHAR (SER_DLM(Rs232PortBase
), (divisor
>> 8) & 0xff);
214 /* reset DLAB and set 8N1 format */
215 WRITE_PORT_UCHAR (SER_LCR(Rs232PortBase
),
216 SR_LCR_CS8
| SR_LCR_ST1
| SR_LCR_PNO
);
218 /* read junk out of the RBR */
219 lcr
= READ_PORT_UCHAR (SER_RBR(Rs232PortBase
));
224 //KdComPortInUse = (ULONG)PortBase;
227 * print message to blue screen
230 "\nKernel Debugger: COM%ld (Port 0x%lx) BaudRate %ld\n\n",
235 HalDisplayString (buffer);*/
240 BOOLEAN
Rs232PortGetByte(PUCHAR ByteRecieved
)
242 if (PortInitialized
== FALSE
)
245 if ((READ_PORT_UCHAR (SER_LSR(Rs232PortBase
)) & SR_LSR_DR
))
247 *ByteRecieved
= READ_PORT_UCHAR (SER_RBR(Rs232PortBase
));
254 BOOLEAN
Rs232PortPollByte(PUCHAR ByteRecieved
)
256 if (PortInitialized
== FALSE
)
259 while ((READ_PORT_UCHAR (SER_LSR(Rs232PortBase
)) & SR_LSR_DR
) == 0)
262 *ByteRecieved
= READ_PORT_UCHAR (SER_RBR(Rs232PortBase
));
267 VOID
Rs232PortPutByte(UCHAR ByteToSend
)
269 if (PortInitialized
== FALSE
)
272 while ((READ_PORT_UCHAR (SER_LSR(Rs232PortBase
)) & SR_LSR_TBE
) == 0)
275 WRITE_PORT_UCHAR (SER_THR(Rs232PortBase
), ByteToSend
);
280 BOOLEAN
Rs232PortInUse(ULONG Base
)
283 return PortInitialized
&& Rs232PortBase
== (PUCHAR
)Base
? TRUE
: FALSE
;