xref: /freebsd/sys/arm/mv/clk/periph_clk_mux_gate.c (revision be82b3a0bf72ed3b5f01ac9fcd8dcd3802e3c742)
1f21c469dSHubert Mazur /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3f21c469dSHubert Mazur  *
4f21c469dSHubert Mazur  * Copyright (c) 2021 Semihalf.
5f21c469dSHubert Mazur  *
6f21c469dSHubert Mazur  * Redistribution and use in source and binary forms, with or without
7f21c469dSHubert Mazur  * modification, are permitted provided that the following conditions
8f21c469dSHubert Mazur  * are met:
9f21c469dSHubert Mazur  * 1. Redistributions of source code must retain the above copyright
10f21c469dSHubert Mazur  *    notice, this list of conditions and the following disclaimer.
11f21c469dSHubert Mazur  * 2. Redistributions in binary form must reproduce the above copyright
12f21c469dSHubert Mazur  *    notice, this list of conditions and the following disclaimer in the
13f21c469dSHubert Mazur  *    documentation and/or other materials provided with the distribution.
14f21c469dSHubert Mazur  *
15f21c469dSHubert Mazur  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16f21c469dSHubert Mazur  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17f21c469dSHubert Mazur  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18f21c469dSHubert Mazur  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19f21c469dSHubert Mazur  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20f21c469dSHubert Mazur  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21f21c469dSHubert Mazur  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22f21c469dSHubert Mazur  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23f21c469dSHubert Mazur  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24f21c469dSHubert Mazur  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25f21c469dSHubert Mazur  * SUCH DAMAGE.
26f21c469dSHubert Mazur  */
27f21c469dSHubert Mazur 
28f21c469dSHubert Mazur #include <sys/param.h>
29f21c469dSHubert Mazur #include <sys/bus.h>
30f21c469dSHubert Mazur #include <sys/kernel.h>
31f21c469dSHubert Mazur #include <sys/module.h>
32f21c469dSHubert Mazur #include <sys/mutex.h>
33f21c469dSHubert Mazur #include <sys/rman.h>
34f21c469dSHubert Mazur #include <machine/bus.h>
35f21c469dSHubert Mazur 
36f21c469dSHubert Mazur #include <dev/fdt/simplebus.h>
37f21c469dSHubert Mazur 
38*be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
39*be82b3a0SEmmanuel Vadot #include <dev/clk/clk_div.h>
40*be82b3a0SEmmanuel Vadot #include <dev/clk/clk_fixed.h>
41*be82b3a0SEmmanuel Vadot #include <dev/clk/clk_gate.h>
42*be82b3a0SEmmanuel Vadot #include <dev/clk/clk_mux.h>
43f21c469dSHubert Mazur 
44f21c469dSHubert Mazur #include <dev/ofw/ofw_bus.h>
45f21c469dSHubert Mazur #include <dev/ofw/ofw_bus_subr.h>
46f21c469dSHubert Mazur 
47f21c469dSHubert Mazur #include "clkdev_if.h"
48f21c469dSHubert Mazur #include "periph.h"
49f21c469dSHubert Mazur 
50f21c469dSHubert Mazur #define PARENT_CNT		2
51f21c469dSHubert Mazur #define TBG_A_S_OFW_INDEX	0
52f21c469dSHubert Mazur 
53f21c469dSHubert Mazur /*
54f21c469dSHubert Mazur  * Register chain: fixed (freq/2) -> mux (choose fixed or parent frequency) ->
55f21c469dSHubert Mazur  * gate (enable or disable clock).
56f21c469dSHubert Mazur  */
57f21c469dSHubert Mazur 
58f21c469dSHubert Mazur int
a37x0_periph_register_mux_gate(struct clkdom * clkdom,struct a37x0_periph_clknode_def * device_def)59f21c469dSHubert Mazur a37x0_periph_register_mux_gate(struct clkdom *clkdom,
60f21c469dSHubert Mazur     struct a37x0_periph_clknode_def *device_def)
61f21c469dSHubert Mazur {
62f21c469dSHubert Mazur 	const char *parent_names[PARENT_CNT];
63f21c469dSHubert Mazur 	struct clk_fixed_def fixed;
64f21c469dSHubert Mazur 	struct clk_gate_def *gate;
65f21c469dSHubert Mazur 	struct clk_mux_def *mux;
66f21c469dSHubert Mazur 	int error, dev_id;
67f21c469dSHubert Mazur 
68f21c469dSHubert Mazur 	dev_id = device_def->common_def.device_id;
69f21c469dSHubert Mazur 	mux = &device_def->clk_def.mux_gate.mux;
70f21c469dSHubert Mazur 	gate = &device_def->clk_def.mux_gate.gate;
71f21c469dSHubert Mazur 	fixed = device_def->clk_def.fixed.fixed;
72f21c469dSHubert Mazur 
73f21c469dSHubert Mazur 	fixed.clkdef.id = A37x0_INTERNAL_CLK_ID(dev_id, FIXED1_POS);
74f21c469dSHubert Mazur 	fixed.clkdef.parent_names = &device_def->common_def.pname;
75f21c469dSHubert Mazur 	fixed.clkdef.parent_cnt = 1;
76f21c469dSHubert Mazur 	fixed.clkdef.flags = 0x0;
77f21c469dSHubert Mazur 	fixed.mult = 1;
78f21c469dSHubert Mazur 	fixed.div = 2;
79f21c469dSHubert Mazur 	fixed.freq = 0;
80f21c469dSHubert Mazur 
81f21c469dSHubert Mazur 	error = clknode_fixed_register(clkdom, &fixed);
82f21c469dSHubert Mazur 	if (error)
83f21c469dSHubert Mazur 		goto fail;
84f21c469dSHubert Mazur 
85f21c469dSHubert Mazur 	parent_names[0] = device_def->common_def.pname;
86f21c469dSHubert Mazur 	parent_names[1] = fixed.clkdef.name;
87f21c469dSHubert Mazur 
88f21c469dSHubert Mazur 	a37x0_periph_set_props(&mux->clkdef, parent_names, PARENT_CNT);
89f21c469dSHubert Mazur 	error = a37x0_periph_create_mux(clkdom, mux,
90f21c469dSHubert Mazur 	    A37x0_INTERNAL_CLK_ID(dev_id, MUX_POS));
91f21c469dSHubert Mazur 	if (error)
92f21c469dSHubert Mazur 		goto fail;
93f21c469dSHubert Mazur 
94f21c469dSHubert Mazur 	a37x0_periph_set_props(&gate->clkdef, &mux->clkdef.name, 1);
95f21c469dSHubert Mazur 	error = a37x0_periph_create_gate(clkdom, gate,
96f21c469dSHubert Mazur 	    dev_id);
97f21c469dSHubert Mazur 	if (error)
98f21c469dSHubert Mazur 		goto fail;
99f21c469dSHubert Mazur 
100f21c469dSHubert Mazur fail:
101f21c469dSHubert Mazur 
102f21c469dSHubert Mazur 	return (error);
103f21c469dSHubert Mazur }
104f21c469dSHubert Mazur 
105f21c469dSHubert Mazur /*
106f21c469dSHubert Mazur  * Register chain: fixed1 (freq/2) -> mux (fixed1 or TBG-A-S frequency) ->
107f21c469dSHubert Mazur  * gate -> fixed2 (freq/2).
108f21c469dSHubert Mazur  */
109f21c469dSHubert Mazur 
110f21c469dSHubert Mazur int
a37x0_periph_register_mux_gate_fixed(struct clkdom * clkdom,struct a37x0_periph_clknode_def * device_def)111f21c469dSHubert Mazur a37x0_periph_register_mux_gate_fixed(struct clkdom * clkdom,
112f21c469dSHubert Mazur     struct a37x0_periph_clknode_def *device_def)
113f21c469dSHubert Mazur {
114f21c469dSHubert Mazur 	struct clk_fixed_def *fixed1, *fixed2;
115f21c469dSHubert Mazur 	const char *parent_names[PARENT_CNT];
116f21c469dSHubert Mazur 	struct clk_gate_def *gate;
117f21c469dSHubert Mazur 	struct clk_mux_def *mux;
118f21c469dSHubert Mazur 	int error, dev_id;
119f21c469dSHubert Mazur 
120f21c469dSHubert Mazur 	dev_id = device_def->common_def.device_id;
121f21c469dSHubert Mazur 	mux = &device_def->clk_def.mux_gate_fixed.mux;
122f21c469dSHubert Mazur 	gate = &device_def->clk_def.mux_gate_fixed.gate;
123f21c469dSHubert Mazur 	fixed1 = &device_def->clk_def.mux_gate_fixed.fixed1;
124f21c469dSHubert Mazur 	fixed2 = &device_def->clk_def.mux_gate_fixed.fixed2;
125f21c469dSHubert Mazur 
126f21c469dSHubert Mazur 	fixed1->clkdef.parent_names = &device_def->common_def.pname;
127f21c469dSHubert Mazur 	fixed1->clkdef.id = A37x0_INTERNAL_CLK_ID(dev_id, FIXED1_POS);
128f21c469dSHubert Mazur 	fixed1->clkdef.flags = 0x0;
129f21c469dSHubert Mazur 	fixed1->mult = 1;
130f21c469dSHubert Mazur 	fixed1->div = 2;
131f21c469dSHubert Mazur 	fixed1->freq = 0;
132f21c469dSHubert Mazur 
133f21c469dSHubert Mazur 	error = clknode_fixed_register(clkdom, fixed1);
134f21c469dSHubert Mazur 	if (error)
135f21c469dSHubert Mazur 		goto fail;
136f21c469dSHubert Mazur 
137f21c469dSHubert Mazur 	parent_names[0] = device_def->common_def.tbgs[TBG_A_S_OFW_INDEX];
138f21c469dSHubert Mazur 	parent_names[1] = fixed1->clkdef.name;
139f21c469dSHubert Mazur 
140f21c469dSHubert Mazur 	a37x0_periph_set_props(&mux->clkdef, parent_names, PARENT_CNT);
141f21c469dSHubert Mazur 	error = a37x0_periph_create_mux(clkdom, mux,
142f21c469dSHubert Mazur 	    A37x0_INTERNAL_CLK_ID(dev_id, MUX_POS));
143f21c469dSHubert Mazur 	if (error)
144f21c469dSHubert Mazur 		goto fail;
145f21c469dSHubert Mazur 
146f21c469dSHubert Mazur 	a37x0_periph_set_props(&gate->clkdef, &mux->clkdef.name, 1);
147f21c469dSHubert Mazur 	error = a37x0_periph_create_gate(clkdom, gate,
148f21c469dSHubert Mazur 	    A37x0_INTERNAL_CLK_ID(dev_id, GATE_POS));
149f21c469dSHubert Mazur 	if (error)
150f21c469dSHubert Mazur 		goto fail;
151f21c469dSHubert Mazur 
152f21c469dSHubert Mazur 	fixed2->clkdef.parent_names = &gate->clkdef.name;
153f21c469dSHubert Mazur 	fixed2->clkdef.parent_cnt = 1;
154f21c469dSHubert Mazur 	fixed2->clkdef.id = dev_id;
155f21c469dSHubert Mazur 
156f21c469dSHubert Mazur 	error = clknode_fixed_register(clkdom, fixed2);
157f21c469dSHubert Mazur 	if (error)
158f21c469dSHubert Mazur 		goto fail;
159f21c469dSHubert Mazur 
160f21c469dSHubert Mazur fail:
161f21c469dSHubert Mazur 
162f21c469dSHubert Mazur 	return (error);
163f21c469dSHubert Mazur }
164