hwspinlock-uclass.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
  2. /*
  3. * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
  4. */
  5. #define LOG_CATEGORY UCLASS_HWSPINLOCK
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <hwspinlock.h>
  10. #include <log.h>
  11. #include <dm/device-internal.h>
  12. #include <dm/device_compat.h>
  13. #include <linux/compat.h>
  14. #include <asm/global_data.h>
  15. static inline const struct hwspinlock_ops *
  16. hwspinlock_dev_ops(struct udevice *dev)
  17. {
  18. return (const struct hwspinlock_ops *)dev->driver->ops;
  19. }
  20. static int hwspinlock_of_xlate_default(struct hwspinlock *hws,
  21. struct ofnode_phandle_args *args)
  22. {
  23. if (args->args_count > 1) {
  24. debug("Invaild args_count: %d\n", args->args_count);
  25. return -EINVAL;
  26. }
  27. if (args->args_count)
  28. hws->id = args->args[0];
  29. else
  30. hws->id = 0;
  31. return 0;
  32. }
  33. int hwspinlock_get_by_index(struct udevice *dev, int index,
  34. struct hwspinlock *hws)
  35. {
  36. int ret;
  37. struct ofnode_phandle_args args;
  38. struct udevice *dev_hws;
  39. const struct hwspinlock_ops *ops;
  40. assert(hws);
  41. hws->dev = NULL;
  42. ret = dev_read_phandle_with_args(dev, "hwlocks", "#hwlock-cells", 1,
  43. index, &args);
  44. if (ret) {
  45. dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
  46. __func__, ret);
  47. return ret;
  48. }
  49. ret = uclass_get_device_by_ofnode(UCLASS_HWSPINLOCK,
  50. args.node, &dev_hws);
  51. if (ret) {
  52. dev_dbg(dev,
  53. "%s: uclass_get_device_by_of_offset failed: err=%d\n",
  54. __func__, ret);
  55. return ret;
  56. }
  57. hws->dev = dev_hws;
  58. ops = hwspinlock_dev_ops(dev_hws);
  59. if (ops->of_xlate)
  60. ret = ops->of_xlate(hws, &args);
  61. else
  62. ret = hwspinlock_of_xlate_default(hws, &args);
  63. if (ret)
  64. dev_dbg(dev, "of_xlate() failed: %d\n", ret);
  65. return ret;
  66. }
  67. int hwspinlock_lock_timeout(struct hwspinlock *hws, unsigned int timeout)
  68. {
  69. const struct hwspinlock_ops *ops;
  70. ulong start;
  71. int ret;
  72. assert(hws);
  73. if (!hws->dev)
  74. return -EINVAL;
  75. ops = hwspinlock_dev_ops(hws->dev);
  76. if (!ops->lock)
  77. return -ENOSYS;
  78. start = get_timer(0);
  79. do {
  80. ret = ops->lock(hws->dev, hws->id);
  81. if (!ret)
  82. return ret;
  83. if (ops->relax)
  84. ops->relax(hws->dev);
  85. } while (get_timer(start) < timeout);
  86. return -ETIMEDOUT;
  87. }
  88. int hwspinlock_unlock(struct hwspinlock *hws)
  89. {
  90. const struct hwspinlock_ops *ops;
  91. assert(hws);
  92. if (!hws->dev)
  93. return -EINVAL;
  94. ops = hwspinlock_dev_ops(hws->dev);
  95. if (!ops->unlock)
  96. return -ENOSYS;
  97. return ops->unlock(hws->dev, hws->id);
  98. }
  99. static int hwspinlock_post_bind(struct udevice *dev)
  100. {
  101. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  102. struct hwspinlock_ops *ops = device_get_ops(dev);
  103. static int reloc_done;
  104. if (!reloc_done) {
  105. if (ops->lock)
  106. ops->lock += gd->reloc_off;
  107. if (ops->unlock)
  108. ops->unlock += gd->reloc_off;
  109. if (ops->relax)
  110. ops->relax += gd->reloc_off;
  111. reloc_done++;
  112. }
  113. #endif
  114. return 0;
  115. }
  116. UCLASS_DRIVER(hwspinlock) = {
  117. .id = UCLASS_HWSPINLOCK,
  118. .name = "hwspinlock",
  119. .post_bind = hwspinlock_post_bind,
  120. };