Path: blob/main/sys/arm64/freescale/imx/clk/imx_clk_gate.c
39566 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright 2016 Michal Meloun <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/param.h>29#include <sys/systm.h>30#include <sys/bus.h>3132#include <dev/clk/clk.h>3334#include <arm64/freescale/imx/clk/imx_clk_gate.h>3536#include "clkdev_if.h"3738#define WR4(_clk, off, val) \39CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)40#define RD4(_clk, off, val) \41CLKDEV_READ_4(clknode_get_device(_clk), off, val)42#define MD4(_clk, off, clr, set ) \43CLKDEV_MODIFY_4(clknode_get_device(_clk), off, clr, set)44#define DEVICE_LOCK(_clk) \45CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))46#define DEVICE_UNLOCK(_clk) \47CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))4849static int imx_clk_gate_init(struct clknode *clk, device_t dev);50static int imx_clk_gate_set_gate(struct clknode *clk, bool enable);51struct imx_clk_gate_sc {52uint32_t offset;53uint32_t shift;54uint32_t mask;55int gate_flags;56};5758static clknode_method_t imx_clk_gate_methods[] = {59/* Device interface */60CLKNODEMETHOD(clknode_init, imx_clk_gate_init),61CLKNODEMETHOD(clknode_set_gate, imx_clk_gate_set_gate),62CLKNODEMETHOD_END63};64DEFINE_CLASS_1(imx_clk_gate, imx_clk_gate_class, imx_clk_gate_methods,65sizeof(struct imx_clk_gate_sc), clknode_class);6667static int68imx_clk_gate_init(struct clknode *clk, device_t dev)69{7071clknode_init_parent_idx(clk, 0);72return(0);73}7475static int76imx_clk_gate_set_gate(struct clknode *clk, bool enable)77{78uint32_t reg;79struct imx_clk_gate_sc *sc;80int rv;8182sc = clknode_get_softc(clk);83DEVICE_LOCK(clk);84rv = MD4(clk, sc->offset, sc->mask << sc->shift,85(enable ? sc->mask : 0) << sc->shift);86if (rv != 0) {87DEVICE_UNLOCK(clk);88return (rv);89}90RD4(clk, sc->offset, ®);91DEVICE_UNLOCK(clk);92return(0);93}9495int96imx_clk_gate_register(struct clkdom *clkdom, struct imx_clk_gate_def *clkdef)97{98struct clknode *clk;99struct imx_clk_gate_sc *sc;100101clk = clknode_create(clkdom, &imx_clk_gate_class, &clkdef->clkdef);102if (clk == NULL)103return (1);104105sc = clknode_get_softc(clk);106sc->offset = clkdef->offset;107sc->shift = clkdef->shift;108sc->mask = clkdef->mask;109sc->gate_flags = clkdef->gate_flags;110111clknode_register(clkdom, clk);112return (0);113}114115116