blob: 5f2503d2ce3f039542b47e63f87aee1ef3a5fa8f [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 */
David Hendricks3770a112004-03-17 21:47:30 +000026#include <stdio.h>
27#include "flash.h"
28#include "jedec.h"
29#include "sst49lf040.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
Ollie Lhocec28792004-03-17 23:03:37 +000038static int erase_sector_49lf040 (volatile char * bios, unsigned int page)
David Hendricks3770a112004-03-17 21:47:30 +000039{
40 /* Chip erase function does not exist for LPC mode on 49lf040.
41 * Erase sector-by-sector instead. */
42 volatile unsigned char *Temp;
43
44 /* Issue the Sector Erase command to 40LF040 */
45 Temp = bios + 0x5555; /* set up address to be C000:5555h */
46 *Temp = 0xAA; /* write data 0xAA to the address */
47 myusec_delay(10);
48 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
49 *Temp = 0x55; /* write data 0x55 to the address */
50 myusec_delay(10);
51 Temp = bios + 0x5555; /* set up address to be C000:5555h */
52 *Temp = 0x80; /* write data 0x80 to the address */
53 myusec_delay(10);
54 Temp = bios + 0x5555; /* set up address to be C000:5555h */
55 *Temp = 0xAA; /* write data 0xAA to the address */
56 myusec_delay(10);
57 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
58 *Temp = 0x55; /* write data 0x55 to the address */
59 myusec_delay(10);
60 Temp = bios + page; /* set up address to be the current sector */
61 *Temp = 0x30; /* write data 0x30 to the address */
62 myusec_delay(25000);
63
64 /* wait for Toggle bit ready */
65 toggle_ready_jedec(bios);
66 myusec_delay(25000);
67
68 return(0);
69}
70
71static __inline__ int write_sector_49lf040(volatile char * bios,
Ollie Lhocec28792004-03-17 23:03:37 +000072 unsigned char * src,
73 volatile unsigned char * dst,
74 unsigned int page_size)
David Hendricks3770a112004-03-17 21:47:30 +000075{
76 int i;
77 volatile char *Temp;
78
79 for (i = 0; i < page_size; i++) {
80 if (*dst != 0xff) {
81 printf("FATAL: dst %p not erased (val 0x%x)\n", dst, *dst);
82 return(-1);
83 }
84 /* transfer data from source to destination */
85 if (*src == 0xFF) {
86 dst++, src++;
87 /* If the data is 0xFF, don't program it */
88 continue;
89 }
90 Temp = (bios + 0x5555);
91 *Temp = 0xAA;
92 Temp = bios + 0x2AAA;
93 *Temp = 0x55;
94 Temp = bios + 0x5555;
95 *Temp = 0xA0;
96 *dst = *src;
97 toggle_ready_jedec(bios);
98
99 data_polling_jedec(dst, *src);
100 if (*dst != *src)
101 printf("BAD! dst 0x%lx val 0x%x src 0x%x\n",
102 (unsigned long)dst, *dst, *src);
103 dst++, src++;
104 }
105
106 return(0);
107}
108
109int probe_49lf040 (struct flashchip * flash)
110{
111 volatile char * bios = flash->virt_addr;
112 unsigned char id1, id2;
113
114 *(volatile char *) (bios + 0x5555) = 0xAA;
115 myusec_delay(10);
116 *(volatile char *) (bios + 0x2AAA) = 0x55;
117 myusec_delay(10);
118 *(volatile char *) (bios + 0x5555) = 0x90;
David Hendricks3770a112004-03-17 21:47:30 +0000119 myusec_delay(10);
120
121 id1 = *(volatile unsigned char *) bios;
122 id2 = *(volatile unsigned char *) (bios + 0x01);
123
124 *(volatile char *) (bios + 0x5555) = 0xAA;
125 *(volatile char *) (bios + 0x2AAA) = 0x55;
126 *(volatile char *) (bios + 0x5555) = 0xF0;
127
128 myusec_delay(10);
129
130 printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
131
132 if (id1 == flash->manufacture_id && id2 == flash->model_id)
133 return 1;
134
135 return 0;
136}
Ollie Lhocec28792004-03-17 23:03:37 +0000137
David Hendricks3770a112004-03-17 21:47:30 +0000138/* Chip erase only works in parallel programming mode for the 49lf040.
139 * Use sector-erase instead */
140int erase_49lf040 (struct flashchip * flash)
141{
142 volatile unsigned char * bios = flash->virt_addr;
143 volatile unsigned char *Temp;
144
145 /* Issue the Sector Erase command to 40LF040 */
146 Temp = bios + 0x5555; /* set up address to be C000:5555h */
147 *Temp = 0xAA; /* write data 0xAA to the address */
148 myusec_delay(10);
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 */
David Hendricks3770a112004-03-17 21:47:30 +0000163 myusec_delay(50000);
164
165 return(0);
166}
167
168int write_49lf040 (struct flashchip * flash, unsigned char * buf)
169{
170 int i;
171 int total_size = flash->total_size * 1024, page_size = flash->page_size;
172 volatile char * bios = flash->virt_addr;
173
David Hendricks3770a112004-03-17 21:47:30 +0000174 printf ("Programming Page: ");
175 for (i = 0; i < total_size/page_size; i++) {
176 /* erase the page before programming */
177 erase_sector_49lf040(bios, i * page_size);
178
179 /* write to the sector */
180 printf ("%04d at address: 0x%08x ", i, i * page_size);
181 write_sector_49lf040(bios, buf + i * page_size, bios + i * page_size,
182 page_size);
183 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");
184 fflush(stdout);
185 }
186 printf("\n");
187
David Hendricks3770a112004-03-17 21:47:30 +0000188 return(0);
189}