blob: 49e695f890c2b74e3700e48f165286ef6cb7f10e [file] [log] [blame]
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +00001/*
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"
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
38static __inline__ void protect_39sf020 (volatile char * bios)
39{
40 /* ask compiler not to optimize this */
41 volatile unsigned char tmp;
42
43 tmp = *(volatile unsigned char *) (bios + 0x1823);
44 tmp = *(volatile unsigned char *) (bios + 0x1820);
45 tmp = *(volatile unsigned char *) (bios + 0x1822);
46 tmp = *(volatile unsigned char *) (bios + 0x0418);
47 tmp = *(volatile unsigned char *) (bios + 0x041B);
48 tmp = *(volatile unsigned char *) (bios + 0x0419);
49 tmp = *(volatile unsigned char *) (bios + 0x040A);
50}
51
52static __inline__ void unprotect_39sf020 (volatile char * bios)
53{
54 /* ask compiler not to optimize this */
55 volatile unsigned char tmp;
56
57 tmp = *(volatile unsigned char *) (bios + 0x1823);
58 tmp = *(volatile unsigned char *) (bios + 0x1820);
59 tmp = *(volatile unsigned char *) (bios + 0x1822);
60 tmp = *(volatile unsigned char *) (bios + 0x0418);
61 tmp = *(volatile unsigned char *) (bios + 0x041B);
62 tmp = *(volatile unsigned char *) (bios + 0x0419);
63 tmp = *(volatile unsigned char *) (bios + 0x041A);
64}
65
66static __inline__ erase_sector_39sf020 (volatile char * bios, unsigned long address)
67{
68 *bios = AUTO_PG_ERASE1;
69 *(bios + address) = AUTO_PG_ERASE2;
70
71 /* wait for Toggle bit ready */
72 toggle_ready_jedec(bios);
73}
74
Ronald G. Minnich213ee712002-04-10 16:01:38 +000075static __inline__ write_sector_39sf020(volatile char * bios,
76 unsigned char * src,
77 volatile unsigned char * dst,
78 unsigned int page_size)
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +000079{
80 int i;
81 volatile char *Temp;
82
83 for (i = 0; i < page_size; i++) {
Ronald G. Minnich213ee712002-04-10 16:01:38 +000084 if (*dst != 0xff) {
85 printf("FATAL: dst %p not erased (val 0x%x\n", dst, *dst);
86 return;
87 }
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +000088 /* transfer data from source to destination */
89 if (*src == 0xFF) {
90 dst++, src++;
91 /* If the data is 0xFF, don't program it */
92 continue;
93 }
94 Temp = (bios + 0x5555);
95 *Temp = 0xAA;
96 Temp = bios + 0x2AAA;
97 *Temp = 0x55;
98 Temp = bios + 0x5555;
99 *Temp = 0xA0;
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000100 *dst = *src;
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000101 toggle_ready_jedec(bios);
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000102 if (*dst != *src) printf("BAD! dst 0x%x val 0x%x src 0x%x\n",
103 dst, *dst, *src);
104 dst++, src++;
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000105 }
106}
107
108int probe_39sf020 (struct flashchip * flash)
109{
110 volatile char * bios = flash->virt_addr;
111 unsigned char id1, id2;
112
113 *(volatile char *) (bios + 0x5555) = 0xAA;
114 myusec_delay(10);
115 *(volatile char *) (bios + 0x2AAA) = 0x55;
116 myusec_delay(10);
117 *(volatile char *) (bios + 0x5555) = 0x90;
118
119 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(__FUNCTION__ "id1 %d, id2 %d\n", id1, id2);
131 if (id1 == flash->manufacture_id && id2 == flash->model_id)
132 return 1;
133
134 return 0;
135}
136
137int erase_39sf020 (struct flashchip * flash)
138{
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000139 volatile unsigned char * bios = flash->virt_addr;
140 volatile unsigned char *Temp;
141 /* Issue the Sector Erase command to 39SF020 */
142 printf(__FUNCTION__ " bios is %p\n", bios);
143 Temp = bios + 0x5555; /* set up address to be C000:5555h */
144 *Temp = 0xAA; /* write data 0xAA to the address */
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000145 myusec_delay(10);
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000146 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
147 *Temp = 0x55; /* write data 0x55 to the address */
148 myusec_delay(10);
149 Temp = bios + 0x5555; /* set up address to be C000:5555h */
150 *Temp = 0x80; /* write data 0x80 to the address */
151 myusec_delay(10);
152 Temp = bios + 0x5555; /* set up address to be C000:5555h */
153 *Temp = 0xAA; /* write data 0xAA to the address */
154 myusec_delay(10);
155 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
156 *Temp = 0x55; /* write data 0x55 to the address */
157 myusec_delay(10);
158 Temp = bios + 0x5555; /* set up address to be C000:5555h */
159 *Temp = 0x10; /* write data 0x55 to the address */
160
161 myusec_delay(20000);
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000162}
163
164int write_39sf020 (struct flashchip * flash, char * buf)
165{
166 int i;
167 int total_size = flash->total_size * 1024, page_size = flash->page_size;
168 volatile char * bios = flash->virt_addr;
169
170// unprotect_39sf020 (bios);
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000171 erase_39sf020(flash);
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000172 printf ("Programming Page: ");
173 for (i = 0; i < total_size/page_size; i++) {
174 /* erase the page before programming */
175 //erase_sector_39sf020(bios, i * page_size);
176
177 /* write to the sector */
178 printf ("%04d at address: 0x%08x", i, i * page_size);
179 write_sector_39sf020(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");
182 }
183 printf("\n");
184
185// protect_39sf020 (bios);
186}