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