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

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on Mar 20, 2014 at 9:10:47 PM

Change all references of rtems.com to rtems.org.

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