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