-
Notifications
You must be signed in to change notification settings - Fork 0
/
sam5250.c
348 lines (234 loc) · 7.15 KB
/
sam5250.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
Module Name:
sam5250.c
Abstract:
This module contains support for the Samsung 5250 serial UART.
--*/
// ------------------------------------------------------------------- Includes
#include "common.h"
// ---------------------------------------------------------------- Definitions
#define ULCON 0x00
#define UCON 0x04
#define UFCON 0x08
#define UTRSTAT 0x10
#define UERSTAT 0x14
#define UFSTAT 0x18
#define UTXH 0x20
#define URXH 0x24
#define UINTP 0x30
#define UINTM 0x38
#define UFSTAT_TXFE (1 << 24)
#define UTRSTAT_RXFE (1 << 0)
#define UERSTAT_OE (1 << 0)
#define UERSTAT_PE (1 << 1)
#define UERSTAT_FE (1 << 2)
#define UERSTAT_BE (1 << 3)
// ----------------------------------------------- Internal Function Prototypes
BOOLEAN
Sam5250SetBaud (
_Inout_ PCPPORT Port,
ULONG Rate
);
// ------------------------------------------------------------------ Functions
BOOLEAN
Sam5250InitializePort (
_In_opt_ _Null_terminated_ PCHAR LoadOptions,
_Inout_ PCPPORT Port,
BOOLEAN MemoryMapped,
UCHAR AccessSize,
UCHAR BitWidth
)
/*++
Routine Description:
This routine performs the initialization of a Samsung 5250 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;
//
// Disable UART.
//
WRITE_REGISTER_ULONG((PULONG)(Port->Address + UCON), 0);
//
// Set word length to 8 bits and disable parity.
//
WRITE_REGISTER_ULONG((PULONG)(Port->Address + ULCON), 0x3);
//
// Enable the FIFO.
//
WRITE_REGISTER_ULONG((PULONG)(Port->Address + UFCON), 0x1);
//
// Mask all interrupts.
//
WRITE_REGISTER_ULONG((PULONG)(Port->Address + UINTM), 0xF);
//
// Clear all interrupts.
//
WRITE_REGISTER_ULONG((PULONG)(Port->Address + UINTP), 0xF);
//
// Enable UART, enable Transmit (mode: 01) and Receive (mode: 01).
//
WRITE_REGISTER_ULONG((PULONG)(Port->Address + UCON), 0x5);
return TRUE;
}
BOOLEAN
Sam5250SetBaud (
_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;
}
//
// There is no way to determine reference clock frequency for this UART so
// just remember the desired baud rate but don't do anything else.
//
Port->BaudRate = Rate;
return TRUE;
}
UART_STATUS
Sam5250GetByte (
_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 Fsr;
ULONG Error;
ULONG Value;
if ((Port == NULL) || (Port->Address == NULL)) {
return UartNotReady;
}
//
// Get FIFO status.
//
Fsr = READ_REGISTER_ULONG((PULONG)(Port->Address + UTRSTAT));
//
// Is at least one character available?
//
if ((Fsr & UTRSTAT_RXFE) != 0) {
//
// Fetch the data byte and associated error information.
//
Value = READ_REGISTER_ULONG((PULONG)(Port->Address + URXH));
//
// Check for errors. Deliberately don't treat overrun as an error.
//
Error = READ_REGISTER_ULONG((PULONG)(Port->Address + UERSTAT));
if ((Error & (UERSTAT_PE | UERSTAT_FE | UERSTAT_BE)) != 0) {
*Byte = 0;
return UartError;
}
*Byte = Value & (UCHAR)0xFF;
return UartSuccess;
}
return UartNoData;
}
UART_STATUS
Sam5250PutByte (
_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.
//
// _ARM_WORKAROUND_ Modem control is not supported.
//
if (BusyWait != FALSE) {
while (READ_REGISTER_ULONG((PULONG)(Port->Address + UFSTAT)) & (UFSTAT_TXFE));
} else {
if (READ_REGISTER_ULONG((PULONG)(Port->Address + UFSTAT)) & (UFSTAT_TXFE)) {
return UartNotReady;
}
}
//
// Send the byte.
//
WRITE_REGISTER_ULONG((PULONG)(Port->Address + UTXH), (ULONG)Byte);
return UartSuccess;
}
BOOLEAN
Sam5250RxReady (
_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 Flags;
if ((Port == NULL) || (Port->Address == NULL)) {
return FALSE;
}
//
// Read the Flag Register to determine if there is any pending data to read.
//
Flags = READ_REGISTER_ULONG((PULONG)(Port->Address + UTRSTAT));
if ((Flags & UTRSTAT_RXFE) != 0) {
return TRUE;
}
return FALSE;
}
// -------------------------------------------------------------------- Globals
UART_HARDWARE_DRIVER Sam5250HardwareDriver = {
Sam5250InitializePort,
Sam5250SetBaud,
Sam5250GetByte,
Sam5250PutByte,
Sam5250RxReady
};