summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/m68k/efi68k/console/console.c
blob: 2efbca660522bd9fc6ab0d9adae6bec84ed48acb (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
/*
 *  This file contains the efi68k console IO package.
 *
 *  COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994.
 *  On-Line Applications Research Corporation (OAR).
 *  All rights assigned to U.S. Government, 1994.
 *
 *  This material may be reproduced by or for the U.S. Government pursuant
 *  to the copyright license under the clause at DFARS 252.227-7013.  This
 *  notice must appear in all copies of this file and its derivatives.
 *
 *  $Id$
 */

#include <stdlib.h>
#include <bsp.h>
#include <rtems/libio.h>


#define BAUD 38400
#define CLK_FREQ 8000000.0

/* BUFFER_LENGTH must be 2^n for n=1, 2, 3, .... */
#define BUFFER_LENGTH 256
#define RTS_STOP_SIZE BUFFER_LENGTH-64
#define RTS_START_SIZE 16

char xmt_buf[BUFFER_LENGTH];
char rcv_buf[BUFFER_LENGTH];
/* in: last entry into the buffer; always on a valid character */
/* out: points to the next character to be pull from the buffer */
/*    in+1=out => buffer empty */
/*    in+2=out => buffer full */
struct UART_buf {
  char *offset; 
  char *in; 
  char *out;
};
static volatile struct UART_buf  xmt = { xmt_buf, (char *)0, (char *)1};
static volatile struct UART_buf  rcv = { rcv_buf, (char *)0, (char *)1};
static volatile char _debug_flag = 0;
static volatile char _tx_stop = 0;

/* _catchUARTint is the interrupt front-end */
extern void _catchUARTint();
asm("   .text
        .align 2
        .globl _catchUARTint
_catchUARTint:
        lea    %sp@(4),%sp                /* pop return address */
        moveml %d0-%d7/%a0-%a6,%sp@-       /* save registers */
        jbsr    uart_interrupt
        moveml  %sp@+,%d0-%d7/%a0-%a6			        
        rte
    ");

/* _fake_trap_1 will continue the UART interrupt (%sr *still*
   UART_ISR_LEVEL) as a trap #1 to enter the debugger */
asm("   .text
        .align 2
_fake_trap_1:
        unlk %a6		/* clear interrupt frame */
        lea %sp@(4),%sp		/* remove jbsr instruction */
        moveml %sp@+,%d0-%d7/%a0-%a6 /* pop registers */
        jmp (33*6-12+_VBR)	/* jump exception 1 */
        ");

/* dispatch UART interrupt */
void xmit_interrupt(void);
void rcvr_interrupt(void);
void modem_status(void);
void _fake_trap_1(void);
void uart_interrupt(void) {
  register char a;

  a=*IIR & (NIP | IID_MASK); /* read interrupt id register */
  switch (a) {
  case 0x04: case 0x0c:
    rcvr_interrupt();
    break;
  case 0x02:
    xmit_interrupt();
    break;
  case 0x00:
    modem_status();
    break;
  default:
    break;
  }
  if (_debug_flag) {
    _debug_flag = 0;		/* reset the flag */
    _fake_trap_1();		/* fake a trap #1 */
  }
}

/* transfer received character to the buffer */
void rcvr_interrupt(void) {
  register char *a, c;
  register int length;

  while ( (*LSR & DR) != 0) {
    if ((c=*RBR) == 0x1a)	/* use ctl-z to reboot */
      reboot();
    else if (c == 0x03) {	/* use ctl-c to enter debugger */
      _debug_flag = 1;
      continue;
    }
    *(char *)((int)rcv.offset +(int)
	      (a=(char *)(((int)rcv.in+1) & ((int)BUFFER_LENGTH-1)))) = c;
    if ((char *)(((int)rcv.in+2) & ((int)BUFFER_LENGTH-1)) != rcv.out)
      rcv.in=a;
  }
  length = (BUFFER_LENGTH -1) & ( 
    ( ((int)rcv.out <= (int)rcv.in) ? 0 : BUFFER_LENGTH) - (int)rcv.out 
    + (int)rcv.in + 1);
  if (length >= RTS_STOP_SIZE)
    *MCR &= (char) (~RTS);
}

/* tranfer buffered characters to the UART */
void xmit_interrupt(void) {
  register short int i, oldsr;

  _CPU_ISR_Disable( oldsr ); /* for when outbyte calls */
  if ( (*LSR & THRE) != 0 && _tx_stop == 0 )
    for (i=0;i<16;i++) {
      if ((char *)(((int)xmt.in+1) & ((int)BUFFER_LENGTH-1))== xmt.out)
	break;
      *THR=*(char *)((int)xmt.offset+(int)xmt.out);
      xmt.out= (char *)(((int)xmt.out+1) & ((int)BUFFER_LENGTH-1));
    }
  _CPU_ISR_Enable( oldsr );
}

void modem_status(void) {
  register char a;
  
  if ( ((a=*MDSR) & DCTS) != 0 )
    if ( (a & CTS) == 0)
      _tx_stop = 1;
    else {
      _tx_stop = 0;
      xmit_interrupt();
    }
}

/* transfer character from the buffer */
char inbyte(void) {
  register char a;
  register int length;

  while ((char *)(((int)rcv.in+1) & ((int)BUFFER_LENGTH-1))== rcv.out);
  a=*(char *)((int)rcv.offset+(int)rcv.out);
  rcv.out= (char *)(((int)rcv.out+1) & ((int)BUFFER_LENGTH-1));
  length = (BUFFER_LENGTH -1) & ( 
    ( ((int)rcv.out <= (int)rcv.in) ? 0 : BUFFER_LENGTH) - (int)rcv.out 
    + (int)rcv.in + 1);
  if (length < RTS_START_SIZE)
    *MCR |= (char) RTS;
  return (a);
}

/* once room is avaliable in the buffer, transfer
   the character into the buffer and enable 
   the xmtr interrupt */
void outbyte(char c) {
  register char *a;
  
  while ((char *)(((int)xmt.in+2) & ((int)BUFFER_LENGTH-1)) == xmt.out);
  *(char *)((int)xmt.offset+(int)
	    (a=(char *)(((int)xmt.in+1) & ((int)BUFFER_LENGTH-1))))=c;
  xmt.in=a;

  if ( (*LSR & THRE) != 0 ) 	/* if THRE, uart has already interrupted */
    xmit_interrupt();		/*   and was ignored. Need to restart. */
}

void _UART_flush(void) {
  /* loop till xmt buffer empty. Works with interrupts disabled */
  while ((char *)(((int)xmt.in+1) & ((int)BUFFER_LENGTH-1)) != xmt.out)
    xmit_interrupt();
  /* loop till UART buffer empty */
  while ( (*LSR & TEMT) == 0 );
}

/*  console_initialize
 *
 *  This routine initializes the console IO driver.
 *
 *  Input parameters: NONE
 *
 *  Output parameters:  NONE
 *
 *  Return values:
 */
 
void console_init()
{
  /* set clock divisor */
  *LCR = (char)(DLAB);
  *DLL = (char)((int)(CLK_FREQ/BAUD/16.0+0.5) & 0xFF);
  *DLM = (char)(((int)(CLK_FREQ/BAUD/16.0+0.5) & 0xFF00) >> 8);

  /* Line control setup */
  *LCR = (char)(WL_8 | NSB);

  /* Interrupt setup */
  *IER = (char) 0x0b;		/* enable transmit, receive, modem stat int */
  
  /* FIFO setup */
  *FCR = (char)(FIFO_E | 0xc0);

  /* Modem control setup */
  *MCR = (char) RTS;

  /* init tx_stop with CTS */
  _tx_stop = ( (*MDSR & CTS) ? 0 : 1);

  set_vector(_catchUARTint, UART_ISR_LEVEL+24, 0);
}

rtems_device_driver console_initialize(
  rtems_device_major_number  major,
  rtems_device_minor_number  minor,
  void                      *arg
)
{
  rtems_status_code status;

  status = rtems_io_register_name(
    "/dev/console",
    major,
    (rtems_device_minor_number) 0
  );
 
  if (status != RTEMS_SUCCESSFUL)
    rtems_fatal_error_occurred(status);
 
  return RTEMS_SUCCESSFUL;

}


/*  is_character_ready
 *
 *  This routine returns TRUE if a character is available.
 *
 *  Input parameters: NONE
 *
 *  Output parameters:  NONE
 *
 *  Return values:
 */

rtems_boolean is_character_ready(
  char *ch
)
{
  if ((char *)(((int)rcv.in+1) & ((int)BUFFER_LENGTH-1))== rcv.out)
    return(FALSE);
  else
    return(TRUE);
}

/*
 *  Open entry point
 */
 
rtems_device_driver console_open(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void                    * arg
)
{
  return RTEMS_SUCCESSFUL;
}
 
/*
 *  Close entry point
 */
 
rtems_device_driver console_close(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void                    * arg
)
{
  return RTEMS_SUCCESSFUL;
}
 
/*
 * read bytes from the serial port. We only have stdin.
 */
 
rtems_device_driver console_read(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void                    * arg
)
{
  rtems_libio_rw_args_t *rw_args;
  char *buffer;
  int maximum;
  int count = 0;
 
  rw_args = (rtems_libio_rw_args_t *) arg;
 
  buffer = rw_args->buffer;
  maximum = rw_args->count;
 
  for (count = 0; count < maximum; count++) {
    buffer[ count ] = inbyte();
    if (buffer[ count ] == '\n' || buffer[ count ] == '\r') {
      buffer[ count++ ]  = '\n';
      buffer[ count ]  = 0;
      break;
    }
  }
 
  rw_args->bytes_moved = count;
  return (count >= 0) ? RTEMS_SUCCESSFUL : RTEMS_UNSATISFIED;
}
 
/*
 * write bytes to the serial port. Stdout and stderr are the same.
 */
 
rtems_device_driver console_write(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void                    * arg
)
{
  int count;
  int maximum;
  rtems_libio_rw_args_t *rw_args;
  char *buffer;
 
  rw_args = (rtems_libio_rw_args_t *) arg;
 
  buffer = rw_args->buffer;
  maximum = rw_args->count;
 
  for (count = 0; count < maximum; count++) {
    if ( buffer[ count ] == '\n') {
      outbyte('\r');
    }
    outbyte( buffer[ count ] );
  }
 
  rw_args->bytes_moved = maximum;
  return 0;
}
 
/*
 *  IO Control entry point
 */
 
rtems_device_driver console_control(
  rtems_device_major_number major,
  rtems_device_minor_number minor,
  void                    * arg
)
{
  return RTEMS_SUCCESSFUL;
}