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
|
#include <sys/time.h>
#include "devices.h"
#include "processor.h"
#include "term.h"
#define UART_QUEUE_SIZE 64
#define UART_RX 0 /* In: Receive buffer */
#define UART_TX 0 /* Out: Transmit buffer */
#define UART_IER 1 /* Out: Interrupt Enable Register */
#define UART_IER_MSI 0x08 /* Enable Modem status interrupt */
#define UART_IER_RLSI 0x04 /* Enable receiver line status interrupt */
#define UART_IER_THRI 0x02 /* Enable Transmitter holding register int. */
#define UART_IER_RDI 0x01 /* Enable receiver data interrupt */
#define UART_IIR 2 /* In: Interrupt ID Register */
#define UART_IIR_NO_INT 0x01 /* No interrupts pending */
#define UART_IIR_ID 0x0e /* Mask for the interrupt ID */
#define UART_IIR_MSI 0x00 /* Modem status interrupt */
#define UART_IIR_THRI 0x02 /* Transmitter holding register empty */
#define UART_IIR_RDI 0x04 /* Receiver data interrupt */
#define UART_IIR_RLSI 0x06 /* Receiver line status interrupt */
#define UART_IIR_TYPE_BITS 0xc0
#define UART_FCR 2 /* Out: FIFO Control Register */
#define UART_FCR_ENABLE_FIFO 0x01 /* Enable the FIFO */
#define UART_FCR_CLEAR_RCVR 0x02 /* Clear the RCVR FIFO */
#define UART_FCR_CLEAR_XMIT 0x04 /* Clear the XMIT FIFO */
#define UART_FCR_DMA_SELECT 0x08 /* For DMA applications */
#define UART_LCR 3 /* Out: Line Control Register */
#define UART_LCR_DLAB 0x80 /* Divisor latch access bit */
#define UART_LCR_SBC 0x40 /* Set break control */
#define UART_LCR_SPAR 0x20 /* Stick parity (?) */
#define UART_LCR_EPAR 0x10 /* Even parity select */
#define UART_LCR_PARITY 0x08 /* Parity Enable */
#define UART_LCR_STOP 0x04 /* Stop bits: 0=1 bit, 1=2 bits */
#define UART_MCR 4 /* Out: Modem Control Register */
#define UART_MCR_LOOP 0x10 /* Enable loopback test mode */
#define UART_MCR_OUT2 0x08 /* Out2 complement */
#define UART_MCR_OUT1 0x04 /* Out1 complement */
#define UART_MCR_RTS 0x02 /* RTS complement */
#define UART_MCR_DTR 0x01 /* DTR complement */
#define UART_LSR 5 /* In: Line Status Register */
#define UART_LSR_FIFOE 0x80 /* Fifo error */
#define UART_LSR_TEMT 0x40 /* Transmitter empty */
#define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */
#define UART_LSR_BI 0x10 /* Break interrupt indicator */
#define UART_LSR_FE 0x08 /* Frame error indicator */
#define UART_LSR_PE 0x04 /* Parity error indicator */
#define UART_LSR_OE 0x02 /* Overrun error indicator */
#define UART_LSR_DR 0x01 /* Receiver data ready */
#define UART_LSR_BRK_ERROR_BITS 0x1E /* BI, FE, PE, OE bits */
#define UART_MSR 6 /* In: Modem Status Register */
#define UART_MSR_DCD 0x80 /* Data Carrier Detect */
#define UART_MSR_RI 0x40 /* Ring Indicator */
#define UART_MSR_DSR 0x20 /* Data Set Ready */
#define UART_MSR_CTS 0x10 /* Clear to Send */
#define UART_MSR_DDCD 0x08 /* Delta DCD */
#define UART_MSR_TERI 0x04 /* Trailing edge ring indicator */
#define UART_MSR_DDSR 0x02 /* Delta DSR */
#define UART_MSR_DCTS 0x01 /* Delta CTS */
#define UART_MSR_ANY_DELTA 0x0F /* Any of the delta bits! */
#define UART_SCR 7 /* I/O: Scratch Register */
ns16550_t::ns16550_t(abstract_interrupt_controller_t *intctrl,
uint32_t interrupt_id, uint32_t reg_shift, uint32_t reg_io_width)
: intctrl(intctrl), interrupt_id(interrupt_id), reg_shift(reg_shift), reg_io_width(reg_io_width), backoff_counter(0)
{
ier = 0;
iir = UART_IIR_NO_INT;
fcr = 0;
lcr = 0;
lsr = UART_LSR_TEMT | UART_LSR_THRE;
msr = UART_MSR_DCD | UART_MSR_DSR | UART_MSR_CTS;
dll = 0x0C;
mcr = UART_MCR_OUT2;
scr = 0;
}
void ns16550_t::update_interrupt(void)
{
uint8_t interrupts = 0;
/* Handle clear rx */
if (lcr & UART_FCR_CLEAR_RCVR) {
lcr &= ~UART_FCR_CLEAR_RCVR;
while (!rx_queue.empty()) {
rx_queue.pop();
}
lsr &= ~UART_LSR_DR;
}
/* Handle clear tx */
if (lcr & UART_FCR_CLEAR_XMIT) {
lcr &= ~UART_FCR_CLEAR_XMIT;
lsr |= UART_LSR_TEMT | UART_LSR_THRE;
}
/* Data ready and rcv interrupt enabled ? */
if ((ier & UART_IER_RDI) && (lsr & UART_LSR_DR)) {
interrupts |= UART_IIR_RDI;
}
/* Transmitter empty and interrupt enabled ? */
if ((ier & UART_IER_THRI) && (lsr & UART_LSR_TEMT)) {
interrupts |= UART_IIR_THRI;
}
/* Now update the interrup line, if necessary */
if (!interrupts) {
iir = UART_IIR_NO_INT;
intctrl->set_interrupt_level(interrupt_id, 0);
} else {
iir = interrupts;
intctrl->set_interrupt_level(interrupt_id, 1);
}
/*
* If the OS disabled the tx interrupt, we know that there is nothing
* more to transmit.
*/
if (!(ier & UART_IER_THRI)) {
lsr |= UART_LSR_TEMT | UART_LSR_THRE;
}
}
uint8_t ns16550_t::rx_byte(void)
{
if (rx_queue.empty()) {
lsr &= ~UART_LSR_DR;
return 0;
}
/* Break issued ? */
if (lsr & UART_LSR_BI) {
lsr &= ~UART_LSR_BI;
return 0;
}
uint8_t ret = rx_queue.front();
rx_queue.pop();
if (rx_queue.empty()) {
lsr &= ~UART_LSR_DR;
}
return ret;
}
void ns16550_t::tx_byte(uint8_t val)
{
lsr |= UART_LSR_TEMT | UART_LSR_THRE;
canonical_terminal_t::write(val);
}
bool ns16550_t::load(reg_t addr, size_t len, uint8_t* bytes)
{
uint8_t val;
bool ret = true, update = false;
if (reg_io_width != len) {
return false;
}
addr >>= reg_shift;
addr &= 7;
switch (addr) {
case UART_RX:
if (lcr & UART_LCR_DLAB) {
val = dll;
} else {
val = rx_byte();
}
update = true;
break;
case UART_IER:
if (lcr & UART_LCR_DLAB) {
val = dlm;
} else {
val = ier;
}
break;
case UART_IIR:
val = iir | UART_IIR_TYPE_BITS;
break;
case UART_LCR:
val = lcr;
break;
case UART_MCR:
val = mcr;
break;
case UART_LSR:
val = lsr;
break;
case UART_MSR:
val = msr;
break;
case UART_SCR:
val = scr;
break;
default:
ret = false;
break;
};
if (ret) {
bytes[0] = val;
}
if (update) {
update_interrupt();
}
return ret;
}
bool ns16550_t::store(reg_t addr, size_t len, const uint8_t* bytes)
{
uint8_t val;
bool ret = true, update = false;
if (reg_io_width != len) {
return false;
}
addr >>= reg_shift;
addr &= 7;
val = bytes[0];
switch (addr) {
case UART_TX:
update = true;
if (lcr & UART_LCR_DLAB) {
dll = val;
break;
}
/* Loopback mode */
if (mcr & UART_MCR_LOOP) {
if (rx_queue.size() < UART_QUEUE_SIZE) {
rx_queue.push(val);
lsr |= UART_LSR_DR;
}
break;
}
tx_byte(val);
break;
case UART_IER:
if (!(lcr & UART_LCR_DLAB)) {
ier = val & 0x0f;
} else {
dlm = val;
}
update = true;
break;
case UART_FCR:
fcr = val;
update = true;
break;
case UART_LCR:
lcr = val;
update = true;
break;
case UART_MCR:
mcr = val;
update = true;
break;
case UART_LSR:
/* Factory test */
break;
case UART_MSR:
/* Not used */
break;
case UART_SCR:
scr = val;
break;
default:
ret = false;
break;
};
if (update) {
update_interrupt();
}
return ret;
}
void ns16550_t::tick(reg_t UNUSED rtc_ticks)
{
if (!(fcr & UART_FCR_ENABLE_FIFO) ||
(mcr & UART_MCR_LOOP) ||
(UART_QUEUE_SIZE <= rx_queue.size())) {
return;
}
if (backoff_counter > 0 && backoff_counter < MAX_BACKOFF) {
backoff_counter++;
return;
}
int rc = canonical_terminal_t::read();
if (rc < 0) {
backoff_counter = 1;
return;
}
backoff_counter = 0;
rx_queue.push((uint8_t)rc);
lsr |= UART_LSR_DR;
update_interrupt();
}
|