ti_gate_clock.c revision 1.1
11.1Sskrll/* $NetBSD: ti_gate_clock.c,v 1.1 2025/12/16 12:20:22 skrll Exp $ */ 21.1Sskrll 31.1Sskrll/*- 41.1Sskrll * Copyright (c) 2025 Rui-Xiang Guo 51.1Sskrll * Copyright (c) 2019 Jared McNeill <jmcneill@invisible.ca> 61.1Sskrll * All rights reserved. 71.1Sskrll * 81.1Sskrll * Redistribution and use in source and binary forms, with or without 91.1Sskrll * modification, are permitted provided that the following conditions 101.1Sskrll * are met: 111.1Sskrll * 1. Redistributions of source code must retain the above copyright 121.1Sskrll * notice, this list of conditions and the following disclaimer. 131.1Sskrll * 2. Redistributions in binary form must reproduce the above copyright 141.1Sskrll * notice, this list of conditions and the following disclaimer in the 151.1Sskrll * documentation and/or other materials provided with the distribution. 161.1Sskrll * 171.1Sskrll * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 181.1Sskrll * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 191.1Sskrll * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 201.1Sskrll * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 211.1Sskrll * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 221.1Sskrll * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 231.1Sskrll * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 241.1Sskrll * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 251.1Sskrll * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261.1Sskrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271.1Sskrll * SUCH DAMAGE. 281.1Sskrll */ 291.1Sskrll 301.1Sskrll#include <sys/cdefs.h> 311.1Sskrll__KERNEL_RCSID(0, "$NetBSD: ti_gate_clock.c,v 1.1 2025/12/16 12:20:22 skrll Exp $"); 321.1Sskrll 331.1Sskrll#include <sys/param.h> 341.1Sskrll#include <sys/systm.h> 351.1Sskrll#include <sys/device.h> 361.1Sskrll#include <sys/kmem.h> 371.1Sskrll#include <sys/bus.h> 381.1Sskrll 391.1Sskrll#include <dev/clk/clk_backend.h> 401.1Sskrll 411.1Sskrll#include <dev/fdt/fdtvar.h> 421.1Sskrll 431.1Sskrllstatic int ti_gate_clock_match(device_t, cfdata_t, void *); 441.1Sskrllstatic void ti_gate_clock_attach(device_t, device_t, void *); 451.1Sskrll 461.1Sskrllstatic struct clk *ti_gate_clock_decode(device_t, int, const void *, size_t); 471.1Sskrll 481.1Sskrllstatic const struct fdtbus_clock_controller_func ti_gate_clock_fdt_funcs = { 491.1Sskrll .decode = ti_gate_clock_decode 501.1Sskrll}; 511.1Sskrll 521.1Sskrllstatic struct clk *ti_gate_clock_get(void *, const char *); 531.1Sskrllstatic void ti_gate_clock_put(void *, struct clk *); 541.1Sskrllstatic int ti_gate_clock_enable(void *, struct clk *); 551.1Sskrllstatic int ti_gate_clock_disable(void *, struct clk *); 561.1Sskrllstatic struct clk *ti_gate_clock_get_parent(void *, struct clk *); 571.1Sskrll 581.1Sskrllstatic const struct clk_funcs ti_gate_clock_clk_funcs = { 591.1Sskrll .get = ti_gate_clock_get, 601.1Sskrll .put = ti_gate_clock_put, 611.1Sskrll .enable = ti_gate_clock_enable, 621.1Sskrll .disable = ti_gate_clock_disable, 631.1Sskrll .get_parent = ti_gate_clock_get_parent, 641.1Sskrll}; 651.1Sskrll 661.1Sskrllstruct ti_gate_clock_softc { 671.1Sskrll device_t sc_dev; 681.1Sskrll int sc_phandle; 691.1Sskrll bus_space_tag_t sc_bst; 701.1Sskrll bus_space_handle_t sc_bsh; 711.1Sskrll 721.1Sskrll struct clk_domain sc_clkdom; 731.1Sskrll struct clk sc_clk; 741.1Sskrll 751.1Sskrll uint32_t sc_shift; 761.1Sskrll}; 771.1Sskrll 781.1Sskrll#define RD4(sc, reg) \ 791.1Sskrll bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg)) 801.1Sskrll#define WR4(sc, reg, val) \ 811.1Sskrll bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) 821.1Sskrll 831.1SskrllCFATTACH_DECL_NEW(ti_gate_clock, sizeof(struct ti_gate_clock_softc), 841.1Sskrll ti_gate_clock_match, ti_gate_clock_attach, NULL, NULL); 851.1Sskrll 861.1Sskrllstatic const struct device_compatible_entry compat_data[] = { 871.1Sskrll { .compat = "ti,composite-no-wait-gate-clock" }, 881.1Sskrll { .compat = "ti,gate-clock" }, 891.1Sskrll DEVICE_COMPAT_EOL 901.1Sskrll}; 911.1Sskrll 921.1Sskrllstatic int 931.1Sskrllti_gate_clock_match(device_t parent, cfdata_t cf, void *aux) 941.1Sskrll{ 951.1Sskrll const struct fdt_attach_args *faa = aux; 961.1Sskrll 971.1Sskrll return of_compatible_match(faa->faa_phandle, compat_data); 981.1Sskrll} 991.1Sskrll 1001.1Sskrllstatic void 1011.1Sskrllti_gate_clock_attach(device_t parent, device_t self, void *aux) 1021.1Sskrll{ 1031.1Sskrll struct ti_gate_clock_softc * const sc = device_private(self); 1041.1Sskrll const struct fdt_attach_args *faa = aux; 1051.1Sskrll const int phandle = faa->faa_phandle; 1061.1Sskrll bus_addr_t addr, base_addr; 1071.1Sskrll u_int shift; 1081.1Sskrll 1091.1Sskrll const int prcm_phandle = OF_parent(OF_parent(phandle)); 1101.1Sskrll if (fdtbus_get_reg(phandle, 0, &addr, NULL) != 0 || 1111.1Sskrll fdtbus_get_reg(prcm_phandle, 0, &base_addr, NULL) != 0) { 1121.1Sskrll aprint_error(": couldn't get registers\n"); 1131.1Sskrll return; 1141.1Sskrll } 1151.1Sskrll 1161.1Sskrll sc->sc_dev = self; 1171.1Sskrll sc->sc_phandle = phandle; 1181.1Sskrll sc->sc_bst = faa->faa_bst; 1191.1Sskrll if (bus_space_map(sc->sc_bst, base_addr + addr, 4, 0, &sc->sc_bsh) != 0) { 1201.1Sskrll aprint_error(": couldn't map registers\n"); 1211.1Sskrll return; 1221.1Sskrll } 1231.1Sskrll 1241.1Sskrll if (of_getprop_uint32(phandle, "ti,bit-shift", &shift) != 0) 1251.1Sskrll shift = 0; 1261.1Sskrll 1271.1Sskrll sc->sc_shift = shift; 1281.1Sskrll 1291.1Sskrll sc->sc_clkdom.name = device_xname(self); 1301.1Sskrll sc->sc_clkdom.funcs = &ti_gate_clock_clk_funcs; 1311.1Sskrll sc->sc_clkdom.priv = sc; 1321.1Sskrll 1331.1Sskrll sc->sc_clk.domain = &sc->sc_clkdom; 1341.1Sskrll sc->sc_clk.name = kmem_asprintf("%s", faa->faa_name); 1351.1Sskrll clk_attach(&sc->sc_clk); 1361.1Sskrll 1371.1Sskrll aprint_naive("\n"); 1381.1Sskrll aprint_normal(": TI gate clock (%s)\n", sc->sc_clk.name); 1391.1Sskrll 1401.1Sskrll fdtbus_register_clock_controller(self, phandle, &ti_gate_clock_fdt_funcs); 1411.1Sskrll} 1421.1Sskrll 1431.1Sskrllstatic struct clk * 1441.1Sskrllti_gate_clock_decode(device_t dev, int cc_phandle, const void *data, 1451.1Sskrll size_t len) 1461.1Sskrll{ 1471.1Sskrll struct ti_gate_clock_softc * const sc = device_private(dev); 1481.1Sskrll 1491.1Sskrll return &sc->sc_clk; 1501.1Sskrll} 1511.1Sskrll 1521.1Sskrllstatic struct clk * 1531.1Sskrllti_gate_clock_get(void *priv, const char *name) 1541.1Sskrll{ 1551.1Sskrll struct ti_gate_clock_softc * const sc = priv; 1561.1Sskrll 1571.1Sskrll return &sc->sc_clk; 1581.1Sskrll} 1591.1Sskrll 1601.1Sskrllstatic void 1611.1Sskrllti_gate_clock_put(void *priv, struct clk *clk) 1621.1Sskrll{ 1631.1Sskrll} 1641.1Sskrll 1651.1Sskrllstatic int 1661.1Sskrllti_gate_clock_enable(void *priv, struct clk *clk) 1671.1Sskrll{ 1681.1Sskrll struct ti_gate_clock_softc * const sc = priv; 1691.1Sskrll struct clk *clk_parent = clk_get_parent(clk); 1701.1Sskrll uint32_t val; 1711.1Sskrll 1721.1Sskrll val = RD4(sc, 0); 1731.1Sskrll val |= __BIT(sc->sc_shift); 1741.1Sskrll WR4(sc, 0, val); 1751.1Sskrll 1761.1Sskrll return clk_enable(clk_parent); 1771.1Sskrll} 1781.1Sskrll 1791.1Sskrllstatic int 1801.1Sskrllti_gate_clock_disable(void *priv, struct clk *clk) 1811.1Sskrll{ 1821.1Sskrll struct ti_gate_clock_softc * const sc = priv; 1831.1Sskrll struct clk *clk_parent = clk_get_parent(clk); 1841.1Sskrll uint32_t val; 1851.1Sskrll 1861.1Sskrll val = RD4(sc, 0); 1871.1Sskrll val &= ~__BIT(sc->sc_shift); 1881.1Sskrll WR4(sc, 0, val); 1891.1Sskrll 1901.1Sskrll return clk_disable(clk_parent); 1911.1Sskrll} 1921.1Sskrll 1931.1Sskrllstatic struct clk * 1941.1Sskrllti_gate_clock_get_parent(void *priv, struct clk *clk) 1951.1Sskrll{ 1961.1Sskrll struct ti_gate_clock_softc * const sc = priv; 1971.1Sskrll 1981.1Sskrll return fdtbus_clock_get_index(sc->sc_phandle, 0); 1991.1Sskrll} 200