omap_hwspinlock.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * OMAP hardware spinlock driver
  4. *
  5. * Copyright (C) 2010-2021 Texas Instruments Incorporated - https://www.ti.com
  6. *
  7. * Contact: Simon Que <[email protected]>
  8. * Hari Kanigeri <[email protected]>
  9. * Ohad Ben-Cohen <[email protected]>
  10. * Suman Anna <[email protected]>
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/delay.h>
  16. #include <linux/io.h>
  17. #include <linux/bitops.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/hwspinlock.h>
  22. #include <linux/of.h>
  23. #include <linux/platform_device.h>
  24. #include "hwspinlock_internal.h"
  25. /* Spinlock register offsets */
  26. #define SYSSTATUS_OFFSET 0x0014
  27. #define LOCK_BASE_OFFSET 0x0800
  28. #define SPINLOCK_NUMLOCKS_BIT_OFFSET (24)
  29. /* Possible values of SPINLOCK_LOCK_REG */
  30. #define SPINLOCK_NOTTAKEN (0) /* free */
  31. #define SPINLOCK_TAKEN (1) /* locked */
  32. static int omap_hwspinlock_trylock(struct hwspinlock *lock)
  33. {
  34. void __iomem *lock_addr = lock->priv;
  35. /* attempt to acquire the lock by reading its value */
  36. return (SPINLOCK_NOTTAKEN == readl(lock_addr));
  37. }
  38. static void omap_hwspinlock_unlock(struct hwspinlock *lock)
  39. {
  40. void __iomem *lock_addr = lock->priv;
  41. /* release the lock by writing 0 to it */
  42. writel(SPINLOCK_NOTTAKEN, lock_addr);
  43. }
  44. /*
  45. * relax the OMAP interconnect while spinning on it.
  46. *
  47. * The specs recommended that the retry delay time will be
  48. * just over half of the time that a requester would be
  49. * expected to hold the lock.
  50. *
  51. * The number below is taken from an hardware specs example,
  52. * obviously it is somewhat arbitrary.
  53. */
  54. static void omap_hwspinlock_relax(struct hwspinlock *lock)
  55. {
  56. ndelay(50);
  57. }
  58. static const struct hwspinlock_ops omap_hwspinlock_ops = {
  59. .trylock = omap_hwspinlock_trylock,
  60. .unlock = omap_hwspinlock_unlock,
  61. .relax = omap_hwspinlock_relax,
  62. };
  63. static int omap_hwspinlock_probe(struct platform_device *pdev)
  64. {
  65. struct device_node *node = pdev->dev.of_node;
  66. struct hwspinlock_device *bank;
  67. struct hwspinlock *hwlock;
  68. void __iomem *io_base;
  69. int num_locks, i, ret;
  70. /* Only a single hwspinlock block device is supported */
  71. int base_id = 0;
  72. if (!node)
  73. return -ENODEV;
  74. io_base = devm_platform_ioremap_resource(pdev, 0);
  75. if (IS_ERR(io_base))
  76. return PTR_ERR(io_base);
  77. /*
  78. * make sure the module is enabled and clocked before reading
  79. * the module SYSSTATUS register
  80. */
  81. pm_runtime_enable(&pdev->dev);
  82. ret = pm_runtime_resume_and_get(&pdev->dev);
  83. if (ret < 0)
  84. goto runtime_err;
  85. /* Determine number of locks */
  86. i = readl(io_base + SYSSTATUS_OFFSET);
  87. i >>= SPINLOCK_NUMLOCKS_BIT_OFFSET;
  88. /*
  89. * runtime PM will make sure the clock of this module is
  90. * enabled again iff at least one lock is requested
  91. */
  92. ret = pm_runtime_put(&pdev->dev);
  93. if (ret < 0)
  94. goto runtime_err;
  95. /* one of the four lsb's must be set, and nothing else */
  96. if (hweight_long(i & 0xf) != 1 || i > 8) {
  97. ret = -EINVAL;
  98. goto runtime_err;
  99. }
  100. num_locks = i * 32; /* actual number of locks in this device */
  101. bank = devm_kzalloc(&pdev->dev, struct_size(bank, lock, num_locks),
  102. GFP_KERNEL);
  103. if (!bank) {
  104. ret = -ENOMEM;
  105. goto runtime_err;
  106. }
  107. platform_set_drvdata(pdev, bank);
  108. for (i = 0, hwlock = &bank->lock[0]; i < num_locks; i++, hwlock++)
  109. hwlock->priv = io_base + LOCK_BASE_OFFSET + sizeof(u32) * i;
  110. ret = hwspin_lock_register(bank, &pdev->dev, &omap_hwspinlock_ops,
  111. base_id, num_locks);
  112. if (ret)
  113. goto runtime_err;
  114. dev_dbg(&pdev->dev, "Registered %d locks with HwSpinlock core\n",
  115. num_locks);
  116. return 0;
  117. runtime_err:
  118. pm_runtime_disable(&pdev->dev);
  119. return ret;
  120. }
  121. static int omap_hwspinlock_remove(struct platform_device *pdev)
  122. {
  123. struct hwspinlock_device *bank = platform_get_drvdata(pdev);
  124. int ret;
  125. ret = hwspin_lock_unregister(bank);
  126. if (ret) {
  127. dev_err(&pdev->dev, "%s failed: %d\n", __func__, ret);
  128. return ret;
  129. }
  130. pm_runtime_disable(&pdev->dev);
  131. return 0;
  132. }
  133. static const struct of_device_id omap_hwspinlock_of_match[] = {
  134. { .compatible = "ti,omap4-hwspinlock", },
  135. { .compatible = "ti,am64-hwspinlock", },
  136. { .compatible = "ti,am654-hwspinlock", },
  137. { /* end */ },
  138. };
  139. MODULE_DEVICE_TABLE(of, omap_hwspinlock_of_match);
  140. static struct platform_driver omap_hwspinlock_driver = {
  141. .probe = omap_hwspinlock_probe,
  142. .remove = omap_hwspinlock_remove,
  143. .driver = {
  144. .name = "omap_hwspinlock",
  145. .of_match_table = of_match_ptr(omap_hwspinlock_of_match),
  146. },
  147. };
  148. static int __init omap_hwspinlock_init(void)
  149. {
  150. return platform_driver_register(&omap_hwspinlock_driver);
  151. }
  152. /* board init code might need to reserve hwspinlocks for predefined purposes */
  153. postcore_initcall(omap_hwspinlock_init);
  154. static void __exit omap_hwspinlock_exit(void)
  155. {
  156. platform_driver_unregister(&omap_hwspinlock_driver);
  157. }
  158. module_exit(omap_hwspinlock_exit);
  159. MODULE_LICENSE("GPL v2");
  160. MODULE_DESCRIPTION("Hardware spinlock driver for OMAP");
  161. MODULE_AUTHOR("Simon Que <[email protected]>");
  162. MODULE_AUTHOR("Hari Kanigeri <[email protected]>");
  163. MODULE_AUTHOR("Ohad Ben-Cohen <[email protected]>");