From patchwork Wed Oct 18 09:17:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Crescent CY Hsieh X-Patchwork-Id: 735440 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0B5E7CDB482 for ; Wed, 18 Oct 2023 09:21:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230224AbjJRJVi (ORCPT ); Wed, 18 Oct 2023 05:21:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229987AbjJRJVC (ORCPT ); Wed, 18 Oct 2023 05:21:02 -0400 Received: from APC01-PSA-obe.outbound.protection.outlook.com (mail-psaapc01on2040.outbound.protection.outlook.com [40.107.255.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E073119B; Wed, 18 Oct 2023 02:20:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NBCkEyzqAsmdseXUxLMtueTriyisXNl029t9Y5DeftD5g0x9+sGFDSnMDkOgD2FCd4eiirqqof/UesuB1lLIeNsNq2DlzU8pEbVFntpCpRSytc96yA2TDzidt1Ot1qkpCm8L0M2Anymi+liavclMVwlAaQusuNNN0fr50+arZ8ZxqpFgS/MZQPfrI/rofYSldB1Bm1jdWE2ppGHrDO4aYtFJ47KkyAGWZRVkREO0rWVd28etgXblVKkuYqCifqCldU62CCpyb7NRoAT8DwXKLYlzp7/8nSbKdLk4lYwN3hPlMUlX/uXy2AEWCBtVjT4V1urT+wRV0VxnzS/+mIhoNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=NkbbBbfcEHv9MnXqSsE7CtXcIxgi6M7qgVEk7UIznBQ=; b=Zs41PM8F/yA/g8x0XFKlfAbM4vbNfXAlOaLIX8Wc3Dq4M7wYjldzRg7D+1rgDHBq15CAShK/37f5DkkqjrfEaG68pdrklXQUOGEmGlFRI4LyB726r1/sTtDlxJcUIectEuHaZMhvIIqD94pnBPNBns+PHMw+soaZbRgpMqckogXg7+EarMScy9c6Z81fJo1B2W/Yh/Rbev7jP5YYBNhkIdLIzjRZ2V9uNyGqyECAQ06W+1iJjkJ9q9SVpwnF2X3HuT4M/ob7IGjkdnHscHxcJ/zAXQ2W34pX2UpKKxFBk/rP1Pb8Fwb4/p2CjrNnSp2EIeNaE2TKKHT79PP3K2N4MA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=moxa.com; dmarc=pass action=none header.from=moxa.com; dkim=pass header.d=moxa.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=moxa.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=NkbbBbfcEHv9MnXqSsE7CtXcIxgi6M7qgVEk7UIznBQ=; b=i7dlSiGXfs3jN5gpvAnfOvhRiv7yh1LGo/ZH5H6t/pJiDU8XSq+0/m+5JWwqEIZQ7uE+aTU6jq7wh0OLvD6t8MDBZ4EkcDVNiyEcM+JXeag2bZOvFJ1p3rnkXl0N/z/oINciZZQ8U//bYpuTIJ44qJkqLgNm2YPOCKgnfFIqT8Y= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=moxa.com; Received: from PUZPR01MB5405.apcprd01.prod.exchangelabs.com (2603:1096:301:115::14) by SEZPR01MB4134.apcprd01.prod.exchangelabs.com (2603:1096:101:46::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6907.21; Wed, 18 Oct 2023 09:20:49 +0000 Received: from PUZPR01MB5405.apcprd01.prod.exchangelabs.com ([fe80::3f1:6c0e:dec3:714b]) by PUZPR01MB5405.apcprd01.prod.exchangelabs.com ([fe80::3f1:6c0e:dec3:714b%5]) with mapi id 15.20.6886.034; Wed, 18 Oct 2023 09:20:49 +0000 From: Crescent CY Hsieh To: Greg Kroah-Hartman , Jiri Slaby Cc: linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org, Crescent CY Hsieh Subject: [PATCH v3 5/6] tty: serial: 8250: Fix MOXA RS422/RS485 PCIe boards not work by default Date: Wed, 18 Oct 2023 17:17:38 +0800 Message-Id: <20231018091739.10125-6-crescentcy.hsieh@moxa.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231018091739.10125-1-crescentcy.hsieh@moxa.com> References: <20231018091739.10125-1-crescentcy.hsieh@moxa.com> X-ClientProxiedBy: TYCP286CA0015.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:26c::19) To PUZPR01MB5405.apcprd01.prod.exchangelabs.com (2603:1096:301:115::14) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PUZPR01MB5405:EE_|SEZPR01MB4134:EE_ X-MS-Office365-Filtering-Correlation-Id: a9a918b2-16d6-424d-73af-08dbcfbb8449 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: QwYJW3cNLi+BXXY2t3K3I5b9dxn9Recd8A5ldzeIsIHQZifKJGfxDcbXz16r3JClZivQebqMTwMqUXoxmBQ6dIo6FFVPukX3Ry5+qelbIm0PGFikOrluMW/mYjKrNtukBhXiBMOAXZySj2szsG59WIs8Ogmtcg8tJjhQ+J1W0PzyeP6PB2ZukT/rbGgfqS0URBT6viapl2pa1yRm8Xmq7coWtaAIW4d5OgC+EvkmIuv/zomYGOM0tPlxDRdBUUWALQw15OL6xQEsR/2ALh1jMiwiwaHzrQN6wwM/pY6YKINS8teoQGUgVRxxZyvoLBnKtfJFd6vITsVaQKpWAsNcqBWMqdsV8Wmeg8vIA+AYQNf7W4VcygWKQ3jx2u2yUzvOLpvXLFxcZBd1JeaZtVmmykGUVjV4R+RL7jjMv0fw5FhmJW5uqcYBw344ump8yJmbkYus5+FjsLTPFc7CLQ6QSxNMDAoo8Y7JncZJPkxncwXXAUnWDQqpumes/nEB/GBhy0WO2bww33a77REY/ika1mqJ8k/MjmHPUcC5L+Mhc6RwADiD4OYqt3KCu5tTc294rTFk5A8kipNs3VokGezxiVW4e9GWKmUKPjA8VPBODYDaL1StgjRLgDIpmSVZTdx+ X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PUZPR01MB5405.apcprd01.prod.exchangelabs.com; PTR:; CAT:NONE; SFS:(13230031)(346002)(366004)(396003)(136003)(376002)(39860400002)(230922051799003)(64100799003)(186009)(451199024)(1800799009)(107886003)(38350700005)(6666004)(6506007)(52116002)(6512007)(6486002)(508600001)(66476007)(110136005)(316002)(26005)(36756003)(1076003)(66556008)(66946007)(2616005)(41300700001)(38100700002)(5660300002)(8936002)(4326008)(8676002)(2906002)(86362001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: SFr/bINUc0mU/C23FiAv6pS0z9lYlFPELmTeQqVMqCE8thIfEP0cFtmzN1aoKL9Q2wdHZamSWGeiNdQkgSMFaxYUlRwX+0SYAEUTk/iBAKFEcb0YMbTpwxHm9f42ZuwkCkubJ97v4B5ZKNt5ni5Zj+g7p+uySVK1o5LhrtyIHgd6LCSQ6tsy51jks74lZl7nSQkMqdpUT8KCfhbFNKKckw2QffDouaavZJLWvAXGNv6gKmwY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D6b5KFFVv0LtY7nTvCDCnEdXlQ0lwtNzPDWwP6GAfM2WSMIf3KfjplOYcZJcl3LgVPIM+r8ZKZGcjunaTSILaBXo76USQGKx5BFgKExv7fnqCsQDz1taessJ42/FWuyRTWP4sbtKAXQOtMtOOM9tl9JqNdzvIsbiDHfscIJc7sjyvi9M2uh1BHHLocpIBhkKX5vU6qgccwftldctfb5xynk2YQyIsAx3okb6QkCCI8j8Ndsm00CfO6SnCMpeOZqv2kcCaOhf1znwIgzKK70yYkyZ4Ssvq2VwS9l9kCobWqPtRQtkge8sAAeg== X-OriginatorOrg: moxa.com X-MS-Exchange-CrossTenant-Network-Message-Id: a9a918b2-16d6-424d-73af-08dbcfbb8449 X-MS-Exchange-CrossTenant-AuthSource: PUZPR01MB5405.apcprd01.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Oct 2023 09:20:48.9647 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 5571c7d4-286b-47f6-9dd5-0aa688773c8e X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: swszrXqeeOfBLjpc2E8Vwf0XgF9WgAlh7YfiX9L8W7F8QObFRSnaol2Ij1yC7GFXbtHFcDl++2jQ0NAJPOPOKlTYo/oBA5rq61zJ4E/Mxjk= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SEZPR01MB4134 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org MOXA PCIe RS422/RS485 boards will not function by default because of the initial default serial interface of all MOXA PCIe boards is set to RS232. This patch fixes the problem above by setting the initial default serial interface to RS422 for those MOXA RS422/RS485 PCIe boards. Signed-off-by: Crescent CY Hsieh Reviewed-by: Jiri Slaby --- drivers/tty/serial/8250/8250_pci.c | 52 ++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/drivers/tty/serial/8250/8250_pci.c b/drivers/tty/serial/8250/8250_pci.c index b2be3783f..29a28e72b 100644 --- a/drivers/tty/serial/8250/8250_pci.c +++ b/drivers/tty/serial/8250/8250_pci.c @@ -1968,6 +1968,12 @@ pci_sunix_setup(struct serial_private *priv, #define MOXA_GPIO_PIN2 BIT(2) +#define MOXA_RS232 0x00 +#define MOXA_RS422 0x01 +#define MOXA_RS485_4W 0x0B +#define MOXA_RS485_2W 0x0F +#define MOXA_UIR_OFFSET 0x04 + static bool pci_moxa_is_mini_pcie(unsigned short device) { if (device == PCI_DEVICE_ID_MOXA_CP102N || @@ -1981,13 +1987,59 @@ static bool pci_moxa_is_mini_pcie(unsigned short device) return false; } +/* + * The second digit of the MOXA PCIe device ID in hexadecimal indicates + * which serial interface modes this board supports: + * + * 0x*0** - RS232 + * 0x*1** - RS232/RS422/RS485 + * 0x*3** - RS422/RS485 + * 0x*6** - RS232 + */ +static bool pci_moxa_match_second_digit(unsigned short device, + unsigned short pattern) +{ + return (device & 0x0F00) == pattern; +} + +static int pci_moxa_set_interface(const struct pci_dev *dev, + unsigned int port_idx, + u8 mode) +{ + resource_size_t iobar_addr = pci_resource_start(dev, 2); + resource_size_t UIR_addr = iobar_addr + MOXA_UIR_OFFSET + port_idx / 2; + u8 val; + + val = inb(UIR_addr); + + if (port_idx % 2) { + val &= 0x0F; + val |= mode << 4; + } else { + val &= 0xF0; + val |= mode; + } + outb(val, UIR_addr); + + return 0; +} + static int pci_moxa_init(struct pci_dev *dev) { unsigned short device = dev->device; resource_size_t iobar_addr = pci_resource_start(dev, 2); + unsigned int i; unsigned int num_ports = (device & 0x00F0) >> 4; u8 val; + /* + * For the device IDs of MOXA PCIe boards match the pattern 0x*3**, + * the initial default serial interface mode should be set to RS422. + */ + if (pci_moxa_match_second_digit(device, 0x0300)) { + for (i = 0; i < num_ports; ++i) + pci_moxa_set_interface(dev, i, MOXA_RS422); + } /* * Enable hardware buffer to prevent break signal output when system boots up. * This hardware buffer is only supported on Mini PCIe series.