blob: 48ef44084f629472f8f9941e0796726a59f45898 [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 */
David Hendricks3770a112004-03-17 21:47:30 +000064
65 /* wait for Toggle bit ready */
66 toggle_ready_jedec(bios);
David Hendricks3770a112004-03-17 21:47:30 +000067
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);
David Hendricks3770a112004-03-17 21:47:30 +000098 if (*dst != *src)
99 printf("BAD! dst 0x%lx val 0x%x src 0x%x\n",
100 (unsigned long)dst, *dst, *src);
101 dst++, src++;
102 }
103
104 return(0);
105}
106
107int probe_49lf040 (struct flashchip * flash)
108{
109 volatile char * bios = flash->virt_addr;
110 unsigned char id1, id2;
111
112 *(volatile char *) (bios + 0x5555) = 0xAA;
113 myusec_delay(10);
114 *(volatile char *) (bios + 0x2AAA) = 0x55;
115 myusec_delay(10);
116 *(volatile char *) (bios + 0x5555) = 0x90;
David Hendricks3770a112004-03-17 21:47:30 +0000117 myusec_delay(10);
118
119 id1 = *(volatile unsigned char *) bios;
120 id2 = *(volatile unsigned char *) (bios + 0x01);
121
122 *(volatile char *) (bios + 0x5555) = 0xAA;
123 *(volatile char *) (bios + 0x2AAA) = 0x55;
124 *(volatile char *) (bios + 0x5555) = 0xF0;
125
126 myusec_delay(10);
127
128 printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
129
130 if (id1 == flash->manufacture_id && id2 == flash->model_id)
131 return 1;
132
133 return 0;
134}
Ollie Lhocec28792004-03-17 23:03:37 +0000135
David Hendricks3770a112004-03-17 21:47:30 +0000136/* Chip erase only works in parallel programming mode for the 49lf040.
137 * Use sector-erase instead */
138int erase_49lf040 (struct flashchip * flash)
139{
140 volatile unsigned char * bios = flash->virt_addr;
141 volatile unsigned char *Temp;
142
143 /* Issue the Sector Erase command to 40LF040 */
144 Temp = bios + 0x5555; /* set up address to be C000:5555h */
145 *Temp = 0xAA; /* write data 0xAA to the address */
146 myusec_delay(10);
147 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
148 *Temp = 0x55; /* write data 0x55 to the address */
149 myusec_delay(10);
150 Temp = bios + 0x5555; /* set up address to be C000:5555h */
151 *Temp = 0x80; /* write data 0x80 to the address */
152 myusec_delay(10);
153 Temp = bios + 0x5555; /* set up address to be C000:5555h */
154 *Temp = 0xAA; /* write data 0xAA to the address */
155 myusec_delay(10);
156 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
157 *Temp = 0x55; /* write data 0x55 to the address */
158 myusec_delay(10);
159 Temp = bios + 0x5555; /* set up address to be C000:5555h */
160 *Temp = 0x10; /* write data 0x55 to the address */
David Hendricks3770a112004-03-17 21:47:30 +0000161 myusec_delay(50000);
162
163 return(0);
164}
165
166int write_49lf040 (struct flashchip * flash, unsigned char * buf)
167{
168 int i;
169 int total_size = flash->total_size * 1024, page_size = flash->page_size;
170 volatile char * bios = flash->virt_addr;
171
David Hendricks3770a112004-03-17 21:47:30 +0000172 printf ("Programming Page: ");
173 for (i = 0; i < total_size/page_size; i++) {
174 /* erase the page before programming */
175 erase_sector_49lf040(bios, i * page_size);
176
177 /* write to the sector */
178 printf ("%04d at address: 0x%08x ", i, i * page_size);
179 write_sector_49lf040(bios, buf + i * page_size, bios + i * page_size,
180 page_size);
181 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");
182 fflush(stdout);
183 }
184 printf("\n");
185
David Hendricks3770a112004-03-17 21:47:30 +0000186 return(0);
187}