From 94c1b0ccc03c266b7bd40cba25add6d34b4d97fa Mon Sep 17 00:00:00 2001 From: "Ronald G. Minnich" Date: Sat, 25 Oct 2003 17:01:29 +0000 Subject: [PATCH] due to popular demand, added flash_and_burn to the freebios2 tree. git-svn-id: svn://svn.coreboot.org/coreboot/trunk@1232 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1 --- util/flash_and_burn/82802ab.c | 199 +++++++++++ util/flash_and_burn/82802ab.h | 48 +++ util/flash_and_burn/Makefile | 30 ++ util/flash_and_burn/README | 20 ++ util/flash_and_burn/am29f040b.c | 130 ++++++++ util/flash_and_burn/am29f040b.h | 8 + util/flash_and_burn/flash.h | 55 ++++ util/flash_and_burn/flash_on.c | 77 +++++ util/flash_and_burn/flash_rom.c | 543 +++++++++++++++++++++++++++++++ util/flash_and_burn/jedec.c | 98 ++++++ util/flash_and_burn/jedec.h | 66 ++++ util/flash_and_burn/m29f400bt.c | 189 +++++++++++ util/flash_and_burn/m29f400bt.h | 74 +++++ util/flash_and_burn/msys_doc.c | 289 ++++++++++++++++ util/flash_and_burn/msys_doc.h | 110 +++++++ util/flash_and_burn/mx29f002.c | 119 +++++++ util/flash_and_burn/mx29f002.h | 8 + util/flash_and_burn/sst28sf040.c | 169 ++++++++++ util/flash_and_burn/sst28sf040.h | 8 + util/flash_and_burn/sst39sf020.c | 199 +++++++++++ util/flash_and_burn/sst39sf020.h | 8 + util/flash_and_burn/w49f002u.c | 119 +++++++ util/flash_and_burn/w49f002u.h | 8 + 23 files changed, 2574 insertions(+) create mode 100644 util/flash_and_burn/82802ab.c create mode 100644 util/flash_and_burn/82802ab.h create mode 100644 util/flash_and_burn/Makefile create mode 100644 util/flash_and_burn/README create mode 100644 util/flash_and_burn/am29f040b.c create mode 100644 util/flash_and_burn/am29f040b.h create mode 100644 util/flash_and_burn/flash.h create mode 100644 util/flash_and_burn/flash_on.c create mode 100644 util/flash_and_burn/flash_rom.c create mode 100644 util/flash_and_burn/jedec.c create mode 100644 util/flash_and_burn/jedec.h create mode 100644 util/flash_and_burn/m29f400bt.c create mode 100644 util/flash_and_burn/m29f400bt.h create mode 100644 util/flash_and_burn/msys_doc.c create mode 100644 util/flash_and_burn/msys_doc.h create mode 100644 util/flash_and_burn/mx29f002.c create mode 100644 util/flash_and_burn/mx29f002.h create mode 100644 util/flash_and_burn/sst28sf040.c create mode 100644 util/flash_and_burn/sst28sf040.h create mode 100644 util/flash_and_burn/sst39sf020.c create mode 100644 util/flash_and_burn/sst39sf020.h create mode 100644 util/flash_and_burn/w49f002u.c create mode 100644 util/flash_and_burn/w49f002u.h diff --git a/util/flash_and_burn/82802ab.c b/util/flash_and_burn/82802ab.c new file mode 100644 index 0000000000..c818e92151 --- /dev/null +++ b/util/flash_and_burn/82802ab.c @@ -0,0 +1,199 @@ +/* + * 82802ab.c: driver for programming JEDEC standard flash parts + * + * + * Copyright 2000 Silicon Integrated System Corporation + * + * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * Reference: http://www.intel.com/design/chipsets/datashts/290658.htm + * + * $Id$ + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "flash.h" +#include "82802ab.h" + +// I need that Berkeley bit-map printer +void print_82802ab_status(unsigned char status) +{ + printf("%s", status & 0x80 ? "Ready:" : "Busy:"); + printf("%s", status & 0x40 ? "BE SUSPEND:" : "BE RUN/FINISH:"); + printf("%s", status & 0x20 ? "BE ERROR:" : "BE OK:"); + printf("%s", status & 0x10 ? "PROG ERR:" : "PROG OK:"); + printf("%s", status & 0x8 ? "VP ERR:" : "VPP OK:"); + printf("%s", status & 0x4 ? "PROG SUSPEND:" : "PROG RUN/FINISH:"); + printf("%s", status & 0x2 ? "WP|TBL#|WP#,ABORT:" : "UNLOCK:"); +} + +int probe_82802ab (struct flashchip * flash) +{ + volatile unsigned char * bios = flash->virt_addr; + unsigned char id1, id2; + +#if 0 + *(volatile unsigned char *) (bios + 0x5555) = 0xAA; + *(volatile unsigned char *) (bios + 0x2AAA) = 0x55; + *(volatile unsigned char *) (bios + 0x5555) = 0x90; +#endif + + *bios = 0xff; + myusec_delay(10); + *bios = 0x90; + myusec_delay(10); + + id1 = *(volatile unsigned char *) bios; + id2 = *(volatile unsigned char *) (bios + 0x01); + +#if 1 + *(volatile unsigned char *) (bios + 0x5555) = 0xAA; + *(volatile unsigned char *) (bios + 0x2AAA) = 0x55; + *(volatile unsigned char *) (bios + 0x5555) = 0xF0; + +#endif + myusec_delay(10); + + printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2); + + if (id1 == flash->manufacture_id && id2 == flash->model_id) { + size_t size = flash->total_size * 1024; + // we need to mmap the write-protect space. + bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED, + flash->fd_mem, (off_t) (0 - 0x400000 - size)); + if (bios == MAP_FAILED) { + // it's this part but we can't map it ... + perror("Error MMAP /dev/mem"); + exit(1); + } + + flash->virt_addr_2 = bios; + return 1; + } + + return 0; +} + +unsigned char wait_82802ab(volatile unsigned char * bios) +{ + + unsigned char status; + unsigned char id1, id2; + + *bios = 0x70; + if ((*bios & 0x80) == 0) { // it's busy + while ((*bios & 0x80) == 0) + ; + } + + status = *bios; + + // put another command to get out of status register mode + + *bios = 0x90; + myusec_delay(10); + + id1 = *(volatile unsigned char *) bios; + id2 = *(volatile unsigned char *) (bios + 0x01); + + // this is needed to jam it out of "read id" mode + *(volatile unsigned char *) (bios + 0x5555) = 0xAA; + *(volatile unsigned char *) (bios + 0x2AAA) = 0x55; + *(volatile unsigned char *) (bios + 0x5555) = 0xF0; + return status; + +} +int erase_82802ab_block(struct flashchip *flash, int offset) +{ + volatile unsigned char * bios = flash->virt_addr + offset; + volatile unsigned char *wrprotect = flash->virt_addr_2 + offset + 2; + unsigned char status; + + // clear status register + *bios = 0x50; + //printf("Erase at %p\n", bios); + // clear write protect + //printf("write protect is at %p\n", (wrprotect)); + //printf("write protect is 0x%x\n", *(wrprotect)); + *(wrprotect) = 0; + //printf("write protect is 0x%x\n", *(wrprotect)); + + // now start it + *(volatile unsigned char *) (bios) = 0x20; + *(volatile unsigned char *) (bios) = 0xd0; + myusec_delay(10); + // now let's see what the register is + status = wait_82802ab(flash->virt_addr); + //print_82802ab_status(status); + printf("DONE BLOCK 0x%x\n", offset); + return(0); +} +int erase_82802ab (struct flashchip * flash) +{ + int i; + unsigned int total_size = flash->total_size * 1024; + + printf("total_size is %d; flash->page_size is %d\n", + total_size, flash->page_size); + for(i = 0; i < total_size; i += flash->page_size) + erase_82802ab_block(flash, i); + printf("DONE ERASE\n"); + return(0); +} + +void write_page_82802ab (volatile char * bios, char * src, volatile char * dst, + int page_size) +{ + int i; + + for (i = 0; i < page_size; i++) { + /* transfer data from source to destination */ + *dst = 0x40; + *dst++ = *src++; + wait_82802ab(bios); + } + +} + +int write_82802ab (struct flashchip * flash, unsigned char * buf) +{ + int i; + int total_size = flash->total_size *1024, page_size = flash->page_size; + volatile unsigned char * bios = flash->virt_addr; + + erase_82802ab (flash); + if (*bios != 0xff) { + printf("ERASE FAILED\n"); + return -1; + } + printf ("Programming Page: "); + for (i = 0; i < total_size/page_size; i++) { + printf ("%04d at address: 0x%08x", i, i * page_size); + write_page_82802ab(bios, buf + i * page_size, bios + i * page_size, + page_size); + printf ("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); + } + printf("\n"); + protect_82802ab (bios); + return(0); +} diff --git a/util/flash_and_burn/82802ab.h b/util/flash_and_burn/82802ab.h new file mode 100644 index 0000000000..61e7f100af --- /dev/null +++ b/util/flash_and_burn/82802ab.h @@ -0,0 +1,48 @@ +#ifndef __82802AB_H__ +#define __82802AB_H__ 1 + +extern int probe_82802ab (struct flashchip * flash); +extern int erase_82802ab (struct flashchip * flash); +extern int write_82802ab (struct flashchip * flash, unsigned char * buf); + +extern __inline__ void toggle_ready_82802ab (volatile char * dst) +{ + unsigned int i = 0; + char tmp1, tmp2; + + tmp1 = *dst & 0x40; + + while (i++ < 0xFFFFFF) { + tmp2 = *dst & 0x40; + if (tmp1 == tmp2) { + break; + } + tmp1 = tmp2; + } +} + +extern __inline__ void data_polling_82802ab (volatile char * dst, char data) +{ + unsigned int i = 0; + char tmp; + + data &= 0x80; + + while (i++ < 0xFFFFFF) { + tmp = *dst & 0x80; + if (tmp == data) { + break; + } + } +} + +extern __inline__ void protect_82802ab (volatile char * bios) +{ + *(volatile char *) (bios + 0x5555) = 0xAA; + *(volatile char *) (bios + 0x2AAA) = 0x55; + *(volatile char *) (bios + 0x5555) = 0xA0; + + usleep(200); +} + +#endif /* !__82802AB_H__ */ diff --git a/util/flash_and_burn/Makefile b/util/flash_and_burn/Makefile new file mode 100644 index 0000000000..5c8ffe9d42 --- /dev/null +++ b/util/flash_and_burn/Makefile @@ -0,0 +1,30 @@ +OBJS = jedec.o sst28sf040.o am29f040b.o mx29f002.c sst39sf020.o m29f400bt.o \ + w49f002u.o 82802ab.o msys_doc.o +CC = gcc -O2 -g -Wall -Werror + +all: flash_rom flash_on + +flash_rom: flash_rom.o ${OBJS} + ${CC} -o flash_rom flash_rom.c ${OBJS} -lpci + +flash_on: flash_on.c + ${CC} -o flash_on flash_on.c + +clean: + rm -f flash_rom flash_on *.o *~ + +flash_rom.o: flash_rom.c flash.h jedec.h \ + 82802ab.h am29f040b.h m29f400bt.h msys_doc.h mx29f002.h sst28sf040.h \ + sst39sf020.h w49f002u.h +flash_on.o: flash_on.c + +82802ab.o: 82802ab.c 82802ab.h flash.h +am29f040b.o: am29f040b.c am29f040b.h jedec.h flash.h +m29f400bt.o: m29f400bt.c m29f400bt.h flash.h +msys_doc.o: msys_doc.c msys_doc.h flash.h +mx29f002.o: mx29f002.c mx29f002.h jedec.h flash.h +sst28sf040.o: sst28sf040.c sst28sf040.h jedec.h flash.h +sst39sf020.o: sst39sf020.c sst39sf020.h jedec.h flash.h +w49f002u.o: w49f002u.c w49f002u.h jedec.h flash.h + +jedec.o: jedec.c jedec.h flash.h diff --git a/util/flash_and_burn/README b/util/flash_and_burn/README new file mode 100644 index 0000000000..48f6fb8754 --- /dev/null +++ b/util/flash_and_burn/README @@ -0,0 +1,20 @@ +on the cs5530 southbridge, + +setpci -s 0:12.0 52.b=ee +setpci -x 0:12.0 5b.b= 0x20 (| with whatever is there) + +I am making this a general-purpose userland flash burner -- RGM + +Earlier notes from Ollie: + +Here is some utilities for using/programming flash ROM on SiS 630/950 M/Bs + + 1. flash_on, turnning on the flash writer enable for 630/950 M/Bs, + you have to run this before load DoC drivers. + + 2. flash_rom, use your 630/950 M/Bs as a flash programmer for some + flash parts. This utility is made as modular as possible. If + you find your flash part is not supported, you can add a driver + your own. Or sending me the data sheet. + +Ollie diff --git a/util/flash_and_burn/am29f040b.c b/util/flash_and_burn/am29f040b.c new file mode 100644 index 0000000000..89cdff9806 --- /dev/null +++ b/util/flash_and_burn/am29f040b.c @@ -0,0 +1,130 @@ +/* + * am29f040.c: driver for programming AMD am29f040b models + * + * + * Copyright 2000 Silicon Integrated System Corporation + * + * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * Reference: + * AMD Am29F040B data sheet + * $Id$ + */ + +#include +#include "flash.h" +#include "jedec.h" + +static __inline__ int erase_sector_29f040b (volatile char * bios, unsigned long address) +{ + *(bios + 0x555) = 0xAA; + *(bios + 0x2AA) = 0x55; + *(bios + 0x555) = 0x80; + *(bios + 0x555) = 0xAA; + *(bios + 0x2AA) = 0x55; + *(bios + address) = 0x30; + + sleep(2); + + /* wait for Toggle bit ready */ + toggle_ready_jedec(bios + address); + + return(0); +} + +static __inline__ int write_sector_29f040b(volatile char * bios, unsigned char * src, + volatile unsigned char * dst, unsigned int page_size) +{ + int i; + + for (i = 0; i < page_size; i++) { + printf("0x%08lx", (unsigned long) dst - (unsigned long) bios); + + *(bios + 0x555) = 0xAA; + *(bios + 0x2AA) = 0x55; + *(bios + 0x555) = 0xA0; + *dst++ = *src++; + + /* wait for Toggle bit ready */ + toggle_ready_jedec(bios); + + printf("\b\b\b\b\b\b\b\b\b\b"); + } + + return(0); +} + +int probe_29f040b (struct flashchip * flash) +{ + volatile char * bios = flash->virt_addr; + unsigned char id1, id2; + + *(bios + 0x555) = 0xAA; + *(bios + 0x2AA) = 0x55; + *(bios + 0x555) = 0x90; + + id1 = *(unsigned char *) bios; + id2 = *(unsigned char *) (bios + 0x01); + + *bios = 0xF0; + + myusec_delay(10); + + printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2); + if (id1 == flash->manufacture_id && id2 == flash->model_id) + return 1; + + return 0; +} + +int erase_29f040b (struct flashchip * flash) +{ + volatile char * bios = flash->virt_addr; + + *(bios + 0x555) = 0xAA; + *(bios + 0x2AA) = 0x55; + *(bios + 0x555) = 0x80; + *(bios + 0x555) = 0xAA; + *(bios + 0x2AA) = 0x55; + *(bios + 0x555) = 0x10; + + myusec_delay(10); + toggle_ready_jedec(bios); + + return(0); +} + +int write_29f040b (struct flashchip * flash, unsigned char * buf) +{ + int i; + int total_size = flash->total_size * 1024, page_size = flash->page_size; + volatile char * bios = flash->virt_addr; + + printf ("Programming Page: "); + for (i = 0; i < total_size/page_size; i++) { + /* erase the page before programming */ + erase_sector_29f040b(bios, i * page_size); + + /* write to the sector */ + printf ("%04d at address: ", i); + write_sector_29f040b(bios, buf + i * page_size, bios + i * page_size, + page_size); + printf ("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); + } + printf("\n"); + + return(0); +} diff --git a/util/flash_and_burn/am29f040b.h b/util/flash_and_burn/am29f040b.h new file mode 100644 index 0000000000..dee8f629a6 --- /dev/null +++ b/util/flash_and_burn/am29f040b.h @@ -0,0 +1,8 @@ +#ifndef __AM29F040B_H__ +#define __AM29F040B_H__ 1 + +extern int probe_29f040b (struct flashchip * flash); +extern int erase_29f040b (struct flashchip * flash); +extern int write_29f040b (struct flashchip * flash, unsigned char * buf); + +#endif /* !__AM29F040B_H__ */ diff --git a/util/flash_and_burn/flash.h b/util/flash_and_burn/flash.h new file mode 100644 index 0000000000..0e62b89753 --- /dev/null +++ b/util/flash_and_burn/flash.h @@ -0,0 +1,55 @@ +#ifndef __FLASH_H__ +#define __FLASH_H__ 1 + +#include +#include + +struct flashchip { + char * name; + int manufacture_id; + int model_id; + + volatile char * virt_addr; + int total_size; + int page_size; + + int (*probe) (struct flashchip * flash); + int (*erase) (struct flashchip * flash); + int (*write) (struct flashchip * flash, unsigned char * buf); + int (*read) (struct flashchip * flash, unsigned char * buf); + + int fd_mem; + volatile char *virt_addr_2; +}; + +#define AMD_ID 0x01 +#define AM_29F040B 0xA4 + +#define ATMEL_ID 0x1F /* Winbond Manufacture ID code */ +#define AT_29C040A 0xA4 /* Winbond w29c020c device code*/ + +#define MX_ID 0xC2 +#define MX_29F002 0xB0 + +#define SST_ID 0xBF /* SST Manufacturer ID[B code */ +#define SST_29EE020A 0x10 /* SST 29EE020 device code */ +#define SST_28SF040 0x04 /* SST 29EE040 device code */ +#define SST_39SF020 0xB6 /* SST 39SF020 device */ +#define SST_39VF020 0xD6 /* SST 39SF020 device */ + +#define WINBOND_ID 0xDA /* Winbond Manufacture ID code */ +#define W_29C011 0xC1 /* Winbond w29c011 device code*/ +#define W_29C020C 0x45 /* Winbond w29c020c device code*/ +#define W_49F002U 0x0B /* Winbond w29c020c device code*/ + +#define ST_ID 0x20 +#define ST_M29F400BT 0xD5 + +#define MSYSTEMS_ID 0x156f +#define MSYSTEMS_MD2200 0xdb /* ? */ +#define MSYSTEMS_MD2800 0x30 /* hmm -- both 0x30 */ +#define MSYSTEMS_MD2802 0x30 /* hmm -- both 0x30 */ + +extern void myusec_delay(int time); + +#endif /* !__FLASH_H__ */ diff --git a/util/flash_and_burn/flash_on.c b/util/flash_and_burn/flash_on.c new file mode 100644 index 0000000000..a43f1da8c2 --- /dev/null +++ b/util/flash_and_burn/flash_on.c @@ -0,0 +1,77 @@ +/* + * flash_rom.c: Turnning on Flash Write Enable for SiS 630/950 M/Bs, + * use this program before loading DoC drivers. + * + * + * Copyright 2000 Silicon Integrated System Corporation + * + * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * Reference: + * 1. SiS 630 Specification + * 2. SiS 950 Specification + * + * $Id$ + */ + +#include +#include +#include +#include +#include +#include +#include + +int main() +{ + char b; + + /* get io privilege access PCI configuration space */ + if (iopl(3) != 0) { + perror("Can not set io priviliage"); + exit(1); + } + + /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630 */ + outl(0x80000840, 0x0cf8); + b = inb(0x0cfc) | 0x0b; + outb(b, 0xcfc); + /* Flash write enable on SiS 540/630 */ + outl(0x80000845, 0x0cf8); + b = inb(0x0cfd) | 0x40; + outb(b, 0xcfd); + + /* The same thing on SiS 950 SuperIO side */ + outb(0x87, 0x2e); + outb(0x01, 0x2e); + outb(0x55, 0x2e); + outb(0x55, 0x2e); + + if (inb(0x2f) != 0x87) { + printf("Can not access SiS 950\n"); + return -1; + } + + outb(0x24, 0x2e); + b = inb(0x2f) | 0xfc; + outb(0x24, 0x2e); + outb(b, 0x2f); + + outb(0x02, 0x2e); + outb(0x02, 0x2f); + + return(0); +} diff --git a/util/flash_and_burn/flash_rom.c b/util/flash_and_burn/flash_rom.c new file mode 100644 index 0000000000..a13d48bb51 --- /dev/null +++ b/util/flash_and_burn/flash_rom.c @@ -0,0 +1,543 @@ +/* + * flash_rom.c: Flash programming utility for SiS 630/950 M/Bs + * + * + * Copyright 2000 Silicon Integrated System Corporation + * + * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * Reference: + * 1. SiS 630 Specification + * 2. SiS 950 Specification + * + * $Id$ + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "flash.h" +#include "jedec.h" +#include "m29f400bt.h" +#include "82802ab.h" +#include "msys_doc.h" +#include "am29f040b.h" +#include "sst28sf040.h" +#include "w49f002u.h" +#include "sst39sf020.h" +#include "mx29f002.h" + +struct flashchip flashchips[] = { + {"Am29F040B", AMD_ID, AM_29F040B, NULL, 512, 64*1024, + probe_29f040b, erase_29f040b, write_29f040b, NULL}, + {"At29C040A", ATMEL_ID, AT_29C040A, NULL, 512, 256, + probe_jedec, erase_jedec, write_jedec, NULL}, + {"Mx29f002", MX_ID, MX_29F002, NULL, 256, 64*1024, + probe_29f002, erase_29f002, write_29f002, NULL}, + {"SST29EE020A", SST_ID, SST_29EE020A, NULL, 256, 128, + probe_jedec, erase_jedec, write_jedec, NULL}, + {"SST28SF040A", SST_ID, SST_28SF040, NULL, 512, 256, + probe_28sf040, erase_28sf040, write_28sf040, NULL}, + {"SST39SF020A", SST_ID, SST_39SF020, NULL, 256, 4096, + probe_39sf020, erase_39sf020, write_39sf020, NULL}, + {"SST39VF020", SST_ID, SST_39VF020, NULL, 256, 4096, + probe_39sf020, erase_39sf020, write_39sf020, NULL}, + {"W29C011", WINBOND_ID, W_29C011, NULL, 128, 128, + probe_jedec, erase_jedec, write_jedec, NULL}, + {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128, + probe_jedec, erase_jedec, write_jedec, NULL}, + {"W49F002U", WINBOND_ID, W_49F002U, NULL, 256, 128, + probe_49f002, erase_49f002, write_49f002, NULL}, + {"M29F400BT", ST_ID, ST_M29F400BT , NULL, 512, 64*1024, + probe_m29f400bt, erase_m29f400bt, write_linuxbios_m29f400bt, NULL}, + {"82802ab", 137, 173 , NULL, 512, 64*1024, + probe_82802ab, erase_82802ab, write_82802ab, NULL}, + {"82802ac", 137, 172 , NULL, 1024, 64*1024, + probe_82802ab, erase_82802ab, write_82802ab, NULL}, + {"MD-2802 (M-Systems DiskOnChip Millennium Module)", + MSYSTEMS_ID, MSYSTEMS_MD2802, + NULL, 8, 8*1024, + probe_md2802, erase_md2802, write_md2802, read_md2802}, + {NULL,} +}; + +char *chip_to_probe = NULL; + +int enable_flash_sis630 (struct pci_dev *dev, char *name) +{ + char b; + + /* get io privilege access PCI configuration space */ + if (iopl(3) != 0) { + perror("Can not set io priviliage"); + exit(1); + } + + /* Enable 0xFFF8000~0xFFFF0000 decoding on SiS 540/630 */ + outl(0x80000840, 0x0cf8); + b = inb(0x0cfc) | 0x0b; + outb(b, 0xcfc); + /* Flash write enable on SiS 540/630 */ + outl(0x80000845, 0x0cf8); + b = inb(0x0cfd) | 0x40; + outb(b, 0xcfd); + + /* The same thing on SiS 950 SuperIO side */ + outb(0x87, 0x2e); + outb(0x01, 0x2e); + outb(0x55, 0x2e); + outb(0x55, 0x2e); + + if (inb(0x2f) != 0x87) { + outb(0x87, 0x4e); + outb(0x01, 0x4e); + outb(0x55, 0x4e); + outb(0xaa, 0x4e); + if (inb(0x4f) != 0x87) { + printf("Can not access SiS 950\n"); + return -1; + } + outb(0x24, 0x4e); + b = inb(0x4f) | 0xfc; + outb(0x24, 0x4e); + outb(b, 0x4f); + outb(0x02, 0x4e); + outb(0x02, 0x4f); + } + + outb(0x24, 0x2e); + printf("2f is %#x\n", inb(0x2f)); + b = inb(0x2f) | 0xfc; + outb(0x24, 0x2e); + outb(b, 0x2f); + + outb(0x02, 0x2e); + outb(0x02, 0x2f); + + return 0; +} + +int +enable_flash_e7500(struct pci_dev *dev, char *name) { + /* register 4e.b gets or'ed with one */ + unsigned char old, new; + /* if it fails, it fails. There are so many variations of broken mobos + * that it is hard to argue that we should quit at this point. + */ + + old = pci_read_byte(dev, 0x4e); + + new = old | 1; + + if (new == old) + return 0; + + pci_write_byte(dev, 0x4e, new); + + if (pci_read_byte(dev, 0x4e) != new) { + printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", + 0x4e, new, name); + return -1; + } + return 0; +} + +int +enable_flash_vt8235(struct pci_dev *dev, char *name) { + unsigned char old, new, val; + unsigned int base; + int ok; + + /* get io privilege access PCI configuration space */ + if (iopl(3) != 0) { + perror("Can not set io priviliage"); + exit(1); + } + + old = pci_read_byte(dev, 0x40); + + new = old | 0x10; + + if (new == old) + return 0; + + ok = pci_write_byte(dev, 0x40, new); + if (ok != 0) { + printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", + old, new, name); + } + + /* enable GPIO15 which is connected to write protect. */ + base = ((pci_read_byte(dev, 0x88) & 0x80) | pci_read_byte(dev, 0x89) << 8); + val = inb(base + 0x4d); + val |= 0x80; + outb(val, base + 0x4d); + + if (ok != 0) { + return -1; + } else { + return 0; + } +} + +int +enable_flash_vt8231(struct pci_dev *dev, char *name) { + unsigned char val; + + val = pci_read_byte(dev, 0x40); + val |= 0x10; + pci_write_byte(dev, 0x40, val); + + if (pci_read_byte(dev, 0x40) != val) { + printf("tried to set 0x%x to 0x%x on %s failed (WARNING ONLY)\n", + 0x40, val, name); + return -1; + } + return 0; +} + +int +enable_flash_cs5530(struct pci_dev *dev, char *name) { + unsigned char new; + + pci_write_byte(dev, 0x52, 0xee); + + new = pci_read_byte(dev, 0x52); + + if (new != 0xee) { + printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", + 0x52, new, name); + return -1; + } + return 0; +} + +int +enable_flash_sc1100(struct pci_dev *dev, char *name) { + unsigned char new; + + pci_write_byte(dev, 0x52, 0xee); + + new = pci_read_byte(dev, 0x52); + + if (new != 0xee) { + printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", + 0x52, new, name); + return -1; + } + return 0; +} + +int +enable_flash_sis5595(struct pci_dev *dev, char *name) { + unsigned char new, newer; + + new = pci_read_byte(dev, 0x45); + + /* clear bit 5 */ + new &= (~ 0x20); + /* set bit 2 */ + new |= 0x4; + + pci_write_byte(dev, 0x45, new); + + newer = pci_read_byte(dev, 0x45); + if (newer != new) { + printf("tried to set register 0x%x to 0x%x on %s failed (WARNING ONLY)\n", + 0x45, new, name); + printf("Stuck at 0x%x\n", newer); + return -1; + } + return 0; +} + +struct flashchip * probe_flash(struct flashchip * flash) +{ + int fd_mem; + volatile char * bios; + unsigned long size; + + if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) { + perror("Can not open /dev/mem"); + exit(1); + } + + while (flash->name != NULL) { + if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) { + flash++; + continue; + } + printf("Trying %s, %d KB\n", flash->name, flash->total_size); + size = flash->total_size * 1024; +/* BUG? what happens if getpagesize() > size!? + -> ``Error MMAP /dev/mem: Invalid argument'' NIKI */ + if(getpagesize() > size) + { + size = getpagesize(); + printf("%s: warning: size: %d -> %ld\n", __FUNCTION__, + flash->total_size * 1024, (unsigned long)size); + } + bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED, + fd_mem, (off_t) (0 - size)); + if (bios == MAP_FAILED) { + perror("Error MMAP /dev/mem"); + exit(1); + } + flash->virt_addr = bios; + flash->fd_mem = fd_mem; + + if (flash->probe(flash) == 1) { + printf ("%s found at physical address: 0x%lx\n", + flash->name, (0 - size)); + return flash; + } + munmap ((void *) bios, size); + flash++; + } + return NULL; +} + +int verify_flash (struct flashchip * flash, char * buf, int verbose) +{ + int i = 0; + int total_size = flash->total_size *1024; + volatile char * bios = flash->virt_addr; + + printf("Verifying address: "); + while (i++ < total_size) { + if (verbose) + printf("0x%08x", i); + if (*(bios+i) != *(buf+i)) { + printf("FAILED\n"); + return 0; + } + if (verbose) + printf("\b\b\b\b\b\b\b\b\b\b"); + } + if (verbose) + printf("\n"); + else + printf("VERIFIED\n"); + return 1; +} + +// count to a billion. Time it. If it's < 1 sec, count to 10B, etc. + +unsigned long micro = 1; + +void +myusec_calibrate_delay() +{ + int count = 1000; + unsigned long timeusec; + struct timeval start, end; + int ok = 0; + void myusec_delay(int time); + + printf("Setting up microsecond timing loop\n"); + while (! ok) { + //fprintf(stderr, "Try %d\n", count); + gettimeofday(&start, 0); + myusec_delay(count); + gettimeofday(&end, 0); + timeusec = 1000000 * (end.tv_sec - start.tv_sec ) + + (end.tv_usec - start.tv_usec); + //fprintf(stderr, "timeusec is %d\n", timeusec); + count *= 2; + if (timeusec < 1000000/4) + continue; + ok = 1; + } + + // compute one microsecond. That will be count / time + micro = count / timeusec; + + fprintf(stderr, "%ldM loops per second\n", (unsigned long)micro); + + +} + +void +myusec_delay(int time) +{ + volatile unsigned long i; + for(i = 0; i < time * micro; i++) + ; +} + +typedef struct penable { + unsigned short vendor, device; + char *name; + int (*doit)(struct pci_dev *dev, char *name); +} FLASH_ENABLE; + +FLASH_ENABLE enables[] = { + + {0x1, 0x1, "sis630 -- what's the ID?", enable_flash_sis630}, + {0x8086, 0x2480, "E7500", enable_flash_e7500}, + {0x1106, 0x8231, "VT8231", enable_flash_vt8231}, + {0x1106, 0x3177, "VT8235", enable_flash_vt8235}, + {0x1078, 0x0100, "CS5530", enable_flash_cs5530}, + {0x100b, 0x0510, "SC1100", enable_flash_sc1100}, + {0x1039, 0x8, "SIS5595", enable_flash_sis5595}, +}; + +int +enable_flash_write() { + int i; + struct pci_access *pacc; + struct pci_dev *dev = 0; + FLASH_ENABLE *enable = 0; + + pacc = pci_alloc(); /* Get the pci_access structure */ + /* Set all options you want -- here we stick with the defaults */ + pci_init(pacc); /* Initialize the PCI library */ + pci_scan_bus(pacc); /* We want to get the list of devices */ + + /* now let's try to find the chipset we have ... */ + for(i = 0; i < sizeof(enables)/sizeof(enables[0]) && (! dev); i++) { + struct pci_filter f; + struct pci_dev *z; + /* the first param is unused. */ + pci_filter_init((struct pci_access *) 0, &f); + f.vendor = enables[i].vendor; + f.device = enables[i].device; + for(z=pacc->devices; z; z=z->next) + if (pci_filter_match(&f, z)) { + enable = &enables[i]; + dev = z; + } + } + + /* now do the deed. */ + if (enable) { + printf("Enabling flash write on %s...", enable->name); + if (enable->doit(dev, enable->name) == 0) + printf("OK\n"); + } + return 0; +} + +void usage(const char *name) +{ + printf("usage: %s [-rwv] [-c chipname][file]\n", name); + printf("-r: read flash and save into file\n" + "-w: write file into flash (default when file is specified)\n" + "-v: verify flash against file\n" + "-c: probe only for specified flash chip\n" + " If no file is specified, then all that happens\n" + " is that flash info is dumped\n"); + exit(1); +} + +int +main (int argc, char * argv[]) +{ + char * buf; + unsigned long size; + FILE * image; + struct flashchip * flash; + int opt; + int read_it = 0, write_it = 0, verify_it = 0; + char *filename = NULL; + + setbuf(stdout, NULL); + + while ((opt = getopt(argc, argv, "rwvc:")) != EOF) { + switch (opt) { + case 'r': + read_it = 1; + break; + case 'w': + write_it = 1; + break; + case 'v': + verify_it = 1; + break; + case 'c': + chip_to_probe = strdup(optarg); + break; + default: + usage(argv[0]); + break; + } + } + if (read_it && write_it) { + printf("-r and -w are mutually exclusive\n"); + usage(argv[0]); + } + + if (optind < argc) + filename = argv[optind++]; + + printf("Calibrating timer since microsleep sucks ... takes a second\n"); + myusec_calibrate_delay(); + printf("OK, calibrated, now do the deed\n"); + + /* try to enable it. Failure IS an option, since not all motherboards + * really need this to be done, etc., etc. It sucks. + */ + (void) enable_flash_write(); + + if ((flash = probe_flash (flashchips)) == NULL) { + printf("EEPROM not found\n"); + exit(1); + } + + printf("Part is %s\n", flash->name); + if (!filename){ + printf("OK, only ENABLING flash write, but NOT FLASHING\n"); + return 0; + } + size = flash->total_size * 1024; + buf = (char *) calloc (size, sizeof(char)); + + if (read_it) { + if ((image = fopen (filename, "w")) == NULL) { + perror(filename); + exit(1); + } + printf("Reading Flash..."); + if(flash->read == NULL) + memcpy(buf, (const char *) flash->virt_addr, size); + else + flash->read (flash, buf); + fwrite(buf, sizeof(char), size, image); + fclose(image); + printf("done\n"); + } else { + if ((image = fopen (filename, "r")) == NULL) { + perror(filename); + exit(1); + } + fread (buf, sizeof(char), size, image); + fclose(image); + } + + if (write_it || (!read_it && !verify_it)) + flash->write (flash, buf); + if (verify_it) + verify_flash (flash, buf, /* verbose = */ 0); + return 0; +} diff --git a/util/flash_and_burn/jedec.c b/util/flash_and_burn/jedec.c new file mode 100644 index 0000000000..bb4a874ab1 --- /dev/null +++ b/util/flash_and_burn/jedec.c @@ -0,0 +1,98 @@ +/* + * jedec.c: driver for programming JEDEC standard flash parts + * + * + * Copyright 2000 Silicon Integrated System Corporation + * + * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * Reference: + * + * $Id$ + */ + +#include +#include "flash.h" +#include "jedec.h" + +int probe_jedec (struct flashchip * flash) +{ + volatile char * bios = flash->virt_addr; + unsigned char id1, id2; + + *(volatile char *) (bios + 0x5555) = 0xAA; + *(volatile char *) (bios + 0x2AAA) = 0x55; + *(volatile char *) (bios + 0x5555) = 0x90; + + myusec_delay(10); + + id1 = *(volatile unsigned char *) bios; + id2 = *(volatile unsigned char *) (bios + 0x01); + + *(volatile char *) (bios + 0x5555) = 0xAA; + *(volatile char *) (bios + 0x2AAA) = 0x55; + *(volatile char *) (bios + 0x5555) = 0xF0; + + myusec_delay(10); + + printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2); + if (id1 == flash->manufacture_id && id2 == flash->model_id) + return 1; + + return 0; +} + +int erase_jedec (struct flashchip * flash) +{ + volatile char * bios = flash->virt_addr; + + *(volatile char *) (bios + 0x5555) = 0xAA; + *(volatile char *) (bios + 0x2AAA) = 0x55; + *(volatile char *) (bios + 0x5555) = 0x80; + + *(volatile char *) (bios + 0x5555) = 0xAA; + *(volatile char *) (bios + 0x2AAA) = 0x55; + *(volatile char *) (bios + 0x5555) = 0x10; + + myusec_delay(10); + toggle_ready_jedec(bios); + + return(0); +} + +int write_jedec (struct flashchip * flash, unsigned char * buf) +{ + int i; + int total_size = flash->total_size *1024, page_size = flash->page_size; + volatile unsigned char * bios = flash->virt_addr; + + erase_jedec (flash); + if (*bios != (unsigned char ) 0xff) { + printf("ERASE FAILED\n"); + return -1; + } + printf ("Programming Page: "); + for (i = 0; i < total_size/page_size; i++) { + printf ("%04d at address: 0x%08x", i, i * page_size); + write_page_jedec(bios, buf + i * page_size, bios + i * page_size, + page_size); + printf ("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); + } + printf("\n"); + protect_jedec (bios); + + return(0); +} diff --git a/util/flash_and_burn/jedec.h b/util/flash_and_burn/jedec.h new file mode 100644 index 0000000000..40261fd4fd --- /dev/null +++ b/util/flash_and_burn/jedec.h @@ -0,0 +1,66 @@ +#ifndef __JEDEC_H__ +#define __JEDEC_H__ 1 + +extern int probe_jedec (struct flashchip * flash); +extern int erase_jedec (struct flashchip * flash); +extern int write_jedec (struct flashchip * flash, unsigned char * buf); + +extern __inline__ void toggle_ready_jedec (volatile char * dst) +{ + unsigned int i = 0; + char tmp1, tmp2; + + tmp1 = *dst & 0x40; + + while (i++ < 0xFFFFFF) { + tmp2 = *dst & 0x40; + if (tmp1 == tmp2) { + break; + } + tmp1 = tmp2; + } +} + +extern __inline__ void data_polling_jedec (volatile char * dst, char data) +{ + unsigned int i = 0; + char tmp; + + data &= 0x80; + + while (i++ < 0xFFFFFF) { + tmp = *dst & 0x80; + if (tmp == data) { + break; + } + } +} + +extern __inline__ void protect_jedec (volatile char * bios) +{ + *(volatile char *) (bios + 0x5555) = 0xAA; + *(volatile char *) (bios + 0x2AAA) = 0x55; + *(volatile char *) (bios + 0x5555) = 0xA0; + + usleep(200); +} + +extern __inline__ void write_page_jedec (volatile char * bios, char * src, volatile char * dst, + int page_size) +{ + int i; + + *(volatile char *) (bios + 0x5555) = 0xAA; + *(volatile char *) (bios + 0x2AAA) = 0x55; + *(volatile char *) (bios + 0x5555) = 0xA0; + + for (i = 0; i < page_size; i++) { + /* transfer data from source to destination */ + *dst++ = *src++; + } + + usleep(100); + toggle_ready_jedec(dst-1); +} + +#endif /* !__JEDEC_H__ */ diff --git a/util/flash_and_burn/m29f400bt.c b/util/flash_and_burn/m29f400bt.c new file mode 100644 index 0000000000..a19c8cd6ca --- /dev/null +++ b/util/flash_and_burn/m29f400bt.c @@ -0,0 +1,189 @@ +/* + * m29f400bt.c: driver for programming JEDEC standard flash parts + * + * + * Copyright 2000 Silicon Integrated System Corporation + * + * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * Reference: + * + * $Id$ + */ + +#include "flash.h" +#include "m29f400bt.h" + +int probe_m29f400bt (struct flashchip * flash) +{ + volatile char * bios = flash->virt_addr; + unsigned char id1, id2; + + *(volatile char *) (bios + 0xAAA) = 0xAA; + *(volatile char *) (bios + 0x555) = 0x55; + *(volatile char *) (bios + 0xAAA) = 0x90; + + myusec_delay(10); + + id1 = *(volatile unsigned char *) bios; + id2 = *(volatile unsigned char *) (bios + 0x02); + + *(volatile char *) (bios + 0xAAA) = 0xAA; + *(volatile char *) (bios + 0x555) = 0x55; + *(volatile char *) (bios + 0xAAA) = 0xF0; + + myusec_delay(10); + + printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2); + + + if (id1 == flash->manufacture_id && id2 == flash->model_id) + return 1; + + return 0; +} + +int erase_m29f400bt (struct flashchip * flash) +{ + volatile char * bios = flash->virt_addr; + + *(volatile char *) (bios + 0xAAA) = 0xAA; + *(volatile char *) (bios + 0x555) = 0x55; + *(volatile char *) (bios + 0xAAA) = 0x80; + + *(volatile char *) (bios + 0xAAA) = 0xAA; + *(volatile char *) (bios + 0x555) = 0x55; + *(volatile char *) (bios + 0xAAA) = 0x10; + + myusec_delay(10); + toggle_ready_m29f400bt(bios); + + return(0); +} + +int block_erase_m29f400bt (volatile char * bios ,volatile char * dst) +{ + + *(volatile char *) (bios + 0xAAA) = 0xAA; + *(volatile char *) (bios + 0x555) = 0x55; + *(volatile char *) (bios + 0xAAA) = 0x80; + + *(volatile char *) (bios + 0xAAA) = 0xAA; + *(volatile char *) (bios + 0x555) = 0x55; + //*(volatile char *) (bios + 0xAAA) = 0x10; + *dst = 0x30; + + myusec_delay(10); + toggle_ready_m29f400bt(bios); + + return(0); +} + +int write_m29f400bt (struct flashchip * flash, unsigned char * buf) +{ + int i; + int total_size = flash->total_size *1024, page_size = flash->page_size; + volatile char * bios = flash->virt_addr; + + //erase_m29f400bt (flash); + printf ("Programming Page:\n "); + /********************************* + *Pages for M29F400BT: + * 16 0x7c000 0x7ffff TOP + * 8 0x7a000 0x7bfff + * 8 0x78000 0x79fff + * 32 0x70000 0x77fff + * 64 0x60000 0x6ffff + * 64 0x50000 0x5ffff + * 64 0x40000 0x4ffff + *--------------------------------- + * 64 0x30000 0x3ffff + * 64 0x20000 0x2ffff + * 64 0x10000 0x1ffff + * 64 0x00000 0x0ffff BOTTOM + *********************************/ + printf("total_size/page_size = %d\n",total_size/page_size); + for (i = 0; i < (total_size/page_size)-1; i++) { + printf("%04d at address: 0x%08x\n", i, i * page_size); + block_erase_m29f400bt(bios, bios + i * page_size); + write_page_m29f400bt(bios, buf + i * page_size, bios + i * page_size, + page_size); + printf ("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); + } + + printf("%04d at address: 0x%08x\n",7,0x70000); + block_erase_m29f400bt(bios , bios + 0x70000); + write_page_m29f400bt(bios,buf + 0x70000, bios + 0x70000, 32*1024); + + printf("%04d at address: 0x%08x\n",8,0x78000); + block_erase_m29f400bt(bios , bios + 0x78000); + write_page_m29f400bt(bios,buf + 0x78000, bios + 0x78000, 8*1024); + + printf("%04d at address: 0x%08x\n",9,0x7a000); + block_erase_m29f400bt(bios , bios + 0x7a000); + write_page_m29f400bt(bios,buf + 0x7a000, bios + 0x7a000, 8*1024); + + printf("%04d at address: 0x%08x\n",10,0x7c000); + block_erase_m29f400bt(bios , bios + 0x7c000); + write_page_m29f400bt(bios,buf + 0x7c000, bios + 0x7c000, 16*1024); + + printf("\n"); + //protect_m29f400bt (bios); + + return(0); +} + +int write_linuxbios_m29f400bt (struct flashchip * flash, unsigned char * buf) +{ + volatile char * bios = flash->virt_addr; + + printf ("Programming Page:\n "); + /********************************* + *Pages for M29F400BT: + * 16 0x7c000 0x7ffff TOP + * 8 0x7a000 0x7bfff + * 8 0x78000 0x79fff + * 32 0x70000 0x77fff + * 64 0x60000 0x6ffff + * 64 0x50000 0x5ffff + * 64 0x40000 0x4ffff + *--------------------------------- + * 64 0x30000 0x3ffff + * 64 0x20000 0x2ffff + * 64 0x10000 0x1ffff + * 64 0x00000 0x0ffff BOTTOM + *********************************/ + printf("%04d at address: 0x%08x\n",7,0x00000); + block_erase_m29f400bt(bios , bios + 0x00000); + write_page_m29f400bt(bios,buf + 0x00000, bios + 0x00000, 64*1024); + + printf("%04d at address: 0x%08x\n",7,0x10000); + block_erase_m29f400bt(bios , bios + 0x10000); + write_page_m29f400bt(bios,buf + 0x10000, bios + 0x10000, 64*1024); + + printf("%04d at address: 0x%08x\n",7,0x20000); + block_erase_m29f400bt(bios , bios + 0x20000); + write_page_m29f400bt(bios,buf + 0x20000, bios + 0x20000, 64*1024); + + printf("%04d at address: 0x%08x\n",7,0x30000); + block_erase_m29f400bt(bios , bios + 0x30000); + write_page_m29f400bt(bios,buf + 0x30000, bios + 0x30000, 64*1024); + + printf("\n"); + //protect_m29f400bt (bios); + + return(0); +} diff --git a/util/flash_and_burn/m29f400bt.h b/util/flash_and_burn/m29f400bt.h new file mode 100644 index 0000000000..3d1799f4e2 --- /dev/null +++ b/util/flash_and_burn/m29f400bt.h @@ -0,0 +1,74 @@ +#ifndef __M29F400BT_H__ +#define __M29F400BT_H__ 1 + +#include + +extern int probe_m29f400bt (struct flashchip * flash); +extern int erase_m29f400bt (struct flashchip * flash); +extern int block_erase_m29f400bt (volatile char * bios, volatile char * dst); +extern int write_m29f400bt (struct flashchip * flash, unsigned char * buf); +extern int write_linuxbios_m29f400bt (struct flashchip * flash, unsigned char * buf); + +extern __inline__ void toggle_ready_m29f400bt (volatile char * dst) +{ + unsigned int i = 0; + char tmp1, tmp2; + + tmp1 = *dst & 0x40; + + while (i++ < 0xFFFFFF) { + tmp2 = *dst & 0x40; + if (tmp1 == tmp2) { + break; + } + tmp1 = tmp2; + } +} + +extern __inline__ void data_polling_m29f400bt (volatile char * dst, unsigned char data) +{ + unsigned int i = 0; + char tmp; + + data &= 0x80; + + while (i++ < 0xFFFFFF) { + tmp = *dst & 0x80; + if (tmp == data) { + break; + } + } +} + +extern __inline__ void protect_m29f400bt (volatile char * bios) +{ + *(volatile char *) (bios + 0xAAA) = 0xAA; + *(volatile char *) (bios + 0x555) = 0x55; + *(volatile char *) (bios + 0xAAA) = 0xA0; + + usleep(200); +} + +extern __inline__ void write_page_m29f400bt (volatile char * bios, char * src, volatile char * dst, + int page_size) +{ + int i; + + for (i = 0; i < page_size; i++) { + *(volatile char *) (bios + 0xAAA) = 0xAA; + *(volatile char *) (bios + 0x555) = 0x55; + *(volatile char *) (bios + 0xAAA) = 0xA0; + + /* transfer data from source to destination */ + *dst = *src; + //*(volatile char *) (bios) = 0xF0; + //usleep(5); + toggle_ready_m29f400bt(dst); + printf("Value in the flash at address %p = %#x, want %#x\n",(char *)(dst-bios), *dst, *src); + dst++; + src++; + } + +} + +#endif /* !__M29F400BT_H__ */ diff --git a/util/flash_and_burn/msys_doc.c b/util/flash_and_burn/msys_doc.c new file mode 100644 index 0000000000..bfa4ecae9b --- /dev/null +++ b/util/flash_and_burn/msys_doc.c @@ -0,0 +1,289 @@ +/* + * msys_doc.c: driver for programming m-systems doc devices + * + * + * Copyright 2003 Niki W. Waibel + * + * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + + +#include +#include +#include "flash.h" +#include "msys_doc.h" + + + + + +static int doc_wait(volatile char *bios, int timeout); +static unsigned char doc_read_chipid(volatile char *bios); +static unsigned char doc_read_docstatus(volatile char *bios); +static unsigned char doc_read_cdsncontrol(volatile char *bios); +static void doc_write_cdsncontrol(volatile char *bios, unsigned char data); + + + + + +int +probe_md2802(struct flashchip * flash) +{ + volatile char *bios = flash->virt_addr; + unsigned char chipid; +#ifndef MSYSTEMS_DOC_NO_55AA_CHECKING + unsigned char id_0x55, id_0xAA; +#endif /* !MSYSTEMS_DOC_NO_55AA_CHECKING */ + int i, toggle_a, toggle_b; + + printf("%s:\n", __FUNCTION__); + printf("%s: *******************************\n", __FUNCTION__); + printf("%s: * THIS IS A PRE ALPHA VERSION *\n", __FUNCTION__); + printf("%s: * IN THE DEVELOPEMENT *********\n", __FUNCTION__); + printf("%s: * PROCESS RIGHT NOW. **********\n", __FUNCTION__); + printf("%s: *******************************\n", __FUNCTION__); + printf("%s: * IF YOU ARE NOT A DEVELOPER **\n", __FUNCTION__); + printf("%s: * THEN DO NOT TRY TO READ OR **\n", __FUNCTION__); + printf("%s: * WRITE TO THIS DEVICE ********\n", __FUNCTION__); + printf("%s: *******************************\n", __FUNCTION__); + printf("%s:\n", __FUNCTION__); + + printf("%s: switching off reset mode ...\n", __FUNCTION__); + doc_write(0x85, bios, DOCControl); + doc_write(0x85, bios, DOCControl); + doc_read_4nop(bios); + if(doc_wait(bios, 5000)) + return(-1); + printf("%s: switching off reset mode ... done\n", __FUNCTION__); + printf("%s:\n", __FUNCTION__); + + printf("%s: switching off write protection ...\n", __FUNCTION__); + doc_write_cdsncontrol(bios, doc_read_cdsncontrol(bios)&(~0x08)); + printf("%s: switching off write protection ... done\n", __FUNCTION__); + printf("%s:\n", __FUNCTION__); + + + chipid = doc_read_chipid(bios); +#ifndef MSYSTEMS_DOC_NO_55AA_CHECKING + id_0x55 = doc_read(bios, IPL_0x0000); + id_0xAA = doc_read(bios, IPL_0x0001); +#endif /* !MSYSTEMS_DOC_NO_55AA_CHECKING */ + printf("%s: IPL_0x0000: 0x%02x\n", __FUNCTION__, id_0x55); + printf("%s: IPL_0x0001: 0x%02x\n", __FUNCTION__, id_0xAA); + printf("%s: IPL_0x0002: 0x%02x\n", __FUNCTION__, + doc_read(bios, IPL_0x0002)); + printf("%s: IPL_0x0003: 0x%02x\n", __FUNCTION__, + doc_read(bios, IPL_0x0003)); + printf("%s:\n", __FUNCTION__); + printf("%s: ChipID: 0x%02x\n", __FUNCTION__, chipid); + printf("%s: DOCStatus: 0x%02x\n", __FUNCTION__, + doc_read_docstatus(bios)); + printf("%s: FloorSelect: 0x%02x\n", __FUNCTION__, + doc_read(bios, FloorSelect)); + printf("%s: CDSNControl: 0x%02x\n", __FUNCTION__, + doc_read_cdsncontrol(bios)); + printf("%s: CDSNDeviceSelect: 0x%02x\n", __FUNCTION__, + doc_read(bios, CDSNDeviceSelect)); + printf("%s: ECCConfiguration: 0x%02x\n", __FUNCTION__, + doc_read(bios, ECCConfiguration)); + printf("%s: CDSNSlowIO: 0x%02x\n", __FUNCTION__, + doc_read(bios, CDSNSlowIO)); + printf("%s: ECCSyndrome0: 0x%02x\n", __FUNCTION__, + doc_read(bios, ECCSyndrome0)); + printf("%s: ECCSyndrome1: 0x%02x\n", __FUNCTION__, + doc_read(bios, ECCSyndrome1)); + printf("%s: ECCSyndrome2: 0x%02x\n", __FUNCTION__, + doc_read(bios, ECCSyndrome2)); + printf("%s: ECCSyndrome3: 0x%02x\n", __FUNCTION__, + doc_read(bios, ECCSyndrome3)); + printf("%s: ECCSyndrome4: 0x%02x\n", __FUNCTION__, + doc_read(bios, ECCSyndrome4)); + printf("%s: ECCSyndrome5: 0x%02x\n", __FUNCTION__, + doc_read(bios, ECCSyndrome5)); + printf("%s: AliasResolution: 0x%02x\n", __FUNCTION__, + doc_read(bios, AliasResolution)); + printf("%s: ConfigurationInput: 0x%02x\n", __FUNCTION__, + doc_read(bios, ConfigurationInput)); + printf("%s: ReadPipelineInitialization: 0x%02x\n", __FUNCTION__, + doc_read(bios, ReadPipelineInitialization)); + printf("%s: LastDataRead: 0x%02x\n", __FUNCTION__, + doc_read(bios, LastDataRead)); + printf("%s:\n", __FUNCTION__); + + printf("%s: checking ECCConfiguration toggle bit\n", __FUNCTION__); + printf("%s:", __FUNCTION__); + toggle_a = toggle_b = 0; + for(i=0; i<10; i++) + { + unsigned char toggle = doc_toggle(bios); + + printf(" 0x%02x", toggle); + + if(i%2) + toggle_a += toggle; + else + toggle_b += toggle; + } /* for(i=0; i<10; i++) */ + printf("\n%s: toggle result: %d/%d\n", __FUNCTION__, toggle_a, toggle_b); + + if(chipid == flash->model_id + && ((toggle_a==5 && toggle_b==0)||(toggle_a==0 && toggle_b==5)) +#ifndef MSYSTEMS_DOC_NO_55AA_CHECKING + && id_0x55 == 0x55 && id_0xAA == 0xaa +#endif /* !MSYSTEMS_DOC_NO_55AA_CHECKING */ + ) + { + return(1); + } + + return(0); +} /* int probe_md2802(struct flashchip *flash) */ + + + +int +read_md2802(struct flashchip *flash, unsigned char *buf) +{ + + return(0); +} /* int read_md2802(struct flashchip *flash, unsigned char *buf) */ + + + +int +erase_md2802(struct flashchip *flash) +{ + volatile char *bios = flash->virt_addr; + + return(1); + *(volatile char *)(bios + 0x5555) = 0xAA; + *(volatile char *)(bios + 0x2AAA) = 0x55; + *(volatile char *)(bios + 0x5555) = 0x80; + + *(volatile char *)(bios + 0x5555) = 0xAA; + *(volatile char *)(bios + 0x2AAA) = 0x55; + *(volatile char *)(bios + 0x5555) = 0x10; +} /* int erase_md2802(struct flashchip *flash) */ + + + +int +write_md2802(struct flashchip *flash, unsigned char *buf) +{ + int i; + int total_size = flash->total_size *1024, page_size = flash->page_size; + volatile unsigned char * bios = flash->virt_addr; + + return(1); + erase_md2802 (flash); + if (*bios != (unsigned char ) 0xff) { + printf("ERASE FAILED\n"); + return -1; + } + printf ("Programming Page: "); + for (i = 0; i < total_size/page_size; i++) { + printf ("%04d at address: 0x%08x", i, i * page_size); + //write_page_md2802(bios, buf + i * page_size, bios + i * page_size, page_size); + printf ("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); + } + printf("\n"); + //protect_md2802(bios); + +} /* int write_md2802(struct flashchip *flash, char *buf) */ + + + + + + +/* + wait timeout msec for doc to become ready + return: + 0: ready + -1: timeout expired +*/ +static int +doc_wait(volatile char *bios, int timeout) +{ + int i = 20; + + doc_read_4nop(bios); + + while(_doc_busy(bios) && (i != 0)) + { + usleep(timeout*1000/20); + i--; + } + + doc_read_2nop(bios); + + if(_doc_busy(bios)) + { + doc_read_2nop(bios); + return(-1); + } + + return(0); +} /* static int doc_wait(volatile char *bios, int timeout) */ + + + +static unsigned char +doc_read_docstatus(volatile char *bios) +{ + doc_read(bios, CDSNSlowIO); + doc_read_2nop(bios); + + return(doc_read(bios, _DOCStatus)); +} /* static unsigned char doc_read_docstatus(volatile char *bios) */ + + + +static unsigned char +doc_read_chipid(volatile char *bios) +{ + doc_read(bios, CDSNSlowIO); + doc_read_2nop(bios); + + return(doc_read(bios, _ChipID)); +} /* static unsigned char doc_read_chipid(volatile char *bios) */ + + + +static unsigned char +doc_read_cdsncontrol(volatile char *bios) +{ + unsigned char value; + + /* the delays might be necessary when reading the busy bit, + but because a read to this reg reads the busy bit + anyway we better do this delays... */ + doc_read_4nop(bios); + value = doc_read(bios, _CDSNControl); + doc_read_2nop(bios); + + return(value); +} /* static unsigned char doc_read_chipid(volatile char *bios) */ + + + +static void +doc_write_cdsncontrol(volatile char *bios, unsigned char data) +{ + doc_write(data, bios, _CDSNControl); + doc_read_4nop(bios); +} /* static void doc_write_chipid(volatile char *bios, unsigned char data) */ diff --git a/util/flash_and_burn/msys_doc.h b/util/flash_and_burn/msys_doc.h new file mode 100644 index 0000000000..9ea523cc4f --- /dev/null +++ b/util/flash_and_burn/msys_doc.h @@ -0,0 +1,110 @@ +/* + * msys_doc.h: header file of msys_doc.c + * + * + * Copyright 2003 Niki W. Waibel + * + * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + + +#ifndef __MSYS_DOC_H__ +#define __MSYS_DOC_H__ 1 + + + +/* idea from include/linux/mtd/doc2000.h */ +/* registers with __ should not be read/written directly */ +#define MSYSTEMS_DOC_R__ChipID 0x1000 +#define MSYSTEMS_DOC_R__DOCStatus 0x1001 +#define MSYSTEMS_DOC_W_DOCControl 0x1002 +#define MSYSTEMS_DOC_R_FloorSelect 0x1003 +#define MSYSTEMS_DOC_W_FloorSelect 0x1003 +#define MSYSTEMS_DOC_R__CDSNControl 0x1004 +#define MSYSTEMS_DOC_W__CDSNControl 0x1004 +#define MSYSTEMS_DOC_R_CDSNDeviceSelect 0x1005 +#define MSYSTEMS_DOC_W_CDSNDeviceSelect 0x1005 +#define MSYSTEMS_DOC_R_ECCConfiguration 0x1006 +#define MSYSTEMS_DOC_W_ECCConfiguration 0x1006 +#define MSYSTEMS_DOC_R_CDSNSlowIO 0x100d +#define MSYSTEMS_DOC_W_CDSNSlowIO 0x100d +#define MSYSTEMS_DOC_R_ECCSyndrome0 0x1010 +#define MSYSTEMS_DOC_R_ECCSyndrome1 0x1011 +#define MSYSTEMS_DOC_R_ECCSyndrome2 0x1012 +#define MSYSTEMS_DOC_R_ECCSyndrome3 0x1013 +#define MSYSTEMS_DOC_R_ECCSyndrome4 0x1014 +#define MSYSTEMS_DOC_R_ECCSyndrome5 0x1015 +#define MSYSTEMS_DOC_R_AliasResolution 0x101b +#define MSYSTEMS_DOC_W_AliasResolution 0x101b +#define MSYSTEMS_DOC_R_ConfigurationInput 0x101c +#define MSYSTEMS_DOC_W_ConfigurationInput 0x101c +#define MSYSTEMS_DOC_R_ReadPipelineInitialization 0x101d +#define MSYSTEMS_DOC_W_WritePipelineTermination 0x101e +#define MSYSTEMS_DOC_R_LastDataRead 0x101f +#define MSYSTEMS_DOC_W_LastDataRead 0x101f +#define MSYSTEMS_DOC_R_NOP 0x1020 +#define MSYSTEMS_DOC_W_NOP 0x1020 + +#define MSYSTEMS_DOC_R_IPL_0x0000 0x0000 +#define MSYSTEMS_DOC_R_IPL_0x0001 0x0001 +#define MSYSTEMS_DOC_R_IPL_0x0002 0x0002 +#define MSYSTEMS_DOC_R_IPL_0x0003 0x0003 + +#define MSYSTEMS_DOC_R_CDSNIO_BASE 0x0800 +#define MSYSTEMS_DOC_W_CDSNIO_BASE 0x0800 + + + +#define doc_read(base,reg) \ + (*(volatile unsigned char *)(base + MSYSTEMS_DOC_R_##reg)) + +#define doc_read_nop(base) \ + doc_read(base, NOP) + +#define doc_read_2nop(base) \ + { doc_read_nop(base); doc_read_nop(base); } + +#define doc_read_4nop(base) \ + { doc_read_2nop(base); doc_read_2nop(base); } + +#define doc_write(data,base,reg) \ + (*(volatile unsigned char *)(base + MSYSTEMS_DOC_W_##reg)) = data + +#define doc_write_nop(base) \ + doc_write(0, base, NOP) + +#define doc_write_2nop(base) \ + { doc_write_nop(base); doc_write_nop(base); } + +#define doc_write_4nop(base) \ + { doc_write_2nop(base); doc_write_2nop(base); } + +#define _doc_busy(base) /* 0: ready; -1: busy */ \ + ( ((doc_read(base, _CDSNControl) & 0x80) >> 7) - 1) + +#define doc_toggle(base) /* 0, 1, 0, 1, 0, 1, ... if a doc is present */ \ + ( (doc_read(base, ECCConfiguration) & 0x04) >> 2 ) + + + +extern int probe_md2802(struct flashchip *flash); +extern int read_md2802(struct flashchip *flash, unsigned char *buf); +extern int erase_md2802(struct flashchip *flash); +extern int write_md2802(struct flashchip *flash, unsigned char *buf); + + + +#endif /* !__MSYS_DOC_H__ */ diff --git a/util/flash_and_burn/mx29f002.c b/util/flash_and_burn/mx29f002.c new file mode 100644 index 0000000000..4c22cd43f3 --- /dev/null +++ b/util/flash_and_burn/mx29f002.c @@ -0,0 +1,119 @@ +/* + * mx29f002.c: driver for MXIC MX29F002 flash models + * + * + * Copyright 2000 Silicon Integrated System Corporation + * + * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * Reference: + * MX29F002/002N data sheet + * + * $Id$ + */ + +#include +#include "flash.h" +#include "jedec.h" +#include "mx29f002.h" + +int probe_29f002 (struct flashchip * flash) +{ + volatile char * bios = flash->virt_addr; + unsigned char id1, id2; + + *(bios + 0x5555) = 0xAA; + *(bios + 0x2AAA) = 0x55; + *(bios + 0x5555) = 0x90; + + id1 = *(volatile unsigned char *) bios; + id2 = *(volatile unsigned char *) (bios + 0x01); + + *bios = 0xF0; + + myusec_delay(10); + + printf("%s: id1 %d, id2 %d\n", __FUNCTION__, id1, id2); + if (id1 == flash->manufacture_id && id2 == flash->model_id) + return 1; + + return 0; +} + +int erase_29f002 (struct flashchip * flash) +{ + volatile char * bios = flash->virt_addr; + + *(bios + 0x555) = 0xF0; + *(bios + 0x555) = 0xAA; + *(bios + 0x2AA) = 0x55; + *(bios + 0x555) = 0x80; + *(bios + 0x555) = 0xAA; + *(bios + 0x2AA) = 0x55; + *(bios + 0x555) = 0x10; + + myusec_delay(100); + toggle_ready_jedec(bios); + + // while ((*bios & 0x40) != 0x40) + //; + +#if 0 + toggle_ready_jedec(bios); + *(bios + 0x0ffff) = 0x30; + *(bios + 0x1ffff) = 0x30; + *(bios + 0x2ffff) = 0x30; + *(bios + 0x37fff) = 0x30; + *(bios + 0x39fff) = 0x30; + *(bios + 0x3bfff) = 0x30; +#endif + + return(0); +} + +int write_29f002 (struct flashchip * flash, unsigned char * buf) +{ + int i; + int total_size = flash->total_size * 1024; + volatile char * bios = flash->virt_addr; + volatile char * dst = bios; + + *bios = 0xF0; + myusec_delay(10); + erase_29f002(flash); + //*bios = 0xF0; +#if 1 + printf ("Programming Page: "); + for (i = 0; i < total_size; i++) { + /* write to the sector */ + if ((i & 0xfff) == 0) + printf ("address: 0x%08lx", (unsigned long)i); + *(bios + 0x5555) = 0xAA; + *(bios + 0x2AAA) = 0x55; + *(bios + 0x5555) = 0xA0; + *dst++ = *buf++; + + /* wait for Toggle bit ready */ + toggle_ready_jedec(dst); + + if ((i & 0xfff) == 0) + printf ("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); + } +#endif + printf("\n"); + + return(0); +} diff --git a/util/flash_and_burn/mx29f002.h b/util/flash_and_burn/mx29f002.h new file mode 100644 index 0000000000..0d252bbe9e --- /dev/null +++ b/util/flash_and_burn/mx29f002.h @@ -0,0 +1,8 @@ +#ifndef __MX29F002_H__ +#define __MX29F002_H__ 1 + +extern int probe_29f002 (struct flashchip * flash); +extern int erase_29f002 (struct flashchip * flash); +extern int write_29f002 (struct flashchip * flash, unsigned char * buf); + +#endif /* !__MX29F002_H__ */ diff --git a/util/flash_and_burn/sst28sf040.c b/util/flash_and_burn/sst28sf040.c new file mode 100644 index 0000000000..ee49f79786 --- /dev/null +++ b/util/flash_and_burn/sst28sf040.c @@ -0,0 +1,169 @@ +/* + * sst28sf040.c: driver for SST28SF040C flash models. + * + * + * Copyright 2000 Silicon Integrated System Corporation + * + * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * Reference: + * 4 MEgabit (512K x 8) SuperFlash EEPROM, SST28SF040 data sheet + * + * $Id$ + */ + +#include +#include "flash.h" +#include "jedec.h" + +#define AUTO_PG_ERASE1 0x20 +#define AUTO_PG_ERASE2 0xD0 +#define AUTO_PGRM 0x10 +#define CHIP_ERASE 0x30 +#define RESET 0xFF +#define READ_ID 0x90 + +static __inline__ void protect_28sf040 (volatile char * bios) +{ + /* ask compiler not to optimize this */ + volatile unsigned char tmp; + + tmp = *(volatile unsigned char *) (bios + 0x1823); + tmp = *(volatile unsigned char *) (bios + 0x1820); + tmp = *(volatile unsigned char *) (bios + 0x1822); + tmp = *(volatile unsigned char *) (bios + 0x0418); + tmp = *(volatile unsigned char *) (bios + 0x041B); + tmp = *(volatile unsigned char *) (bios + 0x0419); + tmp = *(volatile unsigned char *) (bios + 0x040A); +} + +static __inline__ void unprotect_28sf040 (volatile char * bios) +{ + /* ask compiler not to optimize this */ + volatile unsigned char tmp; + + tmp = *(volatile unsigned char *) (bios + 0x1823); + tmp = *(volatile unsigned char *) (bios + 0x1820); + tmp = *(volatile unsigned char *) (bios + 0x1822); + tmp = *(volatile unsigned char *) (bios + 0x0418); + tmp = *(volatile unsigned char *) (bios + 0x041B); + tmp = *(volatile unsigned char *) (bios + 0x0419); + tmp = *(volatile unsigned char *) (bios + 0x041A); +} + +static __inline__ int erase_sector_28sf040 (volatile char * bios, unsigned long address) +{ + *bios = AUTO_PG_ERASE1; + *(bios + address) = AUTO_PG_ERASE2; + + /* wait for Toggle bit ready */ + toggle_ready_jedec(bios); + + return(0); +} + +static __inline__ int write_sector_28sf040(volatile char * bios, unsigned char * src, + volatile unsigned char * dst, unsigned int page_size) +{ + int i; + + for (i = 0; i < page_size; i++) { + /* transfer data from source to destination */ + if (*src == 0xFF) { + dst++, src++; + /* If the data is 0xFF, don't program it */ + continue; + } + /*issue AUTO PROGRAM command */ + *dst = AUTO_PGRM; + *dst++ = *src++; + + /* wait for Toggle bit ready */ + toggle_ready_jedec(bios); + } + + return(0); +} + +int probe_28sf040 (struct flashchip * flash) +{ + volatile char * bios = flash->virt_addr; + unsigned char id1, id2, tmp; + + /* save the value at the beginning of the Flash */ + tmp = *bios; + + *bios = RESET; + myusec_delay(10); + + *bios = READ_ID; + myusec_delay(10); + id1 = *(volatile unsigned char *) bios; + myusec_delay(10); + id2 = *(volatile unsigned char *) (bios + 0x01); + + *bios = RESET; + myusec_delay(10); + + printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__ , id1, id2); + if (id1 == flash->manufacture_id && id2 == flash->model_id) + return 1; + + /* if there is no SST28SF040, restore the original value */ + *bios = tmp; + return 0; +} + +int erase_28sf040 (struct flashchip * flash) +{ + volatile char * bios = flash->virt_addr; + + unprotect_28sf040 (bios); + *bios = CHIP_ERASE; + *bios = CHIP_ERASE; + protect_28sf040 (bios); + + myusec_delay(10); + toggle_ready_jedec(bios); + + return(0); +} + +int write_28sf040 (struct flashchip * flash, unsigned char * buf) +{ + int i; + int total_size = flash->total_size * 1024, page_size = flash->page_size; + volatile char * bios = flash->virt_addr; + + unprotect_28sf040 (bios); + + printf ("Programming Page: "); + for (i = 0; i < total_size/page_size; i++) { + /* erase the page before programming */ + erase_sector_28sf040(bios, i * page_size); + + /* write to the sector */ + printf ("%04d at address: 0x%08x", i, i * page_size); + write_sector_28sf040(bios, buf + i * page_size, bios + i * page_size, + page_size); + printf ("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); + } + printf("\n"); + + protect_28sf040 (bios); + + return(0); +} diff --git a/util/flash_and_burn/sst28sf040.h b/util/flash_and_burn/sst28sf040.h new file mode 100644 index 0000000000..ab2641d329 --- /dev/null +++ b/util/flash_and_burn/sst28sf040.h @@ -0,0 +1,8 @@ +#ifndef __SST28SF040_H__ +#define __SST28SF040_H__ + +extern int probe_28sf040 (struct flashchip * flash); +extern int erase_28sf040 (struct flashchip * flash); +extern int write_28sf040 (struct flashchip * flash, unsigned char * buf); + +#endif /* !__SST28SF040_H__ */ diff --git a/util/flash_and_burn/sst39sf020.c b/util/flash_and_burn/sst39sf020.c new file mode 100644 index 0000000000..65fc623825 --- /dev/null +++ b/util/flash_and_burn/sst39sf020.c @@ -0,0 +1,199 @@ +/* + * sst39sf020.c: driver for SST28SF040C flash models. + * + * + * Copyright 2000 Silicon Integrated System Corporation + * + * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * Reference: + * 4 MEgabit (512K x 8) SuperFlash EEPROM, SST28SF040 data sheet + * + * $Id$ + */ + +#include +#include "flash.h" +#include "jedec.h" +#include "sst39sf020.h" + +#define AUTO_PG_ERASE1 0x20 +#define AUTO_PG_ERASE2 0xD0 +#define AUTO_PGRM 0x10 +#define CHIP_ERASE 0x30 +#define RESET 0xFF +#define READ_ID 0x90 + +static __inline__ void protect_39sf020 (volatile char * bios) +{ + /* ask compiler not to optimize this */ + volatile unsigned char tmp; + + tmp = *(volatile unsigned char *) (bios + 0x1823); + tmp = *(volatile unsigned char *) (bios + 0x1820); + tmp = *(volatile unsigned char *) (bios + 0x1822); + tmp = *(volatile unsigned char *) (bios + 0x0418); + tmp = *(volatile unsigned char *) (bios + 0x041B); + tmp = *(volatile unsigned char *) (bios + 0x0419); + tmp = *(volatile unsigned char *) (bios + 0x040A); +} + +static __inline__ void unprotect_39sf020 (volatile char * bios) +{ + /* ask compiler not to optimize this */ + volatile unsigned char tmp; + + tmp = *(volatile unsigned char *) (bios + 0x1823); + tmp = *(volatile unsigned char *) (bios + 0x1820); + tmp = *(volatile unsigned char *) (bios + 0x1822); + tmp = *(volatile unsigned char *) (bios + 0x0418); + tmp = *(volatile unsigned char *) (bios + 0x041B); + tmp = *(volatile unsigned char *) (bios + 0x0419); + tmp = *(volatile unsigned char *) (bios + 0x041A); +} + +static __inline__ int erase_sector_39sf020 (volatile char * bios, unsigned long address) +{ + *bios = AUTO_PG_ERASE1; + *(bios + address) = AUTO_PG_ERASE2; + + /* wait for Toggle bit ready */ + toggle_ready_jedec(bios); + + return(0); +} + +static __inline__ int write_sector_39sf020(volatile char * bios, + unsigned char * src, + volatile unsigned char * dst, + unsigned int page_size) +{ + int i; + volatile char *Temp; + + for (i = 0; i < page_size; i++) { + if (*dst != 0xff) { + printf("FATAL: dst %p not erased (val 0x%x\n", dst, *dst); + return(-1); + } + /* transfer data from source to destination */ + if (*src == 0xFF) { + dst++, src++; + /* If the data is 0xFF, don't program it */ + continue; + } + Temp = (bios + 0x5555); + *Temp = 0xAA; + Temp = bios + 0x2AAA; + *Temp = 0x55; + Temp = bios + 0x5555; + *Temp = 0xA0; + *dst = *src; + toggle_ready_jedec(bios); + if (*dst != *src) + printf("BAD! dst 0x%lx val 0x%x src 0x%x\n", + (unsigned long)dst, *dst, *src); + dst++, src++; + } + + return(0); +} + +int probe_39sf020 (struct flashchip * flash) +{ + volatile char * bios = flash->virt_addr; + unsigned char id1, id2; + + *(volatile char *) (bios + 0x5555) = 0xAA; + myusec_delay(10); + *(volatile char *) (bios + 0x2AAA) = 0x55; + myusec_delay(10); + *(volatile char *) (bios + 0x5555) = 0x90; + + myusec_delay(10); + + id1 = *(volatile unsigned char *) bios; + id2 = *(volatile unsigned char *) (bios + 0x01); + + *(volatile char *) (bios + 0x5555) = 0xAA; + *(volatile char *) (bios + 0x2AAA) = 0x55; + *(volatile char *) (bios + 0x5555) = 0xF0; + + myusec_delay(10); + + printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2); + + if (id1 == flash->manufacture_id && id2 == flash->model_id) + return 1; + + return 0; +} + +int erase_39sf020 (struct flashchip * flash) +{ + volatile unsigned char * bios = flash->virt_addr; + volatile unsigned char *Temp; + + /* Issue the Sector Erase command to 39SF020 */ + Temp = bios + 0x5555; /* set up address to be C000:5555h */ + *Temp = 0xAA; /* write data 0xAA to the address */ + myusec_delay(10); + Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */ + *Temp = 0x55; /* write data 0x55 to the address */ + myusec_delay(10); + Temp = bios + 0x5555; /* set up address to be C000:5555h */ + *Temp = 0x80; /* write data 0x80 to the address */ + myusec_delay(10); + Temp = bios + 0x5555; /* set up address to be C000:5555h */ + *Temp = 0xAA; /* write data 0xAA to the address */ + myusec_delay(10); + Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */ + *Temp = 0x55; /* write data 0x55 to the address */ + myusec_delay(10); + Temp = bios + 0x5555; /* set up address to be C000:5555h */ + *Temp = 0x10; /* write data 0x55 to the address */ + + myusec_delay(50000); + + return(0); +} + +int write_39sf020 (struct flashchip * flash, unsigned char * buf) +{ + int i; + int total_size = flash->total_size * 1024, page_size = flash->page_size; + volatile char * bios = flash->virt_addr; + +// unprotect_39sf020 (bios); + erase_39sf020(flash); + printf ("Programming Page: "); + for (i = 0; i < total_size/page_size; i++) { + /* erase the page before programming */ + //erase_sector_39sf020(bios, i * page_size); + + /* write to the sector */ + printf ("%04d at address: 0x%08x", i, i * page_size); + write_sector_39sf020(bios, buf + i * page_size, bios + i * page_size, + page_size); + printf ("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); + fflush(stdout); + } + printf("\n"); + +// protect_39sf020 (bios); + + return(0); +} diff --git a/util/flash_and_burn/sst39sf020.h b/util/flash_and_burn/sst39sf020.h new file mode 100644 index 0000000000..dfbedfffc7 --- /dev/null +++ b/util/flash_and_burn/sst39sf020.h @@ -0,0 +1,8 @@ +#ifndef __SST39SF020_H__ +#define __SST39SF020_H__ 1 + +extern int probe_39sf020 (struct flashchip * flash); +extern int erase_39sf020 (struct flashchip * flash); +extern int write_39sf020 (struct flashchip * flash, unsigned char * buf); + +#endif /* !__SST39SF020_H__ */ diff --git a/util/flash_and_burn/w49f002u.c b/util/flash_and_burn/w49f002u.c new file mode 100644 index 0000000000..b7c57eb256 --- /dev/null +++ b/util/flash_and_burn/w49f002u.c @@ -0,0 +1,119 @@ +/* + * w49f002u.c: driver for Winbond 49F002U flash models + * + * + * Copyright 2000 Silicon Integrated System Corporation + * + * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * Reference: + * W49F002U data sheet + * + * $Id + */ + +#include +#include "flash.h" +#include "jedec.h" +#include "w49f002u.h" + +int probe_49f002 (struct flashchip * flash) +{ + volatile char * bios = flash->virt_addr; + unsigned char id1, id2; + + *(bios + 0x5555) = 0xAA; + *(bios + 0x2AAA) = 0x55; + *(bios + 0x5555) = 0x90; + + id1 = *(volatile unsigned char *) bios; + id2 = *(volatile unsigned char *) (bios + 0x01); + + *bios = 0xF0; + + myusec_delay(10); + + printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2); + + if (id1 == flash->manufacture_id && id2 == flash->model_id) + return 1; + + return 0; +} + +int erase_49f002 (struct flashchip * flash) +{ + volatile char * bios = flash->virt_addr; + + *(bios + 0x5555) = 0xAA; + *(bios + 0x2AAA) = 0x55; + *(bios + 0x5555) = 0x80; + *(bios + 0x5555) = 0xAA; + *(bios + 0x2AAA) = 0x55; + *(bios + 0x5555) = 0x10; + + myusec_delay(100); + toggle_ready_jedec(bios); + + // while ((*bios & 0x40) != 0x40) + //; + +#if 0 + toggle_ready_jedec(bios); + *(bios + 0x0ffff) = 0x30; + *(bios + 0x1ffff) = 0x30; + *(bios + 0x2ffff) = 0x30; + *(bios + 0x37fff) = 0x30; + *(bios + 0x39fff) = 0x30; + *(bios + 0x3bfff) = 0x30; +#endif + + return(0); +} + +int write_49f002 (struct flashchip * flash, unsigned char * buf) +{ + int i; + int total_size = flash->total_size * 1024; + volatile char * bios = flash->virt_addr; + volatile char * dst = bios; + + *bios = 0xF0; + myusec_delay(10); + erase_49f002(flash); + //*bios = 0xF0; +#if 1 + printf ("Programming Page: "); + for (i = 0; i < total_size; i++) { + /* write to the sector */ + if ((i & 0xfff) == 0) + printf ("address: 0x%08lx", (unsigned long)i); + *(bios + 0x5555) = 0xAA; + *(bios + 0x2AAA) = 0x55; + *(bios + 0x5555) = 0xA0; + *dst++ = *buf++; + + /* wait for Toggle bit ready */ + toggle_ready_jedec(dst); + + if ((i & 0xfff) == 0) + printf ("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); + } +#endif + printf("\n"); + + return(0); +} diff --git a/util/flash_and_burn/w49f002u.h b/util/flash_and_burn/w49f002u.h new file mode 100644 index 0000000000..9705d4247b --- /dev/null +++ b/util/flash_and_burn/w49f002u.h @@ -0,0 +1,8 @@ +#ifndef __W49F002U_H__ +#define __W49F002U_H__ 1 + +extern int probe_49f002 (struct flashchip * flash); +extern int erase_49f002 (struct flashchip * flash); +extern int write_49f002 (struct flashchip * flash, unsigned char * buf); + +#endif /* !__W49F002U_H__ */