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