sirf_hwspinlock.c 3.0 KB

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