dma-uclass.c 1.5 KB

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