source: rtems/c/src/lib/libchip/serial/ns16550.c @ 0737710

4.104.114.84.95
Last change on this file since 0737710 was 0737710, checked in by Joel Sherrill <joel.sherrill@…>, on 06/13/98 at 15:48:25

Base code from ppcn_60x BSP

  • Property mode set to 100644
File size: 14.6 KB
Line 
1/*
2 *  This file contains the TTY driver for the NS16550
3 *
4 *  COPYRIGHT (c) 1998 by Radstone Technology
5 *
6 *
7 * THIS FILE IS PROVIDED TO YOU, THE USER, "AS IS", WITHOUT WARRANTY OF ANY
8 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
9 * IMPLIED WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK
10 * AS TO THE QUALITY AND PERFORMANCE OF ALL CODE IN THIS FILE IS WITH YOU.
11 *
12 * You are hereby granted permission to use, copy, modify, and distribute
13 * this file, provided that this notice, plus the above copyright notice
14 * and disclaimer, appears in all copies. Radstone Technology will provide
15 * no support for this code.
16 *
17 *  This driver uses the termios pseudo driver.
18 */
19
20#include <rtems.h>
21#include <rtems/libio.h>
22#include <stdlib.h>
23
24#include "console.h"
25#include "ns16550_p.h"
26
27/*
28 * Flow control is only supported when using interrupts
29 */
30console_flow ns16550_flow_RTSCTS =
31{
32  ns16550_negate_RTS,             /* deviceStopRemoteTx */
33  ns16550_assert_RTS              /* deviceStartRemoteTx */
34};
35
36console_flow ns16550_flow_DTRCTS =
37{
38  ns16550_negate_DTR,             /* deviceStopRemoteTx */
39  ns16550_assert_DTR              /* deviceStartRemoteTx */
40};
41
42console_fns ns16550_fns =
43{
44  ns16550_probe,                  /* deviceProbe */
45  ns16550_open,                   /* deviceFirstOpen */
46  ns16550_flush,                  /* deviceLastClose */
47  NULL,                           /* deviceRead */
48  ns16550_write_support_int,      /* deviceWrite */
49  ns16550_initialize_interrupts,  /* deviceInitialize */
50  ns16550_write_polled,           /* deviceWritePolled */
51  FALSE,                          /* deviceOutputUsesInterrupts */
52};
53
54console_fns ns16550_fns_polled =
55{
56  ns16550_probe,                       /* deviceProbe */
57  ns16550_open,                        /* deviceFirstOpen */
58  ns16550_close,                       /* deviceLastClose */
59  ns16550_inbyte_nonblocking_polled,   /* deviceRead */
60  ns16550_write_support_polled,        /* deviceWrite */
61  ns16550_init,                        /* deviceInitialize */
62  ns16550_write_polled,                /* deviceWritePolled */
63  FALSE,                               /* deviceOutputUsesInterrupts */
64};
65
66extern void set_vector( rtems_isr_entry, rtems_vector_number, int );
67
68/*
69 *  Types for get and set register routines
70 */
71
72typedef unsigned8 (*getRegister_f)(unsigned32 port, unsigned8 register);
73typedef void      (*setRegister_f)(
74                            unsigned32 port, unsigned8 reg, unsigned8 value);
75/*
76 *  Console Device Driver Entry Points
77 */
78static boolean ns16550_probe(int minor)
79{
80  /*
81   * If the configuration dependant probe has located the device then
82   * assume it is there
83   */
84  return(TRUE);
85}
86
87static void ns16550_init(int minor)
88{
89  unsigned32              pNS16550;
90  unsigned8               ucTrash;
91  unsigned8               ucDataByte;
92  unsigned32              ulBaudDivisor;
93  ns16550_context        *pns16550Context;
94  setRegister_f           setReg;
95  getRegister_f           getReg;
96
97  pns16550Context=(ns16550_context *)malloc(sizeof(ns16550_context));
98
99  Console_Port_Data[minor].pDeviceContext=(void *)pns16550Context;
100  pns16550Context->ucModemCtrl=SP_MODEM_IRQ;
101
102  pNS16550 = Console_Port_Tbl[minor].ulCtrlPort1;
103  setReg   = Console_Port_Tbl[minor].setRegister;
104  getReg   = Console_Port_Tbl[minor].getRegister;
105
106  /* Clear the divisor latch, clear all interrupt enables,
107   * and reset and
108   * disable the FIFO's.
109   */
110
111  (*setReg)(pNS16550, NS16550_LINE_CONTROL, 0x0);
112  (*setReg)(pNS16550, NS16550_INTERRUPT_ENABLE, 0x0);
113
114  /* Set the divisor latch and set the baud rate. */
115
116  ulBaudDivisor=NS16550_Baud((unsigned32)Console_Port_Tbl[minor].pDeviceParams);
117  ucDataByte = SP_LINE_DLAB;
118  (*setReg)(pNS16550, NS16550_LINE_CONTROL, ucDataByte);
119  (*setReg)(pNS16550, NS16550_TRANSMIT_BUFFER, ulBaudDivisor&0xff);
120  (*setReg)(pNS16550, NS16550_INTERRUPT_ENABLE, (ulBaudDivisor>>8)&0xff);
121
122  /* Clear the divisor latch and set the character size to eight bits */
123  /* with one stop bit and no parity checking. */
124  ucDataByte = EIGHT_BITS;
125  (*setReg)(pNS16550, NS16550_LINE_CONTROL, ucDataByte);
126
127  /* Enable and reset transmit and receive FIFOs. TJA     */
128  ucDataByte = SP_FIFO_ENABLE;
129  (*setReg)(pNS16550, NS16550_FIFO_CONTROL, ucDataByte);
130
131  ucDataByte = SP_FIFO_ENABLE | SP_FIFO_RXRST | SP_FIFO_TXRST;
132  (*setReg)(pNS16550, NS16550_FIFO_CONTROL, ucDataByte);
133
134  /*
135   * Disable interrupts
136   */
137  ucDataByte = 0;
138  (*setReg)(pNS16550, NS16550_INTERRUPT_ENABLE, ucDataByte);
139
140  /* Set data terminal ready. */
141  /* And open interrupt tristate line */
142  (*setReg)(pNS16550, NS16550_MODEM_CONTROL,pns16550Context->ucModemCtrl);
143
144  ucTrash = (*getReg)(pNS16550, NS16550_LINE_STATUS );
145  ucTrash = (*getReg)(pNS16550, NS16550_RECEIVE_BUFFER );
146}
147
148static int ns16550_open(
149  int      major,
150  int      minor,
151  void    * arg
152)
153{
154  /*
155   * Assert DTR
156   */
157
158  if(Console_Port_Tbl[minor].pDeviceFlow != &ns16550_flow_DTRCTS) {
159    ns16550_assert_DTR(minor);
160  }
161
162  return(RTEMS_SUCCESSFUL);
163}
164
165static int ns16550_close(
166  int      major,
167  int      minor,
168  void    * arg
169)
170{
171  /*
172   * Negate DTR
173   */
174  if(Console_Port_Tbl[minor].pDeviceFlow != &ns16550_flow_DTRCTS) {
175    ns16550_negate_DTR(minor);
176  }
177
178  return(RTEMS_SUCCESSFUL);
179}
180
181/*
182 *  ns16550_write_polled
183 */
184static void ns16550_write_polled(
185  int   minor,
186  char  cChar
187)
188{
189  unsigned32              pNS16550;
190  unsigned char           ucLineStatus;
191  int                     iTimeout;
192  getRegister_f           getReg;
193  setRegister_f           setReg;
194
195  pNS16550 = Console_Port_Tbl[minor].ulCtrlPort1;
196  getReg   = Console_Port_Tbl[minor].getRegister;
197  setReg   = Console_Port_Tbl[minor].setRegister;
198
199  /*
200   * wait for transmitter holding register to be empty
201   */
202  iTimeout=1000;
203  ucLineStatus = (*getReg)(pNS16550, NS16550_LINE_STATUS);
204  while ((ucLineStatus & SP_LSR_THOLD) == 0) {
205    /*
206     * Yield while we wait
207     */
208     if(_System_state_Is_up(_System_state_Get())) {
209       rtems_task_wake_after(RTEMS_YIELD_PROCESSOR);
210     }
211     ucLineStatus = (*getReg)(pNS16550, NS16550_LINE_STATUS);
212     if(!--iTimeout) {
213       break;
214     }
215  }
216
217  /*
218   * transmit character
219   */
220  (*setReg)(pNS16550, NS16550_TRANSMIT_BUFFER, cChar);
221}
222
223/*
224 * These routines provide control of the RTS and DTR lines
225 */
226/*
227 *  ns16550_assert_RTS
228 */
229static int ns16550_assert_RTS(int minor)
230{
231  unsigned32              pNS16550;
232  unsigned32              Irql;
233  ns16550_context        *pns16550Context;
234  setRegister_f           setReg;
235
236  pns16550Context=(ns16550_context *) Console_Port_Data[minor].pDeviceContext;
237
238  pNS16550 = Console_Port_Tbl[minor].ulCtrlPort1;
239  setReg   = Console_Port_Tbl[minor].setRegister;
240
241  /*
242   * Assert RTS
243   */
244  rtems_interrupt_disable(Irql);
245  pns16550Context->ucModemCtrl|=SP_MODEM_RTS;
246  (*setReg)(pNS16550, NS16550_MODEM_CONTROL, pns16550Context->ucModemCtrl);
247  rtems_interrupt_enable(Irql);
248  return 0;
249}
250
251/*
252 *  ns16550_negate_RTS
253 */
254static int ns16550_negate_RTS(int minor)
255{
256  unsigned32              pNS16550;
257  unsigned32              Irql;
258  ns16550_context        *pns16550Context;
259  setRegister_f           setReg;
260
261  pns16550Context=(ns16550_context *) Console_Port_Data[minor].pDeviceContext;
262
263  pNS16550 = Console_Port_Tbl[minor].ulCtrlPort1;
264  setReg   = Console_Port_Tbl[minor].setRegister;
265
266  /*
267   * Negate RTS
268   */
269  rtems_interrupt_disable(Irql);
270  pns16550Context->ucModemCtrl&=~SP_MODEM_RTS;
271  (*setReg)(pNS16550, NS16550_MODEM_CONTROL, pns16550Context->ucModemCtrl);
272  rtems_interrupt_enable(Irql);
273  return 0;
274}
275
276/*
277 * These flow control routines utilise a connection from the local DTR
278 * line to the remote CTS line
279 */
280/*
281 *  ns16550_assert_DTR
282 */
283static int ns16550_assert_DTR(int minor)
284{
285  unsigned32              pNS16550;
286  unsigned32              Irql;
287  ns16550_context        *pns16550Context;
288  setRegister_f           setReg;
289
290  pns16550Context=(ns16550_context *) Console_Port_Data[minor].pDeviceContext;
291
292  pNS16550 = Console_Port_Tbl[minor].ulCtrlPort1;
293  setReg   = Console_Port_Tbl[minor].setRegister;
294
295  /*
296   * Assert DTR
297   */
298  rtems_interrupt_disable(Irql);
299  pns16550Context->ucModemCtrl|=SP_MODEM_DTR;
300  (*setReg)(pNS16550, NS16550_MODEM_CONTROL, pns16550Context->ucModemCtrl);
301  rtems_interrupt_enable(Irql);
302  return 0;
303}
304
305/*
306 *  ns16550_negate_DTR
307 */
308static int ns16550_negate_DTR(int minor)
309{
310  unsigned32              pNS16550;
311  unsigned32              Irql;
312  ns16550_context        *pns16550Context;
313  setRegister_f           setReg;
314
315  pns16550Context=(ns16550_context *) Console_Port_Data[minor].pDeviceContext;
316
317  pNS16550 = Console_Port_Tbl[minor].ulCtrlPort1;
318  setReg   = Console_Port_Tbl[minor].setRegister;
319
320  /*
321   * Negate DTR
322   */
323  rtems_interrupt_disable(Irql);
324  pns16550Context->ucModemCtrl&=~SP_MODEM_DTR;
325  (*setReg)(pNS16550, NS16550_MODEM_CONTROL,pns16550Context->ucModemCtrl);
326  rtems_interrupt_enable(Irql);
327  return 0;
328}
329
330/*
331 *  ns16550_isr
332 *
333 *  This routine is the console interrupt handler for COM1 and COM2
334 *
335 *  Input parameters:
336 *    vector - vector number
337 *
338 *  Output parameters: NONE
339 *
340 *  Return values:     NONE
341 */
342
343static void ns16550_process(
344        int             minor
345)
346{
347  unsigned32              pNS16550;
348  volatile unsigned8      ucLineStatus;
349  volatile unsigned8      ucInterruptId;
350  char                    cChar;
351  getRegister_f           getReg;
352  setRegister_f           setReg;
353
354  pNS16550 = Console_Port_Tbl[minor].ulCtrlPort1;
355  getReg   = Console_Port_Tbl[minor].getRegister;
356  setReg   = Console_Port_Tbl[minor].setRegister;
357
358  do {
359    /*
360     * Deal with any received characters
361     */
362    while(TRUE) {
363      ucLineStatus = (*getReg)(pNS16550, NS16550_LINE_STATUS);
364      if(~ucLineStatus & SP_LSR_RDY) {
365        break;
366      }
367      cChar = (*getReg)(pNS16550, NS16550_RECEIVE_BUFFER);
368      rtems_termios_enqueue_raw_characters(
369        Console_Port_Data[minor].termios_data,
370        &cChar,
371        1
372      );
373    }
374
375    while(TRUE) {
376      if(Ring_buffer_Is_empty(&Console_Port_Data[minor].TxBuffer)) {
377        Console_Port_Data[minor].bActive=FALSE;
378        if(Console_Port_Tbl[minor].pDeviceFlow !=&ns16550_flow_RTSCTS) {
379          ns16550_negate_RTS(minor);
380        }
381
382        /*
383         * There is no data to transmit
384         */
385        break;
386      }
387
388      ucLineStatus = (*getReg)(pNS16550, NS16550_LINE_STATUS);
389      if(~ucLineStatus & SP_LSR_THOLD) {
390        /*
391         * We'll get another interrupt when
392         * the transmitter holding reg. becomes
393         * free again
394         */
395        break;
396      }
397
398      Ring_buffer_Remove_character( &Console_Port_Data[minor].TxBuffer, cChar);
399      /*
400       * transmit character
401       */
402      (*setReg)(pNS16550, NS16550_TRANSMIT_BUFFER, cChar);
403    }
404
405    ucInterruptId = (*getReg)(pNS16550, NS16550_INTERRUPT_ID);
406  }
407  while((ucInterruptId&0xf)!=0x1);
408}
409
410static rtems_isr ns16550_isr(
411  rtems_vector_number vector
412)
413{
414  int     minor;
415
416  for(minor=0;minor<Console_Port_Count;minor++) {
417    if(vector==Console_Port_Tbl[minor].ulIntVector) {
418      ns16550_process(minor);
419    }
420  }
421}
422
423/*
424 *  ns16550_flush
425 */
426static int ns16550_flush(int major, int minor, void *arg)
427{
428  while(!Ring_buffer_Is_empty(&Console_Port_Data[minor].TxBuffer)) {
429    /*
430     * Yield while we wait
431     */
432    if(_System_state_Is_up(_System_state_Get())) {
433      rtems_task_wake_after(RTEMS_YIELD_PROCESSOR);
434    }
435  }
436
437  ns16550_close(major, minor, arg);
438
439  return(RTEMS_SUCCESSFUL);
440}
441
442/*
443 *  ns16550_initialize_interrupts
444 *
445 *  This routine initializes the console's receive and transmit
446 *  ring buffers and loads the appropriate vectors to handle the interrupts.
447 *
448 *  Input parameters:  NONE
449 *
450 *  Output parameters: NONE
451 *
452 *  Return values:     NONE
453 */
454
455static void ns16550_enable_interrupts(
456  int minor
457)
458{
459  unsigned32            pNS16550;
460  unsigned8             ucDataByte;
461  setRegister_f           setReg;
462
463  pNS16550 = Console_Port_Tbl[minor].ulCtrlPort1;
464  setReg   = Console_Port_Tbl[minor].setRegister;
465
466  /*
467   * Enable interrupts
468   */
469  ucDataByte = SP_INT_RX_ENABLE | SP_INT_TX_ENABLE;
470  (*setReg)(pNS16550, NS16550_INTERRUPT_ENABLE, ucDataByte);
471
472}
473
474static void ns16550_initialize_interrupts(int minor)
475{
476  ns16550_init(minor);
477
478  Ring_buffer_Initialize(&Console_Port_Data[minor].TxBuffer);
479
480  Console_Port_Data[minor].bActive = FALSE;
481
482  set_vector(ns16550_isr, Console_Port_Tbl[minor].ulIntVector, 1);
483
484  ns16550_enable_interrupts(minor);
485}
486
487/*
488 *  ns16550_write_support_int
489 *
490 *  Console Termios output entry point.
491 *
492 */
493static int ns16550_write_support_int(
494  int   minor,
495  const char *buf,
496  int   len
497)
498{
499  int i;
500  unsigned32 Irql;
501
502  for(i=0; i<len;) {
503    if(Ring_buffer_Is_full(&Console_Port_Data[minor].TxBuffer)) {
504      if(!Console_Port_Data[minor].bActive) {
505        /*
506         * Wake up the device
507         */
508        rtems_interrupt_disable(Irql);
509        Console_Port_Data[minor].bActive = TRUE;
510        if(Console_Port_Tbl[minor].pDeviceFlow != &ns16550_flow_RTSCTS) {
511          ns16550_assert_RTS(minor);
512        }
513        ns16550_process(minor);
514        rtems_interrupt_enable(Irql);
515      } else {
516        /*
517         * Yield
518         */
519        rtems_task_wake_after(RTEMS_YIELD_PROCESSOR);
520      }
521
522      /*
523       * Wait for ring buffer to empty
524       */
525      continue;
526    }
527    else {
528      Ring_buffer_Add_character( &Console_Port_Data[minor].TxBuffer, buf[i]);
529      i++;
530    }
531  }
532
533  /*
534   * Ensure that characters are on the way
535   */
536  if(!Console_Port_Data[minor].bActive) {
537    /*
538     * Wake up the device
539     */
540    rtems_interrupt_disable(Irql);
541    Console_Port_Data[minor].bActive = TRUE;
542    if(Console_Port_Tbl[minor].pDeviceFlow !=&ns16550_flow_RTSCTS) {
543      ns16550_assert_RTS(minor);
544    }
545    ns16550_process(minor);
546    rtems_interrupt_enable(Irql);
547  }
548
549  return (len);
550}
551
552/*
553 *  ns16550_write_support_polled
554 *
555 *  Console Termios output entry point.
556 *
557 */
558static int ns16550_write_support_polled(
559  int         minor,
560  const char *buf,
561  int         len
562)
563{
564  int nwrite = 0;
565
566  /*
567   * poll each byte in the string out of the port.
568   */
569  while (nwrite < len) {
570    /*
571     * transmit character
572     */
573    ns16550_write_polled(minor, *buf++);
574    nwrite++;
575  }
576
577  /*
578   * return the number of bytes written.
579   */
580  return nwrite;
581}
582
583/*
584 *  ns16550_inbyte_nonblocking_polled
585 *
586 *  Console Termios polling input entry point.
587 */
588
589static int ns16550_inbyte_nonblocking_polled(
590  int minor
591)
592{
593  unsigned32           pNS16550;
594  unsigned char        ucLineStatus;
595  char                 cChar;
596  getRegister_f        getReg;
597
598  pNS16550 = Console_Port_Tbl[minor].ulCtrlPort1;
599  getReg   = Console_Port_Tbl[minor].getRegister;
600
601  ucLineStatus = (*getReg)(pNS16550, NS16550_LINE_STATUS);
602  if(ucLineStatus & SP_LSR_RDY) {
603    cChar = (*getReg)(pNS16550, NS16550_RECEIVE_BUFFER);
604    return((int)cChar);
605  } else {
606    return(-1);
607  }
608}
Note: See TracBrowser for help on using the repository browser.