summaryrefslogtreecommitdiff
path: root/kernel/old/sio.c
blob: a5c7b758f8518fe48e13b88248f1197b2d5f17f9 (plain)
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/**
** @file	sio.c
**
** @author	Warren R. Carithers
**
** @brief	SIO module
**
** For maximum compatibility from semester to semester, this code uses
** several "stand-in" type names and macros which should be defined
** in the accompanying "compat.h" header file if they're not part of
** the baseline system:
**
**      standard-sized integer types:  intN_t, uintN_t
**      other types:  PCBTYPE, QTYPE
**      scheduler functions:  SCHED, DISPATCH
**      queue functions:  QCREATE, QLENGTH, QDEQUE
**      other functions:  SLENGTH
**      sio read queue:  QNAME
**
** 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 awakeneda and it
**      gets the character.
**
**      When a process invokes readch(), if there is a character in
**      the input buffer, the process gets it; otherwise, it is
**      blocked until input appears
**
**      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_read() 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_write()
**      writes a sized 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 KERNEL_SRC

// this should do all includes required for this OS
#include <compat.h>

// all other framework includes are next
#include <x86/uart.h>
#include <x86/arch.h>
#include <x86/pic.h>

#include <sio.h>
#include <lib.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
*/

// queue for read-blocked processes
#ifdef QNAME
QTYPE QNAME;
#endif

/*
** PRIVATE FUNCTIONS
*/

/**
** sio_isr(vector,ecode)
**
** Interrupt handler for the SIO module.  Handles all pending
** events (as described by the SIO controller).
**
** @param vector   The interrupt vector number for this interrupt
** @param ecode    The error code associated with this interrupt
*/
static void sio_isr( int vector, int ecode ) {
	int ch;

#if TRACING_SIO_ISR
	cio_puts( "SIO: int:" );
#endif
	//
	// Must process all pending events; loop until the IRR
	// says there's nothing else to do.
	//

	for(;;) {

		// get the "pending event" indicator
		int iir = inb( UA4_IIR ) & UA4_IIR_INT_PRI_MASK;

		// process this event
		switch( iir ) {

		case UA4_IIR_LINE_STATUS:
			// shouldn't happen, but just in case....
			cio_printf( "** SIO int, LSR = %02x\n", inb(UA4_LSR) );
			break;

		case UA4_IIR_RX:
#if TRACING_SIO_ISR
	cio_puts( " RX" );
#endif
			// get the character
			ch = inb( UA4_RXD );
			if( ch == '\r' ) {    // map CR to LF
				ch = '\n';
			}
#if TRACING_SIO_ISR
	cio_printf( " ch %02x", ch );
#endif

#ifdef QNAME
			//
			// If there is a waiting process, this must be
			// the first input character; give it to that
			// process and awaken the process.
			//

			if( !QEMPTY(QNAME) ) {
				PCBTYPE *pcb;

				QDEQUE( QNAME, pcb );
				// make sure we got a non-NULL result
				assert( pcb );

				// return char via arg #2 and count in EAX
				char *buf = (char *) ARG(pcb,2);
				*buf = ch & 0xff;
				RET(pcb) = 1;
				SCHED( pcb );

			} else {
#endif /* QNAME */

				//
				// Nobody waiting - add to the input buffer
				// if there is room, otherwise just ignore it.
				//

				if( incount < BUF_SIZE ) {
					*inlast++ = ch;
					++incount;
				}

#ifdef QNAME
			}
#endif /* QNAME */
			break;

		case UA5_IIR_RX_FIFO:
			// shouldn't happen, but just in case....
			ch = inb( UA4_RXD );
			cio_printf( "** SIO FIFO timeout, RXD = %02x\n", ch );
			break;

		case UA4_IIR_TX:
#if TRACING_SIO_ISR
	cio_puts( " TX" );
#endif
			// if there is another character, send it
			if( sending && outcount > 0 ) {
#if TRACING_SIO_ISR
	cio_printf( " ch %02x", *outnext );
#endif
				outb( UA4_TXD, *outnext );
				++outnext;
				// wrap around if necessary
				if( outnext >= (outbuffer + BUF_SIZE) ) {
					outnext = outbuffer;
				}
				--outcount;
#if TRACING_SIO_ISR
	cio_printf( " (outcount %d)", outcount );
#endif
			} else {
#if TRACING_SIO_ISR
	cio_puts( " EOS" );
#endif
				// no more data - reset the output vars
				outcount = 0;
				outlast = outnext = outbuffer;
				sending = 0;
				// disable TX interrupts
				sio_disable( SIO_TX );
			}
			break;

		case UA4_IIR_NO_INT:
#if TRACING_SIO_ISR
	cio_puts( " EOI\n" );
#endif
			// nothing to do - tell the PIC we're done
			outb( PIC1_CMD, PIC_EOI );
			return;

		case UA4_IIR_MODEM_STATUS:
			// shouldn't happen, but just in case....
			cio_printf( "** SIO int, MSR = %02x\n", inb(UA4_MSR) );
			break;

		default:
			// uh-oh....
			sprint( b256, "sio isr: IIR %02x\n", ((uint32_t) iir) & 0xff );
			PANIC( 0, b256 );
		}
	
	}

	// should never reach this point!
	assert( false );
}

/*
** PUBLIC FUNCTIONS
*/

/**
** sio_init()
**
** Initialize the UART chip.
*/
void sio_init( void ) {

#if TRACING_INIT
	cio_puts( " Sio" );
#endif

	/*
	** Initialize SIO variables.
	*/

	memclr( (void *) inbuffer, sizeof(inbuffer) );
	inlast = innext = inbuffer;
	incount = 0;

	memclr( (void *) outbuffer, sizeof(outbuffer) );
	outlast = outnext = outbuffer;
	outcount = 0;
	sending = 0;

	// queue of read-blocked processes
	QCREATE( QNAME );

	/*
	** Next, initialize the UART.
	**
	** Initialize the FIFOs
	**
	** this is a bizarre little sequence of operations
	*/

	outb( UA5_FCR, 0x20 );
	outb( UA5_FCR, UA5_FCR_FIFO_RESET );               // 0x00
	outb( UA5_FCR, UA5_FCR_FIFO_EN );                  // 0x01
	outb( UA5_FCR, UA5_FCR_FIFO_EN | UA5_FCR_RXSR );   // 0x03
	outb( UA5_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 the divisor latch registers and set the data rate
	*/

	outb( UA4_LCR, UA4_LCR_DLAB );
	outb( UA4_DLL, BAUD_LOW_BYTE( DL_BAUD_9600 ) );
	outb( UA4_DLM, BAUD_HIGH_BYTE( DL_BAUD_9600 ) );

	/*
	** deselect the latch registers, by setting the data
	** characteristics in the LCR
	*/

	outb( UA4_LCR, UA4_LCR_WLS_8 | UA4_LCR_1_STOP_BIT | UA4_LCR_NO_PARITY );
	
	/*
	** Set the ISEN bit to enable the interrupt request signal,
	** and the DTR and RTS bits to enable two-way communication.
	*/

	outb( UA4_MCR, UA4_MCR_ISEN | UA4_MCR_DTR | UA4_MCR_RTS );

	/*
	** Install our ISR
	*/

	install_isr( VEC_COM1, sio_isr );
}

/**
** sio_enable()
**
** Enable SIO interrupts
**
** usage:    uint8_t old = sio_enable( uint8_t which )
**
** @param which   Bit mask indicating which interrupt(s) to enable
**
** @return the prior IER setting
*/
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_IE;
	}

	if( which & SIO_RX ) {
		ier |= UA4_IER_RX_IE;
	}

	// 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:    uint8_t old = sio_disable( uint8_t which )
**
** @param which   Bit mask indicating which interrupt(s) to disable
**
** @return the prior IER setting
*/
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_IE;
	}

	if( which & SIO_RX ) {
		ier &= ~UA4_IER_RX_IE;
	}

	// if there was a change, make it

	if( old != ier ) {
		outb( UA4_IER, ier );
	}

	// return the prior settings

	return( old );
}

/**
** sio_inq_length()
**
** Get the input queue length
**
** usage:    int num = sio_inq_length()
**
** @return the count of characters still in the input queue
*/
int sio_inq_length( void ) {
	return( incount );
}

/**
** sio_readc()
**
** Get the next input character
**
** usage:    int ch = sio_readc()
**
** @return 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_read(buf,length)
**
** Read the entire input buffer into a user buffer of a specified size
**
** usage:    int num = sio_read( char *buffer, int length )
**
** @param buf     The destination buffer
** @param length  Length of the buffer
**
** @return the number of bytes copied, or 0 if no characters were available
*/

int sio_read( 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( int ch )
**
** @param ch   Character to be written (in the low-order 8 bits)
*/
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_write( buffer, length )
**
** Write a buffer of characters to the serial output
**
** usage:    int num = sio_write( const char *buffer, int length )
**
** @param buffer   Buffer containing characters to write
** @param length   Number of characters to write
**
** @return the number of characters copied into the SIO output buffer
*/
int sio_write( const char *buffer, int length ) {
	int first = *buffer;
	const 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:    int num = sio_puts( const char *buffer )
**
** @param buffer  The buffer containing a NUL-terminated string
**
** @return the count of bytes transferred
*/
int sio_puts( const char *buffer ) {
	int n;  // must be outside the loop so we can return it

	n = SLENGTH( buffer );
	sio_write( buffer, n );

	return( n );
}

/**
** sio_dump( full )
**
** dump the contents of the SIO buffers to the console
**
** usage:    sio_dump(true) or sio_dump(false)
**
** @param full   Boolean indicating whether or not a "full" dump
**               is being requested (which includes the contents
**               of the queues)
*/

void sio_dump( bool_t full ) {
	int n;
	char *ptr;

	// dump basic info into the status region

	cio_printf_at( 48, 0,
		"SIO: IER %02x (%c%c%c) in %d ot %d",
			((uint32_t)ier) & 0xff, sending ? '*' : '.',
			(ier & UA4_IER_TX_IE) ? 'T' : 't',
			(ier & UA4_IER_RX_IE) ? 'R' : 'r',
			incount, outcount );

	// if we're not doing a full dump, stop now

	if( !full ) {
		return;
	}

	// also want the queue contents, but we'll
	// dump them into the scrolling region

	if( incount ) {
		cio_puts( "SIO input queue: \"" );
		ptr = innext; 
		for( n = 0; n < incount; ++n ) {
			put_char_or_code( *ptr++ );
		}
		cio_puts( "\"\n" );
	}

	if( outcount ) {
		cio_puts( "SIO output queue: \"" );
		cio_puts( " ot: \"" );
		ptr = outnext; 
		for( n = 0; n < outcount; ++n )  {
			put_char_or_code( *ptr++ );
		}
		cio_puts( "\"\n" );
	}
}