Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 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> |
Peng Fan | c9cac3f | 2015-02-10 14:46:32 +0800 | [diff] [blame^] | 15 | #include <fdtdec.h> |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 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 */ |
Simon Glass | f2bc6fc | 2014-06-11 23:29:54 -0600 | [diff] [blame] | 25 | #define DM_FLAG_ALLOC_PDATA (1 << 1) |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 26 | |
Simon Glass | 00606d7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 27 | /* DM should init this device prior to relocation */ |
| 28 | #define DM_FLAG_PRE_RELOC (1 << 2) |
| 29 | |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 30 | /* DM is responsible for allocating and freeing parent_platdata */ |
| 31 | #define DM_FLAG_ALLOC_PARENT_PDATA (1 << 3) |
| 32 | |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 33 | /** |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 34 | * struct udevice - An instance of a driver |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 35 | * |
| 36 | * This holds information about a device, which is a driver bound to a |
| 37 | * particular port or peripheral (essentially a driver instance). |
| 38 | * |
| 39 | * A device will come into existence through a 'bind' call, either due to |
| 40 | * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node |
| 41 | * in the device tree (in which case of_offset is >= 0). In the latter case |
| 42 | * we translate the device tree information into platdata in a function |
| 43 | * implemented by the driver ofdata_to_platdata method (called just before the |
| 44 | * probe method if the device has a device tree node. |
| 45 | * |
| 46 | * All three of platdata, priv and uclass_priv can be allocated by the |
| 47 | * driver, or you can use the auto_alloc_size members of struct driver and |
| 48 | * struct uclass_driver to have driver model do this automatically. |
| 49 | * |
| 50 | * @driver: The driver used by this device |
| 51 | * @name: Name of device, typically the FDT node name |
| 52 | * @platdata: Configuration data for this device |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 53 | * @parent_platdata: The parent bus's configuration data for this device |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 54 | * @of_offset: Device tree node offset for this device (- for none) |
Simon Glass | 2ef249b | 2014-11-11 10:46:18 -0700 | [diff] [blame] | 55 | * @of_id: Pointer to the udevice_id structure which created the device |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 56 | * @parent: Parent of this device, or NULL for the top level device |
| 57 | * @priv: Private data for this device |
| 58 | * @uclass: Pointer to uclass for this device |
| 59 | * @uclass_priv: The uclass's private data for this device |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 60 | * @parent_priv: The parent's private data for this device |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 61 | * @uclass_node: Used by uclass to link its devices |
| 62 | * @child_head: List of children of this device |
| 63 | * @sibling_node: Next device in list of all devices |
| 64 | * @flags: Flags for this device DM_FLAG_... |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 65 | * @req_seq: Requested sequence number for this device (-1 = any) |
Simon Glass | 547cea1 | 2014-10-13 23:41:51 -0600 | [diff] [blame] | 66 | * @seq: Allocated sequence number for this device (-1 = none). This is set up |
| 67 | * when the device is probed and will be unique within the device's uclass. |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 68 | */ |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 69 | struct udevice { |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 70 | struct driver *driver; |
| 71 | const char *name; |
| 72 | void *platdata; |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 73 | void *parent_platdata; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 74 | int of_offset; |
Simon Glass | 2ef249b | 2014-11-11 10:46:18 -0700 | [diff] [blame] | 75 | const struct udevice_id *of_id; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 76 | struct udevice *parent; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 77 | void *priv; |
| 78 | struct uclass *uclass; |
| 79 | void *uclass_priv; |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 80 | void *parent_priv; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 81 | struct list_head uclass_node; |
| 82 | struct list_head child_head; |
| 83 | struct list_head sibling_node; |
| 84 | uint32_t flags; |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 85 | int req_seq; |
| 86 | int seq; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 89 | /* Maximum sequence number supported */ |
| 90 | #define DM_MAX_SEQ 999 |
| 91 | |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 92 | /* Returns the operations for a device */ |
| 93 | #define device_get_ops(dev) (dev->driver->ops) |
| 94 | |
| 95 | /* Returns non-zero if the device is active (probed and not removed) */ |
| 96 | #define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED) |
| 97 | |
| 98 | /** |
Simon Glass | ae7f451 | 2014-06-11 23:29:45 -0600 | [diff] [blame] | 99 | * struct udevice_id - Lists the compatible strings supported by a driver |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 100 | * @compatible: Compatible string |
| 101 | * @data: Data for this compatible string |
| 102 | */ |
Simon Glass | ae7f451 | 2014-06-11 23:29:45 -0600 | [diff] [blame] | 103 | struct udevice_id { |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 104 | const char *compatible; |
| 105 | ulong data; |
| 106 | }; |
| 107 | |
Masahiro Yamada | f887cb6 | 2014-10-07 14:51:31 +0900 | [diff] [blame] | 108 | #ifdef CONFIG_OF_CONTROL |
| 109 | #define of_match_ptr(_ptr) (_ptr) |
| 110 | #else |
| 111 | #define of_match_ptr(_ptr) NULL |
| 112 | #endif /* CONFIG_OF_CONTROL */ |
| 113 | |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 114 | /** |
| 115 | * struct driver - A driver for a feature or peripheral |
| 116 | * |
| 117 | * This holds methods for setting up a new device, and also removing it. |
| 118 | * The device needs information to set itself up - this is provided either |
| 119 | * by platdata or a device tree node (which we find by looking up |
| 120 | * matching compatible strings with of_match). |
| 121 | * |
| 122 | * Drivers all belong to a uclass, representing a class of devices of the |
| 123 | * same type. Common elements of the drivers can be implemented in the uclass, |
| 124 | * or the uclass can provide a consistent interface to the drivers within |
| 125 | * it. |
| 126 | * |
| 127 | * @name: Device name |
| 128 | * @id: Identiies the uclass we belong to |
| 129 | * @of_match: List of compatible strings to match, and any identifying data |
| 130 | * for each. |
| 131 | * @bind: Called to bind a device to its driver |
| 132 | * @probe: Called to probe a device, i.e. activate it |
| 133 | * @remove: Called to remove a device, i.e. de-activate it |
| 134 | * @unbind: Called to unbind a device from its driver |
| 135 | * @ofdata_to_platdata: Called before probe to decode device tree data |
Simon Glass | 0118ce7 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 136 | * @child_post_bind: Called after a new child has been bound |
Simon Glass | a327dee | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 137 | * @child_pre_probe: Called before a child device is probed. The device has |
| 138 | * memory allocated but it has not yet been probed. |
| 139 | * @child_post_remove: Called after a child device is removed. The device |
| 140 | * has memory allocated but its device_remove() method has been called. |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 141 | * @priv_auto_alloc_size: If non-zero this is the size of the private data |
| 142 | * to be allocated in the device's ->priv pointer. If zero, then the driver |
| 143 | * is responsible for allocating any data required. |
| 144 | * @platdata_auto_alloc_size: If non-zero this is the size of the |
| 145 | * platform data to be allocated in the device's ->platdata pointer. |
| 146 | * This is typically only useful for device-tree-aware drivers (those with |
| 147 | * an of_match), since drivers which use platdata will have the data |
| 148 | * provided in the U_BOOT_DEVICE() instantiation. |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 149 | * @per_child_auto_alloc_size: Each device can hold private data owned by |
| 150 | * its parent. If required this will be automatically allocated if this |
| 151 | * value is non-zero. |
Simon Glass | accd4b1 | 2014-10-13 23:41:50 -0600 | [diff] [blame] | 152 | * TODO(sjg@chromium.org): I'm considering dropping this, and just having |
| 153 | * device_probe_child() pass it in. So far the use case for allocating it |
| 154 | * is SPI, but I found that unsatisfactory. Since it is here I will leave it |
| 155 | * until things are clearer. |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 156 | * @per_child_platdata_auto_alloc_size: A bus likes to store information about |
| 157 | * its children. If non-zero this is the size of this data, to be allocated |
| 158 | * in the child's parent_platdata pointer. |
Simon Glass | 0040b94 | 2014-07-23 06:55:17 -0600 | [diff] [blame] | 159 | * @ops: Driver-specific operations. This is typically a list of function |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 160 | * pointers defined by the driver, to implement driver functions required by |
| 161 | * the uclass. |
Simon Glass | 00606d7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 162 | * @flags: driver flags - see DM_FLAGS_... |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 163 | */ |
| 164 | struct driver { |
| 165 | char *name; |
| 166 | enum uclass_id id; |
Simon Glass | ae7f451 | 2014-06-11 23:29:45 -0600 | [diff] [blame] | 167 | const struct udevice_id *of_match; |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 168 | int (*bind)(struct udevice *dev); |
| 169 | int (*probe)(struct udevice *dev); |
| 170 | int (*remove)(struct udevice *dev); |
| 171 | int (*unbind)(struct udevice *dev); |
| 172 | int (*ofdata_to_platdata)(struct udevice *dev); |
Simon Glass | 0118ce7 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 173 | int (*child_post_bind)(struct udevice *dev); |
Simon Glass | a327dee | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 174 | int (*child_pre_probe)(struct udevice *dev); |
| 175 | int (*child_post_remove)(struct udevice *dev); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 176 | int priv_auto_alloc_size; |
| 177 | int platdata_auto_alloc_size; |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 178 | int per_child_auto_alloc_size; |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 179 | int per_child_platdata_auto_alloc_size; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 180 | const void *ops; /* driver-specific operations */ |
Simon Glass | 00606d7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 181 | uint32_t flags; |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | /* Declare a new U-Boot driver */ |
| 185 | #define U_BOOT_DRIVER(__name) \ |
| 186 | ll_entry_declare(struct driver, __name, driver) |
| 187 | |
| 188 | /** |
| 189 | * dev_get_platdata() - Get the platform data for a device |
| 190 | * |
| 191 | * This checks that dev is not NULL, but no other checks for now |
| 192 | * |
| 193 | * @dev Device to check |
| 194 | * @return platform data, or NULL if none |
| 195 | */ |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 196 | void *dev_get_platdata(struct udevice *dev); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 197 | |
| 198 | /** |
Simon Glass | cdc133b | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 199 | * dev_get_parent_platdata() - Get the parent platform data for a device |
| 200 | * |
| 201 | * This checks that dev is not NULL, but no other checks for now |
| 202 | * |
| 203 | * @dev Device to check |
| 204 | * @return parent's platform data, or NULL if none |
| 205 | */ |
| 206 | void *dev_get_parent_platdata(struct udevice *dev); |
| 207 | |
| 208 | /** |
Simon Glass | e59f458 | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 209 | * dev_get_parentdata() - Get the parent data for a device |
| 210 | * |
| 211 | * The parent data is data stored in the device but owned by the parent. |
| 212 | * For example, a USB device may have parent data which contains information |
| 213 | * about how to talk to the device over USB. |
| 214 | * |
| 215 | * This checks that dev is not NULL, but no other checks for now |
| 216 | * |
| 217 | * @dev Device to check |
| 218 | * @return parent data, or NULL if none |
| 219 | */ |
| 220 | void *dev_get_parentdata(struct udevice *dev); |
| 221 | |
| 222 | /** |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 223 | * dev_get_priv() - Get the private data for a device |
| 224 | * |
| 225 | * This checks that dev is not NULL, but no other checks for now |
| 226 | * |
| 227 | * @dev Device to check |
| 228 | * @return private data, or NULL if none |
| 229 | */ |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 230 | void *dev_get_priv(struct udevice *dev); |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 231 | |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 232 | /** |
Simon Glass | 479728c | 2014-11-11 10:46:19 -0700 | [diff] [blame] | 233 | * struct dev_get_parent() - Get the parent of a device |
| 234 | * |
| 235 | * @child: Child to check |
| 236 | * @return parent of child, or NULL if this is the root device |
| 237 | */ |
| 238 | struct udevice *dev_get_parent(struct udevice *child); |
| 239 | |
| 240 | /** |
Simon Glass | 2ef249b | 2014-11-11 10:46:18 -0700 | [diff] [blame] | 241 | * dev_get_of_data() - get the device tree data used to bind a device |
| 242 | * |
| 243 | * When a device is bound using a device tree node, it matches a |
| 244 | * particular compatible string as in struct udevice_id. This function |
| 245 | * returns the associated data value for that compatible string |
| 246 | */ |
| 247 | ulong dev_get_of_data(struct udevice *dev); |
| 248 | |
Simon Glass | b367053 | 2015-01-25 08:27:04 -0700 | [diff] [blame] | 249 | /* |
| 250 | * device_get_uclass_id() - return the uclass ID of a device |
| 251 | * |
| 252 | * @dev: Device to check |
| 253 | * @return uclass ID for the device |
| 254 | */ |
| 255 | enum uclass_id device_get_uclass_id(struct udevice *dev); |
| 256 | |
Simon Glass | 2ef249b | 2014-11-11 10:46:18 -0700 | [diff] [blame] | 257 | /** |
Simon Glass | 997c87b | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 258 | * device_get_child() - Get the child of a device by index |
| 259 | * |
| 260 | * Returns the numbered child, 0 being the first. This does not use |
| 261 | * sequence numbers, only the natural order. |
| 262 | * |
| 263 | * @dev: Parent device to check |
| 264 | * @index: Child index |
| 265 | * @devp: Returns pointer to device |
| 266 | */ |
| 267 | int device_get_child(struct udevice *parent, int index, struct udevice **devp); |
| 268 | |
| 269 | /** |
Simon Glass | 5a66a8f | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 270 | * device_find_child_by_seq() - Find a child device based on a sequence |
| 271 | * |
| 272 | * This searches for a device with the given seq or req_seq. |
| 273 | * |
| 274 | * For seq, if an active device has this sequence it will be returned. |
| 275 | * If there is no such device then this will return -ENODEV. |
| 276 | * |
| 277 | * For req_seq, if a device (whether activated or not) has this req_seq |
| 278 | * value, that device will be returned. This is a strong indication that |
| 279 | * the device will receive that sequence when activated. |
| 280 | * |
| 281 | * @parent: Parent device |
| 282 | * @seq_or_req_seq: Sequence number to find (0=first) |
| 283 | * @find_req_seq: true to find req_seq, false to find seq |
| 284 | * @devp: Returns pointer to device (there is only one per for each seq). |
| 285 | * Set to NULL if none is found |
| 286 | * @return 0 if OK, -ve on error |
| 287 | */ |
| 288 | int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq, |
| 289 | bool find_req_seq, struct udevice **devp); |
| 290 | |
Simon Glass | 997c87b | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 291 | /** |
| 292 | * device_get_child_by_seq() - Get a child device based on a sequence |
| 293 | * |
| 294 | * If an active device has this sequence it will be returned. If there is no |
| 295 | * such device then this will check for a device that is requesting this |
| 296 | * sequence. |
| 297 | * |
| 298 | * The device is probed to activate it ready for use. |
| 299 | * |
| 300 | * @parent: Parent device |
| 301 | * @seq: Sequence number to find (0=first) |
| 302 | * @devp: Returns pointer to device (there is only one per for each seq) |
| 303 | * Set to NULL if none is found |
| 304 | * @return 0 if OK, -ve on error |
| 305 | */ |
| 306 | int device_get_child_by_seq(struct udevice *parent, int seq, |
| 307 | struct udevice **devp); |
| 308 | |
| 309 | /** |
| 310 | * device_find_child_by_of_offset() - Find a child device based on FDT offset |
| 311 | * |
| 312 | * Locates a child device by its device tree offset. |
| 313 | * |
| 314 | * @parent: Parent device |
| 315 | * @of_offset: Device tree offset to find |
| 316 | * @devp: Returns pointer to device if found, otherwise this is set to NULL |
| 317 | * @return 0 if OK, -ve on error |
| 318 | */ |
| 319 | int device_find_child_by_of_offset(struct udevice *parent, int of_offset, |
| 320 | struct udevice **devp); |
| 321 | |
| 322 | /** |
| 323 | * device_get_child_by_of_offset() - Get a child device based on FDT offset |
| 324 | * |
| 325 | * Locates a child device by its device tree offset. |
| 326 | * |
| 327 | * The device is probed to activate it ready for use. |
| 328 | * |
| 329 | * @parent: Parent device |
| 330 | * @of_offset: Device tree offset to find |
| 331 | * @devp: Returns pointer to device if found, otherwise this is set to NULL |
| 332 | * @return 0 if OK, -ve on error |
| 333 | */ |
| 334 | int device_get_child_by_of_offset(struct udevice *parent, int seq, |
| 335 | struct udevice **devp); |
| 336 | |
Simon Glass | a8981d4 | 2014-10-13 23:41:49 -0600 | [diff] [blame] | 337 | /** |
| 338 | * device_find_first_child() - Find the first child of a device |
| 339 | * |
| 340 | * @parent: Parent device to search |
| 341 | * @devp: Returns first child device, or NULL if none |
| 342 | * @return 0 |
| 343 | */ |
| 344 | int device_find_first_child(struct udevice *parent, struct udevice **devp); |
| 345 | |
| 346 | /** |
| 347 | * device_find_first_child() - Find the first child of a device |
| 348 | * |
| 349 | * @devp: Pointer to previous child device on entry. Returns pointer to next |
| 350 | * child device, or NULL if none |
| 351 | * @return 0 |
| 352 | */ |
| 353 | int device_find_next_child(struct udevice **devp); |
| 354 | |
Peng Fan | c9cac3f | 2015-02-10 14:46:32 +0800 | [diff] [blame^] | 355 | /** |
| 356 | * dev_get_addr() - Get the reg property of a device |
| 357 | * |
| 358 | * @dev: Pointer to a device |
| 359 | * |
| 360 | * @return addr |
| 361 | */ |
| 362 | fdt_addr_t dev_get_addr(struct udevice *dev); |
| 363 | |
Simon Glass | 6494d70 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 364 | #endif |