From owner-freebsd-net@freebsd.org Mon Mar 6 06:50:06 2017 Return-Path: Delivered-To: freebsd-net@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 7EE38CFB10F for ; Mon, 6 Mar 2017 06:50:06 +0000 (UTC) (envelope-from sebastian.huber@embedded-brains.de) Received: from dedi548.your-server.de (dedi548.your-server.de [85.10.215.148]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 3F3531D6B for ; Mon, 6 Mar 2017 06:50:05 +0000 (UTC) (envelope-from sebastian.huber@embedded-brains.de) Received: from [88.198.220.130] (helo=sslproxy01.your-server.de) by dedi548.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.85_2) (envelope-from ) id 1ckmSz-0006Yn-2n for freebsd-net@freebsd.org; Mon, 06 Mar 2017 07:49:57 +0100 Received: from [82.135.62.35] (helo=mail.embedded-brains.de) by sslproxy01.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ckmSy-0006lQ-Os for freebsd-net@freebsd.org; Mon, 06 Mar 2017 07:49:56 +0100 Received: from localhost (localhost.localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id 8C1EC2A1805 for ; Mon, 6 Mar 2017 07:50:04 +0100 (CET) Received: from mail.embedded-brains.de ([127.0.0.1]) by localhost (zimbra.eb.localhost [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id 1cKXrkir_mKI; Mon, 6 Mar 2017 07:50:04 +0100 (CET) Received: from localhost (localhost.localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id 259822A1804; Mon, 6 Mar 2017 07:50:04 +0100 (CET) X-Virus-Scanned: amavisd-new at zimbra.eb.localhost Received: from mail.embedded-brains.de ([127.0.0.1]) by localhost (zimbra.eb.localhost [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id WEDQ_dh5jSpR; Mon, 6 Mar 2017 07:50:04 +0100 (CET) Received: from huber-linux.eb.localhost (unknown [192.168.96.129]) by mail.embedded-brains.de (Postfix) with ESMTP id 05C912A17F3; Mon, 6 Mar 2017 07:50:04 +0100 (CET) From: Sebastian Huber To: freebsd-net@freebsd.org Cc: Sebastian Huber Subject: [PATCH 1/2] if_tsec: Fix MII communication Date: Mon, 6 Mar 2017 07:49:53 +0100 Message-Id: <1488782994-9064-1-git-send-email-sebastian.huber@embedded-brains.de> X-Mailer: git-send-email 1.8.4.5 X-Authenticated-Sender: smtp-embedded@poldinet.de X-Virus-Scanned: Clear (ClamAV 0.99.2/23174/Mon Mar 6 05:58:01 2017) X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 06 Mar 2017 06:50:06 -0000 Use a common tsec_mii_wait() function to busy wait for status changes. The status indicators are not set immediatly after a command. Discard the first value. Unlock the PHY mutex after a timeout in tsec_init_locked(). Tested on the P1020RDB. --- sys/dev/tsec/if_tsec.c | 58 +++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/sys/dev/tsec/if_tsec.c b/sys/dev/tsec/if_tsec.c index 3fd8726..927d339 100644 --- a/sys/dev/tsec/if_tsec.c +++ b/sys/dev/tsec/if_tsec.c @@ -357,13 +357,32 @@ tsec_init(void *xsc) TSEC_GLOBAL_UNLOCK(sc); } +static int +tsec_mii_wait(struct tsec_softc *sc, uint32_t flags) +{ + int timeout; + + /* + * The status indicators are not set immediatly after a command. + * Discard the first value. + */ + TSEC_PHY_READ(sc, TSEC_REG_MIIMIND); + + timeout = TSEC_READ_RETRY; + while ((TSEC_PHY_READ(sc, TSEC_REG_MIIMIND) & flags) && --timeout) + DELAY(TSEC_READ_DELAY); + + return (timeout == 0); +} + static void tsec_init_locked(struct tsec_softc *sc) { struct tsec_desc *tx_desc = sc->tsec_tx_vaddr; struct tsec_desc *rx_desc = sc->tsec_rx_vaddr; struct ifnet *ifp = sc->tsec_ifp; - uint32_t timeout, val, i; + uint32_t val, i; + int timeout; if (ifp->if_drv_flags & IFF_DRV_RUNNING) return; @@ -422,15 +441,13 @@ tsec_init_locked(struct tsec_softc *sc) TSEC_PHY_WRITE(sc, TSEC_REG_MIIMCFG, TSEC_MIIMCFG_CLKDIV28); /* Step 8: Read MII Mgmt indicator register and check for Busy = 0 */ - timeout = TSEC_READ_RETRY; - while (--timeout && (TSEC_PHY_READ(sc, TSEC_REG_MIIMIND) & - TSEC_MIIMIND_BUSY)) - DELAY(TSEC_READ_DELAY); - if (timeout == 0) { + timeout = tsec_mii_wait(sc, TSEC_MIIMIND_BUSY); + + TSEC_PHY_UNLOCK(sc); + if (timeout) { if_printf(ifp, "tsec_init_locked(): Mgmt busy timeout\n"); return; } - TSEC_PHY_UNLOCK(sc); /* Step 9: Setup the MII Mgmt */ mii_mediachg(sc->tsec_mii); @@ -1570,7 +1587,7 @@ int tsec_miibus_readreg(device_t dev, int phy, int reg) { struct tsec_softc *sc; - uint32_t timeout; + int timeout; int rv; sc = device_get_softc(dev); @@ -1579,18 +1596,11 @@ tsec_miibus_readreg(device_t dev, int phy, int reg) TSEC_PHY_WRITE(sc, TSEC_REG_MIIMADD, (phy << 8) | reg); TSEC_PHY_WRITE(sc, TSEC_REG_MIIMCOM, 0); TSEC_PHY_WRITE(sc, TSEC_REG_MIIMCOM, TSEC_MIIMCOM_READCYCLE); - - timeout = TSEC_READ_RETRY; - while (--timeout && TSEC_PHY_READ(sc, TSEC_REG_MIIMIND) & - (TSEC_MIIMIND_NOTVALID | TSEC_MIIMIND_BUSY)) - DELAY(TSEC_READ_DELAY); - - if (timeout == 0) - device_printf(dev, "Timeout while reading from PHY!\n"); - + timeout = tsec_mii_wait(sc, TSEC_MIIMIND_NOTVALID | TSEC_MIIMIND_BUSY); rv = TSEC_PHY_READ(sc, TSEC_REG_MIIMSTAT); TSEC_PHY_UNLOCK(); - + if (timeout) + device_printf(dev, "Timeout while reading from PHY!\n"); return (rv); } @@ -1598,23 +1608,17 @@ int tsec_miibus_writereg(device_t dev, int phy, int reg, int value) { struct tsec_softc *sc; - uint32_t timeout; + int timeout; sc = device_get_softc(dev); TSEC_PHY_LOCK(); TSEC_PHY_WRITE(sc, TSEC_REG_MIIMADD, (phy << 8) | reg); TSEC_PHY_WRITE(sc, TSEC_REG_MIIMCON, value); - - timeout = TSEC_READ_RETRY; - while (--timeout && (TSEC_READ(sc, TSEC_REG_MIIMIND) & - TSEC_MIIMIND_BUSY)) - DELAY(TSEC_READ_DELAY); + timeout = tsec_mii_wait(sc, TSEC_MIIMIND_BUSY); TSEC_PHY_UNLOCK(); - - if (timeout == 0) + if (timeout) device_printf(dev, "Timeout while writing to PHY!\n"); - return (0); } -- 1.8.4.5