synopGMAC_plat.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2006-2018, 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. * 2020-08-10 lizhirui porting to ls2k
  10. */
  11. #include "synopGMAC_plat.h"
  12. #include "synopGMAC_Dev.h"
  13. #include <rthw.h>
  14. #include <rtthread.h>
  15. void flush_cache(unsigned long start_addr, unsigned long size)
  16. {
  17. /*r4k_dcache_wback_inv(start_addr,size);
  18. //rt_kprintf("flush_cache:start_addr = 0x%p,size = 0x%p",start_addr,size);
  19. unsigned long new_addr = start_addr - CACHED_MEMORY_ADDR + UNCACHED_MEMORY_ADDR;
  20. rt_memcpy(new_addr,start_addr,size);
  21. if(rt_memcmp(start_addr,new_addr,size) != 0)
  22. {
  23. rt_kprintf("flush_cache:data isn't matched!\n");
  24. while(1);
  25. }
  26. else
  27. {
  28. //rt_kprintf("flush_cache:data is matched!\n");
  29. }*/
  30. }
  31. //convert virtual address to physical address
  32. dma_addr_t __attribute__((weak)) gmac_dmamap(unsigned long va,u32 size)
  33. {
  34. return VA_TO_PA (va);
  35. //return UNCACHED_TO_PHYS(va);
  36. }
  37. /**
  38. * This is a wrapper function for Memory allocation routine. In linux Kernel
  39. * it it kmalloc function
  40. * @param[in] bytes in bytes to allocate
  41. */
  42. void *plat_alloc_memory(u32 bytes)
  43. {
  44. //return (void*)malloc((size_t)bytes, M_DEVBUF, M_DONTWAIT);
  45. void *buf = (void*)rt_malloc((u32)bytes);
  46. flush_cache((unsigned long)buf, bytes);
  47. return buf;
  48. }
  49. /**
  50. * This is a wrapper function for consistent dma-able Memory allocation routine.
  51. * In linux Kernel, it depends on pci dev structure
  52. * @param[in] bytes in bytes to allocate
  53. */
  54. //allocate a space aligned to 16-byte boundary without cache
  55. void *plat_alloc_consistent_dmaable_memory(synopGMACdevice *pcidev, u32 size, u32 *addr)
  56. {
  57. void *buf;
  58. buf = (void*)rt_malloc((u32)(size + 16));
  59. //CPU_IOFlushDCache( buf,size, SYNC_W);
  60. unsigned long i = (unsigned long)buf;
  61. // rt_kprintf("size = %d\n", size);
  62. // rt_kprintf("bufaddr = %p\n", buf);
  63. // rt_kprintf("i%%16 == %d\n", i%16);
  64. if(i % 16 == 8){
  65. i += 8;
  66. }
  67. else if(i % 16 == 4){
  68. i += 12;
  69. }
  70. else if(i % 16 == 12){
  71. i += 4;
  72. }
  73. flush_cache(i, size);
  74. *addr = gmac_dmamap(i, size);
  75. buf = (unsigned char *)CACHED_TO_UNCACHED(i);
  76. //rt_kprintf("bufaddr = %p\n", buf);
  77. return buf;
  78. }
  79. /**
  80. * This is a wrapper function for freeing consistent dma-able Memory.
  81. * In linux Kernel, it depends on pci dev structure
  82. * @param[in] bytes in bytes to allocate
  83. */
  84. //void plat_free_consistent_dmaable_memory(void * addr)
  85. void plat_free_consistent_dmaable_memory(synopGMACdevice *pcidev, u32 size, void * addr,u64 dma_addr)
  86. {
  87. rt_free((void*)PHYS_TO_CACHED(UNCACHED_TO_PHYS(addr)));
  88. return;
  89. }
  90. /**
  91. * This is a wrapper function for Memory free routine. In linux Kernel
  92. * it it kfree function
  93. * @param[in] buffer pointer to be freed
  94. */
  95. void plat_free_memory(void *buffer)
  96. {
  97. rt_free(buffer);
  98. return ;
  99. }
  100. //convert virtual address to physical address and flush cache
  101. dma_addr_t plat_dma_map_single(void *hwdev,void *ptr,u32 size)
  102. {
  103. unsigned long addr = (unsigned long) ptr;
  104. //CPU_IOFlushDCache(addr,size, direction);
  105. flush_cache(addr, size);
  106. return gmac_dmamap(addr, size);
  107. }
  108. /**
  109. * This is a wrapper function for platform dependent delay
  110. * Take care while passing the argument to this function
  111. * @param[in] buffer pointer to be freed
  112. */
  113. void plat_delay(u32 delay)
  114. {
  115. while (delay--);
  116. return;
  117. }