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