1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Serial Port driver for Aspeed VUART device 4 * 5 * Copyright (C) 2016 Jeremy Kerr <jk@ozlabs.org>, IBM Corp. 6 * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>, IBM Corp. 7 */ 8 #include <linux/device.h> 9 #include <linux/module.h> 10 #include <linux/of_address.h> 11 #include <linux/of_irq.h> 12 #include <linux/of_platform.h> 13 #include <linux/regmap.h> 14 #include <linux/mfd/syscon.h> 15 #include <linux/tty.h> 16 #include <linux/tty_flip.h> 17 #include <linux/clk.h> 18 19 #include "8250.h" 20 21 #define ASPEED_VUART_GCRA 0x20 22 #define ASPEED_VUART_GCRA_VUART_EN BIT(0) 23 #define ASPEED_VUART_GCRA_HOST_SIRQ_POLARITY BIT(1) 24 #define ASPEED_VUART_GCRA_DISABLE_HOST_TX_DISCARD BIT(5) 25 #define ASPEED_VUART_GCRB 0x24 26 #define ASPEED_VUART_GCRB_HOST_SIRQ_MASK GENMASK(7, 4) 27 #define ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT 4 28 #define ASPEED_VUART_ADDRL 0x28 29 #define ASPEED_VUART_ADDRH 0x2c 30 31 #define ASPEED_VUART_DEFAULT_LPC_ADDR 0x3f8 32 #define ASPEED_VUART_DEFAULT_SIRQ 4 33 #define ASPEED_VUART_DEFAULT_SIRQ_POLARITY IRQ_TYPE_LEVEL_LOW 34 35 struct aspeed_vuart { 36 struct device *dev; 37 void __iomem *regs; 38 struct clk *clk; 39 int line; 40 struct timer_list unthrottle_timer; 41 struct uart_8250_port *port; 42 }; 43 44 /* 45 * If we fill the tty flip buffers, we throttle the data ready interrupt 46 * to prevent dropped characters. This timeout defines how long we wait 47 * to (conditionally, depending on buffer state) unthrottle. 48 */ 49 static const int unthrottle_timeout = HZ/10; 50 51 /* 52 * The VUART is basically two UART 'front ends' connected by their FIFO 53 * (no actual serial line in between). One is on the BMC side (management 54 * controller) and one is on the host CPU side. 55 * 56 * It allows the BMC to provide to the host a "UART" that pipes into 57 * the BMC itself and can then be turned by the BMC into a network console 58 * of some sort for example. 59 * 60 * This driver is for the BMC side. The sysfs files allow the BMC 61 * userspace which owns the system configuration policy, to specify 62 * at what IO port and interrupt number the host side will appear 63 * to the host on the Host <-> BMC LPC bus. It could be different on a 64 * different system (though most of them use 3f8/4). 65 */ 66 67 static ssize_t lpc_address_show(struct device *dev, 68 struct device_attribute *attr, char *buf) 69 { 70 struct aspeed_vuart *vuart = dev_get_drvdata(dev); 71 u16 addr; 72 73 addr = (readb(vuart->regs + ASPEED_VUART_ADDRH) << 8) | 74 (readb(vuart->regs + ASPEED_VUART_ADDRL)); 75 76 return snprintf(buf, PAGE_SIZE - 1, "0x%x\n", addr); 77 } 78 79 static int aspeed_vuart_set_lpc_address(struct aspeed_vuart *vuart, u32 addr) 80 { 81 if (addr > U16_MAX) 82 return -EINVAL; 83 84 writeb(addr >> 8, vuart->regs + ASPEED_VUART_ADDRH); 85 writeb(addr >> 0, vuart->regs + ASPEED_VUART_ADDRL); 86 87 return 0; 88 } 89 90 static ssize_t lpc_address_store(struct device *dev, 91 struct device_attribute *attr, 92 const char *buf, size_t count) 93 { 94 struct aspeed_vuart *vuart = dev_get_drvdata(dev); 95 u32 val; 96 int err; 97 98 err = kstrtou32(buf, 0, &val); 99 if (err) 100 return err; 101 102 err = aspeed_vuart_set_lpc_address(vuart, val); 103 return err ? : count; 104 } 105 106 static DEVICE_ATTR_RW(lpc_address); 107 108 static ssize_t sirq_show(struct device *dev, 109 struct device_attribute *attr, char *buf) 110 { 111 struct aspeed_vuart *vuart = dev_get_drvdata(dev); 112 u8 reg; 113 114 reg = readb(vuart->regs + ASPEED_VUART_GCRB); 115 reg &= ASPEED_VUART_GCRB_HOST_SIRQ_MASK; 116 reg >>= ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT; 117 118 return snprintf(buf, PAGE_SIZE - 1, "%u\n", reg); 119 } 120 121 static int aspeed_vuart_set_sirq(struct aspeed_vuart *vuart, u32 sirq) 122 { 123 u8 reg; 124 125 if (sirq > (ASPEED_VUART_GCRB_HOST_SIRQ_MASK >> ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT)) 126 return -EINVAL; 127 128 sirq <<= ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT; 129 sirq &= ASPEED_VUART_GCRB_HOST_SIRQ_MASK; 130 131 reg = readb(vuart->regs + ASPEED_VUART_GCRB); 132 reg &= ~ASPEED_VUART_GCRB_HOST_SIRQ_MASK; 133 reg |= sirq; 134 writeb(reg, vuart->regs + ASPEED_VUART_GCRB); 135 136 return 0; 137 } 138 139 static ssize_t sirq_store(struct device *dev, struct device_attribute *attr, 140 const char *buf, size_t count) 141 { 142 struct aspeed_vuart *vuart = dev_get_drvdata(dev); 143 unsigned long val; 144 int err; 145 146 err = kstrtoul(buf, 0, &val); 147 if (err) 148 return err; 149 150 err = aspeed_vuart_set_sirq(vuart, val); 151 return err ? : count; 152 } 153 154 static DEVICE_ATTR_RW(sirq); 155 156 static ssize_t sirq_polarity_show(struct device *dev, 157 struct device_attribute *attr, char *buf) 158 { 159 struct aspeed_vuart *vuart = dev_get_drvdata(dev); 160 u8 reg; 161 162 reg = readb(vuart->regs + ASPEED_VUART_GCRA); 163 reg &= ASPEED_VUART_GCRA_HOST_SIRQ_POLARITY; 164 165 return snprintf(buf, PAGE_SIZE - 1, "%u\n", reg ? 1 : 0); 166 } 167 168 static void aspeed_vuart_set_sirq_polarity(struct aspeed_vuart *vuart, 169 bool polarity) 170 { 171 u8 reg = readb(vuart->regs + ASPEED_VUART_GCRA); 172 173 if (polarity) 174 reg |= ASPEED_VUART_GCRA_HOST_SIRQ_POLARITY; 175 else 176 reg &= ~ASPEED_VUART_GCRA_HOST_SIRQ_POLARITY; 177 178 writeb(reg, vuart->regs + ASPEED_VUART_GCRA); 179 } 180 181 static ssize_t sirq_polarity_store(struct device *dev, 182 struct device_attribute *attr, 183 const char *buf, size_t count) 184 { 185 struct aspeed_vuart *vuart = dev_get_drvdata(dev); 186 unsigned long val; 187 int err; 188 189 err = kstrtoul(buf, 0, &val); 190 if (err) 191 return err; 192 193 aspeed_vuart_set_sirq_polarity(vuart, val != 0); 194 195 return count; 196 } 197 198 static DEVICE_ATTR_RW(sirq_polarity); 199 200 static struct attribute *aspeed_vuart_attrs[] = { 201 &dev_attr_sirq.attr, 202 &dev_attr_sirq_polarity.attr, 203 &dev_attr_lpc_address.attr, 204 NULL, 205 }; 206 207 static const struct attribute_group aspeed_vuart_attr_group = { 208 .attrs = aspeed_vuart_attrs, 209 }; 210 211 static void aspeed_vuart_set_enabled(struct aspeed_vuart *vuart, bool enabled) 212 { 213 u8 reg = readb(vuart->regs + ASPEED_VUART_GCRA); 214 215 if (enabled) 216 reg |= ASPEED_VUART_GCRA_VUART_EN; 217 else 218 reg &= ~ASPEED_VUART_GCRA_VUART_EN; 219 220 writeb(reg, vuart->regs + ASPEED_VUART_GCRA); 221 } 222 223 static void aspeed_vuart_set_host_tx_discard(struct aspeed_vuart *vuart, 224 bool discard) 225 { 226 u8 reg; 227 228 reg = readb(vuart->regs + ASPEED_VUART_GCRA); 229 230 /* If the DISABLE_HOST_TX_DISCARD bit is set, discard is disabled */ 231 if (!discard) 232 reg |= ASPEED_VUART_GCRA_DISABLE_HOST_TX_DISCARD; 233 else 234 reg &= ~ASPEED_VUART_GCRA_DISABLE_HOST_TX_DISCARD; 235 236 writeb(reg, vuart->regs + ASPEED_VUART_GCRA); 237 } 238 239 static int aspeed_vuart_startup(struct uart_port *uart_port) 240 { 241 struct uart_8250_port *uart_8250_port = up_to_u8250p(uart_port); 242 struct aspeed_vuart *vuart = uart_8250_port->port.private_data; 243 int rc; 244 245 rc = serial8250_do_startup(uart_port); 246 if (rc) 247 return rc; 248 249 aspeed_vuart_set_host_tx_discard(vuart, false); 250 251 return 0; 252 } 253 254 static void aspeed_vuart_shutdown(struct uart_port *uart_port) 255 { 256 struct uart_8250_port *uart_8250_port = up_to_u8250p(uart_port); 257 struct aspeed_vuart *vuart = uart_8250_port->port.private_data; 258 259 aspeed_vuart_set_host_tx_discard(vuart, true); 260 261 serial8250_do_shutdown(uart_port); 262 } 263 264 static void __aspeed_vuart_set_throttle(struct uart_8250_port *up, 265 bool throttle) 266 { 267 unsigned char irqs = UART_IER_RLSI | UART_IER_RDI; 268 269 up->ier &= ~irqs; 270 if (!throttle) 271 up->ier |= irqs; 272 serial_out(up, UART_IER, up->ier); 273 } 274 static void aspeed_vuart_set_throttle(struct uart_port *port, bool throttle) 275 { 276 struct uart_8250_port *up = up_to_u8250p(port); 277 unsigned long flags; 278 279 spin_lock_irqsave(&port->lock, flags); 280 __aspeed_vuart_set_throttle(up, throttle); 281 spin_unlock_irqrestore(&port->lock, flags); 282 } 283 284 static void aspeed_vuart_throttle(struct uart_port *port) 285 { 286 aspeed_vuart_set_throttle(port, true); 287 } 288 289 static void aspeed_vuart_unthrottle(struct uart_port *port) 290 { 291 aspeed_vuart_set_throttle(port, false); 292 } 293 294 static void aspeed_vuart_unthrottle_exp(struct timer_list *timer) 295 { 296 struct aspeed_vuart *vuart = from_timer(vuart, timer, unthrottle_timer); 297 struct uart_8250_port *up = vuart->port; 298 299 if (!tty_buffer_space_avail(&up->port.state->port)) { 300 mod_timer(&vuart->unthrottle_timer, 301 jiffies + unthrottle_timeout); 302 return; 303 } 304 305 aspeed_vuart_unthrottle(&up->port); 306 } 307 308 /* 309 * Custom interrupt handler to manage finer-grained flow control. Although we 310 * have throttle/unthrottle callbacks, we've seen that the VUART device can 311 * deliver characters faster than the ldisc has a chance to check buffer space 312 * against the throttle threshold. This results in dropped characters before 313 * the throttle. 314 * 315 * We do this by checking for flip buffer space before RX. If we have no space, 316 * throttle now and schedule an unthrottle for later, once the ldisc has had 317 * a chance to drain the buffers. 318 */ 319 static int aspeed_vuart_handle_irq(struct uart_port *port) 320 { 321 struct uart_8250_port *up = up_to_u8250p(port); 322 unsigned int iir, lsr; 323 int space, count; 324 325 iir = serial_port_in(port, UART_IIR); 326 327 if (iir & UART_IIR_NO_INT) 328 return 0; 329 330 spin_lock(&port->lock); 331 332 lsr = serial_port_in(port, UART_LSR); 333 334 if (lsr & (UART_LSR_DR | UART_LSR_BI)) { 335 space = tty_buffer_space_avail(&port->state->port); 336 337 if (!space) { 338 /* throttle and schedule an unthrottle later */ 339 struct aspeed_vuart *vuart = port->private_data; 340 __aspeed_vuart_set_throttle(up, true); 341 342 if (!timer_pending(&vuart->unthrottle_timer)) { 343 vuart->port = up; 344 mod_timer(&vuart->unthrottle_timer, 345 jiffies + unthrottle_timeout); 346 } 347 348 } else { 349 count = min(space, 256); 350 351 do { 352 serial8250_read_char(up, lsr); 353 lsr = serial_in(up, UART_LSR); 354 if (--count == 0) 355 break; 356 } while (lsr & (UART_LSR_DR | UART_LSR_BI)); 357 358 tty_flip_buffer_push(&port->state->port); 359 } 360 } 361 362 serial8250_modem_status(up); 363 if (lsr & UART_LSR_THRE) 364 serial8250_tx_chars(up); 365 366 uart_unlock_and_check_sysrq(port); 367 368 return 1; 369 } 370 371 static void aspeed_vuart_auto_configure_sirq_polarity( 372 struct aspeed_vuart *vuart, struct device_node *syscon_np, 373 u32 reg_offset, u32 reg_mask) 374 { 375 struct regmap *regmap; 376 u32 value; 377 378 regmap = syscon_node_to_regmap(syscon_np); 379 if (IS_ERR(regmap)) { 380 dev_warn(vuart->dev, 381 "could not get regmap for aspeed,sirq-polarity-sense\n"); 382 return; 383 } 384 if (regmap_read(regmap, reg_offset, &value)) { 385 dev_warn(vuart->dev, "could not read hw strap table\n"); 386 return; 387 } 388 389 aspeed_vuart_set_sirq_polarity(vuart, (value & reg_mask) == 0); 390 } 391 392 static int aspeed_vuart_map_irq_polarity(u32 dt) 393 { 394 switch (dt) { 395 case IRQ_TYPE_LEVEL_LOW: 396 return 0; 397 case IRQ_TYPE_LEVEL_HIGH: 398 return 1; 399 default: 400 return -EINVAL; 401 } 402 } 403 404 static int aspeed_vuart_probe(struct platform_device *pdev) 405 { 406 struct of_phandle_args sirq_polarity_sense_args; 407 struct uart_8250_port port; 408 struct aspeed_vuart *vuart; 409 struct device_node *np; 410 struct resource *res; 411 u32 clk, prop, sirq[2]; 412 int rc, sirq_polarity; 413 414 np = pdev->dev.of_node; 415 416 vuart = devm_kzalloc(&pdev->dev, sizeof(*vuart), GFP_KERNEL); 417 if (!vuart) 418 return -ENOMEM; 419 420 vuart->dev = &pdev->dev; 421 timer_setup(&vuart->unthrottle_timer, aspeed_vuart_unthrottle_exp, 0); 422 423 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 424 vuart->regs = devm_ioremap_resource(&pdev->dev, res); 425 if (IS_ERR(vuart->regs)) 426 return PTR_ERR(vuart->regs); 427 428 memset(&port, 0, sizeof(port)); 429 port.port.private_data = vuart; 430 port.port.membase = vuart->regs; 431 port.port.mapbase = res->start; 432 port.port.mapsize = resource_size(res); 433 port.port.startup = aspeed_vuart_startup; 434 port.port.shutdown = aspeed_vuart_shutdown; 435 port.port.throttle = aspeed_vuart_throttle; 436 port.port.unthrottle = aspeed_vuart_unthrottle; 437 port.port.status = UPSTAT_SYNC_FIFO; 438 port.port.dev = &pdev->dev; 439 port.port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE); 440 441 rc = sysfs_create_group(&vuart->dev->kobj, &aspeed_vuart_attr_group); 442 if (rc < 0) 443 return rc; 444 445 if (of_property_read_u32(np, "clock-frequency", &clk)) { 446 vuart->clk = devm_clk_get(&pdev->dev, NULL); 447 if (IS_ERR(vuart->clk)) { 448 dev_warn(&pdev->dev, 449 "clk or clock-frequency not defined\n"); 450 rc = PTR_ERR(vuart->clk); 451 goto err_sysfs_remove; 452 } 453 454 rc = clk_prepare_enable(vuart->clk); 455 if (rc < 0) 456 goto err_sysfs_remove; 457 458 clk = clk_get_rate(vuart->clk); 459 } 460 461 /* If current-speed was set, then try not to change it. */ 462 if (of_property_read_u32(np, "current-speed", &prop) == 0) 463 port.port.custom_divisor = clk / (16 * prop); 464 465 /* Check for shifted address mapping */ 466 if (of_property_read_u32(np, "reg-offset", &prop) == 0) 467 port.port.mapbase += prop; 468 469 /* Check for registers offset within the devices address range */ 470 if (of_property_read_u32(np, "reg-shift", &prop) == 0) 471 port.port.regshift = prop; 472 473 /* Check for fifo size */ 474 if (of_property_read_u32(np, "fifo-size", &prop) == 0) 475 port.port.fifosize = prop; 476 477 /* Check for a fixed line number */ 478 rc = of_alias_get_id(np, "serial"); 479 if (rc >= 0) 480 port.port.line = rc; 481 482 port.port.irq = irq_of_parse_and_map(np, 0); 483 port.port.handle_irq = aspeed_vuart_handle_irq; 484 port.port.iotype = UPIO_MEM; 485 port.port.type = PORT_16550A; 486 port.port.uartclk = clk; 487 port.port.flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF 488 | UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_NO_THRE_TEST; 489 490 if (of_property_read_bool(np, "no-loopback-test")) 491 port.port.flags |= UPF_SKIP_TEST; 492 493 if (port.port.fifosize) 494 port.capabilities = UART_CAP_FIFO; 495 496 if (of_property_read_bool(np, "auto-flow-control")) 497 port.capabilities |= UART_CAP_AFE; 498 499 rc = serial8250_register_8250_port(&port); 500 if (rc < 0) 501 goto err_clk_disable; 502 503 vuart->line = rc; 504 505 rc = of_parse_phandle_with_fixed_args( 506 np, "aspeed,sirq-polarity-sense", 2, 0, 507 &sirq_polarity_sense_args); 508 if (rc < 0) { 509 dev_dbg(&pdev->dev, 510 "aspeed,sirq-polarity-sense property not found\n"); 511 } else { 512 aspeed_vuart_auto_configure_sirq_polarity( 513 vuart, sirq_polarity_sense_args.np, 514 sirq_polarity_sense_args.args[0], 515 BIT(sirq_polarity_sense_args.args[1])); 516 of_node_put(sirq_polarity_sense_args.np); 517 } 518 519 rc = of_property_read_u32(np, "aspeed,lpc-io-reg", &prop); 520 if (rc < 0) 521 prop = ASPEED_VUART_DEFAULT_LPC_ADDR; 522 523 rc = aspeed_vuart_set_lpc_address(vuart, prop); 524 if (rc < 0) { 525 dev_err(&pdev->dev, "invalid value in aspeed,lpc-io-reg property\n"); 526 goto err_clk_disable; 527 } 528 529 rc = of_property_read_u32_array(np, "aspeed,lpc-interrupts", sirq, 2); 530 if (rc < 0) { 531 sirq[0] = ASPEED_VUART_DEFAULT_SIRQ; 532 sirq[1] = ASPEED_VUART_DEFAULT_SIRQ_POLARITY; 533 } 534 535 rc = aspeed_vuart_set_sirq(vuart, sirq[0]); 536 if (rc < 0) { 537 dev_err(&pdev->dev, "invalid sirq number in aspeed,lpc-interrupts property\n"); 538 goto err_clk_disable; 539 } 540 541 sirq_polarity = aspeed_vuart_map_irq_polarity(sirq[1]); 542 if (sirq_polarity < 0) { 543 dev_err(&pdev->dev, "invalid sirq polarity in aspeed,lpc-interrupts property\n"); 544 rc = sirq_polarity; 545 goto err_clk_disable; 546 } 547 548 aspeed_vuart_set_sirq_polarity(vuart, sirq_polarity); 549 550 aspeed_vuart_set_enabled(vuart, true); 551 aspeed_vuart_set_host_tx_discard(vuart, true); 552 platform_set_drvdata(pdev, vuart); 553 554 return 0; 555 556 err_clk_disable: 557 clk_disable_unprepare(vuart->clk); 558 irq_dispose_mapping(port.port.irq); 559 err_sysfs_remove: 560 sysfs_remove_group(&vuart->dev->kobj, &aspeed_vuart_attr_group); 561 return rc; 562 } 563 564 static int aspeed_vuart_remove(struct platform_device *pdev) 565 { 566 struct aspeed_vuart *vuart = platform_get_drvdata(pdev); 567 568 del_timer_sync(&vuart->unthrottle_timer); 569 aspeed_vuart_set_enabled(vuart, false); 570 serial8250_unregister_port(vuart->line); 571 sysfs_remove_group(&vuart->dev->kobj, &aspeed_vuart_attr_group); 572 clk_disable_unprepare(vuart->clk); 573 574 return 0; 575 } 576 577 static const struct of_device_id aspeed_vuart_table[] = { 578 { .compatible = "aspeed,ast2400-vuart" }, 579 { .compatible = "aspeed,ast2500-vuart" }, 580 { }, 581 }; 582 583 static struct platform_driver aspeed_vuart_driver = { 584 .driver = { 585 .name = "aspeed-vuart", 586 .of_match_table = aspeed_vuart_table, 587 }, 588 .probe = aspeed_vuart_probe, 589 .remove = aspeed_vuart_remove, 590 }; 591 592 module_platform_driver(aspeed_vuart_driver); 593 594 MODULE_AUTHOR("Jeremy Kerr <jk@ozlabs.org>"); 595 MODULE_LICENSE("GPL"); 596 MODULE_DESCRIPTION("Driver for Aspeed VUART device"); 597