dw_spi_pci.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * mrst_spi_pci.c - 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/spi/dw_spi.h>
  22. #include <linux/spi/spi.h>
  23. #define DRIVER_NAME "dw_spi_pci"
  24. struct dw_spi_pci {
  25. struct pci_dev *pdev;
  26. struct dw_spi dws;
  27. };
  28. static int __devinit spi_pci_probe(struct pci_dev *pdev,
  29. const struct pci_device_id *ent)
  30. {
  31. struct dw_spi_pci *dwpci;
  32. struct dw_spi *dws;
  33. int pci_bar = 0;
  34. int ret;
  35. printk(KERN_INFO "DW: found PCI SPI controller(ID: %04x:%04x)\n",
  36. pdev->vendor, pdev->device);
  37. ret = pci_enable_device(pdev);
  38. if (ret)
  39. return ret;
  40. dwpci = kzalloc(sizeof(struct dw_spi_pci), GFP_KERNEL);
  41. if (!dwpci) {
  42. ret = -ENOMEM;
  43. goto err_disable;
  44. }
  45. dwpci->pdev = pdev;
  46. dws = &dwpci->dws;
  47. /* Get basic io resource and map it */
  48. dws->paddr = pci_resource_start(pdev, pci_bar);
  49. dws->iolen = pci_resource_len(pdev, pci_bar);
  50. ret = pci_request_region(pdev, pci_bar, dev_name(&pdev->dev));
  51. if (ret)
  52. goto err_kfree;
  53. dws->regs = ioremap_nocache((unsigned long)dws->paddr,
  54. pci_resource_len(pdev, pci_bar));
  55. if (!dws->regs) {
  56. ret = -ENOMEM;
  57. goto err_release_reg;
  58. }
  59. dws->parent_dev = &pdev->dev;
  60. dws->bus_num = 0;
  61. dws->num_cs = 4;
  62. dws->max_freq = 25000000; /* for Moorestwon */
  63. dws->irq = pdev->irq;
  64. ret = dw_spi_add_host(dws);
  65. if (ret)
  66. goto err_unmap;
  67. /* PCI hook and SPI hook use the same drv data */
  68. pci_set_drvdata(pdev, dwpci);
  69. return 0;
  70. err_unmap:
  71. iounmap(dws->regs);
  72. err_release_reg:
  73. pci_release_region(pdev, pci_bar);
  74. err_kfree:
  75. kfree(dwpci);
  76. err_disable:
  77. pci_disable_device(pdev);
  78. return ret;
  79. }
  80. static void __devexit spi_pci_remove(struct pci_dev *pdev)
  81. {
  82. struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
  83. pci_set_drvdata(pdev, NULL);
  84. iounmap(dwpci->dws.regs);
  85. pci_release_region(pdev, 0);
  86. kfree(dwpci);
  87. pci_disable_device(pdev);
  88. }
  89. #ifdef CONFIG_PM
  90. static int spi_suspend(struct pci_dev *pdev, pm_message_t state)
  91. {
  92. struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
  93. int ret;
  94. ret = dw_spi_suspend_host(&dwpci->dws);
  95. if (ret)
  96. return ret;
  97. pci_save_state(pdev);
  98. pci_disable_device(pdev);
  99. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  100. return ret;
  101. }
  102. static int spi_resume(struct pci_dev *pdev)
  103. {
  104. struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
  105. int ret;
  106. pci_set_power_state(pdev, PCI_D0);
  107. pci_restore_state(pdev);
  108. ret = pci_enable_device(pdev);
  109. if (ret)
  110. return ret;
  111. return dw_spi_resume_host(&dwpci->dws);
  112. }
  113. #else
  114. #define spi_suspend NULL
  115. #define spi_resume NULL
  116. #endif
  117. static const struct pci_device_id pci_ids[] __devinitdata = {
  118. /* Intel Moorestown platform SPI controller 0 */
  119. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x0800) },
  120. {},
  121. };
  122. static struct pci_driver dw_spi_driver = {
  123. .name = DRIVER_NAME,
  124. .id_table = pci_ids,
  125. .probe = spi_pci_probe,
  126. .remove = __devexit_p(spi_pci_remove),
  127. .suspend = spi_suspend,
  128. .resume = spi_resume,
  129. };
  130. static int __init mrst_spi_init(void)
  131. {
  132. return pci_register_driver(&dw_spi_driver);
  133. }
  134. static void __exit mrst_spi_exit(void)
  135. {
  136. pci_unregister_driver(&dw_spi_driver);
  137. }
  138. module_init(mrst_spi_init);
  139. module_exit(mrst_spi_exit);
  140. MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
  141. MODULE_DESCRIPTION("PCI interface driver for DW SPI Core");
  142. MODULE_LICENSE("GPL v2");