xref: /freebsd/sys/arm/ti/clk/ti_clkctrl.c (revision b4af4f93c682e445bf159f0d1ec90b636296c946)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
5  *
6  * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/fbio.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/rman.h>
42 #include <sys/resource.h>
43 #include <machine/bus.h>
44 #include <vm/vm.h>
45 #include <vm/vm_extern.h>
46 #include <vm/vm_kern.h>
47 #include <vm/pmap.h>
48 
49 #include <dev/fdt/simplebus.h>
50 
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
53 
54 #include <arm/ti/clk/ti_clk_clkctrl.h>
55 #include <arm/ti/ti_omap4_cm.h>
56 #include <arm/ti/ti_cpuid.h>
57 
58 #if 0
59 #define DPRINTF(dev, msg...) device_printf(dev, msg)
60 #else
61 #define DPRINTF(dev, msg...)
62 #endif
63 
64 #define L4LS_CLKCTRL_38 	2
65 #define L4_WKUP_CLKCTRL_0	1
66 #define NO_SPECIAL_REG		0
67 
68 /* Documentation/devicetree/bindings/clock/ti-clkctrl.txt */
69 
70 #define TI_CLKCTRL_L4_WKUP	5
71 #define TI_CLKCTRL_L4_SECURE	4
72 #define TI_CLKCTRL_L4_PER	3
73 #define TI_CLKCTRL_L4_CFG	2
74 #define TI_CLKCTRL		1
75 #define TI_CLKCTRL_END		0
76 
77 static struct ofw_compat_data compat_data[] = {
78 	{ "ti,clkctrl-l4-wkup",		TI_CLKCTRL_L4_WKUP },
79 	{ "ti,clkctrl-l4-secure",	TI_CLKCTRL_L4_SECURE },
80 	{ "ti,clkctrl-l4-per",		TI_CLKCTRL_L4_PER },
81 	{ "ti,clkctrl-l4-cfg",		TI_CLKCTRL_L4_CFG },
82 	{ "ti,clkctrl",			TI_CLKCTRL },
83 	{ NULL,				TI_CLKCTRL_END }
84 };
85 
86 struct ti_clkctrl_softc {
87 	device_t			dev;
88 
89 	struct clkdom			*clkdom;
90 };
91 
92 static int ti_clkctrl_probe(device_t dev);
93 static int ti_clkctrl_attach(device_t dev);
94 static int ti_clkctrl_detach(device_t dev);
95 int clkctrl_ofw_map(struct clkdom *clkdom, uint32_t ncells,
96     phandle_t *cells, struct clknode **clk);
97 static int
98 create_clkctrl(struct ti_clkctrl_softc *sc, cell_t *reg, uint32_t index, uint32_t reg_offset,
99     uint64_t parent_offset, const char *org_name, bool special_gdbclk_reg);
100 
101 static int
102 ti_clkctrl_probe(device_t dev)
103 {
104 	if (!ofw_bus_status_okay(dev))
105 		return (ENXIO);
106 
107 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
108 		return (ENXIO);
109 
110 	device_set_desc(dev, "TI clkctrl");
111 
112 	return (BUS_PROBE_DEFAULT);
113 }
114 
115 static int
116 ti_clkctrl_attach(device_t dev)
117 {
118 	struct ti_clkctrl_softc *sc;
119 	phandle_t node;
120 	cell_t	*reg;
121 	ssize_t numbytes_reg;
122 	int num_reg, err, ti_clock_cells;
123 	uint32_t index, reg_offset, reg_address;
124 	const char *org_name;
125 	uint64_t parent_offset;
126 	uint8_t special_reg = NO_SPECIAL_REG;
127 
128 	sc = device_get_softc(dev);
129 	sc->dev = dev;
130 	node = ofw_bus_get_node(dev);
131 
132 	/* Sanity check */
133 	err = OF_searchencprop(node, "#clock-cells",
134 		&ti_clock_cells, sizeof(ti_clock_cells));
135 	if (err == -1) {
136 		device_printf(sc->dev, "Failed to get #clock-cells\n");
137 		return (ENXIO);
138 	}
139 
140 	if (ti_clock_cells != 2) {
141 		device_printf(sc->dev, "clock cells(%d) != 2\n",
142 			ti_clock_cells);
143 		return (ENXIO);
144 	}
145 
146 	/* Grab the content of reg properties */
147 	numbytes_reg = OF_getproplen(node, "reg");
148 	if (numbytes_reg == 0) {
149 		device_printf(sc->dev, "reg property empty - check your devicetree\n");
150 		return (ENXIO);
151 	}
152 	num_reg = numbytes_reg / sizeof(cell_t);
153 
154 	reg = malloc(numbytes_reg, M_DEVBUF, M_WAITOK);
155 	OF_getencprop(node, "reg", reg, numbytes_reg);
156 
157 	/* Create clock domain */
158 	sc->clkdom = clkdom_create(sc->dev);
159 	if (sc->clkdom == NULL) {
160 		free(reg, M_DEVBUF);
161 		DPRINTF(sc->dev, "Failed to create clkdom\n");
162 		return (ENXIO);
163 	}
164 	clkdom_set_ofw_mapper(sc->clkdom, clkctrl_ofw_map);
165 
166 	/* Create clock nodes */
167 	/* name */
168 	clk_parse_ofw_clk_name(sc->dev, node, &org_name);
169 
170 	/* Get parent range */
171 	parent_offset = ti_omap4_cm_get_simplebus_base_host(device_get_parent(dev));
172 
173 	/* Check if this is a clkctrl with special registers like gpio */
174 	switch (ti_chip()) {
175 #ifdef SOC_OMAP4
176 	case CHIP_OMAP_4:
177 		/* FIXME: Todo */
178 		break;
179 
180 #endif /* SOC_OMAP4 */
181 #ifdef SOC_TI_AM335X
182 	/* Checkout TRM 8.1.12.1.29 - 8.1.12.31 and 8.1.12.2.3
183 	 * and the DTS.
184 	 */
185 	case CHIP_AM335X:
186 		if (strcmp(org_name, "l4ls-clkctrl@38") == 0)
187 			special_reg = L4LS_CLKCTRL_38;
188 		else if (strcmp(org_name, "l4-wkup-clkctrl@0") == 0)
189 			special_reg = L4_WKUP_CLKCTRL_0;
190 		break;
191 #endif /* SOC_TI_AM335X */
192 	default:
193 		break;
194 	}
195 
196 	/* reg property has a pair of (base address, length) */
197 	for (index = 0; index < num_reg; index += 2) {
198 		for (reg_offset = 0; reg_offset < reg[index+1]; reg_offset += sizeof(cell_t)) {
199 
200 			err = create_clkctrl(sc, reg, index, reg_offset, parent_offset,
201 			    org_name, false);
202 			if (err)
203 				goto cleanup;
204 
205 			/* Create special clkctrl for GDBCLK in GPIO registers */
206 			switch (special_reg) {
207 			case NO_SPECIAL_REG:
208 				break;
209 			case L4LS_CLKCTRL_38:
210 				reg_address = reg[index] + reg_offset-reg[0];
211 				if (reg_address == 0x74 ||
212 				    reg_address == 0x78 ||
213 				    reg_address == 0x7C)
214 				{
215 					err = create_clkctrl(sc, reg, index, reg_offset,
216 					    parent_offset, org_name, true);
217 					if (err)
218 						goto cleanup;
219 				}
220 				break;
221 			case L4_WKUP_CLKCTRL_0:
222 				reg_address = reg[index] + reg_offset - reg[0];
223 				if (reg_address == 0x8)
224 				{
225 					err = create_clkctrl(sc, reg, index, reg_offset,
226 					    parent_offset, org_name, true);
227 					if (err)
228 						goto cleanup;
229 				}
230 				break;
231 			} /* switch (special_reg) */
232 		} /* inner for */
233 	} /* for */
234 
235 	err = clkdom_finit(sc->clkdom);
236 	if (err) {
237 		DPRINTF(sc->dev, "Clk domain finit fails %x.\n", err);
238 		err = ENXIO;
239 		goto cleanup;
240 	}
241 
242 cleanup:
243 	OF_prop_free(__DECONST(char *, org_name));
244 
245 	free(reg, M_DEVBUF);
246 
247 	if (err)
248 		return (err);
249 
250 	return (bus_generic_attach(dev));
251 }
252 
253 static int
254 ti_clkctrl_detach(device_t dev)
255 {
256 	return (EBUSY);
257 }
258 
259 /* modified version of default mapper from clk.c */
260 int
261 clkctrl_ofw_map(struct clkdom *clkdom, uint32_t ncells,
262     phandle_t *cells, struct clknode **clk) {
263 	if (ncells == 0)
264 		*clk = clknode_find_by_id(clkdom, 1);
265 	else if (ncells == 1)
266 		*clk = clknode_find_by_id(clkdom, cells[0]);
267 	else if (ncells == 2) {
268 		/* To avoid collision with other IDs just add one.
269 		 * All other registers has an offset of 4 from each other.
270 		 */
271 		if (cells[1])
272 			*clk = clknode_find_by_id(clkdom, cells[0]+1);
273 		else
274 			*clk = clknode_find_by_id(clkdom, cells[0]);
275 	}
276 	else
277 		return (ERANGE);
278 
279 	if (*clk == NULL)
280 		return (ENXIO);
281 
282 	return (0);
283 }
284 
285 static int
286 create_clkctrl(struct ti_clkctrl_softc *sc, cell_t *reg, uint32_t index, uint32_t reg_offset,
287     uint64_t parent_offset, const char *org_name, bool special_gdbclk_reg) {
288 	struct ti_clk_clkctrl_def def;
289 	char *name;
290 	size_t name_len;
291 	int err;
292 
293 	name_len = strlen(org_name) + 1 + 5; /* 5 = _xxxx */
294 	name = malloc(name_len, M_OFWPROP, M_WAITOK);
295 
296 	/*
297 	 * Check out XX_CLKCTRL-INDEX(offset)-macro dance in
298 	 * sys/gnu/dts/dts/include/dt-bindings/clock/am3.h
299 	 * sys/gnu/dts/dts/include/dt-bindings/clock/am4.h
300 	 * sys/gnu/dts/dts/include/dt-bindings/clock/dra7.h
301 	 * reg[0] are in practice the same as the offset described in the dts.
302 	 */
303 	/* special_gdbclk_reg are 0 or 1 */
304 	def.clkdef.id = reg[index] + reg_offset - reg[0] + special_gdbclk_reg;
305 	def.register_offset = parent_offset + reg[index] + reg_offset;
306 
307 	/* Indicate this clkctrl is special and dont use IDLEST/MODULEMODE */
308 	def.gdbclk = special_gdbclk_reg;
309 
310 	/* Make up an uniq name in the namespace for each clkctrl */
311 	snprintf(name, name_len, "%s_%x",
312 		org_name, def.clkdef.id);
313 	def.clkdef.name = (const char *) name;
314 
315 	DPRINTF(sc->dev, "ti_clkctrl_attach: reg[%d]: %s %x\n",
316 		index, def.clkdef.name, def.clkdef.id);
317 
318 	/* No parent name */
319 	def.clkdef.parent_cnt = 0;
320 
321 	/* set flags */
322 	def.clkdef.flags = 0x0;
323 
324 	/* Register the clkctrl */
325 	err = ti_clknode_clkctrl_register(sc->clkdom, &def);
326 	if (err) {
327 		DPRINTF(sc->dev,
328 			"ti_clknode_clkctrl_register[%d:%d] failed %x\n",
329 			index, reg_offset, err);
330 		err = ENXIO;
331 	}
332 	OF_prop_free(name);
333 	return (err);
334 }
335 
336 static device_method_t ti_clkctrl_methods[] = {
337 	/* Device interface */
338 	DEVMETHOD(device_probe,		ti_clkctrl_probe),
339 	DEVMETHOD(device_attach,	ti_clkctrl_attach),
340 	DEVMETHOD(device_detach,	ti_clkctrl_detach),
341 
342 	DEVMETHOD_END
343 };
344 
345 DEFINE_CLASS_0(ti_clkctrl, ti_clkctrl_driver, ti_clkctrl_methods,
346     sizeof(struct ti_clkctrl_softc));
347 
348 static devclass_t ti_clkctrl_devclass;
349 
350 EARLY_DRIVER_MODULE(ti_clkctrl, simplebus, ti_clkctrl_driver,
351 ti_clkctrl_devclass, 0, 0, BUS_PASS_BUS+BUS_PASS_ORDER_MIDDLE);
352 
353 MODULE_VERSION(ti_clkctrl, 1);
354