]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Bus/Isa/IsaSerialDxe/Serial.h
6bef39b48b3bfb7505563f4278248b7f10ac98b5
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Isa / IsaSerialDxe / Serial.h
1 /*++
2
3 Copyright (c) 2006 - 2007, Intel Corporation<BR>
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12
13 Module Name:
14
15 serial.h
16
17 Abstract:
18
19 Include for Serial Driver
20
21 Revision History:
22
23 --*/
24
25 #ifndef _SERIAL_H
26 #define _SERIAL_H
27
28 //
29 // The package level header files this module uses
30 //
31 #include <PiDxe.h>
32 #include <FrameworkPei.h>
33 //
34 // The protocols, PPI and GUID defintions for this module
35 //
36 #include <Protocol/IsaIo.h>
37 #include <Protocol/SerialIo.h>
38 #include <Protocol/DevicePath.h>
39 //
40 // The Library classes this module consumes
41 //
42 #include <Library/DebugLib.h>
43 #include <Library/UefiDriverEntryPoint.h>
44 #include <Library/BaseLib.h>
45 #include <Library/UefiLib.h>
46 #include <Library/DevicePathLib.h>
47 #include <Library/BaseMemoryLib.h>
48 #include <Library/MemoryAllocationLib.h>
49 #include <Library/UefiBootServicesTableLib.h>
50 #include <Library/ReportStatusCodeLib.h>
51 #include <Library/PcdLib.h>
52 //
53 // Driver Binding Externs
54 //
55 extern EFI_DRIVER_BINDING_PROTOCOL gSerialControllerDriver;
56 extern EFI_COMPONENT_NAME_PROTOCOL gIsaSerialComponentName;
57
58 //
59 // Internal Data Structures
60 //
61 #define SERIAL_DEV_SIGNATURE EFI_SIGNATURE_32 ('s', 'e', 'r', 'd')
62 #define SERIAL_MAX_BUFFER_SIZE 16
63 #define TIMEOUT_STALL_INTERVAL 10
64
65 //
66 // Name: SERIAL_DEV_FIFO
67 // Purpose: To define Receive FIFO and Transmit FIFO
68 // Context: Used by serial data transmit and receive
69 // Fields:
70 // First UINT32: The index of the first data in array Data[]
71 // Last UINT32: The index, which you can put a new data into array Data[]
72 // Surplus UINT32: Identify how many data you can put into array Data[]
73 // Data[] UINT8 : An array, which used to store data
74 //
75 typedef struct {
76 UINT32 First;
77 UINT32 Last;
78 UINT32 Surplus;
79 UINT8 Data[SERIAL_MAX_BUFFER_SIZE];
80 } SERIAL_DEV_FIFO;
81
82 typedef enum {
83 UART8250 = 0,
84 UART16450 = 1,
85 UART16550 = 2,
86 UART16550A= 3
87 } EFI_UART_TYPE;
88
89 //
90 // Name: SERIAL_DEV
91 // Purpose: To provide device specific information
92 // Context:
93 // Fields:
94 // Signature UINTN: The identity of the serial device
95 // SerialIo SERIAL_IO_PROTOCOL: Serial I/O protocol interface
96 // SerialMode SERIAL_IO_MODE:
97 // DevicePath EFI_DEVICE_PATH_PROTOCOL *: Device path of the serial device
98 // Handle EFI_HANDLE: The handle instance attached to serial device
99 // BaseAddress UINT16: The base address of specific serial device
100 // Receive SERIAL_DEV_FIFO: The FIFO used to store data,
101 // which is received by UART
102 // Transmit SERIAL_DEV_FIFO: The FIFO used to store data,
103 // which you want to transmit by UART
104 // SoftwareLoopbackEnable BOOLEAN:
105 // Type EFI_UART_TYPE: Specify the UART type of certain serial device
106 //
107 typedef struct {
108 UINTN Signature;
109
110 EFI_HANDLE Handle;
111 EFI_SERIAL_IO_PROTOCOL SerialIo;
112 EFI_SERIAL_IO_MODE SerialMode;
113 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
114
115 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
116 UART_DEVICE_PATH UartDevicePath;
117 EFI_ISA_IO_PROTOCOL *IsaIo;
118
119 UINT16 BaseAddress;
120 SERIAL_DEV_FIFO Receive;
121 SERIAL_DEV_FIFO Transmit;
122 BOOLEAN SoftwareLoopbackEnable;
123 BOOLEAN HardwareFlowControl;
124 EFI_UART_TYPE Type;
125 EFI_UNICODE_STRING_TABLE *ControllerNameTable;
126 } SERIAL_DEV;
127
128 #include "ComponentName.h"
129
130 #define SERIAL_DEV_FROM_THIS(a) CR (a, SERIAL_DEV, SerialIo, SERIAL_DEV_SIGNATURE)
131
132 //
133 // Globale Variables
134 //
135 extern EFI_DRIVER_BINDING_PROTOCOL gSerialControllerDriver;
136
137 //
138 // Serial Driver Defaults
139 //
140 #define SERIAL_PORT_DEFAULT_BAUD_RATE 115200
141 #define SERIAL_PORT_DEFAULT_RECEIVE_FIFO_DEPTH 1
142 #define SERIAL_PORT_DEFAULT_TIMEOUT 1000000
143 #define SERIAL_PORT_DEFAULT_PARITY NoParity
144 #define SERIAL_PORT_DEFAULT_DATA_BITS 8
145 #define SERIAL_PORT_DEFAULT_STOP_BITS 1
146 #define SERIAL_PORT_DEFAULT_CONTROL_MASK 0
147
148 //
149 // (24000000/13)MHz input clock
150 //
151 #define SERIAL_PORT_INPUT_CLOCK 1843200
152
153 //
154 // 115200 baud with rounding errors
155 //
156 #define SERIAL_PORT_MAX_BAUD_RATE 115400
157 #define SERIAL_PORT_MIN_BAUD_RATE 50
158
159 #define SERIAL_PORT_MAX_RECEIVE_FIFO_DEPTH 16
160 #define SERIAL_PORT_MIN_TIMEOUT 1 // 1 uS
161 #define SERIAL_PORT_MAX_TIMEOUT 100000000 // 100 seconds
162 //
163 // UART Registers
164 //
165 #define SERIAL_REGISTER_THR 0 // WO Transmit Holding Register
166 #define SERIAL_REGISTER_RBR 0 // RO Receive Buffer Register
167 #define SERIAL_REGISTER_DLL 0 // R/W Divisor Latch LSB
168 #define SERIAL_REGISTER_DLM 1 // R/W Divisor Latch MSB
169 #define SERIAL_REGISTER_IER 1 // R/W Interrupt Enable Register
170 #define SERIAL_REGISTER_IIR 2 // RO Interrupt Identification Register
171 #define SERIAL_REGISTER_FCR 2 // WO FIFO Cotrol Register
172 #define SERIAL_REGISTER_LCR 3 // R/W Line Control Register
173 #define SERIAL_REGISTER_MCR 4 // R/W Modem Control Register
174 #define SERIAL_REGISTER_LSR 5 // R/W Line Status Register
175 #define SERIAL_REGISTER_MSR 6 // R/W Modem Status Register
176 #define SERIAL_REGISTER_SCR 7 // R/W Scratch Pad Register
177 #pragma pack(1)
178 //
179 // Name: SERIAL_PORT_IER_BITS
180 // Purpose: Define each bit in Interrupt Enable Register
181 // Context:
182 // Fields:
183 // RAVIE Bit0: Receiver Data Available Interrupt Enable
184 // THEIE Bit1: Transmistter Holding Register Empty Interrupt Enable
185 // RIE Bit2: Receiver Interrupt Enable
186 // MIE Bit3: Modem Interrupt Enable
187 // Reserved Bit4-Bit7: Reserved
188 //
189 typedef struct {
190 UINT8 RAVIE : 1;
191 UINT8 THEIE : 1;
192 UINT8 RIE : 1;
193 UINT8 MIE : 1;
194 UINT8 Reserved : 4;
195 } SERIAL_PORT_IER_BITS;
196
197 //
198 // Name: SERIAL_PORT_IER
199 // Purpose:
200 // Context:
201 // Fields:
202 // Bits SERIAL_PORT_IER_BITS: Bits of the IER
203 // Data UINT8: the value of the IER
204 //
205 typedef union {
206 SERIAL_PORT_IER_BITS Bits;
207 UINT8 Data;
208 } SERIAL_PORT_IER;
209
210 //
211 // Name: SERIAL_PORT_IIR_BITS
212 // Purpose: Define each bit in Interrupt Identification Register
213 // Context:
214 // Fields:
215 // IPS Bit0: Interrupt Pending Status
216 // IIB Bit1-Bit3: Interrupt ID Bits
217 // Reserved Bit4-Bit5: Reserved
218 // FIFOES Bit6-Bit7: FIFO Mode Enable Status
219 //
220 typedef struct {
221 UINT8 IPS : 1;
222 UINT8 IIB : 3;
223 UINT8 Reserved : 2;
224 UINT8 FIFOES : 2;
225 } SERIAL_PORT_IIR_BITS;
226
227 //
228 // Name: SERIAL_PORT_IIR
229 // Purpose:
230 // Context:
231 // Fields:
232 // Bits SERIAL_PORT_IIR_BITS: Bits of the IIR
233 // Data UINT8: the value of the IIR
234 //
235 typedef union {
236 SERIAL_PORT_IIR_BITS Bits;
237 UINT8 Data;
238 } SERIAL_PORT_IIR;
239
240 //
241 // Name: SERIAL_PORT_FCR_BITS
242 // Purpose: Define each bit in FIFO Control Register
243 // Context:
244 // Fields:
245 // TRFIFOE Bit0: Transmit and Receive FIFO Enable
246 // RESETRF Bit1: Reset Reciever FIFO
247 // RESETTF Bit2: Reset Transmistter FIFO
248 // DMS Bit3: DMA Mode Select
249 // Reserved Bit4-Bit5: Reserved
250 // RTB Bit6-Bit7: Receive Trigger Bits
251 //
252 typedef struct {
253 UINT8 TRFIFOE : 1;
254 UINT8 RESETRF : 1;
255 UINT8 RESETTF : 1;
256 UINT8 DMS : 1;
257 UINT8 Reserved : 2;
258 UINT8 RTB : 2;
259 } SERIAL_PORT_FCR_BITS;
260
261 //
262 // Name: SERIAL_PORT_FCR
263 // Purpose:
264 // Context:
265 // Fields:
266 // Bits SERIAL_PORT_FCR_BITS: Bits of the FCR
267 // Data UINT8: the value of the FCR
268 //
269 typedef union {
270 SERIAL_PORT_FCR_BITS Bits;
271 UINT8 Data;
272 } SERIAL_PORT_FCR;
273
274 //
275 // Name: SERIAL_PORT_LCR_BITS
276 // Purpose: Define each bit in Line Control Register
277 // Context:
278 // Fields:
279 // SERIALDB Bit0-Bit1: Number of Serial Data Bits
280 // STOPB Bit2: Number of Stop Bits
281 // PAREN Bit3: Parity Enable
282 // EVENPAR Bit4: Even Parity Select
283 // STICPAR Bit5: Sticky Parity
284 // BRCON Bit6: Break Control
285 // DLAB Bit7: Divisor Latch Access Bit
286 //
287 typedef struct {
288 UINT8 SERIALDB : 2;
289 UINT8 STOPB : 1;
290 UINT8 PAREN : 1;
291 UINT8 EVENPAR : 1;
292 UINT8 STICPAR : 1;
293 UINT8 BRCON : 1;
294 UINT8 DLAB : 1;
295 } SERIAL_PORT_LCR_BITS;
296
297 //
298 // Name: SERIAL_PORT_LCR
299 // Purpose:
300 // Context:
301 // Fields:
302 // Bits SERIAL_PORT_LCR_BITS: Bits of the LCR
303 // Data UINT8: the value of the LCR
304 //
305 typedef union {
306 SERIAL_PORT_LCR_BITS Bits;
307 UINT8 Data;
308 } SERIAL_PORT_LCR;
309
310 //
311 // Name: SERIAL_PORT_MCR_BITS
312 // Purpose: Define each bit in Modem Control Register
313 // Context:
314 // Fields:
315 // DTRC Bit0: Data Terminal Ready Control
316 // RTS Bit1: Request To Send Control
317 // OUT1 Bit2: Output1
318 // OUT2 Bit3: Output2, used to disable interrupt
319 // LME; Bit4: Loopback Mode Enable
320 // Reserved Bit5-Bit7: Reserved
321 //
322 typedef struct {
323 UINT8 DTRC : 1;
324 UINT8 RTS : 1;
325 UINT8 OUT1 : 1;
326 UINT8 OUT2 : 1;
327 UINT8 LME : 1;
328 UINT8 Reserved : 3;
329 } SERIAL_PORT_MCR_BITS;
330
331 //
332 // Name: SERIAL_PORT_MCR
333 // Purpose:
334 // Context:
335 // Fields:
336 // Bits SERIAL_PORT_MCR_BITS: Bits of the MCR
337 // Data UINT8: the value of the MCR
338 //
339 typedef union {
340 SERIAL_PORT_MCR_BITS Bits;
341 UINT8 Data;
342 } SERIAL_PORT_MCR;
343
344 //
345 // Name: SERIAL_PORT_LSR_BITS
346 // Purpose: Define each bit in Line Status Register
347 // Context:
348 // Fields:
349 // DR Bit0: Receiver Data Ready Status
350 // OE Bit1: Overrun Error Status
351 // PE Bit2: Parity Error Status
352 // FE Bit3: Framing Error Status
353 // BI Bit4: Break Interrupt Status
354 // THRE Bit5: Transmistter Holding Register Status
355 // TEMT Bit6: Transmitter Empty Status
356 // FIFOE Bit7: FIFO Error Status
357 //
358 typedef struct {
359 UINT8 DR : 1;
360 UINT8 OE : 1;
361 UINT8 PE : 1;
362 UINT8 FE : 1;
363 UINT8 BI : 1;
364 UINT8 THRE : 1;
365 UINT8 TEMT : 1;
366 UINT8 FIFOE : 1;
367 } SERIAL_PORT_LSR_BITS;
368
369 //
370 // Name: SERIAL_PORT_LSR
371 // Purpose:
372 // Context:
373 // Fields:
374 // Bits SERIAL_PORT_LSR_BITS: Bits of the LSR
375 // Data UINT8: the value of the LSR
376 //
377 typedef union {
378 SERIAL_PORT_LSR_BITS Bits;
379 UINT8 Data;
380 } SERIAL_PORT_LSR;
381
382 //
383 // Name: SERIAL_PORT_MSR_BITS
384 // Purpose: Define each bit in Modem Status Register
385 // Context:
386 // Fields:
387 // DeltaCTS Bit0: Delta Clear To Send Status
388 // DeltaDSR Bit1: Delta Data Set Ready Status
389 // TrailingEdgeRI Bit2: Trailing Edge of Ring Indicator Status
390 // DeltaDCD Bit3: Delta Data Carrier Detect Status
391 // CTS Bit4: Clear To Send Status
392 // DSR Bit5: Data Set Ready Status
393 // RI Bit6: Ring Indicator Status
394 // DCD Bit7: Data Carrier Detect Status
395 //
396 typedef struct {
397 UINT8 DeltaCTS : 1;
398 UINT8 DeltaDSR : 1;
399 UINT8 TrailingEdgeRI : 1;
400 UINT8 DeltaDCD : 1;
401 UINT8 CTS : 1;
402 UINT8 DSR : 1;
403 UINT8 RI : 1;
404 UINT8 DCD : 1;
405 } SERIAL_PORT_MSR_BITS;
406
407 //
408 // Name: SERIAL_PORT_MSR
409 // Purpose:
410 // Context:
411 // Fields:
412 // Bits SERIAL_PORT_MSR_BITS: Bits of the MSR
413 // Data UINT8: the value of the MSR
414 //
415 typedef union {
416 SERIAL_PORT_MSR_BITS Bits;
417 UINT8 Data;
418 } SERIAL_PORT_MSR;
419
420 #pragma pack()
421 //
422 // Define serial register I/O macros
423 //
424 #define READ_RBR(IO, B) IsaSerialReadPort (IO, B, SERIAL_REGISTER_RBR)
425 #define READ_DLL(IO, B) IsaSerialReadPort (IO, B, SERIAL_REGISTER_DLL)
426 #define READ_DLM(IO, B) IsaSerialReadPort (IO, B, SERIAL_REGISTER_DLM)
427 #define READ_IER(IO, B) IsaSerialReadPort (IO, B, SERIAL_REGISTER_IER)
428 #define READ_IIR(IO, B) IsaSerialReadPort (IO, B, SERIAL_REGISTER_IIR)
429 #define READ_LCR(IO, B) IsaSerialReadPort (IO, B, SERIAL_REGISTER_LCR)
430 #define READ_MCR(IO, B) IsaSerialReadPort (IO, B, SERIAL_REGISTER_MCR)
431 #define READ_LSR(IO, B) IsaSerialReadPort (IO, B, SERIAL_REGISTER_LSR)
432 #define READ_MSR(IO, B) IsaSerialReadPort (IO, B, SERIAL_REGISTER_MSR)
433 #define READ_SCR(IO, B) IsaSerialReadPort (IO, B, SERIAL_REGISTER_SCR)
434
435 #define WRITE_THR(IO, B, D) IsaSerialWritePort (IO, B, SERIAL_REGISTER_THR, D)
436 #define WRITE_DLL(IO, B, D) IsaSerialWritePort (IO, B, SERIAL_REGISTER_DLL, D)
437 #define WRITE_DLM(IO, B, D) IsaSerialWritePort (IO, B, SERIAL_REGISTER_DLM, D)
438 #define WRITE_IER(IO, B, D) IsaSerialWritePort (IO, B, SERIAL_REGISTER_IER, D)
439 #define WRITE_FCR(IO, B, D) IsaSerialWritePort (IO, B, SERIAL_REGISTER_FCR, D)
440 #define WRITE_LCR(IO, B, D) IsaSerialWritePort (IO, B, SERIAL_REGISTER_LCR, D)
441 #define WRITE_MCR(IO, B, D) IsaSerialWritePort (IO, B, SERIAL_REGISTER_MCR, D)
442 #define WRITE_LSR(IO, B, D) IsaSerialWritePort (IO, B, SERIAL_REGISTER_LSR, D)
443 #define WRITE_MSR(IO, B, D) IsaSerialWritePort (IO, B, SERIAL_REGISTER_MSR, D)
444 #define WRITE_SCR(IO, B, D) IsaSerialWritePort (IO, B, SERIAL_REGISTER_SCR, D)
445
446 //
447 // Prototypes
448 // Driver model protocol interface
449 //
450
451 EFI_STATUS
452 EFIAPI
453 SerialControllerDriverSupported (
454 IN EFI_DRIVER_BINDING_PROTOCOL *This,
455 IN EFI_HANDLE Controller,
456 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
457 );
458
459 EFI_STATUS
460 EFIAPI
461 SerialControllerDriverStart (
462 IN EFI_DRIVER_BINDING_PROTOCOL *This,
463 IN EFI_HANDLE Controller,
464 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
465 );
466
467 EFI_STATUS
468 EFIAPI
469 SerialControllerDriverStop (
470 IN EFI_DRIVER_BINDING_PROTOCOL *This,
471 IN EFI_HANDLE Controller,
472 IN UINTN NumberOfChildren,
473 IN EFI_HANDLE *ChildHandleBuffer
474 );
475
476 //
477 // Serial I/O Protocol Interface
478 //
479 EFI_STATUS
480 EFIAPI
481 IsaSerialReset (
482 IN EFI_SERIAL_IO_PROTOCOL *This
483 );
484
485 EFI_STATUS
486 EFIAPI
487 IsaSerialSetAttributes (
488 IN EFI_SERIAL_IO_PROTOCOL *This,
489 IN UINT64 BaudRate,
490 IN UINT32 ReceiveFifoDepth,
491 IN UINT32 Timeout,
492 IN EFI_PARITY_TYPE Parity,
493 IN UINT8 DataBits,
494 IN EFI_STOP_BITS_TYPE StopBits
495 );
496
497 EFI_STATUS
498 EFIAPI
499 IsaSerialSetControl (
500 IN EFI_SERIAL_IO_PROTOCOL *This,
501 IN UINT32 Control
502 );
503
504 EFI_STATUS
505 EFIAPI
506 IsaSerialGetControl (
507 IN EFI_SERIAL_IO_PROTOCOL *This,
508 OUT UINT32 *Control
509 );
510
511 EFI_STATUS
512 EFIAPI
513 IsaSerialWrite (
514 IN EFI_SERIAL_IO_PROTOCOL *This,
515 IN OUT UINTN *BufferSize,
516 IN VOID *Buffer
517 );
518
519 EFI_STATUS
520 EFIAPI
521 IsaSerialRead (
522 IN EFI_SERIAL_IO_PROTOCOL *This,
523 IN OUT UINTN *BufferSize,
524 OUT VOID *Buffer
525 );
526
527 //
528 // Internal Functions
529 //
530 BOOLEAN
531 IsaSerialPortPresent (
532 IN SERIAL_DEV *SerialDevice
533 );
534
535 BOOLEAN
536 IsaSerialFifoFull (
537 IN SERIAL_DEV_FIFO *Fifo
538 );
539
540 BOOLEAN
541 IsaSerialFifoEmpty (
542 IN SERIAL_DEV_FIFO *Fifo
543 );
544
545 EFI_STATUS
546 IsaSerialFifoAdd (
547 IN SERIAL_DEV_FIFO *Fifo,
548 IN UINT8 Data
549 );
550
551 EFI_STATUS
552 IsaSerialFifoRemove (
553 IN SERIAL_DEV_FIFO *Fifo,
554 OUT UINT8 *Data
555 );
556
557 EFI_STATUS
558 IsaSerialReceiveTransmit (
559 IN SERIAL_DEV *SerialDevice
560 );
561
562 UINT8
563 IsaSerialReadPort (
564 IN EFI_ISA_IO_PROTOCOL *IsaIo,
565 IN UINT16 BaseAddress,
566 IN UINT32 Offset
567 );
568
569 VOID
570 IsaSerialWritePort (
571 IN EFI_ISA_IO_PROTOCOL *IsaIo,
572 IN UINT16 BaseAddress,
573 IN UINT32 Offset,
574 IN UINT8 Data
575 );
576
577 #endif