xref: /freebsd/sys/dev/fdt/fdt_clock.c (revision 50878a7bbdbbd7b42b2101e4ca1606b974b8da20)
16b6d6c44SIan Lepore /*-
26b6d6c44SIan Lepore  * Copyright (c) 2014 Ian Lepore <ian@freebsd.org>
36b6d6c44SIan Lepore  * All rights reserved.
46b6d6c44SIan Lepore  *
56b6d6c44SIan Lepore  * Redistribution and use in source and binary forms, with or without
66b6d6c44SIan Lepore  * modification, are permitted provided that the following conditions
76b6d6c44SIan Lepore  * are met:
86b6d6c44SIan Lepore  * 1. Redistributions of source code must retain the above copyright
96b6d6c44SIan Lepore  *    notice, this list of conditions and the following disclaimer.
106b6d6c44SIan Lepore  * 2. Redistributions in binary form must reproduce the above copyright
116b6d6c44SIan Lepore  *    notice, this list of conditions and the following disclaimer in the
126b6d6c44SIan Lepore  *    documentation and/or other materials provided with the distribution.
136b6d6c44SIan Lepore  *
146b6d6c44SIan Lepore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
156b6d6c44SIan Lepore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
166b6d6c44SIan Lepore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
176b6d6c44SIan Lepore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
186b6d6c44SIan Lepore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
196b6d6c44SIan Lepore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
206b6d6c44SIan Lepore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
216b6d6c44SIan Lepore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
226b6d6c44SIan Lepore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
236b6d6c44SIan Lepore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
246b6d6c44SIan Lepore  * SUCH DAMAGE.
256b6d6c44SIan Lepore  *
266b6d6c44SIan Lepore  * $FreeBSD$
276b6d6c44SIan Lepore  */
286b6d6c44SIan Lepore 
296b6d6c44SIan Lepore #include <sys/cdefs.h>
306b6d6c44SIan Lepore #include <sys/param.h>
316b6d6c44SIan Lepore #include <sys/kernel.h>
326b6d6c44SIan Lepore #include <sys/lock.h>
336b6d6c44SIan Lepore #include <sys/mutex.h>
346b6d6c44SIan Lepore #include <sys/queue.h>
35b4172e33SIan Lepore #include <sys/systm.h>
366b6d6c44SIan Lepore 
376b6d6c44SIan Lepore #include <dev/ofw/ofw_bus.h>
386b6d6c44SIan Lepore #include <dev/ofw/ofw_bus_subr.h>
396b6d6c44SIan Lepore 
406b6d6c44SIan Lepore #include "fdt_clock_if.h"
416b6d6c44SIan Lepore #include <dev/fdt/fdt_clock.h>
426b6d6c44SIan Lepore 
436b6d6c44SIan Lepore /*
446b6d6c44SIan Lepore  * Loop through all the tuples in the clocks= property for a device, enabling or
456b6d6c44SIan Lepore  * disabling each clock.
466b6d6c44SIan Lepore  *
476b6d6c44SIan Lepore  * Be liberal about errors for now: warn about a failure to enable but keep
486b6d6c44SIan Lepore  * trying with any other clocks in the list.  Return ENXIO if any errors were
496b6d6c44SIan Lepore  * found, and let the caller decide whether the problem is fatal.
506b6d6c44SIan Lepore  */
516b6d6c44SIan Lepore static int
526b6d6c44SIan Lepore enable_disable_all(device_t consumer, boolean_t enable)
536b6d6c44SIan Lepore {
546b6d6c44SIan Lepore 	phandle_t cnode;
556b6d6c44SIan Lepore 	device_t clockdev;
566b6d6c44SIan Lepore 	int clocknum, err, i, ncells;
576b6d6c44SIan Lepore 	uint32_t *clks;
586b6d6c44SIan Lepore 	boolean_t anyerrors;
596b6d6c44SIan Lepore 
606b6d6c44SIan Lepore 	cnode = ofw_bus_get_node(consumer);
616b6d6c44SIan Lepore 	ncells = OF_getencprop_alloc(cnode, "clocks", sizeof(*clks),
626b6d6c44SIan Lepore 	    (void **)&clks);
636b6d6c44SIan Lepore 	if (enable && ncells < 2) {
646b6d6c44SIan Lepore 		device_printf(consumer, "Warning: No clocks specified in fdt "
656b6d6c44SIan Lepore 		    "data; device may not function.");
666b6d6c44SIan Lepore 		return (ENXIO);
676b6d6c44SIan Lepore 	}
686b6d6c44SIan Lepore 	anyerrors = false;
696b6d6c44SIan Lepore 	for (i = 0; i < ncells; i += 2) {
706b6d6c44SIan Lepore 		clockdev = OF_device_from_xref(clks[i]);
716b6d6c44SIan Lepore 		clocknum = clks[i + 1];
726b6d6c44SIan Lepore 		if (clockdev == NULL) {
736b6d6c44SIan Lepore 			if (enable)
746b6d6c44SIan Lepore 				device_printf(consumer, "Warning: can not find "
756b6d6c44SIan Lepore 				    "driver for clock number %u; device may not "
766b6d6c44SIan Lepore 				    "function\n", clocknum);
776b6d6c44SIan Lepore 			anyerrors = true;
786b6d6c44SIan Lepore 			continue;
796b6d6c44SIan Lepore 		}
806b6d6c44SIan Lepore 		if (enable)
816b6d6c44SIan Lepore 			err = FDT_CLOCK_ENABLE(clockdev, clocknum);
826b6d6c44SIan Lepore 		else
836b6d6c44SIan Lepore 			err = FDT_CLOCK_DISABLE(clockdev, clocknum);
846b6d6c44SIan Lepore 		if (err != 0) {
856b6d6c44SIan Lepore 			if (enable)
866b6d6c44SIan Lepore 				device_printf(consumer, "Warning: failed to "
876b6d6c44SIan Lepore 				    "enable clock number %u; device may not "
886b6d6c44SIan Lepore 				    "function\n", clocknum);
896b6d6c44SIan Lepore 			anyerrors = true;
906b6d6c44SIan Lepore 		}
916b6d6c44SIan Lepore 	}
926b6d6c44SIan Lepore 	free(clks, M_OFWPROP);
936b6d6c44SIan Lepore 	return (anyerrors ? ENXIO : 0);
946b6d6c44SIan Lepore }
956b6d6c44SIan Lepore 
966b6d6c44SIan Lepore int
976b6d6c44SIan Lepore fdt_clock_get_info(device_t consumer, int n, struct fdt_clock_info *info)
986b6d6c44SIan Lepore {
996b6d6c44SIan Lepore 	phandle_t cnode;
1006b6d6c44SIan Lepore 	device_t clockdev;
1016b6d6c44SIan Lepore 	int clocknum, err, ncells;
1026b6d6c44SIan Lepore 	uint32_t *clks;
1036b6d6c44SIan Lepore 
1046b6d6c44SIan Lepore 	cnode = ofw_bus_get_node(consumer);
1056b6d6c44SIan Lepore 	ncells = OF_getencprop_alloc(cnode, "clocks", sizeof(*clks),
1066b6d6c44SIan Lepore 	    (void **)&clks);
1076b6d6c44SIan Lepore 	if (ncells <= 0)
1086b6d6c44SIan Lepore 		return (ENXIO);
1096b6d6c44SIan Lepore 	n *= 2;
1106b6d6c44SIan Lepore 	if (ncells <= n)
1116b6d6c44SIan Lepore 		err = ENXIO;
1126b6d6c44SIan Lepore 	else {
1136b6d6c44SIan Lepore 		clockdev = OF_device_from_xref(clks[n]);
1146b6d6c44SIan Lepore 		if (clockdev == NULL)
1156b6d6c44SIan Lepore 			err = ENXIO;
1166b6d6c44SIan Lepore 		else  {
1176b6d6c44SIan Lepore 			/*
1186b6d6c44SIan Lepore 			 * Make struct contents minimally valid, then call
1196b6d6c44SIan Lepore 			 * provider to fill in what it knows (provider can
1206b6d6c44SIan Lepore 			 * override anything it wants to).
1216b6d6c44SIan Lepore 			 */
1226b6d6c44SIan Lepore 			clocknum = clks[n + 1];
123b4172e33SIan Lepore 			bzero(info, sizeof(*info));
1246b6d6c44SIan Lepore 			info->provider = clockdev;
1256b6d6c44SIan Lepore 			info->index = clocknum;
1266b6d6c44SIan Lepore 			info->name = "";
1276b6d6c44SIan Lepore 			err = FDT_CLOCK_GET_INFO(clockdev, clocknum, info);
1286b6d6c44SIan Lepore 		}
1296b6d6c44SIan Lepore 	}
1306b6d6c44SIan Lepore 	free(clks, M_OFWPROP);
1316b6d6c44SIan Lepore 	return (err);
1326b6d6c44SIan Lepore }
1336b6d6c44SIan Lepore 
1346b6d6c44SIan Lepore int
1356b6d6c44SIan Lepore fdt_clock_enable_all(device_t consumer)
1366b6d6c44SIan Lepore {
1376b6d6c44SIan Lepore 
1386b6d6c44SIan Lepore 	return (enable_disable_all(consumer, true));
1396b6d6c44SIan Lepore }
1406b6d6c44SIan Lepore 
1416b6d6c44SIan Lepore int
1426b6d6c44SIan Lepore fdt_clock_disable_all(device_t consumer)
1436b6d6c44SIan Lepore {
1446b6d6c44SIan Lepore 
1456b6d6c44SIan Lepore 	return (enable_disable_all(consumer, false));
1466b6d6c44SIan Lepore }
1476b6d6c44SIan Lepore 
1486b6d6c44SIan Lepore void
1496b6d6c44SIan Lepore fdt_clock_register_provider(device_t provider)
1506b6d6c44SIan Lepore {
1516b6d6c44SIan Lepore 
152*50878a7bSIan Lepore 	OF_device_register_xref(
153*50878a7bSIan Lepore         OF_xref_from_node(ofw_bus_get_node(provider)), provider);
1546b6d6c44SIan Lepore }
1556b6d6c44SIan Lepore 
1566b6d6c44SIan Lepore void
1576b6d6c44SIan Lepore fdt_clock_unregister_provider(device_t provider)
1586b6d6c44SIan Lepore {
1596b6d6c44SIan Lepore 
160b4172e33SIan Lepore 	OF_device_register_xref(OF_xref_from_device(provider), NULL);
1616b6d6c44SIan Lepore }
1626b6d6c44SIan Lepore 
163