Adding support for BeagleBoard.
[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
27 #include <Protocol/SerialIo.h>
28
29 /**
30 Reset the serial device.
31
32 @param This Protocol instance pointer.
33
34 @retval EFI_SUCCESS The device was reset.
35 @retval EFI_DEVICE_ERROR The serial device could not be reset.
36
37 **/
38 EFI_STATUS
39 EFIAPI
40 SerialReset (
41 IN EFI_SERIAL_IO_PROTOCOL *This
42 )
43 {
44 SerialPortInitialize ();
45 return EFI_SUCCESS;
46 }
47
48
49 /**
50 Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
51 data buts, and stop bits on a serial device.
52
53 @param This Protocol instance pointer.
54 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the
55 device's default interface speed.
56 @param ReveiveFifoDepth The requested depth of the FIFO on the receive side of the
57 serial interface. A ReceiveFifoDepth value of 0 will use
58 the device's dfault FIFO depth.
59 @param Timeout The requested time out for a single character in microseconds.
60 This timeout applies to both the transmit and receive side of the
61 interface. A Timeout value of 0 will use the device's default time
62 out value.
63 @param Parity The type of parity to use on this serial device. A Parity value of
64 DefaultParity will use the device's default parity value.
65 @param DataBits The number of data bits to use on the serial device. A DataBits
66 vaule of 0 will use the device's default data bit setting.
67 @param StopBits The number of stop bits to use on this serial device. A StopBits
68 value of DefaultStopBits will use the device's default number of
69 stop bits.
70
71 @retval EFI_SUCCESS The device was reset.
72 @retval EFI_DEVICE_ERROR The serial device could not be reset.
73
74 **/
75 EFI_STATUS
76 EFIAPI
77 SerialSetAttributes (
78 IN EFI_SERIAL_IO_PROTOCOL *This,
79 IN UINT64 BaudRate,
80 IN UINT32 ReceiveFifoDepth,
81 IN UINT32 Timeout,
82 IN EFI_PARITY_TYPE Parity,
83 IN UINT8 DataBits,
84 IN EFI_STOP_BITS_TYPE StopBits
85 )
86 {
87 return EFI_UNSUPPORTED;
88 }
89
90
91 /**
92 Set the control bits on a serial device
93
94 @param This Protocol instance pointer.
95 @param Control Set the bits of Control that are settable.
96
97 @retval EFI_SUCCESS The new control bits were set on the serial device.
98 @retval EFI_UNSUPPORTED The serial device does not support this operation.
99 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
100
101 **/
102 EFI_STATUS
103 EFIAPI
104 SerialSetControl (
105 IN EFI_SERIAL_IO_PROTOCOL *This,
106 IN UINT32 Control
107 )
108 {
109 return EFI_UNSUPPORTED;
110 }
111
112
113 /**
114 Retrieves the status of thecontrol bits on a serial device
115
116 @param This Protocol instance pointer.
117 @param Control A pointer to return the current Control signals from the serial device.
118
119 @retval EFI_SUCCESS The control bits were read from the serial device.
120 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
121
122 **/
123 EFI_STATUS
124 EFIAPI
125 SerialGetControl (
126 IN EFI_SERIAL_IO_PROTOCOL *This,
127 OUT UINT32 *Control
128 )
129 {
130 if (SerialPortPoll ()) {
131 // If a character is pending don't set EFI_SERIAL_INPUT_BUFFER_EMPTY
132 *Control = EFI_SERIAL_OUTPUT_BUFFER_EMPTY;
133 } else {
134 *Control = EFI_SERIAL_INPUT_BUFFER_EMPTY | EFI_SERIAL_OUTPUT_BUFFER_EMPTY;
135 }
136 return EFI_SUCCESS;
137 }
138
139
140 /**
141 Writes data to a serial device.
142
143 @param This Protocol instance pointer.
144 @param BufferSize On input, the size of the Buffer. On output, the amount of
145 data actually written.
146 @param Buffer The buffer of data to write
147
148 @retval EFI_SUCCESS The data was written.
149 @retval EFI_DEVICE_ERROR The device reported an error.
150 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
151
152 **/
153 EFI_STATUS
154 EFIAPI
155 SerialWrite (
156 IN EFI_SERIAL_IO_PROTOCOL *This,
157 IN OUT UINTN *BufferSize,
158 IN VOID *Buffer
159 )
160 {
161 UINTN Count;
162
163 Count = SerialPortWrite (Buffer, *BufferSize);
164 *BufferSize = Count;
165 return (Count == 0) ? EFI_DEVICE_ERROR : EFI_SUCCESS;
166 }
167
168 /**
169 Writes data to a serial device.
170
171 @param This Protocol instance pointer.
172 @param BufferSize On input, the size of the Buffer. On output, the amount of
173 data returned in Buffer.
174 @param Buffer The buffer to return the data into.
175
176 @retval EFI_SUCCESS The data was read.
177 @retval EFI_DEVICE_ERROR The device reported an error.
178 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
179
180 **/
181
182 EFI_STATUS
183 EFIAPI
184 SerialRead (
185 IN EFI_SERIAL_IO_PROTOCOL *This,
186 IN OUT UINTN *BufferSize,
187 OUT VOID *Buffer
188 )
189 {
190 UINTN Count;
191
192 Count = SerialPortWrite (Buffer, *BufferSize);
193 *BufferSize = Count;
194 return (Count == 0) ? EFI_DEVICE_ERROR : EFI_SUCCESS;
195 }
196
197
198 EFI_HANDLE gHandle = NULL;
199
200 //
201 // Template used to initailize the GDB Serial IO protocols
202 //
203 EFI_SERIAL_IO_MODE gSerialIoMode = {
204 0, // ControlMask
205 0, // Timeout
206 0, // BaudRate
207 1, // RceiveFifoDepth
208 0, // DataBits
209 0, // Parity
210 0 // StopBits
211 };
212
213
214 EFI_SERIAL_IO_PROTOCOL gSerialIoTemplate = {
215 SERIAL_IO_INTERFACE_REVISION,
216 SerialReset,
217 SerialSetAttributes,
218 SerialSetControl,
219 SerialGetControl,
220 SerialWrite,
221 SerialRead,
222 &gSerialIoMode
223 };
224
225
226 /**
227 Initialize the state information for the Serial Io Protocol
228
229 @param ImageHandle of the loaded driver
230 @param SystemTable Pointer to the System Table
231
232 @retval EFI_SUCCESS Protocol registered
233 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
234 @retval EFI_DEVICE_ERROR Hardware problems
235
236 **/
237 EFI_STATUS
238 EFIAPI
239 SerialDxeInitialize (
240 IN EFI_HANDLE ImageHandle,
241 IN EFI_SYSTEM_TABLE *SystemTable
242 )
243 {
244 EFI_STATUS Status;
245
246
247 // Make a new handle with Serial IO protocol and its device path on it.
248 Status = gBS->InstallMultipleProtocolInterfaces (
249 &gHandle,
250 &gEfiSerialIoProtocolGuid, &gSerialIoTemplate,
251 &gEfiDevicePathProtocolGuid, NULL, // BugBug: Need a device path
252 NULL
253 );
254 ASSERT_EFI_ERROR (Status);
255
256 return Status;
257 }
258