core.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /*
  2. * Reset Controller framework
  3. *
  4. * Copyright 2013 Philipp Zabel, Pengutronix
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/atomic.h>
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/export.h>
  15. #include <linux/kernel.h>
  16. #include <linux/kref.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/reset.h>
  20. #include <linux/reset-controller.h>
  21. #include <linux/slab.h>
  22. static DEFINE_MUTEX(reset_list_mutex);
  23. static LIST_HEAD(reset_controller_list);
  24. /**
  25. * struct reset_control - a reset control
  26. * @rcdev: a pointer to the reset controller device
  27. * this reset control belongs to
  28. * @list: list entry for the rcdev's reset controller list
  29. * @id: ID of the reset controller in the reset
  30. * controller device
  31. * @refcnt: Number of gets of this reset_control
  32. * @shared: Is this a shared (1), or an exclusive (0) reset_control?
  33. * @deassert_cnt: Number of times this reset line has been deasserted
  34. * @triggered_count: Number of times this reset line has been reset. Currently
  35. * only used for shared resets, which means that the value
  36. * will be either 0 or 1.
  37. */
  38. struct reset_control {
  39. struct reset_controller_dev *rcdev;
  40. struct list_head list;
  41. unsigned int id;
  42. struct kref refcnt;
  43. bool shared;
  44. bool array;
  45. atomic_t deassert_count;
  46. atomic_t triggered_count;
  47. };
  48. /**
  49. * struct reset_control_array - an array of reset controls
  50. * @base: reset control for compatibility with reset control API functions
  51. * @num_rstcs: number of reset controls
  52. * @rstc: array of reset controls
  53. */
  54. struct reset_control_array {
  55. struct reset_control base;
  56. unsigned int num_rstcs;
  57. struct reset_control *rstc[];
  58. };
  59. /**
  60. * of_reset_simple_xlate - translate reset_spec to the reset line number
  61. * @rcdev: a pointer to the reset controller device
  62. * @reset_spec: reset line specifier as found in the device tree
  63. * @flags: a flags pointer to fill in (optional)
  64. *
  65. * This simple translation function should be used for reset controllers
  66. * with 1:1 mapping, where reset lines can be indexed by number without gaps.
  67. */
  68. static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
  69. const struct of_phandle_args *reset_spec)
  70. {
  71. if (reset_spec->args[0] >= rcdev->nr_resets)
  72. return -EINVAL;
  73. return reset_spec->args[0];
  74. }
  75. /**
  76. * reset_controller_register - register a reset controller device
  77. * @rcdev: a pointer to the initialized reset controller device
  78. */
  79. int reset_controller_register(struct reset_controller_dev *rcdev)
  80. {
  81. if (!rcdev->of_xlate) {
  82. rcdev->of_reset_n_cells = 1;
  83. rcdev->of_xlate = of_reset_simple_xlate;
  84. }
  85. INIT_LIST_HEAD(&rcdev->reset_control_head);
  86. mutex_lock(&reset_list_mutex);
  87. list_add(&rcdev->list, &reset_controller_list);
  88. mutex_unlock(&reset_list_mutex);
  89. return 0;
  90. }
  91. EXPORT_SYMBOL_GPL(reset_controller_register);
  92. /**
  93. * reset_controller_unregister - unregister a reset controller device
  94. * @rcdev: a pointer to the reset controller device
  95. */
  96. void reset_controller_unregister(struct reset_controller_dev *rcdev)
  97. {
  98. mutex_lock(&reset_list_mutex);
  99. list_del(&rcdev->list);
  100. mutex_unlock(&reset_list_mutex);
  101. }
  102. EXPORT_SYMBOL_GPL(reset_controller_unregister);
  103. static void devm_reset_controller_release(struct device *dev, void *res)
  104. {
  105. reset_controller_unregister(*(struct reset_controller_dev **)res);
  106. }
  107. /**
  108. * devm_reset_controller_register - resource managed reset_controller_register()
  109. * @dev: device that is registering this reset controller
  110. * @rcdev: a pointer to the initialized reset controller device
  111. *
  112. * Managed reset_controller_register(). For reset controllers registered by
  113. * this function, reset_controller_unregister() is automatically called on
  114. * driver detach. See reset_controller_register() for more information.
  115. */
  116. int devm_reset_controller_register(struct device *dev,
  117. struct reset_controller_dev *rcdev)
  118. {
  119. struct reset_controller_dev **rcdevp;
  120. int ret;
  121. rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
  122. GFP_KERNEL);
  123. if (!rcdevp)
  124. return -ENOMEM;
  125. ret = reset_controller_register(rcdev);
  126. if (!ret) {
  127. *rcdevp = rcdev;
  128. devres_add(dev, rcdevp);
  129. } else {
  130. devres_free(rcdevp);
  131. }
  132. return ret;
  133. }
  134. EXPORT_SYMBOL_GPL(devm_reset_controller_register);
  135. static inline struct reset_control_array *
  136. rstc_to_array(struct reset_control *rstc) {
  137. return container_of(rstc, struct reset_control_array, base);
  138. }
  139. static int reset_control_array_reset(struct reset_control_array *resets)
  140. {
  141. int ret, i;
  142. for (i = 0; i < resets->num_rstcs; i++) {
  143. ret = reset_control_reset(resets->rstc[i]);
  144. if (ret)
  145. return ret;
  146. }
  147. return 0;
  148. }
  149. static int reset_control_array_assert(struct reset_control_array *resets)
  150. {
  151. int ret, i;
  152. for (i = 0; i < resets->num_rstcs; i++) {
  153. ret = reset_control_assert(resets->rstc[i]);
  154. if (ret)
  155. goto err;
  156. }
  157. return 0;
  158. err:
  159. while (i--)
  160. reset_control_deassert(resets->rstc[i]);
  161. return ret;
  162. }
  163. static int reset_control_array_deassert(struct reset_control_array *resets)
  164. {
  165. int ret, i;
  166. for (i = 0; i < resets->num_rstcs; i++) {
  167. ret = reset_control_deassert(resets->rstc[i]);
  168. if (ret)
  169. goto err;
  170. }
  171. return 0;
  172. err:
  173. while (i--)
  174. reset_control_assert(resets->rstc[i]);
  175. return ret;
  176. }
  177. static inline bool reset_control_is_array(struct reset_control *rstc)
  178. {
  179. return rstc->array;
  180. }
  181. /**
  182. * reset_control_reset - reset the controlled device
  183. * @rstc: reset controller
  184. *
  185. * On a shared reset line the actual reset pulse is only triggered once for the
  186. * lifetime of the reset_control instance: for all but the first caller this is
  187. * a no-op.
  188. * Consumers must not use reset_control_(de)assert on shared reset lines when
  189. * reset_control_reset has been used.
  190. *
  191. * If rstc is NULL it is an optional reset and the function will just
  192. * return 0.
  193. */
  194. int reset_control_reset(struct reset_control *rstc)
  195. {
  196. int ret;
  197. if (!rstc)
  198. return 0;
  199. if (WARN_ON(IS_ERR(rstc)))
  200. return -EINVAL;
  201. if (reset_control_is_array(rstc))
  202. return reset_control_array_reset(rstc_to_array(rstc));
  203. if (!rstc->rcdev->ops->reset)
  204. return -ENOTSUPP;
  205. if (rstc->shared) {
  206. if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
  207. return -EINVAL;
  208. if (atomic_inc_return(&rstc->triggered_count) != 1)
  209. return 0;
  210. }
  211. ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
  212. if (rstc->shared && ret)
  213. atomic_dec(&rstc->triggered_count);
  214. return ret;
  215. }
  216. EXPORT_SYMBOL_GPL(reset_control_reset);
  217. /**
  218. * reset_control_assert - asserts the reset line
  219. * @rstc: reset controller
  220. *
  221. * Calling this on an exclusive reset controller guarantees that the reset
  222. * will be asserted. When called on a shared reset controller the line may
  223. * still be deasserted, as long as other users keep it so.
  224. *
  225. * For shared reset controls a driver cannot expect the hw's registers and
  226. * internal state to be reset, but must be prepared for this to happen.
  227. * Consumers must not use reset_control_reset on shared reset lines when
  228. * reset_control_(de)assert has been used.
  229. * return 0.
  230. *
  231. * If rstc is NULL it is an optional reset and the function will just
  232. * return 0.
  233. */
  234. int reset_control_assert(struct reset_control *rstc)
  235. {
  236. if (!rstc)
  237. return 0;
  238. if (WARN_ON(IS_ERR(rstc)))
  239. return -EINVAL;
  240. if (reset_control_is_array(rstc))
  241. return reset_control_array_assert(rstc_to_array(rstc));
  242. if (rstc->shared) {
  243. if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
  244. return -EINVAL;
  245. if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
  246. return -EINVAL;
  247. if (atomic_dec_return(&rstc->deassert_count) != 0)
  248. return 0;
  249. /*
  250. * Shared reset controls allow the reset line to be in any state
  251. * after this call, so doing nothing is a valid option.
  252. */
  253. if (!rstc->rcdev->ops->assert)
  254. return 0;
  255. } else {
  256. /*
  257. * If the reset controller does not implement .assert(), there
  258. * is no way to guarantee that the reset line is asserted after
  259. * this call.
  260. */
  261. if (!rstc->rcdev->ops->assert)
  262. return -ENOTSUPP;
  263. }
  264. return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
  265. }
  266. EXPORT_SYMBOL_GPL(reset_control_assert);
  267. /**
  268. * reset_control_deassert - deasserts the reset line
  269. * @rstc: reset controller
  270. *
  271. * After calling this function, the reset is guaranteed to be deasserted.
  272. * Consumers must not use reset_control_reset on shared reset lines when
  273. * reset_control_(de)assert has been used.
  274. * return 0.
  275. *
  276. * If rstc is NULL it is an optional reset and the function will just
  277. * return 0.
  278. */
  279. int reset_control_deassert(struct reset_control *rstc)
  280. {
  281. if (!rstc)
  282. return 0;
  283. if (WARN_ON(IS_ERR(rstc)))
  284. return -EINVAL;
  285. if (reset_control_is_array(rstc))
  286. return reset_control_array_deassert(rstc_to_array(rstc));
  287. if (rstc->shared) {
  288. if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
  289. return -EINVAL;
  290. if (atomic_inc_return(&rstc->deassert_count) != 1)
  291. return 0;
  292. }
  293. /*
  294. * If the reset controller does not implement .deassert(), we assume
  295. * that it handles self-deasserting reset lines via .reset(). In that
  296. * case, the reset lines are deasserted by default. If that is not the
  297. * case, the reset controller driver should implement .deassert() and
  298. * return -ENOTSUPP.
  299. */
  300. if (!rstc->rcdev->ops->deassert)
  301. return 0;
  302. return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
  303. }
  304. EXPORT_SYMBOL_GPL(reset_control_deassert);
  305. /**
  306. * reset_control_status - returns a negative errno if not supported, a
  307. * positive value if the reset line is asserted, or zero if the reset
  308. * line is not asserted or if the desc is NULL (optional reset).
  309. * @rstc: reset controller
  310. */
  311. int reset_control_status(struct reset_control *rstc)
  312. {
  313. if (!rstc)
  314. return 0;
  315. if (WARN_ON(IS_ERR(rstc)) || reset_control_is_array(rstc))
  316. return -EINVAL;
  317. if (rstc->rcdev->ops->status)
  318. return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
  319. return -ENOTSUPP;
  320. }
  321. EXPORT_SYMBOL_GPL(reset_control_status);
  322. static struct reset_control *__reset_control_get_internal(
  323. struct reset_controller_dev *rcdev,
  324. unsigned int index, bool shared)
  325. {
  326. struct reset_control *rstc;
  327. lockdep_assert_held(&reset_list_mutex);
  328. list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
  329. if (rstc->id == index) {
  330. if (WARN_ON(!rstc->shared || !shared))
  331. return ERR_PTR(-EBUSY);
  332. kref_get(&rstc->refcnt);
  333. return rstc;
  334. }
  335. }
  336. rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
  337. if (!rstc)
  338. return ERR_PTR(-ENOMEM);
  339. try_module_get(rcdev->owner);
  340. rstc->rcdev = rcdev;
  341. list_add(&rstc->list, &rcdev->reset_control_head);
  342. rstc->id = index;
  343. kref_init(&rstc->refcnt);
  344. rstc->shared = shared;
  345. return rstc;
  346. }
  347. static void __reset_control_release(struct kref *kref)
  348. {
  349. struct reset_control *rstc = container_of(kref, struct reset_control,
  350. refcnt);
  351. lockdep_assert_held(&reset_list_mutex);
  352. module_put(rstc->rcdev->owner);
  353. list_del(&rstc->list);
  354. kfree(rstc);
  355. }
  356. static void __reset_control_put_internal(struct reset_control *rstc)
  357. {
  358. lockdep_assert_held(&reset_list_mutex);
  359. kref_put(&rstc->refcnt, __reset_control_release);
  360. }
  361. struct reset_control *__of_reset_control_get(struct device_node *node,
  362. const char *id, int index, bool shared,
  363. bool optional)
  364. {
  365. struct reset_control *rstc;
  366. struct reset_controller_dev *r, *rcdev;
  367. struct of_phandle_args args;
  368. int rstc_id;
  369. int ret;
  370. if (!node)
  371. return ERR_PTR(-EINVAL);
  372. if (id) {
  373. index = of_property_match_string(node,
  374. "reset-names", id);
  375. if (index == -EILSEQ)
  376. return ERR_PTR(index);
  377. if (index < 0)
  378. return optional ? NULL : ERR_PTR(-ENOENT);
  379. }
  380. ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
  381. index, &args);
  382. if (ret == -EINVAL)
  383. return ERR_PTR(ret);
  384. if (ret)
  385. return optional ? NULL : ERR_PTR(ret);
  386. mutex_lock(&reset_list_mutex);
  387. rcdev = NULL;
  388. list_for_each_entry(r, &reset_controller_list, list) {
  389. if (args.np == r->of_node) {
  390. rcdev = r;
  391. break;
  392. }
  393. }
  394. of_node_put(args.np);
  395. if (!rcdev) {
  396. mutex_unlock(&reset_list_mutex);
  397. return ERR_PTR(-EPROBE_DEFER);
  398. }
  399. if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
  400. mutex_unlock(&reset_list_mutex);
  401. return ERR_PTR(-EINVAL);
  402. }
  403. rstc_id = rcdev->of_xlate(rcdev, &args);
  404. if (rstc_id < 0) {
  405. mutex_unlock(&reset_list_mutex);
  406. return ERR_PTR(rstc_id);
  407. }
  408. /* reset_list_mutex also protects the rcdev's reset_control list */
  409. rstc = __reset_control_get_internal(rcdev, rstc_id, shared);
  410. mutex_unlock(&reset_list_mutex);
  411. return rstc;
  412. }
  413. EXPORT_SYMBOL_GPL(__of_reset_control_get);
  414. struct reset_control *__reset_control_get(struct device *dev, const char *id,
  415. int index, bool shared, bool optional)
  416. {
  417. if (dev->of_node)
  418. return __of_reset_control_get(dev->of_node, id, index, shared,
  419. optional);
  420. return optional ? NULL : ERR_PTR(-EINVAL);
  421. }
  422. EXPORT_SYMBOL_GPL(__reset_control_get);
  423. static void reset_control_array_put(struct reset_control_array *resets)
  424. {
  425. int i;
  426. mutex_lock(&reset_list_mutex);
  427. for (i = 0; i < resets->num_rstcs; i++)
  428. __reset_control_put_internal(resets->rstc[i]);
  429. mutex_unlock(&reset_list_mutex);
  430. }
  431. /**
  432. * reset_control_put - free the reset controller
  433. * @rstc: reset controller
  434. */
  435. void reset_control_put(struct reset_control *rstc)
  436. {
  437. if (IS_ERR_OR_NULL(rstc))
  438. return;
  439. if (reset_control_is_array(rstc)) {
  440. reset_control_array_put(rstc_to_array(rstc));
  441. return;
  442. }
  443. mutex_lock(&reset_list_mutex);
  444. __reset_control_put_internal(rstc);
  445. mutex_unlock(&reset_list_mutex);
  446. }
  447. EXPORT_SYMBOL_GPL(reset_control_put);
  448. static void devm_reset_control_release(struct device *dev, void *res)
  449. {
  450. reset_control_put(*(struct reset_control **)res);
  451. }
  452. struct reset_control *__devm_reset_control_get(struct device *dev,
  453. const char *id, int index, bool shared,
  454. bool optional)
  455. {
  456. struct reset_control **ptr, *rstc;
  457. ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
  458. GFP_KERNEL);
  459. if (!ptr)
  460. return ERR_PTR(-ENOMEM);
  461. rstc = __reset_control_get(dev, id, index, shared, optional);
  462. if (!IS_ERR(rstc)) {
  463. *ptr = rstc;
  464. devres_add(dev, ptr);
  465. } else {
  466. devres_free(ptr);
  467. }
  468. return rstc;
  469. }
  470. EXPORT_SYMBOL_GPL(__devm_reset_control_get);
  471. /**
  472. * device_reset - find reset controller associated with the device
  473. * and perform reset
  474. * @dev: device to be reset by the controller
  475. *
  476. * Convenience wrapper for reset_control_get() and reset_control_reset().
  477. * This is useful for the common case of devices with single, dedicated reset
  478. * lines.
  479. */
  480. int device_reset(struct device *dev)
  481. {
  482. struct reset_control *rstc;
  483. int ret;
  484. rstc = reset_control_get(dev, NULL);
  485. if (IS_ERR(rstc))
  486. return PTR_ERR(rstc);
  487. ret = reset_control_reset(rstc);
  488. reset_control_put(rstc);
  489. return ret;
  490. }
  491. EXPORT_SYMBOL_GPL(device_reset);
  492. /**
  493. * APIs to manage an array of reset controls.
  494. */
  495. /**
  496. * of_reset_control_get_count - Count number of resets available with a device
  497. *
  498. * @node: device node that contains 'resets'.
  499. *
  500. * Returns positive reset count on success, or error number on failure and
  501. * on count being zero.
  502. */
  503. static int of_reset_control_get_count(struct device_node *node)
  504. {
  505. int count;
  506. if (!node)
  507. return -EINVAL;
  508. count = of_count_phandle_with_args(node, "resets", "#reset-cells");
  509. if (count == 0)
  510. count = -ENOENT;
  511. return count;
  512. }
  513. /**
  514. * of_reset_control_array_get - Get a list of reset controls using
  515. * device node.
  516. *
  517. * @np: device node for the device that requests the reset controls array
  518. * @shared: whether reset controls are shared or not
  519. * @optional: whether it is optional to get the reset controls
  520. *
  521. * Returns pointer to allocated reset_control_array on success or
  522. * error on failure
  523. */
  524. struct reset_control *
  525. of_reset_control_array_get(struct device_node *np, bool shared, bool optional)
  526. {
  527. struct reset_control_array *resets;
  528. struct reset_control *rstc;
  529. int num, i;
  530. num = of_reset_control_get_count(np);
  531. if (num < 0)
  532. return optional ? NULL : ERR_PTR(num);
  533. resets = kzalloc(sizeof(*resets) + sizeof(resets->rstc[0]) * num,
  534. GFP_KERNEL);
  535. if (!resets)
  536. return ERR_PTR(-ENOMEM);
  537. for (i = 0; i < num; i++) {
  538. rstc = __of_reset_control_get(np, NULL, i, shared, optional);
  539. if (IS_ERR(rstc))
  540. goto err_rst;
  541. resets->rstc[i] = rstc;
  542. }
  543. resets->num_rstcs = num;
  544. resets->base.array = true;
  545. return &resets->base;
  546. err_rst:
  547. mutex_lock(&reset_list_mutex);
  548. while (--i >= 0)
  549. __reset_control_put_internal(resets->rstc[i]);
  550. mutex_unlock(&reset_list_mutex);
  551. kfree(resets);
  552. return rstc;
  553. }
  554. EXPORT_SYMBOL_GPL(of_reset_control_array_get);
  555. /**
  556. * devm_reset_control_array_get - Resource managed reset control array get
  557. *
  558. * @dev: device that requests the list of reset controls
  559. * @shared: whether reset controls are shared or not
  560. * @optional: whether it is optional to get the reset controls
  561. *
  562. * The reset control array APIs are intended for a list of resets
  563. * that just have to be asserted or deasserted, without any
  564. * requirements on the order.
  565. *
  566. * Returns pointer to allocated reset_control_array on success or
  567. * error on failure
  568. */
  569. struct reset_control *
  570. devm_reset_control_array_get(struct device *dev, bool shared, bool optional)
  571. {
  572. struct reset_control **devres;
  573. struct reset_control *rstc;
  574. devres = devres_alloc(devm_reset_control_release, sizeof(*devres),
  575. GFP_KERNEL);
  576. if (!devres)
  577. return ERR_PTR(-ENOMEM);
  578. rstc = of_reset_control_array_get(dev->of_node, shared, optional);
  579. if (IS_ERR(rstc)) {
  580. devres_free(devres);
  581. return rstc;
  582. }
  583. *devres = rstc;
  584. devres_add(dev, devres);
  585. return rstc;
  586. }
  587. EXPORT_SYMBOL_GPL(devm_reset_control_array_get);