Cleanup SerailIO drivers to have a device path and use PCD settings for various stuff...
[mirror_edk2.git] / EmbeddedPkg / SerialDxe / SerialIo.c
1 /** @file
2 Serial IO Abstraction for GDB stub. This allows an EFI consoles that shows up on the system
3 running GDB. One consle for error information and another console for user input/output.
4
5 Basic packet format is $packet-data#checksum. So every comand has 4 bytes of overhead: $,
6 #, 0, 0. The 0 and 0 are the ascii characters for the checksum.
7
8
9 Copyright (c) 2008-2009, Apple Inc. All rights reserved.
10
11 All rights reserved. This program and the accompanying materials
12 are licensed and made available under the terms and conditions of the BSD License
13 which accompanies this distribution. The full text of the license may be found at
14 http://opensource.org/licenses/bsd-license.php
15
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18
19 **/
20
21 #include <PiDxe.h>
22 #include <Library/UefiLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
24 #include <Library/DebugLib.h>
25 #include <Library/SerialPortLib.h>
26 #include <Library/PcdLib.h>
27
28 #include <Protocol/SerialIo.h>
29
30 /**
31 Reset the serial device.
32
33 @param This Protocol instance pointer.
34
35 @retval EFI_SUCCESS The device was reset.
36 @retval EFI_DEVICE_ERROR The serial device could not be reset.
37
38 **/
39 EFI_STATUS
40 EFIAPI
41 SerialReset (
42 IN EFI_SERIAL_IO_PROTOCOL *This
43 )
44 {
45 SerialPortInitialize ();
46 return EFI_SUCCESS;
47 }
48
49
50 /**
51 Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
52 data buts, and stop bits on a serial device.
53
54 @param This Protocol instance pointer.
55 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the
56 device's default interface speed.
57 @param ReveiveFifoDepth The requested depth of the FIFO on the receive side of the
58 serial interface. A ReceiveFifoDepth value of 0 will use
59 the device's dfault FIFO depth.
60 @param Timeout The requested time out for a single character in microseconds.
61 This timeout applies to both the transmit and receive side of the
62 interface. A Timeout value of 0 will use the device's default time
63 out value.
64 @param Parity The type of parity to use on this serial device. A Parity value of
65 DefaultParity will use the device's default parity value.
66 @param DataBits The number of data bits to use on the serial device. A DataBits
67 vaule of 0 will use the device's default data bit setting.
68 @param StopBits The number of stop bits to use on this serial device. A StopBits
69 value of DefaultStopBits will use the device's default number of
70 stop bits.
71
72 @retval EFI_SUCCESS The device was reset.
73 @retval EFI_DEVICE_ERROR The serial device could not be reset.
74
75 **/
76 EFI_STATUS
77 EFIAPI
78 SerialSetAttributes (
79 IN EFI_SERIAL_IO_PROTOCOL *This,
80 IN UINT64 BaudRate,
81 IN UINT32 ReceiveFifoDepth,
82 IN UINT32 Timeout,
83 IN EFI_PARITY_TYPE Parity,
84 IN UINT8 DataBits,
85 IN EFI_STOP_BITS_TYPE StopBits
86 )
87 {
88 return EFI_UNSUPPORTED;
89 }
90
91
92 /**
93 Set the control bits on a serial device
94
95 @param This Protocol instance pointer.
96 @param Control Set the bits of Control that are settable.
97
98 @retval EFI_SUCCESS The new control bits were set on the serial device.
99 @retval EFI_UNSUPPORTED The serial device does not support this operation.
100 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
101
102 **/
103 EFI_STATUS
104 EFIAPI
105 SerialSetControl (
106 IN EFI_SERIAL_IO_PROTOCOL *This,
107 IN UINT32 Control
108 )
109 {
110 return EFI_UNSUPPORTED;
111 }
112
113
114 /**
115 Retrieves the status of thecontrol bits on a serial device
116
117 @param This Protocol instance pointer.
118 @param Control A pointer to return the current Control signals from the serial device.
119
120 @retval EFI_SUCCESS The control bits were read from the serial device.
121 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
122
123 **/
124 EFI_STATUS
125 EFIAPI
126 SerialGetControl (
127 IN EFI_SERIAL_IO_PROTOCOL *This,
128 OUT UINT32 *Control
129 )
130 {
131 if (SerialPortPoll ()) {
132 // If a character is pending don't set EFI_SERIAL_INPUT_BUFFER_EMPTY
133 *Control = EFI_SERIAL_OUTPUT_BUFFER_EMPTY;
134 } else {
135 *Control = EFI_SERIAL_INPUT_BUFFER_EMPTY | EFI_SERIAL_OUTPUT_BUFFER_EMPTY;
136 }
137 return EFI_SUCCESS;
138 }
139
140
141 /**
142 Writes data to a serial device.
143
144 @param This Protocol instance pointer.
145 @param BufferSize On input, the size of the Buffer. On output, the amount of
146 data actually written.
147 @param Buffer The buffer of data to write
148
149 @retval EFI_SUCCESS The data was written.
150 @retval EFI_DEVICE_ERROR The device reported an error.
151 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
152
153 **/
154 EFI_STATUS
155 EFIAPI
156 SerialWrite (
157 IN EFI_SERIAL_IO_PROTOCOL *This,
158 IN OUT UINTN *BufferSize,
159 IN VOID *Buffer
160 )
161 {
162 UINTN Count;
163
164 Count = SerialPortWrite (Buffer, *BufferSize);
165 *BufferSize = Count;
166 return (Count == 0) ? EFI_DEVICE_ERROR : EFI_SUCCESS;
167 }
168
169 /**
170 Writes data to a serial device.
171
172 @param This Protocol instance pointer.
173 @param BufferSize On input, the size of the Buffer. On output, the amount of
174 data returned in Buffer.
175 @param Buffer The buffer to return the data into.
176
177 @retval EFI_SUCCESS The data was read.
178 @retval EFI_DEVICE_ERROR The device reported an error.
179 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
180
181 **/
182
183 EFI_STATUS
184 EFIAPI
185 SerialRead (
186 IN EFI_SERIAL_IO_PROTOCOL *This,
187 IN OUT UINTN *BufferSize,
188 OUT VOID *Buffer
189 )
190 {
191 UINTN Count;
192
193 Count = SerialPortRead (Buffer, *BufferSize);
194 *BufferSize = Count;
195 return (Count == 0) ? EFI_DEVICE_ERROR : EFI_SUCCESS;
196 }
197
198
199 EFI_HANDLE gHandle = NULL;
200
201 //
202 // Template used to initailize the GDB Serial IO protocols
203 //
204 EFI_SERIAL_IO_MODE gSerialIoMode = {
205 0, // ControlMask
206 0, // Timeout
207 FixedPcdGet64 (PcdUartDefaultBaudRate), // BaudRate
208 1, // RceiveFifoDepth
209 FixedPcdGet8 (PcdUartDefaultDataBits), // DataBits
210 FixedPcdGet8 (PcdUartDefaultParity), // Parity
211 FixedPcdGet8 (PcdUartDefaultStopBits) // StopBits
212 };
213
214
215 EFI_SERIAL_IO_PROTOCOL gSerialIoTemplate = {
216 SERIAL_IO_INTERFACE_REVISION,
217 SerialReset,
218 SerialSetAttributes,
219 SerialSetControl,
220 SerialGetControl,
221 SerialWrite,
222 SerialRead,
223 &gSerialIoMode
224 };
225
226 typedef struct {
227 VENDOR_DEVICE_PATH Guid;
228 UART_DEVICE_PATH Uart;
229 EFI_DEVICE_PATH_PROTOCOL End;
230 } SIMPLE_TEXT_OUT_DEVICE_PATH;
231
232 SIMPLE_TEXT_OUT_DEVICE_PATH mDevicePath = {
233 {
234 { HARDWARE_DEVICE_PATH, HW_VENDOR_DP, sizeof (VENDOR_DEVICE_PATH), 0},
235 EFI_CALLER_ID_GUID // Use the drivers GUID
236 },
237 {
238 { END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, sizeof (UART_DEVICE_PATH), 0},
239 0, // Reserved
240 FixedPcdGet64 (PcdUartDefaultBaudRate), // BaudRate
241 FixedPcdGet8 (PcdUartDefaultDataBits), // DataBits
242 FixedPcdGet8 (PcdUartDefaultParity), // Parity (N)
243 FixedPcdGet8 (PcdUartDefaultStopBits) // StopBits
244 },
245 { END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, sizeof (EFI_DEVICE_PATH_PROTOCOL), 0}
246 };
247
248
249 /**
250 Initialize the state information for the Serial Io Protocol
251
252 @param ImageHandle of the loaded driver
253 @param SystemTable Pointer to the System Table
254
255 @retval EFI_SUCCESS Protocol registered
256 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
257 @retval EFI_DEVICE_ERROR Hardware problems
258
259 **/
260 EFI_STATUS
261 EFIAPI
262 SerialDxeInitialize (
263 IN EFI_HANDLE ImageHandle,
264 IN EFI_SYSTEM_TABLE *SystemTable
265 )
266 {
267 EFI_STATUS Status;
268
269
270 // Make a new handle with Serial IO protocol and its device path on it.
271 Status = gBS->InstallMultipleProtocolInterfaces (
272 &gHandle,
273 &gEfiSerialIoProtocolGuid, &gSerialIoTemplate,
274 &gEfiDevicePathProtocolGuid, &mDevicePath,
275 NULL
276 );
277 ASSERT_EFI_ERROR (Status);
278
279 return Status;
280 }
281