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