dw_spi_pci.c 4.0 KB

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