xref: /netbsd-src/sys/arch/arm/broadcom/bcm2835_cprman.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1 /* $NetBSD: bcm2835_cprman.c,v 1.5 2021/01/27 03:10:19 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2017 Jared D. McNeill <jmcneill@invisible.ca>
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 ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * 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 __KERNEL_RCSID(0, "$NetBSD: bcm2835_cprman.c,v 1.5 2021/01/27 03:10:19 thorpej Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/kmem.h>
36 #include <sys/bus.h>
37 
38 #include <dev/clk/clk_backend.h>
39 
40 #include <dev/fdt/fdtvar.h>
41 
42 #include <arm/broadcom/bcm2835var.h>
43 
44 enum {
45 	CPRMAN_CLOCK_TIMER = 17,
46 	CPRMAN_CLOCK_UART = 19,
47 	CPRMAN_CLOCK_VPU = 20,
48 	CPRMAN_CLOCK_V3D = 21,
49 	CPRMAN_CLOCK_ISP = 22,
50 	CPRMAN_CLOCK_H264 = 23,
51 	CPRMAN_CLOCK_VEC = 24,
52 	CPRMAN_CLOCK_HSM = 25,
53 	CPRMAN_CLOCK_SDRAM = 26,
54 	CPRMAN_CLOCK_TSENS = 27,
55 	CPRMAN_CLOCK_EMMC = 28,
56 	CPRMAN_CLOCK_PERIIMAGE = 29,
57 	CPRMAN_CLOCK_PWM = 30,
58 	CPRMAN_CLOCK_PCM = 31,
59 	CPRMAN_CLOCK_EMMC2 = 51,
60 	CPRMAN_NCLOCK
61 };
62 
63 struct cprman_clk {
64 	struct clk	base;
65 	u_int		id;
66 };
67 
68 struct cprman_softc {
69 	device_t	sc_dev;
70 	int		sc_phandle;
71 
72 	struct clk_domain sc_clkdom;
73 	struct cprman_clk sc_clk[CPRMAN_NCLOCK];
74 };
75 
76 
77 static struct clk *
cprman_decode(device_t dev,int cc_phandle,const void * data,size_t len)78 cprman_decode(device_t dev, int cc_phandle, const void *data, size_t len)
79 {
80 	struct cprman_softc * const sc = device_private(dev);
81 	struct cprman_clk *clk;
82 	const u_int *spec = data;
83 	u_int id;
84 
85 	if (len != 4)
86 		return NULL;
87 
88 	id = be32toh(spec[0]);
89 
90 	if (id >= CPRMAN_NCLOCK)
91 		return NULL;
92 	clk = &sc->sc_clk[id];
93 	if (clk->base.name == NULL)
94 		return NULL;
95 
96 	return &clk->base;
97 }
98 
99 static const struct fdtbus_clock_controller_func cprman_fdt_funcs = {
100 	.decode = cprman_decode
101 };
102 
103 static struct clk *
cprman_get(void * priv,const char * name)104 cprman_get(void *priv, const char *name)
105 {
106 	struct cprman_softc * const sc = priv;
107 	u_int n;
108 
109 	for (n = 0; n < __arraycount(sc->sc_clk); n++) {
110 		if (sc->sc_clk[n].base.name == NULL)
111 			continue;
112 		if (strcmp(sc->sc_clk[n].base.name, name) == 0)
113 			return &sc->sc_clk[n].base;
114 	}
115 
116 	return NULL;
117 }
118 
119 static void
cprman_put(void * priv,struct clk * clk)120 cprman_put(void *priv, struct clk *clk)
121 {
122 }
123 
124 static u_int
cprman_get_rate(void * priv,struct clk * baseclk)125 cprman_get_rate(void *priv, struct clk *baseclk)
126 {
127 	//struct cprman_softc * const sc = priv;
128 	struct cprman_clk *clk = container_of(baseclk, struct cprman_clk, base);
129 
130 	switch (clk->id) {
131 	case CPRMAN_CLOCK_UART:
132 		return bcm283x_clk_get_rate_uart();
133 	case CPRMAN_CLOCK_VPU:
134 		return bcm283x_clk_get_rate_vpu();
135 	case CPRMAN_CLOCK_EMMC:
136 		return bcm283x_clk_get_rate_emmc();
137 	case CPRMAN_CLOCK_EMMC2:
138 		return bcm283x_clk_get_rate_emmc2();
139 	default:
140 		panic("unsupported clock id %d\n", clk->id);
141 	}
142 }
143 
144 static const struct clk_funcs cprman_clk_funcs = {
145 	.get = cprman_get,
146 	.put = cprman_put,
147 	.get_rate = cprman_get_rate,
148 };
149 
150 static void
cprman_add_clock(struct cprman_softc * sc,u_int id,const char * name)151 cprman_add_clock(struct cprman_softc *sc, u_int id, const char *name)
152 {
153 	sc->sc_clk[id].base.domain = &sc->sc_clkdom;
154 	sc->sc_clk[id].base.name = name;
155 	sc->sc_clk[id].id = id;
156 }
157 
158 static const struct device_compatible_entry compat_data[] = {
159 	{ .compat = "brcm,bcm2835-cprman" },
160 	{ .compat = "brcm,bcm2711-cprman" },
161 	DEVICE_COMPAT_EOL
162 };
163 
164 static int
cprman_match(device_t parent,cfdata_t cf,void * aux)165 cprman_match(device_t parent, cfdata_t cf, void *aux)
166 {
167 	const struct fdt_attach_args *faa = aux;
168 
169 	return of_compatible_match(faa->faa_phandle, compat_data);
170 }
171 
172 static void
cprman_attach(device_t parent,device_t self,void * aux)173 cprman_attach(device_t parent, device_t self, void *aux)
174 {
175 	struct cprman_softc * const sc = device_private(self);
176 	const struct fdt_attach_args *faa = aux;
177 	const int phandle = faa->faa_phandle;
178 
179 	sc->sc_dev = self;
180 	sc->sc_phandle = phandle;
181 	sc->sc_clkdom.funcs = &cprman_clk_funcs;
182 	sc->sc_clkdom.priv = sc;
183 
184 	cprman_add_clock(sc, CPRMAN_CLOCK_UART, "uart");
185 	cprman_add_clock(sc, CPRMAN_CLOCK_VPU, "vpu");
186 	cprman_add_clock(sc, CPRMAN_CLOCK_EMMC, "emmc");
187 	cprman_add_clock(sc, CPRMAN_CLOCK_EMMC2, "emmc2");
188 
189 	aprint_naive("\n");
190 	aprint_normal(": BCM283x Clock Controller\n");
191 
192 	fdtbus_register_clock_controller(self, phandle, &cprman_fdt_funcs);
193 }
194 
195 CFATTACH_DECL_NEW(bcmcprman_fdt, sizeof(struct cprman_softc),
196     cprman_match, cprman_attach, NULL, NULL);
197