From patchwork Wed Apr 5 04:03:51 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Stanley X-Patchwork-Id: 96767 Delivered-To: patch@linaro.org Received: by 10.140.89.233 with SMTP id v96csp88698qgd; Tue, 4 Apr 2017 21:05:20 -0700 (PDT) X-Received: by 10.84.239.8 with SMTP id w8mr33212521plk.73.1491365120062; Tue, 04 Apr 2017 21:05:20 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 1si19364023plo.277.2017.04.04.21.05.19; Tue, 04 Apr 2017 21:05:20 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-serial-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: best guess record for domain of linux-serial-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-serial-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751221AbdDEEFS (ORCPT + 3 others); Wed, 5 Apr 2017 00:05:18 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]:34811 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751208AbdDEEFQ (ORCPT ); Wed, 5 Apr 2017 00:05:16 -0400 Received: by mail-pf0-f194.google.com with SMTP id o126so312444pfb.1; Tue, 04 Apr 2017 21:05:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=G2nSPwbs8cI5H5NfsY4TrCLlKQZ6Q+Idx3xuEj4wz7w=; b=CfnPjP5PS4Meg9R3+8xFg+j1SNJFvLK9K3epZKmSVVQXnl014CmVUj05x0hsBk7+Cg cpJz17Z1zJSfl3Ytz5nFnNqAsRhyTjc6+uS2taYxwVeAR1uMqhY2TlGF6p03CeRvDJkh 66NSsTuThVTRy1tZM4QZYo3/sVn0O8QKmoClOpgptkLzkXRwilBvlDZ9Yf5GxsfxWUTa oPyGmvz8IjIByo+HkaAdxkX2B5vaZNqVe7ahmQZlSbZsZ3RnF6Mmw60kQs9rR562gArp HuDNwAHhz5eU34jC3A7bDYhdI61OdDLGjlNLN7Y42hdkWZ7caQfnCynAMYZAViOSKeZD Jucw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=G2nSPwbs8cI5H5NfsY4TrCLlKQZ6Q+Idx3xuEj4wz7w=; b=KM1AxLHCzXxbj6wf5b3gM9CSUTeYLkZUd6xkMJK+yltlIaqGUYg87Z4SD2k7LyGGQX MNhUHra8Ethk//vmpWdI7ND06y4rquREibV5XX5HXf1SwzX8bVNjqugmFrKSTw/bLzPd ErVriUvsTJl8AGqekSAWZHeTU25HDT8J/gp4K8wZpeQTnKiCHWACvd8WPhry+XrMg1+B qSx8PTNCyEW7ANlWg6mg0YPNCNPOhBmz03KdbrCLkOU63YUHe6DStQx6U7fR7xVVPyMx eQH6981MgD+qxM1IqV9m/sKlyM0NtnlFkNBa5NOB4pD3LrVwsMMwVpHYX1rTo8HJk2qk KO2w== X-Gm-Message-State: AFeK/H1xiFNJ+7GlR69QpHq+CkK0TisGmfx8Eroa//yDAaSJa0bUFr+YA6Vjm6aO8S28Zg== X-Received: by 10.98.217.67 with SMTP id s64mr27199798pfg.119.1491365110311; Tue, 04 Apr 2017 21:05:10 -0700 (PDT) Received: from aurora.jms.id.au ([203.0.153.9]) by smtp.gmail.com with ESMTPSA id i15sm34250314pfj.51.2017.04.04.21.05.03 (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 04 Apr 2017 21:05:09 -0700 (PDT) Received: by aurora.jms.id.au (sSMTP sendmail emulation); Wed, 05 Apr 2017 13:35:00 +0930 From: Joel Stanley To: Greg Kroah-Hartman , Jiri Slaby , Mark Rutland , Rob Herring Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, Andy Shevchenko , Benjamin Herrenschmidt , Jeremy Kerr , openbmc@lists.ozlabs.org Subject: [PATCH v2 1/2] serial: 8250: Add flag so drivers can avoid THRE probe Date: Wed, 5 Apr 2017 13:33:51 +0930 Message-Id: <20170405040352.5661-2-joel@jms.id.au> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170405040352.5661-1-joel@jms.id.au> References: <20170405040352.5661-1-joel@jms.id.au> Sender: linux-serial-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org The probing of THRE irq behaviour assumes the other end will be reading bytes out of the buffer in order to probe the port at driver init. In some cases the other end cannot be relied upon to read these bytes, so provide a flag for them to skip this step. Bit 16 was chosen as the flags are a int and the top bits are taken. Acked-by: Benjamin Herrenschmidt Signed-off-by: Joel Stanley --- drivers/tty/serial/8250/8250_port.c | 2 +- include/linux/serial_core.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) -- 2.11.0 -- To unsubscribe from this list: send the line "unsubscribe linux-serial" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/8250_port.c index 6119516ef5fc..60a6c247340f 100644 --- a/drivers/tty/serial/8250/8250_port.c +++ b/drivers/tty/serial/8250/8250_port.c @@ -2229,7 +2229,7 @@ int serial8250_do_startup(struct uart_port *port) } } - if (port->irq) { + if (port->irq && !(up->port.flags & UPF_NO_THRE_TEST)) { unsigned char iir1; /* * Test for UARTs that do not reassert THRE when the diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h index 58484fb35cc8..260245deec94 100644 --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -195,6 +195,7 @@ struct uart_port { #define UPF_NO_TXEN_TEST ((__force upf_t) (1 << 15)) #define UPF_MAGIC_MULTIPLIER ((__force upf_t) ASYNC_MAGIC_MULTIPLIER /* 16 */ ) +#define UPF_NO_THRE_TEST ((__force upf_t) (1 << 19)) /* Port has hardware-assisted h/w flow control */ #define UPF_AUTO_CTS ((__force upf_t) (1 << 20)) #define UPF_AUTO_RTS ((__force upf_t) (1 << 21)) From patchwork Wed Apr 5 04:03:52 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Stanley X-Patchwork-Id: 96768 Delivered-To: patch@linaro.org Received: by 10.140.89.233 with SMTP id v96csp88798qgd; Tue, 4 Apr 2017 21:05:44 -0700 (PDT) X-Received: by 10.84.197.35 with SMTP id m32mr33460118pld.123.1491365144733; Tue, 04 Apr 2017 21:05:44 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i10si15777388pfe.357.2017.04.04.21.05.44; Tue, 04 Apr 2017 21:05:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-serial-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: best guess record for domain of linux-serial-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-serial-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752108AbdDEEF0 (ORCPT + 3 others); Wed, 5 Apr 2017 00:05:26 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:36030 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751653AbdDEEFW (ORCPT ); Wed, 5 Apr 2017 00:05:22 -0400 Received: by mail-pf0-f193.google.com with SMTP id r137so303555pfr.3; Tue, 04 Apr 2017 21:05:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=DHD2p4UEih4H6uhYkm0Gj67PcA1CyGX+ro9OG2UTF18=; b=P0X8V+JVVjpxPqRmD29tsapFH9TFR3JTneCMXspqHifQQG378H3JaWQ8NFVYw0h9WB N7KpY2EsQXyfOzuMKBk7tuh6jGVWLcLv2d9Sj4BPSeaEGAjTygtH3rrMWIQoEvIEkXsi XYboc2emnJJp7hvwfmZ9LqJL8IvCDpdkhC/W82XmIWFlkRaTXmFl88LLJZ/ypPGJSR/L xZu5HlJltWe838ruqbWbmewyOYh0v+WDshH1iOyjHVCmxqd8TRG0XQzmEIgJ/26io4K1 NrBXixNm8G46icu++GQMQeTl7DvIxbvSywy2AxIP2q6FKgNe0dXTr0fz+83lrOVbyUBF NLKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=DHD2p4UEih4H6uhYkm0Gj67PcA1CyGX+ro9OG2UTF18=; b=O75//URlA1AKe1lUYBTJXi2fxILJ7vM36YuXLGwuwgReWN7zVth0kkfPWa/ZHSots6 hjiObtnjmWjcEq1RbUYG4Y/gXVu05IOH7zXFfYwryjbmcLU70t2d67NmEv0TU+j4LlnG LL5X0o4pcOev+J3EzNTaIQw6lPhPKUN7S1X7c4/UAAXD3G2DslTKaEgvgroAqRi8LOgW AmpW+tbjuNnu4jAv41Q5ZbfvW3pG8Iandoxd9kCqnzWnl/DbiJ2B9BSI7snwHnQZRqCa Wht96ycyorziOuRIzrr192FqEsRTLNgU36SfDHhsZ7+bhZjDkhxl+uuwBJW6wQZwQfLs 33/w== X-Gm-Message-State: AFeK/H3nFqy32RNO8R/RlbNohNxwJRM1/rQwetZ779hUMa8K0Se2K+o5JZYWZs9hjG89rg== X-Received: by 10.98.163.79 with SMTP id s76mr26685263pfe.32.1491365121019; Tue, 04 Apr 2017 21:05:21 -0700 (PDT) Received: from aurora.jms.id.au ([203.0.153.9]) by smtp.gmail.com with ESMTPSA id a77sm34253575pfl.91.2017.04.04.21.05.14 (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 04 Apr 2017 21:05:20 -0700 (PDT) Received: by aurora.jms.id.au (sSMTP sendmail emulation); Wed, 05 Apr 2017 13:35:10 +0930 From: Joel Stanley To: Greg Kroah-Hartman , Jiri Slaby , Mark Rutland , Rob Herring Cc: Jeremy Kerr , linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, Andy Shevchenko , Benjamin Herrenschmidt , openbmc@lists.ozlabs.org Subject: [PATCH v2 2/2] drivers/serial: Add driver for Aspeed virtual UART Date: Wed, 5 Apr 2017 13:33:52 +0930 Message-Id: <20170405040352.5661-3-joel@jms.id.au> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170405040352.5661-1-joel@jms.id.au> References: <20170405040352.5661-1-joel@jms.id.au> Sender: linux-serial-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org From: Jeremy Kerr This change adds a driver for the 16550-based Aspeed virtual UART device. We use a similar process to the of_serial driver for device probe, but expose some VUART-specific functions through sysfs too. The VUART is two UART 'front ends' connected by their FIFO (no actual serial line in between). One is on the BMC side (management controller) and one is on the host CPU side. This driver is for the BMC side. The sysfs files allow the BMC userspace, which owns the system configuration policy, to specify at what IO port and interrupt number the host side will appear to the host on the Host <-> BMC LPC bus. It could be different on a different system (though most of them use 3f8/4). OpenPOWER host firmware doesn't like it when the host-side of the VUART's FIFO is not drained. This driver only disables host TX discard mode when the port is in use. We set the VUART enabled bit when we bind to the device, and clear it on unbind. We don't want to do this on open/release, as the host may be using this bit to configure serial output modes, which is independent of whether the devices has been opened by BMC userspace. Signed-off-by: Jeremy Kerr Signed-off-by: Joel Stanley Acked-by: Rob Herring --- v2: - Use attribute groups and DEVICE_ATTR_RW - Use platform_get_resource/devm_ioremap_resource - of_find_property -> of_property_read_bool - Drop unncessary 0xff mask - Fix comment style - Use BIT and GENMASK where pssible - Move to 8250 directory - Rename ast -> aspeed to match other Aspeed drivers - Add documentation of sysfs file - Add detail to the commit message - add Rob's ack for the binding change --- Documentation/ABI/stable/sysfs-driver-aspeed-vuart | 15 + Documentation/devicetree/bindings/serial/8250.txt | 2 + drivers/tty/serial/8250/Kconfig | 10 + drivers/tty/serial/8250/Makefile | 1 + drivers/tty/serial/8250/aspeed-vuart.c | 341 +++++++++++++++++++++ 5 files changed, 369 insertions(+) create mode 100644 Documentation/ABI/stable/sysfs-driver-aspeed-vuart create mode 100644 drivers/tty/serial/8250/aspeed-vuart.c -- 2.11.0 -- To unsubscribe from this list: send the line "unsubscribe linux-serial" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/Documentation/ABI/stable/sysfs-driver-aspeed-vuart b/Documentation/ABI/stable/sysfs-driver-aspeed-vuart new file mode 100644 index 000000000000..8062953ce77b --- /dev/null +++ b/Documentation/ABI/stable/sysfs-driver-aspeed-vuart @@ -0,0 +1,15 @@ +What: /sys/bus/platform/drivers/aspeed-vuart/*/lpc_address +Date: April 2017 +Contact: Jeremy Kerr +Description: Configures which IO port the host side of the UART + will appear on the host <-> BMC LPC bus. +Users: OpenBMC. Proposed changes should be mailed to + openbmc@lists.ozlabs.org + +What: /sys/bus/platform/drivers/aspeed-vuart*/sirq +Date: April 2017 +Contact: Jeremy Kerr +Description: Configures which interrupt number the host side of + the UART will appear on the host <-> BMC LPC bus. +Users: OpenBMC. Proposed changes should be mailed to + openbmc@lists.ozlabs.org diff --git a/Documentation/devicetree/bindings/serial/8250.txt b/Documentation/devicetree/bindings/serial/8250.txt index 10276a46ecef..656733949309 100644 --- a/Documentation/devicetree/bindings/serial/8250.txt +++ b/Documentation/devicetree/bindings/serial/8250.txt @@ -20,6 +20,8 @@ Required properties: - "fsl,16550-FIFO64" - "fsl,ns16550" - "ti,da830-uart" + - "aspeed,ast2400-vuart" + - "aspeed,ast2500-vuart" - "serial" if the port type is unknown. - reg : offset and length of the register set for the device. - interrupts : should contain uart interrupt. diff --git a/drivers/tty/serial/8250/Kconfig b/drivers/tty/serial/8250/Kconfig index a65fb8197aec..fb217f02ec94 100644 --- a/drivers/tty/serial/8250/Kconfig +++ b/drivers/tty/serial/8250/Kconfig @@ -220,6 +220,16 @@ config SERIAL_8250_ACCENT To compile this driver as a module, choose M here: the module will be called 8250_accent. +config SERIAL_8250_ASPEED_VUART + tristate "Aspeed Virtual UART" + depends on SERIAL_8250 + depends on OF + help + If you want to use the virtual UART (VUART) device on Aspeed + BMC platforms, enable this option. This enables the 16550A- + compatible device on the local LPC bus, giving a UART device + with no physical RS232 connections. + config SERIAL_8250_BOCA tristate "Support Boca cards" depends on SERIAL_8250 != n && ISA && SERIAL_8250_MANY_PORTS diff --git a/drivers/tty/serial/8250/Makefile b/drivers/tty/serial/8250/Makefile index 2f30f9ecdb1b..405d720b51c7 100644 --- a/drivers/tty/serial/8250/Makefile +++ b/drivers/tty/serial/8250/Makefile @@ -14,6 +14,7 @@ obj-$(CONFIG_SERIAL_8250_EXAR) += 8250_exar.o obj-$(CONFIG_SERIAL_8250_HP300) += 8250_hp300.o obj-$(CONFIG_SERIAL_8250_CS) += serial_cs.o obj-$(CONFIG_SERIAL_8250_ACORN) += 8250_acorn.o +obj-$(CONFIG_SERIAL_8250_ASPEED_VUART) += aspeed-vuart.o obj-$(CONFIG_SERIAL_8250_BCM2835AUX) += 8250_bcm2835aux.o obj-$(CONFIG_SERIAL_8250_CONSOLE) += 8250_early.o obj-$(CONFIG_SERIAL_8250_FOURPORT) += 8250_fourport.o diff --git a/drivers/tty/serial/8250/aspeed-vuart.c b/drivers/tty/serial/8250/aspeed-vuart.c new file mode 100644 index 000000000000..261b3dbaa7c1 --- /dev/null +++ b/drivers/tty/serial/8250/aspeed-vuart.c @@ -0,0 +1,341 @@ +/* + * Serial Port driver for Aspeed VUART device + * + * Copyright (C) 2016 Jeremy Kerr , IBM Corp. + * Copyright (C) 2006 Arnd Bergmann , IBM Corp. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + * + */ +#include +#include +#include +#include +#include +#include + +#include "8250.h" + +#define ASPEED_VUART_GCRA 0x20 +#define ASPEED_VUART_GCRA_VUART_EN BIT(0) +#define ASPEED_VUART_GCRA_HOST_TX_DISCARD BIT(5) +#define ASPEED_VUART_GCRB 0x24 +#define ASPEED_VUART_GCRB_HOST_SIRQ_MASK GENMASK(7, 4) +#define ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT 4 +#define ASPEED_VUART_ADDRL 0x28 +#define ASPEED_VUART_ADDRH 0x2c + +struct aspeed_vuart { + struct device *dev; + void __iomem *regs; + struct clk *clk; + int line; +}; + +/* + * The VUART is basically two UART 'front ends' connected by their FIFO + * (no actual serial line in between). One is on the BMC side (management + * controller) and one is on the host CPU side. + * + * It allows the BMC to provide to the host a "UART" that pipes into + * the BMC itself and can then be turned by the BMC into a network console + * of some sort for example. + * + * This driver is for the BMC side. The sysfs files allow the BMC + * userspace which owns the system configuration policy, to specify + * at what IO port and interrupt number the host side will appear + * to the host on the Host <-> BMC LPC bus. It could be different on a + * different system (though most of them use 3f8/4). + */ + +static ssize_t lpc_address_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct aspeed_vuart *vuart = dev_get_drvdata(dev); + u16 addr; + + addr = (readb(vuart->regs + ASPEED_VUART_ADDRH) << 8) | + (readb(vuart->regs + ASPEED_VUART_ADDRL)); + + return snprintf(buf, PAGE_SIZE - 1, "0x%x\n", addr); +} + +static ssize_t lpc_address_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct aspeed_vuart *vuart = dev_get_drvdata(dev); + unsigned long val; + int err; + + err = kstrtoul(buf, 0, &val); + if (err) + return err; + + writeb(val >> 8, vuart->regs + ASPEED_VUART_ADDRH); + writeb(val >> 0, vuart->regs + ASPEED_VUART_ADDRL); + + return count; +} + +static DEVICE_ATTR_RW(lpc_address); + +static ssize_t sirq_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct aspeed_vuart *vuart = dev_get_drvdata(dev); + u8 reg; + + reg = readb(vuart->regs + ASPEED_VUART_GCRB); + reg &= ASPEED_VUART_GCRB_HOST_SIRQ_MASK; + reg >>= ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT; + + return snprintf(buf, PAGE_SIZE - 1, "%u\n", reg); +} + +static ssize_t sirq_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct aspeed_vuart *vuart = dev_get_drvdata(dev); + unsigned long val; + int err; + u8 reg; + + err = kstrtoul(buf, 0, &val); + if (err) + return err; + + val <<= ASPEED_VUART_GCRB_HOST_SIRQ_SHIFT; + val &= ASPEED_VUART_GCRB_HOST_SIRQ_MASK; + + reg = readb(vuart->regs + ASPEED_VUART_GCRB); + reg &= ~ASPEED_VUART_GCRB_HOST_SIRQ_MASK; + reg |= val; + writeb(reg, vuart->regs + ASPEED_VUART_GCRB); + + return count; +} + +static DEVICE_ATTR_RW(sirq); + +static struct attribute *aspeed_vuart_attrs[] = { + &dev_attr_sirq.attr, + &dev_attr_lpc_address.attr, + NULL, +}; + +static const struct attribute_group aspeed_vuart_attr_group = { + .attrs = aspeed_vuart_attrs, +}; + +static void aspeed_vuart_set_enabled(struct aspeed_vuart *vuart, bool enabled) +{ + u8 reg; + + reg = readb(vuart->regs + ASPEED_VUART_GCRA); + reg &= ~ASPEED_VUART_GCRA_VUART_EN; + if (enabled) + reg |= ASPEED_VUART_GCRA_VUART_EN; + writeb(reg, vuart->regs + ASPEED_VUART_GCRA); +} + +static void aspeed_vuart_set_host_tx_discard(struct aspeed_vuart *vuart, + bool discard) +{ + u8 reg; + + reg = readb(vuart->regs + ASPEED_VUART_GCRA); + + /* if the HOST_TX_DISCARD bit is set, discard is *disabled* */ + reg &= ~ASPEED_VUART_GCRA_HOST_TX_DISCARD; + if (!discard) + reg |= ASPEED_VUART_GCRA_HOST_TX_DISCARD; + + writeb(reg, vuart->regs + ASPEED_VUART_GCRA); +} + +static int aspeed_vuart_startup(struct uart_port *uart_port) +{ + struct uart_8250_port *uart_8250_port = up_to_u8250p(uart_port); + struct aspeed_vuart *vuart = uart_8250_port->port.private_data; + int rc; + + rc = serial8250_do_startup(uart_port); + if (rc) + return rc; + + aspeed_vuart_set_host_tx_discard(vuart, false); + + return 0; +} + +static void aspeed_vuart_shutdown(struct uart_port *uart_port) +{ + struct uart_8250_port *uart_8250_port = up_to_u8250p(uart_port); + struct aspeed_vuart *vuart = uart_8250_port->port.private_data; + + aspeed_vuart_set_host_tx_discard(vuart, true); + + serial8250_do_shutdown(uart_port); +} + +static int aspeed_vuart_probe(struct platform_device *pdev) +{ + struct uart_8250_port port; + struct resource *res; + struct aspeed_vuart *vuart; + struct device_node *np; + u32 clk, prop; + int rc; + + np = pdev->dev.of_node; + + vuart = devm_kzalloc(&pdev->dev, sizeof(*vuart), GFP_KERNEL); + if (!vuart) + return -ENOMEM; + + vuart->dev = &pdev->dev; + + /* The 8510 core creates the mapping, which we grab a reference to + * for VUART-specific registers */ + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + vuart->regs = devm_ioremap_resource(&pdev->dev, res); + if (IS_ERR(vuart->regs)) + return PTR_ERR(vuart->regs); + + memset(&port, 0, sizeof(port)); + port.port.private_data = vuart; + port.port.membase = vuart->regs; + port.port.mapbase = res->start; + port.port.mapsize = resource_size(res); + port.port.startup = aspeed_vuart_startup; + port.port.shutdown = aspeed_vuart_shutdown; + port.port.dev = &pdev->dev; + + rc = sysfs_create_group(&vuart->dev->kobj, &aspeed_vuart_attr_group); + if (rc < 0) + return rc; + + if (of_property_read_u32(np, "clock-frequency", &clk)) { + vuart->clk = devm_clk_get(&pdev->dev, NULL); + if (IS_ERR(vuart->clk)) { + dev_warn(&pdev->dev, + "clk or clock-frequency not defined\n"); + return PTR_ERR(vuart->clk); + } + + rc = clk_prepare_enable(vuart->clk); + if (rc < 0) + return rc; + + clk = clk_get_rate(vuart->clk); + } + + /* If current-speed was set, then try not to change it. */ + if (of_property_read_u32(np, "current-speed", &prop) == 0) + port.port.custom_divisor = clk / (16 * prop); + + /* Check for shifted address mapping */ + if (of_property_read_u32(np, "reg-offset", &prop) == 0) + port.port.mapbase += prop; + + /* Check for registers offset within the devices address range */ + if (of_property_read_u32(np, "reg-shift", &prop) == 0) + port.port.regshift = prop; + + /* Check for fifo size */ + if (of_property_read_u32(np, "fifo-size", &prop) == 0) + port.port.fifosize = prop; + + /* Check for a fixed line number */ + rc = of_alias_get_id(np, "serial"); + if (rc >= 0) + port.port.line = rc; + + port.port.irq = irq_of_parse_and_map(np, 0); + port.port.irqflags = IRQF_SHARED; + port.port.iotype = UPIO_MEM; + if (of_property_read_u32(np, "reg-io-width", &prop) == 0) { + switch (prop) { + case 1: + port.port.iotype = UPIO_MEM; + break; + case 4: + port.port.iotype = of_device_is_big_endian(np) ? + UPIO_MEM32BE : UPIO_MEM32; + break; + default: + dev_warn(&pdev->dev, "unsupported reg-io-width (%d)\n", + prop); + rc = -EINVAL; + goto err_clk_disable; + } + } + + port.port.type = PORT_16550A; + port.port.uartclk = clk; + port.port.flags = UPF_SHARE_IRQ | UPF_BOOT_AUTOCONF + | UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_NO_THRE_TEST; + + if (of_property_read_bool(np, "no-loopback-test")) + port.port.flags |= UPF_SKIP_TEST; + + if (port.port.fifosize) + port.capabilities = UART_CAP_FIFO; + + if (of_property_read_bool(np, "auto-flow-control")) + port.capabilities |= UART_CAP_AFE; + + rc = serial8250_register_8250_port(&port); + if (rc < 0) + goto err_clk_disable; + + vuart->line = rc; + + aspeed_vuart_set_enabled(vuart, true); + aspeed_vuart_set_host_tx_discard(vuart, true); + platform_set_drvdata(pdev, vuart); + + return 0; + +err_clk_disable: + clk_disable_unprepare(vuart->clk); + irq_dispose_mapping(port.port.irq); + return rc; +} + +static int aspeed_vuart_remove(struct platform_device *pdev) +{ + struct aspeed_vuart *vuart = platform_get_drvdata(pdev); + + aspeed_vuart_set_enabled(vuart, false); + serial8250_unregister_port(vuart->line); + sysfs_remove_group(&vuart->dev->kobj, &aspeed_vuart_attr_group); + clk_disable_unprepare(vuart->clk); + + return 0; +} + +static const struct of_device_id aspeed_vuart_table[] = { + { .compatible = "aspeed,ast2400-vuart" }, + { .compatible = "aspeed,ast2500-vuart" }, + { }, +}; + +static struct platform_driver aspeed_vuart_driver = { + .driver = { + .name = "aspeed-vuart", + .of_match_table = aspeed_vuart_table, + }, + .probe = aspeed_vuart_probe, + .remove = aspeed_vuart_remove, +}; + +module_platform_driver(aspeed_vuart_driver); + +MODULE_AUTHOR("Jeremy Kerr "); +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Driver for Aspeed VUART device");