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