1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 
33 #include <sys/param.h>
34 #include <sys/conf.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 
39 #include <machine/bus.h>
40 
41 #include <dev/extres/clk/clk.h>
42 
43 #include <arm64/freescale/imx/clk/imx_clk_mux.h>
44 
45 #include "clkdev_if.h"
46 
47 #define	WR4(_clk, off, val)						\
48 	CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)
49 #define	RD4(_clk, off, val)						\
50 	CLKDEV_READ_4(clknode_get_device(_clk), off, val)
51 #define	MD4(_clk, off, clr, set )					\
52 	CLKDEV_MODIFY_4(clknode_get_device(_clk), off, clr, set)
53 #define	DEVICE_LOCK(_clk)						\
54 	CLKDEV_DEVICE_LOCK(clknode_get_device(_clk))
55 #define	DEVICE_UNLOCK(_clk)						\
56 	CLKDEV_DEVICE_UNLOCK(clknode_get_device(_clk))
57 
58 static int imx_clk_mux_init(struct clknode *clk, device_t dev);
59 static int imx_clk_mux_set_mux(struct clknode *clk, int idx);
60 
61 struct imx_clk_mux_sc {
62 	uint32_t	offset;
63 	uint32_t	shift;
64 	uint32_t	mask;
65 	int		mux_flags;
66 };
67 
68 static clknode_method_t imx_clk_mux_methods[] = {
69 	/* Device interface */
70 	CLKNODEMETHOD(clknode_init, 	imx_clk_mux_init),
71 	CLKNODEMETHOD(clknode_set_mux, 	imx_clk_mux_set_mux),
72 	CLKNODEMETHOD_END
73 };
74 DEFINE_CLASS_1(imx_clk_mux, imx_clk_mux_class, imx_clk_mux_methods,
75    sizeof(struct imx_clk_mux_sc), clknode_class);
76 
77 
78 static int
79 imx_clk_mux_init(struct clknode *clk, device_t dev)
80 {
81 	uint32_t reg;
82 	struct imx_clk_mux_sc *sc;
83 	int rv;
84 
85 	sc = clknode_get_softc(clk);
86 
87 	DEVICE_LOCK(clk);
88 	rv = RD4(clk, sc->offset, &reg);
89 	DEVICE_UNLOCK(clk);
90 	if (rv != 0) {
91 		return (rv);
92 	}
93 	reg = (reg >> sc->shift) & sc->mask;
94 	clknode_init_parent_idx(clk, reg);
95 	return(0);
96 }
97 
98 static int
99 imx_clk_mux_set_mux(struct clknode *clk, int idx)
100 {
101 	uint32_t reg;
102 	struct imx_clk_mux_sc *sc;
103 	int rv;
104 
105 	sc = clknode_get_softc(clk);
106 
107 	DEVICE_LOCK(clk);
108 	rv = MD4(clk, sc->offset, sc->mask << sc->shift,
109 	    ((idx & sc->mask) << sc->shift));
110 	if (rv != 0) {
111 		DEVICE_UNLOCK(clk);
112 		return (rv);
113 	}
114 	RD4(clk, sc->offset, &reg);
115 	DEVICE_UNLOCK(clk);
116 
117 	return(0);
118 }
119 
120 int
121 imx_clk_mux_register(struct clkdom *clkdom, struct imx_clk_mux_def *clkdef)
122 {
123 	struct clknode *clk;
124 	struct imx_clk_mux_sc *sc;
125 
126 	clk = clknode_create(clkdom, &imx_clk_mux_class, &clkdef->clkdef);
127 	if (clk == NULL)
128 		return (1);
129 
130 	sc = clknode_get_softc(clk);
131 	sc->offset = clkdef->offset;
132 	sc->shift = clkdef->shift;
133 	sc->mask =  (1 << clkdef->width) - 1;
134 	sc->mux_flags = clkdef->mux_flags;
135 
136 	clknode_register(clkdom, clk);
137 	return (0);
138 }
139