fdt_clock.c revision 1.5 1 1.5 jmcneill /* $NetBSD: fdt_clock.c,v 1.5 2018/06/30 20:34:43 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.5 jmcneill __KERNEL_RCSID(0, "$NetBSD: fdt_clock.c,v 1.5 2018/06/30 20:34:43 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/kmem.h>
35 1.5 jmcneill #include <sys/queue.h>
36 1.1 jmcneill
37 1.1 jmcneill #include <libfdt.h>
38 1.1 jmcneill #include <dev/fdt/fdtvar.h>
39 1.1 jmcneill
40 1.3 jmcneill #include <dev/clk/clk_backend.h>
41 1.3 jmcneill
42 1.1 jmcneill struct fdtbus_clock_controller {
43 1.1 jmcneill device_t cc_dev;
44 1.1 jmcneill int cc_phandle;
45 1.1 jmcneill const struct fdtbus_clock_controller_func *cc_funcs;
46 1.1 jmcneill
47 1.5 jmcneill LIST_ENTRY(fdtbus_clock_controller) cc_next;
48 1.1 jmcneill };
49 1.1 jmcneill
50 1.5 jmcneill static LIST_HEAD(, fdtbus_clock_controller) fdtbus_clock_controllers =
51 1.5 jmcneill LIST_HEAD_INITIALIZER(fdtbus_clock_controller);
52 1.1 jmcneill
53 1.1 jmcneill int
54 1.1 jmcneill fdtbus_register_clock_controller(device_t dev, int phandle,
55 1.1 jmcneill const struct fdtbus_clock_controller_func *funcs)
56 1.1 jmcneill {
57 1.1 jmcneill struct fdtbus_clock_controller *cc;
58 1.1 jmcneill
59 1.1 jmcneill cc = kmem_alloc(sizeof(*cc), KM_SLEEP);
60 1.1 jmcneill cc->cc_dev = dev;
61 1.1 jmcneill cc->cc_phandle = phandle;
62 1.1 jmcneill cc->cc_funcs = funcs;
63 1.1 jmcneill
64 1.5 jmcneill LIST_INSERT_HEAD(&fdtbus_clock_controllers, cc, cc_next);
65 1.1 jmcneill
66 1.3 jmcneill fdtbus_clock_assign(phandle);
67 1.3 jmcneill
68 1.1 jmcneill return 0;
69 1.1 jmcneill }
70 1.1 jmcneill
71 1.1 jmcneill static struct fdtbus_clock_controller *
72 1.1 jmcneill fdtbus_get_clock_controller(int phandle)
73 1.1 jmcneill {
74 1.1 jmcneill struct fdtbus_clock_controller *cc;
75 1.1 jmcneill
76 1.5 jmcneill LIST_FOREACH(cc, &fdtbus_clock_controllers, cc_next) {
77 1.5 jmcneill if (cc->cc_phandle == phandle)
78 1.1 jmcneill return cc;
79 1.1 jmcneill }
80 1.1 jmcneill
81 1.1 jmcneill return NULL;
82 1.1 jmcneill }
83 1.1 jmcneill
84 1.3 jmcneill static struct clk *
85 1.3 jmcneill fdtbus_clock_get_index_prop(int phandle, u_int index, const char *prop)
86 1.1 jmcneill {
87 1.1 jmcneill struct fdtbus_clock_controller *cc;
88 1.1 jmcneill struct clk *clk = NULL;
89 1.3 jmcneill const u_int *p;
90 1.1 jmcneill u_int n, clock_cells;
91 1.1 jmcneill int len, resid;
92 1.1 jmcneill
93 1.3 jmcneill p = fdtbus_get_prop(phandle, prop, &len);
94 1.3 jmcneill if (p == NULL)
95 1.1 jmcneill return NULL;
96 1.1 jmcneill
97 1.1 jmcneill for (n = 0, resid = len; resid > 0; n++) {
98 1.1 jmcneill const int cc_phandle =
99 1.1 jmcneill fdtbus_get_phandle_from_native(be32toh(p[0]));
100 1.1 jmcneill if (of_getprop_uint32(cc_phandle, "#clock-cells", &clock_cells))
101 1.1 jmcneill break;
102 1.1 jmcneill if (n == index) {
103 1.1 jmcneill cc = fdtbus_get_clock_controller(cc_phandle);
104 1.1 jmcneill if (cc == NULL)
105 1.3 jmcneill break;
106 1.1 jmcneill clk = cc->cc_funcs->decode(cc->cc_dev,
107 1.1 jmcneill clock_cells > 0 ? &p[1] : NULL, clock_cells * 4);
108 1.1 jmcneill break;
109 1.1 jmcneill }
110 1.1 jmcneill resid -= (clock_cells + 1) * 4;
111 1.1 jmcneill p += clock_cells + 1;
112 1.1 jmcneill }
113 1.1 jmcneill
114 1.1 jmcneill return clk;
115 1.1 jmcneill }
116 1.1 jmcneill
117 1.1 jmcneill struct clk *
118 1.3 jmcneill fdtbus_clock_get_index(int phandle, u_int index)
119 1.3 jmcneill {
120 1.3 jmcneill return fdtbus_clock_get_index_prop(phandle, index, "clocks");
121 1.3 jmcneill }
122 1.3 jmcneill
123 1.3 jmcneill static struct clk *
124 1.3 jmcneill fdtbus_clock_get_prop(int phandle, const char *clkname, const char *prop)
125 1.1 jmcneill {
126 1.1 jmcneill struct clk *clk = NULL;
127 1.1 jmcneill const char *p;
128 1.1 jmcneill u_int index;
129 1.1 jmcneill int len, resid;
130 1.1 jmcneill
131 1.3 jmcneill p = fdtbus_get_prop(phandle, prop, &len);
132 1.3 jmcneill if (p == NULL)
133 1.1 jmcneill return NULL;
134 1.1 jmcneill
135 1.1 jmcneill for (index = 0, resid = len; resid > 0; index++) {
136 1.1 jmcneill if (strcmp(p, clkname) == 0) {
137 1.1 jmcneill clk = fdtbus_clock_get_index(phandle, index);
138 1.1 jmcneill break;
139 1.1 jmcneill }
140 1.1 jmcneill resid -= strlen(p);
141 1.1 jmcneill p += strlen(p) + 1;
142 1.1 jmcneill }
143 1.1 jmcneill
144 1.3 jmcneill return clk;
145 1.3 jmcneill }
146 1.1 jmcneill
147 1.3 jmcneill static u_int
148 1.3 jmcneill fdtbus_clock_count_prop(int phandle, const char *prop)
149 1.3 jmcneill {
150 1.3 jmcneill u_int n, clock_cells;
151 1.3 jmcneill int len, resid;
152 1.3 jmcneill
153 1.3 jmcneill const u_int *p = fdtbus_get_prop(phandle, prop, &len);
154 1.3 jmcneill if (p == NULL)
155 1.3 jmcneill return 0;
156 1.3 jmcneill
157 1.3 jmcneill for (n = 0, resid = len; resid > 0; n++) {
158 1.3 jmcneill const int cc_phandle =
159 1.3 jmcneill fdtbus_get_phandle_from_native(be32toh(p[0]));
160 1.3 jmcneill if (of_getprop_uint32(cc_phandle, "#clock-cells", &clock_cells))
161 1.3 jmcneill break;
162 1.3 jmcneill resid -= (clock_cells + 1) * 4;
163 1.3 jmcneill p += clock_cells + 1;
164 1.3 jmcneill }
165 1.3 jmcneill
166 1.3 jmcneill return n;
167 1.3 jmcneill }
168 1.3 jmcneill
169 1.3 jmcneill struct clk *
170 1.3 jmcneill fdtbus_clock_get(int phandle, const char *clkname)
171 1.3 jmcneill {
172 1.3 jmcneill return fdtbus_clock_get_prop(phandle, clkname, "clock-names");
173 1.1 jmcneill }
174 1.2 jmcneill
175 1.2 jmcneill /*
176 1.2 jmcneill * Search the DT for a clock by "clock-output-names" property.
177 1.2 jmcneill *
178 1.2 jmcneill * This should only be used by clk backends. Not for use by ordinary
179 1.2 jmcneill * clock consumers!
180 1.2 jmcneill */
181 1.2 jmcneill struct clk *
182 1.2 jmcneill fdtbus_clock_byname(const char *clkname)
183 1.2 jmcneill {
184 1.2 jmcneill struct fdtbus_clock_controller *cc;
185 1.2 jmcneill u_int len, resid, index, clock_cells;
186 1.2 jmcneill const char *p;
187 1.2 jmcneill
188 1.5 jmcneill LIST_FOREACH(cc, &fdtbus_clock_controllers, cc_next) {
189 1.2 jmcneill if (!of_hasprop(cc->cc_phandle, "clock-output-names"))
190 1.2 jmcneill continue;
191 1.2 jmcneill p = fdtbus_get_prop(cc->cc_phandle, "clock-output-names", &len);
192 1.2 jmcneill for (index = 0, resid = len; resid > 0; index++) {
193 1.2 jmcneill if (strcmp(p, clkname) == 0) {
194 1.2 jmcneill if (of_getprop_uint32(cc->cc_phandle, "#clock-cells", &clock_cells))
195 1.2 jmcneill break;
196 1.2 jmcneill const u_int index_raw = htobe32(index);
197 1.2 jmcneill return cc->cc_funcs->decode(cc->cc_dev,
198 1.2 jmcneill clock_cells > 0 ? &index_raw : NULL,
199 1.2 jmcneill clock_cells > 0 ? 4 : 0);
200 1.2 jmcneill }
201 1.2 jmcneill resid -= strlen(p) + 1;
202 1.2 jmcneill p += strlen(p) + 1;
203 1.2 jmcneill }
204 1.2 jmcneill }
205 1.2 jmcneill
206 1.2 jmcneill return NULL;
207 1.2 jmcneill }
208 1.3 jmcneill
209 1.3 jmcneill /*
210 1.3 jmcneill * Apply assigned clock parents and rates.
211 1.3 jmcneill *
212 1.3 jmcneill * This is automatically called by fdtbus_register_clock_controller, so clock
213 1.3 jmcneill * drivers likely don't need to call this directly.
214 1.3 jmcneill */
215 1.3 jmcneill void
216 1.3 jmcneill fdtbus_clock_assign(int phandle)
217 1.3 jmcneill {
218 1.3 jmcneill u_int index, rates_len;
219 1.3 jmcneill struct clk *clk, *clk_parent;
220 1.3 jmcneill int error;
221 1.3 jmcneill
222 1.3 jmcneill const u_int *rates = fdtbus_get_prop(phandle, "assigned-clock-rates", &rates_len);
223 1.3 jmcneill if (rates == NULL)
224 1.3 jmcneill rates_len = 0;
225 1.3 jmcneill
226 1.3 jmcneill const u_int nclocks = fdtbus_clock_count_prop(phandle, "assigned-clocks");
227 1.4 jmcneill const u_int nparents = fdtbus_clock_count_prop(phandle, "assigned-clock-parents");
228 1.4 jmcneill const u_int nrates = rates_len / sizeof(*rates);
229 1.3 jmcneill
230 1.3 jmcneill for (index = 0; index < nclocks; index++) {
231 1.3 jmcneill clk = fdtbus_clock_get_index_prop(phandle, index, "assigned-clocks");
232 1.3 jmcneill if (clk == NULL) {
233 1.3 jmcneill aprint_debug("clk: assigned clock (%u) not found, skipping...\n", index);
234 1.3 jmcneill continue;
235 1.3 jmcneill }
236 1.3 jmcneill
237 1.4 jmcneill if (index < nparents) {
238 1.4 jmcneill clk_parent = fdtbus_clock_get_index_prop(phandle, index, "assigned-clock-parents");
239 1.4 jmcneill if (clk_parent != NULL) {
240 1.4 jmcneill error = clk_set_parent(clk, clk_parent);
241 1.4 jmcneill if (error != 0) {
242 1.4 jmcneill aprint_error("clk: failed to set %s parent to %s, error %d\n",
243 1.4 jmcneill clk->name, clk_parent->name, error);
244 1.4 jmcneill }
245 1.4 jmcneill } else {
246 1.4 jmcneill aprint_debug("clk: failed to set %s parent (not found)\n", clk->name);
247 1.3 jmcneill }
248 1.3 jmcneill }
249 1.3 jmcneill
250 1.4 jmcneill if (index < nrates) {
251 1.4 jmcneill const u_int rate = be32toh(rates[index]);
252 1.3 jmcneill if (rate != 0) {
253 1.3 jmcneill error = clk_set_rate(clk, rate);
254 1.3 jmcneill if (error != 0)
255 1.3 jmcneill aprint_error("clk: failed to set %s rate to %u Hz, error %d\n",
256 1.3 jmcneill clk->name, rate, error);
257 1.3 jmcneill }
258 1.3 jmcneill }
259 1.3 jmcneill }
260 1.3 jmcneill }
261