blob: 48a1d596a6cebe213496abeee6895525b43c41ad [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
131 printf(__FUNCTION__ "id1 %d, id2 %d\n", id1, id2);
132 if (id1 == flash->manufacture_id && id2 == flash->model_id)
133 return 1;
134
135 return 0;
136}
137
138int erase_39sf020 (struct flashchip * flash)
139{
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000140 volatile unsigned char * bios = flash->virt_addr;
Ollie Lho6041bcd2002-07-18 03:32:00 +0000141 volatile unsigned char *Temp;
142
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000143 /* Issue the Sector Erase command to 39SF020 */
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000144 Temp = bios + 0x5555; /* set up address to be C000:5555h */
145 *Temp = 0xAA; /* write data 0xAA to the address */
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000146 myusec_delay(10);
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000147 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 */
161
162 myusec_delay(20000);
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000163}
164
165int write_39sf020 (struct flashchip * flash, char * buf)
166{
167 int i;
168 int total_size = flash->total_size * 1024, page_size = flash->page_size;
169 volatile char * bios = flash->virt_addr;
170
171// unprotect_39sf020 (bios);
Ronald G. Minnich213ee712002-04-10 16:01:38 +0000172 erase_39sf020(flash);
Ronald G. Minnich5e8dfff2002-03-21 22:41:11 +0000173 printf ("Programming Page: ");
174 for (i = 0; i < total_size/page_size; i++) {
175 /* erase the page before programming */
176 //erase_sector_39sf020(bios, i * page_size);
177
178 /* write to the sector */
179 printf ("%04d at address: 0x%08x", i, i * page_size);
180 write_sector_39sf020(bios, buf + i * page_size, bios + i * page_size,
181 page_size);
182 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");
183 }
184 printf("\n");
185
186// protect_39sf020 (bios);
187}