synopGMAC_plat.c 2.9 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2022, RT-Thread Development Team
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8 9 10
 *
 * Change Logs:
 * Date           Author       Notes
 * 2017-08-24     chinesebear  first version
 */

11

12 13 14 15 16 17 18
#include "synopGMAC_plat.h"
#include "synopGMAC_Dev.h"
#include <rthw.h>
#include <rtthread.h>
extern void flush_cache(unsigned long start_addr, unsigned long size);
dma_addr_t __attribute__((weak)) gmac_dmamap(unsigned long va,u32 size)
{
19 20
    return VA_TO_PA (va);
    //return UNCACHED_TO_PHYS(va);
21 22 23 24 25
}



/**
26
  * This is a wrapper function for Memory allocation routine. In linux Kernel
27 28 29 30
  * it it kmalloc function
  * @param[in] bytes in bytes to allocate
  */

31
void *plat_alloc_memory(u32 bytes)
32 33
{
//return (void*)malloc((size_t)bytes, M_DEVBUF, M_DONTWAIT);
34
    void *buf = (void*)rt_malloc((u32)bytes);
35

36 37
    flush_cache((unsigned long)buf, bytes);
    return buf;
38 39 40
}

/**
41
  * This is a wrapper function for consistent dma-able Memory allocation routine.
42 43 44 45
  * In linux Kernel, it depends on pci dev structure
  * @param[in] bytes in bytes to allocate
  */

46 47
//void *plat_alloc_consistent_dmaable_memory(struct synopGMACdevice *dev, u32 size, u32 *addr)
void *plat_alloc_consistent_dmaable_memory(synopGMACdevice *pcidev, u32 size, u32 *addr)
48
{
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    void *buf;
    buf = (void*)rt_malloc((u32)(size+16));
    //CPU_IOFlushDCache( buf,size, SYNC_W);
    unsigned long i = (unsigned long)buf;
//  rt_kprintf("size = %d\n", size);
//  rt_kprintf("bufaddr = %p\n", buf);
//  rt_kprintf("i%%16 == %d\n", i%16);
    if(i%16 == 8){
        i += 8;
    }
    else if(i%16 == 4){
        i += 12;
    }
    else if(i%16 == 12){
        i += 4;
    }

    flush_cache(i, size);
    *addr =gmac_dmamap(i, size);
    buf = (unsigned char *)CACHED_TO_UNCACHED(i);
//  rt_kprintf("bufaddr = %p\n", buf);
    return buf;
71 72 73 74 75 76 77 78 79 80
}


/**
  * This is a wrapper function for freeing consistent dma-able Memory.
  * In linux Kernel, it depends on pci dev structure
  * @param[in] bytes in bytes to allocate
  */


81 82
//void plat_free_consistent_dmaable_memory(void * addr)
void plat_free_consistent_dmaable_memory(synopGMACdevice *pcidev, u32 size, void * addr,u32 dma_addr)
83
{
84
    rt_free((void*)PHYS_TO_CACHED(UNCACHED_TO_PHYS(addr)));
85 86 87 88 89 90
 return;
}



/**
91
  * This is a wrapper function for Memory free routine. In linux Kernel
92 93 94
  * it it kfree function
  * @param[in] buffer pointer to be freed
  */
95
void plat_free_memory(void *buffer)
96
{
97 98
    rt_free(buffer);
    return ;
99 100 101 102 103
}



dma_addr_t plat_dma_map_single(void *hwdev, void *ptr,
104
                            u32 size)
105
{
106
        unsigned long addr = (unsigned long) ptr;
107
//CPU_IOFlushDCache(addr,size, direction);
108
    flush_cache(addr, size);
109 110 111 112
return gmac_dmamap(addr, size);
}

/**
113 114
  * This is a wrapper function for platform dependent delay
  * Take care while passing the argument to this function
115 116 117 118
  * @param[in] buffer pointer to be freed
  */
void plat_delay(u32 delay)
{
119 120
    while (delay--);
    return;
121 122 123
}