364bc0d6b1ea6a795a246664805c68a30d428d6b
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 *******************************************************************/
26 #define DEFAULT_BAUD_RATE 19200
28 #define SER_RBR(x) ((x)+0)
29 #define SER_THR(x) ((x)+0)
30 #define SER_DLL(x) ((x)+0)
31 #define SER_IER(x) ((x)+1)
32 #define SER_DLM(x) ((x)+1)
33 #define SER_IIR(x) ((x)+2)
34 #define SER_LCR(x) ((x)+3)
35 #define SR_LCR_CS5 0x00
36 #define SR_LCR_CS6 0x01
37 #define SR_LCR_CS7 0x02
38 #define SR_LCR_CS8 0x03
39 #define SR_LCR_ST1 0x00
40 #define SR_LCR_ST2 0x04
41 #define SR_LCR_PNO 0x00
42 #define SR_LCR_POD 0x08
43 #define SR_LCR_PEV 0x18
44 #define SR_LCR_PMK 0x28
45 #define SR_LCR_PSP 0x38
46 #define SR_LCR_BRK 0x40
47 #define SR_LCR_DLAB 0x80
48 #define SER_MCR(x) ((x)+4)
49 #define SR_MCR_DTR 0x01
50 #define SR_MCR_RTS 0x02
51 #define SER_LSR(x) ((x)+5)
52 #define SR_LSR_DR 0x01
53 #define SR_LSR_TBE 0x20
54 #define SER_MSR(x) ((x)+6)
55 #define SR_MSR_CTS 0x10
56 #define SR_MSR_DSR 0x20
57 #define SER_SCR(x) ((x)+7)
59 /* STATIC VARIABLES *********************************************************/
61 static ULONG Rs232ComPort
= 0;
62 static ULONG Rs232BaudRate
= 0;
63 static PUCHAR Rs232PortBase
= (PUCHAR
)0;
65 /* The com port must only be initialized once! */
66 static BOOLEAN PortInitialized
= FALSE
;
68 /* STATIC FUNCTIONS *********************************************************/
70 static BOOL
Rs232DoesComPortExist(PUCHAR BaseAddress
)
78 /* save Modem Control Register (MCR) */
79 mcr
= READ_PORT_UCHAR (SER_MCR(BaseAddress
));
81 /* enable loop mode (set Bit 4 of the MCR) */
82 WRITE_PORT_UCHAR (SER_MCR(BaseAddress
), 0x10);
84 /* clear all modem output bits */
85 WRITE_PORT_UCHAR (SER_MCR(BaseAddress
), 0x10);
87 /* read the Modem Status Register */
88 msr
= READ_PORT_UCHAR (SER_MSR(BaseAddress
));
91 * the upper nibble of the MSR (modem output bits) must be
92 * equal to the lower nibble of the MCR (modem input bits)
94 if ((msr
& 0xF0) == 0x00)
96 /* set all modem output bits */
97 WRITE_PORT_UCHAR (SER_MCR(BaseAddress
), 0x1F);
99 /* read the Modem Status Register */
100 msr
= READ_PORT_UCHAR (SER_MSR(BaseAddress
));
103 * the upper nibble of the MSR (modem output bits) must be
104 * equal to the lower nibble of the MCR (modem input bits)
106 if ((msr
& 0xF0) == 0xF0)
111 WRITE_PORT_UCHAR (SER_MCR(BaseAddress
), mcr
);
116 /* FUNCTIONS *********************************************************/
118 BOOL
Rs232PortInitialize(ULONG ComPort
, ULONG BaudRate
)
120 ULONG BaseArray
[5] = {0, 0x3F8, 0x2F8, 0x3E8, 0x2E8};
125 if (PortInitialized
== FALSE
)
129 Rs232BaudRate
= BaudRate
;
133 Rs232BaudRate
= DEFAULT_BAUD_RATE
;
138 if (Rs232DoesComPortExist ((PUCHAR
)BaseArray
[2]))
140 Rs232PortBase
= (PUCHAR
)BaseArray
[2];
144 "\nSerial port COM%ld found at 0x%lx\n",
147 HalDisplayString (buffer);
148 #endif*/ /* NDEBUG */
150 else if (Rs232DoesComPortExist ((PUCHAR
)BaseArray
[1]))
152 Rs232PortBase
= (PUCHAR
)BaseArray
[1];
156 "\nSerial port COM%ld found at 0x%lx\n",
159 HalDisplayString (buffer);
160 #endif*/ /* NDEBUG */
165 "\nKernel Debugger: No COM port found!!!\n\n");
166 HalDisplayString (buffer);*/
172 if (Rs232DoesComPortExist ((PUCHAR
)BaseArray
[ComPort
]))
174 Rs232PortBase
= (PUCHAR
)BaseArray
[ComPort
];
175 Rs232ComPort
= ComPort
;
178 "\nSerial port COM%ld found at 0x%lx\n",
181 HalDisplayString (buffer);
182 #endif*/ /* NDEBUG */
187 "\nKernel Debugger: No serial port found!!!\n\n");
188 HalDisplayString (buffer);*/
193 PortInitialized
= TRUE
;
197 * set baud rate and data format (8N1)
200 /* turn on DTR and RTS */
201 WRITE_PORT_UCHAR (SER_MCR(Rs232PortBase
), SR_MCR_DTR
| SR_MCR_RTS
);
204 lcr
= READ_PORT_UCHAR (SER_LCR(Rs232PortBase
)) | SR_LCR_DLAB
;
205 WRITE_PORT_UCHAR (SER_LCR(Rs232PortBase
), lcr
);
208 divisor
= 115200 / BaudRate
;
209 WRITE_PORT_UCHAR (SER_DLL(Rs232PortBase
), divisor
& 0xff);
210 WRITE_PORT_UCHAR (SER_DLM(Rs232PortBase
), (divisor
>> 8) & 0xff);
212 /* reset DLAB and set 8N1 format */
213 WRITE_PORT_UCHAR (SER_LCR(Rs232PortBase
),
214 SR_LCR_CS8
| SR_LCR_ST1
| SR_LCR_PNO
);
216 /* read junk out of the RBR */
217 lcr
= READ_PORT_UCHAR (SER_RBR(Rs232PortBase
));
222 //KdComPortInUse = (ULONG)PortBase;
225 * print message to blue screen
228 "\nKernel Debugger: COM%ld (Port 0x%lx) BaudRate %ld\n\n",
233 HalDisplayString (buffer);*/
238 BOOL
Rs232PortGetByte(PUCHAR ByteRecieved
)
240 if (PortInitialized
== FALSE
)
243 if ((READ_PORT_UCHAR (SER_LSR(Rs232PortBase
)) & SR_LSR_DR
))
245 *ByteRecieved
= READ_PORT_UCHAR (SER_RBR(Rs232PortBase
));
252 BOOL
Rs232PortPollByte(PUCHAR ByteRecieved
)
254 if (PortInitialized
== FALSE
)
257 while ((READ_PORT_UCHAR (SER_LSR(Rs232PortBase
)) & SR_LSR_DR
) == 0)
260 *ByteRecieved
= READ_PORT_UCHAR (SER_RBR(Rs232PortBase
));
265 VOID
Rs232PortPutByte(UCHAR ByteToSend
)
267 if (PortInitialized
== FALSE
)
270 while ((READ_PORT_UCHAR (SER_LSR(Rs232PortBase
)) & SR_LSR_TBE
) == 0)
273 WRITE_PORT_UCHAR (SER_THR(Rs232PortBase
), ByteToSend
);