omap_hwspinlock.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. struct omap_hwspinlock lock[0]; /* Array of 'num_locks' locks */
  48. };
  49. static int omap_hwspinlock_trylock(struct hwspinlock *lock)
  50. {
  51. struct omap_hwspinlock *omap_lock = to_omap_hwspinlock(lock);
  52. /* attempt to acquire the lock by reading its value */
  53. return (SPINLOCK_NOTTAKEN == readl(omap_lock->addr));
  54. }
  55. static void omap_hwspinlock_unlock(struct hwspinlock *lock)
  56. {
  57. struct omap_hwspinlock *omap_lock = to_omap_hwspinlock(lock);
  58. /* release the lock by writing 0 to it */
  59. writel(SPINLOCK_NOTTAKEN, omap_lock->addr);
  60. }
  61. /*
  62. * relax the OMAP interconnect while spinning on it.
  63. *
  64. * The specs recommended that the retry delay time will be
  65. * just over half of the time that a requester would be
  66. * expected to hold the lock.
  67. *
  68. * The number below is taken from an hardware specs example,
  69. * obviously it is somewhat arbitrary.
  70. */
  71. static void omap_hwspinlock_relax(struct hwspinlock *lock)
  72. {
  73. ndelay(50);
  74. }
  75. static const struct hwspinlock_ops omap_hwspinlock_ops = {
  76. .trylock = omap_hwspinlock_trylock,
  77. .unlock = omap_hwspinlock_unlock,
  78. .relax = omap_hwspinlock_relax,
  79. };
  80. static int __devinit omap_hwspinlock_probe(struct platform_device *pdev)
  81. {
  82. struct omap_hwspinlock *omap_lock;
  83. struct omap_hwspinlock_state *state;
  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. io_base = ioremap(res->start, resource_size(res));
  91. if (!io_base)
  92. return -ENOMEM;
  93. /* Determine number of locks */
  94. i = readl(io_base + SYSSTATUS_OFFSET);
  95. i >>= SPINLOCK_NUMLOCKS_BIT_OFFSET;
  96. /* one of the four lsb's must be set, and nothing else */
  97. if (hweight_long(i & 0xf) != 1 || i > 8) {
  98. ret = -EINVAL;
  99. goto iounmap_base;
  100. }
  101. i *= 32; /* actual number of locks in this device */
  102. state = kzalloc(sizeof(*state) + i * sizeof(*omap_lock), GFP_KERNEL);
  103. if (!state) {
  104. ret = -ENOMEM;
  105. goto iounmap_base;
  106. }
  107. state->num_locks = i;
  108. state->io_base = io_base;
  109. platform_set_drvdata(pdev, state);
  110. /*
  111. * runtime PM will make sure the clock of this module is
  112. * enabled iff at least one lock is requested
  113. */
  114. pm_runtime_enable(&pdev->dev);
  115. for (i = 0; i < state->num_locks; i++) {
  116. omap_lock = &state->lock[i];
  117. omap_lock->lock.dev = &pdev->dev;
  118. omap_lock->lock.id = i;
  119. omap_lock->lock.ops = &omap_hwspinlock_ops;
  120. omap_lock->addr = io_base + LOCK_BASE_OFFSET + sizeof(u32) * i;
  121. ret = hwspin_lock_register(&omap_lock->lock);
  122. if (ret)
  123. goto free_locks;
  124. }
  125. return 0;
  126. free_locks:
  127. while (--i >= 0)
  128. hwspin_lock_unregister(i);
  129. pm_runtime_disable(&pdev->dev);
  130. kfree(state);
  131. iounmap_base:
  132. iounmap(io_base);
  133. return ret;
  134. }
  135. static int omap_hwspinlock_remove(struct platform_device *pdev)
  136. {
  137. struct omap_hwspinlock_state *state = platform_get_drvdata(pdev);
  138. struct hwspinlock *lock;
  139. int i;
  140. for (i = 0; i < state->num_locks; i++) {
  141. lock = hwspin_lock_unregister(i);
  142. /* this shouldn't happen at this point. if it does, at least
  143. * don't continue with the remove */
  144. if (!lock) {
  145. dev_err(&pdev->dev, "%s: failed on %d\n", __func__, i);
  146. return -EBUSY;
  147. }
  148. }
  149. pm_runtime_disable(&pdev->dev);
  150. iounmap(state->io_base);
  151. kfree(state);
  152. return 0;
  153. }
  154. static struct platform_driver omap_hwspinlock_driver = {
  155. .probe = omap_hwspinlock_probe,
  156. .remove = omap_hwspinlock_remove,
  157. .driver = {
  158. .name = "omap_hwspinlock",
  159. .owner = THIS_MODULE,
  160. },
  161. };
  162. static int __init omap_hwspinlock_init(void)
  163. {
  164. return platform_driver_register(&omap_hwspinlock_driver);
  165. }
  166. /* board init code might need to reserve hwspinlocks for predefined purposes */
  167. postcore_initcall(omap_hwspinlock_init);
  168. static void __exit omap_hwspinlock_exit(void)
  169. {
  170. platform_driver_unregister(&omap_hwspinlock_driver);
  171. }
  172. module_exit(omap_hwspinlock_exit);
  173. MODULE_LICENSE("GPL v2");
  174. MODULE_DESCRIPTION("Hardware spinlock driver for OMAP");
  175. MODULE_AUTHOR("Simon Que <sque@ti.com>");
  176. MODULE_AUTHOR("Hari Kanigeri <h-kanigeri2@ti.com>");
  177. MODULE_AUTHOR("Ohad Ben-Cohen <ohad@wizery.com>");