reset.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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, or NULL to request the first reset
  224. * signal in the list.
  225. * @reset_ctl: A pointer to a reset control struct to initialize.
  226. * Return: 0 if OK, or a negative error code.
  227. */
  228. int reset_get_by_name(struct udevice *dev, const char *name,
  229. struct reset_ctl *reset_ctl);
  230. /**
  231. * reset_request - Request a reset signal.
  232. *
  233. * @reset_ctl: A reset control struct.
  234. *
  235. * Return: 0 if OK, or a negative error code.
  236. */
  237. int reset_request(struct reset_ctl *reset_ctl);
  238. /**
  239. * reset_free - Free a previously requested reset signal.
  240. *
  241. * @reset_ctl: A reset control struct that was previously successfully
  242. * requested by reset_get_by_*().
  243. * Return: 0 if OK, or a negative error code.
  244. */
  245. int reset_free(struct reset_ctl *reset_ctl);
  246. /**
  247. * reset_assert - Assert a reset signal.
  248. *
  249. * This function will assert the specified reset signal, thus resetting the
  250. * affected HW module(s). Depending on the reset controller hardware, the reset
  251. * signal will either stay asserted until reset_deassert() is called, or the
  252. * hardware may autonomously clear the reset signal itself.
  253. *
  254. * @reset_ctl: A reset control struct that was previously successfully
  255. * requested by reset_get_by_*().
  256. * Return: 0 if OK, or a negative error code.
  257. */
  258. int reset_assert(struct reset_ctl *reset_ctl);
  259. /**
  260. * reset_assert_bulk - Assert all reset signals in a reset control bulk struct.
  261. *
  262. * This function will assert the specified reset signals in a reset control
  263. * bulk struct, thus resetting the affected HW module(s). Depending on the
  264. * reset controller hardware, the reset signals will either stay asserted
  265. * until reset_deassert_bulk() is called, or the hardware may autonomously
  266. * clear the reset signals itself.
  267. *
  268. * @bulk: A reset control bulk struct that was previously successfully
  269. * requested by reset_get_bulk().
  270. * Return: 0 if OK, or a negative error code.
  271. */
  272. int reset_assert_bulk(struct reset_ctl_bulk *bulk);
  273. /**
  274. * reset_deassert - Deassert a reset signal.
  275. *
  276. * This function will deassert the specified reset signal, thus releasing the
  277. * affected HW modules() from reset, and allowing them to continue normal
  278. * operation.
  279. *
  280. * @reset_ctl: A reset control struct that was previously successfully
  281. * requested by reset_get_by_*().
  282. * Return: 0 if OK, or a negative error code.
  283. */
  284. int reset_deassert(struct reset_ctl *reset_ctl);
  285. /**
  286. * reset_deassert_bulk - Deassert all reset signals in a reset control bulk
  287. * struct.
  288. *
  289. * This function will deassert the specified reset signals in a reset control
  290. * bulk struct, thus releasing the affected HW modules() from reset, and
  291. * allowing them to continue normal operation.
  292. *
  293. * @bulk: A reset control bulk struct that was previously successfully
  294. * requested by reset_get_bulk().
  295. * Return: 0 if OK, or a negative error code.
  296. */
  297. int reset_deassert_bulk(struct reset_ctl_bulk *bulk);
  298. /**
  299. * rst_status - Check reset signal status.
  300. *
  301. * @reset_ctl: The reset signal to check.
  302. * Return: 0 if deasserted, positive if asserted, or a negative
  303. * error code.
  304. */
  305. int reset_status(struct reset_ctl *reset_ctl);
  306. /**
  307. * reset_release_all - Assert/Free an array of previously requested resets.
  308. *
  309. * For each reset contained in the reset array, this function will check if
  310. * reset has been previously requested and then will assert and free it.
  311. *
  312. * @reset_ctl: A reset struct array that was previously successfully
  313. * requested by reset_get_by_*().
  314. * @count Number of reset contained in the array
  315. * Return: 0 if OK, or a negative error code.
  316. */
  317. int reset_release_all(struct reset_ctl *reset_ctl, int count);
  318. /**
  319. * reset_release_bulk - Assert/Free an array of previously requested reset
  320. * signals in a reset control bulk struct.
  321. *
  322. * For each reset contained in the reset control bulk struct, this function
  323. * will check if reset has been previously requested and then will assert
  324. * and free it.
  325. *
  326. * @bulk: A reset control bulk struct that was previously successfully
  327. * requested by reset_get_bulk().
  328. * Return: 0 if OK, or a negative error code.
  329. */
  330. static inline int reset_release_bulk(struct reset_ctl_bulk *bulk)
  331. {
  332. return reset_release_all(bulk->resets, bulk->count);
  333. }
  334. #else
  335. static inline struct reset_ctl *devm_reset_control_get(struct udevice *dev,
  336. const char *id)
  337. {
  338. return ERR_PTR(-ENOTSUPP);
  339. }
  340. static inline struct reset_ctl *devm_reset_control_get_optional(struct udevice *dev,
  341. const char *id)
  342. {
  343. return NULL;
  344. }
  345. static inline struct reset_ctl *devm_reset_control_get_by_index(struct udevice *dev,
  346. int index)
  347. {
  348. return ERR_PTR(-ENOTSUPP);
  349. }
  350. static inline struct reset_ctl_bulk *devm_reset_bulk_get(struct udevice *dev)
  351. {
  352. return ERR_PTR(-ENOTSUPP);
  353. }
  354. static inline struct reset_ctl_bulk *devm_reset_bulk_get_optional(struct udevice *dev)
  355. {
  356. return NULL;
  357. }
  358. static inline struct reset_ctl_bulk *devm_reset_bulk_get_by_node(struct udevice *dev,
  359. ofnode node)
  360. {
  361. return ERR_PTR(-ENOTSUPP);
  362. }
  363. static inline struct reset_ctl_bulk *devm_reset_bulk_get_optional_by_node(struct udevice *dev,
  364. ofnode node)
  365. {
  366. return NULL;
  367. }
  368. static inline int reset_get_by_index(struct udevice *dev, int index,
  369. struct reset_ctl *reset_ctl)
  370. {
  371. return -ENOTSUPP;
  372. }
  373. static inline int reset_get_bulk(struct udevice *dev,
  374. struct reset_ctl_bulk *bulk)
  375. {
  376. return -ENOTSUPP;
  377. }
  378. static inline int reset_get_by_name(struct udevice *dev, const char *name,
  379. struct reset_ctl *reset_ctl)
  380. {
  381. return -ENOTSUPP;
  382. }
  383. static inline int reset_free(struct reset_ctl *reset_ctl)
  384. {
  385. return 0;
  386. }
  387. static inline int reset_assert(struct reset_ctl *reset_ctl)
  388. {
  389. return 0;
  390. }
  391. static inline int reset_assert_bulk(struct reset_ctl_bulk *bulk)
  392. {
  393. return 0;
  394. }
  395. static inline int reset_deassert(struct reset_ctl *reset_ctl)
  396. {
  397. return 0;
  398. }
  399. static inline int reset_deassert_bulk(struct reset_ctl_bulk *bulk)
  400. {
  401. return 0;
  402. }
  403. static inline int reset_status(struct reset_ctl *reset_ctl)
  404. {
  405. return -ENOTSUPP;
  406. }
  407. static inline int reset_release_all(struct reset_ctl *reset_ctl, int count)
  408. {
  409. return 0;
  410. }
  411. static inline int reset_release_bulk(struct reset_ctl_bulk *bulk)
  412. {
  413. return 0;
  414. }
  415. #endif
  416. /**
  417. * reset_valid() - check if reset is valid
  418. *
  419. * @reset_ctl: the reset to check
  420. * Return: TRUE if valid, or FALSE
  421. */
  422. static inline bool reset_valid(struct reset_ctl *reset_ctl)
  423. {
  424. return !!reset_ctl->dev;
  425. }
  426. #endif