Search
lxdream.org :: lxdream/src/gdrom/ide.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/ide.c
changeset 140:d26f4899898d
prev138:afabd7e6d26d
next142:2f631c3a3946
author nkeynes
date Sun Apr 30 12:22:31 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Fix IDENTIFY to be non-completely-broken
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@140
     2
 * $Id: ide.c,v 1.10 2006-04-30 12:22:31 nkeynes Exp $
nkeynes@2
     3
 *
nkeynes@31
     4
 * IDE interface implementation
nkeynes@31
     5
 *
nkeynes@31
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@31
     7
 *
nkeynes@31
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@31
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@31
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@31
    11
 * (at your option) any later version.
nkeynes@31
    12
 *
nkeynes@31
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@31
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@31
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@31
    16
 * GNU General Public License for more details.
nkeynes@2
    17
 */
nkeynes@35
    18
nkeynes@35
    19
#define MODULE ide_module
nkeynes@35
    20
nkeynes@138
    21
#include <assert.h>
nkeynes@2
    22
#include <stdlib.h>
nkeynes@35
    23
#include "dream.h"
nkeynes@125
    24
#include "asic.h"
nkeynes@39
    25
#include "gdrom/ide.h"
nkeynes@125
    26
#include "gdrom/gdrom.h"
nkeynes@2
    27
nkeynes@138
    28
#define MAX_WRITE_BUF 4096
nkeynes@138
    29
#define MAX_SECTOR_SIZE 2352 /* Audio sector */
nkeynes@138
    30
#define DEFAULT_DATA_SECTORS 8
nkeynes@2
    31
nkeynes@138
    32
static void ide_init( void );
nkeynes@138
    33
static void ide_reset( void );
nkeynes@138
    34
static void ide_save_state( FILE *f );
nkeynes@138
    35
static int ide_load_state( FILE *f );
nkeynes@138
    36
static void ide_raise_interrupt( void );
nkeynes@138
    37
static void ide_clear_interrupt( void );
nkeynes@15
    38
nkeynes@15
    39
struct dreamcast_module ide_module = { "IDE", ide_init, ide_reset, NULL, NULL,
nkeynes@138
    40
				       NULL, ide_save_state, ide_load_state };
nkeynes@15
    41
nkeynes@2
    42
struct ide_registers idereg;
nkeynes@2
    43
nkeynes@125
    44
static unsigned char command_buffer[12];
nkeynes@138
    45
unsigned char *data_buffer = NULL;
nkeynes@138
    46
uint32_t data_buffer_len = 0;
nkeynes@2
    47
nkeynes@2
    48
/* "\0\0\0\0\xb4\x19\0\0\x08SE      REV 6.42990316" */
nkeynes@125
    49
unsigned char gdrom_ident[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0xb4, 0x19, 0x00,
nkeynes@2
    50
                       0x00, 0x08, 0x53, 0x45, 0x20, 0x20, 0x20, 0x20,
nkeynes@2
    51
                       0x20, 0x20, 0x52, 0x65, 0x76, 0x20, 0x36, 0x2e,
nkeynes@2
    52
                       0x34, 0x32, 0x39, 0x39, 0x30, 0x33, 0x31, 0x36 };
nkeynes@2
    53
nkeynes@2
    54
nkeynes@138
    55
static void ide_init( void )
nkeynes@138
    56
{
nkeynes@138
    57
    ide_reset();
nkeynes@138
    58
    data_buffer_len = DEFAULT_DATA_SECTORS; 
nkeynes@138
    59
    data_buffer = malloc( MAX_SECTOR_SIZE * data_buffer_len ); 
nkeynes@138
    60
    assert( data_buffer != NULL );
nkeynes@138
    61
}
nkeynes@138
    62
nkeynes@138
    63
static void ide_reset( void )
nkeynes@138
    64
{
nkeynes@138
    65
    ide_clear_interrupt();
nkeynes@138
    66
    idereg.error = 0x01;
nkeynes@138
    67
    idereg.count = 0x01;
nkeynes@138
    68
    idereg.lba0 = /* 0x21; */ 0x81;
nkeynes@138
    69
    idereg.lba1 = 0x14;
nkeynes@138
    70
    idereg.lba2 = 0xeb;
nkeynes@138
    71
    idereg.feature = 0; /* Indeterminate really */
nkeynes@138
    72
    idereg.status = 0x00;
nkeynes@138
    73
    idereg.device = 0x00;
nkeynes@138
    74
    idereg.disc = gdrom_is_mounted() ? IDE_DISC_NONE : (IDE_DISC_CDROM|IDE_DISC_READY);
nkeynes@138
    75
}
nkeynes@138
    76
nkeynes@138
    77
static void ide_save_state( FILE *f )
nkeynes@138
    78
{
nkeynes@138
    79
    
nkeynes@138
    80
nkeynes@138
    81
}
nkeynes@138
    82
nkeynes@138
    83
static int ide_load_state( FILE *f )
nkeynes@138
    84
{
nkeynes@138
    85
    return 0;
nkeynes@138
    86
}
nkeynes@125
    87
nkeynes@125
    88
static void ide_set_write_buffer( unsigned char *buf, int len )
nkeynes@2
    89
{
nkeynes@2
    90
    idereg.status |= IDE_ST_DATA;
nkeynes@2
    91
    idereg.data = buf;
nkeynes@2
    92
    idereg.datalen = len;
nkeynes@2
    93
    idereg.writeptr = (uint16_t *)buf;
nkeynes@2
    94
    idereg.readptr = NULL;
nkeynes@2
    95
}
nkeynes@2
    96
nkeynes@125
    97
static void ide_set_read_buffer( unsigned char *buf, int len, int blocksize )
nkeynes@2
    98
{
nkeynes@2
    99
    idereg.status |= IDE_ST_DATA;
nkeynes@2
   100
    idereg.data = buf;
nkeynes@2
   101
    idereg.datalen = len;
nkeynes@2
   102
    idereg.readptr = (uint16_t *)buf;
nkeynes@2
   103
    idereg.writeptr = NULL;
nkeynes@2
   104
    idereg.lba1 = len&0xFF;
nkeynes@2
   105
    idereg.lba2 = len>>8;
nkeynes@125
   106
    idereg.blocksize = idereg.blockleft = blocksize;
nkeynes@2
   107
}
nkeynes@2
   108
nkeynes@125
   109
static void ide_raise_interrupt( void )
nkeynes@2
   110
{
nkeynes@125
   111
    if( idereg.intrq_pending == 0 ) {
nkeynes@125
   112
	idereg.intrq_pending = 1;
nkeynes@125
   113
	if( IS_IDE_IRQ_ENABLED() )
nkeynes@125
   114
	    asic_event( EVENT_IDE );
nkeynes@125
   115
    }
nkeynes@125
   116
}
nkeynes@125
   117
nkeynes@125
   118
static void ide_clear_interrupt( void ) 
nkeynes@125
   119
{
nkeynes@125
   120
    if( idereg.intrq_pending != 0 ) {
nkeynes@125
   121
	idereg.intrq_pending = 0;
nkeynes@125
   122
	if( IS_IDE_IRQ_ENABLED() )
nkeynes@125
   123
	    asic_clear_event( EVENT_IDE );
nkeynes@125
   124
    }
nkeynes@125
   125
}
nkeynes@125
   126
nkeynes@125
   127
static void ide_set_error( int error_code )
nkeynes@125
   128
{
nkeynes@125
   129
    idereg.status = 0x51;
nkeynes@125
   130
    idereg.error = error_code;
nkeynes@2
   131
}
nkeynes@2
   132
nkeynes@125
   133
uint8_t ide_read_status( void ) 
nkeynes@125
   134
{
nkeynes@125
   135
    if( (idereg.status & IDE_ST_BUSY) == 0 )
nkeynes@125
   136
	ide_clear_interrupt();
nkeynes@125
   137
    return idereg.status;
nkeynes@2
   138
}
nkeynes@2
   139
nkeynes@2
   140
uint16_t ide_read_data_pio( void ) {
nkeynes@2
   141
    if( idereg.readptr == NULL )
nkeynes@2
   142
        return 0xFFFF;
nkeynes@2
   143
    uint16_t rv = *idereg.readptr++;
nkeynes@2
   144
    idereg.datalen-=2;
nkeynes@125
   145
    idereg.blockleft-=2;
nkeynes@2
   146
    if( idereg.datalen <=0 ) {
nkeynes@2
   147
        idereg.readptr = NULL;
nkeynes@2
   148
        idereg.status &= ~IDE_ST_DATA;
nkeynes@125
   149
    } else if( idereg.blockleft <= 0 ) {
nkeynes@125
   150
	ide_raise_interrupt();
nkeynes@125
   151
	idereg.blockleft = idereg.blocksize;
nkeynes@2
   152
    }
nkeynes@2
   153
    return rv;
nkeynes@2
   154
}
nkeynes@2
   155
nkeynes@2
   156
void ide_write_data_pio( uint16_t val ) {
nkeynes@2
   157
    if( idereg.writeptr == NULL )
nkeynes@2
   158
        return;
nkeynes@2
   159
    *idereg.writeptr++ = val;
nkeynes@2
   160
    idereg.datalen-=2;
nkeynes@2
   161
    if( idereg.datalen <= 0 ) {
nkeynes@125
   162
	int len = ((unsigned char *)idereg.writeptr) - idereg.data;
nkeynes@2
   163
        idereg.writeptr = NULL;
nkeynes@2
   164
        idereg.status &= ~IDE_ST_DATA;
nkeynes@125
   165
        ide_write_buffer( idereg.data, len );
nkeynes@2
   166
    }
nkeynes@2
   167
}
nkeynes@2
   168
nkeynes@2
   169
void ide_write_control( uint8_t val ) {
nkeynes@125
   170
    if( IS_IDE_IRQ_ENABLED() ) {
nkeynes@125
   171
	if( (val & 0x02) != 0 && idereg.intrq_pending != 0 )
nkeynes@125
   172
	    asic_clear_event( EVENT_IDE );
nkeynes@125
   173
    } else {
nkeynes@125
   174
	if( (val & 0x02) == 0 && idereg.intrq_pending != 0 )
nkeynes@125
   175
	    asic_event( EVENT_IDE );
nkeynes@125
   176
    }
nkeynes@125
   177
    idereg.control = val;
nkeynes@2
   178
}
nkeynes@2
   179
nkeynes@2
   180
void ide_write_command( uint8_t val ) {
nkeynes@125
   181
    ide_clear_interrupt();
nkeynes@2
   182
    idereg.command = val;
nkeynes@2
   183
    switch( val ) {
nkeynes@39
   184
    case IDE_CMD_RESET_DEVICE:
nkeynes@39
   185
	ide_reset();
nkeynes@39
   186
	break;
nkeynes@39
   187
    case IDE_CMD_PACKET:
nkeynes@125
   188
	ide_set_write_buffer(command_buffer,12);
nkeynes@39
   189
	break;
nkeynes@39
   190
    case IDE_CMD_SET_FEATURE:
nkeynes@39
   191
	switch( idereg.feature ) {
nkeynes@47
   192
	case IDE_FEAT_SET_TRANSFER_MODE:
nkeynes@47
   193
	    switch( idereg.count & 0xF8 ) {
nkeynes@47
   194
	    case IDE_XFER_PIO:
nkeynes@47
   195
		INFO( "Set PIO default mode: %d", idereg.count&0x07 );
nkeynes@47
   196
		break;
nkeynes@47
   197
	    case IDE_XFER_PIO_FLOW:
nkeynes@47
   198
		INFO( "Set PIO Flow-control mode: %d", idereg.count&0x07 );
nkeynes@47
   199
		break;
nkeynes@47
   200
	    case IDE_XFER_MULTI_DMA:
nkeynes@47
   201
		INFO( "Set Multiword DMA mode: %d", idereg.count&0x07 );
nkeynes@47
   202
		break;
nkeynes@47
   203
	    case IDE_XFER_ULTRA_DMA:
nkeynes@47
   204
		INFO( "Set Ultra DMA mode: %d", idereg.count&0x07 );
nkeynes@47
   205
		break;
nkeynes@47
   206
	    default:
nkeynes@47
   207
		INFO( "Setting unknown transfer mode: %02X", idereg.count );
nkeynes@47
   208
		break;
nkeynes@47
   209
	    }
nkeynes@47
   210
	    break;
nkeynes@39
   211
	default:
nkeynes@39
   212
	    WARN( "IDE: unimplemented feature: %02X", idereg.feature );
nkeynes@39
   213
	}
nkeynes@125
   214
	ide_raise_interrupt( );
nkeynes@39
   215
	break;
nkeynes@39
   216
    default:
nkeynes@39
   217
	WARN( "IDE: Unimplemented command: %02X", val );
nkeynes@2
   218
    }
nkeynes@2
   219
    idereg.status |= IDE_ST_READY | IDE_ST_SERV;
nkeynes@2
   220
}
nkeynes@2
   221
nkeynes@125
   222
void ide_packet_command( unsigned char *cmd )
nkeynes@125
   223
{
nkeynes@138
   224
    uint32_t length, datalen;
nkeynes@125
   225
    uint32_t lba;
nkeynes@125
   226
    int blocksize = idereg.lba1 + (idereg.lba2<<8);
nkeynes@125
   227
nkeynes@125
   228
    ide_raise_interrupt( );
nkeynes@125
   229
    /* Okay we have the packet in the command buffer */
nkeynes@125
   230
    WARN( "ATAPI: Received Packet command: %02X", cmd[0] );
nkeynes@125
   231
    fwrite_dump( (unsigned char *)cmd, 12, stderr );
nkeynes@125
   232
    switch( cmd[0] ) {
nkeynes@125
   233
    case PKT_CMD_IDENTIFY:
nkeynes@125
   234
	/* NB: Bios sets cmd[4] = 0x08, no idea what this is for;
nkeynes@125
   235
	 * different values here appear to have no effect.
nkeynes@125
   236
	 */
nkeynes@140
   237
	lba = cmd[2];
nkeynes@140
   238
	if( lba >= sizeof(gdrom_ident) ) {
nkeynes@140
   239
	    ide_set_error(0x50);
nkeynes@140
   240
	    return;
nkeynes@140
   241
	}
nkeynes@140
   242
	length = cmd[4];
nkeynes@140
   243
	if( lba+length > sizeof(gdrom_ident) )
nkeynes@140
   244
	    length = sizeof(gdrom_ident) - lba;
nkeynes@140
   245
	ide_set_read_buffer(gdrom_ident + lba, length, blocksize);
nkeynes@125
   246
	break;
nkeynes@125
   247
    case PKT_CMD_READ_TOC:
nkeynes@138
   248
	if( !gdrom_get_toc( data_buffer ) ) {
nkeynes@138
   249
	    ide_set_error( 0x50 );
nkeynes@138
   250
	    return;
nkeynes@138
   251
	} 
nkeynes@138
   252
	ide_set_read_buffer( data_buffer, sizeof( struct gdrom_toc ), blocksize );
nkeynes@125
   253
	break;
nkeynes@125
   254
    case PKT_CMD_READ_SECTOR:
nkeynes@125
   255
	lba = cmd[2] << 16 | cmd[3] << 8 | cmd[4];
nkeynes@125
   256
	length = cmd[8] << 16 | cmd[9] << 8 | cmd[10]; /* blocks */
nkeynes@138
   257
	if( length > data_buffer_len ) {
nkeynes@138
   258
	    do {
nkeynes@138
   259
		data_buffer_len = data_buffer_len << 1;
nkeynes@138
   260
	    } while( data_buffer_len < length );
nkeynes@138
   261
	    data_buffer = realloc( data_buffer, data_buffer_len );
nkeynes@138
   262
	}
nkeynes@138
   263
	
nkeynes@138
   264
	datalen = gdrom_read_sectors( lba, length, data_buffer );
nkeynes@138
   265
	if( datalen == 0 ) {
nkeynes@125
   266
	    ide_set_error( 0x50 );
nkeynes@125
   267
	    return;
nkeynes@125
   268
	}
nkeynes@138
   269
	ide_set_read_buffer( data_buffer, datalen, blocksize );
nkeynes@125
   270
	break;
nkeynes@125
   271
    }
nkeynes@125
   272
}
nkeynes@125
   273
nkeynes@125
   274
void ide_write_buffer( unsigned char *data, int datalen ) {
nkeynes@2
   275
    switch( idereg.command ) {
nkeynes@2
   276
        case IDE_CMD_PACKET:
nkeynes@125
   277
	    ide_packet_command( data );
nkeynes@2
   278
            break;
nkeynes@2
   279
    }
nkeynes@2
   280
}
nkeynes@125
   281
nkeynes@125
   282
/**
nkeynes@125
   283
 * DMA read request
nkeynes@125
   284
 *
nkeynes@125
   285
 * This method is called from the ASIC side when a DMA read request is
nkeynes@125
   286
 * initiated. If there is a pending DMA transfer already, we copy the
nkeynes@125
   287
 * data immediately, otherwise we record the DMA buffer for use when we
nkeynes@125
   288
 * get to actually doing the transfer.
nkeynes@125
   289
 */
nkeynes@125
   290
void ide_dma_read_req( uint32_t addr, uint32_t length )
nkeynes@125
   291
{
nkeynes@125
   292
    
nkeynes@125
   293
nkeynes@125
   294
}
.