diskmain.c 13.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
/********************************************************************************
						   NINTENDO64 Disk Drive IPL4

							   disk control module

								February 4, 1997
 ********************************************************************************/

#include <ultra64.h>
#include "iplleo.h"
#include "ipl4.h"
#include "AUDIO/cpuaudio.h"


#define	PHASE_RECEIVE_MESSAGE		0
#define	PHASE_INSERT_DDDISK			1
#define	PHASE_LOAD_APPLICATION		2
#define	PHASE_EJECT_DDDISK			3
#define	PHASE_DONE_LOADING			4
#define	PHASE_DISK_HANGUP			5

#define       IPLROMID_JPNCODE                0xC3
#define       IPLROMID_USACODE                0x04
#define       IPLROMID_ADDRESS                0x09FF00

volatile static LEOCmdReadIPL cmdReadIPL;

DDClock diskClock = {
	0x97, 0x01, 0x01, 0x00, 0x00, 0x00, CLOCK_STAT_ACTIVE
};


//////////////////////////////////////////////////////////////////////////////////
//
//
//
//
//		Disk I/O routines
//
//
//
//
//////////////////////////////////////////////////////////////////////////////////

/********************************************************************************/
/*																				*/
/*	Read disk ID.																*/
/*																				*/
/********************************************************************************/
static void
LeoIPLReadDiskID(volatile LEOCmdReadIPL *cmdBlock)
{
	cmdBlock->header.command	= LEO_COMMAND_READ_DISK_ID;
	cmdBlock->header.reserve1	= 0;
	cmdBlock->header.control	= 0;
	cmdBlock->header.reserve3	= 0;
	cmdBlock->header.status		= 0;
	cmdBlock->header.sense		= 0;
	cmdBlock->buff_ptr			= (uwlong *)diskInformation;

	leoIPLCommand((void *)cmdBlock);
	while(cmdBlock->header.status == LEO_STATUS_BUSY);
}
/********************************************************************************/
/*																				*/
/*	Test drive unit ready.														*/
/*																				*/
/********************************************************************************/
static void
LeoIPLTestUnitReady(volatile LEOCmdReadIPL *cmdBlock)
{
	cmdBlock->header.command	= LEO_COMMAND_TEST_UNIT_READY;
	cmdBlock->header.reserve1	= 0;
	cmdBlock->header.control	= 0;
	cmdBlock->header.reserve3	= 0;
	cmdBlock->header.status		= 0;
	cmdBlock->header.sense		= 0;

	leoIPLCommand((void *)cmdBlock);
	while(cmdBlock->header.status == LEO_STATUS_BUSY);
}
/********************************************************************************/
/*																				*/
/*	Read disk.																	*/
/*																				*/
/********************************************************************************/
static void
LeoIPLRead(volatile LEOCmdReadIPL *cmdBlock)
{
	OSMesgQueue	mesgque;
	OSMesg		mesgbuf;


	osCreateMesgQueue(&mesgque, &mesgbuf, 1);

	cmdBlock->header.command	= LEO_COMMAND_READ;
	cmdBlock->header.reserve1	= 0;
	cmdBlock->header.control	= LEO_CONTROL_POST;
	cmdBlock->header.reserve3	= 0;
	cmdBlock->header.status		= 0;
	cmdBlock->header.sense		= 0;
	cmdBlock->header.reserve6	= 0;
	cmdBlock->header.post		= &mesgque;
	cmdBlock->code				= LEO_COUNTRY_CODE;

	leoIPLCommand((void *)cmdBlock);
	osRecvMesg(&mesgque, NULL, OS_MESG_BLOCK);
}
/********************************************************************************/
/*																				*/
/*	Read timer.																	*/
/*																				*/
/********************************************************************************/
static void
LeoIPLReadTimer(volatile LEOCmdTimeIPL *cmdBlock)
{
	cmdBlock->header.command	= LEO_COMMAND_READ_TIMER;
	cmdBlock->header.reserve1	= 0;
	cmdBlock->header.control	= 0;
	cmdBlock->header.reserve3	= 0;
	cmdBlock->header.status		= 0;
	cmdBlock->header.sense		= 0;

	leoIPLCommand((void *)cmdBlock);
	while(cmdBlock->header.status == LEO_STATUS_BUSY);
}
/********************************************************************************/
/*																				*/
/*	Set timer.																	*/
/*																				*/
/********************************************************************************/
static void
LeoIPLSetTimer(volatile LEOCmdTimeIPL *cmdBlock)
{
	cmdBlock->header.command	= LEO_COMMAND_SET_TIMER;
	cmdBlock->header.reserve1	= 0;
	cmdBlock->header.control	= 0;
	cmdBlock->header.reserve3	= 0;
	cmdBlock->header.status		= 0;
	cmdBlock->header.sense		= 0;

	leoIPLCommand((void *)cmdBlock);
	while(cmdBlock->header.status == LEO_STATUS_BUSY);
}
/********************************************************************************/
/*																				*/
/*	Disk process.																*/
/*																				*/
/********************************************************************************/
static void
DiskBootable(void *bootAddr)
{
	bootHeader.location = bootAddr;
	bootHeader.entry	= bootAddr;
	bootHeader.skip		= 0x0;
	bootHeader.nbytes	= 0;
	bootHeader.firstlba	= 0;
}
/********************************************************************************/
/*																				*/
/*	Check disk condition (sense code).											*/
/*																				*/
/********************************************************************************/

#define MediumNotPresent()	((diskStatus == DISK_STAT_LOADING) ? DISK_STAT_DISKEJECT : DISK_STAT_NODISK)

static int
CheckDiskCondition(volatile LEOCmdHeader *header)
{
	int result;

	switch (header->sense) {
		case LEO_SENSE_DEVICE_COMMUNICATION_FAILURE		: result = DISK_STAT_NOCONNECT;		break;
		case LEO_SENSE_POWERONRESET_DEVICERESET_OCCURED	: result = DISK_STAT_WAITING;		break;
		case LEO_SENSE_MEDIUM_NOT_PRESENT				: result = MediumNotPresent();		break;
		case LEO_SENSE_DIAGNOSTIC_FAILURE				: result = DISK_STAT_BADINSERT;		break;
		default											: result = DISK_STAT_DISKERROR;		break;
	}
	return(result);
}
/********************************************************************************/
/*																				*/
/*	Check disk status.															*/
/*																				*/
/********************************************************************************/
static int
CheckDiskStatus(volatile LEOCmdHeader *header, int goodStatus)
{
	int result = DISK_STAT_WAITING;

	switch (header->status) {
		case LEO_STATUS_BUSY			: result = DISK_STAT_WORKING;			break;
		case LEO_STATUS_GOOD			: result = goodStatus;					break;
		case LEO_STATUS_CHECK_CONDITION	: result = CheckDiskCondition(header);	break;
	}
	return(result);
}


//////////////////////////////////////////////////////////////////////////////////
//
//
//
//
//		Clock control
//
//
//
//
//////////////////////////////////////////////////////////////////////////////////

/********************************************************************************/
/*																				*/
/*	Read time from disk drive.													*/
/*																				*/
/********************************************************************************/
static void
ReadDDClock(DDClock *time)
{
	static	 ulong			timeStump = 0;
	volatile LEOCmdTimeIPL	cmdBlock;

	if (frameCounter != timeStump) {
		timeStump = frameCounter;
		LeoIPLReadTimer(&cmdBlock);
		if (cmdBlock.header.status == LEO_STATUS_GOOD) {
			time->year	 = cmdBlock.year;
			time->month  = cmdBlock.month;
			time->day	 = cmdBlock.day;
			time->hour	 = cmdBlock.hour;
			time->minute = cmdBlock.minute;
			time->second = cmdBlock.second;
			time->status = CLOCK_STAT_ACTIVE;
		}
		else if (cmdBlock.header.sense == LEO_SENSE_REAL_TIME_CLOCK_FAILURE) {
			time->status = CLOCK_STAT_INACTIVE;
		}
	}
}
/********************************************************************************/
/*																				*/
/*	Read time from disk drive.													*/
/*																				*/
/********************************************************************************/
extern void
WriteDDClock(DDClock *time)
{
	volatile LEOCmdTimeIPL cmdBlock;

	cmdBlock.year	= time->year;
	cmdBlock.month  = time->month;
	cmdBlock.day	= time->day;
	cmdBlock.hour	= time->hour;
	cmdBlock.minute = time->minute;
	cmdBlock.second = time->second;
	LeoIPLSetTimer(&cmdBlock);
}


//////////////////////////////////////////////////////////////////////////////////
//
//
//
//
//		Disk control main
//
//
//
//
//////////////////////////////////////////////////////////////////////////////////

/********************************************************************************/
/*																				*/
/*	Wait until receive the start message.										*/
/*																				*/
/********************************************************************************/
static int
ReceiveStartMessage(void)
{
	OSMesg	message;
	int		phase = PHASE_RECEIVE_MESSAGE;


	diskStatus = DISK_STAT_WAITING;

	while (phase == PHASE_RECEIVE_MESSAGE) {
		osRecvMesg(&diskMesgque, &message, OS_MESG_BLOCK);
		if (message == MESG_DISK_START )  phase = PHASE_INSERT_DDDISK;
		if (message == MESG_DISK_HANGUP)  phase = PHASE_DISK_HANGUP;
	};
	return(phase);
}
/********************************************************************************/
/*																				*/
/*	Wait until insert disk.														*/
/*																				*/
/********************************************************************************/
static int
WaitDiskInsert(void)
{
	do {
		ReadDDClock(&diskClock);
		LeoIPLTestUnitReady(&cmdReadIPL);
		diskStatus  = CheckDiskStatus(&cmdReadIPL.header, DISK_STAT_LOADING);
		diskErrorNo = cmdReadIPL.header.sense;

//		if ((cmdReadIPL.header.status != LEO_STATUS_GOOD) && (cmdReadIPL.header.sense != LEO_SENSE_MEDIUM_NOT_PRESENT)) {
//			return(PHASE_DISK_HANGUP);
//		}
		if (diskStatus == DISK_STAT_NOCONNECT || diskStatus == DISK_STAT_BADINSERT || diskStatus == DISK_STAT_DISKERROR) {
			return(PHASE_DISK_HANGUP);
		}
	} while (clockSetting || diskClock.status == CLOCK_STAT_INACTIVE || cmdReadIPL.header.status != LEO_STATUS_GOOD);

	return(PHASE_LOAD_APPLICATION);
}
/********************************************************************************/
/*																				*/
/*	Wait until eject disk.														*/
/*																				*/
/********************************************************************************/
static int
WaitDiskEject(void)
{
	LeoIPLTestUnitReady(&cmdReadIPL);

	while (CheckDiskStatus(&cmdReadIPL.header, DISK_STAT_WAITING) != DISK_STAT_NODISK) {
		ReadDDClock(&diskClock);
		LeoIPLTestUnitReady(&cmdReadIPL);
	}
	osSendMesg(&grphMesgque, MESG_GRPH_RESTART, OS_MESG_BLOCK);
	return(PHASE_RECEIVE_MESSAGE);
}
/********************************************************************************/
/*																				*/
/*	Load a application program.													*/
/*																				*/
/********************************************************************************/
static int
LoadApplication(void)
{
  u32 cur_status;
  u32 stat;
#ifdef DISKROM
        OSPiHandle *PiHandle_test = osDriveRomInit();
#else
        OSPiHandle *PiHandle_test = osCartRomInit();
#endif

	Nacp_SeStart(NACP_INSERT);
	LeoIPLReadDiskID(&cmdReadIPL);
	if (cmdReadIPL.header.status == LEO_STATUS_GOOD) {
		LeoIPLRead(&cmdReadIPL);
	}
	diskStatus  = CheckDiskStatus(&cmdReadIPL.header, DISK_STAT_LOADEND);
	diskErrorNo = cmdReadIPL.header.sense;

	if (cmdReadIPL.header.status != LEO_STATUS_GOOD) {
//		return(PHASE_EJECT_DDDISK);
		return(PHASE_DISK_HANGUP);
	}

 /* ASIC_ID test */

  stat = IO_READ(PI_STATUS_REG);
  while (stat & (PI_STATUS_IO_BUSY | PI_STATUS_DMA_BUSY)) {
    stat = IO_READ(PI_STATUS_REG);
  } 
  cur_status = IO_READ(0xa5000540);

#ifndef DEVELOP_ID
#ifdef DISKROM
  if ((cur_status & 0x00070000) != 0x00030000){
    diskStatus  = DISK_STAT_DISKERROR;
    diskErrorNo = 3;
    return(PHASE_DISK_HANGUP);
  }
#else
  if ((cur_status & 0x00070000) == 0x00040000){
    diskStatus  = DISK_STAT_DISKERROR;
    diskErrorNo = 3;
    return(PHASE_DISK_HANGUP);
  }
#endif
#else
  if ((cur_status & 0x00070000) != 0x00040000){
    diskStatus  = DISK_STAT_DISKERROR;
    diskErrorNo = 3;
    return(PHASE_DISK_HANGUP);
  }
#endif

  /* iplromID test */
  osEPiReadIo(PiHandle_test, IPLROMID_ADDRESS, &cur_status);

#ifdef JAPAN_ID
  if (((cur_status>> 24) & 0xff) != IPLROMID_JPNCODE){
    diskStatus  = DISK_STAT_DISKERROR;
    diskErrorNo = 3;
    return(PHASE_DISK_HANGUP);
  }
#endif
#ifdef USACODE_ID
  if (((cur_status>> 24) & 0xff) != IPLROMID_USACODE){
    diskStatus  = DISK_STAT_DISKERROR;
    diskErrorNo = 3;
    return(PHASE_DISK_HANGUP);
  }
#endif
	return(PHASE_DONE_LOADING);
}
/********************************************************************************/
/*																				*/
/*	Wait until user's RESET.													*/
/*																				*/
/********************************************************************************/
static int
DiskHangup(void)
{
	while (1) {
		ReadDDClock(&diskClock);
	}
	return(PHASE_DISK_HANGUP);
}
/********************************************************************************/
/*																				*/
/*	Disk process.																*/
/*																				*/
/********************************************************************************/
extern void
DiskProcess(void *arg)
{
	int	phase = PHASE_RECEIVE_MESSAGE;


	leoIPLInitialize(LEO_PRIORITY_WRK, LEO_PRIORITY_INT);

	while (phase != PHASE_DONE_LOADING) {
		switch (phase) {
			case PHASE_RECEIVE_MESSAGE	: phase = ReceiveStartMessage();	break;
			case PHASE_INSERT_DDDISK	: phase = WaitDiskInsert();			break;
			case PHASE_LOAD_APPLICATION	: phase = LoadApplication();		break;
			case PHASE_EJECT_DDDISK		: phase = WaitDiskEject();			break;
			case PHASE_DISK_HANGUP		: phase = DiskHangup();				break;
		}
	}
	DiskBootable(cmdReadIPL.buff_ptr);
	osSendMesg(&mainMesgque, MESG_MAIN_LOADDONE, OS_MESG_BLOCK);
}
/********************************************************************************/
/*																				*/
/*	Reset disk driver.															*/
/*																				*/
/********************************************************************************/
extern void
ResetDiskDriver(void)
{
	leoIPLReset();
}