1 /*- 2 * Copyright (c) 2017 Kyle Evans <[email protected]> 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 27 /* 28 * This is a generic syscon driver, whose purpose is to provide access to 29 * various unrelated bits packed in a single register space. It is usually used 30 * as a fallback to more specific driver, but works well enough for simple 31 * access. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 #include "opt_platform.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/bus.h> 41 #include <sys/kernel.h> 42 #include <sys/kobj.h> 43 #include <sys/lock.h> 44 #include <sys/module.h> 45 #include <sys/rman.h> 46 #include <sys/sx.h> 47 #include <sys/queue.h> 48 49 #include <machine/bus.h> 50 51 #ifdef FDT 52 #include <dev/ofw/ofw_bus.h> 53 #include <dev/ofw/ofw_bus_subr.h> 54 #endif 55 56 #include "syscon_if.h" 57 #include "syscon.h" 58 59 /* 60 * Syscon interface details 61 */ 62 typedef TAILQ_HEAD(syscon_list, syscon) syscon_list_t; 63 64 /* 65 * Declarations 66 */ 67 static int syscon_method_init(struct syscon *syscon); 68 static int syscon_method_uninit(struct syscon *syscon); 69 70 MALLOC_DEFINE(M_SYSCON, "syscon", "Syscon driver"); 71 72 static syscon_list_t syscon_list = TAILQ_HEAD_INITIALIZER(syscon_list); 73 static struct sx syscon_topo_lock; 74 SX_SYSINIT(syscon_topology, &syscon_topo_lock, "Syscon topology lock"); 75 76 /* 77 * Syscon methods. 78 */ 79 static syscon_method_t syscon_methods[] = { 80 SYSCONMETHOD(syscon_init, syscon_method_init), 81 SYSCONMETHOD(syscon_uninit, syscon_method_uninit), 82 83 SYSCONMETHOD_END 84 }; 85 DEFINE_CLASS_0(syscon, syscon_class, syscon_methods, 0); 86 87 #define SYSCON_TOPO_SLOCK() sx_slock(&syscon_topo_lock) 88 #define SYSCON_TOPO_XLOCK() sx_xlock(&syscon_topo_lock) 89 #define SYSCON_TOPO_UNLOCK() sx_unlock(&syscon_topo_lock) 90 #define SYSCON_TOPO_ASSERT() sx_assert(&syscon_topo_lock, SA_LOCKED) 91 #define SYSCON_TOPO_XASSERT() sx_assert(&syscon_topo_lock, SA_XLOCKED) 92 93 /* 94 * Default syscon methods for base class. 95 */ 96 static int 97 syscon_method_init(struct syscon *syscon) 98 { 99 100 return (0); 101 }; 102 103 static int 104 syscon_method_uninit(struct syscon *syscon) 105 { 106 107 return (0); 108 }; 109 110 void * 111 syscon_get_softc(struct syscon *syscon) 112 { 113 114 return (syscon->softc); 115 }; 116 117 /* 118 * Create and initialize syscon object, but do not register it. 119 */ 120 struct syscon * 121 syscon_create(device_t pdev, syscon_class_t syscon_class) 122 { 123 struct syscon *syscon; 124 125 /* Create object and initialize it. */ 126 syscon = malloc(sizeof(struct syscon), M_SYSCON, 127 M_WAITOK | M_ZERO); 128 kobj_init((kobj_t)syscon, (kobj_class_t)syscon_class); 129 130 /* Allocate softc if required. */ 131 if (syscon_class->size > 0) 132 syscon->softc = malloc(syscon_class->size, M_SYSCON, 133 M_WAITOK | M_ZERO); 134 135 /* Rest of init. */ 136 syscon->pdev = pdev; 137 return (syscon); 138 } 139 140 /* Register syscon object. */ 141 struct syscon * 142 syscon_register(struct syscon *syscon) 143 { 144 int rv; 145 146 #ifdef FDT 147 if (syscon->ofw_node <= 0) 148 syscon->ofw_node = ofw_bus_get_node(syscon->pdev); 149 if (syscon->ofw_node <= 0) 150 return (NULL); 151 #endif 152 153 rv = SYSCON_INIT(syscon); 154 if (rv != 0) { 155 printf("SYSCON_INIT failed: %d\n", rv); 156 return (NULL); 157 } 158 159 #ifdef FDT 160 OF_device_register_xref(OF_xref_from_node(syscon->ofw_node), 161 syscon->pdev); 162 #endif 163 SYSCON_TOPO_XLOCK(); 164 TAILQ_INSERT_TAIL(&syscon_list, syscon, syscon_link); 165 SYSCON_TOPO_UNLOCK(); 166 return (syscon); 167 } 168 169 int 170 syscon_unregister(struct syscon *syscon) 171 { 172 173 SYSCON_TOPO_XLOCK(); 174 TAILQ_REMOVE(&syscon_list, syscon, syscon_link); 175 SYSCON_TOPO_UNLOCK(); 176 #ifdef FDT 177 OF_device_register_xref(OF_xref_from_node(syscon->ofw_node), NULL); 178 #endif 179 return (SYSCON_UNINIT(syscon)); 180 } 181 182 /** 183 * Provider methods 184 */ 185 #ifdef FDT 186 static struct syscon * 187 syscon_find_by_ofw_node(phandle_t node) 188 { 189 struct syscon *entry; 190 191 SYSCON_TOPO_ASSERT(); 192 193 TAILQ_FOREACH(entry, &syscon_list, syscon_link) { 194 if (entry->ofw_node == node) 195 return (entry); 196 } 197 198 return (NULL); 199 } 200 201 struct syscon * 202 syscon_create_ofw_node(device_t pdev, syscon_class_t syscon_class, 203 phandle_t node) 204 { 205 struct syscon *syscon; 206 207 syscon = syscon_create(pdev, syscon_class); 208 if (syscon == NULL) 209 return (NULL); 210 syscon->ofw_node = node; 211 if (syscon_register(syscon) == NULL) 212 return (NULL); 213 return (syscon); 214 } 215 216 phandle_t 217 syscon_get_ofw_node(struct syscon *syscon) 218 { 219 220 return (syscon->ofw_node); 221 } 222 223 int 224 syscon_get_by_ofw_property(device_t cdev, phandle_t cnode, char *name, 225 struct syscon **syscon) 226 { 227 pcell_t *cells; 228 int ncells; 229 230 if (cnode <= 0) 231 cnode = ofw_bus_get_node(cdev); 232 if (cnode <= 0) { 233 device_printf(cdev, 234 "%s called on not ofw based device\n", __func__); 235 return (ENXIO); 236 } 237 ncells = OF_getencprop_alloc(cnode, name, sizeof(pcell_t), 238 (void **)&cells); 239 if (ncells < 1) 240 return (ENXIO); 241 242 /* Translate to syscon node. */ 243 SYSCON_TOPO_SLOCK(); 244 *syscon = syscon_find_by_ofw_node(OF_node_from_xref(cells[0])); 245 if (*syscon == NULL) { 246 SYSCON_TOPO_UNLOCK(); 247 device_printf(cdev, "Failed to find syscon node\n"); 248 OF_prop_free(cells); 249 return (ENODEV); 250 } 251 SYSCON_TOPO_UNLOCK(); 252 OF_prop_free(cells); 253 return (0); 254 } 255 #endif 256