Home | History | Annotate | Line # | Download | only in fdt
fdt_clock.c revision 1.1.20.2
      1  1.1.20.2  pgoyette /* $NetBSD: fdt_clock.c,v 1.1.20.2 2018/07/28 04:37:44 pgoyette 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.1.20.2  pgoyette __KERNEL_RCSID(0, "$NetBSD: fdt_clock.c,v 1.1.20.2 2018/07/28 04:37:44 pgoyette 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.1.20.2  pgoyette #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.1.20.1  pgoyette #include <dev/clk/clk_backend.h>
     41  1.1.20.1  pgoyette 
     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.1.20.2  pgoyette 	LIST_ENTRY(fdtbus_clock_controller) cc_next;
     48       1.1  jmcneill };
     49       1.1  jmcneill 
     50  1.1.20.2  pgoyette static LIST_HEAD(, fdtbus_clock_controller) fdtbus_clock_controllers =
     51  1.1.20.2  pgoyette     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.1.20.2  pgoyette 	LIST_INSERT_HEAD(&fdtbus_clock_controllers, cc, cc_next);
     65       1.1  jmcneill 
     66  1.1.20.1  pgoyette 	fdtbus_clock_assign(phandle);
     67  1.1.20.1  pgoyette 
     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.1.20.2  pgoyette 	LIST_FOREACH(cc, &fdtbus_clock_controllers, cc_next) {
     77  1.1.20.2  pgoyette 		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.1.20.1  pgoyette static struct clk *
     85  1.1.20.1  pgoyette 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.1.20.1  pgoyette 	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.1.20.1  pgoyette 	p = fdtbus_get_prop(phandle, prop, &len);
     94  1.1.20.1  pgoyette 	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.1.20.1  pgoyette 				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.1.20.1  pgoyette fdtbus_clock_get_index(int phandle, u_int index)
    119  1.1.20.1  pgoyette {
    120  1.1.20.1  pgoyette 	return fdtbus_clock_get_index_prop(phandle, index, "clocks");
    121  1.1.20.1  pgoyette }
    122  1.1.20.1  pgoyette 
    123  1.1.20.1  pgoyette static struct clk *
    124  1.1.20.1  pgoyette 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.1.20.1  pgoyette 	p = fdtbus_get_prop(phandle, prop, &len);
    132  1.1.20.1  pgoyette 	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.1  jmcneill 	return clk;
    145       1.1  jmcneill }
    146  1.1.20.1  pgoyette 
    147  1.1.20.1  pgoyette static u_int
    148  1.1.20.1  pgoyette fdtbus_clock_count_prop(int phandle, const char *prop)
    149  1.1.20.1  pgoyette {
    150  1.1.20.1  pgoyette 	u_int n, clock_cells;
    151  1.1.20.1  pgoyette 	int len, resid;
    152  1.1.20.1  pgoyette 
    153  1.1.20.1  pgoyette 	const u_int *p = fdtbus_get_prop(phandle, prop, &len);
    154  1.1.20.1  pgoyette 	if (p == NULL)
    155  1.1.20.1  pgoyette 		return 0;
    156  1.1.20.1  pgoyette 
    157  1.1.20.1  pgoyette 	for (n = 0, resid = len; resid > 0; n++) {
    158  1.1.20.1  pgoyette 		const int cc_phandle =
    159  1.1.20.1  pgoyette 		    fdtbus_get_phandle_from_native(be32toh(p[0]));
    160  1.1.20.1  pgoyette 		if (of_getprop_uint32(cc_phandle, "#clock-cells", &clock_cells))
    161  1.1.20.1  pgoyette 			break;
    162  1.1.20.1  pgoyette 		resid -= (clock_cells + 1) * 4;
    163  1.1.20.1  pgoyette 		p += clock_cells + 1;
    164  1.1.20.1  pgoyette 	}
    165  1.1.20.1  pgoyette 
    166  1.1.20.1  pgoyette 	return n;
    167  1.1.20.1  pgoyette }
    168  1.1.20.1  pgoyette 
    169  1.1.20.1  pgoyette struct clk *
    170  1.1.20.1  pgoyette fdtbus_clock_get(int phandle, const char *clkname)
    171  1.1.20.1  pgoyette {
    172  1.1.20.1  pgoyette 	return fdtbus_clock_get_prop(phandle, clkname, "clock-names");
    173  1.1.20.1  pgoyette }
    174  1.1.20.1  pgoyette 
    175  1.1.20.1  pgoyette /*
    176  1.1.20.1  pgoyette  * Search the DT for a clock by "clock-output-names" property.
    177  1.1.20.1  pgoyette  *
    178  1.1.20.1  pgoyette  * This should only be used by clk backends. Not for use by ordinary
    179  1.1.20.1  pgoyette  * clock consumers!
    180  1.1.20.1  pgoyette  */
    181  1.1.20.1  pgoyette struct clk *
    182  1.1.20.1  pgoyette fdtbus_clock_byname(const char *clkname)
    183  1.1.20.1  pgoyette {
    184  1.1.20.1  pgoyette 	struct fdtbus_clock_controller *cc;
    185  1.1.20.1  pgoyette 	u_int len, resid, index, clock_cells;
    186  1.1.20.1  pgoyette 	const char *p;
    187  1.1.20.1  pgoyette 
    188  1.1.20.2  pgoyette 	LIST_FOREACH(cc, &fdtbus_clock_controllers, cc_next) {
    189  1.1.20.1  pgoyette 		if (!of_hasprop(cc->cc_phandle, "clock-output-names"))
    190  1.1.20.1  pgoyette 			continue;
    191  1.1.20.1  pgoyette 		p = fdtbus_get_prop(cc->cc_phandle, "clock-output-names", &len);
    192  1.1.20.1  pgoyette 		for (index = 0, resid = len; resid > 0; index++) {
    193  1.1.20.1  pgoyette 			if (strcmp(p, clkname) == 0) {
    194  1.1.20.1  pgoyette 				if (of_getprop_uint32(cc->cc_phandle, "#clock-cells", &clock_cells))
    195  1.1.20.1  pgoyette 					break;
    196  1.1.20.1  pgoyette 				const u_int index_raw = htobe32(index);
    197  1.1.20.1  pgoyette 				return cc->cc_funcs->decode(cc->cc_dev,
    198  1.1.20.1  pgoyette 				    clock_cells > 0 ? &index_raw : NULL,
    199  1.1.20.1  pgoyette 				    clock_cells > 0 ? 4 : 0);
    200  1.1.20.1  pgoyette 			}
    201  1.1.20.1  pgoyette 			resid -= strlen(p) + 1;
    202  1.1.20.1  pgoyette 			p += strlen(p) + 1;
    203  1.1.20.1  pgoyette 		}
    204  1.1.20.1  pgoyette 	}
    205  1.1.20.1  pgoyette 
    206  1.1.20.1  pgoyette 	return NULL;
    207  1.1.20.1  pgoyette }
    208  1.1.20.1  pgoyette 
    209  1.1.20.1  pgoyette /*
    210  1.1.20.1  pgoyette  * Apply assigned clock parents and rates.
    211  1.1.20.1  pgoyette  *
    212  1.1.20.1  pgoyette  * This is automatically called by fdtbus_register_clock_controller, so clock
    213  1.1.20.1  pgoyette  * drivers likely don't need to call this directly.
    214  1.1.20.1  pgoyette  */
    215  1.1.20.1  pgoyette void
    216  1.1.20.1  pgoyette fdtbus_clock_assign(int phandle)
    217  1.1.20.1  pgoyette {
    218  1.1.20.1  pgoyette 	u_int index, rates_len;
    219  1.1.20.1  pgoyette 	struct clk *clk, *clk_parent;
    220  1.1.20.1  pgoyette 	int error;
    221  1.1.20.1  pgoyette 
    222  1.1.20.1  pgoyette 	const u_int *rates = fdtbus_get_prop(phandle, "assigned-clock-rates", &rates_len);
    223  1.1.20.1  pgoyette 	if (rates == NULL)
    224  1.1.20.1  pgoyette 		rates_len = 0;
    225  1.1.20.1  pgoyette 
    226  1.1.20.1  pgoyette 	const u_int nclocks = fdtbus_clock_count_prop(phandle, "assigned-clocks");
    227  1.1.20.1  pgoyette 	const u_int nparents = fdtbus_clock_count_prop(phandle, "assigned-clock-parents");
    228  1.1.20.1  pgoyette 	const u_int nrates = rates_len / sizeof(*rates);
    229  1.1.20.1  pgoyette 
    230  1.1.20.1  pgoyette 	for (index = 0; index < nclocks; index++) {
    231  1.1.20.1  pgoyette 		clk = fdtbus_clock_get_index_prop(phandle, index, "assigned-clocks");
    232  1.1.20.1  pgoyette 		if (clk == NULL) {
    233  1.1.20.1  pgoyette 			aprint_debug("clk: assigned clock (%u) not found, skipping...\n", index);
    234  1.1.20.1  pgoyette 			continue;
    235  1.1.20.1  pgoyette 		}
    236  1.1.20.1  pgoyette 
    237  1.1.20.1  pgoyette 		if (index < nparents) {
    238  1.1.20.1  pgoyette 			clk_parent = fdtbus_clock_get_index_prop(phandle, index, "assigned-clock-parents");
    239  1.1.20.1  pgoyette 			if (clk_parent != NULL) {
    240  1.1.20.1  pgoyette 				error = clk_set_parent(clk, clk_parent);
    241  1.1.20.1  pgoyette 				if (error != 0) {
    242  1.1.20.1  pgoyette 					aprint_error("clk: failed to set %s parent to %s, error %d\n",
    243  1.1.20.1  pgoyette 					    clk->name, clk_parent->name, error);
    244  1.1.20.1  pgoyette 				}
    245  1.1.20.1  pgoyette 			} else {
    246  1.1.20.1  pgoyette 				aprint_debug("clk: failed to set %s parent (not found)\n", clk->name);
    247  1.1.20.1  pgoyette 			}
    248  1.1.20.1  pgoyette 		}
    249  1.1.20.1  pgoyette 
    250  1.1.20.1  pgoyette 		if (index < nrates) {
    251  1.1.20.1  pgoyette 			const u_int rate = be32toh(rates[index]);
    252  1.1.20.1  pgoyette 			if (rate != 0) {
    253  1.1.20.1  pgoyette 				error = clk_set_rate(clk, rate);
    254  1.1.20.1  pgoyette 				if (error != 0)
    255  1.1.20.1  pgoyette 					aprint_error("clk: failed to set %s rate to %u Hz, error %d\n",
    256  1.1.20.1  pgoyette 					    clk->name, rate, error);
    257  1.1.20.1  pgoyette 			}
    258  1.1.20.1  pgoyette 		}
    259  1.1.20.1  pgoyette 	}
    260  1.1.20.1  pgoyette }
    261