alcor_pci.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018 Oleksij Rempel <linux@rempel-privat.de>
  4. *
  5. * Driver for Alcor Micro AU6601 and AU6621 controllers
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/io.h>
  10. #include <linux/irq.h>
  11. #include <linux/mfd/core.h>
  12. #include <linux/module.h>
  13. #include <linux/pci.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm.h>
  16. #include <linux/alcor_pci.h>
  17. #define DRV_NAME_ALCOR_PCI "alcor_pci"
  18. static DEFINE_IDA(alcor_pci_idr);
  19. static struct mfd_cell alcor_pci_cells[] = {
  20. [ALCOR_SD_CARD] = {
  21. .name = DRV_NAME_ALCOR_PCI_SDMMC,
  22. },
  23. [ALCOR_MS_CARD] = {
  24. .name = DRV_NAME_ALCOR_PCI_MS,
  25. },
  26. };
  27. static const struct alcor_dev_cfg alcor_cfg = {
  28. .dma = 0,
  29. };
  30. static const struct alcor_dev_cfg au6621_cfg = {
  31. .dma = 1,
  32. };
  33. static const struct alcor_dev_cfg au6625_cfg = {
  34. .dma = 0,
  35. };
  36. static const struct pci_device_id pci_ids[] = {
  37. { PCI_DEVICE(PCI_ID_ALCOR_MICRO, PCI_ID_AU6601),
  38. .driver_data = (kernel_ulong_t)&alcor_cfg },
  39. { PCI_DEVICE(PCI_ID_ALCOR_MICRO, PCI_ID_AU6621),
  40. .driver_data = (kernel_ulong_t)&au6621_cfg },
  41. { PCI_DEVICE(PCI_ID_ALCOR_MICRO, PCI_ID_AU6625),
  42. .driver_data = (kernel_ulong_t)&au6625_cfg },
  43. {},
  44. };
  45. MODULE_DEVICE_TABLE(pci, pci_ids);
  46. void alcor_write8(struct alcor_pci_priv *priv, u8 val, unsigned int addr)
  47. {
  48. writeb(val, priv->iobase + addr);
  49. }
  50. EXPORT_SYMBOL_GPL(alcor_write8);
  51. void alcor_write16(struct alcor_pci_priv *priv, u16 val, unsigned int addr)
  52. {
  53. writew(val, priv->iobase + addr);
  54. }
  55. EXPORT_SYMBOL_GPL(alcor_write16);
  56. void alcor_write32(struct alcor_pci_priv *priv, u32 val, unsigned int addr)
  57. {
  58. writel(val, priv->iobase + addr);
  59. }
  60. EXPORT_SYMBOL_GPL(alcor_write32);
  61. void alcor_write32be(struct alcor_pci_priv *priv, u32 val, unsigned int addr)
  62. {
  63. iowrite32be(val, priv->iobase + addr);
  64. }
  65. EXPORT_SYMBOL_GPL(alcor_write32be);
  66. u8 alcor_read8(struct alcor_pci_priv *priv, unsigned int addr)
  67. {
  68. return readb(priv->iobase + addr);
  69. }
  70. EXPORT_SYMBOL_GPL(alcor_read8);
  71. u32 alcor_read32(struct alcor_pci_priv *priv, unsigned int addr)
  72. {
  73. return readl(priv->iobase + addr);
  74. }
  75. EXPORT_SYMBOL_GPL(alcor_read32);
  76. u32 alcor_read32be(struct alcor_pci_priv *priv, unsigned int addr)
  77. {
  78. return ioread32be(priv->iobase + addr);
  79. }
  80. EXPORT_SYMBOL_GPL(alcor_read32be);
  81. static int alcor_pci_probe(struct pci_dev *pdev,
  82. const struct pci_device_id *ent)
  83. {
  84. struct alcor_dev_cfg *cfg;
  85. struct alcor_pci_priv *priv;
  86. int ret, i, bar = 0;
  87. cfg = (void *)ent->driver_data;
  88. ret = pcim_enable_device(pdev);
  89. if (ret)
  90. return ret;
  91. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  92. if (!priv)
  93. return -ENOMEM;
  94. ret = ida_alloc(&alcor_pci_idr, GFP_KERNEL);
  95. if (ret < 0)
  96. return ret;
  97. priv->id = ret;
  98. priv->pdev = pdev;
  99. priv->parent_pdev = pdev->bus->self;
  100. priv->dev = &pdev->dev;
  101. priv->cfg = cfg;
  102. priv->irq = pdev->irq;
  103. ret = pci_request_regions(pdev, DRV_NAME_ALCOR_PCI);
  104. if (ret) {
  105. dev_err(&pdev->dev, "Cannot request region\n");
  106. ret = -ENOMEM;
  107. goto error_free_ida;
  108. }
  109. if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
  110. dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
  111. ret = -ENODEV;
  112. goto error_release_regions;
  113. }
  114. priv->iobase = pcim_iomap(pdev, bar, 0);
  115. if (!priv->iobase) {
  116. ret = -ENOMEM;
  117. goto error_release_regions;
  118. }
  119. /* make sure irqs are disabled */
  120. alcor_write32(priv, 0, AU6601_REG_INT_ENABLE);
  121. alcor_write32(priv, 0, AU6601_MS_INT_ENABLE);
  122. ret = dma_set_mask_and_coherent(priv->dev, AU6601_SDMA_MASK);
  123. if (ret) {
  124. dev_err(priv->dev, "Failed to set DMA mask\n");
  125. goto error_release_regions;
  126. }
  127. pci_set_master(pdev);
  128. pci_set_drvdata(pdev, priv);
  129. for (i = 0; i < ARRAY_SIZE(alcor_pci_cells); i++) {
  130. alcor_pci_cells[i].platform_data = priv;
  131. alcor_pci_cells[i].pdata_size = sizeof(*priv);
  132. }
  133. ret = mfd_add_devices(&pdev->dev, priv->id, alcor_pci_cells,
  134. ARRAY_SIZE(alcor_pci_cells), NULL, 0, NULL);
  135. if (ret < 0)
  136. goto error_clear_drvdata;
  137. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
  138. return 0;
  139. error_clear_drvdata:
  140. pci_clear_master(pdev);
  141. pci_set_drvdata(pdev, NULL);
  142. error_release_regions:
  143. pci_release_regions(pdev);
  144. error_free_ida:
  145. ida_free(&alcor_pci_idr, priv->id);
  146. return ret;
  147. }
  148. static void alcor_pci_remove(struct pci_dev *pdev)
  149. {
  150. struct alcor_pci_priv *priv;
  151. priv = pci_get_drvdata(pdev);
  152. mfd_remove_devices(&pdev->dev);
  153. ida_free(&alcor_pci_idr, priv->id);
  154. pci_release_regions(pdev);
  155. pci_clear_master(pdev);
  156. pci_set_drvdata(pdev, NULL);
  157. }
  158. #ifdef CONFIG_PM_SLEEP
  159. static int alcor_suspend(struct device *dev)
  160. {
  161. return 0;
  162. }
  163. static int alcor_resume(struct device *dev)
  164. {
  165. struct alcor_pci_priv *priv = dev_get_drvdata(dev);
  166. pci_disable_link_state(priv->pdev,
  167. PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
  168. return 0;
  169. }
  170. #endif /* CONFIG_PM_SLEEP */
  171. static SIMPLE_DEV_PM_OPS(alcor_pci_pm_ops, alcor_suspend, alcor_resume);
  172. static struct pci_driver alcor_driver = {
  173. .name = DRV_NAME_ALCOR_PCI,
  174. .id_table = pci_ids,
  175. .probe = alcor_pci_probe,
  176. .remove = alcor_pci_remove,
  177. .driver = {
  178. .pm = &alcor_pci_pm_ops
  179. },
  180. };
  181. module_pci_driver(alcor_driver);
  182. MODULE_AUTHOR("Oleksij Rempel <linux@rempel-privat.de>");
  183. MODULE_DESCRIPTION("PCI driver for Alcor Micro AU6601 Secure Digital Host Controller Interface");
  184. MODULE_LICENSE("GPL");