omap_hwspinlock.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 hwspinlock_pdata *pdata = pdev->dev.platform_data;
  83. struct omap_hwspinlock *omap_lock;
  84. struct omap_hwspinlock_state *state;
  85. struct resource *res;
  86. void __iomem *io_base;
  87. int i, ret;
  88. if (!pdata)
  89. return -ENODEV;
  90. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  91. if (!res)
  92. return -ENODEV;
  93. io_base = ioremap(res->start, resource_size(res));
  94. if (!io_base)
  95. return -ENOMEM;
  96. /* Determine number of locks */
  97. i = readl(io_base + SYSSTATUS_OFFSET);
  98. i >>= SPINLOCK_NUMLOCKS_BIT_OFFSET;
  99. /* one of the four lsb's must be set, and nothing else */
  100. if (hweight_long(i & 0xf) != 1 || i > 8) {
  101. ret = -EINVAL;
  102. goto iounmap_base;
  103. }
  104. i *= 32; /* actual number of locks in this device */
  105. state = kzalloc(sizeof(*state) + i * sizeof(*omap_lock), GFP_KERNEL);
  106. if (!state) {
  107. ret = -ENOMEM;
  108. goto iounmap_base;
  109. }
  110. state->num_locks = i;
  111. state->io_base = io_base;
  112. platform_set_drvdata(pdev, state);
  113. /*
  114. * runtime PM will make sure the clock of this module is
  115. * enabled iff at least one lock is requested
  116. */
  117. pm_runtime_enable(&pdev->dev);
  118. for (i = 0; i < state->num_locks; i++) {
  119. omap_lock = &state->lock[i];
  120. omap_lock->lock.dev = &pdev->dev;
  121. omap_lock->lock.id = pdata->base_id + i;
  122. omap_lock->lock.ops = &omap_hwspinlock_ops;
  123. omap_lock->addr = io_base + LOCK_BASE_OFFSET + sizeof(u32) * i;
  124. ret = hwspin_lock_register(&omap_lock->lock);
  125. if (ret)
  126. goto free_locks;
  127. }
  128. return 0;
  129. free_locks:
  130. while (--i >= 0)
  131. hwspin_lock_unregister(i);
  132. pm_runtime_disable(&pdev->dev);
  133. kfree(state);
  134. iounmap_base:
  135. iounmap(io_base);
  136. return ret;
  137. }
  138. static int omap_hwspinlock_remove(struct platform_device *pdev)
  139. {
  140. struct omap_hwspinlock_state *state = platform_get_drvdata(pdev);
  141. struct hwspinlock *lock;
  142. int i;
  143. for (i = 0; i < state->num_locks; i++) {
  144. lock = hwspin_lock_unregister(i);
  145. /* this shouldn't happen at this point. if it does, at least
  146. * don't continue with the remove */
  147. if (!lock) {
  148. dev_err(&pdev->dev, "%s: failed on %d\n", __func__, i);
  149. return -EBUSY;
  150. }
  151. }
  152. pm_runtime_disable(&pdev->dev);
  153. iounmap(state->io_base);
  154. kfree(state);
  155. return 0;
  156. }
  157. static struct platform_driver omap_hwspinlock_driver = {
  158. .probe = omap_hwspinlock_probe,
  159. .remove = omap_hwspinlock_remove,
  160. .driver = {
  161. .name = "omap_hwspinlock",
  162. .owner = THIS_MODULE,
  163. },
  164. };
  165. static int __init omap_hwspinlock_init(void)
  166. {
  167. return platform_driver_register(&omap_hwspinlock_driver);
  168. }
  169. /* board init code might need to reserve hwspinlocks for predefined purposes */
  170. postcore_initcall(omap_hwspinlock_init);
  171. static void __exit omap_hwspinlock_exit(void)
  172. {
  173. platform_driver_unregister(&omap_hwspinlock_driver);
  174. }
  175. module_exit(omap_hwspinlock_exit);
  176. MODULE_LICENSE("GPL v2");
  177. MODULE_DESCRIPTION("Hardware spinlock driver for OMAP");
  178. MODULE_AUTHOR("Simon Que <sque@ti.com>");
  179. MODULE_AUTHOR("Hari Kanigeri <h-kanigeri2@ti.com>");
  180. MODULE_AUTHOR("Ohad Ben-Cohen <ohad@wizery.com>");