dw_spi_pci.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. dws->fifo_len = 40; /* FIFO has 40 words buffer */
  65. ret = dw_spi_add_host(dws);
  66. if (ret)
  67. goto err_unmap;
  68. /* PCI hook and SPI hook use the same drv data */
  69. pci_set_drvdata(pdev, dwpci);
  70. return 0;
  71. err_unmap:
  72. iounmap(dws->regs);
  73. err_release_reg:
  74. pci_release_region(pdev, pci_bar);
  75. err_kfree:
  76. kfree(dwpci);
  77. err_disable:
  78. pci_disable_device(pdev);
  79. return ret;
  80. }
  81. static void __devexit spi_pci_remove(struct pci_dev *pdev)
  82. {
  83. struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
  84. pci_set_drvdata(pdev, NULL);
  85. dw_spi_remove_host(&dwpci->dws);
  86. iounmap(dwpci->dws.regs);
  87. pci_release_region(pdev, 0);
  88. kfree(dwpci);
  89. pci_disable_device(pdev);
  90. }
  91. #ifdef CONFIG_PM
  92. static int spi_suspend(struct pci_dev *pdev, pm_message_t state)
  93. {
  94. struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
  95. int ret;
  96. ret = dw_spi_suspend_host(&dwpci->dws);
  97. if (ret)
  98. return ret;
  99. pci_save_state(pdev);
  100. pci_disable_device(pdev);
  101. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  102. return ret;
  103. }
  104. static int spi_resume(struct pci_dev *pdev)
  105. {
  106. struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
  107. int ret;
  108. pci_set_power_state(pdev, PCI_D0);
  109. pci_restore_state(pdev);
  110. ret = pci_enable_device(pdev);
  111. if (ret)
  112. return ret;
  113. return dw_spi_resume_host(&dwpci->dws);
  114. }
  115. #else
  116. #define spi_suspend NULL
  117. #define spi_resume NULL
  118. #endif
  119. static const struct pci_device_id pci_ids[] __devinitdata = {
  120. /* Intel Moorestown platform SPI controller 0 */
  121. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x0800) },
  122. {},
  123. };
  124. static struct pci_driver dw_spi_driver = {
  125. .name = DRIVER_NAME,
  126. .id_table = pci_ids,
  127. .probe = spi_pci_probe,
  128. .remove = __devexit_p(spi_pci_remove),
  129. .suspend = spi_suspend,
  130. .resume = spi_resume,
  131. };
  132. static int __init mrst_spi_init(void)
  133. {
  134. return pci_register_driver(&dw_spi_driver);
  135. }
  136. static void __exit mrst_spi_exit(void)
  137. {
  138. pci_unregister_driver(&dw_spi_driver);
  139. }
  140. module_init(mrst_spi_init);
  141. module_exit(mrst_spi_exit);
  142. MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
  143. MODULE_DESCRIPTION("PCI interface driver for DW SPI Core");
  144. MODULE_LICENSE("GPL v2");