From patchwork Thu Dec 19 09:08:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tyrone Ting X-Patchwork-Id: 852171 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 82FC8221DA4; Thu, 19 Dec 2024 09:09:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734599351; cv=none; b=HDMX/4tf0gCOS0ia20e/xa+0/D4V5esFerdnTGI9541zz0/c1MCTc/uT8K/koIwpnQopr9Dez0swFSQOKs+z37c73uQ3E6kMjxrB1hvoffs2Q37Im9dCWNnbibtIv99cxh10lH7WQqU1hxWybKF3Hsqx1OZfClqfJfwequNKWjY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734599351; c=relaxed/simple; bh=XH0Bpt5rg59dfvf3/V7+3t02QN0m79miR/wFz80Nmr8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=UXL/2B/vJrpD0Uy4IhxdfzEfL6nTMwxIzGPbEWCDlF0y3Ynpb+vGO2BpoW9cMEZUhLLlK68y7PsbiKfsagaRP5J11L8ZghjmVxCbzqEegBUAZeiE72R0JtDcJ/ARzA3nFlKrRCZ/BFdSdnE99/djt8EAqIP8kYwFtWNnQ2RXKx4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jCVm2fgb; arc=none smtp.client-ip=209.85.214.181 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.b="jCVm2fgb" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2167141dfa1so5021895ad.1; Thu, 19 Dec 2024 01:09:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734599349; x=1735204149; darn=vger.kernel.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=sUTvgfGYPu+IPYUs2nFsjwTZ63vlpmHE9eVnRYu8l/s=; b=jCVm2fgbt7fArlXYJKJzzgwMlDQniOEliFnvmcYRR5GPiNk3RtsuR3bQVNYyz/wxIr jUQAVb3Xt05mfjLCuM6HsyTjl00+IlxTByUphBbd3ymvE2SExxIJPxLb/m0/WaYbbyaW e61iIm47nYiwDo6+ImmbudAWdNn/FpcCIKQxQjOXtH/7tKCrVtJraW1ZvYuHADi+n/8p /XG6CEHVfa9Rr7QZPdHX+Blbcge+U1FQvzaiLwlV5pmBfvBwwXeaiiq6eNtH+jfv8VdL jMd59IuJhbSaR8RLUe+97SP23zxnFcdO4G5t4goYWm+Wx3PSMF5C89PGil5FN7BD0Lxn bD/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734599349; x=1735204149; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sUTvgfGYPu+IPYUs2nFsjwTZ63vlpmHE9eVnRYu8l/s=; b=NOE/XI/vWrs7e/C6HftQAFssXbsK0AWdiK1Q9dgDtpiYOuMM5hZO1AjhuX3Cga7K9h uxVyfaB9NkJT286m66sqjYfdY3z8CFy49erAsZUcEfetp9xZp9S1gJ0EtuFOVUGpQJCu s0o/SCi3wPOkRtbvh4TjvPtwOtbtQDi13VULr7nU9l7+T9ZtFyp+csk0SWiar4Hm28K5 pE6N4VQPeW/vGt6UTk20N2f4mxxtCE+haXi2cytaftgRvpz2TxescKPZz8ycJPL3hWYK qFojgidpWMSFnB7HHTeEwAx3RZLWptu546TImNgp/F7yhdX7xdiP7/C4se+7laGHwyop mQKA== X-Forwarded-Encrypted: i=1; AJvYcCVI9nWI3PmBJdhDk8rPQub/T/HpeIdKPGTEtWPmO6E/+fVkB6ZWjoaFcSD4kQyRCdAbobDPmgL8BAg=@vger.kernel.org, AJvYcCXzBIaSw+/mHJJ3NPCrAjG5X5yQSgEzkjNBYOZSFkM4giPKWzeKDLcPgzdqkeH69A5y5RSrNrD+YPFE4KKY@vger.kernel.org X-Gm-Message-State: AOJu0Yyanh7900d9csST9HlJPHHBLCfFG2c5SjYWXsliXx2B2zbj1ECj 4+9ZjjW2AhW/Ous4sL3YiMAnZd/oc05fRxn5YabMPUKIMwARMAc= X-Gm-Gg: ASbGncuF41Ve1ajn/eb/+VRfzYA2bMgLZC4Z2SKP+tvFhcEMbAf6x9y+fOk2xrctzv2 ge6+RZN/8RVjrXkiXMbQ06jWwNyH4sTlJhSnIczVEWsKI7a99fbJ1wkUXwif2GdYEZl5P5fLECv SfbtTFm1ox4++3SUJ/sMqmJo1cOuD03EK0c1dRJL6Dh+NI1aii0CymjdojuFtjBvzFPQHVBn/45 bg1QkszJ19ynjTMU2HIHkwurROkjvLgdmIuan1e20oSxrRtxcOLIu2wBY2u++Q0U+UinN85Iup4 1G25HXR9wdItBSvckrs1lJb0Ns3X3czj5bPaZUcFAv+kECMz X-Google-Smtp-Source: AGHT+IG3ozXV/JZXCvMKLY0p+P+fsSoQftBArX2wWb9Rq3a6/vq1cDweO1cvd0cvjFNqium7LTNmfA== X-Received: by 2002:a17:902:ef03:b0:215:58be:334e with SMTP id d9443c01a7336-219da5d67f4mr41922355ad.10.1734599348454; Thu, 19 Dec 2024 01:09:08 -0800 (PST) Received: from localhost (2001-b400-e356-b45b-f574-35d0-91d3-527b.emome-ip6.hinet.net. [2001:b400:e356:b45b:f574:35d0:91d3:527b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dca0194bsm8086265ad.248.2024.12.19.01.09.07 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 19 Dec 2024 01:09:07 -0800 (PST) From: Tyrone Ting X-Google-Original-From: Tyrone Ting To: avifishman70@gmail.com, tmaimon77@gmail.com, tali.perry1@gmail.com, venture@google.com, yuenn@google.com, benjaminfair@google.com, andi.shyti@kernel.org, andriy.shevchenko@linux.intel.com, wsa@kernel.org, rand.sec96@gmail.com, wsa+renesas@sang-engineering.com, warp5tw@gmail.com, tali.perry@nuvoton.com, Avi.Fishman@nuvoton.com, tomer.maimon@nuvoton.com, KWLIU@nuvoton.com, JJLIU0@nuvoton.com, kfting@nuvoton.com Cc: openbmc@lists.ozlabs.org, linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v8 1/4] i2c: npcm: Modify timeout evaluation mechanism Date: Thu, 19 Dec 2024 17:08:56 +0800 Message-Id: <20241219090859.18722-2-kfting@nuvoton.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20241219090859.18722-1-kfting@nuvoton.com> References: <20241219090859.18722-1-kfting@nuvoton.com> Precedence: bulk X-Mailing-List: linux-i2c@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: From: Tyrone Ting The users want to connect a lot of masters on the same bus. This timeout is used to determine the time it takes to take bus ownership. The transactions are very long, so waiting 35ms is not enough. Increase the timeout and treat it as the total timeout, including retries. The total timeout is 2 seconds now. The i2c core layer will have chances to retry to call the i2c driver transfer function if the i2c driver reports that the bus is busy and returns -EAGAIN. Signed-off-by: Tyrone Ting Reviewed-by: Tali Perry --- drivers/i2c/busses/i2c-npcm7xx.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/drivers/i2c/busses/i2c-npcm7xx.c b/drivers/i2c/busses/i2c-npcm7xx.c index 482a0074d448..c96a25d37c14 100644 --- a/drivers/i2c/busses/i2c-npcm7xx.c +++ b/drivers/i2c/busses/i2c-npcm7xx.c @@ -2132,19 +2132,12 @@ static int npcm_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, } } - /* - * Adaptive TimeOut: estimated time in usec + 100% margin: - * 2: double the timeout for clock stretching case - * 9: bits per transaction (including the ack/nack) - */ - timeout_usec = (2 * 9 * USEC_PER_SEC / bus->bus_freq) * (2 + nread + nwrite); - timeout = max_t(unsigned long, bus->adap.timeout, usecs_to_jiffies(timeout_usec)); if (nwrite >= 32 * 1024 || nread >= 32 * 1024) { dev_err(bus->dev, "i2c%d buffer too big\n", bus->num); return -EINVAL; } - time_left = jiffies + timeout + 1; + time_left = jiffies + bus->adap.timeout / bus->adap.retries + 1; do { /* * we must clear slave address immediately when the bus is not @@ -2192,6 +2185,14 @@ static int npcm_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, if (npcm_i2c_master_start_xmit(bus, slave_addr, nwrite, nread, write_data, read_data, read_PEC, read_block)) { + /* + * Adaptive TimeOut: estimated time in usec + 100% margin: + * 2: double the timeout for clock stretching case + * 9: bits per transaction (including the ack/nack) + */ + timeout_usec = (2 * 9 * USEC_PER_SEC / bus->bus_freq) * (2 + nread + nwrite); + timeout = max_t(unsigned long, bus->adap.timeout / bus->adap.retries, + usecs_to_jiffies(timeout_usec)); time_left = wait_for_completion_timeout(&bus->cmd_complete, timeout); @@ -2317,7 +2318,12 @@ static int npcm_i2c_probe_bus(struct platform_device *pdev) adap = &bus->adap; adap->owner = THIS_MODULE; adap->retries = 3; - adap->timeout = msecs_to_jiffies(35); + /* + * The users want to connect a lot of masters on the same bus. + * This timeout is used to determine the time it takes to take bus ownership. + * The transactions are very long, so waiting 35ms is not enough. + */ + adap->timeout = 2 * HZ; adap->algo = &npcm_i2c_algo; adap->quirks = &npcm_i2c_quirks; adap->algo_data = bus; From patchwork Thu Dec 19 09:08:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tyrone Ting X-Patchwork-Id: 852600 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C843D222570; Thu, 19 Dec 2024 09:09:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734599354; cv=none; b=lMuF4orhBQrxtK/LEY/ZPVcKIAaHAwec2s/dRf1y7cKsLv8uaS1RsjC94BPuevqeZEx/qVMeHgYH+TvJQpZr9sgXMv8v0qQygE6qSf/lezKe0HJkwi2FN9GiWW4J275m5F7d/7Qb9bYIJ93U8qEjIMP++CGxhDZDJKcLbDCOml0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734599354; c=relaxed/simple; bh=PFIHSsZ4tZDzQ6Wxg6A1hng+1abgPyT1QlTDHa5uS+E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=Iog5f/Jd0h45qNa1aVCjh+bmo1FNKhaSy2FrlWEjB6L5oFYlN7BfZ5ES1N9B15xhLOge79ev3A5KCd7FqqI7zow8BI8L0ivreVv36ZcFwsN+LgV+OmHkhbn6bteJlQgmka5CtUZxd8NhiIodgj1uzK5g01FRtumweTuSJNRLlMU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=h6x3goTl; arc=none smtp.client-ip=209.85.214.171 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.b="h6x3goTl" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-21628b3fe7dso4595365ad.3; Thu, 19 Dec 2024 01:09:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734599352; x=1735204152; darn=vger.kernel.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=piYN//2t/Q3gekIk2iV6YPjUEpOk8bMx3T+8iGXFsUI=; b=h6x3goTl8Qm4tntgsu4JPWRFd4fFZt9Lz6ae3/sQk5YFI6NIi7TUxC0FQq2dKTa0h1 KAM2eCSXJDi9iTd53C8pjRqIkqdUg3RHrNrzBmqMX+x+N5V0Qpil/iZZeoejH4ydnTY5 0SiuNhVNB3LCT1S08U7FConq8vBVsCarg8L2BVR6TrilnFzFheH3aAfBqr8CiGOL8FRC O4RVgkrIY+tUTT4CbQDZENnkOKGDhSsqVS9eKnIh2aMiPhVoLf8la+tUcpacNJdPMTV/ kkRpOUOBZQoUel//jNEBIFLNtKZbQNJ5KgcuoeAOMykMHIQXYHAvkQas+FMzQAJnuCJQ 2/iA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734599352; x=1735204152; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=piYN//2t/Q3gekIk2iV6YPjUEpOk8bMx3T+8iGXFsUI=; b=N7OCPdES857+OTUKZWxyUckGkaNv8HLaztsBQ++z7D4f5u7gmUm1ZR6l/5WVSZCFMP 8d9CkHGlSJ6ZrLzWtqYPtIoggYzyxN5ZCjboILLcKBuAp1nzkc0x9Z3Ukqk2lL9MH/0A vYGE/RMET7sdrD5X8mYqap52LkSnaLav9OebzT7OLPP/QfwcqgoAwpFSjCYjzeu2tl3B Cls+kkBHnfaT/B1Fkf8FK2hPxFrklKMPd58KFRC9CN0Tfm9Mj8bhJTw2VXAmmeK3nM6H 3AKBaIhnot+1FrAvP9/QZwuFK/C+AJi8F7Bsjju1jEST0JApYn2UBPQs1HEuUdtJUVHg ihEA== X-Forwarded-Encrypted: i=1; AJvYcCWLkmXT3++rEYUl9ZuuGe4oxukiezk2kIOAkFvJjGFhLN5pN5nGLfZTAVkwcLYmdEEnJMWA8LDbex1S3lQw@vger.kernel.org, AJvYcCXNn30dI3XDqKJwGPXJiWAg5daEpvZKzldQySb7y8XSXQJ+o0Gkj+OXXuMu3VwxzYyWuzxQ1T+Ndis=@vger.kernel.org X-Gm-Message-State: AOJu0YzA2J9cxueXgwCuRBzGd9nvVQZaeM5m6q0msJmNJREjGUQIW638 TSxsB0Tct7e5P8vna3F2TjO8QphzU8LVBIxGM8Mq3l9MBBLFFas= X-Gm-Gg: ASbGnctx4kSJyBroSIUlBzSb211KnjEIqSwcYdeiq8YKuEy9UQfZ+obLQKf1CWeZEbi OcRgCPDD8XfPhFDgd68dtZWu9zAkcMpTmTQjR09/8jW6jzHjxWR6YrC8UbuYGMPJmC7YiMMrNY6 iwzBF77E2kPwOID9qMfiBbTiZu7Qhf/ROYwpH6bE286MRhybGqLsQjSZmWZSGmVlSP94srE/xpd /Wda96kpaWsM2CohJ12duaBqa1kvb7+ZIAmgpDJ+taJqPCttt+pw27iU38DggJCye07/YrPCV2t ZlL5rqRyzPiX0fGZG700MBD8djkXtr/UNb3jIr/m9ylHJcMc X-Google-Smtp-Source: AGHT+IHTqzdBHtaXdUfJQLh5OtQUwGieCVyrC3tJXdDiWFRu/A8iaXaQpl+DwoVlL2pK77Rw42fZNw== X-Received: by 2002:a17:90b:2f03:b0:2ee:d7d3:3019 with SMTP id 98e67ed59e1d1-2f2e91f0dc9mr10901708a91.12.1734599351937; Thu, 19 Dec 2024 01:09:11 -0800 (PST) Received: from localhost (2001-b400-e356-b45b-f574-35d0-91d3-527b.emome-ip6.hinet.net. [2001:b400:e356:b45b:f574:35d0:91d3:527b]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2f4477e8222sm956016a91.23.2024.12.19.01.09.11 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 19 Dec 2024 01:09:11 -0800 (PST) From: Tyrone Ting X-Google-Original-From: Tyrone Ting To: avifishman70@gmail.com, tmaimon77@gmail.com, tali.perry1@gmail.com, venture@google.com, yuenn@google.com, benjaminfair@google.com, andi.shyti@kernel.org, andriy.shevchenko@linux.intel.com, wsa@kernel.org, rand.sec96@gmail.com, wsa+renesas@sang-engineering.com, warp5tw@gmail.com, tali.perry@nuvoton.com, Avi.Fishman@nuvoton.com, tomer.maimon@nuvoton.com, KWLIU@nuvoton.com, JJLIU0@nuvoton.com, kfting@nuvoton.com Cc: openbmc@lists.ozlabs.org, linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v8 2/4] i2c: npcm: Assign client address earlier for `i2c_recover_bus()` Date: Thu, 19 Dec 2024 17:08:57 +0800 Message-Id: <20241219090859.18722-3-kfting@nuvoton.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20241219090859.18722-1-kfting@nuvoton.com> References: <20241219090859.18722-1-kfting@nuvoton.com> Precedence: bulk X-Mailing-List: linux-i2c@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: From: Tyrone Ting Store the client address earlier since it might get called in the i2c_recover_bus() logic flow at the early stage of npcm_i2c_master_xfer(). Signed-off-by: Tyrone Ting Reviewed-by: Tali Perry --- drivers/i2c/busses/i2c-npcm7xx.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/drivers/i2c/busses/i2c-npcm7xx.c b/drivers/i2c/busses/i2c-npcm7xx.c index c96a25d37c14..760608fdd075 100644 --- a/drivers/i2c/busses/i2c-npcm7xx.c +++ b/drivers/i2c/busses/i2c-npcm7xx.c @@ -2035,7 +2035,7 @@ static irqreturn_t npcm_i2c_bus_irq(int irq, void *dev_id) } static bool npcm_i2c_master_start_xmit(struct npcm_i2c *bus, - u8 slave_addr, u16 nwrite, u16 nread, + u16 nwrite, u16 nread, u8 *write_data, u8 *read_data, bool use_PEC, bool use_read_block) { @@ -2043,7 +2043,6 @@ static bool npcm_i2c_master_start_xmit(struct npcm_i2c *bus, bus->cmd_err = -EBUSY; return false; } - bus->dest_addr = slave_addr << 1; bus->wr_buf = write_data; bus->wr_size = nwrite; bus->wr_ind = 0; @@ -2086,7 +2085,6 @@ static int npcm_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, unsigned long time_left, flags; u16 nwrite, nread; u8 *write_data, *read_data; - u8 slave_addr; unsigned long timeout; bool read_block = false; bool read_PEC = false; @@ -2099,7 +2097,6 @@ static int npcm_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, } msg0 = &msgs[0]; - slave_addr = msg0->addr; if (msg0->flags & I2C_M_RD) { /* read */ nwrite = 0; write_data = NULL; @@ -2155,6 +2152,21 @@ static int npcm_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, } while (time_is_after_jiffies(time_left) && bus_busy); + /* + * Store the address early in a global position to ensure it is + * accessible for a potential call to i2c_recover_bus(). + * + * Since the transfer might be a read operation, remove the I2C_M_RD flag + * from the bus->dest_addr for the i2c_recover_bus() call later. + * + * The i2c_recover_bus() uses the address in a write direction to recover + * the i2c bus if some error condition occurs. + * + * Remove the I2C_M_RD flag from the address since npcm_i2c_master_start_xmit() + * handles the read/write operation internally. + */ + bus->dest_addr = i2c_8bit_addr_from_msg(msg0) & ~I2C_M_RD; + /* * Check the BER (bus error) state, when ber_state is true, it means that the module * detects the bus error which is caused by some factor like that the electricity @@ -2172,7 +2184,6 @@ static int npcm_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, } npcm_i2c_init_params(bus); - bus->dest_addr = slave_addr; bus->msgs = msgs; bus->msgs_num = num; bus->cmd_err = 0; @@ -2182,7 +2193,7 @@ static int npcm_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, npcm_i2c_int_enable(bus, true); - if (npcm_i2c_master_start_xmit(bus, slave_addr, nwrite, nread, + if (npcm_i2c_master_start_xmit(bus, nwrite, nread, write_data, read_data, read_PEC, read_block)) { /* From patchwork Thu Dec 19 09:08:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tyrone Ting X-Patchwork-Id: 852170 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 65EA4222593; Thu, 19 Dec 2024 09:09:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734599359; cv=none; b=Y/8TWNP1IZzG75iV+v3gz82MG9n3k02HlUGPuHigjQLTflRiimpp1scXnhU9cUuKpFK53YcJFursSwsDnU6eay5lKMqKLjDQ1vxxK/mdYGjOUmEsajRxjJuxMnuNNQsykY5xDWnc4ATQ+U+h0KE0yF2M3axKKuW34EnN+es4cVY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734599359; c=relaxed/simple; bh=o/ppNsa0UqRvx5GuyZxShMFOo2NaWsBj7opKW2E18W8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=UOsfrvF5hEUTw1gYAFtj8MX0HT5724wc8To/xbIvAc1yVVsqA9oQgXxVgvYI9yksflk7Tkj+6xvcStRwInUO8zCqnEbh//oQaTJzHLuPndXznSdhKCpM1mdnRlf6FrhGxR3eicl/46WZTNgLpNHoo33oqEhvhQLzGEVCewQkKP0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=U/mBxOA9; arc=none smtp.client-ip=209.85.210.174 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.b="U/mBxOA9" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-728eedfca37so630671b3a.2; Thu, 19 Dec 2024 01:09:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734599355; x=1735204155; darn=vger.kernel.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=LRxrdan5tHoMlrGZXe9w+RTf9KmkjJ92usLwMDr0nrc=; b=U/mBxOA9T1WmtFQ2Iah6d5em/BzUst8D7H0cE73suQhuqCyDKhpqkJPHRWxvb21M/X 9CMUf3HLdyJCdbAOg5oNPbCQgrMgkxg8CdQqtS7M8MHrIc6p1l54RMFarmi+MhP31ERh A06A4g3T9of6jt4fFOvqpSV7Qk6Wv0na8knD2bKly5UqWIe9qHf7A0a7cL3HZT5r2SN9 yFVDdpSrkxyfWZA23qvirUvOHeTutGA/AG70sahou1TGI9H3czaWpIMR/FRcY2qBowzX T38ULgtrJJkg3jjW5mluqJH2ntAm6rqcroxnlKo/qiG94olewQ00sXcxGpjwZZk7YiRJ OwhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734599355; x=1735204155; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=LRxrdan5tHoMlrGZXe9w+RTf9KmkjJ92usLwMDr0nrc=; b=ah/JxveVoNUc65bsC4d3QKSmASC8hNZ7nfLzO0pm4Vk5/9JbRmQT5V+wPwp6BoD79J tMlly7Ev4Ku1Qikgu/ZWAfdbbzKbLfY5GyhA+pGbaUa+WaXHs+W+ZEY5Ajwty88vw3Rj tQbzaM6IRxpPAPlORyGDZRFUsOJfd2dZ/NTKYvxywOJRbH8kSJeEARs4bdpib3ABY0Jj 9XyKxIO8M+ASGVerSKIFOghrqPR91jc0nQE2NsCgm1ZA+MXdDBP+z1WDF5+AJPpP7ga9 G6Bh7j9effSsAhqbkAwNMdGgGeUOd5FJLmqYgMwQEjAsUakCHQ9ca/9ppt8L4AT5g2k6 L5EA== X-Forwarded-Encrypted: i=1; AJvYcCW9u0SWq9vzmEy8azhey3n5i9XtDEgAZDcew6ri36Th/NOkQOxuOtlGgMkSqfjvJWxxjyENOyYzySQ=@vger.kernel.org, AJvYcCXpzXOYH9r/M44lnK76B+vosFor8F1aQ4eeGDzRSvU13wJw6Ur82al3INDxnboY2bam1yPIGf3wOH5gkTdh@vger.kernel.org X-Gm-Message-State: AOJu0Yx6czniCKsujqRcWm/ZVaQfrWIAzpnCHcBBEks2gs7pNbJypHho Q+h1yhtFl+crSS1NJgeTyk21krPypx9Yz1Fr8mnGlq2WiupOkCw= X-Gm-Gg: ASbGncuVfzhL1JWmWoBcLZYzbwqUTjTEzlZr0aO647K9G93sySul/F/KhF4OgHgk/IX q1untM8aAKXHXjREHtPoSMREKMPHvImWafejLZRu4cGAgegPMNl++6k4uZMNPZEnTA0IWaN1wSC KWG+57RqViEjJFl2COC09X1tx+s0JhfZAmhBBeHgQlK8jBYpr3k84edRWQ4EBWwzF76WMiueG4t fJWYRDf1F5TbHTid6BkobHPkISNlwiHwrDVNZ42dqHz5kNQvQiNeXgTFjgV2IGZEKfFigvmUzib sVF6DmMPM5lnw9HYTud43sQrWH2k/aro93U5qPWGdrbFbV4U X-Google-Smtp-Source: AGHT+IGmXYteJJlUcoAavZ+KIoD3czehp0N0Ue4e6GT89wgj0kegwbwWZSbvo2mGJxNaByTfgAu/pg== X-Received: by 2002:a05:6a00:a81:b0:725:8b00:167e with SMTP id d2e1a72fcca58-72aa8dab6edmr4287520b3a.16.1734599355482; Thu, 19 Dec 2024 01:09:15 -0800 (PST) Received: from localhost (2001-b400-e356-b45b-f574-35d0-91d3-527b.emome-ip6.hinet.net. [2001:b400:e356:b45b:f574:35d0:91d3:527b]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72aad8dbb70sm813090b3a.98.2024.12.19.01.09.14 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 19 Dec 2024 01:09:14 -0800 (PST) From: Tyrone Ting X-Google-Original-From: Tyrone Ting To: avifishman70@gmail.com, tmaimon77@gmail.com, tali.perry1@gmail.com, venture@google.com, yuenn@google.com, benjaminfair@google.com, andi.shyti@kernel.org, andriy.shevchenko@linux.intel.com, wsa@kernel.org, rand.sec96@gmail.com, wsa+renesas@sang-engineering.com, warp5tw@gmail.com, tali.perry@nuvoton.com, Avi.Fishman@nuvoton.com, tomer.maimon@nuvoton.com, KWLIU@nuvoton.com, JJLIU0@nuvoton.com, kfting@nuvoton.com Cc: openbmc@lists.ozlabs.org, linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v8 3/4] i2c: npcm: use i2c frequency table Date: Thu, 19 Dec 2024 17:08:58 +0800 Message-Id: <20241219090859.18722-4-kfting@nuvoton.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20241219090859.18722-1-kfting@nuvoton.com> References: <20241219090859.18722-1-kfting@nuvoton.com> Precedence: bulk X-Mailing-List: linux-i2c@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: From: Tyrone Ting Modify i2c frequency from table parameters for NPCM i2c modules. Supported frequencies are: 1. 100KHz 2. 400KHz 3. 1MHz The original equations were tested on a variety of chips and base clocks. Since we added devices that use higher frequencies of the module we saw that there is a mismatch between the equation and the actual results on the bus itself, measured on scope. Meanwhile, the equations were not accurate to begin with. They are an approximation of the ideal value. The ideal value is calculated per frequency of the core module. So instead of using the equations we did an optimization per module frequency, verified on a device. Most of the work was focused on the rise time of the SCL and SDA, which depends on external load of the bus and PU. Different PCB designs, or specifically to this case: the number and type of targets on the bus, impact the required values for the timing registers. Users can recalculate the numbers for each bus and get an even better optimization, but our users chose not to. We manually picked values per frequency that match the entire valid range of targets (from 1 to max number). Then we check against the AMR described in SMB spec and make sure that none of the values is exceeding. This process was led by the chip architect and included a lot of testing. Signed-off-by: Tyrone Ting Reviewed-by: Tali Perry --- drivers/i2c/busses/i2c-npcm7xx.c | 374 ++++++++++++++++++++++++------- 1 file changed, 288 insertions(+), 86 deletions(-) diff --git a/drivers/i2c/busses/i2c-npcm7xx.c b/drivers/i2c/busses/i2c-npcm7xx.c index 760608fdd075..1aae1a8a8055 100644 --- a/drivers/i2c/busses/i2c-npcm7xx.c +++ b/drivers/i2c/busses/i2c-npcm7xx.c @@ -263,6 +263,265 @@ static const int npcm_i2caddr[I2C_NUM_OWN_ADDR] = { #define I2C_FREQ_MIN_HZ 10000 #define I2C_FREQ_MAX_HZ I2C_MAX_FAST_MODE_PLUS_FREQ +struct smb_timing_t { + u32 core_clk; + u8 hldt; + u8 dbcnt; + u16 sclfrq; + u8 scllt; + u8 sclht; + bool fast_mode; +}; + +static struct smb_timing_t smb_timing_100khz[] = { + { + .core_clk = 100000000, .hldt = 0x2A, .dbcnt = 0x4, + .sclfrq = 0xFB, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 62500000, .hldt = 0x2A, .dbcnt = 0x1, + .sclfrq = 0x9D, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 50000000, .hldt = 0x2A, .dbcnt = 0x1, + .sclfrq = 0x7E, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 48000000, .hldt = 0x2A, .dbcnt = 0x1, + .sclfrq = 0x79, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 40000000, .hldt = 0x2A, .dbcnt = 0x1, + .sclfrq = 0x65, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 30000000, .hldt = 0x2A, .dbcnt = 0x1, + .sclfrq = 0x4C, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 29000000, .hldt = 0x2A, .dbcnt = 0x1, + .sclfrq = 0x49, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 26000000, .hldt = 0x2A, .dbcnt = 0x1, + .sclfrq = 0x42, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 25000000, .hldt = 0x2A, .dbcnt = 0x1, + .sclfrq = 0x3F, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 24000000, .hldt = 0x2A, .dbcnt = 0x1, + .sclfrq = 0x3D, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 20000000, .hldt = 0x2A, .dbcnt = 0x1, + .sclfrq = 0x33, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 16180000, .hldt = 0x2A, .dbcnt = 0x1, + .sclfrq = 0x29, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 15000000, .hldt = 0x23, .dbcnt = 0x1, + .sclfrq = 0x26, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 13000000, .hldt = 0x1D, .dbcnt = 0x1, + .sclfrq = 0x21, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 12000000, .hldt = 0x1B, .dbcnt = 0x1, + .sclfrq = 0x1F, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 10000000, .hldt = 0x18, .dbcnt = 0x1, + .sclfrq = 0x1A, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 9000000, .hldt = 0x16, .dbcnt = 0x1, + .sclfrq = 0x17, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 8090000, .hldt = 0x14, .dbcnt = 0x1, + .sclfrq = 0x15, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 7500000, .hldt = 0x7, .dbcnt = 0x1, + .sclfrq = 0x13, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 6500000, .hldt = 0xE, .dbcnt = 0x1, + .sclfrq = 0x11, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, + { + .core_clk = 4000000, .hldt = 0x9, .dbcnt = 0x1, + .sclfrq = 0xB, .scllt = 0x0, .sclht = 0x0, + .fast_mode = false, + }, +}; + +static struct smb_timing_t smb_timing_400khz[] = { + { + .core_clk = 100000000, .hldt = 0x2A, .dbcnt = 0x3, + .sclfrq = 0x0, .scllt = 0x47, .sclht = 0x35, + .fast_mode = true, + }, + { + .core_clk = 62500000, .hldt = 0x2A, .dbcnt = 0x2, + .sclfrq = 0x0, .scllt = 0x2C, .sclht = 0x22, + .fast_mode = true, + }, + { + .core_clk = 50000000, .hldt = 0x21, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0x24, .sclht = 0x1B, + .fast_mode = true, + }, + { + .core_clk = 48000000, .hldt = 0x1E, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0x24, .sclht = 0x19, + .fast_mode = true, + }, + { + .core_clk = 40000000, .hldt = 0x1B, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0x1E, .sclht = 0x14, + .fast_mode = true, + }, + { + .core_clk = 33000000, .hldt = 0x15, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0x19, .sclht = 0x11, + .fast_mode = true, + }, + { + .core_clk = 30000000, .hldt = 0x15, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0x19, .sclht = 0xD, + .fast_mode = true, + }, + { + .core_clk = 29000000, .hldt = 0x11, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0x15, .sclht = 0x10, + .fast_mode = true, + }, + { + .core_clk = 26000000, .hldt = 0x10, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0x13, .sclht = 0xE, + .fast_mode = true, + }, + { + .core_clk = 25000000, .hldt = 0xF, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0x13, .sclht = 0xD, + .fast_mode = true, + }, + { + .core_clk = 24000000, .hldt = 0xD, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0x12, .sclht = 0xD, + .fast_mode = true, + }, + { + .core_clk = 20000000, .hldt = 0xB, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0xF, .sclht = 0xA, + .fast_mode = true, + }, + { + .core_clk = 16180000, .hldt = 0xA, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0xC, .sclht = 0x9, + .fast_mode = true, + }, + { + .core_clk = 15000000, .hldt = 0x9, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0xB, .sclht = 0x8, + .fast_mode = true, + }, + { + .core_clk = 13000000, .hldt = 0x7, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0xA, .sclht = 0x7, + .fast_mode = true, + }, + { + .core_clk = 12000000, .hldt = 0x7, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0xA, .sclht = 0x6, + .fast_mode = true, + }, + { + .core_clk = 10000000, .hldt = 0x6, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0x8, .sclht = 0x5, + .fast_mode = true, + }, +}; + +static struct smb_timing_t smb_timing_1000khz[] = { + { + .core_clk = 100000000, .hldt = 0x15, .dbcnt = 0x4, + .sclfrq = 0x0, .scllt = 0x1C, .sclht = 0x15, + .fast_mode = true, + }, + { + .core_clk = 62500000, .hldt = 0xF, .dbcnt = 0x3, + .sclfrq = 0x0, .scllt = 0x11, .sclht = 0xE, + .fast_mode = true, + }, + { + .core_clk = 50000000, .hldt = 0xA, .dbcnt = 0x2, + .sclfrq = 0x0, .scllt = 0xE, .sclht = 0xB, + .fast_mode = true, + }, + { + .core_clk = 48000000, .hldt = 0x9, .dbcnt = 0x2, + .sclfrq = 0x0, .scllt = 0xD, .sclht = 0xB, + .fast_mode = true, + }, + { + .core_clk = 41000000, .hldt = 0x9, .dbcnt = 0x2, + .sclfrq = 0x0, .scllt = 0xC, .sclht = 0x9, + .fast_mode = true, + }, + { + .core_clk = 40000000, .hldt = 0x8, .dbcnt = 0x2, + .sclfrq = 0x0, .scllt = 0xB, .sclht = 0x9, + .fast_mode = true, + }, + { + .core_clk = 33000000, .hldt = 0x7, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0xA, .sclht = 0x7, + .fast_mode = true, + }, + { + .core_clk = 25000000, .hldt = 0x4, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0x7, .sclht = 0x6, + .fast_mode = true, + }, + { + .core_clk = 24000000, .hldt = 0x7, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0x8, .sclht = 0x5, + .fast_mode = true, + }, + { + .core_clk = 20000000, .hldt = 0x4, .dbcnt = 0x1, + .sclfrq = 0x0, .scllt = 0x6, .sclht = 0x4, + .fast_mode = true, + }, +}; + struct npcm_i2c_data { u8 fifo_size; u32 segctl_init_val; @@ -1805,102 +2064,45 @@ static void npcm_i2c_recovery_init(struct i2c_adapter *_adap) */ static int npcm_i2c_init_clk(struct npcm_i2c *bus, u32 bus_freq_hz) { - u32 k1 = 0; - u32 k2 = 0; - u8 dbnct = 0; - u32 sclfrq = 0; - u8 hldt = 7; + struct smb_timing_t *smb_timing; + u8 scl_table_cnt = 0, table_size = 0; u8 fast_mode = 0; - u32 src_clk_khz; - u32 bus_freq_khz; - src_clk_khz = bus->apb_clk / 1000; - bus_freq_khz = bus_freq_hz / 1000; bus->bus_freq = bus_freq_hz; - /* 100KHz and below: */ - if (bus_freq_hz <= I2C_MAX_STANDARD_MODE_FREQ) { - sclfrq = src_clk_khz / (bus_freq_khz * 4); - - if (sclfrq < SCLFRQ_MIN || sclfrq > SCLFRQ_MAX) - return -EDOM; - - if (src_clk_khz >= 40000) - hldt = 17; - else if (src_clk_khz >= 12500) - hldt = 15; - else - hldt = 7; - } - - /* 400KHz: */ - else if (bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ) { - sclfrq = 0; + switch (bus_freq_hz) { + case I2C_MAX_STANDARD_MODE_FREQ: + smb_timing = smb_timing_100khz; + table_size = ARRAY_SIZE(smb_timing_100khz); + break; + case I2C_MAX_FAST_MODE_FREQ: + smb_timing = smb_timing_400khz; + table_size = ARRAY_SIZE(smb_timing_400khz); fast_mode = I2CCTL3_400K_MODE; - - if (src_clk_khz < 7500) - /* 400KHZ cannot be supported for core clock < 7.5MHz */ - return -EDOM; - - else if (src_clk_khz >= 50000) { - k1 = 80; - k2 = 48; - hldt = 12; - dbnct = 7; - } - - /* Master or Slave with frequency > 25MHz */ - else if (src_clk_khz > 25000) { - hldt = clk_coef(src_clk_khz, 300) + 7; - k1 = clk_coef(src_clk_khz, 1600); - k2 = clk_coef(src_clk_khz, 900); - } - } - - /* 1MHz: */ - else if (bus_freq_hz <= I2C_MAX_FAST_MODE_PLUS_FREQ) { - sclfrq = 0; + break; + case I2C_MAX_FAST_MODE_PLUS_FREQ: + smb_timing = smb_timing_1000khz; + table_size = ARRAY_SIZE(smb_timing_1000khz); fast_mode = I2CCTL3_400K_MODE; - - /* 1MHZ cannot be supported for core clock < 24 MHz */ - if (src_clk_khz < 24000) - return -EDOM; - - k1 = clk_coef(src_clk_khz, 620); - k2 = clk_coef(src_clk_khz, 380); - - /* Core clk > 40 MHz */ - if (src_clk_khz > 40000) { - /* - * Set HLDT: - * SDA hold time: (HLDT-7) * T(CLK) >= 120 - * HLDT = 120/T(CLK) + 7 = 120 * FREQ(CLK) + 7 - */ - hldt = clk_coef(src_clk_khz, 120) + 7; - } else { - hldt = 7; - dbnct = 2; - } + break; + default: + return -EINVAL; } - /* Frequency larger than 1 MHz is not supported */ - else - return -EINVAL; + for (scl_table_cnt = 0; scl_table_cnt < table_size; scl_table_cnt++) + if (bus->apb_clk >= smb_timing[scl_table_cnt].core_clk) + break; - if (bus_freq_hz >= I2C_MAX_FAST_MODE_FREQ) { - k1 = round_up(k1, 2); - k2 = round_up(k2 + 1, 2); - if (k1 < SCLFRQ_MIN || k1 > SCLFRQ_MAX || - k2 < SCLFRQ_MIN || k2 > SCLFRQ_MAX) - return -EDOM; - } + if (scl_table_cnt == table_size) + return -EINVAL; /* write sclfrq value. bits [6:0] are in I2CCTL2 reg */ - iowrite8(FIELD_PREP(I2CCTL2_SCLFRQ6_0, sclfrq & 0x7F), + iowrite8(FIELD_PREP(I2CCTL2_SCLFRQ6_0, smb_timing[scl_table_cnt].sclfrq & 0x7F), bus->reg + NPCM_I2CCTL2); /* bits [8:7] are in I2CCTL3 reg */ - iowrite8(fast_mode | FIELD_PREP(I2CCTL3_SCLFRQ8_7, (sclfrq >> 7) & 0x3), + iowrite8(FIELD_PREP(I2CCTL3_SCLFRQ8_7, (smb_timing[scl_table_cnt].sclfrq >> 7) & 0x3) | + fast_mode, bus->reg + NPCM_I2CCTL3); /* Select Bank 0 to access NPCM_I2CCTL4/NPCM_I2CCTL5 */ @@ -1912,13 +2114,13 @@ static int npcm_i2c_init_clk(struct npcm_i2c *bus, u32 bus_freq_hz) * k1 = 2 * SCLLT7-0 -> Low Time = k1 / 2 * k2 = 2 * SCLLT7-0 -> High Time = k2 / 2 */ - iowrite8(k1 / 2, bus->reg + NPCM_I2CSCLLT); - iowrite8(k2 / 2, bus->reg + NPCM_I2CSCLHT); + iowrite8(smb_timing[scl_table_cnt].scllt, bus->reg + NPCM_I2CSCLLT); + iowrite8(smb_timing[scl_table_cnt].sclht, bus->reg + NPCM_I2CSCLHT); - iowrite8(dbnct, bus->reg + NPCM_I2CCTL5); + iowrite8(smb_timing[scl_table_cnt].dbcnt, bus->reg + NPCM_I2CCTL5); } - iowrite8(hldt, bus->reg + NPCM_I2CCTL4); + iowrite8(smb_timing[scl_table_cnt].hldt, bus->reg + NPCM_I2CCTL4); /* Return to Bank 1, and stay there by default: */ npcm_i2c_select_bank(bus, I2C_BANK_1); From patchwork Thu Dec 19 09:08:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tyrone Ting X-Patchwork-Id: 852599 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B9D122147D; Thu, 19 Dec 2024 09:09:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734599360; cv=none; b=Nfl/LJ1D16TaT19XUI/5bezT9RYvrY/7QPSQv8790cfRsCYWRIiTXFpyCt3T3HAcxd/HeVYQs+jktn9c/Qss9zQwSbXF2yovajgxaLuz7MC4LoDcYeLPbeb/x/VJvQhFNzd8il1XE2NsUCQ0p44u1sfuJSIvOAjImnWkQnrT/mo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734599360; c=relaxed/simple; bh=zCTHUokrIv5wtZdtVEeGYNWrFA+kwuabQ6ehlYaapbg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=VTAjiLo1SYNH0ZjUuM6nPmOwCAI+q0VaOkVILtaOdT87ev658wSsj/IvdYUFni1NKjsJ76X8kg57d3P1yhbJDOpqWeCpgQXAEP/w6XNDWykwvZHWopppohliMVSJGTMXJC+bmbHnjkTvvV2VqXYXhHbW6Elky7oisQSBzlEQJlA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=VC0ONggL; arc=none smtp.client-ip=209.85.214.169 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.b="VC0ONggL" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2165cb60719so5183865ad.0; Thu, 19 Dec 2024 01:09:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734599358; x=1735204158; darn=vger.kernel.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=jzZT6TwhtRV8As1g0Br4z6oNEAAXSFB3IR9CjR0VtHs=; b=VC0ONggLXh0k8EtLZJQKQvy55UGT5QhMBw48ChaHt7bA+AhQPgGsaMOQlZYyA5JZ0x UTYM9vnChussgfg99Au3Jx2VGKOQICSemJrrNVMNdX2TOYLwZhhxRR946Wmz5UEBrrv/ wibUGFw6Q4ycqUaP9fGc4vg9q3eujl/fHBirnpv6/LFDG5sNKUxfQet8Vc3ngq1fqRNB 9LIbUe0wTw/ImbFlf1p3nTfNYXm3z6ZSOdmPXpgy82o/6QTir7/VHsxjWOAhjKm8LFG1 tB6gN8KAKc0i4uEkyqlNaks3s17Lz909liyxg8YDjw0y2M27ziEByukiq3XuoEkkmmAQ L9Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734599358; x=1735204158; h=references:in-reply-to:message-id:date:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=jzZT6TwhtRV8As1g0Br4z6oNEAAXSFB3IR9CjR0VtHs=; b=NzhyjmqfDxCIi8uCcP0bkUaOHLDpUfq1+qvCPfN+Kbu92OSbHFcstAI8YBbTXoDWeo M68qZltxa08axprZH78qnwtE6ilEWv2jNlEl+61+TXCefoD7gp31ijFqKlAWcywJBzr7 7liUlpJn+vROTiSOx8FOlwJ52D/sy6lJXScbnTRHtevePg9fB4MGPSkRhVUqSOaadFIw 36klH3XcQ3z8y4AJF36udfZtjgnAVh0tOoACBrrno5S//O84tIuw9OpFPztA73ybSo8I CYhQygzHnWLzyb3pOwGameNQMthgp5JuR3fE6R+/3Ry5Usv1aeTrsOuW7gSK7+Gh9Lbx SYjQ== X-Forwarded-Encrypted: i=1; AJvYcCU4Qe+qvrEodR2Fnzh9rQP+l/I7iF5Ss5qt8Ul266J6fyDglaawVcXvrseqodzORKZH3S9IynRQjHc=@vger.kernel.org, AJvYcCVl1nsDhqu/RnRwLNdDSsSfjFK0copFxW8p4+F0AveM6QQPLFmwVU3BdQVkuQGcEUZHUNXYY8eo+dGmalay@vger.kernel.org X-Gm-Message-State: AOJu0YwRz2+7s7g7rJiWI89xFytaTuDnoBQxkL84WGlspy0nWVj6gItL aDYfWuMue4XasyFbG9Taf2nf1VEtmcpiM5VMevPZH4t8a6n2KUk= X-Gm-Gg: ASbGnct8W/KpAxNm2ROjNW8rKfrxOgy/c2pNNKkvjkhBaBFen1aTVHZoAwChA36S9j2 IsOGLz3j3RgjWxSU6/K4wA+7e+0Ei0vWhBlDU36vNUqzqTymgq9mrXKq1T6f28gwyxR516egNHF N0H33FMpcsx1jMQjydeeIYNK5d2HlL3EttFBZpPwxyayehV9KIjYJ2HtRG0+kfC1JyYRDnni1b0 IDekCua1Z/4O9OLEurRio5aZddDhLxTHNdeXXRJJ8oGDpz8Efe/gq5GFbcxNibbF9ccsqWpw0eX PlX784gBhckiufLgJdEYeiZFFJv8S67kmxHJL7R1wPEM2zD5 X-Google-Smtp-Source: AGHT+IErJZuvW91Ox1GbnItc2RtMNO/lrRVXIpHpCWNjIRs59rsAH/atATMYg7HsgdgvdzF4Cm1AQA== X-Received: by 2002:a17:902:fc4d:b0:216:2dc5:233c with SMTP id d9443c01a7336-219d9692f2dmr44680875ad.41.1734599358516; Thu, 19 Dec 2024 01:09:18 -0800 (PST) Received: from localhost (2001-b400-e356-b45b-f574-35d0-91d3-527b.emome-ip6.hinet.net. [2001:b400:e356:b45b:f574:35d0:91d3:527b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc962db4sm8151225ad.7.2024.12.19.01.09.17 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 19 Dec 2024 01:09:18 -0800 (PST) From: Tyrone Ting X-Google-Original-From: Tyrone Ting To: avifishman70@gmail.com, tmaimon77@gmail.com, tali.perry1@gmail.com, venture@google.com, yuenn@google.com, benjaminfair@google.com, andi.shyti@kernel.org, andriy.shevchenko@linux.intel.com, wsa@kernel.org, rand.sec96@gmail.com, wsa+renesas@sang-engineering.com, warp5tw@gmail.com, tali.perry@nuvoton.com, Avi.Fishman@nuvoton.com, tomer.maimon@nuvoton.com, KWLIU@nuvoton.com, JJLIU0@nuvoton.com, kfting@nuvoton.com Cc: openbmc@lists.ozlabs.org, linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, Charles Boyer , Vivekanand Veeracholan Subject: [PATCH v8 4/4] i2c: npcm: Enable slave in eob interrupt Date: Thu, 19 Dec 2024 17:08:59 +0800 Message-Id: <20241219090859.18722-5-kfting@nuvoton.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20241219090859.18722-1-kfting@nuvoton.com> References: <20241219090859.18722-1-kfting@nuvoton.com> Precedence: bulk X-Mailing-List: linux-i2c@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: From: Charles Boyer Nuvoton slave enable was in user space API call master_xfer, so it is subject to delays from the OS scheduler. If the BMC is not enabled for slave mode in time for master to send response, then it will NAK the address match. Then the PLDM request timeout occurs. If the slave enable is moved to the EOB interrupt service routine, then the BMC can be ready in slave mode by the time it needs to receive a response. Signed-off-by: Charles Boyer Signed-off-by: Vivekanand Veeracholan Signed-off-by: Tyrone Ting Reviewed-by: Tali Perry --- drivers/i2c/busses/i2c-npcm7xx.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/i2c/busses/i2c-npcm7xx.c b/drivers/i2c/busses/i2c-npcm7xx.c index 1aae1a8a8055..3ca08b8ef8af 100644 --- a/drivers/i2c/busses/i2c-npcm7xx.c +++ b/drivers/i2c/busses/i2c-npcm7xx.c @@ -1925,6 +1925,12 @@ static int npcm_i2c_int_master_handler(struct npcm_i2c *bus) (FIELD_GET(NPCM_I2CCST3_EO_BUSY, ioread8(bus->reg + NPCM_I2CCST3)))) { npcm_i2c_irq_handle_eob(bus); +#if IS_ENABLED(CONFIG_I2C_SLAVE) + /* reenable slave if it was enabled */ + if (bus->slave) + iowrite8(bus->slave->addr | NPCM_I2CADDR_SAEN, + bus->reg + NPCM_I2CADDR1); +#endif return 0; }