From owner-svn-src-head@freebsd.org Sat May 11 15:03:52 2019 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 4A0FB159F89A; Sat, 11 May 2019 15:03:52 +0000 (UTC) (envelope-from manu@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) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id E4F228C3ED; Sat, 11 May 2019 15:03:51 +0000 (UTC) (envelope-from manu@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id BC2871D36A; Sat, 11 May 2019 15:03:51 +0000 (UTC) (envelope-from manu@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x4BF3ppA096498; Sat, 11 May 2019 15:03:51 GMT (envelope-from manu@FreeBSD.org) Received: (from manu@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x4BF3pij096497; Sat, 11 May 2019 15:03:51 GMT (envelope-from manu@FreeBSD.org) Message-Id: <201905111503.x4BF3pij096497@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: manu set sender to manu@FreeBSD.org using -f From: Emmanuel Vadot Date: Sat, 11 May 2019 15:03:51 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r347491 - head/sys/dev/iicbus/twsi X-SVN-Group: head X-SVN-Commit-Author: manu X-SVN-Commit-Paths: head/sys/dev/iicbus/twsi X-SVN-Commit-Revision: 347491 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: E4F228C3ED X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.96 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_MEDIUM(-1.00)[-0.996,0]; NEURAL_HAM_SHORT(-0.96)[-0.962,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US]; NEURAL_HAM_LONG(-1.00)[-1.000,0] X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 11 May 2019 15:03:52 -0000 Author: manu Date: Sat May 11 15:03:51 2019 New Revision: 347491 URL: https://svnweb.freebsd.org/changeset/base/347491 Log: twsi: Calculate the clock param based on the bus frequency Instead of precalculating the different speed, respect the bus frequency and calculate the clock register parameter based on it. If the platform didn't register the core clk, fallback on the precomputed values (This is likely do be the case on Marvell boards). Modified: head/sys/dev/iicbus/twsi/a10_twsi.c head/sys/dev/iicbus/twsi/twsi.c Modified: head/sys/dev/iicbus/twsi/a10_twsi.c ============================================================================== --- head/sys/dev/iicbus/twsi/a10_twsi.c Sat May 11 15:02:55 2019 (r347490) +++ head/sys/dev/iicbus/twsi/a10_twsi.c Sat May 11 15:03:51 2019 (r347491) @@ -87,9 +87,7 @@ static int a10_twsi_attach(device_t dev) { struct twsi_softc *sc; - clk_t clk; hwreset_t rst; - uint64_t freq; int error; sc = device_get_softc(dev); @@ -104,12 +102,12 @@ a10_twsi_attach(device_t dev) } /* Activate clock */ - error = clk_get_by_ofw_index(dev, 0, 0, &clk); + error = clk_get_by_ofw_index(dev, 0, 0, &sc->clk_core); if (error != 0) { device_printf(dev, "could not find clock\n"); return (error); } - error = clk_enable(clk); + error = clk_enable(sc->clk_core); if (error != 0) { device_printf(dev, "could not enable clock\n"); return (error); @@ -122,31 +120,6 @@ a10_twsi_attach(device_t dev) sc->reg_status = TWI_STAT; sc->reg_baud_rate = TWI_CCR; sc->reg_soft_reset = TWI_SRST; - - /* Setup baud rate params */ - clk_get_freq(clk, &freq); - switch (freq) { - /* - * Formula is - * F0 = FINT / 2 ^ CLK_N - * F1 = F0 / (CLK_M + 1) - * - * Doc says that the output freq is F1/10 but my logic analyzer says otherwise - */ - case 48000000: - sc->baud_rate[IIC_SLOW].param = TWSI_BAUD_RATE_PARAM(11, 1); - sc->baud_rate[IIC_FAST].param = TWSI_BAUD_RATE_PARAM(11, 1); - sc->baud_rate[IIC_FASTEST].param = TWSI_BAUD_RATE_PARAM(2, 1); - break; - case 24000000: - sc->baud_rate[IIC_SLOW].param = TWSI_BAUD_RATE_PARAM(5, 2); - sc->baud_rate[IIC_FAST].param = TWSI_BAUD_RATE_PARAM(5, 2); - sc->baud_rate[IIC_FASTEST].param = TWSI_BAUD_RATE_PARAM(2, 2); - break; - default: - device_printf(dev, "Non supported frequency\n"); - return (ENXIO); - } sc->need_ack = true; return (twsi_attach(dev)); Modified: head/sys/dev/iicbus/twsi/twsi.c ============================================================================== --- head/sys/dev/iicbus/twsi/twsi.c Sat May 11 15:02:55 2019 (r347490) +++ head/sys/dev/iicbus/twsi/twsi.c Sat May 11 15:03:51 2019 (r347491) @@ -243,6 +243,43 @@ twsi_locked_start(device_t dev, struct twsi_softc *sc, return (IIC_NOERR); } +#ifdef EXT_RESOURCES +#define TWSI_BAUD_RATE_RAW(C,M,N) ((C)/((10*(M+1))<<(N))) +#define ABSSUB(a,b) (((a) > (b)) ? (a) - (b) : (b) - (a)) + +static int +twsi_calc_baud_rate(struct twsi_softc *sc, const u_int target, + int *param) +{ + uint64_t clk; + uint32_t cur, diff, diff0; + int m, n, m0, n0; + + /* Calculate baud rate. */ + diff0 = 0xffffffff; + + if (clk_get_freq(sc->clk_core, &clk) < 0) + return (-1); + + debugf(sc->dev, "Bus clock is at %lu\n", clk); + + for (n = 0; n < 8; n++) { + for (m = 0; m < 16; m++) { + cur = TWSI_BAUD_RATE_RAW(clk,m,n); + diff = ABSSUB(target, cur); + if (diff < diff0) { + m0 = m; + n0 = n; + diff0 = diff; + } + } + } + *param = TWSI_BAUD_RATE_PARAM(m0, n0); + + return (0); +} +#endif /* EXT_RESOURCES */ + /* * Only slave mode supported, disregard [old]addr */ @@ -251,24 +288,36 @@ twsi_reset(device_t dev, u_char speed, u_char addr, u_ { struct twsi_softc *sc; uint32_t param; - /* uint32_t val; */ +#ifdef EXT_RESOURCES + u_int busfreq; +#endif sc = device_get_softc(dev); - switch (speed) { - case IIC_SLOW: - case IIC_FAST: - param = sc->baud_rate[speed].param; - debugf(dev, "Using IIC_FAST mode with speed param=%x\n", param); - break; - case IIC_FASTEST: - case IIC_UNKNOWN: - default: - param = sc->baud_rate[IIC_FAST].param; - debugf(dev, "Using IIC_FASTEST/UNKNOWN mode with speed param=%x\n", param); - break; +#ifdef EXT_RESOURCES + busfreq = IICBUS_GET_FREQUENCY(sc->iicbus, speed); + + if (twsi_calc_baud_rate(sc, busfreq, ¶m) == -1) { +#endif + switch (speed) { + case IIC_SLOW: + case IIC_FAST: + param = sc->baud_rate[speed].param; + debugf(dev, "Using IIC_FAST mode with speed param=%x\n", param); + break; + case IIC_FASTEST: + case IIC_UNKNOWN: + default: + param = sc->baud_rate[IIC_FAST].param; + debugf(dev, "Using IIC_FASTEST/UNKNOWN mode with speed param=%x\n", param); + break; + } +#ifdef EXT_RESOURCES } +#endif + debugf(dev, "Using clock param=%x\n", param); + mtx_lock(&sc->mutex); TWSI_WRITE(sc, sc->reg_soft_reset, 0x0); TWSI_WRITE(sc, sc->reg_baud_rate, param); @@ -521,6 +570,7 @@ twsi_intr(void *arg) break; case TWSI_STATUS_ADDR_R_ACK: + debugf(sc->dev, "Ack received after transmitting the address\n"); sc->recv_bytes = 0; TWSI_WRITE(sc, sc->reg_control, sc->control_val);