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.
27 /* MACROS *******************************************************************/
29 #define DEFAULT_BAUD_RATE 19200
31 #define SER_RBR(x) ((x)+0)
32 #define SER_THR(x) ((x)+0)
33 #define SER_DLL(x) ((x)+0)
34 #define SER_IER(x) ((x)+1)
35 #define SER_DLM(x) ((x)+1)
36 #define SER_IIR(x) ((x)+2)
37 #define SER_LCR(x) ((x)+3)
38 #define SR_LCR_CS5 0x00
39 #define SR_LCR_CS6 0x01
40 #define SR_LCR_CS7 0x02
41 #define SR_LCR_CS8 0x03
42 #define SR_LCR_ST1 0x00
43 #define SR_LCR_ST2 0x04
44 #define SR_LCR_PNO 0x00
45 #define SR_LCR_POD 0x08
46 #define SR_LCR_PEV 0x18
47 #define SR_LCR_PMK 0x28
48 #define SR_LCR_PSP 0x38
49 #define SR_LCR_BRK 0x40
50 #define SR_LCR_DLAB 0x80
51 #define SER_MCR(x) ((x)+4)
52 #define SR_MCR_DTR 0x01
53 #define SR_MCR_RTS 0x02
54 #define SER_LSR(x) ((x)+5)
55 #define SR_LSR_DR 0x01
56 #define SR_LSR_TBE 0x20
57 #define SER_MSR(x) ((x)+6)
58 #define SR_MSR_CTS 0x10
59 #define SR_MSR_DSR 0x20
60 #define SER_SCR(x) ((x)+7)
62 /* STATIC VARIABLES *********************************************************/
64 static U32 Rs232ComPort
= 0;
65 static U32 Rs232BaudRate
= 0;
66 static PUCHAR Rs232PortBase
= (PUCHAR
)0;
68 /* The com port must only be initialized once! */
69 static BOOLEAN PortInitialized
= FALSE
;
71 /* STATIC FUNCTIONS *********************************************************/
73 static BOOL
Rs232DoesComPortExist(PUCHAR BaseAddress
)
81 /* save Modem Control Register (MCR) */
82 mcr
= READ_PORT_UCHAR (SER_MCR(BaseAddress
));
84 /* enable loop mode (set Bit 4 of the MCR) */
85 WRITE_PORT_UCHAR (SER_MCR(BaseAddress
), 0x10);
87 /* clear all modem output bits */
88 WRITE_PORT_UCHAR (SER_MCR(BaseAddress
), 0x10);
90 /* read the Modem Status Register */
91 msr
= READ_PORT_UCHAR (SER_MSR(BaseAddress
));
94 * the upper nibble of the MSR (modem output bits) must be
95 * equal to the lower nibble of the MCR (modem input bits)
97 if ((msr
& 0xF0) == 0x00)
99 /* set all modem output bits */
100 WRITE_PORT_UCHAR (SER_MCR(BaseAddress
), 0x1F);
102 /* read the Modem Status Register */
103 msr
= READ_PORT_UCHAR (SER_MSR(BaseAddress
));
106 * the upper nibble of the MSR (modem output bits) must be
107 * equal to the lower nibble of the MCR (modem input bits)
109 if ((msr
& 0xF0) == 0xF0)
114 WRITE_PORT_UCHAR (SER_MCR(BaseAddress
), mcr
);
119 /* FUNCTIONS *********************************************************/
121 BOOL
Rs232PortInitialize(U32 ComPort
, U32 BaudRate
)
123 U32 BaseArray
[5] = {0, 0x3F8, 0x2F8, 0x3E8, 0x2E8};
128 if (PortInitialized
== FALSE
)
132 Rs232BaudRate
= BaudRate
;
136 Rs232BaudRate
= DEFAULT_BAUD_RATE
;
141 if (Rs232DoesComPortExist ((PUCHAR
)BaseArray
[2]))
143 Rs232PortBase
= (PUCHAR
)BaseArray
[2];
147 "\nSerial port COM%ld found at 0x%lx\n",
150 HalDisplayString (buffer);
151 #endif*/ /* NDEBUG */
153 else if (Rs232DoesComPortExist ((PUCHAR
)BaseArray
[1]))
155 Rs232PortBase
= (PUCHAR
)BaseArray
[1];
159 "\nSerial port COM%ld found at 0x%lx\n",
162 HalDisplayString (buffer);
163 #endif*/ /* NDEBUG */
168 "\nKernel Debugger: No COM port found!!!\n\n");
169 HalDisplayString (buffer);*/
175 if (Rs232DoesComPortExist ((PUCHAR
)BaseArray
[ComPort
]))
177 Rs232PortBase
= (PUCHAR
)BaseArray
[ComPort
];
178 Rs232ComPort
= ComPort
;
181 "\nSerial port COM%ld found at 0x%lx\n",
184 HalDisplayString (buffer);
185 #endif*/ /* NDEBUG */
190 "\nKernel Debugger: No serial port found!!!\n\n");
191 HalDisplayString (buffer);*/
196 PortInitialized
= TRUE
;
200 * set baud rate and data format (8N1)
203 /* turn on DTR and RTS */
204 WRITE_PORT_UCHAR (SER_MCR(Rs232PortBase
), SR_MCR_DTR
| SR_MCR_RTS
);
207 lcr
= READ_PORT_UCHAR (SER_LCR(Rs232PortBase
)) | SR_LCR_DLAB
;
208 WRITE_PORT_UCHAR (SER_LCR(Rs232PortBase
), lcr
);
211 divisor
= 115200 / BaudRate
;
212 WRITE_PORT_UCHAR (SER_DLL(Rs232PortBase
), divisor
& 0xff);
213 WRITE_PORT_UCHAR (SER_DLM(Rs232PortBase
), (divisor
>> 8) & 0xff);
215 /* reset DLAB and set 8N1 format */
216 WRITE_PORT_UCHAR (SER_LCR(Rs232PortBase
),
217 SR_LCR_CS8
| SR_LCR_ST1
| SR_LCR_PNO
);
219 /* read junk out of the RBR */
220 lcr
= READ_PORT_UCHAR (SER_RBR(Rs232PortBase
));
225 //KdComPortInUse = (U32)PortBase;
228 * print message to blue screen
231 "\nKernel Debugger: COM%ld (Port 0x%lx) BaudRate %ld\n\n",
236 HalDisplayString (buffer);*/
241 BOOL
Rs232PortGetByte(PUCHAR ByteRecieved
)
243 if (PortInitialized
== FALSE
)
246 if ((READ_PORT_UCHAR (SER_LSR(Rs232PortBase
)) & SR_LSR_DR
))
248 *ByteRecieved
= READ_PORT_UCHAR (SER_RBR(Rs232PortBase
));
255 BOOL
Rs232PortPollByte(PUCHAR ByteRecieved
)
257 if (PortInitialized
== FALSE
)
260 while ((READ_PORT_UCHAR (SER_LSR(Rs232PortBase
)) & SR_LSR_DR
) == 0)
263 *ByteRecieved
= READ_PORT_UCHAR (SER_RBR(Rs232PortBase
));
268 VOID
Rs232PortPutByte(UCHAR ByteToSend
)
270 if (PortInitialized
== FALSE
)
273 while ((READ_PORT_UCHAR (SER_LSR(Rs232PortBase
)) & SR_LSR_TBE
) == 0)
276 WRITE_PORT_UCHAR (SER_THR(Rs232PortBase
), ByteToSend
);