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

5
Last change on this file since 9c18598d was ef559c7, checked in by Joel Sherrill <joel.sherrill@…>, on 10/15/14 at 22:44:14

m68k/mvme167: Fix warnings

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