diff --git a/jtag/ChangeLog b/jtag/ChangeLog index f8c4ee1d..f125f02a 100644 --- a/jtag/ChangeLog +++ b/jtag/ChangeLog @@ -1,3 +1,27 @@ +2003-02-16 Marcel Telka + + * src/Makefile.am (jtag_SOURCES): Added new flash files with flash driver + framework (August Hörandl). + * src/flash-amd.c: New file with AMD flash support (August Hörandl). + * src/flash-intel.c: New file. Extracted Intel flash support from src/flash.c + file (August Hörandl, me). + * src/flash.c (set_flash_driver): New function. Added support for flash + drivers (August Hörandl). + (flashceck): Ditto. + (flashmsbin): Added support for flash driver (August Hörandl). + (flashmem): Ditto. + (flash_erase_block): Function moved to src/flash-intel.c (August Hörandl). + (flash_unlock_block): Ditto. + (flash_program): Ditto. + (flash_erase_block32): Ditto. + (flash_unlock_block32): Ditto. + (flash_program32): Ditto. + * src/flash.h: New file. Interface for flash drivers (August Hörandl). + * src/help.c (help): Added list of supported flash drivers for 'flashmem' + command (August Hörandl). + * src/readmem.c (detectflash): Removed Intel specific code. Enhanced output + messages (August Hörandl). + 2003-02-14 Marcel Telka * src/jtag.c (jtag): Added command history load/save support (August Hörandl). diff --git a/jtag/NEWS b/jtag/NEWS index 18d13bd9..eff4ab1a 100644 --- a/jtag/NEWS +++ b/jtag/NEWS @@ -12,6 +12,8 @@ $Id$ * Added support for 1 x 16 bit memory configuration (Christian Pellegrin). * Added buffered file reads/writes (Christian Pellegrin). * Added command history load/save support (August Hörandl). + * Added support for flash drivers (August Hörandl). + * Added flash driver for AMD chips (August Hörandl). jtag-0.2.2 (2003-02-04): diff --git a/jtag/README b/jtag/README index c5847110..c73a4312 100644 --- a/jtag/README +++ b/jtag/README @@ -56,6 +56,7 @@ Flash chips (2 x 16 bit configuration only): - Intel 28FxxxJ3A (28F320J3A, 28F640J3A, 28F128J3A) - Intel 28FxxxK3 (28F640K3, 28F128K3, 28F256K3) - Intel 28FxxxK18 (28F640K18, 28F128K18, 28F256K18) +- AMD Am29LV64xD (Am29LV640D, Am29LV641D, Am29LV642D) Tested hardware @@ -64,6 +65,7 @@ Tested hardware Intel SA1110 B2 + 2 x Intel 28F128J3A (Compaq iPAQ H3630) Intel SA1110 B4 + 2 x Intel 28F640J3A (Compaq iPAQ H3600, see http://openwince.sourceforge.net/jtag/iPAQ-3600/) Intel SA1110 B4 + 2 x Intel 28F128J3A +Intel PXA250 B2 + 2 x AMD Am29LV640D Intel PXA250 B1 + 2 x Intel 28F128K3 (ETC WEP EP250) Intel PXA250 C0 + 2 x Intel 28F128K3 (ETC WEP EP250) Xilinx XCR3128XL-CS144 (ETC WEP EP250) diff --git a/jtag/src/Makefile.am b/jtag/src/Makefile.am index bd6e72ef..a1eb79df 100644 --- a/jtag/src/Makefile.am +++ b/jtag/src/Makefile.am @@ -39,6 +39,8 @@ jtag_SOURCES = \ ixp425.c \ cfi.c \ flash.c \ + flash-intel.c \ + flash-amd.c \ help.c jtag_DEPENDENCIES = tap/libtap.a part/libpart.a diff --git a/jtag/src/flash-amd.c b/jtag/src/flash-amd.c new file mode 100644 index 00000000..4bbcd209 --- /dev/null +++ b/jtag/src/flash-amd.c @@ -0,0 +1,245 @@ +/* + * $Id$ + * + * Flash driver for AMD Am29LV640D, Am29LV641D, Am29LV642D + * Copyright (C) 2003 AH + * + * 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 August Hörandl + * + * Documentation: + * [1] Advanced Micro Devices, "Am29LV640D/Am29LV641D", + * September 20, 2002 Rev B, 22366b8.pdf + * [2] Advanced Micro Devices, "Am29LV642D", + * August 14, 2001 Rev A, 25022.pdf + * + */ + +#include +#include +#include +#include +#include +#include + +#include "flash.h" + +static int dbg = 0; + +static int amd_flash_erase_block( parts *ps, uint32_t adr ); +static int amd_flash_unlock_block( parts *ps, uint32_t adr ); +static int amd_flash_program( parts *ps, uint32_t adr, uint32_t data ); +static void amd_flash_read_array( parts *ps); + +/* autodetect, we can handle this chip */ +static int +amd_flash_autodetect( parts *ps, cfi_query_structure_t *cfi ) +{ + return (cfi->identification_string.pri_id_code == CFI_VENDOR_AMD_SCS); +} + +/* + * check device status + * 1/true PASS + * 0/false FAIL + */ +/* + * first implementation: see [1], page 29 + */ +static int +amdstatus29( parts *ps, uint32_t adr, uint32_t data ) +{ + int o = 2; + int timeout; + uint32_t dq7mask = ((1 << 7) << 16) + (1 << 7); + uint32_t dq5mask = ((1 << 5) << 16) + (1 << 5); + uint32_t bit7 = (data & (1 << 7)) != 0; + uint32_t data1; + + for (timeout = 0; timeout < 100; timeout++) { + data1 = bus_read( ps, adr << o ); + data1 = bus_read( ps, adr << o ); + if (dbg) + printf( "amdstatus %d: %04X (%04X) = %04X\n", timeout, data1, (data1 & dq7mask), bit7 ); + if (((data1 & dq7mask) == dq7mask) == bit7) /* FIXME: This looks non-portable */ + return 1; + + if ((data1 & dq5mask) == dq5mask) + break; + usleep( 100 ); + } + + data1 = bus_read( ps, adr << o ); + if (((data1 & dq7mask) == dq7mask) == bit7) /* FIXME: This looks non-portable */ + return 1; + + return 0; +} + +/* + * second implementation: see [1], page 30 + */ +static int +amdstatus( parts *ps, uint32_t adr, int data ) +{ + int timeout; + uint32_t togglemask = ((1 << 6) << 16) + (1 << 6); /* DQ 6 */ + /* int dq5mask = ((1 << 5) << 16) + (1 << 5); DQ5 */ + + for (timeout = 0; timeout < 100; timeout++) { + uint32_t data1 = bus_read( ps, adr ); + uint32_t data2 = bus_read( ps, adr ); + + /*printf("amdstatus %d: %04X/%04X %04X/%04X \n", */ + /* timeout, data1, data2, (data1 & togglemask), (data2 & togglemask)); */ + if ( (data1 & togglemask) == (data2 & togglemask)) /* no toggle */ + return 1; + + /* if ( (data1 & dq5mask) != 0 ) TODO */ + /* return 0; */ + if (dbg) + printf( "amdstatus %d: %04X/%04X\n", timeout, data1, data2 ); + else + printf( "." ); + usleep( 100 ); + } + return 0; +} + +static int +amdisprotected( parts *ps, uint32_t adr ) +{ + uint32_t data; + int o = 2; + + bus_write( ps, 0x0555 << o, 0x00aa00aa ); /* autoselect p29, sector erase */ + bus_write( ps, 0x02aa << o, 0x00550055 ); + bus_write( ps, 0x0555 << o, 0x00900090 ); + + data = bus_read( ps, adr + (0x0002 << 2) ); + /* Read Array */ + amd_flash_read_array( ps ); /* AMD reset */ + + return ((data & 0x00ff00ff) != 0); +} + +static void +amd_flash_print_info( parts *ps ) +{ + int o = 2; + int mid, cid, prot; + bus_write( ps, 0x0555 << o, 0x00aa00aa ); /* autoselect p29 */ + bus_write( ps, 0x02aa << o, 0x00550055 ); + bus_write( ps, 0x0555 << o, 0x00900090 ); + mid = bus_read( ps, 0x00 << o ) & 0xFFFF; + cid = bus_read( ps, 0x01 << o ) & 0xFFFF; + prot = bus_read( ps, 0x02 << o ) & 0xFF; + amd_flash_read_array(ps); /* AMD reset */ + printf( "Chip: AMD Flash\n\tManufacturer: " ); + switch (mid) { + case 0x0001: + printf( "AMD" ); + break; + default: + printf( "Unknown manufacturer (ID 0x%04x)", mid ); + break; + } + printf( "\n\tChip: " ); + switch (cid) { + case 0x22D7: + printf( "Am29LV640D/Am29LV641D/Am29LV642D" ); + break; + default: + printf ( "Unknown (ID 0x%04x)", cid ); + break; + } + printf( "\n\tProtected: %04x\n", prot ); +} + +static int +amd_flash_erase_block( parts *ps, uint32_t adr ) +{ + int o = 2; + + printf("flash_erase_block 0x%08X\n", adr); + + /* printf("protected: %d\n", amdisprotected(ps, adr)); */ + + bus_write( ps, 0x0555 << o, 0x00aa00aa ); /* autoselect p29, sector erase */ + bus_write( ps, 0x02aa << o, 0x00550055 ); + bus_write( ps, 0x0555 << o, 0x00800080 ); + bus_write( ps, 0x0555 << o, 0x00aa00aa ); + bus_write( ps, 0x02aa << o, 0x00550055 ); + bus_write( ps, adr, 0x00300030 ); + + if (amdstatus(ps, adr, 0xffff)) { + printf( "flash_erase_block 0x%08X DONE\n", adr ); + amd_flash_read_array( ps ); /* AMD reset */ + return 0; + } + printf( "flash_erase_block 0x%08X FAILED\n", adr ); + /* Read Array */ + amd_flash_read_array( ps ); /* AMD reset */ + + return CFI_INTEL_ERROR_UNKNOWN; +} + +static int +amd_flash_unlock_block( parts *ps, uint32_t adr ) +{ + printf( "flash_unlock_block 0x%08X IGNORE\n", adr ); + return 0; +} + +static int +amd_flash_program( parts *ps, uint32_t adr, uint32_t data ) +{ + int o = 2; + int status; + + if (dbg) + printf("\nflash_program 0x%08X = 0x%08X\n", adr, data); + + bus_write( ps, 0x0555 << o, 0x00aa00aa ); /* autoselect p29, program */ + bus_write( ps, 0x02aa << o, 0x00550055 ); + bus_write( ps, 0x0555 << o, 0x00A000A0 ); + + bus_write( ps, adr, data ); + status = amdstatus( ps, adr, data ); + /* amd_flash_read_array(ps); */ + + return !status; +} + +static void +amd_flash_read_array( parts *ps) +{ + /* Read Array */ + bus_write( ps, 0x0, 0x00F000F0 ); /* AMD reset */ +} + +flash_driver_t amd_32_flash_driver = { + 4, /* buswidth */ + "AMD/Fujitsu Standard Command Set", + "supported: AMD 29LV640D, 29LV641D, 29LV642D; 2x16 Bit", + amd_flash_autodetect, + amd_flash_print_info, + amd_flash_erase_block, + amd_flash_unlock_block, + amd_flash_program, + amd_flash_read_array, +}; diff --git a/jtag/src/flash-intel.c b/jtag/src/flash-intel.c new file mode 100644 index 00000000..cf12c06f --- /dev/null +++ b/jtag/src/flash-intel.c @@ -0,0 +1,307 @@ +/* + * $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. + * Changed by August Hörandl, 2003 + * + * Documentation: + * [1] Advanced Micro Devices, "Common Flash Memory Interface Specification Release 2.0", + * December 1, 2001 + * [2] Intel Corporation, "Intel PXA250 and PXA210 Application Processors + * Developer's Manual", February 2002, Order Number: 278522-001 + * [3] Intel Corporation, "Common Flash Interface (CFI) and Command Sets + * Application Note 646", April 2000, Order Number: 292204-004 + * [4] Advanced Micro Devices, "Common Flash Memory Interface Publication 100 Vendor & Device + * ID Code Assignments", December 1, 2001, Volume Number: 96.1 + * + */ + +#include +#include +#include +#include +#include +#include + +#include "flash.h" + +static int intel_flash_erase_block( parts *ps, uint32_t adr ); +static int intel_flash_unlock_block( parts *ps, uint32_t adr ); +static int intel_flash_program( parts *ps, uint32_t adr, uint32_t data ); +static int intel_flash_erase_block32( parts *ps, uint32_t adr ); +static int intel_flash_unlock_block32( parts *ps, uint32_t adr ); +static int intel_flash_program32( parts *ps, uint32_t adr, uint32_t data ); + +/* autodetect, we can handle this chip */ +static int +intel_flash_autodetect32( parts *ps, cfi_query_structure_t *cfi ) +{ + return (cfi->identification_string.pri_id_code == CFI_VENDOR_INTEL_SCS) && (bus_width( ps ) == 32); +} + +static int +intel_flash_autodetect( parts *ps, cfi_query_structure_t *cfi ) +{ + return (cfi->identification_string.pri_id_code == CFI_VENDOR_INTEL_SCS) && (bus_width( ps ) == 16); +} + +static +void _intel_flash_print_info( parts *ps, int o ) +{ + uint32_t mid, cid; + + mid = (bus_read( ps, 0x00 << o ) & 0xFF); + switch (mid) { + case STD_MIC_INTEL: + printf( "Manufacturer: %s\n", STD_MICN_INTEL ); + break; + default: + printf( "Unknown manufacturer (0x%04X)!\n", mid); + break; + } + + printf( "Chip: " ); + cid = (bus_read( ps, 0x01 << o ) & 0xFFFF); + switch (cid) { + case 0x0016: + printf( "28F320J3A\n" ); + break; + case 0x0017: + printf( "28F640J3A\n" ); + break; + case 0x0018: + printf( "28F128J3A\n" ); + break; + case 0x8801: + printf( "28F640K3\n" ); + break; + case 0x8802: + printf( "28F128K3\n" ); + break; + case 0x8803: + printf( "28F256K3\n" ); + break; + case 0x8805: + printf( "28F640K18\n" ); + break; + case 0x8806: + printf( "28F128K18\n" ); + break; + case 0x8807: + printf( "28F256K18\n" ); + break; + default: + printf( "Unknown (0x%02X)!\n", cid ); + break; + } + + /* Read Array */ + bus_write( ps, 0 << o, 0x00FF00FF ); +} + +static +void intel_flash_print_info( parts *ps ) +{ + int o = 1; + /* Intel Primary Algorithm Extended Query Table - see Table 5. in [3] */ + /* TODO */ + + /* Clear Status Register */ + bus_write( ps, 0 << o, 0x0050 ); + + /* Read Identifier Command */ + bus_write( ps, 0 << 0, 0x0090 ); + + _intel_flash_print_info( ps, o ); +} + +static +void intel_flash_print_info32( parts *ps ) +{ + int o = 2; + /* Intel Primary Algorithm Extended Query Table - see Table 5. in [3] */ + /* TODO */ + + /* Clear Status Register */ + bus_write( ps, 0 << o, 0x00500050 ); + + /* Read Identifier Command */ + bus_write( ps, 0 << 0, 0x00900090 ); + + _intel_flash_print_info( ps, o ); +} + +static int +intel_flash_erase_block( parts *ps, uint32_t adr ) +{ + uint16_t sr; + + bus_write( ps, 0, CFI_INTEL_CMD_CLEAR_STATUS_REGISTER ); + bus_write( ps, adr, CFI_INTEL_CMD_BLOCK_ERASE ); + bus_write( ps, adr, CFI_INTEL_CMD_CONFIRM ); + + while (!((sr = bus_read( ps, 0 ) & 0xFE) & CFI_INTEL_SR_READY)) ; /* TODO: add timeout */ + + switch (sr & ~CFI_INTEL_SR_READY) { + case 0: + return 0; + case CFI_INTEL_SR_ERASE_ERROR | CFI_INTEL_SR_PROGRAM_ERROR: + printf("flash: invalid command seq\n"); + return CFI_INTEL_ERROR_INVALID_COMMAND_SEQUENCE; + case CFI_INTEL_SR_ERASE_ERROR | CFI_INTEL_SR_VPEN_ERROR: + printf("flash: low vpen\n"); + return CFI_INTEL_ERROR_LOW_VPEN; + case CFI_INTEL_SR_ERASE_ERROR | CFI_INTEL_SR_BLOCK_LOCKED: + printf("flash: block locked\n"); + return CFI_INTEL_ERROR_BLOCK_LOCKED; + default: + break; + } + + return CFI_INTEL_ERROR_UNKNOWN; +} + +static int +intel_flash_unlock_block( parts *ps, uint32_t adr ) +{ + uint16_t sr; + + bus_write( ps, 0, CFI_INTEL_CMD_CLEAR_STATUS_REGISTER ); + bus_write( ps, adr, CFI_INTEL_CMD_LOCK_SETUP ); + bus_write( ps, adr, CFI_INTEL_CMD_UNLOCK_BLOCK ); + + while (!((sr = bus_read( ps, 0 ) & 0xFE) & CFI_INTEL_SR_READY)) ; /* TODO: add timeout */ + + if (sr != CFI_INTEL_SR_READY) { + printf("flash: unknown error while unblocking\n"); + return CFI_INTEL_ERROR_UNKNOWN; + } else + return 0; +} + +static int +intel_flash_program( parts *ps, uint32_t adr, uint32_t data ) +{ + uint16_t sr; + + bus_write( ps, 0, CFI_INTEL_CMD_CLEAR_STATUS_REGISTER ); + bus_write( ps, adr, CFI_INTEL_CMD_PROGRAM1 ); + bus_write( ps, adr, data ); + + while (!((sr = bus_read( ps, 0 ) & 0xFE) & CFI_INTEL_SR_READY)) ; /* TODO: add timeout */ + + if (sr != CFI_INTEL_SR_READY) { + printf("flash: unknown error while programming\n"); + return CFI_INTEL_ERROR_UNKNOWN; + } else + return 0; +} + +static int +intel_flash_erase_block32( parts *ps, uint32_t adr ) +{ + uint32_t sr; + + bus_write( ps, 0, (CFI_INTEL_CMD_CLEAR_STATUS_REGISTER << 16) | CFI_INTEL_CMD_CLEAR_STATUS_REGISTER ); + bus_write( ps, adr, (CFI_INTEL_CMD_BLOCK_ERASE << 16) | CFI_INTEL_CMD_BLOCK_ERASE ); + bus_write( ps, adr, (CFI_INTEL_CMD_CONFIRM << 16) | CFI_INTEL_CMD_CONFIRM ); + + while (((sr = bus_read( ps, 0 ) & 0x00FE00FE) & ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) != ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) ; /* TODO: add timeout */ + + if (sr != ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) { + printf( "\nsr = 0x%08X\n", sr ); + return CFI_INTEL_ERROR_UNKNOWN; + } else + return 0; +} + +static int +intel_flash_unlock_block32( parts *ps, uint32_t adr ) +{ + uint32_t sr; + + bus_write( ps, 0, (CFI_INTEL_CMD_CLEAR_STATUS_REGISTER << 16) | CFI_INTEL_CMD_CLEAR_STATUS_REGISTER ); + bus_write( ps, adr, (CFI_INTEL_CMD_LOCK_SETUP << 16) | CFI_INTEL_CMD_LOCK_SETUP ); + bus_write( ps, adr, (CFI_INTEL_CMD_UNLOCK_BLOCK << 16) | CFI_INTEL_CMD_UNLOCK_BLOCK ); + + while (((sr = bus_read( ps, 0 ) & 0x00FE00FE) & ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) != ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) ; /* TODO: add timeout */ + + if (sr != ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) { + printf( "\nsr = 0x%08X\n", sr ); + return CFI_INTEL_ERROR_UNKNOWN; + } else + return 0; +} + +static int +intel_flash_program32( parts *ps, uint32_t adr, uint32_t data ) +{ + uint32_t sr; + + bus_write( ps, 0, (CFI_INTEL_CMD_CLEAR_STATUS_REGISTER << 16) | CFI_INTEL_CMD_CLEAR_STATUS_REGISTER ); + bus_write( ps, adr, (CFI_INTEL_CMD_PROGRAM1 << 16) | CFI_INTEL_CMD_PROGRAM1 ); + bus_write( ps, adr, data ); + + while (((sr = bus_read( ps, 0 ) & 0x00FE00FE) & ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) != ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) ; /* TODO: add timeout */ + + if (sr != ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) { + printf( "\nsr = 0x%08X\n", sr ); + return CFI_INTEL_ERROR_UNKNOWN; + } else + return 0; +} + +static void +intel_flash_readarray32( parts *ps ) +{ + /* Read Array */ + bus_write( ps, 0, 0x00FF00FF ); +} + +static void +intel_flash_readarray( parts *ps ) +{ + /* Read Array */ + bus_write( ps, 0, 0x00FF00FF ); +} + +flash_driver_t intel_32_flash_driver = { + 4, /* buswidth */ + "Intel Standard Command Set", + "supported: 28Fxxxx, 2 x 16 bit", + intel_flash_autodetect32, + intel_flash_print_info32, + intel_flash_erase_block32, + intel_flash_unlock_block32, + intel_flash_program32, + intel_flash_readarray32, +}; + +flash_driver_t intel_16_flash_driver = { + 2, /* buswidth */ + "Intel Standard Command Set", + "supported: 28Fxxxx, 1 x 16 bit", + intel_flash_autodetect, + intel_flash_print_info, + intel_flash_erase_block, + intel_flash_unlock_block, + intel_flash_program, + intel_flash_readarray, +}; diff --git a/jtag/src/flash.c b/jtag/src/flash.c index e60e8dda..e7d1d389 100644 --- a/jtag/src/flash.c +++ b/jtag/src/flash.c @@ -43,25 +43,45 @@ #include "part.h" #include "bus.h" +#include "flash.h" -int flash_erase_block( parts *ps, uint32_t adr ); -int flash_unlock_block( parts *ps, uint32_t adr ); -int flash_program( parts *ps, uint32_t adr, uint32_t data ); -int flash_erase_block32( parts *ps, uint32_t adr ); -int flash_unlock_block32( parts *ps, uint32_t adr ); -int flash_program32( parts *ps, uint32_t adr, uint32_t data ); +extern flash_driver_t amd_32_flash_driver; +extern flash_driver_t intel_32_flash_driver; -cfi_query_structure_t *detect_cfi( parts *ps ); +flash_driver_t *flash_drivers[] = { + &amd_32_flash_driver, + &intel_32_flash_driver, + NULL +}; + +flash_driver_t *flash_driver = NULL; void -flashmsbin( parts *ps, FILE *f ) +set_flash_driver( parts *ps, cfi_query_structure_t *cfi ) +{ + int i; + flash_driver = NULL; + + for (i = 0; flash_drivers[i] != NULL; i++) + if (flash_drivers[i]->flash_autodetect( ps, cfi )) { + flash_driver = flash_drivers[i]; + return; + } + + printf( "unknown flash - vendor id: %d (0x%04x)\n", + cfi->identification_string.pri_id_code, + cfi->identification_string.pri_id_code ); +} + +/* check for flashmem - set driver */ +void +flashcheck( parts *ps, cfi_query_structure_t **cfi ) { part *p = ps->parts[0]; int o = 0; - uint32_t adr; - cfi_query_structure_t *cfi; + flash_driver = NULL; - printf( "Note: Supported configuration is 2 x 16 bit only\n" ); + printf( "Note: Supported configuration is 2 x 16 bit or 1 x 16 bit only\n" ); switch (bus_width( ps )) { case 16: @@ -79,11 +99,30 @@ flashmsbin( parts *ps, FILE *f ) part_set_instruction( p, "EXTEST" ); parts_shift_instructions( ps ); - cfi = detect_cfi( ps ); - if (!cfi) { + *cfi = detect_cfi( ps ); + if (!*cfi) { printf( "Flash not found!\n" ); return; } + set_flash_driver( ps, *cfi ); + if (!flash_driver) { + printf( "Flash not supported!\n" ); + return; + } + flash_driver->flash_print_info( ps ); +} + +void +flashmsbin( parts *ps, FILE *f ) +{ + uint32_t adr; + cfi_query_structure_t *cfi = 0; + + flashcheck( ps, &cfi ); + if (!cfi || !flash_driver) { + printf( "no flash driver found\n" ); + return; + } /* test sync bytes */ { @@ -108,9 +147,9 @@ flashmsbin( parts *ps, FILE *f ) last = (start + len - 1) / (cfi->device_geometry.erase_block_regions[0].erase_block_size * 2); for (; first <= last; first++) { adr = first * cfi->device_geometry.erase_block_regions[0].erase_block_size * 2; - flash_unlock_block32( ps, adr ); + flash_unlock_block( ps, adr ); printf( "block %d unlocked\n", first ); - printf( "erasing block %d: %d\n", first, flash_erase_block32( ps, adr ) ); + printf( "erasing block %d: %d\n", first, flash_erase_block( ps, adr ) ); } } @@ -137,9 +176,10 @@ flashmsbin( parts *ps, FILE *f ) uint32_t data; printf( "addr: 0x%08X\r", a ); + fflush(stdout); fread( &data, sizeof data, 1, f ); - if (flash_program32( ps, a, data )) { - printf( "\nflash error\n" ); + if (flash_program( ps, a, data )) { + printf( "\nflash error 1\n" ); return; } a += 4; @@ -148,8 +188,7 @@ flashmsbin( parts *ps, FILE *f ) } printf( "\n" ); - /* Read Array */ - bus_write( ps, 0 << o, 0x00FF00FF ); + flash_readarray(ps); fseek( f, 15, SEEK_SET ); printf( "verify:\n" ); @@ -176,10 +215,12 @@ flashmsbin( parts *ps, FILE *f ) uint32_t data, readed; printf( "addr: 0x%08X\r", a ); + fflush( stdout ); fread( &data, sizeof data, 1, f ); readed = bus_read( ps, a ); if (data != readed) { - printf( "\nverify error: 0x%08X vs. 0x%08X\n", readed, data ); + printf( "\nverify error: 0x%08X vs. 0x%08X at addr %08X\n", + readed, data, a ); return; } a += 4; @@ -198,34 +239,14 @@ flashmsbin( parts *ps, FILE *f ) void flashmem( parts *ps, FILE *f, uint32_t addr ) { - part *p = ps->parts[0]; - int o = 0; uint32_t adr; - cfi_query_structure_t *cfi; + cfi_query_structure_t *cfi = NULL; int *erased; int i; - printf( "Note: Supported configuration is 2 x 16 bit or 1 x 16 bit only\n" ); - - switch (bus_width( ps )) { - case 16: - o = 1; - break; - case 32: - o = 2; - break; - default: - printf( "Error: Unknown bus width!\n" ); - return; - } - - /* EXTEST */ - part_set_instruction( p, "EXTEST" ); - parts_shift_instructions( ps ); - - cfi = detect_cfi( ps ); - if (!cfi) { - printf( "Flash not found!\n" ); + flashcheck( ps, &cfi ); + if (!cfi || !flash_driver) { + printf( "no flash driver found\n" ); return; } @@ -244,41 +265,36 @@ flashmem( parts *ps, FILE *f, uint32_t addr ) #define BSIZE 4096 char b[BSIZE]; int bc = 0, bn = 0; - /* FIXME: block_no is probably invalid for 1 x 16 bit memory configuration */ - int block_no = adr / (cfi->device_geometry.erase_block_regions[0].erase_block_size * 2); + int block_no = adr / (cfi->device_geometry.erase_block_regions[0].erase_block_size * flash_driver->buswidth / 2); printf( "addr: 0x%08X\r", adr ); + fflush( stdout ); if (!erased[block_no]) { - if (o == 1) - flash_unlock_block( ps, adr ); - else - flash_unlock_block32( ps, adr ); + flash_unlock_block( ps, adr ); printf( "block %d unlocked\n", block_no ); - printf( "erasing block %d: %d\n", block_no, - (o == 1) ? flash_erase_block( ps, adr ) : flash_erase_block32( ps, adr ) ); + printf( "erasing block %d: %d\n", block_no, flash_erase_block( ps, adr ) ); erased[block_no] = 1; } bn = fread( b, 1, BSIZE, f ); printf("addr 0x%08X (n is %d)\n", adr, bn); - for (bc = 0; bc < bn; bc += (o == 1) ? 2 : 4) { - if (o == 1) + for (bc = 0; bc < bn; bc += flash_driver->buswidth) { + if (flash_driver->buswidth == 2) data = htons( *((uint16_t *) &b[bc]) ); else data = * ((uint32_t *) &b[bc]); - if ((o == 1) ? flash_program( ps, adr, data ) : flash_program32( ps, adr, data )) { - printf( "\nflash error\n" ); + if (flash_program( ps, adr, data )) { + printf( "\nflash error 2\n" ); return; } - adr += (o == 1) ? 2 : 4; + adr += flash_driver->buswidth; } } printf( "\n" ); - /* Read Array */ - bus_write( ps, 0 << o, 0x00FF00FF ); + flash_readarray( ps ); - if (o != 1) { /* TODO: not available in 1 x 16 bit mode */ + if (flash_driver->buswidth == 2) { /* TODO: not available in 1 x 16 bit mode */ fseek( f, 0, SEEK_SET ); printf( "verify:\n" ); adr = addr; @@ -286,13 +302,14 @@ flashmem( parts *ps, FILE *f, uint32_t addr ) uint32_t data; uint32_t readed; printf( "addr: 0x%08X\r", adr ); - fread( &data, sizeof data, 1, f ); + fflush( stdout ); + fread( &data, flash_driver->buswidth, 1, f ); readed = bus_read( ps, adr ); if (data != readed) { - printf( "\nverify error: 0x%08X vs. 0x%08X\n", readed, data ); + printf( "\nverify error: 0x%08X vs. 0x%08X at addr %08X\n", readed, data, adr ); return; } - adr += 4; + adr += flash_driver->buswidth; } printf( "\nDone.\n" ); } else @@ -305,127 +322,6 @@ flashmem( parts *ps, FILE *f, uint32_t addr ) free( erased ); } -#define CFI_INTEL_ERROR_UNKNOWN 1 -#define CFI_INTEL_ERROR_UNSUPPORTED 2 -#define CFI_INTEL_ERROR_LOW_VPEN 3 -#define CFI_INTEL_ERROR_BLOCK_LOCKED 4 -#define CFI_INTEL_ERROR_INVALID_COMMAND_SEQUENCE 5 - -int -flash_erase_block( parts *ps, uint32_t adr ) -{ - uint16_t sr; - - bus_write( ps, 0, CFI_INTEL_CMD_CLEAR_STATUS_REGISTER ); - bus_write( ps, adr, CFI_INTEL_CMD_BLOCK_ERASE ); - bus_write( ps, adr, CFI_INTEL_CMD_CONFIRM ); - - while (!((sr = bus_read( ps, 0 ) & 0xFE) & CFI_INTEL_SR_READY)) ; /* TODO: add timeout */ - - switch (sr & ~CFI_INTEL_SR_READY) { - case 0: - return 0; - case CFI_INTEL_SR_ERASE_ERROR | CFI_INTEL_SR_PROGRAM_ERROR: - printf("flash: invalid command seq\n"); - return CFI_INTEL_ERROR_INVALID_COMMAND_SEQUENCE; - case CFI_INTEL_SR_ERASE_ERROR | CFI_INTEL_SR_VPEN_ERROR: - printf("flash: low vpen\n"); - return CFI_INTEL_ERROR_LOW_VPEN; - case CFI_INTEL_SR_ERASE_ERROR | CFI_INTEL_SR_BLOCK_LOCKED: - printf("flash: block locked\n"); - return CFI_INTEL_ERROR_BLOCK_LOCKED; - default: - break; - } - - return CFI_INTEL_ERROR_UNKNOWN; -} - -int -flash_unlock_block( parts *ps, uint32_t adr ) -{ - uint16_t sr; - - bus_write( ps, 0, CFI_INTEL_CMD_CLEAR_STATUS_REGISTER ); - bus_write( ps, adr, CFI_INTEL_CMD_LOCK_SETUP ); - bus_write( ps, adr, CFI_INTEL_CMD_UNLOCK_BLOCK ); - - while (!((sr = bus_read( ps, 0 ) & 0xFE) & CFI_INTEL_SR_READY)) ; /* TODO: add timeout */ - - if (sr != CFI_INTEL_SR_READY) { - printf("flash: unknown error while unblocking\n"); - return CFI_INTEL_ERROR_UNKNOWN; - } else - return 0; -} - -int -flash_program( parts *ps, uint32_t adr, uint32_t data ) -{ - uint16_t sr; - - bus_write( ps, 0, CFI_INTEL_CMD_CLEAR_STATUS_REGISTER ); - bus_write( ps, adr, CFI_INTEL_CMD_PROGRAM1 ); - bus_write( ps, adr, data ); - - while (!((sr = bus_read( ps, 0 ) & 0xFE) & CFI_INTEL_SR_READY)) ; /* TODO: add timeout */ - - if (sr != CFI_INTEL_SR_READY) { - printf("flash: unknown error while programming\n"); - return CFI_INTEL_ERROR_UNKNOWN; - } else - return 0; -} -int -flash_erase_block32( parts *ps, uint32_t adr ) -{ - uint32_t sr; - - bus_write( ps, 0, (CFI_INTEL_CMD_CLEAR_STATUS_REGISTER << 16) | CFI_INTEL_CMD_CLEAR_STATUS_REGISTER ); - bus_write( ps, adr, (CFI_INTEL_CMD_BLOCK_ERASE << 16) | CFI_INTEL_CMD_BLOCK_ERASE ); - bus_write( ps, adr, (CFI_INTEL_CMD_CONFIRM << 16) | CFI_INTEL_CMD_CONFIRM ); - while (((sr = bus_read( ps, 0 ) & 0x00FE00FE) & ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) != ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) ; /* TODO: add timeout */ - - if (sr != ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) { - printf( "\nsr = 0x%08X\n", sr ); - return CFI_INTEL_ERROR_UNKNOWN; - } else - return 0; -} - -int -flash_unlock_block32( parts *ps, uint32_t adr ) -{ - uint32_t sr; - bus_write( ps, 0, (CFI_INTEL_CMD_CLEAR_STATUS_REGISTER << 16) | CFI_INTEL_CMD_CLEAR_STATUS_REGISTER ); - bus_write( ps, adr, (CFI_INTEL_CMD_LOCK_SETUP << 16) | CFI_INTEL_CMD_LOCK_SETUP ); - bus_write( ps, adr, (CFI_INTEL_CMD_UNLOCK_BLOCK << 16) | CFI_INTEL_CMD_UNLOCK_BLOCK ); - while (((sr = bus_read( ps, 0 ) & 0x00FE00FE) & ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) != ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) ; /* TODO: add timeout */ - - if (sr != ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) { - printf( "\nsr = 0x%08X\n", sr ); - return CFI_INTEL_ERROR_UNKNOWN; - } else - return 0; -} - -int -flash_program32( parts *ps, uint32_t adr, uint32_t data ) -{ - uint32_t sr; - - bus_write( ps, 0, (CFI_INTEL_CMD_CLEAR_STATUS_REGISTER << 16) | CFI_INTEL_CMD_CLEAR_STATUS_REGISTER ); - bus_write( ps, adr, (CFI_INTEL_CMD_PROGRAM1 << 16) | CFI_INTEL_CMD_PROGRAM1 ); - bus_write( ps, adr, data ); - - while (((sr = bus_read( ps, 0 ) & 0x00FE00FE) & ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) != ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) ; /* TODO: add timeout */ - - if (sr != ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) { - printf( "\nsr = 0x%08X\n", sr ); - return CFI_INTEL_ERROR_UNKNOWN; - } else - return 0; -} diff --git a/jtag/src/flash.h b/jtag/src/flash.h new file mode 100644 index 00000000..ca573980 --- /dev/null +++ b/jtag/src/flash.h @@ -0,0 +1,66 @@ +/* + * $Id$ + * + * Flash Memory interface + * Copyright (C) 2003 AH + * + * 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 August Hörandl + * + */ + +#ifndef FLASH_H +#define FLASH_H + +#include +#include "part.h" +#include "bus.h" +#include + +/* from cfi.c */ +cfi_query_structure_t *detect_cfi( parts *ps ); + +typedef struct { + int buswidth; /* supported bus width, 1/2/4 bytes */ + const char *name; + const char *description; + int (*flash_autodetect)( parts *ps, cfi_query_structure_t * ); + void (*flash_print_info)( parts *ps ); + int (*flash_erase_block)( parts *ps, uint32_t adr ); + int (*flash_unlock_block)( parts *ps, uint32_t adr ); + int (*flash_program)( parts *ps, uint32_t adr, uint32_t data ); + void (*flash_readarray)( parts *ps ); +} flash_driver_t; + +extern flash_driver_t *flash_driver; +extern flash_driver_t *flash_drivers[]; + +/* #define flash_print_info flash_driver->flash_print_info */ +#define flash_erase_block flash_driver->flash_erase_block +#define flash_unlock_block flash_driver->flash_unlock_block +#define flash_program flash_driver->flash_program +#define flash_readarray flash_driver->flash_readarray + +extern void set_flash_driver( parts *ps, cfi_query_structure_t *cfi ); + +#define CFI_INTEL_ERROR_UNKNOWN 1 +#define CFI_INTEL_ERROR_UNSUPPORTED 2 +#define CFI_INTEL_ERROR_LOW_VPEN 3 +#define CFI_INTEL_ERROR_BLOCK_LOCKED 4 +#define CFI_INTEL_ERROR_INVALID_COMMAND_SEQUENCE 5 + +#endif /* FLASH_H */ diff --git a/jtag/src/help.c b/jtag/src/help.c index a7f0a446..77174085 100644 --- a/jtag/src/help.c +++ b/jtag/src/help.c @@ -30,6 +30,7 @@ #include #include "cable.h" +#include "flash.h" void help( const char *cmd ) @@ -169,7 +170,8 @@ help( const char *cmd ) "\n" "readmem works only with part 0. Part 0 must support bus operations.\n" ); - else if (strcmp( cmd, "flashmem" ) == 0) + else if (strcmp( cmd, "flashmem" ) == 0) { + int i; printf( "Usage: flashmem ADDR FILENAME\n" "Usage: flashmem msbin FILENAME\n" @@ -182,7 +184,11 @@ help( const char *cmd ) "ADDR could be in decimal or hexadecimal (prefixed with 0x) form.\n" "\n" "flashmem works only with part 0. Part 0 must support bus operations.\n" + "Supported Flash Memories\n" ); + for (i = 0; flash_drivers[i]; i++) + printf( "%s\n %s\n", flash_drivers[i]->name, flash_drivers[i]->description ); + } else if (strcmp( cmd, "set" ) == 0) printf( "Usage: set signal PART SIGNAL DIR [DATA]\n" diff --git a/jtag/src/readmem.c b/jtag/src/readmem.c index 00d6c4f5..8b17cf64 100644 --- a/jtag/src/readmem.c +++ b/jtag/src/readmem.c @@ -44,6 +44,7 @@ #include "part.h" #include "bus.h" +#include "flash.h" cfi_query_structure_t *detect_cfi( parts * ); @@ -168,7 +169,10 @@ detectflash( parts *ps ) /* see 3.3.4 in [1] */ printf( "Device Geometry Definition:\n" ); - printf( "\tDevice Size: %d B\n", cfi->device_geometry.device_size ); + printf( "\tDevice Size: %d B (%d KiB, %d MiB)\n", + cfi->device_geometry.device_size, + cfi->device_geometry.device_size / 1024, + cfi->device_geometry.device_size / (1024 * 1024) ); printf( "\tFlash Device Interface description: 0x%04X ", cfi->device_geometry.device_interface ); /* see Section 2. in [4] */ switch (cfi->device_geometry.device_interface) { @@ -199,65 +203,16 @@ detectflash( parts *ps ) for (i = 0; i < cfi->device_geometry.number_of_erase_regions; i++) { printf( "\t\tRegion %d:\n", i ); - printf( "\t\t\tErase Block Size: %d\n", cfi->device_geometry.erase_block_regions[i].erase_block_size ); + printf( "\t\t\tErase Block Size: %d B (%d KiB)\n", + cfi->device_geometry.erase_block_regions[i].erase_block_size, + cfi->device_geometry.erase_block_regions[i].erase_block_size / 1024 ); printf( "\t\t\tNumber of Erase Blocks: %d\n", cfi->device_geometry.erase_block_regions[i].number_of_erase_blocks ); } } - /* Intel Primary Algorithm Extended Query Table - see Table 5. in [3] */ - /* TODO */ - - /* Clear Status Register */ - bus_write( ps, 0 << o, 0x00500050 ); - - /* Read Identifier Command */ - bus_write( ps, 0 << 0, 0x00900090 ); - - switch (bus_read( ps, 0x00 << o ) & 0xFF) { - case STD_MIC_INTEL: - printf( "Manufacturer: %s\n", STD_MICN_INTEL ); - break; - default: - printf( "Unknown manufacturer!\n" ); - break; - } - - printf( "Chip: " ); - switch (bus_read( ps, 0x01 << o ) & 0xFFFF) { - case 0x0016: - printf( "28F320J3A\n" ); - break; - case 0x0017: - printf( "28F640J3A\n" ); - break; - case 0x0018: - printf( "28F128J3A\n" ); - break; - case 0x8801: - printf( "28F640K3\n" ); - break; - case 0x8802: - printf( "28F128K3\n" ); - break; - case 0x8803: - printf( "28F256K3\n" ); - break; - case 0x8805: - printf( "28F640K18\n" ); - break; - case 0x8806: - printf( "28F128K18\n" ); - break; - case 0x8807: - printf( "28F256K18\n" ); - break; - default: - printf( "Unknown!\n" ); - break; - } - - /* Read Array */ - bus_write( ps, 0 << o, 0x00FF00FF ); + set_flash_driver( ps, cfi ); + if (flash_driver) + flash_driver->flash_print_info( ps ); } void