1 /*-------------------------------------------------------------
3 stm.c - System and miscellaneous hardware control functions
6 Michael Wiedenbauer (shagkur)
7 Dave Murphy (WinterMute)
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
29 -------------------------------------------------------------*/
37 #include "processor.h"
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;
82 if(__stm_initialized
==1) return 1;
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;
95 printf("STM FDs: %d, %d\n",__stm_imm_fd
, __stm_eh_fd
);
98 __stm_initialized
= 1;
107 __STM_ReleaseEventHook();
109 if(__stm_imm_fd
>= 0) {
110 res
= IOS_Close(__stm_imm_fd
);
111 if(res
< 0) ret
= res
;
114 if(__stm_eh_fd
>= 0) {
115 res
= IOS_Close(__stm_eh_fd
);
116 if(res
< 0) ret
= res
;
119 __stm_initialized
= 0;
123 s32
__STM_SetEventHook()
128 if(__stm_initialized
==0) return STM_ENOTINIT
;
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
);
141 s32
__STM_ReleaseEventHook()
145 if(__stm_initialized
==0) return STM_ENOTINIT
;
146 if(__stm_ehregistered
==0) return STM_ENOHANDLER
;
150 ret
= IOS_Ioctl(__stm_imm_fd
,IOCTL_STM_RELEASE_EH
,__stm_immbufin
,0x20,__stm_immbufout
,0x20);
151 if(ret
>=0) __stm_ehregistered
= 0;
156 static s32
__STMEventHandler(s32 result
,void *usrdata
)
158 __stm_ehregistered
= 0;
160 if(result
< 0) { // shouldn't happen
165 printf("STM Event: %08x\n",__stm_ehbufout
[0]);
168 if(__stm_ehclear
) { //release
173 __stm_eventcb(__stm_ehbufout
[0]);
176 __STM_SetEventHook();
181 stmcallback
STM_RegisterEventHandler(stmcallback newhandler
)
185 __stm_eventcb
= newhandler
;
189 s32
STM_ShutdownToStandby()
194 if(__stm_initialized
==0) {
196 printf("STM notinited\n");
200 __stm_immbufin
[0] = 0;
201 res
= IOS_Ioctl(__stm_imm_fd
,IOCTL_STM_SHUTDOWN
,__stm_immbufin
,0x20,__stm_immbufout
,0x20);
204 printf("STM STBY failed: %d\n",res
);
210 s32
STM_ShutdownToIdle()
215 if(__stm_initialized
==0) {
217 printf("STM notinited\n");
221 switch(SYS_GetHollywoodRevision()) {
225 __stm_immbufin
[0] = 0xFCA08280;
227 __stm_immbufin
[0] = 0xFCE082C0;
229 res
= IOS_Ioctl(__stm_imm_fd
,IOCTL_STM_IDLE
,__stm_immbufin
,0x20,__stm_immbufout
,0x20);
232 printf("STM IDLE failed: %d\n",res
);
238 s32
STM_SetLedMode(u32 mode
)
241 if(__stm_initialized
==0) {
243 printf("STM notinited\n");
247 __stm_immbufin
[0] = mode
;
248 res
= IOS_Ioctl(__stm_imm_fd
,IOCTL_STM_LEDMODE
,__stm_immbufin
,0x20,__stm_immbufout
,0x20);
251 printf("STM LEDMODE failed: %d\n",res
);
257 s32
STM_RebootSystem()
262 if(__stm_initialized
==0) {
264 printf("STM notinited\n");
268 __stm_immbufin
[0] = 0;
269 res
= IOS_Ioctl(__stm_imm_fd
,IOCTL_STM_HOTRESET
,__stm_immbufin
,0x20,__stm_immbufout
,0x20);
272 printf("STM HRST failed: %d\n",res
);
280 #endif /* defined(HW_RVL) */