- head deinitialization removed from sdio_Deintialize. Only close the file descriptor.
[libogc.git] / libogc / stm.c
blob9fc3334b9cea398c61b794eb74185e8a69346110
1 /*-------------------------------------------------------------
3 stm.c - System and miscellaneous hardware control functions
5 Copyright (C) 2008
6 Michael Wiedenbauer (shagkur)
7 Dave Murphy (WinterMute)
8 Hector Martin (marcan)
10 This software is provided 'as-is', without any express or implied
11 warranty. In no event will the authors be held liable for any
12 damages arising from the use of this software.
14 Permission is granted to anyone to use this software for any
15 purpose, including commercial applications, and to alter it and
16 redistribute it freely, subject to the following restrictions:
18 1. The origin of this software must not be misrepresented; you
19 must not claim that you wrote the original software. If you use
20 this software in a product, an acknowledgment in the product
21 documentation would be appreciated but is not required.
23 2. Altered source versions must be plainly marked as such, and
24 must not be misrepresented as being the original software.
26 3. This notice may not be removed or altered from any source
27 distribution.
29 -------------------------------------------------------------*/
31 #if defined(HW_RVL)
33 #include <stdio.h>
34 #include "ipc.h"
35 #include "system.h"
36 #include "asm.h"
37 #include "processor.h"
38 #include "stm.h"
40 //#define DEBUG_STM
42 #define IOCTL_STM_EVENTHOOK 0x1000
43 #define IOCTL_STM_GET_IDLEMODE 0x3001
44 #define IOCTL_STM_RELEASE_EH 0x3002
45 #define IOCTL_STM_HOTRESET 0x2001
46 #define IOCTL_STM_HOTRESET_FOR_PD 0x2002
47 #define IOCTL_STM_SHUTDOWN 0x2003
48 #define IOCTL_STM_IDLE 0x2004
49 #define IOCTL_STM_WAKEUP 0x2005
50 #define IOCTL_STM_VIDIMMING 0x5001
51 #define IOCTL_STM_LEDFLASH 0x6001
52 #define IOCTL_STM_LEDMODE 0x6002
53 #define IOCTL_STM_READVER 0x7001
54 #define IOCTL_STM_READDDRREG 0x4001
55 #define IOCTL_STM_READDDRREG2 0x4002
57 static s32 __stm_eh_fd = -1;
58 static s32 __stm_imm_fd = -1;
59 static u32 __stm_vdinuse = 0;
60 static u32 __stm_initialized= 0;
61 static u32 __stm_ehregistered= 0;
62 static u32 __stm_ehclear= 0;
64 static u32 __stm_ehbufin[0x08] ATTRIBUTE_ALIGN(32) = {0,0,0,0,0,0,0,0};
65 static u32 __stm_ehbufout[0x08] ATTRIBUTE_ALIGN(32) = {0,0,0,0,0,0,0,0};
66 static u32 __stm_immbufin[0x08] ATTRIBUTE_ALIGN(32) = {0,0,0,0,0,0,0,0};
67 static u32 __stm_immbufout[0x08] ATTRIBUTE_ALIGN(32) = {0,0,0,0,0,0,0,0};
69 static char __stm_eh_fs[] ATTRIBUTE_ALIGN(32) = "/dev/stm/eventhook";
70 static char __stm_imm_fs[] ATTRIBUTE_ALIGN(32) = "/dev/stm/immediate";
72 s32 __STM_SetEventHook();
73 s32 __STM_ReleaseEventHook();
74 static s32 __STMEventHandler(s32 result,void *usrdata);
76 stmcallback __stm_eventcb = NULL;
78 static vu16* const _viReg = (u16*)0xCC002000;
80 s32 __STM_Init()
82 if(__stm_initialized==1) return 1;
83 #ifdef DEBUG_STM
84 printf("STM Init\n");
85 #endif
87 __stm_vdinuse = 0;
88 __stm_imm_fd = IOS_Open(__stm_imm_fs,0);
89 if(__stm_imm_fd<0) return 0;
91 __stm_eh_fd = IOS_Open(__stm_eh_fs,0);
92 if(__stm_eh_fd<0) return 0;
94 #ifdef DEBUG_STM
95 printf("STM FDs: %d, %d\n",__stm_imm_fd, __stm_eh_fd);
96 #endif
98 __stm_initialized = 1;
99 __STM_SetEventHook();
100 return 1;
103 s32 __STM_Close()
105 s32 res;
106 s32 ret = 0;
107 __STM_ReleaseEventHook();
109 if(__stm_imm_fd >= 0) {
110 res = IOS_Close(__stm_imm_fd);
111 if(res < 0) ret = res;
112 __stm_imm_fd = -1;
114 if(__stm_eh_fd >= 0) {
115 res = IOS_Close(__stm_eh_fd);
116 if(res < 0) ret = res;
117 __stm_eh_fd = -1;
119 __stm_initialized = 0;
120 return ret;
123 s32 __STM_SetEventHook()
125 s32 ret;
126 u32 level;
128 if(__stm_initialized==0) return STM_ENOTINIT;
130 __stm_ehclear = 0;
132 _CPU_ISR_Disable(level);
133 ret = IOS_IoctlAsync(__stm_eh_fd,IOCTL_STM_EVENTHOOK,__stm_ehbufin,0x20,__stm_ehbufout,0x20,__STMEventHandler,NULL);
134 if(ret<0) __stm_ehregistered = 0;
135 else __stm_ehregistered = 1;
136 _CPU_ISR_Restore(level);
138 return ret;
141 s32 __STM_ReleaseEventHook()
143 s32 ret;
145 if(__stm_initialized==0) return STM_ENOTINIT;
146 if(__stm_ehregistered==0) return STM_ENOHANDLER;
148 __stm_ehclear = 1;
150 ret = IOS_Ioctl(__stm_imm_fd,IOCTL_STM_RELEASE_EH,__stm_immbufin,0x20,__stm_immbufout,0x20);
151 if(ret>=0) __stm_ehregistered = 0;
153 return ret;
156 static s32 __STMEventHandler(s32 result,void *usrdata)
158 __stm_ehregistered = 0;
160 if(result < 0) { // shouldn't happen
161 return result;
164 #ifdef DEBUG_STM
165 printf("STM Event: %08x\n",__stm_ehbufout[0]);
166 #endif
168 if(__stm_ehclear) { //release
169 return 0;
172 if(__stm_eventcb) {
173 __stm_eventcb(__stm_ehbufout[0]);
176 __STM_SetEventHook();
178 return 0;
181 stmcallback STM_RegisterEventHandler(stmcallback newhandler)
183 stmcallback old;
184 old = __stm_eventcb;
185 __stm_eventcb = newhandler;
186 return old;
189 s32 STM_ShutdownToStandby()
191 int res;
193 _viReg[1] = 0;
194 if(__stm_initialized==0) {
195 #ifdef DEBUG_STM
196 printf("STM notinited\n");
197 #endif
198 return STM_ENOTINIT;
200 __stm_immbufin[0] = 0;
201 res= IOS_Ioctl(__stm_imm_fd,IOCTL_STM_SHUTDOWN,__stm_immbufin,0x20,__stm_immbufout,0x20);
202 if(res<0) {
203 #ifdef DEBUG_STM
204 printf("STM STBY failed: %d\n",res);
205 #endif
207 return res;
210 s32 STM_ShutdownToIdle()
212 int res;
214 _viReg[1] = 0;
215 if(__stm_initialized==0) {
216 #ifdef DEBUG_STM
217 printf("STM notinited\n");
218 #endif
219 return STM_ENOTINIT;
221 switch(SYS_GetHollywoodRevision()) {
222 case 0:
223 case 1:
224 case 2:
225 __stm_immbufin[0] = 0xFCA08280;
226 default:
227 __stm_immbufin[0] = 0xFCE082C0;
229 res= IOS_Ioctl(__stm_imm_fd,IOCTL_STM_IDLE,__stm_immbufin,0x20,__stm_immbufout,0x20);
230 if(res<0) {
231 #ifdef DEBUG_STM
232 printf("STM IDLE failed: %d\n",res);
233 #endif
235 return res;
238 s32 STM_SetLedMode(u32 mode)
240 int res;
241 if(__stm_initialized==0) {
242 #ifdef DEBUG_STM
243 printf("STM notinited\n");
244 #endif
245 return STM_ENOTINIT;
247 __stm_immbufin[0] = mode;
248 res= IOS_Ioctl(__stm_imm_fd,IOCTL_STM_LEDMODE,__stm_immbufin,0x20,__stm_immbufout,0x20);
249 if(res<0) {
250 #ifdef DEBUG_STM
251 printf("STM LEDMODE failed: %d\n",res);
252 #endif
254 return res;
257 s32 STM_RebootSystem()
259 int res;
261 _viReg[1] = 0;
262 if(__stm_initialized==0) {
263 #ifdef DEBUG_STM
264 printf("STM notinited\n");
265 #endif
266 return STM_ENOTINIT;
268 __stm_immbufin[0] = 0;
269 res= IOS_Ioctl(__stm_imm_fd,IOCTL_STM_HOTRESET,__stm_immbufin,0x20,__stm_immbufout,0x20);
270 if(res<0) {
271 #ifdef DEBUG_STM
272 printf("STM HRST failed: %d\n",res);
273 #endif
275 return res;
280 #endif /* defined(HW_RVL) */