Received: by 10.192.165.148 with SMTP id m20csp2189076imm; Thu, 26 Apr 2018 07:13:49 -0700 (PDT) X-Google-Smtp-Source: AIpwx48/An+1eNt3isKTQtrZ96DTzqea4uipieqn8a6prc9vfuGjPky6arS0sCDovB3hjNBT2k4c X-Received: by 10.98.103.86 with SMTP id b83mr32755079pfc.76.1524752029266; Thu, 26 Apr 2018 07:13:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524752029; cv=none; d=google.com; s=arc-20160816; b=CbzBtwdCnI+x1xc2z1uc++tElzWVuL+H7zLlvs3YT1lfuRRqXfn/xRv01OQRuqLWm8 HfKwj0bsRXZGCTUN528gSBFdt/H45zU49MLiww/irD1oFaqhrQuXtyDtr7IvOp/reSPE 5Tvo8anuvvR+k13godfoYStbVinsvyu021T0FzH+3hWMwjwIoBZPbQJPpmfpYCD8+rvn Rc/Dyv1If6dmBFTRXG/46gtZVaFkkgPsnqZ+g4xZNQ1dFHSvzugFYX8Ih/Hnh9K+hSLW CGDh7Vot1dcTn5CzUeYXD1H6EgA9sHX0z6n/hgQVRW1+fYGxiCUjtt6HknNY6/uPKmB2 OIjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=lXiRgNIlbs2ZhrTnuwv/Pzzy/qNw0VwwFxMvzSF1djc=; b=JJFfpC8bBKVskalmaA7VdhC7wN+1iI6dB050jSIh0S3Qr20GQTF/4Bw2UW1vdITKUy sAo18ce5yaRciVrpPrA/ypnOe3CHFt+w8PSY2DAbsa1cWJimxV78gq5/tmonOUbAYAmM MJKDBWTIBzK3u08ZGhYx//59/IBFBPoBpxC/X/byRId99ha7c+F/q9RRXDCh8rBEz5I6 Bep0DYoV/8ABXGiddmJOieczX8x4A6vUz4ONE5/eQaBiCQkiOJf3TWiy/pKWliYnd68Z mK7UyDxBvLDxbar924/QWnPOaafUVIivYFvYO9hXLPLIdxPCnR42ZN8qe+SK95on9iKX O9qA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@monstr-eu.20150623.gappssmtp.com header.s=20150623 header.b=tsJqkxYm; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x9-v6si18960481pln.508.2018.04.26.07.13.35; Thu, 26 Apr 2018 07:13:49 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@monstr-eu.20150623.gappssmtp.com header.s=20150623 header.b=tsJqkxYm; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932162AbeDZOIt (ORCPT + 99 others); Thu, 26 Apr 2018 10:08:49 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:51056 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756542AbeDZOIb (ORCPT ); Thu, 26 Apr 2018 10:08:31 -0400 Received: by mail-wm0-f68.google.com with SMTP id t11so6347528wmt.0 for ; Thu, 26 Apr 2018 07:08:30 -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=keH5AuOCDLjQmhar0wNY7ZXLQqJVGeib5/cI9Ph6F2pKlrVkR0n3C/iDaMqmxySQ+1 Qh4FQHgN6pFyoel0OHslWloLokGfGtBJllfA5RxdQcxKBQlRmYAgepKGe/CVU0Fk5IMM wxawOJ/SC6o+L4YgetW9tK5rdCQRlXq/iqXXtZGUHOjTShv1RGQlYWq1kqmHXB56wqlP YGnA6xZ9bbrfwhNXpqPzICE9RLr+3tSTasxO+UH3STcdJB9qlIEYLhSQWbOoH5HUg3kE Y5G+npfI0Xctsr/AL/XXhaB0iWD1i/ov5uTZ3K8gB941XRhJIx2EsiO8tQ0ACZm6lSjT NcFQ== X-Gm-Message-State: ALQs6tCk2f7BiE+lgmGuBRJSsGMe9FvPFaxMhL66KGSG9//D6+pY7qyK sYuiEtiZByWj+KX86XrTB0rvrHB1 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 Cc: Jiri Slaby , linux-serial@vger.kernel.org, Greg Kroah-Hartman , linux-arm-kernel@lists.infradead.org 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: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); -- 2.17.0