spi-dw-pci.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * PCI interface driver for DW SPI Core
  3. *
  4. * Copyright (c) 2009, 2014 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. #include <linux/interrupt.h>
  16. #include <linux/pci.h>
  17. #include <linux/slab.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/module.h>
  20. #include "spi-dw.h"
  21. #define DRIVER_NAME "dw_spi_pci"
  22. struct dw_spi_pci {
  23. struct pci_dev *pdev;
  24. struct dw_spi dws;
  25. };
  26. struct spi_pci_desc {
  27. int (*setup)(struct dw_spi *);
  28. u16 num_cs;
  29. u16 bus_num;
  30. };
  31. static struct spi_pci_desc spi_pci_mid_desc_1 = {
  32. .setup = dw_spi_mid_init,
  33. .num_cs = 5,
  34. .bus_num = 0,
  35. };
  36. static struct spi_pci_desc spi_pci_mid_desc_2 = {
  37. .setup = dw_spi_mid_init,
  38. .num_cs = 2,
  39. .bus_num = 1,
  40. };
  41. static int spi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  42. {
  43. struct dw_spi_pci *dwpci;
  44. struct dw_spi *dws;
  45. struct spi_pci_desc *desc = (struct spi_pci_desc *)ent->driver_data;
  46. int pci_bar = 0;
  47. int ret;
  48. ret = pcim_enable_device(pdev);
  49. if (ret)
  50. return ret;
  51. dwpci = devm_kzalloc(&pdev->dev, sizeof(struct dw_spi_pci),
  52. GFP_KERNEL);
  53. if (!dwpci)
  54. return -ENOMEM;
  55. dwpci->pdev = pdev;
  56. dws = &dwpci->dws;
  57. /* Get basic io resource and map it */
  58. dws->paddr = pci_resource_start(pdev, pci_bar);
  59. ret = pcim_iomap_regions(pdev, 1 << pci_bar, pci_name(pdev));
  60. if (ret)
  61. return ret;
  62. dws->regs = pcim_iomap_table(pdev)[pci_bar];
  63. dws->irq = pdev->irq;
  64. /*
  65. * Specific handling for paltforms, like dma setup,
  66. * clock rate, FIFO depth.
  67. */
  68. if (desc) {
  69. dws->num_cs = desc->num_cs;
  70. dws->bus_num = desc->bus_num;
  71. if (desc->setup) {
  72. ret = desc->setup(dws);
  73. if (ret)
  74. return ret;
  75. }
  76. } else {
  77. return -ENODEV;
  78. }
  79. ret = dw_spi_add_host(&pdev->dev, dws);
  80. if (ret)
  81. return ret;
  82. /* PCI hook and SPI hook use the same drv data */
  83. pci_set_drvdata(pdev, dwpci);
  84. dev_info(&pdev->dev, "found PCI SPI controller(ID: %04x:%04x)\n",
  85. pdev->vendor, pdev->device);
  86. return 0;
  87. }
  88. static void spi_pci_remove(struct pci_dev *pdev)
  89. {
  90. struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
  91. dw_spi_remove_host(&dwpci->dws);
  92. }
  93. #ifdef CONFIG_PM_SLEEP
  94. static int spi_suspend(struct device *dev)
  95. {
  96. struct pci_dev *pdev = to_pci_dev(dev);
  97. struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
  98. return dw_spi_suspend_host(&dwpci->dws);
  99. }
  100. static int spi_resume(struct device *dev)
  101. {
  102. struct pci_dev *pdev = to_pci_dev(dev);
  103. struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
  104. return dw_spi_resume_host(&dwpci->dws);
  105. }
  106. #endif
  107. static SIMPLE_DEV_PM_OPS(dw_spi_pm_ops, spi_suspend, spi_resume);
  108. static const struct pci_device_id pci_ids[] = {
  109. /* Intel MID platform SPI controller 0 */
  110. /*
  111. * The access to the device 8086:0801 is disabled by HW, since it's
  112. * exclusively used by SCU to communicate with MSIC.
  113. */
  114. /* Intel MID platform SPI controller 1 */
  115. { PCI_VDEVICE(INTEL, 0x0800), (kernel_ulong_t)&spi_pci_mid_desc_1},
  116. /* Intel MID platform SPI controller 2 */
  117. { PCI_VDEVICE(INTEL, 0x0812), (kernel_ulong_t)&spi_pci_mid_desc_2},
  118. {},
  119. };
  120. static struct pci_driver dw_spi_driver = {
  121. .name = DRIVER_NAME,
  122. .id_table = pci_ids,
  123. .probe = spi_pci_probe,
  124. .remove = spi_pci_remove,
  125. .driver = {
  126. .pm = &dw_spi_pm_ops,
  127. },
  128. };
  129. module_pci_driver(dw_spi_driver);
  130. MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
  131. MODULE_DESCRIPTION("PCI interface driver for DW SPI Core");
  132. MODULE_LICENSE("GPL v2");