sprd_hwspinlock.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Spreadtrum hardware spinlock driver
  4. * Copyright (C) 2017 Spreadtrum - http://www.spreadtrum.com
  5. */
  6. #include <linux/bitops.h>
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/device.h>
  10. #include <linux/hwspinlock.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/slab.h>
  19. #include "hwspinlock_internal.h"
  20. /* hwspinlock registers definition */
  21. #define HWSPINLOCK_RECCTRL 0x4
  22. #define HWSPINLOCK_MASTERID(_X_) (0x80 + 0x4 * (_X_))
  23. #define HWSPINLOCK_TOKEN(_X_) (0x800 + 0x4 * (_X_))
  24. /* unlocked value */
  25. #define HWSPINLOCK_NOTTAKEN 0x55aa10c5
  26. /* bits definition of RECCTRL reg */
  27. #define HWSPINLOCK_USER_BITS 0x1
  28. /* hwspinlock number */
  29. #define SPRD_HWLOCKS_NUM 32
  30. struct sprd_hwspinlock_dev {
  31. void __iomem *base;
  32. struct clk *clk;
  33. struct hwspinlock_device bank;
  34. };
  35. /* try to lock the hardware spinlock */
  36. static int sprd_hwspinlock_trylock(struct hwspinlock *lock)
  37. {
  38. struct sprd_hwspinlock_dev *sprd_hwlock =
  39. dev_get_drvdata(lock->bank->dev);
  40. void __iomem *addr = lock->priv;
  41. int user_id, lock_id;
  42. if (!readl(addr))
  43. return 1;
  44. lock_id = hwlock_to_id(lock);
  45. /* get the hardware spinlock master/user id */
  46. user_id = readl(sprd_hwlock->base + HWSPINLOCK_MASTERID(lock_id));
  47. dev_warn(sprd_hwlock->bank.dev,
  48. "hwspinlock [%d] lock failed and master/user id = %d!\n",
  49. lock_id, user_id);
  50. return 0;
  51. }
  52. /* unlock the hardware spinlock */
  53. static void sprd_hwspinlock_unlock(struct hwspinlock *lock)
  54. {
  55. void __iomem *lock_addr = lock->priv;
  56. writel(HWSPINLOCK_NOTTAKEN, lock_addr);
  57. }
  58. /* The specs recommended below number as the retry delay time */
  59. static void sprd_hwspinlock_relax(struct hwspinlock *lock)
  60. {
  61. ndelay(10);
  62. }
  63. static const struct hwspinlock_ops sprd_hwspinlock_ops = {
  64. .trylock = sprd_hwspinlock_trylock,
  65. .unlock = sprd_hwspinlock_unlock,
  66. .relax = sprd_hwspinlock_relax,
  67. };
  68. static int sprd_hwspinlock_probe(struct platform_device *pdev)
  69. {
  70. struct sprd_hwspinlock_dev *sprd_hwlock;
  71. struct hwspinlock *lock;
  72. struct resource *res;
  73. int i, ret;
  74. if (!pdev->dev.of_node)
  75. return -ENODEV;
  76. sprd_hwlock = devm_kzalloc(&pdev->dev,
  77. sizeof(struct sprd_hwspinlock_dev) +
  78. SPRD_HWLOCKS_NUM * sizeof(*lock),
  79. GFP_KERNEL);
  80. if (!sprd_hwlock)
  81. return -ENOMEM;
  82. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  83. sprd_hwlock->base = devm_ioremap_resource(&pdev->dev, res);
  84. if (IS_ERR(sprd_hwlock->base))
  85. return PTR_ERR(sprd_hwlock->base);
  86. sprd_hwlock->clk = devm_clk_get(&pdev->dev, "enable");
  87. if (IS_ERR(sprd_hwlock->clk)) {
  88. dev_err(&pdev->dev, "get hwspinlock clock failed!\n");
  89. return PTR_ERR(sprd_hwlock->clk);
  90. }
  91. clk_prepare_enable(sprd_hwlock->clk);
  92. /* set the hwspinlock to record user id to identify subsystems */
  93. writel(HWSPINLOCK_USER_BITS, sprd_hwlock->base + HWSPINLOCK_RECCTRL);
  94. for (i = 0; i < SPRD_HWLOCKS_NUM; i++) {
  95. lock = &sprd_hwlock->bank.lock[i];
  96. lock->priv = sprd_hwlock->base + HWSPINLOCK_TOKEN(i);
  97. }
  98. platform_set_drvdata(pdev, sprd_hwlock);
  99. pm_runtime_enable(&pdev->dev);
  100. ret = hwspin_lock_register(&sprd_hwlock->bank, &pdev->dev,
  101. &sprd_hwspinlock_ops, 0, SPRD_HWLOCKS_NUM);
  102. if (ret) {
  103. pm_runtime_disable(&pdev->dev);
  104. clk_disable_unprepare(sprd_hwlock->clk);
  105. return ret;
  106. }
  107. return 0;
  108. }
  109. static int sprd_hwspinlock_remove(struct platform_device *pdev)
  110. {
  111. struct sprd_hwspinlock_dev *sprd_hwlock = platform_get_drvdata(pdev);
  112. hwspin_lock_unregister(&sprd_hwlock->bank);
  113. pm_runtime_disable(&pdev->dev);
  114. clk_disable_unprepare(sprd_hwlock->clk);
  115. return 0;
  116. }
  117. static const struct of_device_id sprd_hwspinlock_of_match[] = {
  118. { .compatible = "sprd,hwspinlock-r3p0", },
  119. { /* sentinel */ }
  120. };
  121. MODULE_DEVICE_TABLE(of, sprd_hwspinlock_of_match);
  122. static struct platform_driver sprd_hwspinlock_driver = {
  123. .probe = sprd_hwspinlock_probe,
  124. .remove = sprd_hwspinlock_remove,
  125. .driver = {
  126. .name = "sprd_hwspinlock",
  127. .of_match_table = of_match_ptr(sprd_hwspinlock_of_match),
  128. },
  129. };
  130. static int __init sprd_hwspinlock_init(void)
  131. {
  132. return platform_driver_register(&sprd_hwspinlock_driver);
  133. }
  134. postcore_initcall(sprd_hwspinlock_init);
  135. static void __exit sprd_hwspinlock_exit(void)
  136. {
  137. platform_driver_unregister(&sprd_hwspinlock_driver);
  138. }
  139. module_exit(sprd_hwspinlock_exit);
  140. MODULE_LICENSE("GPL v2");
  141. MODULE_DESCRIPTION("Hardware spinlock driver for Spreadtrum");
  142. MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");
  143. MODULE_AUTHOR("Lanqing Liu <lanqing.liu@spreadtrum.com>");
  144. MODULE_AUTHOR("Long Cheng <aiden.cheng@spreadtrum.com>");