1 // SPDX-License-Identifier: GPL-2.0+
3 * Serial Port driver for Aspeed VUART device
5 * Copyright (C) 2016 Jeremy Kerr <jk@ozlabs.org>, IBM Corp.
6 * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>, IBM Corp.
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>
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
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
39 struct timer_list unthrottle_timer;
40 struct uart_8250_port *port;
44 * If we fill the tty flip buffers, we throttle the data ready interrupt
45 * to prevent dropped characters. This timeout defines how long we wait
46 * to (conditionally, depending on buffer state) unthrottle.
48 static const int unthrottle_timeout = HZ/10;
51 * The VUART is basically two UART 'front ends' connected by their FIFO
52 * (no actual serial line in between). One is on the BMC side (management
53 * controller) and one is on the host CPU side.
55 * It allows the BMC to provide to the host a "UART" that pipes into
56 * the BMC itself and can then be turned by the BMC into a network console
57 * of some sort for example.
59 * This driver is for the BMC side. The sysfs files allow the BMC
60 * userspace which owns the system configuration policy, to specify
61 * at what IO port and interrupt number the host side will appear
62 * to the host on the Host <-> BMC LPC bus. It could be different on a
63 * different system (though most of them use 3f8/4).
66 static inline u8 aspeed_vuart_readb(struct aspeed_vuart *vuart, u8 reg)
68 return readb(vuart->port->port.membase + reg);
71 static inline void aspeed_vuart_writeb(struct aspeed_vuart *vuart, u8 val, u8 reg)
73 writeb(val, vuart->port->port.membase + reg);
76 static ssize_t lpc_address_show(struct device *dev,
77 struct device_attribute *attr, char *buf)
79 struct aspeed_vuart *vuart = dev_get_drvdata(dev);
82 addr = (aspeed_vuart_readb(vuart, ASPEED_VUART_ADDRH) << 8) |
83 (aspeed_vuart_readb(vuart, ASPEED_VUART_ADDRL));
85 return sysfs_emit(buf, "0x%x\n", addr);
88 static int aspeed_vuart_set_lpc_address(struct aspeed_vuart *vuart, u32 addr)
93 aspeed_vuart_writeb(vuart, addr >> 8, ASPEED_VUART_ADDRH);
94 aspeed_vuart_writeb(vuart, addr >> 0, ASPEED_VUART_ADDRL);
99 static ssize_t lpc_address_store(struct device *dev,
100 struct device_attribute *attr,
101 const char *buf, size_t count)
103 struct aspeed_vuart *vuart = dev_get_drvdata(dev);
107 err = kstrtou32(buf, 0, &val);
111 err = aspeed_vuart_set_lpc_address(vuart, val);
112 return err ? : count;
115 static DEVICE_ATTR_RW(lpc_address);
117 static ssize_t sirq_show(struct device *dev,
118 struct device_attribute *attr, char *buf)
120 struct aspeed_vuart *vuart = dev_get_drvdata(dev);
123 reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRB);
124 reg &= ASPEED_VUART_GCRB_HOST_SIRQ_MASK;
125 reg >>= ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT;
127 return sysfs_emit(buf, "%u\n", reg);
130 static int aspeed_vuart_set_sirq(struct aspeed_vuart *vuart, u32 sirq)
134 if (sirq > (ASPEED_VUART_GCRB_HOST_SIRQ_MASK >> ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT))
137 sirq <<= ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT;
138 sirq &= ASPEED_VUART_GCRB_HOST_SIRQ_MASK;
140 reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRB);
141 reg &= ~ASPEED_VUART_GCRB_HOST_SIRQ_MASK;
143 aspeed_vuart_writeb(vuart, reg, ASPEED_VUART_GCRB);
148 static ssize_t sirq_store(struct device *dev, struct device_attribute *attr,
149 const char *buf, size_t count)
151 struct aspeed_vuart *vuart = dev_get_drvdata(dev);
155 err = kstrtoul(buf, 0, &val);
159 err = aspeed_vuart_set_sirq(vuart, val);
160 return err ? : count;
163 static DEVICE_ATTR_RW(sirq);
165 static ssize_t sirq_polarity_show(struct device *dev,
166 struct device_attribute *attr, char *buf)
168 struct aspeed_vuart *vuart = dev_get_drvdata(dev);
171 reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRA);
172 reg &= ASPEED_VUART_GCRA_HOST_SIRQ_POLARITY;
174 return sysfs_emit(buf, "%u\n", reg ? 1 : 0);
177 static void aspeed_vuart_set_sirq_polarity(struct aspeed_vuart *vuart,
180 u8 reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRA);
183 reg |= ASPEED_VUART_GCRA_HOST_SIRQ_POLARITY;
185 reg &= ~ASPEED_VUART_GCRA_HOST_SIRQ_POLARITY;
187 aspeed_vuart_writeb(vuart, reg, ASPEED_VUART_GCRA);
190 static ssize_t sirq_polarity_store(struct device *dev,
191 struct device_attribute *attr,
192 const char *buf, size_t count)
194 struct aspeed_vuart *vuart = dev_get_drvdata(dev);
198 err = kstrtoul(buf, 0, &val);
202 aspeed_vuart_set_sirq_polarity(vuart, val != 0);
207 static DEVICE_ATTR_RW(sirq_polarity);
209 static struct attribute *aspeed_vuart_attrs[] = {
211 &dev_attr_sirq_polarity.attr,
212 &dev_attr_lpc_address.attr,
216 static const struct attribute_group aspeed_vuart_attr_group = {
217 .attrs = aspeed_vuart_attrs,
220 static void aspeed_vuart_set_enabled(struct aspeed_vuart *vuart, bool enabled)
222 u8 reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRA);
225 reg |= ASPEED_VUART_GCRA_VUART_EN;
227 reg &= ~ASPEED_VUART_GCRA_VUART_EN;
229 aspeed_vuart_writeb(vuart, reg, ASPEED_VUART_GCRA);
232 static void aspeed_vuart_set_host_tx_discard(struct aspeed_vuart *vuart,
237 reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRA);
239 /* If the DISABLE_HOST_TX_DISCARD bit is set, discard is disabled */
241 reg |= ASPEED_VUART_GCRA_DISABLE_HOST_TX_DISCARD;
243 reg &= ~ASPEED_VUART_GCRA_DISABLE_HOST_TX_DISCARD;
245 aspeed_vuart_writeb(vuart, reg, ASPEED_VUART_GCRA);
248 static int aspeed_vuart_startup(struct uart_port *uart_port)
250 struct uart_8250_port *uart_8250_port = up_to_u8250p(uart_port);
251 struct aspeed_vuart *vuart = uart_8250_port->port.private_data;
254 rc = serial8250_do_startup(uart_port);
258 aspeed_vuart_set_host_tx_discard(vuart, false);
263 static void aspeed_vuart_shutdown(struct uart_port *uart_port)
265 struct uart_8250_port *uart_8250_port = up_to_u8250p(uart_port);
266 struct aspeed_vuart *vuart = uart_8250_port->port.private_data;
268 aspeed_vuart_set_host_tx_discard(vuart, true);
270 serial8250_do_shutdown(uart_port);
273 static void __aspeed_vuart_set_throttle(struct uart_8250_port *up,
276 unsigned char irqs = UART_IER_RLSI | UART_IER_RDI;
278 /* Port locked to synchronize UART_IER access against the console. */
279 lockdep_assert_held_once(&up->port.lock);
284 serial_out(up, UART_IER, up->ier);
286 static void aspeed_vuart_set_throttle(struct uart_port *port, bool throttle)
288 struct uart_8250_port *up = up_to_u8250p(port);
291 spin_lock_irqsave(&port->lock, flags);
292 __aspeed_vuart_set_throttle(up, throttle);
293 spin_unlock_irqrestore(&port->lock, flags);
296 static void aspeed_vuart_throttle(struct uart_port *port)
298 aspeed_vuart_set_throttle(port, true);
301 static void aspeed_vuart_unthrottle(struct uart_port *port)
303 aspeed_vuart_set_throttle(port, false);
306 static void aspeed_vuart_unthrottle_exp(struct timer_list *timer)
308 struct aspeed_vuart *vuart = from_timer(vuart, timer, unthrottle_timer);
309 struct uart_8250_port *up = vuart->port;
311 if (!tty_buffer_space_avail(&up->port.state->port)) {
312 mod_timer(&vuart->unthrottle_timer,
313 jiffies + unthrottle_timeout);
317 aspeed_vuart_unthrottle(&up->port);
321 * Custom interrupt handler to manage finer-grained flow control. Although we
322 * have throttle/unthrottle callbacks, we've seen that the VUART device can
323 * deliver characters faster than the ldisc has a chance to check buffer space
324 * against the throttle threshold. This results in dropped characters before
327 * We do this by checking for flip buffer space before RX. If we have no space,
328 * throttle now and schedule an unthrottle for later, once the ldisc has had
329 * a chance to drain the buffers.
331 static int aspeed_vuart_handle_irq(struct uart_port *port)
333 struct uart_8250_port *up = up_to_u8250p(port);
334 unsigned int iir, lsr;
336 unsigned int space, count;
338 iir = serial_port_in(port, UART_IIR);
340 if (iir & UART_IIR_NO_INT)
343 spin_lock_irqsave(&port->lock, flags);
345 lsr = serial_port_in(port, UART_LSR);
347 if (lsr & (UART_LSR_DR | UART_LSR_BI)) {
348 space = tty_buffer_space_avail(&port->state->port);
351 /* throttle and schedule an unthrottle later */
352 struct aspeed_vuart *vuart = port->private_data;
353 __aspeed_vuart_set_throttle(up, true);
355 if (!timer_pending(&vuart->unthrottle_timer))
356 mod_timer(&vuart->unthrottle_timer,
357 jiffies + unthrottle_timeout);
360 count = min(space, 256U);
363 serial8250_read_char(up, lsr);
364 lsr = serial_in(up, UART_LSR);
367 } while (lsr & (UART_LSR_DR | UART_LSR_BI));
369 tty_flip_buffer_push(&port->state->port);
373 serial8250_modem_status(up);
374 if (lsr & UART_LSR_THRE)
375 serial8250_tx_chars(up);
377 uart_unlock_and_check_sysrq_irqrestore(port, flags);
382 static void aspeed_vuart_auto_configure_sirq_polarity(
383 struct aspeed_vuart *vuart, struct device_node *syscon_np,
384 u32 reg_offset, u32 reg_mask)
386 struct regmap *regmap;
389 regmap = syscon_node_to_regmap(syscon_np);
390 if (IS_ERR(regmap)) {
392 "could not get regmap for aspeed,sirq-polarity-sense\n");
395 if (regmap_read(regmap, reg_offset, &value)) {
396 dev_warn(vuart->dev, "could not read hw strap table\n");
400 aspeed_vuart_set_sirq_polarity(vuart, (value & reg_mask) == 0);
403 static int aspeed_vuart_map_irq_polarity(u32 dt)
406 case IRQ_TYPE_LEVEL_LOW:
408 case IRQ_TYPE_LEVEL_HIGH:
415 static int aspeed_vuart_probe(struct platform_device *pdev)
417 struct of_phandle_args sirq_polarity_sense_args;
418 struct uart_8250_port port;
419 struct aspeed_vuart *vuart;
420 struct device_node *np;
421 struct resource *res;
422 u32 clk, prop, sirq[2];
423 int rc, sirq_polarity;
425 np = pdev->dev.of_node;
427 vuart = devm_kzalloc(&pdev->dev, sizeof(*vuart), GFP_KERNEL);
431 vuart->dev = &pdev->dev;
432 timer_setup(&vuart->unthrottle_timer, aspeed_vuart_unthrottle_exp, 0);
434 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
438 memset(&port, 0, sizeof(port));
439 port.port.private_data = vuart;
440 port.port.mapbase = res->start;
441 port.port.mapsize = resource_size(res);
442 port.port.startup = aspeed_vuart_startup;
443 port.port.shutdown = aspeed_vuart_shutdown;
444 port.port.throttle = aspeed_vuart_throttle;
445 port.port.unthrottle = aspeed_vuart_unthrottle;
446 port.port.status = UPSTAT_SYNC_FIFO;
447 port.port.dev = &pdev->dev;
448 port.port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
449 port.bugs |= UART_BUG_TXRACE;
451 rc = sysfs_create_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
455 if (of_property_read_u32(np, "clock-frequency", &clk)) {
456 vuart->clk = devm_clk_get(&pdev->dev, NULL);
457 if (IS_ERR(vuart->clk)) {
459 "clk or clock-frequency not defined\n");
460 rc = PTR_ERR(vuart->clk);
461 goto err_sysfs_remove;
464 rc = clk_prepare_enable(vuart->clk);
466 goto err_sysfs_remove;
468 clk = clk_get_rate(vuart->clk);
471 /* If current-speed was set, then try not to change it. */
472 if (of_property_read_u32(np, "current-speed", &prop) == 0)
473 port.port.custom_divisor = clk / (16 * prop);
475 /* Check for shifted address mapping */
476 if (of_property_read_u32(np, "reg-offset", &prop) == 0)
477 port.port.mapbase += prop;
479 /* Check for registers offset within the devices address range */
480 if (of_property_read_u32(np, "reg-shift", &prop) == 0)
481 port.port.regshift = prop;
483 /* Check for fifo size */
484 if (of_property_read_u32(np, "fifo-size", &prop) == 0)
485 port.port.fifosize = prop;
487 /* Check for a fixed line number */
488 rc = of_alias_get_id(np, "serial");
492 port.port.irq = irq_of_parse_and_map(np, 0);
493 port.port.handle_irq = aspeed_vuart_handle_irq;
494 port.port.iotype = UPIO_MEM;
495 port.port.type = PORT_ASPEED_VUART;
496 port.port.uartclk = clk;
497 port.port.flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
498 | UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_NO_THRE_TEST;
500 if (of_property_read_bool(np, "no-loopback-test"))
501 port.port.flags |= UPF_SKIP_TEST;
503 if (port.port.fifosize)
504 port.capabilities = UART_CAP_FIFO;
506 if (of_property_read_bool(np, "auto-flow-control"))
507 port.capabilities |= UART_CAP_AFE;
509 rc = serial8250_register_8250_port(&port);
511 goto err_clk_disable;
514 vuart->port = serial8250_get_port(vuart->line);
516 rc = of_parse_phandle_with_fixed_args(
517 np, "aspeed,sirq-polarity-sense", 2, 0,
518 &sirq_polarity_sense_args);
521 "aspeed,sirq-polarity-sense property not found\n");
523 aspeed_vuart_auto_configure_sirq_polarity(
524 vuart, sirq_polarity_sense_args.np,
525 sirq_polarity_sense_args.args[0],
526 BIT(sirq_polarity_sense_args.args[1]));
527 of_node_put(sirq_polarity_sense_args.np);
530 rc = of_property_read_u32(np, "aspeed,lpc-io-reg", &prop);
532 prop = ASPEED_VUART_DEFAULT_LPC_ADDR;
534 rc = aspeed_vuart_set_lpc_address(vuart, prop);
536 dev_err(&pdev->dev, "invalid value in aspeed,lpc-io-reg property\n");
537 goto err_clk_disable;
540 rc = of_property_read_u32_array(np, "aspeed,lpc-interrupts", sirq, 2);
542 sirq[0] = ASPEED_VUART_DEFAULT_SIRQ;
543 sirq[1] = ASPEED_VUART_DEFAULT_SIRQ_POLARITY;
546 rc = aspeed_vuart_set_sirq(vuart, sirq[0]);
548 dev_err(&pdev->dev, "invalid sirq number in aspeed,lpc-interrupts property\n");
549 goto err_clk_disable;
552 sirq_polarity = aspeed_vuart_map_irq_polarity(sirq[1]);
553 if (sirq_polarity < 0) {
554 dev_err(&pdev->dev, "invalid sirq polarity in aspeed,lpc-interrupts property\n");
556 goto err_clk_disable;
559 aspeed_vuart_set_sirq_polarity(vuart, sirq_polarity);
561 aspeed_vuart_set_enabled(vuart, true);
562 aspeed_vuart_set_host_tx_discard(vuart, true);
563 platform_set_drvdata(pdev, vuart);
568 clk_disable_unprepare(vuart->clk);
569 irq_dispose_mapping(port.port.irq);
571 sysfs_remove_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
575 static int aspeed_vuart_remove(struct platform_device *pdev)
577 struct aspeed_vuart *vuart = platform_get_drvdata(pdev);
579 del_timer_sync(&vuart->unthrottle_timer);
580 aspeed_vuart_set_enabled(vuart, false);
581 serial8250_unregister_port(vuart->line);
582 sysfs_remove_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
583 clk_disable_unprepare(vuart->clk);
588 static const struct of_device_id aspeed_vuart_table[] = {
589 { .compatible = "aspeed,ast2400-vuart" },
590 { .compatible = "aspeed,ast2500-vuart" },
594 static struct platform_driver aspeed_vuart_driver = {
596 .name = "aspeed-vuart",
597 .of_match_table = aspeed_vuart_table,
599 .probe = aspeed_vuart_probe,
600 .remove = aspeed_vuart_remove,
603 module_platform_driver(aspeed_vuart_driver);
605 MODULE_AUTHOR("Jeremy Kerr <jk@ozlabs.org>");
606 MODULE_LICENSE("GPL");
607 MODULE_DESCRIPTION("Driver for Aspeed VUART device");