mmci_qcom_dml.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (c) 2011, The Linux Foundation. All rights reserved.
  5. */
  6. #include <linux/of.h>
  7. #include <linux/of_dma.h>
  8. #include <linux/bitops.h>
  9. #include <linux/mmc/host.h>
  10. #include <linux/mmc/card.h>
  11. #include "mmci.h"
  12. /* Registers */
  13. #define DML_CONFIG 0x00
  14. #define PRODUCER_CRCI_MSK GENMASK(1, 0)
  15. #define PRODUCER_CRCI_DISABLE 0
  16. #define PRODUCER_CRCI_X_SEL BIT(0)
  17. #define PRODUCER_CRCI_Y_SEL BIT(1)
  18. #define CONSUMER_CRCI_MSK GENMASK(3, 2)
  19. #define CONSUMER_CRCI_DISABLE 0
  20. #define CONSUMER_CRCI_X_SEL BIT(2)
  21. #define CONSUMER_CRCI_Y_SEL BIT(3)
  22. #define PRODUCER_TRANS_END_EN BIT(4)
  23. #define BYPASS BIT(16)
  24. #define DIRECT_MODE BIT(17)
  25. #define INFINITE_CONS_TRANS BIT(18)
  26. #define DML_SW_RESET 0x08
  27. #define DML_PRODUCER_START 0x0c
  28. #define DML_CONSUMER_START 0x10
  29. #define DML_PRODUCER_PIPE_LOGICAL_SIZE 0x14
  30. #define DML_CONSUMER_PIPE_LOGICAL_SIZE 0x18
  31. #define DML_PIPE_ID 0x1c
  32. #define PRODUCER_PIPE_ID_SHFT 0
  33. #define PRODUCER_PIPE_ID_MSK GENMASK(4, 0)
  34. #define CONSUMER_PIPE_ID_SHFT 16
  35. #define CONSUMER_PIPE_ID_MSK GENMASK(20, 16)
  36. #define DML_PRODUCER_BAM_BLOCK_SIZE 0x24
  37. #define DML_PRODUCER_BAM_TRANS_SIZE 0x28
  38. /* other definitions */
  39. #define PRODUCER_PIPE_LOGICAL_SIZE 4096
  40. #define CONSUMER_PIPE_LOGICAL_SIZE 4096
  41. #define DML_OFFSET 0x800
  42. static int qcom_dma_start(struct mmci_host *host, unsigned int *datactrl)
  43. {
  44. u32 config;
  45. void __iomem *base = host->base + DML_OFFSET;
  46. struct mmc_data *data = host->data;
  47. int ret = mmci_dmae_start(host, datactrl);
  48. if (ret)
  49. return ret;
  50. if (data->flags & MMC_DATA_READ) {
  51. /* Read operation: configure DML for producer operation */
  52. /* Set producer CRCI-x and disable consumer CRCI */
  53. config = readl_relaxed(base + DML_CONFIG);
  54. config = (config & ~PRODUCER_CRCI_MSK) | PRODUCER_CRCI_X_SEL;
  55. config = (config & ~CONSUMER_CRCI_MSK) | CONSUMER_CRCI_DISABLE;
  56. writel_relaxed(config, base + DML_CONFIG);
  57. /* Set the Producer BAM block size */
  58. writel_relaxed(data->blksz, base + DML_PRODUCER_BAM_BLOCK_SIZE);
  59. /* Set Producer BAM Transaction size */
  60. writel_relaxed(data->blocks * data->blksz,
  61. base + DML_PRODUCER_BAM_TRANS_SIZE);
  62. /* Set Producer Transaction End bit */
  63. config = readl_relaxed(base + DML_CONFIG);
  64. config |= PRODUCER_TRANS_END_EN;
  65. writel_relaxed(config, base + DML_CONFIG);
  66. /* Trigger producer */
  67. writel_relaxed(1, base + DML_PRODUCER_START);
  68. } else {
  69. /* Write operation: configure DML for consumer operation */
  70. /* Set consumer CRCI-x and disable producer CRCI*/
  71. config = readl_relaxed(base + DML_CONFIG);
  72. config = (config & ~CONSUMER_CRCI_MSK) | CONSUMER_CRCI_X_SEL;
  73. config = (config & ~PRODUCER_CRCI_MSK) | PRODUCER_CRCI_DISABLE;
  74. writel_relaxed(config, base + DML_CONFIG);
  75. /* Clear Producer Transaction End bit */
  76. config = readl_relaxed(base + DML_CONFIG);
  77. config &= ~PRODUCER_TRANS_END_EN;
  78. writel_relaxed(config, base + DML_CONFIG);
  79. /* Trigger consumer */
  80. writel_relaxed(1, base + DML_CONSUMER_START);
  81. }
  82. /* make sure the dml is configured before dma is triggered */
  83. wmb();
  84. return 0;
  85. }
  86. static int of_get_dml_pipe_index(struct device_node *np, const char *name)
  87. {
  88. int index;
  89. struct of_phandle_args dma_spec;
  90. index = of_property_match_string(np, "dma-names", name);
  91. if (index < 0)
  92. return -ENODEV;
  93. if (of_parse_phandle_with_args(np, "dmas", "#dma-cells", index,
  94. &dma_spec))
  95. return -ENODEV;
  96. if (dma_spec.args_count)
  97. return dma_spec.args[0];
  98. return -ENODEV;
  99. }
  100. /* Initialize the dml hardware connected to SD Card controller */
  101. static int qcom_dma_setup(struct mmci_host *host)
  102. {
  103. u32 config;
  104. void __iomem *base;
  105. int consumer_id, producer_id;
  106. struct device_node *np = host->mmc->parent->of_node;
  107. if (mmci_dmae_setup(host))
  108. return -EINVAL;
  109. consumer_id = of_get_dml_pipe_index(np, "tx");
  110. producer_id = of_get_dml_pipe_index(np, "rx");
  111. if (producer_id < 0 || consumer_id < 0) {
  112. mmci_dmae_release(host);
  113. return -EINVAL;
  114. }
  115. base = host->base + DML_OFFSET;
  116. /* Reset the DML block */
  117. writel_relaxed(1, base + DML_SW_RESET);
  118. /* Disable the producer and consumer CRCI */
  119. config = (PRODUCER_CRCI_DISABLE | CONSUMER_CRCI_DISABLE);
  120. /*
  121. * Disable the bypass mode. Bypass mode will only be used
  122. * if data transfer is to happen in PIO mode and don't
  123. * want the BAM interface to connect with SDCC-DML.
  124. */
  125. config &= ~BYPASS;
  126. /*
  127. * Disable direct mode as we don't DML to MASTER the AHB bus.
  128. * BAM connected with DML should MASTER the AHB bus.
  129. */
  130. config &= ~DIRECT_MODE;
  131. /*
  132. * Disable infinite mode transfer as we won't be doing any
  133. * infinite size data transfers. All data transfer will be
  134. * of finite data size.
  135. */
  136. config &= ~INFINITE_CONS_TRANS;
  137. writel_relaxed(config, base + DML_CONFIG);
  138. /*
  139. * Initialize the logical BAM pipe size for producer
  140. * and consumer.
  141. */
  142. writel_relaxed(PRODUCER_PIPE_LOGICAL_SIZE,
  143. base + DML_PRODUCER_PIPE_LOGICAL_SIZE);
  144. writel_relaxed(CONSUMER_PIPE_LOGICAL_SIZE,
  145. base + DML_CONSUMER_PIPE_LOGICAL_SIZE);
  146. /* Initialize Producer/consumer pipe id */
  147. writel_relaxed(producer_id | (consumer_id << CONSUMER_PIPE_ID_SHFT),
  148. base + DML_PIPE_ID);
  149. /* Make sure dml initialization is finished */
  150. mb();
  151. return 0;
  152. }
  153. static u32 qcom_get_dctrl_cfg(struct mmci_host *host)
  154. {
  155. return MCI_DPSM_ENABLE | (host->data->blksz << 4);
  156. }
  157. static struct mmci_host_ops qcom_variant_ops = {
  158. .prep_data = mmci_dmae_prep_data,
  159. .unprep_data = mmci_dmae_unprep_data,
  160. .get_datactrl_cfg = qcom_get_dctrl_cfg,
  161. .get_next_data = mmci_dmae_get_next_data,
  162. .dma_setup = qcom_dma_setup,
  163. .dma_release = mmci_dmae_release,
  164. .dma_start = qcom_dma_start,
  165. .dma_finalize = mmci_dmae_finalize,
  166. .dma_error = mmci_dmae_error,
  167. };
  168. void qcom_variant_init(struct mmci_host *host)
  169. {
  170. host->ops = &qcom_variant_ops;
  171. }