summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/powerpc/ppcn_60x/console/ns16550.c
blob: 30c10f73b2ed55748669e0fe7d2a516a173385dc (plain) (blame)
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
/*
 *  This file contains the TTY driver for the NS16550
 *
 *  COPYRIGHT (c) 1998 by Radstone Technology
 *
 *
 * THIS FILE IS PROVIDED TO YOU, THE USER, "AS IS", WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK
 * AS TO THE QUALITY AND PERFORMANCE OF ALL CODE IN THIS FILE IS WITH YOU.
 *
 * You are hereby granted permission to use, copy, modify, and distribute
 * this file, provided that this notice, plus the above copyright notice
 * and disclaimer, appears in all copies. Radstone Technology will provide
 * no support for this code.
 *
 *  This driver uses the termios pseudo driver.
 */

#include <rtems.h>
#include <bsp.h>
#include <rtems/libio.h>
#include <stdlib.h>

#include "console.h"
#include "ns16550_p.h"

/*
 * Flow control is only supported when using interrupts
 */
console_flow ns16550_flow_RTSCTS =
{
	ns16550_negate_RTS,		/* deviceStopRemoteTx */
	ns16550_assert_RTS		/* deviceStartRemoteTx */
};

console_flow ns16550_flow_DTRCTS =
{
	ns16550_negate_DTR,		/* deviceStopRemoteTx */
	ns16550_assert_DTR		/* deviceStartRemoteTx */
};

console_fns ns16550_fns =
{
	ns16550_probe,			/* deviceProbe */
	ns16550_open,			/* deviceFirstOpen */
	ns16550_flush,			/* deviceLastClose */
	NULL,				/* deviceRead */
	ns16550_write_support_int,	/* deviceWrite */
	ns16550_initialize_interrupts,	/* deviceInitialize */
	ns16550_write_polled,		/* deviceWritePolled */
	FALSE,				/* deviceOutputUsesInterrupts */
};

console_fns ns16550_fns_polled =
{
	ns16550_probe,			/* deviceProbe */
	ns16550_open,			/* deviceFirstOpen */
	ns16550_close,			/* deviceLastClose */
	ns16550_inbyte_nonblocking_polled,	/* deviceRead */
	ns16550_write_support_polled,	/* deviceWrite */
	ns16550_init,			/* deviceInitialize */
	ns16550_write_polled,		/* deviceWritePolled */
	FALSE,				/* deviceOutputUsesInterrupts */
};

/*
 *  Console Device Driver Entry Points
 */
static boolean ns16550_probe(int minor)
{
	/*
	 * If the configuration dependant probe has located the device then
	 * assume it is there
	 */
	return(TRUE);
}

static void ns16550_init(int minor)
{
	PSP_READ_REGISTERS	pNS16550Read;
	PSP_WRITE_REGISTERS	pNS16550Write;
	unsigned8	 	ucTrash;
	unsigned8	 	ucDataByte;
	unsigned32		ulBaudDivisor;
	ns16550_context		*pns16550Context;

	pns16550Context=(ns16550_context *)malloc(sizeof(ns16550_context));

	Console_Port_Data[minor].pDeviceContext=(void *)pns16550Context;
	pns16550Context->ucModemCtrl=SP_MODEM_IRQ;

	pNS16550Read=(PSP_READ_REGISTERS)Console_Port_Tbl[minor].ulCtrlPort1;
	pNS16550Write=(PSP_WRITE_REGISTERS)pNS16550Read;

	/* Clear the divisor latch, clear all interrupt enables,
	 * and reset and
	 * disable the FIFO's.
	 */

	outport_byte(&pNS16550Write->LineControl, 0x0);
	outport_byte(&pNS16550Write->InterruptEnable, 0x0);

	/* Set the divisor latch and set the baud rate. */

	ulBaudDivisor=NS16550_Baud(
		(unsigned32)Console_Port_Tbl[minor].pDeviceParams);
	ucDataByte = SP_LINE_DLAB;
	outport_byte(&pNS16550Write->LineControl, ucDataByte);
	outport_byte(&pNS16550Write->TransmitBuffer, ulBaudDivisor&0xff);
	outport_byte(&pNS16550Write->InterruptEnable, (ulBaudDivisor>>8)&0xff);

	/* Clear the divisor latch and set the character size to eight bits */
	/* with one stop bit and no parity checking. */

	ucDataByte = EIGHT_BITS;
	outport_byte(&pNS16550Write->LineControl, ucDataByte);

	/* Enable and reset transmit and receive FIFOs. TJA	*/
	ucDataByte = SP_FIFO_ENABLE;
	outport_byte(&pNS16550Write->FifoControl, ucDataByte);

	ucDataByte = SP_FIFO_ENABLE | SP_FIFO_RXRST | SP_FIFO_TXRST;
	outport_byte(&pNS16550Write->FifoControl, ucDataByte);

	/*
	 * Disable interrupts
	 */
	ucDataByte = 0;
	outport_byte(&pNS16550Write->InterruptEnable, ucDataByte);

	/* Set data terminal ready. */
	/* And open interrupt tristate line */

	outport_byte(&pNS16550Write->ModemControl,
		     pns16550Context->ucModemCtrl);

	inport_byte(&pNS16550Read->LineStatus, ucTrash);
	inport_byte(&pNS16550Read->ReceiveBuffer, ucTrash);
}

static int ns16550_open(
	int	 major,
	int	 minor,
	void	* arg
)
{
	PSP_WRITE_REGISTERS	pNS16550Write;

	pNS16550Write=(PSP_WRITE_REGISTERS)Console_Port_Tbl[minor].ulCtrlPort1;

	/*
	 * Assert DTR
	 */
	if(Console_Port_Tbl[minor].pDeviceFlow
	   !=&ns16550_flow_DTRCTS)
	{
		ns16550_assert_DTR(minor);
	}

	return(RTEMS_SUCCESSFUL);
}

static int ns16550_close(
	int	 major,
	int	 minor,
	void	* arg
)
{
	PSP_WRITE_REGISTERS	pNS16550Write;

	pNS16550Write=(PSP_WRITE_REGISTERS)Console_Port_Tbl[minor].ulCtrlPort1;

	/*
	 * Negate DTR
	 */
	if(Console_Port_Tbl[minor].pDeviceFlow
	   !=&ns16550_flow_DTRCTS)
	{
		ns16550_negate_DTR(minor);
	}

	return(RTEMS_SUCCESSFUL);
}

/* 
 *  ns16550_write_polled
 */
static void ns16550_write_polled(
	int   minor, 
	char  cChar
)
{
	PSP_READ_REGISTERS	pNS16550Read;
	PSP_WRITE_REGISTERS	pNS16550Write;
	unsigned char	ucLineStatus;
	int iTimeout;

	pNS16550Read=(PSP_READ_REGISTERS)Console_Port_Tbl[minor].ulCtrlPort1;
	pNS16550Write=(PSP_WRITE_REGISTERS)pNS16550Read;

	/*
	 * wait for transmitter holding register to be empty
	 */
	iTimeout=1000;
	inport_byte(&pNS16550Read->LineStatus, ucLineStatus);
	while ((ucLineStatus & SP_LSR_THOLD) == 0)
	{
		/*
		 * Yield while we wait
		 */
		if(_System_state_Is_up(_System_state_Get()))
		{
			rtems_task_wake_after(RTEMS_YIELD_PROCESSOR);
		}
		inport_byte(&pNS16550Read->LineStatus, ucLineStatus);
		if(!--iTimeout)
		{
			break;
		}
	}

	/*
	 * transmit character
	 */
	outport_byte(&pNS16550Write->TransmitBuffer, cChar);
}

/*
 * These routines provide control of the RTS and DTR lines
 */
/*
 *  ns16550_assert_RTS
 */
static void ns16550_assert_RTS(int minor)
{
	PSP_WRITE_REGISTERS	pNS16550Write;
	unsigned32 Irql;
	ns16550_context	*pns16550Context;

	pns16550Context=(ns16550_context *)
		       Console_Port_Data[minor].pDeviceContext;

	pNS16550Write=(PSP_WRITE_REGISTERS)Console_Port_Tbl[minor].ulCtrlPort1;

	/*
	 * Assert RTS
	 */
	rtems_interrupt_disable(Irql);
	pns16550Context->ucModemCtrl|=SP_MODEM_RTS;
	outport_byte(&pNS16550Write->ModemControl,
		     pns16550Context->ucModemCtrl);
	rtems_interrupt_enable(Irql);
}

/*
 *  ns16550_negate_RTS
 */
static void ns16550_negate_RTS(int minor)
{
	PSP_WRITE_REGISTERS	pNS16550Write;
	unsigned32 Irql;
	ns16550_context	*pns16550Context;

	pns16550Context=(ns16550_context *)
		       Console_Port_Data[minor].pDeviceContext;

	pNS16550Write=(PSP_WRITE_REGISTERS)Console_Port_Tbl[minor].ulCtrlPort1;

	/*
	 * Negate RTS
	 */
	rtems_interrupt_disable(Irql);
	pns16550Context->ucModemCtrl&=~SP_MODEM_RTS;
	outport_byte(&pNS16550Write->ModemControl,
		     pns16550Context->ucModemCtrl);
	rtems_interrupt_enable(Irql);
}

/*
 * These flow control routines utilise a connection from the local DTR
 * line to the remote CTS line
 */
/*
 *  ns16550_assert_DTR
 */
static void ns16550_assert_DTR(int minor)
{
	PSP_WRITE_REGISTERS	pNS16550Write;
	unsigned32 Irql;
	ns16550_context	*pns16550Context;

	pns16550Context=(ns16550_context *)
		       Console_Port_Data[minor].pDeviceContext;

	pNS16550Write=(PSP_WRITE_REGISTERS)Console_Port_Tbl[minor].ulCtrlPort1;

	/*
	 * Assert DTR
	 */
	rtems_interrupt_disable(Irql);
	pns16550Context->ucModemCtrl|=SP_MODEM_DTR;
	outport_byte(&pNS16550Write->ModemControl,
		     pns16550Context->ucModemCtrl);
	rtems_interrupt_enable(Irql);
}

/*
 *  ns16550_negate_DTR
 */
static void ns16550_negate_DTR(int minor)
{
	PSP_WRITE_REGISTERS	pNS16550Write;
	unsigned32 Irql;
	ns16550_context	*pns16550Context;

	pns16550Context=(ns16550_context *)
		       Console_Port_Data[minor].pDeviceContext;

	pNS16550Write=(PSP_WRITE_REGISTERS)Console_Port_Tbl[minor].ulCtrlPort1;

	/*
	 * Negate DTR
	 */
	rtems_interrupt_disable(Irql);
	pns16550Context->ucModemCtrl&=~SP_MODEM_DTR;
	outport_byte(&pNS16550Write->ModemControl,
		     pns16550Context->ucModemCtrl);
	rtems_interrupt_enable(Irql);
}

/*
 *  ns16550_isr
 *
 *  This routine is the console interrupt handler for COM1 and COM2
 *
 *  Input parameters:
 *    vector - vector number
 *
 *  Output parameters: NONE
 *
 *  Return values:     NONE
 */

static void ns16550_process(
	int		minor
)
{
	PSP_READ_REGISTERS	pNS16550Read;
	PSP_WRITE_REGISTERS	pNS16550Write;
	volatile unsigned8 ucLineStatus, ucInterruptId;
	char	cChar;

	pNS16550Read=(PSP_READ_REGISTERS)Console_Port_Tbl[minor].ulCtrlPort1;
	pNS16550Write=(PSP_WRITE_REGISTERS)pNS16550Read;

	do
	{
		/*
		 * Deal with any received characters
		 */
		while(TRUE)
		{
			inport_byte(&pNS16550Read->LineStatus, ucLineStatus);
			if(~ucLineStatus & SP_LSR_RDY)
			{
				break;
			}
			inport_byte(&pNS16550Read->ReceiveBuffer, cChar);
			rtems_termios_enqueue_raw_characters(
				Console_Port_Data[minor].termios_data,
				&cChar, 1 );
		}

		while(TRUE)
		{
			if(Ring_buffer_Is_empty(
				&Console_Port_Data[minor].TxBuffer))
			{
				Console_Port_Data[minor].bActive=FALSE;
				if(Console_Port_Tbl[minor].pDeviceFlow
				   !=&ns16550_flow_RTSCTS)
				{
					ns16550_negate_RTS(minor);
				}
				/*
				 * There is no data to transmit
				 */
				break;
			}

			inport_byte(&pNS16550Read->LineStatus, ucLineStatus);
			if(~ucLineStatus & SP_LSR_THOLD)
			{
				/*
				 * We'll get another interrupt when
				 * the transmitter holding reg. becomes
				 * free again
				 */
				break;
			}

			Ring_buffer_Remove_character(
				&Console_Port_Data[minor].TxBuffer,
				cChar);
			/*
			 * transmit character
			 */
			outport_byte(&pNS16550Write->TransmitBuffer, cChar);
		}

		inport_byte(&pNS16550Read->InterruptId, ucInterruptId);
	}
	while((ucInterruptId&0xf)!=0x1);
}

static rtems_isr ns16550_isr(
  rtems_vector_number vector
)
{
	int	minor;

	for(minor=0;minor<Console_Port_Count;minor++)
	{
		if(vector==Console_Port_Tbl[minor].ulIntVector)
		{
			ns16550_process(minor);
		}
	}
}

/*
 *  ns16550_flush
 */
static int ns16550_flush(int major, int minor, void *arg)
{
	while(!Ring_buffer_Is_empty(&Console_Port_Data[minor].TxBuffer))
	{
		/*
		 * Yield while we wait
		 */
		if(_System_state_Is_up(_System_state_Get()))
		{
			rtems_task_wake_after(RTEMS_YIELD_PROCESSOR);
		}
	}

	ns16550_close(major, minor, arg);

	return(RTEMS_SUCCESSFUL);
}

/*
 *  ns16550_initialize_interrupts
 *
 *  This routine initializes the console's receive and transmit
 *  ring buffers and loads the appropriate vectors to handle the interrupts.
 *
 *  Input parameters:  NONE
 *
 *  Output parameters: NONE
 *
 *  Return values:     NONE
 */

static void ns16550_enable_interrupts(
	int minor
)
{
	PSP_WRITE_REGISTERS	pNS16550Write;
	unsigned8	ucDataByte;

	pNS16550Write=(PSP_WRITE_REGISTERS)Console_Port_Tbl[minor].ulCtrlPort1;

	/*
	 * Enable interrupts
	 */
	ucDataByte = SP_INT_RX_ENABLE | SP_INT_TX_ENABLE;
	outport_byte(&pNS16550Write->InterruptEnable, ucDataByte);

}

static void ns16550_initialize_interrupts(int minor)
{
	ns16550_init(minor);

	Ring_buffer_Initialize(&Console_Port_Data[minor].TxBuffer);

	Console_Port_Data[minor].bActive = FALSE;

	set_vector(ns16550_isr,
		   Console_Port_Tbl[minor].ulIntVector,
		   1);

	ns16550_enable_interrupts(minor);
}

/* 
 *  ns16550_write_support_int
 *
 *  Console Termios output entry point.
 *
 */
static int ns16550_write_support_int(
  int   minor, 
  const char *buf, 
  int   len)
{
	int i;
	unsigned32 Irql;

	for(i=0; i<len;)
	{
		if(Ring_buffer_Is_full(&Console_Port_Data[minor].TxBuffer))
		{
			if(!Console_Port_Data[minor].bActive)
			{
				/*
				 * Wake up the device
				 */
				rtems_interrupt_disable(Irql);
				Console_Port_Data[minor].bActive = TRUE;
				if(Console_Port_Tbl[minor].pDeviceFlow
				   !=&ns16550_flow_RTSCTS)
				{
					ns16550_assert_RTS(minor);
				}
				ns16550_process(minor);
				rtems_interrupt_enable(Irql);
			}
			else
			{
				/*
				 * Yield
				 */
				rtems_task_wake_after(RTEMS_YIELD_PROCESSOR);
			}

			/*
			 * Wait for ring buffer to empty
			 */
			continue;
		}
		else
		{
			Ring_buffer_Add_character(
				&Console_Port_Data[minor].TxBuffer,
				buf[i]);
			i++;
		}
	}

	/*
	 * Ensure that characters are on the way
	 */
	if(!Console_Port_Data[minor].bActive)
	{
		/*
		 * Wake up the device
		 */
		rtems_interrupt_disable(Irql);
		Console_Port_Data[minor].bActive = TRUE;
		if(Console_Port_Tbl[minor].pDeviceFlow
		   !=&ns16550_flow_RTSCTS)
		{
			ns16550_assert_RTS(minor);
		}
		ns16550_process(minor);
		rtems_interrupt_enable(Irql);
	}

	return (len);
}

/* 
 *  ns16550_write_support_polled
 *
 *  Console Termios output entry point.
 *
 */
static int ns16550_write_support_polled(
  int   minor, 
  const char *buf, 
  int   len)
{
	int nwrite = 0;

	/*
	 * poll each byte in the string out of the port.
	 */
	while (nwrite < len)
	{
		/*
		 * transmit character
		 */
		ns16550_write_polled(minor, *buf++);
		nwrite++;
	}

	/*
	 * return the number of bytes written.
	 */
	return nwrite;
}

/* 
 *  ns16550_inbyte_nonblocking_polled 
 *
 *  Console Termios polling input entry point.
 */

static int ns16550_inbyte_nonblocking_polled( 
	int minor 
)
{
	PSP_READ_REGISTERS	pNS16550Read;
	unsigned char	ucLineStatus;
	char	cChar;

	pNS16550Read=(PSP_READ_REGISTERS)Console_Port_Tbl[minor].ulCtrlPort1;

	inport_byte(&pNS16550Read->LineStatus, ucLineStatus);
	if(ucLineStatus & SP_LSR_RDY)
	{
		inport_byte(&pNS16550Read->ReceiveBuffer, cChar);
		return((int)cChar);
	}
	else
	{
	return(-1);
	}
}