i2c-designware-baytrail.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Intel BayTrail PMIC I2C bus semaphore implementaion
  3. * Copyright (c) 2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/acpi.h>
  18. #include <linux/i2c.h>
  19. #include <linux/interrupt.h>
  20. #include <asm/iosf_mbi.h>
  21. #include "i2c-designware-core.h"
  22. #define SEMAPHORE_TIMEOUT 100
  23. #define PUNIT_SEMAPHORE 0x7
  24. #define PUNIT_SEMAPHORE_BIT BIT(0)
  25. #define PUNIT_SEMAPHORE_ACQUIRE BIT(1)
  26. static unsigned long acquired;
  27. static int get_sem(struct device *dev, u32 *sem)
  28. {
  29. u32 data;
  30. int ret;
  31. ret = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ, PUNIT_SEMAPHORE,
  32. &data);
  33. if (ret) {
  34. dev_err(dev, "iosf failed to read punit semaphore\n");
  35. return ret;
  36. }
  37. *sem = data & PUNIT_SEMAPHORE_BIT;
  38. return 0;
  39. }
  40. static void reset_semaphore(struct device *dev)
  41. {
  42. u32 data;
  43. if (iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ,
  44. PUNIT_SEMAPHORE, &data)) {
  45. dev_err(dev, "iosf failed to reset punit semaphore during read\n");
  46. return;
  47. }
  48. data &= ~PUNIT_SEMAPHORE_BIT;
  49. if (iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE,
  50. PUNIT_SEMAPHORE, data))
  51. dev_err(dev, "iosf failed to reset punit semaphore during write\n");
  52. }
  53. static int baytrail_i2c_acquire(struct dw_i2c_dev *dev)
  54. {
  55. u32 sem;
  56. int ret;
  57. unsigned long start, end;
  58. might_sleep();
  59. if (!dev || !dev->dev)
  60. return -ENODEV;
  61. if (!dev->release_lock)
  62. return 0;
  63. /* host driver writes to side band semaphore register */
  64. ret = iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE,
  65. PUNIT_SEMAPHORE, PUNIT_SEMAPHORE_ACQUIRE);
  66. if (ret) {
  67. dev_err(dev->dev, "iosf punit semaphore request failed\n");
  68. return ret;
  69. }
  70. /* host driver waits for bit 0 to be set in semaphore register */
  71. start = jiffies;
  72. end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT);
  73. do {
  74. ret = get_sem(dev->dev, &sem);
  75. if (!ret && sem) {
  76. acquired = jiffies;
  77. dev_dbg(dev->dev, "punit semaphore acquired after %ums\n",
  78. jiffies_to_msecs(jiffies - start));
  79. return 0;
  80. }
  81. usleep_range(1000, 2000);
  82. } while (time_before(jiffies, end));
  83. dev_err(dev->dev, "punit semaphore timed out, resetting\n");
  84. reset_semaphore(dev->dev);
  85. ret = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ,
  86. PUNIT_SEMAPHORE, &sem);
  87. if (ret)
  88. dev_err(dev->dev, "iosf failed to read punit semaphore\n");
  89. else
  90. dev_err(dev->dev, "PUNIT SEM: %d\n", sem);
  91. WARN_ON(1);
  92. return -ETIMEDOUT;
  93. }
  94. static void baytrail_i2c_release(struct dw_i2c_dev *dev)
  95. {
  96. if (!dev || !dev->dev)
  97. return;
  98. if (!dev->acquire_lock)
  99. return;
  100. reset_semaphore(dev->dev);
  101. dev_dbg(dev->dev, "punit semaphore held for %ums\n",
  102. jiffies_to_msecs(jiffies - acquired));
  103. }
  104. int i2c_dw_eval_lock_support(struct dw_i2c_dev *dev)
  105. {
  106. acpi_status status;
  107. unsigned long long shared_host = 0;
  108. acpi_handle handle;
  109. if (!dev || !dev->dev)
  110. return 0;
  111. handle = ACPI_HANDLE(dev->dev);
  112. if (!handle)
  113. return 0;
  114. status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host);
  115. if (ACPI_FAILURE(status))
  116. return 0;
  117. if (shared_host) {
  118. dev_info(dev->dev, "I2C bus managed by PUNIT\n");
  119. dev->acquire_lock = baytrail_i2c_acquire;
  120. dev->release_lock = baytrail_i2c_release;
  121. dev->pm_runtime_disabled = true;
  122. }
  123. if (!iosf_mbi_available())
  124. return -EPROBE_DEFER;
  125. return 0;
  126. }
  127. MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
  128. MODULE_DESCRIPTION("Baytrail I2C Semaphore driver");
  129. MODULE_LICENSE("GPL v2");