ufshcd-pci.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #ifdef CONFIG_PM
  39. /**
  40. * ufshcd_pci_suspend - suspend power management function
  41. * @pdev: pointer to PCI device handle
  42. * @state: power state
  43. *
  44. * Returns -ENOSYS
  45. */
  46. static int ufshcd_pci_suspend(struct device *dev)
  47. {
  48. /*
  49. * TODO:
  50. * 1. Call ufshcd_suspend
  51. * 2. Do bus specific power management
  52. */
  53. return -ENOSYS;
  54. }
  55. /**
  56. * ufshcd_pci_resume - resume power management function
  57. * @pdev: pointer to PCI device handle
  58. *
  59. * Returns -ENOSYS
  60. */
  61. static int ufshcd_pci_resume(struct device *dev)
  62. {
  63. /*
  64. * TODO:
  65. * 1. Call ufshcd_resume.
  66. * 2. Do bus specific wake up
  67. */
  68. return -ENOSYS;
  69. }
  70. #else
  71. #define ufshcd_pci_suspend NULL
  72. #define ufshcd_pci_resume NULL
  73. #endif /* CONFIG_PM */
  74. #ifdef CONFIG_PM_RUNTIME
  75. static int ufshcd_pci_runtime_suspend(struct device *dev)
  76. {
  77. struct ufs_hba *hba = dev_get_drvdata(dev);
  78. if (!hba)
  79. return 0;
  80. return ufshcd_runtime_suspend(hba);
  81. }
  82. static int ufshcd_pci_runtime_resume(struct device *dev)
  83. {
  84. struct ufs_hba *hba = dev_get_drvdata(dev);
  85. if (!hba)
  86. return 0;
  87. return ufshcd_runtime_resume(hba);
  88. }
  89. static int ufshcd_pci_runtime_idle(struct device *dev)
  90. {
  91. struct ufs_hba *hba = dev_get_drvdata(dev);
  92. if (!hba)
  93. return 0;
  94. return ufshcd_runtime_idle(hba);
  95. }
  96. #else /* !CONFIG_PM_RUNTIME */
  97. #define ufshcd_pci_runtime_suspend NULL
  98. #define ufshcd_pci_runtime_resume NULL
  99. #define ufshcd_pci_runtime_idle NULL
  100. #endif /* CONFIG_PM_RUNTIME */
  101. /**
  102. * ufshcd_pci_shutdown - main function to put the controller in reset state
  103. * @pdev: pointer to PCI device handle
  104. */
  105. static void ufshcd_pci_shutdown(struct pci_dev *pdev)
  106. {
  107. ufshcd_hba_stop((struct ufs_hba *)pci_get_drvdata(pdev));
  108. }
  109. /**
  110. * ufshcd_pci_remove - de-allocate PCI/SCSI host and host memory space
  111. * data structure memory
  112. * @pdev - pointer to PCI handle
  113. */
  114. static void ufshcd_pci_remove(struct pci_dev *pdev)
  115. {
  116. struct ufs_hba *hba = pci_get_drvdata(pdev);
  117. pm_runtime_forbid(&pdev->dev);
  118. pm_runtime_get_noresume(&pdev->dev);
  119. ufshcd_remove(hba);
  120. }
  121. /**
  122. * ufshcd_set_dma_mask - Set dma mask based on the controller
  123. * addressing capability
  124. * @pdev: PCI device structure
  125. *
  126. * Returns 0 for success, non-zero for failure
  127. */
  128. static int ufshcd_set_dma_mask(struct pci_dev *pdev)
  129. {
  130. int err;
  131. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))
  132. && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
  133. return 0;
  134. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  135. if (!err)
  136. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  137. return err;
  138. }
  139. /**
  140. * ufshcd_pci_probe - probe routine of the driver
  141. * @pdev: pointer to PCI device handle
  142. * @id: PCI device id
  143. *
  144. * Returns 0 on success, non-zero value on failure
  145. */
  146. static int
  147. ufshcd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  148. {
  149. struct ufs_hba *hba;
  150. void __iomem *mmio_base;
  151. int err;
  152. err = pcim_enable_device(pdev);
  153. if (err) {
  154. dev_err(&pdev->dev, "pcim_enable_device failed\n");
  155. return err;
  156. }
  157. pci_set_master(pdev);
  158. err = pcim_iomap_regions(pdev, 1 << 0, UFSHCD);
  159. if (err < 0) {
  160. dev_err(&pdev->dev, "request and iomap failed\n");
  161. return err;
  162. }
  163. mmio_base = pcim_iomap_table(pdev)[0];
  164. err = ufshcd_set_dma_mask(pdev);
  165. if (err) {
  166. dev_err(&pdev->dev, "set dma mask failed\n");
  167. return err;
  168. }
  169. err = ufshcd_init(&pdev->dev, &hba, mmio_base, pdev->irq);
  170. if (err) {
  171. dev_err(&pdev->dev, "Initialization failed\n");
  172. return err;
  173. }
  174. pci_set_drvdata(pdev, hba);
  175. pm_runtime_put_noidle(&pdev->dev);
  176. pm_runtime_allow(&pdev->dev);
  177. return 0;
  178. }
  179. static const struct dev_pm_ops ufshcd_pci_pm_ops = {
  180. .suspend = ufshcd_pci_suspend,
  181. .resume = ufshcd_pci_resume,
  182. .runtime_suspend = ufshcd_pci_runtime_suspend,
  183. .runtime_resume = ufshcd_pci_runtime_resume,
  184. .runtime_idle = ufshcd_pci_runtime_idle,
  185. };
  186. static DEFINE_PCI_DEVICE_TABLE(ufshcd_pci_tbl) = {
  187. { PCI_VENDOR_ID_SAMSUNG, 0xC00C, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  188. { } /* terminate list */
  189. };
  190. MODULE_DEVICE_TABLE(pci, ufshcd_pci_tbl);
  191. static struct pci_driver ufshcd_pci_driver = {
  192. .name = UFSHCD,
  193. .id_table = ufshcd_pci_tbl,
  194. .probe = ufshcd_pci_probe,
  195. .remove = ufshcd_pci_remove,
  196. .shutdown = ufshcd_pci_shutdown,
  197. .driver = {
  198. .pm = &ufshcd_pci_pm_ops
  199. },
  200. };
  201. module_pci_driver(ufshcd_pci_driver);
  202. MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
  203. MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
  204. MODULE_DESCRIPTION("UFS host controller PCI glue driver");
  205. MODULE_LICENSE("GPL");
  206. MODULE_VERSION(UFSHCD_DRIVER_VERSION);