xref: /utopia/UTPA2-700.0.x/modules/cpu/drv/cpu/mdrvCPU.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 #include "string.h"
2 #include "MsTypes.h"
3 #include <stdio.h>
4 #include "utopia_dapi.h"
5 #include "drvCPU.h"
6 #include "drvCPU_private.h"
7 #include "MsOS.h"
8 #if (CPU_UTOPIA20)
9 #include "utopia.h"
10 #endif
11 
12 #include "ULog.h"
13 
14 #define TAG_CPU "CPU"
15 
16 enum
17 {
18     CPU_POOL_ID_CPU0=0
19 } eCPUPoolID;
20 
21 // this func will be called to init by utopia20 framework
CPURegisterToUtopia(FUtopiaOpen ModuleType)22 void CPURegisterToUtopia(FUtopiaOpen ModuleType)
23 {
24     // 1. deal with module
25     void* pUtopiaModule = NULL;
26 	UtopiaModuleCreate(MODULE_CPU, 8, &pUtopiaModule);
27     UtopiaModuleRegister(pUtopiaModule);
28     // register func for module, after register here, then ap call UtopiaOpen/UtopiaIoctl/UtopiaClose can call to these registered standard func
29     UtopiaModuleSetupFunctionPtr(pUtopiaModule, (FUtopiaOpen)CPUOpen, (FUtopiaClose)CPUClose, (FUtopiaIOctl)CPUIoctl);
30 
31     // 2. deal with resource
32     void* psResource = NULL;
33     // start func to add res, call once will create 2 access in resource. Also can declare CPU_POOL_ID_CPU1 for another channel depend on driver owner.
34 	UtopiaModuleAddResourceStart(pUtopiaModule, CPU_POOL_ID_CPU0);
35     // resource can alloc private for internal use, ex, CPU_RESOURCE_PRIVATE
36     UtopiaResourceCreate("cpu0", sizeof(CPU_RESOURCE_PRIVATE), &psResource);
37     // func to reg res
38     UtopiaResourceRegister(pUtopiaModule, psResource, CPU_POOL_ID_CPU0);
39     // end function to add res
40 	UtopiaModuleAddResourceEnd(pUtopiaModule, CPU_POOL_ID_CPU0);
41 }
42 
CPUOpen(void ** ppInstance,MS_U32 u32ModuleVersion,void * pAttribute)43 MS_U32 CPUOpen(void** ppInstance, MS_U32 u32ModuleVersion, void* pAttribute)
44 {
45     CPU_INSTANT_PRIVATE *psCpuInstPri = NULL;
46     void *pCpuInstPriVoid = NULL;
47 
48 	ULOGD(TAG_CPU, "\n[CPU INFO] cpu open");
49     //UTOPIA_TRACE(MS_UTOPIA_DB_LEVEL_TRACE,printf("enter %s %d\n",__FUNCTION__,__LINE__));
50     // instance is allocated here, also can allocate private for internal use, ex, CPU_INSTANT_PRIVATE
51     UtopiaInstanceCreate(sizeof(CPU_INSTANT_PRIVATE), ppInstance);
52     // setup func in private and assign the calling func in func ptr in instance private
53     UtopiaInstanceGetPrivate(*ppInstance, &pCpuInstPriVoid);
54     psCpuInstPri = (CPU_INSTANT_PRIVATE*)pCpuInstPriVoid;
55     psCpuInstPri->fpCPUInitFront = (IOCTL_CPU_INITFRONT)_MDrv_CPU_InitFront;
56     psCpuInstPri->fpCPUInitEnd = (IOCTL_CPU_INITEND)_MDrv_CPU_InitEnd;
57     psCpuInstPri->fpCPUDisable = (IOCTL_CPU_DISABLE)_MDrv_CPU_Disable;
58     psCpuInstPri->fpCPUEnable = (IOCTL_CPU_ENABLE)_MDrv_CPU_Enable;
59     psCpuInstPri->fpCPUSetHost = (IOCTL_CPU_SETHOST)_MDrv_CPU_SetHost;
60     psCpuInstPri->fpCPUGetInfo = (IOCTL_CPU_GETINFO)_MDrv_CPU_GetInfo;
61     psCpuInstPri->fpCPUGetLibVer = (IOCTL_CPU_GETLIBVER)_MDrv_CPU_GetLibVer;
62     psCpuInstPri->fpCPUGetStatus = (IOCTL_CPU_GETSTATUS)_MDrv_CPU_GetStatus;
63     psCpuInstPri->fpCPUSetDbgLevel = (IOCTL_CPU_SETDBGLEVEL)_MDrv_CPU_SetDbgLevel;
64     psCpuInstPri->fpCPUGetBase = (IOCTL_CPU_GETBASE)_MDrv_CPU_GetBase;
65     psCpuInstPri->fpCPUQueryClock = (IOCTL_CPU_QUERYCLOCK)_MDrv_CPU_QueryClock;
66 
67 	_MDrv_CPU_GetBase();
68 
69 	return UTOPIA_STATUS_SUCCESS;
70 }
71 
72 // FIXME: why static?
CPUIoctl(void * pInstance,MS_U32 u32Cmd,void * pArgs)73 MS_U32 CPUIoctl(void* pInstance, MS_U32 u32Cmd, void* pArgs)
74 {
75 	void* pModule = NULL;
76 	void* pResource = NULL;
77     CPU_INSTANT_PRIVATE* psCpuInstPri = NULL;
78     void* pCpuInstPriVoid = NULL;
79 
80     PCPU_ENABLE_PARAM pEnableParam = NULL;
81     PCPU_SETHOST_PARAM pSetHostParam = NULL;
82     PCPU_INITEND_PARAM pInitEndParam = NULL;
83     PCPU_GETLIBVER_PARAM pGetLibVerParam = NULL;
84     PCPU_GETSTATUS_PARAM pGetStatusParam = NULL;
85     PCPU_SETDBGLEVEL_PARAM pSetDbgLevelParam = NULL;
86     PCPU_GETINFO_PARAM pGetInfoParam = NULL;
87     COPRO_DrvInfo* pCPUInfo = NULL;
88     COPRO_Result eCPURet=E_COPRO_FAIL;
89 
90 	UtopiaInstanceGetModule(pInstance, &pModule);
91     if(pModule==(void*)NULL)
92     {
93         ULOGE(TAG_CPU, "UtopiaInstanceGetModule fail\n");
94         return UTOPIA_STATUS_FAIL;
95     }
96 
97 	UtopiaInstanceGetPrivate(pInstance, &pCpuInstPriVoid);
98     psCpuInstPri = (CPU_INSTANT_PRIVATE*)pCpuInstPriVoid;
99     if(psCpuInstPri==(CPU_INSTANT_PRIVATE*)NULL)
100     {
101         ULOGE(TAG_CPU,"UtopiaInstanceGetPrivate fail\n");
102         return UTOPIA_STATUS_FAIL;
103     }
104 
105     // when calling func occpuy h/w, need to call UtopiaResourceObtainToInstant to obtain res to lock other process
106     switch(u32Cmd)
107     {
108         case MDrv_CMD_CPU_InitFront:
109             ULOGD(TAG_CPU, "CPUIoctl - MDrv_CMD_CPU_InitFront\n");
110             if(UtopiaResourceObtain(pModule, CPU_POOL_ID_CPU0, &pResource) != 0)
111             {
112                 ULOGE(TAG_CPU, "UtopiaResourceObtainToInstant fail\n");
113                 return UTOPIA_STATUS_ERR_RESOURCE;
114             }
115             psCpuInstPri->fpCPUInitFront();
116             UtopiaResourceRelease(pResource);
117             return UTOPIA_STATUS_SUCCESS;
118 
119         case MDrv_CMD_CPU_InitEnd:
120             ULOGD(TAG_CPU, "CPUIoctl - MDrv_CMD_CPU_InitEnd\n");
121             if(UtopiaResourceObtain(pModule, CPU_POOL_ID_CPU0, &pResource) != 0)
122             {
123                 ULOGE(TAG_CPU, "UtopiaResourceObtainToInstant fail\n");
124                 return UTOPIA_STATUS_ERR_RESOURCE;
125             }
126             pInitEndParam = (PCPU_INITEND_PARAM)pArgs;
127             if(pInitEndParam==(PCPU_INITEND_PARAM)NULL)
128             {
129                 return UTOPIA_STATUS_PARAMETER_ERROR;
130             }
131             psCpuInstPri->fpCPUInitEnd(pInitEndParam->u32_ADR);
132             UtopiaResourceRelease(pResource);
133             return UTOPIA_STATUS_SUCCESS;
134 
135         case MDrv_CMD_CPU_Disable:
136             ULOGD(TAG_CPU, "CPUIoctl - MDrv_CMD_CPU_Disable\n");
137             if(UtopiaResourceObtain(pModule, CPU_POOL_ID_CPU0, &pResource) != 0)
138             {
139                 ULOGE(TAG_CPU, "UtopiaResourceObtainToInstant fail\n");
140 	            return UTOPIA_STATUS_ERR_RESOURCE;
141             }
142             psCpuInstPri->fpCPUDisable();
143             UtopiaResourceRelease(pResource);
144             return UTOPIA_STATUS_SUCCESS;
145 
146         case MDrv_CMD_CPU_Enable:
147             ULOGD(TAG_CPU, "CPUIoctl - MDrv_CMD_CPU_Enable\n");
148             if(UtopiaResourceObtain(pModule, CPU_POOL_ID_CPU0, &pResource) != 0)
149             {
150                 ULOGE(TAG_CPU, "UtopiaResourceObtainToInstant fail\n");
151                 return UTOPIA_STATUS_ERR_RESOURCE;
152             }
153             pEnableParam = (PCPU_ENABLE_PARAM)pArgs;
154             if(pEnableParam==(PCPU_ENABLE_PARAM)NULL)
155             {
156                 return UTOPIA_STATUS_PARAMETER_ERROR;
157             }
158             psCpuInstPri->fpCPUEnable(pEnableParam->u32_ADR);
159             UtopiaResourceRelease(pResource);
160             return UTOPIA_STATUS_SUCCESS;
161 
162         case MDrv_CMD_CPU_SetHost:
163             ULOGD(TAG_CPU, "CPUIoctl - MDrv_CMD_CPU_SetHost\n");
164             if(UtopiaResourceObtain(pModule, CPU_POOL_ID_CPU0, &pResource) != 0)
165             {
166                 ULOGE(TAG_CPU, "UtopiaResourceObtainToInstant fail\n");
167                 return UTOPIA_STATUS_ERR_RESOURCE;
168             }
169             pSetHostParam = (PCPU_SETHOST_PARAM)pArgs;
170             if(pSetHostParam==(PCPU_SETHOST_PARAM)NULL)
171             {
172                 return UTOPIA_STATUS_PARAMETER_ERROR;
173             }
174             psCpuInstPri->fpCPUSetHost(pSetHostParam->bHost);
175             UtopiaResourceRelease(pResource);
176             return UTOPIA_STATUS_SUCCESS;
177 
178         case MDrv_CMD_CPU_GetInfo:
179             ULOGD(TAG_CPU, "CPUIoctl - MDrv_CMD_CPU_GetInfo\n");
180             pGetInfoParam = (PCPU_GETINFO_PARAM)pArgs;
181             if(pGetInfoParam==(PCPU_GETINFO_PARAM)NULL)
182             {
183                 return UTOPIA_STATUS_PARAMETER_ERROR;
184             }
185             pCPUInfo = (COPRO_DrvInfo*)psCpuInstPri->fpCPUGetInfo();
186             if(pCPUInfo==(COPRO_DrvInfo*)NULL)
187             {
188                 return UTOPIA_STATUS_FAIL;
189             }
190             memcpy((void*)pGetInfoParam,(void*)pCPUInfo,sizeof(COPRO_DrvInfo));
191             return UTOPIA_STATUS_SUCCESS;
192 
193         case MDrv_CMD_CPU_GetLibVer:
194             ULOGD(TAG_CPU, "CPUIoctl - MDrv_CMD_CPU_GetLibVer\n");
195             // this function won't occupy h/w, so don't need to call UtopiaResourceObtainToInstant
196             pGetLibVerParam = (PCPU_GETLIBVER_PARAM)pArgs;
197             if(pGetLibVerParam==(PCPU_GETLIBVER_PARAM)NULL)
198             {
199                 return UTOPIA_STATUS_PARAMETER_ERROR;
200             }
201             eCPURet = psCpuInstPri->fpCPUGetLibVer(pGetLibVerParam->ppVersion);
202             if (eCPURet == E_COPRO_FAIL)
203             {
204                 return UTOPIA_STATUS_FAIL;
205             }
206             return UTOPIA_STATUS_SUCCESS;
207 
208         case MDrv_CMD_CPU_GetStatus:
209             ULOGD(TAG_CPU, "CPUIoctl - MDrv_CMD_CPU_GetStatus\n");
210             // this function won't occupy h/w, so don't need to call UtopiaResourceObtainToInstant
211             pGetStatusParam = (PCPU_GETSTATUS_PARAM)pArgs;
212             if(pGetStatusParam==(PCPU_GETSTATUS_PARAM)NULL)
213             {
214                 return UTOPIA_STATUS_PARAMETER_ERROR;
215             }
216             eCPURet = psCpuInstPri->fpCPUGetStatus(pGetStatusParam->pStatus);
217             if (eCPURet == E_COPRO_FAIL)
218             {
219                 return UTOPIA_STATUS_FAIL;
220             }
221             return UTOPIA_STATUS_SUCCESS;
222 
223         case MDrv_CMD_CPU_SetDbgLevel:
224             ULOGD(TAG_CPU, "CPUIoctl - MDrv_CMD_CPU_SetDbgLevel\n");
225             // this function won't occupy h/w, so don't need to call UtopiaResourceObtainToInstant
226             pSetDbgLevelParam = (PCPU_SETDBGLEVEL_PARAM)pArgs;
227             if(pSetDbgLevelParam==(PCPU_SETDBGLEVEL_PARAM)NULL)
228             {
229                 return UTOPIA_STATUS_PARAMETER_ERROR;
230             }
231             psCpuInstPri->fpCPUSetDbgLevel(pSetDbgLevelParam->u8Level);
232             return UTOPIA_STATUS_SUCCESS;
233 
234         case MDrv_CMD_CPU_GetBase:
235             ULOGD(TAG_CPU, "CPUIoctl - MDrv_CMD_CPU_GetBase\n");
236             if(UtopiaResourceObtain(pModule, CPU_POOL_ID_CPU0, &pResource) != 0)
237             {
238                 ULOGE(TAG_CPU, "UtopiaResourceObtainToInstant fail\n");
239                 return UTOPIA_STATUS_ERR_RESOURCE;
240             }
241             if(pArgs==(void*)NULL)
242             {
243                 return UTOPIA_STATUS_PARAMETER_ERROR;
244             }
245             *((MS_U32*)pArgs) = psCpuInstPri->fpCPUGetBase();
246             UtopiaResourceRelease(pResource);
247             return UTOPIA_STATUS_SUCCESS;
248 
249         case MDrv_CMD_CPU_QueryClock:
250             ULOGD(TAG_CPU, "CPUIoctl - MDrv_CMD_CPU_QueryClock\n");
251             if(UtopiaResourceObtain(pModule, CPU_POOL_ID_CPU0, &pResource) != 0)
252             {
253                 ULOGE(TAG_CPU, "UtopiaResourceObtainToInstant fail\n");
254                 return UTOPIA_STATUS_ERR_RESOURCE;
255             }
256             if(pArgs==(void*)NULL)
257             {
258                 return UTOPIA_STATUS_PARAMETER_ERROR;
259             }
260             *((MS_U32*)pArgs) = psCpuInstPri->fpCPUQueryClock();
261             UtopiaResourceRelease(pResource);
262             return UTOPIA_STATUS_SUCCESS;
263 
264         default:
265             return UTOPIA_STATUS_ERR_UNIMPLEM;
266     };
267 
268 	return UTOPIA_STATUS_SUCCESS;
269 }
270 
CPUClose(void * pInstance)271 MS_U32 CPUClose(void* pInstance)
272 {
273 	UtopiaInstanceDelete(pInstance);
274 
275 	return UTOPIA_STATUS_SUCCESS;
276 }
277 
278 
279 
280