-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpl011.cpp
360 lines (297 loc) · 8.16 KB
/
pl011.cpp
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
349
350
351
352
353
354
355
356
357
358
359
360
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <list>
#include <muos/array.h>
#include <muos/bits.h>
#include <muos/compiler.h>
#include <muos/error.h>
#include <muos/io.h>
#include <muos/message.h>
#include <muos/naming.h>
#include <muos/process.h>
#include "uart.h"
#define VERSATILE_UART0_BASE 0x101F1000
#define VERSATILE_UART0_IRQ 12
#define PL011_MMAP_SIZE 4096
typedef struct
{
uint32_t DR; /* Offset 0x000 */
uint32_t SR; /* Offset 0x004 */
uint32_t reserved1; /* Offset 0x008 */
uint32_t reserved2; /* Offset 0x00c */
uint32_t reserved3; /* Offset 0x010 */
uint32_t reserved4; /* Offset 0x014 */
uint32_t const FR; /* Offset 0x018 */
uint32_t reserved5; /* Offset 0x01c */
uint32_t ILPR; /* Offset 0x020 */
uint32_t IBRD; /* Offset 0x024 */
uint32_t FBRD; /* Offset 0x028 */
uint32_t LCR_H; /* Offset 0x02C */
uint32_t CR; /* Offset 0x030 */
uint32_t IFLS; /* Offset 0x034 */
uint32_t IMSC; /* Offset 0x038 */
uint32_t const RIS; /* Offset 0x03C */
uint32_t const MIS; /* Offset 0x040 */
uint32_t ICR; /* Offset 0x044 */
uint32_t DMACR; /* Offset 0x048 */
} pl011_t;
COMPILER_ASSERT(sizeof(pl011_t) == (0x048 + 4));
enum
{
/**
* \brief Receive Fifo Empty
*
* Bit in FR register which, when set, says that there are not
* bytes available to read.
*/
FR_RXFE = SETBIT(4),
/**
* \brief Transmit Fifo Full
*
* Bit in FR register which, when set, says that the output
* pipeline is full.
*/
FR_TXFF = SETBIT(5),
};
enum
{
/**
* \brief Transmit enabled
*/
CR_TXE = SETBIT(8),
/**
* \brief Receive enabled
*/
CR_RXE = SETBIT(9),
/**
* \brief UART enabled
*/
CR_UARTEN = SETBIT(0),
};
enum
{
/**
* \brief Receive interrupt masked
*/
IMSC_RX = SETBIT(4),
/**
* \brief Transmit interrupt masked
*/
IMSC_TX = SETBIT(5),
};
enum
{
/**
* \brief Receive interrupt is high
*/
MIS_RX = SETBIT(4),
/**
* \brief Transmit interrupt is high
*/
MIS_TX = SETBIT(5),
};
enum
{
/**
* \brief Clear Receive interrupt
*/
ICR_RX = SETBIT(4),
/**
* \brief Clear Transmit interrupt
*/
ICR_TX = SETBIT(5),
/**
* \brief Clear all interrupts
*/
ICR_ALL = 0x7ff,
};
static bool pl011_read_ready (pl011_t volatile * uart)
{
return (uart->FR & FR_RXFE) == 0;
}
static uint8_t pl011_blocking_read (pl011_t volatile * uart)
{
while (!pl011_read_ready(uart));
return uart->DR;
}
static bool pl011_write_ready (pl011_t volatile * uart)
{
return (uart->FR & FR_TXFF) == 0;
}
static void pl011_blocking_write (pl011_t volatile * uart, uint8_t c)
{
while (!pl011_write_ready(uart));
uart->DR = c;
}
static void pl011_printf (pl011_t volatile * uart,
const char format[],
...)
{
char buf[64];
va_list list;
int num;
int i;
va_start(list, format);
vsnprintf(buf, sizeof(buf), format, list);
va_end(list);
num = strlen(buf);
for (i = 0; i < num; ++i) {
pl011_blocking_write(uart, buf[i]);
}
}
char my_toupper (char c)
{
if (c >= 'a' && c <= 'z') {
return c + ('A' - 'a');
}
else if (c >= 'A' && c <= 'Z') {
return c;
}
else if (c == '\r' || c == '\n') {
return c;
}
else {
return '?';
}
}
class Read
{
public:
int mMessageId;
size_t const mLen;
size_t mLenDone;
uint8_t * mBuf;
Read (int aMessageId, size_t size)
: mMessageId(aMessageId)
, mLen(size)
, mLenDone(0)
{
mBuf = (uint8_t *)malloc(mLen);
}
~Read ()
{
free(mBuf);
}
};
std::list<Read *> gReads;
Read * gCurrentRead = NULL;
void pl011_isr (pl011_t volatile * uart, int irq_handler_id)
{
uint32_t mis = uart->MIS;
if (mis & MIS_TX) {
/*
The transmit interrupt generated by somebody sending.
There's no real work to do here, so just clear it and move on.
*/
uart->ICR = ICR_TX;
}
if (mis & MIS_RX) {
while (pl011_read_ready(uart)) {
uint8_t payload;
payload = pl011_blocking_read(uart);
if (!gCurrentRead && !gReads.empty()) {
gCurrentRead = *gReads.begin();
gReads.erase(gReads.begin());
}
if (gCurrentRead) {
assert(gCurrentRead->mLen > gCurrentRead->mLenDone);
gCurrentRead->mBuf[gCurrentRead->mLenDone] = payload;
gCurrentRead->mLenDone++;
if (gCurrentRead->mLenDone == gCurrentRead->mLen) {
struct iovec msg_parts[2];
UartReply reply;
reply.type = UART_MESSAGE_READ;
reply.payload.read.len = gCurrentRead->mLen;
msg_parts[0].iov_base = &reply;
msg_parts[0].iov_len = offsetof(UartReply, payload.read.buf);
msg_parts[1].iov_base = gCurrentRead->mBuf;
msg_parts[1].iov_len = gCurrentRead->mLen;
MessageReplyV(gCurrentRead->mMessageId,
ERROR_OK,
msg_parts,
N_ELEMENTS(msg_parts));
delete gCurrentRead;
gCurrentRead = NULL;
}
}
}
uart->ICR = ICR_RX;
}
InterruptComplete(irq_handler_id);
}
#define MAX(_a, _b) \
({ \
typeof(_a) a = _a; \
typeof(_b) b = _b; \
a > b ? a : b; \
})
#define MIN(_a, _b) \
({ \
typeof(_a) a = _a; \
typeof(_b) b = _b; \
a < b ? a : b; \
})
int main (int argc, char *argv[]) {
typedef union
{
struct Pulse async;
UartMessage sync;
} Msg_t;
Msg_t msg;
pl011_t volatile * uart0;
int irq_handler_id;
int chid;
int coid;
chid = NameAttach("/dev/uart");
coid = Connect(SELF_PID, chid);
Spawn("uio");
Spawn("terminal");
uart0 = (volatile pl011_t *)MapPhysical(VERSATILE_UART0_BASE, PL011_MMAP_SIZE);
pl011_printf(uart0, "PL011 UART driver started up in echo mode...\n");
// Clear all pending interrupts
uart0->ICR = ICR_ALL;
// Enable interrupts
uart0->IMSC = (IMSC_RX | IMSC_TX);
irq_handler_id = InterruptAttach(coid, VERSATILE_UART0_IRQ, NULL);
// Main interrupt-handling loop
for (;;) {
int msgid;
MessageReceive(chid, &msgid, &msg, sizeof(msg));
if (msgid == 0) {
// Pulse
pl011_isr(uart0, irq_handler_id);
}
else {
if (msg.sync.type == UART_MESSAGE_READ) {
gReads.push_back(new Read(msgid, msg.sync.payload.read.len));
}
else if (msg.sync.type == UART_MESSAGE_WRITE) {
for (size_t i = 0; i < msg.sync.payload.write.len; ++i) {
uint8_t b;
MessageRead(msgid, offsetof(UartMessage, payload.write.buf) + i, &b, sizeof(b));
pl011_blocking_write(uart0, b);
}
UartReply reply;
reply.type = UART_MESSAGE_WRITE;
reply.payload.write.len = msg.sync.payload.write.len;
MessageReply(msgid, ERROR_OK, &reply, sizeof(reply));
}
else {
pl011_printf(uart0, "Got unknown message\n");
MessageReply(msgid, ERROR_NO_SYS, NULL, 0);
}
}
}
// Disable interrupts
uart0->IMSC &= ~(IMSC_RX | IMSC_TX);
InterruptDetach(irq_handler_id);
while (true) {
uint8_t c = pl011_blocking_read(uart0);
pl011_blocking_write(uart0, my_toupper(c));
}
return 0;
}