omap_hwspinlock.c 4.8 KB

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