2 * Copyright (c) 2013 Google, Inc
5 * Pavel Herrmann <morpheus.ibis@gmail.com>
6 * Marek Vasut <marex@denx.de>
8 * SPDX-License-Identifier: GPL-2.0+
14 #include <dm/uclass-id.h>
16 #include <linker_lists.h>
17 #include <linux/list.h>
21 /* Driver is active (probed). Cleared when it is removed */
22 #define DM_FLAG_ACTIVATED (1 << 0)
24 /* DM is responsible for allocating and freeing platdata */
25 #define DM_FLAG_ALLOC_PDATA (1 << 1)
27 /* DM should init this device prior to relocation */
28 #define DM_FLAG_PRE_RELOC (1 << 2)
30 /* DM is responsible for allocating and freeing parent_platdata */
31 #define DM_FLAG_ALLOC_PARENT_PDATA (1 << 3)
33 /* DM is responsible for allocating and freeing uclass_platdata */
34 #define DM_FLAG_ALLOC_UCLASS_PDATA (1 << 4)
36 /* Allocate driver private data on a DMA boundary */
37 #define DM_FLAG_ALLOC_PRIV_DMA (1 << 5)
40 #define DM_FLAG_BOUND (1 << 6)
43 * struct udevice - An instance of a driver
45 * This holds information about a device, which is a driver bound to a
46 * particular port or peripheral (essentially a driver instance).
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.
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.
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
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.
81 const struct driver *driver;
84 void *parent_platdata;
85 void *uclass_platdata;
88 struct udevice *parent;
90 struct uclass *uclass;
93 struct list_head uclass_node;
94 struct list_head child_head;
95 struct list_head sibling_node;
99 struct list_head devres_head;
102 /* Maximum sequence number supported */
103 #define DM_MAX_SEQ 999
105 /* Returns the operations for a device */
106 #define device_get_ops(dev) (dev->driver->ops)
108 /* Returns non-zero if the device is active (probed and not removed) */
109 #define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED)
112 * struct udevice_id - Lists the compatible strings supported by a driver
113 * @compatible: Compatible string
114 * @data: Data for this compatible string
117 const char *compatible;
121 #ifdef CONFIG_OF_CONTROL
122 #define of_match_ptr(_ptr) (_ptr)
124 #define of_match_ptr(_ptr) NULL
125 #endif /* CONFIG_OF_CONTROL */
128 * struct driver - A driver for a feature or peripheral
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).
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
141 * @id: Identiies the uclass we belong to
142 * @of_match: List of compatible strings to match, and any identifying data
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
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
175 * @flags: driver flags - see DM_FLAGS_...
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 */
197 /* Declare a new U-Boot driver */
198 #define U_BOOT_DRIVER(__name) \
199 ll_entry_declare(struct driver, __name, driver)
202 * dev_get_platdata() - Get the platform data for a device
204 * This checks that dev is not NULL, but no other checks for now
206 * @dev Device to check
207 * @return platform data, or NULL if none
209 void *dev_get_platdata(struct udevice *dev);
212 * dev_get_parent_platdata() - Get the parent platform data for a device
214 * This checks that dev is not NULL, but no other checks for now
216 * @dev Device to check
217 * @return parent's platform data, or NULL if none
219 void *dev_get_parent_platdata(struct udevice *dev);
222 * dev_get_uclass_platdata() - Get the uclass platform data for a device
224 * This checks that dev is not NULL, but no other checks for now
226 * @dev Device to check
227 * @return uclass's platform data, or NULL if none
229 void *dev_get_uclass_platdata(struct udevice *dev);
232 * dev_get_parentdata() - Get the parent data for a device
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.
238 * This checks that dev is not NULL, but no other checks for now
240 * @dev Device to check
241 * @return parent data, or NULL if none
243 void *dev_get_parentdata(struct udevice *dev);
246 * dev_get_priv() - Get the private data for a device
248 * This checks that dev is not NULL, but no other checks for now
250 * @dev Device to check
251 * @return private data, or NULL if none
253 void *dev_get_priv(struct udevice *dev);
256 * struct dev_get_parent() - Get the parent of a device
258 * @child: Child to check
259 * @return parent of child, or NULL if this is the root device
261 struct udevice *dev_get_parent(struct udevice *child);
264 * dev_get_uclass_priv() - Get the private uclass data for a device
266 * This checks that dev is not NULL, but no other checks for now
268 * @dev Device to check
269 * @return private uclass data for this device, or NULL if none
271 void *dev_get_uclass_priv(struct udevice *dev);
274 * dev_get_driver_data() - get the driver data used to bind a device
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.
281 * For USB devices, this is the driver_info field in struct usb_device_id.
283 * @dev: Device to check
285 ulong dev_get_driver_data(struct udevice *dev);
288 * dev_get_driver_ops() - get the device's driver's operations
290 * This checks that dev is not NULL, and returns the pointer to device's
291 * driver's operations.
293 * @dev: Device to check
294 * @return void pointer to driver's operations or NULL for NULL-dev or NULL-ops
296 const void *dev_get_driver_ops(struct udevice *dev);
299 * device_get_uclass_id() - return the uclass ID of a device
301 * @dev: Device to check
302 * @return uclass ID for the device
304 enum uclass_id device_get_uclass_id(struct udevice *dev);
307 * dev_get_uclass_name() - return the uclass name of a device
309 * This checks that dev is not NULL.
311 * @dev: Device to check
312 * @return pointer to the uclass name for the device
314 const char *dev_get_uclass_name(struct udevice *dev);
317 * device_get_child() - Get the child of a device by index
319 * Returns the numbered child, 0 being the first. This does not use
320 * sequence numbers, only the natural order.
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
328 int device_get_child(struct udevice *parent, int index, struct udevice **devp);
331 * device_find_child_by_seq() - Find a child device based on a sequence
333 * This searches for a device with the given seq or req_seq.
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.
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.
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
349 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
350 bool find_req_seq, struct udevice **devp);
353 * device_get_child_by_seq() - Get a child device based on a sequence
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
359 * The device is probed to activate it ready for use.
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
367 int device_get_child_by_seq(struct udevice *parent, int seq,
368 struct udevice **devp);
371 * device_find_child_by_of_offset() - Find a child device based on FDT offset
373 * Locates a child device by its device tree offset.
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
380 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
381 struct udevice **devp);
384 * device_get_child_by_of_offset() - Get a child device based on FDT offset
386 * Locates a child device by its device tree offset.
388 * The device is probed to activate it ready for use.
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
395 int device_get_child_by_of_offset(struct udevice *parent, int of_offset,
396 struct udevice **devp);
399 * device_get_global_by_of_offset() - Get a device based on FDT offset
401 * Locates a device by its device tree offset, searching globally throughout
402 * the all driver model devices.
404 * The device is probed to activate it ready for use.
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
410 int device_get_global_by_of_offset(int of_offset, struct udevice **devp);
413 * device_find_first_child() - Find the first child of a device
415 * @parent: Parent device to search
416 * @devp: Returns first child device, or NULL if none
419 int device_find_first_child(struct udevice *parent, struct udevice **devp);
422 * device_find_next_child() - Find the next child of a device
424 * @devp: Pointer to previous child device on entry. Returns pointer to next
425 * child device, or NULL if none
428 int device_find_next_child(struct udevice **devp);
431 * dev_get_addr() - Get the reg property of a device
433 * @dev: Pointer to a device
437 fdt_addr_t dev_get_addr(struct udevice *dev);
440 * device_has_children() - check if a device has any children
442 * @dev: Device to check
443 * @return true if the device has one or more children
445 bool device_has_children(struct udevice *dev);
448 * device_has_active_children() - check if a device has any active children
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).
454 bool device_has_active_children(struct udevice *dev);
457 * device_is_last_sibling() - check if a device is the last sibling
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.
463 * @dev: Device to check
464 * @return true if there are no more siblings after this one - i.e. is it
467 bool device_is_last_sibling(struct udevice *dev);
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);
473 #ifdef CONFIG_DEBUG_DEVRES
474 void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
476 #define _devres_alloc(release, size, gfp) \
477 __devres_alloc(release, size, gfp, #release)
479 void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
483 * devres_alloc - Allocate device resource data
484 * @release: Release function devres will be associated with
485 * @size: Allocation size
486 * @gfp: Allocation flags
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.
493 * Pointer to allocated devres on success, NULL on failure.
495 #define devres_alloc(release, size, gfp) \
496 _devres_alloc(release, size, gfp | __GFP_ZERO)
499 * devres_free - Free device resource data
500 * @res: Pointer to devres data to free
502 * Free devres created with devres_alloc().
504 void devres_free(void *res);
507 * devres_add - Register device resource
508 * @dev: Device to add resource to
509 * @res: Resource to register
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.
515 void devres_add(struct udevice *dev, void *res);
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
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
529 * Pointer to found devres, NULL if not found.
531 void *devres_find(struct udevice *dev, dr_release_t release,
532 dr_match_t match, void *match_data);
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
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.
546 * Pointer to found or added devres.
548 void *devres_get(struct udevice *dev, void *new_res,
549 dr_match_t match, void *match_data);
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
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
564 * Pointer to removed devres on success, NULL if not found.
566 void *devres_remove(struct udevice *dev, dr_release_t release,
567 dr_match_t match, void *match_data);
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
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.
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.
585 * 0 if devres is found and freed, -ENOENT if not found.
587 int devres_destroy(struct udevice *dev, dr_release_t release,
588 dr_match_t match, void *match_data);
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
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.
603 * 0 if devres is found and freed, -ENOENT if not found.
605 int devres_release(struct udevice *dev, dr_release_t release,
606 dr_match_t match, void *match_data);