diff --git a/jtag/src/.cvsignore b/jtag/src/.cvsignore index c25f7e42..8f41fc09 100644 --- a/jtag/src/.cvsignore +++ b/jtag/src/.cvsignore @@ -1,4 +1,4 @@ Makefile.in Makefile .deps -detect +jtag diff --git a/jtag/src/Makefile.am b/jtag/src/Makefile.am index 81c9cb83..b8417813 100644 --- a/jtag/src/Makefile.am +++ b/jtag/src/Makefile.am @@ -25,12 +25,12 @@ SUBDIRS = \ tap \ part -bin_PROGRAMS = detect +bin_PROGRAMS = jtag -detect_SOURCES = detect.c +jtag_SOURCES = jtag.c detect.c readmem.c -detect_DEPENDENCIES = tap/libtap.a part/libpart.a +jtag_DEPENDENCIES = tap/libtap.a part/libpart.a -detect_LDADD = -Ltap -ltap -Lpart -lpart +jtag_LDADD = -lreadline -lncurses -Ltap -ltap -Lpart -lpart INCLUDES = -I$(top_srcdir)/include diff --git a/jtag/src/detect.c b/jtag/src/detect.c index 7ccbdc99..b1bb8849 100644 --- a/jtag/src/detect.c +++ b/jtag/src/detect.c @@ -23,8 +23,6 @@ */ #include -#include -#include #include #include @@ -33,6 +31,8 @@ #include #include +#include "detect.h" + struct id_record { char name[20]; char fullname[100]; @@ -155,19 +155,26 @@ find_record( char *filename, tap_register *key, struct id_record *idr ) parts * detect_parts( char *db_path ) { - tap_register *zeros; - tap_register *id; char data_path[1024]; + char manufacturer[MAXLEN_MANUFACTURER + 1]; + char partname[MAXLEN_PART + 1]; + char stepping[MAXLEN_STEPPING + 1]; + tap_register *zeros = register_fill( register_alloc( 32 ), 0 ); + tap_register *ones = register_fill( register_alloc( 32 ), 1 ); + tap_register *id = register_alloc( 32 ); parts *ps = parts_alloc(); - if (!ps) { + + if (!zeros || !ones || !id || !ps) { printf( __FUNCTION__ ": out of memory\n" ); + + register_free( zeros ); + register_free( ones ); + register_free( id ); + parts_free( ps ); return NULL; } - zeros = register_fill( register_alloc( 32 ), 0 ); - id = register_alloc( 32 ); - tap_reset(); tap_capture_dr(); @@ -175,14 +182,19 @@ detect_parts( char *db_path ) tap_register *key; struct id_record idr; char *p; + FILE *f; + part *part; tap_shift_register( zeros, id, 0 ); - if (!register_compare( id, zeros )) { - tap_clock( 1, 0 ); /* Exit1-DR */ - tap_clock( 1, 0 ); /* Update-DR */ + if (!register_compare( id, zeros )) + break; /* end of chain */ + + if (!register_compare( ones, id )) { + printf( __FUNCTION__ ": bad JTAG connection (TDO is 1)\n" ); break; } - printf( "%s\n", register_get_string( id ) ); + + printf( "Device Id: %s\n", register_get_string( id ) ); strcpy( data_path, db_path ); /* FIXME: Buffer overrun */ @@ -199,6 +211,10 @@ detect_parts( char *db_path ) register_free( key ); printf( " Manufacturer: %s\n", idr.fullname ); + if (strlen( idr.fullname ) > MAXLEN_MANUFACTURER) + printf( "Warning: Manufacturer too long\n" ); + strncpy( manufacturer, idr.fullname, MAXLEN_MANUFACTURER ); + manufacturer[MAXLEN_MANUFACTURER] = '\0'; /* parts */ p = strrchr( data_path, '/' ); @@ -219,6 +235,10 @@ detect_parts( char *db_path ) register_free( key ); printf( " Part: %s\n", idr.fullname ); + if (strlen( idr.fullname ) > MAXLEN_PART) + printf( "Warning: Part too long\n" ); + strncpy( partname, idr.fullname, MAXLEN_PART ); + partname[MAXLEN_PART] = '\0'; /* steppings */ p = strrchr( data_path, '/' ); @@ -239,6 +259,10 @@ detect_parts( char *db_path ) register_free( key ); printf( " Stepping: %s\n", idr.fullname ); + if (strlen( idr.fullname ) > MAXLEN_STEPPING) + printf( "Warning: Stepping too long\n" ); + strncpy( stepping, idr.fullname, MAXLEN_STEPPING ); + stepping[MAXLEN_STEPPING] = '\0'; /* part definition file */ p = strrchr( data_path, '/' ); @@ -248,414 +272,29 @@ detect_parts( char *db_path ) data_path[0] = '\0'; strcat( data_path, idr.name ); - printf( "Reading file: %s ... ", data_path ); - { - FILE *f = fopen( data_path, "r" ); - part *p = read_part( f ); - p->active_instruction = part_find_instruction( p, "IDCODE" ); - parts_add_part( ps, p ); + printf( " Filename: %s\n", data_path ); + f = fopen( data_path, "r" ); + part = read_part( f, id ); + if (part) { + strcpy( part->manufacturer, manufacturer ); + strcpy( part->part, partname ); + strcpy( part->stepping, stepping ); + part->active_instruction = part_find_instruction( part, "IDCODE" ); + parts_add_part( ps, part ); + } else { + printf( "%s(%s:%d) Error: part read failed\n", __FUNCTION__, __FILE__, __LINE__ ); + exit( 1 ); } - printf( "done\n" ); + if (f) + fclose( f ); } + tap_clock( 1, 0 ); /* Exit1-DR */ + tap_clock( 1, 0 ); /* Update-DR */ + register_free( zeros ); + register_free( ones ); register_free( id ); return ps; } - -void -setup_address( part *p, unsigned int a ) -{ - int i; - char buff[10]; - - for (i = 0; i < 26; i++) { - sprintf( buff, "MA[%d]", i ); - part_set_signal( p, buff, 1, (a >> i) & 1 ); - } -} - -void -setup_data( part *p, unsigned int d ) -{ - int i; - char buff[10]; - - for (i = 0; i < 32; i++) { - sprintf( buff, "MD[%d]", i ); - part_set_signal( p, buff, 1, (d >> i) & 1 ); - } -} - -unsigned int -get_data( part *p ) -{ - int i; - char buff[10]; - unsigned int d = 0; - - for (i = 0; i < 32; i++) { - sprintf( buff, "MD[%d]", i ); - d |= (unsigned int) (part_get_signal( p, buff ) << i); - } - - return d; -} - -void set_data_in( part *p ) -{ - int i; - char buff[10]; - - for (i = 0; i < 32; i++) { - sprintf( buff, "MD[%d]", i ); - part_set_signal( p, buff, 0, 0 ); - } -} - -#define AB_READ 0 -#define AB_WRITE 1 -#define AB_SETUP 2 -#define AB_HOLD 3 - -unsigned int -access_bus( part *p, int type, unsigned int a, unsigned int d ) -{ - part_set_signal( p, "nCS[0]", 1, 0 ); - setup_address( p, a ); - - switch (type) { - case AB_READ: - part_set_signal( p, "nOE", 1, 0 ); - part_set_signal( p, "nWE", 1, 1 ); - set_data_in( p ); - break; - case AB_WRITE: - part_set_signal( p, "nOE", 1, 1 ); - part_set_signal( p, "nWE", 1, 0 ); - setup_data( p, d ); - break; - case AB_SETUP: - case AB_HOLD: - part_set_signal( p, "nOE", 1, 1 ); - part_set_signal( p, "nWE", 1, 1 ); - setup_data( p, d ); - break; - default: - printf( "access_bus: invalid type\n" ); - return 0; - } - - tap_capture_dr(); - tap_shift_register( p->bsr, p->prev_bsr, 1 ); - - return get_data( p ); -} - -unsigned int -access_rom( part *p, int type, unsigned int a, unsigned int d ) -{ - return access_bus( p, type, a << 2, d ); -} - -void -program_flash( part *p, unsigned int a, unsigned int d ) -{ - - access_bus( p, AB_SETUP, a, 0x00400040 ); - access_bus( p, AB_WRITE, a, 0x00400040 ); - access_bus( p, AB_HOLD, a, 0x00400040 ); - - access_bus( p, AB_SETUP, a, d ); - access_bus( p, AB_WRITE, a, d ); - access_bus( p, AB_HOLD, a, d ); - - access_bus( p, AB_READ, 0, 0 ); - printf( "pf: %08X\n", access_bus( p, AB_READ, 0, 0 ) ); -} - -void -unlock( part *p, unsigned int a ) -{ - access_bus( p, AB_SETUP, a, 0x00600060 ); - access_bus( p, AB_WRITE, a, 0x00600060 ); - access_bus( p, AB_HOLD, a, 0x00600060 ); - - access_bus( p, AB_SETUP, a, 0x00D000D0 ); - access_bus( p, AB_WRITE, a, 0x00D000D0 ); - access_bus( p, AB_HOLD, a, 0x00D000D0 ); -} - -void -erase( part *p, unsigned int a ) -{ - printf( "erase\n" ); - - access_bus( p, AB_SETUP, a, 0x00200020 ); - access_bus( p, AB_WRITE, a, 0x00200020 ); - access_bus( p, AB_HOLD, a, 0x00200020 ); - - access_bus( p, AB_SETUP, a, 0x00D000D0 ); - access_bus( p, AB_WRITE, a, 0x00D000D0 ); - access_bus( p, AB_HOLD, a, 0x00D000D0 ); - - access_bus( p, AB_READ, 0, 0 ); - printf( "pf: %08X\n", access_bus( p, AB_READ, 0, 0 ) ); - sleep( 4 ); - access_bus( p, AB_READ, 0, 0 ); - printf( "pf: %08X\n", access_bus( p, AB_READ, 0, 0 ) ); - printf( "pf: %08X\n", access_bus( p, AB_READ, 0, 0 ) ); -} - -int -main( void ) -{ - parts *ps; - part *p; - - unsigned int max_erase_time; - unsigned int dsize; - unsigned int ebri; - - tap_init(); - - tap_set_trst( 0 ); - tap_set_trst( 1 ); - - ps = detect_parts( "../data" ); - - if (ps->len == 0) { - printf( "Not detected!!!\n" ); - return 0; - } - - p = ps->parts[0]; - - printf( "Setting up safe default values\n" ); - parts_set_instruction( ps, "SAMPLE/PRELOAD" ); - tap_capture_dr(); - tap_shift_register( p->bsr, p->prev_bsr, 1 ); - -printf( "%s\n", register_get_string( p->bsr ) ); -printf( "%s\n", register_get_string( p->prev_bsr ) ); - - parts_set_instruction( ps, "EXTEST" ); - - access_bus( p, AB_SETUP, 0x00000000, 0x00500050 ); - access_bus( p, AB_WRITE, 0x00000000, 0x00500050 ); - access_bus( p, AB_HOLD, 0x00000000, 0x00500050 ); - - access_bus( p, AB_SETUP, 0x00000000, 0x00980098 ); - access_bus( p, AB_WRITE, 0x00000000, 0x00980098 ); - access_bus( p, AB_HOLD, 0x00000000, 0x00980098 ); - - access_rom( p, AB_READ, 0x10, 0 ); - printf( "read 0x10: %08X\n", access_rom( p, AB_READ, 0x11, 0 ) ); - printf( "read 0x11: %08X\n", access_rom( p, AB_READ, 0x12, 0 ) ); - printf( "read 0x12: %08X\n", access_rom( p, AB_READ, 0x25, 0 ) ); - - - printf( "read max_erase_time: %08X\n", max_erase_time = access_rom( p, AB_READ, 0x27, 0 ) ); - dsize = 1 << (access_rom( p, AB_READ, 0x2D, 0 ) & 0xFFFF); - printf( "device size: %08X\n", dsize ); - ebri = access_rom( p, AB_READ, 0x2D, 0 ); - printf( "ebri: %08X\n", ebri ); - - unlock( p, 0 ); - erase( p, 0 ); - - { - FILE *f = fopen( "brux.b", "r" ); - unsigned int d; - unsigned int a = 0; - - - while (fread( &d, sizeof d, 1, f ) == 1) { - printf( "adr: %08X\n", a ); - program_flash( p, a, d ); - a += 4; - } - fclose( f ); - } - - access_bus( p, AB_SETUP, 0, 0x00FF00FF ); - access_bus( p, AB_WRITE, 0, 0x00FF00FF ); - access_bus( p, AB_HOLD, 0, 0x00FF00FF ); - - { - FILE *f = fopen( "brux.b", "r" ); - unsigned int d; - unsigned int a = 0; - - - while (fread( &d, sizeof d, 1, f ) == 1) { - unsigned int x; - printf( "adr: %08X\n", a ); - access_bus( p, AB_READ, a, 0 ); - x = access_bus( p, AB_READ, a, 0 ); - if (x != d) { - printf( "error read = %08X, expect = %08X\n", x, d ); - exit( 0 ); - } - a += 4; - } - fclose( f ); - } - exit( 0 ); - - - unlock( p, 0 ); - erase( p, 0 ); - - { - unsigned int i; - for (i = 0; i < 256 * 1024; i += 4) { - printf( "program: %08X\n", i ); - program_flash( p, i, i ); - } - } - - access_bus( p, AB_SETUP, 0, 0x00FF00FF ); - access_bus( p, AB_WRITE, 0, 0x00FF00FF ); - access_bus( p, AB_HOLD, 0, 0x00FF00FF ); - - { - unsigned int i; - unsigned int j; - for (i = 0; i < 256 * 1024; i += 4) { - printf( "read: %08X\n", i ); - access_bus( p, AB_READ, i, 0 ); - j = access_bus( p, AB_READ, i, 0 ); - if (i != j) - printf( "error: a = %08X, d = %08X\n", i, j ); - } - } - - - { - unsigned int b; - for (b = 1; b < 128; b++) { - unsigned int x; - unsigned int d; - - access_bus( p, AB_SETUP, 0x00000000, 0x00500050 ); - access_bus( p, AB_WRITE, 0x00000000, 0x00500050 ); - access_bus( p, AB_HOLD, 0x00000000, 0x00500050 ); - - access_bus( p, AB_SETUP, 0x00000000, 0x00980098 ); - access_bus( p, AB_WRITE, 0x00000000, 0x00980098 ); - access_bus( p, AB_HOLD, 0x00000000, 0x00980098 ); - - unlock( p, b * 256 * 1024 ); - erase( p, b * 256 * 1024 ); - - program_flash( p, b * 256 * 1024, 0x55AA55AA ); - - access_bus( p, AB_SETUP, 0, 0x00FF00FF ); - access_bus( p, AB_WRITE, 0, 0x00FF00FF ); - access_bus( p, AB_HOLD, 0, 0x00FF00FF ); - - printf( "Test: %08X\n", b * 256 * 1024 ); - - for (x = 1; x < 128; x++) { - access_bus( p, AB_READ, x * 256 * 1024, 0 ); - d = access_bus( p, AB_READ, x * 256 * 1024, 0 ); - if (d == 0x55AA55AA) - printf( " at %08X\n", x * 256 * 1024 ); - } - - access_bus( p, AB_SETUP, 0x00000000, 0x00500050 ); - access_bus( p, AB_WRITE, 0x00000000, 0x00500050 ); - access_bus( p, AB_HOLD, 0x00000000, 0x00500050 ); - - access_bus( p, AB_SETUP, 0x00000000, 0x00980098 ); - access_bus( p, AB_WRITE, 0x00000000, 0x00980098 ); - access_bus( p, AB_HOLD, 0x00000000, 0x00980098 ); - - erase( p, b * 256 * 1024 ); - } - } - - - access_bus( p, AB_SETUP, 0, 0x00FF00FF ); - access_bus( p, AB_WRITE, 0, 0x00FF00FF ); - access_bus( p, AB_HOLD, 0, 0x00FF00FF ); - - { - unsigned int i; - unsigned int j; - for (i = 0; i < 256 * 1024; i += 4) { - printf( "read: %08X\n", i ); - access_bus( p, AB_READ, i, 0 ); - j = access_bus( p, AB_READ, i, 0 ); - if (i != j) - printf( "error: a = %08X, d = %08X\n", i, j ); - } - } - - - -#if 0 - program_flash( p, 0x0, 0xE1A00000 ); - program_flash( p, 0x4, 0xE1A00000 ); - program_flash( p, 0x8, 0xE1A00000 ); - program_flash( p, 0xC, 0xE1A00000 ); - program_flash( p, 0x10, 0xE1A00000 ); - program_flash( p, 0x14, 0xEAFFFFF9 ); -#endif - - //erase( p, 0 ); - - access_bus( p, AB_SETUP, 0, 0x00FF00FF ); - access_bus( p, AB_WRITE, 0, 0x00FF00FF ); - access_bus( p, AB_HOLD, 0, 0x00FF00FF ); - - access_bus( p, AB_READ, 0, 0 ); - printf( "read data from 0x00: %08X\n", access_bus( p, AB_READ, 0x04, 0 ) ); - printf( "read data from 0x04: %08X\n", access_bus( p, AB_READ, 0x08, 0 ) ); - printf( "read data from 0x08: %08X\n", access_bus( p, AB_READ, 0x0C, 0 ) ); - printf( "read data from 0x0C: %08X\n", access_bus( p, AB_READ, 0x10, 0 ) ); - printf( "read data from 0x10: %08X\n", access_bus( p, AB_READ, 0x14, 0 ) ); - printf( "read data from 0x14: %08X\n", access_bus( p, AB_READ, 0x18, 0 ) ); - printf( "read data from 0x18: %08X\n", access_bus( p, AB_READ, 0x1C, 0 ) ); - printf( "read data from 0x1C: %08X\n", access_bus( p, AB_READ, 0x20, 0 ) ); - printf( "read data from 0x20: %08X\n", access_bus( p, AB_READ, 0x24, 0 ) ); - printf( "read data from 0x24: %08X\n", access_bus( p, AB_READ, 0x28, 0 ) ); - printf( "read data from 0x28: %08X\n", access_bus( p, AB_READ, 0x2C, 0 ) ); - printf( "read data from 0x2C: %08X\n", access_bus( p, AB_READ, 0x30, 0 ) ); - printf( "read data from 0x30: %08X\n", access_bus( p, AB_READ, 0x34, 0 ) ); - -#if 0 - tap_capture_dr(); -a: - printf( "Jedna\n" ); - part_set_signal( sa1110, "MA[24]", 1, 1 ); - tap_shift_register( sa1110->bsr, sa1110->prev_bsr, 1 ); - - tap_capture_dr(); - sleep(5); - printf( "Nula\n" ); - part_set_signal( sa1110, "MA[24]", 1, 0 ); - tap_shift_register( sa1110->bsr, sa1110->prev_bsr, 1 ); - - tap_capture_dr(); - sleep(5); - goto a; - - - printf( "%s\n", register_get_string( sa1110->bsr ) ); - printf( "%s\n", register_get_string( sa1110->prev_bsr ) ); -// for (i = 0; i < s1110->boundary_length; i++) { -// -// } -#endif - - parts_free( ps ); - - tap_reset(); - - tap_done(); - - return 0; -} diff --git a/jtag/src/detect.h b/jtag/src/detect.h new file mode 100644 index 00000000..d7497e80 --- /dev/null +++ b/jtag/src/detect.h @@ -0,0 +1,32 @@ +/* + * $Id$ + * + * Copyright (C) 2002 ETC s.r.o. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + * Written by Marcel Telka , 2002. + * + */ + +#ifndef DETECT_H +#define DETECT_H + +#include + +parts *detect_parts( char *db_path ); + +#endif /* DETECT_H */ diff --git a/jtag/src/jtag.c b/jtag/src/jtag.c new file mode 100644 index 00000000..d483e9f8 --- /dev/null +++ b/jtag/src/jtag.c @@ -0,0 +1,117 @@ +/* + * $Id$ + * + * Copyright (C) 2002 ETC s.r.o. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + * Written by Marcel Telka , 2002. + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + +#include +#include +#include + +#include "detect.h" + +static char * +get_token( char *buf ) +{ + return strtok( buf, " \f\n\r\t\v" ); +} + +int +main( void ) +{ + char *line = NULL; + parts *ps = NULL; + + printf( + PACKAGE_STRING "\n" + "Copyright (C) 2002 ETC s.r.o.\n" + PACKAGE " is free software, covered by the GNU General Public License, and you are\n" + "welcome to change it and/or distribute copies of it under certain conditions.\n" + "There is absolutely no warranty for " PACKAGE ".\n\n" + ); + + tap_init(); + + tap_set_trst( 0 ); + tap_set_trst( 1 ); + + for (;;) { + char *t; + + free( line ); + line = readline( "jtag> " ); + + if (!line || !*line) + continue; + add_history( line ); + + t = get_token( line ); + if (!t) + continue; + + if (strcmp( t, "quit" ) == 0) + break; + + if (strcmp( t, "detect" ) == 0) { + if (ps) + parts_free( ps ); + ps = detect_parts( "../data" ); + continue; + } + + if (strcmp( t, "readmem" ) == 0) { + readmem( ps ); + continue; + } + + if (strcmp( t, "print" ) == 0) { + parts_print( ps, 1 ); + continue; + } + + if (strcmp( t, "set" ) == 0) { + t = get_token( NULL ); + if (!t) { + printf( "set: syntax error\n" ); + continue; + } + + continue; + } + } + + free( line ); + parts_free( ps ); + + tap_reset(); + tap_done(); + + return 0; +} diff --git a/jtag/src/readmem.c b/jtag/src/readmem.c new file mode 100644 index 00000000..48421440 --- /dev/null +++ b/jtag/src/readmem.c @@ -0,0 +1,31 @@ +/* + * $Id$ + * + * Copyright (C) 2002 ETC s.r.o. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + * Written by Marcel Telka , 2002. + * + */ + +#include + +void +readmem( parts *ps ) +{ + parts_set_instruction( ps, "BYPASS" ); +}