sirf_hwspinlock.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SIRF hardware spinlock driver
  4. *
  5. * Copyright (c) 2015 Cambridge Silicon Radio Limited, a CSR plc group company.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/device.h>
  10. #include <linux/io.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/hwspinlock.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include "hwspinlock_internal.h"
  19. struct sirf_hwspinlock {
  20. void __iomem *io_base;
  21. struct hwspinlock_device bank;
  22. };
  23. /* Number of Hardware Spinlocks*/
  24. #define HW_SPINLOCK_NUMBER 30
  25. /* Hardware spinlock register offsets */
  26. #define HW_SPINLOCK_BASE 0x404
  27. #define HW_SPINLOCK_OFFSET(x) (HW_SPINLOCK_BASE + 0x4 * (x))
  28. static int sirf_hwspinlock_trylock(struct hwspinlock *lock)
  29. {
  30. void __iomem *lock_addr = lock->priv;
  31. /* attempt to acquire the lock by reading value == 1 from it */
  32. return !!readl(lock_addr);
  33. }
  34. static void sirf_hwspinlock_unlock(struct hwspinlock *lock)
  35. {
  36. void __iomem *lock_addr = lock->priv;
  37. /* release the lock by writing 0 to it */
  38. writel(0, lock_addr);
  39. }
  40. static const struct hwspinlock_ops sirf_hwspinlock_ops = {
  41. .trylock = sirf_hwspinlock_trylock,
  42. .unlock = sirf_hwspinlock_unlock,
  43. };
  44. static int sirf_hwspinlock_probe(struct platform_device *pdev)
  45. {
  46. struct sirf_hwspinlock *hwspin;
  47. struct hwspinlock *hwlock;
  48. int idx, ret;
  49. if (!pdev->dev.of_node)
  50. return -ENODEV;
  51. hwspin = devm_kzalloc(&pdev->dev,
  52. struct_size(hwspin, bank.lock,
  53. HW_SPINLOCK_NUMBER),
  54. GFP_KERNEL);
  55. if (!hwspin)
  56. return -ENOMEM;
  57. /* retrieve io base */
  58. hwspin->io_base = of_iomap(pdev->dev.of_node, 0);
  59. if (!hwspin->io_base)
  60. return -ENOMEM;
  61. for (idx = 0; idx < HW_SPINLOCK_NUMBER; idx++) {
  62. hwlock = &hwspin->bank.lock[idx];
  63. hwlock->priv = hwspin->io_base + HW_SPINLOCK_OFFSET(idx);
  64. }
  65. platform_set_drvdata(pdev, hwspin);
  66. pm_runtime_enable(&pdev->dev);
  67. ret = hwspin_lock_register(&hwspin->bank, &pdev->dev,
  68. &sirf_hwspinlock_ops, 0,
  69. HW_SPINLOCK_NUMBER);
  70. if (ret)
  71. goto reg_failed;
  72. return 0;
  73. reg_failed:
  74. pm_runtime_disable(&pdev->dev);
  75. iounmap(hwspin->io_base);
  76. return ret;
  77. }
  78. static int sirf_hwspinlock_remove(struct platform_device *pdev)
  79. {
  80. struct sirf_hwspinlock *hwspin = platform_get_drvdata(pdev);
  81. int ret;
  82. ret = hwspin_lock_unregister(&hwspin->bank);
  83. if (ret) {
  84. dev_err(&pdev->dev, "%s failed: %d\n", __func__, ret);
  85. return ret;
  86. }
  87. pm_runtime_disable(&pdev->dev);
  88. iounmap(hwspin->io_base);
  89. return 0;
  90. }
  91. static const struct of_device_id sirf_hwpinlock_ids[] = {
  92. { .compatible = "sirf,hwspinlock", },
  93. {},
  94. };
  95. MODULE_DEVICE_TABLE(of, sirf_hwpinlock_ids);
  96. static struct platform_driver sirf_hwspinlock_driver = {
  97. .probe = sirf_hwspinlock_probe,
  98. .remove = sirf_hwspinlock_remove,
  99. .driver = {
  100. .name = "atlas7_hwspinlock",
  101. .of_match_table = of_match_ptr(sirf_hwpinlock_ids),
  102. },
  103. };
  104. module_platform_driver(sirf_hwspinlock_driver);
  105. MODULE_LICENSE("GPL v2");
  106. MODULE_DESCRIPTION("SIRF Hardware spinlock driver");
  107. MODULE_AUTHOR("Wei Chen <wei.chen@csr.com>");