synopGMAC_plat.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. {
  66. i += 8;
  67. }
  68. else if (i % 16 == 4)
  69. {
  70. i += 12;
  71. }
  72. else if (i % 16 == 12)
  73. {
  74. i += 4;
  75. }
  76. flush_cache(i, size);
  77. *addr = gmac_dmamap(i, size);
  78. buf = (unsigned char *)CACHED_TO_UNCACHED(i);
  79. //rt_kprintf("bufaddr = %p\n", buf);
  80. return buf;
  81. }
  82. /**
  83. * This is a wrapper function for freeing consistent dma-able Memory.
  84. * In linux Kernel, it depends on pci dev structure
  85. * @param[in] bytes in bytes to allocate
  86. */
  87. //void plat_free_consistent_dmaable_memory(void * addr)
  88. void plat_free_consistent_dmaable_memory(synopGMACdevice *pcidev, u32 size, void *addr, u64 dma_addr)
  89. {
  90. rt_free((void *)PHYS_TO_CACHED(UNCACHED_TO_PHYS(addr)));
  91. return;
  92. }
  93. /**
  94. * This is a wrapper function for Memory free routine. In linux Kernel
  95. * it it kfree function
  96. * @param[in] buffer pointer to be freed
  97. */
  98. void plat_free_memory(void *buffer)
  99. {
  100. rt_free(buffer);
  101. return ;
  102. }
  103. //convert virtual address to physical address and flush cache
  104. dma_addr_t plat_dma_map_single(void *hwdev, void *ptr, u32 size)
  105. {
  106. unsigned long addr = (unsigned long) ptr;
  107. //CPU_IOFlushDCache(addr,size, direction);
  108. flush_cache(addr, size);
  109. return gmac_dmamap(addr, size);
  110. }
  111. /**
  112. * This is a wrapper function for platform dependent delay
  113. * Take care while passing the argument to this function
  114. * @param[in] buffer pointer to be freed
  115. */
  116. void plat_delay(u32 delay)
  117. {
  118. while (delay--);
  119. return;
  120. }