OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rtems-20020807/] [c/] [src/] [lib/] [libbsp/] [m68k/] [mvme167/] [console/] [console.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.