[4f1173bc] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
[aa0da6b] | 4 | * This file contains the TTY driver for the National Semiconductor NS16550. |
---|
| 5 | * |
---|
[a3d3d9a] | 6 | * This part is widely cloned and second sourced. It is found in a number |
---|
[aa0da6b] | 7 | * of "Super IO" controllers. |
---|
[0737710] | 8 | * |
---|
[4f1173bc] | 9 | * This driver uses the termios pseudo driver. |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | /* |
---|
[0737710] | 13 | * COPYRIGHT (c) 1998 by Radstone Technology |
---|
| 14 | * |
---|
[4f1173bc] | 15 | * THIS FILE IS PROVIDED TO YOU, THE USER, "AS IS", WITHOUT WARRANTY OF ANY |
---|
| 16 | * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 17 | * IMPLIED WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK |
---|
| 18 | * AS TO THE QUALITY AND PERFORMANCE OF ALL CODE IN THIS FILE IS WITH YOU. |
---|
[0737710] | 19 | * |
---|
[4f1173bc] | 20 | * You are hereby granted permission to use, copy, modify, and distribute |
---|
| 21 | * this file, provided that this notice, plus the above copyright notice |
---|
| 22 | * and disclaimer, appears in all copies. Radstone Technology will provide |
---|
| 23 | * no support for this code. |
---|
[0737710] | 24 | * |
---|
[4f1173bc] | 25 | * COPYRIGHT (c) 1989-2012. |
---|
| 26 | * On-Line Applications Research Corporation (OAR). |
---|
[0737710] | 27 | * |
---|
[4f1173bc] | 28 | * The license and distribution terms for this file may be |
---|
| 29 | * found in the file LICENSE in this distribution or at |
---|
| 30 | * http://www.rtems.com/license/LICENSE. |
---|
[69be08ee] | 31 | */ |
---|
[0737710] | 32 | |
---|
[e97e0e0] | 33 | #include <stdlib.h> |
---|
| 34 | |
---|
[0737710] | 35 | #include <rtems.h> |
---|
| 36 | #include <rtems/libio.h> |
---|
[4caeb10] | 37 | #include <rtems/ringbuf.h> |
---|
[e97e0e0] | 38 | #include <rtems/bspIo.h> |
---|
[ec5d4505] | 39 | #include <rtems/termiostypes.h> |
---|
[0737710] | 40 | |
---|
[ee3b242b] | 41 | #include <libchip/serial.h> |
---|
[7e05b53] | 42 | #include <libchip/sersupp.h> |
---|
[e97e0e0] | 43 | |
---|
[8c0cab0] | 44 | #include <bsp.h> |
---|
[0737710] | 45 | |
---|
[8c0cab0] | 46 | #include "ns16550_p.h" |
---|
[49f65bef] | 47 | #include "ns16550.h" |
---|
[fc9cbcf3] | 48 | |
---|
[2e3f4398] | 49 | #if defined(BSP_FEATURE_IRQ_EXTENSION) |
---|
[8c0cab0] | 50 | #include <bsp/irq.h> |
---|
[2e3f4398] | 51 | #elif defined(BSP_FEATURE_IRQ_LEGACY) |
---|
[8c0cab0] | 52 | #include <bsp/irq.h> |
---|
[2e3f4398] | 53 | #elif defined(__PPC__) || defined(__i386__) |
---|
[8c0cab0] | 54 | #include <bsp/irq.h> |
---|
[e97e0e0] | 55 | #define BSP_FEATURE_IRQ_LEGACY |
---|
| 56 | #ifdef BSP_SHARED_HANDLER_SUPPORT |
---|
| 57 | #define BSP_FEATURE_IRQ_LEGACY_SHARED_HANDLER_SUPPORT |
---|
| 58 | #endif |
---|
| 59 | #endif |
---|
| 60 | |
---|
[0737710] | 61 | /* |
---|
| 62 | * Flow control is only supported when using interrupts |
---|
| 63 | */ |
---|
[cae0504] | 64 | |
---|
[c8bd3cd] | 65 | const console_flow ns16550_flow_RTSCTS = { |
---|
[0737710] | 66 | ns16550_negate_RTS, /* deviceStopRemoteTx */ |
---|
| 67 | ns16550_assert_RTS /* deviceStartRemoteTx */ |
---|
| 68 | }; |
---|
| 69 | |
---|
[c8bd3cd] | 70 | const console_flow ns16550_flow_DTRCTS = { |
---|
[0737710] | 71 | ns16550_negate_DTR, /* deviceStopRemoteTx */ |
---|
| 72 | ns16550_assert_DTR /* deviceStartRemoteTx */ |
---|
| 73 | }; |
---|
| 74 | |
---|
[c8bd3cd] | 75 | const console_fns ns16550_fns = { |
---|
[064b9be] | 76 | libchip_serial_default_probe, /* deviceProbe */ |
---|
[0737710] | 77 | ns16550_open, /* deviceFirstOpen */ |
---|
[29badca6] | 78 | ns16550_close, /* deviceLastClose */ |
---|
[0737710] | 79 | NULL, /* deviceRead */ |
---|
| 80 | ns16550_write_support_int, /* deviceWrite */ |
---|
[29badca6] | 81 | ns16550_init, /* deviceInitialize */ |
---|
[0737710] | 82 | ns16550_write_polled, /* deviceWritePolled */ |
---|
[8739322] | 83 | ns16550_set_attributes, /* deviceSetAttributes */ |
---|
[6640459d] | 84 | true /* deviceOutputUsesInterrupts */ |
---|
[0737710] | 85 | }; |
---|
| 86 | |
---|
[c8bd3cd] | 87 | const console_fns ns16550_fns_polled = { |
---|
[064b9be] | 88 | libchip_serial_default_probe, /* deviceProbe */ |
---|
[0737710] | 89 | ns16550_open, /* deviceFirstOpen */ |
---|
| 90 | ns16550_close, /* deviceLastClose */ |
---|
| 91 | ns16550_inbyte_nonblocking_polled, /* deviceRead */ |
---|
| 92 | ns16550_write_support_polled, /* deviceWrite */ |
---|
| 93 | ns16550_init, /* deviceInitialize */ |
---|
| 94 | ns16550_write_polled, /* deviceWritePolled */ |
---|
[8739322] | 95 | ns16550_set_attributes, /* deviceSetAttributes */ |
---|
[6640459d] | 96 | false /* deviceOutputUsesInterrupts */ |
---|
[0737710] | 97 | }; |
---|
| 98 | |
---|
[c39148d6] | 99 | static uint32_t NS16550_GetBaudDivisor(const console_tbl *c, uint32_t baud) |
---|
| 100 | { |
---|
| 101 | uint32_t clock = c->ulClock; |
---|
| 102 | uint32_t baudDivisor = (clock != 0 ? clock : 115200) / (baud * 16); |
---|
| 103 | |
---|
| 104 | if (c->deviceType == SERIAL_NS16550_WITH_FDR) { |
---|
| 105 | uint32_t fractionalDivider = 0x10; |
---|
| 106 | uint32_t err = baud; |
---|
| 107 | uint32_t mulVal; |
---|
| 108 | uint32_t divAddVal; |
---|
| 109 | |
---|
| 110 | clock /= 16 * baudDivisor; |
---|
| 111 | for (mulVal = 1; mulVal < 16; ++mulVal) { |
---|
| 112 | for (divAddVal = 0; divAddVal < mulVal; ++divAddVal) { |
---|
| 113 | uint32_t actual = (mulVal * clock) / (mulVal + divAddVal); |
---|
| 114 | uint32_t newErr = actual > baud ? actual - baud : baud - actual; |
---|
| 115 | |
---|
| 116 | if (newErr < err) { |
---|
| 117 | err = newErr; |
---|
| 118 | fractionalDivider = (mulVal << 4) | divAddVal; |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | (*c->setRegister)( |
---|
| 124 | c->ulCtrlPort1, |
---|
| 125 | NS16550_FRACTIONAL_DIVIDER, |
---|
| 126 | fractionalDivider |
---|
| 127 | ); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | return baudDivisor; |
---|
| 131 | } |
---|
| 132 | |
---|
[cae0504] | 133 | /* |
---|
| 134 | * ns16550_init |
---|
| 135 | */ |
---|
| 136 | |
---|
[5455f23a] | 137 | void ns16550_init(int minor) |
---|
[0737710] | 138 | { |
---|
[642c500] | 139 | uintptr_t pNS16550; |
---|
[ee4f57d] | 140 | uint8_t ucDataByte; |
---|
| 141 | uint32_t ulBaudDivisor; |
---|
[0737710] | 142 | ns16550_context *pns16550Context; |
---|
| 143 | setRegister_f setReg; |
---|
| 144 | getRegister_f getReg; |
---|
[4f1173bc] | 145 | console_tbl *c = Console_Port_Tbl [minor]; |
---|
[0737710] | 146 | |
---|
| 147 | pns16550Context=(ns16550_context *)malloc(sizeof(ns16550_context)); |
---|
| 148 | |
---|
[e97e0e0] | 149 | if (pns16550Context == NULL) { |
---|
| 150 | printk( "%s: Error: Not enough memory\n", __func__); |
---|
| 151 | rtems_fatal_error_occurred( 0xdeadbeef); |
---|
| 152 | } |
---|
| 153 | |
---|
[0737710] | 154 | Console_Port_Data[minor].pDeviceContext=(void *)pns16550Context; |
---|
| 155 | pns16550Context->ucModemCtrl=SP_MODEM_IRQ; |
---|
| 156 | |
---|
[4f1173bc] | 157 | pNS16550 = c->ulCtrlPort1; |
---|
| 158 | setReg = c->setRegister; |
---|
| 159 | getReg = c->getRegister; |
---|
[0737710] | 160 | |
---|
| 161 | /* Clear the divisor latch, clear all interrupt enables, |
---|
| 162 | * and reset and |
---|
| 163 | * disable the FIFO's. |
---|
| 164 | */ |
---|
| 165 | |
---|
| 166 | (*setReg)(pNS16550, NS16550_LINE_CONTROL, 0x0); |
---|
[4f1173bc] | 167 | ns16550_enable_interrupts( c, NS16550_DISABLE_ALL_INTR ); |
---|
[0737710] | 168 | |
---|
| 169 | /* Set the divisor latch and set the baud rate. */ |
---|
| 170 | |
---|
[c39148d6] | 171 | ulBaudDivisor = NS16550_GetBaudDivisor(c, (uintptr_t) c->pDeviceParams); |
---|
[0737710] | 172 | ucDataByte = SP_LINE_DLAB; |
---|
| 173 | (*setReg)(pNS16550, NS16550_LINE_CONTROL, ucDataByte); |
---|
[70a9451] | 174 | |
---|
| 175 | /* XXX */ |
---|
[4f1173bc] | 176 | (*setReg)(pNS16550,NS16550_TRANSMIT_BUFFER,(uint8_t)(ulBaudDivisor & 0xffU)); |
---|
| 177 | (*setReg)( |
---|
| 178 | pNS16550,NS16550_INTERRUPT_ENABLE, |
---|
| 179 | (uint8_t)(( ulBaudDivisor >> 8 ) & 0xffU ) |
---|
| 180 | ); |
---|
[0737710] | 181 | |
---|
| 182 | /* Clear the divisor latch and set the character size to eight bits */ |
---|
| 183 | /* with one stop bit and no parity checking. */ |
---|
| 184 | ucDataByte = EIGHT_BITS; |
---|
| 185 | (*setReg)(pNS16550, NS16550_LINE_CONTROL, ucDataByte); |
---|
| 186 | |
---|
| 187 | /* Enable and reset transmit and receive FIFOs. TJA */ |
---|
| 188 | ucDataByte = SP_FIFO_ENABLE; |
---|
| 189 | (*setReg)(pNS16550, NS16550_FIFO_CONTROL, ucDataByte); |
---|
| 190 | |
---|
| 191 | ucDataByte = SP_FIFO_ENABLE | SP_FIFO_RXRST | SP_FIFO_TXRST; |
---|
| 192 | (*setReg)(pNS16550, NS16550_FIFO_CONTROL, ucDataByte); |
---|
| 193 | |
---|
[4f1173bc] | 194 | ns16550_enable_interrupts(c, NS16550_DISABLE_ALL_INTR); |
---|
[0737710] | 195 | |
---|
| 196 | /* Set data terminal ready. */ |
---|
| 197 | /* And open interrupt tristate line */ |
---|
| 198 | (*setReg)(pNS16550, NS16550_MODEM_CONTROL,pns16550Context->ucModemCtrl); |
---|
| 199 | |
---|
[d8b4256] | 200 | (*getReg)(pNS16550, NS16550_LINE_STATUS ); |
---|
| 201 | (*getReg)(pNS16550, NS16550_RECEIVE_BUFFER ); |
---|
[0737710] | 202 | } |
---|
| 203 | |
---|
[cae0504] | 204 | /* |
---|
| 205 | * ns16550_open |
---|
| 206 | */ |
---|
| 207 | |
---|
[5455f23a] | 208 | int ns16550_open( |
---|
[ec5d4505] | 209 | int major, |
---|
| 210 | int minor, |
---|
| 211 | void *arg |
---|
[0737710] | 212 | ) |
---|
| 213 | { |
---|
[ec5d4505] | 214 | rtems_libio_open_close_args_t *oc = (rtems_libio_open_close_args_t *) arg; |
---|
| 215 | struct rtems_termios_tty *tty = (struct rtems_termios_tty *) oc->iop->data1; |
---|
[229bcca8] | 216 | console_tbl *c = Console_Port_Tbl [minor]; |
---|
[7cafde22] | 217 | console_data *d = &Console_Port_Data [minor]; |
---|
| 218 | |
---|
| 219 | d->termios_data = tty; |
---|
[0737710] | 220 | |
---|
[ec5d4505] | 221 | /* Assert DTR */ |
---|
| 222 | if (c->pDeviceFlow != &ns16550_flow_DTRCTS) { |
---|
| 223 | ns16550_assert_DTR( minor); |
---|
[0737710] | 224 | } |
---|
| 225 | |
---|
[ec5d4505] | 226 | /* Set initial baud */ |
---|
[642c500] | 227 | rtems_termios_set_initial_baud( tty, (intptr_t) c->pDeviceParams); |
---|
[ec5d4505] | 228 | |
---|
[7cafde22] | 229 | if (c->pDeviceFns->deviceOutputUsesInterrupts) { |
---|
[29badca6] | 230 | ns16550_initialize_interrupts( minor); |
---|
[4f1173bc] | 231 | ns16550_enable_interrupts( c, NS16550_ENABLE_ALL_INTR_EXCEPT_TX); |
---|
[7cafde22] | 232 | } |
---|
| 233 | |
---|
[ec5d4505] | 234 | return RTEMS_SUCCESSFUL; |
---|
[0737710] | 235 | } |
---|
| 236 | |
---|
[cae0504] | 237 | /* |
---|
| 238 | * ns16550_close |
---|
| 239 | */ |
---|
| 240 | |
---|
[5455f23a] | 241 | int ns16550_close( |
---|
[0737710] | 242 | int major, |
---|
| 243 | int minor, |
---|
| 244 | void * arg |
---|
| 245 | ) |
---|
| 246 | { |
---|
[9495580] | 247 | console_tbl *c = Console_Port_Tbl [minor]; |
---|
[fbf7e58] | 248 | |
---|
[0737710] | 249 | /* |
---|
| 250 | * Negate DTR |
---|
| 251 | */ |
---|
[fbf7e58] | 252 | if (c->pDeviceFlow != &ns16550_flow_DTRCTS) { |
---|
[0737710] | 253 | ns16550_negate_DTR(minor); |
---|
| 254 | } |
---|
| 255 | |
---|
[4f1173bc] | 256 | ns16550_enable_interrupts(c, NS16550_DISABLE_ALL_INTR); |
---|
[7cafde22] | 257 | |
---|
[fbf7e58] | 258 | if (c->pDeviceFns->deviceOutputUsesInterrupts) { |
---|
| 259 | ns16550_cleanup_interrupts(minor); |
---|
| 260 | } |
---|
| 261 | |
---|
[0737710] | 262 | return(RTEMS_SUCCESSFUL); |
---|
| 263 | } |
---|
| 264 | |
---|
[9151ec6] | 265 | /** |
---|
| 266 | * @brief Polled write for NS16550. |
---|
[0737710] | 267 | */ |
---|
[4f1173bc] | 268 | void ns16550_outch_polled(console_tbl *c, char out) |
---|
[0737710] | 269 | { |
---|
[642c500] | 270 | uintptr_t port = c->ulCtrlPort1; |
---|
[ec5d4505] | 271 | getRegister_f get = c->getRegister; |
---|
| 272 | setRegister_f set = c->setRegister; |
---|
| 273 | uint32_t status = 0; |
---|
[9151ec6] | 274 | rtems_interrupt_level level; |
---|
| 275 | |
---|
[ec5d4505] | 276 | /* Save port interrupt mask */ |
---|
| 277 | uint32_t interrupt_mask = get( port, NS16550_INTERRUPT_ENABLE); |
---|
| 278 | |
---|
| 279 | /* Disable port interrupts */ |
---|
[4f1173bc] | 280 | ns16550_enable_interrupts( c, NS16550_DISABLE_ALL_INTR); |
---|
[ec5d4505] | 281 | |
---|
| 282 | while (true) { |
---|
[9151ec6] | 283 | /* Try to transmit the character in a critical section */ |
---|
| 284 | rtems_interrupt_disable( level); |
---|
| 285 | |
---|
| 286 | /* Read the transmitter holding register and check it */ |
---|
| 287 | status = get( port, NS16550_LINE_STATUS); |
---|
| 288 | if ((status & SP_LSR_THOLD) != 0) { |
---|
| 289 | /* Transmit character */ |
---|
[ec5d4505] | 290 | set( port, NS16550_TRANSMIT_BUFFER, out); |
---|
[9151ec6] | 291 | |
---|
| 292 | /* Finished */ |
---|
| 293 | rtems_interrupt_enable( level); |
---|
| 294 | break; |
---|
| 295 | } else { |
---|
| 296 | rtems_interrupt_enable( level); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | /* Wait for transmitter holding register to be empty */ |
---|
| 300 | do { |
---|
| 301 | status = get( port, NS16550_LINE_STATUS); |
---|
| 302 | } while ((status & SP_LSR_THOLD) == 0); |
---|
[0737710] | 303 | } |
---|
[ec5d4505] | 304 | |
---|
| 305 | /* Restore port interrupt mask */ |
---|
| 306 | set( port, NS16550_INTERRUPT_ENABLE, interrupt_mask); |
---|
[0737710] | 307 | } |
---|
| 308 | |
---|
[5455f23a] | 309 | void ns16550_write_polled(int minor, char out) |
---|
[4f1173bc] | 310 | { |
---|
| 311 | console_tbl *c = Console_Port_Tbl [minor]; |
---|
| 312 | |
---|
| 313 | ns16550_outch_polled( c, out ); |
---|
| 314 | } |
---|
| 315 | |
---|
[0737710] | 316 | /* |
---|
| 317 | * These routines provide control of the RTS and DTR lines |
---|
| 318 | */ |
---|
[cae0504] | 319 | |
---|
[0737710] | 320 | /* |
---|
| 321 | * ns16550_assert_RTS |
---|
| 322 | */ |
---|
[cae0504] | 323 | |
---|
[27045a82] | 324 | NS16550_STATIC int ns16550_assert_RTS(int minor) |
---|
[0737710] | 325 | { |
---|
[ee4f57d] | 326 | uint32_t pNS16550; |
---|
| 327 | uint32_t Irql; |
---|
[0737710] | 328 | ns16550_context *pns16550Context; |
---|
| 329 | setRegister_f setReg; |
---|
| 330 | |
---|
| 331 | pns16550Context=(ns16550_context *) Console_Port_Data[minor].pDeviceContext; |
---|
| 332 | |
---|
[229bcca8] | 333 | pNS16550 = Console_Port_Tbl[minor]->ulCtrlPort1; |
---|
| 334 | setReg = Console_Port_Tbl[minor]->setRegister; |
---|
[0737710] | 335 | |
---|
| 336 | /* |
---|
| 337 | * Assert RTS |
---|
| 338 | */ |
---|
| 339 | rtems_interrupt_disable(Irql); |
---|
| 340 | pns16550Context->ucModemCtrl|=SP_MODEM_RTS; |
---|
| 341 | (*setReg)(pNS16550, NS16550_MODEM_CONTROL, pns16550Context->ucModemCtrl); |
---|
| 342 | rtems_interrupt_enable(Irql); |
---|
| 343 | return 0; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | /* |
---|
| 347 | * ns16550_negate_RTS |
---|
| 348 | */ |
---|
[cae0504] | 349 | |
---|
[27045a82] | 350 | NS16550_STATIC int ns16550_negate_RTS(int minor) |
---|
[0737710] | 351 | { |
---|
[ee4f57d] | 352 | uint32_t pNS16550; |
---|
| 353 | uint32_t Irql; |
---|
[0737710] | 354 | ns16550_context *pns16550Context; |
---|
| 355 | setRegister_f setReg; |
---|
| 356 | |
---|
| 357 | pns16550Context=(ns16550_context *) Console_Port_Data[minor].pDeviceContext; |
---|
| 358 | |
---|
[229bcca8] | 359 | pNS16550 = Console_Port_Tbl[minor]->ulCtrlPort1; |
---|
| 360 | setReg = Console_Port_Tbl[minor]->setRegister; |
---|
[0737710] | 361 | |
---|
| 362 | /* |
---|
| 363 | * Negate RTS |
---|
| 364 | */ |
---|
| 365 | rtems_interrupt_disable(Irql); |
---|
| 366 | pns16550Context->ucModemCtrl&=~SP_MODEM_RTS; |
---|
| 367 | (*setReg)(pNS16550, NS16550_MODEM_CONTROL, pns16550Context->ucModemCtrl); |
---|
| 368 | rtems_interrupt_enable(Irql); |
---|
| 369 | return 0; |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | /* |
---|
| 373 | * These flow control routines utilise a connection from the local DTR |
---|
| 374 | * line to the remote CTS line |
---|
| 375 | */ |
---|
[cae0504] | 376 | |
---|
[0737710] | 377 | /* |
---|
| 378 | * ns16550_assert_DTR |
---|
| 379 | */ |
---|
[cae0504] | 380 | |
---|
[27045a82] | 381 | NS16550_STATIC int ns16550_assert_DTR(int minor) |
---|
[0737710] | 382 | { |
---|
[ee4f57d] | 383 | uint32_t pNS16550; |
---|
| 384 | uint32_t Irql; |
---|
[0737710] | 385 | ns16550_context *pns16550Context; |
---|
| 386 | setRegister_f setReg; |
---|
| 387 | |
---|
| 388 | pns16550Context=(ns16550_context *) Console_Port_Data[minor].pDeviceContext; |
---|
| 389 | |
---|
[229bcca8] | 390 | pNS16550 = Console_Port_Tbl[minor]->ulCtrlPort1; |
---|
| 391 | setReg = Console_Port_Tbl[minor]->setRegister; |
---|
[0737710] | 392 | |
---|
| 393 | /* |
---|
| 394 | * Assert DTR |
---|
| 395 | */ |
---|
| 396 | rtems_interrupt_disable(Irql); |
---|
| 397 | pns16550Context->ucModemCtrl|=SP_MODEM_DTR; |
---|
| 398 | (*setReg)(pNS16550, NS16550_MODEM_CONTROL, pns16550Context->ucModemCtrl); |
---|
| 399 | rtems_interrupt_enable(Irql); |
---|
| 400 | return 0; |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | /* |
---|
| 404 | * ns16550_negate_DTR |
---|
| 405 | */ |
---|
[8739322] | 406 | |
---|
[27045a82] | 407 | NS16550_STATIC int ns16550_negate_DTR(int minor) |
---|
[0737710] | 408 | { |
---|
[ee4f57d] | 409 | uint32_t pNS16550; |
---|
| 410 | uint32_t Irql; |
---|
[0737710] | 411 | ns16550_context *pns16550Context; |
---|
| 412 | setRegister_f setReg; |
---|
| 413 | |
---|
| 414 | pns16550Context=(ns16550_context *) Console_Port_Data[minor].pDeviceContext; |
---|
| 415 | |
---|
[229bcca8] | 416 | pNS16550 = Console_Port_Tbl[minor]->ulCtrlPort1; |
---|
| 417 | setReg = Console_Port_Tbl[minor]->setRegister; |
---|
[0737710] | 418 | |
---|
| 419 | /* |
---|
| 420 | * Negate DTR |
---|
| 421 | */ |
---|
| 422 | rtems_interrupt_disable(Irql); |
---|
| 423 | pns16550Context->ucModemCtrl&=~SP_MODEM_DTR; |
---|
| 424 | (*setReg)(pNS16550, NS16550_MODEM_CONTROL,pns16550Context->ucModemCtrl); |
---|
| 425 | rtems_interrupt_enable(Irql); |
---|
| 426 | return 0; |
---|
| 427 | } |
---|
| 428 | |
---|
[8739322] | 429 | /* |
---|
| 430 | * ns16550_set_attributes |
---|
| 431 | * |
---|
| 432 | * This function sets the channel to reflect the requested termios |
---|
| 433 | * port settings. |
---|
| 434 | */ |
---|
| 435 | |
---|
[5455f23a] | 436 | int ns16550_set_attributes( |
---|
[8739322] | 437 | int minor, |
---|
| 438 | const struct termios *t |
---|
| 439 | ) |
---|
| 440 | { |
---|
[ee4f57d] | 441 | uint32_t pNS16550; |
---|
| 442 | uint32_t ulBaudDivisor; |
---|
| 443 | uint8_t ucLineControl; |
---|
| 444 | uint32_t baud_requested; |
---|
[8739322] | 445 | setRegister_f setReg; |
---|
[ee4f57d] | 446 | uint32_t Irql; |
---|
[c39148d6] | 447 | const console_tbl *c = Console_Port_Tbl [minor]; |
---|
[8739322] | 448 | |
---|
[c39148d6] | 449 | pNS16550 = c->ulCtrlPort1; |
---|
| 450 | setReg = c->setRegister; |
---|
[8739322] | 451 | |
---|
| 452 | /* |
---|
| 453 | * Calculate the baud rate divisor |
---|
| 454 | */ |
---|
| 455 | |
---|
[c39148d6] | 456 | baud_requested = rtems_termios_baud_to_number(t->c_cflag); |
---|
| 457 | ulBaudDivisor = NS16550_GetBaudDivisor(c, baud_requested); |
---|
[8739322] | 458 | |
---|
| 459 | ucLineControl = 0; |
---|
| 460 | |
---|
| 461 | /* |
---|
| 462 | * Parity |
---|
| 463 | */ |
---|
| 464 | |
---|
| 465 | if (t->c_cflag & PARENB) { |
---|
| 466 | ucLineControl |= SP_LINE_PAR; |
---|
| 467 | if (!(t->c_cflag & PARODD)) |
---|
| 468 | ucLineControl |= SP_LINE_ODD; |
---|
| 469 | } |
---|
| 470 | |
---|
| 471 | /* |
---|
| 472 | * Character Size |
---|
| 473 | */ |
---|
| 474 | |
---|
| 475 | if (t->c_cflag & CSIZE) { |
---|
| 476 | switch (t->c_cflag & CSIZE) { |
---|
| 477 | case CS5: ucLineControl |= FIVE_BITS; break; |
---|
| 478 | case CS6: ucLineControl |= SIX_BITS; break; |
---|
| 479 | case CS7: ucLineControl |= SEVEN_BITS; break; |
---|
| 480 | case CS8: ucLineControl |= EIGHT_BITS; break; |
---|
| 481 | } |
---|
| 482 | } else { |
---|
| 483 | ucLineControl |= EIGHT_BITS; /* default to 9600,8,N,1 */ |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | /* |
---|
| 487 | * Stop Bits |
---|
| 488 | */ |
---|
| 489 | |
---|
| 490 | if (t->c_cflag & CSTOPB) { |
---|
| 491 | ucLineControl |= SP_LINE_STOP; /* 2 stop bits */ |
---|
| 492 | } else { |
---|
| 493 | ; /* 1 stop bit */ |
---|
| 494 | } |
---|
| 495 | |
---|
| 496 | /* |
---|
| 497 | * Now actually set the chip |
---|
| 498 | */ |
---|
| 499 | |
---|
| 500 | rtems_interrupt_disable(Irql); |
---|
| 501 | |
---|
| 502 | /* |
---|
[a3d3d9a] | 503 | * Set the baud rate |
---|
[48a22ed] | 504 | * |
---|
| 505 | * NOTE: When the Divisor Latch Access Bit (DLAB) is set to 1, |
---|
| 506 | * the transmit buffer and interrupt enable registers |
---|
| 507 | * turn into the LSB and MSB divisor latch registers. |
---|
[8739322] | 508 | */ |
---|
| 509 | |
---|
| 510 | (*setReg)(pNS16550, NS16550_LINE_CONTROL, SP_LINE_DLAB); |
---|
| 511 | (*setReg)(pNS16550, NS16550_TRANSMIT_BUFFER, ulBaudDivisor&0xff); |
---|
| 512 | (*setReg)(pNS16550, NS16550_INTERRUPT_ENABLE, (ulBaudDivisor>>8)&0xff); |
---|
| 513 | |
---|
| 514 | /* |
---|
| 515 | * Now write the line control |
---|
| 516 | */ |
---|
| 517 | (*setReg)(pNS16550, NS16550_LINE_CONTROL, ucLineControl ); |
---|
| 518 | |
---|
| 519 | rtems_interrupt_enable(Irql); |
---|
| 520 | |
---|
[07a3253d] | 521 | return 0; |
---|
[8739322] | 522 | } |
---|
| 523 | |
---|
[2e3f4398] | 524 | #if defined(BSP_FEATURE_IRQ_EXTENSION) || defined(BSP_FEATURE_IRQ_LEGACY) |
---|
[0737710] | 525 | |
---|
[ec5d4505] | 526 | /** |
---|
| 527 | * @brief Process interrupt. |
---|
| 528 | */ |
---|
| 529 | NS16550_STATIC void ns16550_process( int minor) |
---|
[0737710] | 530 | { |
---|
[229bcca8] | 531 | console_tbl *c = Console_Port_Tbl [minor]; |
---|
[e97e0e0] | 532 | console_data *d = &Console_Port_Data [minor]; |
---|
[ec5d4505] | 533 | ns16550_context *ctx = d->pDeviceContext; |
---|
| 534 | uint32_t port = c->ulCtrlPort1; |
---|
| 535 | getRegister_f get = c->getRegister; |
---|
| 536 | int i = 0; |
---|
| 537 | char buf [SP_FIFO_SIZE]; |
---|
| 538 | |
---|
| 539 | /* Iterate until no more interrupts are pending */ |
---|
[0737710] | 540 | do { |
---|
[ec5d4505] | 541 | /* Fetch received characters */ |
---|
| 542 | for (i = 0; i < SP_FIFO_SIZE; ++i) { |
---|
| 543 | if ((get( port, NS16550_LINE_STATUS) & SP_LSR_RDY) != 0) { |
---|
| 544 | buf [i] = (char) get(port, NS16550_RECEIVE_BUFFER); |
---|
| 545 | } else { |
---|
[0737710] | 546 | break; |
---|
| 547 | } |
---|
| 548 | } |
---|
| 549 | |
---|
[ec5d4505] | 550 | /* Enqueue fetched characters */ |
---|
| 551 | rtems_termios_enqueue_raw_characters( d->termios_data, buf, i); |
---|
[70a9451] | 552 | |
---|
[ec5d4505] | 553 | /* Check if we can dequeue transmitted characters */ |
---|
| 554 | if (ctx->transmitFifoChars > 0 |
---|
| 555 | && (get( port, NS16550_LINE_STATUS) & SP_LSR_THOLD) != 0) { |
---|
[70a9451] | 556 | |
---|
[ec5d4505] | 557 | /* Dequeue transmitted characters */ |
---|
[15f3a91] | 558 | rtems_termios_dequeue_characters( |
---|
| 559 | d->termios_data, |
---|
| 560 | ctx->transmitFifoChars |
---|
| 561 | ); |
---|
[0737710] | 562 | } |
---|
[ec5d4505] | 563 | } while ((get( port, NS16550_INTERRUPT_ID) & SP_IID_0) == 0); |
---|
| 564 | } |
---|
| 565 | #endif |
---|
| 566 | |
---|
| 567 | /** |
---|
| 568 | * @brief Transmits up to @a len characters from @a buf. |
---|
| 569 | * |
---|
| 570 | * This routine is invoked either from task context with disabled interrupts to |
---|
| 571 | * start a new transmission process with exactly one character in case of an |
---|
| 572 | * idle output state or from the interrupt handler to refill the transmitter. |
---|
| 573 | * |
---|
| 574 | * Returns always zero. |
---|
| 575 | */ |
---|
[5455f23a] | 576 | ssize_t ns16550_write_support_int( |
---|
[ec5d4505] | 577 | int minor, |
---|
| 578 | const char *buf, |
---|
[3ed964f9] | 579 | size_t len |
---|
[ec5d4505] | 580 | ) |
---|
| 581 | { |
---|
[229bcca8] | 582 | console_tbl *c = Console_Port_Tbl [minor]; |
---|
[ec5d4505] | 583 | console_data *d = &Console_Port_Data [minor]; |
---|
| 584 | ns16550_context *ctx = d->pDeviceContext; |
---|
| 585 | uint32_t port = c->ulCtrlPort1; |
---|
| 586 | setRegister_f set = c->setRegister; |
---|
| 587 | int i = 0; |
---|
| 588 | int out = len > SP_FIFO_SIZE ? SP_FIFO_SIZE : len; |
---|
| 589 | |
---|
| 590 | for (i = 0; i < out; ++i) { |
---|
| 591 | set( port, NS16550_TRANSMIT_BUFFER, buf [i]); |
---|
| 592 | } |
---|
| 593 | |
---|
[15f3a91] | 594 | ctx->transmitFifoChars = out; |
---|
| 595 | |
---|
| 596 | if (out > 0) { |
---|
[4f1173bc] | 597 | ns16550_enable_interrupts( c, NS16550_ENABLE_ALL_INTR); |
---|
[15f3a91] | 598 | } else { |
---|
| 599 | ns16550_enable_interrupts( c, NS16550_ENABLE_ALL_INTR_EXCEPT_TX); |
---|
[ec5d4505] | 600 | } |
---|
[c163605f] | 601 | |
---|
[ec5d4505] | 602 | return 0; |
---|
[c163605f] | 603 | } |
---|
| 604 | |
---|
[0737710] | 605 | /* |
---|
[70a9451] | 606 | * ns16550_enable_interrupts |
---|
[0737710] | 607 | * |
---|
[70a9451] | 608 | * This routine initializes the port to have the specified interrupts masked. |
---|
[0737710] | 609 | */ |
---|
[27045a82] | 610 | NS16550_STATIC void ns16550_enable_interrupts( |
---|
[4f1173bc] | 611 | console_tbl *c, |
---|
| 612 | int mask |
---|
[0737710] | 613 | ) |
---|
| 614 | { |
---|
[ee4f57d] | 615 | uint32_t pNS16550; |
---|
[70a9451] | 616 | setRegister_f setReg; |
---|
[0737710] | 617 | |
---|
[4f1173bc] | 618 | pNS16550 = c->ulCtrlPort1; |
---|
| 619 | setReg = c->setRegister; |
---|
[0737710] | 620 | |
---|
[70a9451] | 621 | (*setReg)(pNS16550, NS16550_INTERRUPT_ENABLE, mask); |
---|
[0737710] | 622 | } |
---|
| 623 | |
---|
[2e3f4398] | 624 | #if defined(BSP_FEATURE_IRQ_EXTENSION) || defined(BSP_FEATURE_IRQ_LEGACY) |
---|
[5455f23a] | 625 | void ns16550_isr(void *arg) |
---|
[2e3f4398] | 626 | { |
---|
[e97e0e0] | 627 | int minor = (int) arg; |
---|
| 628 | |
---|
| 629 | ns16550_process( minor); |
---|
[2e3f4398] | 630 | } |
---|
| 631 | #endif |
---|
[e97e0e0] | 632 | |
---|
[70a9451] | 633 | /* |
---|
| 634 | * ns16550_initialize_interrupts |
---|
| 635 | * |
---|
| 636 | * This routine initializes the port to operate in interrupt driver mode. |
---|
[a3d3d9a] | 637 | */ |
---|
[e97e0e0] | 638 | NS16550_STATIC void ns16550_initialize_interrupts( int minor) |
---|
[0737710] | 639 | { |
---|
[69be08ee] | 640 | #if defined(BSP_FEATURE_IRQ_EXTENSION) || defined(BSP_FEATURE_IRQ_LEGACY) |
---|
[9495580] | 641 | console_tbl *c = Console_Port_Tbl [minor]; |
---|
[2e3f4398] | 642 | #endif |
---|
[e97e0e0] | 643 | |
---|
| 644 | #ifdef BSP_FEATURE_IRQ_EXTENSION |
---|
| 645 | { |
---|
| 646 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
| 647 | sc = rtems_interrupt_handler_install( |
---|
| 648 | c->ulIntVector, |
---|
| 649 | "NS16550", |
---|
| 650 | RTEMS_INTERRUPT_SHARED, |
---|
| 651 | ns16550_isr, |
---|
| 652 | (void *) minor |
---|
| 653 | ); |
---|
| 654 | if (sc != RTEMS_SUCCESSFUL) { |
---|
| 655 | /* FIXME */ |
---|
| 656 | printk( "%s: Error: Install interrupt handler\n", __func__); |
---|
| 657 | rtems_fatal_error_occurred( 0xdeadbeef); |
---|
| 658 | } |
---|
| 659 | } |
---|
[69be08ee] | 660 | #elif defined(BSP_FEATURE_IRQ_LEGACY) |
---|
[e97e0e0] | 661 | { |
---|
| 662 | int rv = 0; |
---|
| 663 | #ifdef BSP_FEATURE_IRQ_LEGACY_SHARED_HANDLER_SUPPORT |
---|
| 664 | rtems_irq_connect_data cd = { |
---|
| 665 | c->ulIntVector, |
---|
| 666 | ns16550_isr, |
---|
[95ef82c] | 667 | (void *) minor, |
---|
[e97e0e0] | 668 | NULL, |
---|
| 669 | NULL, |
---|
| 670 | NULL, |
---|
| 671 | NULL |
---|
| 672 | }; |
---|
| 673 | rv = BSP_install_rtems_shared_irq_handler( &cd); |
---|
| 674 | #else |
---|
| 675 | rtems_irq_connect_data cd = { |
---|
| 676 | c->ulIntVector, |
---|
| 677 | ns16550_isr, |
---|
[0fbd231] | 678 | (void *) minor, |
---|
[e97e0e0] | 679 | NULL, |
---|
| 680 | NULL, |
---|
| 681 | NULL |
---|
| 682 | }; |
---|
| 683 | rv = BSP_install_rtems_irq_handler( &cd); |
---|
| 684 | #endif |
---|
| 685 | if (rv == 0) { |
---|
| 686 | /* FIXME */ |
---|
| 687 | printk( "%s: Error: Install interrupt handler\n", __func__); |
---|
| 688 | rtems_fatal_error_occurred( 0xdeadbeef); |
---|
| 689 | } |
---|
| 690 | } |
---|
| 691 | #endif |
---|
[0737710] | 692 | } |
---|
| 693 | |
---|
[fbf7e58] | 694 | NS16550_STATIC void ns16550_cleanup_interrupts(int minor) |
---|
| 695 | { |
---|
| 696 | #if defined(BSP_FEATURE_IRQ_EXTENSION) |
---|
| 697 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
[9495580] | 698 | console_tbl *c = Console_Port_Tbl [minor]; |
---|
[fbf7e58] | 699 | sc = rtems_interrupt_handler_remove( |
---|
| 700 | c->ulIntVector, |
---|
| 701 | ns16550_isr, |
---|
| 702 | (void *) minor |
---|
| 703 | ); |
---|
| 704 | if (sc != RTEMS_SUCCESSFUL) { |
---|
| 705 | /* FIXME */ |
---|
| 706 | printk("%s: Error: Remove interrupt handler\n", __func__); |
---|
| 707 | rtems_fatal_error_occurred(0xdeadbeef); |
---|
| 708 | } |
---|
| 709 | #elif defined(BSP_FEATURE_IRQ_LEGACY) |
---|
| 710 | int rv = 0; |
---|
[9495580] | 711 | console_tbl *c = Console_Port_Tbl [minor]; |
---|
[fbf7e58] | 712 | rtems_irq_connect_data cd = { |
---|
| 713 | .name = c->ulIntVector, |
---|
| 714 | .hdl = ns16550_isr, |
---|
| 715 | .handle = (void *) minor |
---|
| 716 | }; |
---|
| 717 | rv = BSP_remove_rtems_irq_handler(&cd); |
---|
| 718 | if (rv == 0) { |
---|
| 719 | /* FIXME */ |
---|
| 720 | printk("%s: Error: Remove interrupt handler\n", __func__); |
---|
| 721 | rtems_fatal_error_occurred(0xdeadbeef); |
---|
| 722 | } |
---|
| 723 | #endif |
---|
| 724 | } |
---|
| 725 | |
---|
[a3d3d9a] | 726 | /* |
---|
[0737710] | 727 | * ns16550_write_support_polled |
---|
| 728 | * |
---|
| 729 | * Console Termios output entry point. |
---|
| 730 | * |
---|
| 731 | */ |
---|
[70a9451] | 732 | |
---|
[5455f23a] | 733 | ssize_t ns16550_write_support_polled( |
---|
[a3d3d9a] | 734 | int minor, |
---|
| 735 | const char *buf, |
---|
[3ed964f9] | 736 | size_t len |
---|
[0737710] | 737 | ) |
---|
| 738 | { |
---|
| 739 | int nwrite = 0; |
---|
| 740 | |
---|
| 741 | /* |
---|
| 742 | * poll each byte in the string out of the port. |
---|
| 743 | */ |
---|
| 744 | while (nwrite < len) { |
---|
| 745 | /* |
---|
| 746 | * transmit character |
---|
| 747 | */ |
---|
| 748 | ns16550_write_polled(minor, *buf++); |
---|
| 749 | nwrite++; |
---|
| 750 | } |
---|
| 751 | |
---|
| 752 | /* |
---|
| 753 | * return the number of bytes written. |
---|
| 754 | */ |
---|
| 755 | return nwrite; |
---|
| 756 | } |
---|
| 757 | |
---|
[a3d3d9a] | 758 | /* |
---|
[4f1173bc] | 759 | * Debug gets() support |
---|
[0737710] | 760 | */ |
---|
[4f1173bc] | 761 | int ns16550_inch_polled( |
---|
| 762 | console_tbl *c |
---|
[0737710] | 763 | ) |
---|
| 764 | { |
---|
[ee4f57d] | 765 | uint32_t pNS16550; |
---|
[0737710] | 766 | unsigned char ucLineStatus; |
---|
[36cfb55f] | 767 | uint8_t cChar; |
---|
[0737710] | 768 | getRegister_f getReg; |
---|
| 769 | |
---|
[4f1173bc] | 770 | pNS16550 = c->ulCtrlPort1; |
---|
| 771 | getReg = c->getRegister; |
---|
[0737710] | 772 | |
---|
| 773 | ucLineStatus = (*getReg)(pNS16550, NS16550_LINE_STATUS); |
---|
[4f1173bc] | 774 | if (ucLineStatus & SP_LSR_RDY) { |
---|
[0737710] | 775 | cChar = (*getReg)(pNS16550, NS16550_RECEIVE_BUFFER); |
---|
[8a2d4f2b] | 776 | return (int)cChar; |
---|
[0737710] | 777 | } |
---|
[4f1173bc] | 778 | return -1; |
---|
| 779 | } |
---|
| 780 | |
---|
| 781 | /* |
---|
| 782 | * ns16550_inbyte_nonblocking_polled |
---|
| 783 | * |
---|
| 784 | * Console Termios polling input entry point. |
---|
| 785 | */ |
---|
[5455f23a] | 786 | int ns16550_inbyte_nonblocking_polled(int minor) |
---|
[4f1173bc] | 787 | { |
---|
| 788 | console_tbl *c = Console_Port_Tbl [minor]; |
---|
| 789 | |
---|
| 790 | return ns16550_inch_polled( c ); |
---|
[0737710] | 791 | } |
---|