i2c-designware-baytrail.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <linux/pm_qos.h>
  20. #include <asm/iosf_mbi.h>
  21. #include "i2c-designware-core.h"
  22. #define SEMAPHORE_TIMEOUT 500
  23. #define PUNIT_SEMAPHORE 0x7
  24. #define PUNIT_SEMAPHORE_CHT 0x10e
  25. #define PUNIT_SEMAPHORE_BIT BIT(0)
  26. #define PUNIT_SEMAPHORE_ACQUIRE BIT(1)
  27. static unsigned long acquired;
  28. static u32 get_sem_addr(struct dw_i2c_dev *dev)
  29. {
  30. if (dev->flags & MODEL_CHERRYTRAIL)
  31. return PUNIT_SEMAPHORE_CHT;
  32. else
  33. return PUNIT_SEMAPHORE;
  34. }
  35. static int get_sem(struct dw_i2c_dev *dev, u32 *sem)
  36. {
  37. u32 addr = get_sem_addr(dev);
  38. u32 data;
  39. int ret;
  40. ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, addr, &data);
  41. if (ret) {
  42. dev_err(dev->dev, "iosf failed to read punit semaphore\n");
  43. return ret;
  44. }
  45. *sem = data & PUNIT_SEMAPHORE_BIT;
  46. return 0;
  47. }
  48. static void reset_semaphore(struct dw_i2c_dev *dev)
  49. {
  50. if (iosf_mbi_modify(BT_MBI_UNIT_PMC, MBI_REG_READ, get_sem_addr(dev),
  51. 0, PUNIT_SEMAPHORE_BIT))
  52. dev_err(dev->dev, "iosf failed to reset punit semaphore during write\n");
  53. pm_qos_update_request(&dev->pm_qos, PM_QOS_DEFAULT_VALUE);
  54. iosf_mbi_call_pmic_bus_access_notifier_chain(MBI_PMIC_BUS_ACCESS_END,
  55. NULL);
  56. iosf_mbi_punit_release();
  57. }
  58. static int baytrail_i2c_acquire(struct dw_i2c_dev *dev)
  59. {
  60. u32 addr;
  61. u32 sem = PUNIT_SEMAPHORE_ACQUIRE;
  62. int ret;
  63. unsigned long start, end;
  64. might_sleep();
  65. if (!dev || !dev->dev)
  66. return -ENODEV;
  67. if (!dev->release_lock)
  68. return 0;
  69. iosf_mbi_punit_acquire();
  70. iosf_mbi_call_pmic_bus_access_notifier_chain(MBI_PMIC_BUS_ACCESS_BEGIN,
  71. NULL);
  72. /*
  73. * Disallow the CPU to enter C6 or C7 state, entering these states
  74. * requires the punit to talk to the pmic and if this happens while
  75. * we're holding the semaphore, the SoC hangs.
  76. */
  77. pm_qos_update_request(&dev->pm_qos, 0);
  78. addr = get_sem_addr(dev);
  79. /* host driver writes to side band semaphore register */
  80. ret = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE, addr, sem);
  81. if (ret) {
  82. dev_err(dev->dev, "iosf punit semaphore request failed\n");
  83. goto out;
  84. }
  85. /* host driver waits for bit 0 to be set in semaphore register */
  86. start = jiffies;
  87. end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT);
  88. do {
  89. ret = get_sem(dev, &sem);
  90. if (!ret && sem) {
  91. acquired = jiffies;
  92. dev_dbg(dev->dev, "punit semaphore acquired after %ums\n",
  93. jiffies_to_msecs(jiffies - start));
  94. return 0;
  95. }
  96. usleep_range(1000, 2000);
  97. } while (time_before(jiffies, end));
  98. dev_err(dev->dev, "punit semaphore timed out, resetting\n");
  99. out:
  100. reset_semaphore(dev);
  101. ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, addr, &sem);
  102. if (ret)
  103. dev_err(dev->dev, "iosf failed to read punit semaphore\n");
  104. else
  105. dev_err(dev->dev, "PUNIT SEM: %d\n", sem);
  106. WARN_ON(1);
  107. return -ETIMEDOUT;
  108. }
  109. static void baytrail_i2c_release(struct dw_i2c_dev *dev)
  110. {
  111. if (!dev || !dev->dev)
  112. return;
  113. if (!dev->acquire_lock)
  114. return;
  115. reset_semaphore(dev);
  116. dev_dbg(dev->dev, "punit semaphore held for %ums\n",
  117. jiffies_to_msecs(jiffies - acquired));
  118. }
  119. int i2c_dw_probe_lock_support(struct dw_i2c_dev *dev)
  120. {
  121. acpi_status status;
  122. unsigned long long shared_host = 0;
  123. acpi_handle handle;
  124. if (!dev || !dev->dev)
  125. return 0;
  126. handle = ACPI_HANDLE(dev->dev);
  127. if (!handle)
  128. return 0;
  129. status = acpi_evaluate_integer(handle, "_SEM", NULL, &shared_host);
  130. if (ACPI_FAILURE(status))
  131. return 0;
  132. if (!shared_host)
  133. return 0;
  134. if (!iosf_mbi_available())
  135. return -EPROBE_DEFER;
  136. dev_info(dev->dev, "I2C bus managed by PUNIT\n");
  137. dev->acquire_lock = baytrail_i2c_acquire;
  138. dev->release_lock = baytrail_i2c_release;
  139. dev->pm_disabled = true;
  140. pm_qos_add_request(&dev->pm_qos, PM_QOS_CPU_DMA_LATENCY,
  141. PM_QOS_DEFAULT_VALUE);
  142. return 0;
  143. }
  144. void i2c_dw_remove_lock_support(struct dw_i2c_dev *dev)
  145. {
  146. if (dev->acquire_lock)
  147. pm_qos_remove_request(&dev->pm_qos);
  148. }