-
Notifications
You must be signed in to change notification settings - Fork 42
/
16x50.c
303 lines (277 loc) · 6.78 KB
/
16x50.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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "serialdevice.h"
#include "16x50.h"
/* UART: very mimimal for the moment */
struct uart16x50 {
uint8_t ier;
uint8_t iir;
uint8_t fcr;
uint8_t lcr;
uint8_t mcr;
uint8_t lsr;
uint8_t msr;
uint8_t scratch;
uint8_t ls;
uint8_t ms;
uint8_t dlab;
uint8_t irq;
#define RXDA 1
#define TEMT 2
#define MODEM 8
uint8_t irqline;
unsigned clock;
int trace;
struct serial_device *dev;
};
void uart16x50_reset(struct uart16x50 *uptr)
{
uptr->dlab = 0;
uptr->iir = 1;
}
/* Compute the interrupt indicator register from what is pending */
static void uart16x50_recalc_iir(struct uart16x50 *uptr)
{
if (uptr->irq & RXDA)
uptr->iir = 0x04;
else if (uptr->irq & TEMT)
uptr->iir = 0x02;
else if (uptr->irq & MODEM)
uptr->iir = 0x00;
else {
uptr->iir = 0x01; /* No interrupt */
uptr->irqline = 0;
return;
}
/* Ok so we have an event, do we need to waggle the line */
if (uptr->irqline)
return;
uptr->irqline = uptr->irq;
}
/* Raise an interrupt source. Only has an effect if enabled in the ier */
static void uart16x50_interrupt(struct uart16x50 *uptr, uint8_t n)
{
if (uptr->irq & n)
return;
if (!(uptr->ier & n))
return;
uptr->irq |= n;
uart16x50_recalc_iir(uptr);
}
static void uart16x50_clear_interrupt(struct uart16x50 *uptr, uint8_t n)
{
if (!(uptr->irq & n))
return;
uptr->irq &= ~n;
uart16x50_recalc_iir(uptr);
}
void uart16x50_event(struct uart16x50 *uptr)
{
uint8_t r = uptr->dev->ready(uptr->dev);
uint8_t old = uptr->lsr;
uint8_t dhigh;
uptr->lsr &= ~0x61; /* Clear RX and TX bits */
if (r & 1)
uptr->lsr |= 0x01; /* RX not empty */
if (r & 2)
uptr->lsr |= 0x60; /* TX empty */
dhigh = (old ^ uptr->lsr);
dhigh &= uptr->lsr; /* Changed high bits */
if (dhigh & 1)
uart16x50_interrupt(uptr, RXDA);
if (dhigh & 0x2)
uart16x50_interrupt(uptr, TEMT);
}
static void show_settings(struct uart16x50 *uptr)
{
uint32_t baud;
if (uptr->trace == 0)
return;
baud = uptr->ls + (uptr->ms << 8);
if (baud == 0)
baud = uptr->clock;
baud = uptr->clock / baud;
baud /= 16;
fprintf(stderr, "[%d:%d", baud, (uptr->lcr &3) + 5);
switch(uptr->lcr & 0x38) {
case 0x00:
case 0x10:
case 0x20:
case 0x30:
fprintf(stderr, "N");
break;
case 0x08:
fprintf(stderr, "O");
break;
case 0x18:
fprintf(stderr, "E");
break;
case 0x28:
fprintf(stderr, "M");
break;
case 0x38:
fprintf(stderr, "S");
break;
}
fprintf(stderr, "%d ",
(uptr->lcr & 4) ? 2 : 1);
if (uptr->lcr & 0x40)
fprintf(stderr, "break ");
if (uptr->lcr & 0x80)
fprintf(stderr, "dlab ");
if (uptr->mcr & 1)
fprintf(stderr, "DTR ");
if (uptr->mcr & 2)
fprintf(stderr, "RTS ");
if (uptr->mcr & 4)
fprintf(stderr, "OUT1 ");
if (uptr->mcr & 8)
fprintf(stderr, "OUT2 ");
if (uptr->mcr & 16)
fprintf(stderr, "LOOP ");
fprintf(stderr, "ier %02x]\n", uptr->ier);
}
void uart16x50_write(struct uart16x50 *uptr, uint8_t addr, uint8_t val)
{
switch(addr) {
case 0: /* If dlab = 0, then write else LS*/
if (uptr->dlab == 0) {
uptr->dev->put(uptr->dev, val);
uart16x50_clear_interrupt(uptr, TEMT);
uart16x50_interrupt(uptr, TEMT);
} else {
uptr->ls = val;
show_settings(uptr);
}
break;
case 1: /* If dlab = 0, then IER */
if (uptr->dlab) {
uptr->ms= val;
show_settings(uptr);
}
else
uptr->ier = val;
break;
case 2: /* FCR */
uptr->fcr = val & 0x9F;
break;
case 3: /* LCR */
uptr->lcr = val;
uptr->dlab = (uptr->lcr & 0x80);
show_settings(uptr);
break;
case 4: /* MCR */
uptr->mcr = val & 0x1F;
uart16x50_signal_change(uptr, uptr->mcr);
show_settings(uptr);
break;
case 5: /* LSR (r/o) */
break;
case 6: /* MSR (r/o) */
break;
case 7: /* Scratch */
uptr->scratch = val;
break;
}
}
uint8_t uart16x50_read(struct uart16x50 *uptr, uint8_t addr)
{
uint8_t r;
switch(addr) {
case 0:
/* receive buffer */
if (uptr->dlab == 0) {
uart16x50_clear_interrupt(uptr, RXDA);
return uptr->dev->get(uptr->dev);
} else
return uptr->ls;
break;
case 1:
/* IER */
if (uptr->dlab == 0)
return uptr->ier;
else
return uptr->ms;
case 2:
/* IIR */
return uptr->iir;
case 3:
/* LCR */
return uptr->lcr;
case 4:
/* mcr */
return uptr->mcr;
case 5:
/* Reading the LSR causes these bits to clear */
r = uptr->lsr;
uptr->lsr &= 0xF0;
return r;
case 6:
/* msr */
r = uptr->msr;
/* Reading clears the delta bits */
uptr->msr &= 0xF0;
uart16x50_clear_interrupt(uptr, MODEM);
return r;
case 7:
return uptr->scratch;
}
return 0xFF;
}
/* Model the external timer on DSR mod */
void uart16x50_dsr_timer(struct uart16x50 *uart16x50)
{
uart16x50->msr ^= 0x20; /* DSR toggles */
uart16x50->msr |= 0x02; /* DSR delta */
uart16x50_interrupt(uart16x50, MODEM);
}
/* Model specific signal changes */
void uart16x50_signal_event(struct uart16x50 *uart16x50, uint8_t msr)
{
uint8_t delta = msr ^ uart16x50->msr;
if (delta) {
unsigned n;
for(n = 4; n <= 7; n++) {
if (delta & (1 << n))
uart16x50->msr |= (1 << (n - 4));
}
uart16x50->msr &= 0x0F;
uart16x50->msr |= (msr & 0xF0);
uart16x50_interrupt(uart16x50, MODEM);
}
}
void uart16x50_attach(struct uart16x50 *uart16x50, struct serial_device *dev)
{
uart16x50->dev = dev;
}
void uart16x50_trace(struct uart16x50 *uart16x50, int onoff)
{
uart16x50->trace = onoff;
}
uint8_t uart16x50_irq_pending(struct uart16x50 *d)
{
return !!d->irqline;
}
struct uart16x50 *uart16x50_create(void)
{
struct uart16x50 *d = malloc(sizeof(struct uart16x50));
if (d == NULL) {
fprintf(stderr, "Out of memory.\n");
exit(1);
}
memset(d, 0, sizeof(*d));
d->clock = 1843200; /* Usual default */
uart16x50_reset(d);
return d;
}
void uart16x50_set_clock(struct uart16x50 *d, unsigned clock)
{
d->clock = clock;
}
void uart16x50_free(struct uart16x50 *d)
{
free(d);
}