Search
lxdream.org :: lxdream/src/gdrom/ide.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/ide.c
changeset 245:a1d0655a88d3
prev240:9ae4bd697292
next250:84e056e12a19
author nkeynes
date Tue Dec 19 09:52:56 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Work in progress: 0x40,1 (read status)
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@245
     2
 * $Id: ide.c,v 1.18 2006-12-19 09:52:56 nkeynes Exp $
nkeynes@2
     3
 *
nkeynes@31
     4
 * IDE interface implementation
nkeynes@31
     5
 *
nkeynes@152
     6
 *
nkeynes@152
     7
 * Note: All references to read/write are from the host's point of view.
nkeynes@152
     8
 *
nkeynes@152
     9
 * See: INF-8020 
nkeynes@31
    10
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@31
    11
 *
nkeynes@31
    12
 * This program is free software; you can redistribute it and/or modify
nkeynes@31
    13
 * it under the terms of the GNU General Public License as published by
nkeynes@31
    14
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@31
    15
 * (at your option) any later version.
nkeynes@31
    16
 *
nkeynes@31
    17
 * This program is distributed in the hope that it will be useful,
nkeynes@31
    18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@31
    19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@31
    20
 * GNU General Public License for more details.
nkeynes@2
    21
 */
nkeynes@35
    22
nkeynes@35
    23
#define MODULE ide_module
nkeynes@35
    24
nkeynes@138
    25
#include <assert.h>
nkeynes@2
    26
#include <stdlib.h>
nkeynes@35
    27
#include "dream.h"
nkeynes@125
    28
#include "asic.h"
nkeynes@39
    29
#include "gdrom/ide.h"
nkeynes@125
    30
#include "gdrom/gdrom.h"
nkeynes@142
    31
#include "gdrom/packet.h"
nkeynes@2
    32
nkeynes@138
    33
#define MAX_WRITE_BUF 4096
nkeynes@138
    34
#define MAX_SECTOR_SIZE 2352 /* Audio sector */
nkeynes@138
    35
#define DEFAULT_DATA_SECTORS 8
nkeynes@2
    36
nkeynes@138
    37
static void ide_init( void );
nkeynes@138
    38
static void ide_reset( void );
nkeynes@138
    39
static void ide_save_state( FILE *f );
nkeynes@138
    40
static int ide_load_state( FILE *f );
nkeynes@138
    41
static void ide_raise_interrupt( void );
nkeynes@138
    42
static void ide_clear_interrupt( void );
nkeynes@152
    43
static void ide_packet_command( unsigned char *data );
nkeynes@15
    44
nkeynes@15
    45
struct dreamcast_module ide_module = { "IDE", ide_init, ide_reset, NULL, NULL,
nkeynes@138
    46
				       NULL, ide_save_state, ide_load_state };
nkeynes@15
    47
nkeynes@2
    48
struct ide_registers idereg;
nkeynes@138
    49
unsigned char *data_buffer = NULL;
nkeynes@138
    50
uint32_t data_buffer_len = 0;
nkeynes@2
    51
nkeynes@152
    52
#define WRITE_BUFFER(x16) *((uint16_t *)(data_buffer + idereg.data_offset)) = x16
nkeynes@152
    53
#define READ_BUFFER() *((uint16_t *)(data_buffer + idereg.data_offset))
nkeynes@152
    54
nkeynes@2
    55
/* "\0\0\0\0\xb4\x19\0\0\x08SE      REV 6.42990316" */
nkeynes@125
    56
unsigned char gdrom_ident[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0xb4, 0x19, 0x00,
nkeynes@2
    57
                       0x00, 0x08, 0x53, 0x45, 0x20, 0x20, 0x20, 0x20,
nkeynes@2
    58
                       0x20, 0x20, 0x52, 0x65, 0x76, 0x20, 0x36, 0x2e,
nkeynes@2
    59
                       0x34, 0x32, 0x39, 0x39, 0x30, 0x33, 0x31, 0x36 };
nkeynes@2
    60
nkeynes@158
    61
unsigned char gdrom_71[] = { 0x3E, 0x0F, 0x90, 0xBE,  0x1D, 0xD9, 0x89, 0x04,  0x28, 0x3A, 0x8E, 0x26,  0x5C, 0x95, 0x10, 0x5A,
nkeynes@158
    62
				 0x0A, 0x99, 0xEE, 0xFB,  0x69, 0xCE, 0xD9, 0x63,  0x00, 0xF5, 0x0A, 0xBC,  0x2C, 0x0D, 0xF8, 0xE2,
nkeynes@158
    63
				 0x05, 0x02, 0x00, 0x7C,  0x03, 0x00, 0x3D, 0x08,  0xD8, 0x8D, 0x08, 0x7A,  0x6D, 0x00, 0x35, 0x06,
nkeynes@158
    64
				 0xBA, 0x66, 0x10, 0x00,  0x91, 0x08, 0x10, 0x29,  0xD0, 0x45, 0xDA, 0x00,  0x2D, 0x05, 0x69, 0x09,
nkeynes@158
    65
				 0x00, 0x5E, 0x0F, 0x70,  0x86, 0x12, 0x6C, 0x77,  0x5A, 0xFB, 0xCD, 0x56,  0xFB, 0xF7, 0xB7, 0x00,
nkeynes@158
    66
				 0x5D, 0x07, 0x19, 0x99,  0xF2, 0xAF, 0x00, 0x63,  0x03, 0x00, 0xF0, 0x10,  0xBE, 0xD7, 0xA0, 0x63,
nkeynes@158
    67
				 0xFA, 0x84, 0xA7, 0x74,  0x94, 0xEF, 0xAD, 0xC2,  0xAC, 0x00, 0x78, 0x07,  0x9F, 0x57, 0x0B, 0x62,
nkeynes@158
    68
				 0x00, 0xFE, 0x08, 0x08,  0x5D, 0x5A, 0x6A, 0x54,  0x00, 0xE2, 0x09, 0x93,  0x7E, 0x62, 0x2A, 0x5E,
nkeynes@158
    69
				 0xDA, 0x00, 0x7E, 0x0F,  0xF0, 0x07, 0x01, 0x6D,  0x50, 0x86, 0xDD, 0x4A,  0x15, 0x54, 0xC7, 0xEC,
nkeynes@158
    70
				 0x00, 0xF2, 0x0B, 0x07,  0xF8, 0x1A, 0xB0, 0x99,  0x3B, 0xF1, 0x36, 0x00,  0x94, 0x07, 0x34, 0xE3,
nkeynes@158
    71
				 0xBC, 0x6E, 0x00, 0x34,  0x0D, 0x6F, 0xDA, 0xBD,  0xEE, 0xF7, 0xCC, 0xCE,  0x39, 0x7E, 0xE3, 0x00,
nkeynes@158
    72
				 0x14, 0x08, 0xDC, 0xD2,  0xB9, 0xF9, 0x31, 0x00,  0xB0, 0x0C, 0x10, 0xA3,  0x45, 0x12, 0xC7, 0xCD,
nkeynes@158
    73
				 0xBF, 0x05, 0x37, 0x00,  0xC4, 0x0D, 0x5F, 0xE0,  0x59, 0xBB, 0x01, 0x59,  0x03, 0xD6, 0x29, 0x9C,
nkeynes@158
    74
				 0x00, 0x01, 0x0A, 0x09,  0xAA, 0xA8, 0xA8, 0x24,  0x0B, 0x66, 0x00, 0x5C,  0x05, 0xA5, 0xCE, 0x00,
nkeynes@158
    75
				 0xC1, 0x0B, 0xB7, 0xA0,  0x6F, 0xE9, 0x2B, 0xCC,  0xB5, 0xFC, 0x00, 0x8D,  0x05, 0xF4, 0xAC, 0x00,
nkeynes@158
    76
				 0x57, 0x04, 0xB6, 0x00,  0xFC, 0x03, 0x00, 0xC3,  0x10, 0x43, 0x3B, 0xBE,  0xA2, 0x96, 0xC3, 0x65,
nkeynes@158
    77
				 0x9F, 0x9A, 0x88, 0xD5,  0x49, 0x68, 0x00, 0xDC,  0x11, 0x56, 0x23, 0x2D,  0xF9, 0xFC, 0xF5, 0x8B,
nkeynes@158
    78
				 0x1B, 0xB1, 0xB7, 0x10,  0x21, 0x1C, 0x12, 0x00,  0x0D, 0x0D, 0xEB, 0x86,  0xA2, 0x49, 0x8D, 0x8D,
nkeynes@158
    79
				 0xBE, 0xA1, 0x6D, 0x53,  0x00, 0xE1, 0x0A, 0x8E,  0x67, 0xAA, 0x16, 0x79,  0x39, 0x59, 0x00, 0x36,
nkeynes@158
    80
				 0x0B, 0x2A, 0x4E, 0xAE,  0x51, 0x4B, 0xD0, 0x66,  0x33, 0x00, 0x8A, 0x07,  0xCD, 0x6F, 0xBA, 0x92,
nkeynes@158
    81
				 0x00, 0x1A, 0x0E, 0xDF,  0x4A, 0xB3, 0x77, 0x1F,  0xA5, 0x90, 0x19, 0xFA,  0x59, 0xD7, 0x00, 0x04,
nkeynes@158
    82
				 0x0F, 0xAC, 0xCA, 0x9F,  0xA4, 0xFC, 0x6D, 0x90,  0x86, 0x9E, 0x1F, 0x44,  0x40, 0x00, 0x9F, 0x04,
nkeynes@158
    83
				 0x56, 0x00, 0x22, 0x03,  0x00, 0xB8, 0x10, 0x2C,  0x7A, 0x53, 0xA4, 0xBF,  0xA3, 0x90, 0x90, 0x14,
nkeynes@158
    84
				 0x9D, 0x46, 0x6C, 0x96,  0x00, 0xC6, 0x0B, 0x9B,  0xBB, 0xB0, 0xAE, 0x60,  0x92, 0x8E, 0x0C, 0x00,
nkeynes@158
    85
				 0x14, 0x06, 0x4B, 0xAE,  0x7F, 0x00, 0x5C, 0x0B,  0x23, 0xFA, 0xE7, 0x51,  0xDA, 0x61, 0x49, 0x5E,
nkeynes@158
    86
				 0x00, 0xD7, 0x0B, 0x01,  0xFC, 0x55, 0x31, 0x84,  0xC5, 0x0C, 0x98, 0x00,  0x97, 0x50, 0x6E, 0xF9,
nkeynes@158
    87
				 0xEE, 0x75, 0x92, 0x53,  0xD3, 0x66, 0xA4, 0xAF,  0x3B, 0xFE, 0x7B, 0x27,  0x30, 0xBB, 0xB6, 0xF2,
nkeynes@158
    88
				 0x76, 0x22, 0x45, 0x42,  0xCA, 0xF9, 0xF0, 0xDE,  0x9F, 0x45, 0x16, 0x68,  0x22, 0xB9, 0x84, 0x28,
nkeynes@158
    89
				 0x8F, 0x2B, 0xB5, 0x5C,  0xD2, 0xF5, 0x45, 0x36,  0x3E, 0x76, 0xC6, 0xBF,  0x32, 0x5C, 0x41, 0xA6,
nkeynes@158
    90
				 0x26, 0xC7, 0x82, 0x2F,  0x2E, 0xB5, 0x75, 0xC6,  0xE6, 0x67, 0x9E, 0x77,  0x94, 0xAF, 0x6A, 0x05,
nkeynes@158
    91
				 0xC0, 0x05, 0x61, 0x71,  0x89, 0x5A, 0xB1, 0xD0,  0xFC, 0x7E, 0xC0, 0x9B,  0xCB, 0x3B, 0x69, 0xD9,
nkeynes@158
    92
				 0x5F, 0xAF, 0xCA, 0xAB,  0x25, 0xD5, 0xBE, 0x8A,  0x6B, 0xB0, 0xFB, 0x61,  0x6C, 0xEB, 0x85, 0x6E,
nkeynes@158
    93
				 0x7A, 0x48, 0xFF, 0x97,  0x91, 0x06, 0x3D, 0x4D,  0x68, 0xD3, 0x65, 0x83,  0x90, 0xA0, 0x08, 0x5C,
nkeynes@158
    94
				 0xFC, 0xEE, 0x7C, 0x33,  0x43, 0x7F, 0x80, 0x52,  0x8B, 0x19, 0x72, 0xF2,  0xC9, 0xAB, 0x93, 0xAF,
nkeynes@158
    95
				 0x16, 0xED, 0x36, 0x48,  0xAB, 0xC9, 0xD1, 0x03,  0xB3, 0xDC, 0x2F, 0xF2,  0x92, 0x3F, 0x0A, 0x19,
nkeynes@158
    96
				 0x25, 0xE2, 0xEF, 0x7A,  0x22, 0xDA, 0xDB, 0xCB,  0x32, 0x12, 0x61, 0x49,  0x5B, 0x74, 0x7C, 0x65,
nkeynes@158
    97
				 0x20, 0x89, 0x54, 0x9E,  0x0E, 0xC9, 0x52, 0xE3,  0xC9, 0x9A, 0x44, 0xC9,  0x5D, 0xA6, 0x77, 0xC0,
nkeynes@158
    98
				 0xE7, 0x60, 0x91, 0x80,  0x50, 0x1F, 0x33, 0xB1,  0xCD, 0xAD, 0xF4, 0x0D,  0xBB, 0x08, 0xB1, 0xD0,
nkeynes@158
    99
				 0x13, 0x95, 0xAE, 0xC9,  0xE2, 0x64, 0xA2, 0x65,  0xFB, 0x8F, 0xE9, 0xA2,  0x8A, 0xBC, 0x98, 0x81,
nkeynes@158
   100
				 0x45, 0xB4, 0x55, 0x4E,  0xB9, 0x74, 0xB4, 0x50,  0x76, 0xBF, 0xF0, 0x45,  0xE7, 0xEE, 0x41, 0x64,
nkeynes@158
   101
				 0x9F, 0xB5, 0xE0, 0xBB,  0x1C, 0xBB, 0x28, 0x66,  0x1B, 0xDD, 0x2B, 0x02,  0x66, 0xBF, 0xFD, 0x7D,
nkeynes@158
   102
				 0x37, 0x35, 0x1D, 0x76,  0x21, 0xC3, 0x8F, 0xAF,  0xF6, 0xF9, 0xE9, 0x27,  0x48, 0xE7, 0x3D, 0x95,
nkeynes@158
   103
				 0x74, 0x0C, 0x77, 0x88,  0x56, 0xD9, 0x84, 0xC8,  0x7D, 0x20, 0x31, 0x43,  0x53, 0xF1, 0xC1, 0xC7,
nkeynes@158
   104
				 0xC9, 0xF7, 0x5C, 0xC0,  0xA6, 0x5A, 0x27, 0x0A,  0x41, 0xD4, 0x44, 0x94,  0x65, 0x4F, 0xE2, 0x53,
nkeynes@158
   105
				 0x60, 0x0B, 0xD1, 0x23,  0x6C, 0x0C, 0xBC, 0x70,  0x6C, 0x26, 0x1A, 0x61,  0x1D, 0x35, 0x88, 0xEC,
nkeynes@158
   106
				 0xB8, 0x15, 0xE3, 0xB4,  0x82, 0xEE, 0xB3, 0x21,  0xAC, 0x6C, 0xB7, 0x33,  0x6D, 0x78, 0x0C, 0x0D,
nkeynes@158
   107
				 0xB4, 0x0B, 0x29, 0xF2,  0xD4, 0x8C, 0x3F, 0xDD,  0x3F, 0x47, 0xDD, 0xF2,  0xD8, 0x39, 0x57, 0x20,
nkeynes@158
   108
				 0x28, 0xD8, 0xDD, 0x32,  0xE2, 0x6A, 0x47, 0x53,  0x57, 0xC6, 0xFA, 0x7A,  0x38, 0x30, 0x31, 0x8F,
nkeynes@158
   109
				 0xE7, 0xD3, 0x84, 0x2B,  0x5D, 0x4F, 0x95, 0x98,  0xED, 0x0B, 0xD7, 0x50,  0x0C, 0x49, 0xDA, 0x59,
nkeynes@158
   110
				 0x15, 0xF1, 0x39, 0xF3,  0x40, 0xDC, 0xDC, 0x25,  0x24, 0x56, 0x6E, 0xA9,  0x2F, 0xF0, 0x00, 0x00 };
nkeynes@158
   111
nkeynes@2
   112
nkeynes@138
   113
static void ide_init( void )
nkeynes@138
   114
{
nkeynes@138
   115
    ide_reset();
nkeynes@138
   116
    data_buffer_len = DEFAULT_DATA_SECTORS; 
nkeynes@138
   117
    data_buffer = malloc( MAX_SECTOR_SIZE * data_buffer_len ); 
nkeynes@138
   118
    assert( data_buffer != NULL );
nkeynes@138
   119
}
nkeynes@138
   120
nkeynes@138
   121
static void ide_reset( void )
nkeynes@138
   122
{
nkeynes@138
   123
    ide_clear_interrupt();
nkeynes@138
   124
    idereg.error = 0x01;
nkeynes@138
   125
    idereg.count = 0x01;
nkeynes@138
   126
    idereg.lba0 = /* 0x21; */ 0x81;
nkeynes@138
   127
    idereg.lba1 = 0x14;
nkeynes@138
   128
    idereg.lba2 = 0xeb;
nkeynes@138
   129
    idereg.feature = 0; /* Indeterminate really */
nkeynes@158
   130
    idereg.status = 0x50;
nkeynes@138
   131
    idereg.device = 0x00;
nkeynes@143
   132
    idereg.disc = gdrom_is_mounted() ? (IDE_DISC_CDROM|IDE_DISC_READY) : IDE_DISC_NONE;
nkeynes@152
   133
    idereg.state = IDE_STATE_IDLE;
nkeynes@152
   134
    memset( idereg.gdrom_sense, '\0', 10 );
nkeynes@152
   135
    idereg.data_offset = -1;
nkeynes@152
   136
    idereg.data_length = -1;
nkeynes@245
   137
    idereg.last_read_track = 1;
nkeynes@245
   138
    idereg.last_read_lba = 150;
nkeynes@245
   139
    idereg.last_read_count = 0;
nkeynes@138
   140
}
nkeynes@138
   141
nkeynes@138
   142
static void ide_save_state( FILE *f )
nkeynes@138
   143
{
nkeynes@152
   144
    fwrite( &idereg, sizeof(idereg), 1, f );
nkeynes@152
   145
    fwrite( &data_buffer_len, sizeof(data_buffer_len), 1, f );
nkeynes@158
   146
    fwrite( data_buffer, MAX_SECTOR_SIZE, data_buffer_len, f );
nkeynes@138
   147
}
nkeynes@138
   148
nkeynes@138
   149
static int ide_load_state( FILE *f )
nkeynes@138
   150
{
nkeynes@152
   151
    uint32_t length;
nkeynes@152
   152
    fread( &idereg, sizeof(idereg), 1, f );
nkeynes@152
   153
    fread( &length, sizeof(uint32_t), 1, f );
nkeynes@152
   154
    if( length > data_buffer_len ) {
nkeynes@152
   155
	if( data_buffer != NULL )
nkeynes@152
   156
	    free( data_buffer );
nkeynes@158
   157
	data_buffer = malloc( MAX_SECTOR_SIZE * length  );
nkeynes@152
   158
	assert( data_buffer != NULL );
nkeynes@152
   159
	data_buffer_len = length;
nkeynes@152
   160
    }
nkeynes@158
   161
    fread( data_buffer, MAX_SECTOR_SIZE, length, f );
nkeynes@138
   162
    return 0;
nkeynes@138
   163
}
nkeynes@125
   164
nkeynes@152
   165
/************************ State transitions *************************/
nkeynes@152
   166
nkeynes@158
   167
void ide_set_packet_result( uint16_t result )
nkeynes@158
   168
{
nkeynes@158
   169
    idereg.gdrom_sense[0] = 0xf0;
nkeynes@158
   170
    idereg.gdrom_sense[2] = result & 0xFF;
nkeynes@158
   171
    idereg.gdrom_sense[8] = (result >> 8) & 0xFF;
nkeynes@158
   172
    idereg.error = (result & 0x0F) << 4;
nkeynes@158
   173
    if( result != 0 ) {
nkeynes@158
   174
	idereg.status = 0x51;
nkeynes@158
   175
	ide_raise_interrupt();
nkeynes@158
   176
    } else {
nkeynes@158
   177
	idereg.status = idereg.status & ~(IDE_STATUS_BSY|IDE_STATUS_CHK);
nkeynes@158
   178
    }
nkeynes@158
   179
}
nkeynes@158
   180
nkeynes@152
   181
/**
nkeynes@152
   182
 * Begin command packet write to the device. This is always 12 bytes of PIO data
nkeynes@152
   183
 */
nkeynes@152
   184
static void ide_start_command_packet_write( )
nkeynes@2
   185
{
nkeynes@152
   186
    idereg.state = IDE_STATE_CMD_WRITE;
nkeynes@152
   187
    idereg.status = IDE_STATUS_DRDY | IDE_STATUS_DRQ;
nkeynes@152
   188
    idereg.error = idereg.feature & 0x03; /* Copy values of OVL/DMA */
nkeynes@152
   189
    idereg.count = IDE_COUNT_CD;
nkeynes@152
   190
    idereg.data_offset = 0;
nkeynes@152
   191
    idereg.data_length = 12;
nkeynes@2
   192
}
nkeynes@2
   193
nkeynes@152
   194
/**
nkeynes@152
   195
 * Begin PIO read from the device. The data is assumed to already be
nkeynes@152
   196
 * in the buffer at this point.
nkeynes@152
   197
 */
nkeynes@152
   198
static void ide_start_read( int length, int blocksize, gboolean dma ) 
nkeynes@2
   199
{
nkeynes@152
   200
    idereg.count = IDE_COUNT_IO;
nkeynes@152
   201
    idereg.data_length = length;
nkeynes@152
   202
    idereg.data_offset = 0;
nkeynes@152
   203
    if( dma ) {
nkeynes@152
   204
	idereg.state = IDE_STATE_DMA_READ;
nkeynes@158
   205
	idereg.status = 0xD0;
nkeynes@152
   206
    } else {
nkeynes@152
   207
	idereg.state = IDE_STATE_PIO_READ;
nkeynes@158
   208
	idereg.status = IDE_STATUS_DRDY | IDE_STATUS_DRQ;
nkeynes@152
   209
	idereg.lba1 = length & 0xFF;
nkeynes@152
   210
	idereg.lba2 = (length >> 8) & 0xFF;
nkeynes@152
   211
	//	idereg.lba1 = blocksize & 0xFF;
nkeynes@152
   212
	//	idereg.lba2 = blocksize >> 8; 
nkeynes@152
   213
	idereg.block_length = blocksize;
nkeynes@152
   214
	idereg.block_left = blocksize;
nkeynes@158
   215
	ide_raise_interrupt( );
nkeynes@152
   216
    }
nkeynes@152
   217
}
nkeynes@152
   218
nkeynes@152
   219
static void ide_start_packet_read( int length, int blocksize )
nkeynes@152
   220
{
nkeynes@158
   221
    ide_set_packet_result( PKT_ERR_OK );
nkeynes@158
   222
    ide_start_read( length, blocksize, (idereg.feature & IDE_FEAT_DMA) ? TRUE : FALSE );
nkeynes@2
   223
}
nkeynes@2
   224
nkeynes@125
   225
static void ide_raise_interrupt( void )
nkeynes@2
   226
{
nkeynes@125
   227
    if( idereg.intrq_pending == 0 ) {
nkeynes@125
   228
	idereg.intrq_pending = 1;
nkeynes@125
   229
	if( IS_IDE_IRQ_ENABLED() )
nkeynes@125
   230
	    asic_event( EVENT_IDE );
nkeynes@125
   231
    }
nkeynes@125
   232
}
nkeynes@125
   233
nkeynes@125
   234
static void ide_clear_interrupt( void ) 
nkeynes@125
   235
{
nkeynes@125
   236
    if( idereg.intrq_pending != 0 ) {
nkeynes@125
   237
	idereg.intrq_pending = 0;
nkeynes@125
   238
	if( IS_IDE_IRQ_ENABLED() )
nkeynes@125
   239
	    asic_clear_event( EVENT_IDE );
nkeynes@125
   240
    }
nkeynes@125
   241
}
nkeynes@125
   242
nkeynes@125
   243
static void ide_set_error( int error_code )
nkeynes@125
   244
{
nkeynes@125
   245
    idereg.status = 0x51;
nkeynes@125
   246
    idereg.error = error_code;
nkeynes@2
   247
}
nkeynes@2
   248
nkeynes@125
   249
uint8_t ide_read_status( void ) 
nkeynes@125
   250
{
nkeynes@152
   251
    if( (idereg.status & IDE_STATUS_BSY) == 0 )
nkeynes@125
   252
	ide_clear_interrupt();
nkeynes@125
   253
    return idereg.status;
nkeynes@2
   254
}
nkeynes@2
   255
nkeynes@2
   256
uint16_t ide_read_data_pio( void ) {
nkeynes@152
   257
    if( idereg.state == IDE_STATE_PIO_READ ) {
nkeynes@152
   258
	uint16_t rv = READ_BUFFER();
nkeynes@152
   259
	idereg.data_offset += 2;
nkeynes@152
   260
	idereg.block_left -= 2;
nkeynes@152
   261
	if( idereg.data_offset >= idereg.data_length ) {
nkeynes@152
   262
	    idereg.state = IDE_STATE_IDLE;
nkeynes@152
   263
	    idereg.status &= ~IDE_STATUS_DRQ;
nkeynes@152
   264
	    idereg.data_offset = -1;
nkeynes@152
   265
	    ide_raise_interrupt();
nkeynes@152
   266
	} else if( idereg.block_left <= 0 ) {
nkeynes@152
   267
	    idereg.block_left = idereg.block_length;
nkeynes@152
   268
	    ide_raise_interrupt();
nkeynes@152
   269
	}
nkeynes@152
   270
	return rv;
nkeynes@152
   271
    } else {
nkeynes@2
   272
        return 0xFFFF;
nkeynes@2
   273
    }
nkeynes@2
   274
}
nkeynes@2
   275
nkeynes@152
   276
nkeynes@152
   277
/**
nkeynes@152
   278
 * DMA read request
nkeynes@152
   279
 *
nkeynes@152
   280
 * This method is called from the ASIC side when a DMA read request is
nkeynes@152
   281
 * initiated. If there is a pending DMA transfer already, we copy the
nkeynes@152
   282
 * data immediately, otherwise we record the DMA buffer for use when we
nkeynes@152
   283
 * get to actually doing the transfer.
nkeynes@152
   284
 * @return number of bytes transfered
nkeynes@152
   285
 */
nkeynes@152
   286
uint32_t ide_read_data_dma( uint32_t addr, uint32_t length )
nkeynes@152
   287
{
nkeynes@152
   288
    if( idereg.state == IDE_STATE_DMA_READ ) {
nkeynes@152
   289
	int xferlen = length;
nkeynes@152
   290
	int remaining = idereg.data_length - idereg.data_offset;
nkeynes@152
   291
	if( xferlen > remaining )
nkeynes@152
   292
	    xferlen = remaining;
nkeynes@152
   293
	mem_copy_to_sh4( addr, data_buffer + idereg.data_offset, xferlen );
nkeynes@152
   294
	idereg.data_offset += xferlen;
nkeynes@152
   295
	if( idereg.data_offset >= idereg.data_length ) {
nkeynes@152
   296
	    idereg.data_offset = -1;
nkeynes@152
   297
	    idereg.state = IDE_STATE_IDLE;
nkeynes@158
   298
	    idereg.status = 0x50;
nkeynes@152
   299
	    ide_raise_interrupt();
nkeynes@158
   300
	    asic_event( EVENT_IDE_DMA );
nkeynes@152
   301
	}
nkeynes@152
   302
	return xferlen;
nkeynes@152
   303
    }
nkeynes@152
   304
    return 0;
nkeynes@152
   305
}
nkeynes@152
   306
nkeynes@2
   307
void ide_write_data_pio( uint16_t val ) {
nkeynes@152
   308
    if( idereg.state == IDE_STATE_CMD_WRITE ) {
nkeynes@152
   309
	WRITE_BUFFER(val);
nkeynes@152
   310
	idereg.data_offset+=2;
nkeynes@152
   311
	if( idereg.data_offset >= idereg.data_length ) {
nkeynes@152
   312
	    idereg.state = IDE_STATE_BUSY;
nkeynes@152
   313
	    idereg.status = (idereg.status & ~IDE_STATUS_DRQ) | IDE_STATUS_BSY;
nkeynes@152
   314
	    idereg.data_offset = -1;
nkeynes@152
   315
	    ide_packet_command(data_buffer);
nkeynes@152
   316
	}
nkeynes@152
   317
    } else if( idereg.state == IDE_STATE_PIO_WRITE ) {
nkeynes@152
   318
	WRITE_BUFFER(val);
nkeynes@152
   319
	if( idereg.data_offset >= idereg.data_length ) {
nkeynes@152
   320
	    idereg.state = IDE_STATE_BUSY;
nkeynes@152
   321
	    idereg.data_offset = -1;
nkeynes@152
   322
	    idereg.status = (idereg.status & ~IDE_STATUS_DRQ) | IDE_STATUS_BSY;
nkeynes@152
   323
	    /* ??? - no data writes yet anyway */
nkeynes@152
   324
	}
nkeynes@2
   325
    }
nkeynes@2
   326
}
nkeynes@2
   327
nkeynes@2
   328
void ide_write_control( uint8_t val ) {
nkeynes@125
   329
    if( IS_IDE_IRQ_ENABLED() ) {
nkeynes@125
   330
	if( (val & 0x02) != 0 && idereg.intrq_pending != 0 )
nkeynes@125
   331
	    asic_clear_event( EVENT_IDE );
nkeynes@125
   332
    } else {
nkeynes@125
   333
	if( (val & 0x02) == 0 && idereg.intrq_pending != 0 )
nkeynes@125
   334
	    asic_event( EVENT_IDE );
nkeynes@125
   335
    }
nkeynes@125
   336
    idereg.control = val;
nkeynes@2
   337
}
nkeynes@2
   338
nkeynes@2
   339
void ide_write_command( uint8_t val ) {
nkeynes@125
   340
    ide_clear_interrupt();
nkeynes@2
   341
    idereg.command = val;
nkeynes@2
   342
    switch( val ) {
nkeynes@240
   343
    case IDE_CMD_NOP: /* Effectively an "abort" */
nkeynes@240
   344
	idereg.state = IDE_STATE_IDLE;
nkeynes@240
   345
	idereg.status = 0x51;
nkeynes@240
   346
	idereg.error = 0x04;
nkeynes@240
   347
	idereg.data_offset = -1;
nkeynes@240
   348
	ide_raise_interrupt();
nkeynes@240
   349
	return;
nkeynes@39
   350
    case IDE_CMD_RESET_DEVICE:
nkeynes@39
   351
	ide_reset();
nkeynes@39
   352
	break;
nkeynes@39
   353
    case IDE_CMD_PACKET:
nkeynes@152
   354
	ide_start_command_packet_write();
nkeynes@39
   355
	break;
nkeynes@39
   356
    case IDE_CMD_SET_FEATURE:
nkeynes@39
   357
	switch( idereg.feature ) {
nkeynes@47
   358
	case IDE_FEAT_SET_TRANSFER_MODE:
nkeynes@47
   359
	    switch( idereg.count & 0xF8 ) {
nkeynes@47
   360
	    case IDE_XFER_PIO:
nkeynes@47
   361
		INFO( "Set PIO default mode: %d", idereg.count&0x07 );
nkeynes@47
   362
		break;
nkeynes@47
   363
	    case IDE_XFER_PIO_FLOW:
nkeynes@47
   364
		INFO( "Set PIO Flow-control mode: %d", idereg.count&0x07 );
nkeynes@47
   365
		break;
nkeynes@47
   366
	    case IDE_XFER_MULTI_DMA:
nkeynes@47
   367
		INFO( "Set Multiword DMA mode: %d", idereg.count&0x07 );
nkeynes@47
   368
		break;
nkeynes@47
   369
	    case IDE_XFER_ULTRA_DMA:
nkeynes@47
   370
		INFO( "Set Ultra DMA mode: %d", idereg.count&0x07 );
nkeynes@47
   371
		break;
nkeynes@47
   372
	    default:
nkeynes@47
   373
		INFO( "Setting unknown transfer mode: %02X", idereg.count );
nkeynes@47
   374
		break;
nkeynes@47
   375
	    }
nkeynes@47
   376
	    break;
nkeynes@39
   377
	default:
nkeynes@39
   378
	    WARN( "IDE: unimplemented feature: %02X", idereg.feature );
nkeynes@39
   379
	}
nkeynes@125
   380
	ide_raise_interrupt( );
nkeynes@39
   381
	break;
nkeynes@39
   382
    default:
nkeynes@39
   383
	WARN( "IDE: Unimplemented command: %02X", val );
nkeynes@2
   384
    }
nkeynes@152
   385
    idereg.status = (idereg.status | IDE_STATUS_DRDY | IDE_STATUS_SERV) & (~IDE_STATUS_CHK);
nkeynes@2
   386
}
nkeynes@2
   387
nkeynes@142
   388
/**
nkeynes@142
   389
 * Execute a packet command. This particular method is responsible for parsing
nkeynes@142
   390
 * the command buffers (12 bytes), and generating the appropriate responses, 
nkeynes@142
   391
 * although the actual implementation is mostly delegated to gdrom.c
nkeynes@142
   392
 */
nkeynes@125
   393
void ide_packet_command( unsigned char *cmd )
nkeynes@125
   394
{
nkeynes@138
   395
    uint32_t length, datalen;
nkeynes@143
   396
    uint32_t lba, status;
nkeynes@143
   397
    int mode;
nkeynes@125
   398
    int blocksize = idereg.lba1 + (idereg.lba2<<8);
nkeynes@125
   399
nkeynes@125
   400
    /* Okay we have the packet in the command buffer */
nkeynes@166
   401
    INFO( "ATAPI packet: %02X %02X %02X %02X  %02X %02X %02X %02X  %02X %02X %02X %02X", 
nkeynes@166
   402
	  cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], cmd[5], cmd[6], cmd[7],
nkeynes@166
   403
	  cmd[8], cmd[9], cmd[10], cmd[11] );
nkeynes@125
   404
    switch( cmd[0] ) {
nkeynes@142
   405
    case PKT_CMD_TEST_READY:
nkeynes@142
   406
	if( !gdrom_is_mounted() ) {
nkeynes@152
   407
	    ide_set_packet_result( PKT_ERR_NODISC );
nkeynes@158
   408
	} else {
nkeynes@158
   409
	    ide_set_packet_result( 0 );
nkeynes@158
   410
	    ide_raise_interrupt();
nkeynes@158
   411
	    idereg.status = 0x50;
nkeynes@142
   412
	}
nkeynes@142
   413
	break;
nkeynes@125
   414
    case PKT_CMD_IDENTIFY:
nkeynes@140
   415
	lba = cmd[2];
nkeynes@140
   416
	if( lba >= sizeof(gdrom_ident) ) {
nkeynes@142
   417
	    ide_set_error(PKT_ERR_BADFIELD);
nkeynes@158
   418
	} else {
nkeynes@158
   419
	    length = cmd[4];
nkeynes@158
   420
	    if( lba+length > sizeof(gdrom_ident) )
nkeynes@158
   421
		length = sizeof(gdrom_ident) - lba;
nkeynes@158
   422
	    memcpy( data_buffer, gdrom_ident + lba, length );
nkeynes@158
   423
	    ide_start_packet_read( length, blocksize );
nkeynes@140
   424
	}
nkeynes@125
   425
	break;
nkeynes@142
   426
    case PKT_CMD_SENSE:
nkeynes@142
   427
	length = cmd[4];
nkeynes@142
   428
	if( length > 10 )
nkeynes@142
   429
	    length = 10;
nkeynes@152
   430
	memcpy( data_buffer, idereg.gdrom_sense, length );
nkeynes@152
   431
	ide_start_packet_read( length, blocksize );
nkeynes@142
   432
	break;
nkeynes@125
   433
    case PKT_CMD_READ_TOC:
nkeynes@142
   434
	length = (cmd[3]<<8) | cmd[4];
nkeynes@142
   435
	if( length > sizeof(struct gdrom_toc) )
nkeynes@142
   436
	    length = sizeof(struct gdrom_toc);
nkeynes@152
   437
nkeynes@152
   438
	status = gdrom_get_toc( data_buffer );
nkeynes@152
   439
	if( status != PKT_ERR_OK ) {
nkeynes@152
   440
	    ide_set_packet_result( status );
nkeynes@158
   441
	} else {
nkeynes@158
   442
	    ide_start_packet_read( length, blocksize );
nkeynes@152
   443
	}
nkeynes@125
   444
	break;
nkeynes@158
   445
    case PKT_CMD_SESSION_INFO:
nkeynes@149
   446
	length = cmd[4];
nkeynes@149
   447
	if( length > 6 )
nkeynes@149
   448
	    length = 6;
nkeynes@152
   449
	status = gdrom_get_info( data_buffer, cmd[2] );
nkeynes@152
   450
	if( status != PKT_ERR_OK ) {
nkeynes@152
   451
	    ide_set_packet_result( status );
nkeynes@158
   452
	} else {
nkeynes@158
   453
	    ide_start_packet_read( length, blocksize );
nkeynes@152
   454
	}
nkeynes@149
   455
	break;
nkeynes@125
   456
    case PKT_CMD_READ_SECTOR:
nkeynes@125
   457
	lba = cmd[2] << 16 | cmd[3] << 8 | cmd[4];
nkeynes@125
   458
	length = cmd[8] << 16 | cmd[9] << 8 | cmd[10]; /* blocks */
nkeynes@143
   459
	switch( cmd[1] ) {
nkeynes@245
   460
	case 0x20: mode = GDROM_MODE1; break;     /* TODO - might be unchecked? */
nkeynes@143
   461
	case 0x24: mode = GDROM_GD; break;
nkeynes@245
   462
	case 0x28: mode = GDROM_MODE2_XA1; break; /* ??? */
nkeynes@143
   463
	case 0x30: mode = GDROM_RAW; break;
nkeynes@143
   464
	default:
nkeynes@143
   465
	    ERROR( "Unrecognized read mode '%02X' in GD-Rom read request", cmd[1] );
nkeynes@152
   466
	    ide_set_packet_result( PKT_ERR_BADFIELD );
nkeynes@142
   467
	    return;
nkeynes@142
   468
	}
nkeynes@158
   469
nkeynes@158
   470
	if( length > data_buffer_len ) {
nkeynes@158
   471
	    do {
nkeynes@158
   472
		data_buffer_len = data_buffer_len << 1;
nkeynes@158
   473
	    } while( data_buffer_len < length );
nkeynes@158
   474
	    data_buffer = realloc( data_buffer, MAX_SECTOR_SIZE * data_buffer_len );
nkeynes@158
   475
	}
nkeynes@158
   476
nkeynes@152
   477
	datalen = data_buffer_len;
nkeynes@152
   478
	status = gdrom_read_sectors( lba, length, mode, data_buffer, &datalen );
nkeynes@143
   479
	if( status != 0 ) {
nkeynes@152
   480
	    ide_set_packet_result( status );
nkeynes@152
   481
	    idereg.gdrom_sense[5] = (lba >> 16) & 0xFF;
nkeynes@152
   482
	    idereg.gdrom_sense[6] = (lba >> 8) & 0xFF;
nkeynes@152
   483
	    idereg.gdrom_sense[7] = lba & 0xFF;
nkeynes@158
   484
	} else {
nkeynes@245
   485
	    idereg.last_read_count += length;
nkeynes@245
   486
	    idereg.last_read_lba = lba + length;
nkeynes@245
   487
	    idereg.last_read_track = gdrom_get_track_no_by_lba( idereg.last_read_lba );
nkeynes@158
   488
	    ide_start_packet_read( datalen, blocksize );
nkeynes@125
   489
	}
nkeynes@125
   490
	break;
nkeynes@149
   491
    case PKT_CMD_SPIN_UP:
nkeynes@149
   492
	/* do nothing? */
nkeynes@158
   493
	ide_set_packet_result( PKT_ERR_OK );
nkeynes@158
   494
	ide_raise_interrupt();
nkeynes@158
   495
	break;
nkeynes@245
   496
    case PKT_CMD_STATUS:
nkeynes@245
   497
	length = cmd[4];
nkeynes@245
   498
	if( !gdrom_is_mounted() ) {
nkeynes@245
   499
	    ide_set_packet_result( PKT_ERR_NODISC );
nkeynes@245
   500
	} else {
nkeynes@245
   501
	    switch( cmd[1] ) {
nkeynes@245
   502
	    case 0:
nkeynes@245
   503
		break;
nkeynes@245
   504
	    case 1:
nkeynes@245
   505
		if( length > 16 ) {
nkeynes@245
   506
		    length = 16;
nkeynes@245
   507
		}
nkeynes@245
   508
		data_buffer[0] = 0x00;
nkeynes@245
   509
		data_buffer[1] = 0x15; /* ??? */
nkeynes@245
   510
		data_buffer[2] = 0x00;
nkeynes@245
   511
		data_buffer[3] = 0x0E;
nkeynes@245
   512
		data_buffer[4] = gdrom_get_track(idereg.last_read_track)->flags;
nkeynes@245
   513
		data_buffer[5] = idereg.last_read_track;
nkeynes@245
   514
		data_buffer[6] = 0x01; /* ?? */
nkeynes@245
   515
		data_buffer[7] = (idereg.last_read_count >> 16) & 0xFF;
nkeynes@245
   516
		data_buffer[8] = (idereg.last_read_count >> 8) & 0xFF;
nkeynes@245
   517
		data_buffer[9] = idereg.last_read_count & 0xFF;
nkeynes@245
   518
		data_buffer[10] = (idereg.last_read_lba >> 24) & 0xFF;
nkeynes@245
   519
		data_buffer[11] = (idereg.last_read_lba >> 16) & 0xFF;
nkeynes@245
   520
		data_buffer[12] = (idereg.last_read_lba >> 8) & 0xFF;
nkeynes@245
   521
		data_buffer[13] = idereg.last_read_lba & 0xFF;
nkeynes@245
   522
		data_buffer[14] = 0x00;
nkeynes@245
   523
		data_buffer[15] = 0x00;
nkeynes@245
   524
		ide_start_packet_read( length, blocksize );
nkeynes@245
   525
		break;
nkeynes@245
   526
	    }
nkeynes@245
   527
	}
nkeynes@245
   528
	break;
nkeynes@158
   529
    case PKT_CMD_71:
nkeynes@158
   530
	/* This is a weird one. As far as I can tell it returns random garbage
nkeynes@158
   531
	 * (and not even the same length each time, never mind the same data).
nkeynes@158
   532
	 * For sake of something to do, it returns the results from a test dump
nkeynes@158
   533
	 */
nkeynes@158
   534
	memcpy( data_buffer, gdrom_71, sizeof(gdrom_71) );
nkeynes@158
   535
	ide_start_packet_read( sizeof(gdrom_71), blocksize );
nkeynes@149
   536
	break;
nkeynes@142
   537
    default:
nkeynes@152
   538
	ide_set_packet_result( PKT_ERR_BADCMD ); /* Invalid command */
nkeynes@142
   539
	return;
nkeynes@125
   540
    }
nkeynes@125
   541
}
.