source: rtems/c/src/lib/libcpu/sh/sh7750/sci/console.c @ ba71076

4.104.114.84.95
Last change on this file since ba71076 was ba71076, checked in by Joel Sherrill <joel.sherrill@…>, on Oct 12, 2001 at 1:19:08 PM

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

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