-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart16550.c
721 lines (500 loc) · 16.8 KB
/
uart16550.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
Module Name:
uart16550.c
Abstract:
This module contains support for the standard 16550 serial UART.
--*/
// ------------------------------------------------------------------- Includes
#include "common.h"
#include "kdcom.h"
// ----------------------------------------------- Internal Function Prototypes
BOOLEAN
Uart16550SetBaud (
_Inout_ PCPPORT Port,
ULONG Rate
);
// ------------------------------------------------------------------ Functions
BOOLEAN
Uart16550InitializePortCommon (
_In_opt_ _Null_terminated_ PCHAR LoadOptions,
_Inout_ PCPPORT Port,
BOOLEAN MemoryMapped,
UCHAR AccessSize,
UCHAR BitWidth
)
/*++
Routine Description:
This routine performs the common initialization of a 16550 serial UART.
Arguments:
LoadOptions - Optional load option string. Currently unused.
Port - Supplies a pointer to a CPPORT object which will be filled in as
part of the port initialization.
MemoryMapped - Supplies a boolean which indicates if the UART is accessed
through memory-mapped registers or legacy port I/O.
AccessSize - Supplies an ACPI Generic Access Size value which indicates the
type of bus access that should be performed when accessing the UART.
BitWidth - Supplies a number indicating how wide the UART's registers are.
Return Value:
TRUE if the port has been successfully initialized, FALSE otherwise.
--*/
{
UCHAR RegisterValue;
UNREFERENCED_PARAMETER(LoadOptions);
//
// Set the Read / Write function pointers for this serial port.
//
UartpSetAccess(Port, MemoryMapped, AccessSize, BitWidth);
//
// Set DLAB to zero. The DLAB controls the meaning of the first two
// registers. When zero, the first register is used for all byte transfers
// and the second register controls device interrupts.
//
RegisterValue = Port->Read(Port, COM_LCR);
RegisterValue &= ~LC_DLAB;
Port->Write(Port, COM_LCR, RegisterValue);
//
// Disable device interrupts. This implementation will handle state
// transitions by request only.
//
Port->Write(Port, COM_IEN, 0);
//
// Reset and disable the FIFO queue.
// N.B. FIFO will be reenabled before returning from this routine.
//
Port->Write(Port, COM_FCR, FC_CLEAR_TRANSMIT | FC_CLEAR_RECEIVE);
//
// Configure the baud rate and mode.
//
Uart16550SetBaud(Port, Port->BaudRate);
//
// Enable the FIFO.
//
Port->Write(Port, COM_FCR, FC_ENABLE);
//
// Assert DTR, RTS. Disable loopback. Indicate to the device that
// we are able to send and receive data.
//
Port->Write(Port, COM_MCR, MC_DTRRTS);
//
// Initialize ring indicator bit based on hardware state.
//
RegisterValue = Port->Read(Port, COM_MSR);
if (CHECK_FLAG(RegisterValue, SERIAL_MSR_RI)) {
Port->Flags |= PORT_RING_INDICATOR;
}
return TRUE;
}
BOOLEAN
Uart16550LegacyInitializePort (
_In_opt_ _Null_terminated_ PCHAR LoadOptions,
_Inout_ PCPPORT Port,
BOOLEAN MemoryMapped,
UCHAR AccessSize,
UCHAR BitWidth
)
/*++
Routine Description:
This routine initializes the 16550 UART as a legacy serial port. In this
case, legacy refers to the fact that the serial port is bound to one of the
four well-known PC I/O port addresses (selected with a COM port number),
has an access size of one byte, and a register width of 8 bits. This
routine is intended for use with manually (user) specified COM ports.
Arguments:
LoadOptions - Optional load option string. Currently unused.
Port - Supplies a pointer to a CPPORT object which will be filled in as
part of the port initialization.
MemoryMapped - Unused.
AccessSize - Unused.
BitWidth - Unused.
Return Value:
TRUE if the port has been successfully initialized, FALSE otherwise.
--*/
{
UNREFERENCED_PARAMETER(AccessSize);
UNREFERENCED_PARAMETER(BitWidth);
UNREFERENCED_PARAMETER(MemoryMapped);
switch ((ULONG_PTR)Port->Address) {
case 1:
Port->Address = (PUCHAR)COM1_PORT;
break;
case 2:
Port->Address = (PUCHAR)COM2_PORT;
break;
case 3:
Port->Address = (PUCHAR)COM3_PORT;
break;
case 4:
Port->Address = (PUCHAR)COM4_PORT;
break;
default:
return FALSE;
}
Port->Flags = 0;
return Uart16550InitializePortCommon(LoadOptions,
Port,
FALSE,
AcpiGenericAccessSizeByte,
8);
}
BOOLEAN
Uart16550InitializePort (
_In_opt_ _Null_terminated_ PCHAR LoadOptions,
_Inout_ PCPPORT Port,
BOOLEAN MemoryMapped,
UCHAR AccessSize,
UCHAR BitWidth
)
/*++
Routine Description:
This routine initializes the 16550 serial UART using a base address and
address space ID that originated in an ACPI Generic Address Structure. This
routine is intended for use with DBGP-compatible serial ports.
Arguments:
LoadOptions - Optional load option string. Currently unused.
Port - Supplies a pointer to a CPPORT object which will be filled in as
part of the port initialization.
MemoryMapped - Supplies a boolean which indicates if the UART is accessed
through memory-mapped registers or legacy port I/O.
AccessSize - Unused.
BitWidth - Unused.
Return Value:
TRUE if the port has been successfully initialized, FALSE otherwise.
--*/
{
UCHAR RegisterBitWidth;
UNREFERENCED_PARAMETER(AccessSize);
UNREFERENCED_PARAMETER(BitWidth);
#if defined(_ARM_) || defined(_ARM64_)
//
// ARM-based systems are by definition MMIO and tend to space the ports
// apart at 4-byte intervals. Expand to DWORD boundaries for ARM devices.
//
RegisterBitWidth = 32;
if (MemoryMapped == FALSE) {
return FALSE;
}
//
// ARM-based systems typically have non-standard dividends for baud rate.
// It is deemed safest to assume the UEFI firmware has already set up
// the serial port properly and just inherit the baud rate that is already
// set.
//
Port->Flags = PORT_DEFAULT_RATE;
#else
RegisterBitWidth = 8;
Port->Flags = 0;
#endif
return Uart16550InitializePortCommon(LoadOptions,
Port,
MemoryMapped,
AcpiGenericAccessSizeByte,
RegisterBitWidth);
}
BOOLEAN
Uart16550MmInitializePort (
_In_opt_ _Null_terminated_ PCHAR LoadOptions,
_Inout_ PCPPORT Port,
BOOLEAN MemoryMapped,
UCHAR AccessSize,
UCHAR BitWidth
)
/*++
Routine Description:
This routine initializes the 16550 UART as a memory-mapped serial port.
The base address, access size, and bit width are all respected by this
type of serial port. This routine is intended for use with DBG2-compatible
serial ports.
Arguments:
LoadOptions - Optional load option string. Currently unused.
Port - Supplies a pointer to a CPPORT object which will be filled in as
part of the port initialization.
MemoryMapped - Supplies a boolean which indicates if the UART is accessed
through memory-mapped registers or legacy port I/O.
AccessSize - Supplies an ACPI Generic Access Size value which indicates the
type of bus access that should be performed when accessing the UART.
BitWidth - Supplies a number indicating how wide the UART's registers are.
Return Value:
TRUE if the port has been successfully initialized, FALSE otherwise.
--*/
{
Port->Flags = PORT_DEFAULT_RATE;
return Uart16550InitializePortCommon(LoadOptions,
Port,
MemoryMapped,
AccessSize,
BitWidth);
}
BOOLEAN
Uart16550SetBaudCommon (
_Inout_ PCPPORT Port,
ULONG Rate,
ULONG Clock
)
/*++
Routine Description:
Set the baud rate for the UART hardware and record it in the port object.
Arguments:
Port - Supplies the address of the port object that describes the UART.
Rate - Supplies the desired baud rate in bits per second.
Clock - Supplies the base clock frequency of the UART in Hz.
Return Value:
TRUE if the baud rate was programmed, FALSE if it was not.
--*/
{
UCHAR Lcr;
if ((Port == NULL) || (Port->Address == NULL)) {
return FALSE;
}
if ((Rate == 0) || (Clock == 0)) {
return FALSE;
}
//
// A device's baud rate is written to DLL and DLM. The values of these
// registers are the resultant when the max rate (clock) is divided by the
// device's desired operating rate.
//
const ULONG DivisorLatch = Clock / Rate;
//
// Set the divisor latch access bit (DLAB) in the line control register.
// When non-zero, the first two registers become DLL and DLM.
//
Lcr = Port->Read(Port, COM_LCR);
Lcr |= LC_DLAB;
Port->Write(Port, COM_LCR, Lcr);
//
// Set the divisor latch value (MSB first, then LSB).
//
Port->Write(Port, COM_DLM, (UCHAR)((DivisorLatch >> 8) & 0xFF));
Port->Write(Port, COM_DLL, (UCHAR)(DivisorLatch & 0xFF));
//
// Set Line Control Register to 8N1 (no parity, 8 data bits, 1 stop bit).
// This also sets the DLAB back to zero.
//
Port->Write(Port, COM_LCR, 3);
//
// Remember the baud rate.
//
Port->BaudRate = Rate;
return TRUE;
}
BOOLEAN
Uart16550SetBaud (
_Inout_ PCPPORT Port,
ULONG Rate
)
/*++
Routine Description:
Set the baud rate for the UART hardware and record it in the port object.
Arguments:
Port - Supplies the address of the port object that describes the UART.
Rate - Supplies the desired baud rate in bits per second.
Return Value:
TRUE if the baud rate was programmed, FALSE if it was not.
--*/
{
if (CHECK_FLAG(Port->Flags, PORT_DEFAULT_RATE)) {
return FALSE;
}
return Uart16550SetBaudCommon(Port, Rate, CLOCK_RATE);
}
UART_STATUS
Uart16550GetByte (
_Inout_ PCPPORT Port,
_Out_ PUCHAR Byte
)
/*++
Routine Description:
Fetch a data byte from the UART device and return it.
Arguments:
Port - Supplies the address of the port object that describes the UART.
Byte - Supplies the address of variable to hold the result.
Return Value:
UART_STATUS code.
--*/
{
UCHAR Data;
UCHAR Lsr;
UCHAR Msr;
if ((Port == NULL) || (Port->Address == NULL)) {
return UartNotReady;
}
//
// Check to see if all bits are set in LSR. If this is the case, it means
// the port I/O address is invalid as 0xFF is nonsense for LSR.
//
Lsr = Port->Read(Port, COM_LSR);
if (Lsr == SERIAL_LSR_NOT_PRESENT) {
return UartNotReady;
}
if (CHECK_FLAG(Lsr, COM_DATRDY)) {
//
// Return unsuccessfully if any errors are indicated by the
// LSR.
//
if (CHECK_FLAG(Lsr, COM_PE) ||
CHECK_FLAG(Lsr, COM_FE) ||
CHECK_FLAG(Lsr, COM_OE)) {
return UartError;
}
Data = Port->Read(Port, COM_DAT);
//
// When using modem control, ignore any bytes that don't have
// the carrier detect flag set.
//
if (CHECK_FLAG(Port->Flags, PORT_MODEM_CONTROL)) {
Msr = Port->Read(Port, COM_MSR);
if (CHECK_FLAG(Msr, MS_CD) == FALSE) {
return UartNoData;
}
}
*Byte = Data;
return UartSuccess;
} else {
//
// Data is not available. Determine if the ring indicator has toggled.
// If so, enable modem control.
//
Msr = Port->Read(Port, COM_MSR);
if ((CHECK_FLAG(Port->Flags, PORT_RING_INDICATOR) &&
!CHECK_FLAG(Msr, SERIAL_MSR_RI)) ||
(!CHECK_FLAG(Port->Flags, PORT_RING_INDICATOR) &&
CHECK_FLAG(Msr, SERIAL_MSR_RI))) {
Port->Flags |= PORT_MODEM_CONTROL;
}
return UartNoData;
}
}
UART_STATUS
Uart16550PutByte (
_Inout_ PCPPORT Port,
UCHAR Byte,
BOOLEAN BusyWait
)
/*++
Routine Description:
Write a data byte out to the UART device.
Arguments:
Port - Supplies the address of the port object that describes the UART.
Byte - Supplies the data to emit.
BusyWait - Supplies a flag to control whether this routine will busy
wait (spin) for the UART hardware to be ready to transmit.
--*/
{
UCHAR Lsr;
UCHAR Msr;
if ((Port == NULL) || (Port->Address == NULL)) {
return UartNotReady;
}
//
// When using modem control, DSR, CTS, and CD flags must all be set before
// sending any data.
//
if (CHECK_FLAG(Port->Flags, PORT_MODEM_CONTROL)) {
Msr = Port->Read(Port, COM_MSR);
while ((Msr & MS_DSRCTSCD) != MS_DSRCTSCD) {
//
// If there's a byte ready, discard it from the input queue.
//
if (!CHECK_FLAG(Msr, MS_CD)) {
Lsr = Port->Read(Port, COM_LSR);
if (CHECK_FLAG(Port->Flags, COM_DATRDY)) {
Port->Read(Port, COM_DAT);
}
}
Msr = Port->Read(Port, COM_MSR);
}
}
//
// Check to see if all bits are set in LSR. If this is the case, it means
// the port I/O address is invalid as 0xFF is nonsense for LSR. This
// prevents writing a byte to non-existent hardware.
//
Lsr = Port->Read(Port, COM_LSR);
if (Lsr == SERIAL_LSR_NOT_PRESENT) {
return UartNotReady;
}
//
// The port must be ready to accept a byte for output before continuing.
//
while (!CHECK_FLAG(Lsr, COM_OUTRDY)) {
//
// Determine if the ring indicator has toggled.
// If so, enable modem control.
//
Msr = Port->Read(Port, COM_MSR);
if ((CHECK_FLAG(Port->Flags, PORT_RING_INDICATOR) &&
!CHECK_FLAG(Msr, SERIAL_MSR_RI)) ||
(!CHECK_FLAG(Port->Flags, PORT_RING_INDICATOR) &&
CHECK_FLAG(Msr, SERIAL_MSR_RI))) {
Port->Flags |= PORT_MODEM_CONTROL;
}
if (BusyWait == FALSE) {
return UartNotReady;
}
Lsr = Port->Read(Port, COM_LSR);
}
//
// Transmitter holding register is empty. Send the byte.
//
Port->Write(Port, COM_DAT, Byte);
return UartSuccess;
}
BOOLEAN
Uart16550RxReady (
_Inout_ PCPPORT Port
)
/*++
Routine Description:
This routine determines if there is data pending in the UART.
Arguments:
Port - Supplies the address of the port object that describes the UART.
Return Value:
TRUE if data is available, FALSE otherwise.
--*/
{
UCHAR Lsr;
if ((Port == NULL) || (Port->Address == NULL)) {
return FALSE;
}
//
// Check to see if all bits are set in LSR. If this is the case, it means
// the port I/O address is invalid as 0xFF is nonsense for LSR. This
// prevents the DATRDY check below from returning TRUE, which could cause
// a caller to think that data is pending when in actuality there is no
// UART present.
//
Lsr = Port->Read(Port, COM_LSR);
if (Lsr == SERIAL_LSR_NOT_PRESENT) {
return FALSE;
}
//
// Look at the Line Status Register to determine if there is pending data.
//
if (CHECK_FLAG(Lsr, COM_DATRDY)) {
return TRUE;
}
return FALSE;
}
// -------------------------------------------------------------------- Globals
UART_HARDWARE_DRIVER Legacy16550HardwareDriver = {
Uart16550LegacyInitializePort,
Uart16550SetBaud,
Uart16550GetByte,
Uart16550PutByte,
Uart16550RxReady
};
UART_HARDWARE_DRIVER Uart16550HardwareDriver = {
Uart16550InitializePort,
Uart16550SetBaud,
Uart16550GetByte,
Uart16550PutByte,
Uart16550RxReady
};
UART_HARDWARE_DRIVER MM16550HardwareDriver = {
Uart16550MmInitializePort,
Uart16550SetBaud,
Uart16550GetByte,
Uart16550PutByte,
Uart16550RxReady
};