From nobody Fri Nov 20 01:13:44 2026 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 4jVy5h54DDz71pZ8 for ; Fri, 20 Nov 2026 01:13: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) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jVy5h1SjQz4YSW for ; Fri, 20 Nov 2026 01:13:44 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795137224; 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=m5Acn0R758gKZlZ7I1SbDQspHAcoaHDAOuW5lJD5YGU=; b=qr9OhXAbnS6nCnEWpyJT1xu94pouX/ZSnT4k4I1U3e4Nuw2ZMl9/XuQ1aPypnDC0AwDGmW EK/dKSApP7BWZdNMrec9b8ga39OtJ/BCpJ7ZmYzip5pOMTrUdjGdAHzi43GsF9BKq4xnV4 eBUJpy9RRoUrf+1mI2pWRE030IYcbU3do/2EyNjnwW8A1qpA6B+sZn7WUDAubxL9i107xr F++SOvdpBurTjuXdrC5213pSyJNI7oo5YW/vdthyHQ9lsvv1Sg5OoTCbXnxG2sxXzEUwgA 83oymjlzyftx01ZD21ndC+0tSIfERJa0JL3p7MjJq2/iSk4QLJ8xeQcVw5z7Hw== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1795137224; b=ukXWV+a4QcrtsWMfGg734BMrX5k9KIg2JNzJgYCfVLRe4jjFsC+cOuWmkVEjnBDkCOabS9 pu1EfWsVnG6Fc6Ga43sKMQsiWbgH8I7Xk3dwwpopCpif56+eI1kbzz2PRlFtV5nTynV0EQ E50u1xlIfdj0lk+M1Huf2nronlbiZ/TYmoZxu8SssBG8aTlqEvGWXI/5vzoyjXff9NA6xs 3VXxuZtRglA3ItYEZwbq5ACtjjVfMdfCBEyPhbBk+pFreGMihfb20nVjJ3e3mY/2n+1Z+K Cgu9gUplEM/ZaiDyKg2Ox/l0QGNejYp4MQnTgm/TCQg1Ssz72QTkyiC+gj+G0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795137224; 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=m5Acn0R758gKZlZ7I1SbDQspHAcoaHDAOuW5lJD5YGU=; b=vFP336Z1j+fCzLsvr8lTBjc/uoyMWjcTHdSE9h8BdGW+rC4JDZ7jftEURbdkXXV20YkGiQ nz2hlN4hU27fm41inmmq1u2fnT4gvGhJkpIjkfYG4bdIzCCRldz6euG2qSuZkaZnnTBeN2 dkOrc+K1To8TnsV3Qv5gtSaKZ8pcVS1OgaS70NMz4taMXWByWuWx93bLGRkvH2+qZ4Kj9V rSQPHE+dw/7PQD+tkYDMp+hhTKSPpXpdBFJ27IF4B9swm0nZVLHddygh+4EssrVPUDOlZV DJOq7zP9PYoEEvGe9FF3IZgFeC1d06jkzewsBXLTBznLzZVHLcwXZOeOyd42VA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jVy5h0WfMzQFX for ; Fri, 20 Nov 2026 01:13:44 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 1779 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Fri, 20 Nov 2026 01:13:44 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Hayzam Sherif Subject: git: 316e388aae6e - main - clk/mediatek: Add common MediaTek clock and reset support List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: hayzam X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 316e388aae6ed248454c72968c50988221ba57a6 Auto-Submitted: auto-generated Date: Fri, 20 Nov 2026 01:13:44 +0000 Message-Id: <6aff9ec8.1779.63e0e61b@gitrepo.freebsd.org> The branch main has been updated by hayzam: URL: https://cgit.FreeBSD.org/src/commit/?id=316e388aae6ed248454c72968c50988221ba57a6 commit 316e388aae6ed248454c72968c50988221ba57a6 Author: Hayzam Sherif AuthorDate: 2026-11-04 10:26:50 +0000 Commit: Hayzam Sherif CommitDate: 2026-11-20 01:10:53 +0000 clk/mediatek: Add common MediaTek clock and reset support MediaTek SoCs split their clock tree over several controllers built from the same blocks. Add a base driver that maps a controller's registers and registers its table of clocks, and the node types the tables use: - PLL with the MT7986 register layout: the rate is set by the frequency control word and the post divider. A new control word is only taken over when the PLL is enabled, so a running PLL is stopped while its rate is changed, and a PLL that cannot stop keeps its rate. Without a lock bit, a PLL that was never programmed cannot be told from a stopped one, so the node has no gate: firmware starts the PLLs, and a PLL that is off reports 0 Hz and cannot be set. - Mux with an optional gate bit and an optional update latch, changed by read-modify-write or, where the mux register is read-only, through set/clear registers. - Gate, changed in the same two ways. Clocks of other controllers are linked clocks, so the registration order does not matter. A registration failure, including a mux selection the table does not describe, panics, since nodes cannot be unregistered. The clocks the system runs on (bus, DRAM) are marked CLK_NODE_CANNOT_STOP in the tables, together with every node on their path to the crystal that could stop. A controller can also be a reset controller, with banks of assert, deassert and status registers. The single register per bank of older SoCs is not supported yet. Reviewed by: mmel Approved by: markj (mentor) Sponsored by: Alchemilla Ventures Private Limited Differential Revision: https://reviews.freebsd.org/D61187 --- sys/conf/files.arm64 | 6 + sys/dev/clk/mediatek/mtk_clk.c | 227 ++++++++++++++++++++++++++++++++++++ sys/dev/clk/mediatek/mtk_clk.h | 135 +++++++++++++++++++++ sys/dev/clk/mediatek/mtk_clk_gate.c | 112 ++++++++++++++++++ sys/dev/clk/mediatek/mtk_clk_mux.c | 164 ++++++++++++++++++++++++++ sys/dev/clk/mediatek/mtk_clk_pll.c | 214 +++++++++++++++++++++++++++++++++ 6 files changed, 858 insertions(+) diff --git a/sys/conf/files.arm64 b/sys/conf/files.arm64 index 2f914c4efd64..80e58541b4c8 100644 --- a/sys/conf/files.arm64 +++ b/sys/conf/files.arm64 @@ -844,6 +844,12 @@ arm64/rockchip/rk_tsadc.c optional fdt soc_rockchip_rk3399 | fdt soc_rockchip_ arm64/rockchip/rk_pcie.c optional fdt pci soc_rockchip_rk3399 arm64/rockchip/rk_pcie_phy.c optional fdt pci soc_rockchip_rk3399 +# MediaTek Clock support +dev/clk/mediatek/mtk_clk.c optional fdt soc_mediatek +dev/clk/mediatek/mtk_clk_gate.c optional fdt soc_mediatek +dev/clk/mediatek/mtk_clk_mux.c optional fdt soc_mediatek +dev/clk/mediatek/mtk_clk_pll.c optional fdt soc_mediatek + # RockChip Clock support dev/clk/rockchip/rk_cru.c optional fdt soc_rockchip dev/clk/rockchip/rk_clk_armclk.c optional fdt soc_rockchip diff --git a/sys/dev/clk/mediatek/mtk_clk.c b/sys/dev/clk/mediatek/mtk_clk.c new file mode 100644 index 000000000000..02a18b8bb6c6 --- /dev/null +++ b/sys/dev/clk/mediatek/mtk_clk.c @@ -0,0 +1,227 @@ +/* + * Copyright (c) 2026 Hayzam Sherif + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +/* + * Common part of the MediaTek clock controller drivers. Each controller + * supplies a table of clocks. This driver owns the registers and the lock + * for read-modify-write access to them. + */ + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +#include "clkdev_if.h" +#include "hwreset_if.h" + +static int +mtk_clk_write_4(device_t dev, bus_addr_t addr, uint32_t val) +{ + struct mtk_clk_softc *sc = device_get_softc(dev); + + bus_write_4(sc->res, addr, val); + return (0); +} + +static int +mtk_clk_read_4(device_t dev, bus_addr_t addr, uint32_t *val) +{ + struct mtk_clk_softc *sc = device_get_softc(dev); + + *val = bus_read_4(sc->res, addr); + return (0); +} + +static int +mtk_clk_modify_4(device_t dev, bus_addr_t addr, uint32_t clr, uint32_t set) +{ + struct mtk_clk_softc *sc = device_get_softc(dev); + uint32_t val; + + val = bus_read_4(sc->res, addr); + val &= ~clr; + val |= set; + bus_write_4(sc->res, addr, val); + return (0); +} + +static void +mtk_clk_device_lock(device_t dev) +{ + struct mtk_clk_softc *sc = device_get_softc(dev); + + mtx_lock(&sc->mtx); +} + +static void +mtk_clk_device_unlock(device_t dev) +{ + struct mtk_clk_softc *sc = device_get_softc(dev); + + mtx_unlock(&sc->mtx); +} + +static int +mtk_clk_reset_lookup(struct mtk_clk_softc *sc, intptr_t id, bus_size_t *reg, + uint32_t *mask) +{ + const struct mtk_clk_reset_desc *rd = sc->resets; + + if (id < 0 || (size_t)(id / 32) >= rd->nbanks) + return (EINVAL); + *reg = rd->banks[id / 32]; + *mask = 1u << (id % 32); + return (0); +} + +static int +mtk_clk_reset_assert(device_t dev, intptr_t id, bool reset) +{ + struct mtk_clk_softc *sc = device_get_softc(dev); + bus_size_t reg; + uint32_t mask; + int rv; + + if ((rv = mtk_clk_reset_lookup(sc, id, ®, &mask)) != 0) + return (rv); + mtx_lock(&sc->mtx); + bus_write_4(sc->res, reset ? reg : reg + 4, mask); + mtx_unlock(&sc->mtx); + return (0); +} + +static int +mtk_clk_reset_is_asserted(device_t dev, intptr_t id, bool *reset) +{ + struct mtk_clk_softc *sc = device_get_softc(dev); + bus_size_t reg; + uint32_t mask; + int rv; + + if ((rv = mtk_clk_reset_lookup(sc, id, ®, &mask)) != 0) + return (rv); + mtx_lock(&sc->mtx); + *reset = (bus_read_4(sc->res, reg + 8) & mask) != 0; + mtx_unlock(&sc->mtx); + return (0); +} + +static const struct clknode_init_def * +mtk_clk_def_clkdef(const struct mtk_clk_def *def) +{ + switch (def->type) { + case MTK_CLK_LINK: + return (&def->link.clkdef); + case MTK_CLK_FIXED: + return (&def->fixed.clkdef); + case MTK_CLK_PLL: + return (&def->pll.clkdef); + case MTK_CLK_MUX: + return (&def->mux.clkdef); + case MTK_CLK_GATE: + return (&def->gate.clkdef); + default: + return (NULL); + } +} + +int +mtk_clk_attach(device_t dev, const struct mtk_clk_desc *desc) +{ + struct mtk_clk_softc *sc = device_get_softc(dev); + struct mtk_clk_def *def; + size_t i; + int rid, rv; + + rid = 0; + sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); + if (sc->res == NULL) { + device_printf(dev, "cannot allocate registers\n"); + return (ENXIO); + } + mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF); + + sc->clkdom = clkdom_create(dev); + if (sc->clkdom == NULL) { + device_printf(dev, "cannot create clock domain\n"); + rv = ENXIO; + goto fail; + } + + for (i = 0; i < desc->nclks; i++) { + def = &desc->clks[i]; + switch (def->type) { + case MTK_CLK_LINK: + rv = clknode_link_register(sc->clkdom, &def->link); + break; + case MTK_CLK_FIXED: + rv = clknode_fixed_register(sc->clkdom, &def->fixed); + break; + case MTK_CLK_PLL: + rv = mtk_clk_pll_register(sc->clkdom, &def->pll); + break; + case MTK_CLK_MUX: + rv = mtk_clk_mux_register(sc->clkdom, &def->mux); + break; + case MTK_CLK_GATE: + rv = mtk_clk_gate_register(sc->clkdom, &def->gate); + break; + default: + rv = EINVAL; + break; + } + /* + * Nodes cannot be unregistered once others reference them, so + * a failure here cannot be undone. + */ + if (rv != 0) + panic("%s: cannot register clock %s: %d", + device_get_nameunit(dev), + mtk_clk_def_clkdef(def)->name, rv); + } + + if (clkdom_finit(sc->clkdom) != 0) + panic("%s: cannot finalize clock domain", + device_get_nameunit(dev)); + if (bootverbose) + clkdom_dump(sc->clkdom); + + if (desc->resets != NULL) { + sc->resets = desc->resets; + hwreset_register_ofw_provider(dev); + } + + return (0); + +fail: + mtx_destroy(&sc->mtx); + bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->res); + return (rv); +} + +static device_method_t mtk_clk_methods[] = { + DEVMETHOD(clkdev_write_4, mtk_clk_write_4), + DEVMETHOD(clkdev_read_4, mtk_clk_read_4), + DEVMETHOD(clkdev_modify_4, mtk_clk_modify_4), + DEVMETHOD(clkdev_device_lock, mtk_clk_device_lock), + DEVMETHOD(clkdev_device_unlock, mtk_clk_device_unlock), + + DEVMETHOD(hwreset_assert, mtk_clk_reset_assert), + DEVMETHOD(hwreset_is_asserted, mtk_clk_reset_is_asserted), + + DEVMETHOD_END +}; + +DEFINE_CLASS_0(mtk_clk, mtk_clk_driver, mtk_clk_methods, + sizeof(struct mtk_clk_softc)); diff --git a/sys/dev/clk/mediatek/mtk_clk.h b/sys/dev/clk/mediatek/mtk_clk.h new file mode 100644 index 000000000000..ed56bbf8097e --- /dev/null +++ b/sys/dev/clk/mediatek/mtk_clk.h @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2026 Hayzam Sherif + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#ifndef _DEV_CLK_MEDIATEK_MTK_CLK_H_ +#define _DEV_CLK_MEDIATEK_MTK_CLK_H_ + +#include +#include +#include + +/* Clock id for nodes that are not exported through the DT binding. */ +#define MTK_CLK_NOID (-1) + +/* PLL, see mtk_clk_pll.c for the register layout. */ +struct mtk_clk_pll_def { + struct clknode_init_def clkdef; + uint32_t con0; /* control register */ + uint32_t rst_bar; /* CON0 reset bar mask, or 0 */ +}; + +/* + * Mux with an optional gate bit (set means gated) and an optional update + * register latching the new selection. The field is changed by + * read-modify-write of reg, or, with MTK_MUX_SETCLR for a read-only reg, + * through write-1-to-set/clear registers. + */ +#define MTK_MUX_SETCLR 0x0001 + +struct mtk_clk_mux_def { + struct clknode_init_def clkdef; + uint32_t reg; /* selection */ + uint32_t set_reg; + uint32_t clr_reg; + uint32_t shift; + uint32_t width; + int gate_bit; /* -1 if none */ + uint32_t upd_reg; + int upd_bit; /* -1 if none */ + uint32_t flags; +}; + +/* + * Gate bit, changed by read-modify-write of reg, or, with MTK_GATE_SETCLR + * for a read-only reg, through write-1-to-set/clear registers. Normally a + * set bit means the clock is gated; MTK_GATE_INVERTED reverses this. + */ +#define MTK_GATE_SETCLR 0x0001 +#define MTK_GATE_INVERTED 0x0002 + +struct mtk_clk_gate_def { + struct clknode_init_def clkdef; + uint32_t reg; /* gate bit */ + uint32_t set_reg; + uint32_t clr_reg; + uint32_t bit; + uint32_t flags; +}; + +enum mtk_clk_type { + MTK_CLK_LINK = 1, + MTK_CLK_FIXED, + MTK_CLK_PLL, + MTK_CLK_MUX, + MTK_CLK_GATE, +}; + +struct mtk_clk_def { + enum mtk_clk_type type; + union { + struct clk_link_def link; + struct clk_fixed_def fixed; + struct mtk_clk_pll_def pll; + struct mtk_clk_mux_def mux; + struct mtk_clk_gate_def gate; + }; +}; + +/* + * Software resets, in banks of 32. Each bank has a write-1-to-set + * (assert) register, a write-1-to-clear (deassert) register at +4 and a + * status register at +8. Reset id n is bit n % 32 of bank n / 32. + * Older SoCs with a single read-write register per bank would need a + * read-modify-write variant, as for gates and muxes. It is left for the + * first SoC that needs it: the MT7986 clock controllers need none. + */ +struct mtk_clk_reset_desc { + const uint32_t *banks; /* set register of each bank */ + size_t nbanks; +}; + +struct mtk_clk_desc { + struct mtk_clk_def *clks; + size_t nclks; + const struct mtk_clk_reset_desc *resets; /* or NULL */ +}; + +struct mtk_clk_softc { + struct resource *res; + struct mtx mtx; + struct clkdom *clkdom; + const struct mtk_clk_reset_desc *resets; +}; + +/* Helpers for clock tables. */ +#define MTK_LINK(_name) \ + { .type = MTK_CLK_LINK, .link = { \ + .clkdef = { .name = (_name), .id = MTK_CLK_NOID } } } + +#define MTK_FIXED(_id, _name, _freq) \ + { .type = MTK_CLK_FIXED, .fixed = { \ + .clkdef = { .name = (_name), .id = (_id) }, \ + .freq = (_freq) } } + +#define MTK_FACTOR(_id, _name, _parent, _mult, _div) \ + { .type = MTK_CLK_FIXED, .fixed = { \ + .clkdef = { .name = (_name), .id = (_id), \ + .parent_names = (const char *[]){ _parent }, \ + .parent_cnt = 1 }, \ + .mult = (_mult), .div = (_div) } } + +int mtk_clk_attach(device_t dev, const struct mtk_clk_desc *desc); + +int mtk_clk_pll_register(struct clkdom *clkdom, + const struct mtk_clk_pll_def *def); +int mtk_clk_mux_register(struct clkdom *clkdom, + const struct mtk_clk_mux_def *def); +int mtk_clk_gate_register(struct clkdom *clkdom, + const struct mtk_clk_gate_def *def); + +DECLARE_CLASS(mtk_clk_driver); + +#endif /* _DEV_CLK_MEDIATEK_MTK_CLK_H_ */ diff --git a/sys/dev/clk/mediatek/mtk_clk_gate.c b/sys/dev/clk/mediatek/mtk_clk_gate.c new file mode 100644 index 000000000000..1b0d17a20b35 --- /dev/null +++ b/sys/dev/clk/mediatek/mtk_clk_gate.c @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2026 Hayzam Sherif + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +/* + * MediaTek clock gate. The gate bit is read from a register and changed + * by read-modify-write of it, or, where that register is read-only, + * through separate write-1-to-set and write-1-to-clear registers. A set + * bit normally means "gated". + */ + +#include +#include +#include + +#include +#include + +#include "clkdev_if.h" + +struct mtk_clk_gate_sc { + uint32_t reg; + uint32_t set_reg; + uint32_t clr_reg; + uint32_t mask; + bool setclr; + bool inverted; +}; + +#define RD4(_clk, _off, _val) \ + CLKDEV_READ_4(clknode_get_device(_clk), _off, _val) +#define WR4(_clk, _off, _val) \ + CLKDEV_WRITE_4(clknode_get_device(_clk), _off, _val) +#define MD4(_clk, _off, _clr, _set) \ + CLKDEV_MODIFY_4(clknode_get_device(_clk), _off, _clr, _set) +#define DEVICE_LOCK(_clk) \ + CLKDEV_DEVICE_LOCK(clknode_get_device(_clk)) +#define DEVICE_UNLOCK(_clk) \ + CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk)) + +static int +mtk_clk_gate_init(struct clknode *clk, device_t dev) +{ + clknode_init_parent_idx(clk, 0); + return (0); +} + +static int +mtk_clk_gate_set_gate(struct clknode *clk, bool enable) +{ + struct mtk_clk_gate_sc *sc = clknode_get_softc(clk); + bool set; + + set = enable == sc->inverted; + DEVICE_LOCK(clk); + if (!sc->setclr) + MD4(clk, sc->reg, sc->mask, set ? sc->mask : 0); + else + WR4(clk, set ? sc->set_reg : sc->clr_reg, sc->mask); + DEVICE_UNLOCK(clk); + return (0); +} + +static int +mtk_clk_gate_get_gate(struct clknode *clk, bool *enabled) +{ + struct mtk_clk_gate_sc *sc = clknode_get_softc(clk); + uint32_t val; + + DEVICE_LOCK(clk); + RD4(clk, sc->reg, &val); + DEVICE_UNLOCK(clk); + *enabled = ((val & sc->mask) == 0) != sc->inverted; + return (0); +} + +static clknode_method_t mtk_clk_gate_methods[] = { + CLKNODEMETHOD(clknode_init, mtk_clk_gate_init), + CLKNODEMETHOD(clknode_set_gate, mtk_clk_gate_set_gate), + CLKNODEMETHOD(clknode_get_gate, mtk_clk_gate_get_gate), + CLKNODEMETHOD_END +}; + +DEFINE_CLASS_1(mtk_clk_gate_clknode, mtk_clk_gate_clknode_class, + mtk_clk_gate_methods, sizeof(struct mtk_clk_gate_sc), clknode_class); + +int +mtk_clk_gate_register(struct clkdom *clkdom, + const struct mtk_clk_gate_def *def) +{ + struct clknode *clk; + struct mtk_clk_gate_sc *sc; + + clk = clknode_create(clkdom, &mtk_clk_gate_clknode_class, + &def->clkdef); + if (clk == NULL) + return (ENXIO); + + sc = clknode_get_softc(clk); + sc->reg = def->reg; + sc->set_reg = def->set_reg; + sc->clr_reg = def->clr_reg; + sc->mask = 1u << def->bit; + sc->setclr = (def->flags & MTK_GATE_SETCLR) != 0; + sc->inverted = (def->flags & MTK_GATE_INVERTED) != 0; + + if (clknode_register(clkdom, clk) == NULL) + return (ENXIO); + return (0); +} diff --git a/sys/dev/clk/mediatek/mtk_clk_mux.c b/sys/dev/clk/mediatek/mtk_clk_mux.c new file mode 100644 index 000000000000..2af1e77f8e1f --- /dev/null +++ b/sys/dev/clk/mediatek/mtk_clk_mux.c @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2026 Hayzam Sherif + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +/* + * MediaTek clock mux. The selection is read from a register and changed + * by read-modify-write of it, or, where that register is read-only, + * through separate write-1-to-clear and write-1-to-set registers. + * Optionally the mux has a gate bit in the same field group (set means + * gated) and an update register whose bit latches a new selection. + */ + +#include +#include +#include + +#include +#include + +#include "clkdev_if.h" + +struct mtk_clk_mux_sc { + uint32_t reg; + uint32_t set_reg; + uint32_t clr_reg; + uint32_t shift; + uint32_t mask; + int gate_bit; + uint32_t upd_reg; + int upd_bit; + bool setclr; +}; + +#define RD4(_clk, _off, _val) \ + CLKDEV_READ_4(clknode_get_device(_clk), _off, _val) +#define WR4(_clk, _off, _val) \ + CLKDEV_WRITE_4(clknode_get_device(_clk), _off, _val) +#define MD4(_clk, _off, _clr, _set) \ + CLKDEV_MODIFY_4(clknode_get_device(_clk), _off, _clr, _set) +#define DEVICE_LOCK(_clk) \ + CLKDEV_DEVICE_LOCK(clknode_get_device(_clk)) +#define DEVICE_UNLOCK(_clk) \ + CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk)) + +/* Change the bits in mask to val. Called with the device lock held. */ +static void +mtk_clk_mux_write(struct clknode *clk, struct mtk_clk_mux_sc *sc, + uint32_t mask, uint32_t val) +{ + if (!sc->setclr) { + MD4(clk, sc->reg, mask, val); + return; + } + if ((mask & ~val) != 0) + WR4(clk, sc->clr_reg, mask & ~val); + if (val != 0) + WR4(clk, sc->set_reg, val); +} + +static void +mtk_clk_mux_update(struct clknode *clk, struct mtk_clk_mux_sc *sc) +{ + if (sc->upd_bit >= 0) + WR4(clk, sc->upd_reg, 1u << sc->upd_bit); +} + +static int +mtk_clk_mux_init(struct clknode *clk, device_t dev) +{ + struct mtk_clk_mux_sc *sc = clknode_get_softc(clk); + uint32_t val; + + DEVICE_LOCK(clk); + RD4(clk, sc->reg, &val); + DEVICE_UNLOCK(clk); + clknode_init_parent_idx(clk, (val >> sc->shift) & sc->mask); + return (0); +} + +static int +mtk_clk_mux_set_mux(struct clknode *clk, int idx) +{ + struct mtk_clk_mux_sc *sc = clknode_get_softc(clk); + + DEVICE_LOCK(clk); + mtk_clk_mux_write(clk, sc, sc->mask << sc->shift, + (uint32_t)idx << sc->shift); + mtk_clk_mux_update(clk, sc); + DEVICE_UNLOCK(clk); + return (0); +} + +static int +mtk_clk_mux_set_gate(struct clknode *clk, bool enable) +{ + struct mtk_clk_mux_sc *sc = clknode_get_softc(clk); + + /* Without a gate bit the mux always runs. */ + if (sc->gate_bit < 0) + return (enable ? 0 : ENXIO); + DEVICE_LOCK(clk); + mtk_clk_mux_write(clk, sc, 1u << sc->gate_bit, + enable ? 0 : 1u << sc->gate_bit); + if (enable) + mtk_clk_mux_update(clk, sc); + DEVICE_UNLOCK(clk); + return (0); +} + +static int +mtk_clk_mux_get_gate(struct clknode *clk, bool *enabled) +{ + struct mtk_clk_mux_sc *sc = clknode_get_softc(clk); + uint32_t val; + + if (sc->gate_bit < 0) { + *enabled = true; + return (0); + } + DEVICE_LOCK(clk); + RD4(clk, sc->reg, &val); + DEVICE_UNLOCK(clk); + *enabled = (val & (1u << sc->gate_bit)) == 0; + return (0); +} + +static clknode_method_t mtk_clk_mux_methods[] = { + CLKNODEMETHOD(clknode_init, mtk_clk_mux_init), + CLKNODEMETHOD(clknode_set_mux, mtk_clk_mux_set_mux), + CLKNODEMETHOD(clknode_set_gate, mtk_clk_mux_set_gate), + CLKNODEMETHOD(clknode_get_gate, mtk_clk_mux_get_gate), + CLKNODEMETHOD_END +}; + +DEFINE_CLASS_1(mtk_clk_mux_clknode, mtk_clk_mux_clknode_class, + mtk_clk_mux_methods, sizeof(struct mtk_clk_mux_sc), clknode_class); + +int +mtk_clk_mux_register(struct clkdom *clkdom, const struct mtk_clk_mux_def *def) +{ + struct clknode *clk; + struct mtk_clk_mux_sc *sc; + + clk = clknode_create(clkdom, &mtk_clk_mux_clknode_class, &def->clkdef); + if (clk == NULL) + return (ENXIO); + + sc = clknode_get_softc(clk); + sc->reg = def->reg; + sc->set_reg = def->set_reg; + sc->clr_reg = def->clr_reg; + sc->shift = def->shift; + sc->mask = (1u << def->width) - 1; + sc->gate_bit = def->gate_bit; + sc->upd_reg = def->upd_reg; + sc->upd_bit = def->upd_bit; + sc->setclr = (def->flags & MTK_MUX_SETCLR) != 0; + + if (clknode_register(clkdom, clk) == NULL) + return (ENXIO); + return (0); +} diff --git a/sys/dev/clk/mediatek/mtk_clk_pll.c b/sys/dev/clk/mediatek/mtk_clk_pll.c new file mode 100644 index 000000000000..da40ba519800 --- /dev/null +++ b/sys/dev/clk/mediatek/mtk_clk_pll.c @@ -0,0 +1,214 @@ +/* + * Copyright (c) 2026 Hayzam Sherif + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +/* + * MediaTek PLL clock node, with the MT7986 register layout. CON0 holds the + * enable bit, the post divider (bits 6:4, a power of two up to 16) and an + * optional reset bar bit. CON1, the register after it, is the 32-bit + * frequency control word (PCW) with 7 integer bits. + * + * rate = parent * PCW / 2^25 / 2^postdiv + * + * A new PCW only takes effect when the PLL is enabled, so a running PLL is + * stopped while its rate is changed. CON1 bit 31, a change strobe on older + * SoCs, is part of the PCW here. + * + * The PLLs have no lock bit, so a PLL that was never programmed cannot be + * told from a stopped one. Firmware starts the PLLs and the node has no + * gate: a PLL that is off reports 0 Hz and its rate cannot be set. + */ + +#include +#include +#include + +#include +#include + +#include "clkdev_if.h" + +#define PLL_CON0_EN 0x00000001 +#define PLL_POSTDIV_SHIFT 4 +#define PLL_POSTDIV_MASK 0x7 +#define PLL_POSTDIV_MAX 4 /* divide by 16 */ +#define PLL_CON1 0x4 /* PCW, from CON0 */ +#define PLL_PCW_FBITS 25 /* fractional bits of the PCW */ + +/* The firmware runs the VCOs between 1520 and 2880 MHz. */ +#define PLL_VCO_MIN 1500000000ULL +#define PLL_VCO_MAX 3000000000ULL + +#define PLL_LOCK_DELAY 20 /* us, after enable */ + +struct mtk_clk_pll_sc { + uint32_t con0; + uint32_t rst_bar; +}; + +#define RD4(_clk, _off, _val) \ + CLKDEV_READ_4(clknode_get_device(_clk), _off, _val) +#define WR4(_clk, _off, _val) \ + CLKDEV_WRITE_4(clknode_get_device(_clk), _off, _val) +#define MD4(_clk, _off, _clr, _set) \ + CLKDEV_MODIFY_4(clknode_get_device(_clk), _off, _clr, _set) +#define DEVICE_LOCK(_clk) \ + CLKDEV_DEVICE_LOCK(clknode_get_device(_clk)) +#define DEVICE_UNLOCK(_clk) \ + CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk)) + +static int +mtk_clk_pll_init(struct clknode *clk, device_t dev) +{ + clknode_init_parent_idx(clk, 0); + return (0); +} + +/* Rate for a PCW and post divider, rounded to the nearest Hz. */ +static uint64_t +mtk_clk_pll_rate(uint64_t fin, uint32_t pcw, uint32_t postdiv) +{ + uint32_t shift = PLL_PCW_FBITS + postdiv; + + return ((fin * pcw + (1ULL << (shift - 1))) >> shift); +} + +static int +mtk_clk_pll_recalc_freq(struct clknode *clk, uint64_t *freq) +{ + struct mtk_clk_pll_sc *sc = clknode_get_softc(clk); + uint32_t con0, pcw; + + DEVICE_LOCK(clk); + RD4(clk, sc->con0, &con0); + RD4(clk, sc->con0 + PLL_CON1, &pcw); + DEVICE_UNLOCK(clk); + + if ((con0 & PLL_CON0_EN) == 0) + *freq = 0; + else + *freq = mtk_clk_pll_rate(*freq, pcw, + (con0 >> PLL_POSTDIV_SHIFT) & PLL_POSTDIV_MASK); + return (0); +} + +/* Enable the PLL and release its reset once it has locked. */ +static void +mtk_clk_pll_start(struct clknode *clk, struct mtk_clk_pll_sc *sc) +{ + MD4(clk, sc->con0, 0, PLL_CON0_EN); + DELAY(PLL_LOCK_DELAY); + if (sc->rst_bar != 0) + MD4(clk, sc->con0, 0, sc->rst_bar); +} + +static void +mtk_clk_pll_stop(struct clknode *clk, struct mtk_clk_pll_sc *sc) +{ + if (sc->rst_bar != 0) + MD4(clk, sc->con0, sc->rst_bar, 0); + MD4(clk, sc->con0, PLL_CON0_EN, 0); +} + +/* Set a new PCW and post divider, stopping the running PLL for it. */ +static void +mtk_clk_pll_program(struct clknode *clk, uint32_t pcw, uint32_t postdiv) +{ + struct mtk_clk_pll_sc *sc = clknode_get_softc(clk); + + DEVICE_LOCK(clk); + mtk_clk_pll_stop(clk, sc); + WR4(clk, sc->con0 + PLL_CON1, pcw); + MD4(clk, sc->con0, PLL_POSTDIV_MASK << PLL_POSTDIV_SHIFT, + postdiv << PLL_POSTDIV_SHIFT); + mtk_clk_pll_start(clk, sc); + DEVICE_UNLOCK(clk); +} + +/* + * Use the smallest post divider that keeps the VCO in its range, as the + * firmware does. The PCW has 25 fractional bits, so the rate is then set + * to within about 1 Hz, and the rounding only chooses between two + * neighbouring PCW values. + */ +static int +mtk_clk_pll_set_freq(struct clknode *clk, uint64_t fin, uint64_t *fout, + int flags, int *stop) +{ + uint64_t cur, freq, vco; + uint32_t pcw, postdiv; + int round; + + *stop = 1; + round = CLK_SET_ROUND(flags); + + /* A new rate would only take effect when the PLL is started. */ + cur = fin; + mtk_clk_pll_recalc_freq(clk, &cur); + if (cur == 0) + return (ENXIO); + + /* A request beyond the range can only be rounded to its limit. */ + vco = *fout; + if (vco > PLL_VCO_MAX && (round & CLK_SET_ROUND_DOWN) != 0) + vco = PLL_VCO_MAX; + else if (vco < (PLL_VCO_MIN >> PLL_POSTDIV_MAX) && + (round & CLK_SET_ROUND_UP) != 0) + vco = PLL_VCO_MIN >> PLL_POSTDIV_MAX; + for (postdiv = 0; postdiv < PLL_POSTDIV_MAX && vco < PLL_VCO_MIN; + postdiv++) + vco <<= 1; + if (vco < PLL_VCO_MIN || vco > PLL_VCO_MAX) + return (ERANGE); + + /* Cannot overflow: the VCO rate is below 2^32. */ + pcw = ((vco << PLL_PCW_FBITS) + fin / 2) / fin; + freq = mtk_clk_pll_rate(fin, pcw, postdiv); + if (round == CLK_SET_ROUND_UP && freq < *fout) + freq = mtk_clk_pll_rate(fin, ++pcw, postdiv); + else if (round == CLK_SET_ROUND_DOWN && freq > *fout) + freq = mtk_clk_pll_rate(fin, --pcw, postdiv); + else if (round == CLK_SET_ROUND_EXACT && freq != *fout) + return (ERANGE); + + if (cur != freq) { + /* The PLL would have to stop. */ + if ((clknode_get_flags(clk) & CLK_NODE_CANNOT_STOP) != 0) + return (EBUSY); + if ((flags & CLK_SET_DRYRUN) == 0) + mtk_clk_pll_program(clk, pcw, postdiv); + } + *fout = freq; + return (0); +} + +static clknode_method_t mtk_clk_pll_methods[] = { + CLKNODEMETHOD(clknode_init, mtk_clk_pll_init), + CLKNODEMETHOD(clknode_recalc_freq, mtk_clk_pll_recalc_freq), + CLKNODEMETHOD(clknode_set_freq, mtk_clk_pll_set_freq), + CLKNODEMETHOD_END +}; + +DEFINE_CLASS_1(mtk_clk_pll_clknode, mtk_clk_pll_clknode_class, + mtk_clk_pll_methods, sizeof(struct mtk_clk_pll_sc), clknode_class); + +int +mtk_clk_pll_register(struct clkdom *clkdom, const struct mtk_clk_pll_def *def) +{ + struct clknode *clk; + struct mtk_clk_pll_sc *sc; + + clk = clknode_create(clkdom, &mtk_clk_pll_clknode_class, &def->clkdef); + if (clk == NULL) + return (ENXIO); + + sc = clknode_get_softc(clk); + sc->con0 = def->con0; + sc->rst_bar = def->rst_bar; + + if (clknode_register(clkdom, clk) == NULL) + return (ENXIO); + return (0); +}