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

4.104.114.84.95
Last change on this file since b7044dc was b7044dc, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/31/04 at 04:40:43

2004-03-31 Ralf Corsepius <ralf_corsepius@…>

  • console/console.c, i2c/i2c.c, i2c/i2cdrv.c, include/bsp.h, include/i2c.h, nvram/nvram.c, startup/bspstart.c, startup/init5206e.c, tod/ds1307.c: Convert to using c99 fixed size types.
  • Property mode set to 100644
File size: 11.0 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 *
13 *  http://www.rtems.com/license/LICENSE.
14 *
15 *  $Id$
16 */
17
18#include <termios.h>
19#include <bsp.h>
20#include <rtems/libio.h>
21#include "mcf5206/mcf5206e.h"
22#include "mcf5206/mcfuart.h"
23
24
25/* Descriptor structures for two on-chip UART channels */
26static mcfuart uart[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 */
33int console_mode = 2;
34#define CONSOLE_MODE_RAW  (0)
35#define CONSOLE_MODE_POLL (1)
36#define CONSOLE_MODE_INT  (2)
37
38/* Wrapper functions for MCF 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 mcfuart_poll_read(&uart[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 mcfuart_interrupt_write(&uart[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 mcfuart_poll_write(&uart[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 mcfuart_set_attributes(&uart[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(uart)/sizeof(uart[0]))
119        return mcfuart_stop_remote_tx(&uart[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(uart)/sizeof(uart[0]))
135        return mcfuart_start_remote_tx(&uart[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    uint8_t         intvec;
157   
158    switch (minor)
159    {
160        case 0: intvec = BSP_INTVEC_UART1; break;
161        case 1: intvec = BSP_INTVEC_UART2; break;
162        default:
163            return RTEMS_INVALID_NUMBER;
164    }
165
166    if (console_mode != CONSOLE_MODE_INT)
167    {
168        intvec = 0;
169    }
170
171    sc = mcfuart_init(&uart[minor],          /* uart */
172                      args->iop->data1,      /* tty */
173                      intvec,                /* interrupt vector number */
174                      minor+1);
175
176    if (sc == RTEMS_SUCCESSFUL)
177        sc = mcfuart_reset(&uart[minor]);
178
179    return sc;
180}
181
182/* console_last_close --
183 *     wrapper for UART controller close function
184 *
185 * PARAMETERS:
186 *     major - major device number
187 *     minor - minor device number
188 *     arg - libio device close argument
189 *
190 * RETURNS:
191 *     error code
192 */
193static int
194console_last_close(int major, int minor, void *arg)
195{
196    return mcfuart_disable(&uart[minor]);
197}
198
199/* console_initialize --
200 *     This routine initializes the console IO drivers and register devices
201 *     in RTEMS I/O system.
202 *
203 * PARAMETERS:
204 *     major - major console device number
205 *     minor - minor console device number (not used)
206 *     arg - device initialize argument
207 *
208 * RETURNS:
209 *     RTEMS error code (RTEMS_SUCCESSFUL if device initialized successfuly)
210 */
211rtems_device_driver
212console_initialize(rtems_device_major_number major,
213                   rtems_device_minor_number minor,
214                   void *arg)
215{
216    rtems_status_code status;
217
218    /*
219     * Set up TERMIOS
220     */
221    if (console_mode != CONSOLE_MODE_RAW)
222        rtems_termios_initialize ();
223
224    /*
225     * Register the devices
226     */
227    status = rtems_io_register_name ("/dev/console", major, 0);
228    if (status != RTEMS_SUCCESSFUL)
229        rtems_fatal_error_occurred (status);
230    status = rtems_io_register_name ("/dev/aux", major, 1);
231    if (status != RTEMS_SUCCESSFUL)
232        rtems_fatal_error_occurred (status);
233
234    if (console_mode == CONSOLE_MODE_RAW)
235    {
236        rtems_status_code sc;
237        sc = mcfuart_init(&uart[0],              /* uart */
238                          NULL,                  /* tty */
239                          0,                     /* interrupt vector number */
240                          1);                    /* UART channel number */
241
242        if (sc == RTEMS_SUCCESSFUL)
243            sc = mcfuart_reset(&uart[0]);
244
245        sc = mcfuart_init(&uart[1],              /* uart */
246                          NULL,                  /* tty */
247                          0,                     /* interrupt vector number */
248                          2);                    /* UART channel number */
249
250        if (sc == RTEMS_SUCCESSFUL)
251            sc = mcfuart_reset(&uart[1]);
252        return sc;
253    }
254   
255    return RTEMS_SUCCESSFUL;
256}
257
258/* console_open --
259 *     Open console device driver. Pass appropriate termios callback
260 *     functions to termios library.
261 *
262 * PARAMETERS:
263 *     major - major device number for console devices
264 *     minor - minor device number for console
265 *     arg - device opening argument
266 *
267 * RETURNS:
268 *     RTEMS error code
269 */
270rtems_device_driver
271console_open(rtems_device_major_number major,
272             rtems_device_minor_number minor,
273             void *arg)
274{
275    static const rtems_termios_callbacks intr_callbacks = {
276        console_first_open,        /* firstOpen */
277        console_last_close,        /* lastClose */
278        NULL,                      /* pollRead */
279        console_interrupt_write,   /* write */
280        console_set_attributes,    /* setAttributes */
281        console_stop_remote_tx,    /* stopRemoteTx */
282        console_start_remote_tx,   /* startRemoteTx */
283        1                          /* outputUsesInterrupts */
284    };
285    static const rtems_termios_callbacks poll_callbacks = {
286        console_first_open,        /* firstOpen */
287        console_last_close,        /* lastClose */
288        console_poll_read,         /* pollRead */
289        console_poll_write,        /* write */
290        console_set_attributes,    /* setAttributes */
291        console_stop_remote_tx,    /* stopRemoteTx */
292        console_start_remote_tx,   /* startRemoteTx */
293        0                          /* outputUsesInterrupts */
294    };
295
296    switch (console_mode)
297    {
298        case CONSOLE_MODE_RAW:
299            return RTEMS_SUCCESSFUL;
300           
301        case CONSOLE_MODE_INT:
302            return rtems_termios_open(major, minor, arg, &intr_callbacks);
303           
304        case CONSOLE_MODE_POLL:
305            return rtems_termios_open(major, minor, arg, &poll_callbacks);
306
307        default:
308            rtems_fatal_error_occurred(0xC07A1310);
309    }
310    return RTEMS_INTERNAL_ERROR;
311}
312
313/* console_close --
314 *     Close console device.
315 *
316 * PARAMETERS:
317 *     major - major device number for console devices
318 *     minor - minor device number for console
319 *     arg - device close argument
320 *
321 * RETURNS:
322 *     RTEMS error code
323 */
324rtems_device_driver
325console_close(rtems_device_major_number major,
326              rtems_device_minor_number minor,
327              void *arg)
328{
329    if (console_mode != CONSOLE_MODE_RAW)
330        return rtems_termios_close (arg);
331    else
332        return RTEMS_SUCCESSFUL;
333}
334
335/* console_read --
336 *     Read from the console device
337 *
338 * PARAMETERS:
339 *     major - major device number for console devices
340 *     minor - minor device number for console
341 *     arg - device read argument
342 *
343 * RETURNS:
344 *     RTEMS error code
345 */
346rtems_device_driver
347console_read(rtems_device_major_number major,
348             rtems_device_minor_number minor,
349             void *arg)
350{
351    if (console_mode != CONSOLE_MODE_RAW)
352    {
353        return rtems_termios_read (arg);
354    }
355    else
356    {
357        rtems_libio_rw_args_t *argp = arg;
358        char *buf = argp->buffer;
359        int count = argp->count;
360        int n = 0;
361        int c;
362        while (n < count)
363        {
364            do {
365                c = mcfuart_poll_read(&uart[minor]);
366            } while (c == -1);
367            if (c == '\r')
368                c = '\n';
369            *(buf++) = c;
370            n++;
371            if (c == '\n')
372                break;
373        }
374        argp->bytes_moved = n;
375        return RTEMS_SUCCESSFUL;
376    }
377}
378
379/* console_write --
380 *     Write to the console device
381 *
382 * PARAMETERS:
383 *     major - major device number for console devices
384 *     minor - minor device number for console
385 *     arg - device write argument
386 *
387 * RETURNS:
388 *     RTEMS error code
389 */
390rtems_device_driver
391console_write(rtems_device_major_number major,
392              rtems_device_minor_number minor,
393              void *arg
394)
395{
396    if (console_mode != CONSOLE_MODE_RAW)
397    {
398        return rtems_termios_write (arg);
399    }
400    else
401    {
402        rtems_libio_rw_args_t *argp = arg;
403        char cr = '\r';
404        char *buf = argp->buffer;
405        int count = argp->count;
406        int i;
407        for (i = 0; i < count; i++)
408        {
409            if (*buf == '\n')
410                mcfuart_poll_write(&uart[minor], &cr, 1);
411            mcfuart_poll_write(&uart[minor], buf, 1);
412            buf++;
413        }
414        argp->bytes_moved = count;
415        return RTEMS_SUCCESSFUL;
416    }
417}
418
419/* console_control --
420 *     Handle console device I/O control (IOCTL)
421 *
422 * PARAMETERS:
423 *     major - major device number for console devices
424 *     minor - minor device number for console
425 *     arg - device ioctl argument
426 *
427 * RETURNS:
428 *     RTEMS error code
429 */
430rtems_device_driver
431console_control(rtems_device_major_number major,
432                rtems_device_minor_number minor,
433                void *arg)
434{
435    if (console_mode != CONSOLE_MODE_RAW)
436    {
437        return rtems_termios_ioctl (arg);
438    }
439    else
440    {
441        return RTEMS_SUCCESSFUL;
442    }
443}
Note: See TracBrowser for help on using the repository browser.