Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp4041757rwi; Sat, 22 Oct 2022 04:19:37 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5wPi7eRhznvuYZ++04IOaIT6IwhLqVRO5JW245ZkZDJ3IRfnfx7y6x/wKl5T2LIq3xG6Kv X-Received: by 2002:aa7:9dcb:0:b0:565:89a8:c708 with SMTP id g11-20020aa79dcb000000b0056589a8c708mr24341023pfq.4.1666437577708; Sat, 22 Oct 2022 04:19:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666437577; cv=none; d=google.com; s=arc-20160816; b=WJlyBUUEr11JEkVaL+1Lcxd5/X4nKxH83w0zCZSs9BGB8R1qidNttns386RZ0FzYtV Up5NROBf7wSRrKCjoYZQ1+teeOr9YqNJOR6xDhnL8GWP5sZWUk5Sg1gJ44FI8VKVCEXg O1cf3ikUNt8XD2U9iLHENMxmm5QJt46fcGyC76GXdMtpgZ00tDfWG3BpLf2OnuR5Sc5u 1ghiKDZQLUa8n1JCxSAtKQsxwyjtOS0CobDyI3RfgBDO/xBAsP7rlrO45xL9SS/Uhm0h +DJCSelDzcoBQWGRVvfhUBgXw4MefhiG/0x5BLur/2Xreya7rCijmfjewYIXq19N7aco HQSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=XER5dUfEoE/M7Y9nlLyPhQerTpPsnslHbLShDqRcJvs=; b=S8Rn4KEWrsA0gQd7N/EO00ileH95T4pofmiGHpAO539rvqIxRjz2r8+Jv+clakK0oY tLWqNKipdAJhkMNnx0bHCF67s9aa1kuKMbt+GWr/HmPl05b+QkxbbxKjYmzGM0RIpXUG VzwrR8hH1VSLEYX1iaMfufpvKY0OTxiZ9UUXcJs03x7Xa6mWmjKBVJlB6dMAQbLM37l/ ohhoqdaMZCUNCFWyJ7RmrdqE10nZQNZzTmCKPJ9sg7REJbUs7YDBA95Qfvo6kJl90quS iZNOUBItyUa+KIlzKB3zxeGYaexh0X+JfrpjrcQKD+rH/h1vNY1uPQQzNKDmzkpKB/NJ v5Lg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GzpNEQPn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b13-20020a6541cd000000b00439ce36e9d3si28474146pgq.570.2022.10.22.04.19.25; Sat, 22 Oct 2022 04:19:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GzpNEQPn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230440AbiJVKqI (ORCPT + 99 others); Sat, 22 Oct 2022 06:46:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230400AbiJVKpl (ORCPT ); Sat, 22 Oct 2022 06:45:41 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7D50981F6; Sat, 22 Oct 2022 03:03:32 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 2277C60AFF; Sat, 22 Oct 2022 07:37:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3021FC433C1; Sat, 22 Oct 2022 07:37:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666424269; bh=1mViT2c5UD5jcinRno6IL7qpXr7J0qcTYNSXnwD/3wk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GzpNEQPnhb+Noa+3eHorNZIFNQDEIj3qHfpBeVCPZ0d1ofZ0YdL78R9wlKctBHNfB W40o6gYlFzHc0paakyFh1s5aidDiVrR4OrHvYMPkgTp+UDeB4h0KO2u5TcjWdxWGxy eoh1L2zA833V+dE2Git/jqPp2e3bxqrsCL2MaYrQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Marek Vasut , =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , Lukas Wunner Subject: [PATCH 5.19 083/717] serial: stm32: Deassert Transmit Enable on ->rs485_config() Date: Sat, 22 Oct 2022 09:19:22 +0200 Message-Id: <20221022072430.021107652@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221022072415.034382448@linuxfoundation.org> References: <20221022072415.034382448@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Lukas Wunner commit adafbbf6895eb0ce41a313c6ee68870ab9aa93cd upstream. The STM32 USART can control RS-485 Transmit Enable in hardware. Since commit 7df5081cbf5e ("serial: stm32: Add RS485 RTS GPIO control"), it can alternatively be controlled in software. That was done to allow RS-485 even if the RTS pin is unavailable because it's pinmuxed to a different function. However the commit neglected to deassert Transmit Enable upon invocation of the ->rs485_config() callback. Fix it. Avoid forward declarations by moving stm32_usart_tx_empty(), stm32_usart_rs485_rts_enable() and stm32_usart_rs485_rts_disable() further up in the driver. Fixes: 7df5081cbf5e ("serial: stm32: Add RS485 RTS GPIO control") Cc: stable@vger.kernel.org # v5.9+ Cc: Marek Vasut Reviewed-by: Ilpo Järvinen Signed-off-by: Lukas Wunner Link: https://lore.kernel.org/r/6059eab35dba394468335ef640df8b0050fd9dbd.1662886616.git.lukas@wunner.de Signed-off-by: Greg Kroah-Hartman --- drivers/tty/serial/stm32-usart.c | 100 ++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 47 deletions(-) --- a/drivers/tty/serial/stm32-usart.c +++ b/drivers/tty/serial/stm32-usart.c @@ -62,6 +62,53 @@ static void stm32_usart_clr_bits(struct writel_relaxed(val, port->membase + reg); } +static unsigned int stm32_usart_tx_empty(struct uart_port *port) +{ + struct stm32_port *stm32_port = to_stm32_port(port); + const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; + + if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC) + return TIOCSER_TEMT; + + return 0; +} + +static void stm32_usart_rs485_rts_enable(struct uart_port *port) +{ + struct stm32_port *stm32_port = to_stm32_port(port); + struct serial_rs485 *rs485conf = &port->rs485; + + if (stm32_port->hw_flow_control || + !(rs485conf->flags & SER_RS485_ENABLED)) + return; + + if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { + mctrl_gpio_set(stm32_port->gpios, + stm32_port->port.mctrl | TIOCM_RTS); + } else { + mctrl_gpio_set(stm32_port->gpios, + stm32_port->port.mctrl & ~TIOCM_RTS); + } +} + +static void stm32_usart_rs485_rts_disable(struct uart_port *port) +{ + struct stm32_port *stm32_port = to_stm32_port(port); + struct serial_rs485 *rs485conf = &port->rs485; + + if (stm32_port->hw_flow_control || + !(rs485conf->flags & SER_RS485_ENABLED)) + return; + + if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { + mctrl_gpio_set(stm32_port->gpios, + stm32_port->port.mctrl & ~TIOCM_RTS); + } else { + mctrl_gpio_set(stm32_port->gpios, + stm32_port->port.mctrl | TIOCM_RTS); + } +} + static void stm32_usart_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE, u32 delay_DDE, u32 baud) { @@ -145,6 +192,12 @@ static int stm32_usart_config_rs485(stru stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); + /* Adjust RTS polarity in case it's driven in software */ + if (stm32_usart_tx_empty(port)) + stm32_usart_rs485_rts_disable(port); + else + stm32_usart_rs485_rts_enable(port); + return 0; } @@ -460,42 +513,6 @@ static void stm32_usart_tc_interrupt_dis stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TCIE); } -static void stm32_usart_rs485_rts_enable(struct uart_port *port) -{ - struct stm32_port *stm32_port = to_stm32_port(port); - struct serial_rs485 *rs485conf = &port->rs485; - - if (stm32_port->hw_flow_control || - !(rs485conf->flags & SER_RS485_ENABLED)) - return; - - if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { - mctrl_gpio_set(stm32_port->gpios, - stm32_port->port.mctrl | TIOCM_RTS); - } else { - mctrl_gpio_set(stm32_port->gpios, - stm32_port->port.mctrl & ~TIOCM_RTS); - } -} - -static void stm32_usart_rs485_rts_disable(struct uart_port *port) -{ - struct stm32_port *stm32_port = to_stm32_port(port); - struct serial_rs485 *rs485conf = &port->rs485; - - if (stm32_port->hw_flow_control || - !(rs485conf->flags & SER_RS485_ENABLED)) - return; - - if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { - mctrl_gpio_set(stm32_port->gpios, - stm32_port->port.mctrl & ~TIOCM_RTS); - } else { - mctrl_gpio_set(stm32_port->gpios, - stm32_port->port.mctrl | TIOCM_RTS); - } -} - static void stm32_usart_transmit_chars_pio(struct uart_port *port) { struct stm32_port *stm32_port = to_stm32_port(port); @@ -738,17 +755,6 @@ static irqreturn_t stm32_usart_threaded_ return IRQ_HANDLED; } -static unsigned int stm32_usart_tx_empty(struct uart_port *port) -{ - struct stm32_port *stm32_port = to_stm32_port(port); - const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; - - if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC) - return TIOCSER_TEMT; - - return 0; -} - static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl) { struct stm32_port *stm32_port = to_stm32_port(port);