1 /** @file
2   Serial I/O Port library functions with no library constructor/destructor
3 
4 
5   Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
6   Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
7 
8   This program and the accompanying materials
9   are licensed and made available under the terms and conditions of the BSD License
10   which accompanies this distribution.  The full text of the license may be found at
11   http://opensource.org/licenses/bsd-license.php
12 
13   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 
16 **/
17 
18 #include <Base.h>
19 #include <Library/DebugLib.h>
20 #include <Library/SerialPortLib.h>
21 #include <Library/PcdLib.h>
22 #include <Library/IoLib.h>
23 #include <Library/OmapLib.h>
24 #include <Omap3530/Omap3530.h>
25 
26 /*
27 
28   Programmed hardware of Serial port.
29 
30   @return    Always return EFI_UNSUPPORTED.
31 
32 **/
33 RETURN_STATUS
34 EFIAPI
SerialPortInitialize(VOID)35 SerialPortInitialize (
36   VOID
37   )
38 {
39   // assume assembly code at reset vector has setup UART
40   return RETURN_SUCCESS;
41 }
42 
43 /**
44   Write data to serial device.
45 
46   @param  Buffer           Point of data buffer which need to be writed.
47   @param  NumberOfBytes    Number of output bytes which are cached in Buffer.
48 
49   @retval 0                Write data failed.
50   @retval !0               Actual number of bytes writed to serial device.
51 
52 **/
53 UINTN
54 EFIAPI
SerialPortWrite(IN UINT8 * Buffer,IN UINTN NumberOfBytes)55 SerialPortWrite (
56   IN UINT8     *Buffer,
57   IN UINTN     NumberOfBytes
58 )
59 {
60   UINT32  LSR = UartBase(PcdGet32(PcdOmap35xxConsoleUart)) + UART_LSR_REG;
61   UINT32  THR = UartBase(PcdGet32(PcdOmap35xxConsoleUart)) + UART_THR_REG;
62   UINTN   Count;
63 
64   for (Count = 0; Count < NumberOfBytes; Count++, Buffer++) {
65     while ((MmioRead8(LSR) & UART_LSR_TX_FIFO_E_MASK) == UART_LSR_TX_FIFO_E_NOT_EMPTY);
66     MmioWrite8(THR, *Buffer);
67   }
68 
69   return NumberOfBytes;
70 }
71 
72 
73 /**
74   Read data from serial device and save the datas in buffer.
75 
76   @param  Buffer           Point of data buffer which need to be writed.
77   @param  NumberOfBytes    Number of output bytes which are cached in Buffer.
78 
79   @retval 0                Read data failed.
80   @retval !0               Aactual number of bytes read from serial device.
81 
82 **/
83 UINTN
84 EFIAPI
SerialPortRead(OUT UINT8 * Buffer,IN UINTN NumberOfBytes)85 SerialPortRead (
86   OUT UINT8     *Buffer,
87   IN  UINTN     NumberOfBytes
88 )
89 {
90   UINT32  LSR = UartBase(PcdGet32(PcdOmap35xxConsoleUart)) + UART_LSR_REG;
91   UINT32  RBR = UartBase(PcdGet32(PcdOmap35xxConsoleUart)) + UART_RBR_REG;
92   UINTN   Count;
93 
94   for (Count = 0; Count < NumberOfBytes; Count++, Buffer++) {
95     while ((MmioRead8(LSR) & UART_LSR_RX_FIFO_E_MASK) == UART_LSR_RX_FIFO_E_EMPTY);
96     *Buffer = MmioRead8(RBR);
97   }
98 
99   return NumberOfBytes;
100 }
101 
102 
103 /**
104   Check to see if any data is avaiable to be read from the debug device.
105 
106   @retval EFI_SUCCESS       At least one byte of data is avaiable to be read
107   @retval EFI_NOT_READY     No data is avaiable to be read
108   @retval EFI_DEVICE_ERROR  The serial device is not functioning properly
109 
110 **/
111 BOOLEAN
112 EFIAPI
SerialPortPoll(VOID)113 SerialPortPoll (
114   VOID
115   )
116 {
117   UINT32 LSR = UartBase(PcdGet32(PcdOmap35xxConsoleUart)) + UART_LSR_REG;
118 
119   if ((MmioRead8(LSR) & UART_LSR_RX_FIFO_E_MASK) == UART_LSR_RX_FIFO_E_NOT_EMPTY) {
120     return TRUE;
121   } else {
122     return FALSE;
123   }
124 }
125 
126 /**
127   Sets the control bits on a serial device.
128 
129   @param[in] Control            Sets the bits of Control that are settable.
130 
131   @retval RETURN_SUCCESS        The new control bits were set on the serial device.
132   @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
133   @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
134 
135 **/
136 RETURN_STATUS
137 EFIAPI
SerialPortSetControl(IN UINT32 Control)138 SerialPortSetControl (
139   IN UINT32 Control
140   )
141 {
142   return RETURN_UNSUPPORTED;
143 }
144 
145 /**
146   Retrieve the status of the control bits on a serial device.
147 
148   @param[out] Control           A pointer to return the current control signals from the serial device.
149 
150   @retval RETURN_SUCCESS        The control bits were read from the serial device.
151   @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
152   @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
153 
154 **/
155 RETURN_STATUS
156 EFIAPI
SerialPortGetControl(OUT UINT32 * Control)157 SerialPortGetControl (
158   OUT UINT32 *Control
159   )
160 {
161   *Control = 0;
162   if (!SerialPortPoll ()) {
163     *Control = EFI_SERIAL_INPUT_BUFFER_EMPTY;
164   }
165   return RETURN_SUCCESS;
166 }
167 
168 /**
169   Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
170   data bits, and stop bits on a serial device.
171 
172   @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
173                             device's default interface speed.
174                             On output, the value actually set.
175   @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
176                             serial interface. A ReceiveFifoDepth value of 0 will use
177                             the device's default FIFO depth.
178                             On output, the value actually set.
179   @param Timeout            The requested time out for a single character in microseconds.
180                             This timeout applies to both the transmit and receive side of the
181                             interface. A Timeout value of 0 will use the device's default time
182                             out value.
183                             On output, the value actually set.
184   @param Parity             The type of parity to use on this serial device. A Parity value of
185                             DefaultParity will use the device's default parity value.
186                             On output, the value actually set.
187   @param DataBits           The number of data bits to use on the serial device. A DataBits
188                             vaule of 0 will use the device's default data bit setting.
189                             On output, the value actually set.
190   @param StopBits           The number of stop bits to use on this serial device. A StopBits
191                             value of DefaultStopBits will use the device's default number of
192                             stop bits.
193                             On output, the value actually set.
194 
195   @retval RETURN_SUCCESS            The new attributes were set on the serial device.
196   @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
197   @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
198   @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
199 
200 **/
201 RETURN_STATUS
202 EFIAPI
SerialPortSetAttributes(IN OUT UINT64 * BaudRate,IN OUT UINT32 * ReceiveFifoDepth,IN OUT UINT32 * Timeout,IN OUT EFI_PARITY_TYPE * Parity,IN OUT UINT8 * DataBits,IN OUT EFI_STOP_BITS_TYPE * StopBits)203 SerialPortSetAttributes (
204   IN OUT UINT64             *BaudRate,
205   IN OUT UINT32             *ReceiveFifoDepth,
206   IN OUT UINT32             *Timeout,
207   IN OUT EFI_PARITY_TYPE    *Parity,
208   IN OUT UINT8              *DataBits,
209   IN OUT EFI_STOP_BITS_TYPE *StopBits
210   )
211 {
212   return RETURN_UNSUPPORTED;
213 }
214 
215