device.h 29 KB

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