xref: /freebsd/sys/dev/fdt/fdt_clock.c (revision fdafd315)
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 
276b6d6c44SIan Lepore #include <sys/param.h>
286b6d6c44SIan Lepore #include <sys/kernel.h>
296b6d6c44SIan Lepore #include <sys/lock.h>
306b6d6c44SIan Lepore #include <sys/mutex.h>
316b6d6c44SIan Lepore #include <sys/queue.h>
32b4172e33SIan Lepore #include <sys/systm.h>
336b6d6c44SIan Lepore 
346b6d6c44SIan Lepore #include <dev/ofw/ofw_bus.h>
356b6d6c44SIan Lepore #include <dev/ofw/ofw_bus_subr.h>
366b6d6c44SIan Lepore 
376b6d6c44SIan Lepore #include "fdt_clock_if.h"
386b6d6c44SIan Lepore #include <dev/fdt/fdt_clock.h>
396b6d6c44SIan Lepore 
406b6d6c44SIan Lepore /*
416b6d6c44SIan Lepore  * Loop through all the tuples in the clocks= property for a device, enabling or
426b6d6c44SIan Lepore  * disabling each clock.
436b6d6c44SIan Lepore  *
446b6d6c44SIan Lepore  * Be liberal about errors for now: warn about a failure to enable but keep
456b6d6c44SIan Lepore  * trying with any other clocks in the list.  Return ENXIO if any errors were
466b6d6c44SIan Lepore  * found, and let the caller decide whether the problem is fatal.
476b6d6c44SIan Lepore  */
486b6d6c44SIan Lepore static int
enable_disable_all(device_t consumer,boolean_t enable)496b6d6c44SIan Lepore enable_disable_all(device_t consumer, boolean_t enable)
506b6d6c44SIan Lepore {
516b6d6c44SIan Lepore 	phandle_t cnode;
526b6d6c44SIan Lepore 	device_t clockdev;
536b6d6c44SIan Lepore 	int clocknum, err, i, ncells;
546b6d6c44SIan Lepore 	uint32_t *clks;
556b6d6c44SIan Lepore 	boolean_t anyerrors;
566b6d6c44SIan Lepore 
576b6d6c44SIan Lepore 	cnode = ofw_bus_get_node(consumer);
58f7604b1bSOleksandr Tymoshenko 	ncells = OF_getencprop_alloc_multi(cnode, "clocks", sizeof(*clks),
596b6d6c44SIan Lepore 	    (void **)&clks);
606b6d6c44SIan Lepore 	if (enable && ncells < 2) {
616b6d6c44SIan Lepore 		device_printf(consumer, "Warning: No clocks specified in fdt "
626b6d6c44SIan Lepore 		    "data; device may not function.");
636b6d6c44SIan Lepore 		return (ENXIO);
646b6d6c44SIan Lepore 	}
656b6d6c44SIan Lepore 	anyerrors = false;
666b6d6c44SIan Lepore 	for (i = 0; i < ncells; i += 2) {
676b6d6c44SIan Lepore 		clockdev = OF_device_from_xref(clks[i]);
686b6d6c44SIan Lepore 		clocknum = clks[i + 1];
696b6d6c44SIan Lepore 		if (clockdev == NULL) {
706b6d6c44SIan Lepore 			if (enable)
716b6d6c44SIan Lepore 				device_printf(consumer, "Warning: can not find "
726b6d6c44SIan Lepore 				    "driver for clock number %u; device may not "
736b6d6c44SIan Lepore 				    "function\n", clocknum);
746b6d6c44SIan Lepore 			anyerrors = true;
756b6d6c44SIan Lepore 			continue;
766b6d6c44SIan Lepore 		}
776b6d6c44SIan Lepore 		if (enable)
786b6d6c44SIan Lepore 			err = FDT_CLOCK_ENABLE(clockdev, clocknum);
796b6d6c44SIan Lepore 		else
806b6d6c44SIan Lepore 			err = FDT_CLOCK_DISABLE(clockdev, clocknum);
816b6d6c44SIan Lepore 		if (err != 0) {
826b6d6c44SIan Lepore 			if (enable)
836b6d6c44SIan Lepore 				device_printf(consumer, "Warning: failed to "
846b6d6c44SIan Lepore 				    "enable clock number %u; device may not "
856b6d6c44SIan Lepore 				    "function\n", clocknum);
866b6d6c44SIan Lepore 			anyerrors = true;
876b6d6c44SIan Lepore 		}
886b6d6c44SIan Lepore 	}
89bc90a48cSOleksandr Tymoshenko 	OF_prop_free(clks);
906b6d6c44SIan Lepore 	return (anyerrors ? ENXIO : 0);
916b6d6c44SIan Lepore }
926b6d6c44SIan Lepore 
936b6d6c44SIan Lepore int
fdt_clock_get_info(device_t consumer,int n,struct fdt_clock_info * info)946b6d6c44SIan Lepore fdt_clock_get_info(device_t consumer, int n, struct fdt_clock_info *info)
956b6d6c44SIan Lepore {
966b6d6c44SIan Lepore 	phandle_t cnode;
976b6d6c44SIan Lepore 	device_t clockdev;
986b6d6c44SIan Lepore 	int clocknum, err, ncells;
996b6d6c44SIan Lepore 	uint32_t *clks;
1006b6d6c44SIan Lepore 
1016b6d6c44SIan Lepore 	cnode = ofw_bus_get_node(consumer);
102f7604b1bSOleksandr Tymoshenko 	ncells = OF_getencprop_alloc_multi(cnode, "clocks", sizeof(*clks),
1036b6d6c44SIan Lepore 	    (void **)&clks);
1046b6d6c44SIan Lepore 	if (ncells <= 0)
1056b6d6c44SIan Lepore 		return (ENXIO);
1066b6d6c44SIan Lepore 	n *= 2;
1076b6d6c44SIan Lepore 	if (ncells <= n)
1086b6d6c44SIan Lepore 		err = ENXIO;
1096b6d6c44SIan Lepore 	else {
1106b6d6c44SIan Lepore 		clockdev = OF_device_from_xref(clks[n]);
1116b6d6c44SIan Lepore 		if (clockdev == NULL)
1126b6d6c44SIan Lepore 			err = ENXIO;
1136b6d6c44SIan Lepore 		else  {
1146b6d6c44SIan Lepore 			/*
1156b6d6c44SIan Lepore 			 * Make struct contents minimally valid, then call
1166b6d6c44SIan Lepore 			 * provider to fill in what it knows (provider can
1176b6d6c44SIan Lepore 			 * override anything it wants to).
1186b6d6c44SIan Lepore 			 */
1196b6d6c44SIan Lepore 			clocknum = clks[n + 1];
120b4172e33SIan Lepore 			bzero(info, sizeof(*info));
1216b6d6c44SIan Lepore 			info->provider = clockdev;
1226b6d6c44SIan Lepore 			info->index = clocknum;
1236b6d6c44SIan Lepore 			info->name = "";
1246b6d6c44SIan Lepore 			err = FDT_CLOCK_GET_INFO(clockdev, clocknum, info);
1256b6d6c44SIan Lepore 		}
1266b6d6c44SIan Lepore 	}
127bc90a48cSOleksandr Tymoshenko 	OF_prop_free(clks);
1286b6d6c44SIan Lepore 	return (err);
1296b6d6c44SIan Lepore }
1306b6d6c44SIan Lepore 
1316b6d6c44SIan Lepore int
fdt_clock_enable_all(device_t consumer)1326b6d6c44SIan Lepore fdt_clock_enable_all(device_t consumer)
1336b6d6c44SIan Lepore {
1346b6d6c44SIan Lepore 
1356b6d6c44SIan Lepore 	return (enable_disable_all(consumer, true));
1366b6d6c44SIan Lepore }
1376b6d6c44SIan Lepore 
1386b6d6c44SIan Lepore int
fdt_clock_disable_all(device_t consumer)1396b6d6c44SIan Lepore fdt_clock_disable_all(device_t consumer)
1406b6d6c44SIan Lepore {
1416b6d6c44SIan Lepore 
1426b6d6c44SIan Lepore 	return (enable_disable_all(consumer, false));
1436b6d6c44SIan Lepore }
1446b6d6c44SIan Lepore 
1456b6d6c44SIan Lepore void
fdt_clock_register_provider(device_t provider)1466b6d6c44SIan Lepore fdt_clock_register_provider(device_t provider)
1476b6d6c44SIan Lepore {
1486b6d6c44SIan Lepore 
14950878a7bSIan Lepore 	OF_device_register_xref(
15050878a7bSIan Lepore 	    OF_xref_from_node(ofw_bus_get_node(provider)), provider);
1516b6d6c44SIan Lepore }
1526b6d6c44SIan Lepore 
1536b6d6c44SIan Lepore void
fdt_clock_unregister_provider(device_t provider)1546b6d6c44SIan Lepore fdt_clock_unregister_provider(device_t provider)
1556b6d6c44SIan Lepore {
1566b6d6c44SIan Lepore 
157b4172e33SIan Lepore 	OF_device_register_xref(OF_xref_from_device(provider), NULL);
1586b6d6c44SIan Lepore }
1596b6d6c44SIan Lepore 
160