From patchwork Thu Dec 1 04:51:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kumaravel Thiagarajan X-Patchwork-Id: 28195 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp75263wrr; Wed, 30 Nov 2022 20:53:02 -0800 (PST) X-Google-Smtp-Source: AA0mqf5t4oLkYGiIDfiVhWLwc061tyH8RyLoWnNkJk2LHEbx6/9TQ8YBPvPH7/AUOWn69aZxZAmd X-Received: by 2002:a17:906:2288:b0:7bf:888b:f039 with SMTP id p8-20020a170906228800b007bf888bf039mr9831527eja.429.1669870382550; Wed, 30 Nov 2022 20:53:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669870382; cv=none; d=google.com; s=arc-20160816; b=f8rY16nD12jBhbKlXJZez5GTg4NpMlEcqd+WZ9duJOMJAIQ3xxWqJLps8JpIRt+Vym 2sh7Wr6aPKeZmmmEn0P175eJBRgEkxfHyPj31oWbYi4+XP4D25dP8TtjOaZNjeCaj63/ lXbWvcAHhRRiC6JFa4ID2ca3WS+rYpgikLhYFpd4VxU5PL/oKN/wY8JeQCdHHLJqAPlc Nrl+P2mPU5UTTpJ/MW1/Ld1qTQq6oYGaeH8gXF5m5Y5l3PJdYWLsXy5bY11RfO/x6lsa niObfc97hxLWgcdrvE+ueutFxGm/KA4lvlozcKceRljGS6mWtE4i02+11Wy28EjpqNJh 05bA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=1thaZUowjUjU+Zj2+csFjUyiFIM4YejTQgTcCTPC0W8=; b=ljQVMXJuQ7KZ3nbg0f4YrCXsZZ+mA4sVV6we7BDomYvvHg2NXmQPJVd6suuAWd30Bz ZxAVBhzPa/5kPIFlTMqRb+zIgXtJ1SkwcH78A5i82h2MNnBOm50GW9nFiwe+UEvQcNFU pAQvqy7M7ukS0R2I7i+8HjuiUMIHnKJOBUx3OVm+GqIpg5UAgqwn1xGiyabxpvv6ysKz LUJyqk49tskv+qRiH5dI/KaMz7FA81BSn/ukRJjperRJWOcapMPhgivwCMb7cZ5m3iyR wA/ZERXndz8N9xNquE8qTG6nrIjG0xyrxpe6/+KEM26ZHgd57Tr230nR11wn/8WdpRaE VUTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=JQqZEgm+; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w14-20020a05640234ce00b004639a46d725si3244287edc.31.2022.11.30.20.52.39; Wed, 30 Nov 2022 20:53:02 -0800 (PST) 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=@microchip.com header.s=mchp header.b=JQqZEgm+; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229736AbiLAEv6 (ORCPT + 99 others); Wed, 30 Nov 2022 23:51:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229513AbiLAEvf (ORCPT ); Wed, 30 Nov 2022 23:51:35 -0500 Received: from esa.microchip.iphmx.com (esa.microchip.iphmx.com [68.232.154.123]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C8A39FA98; Wed, 30 Nov 2022 20:51:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1669870293; x=1701406293; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pwr8pzAseXIeubllSBB96HA4PC9HFFfLLbN91MIJKmg=; b=JQqZEgm+/Ih9k8/idwJ+xrt+fsde8oDf8ckLJ1e2ZgVQzOb3eu2NMaVG DG/a0cSdvwXB3RY3zgYmh7y0vLhem9RavNVLeGxDti+xl3e+GGYmamzQL hKe5xZg//Rw0OiBnKYQxZ8OmnZUp5XNxIFqFLriGhz/afOEwfTrZ2S6RG ++AcO7f/wj7wG9FOEpK9VoujfD1nHhzujaW38ktOuOCPgyL3oXHiGv3u/ YjkxdF84iqOQQaRQ4ATKih6q12yIphEpFeYD7FCXWhCamuHEYgvk5UIFF rvRt3s22I6LqpZa6MPHVV1avinTaYqj3ASJB71qEM01RUWecdbY2HwFDw g==; X-IronPort-AV: E=Sophos;i="5.96,207,1665471600"; d="scan'208";a="185988780" Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa4.microchip.iphmx.com with ESMTP/TLS/AES256-SHA256; 30 Nov 2022 21:51:32 -0700 Received: from chn-vm-ex02.mchp-main.com (10.10.85.144) by chn-vm-ex01.mchp-main.com (10.10.85.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.12; Wed, 30 Nov 2022 21:51:29 -0700 Received: from CHE-LT-UNGSOFTWARE.microchip.com (10.10.115.15) by chn-vm-ex02.mchp-main.com (10.10.85.144) with Microsoft SMTP Server id 15.1.2507.12 via Frontend Transport; Wed, 30 Nov 2022 21:51:22 -0700 From: Kumaravel Thiagarajan To: , CC: , , , , , , , , , , , , , , , , , Tharun Kumar P Subject: [PATCH v6 tty-next 3/4] serial: 8250_pci1xxxx: Add RS485 support to quad-uart driver Date: Thu, 1 Dec 2022 10:21:45 +0530 Message-ID: <20221201045146.1055913-4-kumaravel.thiagarajan@microchip.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221201045146.1055913-1-kumaravel.thiagarajan@microchip.com> References: <20221201045146.1055913-1-kumaravel.thiagarajan@microchip.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750986006019882057?= X-GMAIL-MSGID: =?utf-8?q?1750986006019882057?= pci1xxxx uart supports RS485 mode of operation in the hardware with auto-direction control with configurable delay for releasing RTS after the transmission. This patch adds support for the RS485 mode. Co-developed-by: Tharun Kumar P Signed-off-by: Tharun Kumar P Signed-off-by: Kumaravel Thiagarajan Reviewed-by: Ilpo Järvinen --- Changes in v6: - Modified datatype of delay_in_baud_periods to u64 to avoid overflows Changes in v5: - Removed unnecessary assignments - Corrected styling issues in comments Changes in v4: - No Change Changes in v3: - Remove flags sanitization in driver which is taken care in core Changes in v2: - move pci1xxxx_rs485_config to a separate patch with pci1xxxx_rs485_supported. --- drivers/tty/serial/8250/8250_pci1xxxx.c | 49 +++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/drivers/tty/serial/8250/8250_pci1xxxx.c b/drivers/tty/serial/8250/8250_pci1xxxx.c index d5037e76b636..7585066d6baf 100644 --- a/drivers/tty/serial/8250/8250_pci1xxxx.c +++ b/drivers/tty/serial/8250/8250_pci1xxxx.c @@ -145,6 +145,53 @@ static void pci1xxxx_set_divisor(struct uart_port *port, unsigned int baud, port->membase + UART_BAUD_CLK_DIVISOR_REG); } +static int pci1xxxx_rs485_config(struct uart_port *port, + struct ktermios *termios, + struct serial_rs485 *rs485) +{ + u32 clock_div = readl(port->membase + UART_BAUD_CLK_DIVISOR_REG); + u64 delay_in_baud_periods; + u32 baud_period_in_ns; + u32 data = 0; + + /* + * pci1xxxx's uart hardware supports only RTS delay after + * Tx and in units of bit times to a maximum of 15 + */ + if (rs485->flags & SER_RS485_ENABLED) { + data = ADCL_CFG_EN | ADCL_CFG_PIN_SEL; + + if (!(rs485->flags & SER_RS485_RTS_ON_SEND)) + data |= ADCL_CFG_POL_SEL; + + if (rs485->delay_rts_after_send) { + baud_period_in_ns = + FIELD_GET(BAUD_CLOCK_DIV_INT_MSK, clock_div) * + UART_BIT_SAMPLE_CNT; + delay_in_baud_periods = + rs485->delay_rts_after_send * NSEC_PER_MSEC / + baud_period_in_ns; + delay_in_baud_periods = + min_t(u64, delay_in_baud_periods, + FIELD_MAX(ADCL_CFG_RTS_DELAY_MASK)); + data |= FIELD_PREP(ADCL_CFG_RTS_DELAY_MASK, + delay_in_baud_periods); + rs485->delay_rts_after_send = + baud_period_in_ns * delay_in_baud_periods / + NSEC_PER_MSEC; + } + } + writel(data, port->membase + ADCL_CFG_REG); + return 0; +} + +static const struct serial_rs485 pci1xxxx_rs485_supported = { + .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | + SER_RS485_RTS_AFTER_SEND, + .delay_rts_after_send = 1, + /* Delay RTS before send is not supported */ +}; + static int pci1xxxx_setup(struct pci1xxxx_8250 *priv, struct uart_8250_port *port, int port_idx) { @@ -155,6 +202,8 @@ static int pci1xxxx_setup(struct pci1xxxx_8250 *priv, port->port.set_termios = serial8250_do_set_termios; port->port.get_divisor = pci1xxxx_get_divisor; port->port.set_divisor = pci1xxxx_set_divisor; + port->port.rs485_config = pci1xxxx_rs485_config; + port->port.rs485_supported = pci1xxxx_rs485_supported; ret = serial8250_pci_setup_port(priv->pdev, port, 0, port_idx * 256, 0); if (ret < 0)