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

4.104.115
Last change on this file since 39a9f8e was 39a9f8e, checked in by Thomas Doerfler <Thomas.Doerfler@…>, on Dec 17, 2009 at 8:42:17 AM

adapted to new prototype for *_write function

  • Property mode set to 100644
File size: 53.2 KB
Line 
1/*
2 *  console.c
3 *
4 *  This file contains the MVME167 termios console package. Only asynchronous
5 *  I/O is supported.
6 *
7 *  /dev/tty0 is channel 0, Serial Port 1/Console on the MVME712M.
8 *  /dev/tty1 is channel 1, Serial Port 2/TTY01 on the MVME712M.
9 *  /dev/tty2 is channel 2, Serial Port 3 on the MVME712M.
10 *  /dev/tty3 is channel 3, Serial Port 4 on the MVME712M.
11 *
12 *  Normal I/O uses DMA for output, interrupts for input. /dev/console is
13 *  fixed to be /dev/tty01, Serial Port 2. Very limited support is provided
14 *  for polled I/O. Polled I/O is intended only for running the RTEMS test
15 *  suites. In all cases, Serial Port 1/Console is allocated to 167Bug and
16 *  is the dedicated debugger port. We configure GDB to use 167Bug for
17 *  debugging. When debugging with GDB or 167Bug, do not open /dev/tty00.
18 *
19 *  Modern I/O chips often contain a number of I/O devices that can operate
20 *  almost independently of each other. Typically, in RTEMS, all devices in
21 *  an I/O chip are handled by a single device driver, but that need not be
22 *  always the case. Each device driver must supply six entry points in the
23 *  Device Driver Table: a device initialization function, as well as an open,
24 *  close, read, write and a control function. RTEMS assigns a device major
25 *  number to each device driver. This major device number is the index of the
26 *  device driver entries in the Device Driver Table, and it used to identify
27 *  a particular device driver. To distinguish multiple I/O sub-devices within
28 *  an I/O chip, RTEMS supports device minor numbers. When a I/O device is
29 *  initialized, the major number is supplied to the initialization function.
30 *  That function must register each sub-device with a separate name and minor
31 *  number (as well as the supplied major number). When an application opens a
32 *  device by name, the corresponding major and minor numbers are returned to
33 *  the caller to be used in subsequent I/O operations (although these details
34 *  are typically hidden within the library functions).
35 *
36 *  Such a scheme recognizes that the initialization of the individual
37 *  sub-devices is generally not completely independent. For example, the
38 *  four serial ports of the CD2401 can be configured almost independently
39 *  from each other. One port could be configured to operate in asynchronous
40 *  mode with interrupt-driven I/O, while another port could be configured to
41 *  operate in HDLC mode with DMA I/O. However, a device reset command will
42 *  reset all four channels, and the width of DMA transfers and the number of
43 *  retries following bus errors selected applies to all four channels.
44 *  Consequently, when initializing one channel, one must be careful not to
45 *  destroy the configuration of other channels that are already configured.
46 *
47 *  One problem with the RTEMS I/O initialization model is that no information
48 *  other than a device major number is passed to the initialization function.
49 *  Consequently, the sub-devices must be initialized with some pre-determined
50 *  configuration. To change the configuration of a sub-device, it is
51 *  necessary to either rewrite the initialization function, or to make a
52 *  series of rtems_io_control() calls after initialization. The first
53 *  approach is not very elegant. The second approach is acceptable if an
54 *  application is simply changing baud rates, parity or other such
55 *  asynchronous parameters (as supplied by the termios package). But what if
56 *  an application requires one channel to run in HDLC or Bisync mode and
57 *  another in async mode? With a single driver per I/O chip approach, the
58 *  device driver must support multiple protocols. This is feasible, but it
59 *  often means that an application that only does asynchronous I/O now links
60 *  in code for other unused protocols, thus wasting precious ROM space.
61 *  Worse, it requires that the sub-devices be initialized in some
62 *  configuration, and that configuration then changed through a series of
63 *  device driver control calls. There is no standard API in RTEMS to switch
64 *  a serial line to some synchronous protocol.
65 *
66 *  A better approach is to treat each channel as a separate device, each with
67 *  its own device device driver. The application then supplies its own device
68 *  driver table with only the required protocols (drivers) on each line. The
69 *  problem with this approach is that the device drivers are not really
70 *  independent, given that the I/O sub-devices within a common chip are not
71 *  independent themselves. Consequently, the related device drivers must
72 *  share some information. In RTEMS, there is no standard location in which
73 *  to share information.
74 *
75 *  This driver handles all four channels, i.e. it distinguishes the
76 *  sub-devices using minor device numbers. Only asynchronous I/O is
77 *  supported. The console is currently fixed to be channel 1 on the CD2401,
78 *  which corresponds to the TTY01 port (Serial Port 2) on the MVME712M
79 *  Transition Module.
80 *
81 *  The CD2401 does either interrupt-driven or DMA I/O; it does not support
82 *  polling. In interrupt-driven or DMA I/O modes, interrupts from the CD2401
83 *  are routed to the MC68040, and the processor generates an interrupt
84 *  acknowledge cycle directly to the CD2401 to obtain an interrupt vector.
85 *  The PCCchip2 supports a pseudo-polling mode in which interrupts from the
86 *  CD2401 are not routed to the MC68040, but can be detected by the processor
87 *  by reading the appropriate CD2401 registers. In this mode, interrupt
88 *  acknowledge cycles must be generated to the CD2401 by reading the
89 *  appropriate PCCchip2 registers.
90 *
91 *  Interrupts from the four channels cannot be routed independently; either
92 *  all channels are used in the pseudo-polling mode, or all channels are used
93 *  in interrupt-driven/DMA mode. There is no advantage in using the speudo-
94 *  polling mode. Consenquently, this driver performs DMA input and output.
95 *  Output is performed directly from the termios raw output buffer, while
96 *  input is accumulated into a separate buffer.
97 *
98 *  THIS MODULE IS NOT RE-ENTRANT! Simultaneous access to a device from
99 *  multiple tasks is likely to cause significant problems! Concurrency
100 *  control is implemented in the termios package.
101 *
102 *  THE INTERRUPT LEVEL IS SET TO 1 FOR ALL CHANNELS.
103 *  If the CD2401 is to be used for high speed synchronous serial I/O, the
104 *  interrupt priority might need to be increased.
105 *
106 *  ALL INTERRUPT HANDLERS ARE SHARED.
107 *  When adding extra device drivers, either rewrite the interrupt handlers
108 *  to demultiplex the interrupts, or install separate vectors. Common vectors
109 *  are currently used to catch spurious interrupts. We could already have
110 *  installed separate vectors for each channel and used the spurious
111 *  interrupt handler defined in some other BSPs, but handling spurious
112 *  interrupts from the CD2401 in this device driver allows us to record more
113 *  information on the source of the interrupts. Furthermore, we have observed
114 *  the occasional spurious interrupt from channel 0. We definitely do not
115 *  to call a debugger for those.
116 *
117 *  All page references are to the MVME166/MVME167/MVME187 Single Board
118 *  Computer Programmer's Reference Guide (MVME187PG/D2) with the April
119 *  1993 supplements/addenda (MVME187PG/D2A1).
120 *
121 *  Copyright (c) 1998, National Research Council of Canada
122 *
123 *  The license and distribution terms for this file may be
124 *  found in the file LICENSE in this distribution or at
125 *  http://www.rtems.com/license/LICENSE.
126 */
127
128#define M167_INIT
129
130#include <stdarg.h>
131#include <stdio.h>
132#include <termios.h>
133#include <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;
164
165/* '\r' character in memory. This used to live on
166 * the stack but storing the '\r' character is
167 * optimized away by gcc-4.3.2 (since it seems to
168 * be unused [only referenced from inline assembly
169 * code in _167Bug_pollWrite()]).
170 * Hence we make it a global constant.
171 */
172static const char cr_char = '\r';
173
174/* Channel info */
175/* static */ volatile struct {
176  void *tty;                    /* Really a struct rtems_termios_tty * */
177  int len;                      /* Record nb of chars being TX'ed */
178  const char *buf;              /* Record where DMA is coming from */
179  uint32_t         spur_cnt;    /* Nb of spurious ints so far */
180  uint32_t         spur_dev;    /* Indo on last spurious int */
181  uint32_t         buserr_addr; /* Faulting address */
182  uint32_t         buserr_type; /* Reason of bus error during DMA */
183  uint8_t          own_buf_A;   /* If true, buffer A belongs to the driver */
184  uint8_t          own_buf_B;   /* If true, buffer B belongs to the driver */
185  uint8_t          txEmpty;     /* If true, the output FIFO should be empty */
186} CD2401_Channel_Info[4];
187
188/*
189 *  The number of channels already opened. If zero, enable the interrupts. The
190 *  initial value must be 0. If initialized explicitly, the variable ends up
191 *  in the .data section. Its value is not re-initialized on system restart.
192 *  Furthermore, because the variable is changed, the .data section would not
193 *  be ROMable. We thus leave the variable uninitialized, which causes it to
194 *  be allocated in the .bss section, and rely on RTEMS to zero the .bss
195 *  section on every startup.
196 */
197uint8_t         Init_count;
198
199/* Record previous handlers */
200rtems_isr_entry Prev_re_isr;        /* Previous rx exception isr */
201rtems_isr_entry Prev_rx_isr;        /* Previous rx isr */
202rtems_isr_entry Prev_tx_isr;        /* Previous tx isr */
203rtems_isr_entry Prev_modem_isr;     /* Previous modem/timer isr */
204
205/* Define the following symbol to trace the calls to this driver */
206/* #define CD2401_RECORD_DEBUG_INFO */
207#include "console-recording.h"
208
209/*
210 *  Utility functions.
211 */
212
213/*
214 *  Assumes that clock ticks 1 million times per second.
215 *
216 *  MAXIMUM DELAY IS ABOUT 20 ms
217 *
218 *  Input parameters:
219 *    delay: Number of microseconds to delay.
220 *
221 *  Output parameters: NONE
222 *
223 *  Return values: NONE
224 */
225 void cd2401_udelay
226(
227  unsigned long delay
228)
229{
230  unsigned long i = 20000;  /* In case clock is off */
231  rtems_interval ticks_per_second, start_ticks, end_ticks, current_ticks;
232
233  ticks_per_second = rtems_clock_get_ticks_per_second();
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, i, total;
539  char buffer[256];
540
541  status = cd2401->u5.b.risrl;
542  ch = cd2401->licr >> 2;
543
544  /* Has this channel been initialized or is it a condition we ignore? */
545  if ( CD2401_Channel_Info[ch].tty && !status ) {
546    /* Normal Rx Int, read chars, enqueue them, and issue EOI */
547    total = nchars = cd2401->rfoc;  /* Nb of chars to retrieve from rx FIFO */
548    i = 0;
549    while ( nchars-- > 0 ) {
550      c = (char)cd2401->dr;         /* Next char in rx FIFO */
551      rtems_termios_enqueue_raw_characters( CD2401_Channel_Info[ch].tty ,&c, 1 );
552      buffer[i++] = c;
553    }
554    cd2401->reoir = 0;              /* EOI */
555    CD2401_RECORD_RX_ISR_INFO(( ch, total, buffer ));
556  } else {
557    /* No, record as spurious interrupt */
558    CD2401_Channel_Info[ch].spur_dev =
559        (vector << 24) | (cd2401->stk << 16) | (cd2401->rir << 8) | cd2401->u5.b.risrl;
560    CD2401_Channel_Info[ch].spur_cnt++;
561    cd2401->reoir = 0x04;           /* EOI - character not read */
562    CD2401_RECORD_RX_ISR_SPURIOUS_INFO(( ch, status,
563                                         CD2401_Channel_Info[ch].spur_dev,
564                                         CD2401_Channel_Info[ch].spur_cnt ));
565  }
566}
567
568/*
569 *  cd2401_tx_isr
570 *
571 *  TX interrupt (group 2) from CD2401.
572 *
573 *  Input parameters:
574 *    vector - vector number
575 *
576 *  Output parameters: NONE
577 *
578 *  Return values: NONE
579 */
580rtems_isr cd2401_tx_isr(
581  rtems_vector_number vector
582)
583{
584  uint8_t         ch, status, buserr, initial_ier, final_ier;
585
586  status = cd2401->tisr;
587  ch = cd2401->licr >> 2;
588  initial_ier = cd2401->ier;
589
590  /* Has this channel been initialized? */
591  if ( !CD2401_Channel_Info[ch].tty ) {
592    /* No, record as spurious interrupt */
593    CD2401_Channel_Info[ch].spur_dev =
594        (vector << 24) | (cd2401->stk << 16) | (cd2401->tir << 8) | cd2401->tisr;
595    CD2401_Channel_Info[ch].spur_cnt++;
596    final_ier = cd2401->ier &= 0xFC;/* Shut up, whoever you are */
597    cd2401->teoir = 0x88;           /* EOI - Terminate buffer and no transfer */
598    CD2401_RECORD_TX_ISR_SPURIOUS_INFO(( ch, status, initial_ier, final_ier,
599                                         CD2401_Channel_Info[ch].spur_dev,
600                                         CD2401_Channel_Info[ch].spur_cnt ));
601    return;
602  }
603
604  if ( status & 0x80 ) {
605    /*
606     *  Bus error occurred during DMA transfer. For now, just record.
607     *  Get reason for DMA bus error and clear the report for the next
608     *  occurrence
609     */
610    buserr = pccchip2->SCC_error;
611    pccchip2->SCC_error = 0x01;
612    CD2401_Channel_Info[ch].buserr_type =
613         (vector << 24) | (buserr << 16) | (cd2401->tir << 8) | cd2401->tisr;
614    CD2401_Channel_Info[ch].buserr_addr =
615        (((uint32_t)cd2401->tcbadru) << 16) | cd2401->tcbadrl;
616
617    cd2401->teoir = 0x80;           /* EOI - terminate bad buffer */
618    CD2401_RECORD_TX_ISR_BUSERR_INFO(( ch, status, initial_ier, buserr,
619                                       CD2401_Channel_Info[ch].buserr_type,
620                                       CD2401_Channel_Info[ch].buserr_addr ));
621    return;
622  }
623
624  if ( status & 0x20 ) {
625    /* DMA done -- Turn off TxD int, turn on TxMpty */
626    final_ier = cd2401->ier = (cd2401->ier & 0xFE) | 0x02;
627    if( status & 0x08 ) {
628      /* Transmit buffer B was released */
629      CD2401_Channel_Info[ch].own_buf_B = TRUE;
630    }
631    else {
632      /* Transmit buffer A was released */
633      CD2401_Channel_Info[ch].own_buf_A = TRUE;
634    }
635    CD2401_RECORD_TX_ISR_INFO(( ch, status, initial_ier, final_ier,
636                                CD2401_Channel_Info[ch].txEmpty ));
637
638    /* This call can result in a call to cd2401_write() */
639    rtems_termios_dequeue_characters (
640        CD2401_Channel_Info[ch].tty,
641        CD2401_Channel_Info[ch].len );
642    cd2401->teoir = 0x08;           /* EOI - no data transfered */
643  }
644  else if ( status & 0x02 ) {
645    /* TxEmpty */
646    CD2401_Channel_Info[ch].txEmpty = TRUE;
647    final_ier = cd2401->ier &= 0xFD;/* Shut up the interrupts */
648    cd2401->teoir = 0x08;           /* EOI - no data transfered */
649    CD2401_RECORD_TX_ISR_INFO(( ch, status, initial_ier, final_ier,
650                                CD2401_Channel_Info[ch].txEmpty ));
651  }
652  else {
653    /* Why did we get a Tx interrupt? */
654    CD2401_Channel_Info[ch].spur_dev =
655        (vector << 24) | (cd2401->stk << 16) | (cd2401->tir << 8) | cd2401->tisr;
656    CD2401_Channel_Info[ch].spur_cnt++;
657    cd2401->teoir = 0x08;           /* EOI - no data transfered */
658    CD2401_RECORD_TX_ISR_SPURIOUS_INFO(( ch, status, initial_ier, 0xFF,
659                                         CD2401_Channel_Info[ch].spur_dev,
660                                         CD2401_Channel_Info[ch].spur_cnt ));
661  }
662}
663
664/*
665 *  termios callbacks
666 */
667
668/*
669 *  cd2401_firstOpen
670 *
671 *  This is the first time that this minor device (channel) is opened.
672 *  Complete the asynchronous initialization.
673 *
674 *  Input parameters:
675 *    major - device major number
676 *    minor - channel number
677 *    arg - pointer to a struct rtems_libio_open_close_args_t
678 *
679 *  Output parameters: NONE
680 *
681 *  Return value: IGNORED
682 */
683int cd2401_firstOpen(
684  int major,
685  int minor,
686  void *arg
687)
688{
689  rtems_libio_open_close_args_t *args = arg;
690  rtems_libio_ioctl_args_t newarg;
691  struct termios termios;
692  rtems_status_code sc;
693  rtems_interrupt_level level;
694
695  rtems_interrupt_disable (level);
696
697  /*
698   * Set up the line with the specified parameters. The difficulty is that
699   * the line parameters are stored in the struct termios field of a
700   * struct rtems_termios_tty that is not defined in a public header file.
701   * Therefore, we do not have direct access to the termios passed in with
702   * arg. So we make a rtems_termios_ioctl() call to get a pointer to the
703   * termios structure.
704   *
705   * THIS KLUDGE MAY BREAK IN THE FUTURE!
706   *
707   * We could have made a tcgetattr() call if we had our fd.
708   */
709  newarg.iop = args->iop;
710  newarg.command = RTEMS_IO_GET_ATTRIBUTES;
711  newarg.buffer = &termios;
712  sc = rtems_termios_ioctl (&newarg);
713  if (sc != RTEMS_SUCCESSFUL)
714    rtems_fatal_error_occurred (sc);
715
716  /*
717   *  Turn off hardware flow control. It is a pain with 3-wire cables.
718   *  The rtems_termios_ioctl() call below results in a call to
719   *  cd2401_setAttributes to initialize the line. The caller will "wait"
720   *  on the ttyMutex that it already owns; this is safe in RTEMS.
721   */
722  termios.c_cflag |= CLOCAL;    /* Ignore modem status lines */
723  newarg.command = RTEMS_IO_SET_ATTRIBUTES;
724  sc = rtems_termios_ioctl (&newarg);
725  if (sc != RTEMS_SUCCESSFUL)
726    rtems_fatal_error_occurred (sc);
727
728  /* Mark that the channel as initialized */
729  CD2401_Channel_Info[minor].tty = args->iop->data1;
730
731  /* If the first of the four channels to open, set up the interrupts */
732  if ( !Init_count++ ) {
733    /* Install the interrupt handlers */
734    Prev_re_isr    = (rtems_isr_entry) set_vector( cd2401_re_isr,    0x5C, 1 );
735    Prev_modem_isr = (rtems_isr_entry) set_vector( cd2401_modem_isr, 0x5D, 1 );
736    Prev_tx_isr    = (rtems_isr_entry) set_vector( cd2401_tx_isr,    0x5E, 1 );
737    Prev_rx_isr    = (rtems_isr_entry) set_vector( cd2401_rx_isr,    0x5F, 1 );
738
739    cd2401_interrupts_initialize( TRUE );
740  }
741
742  CD2401_RECORD_FIRST_OPEN_INFO(( minor, Init_count ));
743
744  rtems_interrupt_enable (level);
745
746  /* Return something */
747  return RTEMS_SUCCESSFUL;
748}
749
750/*
751 * cd2401_lastClose
752 *
753 *  There are no more opened file descriptors to this device. Close it down.
754 *
755 *  Input parameters:
756 *    major - device major number
757 *    minor - channel number
758 *    arg - pointer to a struct rtems_libio_open_close_args_t
759 */
760int cd2401_lastClose(
761  int major,
762  int minor,
763  void *arg
764)
765{
766  rtems_interrupt_level level;
767
768  rtems_interrupt_disable (level);
769
770  /* Mark that the channel is no longer is use */
771  CD2401_Channel_Info[minor].tty = NULL;
772
773  /* If the last of the four channels to close, disable the interrupts */
774  if ( !--Init_count ) {
775    cd2401_interrupts_initialize( FALSE );
776
777    /* De-install the interrupt handlers */
778    set_vector( Prev_re_isr,    0x5C, 1 );
779    set_vector( Prev_modem_isr, 0x5D, 1 );
780    set_vector( Prev_tx_isr,    0x5E, 1 );
781    set_vector( Prev_rx_isr,    0x5F, 1 );
782  }
783
784  CD2401_RECORD_LAST_CLOSE_INFO(( minor, Init_count ));
785
786  rtems_interrupt_enable (level);
787
788  /* return something */
789  return RTEMS_SUCCESSFUL;
790}
791
792/*
793 *  cd2401_setAttributes
794 *
795 *  Set up the selected channel of the CD2401 chip for doing asynchronous
796 *  I/O with DMA.
797 *
798 *  The chip must already have been initialized by cd2401_initialize().
799 *
800 *  This code was written for clarity. The code space it occupies could be
801 *  reduced. The code could also be compiled with aggressive optimization
802 *  turned on.
803 *
804 *  Input parameters:
805 *    minor - the selected channel
806 *    t - the termios parameters
807 *
808 *  Output parameters: NONE
809 *
810 *  Return value: IGNORED
811 */
812int cd2401_setAttributes(
813  int minor,
814  const struct termios *t
815)
816{
817  uint8_t         csize, cstopb, parodd, parenb, ignpar, inpck;
818  uint8_t         hw_flow_ctl, sw_flow_ctl, extra_flow_ctl;
819  uint8_t         icrnl, igncr, inlcr, brkint, ignbrk, parmrk, istrip;
820  uint8_t         need_reinitialization = FALSE;
821  uint8_t         read_enabled;
822  uint16_t         tx_period, rx_period;
823  uint32_t         out_baud, in_baud;
824  rtems_interrupt_level level;
825
826  /* Determine what the line parameters should be */
827
828  /* baud rates */
829  out_baud = rtems_termios_baud_to_number(t->c_cflag & CBAUD);
830  in_baud  = rtems_termios_baud_to_number(t->c_cflag & CBAUD);
831
832  /* Number of bits per char */
833  csize = 0x07; /* to avoid a warning */
834  switch ( t->c_cflag & CSIZE ) {
835    case CS5:     csize = 0x04;       break;
836    case CS6:     csize = 0x05;       break;
837    case CS7:     csize = 0x06;       break;
838    case CS8:     csize = 0x07;       break;
839  }
840
841  /* Parity */
842  if ( t->c_cflag & PARODD )
843    parodd = 0x80;              /* Odd parity */
844  else
845    parodd = 0;
846
847  if ( t->c_cflag & PARENB )
848    parenb = 0x40;              /* Parity enabled on Tx and Rx */
849  else
850    parenb = 0x00;              /* No parity on Tx and Rx */
851
852  /* CD2401 IGNPAR and INPCK bits are inverted wrt POSIX standard? */
853  if ( t->c_iflag & INPCK )
854    ignpar = 0;                 /* Check parity on input */
855  else
856    ignpar = 0x10;              /* Do not check parity on input */
857  if ( t->c_iflag & IGNPAR ) {
858    inpck = 0x03;               /* Discard error character */
859    parmrk = 0;
860  } else {
861    if ( t->c_iflag & PARMRK ) {
862      inpck = 0x01;             /* Translate to 0xFF 0x00 <char> */
863      parmrk = 0x04;
864    } else {
865      inpck = 0x01;             /* Translate to 0x00 */
866      parmrk = 0;
867    }
868  }
869
870  /* Stop bits */
871  if ( t->c_cflag & CSTOPB )
872    cstopb = 0x04;              /* Two stop bits */
873  else
874    cstopb = 0x02;              /* One stop bit */
875
876  /* Modem flow control */
877  if ( t->c_cflag & CLOCAL )
878    hw_flow_ctl = 0x04;         /* Always assert RTS before Tx */
879  else
880    hw_flow_ctl = 0x07;         /* Always assert RTS before Tx,
881                                   wait for CTS and DSR */
882
883  /* XON/XOFF Tx flow control */
884  if ( t->c_iflag & IXON ) {
885    sw_flow_ctl = 0x40;         /* Tx in-band flow ctl enabled, wait for XON */
886    extra_flow_ctl = 0x30;      /* Eat XON/XOFF, XON/XOFF in SCHR1, SCHR2 */
887  }
888  else {
889    sw_flow_ctl = 0;            /* Tx in-band flow ctl disabled */
890    extra_flow_ctl = 0;         /* Pass on XON/XOFF */
891  }
892
893  /* CL/LF translation */
894  if ( t->c_iflag & ICRNL )
895    icrnl = 0x40;               /* Map CR to NL on input */
896  else
897    icrnl = 0;                  /* Pass on CR */
898  if ( t->c_iflag & INLCR )
899    inlcr = 0x20;               /* Map NL to CR on input */
900  else
901    inlcr = 0;                  /* Pass on NL */
902  if ( t->c_iflag & IGNCR )
903    igncr = 0x80;               /* CR discarded on input */
904  else
905    igncr = 0;
906
907  /* Break handling */
908  if ( t->c_iflag & IGNBRK ) {
909    ignbrk = 0x10;              /* Ignore break on input */
910    brkint = 0x08;
911  } else {
912    if ( t->c_iflag & BRKINT ) {
913      ignbrk = 0;               /* Generate SIGINT (interrupt ) */
914      brkint = 0;
915    } else {
916      ignbrk = 0;               /* Convert to 0x00 */
917      brkint = 0x08;
918    }
919  }
920
921  /* Stripping */
922  if ( t->c_iflag & ISTRIP )
923    istrip = 0x80;              /* Strip to 7 bits */
924  else
925    istrip = 0;                 /* Leave as 8 bits */
926
927  rx_period = cd2401_bitrate_divisor( 20000000Ul, &in_baud );
928  tx_period = cd2401_bitrate_divisor( 20000000Ul, &out_baud );
929
930  /*
931   *  If this is the first time that the line characteristics are set up, then
932   *  the device must be re-initialized.
933   *  Also check if we need to change anything. It is preferable to not touch
934   *  the device if nothing changes. As soon as we touch it, it tends to
935   *  glitch. If anything changes, we reprogram all registers. This is
936   *  harmless.
937   */
938  if ( ( CD2401_Channel_Info[minor].tty == 0 ) ||
939       ( cd2401->cor1 != (parodd | parenb | ignpar | csize) ) ||
940       ( cd2401->cor2 != (sw_flow_ctl | hw_flow_ctl) ) ||
941       ( cd2401->cor3 != (extra_flow_ctl | cstopb) )  ||
942       ( cd2401->cor6 != (igncr | icrnl | inlcr | ignbrk | brkint | parmrk | inpck) ) ||
943       ( cd2401->cor7 != istrip ) ||
944       ( cd2401->u1.async.schr1 != t->c_cc[VSTART] ) ||
945       ( cd2401->u1.async.schr2 != t->c_cc[VSTOP] ) ||
946       ( cd2401->rbpr != (unsigned char)rx_period ) ||
947       ( cd2401->rcor != (unsigned char)(rx_period >> 8) ) ||
948       ( cd2401->tbpr != (unsigned char)tx_period ) ||
949       ( cd2401->tcor != ( (tx_period >> 3) & 0xE0 ) ) )
950    need_reinitialization = TRUE;
951
952  /* Write to the ports */
953  rtems_interrupt_disable (level);
954
955  cd2401->car = minor;          /* Select channel */
956  read_enabled = cd2401->csr & 0x80 ? TRUE : FALSE;
957
958  if ( (t->c_cflag & CREAD ? TRUE : FALSE ) != read_enabled ) {
959    /* Read enable status is changing */
960    need_reinitialization = TRUE;
961  }
962
963  if ( need_reinitialization ) {
964    /*
965     *  Could not find a way to test whether the CD2401 was done transmitting.
966     *  The TxEmpty interrupt does not seem to indicate that the FIFO is empty
967     *  in DMA mode. So, just wait a while for output to drain. May not be
968     *  enough, but it will have to do (should be long enough for 1 char at
969     *  9600 bsp)...
970     */
971    cd2401_udelay( 2000L );
972
973    /* Clear channel */
974    cd2401_chan_cmd (minor, 0x40, 1);
975
976    cd2401->car = minor;    /* Select channel */
977    cd2401->cmr = 0x42;     /* Interrupt Rx, DMA Tx, async mode */
978    cd2401->cor1 = parodd | parenb | ignpar | csize;
979    cd2401->cor2 = sw_flow_ctl | hw_flow_ctl;
980    cd2401->cor3 = extra_flow_ctl | cstopb;
981    cd2401->cor4 = 0x0A;    /* No DSR/DCD/CTS detect; FIFO threshold of 10 */
982    cd2401->cor5 = 0x0A;    /* No DSR/DCD/CTS detect; DTR threshold of 10 */
983    cd2401->cor6 = igncr | icrnl | inlcr | ignbrk | brkint | parmrk | inpck;
984    cd2401->cor7 = istrip;  /* No LNext; ignore XON/XOFF if frame error; no tx translations */
985    /* Special char 1: XON character */
986    cd2401->u1.async.schr1 = t->c_cc[VSTART];
987    /* special char 2: XOFF character */
988    cd2401->u1.async.schr2 = t->c_cc[VSTOP];
989
990    /*
991     *  Special chars 3 and 4, char range, LNext, RFAR[1..4] and CRC
992     *  are unused, left as is.
993     */
994
995    /* Set baudrates for receiver and transmitter */
996    cd2401->rbpr = (unsigned char)rx_period;
997    cd2401->rcor = (unsigned char)(rx_period >> 8); /* no DPLL */
998    cd2401->tbpr = (unsigned char)tx_period;
999    cd2401->tcor = (tx_period >> 3) & 0xE0; /* no x1 ext clk, no loopback */
1000
1001    /* Timeout for 4 chars at 9600, 8 bits per char, 1 stop bit */
1002    cd2401->u2.w.rtpr  = 0x04;  /* NEED TO LOOK AT THIS LINE! */
1003
1004    if ( t->c_cflag & CREAD ) {
1005      /* Re-initialize channel, enable rx and tx */
1006      cd2401_chan_cmd (minor, 0x2A, 1);
1007      /* Enable rx data ints */
1008      cd2401->ier = 0x08;
1009    } else {
1010      /* Re-initialize channel, enable tx, disable rx */
1011      cd2401_chan_cmd (minor, 0x29, 1);
1012    }
1013  }
1014
1015  CD2401_RECORD_SET_ATTRIBUTES_INFO(( minor, need_reinitialization, csize,
1016                                      cstopb, parodd, parenb, ignpar, inpck,
1017                                      hw_flow_ctl, sw_flow_ctl, extra_flow_ctl,
1018                                      icrnl, igncr, inlcr, brkint, ignbrk,
1019                                      parmrk, istrip, tx_period, rx_period,
1020                                      out_baud, in_baud ));
1021
1022  rtems_interrupt_enable (level);
1023
1024  /*
1025   *  Looks like the CD2401 needs time to settle after initialization. Give it
1026   *  10 ms. I don't really believe it, but if output resumes to quickly after
1027   *  this call, the first few characters are not right.
1028   */
1029  if ( need_reinitialization )
1030    cd2401_udelay( 10000L );
1031
1032  /* Return something */
1033  return RTEMS_SUCCESSFUL;
1034}
1035
1036/*
1037 *  cd2401_startRemoreTx
1038 *
1039 *  Defined as a callback, but it would appear that it is never called. The
1040 *  POSIX standard states that when the tcflow() function is called with the
1041 *  TCION action, the system wall transmit a START character. Presumably,
1042 *  tcflow() is called internally when IXOFF is set in the termios c_iflag
1043 *  field when the input buffer can accomodate enough characters. It should
1044 *  probably be called from fillBufferQueue(). Clearly, the function is also
1045 *  explicitly callable by user code. The action is clearly to send the START
1046 *  character, regardless of whether START/STOP flow control is in effect.
1047 *
1048 *  Input parameters:
1049 *    minor - selected channel
1050 *
1051 *  Output parameters: NONE
1052 *
1053 *  Return value: IGNORED
1054 *
1055 *  PROPER START CHARACTER MUST BE PROGRAMMED IN SCHR1.
1056 */
1057int cd2401_startRemoteTx(
1058  int minor
1059)
1060{
1061  rtems_interrupt_level level;
1062
1063  rtems_interrupt_disable (level);
1064
1065  cd2401->car = minor;              /* Select channel */
1066  cd2401->stcr = 0x01;              /* Send SCHR1 ahead of chars in FIFO */
1067
1068  CD2401_RECORD_START_REMOTE_TX_INFO(( minor ));
1069
1070  rtems_interrupt_enable (level);
1071
1072  /* Return something */
1073  return RTEMS_SUCCESSFUL;
1074}
1075
1076/*
1077 *  cd2401_stopRemoteTx
1078 *
1079 *  Defined as a callback, but it would appear that it is never called. The
1080 *  POSIX standard states that when the tcflow() function is called with the
1081 *  TCIOFF function, the system wall transmit a STOP character. Presumably,
1082 *  tcflow() is called internally when IXOFF is set in the termios c_iflag
1083 *  field as the input buffer is about to overflow. It should probably be
1084 *  called from rtems_termios_enqueue_raw_characters(). Clearly, the function
1085 *  is also explicitly callable by user code. The action is clearly to send
1086 *  the STOP character, regardless of whether START/STOP flow control is in
1087 *  effect.
1088 *
1089 *  Input parameters:
1090 *    minor - selected channel
1091 *
1092 *  Output parameters: NONE
1093 *
1094 *  Return value: IGNORED
1095 *
1096 *  PROPER STOP CHARACTER MUST BE PROGRAMMED IN SCHR2.
1097 */
1098int cd2401_stopRemoteTx(
1099  int minor
1100)
1101{
1102  rtems_interrupt_level level;
1103
1104  rtems_interrupt_disable (level);
1105
1106  cd2401->car = minor;              /* Select channel */
1107  cd2401->stcr = 0x02;              /* Send SCHR2 ahead of chars in FIFO */
1108
1109  CD2401_RECORD_STOP_REMOTE_TX_INFO(( minor ));
1110
1111  rtems_interrupt_enable (level);
1112
1113  /* Return something */
1114  return RTEMS_SUCCESSFUL;
1115}
1116
1117/*
1118 *  cd2401_write
1119 *
1120 *  Initiate DMA output. Termios guarantees that the buffer does not wrap
1121 *  around, so we can do DMA strait from the supplied buffer.
1122 *
1123 *  Input parameters:
1124 *    minor - selected channel
1125 *    buf - output buffer
1126 *    len - number of chars to output
1127 *
1128 *  Output parameters:  NONE
1129 *
1130 *  Return value: IGNORED
1131 *
1132 *  MUST BE EXECUTED WITH THE CD2401 INTERRUPTS DISABLED!
1133 *  The processor is placed at interrupt level CD2401_INT_LEVEL explicitly in
1134 *  console_write(). The processor is necessarily at interrupt level 1 in
1135 *  cd2401_tx_isr().
1136 */
1137ssize_t cd2401_write(
1138  int minor,
1139  const char *buf,
1140  size_t len
1141)
1142{
1143  cd2401->car = minor;              /* Select channel */
1144
1145  if ( (cd2401->dmabsts & 0x08) == 0 ) {
1146    /* Next buffer is A. Wait for it to be ours. */
1147    while ( cd2401->atbsts & 0x01 );
1148
1149    CD2401_Channel_Info[minor].own_buf_A = FALSE;
1150    CD2401_Channel_Info[minor].len = len;
1151    CD2401_Channel_Info[minor].buf = buf;
1152    cd2401->atbadru = (uint16_t)( ( (uint32_t) buf ) >> 16 );
1153    cd2401->atbadrl = (uint16_t)( (uint32_t) buf );
1154    cd2401->atbcnt = len;
1155    CD2401_RECORD_WRITE_INFO(( len, buf, 'A' ));
1156    cd2401->atbsts = 0x03;          /* CD2401 owns buffer, int when empty */
1157  }
1158  else {
1159    /* Next buffer is B. Wait for it to be ours. */
1160    while ( cd2401->btbsts & 0x01 );
1161
1162    CD2401_Channel_Info[minor].own_buf_B = FALSE;
1163    CD2401_Channel_Info[minor].len = len;
1164    CD2401_Channel_Info[minor].buf = buf;
1165    cd2401->btbadru = (uint16_t)( ( (uint32_t) buf ) >> 16 );
1166    cd2401->btbadrl = (uint16_t)( (uint32_t) buf );
1167    cd2401->btbcnt = len;
1168    CD2401_RECORD_WRITE_INFO(( len, buf, 'B' ));
1169    cd2401->btbsts = 0x03;          /* CD2401 owns buffer, int when empty */
1170  }
1171  /* Nuts -- Need TxD ints */
1172  CD2401_Channel_Info[minor].txEmpty = FALSE;
1173  cd2401->ier |= 0x01;
1174
1175  /* Return something */
1176  return len;
1177}
1178
1179#if 0
1180/*
1181 *  cd2401_drainOutput
1182 *
1183 *  Wait for the txEmpty indication on the specified channel.
1184 *
1185 *  Input parameters:
1186 *    minor - selected channel
1187 *
1188 *  Output parameters:  NONE
1189 *
1190 *  Return value: IGNORED
1191 *
1192 *  MUST NOT BE EXECUTED WITH THE CD2401 INTERRUPTS DISABLED!
1193 *  The txEmpty flag is set by the tx ISR.
1194 *
1195 *  DOES NOT WORK! DO NOT ENABLE THIS CODE. THE CD2401 DOES NOT COOPERATE!
1196 *  The code is here to document that the output FIFO is NOT empty when
1197 *  the CD2401 reports that the Tx buffer is empty.
1198 */
1199int cd2401_drainOutput(
1200  int minor
1201)
1202{
1203  CD2401_RECORD_DRAIN_OUTPUT_INFO(( CD2401_Channel_Info[minor].txEmpty,
1204                                    CD2401_Channel_Info[minor].own_buf_A,
1205                                    CD2401_Channel_Info[minor].own_buf_B ));
1206
1207  while( ! (CD2401_Channel_Info[minor].txEmpty &&
1208            CD2401_Channel_Info[minor].own_buf_A &&
1209            CD2401_Channel_Info[minor].own_buf_B) );
1210
1211  /* Return something */
1212  return RTEMS_SUCCESSFUL;
1213}
1214#endif
1215
1216/*
1217 * _167Bug_pollRead
1218 *
1219 *  Read a character from the 167Bug console, and return it. Return -1
1220 *  if there is no character in the input FIFO.
1221 *
1222 *  Input parameters:
1223 *    minor - selected channel
1224 *
1225 *  Output parameters:  NONE
1226 *
1227 *  Return value: char returned as positive signed int
1228 *                -1 if no character is present in the input FIFO.
1229 *
1230 *  CANNOT BE COMBINED WITH INTERRUPT DRIVEN I/O!
1231 */
1232int _167Bug_pollRead(
1233  int minor
1234)
1235{
1236  int char_not_available;
1237  unsigned char c;
1238  rtems_interrupt_level previous_level;
1239
1240  /*
1241   *  Redirection of .INSTAT does not work: 167-Bug crashes.
1242   *  Switch the input stream to the specified port.
1243   *  Make sure this is atomic code.
1244   */
1245  rtems_interrupt_disable( previous_level );
1246
1247  asm volatile( "movew  %1, -(%%sp)\n\t"/* Channel */
1248                "trap   #15\n\t"        /* Trap to 167Bug */
1249                ".short 0x61\n\t"       /* Code for .REDIR_I */
1250                "trap   #15\n\t"        /* Trap to 167Bug */
1251                ".short 0x01\n\t"       /* Code for .INSTAT */
1252                "move   %%cc, %0\n\t"   /* Get condition codes */
1253                "andil  #4, %0"         /* Keep the Zero bit */
1254    : "=d" (char_not_available) : "d" (minor): "%%cc" );
1255
1256  if (char_not_available) {
1257    rtems_interrupt_enable( previous_level );
1258    return -1;
1259  }
1260
1261  /* Read the char and return it */
1262  asm volatile( "subq.l #2,%%a7\n\t"    /* Space for result */
1263                "trap   #15\n\t"        /* Trap to 167 Bug */
1264                ".short 0x00\n\t"       /* Code for .INCHR */
1265                "moveb  (%%a7)+, %0"    /* Pop char into c */
1266    : "=d" (c) : );
1267
1268  rtems_interrupt_enable( previous_level );
1269
1270  return (int)c;
1271}
1272
1273/*
1274 * _167Bug_pollWrite
1275 *
1276 *  Output buffer through 167Bug. Returns only once every character has been
1277 *  sent (polled output).
1278 *
1279 *  Input parameters:
1280 *    minor - selected channel
1281 *    buf - output buffer
1282 *    len - number of chars to output
1283 *
1284 *  Output parameters:  NONE
1285 *
1286 *  Return value: IGNORED
1287 *
1288 *  CANNOT BE COMBINED WITH INTERRUPT DRIVEN I/O!
1289 */
1290ssize_t _167Bug_pollWrite(
1291  int minor,
1292  const char *buf,
1293  size_t len
1294)
1295{
1296  const char *endbuf = buf + len;
1297
1298  asm volatile( "pea    (%0)\n\t"            /* endbuf */
1299                "pea    (%1)\n\t"            /* buf */
1300                "movew  #0x21, -(%%sp)\n\t"  /* Code for .OUTSTR */
1301                "movew  %2, -(%%sp)\n\t"     /* Channel */
1302                "trap   #15\n\t"             /* Trap to 167Bug */
1303                ".short 0x60"                /* Code for .REDIR */
1304    :: "a" (endbuf), "a" (buf), "d" (minor) );
1305
1306  /* Return something */
1307  return len;
1308}
1309
1310/*
1311 *  do_poll_read
1312 *
1313 *  Input characters through 167Bug. Returns has soon as a character has been
1314 *  received. Otherwise, if we wait for the number of requested characters, we
1315 *  could be here forever!
1316 *
1317 *  CR is converted to LF on input. The terminal should not send a CR/LF pair
1318 *  when the return or enter key is pressed.
1319 *
1320 *  Input parameters:
1321 *    major - ignored. Should be the major number for this driver.
1322 *    minor - selected channel.
1323 *    arg->buffer - where to put the received characters.
1324 *    arg->count  - number of characters to receive before returning--Ignored.
1325 *
1326 *  Output parameters:
1327 *    arg->bytes_moved - the number of characters read. Always 1.
1328 *
1329 *  Return value: RTEMS_SUCCESSFUL
1330 *
1331 *  CANNOT BE COMBINED WITH INTERRUPT DRIVEN I/O!
1332 */
1333rtems_status_code do_poll_read(
1334  rtems_device_major_number major,
1335  rtems_device_minor_number minor,
1336  void                    * arg
1337)
1338{
1339  rtems_libio_rw_args_t *rw_args = arg;
1340  int c;
1341
1342  while( (c = _167Bug_pollRead (minor)) == -1 );
1343  rw_args->buffer[0] = (uint8_t)c;
1344  if( rw_args->buffer[0] == '\r' )
1345      rw_args->buffer[0] = '\n';
1346  rw_args->bytes_moved = 1;
1347  return RTEMS_SUCCESSFUL;
1348}
1349
1350/*
1351 *  do_poll_write
1352 *
1353 *  Output characters through 167Bug. Returns only once every character has
1354 *  been sent.
1355 *
1356 *  CR is transmitted AFTER a LF on output.
1357 *
1358 *  Input parameters:
1359 *    major - ignored. Should be the major number for this driver.
1360 *    minor - selected channel
1361 *    arg->buffer - where to get the characters to transmit.
1362 *    arg->count  - the number of characters to transmit before returning.
1363 *
1364 *  Output parameters:
1365 *    arg->bytes_moved - the number of characters read
1366 *
1367 *  Return value: RTEMS_SUCCESSFUL
1368 *
1369 *  CANNOT BE COMBINED WITH INTERRUPT DRIVEN I/O!
1370 */
1371rtems_status_code do_poll_write(
1372  rtems_device_major_number major,
1373  rtems_device_minor_number minor,
1374  void                    * arg
1375)
1376{
1377  rtems_libio_rw_args_t *rw_args = arg;
1378  uint32_t   i;
1379
1380  for( i = 0; i < rw_args->count; i++ ) {
1381    _167Bug_pollWrite(minor, &(rw_args->buffer[i]), 1);
1382    if ( rw_args->buffer[i] == '\n' )
1383      _167Bug_pollWrite(minor, &cr_char, 1);
1384  }
1385  rw_args->bytes_moved = i;
1386  return RTEMS_SUCCESSFUL;
1387}
1388
1389/*
1390 *  _BSP_output_char
1391 *
1392 *  printk() function prototyped in bspIo.h. Does not use termios.
1393 */
1394void _BSP_output_char(char c)
1395{
1396  rtems_device_minor_number printk_minor;
1397
1398  /*
1399   *  Can't rely on console_initialize having been called before this function
1400   *  is used.
1401   */
1402  if ( NVRAM_CONFIGURE )
1403    /* J1-4 is on, use NVRAM info for configuration */
1404    printk_minor = (nvram->console_printk_port & 0x30) >> 4;
1405  else
1406    printk_minor = PRINTK_MINOR;
1407
1408  _167Bug_pollWrite(printk_minor, &c, 1);
1409  if ( c == '\n' )
1410      _167Bug_pollWrite(printk_minor, &cr_char, 1);
1411}
1412
1413/*
1414 ***************
1415 * BOILERPLATE *
1416 ***************
1417 *
1418 *  All these functions are prototyped in rtems/c/src/lib/include/console.h.
1419 */
1420
1421/*
1422 * Initialize and register the device
1423 */
1424rtems_device_driver console_initialize(
1425  rtems_device_major_number  major,
1426  rtems_device_minor_number  minor,
1427  void                      *arg
1428)
1429{
1430  rtems_status_code status;
1431  rtems_device_minor_number console_minor;
1432
1433  /*
1434   * Set up TERMIOS if needed
1435   */
1436  if ( NVRAM_CONFIGURE ) {
1437    /* J1-4 is on, use NVRAM info for configuration */
1438    console_minor = nvram->console_printk_port & 0x03;
1439
1440    if ( nvram->console_mode & 0x01 )
1441      /* termios */
1442      rtems_termios_initialize ();
1443  }
1444  else {
1445    console_minor = CONSOLE_MINOR;
1446#if CD2401_USE_TERMIOS == 1
1447    rtems_termios_initialize ();
1448#endif
1449  }
1450
1451  /*
1452   * Do device-specific initialization
1453   * Does not affect 167-Bug.
1454   */
1455  cd2401_initialize ();
1456
1457  /*
1458   * Register the devices
1459   */
1460  status = rtems_io_register_name ("/dev/tty0", major, 0);
1461  if (status != RTEMS_SUCCESSFUL)
1462    rtems_fatal_error_occurred (status);
1463
1464  status = rtems_io_register_name ("/dev/tty1", major, 1);
1465  if (status != RTEMS_SUCCESSFUL)
1466    rtems_fatal_error_occurred (status);
1467
1468  status = rtems_io_register_name ("/dev/console", major, console_minor);
1469  if (status != RTEMS_SUCCESSFUL)
1470    rtems_fatal_error_occurred (status);
1471
1472  status = rtems_io_register_name ("/dev/tty2", major, 2);
1473  if (status != RTEMS_SUCCESSFUL)
1474    rtems_fatal_error_occurred (status);
1475
1476  status = rtems_io_register_name ("/dev/tty3", major, 3);
1477  if (status != RTEMS_SUCCESSFUL)
1478    rtems_fatal_error_occurred (status);
1479
1480  return RTEMS_SUCCESSFUL;
1481}
1482
1483/*
1484 * Open the device
1485 */
1486rtems_device_driver console_open(
1487  rtems_device_major_number major,
1488  rtems_device_minor_number minor,
1489  void                    * arg
1490)
1491{
1492  static const rtems_termios_callbacks pollCallbacks = {
1493    NULL,                       /* firstOpen */
1494    NULL,                       /* lastClose */
1495    _167Bug_pollRead,           /* pollRead */
1496    _167Bug_pollWrite,          /* write */
1497    NULL,                       /* setAttributes */
1498    NULL,                       /* stopRemoteTx */
1499    NULL,                       /* startRemoteTx */
1500    0                           /* outputUsesInterrupts */
1501  };
1502
1503  static const rtems_termios_callbacks intrCallbacks = {
1504    cd2401_firstOpen,           /* firstOpen */
1505    cd2401_lastClose,           /* lastClose */
1506    NULL,                       /* pollRead */
1507    cd2401_write,               /* write */
1508    cd2401_setAttributes,       /* setAttributes */
1509    cd2401_stopRemoteTx,        /* stopRemoteTx */
1510    cd2401_startRemoteTx,       /* startRemoteTx */
1511    1                           /* outputUsesInterrupts */
1512  };
1513
1514  if ( NVRAM_CONFIGURE )
1515    /* J1-4 is on, use NVRAM info for configuration */
1516    if ( nvram->console_mode & 0x01 )
1517      /* termios */
1518      if ( nvram->console_mode & 0x02 )
1519        /* interrupt-driven I/O */
1520        return rtems_termios_open (major, minor, arg, &intrCallbacks);
1521            else
1522        /* polled I/O */
1523        return rtems_termios_open (major, minor, arg, &pollCallbacks);
1524          else
1525            /* no termios -- default to polled I/O */
1526            return RTEMS_SUCCESSFUL;
1527#if CD2401_USE_TERMIOS == 1
1528#if CD2401_IO_MODE != 1
1529  else
1530    /* termios & polled I/O*/
1531    return rtems_termios_open (major, minor, arg, &pollCallbacks);
1532#else
1533  else
1534    /* termios & interrupt-driven I/O*/
1535    return rtems_termios_open (major, minor, arg, &intrCallbacks);
1536#endif
1537#else
1538  else
1539    /* no termios -- default to polled I/O */
1540    return RTEMS_SUCCESSFUL;
1541#endif
1542}
1543
1544/*
1545 * Close the device
1546 */
1547rtems_device_driver console_close(
1548  rtems_device_major_number major,
1549  rtems_device_minor_number minor,
1550  void                    * arg
1551)
1552{
1553  if ( NVRAM_CONFIGURE ) {
1554    /* J1-4 is on, use NVRAM info for configuration */
1555    if ( nvram->console_mode & 0x01 )
1556      /* termios */
1557      return rtems_termios_close (arg);
1558    else
1559      /* no termios */
1560      return RTEMS_SUCCESSFUL;
1561  }
1562#if CD2401_USE_TERMIOS == 1
1563  else
1564    /* termios */
1565    return rtems_termios_close (arg);
1566#else
1567  else
1568    /* no termios */
1569    return RTEMS_SUCCESSFUL;
1570#endif
1571}
1572
1573/*
1574 * Read from the device
1575 */
1576rtems_device_driver console_read(
1577  rtems_device_major_number major,
1578  rtems_device_minor_number minor,
1579  void                    * arg
1580)
1581{
1582  if ( NVRAM_CONFIGURE ) {
1583    /* J1-4 is on, use NVRAM info for configuration */
1584    if ( nvram->console_mode & 0x01 )
1585      /* termios */
1586      return rtems_termios_read (arg);
1587    else
1588      /* no termios -- default to polled */
1589      return do_poll_read (major, minor, arg);
1590  }
1591#if CD2401_USE_TERMIOS == 1
1592  else
1593    /* termios */
1594    return rtems_termios_read (arg);
1595#else
1596  else
1597    /* no termios -- default to polled */
1598    return do_poll_read (major, minor, arg);
1599#endif
1600}
1601
1602/*
1603 * Write to the device
1604 */
1605rtems_device_driver console_write(
1606  rtems_device_major_number major,
1607  rtems_device_minor_number minor,
1608  void                    * arg
1609)
1610{
1611  if ( NVRAM_CONFIGURE ) {
1612    /* J1-4 is on, use NVRAM info for configuration */
1613    if ( nvram->console_mode & 0x01 )
1614      /* termios */
1615      return rtems_termios_write (arg);
1616    else
1617      /* no termios -- default to polled */
1618      return do_poll_write (major, minor, arg);
1619  }
1620#if CD2401_USE_TERMIOS == 1
1621  else
1622    /* termios */
1623    return rtems_termios_write (arg);
1624#else
1625  else
1626    /* no termios -- default to polled */
1627    return do_poll_write (major, minor, arg);
1628#endif
1629}
1630
1631/*
1632 * Handle ioctl request.
1633 */
1634rtems_device_driver console_control(
1635  rtems_device_major_number major,
1636  rtems_device_minor_number minor,
1637  void                    * arg
1638)
1639{
1640  if ( NVRAM_CONFIGURE ) {
1641    /* J1-4 is on, use NVRAM info for configuration */
1642    if ( nvram->console_mode & 0x01 )
1643      /* termios */
1644      return rtems_termios_ioctl (arg);
1645    else
1646      /* no termios -- default to polled */
1647      return RTEMS_SUCCESSFUL;
1648  }
1649#if CD2401_USE_TERMIOS == 1
1650  else
1651    /* termios */
1652    return rtems_termios_ioctl (arg);
1653#else
1654  else
1655    /* no termios -- default to polled */
1656    return RTEMS_SUCCESSFUL;
1657#endif
1658}
Note: See TracBrowser for help on using the repository browser.