1 /******************************************************************************
2 *                                                                             *
3 * License Agreement                                                           *
4 *                                                                             *
5 * Copyright (c) 2006 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_JTAG_UART_H__
30 #define __ALT_AVALON_JTAG_UART_H__
31 
32 #include <stddef.h>
33 
34 #include "sys/alt_alarm.h"
35 #include "sys/alt_warning.h"
36 
37 #include "os/alt_sem.h"
38 #include "os/alt_flag.h"
39 
40 #ifdef __cplusplus
41 extern "C"
42 {
43 #endif /* __cplusplus */
44 
45 /*
46  * If the user wants all drivers to be small rather than fast then make sure
47  * this one is marked as needing to be small.
48  */
49 #if defined ALT_USE_SMALL_DRIVERS && !defined ALTERA_AVALON_JTAG_UART_SMALL
50 #define ALTERA_AVALON_JTAG_UART_SMALL
51 #endif
52 
53 /*
54  * If the user wants to ignore FIFO full error after timeout
55   */
56 #if defined ALT_JTAG_UART_IGNORE_FIFO_FULL_ERROR && !defined ALTERA_AVALON_JTAG_UART_IGNORE_FIFO_FULL_ERROR
57 #define ALTERA_AVALON_JTAG_UART_IGNORE_FIFO_FULL_ERROR
58 #endif
59 
60 /*
61  * Constants that can be overriden.
62  */
63 #ifndef ALTERA_AVALON_JTAG_UART_DEFAULT_TIMEOUT
64 #define ALTERA_AVALON_JTAG_UART_DEFAULT_TIMEOUT 10
65 #endif
66 
67 #ifndef ALTERA_AVALON_JTAG_UART_BUF_LEN
68 #define ALTERA_AVALON_JTAG_UART_BUF_LEN 2048
69 #endif
70 
71 /*
72  * ALT_JTAG_UART_READ_RDY and ALT_JTAG_UART_WRITE_RDY are the bitmasks
73  * that define uC/OS-II event flags that are releated to this device.
74  *
75  * ALT_JTAG_UART_READ_RDY indicates that there is read data in the buffer
76  * ready to be processed. ALT_JTAG_UART_WRITE_RDY indicates that the transmitter is
77  * ready for more data.
78  */
79 #define ALT_JTAG_UART_READ_RDY  0x1
80 #define ALT_JTAG_UART_WRITE_RDY 0x2
81 #define ALT_JTAG_UART_TIMEOUT   0x4
82 
83 /*
84  * State structure definition. Each instance of the driver uses one
85  * of these structures to hold its associated state.
86  */
87 
88 typedef struct altera_avalon_jtag_uart_state_s
89 {
90   unsigned int base;
91 
92 #ifndef ALTERA_AVALON_JTAG_UART_SMALL
93 
94   unsigned int  timeout; /* Timeout until host is assumed inactive */
95   alt_alarm     alarm;
96   unsigned int  irq_enable;
97   unsigned int  host_inactive;
98 
99   ALT_SEM      (read_lock)
100   ALT_SEM      (write_lock)
101   ALT_FLAG_GRP (events)
102 
103   /* The variables below are volatile because they are modified by the
104    * interrupt routine.  Making them volatile and reading them atomically
105    * means that we don't need any large critical sections.
106    */
107   volatile unsigned int rx_in;
108   unsigned int  rx_out;
109   unsigned int  tx_in;
110   volatile unsigned int tx_out;
111   char          rx_buf[ALTERA_AVALON_JTAG_UART_BUF_LEN];
112   char          tx_buf[ALTERA_AVALON_JTAG_UART_BUF_LEN];
113 
114 #endif /* !ALTERA_AVALON_JTAG_UART_SMALL */
115 
116 } altera_avalon_jtag_uart_state;
117 
118 /*
119  * Macros used by alt_sys_init when the ALT file descriptor facility isn't used.
120  */
121 
122 #ifdef ALTERA_AVALON_JTAG_UART_SMALL
123 
124 #define ALTERA_AVALON_JTAG_UART_STATE_INSTANCE(name, state)    \
125   altera_avalon_jtag_uart_state state =                  \
126   {                                                      \
127     name##_BASE,                                         \
128   }
129 
130 #define ALTERA_AVALON_JTAG_UART_STATE_INIT(name, state)
131 
132 #else /* !ALTERA_AVALON_JTAG_UART_SMALL */
133 
134 #define ALTERA_AVALON_JTAG_UART_STATE_INSTANCE(name, state)   \
135   altera_avalon_jtag_uart_state state =                  \
136   {                                                      \
137     name##_BASE,                                         \
138     ALTERA_AVALON_JTAG_UART_DEFAULT_TIMEOUT,             \
139   }
140 
141 /*
142  * Externally referenced routines
143  */
144 extern void altera_avalon_jtag_uart_init(altera_avalon_jtag_uart_state* sp,
145                                         int irq_controller_id, int irq);
146 
147 #define ALTERA_AVALON_JTAG_UART_STATE_INIT(name, state)                      \
148   {                                                                          \
149     if (name##_IRQ == ALT_IRQ_NOT_CONNECTED)                                 \
150     {                                                                        \
151       ALT_LINK_ERROR ("Error: Interrupt not connected for " #name ". "       \
152                       "You have selected the interrupt driven version of "   \
153                       "the ALTERA Avalon JTAG UART driver, but the "         \
154                       "interrupt is not connected for this device. You can " \
155                       "select a polled mode driver by checking the 'small "  \
156                       "driver' option in the HAL configuration window, or "  \
157                       "by using the -DALTERA_AVALON_JTAG_UART_SMALL "        \
158                       "preprocessor flag.");                                 \
159     }                                                                        \
160     else                                                                     \
161       altera_avalon_jtag_uart_init(&state,                                   \
162                                    name##_IRQ_INTERRUPT_CONTROLLER_ID,       \
163                                    name##_IRQ);                              \
164   }
165 
166 #endif /* ALTERA_AVALON_JTAG_UART_SMALL */
167 
168 /*
169  * Include in case non-direct version of driver required.
170  */
171 #include "altera_avalon_jtag_uart_fd.h"
172 
173 /*
174  * Map alt_sys_init macros to direct or non-direct versions.
175  */
176 #ifdef ALT_USE_DIRECT_DRIVERS
177 
178 #define ALTERA_AVALON_JTAG_UART_INSTANCE(name, state) \
179    ALTERA_AVALON_JTAG_UART_STATE_INSTANCE(name, state)
180 #define ALTERA_AVALON_JTAG_UART_INIT(name, state) \
181    ALTERA_AVALON_JTAG_UART_STATE_INIT(name, state)
182 
183 #else /* !ALT_USE_DIRECT_DRIVERS */
184 
185 #define ALTERA_AVALON_JTAG_UART_INSTANCE(name, dev) \
186    ALTERA_AVALON_JTAG_UART_DEV_INSTANCE(name, dev)
187 #define ALTERA_AVALON_JTAG_UART_INIT(name, dev) \
188    ALTERA_AVALON_JTAG_UART_DEV_INIT(name, dev)
189 
190 #endif /* ALT_USE_DIRECT_DRIVERS */
191 
192 #ifdef __cplusplus
193 }
194 #endif /* __cplusplus */
195 
196 #endif /* __ALT_AVALON_JTAG_UART_H__ */
197