xref: /freebsd/sys/arm64/freescale/imx/clk/imx_clk_gate.c (revision 6580f5c38dd5b01aeeaed16b370f1a12423437f0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 
33 #include <dev/clk/clk.h>
34 
35 #include <arm64/freescale/imx/clk/imx_clk_gate.h>
36 
37 #include "clkdev_if.h"
38 
39 #define	WR4(_clk, off, val)						\
40 	CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)
41 #define	RD4(_clk, off, val)						\
42 	CLKDEV_READ_4(clknode_get_device(_clk), off, val)
43 #define	MD4(_clk, off, clr, set )					\
44 	CLKDEV_MODIFY_4(clknode_get_device(_clk), off, clr, set)
45 #define	DEVICE_LOCK(_clk)						\
46 	CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))
47 #define	DEVICE_UNLOCK(_clk)						\
48 	CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))
49 
50 static int imx_clk_gate_init(struct clknode *clk, device_t dev);
51 static int imx_clk_gate_set_gate(struct clknode *clk, bool enable);
52 struct imx_clk_gate_sc {
53 	uint32_t	offset;
54 	uint32_t	shift;
55 	uint32_t	mask;
56 	int		gate_flags;
57 };
58 
59 static clknode_method_t imx_clk_gate_methods[] = {
60 	/* Device interface */
61 	CLKNODEMETHOD(clknode_init,	imx_clk_gate_init),
62 	CLKNODEMETHOD(clknode_set_gate,	imx_clk_gate_set_gate),
63 	CLKNODEMETHOD_END
64 };
65 DEFINE_CLASS_1(imx_clk_gate, imx_clk_gate_class, imx_clk_gate_methods,
66    sizeof(struct imx_clk_gate_sc), clknode_class);
67 
68 static int
69 imx_clk_gate_init(struct clknode *clk, device_t dev)
70 {
71 
72 	clknode_init_parent_idx(clk, 0);
73 	return(0);
74 }
75 
76 static int
77 imx_clk_gate_set_gate(struct clknode *clk, bool enable)
78 {
79 	uint32_t reg;
80 	struct imx_clk_gate_sc *sc;
81 	int rv;
82 
83 	sc = clknode_get_softc(clk);
84 	DEVICE_LOCK(clk);
85 	rv = MD4(clk, sc->offset, sc->mask << sc->shift,
86 	    (enable ? sc->mask : 0) << sc->shift);
87 	if (rv != 0) {
88 		DEVICE_UNLOCK(clk);
89 		return (rv);
90 	}
91 	RD4(clk, sc->offset, &reg);
92 	DEVICE_UNLOCK(clk);
93 	return(0);
94 }
95 
96 int
97 imx_clk_gate_register(struct clkdom *clkdom, struct imx_clk_gate_def *clkdef)
98 {
99 	struct clknode *clk;
100 	struct imx_clk_gate_sc *sc;
101 
102 	clk = clknode_create(clkdom, &imx_clk_gate_class, &clkdef->clkdef);
103 	if (clk == NULL)
104 		return (1);
105 
106 	sc = clknode_get_softc(clk);
107 	sc->offset = clkdef->offset;
108 	sc->shift = clkdef->shift;
109 	sc->mask =  clkdef->mask;
110 	sc->gate_flags = clkdef->gate_flags;
111 
112 	clknode_register(clkdom, clk);
113 	return (0);
114 }
115