2 * Copyright (c) 2014 Ian Lepore <ian@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/kernel.h>
33 #include <sys/mutex.h>
34 #include <sys/queue.h>
35 #include <sys/systm.h>
37 #include <dev/ofw/ofw_bus.h>
38 #include <dev/ofw/ofw_bus_subr.h>
40 #include "fdt_clock_if.h"
41 #include <dev/fdt/fdt_clock.h>
44 * Loop through all the tuples in the clocks= property for a device, enabling or
45 * disabling each clock.
47 * Be liberal about errors for now: warn about a failure to enable but keep
48 * trying with any other clocks in the list. Return ENXIO if any errors were
49 * found, and let the caller decide whether the problem is fatal.
52 enable_disable_all(device_t consumer, boolean_t enable)
56 int clocknum, err, i, ncells;
60 cnode = ofw_bus_get_node(consumer);
61 ncells = OF_getencprop_alloc_multi(cnode, "clocks", sizeof(*clks),
63 if (enable && ncells < 2) {
64 device_printf(consumer, "Warning: No clocks specified in fdt "
65 "data; device may not function.");
69 for (i = 0; i < ncells; i += 2) {
70 clockdev = OF_device_from_xref(clks[i]);
71 clocknum = clks[i + 1];
72 if (clockdev == NULL) {
74 device_printf(consumer, "Warning: can not find "
75 "driver for clock number %u; device may not "
76 "function\n", clocknum);
81 err = FDT_CLOCK_ENABLE(clockdev, clocknum);
83 err = FDT_CLOCK_DISABLE(clockdev, clocknum);
86 device_printf(consumer, "Warning: failed to "
87 "enable clock number %u; device may not "
88 "function\n", clocknum);
93 return (anyerrors ? ENXIO : 0);
97 fdt_clock_get_info(device_t consumer, int n, struct fdt_clock_info *info)
101 int clocknum, err, ncells;
104 cnode = ofw_bus_get_node(consumer);
105 ncells = OF_getencprop_alloc_multi(cnode, "clocks", sizeof(*clks),
113 clockdev = OF_device_from_xref(clks[n]);
114 if (clockdev == NULL)
118 * Make struct contents minimally valid, then call
119 * provider to fill in what it knows (provider can
120 * override anything it wants to).
122 clocknum = clks[n + 1];
123 bzero(info, sizeof(*info));
124 info->provider = clockdev;
125 info->index = clocknum;
127 err = FDT_CLOCK_GET_INFO(clockdev, clocknum, info);
135 fdt_clock_enable_all(device_t consumer)
138 return (enable_disable_all(consumer, true));
142 fdt_clock_disable_all(device_t consumer)
145 return (enable_disable_all(consumer, false));
149 fdt_clock_register_provider(device_t provider)
152 OF_device_register_xref(
153 OF_xref_from_node(ofw_bus_get_node(provider)), provider);
157 fdt_clock_unregister_provider(device_t provider)
160 OF_device_register_xref(OF_xref_from_device(provider), NULL);