omap_hwspinlock.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * OMAP hardware spinlock driver
  3. *
  4. * Copyright (C) 2010-2015 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/of.h>
  30. #include <linux/platform_device.h>
  31. #include "hwspinlock_internal.h"
  32. /* Spinlock register offsets */
  33. #define SYSSTATUS_OFFSET 0x0014
  34. #define LOCK_BASE_OFFSET 0x0800
  35. #define SPINLOCK_NUMLOCKS_BIT_OFFSET (24)
  36. /* Possible values of SPINLOCK_LOCK_REG */
  37. #define SPINLOCK_NOTTAKEN (0) /* free */
  38. #define SPINLOCK_TAKEN (1) /* locked */
  39. static int omap_hwspinlock_trylock(struct hwspinlock *lock)
  40. {
  41. void __iomem *lock_addr = lock->priv;
  42. /* attempt to acquire the lock by reading its value */
  43. return (SPINLOCK_NOTTAKEN == readl(lock_addr));
  44. }
  45. static void omap_hwspinlock_unlock(struct hwspinlock *lock)
  46. {
  47. void __iomem *lock_addr = lock->priv;
  48. /* release the lock by writing 0 to it */
  49. writel(SPINLOCK_NOTTAKEN, lock_addr);
  50. }
  51. /*
  52. * relax the OMAP interconnect while spinning on it.
  53. *
  54. * The specs recommended that the retry delay time will be
  55. * just over half of the time that a requester would be
  56. * expected to hold the lock.
  57. *
  58. * The number below is taken from an hardware specs example,
  59. * obviously it is somewhat arbitrary.
  60. */
  61. static void omap_hwspinlock_relax(struct hwspinlock *lock)
  62. {
  63. ndelay(50);
  64. }
  65. static const struct hwspinlock_ops omap_hwspinlock_ops = {
  66. .trylock = omap_hwspinlock_trylock,
  67. .unlock = omap_hwspinlock_unlock,
  68. .relax = omap_hwspinlock_relax,
  69. };
  70. static int omap_hwspinlock_probe(struct platform_device *pdev)
  71. {
  72. struct device_node *node = pdev->dev.of_node;
  73. struct hwspinlock_device *bank;
  74. struct hwspinlock *hwlock;
  75. struct resource *res;
  76. void __iomem *io_base;
  77. int num_locks, i, ret;
  78. /* Only a single hwspinlock block device is supported */
  79. int base_id = 0;
  80. if (!node)
  81. return -ENODEV;
  82. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  83. if (!res)
  84. return -ENODEV;
  85. io_base = ioremap(res->start, resource_size(res));
  86. if (!io_base)
  87. return -ENOMEM;
  88. /*
  89. * make sure the module is enabled and clocked before reading
  90. * the module SYSSTATUS register
  91. */
  92. pm_runtime_enable(&pdev->dev);
  93. ret = pm_runtime_get_sync(&pdev->dev);
  94. if (ret < 0) {
  95. pm_runtime_put_noidle(&pdev->dev);
  96. goto iounmap_base;
  97. }
  98. /* Determine number of locks */
  99. i = readl(io_base + SYSSTATUS_OFFSET);
  100. i >>= SPINLOCK_NUMLOCKS_BIT_OFFSET;
  101. /*
  102. * runtime PM will make sure the clock of this module is
  103. * enabled again iff at least one lock is requested
  104. */
  105. ret = pm_runtime_put(&pdev->dev);
  106. if (ret < 0)
  107. goto iounmap_base;
  108. /* one of the four lsb's must be set, and nothing else */
  109. if (hweight_long(i & 0xf) != 1 || i > 8) {
  110. ret = -EINVAL;
  111. goto iounmap_base;
  112. }
  113. num_locks = i * 32; /* actual number of locks in this device */
  114. bank = kzalloc(sizeof(*bank) + num_locks * sizeof(*hwlock), GFP_KERNEL);
  115. if (!bank) {
  116. ret = -ENOMEM;
  117. goto iounmap_base;
  118. }
  119. platform_set_drvdata(pdev, bank);
  120. for (i = 0, hwlock = &bank->lock[0]; i < num_locks; i++, hwlock++)
  121. hwlock->priv = io_base + LOCK_BASE_OFFSET + sizeof(u32) * i;
  122. ret = hwspin_lock_register(bank, &pdev->dev, &omap_hwspinlock_ops,
  123. base_id, num_locks);
  124. if (ret)
  125. goto reg_fail;
  126. return 0;
  127. reg_fail:
  128. kfree(bank);
  129. iounmap_base:
  130. pm_runtime_disable(&pdev->dev);
  131. iounmap(io_base);
  132. return ret;
  133. }
  134. static int omap_hwspinlock_remove(struct platform_device *pdev)
  135. {
  136. struct hwspinlock_device *bank = platform_get_drvdata(pdev);
  137. void __iomem *io_base = bank->lock[0].priv - LOCK_BASE_OFFSET;
  138. int ret;
  139. ret = hwspin_lock_unregister(bank);
  140. if (ret) {
  141. dev_err(&pdev->dev, "%s failed: %d\n", __func__, ret);
  142. return ret;
  143. }
  144. pm_runtime_disable(&pdev->dev);
  145. iounmap(io_base);
  146. kfree(bank);
  147. return 0;
  148. }
  149. static const struct of_device_id omap_hwspinlock_of_match[] = {
  150. { .compatible = "ti,omap4-hwspinlock", },
  151. { /* end */ },
  152. };
  153. MODULE_DEVICE_TABLE(of, omap_hwspinlock_of_match);
  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. .of_match_table = of_match_ptr(omap_hwspinlock_of_match),
  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>");