dma-uclass.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Direct Memory Access U-Class driver
  4. *
  5. * (C) Copyright 2015
  6. * Texas Instruments Incorporated, <www.ti.com>
  7. *
  8. * Author: Mugunthan V N <mugunthanvnm@ti.com>
  9. */
  10. #include <common.h>
  11. #include <dma.h>
  12. #include <dm.h>
  13. #include <dm/uclass-internal.h>
  14. #include <dm/device-internal.h>
  15. #include <errno.h>
  16. int dma_get_device(u32 transfer_type, struct udevice **devp)
  17. {
  18. struct udevice *dev;
  19. int ret;
  20. for (ret = uclass_first_device(UCLASS_DMA, &dev); dev && !ret;
  21. ret = uclass_next_device(&dev)) {
  22. struct dma_dev_priv *uc_priv;
  23. uc_priv = dev_get_uclass_priv(dev);
  24. if (uc_priv->supported & transfer_type)
  25. break;
  26. }
  27. if (!dev) {
  28. pr_err("No DMA device found that supports %x type\n",
  29. transfer_type);
  30. return -EPROTONOSUPPORT;
  31. }
  32. *devp = dev;
  33. return ret;
  34. }
  35. int dma_memcpy(void *dst, void *src, size_t len)
  36. {
  37. struct udevice *dev;
  38. const struct dma_ops *ops;
  39. int ret;
  40. ret = dma_get_device(DMA_SUPPORTS_MEM_TO_MEM, &dev);
  41. if (ret < 0)
  42. return ret;
  43. ops = device_get_ops(dev);
  44. if (!ops->transfer)
  45. return -ENOSYS;
  46. /* Invalidate the area, so no writeback into the RAM races with DMA */
  47. invalidate_dcache_range((unsigned long)dst, (unsigned long)dst +
  48. roundup(len, ARCH_DMA_MINALIGN));
  49. return ops->transfer(dev, DMA_MEM_TO_MEM, dst, src, len);
  50. }
  51. UCLASS_DRIVER(dma) = {
  52. .id = UCLASS_DMA,
  53. .name = "dma",
  54. .flags = DM_UC_FLAG_SEQ_ALIAS,
  55. .per_device_auto_alloc_size = sizeof(struct dma_dev_priv),
  56. };