summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/sparc/erc32/console/erc32_console.c
blob: 8747e3e87b1eafba1d6956b4eac354e1e83dcf37 (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
/**
 * @file
 *
 * @brief Driver for serial ports on the ERC32.
 */

/*
 * Copyright (c) 2010 Tiemen Schut <T.Schut@sron.nl>
 *
 * 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.
 */

#include <unistd.h>
#include <termios.h>
#include <stdlib.h>

#include <rtems.h>
#include <rtems/libio.h>
#include <rtems/console.h>
#include <rtems/termiostypes.h>

#include <libchip/serial.h>
#include <libchip/sersupp.h>

#include <bsp.h>
#include <bspopts.h>

#define CONSOLE_BUF_SIZE (16)

#define CONSOLE_UART_A_TRAP  ERC32_TRAP_TYPE(ERC32_INTERRUPT_UART_A_RX_TX)
#define CONSOLE_UART_B_TRAP  ERC32_TRAP_TYPE(ERC32_INTERRUPT_UART_B_RX_TX)

static uint8_t erc32_console_get_register(uintptr_t addr, uint8_t i)
{
  volatile uint32_t *reg = (volatile uint32_t *)addr;
  return (uint8_t) reg [i];
}

static void erc32_console_set_register(uintptr_t addr, uint8_t i, uint8_t val)
{
  volatile uint32_t *reg = (volatile uint32_t *)addr;
  reg [i] = val;
}

static int erc32_console_first_open(int major, int minor, void *arg);

#if (CONSOLE_USE_INTERRUPTS)
  static ssize_t erc32_console_write_support_int(
    int minor, const char *buf, size_t len);
#else
  int console_inbyte_nonblocking( int port );
  static ssize_t erc32_console_write_support_polled(
      int minor, const char *buf, size_t len);
#endif
static void erc32_console_initialize(int minor);

#if (CONSOLE_USE_INTERRUPTS)
  const console_fns erc32_fns = {
    libchip_serial_default_probe,           /* deviceProbe */
    erc32_console_first_open,               /* deviceFirstOpen */
    NULL,                                   /* deviceLastClose */
    NULL,                                   /* deviceRead */
    erc32_console_write_support_int,        /* deviceWrite */
    erc32_console_initialize,               /* deviceInitialize */
    NULL,                                   /* deviceWritePolled */
    NULL,                                   /* deviceSetAttributes */
    TERMIOS_IRQ_DRIVEN                      /* deviceOutputUsesInterrupts */
  };
#else
  const console_fns erc32_fns = {
    libchip_serial_default_probe,           /* deviceProbe */
    erc32_console_first_open,               /* deviceFirstOpen */
    NULL,                                   /* deviceLastClose */
    console_inbyte_nonblocking,             /* deviceRead */
    erc32_console_write_support_polled,     /* deviceWrite */
    erc32_console_initialize,               /* deviceInitialize */
    NULL,                                   /* deviceWritePolled */
    NULL,                                   /* deviceSetAttributes */
    TERMIOS_POLLED                          /* deviceOutputUsesInterrupts */
  };
#endif

console_tbl Console_Configuration_Ports [] = {
  {
    .sDeviceName = "/dev/console_a",
    .deviceType = SERIAL_CUSTOM,
    .pDeviceFns = &erc32_fns,
    .deviceProbe = NULL,
    .pDeviceFlow = NULL,
    .ulMargin = 16,
    .ulHysteresis = 8,
    .pDeviceParams = (void *) -1,  /* could be baud rate */
    .ulCtrlPort1 = 0,
    .ulCtrlPort2 = 0,
    .ulDataPort = 0,
    .getRegister = erc32_console_get_register,
    .setRegister = erc32_console_set_register,
    .getData = NULL,
    .setData = NULL,
    .ulClock = 16,
    .ulIntVector = ERC32_INTERRUPT_UART_A_RX_TX
  },
  {
    .sDeviceName = "/dev/console_b",
    .deviceType = SERIAL_CUSTOM,
    .pDeviceFns = &erc32_fns,
    .deviceProbe = NULL,
    .pDeviceFlow = NULL,
    .ulMargin = 16,
    .ulHysteresis = 8,
    .pDeviceParams = (void *) -1,  /* could be baud rate */
    .ulCtrlPort1 = 0,
    .ulCtrlPort2 = 0,
    .ulDataPort = 0,
    .getRegister = erc32_console_get_register,
    .setRegister = erc32_console_set_register,
    .getData = NULL,
    .setData = NULL,
    .ulClock = 16,
    .ulIntVector = ERC32_INTERRUPT_UART_B_RX_TX
  },
};

/* always exactly two uarts for erc32 */
#define ERC32_UART_COUNT (2)

unsigned long Console_Configuration_Count = ERC32_UART_COUNT;

static int erc32_console_first_open(int major, int minor, void *arg)
{
  /* Check minor number */
  if (minor < 0 || minor > 1) {
    return -1;
  }
  
  rtems_libio_open_close_args_t *oca = arg;
  struct rtems_termios_tty *tty = oca->iop->data1;
  console_tbl *ct = Console_Port_Tbl [minor];
  console_data *cd = &Console_Port_Data [minor];
  
  cd->termios_data = tty;
  rtems_termios_set_initial_baud(tty, (int32_t)ct->pDeviceParams);
  
  return 0;
}

#if (CONSOLE_USE_INTERRUPTS)
static ssize_t erc32_console_write_support_int(int minor, const char *buf, size_t len)
{
  if (len > 0) {
    console_data *cd = &Console_Port_Data[minor];
    int k = 0;

    if (minor == 0) { /* uart a */
      for (k = 0;
           k < len && (ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_THEA); k ++) {
        ERC32_MEC.UART_Channel_A = (unsigned char)buf[k];
      }
      ERC32_Force_interrupt(ERC32_INTERRUPT_UART_A_RX_TX);
    } else { /* uart b */
      for (k = 0;
           k < len && (ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_THEB); k ++) {
        ERC32_MEC.UART_Channel_B = (unsigned char)buf[k];
      }
      ERC32_Force_interrupt(ERC32_INTERRUPT_UART_B_RX_TX);
    }

    cd->pDeviceContext = (void *)k;
    cd->bActive = true;
  }

  return 0;
}

static void erc32_console_isr_error(
  rtems_vector_number vector
)
{
  int UStat;

  UStat = ERC32_MEC.UART_Status;

  if (UStat & ERC32_MEC_UART_STATUS_ERRA) {
    ERC32_MEC.UART_Status = ERC32_MEC_UART_STATUS_CLRA;
    ERC32_MEC.Control = ERC32_MEC.Control;
  }

  if (UStat & ERC32_MEC_UART_STATUS_ERRB) {
    ERC32_MEC.UART_Status = ERC32_MEC_UART_STATUS_CLRB;
    ERC32_MEC.Control = ERC32_MEC.Control;
  }

  ERC32_Clear_interrupt( ERC32_INTERRUPT_UART_ERROR );
}

static void erc32_console_isr_a(
  rtems_vector_number vector
)
{
  console_data *cd = &Console_Port_Data[0];

  /* check for error */
  if (ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_ERRA) {
    ERC32_MEC.UART_Status = ERC32_MEC_UART_STATUS_CLRA;
    ERC32_MEC.Control = ERC32_MEC.Control;
  }

  do {
    int chars_to_dequeue = (int)cd->pDeviceContext;
    int rv = 0;
    int i = 0;
    char buf[CONSOLE_BUF_SIZE];
        
    /* enqueue received chars */
    while (i < CONSOLE_BUF_SIZE) {
      if (!(ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_DRA))
        break;
      buf[i] = ERC32_MEC.UART_Channel_A;
      ++i;
    }
    if ( i ) 
      rtems_termios_enqueue_raw_characters(cd->termios_data, buf, i);

    /* dequeue transmitted chars */
    if (ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_THEA) {
      rv = rtems_termios_dequeue_characters(
         cd->termios_data, chars_to_dequeue);
      if ( !rv ) {
        cd->pDeviceContext = 0;
        cd->bActive = false;
      }
      ERC32_Clear_interrupt (ERC32_INTERRUPT_UART_A_RX_TX);
    }
  } while (ERC32_Is_interrupt_pending (ERC32_INTERRUPT_UART_A_RX_TX));
}

static void erc32_console_isr_b(
  rtems_vector_number vector
)
{
  console_data *cd = &Console_Port_Data[1];

  /* check for error */
  if (ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_ERRB) {
      ERC32_MEC.UART_Status = ERC32_MEC_UART_STATUS_CLRB;
      ERC32_MEC.Control = ERC32_MEC.Control;
  }

  do {
    int chars_to_dequeue = (int)cd->pDeviceContext;
    int rv = 0;
    int i = 0;
    char buf[CONSOLE_BUF_SIZE];
        
    /* enqueue received chars */
    while (i < CONSOLE_BUF_SIZE) {
      if (!(ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_DRB))
        break;
      buf[i] = ERC32_MEC.UART_Channel_B;
      ++i;
    }
    if ( i ) 
      rtems_termios_enqueue_raw_characters(cd->termios_data, buf, i);

    /* dequeue transmitted chars */
    if (ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_THEB) {
      rv = rtems_termios_dequeue_characters(
         cd->termios_data, chars_to_dequeue);
      if ( !rv ) {
        cd->pDeviceContext = 0;
        cd->bActive = false;
      }
      ERC32_Clear_interrupt (ERC32_INTERRUPT_UART_B_RX_TX);
    }
  } while (ERC32_Is_interrupt_pending (ERC32_INTERRUPT_UART_B_RX_TX));
}
#else

extern void console_outbyte_polled( int  port, unsigned char ch );

static ssize_t erc32_console_write_support_polled(
  int         minor,
  const char *buf,
  size_t      len
)
{
  int nwrite = 0;

  while (nwrite < len) {
    console_outbyte_polled( minor, *buf++ );
    nwrite++;
  }
  return nwrite;
}

#endif


/*
 *  Console Device Driver Entry Points
 *
 */

static void erc32_console_initialize(
    int minor
)
{
  console_data *cd = &Console_Port_Data [minor];

  cd->bActive = false;
  cd->pDeviceContext = 0;

 /*
  * Initialize the Termios infrastructure.  If Termios has already
  * been initialized by another device driver, then this call will
  * have no effect.
  */
  rtems_termios_initialize();

 /*
  *  Initialize Hardware
  */
  #if (CONSOLE_USE_INTERRUPTS)
    set_vector(erc32_console_isr_a, CONSOLE_UART_A_TRAP, 1);
    set_vector(erc32_console_isr_b, CONSOLE_UART_B_TRAP, 1);
    set_vector(erc32_console_isr_error, CONSOLE_UART_ERROR_TRAP, 1);
  #endif

   /* Clear any previous error flags */
   ERC32_MEC.UART_Status = ERC32_MEC_UART_STATUS_CLRA;
   ERC32_MEC.UART_Status = ERC32_MEC_UART_STATUS_CLRB;
}