Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp2241372ybk; Sun, 17 May 2020 14:59:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzOUHaMHFVIERD4Ns89yOanE+Jw4luJ4QZFzX1Sg91VGdcZFhwYsnMDxHw/dPpEwH4adknA X-Received: by 2002:a05:6402:1586:: with SMTP id c6mr11248039edv.54.1589752751365; Sun, 17 May 2020 14:59:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589752751; cv=none; d=google.com; s=arc-20160816; b=TSClZOjGph5f4wC722GFSpZndKxQvwevRD4rH60SbpJjY57+MpKVkmMwlRr/Ui16Z6 zwv1B95WDzWclRDycOmAM8I/WZtZ2ufKsRHlfjoXyuLbVTiDUSUvaETe6UoUZBhRCOau VAYbjG3c3IfqbFdwMFE+7JfV82uKCskn/3V+4RwhWNJbYDiZPFXfIRY2U+wKFjaZ/OH0 W8YcgCzgGlSVszQlUjkBCPN/L4TuayHNsyQIFNFuYvKtzEZZ6gHfc5jLO5+SqHqNkPh8 w7CI0l3a8Ib0cJApK/inXdIGGHDCAkE/qL60k5t5G95Hyf9DvilKpUQ46h6K7aaez+8C llpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=SH+RZgUyBQHTCJEoa4aHzJNxWznUBqU+sTGn5vCOSNk=; b=vk65dLpPNzBVsthq/hlD8u8neQ78ItGqwouqUpOmTETTYuMRXRNyDwionJzgzTpSjA e/Kflyh20il52jt99p9dQ4y0ZJAN3cAQwPc2bLtgoZIvm0KtF0Jg01zowhfpRHUZiEQp Q6/1Jurw2bJLuXnJ9unGz88rmWyDCTUz1QSB53BkJ1/DvijtP9ueR0AWwUYk2IIF5XmM iPmfAB0WlLiu9Vk1tAoE2aH6fwhACwMvTBnVPwlGwcjo23WoEMR2Mo04v9YImMqQIDNN WnqhlVAmz28sSsDaQaf4pmzn0XDAYKmzkvjvgQ9np5igr0+KBZ7Z0qr8i2hgw7LBXg90 zxdw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id sa8si5541595ejb.222.2020.05.17.14.58.48; Sun, 17 May 2020 14:59:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726948AbgEQV4u (ORCPT + 99 others); Sun, 17 May 2020 17:56:50 -0400 Received: from gloria.sntech.de ([185.11.138.130]:45122 "EHLO gloria.sntech.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726444AbgEQV4e (ORCPT ); Sun, 17 May 2020 17:56:34 -0400 Received: from ip5f5aa64a.dynamic.kabel-deutschland.de ([95.90.166.74] helo=phil.sntech) by gloria.sntech.de with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jaRGf-0002am-CV; Sun, 17 May 2020 23:56:21 +0200 From: Heiko Stuebner To: gregkh@linuxfoundation.org Cc: jslaby@suse.com, andriy.shevchenko@linux.intel.com, matwey.kornilov@gmail.com, giulio.benetti@micronovasrl.com, lukas@wunner.de, linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, christoph.muellner@theobroma-systems.com, heiko@sntech.de, Heiko Stuebner Subject: [PATCH v3 4/5] serial: 8250: Handle implementations not having TEMT interrupt using em485 Date: Sun, 17 May 2020 23:56:09 +0200 Message-Id: <20200517215610.2131618-5-heiko@sntech.de> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200517215610.2131618-1-heiko@sntech.de> References: <20200517215610.2131618-1-heiko@sntech.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Giulio Benetti Some 8250 ports have a TEMT interrupt but it's not a part of the 8250 standard, instead only available on some implementations. The current em485 implementation does not work on ports without it. The only chance to make it work is to loop-read on LSR register. So add UART_CAP_TEMT to mark 8250 uarts having this interrupt, update all current em485 users with that capability and add a loop-reading during __stop_tx_rs485() on uarts not having it. As __stop_tx_rs485() can also be called from a hard-irq context the loop-reading is split. If the fifo clears in under 100us in __stop_tx_rs485() itself just the regular stop calls get executed. If it takes longer, re-use the existing stop-timer infrastructure but with only a 10us timer to again poll the LSR registers. Signed-off-by: Giulio Benetti [moved to use added UART_CAP_TEMT, use readx_poll_timeout] Signed-off-by: Heiko Stuebner --- drivers/tty/serial/8250/8250.h | 1 + drivers/tty/serial/8250/8250_bcm2835aux.c | 2 +- drivers/tty/serial/8250/8250_of.c | 2 + drivers/tty/serial/8250/8250_omap.c | 2 +- drivers/tty/serial/8250/8250_port.c | 51 ++++++++++++++++++++--- 5 files changed, 51 insertions(+), 7 deletions(-) diff --git a/drivers/tty/serial/8250/8250.h b/drivers/tty/serial/8250/8250.h index 0df02c055107..50c88dd3f857 100644 --- a/drivers/tty/serial/8250/8250.h +++ b/drivers/tty/serial/8250/8250.h @@ -82,6 +82,7 @@ struct serial8250_config { #define UART_CAP_MINI (1 << 17) /* Mini UART on BCM283X family lacks: * STOP PARITY EPAR SPAR WLEN5 WLEN6 */ +#define UART_CAP_TEMT (1 << 18) /* UART has TEMT interrupt */ #define UART_BUG_QUOT (1 << 0) /* UART has buggy quot LSB */ #define UART_BUG_TXEN (1 << 1) /* UART has buggy TX IIR status */ diff --git a/drivers/tty/serial/8250/8250_bcm2835aux.c b/drivers/tty/serial/8250/8250_bcm2835aux.c index 1cc0620b596c..c2226f1fd6ac 100644 --- a/drivers/tty/serial/8250/8250_bcm2835aux.c +++ b/drivers/tty/serial/8250/8250_bcm2835aux.c @@ -91,7 +91,7 @@ static int bcm2835aux_serial_probe(struct platform_device *pdev) return -ENOMEM; /* initialize data */ - up.capabilities = UART_CAP_FIFO | UART_CAP_MINI; + up.capabilities = UART_CAP_FIFO | UART_CAP_MINI | UART_CAP_TEMT; up.port.dev = &pdev->dev; up.port.regshift = 2; up.port.type = PORT_16550; diff --git a/drivers/tty/serial/8250/8250_of.c b/drivers/tty/serial/8250/8250_of.c index 0b89954a3781..e96abd51454c 100644 --- a/drivers/tty/serial/8250/8250_of.c +++ b/drivers/tty/serial/8250/8250_of.c @@ -223,6 +223,8 @@ static int of_platform_serial_probe(struct platform_device *ofdev) &port8250.overrun_backoff_time_ms) != 0) port8250.overrun_backoff_time_ms = 0; + port8250.capabilities |= UART_CAP_TEMT; + ret = serial8250_register_8250_port(&port8250); if (ret < 0) goto err_dispose; diff --git a/drivers/tty/serial/8250/8250_omap.c b/drivers/tty/serial/8250/8250_omap.c index 6cae3782e5fa..241d7307c38f 100644 --- a/drivers/tty/serial/8250/8250_omap.c +++ b/drivers/tty/serial/8250/8250_omap.c @@ -1144,7 +1144,7 @@ static int omap8250_probe(struct platform_device *pdev) up.port.regshift = 2; up.port.fifosize = 64; up.tx_loadsz = 64; - up.capabilities = UART_CAP_FIFO; + up.capabilities = UART_CAP_FIFO | UART_CAP_TEMT; #ifdef CONFIG_PM /* * Runtime PM is mostly transparent. However to do it right we need to a diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/8250_port.c index 9e8fec85d1a3..a456e81d3e0b 100644 --- a/drivers/tty/serial/8250/8250_port.c +++ b/drivers/tty/serial/8250/8250_port.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -1467,11 +1468,26 @@ void serial8250_em485_stop_tx(struct uart_8250_port *p) } EXPORT_SYMBOL_GPL(serial8250_em485_stop_tx); +static inline int __get_lsr(struct uart_8250_port *p) +{ + return serial_in(p, UART_LSR); +} + +static inline int __wait_for_empty(struct uart_8250_port *p, u64 timeout_us) +{ + int lsr; + + return readx_poll_timeout(__get_lsr, p, lsr, + (lsr & BOTH_EMPTY) == BOTH_EMPTY, + 0, timeout_us); +} + static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t) { struct uart_8250_em485 *em485; struct uart_8250_port *p; unsigned long flags; + enum hrtimer_restart restart = HRTIMER_NORESTART; em485 = container_of(t, struct uart_8250_em485, stop_tx_timer); p = em485->port; @@ -1479,13 +1495,27 @@ static enum hrtimer_restart serial8250_em485_handle_stop_tx(struct hrtimer *t) serial8250_rpm_get(p); spin_lock_irqsave(&p->port.lock, flags); if (em485->active_timer == &em485->stop_tx_timer) { + /* + * On 8250 without TEMT interrupt, check LSR state and + * restart timer if not empty yet. + */ + if (!(p->capabilities & UART_CAP_TEMT)) { + int ret = __wait_for_empty(p, 100); + + if (ret < 0) { + restart = HRTIMER_RESTART; + goto out; + } + } + p->rs485_stop_tx(p); em485->active_timer = NULL; em485->tx_stopped = true; } +out: spin_unlock_irqrestore(&p->port.lock, flags); serial8250_rpm_put(p); - return HRTIMER_NORESTART; + return restart; } static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec) @@ -1509,6 +1539,13 @@ static void __stop_tx_rs485(struct uart_8250_port *p) em485->active_timer = &em485->stop_tx_timer; start_hrtimer_ms(&em485->stop_tx_timer, p->port.rs485.delay_rts_after_send); + } else if (!(p->capabilities & UART_CAP_TEMT) && + __wait_for_empty(p, 100)) { + /* Short timer of 1us to check for clear fifos */ + ktime_t tim = ktime_set(0, 1000); + + em485->active_timer = &em485->stop_tx_timer; + hrtimer_start(&em485->stop_tx_timer, tim, HRTIMER_MODE_REL); } else { p->rs485_stop_tx(p); em485->active_timer = NULL; @@ -1531,11 +1568,15 @@ static inline void __stop_tx(struct uart_8250_port *p) /* * To provide required timeing and allow FIFO transfer, * __stop_tx_rs485() must be called only when both FIFO and - * shift register are empty. It is for device driver to enable - * interrupt on TEMT. + * shift register are empty. If 8250 port supports it, + * it is for device driver to enable interrupt on TEMT. + * Otherwise must loop-read until TEMT and THRE flags are set, + * which happens in __stop_tx_rs485() */ - if ((lsr & BOTH_EMPTY) != BOTH_EMPTY) - return; + if (p->capabilities & UART_CAP_TEMT) { + if ((lsr & BOTH_EMPTY) != BOTH_EMPTY) + return; + } __stop_tx_rs485(p); } -- 2.25.1