1 /* $NetBSD: fdt_clock.c,v 1.1 2015/12/22 21:42:11 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: fdt_clock.c,v 1.1 2015/12/22 21:42:11 jmcneill Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/kmem.h> 35 36 #include <libfdt.h> 37 #include <dev/fdt/fdtvar.h> 38 39 struct fdtbus_clock_controller { 40 device_t cc_dev; 41 int cc_phandle; 42 const struct fdtbus_clock_controller_func *cc_funcs; 43 44 struct fdtbus_clock_controller *cc_next; 45 }; 46 47 static struct fdtbus_clock_controller *fdtbus_cc = NULL; 48 49 int 50 fdtbus_register_clock_controller(device_t dev, int phandle, 51 const struct fdtbus_clock_controller_func *funcs) 52 { 53 struct fdtbus_clock_controller *cc; 54 55 cc = kmem_alloc(sizeof(*cc), KM_SLEEP); 56 cc->cc_dev = dev; 57 cc->cc_phandle = phandle; 58 cc->cc_funcs = funcs; 59 60 cc->cc_next = fdtbus_cc; 61 fdtbus_cc = cc; 62 63 return 0; 64 } 65 66 static struct fdtbus_clock_controller * 67 fdtbus_get_clock_controller(int phandle) 68 { 69 struct fdtbus_clock_controller *cc; 70 71 for (cc = fdtbus_cc; cc; cc = cc->cc_next) { 72 if (cc->cc_phandle == phandle) { 73 return cc; 74 } 75 } 76 77 return NULL; 78 } 79 80 struct clk * 81 fdtbus_clock_get_index(int phandle, u_int index) 82 { 83 struct fdtbus_clock_controller *cc; 84 struct clk *clk = NULL; 85 uint32_t *clocks = NULL; 86 uint32_t *p; 87 u_int n, clock_cells; 88 int len, resid; 89 90 len = OF_getproplen(phandle, "clocks"); 91 if (len <= 0) 92 return NULL; 93 94 clocks = kmem_alloc(len, KM_SLEEP); 95 if (OF_getprop(phandle, "clocks", clocks, len) != len) { 96 kmem_free(clocks, len); 97 return NULL; 98 } 99 100 p = clocks; 101 for (n = 0, resid = len; resid > 0; n++) { 102 const int cc_phandle = 103 fdtbus_get_phandle_from_native(be32toh(p[0])); 104 if (of_getprop_uint32(cc_phandle, "#clock-cells", &clock_cells)) 105 break; 106 if (n == index) { 107 cc = fdtbus_get_clock_controller(cc_phandle); 108 if (cc == NULL) 109 goto done; 110 clk = cc->cc_funcs->decode(cc->cc_dev, 111 clock_cells > 0 ? &p[1] : NULL, clock_cells * 4); 112 break; 113 } 114 resid -= (clock_cells + 1) * 4; 115 p += clock_cells + 1; 116 } 117 118 done: 119 if (clocks) 120 kmem_free(clocks, len); 121 122 return clk; 123 } 124 125 struct clk * 126 fdtbus_clock_get(int phandle, const char *clkname) 127 { 128 struct clk *clk = NULL; 129 char *clock_names = NULL; 130 const char *p; 131 u_int index; 132 int len, resid; 133 134 len = OF_getproplen(phandle, "clock-names"); 135 if (len <= 0) 136 return NULL; 137 138 clock_names = kmem_alloc(len, KM_SLEEP); 139 if (OF_getprop(phandle, "clock-names", clock_names, len) != len) { 140 kmem_free(clock_names, len); 141 return NULL; 142 } 143 144 p = clock_names; 145 for (index = 0, resid = len; resid > 0; index++) { 146 if (strcmp(p, clkname) == 0) { 147 clk = fdtbus_clock_get_index(phandle, index); 148 break; 149 } 150 resid -= strlen(p); 151 p += strlen(p) + 1; 152 } 153 154 if (clock_names) 155 kmem_free(clock_names, len); 156 157 return clk; 158 } 159