blob: 9899e44fa639cf6cfa19fc87fa6f1bb1516ba972 [file] [log] [blame]
David Hendricks3770a112004-03-17 21:47:30 +00001/* sst40lf020.c: driver for SST40LF040 flash models.
2 *
3 *
4 * Copyright 2000 Silicon Integrated System Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 *
21 * Reference:
22 * 4 MEgabit (512K x 8) SuperFlash EEPROM, SST49lF040 data sheet
23 *
24 * $Id$
25 */
26
27
28#include <stdio.h>
29#include "flash.h"
30#include "jedec.h"
31#include "sst49lf040.h"
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
40static __inline__ void protect_49lf040 (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
54static __inline__ void unprotect_49lf040 (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
68int erase_sector_49lf040 (volatile char * bios, unsigned int page)
69{
70 /* Chip erase function does not exist for LPC mode on 49lf040.
71 * Erase sector-by-sector instead. */
72 volatile unsigned char *Temp;
73
74 /* Issue the Sector Erase command to 40LF040 */
75 Temp = bios + 0x5555; /* set up address to be C000:5555h */
76 *Temp = 0xAA; /* write data 0xAA to the address */
77 myusec_delay(10);
78 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
79 *Temp = 0x55; /* write data 0x55 to the address */
80 myusec_delay(10);
81 Temp = bios + 0x5555; /* set up address to be C000:5555h */
82 *Temp = 0x80; /* write data 0x80 to the address */
83 myusec_delay(10);
84 Temp = bios + 0x5555; /* set up address to be C000:5555h */
85 *Temp = 0xAA; /* write data 0xAA to the address */
86 myusec_delay(10);
87 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
88 *Temp = 0x55; /* write data 0x55 to the address */
89 myusec_delay(10);
90 Temp = bios + page; /* set up address to be the current sector */
91 *Temp = 0x30; /* write data 0x30 to the address */
92 myusec_delay(25000);
93
94 /* wait for Toggle bit ready */
95 toggle_ready_jedec(bios);
96 myusec_delay(25000);
97
98 return(0);
99}
100
101static __inline__ int write_sector_49lf040(volatile char * bios,
102 unsigned char * src,
103 volatile unsigned char * dst,
104 unsigned int page_size)
105{
106 int i;
107 volatile char *Temp;
108
109 for (i = 0; i < page_size; i++) {
110 if (*dst != 0xff) {
111 printf("FATAL: dst %p not erased (val 0x%x)\n", dst, *dst);
112 return(-1);
113 }
114 /* transfer data from source to destination */
115 if (*src == 0xFF) {
116 dst++, src++;
117 /* If the data is 0xFF, don't program it */
118 continue;
119 }
120 Temp = (bios + 0x5555);
121 *Temp = 0xAA;
122 Temp = bios + 0x2AAA;
123 *Temp = 0x55;
124 Temp = bios + 0x5555;
125 *Temp = 0xA0;
126 *dst = *src;
127 toggle_ready_jedec(bios);
128
129 data_polling_jedec(dst, *src);
130 if (*dst != *src)
131 printf("BAD! dst 0x%lx val 0x%x src 0x%x\n",
132 (unsigned long)dst, *dst, *src);
133 dst++, src++;
134 }
135
136 return(0);
137}
138
139int probe_49lf040 (struct flashchip * flash)
140{
141 volatile char * bios = flash->virt_addr;
142 unsigned char id1, id2;
143
144 *(volatile char *) (bios + 0x5555) = 0xAA;
145 myusec_delay(10);
146 *(volatile char *) (bios + 0x2AAA) = 0x55;
147 myusec_delay(10);
148 *(volatile char *) (bios + 0x5555) = 0x90;
149
150 myusec_delay(10);
151
152 id1 = *(volatile unsigned char *) bios;
153 id2 = *(volatile unsigned char *) (bios + 0x01);
154
155 *(volatile char *) (bios + 0x5555) = 0xAA;
156 *(volatile char *) (bios + 0x2AAA) = 0x55;
157 *(volatile char *) (bios + 0x5555) = 0xF0;
158
159 myusec_delay(10);
160
161 printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
162
163 if (id1 == flash->manufacture_id && id2 == flash->model_id)
164 return 1;
165
166 return 0;
167}
168/* Chip erase only works in parallel programming mode for the 49lf040.
169 * Use sector-erase instead */
170int erase_49lf040 (struct flashchip * flash)
171{
172 volatile unsigned char * bios = flash->virt_addr;
173 volatile unsigned char *Temp;
174
175 /* Issue the Sector Erase command to 40LF040 */
176 Temp = bios + 0x5555; /* set up address to be C000:5555h */
177 *Temp = 0xAA; /* write data 0xAA to the address */
178 myusec_delay(10);
179 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
180 *Temp = 0x55; /* write data 0x55 to the address */
181 myusec_delay(10);
182 Temp = bios + 0x5555; /* set up address to be C000:5555h */
183 *Temp = 0x80; /* write data 0x80 to the address */
184 myusec_delay(10);
185 Temp = bios + 0x5555; /* set up address to be C000:5555h */
186 *Temp = 0xAA; /* write data 0xAA to the address */
187 myusec_delay(10);
188 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
189 *Temp = 0x55; /* write data 0x55 to the address */
190 myusec_delay(10);
191 Temp = bios + 0x5555; /* set up address to be C000:5555h */
192 *Temp = 0x10; /* write data 0x55 to the address */
193
194 myusec_delay(50000);
195
196 return(0);
197}
198
199int write_49lf040 (struct flashchip * flash, unsigned char * buf)
200{
201 int i;
202 int total_size = flash->total_size * 1024, page_size = flash->page_size;
203 volatile char * bios = flash->virt_addr;
204
205// unprotect_49lf040 (bios);
206// erase_49lf040(flash); /* Must be done sector-by-sector in LPC mode */
207 printf ("Programming Page: ");
208 for (i = 0; i < total_size/page_size; i++) {
209 /* erase the page before programming */
210 erase_sector_49lf040(bios, i * page_size);
211
212 /* write to the sector */
213 printf ("%04d at address: 0x%08x ", i, i * page_size);
214 write_sector_49lf040(bios, buf + i * page_size, bios + i * page_size,
215 page_size);
216 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\b");
217 fflush(stdout);
218 }
219 printf("\n");
220
221// protect_49lf040 (bios);
222
223 return(0);
224}