dw_spi_pci.c 4.1 KB

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