xref: /freebsd/sys/dev/fdt/fdt_clock.c (revision fdafd315)
1 /*-
2  * Copyright (c) 2014 Ian Lepore <ian@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/param.h>
28 #include <sys/kernel.h>
29 #include <sys/lock.h>
30 #include <sys/mutex.h>
31 #include <sys/queue.h>
32 #include <sys/systm.h>
33 
34 #include <dev/ofw/ofw_bus.h>
35 #include <dev/ofw/ofw_bus_subr.h>
36 
37 #include "fdt_clock_if.h"
38 #include <dev/fdt/fdt_clock.h>
39 
40 /*
41  * Loop through all the tuples in the clocks= property for a device, enabling or
42  * disabling each clock.
43  *
44  * Be liberal about errors for now: warn about a failure to enable but keep
45  * trying with any other clocks in the list.  Return ENXIO if any errors were
46  * found, and let the caller decide whether the problem is fatal.
47  */
48 static int
enable_disable_all(device_t consumer,boolean_t enable)49 enable_disable_all(device_t consumer, boolean_t enable)
50 {
51 	phandle_t cnode;
52 	device_t clockdev;
53 	int clocknum, err, i, ncells;
54 	uint32_t *clks;
55 	boolean_t anyerrors;
56 
57 	cnode = ofw_bus_get_node(consumer);
58 	ncells = OF_getencprop_alloc_multi(cnode, "clocks", sizeof(*clks),
59 	    (void **)&clks);
60 	if (enable && ncells < 2) {
61 		device_printf(consumer, "Warning: No clocks specified in fdt "
62 		    "data; device may not function.");
63 		return (ENXIO);
64 	}
65 	anyerrors = false;
66 	for (i = 0; i < ncells; i += 2) {
67 		clockdev = OF_device_from_xref(clks[i]);
68 		clocknum = clks[i + 1];
69 		if (clockdev == NULL) {
70 			if (enable)
71 				device_printf(consumer, "Warning: can not find "
72 				    "driver for clock number %u; device may not "
73 				    "function\n", clocknum);
74 			anyerrors = true;
75 			continue;
76 		}
77 		if (enable)
78 			err = FDT_CLOCK_ENABLE(clockdev, clocknum);
79 		else
80 			err = FDT_CLOCK_DISABLE(clockdev, clocknum);
81 		if (err != 0) {
82 			if (enable)
83 				device_printf(consumer, "Warning: failed to "
84 				    "enable clock number %u; device may not "
85 				    "function\n", clocknum);
86 			anyerrors = true;
87 		}
88 	}
89 	OF_prop_free(clks);
90 	return (anyerrors ? ENXIO : 0);
91 }
92 
93 int
fdt_clock_get_info(device_t consumer,int n,struct fdt_clock_info * info)94 fdt_clock_get_info(device_t consumer, int n, struct fdt_clock_info *info)
95 {
96 	phandle_t cnode;
97 	device_t clockdev;
98 	int clocknum, err, ncells;
99 	uint32_t *clks;
100 
101 	cnode = ofw_bus_get_node(consumer);
102 	ncells = OF_getencprop_alloc_multi(cnode, "clocks", sizeof(*clks),
103 	    (void **)&clks);
104 	if (ncells <= 0)
105 		return (ENXIO);
106 	n *= 2;
107 	if (ncells <= n)
108 		err = ENXIO;
109 	else {
110 		clockdev = OF_device_from_xref(clks[n]);
111 		if (clockdev == NULL)
112 			err = ENXIO;
113 		else  {
114 			/*
115 			 * Make struct contents minimally valid, then call
116 			 * provider to fill in what it knows (provider can
117 			 * override anything it wants to).
118 			 */
119 			clocknum = clks[n + 1];
120 			bzero(info, sizeof(*info));
121 			info->provider = clockdev;
122 			info->index = clocknum;
123 			info->name = "";
124 			err = FDT_CLOCK_GET_INFO(clockdev, clocknum, info);
125 		}
126 	}
127 	OF_prop_free(clks);
128 	return (err);
129 }
130 
131 int
fdt_clock_enable_all(device_t consumer)132 fdt_clock_enable_all(device_t consumer)
133 {
134 
135 	return (enable_disable_all(consumer, true));
136 }
137 
138 int
fdt_clock_disable_all(device_t consumer)139 fdt_clock_disable_all(device_t consumer)
140 {
141 
142 	return (enable_disable_all(consumer, false));
143 }
144 
145 void
fdt_clock_register_provider(device_t provider)146 fdt_clock_register_provider(device_t provider)
147 {
148 
149 	OF_device_register_xref(
150 	    OF_xref_from_node(ofw_bus_get_node(provider)), provider);
151 }
152 
153 void
fdt_clock_unregister_provider(device_t provider)154 fdt_clock_unregister_provider(device_t provider)
155 {
156 
157 	OF_device_register_xref(OF_xref_from_device(provider), NULL);
158 }
159 
160