Ronald G. Minnich | 5e5f75e | 2002-01-29 18:21:41 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * sst28sf040.c: driver for SST28SF040C flash models. |
| 3 | * |
| 4 | * |
| 5 | * Copyright 2000 Silicon Integrated System Corporation |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | * |
| 21 | * |
| 22 | * Reference: |
| 23 | * 4 MEgabit (512K x 8) SuperFlash EEPROM, SST28SF040 data sheet |
| 24 | * |
| 25 | * $Id$ |
| 26 | */ |
| 27 | |
| 28 | #include "flash.h" |
| 29 | #include "jedec.h" |
| 30 | |
| 31 | #define AUTO_PG_ERASE1 0x20 |
| 32 | #define AUTO_PG_ERASE2 0xD0 |
| 33 | #define AUTO_PGRM 0x10 |
| 34 | #define CHIP_ERASE 0x30 |
| 35 | #define RESET 0xFF |
| 36 | #define READ_ID 0x90 |
| 37 | |
| 38 | static __inline__ void protect_28sf040 (char * bios) |
| 39 | { |
| 40 | /* ask compiler not to optimize this */ |
| 41 | volatile unsigned char tmp; |
| 42 | |
| 43 | tmp = *(unsigned char *) (bios + 0x1823); |
| 44 | tmp = *(unsigned char *) (bios + 0x1820); |
| 45 | tmp = *(unsigned char *) (bios + 0x1822); |
| 46 | tmp = *(unsigned char *) (bios + 0x0418); |
| 47 | tmp = *(unsigned char *) (bios + 0x041B); |
| 48 | tmp = *(unsigned char *) (bios + 0x0419); |
| 49 | tmp = *(unsigned char *) (bios + 0x040A); |
| 50 | } |
| 51 | |
| 52 | static __inline__ void unprotect_28sf040 (char * bios) |
| 53 | { |
| 54 | /* ask compiler not to optimize this */ |
| 55 | volatile unsigned char tmp; |
| 56 | |
| 57 | tmp = *(unsigned char *) (bios + 0x1823); |
| 58 | tmp = *(unsigned char *) (bios + 0x1820); |
| 59 | tmp = *(unsigned char *) (bios + 0x1822); |
| 60 | tmp = *(unsigned char *) (bios + 0x0418); |
| 61 | tmp = *(unsigned char *) (bios + 0x041B); |
| 62 | tmp = *(unsigned char *) (bios + 0x0419); |
| 63 | tmp = *(unsigned char *) (bios + 0x041A); |
| 64 | } |
| 65 | |
| 66 | static __inline__ erase_sector_28sf040 (char * bios, unsigned long address) |
| 67 | { |
| 68 | *bios = AUTO_PG_ERASE1; |
| 69 | *(bios + address) = AUTO_PG_ERASE2; |
| 70 | |
| 71 | /* wait for Toggle bit ready */ |
| 72 | toggle_ready_jedec(bios); |
| 73 | } |
| 74 | |
| 75 | static __inline__ write_sector_28sf040(char * bios, unsigned char * src, |
| 76 | unsigned char * dst, unsigned int page_size) |
| 77 | { |
| 78 | int i; |
| 79 | |
| 80 | for (i = 0; i < page_size; i++) { |
| 81 | /* transfer data from source to destination */ |
| 82 | if (*src == 0xFF) { |
| 83 | dst++, src++; |
| 84 | /* If the data is 0xFF, don't program it */ |
| 85 | continue; |
| 86 | } |
| 87 | /*issue AUTO PROGRAM command */ |
| 88 | *dst = AUTO_PGRM; |
| 89 | *dst++ = *src++; |
| 90 | |
| 91 | /* wait for Toggle bit ready */ |
| 92 | toggle_ready_jedec(bios); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | int probe_28sf040 (struct flashchip * flash) |
| 97 | { |
| 98 | char * bios = flash->virt_addr; |
| 99 | unsigned char id1, id2, tmp; |
| 100 | |
| 101 | /* save the value at the beginning of the Flash */ |
| 102 | tmp = *bios; |
| 103 | |
| 104 | *bios = RESET; |
| 105 | usleep(10); |
| 106 | |
| 107 | *bios = READ_ID; |
| 108 | usleep(10); |
| 109 | id1 = *(unsigned char *) bios; |
| 110 | usleep(10); |
| 111 | id2 = *(unsigned char *) (bios + 0x01); |
| 112 | |
| 113 | *bios = RESET; |
| 114 | usleep(10); |
| 115 | |
| 116 | if (id1 == flash->manufacture_id && id2 == flash->model_id) |
| 117 | return 1; |
| 118 | |
| 119 | /* if there is no SST28SF040, restore the original value */ |
| 120 | *bios = tmp; |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | int erase_28sf040 (struct flashchip * flash) |
| 125 | { |
| 126 | char * bios = flash->virt_addr; |
| 127 | |
| 128 | unprotect_28sf040 (bios); |
| 129 | *bios = CHIP_ERASE; |
| 130 | *bios = CHIP_ERASE; |
| 131 | protect_28sf040 (bios); |
| 132 | |
| 133 | usleep(10); |
| 134 | toggle_ready_jedec(bios); |
| 135 | } |
| 136 | |
| 137 | int write_28sf040 (struct flashchip * flash, char * buf) |
| 138 | { |
| 139 | int i; |
| 140 | int total_size = flash->total_size * 1024, page_size = flash->page_size; |
| 141 | char * bios = flash->virt_addr; |
| 142 | |
| 143 | unprotect_28sf040 (bios); |
| 144 | |
| 145 | printf ("Programming Page: "); |
| 146 | for (i = 0; i < total_size/page_size; i++) { |
| 147 | /* erase the page before programming */ |
| 148 | erase_sector_28sf040(bios, i * page_size); |
| 149 | |
| 150 | /* write to the sector */ |
| 151 | printf ("%04d at address: 0x%08x", i, i * page_size); |
| 152 | write_sector_28sf040(bios, buf + i * page_size, bios + i * page_size, |
| 153 | page_size); |
| 154 | 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"); |
| 155 | } |
| 156 | printf("\n"); |
| 157 | |
| 158 | protect_28sf040 (bios); |
| 159 | } |