[1612af0] | 1 | /* |
---|
| 2 | * Multi UART console serial I/O. |
---|
| 3 | * |
---|
| 4 | * TO DO: Add DMA input/output |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | #include <stdio.h> |
---|
| 8 | #include <fcntl.h> |
---|
| 9 | #include <termios.h> |
---|
| 10 | #include <malloc.h> |
---|
| 11 | |
---|
[fb31ec0b] | 12 | #include <rtems/libio.h> |
---|
| 13 | #include <rtems/termiostypes.h> |
---|
[1612af0] | 14 | #include <rtems/bspIo.h> |
---|
[fb31ec0b] | 15 | #include <rtems/console.h> |
---|
| 16 | #include <bsp.h> |
---|
[1612af0] | 17 | |
---|
| 18 | #define UART_INTC0_IRQ_VECTOR(x) (64+13+(x)) |
---|
| 19 | |
---|
| 20 | #define MCF5235_UART_USR_ERROR ( MCF5235_UART_USR_RB | \ |
---|
| 21 | MCF5235_UART_USR_FE | \ |
---|
| 22 | MCF5235_UART_USR_PE | \ |
---|
| 23 | MCF5235_UART_USR_OE ) |
---|
| 24 | |
---|
[39a9f8e] | 25 | static ssize_t IntUartPollWrite(int minor, const char *buf, size_t len); |
---|
| 26 | static ssize_t IntUartInterruptWrite (int minor, const char *buf, size_t len); |
---|
[1612af0] | 27 | |
---|
| 28 | static void |
---|
| 29 | _BSP_null_char( char c ) |
---|
[d4b4664b] | 30 | { |
---|
[1612af0] | 31 | int level; |
---|
| 32 | |
---|
| 33 | if (c == '\n') |
---|
| 34 | _BSP_null_char('\r'); |
---|
| 35 | rtems_interrupt_disable(level); |
---|
| 36 | while ( (MCF5235_UART_USR(CONSOLE_PORT) & MCF5235_UART_USR_TXRDY) == 0 ) |
---|
| 37 | continue; |
---|
| 38 | MCF5235_UART_UTB(CONSOLE_PORT) = c; |
---|
| 39 | while ( (MCF5235_UART_USR(CONSOLE_PORT) & MCF5235_UART_USR_TXRDY) == 0 ) |
---|
| 40 | continue; |
---|
| 41 | rtems_interrupt_enable(level); |
---|
| 42 | } |
---|
[989938f1] | 43 | BSP_output_char_function_type BSP_output_char = _BSP_null_char; |
---|
| 44 | BSP_polling_getchar_function_type BSP_poll_char = NULL; |
---|
[1612af0] | 45 | |
---|
| 46 | #define MAX_UART_INFO 3 |
---|
| 47 | #define RX_BUFFER_SIZE 512 |
---|
| 48 | |
---|
| 49 | struct IntUartInfoStruct |
---|
| 50 | { |
---|
| 51 | int iomode; |
---|
| 52 | volatile int uimr; |
---|
| 53 | int baud; |
---|
| 54 | int databits; |
---|
| 55 | int parity; |
---|
| 56 | int stopbits; |
---|
| 57 | int hwflow; |
---|
| 58 | int rx_in; |
---|
| 59 | int rx_out; |
---|
| 60 | char rx_buffer[RX_BUFFER_SIZE]; |
---|
| 61 | void *ttyp; |
---|
| 62 | }; |
---|
| 63 | |
---|
| 64 | struct IntUartInfoStruct IntUartInfo[MAX_UART_INFO]; |
---|
| 65 | |
---|
| 66 | /*************************************************************************** |
---|
| 67 | Function : IntUartSet |
---|
| 68 | |
---|
| 69 | Description : This updates the hardware UART settings. |
---|
| 70 | ***************************************************************************/ |
---|
| 71 | static void |
---|
| 72 | IntUartSet(int minor, int baud, int databits, int parity, int stopbits, int hwflow) |
---|
| 73 | { |
---|
| 74 | int divisor; |
---|
[8fd9e978] | 75 | uint32_t clock_speed; |
---|
| 76 | uint8_t umr1 = 0; |
---|
| 77 | uint8_t umr2 = 0; |
---|
[1612af0] | 78 | struct IntUartInfoStruct *info = &IntUartInfo[minor]; |
---|
| 79 | int level; |
---|
| 80 | |
---|
| 81 | rtems_interrupt_disable(level); |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | /* disable interrupts, clear RTS line, and disable the UARTS */ |
---|
| 85 | MCF5235_UART_UIMR(minor) = 0; |
---|
| 86 | MCF5235_UART_UOP0(minor) = 1; |
---|
| 87 | MCF5235_UART_UCR(minor) = (MCF5235_UART_UCR_TX_DISABLED | MCF5235_UART_UCR_RX_DISABLED); |
---|
| 88 | |
---|
| 89 | /* save the current values */ |
---|
| 90 | info->uimr = 0; |
---|
| 91 | info->baud = baud; |
---|
| 92 | info->databits = databits; |
---|
| 93 | info->parity = parity; |
---|
| 94 | info->stopbits = stopbits; |
---|
| 95 | info->hwflow = hwflow; |
---|
| 96 | |
---|
| 97 | clock_speed = get_CPU_clock_speed(); |
---|
| 98 | /* determine the baud divisor value */ |
---|
| 99 | divisor = ((clock_speed/2) / ( 32 * baud )); |
---|
| 100 | if ( divisor < 2 ) |
---|
| 101 | divisor = 2; |
---|
| 102 | |
---|
| 103 | /* check to see if doing hardware flow control */ |
---|
| 104 | if ( hwflow ) |
---|
| 105 | { |
---|
| 106 | /* set hardware flow options */ |
---|
| 107 | umr1 |= MCF5235_UART_UMR_RXRTS; |
---|
| 108 | umr2 |= MCF5235_UART_UMR_TXCTS; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /* determine the new umr values */ |
---|
| 112 | umr1 |= (parity | databits); |
---|
| 113 | umr2 |= (stopbits); |
---|
| 114 | |
---|
| 115 | /* reset the uart */ |
---|
| 116 | MCF5235_UART_UCR(minor) = MCF5235_UART_UCR_RESET_ERROR; |
---|
| 117 | MCF5235_UART_UCR(minor) = MCF5235_UART_UCR_RESET_RX; |
---|
| 118 | MCF5235_UART_UCR(minor) = MCF5235_UART_UCR_RESET_TX; |
---|
| 119 | |
---|
| 120 | /* reset the uart mode register and update values */ |
---|
| 121 | MCF5235_UART_UCR(minor) = MCF5235_UART_UCR_RESET_MR; |
---|
| 122 | MCF5235_UART_UMR(minor) = umr1; |
---|
| 123 | MCF5235_UART_UMR(minor) = umr2; |
---|
| 124 | |
---|
| 125 | /* set the baud rate values */ |
---|
| 126 | MCF5235_UART_UCSR(minor) = (MCF5235_UART_UCSR_RCS_SYS_CLK | MCF5235_UART_UCSR_TCS_SYS_CLK); |
---|
| 127 | MCF5235_UART_UBG1(minor) = (divisor & 0xff00) >> 8; |
---|
| 128 | MCF5235_UART_UBG2(minor) = (divisor & 0x00ff); |
---|
| 129 | |
---|
| 130 | /* enable the uart */ |
---|
| 131 | MCF5235_UART_UCR(minor) = (MCF5235_UART_UCR_TX_ENABLED | MCF5235_UART_UCR_RX_ENABLED); |
---|
| 132 | |
---|
| 133 | /* check to see if interrupts need to be enabled */ |
---|
| 134 | if ( info->iomode != TERMIOS_POLLED ) |
---|
| 135 | { |
---|
| 136 | /* enable rx interrupts */ |
---|
| 137 | info->uimr |= MCF5235_UART_UIMR_FFULL; |
---|
| 138 | MCF5235_UART_UIMR(minor) = info->uimr; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /* check to see if doing hardware flow control */ |
---|
| 142 | if ( hwflow ) |
---|
| 143 | { |
---|
| 144 | /* assert the RTS line */ |
---|
| 145 | MCF5235_UART_UOP1(minor) = 1; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | rtems_interrupt_enable(level); |
---|
| 149 | |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | /*************************************************************************** |
---|
| 154 | Function : IntUartSetAttributes |
---|
| 155 | |
---|
| 156 | Description : This provides the hardware-dependent portion of tcsetattr(). |
---|
| 157 | value and sets it. At the moment this just sets the baud rate. |
---|
| 158 | |
---|
[d4b4664b] | 159 | Note: The highest baudrate is 115200 as this stays within |
---|
[1612af0] | 160 | an error of +/- 5% at 25MHz processor clock |
---|
| 161 | ***************************************************************************/ |
---|
| 162 | static int |
---|
| 163 | IntUartSetAttributes(int minor, const struct termios *t) |
---|
| 164 | { |
---|
| 165 | /* set default index values */ |
---|
| 166 | int baud = (int)19200; |
---|
| 167 | int databits = (int)MCF5235_UART_UMR_BC_8; |
---|
| 168 | int parity = (int)MCF5235_UART_UMR_PM_NONE; |
---|
| 169 | int stopbits = (int)MCF5235_UART_UMR_STOP_BITS_1; |
---|
| 170 | int hwflow = (int)0; |
---|
| 171 | struct IntUartInfoStruct *info = &IntUartInfo[minor]; |
---|
| 172 | |
---|
| 173 | /* check to see if input is valid */ |
---|
| 174 | if ( t != (const struct termios *)0 ) |
---|
| 175 | { |
---|
| 176 | /* determine baud rate index */ |
---|
[1c6926c1] | 177 | baud = rtems_termios_baud_to_number(t->c_ospeed); |
---|
[1612af0] | 178 | |
---|
| 179 | /* determine data bits */ |
---|
| 180 | switch ( t->c_cflag & CSIZE ) |
---|
| 181 | { |
---|
| 182 | case CS5: |
---|
| 183 | databits = (int)MCF5235_UART_UMR_BC_5; |
---|
| 184 | break; |
---|
| 185 | case CS6: |
---|
| 186 | databits = (int)MCF5235_UART_UMR_BC_6; |
---|
| 187 | break; |
---|
| 188 | case CS7: |
---|
| 189 | databits = (int)MCF5235_UART_UMR_BC_7; |
---|
| 190 | break; |
---|
| 191 | case CS8: |
---|
| 192 | databits = (int)MCF5235_UART_UMR_BC_8; |
---|
| 193 | break; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | /* determine if parity is enabled */ |
---|
| 197 | if ( t->c_cflag & PARENB ) |
---|
| 198 | { |
---|
| 199 | if ( t->c_cflag & PARODD ) |
---|
| 200 | { |
---|
| 201 | /* odd parity */ |
---|
| 202 | parity = (int)MCF5235_UART_UMR_PM_ODD; |
---|
| 203 | } |
---|
| 204 | else |
---|
| 205 | { |
---|
| 206 | /* even parity */ |
---|
| 207 | parity = (int)MCF5235_UART_UMR_PM_EVEN; |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | /* determine stop bits */ |
---|
| 212 | if ( t->c_cflag & CSTOPB ) |
---|
| 213 | { |
---|
| 214 | /* two stop bits */ |
---|
| 215 | stopbits = (int)MCF5235_UART_UMR_STOP_BITS_2; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | /* check to see if hardware flow control */ |
---|
| 219 | if ( t->c_cflag & CRTSCTS ) |
---|
| 220 | { |
---|
| 221 | hwflow = 1; |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | /* check to see if values have changed */ |
---|
| 226 | if ( ( baud != info->baud ) || |
---|
| 227 | ( databits != info->databits ) || |
---|
| 228 | ( parity != info->parity ) || |
---|
| 229 | ( stopbits != info->stopbits ) || |
---|
| 230 | ( hwflow != info->hwflow ) ) |
---|
| 231 | { |
---|
| 232 | |
---|
| 233 | /* call function to set values */ |
---|
| 234 | IntUartSet(minor, baud, databits, parity, stopbits, hwflow); |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | return( RTEMS_SUCCESSFUL ); |
---|
| 238 | |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | /*************************************************************************** |
---|
| 242 | Function : IntUartInterruptHandler |
---|
| 243 | |
---|
| 244 | Description : This is the interrupt handler for the internal uart. It |
---|
| 245 | determines which channel caused the interrupt before queueing any received |
---|
| 246 | chars and dequeueing chars waiting for transmission. |
---|
| 247 | ***************************************************************************/ |
---|
| 248 | static rtems_isr |
---|
| 249 | IntUartInterruptHandler(rtems_vector_number v) |
---|
| 250 | { |
---|
| 251 | unsigned int chan = v - UART_INTC0_IRQ_VECTOR(0); |
---|
| 252 | struct IntUartInfoStruct *info = &IntUartInfo[chan]; |
---|
| 253 | |
---|
| 254 | /* check to see if received data */ |
---|
| 255 | if ( MCF5235_UART_UISR(chan) & MCF5235_UART_UISR_RXRDY ) |
---|
| 256 | { |
---|
| 257 | /* read data and put into the receive buffer */ |
---|
| 258 | while ( MCF5235_UART_USR(chan) & MCF5235_UART_USR_RXRDY ) |
---|
| 259 | { |
---|
| 260 | |
---|
| 261 | if ( MCF5235_UART_USR(chan) & MCF5235_UART_USR_ERROR ) |
---|
| 262 | { |
---|
| 263 | /* clear the error */ |
---|
| 264 | MCF5235_UART_UCR(chan) = MCF5235_UART_UCR_RESET_ERROR; |
---|
| 265 | } |
---|
| 266 | /* put data in rx buffer and check for errors */ |
---|
| 267 | info->rx_buffer[info->rx_in] = MCF5235_UART_URB(chan); |
---|
| 268 | |
---|
| 269 | /* update buffer values */ |
---|
| 270 | info->rx_in++; |
---|
| 271 | |
---|
| 272 | if ( info->rx_in >= RX_BUFFER_SIZE ) |
---|
| 273 | { |
---|
| 274 | info->rx_in = 0; |
---|
| 275 | } |
---|
| 276 | } |
---|
| 277 | /* Make sure the port has been opened */ |
---|
| 278 | if ( info->ttyp ) |
---|
| 279 | { |
---|
| 280 | |
---|
| 281 | /* check to see if task driven */ |
---|
| 282 | if ( info->iomode == TERMIOS_TASK_DRIVEN ) |
---|
| 283 | { |
---|
| 284 | /* notify rx task that rx buffer has data */ |
---|
| 285 | rtems_termios_rxirq_occured(info->ttyp); |
---|
| 286 | } |
---|
| 287 | else |
---|
| 288 | { |
---|
| 289 | /* Push up the received data */ |
---|
| 290 | rtems_termios_enqueue_raw_characters(info->ttyp, info->rx_buffer, info->rx_in); |
---|
| 291 | info->rx_in = 0; |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | /* check to see if data needs to be transmitted */ |
---|
| 297 | if ( ( info->uimr & MCF5235_UART_UIMR_TXRDY ) && |
---|
| 298 | ( MCF5235_UART_UISR(chan) & MCF5235_UART_UISR_TXRDY ) ) |
---|
| 299 | { |
---|
| 300 | |
---|
| 301 | /* disable tx interrupts */ |
---|
| 302 | info->uimr &= ~MCF5235_UART_UIMR_TXRDY; |
---|
| 303 | MCF5235_UART_UIMR(chan) = info->uimr; |
---|
| 304 | |
---|
| 305 | /* tell upper level that character has been sent */ |
---|
| 306 | if ( info->ttyp ) |
---|
| 307 | rtems_termios_dequeue_characters(info->ttyp, 1); |
---|
| 308 | } |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | |
---|
| 312 | |
---|
| 313 | /*************************************************************************** |
---|
| 314 | Function : IntUartInitialize |
---|
| 315 | |
---|
| 316 | Description : This initialises the internal uart hardware for all |
---|
| 317 | internal uarts. If the internal uart is to be interrupt driven then the |
---|
| 318 | interrupt vectors are hooked. |
---|
| 319 | ***************************************************************************/ |
---|
| 320 | static void |
---|
| 321 | IntUartInitialize(void) |
---|
| 322 | { |
---|
| 323 | unsigned int chan; |
---|
| 324 | struct IntUartInfoStruct *info; |
---|
| 325 | rtems_isr_entry old_handler; |
---|
| 326 | int level; |
---|
| 327 | |
---|
| 328 | for ( chan = 0; chan < MAX_UART_INFO; chan++ ) |
---|
| 329 | { |
---|
| 330 | info = &IntUartInfo[chan]; |
---|
| 331 | |
---|
| 332 | info->ttyp = NULL; |
---|
| 333 | info->rx_in = 0; |
---|
| 334 | info->rx_out = 0; |
---|
| 335 | info->baud = -1; |
---|
| 336 | info->databits = -1; |
---|
| 337 | info->parity = -1; |
---|
| 338 | info->stopbits = -1; |
---|
| 339 | info->hwflow = -1; |
---|
[1883c87] | 340 | info->iomode = TERMIOS_POLLED; /*polled console io */ |
---|
[1612af0] | 341 | |
---|
| 342 | MCF5235_UART_UACR(chan) = 0; |
---|
| 343 | MCF5235_UART_UIMR(chan) = 0; |
---|
| 344 | if ( info->iomode != TERMIOS_POLLED ) |
---|
| 345 | { |
---|
| 346 | rtems_interrupt_catch (IntUartInterruptHandler, |
---|
| 347 | UART_INTC0_IRQ_VECTOR(chan), |
---|
| 348 | &old_handler); |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | /* set uart default values */ |
---|
| 352 | IntUartSetAttributes(chan, NULL); |
---|
| 353 | |
---|
| 354 | /* unmask interrupt */ |
---|
| 355 | rtems_interrupt_disable(level); |
---|
| 356 | switch(chan) { |
---|
| 357 | case 0: |
---|
| 358 | MCF5235_INTC0_ICR13 = MCF5235_INTC_ICR_IL(UART0_IRQ_LEVEL) | |
---|
| 359 | MCF5235_INTC_ICR_IP(UART0_IRQ_PRIORITY); |
---|
| 360 | MCF5235_INTC0_IMRL &= ~(MCF5235_INTC0_IMRL_INT13 | |
---|
| 361 | MCF5235_INTC0_IMRL_MASKALL); |
---|
| 362 | break; |
---|
| 363 | |
---|
| 364 | case 1: |
---|
| 365 | MCF5235_INTC0_ICR14 = MCF5235_INTC_ICR_IL(UART1_IRQ_LEVEL) | |
---|
| 366 | MCF5235_INTC_ICR_IP(UART1_IRQ_PRIORITY); |
---|
| 367 | MCF5235_INTC0_IMRL &= ~(MCF5235_INTC0_IMRL_INT14 | |
---|
| 368 | MCF5235_INTC0_IMRL_MASKALL); |
---|
| 369 | break; |
---|
| 370 | |
---|
| 371 | case 2: |
---|
| 372 | MCF5235_INTC0_ICR15 = MCF5235_INTC_ICR_IL(UART2_IRQ_LEVEL) | |
---|
| 373 | MCF5235_INTC_ICR_IP(UART2_IRQ_PRIORITY); |
---|
| 374 | MCF5235_INTC0_IMRL &= ~(MCF5235_INTC0_IMRL_INT15 | |
---|
| 375 | MCF5235_INTC0_IMRL_MASKALL); |
---|
| 376 | break; |
---|
| 377 | } |
---|
| 378 | rtems_interrupt_enable(level); |
---|
| 379 | |
---|
| 380 | } /* of chan loop */ |
---|
| 381 | |
---|
| 382 | |
---|
| 383 | } /* IntUartInitialise */ |
---|
| 384 | |
---|
| 385 | |
---|
| 386 | /*************************************************************************** |
---|
| 387 | Function : IntUartInterruptWrite |
---|
| 388 | |
---|
[d4b4664b] | 389 | Description : This writes a single character to the appropriate uart |
---|
[1612af0] | 390 | channel. This is either called during an interrupt or in the user's task |
---|
[d4b4664b] | 391 | to initiate a transmit sequence. Calling this routine enables Tx |
---|
[1612af0] | 392 | interrupts. |
---|
| 393 | ***************************************************************************/ |
---|
[39a9f8e] | 394 | static ssize_t |
---|
| 395 | IntUartInterruptWrite (int minor, const char *buf, size_t len) |
---|
[1612af0] | 396 | { |
---|
[e18db9f] | 397 | if (len > 0) { |
---|
| 398 | /* write out character */ |
---|
| 399 | MCF5235_UART_UTB(minor) = *buf; |
---|
[1612af0] | 400 | |
---|
[e18db9f] | 401 | /* enable tx interrupt */ |
---|
| 402 | IntUartInfo[minor].uimr |= MCF5235_UART_UIMR_TXRDY; |
---|
| 403 | MCF5235_UART_UIMR(minor) = IntUartInfo[minor].uimr; |
---|
| 404 | } |
---|
[1612af0] | 405 | |
---|
| 406 | return( 0 ); |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | /*************************************************************************** |
---|
| 410 | Function : IntUartInterruptOpen |
---|
| 411 | |
---|
| 412 | Description : This enables interrupts when the tty is opened. |
---|
| 413 | ***************************************************************************/ |
---|
| 414 | static int |
---|
| 415 | IntUartInterruptOpen(int major, int minor, void *arg) |
---|
| 416 | { |
---|
| 417 | struct IntUartInfoStruct *info = &IntUartInfo[minor]; |
---|
| 418 | |
---|
| 419 | /* enable the uart */ |
---|
| 420 | MCF5235_UART_UCR(minor) = (MCF5235_UART_UCR_TX_ENABLED | MCF5235_UART_UCR_RX_ENABLED); |
---|
| 421 | |
---|
| 422 | /* check to see if interrupts need to be enabled */ |
---|
| 423 | if ( info->iomode != TERMIOS_POLLED ) |
---|
| 424 | { |
---|
| 425 | /* enable rx interrupts */ |
---|
| 426 | info->uimr |= MCF5235_UART_UIMR_FFULL; |
---|
| 427 | MCF5235_UART_UIMR(minor) = info->uimr; |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | /* check to see if doing hardware flow control */ |
---|
| 431 | if ( info->hwflow ) |
---|
| 432 | { |
---|
| 433 | /* assert the RTS line */ |
---|
| 434 | MCF5235_UART_UOP1(minor) = 1; |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | return( 0 ); |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | |
---|
| 441 | |
---|
| 442 | /*************************************************************************** |
---|
| 443 | Function : IntUartInterruptClose |
---|
| 444 | |
---|
| 445 | Description : This disables interrupts when the tty is closed. |
---|
| 446 | ***************************************************************************/ |
---|
| 447 | static int |
---|
| 448 | IntUartInterruptClose(int major, int minor, void *arg) |
---|
| 449 | { |
---|
| 450 | struct IntUartInfoStruct *info = &IntUartInfo[minor]; |
---|
| 451 | |
---|
| 452 | /* disable the interrupts and the uart */ |
---|
| 453 | MCF5235_UART_UIMR(minor) = 0; |
---|
| 454 | MCF5235_UART_UCR(minor) = (MCF5235_UART_UCR_TX_DISABLED | MCF5235_UART_UCR_RX_DISABLED); |
---|
| 455 | |
---|
| 456 | /* reset values */ |
---|
| 457 | info->ttyp = NULL; |
---|
| 458 | info->uimr = 0; |
---|
| 459 | info->rx_in = 0; |
---|
| 460 | info->rx_out = 0; |
---|
| 461 | |
---|
| 462 | return( 0 ); |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | /*************************************************************************** |
---|
| 466 | Function : IntUartTaskRead |
---|
| 467 | |
---|
| 468 | Description : This reads all available characters from the internal uart |
---|
| 469 | and places them into the termios buffer. The rx interrupts will be |
---|
| 470 | re-enabled after all data has been read. |
---|
| 471 | ***************************************************************************/ |
---|
| 472 | static int |
---|
| 473 | IntUartTaskRead(int minor) |
---|
| 474 | { |
---|
| 475 | char buffer[RX_BUFFER_SIZE]; |
---|
| 476 | int count; |
---|
| 477 | int rx_in; |
---|
| 478 | int index = 0; |
---|
| 479 | struct IntUartInfoStruct *info = &IntUartInfo[minor]; |
---|
| 480 | |
---|
| 481 | /* determine number of values to copy out */ |
---|
| 482 | rx_in = info->rx_in; |
---|
| 483 | if ( info->rx_out <= rx_in ) |
---|
| 484 | { |
---|
| 485 | count = rx_in - info->rx_out; |
---|
| 486 | } |
---|
| 487 | else |
---|
| 488 | { |
---|
| 489 | count = (RX_BUFFER_SIZE - info->rx_out) + rx_in; |
---|
| 490 | } |
---|
| 491 | |
---|
| 492 | /* copy data into local buffer from rx buffer */ |
---|
| 493 | while ( ( index < count ) && ( index < RX_BUFFER_SIZE ) ) |
---|
| 494 | { |
---|
[d4b4664b] | 495 | /* copy data byte */ |
---|
[1612af0] | 496 | buffer[index] = info->rx_buffer[info->rx_out]; |
---|
| 497 | index++; |
---|
| 498 | |
---|
| 499 | /* increment rx buffer values */ |
---|
| 500 | info->rx_out++; |
---|
| 501 | if ( info->rx_out >= RX_BUFFER_SIZE ) |
---|
| 502 | { |
---|
| 503 | info->rx_out = 0; |
---|
| 504 | } |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | /* check to see if buffer is not empty */ |
---|
| 508 | if ( count > 0 ) |
---|
| 509 | { |
---|
| 510 | /* set characters into termios buffer */ |
---|
| 511 | rtems_termios_enqueue_raw_characters(info->ttyp, buffer, count); |
---|
| 512 | } |
---|
| 513 | |
---|
| 514 | return( EOF ); |
---|
| 515 | } |
---|
| 516 | |
---|
| 517 | |
---|
| 518 | |
---|
| 519 | /*************************************************************************** |
---|
| 520 | Function : IntUartPollRead |
---|
| 521 | |
---|
[d4b4664b] | 522 | Description : This reads a character from the internal uart. It returns |
---|
[1612af0] | 523 | to the caller without blocking if not character is waiting. |
---|
| 524 | ***************************************************************************/ |
---|
| 525 | static int |
---|
| 526 | IntUartPollRead (int minor) |
---|
| 527 | { |
---|
| 528 | if ( (MCF5235_UART_USR(minor) & MCF5235_UART_USR_RXRDY) == 0 ) |
---|
| 529 | return(-1); |
---|
| 530 | |
---|
| 531 | return(MCF5235_UART_URB(minor)); |
---|
| 532 | } |
---|
| 533 | |
---|
| 534 | |
---|
| 535 | /*************************************************************************** |
---|
| 536 | Function : IntUartPollWrite |
---|
| 537 | |
---|
[d4b4664b] | 538 | Description : This writes out each character in the buffer to the |
---|
| 539 | appropriate internal uart channel waiting till each one is sucessfully |
---|
[1612af0] | 540 | transmitted. |
---|
| 541 | ***************************************************************************/ |
---|
[39a9f8e] | 542 | static ssize_t |
---|
| 543 | IntUartPollWrite (int minor, const char *buf, size_t len) |
---|
[1612af0] | 544 | { |
---|
[39a9f8e] | 545 | size_t retval = len; |
---|
[1612af0] | 546 | /* loop over buffer */ |
---|
| 547 | while ( len-- ) |
---|
| 548 | { |
---|
| 549 | /* block until we can transmit */ |
---|
| 550 | while ( (MCF5235_UART_USR(minor) & MCF5235_UART_USR_TXRDY) == 0 ) |
---|
| 551 | continue; |
---|
| 552 | /* transmit data byte */ |
---|
| 553 | MCF5235_UART_UTB(minor) = *buf++; |
---|
| 554 | } |
---|
[39a9f8e] | 555 | return retval; |
---|
[1612af0] | 556 | } |
---|
| 557 | |
---|
| 558 | /*************************************************************************** |
---|
| 559 | Function : console_initialize |
---|
| 560 | |
---|
| 561 | Description : This initialises termios, both sets of uart hardware before |
---|
| 562 | registering /dev/tty devices for each channel and the system /dev/console. |
---|
| 563 | ***************************************************************************/ |
---|
| 564 | rtems_device_driver console_initialize( |
---|
| 565 | rtems_device_major_number major, |
---|
| 566 | rtems_device_minor_number minor, |
---|
| 567 | void *arg ) |
---|
| 568 | { |
---|
| 569 | rtems_status_code status; |
---|
| 570 | |
---|
| 571 | |
---|
| 572 | /* Set up TERMIOS */ |
---|
| 573 | rtems_termios_initialize (); |
---|
| 574 | |
---|
| 575 | /* set io modes for the different channels and initialize device */ |
---|
| 576 | IntUartInfo[minor].iomode = TERMIOS_IRQ_DRIVEN; |
---|
[d4b4664b] | 577 | IntUartInitialize(); |
---|
[1612af0] | 578 | |
---|
| 579 | /* Register the console port */ |
---|
| 580 | status = rtems_io_register_name ("/dev/console", major, CONSOLE_PORT); |
---|
| 581 | if ( status != RTEMS_SUCCESSFUL ) |
---|
| 582 | { |
---|
| 583 | rtems_fatal_error_occurred (status); |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | /* Register the other port */ |
---|
| 587 | if ( CONSOLE_PORT != 0 ) |
---|
| 588 | { |
---|
| 589 | status = rtems_io_register_name ("/dev/tty00", major, 0); |
---|
| 590 | if ( status != RTEMS_SUCCESSFUL ) |
---|
| 591 | { |
---|
| 592 | rtems_fatal_error_occurred (status); |
---|
| 593 | } |
---|
| 594 | } |
---|
| 595 | if ( CONSOLE_PORT != 1 ) |
---|
| 596 | { |
---|
| 597 | status = rtems_io_register_name ("/dev/tty01", major, 1); |
---|
| 598 | if ( status != RTEMS_SUCCESSFUL ) |
---|
| 599 | { |
---|
| 600 | rtems_fatal_error_occurred (status); |
---|
| 601 | } |
---|
| 602 | } |
---|
| 603 | |
---|
| 604 | return(RTEMS_SUCCESSFUL); |
---|
| 605 | } |
---|
| 606 | |
---|
| 607 | /*************************************************************************** |
---|
| 608 | Function : console_open |
---|
| 609 | |
---|
[d4b4664b] | 610 | Description : This actually opens the device depending on the minor |
---|
[1612af0] | 611 | number set during initialisation. The device specific access routines are |
---|
| 612 | passed to termios when the devices is opened depending on whether it is |
---|
| 613 | polled or not. |
---|
| 614 | ***************************************************************************/ |
---|
| 615 | rtems_device_driver console_open( |
---|
| 616 | rtems_device_major_number major, |
---|
| 617 | rtems_device_minor_number minor, |
---|
| 618 | void * arg) |
---|
| 619 | { |
---|
| 620 | rtems_status_code status = RTEMS_INVALID_NUMBER; |
---|
| 621 | rtems_libio_open_close_args_t *args = (rtems_libio_open_close_args_t *)arg; |
---|
| 622 | struct IntUartInfoStruct *info; |
---|
| 623 | |
---|
| 624 | static const rtems_termios_callbacks IntUartPollCallbacks = { |
---|
| 625 | NULL, /* firstOpen */ |
---|
| 626 | NULL, /* lastClose */ |
---|
| 627 | IntUartPollRead, /* pollRead */ |
---|
| 628 | IntUartPollWrite, /* write */ |
---|
| 629 | IntUartSetAttributes, /* setAttributes */ |
---|
| 630 | NULL, /* stopRemoteTx */ |
---|
| 631 | NULL, /* startRemoteTx */ |
---|
| 632 | TERMIOS_POLLED /* mode */ |
---|
| 633 | }; |
---|
| 634 | static const rtems_termios_callbacks IntUartIntrCallbacks = { |
---|
| 635 | IntUartInterruptOpen, /* firstOpen */ |
---|
| 636 | IntUartInterruptClose, /* lastClose */ |
---|
| 637 | NULL, /* pollRead */ |
---|
| 638 | IntUartInterruptWrite, /* write */ |
---|
| 639 | IntUartSetAttributes, /* setAttributes */ |
---|
| 640 | NULL, /* stopRemoteTx */ |
---|
| 641 | NULL, /* startRemoteTx */ |
---|
| 642 | TERMIOS_IRQ_DRIVEN /* mode */ |
---|
| 643 | }; |
---|
| 644 | |
---|
| 645 | static const rtems_termios_callbacks IntUartTaskCallbacks = { |
---|
| 646 | IntUartInterruptOpen, /* firstOpen */ |
---|
| 647 | IntUartInterruptClose, /* lastClose */ |
---|
| 648 | IntUartTaskRead, /* pollRead */ |
---|
| 649 | IntUartInterruptWrite, /* write */ |
---|
| 650 | IntUartSetAttributes, /* setAttributes */ |
---|
| 651 | NULL, /* stopRemoteTx */ |
---|
| 652 | NULL, /* startRemoteTx */ |
---|
| 653 | TERMIOS_TASK_DRIVEN /* mode */ |
---|
| 654 | }; |
---|
| 655 | |
---|
| 656 | /* open the port depending on the minor device number */ |
---|
| 657 | if ( ( minor >= 0 ) && ( minor < MAX_UART_INFO ) ) |
---|
| 658 | { |
---|
| 659 | info = &IntUartInfo[minor]; |
---|
| 660 | switch ( info->iomode ) |
---|
| 661 | { |
---|
| 662 | case TERMIOS_POLLED: |
---|
| 663 | status = rtems_termios_open(major, minor, arg, &IntUartPollCallbacks); |
---|
| 664 | break; |
---|
| 665 | case TERMIOS_IRQ_DRIVEN: |
---|
| 666 | status = rtems_termios_open(major, minor, arg, &IntUartIntrCallbacks); |
---|
| 667 | info->ttyp = args->iop->data1; |
---|
| 668 | break; |
---|
| 669 | case TERMIOS_TASK_DRIVEN: |
---|
| 670 | status = rtems_termios_open(major, minor, arg, &IntUartTaskCallbacks); |
---|
| 671 | info->ttyp = args->iop->data1; |
---|
| 672 | break; |
---|
| 673 | } |
---|
| 674 | } |
---|
| 675 | |
---|
[7ace382] | 676 | if (status == RTEMS_SUCCESSFUL) |
---|
| 677 | { |
---|
| 678 | /* |
---|
| 679 | * Reset the default baudrate. |
---|
| 680 | */ |
---|
| 681 | struct termios term; |
---|
| 682 | if (tcgetattr (STDIN_FILENO, &term) >= 0) |
---|
| 683 | { |
---|
[1c6926c1] | 684 | term.c_cflag &= ~(CSIZE); |
---|
| 685 | term.c_cflag |= CS8; |
---|
| 686 | term.c_ispeed = B19200; |
---|
| 687 | term.c_ospeed = B19200; |
---|
[7ace382] | 688 | tcsetattr (STDIN_FILENO, TCSANOW, &term); |
---|
| 689 | } |
---|
| 690 | } |
---|
| 691 | |
---|
[1612af0] | 692 | return( status ); |
---|
| 693 | } |
---|
| 694 | |
---|
| 695 | /*************************************************************************** |
---|
| 696 | Function : console_close |
---|
| 697 | |
---|
| 698 | Description : This closes the device via termios |
---|
| 699 | ***************************************************************************/ |
---|
| 700 | rtems_device_driver console_close( |
---|
| 701 | rtems_device_major_number major, |
---|
| 702 | rtems_device_minor_number minor, |
---|
| 703 | void * arg) |
---|
| 704 | { |
---|
| 705 | return(rtems_termios_close (arg)); |
---|
| 706 | } |
---|
| 707 | |
---|
| 708 | /****************** |
---|
| 709 | ********************************************************* |
---|
| 710 | Function : console_read |
---|
| 711 | |
---|
| 712 | Description : Read from the device via termios |
---|
| 713 | ***************************************************************************/ |
---|
| 714 | rtems_device_driver console_read( |
---|
| 715 | rtems_device_major_number major, |
---|
| 716 | rtems_device_minor_number minor, |
---|
| 717 | void * arg) |
---|
| 718 | { |
---|
| 719 | return(rtems_termios_read (arg)); |
---|
| 720 | } |
---|
| 721 | |
---|
| 722 | /*************************************************************************** |
---|
| 723 | Function : console_write |
---|
| 724 | |
---|
| 725 | Description : Write to the device via termios |
---|
| 726 | ***************************************************************************/ |
---|
| 727 | rtems_device_driver console_write( |
---|
| 728 | rtems_device_major_number major, |
---|
| 729 | rtems_device_minor_number minor, |
---|
| 730 | void * arg) |
---|
| 731 | { |
---|
| 732 | return(rtems_termios_write (arg)); |
---|
| 733 | } |
---|
| 734 | |
---|
| 735 | /*************************************************************************** |
---|
| 736 | Function : console_ioctl |
---|
| 737 | |
---|
| 738 | Description : Pass the IOCtl call to termios |
---|
| 739 | ***************************************************************************/ |
---|
| 740 | rtems_device_driver console_control( |
---|
| 741 | rtems_device_major_number major, |
---|
| 742 | rtems_device_minor_number minor, |
---|
| 743 | void * arg) |
---|
| 744 | { |
---|
| 745 | return( rtems_termios_ioctl (arg) ); |
---|
| 746 | } |
---|