1 /* 2 * Synopsys DesignWare 8250 driver. 3 * 4 * Copyright 2011 Picochip, Jamie Iles. 5 * Copyright 2013 Intel Corporation 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * The Synopsys DesignWare 8250 has an extra feature whereby it detects if the 13 * LCR is written whilst busy. If it is, then a busy detect interrupt is 14 * raised, the LCR needs to be rewritten and the uart status register read. 15 */ 16 #include <linux/device.h> 17 #include <linux/io.h> 18 #include <linux/module.h> 19 #include <linux/serial_8250.h> 20 #include <linux/serial_reg.h> 21 #include <linux/of.h> 22 #include <linux/of_irq.h> 23 #include <linux/of_platform.h> 24 #include <linux/platform_device.h> 25 #include <linux/slab.h> 26 #include <linux/acpi.h> 27 #include <linux/clk.h> 28 #include <linux/reset.h> 29 #include <linux/pm_runtime.h> 30 31 #include <asm/byteorder.h> 32 33 #include "8250.h" 34 35 /* Offsets for the DesignWare specific registers */ 36 #define DW_UART_USR 0x1f /* UART Status Register */ 37 #define DW_UART_CPR 0xf4 /* Component Parameter Register */ 38 #define DW_UART_UCV 0xf8 /* UART Component Version */ 39 40 /* Component Parameter Register bits */ 41 #define DW_UART_CPR_ABP_DATA_WIDTH (3 << 0) 42 #define DW_UART_CPR_AFCE_MODE (1 << 4) 43 #define DW_UART_CPR_THRE_MODE (1 << 5) 44 #define DW_UART_CPR_SIR_MODE (1 << 6) 45 #define DW_UART_CPR_SIR_LP_MODE (1 << 7) 46 #define DW_UART_CPR_ADDITIONAL_FEATURES (1 << 8) 47 #define DW_UART_CPR_FIFO_ACCESS (1 << 9) 48 #define DW_UART_CPR_FIFO_STAT (1 << 10) 49 #define DW_UART_CPR_SHADOW (1 << 11) 50 #define DW_UART_CPR_ENCODED_PARMS (1 << 12) 51 #define DW_UART_CPR_DMA_EXTRA (1 << 13) 52 #define DW_UART_CPR_FIFO_MODE (0xff << 16) 53 /* Helper for fifo size calculation */ 54 #define DW_UART_CPR_FIFO_SIZE(a) (((a >> 16) & 0xff) * 16) 55 56 57 struct dw8250_data { 58 u8 usr_reg; 59 int line; 60 int msr_mask_on; 61 int msr_mask_off; 62 struct clk *clk; 63 struct clk *pclk; 64 struct reset_control *rst; 65 struct uart_8250_dma dma; 66 }; 67 68 #define BYT_PRV_CLK 0x800 69 #define BYT_PRV_CLK_EN (1 << 0) 70 #define BYT_PRV_CLK_M_VAL_SHIFT 1 71 #define BYT_PRV_CLK_N_VAL_SHIFT 16 72 #define BYT_PRV_CLK_UPDATE (1 << 31) 73 74 static inline int dw8250_modify_msr(struct uart_port *p, int offset, int value) 75 { 76 struct dw8250_data *d = p->private_data; 77 78 /* Override any modem control signals if needed */ 79 if (offset == UART_MSR) { 80 value |= d->msr_mask_on; 81 value &= ~d->msr_mask_off; 82 } 83 84 return value; 85 } 86 87 static void dw8250_force_idle(struct uart_port *p) 88 { 89 struct uart_8250_port *up = up_to_u8250p(p); 90 91 serial8250_clear_and_reinit_fifos(up); 92 (void)p->serial_in(p, UART_RX); 93 } 94 95 static void dw8250_serial_out(struct uart_port *p, int offset, int value) 96 { 97 writeb(value, p->membase + (offset << p->regshift)); 98 99 /* Make sure LCR write wasn't ignored */ 100 if (offset == UART_LCR) { 101 int tries = 1000; 102 while (tries--) { 103 unsigned int lcr = p->serial_in(p, UART_LCR); 104 if ((value & ~UART_LCR_SPAR) == (lcr & ~UART_LCR_SPAR)) 105 return; 106 dw8250_force_idle(p); 107 writeb(value, p->membase + (UART_LCR << p->regshift)); 108 } 109 /* 110 * FIXME: this deadlocks if port->lock is already held 111 * dev_err(p->dev, "Couldn't set LCR to %d\n", value); 112 */ 113 } 114 } 115 116 static unsigned int dw8250_serial_in(struct uart_port *p, int offset) 117 { 118 unsigned int value = readb(p->membase + (offset << p->regshift)); 119 120 return dw8250_modify_msr(p, offset, value); 121 } 122 123 #ifdef CONFIG_64BIT 124 static unsigned int dw8250_serial_inq(struct uart_port *p, int offset) 125 { 126 unsigned int value; 127 128 value = (u8)__raw_readq(p->membase + (offset << p->regshift)); 129 130 return dw8250_modify_msr(p, offset, value); 131 } 132 133 static void dw8250_serial_outq(struct uart_port *p, int offset, int value) 134 { 135 value &= 0xff; 136 __raw_writeq(value, p->membase + (offset << p->regshift)); 137 /* Read back to ensure register write ordering. */ 138 __raw_readq(p->membase + (UART_LCR << p->regshift)); 139 140 /* Make sure LCR write wasn't ignored */ 141 if (offset == UART_LCR) { 142 int tries = 1000; 143 while (tries--) { 144 unsigned int lcr = p->serial_in(p, UART_LCR); 145 if ((value & ~UART_LCR_SPAR) == (lcr & ~UART_LCR_SPAR)) 146 return; 147 dw8250_force_idle(p); 148 __raw_writeq(value & 0xff, 149 p->membase + (UART_LCR << p->regshift)); 150 } 151 /* 152 * FIXME: this deadlocks if port->lock is already held 153 * dev_err(p->dev, "Couldn't set LCR to %d\n", value); 154 */ 155 } 156 } 157 #endif /* CONFIG_64BIT */ 158 159 static void dw8250_serial_out32(struct uart_port *p, int offset, int value) 160 { 161 writel(value, p->membase + (offset << p->regshift)); 162 163 /* Make sure LCR write wasn't ignored */ 164 if (offset == UART_LCR) { 165 int tries = 1000; 166 while (tries--) { 167 unsigned int lcr = p->serial_in(p, UART_LCR); 168 if ((value & ~UART_LCR_SPAR) == (lcr & ~UART_LCR_SPAR)) 169 return; 170 dw8250_force_idle(p); 171 writel(value, p->membase + (UART_LCR << p->regshift)); 172 } 173 /* 174 * FIXME: this deadlocks if port->lock is already held 175 * dev_err(p->dev, "Couldn't set LCR to %d\n", value); 176 */ 177 } 178 } 179 180 static unsigned int dw8250_serial_in32(struct uart_port *p, int offset) 181 { 182 unsigned int value = readl(p->membase + (offset << p->regshift)); 183 184 return dw8250_modify_msr(p, offset, value); 185 } 186 187 static int dw8250_handle_irq(struct uart_port *p) 188 { 189 struct dw8250_data *d = p->private_data; 190 unsigned int iir = p->serial_in(p, UART_IIR); 191 192 if (serial8250_handle_irq(p, iir)) { 193 return 1; 194 } else if ((iir & UART_IIR_BUSY) == UART_IIR_BUSY) { 195 /* Clear the USR */ 196 (void)p->serial_in(p, d->usr_reg); 197 198 return 1; 199 } 200 201 return 0; 202 } 203 204 static void 205 dw8250_do_pm(struct uart_port *port, unsigned int state, unsigned int old) 206 { 207 if (!state) 208 pm_runtime_get_sync(port->dev); 209 210 serial8250_do_pm(port, state, old); 211 212 if (state) 213 pm_runtime_put_sync_suspend(port->dev); 214 } 215 216 static void dw8250_set_termios(struct uart_port *p, struct ktermios *termios, 217 struct ktermios *old) 218 { 219 unsigned int baud = tty_termios_baud_rate(termios); 220 struct dw8250_data *d = p->private_data; 221 unsigned int rate; 222 int ret; 223 224 if (IS_ERR(d->clk) || !old) 225 goto out; 226 227 /* Not requesting clock rates below 1.8432Mhz */ 228 if (baud < 115200) 229 baud = 115200; 230 231 clk_disable_unprepare(d->clk); 232 rate = clk_round_rate(d->clk, baud * 16); 233 ret = clk_set_rate(d->clk, rate); 234 clk_prepare_enable(d->clk); 235 236 if (!ret) 237 p->uartclk = rate; 238 239 p->status &= ~UPSTAT_AUTOCTS; 240 if (termios->c_cflag & CRTSCTS) 241 p->status |= UPSTAT_AUTOCTS; 242 243 out: 244 serial8250_do_set_termios(p, termios, old); 245 } 246 247 static bool dw8250_dma_filter(struct dma_chan *chan, void *param) 248 { 249 return false; 250 } 251 252 static void dw8250_setup_port(struct uart_8250_port *up) 253 { 254 struct uart_port *p = &up->port; 255 u32 reg = readl(p->membase + DW_UART_UCV); 256 257 /* 258 * If the Component Version Register returns zero, we know that 259 * ADDITIONAL_FEATURES are not enabled. No need to go any further. 260 */ 261 if (!reg) 262 return; 263 264 dev_dbg_ratelimited(p->dev, "Designware UART version %c.%c%c\n", 265 (reg >> 24) & 0xff, (reg >> 16) & 0xff, (reg >> 8) & 0xff); 266 267 reg = readl(p->membase + DW_UART_CPR); 268 if (!reg) 269 return; 270 271 /* Select the type based on fifo */ 272 if (reg & DW_UART_CPR_FIFO_MODE) { 273 p->type = PORT_16550A; 274 p->flags |= UPF_FIXED_TYPE; 275 p->fifosize = DW_UART_CPR_FIFO_SIZE(reg); 276 up->tx_loadsz = p->fifosize; 277 up->capabilities = UART_CAP_FIFO; 278 } 279 280 if (reg & DW_UART_CPR_AFCE_MODE) 281 up->capabilities |= UART_CAP_AFE; 282 } 283 284 static int dw8250_probe_of(struct uart_port *p, 285 struct dw8250_data *data) 286 { 287 struct device_node *np = p->dev->of_node; 288 struct uart_8250_port *up = up_to_u8250p(p); 289 u32 val; 290 bool has_ucv = true; 291 int id; 292 293 #ifdef CONFIG_64BIT 294 if (of_device_is_compatible(np, "cavium,octeon-3860-uart")) { 295 p->serial_in = dw8250_serial_inq; 296 p->serial_out = dw8250_serial_outq; 297 p->flags = UPF_SKIP_TEST | UPF_SHARE_IRQ | UPF_FIXED_TYPE; 298 p->type = PORT_OCTEON; 299 data->usr_reg = 0x27; 300 has_ucv = false; 301 } else 302 #endif 303 if (!of_property_read_u32(np, "reg-io-width", &val)) { 304 switch (val) { 305 case 1: 306 break; 307 case 4: 308 p->iotype = UPIO_MEM32; 309 p->serial_in = dw8250_serial_in32; 310 p->serial_out = dw8250_serial_out32; 311 break; 312 default: 313 dev_err(p->dev, "unsupported reg-io-width (%u)\n", val); 314 return -EINVAL; 315 } 316 } 317 if (has_ucv) 318 dw8250_setup_port(up); 319 320 /* if we have a valid fifosize, try hooking up DMA here */ 321 if (p->fifosize) { 322 up->dma = &data->dma; 323 324 up->dma->rxconf.src_maxburst = p->fifosize / 4; 325 up->dma->txconf.dst_maxburst = p->fifosize / 4; 326 } 327 328 if (!of_property_read_u32(np, "reg-shift", &val)) 329 p->regshift = val; 330 331 /* get index of serial line, if found in DT aliases */ 332 id = of_alias_get_id(np, "serial"); 333 if (id >= 0) 334 p->line = id; 335 336 if (of_property_read_bool(np, "dcd-override")) { 337 /* Always report DCD as active */ 338 data->msr_mask_on |= UART_MSR_DCD; 339 data->msr_mask_off |= UART_MSR_DDCD; 340 } 341 342 if (of_property_read_bool(np, "dsr-override")) { 343 /* Always report DSR as active */ 344 data->msr_mask_on |= UART_MSR_DSR; 345 data->msr_mask_off |= UART_MSR_DDSR; 346 } 347 348 if (of_property_read_bool(np, "cts-override")) { 349 /* Always report CTS as active */ 350 data->msr_mask_on |= UART_MSR_CTS; 351 data->msr_mask_off |= UART_MSR_DCTS; 352 } 353 354 if (of_property_read_bool(np, "ri-override")) { 355 /* Always report Ring indicator as inactive */ 356 data->msr_mask_off |= UART_MSR_RI; 357 data->msr_mask_off |= UART_MSR_TERI; 358 } 359 360 return 0; 361 } 362 363 static bool dw8250_idma_filter(struct dma_chan *chan, void *param) 364 { 365 struct device *dev = param; 366 367 if (dev != chan->device->dev->parent) 368 return false; 369 370 return true; 371 } 372 373 static int dw8250_probe_acpi(struct uart_8250_port *up, 374 struct dw8250_data *data) 375 { 376 struct uart_port *p = &up->port; 377 378 dw8250_setup_port(up); 379 380 p->iotype = UPIO_MEM32; 381 p->serial_in = dw8250_serial_in32; 382 p->serial_out = dw8250_serial_out32; 383 p->regshift = 2; 384 385 /* Platforms with iDMA */ 386 if (platform_get_resource_byname(to_platform_device(up->port.dev), 387 IORESOURCE_MEM, "lpss_priv")) { 388 data->dma.rx_param = up->port.dev->parent; 389 data->dma.tx_param = up->port.dev->parent; 390 data->dma.fn = dw8250_idma_filter; 391 } 392 393 up->dma = &data->dma; 394 up->dma->rxconf.src_maxburst = p->fifosize / 4; 395 up->dma->txconf.dst_maxburst = p->fifosize / 4; 396 397 up->port.set_termios = dw8250_set_termios; 398 399 return 0; 400 } 401 402 static int dw8250_probe(struct platform_device *pdev) 403 { 404 struct uart_8250_port uart = {}; 405 struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); 406 int irq = platform_get_irq(pdev, 0); 407 struct dw8250_data *data; 408 int err; 409 410 if (!regs) { 411 dev_err(&pdev->dev, "no registers defined\n"); 412 return -EINVAL; 413 } 414 415 if (irq < 0) { 416 if (irq != -EPROBE_DEFER) 417 dev_err(&pdev->dev, "cannot get irq\n"); 418 return irq; 419 } 420 421 spin_lock_init(&uart.port.lock); 422 uart.port.mapbase = regs->start; 423 uart.port.irq = irq; 424 uart.port.handle_irq = dw8250_handle_irq; 425 uart.port.pm = dw8250_do_pm; 426 uart.port.type = PORT_8250; 427 uart.port.flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_FIXED_PORT; 428 uart.port.dev = &pdev->dev; 429 430 uart.port.membase = devm_ioremap(&pdev->dev, regs->start, 431 resource_size(regs)); 432 if (!uart.port.membase) 433 return -ENOMEM; 434 435 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); 436 if (!data) 437 return -ENOMEM; 438 439 data->usr_reg = DW_UART_USR; 440 441 /* Always ask for fixed clock rate from a property. */ 442 device_property_read_u32(&pdev->dev, "clock-frequency", 443 &uart.port.uartclk); 444 445 /* If there is separate baudclk, get the rate from it. */ 446 data->clk = devm_clk_get(&pdev->dev, "baudclk"); 447 if (IS_ERR(data->clk) && PTR_ERR(data->clk) != -EPROBE_DEFER) 448 data->clk = devm_clk_get(&pdev->dev, NULL); 449 if (IS_ERR(data->clk) && PTR_ERR(data->clk) == -EPROBE_DEFER) 450 return -EPROBE_DEFER; 451 if (!IS_ERR_OR_NULL(data->clk)) { 452 err = clk_prepare_enable(data->clk); 453 if (err) 454 dev_warn(&pdev->dev, "could not enable optional baudclk: %d\n", 455 err); 456 else 457 uart.port.uartclk = clk_get_rate(data->clk); 458 } 459 460 /* If no clock rate is defined, fail. */ 461 if (!uart.port.uartclk) { 462 dev_err(&pdev->dev, "clock rate not defined\n"); 463 return -EINVAL; 464 } 465 466 data->pclk = devm_clk_get(&pdev->dev, "apb_pclk"); 467 if (IS_ERR(data->clk) && PTR_ERR(data->clk) == -EPROBE_DEFER) { 468 err = -EPROBE_DEFER; 469 goto err_clk; 470 } 471 if (!IS_ERR(data->pclk)) { 472 err = clk_prepare_enable(data->pclk); 473 if (err) { 474 dev_err(&pdev->dev, "could not enable apb_pclk\n"); 475 goto err_clk; 476 } 477 } 478 479 data->rst = devm_reset_control_get_optional(&pdev->dev, NULL); 480 if (IS_ERR(data->rst) && PTR_ERR(data->rst) == -EPROBE_DEFER) { 481 err = -EPROBE_DEFER; 482 goto err_pclk; 483 } 484 if (!IS_ERR(data->rst)) 485 reset_control_deassert(data->rst); 486 487 data->dma.rx_param = data; 488 data->dma.tx_param = data; 489 data->dma.fn = dw8250_dma_filter; 490 491 uart.port.iotype = UPIO_MEM; 492 uart.port.serial_in = dw8250_serial_in; 493 uart.port.serial_out = dw8250_serial_out; 494 uart.port.private_data = data; 495 496 if (pdev->dev.of_node) { 497 err = dw8250_probe_of(&uart.port, data); 498 if (err) 499 goto err_reset; 500 } else if (ACPI_HANDLE(&pdev->dev)) { 501 err = dw8250_probe_acpi(&uart, data); 502 if (err) 503 goto err_reset; 504 } else { 505 err = -ENODEV; 506 goto err_reset; 507 } 508 509 data->line = serial8250_register_8250_port(&uart); 510 if (data->line < 0) { 511 err = data->line; 512 goto err_reset; 513 } 514 515 platform_set_drvdata(pdev, data); 516 517 pm_runtime_set_active(&pdev->dev); 518 pm_runtime_enable(&pdev->dev); 519 520 return 0; 521 522 err_reset: 523 if (!IS_ERR(data->rst)) 524 reset_control_assert(data->rst); 525 526 err_pclk: 527 if (!IS_ERR(data->pclk)) 528 clk_disable_unprepare(data->pclk); 529 530 err_clk: 531 if (!IS_ERR(data->clk)) 532 clk_disable_unprepare(data->clk); 533 534 return err; 535 } 536 537 static int dw8250_remove(struct platform_device *pdev) 538 { 539 struct dw8250_data *data = platform_get_drvdata(pdev); 540 541 pm_runtime_get_sync(&pdev->dev); 542 543 serial8250_unregister_port(data->line); 544 545 if (!IS_ERR(data->rst)) 546 reset_control_assert(data->rst); 547 548 if (!IS_ERR(data->pclk)) 549 clk_disable_unprepare(data->pclk); 550 551 if (!IS_ERR(data->clk)) 552 clk_disable_unprepare(data->clk); 553 554 pm_runtime_disable(&pdev->dev); 555 pm_runtime_put_noidle(&pdev->dev); 556 557 return 0; 558 } 559 560 #ifdef CONFIG_PM_SLEEP 561 static int dw8250_suspend(struct device *dev) 562 { 563 struct dw8250_data *data = dev_get_drvdata(dev); 564 565 serial8250_suspend_port(data->line); 566 567 return 0; 568 } 569 570 static int dw8250_resume(struct device *dev) 571 { 572 struct dw8250_data *data = dev_get_drvdata(dev); 573 574 serial8250_resume_port(data->line); 575 576 return 0; 577 } 578 #endif /* CONFIG_PM_SLEEP */ 579 580 #ifdef CONFIG_PM 581 static int dw8250_runtime_suspend(struct device *dev) 582 { 583 struct dw8250_data *data = dev_get_drvdata(dev); 584 585 if (!IS_ERR(data->clk)) 586 clk_disable_unprepare(data->clk); 587 588 if (!IS_ERR(data->pclk)) 589 clk_disable_unprepare(data->pclk); 590 591 return 0; 592 } 593 594 static int dw8250_runtime_resume(struct device *dev) 595 { 596 struct dw8250_data *data = dev_get_drvdata(dev); 597 598 if (!IS_ERR(data->pclk)) 599 clk_prepare_enable(data->pclk); 600 601 if (!IS_ERR(data->clk)) 602 clk_prepare_enable(data->clk); 603 604 return 0; 605 } 606 #endif 607 608 static const struct dev_pm_ops dw8250_pm_ops = { 609 SET_SYSTEM_SLEEP_PM_OPS(dw8250_suspend, dw8250_resume) 610 SET_RUNTIME_PM_OPS(dw8250_runtime_suspend, dw8250_runtime_resume, NULL) 611 }; 612 613 static const struct of_device_id dw8250_of_match[] = { 614 { .compatible = "snps,dw-apb-uart" }, 615 { .compatible = "cavium,octeon-3860-uart" }, 616 { /* Sentinel */ } 617 }; 618 MODULE_DEVICE_TABLE(of, dw8250_of_match); 619 620 static const struct acpi_device_id dw8250_acpi_match[] = { 621 { "INT33C4", 0 }, 622 { "INT33C5", 0 }, 623 { "INT3434", 0 }, 624 { "INT3435", 0 }, 625 { "80860F0A", 0 }, 626 { "8086228A", 0 }, 627 { "APMC0D08", 0}, 628 { "AMD0020", 0 }, 629 { }, 630 }; 631 MODULE_DEVICE_TABLE(acpi, dw8250_acpi_match); 632 633 static struct platform_driver dw8250_platform_driver = { 634 .driver = { 635 .name = "dw-apb-uart", 636 .pm = &dw8250_pm_ops, 637 .of_match_table = dw8250_of_match, 638 .acpi_match_table = ACPI_PTR(dw8250_acpi_match), 639 }, 640 .probe = dw8250_probe, 641 .remove = dw8250_remove, 642 }; 643 644 module_platform_driver(dw8250_platform_driver); 645 646 MODULE_AUTHOR("Jamie Iles"); 647 MODULE_LICENSE("GPL"); 648 MODULE_DESCRIPTION("Synopsys DesignWare 8250 serial port driver"); 649 MODULE_ALIAS("platform:dw-apb-uart"); 650