xref: /freebsd/sys/dev/clk/xilinx/zynqmp_clk_gate.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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_gate.h>
37 
38 #include "clkdev_if.h"
39 #include "zynqmp_firmware_if.h"
40 
41 struct zynqmp_clk_gate_softc {
42 	device_t	firmware;
43 	uint32_t	id;
44 };
45 
46 static int
47 zynqmp_clk_gate_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_set_gate(struct clknode *clk, bool enable)
56 {
57 	struct zynqmp_clk_gate_softc *sc;
58 	int rv;
59 
60 	sc = clknode_get_softc(clk);
61 	if (enable)
62 		rv = ZYNQMP_FIRMWARE_CLOCK_ENABLE(sc->firmware, sc->id);
63 	else
64 		rv = ZYNQMP_FIRMWARE_CLOCK_DISABLE(sc->firmware, sc->id);
65 	if (rv != 0) {
66 		printf("%s: Error %sbling %s\n",
67 		    __func__,
68 		    enable == true ? "ena" : "disa",
69 		    clknode_get_name(clk));
70 		return (EINVAL);
71 	}
72 	return (0);
73 }
74 
75 static clknode_method_t zynqmp_clk_gate_clknode_methods[] = {
76 	/* Device interface */
77 	CLKNODEMETHOD(clknode_init,	zynqmp_clk_gate_init),
78 	CLKNODEMETHOD(clknode_set_gate,	zynqmp_clk_set_gate),
79 	CLKNODEMETHOD_END
80 };
81 
82 DEFINE_CLASS_1(zynqmp_clk_gate_clknode, zynqmp_clk_gate_clknode_class,
83     zynqmp_clk_gate_clknode_methods, sizeof(struct zynqmp_clk_gate_softc), clknode_class);
84 
85 int
86 zynqmp_clk_gate_register(struct clkdom *clkdom, device_t fw, struct clknode_init_def *clkdef)
87 {
88 	struct clknode *clk;
89 	struct zynqmp_clk_gate_softc *sc;
90 	uint32_t fw_clk_id;
91 
92 	fw_clk_id = clkdef->id - 1;
93 	clkdef->id = 0;
94 	clk = clknode_create(clkdom, &zynqmp_clk_gate_clknode_class, clkdef);
95 	if (clk == NULL)
96 		return (1);
97 	sc = clknode_get_softc(clk);
98 	sc->id = fw_clk_id;
99 	sc->firmware = fw;
100 	clknode_register(clkdom, clk);
101 	return (0);
102 }
103