sandbox-dma-test.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Direct Memory Access U-Class Simulation driver
  4. *
  5. * Copyright (C) 2018 Texas Instruments Incorporated <www.ti.com>
  6. *
  7. * Author: Grygorii Strashko <grygorii.strashko@ti.com>
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <malloc.h>
  12. #include <dm/read.h>
  13. #include <dma-uclass.h>
  14. #include <dt-structs.h>
  15. #include <errno.h>
  16. #define SANDBOX_DMA_CH_CNT 3
  17. #define SANDBOX_DMA_BUF_SIZE 1024
  18. struct sandbox_dma_chan {
  19. struct sandbox_dma_dev *ud;
  20. char name[20];
  21. u32 id;
  22. enum dma_direction dir;
  23. bool in_use;
  24. bool enabled;
  25. };
  26. struct sandbox_dma_dev {
  27. struct device *dev;
  28. u32 ch_count;
  29. struct sandbox_dma_chan channels[SANDBOX_DMA_CH_CNT];
  30. uchar buf[SANDBOX_DMA_BUF_SIZE];
  31. uchar *buf_rx;
  32. size_t data_len;
  33. u32 meta;
  34. };
  35. static int sandbox_dma_transfer(struct udevice *dev, int direction,
  36. void *dst, void *src, size_t len)
  37. {
  38. memcpy(dst, src, len);
  39. return 0;
  40. }
  41. static int sandbox_dma_of_xlate(struct dma *dma,
  42. struct ofnode_phandle_args *args)
  43. {
  44. struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
  45. struct sandbox_dma_chan *uc;
  46. debug("%s(dma id=%u)\n", __func__, args->args[0]);
  47. if (args->args[0] >= SANDBOX_DMA_CH_CNT)
  48. return -EINVAL;
  49. dma->id = args->args[0];
  50. uc = &ud->channels[dma->id];
  51. if (dma->id == 1)
  52. uc->dir = DMA_MEM_TO_DEV;
  53. else if (dma->id == 2)
  54. uc->dir = DMA_DEV_TO_MEM;
  55. else
  56. uc->dir = DMA_MEM_TO_MEM;
  57. debug("%s(dma id=%lu dir=%d)\n", __func__, dma->id, uc->dir);
  58. return 0;
  59. }
  60. static int sandbox_dma_request(struct dma *dma)
  61. {
  62. struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
  63. struct sandbox_dma_chan *uc;
  64. if (dma->id >= SANDBOX_DMA_CH_CNT)
  65. return -EINVAL;
  66. uc = &ud->channels[dma->id];
  67. if (uc->in_use)
  68. return -EBUSY;
  69. uc->in_use = true;
  70. debug("%s(dma id=%lu in_use=%d)\n", __func__, dma->id, uc->in_use);
  71. return 0;
  72. }
  73. static int sandbox_dma_rfree(struct dma *dma)
  74. {
  75. struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
  76. struct sandbox_dma_chan *uc;
  77. if (dma->id >= SANDBOX_DMA_CH_CNT)
  78. return -EINVAL;
  79. uc = &ud->channels[dma->id];
  80. if (!uc->in_use)
  81. return -EINVAL;
  82. uc->in_use = false;
  83. ud->buf_rx = NULL;
  84. ud->data_len = 0;
  85. debug("%s(dma id=%lu in_use=%d)\n", __func__, dma->id, uc->in_use);
  86. return 0;
  87. }
  88. static int sandbox_dma_enable(struct dma *dma)
  89. {
  90. struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
  91. struct sandbox_dma_chan *uc;
  92. if (dma->id >= SANDBOX_DMA_CH_CNT)
  93. return -EINVAL;
  94. uc = &ud->channels[dma->id];
  95. if (!uc->in_use)
  96. return -EINVAL;
  97. if (uc->enabled)
  98. return -EINVAL;
  99. uc->enabled = true;
  100. debug("%s(dma id=%lu enabled=%d)\n", __func__, dma->id, uc->enabled);
  101. return 0;
  102. }
  103. static int sandbox_dma_disable(struct dma *dma)
  104. {
  105. struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
  106. struct sandbox_dma_chan *uc;
  107. if (dma->id >= SANDBOX_DMA_CH_CNT)
  108. return -EINVAL;
  109. uc = &ud->channels[dma->id];
  110. if (!uc->in_use)
  111. return -EINVAL;
  112. if (!uc->enabled)
  113. return -EINVAL;
  114. uc->enabled = false;
  115. debug("%s(dma id=%lu enabled=%d)\n", __func__, dma->id, uc->enabled);
  116. return 0;
  117. }
  118. static int sandbox_dma_send(struct dma *dma,
  119. void *src, size_t len, void *metadata)
  120. {
  121. struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
  122. struct sandbox_dma_chan *uc;
  123. if (dma->id >= SANDBOX_DMA_CH_CNT)
  124. return -EINVAL;
  125. if (!src || !metadata)
  126. return -EINVAL;
  127. debug("%s(dma id=%lu)\n", __func__, dma->id);
  128. uc = &ud->channels[dma->id];
  129. if (uc->dir != DMA_MEM_TO_DEV)
  130. return -EINVAL;
  131. if (!uc->in_use)
  132. return -EINVAL;
  133. if (!uc->enabled)
  134. return -EINVAL;
  135. if (len >= SANDBOX_DMA_BUF_SIZE)
  136. return -EINVAL;
  137. memcpy(ud->buf, src, len);
  138. ud->data_len = len;
  139. ud->meta = *((u32 *)metadata);
  140. debug("%s(dma id=%lu len=%zu meta=%08x)\n",
  141. __func__, dma->id, len, ud->meta);
  142. return 0;
  143. }
  144. static int sandbox_dma_receive(struct dma *dma, void **dst, void *metadata)
  145. {
  146. struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
  147. struct sandbox_dma_chan *uc;
  148. if (dma->id >= SANDBOX_DMA_CH_CNT)
  149. return -EINVAL;
  150. if (!dst || !metadata)
  151. return -EINVAL;
  152. uc = &ud->channels[dma->id];
  153. if (uc->dir != DMA_DEV_TO_MEM)
  154. return -EINVAL;
  155. if (!uc->in_use)
  156. return -EINVAL;
  157. if (!uc->enabled)
  158. return -EINVAL;
  159. if (!ud->data_len)
  160. return 0;
  161. if (ud->buf_rx) {
  162. memcpy(ud->buf_rx, ud->buf, ud->data_len);
  163. *dst = ud->buf_rx;
  164. } else {
  165. memcpy(*dst, ud->buf, ud->data_len);
  166. }
  167. *((u32 *)metadata) = ud->meta;
  168. debug("%s(dma id=%lu len=%zu meta=%08x %p)\n",
  169. __func__, dma->id, ud->data_len, ud->meta, *dst);
  170. return ud->data_len;
  171. }
  172. static int sandbox_dma_prepare_rcv_buf(struct dma *dma, void *dst, size_t size)
  173. {
  174. struct sandbox_dma_dev *ud = dev_get_priv(dma->dev);
  175. ud->buf_rx = dst;
  176. return 0;
  177. }
  178. static const struct dma_ops sandbox_dma_ops = {
  179. .transfer = sandbox_dma_transfer,
  180. .of_xlate = sandbox_dma_of_xlate,
  181. .request = sandbox_dma_request,
  182. .rfree = sandbox_dma_rfree,
  183. .enable = sandbox_dma_enable,
  184. .disable = sandbox_dma_disable,
  185. .send = sandbox_dma_send,
  186. .receive = sandbox_dma_receive,
  187. .prepare_rcv_buf = sandbox_dma_prepare_rcv_buf,
  188. };
  189. static int sandbox_dma_probe(struct udevice *dev)
  190. {
  191. struct dma_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  192. struct sandbox_dma_dev *ud = dev_get_priv(dev);
  193. int i, ret = 0;
  194. uc_priv->supported = DMA_SUPPORTS_MEM_TO_MEM |
  195. DMA_SUPPORTS_MEM_TO_DEV |
  196. DMA_SUPPORTS_DEV_TO_MEM;
  197. ud->ch_count = SANDBOX_DMA_CH_CNT;
  198. ud->buf_rx = NULL;
  199. ud->meta = 0;
  200. ud->data_len = 0;
  201. pr_err("Number of channels: %u\n", ud->ch_count);
  202. for (i = 0; i < ud->ch_count; i++) {
  203. struct sandbox_dma_chan *uc = &ud->channels[i];
  204. uc->ud = ud;
  205. uc->id = i;
  206. sprintf(uc->name, "DMA chan%d\n", i);
  207. uc->in_use = false;
  208. uc->enabled = false;
  209. }
  210. return ret;
  211. }
  212. static const struct udevice_id sandbox_dma_ids[] = {
  213. { .compatible = "sandbox,dma" },
  214. { }
  215. };
  216. U_BOOT_DRIVER(sandbox_dma) = {
  217. .name = "sandbox-dma",
  218. .id = UCLASS_DMA,
  219. .of_match = sandbox_dma_ids,
  220. .ops = &sandbox_dma_ops,
  221. .probe = sandbox_dma_probe,
  222. .priv_auto_alloc_size = sizeof(struct sandbox_dma_dev),
  223. };