Skip to content

Commit

Permalink
minichlink: add ch641
Browse files Browse the repository at this point in the history
  • Loading branch information
recalci committed Dec 17, 2024
1 parent 2491e92 commit f9a15ff
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 35 deletions.
64 changes: 32 additions & 32 deletions minichlink/minichlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -948,42 +948,41 @@ int DefaultDetermineChipType( void * dev )
MCF.WriteReg32( dev, DMCOMMAND, 0x00231008 ); // Copy data to x8
MCF.WriteReg32( dev, DMDATA0, old_data0 );

if( data0offset == 0xe00000f4 )
{
// Only known processor with this signature is a CH32V003.
iss->target_chip_type = CHIP_CH32V003;
fprintf( stderr, "Autodetected a ch32x003\n" );
}
else if( data0offset == 0xe0000380 )
uint32_t chip_type = (vendorid & 0xfff00000)>>20;
printf( "Chip Type: %03x\n", chip_type );

switch( chip_type )
{
// All other known chips.
uint32_t chip_type = (vendorid & 0xfff00000)>>20;
printf( "Chip Type: %03x\n", chip_type );
switch( chip_type )
{
case 0x103:
fprintf( stderr, "Autodetected a ch32v10x\n" );
iss->target_chip_type = CHIP_CH32V10x;
break;
case 0x035: case 0x033:
fprintf( stderr, "Autodetected a ch32x03x\n" );
iss->target_chip_type = CHIP_CH32X03x;
break;
case 0x203: case 0x205: case 0x208:
fprintf( stderr, "Autodetected a ch32v20x\n" );
iss->target_chip_type = CHIP_CH32V20x;
break;
case 0x303: case 0x305: case 0x307:
fprintf( stderr, "Autodetected a ch32v30x\n" );
iss->target_chip_type = CHIP_CH32V30x;
break;
}
case 0x003:
fprintf( stderr, "Autodetected a ch32v003\n" );
iss->target_chip_type = CHIP_CH32V003;
break;
case 0x103:
fprintf( stderr, "Autodetected a ch32v10x\n" );
iss->target_chip_type = CHIP_CH32V10x;
break;
case 0x035: case 0x033:
fprintf( stderr, "Autodetected a ch32x03x\n" );
iss->target_chip_type = CHIP_CH32X03x;
break;
case 0x203: case 0x205: case 0x208:
fprintf( stderr, "Autodetected a ch32v20x\n" );
iss->target_chip_type = CHIP_CH32V20x;
break;
case 0x303: case 0x305: case 0x307:
fprintf( stderr, "Autodetected a ch32v30x\n" );
iss->target_chip_type = CHIP_CH32V30x;
break;
case 0xb96:
fprintf( stderr, "Autodetected a ch641\n" );
iss->target_chip_type = CHIP_CH641;
break;
}

if( iss->target_chip_type == CHIP_UNKNOWN )
{
fprintf( stderr, "Unknown chip type. Report as bug with picture of chip.\n" );
fprintf( stderr, "Vendored: %08x\n", vendorid );
fprintf( stderr, "Vendorid: %08x\n", vendorid );
fprintf( stderr, "marchid : %08x\n", marchid );
fprintf( stderr, "HARTINFO: %08x\n", rr );
return -2;
Expand Down Expand Up @@ -1230,7 +1229,7 @@ static int DefaultWriteWord( void * dev, uint32_t address_to_write, uint32_t dat
// fc75 c.bnez x8, -4
// c.ebreak
MCF.WriteReg32( dev, DMPROGBUF3,
(iss->target_chip_type == CHIP_CH32X03x || iss->target_chip_type == CHIP_CH32V003) ?
(iss->target_chip_type == CHIP_CH32X03x || iss->target_chip_type == CHIP_CH32V003 || iss->target_chip_type == CHIP_CH641) ?
0x4200c254 : 0x42000001 );

MCF.WriteReg32( dev, DMPROGBUF4,
Expand Down Expand Up @@ -2028,6 +2027,7 @@ void PostSetupConfigureInterface( void * dev )
break;
default:
case CHIP_CH32V003:
case CHIP_CH641:
iss->sector_size = 64;
iss->nr_registers_for_debug = 16;
break;
Expand Down Expand Up @@ -2371,7 +2371,7 @@ int DefaultUnbrick( void * dev )
InternalUnlockFlash(dev, iss);

const uint8_t * option_data =
( iss->target_chip_type == CHIP_CH32X03x || iss->target_chip_type == CHIP_CH32V003 ) ?
( iss->target_chip_type == CHIP_CH32X03x || iss->target_chip_type == CHIP_CH32V003 || iss->target_chip_type == CHIP_CH641 ) ?
option_data_003_x03x : option_data_20x_30x;

DefaultWriteBinaryBlob(dev, 0x1ffff800, 16, option_data );
Expand Down
9 changes: 9 additions & 0 deletions minichlink/minichlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,17 @@ enum RiscVChip {
CHIP_CH32V30x = 0x06,
CHIP_CH58x = 0x07,
CHIP_CH32V003 = 0x09,
CHIP_CH59x = 0x0b,
CHIP_CH643 = 0x0c,
CHIP_CH32X03x = 0x0d,
CHIP_CH32L10x = 0x0e,
CHIP_CH564 = 0x0f,
CHIP_CH645 = 0x46,
CHIP_CH641 = 0x49,
CHIP_CH32V00x = 0x4e,
CHIP_CH32V317 = 0x86,
};
#define CHIP_CH32M007 CHIP_CH32V00x

enum RAMSplit {
// For supported V30x and some V20x devices
Expand Down
32 changes: 29 additions & 3 deletions minichlink/pgm-wch-linke.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,35 @@ static void printChipInfo(enum RiscVChip chip) {
case CHIP_CH58x:
fprintf(stderr, "Detected: CH58x\n");
break;
case CHIP_CH32V003:
fprintf(stderr, "Detected: CH32V003\n");
break;
case CHIP_CH59x:
fprintf(stderr, "Detected: CH59x\n");
break;
case CHIP_CH643:
fprintf(stderr, "Detected: CH643\n");
break;
case CHIP_CH32X03x:
fprintf(stderr, "Detected: CH32X03x\n");
break;
case CHIP_CH32V003:
fprintf(stderr, "Detected: CH32V003\n");
case CHIP_CH32L10x:
fprintf(stderr, "Detected: CH32L10x\n");
break;
case CHIP_CH564:
fprintf(stderr, "Detected: CH564\n");
break;
case CHIP_CH645:
fprintf(stderr, "Detected: CH645\n");
break;
case CHIP_CH641:
fprintf(stderr, "Detected: CH641\n");
break;
case CHIP_CH32V00x:
fprintf(stderr, "Detected: CH32V00x or CH32M007\n");
break;
case CHIP_CH32V317:
fprintf(stderr, "Detected: CH32V317\n");
break;
}
}
Expand All @@ -53,6 +77,7 @@ static int checkChip(enum RiscVChip chip) {
switch(chip) {
case CHIP_CH32V003:
case CHIP_CH32X03x:
case CHIP_CH641:
return 0; // Use direct mode
case CHIP_CH32V10x:
case CHIP_CH32V20x:
Expand Down Expand Up @@ -411,7 +436,8 @@ static int LESetupInterface( void * d )
printf( "Full Chip Type Reply: [%d] %02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x\n", transferred, rbuff[0], rbuff[1], rbuff[2], rbuff[3], rbuff[4], rbuff[5], rbuff[6], rbuff[7], rbuff[8] );

enum RiscVChip chip = (enum RiscVChip)rbuff[3];
if( ( chip == 0x08 || chip > 0x09 ) && chip != CHIP_CH32X03x ) {
if( ( chip == 0x04 || chip == 0x08 || chip == 0x0a || chip > 0x0f )
&& chip != CHIP_CH645 && chip != CHIP_CH641 && chip != CHIP_CH32V00x && chip != CHIP_CH32V317 ) {
fprintf( stderr, "Chip Type unknown [%02x]. Aborting...\n", chip );
return -1;
}
Expand Down

0 comments on commit f9a15ff

Please sign in to comment.