From owner-freebsd-arm@freebsd.org Sun Apr 25 15:41:41 2021 Return-Path: Delivered-To: freebsd-arm@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 7CF3E626881 for ; Sun, 25 Apr 2021 15:41:41 +0000 (UTC) (envelope-from danny@cs.huji.ac.il) Received: from kabab.cs.huji.ac.il (kabab.cs.huji.ac.il [132.65.116.210]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4FSshP2Dv3z3lS0; Sun, 25 Apr 2021 15:41:40 +0000 (UTC) (envelope-from danny@cs.huji.ac.il) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=cs.huji.ac.il; s=57791128; h=To:References:Message-Id:Content-Transfer-Encoding:Cc:Date:In-Reply-To:From:Subject:Mime-Version:Content-Type; bh=f/TZuK+z92KgTLSU86+RgOwf1cdeCXu1B+nCLzYSD8E=; b=wol/O1xxx11kALwZobD8uIg5k7W+jbaltzo4THxlfFJefXK+IDpqBHc8404JjjK9ZiBFNQs4ORCJyLxZuSm0CzDUUeXrSQH8YAVsvv2YvfffqLBxVxtvSInIAY0iXlV7+Kwsk5vM2wU0u7jIXPDDmnTfUYoAa6/NADM4vJIrcTYIYZTdtkyIiPrDNwMvaEV3/ecsq/qPzHP5WC6As4jjqS8G3wSPtD0lKC6H77rZ/n4hyCQmeQh/VPFcjWWl0pn+Em3+ZBU9qjRrBPeR0aXwW1GZZnu6uPb3OiNut17AXYr+B+xrE1n+vnCI8HRYRWQntf6HugxwqpPh2nEJs4fPtA==; Received: from mbpro2.bk.cs.huji.ac.il ([132.65.179.20] helo=smtpclient.apple) by kabab.cs.huji.ac.il with esmtp id 1lagt1-0000KC-Jr; Sun, 25 Apr 2021 18:41:31 +0300 Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.80.0.2.41\)) Subject: Re: I2C/IIC working on RPI4 8GB? From: Daniel Braniss In-Reply-To: <1C2DD11C-B1F6-4C2A-9AB0-5F1553520FF5@FreeBSD.org> Date: Sun, 25 Apr 2021 18:41:31 +0300 Cc: freebsd-arm Content-Transfer-Encoding: quoted-printable Message-Id: References: <1C2DD11C-B1F6-4C2A-9AB0-5F1553520FF5@FreeBSD.org> To: Mark Murray X-Mailer: Apple Mail (2.3654.80.0.2.41) X-Rspamd-Queue-Id: 4FSshP2Dv3z3lS0 X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[] X-BeenThere: freebsd-arm@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Porting FreeBSD to ARM processors List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 25 Apr 2021 15:41:41 -0000 > On 25 Apr 2021, at 15:32, Mark Murray wrote: >=20 > Hi All, >=20 > Does anyone here have IIC/I2C working on a CURRENT (I'm running = latest) FreeBSD? >=20 > I've never seen it work; Last time I had working IIC was on an RPI3. >=20 > There is a /dev/iic0, and I have a known working RTC on it, but the = scan just times out: >=20 > [root@grasshopper ~]# i2c -s > Hardware may not support START/STOP scanning; trying less-reliable = read method. > > Scanning I2C devices on /dev/iic0: >=20 >=20 > I suspect something not-quite-right in DTS land, but I lack the = knowledge to investigate. I've done some random stumbling around in the = dark with overlays and the like, but I've either seen the above error, = or a total lack of /dev/iic0. >=20 > Thanks! >=20 > M > -- > Mark R V Murray >=20 Q1: do you see /dev/iic? Q2: i2c -s shows anything? fianlly, you can try my patch, it works for me, but I understand it=E2=80=99= s a five kilo hammer :-) diff --git a/sys/dev/iicbus/twsi/twsi.c b/sys/dev/iicbus/twsi/twsi.c index a606c2aef..8ede62073 100644 --- a/sys/dev/iicbus/twsi/twsi.c +++ b/sys/dev/iicbus/twsi/twsi.c @@ -86,6 +86,8 @@ __FBSDID("$FreeBSD$"); #define TWSI_DEBUG #undef TWSI_DEBUG +#define debug_(dev, fmt, args...) device_printf(dev, "%s: " fmt, = __func__, ##args) + #ifdef TWSI_DEBUG #define debugf(dev, fmt, args...) device_printf(dev, "%s: " fmt, = __func__, ##args) #else @@ -103,6 +105,7 @@ TWSI_READ(struct twsi_softc *sc, bus_size_t off) { uint32_t val; + DELAY(1000); // danny: needed=20 val =3D bus_read_4(sc->res[0], off); debugf(sc->dev, "read %x from %lx\n", val, off); return (val); @@ -165,15 +168,14 @@ twsi_clear_iflg(struct twsi_softc *sc) static int twsi_poll_ctrl(struct twsi_softc *sc, int timeout, uint32_t mask) { - timeout /=3D 10; - debugf(sc->dev, "Waiting for ctrl reg to match mask %x\n", = mask); + debug_(sc->dev, "Waiting for ctrl reg to match mask %x = timeout=3D%d\n", mask, timeout); while (!(TWSI_READ(sc, sc->reg_control) & mask)) { - DELAY(10); + // DELAY(10); if (--timeout < 0) return (timeout); } - debugf(sc->dev, "done\n"); + debug_(sc->dev, "done\n"); return (0); } @@ -212,7 +214,7 @@ twsi_locked_start(device_t dev, struct twsi_softc = *sc, int32_t mask, DELAY(1000); if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) { - debugf(dev, "timeout sending %sSTART condition\n", + debug_(dev, "timeout sending %sSTART condition\n", mask =3D=3D TWSI_STATUS_START ? "" : "repeated "); return (IIC_ETIMEOUT); } @@ -221,7 +223,7 @@ twsi_locked_start(device_t dev, struct twsi_softc = *sc, int32_t mask, debugf(dev, "status=3D%x\n", status); if (status !=3D mask) { - debugf(dev, "wrong status (%02x) after sending %sSTART = condition\n", + debug_(dev, "wrong status (%02x) after sending %sSTART = condition\n", status, mask =3D=3D TWSI_STATUS_START ? "" : = "repeated "); return (IIC_ESTATUS); } @@ -231,7 +233,7 @@ twsi_locked_start(device_t dev, struct twsi_softc = *sc, int32_t mask, DELAY(1000); if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) { - debugf(dev, "timeout sending slave address = (timeout=3D%d)\n", timeout); + debug_(dev, "timeout sending slave address = (timeout=3D%d)\n", timeout); return (IIC_ETIMEOUT); } @@ -239,7 +241,7 @@ twsi_locked_start(device_t dev, struct twsi_softc = *sc, int32_t mask, status =3D TWSI_READ(sc, sc->reg_status); if (status !=3D (read_access ? TWSI_STATUS_ADDR_R_ACK : TWSI_STATUS_ADDR_W_ACK)) { - debugf(dev, "no ACK (status: %02x) after sending slave = address\n", + debug_(dev, "no ACK (status: %02x) after sending slave = address\n", status); return (IIC_ENOACK); } @@ -405,7 +407,8 @@ twsi_read(device_t dev, char *buf, int len, int = *read, int last, int delay) int last_byte, rv; sc =3D device_get_softc(dev); - + debug_(dev, "twsi_read: len=3D%d delay=3D%d", len, delay); // = danny +=09 mtx_lock(&sc->mutex); *read =3D 0; while (*read < len) { @@ -423,7 +426,7 @@ twsi_read(device_t dev, char *buf, int len, int = *read, int last, int delay) DELAY(1000); if (twsi_poll_ctrl(sc, delay, TWSI_CONTROL_IFLG)) { - debugf(dev, "timeout reading data (delay=3D%d)\n",= delay); + debug_(dev, "timeout reading data (delay=3D%d)\n",= delay); rv =3D IIC_ETIMEOUT; goto out; } @@ -431,7 +434,7 @@ twsi_read(device_t dev, char *buf, int len, int = *read, int last, int delay) status =3D TWSI_READ(sc, sc->reg_status); if (status !=3D (last_byte ? TWSI_STATUS_DATA_RD_NOACK : = TWSI_STATUS_DATA_RD_ACK)) { - debugf(dev, "wrong status (%02x) while = reading\n", status); + debug_(dev, "wrong status (%02x) while = reading\n", status); rv =3D IIC_ESTATUS; goto out; } @@ -462,14 +465,14 @@ twsi_write(device_t dev, const char *buf, int len, = int *sent, int timeout) twsi_clear_iflg(sc); DELAY(1000); if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) { - debugf(dev, "timeout writing data = (timeout=3D%d)\n", timeout); + debug_(dev, "timeout writing data = (timeout=3D%d)\n", timeout); rv =3D IIC_ETIMEOUT; goto out; } status =3D TWSI_READ(sc, sc->reg_status); if (status !=3D TWSI_STATUS_DATA_WR_ACK) { - debugf(dev, "wrong status (%02x) while = writing\n", status); + debug_(dev, "wrong status (%02x) while = writing\n", status); rv =3D IIC_ESTATUS; goto out; } @@ -496,8 +499,12 @@ twsi_transfer(device_t dev, struct iic_msg *msgs, = uint32_t nmsgs) sc->control_val =3D TWSI_CONTROL_TWSIEN | TWSI_CONTROL_INTEN | TWSI_CONTROL_ACK; TWSI_WRITE(sc, sc->reg_control, sc->control_val); - debugf(dev, "transmitting %d messages\n", nmsgs); - debugf(sc->dev, "status=3D%x\n", TWSI_READ(sc, sc->reg_status)); +#if 0 + debug_(dev, "transmitting %d messages\n", nmsgs); + debug_(sc->dev, "status=3D%x\n", TWSI_READ(sc, sc->reg_status)); +#else + DELAY(8000); +#endif sc->nmsgs =3D nmsgs; sc->msgs =3D msgs; sc->msg_idx =3D 0; @@ -519,15 +526,24 @@ twsi_transfer(device_t dev, struct iic_msg *msgs, = uint32_t nmsgs) debugf(sc->dev, "pause finish\n"); if (sc->error) { - debugf(sc->dev, "Error, aborting (%d)\n", sc->error); + debug_(sc->dev, "Error, aborting (%d)\n", sc->error); TWSI_WRITE(sc, sc->reg_control, 0); } /* Disable module and interrupts */ - debugf(sc->dev, "status=3D%x\n", TWSI_READ(sc, sc->reg_status)); +#if 0 + debug_(sc->dev, "status=3D%x\n", TWSI_READ(sc, sc->reg_status)); = // TWSI_WRITE(sc, sc->reg_control, 0); - debugf(sc->dev, "status=3D%x\n", TWSI_READ(sc, sc->reg_status)); - + debugf(sc->dev, "status=3D%x\n", TWSI_READ(sc, sc->reg_status)); = // + debugf(sc->dev, "error=3D%d\n", sc->error); // danny +#else + int status; + DELAY(8000); // danny: works! + status =3D TWSI_READ(sc, sc->reg_status); + TWSI_WRITE(sc, sc->reg_control, 0); + status =3D TWSI_READ(sc, sc->reg_status); + //debug_(sc->dev, "status=3D%x\n", TWSI_READ(sc, = sc->reg_status)); +#endif return (sc->error); } @@ -581,7 +597,7 @@ twsi_intr(void *arg) case TWSI_STATUS_ADDR_W_NACK: case TWSI_STATUS_ADDR_R_NACK: - debugf(sc->dev, "No ack received after transmitting the = address\n"); + debug_(sc->dev, "No ack received after transmitting the = address\n"); sc->transfer =3D 0; sc->error =3D IIC_ENOACK; sc->control_val =3D 0; @@ -662,7 +678,7 @@ twsi_intr(void *arg) break; default: - debugf(sc->dev, "status=3D%x hot handled\n", status); + debug_(sc->dev, "status=3D%x hot handled\n", status); sc->transfer =3D 0; sc->error =3D IIC_EBUSERR; sc->control_val =3D 0;=