u8500_hsem.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * u8500 HWSEM driver
  3. *
  4. * Copyright (C) 2010-2011 ST-Ericsson
  5. *
  6. * Implements u8500 semaphore handling for protocol 1, no interrupts.
  7. *
  8. * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
  9. * Heavily borrowed from the work of :
  10. * Simon Que <sque@ti.com>
  11. * Hari Kanigeri <h-kanigeri2@ti.com>
  12. * Ohad Ben-Cohen <ohad@wizery.com>
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * version 2 as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful, but
  19. * WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * General Public License for more details.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/delay.h>
  25. #include <linux/io.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/slab.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/hwspinlock.h>
  30. #include <linux/platform_device.h>
  31. #include "hwspinlock_internal.h"
  32. /*
  33. * Implementation of STE's HSem protocol 1 without interrutps.
  34. * The only masterID we allow is '0x01' to force people to use
  35. * HSems for synchronisation between processors rather than processes
  36. * on the ARM core.
  37. */
  38. #define U8500_MAX_SEMAPHORE 32 /* a total of 32 semaphore */
  39. #define RESET_SEMAPHORE (0) /* free */
  40. /*
  41. * CPU ID for master running u8500 kernel.
  42. * Hswpinlocks should only be used to synchonise operations
  43. * between the Cortex A9 core and the other CPUs. Hence
  44. * forcing the masterID to a preset value.
  45. */
  46. #define HSEM_MASTER_ID 0x01
  47. #define HSEM_REGISTER_OFFSET 0x08
  48. #define HSEM_CTRL_REG 0x00
  49. #define HSEM_ICRALL 0x90
  50. #define HSEM_PROTOCOL_1 0x01
  51. static int u8500_hsem_trylock(struct hwspinlock *lock)
  52. {
  53. void __iomem *lock_addr = lock->priv;
  54. writel(HSEM_MASTER_ID, lock_addr);
  55. /* get only first 4 bit and compare to masterID.
  56. * if equal, we have the semaphore, otherwise
  57. * someone else has it.
  58. */
  59. return (HSEM_MASTER_ID == (0x0F & readl(lock_addr)));
  60. }
  61. static void u8500_hsem_unlock(struct hwspinlock *lock)
  62. {
  63. void __iomem *lock_addr = lock->priv;
  64. /* release the lock by writing 0 to it */
  65. writel(RESET_SEMAPHORE, lock_addr);
  66. }
  67. /*
  68. * u8500: what value is recommended here ?
  69. */
  70. static void u8500_hsem_relax(struct hwspinlock *lock)
  71. {
  72. ndelay(50);
  73. }
  74. static const struct hwspinlock_ops u8500_hwspinlock_ops = {
  75. .trylock = u8500_hsem_trylock,
  76. .unlock = u8500_hsem_unlock,
  77. .relax = u8500_hsem_relax,
  78. };
  79. static int u8500_hsem_probe(struct platform_device *pdev)
  80. {
  81. struct hwspinlock_pdata *pdata = pdev->dev.platform_data;
  82. struct hwspinlock_device *bank;
  83. struct hwspinlock *hwlock;
  84. struct resource *res;
  85. void __iomem *io_base;
  86. int i, ret, num_locks = U8500_MAX_SEMAPHORE;
  87. ulong val;
  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. /* make sure protocol 1 is selected */
  97. val = readl(io_base + HSEM_CTRL_REG);
  98. writel((val & ~HSEM_PROTOCOL_1), io_base + HSEM_CTRL_REG);
  99. /* clear all interrupts */
  100. writel(0xFFFF, io_base + HSEM_ICRALL);
  101. bank = kzalloc(sizeof(*bank) + num_locks * sizeof(*hwlock), GFP_KERNEL);
  102. if (!bank) {
  103. ret = -ENOMEM;
  104. goto iounmap_base;
  105. }
  106. platform_set_drvdata(pdev, bank);
  107. for (i = 0, hwlock = &bank->lock[0]; i < num_locks; i++, hwlock++)
  108. hwlock->priv = io_base + HSEM_REGISTER_OFFSET + sizeof(u32) * i;
  109. /* no pm needed for HSem but required to comply with hwspilock core */
  110. pm_runtime_enable(&pdev->dev);
  111. ret = hwspin_lock_register(bank, &pdev->dev, &u8500_hwspinlock_ops,
  112. pdata->base_id, num_locks);
  113. if (ret)
  114. goto reg_fail;
  115. return 0;
  116. reg_fail:
  117. pm_runtime_disable(&pdev->dev);
  118. kfree(bank);
  119. iounmap_base:
  120. iounmap(io_base);
  121. return ret;
  122. }
  123. static int u8500_hsem_remove(struct platform_device *pdev)
  124. {
  125. struct hwspinlock_device *bank = platform_get_drvdata(pdev);
  126. void __iomem *io_base = bank->lock[0].priv - HSEM_REGISTER_OFFSET;
  127. int ret;
  128. /* clear all interrupts */
  129. writel(0xFFFF, io_base + HSEM_ICRALL);
  130. ret = hwspin_lock_unregister(bank);
  131. if (ret) {
  132. dev_err(&pdev->dev, "%s failed: %d\n", __func__, ret);
  133. return ret;
  134. }
  135. pm_runtime_disable(&pdev->dev);
  136. iounmap(io_base);
  137. kfree(bank);
  138. return 0;
  139. }
  140. static struct platform_driver u8500_hsem_driver = {
  141. .probe = u8500_hsem_probe,
  142. .remove = u8500_hsem_remove,
  143. .driver = {
  144. .name = "u8500_hsem",
  145. },
  146. };
  147. static int __init u8500_hsem_init(void)
  148. {
  149. return platform_driver_register(&u8500_hsem_driver);
  150. }
  151. /* board init code might need to reserve hwspinlocks for predefined purposes */
  152. postcore_initcall(u8500_hsem_init);
  153. static void __exit u8500_hsem_exit(void)
  154. {
  155. platform_driver_unregister(&u8500_hsem_driver);
  156. }
  157. module_exit(u8500_hsem_exit);
  158. MODULE_LICENSE("GPL v2");
  159. MODULE_DESCRIPTION("Hardware Spinlock driver for u8500");
  160. MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");