xref: /freebsd/sys/arm/ti/clk/ti_gate_clock.c (revision be82b3a0bf72ed3b5f01ac9fcd8dcd3802e3c742)
10050ea24SMichal Meloun /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
30050ea24SMichal Meloun  *
40050ea24SMichal Meloun  * Copyright (c) 2020 Oskar Holmlund <oskar.holmlund@ohdata.se>
50050ea24SMichal Meloun  *
60050ea24SMichal Meloun  * Redistribution and use in source and binary forms, with or without
70050ea24SMichal Meloun  * modification, are permitted provided that the following conditions
80050ea24SMichal Meloun  * are met:
90050ea24SMichal Meloun  * 1. Redistributions of source code must retain the above copyright
100050ea24SMichal Meloun  *    notice, this list of conditions and the following disclaimer.
110050ea24SMichal Meloun  * 2. Redistributions in binary form must reproduce the above copyright
120050ea24SMichal Meloun  *    notice, this list of conditions and the following disclaimer in the
130050ea24SMichal Meloun  *    documentation and/or other materials provided with the distribution.
140050ea24SMichal Meloun  *
150050ea24SMichal Meloun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
160050ea24SMichal Meloun  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
170050ea24SMichal Meloun  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
180050ea24SMichal Meloun  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
190050ea24SMichal Meloun  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
200050ea24SMichal Meloun  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
210050ea24SMichal Meloun  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
220050ea24SMichal Meloun  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
230050ea24SMichal Meloun  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
240050ea24SMichal Meloun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
250050ea24SMichal Meloun  * SUCH DAMAGE.
260050ea24SMichal Meloun  */
270050ea24SMichal Meloun 
280050ea24SMichal Meloun #include <sys/param.h>
290050ea24SMichal Meloun #include <sys/conf.h>
300050ea24SMichal Meloun #include <sys/bus.h>
310050ea24SMichal Meloun #include <sys/kernel.h>
320050ea24SMichal Meloun #include <sys/module.h>
330050ea24SMichal Meloun #include <sys/systm.h>
340050ea24SMichal Meloun #include <sys/libkern.h>
350050ea24SMichal Meloun 
360050ea24SMichal Meloun #include <machine/bus.h>
370050ea24SMichal Meloun #include <dev/fdt/simplebus.h>
380050ea24SMichal Meloun 
39*be82b3a0SEmmanuel Vadot #include <dev/clk/clk_gate.h>
400050ea24SMichal Meloun #include <dev/ofw/ofw_bus.h>
410050ea24SMichal Meloun #include <dev/ofw/ofw_bus_subr.h>
420050ea24SMichal Meloun 
430050ea24SMichal Meloun #include "clock_common.h"
440050ea24SMichal Meloun 
450050ea24SMichal Meloun #define DEBUG_GATE	0
460050ea24SMichal Meloun 
470050ea24SMichal Meloun #if DEBUG_GATE
480050ea24SMichal Meloun #define DPRINTF(dev, msg...) device_printf(dev, msg)
490050ea24SMichal Meloun #else
500050ea24SMichal Meloun #define DPRINTF(dev, msg...)
510050ea24SMichal Meloun #endif
520050ea24SMichal Meloun 
530050ea24SMichal Meloun /*
540050ea24SMichal Meloun  * Devicetree description
550050ea24SMichal Meloun  * Documentation/devicetree/bindings/clock/ti/gate.txt
560050ea24SMichal Meloun  */
570050ea24SMichal Meloun 
580050ea24SMichal Meloun struct ti_gate_softc {
590050ea24SMichal Meloun 	device_t		sc_dev;
600050ea24SMichal Meloun 	bool			attach_done;
610050ea24SMichal Meloun 	uint8_t			sc_type;
620050ea24SMichal Meloun 
630050ea24SMichal Meloun 	struct clk_gate_def	gate_def;
640050ea24SMichal Meloun 	struct clock_cell_info  clock_cell;
650050ea24SMichal Meloun 	struct clkdom		*clkdom;
660050ea24SMichal Meloun };
670050ea24SMichal Meloun 
680050ea24SMichal Meloun static int ti_gate_probe(device_t dev);
690050ea24SMichal Meloun static int ti_gate_attach(device_t dev);
700050ea24SMichal Meloun static int ti_gate_detach(device_t dev);
710050ea24SMichal Meloun 
720050ea24SMichal Meloun #define TI_GATE_CLOCK			7
730050ea24SMichal Meloun #define TI_WAIT_GATE_CLOCK		6
740050ea24SMichal Meloun #define TI_DSS_GATE_CLOCK		5
750050ea24SMichal Meloun #define TI_AM35XX_GATE_CLOCK		4
760050ea24SMichal Meloun #define TI_CLKDM_GATE_CLOCK		3
770050ea24SMichal Meloun #define TI_HSDIV_GATE_CLOCK		2
780050ea24SMichal Meloun #define TI_COMPOSITE_NO_WAIT_GATE_CLOCK	1
790050ea24SMichal Meloun #define TI_GATE_END			0
800050ea24SMichal Meloun 
810050ea24SMichal Meloun static struct ofw_compat_data compat_data[] = {
820050ea24SMichal Meloun 	{ "ti,gate-clock",			TI_GATE_CLOCK },
830050ea24SMichal Meloun 	{ "ti,wait-gate-clock",			TI_WAIT_GATE_CLOCK },
840050ea24SMichal Meloun 	{ "ti,dss-gate-clock",			TI_DSS_GATE_CLOCK },
850050ea24SMichal Meloun 	{ "ti,am35xx-gate-clock",		TI_AM35XX_GATE_CLOCK },
860050ea24SMichal Meloun 	{ "ti,clkdm-gate-clock",		TI_CLKDM_GATE_CLOCK },
870050ea24SMichal Meloun 	{ "ti,hsdiv-gate-cloc",			TI_HSDIV_GATE_CLOCK },
880050ea24SMichal Meloun 	{ "ti,composite-no-wait-gate-clock",	TI_COMPOSITE_NO_WAIT_GATE_CLOCK },
890050ea24SMichal Meloun 	{ NULL,					TI_GATE_END }
900050ea24SMichal Meloun };
910050ea24SMichal Meloun 
920050ea24SMichal Meloun static int
ti_gate_probe(device_t dev)930050ea24SMichal Meloun ti_gate_probe(device_t dev)
940050ea24SMichal Meloun {
950050ea24SMichal Meloun 	if (!ofw_bus_status_okay(dev))
960050ea24SMichal Meloun 		return (ENXIO);
970050ea24SMichal Meloun 
980050ea24SMichal Meloun 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
990050ea24SMichal Meloun 		return (ENXIO);
1000050ea24SMichal Meloun 
1010050ea24SMichal Meloun 	device_set_desc(dev, "TI Gate Clock");
1020050ea24SMichal Meloun 
1030050ea24SMichal Meloun 	return (BUS_PROBE_DEFAULT);
1040050ea24SMichal Meloun }
1050050ea24SMichal Meloun 
1060050ea24SMichal Meloun static int
register_clk(struct ti_gate_softc * sc)1070050ea24SMichal Meloun register_clk(struct ti_gate_softc *sc) {
1080050ea24SMichal Meloun 	int err;
1090050ea24SMichal Meloun 	sc->clkdom = clkdom_create(sc->sc_dev);
1100050ea24SMichal Meloun 	if (sc->clkdom == NULL) {
1110050ea24SMichal Meloun 		DPRINTF(sc->sc_dev, "Failed to create clkdom\n");
1120050ea24SMichal Meloun 		return ENXIO;
1130050ea24SMichal Meloun 	}
1140050ea24SMichal Meloun 
1150050ea24SMichal Meloun 	err = clknode_gate_register(sc->clkdom, &sc->gate_def);
1160050ea24SMichal Meloun 	if (err) {
1170050ea24SMichal Meloun 		DPRINTF(sc->sc_dev, "clknode_gate_register failed %x\n", err);
1180050ea24SMichal Meloun 		return ENXIO;
1190050ea24SMichal Meloun 	}
1200050ea24SMichal Meloun 
1210050ea24SMichal Meloun 	err = clkdom_finit(sc->clkdom);
1220050ea24SMichal Meloun 	if (err) {
1230050ea24SMichal Meloun 		DPRINTF(sc->sc_dev, "Clk domain finit fails %x.\n", err);
1240050ea24SMichal Meloun 		return ENXIO;
1250050ea24SMichal Meloun 	}
1260050ea24SMichal Meloun 
1270050ea24SMichal Meloun 	return (0);
1280050ea24SMichal Meloun }
1290050ea24SMichal Meloun 
1300050ea24SMichal Meloun static int
ti_gate_attach(device_t dev)1310050ea24SMichal Meloun ti_gate_attach(device_t dev)
1320050ea24SMichal Meloun {
1330050ea24SMichal Meloun 	struct ti_gate_softc *sc;
1340050ea24SMichal Meloun 	phandle_t node;
1350050ea24SMichal Meloun 	int err;
1360050ea24SMichal Meloun 	cell_t value;
1370050ea24SMichal Meloun 
1380050ea24SMichal Meloun 	sc = device_get_softc(dev);
1390050ea24SMichal Meloun 	sc->sc_dev = dev;
1400050ea24SMichal Meloun 	node = ofw_bus_get_node(dev);
1410050ea24SMichal Meloun 
1420050ea24SMichal Meloun 	/* Get the compatible type */
1430050ea24SMichal Meloun 	sc->sc_type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
1440050ea24SMichal Meloun 
1450050ea24SMichal Meloun 	/* Get the content of reg properties */
1460050ea24SMichal Meloun 	if (sc->sc_type != TI_CLKDM_GATE_CLOCK) {
1470050ea24SMichal Meloun 		OF_getencprop(node, "reg", &value, sizeof(value));
1480050ea24SMichal Meloun 		sc->gate_def.offset = value;
1490050ea24SMichal Meloun 	}
1500050ea24SMichal Meloun #if DEBUG_GATE
1510050ea24SMichal Meloun 	else {
1520050ea24SMichal Meloun 		DPRINTF(sc->sc_dev, "no reg (TI_CLKDM_GATE_CLOCK)\n");
1530050ea24SMichal Meloun 	}
1540050ea24SMichal Meloun #endif
1550050ea24SMichal Meloun 
1560050ea24SMichal Meloun 	if (OF_hasprop(node, "ti,bit-shift")) {
1570050ea24SMichal Meloun 		OF_getencprop(node, "ti,bit-shift", &value, sizeof(value));
1580050ea24SMichal Meloun 		sc->gate_def.shift = value;
1590050ea24SMichal Meloun 		DPRINTF(sc->sc_dev, "ti,bit-shift => shift %x\n", sc->gate_def.shift);
1600050ea24SMichal Meloun 	}
1610050ea24SMichal Meloun 	if (OF_hasprop(node, "ti,set-bit-to-disable")) {
1620050ea24SMichal Meloun 		sc->gate_def.on_value = 0;
1630050ea24SMichal Meloun 		sc->gate_def.off_value = 1;
1640050ea24SMichal Meloun 		DPRINTF(sc->sc_dev,
1650050ea24SMichal Meloun 			"on_value = 0, off_value = 1 (ti,set-bit-to-disable)\n");
1660050ea24SMichal Meloun 	} else {
1670050ea24SMichal Meloun 		sc->gate_def.on_value = 1;
1680050ea24SMichal Meloun 		sc->gate_def.off_value = 0;
1690050ea24SMichal Meloun 		DPRINTF(sc->sc_dev, "on_value = 1, off_value = 0\n");
1700050ea24SMichal Meloun 	}
1710050ea24SMichal Meloun 
1720050ea24SMichal Meloun 	sc->gate_def.gate_flags = 0x0;
1730050ea24SMichal Meloun 
1740050ea24SMichal Meloun 	read_clock_cells(sc->sc_dev, &sc->clock_cell);
1750050ea24SMichal Meloun 
1760050ea24SMichal Meloun 	create_clkdef(sc->sc_dev, &sc->clock_cell, &sc->gate_def.clkdef);
1770050ea24SMichal Meloun 
1780050ea24SMichal Meloun 	/* Calculate mask */
1790050ea24SMichal Meloun 	sc->gate_def.mask = (1 << fls(sc->clock_cell.num_real_clocks)) - 1;
1800050ea24SMichal Meloun 	DPRINTF(sc->sc_dev, "num_real_clocks %x gate_def.mask %x\n",
1810050ea24SMichal Meloun 		sc->clock_cell.num_real_clocks, sc->gate_def.mask);
1820050ea24SMichal Meloun 
1830050ea24SMichal Meloun 	err = find_parent_clock_names(sc->sc_dev, &sc->clock_cell, &sc->gate_def.clkdef);
1840050ea24SMichal Meloun 
1850050ea24SMichal Meloun 	if (err) {
1860050ea24SMichal Meloun 		/* free_clkdef will be called in ti_gate_new_pass */
1870050ea24SMichal Meloun 		DPRINTF(sc->sc_dev, "find_parent_clock_names failed\n");
1880050ea24SMichal Meloun 		return (bus_generic_attach(sc->sc_dev));
1890050ea24SMichal Meloun 	}
1900050ea24SMichal Meloun 
1910050ea24SMichal Meloun 	err = register_clk(sc);
1920050ea24SMichal Meloun 
1930050ea24SMichal Meloun 	if (err) {
1940050ea24SMichal Meloun 		/* free_clkdef will be called in ti_gate_new_pass */
1950050ea24SMichal Meloun 		DPRINTF(sc->sc_dev, "register_clk failed\n");
1960050ea24SMichal Meloun 		return (bus_generic_attach(sc->sc_dev));
1970050ea24SMichal Meloun 	}
1980050ea24SMichal Meloun 
1990050ea24SMichal Meloun 	sc->attach_done = true;
2000050ea24SMichal Meloun 
2010050ea24SMichal Meloun 	free_clkdef(&sc->gate_def.clkdef);
2020050ea24SMichal Meloun 
2030050ea24SMichal Meloun 	return (bus_generic_attach(sc->sc_dev));
2040050ea24SMichal Meloun }
2050050ea24SMichal Meloun 
2060050ea24SMichal Meloun static int
ti_gate_detach(device_t dev)2070050ea24SMichal Meloun ti_gate_detach(device_t dev)
2080050ea24SMichal Meloun {
2090050ea24SMichal Meloun 	return (EBUSY);
2100050ea24SMichal Meloun }
2110050ea24SMichal Meloun 
2120050ea24SMichal Meloun static void
ti_gate_new_pass(device_t dev)2130050ea24SMichal Meloun ti_gate_new_pass(device_t dev) {
2140050ea24SMichal Meloun 	struct ti_gate_softc *sc;
2150050ea24SMichal Meloun 	int err;
2160050ea24SMichal Meloun 
2170050ea24SMichal Meloun 	sc = device_get_softc(dev);
2180050ea24SMichal Meloun 
2190050ea24SMichal Meloun 	if (sc->attach_done) {
2200050ea24SMichal Meloun 		return;
2210050ea24SMichal Meloun 	}
2220050ea24SMichal Meloun 
2230050ea24SMichal Meloun 	err = find_parent_clock_names(sc->sc_dev, &sc->clock_cell, &sc->gate_def.clkdef);
2240050ea24SMichal Meloun 	if (err) {
2250050ea24SMichal Meloun 		/* free_clkdef will be called in later call to ti_gate_new_pass */
2260050ea24SMichal Meloun 		DPRINTF(sc->sc_dev, "new_pass find_parent_clock_names failed\n");
2270050ea24SMichal Meloun 		return;
2280050ea24SMichal Meloun 	}
2290050ea24SMichal Meloun 
2300050ea24SMichal Meloun 	err = register_clk(sc);
2310050ea24SMichal Meloun 	if (err) {
2320050ea24SMichal Meloun 		/* free_clkdef will be called in later call to ti_gate_new_pass */
2330050ea24SMichal Meloun 		DPRINTF(sc->sc_dev, "new_pass register_clk failed\n");
2340050ea24SMichal Meloun 		return;
2350050ea24SMichal Meloun 	}
2360050ea24SMichal Meloun 
2370050ea24SMichal Meloun 	sc->attach_done = true;
2380050ea24SMichal Meloun 
2390050ea24SMichal Meloun 	free_clkdef(&sc->gate_def.clkdef);
2400050ea24SMichal Meloun }
2410050ea24SMichal Meloun 
2420050ea24SMichal Meloun static device_method_t ti_gate_methods[] = {
2430050ea24SMichal Meloun 	/* Device interface */
2440050ea24SMichal Meloun 	DEVMETHOD(device_probe,		ti_gate_probe),
2450050ea24SMichal Meloun 	DEVMETHOD(device_attach,	ti_gate_attach),
2460050ea24SMichal Meloun 	DEVMETHOD(device_detach,	ti_gate_detach),
2470050ea24SMichal Meloun 
2480050ea24SMichal Meloun 	/* Bus interface */
2490050ea24SMichal Meloun 	DEVMETHOD(bus_new_pass,		ti_gate_new_pass),
2500050ea24SMichal Meloun 
2510050ea24SMichal Meloun 	DEVMETHOD_END
2520050ea24SMichal Meloun };
2530050ea24SMichal Meloun 
2540050ea24SMichal Meloun DEFINE_CLASS_0(ti_gate, ti_gate_driver, ti_gate_methods,
2550050ea24SMichal Meloun 	sizeof(struct ti_gate_softc));
2560050ea24SMichal Meloun 
2578537e671SJohn Baldwin EARLY_DRIVER_MODULE(ti_gate, simplebus, ti_gate_driver, 0, 0,
2588537e671SJohn Baldwin     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
2590050ea24SMichal Meloun MODULE_VERSION(ti_gate, 1);
260