qcom_hwspinlock.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/regmap.h>
  15. #include "hwspinlock_internal.h"
  16. #define QCOM_MUTEX_APPS_PROC_ID 1
  17. #define QCOM_MUTEX_NUM_LOCKS 32
  18. struct qcom_hwspinlock_of_data {
  19. u32 offset;
  20. u32 stride;
  21. const struct regmap_config *regmap_config;
  22. };
  23. static int qcom_hwspinlock_trylock(struct hwspinlock *lock)
  24. {
  25. struct regmap_field *field = lock->priv;
  26. u32 lock_owner;
  27. int ret;
  28. ret = regmap_field_write(field, QCOM_MUTEX_APPS_PROC_ID);
  29. if (ret)
  30. return ret;
  31. ret = regmap_field_read(field, &lock_owner);
  32. if (ret)
  33. return ret;
  34. return lock_owner == QCOM_MUTEX_APPS_PROC_ID;
  35. }
  36. static void qcom_hwspinlock_unlock(struct hwspinlock *lock)
  37. {
  38. struct regmap_field *field = lock->priv;
  39. u32 lock_owner;
  40. int ret;
  41. ret = regmap_field_read(field, &lock_owner);
  42. if (ret) {
  43. pr_err("%s: unable to query spinlock owner\n", __func__);
  44. return;
  45. }
  46. if (lock_owner != QCOM_MUTEX_APPS_PROC_ID) {
  47. pr_err("%s: spinlock not owned by us (actual owner is %d)\n",
  48. __func__, lock_owner);
  49. }
  50. ret = regmap_field_write(field, 0);
  51. if (ret)
  52. pr_err("%s: failed to unlock spinlock\n", __func__);
  53. }
  54. static const struct hwspinlock_ops qcom_hwspinlock_ops = {
  55. .trylock = qcom_hwspinlock_trylock,
  56. .unlock = qcom_hwspinlock_unlock,
  57. };
  58. static const struct regmap_config sfpb_mutex_config = {
  59. .reg_bits = 32,
  60. .reg_stride = 4,
  61. .val_bits = 32,
  62. .max_register = 0x100,
  63. .fast_io = true,
  64. };
  65. static const struct qcom_hwspinlock_of_data of_sfpb_mutex = {
  66. .offset = 0x4,
  67. .stride = 0x4,
  68. .regmap_config = &sfpb_mutex_config,
  69. };
  70. static const struct regmap_config tcsr_msm8226_mutex_config = {
  71. .reg_bits = 32,
  72. .reg_stride = 4,
  73. .val_bits = 32,
  74. .max_register = 0x1000,
  75. .fast_io = true,
  76. };
  77. static const struct qcom_hwspinlock_of_data of_msm8226_tcsr_mutex = {
  78. .offset = 0,
  79. .stride = 0x80,
  80. .regmap_config = &tcsr_msm8226_mutex_config,
  81. };
  82. static const struct regmap_config tcsr_mutex_config = {
  83. .reg_bits = 32,
  84. .reg_stride = 4,
  85. .val_bits = 32,
  86. .max_register = 0x20000,
  87. .fast_io = true,
  88. };
  89. static const struct qcom_hwspinlock_of_data of_tcsr_mutex = {
  90. .offset = 0,
  91. .stride = 0x1000,
  92. .regmap_config = &tcsr_mutex_config,
  93. };
  94. static const struct of_device_id qcom_hwspinlock_of_match[] = {
  95. { .compatible = "qcom,sfpb-mutex", .data = &of_sfpb_mutex },
  96. { .compatible = "qcom,tcsr-mutex", .data = &of_tcsr_mutex },
  97. { .compatible = "qcom,apq8084-tcsr-mutex", .data = &of_msm8226_tcsr_mutex },
  98. { .compatible = "qcom,ipq6018-tcsr-mutex", .data = &of_msm8226_tcsr_mutex },
  99. { .compatible = "qcom,msm8226-tcsr-mutex", .data = &of_msm8226_tcsr_mutex },
  100. { .compatible = "qcom,msm8974-tcsr-mutex", .data = &of_msm8226_tcsr_mutex },
  101. { .compatible = "qcom,msm8994-tcsr-mutex", .data = &of_msm8226_tcsr_mutex },
  102. { }
  103. };
  104. MODULE_DEVICE_TABLE(of, qcom_hwspinlock_of_match);
  105. static struct regmap *qcom_hwspinlock_probe_syscon(struct platform_device *pdev,
  106. u32 *base, u32 *stride)
  107. {
  108. struct device_node *syscon;
  109. struct regmap *regmap;
  110. int ret;
  111. syscon = of_parse_phandle(pdev->dev.of_node, "syscon", 0);
  112. if (!syscon)
  113. return ERR_PTR(-ENODEV);
  114. regmap = syscon_node_to_regmap(syscon);
  115. of_node_put(syscon);
  116. if (IS_ERR(regmap))
  117. return regmap;
  118. ret = of_property_read_u32_index(pdev->dev.of_node, "syscon", 1, base);
  119. if (ret < 0) {
  120. dev_err(&pdev->dev, "no offset in syscon\n");
  121. return ERR_PTR(-EINVAL);
  122. }
  123. ret = of_property_read_u32_index(pdev->dev.of_node, "syscon", 2, stride);
  124. if (ret < 0) {
  125. dev_err(&pdev->dev, "no stride syscon\n");
  126. return ERR_PTR(-EINVAL);
  127. }
  128. return regmap;
  129. }
  130. static struct regmap *qcom_hwspinlock_probe_mmio(struct platform_device *pdev,
  131. u32 *offset, u32 *stride)
  132. {
  133. const struct qcom_hwspinlock_of_data *data;
  134. struct device *dev = &pdev->dev;
  135. void __iomem *base;
  136. data = of_device_get_match_data(dev);
  137. if (!data->regmap_config)
  138. return ERR_PTR(-EINVAL);
  139. *offset = data->offset;
  140. *stride = data->stride;
  141. base = devm_platform_ioremap_resource(pdev, 0);
  142. if (IS_ERR(base))
  143. return ERR_CAST(base);
  144. return devm_regmap_init_mmio(dev, base, data->regmap_config);
  145. }
  146. static int qcom_hwspinlock_probe(struct platform_device *pdev)
  147. {
  148. struct hwspinlock_device *bank;
  149. struct reg_field field;
  150. struct regmap *regmap;
  151. size_t array_size;
  152. u32 stride;
  153. u32 base;
  154. int i;
  155. regmap = qcom_hwspinlock_probe_syscon(pdev, &base, &stride);
  156. if (IS_ERR(regmap) && PTR_ERR(regmap) == -ENODEV)
  157. regmap = qcom_hwspinlock_probe_mmio(pdev, &base, &stride);
  158. if (IS_ERR(regmap))
  159. return PTR_ERR(regmap);
  160. array_size = QCOM_MUTEX_NUM_LOCKS * sizeof(struct hwspinlock);
  161. bank = devm_kzalloc(&pdev->dev, sizeof(*bank) + array_size, GFP_KERNEL);
  162. if (!bank)
  163. return -ENOMEM;
  164. platform_set_drvdata(pdev, bank);
  165. for (i = 0; i < QCOM_MUTEX_NUM_LOCKS; i++) {
  166. field.reg = base + i * stride;
  167. field.lsb = 0;
  168. field.msb = 31;
  169. bank->lock[i].priv = devm_regmap_field_alloc(&pdev->dev,
  170. regmap, field);
  171. }
  172. return devm_hwspin_lock_register(&pdev->dev, bank, &qcom_hwspinlock_ops,
  173. 0, QCOM_MUTEX_NUM_LOCKS);
  174. }
  175. static struct platform_driver qcom_hwspinlock_driver = {
  176. .probe = qcom_hwspinlock_probe,
  177. .driver = {
  178. .name = "qcom_hwspinlock",
  179. .of_match_table = qcom_hwspinlock_of_match,
  180. },
  181. };
  182. static int __init qcom_hwspinlock_init(void)
  183. {
  184. return platform_driver_register(&qcom_hwspinlock_driver);
  185. }
  186. /* board init code might need to reserve hwspinlocks for predefined purposes */
  187. postcore_initcall(qcom_hwspinlock_init);
  188. static void __exit qcom_hwspinlock_exit(void)
  189. {
  190. platform_driver_unregister(&qcom_hwspinlock_driver);
  191. }
  192. module_exit(qcom_hwspinlock_exit);
  193. MODULE_LICENSE("GPL v2");
  194. MODULE_DESCRIPTION("Hardware spinlock driver for Qualcomm SoCs");