Path: blob/main/sys/arm64/qoriq/clk/qoriq_clk_pll.c
39536 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2020 Alstom Group.4* Copyright (c) 2020 Semihalf.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>31#include <sys/malloc.h>3233#include <dev/clk/clk.h>34#include <dev/clk/clk_fixed.h>3536#include <arm64/qoriq/clk/qoriq_clkgen.h>3738#include "clkdev_if.h"3940struct qoriq_clk_pll_softc {41bus_addr_t offset;4243uint32_t mask;44uint32_t shift;4546uint32_t flags;47};4849#define WR4(_clk, offset, val) \50CLKDEV_WRITE_4(clknode_get_device(_clk), offset, val)51#define RD4(_clk, offset, val) \52CLKDEV_READ_4(clknode_get_device(_clk), offset, val)53#define DEVICE_LOCK(_clk) \54CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))55#define DEVICE_UNLOCK(_clk) \56CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))5758#define QORIQ_PLL_KILL_BIT (1 << 31)5960static int61qoriq_clk_pll_init(struct clknode *clk, device_t dev)62{6364clknode_init_parent_idx(clk, 0);6566return (0);67}6869static int70qoriq_clk_pll_recalc_freq(struct clknode *clk, uint64_t *freq)71{72struct qoriq_clk_pll_softc *sc;73uint32_t mul;7475sc = clknode_get_softc(clk);7677RD4(clk, sc->offset, &mul);7879if (sc->flags & QORIQ_CLK_PLL_HAS_KILL_BIT && mul & QORIQ_PLL_KILL_BIT)80return (0);8182mul &= sc->mask;83mul >>= sc->shift;8485*freq = *freq * mul;8687return (0);88}8990static clknode_method_t qoriq_clk_pll_clknode_methods[] = {91CLKNODEMETHOD(clknode_init, qoriq_clk_pll_init),92CLKNODEMETHOD(clknode_recalc_freq, qoriq_clk_pll_recalc_freq),9394CLKNODEMETHOD_END95};9697DEFINE_CLASS_1(qoriq_clk_pll_clknode, qoriq_clk_pll_clknode_class,98qoriq_clk_pll_clknode_methods, sizeof(struct qoriq_clk_pll_softc),99clknode_class);100101int102qoriq_clk_pll_register(struct clkdom *clkdom,103const struct qoriq_clk_pll_def *clkdef)104{105char namebuf[QORIQ_CLK_NAME_MAX_LEN];106struct qoriq_clk_pll_softc *sc;107struct clk_fixed_def def;108const char *parent_name;109struct clknode *clk;110int error;111int i;112113clk = clknode_create(clkdom, &qoriq_clk_pll_clknode_class,114&clkdef->clkdef);115if (clk == NULL)116return (1);117118sc = clknode_get_softc(clk);119sc->mask = clkdef->mask;120sc->shift = clkdef->shift;121sc->flags = clkdef->flags;122sc->offset = clkdef->offset;123124clknode_register(clkdom, clk);125126parent_name = clkdef->clkdef.name;127128def.clkdef.parent_names = &parent_name;129def.clkdef.parent_cnt = 1;130def.clkdef.name = namebuf;131def.mult = 1;132def.freq = 0;133134i = 0;135while (clkdef->dividers[i] != 0) {136def.div = clkdef->dividers[i];137def.clkdef.id = clkdef->clkdef.id + i;138snprintf(namebuf, QORIQ_CLK_NAME_MAX_LEN, "%s_div%d",139parent_name, clkdef->dividers[i]);140141error = clknode_fixed_register(clkdom, &def);142if (error != 0)143return (error);144145i++;146}147148return (0);149}150151152