source: rtems/c/src/lib/libbsp/m68k/mvme167/console/console.c @ 6825d06

4.104.114.95
Last change on this file since 6825d06 was 6825d06, checked in by Joel Sherrill <joel.sherrill@…>, on 05/23/08 at 15:48:38

2008-05-23 Joel Sherrill <joel.sherrill@…>

  • console/console.c: Eliminate copies of switches to convert termios Bxxx constants to xxx as an integer. Use the shared termios_baud_to_number() routine to do the same conversion.
  • Property mode set to 100644
File size: 52.9 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.rtems.com/license/LICENSE.
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/* Utility functions */
137void cd2401_udelay( unsigned long delay );
138void cd2401_chan_cmd( uint8_t         channel, uint8_t         cmd, uint8_t         wait );
139uint16_t         cd2401_bitrate_divisor( uint32_t         clkrate, uint32_t        * bitrate );
140void cd2401_initialize( void );
141void cd2401_interrupts_initialize( rtems_boolean enable );
142
143/* ISRs */
144rtems_isr cd2401_modem_isr( rtems_vector_number vector );
145rtems_isr cd2401_re_isr( rtems_vector_number vector );
146rtems_isr cd2401_rx_isr( rtems_vector_number vector );
147rtems_isr cd2401_tx_isr( rtems_vector_number vector );
148
149/* Termios callbacks */
150int cd2401_firstOpen( int major, int minor, void *arg );
151int cd2401_lastClose( int major, int minor, void *arg );
152int cd2401_setAttributes( int minor, const struct termios *t );
153int cd2401_startRemoteTx( int minor );
154int cd2401_stopRemoteTx( int minor );
155int cd2401_write( int minor, const char *buf, int len );
156int cd2401_drainOutput( int minor );
157int _167Bug_pollRead( int minor );
158int _167Bug_pollWrite( int minor, const char *buf, int len );
159
160/* Printk function */
161static void _BSP_output_char( char c );
162BSP_output_char_function_type BSP_output_char = _BSP_output_char;
163
164/* Channel info */
165/* static */ volatile struct {
166  void *tty;                    /* Really a struct rtems_termios_tty * */
167  int len;                      /* Record nb of chars being TX'ed */
168  const char *buf;              /* Record where DMA is coming from */
169  uint32_t         spur_cnt;    /* Nb of spurious ints so far */
170  uint32_t         spur_dev;    /* Indo on last spurious int */
171  uint32_t         buserr_addr; /* Faulting address */
172  uint32_t         buserr_type; /* Reason of bus error during DMA */
173  uint8_t          own_buf_A;   /* If true, buffer A belongs to the driver */
174  uint8_t          own_buf_B;   /* If true, buffer B belongs to the driver */
175  uint8_t          txEmpty;     /* If true, the output FIFO should be empty */
176} CD2401_Channel_Info[4];
177
178/*
179 *  The number of channels already opened. If zero, enable the interrupts. The
180 *  initial value must be 0. If initialized explicitly, the variable ends up
181 *  in the .data section. Its value is not re-initialized on system restart.
182 *  Furthermore, because the variable is changed, the .data section would not
183 *  be ROMable. We thus leave the variable uninitialized, which causes it to
184 *  be allocated in the .bss section, and rely on RTEMS to zero the .bss
185 *  section on every startup.
186 */
187uint8_t         Init_count;
188
189/* Record previous handlers */
190rtems_isr_entry Prev_re_isr;        /* Previous rx exception isr */
191rtems_isr_entry Prev_rx_isr;        /* Previous rx isr */
192rtems_isr_entry Prev_tx_isr;        /* Previous tx isr */
193rtems_isr_entry Prev_modem_isr;     /* Previous modem/timer isr */
194
195/* Define the following symbol to trace the calls to this driver */
196/* #define CD2401_RECORD_DEBUG_INFO */
197#include "console-recording.h"
198
199/*
200 *  Utility functions.
201 */
202
203/*
204 *  Assumes that clock ticks 1 million times per second.
205 *
206 *  MAXIMUM DELAY IS ABOUT 20 ms
207 *
208 *  Input parameters:
209 *    delay: Number of microseconds to delay.
210 *
211 *  Output parameters: NONE
212 *
213 *  Return values: NONE
214 */
215 void cd2401_udelay
216(
217  unsigned long delay
218)
219{
220  unsigned long i = 20000;  /* In case clock is off */
221  rtems_interval ticks_per_second, start_ticks, end_ticks, current_ticks;
222
223  rtems_clock_get( RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticks_per_second );
224  rtems_clock_get( RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &start_ticks );
225  end_ticks = start_ticks + delay;
226
227  do {
228    rtems_clock_get(RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &current_ticks);
229  } while ( --i && (current_ticks <= end_ticks) );
230
231  CD2401_RECORD_DELAY_INFO(( start_ticks, end_ticks, current_ticks, i ));
232}
233
234/*
235 *  cd2401_chan_cmd
236 *
237 *  Sends a CCR command to the specified channel. Waits for any unfinished
238 *  previous command to complete, then sends the specified command. Optionally
239 *  wait for the current command to finish before returning.
240 *
241 *  Input parameters:
242 *    channel - CD2401 channel number
243 *    cmd  - command byte
244 *    wait - if non-zero, wait for specified command to complete before
245 *          returning.
246 *
247 *  Output parameters: NONE
248 *
249 *  Return values: NONE
250 */
251void cd2401_chan_cmd(
252  uint8_t         channel,
253  uint8_t         cmd,
254  uint8_t         wait
255)
256{
257  if ( channel < 4 ) {
258    cd2401->car = channel;      /* Select channel */
259
260    while ( cd2401->ccr != 0 ); /* Wait for completion of previous command */
261    cd2401->ccr = cmd;          /* Send command */
262    if ( wait )
263      while( cd2401->ccr != 0 );/* Wait for completion */
264  }
265  else {
266    /* This may not be the best error message */
267    rtems_fatal_error_occurred( RTEMS_INVALID_NUMBER );
268  }
269}
270
271/*
272 *  cd2401_bitrate_divisor
273 *
274 *  Compute the divisor and clock source to use to obtain the desired bitrate.
275 *
276 *  Input parameters:
277 *    clkrate - system clock rate (CLK input frequency)
278 *    bitrate - the desired bitrate
279 *
280 *  Output parameters:
281 *    bitrate - The actual bitrate achievable, to the nearest bps.
282 *
283 *  Return values:
284 *    Returns divisor in lower byte and clock source in upper byte for the
285 *    specified bitrate.
286 */
287uint16_t         cd2401_bitrate_divisor(
288  uint32_t         clkrate,
289  uint32_t        * bitrate
290)
291{
292  uint32_t         divisor;
293  uint16_t         clksource;
294
295  divisor = *bitrate << 3;          /* temporary; multiply by 8 for CLK/8 */
296  divisor = (clkrate + (divisor>>1)) / divisor; /* divisor for clk0 (CLK/8) */
297
298  /* Use highest speed clock source for best precision - try clk0 to clk4 */
299  for( clksource = 0; clksource < 0x0400 && divisor > 0x100; clksource += 0x0100 )
300      divisor >>= 2;
301  divisor--;                        /* adjustment, see specs */
302  if( divisor < 1 )
303    divisor = 1;
304  else if( divisor > 0xFF )
305    divisor = 0xFF;
306  *bitrate = clkrate / (1 << ((clksource >> 7)+3)) / (divisor+1);
307  return( clksource | divisor );
308}
309
310/*
311 *  cd2401_initialize
312 *
313 *  Initializes the CD2401 device. Individual channels on the chip are left in
314 *  their default reset state, and should be subsequently configured.
315 *
316 *  Input parameters: NONE
317 *
318 *  Output parameters:  NONE
319 *
320 *  Return values: NONE
321 */
322void cd2401_initialize( void )
323{
324  int i;
325
326  for ( i = 3; i >= 0; i-- ) {
327    CD2401_Channel_Info[i].tty = NULL;
328    CD2401_Channel_Info[i].len = 0;
329    CD2401_Channel_Info[i].buf = NULL;
330    CD2401_Channel_Info[i].spur_cnt = 0;
331    CD2401_Channel_Info[i].spur_dev = 0;
332    CD2401_Channel_Info[i].buserr_type = 0;
333    CD2401_Channel_Info[i].buserr_addr = 0;
334    CD2401_Channel_Info[i].own_buf_A = TRUE;
335    CD2401_Channel_Info[i].own_buf_B = TRUE;
336    CD2401_Channel_Info[i].txEmpty = TRUE;
337  }
338
339 /*
340  *  Normally, do a device reset here. If we do it, we will most likely clober
341  *  the port settings for 167Bug on channel 0. So we just shut up all the
342  *  ports by disabling their interrupts.
343  */
344#if 0
345  cd2401->gfrcr = 0;            /* So we can detect that device init is done */
346  cd2401_chan_cmd( 0x10, 0);    /* Reset all */
347  while(cd2401->gfrcr == 0);    /* Wait for reset all */
348#endif
349
350  /*
351   *  The CL-CD2400/2401 manual (part no 542400-003) states on page 87 that
352   *  the LICR "contains the number of the interrupting channel being served.
353   *  The channel number is always that of the current acknowledged interrupt."
354   *  THE USER MUST PROGRAM CHANNEL NUMBER IN LICR! It is not set automatically
355   *  by the hardware, as suggested by the manual.
356   *
357   *  The updated manual (part no 542400-007) has the story straight. The
358   *  CD2401 automatically initializes the LICR to contain the channel number
359   *  in bits 2 and 3. However, these bits are not preserved when the user
360   *  defined bits are written.
361   *
362   *  The same vector number is used for all four channels. Different vector
363   *  numbers could be programmed for each channel, thus avoiding the need to
364   *  demultiplex the interrupts in the ISR.
365   */
366  for ( i = 0; i < 4; i++ ) {
367    cd2401->car = i;            /* Select channel */
368    cd2401->livr = 0x5C;        /* Motorola suggested value p. 3-15 */
369    cd2401->licr = i << 2;      /* Don't rely on reset value */
370    cd2401->ier = 0;            /* Disable all interrupts */
371  }
372
373  /*
374   *  The content of the CD2401 xpilr registers must match the A7-A0 addresses
375   *  generated by the PCCchip2 during interrupt acknowledge cycles in order
376   *  for the CD2401 to recognize the IACK cycle and clear its interrupt
377   *  request.
378   */
379  cd2401->mpilr = 0x01;         /* Match pccchip2->modem_piack p. 3-27 */
380  cd2401->tpilr = 0x02;         /* Match pccchip2->tx_piack p. 3-28 */
381  cd2401->rpilr = 0x03;         /* Match pccchip2->rx_piack p. 3-29 */
382
383  /* Global CD2401 registers */
384  cd2401->dmr = 0;              /* 16-bit DMA transfers when possible */
385  cd2401->bercnt = 0;           /* Do not retry DMA upon bus errors */
386
387  /*
388   *  Setup timer prescaler period, which clocks timers 1 and 2 (or rx timeout
389   *  and tx delay). The prescaler is clocked by the system clock) / 2048. The
390   *  register must be in the range 0x0A..0xFF, ie. a rescaler period range of
391   *  about 1ms..26ms for a nominal system clock rate  of 20MHz.
392   */
393  cd2401->tpr  = 0x0A;          /* Same value as 167Bug */
394}
395
396/*
397 *  cd2401_interrupts_initialize
398 *
399 *  This routine enables or disables the CD2401 interrupts to the MC68040.
400 *  Interrupts cannot be enabled/disabled on a per-channel basis.
401 *
402 *  Input parameters:
403 *    enable - if true, enable the interrupts, else disable them.
404 *
405 *  Output parameters:  NONE
406 *
407 *  Return values: NONE
408 *
409 *  THE FIRST CD2401 CHANNEL OPENED SHOULD ENABLE INTERRUPTS.
410 *  THE LAST CD2401 CHANNEL CLOSED SHOULD DISABLE INTERRUPTS.
411 */
412void cd2401_interrupts_initialize(
413  rtems_boolean enable
414)
415{
416  if ( enable ) {
417   /*
418    *  Enable interrupts from the CD2401 in the PCCchip2.
419    *  During DMA transfers, the MC68040 supplies dirty data during read cycles
420    *  from the CD2401 and leaves the data dirty in its data cache if there is
421    *  a cache hit. The MC68040 updates the data cache during write cycles from
422    *  the CD2401 if there is a cache hit.
423    */
424    pccchip2->SCC_error = 0x01;
425    pccchip2->SCC_modem_int_ctl = 0x10 | CD2401_INT_LEVEL;
426    pccchip2->SCC_tx_int_ctl = 0x10 | CD2401_INT_LEVEL;
427    pccchip2->SCC_rx_int_ctl = 0x50 | CD2401_INT_LEVEL;
428
429    pccchip2->gen_control |= 0x02;      /* Enable pccchip2 interrupts */
430  }
431  else {
432    /* Disable interrupts */
433    pccchip2->SCC_modem_int_ctl &= 0xEF;
434    pccchip2->SCC_tx_int_ctl &= 0xEF;
435    pccchip2->SCC_rx_int_ctl &= 0xEF;
436  }
437}
438
439/* ISRs */
440
441/*
442 *  cd2401_modem_isr
443 *
444 *  Modem/timer interrupt (group 1) from CD2401. These are not used, and not
445 *  expected. Record as spurious and clear.
446 *
447 *  Input parameters:
448 *    vector - vector number
449 *
450 *  Output parameters: NONE
451 *
452 *  Return values: NONE
453 */
454rtems_isr cd2401_modem_isr(
455  rtems_vector_number vector
456)
457{
458  uint8_t         ch;
459
460  /* Get interrupting channel ID */
461  ch = cd2401->licr >> 2;
462
463  /* Record interrupt info for debugging */
464  CD2401_Channel_Info[ch].spur_dev =
465      (vector << 24) | (cd2401->stk << 16) | (cd2401->mir << 8) | cd2401->misr;
466  CD2401_Channel_Info[ch].spur_cnt++;
467
468  cd2401->meoir = 0;            /* EOI */
469  CD2401_RECORD_MODEM_ISR_SPURIOUS_INFO(( ch,
470                                          CD2401_Channel_Info[ch].spur_dev,
471                                          CD2401_Channel_Info[ch].spur_cnt ));
472}
473
474/*
475 *  cd2401_re_isr
476 *
477 *  RX exception interrupt (group 3, receiver exception) from CD2401. These are
478 *  not used, and not expected. Record as spurious and clear.
479 *
480 *  FIX THIS ISR TO DETECT BREAK CONDITIONS AND RAISE SIGINT
481 *
482 *  Input parameters:
483 *    vector - vector number
484 *
485 *  Output parameters: NONE
486 *
487 *  Return values: NONE
488 */
489rtems_isr cd2401_re_isr(
490  rtems_vector_number vector
491)
492{
493  uint8_t         ch;
494
495  /* Get interrupting channel ID */
496  ch = cd2401->licr >> 2;
497
498  /* Record interrupt info for debugging */
499  CD2401_Channel_Info[ch].spur_dev =
500      (vector << 24) | (cd2401->stk << 16) | (cd2401->rir << 8) | cd2401->u5.b.risrl;
501  CD2401_Channel_Info[ch].spur_cnt++;
502
503  if ( cd2401->u5.b.risrl & 0x80 )  /* Timeout interrupt? */
504    cd2401->ier &= 0xDF;            /* Disable rx timeout interrupt */
505  cd2401->reoir = 0x08;             /* EOI; exception char not read */
506  CD2401_RECORD_RE_ISR_SPURIOUS_INFO(( ch,
507                                       CD2401_Channel_Info[ch].spur_dev,
508                                       CD2401_Channel_Info[ch].spur_cnt ));
509}
510
511/*
512 *  cd2401_rx_isr
513 *
514 *  RX interrupt (group 3, receiver data) from CD2401.
515 *
516 *  Input parameters:
517 *     vector - vector number
518 *
519 *  Output parameters: NONE
520 *
521 *  Return values: NONE
522 */
523rtems_isr cd2401_rx_isr(
524  rtems_vector_number vector
525)
526{
527  char c;
528  uint8_t         ch, status, nchars, i, total;
529  char buffer[256];
530
531  status = cd2401->u5.b.risrl;
532  ch = cd2401->licr >> 2;
533
534  /* Has this channel been initialized or is it a condition we ignore? */
535  if ( CD2401_Channel_Info[ch].tty && !status ) {
536    /* Normal Rx Int, read chars, enqueue them, and issue EOI */
537    total = nchars = cd2401->rfoc;  /* Nb of chars to retrieve from rx FIFO */
538    i = 0;
539    while ( nchars-- > 0 ) {
540      c = (char)cd2401->dr;         /* Next char in rx FIFO */
541      rtems_termios_enqueue_raw_characters( CD2401_Channel_Info[ch].tty ,&c, 1 );
542      buffer[i++] = c;
543    }
544    cd2401->reoir = 0;              /* EOI */
545    CD2401_RECORD_RX_ISR_INFO(( ch, total, buffer ));
546  } else {
547    /* No, record as spurious interrupt */
548    CD2401_Channel_Info[ch].spur_dev =
549        (vector << 24) | (cd2401->stk << 16) | (cd2401->rir << 8) | cd2401->u5.b.risrl;
550    CD2401_Channel_Info[ch].spur_cnt++;
551    cd2401->reoir = 0x04;           /* EOI - character not read */
552    CD2401_RECORD_RX_ISR_SPURIOUS_INFO(( ch, status,
553                                         CD2401_Channel_Info[ch].spur_dev,
554                                         CD2401_Channel_Info[ch].spur_cnt ));
555  }
556}
557
558/*
559 *  cd2401_tx_isr
560 *
561 *  TX interrupt (group 2) from CD2401.
562 *
563 *  Input parameters:
564 *    vector - vector number
565 *
566 *  Output parameters: NONE
567 *
568 *  Return values: NONE
569 */
570rtems_isr cd2401_tx_isr(
571  rtems_vector_number vector
572)
573{
574  uint8_t         ch, status, buserr, initial_ier, final_ier;
575
576  status = cd2401->tisr;
577  ch = cd2401->licr >> 2;
578  initial_ier = cd2401->ier;
579
580  /* Has this channel been initialized? */
581  if ( !CD2401_Channel_Info[ch].tty ) {
582    /* No, record as spurious interrupt */
583    CD2401_Channel_Info[ch].spur_dev =
584        (vector << 24) | (cd2401->stk << 16) | (cd2401->tir << 8) | cd2401->tisr;
585    CD2401_Channel_Info[ch].spur_cnt++;
586    final_ier = cd2401->ier &= 0xFC;/* Shut up, whoever you are */
587    cd2401->teoir = 0x88;           /* EOI - Terminate buffer and no transfer */
588    CD2401_RECORD_TX_ISR_SPURIOUS_INFO(( ch, status, initial_ier, final_ier,
589                                         CD2401_Channel_Info[ch].spur_dev,
590                                         CD2401_Channel_Info[ch].spur_cnt ));
591    return;
592  }
593
594  if ( status & 0x80 ) {
595    /*
596     *  Bus error occurred during DMA transfer. For now, just record.
597     *  Get reason for DMA bus error and clear the report for the next
598     *  occurrence
599     */
600    buserr = pccchip2->SCC_error;
601    pccchip2->SCC_error = 0x01;
602    CD2401_Channel_Info[ch].buserr_type =
603         (vector << 24) | (buserr << 16) | (cd2401->tir << 8) | cd2401->tisr;
604    CD2401_Channel_Info[ch].buserr_addr =
605        (((uint32_t)cd2401->tcbadru) << 16) | cd2401->tcbadrl;
606
607    cd2401->teoir = 0x80;           /* EOI - terminate bad buffer */
608    CD2401_RECORD_TX_ISR_BUSERR_INFO(( ch, status, initial_ier, buserr,
609                                       CD2401_Channel_Info[ch].buserr_type,
610                                       CD2401_Channel_Info[ch].buserr_addr ));
611    return;
612  }
613
614  if ( status & 0x20 ) {
615    /* DMA done -- Turn off TxD int, turn on TxMpty */
616    final_ier = cd2401->ier = (cd2401->ier & 0xFE) | 0x02;
617    if( status & 0x08 ) {
618      /* Transmit buffer B was released */
619      CD2401_Channel_Info[ch].own_buf_B = TRUE;
620    }
621    else {
622      /* Transmit buffer A was released */
623      CD2401_Channel_Info[ch].own_buf_A = TRUE;
624    }
625    CD2401_RECORD_TX_ISR_INFO(( ch, status, initial_ier, final_ier,
626                                CD2401_Channel_Info[ch].txEmpty ));
627
628    /* This call can result in a call to cd2401_write() */
629    rtems_termios_dequeue_characters (
630        CD2401_Channel_Info[ch].tty,
631        CD2401_Channel_Info[ch].len );
632    cd2401->teoir = 0x08;           /* EOI - no data transfered */
633  }
634  else if ( status & 0x02 ) {
635    /* TxEmpty */
636    CD2401_Channel_Info[ch].txEmpty = TRUE;
637    final_ier = cd2401->ier &= 0xFD;/* Shut up the interrupts */
638    cd2401->teoir = 0x08;           /* EOI - no data transfered */
639    CD2401_RECORD_TX_ISR_INFO(( ch, status, initial_ier, final_ier,
640                                CD2401_Channel_Info[ch].txEmpty ));
641  }
642  else {
643    /* Why did we get a Tx interrupt? */
644    CD2401_Channel_Info[ch].spur_dev =
645        (vector << 24) | (cd2401->stk << 16) | (cd2401->tir << 8) | cd2401->tisr;
646    CD2401_Channel_Info[ch].spur_cnt++;
647    cd2401->teoir = 0x08;           /* EOI - no data transfered */
648    CD2401_RECORD_TX_ISR_SPURIOUS_INFO(( ch, status, initial_ier, 0xFF,
649                                         CD2401_Channel_Info[ch].spur_dev,
650                                         CD2401_Channel_Info[ch].spur_cnt ));
651  }
652}
653
654/*
655 *  termios callbacks
656 */
657
658/*
659 *  cd2401_firstOpen
660 *
661 *  This is the first time that this minor device (channel) is opened.
662 *  Complete the asynchronous initialization.
663 *
664 *  Input parameters:
665 *    major - device major number
666 *    minor - channel number
667 *    arg - pointer to a struct rtems_libio_open_close_args_t
668 *
669 *  Output parameters: NONE
670 *
671 *  Return value: IGNORED
672 */
673int cd2401_firstOpen(
674  int major,
675  int minor,
676  void *arg
677)
678{
679  rtems_libio_open_close_args_t *args = arg;
680  rtems_libio_ioctl_args_t newarg;
681  struct termios termios;
682  rtems_status_code sc;
683  rtems_interrupt_level level;
684
685  rtems_interrupt_disable (level);
686
687  /*
688   * Set up the line with the specified parameters. The difficulty is that
689   * the line parameters are stored in the struct termios field of a
690   * struct rtems_termios_tty that is not defined in a public header file.
691   * Therefore, we do not have direct access to the termios passed in with
692   * arg. So we make a rtems_termios_ioctl() call to get a pointer to the
693   * termios structure.
694   *
695   * THIS KLUDGE MAY BREAK IN THE FUTURE!
696   *
697   * We could have made a tcgetattr() call if we had our fd.
698   */
699  newarg.iop = args->iop;
700  newarg.command = RTEMS_IO_GET_ATTRIBUTES;
701  newarg.buffer = &termios;
702  sc = rtems_termios_ioctl (&newarg);
703  if (sc != RTEMS_SUCCESSFUL)
704    rtems_fatal_error_occurred (sc);
705
706  /*
707   *  Turn off hardware flow control. It is a pain with 3-wire cables.
708   *  The rtems_termios_ioctl() call below results in a call to
709   *  cd2401_setAttributes to initialize the line. The caller will "wait"
710   *  on the ttyMutex that it already owns; this is safe in RTEMS.
711   */
712  termios.c_cflag |= CLOCAL;    /* Ignore modem status lines */
713  newarg.command = RTEMS_IO_SET_ATTRIBUTES;
714  sc = rtems_termios_ioctl (&newarg);
715  if (sc != RTEMS_SUCCESSFUL)
716    rtems_fatal_error_occurred (sc);
717
718  /* Mark that the channel as initialized */
719  CD2401_Channel_Info[minor].tty = args->iop->data1;
720
721  /* If the first of the four channels to open, set up the interrupts */
722  if ( !Init_count++ ) {
723    /* Install the interrupt handlers */
724    Prev_re_isr    = (rtems_isr_entry) set_vector( cd2401_re_isr,    0x5C, 1 );
725    Prev_modem_isr = (rtems_isr_entry) set_vector( cd2401_modem_isr, 0x5D, 1 );
726    Prev_tx_isr    = (rtems_isr_entry) set_vector( cd2401_tx_isr,    0x5E, 1 );
727    Prev_rx_isr    = (rtems_isr_entry) set_vector( cd2401_rx_isr,    0x5F, 1 );
728
729    cd2401_interrupts_initialize( TRUE );
730  }
731
732  CD2401_RECORD_FIRST_OPEN_INFO(( minor, Init_count ));
733
734  rtems_interrupt_enable (level);
735
736  /* Return something */
737  return RTEMS_SUCCESSFUL;
738}
739
740/*
741 * cd2401_lastClose
742 *
743 *  There are no more opened file descriptors to this device. Close it down.
744 *
745 *  Input parameters:
746 *    major - device major number
747 *    minor - channel number
748 *    arg - pointer to a struct rtems_libio_open_close_args_t
749 */
750int cd2401_lastClose(
751  int major,
752  int minor,
753  void *arg
754)
755{
756  rtems_interrupt_level level;
757
758  rtems_interrupt_disable (level);
759
760  /* Mark that the channel is no longer is use */
761  CD2401_Channel_Info[minor].tty = NULL;
762
763  /* If the last of the four channels to close, disable the interrupts */
764  if ( !--Init_count ) {
765    cd2401_interrupts_initialize( FALSE );
766
767    /* De-install the interrupt handlers */
768    set_vector( Prev_re_isr,    0x5C, 1 );
769    set_vector( Prev_modem_isr, 0x5D, 1 );
770    set_vector( Prev_tx_isr,    0x5E, 1 );
771    set_vector( Prev_rx_isr,    0x5F, 1 );
772  }
773
774  CD2401_RECORD_LAST_CLOSE_INFO(( minor, Init_count ));
775
776  rtems_interrupt_enable (level);
777
778  /* return something */
779  return RTEMS_SUCCESSFUL;
780}
781
782/*
783 *  cd2401_setAttributes
784 *
785 *  Set up the selected channel of the CD2401 chip for doing asynchronous
786 *  I/O with DMA.
787 *
788 *  The chip must already have been initialized by cd2401_initialize().
789 *
790 *  This code was written for clarity. The code space it occupies could be
791 *  reduced. The code could also be compiled with aggressive optimization
792 *  turned on.
793 *
794 *  Input parameters:
795 *    minor - the selected channel
796 *    t - the termios parameters
797 *
798 *  Output parameters: NONE
799 *
800 *  Return value: IGNORED
801 */
802int cd2401_setAttributes(
803  int minor,
804  const struct termios *t
805)
806{
807  uint8_t         csize, cstopb, parodd, parenb, ignpar, inpck;
808  uint8_t         hw_flow_ctl, sw_flow_ctl, extra_flow_ctl;
809  uint8_t         icrnl, igncr, inlcr, brkint, ignbrk, parmrk, istrip;
810  uint8_t         need_reinitialization = FALSE;
811  uint8_t         read_enabled;
812  uint16_t         tx_period, rx_period;
813  uint32_t         out_baud, in_baud;
814  rtems_interrupt_level level;
815
816  /* Determine what the line parameters should be */
817
818  /* baud rates */
819  out_baud = termios_baud_to_number(t->c_cflag & CBAUD);
820  in_baud  = termios_baud_to_number(t->c_cflag & CBAUD);
821
822  /* Number of bits per char */
823  csize = 0x07; /* to avoid a warning */
824  switch ( t->c_cflag & CSIZE ) {
825    case CS5:     csize = 0x04;       break;
826    case CS6:     csize = 0x05;       break;
827    case CS7:     csize = 0x06;       break;
828    case CS8:     csize = 0x07;       break;
829  }
830
831  /* Parity */
832  if ( t->c_cflag & PARODD )
833    parodd = 0x80;              /* Odd parity */
834  else
835    parodd = 0;
836
837  if ( t->c_cflag & PARENB )
838    parenb = 0x40;              /* Parity enabled on Tx and Rx */
839  else
840    parenb = 0x00;              /* No parity on Tx and Rx */
841
842  /* CD2401 IGNPAR and INPCK bits are inverted wrt POSIX standard? */
843  if ( t->c_iflag & INPCK )
844    ignpar = 0;                 /* Check parity on input */
845  else
846    ignpar = 0x10;              /* Do not check parity on input */
847  if ( t->c_iflag & IGNPAR ) {
848    inpck = 0x03;               /* Discard error character */
849    parmrk = 0;
850  } else {
851    if ( t->c_iflag & PARMRK ) {
852      inpck = 0x01;             /* Translate to 0xFF 0x00 <char> */
853      parmrk = 0x04;
854    } else {
855      inpck = 0x01;             /* Translate to 0x00 */
856      parmrk = 0;
857    }
858  }
859
860  /* Stop bits */
861  if ( t->c_cflag & CSTOPB )
862    cstopb = 0x04;              /* Two stop bits */
863  else
864    cstopb = 0x02;              /* One stop bit */
865
866  /* Modem flow control */
867  if ( t->c_cflag & CLOCAL )
868    hw_flow_ctl = 0x04;         /* Always assert RTS before Tx */
869  else
870    hw_flow_ctl = 0x07;         /* Always assert RTS before Tx,
871                                   wait for CTS and DSR */
872
873  /* XON/XOFF Tx flow control */
874  if ( t->c_iflag & IXON ) {
875    sw_flow_ctl = 0x40;         /* Tx in-band flow ctl enabled, wait for XON */
876    extra_flow_ctl = 0x30;      /* Eat XON/XOFF, XON/XOFF in SCHR1, SCHR2 */
877  }
878  else {
879    sw_flow_ctl = 0;            /* Tx in-band flow ctl disabled */
880    extra_flow_ctl = 0;         /* Pass on XON/XOFF */
881  }
882
883  /* CL/LF translation */
884  if ( t->c_iflag & ICRNL )
885    icrnl = 0x40;               /* Map CR to NL on input */
886  else
887    icrnl = 0;                  /* Pass on CR */
888  if ( t->c_iflag & INLCR )
889    inlcr = 0x20;               /* Map NL to CR on input */
890  else
891    inlcr = 0;                  /* Pass on NL */
892  if ( t->c_iflag & IGNCR )
893    igncr = 0x80;               /* CR discarded on input */
894  else
895    igncr = 0;
896
897  /* Break handling */
898  if ( t->c_iflag & IGNBRK ) {
899    ignbrk = 0x10;              /* Ignore break on input */
900    brkint = 0x08;
901  } else {
902    if ( t->c_iflag & BRKINT ) {
903      ignbrk = 0;               /* Generate SIGINT (interrupt ) */
904      brkint = 0;
905    } else {
906      ignbrk = 0;               /* Convert to 0x00 */
907      brkint = 0x08;
908    }
909  }
910
911  /* Stripping */
912  if ( t->c_iflag & ISTRIP )
913    istrip = 0x80;              /* Strip to 7 bits */
914  else
915    istrip = 0;                 /* Leave as 8 bits */
916
917  rx_period = cd2401_bitrate_divisor( 20000000Ul, &in_baud );
918  tx_period = cd2401_bitrate_divisor( 20000000Ul, &out_baud );
919
920  /*
921   *  If this is the first time that the line characteristics are set up, then
922   *  the device must be re-initialized.
923   *  Also check if we need to change anything. It is preferable to not touch
924   *  the device if nothing changes. As soon as we touch it, it tends to
925   *  glitch. If anything changes, we reprogram all registers. This is
926   *  harmless.
927   */
928  if ( ( CD2401_Channel_Info[minor].tty == 0 ) ||
929       ( cd2401->cor1 != (parodd | parenb | ignpar | csize) ) ||
930       ( cd2401->cor2 != (sw_flow_ctl | hw_flow_ctl) ) ||
931       ( cd2401->cor3 != (extra_flow_ctl | cstopb) )  ||
932       ( cd2401->cor6 != (igncr | icrnl | inlcr | ignbrk | brkint | parmrk | inpck) ) ||
933       ( cd2401->cor7 != istrip ) ||
934       ( cd2401->u1.async.schr1 != t->c_cc[VSTART] ) ||
935       ( cd2401->u1.async.schr2 != t->c_cc[VSTOP] ) ||
936       ( cd2401->rbpr != (unsigned char)rx_period ) ||
937       ( cd2401->rcor != (unsigned char)(rx_period >> 8) ) ||
938       ( cd2401->tbpr != (unsigned char)tx_period ) ||
939       ( cd2401->tcor != ( (tx_period >> 3) & 0xE0 ) ) )
940    need_reinitialization = TRUE;
941
942  /* Write to the ports */
943  rtems_interrupt_disable (level);
944
945  cd2401->car = minor;          /* Select channel */
946  read_enabled = cd2401->csr & 0x80 ? TRUE : FALSE;
947
948  if ( (t->c_cflag & CREAD ? TRUE : FALSE ) != read_enabled ) {
949    /* Read enable status is changing */
950    need_reinitialization = TRUE;
951  }
952
953  if ( need_reinitialization ) {
954    /*
955     *  Could not find a way to test whether the CD2401 was done transmitting.
956     *  The TxEmpty interrupt does not seem to indicate that the FIFO is empty
957     *  in DMA mode. So, just wait a while for output to drain. May not be
958     *  enough, but it will have to do (should be long enough for 1 char at
959     *  9600 bsp)...
960     */
961    cd2401_udelay( 2000L );
962
963    /* Clear channel */
964    cd2401_chan_cmd (minor, 0x40, 1);
965
966    cd2401->car = minor;    /* Select channel */
967    cd2401->cmr = 0x42;     /* Interrupt Rx, DMA Tx, async mode */
968    cd2401->cor1 = parodd | parenb | ignpar | csize;
969    cd2401->cor2 = sw_flow_ctl | hw_flow_ctl;
970    cd2401->cor3 = extra_flow_ctl | cstopb;
971    cd2401->cor4 = 0x0A;    /* No DSR/DCD/CTS detect; FIFO threshold of 10 */
972    cd2401->cor5 = 0x0A;    /* No DSR/DCD/CTS detect; DTR threshold of 10 */
973    cd2401->cor6 = igncr | icrnl | inlcr | ignbrk | brkint | parmrk | inpck;
974    cd2401->cor7 = istrip;  /* No LNext; ignore XON/XOFF if frame error; no tx translations */
975    /* Special char 1: XON character */
976    cd2401->u1.async.schr1 = t->c_cc[VSTART];
977    /* special char 2: XOFF character */
978    cd2401->u1.async.schr2 = t->c_cc[VSTOP];
979
980    /*
981     *  Special chars 3 and 4, char range, LNext, RFAR[1..4] and CRC
982     *  are unused, left as is.
983     */
984
985    /* Set baudrates for receiver and transmitter */
986    cd2401->rbpr = (unsigned char)rx_period;
987    cd2401->rcor = (unsigned char)(rx_period >> 8); /* no DPLL */
988    cd2401->tbpr = (unsigned char)tx_period;
989    cd2401->tcor = (tx_period >> 3) & 0xE0; /* no x1 ext clk, no loopback */
990
991    /* Timeout for 4 chars at 9600, 8 bits per char, 1 stop bit */
992    cd2401->u2.w.rtpr  = 0x04;  /* NEED TO LOOK AT THIS LINE! */
993
994    if ( t->c_cflag & CREAD ) {
995      /* Re-initialize channel, enable rx and tx */
996      cd2401_chan_cmd (minor, 0x2A, 1);
997      /* Enable rx data ints */
998      cd2401->ier = 0x08;
999    } else {
1000      /* Re-initialize channel, enable tx, disable rx */
1001      cd2401_chan_cmd (minor, 0x29, 1);
1002    }
1003  }
1004
1005  CD2401_RECORD_SET_ATTRIBUTES_INFO(( minor, need_reinitialization, csize,
1006                                      cstopb, parodd, parenb, ignpar, inpck,
1007                                      hw_flow_ctl, sw_flow_ctl, extra_flow_ctl,
1008                                      icrnl, igncr, inlcr, brkint, ignbrk,
1009                                      parmrk, istrip, tx_period, rx_period,
1010                                      out_baud, in_baud ));
1011
1012  rtems_interrupt_enable (level);
1013
1014  /*
1015   *  Looks like the CD2401 needs time to settle after initialization. Give it
1016   *  10 ms. I don't really believe it, but if output resumes to quickly after
1017   *  this call, the first few characters are not right.
1018   */
1019  if ( need_reinitialization )
1020    cd2401_udelay( 10000L );
1021
1022  /* Return something */
1023  return RTEMS_SUCCESSFUL;
1024}
1025
1026/*
1027 *  cd2401_startRemoreTx
1028 *
1029 *  Defined as a callback, but it would appear that it is never called. The
1030 *  POSIX standard states that when the tcflow() function is called with the
1031 *  TCION action, the system wall transmit a START character. Presumably,
1032 *  tcflow() is called internally when IXOFF is set in the termios c_iflag
1033 *  field when the input buffer can accomodate enough characters. It should
1034 *  probably be called from fillBufferQueue(). Clearly, the function is also
1035 *  explicitly callable by user code. The action is clearly to send the START
1036 *  character, regardless of whether START/STOP flow control is in effect.
1037 *
1038 *  Input parameters:
1039 *    minor - selected channel
1040 *
1041 *  Output parameters: NONE
1042 *
1043 *  Return value: IGNORED
1044 *
1045 *  PROPER START CHARACTER MUST BE PROGRAMMED IN SCHR1.
1046 */
1047int cd2401_startRemoteTx(
1048  int minor
1049)
1050{
1051  rtems_interrupt_level level;
1052
1053  rtems_interrupt_disable (level);
1054
1055  cd2401->car = minor;              /* Select channel */
1056  cd2401->stcr = 0x01;              /* Send SCHR1 ahead of chars in FIFO */
1057
1058  CD2401_RECORD_START_REMOTE_TX_INFO(( minor ));
1059
1060  rtems_interrupt_enable (level);
1061
1062  /* Return something */
1063  return RTEMS_SUCCESSFUL;
1064}
1065
1066/*
1067 *  cd2401_stopRemoteTx
1068 *
1069 *  Defined as a callback, but it would appear that it is never called. The
1070 *  POSIX standard states that when the tcflow() function is called with the
1071 *  TCIOFF function, the system wall transmit a STOP character. Presumably,
1072 *  tcflow() is called internally when IXOFF is set in the termios c_iflag
1073 *  field as the input buffer is about to overflow. It should probably be
1074 *  called from rtems_termios_enqueue_raw_characters(). Clearly, the function
1075 *  is also explicitly callable by user code. The action is clearly to send
1076 *  the STOP character, regardless of whether START/STOP flow control is in
1077 *  effect.
1078 *
1079 *  Input parameters:
1080 *    minor - selected channel
1081 *
1082 *  Output parameters: NONE
1083 *
1084 *  Return value: IGNORED
1085 *
1086 *  PROPER STOP CHARACTER MUST BE PROGRAMMED IN SCHR2.
1087 */
1088int cd2401_stopRemoteTx(
1089  int minor
1090)
1091{
1092  rtems_interrupt_level level;
1093
1094  rtems_interrupt_disable (level);
1095
1096  cd2401->car = minor;              /* Select channel */
1097  cd2401->stcr = 0x02;              /* Send SCHR2 ahead of chars in FIFO */
1098
1099  CD2401_RECORD_STOP_REMOTE_TX_INFO(( minor ));
1100
1101  rtems_interrupt_enable (level);
1102
1103  /* Return something */
1104  return RTEMS_SUCCESSFUL;
1105}
1106
1107/*
1108 *  cd2401_write
1109 *
1110 *  Initiate DMA output. Termios guarantees that the buffer does not wrap
1111 *  around, so we can do DMA strait from the supplied buffer.
1112 *
1113 *  Input parameters:
1114 *    minor - selected channel
1115 *    buf - output buffer
1116 *    len - number of chars to output
1117 *
1118 *  Output parameters:  NONE
1119 *
1120 *  Return value: IGNORED
1121 *
1122 *  MUST BE EXECUTED WITH THE CD2401 INTERRUPTS DISABLED!
1123 *  The processor is placed at interrupt level CD2401_INT_LEVEL explicitly in
1124 *  console_write(). The processor is necessarily at interrupt level 1 in
1125 *  cd2401_tx_isr().
1126 */
1127int cd2401_write(
1128  int minor,
1129  const char *buf,
1130  int len
1131)
1132{
1133  cd2401->car = minor;              /* Select channel */
1134
1135  if ( (cd2401->dmabsts & 0x08) == 0 ) {
1136    /* Next buffer is A. Wait for it to be ours. */
1137    while ( cd2401->atbsts & 0x01 );
1138
1139    CD2401_Channel_Info[minor].own_buf_A = FALSE;
1140    CD2401_Channel_Info[minor].len = len;
1141    CD2401_Channel_Info[minor].buf = buf;
1142    cd2401->atbadru = (uint16_t)( ( (uint32_t) buf ) >> 16 );
1143    cd2401->atbadrl = (uint16_t)( (uint32_t) buf );
1144    cd2401->atbcnt = len;
1145    CD2401_RECORD_WRITE_INFO(( len, buf, 'A' ));
1146    cd2401->atbsts = 0x03;          /* CD2401 owns buffer, int when empty */
1147  }
1148  else {
1149    /* Next buffer is B. Wait for it to be ours. */
1150    while ( cd2401->btbsts & 0x01 );
1151
1152    CD2401_Channel_Info[minor].own_buf_B = FALSE;
1153    CD2401_Channel_Info[minor].len = len;
1154    CD2401_Channel_Info[minor].buf = buf;
1155    cd2401->btbadru = (uint16_t)( ( (uint32_t) buf ) >> 16 );
1156    cd2401->btbadrl = (uint16_t)( (uint32_t) buf );
1157    cd2401->btbcnt = len;
1158    CD2401_RECORD_WRITE_INFO(( len, buf, 'B' ));
1159    cd2401->btbsts = 0x03;          /* CD2401 owns buffer, int when empty */
1160  }
1161  /* Nuts -- Need TxD ints */
1162  CD2401_Channel_Info[minor].txEmpty = FALSE;
1163  cd2401->ier |= 0x01;
1164
1165  /* Return something */
1166  return RTEMS_SUCCESSFUL;
1167}
1168
1169#if 0
1170/*
1171 *  cd2401_drainOutput
1172 *
1173 *  Wait for the txEmpty indication on the specified channel.
1174 *
1175 *  Input parameters:
1176 *    minor - selected channel
1177 *
1178 *  Output parameters:  NONE
1179 *
1180 *  Return value: IGNORED
1181 *
1182 *  MUST NOT BE EXECUTED WITH THE CD2401 INTERRUPTS DISABLED!
1183 *  The txEmpty flag is set by the tx ISR.
1184 *
1185 *  DOES NOT WORK! DO NOT ENABLE THIS CODE. THE CD2401 DOES NOT COOPERATE!
1186 *  The code is here to document that the output FIFO is NOT empty when
1187 *  the CD2401 reports that the Tx buffer is empty.
1188 */
1189int cd2401_drainOutput(
1190  int minor
1191)
1192{
1193  CD2401_RECORD_DRAIN_OUTPUT_INFO(( CD2401_Channel_Info[minor].txEmpty,
1194                                    CD2401_Channel_Info[minor].own_buf_A,
1195                                    CD2401_Channel_Info[minor].own_buf_B ));
1196
1197  while( ! (CD2401_Channel_Info[minor].txEmpty &&
1198            CD2401_Channel_Info[minor].own_buf_A &&
1199            CD2401_Channel_Info[minor].own_buf_B) );
1200
1201  /* Return something */
1202  return RTEMS_SUCCESSFUL;
1203}
1204#endif
1205
1206/*
1207 * _167Bug_pollRead
1208 *
1209 *  Read a character from the 167Bug console, and return it. Return -1
1210 *  if there is no character in the input FIFO.
1211 *
1212 *  Input parameters:
1213 *    minor - selected channel
1214 *
1215 *  Output parameters:  NONE
1216 *
1217 *  Return value: char returned as positive signed int
1218 *                -1 if no character is present in the input FIFO.
1219 *
1220 *  CANNOT BE COMBINED WITH INTERRUPT DRIVEN I/O!
1221 */
1222int _167Bug_pollRead(
1223  int minor
1224)
1225{
1226  int char_not_available;
1227  unsigned char c;
1228  rtems_interrupt_level previous_level;
1229
1230  /*
1231   *  Redirection of .INSTAT does not work: 167-Bug crashes.
1232   *  Switch the input stream to the specified port.
1233   *  Make sure this is atomic code.
1234   */
1235  rtems_interrupt_disable( previous_level );
1236
1237  asm volatile( "movew  %1, -(%%sp)\n\t"/* Channel */
1238                "trap   #15\n\t"        /* Trap to 167Bug */
1239                ".short 0x61\n\t"       /* Code for .REDIR_I */
1240                "trap   #15\n\t"        /* Trap to 167Bug */
1241                ".short 0x01\n\t"       /* Code for .INSTAT */
1242                "move   %%cc, %0\n\t"   /* Get condition codes */
1243                "andil  #4, %0"         /* Keep the Zero bit */
1244    : "=d" (char_not_available) : "d" (minor): "%%cc" );
1245
1246  if (char_not_available) {
1247    rtems_interrupt_enable( previous_level );
1248    return -1;
1249  }
1250
1251  /* Read the char and return it */
1252  asm volatile( "subq.l #2,%%a7\n\t"    /* Space for result */
1253                "trap   #15\n\t"        /* Trap to 167 Bug */
1254                ".short 0x00\n\t"       /* Code for .INCHR */
1255                "moveb  (%%a7)+, %0"    /* Pop char into c */
1256    : "=d" (c) : );
1257
1258  rtems_interrupt_enable( previous_level );
1259
1260  return (int)c;
1261}
1262
1263/*
1264 * _167Bug_pollWrite
1265 *
1266 *  Output buffer through 167Bug. Returns only once every character has been
1267 *  sent (polled output).
1268 *
1269 *  Input parameters:
1270 *    minor - selected channel
1271 *    buf - output buffer
1272 *    len - number of chars to output
1273 *
1274 *  Output parameters:  NONE
1275 *
1276 *  Return value: IGNORED
1277 *
1278 *  CANNOT BE COMBINED WITH INTERRUPT DRIVEN I/O!
1279 */
1280int _167Bug_pollWrite(
1281  int minor,
1282  const char *buf,
1283  int len
1284)
1285{
1286  const char *endbuf = buf + len;
1287
1288  asm volatile( "pea    (%0)\n\t"            /* endbuf */
1289                "pea    (%1)\n\t"            /* buf */
1290                "movew  #0x21, -(%%sp)\n\t"  /* Code for .OUTSTR */
1291                "movew  %2, -(%%sp)\n\t"     /* Channel */
1292                "trap   #15\n\t"             /* Trap to 167Bug */
1293                ".short 0x60"                /* Code for .REDIR */
1294    :: "a" (endbuf), "a" (buf), "d" (minor) );
1295
1296  /* Return something */
1297  return RTEMS_SUCCESSFUL;
1298}
1299
1300/*
1301 *  do_poll_read
1302 *
1303 *  Input characters through 167Bug. Returns has soon as a character has been
1304 *  received. Otherwise, if we wait for the number of requested characters, we
1305 *  could be here forever!
1306 *
1307 *  CR is converted to LF on input. The terminal should not send a CR/LF pair
1308 *  when the return or enter key is pressed.
1309 *
1310 *  Input parameters:
1311 *    major - ignored. Should be the major number for this driver.
1312 *    minor - selected channel.
1313 *    arg->buffer - where to put the received characters.
1314 *    arg->count  - number of characters to receive before returning--Ignored.
1315 *
1316 *  Output parameters:
1317 *    arg->bytes_moved - the number of characters read. Always 1.
1318 *
1319 *  Return value: RTEMS_SUCCESSFUL
1320 *
1321 *  CANNOT BE COMBINED WITH INTERRUPT DRIVEN I/O!
1322 */
1323rtems_status_code do_poll_read(
1324  rtems_device_major_number major,
1325  rtems_device_minor_number minor,
1326  void                    * arg
1327)
1328{
1329  rtems_libio_rw_args_t *rw_args = arg;
1330  int c;
1331
1332  while( (c = _167Bug_pollRead (minor)) == -1 );
1333  rw_args->buffer[0] = (uint8_t)c;
1334  if( rw_args->buffer[0] == '\r' )
1335      rw_args->buffer[0] = '\n';
1336  rw_args->bytes_moved = 1;
1337  return RTEMS_SUCCESSFUL;
1338}
1339
1340/*
1341 *  do_poll_write
1342 *
1343 *  Output characters through 167Bug. Returns only once every character has
1344 *  been sent.
1345 *
1346 *  CR is transmitted AFTER a LF on output.
1347 *
1348 *  Input parameters:
1349 *    major - ignored. Should be the major number for this driver.
1350 *    minor - selected channel
1351 *    arg->buffer - where to get the characters to transmit.
1352 *    arg->count  - the number of characters to transmit before returning.
1353 *
1354 *  Output parameters:
1355 *    arg->bytes_moved - the number of characters read
1356 *
1357 *  Return value: RTEMS_SUCCESSFUL
1358 *
1359 *  CANNOT BE COMBINED WITH INTERRUPT DRIVEN I/O!
1360 */
1361rtems_status_code do_poll_write(
1362  rtems_device_major_number major,
1363  rtems_device_minor_number minor,
1364  void                    * arg
1365)
1366{
1367  rtems_libio_rw_args_t *rw_args = arg;
1368  uint32_t   i;
1369  char cr ='\r';
1370
1371  for( i = 0; i < rw_args->count; i++ ) {
1372    _167Bug_pollWrite(minor, &(rw_args->buffer[i]), 1);
1373    if ( rw_args->buffer[i] == '\n' )
1374      _167Bug_pollWrite(minor, &cr, 1);
1375  }
1376  rw_args->bytes_moved = i;
1377  return RTEMS_SUCCESSFUL;
1378}
1379
1380/*
1381 *  _BSP_output_char
1382 *
1383 *  printk() function prototyped in bspIo.h. Does not use termios.
1384 */
1385void _BSP_output_char(char c)
1386{
1387  rtems_device_minor_number printk_minor;
1388  char cr ='\r';
1389
1390  /*
1391   *  Can't rely on console_initialize having been called before this function
1392   *  is used.
1393   */
1394  if ( NVRAM_CONFIGURE )
1395    /* J1-4 is on, use NVRAM info for configuration */
1396    printk_minor = (nvram->console_printk_port & 0x30) >> 4;
1397  else
1398    printk_minor = PRINTK_MINOR;
1399
1400  _167Bug_pollWrite(printk_minor, &c, 1);
1401  if ( c == '\n' )
1402      _167Bug_pollWrite(printk_minor, &cr, 1);
1403}
1404
1405/*
1406 ***************
1407 * BOILERPLATE *
1408 ***************
1409 *
1410 *  All these functions are prototyped in rtems/c/src/lib/include/console.h.
1411 */
1412
1413/*
1414 * Initialize and register the device
1415 */
1416rtems_device_driver console_initialize(
1417  rtems_device_major_number  major,
1418  rtems_device_minor_number  minor,
1419  void                      *arg
1420)
1421{
1422  rtems_status_code status;
1423  rtems_device_minor_number console_minor;
1424
1425  /*
1426   * Set up TERMIOS if needed
1427   */
1428  if ( NVRAM_CONFIGURE ) {
1429    /* J1-4 is on, use NVRAM info for configuration */
1430    console_minor = nvram->console_printk_port & 0x03;
1431
1432    if ( nvram->console_mode & 0x01 )
1433      /* termios */
1434      rtems_termios_initialize ();
1435  }
1436  else {
1437    console_minor = CONSOLE_MINOR;
1438#if CD2401_USE_TERMIOS == 1
1439    rtems_termios_initialize ();
1440#endif
1441  }
1442
1443  /*
1444   * Do device-specific initialization
1445   * Does not affect 167-Bug.
1446   */
1447  cd2401_initialize ();
1448
1449  /*
1450   * Register the devices
1451   */
1452  status = rtems_io_register_name ("/dev/tty0", major, 0);
1453  if (status != RTEMS_SUCCESSFUL)
1454    rtems_fatal_error_occurred (status);
1455
1456  status = rtems_io_register_name ("/dev/tty1", major, 1);
1457  if (status != RTEMS_SUCCESSFUL)
1458    rtems_fatal_error_occurred (status);
1459
1460  status = rtems_io_register_name ("/dev/console", major, console_minor);
1461  if (status != RTEMS_SUCCESSFUL)
1462    rtems_fatal_error_occurred (status);
1463
1464  status = rtems_io_register_name ("/dev/tty2", major, 2);
1465  if (status != RTEMS_SUCCESSFUL)
1466    rtems_fatal_error_occurred (status);
1467
1468  status = rtems_io_register_name ("/dev/tty3", major, 3);
1469  if (status != RTEMS_SUCCESSFUL)
1470    rtems_fatal_error_occurred (status);
1471
1472  return RTEMS_SUCCESSFUL;
1473}
1474
1475/*
1476 * Open the device
1477 */
1478rtems_device_driver console_open(
1479  rtems_device_major_number major,
1480  rtems_device_minor_number minor,
1481  void                    * arg
1482)
1483{
1484  static const rtems_termios_callbacks pollCallbacks = {
1485    NULL,                       /* firstOpen */
1486    NULL,                       /* lastClose */
1487    _167Bug_pollRead,           /* pollRead */
1488    _167Bug_pollWrite,          /* write */
1489    NULL,                       /* setAttributes */
1490    NULL,                       /* stopRemoteTx */
1491    NULL,                       /* startRemoteTx */
1492    0                           /* outputUsesInterrupts */
1493  };
1494
1495  static const rtems_termios_callbacks intrCallbacks = {
1496    cd2401_firstOpen,           /* firstOpen */
1497    cd2401_lastClose,           /* lastClose */
1498    NULL,                       /* pollRead */
1499    cd2401_write,               /* write */
1500    cd2401_setAttributes,       /* setAttributes */
1501    cd2401_stopRemoteTx,        /* stopRemoteTx */
1502    cd2401_startRemoteTx,       /* startRemoteTx */
1503    1                           /* outputUsesInterrupts */
1504  };
1505
1506  if ( NVRAM_CONFIGURE )
1507    /* J1-4 is on, use NVRAM info for configuration */
1508    if ( nvram->console_mode & 0x01 )
1509      /* termios */
1510      if ( nvram->console_mode & 0x02 )
1511        /* interrupt-driven I/O */
1512        return rtems_termios_open (major, minor, arg, &intrCallbacks);
1513            else
1514        /* polled I/O */
1515        return rtems_termios_open (major, minor, arg, &pollCallbacks);
1516          else
1517            /* no termios -- default to polled I/O */
1518            return RTEMS_SUCCESSFUL;
1519#if CD2401_USE_TERMIOS == 1
1520#if CD2401_IO_MODE != 1
1521  else
1522    /* termios & polled I/O*/
1523    return rtems_termios_open (major, minor, arg, &pollCallbacks);
1524#else
1525  else
1526    /* termios & interrupt-driven I/O*/
1527    return rtems_termios_open (major, minor, arg, &intrCallbacks);
1528#endif
1529#else
1530  else
1531    /* no termios -- default to polled I/O */
1532    return RTEMS_SUCCESSFUL;
1533#endif
1534}
1535
1536/*
1537 * Close the device
1538 */
1539rtems_device_driver console_close(
1540  rtems_device_major_number major,
1541  rtems_device_minor_number minor,
1542  void                    * arg
1543)
1544{
1545  if ( NVRAM_CONFIGURE ) {
1546    /* J1-4 is on, use NVRAM info for configuration */
1547    if ( nvram->console_mode & 0x01 )
1548      /* termios */
1549      return rtems_termios_close (arg);
1550    else
1551      /* no termios */
1552      return RTEMS_SUCCESSFUL;
1553  }
1554#if CD2401_USE_TERMIOS == 1
1555  else
1556    /* termios */
1557    return rtems_termios_close (arg);
1558#else
1559  else
1560    /* no termios */
1561    return RTEMS_SUCCESSFUL;
1562#endif
1563}
1564
1565/*
1566 * Read from the device
1567 */
1568rtems_device_driver console_read(
1569  rtems_device_major_number major,
1570  rtems_device_minor_number minor,
1571  void                    * arg
1572)
1573{
1574  if ( NVRAM_CONFIGURE ) {
1575    /* J1-4 is on, use NVRAM info for configuration */
1576    if ( nvram->console_mode & 0x01 )
1577      /* termios */
1578      return rtems_termios_read (arg);
1579    else
1580      /* no termios -- default to polled */
1581      return do_poll_read (major, minor, arg);
1582  }
1583#if CD2401_USE_TERMIOS == 1
1584  else
1585    /* termios */
1586    return rtems_termios_read (arg);
1587#else
1588  else
1589    /* no termios -- default to polled */
1590    return do_poll_read (major, minor, arg);
1591#endif
1592}
1593
1594/*
1595 * Write to the device
1596 */
1597rtems_device_driver console_write(
1598  rtems_device_major_number major,
1599  rtems_device_minor_number minor,
1600  void                    * arg
1601)
1602{
1603  if ( NVRAM_CONFIGURE ) {
1604    /* J1-4 is on, use NVRAM info for configuration */
1605    if ( nvram->console_mode & 0x01 )
1606      /* termios */
1607      return rtems_termios_write (arg);
1608    else
1609      /* no termios -- default to polled */
1610      return do_poll_write (major, minor, arg);
1611  }
1612#if CD2401_USE_TERMIOS == 1
1613  else
1614    /* termios */
1615    return rtems_termios_write (arg);
1616#else
1617  else
1618    /* no termios -- default to polled */
1619    return do_poll_write (major, minor, arg);
1620#endif
1621}
1622
1623/*
1624 * Handle ioctl request.
1625 */
1626rtems_device_driver console_control(
1627  rtems_device_major_number major,
1628  rtems_device_minor_number minor,
1629  void                    * arg
1630)
1631{
1632  if ( NVRAM_CONFIGURE ) {
1633    /* J1-4 is on, use NVRAM info for configuration */
1634    if ( nvram->console_mode & 0x01 )
1635      /* termios */
1636      return rtems_termios_ioctl (arg);
1637    else
1638      /* no termios -- default to polled */
1639      return RTEMS_SUCCESSFUL;
1640  }
1641#if CD2401_USE_TERMIOS == 1
1642  else
1643    /* termios */
1644    return rtems_termios_ioctl (arg);
1645#else
1646  else
1647    /* no termios -- default to polled */
1648    return RTEMS_SUCCESSFUL;
1649#endif
1650}
Note: See TracBrowser for help on using the repository browser.