From nobody Wed Dec 22 06:59:23 2021 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 0972918FA7D0; Wed, 22 Dec 2021 06:59:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4JJkhW6CMCz3PRx; Wed, 22 Dec 2021 06:59:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 918B61884F; Wed, 22 Dec 2021 06:59:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1BM6xNDP036989; Wed, 22 Dec 2021 06:59:23 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1BM6xN6t036988; Wed, 22 Dec 2021 06:59:23 GMT (envelope-from git) Date: Wed, 22 Dec 2021 06:59:23 GMT Message-Id: <202112220659.1BM6xN6t036988@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Andriy Gapon Subject: git: 342d73431ee5 - stable/12 - rk_i2c_fill_tx: fix a number of issues List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: avg X-Git-Repository: src X-Git-Refname: refs/heads/stable/12 X-Git-Reftype: branch X-Git-Commit: 342d73431ee555ae9a62820c28a39ee53cffc9e5 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1640156364; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=nTjDl/kmia9nzfJf5wJfTsTc9sbCzASJy4AAoEHQHyc=; b=MekhD0dxjLMr8JgTWM66AtezGwgokf9i3j1wfng3vznNY3b+cY9E7M7fCbr6zp9KpNmmss DmyGOgKPCpo44VaGksZ+dlnoBS0pDEEQ+E2iP+J08U3X1tMODXoxbsiVCMDACsuo6CAZnE +7hfojp1L76cZDBNUhPw8j6B6DrKQaixl7F9tK35aTz1PeG1f7g8D4to8AeBR7rUYO6Qi7 ADNARJyKjeZdKEH7SuqE/2zjllUUzwH+wdC/JZ+RtvROfxNc+9+3zexD+6960TOVLjfKSJ 3fyqV008Yc/oFbek29S7PJkJz/5q+bTSNpdEdzcGNtKOAv5IKfeezWraRi7ddA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1640156364; a=rsa-sha256; cv=none; b=hTiU1YPdyWcsBNvVEC6sAOI4EplcdjOkCGeGhl6R0p7CbsDiGhbQq+2KC/pXatba3jBADY N2y4IR4pQSlaNTdrxefNNKElFGwblMw7aMxFJVrpglee6MHo+ao/ZLZ76mKz+viAIAdzF/ gEcMliawMhTxsChxN0Iw0tokxVasOxNNmITZCfs/TpyBSdj+2GNFT7RHMAc4cxy8fepcHM LJN5Ext3bsml8p7g82lSbKDRO7V+t+JvvKui+yxmSWc6ebjRAOphwGAKUfuHLO/PiFKO+u +58y/BwfelgJ9gnIRIF9rBzhmvo5LinFgBlv098O6nmT4kSnJLFGfLSJB6MQMA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/12 has been updated by avg: URL: https://cgit.FreeBSD.org/src/commit/?id=342d73431ee555ae9a62820c28a39ee53cffc9e5 commit 342d73431ee555ae9a62820c28a39ee53cffc9e5 Author: Andriy Gapon AuthorDate: 2021-12-15 11:00:45 +0000 Commit: Andriy Gapon CommitDate: 2021-12-22 06:58:58 +0000 rk_i2c_fill_tx: fix a number of issues - maximum number of bytes that can be sent is 32, not 8; - previous interface required callers to bump sc->msg->len in addition to setting sc->tx_slave_addr; - because of the above there was an issue with writing one too many bytes because sc->cnt is not advanced when the slave address is written; - the inetraction between outer and inner loops was confusing as the former was bounded on the number of bytes to write and the counter was incremented by one, but the inner loop advanced four bytes at a time; - the return value was incorrect in the tx_slave_addr case; one call place had to use its own (and incorrect in some cases) notion of the write lenth. All of the above issues should be fixed. Some sanity asserts are added. All callers use the return value to program RK_I2C_MTXCNT. iic_msg::len no longer needs to be hacked. A constant is added to reflect the maximum number of octets that can be sent or received in one go (they are the same). (cherry picked from commit c6635459510c9c03a439bc5b59fef37259d21967) (cherry picked from commit b7bcd21d2da50364a512092cb4e75f2d53070ba6) --- sys/arm64/rockchip/rk_i2c.c | 47 +++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/sys/arm64/rockchip/rk_i2c.c b/sys/arm64/rockchip/rk_i2c.c index 09bfd52a5abe..56d3e6cb2a1c 100644 --- a/sys/arm64/rockchip/rk_i2c.c +++ b/sys/arm64/rockchip/rk_i2c.c @@ -111,6 +111,9 @@ __FBSDID("$FreeBSD$"); #define RK_I2C_RXDATA_BASE 0x200 +/* 8 data registers, 4 bytes each. */ +#define RK_I2C_MAX_RXTX_LEN 32 + enum rk_i2c_state { STATE_IDLE = 0, STATE_START, @@ -215,37 +218,40 @@ rk_i2c_fill_tx(struct rk_i2c_softc *sc) uint8_t buf; int i, j, len; - if (sc->msg == NULL || sc->msg->len == sc->cnt) - return (0); - len = sc->msg->len - sc->cnt; - if (len > 8) - len = 8; + if (sc->tx_slave_addr) { + KASSERT(sc->cnt == 0, ("tx_slave_addr in the middle of data")); + len++; + } - for (i = 0; i < len; i++) { + if (len > RK_I2C_MAX_RXTX_LEN) + len = RK_I2C_MAX_RXTX_LEN; + + for (i = 0; i < len; ) { buf32 = 0; - for (j = 0; j < 4 ; j++) { - if (sc->cnt == sc->msg->len) - break; + /* Process next 4 bytes or whatever remains. */ + for (j = 0; j < MIN(len - i, 4); j++) { /* Fill the addr if needed */ - if (sc->cnt == 0 && sc->tx_slave_addr) { + if (sc->tx_slave_addr) { buf = sc->msg->slave; sc->tx_slave_addr = false; } else { + KASSERT(sc->cnt < sc->msg->len, + ("%s: data buffer overrun", __func__)); buf = sc->msg->buf[sc->cnt]; sc->cnt++; } - buf32 |= buf << (j * 8); - + buf32 |= (uint32_t)buf << (j * 8); } - RK_I2C_WRITE(sc, RK_I2C_TXDATA_BASE + 4 * i, buf32); - if (sc->cnt == sc->msg->len) - break; + KASSERT(i % 4 == 0, ("%s: misaligned write offset", __func__)); + RK_I2C_WRITE(sc, RK_I2C_TXDATA_BASE + i, buf32); + + i += j; } - return (uint8_t)len; + return (len); } static void @@ -262,8 +268,8 @@ rk_i2c_drain_rx(struct rk_i2c_softc *sc) } len = sc->msg->len - sc->cnt; - if (len > 32) - len = 32; + if (len > RK_I2C_MAX_RXTX_LEN) + len = RK_I2C_MAX_RXTX_LEN; for (i = 0; i < len; i++) { if (i % 4 == 0) @@ -335,9 +341,8 @@ rk_i2c_intr_locked(struct rk_i2c_softc *sc) RK_I2C_WRITE(sc, RK_I2C_IEN, RK_I2C_IEN_MBTFIEN | RK_I2C_IEN_NAKRCVIEN); - sc->msg->len += 1; - rk_i2c_fill_tx(sc); - RK_I2C_WRITE(sc, RK_I2C_MTXCNT, sc->msg->len); + transfer_len = rk_i2c_fill_tx(sc); + RK_I2C_WRITE(sc, RK_I2C_MTXCNT, transfer_len); } break; case STATE_READ: