summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/powerpc/mbx8xx/console/console.c
blob: c152d17321f8365a0fd4d5a49e93562b9e9e3e55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
 *  console.c
 *
 *  This file contains the MBX8xx termios serial I/O package.
 *  Only asynchronous I/O is supported.
 *
 *  The SCCs and SMCs are assigned as follows
 *
 *   Channel     Device      Minor   Note
 *    SMC1      /dev/tty0      0
 *    SMC2      /dev/tty1      1
 *    SCC1                     2     N/A. Hardwired as ethernet port
 *    SCC2      /dev/tty2      3
 *    SCC3      /dev/tty3      4
 *    SCC4      /dev/tty4      5
 *
 *  All ports support termios. All I/O is interrupt-driven, unless EPPCBug
 *  is used to do the I/O. To use EPPCBug, define the EPPCBUG_SMC1
 *  manifest constant in the configuration file (mbx8xx.cfg). EPPCBug I/O
 *  is currently limited to the EPPCBug debug console. This is a limitation
 *  of firmware revision 1.1. Later firmware should be able to do I/O
 *  through any port.This code assumes that the EPPCBug console is the
 *  default: SMC1.
 * 
 *  TO RUN THE TESTS, USE POLLED I/O THROUGH EPPCBUG. Some tests play with
 *  the interrupt masks and turn off I/O. Those tests will hang with when
 *  interrupt-driven I/O is used.
 *  
 *  Set CONSOLE_MINOR to the appropriate device minor number in the
 *  config file. This allows the RTEMS application console to be different
 *  from the EPPBug debug console or the GDB stup I/O port.
 * 
 *  This driver handles all five available serial ports: it distinguishes
 *  the sub-devices using minor device numbers. It is not possible to have
 *  other protocols running on the other ports when this driver is used as
 *  currently written.
 *  
 *  Based on code (alloc860.c in eth_comm port) by
 *  Jay Monkman (jmonkman@frasca.com),
 *  Copyright (C) 1998 by Frasca International, Inc.
 *
 *  Modifications by Darlene Stewart <Darlene.Stewart@iit.nrc.ca>
 *  and Charles-Antoine Gauthier <charles.gauthier@iit.nrc.ca>.
 *  Copyright (c) 1999, National Research Council of Canada
 *
 */
#include <stdarg.h>
#include <stdio.h>
#include <bsp.h>                /* Must be before libio.h */
#include <rtems/libio.h>
#include <termios.h>

static int _EPPCBug_pollRead( int minor );
static int _EPPCBug_pollWrite( int minor, const char *buf, int len );


/*
 * _EPPCBug_pollRead
 *
 *  Read a character from the EPPCBug console, and return it. Return -1
 *  if there is no character in the input FIFO.
 *
 *  Input parameters:
 *    minor - selected channel
 *
 *  Output parameters:  NONE
 *
 *  Return value: char returned as positive signed int
 *                -1 if no character is present in the input FIFO.
 */
int _EPPCBug_pollRead(
  int minor
)
{
  extern volatile m8xx_t m8xx;

  char c;
  volatile int simask;		/* We must read and write m8xx.simask */
  int retval;
  ISR_Level level;
  
  struct {
    int clun;
    int dlun;
    char * inbuf;
    int nbytes_requested;
    int reserved;
  } volatile input_params;
  
  struct {
    int status;
    union {
      struct {
        int input_char_available;
        int output_possible;
        int break_detected;
        int modem_status;
      } stat;
      struct {
        int nbytes_received;
      } read;
    } u;
  } volatile output_params;

  retval = -1;

  /* Input through EPPCBug console  */
  input_params.clun = 0;
  input_params.dlun = 0;
  input_params.reserved = 0;
  
  _ISR_Disable( level );
  simask = m8xx.simask;

  /* Check for a char in the input FIFO using .CIO_STAT */
  asm volatile( "li 10,0x202
                 mr 3, %0
                 mr 4, %1
                 sc"
    :: "g" (&input_params), "g" (&output_params) : "3", "4", "10" );

  if ( (output_params.status == 0) && output_params.u.stat.input_char_available) {
  
    /* Read the char and return it */
    input_params.inbuf = &c;
    input_params.nbytes_requested = 1;
  
    asm volatile( "li     10,0x200     /* Code for .CIO_READ */
                   mr    3, %0         /* Address of input_params */
                   mr    4, %1         /* Address of output_params */
                   sc"             /* Call EPPCBUG */ 
      :: "g" (&input_params), "g" (&output_params) : "3", "4", "10" );

    if ( (output_params.status == 0) && output_params.u.read.nbytes_received)
      retval = (int)c;
  }
  
  m8xx.simask = simask;
  _ISR_Enable( level );
  return retval;
}


/*
 * _EPPCBug_pollWrite
 *
 *  Output buffer through EPPCBug. Returns only once every character has been
 *  sent (polled output).
 *
 *  Input parameters:
 *    minor - selected channel
 *    buf - output buffer
 *    len - number of chars to output
 *
 *  Output parameters:  NONE
 *
 *  Return value: IGNORED
 */
int _EPPCBug_pollWrite(
  int minor,
  const char *buf,
  int len
)
{
  extern volatile m8xx_t m8xx;

  volatile int simask;
  int i, retval;
  ISR_Level level;
  
  struct {
    int clun;
    int dlun;
    const char * outbuf;
    int nbytes_to_output;
    int reserved;
  } volatile input_params;
  
  struct {
    int status;
    union {
      struct {
        int input_char_available;
        int output_possible;
        int break_detected;
        int modem_status;
      } stat;
      struct {
        int nbytes_sent;
      } write;
    } u;
  } volatile output_params;

  retval = -1;

  /* Output through EPPCBug console  */
  input_params.clun = 0;
  input_params.dlun = 0;
  input_params.reserved = 0;

  i = 0;

  _ISR_Disable( level );
  simask = m8xx.simask;

  while (i < len) {
    /* Wait for space in the output buffer */
    do {
      /* Check for space in the output FIFO */
      asm volatile( "li 10,0x202        /* Code for .CIO_STAT */
                     mr 3, %0           /* Address of input_params */
                     mr 4, %1           /* Address of output_params */
                     sc"            /* Call EPPCBUG */ 
        :: "g" (&input_params), "g" (&output_params) : "3", "4", "10" );

      if (output_params.status)
        goto error;
    } while (!output_params.u.stat.output_possible);

    /* Output the characters until done */
    input_params.outbuf = &buf[i];
    input_params.nbytes_to_output = len - i;
  
    asm volatile( "li 10,0x201          /* Code for .CIO_WRITE */
                   mr 3, %0             /* Address of input_params */
                   mr 4, %1             /* Address of output_params */
                   sc"          /* Call EPPCBUG */ 
      :: "g" (&input_params), "g" (&output_params) : "3", "4", "10" );

    if (output_params.status)
      goto error;

    i += output_params.u.write.nbytes_sent;
  }
  
  /* Return something */
  m8xx.simask = simask;
  _ISR_Enable( level );
  return RTEMS_SUCCESSFUL;

error:
  m8xx.simask = simask;
  _ISR_Enable( level );
  return -1;
}


/*
 *  Print functions: prototyped in bsp.h
 *  Debug printing on Channel 1
 */
 
void printk( char *fmt, ... )
{
  va_list  ap;                  /* points to each unnamed argument in turn */
  static char buf[256];
  unsigned int level;
  
  _CPU_ISR_Disable(level);
  
  va_start(ap, fmt);            /* make ap point to 1st unnamed arg */
  vsprintf(buf, fmt, ap);       /* send output to buffer */
  
  BSP_output_string(buf);       /* print buffer -- Channel 1 */
  
  va_end(ap);               /* clean up and re-enable interrupts */
  _CPU_ISR_Enable(level);
}


void BSP_output_string( char * buf )
{
  int len = strlen(buf);
  int minor;                /* will be ignored */
  rtems_status_code sc;

  sc = _EPPCBug_pollWrite(minor, buf, len);
  if (sc != RTEMS_SUCCESSFUL)
    rtems_fatal_error_occurred (sc);
}


/*
 ***************
 * BOILERPLATE *
 ***************
 *
 *  All these functions are prototyped in rtems/c/src/lib/include/console.h.
 */

/*
 * Initialize and register the device
 */
rtems_device_driver console_initialize(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  rtems_status_code status;
  
  /*
   * Set up TERMIOS
   */
  rtems_termios_initialize();

  /*
   *  Do common initialization.
   */
  m8xx_uart_initialize();
  
  /*
   * Do device-specific initialization
   */
#ifndef EPPCBUG_SMC1
  m8xx_uart_smc_initialize(SMC1_MINOR); /* /dev/tty0 */
#endif /* EPPCBUG_SMC1 */

  m8xx_uart_smc_initialize(SMC2_MINOR); /* /dev/tty1 */                             
  m8xx_uart_scc_initialize(SCC2_MINOR); /* /dev/tty2    */
                              
#ifdef mpc860
  m8xx_uart_scc_initialize(SCC3_MINOR); /* /dev/tty3    */
  m8xx_uart_scc_initialize(SCC4_MINOR); /* /dev/tty4    */
#endif /* mpc860 */

  /*
   * Set up interrupts
   */
   m8xx_uart_interrupts_initialize();

  status = rtems_io_register_name ("/dev/tty0", major, SMC1_MINOR);
  if (status != RTEMS_SUCCESSFUL)
    rtems_fatal_error_occurred (status);
    
  status = rtems_io_register_name ("/dev/tty1", major, SMC2_MINOR);
  if (status != RTEMS_SUCCESSFUL)
    rtems_fatal_error_occurred (status);
    
  status = rtems_io_register_name ("/dev/tty2", major, SCC2_MINOR);
  if (status != RTEMS_SUCCESSFUL)
    rtems_fatal_error_occurred (status);
    
#ifdef mpc860
  status = rtems_io_register_name ("/dev/tty3", major, SCC3_MINOR);
  if (status != RTEMS_SUCCESSFUL)
    rtems_fatal_error_occurred (status);
                              
  status = rtems_io_register_name ("/dev/tty4", major, SCC4_MINOR);
  if (status != RTEMS_SUCCESSFUL)
    rtems_fatal_error_occurred (status);
    
#endif /* mpc860 */
    
  /* Now register the RTEMS console */
  status = rtems_io_register_name ("/dev/console", major, CONSOLE_MINOR);
  if (status != RTEMS_SUCCESSFUL)
    rtems_fatal_error_occurred (status);
    
  return RTEMS_SUCCESSFUL;
}


/*
 * Open the device
 */
rtems_device_driver console_open(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  /* Used to track termios private data for callbacks */
  extern struct rtems_termios_tty *ttyp[];

  volatile m8xxSCCRegisters_t *sccregs;
  rtems_status_code sc;
  rtems_libio_open_close_args_t *args = arg;
  
#ifdef EPPCBUG_SMC1
  static const rtems_termios_callbacks sccEPPCBugCallbacks = {
    NULL,                       	/* firstOpen */
    NULL,                       	/* lastClose */
    _EPPCBug_pollRead,          	/* pollRead */
    _EPPCBug_pollWrite,         	/* write */
    NULL,                       	/* stopRemoteTx */
    NULL,                       	/* startRemoteTx */
    0                           	/* outputUsesInterrupts */
  };  
#endif

#ifdef UARTS_USE_INTERRUPTS  
  static const rtems_termios_callbacks intrCallbacks = {
    NULL,                       	/* firstOpen */
    NULL,                       	/* lastClose */
    NULL,                               /* pollRead */
    m8xx_uart_write,       	        /* write */
    m8xx_uart_setAttributes,    	/* setAttributes */
    NULL,                       	/* stopRemoteTx */
    NULL,                       	/* startRemoteTx */
    1                           	/* outputUsesInterrupts */
  };
#else
  static const rtems_termios_callbacks pollCallbacks = {
    NULL,                       	/* firstOpen */
    NULL,                       	/* lastClose */
    m8xx_uart_pollRead,        	        /* pollRead */
    m8xx_uart_pollWrite,       	        /* write */
    m8xx_uart_setAttributes,      	/* setAttributes */
    NULL,                       	/* stopRemoteTx */
    NULL,                       	/* startRemoteTx */
    0                           	/* outputUsesInterrupts */
  };
#endif

  if ( (minor < SMC1_MINOR) || (minor > NUM_PORTS-1) )
    return RTEMS_INVALID_NUMBER;
    
#ifdef EPPCBUG_SMC1
  if (minor == SMC1_MINOR)
    return rtems_termios_open (major, minor, arg, &sccEPPCBugCallbacks);
#endif /* EPPCBUG_SMC1 */

#ifdef UARTS_USE_INTERRUPTS  
  sc = rtems_termios_open (major, minor, arg, &intrCallbacks);
  ttyp[minor] = args->iop->data1;        /* Keep cookie returned by termios_open */
#else
  sc = rtems_termios_open (major, minor, arg, &pollCallbacks);
#endif
  return sc;
}


/*
 * Close the device
 */
rtems_device_driver console_close(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  return rtems_termios_close (arg);
}


/*
 * Read from the device
 */
rtems_device_driver console_read(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  return rtems_termios_read(arg);
}


/*
 * Write to the device
 */
rtems_device_driver console_write(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{
  return rtems_termios_write(arg);
}


/*
 * Handle ioctl request.
 */
rtems_device_driver console_control(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void *arg
)
{ 
  return rtems_termios_ioctl (arg);
}