xref: /freebsd/sys/dev/clk/xilinx/zynqmp_clk_fixed.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2023 Beckhoff Automation GmbH & Co. KG
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 
34 #include <dev/clk/clk.h>
35 
36 #include <dev/clk/xilinx/zynqmp_clk_fixed.h>
37 
38 #include "clkdev_if.h"
39 #include "zynqmp_firmware_if.h"
40 
41 struct zynqmp_clk_fixed_softc {
42 	device_t	firmware;
43 	uint32_t	id;
44 };
45 
46 static int
47 zynqmp_clk_fixed_init(struct clknode *clk, device_t dev)
48 {
49 
50 	clknode_init_parent_idx(clk, 0);
51 	return (0);
52 }
53 
54 static int
55 zynqmp_clk_fixed_recalc(struct clknode *clk, uint64_t *freq)
56 {
57 	struct zynqmp_clk_fixed_softc *sc;
58 	uint32_t mult, div;
59 	int rv;
60 
61 	sc = clknode_get_softc(clk);
62 	rv = ZYNQMP_FIRMWARE_CLOCK_GET_FIXEDFACTOR(sc->firmware, sc->id, &mult, &div);
63 	if (rv != 0) {
64 		printf("%s: Error while getting fixed factor for %s\n",
65 		    __func__,
66 		    clknode_get_name(clk));
67 		return (EINVAL);
68 	}
69 
70 	*freq = (*freq * mult) / div;
71 	return (0);
72 }
73 
74 static clknode_method_t zynqmp_clk_fixed_clknode_methods[] = {
75 	/* Device interface */
76 	CLKNODEMETHOD(clknode_init,		zynqmp_clk_fixed_init),
77 	CLKNODEMETHOD(clknode_recalc_freq,	zynqmp_clk_fixed_recalc),
78 	CLKNODEMETHOD_END
79 };
80 
81 DEFINE_CLASS_1(zynqmp_clk_fixed_clknode, zynqmp_clk_fixed_clknode_class,
82     zynqmp_clk_fixed_clknode_methods, sizeof(struct zynqmp_clk_fixed_softc), clknode_class);
83 
84 int
85 zynqmp_clk_fixed_register(struct clkdom *clkdom, device_t fw, struct clknode_init_def *clkdef)
86 {
87 	struct clknode *clk;
88 	struct zynqmp_clk_fixed_softc *sc;
89 	uint32_t fw_clk_id;
90 
91 	fw_clk_id = clkdef->id - 1;
92 	clkdef->id = 0;
93 	clk = clknode_create(clkdom, &zynqmp_clk_fixed_clknode_class, clkdef);
94 	if (clk == NULL)
95 		return (1);
96 	sc = clknode_get_softc(clk);
97 	sc->id = fw_clk_id;
98 	sc->firmware = fw;
99 	clknode_register(clkdom, clk);
100 	return (0);
101 }
102