omap_hwspinlock.c 5.0 KB

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