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