drv_dma.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-12-02 bigmagic first version
  9. */
  10. #include "drv_dma.h"
  11. #include "raspi4.h"
  12. volatile unsigned int __attribute__((aligned(256))) dma_disc[32];
  13. //https://www.raspberrypi.org/forums/viewtopic.php?f=72&t=10276
  14. static struct rt_semaphore dma_sem;
  15. //DMA 0 1 2 3 4 5 6
  16. typedef struct _dma_ctrl_block
  17. {
  18. unsigned int TI; // Transfer information
  19. unsigned int SOURCE_AD; // source address
  20. unsigned int DEST_AD; // destination address
  21. unsigned int TXFR_LEN; // transfer length
  22. unsigned int STRIDE; // 2D mode stride
  23. struct _dma_ctrl_block *NEXTCONBK; // Next control block address
  24. unsigned int DEBUG;
  25. unsigned int reserved1;
  26. } dma_ctrl_block_t;
  27. //DMA 7 8 9 10
  28. typedef struct _dma_lite_ctrl_block
  29. {
  30. unsigned int TI; // Transfer information
  31. unsigned int SOURCE_AD; // source address
  32. unsigned int DEST_AD; // destination address
  33. unsigned int TXFR_LEN; // transfer length
  34. struct _dma_lite_ctrl_block *NEXTCONBK; // Next control block address
  35. unsigned int DEBUG;
  36. unsigned int reserved1;
  37. unsigned int reserved2;
  38. } dma_lite_ctrl_block_t;
  39. //DMA 11 12 13 14 15
  40. typedef struct _dma4_ctrl_block
  41. {
  42. unsigned int TI; // Transfer information
  43. unsigned int SOURCE_AD0; // source address0
  44. unsigned int SOURCE_AD1; // source address1
  45. unsigned int DEST_AD0; // destination address0
  46. unsigned int DEST_AD1; // destination address1
  47. unsigned int TXFR_LEN; // transfer length
  48. unsigned int STRIDE; // 2D mode stride
  49. struct _dma4_ctrl_block *NEXTCONBK; // Next control block address
  50. } dma4_ctrl_block_t;
  51. static dma_lite_ctrl_block_t *ctr_blocks;
  52. static void dma_irq(int irq, void *param)
  53. {
  54. if (DMA_INT_STATUS_REG & DMA_INT7)
  55. {
  56. DMA_CS(7) = DMA_CS_INT;
  57. rt_sem_release(&dma_sem);
  58. }
  59. }
  60. //dma 7 8 9 10:XLENGTH
  61. rt_err_t dma_memcpy(void *src, void *dst, unsigned int size, unsigned int dch, unsigned int timeout)
  62. {
  63. rt_hw_cpu_dcache_ops(RT_HW_CACHE_INVALIDATE, dst, size);
  64. /* Stop DMA, if it was already started */
  65. DMA_CS(dch) = DMA_CS_RESET;
  66. /* Clear DMA status flags */
  67. DMA_CS(dch) = DMA_CS_INT | DMA_CS_END; /* Interrupted flag & Transmission ended flag*/
  68. //cb info
  69. ctr_blocks->TI = DMA_TI_SRC_INC | DMA_TI_DEST_INC | DMA_TI_INTEN;
  70. ctr_blocks->SOURCE_AD = (unsigned int)src;
  71. ctr_blocks->DEST_AD = (unsigned int)dst;
  72. ctr_blocks->TXFR_LEN = size;
  73. ctr_blocks->NEXTCONBK = 0;
  74. ctr_blocks->reserved1 = 0;
  75. ctr_blocks->reserved2 = 0;
  76. rt_hw_cpu_dcache_ops(RT_HW_CACHE_INVALIDATE, ctr_blocks, sizeof(dma_lite_ctrl_block_t) * 8);
  77. DMA_CONBLK_AD(dch) = (rt_uint32_t)ctr_blocks;
  78. DMA_CS(dch) = DMA_CS_INT | DMA_CS_END | DMA_CS_ACTIVE;
  79. if(rt_sem_take(&dma_sem, timeout) != RT_EOK)
  80. {
  81. rt_kprintf("dma transfer timeout!\n");
  82. return RT_ERROR;
  83. }
  84. return RT_EOK;
  85. }
  86. void dma_init(unsigned char dch)
  87. {
  88. rt_sem_init(&dma_sem, "dma_sem", 0, RT_IPC_FLAG_FIFO);
  89. ctr_blocks = (dma_lite_ctrl_block_t *)&dma_disc[0]; //rt_malloc(sizeof(DMA_Lite_Control_Block));
  90. //Make sure DMA channel is enabled by
  91. //writing the corresponding bit in DMA_ENABLE in the DMA register to 1
  92. DMA_ENABLE_REG = (1 << dch);
  93. rt_hw_interrupt_install(IRQ_DMA7_DMA8, dma_irq, RT_NULL, "dma_irq");
  94. rt_hw_interrupt_umask(IRQ_DMA7_DMA8);
  95. }