spi-dw-pci.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * PCI interface driver for DW SPI Core
  3. *
  4. * Copyright (c) 2009, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include <linux/interrupt.h>
  20. #include <linux/pci.h>
  21. #include <linux/slab.h>
  22. #include <linux/spi/spi.h>
  23. #include <linux/module.h>
  24. #include "spi-dw.h"
  25. #define DRIVER_NAME "dw_spi_pci"
  26. struct dw_spi_pci {
  27. struct pci_dev *pdev;
  28. struct dw_spi dws;
  29. };
  30. static int __devinit spi_pci_probe(struct pci_dev *pdev,
  31. const struct pci_device_id *ent)
  32. {
  33. struct dw_spi_pci *dwpci;
  34. struct dw_spi *dws;
  35. int pci_bar = 0;
  36. int ret;
  37. printk(KERN_INFO "DW: found PCI SPI controller(ID: %04x:%04x)\n",
  38. pdev->vendor, pdev->device);
  39. ret = pci_enable_device(pdev);
  40. if (ret)
  41. return ret;
  42. dwpci = kzalloc(sizeof(struct dw_spi_pci), GFP_KERNEL);
  43. if (!dwpci) {
  44. ret = -ENOMEM;
  45. goto err_disable;
  46. }
  47. dwpci->pdev = pdev;
  48. dws = &dwpci->dws;
  49. /* Get basic io resource and map it */
  50. dws->paddr = pci_resource_start(pdev, pci_bar);
  51. dws->iolen = pci_resource_len(pdev, pci_bar);
  52. ret = pci_request_region(pdev, pci_bar, dev_name(&pdev->dev));
  53. if (ret)
  54. goto err_kfree;
  55. dws->regs = ioremap_nocache((unsigned long)dws->paddr,
  56. pci_resource_len(pdev, pci_bar));
  57. if (!dws->regs) {
  58. ret = -ENOMEM;
  59. goto err_release_reg;
  60. }
  61. dws->parent_dev = &pdev->dev;
  62. dws->bus_num = 0;
  63. dws->num_cs = 4;
  64. dws->irq = pdev->irq;
  65. /*
  66. * Specific handling for Intel MID paltforms, like dma setup,
  67. * clock rate, FIFO depth.
  68. */
  69. if (pdev->device == 0x0800) {
  70. ret = dw_spi_mid_init(dws);
  71. if (ret)
  72. goto err_unmap;
  73. }
  74. ret = dw_spi_add_host(dws);
  75. if (ret)
  76. goto err_unmap;
  77. /* PCI hook and SPI hook use the same drv data */
  78. pci_set_drvdata(pdev, dwpci);
  79. return 0;
  80. err_unmap:
  81. iounmap(dws->regs);
  82. err_release_reg:
  83. pci_release_region(pdev, pci_bar);
  84. err_kfree:
  85. kfree(dwpci);
  86. err_disable:
  87. pci_disable_device(pdev);
  88. return ret;
  89. }
  90. static void __devexit spi_pci_remove(struct pci_dev *pdev)
  91. {
  92. struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
  93. pci_set_drvdata(pdev, NULL);
  94. dw_spi_remove_host(&dwpci->dws);
  95. iounmap(dwpci->dws.regs);
  96. pci_release_region(pdev, 0);
  97. kfree(dwpci);
  98. pci_disable_device(pdev);
  99. }
  100. #ifdef CONFIG_PM
  101. static int spi_suspend(struct pci_dev *pdev, pm_message_t state)
  102. {
  103. struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
  104. int ret;
  105. ret = dw_spi_suspend_host(&dwpci->dws);
  106. if (ret)
  107. return ret;
  108. pci_save_state(pdev);
  109. pci_disable_device(pdev);
  110. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  111. return ret;
  112. }
  113. static int spi_resume(struct pci_dev *pdev)
  114. {
  115. struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
  116. int ret;
  117. pci_set_power_state(pdev, PCI_D0);
  118. pci_restore_state(pdev);
  119. ret = pci_enable_device(pdev);
  120. if (ret)
  121. return ret;
  122. return dw_spi_resume_host(&dwpci->dws);
  123. }
  124. #else
  125. #define spi_suspend NULL
  126. #define spi_resume NULL
  127. #endif
  128. static DEFINE_PCI_DEVICE_TABLE(pci_ids) = {
  129. /* Intel MID platform SPI controller 0 */
  130. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x0800) },
  131. {},
  132. };
  133. static struct pci_driver dw_spi_driver = {
  134. .name = DRIVER_NAME,
  135. .id_table = pci_ids,
  136. .probe = spi_pci_probe,
  137. .remove = __devexit_p(spi_pci_remove),
  138. .suspend = spi_suspend,
  139. .resume = spi_resume,
  140. };
  141. static int __init mrst_spi_init(void)
  142. {
  143. return pci_register_driver(&dw_spi_driver);
  144. }
  145. static void __exit mrst_spi_exit(void)
  146. {
  147. pci_unregister_driver(&dw_spi_driver);
  148. }
  149. module_init(mrst_spi_init);
  150. module_exit(mrst_spi_exit);
  151. MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
  152. MODULE_DESCRIPTION("PCI interface driver for DW SPI Core");
  153. MODULE_LICENSE("GPL v2");