16494d708SSimon Glass /* 26494d708SSimon Glass * Copyright (c) 2013 Google, Inc 36494d708SSimon Glass * 46494d708SSimon Glass * (C) Copyright 2012 56494d708SSimon Glass * Pavel Herrmann <morpheus.ibis@gmail.com> 66494d708SSimon Glass * Marek Vasut <marex@denx.de> 76494d708SSimon Glass * 86494d708SSimon Glass * SPDX-License-Identifier: GPL-2.0+ 96494d708SSimon Glass */ 106494d708SSimon Glass 116494d708SSimon Glass #ifndef _DM_DEVICE_H 126494d708SSimon Glass #define _DM_DEVICE_H 136494d708SSimon Glass 146494d708SSimon Glass #include <dm/uclass-id.h> 15c9cac3f8SPeng Fan #include <fdtdec.h> 166494d708SSimon Glass #include <linker_lists.h> 176494d708SSimon Glass #include <linux/list.h> 186494d708SSimon Glass 196494d708SSimon Glass struct driver_info; 206494d708SSimon Glass 216494d708SSimon Glass /* Driver is active (probed). Cleared when it is removed */ 226494d708SSimon Glass #define DM_FLAG_ACTIVATED (1 << 0) 236494d708SSimon Glass 246494d708SSimon Glass /* DM is responsible for allocating and freeing platdata */ 25f2bc6fc3SSimon Glass #define DM_FLAG_ALLOC_PDATA (1 << 1) 266494d708SSimon Glass 2700606d7eSSimon Glass /* DM should init this device prior to relocation */ 2800606d7eSSimon Glass #define DM_FLAG_PRE_RELOC (1 << 2) 2900606d7eSSimon Glass 30cdc133bdSSimon Glass /* DM is responsible for allocating and freeing parent_platdata */ 31cdc133bdSSimon Glass #define DM_FLAG_ALLOC_PARENT_PDATA (1 << 3) 32cdc133bdSSimon Glass 33*2c03c463SSimon Glass /* Allocate driver private data on a DMA boundary */ 34*2c03c463SSimon Glass #define DM_FLAG_ALLOC_PRIV_DMA (1 << 4) 35*2c03c463SSimon Glass 366494d708SSimon Glass /** 3754c5d08aSHeiko Schocher * struct udevice - An instance of a driver 386494d708SSimon Glass * 396494d708SSimon Glass * This holds information about a device, which is a driver bound to a 406494d708SSimon Glass * particular port or peripheral (essentially a driver instance). 416494d708SSimon Glass * 426494d708SSimon Glass * A device will come into existence through a 'bind' call, either due to 436494d708SSimon Glass * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node 446494d708SSimon Glass * in the device tree (in which case of_offset is >= 0). In the latter case 456494d708SSimon Glass * we translate the device tree information into platdata in a function 466494d708SSimon Glass * implemented by the driver ofdata_to_platdata method (called just before the 476494d708SSimon Glass * probe method if the device has a device tree node. 486494d708SSimon Glass * 496494d708SSimon Glass * All three of platdata, priv and uclass_priv can be allocated by the 506494d708SSimon Glass * driver, or you can use the auto_alloc_size members of struct driver and 516494d708SSimon Glass * struct uclass_driver to have driver model do this automatically. 526494d708SSimon Glass * 536494d708SSimon Glass * @driver: The driver used by this device 546494d708SSimon Glass * @name: Name of device, typically the FDT node name 556494d708SSimon Glass * @platdata: Configuration data for this device 56cdc133bdSSimon Glass * @parent_platdata: The parent bus's configuration data for this device 576494d708SSimon Glass * @of_offset: Device tree node offset for this device (- for none) 582ef249b4SSimon Glass * @of_id: Pointer to the udevice_id structure which created the device 596494d708SSimon Glass * @parent: Parent of this device, or NULL for the top level device 606494d708SSimon Glass * @priv: Private data for this device 616494d708SSimon Glass * @uclass: Pointer to uclass for this device 626494d708SSimon Glass * @uclass_priv: The uclass's private data for this device 63e59f458dSSimon Glass * @parent_priv: The parent's private data for this device 646494d708SSimon Glass * @uclass_node: Used by uclass to link its devices 656494d708SSimon Glass * @child_head: List of children of this device 666494d708SSimon Glass * @sibling_node: Next device in list of all devices 676494d708SSimon Glass * @flags: Flags for this device DM_FLAG_... 685a66a8ffSSimon Glass * @req_seq: Requested sequence number for this device (-1 = any) 69547cea19SSimon Glass * @seq: Allocated sequence number for this device (-1 = none). This is set up 70547cea19SSimon Glass * when the device is probed and will be unique within the device's uclass. 716494d708SSimon Glass */ 7254c5d08aSHeiko Schocher struct udevice { 736494d708SSimon Glass struct driver *driver; 746494d708SSimon Glass const char *name; 756494d708SSimon Glass void *platdata; 76cdc133bdSSimon Glass void *parent_platdata; 776494d708SSimon Glass int of_offset; 782ef249b4SSimon Glass const struct udevice_id *of_id; 7954c5d08aSHeiko Schocher struct udevice *parent; 806494d708SSimon Glass void *priv; 816494d708SSimon Glass struct uclass *uclass; 826494d708SSimon Glass void *uclass_priv; 83e59f458dSSimon Glass void *parent_priv; 846494d708SSimon Glass struct list_head uclass_node; 856494d708SSimon Glass struct list_head child_head; 866494d708SSimon Glass struct list_head sibling_node; 876494d708SSimon Glass uint32_t flags; 885a66a8ffSSimon Glass int req_seq; 895a66a8ffSSimon Glass int seq; 906494d708SSimon Glass }; 916494d708SSimon Glass 925a66a8ffSSimon Glass /* Maximum sequence number supported */ 935a66a8ffSSimon Glass #define DM_MAX_SEQ 999 945a66a8ffSSimon Glass 956494d708SSimon Glass /* Returns the operations for a device */ 966494d708SSimon Glass #define device_get_ops(dev) (dev->driver->ops) 976494d708SSimon Glass 986494d708SSimon Glass /* Returns non-zero if the device is active (probed and not removed) */ 996494d708SSimon Glass #define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED) 1006494d708SSimon Glass 1016494d708SSimon Glass /** 102ae7f4513SSimon Glass * struct udevice_id - Lists the compatible strings supported by a driver 1036494d708SSimon Glass * @compatible: Compatible string 1046494d708SSimon Glass * @data: Data for this compatible string 1056494d708SSimon Glass */ 106ae7f4513SSimon Glass struct udevice_id { 1076494d708SSimon Glass const char *compatible; 1086494d708SSimon Glass ulong data; 1096494d708SSimon Glass }; 1106494d708SSimon Glass 111f887cb6dSMasahiro Yamada #ifdef CONFIG_OF_CONTROL 112f887cb6dSMasahiro Yamada #define of_match_ptr(_ptr) (_ptr) 113f887cb6dSMasahiro Yamada #else 114f887cb6dSMasahiro Yamada #define of_match_ptr(_ptr) NULL 115f887cb6dSMasahiro Yamada #endif /* CONFIG_OF_CONTROL */ 116f887cb6dSMasahiro Yamada 1176494d708SSimon Glass /** 1186494d708SSimon Glass * struct driver - A driver for a feature or peripheral 1196494d708SSimon Glass * 1206494d708SSimon Glass * This holds methods for setting up a new device, and also removing it. 1216494d708SSimon Glass * The device needs information to set itself up - this is provided either 1226494d708SSimon Glass * by platdata or a device tree node (which we find by looking up 1236494d708SSimon Glass * matching compatible strings with of_match). 1246494d708SSimon Glass * 1256494d708SSimon Glass * Drivers all belong to a uclass, representing a class of devices of the 1266494d708SSimon Glass * same type. Common elements of the drivers can be implemented in the uclass, 1276494d708SSimon Glass * or the uclass can provide a consistent interface to the drivers within 1286494d708SSimon Glass * it. 1296494d708SSimon Glass * 1306494d708SSimon Glass * @name: Device name 1316494d708SSimon Glass * @id: Identiies the uclass we belong to 1326494d708SSimon Glass * @of_match: List of compatible strings to match, and any identifying data 1336494d708SSimon Glass * for each. 1346494d708SSimon Glass * @bind: Called to bind a device to its driver 1356494d708SSimon Glass * @probe: Called to probe a device, i.e. activate it 1366494d708SSimon Glass * @remove: Called to remove a device, i.e. de-activate it 1376494d708SSimon Glass * @unbind: Called to unbind a device from its driver 1386494d708SSimon Glass * @ofdata_to_platdata: Called before probe to decode device tree data 1390118ce79SSimon Glass * @child_post_bind: Called after a new child has been bound 140a327dee0SSimon Glass * @child_pre_probe: Called before a child device is probed. The device has 141a327dee0SSimon Glass * memory allocated but it has not yet been probed. 142a327dee0SSimon Glass * @child_post_remove: Called after a child device is removed. The device 143a327dee0SSimon Glass * has memory allocated but its device_remove() method has been called. 1446494d708SSimon Glass * @priv_auto_alloc_size: If non-zero this is the size of the private data 1456494d708SSimon Glass * to be allocated in the device's ->priv pointer. If zero, then the driver 1466494d708SSimon Glass * is responsible for allocating any data required. 1476494d708SSimon Glass * @platdata_auto_alloc_size: If non-zero this is the size of the 1486494d708SSimon Glass * platform data to be allocated in the device's ->platdata pointer. 1496494d708SSimon Glass * This is typically only useful for device-tree-aware drivers (those with 1506494d708SSimon Glass * an of_match), since drivers which use platdata will have the data 1516494d708SSimon Glass * provided in the U_BOOT_DEVICE() instantiation. 152e59f458dSSimon Glass * @per_child_auto_alloc_size: Each device can hold private data owned by 153e59f458dSSimon Glass * its parent. If required this will be automatically allocated if this 154e59f458dSSimon Glass * value is non-zero. 155accd4b19SSimon Glass * TODO(sjg@chromium.org): I'm considering dropping this, and just having 156accd4b19SSimon Glass * device_probe_child() pass it in. So far the use case for allocating it 157accd4b19SSimon Glass * is SPI, but I found that unsatisfactory. Since it is here I will leave it 158accd4b19SSimon Glass * until things are clearer. 159cdc133bdSSimon Glass * @per_child_platdata_auto_alloc_size: A bus likes to store information about 160cdc133bdSSimon Glass * its children. If non-zero this is the size of this data, to be allocated 161cdc133bdSSimon Glass * in the child's parent_platdata pointer. 1620040b944SSimon Glass * @ops: Driver-specific operations. This is typically a list of function 1636494d708SSimon Glass * pointers defined by the driver, to implement driver functions required by 1646494d708SSimon Glass * the uclass. 16500606d7eSSimon Glass * @flags: driver flags - see DM_FLAGS_... 1666494d708SSimon Glass */ 1676494d708SSimon Glass struct driver { 1686494d708SSimon Glass char *name; 1696494d708SSimon Glass enum uclass_id id; 170ae7f4513SSimon Glass const struct udevice_id *of_match; 17154c5d08aSHeiko Schocher int (*bind)(struct udevice *dev); 17254c5d08aSHeiko Schocher int (*probe)(struct udevice *dev); 17354c5d08aSHeiko Schocher int (*remove)(struct udevice *dev); 17454c5d08aSHeiko Schocher int (*unbind)(struct udevice *dev); 17554c5d08aSHeiko Schocher int (*ofdata_to_platdata)(struct udevice *dev); 1760118ce79SSimon Glass int (*child_post_bind)(struct udevice *dev); 177a327dee0SSimon Glass int (*child_pre_probe)(struct udevice *dev); 178a327dee0SSimon Glass int (*child_post_remove)(struct udevice *dev); 1796494d708SSimon Glass int priv_auto_alloc_size; 1806494d708SSimon Glass int platdata_auto_alloc_size; 181e59f458dSSimon Glass int per_child_auto_alloc_size; 182cdc133bdSSimon Glass int per_child_platdata_auto_alloc_size; 1836494d708SSimon Glass const void *ops; /* driver-specific operations */ 18400606d7eSSimon Glass uint32_t flags; 1856494d708SSimon Glass }; 1866494d708SSimon Glass 1876494d708SSimon Glass /* Declare a new U-Boot driver */ 1886494d708SSimon Glass #define U_BOOT_DRIVER(__name) \ 1896494d708SSimon Glass ll_entry_declare(struct driver, __name, driver) 1906494d708SSimon Glass 1916494d708SSimon Glass /** 1926494d708SSimon Glass * dev_get_platdata() - Get the platform data for a device 1936494d708SSimon Glass * 1946494d708SSimon Glass * This checks that dev is not NULL, but no other checks for now 1956494d708SSimon Glass * 1966494d708SSimon Glass * @dev Device to check 1976494d708SSimon Glass * @return platform data, or NULL if none 1986494d708SSimon Glass */ 19954c5d08aSHeiko Schocher void *dev_get_platdata(struct udevice *dev); 2006494d708SSimon Glass 2016494d708SSimon Glass /** 202cdc133bdSSimon Glass * dev_get_parent_platdata() - Get the parent platform data for a device 203cdc133bdSSimon Glass * 204cdc133bdSSimon Glass * This checks that dev is not NULL, but no other checks for now 205cdc133bdSSimon Glass * 206cdc133bdSSimon Glass * @dev Device to check 207cdc133bdSSimon Glass * @return parent's platform data, or NULL if none 208cdc133bdSSimon Glass */ 209cdc133bdSSimon Glass void *dev_get_parent_platdata(struct udevice *dev); 210cdc133bdSSimon Glass 211cdc133bdSSimon Glass /** 212e59f458dSSimon Glass * dev_get_parentdata() - Get the parent data for a device 213e59f458dSSimon Glass * 214e59f458dSSimon Glass * The parent data is data stored in the device but owned by the parent. 215e59f458dSSimon Glass * For example, a USB device may have parent data which contains information 216e59f458dSSimon Glass * about how to talk to the device over USB. 217e59f458dSSimon Glass * 218e59f458dSSimon Glass * This checks that dev is not NULL, but no other checks for now 219e59f458dSSimon Glass * 220e59f458dSSimon Glass * @dev Device to check 221e59f458dSSimon Glass * @return parent data, or NULL if none 222e59f458dSSimon Glass */ 223e59f458dSSimon Glass void *dev_get_parentdata(struct udevice *dev); 224e59f458dSSimon Glass 225e59f458dSSimon Glass /** 2266494d708SSimon Glass * dev_get_priv() - Get the private data for a device 2276494d708SSimon Glass * 2286494d708SSimon Glass * This checks that dev is not NULL, but no other checks for now 2296494d708SSimon Glass * 2306494d708SSimon Glass * @dev Device to check 2316494d708SSimon Glass * @return private data, or NULL if none 2326494d708SSimon Glass */ 23354c5d08aSHeiko Schocher void *dev_get_priv(struct udevice *dev); 2346494d708SSimon Glass 2355a66a8ffSSimon Glass /** 236479728cbSSimon Glass * struct dev_get_parent() - Get the parent of a device 237479728cbSSimon Glass * 238479728cbSSimon Glass * @child: Child to check 239479728cbSSimon Glass * @return parent of child, or NULL if this is the root device 240479728cbSSimon Glass */ 241479728cbSSimon Glass struct udevice *dev_get_parent(struct udevice *child); 242479728cbSSimon Glass 243479728cbSSimon Glass /** 244e564f054SSimon Glass * dev_get_uclass_priv() - Get the private uclass data for a device 245e564f054SSimon Glass * 246e564f054SSimon Glass * This checks that dev is not NULL, but no other checks for now 247e564f054SSimon Glass * 248e564f054SSimon Glass * @dev Device to check 249e564f054SSimon Glass * @return private uclass data for this device, or NULL if none 250e564f054SSimon Glass */ 251e564f054SSimon Glass void *dev_get_uclass_priv(struct udevice *dev); 252e564f054SSimon Glass 253e564f054SSimon Glass /** 2542ef249b4SSimon Glass * dev_get_of_data() - get the device tree data used to bind a device 2552ef249b4SSimon Glass * 2562ef249b4SSimon Glass * When a device is bound using a device tree node, it matches a 2572ef249b4SSimon Glass * particular compatible string as in struct udevice_id. This function 2582ef249b4SSimon Glass * returns the associated data value for that compatible string 2592ef249b4SSimon Glass */ 2602ef249b4SSimon Glass ulong dev_get_of_data(struct udevice *dev); 2612ef249b4SSimon Glass 262b3670531SSimon Glass /* 263b3670531SSimon Glass * device_get_uclass_id() - return the uclass ID of a device 264b3670531SSimon Glass * 265b3670531SSimon Glass * @dev: Device to check 266b3670531SSimon Glass * @return uclass ID for the device 267b3670531SSimon Glass */ 268b3670531SSimon Glass enum uclass_id device_get_uclass_id(struct udevice *dev); 269b3670531SSimon Glass 2702ef249b4SSimon Glass /** 271997c87bbSSimon Glass * device_get_child() - Get the child of a device by index 272997c87bbSSimon Glass * 273997c87bbSSimon Glass * Returns the numbered child, 0 being the first. This does not use 274997c87bbSSimon Glass * sequence numbers, only the natural order. 275997c87bbSSimon Glass * 276997c87bbSSimon Glass * @dev: Parent device to check 277997c87bbSSimon Glass * @index: Child index 278997c87bbSSimon Glass * @devp: Returns pointer to device 279997c87bbSSimon Glass */ 280997c87bbSSimon Glass int device_get_child(struct udevice *parent, int index, struct udevice **devp); 281997c87bbSSimon Glass 282997c87bbSSimon Glass /** 2835a66a8ffSSimon Glass * device_find_child_by_seq() - Find a child device based on a sequence 2845a66a8ffSSimon Glass * 2855a66a8ffSSimon Glass * This searches for a device with the given seq or req_seq. 2865a66a8ffSSimon Glass * 2875a66a8ffSSimon Glass * For seq, if an active device has this sequence it will be returned. 2885a66a8ffSSimon Glass * If there is no such device then this will return -ENODEV. 2895a66a8ffSSimon Glass * 2905a66a8ffSSimon Glass * For req_seq, if a device (whether activated or not) has this req_seq 2915a66a8ffSSimon Glass * value, that device will be returned. This is a strong indication that 2925a66a8ffSSimon Glass * the device will receive that sequence when activated. 2935a66a8ffSSimon Glass * 2945a66a8ffSSimon Glass * @parent: Parent device 2955a66a8ffSSimon Glass * @seq_or_req_seq: Sequence number to find (0=first) 2965a66a8ffSSimon Glass * @find_req_seq: true to find req_seq, false to find seq 2975a66a8ffSSimon Glass * @devp: Returns pointer to device (there is only one per for each seq). 2985a66a8ffSSimon Glass * Set to NULL if none is found 2995a66a8ffSSimon Glass * @return 0 if OK, -ve on error 3005a66a8ffSSimon Glass */ 3015a66a8ffSSimon Glass int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq, 3025a66a8ffSSimon Glass bool find_req_seq, struct udevice **devp); 3035a66a8ffSSimon Glass 304997c87bbSSimon Glass /** 305997c87bbSSimon Glass * device_get_child_by_seq() - Get a child device based on a sequence 306997c87bbSSimon Glass * 307997c87bbSSimon Glass * If an active device has this sequence it will be returned. If there is no 308997c87bbSSimon Glass * such device then this will check for a device that is requesting this 309997c87bbSSimon Glass * sequence. 310997c87bbSSimon Glass * 311997c87bbSSimon Glass * The device is probed to activate it ready for use. 312997c87bbSSimon Glass * 313997c87bbSSimon Glass * @parent: Parent device 314997c87bbSSimon Glass * @seq: Sequence number to find (0=first) 315997c87bbSSimon Glass * @devp: Returns pointer to device (there is only one per for each seq) 316997c87bbSSimon Glass * Set to NULL if none is found 317997c87bbSSimon Glass * @return 0 if OK, -ve on error 318997c87bbSSimon Glass */ 319997c87bbSSimon Glass int device_get_child_by_seq(struct udevice *parent, int seq, 320997c87bbSSimon Glass struct udevice **devp); 321997c87bbSSimon Glass 322997c87bbSSimon Glass /** 323997c87bbSSimon Glass * device_find_child_by_of_offset() - Find a child device based on FDT offset 324997c87bbSSimon Glass * 325997c87bbSSimon Glass * Locates a child device by its device tree offset. 326997c87bbSSimon Glass * 327997c87bbSSimon Glass * @parent: Parent device 328997c87bbSSimon Glass * @of_offset: Device tree offset to find 329997c87bbSSimon Glass * @devp: Returns pointer to device if found, otherwise this is set to NULL 330997c87bbSSimon Glass * @return 0 if OK, -ve on error 331997c87bbSSimon Glass */ 332997c87bbSSimon Glass int device_find_child_by_of_offset(struct udevice *parent, int of_offset, 333997c87bbSSimon Glass struct udevice **devp); 334997c87bbSSimon Glass 335997c87bbSSimon Glass /** 336997c87bbSSimon Glass * device_get_child_by_of_offset() - Get a child device based on FDT offset 337997c87bbSSimon Glass * 338997c87bbSSimon Glass * Locates a child device by its device tree offset. 339997c87bbSSimon Glass * 340997c87bbSSimon Glass * The device is probed to activate it ready for use. 341997c87bbSSimon Glass * 342997c87bbSSimon Glass * @parent: Parent device 343997c87bbSSimon Glass * @of_offset: Device tree offset to find 344997c87bbSSimon Glass * @devp: Returns pointer to device if found, otherwise this is set to NULL 345997c87bbSSimon Glass * @return 0 if OK, -ve on error 346997c87bbSSimon Glass */ 347997c87bbSSimon Glass int device_get_child_by_of_offset(struct udevice *parent, int seq, 348997c87bbSSimon Glass struct udevice **devp); 349997c87bbSSimon Glass 350a8981d4fSSimon Glass /** 351a8981d4fSSimon Glass * device_find_first_child() - Find the first child of a device 352a8981d4fSSimon Glass * 353a8981d4fSSimon Glass * @parent: Parent device to search 354a8981d4fSSimon Glass * @devp: Returns first child device, or NULL if none 355a8981d4fSSimon Glass * @return 0 356a8981d4fSSimon Glass */ 357a8981d4fSSimon Glass int device_find_first_child(struct udevice *parent, struct udevice **devp); 358a8981d4fSSimon Glass 359a8981d4fSSimon Glass /** 360a8981d4fSSimon Glass * device_find_first_child() - Find the first child of a device 361a8981d4fSSimon Glass * 362a8981d4fSSimon Glass * @devp: Pointer to previous child device on entry. Returns pointer to next 363a8981d4fSSimon Glass * child device, or NULL if none 364a8981d4fSSimon Glass * @return 0 365a8981d4fSSimon Glass */ 366a8981d4fSSimon Glass int device_find_next_child(struct udevice **devp); 367a8981d4fSSimon Glass 368c9cac3f8SPeng Fan /** 369c9cac3f8SPeng Fan * dev_get_addr() - Get the reg property of a device 370c9cac3f8SPeng Fan * 371c9cac3f8SPeng Fan * @dev: Pointer to a device 372c9cac3f8SPeng Fan * 373c9cac3f8SPeng Fan * @return addr 374c9cac3f8SPeng Fan */ 375c9cac3f8SPeng Fan fdt_addr_t dev_get_addr(struct udevice *dev); 376c9cac3f8SPeng Fan 3776494d708SSimon Glass #endif 378