1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Base port operations for 8250/16550-type serial ports 4 * 5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. 6 * Split from 8250_core.c, Copyright (C) 2001 Russell King. 7 * 8 * A note about mapbase / membase 9 * 10 * mapbase is the physical address of the IO port. 11 * membase is an 'ioremapped' cookie. 12 */ 13 14 #include <linux/module.h> 15 #include <linux/moduleparam.h> 16 #include <linux/ioport.h> 17 #include <linux/init.h> 18 #include <linux/irq.h> 19 #include <linux/console.h> 20 #include <linux/gpio/consumer.h> 21 #include <linux/lockdep.h> 22 #include <linux/sysrq.h> 23 #include <linux/delay.h> 24 #include <linux/platform_device.h> 25 #include <linux/tty.h> 26 #include <linux/ratelimit.h> 27 #include <linux/tty_flip.h> 28 #include <linux/serial.h> 29 #include <linux/serial_8250.h> 30 #include <linux/nmi.h> 31 #include <linux/mutex.h> 32 #include <linux/slab.h> 33 #include <linux/uaccess.h> 34 #include <linux/pm_runtime.h> 35 #include <linux/ktime.h> 36 37 #include <asm/io.h> 38 #include <asm/irq.h> 39 40 #include "8250.h" 41 42 /* 43 * Here we define the default xmit fifo size used for each type of UART. 44 */ 45 static const struct serial8250_config uart_config[] = { 46 [PORT_UNKNOWN] = { 47 .name = "unknown", 48 .fifo_size = 1, 49 .tx_loadsz = 1, 50 }, 51 [PORT_8250] = { 52 .name = "8250", 53 .fifo_size = 1, 54 .tx_loadsz = 1, 55 }, 56 [PORT_16450] = { 57 .name = "16450", 58 .fifo_size = 1, 59 .tx_loadsz = 1, 60 }, 61 [PORT_16550] = { 62 .name = "16550", 63 .fifo_size = 1, 64 .tx_loadsz = 1, 65 }, 66 [PORT_16550A] = { 67 .name = "16550A", 68 .fifo_size = 16, 69 .tx_loadsz = 16, 70 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 71 .rxtrig_bytes = {1, 4, 8, 14}, 72 .flags = UART_CAP_FIFO, 73 }, 74 [PORT_CIRRUS] = { 75 .name = "Cirrus", 76 .fifo_size = 1, 77 .tx_loadsz = 1, 78 }, 79 [PORT_16650] = { 80 .name = "ST16650", 81 .fifo_size = 1, 82 .tx_loadsz = 1, 83 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP, 84 }, 85 [PORT_16650V2] = { 86 .name = "ST16650V2", 87 .fifo_size = 32, 88 .tx_loadsz = 16, 89 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 | 90 UART_FCR_T_TRIG_00, 91 .rxtrig_bytes = {8, 16, 24, 28}, 92 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP, 93 }, 94 [PORT_16750] = { 95 .name = "TI16750", 96 .fifo_size = 64, 97 .tx_loadsz = 64, 98 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 | 99 UART_FCR7_64BYTE, 100 .rxtrig_bytes = {1, 16, 32, 56}, 101 .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE, 102 }, 103 [PORT_STARTECH] = { 104 .name = "Startech", 105 .fifo_size = 1, 106 .tx_loadsz = 1, 107 }, 108 [PORT_16C950] = { 109 .name = "16C950/954", 110 .fifo_size = 128, 111 .tx_loadsz = 128, 112 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01, 113 .rxtrig_bytes = {16, 32, 112, 120}, 114 /* UART_CAP_EFR breaks billionon CF bluetooth card. */ 115 .flags = UART_CAP_FIFO | UART_CAP_SLEEP, 116 }, 117 [PORT_16654] = { 118 .name = "ST16654", 119 .fifo_size = 64, 120 .tx_loadsz = 32, 121 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 | 122 UART_FCR_T_TRIG_10, 123 .rxtrig_bytes = {8, 16, 56, 60}, 124 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP, 125 }, 126 [PORT_16850] = { 127 .name = "XR16850", 128 .fifo_size = 128, 129 .tx_loadsz = 128, 130 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 131 .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP, 132 }, 133 [PORT_RSA] = { 134 .name = "RSA", 135 .fifo_size = 2048, 136 .tx_loadsz = 2048, 137 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11, 138 .flags = UART_CAP_FIFO, 139 }, 140 [PORT_NS16550A] = { 141 .name = "NS16550A", 142 .fifo_size = 16, 143 .tx_loadsz = 16, 144 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 145 .flags = UART_CAP_FIFO | UART_NATSEMI, 146 }, 147 [PORT_XSCALE] = { 148 .name = "XScale", 149 .fifo_size = 32, 150 .tx_loadsz = 32, 151 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 152 .flags = UART_CAP_FIFO | UART_CAP_UUE | UART_CAP_RTOIE, 153 }, 154 [PORT_OCTEON] = { 155 .name = "OCTEON", 156 .fifo_size = 64, 157 .tx_loadsz = 64, 158 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 159 .flags = UART_CAP_FIFO, 160 }, 161 [PORT_U6_16550A] = { 162 .name = "U6_16550A", 163 .fifo_size = 64, 164 .tx_loadsz = 64, 165 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 166 .flags = UART_CAP_FIFO | UART_CAP_AFE, 167 }, 168 [PORT_TEGRA] = { 169 .name = "Tegra", 170 .fifo_size = 32, 171 .tx_loadsz = 8, 172 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 | 173 UART_FCR_T_TRIG_01, 174 .rxtrig_bytes = {1, 4, 8, 14}, 175 .flags = UART_CAP_FIFO | UART_CAP_RTOIE, 176 }, 177 [PORT_XR17D15X] = { 178 .name = "XR17D15X", 179 .fifo_size = 64, 180 .tx_loadsz = 64, 181 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 182 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR | 183 UART_CAP_SLEEP, 184 }, 185 [PORT_XR17V35X] = { 186 .name = "XR17V35X", 187 .fifo_size = 256, 188 .tx_loadsz = 256, 189 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11 | 190 UART_FCR_T_TRIG_11, 191 .flags = UART_CAP_FIFO | UART_CAP_AFE | UART_CAP_EFR | 192 UART_CAP_SLEEP, 193 }, 194 [PORT_LPC3220] = { 195 .name = "LPC3220", 196 .fifo_size = 64, 197 .tx_loadsz = 32, 198 .fcr = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO | 199 UART_FCR_R_TRIG_00 | UART_FCR_T_TRIG_00, 200 .flags = UART_CAP_FIFO, 201 }, 202 [PORT_BRCM_TRUMANAGE] = { 203 .name = "TruManage", 204 .fifo_size = 1, 205 .tx_loadsz = 1024, 206 .flags = UART_CAP_HFIFO, 207 }, 208 [PORT_8250_CIR] = { 209 .name = "CIR port" 210 }, 211 [PORT_ALTR_16550_F32] = { 212 .name = "Altera 16550 FIFO32", 213 .fifo_size = 32, 214 .tx_loadsz = 32, 215 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 216 .rxtrig_bytes = {1, 8, 16, 30}, 217 .flags = UART_CAP_FIFO | UART_CAP_AFE, 218 }, 219 [PORT_ALTR_16550_F64] = { 220 .name = "Altera 16550 FIFO64", 221 .fifo_size = 64, 222 .tx_loadsz = 64, 223 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 224 .rxtrig_bytes = {1, 16, 32, 62}, 225 .flags = UART_CAP_FIFO | UART_CAP_AFE, 226 }, 227 [PORT_ALTR_16550_F128] = { 228 .name = "Altera 16550 FIFO128", 229 .fifo_size = 128, 230 .tx_loadsz = 128, 231 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 232 .rxtrig_bytes = {1, 32, 64, 126}, 233 .flags = UART_CAP_FIFO | UART_CAP_AFE, 234 }, 235 /* 236 * tx_loadsz is set to 63-bytes instead of 64-bytes to implement 237 * workaround of errata A-008006 which states that tx_loadsz should 238 * be configured less than Maximum supported fifo bytes. 239 */ 240 [PORT_16550A_FSL64] = { 241 .name = "16550A_FSL64", 242 .fifo_size = 64, 243 .tx_loadsz = 63, 244 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 | 245 UART_FCR7_64BYTE, 246 .flags = UART_CAP_FIFO | UART_CAP_NOTEMT, 247 }, 248 [PORT_RT2880] = { 249 .name = "Palmchip BK-3103", 250 .fifo_size = 16, 251 .tx_loadsz = 16, 252 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 253 .rxtrig_bytes = {1, 4, 8, 14}, 254 .flags = UART_CAP_FIFO, 255 }, 256 [PORT_DA830] = { 257 .name = "TI DA8xx/66AK2x", 258 .fifo_size = 16, 259 .tx_loadsz = 16, 260 .fcr = UART_FCR_DMA_SELECT | UART_FCR_ENABLE_FIFO | 261 UART_FCR_R_TRIG_10, 262 .rxtrig_bytes = {1, 4, 8, 14}, 263 .flags = UART_CAP_FIFO | UART_CAP_AFE, 264 }, 265 [PORT_MTK_BTIF] = { 266 .name = "MediaTek BTIF", 267 .fifo_size = 16, 268 .tx_loadsz = 16, 269 .fcr = UART_FCR_ENABLE_FIFO | 270 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT, 271 .flags = UART_CAP_FIFO, 272 }, 273 [PORT_NPCM] = { 274 .name = "Nuvoton 16550", 275 .fifo_size = 16, 276 .tx_loadsz = 16, 277 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 | 278 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT, 279 .rxtrig_bytes = {1, 4, 8, 14}, 280 .flags = UART_CAP_FIFO, 281 }, 282 [PORT_SUNIX] = { 283 .name = "Sunix", 284 .fifo_size = 128, 285 .tx_loadsz = 128, 286 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 287 .rxtrig_bytes = {1, 32, 64, 112}, 288 .flags = UART_CAP_FIFO | UART_CAP_SLEEP, 289 }, 290 [PORT_ASPEED_VUART] = { 291 .name = "ASPEED VUART", 292 .fifo_size = 16, 293 .tx_loadsz = 16, 294 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_00, 295 .rxtrig_bytes = {1, 4, 8, 14}, 296 .flags = UART_CAP_FIFO, 297 }, 298 [PORT_MCHP16550A] = { 299 .name = "MCHP16550A", 300 .fifo_size = 256, 301 .tx_loadsz = 256, 302 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01, 303 .rxtrig_bytes = {2, 66, 130, 194}, 304 .flags = UART_CAP_FIFO, 305 }, 306 [PORT_BCM7271] = { 307 .name = "Broadcom BCM7271 UART", 308 .fifo_size = 32, 309 .tx_loadsz = 32, 310 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01, 311 .rxtrig_bytes = {1, 8, 16, 30}, 312 .flags = UART_CAP_FIFO | UART_CAP_AFE, 313 }, 314 [PORT_MUEX50] = { 315 .name = "Moxa MUEx50 UART", 316 .fifo_size = 128, 317 .tx_loadsz = 128, 318 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, 319 .rxtrig_bytes = {15, 31, 63, 111}, 320 .flags = UART_CAP_FIFO, 321 }, 322 [UART_PORT_AIROHA] = { 323 .name = "Airoha UART", 324 .fifo_size = 8, 325 .tx_loadsz = 1, 326 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 | UART_FCR_CLEAR_RCVR, 327 .rxtrig_bytes = {1, 4, 4, 4}, 328 .flags = UART_CAP_FIFO, 329 }, 330 [UART_PORT_AIROHA_HS] = { 331 .name = "Airoha HSUART", 332 .fifo_size = 128, 333 .tx_loadsz = 128, 334 .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 | UART_FCR_CLEAR_RCVR, 335 .rxtrig_bytes = {1, 4}, 336 .flags = UART_CAP_FIFO, 337 }, 338 }; 339 340 /* Uart divisor latch read */ 341 static u32 default_serial_dl_read(struct uart_8250_port *up) 342 { 343 /* Assign these in pieces to truncate any bits above 7. */ 344 unsigned char dll = serial_in(up, UART_DLL); 345 unsigned char dlm = serial_in(up, UART_DLM); 346 347 return dll | dlm << 8; 348 } 349 350 /* Uart divisor latch write */ 351 static void default_serial_dl_write(struct uart_8250_port *up, u32 value) 352 { 353 serial_out(up, UART_DLL, value & 0xff); 354 serial_out(up, UART_DLM, value >> 8 & 0xff); 355 } 356 357 #ifdef CONFIG_HAS_IOPORT 358 static u32 hub6_serial_in(struct uart_port *p, unsigned int offset) 359 { 360 offset = offset << p->regshift; 361 outb(p->hub6 - 1 + offset, p->iobase); 362 return inb(p->iobase + 1); 363 } 364 365 static void hub6_serial_out(struct uart_port *p, unsigned int offset, u32 value) 366 { 367 offset = offset << p->regshift; 368 outb(p->hub6 - 1 + offset, p->iobase); 369 outb(value, p->iobase + 1); 370 } 371 #endif /* CONFIG_HAS_IOPORT */ 372 373 static u32 mem_serial_in(struct uart_port *p, unsigned int offset) 374 { 375 offset = offset << p->regshift; 376 return readb(p->membase + offset); 377 } 378 379 static void mem_serial_out(struct uart_port *p, unsigned int offset, u32 value) 380 { 381 offset = offset << p->regshift; 382 writeb(value, p->membase + offset); 383 } 384 385 static void mem16_serial_out(struct uart_port *p, unsigned int offset, u32 value) 386 { 387 offset = offset << p->regshift; 388 writew(value, p->membase + offset); 389 } 390 391 static u32 mem16_serial_in(struct uart_port *p, unsigned int offset) 392 { 393 offset = offset << p->regshift; 394 return readw(p->membase + offset); 395 } 396 397 static void mem32_serial_out(struct uart_port *p, unsigned int offset, u32 value) 398 { 399 offset = offset << p->regshift; 400 writel(value, p->membase + offset); 401 } 402 403 static u32 mem32_serial_in(struct uart_port *p, unsigned int offset) 404 { 405 offset = offset << p->regshift; 406 return readl(p->membase + offset); 407 } 408 409 static void mem32be_serial_out(struct uart_port *p, unsigned int offset, u32 value) 410 { 411 offset = offset << p->regshift; 412 iowrite32be(value, p->membase + offset); 413 } 414 415 static u32 mem32be_serial_in(struct uart_port *p, unsigned int offset) 416 { 417 offset = offset << p->regshift; 418 return ioread32be(p->membase + offset); 419 } 420 421 #ifdef CONFIG_HAS_IOPORT 422 static u32 io_serial_in(struct uart_port *p, unsigned int offset) 423 { 424 offset = offset << p->regshift; 425 return inb(p->iobase + offset); 426 } 427 428 static void io_serial_out(struct uart_port *p, unsigned int offset, u32 value) 429 { 430 offset = offset << p->regshift; 431 outb(value, p->iobase + offset); 432 } 433 #endif 434 static u32 no_serial_in(struct uart_port *p, unsigned int offset) 435 { 436 return ~0U; 437 } 438 439 static void no_serial_out(struct uart_port *p, unsigned int offset, u32 value) 440 { 441 } 442 443 static int serial8250_default_handle_irq(struct uart_port *port); 444 445 static void set_io_from_upio(struct uart_port *p) 446 { 447 struct uart_8250_port *up = up_to_u8250p(p); 448 449 up->dl_read = default_serial_dl_read; 450 up->dl_write = default_serial_dl_write; 451 452 switch (p->iotype) { 453 #ifdef CONFIG_HAS_IOPORT 454 case UPIO_HUB6: 455 p->serial_in = hub6_serial_in; 456 p->serial_out = hub6_serial_out; 457 break; 458 #endif 459 460 case UPIO_MEM: 461 p->serial_in = mem_serial_in; 462 p->serial_out = mem_serial_out; 463 break; 464 465 case UPIO_MEM16: 466 p->serial_in = mem16_serial_in; 467 p->serial_out = mem16_serial_out; 468 break; 469 470 case UPIO_MEM32: 471 p->serial_in = mem32_serial_in; 472 p->serial_out = mem32_serial_out; 473 break; 474 475 case UPIO_MEM32BE: 476 p->serial_in = mem32be_serial_in; 477 p->serial_out = mem32be_serial_out; 478 break; 479 #ifdef CONFIG_HAS_IOPORT 480 case UPIO_PORT: 481 p->serial_in = io_serial_in; 482 p->serial_out = io_serial_out; 483 break; 484 #endif 485 case UPIO_AU: 486 break; 487 default: 488 WARN(p->iotype != UPIO_PORT || p->iobase, 489 "Unsupported UART type %x\n", p->iotype); 490 p->serial_in = no_serial_in; 491 p->serial_out = no_serial_out; 492 } 493 /* Remember loaded iotype */ 494 up->cur_iotype = p->iotype; 495 p->handle_irq = serial8250_default_handle_irq; 496 } 497 498 static void 499 serial_port_out_sync(struct uart_port *p, int offset, int value) 500 { 501 if (uart_iotype_mmio(p->iotype)) { 502 p->serial_out(p, offset, value); 503 p->serial_in(p, UART_LCR); /* safe, no side-effects */ 504 } else { 505 p->serial_out(p, offset, value); 506 } 507 } 508 509 /* 510 * FIFO support. 511 */ 512 void serial8250_clear_fifos(struct uart_8250_port *p) 513 { 514 if (p->capabilities & UART_CAP_FIFO) { 515 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO); 516 serial_out(p, UART_FCR, UART_FCR_ENABLE_FIFO | 517 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT); 518 serial_out(p, UART_FCR, 0); 519 } 520 } 521 EXPORT_SYMBOL_NS_GPL(serial8250_clear_fifos, "SERIAL_8250"); 522 523 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t); 524 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t); 525 526 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p) 527 { 528 serial8250_clear_fifos(p); 529 serial_out(p, UART_FCR, p->fcr); 530 } 531 EXPORT_SYMBOL_GPL(serial8250_clear_and_reinit_fifos); 532 533 void serial8250_rpm_get(struct uart_8250_port *p) 534 { 535 if (!(p->capabilities & UART_CAP_RPM)) 536 return; 537 pm_runtime_get_sync(p->port.dev); 538 } 539 EXPORT_SYMBOL_GPL(serial8250_rpm_get); 540 541 void serial8250_rpm_put(struct uart_8250_port *p) 542 { 543 if (!(p->capabilities & UART_CAP_RPM)) 544 return; 545 pm_runtime_put_autosuspend(p->port.dev); 546 } 547 EXPORT_SYMBOL_GPL(serial8250_rpm_put); 548 549 /** 550 * serial8250_em485_init() - put uart_8250_port into rs485 emulating 551 * @p: uart_8250_port port instance 552 * 553 * The function is used to start rs485 software emulating on the 554 * &struct uart_8250_port* @p. Namely, RTS is switched before/after 555 * transmission. The function is idempotent, so it is safe to call it 556 * multiple times. 557 * 558 * The caller MUST enable interrupt on empty shift register before 559 * calling serial8250_em485_init(). This interrupt is not a part of 560 * 8250 standard, but implementation defined. 561 * 562 * The function is supposed to be called from .rs485_config callback 563 * or from any other callback protected with p->port.lock spinlock. 564 * 565 * See also serial8250_em485_destroy() 566 * 567 * Return 0 - success, -errno - otherwise 568 */ 569 static int serial8250_em485_init(struct uart_8250_port *p) 570 { 571 /* Port locked to synchronize UART_IER access against the console. */ 572 lockdep_assert_held_once(&p->port.lock); 573 574 if (p->em485) 575 goto deassert_rts; 576 577 p->em485 = kmalloc_obj(struct uart_8250_em485, GFP_ATOMIC); 578 if (!p->em485) 579 return -ENOMEM; 580 581 hrtimer_setup(&p->em485->stop_tx_timer, &serial8250_em485_handle_stop_tx, CLOCK_MONOTONIC, 582 HRTIMER_MODE_REL); 583 hrtimer_setup(&p->em485->start_tx_timer, &serial8250_em485_handle_start_tx, CLOCK_MONOTONIC, 584 HRTIMER_MODE_REL); 585 p->em485->port = p; 586 p->em485->active_timer = NULL; 587 p->em485->tx_stopped = true; 588 589 deassert_rts: 590 if (p->em485->tx_stopped) 591 p->rs485_stop_tx(p, true); 592 593 return 0; 594 } 595 596 /** 597 * serial8250_em485_destroy() - put uart_8250_port into normal state 598 * @p: uart_8250_port port instance 599 * 600 * The function is used to stop rs485 software emulating on the 601 * &struct uart_8250_port* @p. The function is idempotent, so it is safe to 602 * call it multiple times. 603 * 604 * The function is supposed to be called from .rs485_config callback 605 * or from any other callback protected with p->port.lock spinlock. 606 * 607 * See also serial8250_em485_init() 608 */ 609 void serial8250_em485_destroy(struct uart_8250_port *p) 610 { 611 if (!p->em485) 612 return; 613 614 hrtimer_cancel(&p->em485->start_tx_timer); 615 hrtimer_cancel(&p->em485->stop_tx_timer); 616 617 kfree(p->em485); 618 p->em485 = NULL; 619 } 620 EXPORT_SYMBOL_GPL(serial8250_em485_destroy); 621 622 struct serial_rs485 serial8250_em485_supported = { 623 .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND | 624 SER_RS485_TERMINATE_BUS | SER_RS485_RX_DURING_TX, 625 .delay_rts_before_send = 1, 626 .delay_rts_after_send = 1, 627 }; 628 EXPORT_SYMBOL_GPL(serial8250_em485_supported); 629 630 /** 631 * serial8250_em485_config() - generic ->rs485_config() callback 632 * @port: uart port 633 * @termios: termios structure 634 * @rs485: rs485 settings 635 * 636 * Generic callback usable by 8250 uart drivers to activate rs485 settings 637 * if the uart is incapable of driving RTS as a Transmit Enable signal in 638 * hardware, relying on software emulation instead. 639 */ 640 int serial8250_em485_config(struct uart_port *port, struct ktermios *termios, 641 struct serial_rs485 *rs485) 642 { 643 struct uart_8250_port *up = up_to_u8250p(port); 644 645 /* 646 * Both serial8250_em485_init() and serial8250_em485_destroy() 647 * are idempotent. 648 */ 649 if (rs485->flags & SER_RS485_ENABLED) 650 return serial8250_em485_init(up); 651 652 serial8250_em485_destroy(up); 653 return 0; 654 } 655 EXPORT_SYMBOL_GPL(serial8250_em485_config); 656 657 /* 658 * These two wrappers ensure that enable_runtime_pm_tx() can be called more than 659 * once and disable_runtime_pm_tx() will still disable RPM because the fifo is 660 * empty and the HW can idle again. 661 */ 662 static void serial8250_rpm_get_tx(struct uart_8250_port *p) 663 { 664 unsigned char rpm_active; 665 666 if (!(p->capabilities & UART_CAP_RPM)) 667 return; 668 669 rpm_active = xchg(&p->rpm_tx_active, 1); 670 if (rpm_active) 671 return; 672 pm_runtime_get_sync(p->port.dev); 673 } 674 675 static void serial8250_rpm_put_tx(struct uart_8250_port *p) 676 { 677 unsigned char rpm_active; 678 679 if (!(p->capabilities & UART_CAP_RPM)) 680 return; 681 682 rpm_active = xchg(&p->rpm_tx_active, 0); 683 if (!rpm_active) 684 return; 685 pm_runtime_put_autosuspend(p->port.dev); 686 } 687 688 /* 689 * IER sleep support. UARTs which have EFRs need the "extended 690 * capability" bit enabled. Note that on XR16C850s, we need to 691 * reset LCR to write to IER. 692 */ 693 static void serial8250_set_sleep(struct uart_8250_port *p, int sleep) 694 { 695 unsigned char lcr = 0, efr = 0; 696 697 guard(serial8250_rpm)(p); 698 699 if (!(p->capabilities & UART_CAP_SLEEP)) 700 return; 701 702 /* Synchronize UART_IER access against the console. */ 703 guard(uart_port_lock_irq)(&p->port); 704 705 if (p->capabilities & UART_CAP_EFR) { 706 lcr = serial_in(p, UART_LCR); 707 efr = serial_in(p, UART_EFR); 708 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B); 709 serial_out(p, UART_EFR, UART_EFR_ECB); 710 serial_out(p, UART_LCR, 0); 711 } 712 serial_out(p, UART_IER, sleep ? UART_IERX_SLEEP : 0); 713 if (p->capabilities & UART_CAP_EFR) { 714 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_B); 715 serial_out(p, UART_EFR, efr); 716 serial_out(p, UART_LCR, lcr); 717 } 718 } 719 720 /* Clear the interrupt registers. */ 721 static void serial8250_clear_interrupts(struct uart_port *port) 722 { 723 serial_port_in(port, UART_LSR); 724 serial_port_in(port, UART_RX); 725 serial_port_in(port, UART_IIR); 726 serial_port_in(port, UART_MSR); 727 } 728 729 /* 730 * Only to be directly used by serial8250_console_write() and 731 * serial8250_put_poll_char(), which do not require the port lock. 732 * Use serial8250_clear_IER() instead for all other cases. 733 */ 734 static void __serial8250_clear_IER(struct uart_8250_port *up) 735 { 736 if (up->capabilities & UART_CAP_UUE) 737 serial_out(up, UART_IER, UART_IER_UUE); 738 else 739 serial_out(up, UART_IER, 0); 740 } 741 742 static inline void serial8250_clear_IER(struct uart_8250_port *up) 743 { 744 /* Port locked to synchronize UART_IER access against the console */ 745 lockdep_assert_held_once(&up->port.lock); 746 747 __serial8250_clear_IER(up); 748 } 749 750 /* 751 * This is a quickie test to see how big the FIFO is. 752 * It doesn't work at all the time, more's the pity. 753 */ 754 static int size_fifo(struct uart_8250_port *up) 755 { 756 unsigned char old_fcr, old_mcr, old_lcr; 757 u32 old_dl; 758 int count; 759 760 old_lcr = serial_in(up, UART_LCR); 761 serial_out(up, UART_LCR, 0); 762 old_fcr = serial_in(up, UART_FCR); 763 old_mcr = serial8250_in_MCR(up); 764 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | 765 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT); 766 serial8250_out_MCR(up, UART_MCR_LOOP); 767 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A); 768 old_dl = serial_dl_read(up); 769 serial_dl_write(up, 0x0001); 770 serial_out(up, UART_LCR, UART_LCR_WLEN8); 771 for (count = 0; count < 256; count++) 772 serial_out(up, UART_TX, count); 773 mdelay(20);/* FIXME - schedule_timeout */ 774 for (count = 0; (serial_in(up, UART_LSR) & UART_LSR_DR) && 775 (count < 256); count++) 776 serial_in(up, UART_RX); 777 serial_out(up, UART_FCR, old_fcr); 778 serial8250_out_MCR(up, old_mcr); 779 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A); 780 serial_dl_write(up, old_dl); 781 serial_out(up, UART_LCR, old_lcr); 782 783 return count; 784 } 785 786 /* 787 * Read UART ID using the divisor method - set DLL and DLM to zero 788 * and the revision will be in DLL and device type in DLM. We 789 * preserve the device state across this. 790 */ 791 static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p) 792 { 793 unsigned char old_lcr; 794 unsigned int id, old_dl; 795 796 old_lcr = serial_in(p, UART_LCR); 797 serial_out(p, UART_LCR, UART_LCR_CONF_MODE_A); 798 old_dl = serial_dl_read(p); 799 serial_dl_write(p, 0); 800 id = serial_dl_read(p); 801 serial_dl_write(p, old_dl); 802 803 serial_out(p, UART_LCR, old_lcr); 804 805 return id; 806 } 807 808 /* 809 * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's. 810 * When this function is called we know it is at least a StarTech 811 * 16650 V2, but it might be one of several StarTech UARTs, or one of 812 * its clones. (We treat the broken original StarTech 16650 V1 as a 813 * 16550, and why not? Startech doesn't seem to even acknowledge its 814 * existence.) 815 * 816 * What evil have men's minds wrought... 817 */ 818 static void autoconfig_has_efr(struct uart_8250_port *up) 819 { 820 unsigned int id1, id2, id3, rev; 821 822 /* 823 * Everything with an EFR has SLEEP 824 */ 825 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP; 826 827 /* 828 * First we check to see if it's an Oxford Semiconductor UART. 829 * 830 * If we have to do this here because some non-National 831 * Semiconductor clone chips lock up if you try writing to the 832 * LSR register (which serial_icr_read does) 833 */ 834 835 /* 836 * Check for Oxford Semiconductor 16C950. 837 * 838 * EFR [4] must be set else this test fails. 839 * 840 * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca) 841 * claims that it's needed for 952 dual UART's (which are not 842 * recommended for new designs). 843 */ 844 up->acr = 0; 845 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); 846 serial_out(up, UART_EFR, UART_EFR_ECB); 847 serial_out(up, UART_LCR, 0x00); 848 id1 = serial_icr_read(up, UART_ID1); 849 id2 = serial_icr_read(up, UART_ID2); 850 id3 = serial_icr_read(up, UART_ID3); 851 rev = serial_icr_read(up, UART_REV); 852 853 if (id1 == 0x16 && id2 == 0xC9 && 854 (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) { 855 up->port.type = PORT_16C950; 856 857 /* 858 * Enable work around for the Oxford Semiconductor 952 rev B 859 * chip which causes it to seriously miscalculate baud rates 860 * when DLL is 0. 861 */ 862 if (id3 == 0x52 && rev == 0x01) 863 up->bugs |= UART_BUG_QUOT; 864 return; 865 } 866 867 /* 868 * We check for a XR16C850 by setting DLL and DLM to 0, and then 869 * reading back DLL and DLM. The chip type depends on the DLM 870 * value read back: 871 * 0x10 - XR16C850 and the DLL contains the chip revision. 872 * 0x12 - XR16C2850. 873 * 0x14 - XR16C854. 874 */ 875 id1 = autoconfig_read_divisor_id(up); 876 877 id2 = id1 >> 8; 878 if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) { 879 up->port.type = PORT_16850; 880 return; 881 } 882 883 /* 884 * It wasn't an XR16C850. 885 * 886 * We distinguish between the '654 and the '650 by counting 887 * how many bytes are in the FIFO. I'm using this for now, 888 * since that's the technique that was sent to me in the 889 * serial driver update, but I'm not convinced this works. 890 * I've had problems doing this in the past. -TYT 891 */ 892 if (size_fifo(up) == 64) 893 up->port.type = PORT_16654; 894 else 895 up->port.type = PORT_16650V2; 896 } 897 898 /* 899 * We detected a chip without a FIFO. Only two fall into 900 * this category - the original 8250 and the 16450. The 901 * 16450 has a scratch register (accessible with LCR=0) 902 */ 903 static void autoconfig_8250(struct uart_8250_port *up) 904 { 905 unsigned char scratch, status1, status2; 906 907 up->port.type = PORT_8250; 908 909 scratch = serial_in(up, UART_SCR); 910 serial_out(up, UART_SCR, 0xa5); 911 status1 = serial_in(up, UART_SCR); 912 serial_out(up, UART_SCR, 0x5a); 913 status2 = serial_in(up, UART_SCR); 914 serial_out(up, UART_SCR, scratch); 915 916 if (status1 == 0xa5 && status2 == 0x5a) 917 up->port.type = PORT_16450; 918 } 919 920 static int broken_efr(struct uart_8250_port *up) 921 { 922 /* 923 * Exar ST16C2550 "A2" devices incorrectly detect as 924 * having an EFR, and report an ID of 0x0201. See 925 * http://linux.derkeiler.com/Mailing-Lists/Kernel/2004-11/4812.html 926 */ 927 if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16) 928 return 1; 929 930 return 0; 931 } 932 933 /* 934 * We know that the chip has FIFOs. Does it have an EFR? The 935 * EFR is located in the same register position as the IIR and 936 * we know the top two bits of the IIR are currently set. The 937 * EFR should contain zero. Try to read the EFR. 938 */ 939 static void autoconfig_16550a(struct uart_8250_port *up) 940 { 941 unsigned char status1, status2; 942 unsigned int iersave; 943 944 /* Port locked to synchronize UART_IER access against the console. */ 945 lockdep_assert_held_once(&up->port.lock); 946 947 up->port.type = PORT_16550A; 948 up->capabilities |= UART_CAP_FIFO; 949 950 if (!IS_ENABLED(CONFIG_SERIAL_8250_16550A_VARIANTS) && 951 !(up->port.flags & UPF_FULL_PROBE)) 952 return; 953 954 /* 955 * Check for presence of the EFR when DLAB is set. 956 * Only ST16C650V1 UARTs pass this test. 957 */ 958 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A); 959 if (serial_in(up, UART_EFR) == 0) { 960 serial_out(up, UART_EFR, 0xA8); 961 if (serial_in(up, UART_EFR) != 0) { 962 up->port.type = PORT_16650; 963 up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP; 964 } else { 965 serial_out(up, UART_LCR, 0); 966 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | 967 UART_FCR7_64BYTE); 968 status1 = serial_in(up, UART_IIR) & UART_IIR_FIFO_ENABLED_16750; 969 serial_out(up, UART_FCR, 0); 970 serial_out(up, UART_LCR, 0); 971 972 if (status1 == UART_IIR_FIFO_ENABLED_16750) 973 up->port.type = PORT_16550A_FSL64; 974 } 975 serial_out(up, UART_EFR, 0); 976 return; 977 } 978 979 /* 980 * Maybe it requires 0xbf to be written to the LCR. 981 * (other ST16C650V2 UARTs, TI16C752A, etc) 982 */ 983 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); 984 if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) { 985 autoconfig_has_efr(up); 986 return; 987 } 988 989 /* 990 * Check for a National Semiconductor SuperIO chip. 991 * Attempt to switch to bank 2, read the value of the LOOP bit 992 * from EXCR1. Switch back to bank 0, change it in MCR. Then 993 * switch back to bank 2, read it from EXCR1 again and check 994 * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2 995 */ 996 serial_out(up, UART_LCR, 0); 997 status1 = serial8250_in_MCR(up); 998 serial_out(up, UART_LCR, 0xE0); 999 status2 = serial_in(up, 0x02); /* EXCR1 */ 1000 1001 if (!((status2 ^ status1) & UART_MCR_LOOP)) { 1002 serial_out(up, UART_LCR, 0); 1003 serial8250_out_MCR(up, status1 ^ UART_MCR_LOOP); 1004 serial_out(up, UART_LCR, 0xE0); 1005 status2 = serial_in(up, 0x02); /* EXCR1 */ 1006 serial_out(up, UART_LCR, 0); 1007 serial8250_out_MCR(up, status1); 1008 1009 if ((status2 ^ status1) & UART_MCR_LOOP) { 1010 unsigned short quot; 1011 1012 serial_out(up, UART_LCR, 0xE0); 1013 1014 quot = serial_dl_read(up); 1015 quot <<= 3; 1016 1017 if (ns16550a_goto_highspeed(up)) 1018 serial_dl_write(up, quot); 1019 1020 serial_out(up, UART_LCR, 0); 1021 1022 up->port.uartclk = 921600*16; 1023 up->port.type = PORT_NS16550A; 1024 up->capabilities |= UART_NATSEMI; 1025 return; 1026 } 1027 } 1028 1029 /* 1030 * No EFR. Try to detect a TI16750, which only sets bit 5 of 1031 * the IIR when 64 byte FIFO mode is enabled when DLAB is set. 1032 * Try setting it with and without DLAB set. Cheap clones 1033 * set bit 5 without DLAB set. 1034 */ 1035 serial_out(up, UART_LCR, 0); 1036 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE); 1037 status1 = serial_in(up, UART_IIR) & UART_IIR_FIFO_ENABLED_16750; 1038 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO); 1039 1040 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A); 1041 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE); 1042 status2 = serial_in(up, UART_IIR) & UART_IIR_FIFO_ENABLED_16750; 1043 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO); 1044 1045 serial_out(up, UART_LCR, 0); 1046 1047 if (status1 == UART_IIR_FIFO_ENABLED_16550A && 1048 status2 == UART_IIR_FIFO_ENABLED_16750) { 1049 up->port.type = PORT_16750; 1050 up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP; 1051 return; 1052 } 1053 1054 /* 1055 * Try writing and reading the UART_IER_UUE bit (b6). 1056 * If it works, this is probably one of the Xscale platform's 1057 * internal UARTs. 1058 * We're going to explicitly set the UUE bit to 0 before 1059 * trying to write and read a 1 just to make sure it's not 1060 * already a 1 and maybe locked there before we even start. 1061 */ 1062 iersave = serial_in(up, UART_IER); 1063 serial_out(up, UART_IER, iersave & ~UART_IER_UUE); 1064 if (!(serial_in(up, UART_IER) & UART_IER_UUE)) { 1065 /* 1066 * OK it's in a known zero state, try writing and reading 1067 * without disturbing the current state of the other bits. 1068 */ 1069 serial_out(up, UART_IER, iersave | UART_IER_UUE); 1070 if (serial_in(up, UART_IER) & UART_IER_UUE) { 1071 /* 1072 * It's an Xscale. 1073 * We'll leave the UART_IER_UUE bit set to 1 (enabled). 1074 */ 1075 up->port.type = PORT_XSCALE; 1076 up->capabilities |= UART_CAP_UUE | UART_CAP_RTOIE; 1077 return; 1078 } 1079 } 1080 serial_out(up, UART_IER, iersave); 1081 1082 /* 1083 * We distinguish between 16550A and U6 16550A by counting 1084 * how many bytes are in the FIFO. 1085 */ 1086 if (up->port.type == PORT_16550A && size_fifo(up) == 64) { 1087 up->port.type = PORT_U6_16550A; 1088 up->capabilities |= UART_CAP_AFE; 1089 } 1090 } 1091 1092 /* 1093 * This routine is called by rs_init() to initialize a specific serial 1094 * port. It determines what type of UART chip this serial port is 1095 * using: 8250, 16450, 16550, 16550A. The important question is 1096 * whether or not this UART is a 16550A or not, since this will 1097 * determine whether or not we can use its FIFO features or not. 1098 */ 1099 static void autoconfig(struct uart_8250_port *up) 1100 { 1101 unsigned char status1, scratch, scratch2, scratch3; 1102 unsigned char save_lcr, save_mcr; 1103 struct uart_port *port = &up->port; 1104 unsigned long flags; 1105 unsigned int old_capabilities; 1106 1107 if (!port->iobase && !port->mapbase && !port->membase) 1108 return; 1109 1110 /* 1111 * We really do need global IRQs disabled here - we're going to 1112 * be frobbing the chips IRQ enable register to see if it exists. 1113 * 1114 * Synchronize UART_IER access against the console. 1115 */ 1116 uart_port_lock_irqsave(port, &flags); 1117 1118 up->capabilities = 0; 1119 up->bugs = 0; 1120 1121 if (!(port->flags & UPF_BUGGY_UART)) { 1122 /* 1123 * Do a simple existence test first; if we fail this, 1124 * there's no point trying anything else. 1125 * 1126 * 0x80 is used as a nonsense port to prevent against 1127 * false positives due to ISA bus float. The 1128 * assumption is that 0x80 is a non-existent port; 1129 * which should be safe since include/asm/io.h also 1130 * makes this assumption. 1131 * 1132 * Note: this is safe as long as MCR bit 4 is clear 1133 * and the device is in "PC" mode. 1134 */ 1135 scratch = serial_in(up, UART_IER); 1136 serial_out(up, UART_IER, 0); 1137 #if defined(__i386__) && defined(CONFIG_HAS_IOPORT) 1138 outb(0xff, 0x080); 1139 #endif 1140 /* 1141 * Mask out IER[7:4] bits for test as some UARTs (e.g. TL 1142 * 16C754B) allow only to modify them if an EFR bit is set. 1143 */ 1144 scratch2 = serial_in(up, UART_IER) & UART_IER_ALL_INTR; 1145 serial_out(up, UART_IER, UART_IER_ALL_INTR); 1146 #if defined(__i386__) && defined(CONFIG_HAS_IOPORT) 1147 outb(0, 0x080); 1148 #endif 1149 scratch3 = serial_in(up, UART_IER) & UART_IER_ALL_INTR; 1150 serial_out(up, UART_IER, scratch); 1151 if (scratch2 != 0 || scratch3 != UART_IER_ALL_INTR) { 1152 /* 1153 * We failed; there's nothing here 1154 */ 1155 uart_port_unlock_irqrestore(port, flags); 1156 return; 1157 } 1158 } 1159 1160 save_mcr = serial8250_in_MCR(up); 1161 save_lcr = serial_in(up, UART_LCR); 1162 1163 /* 1164 * Check to see if a UART is really there. Certain broken 1165 * internal modems based on the Rockwell chipset fail this 1166 * test, because they apparently don't implement the loopback 1167 * test mode. So this test is skipped on the COM 1 through 1168 * COM 4 ports. This *should* be safe, since no board 1169 * manufacturer would be stupid enough to design a board 1170 * that conflicts with COM 1-4 --- we hope! 1171 */ 1172 if (!(port->flags & UPF_SKIP_TEST)) { 1173 serial8250_out_MCR(up, UART_MCR_LOOP | UART_MCR_OUT2 | UART_MCR_RTS); 1174 status1 = serial_in(up, UART_MSR) & UART_MSR_STATUS_BITS; 1175 serial8250_out_MCR(up, save_mcr); 1176 if (status1 != (UART_MSR_DCD | UART_MSR_CTS)) { 1177 uart_port_unlock_irqrestore(port, flags); 1178 return; 1179 } 1180 } 1181 1182 /* 1183 * We're pretty sure there's a port here. Lets find out what 1184 * type of port it is. The IIR top two bits allows us to find 1185 * out if it's 8250 or 16450, 16550, 16550A or later. This 1186 * determines what we test for next. 1187 * 1188 * We also initialise the EFR (if any) to zero for later. The 1189 * EFR occupies the same register location as the FCR and IIR. 1190 */ 1191 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); 1192 serial_out(up, UART_EFR, 0); 1193 serial_out(up, UART_LCR, 0); 1194 1195 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO); 1196 1197 switch (serial_in(up, UART_IIR) & UART_IIR_FIFO_ENABLED) { 1198 case UART_IIR_FIFO_ENABLED_8250: 1199 autoconfig_8250(up); 1200 break; 1201 case UART_IIR_FIFO_ENABLED_16550: 1202 port->type = PORT_16550; 1203 break; 1204 case UART_IIR_FIFO_ENABLED_16550A: 1205 autoconfig_16550a(up); 1206 break; 1207 default: 1208 port->type = PORT_UNKNOWN; 1209 break; 1210 } 1211 1212 rsa_autoconfig(up); 1213 1214 serial_out(up, UART_LCR, save_lcr); 1215 1216 port->fifosize = uart_config[up->port.type].fifo_size; 1217 old_capabilities = up->capabilities; 1218 up->capabilities = uart_config[port->type].flags; 1219 up->tx_loadsz = uart_config[port->type].tx_loadsz; 1220 1221 if (port->type != PORT_UNKNOWN) { 1222 /* 1223 * Reset the UART. 1224 */ 1225 rsa_reset(up); 1226 serial8250_out_MCR(up, save_mcr); 1227 serial8250_clear_fifos(up); 1228 serial_in(up, UART_RX); 1229 serial8250_clear_IER(up); 1230 } 1231 1232 uart_port_unlock_irqrestore(port, flags); 1233 1234 /* 1235 * Check if the device is a Fintek F81216A 1236 */ 1237 if (port->type == PORT_16550A && port->iotype == UPIO_PORT) 1238 fintek_8250_probe(up); 1239 1240 if (up->capabilities != old_capabilities) { 1241 dev_warn(port->dev, "detected caps %08x should be %08x\n", 1242 old_capabilities, up->capabilities); 1243 } 1244 } 1245 1246 static void autoconfig_irq(struct uart_8250_port *up) 1247 { 1248 struct uart_port *port = &up->port; 1249 unsigned char save_mcr, save_ier; 1250 unsigned char save_ICP = 0; 1251 unsigned int ICP = 0; 1252 unsigned long irqs; 1253 int irq; 1254 1255 if (port->flags & UPF_FOURPORT) { 1256 ICP = (port->iobase & 0xfe0) | 0x1f; 1257 save_ICP = inb_p(ICP); 1258 outb_p(0x80, ICP); 1259 inb_p(ICP); 1260 } 1261 1262 /* forget possible initially masked and pending IRQ */ 1263 probe_irq_off(probe_irq_on()); 1264 save_mcr = serial8250_in_MCR(up); 1265 /* Synchronize UART_IER access against the console. */ 1266 scoped_guard(uart_port_lock_irq, port) 1267 save_ier = serial_in(up, UART_IER); 1268 serial8250_out_MCR(up, UART_MCR_OUT1 | UART_MCR_OUT2); 1269 1270 irqs = probe_irq_on(); 1271 serial8250_out_MCR(up, 0); 1272 udelay(10); 1273 if (port->flags & UPF_FOURPORT) { 1274 serial8250_out_MCR(up, UART_MCR_DTR | UART_MCR_RTS); 1275 } else { 1276 serial8250_out_MCR(up, 1277 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2); 1278 } 1279 /* Synchronize UART_IER access against the console. */ 1280 scoped_guard(uart_port_lock_irq, port) 1281 serial_out(up, UART_IER, UART_IER_ALL_INTR); 1282 serial8250_clear_interrupts(port); 1283 serial_out(up, UART_TX, 0xFF); 1284 udelay(20); 1285 irq = probe_irq_off(irqs); 1286 1287 serial8250_out_MCR(up, save_mcr); 1288 /* Synchronize UART_IER access against the console. */ 1289 scoped_guard(uart_port_lock_irq, port) 1290 serial_out(up, UART_IER, save_ier); 1291 1292 if (port->flags & UPF_FOURPORT) 1293 outb_p(save_ICP, ICP); 1294 1295 port->irq = (irq > 0) ? irq : 0; 1296 } 1297 1298 static void serial8250_stop_rx(struct uart_port *port) 1299 { 1300 struct uart_8250_port *up = up_to_u8250p(port); 1301 1302 /* Port locked to synchronize UART_IER access against the console. */ 1303 lockdep_assert_held_once(&port->lock); 1304 1305 guard(serial8250_rpm)(up); 1306 1307 up->ier &= ~(UART_IER_RLSI | UART_IER_RDI); 1308 serial_port_out(port, UART_IER, up->ier); 1309 } 1310 1311 /** 1312 * serial8250_em485_stop_tx() - generic ->rs485_stop_tx() callback 1313 * @p: uart 8250 port 1314 * @toggle_ier: true to allow enabling receive interrupts 1315 * 1316 * Generic callback usable by 8250 uart drivers to stop rs485 transmission. 1317 */ 1318 void serial8250_em485_stop_tx(struct uart_8250_port *p, bool toggle_ier) 1319 { 1320 unsigned char mcr = serial8250_in_MCR(p); 1321 1322 if (p->port.rs485.flags & SER_RS485_RTS_AFTER_SEND) 1323 mcr |= UART_MCR_RTS; 1324 else 1325 mcr &= ~UART_MCR_RTS; 1326 serial8250_out_MCR(p, mcr); 1327 1328 /* 1329 * Empty the RX FIFO, we are not interested in anything 1330 * received during the half-duplex transmission. 1331 * Enable previously disabled RX interrupts. 1332 */ 1333 if (!(p->port.rs485.flags & SER_RS485_RX_DURING_TX)) { 1334 serial8250_clear_and_reinit_fifos(p); 1335 1336 if (toggle_ier) { 1337 /* 1338 * Port locked to synchronize UART_IER access against 1339 * the console. The lockdep_assert must be restricted 1340 * to this condition because only here is it 1341 * guaranteed that the port lock is held. The other 1342 * hardware access in this function is synchronized 1343 * by console ownership. 1344 */ 1345 lockdep_assert_held_once(&p->port.lock); 1346 1347 p->ier |= UART_IER_RLSI | UART_IER_RDI; 1348 serial_port_out(&p->port, UART_IER, p->ier); 1349 } 1350 } 1351 } 1352 EXPORT_SYMBOL_GPL(serial8250_em485_stop_tx); 1353 1354 static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t) 1355 { 1356 struct uart_8250_em485 *em485 = container_of(t, struct uart_8250_em485, 1357 stop_tx_timer); 1358 struct uart_8250_port *p = em485->port; 1359 1360 guard(serial8250_rpm)(p); 1361 guard(uart_port_lock_irqsave)(&p->port); 1362 1363 if (em485->active_timer == &em485->stop_tx_timer) { 1364 p->rs485_stop_tx(p, true); 1365 em485->active_timer = NULL; 1366 em485->tx_stopped = true; 1367 } 1368 1369 return HRTIMER_NORESTART; 1370 } 1371 1372 static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec) 1373 { 1374 hrtimer_start(hrt, ms_to_ktime(msec), HRTIMER_MODE_REL); 1375 } 1376 1377 static void __stop_tx_rs485(struct uart_8250_port *p, u64 stop_delay) 1378 { 1379 struct uart_8250_em485 *em485 = p->em485; 1380 1381 /* Port locked to synchronize UART_IER access against the console. */ 1382 lockdep_assert_held_once(&p->port.lock); 1383 1384 stop_delay += (u64)p->port.rs485.delay_rts_after_send * NSEC_PER_MSEC; 1385 1386 /* 1387 * rs485_stop_tx() is going to set RTS according to config 1388 * AND flush RX FIFO if required. 1389 */ 1390 if (stop_delay > 0) { 1391 em485->active_timer = &em485->stop_tx_timer; 1392 hrtimer_start(&em485->stop_tx_timer, ns_to_ktime(stop_delay), HRTIMER_MODE_REL); 1393 } else { 1394 p->rs485_stop_tx(p, true); 1395 em485->active_timer = NULL; 1396 em485->tx_stopped = true; 1397 } 1398 } 1399 1400 static inline void __stop_tx(struct uart_8250_port *p) 1401 { 1402 struct uart_8250_em485 *em485 = p->em485; 1403 1404 if (em485) { 1405 u16 lsr = serial_lsr_in(p); 1406 u64 stop_delay = 0; 1407 1408 if (!(lsr & UART_LSR_THRE)) 1409 return; 1410 /* 1411 * To provide required timing and allow FIFO transfer, 1412 * __stop_tx_rs485() must be called only when both FIFO and 1413 * shift register are empty. The device driver should either 1414 * enable interrupt on TEMT or set UART_CAP_NOTEMT that will 1415 * enlarge stop_tx_timer by the tx time of one frame to cover 1416 * for emptying of the shift register. 1417 */ 1418 if (!(lsr & UART_LSR_TEMT)) { 1419 if (!(p->capabilities & UART_CAP_NOTEMT)) 1420 return; 1421 /* 1422 * RTS might get deasserted too early with the normal 1423 * frame timing formula. It seems to suggest THRE might 1424 * get asserted already during tx of the stop bit 1425 * rather than after it is fully sent. 1426 * Roughly estimate 1 extra bit here with / 7. 1427 */ 1428 stop_delay = p->port.frame_time + DIV_ROUND_UP(p->port.frame_time, 7); 1429 } 1430 1431 __stop_tx_rs485(p, stop_delay); 1432 } 1433 1434 if (serial8250_clear_THRI(p)) 1435 serial8250_rpm_put_tx(p); 1436 } 1437 1438 static void serial8250_stop_tx(struct uart_port *port) 1439 { 1440 struct uart_8250_port *up = up_to_u8250p(port); 1441 1442 guard(serial8250_rpm)(up); 1443 __stop_tx(up); 1444 1445 /* 1446 * We really want to stop the transmitter from sending. 1447 */ 1448 if (port->type == PORT_16C950) { 1449 up->acr |= UART_ACR_TXDIS; 1450 serial_icr_write(up, UART_ACR, up->acr); 1451 } 1452 } 1453 1454 static inline void __start_tx(struct uart_port *port) 1455 { 1456 struct uart_8250_port *up = up_to_u8250p(port); 1457 1458 if (up->dma && !up->dma->tx_dma(up)) 1459 return; 1460 1461 if (serial8250_set_THRI(up)) { 1462 if (up->bugs & UART_BUG_TXEN) { 1463 u16 lsr = serial_lsr_in(up); 1464 1465 if (lsr & UART_LSR_THRE) 1466 serial8250_tx_chars(up); 1467 } 1468 } 1469 1470 /* 1471 * Re-enable the transmitter if we disabled it. 1472 */ 1473 if (port->type == PORT_16C950 && up->acr & UART_ACR_TXDIS) { 1474 up->acr &= ~UART_ACR_TXDIS; 1475 serial_icr_write(up, UART_ACR, up->acr); 1476 } 1477 } 1478 1479 /** 1480 * serial8250_em485_start_tx() - generic ->rs485_start_tx() callback 1481 * @up: uart 8250 port 1482 * @toggle_ier: true to allow disabling receive interrupts 1483 * 1484 * Generic callback usable by 8250 uart drivers to start rs485 transmission. 1485 * Assumes that setting the RTS bit in the MCR register means RTS is high. 1486 * (Some chips use inverse semantics.) Further assumes that reception is 1487 * stoppable by disabling the UART_IER_RDI interrupt. (Some chips set the 1488 * UART_LSR_DR bit even when UART_IER_RDI is disabled, foiling this approach.) 1489 */ 1490 void serial8250_em485_start_tx(struct uart_8250_port *up, bool toggle_ier) 1491 { 1492 unsigned char mcr = serial8250_in_MCR(up); 1493 1494 if (!(up->port.rs485.flags & SER_RS485_RX_DURING_TX) && toggle_ier) 1495 serial8250_stop_rx(&up->port); 1496 1497 if (up->port.rs485.flags & SER_RS485_RTS_ON_SEND) 1498 mcr |= UART_MCR_RTS; 1499 else 1500 mcr &= ~UART_MCR_RTS; 1501 serial8250_out_MCR(up, mcr); 1502 } 1503 EXPORT_SYMBOL_GPL(serial8250_em485_start_tx); 1504 1505 /* Returns false, if start_tx_timer was setup to defer TX start */ 1506 static bool start_tx_rs485(struct uart_port *port) 1507 { 1508 struct uart_8250_port *up = up_to_u8250p(port); 1509 struct uart_8250_em485 *em485 = up->em485; 1510 1511 /* 1512 * While serial8250_em485_handle_stop_tx() is a noop if 1513 * em485->active_timer != &em485->stop_tx_timer, it might happen that 1514 * the timer is still armed and triggers only after the current bunch of 1515 * chars is send and em485->active_timer == &em485->stop_tx_timer again. 1516 * So cancel the timer. There is still a theoretical race condition if 1517 * the timer is already running and only comes around to check for 1518 * em485->active_timer when &em485->stop_tx_timer is armed again. 1519 */ 1520 if (em485->active_timer == &em485->stop_tx_timer) 1521 hrtimer_try_to_cancel(&em485->stop_tx_timer); 1522 1523 em485->active_timer = NULL; 1524 1525 if (em485->tx_stopped) { 1526 em485->tx_stopped = false; 1527 1528 up->rs485_start_tx(up, true); 1529 1530 if (up->port.rs485.delay_rts_before_send > 0) { 1531 em485->active_timer = &em485->start_tx_timer; 1532 start_hrtimer_ms(&em485->start_tx_timer, 1533 up->port.rs485.delay_rts_before_send); 1534 return false; 1535 } 1536 } 1537 1538 return true; 1539 } 1540 1541 static enum hrtimer_restart serial8250_em485_handle_start_tx(struct hrtimer *t) 1542 { 1543 struct uart_8250_em485 *em485 = container_of(t, struct uart_8250_em485, 1544 start_tx_timer); 1545 struct uart_8250_port *p = em485->port; 1546 1547 guard(uart_port_lock_irqsave)(&p->port); 1548 1549 if (em485->active_timer == &em485->start_tx_timer) { 1550 __start_tx(&p->port); 1551 em485->active_timer = NULL; 1552 } 1553 1554 return HRTIMER_NORESTART; 1555 } 1556 1557 static void serial8250_start_tx(struct uart_port *port) 1558 { 1559 struct uart_8250_port *up = up_to_u8250p(port); 1560 struct uart_8250_em485 *em485 = up->em485; 1561 1562 /* Port locked to synchronize UART_IER access against the console. */ 1563 lockdep_assert_held_once(&port->lock); 1564 1565 if (!port->x_char && kfifo_is_empty(&port->state->port.xmit_fifo)) 1566 return; 1567 1568 serial8250_rpm_get_tx(up); 1569 1570 if (em485) { 1571 if ((em485->active_timer == &em485->start_tx_timer) || 1572 !start_tx_rs485(port)) 1573 return; 1574 } 1575 __start_tx(port); 1576 } 1577 1578 static void serial8250_throttle(struct uart_port *port) 1579 { 1580 port->throttle(port); 1581 } 1582 1583 static void serial8250_unthrottle(struct uart_port *port) 1584 { 1585 port->unthrottle(port); 1586 } 1587 1588 static void serial8250_disable_ms(struct uart_port *port) 1589 { 1590 struct uart_8250_port *up = up_to_u8250p(port); 1591 1592 /* Port locked to synchronize UART_IER access against the console. */ 1593 lockdep_assert_held_once(&port->lock); 1594 1595 /* no MSR capabilities */ 1596 if (up->bugs & UART_BUG_NOMSR) 1597 return; 1598 1599 mctrl_gpio_disable_ms_no_sync(up->gpios); 1600 1601 up->ier &= ~UART_IER_MSI; 1602 serial_port_out(port, UART_IER, up->ier); 1603 } 1604 1605 static void serial8250_enable_ms(struct uart_port *port) 1606 { 1607 struct uart_8250_port *up = up_to_u8250p(port); 1608 1609 /* Port locked to synchronize UART_IER access against the console. */ 1610 lockdep_assert_held_once(&port->lock); 1611 1612 /* no MSR capabilities */ 1613 if (up->bugs & UART_BUG_NOMSR) 1614 return; 1615 1616 mctrl_gpio_enable_ms(up->gpios); 1617 1618 up->ier |= UART_IER_MSI; 1619 1620 guard(serial8250_rpm)(up); 1621 serial_port_out(port, UART_IER, up->ier); 1622 } 1623 1624 void serial8250_read_char(struct uart_8250_port *up, u16 lsr) 1625 { 1626 struct uart_port *port = &up->port; 1627 u8 ch, flag = TTY_NORMAL; 1628 1629 if (likely(lsr & UART_LSR_DR)) 1630 ch = serial_in(up, UART_RX); 1631 else 1632 /* 1633 * Intel 82571 has a Serial Over Lan device that will 1634 * set UART_LSR_BI without setting UART_LSR_DR when 1635 * it receives a break. To avoid reading from the 1636 * receive buffer without UART_LSR_DR bit set, we 1637 * just force the read character to be 0 1638 */ 1639 ch = 0; 1640 1641 port->icount.rx++; 1642 1643 lsr |= up->lsr_saved_flags; 1644 up->lsr_saved_flags = 0; 1645 1646 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) { 1647 if (lsr & UART_LSR_BI) { 1648 lsr &= ~(UART_LSR_FE | UART_LSR_PE); 1649 port->icount.brk++; 1650 /* 1651 * We do the SysRQ and SAK checking 1652 * here because otherwise the break 1653 * may get masked by ignore_status_mask 1654 * or read_status_mask. 1655 */ 1656 if (uart_handle_break(port)) 1657 return; 1658 } else if (lsr & UART_LSR_PE) 1659 port->icount.parity++; 1660 else if (lsr & UART_LSR_FE) 1661 port->icount.frame++; 1662 if (lsr & UART_LSR_OE) 1663 port->icount.overrun++; 1664 1665 /* 1666 * Mask off conditions which should be ignored. 1667 */ 1668 lsr &= port->read_status_mask; 1669 1670 if (lsr & UART_LSR_BI) { 1671 dev_dbg(port->dev, "handling break\n"); 1672 flag = TTY_BREAK; 1673 } else if (lsr & UART_LSR_PE) 1674 flag = TTY_PARITY; 1675 else if (lsr & UART_LSR_FE) 1676 flag = TTY_FRAME; 1677 } 1678 if (uart_prepare_sysrq_char(port, ch)) 1679 return; 1680 1681 uart_insert_char(port, lsr, UART_LSR_OE, ch, flag); 1682 } 1683 EXPORT_SYMBOL_GPL(serial8250_read_char); 1684 1685 /* 1686 * serial8250_rx_chars - Read characters. The first LSR value must be passed in. 1687 * 1688 * Returns LSR bits. The caller should rely only on non-Rx related LSR bits 1689 * (such as THRE) because the LSR value might come from an already consumed 1690 * character. 1691 */ 1692 u16 serial8250_rx_chars(struct uart_8250_port *up, u16 lsr) 1693 { 1694 struct uart_port *port = &up->port; 1695 int max_count = 256; 1696 1697 do { 1698 serial8250_read_char(up, lsr); 1699 if (--max_count == 0) 1700 break; 1701 lsr = serial_in(up, UART_LSR); 1702 } while (lsr & (UART_LSR_DR | UART_LSR_BI)); 1703 1704 tty_flip_buffer_push(&port->state->port); 1705 return lsr; 1706 } 1707 EXPORT_SYMBOL_GPL(serial8250_rx_chars); 1708 1709 void serial8250_tx_chars(struct uart_8250_port *up) 1710 { 1711 struct uart_port *port = &up->port; 1712 struct tty_port *tport = &port->state->port; 1713 int count; 1714 1715 if (port->x_char) { 1716 uart_xchar_out(port, UART_TX); 1717 return; 1718 } 1719 if (uart_tx_stopped(port)) { 1720 serial8250_stop_tx(port); 1721 return; 1722 } 1723 if (kfifo_is_empty(&tport->xmit_fifo)) { 1724 __stop_tx(up); 1725 return; 1726 } 1727 1728 count = up->tx_loadsz; 1729 do { 1730 unsigned char c; 1731 1732 if (!uart_fifo_get(port, &c)) 1733 break; 1734 1735 serial_out(up, UART_TX, c); 1736 if (up->bugs & UART_BUG_TXRACE) { 1737 /* 1738 * The Aspeed BMC virtual UARTs have a bug where data 1739 * may get stuck in the BMC's Tx FIFO from bursts of 1740 * writes on the APB interface. 1741 * 1742 * Delay back-to-back writes by a read cycle to avoid 1743 * stalling the VUART. Read a register that won't have 1744 * side-effects and discard the result. 1745 */ 1746 serial_in(up, UART_SCR); 1747 } 1748 1749 if ((up->capabilities & UART_CAP_HFIFO) && 1750 !uart_lsr_tx_empty(serial_in(up, UART_LSR))) 1751 break; 1752 /* The BCM2835 MINI UART THRE bit is really a not-full bit. */ 1753 if ((up->capabilities & UART_CAP_MINI) && 1754 !(serial_in(up, UART_LSR) & UART_LSR_THRE)) 1755 break; 1756 } while (--count > 0); 1757 1758 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS) 1759 uart_write_wakeup(port); 1760 1761 /* 1762 * With RPM enabled, we have to wait until the FIFO is empty before the 1763 * HW can go idle. So we get here once again with empty FIFO and disable 1764 * the interrupt and RPM in __stop_tx() 1765 */ 1766 if (kfifo_is_empty(&tport->xmit_fifo) && 1767 !(up->capabilities & UART_CAP_RPM)) 1768 __stop_tx(up); 1769 } 1770 EXPORT_SYMBOL_GPL(serial8250_tx_chars); 1771 1772 /* Caller holds uart port lock */ 1773 unsigned int serial8250_modem_status(struct uart_8250_port *up) 1774 { 1775 struct uart_port *port = &up->port; 1776 unsigned int status = serial_in(up, UART_MSR); 1777 1778 status |= up->msr_saved_flags; 1779 up->msr_saved_flags = 0; 1780 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI && 1781 port->state != NULL) { 1782 if (status & UART_MSR_TERI) 1783 port->icount.rng++; 1784 if (status & UART_MSR_DDSR) 1785 port->icount.dsr++; 1786 if (status & UART_MSR_DDCD) 1787 uart_handle_dcd_change(port, status & UART_MSR_DCD); 1788 if (status & UART_MSR_DCTS) 1789 uart_handle_cts_change(port, status & UART_MSR_CTS); 1790 1791 wake_up_interruptible(&port->state->port.delta_msr_wait); 1792 } 1793 1794 return status; 1795 } 1796 EXPORT_SYMBOL_GPL(serial8250_modem_status); 1797 1798 static bool handle_rx_dma(struct uart_8250_port *up, unsigned int iir) 1799 { 1800 switch (iir & 0x3f) { 1801 case UART_IIR_THRI: 1802 /* 1803 * Postpone DMA or not decision to IIR_RDI or IIR_RX_TIMEOUT 1804 * because it's impossible to do an informed decision about 1805 * that with IIR_THRI. 1806 * 1807 * This also fixes one known DMA Rx corruption issue where 1808 * DR is asserted but DMA Rx only gets a corrupted zero byte 1809 * (too early DR?). 1810 */ 1811 return false; 1812 case UART_IIR_RDI: 1813 if (!up->dma->rx_running) 1814 break; 1815 fallthrough; 1816 case UART_IIR_RLSI: 1817 case UART_IIR_RX_TIMEOUT: 1818 serial8250_rx_dma_flush(up); 1819 return true; 1820 } 1821 return up->dma->rx_dma(up); 1822 } 1823 1824 /* 1825 * Context: port's lock must be held by the caller. The caller must 1826 * release it via guard(uart_port_lock_check_sysrq_irqsave) or 1827 * uart_unlock_and_check_sysrq_irqrestore(), which captures SysRq 1828 * character on unlock. 1829 */ 1830 void serial8250_handle_irq_locked(struct uart_port *port, unsigned int iir) 1831 { 1832 struct uart_8250_port *up = up_to_u8250p(port); 1833 struct tty_port *tport = &port->state->port; 1834 bool skip_rx = false; 1835 u16 status; 1836 1837 lockdep_assert_held_once(&port->lock); 1838 1839 status = serial_lsr_in(up); 1840 1841 /* 1842 * Recover from no-data-ready and FIFO error condition to avoid getting 1843 * stuck in the ISR. 1844 */ 1845 if (!(status & UART_LSR_DR) && (status & UART_LSR_FIFOE)) 1846 serial8250_clear_and_reinit_fifos(up); 1847 1848 /* 1849 * If port is stopped and there are no error conditions in the 1850 * FIFO, then don't drain the FIFO, as this may lead to TTY buffer 1851 * overflow. Not servicing, RX FIFO would trigger auto HW flow 1852 * control when FIFO occupancy reaches preset threshold, thus 1853 * halting RX. This only works when auto HW flow control is 1854 * available. 1855 */ 1856 if (!(status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS)) && 1857 (port->status & (UPSTAT_AUTOCTS | UPSTAT_AUTORTS)) && 1858 !(up->ier & (UART_IER_RLSI | UART_IER_RDI))) 1859 skip_rx = true; 1860 1861 if (status & (UART_LSR_DR | UART_LSR_BI) && !skip_rx) { 1862 struct irq_data *d; 1863 1864 d = irq_get_irq_data(port->irq); 1865 if (d && irqd_is_wakeup_set(d)) 1866 pm_wakeup_event(tport->tty->dev, 0); 1867 if (!up->dma || handle_rx_dma(up, iir)) 1868 status = serial8250_rx_chars(up, status); 1869 } 1870 serial8250_modem_status(up); 1871 if ((status & UART_LSR_THRE) && (up->ier & UART_IER_THRI)) { 1872 if (!up->dma || up->dma->tx_err) 1873 serial8250_tx_chars(up); 1874 else if (!up->dma->tx_running) 1875 __stop_tx(up); 1876 } 1877 } 1878 EXPORT_SYMBOL_NS_GPL(serial8250_handle_irq_locked, "SERIAL_8250"); 1879 1880 /* 1881 * This handles the interrupt from one port. 1882 */ 1883 int serial8250_handle_irq(struct uart_port *port, unsigned int iir) 1884 { 1885 if (iir & UART_IIR_NO_INT) 1886 return 0; 1887 1888 guard(uart_port_lock_check_sysrq_irqsave)(port); 1889 serial8250_handle_irq_locked(port, iir); 1890 1891 return 1; 1892 } 1893 EXPORT_SYMBOL_GPL(serial8250_handle_irq); 1894 1895 static int serial8250_default_handle_irq(struct uart_port *port) 1896 { 1897 unsigned int iir; 1898 int pm_status; 1899 int ret; 1900 1901 /* if driver suspended, return, probably shared interrupt */ 1902 pm_status = pm_runtime_get_if_active(port->dev); 1903 if (!pm_status) 1904 return 0; 1905 1906 iir = serial_port_in(port, UART_IIR); 1907 ret = serial8250_handle_irq(port, iir); 1908 1909 if (pm_status > 0) 1910 pm_runtime_put_autosuspend(port->dev); 1911 1912 return ret; 1913 } 1914 1915 /* 1916 * Newer 16550 compatible parts such as the SC16C650 & Altera 16550 Soft IP 1917 * have a programmable TX threshold that triggers the THRE interrupt in 1918 * the IIR register. In this case, the THRE interrupt indicates the FIFO 1919 * has space available. Load it up with tx_loadsz bytes. 1920 */ 1921 static int serial8250_tx_threshold_handle_irq(struct uart_port *port) 1922 { 1923 unsigned int iir = serial_port_in(port, UART_IIR); 1924 1925 /* TX Threshold IRQ triggered so load up FIFO */ 1926 if ((iir & UART_IIR_ID) == UART_IIR_THRI) { 1927 struct uart_8250_port *up = up_to_u8250p(port); 1928 1929 guard(uart_port_lock_irqsave)(port); 1930 serial8250_tx_chars(up); 1931 } 1932 1933 iir = serial_port_in(port, UART_IIR); 1934 return serial8250_handle_irq(port, iir); 1935 } 1936 1937 static unsigned int serial8250_tx_empty(struct uart_port *port) 1938 { 1939 struct uart_8250_port *up = up_to_u8250p(port); 1940 1941 guard(serial8250_rpm)(up); 1942 guard(uart_port_lock_irqsave)(port); 1943 1944 if (!serial8250_tx_dma_running(up) && uart_lsr_tx_empty(serial_lsr_in(up))) 1945 return TIOCSER_TEMT; 1946 1947 return 0; 1948 } 1949 1950 unsigned int serial8250_do_get_mctrl(struct uart_port *port) 1951 { 1952 struct uart_8250_port *up = up_to_u8250p(port); 1953 unsigned int status; 1954 unsigned int val; 1955 1956 scoped_guard(serial8250_rpm, up) 1957 status = serial8250_modem_status(up); 1958 1959 val = serial8250_MSR_to_TIOCM(status); 1960 if (up->gpios) 1961 return mctrl_gpio_get(up->gpios, &val); 1962 1963 return val; 1964 } 1965 EXPORT_SYMBOL_GPL(serial8250_do_get_mctrl); 1966 1967 static unsigned int serial8250_get_mctrl(struct uart_port *port) 1968 { 1969 if (port->get_mctrl) 1970 return port->get_mctrl(port); 1971 return serial8250_do_get_mctrl(port); 1972 } 1973 1974 void serial8250_do_set_mctrl(struct uart_port *port, unsigned int mctrl) 1975 { 1976 struct uart_8250_port *up = up_to_u8250p(port); 1977 unsigned char mcr; 1978 1979 mcr = serial8250_TIOCM_to_MCR(mctrl); 1980 1981 mcr |= up->mcr; 1982 1983 serial8250_out_MCR(up, mcr); 1984 } 1985 EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl); 1986 1987 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl) 1988 { 1989 if (port->rs485.flags & SER_RS485_ENABLED) 1990 return; 1991 1992 if (port->set_mctrl) 1993 port->set_mctrl(port, mctrl); 1994 else 1995 serial8250_do_set_mctrl(port, mctrl); 1996 } 1997 1998 void serial8250_do_break_ctl(struct uart_port *port, int break_state) 1999 { 2000 struct uart_8250_port *up = up_to_u8250p(port); 2001 2002 guard(serial8250_rpm)(up); 2003 guard(uart_port_lock_irqsave)(port); 2004 2005 if (break_state == -1) 2006 up->lcr |= UART_LCR_SBC; 2007 else 2008 up->lcr &= ~UART_LCR_SBC; 2009 serial_port_out(port, UART_LCR, up->lcr); 2010 } 2011 EXPORT_SYMBOL_GPL(serial8250_do_break_ctl); 2012 2013 static void serial8250_break_ctl(struct uart_port *port, int break_state) 2014 { 2015 if (port->break_ctl) 2016 port->break_ctl(port, break_state); 2017 else 2018 serial8250_do_break_ctl(port, break_state); 2019 } 2020 2021 /* Returns true if @bits were set, false on timeout */ 2022 static bool wait_for_lsr(struct uart_8250_port *up, int bits) 2023 { 2024 unsigned int status, tmout; 2025 2026 /* 2027 * Wait for a character to be sent. Fallback to a safe default 2028 * timeout value if @frame_time is not available. 2029 */ 2030 if (up->port.frame_time) 2031 tmout = up->port.frame_time * 2 / NSEC_PER_USEC; 2032 else 2033 tmout = 10000; 2034 2035 for (;;) { 2036 status = serial_lsr_in(up); 2037 2038 if ((status & bits) == bits) 2039 break; 2040 if (--tmout == 0) 2041 break; 2042 udelay(1); 2043 touch_nmi_watchdog(); 2044 } 2045 2046 return (tmout != 0); 2047 } 2048 2049 /* Wait for transmitter and holding register to empty with timeout */ 2050 void serial8250_wait_for_xmitr(struct uart_8250_port *up, int bits) 2051 { 2052 unsigned int tmout; 2053 bool tx_ready; 2054 2055 tx_ready = wait_for_lsr(up, bits); 2056 2057 /* 2058 * Wait up to 1s for flow control if necessary. 2059 * When 'no_console_suspend' is active (in the window between 2060 * suspend() and resume()), flow control is temporarily ignored 2061 * because the canary workaround is not reliable in all situations, 2062 * leading to flow control timeouts for every character. 2063 */ 2064 if (uart_console_hwflow_active(&up->port) && !up->canary) { 2065 for (tmout = 1000000; tmout; tmout--) { 2066 unsigned int msr = serial_in(up, UART_MSR); 2067 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS; 2068 if (msr & UART_MSR_CTS) { 2069 if (!tx_ready) 2070 wait_for_lsr(up, bits); 2071 break; 2072 } 2073 udelay(1); 2074 touch_nmi_watchdog(); 2075 } 2076 } 2077 } 2078 EXPORT_SYMBOL_NS_GPL(serial8250_wait_for_xmitr, "SERIAL_8250"); 2079 2080 #ifdef CONFIG_CONSOLE_POLL 2081 /* 2082 * Console polling routines for writing and reading from the uart while 2083 * in an interrupt or debug context. 2084 */ 2085 2086 static int serial8250_get_poll_char(struct uart_port *port) 2087 { 2088 struct uart_8250_port *up = up_to_u8250p(port); 2089 u16 lsr; 2090 2091 guard(serial8250_rpm)(up); 2092 2093 lsr = serial_port_in(port, UART_LSR); 2094 if (!(lsr & UART_LSR_DR)) 2095 return NO_POLL_CHAR; 2096 2097 return serial_port_in(port, UART_RX); 2098 } 2099 2100 2101 static void serial8250_put_poll_char(struct uart_port *port, 2102 unsigned char c) 2103 { 2104 unsigned int ier; 2105 struct uart_8250_port *up = up_to_u8250p(port); 2106 2107 /* 2108 * Normally the port is locked to synchronize UART_IER access 2109 * against the console. However, this function is only used by 2110 * KDB/KGDB, where it may not be possible to acquire the port 2111 * lock because all other CPUs are quiesced. The quiescence 2112 * should allow safe lockless usage here. 2113 */ 2114 2115 guard(serial8250_rpm)(up); 2116 /* 2117 * First, save the IER, then disable the interrupts. The special 2118 * variant to clear the IER is used because KDB/KGDB printing 2119 * is synchronized via CPU quiescence without holding the port 2120 * lock. 2121 */ 2122 ier = serial_port_in(port, UART_IER); 2123 __serial8250_clear_IER(up); 2124 2125 serial8250_wait_for_xmitr(up, UART_LSR_BOTH_EMPTY); 2126 /* 2127 * Send the character out. 2128 */ 2129 serial_port_out(port, UART_TX, c); 2130 2131 /* 2132 * Finally, wait for transmitter to become empty 2133 * and restore the IER 2134 */ 2135 serial8250_wait_for_xmitr(up, UART_LSR_BOTH_EMPTY); 2136 serial_port_out(port, UART_IER, ier); 2137 } 2138 2139 #endif /* CONFIG_CONSOLE_POLL */ 2140 2141 static void serial8250_startup_special(struct uart_port *port) 2142 { 2143 struct uart_8250_port *up = up_to_u8250p(port); 2144 2145 switch (port->type) { 2146 case PORT_16C950: { 2147 /* 2148 * Wake up and initialize UART 2149 * 2150 * Synchronize UART_IER access against the console. 2151 */ 2152 guard(uart_port_lock_irqsave)(port); 2153 up->acr = 0; 2154 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B); 2155 serial_port_out(port, UART_EFR, UART_EFR_ECB); 2156 serial_port_out(port, UART_IER, 0); 2157 serial_port_out(port, UART_LCR, 0); 2158 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */ 2159 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B); 2160 serial_port_out(port, UART_EFR, UART_EFR_ECB); 2161 serial_port_out(port, UART_LCR, 0); 2162 break; 2163 } 2164 case PORT_DA830: 2165 /* 2166 * Reset the port 2167 * 2168 * Synchronize UART_IER access against the console. 2169 */ 2170 scoped_guard(uart_port_lock_irqsave, port) { 2171 serial_port_out(port, UART_IER, 0); 2172 serial_port_out(port, UART_DA830_PWREMU_MGMT, 0); 2173 } 2174 mdelay(10); 2175 2176 /* Enable Tx, Rx and free run mode */ 2177 serial_port_out(port, UART_DA830_PWREMU_MGMT, 2178 UART_DA830_PWREMU_MGMT_UTRST | 2179 UART_DA830_PWREMU_MGMT_URRST | 2180 UART_DA830_PWREMU_MGMT_FREE); 2181 break; 2182 case PORT_RSA: 2183 rsa_enable(up); 2184 break; 2185 } 2186 } 2187 2188 static void serial8250_set_TRG_levels(struct uart_port *port) 2189 { 2190 struct uart_8250_port *up = up_to_u8250p(port); 2191 2192 switch (port->type) { 2193 /* For a XR16C850, we need to set the trigger levels */ 2194 case PORT_16850: { 2195 u8 fctr; 2196 2197 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); 2198 2199 fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX); 2200 fctr |= UART_FCTR_TRGD; 2201 serial_port_out(port, UART_FCTR, fctr | UART_FCTR_RX); 2202 serial_port_out(port, UART_TRG, UART_TRG_96); 2203 serial_port_out(port, UART_FCTR, fctr | UART_FCTR_TX); 2204 serial_port_out(port, UART_TRG, UART_TRG_96); 2205 2206 serial_port_out(port, UART_LCR, 0); 2207 break; 2208 } 2209 /* For the Altera 16550 variants, set TX threshold trigger level. */ 2210 case PORT_ALTR_16550_F32: 2211 case PORT_ALTR_16550_F64: 2212 case PORT_ALTR_16550_F128: 2213 if (port->fifosize <= 1) 2214 return; 2215 2216 /* Bounds checking of TX threshold (valid 0 to fifosize-2) */ 2217 if (up->tx_loadsz < 2 || up->tx_loadsz > port->fifosize) { 2218 dev_err(port->dev, "TX FIFO Threshold errors, skipping\n"); 2219 return; 2220 } 2221 serial_port_out(port, UART_ALTR_AFR, UART_ALTR_EN_TXFIFO_LW); 2222 serial_port_out(port, UART_ALTR_TX_LOW, port->fifosize - up->tx_loadsz); 2223 port->handle_irq = serial8250_tx_threshold_handle_irq; 2224 break; 2225 } 2226 } 2227 2228 static void serial8250_THRE_test(struct uart_port *port) 2229 { 2230 struct uart_8250_port *up = up_to_u8250p(port); 2231 bool iir_noint1, iir_noint2; 2232 2233 if (!port->irq) 2234 return; 2235 2236 if (up->port.flags & UPF_NO_THRE_TEST) 2237 return; 2238 2239 disable_irq(port->irq); 2240 2241 /* 2242 * Test for UARTs that do not reassert THRE when the transmitter is idle and the interrupt 2243 * has already been cleared. Real 16550s should always reassert this interrupt whenever the 2244 * transmitter is idle and the interrupt is enabled. Delays are necessary to allow register 2245 * changes to become visible. 2246 * 2247 * Synchronize UART_IER access against the console. 2248 */ 2249 scoped_guard(uart_port_lock_irqsave, port) { 2250 serial8250_wait_for_xmitr(up, UART_LSR_THRE); 2251 serial_port_out_sync(port, UART_IER, UART_IER_THRI); 2252 udelay(1); /* allow THRE to set */ 2253 iir_noint1 = serial_port_in(port, UART_IIR) & UART_IIR_NO_INT; 2254 serial_port_out(port, UART_IER, 0); 2255 serial_port_out_sync(port, UART_IER, UART_IER_THRI); 2256 udelay(1); /* allow a working UART time to re-assert THRE */ 2257 iir_noint2 = serial_port_in(port, UART_IIR) & UART_IIR_NO_INT; 2258 serial_port_out(port, UART_IER, 0); 2259 } 2260 2261 enable_irq(port->irq); 2262 2263 /* 2264 * If the interrupt is not reasserted, or we otherwise don't trust the iir, setup a timer to 2265 * kick the UART on a regular basis. 2266 */ 2267 if ((!iir_noint1 && iir_noint2) || up->port.flags & UPF_BUG_THRE) 2268 up->bugs |= UART_BUG_THRE; 2269 } 2270 2271 static void serial8250_init_mctrl(struct uart_port *port) 2272 { 2273 if (port->flags & UPF_FOURPORT) { 2274 if (!port->irq) 2275 port->mctrl |= TIOCM_OUT1; 2276 } else { 2277 /* Most PC uarts need OUT2 raised to enable interrupts. */ 2278 if (port->irq) 2279 port->mctrl |= TIOCM_OUT2; 2280 } 2281 2282 serial8250_set_mctrl(port, port->mctrl); 2283 } 2284 2285 static void serial8250_iir_txen_test(struct uart_port *port) 2286 { 2287 struct uart_8250_port *up = up_to_u8250p(port); 2288 bool lsr_temt, iir_noint; 2289 2290 if (port->quirks & UPQ_NO_TXEN_TEST) 2291 return; 2292 2293 /* Do a quick test to see if we receive an interrupt when we enable the TX irq. */ 2294 serial_port_out(port, UART_IER, UART_IER_THRI); 2295 lsr_temt = serial_port_in(port, UART_LSR) & UART_LSR_TEMT; 2296 iir_noint = serial_port_in(port, UART_IIR) & UART_IIR_NO_INT; 2297 serial_port_out(port, UART_IER, 0); 2298 2299 /* 2300 * Serial over Lan (SoL) hack: 2301 * Intel 8257x Gigabit ethernet chips have a 16550 emulation, to be used for Serial Over 2302 * Lan. Those chips take a longer time than a normal serial device to signalize that a 2303 * transmission data was queued. Due to that, the above test generally fails. One solution 2304 * would be to delay the reading of iir. However, this is not reliable, since the timeout is 2305 * variable. So, in case of UPQ_NO_TXEN_TEST, let's just don't test if we receive TX irq. 2306 * This way, we'll never enable UART_BUG_TXEN. 2307 */ 2308 if (lsr_temt && iir_noint) { 2309 if (!(up->bugs & UART_BUG_TXEN)) { 2310 up->bugs |= UART_BUG_TXEN; 2311 dev_dbg(port->dev, "enabling bad tx status workarounds\n"); 2312 } 2313 return; 2314 } 2315 2316 /* FIXME: why is this needed? */ 2317 up->bugs &= ~UART_BUG_TXEN; 2318 } 2319 2320 static void serial8250_initialize(struct uart_port *port) 2321 { 2322 guard(uart_port_lock_irqsave)(port); 2323 serial_port_out(port, UART_LCR, UART_LCR_WLEN8); 2324 2325 serial8250_init_mctrl(port); 2326 serial8250_iir_txen_test(port); 2327 } 2328 2329 int serial8250_do_startup(struct uart_port *port) 2330 { 2331 struct uart_8250_port *up = up_to_u8250p(port); 2332 int retval; 2333 2334 if (!port->fifosize) 2335 port->fifosize = uart_config[port->type].fifo_size; 2336 if (!up->tx_loadsz) 2337 up->tx_loadsz = uart_config[port->type].tx_loadsz; 2338 if (!up->capabilities) 2339 up->capabilities = uart_config[port->type].flags; 2340 up->mcr = 0; 2341 2342 if (port->iotype != up->cur_iotype) 2343 set_io_from_upio(port); 2344 2345 guard(serial8250_rpm)(up); 2346 2347 serial8250_startup_special(port); 2348 2349 /* 2350 * Clear the FIFO buffers and disable them. 2351 * (they will be reenabled in set_termios()) 2352 */ 2353 serial8250_clear_fifos(up); 2354 2355 serial8250_clear_interrupts(port); 2356 2357 /* 2358 * At this point, there's no way the LSR could still be 0xff; 2359 * if it is, then bail out, because there's likely no UART 2360 * here. 2361 */ 2362 if (!(port->flags & UPF_BUGGY_UART) && 2363 (serial_port_in(port, UART_LSR) == 0xff)) { 2364 dev_info_ratelimited(port->dev, "LSR safety check engaged!\n"); 2365 return -ENODEV; 2366 } 2367 2368 serial8250_set_TRG_levels(port); 2369 2370 /* Check if we need to have shared IRQs */ 2371 if (port->irq && (up->port.flags & UPF_SHARE_IRQ)) 2372 up->port.irqflags |= IRQF_SHARED; 2373 2374 retval = up->ops->setup_irq(up); 2375 if (retval) 2376 return retval; 2377 2378 serial8250_THRE_test(port); 2379 2380 up->ops->setup_timer(up); 2381 2382 serial8250_initialize(port); 2383 2384 /* 2385 * Clear the interrupt registers again for luck, and clear the 2386 * saved flags to avoid getting false values from polling 2387 * routines or the previous session. 2388 */ 2389 serial8250_clear_interrupts(port); 2390 up->lsr_saved_flags = 0; 2391 up->msr_saved_flags = 0; 2392 2393 /* 2394 * Request DMA channels for both RX and TX. 2395 */ 2396 if (up->dma) { 2397 const char *msg = NULL; 2398 2399 if (uart_console(port)) 2400 msg = "forbid DMA for kernel console"; 2401 else if (serial8250_request_dma(up)) 2402 msg = "failed to request DMA"; 2403 if (msg) { 2404 dev_warn_ratelimited(port->dev, "%s\n", msg); 2405 up->dma = NULL; 2406 } 2407 } 2408 2409 /* 2410 * Set the IER shadow for rx interrupts but defer actual interrupt 2411 * enable until after the FIFOs are enabled; otherwise, an already- 2412 * active sender can swamp the interrupt handler with "too much work". 2413 */ 2414 up->ier = UART_IER_RLSI | UART_IER_RDI; 2415 2416 if (port->flags & UPF_FOURPORT) { 2417 unsigned int icp; 2418 /* 2419 * Enable interrupts on the AST Fourport board 2420 */ 2421 icp = (port->iobase & 0xfe0) | 0x01f; 2422 outb_p(0x80, icp); 2423 inb_p(icp); 2424 } 2425 2426 return 0; 2427 } 2428 EXPORT_SYMBOL_GPL(serial8250_do_startup); 2429 2430 static int serial8250_startup(struct uart_port *port) 2431 { 2432 if (port->startup) 2433 return port->startup(port); 2434 return serial8250_do_startup(port); 2435 } 2436 2437 void serial8250_do_shutdown(struct uart_port *port) 2438 { 2439 struct uart_8250_port *up = up_to_u8250p(port); 2440 u32 lcr; 2441 2442 serial8250_rpm_get(up); 2443 /* 2444 * Disable interrupts from this port 2445 * 2446 * Synchronize UART_IER access against the console. 2447 */ 2448 scoped_guard(uart_port_lock_irqsave, port) { 2449 up->ier = 0; 2450 serial_port_out(port, UART_IER, 0); 2451 } 2452 2453 synchronize_irq(port->irq); 2454 2455 if (up->dma) 2456 serial8250_release_dma(up); 2457 2458 scoped_guard(uart_port_lock_irqsave, port) { 2459 if (port->flags & UPF_FOURPORT) { 2460 /* reset interrupts on the AST Fourport board */ 2461 inb((port->iobase & 0xfe0) | 0x1f); 2462 port->mctrl |= TIOCM_OUT1; 2463 } else 2464 port->mctrl &= ~TIOCM_OUT2; 2465 2466 serial8250_set_mctrl(port, port->mctrl); 2467 2468 /* Disable break condition */ 2469 lcr = serial_port_in(port, UART_LCR); 2470 lcr &= ~UART_LCR_SBC; 2471 serial_port_out(port, UART_LCR, lcr); 2472 } 2473 2474 serial8250_clear_fifos(up); 2475 2476 rsa_disable(up); 2477 2478 /* 2479 * Read data port to reset things, and then unlink from 2480 * the IRQ chain. 2481 */ 2482 serial_port_in(port, UART_RX); 2483 /* 2484 * LCR writes on DW UART can trigger late (unmaskable) IRQs. 2485 * Handle them before releasing the handler. 2486 */ 2487 synchronize_irq(port->irq); 2488 2489 serial8250_rpm_put(up); 2490 2491 up->ops->release_irq(up); 2492 } 2493 EXPORT_SYMBOL_GPL(serial8250_do_shutdown); 2494 2495 static void serial8250_shutdown(struct uart_port *port) 2496 { 2497 if (port->shutdown) 2498 port->shutdown(port); 2499 else 2500 serial8250_do_shutdown(port); 2501 } 2502 2503 static void serial8250_flush_buffer(struct uart_port *port) 2504 { 2505 struct uart_8250_port *up = up_to_u8250p(port); 2506 2507 if (up->dma) 2508 serial8250_tx_dma_flush(up); 2509 } 2510 2511 static unsigned int serial8250_do_get_divisor(struct uart_port *port, unsigned int baud) 2512 { 2513 upf_t magic_multiplier = port->flags & UPF_MAGIC_MULTIPLIER; 2514 struct uart_8250_port *up = up_to_u8250p(port); 2515 unsigned int quot; 2516 2517 /* 2518 * Handle magic divisors for baud rates above baud_base on SMSC 2519 * Super I/O chips. We clamp custom rates from clk/6 and clk/12 2520 * up to clk/4 (0x8001) and clk/8 (0x8002) respectively. These 2521 * magic divisors actually reprogram the baud rate generator's 2522 * reference clock derived from chips's 14.318MHz clock input. 2523 * 2524 * Documentation claims that with these magic divisors the base 2525 * frequencies of 7.3728MHz and 3.6864MHz are used respectively 2526 * for the extra baud rates of 460800bps and 230400bps rather 2527 * than the usual base frequency of 1.8462MHz. However empirical 2528 * evidence contradicts that. 2529 * 2530 * Instead bit 7 of the DLM register (bit 15 of the divisor) is 2531 * effectively used as a clock prescaler selection bit for the 2532 * base frequency of 7.3728MHz, always used. If set to 0, then 2533 * the base frequency is divided by 4 for use by the Baud Rate 2534 * Generator, for the usual arrangement where the value of 1 of 2535 * the divisor produces the baud rate of 115200bps. Conversely, 2536 * if set to 1 and high-speed operation has been enabled with the 2537 * Serial Port Mode Register in the Device Configuration Space, 2538 * then the base frequency is supplied directly to the Baud Rate 2539 * Generator, so for the divisor values of 0x8001, 0x8002, 0x8003, 2540 * 0x8004, etc. the respective baud rates produced are 460800bps, 2541 * 230400bps, 153600bps, 115200bps, etc. 2542 * 2543 * In all cases only low 15 bits of the divisor are used to divide 2544 * the baud base and therefore 32767 is the maximum divisor value 2545 * possible, even though documentation says that the programmable 2546 * Baud Rate Generator is capable of dividing the internal PLL 2547 * clock by any divisor from 1 to 65535. 2548 */ 2549 if (magic_multiplier && baud >= port->uartclk / 6) 2550 quot = 0x8001; 2551 else if (magic_multiplier && baud >= port->uartclk / 12) 2552 quot = 0x8002; 2553 else 2554 quot = uart_get_divisor(port, baud); 2555 2556 /* 2557 * Oxford Semi 952 rev B workaround 2558 */ 2559 if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0) 2560 quot++; 2561 2562 return quot; 2563 } 2564 2565 static unsigned int serial8250_get_divisor(struct uart_port *port, 2566 unsigned int baud, 2567 unsigned int *frac) 2568 { 2569 if (port->get_divisor) 2570 return port->get_divisor(port, baud, frac); 2571 2572 return serial8250_do_get_divisor(port, baud); 2573 } 2574 2575 static unsigned char serial8250_compute_lcr(struct uart_8250_port *up, tcflag_t c_cflag) 2576 { 2577 u8 lcr = UART_LCR_WLEN(tty_get_char_size(c_cflag)); 2578 2579 if (c_cflag & CSTOPB) 2580 lcr |= UART_LCR_STOP; 2581 if (c_cflag & PARENB) 2582 lcr |= UART_LCR_PARITY; 2583 if (!(c_cflag & PARODD)) 2584 lcr |= UART_LCR_EPAR; 2585 if (c_cflag & CMSPAR) 2586 lcr |= UART_LCR_SPAR; 2587 2588 return lcr; 2589 } 2590 2591 void serial8250_do_set_divisor(struct uart_port *port, unsigned int baud, 2592 unsigned int quot) 2593 { 2594 struct uart_8250_port *up = up_to_u8250p(port); 2595 2596 /* Workaround to enable 115200 baud on OMAP1510 internal ports */ 2597 if (is_omap1510_8250(up)) { 2598 if (baud == 115200) { 2599 quot = 1; 2600 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1); 2601 } else 2602 serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0); 2603 } 2604 2605 /* 2606 * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2, 2607 * otherwise just set DLAB 2608 */ 2609 if (up->capabilities & UART_NATSEMI) 2610 serial_port_out(port, UART_LCR, 0xe0); 2611 else 2612 serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB); 2613 2614 serial_dl_write(up, quot); 2615 } 2616 EXPORT_SYMBOL_GPL(serial8250_do_set_divisor); 2617 2618 static void serial8250_set_divisor(struct uart_port *port, unsigned int baud, 2619 unsigned int quot, unsigned int quot_frac) 2620 { 2621 if (port->set_divisor) 2622 port->set_divisor(port, baud, quot, quot_frac); 2623 else 2624 serial8250_do_set_divisor(port, baud, quot); 2625 } 2626 2627 static unsigned int serial8250_get_baud_rate(struct uart_port *port, 2628 struct ktermios *termios, 2629 const struct ktermios *old) 2630 { 2631 unsigned int tolerance = port->uartclk / 100; 2632 unsigned int min; 2633 unsigned int max; 2634 2635 /* 2636 * Handle magic divisors for baud rates above baud_base on SMSC 2637 * Super I/O chips. Enable custom rates of clk/4 and clk/8, but 2638 * disable divisor values beyond 32767, which are unavailable. 2639 */ 2640 if (port->flags & UPF_MAGIC_MULTIPLIER) { 2641 min = port->uartclk / 16 / UART_DIV_MAX >> 1; 2642 max = (port->uartclk + tolerance) / 4; 2643 } else { 2644 min = port->uartclk / 16 / UART_DIV_MAX; 2645 max = (port->uartclk + tolerance) / 16; 2646 } 2647 2648 /* 2649 * Ask the core to calculate the divisor for us. 2650 * Allow 1% tolerance at the upper limit so uart clks marginally 2651 * slower than nominal still match standard baud rates without 2652 * causing transmission errors. 2653 */ 2654 return uart_get_baud_rate(port, termios, old, min, max); 2655 } 2656 2657 /* 2658 * Note in order to avoid the tty port mutex deadlock don't use the next method 2659 * within the uart port callbacks. Primarily it's supposed to be utilized to 2660 * handle a sudden reference clock rate change. 2661 */ 2662 void serial8250_update_uartclk(struct uart_port *port, unsigned int uartclk) 2663 { 2664 struct tty_port *tport = &port->state->port; 2665 2666 scoped_guard(tty_port_tty, tport) { 2667 struct tty_struct *tty = scoped_tty(); 2668 2669 guard(rwsem_write)(&tty->termios_rwsem); 2670 guard(mutex)(&tport->mutex); 2671 2672 if (port->uartclk == uartclk) 2673 return; 2674 2675 port->uartclk = uartclk; 2676 2677 if (!tty_port_initialized(tport)) 2678 return; 2679 2680 serial8250_do_set_termios(port, &tty->termios, NULL); 2681 2682 return; 2683 } 2684 guard(mutex)(&tport->mutex); 2685 port->uartclk = uartclk; 2686 } 2687 EXPORT_SYMBOL_GPL(serial8250_update_uartclk); 2688 2689 static void serial8250_set_mini(struct uart_port *port, struct ktermios *termios) 2690 { 2691 struct uart_8250_port *up = up_to_u8250p(port); 2692 2693 if (!(up->capabilities & UART_CAP_MINI)) 2694 return; 2695 2696 termios->c_cflag &= ~(CSTOPB | PARENB | PARODD | CMSPAR); 2697 2698 tcflag_t csize = termios->c_cflag & CSIZE; 2699 if (csize == CS5 || csize == CS6) { 2700 termios->c_cflag &= ~CSIZE; 2701 termios->c_cflag |= CS7; 2702 } 2703 } 2704 2705 static void serial8250_set_trigger_for_slow_speed(struct uart_port *port, struct ktermios *termios, 2706 unsigned int baud) 2707 { 2708 struct uart_8250_port *up = up_to_u8250p(port); 2709 2710 if (!(up->capabilities & UART_CAP_FIFO)) 2711 return; 2712 if (port->fifosize <= 1) 2713 return; 2714 if (baud >= 2400) 2715 return; 2716 if (up->dma) 2717 return; 2718 2719 up->fcr &= ~UART_FCR_TRIGGER_MASK; 2720 up->fcr |= UART_FCR_TRIGGER_1; 2721 } 2722 2723 /* 2724 * MCR-based auto flow control. When AFE is enabled, RTS will be deasserted when the receive FIFO 2725 * contains more characters than the trigger, or the MCR RTS bit is cleared. 2726 */ 2727 static void serial8250_set_afe(struct uart_port *port, struct ktermios *termios) 2728 { 2729 struct uart_8250_port *up = up_to_u8250p(port); 2730 2731 if (!(up->capabilities & UART_CAP_AFE)) 2732 return; 2733 2734 up->mcr &= ~UART_MCR_AFE; 2735 if (termios->c_cflag & CRTSCTS) 2736 up->mcr |= UART_MCR_AFE; 2737 } 2738 2739 static void serial8250_set_errors_and_ignores(struct uart_port *port, struct ktermios *termios) 2740 { 2741 /* 2742 * Specify which conditions may be considered for error handling and the ignoring of 2743 * characters. The actual ignoring of characters only occurs if the bit is set in 2744 * @ignore_status_mask as well. 2745 */ 2746 port->read_status_mask = UART_LSR_OE | UART_LSR_DR; 2747 if (termios->c_iflag & INPCK) 2748 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE; 2749 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 2750 port->read_status_mask |= UART_LSR_BI; 2751 2752 /* Characters to ignore */ 2753 port->ignore_status_mask = 0; 2754 if (termios->c_iflag & IGNPAR) 2755 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE; 2756 if (termios->c_iflag & IGNBRK) { 2757 port->ignore_status_mask |= UART_LSR_BI; 2758 /* 2759 * If we're ignoring parity and break indicators, ignore overruns too (for real raw 2760 * support). 2761 */ 2762 if (termios->c_iflag & IGNPAR) 2763 port->ignore_status_mask |= UART_LSR_OE; 2764 } 2765 2766 /* ignore all characters if CREAD is not set */ 2767 if ((termios->c_cflag & CREAD) == 0) 2768 port->ignore_status_mask |= UART_LSR_DR; 2769 } 2770 2771 static void serial8250_set_ier(struct uart_port *port, struct ktermios *termios) 2772 { 2773 struct uart_8250_port *up = up_to_u8250p(port); 2774 2775 /* CTS flow control flag and modem status interrupts */ 2776 up->ier &= ~UART_IER_MSI; 2777 if (!(up->bugs & UART_BUG_NOMSR) && UART_ENABLE_MS(&up->port, termios->c_cflag)) 2778 up->ier |= UART_IER_MSI; 2779 if (up->capabilities & UART_CAP_UUE) 2780 up->ier |= UART_IER_UUE; 2781 if (up->capabilities & UART_CAP_RTOIE) 2782 up->ier |= UART_IER_RTOIE; 2783 2784 serial_port_out(port, UART_IER, up->ier); 2785 } 2786 2787 static void serial8250_set_efr(struct uart_port *port, struct ktermios *termios) 2788 { 2789 struct uart_8250_port *up = up_to_u8250p(port); 2790 u8 efr_reg = UART_EFR; 2791 u8 efr = 0; 2792 2793 if (!(up->capabilities & UART_CAP_EFR)) 2794 return; 2795 2796 /* 2797 * TI16C752/Startech hardware flow control. FIXME: 2798 * - TI16C752 requires control thresholds to be set. 2799 * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled. 2800 */ 2801 if (termios->c_cflag & CRTSCTS) 2802 efr |= UART_EFR_CTS; 2803 2804 if (port->flags & UPF_EXAR_EFR) 2805 efr_reg = UART_XR_EFR; 2806 2807 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B); 2808 serial_port_out(port, efr_reg, efr); 2809 } 2810 2811 static void serial8250_set_fcr(struct uart_port *port, struct ktermios *termios) 2812 { 2813 struct uart_8250_port *up = up_to_u8250p(port); 2814 bool is_16750 = port->type == PORT_16750; 2815 2816 if (is_16750) 2817 serial_port_out(port, UART_FCR, up->fcr); 2818 2819 /* 2820 * LCR DLAB must be reset to enable 64-byte FIFO mode. If the FCR is written without DLAB 2821 * set, this mode will be disabled. 2822 */ 2823 serial_port_out(port, UART_LCR, up->lcr); 2824 2825 if (is_16750) 2826 return; 2827 2828 /* emulated UARTs (Lucent Venus 167x) need two steps */ 2829 if (up->fcr & UART_FCR_ENABLE_FIFO) 2830 serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO); 2831 2832 serial_port_out(port, UART_FCR, up->fcr); 2833 } 2834 2835 void 2836 serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios, 2837 const struct ktermios *old) 2838 { 2839 struct uart_8250_port *up = up_to_u8250p(port); 2840 unsigned int baud, quot, frac = 0; 2841 u8 lcr; 2842 2843 serial8250_set_mini(port, termios); 2844 lcr = serial8250_compute_lcr(up, termios->c_cflag); 2845 baud = serial8250_get_baud_rate(port, termios, old); 2846 quot = serial8250_get_divisor(port, baud, &frac); 2847 2848 /* 2849 * Ok, we're now changing the port state. Do it with interrupts disabled. 2850 * 2851 * Synchronize UART_IER access against the console. 2852 */ 2853 scoped_guard(serial8250_rpm, up) { 2854 guard(uart_port_lock_irqsave)(port); 2855 2856 up->lcr = lcr; 2857 serial8250_set_trigger_for_slow_speed(port, termios, baud); 2858 serial8250_set_afe(port, termios); 2859 uart_update_timeout(port, termios->c_cflag, baud); 2860 serial8250_set_errors_and_ignores(port, termios); 2861 serial8250_set_ier(port, termios); 2862 serial8250_set_efr(port, termios); 2863 serial8250_set_divisor(port, baud, quot, frac); 2864 serial8250_set_fcr(port, termios); 2865 /* Consoles manually poll CTS for hardware flow control. */ 2866 if (uart_console(port) && 2867 !(port->rs485.flags & SER_RS485_ENABLED) 2868 && termios->c_cflag & CRTSCTS) { 2869 port->mctrl |= TIOCM_RTS; 2870 } 2871 serial8250_set_mctrl(port, port->mctrl); 2872 } 2873 2874 /* Don't rewrite B0 */ 2875 if (tty_termios_baud_rate(termios)) 2876 tty_termios_encode_baud_rate(termios, baud, baud); 2877 } 2878 EXPORT_SYMBOL(serial8250_do_set_termios); 2879 2880 static void 2881 serial8250_set_termios(struct uart_port *port, struct ktermios *termios, 2882 const struct ktermios *old) 2883 { 2884 if (port->set_termios) 2885 port->set_termios(port, termios, old); 2886 else 2887 serial8250_do_set_termios(port, termios, old); 2888 } 2889 2890 void serial8250_do_set_ldisc(struct uart_port *port, struct ktermios *termios) 2891 { 2892 if (termios->c_line == N_PPS) { 2893 port->flags |= UPF_HARDPPS_CD; 2894 guard(uart_port_lock_irq)(port); 2895 serial8250_enable_ms(port); 2896 } else { 2897 port->flags &= ~UPF_HARDPPS_CD; 2898 if (!UART_ENABLE_MS(port, termios->c_cflag)) { 2899 guard(uart_port_lock_irq)(port); 2900 serial8250_disable_ms(port); 2901 } 2902 } 2903 } 2904 EXPORT_SYMBOL_GPL(serial8250_do_set_ldisc); 2905 2906 static void 2907 serial8250_set_ldisc(struct uart_port *port, struct ktermios *termios) 2908 { 2909 if (port->set_ldisc) 2910 port->set_ldisc(port, termios); 2911 else 2912 serial8250_do_set_ldisc(port, termios); 2913 } 2914 2915 void serial8250_do_pm(struct uart_port *port, unsigned int state, 2916 unsigned int oldstate) 2917 { 2918 struct uart_8250_port *p = up_to_u8250p(port); 2919 2920 serial8250_set_sleep(p, state != 0); 2921 } 2922 EXPORT_SYMBOL(serial8250_do_pm); 2923 2924 static void 2925 serial8250_pm(struct uart_port *port, unsigned int state, 2926 unsigned int oldstate) 2927 { 2928 if (port->pm) 2929 port->pm(port, state, oldstate); 2930 else 2931 serial8250_do_pm(port, state, oldstate); 2932 } 2933 2934 static unsigned int serial8250_port_size(struct uart_8250_port *pt) 2935 { 2936 if (pt->port.mapsize) 2937 return pt->port.mapsize; 2938 if (is_omap1_8250(pt)) 2939 return 0x16 << pt->port.regshift; 2940 2941 return 8 << pt->port.regshift; 2942 } 2943 2944 /* 2945 * Resource handling. 2946 */ 2947 static int serial8250_request_std_resource(struct uart_8250_port *up) 2948 { 2949 unsigned int size = serial8250_port_size(up); 2950 struct uart_port *port = &up->port; 2951 2952 if (uart_iotype_mmio(port->iotype)) { 2953 if (!port->mapbase) 2954 return -EINVAL; 2955 2956 if (!request_mem_region(port->mapbase, size, "serial")) 2957 return -EBUSY; 2958 2959 if (port->flags & UPF_IOREMAP) { 2960 port->membase = ioremap(port->mapbase, size); 2961 if (!port->membase) { 2962 release_mem_region(port->mapbase, size); 2963 return -ENOMEM; 2964 } 2965 } 2966 } else if (uart_iotype_io(port->iotype)) { 2967 if (!request_region(port->iobase, size, "serial")) 2968 return -EBUSY; 2969 } 2970 2971 return 0; 2972 } 2973 2974 static void serial8250_release_std_resource(struct uart_8250_port *up) 2975 { 2976 unsigned int size = serial8250_port_size(up); 2977 struct uart_port *port = &up->port; 2978 2979 if (uart_iotype_mmio(port->iotype)) { 2980 if (!port->mapbase) 2981 return; 2982 2983 if (port->flags & UPF_IOREMAP) { 2984 iounmap(port->membase); 2985 port->membase = NULL; 2986 } 2987 2988 release_mem_region(port->mapbase, size); 2989 } else if (uart_iotype_io(port->iotype)) { 2990 release_region(port->iobase, size); 2991 } 2992 } 2993 2994 static void serial8250_release_port(struct uart_port *port) 2995 { 2996 struct uart_8250_port *up = up_to_u8250p(port); 2997 2998 serial8250_release_std_resource(up); 2999 } 3000 3001 static int serial8250_request_port(struct uart_port *port) 3002 { 3003 struct uart_8250_port *up = up_to_u8250p(port); 3004 3005 return serial8250_request_std_resource(up); 3006 } 3007 3008 static int fcr_get_rxtrig_bytes(struct uart_8250_port *up) 3009 { 3010 const struct serial8250_config *conf_type = &uart_config[up->port.type]; 3011 unsigned char bytes; 3012 3013 bytes = conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)]; 3014 3015 return bytes ? bytes : -EOPNOTSUPP; 3016 } 3017 3018 static int bytes_to_fcr_rxtrig(struct uart_8250_port *up, unsigned char bytes) 3019 { 3020 const struct serial8250_config *conf_type = &uart_config[up->port.type]; 3021 int i; 3022 3023 if (!conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(UART_FCR_R_TRIG_00)]) 3024 return -EOPNOTSUPP; 3025 3026 for (i = 1; i < UART_FCR_R_TRIG_MAX_STATE; i++) { 3027 if (bytes < conf_type->rxtrig_bytes[i]) 3028 /* Use the nearest lower value */ 3029 return (--i) << UART_FCR_R_TRIG_SHIFT; 3030 } 3031 3032 return UART_FCR_R_TRIG_11; 3033 } 3034 3035 static int do_get_rxtrig(struct tty_port *port) 3036 { 3037 struct uart_state *state = container_of(port, struct uart_state, port); 3038 struct uart_port *uport = state->uart_port; 3039 struct uart_8250_port *up = up_to_u8250p(uport); 3040 3041 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1) 3042 return -EINVAL; 3043 3044 return fcr_get_rxtrig_bytes(up); 3045 } 3046 3047 static int do_serial8250_get_rxtrig(struct tty_port *port) 3048 { 3049 int rxtrig_bytes; 3050 3051 mutex_lock(&port->mutex); 3052 rxtrig_bytes = do_get_rxtrig(port); 3053 mutex_unlock(&port->mutex); 3054 3055 return rxtrig_bytes; 3056 } 3057 3058 static ssize_t rx_trig_bytes_show(struct device *dev, 3059 struct device_attribute *attr, char *buf) 3060 { 3061 struct tty_port *port = dev_get_drvdata(dev); 3062 struct uart_state *state = container_of(port, struct uart_state, port); 3063 struct uart_port *uport = state->uart_port; 3064 int rxtrig_bytes; 3065 3066 if (uport->get_rxtrig) 3067 rxtrig_bytes = uport->get_rxtrig(uport); 3068 else 3069 rxtrig_bytes = do_serial8250_get_rxtrig(port); 3070 if (rxtrig_bytes < 0) 3071 return rxtrig_bytes; 3072 3073 return sysfs_emit(buf, "%d\n", rxtrig_bytes); 3074 } 3075 3076 static int do_set_rxtrig(struct tty_port *port, unsigned char bytes) 3077 { 3078 struct uart_state *state = container_of(port, struct uart_state, port); 3079 struct uart_port *uport = state->uart_port; 3080 struct uart_8250_port *up = up_to_u8250p(uport); 3081 int rxtrig; 3082 3083 if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1) 3084 return -EINVAL; 3085 3086 rxtrig = bytes_to_fcr_rxtrig(up, bytes); 3087 if (rxtrig < 0) 3088 return rxtrig; 3089 3090 serial8250_clear_fifos(up); 3091 up->fcr &= ~UART_FCR_TRIGGER_MASK; 3092 up->fcr |= (unsigned char)rxtrig; 3093 serial_out(up, UART_FCR, up->fcr); 3094 return 0; 3095 } 3096 3097 static int do_serial8250_set_rxtrig(struct tty_port *port, unsigned char bytes) 3098 { 3099 int ret; 3100 3101 mutex_lock(&port->mutex); 3102 ret = do_set_rxtrig(port, bytes); 3103 mutex_unlock(&port->mutex); 3104 3105 return ret; 3106 } 3107 3108 static ssize_t rx_trig_bytes_store(struct device *dev, 3109 struct device_attribute *attr, const char *buf, size_t count) 3110 { 3111 struct tty_port *port = dev_get_drvdata(dev); 3112 struct uart_state *state = container_of(port, struct uart_state, port); 3113 struct uart_port *uport = state->uart_port; 3114 unsigned char bytes; 3115 int ret; 3116 3117 if (!count) 3118 return -EINVAL; 3119 3120 ret = kstrtou8(buf, 10, &bytes); 3121 if (ret < 0) 3122 return ret; 3123 3124 if (uport->set_rxtrig) 3125 ret = uport->set_rxtrig(uport, bytes); 3126 else 3127 ret = do_serial8250_set_rxtrig(port, bytes); 3128 if (ret < 0) 3129 return ret; 3130 3131 return count; 3132 } 3133 3134 static DEVICE_ATTR_RW(rx_trig_bytes); 3135 3136 static struct attribute *serial8250_dev_attrs[] = { 3137 &dev_attr_rx_trig_bytes.attr, 3138 NULL 3139 }; 3140 3141 static struct attribute_group serial8250_dev_attr_group = { 3142 .attrs = serial8250_dev_attrs, 3143 }; 3144 3145 static void register_dev_spec_attr_grp(struct uart_8250_port *up) 3146 { 3147 const struct serial8250_config *conf_type = &uart_config[up->port.type]; 3148 3149 if (conf_type->rxtrig_bytes[0]) 3150 up->port.attr_group = &serial8250_dev_attr_group; 3151 } 3152 3153 static void serial8250_config_port(struct uart_port *port, int flags) 3154 { 3155 struct uart_8250_port *up = up_to_u8250p(port); 3156 int ret; 3157 3158 /* 3159 * Find the region that we can probe for. This in turn 3160 * tells us whether we can probe for the type of port. 3161 */ 3162 ret = serial8250_request_std_resource(up); 3163 if (ret < 0) 3164 return; 3165 3166 if (port->iotype != up->cur_iotype) 3167 set_io_from_upio(port); 3168 3169 if (flags & UART_CONFIG_TYPE) 3170 autoconfig(up); 3171 3172 /* HW bugs may trigger IRQ while IIR == NO_INT */ 3173 if (port->type == PORT_TEGRA) 3174 up->bugs |= UART_BUG_NOMSR; 3175 3176 if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ) 3177 autoconfig_irq(up); 3178 3179 if (port->type == PORT_UNKNOWN) 3180 serial8250_release_std_resource(up); 3181 3182 register_dev_spec_attr_grp(up); 3183 up->fcr = uart_config[up->port.type].fcr; 3184 } 3185 3186 static int 3187 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser) 3188 { 3189 if (ser->irq >= irq_get_nr_irqs() || ser->irq < 0 || 3190 ser->baud_base < 9600 || ser->type < PORT_UNKNOWN || 3191 ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS || 3192 ser->type == PORT_STARTECH) 3193 return -EINVAL; 3194 return 0; 3195 } 3196 3197 static const char *serial8250_type(struct uart_port *port) 3198 { 3199 int type = port->type; 3200 3201 if (type >= ARRAY_SIZE(uart_config)) 3202 type = 0; 3203 return uart_config[type].name; 3204 } 3205 3206 static const struct uart_ops serial8250_pops = { 3207 .tx_empty = serial8250_tx_empty, 3208 .set_mctrl = serial8250_set_mctrl, 3209 .get_mctrl = serial8250_get_mctrl, 3210 .stop_tx = serial8250_stop_tx, 3211 .start_tx = serial8250_start_tx, 3212 .throttle = serial8250_throttle, 3213 .unthrottle = serial8250_unthrottle, 3214 .stop_rx = serial8250_stop_rx, 3215 .enable_ms = serial8250_enable_ms, 3216 .break_ctl = serial8250_break_ctl, 3217 .startup = serial8250_startup, 3218 .shutdown = serial8250_shutdown, 3219 .flush_buffer = serial8250_flush_buffer, 3220 .set_termios = serial8250_set_termios, 3221 .set_ldisc = serial8250_set_ldisc, 3222 .pm = serial8250_pm, 3223 .type = serial8250_type, 3224 .release_port = serial8250_release_port, 3225 .request_port = serial8250_request_port, 3226 .config_port = serial8250_config_port, 3227 .verify_port = serial8250_verify_port, 3228 #ifdef CONFIG_CONSOLE_POLL 3229 .poll_get_char = serial8250_get_poll_char, 3230 .poll_put_char = serial8250_put_poll_char, 3231 #endif 3232 }; 3233 3234 void serial8250_init_port(struct uart_8250_port *up) 3235 { 3236 struct uart_port *port = &up->port; 3237 3238 spin_lock_init(&port->lock); 3239 port->ctrl_id = 0; 3240 port->pm = NULL; 3241 port->ops = &serial8250_pops; 3242 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE); 3243 3244 up->cur_iotype = UPIO_UNKNOWN; 3245 } 3246 EXPORT_SYMBOL_GPL(serial8250_init_port); 3247 3248 void serial8250_set_defaults(struct uart_8250_port *up) 3249 { 3250 struct uart_port *port = &up->port; 3251 3252 if (up->port.flags & UPF_FIXED_TYPE) { 3253 unsigned int type = up->port.type; 3254 3255 if (!up->port.fifosize) 3256 up->port.fifosize = uart_config[type].fifo_size; 3257 if (!up->tx_loadsz) 3258 up->tx_loadsz = uart_config[type].tx_loadsz; 3259 if (!up->capabilities) 3260 up->capabilities = uart_config[type].flags; 3261 } 3262 3263 set_io_from_upio(port); 3264 3265 /* default dma handlers */ 3266 if (up->dma) { 3267 if (!up->dma->tx_dma) 3268 up->dma->tx_dma = serial8250_tx_dma; 3269 if (!up->dma->rx_dma) 3270 up->dma->rx_dma = serial8250_rx_dma; 3271 } 3272 } 3273 EXPORT_SYMBOL_GPL(serial8250_set_defaults); 3274 3275 void serial8250_fifo_wait_for_lsr_thre(struct uart_8250_port *up, 3276 struct nbcon_write_context *wctxt, 3277 unsigned int count) 3278 { 3279 unsigned int i; 3280 3281 /* 3282 * For console writing, enter/exit an unsafe section for each byte 3283 * in order to pass the ownership as quickly as possible if a higher 3284 * priority context wants ownership. Otherwise, an attempt to take 3285 * over the ownership might timeout. The new owner will wait for 3286 * UART_LSR_THRE before reusing the fifo. 3287 */ 3288 for (i = 0; i < count; i++) { 3289 bool tx_ready; 3290 3291 if (wctxt && !nbcon_enter_unsafe(wctxt)) 3292 return; 3293 3294 tx_ready = wait_for_lsr(up, UART_LSR_THRE); 3295 3296 if (wctxt) 3297 nbcon_exit_unsafe(wctxt); 3298 3299 if (tx_ready) 3300 break; 3301 } 3302 } 3303 EXPORT_SYMBOL_NS_GPL(serial8250_fifo_wait_for_lsr_thre, "SERIAL_8250"); 3304 3305 #ifdef CONFIG_SERIAL_8250_CONSOLE 3306 3307 static void serial8250_console_putchar(struct uart_port *port, unsigned char ch) 3308 { 3309 struct uart_8250_port *up = up_to_u8250p(port); 3310 3311 serial_port_out(port, UART_TX, ch); 3312 3313 up->console_line_ended = (ch == '\n'); 3314 } 3315 3316 static void serial8250_console_wait_putchar(struct uart_port *port, unsigned char ch) 3317 { 3318 struct uart_8250_port *up = up_to_u8250p(port); 3319 3320 serial8250_wait_for_xmitr(up, UART_LSR_THRE); 3321 serial8250_console_putchar(port, ch); 3322 } 3323 3324 /* 3325 * Restore serial console when h/w power-off detected 3326 */ 3327 static void serial8250_console_restore(struct uart_8250_port *up) 3328 { 3329 struct uart_port *port = &up->port; 3330 struct ktermios termios; 3331 unsigned int baud, quot, frac = 0; 3332 3333 termios.c_cflag = port->cons->cflag; 3334 termios.c_ispeed = port->cons->ispeed; 3335 termios.c_ospeed = port->cons->ospeed; 3336 if (port->state->port.tty && termios.c_cflag == 0) { 3337 termios.c_cflag = port->state->port.tty->termios.c_cflag; 3338 termios.c_ispeed = port->state->port.tty->termios.c_ispeed; 3339 termios.c_ospeed = port->state->port.tty->termios.c_ospeed; 3340 } 3341 3342 baud = serial8250_get_baud_rate(port, &termios, NULL); 3343 quot = serial8250_get_divisor(port, baud, &frac); 3344 3345 serial8250_set_divisor(port, baud, quot, frac); 3346 serial_port_out(port, UART_LCR, up->lcr); 3347 serial8250_out_MCR(up, up->mcr | UART_MCR_DTR | UART_MCR_RTS); 3348 } 3349 3350 /* 3351 * Print a string to the serial port using the device FIFO 3352 * 3353 * It sends fifosize bytes and then waits for the fifo 3354 * to get empty. 3355 */ 3356 static void __serial8250_console_fifo_write(struct uart_8250_port *up, 3357 struct nbcon_write_context *wctxt, 3358 const char *s, unsigned int count) 3359 { 3360 const char *end = s + count; 3361 unsigned int fifosize = up->tx_loadsz; 3362 struct uart_port *port = &up->port; 3363 unsigned int tx_count = 0; 3364 bool cr_sent = false; 3365 unsigned int i; 3366 3367 while (s != end) { 3368 /* Allow timeout for each byte of a possibly full FIFO */ 3369 serial8250_fifo_wait_for_lsr_thre(up, wctxt, fifosize); 3370 3371 /* 3372 * Fill the FIFO. If a handover or takeover occurs, writing 3373 * must be aborted since the string data is no longer valid. 3374 */ 3375 for (i = 0; i < fifosize && s != end; ++i) { 3376 if (!nbcon_enter_unsafe(wctxt)) 3377 return; 3378 3379 if (*s == '\n' && !cr_sent) { 3380 serial8250_console_putchar(port, '\r'); 3381 cr_sent = true; 3382 } else { 3383 serial8250_console_putchar(port, *s++); 3384 cr_sent = false; 3385 } 3386 3387 nbcon_exit_unsafe(wctxt); 3388 } 3389 tx_count = i; 3390 } 3391 3392 /* 3393 * Allow timeout for each byte written since the caller will only wait 3394 * for UART_LSR_BOTH_EMPTY using the timeout of a single character 3395 */ 3396 serial8250_fifo_wait_for_lsr_thre(up, wctxt, tx_count); 3397 } 3398 3399 static void serial8250_console_fifo_write(struct uart_8250_port *up, 3400 struct nbcon_write_context *wctxt) 3401 { 3402 __serial8250_console_fifo_write(up, wctxt, wctxt->outbuf, wctxt->len); 3403 } 3404 3405 static void __serial8250_console_byte_write(struct uart_8250_port *up, 3406 struct nbcon_write_context *wctxt, 3407 const char *s, unsigned int count) 3408 { 3409 struct uart_port *port = &up->port; 3410 const char *end = s + count; 3411 3412 /* 3413 * Write out the message. If a handover or takeover occurs, writing 3414 * must be aborted since the string data is no longer valid. 3415 */ 3416 while (s != end) { 3417 if (!nbcon_enter_unsafe(wctxt)) 3418 return; 3419 3420 uart_console_write(port, s++, 1, serial8250_console_wait_putchar); 3421 3422 nbcon_exit_unsafe(wctxt); 3423 } 3424 } 3425 3426 static void serial8250_console_byte_write(struct uart_8250_port *up, 3427 struct nbcon_write_context *wctxt) 3428 { 3429 __serial8250_console_byte_write(up, wctxt, wctxt->outbuf, wctxt->len); 3430 } 3431 3432 /* 3433 * Print the console line using the appropriate variant. If ownership is lost 3434 * at any time during printing, the printing is aborted. 3435 */ 3436 static void __serial8250_console_write(struct uart_8250_port *up, 3437 struct nbcon_write_context *wctxt, 3438 bool use_fifo) 3439 { 3440 /* 3441 * If the console printer did not fully output the previous line, it 3442 * must have been handed or taken over. Insert a newline in order to 3443 * maintain clean output. 3444 * 3445 * Braille consoles are an exception. The serial port is not used 3446 * for printk(). The driver is supposed to write exactly what it gets. 3447 */ 3448 if (unlikely(!up->console_line_ended && !nbcon_write_context_is_braille(wctxt))) { 3449 if (use_fifo) 3450 __serial8250_console_fifo_write(up, wctxt, "\n", 1); 3451 else 3452 __serial8250_console_byte_write(up, wctxt, "\n", 1); 3453 } 3454 3455 if (use_fifo) 3456 serial8250_console_fifo_write(up, wctxt); 3457 else 3458 serial8250_console_byte_write(up, wctxt); 3459 } 3460 3461 /* 3462 * Print a string to the serial port trying not to disturb 3463 * any possible real use of the port... 3464 */ 3465 void serial8250_console_write(struct uart_8250_port *up, 3466 struct nbcon_write_context *wctxt, 3467 bool is_atomic) 3468 { 3469 struct uart_8250_em485 *em485 = up->em485; 3470 struct uart_port *port = &up->port; 3471 unsigned int ier; 3472 bool use_fifo; 3473 3474 if (!nbcon_enter_unsafe(wctxt)) 3475 return; 3476 3477 /* 3478 * First, save the IER, then disable the interrupts. The special 3479 * variant to clear the IER is used because emergency and panic 3480 * printing is synchronized only by nbcon ownership without 3481 * holding the port lock. 3482 */ 3483 ier = serial_port_in(port, UART_IER); 3484 __serial8250_clear_IER(up); 3485 3486 /* check scratch reg to see if port powered off during system sleep */ 3487 if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) { 3488 serial8250_console_restore(up); 3489 up->canary = 0; 3490 } 3491 3492 if (em485) { 3493 if (em485->tx_stopped) 3494 up->rs485_start_tx(up, false); 3495 mdelay(port->rs485.delay_rts_before_send); 3496 } 3497 3498 use_fifo = (up->capabilities & UART_CAP_FIFO) && 3499 /* 3500 * BCM283x requires to check the fifo 3501 * after each byte. 3502 */ 3503 !(up->capabilities & UART_CAP_MINI) && 3504 /* 3505 * tx_loadsz contains the transmit fifo size 3506 */ 3507 up->tx_loadsz > 1 && 3508 (up->fcr & UART_FCR_ENABLE_FIFO) && 3509 port->state && 3510 test_bit(TTY_PORT_INITIALIZED, &port->state->port.iflags) && 3511 /* 3512 * After we put a data in the fifo, the controller will send 3513 * it regardless of the CTS state. Therefore, only use fifo 3514 * if we don't use control flow. 3515 */ 3516 !uart_console_hwflow_active(&up->port); 3517 3518 nbcon_exit_unsafe(wctxt); 3519 3520 __serial8250_console_write(up, wctxt, use_fifo); 3521 3522 /* 3523 * Re-enter an unsafe section in order to perform final actions 3524 * (such as re-enabling interrupts). If ownership was lost, this 3525 * context must reacquire ownership. 3526 */ 3527 while (!nbcon_enter_unsafe(wctxt)) 3528 nbcon_reacquire_nobuf(wctxt); 3529 3530 /* 3531 * Finally, wait for transmitter to become empty 3532 * and restore the IER 3533 */ 3534 serial8250_wait_for_xmitr(up, UART_LSR_BOTH_EMPTY); 3535 3536 if (em485) { 3537 mdelay(port->rs485.delay_rts_after_send); 3538 3539 /* Toggle unsafe after possibly long delay */ 3540 nbcon_exit_unsafe(wctxt); 3541 while (!nbcon_enter_unsafe(wctxt)) 3542 nbcon_reacquire_nobuf(wctxt); 3543 3544 if (em485->tx_stopped) 3545 up->rs485_stop_tx(up, false); 3546 } 3547 3548 /* Toggle unsafe after possibly long delay */ 3549 nbcon_exit_unsafe(wctxt); 3550 while (!nbcon_enter_unsafe(wctxt)) 3551 nbcon_reacquire_nobuf(wctxt); 3552 3553 serial_port_out(port, UART_IER, ier); 3554 3555 /* 3556 * The receive handling will happen properly because the 3557 * receive ready bit will still be set; it is not cleared 3558 * on read. However, modem control will not, we must 3559 * call it if we have saved something in the saved flags 3560 * while processing with interrupts off. 3561 */ 3562 if (up->msr_saved_flags) { 3563 if (is_atomic) { 3564 /* 3565 * For atomic, MSR handling must be deferred to 3566 * irq_work because this may be a context that does 3567 * not permit waking up tasks. 3568 * 3569 * But no irq_work may be queued when suspending. 3570 * In that case, the MSR handling will occur during 3571 * resume in serial8250_resume_port(). 3572 */ 3573 if (up->console_msr_work_allow) 3574 irq_work_queue(&up->console_msr_work); 3575 } else { 3576 serial8250_modem_status(up); 3577 } 3578 } 3579 3580 nbcon_exit_unsafe(wctxt); 3581 } 3582 3583 static unsigned int probe_baud(struct uart_port *port) 3584 { 3585 unsigned char lcr, dll, dlm; 3586 unsigned int quot; 3587 3588 lcr = serial_port_in(port, UART_LCR); 3589 serial_port_out(port, UART_LCR, lcr | UART_LCR_DLAB); 3590 dll = serial_port_in(port, UART_DLL); 3591 dlm = serial_port_in(port, UART_DLM); 3592 serial_port_out(port, UART_LCR, lcr); 3593 3594 quot = (dlm << 8) | dll; 3595 return (port->uartclk / 16) / quot; 3596 } 3597 3598 /* 3599 * irq_work handler to perform modem control during console output. 3600 * Only triggered via ->write_atomic() callback because it may be 3601 * in a scheduler or NMI context, unable to wake tasks. 3602 */ 3603 static void console_msr_handler(struct irq_work *iwp) 3604 { 3605 struct uart_8250_port *up = container_of(iwp, struct uart_8250_port, console_msr_work); 3606 struct uart_port *port = &up->port; 3607 3608 guard(uart_port_lock)(port); 3609 3610 serial8250_modem_status(up); 3611 } 3612 3613 int serial8250_console_setup(struct uart_port *port, char *options, bool probe) 3614 { 3615 struct uart_8250_port *up = up_to_u8250p(port); 3616 int baud = 9600; 3617 int bits = 8; 3618 int parity = 'n'; 3619 int flow = 'n'; 3620 int ret; 3621 3622 if (!port->iobase && !port->membase) 3623 return -ENODEV; 3624 3625 up->console_line_ended = true; 3626 up->console_msr_work_allow = true; 3627 up->console_msr_work = IRQ_WORK_INIT_LAZY(console_msr_handler); 3628 3629 if (options) 3630 uart_parse_options(options, &baud, &parity, &bits, &flow); 3631 else if (probe) 3632 baud = probe_baud(port); 3633 3634 ret = uart_set_options(port, port->cons, baud, parity, bits, flow); 3635 if (ret) 3636 return ret; 3637 3638 /* Track user-specified console flow control. */ 3639 uart_set_cons_flow_enabled(port, flow == 'r'); 3640 3641 if (port->dev) 3642 pm_runtime_get_sync(port->dev); 3643 3644 return 0; 3645 } 3646 3647 int serial8250_console_exit(struct uart_port *port) 3648 { 3649 struct uart_8250_port *up = up_to_u8250p(port); 3650 3651 irq_work_sync(&up->console_msr_work); 3652 3653 if (port->dev) 3654 pm_runtime_put_sync(port->dev); 3655 3656 return 0; 3657 } 3658 3659 #endif /* CONFIG_SERIAL_8250_CONSOLE */ 3660 3661 MODULE_DESCRIPTION("Base port operations for 8250/16550-type serial ports"); 3662 MODULE_LICENSE("GPL"); 3663