From nobody Mon Dec 12 14:35:35 2022 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 4NW4133tffz4kX4x; Mon, 12 Dec 2022 14:35:35 +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 4NW4133Jcgz4KLl; Mon, 12 Dec 2022 14:35:35 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1670855735; 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=tyXtXBh8nA9ZURzD7pgtfDKjHoy3iL5T86xvnxsL09U=; b=BZgAKCCP8mlwuZshSznIpX19xG3GZIpZilLyQ0WJB7GHygt/ytkfYNZK98UGpHgIsxDovv LCu9N3OQ/GQcb/eMTTTFv5KSu2BNst0VA/YgTyaabdM9uMblaQhhbT3BCbJ1z2v9bMt3I4 eCpgCwZCrVrZAPJwWRYtM0nsTIjvElknutXxMeaTA62n3+bSTvZLbF8MmsqPYefu5oMrJ/ WhvcXC2XqO7k6dlhxOf53+JjUp7IIHBthNCz9jNM6mpQXkjqE88srWPNykNpdOWeBabzmg kO3v6wN0waDnwXS5qnuDUhPYhRsH+LEQFQrxjUuzMZL2Ek6hqW5blrGkMW30qA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1670855735; 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=tyXtXBh8nA9ZURzD7pgtfDKjHoy3iL5T86xvnxsL09U=; b=HP3rQhttlYjnXEThQ/BGdDm++TGALFnx3+u0v+I3F3BztL8B4S+fFqLmdhZEUaaoZaS2gl lObObGMukJmtUE9L9bwIBeeffHrczVrLDCXdEKJHi3B7IY0ynf9ki0TDCj1bO6lp8MQxDj XMrjDjSyX0FHOiO8yQw9u7m6LHFiIpRJfDekzeDpd07jd7jJZJkwwRnbXaSkuAUl+sGWDk Dt8lXwnnfJmBx2/3NmqxcudiRO330iKiHVFbgESDjmCR59nDNWqxbhhQ3t9XRuRJ7ED3jl oaYsgHZML8nDTRMIi9SzZO1fStsmfJ6g8MkaULwdNNxAvExrTkAceaggvKMd6w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1670855735; a=rsa-sha256; cv=none; b=LFCQ50o2EwNjr/AjkXabl8oKsmWDSkXunKWIG1osTLShPqkPhSPYGU5qpvOes4mII3UrkI AnpDre3zlC92oYMgucUM9WMrMMd4iCWKeGQ0nT4+jUlIgsFALqoRBjvVWyS/OIL71+o0rV GATvh9hfkwCA19qbUcwHHHil/9cS3Gs9MWRaw1ledqab1gaob23B5arS9pBw5ghdqnPnip 0BzhmJ4VTcD3ZAy7gBKAH+F5LAQ5ssiijyk4cvP6BtKHwL5CZMr1nwbFamFFFsZ4qEqljQ P7ddpHXGsK9hwl/xm+wQTFxjrvE+Zl2MwfgImjAkW4X5qxojFEU/mdhynOB1cw== 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 4NW4132LKjzdNC; Mon, 12 Dec 2022 14:35:35 +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 2BCEZZmX071111; Mon, 12 Dec 2022 14:35:35 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2BCEZZ76071110; Mon, 12 Dec 2022 14:35:35 GMT (envelope-from git) Date: Mon, 12 Dec 2022 14:35:35 GMT Message-Id: <202212121435.2BCEZZ76071110@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Ganbold Tsagaankhuu Subject: git: 896d3e43b101 - main - Add driver for Rockchip One Time Programmable (OTP) device. This driver created the possibility to assign fixed MAC adresses to eqos devices. 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: ganbold X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 896d3e43b101d027bd5fe9d6825a20c793df9b0f Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by ganbold: URL: https://cgit.FreeBSD.org/src/commit/?id=896d3e43b101d027bd5fe9d6825a20c793df9b0f commit 896d3e43b101d027bd5fe9d6825a20c793df9b0f Author: Søren Schmidt AuthorDate: 2022-12-12 14:34:18 +0000 Commit: Ganbold Tsagaankhuu CommitDate: 2022-12-12 14:34:18 +0000 Add driver for Rockchip One Time Programmable (OTP) device. This driver created the possibility to assign fixed MAC adresses to eqos devices. --- sys/arm64/rockchip/rk_otp.c | 192 +++++++++++++++++++++++++++++++++++++++++ sys/arm64/rockchip/rk_otp.h | 26 ++++++ sys/arm64/rockchip/rk_otp_if.m | 44 ++++++++++ 3 files changed, 262 insertions(+) diff --git a/sys/arm64/rockchip/rk_otp.c b/sys/arm64/rockchip/rk_otp.c new file mode 100644 index 000000000000..79464be75a18 --- /dev/null +++ b/sys/arm64/rockchip/rk_otp.c @@ -0,0 +1,192 @@ +/*- + * Copyright (c) 2022 Soren Schmidt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include "rk_otp.h" +#include "rk_otp_if.h" + +#define OTPC_SBPI_CTRL 0x0020 +#define SBPI_ENABLE_MASK 0x00010000 +#define SBPI_ENABLE 1 +#define SBPI_DAP_ADDR_MASK 0xff000000 +#define SBPI_DAP_ADDR 0x02 +#define SBPI_DAP_ADDR_SHIFT 8 +#define OTPC_SBPI_CMD_VALID_PRE 0x0024 +#define SBPI_CMD_VALID_MASK 0xffff0000 +#define OTPC_SBPI_INT_STATUS 0x0304 +#define OTPC_SBPI_DONE 2 +#define OTPC_USER_DONE 4 +#define OTPC_USER_CTRL 0x0100 +#define OTPC_USER_MASK 0xffff0000 +#define OTPC_USER 1 +#define OTPC_USER_ADDR 0x0104 +#define OTPC_USER_ADDR_MASK 0xffff0000 +#define OTPC_USER_ENABLE 0x0108 +#define OTPC_USER_FSM_ENABLE_MASK 0xffff0000 +#define OTPC_USER_FSM_ENABLE 1 +#define OTPC_USER_Q 0x0124 +#define OTPC_SBPI_CMD0_OFFSET 0x1000 +#define SBPI_DAP_CMD_WRF 0xc0 +#define SBPI_DAP_REG_ECC 0x3a +#define OTPC_SBPI_CMD1_OFFSET 0x1004 +#define SBPI_ECC_ENABLE 0x00 +#define SBPI_ECC_DISABLE 0x09 + + +static struct ofw_compat_data compat_data[] = { + {"rockchip,rk3568-otp", 1}, + {NULL, 0} +}; + +static struct rk_otp_softc { + struct resource *mem; +} rk_otp_sc; + + +static int +rk_otp_wait(struct rk_otp_softc *sc, uint32_t status) +{ + int retry = 10000; + + while (!(bus_read_4(sc->mem, OTPC_SBPI_INT_STATUS) & status)) { + DELAY(10); + if (--retry == 0) + return (ETIMEDOUT); + } + + /* clear status */ + bus_write_4(sc->mem, OTPC_SBPI_INT_STATUS, status); + + return (0); +} + +static int +rk_otp_ecc(struct rk_otp_softc *sc, int enable) +{ + bus_write_4(sc->mem, OTPC_SBPI_CTRL, + SBPI_DAP_ADDR_MASK | (SBPI_DAP_ADDR << SBPI_DAP_ADDR_SHIFT)); + bus_write_4(sc->mem, OTPC_SBPI_CMD_VALID_PRE, + SBPI_CMD_VALID_MASK | 0x1); + bus_write_4(sc->mem, OTPC_SBPI_CMD0_OFFSET, + SBPI_DAP_CMD_WRF | SBPI_DAP_REG_ECC); + if (enable) + bus_write_4(sc->mem, OTPC_SBPI_CMD1_OFFSET, SBPI_ECC_ENABLE); + else + bus_write_4(sc->mem, OTPC_SBPI_CMD1_OFFSET, SBPI_ECC_DISABLE); + bus_write_4(sc->mem, OTPC_SBPI_CTRL, SBPI_ENABLE_MASK | SBPI_ENABLE); + + return (rk_otp_wait(sc, OTPC_SBPI_DONE)); +} + +int +rk_otp_read(device_t dev, uint8_t *buffer, int offset, int size) +{ + struct rk_otp_softc *sc = &rk_otp_sc; + int error; + + /* if not initialized just error out */ + if (!sc->mem) + return (ENXIO); + + if ((error = rk_otp_ecc(sc, 1))) { + device_printf(dev, "timeout waiting for OTP ECC status\n"); + return (error); + } + + bus_write_4(sc->mem, OTPC_USER_CTRL, OTPC_USER | OTPC_USER_MASK); + DELAY(5); + while (size--) { + bus_write_4(sc->mem, OTPC_USER_ADDR, + offset++ | OTPC_USER_ADDR_MASK); + bus_write_4(sc->mem, OTPC_USER_ENABLE, + OTPC_USER_FSM_ENABLE | OTPC_USER_FSM_ENABLE_MASK); + + if ((error = rk_otp_wait(sc, OTPC_USER_DONE))) { + device_printf(dev, "timeout waiting for OTP data\n"); + break; + } + *buffer++ = bus_read_4(sc->mem, OTPC_USER_Q); + } + bus_write_4(sc->mem, OTPC_USER_CTRL, OTPC_USER_MASK); + + return (error); +} + +static int +rk_otp_probe(device_t dev) +{ + + if (!ofw_bus_status_okay(dev)) + return (ENXIO); + if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) + return (ENXIO); + device_set_desc(dev, "RockChip OTP"); + return (BUS_PROBE_DEFAULT); +} + +static int +rk_otp_attach(device_t dev) +{ + struct rk_otp_softc *sc = &rk_otp_sc; + int rid = 0; + + sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); + if (!sc->mem) { + device_printf(dev, "Cannot allocate memory resources\n"); + return (ENXIO); + } + return (0); +} + + +static device_method_t rk_otp_methods[] = { + /* Device interface */ + DEVMETHOD(device_probe, rk_otp_probe), + DEVMETHOD(device_attach, rk_otp_attach), + + DEVMETHOD_END +}; + +DEFINE_CLASS_1(rk_otp, rk_otp_driver, rk_otp_methods, + sizeof(struct simple_mfd_softc), simple_mfd_driver); +EARLY_DRIVER_MODULE(rk_otp, simplebus, rk_otp_driver, 0, 0, + BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); diff --git a/sys/arm64/rockchip/rk_otp.h b/sys/arm64/rockchip/rk_otp.h new file mode 100644 index 000000000000..52a752d55229 --- /dev/null +++ b/sys/arm64/rockchip/rk_otp.h @@ -0,0 +1,26 @@ +/*- + * Copyright (c) 2022 Soren Schmidt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +int rk_otp_read(device_t dev, uint8_t *buffer, int offset, int size); diff --git a/sys/arm64/rockchip/rk_otp_if.m b/sys/arm64/rockchip/rk_otp_if.m new file mode 100644 index 000000000000..35164a1ccc67 --- /dev/null +++ b/sys/arm64/rockchip/rk_otp_if.m @@ -0,0 +1,44 @@ +#- +# Copyright (c) 2022 Soren Schmidt +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $Id: rk_otp_if.m 921 2022-08-09 18:38:11Z sos $ +# + +#include + +INTERFACE rk_otp; + +HEADER { +}; + +# +# Initialize SoC specific registers. +# +METHOD int read { + device_t dev; + void *buffer; + int offset; + int size; +} DEFAULT rk_otp_read;