blob: 7e0853a06fe675c64063311eb13e73f7c9ddcd9d [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++) {
Ollie Lho6041bcd2002-07-18 03:32:00 +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 }
Ollie Lho6041bcd2002-07-18 03:32:00 +000094 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);
Ollie Lho6041bcd2002-07-18 03:32:00 +0000102 if (*dst != *src)
103 printf("BAD! dst 0x%x val 0x%x src 0x%x\n",
104 dst, *dst, *src);
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000105 dst++, src++;
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000106 }
107}
108
109int probe_39sf020 (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;
119
120 myusec_delay(10);
121
122 id1 = *(volatile unsigned char *) bios;
123 id2 = *(volatile unsigned char *) (bios + 0x01);
124
125 *(volatile char *) (bios + 0x5555) = 0xAA;
126 *(volatile char *) (bios + 0x2AAA) = 0x55;
127 *(volatile char *) (bios + 0x5555) = 0xF0;
128
129 myusec_delay(10);
130
Ronald G. Minnichd4228fd2003-02-28 17:21:38 +0000131 printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
132
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000133 if (id1 == flash->manufacture_id && id2 == flash->model_id)
134 return 1;
135
136 return 0;
137}
138
139int erase_39sf020 (struct flashchip * flash)
140{
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000141 volatile unsigned char * bios = flash->virt_addr;
Ollie Lho6041bcd2002-07-18 03:32:00 +0000142 volatile unsigned char *Temp;
143
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000144 /* Issue the Sector Erase command to 39SF020 */
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000145 Temp = bios + 0x5555; /* set up address to be C000:5555h */
146 *Temp = 0xAA; /* write data 0xAA to the address */
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000147 myusec_delay(10);
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000148 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
149 *Temp = 0x55; /* write data 0x55 to the address */
150 myusec_delay(10);
151 Temp = bios + 0x5555; /* set up address to be C000:5555h */
152 *Temp = 0x80; /* write data 0x80 to the address */
153 myusec_delay(10);
154 Temp = bios + 0x5555; /* set up address to be C000:5555h */
155 *Temp = 0xAA; /* write data 0xAA to the address */
156 myusec_delay(10);
157 Temp = bios + 0x2AAA; /* set up address to be C000:2AAAh */
158 *Temp = 0x55; /* write data 0x55 to the address */
159 myusec_delay(10);
160 Temp = bios + 0x5555; /* set up address to be C000:5555h */
161 *Temp = 0x10; /* write data 0x55 to the address */
162
163 myusec_delay(20000);
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000164}
165
166int write_39sf020 (struct flashchip * flash, 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
172// unprotect_39sf020 (bios);
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000173 erase_39sf020(flash);
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000174 printf ("Programming Page: ");
175 for (i = 0; i < total_size/page_size; i++) {
176 /* erase the page before programming */
177 //erase_sector_39sf020(bios, i * page_size);
178
179 /* write to the sector */
180 printf ("%04d at address: 0x%08x", i, i * page_size);
181 write_sector_39sf020(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");
184 }
185 printf("\n");
186
187// protect_39sf020 (bios);
188}