omap_hwspinlock.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * OMAP hardware spinlock driver
  3. *
  4. * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Contact: Simon Que <sque@ti.com>
  7. * Hari Kanigeri <h-kanigeri2@ti.com>
  8. * Ohad Ben-Cohen <ohad@wizery.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * version 2 as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/device.h>
  22. #include <linux/delay.h>
  23. #include <linux/io.h>
  24. #include <linux/bitops.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/hwspinlock.h>
  29. #include <linux/platform_device.h>
  30. #include "hwspinlock_internal.h"
  31. /* Spinlock register offsets */
  32. #define SYSSTATUS_OFFSET 0x0014
  33. #define LOCK_BASE_OFFSET 0x0800
  34. #define SPINLOCK_NUMLOCKS_BIT_OFFSET (24)
  35. /* Possible values of SPINLOCK_LOCK_REG */
  36. #define SPINLOCK_NOTTAKEN (0) /* free */
  37. #define SPINLOCK_TAKEN (1) /* locked */
  38. #define to_omap_hwspinlock(lock) \
  39. container_of(lock, struct omap_hwspinlock, lock)
  40. struct omap_hwspinlock {
  41. struct hwspinlock lock;
  42. void __iomem *addr;
  43. };
  44. struct omap_hwspinlock_state {
  45. int num_locks; /* Total number of locks in system */
  46. void __iomem *io_base; /* Mapped base address */
  47. };
  48. static int omap_hwspinlock_trylock(struct hwspinlock *lock)
  49. {
  50. struct omap_hwspinlock *omap_lock = to_omap_hwspinlock(lock);
  51. /* attempt to acquire the lock by reading its value */
  52. return (SPINLOCK_NOTTAKEN == readl(omap_lock->addr));
  53. }
  54. static void omap_hwspinlock_unlock(struct hwspinlock *lock)
  55. {
  56. struct omap_hwspinlock *omap_lock = to_omap_hwspinlock(lock);
  57. /* release the lock by writing 0 to it */
  58. writel(SPINLOCK_NOTTAKEN, omap_lock->addr);
  59. }
  60. /*
  61. * relax the OMAP interconnect while spinning on it.
  62. *
  63. * The specs recommended that the retry delay time will be
  64. * just over half of the time that a requester would be
  65. * expected to hold the lock.
  66. *
  67. * The number below is taken from an hardware specs example,
  68. * obviously it is somewhat arbitrary.
  69. */
  70. static void omap_hwspinlock_relax(struct hwspinlock *lock)
  71. {
  72. ndelay(50);
  73. }
  74. static const struct hwspinlock_ops omap_hwspinlock_ops = {
  75. .trylock = omap_hwspinlock_trylock,
  76. .unlock = omap_hwspinlock_unlock,
  77. .relax = omap_hwspinlock_relax,
  78. };
  79. static int __devinit omap_hwspinlock_probe(struct platform_device *pdev)
  80. {
  81. struct omap_hwspinlock *omap_lock;
  82. struct omap_hwspinlock_state *state;
  83. struct hwspinlock *lock;
  84. struct resource *res;
  85. void __iomem *io_base;
  86. int i, ret;
  87. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  88. if (!res)
  89. return -ENODEV;
  90. state = kzalloc(sizeof(*state), GFP_KERNEL);
  91. if (!state)
  92. return -ENOMEM;
  93. io_base = ioremap(res->start, resource_size(res));
  94. if (!io_base) {
  95. ret = -ENOMEM;
  96. goto free_state;
  97. }
  98. /* Determine number of locks */
  99. i = readl(io_base + SYSSTATUS_OFFSET);
  100. i >>= SPINLOCK_NUMLOCKS_BIT_OFFSET;
  101. /* one of the four lsb's must be set, and nothing else */
  102. if (hweight_long(i & 0xf) != 1 || i > 8) {
  103. ret = -EINVAL;
  104. goto iounmap_base;
  105. }
  106. state->num_locks = i * 32;
  107. state->io_base = io_base;
  108. platform_set_drvdata(pdev, state);
  109. /*
  110. * runtime PM will make sure the clock of this module is
  111. * enabled iff at least one lock is requested
  112. */
  113. pm_runtime_enable(&pdev->dev);
  114. for (i = 0; i < state->num_locks; i++) {
  115. omap_lock = kzalloc(sizeof(*omap_lock), GFP_KERNEL);
  116. if (!omap_lock) {
  117. ret = -ENOMEM;
  118. goto free_locks;
  119. }
  120. omap_lock->lock.dev = &pdev->dev;
  121. omap_lock->lock.owner = THIS_MODULE;
  122. omap_lock->lock.id = i;
  123. omap_lock->lock.ops = &omap_hwspinlock_ops;
  124. omap_lock->addr = io_base + LOCK_BASE_OFFSET + sizeof(u32) * i;
  125. ret = hwspin_lock_register(&omap_lock->lock);
  126. if (ret) {
  127. kfree(omap_lock);
  128. goto free_locks;
  129. }
  130. }
  131. return 0;
  132. free_locks:
  133. while (--i >= 0) {
  134. lock = hwspin_lock_unregister(i);
  135. /* this should't happen, but let's give our best effort */
  136. if (!lock) {
  137. dev_err(&pdev->dev, "%s: cleanups failed\n", __func__);
  138. continue;
  139. }
  140. omap_lock = to_omap_hwspinlock(lock);
  141. kfree(omap_lock);
  142. }
  143. pm_runtime_disable(&pdev->dev);
  144. iounmap_base:
  145. iounmap(io_base);
  146. free_state:
  147. kfree(state);
  148. return ret;
  149. }
  150. static int omap_hwspinlock_remove(struct platform_device *pdev)
  151. {
  152. struct omap_hwspinlock_state *state = platform_get_drvdata(pdev);
  153. struct hwspinlock *lock;
  154. struct omap_hwspinlock *omap_lock;
  155. int i;
  156. for (i = 0; i < state->num_locks; i++) {
  157. lock = hwspin_lock_unregister(i);
  158. /* this shouldn't happen at this point. if it does, at least
  159. * don't continue with the remove */
  160. if (!lock) {
  161. dev_err(&pdev->dev, "%s: failed on %d\n", __func__, i);
  162. return -EBUSY;
  163. }
  164. omap_lock = to_omap_hwspinlock(lock);
  165. kfree(omap_lock);
  166. }
  167. pm_runtime_disable(&pdev->dev);
  168. iounmap(state->io_base);
  169. kfree(state);
  170. return 0;
  171. }
  172. static struct platform_driver omap_hwspinlock_driver = {
  173. .probe = omap_hwspinlock_probe,
  174. .remove = omap_hwspinlock_remove,
  175. .driver = {
  176. .name = "omap_hwspinlock",
  177. },
  178. };
  179. static int __init omap_hwspinlock_init(void)
  180. {
  181. return platform_driver_register(&omap_hwspinlock_driver);
  182. }
  183. /* board init code might need to reserve hwspinlocks for predefined purposes */
  184. postcore_initcall(omap_hwspinlock_init);
  185. static void __exit omap_hwspinlock_exit(void)
  186. {
  187. platform_driver_unregister(&omap_hwspinlock_driver);
  188. }
  189. module_exit(omap_hwspinlock_exit);
  190. MODULE_LICENSE("GPL v2");
  191. MODULE_DESCRIPTION("Hardware spinlock driver for OMAP");
  192. MODULE_AUTHOR("Simon Que <sque@ti.com>");
  193. MODULE_AUTHOR("Hari Kanigeri <h-kanigeri2@ti.com>");
  194. MODULE_AUTHOR("Ohad Ben-Cohen <ohad@wizery.com>");