Path: blob/main/sys/arm64/freescale/imx/clk/imx_clk_frac_pll.c
39566 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2020 Oleksandr Tymoshenko <[email protected]>4*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 ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES16* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,18* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,19* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;20* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED21* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,22* 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/param.h>28#include <sys/systm.h>29#include <sys/bus.h>3031#include <dev/clk/clk.h>3233#include <arm64/freescale/imx/clk/imx_clk_frac_pll.h>3435#include "clkdev_if.h"3637struct imx_clk_frac_pll_sc {38uint32_t offset;39};4041#define WRITE4(_clk, off, val) \42CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)43#define READ4(_clk, off, val) \44CLKDEV_READ_4(clknode_get_device(_clk), off, val)45#define DEVICE_LOCK(_clk) \46CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))47#define DEVICE_UNLOCK(_clk) \48CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))4950#define CFG0 051#define CFG0_PLL_LOCK (1 << 31)52#define CFG0_PD (1 << 19)53#define CFG0_BYPASS (1 << 14)54#define CFG0_NEWDIV_VAL (1 << 12)55#define CFG0_NEWDIV_ACK (1 << 11)56#define CFG0_OUTPUT_DIV_MASK (0x1f << 0)57#define CFG0_OUTPUT_DIV_SHIFT 058#define CFG1 459#define CFG1_FRAC_DIV_MASK (0xffffff << 7)60#define CFG1_FRAC_DIV_SHIFT 761#define CFG1_INT_DIV_MASK (0x7f << 0)62#define CFG1_INT_DIV_SHIFT 06364#if 065#define dprintf(format, arg...) \66printf("%s:(%s)" format, __func__, clknode_get_name(clk), arg)67#else68#define dprintf(format, arg...)69#endif7071static int72imx_clk_frac_pll_init(struct clknode *clk, device_t dev)73{7475clknode_init_parent_idx(clk, 0);76return (0);77}7879static int80imx_clk_frac_pll_set_gate(struct clknode *clk, bool enable)81{82struct imx_clk_frac_pll_sc *sc;83uint32_t cfg0;84int timeout;8586sc = clknode_get_softc(clk);8788DEVICE_LOCK(clk);89READ4(clk, sc->offset + CFG0, &cfg0);90if (enable)91cfg0 &= ~(CFG0_PD);92else93cfg0 |= CFG0_PD;94WRITE4(clk, sc->offset + CFG0, cfg0);9596/* Wait for PLL to lock */97if (enable && ((cfg0 & CFG0_BYPASS) == 0)) {98for (timeout = 1000; timeout; timeout--) {99READ4(clk, sc->offset + CFG0, &cfg0);100if (cfg0 & CFG0_PLL_LOCK)101break;102DELAY(1);103}104}105106DEVICE_UNLOCK(clk);107108return (0);109}110111static int112imx_clk_frac_pll_recalc(struct clknode *clk, uint64_t *freq)113{114struct imx_clk_frac_pll_sc *sc;115uint32_t cfg0, cfg1;116uint64_t div, divfi, divff, divf_val;117118sc = clknode_get_softc(clk);119120DEVICE_LOCK(clk);121READ4(clk, sc->offset + CFG0, &cfg0);122READ4(clk, sc->offset + CFG1, &cfg1);123DEVICE_UNLOCK(clk);124125div = (cfg0 & CFG0_OUTPUT_DIV_MASK) >> CFG0_OUTPUT_DIV_SHIFT;126div = (div + 1) * 2;127divff = (cfg1 & CFG1_FRAC_DIV_MASK) >> CFG1_FRAC_DIV_SHIFT;128divfi = (cfg1 & CFG1_INT_DIV_MASK) >> CFG1_INT_DIV_SHIFT;129130/* PLL is bypassed */131if (cfg0 & CFG0_BYPASS)132return (0);133134divf_val = 1 + divfi + (divff/0x1000000);135*freq = *freq * 8 * divf_val / div;136137return (0);138}139140static clknode_method_t imx_clk_frac_pll_clknode_methods[] = {141/* Device interface */142CLKNODEMETHOD(clknode_init, imx_clk_frac_pll_init),143CLKNODEMETHOD(clknode_set_gate, imx_clk_frac_pll_set_gate),144CLKNODEMETHOD(clknode_recalc_freq, imx_clk_frac_pll_recalc),145CLKNODEMETHOD_END146};147148DEFINE_CLASS_1(imx_clk_frac_pll_clknode, imx_clk_frac_pll_clknode_class,149imx_clk_frac_pll_clknode_methods, sizeof(struct imx_clk_frac_pll_sc),150clknode_class);151152int153imx_clk_frac_pll_register(struct clkdom *clkdom,154struct imx_clk_frac_pll_def *clkdef)155{156struct clknode *clk;157struct imx_clk_frac_pll_sc *sc;158159clk = clknode_create(clkdom, &imx_clk_frac_pll_clknode_class,160&clkdef->clkdef);161if (clk == NULL)162return (1);163164sc = clknode_get_softc(clk);165166sc->offset = clkdef->offset;167168clknode_register(clkdom, clk);169170return (0);171}172173174