qcom_hwspinlock.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2013, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2015, Sony Mobile Communications AB
  5. */
  6. #include <linux/hwspinlock.h>
  7. #include <linux/io.h>
  8. #include <linux/kernel.h>
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_device.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/regmap.h>
  16. #include "hwspinlock_internal.h"
  17. #define QCOM_MUTEX_APPS_PROC_ID 1
  18. #define QCOM_MUTEX_NUM_LOCKS 32
  19. static int qcom_hwspinlock_trylock(struct hwspinlock *lock)
  20. {
  21. struct regmap_field *field = lock->priv;
  22. u32 lock_owner;
  23. int ret;
  24. ret = regmap_field_write(field, QCOM_MUTEX_APPS_PROC_ID);
  25. if (ret)
  26. return ret;
  27. ret = regmap_field_read(field, &lock_owner);
  28. if (ret)
  29. return ret;
  30. return lock_owner == QCOM_MUTEX_APPS_PROC_ID;
  31. }
  32. static void qcom_hwspinlock_unlock(struct hwspinlock *lock)
  33. {
  34. struct regmap_field *field = lock->priv;
  35. u32 lock_owner;
  36. int ret;
  37. ret = regmap_field_read(field, &lock_owner);
  38. if (ret) {
  39. pr_err("%s: unable to query spinlock owner\n", __func__);
  40. return;
  41. }
  42. if (lock_owner != QCOM_MUTEX_APPS_PROC_ID) {
  43. pr_err("%s: spinlock not owned by us (actual owner is %d)\n",
  44. __func__, lock_owner);
  45. }
  46. ret = regmap_field_write(field, 0);
  47. if (ret)
  48. pr_err("%s: failed to unlock spinlock\n", __func__);
  49. }
  50. static const struct hwspinlock_ops qcom_hwspinlock_ops = {
  51. .trylock = qcom_hwspinlock_trylock,
  52. .unlock = qcom_hwspinlock_unlock,
  53. };
  54. static const struct of_device_id qcom_hwspinlock_of_match[] = {
  55. { .compatible = "qcom,sfpb-mutex" },
  56. { .compatible = "qcom,tcsr-mutex" },
  57. { }
  58. };
  59. MODULE_DEVICE_TABLE(of, qcom_hwspinlock_of_match);
  60. static int qcom_hwspinlock_probe(struct platform_device *pdev)
  61. {
  62. struct hwspinlock_device *bank;
  63. struct device_node *syscon;
  64. struct reg_field field;
  65. struct regmap *regmap;
  66. size_t array_size;
  67. u32 stride;
  68. u32 base;
  69. int ret;
  70. int i;
  71. syscon = of_parse_phandle(pdev->dev.of_node, "syscon", 0);
  72. if (!syscon) {
  73. dev_err(&pdev->dev, "no syscon property\n");
  74. return -ENODEV;
  75. }
  76. regmap = syscon_node_to_regmap(syscon);
  77. of_node_put(syscon);
  78. if (IS_ERR(regmap))
  79. return PTR_ERR(regmap);
  80. ret = of_property_read_u32_index(pdev->dev.of_node, "syscon", 1, &base);
  81. if (ret < 0) {
  82. dev_err(&pdev->dev, "no offset in syscon\n");
  83. return -EINVAL;
  84. }
  85. ret = of_property_read_u32_index(pdev->dev.of_node, "syscon", 2, &stride);
  86. if (ret < 0) {
  87. dev_err(&pdev->dev, "no stride syscon\n");
  88. return -EINVAL;
  89. }
  90. array_size = QCOM_MUTEX_NUM_LOCKS * sizeof(struct hwspinlock);
  91. bank = devm_kzalloc(&pdev->dev, sizeof(*bank) + array_size, GFP_KERNEL);
  92. if (!bank)
  93. return -ENOMEM;
  94. platform_set_drvdata(pdev, bank);
  95. for (i = 0; i < QCOM_MUTEX_NUM_LOCKS; i++) {
  96. field.reg = base + i * stride;
  97. field.lsb = 0;
  98. field.msb = 31;
  99. bank->lock[i].priv = devm_regmap_field_alloc(&pdev->dev,
  100. regmap, field);
  101. }
  102. pm_runtime_enable(&pdev->dev);
  103. ret = hwspin_lock_register(bank, &pdev->dev, &qcom_hwspinlock_ops,
  104. 0, QCOM_MUTEX_NUM_LOCKS);
  105. if (ret)
  106. pm_runtime_disable(&pdev->dev);
  107. return ret;
  108. }
  109. static int qcom_hwspinlock_remove(struct platform_device *pdev)
  110. {
  111. struct hwspinlock_device *bank = platform_get_drvdata(pdev);
  112. int ret;
  113. ret = hwspin_lock_unregister(bank);
  114. if (ret) {
  115. dev_err(&pdev->dev, "%s failed: %d\n", __func__, ret);
  116. return ret;
  117. }
  118. pm_runtime_disable(&pdev->dev);
  119. return 0;
  120. }
  121. static struct platform_driver qcom_hwspinlock_driver = {
  122. .probe = qcom_hwspinlock_probe,
  123. .remove = qcom_hwspinlock_remove,
  124. .driver = {
  125. .name = "qcom_hwspinlock",
  126. .of_match_table = qcom_hwspinlock_of_match,
  127. },
  128. };
  129. static int __init qcom_hwspinlock_init(void)
  130. {
  131. return platform_driver_register(&qcom_hwspinlock_driver);
  132. }
  133. /* board init code might need to reserve hwspinlocks for predefined purposes */
  134. postcore_initcall(qcom_hwspinlock_init);
  135. static void __exit qcom_hwspinlock_exit(void)
  136. {
  137. platform_driver_unregister(&qcom_hwspinlock_driver);
  138. }
  139. module_exit(qcom_hwspinlock_exit);
  140. MODULE_LICENSE("GPL v2");
  141. MODULE_DESCRIPTION("Hardware spinlock driver for Qualcomm SoCs");