11#include "irx_imports.h"
17IRX_ID(
"S147CTRL", 2, 8);
23 int g_watchdog_started;
33static void setup_ac_delay_regs(
void);
34static int setup_ctrl_ioman_drv(
const char *devpfx,
const char *devname);
35static unsigned int watchdog_alarm_cb(
void *userdata);
36static int ctrl_drv_op_open(
const iop_file_t *f,
const char *name,
int flags);
38static int ctrl_drv_op_read(
const iop_file_t *f,
void *ptr,
int size);
39static int ctrl_drv_op_write(
const iop_file_t *f,
void *ptr,
int size);
40static int create_ctrl_sema(
void);
41static int ctrl_do_rtc_read(u32 *rtcbuf);
42static int ctrl_do_rtc_read_inner(
int flgcnt,
int flgmsk);
43static int ctrl_do_rtc_write(
const u32 *rtcbuf);
44static void ctrl_do_rtc_write_inner(
int inflg,
int flgcnt,
int flgmsk);
45static int setup_sram_ioman_drv(
const char *devpfx,
const char *devname);
46static int sram_drv_op_open(
iop_file_t *f,
const char *name,
int flags);
48static int sram_drv_op_read(
iop_file_t *f,
void *ptr,
int size);
49static int sram_drv_op_write(
iop_file_t *f,
void *ptr,
int size);
50static int sram_drv_op_lseek(
iop_file_t *f,
int offset,
int mode);
51static int do_rpc_start1(
void);
52static void rpc_thread1(
void *userdata);
53static void *rpc_1470000_handler(
int fno,
void *buffer,
int length);
54static void *rpc_1470001_handler(
int fno,
void *buffer,
int length);
55static void *rpc_1470002_handler(
int fno,
void *buffer,
int length);
56static void *rpc_1470003_handler(
int fno,
void *buffer,
int length);
57static int do_rpc_start2(
void);
58static void rpc_thread2(
void *userdata);
59static void *rpc_1470200_handler(
int fno,
void *buffer,
int length);
60static void *rpc_1470201_handler(
int fno,
void *buffer,
int length);
63IOMAN_RETURN_VALUE_IMPL(0);
66 IOMAN_RETURN_VALUE(0),
67 IOMAN_RETURN_VALUE(0),
68 IOMAN_RETURN_VALUE(0),
69 (
void *)&ctrl_drv_op_open,
70 (
void *)&ctrl_drv_op_close,
71 (
void *)&ctrl_drv_op_read,
72 (
void *)&ctrl_drv_op_write,
73 IOMAN_RETURN_VALUE(0),
74 IOMAN_RETURN_VALUE(0),
75 IOMAN_RETURN_VALUE(0),
76 IOMAN_RETURN_VALUE(0),
77 IOMAN_RETURN_VALUE(0),
78 IOMAN_RETURN_VALUE(0),
79 IOMAN_RETURN_VALUE(0),
80 IOMAN_RETURN_VALUE(0),
81 IOMAN_RETURN_VALUE(0),
82 IOMAN_RETURN_VALUE(0),
85static int g_rpc_started;
87static int g_watchdog_count_1;
89static char g_watchdog_flag_1;
91static u32 g_max_timer_counter;
93 IOMAN_RETURN_VALUE(0),
94 IOMAN_RETURN_VALUE(0),
95 IOMAN_RETURN_VALUE(0),
96 (
void *)&sram_drv_op_open,
97 (
void *)&sram_drv_op_close,
98 (
void *)&sram_drv_op_read,
99 (
void *)&sram_drv_op_write,
100 (
void *)&sram_drv_op_lseek,
101 IOMAN_RETURN_VALUE(0),
102 IOMAN_RETURN_VALUE(0),
103 IOMAN_RETURN_VALUE(0),
104 IOMAN_RETURN_VALUE(0),
105 IOMAN_RETURN_VALUE(0),
106 IOMAN_RETURN_VALUE(0),
107 IOMAN_RETURN_VALUE(0),
108 IOMAN_RETURN_VALUE(0),
109 IOMAN_RETURN_VALUE(0),
112static int g_rtc_flag;
113static int g_timer_id;
115static int g_ctrl_sema_id;
117static int g_rpc1_buf[8];
118static int g_rpc2_buf[260];
121int _start(
int ac,
char **av)
126 Kprintf(
"s147ctrl.irx: System147 Control/SRAM Driver v%d.%d\n", 2, 8);
127 setup_ac_delay_regs();
128 if ( setup_ctrl_ioman_drv(
"ctrl",
"Ctrl") < 0 )
130 Kprintf(
"s147ctrl.irx: Ctrl initialize failed\n");
131 return MODULE_NO_RESIDENT_END;
134 if ( setup_sram_ioman_drv(
"sram",
"SRAM") < 0 )
136 Kprintf(
"s147ctrl.irx: Sram initialize failed\n");
137 return MODULE_NO_RESIDENT_END;
139 return MODULE_RESIDENT_END;
142static void setup_ac_delay_regs(
void)
144 SetAcMemDelayReg(0x261A2122);
145 SetAcIoDelayReg(0xA61A0166);
148static int setup_ctrl_ioman_drv(
const char *devpfx,
const char *devname)
150 g_watchdog_info.g_watchdog_started = 1;
151 USec2SysClock(0x4E20, &g_watchdog_info.g_watchdog_clock);
152 SetAlarm(&g_watchdog_info.g_watchdog_clock, watchdog_alarm_cb, &g_watchdog_info);
153 if ( create_ctrl_sema() < 0 )
155 g_drv_ctrl_ioman.name = devpfx;
156 g_drv_ctrl_ioman.type = IOP_DT_FS;
158 g_drv_ctrl_ioman.desc = devname;
159 g_drv_ctrl_ioman.ops = &g_ops_ctrl_ioman;
161 AddDrv(&g_drv_ctrl_ioman);
165static unsigned int watchdog_alarm_cb(
void *userdata)
170 USE_S147_DEV9_MEM_MMIO();
171 USE_S147LINK_DEV9_MEM_MMIO();
174 if ( wdi->g_watchdog_started != 1 )
176 s147_dev9_mem_mmio->m_led = 3;
180 s147link_dev9_mem_mmio->m_watchdog_flag_unk34 = 0;
181 unk34_tmp = s147link_dev9_mem_mmio->m_watchdog_flag_unk34;
183 if ( unk34_tmp == 0x3E )
185 s147_dev9_mem_mmio->m_watchdog_flag2 = 0;
187 s147_dev9_mem_mmio->m_led = g_watchdog_flag_1 + 1;
188 g_watchdog_flag_1 = ((((
unsigned int)g_watchdog_count_1 >> 3) & 1) != 0) ? 1 : 0;
189 g_watchdog_count_1 += 1;
191 return wdi->g_watchdog_clock.lo;
194static int ctrl_drv_op_open(
const iop_file_t *f,
const char *name,
int flags)
201 if ( !strcmp(name,
"watchdog-start") )
203 Kprintf(
"s147ctrl.irx: wdt-start\n");
205 g_watchdog_info.g_watchdog_started = 1;
208 else if ( !strcmp(name,
"watchdog-stop") )
210 Kprintf(
"s147ctrl.irx: wdt-stop\n");
212 g_watchdog_info.g_watchdog_started = 0;
215 else if ( !strcmp(name,
"rpcserv-start") )
217 Kprintf(
"s147ctrl.irx: rpcserv-start\n");
218 if ( !g_rpc_started )
236static int ctrl_drv_op_read(
const iop_file_t *f,
void *ptr,
int size)
240 USE_S147_DEV9_MEM_MMIO();
248 retres = ctrl_do_rtc_read(ptr);
250 Kprintf(
"s147ctrl.irx: RTC Read failed (%d)\n", retres);
255 *(u8 *)ptr = s147_dev9_mem_mmio->m_security_unlock_set1;
256 *((u8 *)ptr + 1) = s147_dev9_mem_mmio->m_security_unlock_set2;
261 *(u8 *)ptr = *(u8 *)(unit + 0xB0000000);
266static int ctrl_drv_op_write(
const iop_file_t *f,
void *ptr,
int size)
270 USE_S147_DEV9_MEM_MMIO();
278 retres = ctrl_do_rtc_write(ptr);
280 Kprintf(
"s147ctrl.irx: RTC Write failed (%d)\n", retres);
285 s147_dev9_mem_mmio->m_security_unlock_set1 = *(u8 *)ptr;
286 s147_dev9_mem_mmio->m_security_unlock_set2 = *((u8 *)ptr + 1);
291 *(u8 *)(unit + 0xB0000000) = *(u8 *)ptr;
296static int create_ctrl_sema(
void)
298 g_ctrl_sema_param.initial = 1;
299 g_ctrl_sema_param.max = 1;
300 g_ctrl_sema_param.attr = SA_THPRI;
301 g_ctrl_sema_id = CreateSema(&g_ctrl_sema_param);
302 if ( g_ctrl_sema_id < 0 )
304 Kprintf(
"s147ctrl.irx: CreateSema error (%d)\n", g_ctrl_sema_id);
310static int ctrl_do_rtc_read(u32 *rtcbuf)
312 USE_S147_DEV9_MEM_MMIO();
314 WaitSema(g_ctrl_sema_id);
315 g_timer_id = AllocHardTimer(1, 0x20, 1);
316 if ( g_timer_id < 0 )
319 g_max_timer_counter = 0x40;
320 while ( GetTimerCounter(g_timer_id) < g_max_timer_counter )
322 g_max_timer_counter += 0x40;
323 s147_dev9_mem_mmio->m_rtc_flag = 1;
324 while ( GetTimerCounter(g_timer_id) < g_max_timer_counter )
326 g_max_timer_counter += 0x40;
327 s147_dev9_mem_mmio->m_rtc_flag = 9;
328 while ( GetTimerCounter(g_timer_id) < g_max_timer_counter )
330 g_max_timer_counter += 0x40;
331 rtcbuf[6] = ctrl_do_rtc_read_inner(8, 0x7F);
332 rtcbuf[5] = ctrl_do_rtc_read_inner(8, 0x7F);
333 rtcbuf[4] = ctrl_do_rtc_read_inner(8, 0x3F);
334 rtcbuf[3] = ctrl_do_rtc_read_inner(4, 7);
335 rtcbuf[2] = ctrl_do_rtc_read_inner(8, 0x3F);
336 rtcbuf[1] = ctrl_do_rtc_read_inner(8, 0x1F);
337 *rtcbuf = ctrl_do_rtc_read_inner(8, 0xFF);
338 s147_dev9_mem_mmio->m_rtc_flag = 1;
339 while ( GetTimerCounter(g_timer_id) < g_max_timer_counter )
341 g_max_timer_counter += 0x40;
342 s147_dev9_mem_mmio->m_rtc_flag = 1;
343 while ( GetTimerCounter(g_timer_id) < g_max_timer_counter )
345 g_max_timer_counter += 0x40;
347 FreeHardTimer(g_timer_id);
348 g_max_timer_counter = 0;
349 SignalSema(g_ctrl_sema_id);
353static int ctrl_do_rtc_read_inner(
int flgcnt,
int flgmsk)
356 USE_S147_DEV9_MEM_MMIO();
359 for ( i = 0; i < flgcnt; i += 1 )
361 s147_dev9_mem_mmio->m_rtc_flag = 0xB;
362 while ( GetTimerCounter(g_timer_id) < g_max_timer_counter )
364 g_max_timer_counter += 0x40;
365 g_rtc_flag |= (s147_dev9_mem_mmio->m_rtc_flag & 1) << i;
366 s147_dev9_mem_mmio->m_rtc_flag = 9;
367 while ( GetTimerCounter(g_timer_id) < g_max_timer_counter )
369 g_max_timer_counter += 0x40;
371 return g_rtc_flag & flgmsk;
374static int ctrl_do_rtc_write(
const u32 *rtcbuf)
376 USE_S147_DEV9_MEM_MMIO();
378 WaitSema(g_ctrl_sema_id);
379 g_timer_id = AllocHardTimer(1, 0x20, 1);
380 if ( g_timer_id < 0 )
383 g_max_timer_counter = 0x40;
384 while ( GetTimerCounter(g_timer_id) < g_max_timer_counter )
386 g_max_timer_counter += 0x40;
387 s147_dev9_mem_mmio->m_rtc_flag = 5;
388 while ( GetTimerCounter(g_timer_id) < g_max_timer_counter )
390 g_max_timer_counter += 0x40;
391 s147_dev9_mem_mmio->m_rtc_flag = 0xD;
392 while ( GetTimerCounter(g_timer_id) < g_max_timer_counter )
394 g_max_timer_counter += 0x40;
395 ctrl_do_rtc_write_inner(rtcbuf[6], 8, 0x7F);
396 ctrl_do_rtc_write_inner(rtcbuf[5], 8, 0x7F);
397 ctrl_do_rtc_write_inner(rtcbuf[4], 8, 0x3F);
398 ctrl_do_rtc_write_inner(rtcbuf[3], 4, 7);
399 ctrl_do_rtc_write_inner(rtcbuf[2], 8, 0x3F);
400 ctrl_do_rtc_write_inner(rtcbuf[1], 8, 0x1F);
401 ctrl_do_rtc_write_inner(*rtcbuf, 8, 0xFF);
402 s147_dev9_mem_mmio->m_rtc_flag = 5;
403 while ( GetTimerCounter(g_timer_id) < g_max_timer_counter )
405 g_max_timer_counter += 0x40;
406 s147_dev9_mem_mmio->m_rtc_flag = 1;
407 while ( GetTimerCounter(g_timer_id) < g_max_timer_counter )
409 g_max_timer_counter += 0x40;
411 FreeHardTimer(g_timer_id);
412 g_max_timer_counter = 0;
413 SignalSema(g_ctrl_sema_id);
417static void ctrl_do_rtc_write_inner(
int inflg,
int flgcnt,
int flgmsk)
421 USE_S147_DEV9_MEM_MMIO();
423 xval = inflg & flgmsk;
424 for ( i = 0; i < flgcnt; i += 1 )
426 s147_dev9_mem_mmio->m_rtc_flag = (xval & 1) | 0xC;
427 while ( GetTimerCounter(g_timer_id) < g_max_timer_counter )
429 g_max_timer_counter += 0x40;
430 s147_dev9_mem_mmio->m_rtc_flag = (xval & 1) | 0xE;
431 while ( GetTimerCounter(g_timer_id) < g_max_timer_counter )
433 g_max_timer_counter += 0x40;
436 s147_dev9_mem_mmio->m_rtc_flag = (xval & 1) | 0xC;
437 while ( GetTimerCounter(g_timer_id) < g_max_timer_counter )
439 g_max_timer_counter += 0x40;
442static int setup_sram_ioman_drv(
const char *devpfx,
const char *devname)
444 g_drv_sram_ioman.name = devpfx;
445 g_drv_sram_ioman.type = IOP_DT_FS;
447 g_drv_sram_ioman.desc = devname;
448 g_drv_sram_ioman.ops = &g_ops_sram_ioman;
450 AddDrv(&g_drv_sram_ioman);
454static int sram_drv_op_open(
iop_file_t *f,
const char *name,
int flags)
465 privdata->m_curpos = 0;
466 privdata->m_maxpos = 0x8000;
483static int sram_drv_op_read(
iop_file_t *f,
void *ptr,
int size)
489 if ( (s32)privdata->m_curpos >= (s32)privdata->m_maxpos )
491 sizeb = ((s32)privdata->m_maxpos < (s32)(privdata->m_curpos + size)) ? (privdata->m_maxpos - privdata->m_curpos) :
493 memcpy(ptr, (
const void *)(privdata->m_curpos + 0xB0C00000), sizeb);
494 privdata->m_curpos += sizeb;
498static int sram_drv_op_write(
iop_file_t *f,
void *ptr,
int size)
502 USE_S147_DEV9_MEM_MMIO();
505 if ( (s32)privdata->m_curpos >= (s32)privdata->m_maxpos )
507 sizeb = ((s32)privdata->m_maxpos < (s32)(privdata->m_curpos + size)) ? (privdata->m_maxpos - privdata->m_curpos) :
509 s147_dev9_mem_mmio->m_sram_write_flag = 1;
510 memcpy((
void *)(privdata->m_curpos + 0xB0C00000), ptr, sizeb);
511 s147_dev9_mem_mmio->m_sram_write_flag = 0;
512 privdata->m_curpos += sizeb;
516static int sram_drv_op_lseek(
iop_file_t *f,
int offset,
int mode)
524 privdata->m_curpos = offset;
527 privdata->m_curpos += offset;
530 privdata->m_curpos = privdata->m_maxpos + offset;
535 if ( (s32)privdata->m_maxpos < (s32)privdata->m_curpos )
537 privdata->m_curpos = privdata->m_maxpos;
540 return privdata->m_curpos;
543static int do_rpc_start1(
void)
549 thparam.thread = rpc_thread1;
550 thparam.priority = 10;
551 thparam.stacksize = 0x800;
553 thid = CreateThread(&thparam);
556 StartThread(thid, 0);
560static void rpc_thread1(
void *userdata)
567 sceSifSetRpcQueue(&qd, GetThreadId());
568 sceSifRegisterRpc(&sd[0], 0x1470000, rpc_1470000_handler, g_rpc1_buf, 0, 0, &qd);
569 sceSifRegisterRpc(&sd[1], 0x1470001, rpc_1470001_handler, g_rpc1_buf, 0, 0, &qd);
570 sceSifRegisterRpc(&sd[2], 0x1470002, rpc_1470002_handler, g_rpc1_buf, 0, 0, &qd);
571 sceSifRegisterRpc(&sd[3], 0x1470003, rpc_1470003_handler, g_rpc1_buf, 0, 0, &qd);
575static void *rpc_1470000_handler(
int fno,
void *buffer,
int length)
577 USE_S147_DEV9_MEM_MMIO();
583 s147_dev9_mem_mmio->m_led = *(u8 *)buffer;
587 s147_dev9_mem_mmio->m_security_unlock_unlock = *(u8 *)buffer;
591 s147_dev9_mem_mmio->m_unk03 = *(u8 *)buffer;
595 s147_dev9_mem_mmio->m_rtc_flag = *(u8 *)buffer;
599 s147_dev9_mem_mmio->m_watchdog_flag2 = *(u8 *)buffer;
603 s147_dev9_mem_mmio->m_security_unlock_set1 = *(u8 *)buffer;
607 s147_dev9_mem_mmio->m_security_unlock_set2 = *(u8 *)buffer;
611 *(u32 *)buffer = -22;
617static void *rpc_1470001_handler(
int fno,
void *buffer,
int length)
619 USE_S147_DEV9_MEM_MMIO();
625 *(u8 *)buffer = s147_dev9_mem_mmio->m_unk00;
626 *((u32 *)buffer + 1) = 0;
629 *(u8 *)buffer = s147_dev9_mem_mmio->m_led;
630 *((u32 *)buffer + 1) = 0;
633 *(u8 *)buffer = s147_dev9_mem_mmio->m_security_unlock_unlock;
634 *((u32 *)buffer + 1) = 0;
637 *(u8 *)buffer = s147_dev9_mem_mmio->m_unk03;
638 *((u32 *)buffer + 1) = 0;
641 *(u8 *)buffer = s147_dev9_mem_mmio->m_rtc_flag;
642 *((u32 *)buffer + 1) = 0;
645 *(u8 *)buffer = s147_dev9_mem_mmio->m_watchdog_flag2;
646 *((u32 *)buffer + 1) = 0;
649 *(u8 *)buffer = s147_dev9_mem_mmio->m_unk06;
650 *((u32 *)buffer + 1) = 0;
653 *(u8 *)buffer = s147_dev9_mem_mmio->m_security_unlock_set1;
654 *((u32 *)buffer + 1) = 0;
657 *(u8 *)buffer = s147_dev9_mem_mmio->m_security_unlock_set2;
658 *((u32 *)buffer + 1) = 0;
662 *((u32 *)buffer + 1) = -22;
668static void *rpc_1470002_handler(
int fno,
void *buffer,
int length)
671 *(u32 *)buffer = (fno == 4) ? ctrl_do_rtc_write(buffer) : -22;
675static void *rpc_1470003_handler(
int fno,
void *buffer,
int length)
681 *((u32 *)buffer + 1) = 0;
682 *((u32 *)buffer + 2) = 0;
683 *((u32 *)buffer + 3) = 0;
684 *((u32 *)buffer + 4) = 0;
685 *((u32 *)buffer + 5) = 0;
686 *((u32 *)buffer + 6) = 0;
687 *((u32 *)buffer + 7) = -22;
690 *((u32 *)buffer + 7) = ctrl_do_rtc_read(buffer);
694static int do_rpc_start2(
void)
700 thparam.thread = rpc_thread2;
701 thparam.priority = 10;
702 thparam.stacksize = 0x800;
704 thid = CreateThread(&thparam);
707 StartThread(thid, 0);
711static void rpc_thread2(
void *userdata)
718 sceSifSetRpcQueue(&qd, GetThreadId());
719 sceSifRegisterRpc(&sd[0], 0x1470200, rpc_1470200_handler, g_rpc2_buf, 0, 0, &qd);
720 sceSifRegisterRpc(&sd[1], 0x1470201, rpc_1470201_handler, g_rpc2_buf, 0, 0, &qd);
724static void *rpc_1470200_handler(
int fno,
void *buffer,
int length)
726 USE_S147_DEV9_MEM_MMIO();
729 if ( (
unsigned int)fno >= 3 )
734 s147_dev9_mem_mmio->m_sram_write_flag = 1;
735 memcpy((
void *)(*((u32 *)buffer + 256) + 0xB0C00000), buffer, *((u32 *)buffer + 257));
736 s147_dev9_mem_mmio->m_sram_write_flag = 0;
741static void *rpc_1470201_handler(
int fno,
void *buffer,
int length)
744 if ( (
unsigned int)fno >= 3 )
746 memset(buffer, 0, *((u32 *)buffer + 1));
747 *((u32 *)buffer + 256) = -1;
750 memcpy(buffer, (
const void *)(*(u32 *)buffer + 0xB0C00000), *((u32 *)buffer + 1));
751 *((u32 *)buffer + 256) = 0;
int CpuResumeIntr(int state)
int CpuSuspendIntr(int *state)