Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp693377ybh; Wed, 18 Mar 2020 07:29:03 -0700 (PDT) X-Google-Smtp-Source: ADFU+vu+7IPPxetfxQVAq9jRpU9v3xfbr7XR8CbZkJz2hf37R/yY0PVBo+pqgQvGFgEZ5+l2Zof8 X-Received: by 2002:a9d:7a9:: with SMTP id 38mr3994161oto.60.1584541743796; Wed, 18 Mar 2020 07:29:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584541743; cv=none; d=google.com; s=arc-20160816; b=AY/NXgTiUyNWOiXglzfQjeGYbp/HeX+5HOULCtei9EocnQ5b+qNIEQiZNRm9OCqeva aQZ1w0SfX76MvkZGg8xyrDKvBf2YMFNWhcrrzu1d1KVpQmxinu7J1X0Ul8uCM+R9qqIN MbzzrhcotUzzyr8yCG112wUBJ1OHFOWjNp+TnJZu0d+WBc43tVFq6ZLkp8ycHnDQtcDe 8nc6dsKqomqqXkx0FtU+cmiBBNiXp4XIZ026/AqSWGvSOJVOfWEkS1Ic5Qi2qZZuz1rk DjVR6Hy5Bl1aatMpqXJRqLxXljwix7JZfVK63dHmyZrSuJcm5Yiu1A+l9NPPsx197lwk 87wQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=M2y4B2uDBzDoeT+eyVsZ1Bd5zBBxj+h9otVu/+VqeOM=; b=o7MlJpenlntR0ggMM0qPp7sjdUTsaLc67UyfrtieRYz3/HQUdVJGSmTqqTUNZH4rCx mkEFopFKKB61i4s0aGf27TAU1VNYuwsnPr9MTHFBhi65evWs6PuHn/qKzUCOlxS5HaYU 4mdNJYGTOwda/OPd/gku+i0SI7sVmMjv7nKpq+eUhLDNInA2ccMbh031llWENyRVO9H6 qGk6sV+nl+tmcBF8yBWvk7/MjYayK1w+XdNA0WnBTAVgbL+PF9bDhWwASpi236hFv4vr d5dQU6U7xlBQcVtiePI4eGBxxQVn4PhrBRo6kGaBWbij9BDjxAofg1FRyBzYJ/VK/jhr LOgA== ARC-Authentication-Results: i=1; mx.google.com; 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 p27si2988330ota.194.2020.03.18.07.28.52; Wed, 18 Mar 2020 07:29:03 -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; 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 S1727231AbgCRO1W (ORCPT + 99 others); Wed, 18 Mar 2020 10:27:22 -0400 Received: from gloria.sntech.de ([185.11.138.130]:37354 "EHLO gloria.sntech.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726944AbgCRO1A (ORCPT ); Wed, 18 Mar 2020 10:27:00 -0400 Received: from [94.134.91.63] (helo=phil.fritz.box) by gloria.sntech.de with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.89) (envelope-from ) id 1jEZen-0004YX-OA; Wed, 18 Mar 2020 15:26:53 +0100 From: Heiko Stuebner To: gregkh@linuxfoundation.org Cc: jslaby@suse.com, andriy.shevchenko@linux.intel.com, matwey.kornilov@gmail.com, linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, heiko@sntech.de, Heiko Stuebner Subject: [PATCH 2/7] serial: 8250: add serial_in_poll_timeout helper Date: Wed, 18 Mar 2020 15:26:35 +0100 Message-Id: <20200318142640.982763-3-heiko@sntech.de> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200318142640.982763-1-heiko@sntech.de> References: <20200318142640.982763-1-heiko@sntech.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Heiko Stuebner In cases where a serial register needs to be polled until a specific state, this should have a timeout as noted in the thread bringing em485 support to 8250_dw. To not re-implement timeout handling in each case, add a helper modelled after readx_poll_timeout / regmap_read_poll_timeout to facilitate this. Signed-off-by: Heiko Stuebner --- drivers/tty/serial/8250/8250.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/drivers/tty/serial/8250/8250.h b/drivers/tty/serial/8250/8250.h index 33ad9d6de532..50a4c174410d 100644 --- a/drivers/tty/serial/8250/8250.h +++ b/drivers/tty/serial/8250/8250.h @@ -118,6 +118,40 @@ static inline void serial_out(struct uart_8250_port *up, int offset, int value) up->port.serial_out(&up->port, offset, value); } +/** + * serial_in_poll_timeout - Poll until a condition is met or a timeout occurs + * + * @port: uart_8250_port to read from + * @offs: Offset to poll + * @val: Integer variable to read the value into + * @cond: Break condition (usually involving @val) + * @timeout_us: Timeout in us, 0 means never timeout + * + * Returns 0 on success and -ETIMEDOUT upon a timeout or the serial_in + * error return value in case of a error read. + * + * This is modelled after the readx_poll_timeout macros in linux/iopoll.h. + */ +#define serial_in_poll_timeout(port, offs, val, cond, timeout_us) \ +({ \ + u64 __timeout_us = (timeout_us); \ + ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \ + for (;;) { \ + val = serial_in((port), (offs)); \ + if (val < 0) \ + break; \ + if (cond) \ + break; \ + if ((__timeout_us) && \ + ktime_compare(ktime_get(), __timeout) > 0) { \ + val = serial_in((port), (offs)); \ + break; \ + } \ + cpu_relax(); \ + } \ + (val < 0) ? val : ((cond) ? 0 : -ETIMEDOUT); \ +}) + void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p); static inline int serial_dl_read(struct uart_8250_port *up) -- 2.24.1