leocmdex.c 16.9 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 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
/*
 *  F i l e N a m e : l e o c m d e x . c
 *
 ****************************************************************************
 *                   (C) Copyright ALPS Electric Co., Ltd. 1995-1996
 ****************************************************************************
 * Version:
 *
 *  ver     date
 *  ----  --------
 *  1.18  '97-02-20  Add unit attention handling.
 *  1.17  '97-01-10  Add recal at system read retry.
 *  1.16  '96-12-24  Add "#ifdef _LONGCMD" for long command.
 *  1.15  '96-11-17  Add command "void leoReadTimer(void)" & "void leoSetTimer(void)".
 *  1.14  '96-10-31  Set leoSend_asic_cmd_w() return value in sense.
 *  1.13a '96-10-07  Enable bit 6 of control of command if SYSTEMWRITE is defined.
 *  1.13  '96-09-20  Change to old format disk return "UNKNOWN FORMAT ERROR" .
 *  1.12  '96-09-18  Modefy drive status error handling .
 *  1.11  '96-09-13  Change current drive_mode check .
 *  1.10  '96-09-12  Add LEO_sys_data fmt_type check .
 *  1.09  '96-08-27  See LEO_country_code to check system sector size to remove KAIHATU definition.
 *  1.08  '96-08-26  Add command "void leoReadDiskId(void)".
 *  1.07  '96-08-08  Add ASIC_ID check .
 *  1.06  '96-08-01  Separate from IPL .
 *  1.05  '96-06-24  Modefy Read Command control bit .
 *                   Deleted Read_timer/Set_timer Command .
 *  1.03  '96-05-08  Change System area read retrycntr    (40 -> MAX_RETRY)
 *                   Change Bad ECC Sector read retrycntr ( 5 -> MAX_RETRY)
 *  1.02  '96-04-05  Change support disktype number .
 *  1.01  '96-02-27  Rename leomain.c to leocmdex.c .
 *  1.00  '96-12-17  Initial Revision.
 ****************************************************************************
 */
#include "os.h"

#include <ultra64.h>
#include "leodefine.h"
#include "leodrive.h"
#include "leomacro.h"
#include "leoappli.h"
#ifdef _LONGCMD
#include "leospappli.h"
#endif
/*************************************/
/* PROTOTYPE DEFINITIONS             */
/*************************************/
void leomain(void *arg);
u8   leoRead_system_area(void);

/*************************************/
/* EXTERNAL FUNCTIONS                */
/*************************************/
extern  void leoReset(void);
extern  void leoClr_queue(void);
extern  void leoInquiry(void);
extern  void leoTest_unit_rdy(void);
extern  void leoRezero(void);
extern  void leoRead(void);
extern  void leoWrite(void);
extern  void leoSeek(void);
extern  void leoStart_stop(void);
extern  void leoRd_capacity(void);
extern  void leoTranslate(void);
extern  void leoMode_sel(void);
extern  void leoReadDiskId(void);
extern  void leoReadTimer(void);
extern  void leoSetTimer(void);

extern  void leoReadLong(void);
extern  void leoWriteLong(void);
extern  void leoReadSector(void);
extern  void leoClr_reset(void);

extern void leoRead_common(unsigned int);

extern void leoInitUnit_atten(void);
extern u32  leoChkUnit_atten(void);
extern u32  leoRetUnit_atten(void);
extern void leoClrUA_RESET(void);
extern void leoClrUA_MEDIUM_CHANGED(void);

/*************************************/
/*  EXTERNALY DEFINED RAMS           */
/*************************************/
extern OSMesg LEOcur_command;

extern OSMesgQueue LEOcommand_que;
extern OSMesgQueue LEOevent_que;
extern OSMesgQueue LEOdma_que;
extern u8  LEOdisk_type;


extern union leo_sys_form LEO_sys_data;
extern u8  LEOdrive_flag;
extern volatile u8  LEOclr_que_flag;
extern u16 LEOrw_flags;
extern u32 LEOasic_bm_ctl_shadow;
extern u32 LEOasic_seq_ctl_shadow;
extern union data_trans_form LEO_country_code;

/*----------------- NEW OS -------------------*/
OSPiHandle  *LEOPiInfo;
OSIoMesg    LEOPiDmaParam;

/*************************************/
/* LOCAL DEFINITIONS                 */
/*************************************/
#define chk_cmd     ((LEOCmdHeader *)LEOcur_command)

static void (*cmd_tbl[/*LEO_CMD_NUM*/])(void) = {
        NULL,
        leoClr_queue,
        leoInquiry,
        leoTest_unit_rdy,
        leoRezero,
        leoRead,
        leoWrite,
        leoSeek,
        leoStart_stop,
        leoRd_capacity,
        leoTranslate,
        leoMode_sel,
        leoReadDiskId,
        leoReadTimer,
        leoSetTimer,
        leoClr_reset
#ifdef _LONGCMD
        ,leoReadLong
        ,leoWriteLong
        ,leoReadSector
#endif
    };

/* ==========================================================================
* Function : leomain
* --------------------------------------------------------------------------
* Description : do command
* --------------------------------------------------------------------------
* IN      : NON
* OUT     : LEOdisk_type, system-info,LEOcur_command
* ARG     : NON
* RET     : NON
* ==========================================================================
*/
void leomain(void *arg)
{
    u32  cur_status,sense_code;

    /* Load country code */
    LEO_country_code.u8_data[0] = *(u8*)OS_PHYSICAL_TO_K1(0x00000010);
    LEO_country_code.u8_data[1] = *(u8*)OS_PHYSICAL_TO_K1(0x00000090);
    LEO_country_code.u8_data[2] = *(u8*)OS_PHYSICAL_TO_K1(0x00000110);
    LEO_country_code.u8_data[3] = *(u8*)OS_PHYSICAL_TO_K1(0x00000190);

    /*Initialize shadow rams */
    LEOasic_seq_ctl_shadow = 0;
    LEOasic_bm_ctl_shadow  = 0;

    /* Initialize flags */
    leoClr_sys_read();
    leoClr_clr_que_flag();
    leoInitUnit_atten();

    /* Initialize AD16-bus access parameters */
    LEOPiInfo = osLeoDiskInit();
    LEOPiDmaParam.hdr.pri  = OS_MESG_PRI_HIGH;
    LEOPiDmaParam.hdr.retQueue = DMA_QUE;

    /* Clear drive operation */
    leoGet_leo_status( &cur_status );
    if ( (cur_status & LEO_STAT_RESET) == 0)
    {
        if (cur_status & (LEO_STAT_BUSY + LEO_STAT_BM_INT + LEO_STAT_MECHA_INT) ) /* command executing or done */
        {
            /* reset drive */
            leoDrive_reset();
        }
    }

    while (1)
    {
        /* get command from command que */
        (void)osRecvMesg(COMMAND_QUE, (OSMesg)&LEOcur_command, OS_MESG_BLOCK);

        /*----------------------------*/
        /* check command              */
        /*----------------------------*/
        if (chk_cmd->command == 0)
        {
            leoDrive_reset();  /* reset */
            (void)osRecvMesg(EVENT_QUE, NULL, OS_MESG_NOBLOCK);
            continue;
        }
        /* check drive status */
        sense_code = leoChk_asic_ready(ASIC_RD_SEEK);
        cur_status = leoChkUnit_atten();
        do
        {
            if (cur_status == 0)
            {
                if (sense_code == LEO_SENSE_NO_ADDITIONAL_SENSE_INFOMATION)
                    continue;
            }
            else
            {
                switch (sense_code)
                {
                    case LEO_SENSE_DEVICE_COMMUNICATION_FAILURE:
                    case LEO_SENSE_COMMAND_PHASE_ERROR:
                    case LEO_SENSE_POWERONRESET_DEVICERESET_OCCURED:
                        break;
                    case LEO_SENSE_EJECTED_ILLEGALLY_RESUME:
                        if (leoRetUnit_atten() == LEO_SENSE_POWERONRESET_DEVICERESET_OCCURED)
                            sense_code= LEO_SENSE_POWERONRESET_DEVICERESET_OCCURED;
                        break;
                    /*case LEO_SENSE_MEDIUM_MAY_HAVE_CHANGED:*/
                    default:
                        sense_code = leoRetUnit_atten();
                }
            }

            switch (sense_code)
            {
                case LEO_SENSE_MEDIUM_MAY_HAVE_CHANGED:
                    switch (chk_cmd->command)
                    {
                        case LEO_COMMAND_READ_DISK_ID:
                            leoClrUA_MEDIUM_CHANGED();
                        case LEO_COMMAND_INQUIRY:
                        case LEO_COMMAND_READ_TIMER:
                        case LEO_COMMAND_SET_TIMER:
                        case LEO_COMMAND_RESETCLEAR:
                            continue;
                        default:
                            ;
                    }
                    break;
                case LEO_SENSE_EJECTED_ILLEGALLY_RESUME:
                    switch (chk_cmd->command)
                    {
                        case LEO_COMMAND_INQUIRY:
                        case LEO_COMMAND_MODE_SELECT:
                        case LEO_COMMAND_READ_TIMER:
                        case LEO_COMMAND_SET_TIMER:
                        case LEO_COMMAND_RESETCLEAR:
                            continue;
                        default:
                            sense_code = LEO_SENSE_MEDIUM_NOT_PRESENT;
                    }
                    break;
                case LEO_SENSE_POWERONRESET_DEVICERESET_OCCURED:
                    switch (chk_cmd->command)
                    {
                        case LEO_COMMAND_RESETCLEAR:
                            leoClrUA_RESET();
                        case LEO_COMMAND_INQUIRY:
                        case LEO_COMMAND_READ_TIMER:
                        case LEO_COMMAND_SET_TIMER:
                            continue;
                        default:
                            ;
                    }
                    break;
                /*case LEO_SENSE_COMMAND_PHASE_ERROR:          */
                /*case LEO_SENSE_DEVICE_COMMUNICATION_FAILURE: */
                default:
                    ;
            }
            if (chk_cmd->command == LEO_COMMAND_TEST_UNIT_READY)
                ((LEOCmdTestUnitReady *)LEOcur_command)->test = leoChk_cur_drvmode();

            /* err-code should be set before status */
            chk_cmd->sense  = sense_code;
            chk_cmd->status = LEO_STATUS_CHECK_CONDITION;
            goto post_exe;
        } while (0);
        /*--------------------------------------------*/
        /* Check if disk system information is read  */
        /*--------------------------------------------*/
#ifdef _LONGCMD
        if (chk_cmd->control & LEO_CONTROL_ND) /* bit6 */
        {
            leoClr_sys_read();
            for (LEOdisk_type = 16; LEOdisk_type;)
            {
                LEO_sys_data.param.defect_num[--LEOdisk_type] = 0;
            }
            LEO_sys_data.param.fmt_type      = 0x10;
        }
        else
#endif
        if (!leoChk_sys_read())
        {
            switch (chk_cmd->command)
            {
                /* Execute if these commands */
                case LEO_COMMAND_INQUIRY:
                case LEO_COMMAND_TEST_UNIT_READY:
                case LEO_COMMAND_MODE_SELECT:
                case LEO_COMMAND_READ_TIMER:
                case LEO_COMMAND_SET_TIMER:
                case LEO_COMMAND_RESETCLEAR:
                    break;
                default:
                    if (LEO_country_code.u32_data == 0)
                    {
                        leoGet_leo_asic_id(&cur_status); /* Check ASIC ID */
                        if ((cur_status & 0x00070000) != 0x00040000)
                            do {} while(1);
                    }
                    /* READ SYSTEM INFORMATION */
                    if (leoRead_system_area())
                    {
                        chk_cmd->status = LEO_STATUS_CHECK_CONDITION;
                        goto post_exe;
                    }

                    /*  Transfer format/disk-type information to drive  */
                    if (chk_cmd->sense = leoSend_asic_cmd_w(ASIC_SET_DTYPE, ((u32)LEO_sys_data.param.disk_type << 16)))
                    {
                        chk_cmd->status = LEO_STATUS_CHECK_CONDITION;
                        goto post_exe;
                    }

                    /* Check format-type */
                    if ((LEO_sys_data.param.disk_type & 0xf0) != 0x10)
                        goto invalid_disktype;

                    /* check disk-type */
                    LEOdisk_type = (LEO_sys_data.param.disk_type & 0x0f);
                    if (LEOdisk_type >= DISK_TYPES)
                    {
invalid_disktype:
                        chk_cmd->sense = LEO_SENSE_INCOMPATIBLE_MEDIUM_INSTALLED;
                        chk_cmd->status = LEO_STATUS_CHECK_CONDITION;
                        goto post_exe;
                    }
                    leoSet_sys_read();     /* Set system information flag */
            }
        }
        /* Branch to excute command */
        (*(cmd_tbl[chk_cmd->command]))();

post_exe:
        /* POST */
        if (chk_cmd->control & LEO_CONTROL_POST)
        {
            /* send sense code to post message */
            osSendMesg(chk_cmd->post, (OSMesg)(chk_cmd->sense), OS_MESG_BLOCK);
        }
        /*  Clear que if requested */
        if (leoChk_clr_que_flag())
            leoClr_queue();
    }
}

/* ==========================================================================
* Function : leoRead_system_area
* --------------------------------------------------------------------------
* Description : Read system information.
*             : Hangup if copy protect function detect abnormal format.
* --------------------------------------------------------------------------
* IN     : NON
* OUT    : NON
* ARG    : NON
* RET    :  0     - GOOD
*        : NOT 0  - ERROR
* ==========================================================================
*/
const static LEOCmdRead system_read_cmd =
      { { LEO_COMMAND_READ,           /* COMMAND        */
          0x00,                       /* RESERVE        */
          0x00,                       /* CONTROL        */
          0x00,                       /* RESERVE        */
          0x00,                       /* STATUS         */
          0x00,                       /* SENSE          */
          0x00,                       /* RESERVE        */
          0x00,                       /* RESERVE        */
          NULL                        /* POST QUEUE     */
        },
          SYS_UNCORR_LBA,             /* LBA            */
          0x01,                       /* BLOCKS         */
          0x00,                       /* BUFFER POINTER */
          0x00,                       /* READ BYTES     */
      };

static const u8 system_lba[8] = {
        SYS_DATA_LBA1,
        SYS_DATA_LBA2,
        SYS_DATA_LBA3,
        SYS_DATA_LBA4 };

u8 leoRead_system_area(void)
{
    LEOCmdRead dummy_cmd;
    void *backup_pointer;
    u32 retry_cntr;
    u32 read_mode;

    /* Backup current command */
    backup_pointer = LEOcur_command;
    LEOcur_command = &dummy_cmd;

    read_mode = 0;
    retry_cntr = 0;
    do
    {
        /* Set disk-type,defect number to 0 to use in lba_to_phys() routine */
        LEOdisk_type = 0;
        LEO_sys_data.param.defect_num[0] = 0;
        LEOrw_flags = SECTOR_READ + NO_RETRY;
        dummy_cmd = system_read_cmd;
        dummy_cmd.buff_ptr = &LEO_sys_data.u64_data[0];

        if (read_mode == 0)
        {
            /*************************/
            /* read BAD ECC sector   */
            /*************************/
            leoRead_common(0);
            switch (dummy_cmd.header.sense)
            {
                case LEO_SENSE_NO_ADDITIONAL_SENSE_INFOMATION:
                    do {} while(LEO_country_code.u32_data != 0);
                    break;
                case LEO_SENSE_UNRECOVERED_READ_ERROR:
                    do {} while(LEO_country_code.u32_data == 0);
                    break;
                default:
                    goto system_retry;
            }
            read_mode--;
            retry_cntr = 0;
            continue;
        }
        else
        {
            /*************************/
            /* read disk system data */
            /*************************/
            dummy_cmd.lba = system_lba[(retry_cntr & 0x03)];
            if (LEO_country_code.u32_data == 0)
                dummy_cmd.lba += SYS_LBA_DEV_OFFSET;
#ifdef _ERRCHK
            switch (((LEOCmdHeader *)backup_pointer)->command)
            {
                case LEO_COMMAND_READ_CAPACITY:
                case LEO_COMMAND_TRANSLATE:
                    dummy_cmd.header.reserve1 = ((LEOCmdHeader *)backup_pointer)->reserve1;
                default: ;
            }
#endif
            /* call read routine to read sysem information */
            leoRead_common(0);

            if (dummy_cmd.header.status == LEO_STATUS_GOOD)
            {
                /* compare country_code */
                do {} while (LEO_sys_data.param.country != LEO_country_code.u32_data);
#ifdef _ERRCHK
                if ( (retry_cntr) >= ((LEOCmdHeader *)backup_pointer)->reserve3 )
                    goto sys_read_end;
#else
                goto sys_read_end;
#endif
            }
        }

system_retry:
        if (leoChk_err_retry(dummy_cmd.header.sense))
            break;
        if (retry_cntr++ >= MAX_RETRY)
            break;
        if (leoChk_recal(retry_cntr))
        {
            dummy_cmd.header.sense = leoSend_asic_cmd_w(ASIC_RECAL, 0x00);
            if (dummy_cmd.header.sense)
                goto system_retry;
        }
    } while (1);

sys_read_end:
    /* Restore current command */
    LEOcur_command = backup_pointer;
#ifdef _ERRCHK
    switch (chk_cmd->command)
    {
        case LEO_COMMAND_READ_CAPACITY:
        case LEO_COMMAND_TRANSLATE:
            chk_cmd->reserve7 = retry_cntr;
        default: ;
    }
#endif
    return (chk_cmd->sense = dummy_cmd.header.sense);
}