From nobody Wed Sep 7 15:10:44 2022 X-Original-To: dev-commits-src-branches@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 4MN5Kw6wdrz4bPSw; Wed, 7 Sep 2022 15:10:44 +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 4MN5Kw6TL1z3bZv; Wed, 7 Sep 2022 15:10:44 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1662563444; 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=wJkJ5L0339aY4WrIEyOBd6i7fKOdaXGeoW4nnO/hqYs=; b=NIgQtya9tb7/EMHViYF/XP7HRjV5qJwp7CothvnRpREOGQBJvs6VzIQaXncY6y/U/uEGrj ejYmZgRqQhPVdH5ixb2mKg5Q38mopWAtOeHXRyguZuUpv+y11CesecC1a2wFCreH59hss3 3qLj7NJuah59d8YypI0cszxv9ExlR4qoSw1vUV+Mq/St5KpoBp9Vi6DnRvTM91f08ed6SA XvVBm6ZKYA9KcTFH6yQGTFif+UYDkrfOQ4GvzZoYROhNEjsXysxhARmEeV68WTUAl5zdvI hF+wVZhl7aiXh3+GOYpLCsujYzPw9v15/hyK2RB1z5qy8EBzFkUNg6+m2Sx2xQ== 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 4MN5Kw5XmCzZC3; Wed, 7 Sep 2022 15:10:44 +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 287FAi2K027959; Wed, 7 Sep 2022 15:10:44 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 287FAi71027958; Wed, 7 Sep 2022 15:10:44 GMT (envelope-from git) Date: Wed, 7 Sep 2022 15:10:44 GMT Message-Id: <202209071510.287FAi71027958@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Emmanuel Vadot Subject: git: b3814a4806dc - stable/13 - linuxkpi: unbreak linux_i2cbb List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: manu X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: b3814a4806dc6117644a7faee157e98c5bc5b2bf Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1662563444; 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=wJkJ5L0339aY4WrIEyOBd6i7fKOdaXGeoW4nnO/hqYs=; b=mwG59kde6oa2/ykoQ9SxhMsYTCKSPEjxn+JzXFD+z5nEV2XXdNC7HgPPGogFPJXWiZKUvo yhnGfu3jhdIUHXK7YUGhrAfbYFiIjsh0SUqOvpSsxE5dZ/Z8UJxBBzGrcsNeIP+KQq+vPk VoOQzdOS/fqDw2Vtr0/6D3553rdABt/4J8hNOqQIPmUIMSDapSHbv7A1NBDIv/vypHzjnv SawDJ3PwRFMCJZA9YHg47/8oEe93iwYeeqdagcZiXjNYbxVVGg4sGwpJnjnSUQT1CCOCkj Yzzzq8kOeWmR9FFob9jtEA7NbtnSMkmPkXh7M21pQI0EiAO3W1CJ6byx8vAlCw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1662563444; a=rsa-sha256; cv=none; b=bDBRKKS8V+az0c2m4FsjxyPdOhv5t85/njzRmWjvfyiSbFZxWhCG0o58PZCwY5tSeQp5t6 XtNUP7k2ADqN+agaLHNx3icDpeERh3QyuZH6Kx+XjjkMs+lM7WbQPAWYY5QJLTGGb0R0Mv A9vW0SkTa0BVPcKQhJvJHnZPCMd969+5zGlICNsAFhzjD22nyEG4hlCXOpUringhyTffy7 iFxa8h6tb92esuGuhnvzTOx4HMrrHZCZCSwVRKYx2jukvMiIA4plHTGlMSriQeLDO96v6i PW7tiiLoZ1r6mOITIQL0/TeIjRWwL50G40azC6KGvfK3fNwQAPBJqh4RDPyJxw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by manu: URL: https://cgit.FreeBSD.org/src/commit/?id=b3814a4806dc6117644a7faee157e98c5bc5b2bf commit b3814a4806dc6117644a7faee157e98c5bc5b2bf Author: Emmanuel Vadot AuthorDate: 2022-08-18 15:34:35 +0000 Commit: Emmanuel Vadot CommitDate: 2022-09-07 15:09:06 +0000 linuxkpi: unbreak linux_i2cbb This is a joint work with manu. - fixed conditions in do_i2c_transfer and i2c_transfer as linux_i2cbb does not set adapter->algo->master_xfer but does set adapter->algo_data; - fixed parent bus specification for linux_i2cbb driver module; - actually implemented iicbb_transfer method; - added iicbb_pre_xfer and iicbb_post_xfer methods; - removed unnecessary and harmful delays (and other extra logic) from iicbb methods as iicbb driver already has them; - added setting of iicbb speed based on algo_data->udelay, so that iicbb uses correct delays; PR: 265920 Fixes: 1961a14a4743 linuxkpi: Add i2c support MFC after: 2 weeks Sponsored by: Beckhoff Automation GmbH & Co. KG (manu's work) (cherry picked from commit 6364180582b769b8fb8fba83511748af3b2c9efd) --- sys/compat/linuxkpi/common/include/linux/i2c.h | 4 +- sys/compat/linuxkpi/common/src/linux_i2cbb.c | 155 ++++++++++++++++++------- 2 files changed, 118 insertions(+), 41 deletions(-) diff --git a/sys/compat/linuxkpi/common/include/linux/i2c.h b/sys/compat/linuxkpi/common/include/linux/i2c.h index 0bb8b470edd7..365ab893fdfd 100644 --- a/sys/compat/linuxkpi/common/include/linux/i2c.h +++ b/sys/compat/linuxkpi/common/include/linux/i2c.h @@ -100,7 +100,7 @@ do_i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int nmsgs) retries = adapter->retries == 0 ? 1 : adapter->retries; for (; retries != 0; retries--) { - if (adapter->algo->master_xfer != NULL) + if (adapter->algo != NULL && adapter->algo->master_xfer != NULL) ret = adapter->algo->master_xfer(adapter, msgs, nmsgs); else ret = lkpi_i2cbb_transfer(adapter, msgs, nmsgs); @@ -116,7 +116,7 @@ i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int nmsgs) { int ret; - if (!adapter->algo) + if (adapter->algo == NULL && adapter->algo_data == NULL) return (-EOPNOTSUPP); if (adapter->lock_ops) diff --git a/sys/compat/linuxkpi/common/src/linux_i2cbb.c b/sys/compat/linuxkpi/common/src/linux_i2cbb.c index f7321f060fa0..7d1fd453d357 100644 --- a/sys/compat/linuxkpi/common/src/linux_i2cbb.c +++ b/sys/compat/linuxkpi/common/src/linux_i2cbb.c @@ -41,6 +41,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include "iicbus_if.h" #include "iicbb_if.h" #include "lkpi_iic_if.h" @@ -49,12 +50,35 @@ static void lkpi_iicbb_setscl(device_t dev, int val); static int lkpi_iicbb_getscl(device_t dev); static int lkpi_iicbb_getsda(device_t dev); static int lkpi_iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr); +static int lkpi_iicbb_pre_xfer(device_t dev); +static void lkpi_iicbb_post_xfer(device_t dev); struct lkpi_iicbb_softc { device_t iicbb; struct i2c_adapter *adapter; }; +static struct sx lkpi_sx_i2cbb; + +static void +lkpi_sysinit_i2cbb(void *arg __unused) +{ + + sx_init(&lkpi_sx_i2cbb, "lkpi-i2cbb"); +} + +static void +lkpi_sysuninit_i2cbb(void *arg __unused) +{ + + sx_destroy(&lkpi_sx_i2cbb); +} + +SYSINIT(lkpi_i2cbb, SI_SUB_DRIVERS, SI_ORDER_ANY, + lkpi_sysinit_i2cbb, NULL); +SYSUNINIT(lkpi_i2cbb, SI_SUB_DRIVERS, SI_ORDER_ANY, + lkpi_sysuninit_i2cbb, NULL); + static int lkpi_iicbb_probe(device_t dev) { @@ -93,10 +117,17 @@ static int lkpi_iicbb_add_adapter(device_t dev, struct i2c_adapter *adapter) { struct lkpi_iicbb_softc *sc; + struct i2c_algo_bit_data *algo_data; sc = device_get_softc(dev); sc->adapter = adapter; + /* + * Set iicbb timing parameters deriving speed from the protocol delay. + */ + algo_data = adapter->algo_data; + if (algo_data->udelay != 0) + IICBUS_RESET(sc->iicbb, 1000000 / algo_data->udelay, 0, NULL); return (0); } @@ -123,6 +154,8 @@ static device_method_t lkpi_iicbb_methods[] = { DEVMETHOD(iicbb_getsda, lkpi_iicbb_getsda), DEVMETHOD(iicbb_getscl, lkpi_iicbb_getscl), DEVMETHOD(iicbb_reset, lkpi_iicbb_reset), + DEVMETHOD(iicbb_pre_xfer, lkpi_iicbb_pre_xfer), + DEVMETHOD(iicbb_post_xfer, lkpi_iicbb_post_xfer), /* lkpi_iicbb interface */ DEVMETHOD(lkpi_iic_add_adapter, lkpi_iicbb_add_adapter), @@ -139,9 +172,10 @@ driver_t lkpi_iicbb_driver = { sizeof(struct lkpi_iicbb_softc), }; -DRIVER_MODULE(lkpi_iicbb, lkpi_iic, lkpi_iicbb_driver, lkpi_iicbb_devclass, 0, 0); +DRIVER_MODULE(lkpi_iicbb, drmn, lkpi_iicbb_driver, lkpi_iicbb_devclass, 0, 0); +DRIVER_MODULE(lkpi_iicbb, drm, lkpi_iicbb_driver, lkpi_iicbb_devclass, 0, 0); DRIVER_MODULE(iicbb, lkpi_iicbb, iicbb_driver, iicbb_devclass, 0, 0); -MODULE_DEPEND(lkpi_iicbb, iicbb, IICBB_MINVER, IICBB_PREFVER, IICBB_MAXVER); +MODULE_DEPEND(linuxkpi, iicbb, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); static void lkpi_iicbb_setsda(device_t dev, int val) @@ -150,10 +184,8 @@ lkpi_iicbb_setsda(device_t dev, int val) struct i2c_algo_bit_data *algo_data; sc = device_get_softc(dev); - algo_data = (struct i2c_algo_bit_data *)sc->adapter->algo_data; + algo_data = sc->adapter->algo_data; algo_data->setsda(algo_data->data, val); - cpu_spinwait(); - DELAY(algo_data->udelay); } static void @@ -163,11 +195,8 @@ lkpi_iicbb_setscl(device_t dev, int val) struct i2c_algo_bit_data *algo_data; sc = device_get_softc(dev); - - algo_data = (struct i2c_algo_bit_data *)sc->adapter->algo_data; + algo_data = sc->adapter->algo_data; algo_data->setscl(algo_data->data, val); - cpu_spinwait(); - DELAY(algo_data->udelay); } static int @@ -175,27 +204,11 @@ lkpi_iicbb_getscl(device_t dev) { struct lkpi_iicbb_softc *sc; struct i2c_algo_bit_data *algo_data; - unsigned long orig_ticks; - int ret = 0; + int ret; sc = device_get_softc(dev); - - algo_data = (struct i2c_algo_bit_data *)sc->adapter->algo_data; - - orig_ticks = ticks; - while (!ret) { - ret = algo_data->getscl(algo_data->data); - - if (ret) - break; - - if (ticks > orig_ticks + algo_data->timeout) - return (ETIMEDOUT); - - cpu_spinwait(); - DELAY(algo_data->udelay); - } - DELAY(algo_data->udelay); + algo_data = sc->adapter->algo_data; + ret = algo_data->getscl(algo_data->data); return (ret); } @@ -204,16 +217,11 @@ lkpi_iicbb_getsda(device_t dev) { struct lkpi_iicbb_softc *sc; struct i2c_algo_bit_data *algo_data; - int ret = 0; + int ret; sc = device_get_softc(dev); - algo_data = (struct i2c_algo_bit_data *)sc->adapter->algo_data; - - cpu_spinwait(); - DELAY(algo_data->udelay); + algo_data = sc->adapter->algo_data; ret = algo_data->getsda(algo_data->data); - cpu_spinwait(); - DELAY(algo_data->udelay); return (ret); } @@ -221,15 +229,79 @@ static int lkpi_iicbb_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr) { + /* That doesn't seems to be supported in linux */ return (0); } +static int +lkpi_iicbb_pre_xfer(device_t dev) +{ + struct lkpi_iicbb_softc *sc; + struct i2c_algo_bit_data *algo_data; + int rc = 0; + + sc = device_get_softc(dev); + algo_data = sc->adapter->algo_data; + if (algo_data->pre_xfer != 0) + rc = algo_data->pre_xfer(sc->adapter); + return (rc); +} + +static void +lkpi_iicbb_post_xfer(device_t dev) +{ + struct lkpi_iicbb_softc *sc; + struct i2c_algo_bit_data *algo_data; + + sc = device_get_softc(dev); + algo_data = sc->adapter->algo_data; + if (algo_data->post_xfer != NULL) + algo_data->post_xfer(sc->adapter); +} + int -lkpi_i2cbb_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int nmsgs) +lkpi_i2cbb_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, + int nmsgs) { + struct iic_msg *bsd_msgs; + int ret = ENXIO; + + linux_set_current(curthread); + + bsd_msgs = malloc(sizeof(struct iic_msg) * nmsgs, + M_DEVBUF, M_WAITOK | M_ZERO); + + for (int i = 0; i < nmsgs; i++) { + bsd_msgs[i].slave = msgs[i].addr << 1; + bsd_msgs[i].len = msgs[i].len; + bsd_msgs[i].buf = msgs[i].buf; + if (msgs[i].flags & I2C_M_RD) + bsd_msgs[i].flags |= IIC_M_RD; + if (msgs[i].flags & I2C_M_NOSTART) + bsd_msgs[i].flags |= IIC_M_NOSTART; + } - /* TODO: convert from i2c_msg to iic_msg and call IICBUS_TRANFER */ - return (0); + for (int unit = 0; ; unit++) { + device_t child; + struct lkpi_iicbb_softc *sc; + + child = device_find_child(adapter->dev.parent->bsddev, + "lkpi_iicbb", unit); + if (child == NULL) + break; + if (adapter == LKPI_IIC_GET_ADAPTER(child)) { + sc = device_get_softc(child); + ret = IICBUS_TRANSFER(sc->iicbb, bsd_msgs, nmsgs); + ret = iic2errno(ret); + break; + } + } + + free(bsd_msgs, M_DEVBUF); + + if (ret != 0) + return (-ret); + return (nmsgs); } int @@ -241,19 +313,24 @@ lkpi_i2c_bit_add_bus(struct i2c_adapter *adapter) if (bootverbose) device_printf(adapter->dev.parent->bsddev, "Adding i2c adapter %s\n", adapter->name); + sx_xlock(&lkpi_sx_i2cbb); lkpi_iicbb = device_add_child(adapter->dev.parent->bsddev, "lkpi_iicbb", -1); if (lkpi_iicbb == NULL) { device_printf(adapter->dev.parent->bsddev, "Couldn't add lkpi_iicbb\n"); + sx_xunlock(&lkpi_sx_i2cbb); return (ENXIO); } + bus_topo_lock(); error = bus_generic_attach(adapter->dev.parent->bsddev); + bus_topo_unlock(); if (error) { device_printf(adapter->dev.parent->bsddev, "failed to attach child: error %d\n", error); + sx_xunlock(&lkpi_sx_i2cbb); return (ENXIO); } LKPI_IIC_ADD_ADAPTER(lkpi_iicbb, adapter); + sx_xunlock(&lkpi_sx_i2cbb); return (0); } -