source: rtems/c/src/lib/libbsp/m68k/mvme167/console/console.c @ 270e3cc

4.104.114.84.95
Last change on this file since 270e3cc was df49c60, checked in by Joel Sherrill <joel.sherrill@…>, on 06/12/00 at 15:00:15

Merged from 4.5.0-beta3a

  • Property mode set to 100644
File size: 49.8 KB
Line 
1/*
2 *  console.c
3 *
4 *  This file contains the MVME167 termios console package. Only asynchronous
5 *  I/O is supported.
6 *
7 *  /dev/tty0 is channel 0, Serial Port 1/Console on the MVME712M.
8 *  /dev/tty1 is channel 1, Serial Port 2/TTY01 on the MVME712M.
9 *  /dev/tty2 is channel 2, Serial Port 3 on the MVME712M.
10 *  /dev/tty3 is channel 3, Serial Port 4 on the MVME712M.
11 *
12 *  Normal I/O uses DMA for output, interrupts for input. /dev/console is
13 *  fixed to be /dev/tty01, Serial Port 2. Very limited support is provided
14 *  for polled I/O. Polled I/O is intended only for running the RTEMS test
15 *  suites. In all cases, Serial Port 1/Console is allocated to 167Bug and
16 *  is the dedicated debugger port. We configure GDB to use 167Bug for
17 *  debugging. When debugging with GDB or 167Bug, do not open /dev/tty00.
18 *
19 *  Modern I/O chips often contain a number of I/O devices that can operate
20 *  almost independently of each other. Typically, in RTEMS, all devices in
21 *  an I/O chip are handled by a single device driver, but that need not be
22 *  always the case. Each device driver must supply six entry points in the
23 *  Device Driver Table: a device initialization function, as well as an open,
24 *  close, read, write and a control function. RTEMS assigns a device major
25 *  number to each device driver. This major device number is the index of the
26 *  device driver entries in the Device Driver Table, and it used to identify
27 *  a particular device driver. To distinguish multiple I/O sub-devices within
28 *  an I/O chip, RTEMS supports device minor numbers. When a I/O device is
29 *  initialized, the major number is supplied to the initialization function.
30 *  That function must register each sub-device with a separate name and minor
31 *  number (as well as the supplied major number). When an application opens a
32 *  device by name, the corresponding major and minor numbers are returned to
33 *  the caller to be used in subsequent I/O operations (although these details
34 *  are typically hidden within the library functions).
35 *
36 *  Such a scheme recognizes that the initialization of the individual
37 *  sub-devices is generally not completely independent. For example, the
38 *  four serial ports of the CD2401 can be configured almost independently
39 *  from each other. One port could be configured to operate in asynchronous
40 *  mode with interrupt-driven I/O, while another port could be configured to
41 *  operate in HDLC mode with DMA I/O. However, a device reset command will
42 *  reset all four channels, and the width of DMA transfers and the number of
43 *  retries following bus errors selected applies to all four channels.
44 *  Consequently, when initializing one channel, one must be careful not to
45 *  destroy the configuration of other channels that are already configured.
46 *
47 *  One problem with the RTEMS I/O initialization model is that no information
48 *  other than a device major number is passed to the initialization function.
49 *  Consequently, the sub-devices must be initialized with some pre-determined
50 *  configuration. To change the configuration of a sub-device, it is
51 *  necessary to either rewrite the initialization function, or to make a
52 *  series of rtems_io_control() calls after initialization. The first
53 *  approach is not very elegant. The second approach is acceptable if an
54 *  application is simply changing baud rates, parity or other such
55 *  asynchronous parameters (as supplied by the termios package). But what if
56 *  an application requires one channel to run in HDLC or Bisync mode and
57 *  another in async mode? With a single driver per I/O chip approach, the
58 *  device driver must support multiple protocols. This is feasible, but it
59 *  often means that an application that only does asynchronous I/O now links
60 *  in code for other unused protocols, thus wasting precious ROM space.
61 *  Worse, it requires that the sub-devices be initialized in some
62 *  configuration, and that configuration then changed through a series of
63 *  device driver control calls. There is no standard API in RTEMS to switch
64 *  a serial line to some synchronous protocol.
65 *
66 *  A better approach is to treat each channel as a separate device, each with
67 *  its own device device driver. The application then supplies its own device
68 *  driver table with only the required protocols (drivers) on each line. The
69 *  problem with this approach is that the device drivers are not really
70 *  independent, given that the I/O sub-devices within a common chip are not
71 *  independent themselves. Consequently, the related device drivers must
72 *  share some information. In RTEMS, there is no standard location in which
73 *  to share information.
74 *
75 *  This driver handles all four channels, i.e. it distinguishes the
76 *  sub-devices using minor device numbers. Only asynchronous I/O is
77 *  supported. The console is currently fixed to be channel 1 on the CD2401,
78 *  which corresponds to the TTY01 port (Serial Port 2) on the MVME712M
79 *  Transition Module.
80 *
81 *  The CD2401 does either interrupt-driven or DMA I/O; it does not support
82 *  polling. In interrupt-driven or DMA I/O modes, interrupts from the CD2401
83 *  are routed to the MC68040, and the processor generates an interrupt
84 *  acknowledge cycle directly to the CD2401 to obtain an interrupt vector.
85 *  The PCCchip2 supports a pseudo-polling mode in which interrupts from the
86 *  CD2401 are not routed to the MC68040, but can be detected by the processor
87 *  by reading the appropriate CD2401 registers. In this mode, interrupt
88 *  acknowledge cycles must be generated to the CD2401 by reading the
89 *  appropriate PCCchip2 registers.
90 *
91 *  Interrupts from the four channels cannot be routed independently; either
92 *  all channels are used in the pseudo-polling mode, or all channels are used
93 *  in interrupt-driven/DMA mode. There is no advantage in using the speudo-
94 *  polling mode. Consenquently, this driver performs DMA input and output.
95 *  Output is performed directly from the termios raw output buffer, while
96 *  input is accumulated into a separate buffer.
97 *
98 *  THIS MODULE IS NOT RE-ENTRANT! Simultaneous access to a device from
99 *  multiple tasks is likely to cause significant problems! Concurrency
100 *  control is implemented in the termios package.
101 *
102 *  THE INTERRUPT LEVEL IS SET TO 1 FOR ALL CHANNELS.
103 *  If the CD2401 is to be used for high speed synchronous serial I/O, the
104 *  interrupt priority might need to be increased.
105 *
106 *  ALL INTERRUPT HANDLERS ARE SHARED.
107 *  When adding extra device drivers, either rewrite the interrupt handlers
108 *  to demultiplex the interrupts, or install separate vectors. Common vectors
109 *  are currently used to catch spurious interrupts. We could already have
110 *  installed separate vectors for each channel and used the spurious
111 *  interrupt handler defined in some other BSPs, but handling spurious
112 *  interrupts from the CD2401 in this device driver allows us to record more
113 *  information on the source of the interrupts. Furthermore, we have observed
114 *  the occasional spurious interrupt from channel 0. We definitely do not
115 *  to call a debugger for those.
116 *
117 *  All page references are to the MVME166/MVME167/MVME187 Single Board
118 *  Computer Programmer's Reference Guide (MVME187PG/D2) with the April
119 *  1993 supplements/addenda (MVME187PG/D2A1).
120 *
121 *  Copyright (c) 1998, National Research Council of Canada
122 *
123 *  The license and distribution terms for this file may be
124 *  found in the file LICENSE in this distribution or at
125 *  http://www.OARcorp.com/rtems/license.html.
126 */
127
128#define M167_INIT
129
130#include <stdarg.h>
131#include <stdio.h>
132#include <termios.h>
133#include <bsp.h>                /* Must be before libio.h */
134#include <rtems/libio.h>
135
136
137/* Channel info */
138/* static */ volatile struct {
139  void *tty;                    /* Really a struct rtems_termios_tty * */
140  int len;                      /* Record nb of chars being TX'ed */
141  const char *buf;              /* Record where DMA is coming from */
142  rtems_unsigned32 spur_cnt;    /* Nb of spurious ints so far */
143  rtems_unsigned32 spur_dev;    /* Indo on last spurious int */
144  rtems_unsigned32 buserr_addr; /* Faulting address */
145  rtems_unsigned32 buserr_type; /* Reason of bus error during DMA */
146  rtems_unsigned8  own_buf_A;   /* If true, buffer A belongs to the driver */
147  rtems_unsigned8  own_buf_B;   /* If true, buffer B belongs to the driver */
148  rtems_unsigned8  txEmpty;     /* If true, the output FIFO is supposed to be empty */
149} CD2401_Channel_Info[4];
150
151/*
152 *  The number of channels already opened. If zero, enable the interrupts. The
153 *  initial value must be 0. If initialized explicitly, the variable ends up
154 *  in the .data section. Its value is not re-initialized on system restart.
155 *  Furthermore, because the variable is changed, the .data section would not
156 *  be ROMable. We thus leave the variable uninitialized, which causes it to
157 *  be allocated in the .bss section, and rely on RTEMS to zero the .bss
158 *  section on every startup.
159 */
160rtems_unsigned8 Init_count;
161
162
163/* Record previous handlers */
164rtems_isr_entry Prev_re_isr;        /* Previous rx exception isr */
165rtems_isr_entry Prev_rx_isr;        /* Previous rx isr */
166rtems_isr_entry Prev_tx_isr;        /* Previous tx isr */
167rtems_isr_entry Prev_modem_isr;     /* Previous modem/timer isr */
168
169
170/* Define the following symbol to trace the calls to this driver */
171/* #define CD2401_RECORD_DEBUG_INFO */
172#include "console-recording.c"
173
174
175/* Utility functions */
176void cd2401_udelay( unsigned long delay );
177void cd2401_chan_cmd( rtems_unsigned8 channel, rtems_unsigned8 cmd, rtems_unsigned8 wait );
178rtems_unsigned16 cd2401_bitrate_divisor( rtems_unsigned32 clkrate, rtems_unsigned32* bitrate );
179void cd2401_initialize( void );
180void cd2401_interrupts_initialize( rtems_boolean enable );
181
182/* ISRs */
183rtems_isr cd2401_modem_isr( rtems_vector_number vector );
184rtems_isr cd2401_re_isr( rtems_vector_number vector );
185rtems_isr cd2401_rx_isr( rtems_vector_number vector );
186rtems_isr cd2401_tx_isr( rtems_vector_number vector );
187
188/* Termios callbacks */
189int cd2401_firstOpen( int major, int minor, void *arg );
190int cd2401_lastClose( int major, int minor, void *arg );
191int cd2401_setAttributes( int minor, const struct termios *t );
192int cd2401_startRemoteTx( int minor );
193int cd2401_stopRemoteTx( int minor );
194int cd2401_write( int minor, const char *buf, int len );
195int cd2401_drainOutput( int minor );
196int _167Bug_pollRead( int minor );
197int _167Bug_pollWrite( int minor, const char *buf, int len );
198
199
200/*
201 *  Utility functions.
202 */
203
204/*
205 *  Assumes that clock ticks 1 million times per second.
206 *
207 *  MAXIMUM DELAY IS ABOUT 20 ms
208 *
209 *  Input parameters:
210 *    delay: Number of microseconds to delay.
211 *
212 *  Output parameters: NONE
213 *
214 *  Return values: NONE
215 */
216 void cd2401_udelay
217(
218  unsigned long delay
219)
220{
221  unsigned long i = 20000;  /* In case clock is off */
222  rtems_interval ticks_per_second, start_ticks, end_ticks, current_ticks;
223   
224  rtems_clock_get( RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticks_per_second );
225  rtems_clock_get( RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &start_ticks );
226  end_ticks = start_ticks + delay;
227 
228  do {
229    rtems_clock_get(RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &current_ticks);
230  } while ( --i && (current_ticks <= end_ticks) );
231 
232  CD2401_RECORD_DELAY_INFO(( start_ticks, end_ticks, current_ticks, i ));
233}
234
235
236/*
237 *  cd2401_chan_cmd
238 *
239 *  Sends a CCR command to the specified channel. Waits for any unfinished
240 *  previous command to complete, then sends the specified command. Optionally
241 *  wait for the current command to finish before returning.
242 *
243 *  Input parameters:
244 *    channel - CD2401 channel number
245 *    cmd  - command byte
246 *    wait - if non-zero, wait for specified command to complete before
247 *          returning.
248 *
249 *  Output parameters: NONE
250 *
251 *  Return values: NONE
252 */
253void cd2401_chan_cmd(
254  rtems_unsigned8 channel,
255  rtems_unsigned8 cmd,
256  rtems_unsigned8 wait
257)
258{
259  if ( channel < 4 ) {
260    cd2401->car = channel;      /* Select channel */
261
262    while ( cd2401->ccr != 0 ); /* Wait for completion of any previous command */
263    cd2401->ccr = cmd;          /* Send command */
264    if ( wait )
265      while( cd2401->ccr != 0 );/* Wait for completion */
266  }
267  else {
268    /* This may not be the best error message */
269    rtems_fatal_error_occurred( RTEMS_INVALID_NUMBER );
270  }
271}
272
273
274/*
275 *  cd2401_bitrate_divisor
276 *
277 *  Compute the divisor and clock source to use to obtain the desired bitrate.
278 *
279 *  Input parameters:
280 *    clkrate - system clock rate (CLK input frequency)
281 *    bitrate - the desired bitrate
282 *
283 *  Output parameters:
284 *    bitrate - The actual bitrate achievable, to the nearest bps.
285 *
286 *  Return values:
287 *    Returns divisor in lower byte and clock source in upper byte for the
288 *    specified bitrate.
289 */
290rtems_unsigned16 cd2401_bitrate_divisor(
291  rtems_unsigned32 clkrate,
292  rtems_unsigned32* bitrate
293)
294{
295  rtems_unsigned32 divisor;
296  rtems_unsigned16 clksource;
297
298  divisor = *bitrate << 3;          /* temporary; multiply by 8 for CLK/8 */
299  divisor = (clkrate + (divisor>>1)) / divisor; /* divisor for clk0 (CLK/8) */
300
301  /*  Use highest speed clock source for best precision - try from clk0 to clk4:  */
302  for( clksource = 0; clksource < 0x0400 && divisor > 0x100; clksource += 0x0100 )
303      divisor >>= 2;
304  divisor--;                        /* adjustment, see specs */
305  if( divisor < 1 )
306    divisor = 1;
307  else if( divisor > 0xFF )
308    divisor = 0xFF;
309  *bitrate = clkrate / (1 << ((clksource >> 7)+3)) / (divisor+1);
310  return( clksource | divisor );
311}
312
313
314/*
315 *  cd2401_initialize
316 *
317 *  Initializes the CD2401 device. Individual channels on the chip are left in
318 *  their default reset state, and should be subsequently configured.
319 *
320 *  Input parameters: NONE
321 *
322 *  Output parameters:  NONE
323 *
324 *  Return values: NONE
325 */
326void cd2401_initialize( void )
327{
328  int i;
329
330  for ( i = 3; i >= 0; i-- ) {
331    CD2401_Channel_Info[i].tty = NULL;
332    CD2401_Channel_Info[i].len = 0;
333    CD2401_Channel_Info[i].buf = NULL;
334    CD2401_Channel_Info[i].spur_cnt = 0;
335    CD2401_Channel_Info[i].spur_dev = 0;
336    CD2401_Channel_Info[i].buserr_type = 0;
337    CD2401_Channel_Info[i].buserr_addr = 0;
338    CD2401_Channel_Info[i].own_buf_A = TRUE;
339    CD2401_Channel_Info[i].own_buf_B = TRUE;
340    CD2401_Channel_Info[i].txEmpty = TRUE;
341  }
342
343 /*
344  *  Normally, do a device reset here. If we do it, we will most likely clober
345  *  the port settings for 167Bug on channel 0. So we just shut up all the
346  *  ports by disabling their interrupts.
347  */
348#if 0
349  cd2401->gfrcr = 0;            /* So we can detect that device init is done */
350  cd2401_chan_cmd( 0x10, 0);    /* Reset all */
351  while(cd2401->gfrcr == 0);    /* Wait for reset all */
352#endif
353
354  /*
355   *  The CL-CD2400/2401 manual (part no 542400-003) states on page 87 that
356   *  the LICR "contains the number of the interrupting channel being served.
357   *  The channel number is always that of the current acknowledged interrupt."
358   *  THE USER MUST PROGRAM CHANNEL NUMBER IN LICR! It is not set automatically
359   *  by the hardware, as suggested by the manual.
360   *
361   *  The updated manual (part no 542400-007) has the story strait. The CD2401
362   *  automatically initializes the LICR to contain the channel number in bits
363   *  2 and 3. However, these bits are not preserved when the user defined bits
364   *  are written.
365   *
366   *  The same vector number is used for all four channels. Different vector
367   *  numbers could be programmed for each channel, thus avoiding the need to
368   *  demultiplex the interrupts in the ISR.
369   */
370  for ( i = 0; i < 4; i++ ) {
371    cd2401->car = i;            /* Select channel */
372    cd2401->livr = 0x5C;        /* Motorola suggested value p. 3-15 */
373    cd2401->licr = i << 2;      /* Don't rely on reset value */
374    cd2401->ier = 0;            /* Disable all interrupts */
375  }
376
377  /*
378   *  The content of the CD2401 xpilr registers must match the A7-A0 addresses
379   *  generated by the PCCchip2 during interrupt acknowledge cycles in order
380   *  for the CD2401 to recognize the IACK cycle and clear its interrupt
381   *  request.
382   */
383  cd2401->mpilr = 0x01;         /* Match pccchip2->modem_piack p. 3-27 */
384  cd2401->tpilr = 0x02;         /* Match pccchip2->tx_piack p. 3-28 */
385  cd2401->rpilr = 0x03;         /* Match pccchip2->rx_piack p. 3-29 */
386
387  /* Global CD2401 registers */
388  cd2401->dmr = 0;              /* 16-bit DMA transfers when possible */
389  cd2401->bercnt = 0;           /* Do not retry DMA upon bus errors */
390
391  /*
392   *  Setup timer prescaler period, which clocks timers 1 and 2 (or rx timeout
393   *  and tx delay). The prescaler is clocked by the system clock) / 2048. The
394   *  register must be in the range 0x0A..0xFF, ie. a rescaler period range of
395   *  about 1ms..26ms for a nominal system clock rate  of 20MHz.
396   */
397  cd2401->tpr  = 0x0A;          /* Same value as 167Bug */
398}
399
400
401/*
402 *  cd2401_interrupts_initialize
403 *
404 *  This routine enables or disables the CD2401 interrupts to the MC68040.
405 *  Interrupts cannot be enabled/disabled on a per-channel basis.
406 *
407 *  Input parameters:
408 *    enable - if true, enable the interrupts, else disable them.
409 *
410 *  Output parameters:  NONE
411 *
412 *  Return values: NONE
413 *
414 *  THE FIRST CD2401 CHANNEL OPENED SHOULD ENABLE INTERRUPTS.
415 *  THE LAST CD2401 CHANNEL CLOSED SHOULD DISABLE INTERRUPTS.
416 */
417void cd2401_interrupts_initialize(
418  rtems_boolean enable
419)
420{
421  if ( enable ) {
422   /*
423    *  Enable interrupts from the CD2401 in the PCCchip2.
424    *  During DMA transfers, the MC68040 supplies dirty data during read cycles
425    *  from the CD2401 and leaves the data dirty in its data cache if there is
426    *  a cache hit. The MC68040 updates the data cache during write cycles from
427    *  the CD2401 if there is a cache hit.
428    */
429    pccchip2->SCC_error = 0x01;
430    pccchip2->SCC_modem_int_ctl = 0x10 | CD2401_INT_LEVEL;
431    pccchip2->SCC_tx_int_ctl = 0x10 | CD2401_INT_LEVEL;
432    pccchip2->SCC_rx_int_ctl = 0x50 | CD2401_INT_LEVEL;
433
434    pccchip2->gen_control |= 0x02;      /* Enable pccchip2 interrupts */
435  }
436  else {
437    /* Disable interrupts */
438    pccchip2->SCC_modem_int_ctl &= 0xEF;
439    pccchip2->SCC_tx_int_ctl &= 0xEF;
440    pccchip2->SCC_rx_int_ctl &= 0xEF;
441  }
442}
443
444
445/* ISRs */
446
447/*
448 *  cd2401_modem_isr
449 *
450 *  Modem/timer interrupt (group 1) from CD2401. These are not used, and not
451 *  expected. Record as spurious and clear.
452 *
453 *  Input parameters:
454 *    vector - vector number
455 *
456 *  Output parameters: NONE
457 *
458 *  Return values: NONE
459 */
460rtems_isr cd2401_modem_isr(
461  rtems_vector_number vector
462)
463{
464  rtems_unsigned8 ch;
465
466  /* Get interrupting channel ID */
467  ch = cd2401->licr >> 2;
468
469  /* Record interrupt info for debugging */
470  CD2401_Channel_Info[ch].spur_dev =
471      (vector << 24) | (cd2401->stk << 16) | (cd2401->mir << 8) | cd2401->misr;
472  CD2401_Channel_Info[ch].spur_cnt++;
473
474  cd2401->meoir = 0;            /* EOI */
475  CD2401_RECORD_MODEM_ISR_SPURIOUS_INFO(( ch,
476                                          CD2401_Channel_Info[ch].spur_dev,
477                                          CD2401_Channel_Info[ch].spur_cnt ));
478}
479
480
481/*
482 *  cd2401_re_isr
483 *
484 *  RX exception interrupt (group 3, receiver exception) from CD2401. These are
485 *  not used, and not expected. Record as spurious and clear.
486 *
487 *  FIX THIS ISR TO DETECT BREAK CONDITIONS AND RAISE SIGINT
488 *
489 *  Input parameters:
490 *    vector - vector number
491 *
492 *  Output parameters: NONE
493 *
494 *  Return values: NONE
495 */
496rtems_isr cd2401_re_isr(
497  rtems_vector_number vector
498)
499{
500  rtems_unsigned8 ch;
501
502  /* Get interrupting channel ID */
503  ch = cd2401->licr >> 2;
504
505  /* Record interrupt info for debugging */
506  CD2401_Channel_Info[ch].spur_dev =
507      (vector << 24) | (cd2401->stk << 16) | (cd2401->rir << 8) | cd2401->u5.b.risrl;
508  CD2401_Channel_Info[ch].spur_cnt++;
509
510  if ( cd2401->u5.b.risrl & 0x80 )  /* Timeout interrupt? */
511    cd2401->ier &= 0xDF;            /* Disable rx timeout interrupt */
512  cd2401->reoir = 0x08;             /* EOI; exception char not read */
513  CD2401_RECORD_RE_ISR_SPURIOUS_INFO(( ch,
514                                       CD2401_Channel_Info[ch].spur_dev,
515                                       CD2401_Channel_Info[ch].spur_cnt ));
516}
517
518
519/*
520 *  cd2401_rx_isr
521 *
522 *  RX interrupt (group 3, receiver data) from CD2401.
523 *
524 *  Input parameters:
525 *     vector - vector number
526 *
527 *  Output parameters: NONE
528 *
529 *  Return values: NONE
530 */
531rtems_isr cd2401_rx_isr(
532  rtems_vector_number vector
533)
534{
535  char c;
536  rtems_unsigned8 ch, status, nchars, i, total;
537  char buffer[256];
538
539  status = cd2401->u5.b.risrl;
540  ch = cd2401->licr >> 2;
541
542  /* Has this channel been initialized or is it a condition we ignore? */
543  if ( CD2401_Channel_Info[ch].tty && !status ) {
544    /* Normal Rx Int, read chars, enqueue them, and issue EOI */
545    total = nchars = cd2401->rfoc;  /* Nb of chars to retrieve from rx FIFO */
546    i = 0;
547    while ( nchars-- > 0 ) {
548      c = (char)cd2401->dr;         /* Next char in rx FIFO */
549      rtems_termios_enqueue_raw_characters( CD2401_Channel_Info[ch].tty ,&c, 1 );
550      buffer[i++] = c;
551    }
552    cd2401->reoir = 0;              /* EOI */
553    CD2401_RECORD_RX_ISR_INFO(( ch, total, buffer ));
554  } else {
555    /* No, record as spurious interrupt */
556    CD2401_Channel_Info[ch].spur_dev =
557        (vector << 24) | (cd2401->stk << 16) | (cd2401->rir << 8) | cd2401->u5.b.risrl;
558    CD2401_Channel_Info[ch].spur_cnt++;
559    cd2401->reoir = 0x04;           /* EOI - character not read */
560    CD2401_RECORD_RX_ISR_SPURIOUS_INFO(( ch, status,
561                                         CD2401_Channel_Info[ch].spur_dev,
562                                         CD2401_Channel_Info[ch].spur_cnt ));
563  }
564}
565
566
567/*
568 *  cd2401_tx_isr
569 *
570 *  TX interrupt (group 2) from CD2401.
571 *
572 *  Input parameters:
573 *    vector - vector number
574 *
575 *  Output parameters: NONE
576 *
577 *  Return values: NONE
578 */
579rtems_isr cd2401_tx_isr(
580  rtems_vector_number vector
581)
582{
583  rtems_unsigned8 ch, status, buserr, initial_ier, final_ier;
584
585  status = cd2401->tisr;
586  ch = cd2401->licr >> 2;
587  initial_ier = cd2401->ier;
588
589  /* Has this channel been initialized? */
590  if ( !CD2401_Channel_Info[ch].tty ) {
591    /* No, record as spurious interrupt */
592    CD2401_Channel_Info[ch].spur_dev =
593        (vector << 24) | (cd2401->stk << 16) | (cd2401->tir << 8) | cd2401->tisr;
594    CD2401_Channel_Info[ch].spur_cnt++;
595    final_ier = cd2401->ier &= 0xFC;/* Shut up, whoever you are */
596    cd2401->teoir = 0x88;           /* EOI - Terminate buffer and no transfer */
597    CD2401_RECORD_TX_ISR_SPURIOUS_INFO(( ch, status, initial_ier, final_ier,
598                                         CD2401_Channel_Info[ch].spur_dev,
599                                         CD2401_Channel_Info[ch].spur_cnt ));
600    return;
601  }
602
603  if ( status & 0x80 ) {
604    /*
605     *  Bus error occurred during DMA transfer. For now, just record.
606     *  Get reason for DMA bus error and clear the report for the next occurrence
607     */
608    buserr = pccchip2->SCC_error;
609    pccchip2->SCC_error = 0x01;
610    CD2401_Channel_Info[ch].buserr_type =
611         (vector << 24) | (buserr << 16) | (cd2401->tir << 8) | cd2401->tisr;
612    CD2401_Channel_Info[ch].buserr_addr =
613        (((rtems_unsigned32)cd2401->tcbadru) << 16) | cd2401->tcbadrl;
614
615    cd2401->teoir = 0x80;           /* EOI - terminate bad buffer */
616    CD2401_RECORD_TX_ISR_BUSERR_INFO(( ch, status, initial_ier, buserr,
617                                       CD2401_Channel_Info[ch].buserr_type,
618                                       CD2401_Channel_Info[ch].buserr_addr ));
619    return;
620  }
621
622  if ( status & 0x20 ) {
623    /* DMA done -- Turn off TxD int, turn on TxMpty */
624    final_ier = cd2401->ier = (cd2401->ier & 0xFE) | 0x02;
625    if( status & 0x08 ) {
626      /* Transmit buffer B was released */
627      CD2401_Channel_Info[ch].own_buf_B = TRUE;
628    }
629    else {
630      /* Transmit buffer A was released */
631      CD2401_Channel_Info[ch].own_buf_A = TRUE;
632    }
633    CD2401_RECORD_TX_ISR_INFO(( ch, status, initial_ier, final_ier,
634                                CD2401_Channel_Info[ch].txEmpty ));
635
636    /* This call can result in a call to cd2401_write() */
637    rtems_termios_dequeue_characters (
638        CD2401_Channel_Info[ch].tty,
639        CD2401_Channel_Info[ch].len );
640    cd2401->teoir = 0x08;           /* EOI - no data transfered */
641  }
642  else if ( status & 0x02 ) {
643    /* TxEmpty */
644    CD2401_Channel_Info[ch].txEmpty = TRUE;
645    final_ier = cd2401->ier &= 0xFD;/* Shut up the interrupts */
646    cd2401->teoir = 0x08;           /* EOI - no data transfered */
647    CD2401_RECORD_TX_ISR_INFO(( ch, status, initial_ier, final_ier,
648                                CD2401_Channel_Info[ch].txEmpty ));
649  }
650  else {
651    /* Why did we get a Tx interrupt? */
652    CD2401_Channel_Info[ch].spur_dev =
653        (vector << 24) | (cd2401->stk << 16) | (cd2401->tir << 8) | cd2401->tisr;
654    CD2401_Channel_Info[ch].spur_cnt++;
655    cd2401->teoir = 0x08;           /* EOI - no data transfered */
656    CD2401_RECORD_TX_ISR_SPURIOUS_INFO(( ch, status, initial_ier, 0xFF,
657                                         CD2401_Channel_Info[ch].spur_dev,
658                                         CD2401_Channel_Info[ch].spur_cnt ));
659  }
660}
661
662
663/*
664 *  termios callbacks
665 */
666
667/*
668 *  cd2401_firstOpen
669 *
670 *  This is the first time that this minor device (channel) is opened.
671 *  Complete the asynchronous initialization.
672 *
673 *  Input parameters:
674 *    major - device major number
675 *    minor - channel number
676 *    arg - pointer to a struct rtems_libio_open_close_args_t
677 *
678 *  Output parameters: NONE
679 *
680 *  Return value: IGNORED
681 */
682int cd2401_firstOpen(
683  int major,
684  int minor,
685  void *arg
686)
687{
688  rtems_libio_open_close_args_t *args = arg;
689  rtems_libio_ioctl_args_t newarg;
690  struct termios termios;
691  rtems_status_code sc;
692  rtems_interrupt_level level;
693
694  rtems_interrupt_disable (level);
695
696  /*
697   * Set up the line with the specified parameters. The difficulty is that
698   * the line parameters are stored in the struct termios field of a
699   * struct rtems_termios_tty that is not defined in a public header file.
700   * Therefore, we do not have direct access to the termios passed in with
701   * arg. So we make a rtems_termios_ioctl() call to get a pointer to the
702   * termios structure.
703   *
704   * THIS KLUDGE MAY BREAK IN THE FUTURE!
705   *
706   * We could have made a tcgetattr() call if we had our fd.
707   */
708  newarg.iop = args->iop;
709  newarg.command = RTEMS_IO_GET_ATTRIBUTES;
710  newarg.buffer = &termios;
711  sc = rtems_termios_ioctl (&newarg);
712  if (sc != RTEMS_SUCCESSFUL)
713    rtems_fatal_error_occurred (sc);
714
715  /*
716   *  Turn off hardware flow control. It is a pain with 3-wire cables.
717   *  The rtems_termios_ioctl() call below results in a call to
718   *  cd2401_setAttributes to initialize the line. The caller will "wait"
719   *  on the ttyMutex that it already owns; this is safe in RTEMS.
720   */
721  termios.c_cflag |= CLOCAL;    /* Ignore modem status lines */
722  newarg.command = RTEMS_IO_SET_ATTRIBUTES;
723  sc = rtems_termios_ioctl (&newarg);
724  if (sc != RTEMS_SUCCESSFUL)
725    rtems_fatal_error_occurred (sc);
726
727  /* Mark that the channel as initialized */
728  CD2401_Channel_Info[minor].tty = args->iop->data1;
729
730  /* If the first of the four channels to open, set up the interrupts */
731  if ( !Init_count++ ) {
732    /* Install the interrupt handlers */
733    Prev_re_isr    = (rtems_isr_entry) set_vector( cd2401_re_isr,    0x5C, 1 );
734    Prev_modem_isr = (rtems_isr_entry) set_vector( cd2401_modem_isr, 0x5D, 1 );
735    Prev_tx_isr    = (rtems_isr_entry) set_vector( cd2401_tx_isr,    0x5E, 1 );
736    Prev_rx_isr    = (rtems_isr_entry) set_vector( cd2401_rx_isr,    0x5F, 1 );
737
738    cd2401_interrupts_initialize( TRUE );
739  }
740
741  CD2401_RECORD_FIRST_OPEN_INFO(( minor, Init_count ));
742 
743  rtems_interrupt_enable (level);
744
745  /* Return something */
746  return RTEMS_SUCCESSFUL;
747}
748
749
750/*
751 * cd2401_lastClose
752 *
753 *  There are no more opened file descriptors to this device. Close it down.
754 *
755 *  Input parameters:
756 *    major - device major number
757 *    minor - channel number
758 *    arg - pointer to a struct rtems_libio_open_close_args_t
759 */
760int cd2401_lastClose(
761  int major,
762  int minor,
763  void *arg
764)
765{
766  rtems_interrupt_level level;
767
768  rtems_interrupt_disable (level);
769 
770  /* Mark that the channel is no longer is use */
771  CD2401_Channel_Info[minor].tty = NULL;
772
773  /* If the last of the four channels to close, disable the interrupts */
774  if ( !--Init_count ) {
775    cd2401_interrupts_initialize( FALSE );
776
777    /* De-install the interrupt handlers */
778    set_vector( Prev_re_isr,    0x5C, 1 );
779    set_vector( Prev_modem_isr, 0x5D, 1 );
780    set_vector( Prev_tx_isr,    0x5E, 1 );
781    set_vector( Prev_rx_isr,    0x5F, 1 );
782  }
783
784  CD2401_RECORD_LAST_CLOSE_INFO(( minor, Init_count ));
785 
786  rtems_interrupt_enable (level);
787
788  /* return something */
789  return RTEMS_SUCCESSFUL;
790}
791
792
793/*
794 *  cd2401_setAttributes
795 *
796 *  Set up the selected channel of the CD2401 chip for doing asynchronous
797 *  I/O with DMA.
798 *
799 *  The chip must already have been initialized by cd2401_initialize().
800 *
801 *  This code was written for clarity. The code space it occupies could be
802 *  reduced. The code could also be compiled with aggressive optimization
803 *  turned on.
804 *
805 *  Input parameters:
806 *    minor - the selected channel
807 *    t - the termios parameters
808 *
809 *  Output parameters: NONE
810 *
811 *  Return value: IGNORED
812 */
813int cd2401_setAttributes(
814  int minor,
815  const struct termios *t
816)
817{
818  rtems_unsigned8 csize, cstopb, parodd, parenb, ignpar, inpck;
819  rtems_unsigned8 hw_flow_ctl, sw_flow_ctl, extra_flow_ctl;
820  rtems_unsigned8 icrnl, igncr, inlcr, brkint, ignbrk, parmrk, istrip;
821  rtems_unsigned8 need_reinitialization = FALSE;
822  rtems_unsigned8 read_enabled;
823  rtems_unsigned16 tx_period, rx_period;
824  rtems_unsigned32 out_baud, in_baud;
825  rtems_interrupt_level level;
826
827  /* Determine what the line parameters should be */
828
829  /* Output baud rate */
830  switch ( cfgetospeed (t) ) {
831    default:      out_baud = 9600;    break;
832    case B50:     out_baud = 50;      break;
833    case B75:     out_baud = 75;      break;
834    case B110:    out_baud = 110;     break;
835    case B134:    out_baud = 134;     break;
836    case B150:    out_baud = 150;     break;
837    case B200:    out_baud = 200;     break;
838    case B300:    out_baud = 300;     break;
839    case B600:    out_baud = 600;     break;
840    case B1200:   out_baud = 1200;    break;
841    case B1800:   out_baud = 1800;    break;
842    case B2400:   out_baud = 2400;    break;
843    case B4800:   out_baud = 4800;    break;
844    case B9600:   out_baud = 9600;    break;
845    case B19200:  out_baud = 19200;   break;
846    case B38400:  out_baud = 38400;   break;
847    case B57600:  out_baud = 57600;   break;
848    case B115200: out_baud = 115200;  break;
849    case B230400: out_baud = 230400;  break;
850    case B460800: out_baud = 460800;  break;
851 }
852
853  /* Input baud rate */
854  switch ( cfgetispeed (t) ) {
855    default:      in_baud = out_baud; break;
856    case B50:     in_baud = 50;       break;
857    case B75:     in_baud = 75;       break;
858    case B110:    in_baud = 110;      break;
859    case B134:    in_baud = 134;      break;
860    case B150:    in_baud = 150;      break;
861    case B200:    in_baud = 200;      break;
862    case B300:    in_baud = 300;      break;
863    case B600:    in_baud = 600;      break;
864    case B1200:   in_baud = 1200;     break;
865    case B1800:   in_baud = 1800;     break;
866    case B2400:   in_baud = 2400;     break;
867    case B4800:   in_baud = 4800;     break;
868    case B9600:   in_baud = 9600;     break;
869    case B19200:  in_baud = 19200;    break;
870    case B38400:  in_baud = 38400;    break;
871    case B57600:  in_baud = 57600;    break;
872    case B115200: in_baud = 115200;   break;
873    case B230400: in_baud = 230400;   break;
874    case B460800: in_baud = 460800;   break;
875  }
876
877  /* Number of bits per char */
878  switch ( t->c_cflag & CSIZE ) {
879    case CS5:     csize = 0x04;       break;
880    case CS6:     csize = 0x05;       break;
881    case CS7:     csize = 0x06;       break;
882    case CS8:     csize = 0x07;       break;
883  }
884
885  /* Parity */
886  if ( t->c_cflag & PARODD )
887    parodd = 0x80;              /* Odd parity */
888  else
889    parodd = 0;
890
891  if ( t->c_cflag & PARENB )
892    parenb = 0x40;              /* Parity enabled on Tx and Rx */
893  else
894    parenb = 0x00;              /* No parity on Tx and Rx */
895
896  /* CD2401 IGNPAR and INPCK bits are inverted wrt POSIX standard? */
897  if ( t->c_iflag & INPCK )
898    ignpar = 0;                 /* Check parity on input */
899  else
900    ignpar = 0x10;              /* Do not check parity on input */
901  if ( t->c_iflag & IGNPAR ) {
902    inpck = 0x03;               /* Discard error character */
903    parmrk = 0;
904  } else {
905    if ( t->c_iflag & PARMRK ) {
906      inpck = 0x01;             /* Translate to 0xFF 0x00 <char> */
907      parmrk = 0x04;
908    } else {
909      inpck = 0x01;             /* Translate to 0x00 */
910      parmrk = 0;
911    }
912  }
913
914  /* Stop bits */
915  if ( t->c_cflag & CSTOPB )
916    cstopb = 0x04;              /* Two stop bits */
917  else
918    cstopb = 0x02;              /* One stop bit */
919
920  /* Modem flow control */
921  if ( t->c_cflag & CLOCAL )
922    hw_flow_ctl = 0x04;         /* Always assert RTS before Tx */
923  else
924    hw_flow_ctl = 0x07;         /* Always assert RTS before Tx,
925                                   wait for CTS and DSR */
926
927  /* XON/XOFF Tx flow control */
928  if ( t->c_iflag & IXON ) {
929    sw_flow_ctl = 0x40;         /* Tx in-band flow ctl enabled, wait for XON */
930    extra_flow_ctl = 0x30;      /* Eat XON/XOFF, XON/XOFF in SCHR1, SCHR2 */
931  }
932  else {
933    sw_flow_ctl = 0;            /* Tx in-band flow ctl disabled */
934    extra_flow_ctl = 0;         /* Pass on XON/XOFF */
935  }
936
937  /* CL/LF translation */
938  if ( t->c_iflag & ICRNL )
939    icrnl = 0x40;               /* Map CR to NL on input */
940  else
941    icrnl = 0;                  /* Pass on CR */
942  if ( t->c_iflag & INLCR )
943    inlcr = 0x20;               /* Map NL to CR on input */
944  else
945    inlcr = 0;                  /* Pass on NL */
946  if ( t->c_iflag & IGNCR )
947    igncr = 0x80;               /* CR discarded on input */
948  else
949    igncr = 0;
950
951  /* Break handling */
952  if ( t->c_iflag & IGNBRK ) {
953    ignbrk = 0x10;              /* Ignore break on input */
954    brkint = 0x08;
955  } else {
956    if ( t->c_iflag & BRKINT ) {
957      ignbrk = 0;               /* Generate SIGINT (interrupt ) */
958      brkint = 0;
959    } else {
960      ignbrk = 0;               /* Convert to 0x00 */
961      brkint = 0x08;
962    }
963  }
964
965  /* Stripping */
966  if ( t->c_iflag & ISTRIP )
967    istrip = 0x80;              /* Strip to 7 bits */
968  else
969    istrip = 0;                 /* Leave as 8 bits */
970
971  rx_period = cd2401_bitrate_divisor( 20000000Ul, &in_baud );
972  tx_period = cd2401_bitrate_divisor( 20000000Ul, &out_baud );
973
974  /*
975   *  If this is the first time that the line characteristics are set up, then
976   *  the device must be re-initialized.
977   *  Also check if we need to change anything. It is preferable to not touch
978   *  the device if nothing changes. As soon as we touch it, it tends to
979   *  glitch. If anything changes, we reprogram all registers. This is
980   *  harmless.
981   */
982  if ( ( CD2401_Channel_Info[minor].tty == 0 ) ||
983       ( cd2401->cor1 != (parodd | parenb | ignpar | csize) ) ||
984       ( cd2401->cor2 != (sw_flow_ctl | hw_flow_ctl) ) ||
985       ( cd2401->cor3 != (extra_flow_ctl | cstopb) )  ||
986       ( cd2401->cor6 != (igncr | icrnl | inlcr | ignbrk | brkint | parmrk | inpck) ) ||
987       ( cd2401->cor7 != istrip ) ||
988       ( cd2401->u1.async.schr1 != t->c_cc[VSTART] ) ||
989       ( cd2401->u1.async.schr2 != t->c_cc[VSTOP] ) ||
990       ( cd2401->rbpr != (unsigned char)rx_period ) ||
991       ( cd2401->rcor != (unsigned char)(rx_period >> 8) ) ||
992       ( cd2401->tbpr != (unsigned char)tx_period ) ||
993       ( cd2401->tcor != ( (tx_period >> 3) & 0xE0 ) ) )
994    need_reinitialization = TRUE;
995
996  /* Write to the ports */
997  rtems_interrupt_disable (level);
998
999  cd2401->car = minor;          /* Select channel */
1000  read_enabled = cd2401->csr & 0x80 ? TRUE : FALSE;
1001 
1002  if ( (t->c_cflag & CREAD ? TRUE : FALSE ) != read_enabled ) {
1003    /* Read enable status is changing */
1004    need_reinitialization = TRUE;
1005  }
1006 
1007  if ( need_reinitialization ) {
1008    /*
1009     *  Could not find a way to test whether the CD2401 was done transmitting.
1010     *  The TxEmpty interrupt does not seem to indicate that the FIFO is empty
1011     *  in DMA mode. So, just wait a while for output to drain. May not be
1012     *  enough, but it will have to do (should be long enough for 1 char at
1013     *  9600 bsp)...
1014     */
1015    cd2401_udelay( 2000L );
1016 
1017    /* Clear channel */
1018    cd2401_chan_cmd (minor, 0x40, 1);
1019
1020    cd2401->car = minor;        /* Select channel */
1021    cd2401->cmr = 0x42;         /* Interrupt Rx, DMA Tx, async mode */
1022    cd2401->cor1 = parodd | parenb | ignpar | csize;
1023    cd2401->cor2 = sw_flow_ctl | hw_flow_ctl;
1024    cd2401->cor3 = extra_flow_ctl | cstopb;
1025    cd2401->cor4 = 0x0A;        /* No DSR/DCD/CTS detect; FIFO threshold of 10 */
1026    cd2401->cor5 = 0x0A;        /* No DSR/DCD/CTS detect; DTR threshold of 10 */
1027    cd2401->cor6 = igncr | icrnl | inlcr | ignbrk | brkint | parmrk | inpck;
1028    cd2401->cor7 = istrip;      /* No LNext; ignore XON/XOFF if frame error; no tx translations */
1029    /* Special char 1: XON character */
1030    cd2401->u1.async.schr1 = t->c_cc[VSTART];
1031    /* special char 2: XOFF character */
1032    cd2401->u1.async.schr2 = t->c_cc[VSTOP];
1033   
1034    /*
1035     *  Special chars 3 and 4, char range, LNext, RFAR[1..4] and CRC
1036     *  are unused, left as is.
1037     */
1038
1039    /* Set baudrates for receiver and transmitter */
1040    cd2401->rbpr = (unsigned char)rx_period;
1041    cd2401->rcor = (unsigned char)(rx_period >> 8); /* no DPLL */
1042    cd2401->tbpr = (unsigned char)tx_period;
1043    cd2401->tcor = (tx_period >> 3) & 0xE0;         /* no x1 ext clk, no loopback */
1044 
1045    /* Timeout for 4 chars at 9600, 8 bits per char, 1 stop bit */
1046    cd2401->u2.w.rtpr  = 0x04;  /* NEED TO LOOK AT THIS LINE! */
1047   
1048    if ( t->c_cflag & CREAD ) {
1049      /* Re-initialize channel, enable rx and tx */
1050      cd2401_chan_cmd (minor, 0x2A, 1);
1051      /* Enable rx data ints */
1052      cd2401->ier = 0x08;
1053    } else {
1054      /* Re-initialize channel, enable tx, disable rx */
1055      cd2401_chan_cmd (minor, 0x29, 1);
1056    }
1057  }   
1058 
1059  CD2401_RECORD_SET_ATTRIBUTES_INFO(( minor, need_reinitialization, csize,
1060                                      cstopb, parodd, parenb, ignpar, inpck,
1061                                      hw_flow_ctl, sw_flow_ctl, extra_flow_ctl,
1062                                      icrnl, igncr, inlcr, brkint, ignbrk,
1063                                      parmrk, istrip, tx_period, rx_period,
1064                                      out_baud, in_baud ));
1065
1066  rtems_interrupt_enable (level);
1067 
1068  /*
1069   *  Looks like the CD2401 needs time to settle after initialization. Give it
1070   *  10 ms. I don't really believe it, but if output resumes to quickly after
1071   *  this call, the first few characters are not right.   
1072   */
1073  if ( need_reinitialization )
1074    cd2401_udelay( 10000L );
1075
1076  /* Return something */
1077  return RTEMS_SUCCESSFUL;
1078}
1079
1080
1081/*
1082 *  cd2401_startRemoreTx
1083 *
1084 *  Defined as a callback, but it would appear that it is never called. The
1085 *  POSIX standard states that when the tcflow() function is called with the
1086 *  TCION action, the system wall transmit a START character. Presumably,
1087 *  tcflow() is called internally when IXOFF is set in the termios c_iflag
1088 *  field when the input buffer can accomodate enough characters. It should
1089 *  probably be called from fillBufferQueue(). Clearly, the function is also
1090 *  explicitly callable by user code. The action is clearly to send the START
1091 *  character, regardless of whether START/STOP flow control is in effect.
1092 *
1093 *  Input parameters:
1094 *    minor - selected channel
1095 *
1096 *  Output parameters: NONE
1097 *
1098 *  Return value: IGNORED
1099 *
1100 *  PROPER START CHARACTER MUST BE PROGRAMMED IN SCHR1.
1101 */
1102int cd2401_startRemoteTx(
1103  int minor
1104)
1105{
1106  rtems_interrupt_level level;
1107
1108  rtems_interrupt_disable (level);
1109
1110  cd2401->car = minor;              /* Select channel */
1111  cd2401->stcr = 0x01;              /* Send SCHR1 ahead of chars in FIFO */
1112
1113  CD2401_RECORD_START_REMOTE_TX_INFO(( minor ));
1114 
1115  rtems_interrupt_enable (level);
1116
1117  /* Return something */
1118  return RTEMS_SUCCESSFUL;
1119}
1120
1121
1122/*
1123 *  cd2401_stopRemoteTx
1124 *
1125 *  Defined as a callback, but it would appear that it is never called. The
1126 *  POSIX standard states that when the tcflow() function is called with the
1127 *  TCIOFF function, the system wall transmit a STOP character. Presumably,
1128 *  tcflow() is called internally when IXOFF is set in the termios c_iflag
1129 *  field as the input buffer is about to overflow. It should probably be
1130 *  called from rtems_termios_enqueue_raw_characters(). Clearly, the function
1131 *  is also explicitly callable by user code. The action is clearly to send
1132 *  the STOP character, regardless of whether START/STOP flow control is in
1133 *  effect.
1134 *
1135 *  Input parameters:
1136 *    minor - selected channel
1137 *
1138 *  Output parameters: NONE
1139 *
1140 *  Return value: IGNORED
1141 *
1142 *  PROPER STOP CHARACTER MUST BE PROGRAMMED IN SCHR2.
1143 */
1144int cd2401_stopRemoteTx(
1145  int minor
1146)
1147{
1148  rtems_interrupt_level level;
1149
1150  rtems_interrupt_disable (level);
1151
1152  cd2401->car = minor;              /* Select channel */
1153  cd2401->stcr = 0x02;              /* Send SCHR2 ahead of chars in FIFO */
1154
1155  CD2401_RECORD_STOP_REMOTE_TX_INFO(( minor ));
1156
1157  rtems_interrupt_enable (level);
1158
1159  /* Return something */
1160  return RTEMS_SUCCESSFUL;
1161}
1162
1163
1164/*
1165 *  cd2401_write
1166 *
1167 *  Initiate DMA output. Termios guarantees that the buffer does not wrap
1168 *  around, so we can do DMA strait from the supplied buffer.
1169 *
1170 *  Input parameters:
1171 *    minor - selected channel
1172 *    buf - output buffer
1173 *    len - number of chars to output
1174 *
1175 *  Output parameters:  NONE
1176 *
1177 *  Return value: IGNORED
1178 *
1179 *  MUST BE EXECUTED WITH THE CD2401 INTERRUPTS DISABLED!
1180 *  The processor is placed at interrupt level CD2401_INT_LEVEL explicitly in
1181 *  console_write(). The processor is necessarily at interrupt level 1 in
1182 *  cd2401_tx_isr().
1183 */
1184int cd2401_write(
1185  int minor,
1186  const char *buf,
1187  int len
1188)
1189{
1190  cd2401->car = minor;              /* Select channel */
1191
1192  if ( (cd2401->dmabsts & 0x08) == 0 ) {
1193    /* Next buffer is A. Wait for it to be ours. */
1194    while ( cd2401->atbsts & 0x01 );
1195
1196    CD2401_Channel_Info[minor].own_buf_A = FALSE;
1197    CD2401_Channel_Info[minor].len = len;
1198    CD2401_Channel_Info[minor].buf = buf;
1199    cd2401->atbadru = (rtems_unsigned16)( ( (rtems_unsigned32) buf ) >> 16 );
1200    cd2401->atbadrl = (rtems_unsigned16)( (rtems_unsigned32) buf );
1201    cd2401->atbcnt = len;
1202    CD2401_RECORD_WRITE_INFO(( len, buf, 'A' ));
1203    cd2401->atbsts = 0x03;          /* CD2401 owns buffer, int when empty */
1204  }
1205  else {
1206    /* Next buffer is B. Wait for it to be ours. */
1207    while ( cd2401->btbsts & 0x01 );
1208
1209    CD2401_Channel_Info[minor].own_buf_B = FALSE;
1210    CD2401_Channel_Info[minor].len = len;
1211    CD2401_Channel_Info[minor].buf = buf;
1212    cd2401->btbadru = (rtems_unsigned16)( ( (rtems_unsigned32) buf ) >> 16 );
1213    cd2401->btbadrl = (rtems_unsigned16)( (rtems_unsigned32) buf );
1214    cd2401->btbcnt = len;
1215    CD2401_RECORD_WRITE_INFO(( len, buf, 'B' ));
1216    cd2401->btbsts = 0x03;          /* CD2401 owns buffer, int when empty */
1217  }
1218  /* Nuts -- Need TxD ints */
1219  CD2401_Channel_Info[minor].txEmpty = FALSE;
1220  cd2401->ier |= 0x01;
1221
1222  /* Return something */
1223  return RTEMS_SUCCESSFUL;
1224}
1225
1226#if 0
1227/*
1228 *  cd2401_drainOutput
1229 *
1230 *  Wait for the txEmpty indication on the specified channel.
1231 *
1232 *  Input parameters:
1233 *    minor - selected channel
1234 *
1235 *  Output parameters:  NONE
1236 *
1237 *  Return value: IGNORED
1238 *
1239 *  MUST NOT BE EXECUTED WITH THE CD2401 INTERRUPTS DISABLED!
1240 *  The txEmpty flag is set by the tx ISR.
1241 */
1242int cd2401_drainOutput(
1243  int minor
1244)
1245{
1246  CD2401_RECORD_DRAIN_OUTPUT_INFO(( CD2401_Channel_Info[minor].txEmpty,
1247                                    CD2401_Channel_Info[minor].own_buf_A,
1248                                    CD2401_Channel_Info[minor].own_buf_B ));
1249   
1250  while( ! (CD2401_Channel_Info[minor].txEmpty &&
1251            CD2401_Channel_Info[minor].own_buf_A &&
1252            CD2401_Channel_Info[minor].own_buf_B) );
1253       
1254  /* Return something */
1255  return RTEMS_SUCCESSFUL;
1256}
1257#endif
1258
1259
1260/*
1261 * _167Bug_pollRead
1262 *
1263 *  Read a character from the 167Bug console, and return it. Return -1
1264 *  if there is no character in the input FIFO.
1265 *
1266 *  Input parameters:
1267 *    minor - selected channel
1268 *
1269 *  Output parameters:  NONE
1270 *
1271 *  Return value: char returned as positive signed int
1272 *                -1 if no character is present in the input FIFO.
1273 *
1274 *  CANNOT BE COMBINED WITH INTERRUPT DRIVEN I/O!
1275 *  This function is invoked when the device driver is compiled with
1276 *  CD2401_POLLED_IO set to 1 above. All I/O is then done through 167Bug.
1277 */
1278int _167Bug_pollRead(
1279  int minor
1280)
1281{
1282  int char_not_available;
1283  unsigned char c;
1284
1285  /* Check for a char in the input FIFO */
1286  asm volatile( "movew  #0x1, -(%%sp)   /* Code for .INSTAT */
1287                 movew  %1, -(%%sp)     /* Channel */
1288                 trap   #15             /* Trap to 167Bug */
1289                 .short 0x60            /* Code for .REDIR */
1290                 move   %%cc, %0        /* Get condition codes */
1291                 andil  #4, %0"         /* Keep the Zero bit */
1292    : "=d" (char_not_available) : "d" (minor): "%%cc" );
1293
1294  if (char_not_available)
1295    return -1;
1296
1297  /* Read the char and return it */
1298  asm volatile( "subq.l #2,%%a7         /* Space for result */
1299                 movew  #0x0, -(%%sp)   /* Code for .INCHR */
1300                 movew  %1, -(%%sp)     /* Channel */
1301                 trap   #15             /* Trap to 167 Bug */
1302                 .short 0x60            /* Code for .REDIR */
1303                 moveb  (%%a7)+, %0"    /* Pop char into c */
1304    : "=d" (c) : "d" (minor) );
1305
1306  return (int)c;
1307}
1308
1309
1310/*
1311 * _167Bug_pollWrite
1312 *
1313 *  Output buffer through 167Bug. Returns only once every character has been
1314 *  sent (polled output).
1315 *
1316 *  Input parameters:
1317 *    minor - selected channel
1318 *    buf - output buffer
1319 *    len - number of chars to output
1320 *
1321 *  Output parameters:  NONE
1322 *
1323 *  Return value: IGNORED
1324 *
1325 *  CANNOT BE COMBINED WITH INTERRUPT DRIVEN I/O!
1326 *  This function is invoked when the device driver is compiled with
1327 *  CD2401_POLLED_IO set to 1 above. All I/O is then done through 167Bug.
1328 */
1329int _167Bug_pollWrite(
1330  int minor,
1331  const char *buf,
1332  int len
1333)
1334{
1335  const char *endbuf = buf + len;
1336
1337  asm volatile( "pea    (%0)            /* endbuf */
1338                 pea    (%1)            /* buf */
1339                 movew  #0x21, -(%%sp)  /* Code for .OUTSTR */
1340                 movew  %2, -(%%sp)     /* Channel */
1341                 trap   #15             /* Trap to 167Bug */
1342                 .short 0x60"           /* Code for .REDIR */
1343    :: "a" (endbuf), "a" (buf), "d" (minor) );
1344
1345  /* Return something */
1346  return RTEMS_SUCCESSFUL;
1347}
1348
1349
1350/*
1351 *  Print functions: prototyped in bsp.h
1352 *  Debug printing on Channel 1
1353 */
1354 
1355void printk( char *fmt, ... )
1356{
1357  va_list  ap;                  /* points to each unnamed argument in turn */
1358  static char buf[256];
1359  unsigned int level;
1360 
1361  _CPU_ISR_Disable(level);
1362 
1363  va_start(ap, fmt);            /* make ap point to 1st unnamed arg */
1364  vsprintf(buf, fmt, ap);       /* send output to buffer */
1365 
1366  BSP_output_string(buf);       /* print buffer -- Channel 1 */
1367 
1368  va_end(ap);                           /* clean up and re-enable interrupts */
1369  _CPU_ISR_Enable(level);
1370}
1371
1372
1373void BSP_output_string( char * buf )
1374{
1375  int len = strlen(buf);                       
1376  rtems_status_code sc;
1377 
1378  /* The first argument forces a print to Port2 (ttyS1) */
1379  sc = _167Bug_pollWrite(1, buf, len);
1380  if (sc != RTEMS_SUCCESSFUL)
1381    rtems_fatal_error_occurred (sc);
1382}
1383
1384
1385/*
1386 ***************
1387 * BOILERPLATE *
1388 ***************
1389 *
1390 *  All these functions are prototyped in rtems/c/src/lib/include/console.h.
1391 */
1392
1393/*
1394 * Initialize and register the device
1395 */
1396rtems_device_driver console_initialize(
1397  rtems_device_major_number  major,
1398  rtems_device_minor_number  minor,
1399  void                      *arg
1400)
1401{
1402  rtems_status_code status;
1403
1404  /*
1405   * Set up TERMIOS
1406   */
1407  rtems_termios_initialize ();
1408
1409  /*
1410   * Do device-specific initialization
1411   */
1412  cd2401_initialize ();
1413
1414  /*
1415   * Register the devices
1416   */
1417  status = rtems_io_register_name ("/dev/tty0", major, 0);
1418  if (status != RTEMS_SUCCESSFUL)
1419    rtems_fatal_error_occurred (status);
1420
1421  status = rtems_io_register_name ("/dev/tty1", major, 1);
1422  if (status != RTEMS_SUCCESSFUL)
1423    rtems_fatal_error_occurred (status);
1424
1425  status = rtems_io_register_name ("/dev/console", major, 1);
1426  if (status != RTEMS_SUCCESSFUL)
1427    rtems_fatal_error_occurred (status);
1428
1429  status = rtems_io_register_name ("/dev/tty2", major, 2);
1430  if (status != RTEMS_SUCCESSFUL)
1431    rtems_fatal_error_occurred (status);
1432
1433  status = rtems_io_register_name ("/dev/tty3", major, 3);
1434  if (status != RTEMS_SUCCESSFUL)
1435    rtems_fatal_error_occurred (status);
1436
1437  return RTEMS_SUCCESSFUL;
1438}
1439
1440/*
1441 * Open the device
1442 */
1443rtems_device_driver console_open(
1444  rtems_device_major_number major,
1445  rtems_device_minor_number minor,
1446  void                    * arg
1447)
1448{
1449#if CD2401_POLLED_IO
1450
1451  /* I/O is limited to 167Bug console. minor is ignored! */
1452  static const rtems_termios_callbacks callbacks = {
1453    NULL,                       /* firstOpen */
1454    NULL,                       /* lastClose */
1455    _167Bug_pollRead,           /* pollRead */
1456    _167Bug_pollWrite,          /* write */
1457    NULL,                       /* setAttributes */
1458    NULL,                       /* stopRemoteTx */
1459    NULL,                       /* startRemoteTx */
1460    0                           /* outputUsesInterrupts */
1461  };
1462
1463#else
1464
1465  static const rtems_termios_callbacks callbacks = {
1466    cd2401_firstOpen,           /* firstOpen */
1467    cd2401_lastClose,           /* lastClose */
1468    NULL,                       /* pollRead */
1469    cd2401_write,               /* write */
1470    cd2401_setAttributes,       /* setAttributes */
1471    cd2401_stopRemoteTx,        /* stopRemoteTx */
1472    cd2401_startRemoteTx,       /* startRemoteTx */
1473    1                           /* outputUsesInterrupts */
1474  };
1475
1476#endif
1477
1478  return rtems_termios_open (major, minor, arg, &callbacks);
1479}
1480
1481/*
1482 * Close the device
1483 */
1484rtems_device_driver console_close(
1485  rtems_device_major_number major,
1486  rtems_device_minor_number minor,
1487  void                    * arg
1488)
1489{
1490  return rtems_termios_close (arg);
1491}
1492
1493/*
1494 * Read from the device
1495 */
1496rtems_device_driver console_read(
1497  rtems_device_major_number major,
1498  rtems_device_minor_number minor,
1499  void                    * arg
1500)
1501{
1502  return rtems_termios_read (arg);
1503}
1504
1505/*
1506 * Write to the device
1507 */
1508rtems_device_driver console_write(
1509  rtems_device_major_number major,
1510  rtems_device_minor_number minor,
1511  void                    * arg
1512)
1513{
1514  return rtems_termios_write (arg);
1515}
1516
1517/*
1518 * Handle ioctl request.
1519 */
1520rtems_device_driver console_control(
1521  rtems_device_major_number major,
1522  rtems_device_minor_number minor,
1523  void                    * arg
1524)
1525{
1526  return rtems_termios_ioctl (arg);
1527}
Note: See TracBrowser for help on using the repository browser.