Ronald G. Minnich | 5e8dfff | 2002-03-21 22:41:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * sst39sf020.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 | |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
Ronald G. Minnich | 5e8dfff | 2002-03-21 22:41:11 +0000 | [diff] [blame] | 29 | #include "flash.h" |
| 30 | #include "jedec.h" |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 31 | #include "sst39sf020.h" |
Ronald G. Minnich | 5e8dfff | 2002-03-21 22:41:11 +0000 | [diff] [blame] | 32 | |
| 33 | #define AUTO_PG_ERASE1 0x20 |
| 34 | #define AUTO_PG_ERASE2 0xD0 |
| 35 | #define AUTO_PGRM 0x10 |
| 36 | #define CHIP_ERASE 0x30 |
| 37 | #define RESET 0xFF |
| 38 | #define READ_ID 0x90 |
| 39 | |
| 40 | static __inline__ void protect_39sf020 (volatile char * bios) |
| 41 | { |
| 42 | /* ask compiler not to optimize this */ |
| 43 | volatile unsigned char tmp; |
| 44 | |
| 45 | tmp = *(volatile unsigned char *) (bios + 0x1823); |
| 46 | tmp = *(volatile unsigned char *) (bios + 0x1820); |
| 47 | tmp = *(volatile unsigned char *) (bios + 0x1822); |
| 48 | tmp = *(volatile unsigned char *) (bios + 0x0418); |
| 49 | tmp = *(volatile unsigned char *) (bios + 0x041B); |
| 50 | tmp = *(volatile unsigned char *) (bios + 0x0419); |
| 51 | tmp = *(volatile unsigned char *) (bios + 0x040A); |
| 52 | } |
| 53 | |
| 54 | static __inline__ void unprotect_39sf020 (volatile char * bios) |
| 55 | { |
| 56 | /* ask compiler not to optimize this */ |
| 57 | volatile unsigned char tmp; |
| 58 | |
| 59 | tmp = *(volatile unsigned char *) (bios + 0x1823); |
| 60 | tmp = *(volatile unsigned char *) (bios + 0x1820); |
| 61 | tmp = *(volatile unsigned char *) (bios + 0x1822); |
| 62 | tmp = *(volatile unsigned char *) (bios + 0x0418); |
| 63 | tmp = *(volatile unsigned char *) (bios + 0x041B); |
| 64 | tmp = *(volatile unsigned char *) (bios + 0x0419); |
| 65 | tmp = *(volatile unsigned char *) (bios + 0x041A); |
| 66 | } |
| 67 | |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 68 | static __inline__ int erase_sector_39sf020 (volatile char * bios, unsigned long address) |
Ronald G. Minnich | 5e8dfff | 2002-03-21 22:41:11 +0000 | [diff] [blame] | 69 | { |
| 70 | *bios = AUTO_PG_ERASE1; |
| 71 | *(bios + address) = AUTO_PG_ERASE2; |
| 72 | |
| 73 | /* wait for Toggle bit ready */ |
| 74 | toggle_ready_jedec(bios); |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 75 | |
| 76 | return(0); |
Ronald G. Minnich | 5e8dfff | 2002-03-21 22:41:11 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 79 | static __inline__ int write_sector_39sf020(volatile char * bios, |
Ronald G. Minnich | 213ee71 | 2002-04-10 16:01:38 +0000 | [diff] [blame] | 80 | unsigned char * src, |
| 81 | volatile unsigned char * dst, |
| 82 | unsigned int page_size) |
Ronald G. Minnich | 5e8dfff | 2002-03-21 22:41:11 +0000 | [diff] [blame] | 83 | { |
| 84 | int i; |
| 85 | volatile char *Temp; |
| 86 | |
| 87 | for (i = 0; i < page_size; i++) { |
Ollie Lho | 6041bcd | 2002-07-18 03:32:00 +0000 | [diff] [blame] | 88 | if (*dst != 0xff) { |
| 89 | printf("FATAL: dst %p not erased (val 0x%x\n", dst, *dst); |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 90 | return(-1); |
Ollie Lho | 6041bcd | 2002-07-18 03:32:00 +0000 | [diff] [blame] | 91 | } |
Ronald G. Minnich | 5e8dfff | 2002-03-21 22:41:11 +0000 | [diff] [blame] | 92 | /* transfer data from source to destination */ |
| 93 | if (*src == 0xFF) { |
| 94 | dst++, src++; |
| 95 | /* If the data is 0xFF, don't program it */ |
| 96 | continue; |
| 97 | } |
Ollie Lho | 6041bcd | 2002-07-18 03:32:00 +0000 | [diff] [blame] | 98 | Temp = (bios + 0x5555); |
| 99 | *Temp = 0xAA; |
| 100 | Temp = bios + 0x2AAA; |
| 101 | *Temp = 0x55; |
| 102 | Temp = bios + 0x5555; |
| 103 | *Temp = 0xA0; |
Ronald G. Minnich | 213ee71 | 2002-04-10 16:01:38 +0000 | [diff] [blame] | 104 | *dst = *src; |
Ronald G. Minnich | 5e8dfff | 2002-03-21 22:41:11 +0000 | [diff] [blame] | 105 | toggle_ready_jedec(bios); |
Ollie Lho | 6041bcd | 2002-07-18 03:32:00 +0000 | [diff] [blame] | 106 | if (*dst != *src) |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 107 | printf("BAD! dst 0x%lx val 0x%x src 0x%x\n", |
| 108 | (unsigned long)dst, *dst, *src); |
Ronald G. Minnich | 213ee71 | 2002-04-10 16:01:38 +0000 | [diff] [blame] | 109 | dst++, src++; |
Ronald G. Minnich | 5e8dfff | 2002-03-21 22:41:11 +0000 | [diff] [blame] | 110 | } |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 111 | |
| 112 | return(0); |
Ronald G. Minnich | 5e8dfff | 2002-03-21 22:41:11 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | int probe_39sf020 (struct flashchip * flash) |
| 116 | { |
| 117 | volatile char * bios = flash->virt_addr; |
| 118 | unsigned char id1, id2; |
| 119 | |
| 120 | *(volatile char *) (bios + 0x5555) = 0xAA; |
| 121 | myusec_delay(10); |
| 122 | *(volatile char *) (bios + 0x2AAA) = 0x55; |
| 123 | myusec_delay(10); |
| 124 | *(volatile char *) (bios + 0x5555) = 0x90; |
| 125 | |
| 126 | myusec_delay(10); |
| 127 | |
| 128 | id1 = *(volatile unsigned char *) bios; |
| 129 | id2 = *(volatile unsigned char *) (bios + 0x01); |
| 130 | |
| 131 | *(volatile char *) (bios + 0x5555) = 0xAA; |
| 132 | *(volatile char *) (bios + 0x2AAA) = 0x55; |
| 133 | *(volatile char *) (bios + 0x5555) = 0xF0; |
| 134 | |
| 135 | myusec_delay(10); |
| 136 | |
Ronald G. Minnich | d4228fd | 2003-02-28 17:21:38 +0000 | [diff] [blame] | 137 | printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2); |
| 138 | |
Ronald G. Minnich | 5e8dfff | 2002-03-21 22:41:11 +0000 | [diff] [blame] | 139 | if (id1 == flash->manufacture_id && id2 == flash->model_id) |
| 140 | return 1; |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | int erase_39sf020 (struct flashchip * flash) |
| 146 | { |
Ronald G. Minnich | 213ee71 | 2002-04-10 16:01:38 +0000 | [diff] [blame] | 147 | volatile unsigned char * bios = flash->virt_addr; |
Ollie Lho | 6041bcd | 2002-07-18 03:32:00 +0000 | [diff] [blame] | 148 | volatile unsigned char *Temp; |
| 149 | |
Ronald G. Minnich | 213ee71 | 2002-04-10 16:01:38 +0000 | [diff] [blame] | 150 | /* Issue the Sector Erase command to 39SF020 */ |
Ronald G. Minnich | 213ee71 | 2002-04-10 16:01:38 +0000 | [diff] [blame] | 151 | Temp = bios + 0x5555; /* set up address to be C000:5555h */ |
| 152 | *Temp = 0xAA; /* write data 0xAA to the address */ |
Ronald G. Minnich | 5e8dfff | 2002-03-21 22:41:11 +0000 | [diff] [blame] | 153 | myusec_delay(10); |
Ronald G. Minnich | 213ee71 | 2002-04-10 16:01:38 +0000 | [diff] [blame] | 154 | Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */ |
| 155 | *Temp = 0x55; /* write data 0x55 to the address */ |
| 156 | myusec_delay(10); |
| 157 | Temp = bios + 0x5555; /* set up address to be C000:5555h */ |
| 158 | *Temp = 0x80; /* write data 0x80 to the address */ |
| 159 | myusec_delay(10); |
| 160 | Temp = bios + 0x5555; /* set up address to be C000:5555h */ |
| 161 | *Temp = 0xAA; /* write data 0xAA to the address */ |
| 162 | myusec_delay(10); |
| 163 | Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */ |
| 164 | *Temp = 0x55; /* write data 0x55 to the address */ |
| 165 | myusec_delay(10); |
| 166 | Temp = bios + 0x5555; /* set up address to be C000:5555h */ |
| 167 | *Temp = 0x10; /* write data 0x55 to the address */ |
| 168 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 169 | myusec_delay(50000); |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 170 | |
| 171 | return(0); |
Ronald G. Minnich | 5e8dfff | 2002-03-21 22:41:11 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 174 | int write_39sf020 (struct flashchip * flash, unsigned char * buf) |
Ronald G. Minnich | 5e8dfff | 2002-03-21 22:41:11 +0000 | [diff] [blame] | 175 | { |
| 176 | int i; |
| 177 | int total_size = flash->total_size * 1024, page_size = flash->page_size; |
| 178 | volatile char * bios = flash->virt_addr; |
| 179 | |
| 180 | // unprotect_39sf020 (bios); |
Ronald G. Minnich | 213ee71 | 2002-04-10 16:01:38 +0000 | [diff] [blame] | 181 | erase_39sf020(flash); |
Ronald G. Minnich | 5e8dfff | 2002-03-21 22:41:11 +0000 | [diff] [blame] | 182 | printf ("Programming Page: "); |
| 183 | for (i = 0; i < total_size/page_size; i++) { |
| 184 | /* erase the page before programming */ |
| 185 | //erase_sector_39sf020(bios, i * page_size); |
| 186 | |
| 187 | /* write to the sector */ |
| 188 | printf ("%04d at address: 0x%08x", i, i * page_size); |
| 189 | write_sector_39sf020(bios, buf + i * page_size, bios + i * page_size, |
| 190 | page_size); |
| 191 | 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"); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 192 | fflush(stdout); |
Ronald G. Minnich | 5e8dfff | 2002-03-21 22:41:11 +0000 | [diff] [blame] | 193 | } |
| 194 | printf("\n"); |
| 195 | |
| 196 | // protect_39sf020 (bios); |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 197 | |
| 198 | return(0); |
Ronald G. Minnich | 5e8dfff | 2002-03-21 22:41:11 +0000 | [diff] [blame] | 199 | } |