1 | /* |
---|
2 | * This file contains the TTY driver for the serial ports on the SCORE603e. |
---|
3 | * |
---|
4 | * This driver uses the termios pseudo driver. |
---|
5 | * |
---|
6 | * Currently only polled mode is supported. |
---|
7 | * |
---|
8 | * COPYRIGHT (c) 1989-2009. |
---|
9 | * On-Line Applications Research Corporation (OAR). |
---|
10 | * |
---|
11 | * The license and distribution terms for this file may be |
---|
12 | * found in the file LICENSE in this distribution or at |
---|
13 | * http://www.rtems.com/license/LICENSE. |
---|
14 | * |
---|
15 | * $Id$ |
---|
16 | */ |
---|
17 | |
---|
18 | #include <bsp.h> |
---|
19 | #include <rtems/libio.h> |
---|
20 | #include <stdlib.h> |
---|
21 | #include <assert.h> |
---|
22 | |
---|
23 | #include "consolebsp.h" |
---|
24 | #include <rtems/bspIo.h> |
---|
25 | |
---|
26 | /* |
---|
27 | * The Port Used for the Console interface is based upon which |
---|
28 | * debugger is being used. The SDS debugger uses a binary |
---|
29 | * interface on port 0 as part of the debugger. Thus port 0 can |
---|
30 | * not be used as the console port for the SDS debugger. |
---|
31 | */ |
---|
32 | |
---|
33 | #define USE_FOR_CONSOLE_DEF 0 |
---|
34 | int USE_FOR_CONSOLE = USE_FOR_CONSOLE_DEF; |
---|
35 | |
---|
36 | /* |
---|
37 | * |
---|
38 | * Console Device Driver Entry Points |
---|
39 | */ |
---|
40 | |
---|
41 | /* PAGE |
---|
42 | * |
---|
43 | * console_inbyte_nonblocking |
---|
44 | * |
---|
45 | * Console Termios polling input entry point. |
---|
46 | */ |
---|
47 | |
---|
48 | int console_inbyte_nonblocking( |
---|
49 | int minor |
---|
50 | ) |
---|
51 | { |
---|
52 | int port = minor; |
---|
53 | |
---|
54 | /* |
---|
55 | * verify port Number |
---|
56 | */ |
---|
57 | assert ( port < NUM_Z85C30_PORTS ); |
---|
58 | |
---|
59 | /* |
---|
60 | * return a character from the 85c30 port. |
---|
61 | */ |
---|
62 | return inbyte_nonblocking_85c30( &Ports_85C30[ port ] ); |
---|
63 | } |
---|
64 | |
---|
65 | rtems_device_driver console_close( |
---|
66 | rtems_device_major_number major, |
---|
67 | rtems_device_minor_number minor, |
---|
68 | void * arg |
---|
69 | ) |
---|
70 | { |
---|
71 | return rtems_termios_close (arg); |
---|
72 | } |
---|
73 | |
---|
74 | rtems_device_driver console_read( |
---|
75 | rtems_device_major_number major, |
---|
76 | rtems_device_minor_number minor, |
---|
77 | void * arg |
---|
78 | ) |
---|
79 | { |
---|
80 | return rtems_termios_read (arg); |
---|
81 | } |
---|
82 | |
---|
83 | rtems_device_driver console_write( |
---|
84 | rtems_device_major_number major, |
---|
85 | rtems_device_minor_number minor, |
---|
86 | void * arg |
---|
87 | ) |
---|
88 | { |
---|
89 | return rtems_termios_write (arg); |
---|
90 | } |
---|
91 | |
---|
92 | rtems_device_driver console_control( |
---|
93 | rtems_device_major_number major, |
---|
94 | rtems_device_minor_number minor, |
---|
95 | void * arg |
---|
96 | ) |
---|
97 | { |
---|
98 | return rtems_termios_ioctl (arg); |
---|
99 | } |
---|
100 | |
---|
101 | /* |
---|
102 | * Interrupt driven console IO |
---|
103 | */ |
---|
104 | |
---|
105 | #if CONSOLE_USE_INTERRUPTS |
---|
106 | |
---|
107 | rtems_isr console_isr( |
---|
108 | rtems_vector_number vector |
---|
109 | ) |
---|
110 | { |
---|
111 | int i; |
---|
112 | |
---|
113 | for (i=0; i < NUM_Z85C30_PORTS; i++){ |
---|
114 | ISR_85c30_Async( &Ports_85C30[i] ); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | void console_exit() |
---|
119 | { |
---|
120 | int i; |
---|
121 | volatile Ring_buffer_t *buffer; |
---|
122 | uint32_t ch; |
---|
123 | |
---|
124 | for ( i=0 ; i < NUM_Z85C30_PORTS ; i++ ) { |
---|
125 | |
---|
126 | buffer = &( Ports_85C30[i].Protocol->TX_Buffer); |
---|
127 | |
---|
128 | while ( !Ring_buffer_Is_empty( buffer ) ) { |
---|
129 | Ring_buffer_Remove_character( buffer, ch ); |
---|
130 | outbyte_polled_85c30( Ports_85C30[i].ctrl, ch ); |
---|
131 | } |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | void console_initialize_interrupts( void ) |
---|
136 | { |
---|
137 | volatile Ring_buffer_t *buffer; |
---|
138 | Console_Protocol *protocol; |
---|
139 | int i; |
---|
140 | |
---|
141 | for ( i=0 ; i < NUM_Z85C30_PORTS ; i++ ) { |
---|
142 | protocol = Ports_85C30[i].Protocol; |
---|
143 | |
---|
144 | /* |
---|
145 | * Initialize the ring buffer and set to not transmitting. |
---|
146 | */ |
---|
147 | buffer = &protocol->TX_Buffer; |
---|
148 | Ring_buffer_Initialize( buffer ); |
---|
149 | protocol->Is_TX_active = false; |
---|
150 | } |
---|
151 | |
---|
152 | /* |
---|
153 | * Connect each vector to the interupt service routine. |
---|
154 | */ |
---|
155 | for (i=0; i < NUM_Z85C30_CHIPS; i++) |
---|
156 | set_vector( console_isr, Chips_85C30[i].vector, 1 ); |
---|
157 | |
---|
158 | atexit( console_exit ); |
---|
159 | |
---|
160 | } |
---|
161 | void console_outbyte_interrupts( |
---|
162 | const Port_85C30_info *Port, |
---|
163 | char ch |
---|
164 | ); |
---|
165 | |
---|
166 | #endif |
---|
167 | |
---|
168 | /* PAGE |
---|
169 | * |
---|
170 | * console_initialize |
---|
171 | * |
---|
172 | * Routine called to initialize the console device driver. |
---|
173 | */ |
---|
174 | rtems_device_driver console_initialize( |
---|
175 | rtems_device_major_number major, |
---|
176 | rtems_device_minor_number minor, |
---|
177 | void *arg |
---|
178 | ) |
---|
179 | { |
---|
180 | rtems_status_code status; |
---|
181 | rtems_device_minor_number console; |
---|
182 | int port, chip, p0,p1; |
---|
183 | |
---|
184 | /* |
---|
185 | * initialize the termio interface. |
---|
186 | */ |
---|
187 | rtems_termios_initialize(); |
---|
188 | |
---|
189 | /* |
---|
190 | * Register Device Names |
---|
191 | */ |
---|
192 | console = USE_FOR_CONSOLE; |
---|
193 | status = rtems_io_register_name( "/dev/console", major, console ); |
---|
194 | if (status != RTEMS_SUCCESSFUL) |
---|
195 | rtems_fatal_error_occurred(status); |
---|
196 | |
---|
197 | /* |
---|
198 | * Initialize Hardware |
---|
199 | */ |
---|
200 | |
---|
201 | /* |
---|
202 | * INITIALIZE_COM_PORTS is defined in the linker script. If it is |
---|
203 | * true all serial chips on the board are to be reset at startup |
---|
204 | * otherwise the reset is assumed to occur elsewhere (ie. in the |
---|
205 | * debugger...) |
---|
206 | */ |
---|
207 | #if ( INITIALIZE_COM_PORTS ) |
---|
208 | /* |
---|
209 | * Force to perform a hardware reset w/o |
---|
210 | * Master interrupt enable via register 9 |
---|
211 | */ |
---|
212 | |
---|
213 | for (port=0; port<NUM_Z85C30_PORTS; port++){ |
---|
214 | p0 = port; |
---|
215 | port++; |
---|
216 | p1 = port; |
---|
217 | Reset_85c30_chip( Ports_85C30[p0].ctrl, Ports_85C30[p1].ctrl ); |
---|
218 | } |
---|
219 | #else |
---|
220 | /* TEMP - To see if this makes a diff with the new ports. |
---|
221 | * Never reset chip 1 when using the chip as a monitor |
---|
222 | */ |
---|
223 | for (port=2; port<NUM_Z85C30_PORTS; port++){ |
---|
224 | p0 = port; |
---|
225 | port++; |
---|
226 | p1 = port; |
---|
227 | Reset_85c30_chip( Ports_85C30[p0].ctrl, Ports_85C30[p1].ctrl ); |
---|
228 | } |
---|
229 | #endif |
---|
230 | |
---|
231 | /* |
---|
232 | * Initialize each port. |
---|
233 | * Note: the ports are numbered such that 0,1 are on the first chip |
---|
234 | * 2,3 are on the second .... |
---|
235 | */ |
---|
236 | |
---|
237 | for (port=1; port<NUM_Z85C30_PORTS; port++) { |
---|
238 | chip = port >> 1; |
---|
239 | initialize_85c30_port( &Ports_85C30[port] ); |
---|
240 | } |
---|
241 | |
---|
242 | #if CONSOLE_USE_INTERRUPTS |
---|
243 | console_initialize_interrupts(); |
---|
244 | #endif |
---|
245 | |
---|
246 | return RTEMS_SUCCESSFUL; |
---|
247 | } |
---|
248 | |
---|
249 | /* PAGE |
---|
250 | * |
---|
251 | * console_write_support |
---|
252 | * |
---|
253 | * Console Termios output entry point. |
---|
254 | * |
---|
255 | */ |
---|
256 | int console_write_support( |
---|
257 | int minor, |
---|
258 | const char *buf, |
---|
259 | int len) |
---|
260 | { |
---|
261 | int nwrite = 0; |
---|
262 | volatile uint8_t *csr; |
---|
263 | int port = minor; |
---|
264 | |
---|
265 | /* |
---|
266 | * verify port Number |
---|
267 | */ |
---|
268 | assert ( port < NUM_Z85C30_PORTS ); |
---|
269 | |
---|
270 | /* |
---|
271 | * Set the csr based upon the port number. |
---|
272 | */ |
---|
273 | csr = Ports_85C30[ port ].ctrl; |
---|
274 | |
---|
275 | /* |
---|
276 | * poll each byte in the string out of the port. |
---|
277 | */ |
---|
278 | while (nwrite < len) { |
---|
279 | #if (CONSOLE_USE_INTERRUPTS) |
---|
280 | console_outbyte_interrupts( &Ports_85C30[ port ], *buf++ ); |
---|
281 | #else |
---|
282 | outbyte_polled_85c30( csr, *buf++ ); |
---|
283 | #endif |
---|
284 | nwrite++; |
---|
285 | } |
---|
286 | |
---|
287 | /* |
---|
288 | * return the number of bytes written. |
---|
289 | */ |
---|
290 | return nwrite; |
---|
291 | } |
---|
292 | |
---|
293 | /* PAGE |
---|
294 | * |
---|
295 | * console_open |
---|
296 | * |
---|
297 | * open a port as a termios console. |
---|
298 | * |
---|
299 | */ |
---|
300 | rtems_device_driver console_open( |
---|
301 | rtems_device_major_number major, |
---|
302 | rtems_device_minor_number minor, |
---|
303 | void * arg |
---|
304 | ) |
---|
305 | { |
---|
306 | rtems_status_code sc; |
---|
307 | int port = minor; |
---|
308 | #if (CONSOLE_USE_INTERRUPTS) |
---|
309 | rtems_libio_open_close_args_t *args = arg; |
---|
310 | static const rtems_termios_callbacks intrCallbacks = { |
---|
311 | NULL, /* firstOpen */ |
---|
312 | NULL, /* lastClose */ |
---|
313 | NULL, /* pollRead */ |
---|
314 | console_write_support, /* write */ |
---|
315 | NULL, /* setAttributes */ |
---|
316 | NULL, /* stopRemoteTx */ |
---|
317 | NULL, /* startRemoteTx */ |
---|
318 | 1 /* outputUsesInterrupts */ |
---|
319 | }; |
---|
320 | #else |
---|
321 | static const rtems_termios_callbacks pollCallbacks = { |
---|
322 | NULL, /* firstOpen */ |
---|
323 | NULL, /* lastClose */ |
---|
324 | console_inbyte_nonblocking, /* pollRead */ |
---|
325 | console_write_support, /* write */ |
---|
326 | NULL, /* setAttributes */ |
---|
327 | NULL, /* stopRemoteTx */ |
---|
328 | NULL, /* startRemoteTx */ |
---|
329 | 0 /* outputUsesInterrupts */ |
---|
330 | }; |
---|
331 | #endif |
---|
332 | |
---|
333 | /* |
---|
334 | * Verify the minor number is valid. |
---|
335 | */ |
---|
336 | if (minor < 0) |
---|
337 | return RTEMS_INVALID_NUMBER; |
---|
338 | |
---|
339 | if ( port > NUM_Z85C30_PORTS ) |
---|
340 | return RTEMS_INVALID_NUMBER; |
---|
341 | |
---|
342 | /* |
---|
343 | * open the port as a termios console driver. |
---|
344 | */ |
---|
345 | |
---|
346 | #if (CONSOLE_USE_INTERRUPTS) |
---|
347 | sc = rtems_termios_open( major, minor, arg, &intrCallbacks ); |
---|
348 | |
---|
349 | Ports_85C30[ minor ].Protocol->console_termios_data = args->iop->data1; |
---|
350 | #else |
---|
351 | sc = rtems_termios_open( major, minor, arg, &pollCallbacks ); |
---|
352 | #endif |
---|
353 | |
---|
354 | return sc; |
---|
355 | } |
---|
356 | |
---|
357 | #if (CONSOLE_USE_INTERRUPTS) |
---|
358 | |
---|
359 | /* |
---|
360 | * console_outbyte_interrupts |
---|
361 | * |
---|
362 | * This routine transmits a character out. |
---|
363 | * |
---|
364 | * Input parameters: |
---|
365 | * port - port to transmit character to |
---|
366 | * ch - character to be transmitted |
---|
367 | * |
---|
368 | * Output parameters: NONE |
---|
369 | * |
---|
370 | * Return values: NONE |
---|
371 | */ |
---|
372 | void console_outbyte_interrupts( |
---|
373 | const Port_85C30_info *Port, |
---|
374 | char ch |
---|
375 | ) |
---|
376 | { |
---|
377 | Console_Protocol *protocol; |
---|
378 | uint32_t isrlevel; |
---|
379 | |
---|
380 | protocol = Port->Protocol; |
---|
381 | |
---|
382 | /* |
---|
383 | * If this is the first character then we need to prime the pump |
---|
384 | */ |
---|
385 | |
---|
386 | if ( protocol->Is_TX_active == false ) { |
---|
387 | |
---|
388 | rtems_interrupt_disable( isrlevel ); |
---|
389 | protocol->Is_TX_active = true; |
---|
390 | outbyte_polled_85c30( Port->ctrl, ch ); |
---|
391 | rtems_interrupt_enable( isrlevel ); |
---|
392 | |
---|
393 | return; |
---|
394 | } |
---|
395 | |
---|
396 | while ( Ring_buffer_Is_full( &protocol->TX_Buffer ) ); |
---|
397 | |
---|
398 | Ring_buffer_Add_character( &protocol->TX_Buffer, ch ); |
---|
399 | } |
---|
400 | |
---|
401 | #endif |
---|
402 | |
---|
403 | /* const char arg to be compatible with BSP_output_char decl. */ |
---|
404 | void |
---|
405 | debug_putc_onlcr(const char c) |
---|
406 | { |
---|
407 | int console; |
---|
408 | volatile uint8_t *csr; |
---|
409 | uint32_t isrlevel; |
---|
410 | |
---|
411 | console = USE_FOR_CONSOLE; |
---|
412 | csr = Ports_85C30[ console ].ctrl; |
---|
413 | |
---|
414 | if ('\n'==c){ |
---|
415 | rtems_interrupt_disable( isrlevel ); |
---|
416 | outbyte_polled_85c30( csr, '\r' ); |
---|
417 | asm volatile("isync"); |
---|
418 | rtems_interrupt_enable( isrlevel ); |
---|
419 | } |
---|
420 | |
---|
421 | rtems_interrupt_disable( isrlevel ); |
---|
422 | outbyte_polled_85c30( csr, c ); |
---|
423 | asm volatile("isync"); |
---|
424 | rtems_interrupt_enable( isrlevel ); |
---|
425 | } |
---|
426 | |
---|
427 | BSP_output_char_function_type BSP_output_char = debug_putc_onlcr; |
---|
428 | /* const char arg to be compatible with BSP_output_char decl. */ |
---|
429 | |
---|