sirf_hwspinlock.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/slab.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/hwspinlock.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include "hwspinlock_internal.h"
  18. struct sirf_hwspinlock {
  19. void __iomem *io_base;
  20. struct hwspinlock_device bank;
  21. };
  22. /* Number of Hardware Spinlocks*/
  23. #define HW_SPINLOCK_NUMBER 30
  24. /* Hardware spinlock register offsets */
  25. #define HW_SPINLOCK_BASE 0x404
  26. #define HW_SPINLOCK_OFFSET(x) (HW_SPINLOCK_BASE + 0x4 * (x))
  27. static int sirf_hwspinlock_trylock(struct hwspinlock *lock)
  28. {
  29. void __iomem *lock_addr = lock->priv;
  30. /* attempt to acquire the lock by reading value == 1 from it */
  31. return !!readl(lock_addr);
  32. }
  33. static void sirf_hwspinlock_unlock(struct hwspinlock *lock)
  34. {
  35. void __iomem *lock_addr = lock->priv;
  36. /* release the lock by writing 0 to it */
  37. writel(0, lock_addr);
  38. }
  39. static const struct hwspinlock_ops sirf_hwspinlock_ops = {
  40. .trylock = sirf_hwspinlock_trylock,
  41. .unlock = sirf_hwspinlock_unlock,
  42. };
  43. static int sirf_hwspinlock_probe(struct platform_device *pdev)
  44. {
  45. struct sirf_hwspinlock *hwspin;
  46. struct hwspinlock *hwlock;
  47. int idx;
  48. if (!pdev->dev.of_node)
  49. return -ENODEV;
  50. hwspin = devm_kzalloc(&pdev->dev,
  51. struct_size(hwspin, bank.lock,
  52. HW_SPINLOCK_NUMBER),
  53. GFP_KERNEL);
  54. if (!hwspin)
  55. return -ENOMEM;
  56. /* retrieve io base */
  57. hwspin->io_base = devm_platform_ioremap_resource(pdev, 0);
  58. if (IS_ERR(hwspin->io_base))
  59. return PTR_ERR(hwspin->io_base);
  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. return devm_hwspin_lock_register(&pdev->dev, &hwspin->bank,
  66. &sirf_hwspinlock_ops, 0,
  67. HW_SPINLOCK_NUMBER);
  68. }
  69. static const struct of_device_id sirf_hwpinlock_ids[] = {
  70. { .compatible = "sirf,hwspinlock", },
  71. {},
  72. };
  73. MODULE_DEVICE_TABLE(of, sirf_hwpinlock_ids);
  74. static struct platform_driver sirf_hwspinlock_driver = {
  75. .probe = sirf_hwspinlock_probe,
  76. .driver = {
  77. .name = "atlas7_hwspinlock",
  78. .of_match_table = of_match_ptr(sirf_hwpinlock_ids),
  79. },
  80. };
  81. module_platform_driver(sirf_hwspinlock_driver);
  82. MODULE_LICENSE("GPL v2");
  83. MODULE_DESCRIPTION("SIRF Hardware spinlock driver");
  84. MODULE_AUTHOR("Wei Chen <wei.chen@csr.com>");