1852ba100SJustin Hibbits /*
2852ba100SJustin Hibbits  * Copyright 2008-2012 Freescale Semiconductor Inc.
30aeed3e9SJustin Hibbits  *
40aeed3e9SJustin Hibbits  * Redistribution and use in source and binary forms, with or without
50aeed3e9SJustin Hibbits  * modification, are permitted provided that the following conditions are met:
60aeed3e9SJustin Hibbits  *     * Redistributions of source code must retain the above copyright
70aeed3e9SJustin Hibbits  *       notice, this list of conditions and the following disclaimer.
80aeed3e9SJustin Hibbits  *     * Redistributions in binary form must reproduce the above copyright
90aeed3e9SJustin Hibbits  *       notice, this list of conditions and the following disclaimer in the
100aeed3e9SJustin Hibbits  *       documentation and/or other materials provided with the distribution.
110aeed3e9SJustin Hibbits  *     * Neither the name of Freescale Semiconductor nor the
120aeed3e9SJustin Hibbits  *       names of its contributors may be used to endorse or promote products
130aeed3e9SJustin Hibbits  *       derived from this software without specific prior written permission.
140aeed3e9SJustin Hibbits  *
150aeed3e9SJustin Hibbits  *
160aeed3e9SJustin Hibbits  * ALTERNATIVELY, this software may be distributed under the terms of the
170aeed3e9SJustin Hibbits  * GNU General Public License ("GPL") as published by the Free Software
180aeed3e9SJustin Hibbits  * Foundation, either version 2 of that License or (at your option) any
190aeed3e9SJustin Hibbits  * later version.
200aeed3e9SJustin Hibbits  *
210aeed3e9SJustin Hibbits  * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
220aeed3e9SJustin Hibbits  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
230aeed3e9SJustin Hibbits  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
240aeed3e9SJustin Hibbits  * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
250aeed3e9SJustin Hibbits  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
260aeed3e9SJustin Hibbits  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
270aeed3e9SJustin Hibbits  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
280aeed3e9SJustin Hibbits  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
290aeed3e9SJustin Hibbits  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
300aeed3e9SJustin Hibbits  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
310aeed3e9SJustin Hibbits  */
320aeed3e9SJustin Hibbits 
33852ba100SJustin Hibbits 
340aeed3e9SJustin Hibbits /******************************************************************************
350aeed3e9SJustin Hibbits  @File          FM_muram.c
360aeed3e9SJustin Hibbits 
370aeed3e9SJustin Hibbits  @Description   FM MURAM ...
380aeed3e9SJustin Hibbits *//***************************************************************************/
390aeed3e9SJustin Hibbits #include "error_ext.h"
400aeed3e9SJustin Hibbits #include "std_ext.h"
410aeed3e9SJustin Hibbits #include "mm_ext.h"
420aeed3e9SJustin Hibbits #include "string_ext.h"
430aeed3e9SJustin Hibbits #include "sprint_ext.h"
440aeed3e9SJustin Hibbits #include "fm_muram_ext.h"
450aeed3e9SJustin Hibbits #include "fm_common.h"
460aeed3e9SJustin Hibbits 
470aeed3e9SJustin Hibbits #define __ERR_MODULE__  MODULE_FM_MURAM
480aeed3e9SJustin Hibbits 
490aeed3e9SJustin Hibbits 
500aeed3e9SJustin Hibbits typedef struct
510aeed3e9SJustin Hibbits {
520aeed3e9SJustin Hibbits     t_Handle    h_Mem;
530aeed3e9SJustin Hibbits     uintptr_t   baseAddr;
540aeed3e9SJustin Hibbits     uint32_t    size;
550aeed3e9SJustin Hibbits } t_FmMuram;
560aeed3e9SJustin Hibbits 
570aeed3e9SJustin Hibbits 
FmMuramClear(t_Handle h_FmMuram)580aeed3e9SJustin Hibbits void FmMuramClear(t_Handle h_FmMuram)
590aeed3e9SJustin Hibbits {
600aeed3e9SJustin Hibbits     t_FmMuram   *p_FmMuram = ( t_FmMuram *)h_FmMuram;
610aeed3e9SJustin Hibbits 
620aeed3e9SJustin Hibbits     SANITY_CHECK_RETURN(h_FmMuram, E_INVALID_HANDLE);
630aeed3e9SJustin Hibbits     IOMemSet32(UINT_TO_PTR(p_FmMuram->baseAddr), 0, p_FmMuram->size);
640aeed3e9SJustin Hibbits }
650aeed3e9SJustin Hibbits 
660aeed3e9SJustin Hibbits 
FM_MURAM_ConfigAndInit(uintptr_t baseAddress,uint32_t size)670aeed3e9SJustin Hibbits t_Handle FM_MURAM_ConfigAndInit(uintptr_t baseAddress, uint32_t size)
680aeed3e9SJustin Hibbits {
690aeed3e9SJustin Hibbits     t_Handle    h_Mem;
700aeed3e9SJustin Hibbits     t_FmMuram   *p_FmMuram;
710aeed3e9SJustin Hibbits 
720aeed3e9SJustin Hibbits     if (!baseAddress)
730aeed3e9SJustin Hibbits     {
740aeed3e9SJustin Hibbits         REPORT_ERROR(MAJOR, E_INVALID_VALUE, ("baseAddress 0 is not supported"));
750aeed3e9SJustin Hibbits         return NULL;
760aeed3e9SJustin Hibbits     }
770aeed3e9SJustin Hibbits 
780aeed3e9SJustin Hibbits     if (baseAddress%4)
790aeed3e9SJustin Hibbits     {
800aeed3e9SJustin Hibbits         REPORT_ERROR(MAJOR, E_INVALID_VALUE, ("baseAddress not 4 bytes aligned!"));
810aeed3e9SJustin Hibbits         return NULL;
820aeed3e9SJustin Hibbits     }
830aeed3e9SJustin Hibbits 
840aeed3e9SJustin Hibbits     /* Allocate FM MURAM structure */
850aeed3e9SJustin Hibbits     p_FmMuram = (t_FmMuram *) XX_Malloc(sizeof(t_FmMuram));
860aeed3e9SJustin Hibbits     if (!p_FmMuram)
870aeed3e9SJustin Hibbits     {
880aeed3e9SJustin Hibbits         REPORT_ERROR(MAJOR, E_NO_MEMORY, ("FM MURAM driver structure"));
890aeed3e9SJustin Hibbits         return NULL;
900aeed3e9SJustin Hibbits     }
910aeed3e9SJustin Hibbits     memset(p_FmMuram, 0, sizeof(t_FmMuram));
920aeed3e9SJustin Hibbits 
930aeed3e9SJustin Hibbits 
940aeed3e9SJustin Hibbits     if ((MM_Init(&h_Mem, baseAddress, size) != E_OK) || (!h_Mem))
950aeed3e9SJustin Hibbits     {
960aeed3e9SJustin Hibbits         XX_Free(p_FmMuram);
970aeed3e9SJustin Hibbits         REPORT_ERROR(MAJOR, E_INVALID_HANDLE, ("FM-MURAM partition!!!"));
980aeed3e9SJustin Hibbits         return NULL;
990aeed3e9SJustin Hibbits     }
1000aeed3e9SJustin Hibbits 
1010aeed3e9SJustin Hibbits     /* Initialize FM MURAM parameters which will be kept by the driver */
1020aeed3e9SJustin Hibbits     p_FmMuram->baseAddr = baseAddress;
1030aeed3e9SJustin Hibbits     p_FmMuram->size = size;
1040aeed3e9SJustin Hibbits     p_FmMuram->h_Mem = h_Mem;
1050aeed3e9SJustin Hibbits 
1060aeed3e9SJustin Hibbits     return p_FmMuram;
1070aeed3e9SJustin Hibbits }
1080aeed3e9SJustin Hibbits 
FM_MURAM_Free(t_Handle h_FmMuram)1090aeed3e9SJustin Hibbits t_Error FM_MURAM_Free(t_Handle h_FmMuram)
1100aeed3e9SJustin Hibbits {
1110aeed3e9SJustin Hibbits     t_FmMuram   *p_FmMuram = ( t_FmMuram *)h_FmMuram;
1120aeed3e9SJustin Hibbits 
1130aeed3e9SJustin Hibbits     if (p_FmMuram->h_Mem)
1140aeed3e9SJustin Hibbits         MM_Free(p_FmMuram->h_Mem);
1150aeed3e9SJustin Hibbits 
1160aeed3e9SJustin Hibbits     XX_Free(h_FmMuram);
1170aeed3e9SJustin Hibbits 
1180aeed3e9SJustin Hibbits     return E_OK;
1190aeed3e9SJustin Hibbits }
1200aeed3e9SJustin Hibbits 
FM_MURAM_AllocMem(t_Handle h_FmMuram,uint32_t size,uint32_t align)1210aeed3e9SJustin Hibbits void  * FM_MURAM_AllocMem(t_Handle h_FmMuram, uint32_t size, uint32_t align)
1220aeed3e9SJustin Hibbits {
1230aeed3e9SJustin Hibbits     t_FmMuram   *p_FmMuram = ( t_FmMuram *)h_FmMuram;
1240aeed3e9SJustin Hibbits     uintptr_t   addr;
1250aeed3e9SJustin Hibbits 
1260aeed3e9SJustin Hibbits     SANITY_CHECK_RETURN_VALUE(h_FmMuram, E_INVALID_HANDLE, NULL);
1270aeed3e9SJustin Hibbits     SANITY_CHECK_RETURN_VALUE(p_FmMuram->h_Mem, E_INVALID_HANDLE, NULL);
1280aeed3e9SJustin Hibbits 
1290aeed3e9SJustin Hibbits     addr = (uintptr_t)MM_Get(p_FmMuram->h_Mem, size, align ,"FM MURAM");
1300aeed3e9SJustin Hibbits 
1310aeed3e9SJustin Hibbits     if (addr == ILLEGAL_BASE)
1320aeed3e9SJustin Hibbits         return NULL;
1330aeed3e9SJustin Hibbits 
1340aeed3e9SJustin Hibbits     return UINT_TO_PTR(addr);
1350aeed3e9SJustin Hibbits }
1360aeed3e9SJustin Hibbits 
FM_MURAM_AllocMemForce(t_Handle h_FmMuram,uint64_t base,uint32_t size)1370aeed3e9SJustin Hibbits void  * FM_MURAM_AllocMemForce(t_Handle h_FmMuram, uint64_t base, uint32_t size)
1380aeed3e9SJustin Hibbits {
1390aeed3e9SJustin Hibbits     t_FmMuram   *p_FmMuram = ( t_FmMuram *)h_FmMuram;
1400aeed3e9SJustin Hibbits     uintptr_t   addr;
1410aeed3e9SJustin Hibbits 
1420aeed3e9SJustin Hibbits     SANITY_CHECK_RETURN_VALUE(h_FmMuram, E_INVALID_HANDLE, NULL);
1430aeed3e9SJustin Hibbits     SANITY_CHECK_RETURN_VALUE(p_FmMuram->h_Mem, E_INVALID_HANDLE, NULL);
1440aeed3e9SJustin Hibbits 
1450aeed3e9SJustin Hibbits     addr = (uintptr_t)MM_GetForce(p_FmMuram->h_Mem, base, size, "FM MURAM");
1460aeed3e9SJustin Hibbits 
1470aeed3e9SJustin Hibbits     if (addr == ILLEGAL_BASE)
1480aeed3e9SJustin Hibbits         return NULL;
1490aeed3e9SJustin Hibbits 
1500aeed3e9SJustin Hibbits     return UINT_TO_PTR(addr);
1510aeed3e9SJustin Hibbits }
1520aeed3e9SJustin Hibbits 
FM_MURAM_FreeMem(t_Handle h_FmMuram,void * ptr)1530aeed3e9SJustin Hibbits t_Error FM_MURAM_FreeMem(t_Handle h_FmMuram, void *ptr)
1540aeed3e9SJustin Hibbits {
1550aeed3e9SJustin Hibbits     t_FmMuram   *p_FmMuram = ( t_FmMuram *)h_FmMuram;
1560aeed3e9SJustin Hibbits 
1570aeed3e9SJustin Hibbits     SANITY_CHECK_RETURN_ERROR(h_FmMuram, E_INVALID_HANDLE);
1580aeed3e9SJustin Hibbits     SANITY_CHECK_RETURN_ERROR(p_FmMuram->h_Mem, E_INVALID_HANDLE);
1590aeed3e9SJustin Hibbits 
1600aeed3e9SJustin Hibbits     if (MM_Put(p_FmMuram->h_Mem, PTR_TO_UINT(ptr)) == 0)
161852ba100SJustin Hibbits         RETURN_ERROR(MINOR, E_INVALID_ADDRESS, ("memory pointer!!!"));
1620aeed3e9SJustin Hibbits 
1630aeed3e9SJustin Hibbits     return E_OK;
1640aeed3e9SJustin Hibbits }
165852ba100SJustin Hibbits 
FM_MURAM_GetFreeMemSize(t_Handle h_FmMuram)166852ba100SJustin Hibbits uint64_t FM_MURAM_GetFreeMemSize(t_Handle h_FmMuram)
167852ba100SJustin Hibbits {
168852ba100SJustin Hibbits     t_FmMuram   *p_FmMuram = ( t_FmMuram *)h_FmMuram;
169852ba100SJustin Hibbits 
170852ba100SJustin Hibbits     SANITY_CHECK_RETURN_VALUE(h_FmMuram, E_INVALID_HANDLE, 0);
171852ba100SJustin Hibbits     SANITY_CHECK_RETURN_VALUE(p_FmMuram->h_Mem, E_INVALID_HANDLE, 0);
172852ba100SJustin Hibbits 
173852ba100SJustin Hibbits     return MM_GetFreeMemSize(p_FmMuram->h_Mem);
174852ba100SJustin Hibbits }
175