i2c-designware-baytrail.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/acpi.h>
  17. #include <linux/i2c.h>
  18. #include <linux/interrupt.h>
  19. #include <asm/iosf_mbi.h>
  20. #include "i2c-designware-core.h"
  21. #define SEMAPHORE_TIMEOUT 100
  22. #define PUNIT_SEMAPHORE 0x7
  23. #define PUNIT_SEMAPHORE_BIT BIT(0)
  24. #define PUNIT_SEMAPHORE_ACQUIRE BIT(1)
  25. static unsigned long acquired;
  26. static int get_sem(struct device *dev, u32 *sem)
  27. {
  28. u32 data;
  29. int ret;
  30. ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, PUNIT_SEMAPHORE, &data);
  31. if (ret) {
  32. dev_err(dev, "iosf failed to read punit semaphore\n");
  33. return ret;
  34. }
  35. *sem = data & PUNIT_SEMAPHORE_BIT;
  36. return 0;
  37. }
  38. static void reset_semaphore(struct device *dev)
  39. {
  40. u32 data;
  41. if (iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, PUNIT_SEMAPHORE, &data)) {
  42. dev_err(dev, "iosf failed to reset punit semaphore during read\n");
  43. return;
  44. }
  45. data &= ~PUNIT_SEMAPHORE_BIT;
  46. if (iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, PUNIT_SEMAPHORE, data))
  47. dev_err(dev, "iosf failed to reset punit semaphore during write\n");
  48. }
  49. static int baytrail_i2c_acquire(struct dw_i2c_dev *dev)
  50. {
  51. u32 sem = PUNIT_SEMAPHORE_ACQUIRE;
  52. int ret;
  53. unsigned long start, end;
  54. might_sleep();
  55. if (!dev || !dev->dev)
  56. return -ENODEV;
  57. if (!dev->release_lock)
  58. return 0;
  59. /* host driver writes to side band semaphore register */
  60. ret = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, PUNIT_SEMAPHORE, sem);
  61. if (ret) {
  62. dev_err(dev->dev, "iosf punit semaphore request failed\n");
  63. return ret;
  64. }
  65. /* host driver waits for bit 0 to be set in semaphore register */
  66. start = jiffies;
  67. end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT);
  68. do {
  69. ret = get_sem(dev->dev, &sem);
  70. if (!ret && sem) {
  71. acquired = jiffies;
  72. dev_dbg(dev->dev, "punit semaphore acquired after %ums\n",
  73. jiffies_to_msecs(jiffies - start));
  74. return 0;
  75. }
  76. usleep_range(1000, 2000);
  77. } while (time_before(jiffies, end));
  78. dev_err(dev->dev, "punit semaphore timed out, resetting\n");
  79. reset_semaphore(dev->dev);
  80. ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, PUNIT_SEMAPHORE, &sem);
  81. if (ret)
  82. dev_err(dev->dev, "iosf failed to read punit semaphore\n");
  83. else
  84. dev_err(dev->dev, "PUNIT SEM: %d\n", sem);
  85. WARN_ON(1);
  86. return -ETIMEDOUT;
  87. }
  88. static void baytrail_i2c_release(struct dw_i2c_dev *dev)
  89. {
  90. if (!dev || !dev->dev)
  91. return;
  92. if (!dev->acquire_lock)
  93. return;
  94. reset_semaphore(dev->dev);
  95. dev_dbg(dev->dev, "punit semaphore held for %ums\n",
  96. jiffies_to_msecs(jiffies - acquired));
  97. }
  98. int i2c_dw_eval_lock_support(struct dw_i2c_dev *dev)
  99. {
  100. acpi_status status;
  101. unsigned long long shared_host = 0;
  102. acpi_handle handle;
  103. if (!dev || !dev->dev)
  104. return 0;
  105. handle = ACPI_HANDLE(dev->dev);
  106. if (!handle)
  107. return 0;
  108. status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host);
  109. if (ACPI_FAILURE(status))
  110. return 0;
  111. if (shared_host) {
  112. dev_info(dev->dev, "I2C bus managed by PUNIT\n");
  113. dev->acquire_lock = baytrail_i2c_acquire;
  114. dev->release_lock = baytrail_i2c_release;
  115. dev->pm_runtime_disabled = true;
  116. }
  117. if (!iosf_mbi_available())
  118. return -EPROBE_DEFER;
  119. return 0;
  120. }