1 /* 2 * Copyright (c) 2013 Google, Inc 3 * 4 * (C) Copyright 2012 5 * Pavel Herrmann <morpheus.ibis@gmail.com> 6 * Marek Vasut <marex@denx.de> 7 * 8 * SPDX-License-Identifier: GPL-2.0+ 9 */ 10 11 #ifndef _DM_DEVICE_H 12 #define _DM_DEVICE_H 13 14 #include <dm/uclass-id.h> 15 #include <fdtdec.h> 16 #include <linker_lists.h> 17 #include <linux/list.h> 18 19 struct driver_info; 20 21 /* Driver is active (probed). Cleared when it is removed */ 22 #define DM_FLAG_ACTIVATED (1 << 0) 23 24 /* DM is responsible for allocating and freeing platdata */ 25 #define DM_FLAG_ALLOC_PDATA (1 << 1) 26 27 /* DM should init this device prior to relocation */ 28 #define DM_FLAG_PRE_RELOC (1 << 2) 29 30 /* DM is responsible for allocating and freeing parent_platdata */ 31 #define DM_FLAG_ALLOC_PARENT_PDATA (1 << 3) 32 33 /* DM is responsible for allocating and freeing uclass_platdata */ 34 #define DM_FLAG_ALLOC_UCLASS_PDATA (1 << 4) 35 36 /* Allocate driver private data on a DMA boundary */ 37 #define DM_FLAG_ALLOC_PRIV_DMA (1 << 5) 38 39 /* Device is bound */ 40 #define DM_FLAG_BOUND (1 << 6) 41 42 /** 43 * struct udevice - An instance of a driver 44 * 45 * This holds information about a device, which is a driver bound to a 46 * particular port or peripheral (essentially a driver instance). 47 * 48 * A device will come into existence through a 'bind' call, either due to 49 * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node 50 * in the device tree (in which case of_offset is >= 0). In the latter case 51 * we translate the device tree information into platdata in a function 52 * implemented by the driver ofdata_to_platdata method (called just before the 53 * probe method if the device has a device tree node. 54 * 55 * All three of platdata, priv and uclass_priv can be allocated by the 56 * driver, or you can use the auto_alloc_size members of struct driver and 57 * struct uclass_driver to have driver model do this automatically. 58 * 59 * @driver: The driver used by this device 60 * @name: Name of device, typically the FDT node name 61 * @platdata: Configuration data for this device 62 * @parent_platdata: The parent bus's configuration data for this device 63 * @uclass_platdata: The uclass's configuration data for this device 64 * @of_offset: Device tree node offset for this device (- for none) 65 * @driver_data: Driver data word for the entry that matched this device with 66 * its driver 67 * @parent: Parent of this device, or NULL for the top level device 68 * @priv: Private data for this device 69 * @uclass: Pointer to uclass for this device 70 * @uclass_priv: The uclass's private data for this device 71 * @parent_priv: The parent's private data for this device 72 * @uclass_node: Used by uclass to link its devices 73 * @child_head: List of children of this device 74 * @sibling_node: Next device in list of all devices 75 * @flags: Flags for this device DM_FLAG_... 76 * @req_seq: Requested sequence number for this device (-1 = any) 77 * @seq: Allocated sequence number for this device (-1 = none). This is set up 78 * when the device is probed and will be unique within the device's uclass. 79 */ 80 struct udevice { 81 const struct driver *driver; 82 const char *name; 83 void *platdata; 84 void *parent_platdata; 85 void *uclass_platdata; 86 int of_offset; 87 ulong driver_data; 88 struct udevice *parent; 89 void *priv; 90 struct uclass *uclass; 91 void *uclass_priv; 92 void *parent_priv; 93 struct list_head uclass_node; 94 struct list_head child_head; 95 struct list_head sibling_node; 96 uint32_t flags; 97 int req_seq; 98 int seq; 99 struct list_head devres_head; 100 }; 101 102 /* Maximum sequence number supported */ 103 #define DM_MAX_SEQ 999 104 105 /* Returns the operations for a device */ 106 #define device_get_ops(dev) (dev->driver->ops) 107 108 /* Returns non-zero if the device is active (probed and not removed) */ 109 #define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED) 110 111 /** 112 * struct udevice_id - Lists the compatible strings supported by a driver 113 * @compatible: Compatible string 114 * @data: Data for this compatible string 115 */ 116 struct udevice_id { 117 const char *compatible; 118 ulong data; 119 }; 120 121 #ifdef CONFIG_OF_CONTROL 122 #define of_match_ptr(_ptr) (_ptr) 123 #else 124 #define of_match_ptr(_ptr) NULL 125 #endif /* CONFIG_OF_CONTROL */ 126 127 /** 128 * struct driver - A driver for a feature or peripheral 129 * 130 * This holds methods for setting up a new device, and also removing it. 131 * The device needs information to set itself up - this is provided either 132 * by platdata or a device tree node (which we find by looking up 133 * matching compatible strings with of_match). 134 * 135 * Drivers all belong to a uclass, representing a class of devices of the 136 * same type. Common elements of the drivers can be implemented in the uclass, 137 * or the uclass can provide a consistent interface to the drivers within 138 * it. 139 * 140 * @name: Device name 141 * @id: Identiies the uclass we belong to 142 * @of_match: List of compatible strings to match, and any identifying data 143 * for each. 144 * @bind: Called to bind a device to its driver 145 * @probe: Called to probe a device, i.e. activate it 146 * @remove: Called to remove a device, i.e. de-activate it 147 * @unbind: Called to unbind a device from its driver 148 * @ofdata_to_platdata: Called before probe to decode device tree data 149 * @child_post_bind: Called after a new child has been bound 150 * @child_pre_probe: Called before a child device is probed. The device has 151 * memory allocated but it has not yet been probed. 152 * @child_post_remove: Called after a child device is removed. The device 153 * has memory allocated but its device_remove() method has been called. 154 * @priv_auto_alloc_size: If non-zero this is the size of the private data 155 * to be allocated in the device's ->priv pointer. If zero, then the driver 156 * is responsible for allocating any data required. 157 * @platdata_auto_alloc_size: If non-zero this is the size of the 158 * platform data to be allocated in the device's ->platdata pointer. 159 * This is typically only useful for device-tree-aware drivers (those with 160 * an of_match), since drivers which use platdata will have the data 161 * provided in the U_BOOT_DEVICE() instantiation. 162 * @per_child_auto_alloc_size: Each device can hold private data owned by 163 * its parent. If required this will be automatically allocated if this 164 * value is non-zero. 165 * TODO(sjg@chromium.org): I'm considering dropping this, and just having 166 * device_probe_child() pass it in. So far the use case for allocating it 167 * is SPI, but I found that unsatisfactory. Since it is here I will leave it 168 * until things are clearer. 169 * @per_child_platdata_auto_alloc_size: A bus likes to store information about 170 * its children. If non-zero this is the size of this data, to be allocated 171 * in the child's parent_platdata pointer. 172 * @ops: Driver-specific operations. This is typically a list of function 173 * pointers defined by the driver, to implement driver functions required by 174 * the uclass. 175 * @flags: driver flags - see DM_FLAGS_... 176 */ 177 struct driver { 178 char *name; 179 enum uclass_id id; 180 const struct udevice_id *of_match; 181 int (*bind)(struct udevice *dev); 182 int (*probe)(struct udevice *dev); 183 int (*remove)(struct udevice *dev); 184 int (*unbind)(struct udevice *dev); 185 int (*ofdata_to_platdata)(struct udevice *dev); 186 int (*child_post_bind)(struct udevice *dev); 187 int (*child_pre_probe)(struct udevice *dev); 188 int (*child_post_remove)(struct udevice *dev); 189 int priv_auto_alloc_size; 190 int platdata_auto_alloc_size; 191 int per_child_auto_alloc_size; 192 int per_child_platdata_auto_alloc_size; 193 const void *ops; /* driver-specific operations */ 194 uint32_t flags; 195 }; 196 197 /* Declare a new U-Boot driver */ 198 #define U_BOOT_DRIVER(__name) \ 199 ll_entry_declare(struct driver, __name, driver) 200 201 /** 202 * dev_get_platdata() - Get the platform data for a device 203 * 204 * This checks that dev is not NULL, but no other checks for now 205 * 206 * @dev Device to check 207 * @return platform data, or NULL if none 208 */ 209 void *dev_get_platdata(struct udevice *dev); 210 211 /** 212 * dev_get_parent_platdata() - Get the parent platform data for a device 213 * 214 * This checks that dev is not NULL, but no other checks for now 215 * 216 * @dev Device to check 217 * @return parent's platform data, or NULL if none 218 */ 219 void *dev_get_parent_platdata(struct udevice *dev); 220 221 /** 222 * dev_get_uclass_platdata() - Get the uclass platform data for a device 223 * 224 * This checks that dev is not NULL, but no other checks for now 225 * 226 * @dev Device to check 227 * @return uclass's platform data, or NULL if none 228 */ 229 void *dev_get_uclass_platdata(struct udevice *dev); 230 231 /** 232 * dev_get_parentdata() - Get the parent data for a device 233 * 234 * The parent data is data stored in the device but owned by the parent. 235 * For example, a USB device may have parent data which contains information 236 * about how to talk to the device over USB. 237 * 238 * This checks that dev is not NULL, but no other checks for now 239 * 240 * @dev Device to check 241 * @return parent data, or NULL if none 242 */ 243 void *dev_get_parentdata(struct udevice *dev); 244 245 /** 246 * dev_get_priv() - Get the private data for a device 247 * 248 * This checks that dev is not NULL, but no other checks for now 249 * 250 * @dev Device to check 251 * @return private data, or NULL if none 252 */ 253 void *dev_get_priv(struct udevice *dev); 254 255 /** 256 * struct dev_get_parent() - Get the parent of a device 257 * 258 * @child: Child to check 259 * @return parent of child, or NULL if this is the root device 260 */ 261 struct udevice *dev_get_parent(struct udevice *child); 262 263 /** 264 * dev_get_uclass_priv() - Get the private uclass data for a device 265 * 266 * This checks that dev is not NULL, but no other checks for now 267 * 268 * @dev Device to check 269 * @return private uclass data for this device, or NULL if none 270 */ 271 void *dev_get_uclass_priv(struct udevice *dev); 272 273 /** 274 * dev_get_driver_data() - get the driver data used to bind a device 275 * 276 * When a device is bound using a device tree node, it matches a 277 * particular compatible string as in struct udevice_id. This function 278 * returns the associated data value for that compatible string. This is 279 * the 'data' field in struct udevice_id. 280 * 281 * For USB devices, this is the driver_info field in struct usb_device_id. 282 * 283 * @dev: Device to check 284 */ 285 ulong dev_get_driver_data(struct udevice *dev); 286 287 /** 288 * dev_get_driver_ops() - get the device's driver's operations 289 * 290 * This checks that dev is not NULL, and returns the pointer to device's 291 * driver's operations. 292 * 293 * @dev: Device to check 294 * @return void pointer to driver's operations or NULL for NULL-dev or NULL-ops 295 */ 296 const void *dev_get_driver_ops(struct udevice *dev); 297 298 /* 299 * device_get_uclass_id() - return the uclass ID of a device 300 * 301 * @dev: Device to check 302 * @return uclass ID for the device 303 */ 304 enum uclass_id device_get_uclass_id(struct udevice *dev); 305 306 /* 307 * dev_get_uclass_name() - return the uclass name of a device 308 * 309 * This checks that dev is not NULL. 310 * 311 * @dev: Device to check 312 * @return pointer to the uclass name for the device 313 */ 314 const char *dev_get_uclass_name(struct udevice *dev); 315 316 /** 317 * device_get_child() - Get the child of a device by index 318 * 319 * Returns the numbered child, 0 being the first. This does not use 320 * sequence numbers, only the natural order. 321 * 322 * @dev: Parent device to check 323 * @index: Child index 324 * @devp: Returns pointer to device 325 * @return 0 if OK, -ENODEV if no such device, other error if the device fails 326 * to probe 327 */ 328 int device_get_child(struct udevice *parent, int index, struct udevice **devp); 329 330 /** 331 * device_find_child_by_seq() - Find a child device based on a sequence 332 * 333 * This searches for a device with the given seq or req_seq. 334 * 335 * For seq, if an active device has this sequence it will be returned. 336 * If there is no such device then this will return -ENODEV. 337 * 338 * For req_seq, if a device (whether activated or not) has this req_seq 339 * value, that device will be returned. This is a strong indication that 340 * the device will receive that sequence when activated. 341 * 342 * @parent: Parent device 343 * @seq_or_req_seq: Sequence number to find (0=first) 344 * @find_req_seq: true to find req_seq, false to find seq 345 * @devp: Returns pointer to device (there is only one per for each seq). 346 * Set to NULL if none is found 347 * @return 0 if OK, -ve on error 348 */ 349 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq, 350 bool find_req_seq, struct udevice **devp); 351 352 /** 353 * device_get_child_by_seq() - Get a child device based on a sequence 354 * 355 * If an active device has this sequence it will be returned. If there is no 356 * such device then this will check for a device that is requesting this 357 * sequence. 358 * 359 * The device is probed to activate it ready for use. 360 * 361 * @parent: Parent device 362 * @seq: Sequence number to find (0=first) 363 * @devp: Returns pointer to device (there is only one per for each seq) 364 * Set to NULL if none is found 365 * @return 0 if OK, -ve on error 366 */ 367 int device_get_child_by_seq(struct udevice *parent, int seq, 368 struct udevice **devp); 369 370 /** 371 * device_find_child_by_of_offset() - Find a child device based on FDT offset 372 * 373 * Locates a child device by its device tree offset. 374 * 375 * @parent: Parent device 376 * @of_offset: Device tree offset to find 377 * @devp: Returns pointer to device if found, otherwise this is set to NULL 378 * @return 0 if OK, -ve on error 379 */ 380 int device_find_child_by_of_offset(struct udevice *parent, int of_offset, 381 struct udevice **devp); 382 383 /** 384 * device_get_child_by_of_offset() - Get a child device based on FDT offset 385 * 386 * Locates a child device by its device tree offset. 387 * 388 * The device is probed to activate it ready for use. 389 * 390 * @parent: Parent device 391 * @of_offset: Device tree offset to find 392 * @devp: Returns pointer to device if found, otherwise this is set to NULL 393 * @return 0 if OK, -ve on error 394 */ 395 int device_get_child_by_of_offset(struct udevice *parent, int of_offset, 396 struct udevice **devp); 397 398 /** 399 * device_get_global_by_of_offset() - Get a device based on FDT offset 400 * 401 * Locates a device by its device tree offset, searching globally throughout 402 * the all driver model devices. 403 * 404 * The device is probed to activate it ready for use. 405 * 406 * @of_offset: Device tree offset to find 407 * @devp: Returns pointer to device if found, otherwise this is set to NULL 408 * @return 0 if OK, -ve on error 409 */ 410 int device_get_global_by_of_offset(int of_offset, struct udevice **devp); 411 412 /** 413 * device_find_first_child() - Find the first child of a device 414 * 415 * @parent: Parent device to search 416 * @devp: Returns first child device, or NULL if none 417 * @return 0 418 */ 419 int device_find_first_child(struct udevice *parent, struct udevice **devp); 420 421 /** 422 * device_find_next_child() - Find the next child of a device 423 * 424 * @devp: Pointer to previous child device on entry. Returns pointer to next 425 * child device, or NULL if none 426 * @return 0 427 */ 428 int device_find_next_child(struct udevice **devp); 429 430 /** 431 * dev_get_addr() - Get the reg property of a device 432 * 433 * @dev: Pointer to a device 434 * 435 * @return addr 436 */ 437 fdt_addr_t dev_get_addr(struct udevice *dev); 438 439 /** 440 * device_has_children() - check if a device has any children 441 * 442 * @dev: Device to check 443 * @return true if the device has one or more children 444 */ 445 bool device_has_children(struct udevice *dev); 446 447 /** 448 * device_has_active_children() - check if a device has any active children 449 * 450 * @dev: Device to check 451 * @return true if the device has one or more children and at least one of 452 * them is active (probed). 453 */ 454 bool device_has_active_children(struct udevice *dev); 455 456 /** 457 * device_is_last_sibling() - check if a device is the last sibling 458 * 459 * This function can be useful for display purposes, when special action needs 460 * to be taken when displaying the last sibling. This can happen when a tree 461 * view of devices is being displayed. 462 * 463 * @dev: Device to check 464 * @return true if there are no more siblings after this one - i.e. is it 465 * last in the list. 466 */ 467 bool device_is_last_sibling(struct udevice *dev); 468 469 /* device resource management */ 470 typedef void (*dr_release_t)(struct udevice *dev, void *res); 471 typedef int (*dr_match_t)(struct udevice *dev, void *res, void *match_data); 472 473 #ifdef CONFIG_DEBUG_DEVRES 474 void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp, 475 const char *name); 476 #define _devres_alloc(release, size, gfp) \ 477 __devres_alloc(release, size, gfp, #release) 478 #else 479 void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp); 480 #endif 481 482 /** 483 * devres_alloc - Allocate device resource data 484 * @release: Release function devres will be associated with 485 * @size: Allocation size 486 * @gfp: Allocation flags 487 * 488 * Allocate devres of @size bytes. The allocated area is associated 489 * with @release. The returned pointer can be passed to 490 * other devres_*() functions. 491 * 492 * RETURNS: 493 * Pointer to allocated devres on success, NULL on failure. 494 */ 495 #define devres_alloc(release, size, gfp) \ 496 _devres_alloc(release, size, gfp | __GFP_ZERO) 497 498 /** 499 * devres_free - Free device resource data 500 * @res: Pointer to devres data to free 501 * 502 * Free devres created with devres_alloc(). 503 */ 504 void devres_free(void *res); 505 506 /** 507 * devres_add - Register device resource 508 * @dev: Device to add resource to 509 * @res: Resource to register 510 * 511 * Register devres @res to @dev. @res should have been allocated 512 * using devres_alloc(). On driver detach, the associated release 513 * function will be invoked and devres will be freed automatically. 514 */ 515 void devres_add(struct udevice *dev, void *res); 516 517 /** 518 * devres_find - Find device resource 519 * @dev: Device to lookup resource from 520 * @release: Look for resources associated with this release function 521 * @match: Match function (optional) 522 * @match_data: Data for the match function 523 * 524 * Find the latest devres of @dev which is associated with @release 525 * and for which @match returns 1. If @match is NULL, it's considered 526 * to match all. 527 * 528 * RETURNS: 529 * Pointer to found devres, NULL if not found. 530 */ 531 void *devres_find(struct udevice *dev, dr_release_t release, 532 dr_match_t match, void *match_data); 533 534 /** 535 * devres_get - Find devres, if non-existent, add one atomically 536 * @dev: Device to lookup or add devres for 537 * @new_res: Pointer to new initialized devres to add if not found 538 * @match: Match function (optional) 539 * @match_data: Data for the match function 540 * 541 * Find the latest devres of @dev which has the same release function 542 * as @new_res and for which @match return 1. If found, @new_res is 543 * freed; otherwise, @new_res is added atomically. 544 * 545 * RETURNS: 546 * Pointer to found or added devres. 547 */ 548 void *devres_get(struct udevice *dev, void *new_res, 549 dr_match_t match, void *match_data); 550 551 /** 552 * devres_remove - Find a device resource and remove it 553 * @dev: Device to find resource from 554 * @release: Look for resources associated with this release function 555 * @match: Match function (optional) 556 * @match_data: Data for the match function 557 * 558 * Find the latest devres of @dev associated with @release and for 559 * which @match returns 1. If @match is NULL, it's considered to 560 * match all. If found, the resource is removed atomically and 561 * returned. 562 * 563 * RETURNS: 564 * Pointer to removed devres on success, NULL if not found. 565 */ 566 void *devres_remove(struct udevice *dev, dr_release_t release, 567 dr_match_t match, void *match_data); 568 569 /** 570 * devres_destroy - Find a device resource and destroy it 571 * @dev: Device to find resource from 572 * @release: Look for resources associated with this release function 573 * @match: Match function (optional) 574 * @match_data: Data for the match function 575 * 576 * Find the latest devres of @dev associated with @release and for 577 * which @match returns 1. If @match is NULL, it's considered to 578 * match all. If found, the resource is removed atomically and freed. 579 * 580 * Note that the release function for the resource will not be called, 581 * only the devres-allocated data will be freed. The caller becomes 582 * responsible for freeing any other data. 583 * 584 * RETURNS: 585 * 0 if devres is found and freed, -ENOENT if not found. 586 */ 587 int devres_destroy(struct udevice *dev, dr_release_t release, 588 dr_match_t match, void *match_data); 589 590 /** 591 * devres_release - Find a device resource and destroy it, calling release 592 * @dev: Device to find resource from 593 * @release: Look for resources associated with this release function 594 * @match: Match function (optional) 595 * @match_data: Data for the match function 596 * 597 * Find the latest devres of @dev associated with @release and for 598 * which @match returns 1. If @match is NULL, it's considered to 599 * match all. If found, the resource is removed atomically, the 600 * release function called and the resource freed. 601 * 602 * RETURNS: 603 * 0 if devres is found and freed, -ENOENT if not found. 604 */ 605 int devres_release(struct udevice *dev, dr_release_t release, 606 dr_match_t match, void *match_data); 607 608 #endif 609