sandbox-dma-test.c 5.9 KB

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