cardprobe.c 1.11 KB
#include <ultra64.h>
#include "bcp.h"
#include "bbnand.h"
#include "bbfs.h"
#include "os_bbcard.h"
#include "bbint.h"
#include "piint.h"

s32
osBbCardProbe(u32 dev) {
    s32 count, rv;
    u8 save = __osBbCardChange;

    __osBbCardChange = 0;
    if ((rv = __osBbCardGetAccess()) < 0) return rv;
    IO_WRITE(PI_BUFFER_0_START, 0);
    IO_WRITE(PI_FLASH_CTRL_REG, PI_FLASH_CTRL_START |
				PI_FLASH_CTRL_RDPH |
				(NAND_CMD_STATUS << PI_FLASH_CTRL_CMD_SHIFT) |
				(0 << PI_FLASH_CTRL_BUF_SHIFT) |
				(dev << PI_FLASH_CTRL_DEV_SHIFT) |
				(1 << PI_FLASH_CTRL_SIZE_SHIFT));
    rv = BBCARD_ERR_NO_CARD;
    count = 0;
    while(count < 1000) {
	if (!(IO_READ(PI_FLASH_CTRL_REG)&PI_FLASH_CTRL_BUSY)) {
	    if (((IO_READ(PI_BUFFER_0_START)>>24)&0xff) ==
		    (NAND_STATUS_WRITE_OK|NAND_STATUS_READY)) {
#ifdef _DEBUG
		osSyncPrintf("probe succeeds\n");
#endif
		__osBbCardFlushEvent();
		rv = 0;
		goto out;
	    }
	    break;
	}
	__osBbDelay(10);
	count++;
    }
    IO_WRITE(PI_FLASH_CTRL_REG, 0);
    __osBbCardChange = save;
#ifdef _DEBUG
    osSyncPrintf("probe fails\n");
#endif
out:
    __osBbCardRelAccess();
    return rv;
}