-
Notifications
You must be signed in to change notification settings - Fork 0
/
usif.c
279 lines (182 loc) · 5.61 KB
/
usif.c
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
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
Module Name:
usif.c
Abstract:
Intel Sofia UART serial support.
--*/
// ------------------------------------------------------------------- Includes
#include "common.h"
// ---------------------------------------------------------------- Definitions
#define USIF_FIFO_STAT 0x00000044 // FIFO status register
#define USIF_FIFO_STAT_TXFFS 0x00FF0000 // TX filled FIFO stages
#define USIF_FIFO_STAT_RXFFS 0x000000FF // RX filled FIFO stages
#define USIF_TXD 0x00040000 // Transmisson data register
#define USIF_RXD 0x00080000 // Reception data register
// ------------------------------------------------------------------ Functions
BOOLEAN
UsifInitializePort (
_In_opt_ _Null_terminated_ PCHAR LoadOptions,
_Inout_ PCPPORT Port,
BOOLEAN MemoryMapped,
UCHAR AccessSize,
UCHAR BitWidth
)
/*++
Routine Description:
This routine performs the initialization of an Intel Sofia serial UART.
Arguments:
LoadOptions - Optional load option string. Currently unused.
Port - Supplies a pointer to a CPPORT object which will be filled in as
part of the port initialization.
MemoryMapped - Supplies a boolean which indicates if the UART is accessed
through memory-mapped registers or legacy port I/O.
AccessSize - Supplies an ACPI Generic Access Size value which indicates the
type of bus access that should be performed when accessing the UART.
BitWidth - Supplies a number indicating how wide the UART's registers are.
Return Value:
TRUE if the port has been successfully initialized, FALSE otherwise.
--*/
{
UNREFERENCED_PARAMETER(LoadOptions);
UNREFERENCED_PARAMETER(AccessSize);
UNREFERENCED_PARAMETER(BitWidth);
if (MemoryMapped == FALSE) {
return FALSE;
}
Port->Flags = 0;
return TRUE;
}
BOOLEAN
UsifSetBaud (
_Inout_ PCPPORT Port,
ULONG Rate
)
/*++
Routine Description:
Set the baud rate for the UART hardware and record it in the port object.
Arguments:
Port - Supplies the address of the port object that describes the UART.
Rate - Supplies the desired baud rate in bits per second.
Return Value:
TRUE if the baud rate was programmed, FALSE if it was not.
--*/
{
if ((Port == NULL) || (Port->Address == NULL)) {
return FALSE;
}
//
// Remember the baud rate.
//
Port->BaudRate = Rate;
return TRUE;
}
UART_STATUS
UsifGetByte (
_Inout_ PCPPORT Port,
_Out_ PUCHAR Byte
)
/*++
Routine Description:
Fetch a data byte from the UART device and return it.
Arguments:
Port - Supplies the address of the port object that describes the UART.
Byte - Supplies the address of variable to hold the result.
Return Value:
UART_STATUS code.
--*/
{
ULONG Stat;
ULONG Value;
if ((Port == NULL) || (Port->Address == NULL)) {
return UartNotReady;
}
//
// Get FIFO status.
//
Stat = READ_REGISTER_ULONG((PULONG)(Port->Address + USIF_FIFO_STAT));
//
// Is at least one character available?
//
if ((Stat & USIF_FIFO_STAT_RXFFS) != 0) {
//
// Fetch the data byte.
//
Value = READ_REGISTER_ULONG((PULONG)(Port->Address + USIF_RXD));
*Byte = Value & (UCHAR)0xFF;
return UartSuccess;
}
return UartNoData;
}
UART_STATUS
UsifPutByte (
_Inout_ PCPPORT Port,
UCHAR Byte,
BOOLEAN BusyWait
)
/*++
Routine Description:
Write a data byte out to the UART device.
Arguments:
Port - Supplies the address of the port object that describes the UART.
Byte - Supplies the data to emit.
BusyWait - Supplies a flag to control whether this routine will busy
wait (spin) for the UART hardware to be ready to transmit.
Return Value:
UART_STATUS code.
--*/
{
if ((Port == NULL) || (Port->Address == NULL)) {
return UartNotReady;
}
//
// Wait for port to be free and FIFO not full.
//
if (BusyWait != FALSE) {
while (READ_REGISTER_ULONG((PULONG)(Port->Address + USIF_FIFO_STAT)) &
(USIF_FIFO_STAT_TXFFS));
} else if (READ_REGISTER_ULONG((PULONG)(Port->Address + USIF_FIFO_STAT)) &
(USIF_FIFO_STAT_TXFFS)) {
return UartNotReady;
}
//
// Send the byte.
//
WRITE_REGISTER_ULONG((PULONG)(Port->Address + USIF_TXD), (ULONG)Byte);
return UartSuccess;
}
BOOLEAN
UsifRxReady (
_Inout_ PCPPORT Port
)
/*++
Routine Description:
This routine determines if there is data pending in the UART.
Arguments:
Port - Supplies the address of the port object that describes the UART.
Return Value:
TRUE if data is available, FALSE otherwise.
--*/
{
ULONG Stat;
if ((Port == NULL) || (Port->Address == NULL)) {
return FALSE;
}
//
// Read the STAT Register to determine if there is any pending
// data to read.
//
Stat = READ_REGISTER_ULONG((PULONG)(Port->Address + USIF_FIFO_STAT));
if ((Stat & USIF_FIFO_STAT_RXFFS) != 0) {
return TRUE;
}
return FALSE;
}
// -------------------------------------------------------------------- Globals
UART_HARDWARE_DRIVER UsifHardwareDriver = {
UsifInitializePort,
UsifSetBaud,
UsifGetByte,
UsifPutByte,
UsifRxReady
};