reset.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2016, NVIDIA CORPORATION.
  4. */
  5. #ifndef _RESET_H
  6. #define _RESET_H
  7. #include <dm/ofnode.h>
  8. #include <linux/err.h>
  9. /**
  10. * A reset is a hardware signal indicating that a HW module (or IP block, or
  11. * sometimes an entire off-CPU chip) reset all of its internal state to some
  12. * known-good initial state. Drivers will often reset HW modules when they
  13. * begin execution to ensure that hardware correctly responds to all requests,
  14. * or in response to some error condition. Reset signals are often controlled
  15. * externally to the HW module being reset, by an entity this API calls a reset
  16. * controller. This API provides a standard means for drivers to request that
  17. * reset controllers set or clear reset signals.
  18. *
  19. * A driver that implements UCLASS_RESET is a reset controller or provider. A
  20. * controller will often implement multiple separate reset signals, since the
  21. * hardware it manages often has this capability. reset-uclass.h describes the
  22. * interface which reset controllers must implement.
  23. *
  24. * Reset consumers/clients are the HW modules affected by reset signals. This
  25. * header file describes the API used by drivers for those HW modules.
  26. */
  27. struct udevice;
  28. /**
  29. * struct reset_ctl - A handle to (allowing control of) a single reset signal.
  30. *
  31. * Clients provide storage for reset control handles. The content of the
  32. * structure is managed solely by the reset API and reset drivers. A reset
  33. * control struct is initialized by "get"ing the reset control struct. The
  34. * reset control struct is passed to all other reset APIs to identify which
  35. * reset signal to operate upon.
  36. *
  37. * @dev: The device which implements the reset signal.
  38. * @id: The reset signal ID within the provider.
  39. * @data: An optional data field for scenarios where a single integer ID is not
  40. * sufficient. If used, it can be populated through an .of_xlate op and
  41. * processed during the various reset ops.
  42. * @polarity: An optional polarity field for drivers that support
  43. * different reset polarities.
  44. *
  45. * Should additional information to identify and configure any reset signal
  46. * for any provider be required in the future, the struct could be expanded to
  47. * either (a) add more fields to allow reset providers to store additional
  48. * information, or (b) replace the id field with an opaque pointer, which the
  49. * provider would dynamically allocated during its .of_xlate op, and process
  50. * during is .request op. This may require the addition of an extra op to clean
  51. * up the allocation.
  52. */
  53. struct reset_ctl {
  54. struct udevice *dev;
  55. /*
  56. * Written by of_xlate. In the future, we might add more fields here.
  57. */
  58. unsigned long id;
  59. unsigned long data;
  60. unsigned long polarity;
  61. };
  62. /**
  63. * struct reset_ctl_bulk - A handle to (allowing control of) a bulk of reset
  64. * signals.
  65. *
  66. * Clients provide storage for the reset control bulk. The content of the
  67. * structure is managed solely by the reset API. A reset control bulk struct is
  68. * initialized by "get"ing the reset control bulk struct.
  69. * The reset control bulk struct is passed to all other bulk reset APIs to apply
  70. * the API to all the reset signals in the bulk struct.
  71. *
  72. * @resets: An array of reset signal handles handles.
  73. * @count: The number of reset signal handles in the reset array.
  74. */
  75. struct reset_ctl_bulk {
  76. struct reset_ctl *resets;
  77. unsigned int count;
  78. };
  79. #if CONFIG_IS_ENABLED(DM_RESET)
  80. /**
  81. * devm_reset_control_get - resource managed reset_get_by_name()
  82. * @dev: device to be reset by the controller
  83. * @id: reset line name
  84. *
  85. * Managed reset_get_by_name(). For reset controllers returned
  86. * from this function, reset_free() is called automatically on driver
  87. * detach.
  88. *
  89. * Returns a struct reset_ctl or IS_ERR() condition containing errno.
  90. */
  91. struct reset_ctl *devm_reset_control_get(struct udevice *dev, const char *id);
  92. /**
  93. * devm_reset_control_get_optional - resource managed reset_get_by_name() that
  94. * can fail
  95. * @dev: The client device.
  96. * @id: reset line name
  97. *
  98. * Managed reset_get_by_name(). For reset controllers returned
  99. * from this function, reset_free() is called automatically on driver
  100. * detach.
  101. *
  102. * Returns a struct reset_ctl or a dummy reset controller if it failed.
  103. */
  104. struct reset_ctl *devm_reset_control_get_optional(struct udevice *dev,
  105. const char *id);
  106. /**
  107. * devm_reset_control_get - resource managed reset_get_by_index()
  108. * @dev: The client device.
  109. * @index: The index of the reset signal to request, within the client's
  110. * list of reset signals.
  111. *
  112. * Managed reset_get_by_index(). For reset controllers returned
  113. * from this function, reset_free() is called automatically on driver
  114. * detach.
  115. *
  116. * Returns a struct reset_ctl or IS_ERR() condition containing errno.
  117. */
  118. struct reset_ctl *devm_reset_control_get_by_index(struct udevice *dev,
  119. int index);
  120. /**
  121. * devm_reset_bulk_get - resource managed reset_get_bulk()
  122. * @dev: device to be reset by the controller
  123. *
  124. * Managed reset_get_bulk(). For reset controllers returned
  125. * from this function, reset_free() is called automatically on driver
  126. * detach.
  127. *
  128. * Returns a struct reset_ctl or IS_ERR() condition containing errno.
  129. */
  130. struct reset_ctl_bulk *devm_reset_bulk_get(struct udevice *dev);
  131. /**
  132. * devm_reset_bulk_get_optional - resource managed reset_get_bulk() that
  133. * can fail
  134. * @dev: The client device.
  135. *
  136. * Managed reset_get_bulk(). For reset controllers returned
  137. * from this function, reset_free() is called automatically on driver
  138. * detach.
  139. *
  140. * Returns a struct reset_ctl or NULL if it failed.
  141. */
  142. struct reset_ctl_bulk *devm_reset_bulk_get_optional(struct udevice *dev);
  143. /**
  144. * devm_reset_bulk_get_by_node - resource managed reset_get_bulk()
  145. * @dev: device to be reset by the controller
  146. * @node: ofnode where the "resets" property is. Usually a sub-node of
  147. * the dev's node.
  148. *
  149. * see devm_reset_bulk_get()
  150. */
  151. struct reset_ctl_bulk *devm_reset_bulk_get_by_node(struct udevice *dev,
  152. ofnode node);
  153. /**
  154. * devm_reset_bulk_get_optional_by_node - resource managed reset_get_bulk()
  155. * that can fail
  156. * @dev: device to be reset by the controller
  157. * @node: ofnode where the "resets" property is. Usually a sub-node of
  158. * the dev's node.
  159. *
  160. * see devm_reset_bulk_get_optional()
  161. */
  162. struct reset_ctl_bulk *devm_reset_bulk_get_optional_by_node(struct udevice *dev,
  163. ofnode node);
  164. /**
  165. * reset_get_by_index - Get/request a reset signal by integer index.
  166. *
  167. * This looks up and requests a reset signal. The index is relative to the
  168. * client device; each device is assumed to have n reset signals associated
  169. * with it somehow, and this function finds and requests one of them. The
  170. * mapping of client device reset signal indices to provider reset signals may
  171. * be via device-tree properties, board-provided mapping tables, or some other
  172. * mechanism.
  173. *
  174. * @dev: The client device.
  175. * @index: The index of the reset signal to request, within the client's
  176. * list of reset signals.
  177. * @reset_ctl A pointer to a reset control struct to initialize.
  178. * @return 0 if OK, or a negative error code.
  179. */
  180. int reset_get_by_index(struct udevice *dev, int index,
  181. struct reset_ctl *reset_ctl);
  182. /**
  183. * reset_get_by_index_nodev - Get/request a reset signal by integer index
  184. * without a device.
  185. *
  186. * This is a version of reset_get_by_index() that does not use a device.
  187. *
  188. * @node: The client ofnode.
  189. * @index: The index of the reset signal to request, within the client's
  190. * list of reset signals.
  191. * @reset_ctl A pointer to a reset control struct to initialize.
  192. * @return 0 if OK, or a negative error code.
  193. */
  194. int reset_get_by_index_nodev(ofnode node, int index,
  195. struct reset_ctl *reset_ctl);
  196. /**
  197. * reset_get_bulk - Get/request all reset signals of a device.
  198. *
  199. * This looks up and requests all reset signals of the client device; each
  200. * device is assumed to have n reset signals associated with it somehow,
  201. * and this function finds and requests all of them in a separate structure.
  202. * The mapping of client device reset signals indices to provider reset signals
  203. * may be via device-tree properties, board-provided mapping tables, or some
  204. * other mechanism.
  205. *
  206. * @dev: The client device.
  207. * @bulk A pointer to a reset control bulk struct to initialize.
  208. * @return 0 if OK, or a negative error code.
  209. */
  210. int reset_get_bulk(struct udevice *dev, struct reset_ctl_bulk *bulk);
  211. /**
  212. * reset_get_by_name - Get/request a reset signal by name.
  213. *
  214. * This looks up and requests a reset signal. The name is relative to the
  215. * client device; each device is assumed to have n reset signals associated
  216. * with it somehow, and this function finds and requests one of them. The
  217. * mapping of client device reset signal names to provider reset signal may be
  218. * via device-tree properties, board-provided mapping tables, or some other
  219. * mechanism.
  220. *
  221. * @dev: The client device.
  222. * @name: The name of the reset signal to request, within the client's
  223. * list of reset signals.
  224. * @reset_ctl: A pointer to a reset control struct to initialize.
  225. * @return 0 if OK, or a negative error code.
  226. */
  227. int reset_get_by_name(struct udevice *dev, const char *name,
  228. struct reset_ctl *reset_ctl);
  229. /**
  230. * reset_request - Request a reset signal.
  231. *
  232. * @reset_ctl: A reset control struct.
  233. *
  234. * @return 0 if OK, or a negative error code.
  235. */
  236. int reset_request(struct reset_ctl *reset_ctl);
  237. /**
  238. * reset_free - Free a previously requested reset signal.
  239. *
  240. * @reset_ctl: A reset control struct that was previously successfully
  241. * requested by reset_get_by_*().
  242. * @return 0 if OK, or a negative error code.
  243. */
  244. int reset_free(struct reset_ctl *reset_ctl);
  245. /**
  246. * reset_assert - Assert a reset signal.
  247. *
  248. * This function will assert the specified reset signal, thus resetting the
  249. * affected HW module(s). Depending on the reset controller hardware, the reset
  250. * signal will either stay asserted until reset_deassert() is called, or the
  251. * hardware may autonomously clear the reset signal itself.
  252. *
  253. * @reset_ctl: A reset control struct that was previously successfully
  254. * requested by reset_get_by_*().
  255. * @return 0 if OK, or a negative error code.
  256. */
  257. int reset_assert(struct reset_ctl *reset_ctl);
  258. /**
  259. * reset_assert_bulk - Assert all reset signals in a reset control bulk struct.
  260. *
  261. * This function will assert the specified reset signals in a reset control
  262. * bulk struct, thus resetting the affected HW module(s). Depending on the
  263. * reset controller hardware, the reset signals will either stay asserted
  264. * until reset_deassert_bulk() is called, or the hardware may autonomously
  265. * clear the reset signals itself.
  266. *
  267. * @bulk: A reset control bulk struct that was previously successfully
  268. * requested by reset_get_bulk().
  269. * @return 0 if OK, or a negative error code.
  270. */
  271. int reset_assert_bulk(struct reset_ctl_bulk *bulk);
  272. /**
  273. * reset_deassert - Deassert a reset signal.
  274. *
  275. * This function will deassert the specified reset signal, thus releasing the
  276. * affected HW modules() from reset, and allowing them to continue normal
  277. * operation.
  278. *
  279. * @reset_ctl: A reset control struct that was previously successfully
  280. * requested by reset_get_by_*().
  281. * @return 0 if OK, or a negative error code.
  282. */
  283. int reset_deassert(struct reset_ctl *reset_ctl);
  284. /**
  285. * reset_deassert_bulk - Deassert all reset signals in a reset control bulk
  286. * struct.
  287. *
  288. * This function will deassert the specified reset signals in a reset control
  289. * bulk struct, thus releasing the affected HW modules() from reset, and
  290. * allowing them to continue normal operation.
  291. *
  292. * @bulk: A reset control bulk struct that was previously successfully
  293. * requested by reset_get_bulk().
  294. * @return 0 if OK, or a negative error code.
  295. */
  296. int reset_deassert_bulk(struct reset_ctl_bulk *bulk);
  297. /**
  298. * rst_status - Check reset signal status.
  299. *
  300. * @reset_ctl: The reset signal to check.
  301. * @return 0 if deasserted, positive if asserted, or a negative
  302. * error code.
  303. */
  304. int reset_status(struct reset_ctl *reset_ctl);
  305. /**
  306. * reset_release_all - Assert/Free an array of previously requested resets.
  307. *
  308. * For each reset contained in the reset array, this function will check if
  309. * reset has been previously requested and then will assert and free it.
  310. *
  311. * @reset_ctl: A reset struct array that was previously successfully
  312. * requested by reset_get_by_*().
  313. * @count Number of reset contained in the array
  314. * @return 0 if OK, or a negative error code.
  315. */
  316. int reset_release_all(struct reset_ctl *reset_ctl, int count);
  317. /**
  318. * reset_release_bulk - Assert/Free an array of previously requested reset
  319. * signals in a reset control bulk struct.
  320. *
  321. * For each reset contained in the reset control bulk struct, this function
  322. * will check if reset has been previously requested and then will assert
  323. * and free it.
  324. *
  325. * @bulk: A reset control bulk struct that was previously successfully
  326. * requested by reset_get_bulk().
  327. * @return 0 if OK, or a negative error code.
  328. */
  329. static inline int reset_release_bulk(struct reset_ctl_bulk *bulk)
  330. {
  331. return reset_release_all(bulk->resets, bulk->count);
  332. }
  333. #else
  334. static inline struct reset_ctl *devm_reset_control_get(struct udevice *dev,
  335. const char *id)
  336. {
  337. return ERR_PTR(-ENOTSUPP);
  338. }
  339. static inline struct reset_ctl *devm_reset_control_get_optional(struct udevice *dev,
  340. const char *id)
  341. {
  342. return NULL;
  343. }
  344. static inline struct reset_ctl *devm_reset_control_get_by_index(struct udevice *dev,
  345. int index)
  346. {
  347. return ERR_PTR(-ENOTSUPP);
  348. }
  349. static inline struct reset_ctl_bulk *devm_reset_bulk_get(struct udevice *dev)
  350. {
  351. return ERR_PTR(-ENOTSUPP);
  352. }
  353. static inline struct reset_ctl_bulk *devm_reset_bulk_get_optional(struct udevice *dev)
  354. {
  355. return NULL;
  356. }
  357. static inline struct reset_ctl_bulk *devm_reset_bulk_get_by_node(struct udevice *dev,
  358. ofnode node)
  359. {
  360. return ERR_PTR(-ENOTSUPP);
  361. }
  362. static inline struct reset_ctl_bulk *devm_reset_bulk_get_optional_by_node(struct udevice *dev,
  363. ofnode node)
  364. {
  365. return NULL;
  366. }
  367. static inline int reset_get_by_index(struct udevice *dev, int index,
  368. struct reset_ctl *reset_ctl)
  369. {
  370. return -ENOTSUPP;
  371. }
  372. static inline int reset_get_bulk(struct udevice *dev,
  373. struct reset_ctl_bulk *bulk)
  374. {
  375. return -ENOTSUPP;
  376. }
  377. static inline int reset_get_by_name(struct udevice *dev, const char *name,
  378. struct reset_ctl *reset_ctl)
  379. {
  380. return -ENOTSUPP;
  381. }
  382. static inline int reset_free(struct reset_ctl *reset_ctl)
  383. {
  384. return 0;
  385. }
  386. static inline int reset_assert(struct reset_ctl *reset_ctl)
  387. {
  388. return 0;
  389. }
  390. static inline int reset_assert_bulk(struct reset_ctl_bulk *bulk)
  391. {
  392. return 0;
  393. }
  394. static inline int reset_deassert(struct reset_ctl *reset_ctl)
  395. {
  396. return 0;
  397. }
  398. static inline int reset_deassert_bulk(struct reset_ctl_bulk *bulk)
  399. {
  400. return 0;
  401. }
  402. static inline int reset_status(struct reset_ctl *reset_ctl)
  403. {
  404. return -ENOTSUPP;
  405. }
  406. static inline int reset_release_all(struct reset_ctl *reset_ctl, int count)
  407. {
  408. return 0;
  409. }
  410. static inline int reset_release_bulk(struct reset_ctl_bulk *bulk)
  411. {
  412. return 0;
  413. }
  414. #endif
  415. /**
  416. * reset_valid() - check if reset is valid
  417. *
  418. * @reset_ctl: the reset to check
  419. * @return TRUE if valid, or FALSE
  420. */
  421. static inline bool reset_valid(struct reset_ctl *reset_ctl)
  422. {
  423. return !!reset_ctl->dev;
  424. }
  425. #endif