source: rtems/c/src/lib/libbsp/sh/gensh4/console/console.c @ fedc835c

4.104.114.95
Last change on this file since fedc835c was 0fdc099, checked in by Ralf Corsepius <ralf.corsepius@…>, on 04/16/04 at 21:51:30

Remove stray white spaces.

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