]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/SerialDxe/SerialIo.c
MdeModulePkg/SerialDxe: Fix not able to change serial attributes
[mirror_edk2.git] / MdeModulePkg / Universal / SerialDxe / SerialIo.c
CommitLineData
bf6b810f
SZ
1/** @file\r
2 Serial driver that layers on top of a Serial Port Library instance.\r
3\r
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
5 Copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>\r
6 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
7\r
8 This program and the accompanying materials\r
9 are licensed and made available under the terms and conditions of the BSD License\r
10 which accompanies this distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include <Library/UefiBootServicesTableLib.h>\r
19#include <Library/SerialPortLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/PcdLib.h>\r
22\r
23#include <Protocol/SerialIo.h>\r
24#include <Protocol/DevicePath.h>\r
25\r
26typedef struct {\r
27 VENDOR_DEVICE_PATH Guid;\r
28 UART_DEVICE_PATH Uart;\r
29 EFI_DEVICE_PATH_PROTOCOL End;\r
30} SERIAL_DEVICE_PATH;\r
31\r
32/**\r
33 Reset the serial device.\r
34\r
35 @param This Protocol instance pointer.\r
36\r
37 @retval EFI_SUCCESS The device was reset.\r
38 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
39\r
40**/\r
41EFI_STATUS\r
42EFIAPI\r
43SerialReset (\r
44 IN EFI_SERIAL_IO_PROTOCOL *This\r
45 );\r
46\r
47/**\r
48 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,\r
49 data bits, and stop bits on a serial device.\r
50\r
51 @param This Protocol instance pointer.\r
52 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the\r
53 device's default interface speed.\r
54 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the\r
55 serial interface. A ReceiveFifoDepth value of 0 will use\r
56 the device's default FIFO depth.\r
57 @param Timeout The requested time out for a single character in microseconds.\r
58 This timeout applies to both the transmit and receive side of the\r
59 interface. A Timeout value of 0 will use the device's default time\r
60 out value.\r
61 @param Parity The type of parity to use on this serial device. A Parity value of\r
62 DefaultParity will use the device's default parity value.\r
63 @param DataBits The number of data bits to use on the serial device. A DataBits\r
64 value of 0 will use the device's default data bit setting.\r
65 @param StopBits The number of stop bits to use on this serial device. A StopBits\r
66 value of DefaultStopBits will use the device's default number of\r
67 stop bits.\r
68\r
69 @retval EFI_SUCCESS The device was reset.\r
70 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
71\r
72**/\r
73EFI_STATUS\r
74EFIAPI\r
75SerialSetAttributes (\r
76 IN EFI_SERIAL_IO_PROTOCOL *This,\r
77 IN UINT64 BaudRate,\r
78 IN UINT32 ReceiveFifoDepth,\r
79 IN UINT32 Timeout,\r
80 IN EFI_PARITY_TYPE Parity,\r
81 IN UINT8 DataBits,\r
82 IN EFI_STOP_BITS_TYPE StopBits\r
83 );\r
84\r
85/**\r
86 Set the control bits on a serial device\r
87\r
88 @param This Protocol instance pointer.\r
89 @param Control Set the bits of Control that are settable.\r
90\r
91 @retval EFI_SUCCESS The new control bits were set on the serial device.\r
92 @retval EFI_UNSUPPORTED The serial device does not support this operation.\r
93 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
94\r
95**/\r
96EFI_STATUS\r
97EFIAPI\r
98SerialSetControl (\r
99 IN EFI_SERIAL_IO_PROTOCOL *This,\r
100 IN UINT32 Control\r
101 );\r
102\r
103/**\r
104 Retrieves the status of the control bits on a serial device\r
105\r
106 @param This Protocol instance pointer.\r
107 @param Control A pointer to return the current Control signals from the serial device.\r
108\r
109 @retval EFI_SUCCESS The control bits were read from the serial device.\r
110 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
111\r
112**/\r
113EFI_STATUS\r
114EFIAPI\r
115SerialGetControl (\r
116 IN EFI_SERIAL_IO_PROTOCOL *This,\r
117 OUT UINT32 *Control\r
118 );\r
119\r
120/**\r
121 Writes data to a serial device.\r
122\r
123 @param This Protocol instance pointer.\r
124 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
125 data actually written.\r
126 @param Buffer The buffer of data to write\r
127\r
128 @retval EFI_SUCCESS The data was written.\r
129 @retval EFI_DEVICE_ERROR The device reported an error.\r
130 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
131\r
132**/\r
133EFI_STATUS\r
134EFIAPI\r
135SerialWrite (\r
136 IN EFI_SERIAL_IO_PROTOCOL *This,\r
137 IN OUT UINTN *BufferSize,\r
138 IN VOID *Buffer\r
139 );\r
140\r
141/**\r
142 Reads data from 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 returned in Buffer.\r
147 @param Buffer The buffer to return the data into.\r
148\r
149 @retval EFI_SUCCESS The data was read.\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
156SerialRead (\r
157 IN EFI_SERIAL_IO_PROTOCOL *This,\r
158 IN OUT UINTN *BufferSize,\r
159 OUT VOID *Buffer\r
160 );\r
161\r
162EFI_HANDLE mSerialHandle = NULL;\r
163\r
164SERIAL_DEVICE_PATH mSerialDevicePath = {\r
165 {\r
166 { HARDWARE_DEVICE_PATH, HW_VENDOR_DP, { sizeof (VENDOR_DEVICE_PATH), 0} },\r
167 EFI_CALLER_ID_GUID // Use the driver's GUID\r
168 },\r
169 {\r
170 { MESSAGING_DEVICE_PATH, MSG_UART_DP, { sizeof (UART_DEVICE_PATH), 0} },\r
171 0, // Reserved\r
172 0, // BaudRate\r
173 0, // DataBits\r
174 0, // Parity\r
175 0 // StopBits\r
176 },\r
177 { END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE, { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 } }\r
178};\r
179\r
180//\r
181// Template used to initialize the Serial IO protocols.\r
182//\r
183EFI_SERIAL_IO_MODE mSerialIoMode = {\r
c0beed6d
LE
184 //\r
185 // value field set in SerialDxeInitialize()?\r
186 //--------- ------------------- -----------------------------\r
187 0, // ControlMask\r
ea012619 188 1000 * 1000, // Timeout\r
c0beed6d
LE
189 0, // BaudRate yes\r
190 1, // ReceiveFifoDepth\r
191 0, // DataBits yes\r
192 0, // Parity yes\r
193 0 // StopBits yes\r
bf6b810f
SZ
194};\r
195\r
196EFI_SERIAL_IO_PROTOCOL mSerialIoTemplate = {\r
197 SERIAL_IO_INTERFACE_REVISION,\r
198 SerialReset,\r
199 SerialSetAttributes,\r
200 SerialSetControl,\r
201 SerialGetControl,\r
202 SerialWrite,\r
203 SerialRead,\r
204 &mSerialIoMode\r
205};\r
206\r
207/**\r
208 Reset the serial device.\r
209\r
210 @param This Protocol instance pointer.\r
211\r
212 @retval EFI_SUCCESS The device was reset.\r
213 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
214\r
215**/\r
216EFI_STATUS\r
217EFIAPI\r
218SerialReset (\r
219 IN EFI_SERIAL_IO_PROTOCOL *This\r
220 )\r
221{\r
222 EFI_STATUS Status;\r
bf6b810f
SZ
223\r
224 Status = SerialPortInitialize ();\r
225 if (EFI_ERROR (Status)) {\r
226 return Status;\r
227 }\r
228\r
229 //\r
91cc526b 230 // Go set the current attributes\r
bf6b810f 231 //\r
91cc526b
PB
232 Status = This->SetAttributes (\r
233 This,\r
234 This->Mode->BaudRate,\r
235 This->Mode->ReceiveFifoDepth,\r
236 This->Mode->Timeout,\r
237 (EFI_PARITY_TYPE) This->Mode->Parity,\r
238 (UINT8) This->Mode->DataBits,\r
239 (EFI_STOP_BITS_TYPE) This->Mode->StopBits\r
240 );\r
bf6b810f
SZ
241\r
242 return Status;\r
243}\r
244\r
245/**\r
246 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,\r
247 data bits, and stop bits on a serial device.\r
248\r
249 @param This Protocol instance pointer.\r
250 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the\r
251 device's default interface speed.\r
252 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the\r
253 serial interface. A ReceiveFifoDepth value of 0 will use\r
254 the device's default FIFO depth.\r
255 @param Timeout The requested time out for a single character in microseconds.\r
256 This timeout applies to both the transmit and receive side of the\r
257 interface. A Timeout value of 0 will use the device's default time\r
258 out value.\r
259 @param Parity The type of parity to use on this serial device. A Parity value of\r
260 DefaultParity will use the device's default parity value.\r
261 @param DataBits The number of data bits to use on the serial device. A DataBits\r
262 value of 0 will use the device's default data bit setting.\r
263 @param StopBits The number of stop bits to use on this serial device. A StopBits\r
264 value of DefaultStopBits will use the device's default number of\r
265 stop bits.\r
266\r
267 @retval EFI_SUCCESS The device was reset.\r
268 @retval EFI_DEVICE_ERROR The serial device could not be reset.\r
269\r
270**/\r
271EFI_STATUS\r
272EFIAPI\r
273SerialSetAttributes (\r
274 IN EFI_SERIAL_IO_PROTOCOL *This,\r
275 IN UINT64 BaudRate,\r
276 IN UINT32 ReceiveFifoDepth,\r
277 IN UINT32 Timeout,\r
278 IN EFI_PARITY_TYPE Parity,\r
279 IN UINT8 DataBits,\r
280 IN EFI_STOP_BITS_TYPE StopBits\r
281 )\r
282{\r
283 EFI_STATUS Status;\r
284 EFI_TPL Tpl;\r
285\r
286 Status = SerialPortSetAttributes (&BaudRate, &ReceiveFifoDepth, &Timeout, &Parity, &DataBits, &StopBits);\r
287 if (EFI_ERROR (Status)) {\r
288 return Status;\r
289 }\r
290\r
291 //\r
292 // Set the Serial I/O mode and update the device path\r
293 //\r
294\r
295 Tpl = gBS->RaiseTPL (TPL_NOTIFY);\r
296\r
297 //\r
298 // Set the Serial I/O mode\r
299 //\r
300 This->Mode->ReceiveFifoDepth = ReceiveFifoDepth;\r
301 This->Mode->Timeout = Timeout;\r
302 This->Mode->BaudRate = BaudRate;\r
303 This->Mode->DataBits = (UINT32) DataBits;\r
304 This->Mode->Parity = (UINT32) Parity;\r
305 This->Mode->StopBits = (UINT32) StopBits;\r
306\r
307 //\r
308 // Check if the device path has actually changed\r
309 //\r
310 if (mSerialDevicePath.Uart.BaudRate == BaudRate &&\r
311 mSerialDevicePath.Uart.DataBits == DataBits &&\r
312 mSerialDevicePath.Uart.Parity == (UINT8) Parity &&\r
313 mSerialDevicePath.Uart.StopBits == (UINT8) StopBits\r
314 ) {\r
315 gBS->RestoreTPL (Tpl);\r
316 return EFI_SUCCESS;\r
317 }\r
318\r
319 //\r
320 // Update the device path\r
321 //\r
322 mSerialDevicePath.Uart.BaudRate = BaudRate;\r
323 mSerialDevicePath.Uart.DataBits = DataBits;\r
324 mSerialDevicePath.Uart.Parity = (UINT8) Parity;\r
325 mSerialDevicePath.Uart.StopBits = (UINT8) StopBits;\r
326\r
327 Status = gBS->ReinstallProtocolInterface (\r
328 mSerialHandle,\r
329 &gEfiDevicePathProtocolGuid,\r
330 &mSerialDevicePath,\r
331 &mSerialDevicePath\r
332 );\r
333\r
334 gBS->RestoreTPL (Tpl);\r
335\r
336 return Status;\r
337}\r
338\r
339/**\r
340 Set the control bits on a serial device\r
341\r
342 @param This Protocol instance pointer.\r
343 @param Control Set the bits of Control that are settable.\r
344\r
345 @retval EFI_SUCCESS The new control bits were set on the serial device.\r
346 @retval EFI_UNSUPPORTED The serial device does not support this operation.\r
347 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
348\r
349**/\r
350EFI_STATUS\r
351EFIAPI\r
352SerialSetControl (\r
353 IN EFI_SERIAL_IO_PROTOCOL *This,\r
354 IN UINT32 Control\r
355 )\r
356{\r
357 return SerialPortSetControl (Control);\r
358}\r
359\r
360/**\r
361 Retrieves the status of the control bits on a serial device\r
362\r
363 @param This Protocol instance pointer.\r
364 @param Control A pointer to return the current Control signals from the serial device.\r
365\r
366 @retval EFI_SUCCESS The control bits were read from the serial device.\r
367 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.\r
368\r
369**/\r
370EFI_STATUS\r
371EFIAPI\r
372SerialGetControl (\r
373 IN EFI_SERIAL_IO_PROTOCOL *This,\r
374 OUT UINT32 *Control\r
375 )\r
376{\r
377 return SerialPortGetControl (Control);\r
378}\r
379\r
380/**\r
381 Writes data to a serial device.\r
382\r
383 @param This Protocol instance pointer.\r
384 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
385 data actually written.\r
386 @param Buffer The buffer of data to write\r
387\r
388 @retval EFI_SUCCESS The data was written.\r
389 @retval EFI_DEVICE_ERROR The device reported an error.\r
390 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
391\r
392**/\r
393EFI_STATUS\r
394EFIAPI\r
395SerialWrite (\r
396 IN EFI_SERIAL_IO_PROTOCOL *This,\r
397 IN OUT UINTN *BufferSize,\r
398 IN VOID *Buffer\r
399 )\r
400{\r
401 UINTN Count;\r
402\r
403 Count = SerialPortWrite (Buffer, *BufferSize);\r
404\r
405 if (Count != *BufferSize) {\r
406 *BufferSize = Count;\r
407 return EFI_TIMEOUT;\r
408 }\r
409\r
410 return EFI_SUCCESS;\r
411}\r
412\r
413/**\r
414 Reads data from a serial device.\r
415\r
416 @param This Protocol instance pointer.\r
417 @param BufferSize On input, the size of the Buffer. On output, the amount of\r
418 data returned in Buffer.\r
419 @param Buffer The buffer to return the data into.\r
420\r
421 @retval EFI_SUCCESS The data was read.\r
422 @retval EFI_DEVICE_ERROR The device reported an error.\r
423 @retval EFI_TIMEOUT The data write was stopped due to a timeout.\r
424\r
425**/\r
426EFI_STATUS\r
427EFIAPI\r
428SerialRead (\r
429 IN EFI_SERIAL_IO_PROTOCOL *This,\r
430 IN OUT UINTN *BufferSize,\r
431 OUT VOID *Buffer\r
432 )\r
433{\r
434 UINTN Count;\r
4cf3f37c 435 UINTN TimeOut;\r
bf6b810f
SZ
436\r
437 Count = 0;\r
438\r
4cf3f37c
SZ
439 while (Count < *BufferSize) {\r
440 TimeOut = 0;\r
441 while (TimeOut < mSerialIoMode.Timeout) {\r
442 if (SerialPortPoll ()) {\r
443 break;\r
444 }\r
445 gBS->Stall (10);\r
446 TimeOut += 10;\r
447 }\r
448 if (TimeOut >= mSerialIoMode.Timeout) {\r
449 break;\r
450 }\r
451 SerialPortRead (Buffer, 1);\r
452 Count++;\r
453 Buffer = (VOID *) ((UINT8 *) Buffer + 1);\r
bf6b810f
SZ
454 }\r
455\r
456 if (Count != *BufferSize) {\r
457 *BufferSize = Count;\r
458 return EFI_TIMEOUT;\r
459 }\r
460\r
461 return EFI_SUCCESS;\r
462}\r
463\r
464/**\r
465 Initialization for the Serial Io Protocol.\r
466\r
467 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
468 @param[in] SystemTable A pointer to the EFI System Table.\r
469\r
470 @retval EFI_SUCCESS The entry point is executed successfully.\r
471 @retval other Some error occurs when executing this entry point.\r
472\r
473**/\r
474EFI_STATUS\r
475EFIAPI\r
476SerialDxeInitialize (\r
477 IN EFI_HANDLE ImageHandle,\r
478 IN EFI_SYSTEM_TABLE *SystemTable\r
479 )\r
480{\r
481 EFI_STATUS Status;\r
482\r
bf6b810f
SZ
483 mSerialIoMode.BaudRate = PcdGet64 (PcdUartDefaultBaudRate);\r
484 mSerialIoMode.DataBits = (UINT32) PcdGet8 (PcdUartDefaultDataBits);\r
485 mSerialIoMode.Parity = (UINT32) PcdGet8 (PcdUartDefaultParity);\r
486 mSerialIoMode.StopBits = (UINT32) PcdGet8 (PcdUartDefaultStopBits);\r
f423d760 487 mSerialIoMode.ReceiveFifoDepth = PcdGet16 (PcdUartDefaultReceiveFifoDepth);\r
bf6b810f
SZ
488 mSerialDevicePath.Uart.BaudRate = PcdGet64 (PcdUartDefaultBaudRate);\r
489 mSerialDevicePath.Uart.DataBits = PcdGet8 (PcdUartDefaultDataBits);\r
490 mSerialDevicePath.Uart.Parity = PcdGet8 (PcdUartDefaultParity);\r
491 mSerialDevicePath.Uart.StopBits = PcdGet8 (PcdUartDefaultStopBits);\r
492\r
91cc526b
PB
493 //\r
494 // Issue a reset to initialize the Serial Port\r
495 //\r
496 Status = mSerialIoTemplate.Reset (&mSerialIoTemplate);\r
497 if (EFI_ERROR (Status)) {\r
498 return Status;\r
499 }\r
500\r
bf6b810f
SZ
501 //\r
502 // Make a new handle with Serial IO protocol and its device path on it.\r
503 //\r
504 Status = gBS->InstallMultipleProtocolInterfaces (\r
505 &mSerialHandle,\r
506 &gEfiSerialIoProtocolGuid, &mSerialIoTemplate,\r
507 &gEfiDevicePathProtocolGuid, &mSerialDevicePath,\r
508 NULL\r
509 );\r
510 ASSERT_EFI_ERROR (Status);\r
511\r
512 return Status;\r
513}\r
514\r