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