1 /******************************************************************************
2 *                                                                             *
3 * License Agreement                                                           *
4 *                                                                             *
5 * Copyright (c) 2007 Altera Corporation, San Jose, California, USA.           *
6 * All rights reserved.                                                        *
7 *                                                                             *
8 * Permission is hereby granted, free of charge, to any person obtaining a     *
9 * copy of this software and associated documentation files (the "Software"),  *
10 * to deal in the Software without restriction, including without limitation   *
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,    *
12 * and/or sell copies of the Software, and to permit persons to whom the       *
13 * Software is furnished to do so, subject to the following conditions:        *
14 *                                                                             *
15 * The above copyright notice and this permission notice shall be included in  *
16 * all copies or substantial portions of the Software.                         *
17 *                                                                             *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  *
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    *
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      *
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     *
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         *
24 * DEALINGS IN THE SOFTWARE.                                                   *
25 *                                                                             *
26 *                                                                             *
27 ******************************************************************************/
28 
29 #ifndef __ALT_AVALON_UART_FD_H__
30 #define __ALT_AVALON_UART_FD_H__
31 
32 #include "sys/alt_dev.h"
33 
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #endif /* __cplusplus */
38 
39 /*
40  * Externally referenced routines
41  */
42 extern int altera_avalon_uart_read_fd (alt_fd* fd, char* ptr, int len);
43 extern int altera_avalon_uart_write_fd (alt_fd* fd, const char* ptr,
44   int len);
45 
46 /*
47  * Device structure definition. This is needed by alt_sys_init in order to
48  * reserve memory for the device instance.
49  */
50 typedef struct altera_avalon_uart_dev_s
51 {
52   alt_dev        dev;
53   altera_avalon_uart_state state;
54 } altera_avalon_uart_dev;
55 
56 #if defined(ALT_USE_SMALL_DRIVERS) || defined(ALTERA_AVALON_UART_SMALL)
57 
58 /*
59  * Macros used by alt_sys_init when the ALT file descriptor facility is used.
60  */
61 
62 #define ALTERA_AVALON_UART_DEV_INSTANCE(name, d)    \
63   static altera_avalon_uart_dev d =                 \
64     {                                               \
65       {                                             \
66         ALT_LLIST_ENTRY,                            \
67         name##_NAME,                                \
68         NULL, /* open */                            \
69         NULL, /* close */                           \
70         altera_avalon_uart_read_fd,                 \
71         altera_avalon_uart_write_fd,                \
72         NULL, /* lseek */                           \
73         NULL, /* fstat */                           \
74         NULL, /* ioctl */                           \
75       },                                            \
76       {                                             \
77         name##_BASE,                                \
78       }                                             \
79     }
80 
81 #define ALTERA_AVALON_UART_DEV_INIT(name, d) alt_dev_reg (&d.dev)
82 
83 #else /* use fast version of the driver */
84 
85 extern int altera_avalon_uart_ioctl_fd (alt_fd* fd, int req, void* arg);
86 extern int altera_avalon_uart_close_fd(alt_fd* fd);
87 
88 #ifdef ALTERA_AVALON_UART_USE_IOCTL
89 #define ALTERA_AVALON_UART_IOCTL_FD altera_avalon_uart_ioctl_fd
90 #else
91 #define ALTERA_AVALON_UART_IOCTL_FD NULL
92 #endif
93 
94 #define ALTERA_AVALON_UART_DEV_INSTANCE(name, d)       \
95   static altera_avalon_uart_dev d =                    \
96    {                                                   \
97      {                                                 \
98        ALT_LLIST_ENTRY,                                \
99        name##_NAME,                                    \
100        NULL, /* open */                                \
101        altera_avalon_uart_close_fd,                    \
102        altera_avalon_uart_read_fd,                     \
103        altera_avalon_uart_write_fd,                    \
104        NULL, /* lseek */                               \
105        NULL, /* fstat */                               \
106        ALTERA_AVALON_UART_IOCTL_FD,                    \
107      },                                                \
108      {                                                 \
109        (void*) name##_BASE,                            \
110        0,                                              \
111        0,                                              \
112        0,                                              \
113        0,                                              \
114        0,                                              \
115        ALTERA_AVALON_UART_TERMIOS(name##_STOP_BITS,    \
116                                (name##_PARITY == 'N'), \
117                                (name##_PARITY == 'O'), \
118                                name##_DATA_BITS,       \
119                                name##_USE_CTS_RTS,     \
120                                name##_BAUD)            \
121        ALTERA_AVALON_UART_FREQ(name)                   \
122        (name##_FIXED_BAUD ? ALT_AVALON_UART_FB : 0) |  \
123          (name##_USE_CTS_RTS ? ALT_AVALON_UART_FC : 0) \
124      }                                                 \
125    }
126 
127 #define ALTERA_AVALON_UART_DEV_INIT(name, d)           \
128   {                                                    \
129     ALTERA_AVALON_UART_STATE_INIT(name, d.state);      \
130                                                        \
131     /* make the device available to the system */      \
132     alt_dev_reg(&d.dev);                               \
133   }
134 
135 #endif /* fast driver */
136 
137 #ifdef __cplusplus
138 }
139 #endif /* __cplusplus */
140 
141 #endif /* __ALT_AVALON_UART_FD_H__ */
142