summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/or32/orp/console/console.c
blob: 5630f8cd60e848cea2b974e98b1624bd3cbc7e07 (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
/*
 *  This file contains the template for a console IO package.
 *
 *  COPYRIGHT (c) 1989-1999.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  This file adapted from no_bsp board library of the RTEMS distribution.
 *  The body has been modified for the Bender Or1k implementation by
 *  Chris Ziomkowski. <chris@asics.ws>
 */

#define BENDER_INIT

#include <bsp.h>
#include <rtems/libio.h>
#include "console.h"

static int localEcho;
static UART_16450* uart = (UART_16450*)0x80000000;  /* This is where the simulator puts it */

/*  console_initialize
 *
 *  This routine initializes the console IO driver.
 *
 *  Input parameters: NONE
 *
 *  Output parameters:  NONE
 *
 *  Return values:
 */

void (*old_handler)(unsigned int,unsigned int,unsigned int,unsigned int);

void console_interrupt(unsigned int vector,unsigned int pc,
		       unsigned int effective_addr, unsigned int status)
{
  int reason;
  register int pending;

  /* First thing's first...is this for us? */
  asm volatile ("l.mfspr %0,r0,0x4802 \n\t"  /* Read the PIC status */
		"l.andi  %0,%0,0x4    \n\t" : "=r" (pending));

  if(pending)
    {
      reason = uart->read.IIR;

      switch(reason)
	{
	case 0: /* Interrupt because of modem status */
	  break;
	case 2: /* Interrupt because Transmitter empty */
	  break;
	case 4: /* Interrupt because Received data available */
	  break;
	case 6: /* Interrupt because Status Register */
	  break;
	case 12: /* Interrupt because of character timeout (16550 only) */
	  break;
	default: /* No interrupt */
	  break;
	}
    }

  if(old_handler)
    (*old_handler)(vector,pc,effective_addr,status);
}

rtems_device_driver console_initialize(
  rtems_device_major_number  major,
  rtems_device_minor_number  minor,
  void                      *arg
)
{
  rtems_status_code status;
  int tmp,tmp2;
  uint32_t   sr;
  extern uint32_t   Or1k_Interrupt_Vectors[16];

  /* Make sure the UART (interrupt 2) is enabled and
     reports a low prority interrupt */
  asm volatile ("l.mfspr %0,r0,0x4800  \n\t"  /* Get the PIC mask */
		"l.ori   %0,%0,0x4     \n\t"  /* Enable int 2 */
		"l.mtspr r0,%0,0x4800  \n\t"  /* Write back mask */
		"l.mfspr %0,r0,0x4801  \n\t"  /* Get priority mask */
		"l.addi  %1,r0,-5      \n\t"
		"l.and   %0,%0,%1      \n\t"  /* Set us to low */
		"l.mtspr r0,%0,0x4801  \n\t"  /* Write back to PICPR */
		: "=r" (tmp), "=r" (tmp2));

  /* Install the interrupt handler */
  asm volatile ("l.mfspr %0,r0,0x11 \n\t"
		"l.addi  %1,r0,-5   \n\t"
		"l.and   %1,%1,%0   \n\t"
		"l.mtspr r0,%1,0x11 \n\t": "=&r" (sr) : "r" (tmp));

  old_handler = (void(*)(unsigned int,unsigned int,unsigned int,unsigned int))
    Or1k_Interrupt_Vectors[5];
  Or1k_Interrupt_Vectors[5] = (uint32_t)console_interrupt;

  asm volatile ("l.mtspr r0,%0,0x11\n\t":: "r" (sr));

  /* Assume 1843.2/16 kHz clock */
  uart->latch.LCR = 0x80;    /* Set the divisor latch bit */
  uart->latch.DLM = 2;       /* 57,600 */
  uart->latch.DLL = 0;
  uart->write.LCR = 0x03;    /* 8-N-1 */
  uart->write.MCR = 0x03;    /* Assert RTS & DTR */
  /* uart->write.FCR = 0x00; */ /* Make sure we're in 16450 mode... Ignore for 16450 driver */
  uart->write.IER = 0x05;    /* Don't worry about TEMT unless we need to. */

  tmp = uart->read.LSR;     /* Make sure interrupts are cleared */
  tmp = uart->read.RBR;     /* Clear the input buffer */
  tmp = uart->read.MSR;     /* Clear the modem status register */

  localEcho = 1;       /* Turn on local echo */

  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
)
{
  *ch = '\0';   /* return NULL for no particular reason */
  return(TRUE);
}

/*  inbyte
 *
 *  This routine reads a character from the SOURCE.
 *
 *  Input parameters: NONE
 *
 *  Output parameters:  NONE
 *
 *  Return values:
 *    character read from SOURCE
 */

char inbyte( void )
{
  unsigned int stat;

  stat = uart->read.LSR;
  while(!(stat & 0x01))  /* ! Data Ready */
    {
      rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );
      stat = uart->read.LSR;
    }

  return uart->read.RBR;   /* Return the character */
}

/*  outbyte
 *
 *  This routine transmits a character out the SOURCE.  Flow
 *  control is not currently enabled.
 *
 *  Input parameters:
 *    ch  - character to be transmitted
 *
 *  Output parameters:  NONE
 */

void outbyte(char ch)
{
  unsigned int stat;
  /*
   *  Carriage Return/New line translation.
   */

  stat = uart->read.LSR;
  while(!(stat & 0x40))  /* ! TEMT */
    {
      rtems_task_wake_after( RTEMS_YIELD_PROCESSOR );
      stat = uart->read.LSR;
    }

  uart->write.THR = ch;
  if ( ch == '\n' )
    outbyte( '\r' );
}


/*
 *  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';
	  if(localEcho)
	    outbyte('\n' ); /* newline */
	  break;  /* Return for a newline */
	}
      else if (buffer[ count ] == '\b' && count > 0 )
	{
	  if(localEcho)
	    {
	      outbyte('\b' ); /* move back one space */
	      outbyte(' ' ); /* erase the character */
	      outbyte('\b' ); /* move back one space */
	    }
	  count-=2;
	}
      else if(localEcho)
	outbyte(buffer[ count ]); /* echo the character */
    }

  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
)
{
  int param,div;
  ConsoleIOCTLRequest* request = (ConsoleIOCTLRequest*)arg;

  if (!arg)
    return RTEMS_INVALID_ADDRESS;

  switch(request->command)
    {
    case TERM_LOCAL_ECHO:
      param = (int)(request->data);
      if(param < 0 || param > 1)
	return RTEMS_INVALID_NUMBER;
      localEcho = param;
      break;
    case TERM_BIT_RATE:
      param = (int)(request->data);
      switch(param)
	{
	case 50:
        case 150:
	case 300:
	case 600:
	case 1200:
	case 1800:
	case 2000:
	case 2400:
	case 3600:
	case 4800:
	case 7200:
	case 9600:
	case 19200:
	case 38400:
	case 57600:
	case 115200:
	  div = 115200/param;
	  uart->latch.LCR |= 0x80;    /* Set the divisor latch bit */
	  uart->latch.DLM = div & 0xFF;
	  uart->latch.DLL = div >> 8;
	  uart->write.LCR &= 0x7F;    /* Clear the divisor latch bit */
	  break;
	default:
	  return RTEMS_INVALID_NUMBER;
	}
      break;
    default:
      return RTEMS_NOT_CONFIGURED;
    }

  return RTEMS_SUCCESSFUL;
}