From patchwork Thu Apr 26 14:08:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Simek X-Patchwork-Id: 10366029 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1456E60225 for ; Thu, 26 Apr 2018 14:10:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 039252910D for ; Thu, 26 Apr 2018 14:10:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EC44729102; Thu, 26 Apr 2018 14:10:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B3EAA29102 for ; Thu, 26 Apr 2018 14:10:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=+MD5OXYQ9c3vpjNgAyHJfVqsAVUYEzU+ResrRQzemnw=; b=B8BharmrsMYUcKurxK2cIIrGef ruIu79wTP+U/+whYnRe+/Cz9Q99HWkzbEEDeYUlBDyAQN0jIpRs1qqOyfu8lvMuWIg7ZQ9nholATm LGqh4TY7ulJHrivQxCIL+t+cGRkCuDkr3aMr23LaxYxv8RpA/GAWAOJ6G2Z2D6+nGjv7Q3druri1p jk6e1DSeMi9KUPA/RBO5XZOa1L0kWL5BLV093n1pbDDv2z3qJDVy2VvH14zG51lWvQBhrt9IZVXSj lkCTqqncnymddTdmGJB8kmPCFpcg616oMTC0xjq3S7uO/Njv1tW4Yz8Hp6dUveNP8ZZIAY6zdIfr8 l8DY2Huw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fBhb7-0004Xa-W1; Thu, 26 Apr 2018 14:10:10 +0000 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fBhZg-0003qo-0D for linux-arm-kernel@lists.infradead.org; Thu, 26 Apr 2018 14:08:42 +0000 Received: by mail-wm0-x241.google.com with SMTP id 66so13150109wmd.3 for ; Thu, 26 Apr 2018 07:08:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=monstr-eu.20150623.gappssmtp.com; s=20150623; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=lXiRgNIlbs2ZhrTnuwv/Pzzy/qNw0VwwFxMvzSF1djc=; b=tsJqkxYmYxz0UGkkAEg7lLqeqJKG+KvH7nXJqPodAYw3CF36hc1C5xXvDvaohJyr/5 O9Zo+NbfHYhmmUboV0dJ5pJVtFmMtF+gpzmokvTQ8SZvjzsMyQYBkfx7cGc+TpuVtSij O/ICLI/07ZPD2tVTprH6rHjMF2Ul4PyyvaEFqPmJWDaGf6DMntrWNjP0NX76G6MwatTS E2LRpyEgyb4eRLl5MIloSCRElgCb/UVIELkiGItTlDGM2Kdtq3aNv0p4GtvGzVuxTypX o+cD+P4Q7Pno695zZ100Too/eHMyWCGNO0Cxm+Sl1K8P3z0gdusLDhTdY9mAG6Y+XtuB 2eIg== 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:in-reply-to:references; bh=lXiRgNIlbs2ZhrTnuwv/Pzzy/qNw0VwwFxMvzSF1djc=; b=SZTUQRLgi0K33Zgr3OLLXVwh++cy87v5aw28zg8lfhEvic+PCYzMMKjZH53htsp6Wh c+YcXingBK5wMnXG8floZV+SY8H3CoBPb2GLZUhHYj+m2CvyXQpyNCRx+6uLZekhMPpb PrNxsV2Mrh2/UQNOy/QG4qcDcDUedK038eK6QLXlo5L7vyf4t0Tagwvq9DC4UMufBA4o Ymh9IHvZGZch873m1/ygnqYbrgIu3woFdCr14EJ1SdxPz+zTcM91fH+OtLUovthRWTj9 49OXVtw3aXMTTvl7ury9VZnMLCPDCf2EMvLxAy5j/ym4DBNxH+a9HjXu1DOBMn+Xeg0i EoJA== X-Gm-Message-State: ALQs6tBb9QSQDW/zmgVR8g8fvNX9WdIPgY8hnBomQmuTj0i+6OKQyKcL 6o1uUDkC80/7+nXSoB4769311w== X-Google-Smtp-Source: AIpwx480BV/ztfitKnHUxarWHs2qHaTlgihptGsFL+362KU99m8E8/tWyyQ27N1bSmJOQC1mnQVvmQ== X-Received: by 10.28.69.86 with SMTP id s83mr17827737wma.156.1524751709774; Thu, 26 Apr 2018 07:08:29 -0700 (PDT) Received: from localhost (nat-35.starnet.cz. [178.255.168.35]) by smtp.gmail.com with ESMTPSA id y9-v6sm18480027wrh.63.2018.04.26.07.08.29 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Thu, 26 Apr 2018 07:08:29 -0700 (PDT) From: Michal Simek To: linux-kernel@vger.kernel.org, monstr@monstr.eu, gnomes@lxorguk.ukuu.org.uk, Alexander Graf Subject: [RFC PATCH 3/3] serial: uartps: Change uart ports allocation Date: Thu, 26 Apr 2018 16:08:19 +0200 Message-Id: <782bf2c500578db1a8ee327cc48ed905d3e50497.1524751696.git.michal.simek@xilinx.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: References: In-Reply-To: References: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180426_070840_082834_244C2D87 X-CRM114-Status: GOOD ( 20.72 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Greg Kroah-Hartman , linux-arm-kernel@lists.infradead.org, linux-serial@vger.kernel.org, Jiri Slaby MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP For ips which have alias algorightm all the tim using that alias and minor number. It means serial20 alias ends up as ttyPS20. If aliases are not setup for enabled core the first unused position is used but that's need to be checked if it is really empty because another instance doesn't need to be probed. of_alias_check_id() does that test. If alias pointing to different not compatible IP, it is free to use. Signed-off-by: Michal Simek --- drivers/tty/serial/xilinx_uartps.c | 96 ++++++++++++++++++++++++++---- 1 file changed, 85 insertions(+), 11 deletions(-) diff --git a/drivers/tty/serial/xilinx_uartps.c b/drivers/tty/serial/xilinx_uartps.c index ffb5b66a05b5..7c616b48c117 100644 --- a/drivers/tty/serial/xilinx_uartps.c +++ b/drivers/tty/serial/xilinx_uartps.c @@ -45,6 +45,9 @@ static int rx_timeout = 10; module_param(rx_timeout, uint, S_IRUGO); MODULE_PARM_DESC(rx_timeout, "Rx timeout, 1-255"); +/* The inuse bitfield is recording which IDs are free/used by the driver. */ +static unsigned long *inuse; + /* Register offsets for the UART. */ #define CDNS_UART_CR 0x00 /* Control Register */ #define CDNS_UART_MR 0x04 /* Mode Register */ @@ -1418,6 +1421,75 @@ static const struct of_device_id cdns_uart_of_match[] = { }; MODULE_DEVICE_TABLE(of, cdns_uart_of_match); +static int cdns_get_id(struct platform_device *pdev) +{ + int id; + + /* Look for a serialN alias */ + id = of_alias_get_id(pdev->dev.of_node, "serial"); + if (id < 0) { + dev_warn(&pdev->dev, + "No serial alias passed. Using the first free id\n"); + + /* + * Start with id 0 and check if there is no serial0 alias + * which points to device which is compatible with this driver. + * If alias exists then try next free position. + */ + id = 0; + + for (;;) { + dev_dbg(&pdev->dev, "Validate id %d\n", id); + id = find_next_zero_bit(inuse, id, + cdns_uart_uart_driver.nr); + + dev_dbg(&pdev->dev, "The empty id is %d\n", id); + /* Check if ID is empty */ + + if (of_alias_check_id(cdns_uart_of_match, + "serial", id)) { + dev_dbg(&pdev->dev, + "ID %d already taken - skipped\n", id); + /* Try next one */ + id++; + continue; + } + + /* It shouldn't happen anytime */ + if (id >= cdns_uart_uart_driver.nr) { + dev_dbg(&pdev->dev, + "ID %d is greater than NR of ports\n", + cdns_uart_uart_driver.nr); + return -ENXIO; + } + + /* Break the loop if bit is taken */ + if (!test_and_set_bit(id, inuse)) { + dev_dbg(&pdev->dev, + "Selected ID %d allocation passed\n", + id); + break; + } + dev_dbg(&pdev->dev, + "Selected ID %d allocation failed\n", id); + /* if taking bit fails then try next one */ + id++; + } + } else { + /* + * If alias was found there is no reason to check + * anything else + */ + if (test_and_set_bit(id, inuse)) { + dev_dbg(&pdev->dev, + "ID %d allocation failed\n", id); + return -EAGAIN; + } + } + + return id; +} + /** * cdns_uart_probe - Platform driver probe * @pdev: Pointer to the platform device structure @@ -1446,6 +1518,13 @@ static int cdns_uart_probe(struct platform_device *pdev) dev_err(&pdev->dev, "Failed to register driver\n"); return rc; } + + /* Create bitfield to record which ids are free */ + inuse = devm_kcalloc(&pdev->dev, + BITS_TO_LONGS(cdns_uart_uart_driver.nr), + sizeof(unsigned long), GFP_KERNEL); + if (!inuse) + return -ENOMEM; } cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data), @@ -1463,6 +1542,10 @@ static int cdns_uart_probe(struct platform_device *pdev) cdns_uart_data->quirks = data->quirks; } + id = cdns_get_id(pdev); + if (id < 0) + return id; + cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "pclk"); if (IS_ERR(cdns_uart_data->pclk)) { cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "aper_clk"); @@ -1515,16 +1598,6 @@ static int cdns_uart_probe(struct platform_device *pdev) &cdns_uart_data->clk_rate_change_nb)) dev_warn(&pdev->dev, "Unable to register clock notifier.\n"); #endif - /* Look for a serialN alias */ - id = of_alias_get_id(pdev->dev.of_node, "serial"); - if (id < 0) - id = 0; - - if (id >= CDNS_UART_NR_PORTS) { - dev_err(&pdev->dev, "Cannot get uart_port structure\n"); - rc = -ENODEV; - goto err_out_notif_unreg; - } /* At this point, we've got an empty uart_port struct, initialize it */ spin_lock_init(&port->lock); @@ -1583,10 +1656,10 @@ static int cdns_uart_probe(struct platform_device *pdev) return 0; err_out_pm_disable: + clear_bit(port->line, inuse); pm_runtime_disable(&pdev->dev); pm_runtime_set_suspended(&pdev->dev); pm_runtime_dont_use_autosuspend(&pdev->dev); -err_out_notif_unreg: #ifdef CONFIG_COMMON_CLK clk_notifier_unregister(cdns_uart_data->uartclk, &cdns_uart_data->clk_rate_change_nb); @@ -1618,6 +1691,7 @@ static int cdns_uart_remove(struct platform_device *pdev) #endif rc = uart_remove_one_port(&cdns_uart_uart_driver, port); port->mapbase = 0; + clear_bit(port->line, inuse); clk_disable_unprepare(cdns_uart_data->uartclk); clk_disable_unprepare(cdns_uart_data->pclk); pm_runtime_disable(&pdev->dev);