1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/** @file
UART Serial Port library functions
Copyright (c) 2006 - 2009, Intel Corporation
All rights reserved.
Copyright (c) Huawei Technologies Co., Ltd. 2013. All rights reserved.
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <Base.h>
#include <Library/SerialPortLib.h>
#include "SerialPortLib.h"
#include <Uefi/UefiBaseType.h>
#include <Protocol/SerialIo.h>
static int flag=1;
UINT32 UART_UartClkFreq(void)
{
#define UART_CLK_FREQ_ADRS 0x20000020
UINT32 ulRegVal = 0x00;
UINT32 ulCpllFreq = 0x00;
ulRegVal = ((*(UINT32 *)UART_CLK_FREQ_ADRS));
ulCpllFreq = ((((25000000 /(ulRegVal&0x3f))) /((ulRegVal>>18)&0x07))
/((ulRegVal>>21)&0x07))* ((ulRegVal>>6)&0xfff);
return ulCpllFreq;
}
/**
Initialize the serial device hardware.
If no initialization is required, then return RETURN_SUCCESS.
If the serial device was successfuly initialized, then return RETURN_SUCCESS.
If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
@retval RETURN_SUCCESS The serial device was initialized.
@retval RETURN_DEVICE_ERROR The serail device could not be initialized.
**/
RETURN_STATUS
EFIAPI
SerialPortInitialize (
VOID
)
{
UINT32 ulUartClkFreq;
if(flag>0) return RETURN_SUCCESS;
*(volatile UINT8 *)(UART_LCR_REG) = UART_LCR_DLS8;
*(volatile UINT8 *)(UART_FCR_REG) = UART_FCR_EN | UART_FCR_RXCLR | UART_FCR_TXCLR;
*(volatile UINT8 *)(UART_LCR_REG) = UART_LCR_DLAB | UART_LCR_DLS8;
ulUartClkFreq = 168750000;
//ulUartClkFreq = 50000000;
*(volatile UINT8 *)(UART_DLL_REG) = (ulUartClkFreq / (16 * BAUDRATE) ) & 0xff;
*(volatile UINT8 *)(UART_DLH_REG) = ((ulUartClkFreq/ (16 * BAUDRATE) ) >> 8 ) & 0xff;
//*(volatile UINT8 *)(UART_DLL_REG) = (ulUartClkFreq / (16 * 2400) ) & 0xff;
//*(volatile UINT8 *)(UART_DLH_REG) = ((ulUartClkFreq/ (16 * 2400) ) >> 8 ) & 0xff;
//*(volatile UINT8 *)(UART_DLL_REG) = 0x5B; //115200
//*(volatile UINT8 *)(UART_DLH_REG) = 0x00; //115200
*(volatile UINT8 *)(UART_LCR_REG) = UART_LCR_DLS8;
*(volatile UINT8 *)(UART_IEL_REG) = 0x00;
return RETURN_SUCCESS;
}
UINT8 UART_ChkSndEnd(VOID)
{
if(!(*(UINT8 *)UART_LSR_REG & UART_LSR_THRE))
{
return 0;
}
else
{
return 1;
}
}
/**
Write data from buffer to serial device.
Writes NumberOfBytes data bytes from Buffer to the serial device.
The number of bytes actually written to the serial device is returned.
If the return value is less than NumberOfBytes, then the write operation failed.
If Buffer is NULL, then ASSERT().
If NumberOfBytes is zero, then return 0.
@param Buffer Pointer to the data buffer to be written.
@param NumberOfBytes Number of bytes to written to the serial device.
@retval 0 NumberOfBytes is 0.
@retval >0 The number of bytes written to the serial device.
If this value is less than NumberOfBytes, then the read operation failed.
**/
UINTN
EFIAPI
SerialPortWrite (
IN UINT8 *Buffer,
IN UINTN NumberOfBytes
)
{
UINTN Result;
if (Buffer == NULL) {
return 0;
}
Result = NumberOfBytes;
while (NumberOfBytes--) {
SerialPortWriteChar(*Buffer);
Buffer++;
}
return Result;
}
/**
Reads data from a serial device into a buffer.
@param Buffer Pointer to the data buffer to store the data read from the serial device.
@param NumberOfBytes Number of bytes to read from the serial device.
@retval 0 NumberOfBytes is 0.
@retval >0 The number of bytes read from the serial device.
If this value is less than NumberOfBytes, then the read operation failed.
**/
UINTN
EFIAPI
SerialPortRead (
OUT UINT8 *Buffer,
IN UINTN NumberOfBytes
)
{
UINTN Result;
if (NULL == Buffer) {
return 0;
}
Result = NumberOfBytes;
while (NumberOfBytes--) {
//
// Wait for the serail port to be ready.
//
*Buffer=SerialPortReadChar();
Buffer++ ;
}
return Result;
}
/**
Polls a serial device to see if there is any data waiting to be read.
Polls aserial device to see if there is any data waiting to be read.
If there is data waiting to be read from the serial device, then TRUE is returned.
If there is no data waiting to be read from the serial device, then FALSE is returned.
@retval TRUE Data is waiting to be read from the serial device.
@retval FALSE There is no data waiting to be read from the serial device.
**/
BOOLEAN
EFIAPI
SerialPortPoll (
VOID
)
{
return (BOOLEAN) ((*(volatile UINT8 *)(UART_LSR_REG) & UART_LSR_DR) == UART_LSR_DR);
}
VOID SerialPortWriteChar(UINT8 scShowChar)
{
UINT32 ulLoop = 0;
while(ulLoop < UART_SEND_DELAY)
{
if ((*(volatile UINT8 *)(UART_USR_REG) & 0x02) == 0x02)
{
break;
}
ulLoop++;
}
*(volatile UINT8 *)(UART_THR_REG) = (UINT8)scShowChar;
ulLoop = 0;
while(ulLoop < UART_SEND_DELAY)
{
if ((*(volatile UINT8 *)(UART_USR_REG) & 0x04) == 0x04)
{
break;
}
ulLoop++;
}
return;
}
UINT8 SerialPortReadChar(VOID)
{
UINT8 recvchar = 0;
do
{
if ((*(UINT8 *)(UART_LSR_REG) & UART_LSR_DR) == UART_LSR_DR)
{
break;
}
}while(*(UINT8 *)(UART_USR_REG) & UART_USR_BUSY);
recvchar = (*(volatile UINT8 *)(UART_RBR_REG));
return recvchar;
}
/**
Set new attributes to PL011.
@param BaudRate The baud rate of the serial device. If the baud rate is not supported,
the speed will be reduced down to the nearest supported one and the
variable's value will be updated accordingly.
@param ReceiveFifoDepth The number of characters the device will buffer on input. If the specified
value is not supported, the variable's value will be reduced down to the
nearest supported one.
@param Timeout If applicable, the number of microseconds the device will wait
before timing out a Read or a Write operation.
@param Parity If applicable, this is the EFI_PARITY_TYPE that is computer or checked
as each character is transmitted or received. If the device does not
support parity, the value is the default parity value.
@param DataBits The number of data bits in each character
@param StopBits If applicable, the EFI_STOP_BITS_TYPE number of stop bits per character.
If the device does not support stop bits, the value is the default stop
bit value.
@retval EFI_SUCCESS All attributes were set correctly on the serial device.
@retval EFI_INVALID_PARAMETERS One or more of the attributes has an unsupported value.
**/
RETURN_STATUS
EFIAPI
SerialPortSetAttributes (
IN UINT64 BaudRate,
IN UINT32 ReceiveFifoDepth,
IN UINT32 Timeout,
IN EFI_PARITY_TYPE Parity,
IN UINT8 DataBits,
IN EFI_STOP_BITS_TYPE StopBits
)
{
return EFI_UNSUPPORTED;
}
/**
Set the serial device control bits.
@param Control Control bits which are to be set on the serial device.
@retval EFI_SUCCESS The new control bits were set on the serial device.
@retval EFI_UNSUPPORTED The serial device does not support this operation.
@retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
**/
RETURN_STATUS
EFIAPI
SerialPortSetControl (
IN UINT32 Control
)
{
return EFI_UNSUPPORTED;
}
/**
Get the serial device control bits.
@param Control Control signals read from the serial device.
@retval EFI_SUCCESS The control bits were read from the serial device.
@retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
**/
RETURN_STATUS
EFIAPI
SerialPortGetControl (
OUT UINT32 *Control
)
{
if (SerialPortPoll ()) {
// If a character is pending don't set EFI_SERIAL_INPUT_BUFFER_EMPTY
*Control = EFI_SERIAL_OUTPUT_BUFFER_EMPTY;
} else {
*Control = EFI_SERIAL_INPUT_BUFFER_EMPTY | EFI_SERIAL_OUTPUT_BUFFER_EMPTY;
}
return EFI_SUCCESS;
//return EFI_UNSUPPORTED;
}
|