xref: /freebsd/sys/dev/clk/xilinx/zynqmp_clk_pll.c (revision be82b3a0bf72ed3b5f01ac9fcd8dcd3802e3c742)
14e579ad0SEmmanuel Vadot /*-
24e579ad0SEmmanuel Vadot  * SPDX-License-Identifier: BSD-2-Clause
34e579ad0SEmmanuel Vadot  *
44e579ad0SEmmanuel Vadot  * Copyright (c) 2023 Beckhoff Automation GmbH & Co. KG
54e579ad0SEmmanuel Vadot  *
64e579ad0SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
74e579ad0SEmmanuel Vadot  * modification, are permitted provided that the following conditions
84e579ad0SEmmanuel Vadot  * are met:
94e579ad0SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
104e579ad0SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
114e579ad0SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
124e579ad0SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
134e579ad0SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
144e579ad0SEmmanuel Vadot  *
154e579ad0SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
164e579ad0SEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
174e579ad0SEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
184e579ad0SEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
194e579ad0SEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
204e579ad0SEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
214e579ad0SEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
224e579ad0SEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
234e579ad0SEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
244e579ad0SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
254e579ad0SEmmanuel Vadot  * SUCH DAMAGE.
264e579ad0SEmmanuel Vadot  */
274e579ad0SEmmanuel Vadot 
284e579ad0SEmmanuel Vadot #include <sys/cdefs.h>
294e579ad0SEmmanuel Vadot 
304e579ad0SEmmanuel Vadot #include <sys/param.h>
314e579ad0SEmmanuel Vadot #include <sys/systm.h>
324e579ad0SEmmanuel Vadot #include <sys/bus.h>
334e579ad0SEmmanuel Vadot 
34*be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
354e579ad0SEmmanuel Vadot 
364e579ad0SEmmanuel Vadot #include <dev/clk/xilinx/zynqmp_clk_pll.h>
374e579ad0SEmmanuel Vadot 
384e579ad0SEmmanuel Vadot #include "clkdev_if.h"
394e579ad0SEmmanuel Vadot #include "zynqmp_firmware_if.h"
404e579ad0SEmmanuel Vadot 
414e579ad0SEmmanuel Vadot struct zynqmp_clk_pll_softc {
424e579ad0SEmmanuel Vadot 	device_t	firmware;
434e579ad0SEmmanuel Vadot 	uint32_t	id;
444e579ad0SEmmanuel Vadot };
454e579ad0SEmmanuel Vadot 
464e579ad0SEmmanuel Vadot enum pll_mode {
474e579ad0SEmmanuel Vadot 	PLL_MODE_INT = 0,
484e579ad0SEmmanuel Vadot 	PLL_MODE_FRAC,
494e579ad0SEmmanuel Vadot 	PLL_MODE_ERROR,
504e579ad0SEmmanuel Vadot };
514e579ad0SEmmanuel Vadot 
524e579ad0SEmmanuel Vadot static int
zynqmp_clk_pll_init(struct clknode * clk,device_t dev)534e579ad0SEmmanuel Vadot zynqmp_clk_pll_init(struct clknode *clk, device_t dev)
544e579ad0SEmmanuel Vadot {
554e579ad0SEmmanuel Vadot 
564e579ad0SEmmanuel Vadot 	clknode_init_parent_idx(clk, 0);
574e579ad0SEmmanuel Vadot 	return (0);
584e579ad0SEmmanuel Vadot }
594e579ad0SEmmanuel Vadot 
604e579ad0SEmmanuel Vadot static int
zynqmp_clk_pll_recalc(struct clknode * clk,uint64_t * freq)614e579ad0SEmmanuel Vadot zynqmp_clk_pll_recalc(struct clknode *clk, uint64_t *freq)
624e579ad0SEmmanuel Vadot {
634e579ad0SEmmanuel Vadot 	struct zynqmp_clk_pll_softc *sc;
644e579ad0SEmmanuel Vadot 	uint64_t pll_freq, pll_frac;
654e579ad0SEmmanuel Vadot 	uint32_t div, mode, frac;
664e579ad0SEmmanuel Vadot 	int rv;
674e579ad0SEmmanuel Vadot 
684e579ad0SEmmanuel Vadot 	sc = clknode_get_softc(clk);
694e579ad0SEmmanuel Vadot 	rv = ZYNQMP_FIRMWARE_CLOCK_GETDIVIDER(sc->firmware, sc->id, &div);
704e579ad0SEmmanuel Vadot 	if (rv != 0) {
714e579ad0SEmmanuel Vadot 		printf("%s: Error while getting divider for %s\n",
724e579ad0SEmmanuel Vadot 		    __func__,
734e579ad0SEmmanuel Vadot 		    clknode_get_name(clk));
744e579ad0SEmmanuel Vadot 	}
754e579ad0SEmmanuel Vadot 	rv = ZYNQMP_FIRMWARE_PLL_GET_MODE(sc->firmware, sc->id, &mode);
764e579ad0SEmmanuel Vadot 	if (rv != 0) {
774e579ad0SEmmanuel Vadot 		printf("%s: Error while getting mode for %s\n",
784e579ad0SEmmanuel Vadot 		    __func__,
794e579ad0SEmmanuel Vadot 		    clknode_get_name(clk));
804e579ad0SEmmanuel Vadot 	}
814e579ad0SEmmanuel Vadot 	if (mode == PLL_MODE_ERROR)
824e579ad0SEmmanuel Vadot 		return (0);
834e579ad0SEmmanuel Vadot 
844e579ad0SEmmanuel Vadot 	pll_freq = *freq * div;
854e579ad0SEmmanuel Vadot 	if (mode == PLL_MODE_FRAC) {
864e579ad0SEmmanuel Vadot 		ZYNQMP_FIRMWARE_PLL_GET_FRAC_DATA(sc->firmware, sc->id, &frac);
874e579ad0SEmmanuel Vadot 		pll_frac = (*freq * frac) / (1 << 16);
884e579ad0SEmmanuel Vadot 		pll_freq += pll_frac;
894e579ad0SEmmanuel Vadot 	}
904e579ad0SEmmanuel Vadot 
914e579ad0SEmmanuel Vadot 	*freq = pll_freq;
924e579ad0SEmmanuel Vadot 	return (0);
934e579ad0SEmmanuel Vadot }
944e579ad0SEmmanuel Vadot 
954e579ad0SEmmanuel Vadot static int
zynqmp_clk_pll_set_freq(struct clknode * clk,uint64_t fparent,uint64_t * fout,int flags,int * stop)964e579ad0SEmmanuel Vadot zynqmp_clk_pll_set_freq(struct clknode *clk, uint64_t fparent, uint64_t *fout,
974e579ad0SEmmanuel Vadot     int flags, int *stop)
984e579ad0SEmmanuel Vadot {
994e579ad0SEmmanuel Vadot 
1004e579ad0SEmmanuel Vadot 	/* TODO probably at one point */
1014e579ad0SEmmanuel Vadot 	return (ENOTSUP);
1024e579ad0SEmmanuel Vadot }
1034e579ad0SEmmanuel Vadot 
1044e579ad0SEmmanuel Vadot static clknode_method_t zynqmp_clk_pll_clknode_methods[] = {
1054e579ad0SEmmanuel Vadot 	/* Device interface */
1064e579ad0SEmmanuel Vadot 	CLKNODEMETHOD(clknode_init,		zynqmp_clk_pll_init),
1074e579ad0SEmmanuel Vadot 	CLKNODEMETHOD(clknode_recalc_freq,	zynqmp_clk_pll_recalc),
1084e579ad0SEmmanuel Vadot 	CLKNODEMETHOD(clknode_set_freq,		zynqmp_clk_pll_set_freq),
1094e579ad0SEmmanuel Vadot 	CLKNODEMETHOD_END
1104e579ad0SEmmanuel Vadot };
1114e579ad0SEmmanuel Vadot 
1124e579ad0SEmmanuel Vadot DEFINE_CLASS_1(zynqmp_clk_pll_clknode, zynqmp_clk_pll_clknode_class,
1134e579ad0SEmmanuel Vadot     zynqmp_clk_pll_clknode_methods, sizeof(struct zynqmp_clk_pll_softc), clknode_class);
1144e579ad0SEmmanuel Vadot 
1154e579ad0SEmmanuel Vadot int
zynqmp_clk_pll_register(struct clkdom * clkdom,device_t fw,struct clknode_init_def * clkdef)1164e579ad0SEmmanuel Vadot zynqmp_clk_pll_register(struct clkdom *clkdom, device_t fw, struct clknode_init_def *clkdef)
1174e579ad0SEmmanuel Vadot {
1184e579ad0SEmmanuel Vadot 	struct clknode *clk;
1194e579ad0SEmmanuel Vadot 	struct zynqmp_clk_pll_softc *sc;
1204e579ad0SEmmanuel Vadot 	uint32_t fw_clk_id;
1214e579ad0SEmmanuel Vadot 
1224e579ad0SEmmanuel Vadot 	fw_clk_id = clkdef->id - 1;
1234e579ad0SEmmanuel Vadot 	clkdef->id = 0;
1244e579ad0SEmmanuel Vadot 	clk = clknode_create(clkdom, &zynqmp_clk_pll_clknode_class, clkdef);
1254e579ad0SEmmanuel Vadot 	if (clk == NULL)
1264e579ad0SEmmanuel Vadot 		return (1);
1274e579ad0SEmmanuel Vadot 	sc = clknode_get_softc(clk);
1284e579ad0SEmmanuel Vadot 	sc->id = fw_clk_id;
1294e579ad0SEmmanuel Vadot 	sc->firmware = fw;
1304e579ad0SEmmanuel Vadot 	clknode_register(clkdom, clk);
1314e579ad0SEmmanuel Vadot 	return (0);
1324e579ad0SEmmanuel Vadot }
133