device.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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. #ifndef _DM_DEVICE_H
  11. #define _DM_DEVICE_H
  12. #include <dm/ofnode.h>
  13. #include <dm/uclass-id.h>
  14. #include <fdtdec.h>
  15. #include <linker_lists.h>
  16. #include <linux/compat.h>
  17. #include <linux/kernel.h>
  18. #include <linux/list.h>
  19. #include <linux/printk.h>
  20. struct driver_info;
  21. /* Driver is active (probed). Cleared when it is removed */
  22. #define DM_FLAG_ACTIVATED (1 << 0)
  23. /* DM is responsible for allocating and freeing platdata */
  24. #define DM_FLAG_ALLOC_PDATA (1 << 1)
  25. /* DM should init this device prior to relocation */
  26. #define DM_FLAG_PRE_RELOC (1 << 2)
  27. /* DM is responsible for allocating and freeing parent_platdata */
  28. #define DM_FLAG_ALLOC_PARENT_PDATA (1 << 3)
  29. /* DM is responsible for allocating and freeing uclass_platdata */
  30. #define DM_FLAG_ALLOC_UCLASS_PDATA (1 << 4)
  31. /* Allocate driver private data on a DMA boundary */
  32. #define DM_FLAG_ALLOC_PRIV_DMA (1 << 5)
  33. /* Device is bound */
  34. #define DM_FLAG_BOUND (1 << 6)
  35. /* Device name is allocated and should be freed on unbind() */
  36. #define DM_FLAG_NAME_ALLOCED (1 << 7)
  37. #define DM_FLAG_OF_PLATDATA (1 << 8)
  38. /*
  39. * Call driver remove function to stop currently active DMA transfers or
  40. * give DMA buffers back to the HW / controller. This may be needed for
  41. * some drivers to do some final stage cleanup before the OS is called
  42. * (U-Boot exit)
  43. */
  44. #define DM_FLAG_ACTIVE_DMA (1 << 9)
  45. /*
  46. * Call driver remove function to do some final configuration, before
  47. * U-Boot exits and the OS is started
  48. */
  49. #define DM_FLAG_OS_PREPARE (1 << 10)
  50. /*
  51. * One or multiple of these flags are passed to device_remove() so that
  52. * a selective device removal as specified by the remove-stage and the
  53. * driver flags can be done.
  54. */
  55. enum {
  56. /* Normal remove, remove all devices */
  57. DM_REMOVE_NORMAL = 1 << 0,
  58. /* Remove devices with active DMA */
  59. DM_REMOVE_ACTIVE_DMA = DM_FLAG_ACTIVE_DMA,
  60. /* Remove devices which need some final OS preparation steps */
  61. DM_REMOVE_OS_PREPARE = DM_FLAG_OS_PREPARE,
  62. /* Add more use cases here */
  63. /* Remove devices with any active flag */
  64. DM_REMOVE_ACTIVE_ALL = DM_REMOVE_ACTIVE_DMA | DM_REMOVE_OS_PREPARE,
  65. };
  66. /**
  67. * struct udevice - An instance of a driver
  68. *
  69. * This holds information about a device, which is a driver bound to a
  70. * particular port or peripheral (essentially a driver instance).
  71. *
  72. * A device will come into existence through a 'bind' call, either due to
  73. * a U_BOOT_DEVICE() macro (in which case platdata is non-NULL) or a node
  74. * in the device tree (in which case of_offset is >= 0). In the latter case
  75. * we translate the device tree information into platdata in a function
  76. * implemented by the driver ofdata_to_platdata method (called just before the
  77. * probe method if the device has a device tree node.
  78. *
  79. * All three of platdata, priv and uclass_priv can be allocated by the
  80. * driver, or you can use the auto_alloc_size members of struct driver and
  81. * struct uclass_driver to have driver model do this automatically.
  82. *
  83. * @driver: The driver used by this device
  84. * @name: Name of device, typically the FDT node name
  85. * @platdata: Configuration data for this device
  86. * @parent_platdata: The parent bus's configuration data for this device
  87. * @uclass_platdata: The uclass's configuration data for this device
  88. * @node: Reference to device tree node for this device
  89. * @driver_data: Driver data word for the entry that matched this device with
  90. * its driver
  91. * @parent: Parent of this device, or NULL for the top level device
  92. * @priv: Private data for this device
  93. * @uclass: Pointer to uclass for this device
  94. * @uclass_priv: The uclass's private data for this device
  95. * @parent_priv: The parent's private data for this device
  96. * @uclass_node: Used by uclass to link its devices
  97. * @child_head: List of children of this device
  98. * @sibling_node: Next device in list of all devices
  99. * @flags: Flags for this device DM_FLAG_...
  100. * @req_seq: Requested sequence number for this device (-1 = any)
  101. * @seq: Allocated sequence number for this device (-1 = none). This is set up
  102. * when the device is probed and will be unique within the device's uclass.
  103. * @devres_head: List of memory allocations associated with this device.
  104. * When CONFIG_DEVRES is enabled, devm_kmalloc() and friends will
  105. * add to this list. Memory so-allocated will be freed
  106. * automatically when the device is removed / unbound
  107. */
  108. struct udevice {
  109. const struct driver *driver;
  110. const char *name;
  111. void *platdata;
  112. void *parent_platdata;
  113. void *uclass_platdata;
  114. ofnode node;
  115. ulong driver_data;
  116. struct udevice *parent;
  117. void *priv;
  118. struct uclass *uclass;
  119. void *uclass_priv;
  120. void *parent_priv;
  121. struct list_head uclass_node;
  122. struct list_head child_head;
  123. struct list_head sibling_node;
  124. uint32_t flags;
  125. int req_seq;
  126. int seq;
  127. #ifdef CONFIG_DEVRES
  128. struct list_head devres_head;
  129. #endif
  130. };
  131. /* Maximum sequence number supported */
  132. #define DM_MAX_SEQ 999
  133. /* Returns the operations for a device */
  134. #define device_get_ops(dev) (dev->driver->ops)
  135. /* Returns non-zero if the device is active (probed and not removed) */
  136. #define device_active(dev) ((dev)->flags & DM_FLAG_ACTIVATED)
  137. static inline int dev_of_offset(const struct udevice *dev)
  138. {
  139. return ofnode_to_offset(dev->node);
  140. }
  141. static inline void dev_set_of_offset(struct udevice *dev, int of_offset)
  142. {
  143. dev->node = offset_to_ofnode(of_offset);
  144. }
  145. static inline bool dev_has_of_node(struct udevice *dev)
  146. {
  147. return ofnode_valid(dev->node);
  148. }
  149. /**
  150. * struct udevice_id - Lists the compatible strings supported by a driver
  151. * @compatible: Compatible string
  152. * @data: Data for this compatible string
  153. */
  154. struct udevice_id {
  155. const char *compatible;
  156. ulong data;
  157. };
  158. #if CONFIG_IS_ENABLED(OF_CONTROL)
  159. #define of_match_ptr(_ptr) (_ptr)
  160. #else
  161. #define of_match_ptr(_ptr) NULL
  162. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  163. /**
  164. * struct driver - A driver for a feature or peripheral
  165. *
  166. * This holds methods for setting up a new device, and also removing it.
  167. * The device needs information to set itself up - this is provided either
  168. * by platdata or a device tree node (which we find by looking up
  169. * matching compatible strings with of_match).
  170. *
  171. * Drivers all belong to a uclass, representing a class of devices of the
  172. * same type. Common elements of the drivers can be implemented in the uclass,
  173. * or the uclass can provide a consistent interface to the drivers within
  174. * it.
  175. *
  176. * @name: Device name
  177. * @id: Identiies the uclass we belong to
  178. * @of_match: List of compatible strings to match, and any identifying data
  179. * for each.
  180. * @bind: Called to bind a device to its driver
  181. * @probe: Called to probe a device, i.e. activate it
  182. * @remove: Called to remove a device, i.e. de-activate it
  183. * @unbind: Called to unbind a device from its driver
  184. * @ofdata_to_platdata: Called before probe to decode device tree data
  185. * @child_post_bind: Called after a new child has been bound
  186. * @child_pre_probe: Called before a child device is probed. The device has
  187. * memory allocated but it has not yet been probed.
  188. * @child_post_remove: Called after a child device is removed. The device
  189. * has memory allocated but its device_remove() method has been called.
  190. * @priv_auto_alloc_size: If non-zero this is the size of the private data
  191. * to be allocated in the device's ->priv pointer. If zero, then the driver
  192. * is responsible for allocating any data required.
  193. * @platdata_auto_alloc_size: If non-zero this is the size of the
  194. * platform data to be allocated in the device's ->platdata pointer.
  195. * This is typically only useful for device-tree-aware drivers (those with
  196. * an of_match), since drivers which use platdata will have the data
  197. * provided in the U_BOOT_DEVICE() instantiation.
  198. * @per_child_auto_alloc_size: Each device can hold private data owned by
  199. * its parent. If required this will be automatically allocated if this
  200. * value is non-zero.
  201. * @per_child_platdata_auto_alloc_size: A bus likes to store information about
  202. * its children. If non-zero this is the size of this data, to be allocated
  203. * in the child's parent_platdata pointer.
  204. * @ops: Driver-specific operations. This is typically a list of function
  205. * pointers defined by the driver, to implement driver functions required by
  206. * the uclass.
  207. * @flags: driver flags - see DM_FLAGS_...
  208. */
  209. struct driver {
  210. char *name;
  211. enum uclass_id id;
  212. const struct udevice_id *of_match;
  213. int (*bind)(struct udevice *dev);
  214. int (*probe)(struct udevice *dev);
  215. int (*remove)(struct udevice *dev);
  216. int (*unbind)(struct udevice *dev);
  217. int (*ofdata_to_platdata)(struct udevice *dev);
  218. int (*child_post_bind)(struct udevice *dev);
  219. int (*child_pre_probe)(struct udevice *dev);
  220. int (*child_post_remove)(struct udevice *dev);
  221. int priv_auto_alloc_size;
  222. int platdata_auto_alloc_size;
  223. int per_child_auto_alloc_size;
  224. int per_child_platdata_auto_alloc_size;
  225. const void *ops; /* driver-specific operations */
  226. uint32_t flags;
  227. };
  228. /* Declare a new U-Boot driver */
  229. #define U_BOOT_DRIVER(__name) \
  230. ll_entry_declare(struct driver, __name, driver)
  231. /* Get a pointer to a given driver */
  232. #define DM_GET_DRIVER(__name) \
  233. ll_entry_get(struct driver, __name, driver)
  234. /**
  235. * dev_get_platdata() - Get the platform data for a device
  236. *
  237. * This checks that dev is not NULL, but no other checks for now
  238. *
  239. * @dev Device to check
  240. * @return platform data, or NULL if none
  241. */
  242. void *dev_get_platdata(struct udevice *dev);
  243. /**
  244. * dev_get_parent_platdata() - Get the parent platform data for a device
  245. *
  246. * This checks that dev is not NULL, but no other checks for now
  247. *
  248. * @dev Device to check
  249. * @return parent's platform data, or NULL if none
  250. */
  251. void *dev_get_parent_platdata(struct udevice *dev);
  252. /**
  253. * dev_get_uclass_platdata() - Get the uclass platform data for a device
  254. *
  255. * This checks that dev is not NULL, but no other checks for now
  256. *
  257. * @dev Device to check
  258. * @return uclass's platform data, or NULL if none
  259. */
  260. void *dev_get_uclass_platdata(struct udevice *dev);
  261. /**
  262. * dev_get_priv() - Get the private data for a device
  263. *
  264. * This checks that dev is not NULL, but no other checks for now
  265. *
  266. * @dev Device to check
  267. * @return private data, or NULL if none
  268. */
  269. void *dev_get_priv(struct udevice *dev);
  270. /**
  271. * dev_get_parent_priv() - Get the parent private data for a device
  272. *
  273. * The parent private data is data stored in the device but owned by the
  274. * parent. For example, a USB device may have parent data which contains
  275. * information about how to talk to the device over USB.
  276. *
  277. * This checks that dev is not NULL, but no other checks for now
  278. *
  279. * @dev Device to check
  280. * @return parent data, or NULL if none
  281. */
  282. void *dev_get_parent_priv(struct udevice *dev);
  283. /**
  284. * dev_get_uclass_priv() - Get the private uclass data for a device
  285. *
  286. * This checks that dev is not NULL, but no other checks for now
  287. *
  288. * @dev Device to check
  289. * @return private uclass data for this device, or NULL if none
  290. */
  291. void *dev_get_uclass_priv(struct udevice *dev);
  292. /**
  293. * struct dev_get_parent() - Get the parent of a device
  294. *
  295. * @child: Child to check
  296. * @return parent of child, or NULL if this is the root device
  297. */
  298. struct udevice *dev_get_parent(struct udevice *child);
  299. /**
  300. * dev_get_driver_data() - get the driver data used to bind a device
  301. *
  302. * When a device is bound using a device tree node, it matches a
  303. * particular compatible string in struct udevice_id. This function
  304. * returns the associated data value for that compatible string. This is
  305. * the 'data' field in struct udevice_id.
  306. *
  307. * As an example, consider this structure:
  308. * static const struct udevice_id tegra_i2c_ids[] = {
  309. * { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 },
  310. * { .compatible = "nvidia,tegra20-i2c", .data = TYPE_STD },
  311. * { .compatible = "nvidia,tegra20-i2c-dvc", .data = TYPE_DVC },
  312. * { }
  313. * };
  314. *
  315. * When driver model finds a driver for this it will store the 'data' value
  316. * corresponding to the compatible string it matches. This function returns
  317. * that value. This allows the driver to handle several variants of a device.
  318. *
  319. * For USB devices, this is the driver_info field in struct usb_device_id.
  320. *
  321. * @dev: Device to check
  322. * @return driver data (0 if none is provided)
  323. */
  324. ulong dev_get_driver_data(struct udevice *dev);
  325. /**
  326. * dev_get_driver_ops() - get the device's driver's operations
  327. *
  328. * This checks that dev is not NULL, and returns the pointer to device's
  329. * driver's operations.
  330. *
  331. * @dev: Device to check
  332. * @return void pointer to driver's operations or NULL for NULL-dev or NULL-ops
  333. */
  334. const void *dev_get_driver_ops(struct udevice *dev);
  335. /**
  336. * device_get_uclass_id() - return the uclass ID of a device
  337. *
  338. * @dev: Device to check
  339. * @return uclass ID for the device
  340. */
  341. enum uclass_id device_get_uclass_id(struct udevice *dev);
  342. /**
  343. * dev_get_uclass_name() - return the uclass name of a device
  344. *
  345. * This checks that dev is not NULL.
  346. *
  347. * @dev: Device to check
  348. * @return pointer to the uclass name for the device
  349. */
  350. const char *dev_get_uclass_name(struct udevice *dev);
  351. /**
  352. * device_get_child() - Get the child of a device by index
  353. *
  354. * Returns the numbered child, 0 being the first. This does not use
  355. * sequence numbers, only the natural order.
  356. *
  357. * @dev: Parent device to check
  358. * @index: Child index
  359. * @devp: Returns pointer to device
  360. * @return 0 if OK, -ENODEV if no such device, other error if the device fails
  361. * to probe
  362. */
  363. int device_get_child(struct udevice *parent, int index, struct udevice **devp);
  364. /**
  365. * device_find_child_by_seq() - Find a child device based on a sequence
  366. *
  367. * This searches for a device with the given seq or req_seq.
  368. *
  369. * For seq, if an active device has this sequence it will be returned.
  370. * If there is no such device then this will return -ENODEV.
  371. *
  372. * For req_seq, if a device (whether activated or not) has this req_seq
  373. * value, that device will be returned. This is a strong indication that
  374. * the device will receive that sequence when activated.
  375. *
  376. * @parent: Parent device
  377. * @seq_or_req_seq: Sequence number to find (0=first)
  378. * @find_req_seq: true to find req_seq, false to find seq
  379. * @devp: Returns pointer to device (there is only one per for each seq).
  380. * Set to NULL if none is found
  381. * @return 0 if OK, -ve on error
  382. */
  383. int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
  384. bool find_req_seq, struct udevice **devp);
  385. /**
  386. * device_get_child_by_seq() - Get a child device based on a sequence
  387. *
  388. * If an active device has this sequence it will be returned. If there is no
  389. * such device then this will check for a device that is requesting this
  390. * sequence.
  391. *
  392. * The device is probed to activate it ready for use.
  393. *
  394. * @parent: Parent device
  395. * @seq: Sequence number to find (0=first)
  396. * @devp: Returns pointer to device (there is only one per for each seq)
  397. * Set to NULL if none is found
  398. * @return 0 if OK, -ve on error
  399. */
  400. int device_get_child_by_seq(struct udevice *parent, int seq,
  401. struct udevice **devp);
  402. /**
  403. * device_find_child_by_of_offset() - Find a child device based on FDT offset
  404. *
  405. * Locates a child device by its device tree offset.
  406. *
  407. * @parent: Parent device
  408. * @of_offset: Device tree offset to find
  409. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  410. * @return 0 if OK, -ve on error
  411. */
  412. int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
  413. struct udevice **devp);
  414. /**
  415. * device_get_child_by_of_offset() - Get a child device based on FDT offset
  416. *
  417. * Locates a child device by its device tree offset.
  418. *
  419. * The device is probed to activate it ready for use.
  420. *
  421. * @parent: Parent device
  422. * @of_offset: Device tree offset to find
  423. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  424. * @return 0 if OK, -ve on error
  425. */
  426. int device_get_child_by_of_offset(struct udevice *parent, int of_offset,
  427. struct udevice **devp);
  428. /**
  429. * device_get_global_by_of_offset() - Get a device based on FDT offset
  430. *
  431. * Locates a device by its device tree offset, searching globally throughout
  432. * the all driver model devices.
  433. *
  434. * The device is probed to activate it ready for use.
  435. *
  436. * @of_offset: Device tree offset to find
  437. * @devp: Returns pointer to device if found, otherwise this is set to NULL
  438. * @return 0 if OK, -ve on error
  439. */
  440. int device_get_global_by_of_offset(int of_offset, struct udevice **devp);
  441. /**
  442. * device_find_first_child() - Find the first child of a device
  443. *
  444. * @parent: Parent device to search
  445. * @devp: Returns first child device, or NULL if none
  446. * @return 0
  447. */
  448. int device_find_first_child(struct udevice *parent, struct udevice **devp);
  449. /**
  450. * device_find_next_child() - Find the next child of a device
  451. *
  452. * @devp: Pointer to previous child device on entry. Returns pointer to next
  453. * child device, or NULL if none
  454. * @return 0
  455. */
  456. int device_find_next_child(struct udevice **devp);
  457. /**
  458. * device_has_children() - check if a device has any children
  459. *
  460. * @dev: Device to check
  461. * @return true if the device has one or more children
  462. */
  463. bool device_has_children(struct udevice *dev);
  464. /**
  465. * device_has_active_children() - check if a device has any active children
  466. *
  467. * @dev: Device to check
  468. * @return true if the device has one or more children and at least one of
  469. * them is active (probed).
  470. */
  471. bool device_has_active_children(struct udevice *dev);
  472. /**
  473. * device_is_last_sibling() - check if a device is the last sibling
  474. *
  475. * This function can be useful for display purposes, when special action needs
  476. * to be taken when displaying the last sibling. This can happen when a tree
  477. * view of devices is being displayed.
  478. *
  479. * @dev: Device to check
  480. * @return true if there are no more siblings after this one - i.e. is it
  481. * last in the list.
  482. */
  483. bool device_is_last_sibling(struct udevice *dev);
  484. /**
  485. * device_set_name() - set the name of a device
  486. *
  487. * This must be called in the device's bind() method and no later. Normally
  488. * this is unnecessary but for probed devices which don't get a useful name
  489. * this function can be helpful.
  490. *
  491. * The name is allocated and will be freed automatically when the device is
  492. * unbound.
  493. *
  494. * @dev: Device to update
  495. * @name: New name (this string is allocated new memory and attached to
  496. * the device)
  497. * @return 0 if OK, -ENOMEM if there is not enough memory to allocate the
  498. * string
  499. */
  500. int device_set_name(struct udevice *dev, const char *name);
  501. /**
  502. * device_set_name_alloced() - note that a device name is allocated
  503. *
  504. * This sets the DM_FLAG_NAME_ALLOCED flag for the device, so that when it is
  505. * unbound the name will be freed. This avoids memory leaks.
  506. *
  507. * @dev: Device to update
  508. */
  509. void device_set_name_alloced(struct udevice *dev);
  510. /**
  511. * device_is_compatible() - check if the device is compatible with the compat
  512. *
  513. * This allows to check whether the device is comaptible with the compat.
  514. *
  515. * @dev: udevice pointer for which compatible needs to be verified.
  516. * @compat: Compatible string which needs to verified in the given
  517. * device
  518. * @return true if OK, false if the compatible is not found
  519. */
  520. bool device_is_compatible(struct udevice *dev, const char *compat);
  521. /**
  522. * of_machine_is_compatible() - check if the machine is compatible with
  523. * the compat
  524. *
  525. * This allows to check whether the machine is comaptible with the compat.
  526. *
  527. * @compat: Compatible string which needs to verified
  528. * @return true if OK, false if the compatible is not found
  529. */
  530. bool of_machine_is_compatible(const char *compat);
  531. /**
  532. * device_is_on_pci_bus - Test if a device is on a PCI bus
  533. *
  534. * @dev: device to test
  535. * @return: true if it is on a PCI bus, false otherwise
  536. */
  537. static inline bool device_is_on_pci_bus(struct udevice *dev)
  538. {
  539. return device_get_uclass_id(dev->parent) == UCLASS_PCI;
  540. }
  541. /**
  542. * device_foreach_child_safe() - iterate through child devices safely
  543. *
  544. * This allows the @pos child to be removed in the loop if required.
  545. *
  546. * @pos: struct udevice * for the current device
  547. * @next: struct udevice * for the next device
  548. * @parent: parent device to scan
  549. */
  550. #define device_foreach_child_safe(pos, next, parent) \
  551. list_for_each_entry_safe(pos, next, &parent->child_head, sibling_node)
  552. /**
  553. * dm_scan_fdt_dev() - Bind child device in a the device tree
  554. *
  555. * This handles device which have sub-nodes in the device tree. It scans all
  556. * sub-nodes and binds drivers for each node where a driver can be found.
  557. *
  558. * If this is called prior to relocation, only pre-relocation devices will be
  559. * bound (those marked with u-boot,dm-pre-reloc in the device tree, or where
  560. * the driver has the DM_FLAG_PRE_RELOC flag set). Otherwise, all devices will
  561. * be bound.
  562. *
  563. * @dev: Device to scan
  564. * @return 0 if OK, -ve on error
  565. */
  566. int dm_scan_fdt_dev(struct udevice *dev);
  567. /* device resource management */
  568. typedef void (*dr_release_t)(struct udevice *dev, void *res);
  569. typedef int (*dr_match_t)(struct udevice *dev, void *res, void *match_data);
  570. #ifdef CONFIG_DEVRES
  571. #ifdef CONFIG_DEBUG_DEVRES
  572. void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
  573. const char *name);
  574. #define _devres_alloc(release, size, gfp) \
  575. __devres_alloc(release, size, gfp, #release)
  576. #else
  577. void *_devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
  578. #endif
  579. /**
  580. * devres_alloc() - Allocate device resource data
  581. * @release: Release function devres will be associated with
  582. * @size: Allocation size
  583. * @gfp: Allocation flags
  584. *
  585. * Allocate devres of @size bytes. The allocated area is associated
  586. * with @release. The returned pointer can be passed to
  587. * other devres_*() functions.
  588. *
  589. * RETURNS:
  590. * Pointer to allocated devres on success, NULL on failure.
  591. */
  592. #define devres_alloc(release, size, gfp) \
  593. _devres_alloc(release, size, gfp | __GFP_ZERO)
  594. /**
  595. * devres_free() - Free device resource data
  596. * @res: Pointer to devres data to free
  597. *
  598. * Free devres created with devres_alloc().
  599. */
  600. void devres_free(void *res);
  601. /**
  602. * devres_add() - Register device resource
  603. * @dev: Device to add resource to
  604. * @res: Resource to register
  605. *
  606. * Register devres @res to @dev. @res should have been allocated
  607. * using devres_alloc(). On driver detach, the associated release
  608. * function will be invoked and devres will be freed automatically.
  609. */
  610. void devres_add(struct udevice *dev, void *res);
  611. /**
  612. * devres_find() - Find device resource
  613. * @dev: Device to lookup resource from
  614. * @release: Look for resources associated with this release function
  615. * @match: Match function (optional)
  616. * @match_data: Data for the match function
  617. *
  618. * Find the latest devres of @dev which is associated with @release
  619. * and for which @match returns 1. If @match is NULL, it's considered
  620. * to match all.
  621. *
  622. * @return pointer to found devres, NULL if not found.
  623. */
  624. void *devres_find(struct udevice *dev, dr_release_t release,
  625. dr_match_t match, void *match_data);
  626. /**
  627. * devres_get() - Find devres, if non-existent, add one atomically
  628. * @dev: Device to lookup or add devres for
  629. * @new_res: Pointer to new initialized devres to add if not found
  630. * @match: Match function (optional)
  631. * @match_data: Data for the match function
  632. *
  633. * Find the latest devres of @dev which has the same release function
  634. * as @new_res and for which @match return 1. If found, @new_res is
  635. * freed; otherwise, @new_res is added atomically.
  636. *
  637. * @return ointer to found or added devres.
  638. */
  639. void *devres_get(struct udevice *dev, void *new_res,
  640. dr_match_t match, void *match_data);
  641. /**
  642. * devres_remove() - Find a device resource and remove it
  643. * @dev: Device to find resource from
  644. * @release: Look for resources associated with this release function
  645. * @match: Match function (optional)
  646. * @match_data: Data for the match function
  647. *
  648. * Find the latest devres of @dev associated with @release and for
  649. * which @match returns 1. If @match is NULL, it's considered to
  650. * match all. If found, the resource is removed atomically and
  651. * returned.
  652. *
  653. * @return ointer to removed devres on success, NULL if not found.
  654. */
  655. void *devres_remove(struct udevice *dev, dr_release_t release,
  656. dr_match_t match, void *match_data);
  657. /**
  658. * devres_destroy() - Find a device resource and destroy it
  659. * @dev: Device to find resource from
  660. * @release: Look for resources associated with this release function
  661. * @match: Match function (optional)
  662. * @match_data: Data for the match function
  663. *
  664. * Find the latest devres of @dev associated with @release and for
  665. * which @match returns 1. If @match is NULL, it's considered to
  666. * match all. If found, the resource is removed atomically and freed.
  667. *
  668. * Note that the release function for the resource will not be called,
  669. * only the devres-allocated data will be freed. The caller becomes
  670. * responsible for freeing any other data.
  671. *
  672. * @return 0 if devres is found and freed, -ENOENT if not found.
  673. */
  674. int devres_destroy(struct udevice *dev, dr_release_t release,
  675. dr_match_t match, void *match_data);
  676. /**
  677. * devres_release() - Find a device resource and destroy it, calling release
  678. * @dev: Device to find resource from
  679. * @release: Look for resources associated with this release function
  680. * @match: Match function (optional)
  681. * @match_data: Data for the match function
  682. *
  683. * Find the latest devres of @dev associated with @release and for
  684. * which @match returns 1. If @match is NULL, it's considered to
  685. * match all. If found, the resource is removed atomically, the
  686. * release function called and the resource freed.
  687. *
  688. * @return 0 if devres is found and freed, -ENOENT if not found.
  689. */
  690. int devres_release(struct udevice *dev, dr_release_t release,
  691. dr_match_t match, void *match_data);
  692. /* managed devm_k.alloc/kfree for device drivers */
  693. /**
  694. * devm_kmalloc() - Resource-managed kmalloc
  695. * @dev: Device to allocate memory for
  696. * @size: Allocation size
  697. * @gfp: Allocation gfp flags
  698. *
  699. * Managed kmalloc. Memory allocated with this function is
  700. * automatically freed on driver detach. Like all other devres
  701. * resources, guaranteed alignment is unsigned long long.
  702. *
  703. * @return pointer to allocated memory on success, NULL on failure.
  704. */
  705. void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp);
  706. static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
  707. {
  708. return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
  709. }
  710. static inline void *devm_kmalloc_array(struct udevice *dev,
  711. size_t n, size_t size, gfp_t flags)
  712. {
  713. if (size != 0 && n > SIZE_MAX / size)
  714. return NULL;
  715. return devm_kmalloc(dev, n * size, flags);
  716. }
  717. static inline void *devm_kcalloc(struct udevice *dev,
  718. size_t n, size_t size, gfp_t flags)
  719. {
  720. return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
  721. }
  722. /**
  723. * devm_kfree() - Resource-managed kfree
  724. * @dev: Device this memory belongs to
  725. * @ptr: Memory to free
  726. *
  727. * Free memory allocated with devm_kmalloc().
  728. */
  729. void devm_kfree(struct udevice *dev, void *ptr);
  730. #else /* ! CONFIG_DEVRES */
  731. static inline void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
  732. {
  733. return kzalloc(size, gfp);
  734. }
  735. static inline void devres_free(void *res)
  736. {
  737. kfree(res);
  738. }
  739. static inline void devres_add(struct udevice *dev, void *res)
  740. {
  741. }
  742. static inline void *devres_find(struct udevice *dev, dr_release_t release,
  743. dr_match_t match, void *match_data)
  744. {
  745. return NULL;
  746. }
  747. static inline void *devres_get(struct udevice *dev, void *new_res,
  748. dr_match_t match, void *match_data)
  749. {
  750. return NULL;
  751. }
  752. static inline void *devres_remove(struct udevice *dev, dr_release_t release,
  753. dr_match_t match, void *match_data)
  754. {
  755. return NULL;
  756. }
  757. static inline int devres_destroy(struct udevice *dev, dr_release_t release,
  758. dr_match_t match, void *match_data)
  759. {
  760. return 0;
  761. }
  762. static inline int devres_release(struct udevice *dev, dr_release_t release,
  763. dr_match_t match, void *match_data)
  764. {
  765. return 0;
  766. }
  767. static inline void *devm_kmalloc(struct udevice *dev, size_t size, gfp_t gfp)
  768. {
  769. return kmalloc(size, gfp);
  770. }
  771. static inline void *devm_kzalloc(struct udevice *dev, size_t size, gfp_t gfp)
  772. {
  773. return kzalloc(size, gfp);
  774. }
  775. static inline void *devm_kmaloc_array(struct udevice *dev,
  776. size_t n, size_t size, gfp_t flags)
  777. {
  778. /* TODO: add kmalloc_array() to linux/compat.h */
  779. if (size != 0 && n > SIZE_MAX / size)
  780. return NULL;
  781. return kmalloc(n * size, flags);
  782. }
  783. static inline void *devm_kcalloc(struct udevice *dev,
  784. size_t n, size_t size, gfp_t flags)
  785. {
  786. /* TODO: add kcalloc() to linux/compat.h */
  787. return kmalloc(n * size, flags | __GFP_ZERO);
  788. }
  789. static inline void devm_kfree(struct udevice *dev, void *ptr)
  790. {
  791. kfree(ptr);
  792. }
  793. #endif /* ! CONFIG_DEVRES */
  794. /*
  795. * REVISIT:
  796. * remove the following after resolving conflicts with <linux/compat.h>
  797. */
  798. #ifdef dev_dbg
  799. #undef dev_dbg
  800. #endif
  801. #ifdef dev_vdbg
  802. #undef dev_vdbg
  803. #endif
  804. #ifdef dev_info
  805. #undef dev_info
  806. #endif
  807. #ifdef dev_err
  808. #undef dev_err
  809. #endif
  810. #ifdef dev_warn
  811. #undef dev_warn
  812. #endif
  813. /*
  814. * REVISIT:
  815. * print device name like Linux
  816. */
  817. #define dev_printk(dev, fmt, ...) \
  818. ({ \
  819. printk(fmt, ##__VA_ARGS__); \
  820. })
  821. #define __dev_printk(level, dev, fmt, ...) \
  822. ({ \
  823. if (level < CONFIG_VAL(LOGLEVEL)) \
  824. dev_printk(dev, fmt, ##__VA_ARGS__); \
  825. })
  826. #define dev_emerg(dev, fmt, ...) \
  827. __dev_printk(0, dev, fmt, ##__VA_ARGS__)
  828. #define dev_alert(dev, fmt, ...) \
  829. __dev_printk(1, dev, fmt, ##__VA_ARGS__)
  830. #define dev_crit(dev, fmt, ...) \
  831. __dev_printk(2, dev, fmt, ##__VA_ARGS__)
  832. #define dev_err(dev, fmt, ...) \
  833. __dev_printk(3, dev, fmt, ##__VA_ARGS__)
  834. #define dev_warn(dev, fmt, ...) \
  835. __dev_printk(4, dev, fmt, ##__VA_ARGS__)
  836. #define dev_notice(dev, fmt, ...) \
  837. __dev_printk(5, dev, fmt, ##__VA_ARGS__)
  838. #define dev_info(dev, fmt, ...) \
  839. __dev_printk(6, dev, fmt, ##__VA_ARGS__)
  840. #ifdef DEBUG
  841. #define dev_dbg(dev, fmt, ...) \
  842. __dev_printk(7, dev, fmt, ##__VA_ARGS__)
  843. #else
  844. #define dev_dbg(dev, fmt, ...) \
  845. ({ \
  846. if (0) \
  847. __dev_printk(7, dev, fmt, ##__VA_ARGS__); \
  848. })
  849. #endif
  850. #ifdef VERBOSE_DEBUG
  851. #define dev_vdbg dev_dbg
  852. #else
  853. #define dev_vdbg(dev, fmt, ...) \
  854. ({ \
  855. if (0) \
  856. __dev_printk(7, dev, fmt, ##__VA_ARGS__); \
  857. })
  858. #endif
  859. #endif