1#- 2# Copyright (c) 2015 Landon Fuller <landon@landonf.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 ``AS IS'' AND ANY EXPRESS OR 15# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17# IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 18# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 21# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 23# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24# 25# $FreeBSD$ 26 27#include <sys/types.h> 28#include <sys/bus.h> 29#include <sys/rman.h> 30 31#include <dev/bhnd/bhnd_types.h> 32 33INTERFACE bhnd_bus; 34 35# 36# bhnd(4) bus interface 37# 38 39HEADER { 40 /* forward declarations */ 41 struct bhnd_core_info; 42 struct bhnd_chipid; 43 struct bhnd_resource; 44 struct bhnd_bus_ctx; 45} 46 47CODE { 48 #include <sys/systm.h> 49 50 #include <dev/bhnd/bhndvar.h> 51 52 static struct bhnd_chipid * 53 bhnd_bus_null_get_chipid(device_t dev, device_t child) 54 { 55 panic("bhnd_get_chipid unimplemented"); 56 } 57 58 static int 59 bhnd_bus_null_get_port_rid(device_t dev, device_t child, 60 bhnd_port_type port_type, u_int port, u_int region) 61 { 62 return (-1); 63 } 64 65 static int 66 bhnd_bus_null_decode_port_rid(device_t dev, device_t child, int type, 67 int rid, bhnd_port_type *port_type, u_int *port, u_int *region) 68 { 69 return (ENOENT); 70 } 71 72 static int 73 bhnd_bus_null_get_region_addr(device_t dev, device_t child, 74 bhnd_port_type type, u_int port, u_int region, bhnd_addr_t *addr, 75 bhnd_size_t *size) 76 { 77 return (ENOENT); 78 } 79 80 static int 81 bhnd_bus_null_read_nvram_var(device_t dev, device_t child, 82 const char *name, void *buf, size_t *size) 83 { 84 return (ENOENT); 85 } 86} 87 88/** 89 * Returns true if @p child is serving as a host bridge for the bhnd 90 * bus. 91 * 92 * The default implementation will walk the parent device tree until 93 * the root node is hit, returning false. 94 * 95 * @param dev The device whose child is being examined. 96 * @param child The child device. 97 */ 98METHOD bool is_hostb_device { 99 device_t dev; 100 device_t child; 101} DEFAULT bhnd_generic_is_hostb_device; 102 103/** 104 * Return true if the hardware components required by @p child are unpopulated 105 * or otherwise unusable. 106 * 107 * In some cases, enumerated devices may have pins that are left floating, or 108 * the hardware may otherwise be non-functional; this method allows a parent 109 * device to explicitly specify if a successfully enumerated @p child should 110 * be disabled. 111 * 112 * @param dev The device whose child is being examined. 113 * @param child The child device. 114 */ 115METHOD bool is_hw_disabled { 116 device_t dev; 117 device_t child; 118} DEFAULT bhnd_generic_is_hw_disabled; 119 120/** 121 * Return the probe (and attach) order for @p child. 122 * 123 * All devices on the bhnd(4) bus will be probed, attached, or resumed in 124 * ascending order; they will be suspended, shutdown, and detached in 125 * descending order. 126 * 127 * The following device methods will be dispatched in ascending probe order 128 * by the bus: 129 * 130 * - DEVICE_PROBE() 131 * - DEVICE_ATTACH() 132 * - DEVICE_RESUME() 133 * 134 * The following device methods will be dispatched in descending probe order 135 * by the bus: 136 * 137 * - DEVICE_SHUTDOWN() 138 * - DEVICE_DETACH() 139 * - DEVICE_SUSPEND() 140 * 141 * @param dev The device whose child is being examined. 142 * @param child The child device. 143 * 144 * Refer to BHND_PROBE_* and BHND_PROBE_ORDER_* for the standard set of 145 * priorities. 146 */ 147METHOD int get_probe_order { 148 device_t dev; 149 device_t child; 150} DEFAULT bhnd_generic_get_probe_order; 151 152/** 153 * Return the BHND chip identification for the parent bus. 154 * 155 * @param dev The device whose child is being examined. 156 * @param child The child device. 157 */ 158METHOD const struct bhnd_chipid * get_chipid { 159 device_t dev; 160 device_t child; 161} DEFAULT bhnd_bus_null_get_chipid; 162 163/** 164 * Reset the device's hardware core. 165 * 166 * @param dev The parent of @p child. 167 * @param child The device to be reset. 168 * @param flags Device-specific core flags to be supplied on reset. 169 * 170 * @retval 0 success 171 * @retval non-zero error 172 */ 173METHOD int reset_core { 174 device_t dev; 175 device_t child; 176 uint16_t flags; 177} 178 179/** 180 * Suspend a device hardware core. 181 * 182 * @param dev The parent of @p child. 183 * @param child The device to be reset. 184 * 185 * @retval 0 success 186 * @retval non-zero error 187 */ 188METHOD int suspend_core { 189 device_t dev; 190 device_t child; 191} 192 193/** 194 * Allocate a bhnd resource. 195 * 196 * This method's semantics are functionally identical to the bus API of the same 197 * name; refer to BUS_ALLOC_RESOURCE for complete documentation. 198 */ 199METHOD struct bhnd_resource * alloc_resource { 200 device_t dev; 201 device_t child; 202 int type; 203 int *rid; 204 rman_res_t start; 205 rman_res_t end; 206 rman_res_t count; 207 u_int flags; 208} DEFAULT bhnd_generic_alloc_bhnd_resource; 209 210/** 211 * Release a bhnd resource. 212 * 213 * This method's semantics are functionally identical to the bus API of the same 214 * name; refer to BUS_RELEASE_RESOURCE for complete documentation. 215 */ 216METHOD int release_resource { 217 device_t dev; 218 device_t child; 219 int type; 220 int rid; 221 struct bhnd_resource *res; 222} DEFAULT bhnd_generic_release_bhnd_resource; 223 224/** 225 * Activate a bhnd resource. 226 * 227 * This method's semantics are functionally identical to the bus API of the same 228 * name; refer to BUS_ACTIVATE_RESOURCE for complete documentation. 229 */ 230METHOD int activate_resource { 231 device_t dev; 232 device_t child; 233 int type; 234 int rid; 235 struct bhnd_resource *r; 236} DEFAULT bhnd_generic_activate_bhnd_resource; 237 238/** 239 * Deactivate a bhnd resource. 240 * 241 * This method's semantics are functionally identical to the bus API of the same 242 * name; refer to BUS_DEACTIVATE_RESOURCE for complete documentation. 243 */ 244METHOD int deactivate_resource { 245 device_t dev; 246 device_t child; 247 int type; 248 int rid; 249 struct bhnd_resource *r; 250} DEFAULT bhnd_generic_deactivate_bhnd_resource; 251 252/** 253 * Return true if @p region_num is a valid region on @p port_num of 254 * @p type attached to @p child. 255 * 256 * @param dev The device whose child is being examined. 257 * @param child The child device. 258 * @param type The port type being queried. 259 * @param port_num The port number being queried. 260 * @param region_num The region number being queried. 261 */ 262METHOD u_int is_region_valid { 263 device_t dev; 264 device_t child; 265 bhnd_port_type type; 266 u_int port_num; 267 u_int region_num; 268} DEFAULT bhnd_generic_is_region_valid; 269 270/** 271 * Return the number of ports of type @p type attached to @p child. 272 * 273 * @param dev The device whose child is being examined. 274 * @param child The child device. 275 * @param type The port type being queried. 276 */ 277METHOD u_int get_port_count { 278 device_t dev; 279 device_t child; 280 bhnd_port_type type; 281} 282 283/** 284 * Return the number of memory regions mapped to @p child @p port of 285 * type @p type. 286 * 287 * @param dev The device whose child is being examined. 288 * @param child The child device. 289 * @param port The port number being queried. 290 * @param type The port type being queried. 291 */ 292METHOD u_int get_region_count { 293 device_t dev; 294 device_t child; 295 bhnd_port_type type; 296 u_int port; 297} 298 299/** 300 * Return the SYS_RES_MEMORY resource-ID for a port/region pair attached to 301 * @p child. 302 * 303 * @param dev The bus device. 304 * @param child The bhnd child. 305 * @param port_type The port type. 306 * @param port_num The index of the child interconnect port. 307 * @param region_num The index of the port-mapped address region. 308 * 309 * @retval -1 No such port/region found. 310 */ 311METHOD int get_port_rid { 312 device_t dev; 313 device_t child; 314 bhnd_port_type port_type; 315 u_int port_num; 316 u_int region_num; 317} DEFAULT bhnd_bus_null_get_port_rid; 318 319 320/** 321 * Decode a port / region pair on @p child defined by @p type and @p rid. 322 * 323 * @param dev The bus device. 324 * @param child The bhnd child. 325 * @param type The resource type. 326 * @param rid The resource ID. 327 * @param[out] port_type The port's type. 328 * @param[out] port The port identifier. 329 * @param[out] region The identifier of the memory region on @p port. 330 * 331 * @retval 0 success 332 * @retval non-zero No matching type/rid found. 333 */ 334METHOD int decode_port_rid { 335 device_t dev; 336 device_t child; 337 int type; 338 int rid; 339 bhnd_port_type *port_type; 340 u_int *port; 341 u_int *region; 342} DEFAULT bhnd_bus_null_decode_port_rid; 343 344/** 345 * Get the address and size of @p region on @p port. 346 * 347 * @param dev The bus device. 348 * @param child The bhnd child. 349 * @param port_type The port type. 350 * @param port The port identifier. 351 * @param region The identifier of the memory region on @p port. 352 * @param[out] region_addr The region's base address. 353 * @param[out] region_size The region's size. 354 * 355 * @retval 0 success 356 * @retval non-zero No matching port/region found. 357 */ 358METHOD int get_region_addr { 359 device_t dev; 360 device_t child; 361 bhnd_port_type port_type; 362 u_int port; 363 u_int region; 364 bhnd_addr_t *region_addr; 365 bhnd_size_t *region_size; 366} DEFAULT bhnd_bus_null_get_region_addr; 367 368/** 369 * Read an NVRAM variable. 370 * 371 * It is the responsibility of the bus to delegate this request to 372 * the appropriate NVRAM child device, or to a parent bus implementation. 373 * 374 * @param dev The bus device. 375 * @param child The requesting device. 376 * @param name The NVRAM variable name. 377 * @param[out] buf On success, the requested value will be written 378 * to this buffer. This argment may be NULL if 379 * the value is not desired. 380 * @param[in,out] size The capacity of @p buf. On success, will be set 381 * to the actual size of the requested value. 382 * 383 * @retval 0 success 384 * @retval ENOENT The requested variable was not found. 385 * @retval ENOMEM If @p buf is non-NULL and a buffer of @p size is too 386 * small to hold the requested value. 387 * @retval non-zero If reading @p name otherwise fails, a regular unix 388 * error code will be returned. 389 */ 390METHOD int read_nvram_var { 391 device_t dev; 392 device_t child; 393 const char *name; 394 void *buf; 395 size_t *size; 396} DEFAULT bhnd_bus_null_read_nvram_var; 397 398 399/** An implementation of bus_read_1() compatible with bhnd_resource */ 400METHOD uint8_t read_1 { 401 device_t dev; 402 device_t child; 403 struct bhnd_resource *r; 404 bus_size_t offset; 405} 406 407/** An implementation of bus_read_2() compatible with bhnd_resource */ 408METHOD uint16_t read_2 { 409 device_t dev; 410 device_t child; 411 struct bhnd_resource *r; 412 bus_size_t offset; 413} 414 415/** An implementation of bus_read_4() compatible with bhnd_resource */ 416METHOD uint32_t read_4 { 417 device_t dev; 418 device_t child; 419 struct bhnd_resource *r; 420 bus_size_t offset; 421} 422 423/** An implementation of bus_write_1() compatible with bhnd_resource */ 424METHOD void write_1 { 425 device_t dev; 426 device_t child; 427 struct bhnd_resource *r; 428 bus_size_t offset; 429 uint8_t value; 430} 431 432/** An implementation of bus_write_2() compatible with bhnd_resource */ 433METHOD void write_2 { 434 device_t dev; 435 device_t child; 436 struct bhnd_resource *r; 437 bus_size_t offset; 438 uint16_t value; 439} 440 441/** An implementation of bus_write_4() compatible with bhnd_resource */ 442METHOD void write_4 { 443 device_t dev; 444 device_t child; 445 struct bhnd_resource *r; 446 bus_size_t offset; 447 uint32_t value; 448} 449 450/** An implementation of bus_barrier() compatible with bhnd_resource */ 451METHOD void barrier { 452 device_t dev; 453 device_t child; 454 struct bhnd_resource *r; 455 bus_size_t offset; 456 bus_size_t length; 457 int flags; 458} 459