Path: blob/main/sys/contrib/ncsw/Peripherals/FM/fm_muram.c
48375 views
/*1* Copyright 2008-2012 Freescale Semiconductor Inc.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions are met:5* * Redistributions of source code must retain the above copyright6* notice, this list of conditions and the following disclaimer.7* * Redistributions in binary form must reproduce the above copyright8* notice, this list of conditions and the following disclaimer in the9* documentation and/or other materials provided with the distribution.10* * Neither the name of Freescale Semiconductor nor the11* names of its contributors may be used to endorse or promote products12* derived from this software without specific prior written permission.13*14*15* ALTERNATIVELY, this software may be distributed under the terms of the16* GNU General Public License ("GPL") as published by the Free Software17* Foundation, either version 2 of that License or (at your option) any18* later version.19*20* THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY21* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED22* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE23* DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY24* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES25* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;26* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND27* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT28* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/313233/******************************************************************************34@File FM_muram.c3536@Description FM MURAM ...37*//***************************************************************************/38#include "error_ext.h"39#include "std_ext.h"40#include "mm_ext.h"41#include "string_ext.h"42#include "sprint_ext.h"43#include "fm_muram_ext.h"44#include "fm_common.h"4546#define __ERR_MODULE__ MODULE_FM_MURAM474849typedef struct50{51t_Handle h_Mem;52uintptr_t baseAddr;53uint32_t size;54} t_FmMuram;555657void FmMuramClear(t_Handle h_FmMuram)58{59t_FmMuram *p_FmMuram = ( t_FmMuram *)h_FmMuram;6061SANITY_CHECK_RETURN(h_FmMuram, E_INVALID_HANDLE);62IOMemSet32(UINT_TO_PTR(p_FmMuram->baseAddr), 0, p_FmMuram->size);63}646566t_Handle FM_MURAM_ConfigAndInit(uintptr_t baseAddress, uint32_t size)67{68t_Handle h_Mem;69t_FmMuram *p_FmMuram;7071if (!baseAddress)72{73REPORT_ERROR(MAJOR, E_INVALID_VALUE, ("baseAddress 0 is not supported"));74return NULL;75}7677if (baseAddress%4)78{79REPORT_ERROR(MAJOR, E_INVALID_VALUE, ("baseAddress not 4 bytes aligned!"));80return NULL;81}8283/* Allocate FM MURAM structure */84p_FmMuram = (t_FmMuram *) XX_Malloc(sizeof(t_FmMuram));85if (!p_FmMuram)86{87REPORT_ERROR(MAJOR, E_NO_MEMORY, ("FM MURAM driver structure"));88return NULL;89}90memset(p_FmMuram, 0, sizeof(t_FmMuram));919293if ((MM_Init(&h_Mem, baseAddress, size) != E_OK) || (!h_Mem))94{95XX_Free(p_FmMuram);96REPORT_ERROR(MAJOR, E_INVALID_HANDLE, ("FM-MURAM partition!!!"));97return NULL;98}99100/* Initialize FM MURAM parameters which will be kept by the driver */101p_FmMuram->baseAddr = baseAddress;102p_FmMuram->size = size;103p_FmMuram->h_Mem = h_Mem;104105return p_FmMuram;106}107108t_Error FM_MURAM_Free(t_Handle h_FmMuram)109{110t_FmMuram *p_FmMuram = ( t_FmMuram *)h_FmMuram;111112if (p_FmMuram->h_Mem)113MM_Free(p_FmMuram->h_Mem);114115XX_Free(h_FmMuram);116117return E_OK;118}119120void * FM_MURAM_AllocMem(t_Handle h_FmMuram, uint32_t size, uint32_t align)121{122t_FmMuram *p_FmMuram = ( t_FmMuram *)h_FmMuram;123uintptr_t addr;124125SANITY_CHECK_RETURN_VALUE(h_FmMuram, E_INVALID_HANDLE, NULL);126SANITY_CHECK_RETURN_VALUE(p_FmMuram->h_Mem, E_INVALID_HANDLE, NULL);127128addr = (uintptr_t)MM_Get(p_FmMuram->h_Mem, size, align ,"FM MURAM");129130if (addr == ILLEGAL_BASE)131return NULL;132133return UINT_TO_PTR(addr);134}135136void * FM_MURAM_AllocMemForce(t_Handle h_FmMuram, uint64_t base, uint32_t size)137{138t_FmMuram *p_FmMuram = ( t_FmMuram *)h_FmMuram;139uintptr_t addr;140141SANITY_CHECK_RETURN_VALUE(h_FmMuram, E_INVALID_HANDLE, NULL);142SANITY_CHECK_RETURN_VALUE(p_FmMuram->h_Mem, E_INVALID_HANDLE, NULL);143144addr = (uintptr_t)MM_GetForce(p_FmMuram->h_Mem, base, size, "FM MURAM");145146if (addr == ILLEGAL_BASE)147return NULL;148149return UINT_TO_PTR(addr);150}151152t_Error FM_MURAM_FreeMem(t_Handle h_FmMuram, void *ptr)153{154t_FmMuram *p_FmMuram = ( t_FmMuram *)h_FmMuram;155156SANITY_CHECK_RETURN_ERROR(h_FmMuram, E_INVALID_HANDLE);157SANITY_CHECK_RETURN_ERROR(p_FmMuram->h_Mem, E_INVALID_HANDLE);158159if (MM_Put(p_FmMuram->h_Mem, PTR_TO_UINT(ptr)) == 0)160RETURN_ERROR(MINOR, E_INVALID_ADDRESS, ("memory pointer!!!"));161162return E_OK;163}164165uint64_t FM_MURAM_GetFreeMemSize(t_Handle h_FmMuram)166{167t_FmMuram *p_FmMuram = ( t_FmMuram *)h_FmMuram;168169SANITY_CHECK_RETURN_VALUE(h_FmMuram, E_INVALID_HANDLE, 0);170SANITY_CHECK_RETURN_VALUE(p_FmMuram->h_Mem, E_INVALID_HANDLE, 0);171172return MM_GetFreeMemSize(p_FmMuram->h_Mem);173}174175176