From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.161.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796380155; b=KmB1mF+ngw5X1NCTWD2SnzQWAGgFnu6Sjh4SFW2Ny/Nr+tL9j++e0jZ9bCVe1y+k0nhd MdVDnQsx0TxOHy+x6+0Xs3a+FxnBgirRfF4SLeEP431WtR/zDbREfZpYkMeW6HxJpkgMb zaW28O2VZzYqU/1xEfV/HYrBgmUFBmcGulHDNS/zHCJiT5VBg30OONFaqlF0BGLknGdNF 6o2Zh8ObgMFP5XJh2UbHB5GrpPpjBKa1dRnnLIM7VskGa3BK9sOHha4KwonCTLnt9pev6 HhILbFjZy+f7GAEeYMdHO7R5p0AlCwAZw60vmfwHu5/SvuUn8vU7LqvjWfhbwl4TTYw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380155; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:Date:Subject: MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id: References:In-Reply-To:To:Cc:X-Mailer; bh=3G3CWQIL0JbSsmfNksLi3Fp/1YxbYzf4T7l8kEzH84Y=; b=HisoR32qv1IsX8FXE7Pxz+ouL5AtBHGzOdHSSeiW8RlTFaICaIXhkWchg4RVw9ovGLM/ 4JVYU4iq+a5WI8ias9ilbdfb0Qxayx/P3Bbazj9gTIGZ3YNf3kScK0pviWbwkplnVzNC5 hSIqdgj5DeI167vnRGBB5DLOJi26GmLZCdzlVeYeQf1QsFicLTczboW2MZXUbsjtf8Ru7 fcmiBwADCGVt7+THJLuua4t+nOSevaW8HCBmOkAfqd1Sewz8cl2K+i9BzQZE8W30t1QX8 ArXq5yi+LAVo8mNF1e/h4KQt/5BceZPeGvQLlel9aRDcSXk9ElhAmWxCjuDRAlyrsmQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=e0kOSGNj; arc=none smtp.remote-ip=209.85.161.52 Received: from mail-oo1-f52.google.com (mail-oo1-f52.google.com [209.85.161.52]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 31B74606A69F for ; Fri, 4 Dec 2026 10:29:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 31B74606A69F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=e0kOSGNj Received: by mail-oo1-f52.google.com with SMTP id 006d021491bc7-71bf012f440so452292eaf.0 for ; Fri, 04 Dec 2026 02:29:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796380154; x=1796984954; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=3G3CWQIL0JbSsmfNksLi3Fp/1YxbYzf4T7l8kEzH84Y=; b=e0kOSGNjLLK64iCX5u18bruvUBk1zVPYIk1bDCH2StLvJqPeOgfJS8dYn+MuRkDL+P NNDaBZis7no9Z4+PdmZnTXZ1QLDZkSFEoI94BeK1yGI9OC7BiHE3ZjVz7Wys3AwFWTzD gQWKyBqMcuUjY5OGTqMjnP+Ppy/z/OUS7Kvys18EQxR6tq/NhHUbrg7qItUOxKQ4qSNe TLz0CCKz8leZrkQzNh2QptGriusje7OK0ohqI6W+k41ybwD1wVrXzPIEx7CNUObHZZnJ vaqFL61wDbOAQXw4XjHYDWwHSxc1aRx5VpiJI0kuCsV6Dl+GXo0uz6L2pE1gQraqxug2 Z72Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796380154; x=1796984954; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=3G3CWQIL0JbSsmfNksLi3Fp/1YxbYzf4T7l8kEzH84Y=; b=BOlNFtxg+pjBEA+/X5clO9iaaJXbBgRep8zsJUoZ2mibyVVp6/4KmHw0erWTnTcjEf aGFYzDKi6x/yL6oHXuEoZG4GEUx4TUNSmy1Utwme7H97N2yrUVNfbtp2rXKpjTD21p5x SMo4MY29Enx1+FcxN7P66BXK5Mp0nqDmSe53fvffID48IXq5WDHIQXHoI+A9e2AM0H0g 52XJW2rSTO/p5LzKVCVZxBk7GupLJXvHS3dkjt5IW06YaPOQMAuqWuZX/wUUJJySmN83 xmve00pWiy07sPn9PKNVkra/Tt4MR8nba00/y8cOpNtSdyHbQoc6tj+WvoDEGXRStIFO LAQQ== X-Gm-Message-State: APuU4Z/OSdEBVupfEEtunQzrXtbpOOAhfZNEthANGpiYsttQ6chnaS9O CZOAxi2eUDJCL9+5lEg9WDDVIXiRiK0bMsulye6qnPVywOWbc+tjEMxx X-Gm-Gg: AZFdTZetZTrIQ7JrGMuLz/Ic/y1v00RCGDTcmTYcW3lAtiIjpdyJocob/UymyGrO0Rc U+PvcAidsbp41EQOqkSnfF1WLps/SwEqTsC6OLth14IwH88VZLy8hKkJWN5JSWdVPgzHoSOU/iT bFXlyFlkTcDCLcyrxGY9cVchBoITlOkDffzjHksJId1BZ81u2wOfR9nHrzzSqeBdiNcuaCQ5+kp kGlvYGv6ZhlGhRFkks/3SywA6zqan1Zodd29RfxjQAtJzG71sQoRiyPM6vjQxx/Rl3XL41AU7tB igaIfgZQhNcdjPMPh4uFQ0+SNEv0POtGcrBzwtqwaAyn4qjYWPPZa9VfWsAYT9FsIhP6g4GtpQL 1fCnIDQOuEG/5MDP234og8w8LUkbFeNZFozuh5BFVc2Uaa8ZzV3Ig4YJCDrSEgwQTojqlG7Dqn+ VafBkwbFyfY0tnmwdXqE/CFKXoVuozcW5Kuvsnjt1Bn+mfn52YS1bXpIvg7tUNCj5h7iY010E2V ow2+x29qyy+cyL1/ff5RiJgenqk9isI6GZgQel/OT60WsyCOxVzpVY73pXKeopcUg7IWnakiyFE 0CgBt6I1s5TA4EpSmFpcUANtRVzjdQAEhYK2hTXch1orbDatKd0njYK5hgH6hVUbON09j1gqJz+ hhIxguA8gc7eGS9HnLz8Xm7/C+r43IeGMxFFPBjnoRxBQvKpXuK+GsKQBO7hH8kCTruqZYREZ86 GlhaLrKTQx72SSg/L7L1kPia8SUE7daGHBWrjc7pndJHeEIEBa1KbQLLGDv/FKzDyIKmMPTFFNe SY= X-Received: by 2002:a05:6820:c5d0:10b0:71b:911f:ae01 with SMTP id 006d021491bc7-71bf20a2c0cmr941847eaf.55.1796380154222; Fri, 04 Dec 2026 02:29:14 -0800 (PST) Received: from [127.0.1.1] (174-29-20-82.hlrn.qwest.net. [174.29.20.82]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863f81fad14sm1747533a34.9.2026.12.04.02.29.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 02:29:13 -0800 (PST) From: James Hilliard Date: Fri, 04 Dec 2026 03:28:51 -0700 Subject: [PATCH v10 06/11] serial: 8250_dw: derive RX trigger thresholds from the FIFO depth X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261204-submit-h616-uart-fifo-v1-v10-6-1a9fb4f79635@gmail.com> References: <20261204-submit-h616-uart-fifo-v1-v10-0-1a9fb4f79635@gmail.com> In-Reply-To: <20261204-submit-h616-uart-fifo-v1-v10-0-1a9fb4f79635@gmail.com> To: Andy Shevchenko , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , Greg Kroah-Hartman , Jiri Slaby , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland Cc: linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, James Hilliard X-Mailer: b4 0.17-dev DW_apb_uart uses receive thresholds of one byte, one quarter full, one half full and two bytes below full. The generic 16550A table describes these encodings only for a 16-byte FIFO. For example, the half-full setting on a 64-byte DW FIFO is reported as eight bytes instead of 32, and a sysfs request for 32 bytes selects the 62-byte encoding. Build a per-port threshold table from the discovered FIFO depth in dwlib and install receive-trigger callbacks for CPR-described FIFOs, including LPSS ports. Keep the setup helper available to integrations which obtain the depth without probing the optional DW registers. Validate that each threshold is positive and representable before constructing the table. Serialize FCR updates with termios and the port lock, acquire a runtime PM reference before accessing the device, and propagate resume errors without changing the shadow or hardware. Update only the trigger field so changing the threshold preserves the FIFO contents. The FCR mapping is documented in the DW_apb_uart databook, section 6, FIFO Control Register. The existing default FCR encoding is unchanged. Signed-off-by: James Hilliard --- drivers/tty/serial/8250/8250_dwlib.c | 87 ++++++++++++++++++++++++++++++++++++ drivers/tty/serial/8250/8250_dwlib.h | 2 + 2 files changed, 89 insertions(+) diff --git a/drivers/tty/serial/8250/8250_dwlib.c b/drivers/tty/serial/8250/8250_dwlib.c index 4a06dec6e263..6cfb7674ac11 100644 --- a/drivers/tty/serial/8250/8250_dwlib.c +++ b/drivers/tty/serial/8250/8250_dwlib.c @@ -1,18 +1,27 @@ // SPDX-License-Identifier: GPL-2.0+ /* Synopsys DesignWare 8250 library. */ +#include #include #include #include #include #include +#include #include +#include +#include #include #include #include #include "8250_dwlib.h" +/* DesignWare FIFO-relative receive thresholds */ +#define DW_UART_RXTRIG_QUARTER_DIV 4 +#define DW_UART_RXTRIG_HALF_DIV 2 +#define DW_UART_RXTRIG_FULL_MARGIN 2 + /* * divisor = div(I) + div(F) * "I" means integer, "F" means fractional @@ -208,11 +217,85 @@ static const struct serial_rs485 dw8250_rs485_supported = { SER_RS485_ADDR_DEST, }; +/* The trigger is the masked FCR receive trigger field, in register position */ +static unsigned int dw8250_rxtrig_bytes(struct uart_port *p, unsigned int trigger) +{ + switch (trigger) { + case UART_FCR_TRIGGER_1: + return 1; + case UART_FCR_TRIGGER_4: + return p->fifosize / DW_UART_RXTRIG_QUARTER_DIV; + case UART_FCR_TRIGGER_8: + return p->fifosize / DW_UART_RXTRIG_HALF_DIV; + default: + return p->fifosize - DW_UART_RXTRIG_FULL_MARGIN; + } +} + +static int dw8250_get_rxtrig(struct uart_port *p) +{ + struct dw8250_port_data *pd = p->private_data; + struct uart_8250_port *up = up_to_u8250p(p); + + guard(uart_port_lock_irqsave)(p); + + return pd->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)]; +} + +static int dw8250_set_rxtrig(struct uart_port *p, u16 bytes) +{ + struct dw8250_port_data *pd = p->private_data; + struct uart_8250_port *up = up_to_u8250p(p); + int fcr, ret; + + fcr = serial8250_bytes_to_fcr(pd->rxtrig_bytes, bytes); + if (fcr < 0) + return fcr; + + guard(mutex)(&p->state->port.mutex); + + PM_RUNTIME_ACQUIRE_IF_ENABLED(p->dev, pm); + ret = PM_RUNTIME_ACQUIRE_ERR(&pm); + if (ret) + return ret; + + guard(uart_port_lock_irqsave)(p); + + FIELD_MODIFY(UART_FCR_TRIGGER_MASK, &up->fcr, UART_FCR_R_TRIG_BITS(fcr)); + /* Changing the trigger alone preserves the FIFO contents */ + serial_port_out(p, UART_FCR, up->fcr); + + return 0; +} + +int dw8250_setup_rxtrig(struct uart_port *p) +{ + struct dw8250_port_data *pd = p->private_data; + + /* Each FIFO-relative threshold must be positive and fit in the table */ + if (p->fifosize < DW_UART_RXTRIG_QUARTER_DIV || + p->fifosize - DW_UART_RXTRIG_FULL_MARGIN > U16_MAX) + return dev_err_probe(p->dev, -EINVAL, + "invalid FIFO size %u\n", p->fifosize); + + for (unsigned int i = 0; i < ARRAY_SIZE(pd->rxtrig_bytes); i++) { + unsigned int trigger = FIELD_PREP(UART_FCR_TRIGGER_MASK, i); + + pd->rxtrig_bytes[i] = dw8250_rxtrig_bytes(p, trigger); + } + p->get_rxtrig = dw8250_get_rxtrig; + p->set_rxtrig = dw8250_set_rxtrig; + + return 0; +} +EXPORT_SYMBOL_NS_GPL(dw8250_setup_rxtrig, "SERIAL_8250"); + int dw8250_setup_port(struct uart_port *p) { struct dw8250_port_data *pd = p->private_data; struct uart_8250_port *up = up_to_u8250p(p); u32 reg, old_dlf; + int ret; pd->hw_rs485_support = dw8250_detect_rs485_hw(p); if (pd->hw_rs485_support) { @@ -258,6 +341,10 @@ int dw8250_setup_port(struct uart_port *p) p->flags |= UPF_FIXED_TYPE; p->fifosize = DW_UART_CPR_FIFO_SIZE(reg); up->capabilities = UART_CAP_FIFO | UART_CAP_NOTEMT; + + ret = dw8250_setup_rxtrig(p); + if (ret) + return ret; } if (reg & DW_UART_CPR_AFCE_MODE) diff --git a/drivers/tty/serial/8250/8250_dwlib.h b/drivers/tty/serial/8250/8250_dwlib.h index 4d32651956ab..b896dd9bebb7 100644 --- a/drivers/tty/serial/8250/8250_dwlib.h +++ b/drivers/tty/serial/8250/8250_dwlib.h @@ -86,6 +86,7 @@ struct dw8250_port_data { struct uart_8250_dma dma; /* Hardware configuration */ + u16 rxtrig_bytes[UART_FCR_R_TRIG_MAX_STATE]; u32 cpr_value; u8 dlf_size; @@ -100,6 +101,7 @@ struct dw8250_port_data { unsigned int dw8250_get_divisor_dlf(struct uart_port *p, unsigned int baud, unsigned int *frac); void dw8250_set_divisor_dlf(struct uart_port *p, unsigned int quot_frac); void dw8250_do_set_termios(struct uart_port *p, struct ktermios *termios, const struct ktermios *old); +int dw8250_setup_rxtrig(struct uart_port *p); int dw8250_setup_port(struct uart_port *p); static inline u32 dw8250_readl_ext(struct uart_port *p, int offset) -- 2.53.0