source: rtems/c/src/lib/libbsp/m68k/mcf5206elite/console/console.c @ c499856

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 10.9 KB
Line 
1/*
2 * Console driver for Motorola MCF5206E UART modules
3 *
4 * Copyright (C) 2000 OKTET Ltd., St.-Petersburg, Russia
5 * Author: Victor V. Vengerov <vvv@oktet.ru>
6 *
7 *  COPYRIGHT (c) 1989-1998.
8 *  On-Line Applications Research Corporation (OAR).
9 *
10 *  The license and distribution terms for this file may be
11 *  found in the file LICENSE in this distribution or at
12 *  http://www.rtems.org/license/LICENSE.
13 */
14
15#include <termios.h>
16#include <bsp.h>
17#include <rtems/libio.h>
18#include "mcf5206/mcf5206e.h"
19#include "mcf5206/mcfuart.h"
20
21/* Descriptor structures for two on-chip UART channels */
22static mcfuart uart[2];
23
24/* Console operations mode:
25 *     0 - raw (non-termios) polled input/output
26 *     1 - termios-based polled input/output
27 *     2 - termios-based interrupt-driven input/output
28 */
29int console_mode = 2;
30#define CONSOLE_MODE_RAW  (0)
31#define CONSOLE_MODE_POLL (1)
32#define CONSOLE_MODE_INT  (2)
33
34/* Wrapper functions for MCF UART generic driver */
35
36/* console_poll_read --
37 *     wrapper for poll read function
38 *
39 * PARAMETERS:
40 *     minor - minor device number
41 *
42 * RETURNS:
43 *     character code readed from UART, or -1 if there is no characters
44 *     available
45 */
46static int
47console_poll_read(int minor)
48{
49    return mcfuart_poll_read(&uart[minor]);
50}
51
52/* console_interrupt_write --
53 *     wrapper for interrupt write function
54 *
55 * PARAMETERS:
56 *     minor - minor device number
57 *     buf - output buffer
58 *     len - output buffer length
59 *
60 * RETURNS:
61 *     result code
62 */
63static ssize_t
64console_interrupt_write(int minor, const char *buf, size_t len)
65{
66    return mcfuart_interrupt_write(&uart[minor], buf, len);
67}
68
69/* console_poll_write --
70 *     wrapper for polling mode write function
71 *
72 * PARAMETERS:
73 *     minor - minor device number
74 *     buf - output buffer
75 *     len - output buffer length
76 *
77 * RETURNS:
78 *     result code
79 */
80static ssize_t
81console_poll_write(int minor, const char *buf, size_t len)
82{
83    return mcfuart_poll_write(&uart[minor], buf, len);
84}
85
86/* console_set_attributes --
87 *     wrapper for hardware-dependent termios attributes setting
88 *
89 * PARAMETERS:
90 *     minor - minor device number
91 *     t - pointer to the termios structure
92 *
93 * RETURNS:
94 *     result code
95 */
96static int
97console_set_attributes(int minor, const struct termios *t)
98{
99    return mcfuart_set_attributes(&uart[minor], t);
100}
101
102/* console_stop_remote_tx --
103 *     wrapper for stopping data flow from remote party.
104 *
105 * PARAMETERS:
106 *     minor - minor device number
107 *
108 * RETURNS:
109 *     result code
110 */
111static int
112console_stop_remote_tx(int minor)
113{
114    if (minor < sizeof(uart)/sizeof(uart[0]))
115        return mcfuart_stop_remote_tx(&uart[minor]);
116    else
117        return RTEMS_INVALID_NUMBER;
118}
119
120/* console_start_remote_tx --
121 *     wrapper for resuming data flow from remote party.
122 *
123 * PARAMETERS:
124 *     minor - minor device number
125 *
126 */
127static int
128console_start_remote_tx(int minor)
129{
130    if (minor < sizeof(uart)/sizeof(uart[0]))
131        return mcfuart_start_remote_tx(&uart[minor]);
132    else
133        return RTEMS_INVALID_NUMBER;
134}
135
136/* console_first_open --
137 *     wrapper for UART controller initialization functions
138 *
139 * PARAMETERS:
140 *     major - major device number
141 *     minor - minor device number
142 *     arg - libio device open argument
143 *
144 * RETURNS:
145 *     error code
146 */
147static int
148console_first_open(int major, int minor, void *arg)
149{
150    rtems_libio_open_close_args_t *args = arg;
151    rtems_status_code sc;
152    uint8_t         intvec;
153
154    switch (minor)
155    {
156        case 0: intvec = BSP_INTVEC_UART1; break;
157        case 1: intvec = BSP_INTVEC_UART2; break;
158        default:
159            return RTEMS_INVALID_NUMBER;
160    }
161
162    if (console_mode != CONSOLE_MODE_INT)
163    {
164        intvec = 0;
165    }
166
167    sc = mcfuart_init(&uart[minor],          /* uart */
168                      args->iop->data1,      /* tty */
169                      intvec,                /* interrupt vector number */
170                      minor+1);
171
172    if (sc == RTEMS_SUCCESSFUL)
173        sc = mcfuart_reset(&uart[minor]);
174
175    return sc;
176}
177
178/* console_last_close --
179 *     wrapper for UART controller close function
180 *
181 * PARAMETERS:
182 *     major - major device number
183 *     minor - minor device number
184 *     arg - libio device close argument
185 *
186 * RETURNS:
187 *     error code
188 */
189static int
190console_last_close(int major, int minor, void *arg)
191{
192    return mcfuart_disable(&uart[minor]);
193}
194
195/* console_initialize --
196 *     This routine initializes the console IO drivers and register devices
197 *     in RTEMS I/O system.
198 *
199 * PARAMETERS:
200 *     major - major console device number
201 *     minor - minor console device number (not used)
202 *     arg - device initialize argument
203 *
204 * RETURNS:
205 *     RTEMS error code (RTEMS_SUCCESSFUL if device initialized successfuly)
206 */
207rtems_device_driver
208console_initialize(rtems_device_major_number major,
209                   rtems_device_minor_number minor,
210                   void *arg)
211{
212    rtems_status_code status;
213
214    /*
215     * Set up TERMIOS
216     */
217    if (console_mode != CONSOLE_MODE_RAW)
218        rtems_termios_initialize ();
219
220    /*
221     * Register the devices
222     */
223    status = rtems_io_register_name ("/dev/console", major, 0);
224    if (status != RTEMS_SUCCESSFUL)
225        rtems_fatal_error_occurred (status);
226    status = rtems_io_register_name ("/dev/aux", major, 1);
227    if (status != RTEMS_SUCCESSFUL)
228        rtems_fatal_error_occurred (status);
229
230    if (console_mode == CONSOLE_MODE_RAW)
231    {
232        rtems_status_code sc;
233        sc = mcfuart_init(&uart[0],              /* uart */
234                          NULL,                  /* tty */
235                          0,                     /* interrupt vector number */
236                          1);                    /* UART channel number */
237
238        if (sc == RTEMS_SUCCESSFUL)
239            sc = mcfuart_reset(&uart[0]);
240
241        sc = mcfuart_init(&uart[1],              /* uart */
242                          NULL,                  /* tty */
243                          0,                     /* interrupt vector number */
244                          2);                    /* UART channel number */
245
246        if (sc == RTEMS_SUCCESSFUL)
247            sc = mcfuart_reset(&uart[1]);
248        return sc;
249    }
250
251    return RTEMS_SUCCESSFUL;
252}
253
254/* console_open --
255 *     Open console device driver. Pass appropriate termios callback
256 *     functions to termios library.
257 *
258 * PARAMETERS:
259 *     major - major device number for console devices
260 *     minor - minor device number for console
261 *     arg - device opening argument
262 *
263 * RETURNS:
264 *     RTEMS error code
265 */
266rtems_device_driver
267console_open(rtems_device_major_number major,
268             rtems_device_minor_number minor,
269             void *arg)
270{
271    static const rtems_termios_callbacks intr_callbacks = {
272        console_first_open,        /* firstOpen */
273        console_last_close,        /* lastClose */
274        NULL,                      /* pollRead */
275        console_interrupt_write,   /* write */
276        console_set_attributes,    /* setAttributes */
277        console_stop_remote_tx,    /* stopRemoteTx */
278        console_start_remote_tx,   /* startRemoteTx */
279        1                          /* outputUsesInterrupts */
280    };
281    static const rtems_termios_callbacks poll_callbacks = {
282        console_first_open,        /* firstOpen */
283        console_last_close,        /* lastClose */
284        console_poll_read,         /* pollRead */
285        console_poll_write,        /* write */
286        console_set_attributes,    /* setAttributes */
287        console_stop_remote_tx,    /* stopRemoteTx */
288        console_start_remote_tx,   /* startRemoteTx */
289        0                          /* outputUsesInterrupts */
290    };
291
292    switch (console_mode)
293    {
294        case CONSOLE_MODE_RAW:
295            return RTEMS_SUCCESSFUL;
296
297        case CONSOLE_MODE_INT:
298            return rtems_termios_open(major, minor, arg, &intr_callbacks);
299
300        case CONSOLE_MODE_POLL:
301            return rtems_termios_open(major, minor, arg, &poll_callbacks);
302
303        default:
304            rtems_fatal_error_occurred(0xC07A1310);
305    }
306    return RTEMS_INTERNAL_ERROR;
307}
308
309/* console_close --
310 *     Close console device.
311 *
312 * PARAMETERS:
313 *     major - major device number for console devices
314 *     minor - minor device number for console
315 *     arg - device close argument
316 *
317 * RETURNS:
318 *     RTEMS error code
319 */
320rtems_device_driver
321console_close(rtems_device_major_number major,
322              rtems_device_minor_number minor,
323              void *arg)
324{
325    if (console_mode != CONSOLE_MODE_RAW)
326        return rtems_termios_close (arg);
327    else
328        return RTEMS_SUCCESSFUL;
329}
330
331/* console_read --
332 *     Read from the console device
333 *
334 * PARAMETERS:
335 *     major - major device number for console devices
336 *     minor - minor device number for console
337 *     arg - device read argument
338 *
339 * RETURNS:
340 *     RTEMS error code
341 */
342rtems_device_driver
343console_read(rtems_device_major_number major,
344             rtems_device_minor_number minor,
345             void *arg)
346{
347    if (console_mode != CONSOLE_MODE_RAW)
348    {
349        return rtems_termios_read (arg);
350    }
351    else
352    {
353        rtems_libio_rw_args_t *argp = arg;
354        char *buf = argp->buffer;
355        int count = argp->count;
356        int n = 0;
357        int c;
358        while (n < count)
359        {
360            do {
361                c = mcfuart_poll_read(&uart[minor]);
362            } while (c == -1);
363            if (c == '\r')
364                c = '\n';
365            *(buf++) = c;
366            n++;
367            if (c == '\n')
368                break;
369        }
370        argp->bytes_moved = n;
371        return RTEMS_SUCCESSFUL;
372    }
373}
374
375/* console_write --
376 *     Write to the console device
377 *
378 * PARAMETERS:
379 *     major - major device number for console devices
380 *     minor - minor device number for console
381 *     arg - device write argument
382 *
383 * RETURNS:
384 *     RTEMS error code
385 */
386rtems_device_driver
387console_write(rtems_device_major_number major,
388              rtems_device_minor_number minor,
389              void *arg
390)
391{
392    if (console_mode != CONSOLE_MODE_RAW)
393    {
394        return rtems_termios_write (arg);
395    }
396    else
397    {
398        rtems_libio_rw_args_t *argp = arg;
399        char cr = '\r';
400        char *buf = argp->buffer;
401        int count = argp->count;
402        int i;
403        for (i = 0; i < count; i++)
404        {
405            if (*buf == '\n')
406                mcfuart_poll_write(&uart[minor], &cr, 1);
407            mcfuart_poll_write(&uart[minor], buf, 1);
408            buf++;
409        }
410        argp->bytes_moved = count;
411        return RTEMS_SUCCESSFUL;
412    }
413}
414
415/* console_control --
416 *     Handle console device I/O control (IOCTL)
417 *
418 * PARAMETERS:
419 *     major - major device number for console devices
420 *     minor - minor device number for console
421 *     arg - device ioctl argument
422 *
423 * RETURNS:
424 *     RTEMS error code
425 */
426rtems_device_driver
427console_control(rtems_device_major_number major,
428                rtems_device_minor_number minor,
429                void *arg)
430{
431    if (console_mode != CONSOLE_MODE_RAW)
432    {
433        return rtems_termios_ioctl (arg);
434    }
435    else
436    {
437        return RTEMS_SUCCESSFUL;
438    }
439}
Note: See TracBrowser for help on using the repository browser.