blob: 18296bb68614b9aeae25c46b5ff3c8306e8a36d1 [file] [log] [blame]
Simon Glassdd6ab882014-02-26 15:59:18 -07001/*
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 Fan99b72352015-02-10 14:46:32 +080015#include <fdtdec.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070016#include <linker_lists.h>
17#include <linux/list.h>
18
19struct 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 Glass4dbb5cf2014-06-11 23:29:54 -060025#define DM_FLAG_ALLOC_PDATA (1 << 1)
Simon Glassdd6ab882014-02-26 15:59:18 -070026
Simon Glassfef72b72014-07-23 06:55:03 -060027/* DM should init this device prior to relocation */
28#define DM_FLAG_PRE_RELOC (1 << 2)
29
Simon Glass11b61732015-01-25 08:27:01 -070030/* DM is responsible for allocating and freeing parent_platdata */
31#define DM_FLAG_ALLOC_PARENT_PDATA (1 << 3)
32
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020033/* DM is responsible for allocating and freeing uclass_platdata */
34#define DM_FLAG_ALLOC_UCLASS_PDATA (1 << 4)
35
Simon Glass825d3f92015-03-25 12:21:53 -060036/* Allocate driver private data on a DMA boundary */
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020037#define DM_FLAG_ALLOC_PRIV_DMA (1 << 5)
Simon Glass825d3f92015-03-25 12:21:53 -060038
Simon Glassdd6ab882014-02-26 15:59:18 -070039/**
Heiko Schocherb74fcb42014-05-22 12:43:05 +020040 * struct udevice - An instance of a driver
Simon Glassdd6ab882014-02-26 15:59:18 -070041 *
42 * This holds information about a device, which is a driver bound to a
43 * particular port or peripheral (essentially a driver instance).
44 *
45 * A device will come into existence through a 'bind' call, either due to
46 * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
47 * in the device tree (in which case of_offset is >= 0). In the latter case
48 * we translate the device tree information into platdata in a function
49 * implemented by the driver ofdata_to_platdata method (called just before the
50 * probe method if the device has a device tree node.
51 *
52 * All three of platdata, priv and uclass_priv can be allocated by the
53 * driver, or you can use the auto_alloc_size members of struct driver and
54 * struct uclass_driver to have driver model do this automatically.
55 *
56 * @driver: The driver used by this device
57 * @name: Name of device, typically the FDT node name
58 * @platdata: Configuration data for this device
Simon Glass11b61732015-01-25 08:27:01 -070059 * @parent_platdata: The parent bus's configuration data for this device
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020060 * @uclass_platdata: The uclass's configuration data for this device
Simon Glassdd6ab882014-02-26 15:59:18 -070061 * @of_offset: Device tree node offset for this device (- for none)
Simon Glass46227bd2015-03-25 12:21:55 -060062 * @driver_data: Driver data word for the entry that matched this device with
63 * its driver
Simon Glassdd6ab882014-02-26 15:59:18 -070064 * @parent: Parent of this device, or NULL for the top level device
65 * @priv: Private data for this device
66 * @uclass: Pointer to uclass for this device
67 * @uclass_priv: The uclass's private data for this device
Simon Glass60d971b2014-07-23 06:55:20 -060068 * @parent_priv: The parent's private data for this device
Simon Glassdd6ab882014-02-26 15:59:18 -070069 * @uclass_node: Used by uclass to link its devices
70 * @child_head: List of children of this device
71 * @sibling_node: Next device in list of all devices
72 * @flags: Flags for this device DM_FLAG_...
Simon Glassdb6f0202014-07-23 06:55:12 -060073 * @req_seq: Requested sequence number for this device (-1 = any)
Simon Glass47424ec2014-10-13 23:41:51 -060074 * @seq: Allocated sequence number for this device (-1 = none). This is set up
75 * when the device is probed and will be unique within the device's uclass.
Simon Glassdd6ab882014-02-26 15:59:18 -070076 */
Heiko Schocherb74fcb42014-05-22 12:43:05 +020077struct udevice {
Simon Glassa626dff2015-03-25 12:21:54 -060078 const struct driver *driver;
Simon Glassdd6ab882014-02-26 15:59:18 -070079 const char *name;
80 void *platdata;
Simon Glass11b61732015-01-25 08:27:01 -070081 void *parent_platdata;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020082 void *uclass_platdata;
Simon Glassdd6ab882014-02-26 15:59:18 -070083 int of_offset;
Simon Glass46227bd2015-03-25 12:21:55 -060084 ulong driver_data;
Heiko Schocherb74fcb42014-05-22 12:43:05 +020085 struct udevice *parent;
Simon Glassdd6ab882014-02-26 15:59:18 -070086 void *priv;
87 struct uclass *uclass;
88 void *uclass_priv;
Simon Glass60d971b2014-07-23 06:55:20 -060089 void *parent_priv;
Simon Glassdd6ab882014-02-26 15:59:18 -070090 struct list_head uclass_node;
91 struct list_head child_head;
92 struct list_head sibling_node;
93 uint32_t flags;
Simon Glassdb6f0202014-07-23 06:55:12 -060094 int req_seq;
95 int seq;
Simon Glassdd6ab882014-02-26 15:59:18 -070096};
97
Simon Glassdb6f0202014-07-23 06:55:12 -060098/* Maximum sequence number supported */
99#define DM_MAX_SEQ 999
100
Simon Glassdd6ab882014-02-26 15:59:18 -0700101/* Returns the operations for a device */
102#define device_get_ops(dev) (dev->driver->ops)
103
104/* Returns non-zero if the device is active (probed and not removed) */
105#define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED)
106
107/**
Simon Glass767827a2014-06-11 23:29:45 -0600108 * struct udevice_id - Lists the compatible strings supported by a driver
Simon Glassdd6ab882014-02-26 15:59:18 -0700109 * @compatible: Compatible string
110 * @data: Data for this compatible string
111 */
Simon Glass767827a2014-06-11 23:29:45 -0600112struct udevice_id {
Simon Glassdd6ab882014-02-26 15:59:18 -0700113 const char *compatible;
114 ulong data;
115};
116
Masahiro Yamadaf797bc22014-10-07 14:51:31 +0900117#ifdef CONFIG_OF_CONTROL
118#define of_match_ptr(_ptr) (_ptr)
119#else
120#define of_match_ptr(_ptr) NULL
121#endif /* CONFIG_OF_CONTROL */
122
Simon Glassdd6ab882014-02-26 15:59:18 -0700123/**
124 * struct driver - A driver for a feature or peripheral
125 *
126 * This holds methods for setting up a new device, and also removing it.
127 * The device needs information to set itself up - this is provided either
128 * by platdata or a device tree node (which we find by looking up
129 * matching compatible strings with of_match).
130 *
131 * Drivers all belong to a uclass, representing a class of devices of the
132 * same type. Common elements of the drivers can be implemented in the uclass,
133 * or the uclass can provide a consistent interface to the drivers within
134 * it.
135 *
136 * @name: Device name
137 * @id: Identiies the uclass we belong to
138 * @of_match: List of compatible strings to match, and any identifying data
139 * for each.
140 * @bind: Called to bind a device to its driver
141 * @probe: Called to probe a device, i.e. activate it
142 * @remove: Called to remove a device, i.e. de-activate it
143 * @unbind: Called to unbind a device from its driver
144 * @ofdata_to_platdata: Called before probe to decode device tree data
Simon Glassa4a51a02015-01-25 08:27:03 -0700145 * @child_post_bind: Called after a new child has been bound
Simon Glassd45560d2014-07-23 06:55:21 -0600146 * @child_pre_probe: Called before a child device is probed. The device has
147 * memory allocated but it has not yet been probed.
148 * @child_post_remove: Called after a child device is removed. The device
149 * has memory allocated but its device_remove() method has been called.
Simon Glassdd6ab882014-02-26 15:59:18 -0700150 * @priv_auto_alloc_size: If non-zero this is the size of the private data
151 * to be allocated in the device's ->priv pointer. If zero, then the driver
152 * is responsible for allocating any data required.
153 * @platdata_auto_alloc_size: If non-zero this is the size of the
154 * platform data to be allocated in the device's ->platdata pointer.
155 * This is typically only useful for device-tree-aware drivers (those with
156 * an of_match), since drivers which use platdata will have the data
157 * provided in the U_BOOT_DEVICE() instantiation.
Simon Glass60d971b2014-07-23 06:55:20 -0600158 * @per_child_auto_alloc_size: Each device can hold private data owned by
159 * its parent. If required this will be automatically allocated if this
160 * value is non-zero.
Simon Glass1586a842014-10-13 23:41:50 -0600161 * TODO(sjg@chromium.org): I'm considering dropping this, and just having
162 * device_probe_child() pass it in. So far the use case for allocating it
163 * is SPI, but I found that unsatisfactory. Since it is here I will leave it
164 * until things are clearer.
Simon Glass11b61732015-01-25 08:27:01 -0700165 * @per_child_platdata_auto_alloc_size: A bus likes to store information about
166 * its children. If non-zero this is the size of this data, to be allocated
167 * in the child's parent_platdata pointer.
Simon Glasscebcebb2014-07-23 06:55:17 -0600168 * @ops: Driver-specific operations. This is typically a list of function
Simon Glassdd6ab882014-02-26 15:59:18 -0700169 * pointers defined by the driver, to implement driver functions required by
170 * the uclass.
Simon Glassfef72b72014-07-23 06:55:03 -0600171 * @flags: driver flags - see DM_FLAGS_...
Simon Glassdd6ab882014-02-26 15:59:18 -0700172 */
173struct driver {
174 char *name;
175 enum uclass_id id;
Simon Glass767827a2014-06-11 23:29:45 -0600176 const struct udevice_id *of_match;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200177 int (*bind)(struct udevice *dev);
178 int (*probe)(struct udevice *dev);
179 int (*remove)(struct udevice *dev);
180 int (*unbind)(struct udevice *dev);
181 int (*ofdata_to_platdata)(struct udevice *dev);
Simon Glassa4a51a02015-01-25 08:27:03 -0700182 int (*child_post_bind)(struct udevice *dev);
Simon Glassd45560d2014-07-23 06:55:21 -0600183 int (*child_pre_probe)(struct udevice *dev);
184 int (*child_post_remove)(struct udevice *dev);
Simon Glassdd6ab882014-02-26 15:59:18 -0700185 int priv_auto_alloc_size;
186 int platdata_auto_alloc_size;
Simon Glass60d971b2014-07-23 06:55:20 -0600187 int per_child_auto_alloc_size;
Simon Glass11b61732015-01-25 08:27:01 -0700188 int per_child_platdata_auto_alloc_size;
Simon Glassdd6ab882014-02-26 15:59:18 -0700189 const void *ops; /* driver-specific operations */
Simon Glassfef72b72014-07-23 06:55:03 -0600190 uint32_t flags;
Simon Glassdd6ab882014-02-26 15:59:18 -0700191};
192
193/* Declare a new U-Boot driver */
194#define U_BOOT_DRIVER(__name) \
195 ll_entry_declare(struct driver, __name, driver)
196
197/**
198 * dev_get_platdata() - Get the platform data for a device
199 *
200 * This checks that dev is not NULL, but no other checks for now
201 *
202 * @dev Device to check
203 * @return platform data, or NULL if none
204 */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200205void *dev_get_platdata(struct udevice *dev);
Simon Glassdd6ab882014-02-26 15:59:18 -0700206
207/**
Simon Glass11b61732015-01-25 08:27:01 -0700208 * dev_get_parent_platdata() - Get the parent platform data for a device
209 *
210 * This checks that dev is not NULL, but no other checks for now
211 *
212 * @dev Device to check
213 * @return parent's platform data, or NULL if none
214 */
215void *dev_get_parent_platdata(struct udevice *dev);
216
217/**
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200218 * dev_get_uclass_platdata() - Get the uclass platform data for a device
219 *
220 * This checks that dev is not NULL, but no other checks for now
221 *
222 * @dev Device to check
223 * @return uclass's platform data, or NULL if none
224 */
225void *dev_get_uclass_platdata(struct udevice *dev);
226
227/**
Simon Glass60d971b2014-07-23 06:55:20 -0600228 * dev_get_parentdata() - Get the parent data for a device
229 *
230 * The parent data is data stored in the device but owned by the parent.
231 * For example, a USB device may have parent data which contains information
232 * about how to talk to the device over USB.
233 *
234 * This checks that dev is not NULL, but no other checks for now
235 *
236 * @dev Device to check
237 * @return parent data, or NULL if none
238 */
239void *dev_get_parentdata(struct udevice *dev);
240
241/**
Simon Glassdd6ab882014-02-26 15:59:18 -0700242 * dev_get_priv() - Get the private data for a device
243 *
244 * This checks that dev is not NULL, but no other checks for now
245 *
246 * @dev Device to check
247 * @return private data, or NULL if none
248 */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200249void *dev_get_priv(struct udevice *dev);
Simon Glassdd6ab882014-02-26 15:59:18 -0700250
Simon Glassdb6f0202014-07-23 06:55:12 -0600251/**
Simon Glass43f488a2014-11-11 10:46:19 -0700252 * struct dev_get_parent() - Get the parent of a device
253 *
254 * @child: Child to check
255 * @return parent of child, or NULL if this is the root device
256 */
257struct udevice *dev_get_parent(struct udevice *child);
258
259/**
Simon Glassde0977b2015-03-05 12:25:20 -0700260 * dev_get_uclass_priv() - Get the private uclass data for a device
261 *
262 * This checks that dev is not NULL, but no other checks for now
263 *
264 * @dev Device to check
265 * @return private uclass data for this device, or NULL if none
266 */
267void *dev_get_uclass_priv(struct udevice *dev);
268
269/**
Simon Glass46227bd2015-03-25 12:21:55 -0600270 * dev_get_driver_data() - get the driver data used to bind a device
Simon Glass70c3a0e2014-11-11 10:46:18 -0700271 *
272 * When a device is bound using a device tree node, it matches a
273 * particular compatible string as in struct udevice_id. This function
Simon Glass46227bd2015-03-25 12:21:55 -0600274 * returns the associated data value for that compatible string. This is
275 * the 'data' field in struct udevice_id.
276 *
277 * For USB devices, this is the driver_info field in struct usb_device_id.
278 *
279 * @dev: Device to check
Simon Glass70c3a0e2014-11-11 10:46:18 -0700280 */
Simon Glass46227bd2015-03-25 12:21:55 -0600281ulong dev_get_driver_data(struct udevice *dev);
Simon Glass70c3a0e2014-11-11 10:46:18 -0700282
Przemyslaw Marczakd3ef0d72015-04-15 13:07:24 +0200283/**
284 * dev_get_driver_ops() - get the device's driver's operations
285 *
286 * This checks that dev is not NULL, and returns the pointer to device's
287 * driver's operations.
288 *
289 * @dev: Device to check
290 * @return void pointer to driver's operations or NULL for NULL-dev or NULL-ops
291 */
292const void *dev_get_driver_ops(struct udevice *dev);
293
Simon Glass98fd5d12015-01-25 08:27:04 -0700294/*
295 * device_get_uclass_id() - return the uclass ID of a device
296 *
297 * @dev: Device to check
298 * @return uclass ID for the device
299 */
300enum uclass_id device_get_uclass_id(struct udevice *dev);
301
Przemyslaw Marczak5ed2e422015-04-15 13:07:25 +0200302/*
303 * dev_get_uclass_name() - return the uclass name of a device
304 *
305 * This checks that dev is not NULL.
306 *
307 * @dev: Device to check
308 * @return pointer to the uclass name for the device
309 */
310const char *dev_get_uclass_name(struct udevice *dev);
311
Simon Glass70c3a0e2014-11-11 10:46:18 -0700312/**
Simon Glass48d4e292014-07-23 06:55:19 -0600313 * device_get_child() - Get the child of a device by index
314 *
315 * Returns the numbered child, 0 being the first. This does not use
316 * sequence numbers, only the natural order.
317 *
318 * @dev: Parent device to check
319 * @index: Child index
320 * @devp: Returns pointer to device
321 */
322int device_get_child(struct udevice *parent, int index, struct udevice **devp);
323
324/**
Simon Glassdb6f0202014-07-23 06:55:12 -0600325 * device_find_child_by_seq() - Find a child device based on a sequence
326 *
327 * This searches for a device with the given seq or req_seq.
328 *
329 * For seq, if an active device has this sequence it will be returned.
330 * If there is no such device then this will return -ENODEV.
331 *
332 * For req_seq, if a device (whether activated or not) has this req_seq
333 * value, that device will be returned. This is a strong indication that
334 * the device will receive that sequence when activated.
335 *
336 * @parent: Parent device
337 * @seq_or_req_seq: Sequence number to find (0=first)
338 * @find_req_seq: true to find req_seq, false to find seq
339 * @devp: Returns pointer to device (there is only one per for each seq).
340 * Set to NULL if none is found
341 * @return 0 if OK, -ve on error
342 */
343int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
344 bool find_req_seq, struct udevice **devp);
345
Simon Glass48d4e292014-07-23 06:55:19 -0600346/**
347 * device_get_child_by_seq() - Get a child device based on a sequence
348 *
349 * If an active device has this sequence it will be returned. If there is no
350 * such device then this will check for a device that is requesting this
351 * sequence.
352 *
353 * The device is probed to activate it ready for use.
354 *
355 * @parent: Parent device
356 * @seq: Sequence number to find (0=first)
357 * @devp: Returns pointer to device (there is only one per for each seq)
358 * Set to NULL if none is found
359 * @return 0 if OK, -ve on error
360 */
361int device_get_child_by_seq(struct udevice *parent, int seq,
362 struct udevice **devp);
363
364/**
365 * device_find_child_by_of_offset() - Find a child device based on FDT offset
366 *
367 * Locates a child device by its device tree offset.
368 *
369 * @parent: Parent device
370 * @of_offset: Device tree offset to find
371 * @devp: Returns pointer to device if found, otherwise this is set to NULL
372 * @return 0 if OK, -ve on error
373 */
374int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
375 struct udevice **devp);
376
377/**
378 * device_get_child_by_of_offset() - Get a child device based on FDT offset
379 *
380 * Locates a child device by its device tree offset.
381 *
382 * The device is probed to activate it ready for use.
383 *
384 * @parent: Parent device
385 * @of_offset: Device tree offset to find
386 * @devp: Returns pointer to device if found, otherwise this is set to NULL
387 * @return 0 if OK, -ve on error
388 */
389int device_get_child_by_of_offset(struct udevice *parent, int seq,
390 struct udevice **devp);
391
Simon Glass44da7352014-10-13 23:41:49 -0600392/**
393 * device_find_first_child() - Find the first child of a device
394 *
395 * @parent: Parent device to search
396 * @devp: Returns first child device, or NULL if none
397 * @return 0
398 */
399int device_find_first_child(struct udevice *parent, struct udevice **devp);
400
401/**
402 * device_find_first_child() - Find the first child of a device
403 *
404 * @devp: Pointer to previous child device on entry. Returns pointer to next
405 * child device, or NULL if none
406 * @return 0
407 */
408int device_find_next_child(struct udevice **devp);
409
Peng Fan99b72352015-02-10 14:46:32 +0800410/**
411 * dev_get_addr() - Get the reg property of a device
412 *
413 * @dev: Pointer to a device
414 *
415 * @return addr
416 */
417fdt_addr_t dev_get_addr(struct udevice *dev);
418
Simon Glass40f829a2015-03-25 12:21:57 -0600419/**
420 * device_has_children() - check if a device has any children
421 *
422 * @dev: Device to check
423 * @return true if the device has one or more children
424 */
425bool device_has_children(struct udevice *dev);
426
427/**
428 * device_has_active_children() - check if a device has any active children
429 *
430 * @dev: Device to check
431 * @return true if the device has one or more children and at least one of
432 * them is active (probed).
433 */
434bool device_has_active_children(struct udevice *dev);
435
436/**
437 * device_is_last_sibling() - check if a device is the last sibling
438 *
439 * This function can be useful for display purposes, when special action needs
440 * to be taken when displaying the last sibling. This can happen when a tree
441 * view of devices is being displayed.
442 *
443 * @dev: Device to check
444 * @return true if there are no more siblings after this one - i.e. is it
445 * last in the list.
446 */
447bool device_is_last_sibling(struct udevice *dev);
448
Simon Glassdd6ab882014-02-26 15:59:18 -0700449#endif