source: rtems/c/src/libchip/serial/mc68681.c @ 424e23ee

4.104.114.84.95
Last change on this file since 424e23ee was 424e23ee, checked in by Joel Sherrill <joel.sherrill@…>, on Jun 22, 1998 at 10:17:14 AM

spacing changes

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