cavium-thunderx.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Driver for MMC and SSD cards for Cavium ThunderX SOCs.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 2016 Cavium Inc.
  9. */
  10. #include <linux/device.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/mmc/mmc.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/pci.h>
  18. #include "cavium.h"
  19. static void thunder_mmc_acquire_bus(struct cvm_mmc_host *host)
  20. {
  21. down(&host->mmc_serializer);
  22. }
  23. static void thunder_mmc_release_bus(struct cvm_mmc_host *host)
  24. {
  25. up(&host->mmc_serializer);
  26. }
  27. static void thunder_mmc_int_enable(struct cvm_mmc_host *host, u64 val)
  28. {
  29. writeq(val, host->base + MIO_EMM_INT(host));
  30. writeq(val, host->base + MIO_EMM_INT_EN_SET(host));
  31. }
  32. static int thunder_mmc_register_interrupts(struct cvm_mmc_host *host,
  33. struct pci_dev *pdev)
  34. {
  35. int nvec, ret, i;
  36. nvec = pci_alloc_irq_vectors(pdev, 1, 9, PCI_IRQ_MSIX);
  37. if (nvec < 0)
  38. return nvec;
  39. /* register interrupts */
  40. for (i = 0; i < nvec; i++) {
  41. ret = devm_request_irq(&pdev->dev, pci_irq_vector(pdev, i),
  42. cvm_mmc_interrupt,
  43. 0, cvm_mmc_irq_names[i], host);
  44. if (ret)
  45. return ret;
  46. }
  47. return 0;
  48. }
  49. static int thunder_mmc_probe(struct pci_dev *pdev,
  50. const struct pci_device_id *id)
  51. {
  52. struct device_node *node = pdev->dev.of_node;
  53. struct device *dev = &pdev->dev;
  54. struct device_node *child_node;
  55. struct cvm_mmc_host *host;
  56. int ret, i = 0;
  57. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  58. if (!host)
  59. return -ENOMEM;
  60. pci_set_drvdata(pdev, host);
  61. ret = pcim_enable_device(pdev);
  62. if (ret)
  63. return ret;
  64. ret = pci_request_regions(pdev, KBUILD_MODNAME);
  65. if (ret)
  66. return ret;
  67. host->base = pcim_iomap(pdev, 0, pci_resource_len(pdev, 0));
  68. if (!host->base) {
  69. ret = -EINVAL;
  70. goto error;
  71. }
  72. /* On ThunderX these are identical */
  73. host->dma_base = host->base;
  74. host->reg_off = 0x2000;
  75. host->reg_off_dma = 0x160;
  76. host->clk = devm_clk_get(dev, NULL);
  77. if (IS_ERR(host->clk)) {
  78. ret = PTR_ERR(host->clk);
  79. goto error;
  80. }
  81. ret = clk_prepare_enable(host->clk);
  82. if (ret)
  83. goto error;
  84. host->sys_freq = clk_get_rate(host->clk);
  85. spin_lock_init(&host->irq_handler_lock);
  86. sema_init(&host->mmc_serializer, 1);
  87. host->dev = dev;
  88. host->acquire_bus = thunder_mmc_acquire_bus;
  89. host->release_bus = thunder_mmc_release_bus;
  90. host->int_enable = thunder_mmc_int_enable;
  91. host->use_sg = true;
  92. host->big_dma_addr = true;
  93. host->need_irq_handler_lock = true;
  94. host->last_slot = -1;
  95. ret = dma_set_mask(dev, DMA_BIT_MASK(48));
  96. if (ret)
  97. goto error;
  98. /*
  99. * Clear out any pending interrupts that may be left over from
  100. * bootloader. Writing 1 to the bits clears them.
  101. */
  102. writeq(127, host->base + MIO_EMM_INT_EN(host));
  103. writeq(3, host->base + MIO_EMM_DMA_INT_ENA_W1C(host));
  104. /* Clear DMA FIFO */
  105. writeq(BIT_ULL(16), host->base + MIO_EMM_DMA_FIFO_CFG(host));
  106. ret = thunder_mmc_register_interrupts(host, pdev);
  107. if (ret)
  108. goto error;
  109. for_each_child_of_node(node, child_node) {
  110. /*
  111. * mmc_of_parse and devm* require one device per slot.
  112. * Create a dummy device per slot and set the node pointer to
  113. * the slot. The easiest way to get this is using
  114. * of_platform_device_create.
  115. */
  116. if (of_device_is_compatible(child_node, "mmc-slot")) {
  117. host->slot_pdev[i] = of_platform_device_create(child_node, NULL,
  118. &pdev->dev);
  119. if (!host->slot_pdev[i])
  120. continue;
  121. ret = cvm_mmc_of_slot_probe(&host->slot_pdev[i]->dev, host);
  122. if (ret) {
  123. of_node_put(child_node);
  124. goto error;
  125. }
  126. }
  127. i++;
  128. }
  129. dev_info(dev, "probed\n");
  130. return 0;
  131. error:
  132. for (i = 0; i < CAVIUM_MAX_MMC; i++) {
  133. if (host->slot[i])
  134. cvm_mmc_of_slot_remove(host->slot[i]);
  135. if (host->slot_pdev[i]) {
  136. get_device(&host->slot_pdev[i]->dev);
  137. of_platform_device_destroy(&host->slot_pdev[i]->dev, NULL);
  138. put_device(&host->slot_pdev[i]->dev);
  139. }
  140. }
  141. clk_disable_unprepare(host->clk);
  142. pci_release_regions(pdev);
  143. return ret;
  144. }
  145. static void thunder_mmc_remove(struct pci_dev *pdev)
  146. {
  147. struct cvm_mmc_host *host = pci_get_drvdata(pdev);
  148. u64 dma_cfg;
  149. int i;
  150. for (i = 0; i < CAVIUM_MAX_MMC; i++)
  151. if (host->slot[i])
  152. cvm_mmc_of_slot_remove(host->slot[i]);
  153. dma_cfg = readq(host->dma_base + MIO_EMM_DMA_CFG(host));
  154. dma_cfg &= ~MIO_EMM_DMA_CFG_EN;
  155. writeq(dma_cfg, host->dma_base + MIO_EMM_DMA_CFG(host));
  156. clk_disable_unprepare(host->clk);
  157. pci_release_regions(pdev);
  158. }
  159. static const struct pci_device_id thunder_mmc_id_table[] = {
  160. { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, 0xa010) },
  161. { 0, } /* end of table */
  162. };
  163. static struct pci_driver thunder_mmc_driver = {
  164. .name = KBUILD_MODNAME,
  165. .id_table = thunder_mmc_id_table,
  166. .probe = thunder_mmc_probe,
  167. .remove = thunder_mmc_remove,
  168. };
  169. module_pci_driver(thunder_mmc_driver);
  170. MODULE_AUTHOR("Cavium Inc.");
  171. MODULE_DESCRIPTION("Cavium ThunderX eMMC Driver");
  172. MODULE_LICENSE("GPL");
  173. MODULE_DEVICE_TABLE(pci, thunder_mmc_id_table);