[KDCOM]
[reactos.git] / reactos / drivers / base / kddll / kdcom.c
1 /*
2 * COPYRIGHT: GPL, see COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: drivers/base/kddll/kdcom.c
5 * PURPOSE: COM port functions for the kernel debugger.
6 * PROGRAMMER: Timo Kreuzer (timo.kreuzer@ewactos.org)
7 */
8
9 #include "kddll.h"
10 #include "kdcom.h"
11
12 /* Define wait timeout value. */
13 #define REPEAT_COUNT (1000 * 1000)
14
15 /* serial debug connection */
16 #define DEFAULT_DEBUG_PORT 2 /* COM2 */
17 #define DEFAULT_DEBUG_COM1_IRQ 4 /* COM1 IRQ */
18 #define DEFAULT_DEBUG_COM2_IRQ 3 /* COM2 IRQ */
19 #define DEFAULT_DEBUG_BAUD_RATE 115200 /* 115200 Baud */
20
21 #define DEFAULT_BAUD_RATE 19200
22
23
24 #if defined(_M_IX86) || defined(_M_AMD64)
25 const ULONG BaseArray[5] = {0, 0x3F8, 0x2F8, 0x3E8, 0x2E8};
26 #elif defined(_M_PPC)
27 const ULONG BaseArray[2] = {0, 0x800003f8};
28 #elif defined(_M_MIPS)
29 const ULONG BaseArray[3] = {0, 0x80006000, 0x80007000};
30 #elif defined(_M_ARM)
31 const ULONG BaseArray[2] = {0, 0xF1012000};
32 #else
33 #error Unknown architecture
34 #endif
35
36 /* GLOBALS ********************************************************************/
37
38 PUCHAR ComPortBase;
39 ULONG ComPortNumber = DEFAULT_DEBUG_PORT;
40 ULONG ComPortBaudRate = DEFAULT_DEBUG_BAUD_RATE;
41 ULONG ComPortIrq = 0;
42
43
44 NTSTATUS
45 NTAPI
46 KdpPortInitialize()
47 {
48 ULONG Mode;
49
50 KDDBGPRINT("KdpPortInitialize\n");
51
52 /* Enable loop mode (set Bit 4 of the MCR) */
53 WRITE_PORT_UCHAR(ComPortBase + COM_MCR, MCR_LOOP);
54
55 /* Clear all modem output bits */
56 WRITE_PORT_UCHAR(ComPortBase + COM_MCR, MCR_LOOP);
57
58 /* The upper nibble of the MSR (modem output bits) must be
59 * equal to the lower nibble of the MCR (modem input bits) */
60 if ((READ_PORT_UCHAR(ComPortBase + COM_MSR) & 0xF0) != 0x00)
61 {
62 return STATUS_INVALID_PARAMETER;
63 }
64
65 /* Set all modem output bits */
66 WRITE_PORT_UCHAR(ComPortBase + COM_MCR, MCR_ALL);
67
68 /* The upper nibble of the MSR (modem output bits) must be
69 * equal to the lower nibble of the MCR (modem input bits) */
70 if ((READ_PORT_UCHAR(ComPortBase + COM_MSR) & 0xF0) != 0xF0)
71 {
72 return STATUS_INVALID_PARAMETER;
73 }
74
75 /* Enable FIFO */
76 WRITE_PORT_UCHAR(ComPortBase + COM_FCR,
77 FCR_ENABLE_FIFO | FCR_CLEAR_RCVR | FCR_CLEAR_XMIT);
78
79 /* Disable interrupts */
80 WRITE_PORT_UCHAR(ComPortBase + COM_LCR, 0);
81 WRITE_PORT_UCHAR(ComPortBase + COM_IEN, 0);
82
83 /* Enable on DTR and RTS */
84 WRITE_PORT_UCHAR(ComPortBase + COM_MCR, MCR_DTR | MCR_RTS);
85
86 /* Set DLAB */
87 WRITE_PORT_UCHAR(ComPortBase + COM_LCR, LCR_DLAB);
88
89 /* Set baud rate */
90 Mode = 115200 / ComPortBaudRate;
91 WRITE_PORT_UCHAR(ComPortBase + COM_DLL, (UCHAR)(Mode & 0xff));
92 WRITE_PORT_UCHAR(ComPortBase + COM_DLM, (UCHAR)((Mode >> 8) & 0xff));
93
94 /* Reset DLAB and set 8 data bits, 1 stop bit, no parity, no break */
95 WRITE_PORT_UCHAR(ComPortBase + COM_LCR, LCR_CS8 | LCR_ST1 | LCR_PNO);
96
97 /* Check for 16450/16550 scratch register */
98 WRITE_PORT_UCHAR(ComPortBase + COM_SCR, 0xff);
99 if (READ_PORT_UCHAR(ComPortBase + COM_SCR) != 0xff)
100 {
101 return STATUS_INVALID_PARAMETER;
102 }
103 WRITE_PORT_UCHAR(ComPortBase + COM_SCR, 0x00);
104 if (READ_PORT_UCHAR(ComPortBase + COM_SCR) != 0x00)
105 {
106 return STATUS_INVALID_PARAMETER;
107 }
108
109 return STATUS_SUCCESS;
110 }
111
112 /******************************************************************************
113 * \name KdDebuggerInitialize0
114 * \brief Phase 0 initialization.
115 * \param [opt] LoaderBlock Pointer to the Loader parameter block. Can be NULL.
116 * \return Status
117 */
118 NTSTATUS
119 NTAPI
120 KdDebuggerInitialize0(
121 IN PLOADER_PARAMETER_BLOCK LoaderBlock OPTIONAL)
122 {
123 PCHAR CommandLine, PortString, BaudString, IrqString;
124 ULONG Value;
125
126 /* Check if e have a LoaderBlock */
127 if (LoaderBlock)
128 {
129 /* Get the Command Line */
130 CommandLine = LoaderBlock->LoadOptions;
131
132 /* Upcase it */
133 _strupr(CommandLine);
134
135 /* Get the port and baud rate */
136 PortString = strstr(CommandLine, "DEBUGPORT");
137 BaudString = strstr(CommandLine, "BAUDRATE");
138 IrqString = strstr(CommandLine, "IRQ");
139
140 /* Check if we got the /DEBUGPORT parameter */
141 if (PortString)
142 {
143 /* Move past the actual string, to reach the port*/
144 PortString += strlen("DEBUGPORT");
145
146 /* Now get past any spaces and skip the equal sign */
147 while (*PortString == ' ') PortString++;
148 PortString++;
149
150 /* Do we have a serial port? */
151 if (strncmp(PortString, "COM", 3) != 0)
152 {
153 return STATUS_INVALID_PARAMETER;
154 }
155
156 /* Gheck for a valid Serial Port */
157 PortString += 3;
158 Value = atol(PortString);
159 if (Value > 4)
160 {
161 return STATUS_INVALID_PARAMETER;
162 }
163
164 /* Set the port to use */
165 ComPortNumber = Value;
166 }
167
168 /* Check if we got a baud rate */
169 if (BaudString)
170 {
171 /* Move past the actual string, to reach the rate */
172 BaudString += strlen("BAUDRATE");
173
174 /* Now get past any spaces */
175 while (*BaudString == ' ') BaudString++;
176
177 /* And make sure we have a rate */
178 if (*BaudString)
179 {
180 /* Read and set it */
181 Value = atol(BaudString + 1);
182 if (Value) ComPortBaudRate = Value;
183 }
184 }
185
186 /* Check Serial Port Settings [IRQ] */
187 if (IrqString)
188 {
189 /* Move past the actual string, to reach the rate */
190 IrqString += strlen("IRQ");
191
192 /* Now get past any spaces */
193 while (*IrqString == ' ') IrqString++;
194
195 /* And make sure we have an IRQ */
196 if (*IrqString)
197 {
198 /* Read and set it */
199 Value = atol(IrqString + 1);
200 if (Value) ComPortIrq = Value;
201 }
202 }
203 }
204
205 /* Get base address */
206 ComPortBase = UlongToPtr(BaseArray[ComPortNumber]);
207
208 /* Initialize the port */
209 return KdpPortInitialize();
210 }
211
212 VOID
213 NTAPI
214 KdpSendByte(IN BYTE Byte)
215 {
216 /* Wait for the port to be ready */
217 while ((READ_PORT_UCHAR(ComPortBase + COM_LSR) & LSR_TBE) == 0);
218
219 /* Send the byte */
220 WRITE_PORT_UCHAR(ComPortBase + COM_DAT, Byte);
221 }
222
223 KDP_STATUS
224 NTAPI
225 KdpPollByte(OUT PBYTE OutByte)
226 {
227 /* Check if data is available */
228 if ((READ_PORT_UCHAR(ComPortBase + COM_LSR) & LSR_DR))
229 {
230 /* Yes, return the byte */
231 *OutByte = READ_PORT_UCHAR(ComPortBase + COM_DAT);
232 return KDP_PACKET_RECEIVED;
233 }
234
235 /* Timed out */
236 return KDP_PACKET_TIMEOUT;
237 }
238
239 KDP_STATUS
240 NTAPI
241 KdpReceiveByte(OUT PBYTE OutByte)
242 {
243 ULONG Repeats = REPEAT_COUNT;
244
245 while (Repeats--)
246 {
247 /* Check if data is available */
248 if (KdpPollByte(OutByte) == KDP_PACKET_RECEIVED)
249 {
250 /* We successfully got a byte */
251 return KDP_PACKET_RECEIVED;
252 }
253 }
254
255 /* Timed out */
256 return KDP_PACKET_TIMEOUT;
257 }
258
259 KDP_STATUS
260 NTAPI
261 KdpPollBreakIn()
262 {
263 UCHAR Byte;
264 if (KdpPollByte(&Byte) == KDP_PACKET_RECEIVED)
265 {
266 if (Byte == BREAKIN_PACKET_BYTE)
267 {
268 return KDP_PACKET_RECEIVED;
269 }
270 }
271 return KDP_PACKET_TIMEOUT;
272 }
273
274 NTSTATUS
275 NTAPI
276 KdSave(
277 IN BOOLEAN SleepTransition)
278 {
279 /* Nothing to do on COM ports */
280 return STATUS_SUCCESS;
281 }
282
283 NTSTATUS
284 NTAPI
285 KdRestore(
286 IN BOOLEAN SleepTransition)
287 {
288 /* Nothing to do on COM ports */
289 return STATUS_SUCCESS;
290 }
291