-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsio.c
More file actions
610 lines (480 loc) · 11.3 KB
/
sio.c
File metadata and controls
610 lines (480 loc) · 11.3 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
** SCCS ID: @(#)sio.c 1.3 4/17/15
**
** File: sio.c
**
** Author: Warren R. Carithers
**
** Contributor:
**
** Description: SIO module
**
** Our SIO scheme is very simple:
**
** Input: We maintain a buffer of incoming characters that haven't
** yet been read by processes. When a character comes in,
** if there is no process waiting for it, it goes in the
** buffer; otherwise, the first waiting process is awakened
** and it gets the character.
**
** When a process invokes read(), if there is a character in
** the input buffer, the process gets it; otherwise, it gets
** a return count of -1.
**
** Communication with system calls is via two routines.
** _sio_readc() returns the first available character (if
** there is one), resetting the input variables if this was
** the last character in the buffer. If there are no
** characters in the buffer, _sio_read() returns a -1
** (presumably so the requesting process can be blocked).
**
** _sio_reads() copies the contents of the input buffer into
** a user-supplied buffer. It returns the number of characters
** copied. If there are no characters available, return a
** -1.
**
** Output: We maintain a buffer of outgoing characters that haven't
** yet been sent to the device, and an indication of whether
** or not we are in the middle of a transmit sequence. When
** an interrupt comes in, if there is another character to
** send we copy it to the transmitter buffer; otherwise, we
** end the transmit sequence.
**
** Communication with user processes is via three functions.
** _sio_writec() writes a single character; _sio_writes()
** writes a buffer full of characters; _sio_puts() prints
** a NUL-terminated string. If we are in the middle of a
** transmit sequence, all characters will be added to the
** output buffer (from where they will be sent
** automatically); otherwise, we send the first character
** directly, add the rest of the characters (if there are
** any) to the output buffer, and set the "sending" flag
** to indicate that we're expecting a transmitter interrupt.
*/
#define __SP_KERNEL__
#include "common.h"
#include "sio.h"
#include "queue.h"
#include "process.h"
#include "scheduler.h"
#include "system.h"
#include "startup.h"
#include <uart.h>
#include <x86arch.h>
/*
** PRIVATE DEFINITIONS
*/
#define BUF_SIZE 2048
/*
** PRIVATE GLOBALS
*/
// input character buffer
static char _inbuffer[ BUF_SIZE ];
static char *_inlast;
static char *_innext;
static uint32_t _incount;
// output character buffer
static char _outbuffer[ BUF_SIZE ];
static char *_outlast;
static char *_outnext;
static uint32_t _outcount;
// output control flag
static int _sending;
// interrupt register status
static uint8_t _ier;
/*
** PUBLIC GLOBAL VARIABLES
*/
/*
** PRIVATE FUNCTIONS
*/
/*
** PUBLIC FUNCTIONS
*/
/*
** _sio_isr(vector,code)
**
** Interrupt handler for the SIO module. Handles all pending
** events (as described by the SIO controller).
*/
static void _sio_isr( int vector, int code ) {
int eir, lsr, msr;
int ch;
//
// Must process all pending events; loop until the EIR
// says there's nothing else to do.
//
for(;;) {
// get the "pending event" indicator
eir = __inb( UA4_EIR ) & UA4_EIR_INT_PRI_MASK;
// process this event
switch( eir ) {
case UA4_EIR_LINE_STATUS_INT_PENDING:
// shouldn't happen, but just in case....
lsr = __inb( UA4_LSR );
c_printf( "** SIO line status, LSR = %02x\n", lsr );
break;
case UA4_EIR_RX_INT_PENDING:
// get the character
ch = __inb( UA4_RXD );
if( ch == '\r' ) { // map CR to LF
ch = '\n';
}
//
// Add to the input buffer
// if there is room, otherwise just ignore it.
//
if( _incount < BUF_SIZE ) {
*_inlast++ = ch;
++_incount;
}
break;
case UA5_EIR_RX_FIFO_TIMEOUT_INT_PENDING:
// shouldn't happen, but just in case....
ch = __inb( UA4_RXD );
c_printf( "** SIO FIFO timeout, RXD = %02x\n", ch );
break;
case UA4_EIR_TX_INT_PENDING:
// if there is another character, send it
if( _sending && _outcount > 0 ) {
__outb( UA4_TXD, *_outnext );
++_outnext;
// wrap around if necessary
if( _outnext >= (_outbuffer + BUF_SIZE) ) {
_outnext = _outbuffer;
}
--_outcount;
} else {
// no more data - reset the output vars
_outcount = 0;
_outlast = _outnext = _outbuffer;
_sending = 0;
// disable TX interrupts
_sio_disable( SIO_TX );
}
break;
case UA4_EIR_NO_INT:
// nothing to do - tell the PIC we're done
__outb( PIC_MASTER_CMD_PORT, PIC_EOI );
return;
case UA4_EIR_MODEM_STATUS_INT_PENDING:
// shouldn't happen, but just in case....
msr = __inb( UA4_MSR );
c_printf( "** SIO modem status, MSR = %02x\n", msr );
break;
default:
// make sure the code prints
_kpanic( "_sio_isr", "unknown device status" );
}
}
// should never reach this point!
_kpanic( "_sio_isr", "infinite loop ended???" );
}
/*
** _sio_modinit()
**
** Initialize the UART chip.
*/
void _sio_modinit( void ) {
/*
** Initialize SIO variables.
*/
_memset( (void *) _inbuffer, sizeof(_inbuffer), 0 );
_inlast = _innext = _inbuffer;
_incount = 0;
_memset( (void *) _outbuffer, sizeof(_outbuffer), 0 );
_outlast = _outnext = _outbuffer;
_outcount = 0;
_sending = 0;
/*
** Next, initialize the UART.
*/
/*
** Initialize the FIFOs
**
** this is a bizarre little sequence of operations
*/
__outb( UA4_FCR, 0x20 );
__outb( UA4_FCR, UA5_FCR_FIFO_RESET ); // 0x00
__outb( UA4_FCR, UA5_FCR_FIFO_EN ); // 0x01
__outb( UA4_FCR, UA5_FCR_FIFO_EN |
UA5_FCR_RXSR ); // 0x03
__outb( UA4_FCR, UA5_FCR_FIFO_EN |
UA5_FCR_RXSR |
UA5_FCR_TXSR ); // 0x07
/*
** disable interrupts
**
** note that we leave them disabled; _sio_enable() must be
** called to switch them back on
*/
__outb( UA4_IER, 0 );
_ier = 0;
/*
** select bank 1 and set the data rate
*/
__outb( UA4_LCR, UA4_LCR_BANK1 );
__outb( UA4_LBGD_L, BAUD_LOW_BYTE( BAUD_9600 ) );
__outb( UA4_LBGD_H, BAUD_HIGH_BYTE( BAUD_9600 ) );
/*
** Select bank 0, and at the same time set the LCR for our
** data characteristics.
*/
__outb( UA4_LCR, UA4_LCR_BANK0 |
UA4_LCR_BITS_8 |
UA4_LCR_1_STOP_BIT |
UA4_LCR_NO_PARITY );
/*
** Set the ISEN bit to enable the interrupt request signal.
*/
__outb( UA4_MCR, UA4_MCR_ISEN | UA4_MCR_DTR | UA4_MCR_RTS );
/*
** Install our ISR
*/
__install_isr( INT_VEC_SERIAL_PORT_1, _sio_isr );
/*
** Report that we're done.
*/
c_puts( " SIO" );
}
/*
** _sio_enable()
**
** enable SIO interrupts
**
** usage: old = _sio_enable( which )
**
** enables interrupts according to the 'which' parameter
**
** returns the prior settings
*/
uint8_t _sio_enable( uint8_t which ) {
uint8_t old;
// remember the current status
old = _ier;
// figure out what to enable
if( which & SIO_TX ) {
_ier |= UA4_IER_TX_INT_ENABLE;
}
if( which & SIO_RX ) {
_ier |= UA4_IER_RX_INT_ENABLE;
}
// if there was a change, make it
if( old != _ier ) {
__outb( UA4_IER, _ier );
}
// return the prior settings
return( old );
}
/*
** _sio_disable()
**
** disable SIO interrupts
**
** usage: old = _sio_disable( which )
**
** disables interrupts according to the 'which' parameter
**
** returns the prior settings
*/
uint8_t _sio_disable( uint8_t which ) {
uint8_t old;
// remember the current status
old = _ier;
// figure out what to disable
if( which & SIO_TX ) {
_ier &= ~UA4_IER_TX_INT_ENABLE;
}
if( which & SIO_RX ) {
_ier &= ~UA4_IER_RX_INT_ENABLE;
}
// if there was a change, make it
if( old != _ier ) {
__outb( UA4_IER, _ier );
}
// return the prior settings
return( old );
}
/*
** _sio_input_queue()
**
** get the input queue length
**
** usage: num = _sio_input_queue()
**
** returns the count of characters still in the input queue
*/
int _sio_input_queue( void ) {
return( _incount );
}
/*
** _sio_readc()
**
** get the next input character
**
** usage: ch = _sio_readc()
**
** returns the next character, or -1 if no character is available
*/
int _sio_readc( void ) {
int ch;
// assume there is no character available
ch = -1;
//
// If there is a character, return it
//
if( _incount > 0 ) {
// take it out of the input buffer
ch = ((int)(*_innext++)) & 0xff;
--_incount;
// reset the buffer variables if this was the last one
if( _incount < 1 ) {
_inlast = _innext = _inbuffer;
}
}
return( ch );
}
/*
** _sio_reads(buf,length)
**
** read the entire input buffer into a user buffer of a specified size
**
** returns the number of bytes copied, or 0 if no characters were available
*/
int _sio_reads( char *buf, int length ) {
char *ptr = buf;
int copied = 0;
// if there are no characters, just return 0
if( _incount < 1 ) {
return( 0 );
}
//
// We have characters. Copy as many of them into the user
// buffer as will fit.
//
while( _incount > 0 && copied < length ) {
*ptr++ = *_innext++ & 0xff;
if( _innext > (_inbuffer + BUF_SIZE) ) {
_innext = _inbuffer;
}
--_incount;
++copied;
}
// reset the input buffer if necessary
if( _incount < 1 ) {
_inlast = _innext = _inbuffer;
}
// return the copy count
return( copied );
}
/*
** _sio_writec( ch )
**
** write a character to the serial output
**
** usage: _sio_writec( ch )
*/
void _sio_writec( int ch ){
//
// Must do LF -> CRLF mapping
//
if( ch == '\n' ) {
_sio_writec( '\r' );
}
//
// If we're currently transmitting, just add this to the buffer
//
if( _sending ) {
*_outlast++ = ch;
++_outcount;
return;
}
//
// Not sending - must prime the pump
//
_sending = 1;
__outb( UA4_TXD, ch );
// Also must enable transmitter interrupts
_sio_enable( SIO_TX );
}
/*
** _sio_writes( buffer, length )
**
** write a buffer of characters to the serial output
**
** returns the number of characters copied into the buffer
*/
int _sio_writes( char *buffer, int length ) {
int first = *buffer;
char *ptr = buffer;
int copied = 0;
//
// If we are currently sending, we want to append all
// the characters to the output buffer; else, we want
// to append all but the first character, and then use
// _sio_writec() to send the first one out.
//
if( !_sending ) {
ptr += 1;
copied++;
}
while( copied < length && _outcount < BUF_SIZE ) {
*_outlast++ = *ptr++;
// wrap around if necessary
if( _outlast >= (_outbuffer + BUF_SIZE) ) {
_outlast = _outbuffer;
}
++_outcount;
++copied;
}
//
// We use _sio_writec() to send out the first character,
// as it will correctly set all the other necessary
// variables for us.
//
if( !_sending ) {
_sio_writec( first );
}
// Return the transfer count
return( copied );
}
/*
** _sio_puts( buf )
**
** write a NUL-terminated buffer of characters to the serial output
**
** usage: n = _sio_puts( char *buffer );
**
** returns the count of bytes transferred
*/
int _sio_puts( char *buffer ) {
int n;
for( n = 0; *buffer; ++n ) {
_sio_writec( *buffer++ );
}
return( n );
}
/*
** _sio_dump()
**
** dump the contents of the SIO buffers
*/
void _sio_dump( void ) {
int n;
char *ptr;
c_printf( "SIO buffers: in %d ot %d\n", _incount, _outcount );
if( _incount ) {
c_puts( " in: \"" );
ptr = _innext;
for( n = 0; n < _incount; ++n )
_put_char_or_code( *ptr++ );
c_puts( "\"\n" );
}
if( _outcount ) {
c_puts( " ot: \"" );
ptr = _outnext;
for( n = 0; n < _outcount; ++n )
_put_char_or_code( *ptr++ );
c_puts( "\"\n" );
}
}