sirf_hwspinlock.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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,
  53. struct_size(hwspin, bank.lock,
  54. HW_SPINLOCK_NUMBER),
  55. GFP_KERNEL);
  56. if (!hwspin)
  57. return -ENOMEM;
  58. /* retrieve io base */
  59. hwspin->io_base = of_iomap(pdev->dev.of_node, 0);
  60. if (!hwspin->io_base)
  61. return -ENOMEM;
  62. for (idx = 0; idx < HW_SPINLOCK_NUMBER; idx++) {
  63. hwlock = &hwspin->bank.lock[idx];
  64. hwlock->priv = hwspin->io_base + HW_SPINLOCK_OFFSET(idx);
  65. }
  66. platform_set_drvdata(pdev, hwspin);
  67. pm_runtime_enable(&pdev->dev);
  68. ret = hwspin_lock_register(&hwspin->bank, &pdev->dev,
  69. &sirf_hwspinlock_ops, 0,
  70. HW_SPINLOCK_NUMBER);
  71. if (ret)
  72. goto reg_failed;
  73. return 0;
  74. reg_failed:
  75. pm_runtime_disable(&pdev->dev);
  76. iounmap(hwspin->io_base);
  77. return ret;
  78. }
  79. static int sirf_hwspinlock_remove(struct platform_device *pdev)
  80. {
  81. struct sirf_hwspinlock *hwspin = platform_get_drvdata(pdev);
  82. int ret;
  83. ret = hwspin_lock_unregister(&hwspin->bank);
  84. if (ret) {
  85. dev_err(&pdev->dev, "%s failed: %d\n", __func__, ret);
  86. return ret;
  87. }
  88. pm_runtime_disable(&pdev->dev);
  89. iounmap(hwspin->io_base);
  90. return 0;
  91. }
  92. static const struct of_device_id sirf_hwpinlock_ids[] = {
  93. { .compatible = "sirf,hwspinlock", },
  94. {},
  95. };
  96. MODULE_DEVICE_TABLE(of, sirf_hwpinlock_ids);
  97. static struct platform_driver sirf_hwspinlock_driver = {
  98. .probe = sirf_hwspinlock_probe,
  99. .remove = sirf_hwspinlock_remove,
  100. .driver = {
  101. .name = "atlas7_hwspinlock",
  102. .of_match_table = of_match_ptr(sirf_hwpinlock_ids),
  103. },
  104. };
  105. module_platform_driver(sirf_hwspinlock_driver);
  106. MODULE_LICENSE("GPL v2");
  107. MODULE_DESCRIPTION("SIRF Hardware spinlock driver");
  108. MODULE_AUTHOR("Wei Chen <wei.chen@csr.com>");