Search
lxdream.org :: lxdream/src/asic.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/asic.c
changeset 125:49bf45f8210a
prev100:995e42e96cc9
next137:41907543d890
author nkeynes
date Wed Mar 22 14:29:02 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Rename IDE DMA registers appropriately
Remove forced irq hack
Add correct irq handling for IDE
Miscellaneous WIP for the GD-rom drive
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@125
     2
 * $Id: asic.c,v 1.13 2006-03-22 14:29:00 nkeynes Exp $
nkeynes@31
     3
 *
nkeynes@31
     4
 * Support for the miscellaneous ASIC functions (Primarily event multiplexing,
nkeynes@31
     5
 * and DMA). 
nkeynes@31
     6
 *
nkeynes@31
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@31
     8
 *
nkeynes@31
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@31
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@31
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@31
    12
 * (at your option) any later version.
nkeynes@31
    13
 *
nkeynes@31
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@31
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@31
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@31
    17
 * GNU General Public License for more details.
nkeynes@31
    18
 */
nkeynes@35
    19
nkeynes@35
    20
#define MODULE asic_module
nkeynes@35
    21
nkeynes@1
    22
#include <assert.h>
nkeynes@1
    23
#include "dream.h"
nkeynes@1
    24
#include "mem.h"
nkeynes@1
    25
#include "sh4/intc.h"
nkeynes@56
    26
#include "sh4/dmac.h"
nkeynes@2
    27
#include "dreamcast.h"
nkeynes@25
    28
#include "maple/maple.h"
nkeynes@25
    29
#include "gdrom/ide.h"
nkeynes@15
    30
#include "asic.h"
nkeynes@1
    31
#define MMIO_IMPL
nkeynes@1
    32
#include "asic.h"
nkeynes@1
    33
/*
nkeynes@1
    34
 * Open questions:
nkeynes@1
    35
 *   1) Does changing the mask after event occurance result in the
nkeynes@1
    36
 *      interrupt being delivered immediately?
nkeynes@1
    37
 * TODO: Logic diagram of ASIC event/interrupt logic.
nkeynes@1
    38
 *
nkeynes@1
    39
 * ... don't even get me started on the "EXTDMA" page, about which, apparently,
nkeynes@1
    40
 * practically nothing is publicly known...
nkeynes@1
    41
 */
nkeynes@1
    42
nkeynes@15
    43
struct dreamcast_module asic_module = { "ASIC", asic_init, NULL, NULL, NULL,
nkeynes@23
    44
					NULL, NULL, NULL };
nkeynes@15
    45
nkeynes@20
    46
void asic_check_cleared_events( void );
nkeynes@20
    47
nkeynes@1
    48
void asic_init( void )
nkeynes@1
    49
{
nkeynes@1
    50
    register_io_region( &mmio_region_ASIC );
nkeynes@1
    51
    register_io_region( &mmio_region_EXTDMA );
nkeynes@1
    52
    mmio_region_ASIC.trace_flag = 0; /* Because this is called so often */
nkeynes@1
    53
}
nkeynes@1
    54
nkeynes@1
    55
void mmio_region_ASIC_write( uint32_t reg, uint32_t val )
nkeynes@1
    56
{
nkeynes@1
    57
    switch( reg ) {
nkeynes@125
    58
    case PIRQ1:
nkeynes@125
    59
	val = val & 0xFFFFFFFE; /* Prevent the IDE event from clearing */
nkeynes@125
    60
	/* fallthrough */
nkeynes@56
    61
    case PIRQ0:
nkeynes@56
    62
    case PIRQ2:
nkeynes@56
    63
	/* Clear any interrupts */
nkeynes@56
    64
	MMIO_WRITE( ASIC, reg, MMIO_READ(ASIC, reg)&~val );
nkeynes@56
    65
	asic_check_cleared_events();
nkeynes@56
    66
	break;
nkeynes@56
    67
    case MAPLE_STATE:
nkeynes@56
    68
	MMIO_WRITE( ASIC, reg, val );
nkeynes@56
    69
	if( val & 1 ) {
nkeynes@56
    70
	    uint32_t maple_addr = MMIO_READ( ASIC, MAPLE_DMA) &0x1FFFFFE0;
nkeynes@56
    71
	    WARN( "Maple request initiated at %08X, halting", maple_addr );
nkeynes@56
    72
	    maple_handle_buffer( maple_addr );
nkeynes@56
    73
	    MMIO_WRITE( ASIC, reg, 0 );
nkeynes@56
    74
	}
nkeynes@56
    75
	break;
nkeynes@56
    76
    case PVRDMACTL: /* Initiate PVR DMA transfer */
nkeynes@94
    77
	MMIO_WRITE( ASIC, reg, val );
nkeynes@94
    78
	WARN( "Write to ASIC (%03X <= %08X) [%s: %s]",
nkeynes@94
    79
	      reg, val, MMIO_REGID(ASIC,reg), MMIO_REGDESC(ASIC,reg) );
nkeynes@56
    80
	if( val & 1 ) {
nkeynes@56
    81
	    uint32_t dest_addr = MMIO_READ( ASIC, PVRDMADEST) &0x1FFFFFE0;
nkeynes@56
    82
	    uint32_t count = MMIO_READ( ASIC, PVRDMACNT );
nkeynes@56
    83
	    char *data = alloca( count );
nkeynes@56
    84
	    uint32_t rcount = DMAC_get_buffer( 2, data, count );
nkeynes@56
    85
	    if( rcount != count )
nkeynes@56
    86
		WARN( "PVR received %08X bytes from DMA, expected %08X", rcount, count );
nkeynes@100
    87
	    mem_copy_to_sh4( dest_addr, data, rcount );
nkeynes@56
    88
	    asic_event( EVENT_PVR_DMA );
nkeynes@56
    89
	}
nkeynes@56
    90
	break;
nkeynes@56
    91
    default:
nkeynes@56
    92
	MMIO_WRITE( ASIC, reg, val );
nkeynes@56
    93
	WARN( "Write to ASIC (%03X <= %08X) [%s: %s]",
nkeynes@56
    94
	      reg, val, MMIO_REGID(ASIC,reg), MMIO_REGDESC(ASIC,reg) );
nkeynes@1
    95
    }
nkeynes@1
    96
}
nkeynes@1
    97
nkeynes@1
    98
int32_t mmio_region_ASIC_read( uint32_t reg )
nkeynes@1
    99
{
nkeynes@1
   100
    int32_t val;
nkeynes@1
   101
    switch( reg ) {
nkeynes@2
   102
        /*
nkeynes@2
   103
        case 0x89C:
nkeynes@2
   104
            sh4_stop();
nkeynes@2
   105
            return 0x000000B;
nkeynes@2
   106
        */     
nkeynes@94
   107
    case PIRQ0:
nkeynes@94
   108
    case PIRQ1:
nkeynes@94
   109
    case PIRQ2:
nkeynes@94
   110
    case IRQA0:
nkeynes@94
   111
    case IRQA1:
nkeynes@94
   112
    case IRQA2:
nkeynes@94
   113
    case IRQB0:
nkeynes@94
   114
    case IRQB1:
nkeynes@94
   115
    case IRQB2:
nkeynes@94
   116
    case IRQC0:
nkeynes@94
   117
    case IRQC1:
nkeynes@94
   118
    case IRQC2:
nkeynes@94
   119
	val = MMIO_READ(ASIC, reg);
nkeynes@94
   120
	//            WARN( "Read from ASIC (%03X => %08X) [%s: %s]",
nkeynes@94
   121
	//                  reg, val, MMIO_REGID(ASIC,reg), MMIO_REGDESC(ASIC,reg) );
nkeynes@94
   122
	return val;            
nkeynes@94
   123
    case G2STATUS:
nkeynes@94
   124
	return 0; /* find out later if there's any cases we actually need to care about */
nkeynes@94
   125
    default:
nkeynes@94
   126
	val = MMIO_READ(ASIC, reg);
nkeynes@94
   127
	WARN( "Read from ASIC (%03X => %08X) [%s: %s]",
nkeynes@94
   128
	      reg, val, MMIO_REGID(ASIC,reg), MMIO_REGDESC(ASIC,reg) );
nkeynes@94
   129
	return val;
nkeynes@1
   130
    }
nkeynes@94
   131
    
nkeynes@1
   132
}
nkeynes@1
   133
nkeynes@1
   134
void asic_event( int event )
nkeynes@1
   135
{
nkeynes@1
   136
    int offset = ((event&0x60)>>3);
nkeynes@1
   137
    int result = (MMIO_READ(ASIC, PIRQ0 + offset))  |=  (1<<(event&0x1F));
nkeynes@1
   138
nkeynes@1
   139
    if( result & MMIO_READ(ASIC, IRQA0 + offset) )
nkeynes@1
   140
        intc_raise_interrupt( INT_IRQ13 );
nkeynes@1
   141
    if( result & MMIO_READ(ASIC, IRQB0 + offset) )
nkeynes@1
   142
        intc_raise_interrupt( INT_IRQ11 );
nkeynes@1
   143
    if( result & MMIO_READ(ASIC, IRQC0 + offset) )
nkeynes@1
   144
        intc_raise_interrupt( INT_IRQ9 );
nkeynes@1
   145
}
nkeynes@1
   146
nkeynes@125
   147
void asic_clear_event( int event ) {
nkeynes@125
   148
    int offset = ((event&0x60)>>3);
nkeynes@125
   149
    uint32_t result = MMIO_READ(ASIC, PIRQ0 + offset)  & (~(1<<(event&0x1F)));
nkeynes@125
   150
    MMIO_WRITE( ASIC, PIRQ0 + offset, result );
nkeynes@125
   151
nkeynes@125
   152
    asic_check_cleared_events();
nkeynes@125
   153
}
nkeynes@125
   154
nkeynes@20
   155
void asic_check_cleared_events( )
nkeynes@20
   156
{
nkeynes@20
   157
    int i, setA = 0, setB = 0, setC = 0;
nkeynes@20
   158
    uint32_t bits;
nkeynes@20
   159
    for( i=0; i<3; i++ ) {
nkeynes@20
   160
	bits = MMIO_READ( ASIC, PIRQ0 + i );
nkeynes@20
   161
	setA |= (bits & MMIO_READ(ASIC, IRQA0 + i ));
nkeynes@20
   162
	setB |= (bits & MMIO_READ(ASIC, IRQB0 + i ));
nkeynes@20
   163
	setC |= (bits & MMIO_READ(ASIC, IRQC0 + i ));
nkeynes@20
   164
    }
nkeynes@20
   165
    if( setA == 0 )
nkeynes@20
   166
	intc_clear_interrupt( INT_IRQ13 );
nkeynes@20
   167
    if( setB == 0 )
nkeynes@20
   168
	intc_clear_interrupt( INT_IRQ11 );
nkeynes@20
   169
    if( setC == 0 )
nkeynes@20
   170
	intc_clear_interrupt( INT_IRQ9 );
nkeynes@20
   171
}
nkeynes@1
   172
nkeynes@1
   173
nkeynes@1
   174
MMIO_REGION_WRITE_FN( EXTDMA, reg, val )
nkeynes@1
   175
{
nkeynes@125
   176
    WARN( "EXTDMA write %08X <= %08X", reg, val );
nkeynes@125
   177
nkeynes@2
   178
    switch( reg ) {
nkeynes@125
   179
    case IDEALTSTATUS: /* Device control */
nkeynes@125
   180
	ide_write_control( val );
nkeynes@125
   181
	break;
nkeynes@125
   182
    case IDEDATA:
nkeynes@125
   183
	ide_write_data_pio( val );
nkeynes@125
   184
	break;
nkeynes@125
   185
    case IDEFEAT:
nkeynes@125
   186
	if( ide_can_write_regs() )
nkeynes@125
   187
	    idereg.feature = (uint8_t)val;
nkeynes@125
   188
	break;
nkeynes@125
   189
    case IDECOUNT:
nkeynes@125
   190
	if( ide_can_write_regs() )
nkeynes@125
   191
	    idereg.count = (uint8_t)val;
nkeynes@125
   192
	break;
nkeynes@125
   193
    case IDELBA0:
nkeynes@125
   194
	if( ide_can_write_regs() )
nkeynes@125
   195
	    idereg.lba0 = (uint8_t)val;
nkeynes@125
   196
	break;
nkeynes@125
   197
    case IDELBA1:
nkeynes@125
   198
	if( ide_can_write_regs() )
nkeynes@125
   199
	    idereg.lba1 = (uint8_t)val;
nkeynes@125
   200
	break;
nkeynes@125
   201
    case IDELBA2:
nkeynes@125
   202
	if( ide_can_write_regs() )
nkeynes@125
   203
	    idereg.lba2 = (uint8_t)val;
nkeynes@125
   204
	break;
nkeynes@125
   205
    case IDEDEV:
nkeynes@125
   206
	if( ide_can_write_regs() )
nkeynes@125
   207
	    idereg.device = (uint8_t)val;
nkeynes@125
   208
	break;
nkeynes@125
   209
    case IDECMD:
nkeynes@125
   210
	if( ide_can_write_regs() ) {
nkeynes@125
   211
	    ide_write_command( (uint8_t)val );
nkeynes@125
   212
	}
nkeynes@125
   213
	break;
nkeynes@125
   214
    case IDEDMACTL1:
nkeynes@125
   215
    case IDEDMACTL2:
nkeynes@125
   216
	MMIO_WRITE( EXTDMA, reg, val );
nkeynes@125
   217
	if( MMIO_READ( EXTDMA, IDEDMACTL1 ) == 1 &&
nkeynes@125
   218
	    MMIO_READ( EXTDMA, IDEDMACTL2 ) == 1 ) {
nkeynes@125
   219
	    uint32_t target_addr = MMIO_READ( EXTDMA, IDEDMASH4 );
nkeynes@125
   220
	    uint32_t length = MMIO_READ( EXTDMA, IDEDMASIZ );
nkeynes@125
   221
	    int dir = MMIO_READ( EXTDMA, IDEDMADIR );
nkeynes@125
   222
	}
nkeynes@125
   223
	break;
nkeynes@125
   224
    default:
nkeynes@2
   225
            MMIO_WRITE( EXTDMA, reg, val );
nkeynes@2
   226
    }
nkeynes@1
   227
}
nkeynes@1
   228
nkeynes@1
   229
MMIO_REGION_READ_FN( EXTDMA, reg )
nkeynes@1
   230
{
nkeynes@56
   231
    uint32_t val;
nkeynes@1
   232
    switch( reg ) {
nkeynes@2
   233
        case IDEALTSTATUS: return idereg.status;
nkeynes@2
   234
        case IDEDATA: return ide_read_data_pio( );
nkeynes@2
   235
        case IDEFEAT: return idereg.error;
nkeynes@2
   236
        case IDECOUNT:return idereg.count;
nkeynes@2
   237
        case IDELBA0: return idereg.disc;
nkeynes@2
   238
        case IDELBA1: return idereg.lba1;
nkeynes@2
   239
        case IDELBA2: return idereg.lba2;
nkeynes@2
   240
        case IDEDEV: return idereg.device;
nkeynes@2
   241
        case IDECMD:
nkeynes@125
   242
	    return ide_read_status();
nkeynes@1
   243
        default:
nkeynes@56
   244
	    val = MMIO_READ( EXTDMA, reg );
nkeynes@94
   245
	    //DEBUG( "EXTDMA read %08X => %08X", reg, val );
nkeynes@56
   246
	    return val;
nkeynes@1
   247
    }
nkeynes@1
   248
}
nkeynes@1
   249
.