synopGMAC_plat.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-08-24 chinesebear first version
  9. */
  10. #include "synopGMAC_plat.h"
  11. #include "synopGMAC_Dev.h"
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. extern void flush_cache(unsigned long start_addr, unsigned long size);
  15. dma_addr_t __attribute__((weak)) gmac_dmamap(unsigned long va,u32 size)
  16. {
  17. return VA_TO_PA (va);
  18. //return UNCACHED_TO_PHYS(va);
  19. }
  20. /**
  21. * This is a wrapper function for Memory allocation routine. In linux Kernel
  22. * it it kmalloc function
  23. * @param[in] bytes in bytes to allocate
  24. */
  25. void *plat_alloc_memory(u32 bytes)
  26. {
  27. //return (void*)malloc((size_t)bytes, M_DEVBUF, M_DONTWAIT);
  28. void *buf = (void*)rt_malloc((u32)bytes);
  29. flush_cache((unsigned long)buf, bytes);
  30. return buf;
  31. }
  32. /**
  33. * This is a wrapper function for consistent dma-able Memory allocation routine.
  34. * In linux Kernel, it depends on pci dev structure
  35. * @param[in] bytes in bytes to allocate
  36. */
  37. //void *plat_alloc_consistent_dmaable_memory(struct synopGMACdevice *dev, u32 size, u32 *addr)
  38. void *plat_alloc_consistent_dmaable_memory(synopGMACdevice *pcidev, u32 size, u32 *addr)
  39. {
  40. void *buf;
  41. buf = (void*)rt_malloc((u32)(size+16));
  42. //CPU_IOFlushDCache( buf,size, SYNC_W);
  43. unsigned long i = (unsigned long)buf;
  44. // rt_kprintf("size = %d\n", size);
  45. // rt_kprintf("bufaddr = %p\n", buf);
  46. // rt_kprintf("i%%16 == %d\n", i%16);
  47. if(i%16 == 8){
  48. i += 8;
  49. }
  50. else if(i%16 == 4){
  51. i += 12;
  52. }
  53. else if(i%16 == 12){
  54. i += 4;
  55. }
  56. flush_cache(i, size);
  57. *addr =gmac_dmamap(i, size);
  58. buf = (unsigned char *)CACHED_TO_UNCACHED(i);
  59. // rt_kprintf("bufaddr = %p\n", buf);
  60. return buf;
  61. }
  62. /**
  63. * This is a wrapper function for freeing consistent dma-able Memory.
  64. * In linux Kernel, it depends on pci dev structure
  65. * @param[in] bytes in bytes to allocate
  66. */
  67. //void plat_free_consistent_dmaable_memory(void * addr)
  68. void plat_free_consistent_dmaable_memory(synopGMACdevice *pcidev, u32 size, void * addr,u32 dma_addr)
  69. {
  70. rt_free((void*)PHYS_TO_CACHED(UNCACHED_TO_PHYS(addr)));
  71. return;
  72. }
  73. /**
  74. * This is a wrapper function for Memory free routine. In linux Kernel
  75. * it it kfree function
  76. * @param[in] buffer pointer to be freed
  77. */
  78. void plat_free_memory(void *buffer)
  79. {
  80. rt_free(buffer);
  81. return ;
  82. }
  83. dma_addr_t plat_dma_map_single(void *hwdev, void *ptr,
  84. u32 size)
  85. {
  86. unsigned long addr = (unsigned long) ptr;
  87. //CPU_IOFlushDCache(addr,size, direction);
  88. flush_cache(addr, size);
  89. return gmac_dmamap(addr, size);
  90. }
  91. /**
  92. * This is a wrapper function for platform dependent delay
  93. * Take care while passing the argument to this function
  94. * @param[in] buffer pointer to be freed
  95. */
  96. void plat_delay(u32 delay)
  97. {
  98. while (delay--);
  99. return;
  100. }