Path: blob/main/sys/dev/clk/xilinx/zynqmp_clk_fixed.c
39536 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2023 Beckhoff Automation GmbH & Co. KG4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/cdefs.h>2829#include <sys/param.h>30#include <sys/systm.h>31#include <sys/bus.h>3233#include <dev/clk/clk.h>3435#include <dev/clk/xilinx/zynqmp_clk_fixed.h>3637#include "clkdev_if.h"38#include "zynqmp_firmware_if.h"3940struct zynqmp_clk_fixed_softc {41device_t firmware;42uint32_t id;43};4445static int46zynqmp_clk_fixed_init(struct clknode *clk, device_t dev)47{4849clknode_init_parent_idx(clk, 0);50return (0);51}5253static int54zynqmp_clk_fixed_recalc(struct clknode *clk, uint64_t *freq)55{56struct zynqmp_clk_fixed_softc *sc;57uint32_t mult, div;58int rv;5960sc = clknode_get_softc(clk);61rv = ZYNQMP_FIRMWARE_CLOCK_GET_FIXEDFACTOR(sc->firmware, sc->id, &mult, &div);62if (rv != 0) {63printf("%s: Error while getting fixed factor for %s\n",64__func__,65clknode_get_name(clk));66return (EINVAL);67}6869*freq = (*freq * mult) / div;70return (0);71}7273static clknode_method_t zynqmp_clk_fixed_clknode_methods[] = {74/* Device interface */75CLKNODEMETHOD(clknode_init, zynqmp_clk_fixed_init),76CLKNODEMETHOD(clknode_recalc_freq, zynqmp_clk_fixed_recalc),77CLKNODEMETHOD_END78};7980DEFINE_CLASS_1(zynqmp_clk_fixed_clknode, zynqmp_clk_fixed_clknode_class,81zynqmp_clk_fixed_clknode_methods, sizeof(struct zynqmp_clk_fixed_softc), clknode_class);8283int84zynqmp_clk_fixed_register(struct clkdom *clkdom, device_t fw, struct clknode_init_def *clkdef)85{86struct clknode *clk;87struct zynqmp_clk_fixed_softc *sc;88uint32_t fw_clk_id;8990fw_clk_id = clkdef->id - 1;91clkdef->id = 0;92clk = clknode_create(clkdom, &zynqmp_clk_fixed_clknode_class, clkdef);93if (clk == NULL)94return (1);95sc = clknode_get_softc(clk);96sc->id = fw_clk_id;97sc->firmware = fw;98clknode_register(clkdom, clk);99return (0);100}101102103