blob: 9f754857aaf55315b120e64cd24e9a7aac1999be [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 *
Ollie Lhocf29de82004-03-18 19:40:07 +000024 * ToDo: Consilidated to standard JEDEC code.
25 *
David Hendricks3770a112004-03-17 21:47:30 +000026 * $Id$
27 */
David Hendricks3770a112004-03-17 21:47:30 +000028#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
Ollie Lhocec28792004-03-17 23:03:37 +000040static int erase_sector_49lf040 (volatile char * bios, unsigned int page)
David Hendricks3770a112004-03-17 21:47:30 +000041{
42 /* Chip erase function does not exist for LPC mode on 49lf040.
43 * Erase sector-by-sector instead. */
44 volatile unsigned char *Temp;
45
46 /* Issue the Sector Erase command to 40LF040 */
47 Temp = bios + 0x5555; /* set up address to be C000:5555h */
48 *Temp = 0xAA; /* write data 0xAA to the address */
49 myusec_delay(10);
50 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
51 *Temp = 0x55; /* write data 0x55 to the address */
52 myusec_delay(10);
53 Temp = bios + 0x5555; /* set up address to be C000:5555h */
54 *Temp = 0x80; /* write data 0x80 to the address */
55 myusec_delay(10);
56 Temp = bios + 0x5555; /* set up address to be C000:5555h */
57 *Temp = 0xAA; /* write data 0xAA to the address */
58 myusec_delay(10);
59 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
60 *Temp = 0x55; /* write data 0x55 to the address */
61 myusec_delay(10);
62 Temp = bios + page; /* set up address to be the current sector */
63 *Temp = 0x30; /* write data 0x30 to the address */
Ollie Lhocf29de82004-03-18 19:40:07 +000064 myusec_delay(50000);
David Hendricks3770a112004-03-17 21:47:30 +000065
66 /* wait for Toggle bit ready */
67 toggle_ready_jedec(bios);
68 myusec_delay(25000);
69
70 return(0);
71}
72
73static __inline__ int write_sector_49lf040(volatile char * bios,
Ollie Lhocec28792004-03-17 23:03:37 +000074 unsigned char * src,
75 volatile unsigned char * dst,
76 unsigned int page_size)
David Hendricks3770a112004-03-17 21:47:30 +000077{
78 int i;
79 volatile char *Temp;
80
81 for (i = 0; i < page_size; i++) {
82 if (*dst != 0xff) {
83 printf("FATAL: dst %p not erased (val 0x%x)\n", dst, *dst);
84 return(-1);
85 }
86 /* transfer data from source to destination */
87 if (*src == 0xFF) {
88 dst++, src++;
89 /* If the data is 0xFF, don't program it */
90 continue;
91 }
92 Temp = (bios + 0x5555);
93 *Temp = 0xAA;
94 Temp = bios + 0x2AAA;
95 *Temp = 0x55;
96 Temp = bios + 0x5555;
97 *Temp = 0xA0;
98 *dst = *src;
99 toggle_ready_jedec(bios);
100
101 data_polling_jedec(dst, *src);
102 if (*dst != *src)
103 printf("BAD! dst 0x%lx val 0x%x src 0x%x\n",
104 (unsigned long)dst, *dst, *src);
105 dst++, src++;
106 }
107
108 return(0);
109}
110
111int probe_49lf040 (struct flashchip * flash)
112{
113 volatile char * bios = flash->virt_addr;
114 unsigned char id1, id2;
115
116 *(volatile char *) (bios + 0x5555) = 0xAA;
117 myusec_delay(10);
118 *(volatile char *) (bios + 0x2AAA) = 0x55;
119 myusec_delay(10);
120 *(volatile char *) (bios + 0x5555) = 0x90;
David Hendricks3770a112004-03-17 21:47:30 +0000121 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
132 printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
133
134 if (id1 == flash->manufacture_id && id2 == flash->model_id)
135 return 1;
136
137 return 0;
138}
Ollie Lhocec28792004-03-17 23:03:37 +0000139
David Hendricks3770a112004-03-17 21:47:30 +0000140/* Chip erase only works in parallel programming mode for the 49lf040.
141 * Use sector-erase instead */
142int erase_49lf040 (struct flashchip * flash)
143{
144 volatile unsigned char * bios = flash->virt_addr;
145 volatile unsigned char *Temp;
146
147 /* Issue the Sector Erase command to 40LF040 */
148 Temp = bios + 0x5555; /* set up address to be C000:5555h */
149 *Temp = 0xAA; /* write data 0xAA to the address */
150 myusec_delay(10);
151 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
152 *Temp = 0x55; /* write data 0x55 to the address */
153 myusec_delay(10);
154 Temp = bios + 0x5555; /* set up address to be C000:5555h */
155 *Temp = 0x80; /* write data 0x80 to the address */
156 myusec_delay(10);
157 Temp = bios + 0x5555; /* set up address to be C000:5555h */
158 *Temp = 0xAA; /* write data 0xAA to the address */
159 myusec_delay(10);
160 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
161 *Temp = 0x55; /* write data 0x55 to the address */
162 myusec_delay(10);
163 Temp = bios + 0x5555; /* set up address to be C000:5555h */
164 *Temp = 0x10; /* write data 0x55 to the address */
David Hendricks3770a112004-03-17 21:47:30 +0000165 myusec_delay(50000);
166
167 return(0);
168}
169
170int write_49lf040 (struct flashchip * flash, unsigned char * buf)
171{
172 int i;
173 int total_size = flash->total_size * 1024, page_size = flash->page_size;
174 volatile char * bios = flash->virt_addr;
175
David Hendricks3770a112004-03-17 21:47:30 +0000176 printf ("Programming Page: ");
177 for (i = 0; i < total_size/page_size; i++) {
178 /* erase the page before programming */
179 erase_sector_49lf040(bios, i * page_size);
180
181 /* write to the sector */
182 printf ("%04d at address: 0x%08x ", i, i * page_size);
183 write_sector_49lf040(bios, buf + i * page_size, bios + i * page_size,
184 page_size);
185 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");
186 fflush(stdout);
187 }
188 printf("\n");
189
David Hendricks3770a112004-03-17 21:47:30 +0000190 return(0);
191}