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
38 struct timer_list unthrottle_timer;
39 struct uart_8250_port *port;
43 * If we fill the tty flip buffers, we throttle the data ready interrupt
44 * to prevent dropped characters. This timeout defines how long we wait
45 * to (conditionally, depending on buffer state) unthrottle.
47 static const int unthrottle_timeout = HZ/10;
50 * The VUART is basically two UART 'front ends' connected by their FIFO
51 * (no actual serial line in between). One is on the BMC side (management
52 * controller) and one is on the host CPU side.
54 * It allows the BMC to provide to the host a "UART" that pipes into
55 * the BMC itself and can then be turned by the BMC into a network console
56 * of some sort for example.
58 * This driver is for the BMC side. The sysfs files allow the BMC
59 * userspace which owns the system configuration policy, to specify
60 * at what IO port and interrupt number the host side will appear
61 * to the host on the Host <-> BMC LPC bus. It could be different on a
62 * different system (though most of them use 3f8/4).
65 static inline u8 aspeed_vuart_readb(struct aspeed_vuart *vuart, u8 reg)
67 return readb(vuart->port->port.membase + reg);
70 static inline void aspeed_vuart_writeb(struct aspeed_vuart *vuart, u8 val, u8 reg)
72 writeb(val, vuart->port->port.membase + reg);
75 static ssize_t lpc_address_show(struct device *dev,
76 struct device_attribute *attr, char *buf)
78 struct aspeed_vuart *vuart = dev_get_drvdata(dev);
81 addr = (aspeed_vuart_readb(vuart, ASPEED_VUART_ADDRH) << 8) |
82 (aspeed_vuart_readb(vuart, ASPEED_VUART_ADDRL));
84 return sysfs_emit(buf, "0x%x\n", addr);
87 static int aspeed_vuart_set_lpc_address(struct aspeed_vuart *vuart, u32 addr)
92 aspeed_vuart_writeb(vuart, addr >> 8, ASPEED_VUART_ADDRH);
93 aspeed_vuart_writeb(vuart, addr >> 0, ASPEED_VUART_ADDRL);
98 static ssize_t lpc_address_store(struct device *dev,
99 struct device_attribute *attr,
100 const char *buf, size_t count)
102 struct aspeed_vuart *vuart = dev_get_drvdata(dev);
106 err = kstrtou32(buf, 0, &val);
110 err = aspeed_vuart_set_lpc_address(vuart, val);
111 return err ? : count;
114 static DEVICE_ATTR_RW(lpc_address);
116 static ssize_t sirq_show(struct device *dev,
117 struct device_attribute *attr, char *buf)
119 struct aspeed_vuart *vuart = dev_get_drvdata(dev);
122 reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRB);
123 reg &= ASPEED_VUART_GCRB_HOST_SIRQ_MASK;
124 reg >>= ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT;
126 return sysfs_emit(buf, "%u\n", reg);
129 static int aspeed_vuart_set_sirq(struct aspeed_vuart *vuart, u32 sirq)
133 if (sirq > (ASPEED_VUART_GCRB_HOST_SIRQ_MASK >> ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT))
136 sirq <<= ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT;
137 sirq &= ASPEED_VUART_GCRB_HOST_SIRQ_MASK;
139 reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRB);
140 reg &= ~ASPEED_VUART_GCRB_HOST_SIRQ_MASK;
142 aspeed_vuart_writeb(vuart, reg, ASPEED_VUART_GCRB);
147 static ssize_t sirq_store(struct device *dev, struct device_attribute *attr,
148 const char *buf, size_t count)
150 struct aspeed_vuart *vuart = dev_get_drvdata(dev);
154 err = kstrtoul(buf, 0, &val);
158 err = aspeed_vuart_set_sirq(vuart, val);
159 return err ? : count;
162 static DEVICE_ATTR_RW(sirq);
164 static ssize_t sirq_polarity_show(struct device *dev,
165 struct device_attribute *attr, char *buf)
167 struct aspeed_vuart *vuart = dev_get_drvdata(dev);
170 reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRA);
171 reg &= ASPEED_VUART_GCRA_HOST_SIRQ_POLARITY;
173 return sysfs_emit(buf, "%u\n", reg ? 1 : 0);
176 static void aspeed_vuart_set_sirq_polarity(struct aspeed_vuart *vuart,
179 u8 reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRA);
182 reg |= ASPEED_VUART_GCRA_HOST_SIRQ_POLARITY;
184 reg &= ~ASPEED_VUART_GCRA_HOST_SIRQ_POLARITY;
186 aspeed_vuart_writeb(vuart, reg, ASPEED_VUART_GCRA);
189 static ssize_t sirq_polarity_store(struct device *dev,
190 struct device_attribute *attr,
191 const char *buf, size_t count)
193 struct aspeed_vuart *vuart = dev_get_drvdata(dev);
197 err = kstrtoul(buf, 0, &val);
201 aspeed_vuart_set_sirq_polarity(vuart, val != 0);
206 static DEVICE_ATTR_RW(sirq_polarity);
208 static struct attribute *aspeed_vuart_attrs[] = {
210 &dev_attr_sirq_polarity.attr,
211 &dev_attr_lpc_address.attr,
215 static const struct attribute_group aspeed_vuart_attr_group = {
216 .attrs = aspeed_vuart_attrs,
219 static void aspeed_vuart_set_enabled(struct aspeed_vuart *vuart, bool enabled)
221 u8 reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRA);
224 reg |= ASPEED_VUART_GCRA_VUART_EN;
226 reg &= ~ASPEED_VUART_GCRA_VUART_EN;
228 aspeed_vuart_writeb(vuart, reg, ASPEED_VUART_GCRA);
231 static void aspeed_vuart_set_host_tx_discard(struct aspeed_vuart *vuart,
236 reg = aspeed_vuart_readb(vuart, ASPEED_VUART_GCRA);
238 /* If the DISABLE_HOST_TX_DISCARD bit is set, discard is disabled */
240 reg |= ASPEED_VUART_GCRA_DISABLE_HOST_TX_DISCARD;
242 reg &= ~ASPEED_VUART_GCRA_DISABLE_HOST_TX_DISCARD;
244 aspeed_vuart_writeb(vuart, reg, ASPEED_VUART_GCRA);
247 static int aspeed_vuart_startup(struct uart_port *uart_port)
249 struct uart_8250_port *uart_8250_port = up_to_u8250p(uart_port);
250 struct aspeed_vuart *vuart = uart_8250_port->port.private_data;
253 rc = serial8250_do_startup(uart_port);
257 aspeed_vuart_set_host_tx_discard(vuart, false);
262 static void aspeed_vuart_shutdown(struct uart_port *uart_port)
264 struct uart_8250_port *uart_8250_port = up_to_u8250p(uart_port);
265 struct aspeed_vuart *vuart = uart_8250_port->port.private_data;
267 aspeed_vuart_set_host_tx_discard(vuart, true);
269 serial8250_do_shutdown(uart_port);
272 static void __aspeed_vuart_set_throttle(struct uart_8250_port *up,
275 unsigned char irqs = UART_IER_RLSI | UART_IER_RDI;
277 /* Port locked to synchronize UART_IER access against the console. */
278 lockdep_assert_held_once(&up->port.lock);
283 serial_out(up, UART_IER, up->ier);
285 static void aspeed_vuart_set_throttle(struct uart_port *port, bool throttle)
287 struct uart_8250_port *up = up_to_u8250p(port);
290 uart_port_lock_irqsave(port, &flags);
291 __aspeed_vuart_set_throttle(up, throttle);
292 uart_port_unlock_irqrestore(port, flags);
295 static void aspeed_vuart_throttle(struct uart_port *port)
297 aspeed_vuart_set_throttle(port, true);
300 static void aspeed_vuart_unthrottle(struct uart_port *port)
302 aspeed_vuart_set_throttle(port, false);
305 static void aspeed_vuart_unthrottle_exp(struct timer_list *timer)
307 struct aspeed_vuart *vuart = from_timer(vuart, timer, unthrottle_timer);
308 struct uart_8250_port *up = vuart->port;
310 if (!tty_buffer_space_avail(&up->port.state->port)) {
311 mod_timer(&vuart->unthrottle_timer,
312 jiffies + unthrottle_timeout);
316 aspeed_vuart_unthrottle(&up->port);
320 * Custom interrupt handler to manage finer-grained flow control. Although we
321 * have throttle/unthrottle callbacks, we've seen that the VUART device can
322 * deliver characters faster than the ldisc has a chance to check buffer space
323 * against the throttle threshold. This results in dropped characters before
326 * We do this by checking for flip buffer space before RX. If we have no space,
327 * throttle now and schedule an unthrottle for later, once the ldisc has had
328 * a chance to drain the buffers.
330 static int aspeed_vuart_handle_irq(struct uart_port *port)
332 struct uart_8250_port *up = up_to_u8250p(port);
333 unsigned int iir, lsr;
335 unsigned int space, count;
337 iir = serial_port_in(port, UART_IIR);
339 if (iir & UART_IIR_NO_INT)
342 uart_port_lock_irqsave(port, &flags);
344 lsr = serial_port_in(port, UART_LSR);
346 if (lsr & (UART_LSR_DR | UART_LSR_BI)) {
347 space = tty_buffer_space_avail(&port->state->port);
350 /* throttle and schedule an unthrottle later */
351 struct aspeed_vuart *vuart = port->private_data;
352 __aspeed_vuart_set_throttle(up, true);
354 if (!timer_pending(&vuart->unthrottle_timer))
355 mod_timer(&vuart->unthrottle_timer,
356 jiffies + unthrottle_timeout);
359 count = min(space, 256U);
362 serial8250_read_char(up, lsr);
363 lsr = serial_in(up, UART_LSR);
366 } while (lsr & (UART_LSR_DR | UART_LSR_BI));
368 tty_flip_buffer_push(&port->state->port);
372 serial8250_modem_status(up);
373 if (lsr & UART_LSR_THRE)
374 serial8250_tx_chars(up);
376 uart_unlock_and_check_sysrq_irqrestore(port, flags);
381 static void aspeed_vuart_auto_configure_sirq_polarity(
382 struct aspeed_vuart *vuart, struct device_node *syscon_np,
383 u32 reg_offset, u32 reg_mask)
385 struct regmap *regmap;
388 regmap = syscon_node_to_regmap(syscon_np);
389 if (IS_ERR(regmap)) {
391 "could not get regmap for aspeed,sirq-polarity-sense\n");
394 if (regmap_read(regmap, reg_offset, &value)) {
395 dev_warn(vuart->dev, "could not read hw strap table\n");
399 aspeed_vuart_set_sirq_polarity(vuart, (value & reg_mask) == 0);
402 static int aspeed_vuart_map_irq_polarity(u32 dt)
405 case IRQ_TYPE_LEVEL_LOW:
407 case IRQ_TYPE_LEVEL_HIGH:
414 static int aspeed_vuart_probe(struct platform_device *pdev)
416 struct of_phandle_args sirq_polarity_sense_args;
417 struct device *dev = &pdev->dev;
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;
426 np = pdev->dev.of_node;
428 vuart = devm_kzalloc(&pdev->dev, sizeof(*vuart), GFP_KERNEL);
432 vuart->dev = &pdev->dev;
433 timer_setup(&vuart->unthrottle_timer, aspeed_vuart_unthrottle_exp, 0);
435 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
439 memset(&port, 0, sizeof(port));
440 port.port.private_data = vuart;
441 port.port.mapbase = res->start;
442 port.port.mapsize = resource_size(res);
443 port.port.startup = aspeed_vuart_startup;
444 port.port.shutdown = aspeed_vuart_shutdown;
445 port.port.throttle = aspeed_vuart_throttle;
446 port.port.unthrottle = aspeed_vuart_unthrottle;
447 port.port.status = UPSTAT_SYNC_FIFO;
448 port.port.dev = &pdev->dev;
449 port.port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_8250_CONSOLE);
450 port.bugs |= UART_BUG_TXRACE;
452 rc = sysfs_create_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
456 if (of_property_read_u32(np, "clock-frequency", &clk)) {
457 vclk = devm_clk_get_enabled(dev, NULL);
459 rc = dev_err_probe(dev, PTR_ERR(vclk), "clk or clock-frequency not defined\n");
460 goto err_sysfs_remove;
463 clk = clk_get_rate(vclk);
466 /* If current-speed was set, then try not to change it. */
467 if (of_property_read_u32(np, "current-speed", &prop) == 0)
468 port.port.custom_divisor = clk / (16 * prop);
470 /* Check for shifted address mapping */
471 if (of_property_read_u32(np, "reg-offset", &prop) == 0)
472 port.port.mapbase += prop;
474 /* Check for registers offset within the devices address range */
475 if (of_property_read_u32(np, "reg-shift", &prop) == 0)
476 port.port.regshift = prop;
478 /* Check for fifo size */
479 if (of_property_read_u32(np, "fifo-size", &prop) == 0)
480 port.port.fifosize = prop;
482 /* Check for a fixed line number */
483 rc = of_alias_get_id(np, "serial");
487 port.port.irq = irq_of_parse_and_map(np, 0);
488 port.port.handle_irq = aspeed_vuart_handle_irq;
489 port.port.iotype = UPIO_MEM;
490 port.port.type = PORT_ASPEED_VUART;
491 port.port.uartclk = clk;
492 port.port.flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF | UPF_IOREMAP
493 | UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_NO_THRE_TEST;
495 if (of_property_read_bool(np, "no-loopback-test"))
496 port.port.flags |= UPF_SKIP_TEST;
498 if (port.port.fifosize)
499 port.capabilities = UART_CAP_FIFO;
501 if (of_property_read_bool(np, "auto-flow-control"))
502 port.capabilities |= UART_CAP_AFE;
504 rc = serial8250_register_8250_port(&port);
506 goto err_clk_disable;
509 vuart->port = serial8250_get_port(vuart->line);
511 rc = of_parse_phandle_with_fixed_args(
512 np, "aspeed,sirq-polarity-sense", 2, 0,
513 &sirq_polarity_sense_args);
516 "aspeed,sirq-polarity-sense property not found\n");
518 aspeed_vuart_auto_configure_sirq_polarity(
519 vuart, sirq_polarity_sense_args.np,
520 sirq_polarity_sense_args.args[0],
521 BIT(sirq_polarity_sense_args.args[1]));
522 of_node_put(sirq_polarity_sense_args.np);
525 rc = of_property_read_u32(np, "aspeed,lpc-io-reg", &prop);
527 prop = ASPEED_VUART_DEFAULT_LPC_ADDR;
529 rc = aspeed_vuart_set_lpc_address(vuart, prop);
531 dev_err_probe(dev, rc, "invalid value in aspeed,lpc-io-reg property\n");
532 goto err_clk_disable;
535 rc = of_property_read_u32_array(np, "aspeed,lpc-interrupts", sirq, 2);
537 sirq[0] = ASPEED_VUART_DEFAULT_SIRQ;
538 sirq[1] = ASPEED_VUART_DEFAULT_SIRQ_POLARITY;
541 rc = aspeed_vuart_set_sirq(vuart, sirq[0]);
543 dev_err_probe(dev, rc, "invalid sirq number in aspeed,lpc-interrupts property\n");
544 goto err_clk_disable;
547 sirq_polarity = aspeed_vuart_map_irq_polarity(sirq[1]);
548 if (sirq_polarity < 0) {
549 rc = dev_err_probe(dev, sirq_polarity,
550 "invalid sirq polarity in aspeed,lpc-interrupts property\n");
551 goto err_clk_disable;
554 aspeed_vuart_set_sirq_polarity(vuart, sirq_polarity);
556 aspeed_vuart_set_enabled(vuart, true);
557 aspeed_vuart_set_host_tx_discard(vuart, true);
558 platform_set_drvdata(pdev, vuart);
563 irq_dispose_mapping(port.port.irq);
565 sysfs_remove_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
569 static void aspeed_vuart_remove(struct platform_device *pdev)
571 struct aspeed_vuart *vuart = platform_get_drvdata(pdev);
573 del_timer_sync(&vuart->unthrottle_timer);
574 aspeed_vuart_set_enabled(vuart, false);
575 serial8250_unregister_port(vuart->line);
576 sysfs_remove_group(&vuart->dev->kobj, &aspeed_vuart_attr_group);
579 static const struct of_device_id aspeed_vuart_table[] = {
580 { .compatible = "aspeed,ast2400-vuart" },
581 { .compatible = "aspeed,ast2500-vuart" },
585 static struct platform_driver aspeed_vuart_driver = {
587 .name = "aspeed-vuart",
588 .of_match_table = aspeed_vuart_table,
590 .probe = aspeed_vuart_probe,
591 .remove_new = aspeed_vuart_remove,
594 module_platform_driver(aspeed_vuart_driver);
596 MODULE_AUTHOR("Jeremy Kerr <jk@ozlabs.org>");
597 MODULE_LICENSE("GPL");
598 MODULE_DESCRIPTION("Driver for Aspeed VUART device");