u8500_hsem.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/delay.h>
  24. #include <linux/io.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. /*
  32. * Implementation of STE's HSem protocol 1 without interrutps.
  33. * The only masterID we allow is '0x01' to force people to use
  34. * HSems for synchronisation between processors rather than processes
  35. * on the ARM core.
  36. */
  37. #define U8500_MAX_SEMAPHORE 32 /* a total of 32 semaphore */
  38. #define RESET_SEMAPHORE (0) /* free */
  39. /*
  40. * CPU ID for master running u8500 kernel.
  41. * Hswpinlocks should only be used to synchonise operations
  42. * between the Cortex A9 core and the other CPUs. Hence
  43. * forcing the masterID to a preset value.
  44. */
  45. #define HSEM_MASTER_ID 0x01
  46. #define HSEM_REGISTER_OFFSET 0x08
  47. #define HSEM_CTRL_REG 0x00
  48. #define HSEM_ICRALL 0x90
  49. #define HSEM_PROTOCOL_1 0x01
  50. static int u8500_hsem_trylock(struct hwspinlock *lock)
  51. {
  52. void __iomem *lock_addr = lock->priv;
  53. writel(HSEM_MASTER_ID, lock_addr);
  54. /* get only first 4 bit and compare to masterID.
  55. * if equal, we have the semaphore, otherwise
  56. * someone else has it.
  57. */
  58. return (HSEM_MASTER_ID == (0x0F & readl(lock_addr)));
  59. }
  60. static void u8500_hsem_unlock(struct hwspinlock *lock)
  61. {
  62. void __iomem *lock_addr = lock->priv;
  63. /* release the lock by writing 0 to it */
  64. writel(RESET_SEMAPHORE, lock_addr);
  65. }
  66. /*
  67. * u8500: what value is recommended here ?
  68. */
  69. static void u8500_hsem_relax(struct hwspinlock *lock)
  70. {
  71. ndelay(50);
  72. }
  73. static const struct hwspinlock_ops u8500_hwspinlock_ops = {
  74. .trylock = u8500_hsem_trylock,
  75. .unlock = u8500_hsem_unlock,
  76. .relax = u8500_hsem_relax,
  77. };
  78. static int __devinit u8500_hsem_probe(struct platform_device *pdev)
  79. {
  80. struct hwspinlock_pdata *pdata = pdev->dev.platform_data;
  81. struct hwspinlock_device *bank;
  82. struct hwspinlock *hwlock;
  83. struct resource *res;
  84. void __iomem *io_base;
  85. int i, ret, num_locks = U8500_MAX_SEMAPHORE;
  86. ulong val;
  87. if (!pdata)
  88. return -ENODEV;
  89. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  90. if (!res)
  91. return -ENODEV;
  92. io_base = ioremap(res->start, resource_size(res));
  93. if (!io_base) {
  94. ret = -ENOMEM;
  95. goto free_state;
  96. }
  97. /* make sure protocol 1 is selected */
  98. val = readl(io_base + HSEM_CTRL_REG);
  99. writel((val & ~HSEM_PROTOCOL_1), io_base + HSEM_CTRL_REG);
  100. /* clear all interrupts */
  101. writel(0xFFFF, io_base + HSEM_ICRALL);
  102. bank = kzalloc(sizeof(*bank) + num_locks * sizeof(*hwlock), GFP_KERNEL);
  103. if (!bank) {
  104. ret = -ENOMEM;
  105. goto iounmap_base;
  106. }
  107. platform_set_drvdata(pdev, bank);
  108. for (i = 0, hwlock = &bank->lock[0]; i < num_locks; i++, hwlock++)
  109. hwlock->priv = io_base + HSEM_REGISTER_OFFSET + sizeof(u32) * i;
  110. /* no pm needed for HSem but required to comply with hwspilock core */
  111. pm_runtime_enable(&pdev->dev);
  112. ret = hwspin_lock_register(bank, &pdev->dev, &u8500_hwspinlock_ops,
  113. pdata->base_id, num_locks);
  114. if (ret)
  115. goto reg_fail;
  116. return 0;
  117. reg_fail:
  118. pm_runtime_disable(&pdev->dev);
  119. kfree(bank);
  120. iounmap_base:
  121. iounmap(io_base);
  122. return ret;
  123. }
  124. static int __devexit u8500_hsem_remove(struct platform_device *pdev)
  125. {
  126. struct hwspinlock_device *bank = platform_get_drvdata(pdev);
  127. void __iomem *io_base = bank->lock[0].priv - HSEM_REGISTER_OFFSET;
  128. int ret;
  129. /* clear all interrupts */
  130. writel(0xFFFF, io_base + HSEM_ICRALL);
  131. ret = hwspin_lock_unregister(bank);
  132. if (ret) {
  133. dev_err(&pdev->dev, "%s failed: %d\n", __func__, ret);
  134. return ret;
  135. }
  136. pm_runtime_disable(&pdev->dev);
  137. iounmap(io_base);
  138. kfree(bank);
  139. return 0;
  140. }
  141. static struct platform_driver u8500_hsem_driver = {
  142. .probe = u8500_hsem_probe,
  143. .remove = __devexit_p(u8500_hsem_remove),
  144. .driver = {
  145. .name = "u8500_hsem",
  146. .owner = THIS_MODULE,
  147. },
  148. };
  149. static int __init u8500_hsem_init(void)
  150. {
  151. return platform_driver_register(&u8500_hsem_driver);
  152. }
  153. /* board init code might need to reserve hwspinlocks for predefined purposes */
  154. postcore_initcall(u8500_hsem_init);
  155. static void __exit u8500_hsem_exit(void)
  156. {
  157. platform_driver_unregister(&u8500_hsem_driver);
  158. }
  159. module_exit(u8500_hsem_exit);
  160. MODULE_LICENSE("GPL v2");
  161. MODULE_DESCRIPTION("Hardware Spinlock driver for u8500");
  162. MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");