device.h 30 KB

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