xref: /freebsd/sys/contrib/ncsw/Peripherals/FM/fm_muram.c (revision 4928135658a9d0eaee37003df6137ab363fcb0b4)
1 /*
2  * Copyright 2008-2012 Freescale Semiconductor Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above copyright
9  *       notice, this list of conditions and the following disclaimer in the
10  *       documentation and/or other materials provided with the distribution.
11  *     * Neither the name of Freescale Semiconductor nor the
12  *       names of its contributors may be used to endorse or promote products
13  *       derived from this software without specific prior written permission.
14  *
15  *
16  * ALTERNATIVELY, this software may be distributed under the terms of the
17  * GNU General Public License ("GPL") as published by the Free Software
18  * Foundation, either version 2 of that License or (at your option) any
19  * later version.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 
34 /******************************************************************************
35  @File          FM_muram.c
36 
37  @Description   FM MURAM ...
38 *//***************************************************************************/
39 #include "error_ext.h"
40 #include "std_ext.h"
41 #include "mm_ext.h"
42 #include "string_ext.h"
43 #include "sprint_ext.h"
44 #include "fm_muram_ext.h"
45 #include "fm_common.h"
46 
47 #define __ERR_MODULE__  MODULE_FM_MURAM
48 
49 
50 typedef struct
51 {
52     t_Handle    h_Mem;
53     uintptr_t   baseAddr;
54     uint32_t    size;
55 } t_FmMuram;
56 
57 
58 void FmMuramClear(t_Handle h_FmMuram)
59 {
60     t_FmMuram   *p_FmMuram = ( t_FmMuram *)h_FmMuram;
61 
62     SANITY_CHECK_RETURN(h_FmMuram, E_INVALID_HANDLE);
63     IOMemSet32(UINT_TO_PTR(p_FmMuram->baseAddr), 0, p_FmMuram->size);
64 }
65 
66 
67 t_Handle FM_MURAM_ConfigAndInit(uintptr_t baseAddress, uint32_t size)
68 {
69     t_Handle    h_Mem;
70     t_FmMuram   *p_FmMuram;
71 
72     if (!baseAddress)
73     {
74         REPORT_ERROR(MAJOR, E_INVALID_VALUE, ("baseAddress 0 is not supported"));
75         return NULL;
76     }
77 
78     if (baseAddress%4)
79     {
80         REPORT_ERROR(MAJOR, E_INVALID_VALUE, ("baseAddress not 4 bytes aligned!"));
81         return NULL;
82     }
83 
84     /* Allocate FM MURAM structure */
85     p_FmMuram = (t_FmMuram *) XX_Malloc(sizeof(t_FmMuram));
86     if (!p_FmMuram)
87     {
88         REPORT_ERROR(MAJOR, E_NO_MEMORY, ("FM MURAM driver structure"));
89         return NULL;
90     }
91     memset(p_FmMuram, 0, sizeof(t_FmMuram));
92 
93 
94     if ((MM_Init(&h_Mem, baseAddress, size) != E_OK) || (!h_Mem))
95     {
96         XX_Free(p_FmMuram);
97         REPORT_ERROR(MAJOR, E_INVALID_HANDLE, ("FM-MURAM partition!!!"));
98         return NULL;
99     }
100 
101     /* Initialize FM MURAM parameters which will be kept by the driver */
102     p_FmMuram->baseAddr = baseAddress;
103     p_FmMuram->size = size;
104     p_FmMuram->h_Mem = h_Mem;
105 
106     return p_FmMuram;
107 }
108 
109 t_Error FM_MURAM_Free(t_Handle h_FmMuram)
110 {
111     t_FmMuram   *p_FmMuram = ( t_FmMuram *)h_FmMuram;
112 
113     if (p_FmMuram->h_Mem)
114         MM_Free(p_FmMuram->h_Mem);
115 
116     XX_Free(h_FmMuram);
117 
118     return E_OK;
119 }
120 
121 void  * FM_MURAM_AllocMem(t_Handle h_FmMuram, uint32_t size, uint32_t align)
122 {
123     t_FmMuram   *p_FmMuram = ( t_FmMuram *)h_FmMuram;
124     uintptr_t   addr;
125 
126     SANITY_CHECK_RETURN_VALUE(h_FmMuram, E_INVALID_HANDLE, NULL);
127     SANITY_CHECK_RETURN_VALUE(p_FmMuram->h_Mem, E_INVALID_HANDLE, NULL);
128 
129     addr = (uintptr_t)MM_Get(p_FmMuram->h_Mem, size, align ,"FM MURAM");
130 
131     if (addr == ILLEGAL_BASE)
132         return NULL;
133 
134     return UINT_TO_PTR(addr);
135 }
136 
137 void  * FM_MURAM_AllocMemForce(t_Handle h_FmMuram, uint64_t base, uint32_t size)
138 {
139     t_FmMuram   *p_FmMuram = ( t_FmMuram *)h_FmMuram;
140     uintptr_t   addr;
141 
142     SANITY_CHECK_RETURN_VALUE(h_FmMuram, E_INVALID_HANDLE, NULL);
143     SANITY_CHECK_RETURN_VALUE(p_FmMuram->h_Mem, E_INVALID_HANDLE, NULL);
144 
145     addr = (uintptr_t)MM_GetForce(p_FmMuram->h_Mem, base, size, "FM MURAM");
146 
147     if (addr == ILLEGAL_BASE)
148         return NULL;
149 
150     return UINT_TO_PTR(addr);
151 }
152 
153 t_Error FM_MURAM_FreeMem(t_Handle h_FmMuram, void *ptr)
154 {
155     t_FmMuram   *p_FmMuram = ( t_FmMuram *)h_FmMuram;
156 
157     SANITY_CHECK_RETURN_ERROR(h_FmMuram, E_INVALID_HANDLE);
158     SANITY_CHECK_RETURN_ERROR(p_FmMuram->h_Mem, E_INVALID_HANDLE);
159 
160     if (MM_Put(p_FmMuram->h_Mem, PTR_TO_UINT(ptr)) == 0)
161         RETURN_ERROR(MINOR, E_INVALID_ADDRESS, ("memory pointer!!!"));
162 
163     return E_OK;
164 }
165 
166 uint64_t FM_MURAM_GetFreeMemSize(t_Handle h_FmMuram)
167 {
168     t_FmMuram   *p_FmMuram = ( t_FmMuram *)h_FmMuram;
169 
170     SANITY_CHECK_RETURN_VALUE(h_FmMuram, E_INVALID_HANDLE, 0);
171     SANITY_CHECK_RETURN_VALUE(p_FmMuram->h_Mem, E_INVALID_HANDLE, 0);
172 
173     return MM_GetFreeMemSize(p_FmMuram->h_Mem);
174 }
175