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

4.104.114.84.95
Last change on this file since 5850c533 was 5850c533, checked in by Joel Sherrill <joel.sherrill@…>, on 10/26/01 at 13:41:07

2001-10-26 Alexandra Kossovsky <sasha@…>

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