[7f96eef] | 1 | /* |
---|
[a7e2729] | 2 | * This file contains the TTY driver for the serial ports on the erc32. |
---|
[7f96eef] | 3 | * |
---|
[a7e2729] | 4 | * This driver uses the termios pseudo driver. |
---|
[7f96eef] | 5 | * |
---|
[03f2154e] | 6 | * COPYRIGHT (c) 1989-1997. |
---|
[7f96eef] | 7 | * On-Line Applications Research Corporation (OAR). |
---|
[03f2154e] | 8 | * Copyright assigned to U.S. Government, 1994. |
---|
[7f96eef] | 9 | * |
---|
[98e4ebf5] | 10 | * The license and distribution terms for this file may be |
---|
| 11 | * found in the file LICENSE in this distribution or at |
---|
[03f2154e] | 12 | * http://www.OARcorp.com/rtems/license.html. |
---|
[7f96eef] | 13 | * |
---|
| 14 | * $Id$ |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | #include <bsp.h> |
---|
| 18 | #include <rtems/libio.h> |
---|
| 19 | #include <stdlib.h> |
---|
[a7e2729] | 20 | #include <assert.h> |
---|
[7f96eef] | 21 | |
---|
| 22 | |
---|
[a7e2729] | 23 | #undef CONSOLE_USE_POLLED |
---|
| 24 | #define CONSOLE_USE_INTERRUPTS |
---|
[7f96eef] | 25 | |
---|
| 26 | /* |
---|
| 27 | * Should we use a polled or interrupt drived console? |
---|
| 28 | * |
---|
[a7e2729] | 29 | * NOTE: This is defined in the custom/erc32.cfg file. |
---|
[7f96eef] | 30 | * |
---|
[a7e2729] | 31 | * WARNING: In sis 1.6, it did not appear that the UART interrupts |
---|
[7f96eef] | 32 | * worked in a desirable fashion. Immediately upon writing |
---|
| 33 | * a character into the TX buffer, an interrupt was generated. |
---|
| 34 | * This did not allow enough time for the program to put more |
---|
| 35 | * characters in the buffer. So every character resulted in |
---|
| 36 | * "priming" the transmitter. This effectively results in |
---|
| 37 | * in a polled console with a useless interrupt per character |
---|
| 38 | * on output. It is reasonable to assume that input does not |
---|
| 39 | * share this problem although it was not investigated. |
---|
[a7e2729] | 40 | * |
---|
[7f96eef] | 41 | */ |
---|
| 42 | |
---|
[a7e2729] | 43 | /* |
---|
| 44 | * console_outbyte_polled |
---|
[7f96eef] | 45 | * |
---|
[a7e2729] | 46 | * This routine transmits a character using polling. |
---|
[7f96eef] | 47 | */ |
---|
[a7e2729] | 48 | |
---|
| 49 | void console_outbyte_polled( |
---|
| 50 | int port, |
---|
| 51 | char ch |
---|
[7f96eef] | 52 | ) |
---|
| 53 | { |
---|
[a7e2729] | 54 | if ( port == 0 ) { |
---|
| 55 | while ( (ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_THEA) == 0 ); |
---|
| 56 | ERC32_MEC.UART_Channel_A = (int) ch; |
---|
| 57 | return; |
---|
| 58 | } |
---|
[7f96eef] | 59 | |
---|
[a7e2729] | 60 | while ( (ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_THEB) == 0 ); |
---|
| 61 | ERC32_MEC.UART_Channel_B = (int) ch; |
---|
[7f96eef] | 62 | } |
---|
| 63 | |
---|
[a7e2729] | 64 | /* |
---|
| 65 | * console_inbyte_nonblocking |
---|
[7f96eef] | 66 | * |
---|
[a7e2729] | 67 | * This routine polls for a character. |
---|
[7f96eef] | 68 | */ |
---|
| 69 | |
---|
[a7e2729] | 70 | int console_inbyte_nonblocking( |
---|
| 71 | int port, |
---|
| 72 | char *c |
---|
| 73 | ) |
---|
[7f96eef] | 74 | { |
---|
| 75 | int UStat; |
---|
| 76 | |
---|
[a7e2729] | 77 | UStat = ERC32_MEC.UART_Status; |
---|
[7f96eef] | 78 | |
---|
[a7e2729] | 79 | switch (port) { |
---|
[7f96eef] | 80 | |
---|
[a7e2729] | 81 | case 0: |
---|
| 82 | if (UStat & ERC32_MEC_UART_STATUS_ERRA) { |
---|
| 83 | ERC32_MEC.UART_Status = ERC32_MEC_UART_STATUS_CLRA; |
---|
| 84 | ERC32_MEC.Control = ERC32_MEC.Control; |
---|
| 85 | } |
---|
[7f96eef] | 86 | |
---|
[a7e2729] | 87 | if ((UStat & ERC32_MEC_UART_STATUS_DRA) == 0) |
---|
| 88 | return 0; |
---|
| 89 | *c = (char) ERC32_MEC.UART_Channel_A; |
---|
| 90 | return 1; |
---|
| 91 | |
---|
| 92 | case 1: |
---|
| 93 | if (UStat & ERC32_MEC_UART_STATUS_ERRB) { |
---|
| 94 | ERC32_MEC.UART_Status = ERC32_MEC_UART_STATUS_CLRB; |
---|
| 95 | ERC32_MEC.Control = ERC32_MEC.Control; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | if ((UStat & ERC32_MEC_UART_STATUS_DRB) == 0) |
---|
| 99 | return 0; |
---|
| 100 | *c = (char) ERC32_MEC.UART_Channel_B; |
---|
| 101 | return 1; |
---|
| 102 | |
---|
| 103 | default: |
---|
| 104 | assert( 0 ); |
---|
[7f96eef] | 105 | } |
---|
| 106 | |
---|
[a7e2729] | 107 | return 0; |
---|
[7f96eef] | 108 | } |
---|
| 109 | |
---|
| 110 | /* |
---|
| 111 | * Interrupt driven console IO |
---|
| 112 | */ |
---|
| 113 | |
---|
| 114 | #ifdef CONSOLE_USE_INTERRUPTS |
---|
| 115 | |
---|
| 116 | /* |
---|
| 117 | * Buffers between task and ISRs |
---|
| 118 | */ |
---|
| 119 | |
---|
| 120 | #include <ringbuf.h> |
---|
| 121 | |
---|
| 122 | Ring_buffer_t TX_Buffer[ 2 ]; |
---|
| 123 | boolean Is_TX_active[ 2 ]; |
---|
| 124 | |
---|
[a7e2729] | 125 | void *console_termios_data[ 2 ]; |
---|
| 126 | |
---|
[7f96eef] | 127 | /* |
---|
| 128 | * console_isr_a |
---|
| 129 | * |
---|
| 130 | * This routine is the console interrupt handler for Channel A. |
---|
| 131 | * |
---|
| 132 | * Input parameters: |
---|
| 133 | * vector - vector number |
---|
| 134 | * |
---|
| 135 | * Output parameters: NONE |
---|
| 136 | * |
---|
| 137 | * Return values: NONE |
---|
| 138 | */ |
---|
| 139 | |
---|
| 140 | rtems_isr console_isr_a( |
---|
| 141 | rtems_vector_number vector |
---|
| 142 | ) |
---|
| 143 | { |
---|
| 144 | char ch; |
---|
| 145 | int UStat; |
---|
| 146 | |
---|
| 147 | if ( (UStat = ERC32_MEC.UART_Status) & ERC32_MEC_UART_STATUS_DRA ) { |
---|
| 148 | if (UStat & ERC32_MEC_UART_STATUS_ERRA) { |
---|
| 149 | ERC32_MEC.UART_Status = ERC32_MEC_UART_STATUS_CLRA; |
---|
| 150 | ERC32_MEC.Control = ERC32_MEC.Control; |
---|
| 151 | } |
---|
| 152 | ch = ERC32_MEC.UART_Channel_A; |
---|
[a7e2729] | 153 | |
---|
| 154 | rtems_termios_enqueue_raw_characters( console_termios_data[ 0 ], &ch, 1 ); |
---|
[7f96eef] | 155 | } |
---|
| 156 | |
---|
| 157 | if ( ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_THEA ) { |
---|
| 158 | if ( !Ring_buffer_Is_empty( &TX_Buffer[ 0 ] ) ) { |
---|
| 159 | Ring_buffer_Remove_character( &TX_Buffer[ 0 ], ch ); |
---|
| 160 | ERC32_MEC.UART_Channel_A = (unsigned32) ch; |
---|
| 161 | } else |
---|
| 162 | Is_TX_active[ 0 ] = FALSE; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | ERC32_Clear_interrupt( ERC32_INTERRUPT_UART_A_RX_TX ); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | /* |
---|
| 169 | * console_isr_b |
---|
| 170 | * |
---|
| 171 | * This routine is the console interrupt handler for Channel B. |
---|
| 172 | * |
---|
| 173 | * Input parameters: |
---|
| 174 | * vector - vector number |
---|
| 175 | * |
---|
| 176 | * Output parameters: NONE |
---|
| 177 | * |
---|
| 178 | * Return values: NONE |
---|
| 179 | */ |
---|
| 180 | |
---|
| 181 | rtems_isr console_isr_b( |
---|
| 182 | rtems_vector_number vector |
---|
| 183 | ) |
---|
| 184 | { |
---|
| 185 | char ch; |
---|
| 186 | int UStat; |
---|
| 187 | |
---|
| 188 | if ( (UStat = ERC32_MEC.UART_Status) & ERC32_MEC_UART_STATUS_DRB ) { |
---|
| 189 | if (UStat & ERC32_MEC_UART_STATUS_ERRB) { |
---|
| 190 | ERC32_MEC.UART_Status = ERC32_MEC_UART_STATUS_CLRB; |
---|
| 191 | ERC32_MEC.Control = ERC32_MEC.Control; |
---|
| 192 | } |
---|
| 193 | ch = ERC32_MEC.UART_Channel_B; |
---|
[a7e2729] | 194 | rtems_termios_enqueue_raw_characters( console_termios_data[ 1 ], &ch, 1 ); |
---|
| 195 | |
---|
[7f96eef] | 196 | } |
---|
| 197 | |
---|
| 198 | if ( ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_THEB ) { |
---|
| 199 | if ( !Ring_buffer_Is_empty( &TX_Buffer[ 1 ] ) ) { |
---|
| 200 | Ring_buffer_Remove_character( &TX_Buffer[ 1 ], ch ); |
---|
| 201 | ERC32_MEC.UART_Channel_B = (unsigned32) ch; |
---|
| 202 | } else |
---|
| 203 | Is_TX_active[ 1 ] = FALSE; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | ERC32_Clear_interrupt( ERC32_INTERRUPT_UART_B_RX_TX ); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | /* |
---|
| 210 | * console_exit |
---|
| 211 | * |
---|
| 212 | * This routine allows the console to exit by masking its associated interrupt |
---|
| 213 | * vectors. |
---|
| 214 | * |
---|
| 215 | * Input parameters: NONE |
---|
| 216 | * |
---|
| 217 | * Output parameters: NONE |
---|
| 218 | * |
---|
| 219 | * Return values: NONE |
---|
| 220 | */ |
---|
| 221 | |
---|
| 222 | void console_exit() |
---|
| 223 | { |
---|
| 224 | rtems_unsigned32 port; |
---|
| 225 | rtems_unsigned32 ch; |
---|
| 226 | |
---|
| 227 | /* |
---|
| 228 | * Although the interrupts for the UART are unmasked, the PIL is set to |
---|
| 229 | * disable all external interrupts. So we might as well do this first. |
---|
| 230 | */ |
---|
| 231 | |
---|
| 232 | ERC32_Mask_interrupt( ERC32_INTERRUPT_UART_A_RX_TX ); |
---|
| 233 | ERC32_Mask_interrupt( ERC32_INTERRUPT_UART_B_RX_TX ); |
---|
| 234 | |
---|
| 235 | for ( port=0 ; port <= 1 ; port++ ) { |
---|
| 236 | while ( !Ring_buffer_Is_empty( &TX_Buffer[ port ] ) ) { |
---|
| 237 | Ring_buffer_Remove_character( &TX_Buffer[ port ], ch ); |
---|
| 238 | console_outbyte_polled( port, ch ); |
---|
| 239 | } |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | /* |
---|
| 243 | * Now wait for all the data to actually get out ... the send register |
---|
| 244 | * should be empty. |
---|
| 245 | */ |
---|
| 246 | |
---|
| 247 | while ( (ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_THEA) != |
---|
| 248 | ERC32_MEC_UART_STATUS_THEA ); |
---|
| 249 | |
---|
| 250 | while ( (ERC32_MEC.UART_Status & ERC32_MEC_UART_STATUS_THEB) != |
---|
| 251 | ERC32_MEC_UART_STATUS_THEB ); |
---|
| 252 | |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | #define CONSOLE_UART_A_TRAP ERC32_TRAP_TYPE( ERC32_INTERRUPT_UART_A_RX_TX ) |
---|
| 256 | #define CONSOLE_UART_B_TRAP ERC32_TRAP_TYPE( ERC32_INTERRUPT_UART_B_RX_TX ) |
---|
| 257 | |
---|
| 258 | /* |
---|
| 259 | * console_initialize_interrupts |
---|
| 260 | * |
---|
| 261 | * This routine initializes the console's receive and transmit |
---|
| 262 | * ring buffers and loads the appropriate vectors to handle the interrupts. |
---|
| 263 | * |
---|
| 264 | * Input parameters: NONE |
---|
| 265 | * |
---|
| 266 | * Output parameters: NONE |
---|
| 267 | * |
---|
| 268 | * Return values: NONE |
---|
| 269 | */ |
---|
| 270 | |
---|
| 271 | #ifdef RDB_BREAK_IN |
---|
[2f93371] | 272 | extern unsigned32 trap_table[]; |
---|
[7f96eef] | 273 | #endif |
---|
| 274 | |
---|
[a7e2729] | 275 | void console_initialize_interrupts( void ) |
---|
[7f96eef] | 276 | { |
---|
| 277 | Ring_buffer_Initialize( &TX_Buffer[ 0 ] ); |
---|
| 278 | Ring_buffer_Initialize( &TX_Buffer[ 1 ] ); |
---|
| 279 | |
---|
| 280 | Is_TX_active[ 0 ] = FALSE; |
---|
| 281 | Is_TX_active[ 1 ] = FALSE; |
---|
| 282 | |
---|
| 283 | atexit( console_exit ); |
---|
| 284 | |
---|
| 285 | set_vector( console_isr_a, CONSOLE_UART_A_TRAP, 1 ); |
---|
| 286 | #ifdef RDB_BREAK_IN |
---|
| 287 | if (trap_table[0x150/4] == 0x91d02000) |
---|
| 288 | #endif |
---|
| 289 | set_vector( console_isr_b, CONSOLE_UART_B_TRAP, 1 ); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | /* |
---|
| 293 | * console_outbyte_interrupts |
---|
| 294 | * |
---|
| 295 | * This routine transmits a character out. |
---|
| 296 | * |
---|
| 297 | * Input parameters: |
---|
| 298 | * port - port to transmit character to |
---|
| 299 | * ch - character to be transmitted |
---|
| 300 | * |
---|
| 301 | * Output parameters: NONE |
---|
| 302 | * |
---|
| 303 | * Return values: NONE |
---|
| 304 | */ |
---|
| 305 | |
---|
| 306 | void console_outbyte_interrupts( |
---|
| 307 | int port, |
---|
| 308 | char ch |
---|
| 309 | ) |
---|
| 310 | { |
---|
| 311 | /* |
---|
| 312 | * If this is the first character then we need to prime the pump |
---|
| 313 | */ |
---|
| 314 | |
---|
| 315 | if ( Is_TX_active[ port ] == FALSE ) { |
---|
| 316 | Is_TX_active[ port ] = TRUE; |
---|
| 317 | console_outbyte_polled( port, ch ); |
---|
| 318 | return; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | while ( Ring_buffer_Is_full( &TX_Buffer[ port ] ) ); |
---|
| 322 | |
---|
| 323 | Ring_buffer_Add_character( &TX_Buffer[ port ], ch ); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | #endif /* CONSOLE_USE_INTERRUPTS */ |
---|
| 327 | |
---|
| 328 | /* |
---|
| 329 | * DEBUG_puts |
---|
| 330 | * |
---|
| 331 | * This should be safe in the event of an error. It attempts to insure |
---|
| 332 | * that no TX empty interrupts occur while it is doing polled IO. Then |
---|
| 333 | * it restores the state of that external interrupt. |
---|
| 334 | * |
---|
| 335 | * Input parameters: |
---|
| 336 | * string - pointer to debug output string |
---|
| 337 | * |
---|
| 338 | * Output parameters: NONE |
---|
| 339 | * |
---|
| 340 | * Return values: NONE |
---|
| 341 | */ |
---|
| 342 | |
---|
| 343 | void DEBUG_puts( |
---|
| 344 | char *string |
---|
| 345 | ) |
---|
| 346 | { |
---|
| 347 | char *s; |
---|
| 348 | unsigned32 old_level; |
---|
| 349 | |
---|
| 350 | ERC32_Disable_interrupt( ERC32_INTERRUPT_UART_A_RX_TX, old_level ); |
---|
| 351 | for ( s = string ; *s ; s++ ) |
---|
| 352 | console_outbyte_polled( 0, *s ); |
---|
| 353 | |
---|
| 354 | console_outbyte_polled( 0, '\r' ); |
---|
| 355 | console_outbyte_polled( 0, '\n' ); |
---|
| 356 | ERC32_Restore_interrupt( ERC32_INTERRUPT_UART_A_RX_TX, old_level ); |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | |
---|
| 360 | /* |
---|
[a7e2729] | 361 | * Console Termios Support Entry Points |
---|
[7f96eef] | 362 | * |
---|
[a7e2729] | 363 | */ |
---|
| 364 | |
---|
| 365 | int console_write_support (int minor, char *buf, int len) |
---|
| 366 | { |
---|
| 367 | int nwrite = 0; |
---|
| 368 | |
---|
| 369 | while (nwrite < len) { |
---|
| 370 | #if defined(CONSOLE_USE_INTERRUPTS) |
---|
| 371 | console_outbyte_polled( minor, *buf++ ); |
---|
| 372 | #else |
---|
| 373 | console_outbyte_interrupt( minor, *buf++ ); |
---|
| 374 | #endif |
---|
| 375 | nwrite++; |
---|
| 376 | } |
---|
| 377 | return nwrite; |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | /* |
---|
| 381 | * Console Device Driver Entry Points |
---|
[7f96eef] | 382 | * |
---|
| 383 | */ |
---|
| 384 | |
---|
[a7e2729] | 385 | rtems_device_driver console_initialize( |
---|
| 386 | rtems_device_major_number major, |
---|
| 387 | rtems_device_minor_number minor, |
---|
| 388 | void *arg |
---|
| 389 | ) |
---|
| 390 | { |
---|
| 391 | rtems_status_code status; |
---|
| 392 | |
---|
| 393 | rtems_termios_initialize(); |
---|
| 394 | |
---|
| 395 | /* |
---|
| 396 | * Register Device Names |
---|
| 397 | */ |
---|
| 398 | |
---|
| 399 | status = rtems_io_register_name( "/dev/console", major, 0 ); |
---|
| 400 | if (status != RTEMS_SUCCESSFUL) |
---|
| 401 | rtems_fatal_error_occurred(status); |
---|
| 402 | |
---|
| 403 | status = rtems_io_register_name( "/dev/console_b", major, 1 ); |
---|
| 404 | if (status != RTEMS_SUCCESSFUL) |
---|
| 405 | rtems_fatal_error_occurred(status); |
---|
| 406 | |
---|
| 407 | /* |
---|
| 408 | * Initialize Hardware |
---|
| 409 | */ |
---|
| 410 | |
---|
| 411 | #ifdef CONSOLE_USE_INTERRUPTS |
---|
| 412 | console_initialize_interrupts(); |
---|
| 413 | #endif |
---|
| 414 | |
---|
| 415 | return RTEMS_SUCCESSFUL; |
---|
| 416 | } |
---|
| 417 | |
---|
[7f96eef] | 418 | rtems_device_driver console_open( |
---|
| 419 | rtems_device_major_number major, |
---|
| 420 | rtems_device_minor_number minor, |
---|
| 421 | void * arg |
---|
| 422 | ) |
---|
| 423 | { |
---|
[a7e2729] | 424 | rtems_status_code sc; |
---|
| 425 | #if defined(CONSOLE_USE_INTERRUPTS) |
---|
| 426 | rtems_libio_open_close_args_t *args = arg; |
---|
| 427 | #endif |
---|
| 428 | |
---|
| 429 | assert( minor <= 1 ); |
---|
| 430 | if ( minor > 2 ) |
---|
| 431 | return RTEMS_INVALID_NUMBER; |
---|
| 432 | |
---|
| 433 | #if defined(CONSOLE_USE_INTERRUPTS) |
---|
| 434 | sc = rtems_termios_open (major, minor, arg, |
---|
| 435 | NULL, |
---|
| 436 | NULL, |
---|
| 437 | NULL, |
---|
| 438 | console_write_support); |
---|
| 439 | |
---|
| 440 | console_termios_data[ minor ] = args->iop->data1; |
---|
| 441 | #else |
---|
| 442 | sc = rtems_termios_open (major, minor, arg, |
---|
| 443 | NULL, |
---|
| 444 | NULL, |
---|
| 445 | console_inbyte_nonblocking, |
---|
| 446 | console_write_support); |
---|
| 447 | #endif |
---|
| 448 | |
---|
[7f96eef] | 449 | return RTEMS_SUCCESSFUL; |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | rtems_device_driver console_close( |
---|
| 453 | rtems_device_major_number major, |
---|
| 454 | rtems_device_minor_number minor, |
---|
| 455 | void * arg |
---|
| 456 | ) |
---|
| 457 | { |
---|
[a7e2729] | 458 | return rtems_termios_close (arg); |
---|
[7f96eef] | 459 | } |
---|
| 460 | |
---|
| 461 | rtems_device_driver console_read( |
---|
| 462 | rtems_device_major_number major, |
---|
| 463 | rtems_device_minor_number minor, |
---|
| 464 | void * arg |
---|
| 465 | ) |
---|
| 466 | { |
---|
[a7e2729] | 467 | return rtems_termios_read (arg); |
---|
[7f96eef] | 468 | } |
---|
| 469 | |
---|
| 470 | rtems_device_driver console_write( |
---|
| 471 | rtems_device_major_number major, |
---|
| 472 | rtems_device_minor_number minor, |
---|
| 473 | void * arg |
---|
| 474 | ) |
---|
| 475 | { |
---|
[a7e2729] | 476 | return rtems_termios_write (arg); |
---|
[7f96eef] | 477 | } |
---|
| 478 | |
---|
| 479 | rtems_device_driver console_control( |
---|
| 480 | rtems_device_major_number major, |
---|
| 481 | rtems_device_minor_number minor, |
---|
| 482 | void * arg |
---|
| 483 | ) |
---|
| 484 | { |
---|
[a7e2729] | 485 | return rtems_termios_ioctl (arg); |
---|
[7f96eef] | 486 | } |
---|
| 487 | |
---|