ufshcd-pci.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Universal Flash Storage Host controller PCI glue driver
  3. *
  4. * This code is based on drivers/scsi/ufs/ufshcd-pci.c
  5. * Copyright (C) 2011-2013 Samsung India Software Operations
  6. *
  7. * Authors:
  8. * Santosh Yaraganavi <santosh.sy@samsung.com>
  9. * Vinayak Holikatti <h.vinayak@samsung.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. * See the COPYING file in the top-level directory or visit
  16. * <http://www.gnu.org/licenses/gpl-2.0.html>
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * This program is provided "AS IS" and "WITH ALL FAULTS" and
  24. * without warranty of any kind. You are solely responsible for
  25. * determining the appropriateness of using and distributing
  26. * the program and assume all risks associated with your exercise
  27. * of rights with respect to the program, including but not limited
  28. * to infringement of third party rights, the risks and costs of
  29. * program errors, damage to or loss of data, programs or equipment,
  30. * and unavailability or interruption of operations. Under no
  31. * circumstances will the contributor of this Program be liable for
  32. * any damages of any kind arising from your use or distribution of
  33. * this program.
  34. */
  35. #include "ufshcd.h"
  36. #include <linux/pci.h>
  37. #include <linux/pm_runtime.h>
  38. static int ufs_intel_disable_lcc(struct ufs_hba *hba)
  39. {
  40. u32 attr = UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE);
  41. u32 lcc_enable = 0;
  42. ufshcd_dme_get(hba, attr, &lcc_enable);
  43. if (lcc_enable)
  44. ufshcd_dme_set(hba, attr, 0);
  45. return 0;
  46. }
  47. static int ufs_intel_link_startup_notify(struct ufs_hba *hba,
  48. enum ufs_notify_change_status status)
  49. {
  50. int err = 0;
  51. switch (status) {
  52. case PRE_CHANGE:
  53. err = ufs_intel_disable_lcc(hba);
  54. break;
  55. case POST_CHANGE:
  56. break;
  57. default:
  58. break;
  59. }
  60. return err;
  61. }
  62. static struct ufs_hba_variant_ops ufs_intel_cnl_hba_vops = {
  63. .name = "intel-pci",
  64. .link_startup_notify = ufs_intel_link_startup_notify,
  65. };
  66. #ifdef CONFIG_PM_SLEEP
  67. /**
  68. * ufshcd_pci_suspend - suspend power management function
  69. * @dev: pointer to PCI device handle
  70. *
  71. * Returns 0 if successful
  72. * Returns non-zero otherwise
  73. */
  74. static int ufshcd_pci_suspend(struct device *dev)
  75. {
  76. return ufshcd_system_suspend(dev_get_drvdata(dev));
  77. }
  78. /**
  79. * ufshcd_pci_resume - resume power management function
  80. * @dev: pointer to PCI device handle
  81. *
  82. * Returns 0 if successful
  83. * Returns non-zero otherwise
  84. */
  85. static int ufshcd_pci_resume(struct device *dev)
  86. {
  87. return ufshcd_system_resume(dev_get_drvdata(dev));
  88. }
  89. /**
  90. * ufshcd_pci_poweroff - suspend-to-disk poweroff function
  91. * @dev: pointer to PCI device handle
  92. *
  93. * Returns 0 if successful
  94. * Returns non-zero otherwise
  95. */
  96. static int ufshcd_pci_poweroff(struct device *dev)
  97. {
  98. struct ufs_hba *hba = dev_get_drvdata(dev);
  99. int spm_lvl = hba->spm_lvl;
  100. int ret;
  101. /*
  102. * For poweroff we need to set the UFS device to PowerDown mode.
  103. * Force spm_lvl to ensure that.
  104. */
  105. hba->spm_lvl = 5;
  106. ret = ufshcd_system_suspend(hba);
  107. hba->spm_lvl = spm_lvl;
  108. return ret;
  109. }
  110. #endif /* !CONFIG_PM_SLEEP */
  111. #ifdef CONFIG_PM
  112. static int ufshcd_pci_runtime_suspend(struct device *dev)
  113. {
  114. return ufshcd_runtime_suspend(dev_get_drvdata(dev));
  115. }
  116. static int ufshcd_pci_runtime_resume(struct device *dev)
  117. {
  118. return ufshcd_runtime_resume(dev_get_drvdata(dev));
  119. }
  120. static int ufshcd_pci_runtime_idle(struct device *dev)
  121. {
  122. return ufshcd_runtime_idle(dev_get_drvdata(dev));
  123. }
  124. #endif /* !CONFIG_PM */
  125. /**
  126. * ufshcd_pci_shutdown - main function to put the controller in reset state
  127. * @pdev: pointer to PCI device handle
  128. */
  129. static void ufshcd_pci_shutdown(struct pci_dev *pdev)
  130. {
  131. ufshcd_shutdown((struct ufs_hba *)pci_get_drvdata(pdev));
  132. }
  133. /**
  134. * ufshcd_pci_remove - de-allocate PCI/SCSI host and host memory space
  135. * data structure memory
  136. * @pdev: pointer to PCI handle
  137. */
  138. static void ufshcd_pci_remove(struct pci_dev *pdev)
  139. {
  140. struct ufs_hba *hba = pci_get_drvdata(pdev);
  141. pm_runtime_forbid(&pdev->dev);
  142. pm_runtime_get_noresume(&pdev->dev);
  143. ufshcd_remove(hba);
  144. ufshcd_dealloc_host(hba);
  145. }
  146. /**
  147. * ufshcd_pci_probe - probe routine of the driver
  148. * @pdev: pointer to PCI device handle
  149. * @id: PCI device id
  150. *
  151. * Returns 0 on success, non-zero value on failure
  152. */
  153. static int
  154. ufshcd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  155. {
  156. struct ufs_hba *hba;
  157. void __iomem *mmio_base;
  158. int err;
  159. err = pcim_enable_device(pdev);
  160. if (err) {
  161. dev_err(&pdev->dev, "pcim_enable_device failed\n");
  162. return err;
  163. }
  164. pci_set_master(pdev);
  165. err = pcim_iomap_regions(pdev, 1 << 0, UFSHCD);
  166. if (err < 0) {
  167. dev_err(&pdev->dev, "request and iomap failed\n");
  168. return err;
  169. }
  170. mmio_base = pcim_iomap_table(pdev)[0];
  171. err = ufshcd_alloc_host(&pdev->dev, &hba);
  172. if (err) {
  173. dev_err(&pdev->dev, "Allocation failed\n");
  174. return err;
  175. }
  176. hba->vops = (struct ufs_hba_variant_ops *)id->driver_data;
  177. err = ufshcd_init(hba, mmio_base, pdev->irq);
  178. if (err) {
  179. dev_err(&pdev->dev, "Initialization failed\n");
  180. ufshcd_dealloc_host(hba);
  181. return err;
  182. }
  183. pci_set_drvdata(pdev, hba);
  184. pm_runtime_put_noidle(&pdev->dev);
  185. pm_runtime_allow(&pdev->dev);
  186. return 0;
  187. }
  188. static const struct dev_pm_ops ufshcd_pci_pm_ops = {
  189. #ifdef CONFIG_PM_SLEEP
  190. .suspend = ufshcd_pci_suspend,
  191. .resume = ufshcd_pci_resume,
  192. .freeze = ufshcd_pci_suspend,
  193. .thaw = ufshcd_pci_resume,
  194. .poweroff = ufshcd_pci_poweroff,
  195. .restore = ufshcd_pci_resume,
  196. #endif
  197. SET_RUNTIME_PM_OPS(ufshcd_pci_runtime_suspend,
  198. ufshcd_pci_runtime_resume,
  199. ufshcd_pci_runtime_idle)
  200. };
  201. static const struct pci_device_id ufshcd_pci_tbl[] = {
  202. { PCI_VENDOR_ID_SAMSUNG, 0xC00C, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  203. { PCI_VDEVICE(INTEL, 0x9DFA), (kernel_ulong_t)&ufs_intel_cnl_hba_vops },
  204. { } /* terminate list */
  205. };
  206. MODULE_DEVICE_TABLE(pci, ufshcd_pci_tbl);
  207. static struct pci_driver ufshcd_pci_driver = {
  208. .name = UFSHCD,
  209. .id_table = ufshcd_pci_tbl,
  210. .probe = ufshcd_pci_probe,
  211. .remove = ufshcd_pci_remove,
  212. .shutdown = ufshcd_pci_shutdown,
  213. .driver = {
  214. .pm = &ufshcd_pci_pm_ops
  215. },
  216. };
  217. module_pci_driver(ufshcd_pci_driver);
  218. MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
  219. MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
  220. MODULE_DESCRIPTION("UFS host controller PCI glue driver");
  221. MODULE_LICENSE("GPL");
  222. MODULE_VERSION(UFSHCD_DRIVER_VERSION);