Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp2537791rdb; Wed, 21 Feb 2024 10:35:48 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX/wmcn1wXSJZ3kAiG1olp5pe6cibAAYYNp0mxhI+wc1ik6OzK95ozBeL0wz8kcE6KJUYnTyAbChR9CYfaKNl16yb9+8fGgyGPXcmvGpw== X-Google-Smtp-Source: AGHT+IFtyIxUtP+SUrUymznjwJfDuGUTwySN00JLsIA6gBCt+WYFjSvew19XmGsyrw7WF/gPjIYs X-Received: by 2002:a17:906:35d3:b0:a3f:2f67:4844 with SMTP id p19-20020a17090635d300b00a3f2f674844mr2303602ejb.46.1708540547903; Wed, 21 Feb 2024 10:35:47 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708540547; cv=pass; d=google.com; s=arc-20160816; b=aIzpd1phqu+bw6oBgWzpJRU/PWdsVef9FcM9jce8Vz6Gd8hR7daFT/+2FMiPogHYDd E3Lk7DYb54++7jku95g32rbmnAZzy0O9jM1qnB0lWMXV6LaUKuVN/mPmOIHqji0r0uOA SneTbIgttZmFSev2ho+wlk88PuagpCgzaJHUZIS0CFEWSo2sm6dFJ4g7ZtTYtatIZ8xG RW2vNSbQI5BNk6yiY1TzyUnC2SciEB9aVlnQLDTgQjfHEIh0k/Xjy8ZfWnz8rLXbCbW3 BD6zKuBj6ST0HvS1YomeFvGmW/Mf3O7drQth5w1XZ20kQXIuudKhIeJIjICEvfXGVUa+ yarQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=5K/LWQfk0tTGf8JuWe99L3+EsjmvtZKgszkGqtxWAXo=; fh=yrE9O525sLNdIXC06sJBIlRUjYfWNm6imQ0cf3A93gQ=; b=c2tWG2/KfqENkcBPS+AEZTxw14GkM+LC/hrd4xvLZJkYMbXz/0GTSmo8OfXcu47YiS 7Cb/GaEsJsj78S3WvFwd8gMU2vr/1+wiNuvGsG/bAtAghn3jiY9CfXcWNONesgCnRhba RkfRLe0QV0a1S4KKg4kA8NlvxSL+VbKxEZmvALUbY7+sQqd8e4RoRCMjkWPmMmOJGR2v ADmkxkuZmc1dmT9KHR205p+pjt8lV7OolmWEh3Zn+069DlaA1olNfkYZn4oLFXVmkVr4 8l2NviOn+U1hBu6VEcGYsNT7S99dVZCrmlk7ZedmYV5smu8RSrAMdZ7jJmo0ypcEqAXg 7x5Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=K1C5Ay0C; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-75306-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75306-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id w3-20020a170906b18300b00a3ea1132f8asi2674700ejy.661.2024.02.21.10.35.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 10:35:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75306-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=K1C5Ay0C; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-75306-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75306-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 72B9C1F25A9F for ; Wed, 21 Feb 2024 18:35:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AF85F86625; Wed, 21 Feb 2024 18:35:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="K1C5Ay0C" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C8FDC8564D; Wed, 21 Feb 2024 18:34:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708540500; cv=none; b=sOZ2bI8sFvN57/w4CANbwm31jRNBEVUzFbAeMQwMu9LCNLB5CVRpEK4erHCqPdU+YyNmXAxM5iG2+XajbeB48xsDdGQf9M3A2HNtozMfUa754/CEbG+FT62pKyQXYOUaxhm+Oxdj7x0N81uE6/vF/YHnDXxBspQ0e15lfy9R9XM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708540500; c=relaxed/simple; bh=I4lO6MagkDaFdZKLWh5rqmLxpQuCDQmwf3i8KwokS+U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pCbPQZT+VL/ROtQCG7f9G94CWKAyJ+v8krYeEV5L930oY/fwvWStn2W2gKCg+eRA0lC42FlI9ASZM1CNUzS1BJY17pUjLYSuhSiNEvWMyq8eM2ytL4bDUMULICQuh5dwWm3785bcYjtlLiB1lrQHTJAYixiloUHBQSdjm7PrrqY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=K1C5Ay0C; arc=none smtp.client-ip=192.198.163.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708540499; x=1740076499; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=I4lO6MagkDaFdZKLWh5rqmLxpQuCDQmwf3i8KwokS+U=; b=K1C5Ay0CBtO3If/MXpF0MNUzm3A/coy+m5HOiloBHNadC9w8DMf2YYBw 3XciZn7Dqsnrt4J4ebl6yLsm6+d0/lWkMPy+gPV4dpbKexCoRcVencs4Y UoFg/9VI1DbBa/CRyxYrlhTk0lvDZ1fI48yVCcHrsfGS5JYp8qfQH0q7n dTOtizXmEEWvFcym4+Hc0zdMv6YR9anoJBSgF9j1bCRRpdVQpery4C6zP YC8HnobtZjZFju+u0OxSPDT7sWH9ZCnuhN6xxyGSfanDdYjayfuzRK+vI 9tq2NORBWlcY7xSqvXeOQSIFU2ZjEAH3Bz4GnBugEhOn+OnCuUNr0RR5N A==; X-IronPort-AV: E=McAfee;i="6600,9927,10991"; a="28164725" X-IronPort-AV: E=Sophos;i="6.06,176,1705392000"; d="scan'208";a="28164725" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Feb 2024 10:34:55 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10991"; a="936684741" X-IronPort-AV: E=Sophos;i="6.06,176,1705392000"; d="scan'208";a="936684741" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga001.fm.intel.com with ESMTP; 21 Feb 2024 10:34:47 -0800 Received: by black.fi.intel.com (Postfix, from userid 1003) id DB5503D3; Wed, 21 Feb 2024 20:34:46 +0200 (EET) From: Andy Shevchenko To: Greg Kroah-Hartman , Andy Shevchenko , Thomas Gleixner , linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-aspeed@lists.ozlabs.org, linux-rpi-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-tegra@vger.kernel.org Cc: Jiri Slaby , Joel Stanley , Andrew Jeffery , Florian Fainelli , Ray Jui , Scott Branden , Broadcom internal kernel review list , Al Cooper , =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , Paul Cercueil , Vladimir Zapolskiy , Thierry Reding , Jonathan Hunter , Kunihiko Hayashi , Masami Hiramatsu Subject: [PATCH v1 03/14] serial: port: Introduce a common helper to read properties Date: Wed, 21 Feb 2024 20:31:19 +0200 Message-ID: <20240221183442.4124354-4-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.43.0.rc1.1.gbec44491f096 In-Reply-To: <20240221183442.4124354-1-andriy.shevchenko@linux.intel.com> References: <20240221183442.4124354-1-andriy.shevchenko@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Several serial drivers want to read the same or similar set of the port properties. Make a common helper for them. Signed-off-by: Andy Shevchenko --- drivers/tty/serial/serial_port.c | 135 +++++++++++++++++++++++++++++++ include/linux/serial_core.h | 1 + 2 files changed, 136 insertions(+) diff --git a/drivers/tty/serial/serial_port.c b/drivers/tty/serial/serial_port.c index 88975a4df306..111e1f25fbc6 100644 --- a/drivers/tty/serial/serial_port.c +++ b/drivers/tty/serial/serial_port.c @@ -8,7 +8,10 @@ #include #include +#include +#include #include +#include #include #include @@ -82,6 +85,138 @@ void uart_remove_one_port(struct uart_driver *drv, struct uart_port *port) } EXPORT_SYMBOL(uart_remove_one_port); +/** + * uart_read_port_properties - read firmware properties of the given UART port + * @port: corresponding port + * @use_defaults: apply defaults or validate the values + * + * The following device properties are supported: + * - clock-frequency (optional) + * - fifo-size (optional) + * - no-loopback-test (optional) + * - reg-shift (defaults may apply) + * - reg-offset (value may be validated) + * - reg-io-width (defaults may apply or value may be validated) + * - interrupts (OF only) + * - serial [alias ID] (OF only) + * + * If the port->dev is of struct platform_device type the interrupt line + * will be retrieved via platform_get_irq() call against that device. + * Otherwise it will be assigned by fwnode_irq_get() call. In both cases + * the index 0 of the resource is used. + * + * The caller is responsible to initialize the following fields of the @port + * ->dev (must be valid) + * ->flags + * ->iotype (if @use_defaults is false) + * ->mapbase + * ->mapsize + * ->regshift (if @use_defaults is false) + * before calling this function. Alternatively the above mentioned fields + * may be zeroed, in such case the only ones, that have associated properties + * found, will be set to the respective values. + * + * When @use_defaults is true and the respective property is not found + * the following values will be applied: + * ->iotype = UPIO_MEM + * ->regshift = 0 + * In this case IRQ must be provided, otherwise an error will be returned. + * + * The ->irq, ->mapbase, ->mapsize are always altered if no error happened. + * + * When @use_defaults is false and the respective property is found + * the following values will be validated: + * - reg-io-width (->iotype) + * - reg-offset (->mapsize against ->mapbase) + * + * Returns: 0 on success or negative errno on failure + */ +int uart_read_port_properties(struct uart_port *port, bool use_defaults) +{ + struct device *dev = port->dev; + u32 value; + int ret; + + /* Read optional UART functional clock frequency */ + device_property_read_u32(dev, "clock-frequency", &port->uartclk); + + /* Read the registers alignment (default: 8-bit) */ + ret = device_property_read_u32(dev, "reg-shift", &value); + if (ret) + port->regshift = use_defaults ? 0 : port->regshift; + else + port->regshift = value; + + /* Read the registers I/O access type (default: MMIO 8-bit) */ + ret = device_property_read_u32(dev, "reg-io-width", &value); + if (ret) { + port->iotype = use_defaults ? UPIO_MEM : port->iotype; + } else { + switch (value) { + case 1: + port->iotype = UPIO_MEM; + break; + case 2: + port->iotype = UPIO_MEM16; + break; + case 4: + port->iotype = device_is_big_endian(dev) ? UPIO_MEM32BE : UPIO_MEM32; + break; + default: + if (!use_defaults) { + dev_err(dev, "Unsupported reg-io-width (%u)\n", value); + return -EINVAL; + } + port->iotype = UPIO_UNSET; + break; + } + } + + /* Read the address mapping base offset (default: no offset) */ + ret = device_property_read_u32(dev, "reg-offset", &value); + if (ret) + value = 0; + + /* Check for shifted address mapping overflow */ + if (!use_defaults && port->mapsize < value) { + dev_err(dev, "reg-offset %u exceeds region size %pa\n", value, &port->mapsize); + return -EINVAL; + } + + port->mapbase += value; + port->mapsize -= value; + + /* Read optional FIFO size */ + device_property_read_u32(dev, "fifo-size", &port->fifosize); + + if (device_property_read_bool(dev, "no-loopback-test")) + port->flags |= UPF_SKIP_TEST; + + /* Get index of serial line, if found in DT aliases */ + ret = of_alias_get_id(dev_of_node(dev), "serial"); + if (ret >= 0) + port->line = ret; + + if (dev_is_platform(dev)) + ret = platform_get_irq(to_platform_device(dev), 0); + else + ret = fwnode_irq_get(dev_fwnode(dev), 0); + if (ret == -EPROBE_DEFER) + return ret; + if (ret > 0) + port->irq = ret; + else if (use_defaults) + /* By default IRQ support is mandatory */ + return ret; + else + port->irq = 0; + + port->flags |= UPF_SHARE_IRQ; + + return 0; +} +EXPORT_SYMBOL(uart_read_port_properties); + static struct device_driver serial_port_driver = { .name = "port", .suppress_bind_attrs = true, diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h index 2b0526ae1fac..c93d775fc8a8 100644 --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -962,6 +962,7 @@ int uart_register_driver(struct uart_driver *uart); void uart_unregister_driver(struct uart_driver *uart); int uart_add_one_port(struct uart_driver *reg, struct uart_port *port); void uart_remove_one_port(struct uart_driver *reg, struct uart_port *port); +int uart_read_port_properties(struct uart_port *port, bool use_defaults); bool uart_match_port(const struct uart_port *port1, const struct uart_port *port2); -- 2.43.0.rc1.1.gbec44491f096