power.c 6.6 KB
#include "ultra64.h"
#include "os_bb.h"
#include "bcp.h"

#include "power.h"
#include "nu64sys.h"
#include "graph.h"


#define DMA_QUEUE_SIZE	200
#define PRINTF		osSyncPrintf
//#define IDE_PRINT

extern u32 __osBbIsBb;

/*
 * Thread and stack structures
 */
char   bootStack[STACKSIZE] __attribute__ ((aligned (8)));

static OSThread idleThread;
static char     idleThreadStack[STACKSIZE] __attribute__ ((aligned (8)));

static OSThread mainThread;
static char     mainThreadStack[STACKSIZE] __attribute__ ((aligned (8)));


/*
 * Message queues and message buffers used by this app 
 */
static OSMesg           PiMessages[DMA_QUEUE_SIZE], dmaMessageBuf;
static OSMesgQueue      PiMessageQ, dmaMessageQ;

static OSMesg           SiMessages[DMA_QUEUE_SIZE];
static OSMesgQueue      SiMessageQ;

/*
 * Local variables and routines
 */

static void		idleproc(char *);
static void		mainproc(char *);

void
boot(void)
{
    osInitialize();
    osCreateThread(&idleThread, 1, (void(*)(void *))idleproc, (void *)0,
                   idleThreadStack+STACKSIZE, 8);
    osStartThread(&idleThread);
}


static void
idleproc(char *argv)		/* priority 8 */
{
    osCreateViManager(OS_PRIORITY_VIMGR);
#if	SCREEN_LOW
    osViSetMode(&osViModeTable[OS_VI_NTSC_LPN1]);
#else
    osViSetMode(&osViModeTable[OS_VI_NTSC_HPF1]);
#endif

    /*
     * Start PI Mgr for access to cartridge - start before the debugger
     */
    osCreatePiManager((OSPri) OS_PRIORITY_PIMGR, &PiMessageQ, PiMessages,
            DMA_QUEUE_SIZE);

    osCreateMesgQueue(&dmaMessageQ, &dmaMessageBuf, 1);
    osCreateMesgQueue(&SiMessageQ, SiMessages, DMA_QUEUE_SIZE);
    osSetEventMesg(OS_EVENT_SI, &SiMessageQ, (OSMesg)DMA_QUEUE_SIZE);

    /*
     * The main thread's priority must be the same or lower than the original
     * idle's thread priority. This allows the idle thread to change its
     * priority to 0 before the main thread starts execution.
     */
    osCreateThread(&mainThread, 3, (void(*)(void *))mainproc, argv,
           mainThreadStack+STACKSIZE/8, (OSPri)7);
    osStartThread(&mainThread);

    osSetThreadPri(0, OS_PRIORITY_IDLE);
    for(;;);                                    /* idle thread */
}

#define __REG(x) "$" #x
#define __REG1(x) #x
#define getcp0reg(source)          	                        \
({ int __res;                                                   \
        __asm__ __volatile__(                                   \
	".set\tpush\n\t"					\
	".set\treorder\n\t"					\
        "mfc0\t%0,"__REG(source)"\n\t"                          \
	".set\tpop"						\
        : "=r" (__res));                                        \
        __res;})

static OSMesgQueue	retraceMessageQ, siMessageQ;;
static OSMesg		dummyMessage, retraceMessageBuf;
static u8 testbuf[1024*1024] __attribute__((aligned(16)));
static OSPiHandle* handler;
static OSIoMesg dmaIOMessageBuf;
OSMesg      siMessageBuf;

static u16 cfb[SCREEN_WD*SCREEN_HT] __attribute__((aligned(64)));
static void clear(u16 bg) {
    int i;
    for (i = 0; i < SCREEN_WD*SCREEN_HT; ++i) {
            cfb[i] = bg;
    }
}


static OSBbFs fs;
static u8 blocks[BB_FL_BLOCK_SIZE];
#define ERR_ON    IO_WRITE(PI_GPIO_REG, \
                          ((PI_GPIO_ERROR_BIT | PI_GPIO_POWER_BIT) << PI_GPIO_ENABLE_SHIFT) | \
                           PI_GPIO_POWER_BIT)
int my_poweroff()
{
    osBbPowerOff();

    /* Busy loop to let CPU spin */
    for (; ;);
}

#define ONE_SEC      46882325
static void usb_test(void) 
{
    char buf[128];
    int rv, i, fd;
    u32 power_num=0;
    OSContStatus     sdata[MAXCONTROLLERS];
    OSContPad        ctrl_data[MAXCONTROLLERS];
    char pattern;
    int key;
    int x, y;
    OSTime end;
    int setup_reg[] = { USB0_OTG_INT_EN_REG, USB0_INT_ENB_REG, USB0_ERR_ENB_REG,
                        USB0_ADDR_REG, USB0_FRM_NUML_REG, USB0_FRM_NUMH_REG};

    osSetEventMesg(OS_EVENT_SI, &siMessageQ, (OSMesg)NULL);
    osContInit(&siMessageQ, &pattern, &sdata[0]);

    IO_WRITE(USB1_SECURE_MODE_REG, USB_SECURE_MODE_OFF);
    IO_WRITE(USB0_SECURE_MODE_REG, USB_SECURE_MODE_OFF);

        /* Set USB1 as host */
    IO_WRITE(USB1_OTG_CTRL_REG, 0x3C);
    IO_WRITE(USB1_CTL_REG, 0x9);
    
    /* IO_WRITE(USB1_CTL_REG, 0x1B);
    end = osGetTime() + ONE_SEC / 1000;
    while (osGetTime() < end); */

    IO_WRITE(USB1_ERR_STAT_REG, 0xff);  /* clear error */
    IO_WRITE(USB1_INT_STAT_REG, 0xff);  /* clear all interrupts */
    IO_WRITE(USB1_INT_ENB_REG, 0xff);

        /* Set USB0 as device */
    IO_WRITE(USB0_INT_STAT_REG, 0);
    for (i=0; i<sizeof(setup_reg)/sizeof(int); i++)
        IO_WRITE(setup_reg[i], 0);
    IO_WRITE(USB0_OTG_CTRL_REG, 0x80);
    IO_WRITE(USB0_CTL_REG, 0x02);
    IO_WRITE(USB0_CTL_REG, 0x01);
    IO_WRITE(USB0_INT_ENB_REG, 0xff);

    for (; ;) {
        if (IO_READ(MI_EINTR_REG) & MI_EINTR_BUT_STATUS) 
            my_poweroff();

        osContStartReadData(&siMessageQ);
        osRecvMesg(&siMessageQ, NULL, OS_MESG_BLOCK);
        osContGetReadData(ctrl_data);

        for (i=key=0; i<MAXCONTROLLERS; i++) {
             if (!(ctrl_data[i].errno &CONT_NO_RESPONSE_ERROR))
                 key |= ctrl_data[i].button;
        }

        if (key & (CONT_C | CONT_D | CONT_E | CONT_F)) {
            IO_WRITE(0x4900080, 0xff);
            IO_WRITE(0x4A00080, 0xff);
        }

        sprintf(buf, "otg_int_stat = 0x%02x 0x%02x", 
                     IO_READ(0x4900010),IO_READ(0x4A00010));
        printstr(yellow, 3, 3, buf);    

        sprintf(buf, "otg_int_en = 0x%02x 0x%02x", 
                     IO_READ(0x4900014),IO_READ(0x4A00014));
        printstr(yellow, 3, 4, buf);    
        
        sprintf(buf, "otg_status = 0x%02x 0x%02x", 
                     IO_READ(0x4900018),IO_READ(0x4A00018));
        printstr(yellow, 3, 5, buf);    
        
        sprintf(buf, "int_stat = 0x%02x 0x%02x", 
                     IO_READ(0x4900080),IO_READ(0x4A00080));
        printstr(yellow, 3, 6, buf);    
        
        sprintf(buf, "int_enbl = 0x%02x 0x%02x", 
                     IO_READ(0x4900084),IO_READ(0x4A00084));
        printstr(yellow, 3, 7, buf);    
        
        sprintf(buf, "usb_ctrl = 0x%02x 0x%02x", 
                     IO_READ(0x4900094),IO_READ(0x4A00094));
        printstr(yellow, 3, 8, buf);    
        
    }
    return;
}

static void 
mainproc(char *argv)		{
    osCreateMesgQueue(&siMessageQ, &siMessageBuf, 1);
    osSetEventMesg(OS_EVENT_SI, &siMessageQ, dummyMessage);

    osCreateMesgQueue(&retraceMessageQ, &retraceMessageBuf, 1);
    osViSetEvent(&retraceMessageQ, dummyMessage, 1);
    osViBlack(1);
    osViSwapBuffer(cfb);
    clear(0x0);
    osViBlack(0);
    osWritebackDCacheAll();
    osViSwapBuffer(cfb);

    IO_WRITE(PI_FLASH_CONFIG_REG, 0xa207071f);
    usb_test();
}