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