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